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
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
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
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
|
// SPDX-License-Identifier: GPL-2.0-only
// SPDX-FileCopyrightText: 2010 Sascha Hauer <s.hauer@pengutronix.de>, Pengutronix
// SPDX-FileCopyrightText: 1994-2000 Neil Russell
// SPDX-FileCopyrightText: 2000 Roland Borde
// SPDX-FileCopyrightText: 2000 Paolo Scaffardi
// SPDX-FileCopyrightText: 2000-2002 Wolfgang Denk <wd@denx.de>
/*
* net.c - barebox networking support
*
* based on U-Boot (LiMon) code
*/
#define pr_fmt(fmt) "net: " fmt
#include <common.h>
#include <clock.h>
#include <command.h>
#include <environment.h>
#include <param.h>
#include <net.h>
#include <driver.h>
#include <errno.h>
#include <malloc.h>
#include <init.h>
#include <globalvar.h>
#include <magicvar.h>
#include <linux/ctype.h>
#include <linux/err.h>
unsigned char *NetRxPackets[PKTBUFSRX]; /* Receive packets */
static unsigned int net_ip_id;
char *net_server;
IPaddr_t net_gateway;
static IPaddr_t net_nameserver;
static char *net_domainname;
void net_set_nameserver(IPaddr_t nameserver)
{
net_nameserver = nameserver;
}
IPaddr_t net_get_nameserver(void)
{
return net_nameserver;
}
void net_set_domainname(const char *name)
{
free(net_domainname);
if (name)
net_domainname = xstrdup(name);
else
net_domainname = xstrdup("");
};
const char *net_get_domainname(void)
{
return net_domainname;
}
int net_checksum_ok(unsigned char *ptr, int len)
{
return net_checksum(ptr, len) == 0xffff;
}
uint16_t net_checksum(unsigned char *ptr, int len)
{
uint32_t xsum = 0;
uint16_t *p = (uint16_t *)ptr;
if (len & 1)
ptr[len] = 0;
len = (len + 1) >> 1;
while (len-- > 0)
xsum += *p++;
xsum = (xsum & 0xffff) + (xsum >> 16);
xsum = (xsum & 0xffff) + (xsum >> 16);
return xsum & 0xffff;
}
IPaddr_t getenv_ip(const char *name)
{
IPaddr_t ip;
const char *var = getenv(name);
if (!var)
return 0;
if (!string_to_ip(var, &ip))
return ip;
resolv(var, &ip);
return ip;
}
int setenv_ip(const char *name, IPaddr_t ip)
{
char str[sizeof("255.255.255.255")];
sprintf(str, "%pI4", &ip);
setenv(name, str);
return 0;
}
static unsigned char *arp_ether;
static IPaddr_t arp_wait_ip;
static void arp_handler(struct arprequest *arp)
{
IPaddr_t tmp;
/* are we waiting for a reply */
if (!arp_wait_ip)
return;
tmp = net_read_ip(&arp->ar_data[6]);
/* matched waiting packet's address */
if (tmp == arp_wait_ip) {
/* save address for later use */
memcpy(arp_ether, &arp->ar_data[0], 6);
/* no arp request pending now */
arp_wait_ip = 0;
}
}
struct eth_device *net_route(IPaddr_t dest)
{
struct eth_device *edev;
for_each_netdev(edev) {
if (!edev->ipaddr || !edev->ifup)
continue;
if ((dest & edev->netmask) == (edev->ipaddr & edev->netmask)) {
pr_debug("Route: Using %s (ip=%pI4, nm=%pI4) to reach %pI4\n",
dev_name(&edev->dev), &edev->ipaddr, &edev->netmask,
&dest);
return edev;
}
}
pr_debug("Route: No device found for %pI4\n", &dest);
return NULL;
}
static int arp_request(struct eth_device *edev, IPaddr_t dest, unsigned char *ether)
{
char *pkt;
struct arprequest *arp;
uint64_t arp_start;
static char *arp_packet;
struct ethernet *et;
unsigned retries = 0;
int ret;
if (!edev)
return -EHOSTUNREACH;
if (!arp_packet) {
arp_packet = net_alloc_packet();
if (!arp_packet)
return -ENOMEM;
}
pkt = arp_packet;
et = (struct ethernet *)arp_packet;
arp_wait_ip = dest;
pr_debug("send ARP broadcast for %pI4\n", &dest);
memset(et->et_dest, 0xff, 6);
memcpy(et->et_src, edev->ethaddr, 6);
et->et_protlen = htons(PROT_ARP);
arp = (struct arprequest *)(pkt + ETHER_HDR_SIZE);
arp->ar_hrd = htons(ARP_ETHER);
arp->ar_pro = htons(PROT_IP);
arp->ar_hln = 6;
arp->ar_pln = 4;
arp->ar_op = htons(ARPOP_REQUEST);
memcpy(arp->ar_data, edev->ethaddr, 6); /* source ET addr */
net_write_ip(arp->ar_data + 6, edev->ipaddr); /* source IP addr */
memset(arp->ar_data + 10, 0, 6); /* dest ET addr = 0 */
if ((dest & edev->netmask) != (edev->ipaddr & edev->netmask)) {
if (!net_gateway)
arp_wait_ip = dest;
else
arp_wait_ip = net_gateway;
} else {
arp_wait_ip = dest;
}
net_write_ip(arp->ar_data + 16, arp_wait_ip);
arp_ether = ether;
ret = eth_send(edev, arp_packet, ETHER_HDR_SIZE + ARP_HDR_SIZE);
if (ret)
return ret;
arp_start = get_time_ns();
while (arp_wait_ip) {
if (ctrlc())
return -EINTR;
if (is_timeout(arp_start, 3 * SECOND)) {
printf("T ");
arp_start = get_time_ns();
ret = eth_send(edev, arp_packet, ETHER_HDR_SIZE + ARP_HDR_SIZE);
if (ret)
return ret;
retries++;
}
if (retries > PKT_NUM_RETRIES)
return -ETIMEDOUT;
net_poll();
}
pr_debug("Got ARP REPLY for %pI4: %02x:%02x:%02x:%02x:%02x:%02x\n",
&dest, ether[0], ether[1], ether[2], ether[3], ether[4],
ether[5]);
return 0;
}
void net_poll(void)
{
static bool in_net_poll;
if (in_net_poll)
return;
in_net_poll = true;
eth_rx();
in_net_poll = false;
}
static void __net_poll(struct poller_struct *poller)
{
static uint64_t last;
/*
* USB network controllers take a long time in the receive path,
* so limit the polling rate to once per 10ms. This is due to
* deficiencies in the barebox USB stack: We can't queue URBs and
* receive a callback when they are done. Instead, we always
* synchronously queue an URB and wait for its completion. In case
* of USB network adapters the only way to detect if packets have
* been received is to queue a RX URB and see if it completes (in
* which case we have received data) or if it timeouts (no data
* available). The timeout can't be arbitrarily small, 2ms is the
* smallest we can do with the 1ms USB frame size.
*
* Given that we do a mixture of polling-as-fast-as-possible when
* we are waiting for network traffic (tftp, nfs and other users
* actively calling net_poll()) and doing a low frequency polling
* here to still get packets when no user is actively waiting for
* incoming packets. This is used to receive incoming ping packets
* and to get fastboot over ethernet going.
*/
if (!is_timeout(last, 10 * MSECOND))
return;
net_poll();
last = get_time_ns();
}
static struct poller_struct net_poller = {
.func = __net_poll,
};
static int init_net_poll(void)
{
return poller_register(&net_poller, "net");
}
device_initcall(init_net_poll);
static uint16_t net_udp_new_localport(void)
{
static uint16_t localport;
localport++;
if (localport < 1024)
localport = 1024;
return localport;
}
IPaddr_t net_get_serverip(void)
{
IPaddr_t ip;
int ret;
ret = resolv(net_server, &ip);
if (ret)
return 0;
return ip;
}
void net_set_serverip(IPaddr_t ip)
{
free(net_server);
net_server = xasprintf("%pI4", &ip);
}
void net_set_serverip_empty(IPaddr_t ip)
{
if (net_server && *net_server)
return;
net_set_serverip(ip);
}
void net_set_ip(struct eth_device *edev, IPaddr_t ip)
{
edev->ipaddr = ip;
}
IPaddr_t net_get_ip(struct eth_device *edev)
{
return edev->ipaddr;
}
void net_set_netmask(struct eth_device *edev, IPaddr_t nm)
{
edev->netmask = nm;
}
void net_set_gateway(IPaddr_t gw)
{
net_gateway = gw;
}
IPaddr_t net_get_gateway(void)
{
return net_gateway;
}
static LIST_HEAD(connection_list);
static struct net_connection *net_new(struct eth_device *edev, IPaddr_t dest,
rx_handler_f *handler, void *ctx)
{
struct net_connection *con;
int ret;
if (!edev) {
edev = net_route(dest);
if (!edev && net_gateway)
edev = net_route(net_gateway);
if (!edev)
return ERR_PTR(-EHOSTUNREACH);
}
if (!is_valid_ether_addr(edev->ethaddr)) {
char str[sizeof("xx:xx:xx:xx:xx:xx")];
random_ether_addr(edev->ethaddr);
ethaddr_to_string(edev->ethaddr, str);
dev_warn(&edev->dev, "No MAC address set. Using random address %s\n", str);
eth_set_ethaddr(edev, edev->ethaddr);
}
/* If we don't have an ip only broadcast is allowed */
if (!edev->ipaddr && dest != IP_BROADCAST)
return ERR_PTR(-ENETDOWN);
con = xzalloc(sizeof(*con));
con->packet = net_alloc_packet();
con->priv = ctx;
con->edev = edev;
memset(con->packet, 0, PKTSIZE);
con->et = (struct ethernet *)con->packet;
con->ip = (struct iphdr *)(con->packet + ETHER_HDR_SIZE);
con->udp = (struct udphdr *)(con->packet + ETHER_HDR_SIZE + sizeof(struct iphdr));
con->icmp = (struct icmphdr *)(con->packet + ETHER_HDR_SIZE + sizeof(struct iphdr));
con->handler = handler;
if (dest == IP_BROADCAST) {
memset(con->et->et_dest, 0xff, 6);
} else {
ret = arp_request(edev, dest, con->et->et_dest);
if (ret)
goto out;
}
con->et->et_protlen = htons(PROT_IP);
memcpy(con->et->et_src, edev->ethaddr, 6);
con->ip->hl_v = 0x45;
con->ip->tos = 0;
con->ip->frag_off = htons(0x4000); /* No fragmentation */;
con->ip->ttl = 255;
net_copy_ip(&con->ip->daddr, &dest);
net_copy_ip(&con->ip->saddr, &edev->ipaddr);
list_add_tail(&con->list, &connection_list);
return con;
out:
free(con->packet);
free(con);
return ERR_PTR(ret);
}
struct net_connection *net_udp_eth_new(struct eth_device *edev, IPaddr_t dest,
uint16_t dport, rx_handler_f *handler,
void *ctx)
{
struct net_connection *con = net_new(edev, dest, handler, ctx);
if (IS_ERR(con))
return con;
con->proto = IPPROTO_UDP;
con->udp->uh_dport = htons(dport);
con->udp->uh_sport = htons(net_udp_new_localport());
con->ip->protocol = IPPROTO_UDP;
return con;
}
struct net_connection *net_udp_new(IPaddr_t dest, uint16_t dport,
rx_handler_f *handler, void *ctx)
{
return net_udp_eth_new(NULL, dest, dport, handler, ctx);
}
struct net_connection *net_icmp_new(IPaddr_t dest, rx_handler_f *handler,
void *ctx)
{
struct net_connection *con = net_new(NULL, dest, handler, ctx);
if (IS_ERR(con))
return con;
con->proto = IPPROTO_ICMP;
con->ip->protocol = IPPROTO_ICMP;
return con;
}
void net_unregister(struct net_connection *con)
{
list_del(&con->list);
free(con->packet);
free(con);
}
static int net_ip_send(struct net_connection *con, int len)
{
con->ip->tot_len = htons(sizeof(struct iphdr) + len);
con->ip->id = htons(net_ip_id++);
con->ip->check = 0;
con->ip->check = ~net_checksum((unsigned char *)con->ip, sizeof(struct iphdr));
return eth_send(con->edev, con->packet, ETHER_HDR_SIZE + sizeof(struct iphdr) + len);
}
int net_udp_send(struct net_connection *con, int len)
{
con->udp->uh_ulen = htons(len + 8);
con->udp->uh_sum = 0;
return net_ip_send(con, sizeof(struct udphdr) + len);
}
int net_icmp_send(struct net_connection *con, int len)
{
con->icmp->checksum = ~net_checksum((unsigned char *)con->icmp,
sizeof(struct icmphdr) + len);
return net_ip_send(con, sizeof(struct icmphdr) + len);
}
static int net_answer_arp(struct eth_device *edev, unsigned char *pkt, int len)
{
struct arprequest *arp = (struct arprequest *)(pkt + ETHER_HDR_SIZE);
struct ethernet *et = (struct ethernet *)pkt;
unsigned char *packet;
int ret;
pr_debug("%s\n", __func__);
memcpy (et->et_dest, et->et_src, 6);
memcpy (et->et_src, edev->ethaddr, 6);
et->et_protlen = htons(PROT_ARP);
arp->ar_op = htons(ARPOP_REPLY);
memcpy(&arp->ar_data[10], &arp->ar_data[0], 6);
net_copy_ip(&arp->ar_data[16], &arp->ar_data[6]);
memcpy(&arp->ar_data[0], edev->ethaddr, 6);
net_copy_ip(&arp->ar_data[6], &edev->ipaddr);
packet = net_alloc_packet();
if (!packet)
return 0;
memcpy(packet, pkt, ETHER_HDR_SIZE + ARP_HDR_SIZE);
ret = eth_send(edev, packet, ETHER_HDR_SIZE + ARP_HDR_SIZE);
free(packet);
return ret;
}
static void net_bad_packet(unsigned char *pkt, int len)
{
#ifdef DEBUG
/*
* We received a bad packet. for now just dump it.
* We could add more sophisticated debugging here
*/
memory_display(pkt, 0, len, 1, 0);
#endif
}
static int net_handle_arp(struct eth_device *edev, unsigned char *pkt, int len)
{
struct arprequest *arp;
pr_debug("%s: got arp\n", __func__);
/*
* We have to deal with two types of ARP packets:
* - REQUEST packets will be answered by sending our
* IP address - if we know it.
* - REPLY packets are expected only after we asked
* for the TFTP server's or the gateway's ethernet
* address; so if we receive such a packet, we set
* the server ethernet address
*/
arp = (struct arprequest *)(pkt + ETHER_HDR_SIZE);
if (len < ARP_HDR_SIZE)
goto bad;
if (ntohs(arp->ar_hrd) != ARP_ETHER)
goto bad;
if (ntohs(arp->ar_pro) != PROT_IP)
goto bad;
if (arp->ar_hln != 6)
goto bad;
if (arp->ar_pln != 4)
goto bad;
if (edev->ipaddr == 0)
return 0;
if (net_read_ip(&arp->ar_data[16]) != edev->ipaddr)
return 0;
switch (ntohs(arp->ar_op)) {
case ARPOP_REQUEST:
return net_answer_arp(edev, pkt, len);
case ARPOP_REPLY:
arp_handler(arp);
return 1;
default:
pr_debug("Unexpected ARP opcode 0x%x\n", ntohs(arp->ar_op));
return -EINVAL;
}
bad:
net_bad_packet(pkt, len);
return -EINVAL;
}
static int net_handle_udp(unsigned char *pkt, int len)
{
struct iphdr *ip = (struct iphdr *)(pkt + ETHER_HDR_SIZE);
struct net_connection *con;
struct udphdr *udp;
int port;
udp = (struct udphdr *)(ip + 1);
port = ntohs(udp->uh_dport);
list_for_each_entry(con, &connection_list, list) {
if (con->proto == IPPROTO_UDP && port == ntohs(con->udp->uh_sport)) {
con->handler(con->priv, pkt, len);
return 0;
}
}
return -EINVAL;
}
static int ping_reply(struct eth_device *edev, unsigned char *pkt, int len)
{
struct ethernet *et = (struct ethernet *)pkt;
struct icmphdr *icmp;
struct iphdr *ip = (struct iphdr *)(pkt + ETHER_HDR_SIZE);
unsigned char *packet;
int ret;
memcpy(et->et_dest, et->et_src, 6);
memcpy(et->et_src, edev->ethaddr, 6);
et->et_protlen = htons(PROT_IP);
icmp = net_eth_to_icmphdr(pkt);
icmp->type = ICMP_ECHO_REPLY;
icmp->checksum = 0;
icmp->checksum = ~net_checksum((unsigned char *)icmp,
len - sizeof(struct iphdr) - ETHER_HDR_SIZE);
ip->check = 0;
ip->frag_off = 0;
net_copy_ip((void *)&ip->daddr, &ip->saddr);
net_copy_ip((void *)&ip->saddr, &edev->ipaddr);
ip->check = ~net_checksum((unsigned char *)ip, sizeof(struct iphdr));
packet = net_alloc_packet();
if (!packet)
return 0;
memcpy(packet, pkt, ETHER_HDR_SIZE + len);
ret = eth_send(edev, packet, ETHER_HDR_SIZE + len);
free(packet);
return ret;
}
static int net_handle_icmp(struct eth_device *edev, unsigned char *pkt, int len)
{
struct net_connection *con;
struct icmphdr *icmp;
pr_debug("%s\n", __func__);
icmp = net_eth_to_icmphdr(pkt);
if (icmp->type == ICMP_ECHO_REQUEST)
ping_reply(edev, pkt, len);
list_for_each_entry(con, &connection_list, list) {
if (con->proto == IPPROTO_ICMP) {
con->handler(con->priv, pkt, len);
return 0;
}
}
return 0;
}
static int net_handle_ip(struct eth_device *edev, unsigned char *pkt, int len)
{
struct iphdr *ip = (struct iphdr *)(pkt + ETHER_HDR_SIZE);
IPaddr_t tmp;
pr_debug("%s\n", __func__);
if (len < sizeof(struct ethernet) + sizeof(struct iphdr) ||
len < ETHER_HDR_SIZE + ntohs(ip->tot_len)) {
pr_debug("%s: bad len\n", __func__);
goto bad;
}
if ((ip->hl_v & 0xf0) != 0x40)
goto bad;
if (ip->frag_off & htons(0x1fff)) /* Can't deal w/ fragments */
goto bad;
if (!net_checksum_ok((unsigned char *)ip, sizeof(struct iphdr)))
goto bad;
tmp = net_read_ip(&ip->daddr);
if (edev->ipaddr && tmp != edev->ipaddr && tmp != IP_BROADCAST)
return 0;
switch (ip->protocol) {
case IPPROTO_ICMP:
return net_handle_icmp(edev, pkt, len);
case IPPROTO_UDP:
return net_handle_udp(pkt, len);
}
return 0;
bad:
net_bad_packet(pkt, len);
return 0;
}
int net_receive(struct eth_device *edev, unsigned char *pkt, int len)
{
struct ethernet *et = (struct ethernet *)pkt;
int et_protlen = ntohs(et->et_protlen);
int ret;
led_trigger_network(LED_TRIGGER_NET_RX);
if (len < ETHER_HDR_SIZE) {
ret = 0;
goto out;
}
if (edev->rx_monitor)
edev->rx_monitor(edev, pkt, len);
if (edev->rx_preprocessor) {
ret = edev->rx_preprocessor(edev, &pkt, &len);
if (ret == -ENOMSG)
return 0;
if (ret) {
pr_debug("%s: rx_preprocessor failed %pe\n", __func__,
ERR_PTR(ret));
return ret;
}
}
switch (et_protlen) {
case PROT_ARP:
ret = net_handle_arp(edev, pkt, len);
break;
case PROT_IP:
ret = net_handle_ip(edev, pkt, len);
break;
default:
pr_debug("%s: got unknown protocol type: %d\n", __func__, et_protlen);
ret = 1;
break;
}
out:
return ret;
}
static int net_init(void)
{
int i;
for (i = 0; i < PKTBUFSRX; i++)
NetRxPackets[i] = net_alloc_packet();
globalvar_add_simple_ip("net.nameserver", &net_nameserver);
globalvar_add_simple_string("net.domainname", &net_domainname);
globalvar_add_simple_string("net.server", &net_server);
globalvar_add_simple_ip("net.gateway", &net_gateway);
return 0;
}
postcore_initcall(net_init);
BAREBOX_MAGICVAR(global.net.nameserver, "The DNS server used for resolving host names");
BAREBOX_MAGICVAR(global.net.domainname, "Domain name used for DNS requests");
BAREBOX_MAGICVAR(global.net.server, "Standard server used for NFS/TFTP");
|