STM32F769IDiscovery  1.00
uDANTE Audio Networking with STM32F7 DISCO board
raw.c
Go to the documentation of this file.
1 
9 /*
10  * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
11  * All rights reserved.
12  *
13  * Redistribution and use in source and binary forms, with or without modification,
14  * are permitted provided that the following conditions are met:
15  *
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.
23  *
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.
34  *
35  * This file is part of the lwIP TCP/IP stack.
36  *
37  * Author: Adam Dunkels <adam@sics.se>
38  *
39  */
40 
41 #include "lwip/opt.h"
42 
43 #if LWIP_RAW /* don't build if not configured for use in lwipopts.h */
44 
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 "lwip/ip6.h"
52 #include "lwip/ip6_addr.h"
53 #include "lwip/inet_chksum.h"
54 
55 #include <string.h>
56 
58 static struct raw_pcb *raw_pcbs;
59 
77 u8_t
78 raw_input(struct pbuf *p, struct netif *inp)
79 {
80  struct raw_pcb *pcb, *prev;
81  s16_t proto;
82  u8_t eaten = 0;
83 
84  LWIP_UNUSED_ARG(inp);
85 
86 #if LWIP_IPV6
87 #if LWIP_IPV4
88  if (IP_HDR_GET_VERSION(p->payload) == 6)
89 #endif /* LWIP_IPV4 */
90  {
91  struct ip6_hdr *ip6hdr = (struct ip6_hdr *)p->payload;
92  proto = IP6H_NEXTH(ip6hdr);
93  }
94 #if LWIP_IPV4
95  else
96 #endif /* LWIP_IPV4 */
97 #endif /* LWIP_IPV6 */
98 #if LWIP_IPV4
99  {
100  proto = IPH_PROTO((struct ip_hdr *)p->payload);
101  }
102 #endif /* LWIP_IPV4 */
103 
104  prev = NULL;
105  pcb = raw_pcbs;
106  /* loop through all raw pcbs until the packet is eaten by one */
107  /* this allows multiple pcbs to match against the packet by design */
108  while ((eaten == 0) && (pcb != NULL)) {
109  if ((pcb->protocol == proto) && IP_PCB_IPVER_INPUT_MATCH(pcb) &&
110  (ip_addr_isany(&pcb->local_ip) ||
111  ip_addr_cmp(&pcb->local_ip, ip_current_dest_addr()))) {
112 #if IP_SOF_BROADCAST_RECV
113  /* broadcast filter? */
115  || PCB_ISIPV6(pcb)
116  )
117 #endif /* IP_SOF_BROADCAST_RECV */
118  {
119  /* receive callback function available? */
120  if (pcb->recv != NULL) {
121 #ifndef LWIP_NOASSERT
122  void* old_payload = p->payload;
123 #endif
124  /* the receive callback function did not eat the packet? */
125  eaten = pcb->recv(pcb->recv_arg, pcb, p, ip_current_src_addr());
126  if (eaten != 0) {
127  /* receive function ate the packet */
128  p = NULL;
129  eaten = 1;
130  if (prev != NULL) {
131  /* move the pcb to the front of raw_pcbs so that is
132  found faster next time */
133  prev->next = pcb->next;
134  pcb->next = raw_pcbs;
135  raw_pcbs = pcb;
136  }
137  } else {
138  /* sanity-check that the receive callback did not alter the pbuf */
139  LWIP_ASSERT("raw pcb recv callback altered pbuf payload pointer without eating packet",
140  p->payload == old_payload);
141  }
142  }
143  /* no receive callback function was set for this raw PCB */
144  }
145  /* drop the packet */
146  }
147  prev = pcb;
148  pcb = pcb->next;
149  }
150  return eaten;
151 }
152 
167 err_t
168 raw_bind(struct raw_pcb *pcb, const ip_addr_t *ipaddr)
169 {
170  if ((pcb == NULL) || (ipaddr == NULL) || !IP_ADDR_PCB_VERSION_MATCH(pcb, ipaddr)) {
171  return ERR_VAL;
172  }
173  ip_addr_set_ipaddr(&pcb->local_ip, ipaddr);
174  return ERR_OK;
175 }
176 
190 err_t
191 raw_connect(struct raw_pcb *pcb, const ip_addr_t *ipaddr)
192 {
193  if ((pcb == NULL) || (ipaddr == NULL) || !IP_ADDR_PCB_VERSION_MATCH(pcb, ipaddr)) {
194  return ERR_VAL;
195  }
196  ip_addr_set_ipaddr(&pcb->remote_ip, ipaddr);
197  return ERR_OK;
198 }
199 
213 void
214 raw_recv(struct raw_pcb *pcb, raw_recv_fn recv, void *recv_arg)
215 {
216  /* remember recv() callback and user data */
217  pcb->recv = recv;
218  pcb->recv_arg = recv_arg;
219 }
220 
233 err_t
234 raw_sendto(struct raw_pcb *pcb, struct pbuf *p, const ip_addr_t *ipaddr)
235 {
236  err_t err;
237  struct netif *netif;
238  const ip_addr_t *src_ip;
239  struct pbuf *q; /* q will be sent down the stack */
240  s16_t header_size;
241  const ip_addr_t *dst_ip = ipaddr;
242 
243  if ((pcb == NULL) || (ipaddr == NULL) || !IP_ADDR_PCB_VERSION_MATCH(pcb, ipaddr)) {
244  return ERR_VAL;
245  }
246 
247  LWIP_DEBUGF(RAW_DEBUG | LWIP_DBG_TRACE, ("raw_sendto\n"));
248 
249  header_size = (
250 #if LWIP_IPV4 && LWIP_IPV6
251  PCB_ISIPV6(pcb) ? IP6_HLEN : IP_HLEN);
252 #elif LWIP_IPV4
253  IP_HLEN);
254 #else
255  IP6_HLEN);
256 #endif
257 
258  /* not enough space to add an IP header to first pbuf in given p chain? */
259  if (pbuf_header(p, header_size)) {
260  /* allocate header in new pbuf */
261  q = pbuf_alloc(PBUF_IP, 0, PBUF_RAM);
262  /* new header pbuf could not be allocated? */
263  if (q == NULL) {
264  LWIP_DEBUGF(RAW_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_SERIOUS, ("raw_sendto: could not allocate header\n"));
265  return ERR_MEM;
266  }
267  if (p->tot_len != 0) {
268  /* chain header q in front of given pbuf p */
269  pbuf_chain(q, p);
270  }
271  /* { first pbuf q points to header pbuf } */
272  LWIP_DEBUGF(RAW_DEBUG, ("raw_sendto: added header pbuf %p before given pbuf %p\n", (void *)q, (void *)p));
273  } else {
274  /* first pbuf q equals given pbuf */
275  q = p;
276  if (pbuf_header(q, -header_size)) {
277  LWIP_ASSERT("Can't restore header we just removed!", 0);
278  return ERR_MEM;
279  }
280  }
281 
282  netif = ip_route(PCB_ISIPV6(pcb), &pcb->local_ip, dst_ip);
283  if (netif == NULL) {
284  LWIP_DEBUGF(RAW_DEBUG | LWIP_DBG_LEVEL_WARNING, ("raw_sendto: No route to "));
286  /* free any temporary header pbuf allocated by pbuf_header() */
287  if (q != p) {
288  pbuf_free(q);
289  }
290  return ERR_RTE;
291  }
292 
293 #if IP_SOF_BROADCAST
294  if (!PCB_ISIPV6(pcb))
295  {
296  /* broadcast filter? */
297  if (!ip_get_option(pcb, SOF_BROADCAST) && ip_addr_isbroadcast(ipaddr, netif)) {
298  LWIP_DEBUGF(RAW_DEBUG | LWIP_DBG_LEVEL_WARNING, ("raw_sendto: SOF_BROADCAST not enabled on pcb %p\n", (void *)pcb));
299  /* free any temporary header pbuf allocated by pbuf_header() */
300  if (q != p) {
301  pbuf_free(q);
302  }
303  return ERR_VAL;
304  }
305  }
306 #endif /* IP_SOF_BROADCAST */
307 
308  if (ip_addr_isany(&pcb->local_ip)) {
309  /* use outgoing network interface IP address as source address */
310  src_ip = ip_netif_get_local_ip(PCB_ISIPV6(pcb), netif, dst_ip);
311 #if LWIP_IPV6
312  if (src_ip == NULL) {
313  if (q != p) {
314  pbuf_free(q);
315  }
316  return ERR_RTE;
317  }
318 #endif /* LWIP_IPV6 */
319  } else {
320  /* use RAW PCB local IP address as source address */
321  src_ip = &pcb->local_ip;
322  }
323 
324 #if LWIP_IPV6
325  /* If requested, based on the IPV6_CHECKSUM socket option per RFC3542,
326  compute the checksum and update the checksum in the payload. */
327  if (PCB_ISIPV6(pcb) && pcb->chksum_reqd) {
328  u16_t chksum = ip6_chksum_pseudo(p, pcb->protocol, p->tot_len, ip_2_ip6(src_ip), ip_2_ip6(dst_ip));
329  LWIP_ASSERT("Checksum must fit into first pbuf", p->len >= (pcb->chksum_offset + 2));
330  SMEMCPY(((u8_t *)p->payload) + pcb->chksum_offset, &chksum, sizeof(u16_t));
331  }
332 #endif
333 
334  NETIF_SET_HWADDRHINT(netif, &pcb->addr_hint);
335  err = ip_output_if(PCB_ISIPV6(pcb), q, src_ip, dst_ip, pcb->ttl, pcb->tos, pcb->protocol, netif);
336  NETIF_SET_HWADDRHINT(netif, NULL);
337 
338  /* did we chain a header earlier? */
339  if (q != p) {
340  /* free the header */
341  pbuf_free(q);
342  }
343  return err;
344 }
345 
353 err_t
354 raw_send(struct raw_pcb *pcb, struct pbuf *p)
355 {
356  return raw_sendto(pcb, p, &pcb->remote_ip);
357 }
358 
367 void
368 raw_remove(struct raw_pcb *pcb)
369 {
370  struct raw_pcb *pcb2;
371  /* pcb to be removed is first in list? */
372  if (raw_pcbs == pcb) {
373  /* make list start at 2nd pcb */
374  raw_pcbs = raw_pcbs->next;
375  /* pcb not 1st in list */
376  } else {
377  for (pcb2 = raw_pcbs; pcb2 != NULL; pcb2 = pcb2->next) {
378  /* find pcb in raw_pcbs list */
379  if (pcb2->next != NULL && pcb2->next == pcb) {
380  /* remove pcb from list */
381  pcb2->next = pcb->next;
382  break;
383  }
384  }
385  }
386  memp_free(MEMP_RAW_PCB, pcb);
387 }
388 
399 struct raw_pcb *
400 raw_new(u8_t proto)
401 {
402  struct raw_pcb *pcb;
403 
404  LWIP_DEBUGF(RAW_DEBUG | LWIP_DBG_TRACE, ("raw_new\n"));
405 
406  pcb = (struct raw_pcb *)memp_malloc(MEMP_RAW_PCB);
407  /* could allocate RAW PCB? */
408  if (pcb != NULL) {
409  /* initialize PCB to all zeroes */
410  memset(pcb, 0, sizeof(struct raw_pcb));
411  pcb->protocol = proto;
412  pcb->ttl = RAW_TTL;
413  pcb->next = raw_pcbs;
414  raw_pcbs = pcb;
415  }
416  return pcb;
417 }
418 
419 #if LWIP_IPV6
420 
431 struct raw_pcb *
432 raw_new_ip6(u8_t proto)
433 {
434  struct raw_pcb *pcb;
435  pcb = raw_new(proto);
436 #if LWIP_IPV4
437  ip_set_v6(pcb, 1);
438 #endif /* LWIP_IPV4 */
439  return pcb;
440 }
441 #endif /* LWIP_IPV6 */
442 
443 #endif /* LWIP_RAW */
444 
#define ip_current_netif()
Definition: ip.h:154
#define LWIP_DBG_LEVEL_SERIOUS
Definition: debug.h:47
#define ip_addr_isany(ipaddr)
Definition: ip_addr.h:215
#define ip_2_ip6(ipaddr)
Definition: ip_addr.h:200
#define LWIP_DBG_LEVEL_WARNING
Definition: debug.h:46
signed short s16_t
Definition: cc.h:41
u8_t pbuf_header(struct pbuf *p, s16_t header_size_increment)
Definition: pbuf.c:603
#define ERR_VAL
Definition: err.h:58
#define RAW_TTL
Definition: opt.h:730
#define SMEMCPY(dst, src, len)
Definition: opt.h:92
#define ip_addr_isbroadcast(addr, netif)
Definition: ip_addr.h:219
void memp_free(memp_t type, void *mem)
Definition: memp.c:399
u16_t len
Definition: pbuf.h:125
struct pbuf * pbuf_alloc(pbuf_layer layer, u16_t length, pbuf_type type)
Definition: pbuf.c:199
#define ip_current_dest_addr()
Definition: ip.h:238
Definition: pbuf.h:77
#define IP_ADDR_PCB_VERSION_MATCH(addr, pcb)
Definition: ip_addr.h:153
#define NULL
Definition: usbd_def.h:53
u8_t pbuf_free(struct pbuf *p)
Definition: pbuf.c:652
#define PCB_ISIPV6(pcb)
Definition: ip.h:89
#define ERR_RTE
Definition: err.h:56
void pbuf_chain(struct pbuf *h, struct pbuf *t)
Definition: pbuf.c:820
#define RAW_DEBUG
Definition: opt.h:2851
#define ERR_OK
Definition: err.h:52
u16_t tot_len
Definition: pbuf.h:122
#define NETIF_SET_HWADDRHINT(netif, hint)
Definition: netif.h:411
#define LWIP_DBG_TRACE
Definition: debug.h:57
Definition: pbuf.h:108
s8_t err_t
Definition: err.h:47
#define ip_addr_cmp(addr1, addr2)
Definition: ip_addr.h:214
#define LWIP_ASSERT(message, assertion)
Definition: debug.h:70
Definition: netif.h:182
#define ip_addr_debug_print(debug, ipaddr)
Definition: ip_addr.h:221
#define SOF_BROADCAST
Definition: ip.h:120
#define ip_current_src_addr()
Definition: ip.h:236
unsigned char u8_t
Definition: cc.h:38
#define IP_PCB_IPVER_INPUT_MATCH(pcb)
Definition: ip.h:88
ip6_addr_t ip_addr_t
Definition: ip_addr.h:194
Definition: pbuf.h:65
#define IP_HDR_GET_VERSION(ptr)
Definition: ip.h:56
void * memp_malloc(memp_t type)
Definition: memp.c:303
#define LWIP_DEBUGF(debug, message)
Definition: debug.h:113
#define ERR_MEM
Definition: err.h:53
#define LWIP_UNUSED_ARG(x)
Definition: arch.h:89
unsigned short u16_t
Definition: cc.h:40
void * payload
Definition: pbuf.h:113
#define ip_get_option(pcb, opt)
Definition: ip.h:241
#define ip_addr_set_ipaddr(dest, src)
Definition: ip_addr.h:206