/
arp.c
534 lines (427 loc) · 12.2 KB
/
arp.c
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
/** @file
* @brief ARP related functions
*/
/*
* Copyright (c) 2016 Intel Corporation
*
* SPDX-License-Identifier: Apache-2.0
*/
#if defined(CONFIG_NET_DEBUG_ARP)
#define SYS_LOG_DOMAIN "net/arp"
#define NET_LOG_ENABLED 1
#endif
#include <errno.h>
#include <net/net_core.h>
#include <net/net_pkt.h>
#include <net/net_if.h>
#include <net/net_stats.h>
#include <net/arp.h>
#include "net_private.h"
#define NET_BUF_TIMEOUT MSEC(100)
static struct arp_entry arp_table[CONFIG_NET_ARP_TABLE_SIZE];
static inline struct arp_entry *find_entry(struct net_if *iface,
struct in_addr *dst,
struct arp_entry **free_entry,
struct arp_entry **non_pending)
{
int i;
NET_DBG("dst %s", net_sprint_ipv4_addr(dst));
for (i = 0; i < CONFIG_NET_ARP_TABLE_SIZE; i++) {
NET_DBG("[%d] iface %p dst %s ll %s pending %p", i, iface,
net_sprint_ipv4_addr(&arp_table[i].ip),
net_sprint_ll_addr((u8_t *)&arp_table[i].eth.addr,
sizeof(struct net_eth_addr)),
arp_table[i].pending);
if (arp_table[i].iface == iface &&
net_ipv4_addr_cmp(&arp_table[i].ip, dst)) {
/* Is there already pending operation for this
* IP address.
*/
if (arp_table[i].pending) {
NET_DBG("ARP already pending to %s ll %s",
net_sprint_ipv4_addr(dst),
net_sprint_ll_addr((u8_t *)
&arp_table[i].eth.addr,
sizeof(struct net_eth_addr)));
*free_entry = NULL;
*non_pending = NULL;
return NULL;
}
return &arp_table[i];
}
/* We return also the first free entry */
if (!*free_entry && !arp_table[i].pending &&
!arp_table[i].iface) {
*free_entry = &arp_table[i];
}
/* And also first non pending entry */
if (!*non_pending && !arp_table[i].pending) {
*non_pending = &arp_table[i];
}
}
return NULL;
}
static inline struct in_addr *if_get_addr(struct net_if *iface)
{
struct net_if_ipv4 *ipv4 = iface->config.ip.ipv4;
int i;
if (!ipv4) {
return NULL;
}
for (i = 0; i < NET_IF_MAX_IPV4_ADDR; i++) {
if (ipv4->unicast[i].is_used &&
ipv4->unicast[i].address.family == AF_INET &&
ipv4->unicast[i].addr_state == NET_ADDR_PREFERRED) {
return &ipv4->unicast[i].address.in_addr;
}
}
return NULL;
}
static inline struct net_pkt *prepare_arp(struct net_if *iface,
struct in_addr *next_addr,
struct arp_entry *entry,
struct net_pkt *pending)
{
#if defined(CONFIG_NET_VLAN)
u16_t vlan_tag = net_eth_get_vlan_tag(iface);
#endif
struct ethernet_context *ctx = net_if_l2_data(iface);
int eth_hdr_len = sizeof(struct net_eth_hdr);
struct net_pkt *pkt;
struct net_buf *frag;
struct net_arp_hdr *hdr;
struct net_eth_hdr *eth;
struct in_addr *my_addr;
#if defined(CONFIG_NET_VLAN)
if (ctx->vlan_enabled && vlan_tag != NET_VLAN_TAG_UNSPEC) {
eth_hdr_len = sizeof(struct net_eth_vlan_hdr);
}
#endif
pkt = net_pkt_get_reserve_tx(eth_hdr_len, NET_BUF_TIMEOUT);
if (!pkt) {
return NULL;
}
frag = net_pkt_get_frag(pkt, NET_BUF_TIMEOUT);
if (!frag) {
net_pkt_unref(pkt);
return NULL;
}
net_pkt_frag_add(pkt, frag);
net_pkt_set_iface(pkt, iface);
net_pkt_set_family(pkt, AF_INET);
hdr = NET_ARP_HDR(pkt);
#if defined(CONFIG_NET_VLAN)
net_pkt_set_vlan_tag(pkt, vlan_tag);
#endif
eth = net_eth_fill_header(ctx, pkt, frag, htons(NET_ETH_PTYPE_ARP),
NULL, NULL);
/* If entry is not set, then we are just about to send
* an ARP request using the data in pending net_pkt.
* This can happen if there is already a pending ARP
* request and we want to send it again.
*/
if (entry) {
entry->pending = net_pkt_ref(pending);
entry->iface = net_pkt_iface(pkt);
net_ipaddr_copy(&entry->ip, next_addr);
memcpy(ð->src.addr,
net_if_get_link_addr(entry->iface)->addr,
sizeof(struct net_eth_addr));
} else {
memcpy(ð->src.addr,
net_if_get_link_addr(iface)->addr,
sizeof(struct net_eth_addr));
}
memset(ð->dst.addr, 0xff, sizeof(struct net_eth_addr));
hdr->hwtype = htons(NET_ARP_HTYPE_ETH);
hdr->protocol = htons(NET_ETH_PTYPE_IP);
hdr->hwlen = sizeof(struct net_eth_addr);
hdr->protolen = sizeof(struct in_addr);
hdr->opcode = htons(NET_ARP_REQUEST);
memset(&hdr->dst_hwaddr.addr, 0x00, sizeof(struct net_eth_addr));
net_ipaddr_copy(&hdr->dst_ipaddr, next_addr);
memcpy(hdr->src_hwaddr.addr, eth->src.addr,
sizeof(struct net_eth_addr));
if (entry) {
my_addr = if_get_addr(entry->iface);
} else {
my_addr = &NET_IPV4_HDR(pending)->src;
}
if (my_addr) {
net_ipaddr_copy(&hdr->src_ipaddr, my_addr);
} else {
memset(&hdr->src_ipaddr, 0, sizeof(struct in_addr));
}
net_buf_add(frag, sizeof(struct net_arp_hdr));
return pkt;
}
struct net_pkt *net_arp_prepare(struct net_pkt *pkt)
{
struct ethernet_context *ctx = net_if_l2_data(net_pkt_iface(pkt));
struct arp_entry *entry, *free_entry = NULL, *non_pending = NULL;
struct net_buf *frag;
struct net_linkaddr *ll;
struct net_eth_hdr *hdr;
struct in_addr *addr;
if (!pkt || !pkt->frags) {
return NULL;
}
if (net_pkt_ll_reserve(pkt) != sizeof(struct net_eth_hdr)
#if defined(CONFIG_NET_VLAN)
&& net_pkt_ll_reserve(pkt) != sizeof(struct net_eth_vlan_hdr)
#endif
) {
/* Add the ethernet header if it is missing. */
struct net_buf *header;
header = net_pkt_get_frag(pkt, NET_BUF_TIMEOUT);
if (!header) {
return NULL;
}
net_eth_fill_header(ctx, pkt, header, htons(NET_ETH_PTYPE_IP),
net_pkt_ll_src(pkt)->addr,
net_pkt_ll_dst(pkt)->addr);
net_pkt_frag_insert(pkt, header);
net_pkt_compact(pkt);
}
hdr = (struct net_eth_hdr *)net_pkt_ll(pkt);
/* Is the destination in the local network, if not route via
* the gateway address.
*/
if (!net_if_ipv4_addr_mask_cmp(net_pkt_iface(pkt),
&NET_IPV4_HDR(pkt)->dst)) {
struct net_if_ipv4 *ipv4 = net_pkt_iface(pkt)->config.ip.ipv4;
NET_ASSERT(ipv4);
addr = &ipv4->gw;
if (net_is_ipv4_addr_unspecified(addr)) {
NET_ERR("Gateway not set for iface %p",
net_pkt_iface(pkt));
return NULL;
}
} else {
addr = &NET_IPV4_HDR(pkt)->dst;
}
/* If the destination address is already known, we do not need
* to send any ARP packet.
*/
entry = find_entry(net_pkt_iface(pkt),
addr, &free_entry, &non_pending);
if (!entry) {
if (!free_entry) {
/* So all the slots are occupied, use the first
* that can be taken.
*/
if (!non_pending) {
/* We cannot send the packet, the ARP
* cache is full or there is already a
* pending query to this IP address,
* so this packet must be discarded.
*/
struct net_pkt *req;
req = prepare_arp(net_pkt_iface(pkt),
addr, NULL, pkt);
NET_DBG("Resending ARP %p", req);
return req;
}
free_entry = non_pending;
}
return prepare_arp(net_pkt_iface(pkt), addr, free_entry, pkt);
}
ll = net_if_get_link_addr(entry->iface);
NET_DBG("ARP using ll %s for IP %s",
net_sprint_ll_addr(ll->addr, sizeof(struct net_eth_addr)),
net_sprint_ipv4_addr(&NET_IPV4_HDR(pkt)->src));
frag = pkt->frags;
while (frag) {
/* If there is no room for link layer header, then
* just send the packet as is.
*/
if (!net_buf_headroom(frag)) {
frag = frag->frags;
continue;
}
hdr = net_eth_fill_header(ctx, pkt, frag,
htons(NET_ETH_PTYPE_IP),
ll->addr, entry->eth.addr);
frag = frag->frags;
}
return pkt;
}
static inline void send_pending(struct net_if *iface, struct net_pkt **pkt)
{
struct net_pkt *pending = *pkt;
NET_DBG("dst %s pending %p frag %p",
net_sprint_ipv4_addr(&NET_IPV4_HDR(pending)->dst), pending,
pending->frags);
*pkt = NULL;
if (net_if_send_data(iface, pending) == NET_DROP) {
net_pkt_unref(pending);
}
}
static inline void arp_update(struct net_if *iface,
struct in_addr *src,
struct net_eth_addr *hwaddr)
{
int i;
NET_DBG("src %s", net_sprint_ipv4_addr(src));
for (i = 0; i < CONFIG_NET_ARP_TABLE_SIZE; i++) {
NET_DBG("[%d] iface %p dst %s ll %s pending %p", i, iface,
net_sprint_ipv4_addr(&arp_table[i].ip),
net_sprint_ll_addr((u8_t *)&arp_table[i].eth.addr,
sizeof(struct net_eth_addr)),
arp_table[i].pending);
if (arp_table[i].iface != iface ||
!net_ipv4_addr_cmp(&arp_table[i].ip, src)) {
continue;
}
if (arp_table[i].pending) {
/* We only update the ARP cache if we were
* initiating a request.
*/
memcpy(&arp_table[i].eth, hwaddr,
sizeof(struct net_eth_addr));
/* Set the dst in the pending packet */
net_pkt_ll_dst(arp_table[i].pending)->len =
sizeof(struct net_eth_addr);
net_pkt_ll_dst(arp_table[i].pending)->addr =
(u8_t *)
&NET_ETH_HDR(arp_table[i].pending)->dst.addr;
send_pending(iface, &arp_table[i].pending);
}
return;
}
}
static inline struct net_pkt *prepare_arp_reply(struct net_if *iface,
struct net_pkt *req)
{
struct ethernet_context *ctx = net_if_l2_data(iface);
int eth_hdr_len = sizeof(struct net_eth_hdr);
struct net_pkt *pkt;
struct net_buf *frag;
struct net_arp_hdr *hdr, *query;
struct net_eth_hdr *eth, *eth_query;
#if defined(CONFIG_NET_VLAN)
if (ctx->vlan_enabled &&
net_eth_get_vlan_tag(iface) != NET_VLAN_TAG_UNSPEC) {
eth_hdr_len = sizeof(struct net_eth_vlan_hdr);
}
#endif
pkt = net_pkt_get_reserve_tx(eth_hdr_len, NET_BUF_TIMEOUT);
if (!pkt) {
goto fail;
}
frag = net_pkt_get_frag(pkt, NET_BUF_TIMEOUT);
if (!frag) {
goto fail;
}
net_pkt_frag_add(pkt, frag);
net_pkt_set_iface(pkt, iface);
net_pkt_set_family(pkt, AF_INET);
hdr = NET_ARP_HDR(pkt);
eth = NET_ETH_HDR(pkt);
query = NET_ARP_HDR(req);
eth_query = NET_ETH_HDR(req);
#if defined(CONFIG_NET_VLAN)
net_pkt_set_vlan_tag(pkt, net_pkt_vlan_tag(req));
#endif
net_eth_fill_header(ctx, pkt, frag, htons(NET_ETH_PTYPE_ARP),
net_if_get_link_addr(iface)->addr,
eth_query->src.addr);
hdr->hwtype = htons(NET_ARP_HTYPE_ETH);
hdr->protocol = htons(NET_ETH_PTYPE_IP);
hdr->hwlen = sizeof(struct net_eth_addr);
hdr->protolen = sizeof(struct in_addr);
hdr->opcode = htons(NET_ARP_REPLY);
memcpy(&hdr->dst_hwaddr.addr, ð_query->src.addr,
sizeof(struct net_eth_addr));
memcpy(&hdr->src_hwaddr.addr, ð->src.addr,
sizeof(struct net_eth_addr));
net_ipaddr_copy(&hdr->dst_ipaddr, &query->src_ipaddr);
net_ipaddr_copy(&hdr->src_ipaddr, &query->dst_ipaddr);
net_buf_add(frag, sizeof(struct net_arp_hdr));
return pkt;
fail:
net_pkt_unref(pkt);
return NULL;
}
enum net_verdict net_arp_input(struct net_pkt *pkt)
{
struct net_arp_hdr *arp_hdr;
struct net_pkt *reply;
struct in_addr *addr;
if (net_pkt_get_len(pkt) < (sizeof(struct net_arp_hdr) -
net_pkt_ll_reserve(pkt))) {
NET_DBG("Invalid ARP header (len %zu, min %zu bytes)",
net_pkt_get_len(pkt),
sizeof(struct net_arp_hdr) -
net_pkt_ll_reserve(pkt));
return NET_DROP;
}
arp_hdr = NET_ARP_HDR(pkt);
switch (ntohs(arp_hdr->opcode)) {
case NET_ARP_REQUEST:
/* Someone wants to know our ll address */
addr = if_get_addr(net_pkt_iface(pkt));
if (!addr) {
return NET_DROP;
}
if (!net_ipv4_addr_cmp(&arp_hdr->dst_ipaddr, addr)) {
/* Not for us so drop the packet silently */
return NET_DROP;
}
#if defined(CONFIG_NET_DEBUG_ARP)
do {
char out[sizeof("xxx.xxx.xxx.xxx")];
snprintk(out, sizeof(out), "%s",
net_sprint_ipv4_addr(&arp_hdr->src_ipaddr));
NET_DBG("ARP request from %s [%s] for %s",
out,
net_sprint_ll_addr(
(u8_t *)&arp_hdr->src_hwaddr,
arp_hdr->hwlen),
net_sprint_ipv4_addr(&arp_hdr->dst_ipaddr));
} while (0);
#endif /* CONFIG_NET_DEBUG_ARP */
/* Send reply */
reply = prepare_arp_reply(net_pkt_iface(pkt), pkt);
if (reply) {
net_if_queue_tx(net_pkt_iface(reply), reply);
} else {
NET_DBG("Cannot send ARP reply");
}
break;
case NET_ARP_REPLY:
if (net_is_my_ipv4_addr(&arp_hdr->dst_ipaddr)) {
arp_update(net_pkt_iface(pkt), &arp_hdr->src_ipaddr,
&arp_hdr->src_hwaddr);
}
break;
}
net_pkt_unref(pkt);
return NET_OK;
}
void net_arp_clear_cache(void)
{
int i;
for (i = 0; i < CONFIG_NET_ARP_TABLE_SIZE; i++) {
if (arp_table[i].pending) {
net_pkt_unref(arp_table[i].pending);
}
}
memset(&arp_table, 0, sizeof(arp_table));
}
int net_arp_foreach(net_arp_cb_t cb, void *user_data)
{
int i, ret = 0;
for (i = 0; i < CONFIG_NET_ARP_TABLE_SIZE; i++) {
if (!arp_table[i].iface) {
continue;
}
ret++;
cb(&arp_table[i], user_data);
}
return ret;
}
void net_arp_init(void)
{
net_arp_clear_cache();
}