vm: fix a null dereference on out-of-memory
[minix.git] / lib / liblwip / core / raw.c
blob325990249810fb86bd44cc526b45e7ea07ed42f3
1 /**
2 * @file
3 * Implementation of raw protocol PCBs for low-level handling of
4 * different types of protocols besides (or overriding) those
5 * already available in lwIP.
7 */
9 /*
10 * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
11 * All rights reserved.
13 * Redistribution and use in source and binary forms, with or without modification,
14 * are permitted provided that the following conditions are met:
16 * 1. Redistributions of source code must retain the above copyright notice,
17 * this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright notice,
19 * this list of conditions and the following disclaimer in the documentation
20 * and/or other materials provided with the distribution.
21 * 3. The name of the author may not be used to endorse or promote products
22 * derived from this software without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
25 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
26 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
27 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
28 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
29 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
32 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
33 * OF SUCH DAMAGE.
35 * This file is part of the lwIP TCP/IP stack.
37 * Author: Adam Dunkels <adam@sics.se>
41 #include "lwip/opt.h"
43 #if LWIP_RAW /* don't build if not configured for use in lwipopts.h */
45 #include "lwip/def.h"
46 #include "lwip/memp.h"
47 #include "lwip/ip_addr.h"
48 #include "lwip/netif.h"
49 #include "lwip/raw.h"
50 #include "lwip/stats.h"
51 #include "arch/perf.h"
53 #include <string.h>
55 /** The list of RAW PCBs */
56 static struct raw_pcb *raw_pcbs;
58 /**
59 * Determine if in incoming IP packet is covered by a RAW PCB
60 * and if so, pass it to a user-provided receive callback function.
62 * Given an incoming IP datagram (as a chain of pbufs) this function
63 * finds a corresponding RAW PCB and calls the corresponding receive
64 * callback function.
66 * @param p pbuf to be demultiplexed to a RAW PCB.
67 * @param inp network interface on which the datagram was received.
68 * @return - 1 if the packet has been eaten by a RAW PCB receive
69 * callback function. The caller MAY NOT not reference the
70 * packet any longer, and MAY NOT call pbuf_free().
71 * @return - 0 if packet is not eaten (pbuf is still referenced by the
72 * caller).
75 u8_t
76 raw_input(struct pbuf *p, struct netif *inp)
78 struct raw_pcb *pcb, *prev;
79 struct ip_hdr *iphdr;
80 s16_t proto;
81 u8_t eaten = 0;
83 LWIP_UNUSED_ARG(inp);
85 iphdr = (struct ip_hdr *)p->payload;
86 proto = IPH_PROTO(iphdr);
88 prev = NULL;
89 pcb = raw_pcbs;
90 /* loop through all raw pcbs until the packet is eaten by one */
91 /* this allows multiple pcbs to match against the packet by design */
92 while ((eaten == 0) && (pcb != NULL)) {
93 if ((pcb->protocol == IP_PROTO_ANY || pcb->protocol == proto) &&
94 (ip_addr_isany(&pcb->local_ip) ||
95 ip_addr_cmp(&(pcb->local_ip), &current_iphdr_dest))) {
96 #if IP_SOF_BROADCAST_RECV
97 /* broadcast filter? */
98 if ((pcb->so_options & SOF_BROADCAST) || !ip_addr_isbroadcast(&current_iphdr_dest, inp))
99 #endif /* IP_SOF_BROADCAST_RECV */
101 /* receive callback function available? */
102 if (pcb->recv != NULL) {
103 /* the receive callback function did not eat the packet? */
104 if (pcb->recv(pcb->recv_arg, pcb, p, ip_current_src_addr()) != 0) {
105 /* receive function ate the packet */
106 p = NULL;
107 eaten = 1;
108 if (prev != NULL) {
109 /* move the pcb to the front of raw_pcbs so that is
110 found faster next time */
111 prev->next = pcb->next;
112 pcb->next = raw_pcbs;
113 raw_pcbs = pcb;
117 /* no receive callback function was set for this raw PCB */
119 /* drop the packet */
121 prev = pcb;
122 pcb = pcb->next;
124 return eaten;
128 * Bind a RAW PCB.
130 * @param pcb RAW PCB to be bound with a local address ipaddr.
131 * @param ipaddr local IP address to bind with. Use IP_ADDR_ANY to
132 * bind to all local interfaces.
134 * @return lwIP error code.
135 * - ERR_OK. Successful. No error occured.
136 * - ERR_USE. The specified IP address is already bound to by
137 * another RAW PCB.
139 * @see raw_disconnect()
141 err_t
142 raw_bind(struct raw_pcb *pcb, ip_addr_t *ipaddr)
144 ip_addr_set(&pcb->local_ip, ipaddr);
145 return ERR_OK;
149 * Connect an RAW PCB. This function is required by upper layers
150 * of lwip. Using the raw api you could use raw_sendto() instead
152 * This will associate the RAW PCB with the remote address.
154 * @param pcb RAW PCB to be connected with remote address ipaddr and port.
155 * @param ipaddr remote IP address to connect with.
157 * @return lwIP error code
159 * @see raw_disconnect() and raw_sendto()
161 err_t
162 raw_connect(struct raw_pcb *pcb, ip_addr_t *ipaddr)
164 ip_addr_set(&pcb->remote_ip, ipaddr);
165 return ERR_OK;
170 * Set the callback function for received packets that match the
171 * raw PCB's protocol and binding.
173 * The callback function MUST either
174 * - eat the packet by calling pbuf_free() and returning non-zero. The
175 * packet will not be passed to other raw PCBs or other protocol layers.
176 * - not free the packet, and return zero. The packet will be matched
177 * against further PCBs and/or forwarded to another protocol layers.
179 * @return non-zero if the packet was free()d, zero if the packet remains
180 * available for others.
182 void
183 raw_recv(struct raw_pcb *pcb, raw_recv_fn recv, void *recv_arg)
185 /* remember recv() callback and user data */
186 pcb->recv = recv;
187 pcb->recv_arg = recv_arg;
191 * Send the raw IP packet to the given address. Note that actually you cannot
192 * modify the IP headers (this is inconsistent with the receive callback where
193 * you actually get the IP headers), you can only specify the IP payload here.
194 * It requires some more changes in lwIP. (there will be a raw_send() function
195 * then.)
197 * @param pcb the raw pcb which to send
198 * @param p the IP payload to send
199 * @param ipaddr the destination address of the IP packet
202 err_t
203 raw_sendto(struct raw_pcb *pcb, struct pbuf *p, ip_addr_t *ipaddr)
205 err_t err;
206 struct netif *netif;
207 ip_addr_t *src_ip;
208 struct pbuf *q; /* q will be sent down the stack */
210 LWIP_DEBUGF(RAW_DEBUG | LWIP_DBG_TRACE, ("raw_sendto\n"));
212 /* not enough space to add an IP header to first pbuf in given p chain? */
213 if (pbuf_header(p, IP_HLEN)) {
214 /* allocate header in new pbuf */
215 q = pbuf_alloc(PBUF_IP, 0, PBUF_RAM);
216 /* new header pbuf could not be allocated? */
217 if (q == NULL) {
218 LWIP_DEBUGF(RAW_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_SERIOUS, ("raw_sendto: could not allocate header\n"));
219 return ERR_MEM;
221 /* chain header q in front of given pbuf p */
222 pbuf_chain(q, p);
223 /* { first pbuf q points to header pbuf } */
224 LWIP_DEBUGF(RAW_DEBUG, ("raw_sendto: added header pbuf %p before given pbuf %p\n", (void *)q, (void *)p));
225 } else {
226 /* first pbuf q equals given pbuf */
227 q = p;
228 if(pbuf_header(q, -IP_HLEN)) {
229 LWIP_ASSERT("Can't restore header we just removed!", 0);
230 return ERR_MEM;
234 if ((netif = ip_route(ipaddr)) == NULL) {
235 LWIP_DEBUGF(RAW_DEBUG | LWIP_DBG_LEVEL_WARNING, ("raw_sendto: No route to %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n",
236 ip4_addr1_16(ipaddr), ip4_addr2_16(ipaddr), ip4_addr3_16(ipaddr), ip4_addr4_16(ipaddr)));
237 /* free any temporary header pbuf allocated by pbuf_header() */
238 if (q != p) {
239 pbuf_free(q);
241 return ERR_RTE;
244 #if IP_SOF_BROADCAST
245 /* broadcast filter? */
246 if (((pcb->so_options & SOF_BROADCAST) == 0) && ip_addr_isbroadcast(ipaddr, netif)) {
247 LWIP_DEBUGF(RAW_DEBUG | LWIP_DBG_LEVEL_WARNING, ("raw_sendto: SOF_BROADCAST not enabled on pcb %p\n", (void *)pcb));
248 /* free any temporary header pbuf allocated by pbuf_header() */
249 if (q != p) {
250 pbuf_free(q);
252 return ERR_VAL;
254 #endif /* IP_SOF_BROADCAST */
256 if (ip_addr_isany(&pcb->local_ip)) {
257 /* use outgoing network interface IP address as source address */
258 src_ip = &(netif->ip_addr);
259 } else {
260 /* use RAW PCB local IP address as source address */
261 src_ip = &(pcb->local_ip);
264 #if LWIP_NETIF_HWADDRHINT
265 netif->addr_hint = &(pcb->addr_hint);
266 #endif /* LWIP_NETIF_HWADDRHINT*/
267 err = ip_output_if (q, src_ip, ipaddr, pcb->ttl, pcb->tos, pcb->protocol, netif);
268 #if LWIP_NETIF_HWADDRHINT
269 netif->addr_hint = NULL;
270 #endif /* LWIP_NETIF_HWADDRHINT*/
272 /* did we chain a header earlier? */
273 if (q != p) {
274 /* free the header */
275 pbuf_free(q);
277 return err;
281 * Send the raw IP packet to the address given by raw_connect()
283 * @param pcb the raw pcb which to send
284 * @param p the IP payload to send
287 err_t
288 raw_send(struct raw_pcb *pcb, struct pbuf *p)
290 return raw_sendto(pcb, p, &pcb->remote_ip);
294 * Remove an RAW PCB.
296 * @param pcb RAW PCB to be removed. The PCB is removed from the list of
297 * RAW PCB's and the data structure is freed from memory.
299 * @see raw_new()
301 void
302 raw_remove(struct raw_pcb *pcb)
304 struct raw_pcb *pcb2;
305 /* pcb to be removed is first in list? */
306 if (raw_pcbs == pcb) {
307 /* make list start at 2nd pcb */
308 raw_pcbs = raw_pcbs->next;
309 /* pcb not 1st in list */
310 } else {
311 for(pcb2 = raw_pcbs; pcb2 != NULL; pcb2 = pcb2->next) {
312 /* find pcb in raw_pcbs list */
313 if (pcb2->next != NULL && pcb2->next == pcb) {
314 /* remove pcb from list */
315 pcb2->next = pcb->next;
319 memp_free(MEMP_RAW_PCB, pcb);
323 * Create a RAW PCB.
325 * @return The RAW PCB which was created. NULL if the PCB data structure
326 * could not be allocated.
328 * @param proto the protocol number of the IPs payload (e.g. IP_PROTO_ICMP)
330 * @see raw_remove()
332 struct raw_pcb *
333 raw_new(u8_t proto)
335 struct raw_pcb *pcb;
337 LWIP_DEBUGF(RAW_DEBUG | LWIP_DBG_TRACE, ("raw_new\n"));
339 pcb = (struct raw_pcb *)memp_malloc(MEMP_RAW_PCB);
340 /* could allocate RAW PCB? */
341 if (pcb != NULL) {
342 /* initialize PCB to all zeroes */
343 memset(pcb, 0, sizeof(struct raw_pcb));
344 pcb->protocol = proto;
345 pcb->ttl = RAW_TTL;
346 pcb->next = raw_pcbs;
347 raw_pcbs = pcb;
349 return pcb;
352 #endif /* LWIP_RAW */