Back to home page

Quest Cross Reference

 
 

    


Warning, cross-references for /kernel/lwip/core/raw.c need to be fixed.

0001 /**
0002  * @file
0003  * Implementation of raw protocol PCBs for low-level handling of
0004  * different types of protocols besides (or overriding) those
0005  * already available in lwIP.
0006  *
0007  */
0008 
0009 /*
0010  * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
0011  * All rights reserved.
0012  *
0013  * Redistribution and use in source and binary forms, with or without modification,
0014  * are permitted provided that the following conditions are met:
0015  *
0016  * 1. Redistributions of source code must retain the above copyright notice,
0017  *    this list of conditions and the following disclaimer.
0018  * 2. Redistributions in binary form must reproduce the above copyright notice,
0019  *    this list of conditions and the following disclaimer in the documentation
0020  *    and/or other materials provided with the distribution.
0021  * 3. The name of the author may not be used to endorse or promote products
0022  *    derived from this software without specific prior written permission.
0023  *
0024  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
0025  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
0026  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
0027  * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
0028  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
0029  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0030  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0031  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
0032  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
0033  * OF SUCH DAMAGE.
0034  *
0035  * This file is part of the lwIP TCP/IP stack.
0036  *
0037  * Author: Adam Dunkels <adam@sics.se>
0038  *
0039  */
0040 
0041 #include "lwip/opt.h"
0042 
0043 #if LWIP_RAW /* don't build if not configured for use in lwipopts.h */
0044 
0045 #include "lwip/def.h"
0046 #include "lwip/memp.h"
0047 #include "lwip/inet.h"
0048 #include "lwip/ip_addr.h"
0049 #include "lwip/netif.h"
0050 #include "lwip/raw.h"
0051 #include "lwip/stats.h"
0052 #include "lwip/snmp.h"
0053 #include "arch/perf.h"
0054 
0055 #include <string.h>
0056 
0057 /** The list of RAW PCBs */
0058 static struct raw_pcb *raw_pcbs;
0059 
0060 /**
0061  * Determine if in incoming IP packet is covered by a RAW PCB
0062  * and if so, pass it to a user-provided receive callback function.
0063  *
0064  * Given an incoming IP datagram (as a chain of pbufs) this function
0065  * finds a corresponding RAW PCB and calls the corresponding receive
0066  * callback function.
0067  *
0068  * @param p pbuf to be demultiplexed to a RAW PCB.
0069  * @param inp network interface on which the datagram was received.
0070  * @return - 1 if the packet has been eaten by a RAW PCB receive
0071  *           callback function. The caller MAY NOT not reference the
0072  *           packet any longer, and MAY NOT call pbuf_free().
0073  * @return - 0 if packet is not eaten (pbuf is still referenced by the
0074  *           caller).
0075  *
0076  */
0077 u8_t
0078 raw_input(struct pbuf *p, struct netif *inp)
0079 {
0080   struct raw_pcb *pcb, *prev;
0081   struct ip_hdr *iphdr;
0082   s16_t proto;
0083   u8_t eaten = 0;
0084 
0085   LWIP_UNUSED_ARG(inp);
0086 
0087   iphdr = p->payload;
0088   proto = IPH_PROTO(iphdr);
0089 
0090   prev = NULL;
0091   pcb = raw_pcbs;
0092   /* loop through all raw pcbs until the packet is eaten by one */
0093   /* this allows multiple pcbs to match against the packet by design */
0094   while ((eaten == 0) && (pcb != NULL)) {
0095     if (pcb->protocol == proto) {
0096 #if IP_SOF_BROADCAST_RECV
0097       /* broadcast filter? */
0098       if ((pcb->so_options & SOF_BROADCAST) || !ip_addr_isbroadcast(&(iphdr->dest), inp))
0099 #endif /* IP_SOF_BROADCAST_RECV */
0100       {
0101         /* receive callback function available? */
0102         if (pcb->recv != NULL) {
0103           /* the receive callback function did not eat the packet? */
0104           if (pcb->recv(pcb->recv_arg, pcb, p, &(iphdr->src)) != 0) {
0105             /* receive function ate the packet */
0106             p = NULL;
0107             eaten = 1;
0108             if (prev != NULL) {
0109             /* move the pcb to the front of raw_pcbs so that is
0110                found faster next time */
0111               prev->next = pcb->next;
0112               pcb->next = raw_pcbs;
0113               raw_pcbs = pcb;
0114             }
0115           }
0116         }
0117         /* no receive callback function was set for this raw PCB */
0118       }
0119       /* drop the packet */
0120     }
0121     prev = pcb;
0122     pcb = pcb->next;
0123   }
0124   return eaten;
0125 }
0126 
0127 /**
0128  * Bind a RAW PCB.
0129  *
0130  * @param pcb RAW PCB to be bound with a local address ipaddr.
0131  * @param ipaddr local IP address to bind with. Use IP_ADDR_ANY to
0132  * bind to all local interfaces.
0133  *
0134  * @return lwIP error code.
0135  * - ERR_OK. Successful. No error occured.
0136  * - ERR_USE. The specified IP address is already bound to by
0137  * another RAW PCB.
0138  *
0139  * @see raw_disconnect()
0140  */
0141 err_t
0142 raw_bind(struct raw_pcb *pcb, struct ip_addr *ipaddr)
0143 {
0144   ip_addr_set(&pcb->local_ip, ipaddr);
0145   return ERR_OK;
0146 }
0147 
0148 /**
0149  * Connect an RAW PCB. This function is required by upper layers
0150  * of lwip. Using the raw api you could use raw_sendto() instead
0151  *
0152  * This will associate the RAW PCB with the remote address.
0153  *
0154  * @param pcb RAW PCB to be connected with remote address ipaddr and port.
0155  * @param ipaddr remote IP address to connect with.
0156  *
0157  * @return lwIP error code
0158  *
0159  * @see raw_disconnect() and raw_sendto()
0160  */
0161 err_t
0162 raw_connect(struct raw_pcb *pcb, struct ip_addr *ipaddr)
0163 {
0164   ip_addr_set(&pcb->remote_ip, ipaddr);
0165   return ERR_OK;
0166 }
0167 
0168 
0169 /**
0170  * Set the callback function for received packets that match the
0171  * raw PCB's protocol and binding. 
0172  * 
0173  * The callback function MUST either
0174  * - eat the packet by calling pbuf_free() and returning non-zero. The
0175  *   packet will not be passed to other raw PCBs or other protocol layers.
0176  * - not free the packet, and return zero. The packet will be matched
0177  *   against further PCBs and/or forwarded to another protocol layers.
0178  * 
0179  * @return non-zero if the packet was free()d, zero if the packet remains
0180  * available for others.
0181  */
0182 void
0183 raw_recv(struct raw_pcb *pcb,
0184          u8_t (* recv)(void *arg, struct raw_pcb *upcb, struct pbuf *p,
0185                       struct ip_addr *addr),
0186          void *recv_arg)
0187 {
0188   /* remember recv() callback and user data */
0189   pcb->recv = recv;
0190   pcb->recv_arg = recv_arg;
0191 }
0192 
0193 /**
0194  * Send the raw IP packet to the given address. Note that actually you cannot
0195  * modify the IP headers (this is inconsistent with the receive callback where
0196  * you actually get the IP headers), you can only specify the IP payload here.
0197  * It requires some more changes in lwIP. (there will be a raw_send() function
0198  * then.)
0199  *
0200  * @param pcb the raw pcb which to send
0201  * @param p the IP payload to send
0202  * @param ipaddr the destination address of the IP packet
0203  *
0204  */
0205 err_t
0206 raw_sendto(struct raw_pcb *pcb, struct pbuf *p, struct ip_addr *ipaddr)
0207 {
0208   err_t err;
0209   struct netif *netif;
0210   struct ip_addr *src_ip;
0211   struct pbuf *q; /* q will be sent down the stack */
0212   
0213   LWIP_DEBUGF(RAW_DEBUG | LWIP_DBG_TRACE, ("raw_sendto\n"));
0214   
0215   /* not enough space to add an IP header to first pbuf in given p chain? */
0216   if (pbuf_header(p, IP_HLEN)) {
0217     /* allocate header in new pbuf */
0218     q = pbuf_alloc(PBUF_IP, 0, PBUF_RAM);
0219     /* new header pbuf could not be allocated? */
0220     if (q == NULL) {
0221       LWIP_DEBUGF(RAW_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_SERIOUS, ("raw_sendto: could not allocate header\n"));
0222       return ERR_MEM;
0223     }
0224     /* chain header q in front of given pbuf p */
0225     pbuf_chain(q, p);
0226     /* { first pbuf q points to header pbuf } */
0227     LWIP_DEBUGF(RAW_DEBUG, ("raw_sendto: added header pbuf %p before given pbuf %p\n", (void *)q, (void *)p));
0228   }  else {
0229     /* first pbuf q equals given pbuf */
0230     q = p;
0231     if(pbuf_header(q, -IP_HLEN)) {
0232       LWIP_ASSERT("Can't restore header we just removed!", 0);
0233       return ERR_MEM;
0234     }
0235   }
0236 
0237   if ((netif = ip_route(ipaddr)) == NULL) {
0238     LWIP_DEBUGF(RAW_DEBUG | LWIP_DBG_LEVEL_WARNING, ("raw_sendto: No route to 0x%"X32_F"\n", ipaddr->addr));
0239     /* free any temporary header pbuf allocated by pbuf_header() */
0240     if (q != p) {
0241       pbuf_free(q);
0242     }
0243     return ERR_RTE;
0244   }
0245 
0246 #if IP_SOF_BROADCAST
0247   /* broadcast filter? */
0248   if ( ((pcb->so_options & SOF_BROADCAST) == 0) && ip_addr_isbroadcast(ipaddr, netif) ) {
0249     LWIP_DEBUGF(RAW_DEBUG | LWIP_DBG_LEVEL_WARNING, ("raw_sendto: SOF_BROADCAST not enabled on pcb %p\n", (void *)pcb));
0250     /* free any temporary header pbuf allocated by pbuf_header() */
0251     if (q != p) {
0252       pbuf_free(q);
0253     }
0254     return ERR_VAL;
0255   }
0256 #endif /* IP_SOF_BROADCAST */
0257 
0258   if (ip_addr_isany(&pcb->local_ip)) {
0259     /* use outgoing network interface IP address as source address */
0260     src_ip = &(netif->ip_addr);
0261   } else {
0262     /* use RAW PCB local IP address as source address */
0263     src_ip = &(pcb->local_ip);
0264   }
0265 
0266 #if LWIP_NETIF_HWADDRHINT
0267   netif->addr_hint = &(pcb->addr_hint);
0268 #endif /* LWIP_NETIF_HWADDRHINT*/
0269   err = ip_output_if (q, src_ip, ipaddr, pcb->ttl, pcb->tos, pcb->protocol, netif);
0270 #if LWIP_NETIF_HWADDRHINT
0271   netif->addr_hint = NULL;
0272 #endif /* LWIP_NETIF_HWADDRHINT*/
0273 
0274   /* did we chain a header earlier? */
0275   if (q != p) {
0276     /* free the header */
0277     pbuf_free(q);
0278   }
0279   return err;
0280 }
0281 
0282 /**
0283  * Send the raw IP packet to the address given by raw_connect()
0284  *
0285  * @param pcb the raw pcb which to send
0286  * @param p the IP payload to send
0287  *
0288  */
0289 err_t
0290 raw_send(struct raw_pcb *pcb, struct pbuf *p)
0291 {
0292   return raw_sendto(pcb, p, &pcb->remote_ip);
0293 }
0294 
0295 /**
0296  * Remove an RAW PCB.
0297  *
0298  * @param pcb RAW PCB to be removed. The PCB is removed from the list of
0299  * RAW PCB's and the data structure is freed from memory.
0300  *
0301  * @see raw_new()
0302  */
0303 void
0304 raw_remove(struct raw_pcb *pcb)
0305 {
0306   struct raw_pcb *pcb2;
0307   /* pcb to be removed is first in list? */
0308   if (raw_pcbs == pcb) {
0309     /* make list start at 2nd pcb */
0310     raw_pcbs = raw_pcbs->next;
0311     /* pcb not 1st in list */
0312   } else {
0313     for(pcb2 = raw_pcbs; pcb2 != NULL; pcb2 = pcb2->next) {
0314       /* find pcb in raw_pcbs list */
0315       if (pcb2->next != NULL && pcb2->next == pcb) {
0316         /* remove pcb from list */
0317         pcb2->next = pcb->next;
0318       }
0319     }
0320   }
0321   memp_free(MEMP_RAW_PCB, pcb);
0322 }
0323 
0324 /**
0325  * Create a RAW PCB.
0326  *
0327  * @return The RAW PCB which was created. NULL if the PCB data structure
0328  * could not be allocated.
0329  *
0330  * @param proto the protocol number of the IPs payload (e.g. IP_PROTO_ICMP)
0331  *
0332  * @see raw_remove()
0333  */
0334 struct raw_pcb *
0335 raw_new(u8_t proto) {
0336   struct raw_pcb *pcb;
0337 
0338   LWIP_DEBUGF(RAW_DEBUG | LWIP_DBG_TRACE, ("raw_new\n"));
0339 
0340   pcb = memp_malloc(MEMP_RAW_PCB);
0341   /* could allocate RAW PCB? */
0342   if (pcb != NULL) {
0343     /* initialize PCB to all zeroes */
0344     memset(pcb, 0, sizeof(struct raw_pcb));
0345     pcb->protocol = proto;
0346     pcb->ttl = RAW_TTL;
0347     pcb->next = raw_pcbs;
0348     raw_pcbs = pcb;
0349   }
0350   return pcb;
0351 }
0352 
0353 #endif /* LWIP_RAW */