1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
78
79
80
90
91
92
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
155
156
157
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
227
228
229
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
284
285
286
293
294
295
296
297
303
304
305
306
307
308
309
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
362
363
364
365
383
384
389
390
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
434
435
436
437
438
439
440
441
442
443
444
446
447
449
452
453
454
455
456
457
458
459
460
463
464
465
466
467
468
472
476
477
478
486
487
488
500
501
502
503
507
508
509
517
518
519
520
521
522
523
524
525
526
527
528
529
530
534
535
536
537
538
539
540
541
542
543
544
545
546
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
645
646
647
648
649
650
655
670
671
/* ... */
/* ... */
#include "lwip/opt.h"
#if LWIP_RAW
#include "lwip/def.h"
#include "lwip/memp.h"
#include "lwip/ip_addr.h"
#include "lwip/netif.h"
#include "lwip/raw.h"
#include "lwip/priv/raw_priv.h"
#include "lwip/stats.h"
#include "lwip/ip6.h"
#include "lwip/ip6_addr.h"
#include "lwip/inet_chksum.h"
#include <string.h>
11 includes
static struct raw_pcb *raw_pcbs;
static u8_t
raw_input_local_match(struct raw_pcb *pcb, u8_t broadcast)
{
LWIP_UNUSED_ARG(broadcast);
if ((pcb->netif_idx != NETIF_NO_INDEX) &&
(pcb->netif_idx != netif_get_index(ip_data.current_input_netif))) {
return 0;
}if ((pcb->netif_idx != NETIF_NO_INDEX) && (pcb->netif_idx != netif_get_index(ip_data.current_input_netif))) { ... }
#if LWIP_IPV4 && LWIP_IPV6
if (IP_IS_ANY_TYPE_VAL(pcb->local_ip)) {
#if IP_SOF_BROADCAST_RECV
if ((broadcast != 0) && !ip_get_option(pcb, SOF_BROADCAST)) {
return 0;
}if ((broadcast != 0) && !ip_get_option(pcb, SOF_BROADCAST)) { ... }
/* ... */#endif
return 1;
}if (IP_IS_ANY_TYPE_VAL(pcb->local_ip)) { ... }
/* ... */#endif
if (IP_ADDR_PCB_VERSION_MATCH_EXACT(pcb, ip_current_dest_addr())) {
#if LWIP_IPV4
/* ... */
if (broadcast != 0) {
#if IP_SOF_BROADCAST_RECV
if (ip_get_option(pcb, SOF_BROADCAST))
#endif
{
if (ip4_addr_isany(ip_2_ip4(&pcb->local_ip))) {
return 1;
}if (ip4_addr_isany(ip_2_ip4(&pcb->local_ip))) { ... }
...}
}if (broadcast != 0) { ... } else/* ... */
#endif
if (ip_addr_isany(&pcb->local_ip) ||
ip_addr_cmp(&pcb->local_ip, ip_current_dest_addr())) {
return 1;
}else if (ip_addr_isany(&pcb->local_ip) || ip_addr_cmp(&pcb->local_ip, ip_current_dest_addr())) { ... }
}if (IP_ADDR_PCB_VERSION_MATCH_EXACT(pcb, ip_current_dest_addr())) { ... }
return 0;
}raw_input_local_match (struct raw_pcb *pcb, u8_t broadcast) { ... }
/* ... */
raw_input_state_t
raw_input(struct pbuf *p, struct netif *inp)
{
struct raw_pcb *pcb, *prev;
s16_t proto;
raw_input_state_t ret = RAW_INPUT_NONE;
u8_t broadcast = ip_addr_isbroadcast(ip_current_dest_addr(), ip_current_netif());
LWIP_UNUSED_ARG(inp);
#if LWIP_IPV6
#if LWIP_IPV4
if (IP_HDR_GET_VERSION(p->payload) == 6)
#endif
{
struct ip6_hdr *ip6hdr = (struct ip6_hdr *)p->payload;
proto = IP6H_NEXTH(ip6hdr);
...}
#if LWIP_IPV4
else
#endif /* ... */
#endif
#if LWIP_IPV4
{
proto = IPH_PROTO((struct ip_hdr *)p->payload);
...}/* ... */
#endif
prev = NULL;
pcb = raw_pcbs;
while (pcb != NULL) {
if ((pcb->protocol == proto) && raw_input_local_match(pcb, broadcast) &&
(((pcb->flags & RAW_FLAGS_CONNECTED) == 0) ||
ip_addr_cmp(&pcb->remote_ip, ip_current_src_addr()))) {
if (pcb->recv != NULL) {
u8_t eaten;
#ifndef LWIP_NOASSERT
void *old_payload = p->payload;
#endif
ret = RAW_INPUT_DELIVERED;
eaten = pcb->recv(pcb->recv_arg, pcb, p, ip_current_src_addr());
if (eaten != 0) {
p = NULL;
if (prev != NULL) {
/* ... */
prev->next = pcb->next;
pcb->next = raw_pcbs;
raw_pcbs = pcb;
}if (prev != NULL) { ... }
return RAW_INPUT_EATEN;
}if (eaten != 0) { ... } else {
LWIP_ASSERT("raw pcb recv callback altered pbuf payload pointer without eating packet",
p->payload == old_payload);
}else { ... }
}if (pcb->recv != NULL) { ... }
}if ((pcb->protocol == proto) && raw_input_local_match(pcb, broadcast) && (((pcb->flags & RAW_FLAGS_CONNECTED) == 0) || ip_addr_cmp(&pcb->remote_ip, ip_current_src_addr()))) { ... }
prev = pcb;
pcb = pcb->next;
}while (pcb != NULL) { ... }
return ret;
}raw_input (struct pbuf *p, struct netif *inp) { ... }
/* ... */
err_t
raw_bind(struct raw_pcb *pcb, const ip_addr_t *ipaddr)
{
LWIP_ASSERT_CORE_LOCKED();
if ((pcb == NULL) || (ipaddr == NULL)) {
return ERR_VAL;
}if ((pcb == NULL) || (ipaddr == NULL)) { ... }
ip_addr_set_ipaddr(&pcb->local_ip, ipaddr);
#if LWIP_IPV6 && LWIP_IPV6_SCOPES
/* ... */
if (IP_IS_V6(&pcb->local_ip) &&
ip6_addr_lacks_zone(ip_2_ip6(&pcb->local_ip), IP6_UNKNOWN)) {
ip6_addr_select_zone(ip_2_ip6(&pcb->local_ip), ip_2_ip6(&pcb->local_ip));
}if (IP_IS_V6(&pcb->local_ip) && ip6_addr_lacks_zone(ip_2_ip6(&pcb->local_ip), IP6_UNKNOWN)) { ... }
/* ... */#endif
return ERR_OK;
}raw_bind (struct raw_pcb *pcb, const ip_addr_t *ipaddr) { ... }
/* ... */
void
raw_bind_netif(struct raw_pcb *pcb, const struct netif *netif)
{
LWIP_ASSERT_CORE_LOCKED();
if (netif != NULL) {
pcb->netif_idx = netif_get_index(netif);
}if (netif != NULL) { ... } else {
pcb->netif_idx = NETIF_NO_INDEX;
}else { ... }
}raw_bind_netif (struct raw_pcb *pcb, const struct netif *netif) { ... }
/* ... */
err_t
raw_connect(struct raw_pcb *pcb, const ip_addr_t *ipaddr)
{
LWIP_ASSERT_CORE_LOCKED();
if ((pcb == NULL) || (ipaddr == NULL)) {
return ERR_VAL;
}if ((pcb == NULL) || (ipaddr == NULL)) { ... }
ip_addr_set_ipaddr(&pcb->remote_ip, ipaddr);
#if LWIP_IPV6 && LWIP_IPV6_SCOPES
/* ... */
if (IP_IS_V6(&pcb->remote_ip) &&
ip6_addr_lacks_zone(ip_2_ip6(&pcb->remote_ip), IP6_UNKNOWN)) {
ip6_addr_select_zone(ip_2_ip6(&pcb->remote_ip), ip_2_ip6(&pcb->local_ip));
}if (IP_IS_V6(&pcb->remote_ip) && ip6_addr_lacks_zone(ip_2_ip6(&pcb->remote_ip), IP6_UNKNOWN)) { ... }
/* ... */#endif
raw_set_flags(pcb, RAW_FLAGS_CONNECTED);
return ERR_OK;
}raw_connect (struct raw_pcb *pcb, const ip_addr_t *ipaddr) { ... }
/* ... */
void
raw_disconnect(struct raw_pcb *pcb)
{
LWIP_ASSERT_CORE_LOCKED();
#if LWIP_IPV4 && LWIP_IPV6
if (IP_IS_ANY_TYPE_VAL(pcb->local_ip)) {
ip_addr_copy(pcb->remote_ip, *IP_ANY_TYPE);
}if (IP_IS_ANY_TYPE_VAL(pcb->local_ip)) { ... } else {
#endif
ip_addr_set_any(IP_IS_V6_VAL(pcb->remote_ip), &pcb->remote_ip);
#if LWIP_IPV4 && LWIP_IPV6
}else { ... }
#endif
pcb->netif_idx = NETIF_NO_INDEX;
raw_clear_flags(pcb, RAW_FLAGS_CONNECTED);
}raw_disconnect (struct raw_pcb *pcb) { ... }
/* ... */
void
raw_recv(struct raw_pcb *pcb, raw_recv_fn recv, void *recv_arg)
{
LWIP_ASSERT_CORE_LOCKED();
pcb->recv = recv;
pcb->recv_arg = recv_arg;
}raw_recv (struct raw_pcb *pcb, raw_recv_fn recv, void *recv_arg) { ... }
/* ... */
err_t
raw_sendto(struct raw_pcb *pcb, struct pbuf *p, const ip_addr_t *ipaddr)
{
struct netif *netif;
const ip_addr_t *src_ip;
if ((pcb == NULL) || (ipaddr == NULL) || !IP_ADDR_PCB_VERSION_MATCH(pcb, ipaddr)) {
return ERR_VAL;
}if ((pcb == NULL) || (ipaddr == NULL) || !IP_ADDR_PCB_VERSION_MATCH(pcb, ipaddr)) { ... }
LWIP_DEBUGF(RAW_DEBUG | LWIP_DBG_TRACE, ("raw_sendto\n"));
if (pcb->netif_idx != NETIF_NO_INDEX) {
netif = netif_get_by_index(pcb->netif_idx);
}if (pcb->netif_idx != NETIF_NO_INDEX) { ... } else {
#if LWIP_MULTICAST_TX_OPTIONS
netif = NULL;
if (ip_addr_ismulticast(ipaddr)) {
/* ... */
netif = netif_get_by_index(pcb->mcast_ifindex);
}if (ip_addr_ismulticast(ipaddr)) { ... }
if (netif == NULL)/* ... */
#endif
{
netif = ip_route(&pcb->local_ip, ipaddr);
...}
}else { ... }
if (netif == NULL) {
LWIP_DEBUGF(RAW_DEBUG | LWIP_DBG_LEVEL_WARNING, ("raw_sendto: No route to "));
ip_addr_debug_print(RAW_DEBUG | LWIP_DBG_LEVEL_WARNING, ipaddr);
return ERR_RTE;
}if (netif == NULL) { ... }
if (ip_addr_isany(&pcb->local_ip) || ip_addr_ismulticast(&pcb->local_ip)) {
src_ip = ip_netif_get_local_ip(netif, ipaddr);
#if LWIP_IPV6
if (src_ip == NULL) {
return ERR_RTE;
}if (src_ip == NULL) { ... }
/* ... */#endif
}if (ip_addr_isany(&pcb->local_ip) || ip_addr_ismulticast(&pcb->local_ip)) { ... } else {
src_ip = &pcb->local_ip;
}else { ... }
return raw_sendto_if_src(pcb, p, ipaddr, netif, src_ip);
}raw_sendto (struct raw_pcb *pcb, struct pbuf *p, const ip_addr_t *ipaddr) { ... }
/* ... */
err_t
raw_sendto_if_src(struct raw_pcb *pcb, struct pbuf *p, const ip_addr_t *dst_ip,
struct netif *netif, const ip_addr_t *src_ip)
{
err_t err;
struct pbuf *q;
u16_t header_size;
u8_t ttl;
LWIP_ASSERT_CORE_LOCKED();
if ((pcb == NULL) || (dst_ip == NULL) || (netif == NULL) || (src_ip == NULL) ||
!IP_ADDR_PCB_VERSION_MATCH(pcb, src_ip) || !IP_ADDR_PCB_VERSION_MATCH(pcb, dst_ip)) {
return ERR_VAL;
}if ((pcb == NULL) || (dst_ip == NULL) || (netif == NULL) || (src_ip == NULL) || !IP_ADDR_PCB_VERSION_MATCH(pcb, src_ip) || !IP_ADDR_PCB_VERSION_MATCH(pcb, dst_ip)) { ... }
header_size = (
#if LWIP_IPV4 && LWIP_IPV6
IP_IS_V6(dst_ip) ? IP6_HLEN : IP_HLEN);
#elif LWIP_IPV4
IP_HLEN);
#else
IP6_HLEN);
#endif
/* ... */
if (pcb->flags & RAW_FLAGS_HDRINCL) {
/* ... */
if (p->len < header_size) {
return ERR_VAL;
}if (p->len < header_size) { ... }
NETIF_SET_HINTS(netif, &pcb->netif_hints);
err = ip_output_if_hdrincl(p, src_ip, dst_ip, netif);
NETIF_RESET_HINTS(netif);
return err;
}if (pcb->flags & RAW_FLAGS_HDRINCL) { ... }
if ((u16_t)(p->tot_len + header_size) < p->tot_len) {
return ERR_MEM;
}if ((u16_t)(p->tot_len + header_size) < p->tot_len) { ... }
if (pbuf_add_header(p, header_size)) {
q = pbuf_alloc(PBUF_IP, 0, PBUF_RAM);
if (q == NULL) {
LWIP_DEBUGF(RAW_DEBUG | LWIP_DBG_TRACE | LWIP_DBG_LEVEL_SERIOUS, ("raw_sendto: could not allocate header\n"));
return ERR_MEM;
}if (q == NULL) { ... }
if (p->tot_len != 0) {
pbuf_chain(q, p);
}if (p->tot_len != 0) { ... }
LWIP_DEBUGF(RAW_DEBUG, ("raw_sendto: added header pbuf %p before given pbuf %p\n", (void *)q, (void *)p));
}if (pbuf_add_header(p, header_size)) { ... } else {
q = p;
if (pbuf_remove_header(q, header_size)) {
LWIP_ASSERT("Can't restore header we just removed!", 0);
return ERR_MEM;
}if (pbuf_remove_header(q, header_size)) { ... }
}else { ... }
#if IP_SOF_BROADCAST
if (IP_IS_V4(dst_ip)) {
if (!ip_get_option(pcb, SOF_BROADCAST) && ip_addr_isbroadcast(dst_ip, netif)) {
LWIP_DEBUGF(RAW_DEBUG | LWIP_DBG_LEVEL_WARNING, ("raw_sendto: SOF_BROADCAST not enabled on pcb %p\n", (void *)pcb));
if (q != p) {
pbuf_free(q);
}if (q != p) { ... }
return ERR_VAL;
}if (!ip_get_option(pcb, SOF_BROADCAST) && ip_addr_isbroadcast(dst_ip, netif)) { ... }
}if (IP_IS_V4(dst_ip)) { ... }
/* ... */#endif
#if LWIP_MULTICAST_TX_OPTIONS
if (((pcb->flags & RAW_FLAGS_MULTICAST_LOOP) != 0) && ip_addr_ismulticast(dst_ip)) {
q->flags |= PBUF_FLAG_MCASTLOOP;
}if (((pcb->flags & RAW_FLAGS_MULTICAST_LOOP) != 0) && ip_addr_ismulticast(dst_ip)) { ... }
/* ... */#endif
#if LWIP_IPV6
/* ... */
if (IP_IS_V6(dst_ip) && pcb->chksum_reqd) {
u16_t chksum = ip6_chksum_pseudo(p, pcb->protocol, p->tot_len, ip_2_ip6(src_ip), ip_2_ip6(dst_ip));
LWIP_ASSERT("Checksum must fit into first pbuf", p->len >= (pcb->chksum_offset + 2));
SMEMCPY(((u8_t *)p->payload) + pcb->chksum_offset, &chksum, sizeof(u16_t));
}if (IP_IS_V6(dst_ip) && pcb->chksum_reqd) { ... }
/* ... */#endif
#if LWIP_MULTICAST_TX_OPTIONS
ttl = (ip_addr_ismulticast(dst_ip) ? raw_get_multicast_ttl(pcb) : pcb->ttl);
#else
ttl = pcb->ttl;
#endif
NETIF_SET_HINTS(netif, &pcb->netif_hints);
err = ip_output_if(q, src_ip, dst_ip, ttl, pcb->tos, pcb->protocol, netif);
NETIF_RESET_HINTS(netif);
if (q != p) {
pbuf_free(q);
}if (q != p) { ... }
return err;
}raw_sendto_if_src (struct raw_pcb *pcb, struct pbuf *p, const ip_addr_t *dst_ip, struct netif *netif, const ip_addr_t *src_ip) { ... }
/* ... */
err_t
raw_send(struct raw_pcb *pcb, struct pbuf *p)
{
return raw_sendto(pcb, p, &pcb->remote_ip);
}raw_send (struct raw_pcb *pcb, struct pbuf *p) { ... }
/* ... */
void
raw_remove(struct raw_pcb *pcb)
{
struct raw_pcb *pcb2;
LWIP_ASSERT_CORE_LOCKED();
if (raw_pcbs == pcb) {
raw_pcbs = raw_pcbs->next;
}if (raw_pcbs == pcb) { ... } else {
for (pcb2 = raw_pcbs; pcb2 != NULL; pcb2 = pcb2->next) {
if (pcb2->next != NULL && pcb2->next == pcb) {
pcb2->next = pcb->next;
break;
}if (pcb2->next != NULL && pcb2->next == pcb) { ... }
}for (pcb2 = raw_pcbs; pcb2 != NULL; pcb2 = pcb2->next) { ... }
}else { ... }
memp_free(MEMP_RAW_PCB, pcb);
}raw_remove (struct raw_pcb *pcb) { ... }
/* ... */
struct raw_pcb *
raw_new(u8_t proto)
{
struct raw_pcb *pcb;
LWIP_DEBUGF(RAW_DEBUG | LWIP_DBG_TRACE, ("raw_new\n"));
LWIP_ASSERT_CORE_LOCKED();
pcb = (struct raw_pcb *)memp_malloc(MEMP_RAW_PCB);
if (pcb != NULL) {
memset(pcb, 0, sizeof(struct raw_pcb));
pcb->protocol = proto;
pcb->ttl = RAW_TTL;
#if LWIP_MULTICAST_TX_OPTIONS
raw_set_multicast_ttl(pcb, RAW_TTL);
#endif
pcb->next = raw_pcbs;
raw_pcbs = pcb;
}if (pcb != NULL) { ... }
return pcb;
}raw_new (u8_t proto) { ... }
/* ... */
struct raw_pcb *
raw_new_ip_type(u8_t type, u8_t proto)
{
struct raw_pcb *pcb;
LWIP_ASSERT_CORE_LOCKED();
pcb = raw_new(proto);
#if LWIP_IPV4 && LWIP_IPV6
if (pcb != NULL) {
IP_SET_TYPE_VAL(pcb->local_ip, type);
IP_SET_TYPE_VAL(pcb->remote_ip, type);
}if (pcb != NULL) { ... }
/* ... */#else
LWIP_UNUSED_ARG(type);
#endif
return pcb;
}raw_new_ip_type (u8_t type, u8_t proto) { ... }
/* ... */
void raw_netif_ip_addr_changed(const ip_addr_t *old_addr, const ip_addr_t *new_addr)
{
struct raw_pcb *rpcb;
if (!ip_addr_isany(old_addr) && !ip_addr_isany(new_addr)) {
for (rpcb = raw_pcbs; rpcb != NULL; rpcb = rpcb->next) {
if (ip_addr_cmp(&rpcb->local_ip, old_addr)) {
/* ... */
ip_addr_copy(rpcb->local_ip, *new_addr);
}if (ip_addr_cmp(&rpcb->local_ip, old_addr)) { ... }
}for (rpcb = raw_pcbs; rpcb != NULL; rpcb = rpcb->next) { ... }
}if (!ip_addr_isany(old_addr) && !ip_addr_isany(new_addr)) { ... }
}raw_netif_ip_addr_changed (const ip_addr_t *old_addr, const ip_addr_t *new_addr) { ... }
/* ... */
#endif