]> Git Repo - J-u-boot.git/blob - net/net.c
Merge branch 'master' of https://gitlab.denx.de/u-boot/custodians/u-boot-samsung
[J-u-boot.git] / net / net.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  *      Copied from Linux Monitor (LiMon) - Networking.
4  *
5  *      Copyright 1994 - 2000 Neil Russell.
6  *      (See License)
7  *      Copyright 2000 Roland Borde
8  *      Copyright 2000 Paolo Scaffardi
9  *      Copyright 2000-2002 Wolfgang Denk, [email protected]
10  */
11
12 /*
13  * General Desription:
14  *
15  * The user interface supports commands for BOOTP, RARP, and TFTP.
16  * Also, we support ARP internally. Depending on available data,
17  * these interact as follows:
18  *
19  * BOOTP:
20  *
21  *      Prerequisites:  - own ethernet address
22  *      We want:        - own IP address
23  *                      - TFTP server IP address
24  *                      - name of bootfile
25  *      Next step:      ARP
26  *
27  * LINKLOCAL:
28  *
29  *      Prerequisites:  - own ethernet address
30  *      We want:        - own IP address
31  *      Next step:      ARP
32  *
33  * RARP:
34  *
35  *      Prerequisites:  - own ethernet address
36  *      We want:        - own IP address
37  *                      - TFTP server IP address
38  *      Next step:      ARP
39  *
40  * ARP:
41  *
42  *      Prerequisites:  - own ethernet address
43  *                      - own IP address
44  *                      - TFTP server IP address
45  *      We want:        - TFTP server ethernet address
46  *      Next step:      TFTP
47  *
48  * DHCP:
49  *
50  *     Prerequisites:   - own ethernet address
51  *     We want:         - IP, Netmask, ServerIP, Gateway IP
52  *                      - bootfilename, lease time
53  *     Next step:       - TFTP
54  *
55  * TFTP:
56  *
57  *      Prerequisites:  - own ethernet address
58  *                      - own IP address
59  *                      - TFTP server IP address
60  *                      - TFTP server ethernet address
61  *                      - name of bootfile (if unknown, we use a default name
62  *                        derived from our own IP address)
63  *      We want:        - load the boot file
64  *      Next step:      none
65  *
66  * NFS:
67  *
68  *      Prerequisites:  - own ethernet address
69  *                      - own IP address
70  *                      - name of bootfile (if unknown, we use a default name
71  *                        derived from our own IP address)
72  *      We want:        - load the boot file
73  *      Next step:      none
74  *
75  *
76  * WOL:
77  *
78  *      Prerequisites:  - own ethernet address
79  *      We want:        - magic packet or timeout
80  *      Next step:      none
81  */
82
83 #include <bootstage.h>
84 #include <command.h>
85 #include <console.h>
86 #include <env.h>
87 #include <env_internal.h>
88 #include <errno.h>
89 #include <image.h>
90 #include <log.h>
91 #include <net.h>
92 #include <net6.h>
93 #include <ndisc.h>
94 #include <net/fastboot_udp.h>
95 #include <net/fastboot_tcp.h>
96 #include <net/tftp.h>
97 #include <net/ncsi.h>
98 #if defined(CONFIG_CMD_PCAP)
99 #include <net/pcap.h>
100 #endif
101 #include <net/udp.h>
102 #if defined(CONFIG_LED_STATUS)
103 #include <miiphy.h>
104 #include <status_led.h>
105 #endif
106 #include <watchdog.h>
107 #include <linux/compiler.h>
108 #include <test/test.h>
109 #include <net/tcp.h>
110 #include <net/wget.h>
111 #include "arp.h"
112 #include "bootp.h"
113 #include "cdp.h"
114 #if defined(CONFIG_CMD_DNS)
115 #include "dns.h"
116 #endif
117 #include "link_local.h"
118 #include "nfs.h"
119 #include "ping.h"
120 #include "rarp.h"
121 #if defined(CONFIG_CMD_WOL)
122 #include "wol.h"
123 #endif
124 #include "dhcpv6.h"
125 #include "net_rand.h"
126
127 /** BOOTP EXTENTIONS **/
128
129 /* Our subnet mask (0=unknown) */
130 struct in_addr net_netmask;
131 /* Our gateways IP address */
132 struct in_addr net_gateway;
133 /* Our DNS IP address */
134 struct in_addr net_dns_server;
135 #if defined(CONFIG_BOOTP_DNS2)
136 /* Our 2nd DNS IP address */
137 struct in_addr net_dns_server2;
138 #endif
139 /* Indicates whether the pxe path prefix / config file was specified in dhcp option */
140 char *pxelinux_configfile;
141
142 /** END OF BOOTP EXTENTIONS **/
143
144 /* Our ethernet address */
145 u8 net_ethaddr[6];
146 /* Boot server enet address */
147 u8 net_server_ethaddr[6];
148 /* Our IP addr (0 = unknown) */
149 struct in_addr  net_ip;
150 /* Server IP addr (0 = unknown) */
151 struct in_addr  net_server_ip;
152 /* Current receive packet */
153 uchar *net_rx_packet;
154 /* Current rx packet length */
155 int             net_rx_packet_len;
156 /* IP packet ID */
157 static unsigned net_ip_id;
158 /* Ethernet bcast address */
159 const u8 net_bcast_ethaddr[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
160 const u8 net_null_ethaddr[6];
161 #if defined(CONFIG_API) || defined(CONFIG_EFI_LOADER)
162 void (*push_packet)(void *, int len) = 0;
163 #endif
164 /* Network loop state */
165 enum net_loop_state net_state;
166 /* Tried all network devices */
167 int             net_restart_wrap;
168 /* Network loop restarted */
169 static int      net_restarted;
170 /* At least one device configured */
171 static int      net_dev_exists;
172
173 /* XXX in both little & big endian machines 0xFFFF == ntohs(-1) */
174 /* default is without VLAN */
175 ushort          net_our_vlan = 0xFFFF;
176 /* ditto */
177 ushort          net_native_vlan = 0xFFFF;
178
179 /* Boot File name */
180 char net_boot_file_name[1024];
181 /* Indicates whether the file name was specified on the command line */
182 bool net_boot_file_name_explicit;
183 /* The actual transferred size of the bootfile (in bytes) */
184 u32 net_boot_file_size;
185 /* Boot file size in blocks as reported by the DHCP server */
186 u32 net_boot_file_expected_size_in_blocks;
187
188 static uchar net_pkt_buf[(PKTBUFSRX+1) * PKTSIZE_ALIGN + PKTALIGN];
189 /* Receive packets */
190 uchar *net_rx_packets[PKTBUFSRX];
191 /* Current UDP RX packet handler */
192 static rxhand_f *udp_packet_handler;
193 /* Current ARP RX packet handler */
194 static rxhand_f *arp_packet_handler;
195 #ifdef CONFIG_CMD_TFTPPUT
196 /* Current ICMP rx handler */
197 static rxhand_icmp_f *packet_icmp_handler;
198 #endif
199 /* Current timeout handler */
200 static thand_f *time_handler;
201 /* Time base value */
202 static ulong    time_start;
203 /* Current timeout value */
204 static ulong    time_delta;
205 /* THE transmit packet */
206 uchar *net_tx_packet;
207
208 static int net_check_prereq(enum proto_t protocol);
209
210 static int net_try_count;
211
212 int __maybe_unused net_busy_flag;
213
214 /**********************************************************************/
215
216 static int on_ipaddr(const char *name, const char *value, enum env_op op,
217         int flags)
218 {
219         if (flags & H_PROGRAMMATIC)
220                 return 0;
221
222         net_ip = string_to_ip(value);
223
224         return 0;
225 }
226 U_BOOT_ENV_CALLBACK(ipaddr, on_ipaddr);
227
228 static int on_gatewayip(const char *name, const char *value, enum env_op op,
229         int flags)
230 {
231         if (flags & H_PROGRAMMATIC)
232                 return 0;
233
234         net_gateway = string_to_ip(value);
235
236         return 0;
237 }
238 U_BOOT_ENV_CALLBACK(gatewayip, on_gatewayip);
239
240 static int on_netmask(const char *name, const char *value, enum env_op op,
241         int flags)
242 {
243         if (flags & H_PROGRAMMATIC)
244                 return 0;
245
246         net_netmask = string_to_ip(value);
247
248         return 0;
249 }
250 U_BOOT_ENV_CALLBACK(netmask, on_netmask);
251
252 static int on_serverip(const char *name, const char *value, enum env_op op,
253         int flags)
254 {
255         if (flags & H_PROGRAMMATIC)
256                 return 0;
257
258         net_server_ip = string_to_ip(value);
259
260         return 0;
261 }
262 U_BOOT_ENV_CALLBACK(serverip, on_serverip);
263
264 static int on_nvlan(const char *name, const char *value, enum env_op op,
265         int flags)
266 {
267         if (flags & H_PROGRAMMATIC)
268                 return 0;
269
270         net_native_vlan = string_to_vlan(value);
271
272         return 0;
273 }
274 U_BOOT_ENV_CALLBACK(nvlan, on_nvlan);
275
276 static int on_vlan(const char *name, const char *value, enum env_op op,
277         int flags)
278 {
279         if (flags & H_PROGRAMMATIC)
280                 return 0;
281
282         net_our_vlan = string_to_vlan(value);
283
284         return 0;
285 }
286 U_BOOT_ENV_CALLBACK(vlan, on_vlan);
287
288 #if defined(CONFIG_CMD_DNS)
289 static int on_dnsip(const char *name, const char *value, enum env_op op,
290         int flags)
291 {
292         if (flags & H_PROGRAMMATIC)
293                 return 0;
294
295         net_dns_server = string_to_ip(value);
296
297         return 0;
298 }
299 U_BOOT_ENV_CALLBACK(dnsip, on_dnsip);
300 #endif
301
302 /*
303  * Check if autoload is enabled. If so, use either NFS or TFTP to download
304  * the boot file.
305  */
306 void net_auto_load(void)
307 {
308 #if defined(CONFIG_CMD_NFS) && !defined(CONFIG_SPL_BUILD)
309         const char *s = env_get("autoload");
310
311         if (s != NULL && strcmp(s, "NFS") == 0) {
312                 if (net_check_prereq(NFS)) {
313 /* We aren't expecting to get a serverip, so just accept the assigned IP */
314                         if (IS_ENABLED(CONFIG_BOOTP_SERVERIP)) {
315                                 net_set_state(NETLOOP_SUCCESS);
316                         } else {
317                                 printf("Cannot autoload with NFS\n");
318                                 net_set_state(NETLOOP_FAIL);
319                         }
320                         return;
321                 }
322                 /*
323                  * Use NFS to load the bootfile.
324                  */
325                 nfs_start();
326                 return;
327         }
328 #endif
329         if (env_get_yesno("autoload") == 0) {
330                 /*
331                  * Just use BOOTP/RARP to configure system;
332                  * Do not use TFTP to load the bootfile.
333                  */
334                 net_set_state(NETLOOP_SUCCESS);
335                 return;
336         }
337         if (net_check_prereq(TFTPGET)) {
338 /* We aren't expecting to get a serverip, so just accept the assigned IP */
339                 if (IS_ENABLED(CONFIG_BOOTP_SERVERIP)) {
340                         net_set_state(NETLOOP_SUCCESS);
341                 } else {
342                         printf("Cannot autoload with TFTPGET\n");
343                         net_set_state(NETLOOP_FAIL);
344                 }
345                 return;
346         }
347         tftp_start(TFTPGET);
348 }
349
350 static int net_init_loop(void)
351 {
352         static bool first_call = true;
353
354         if (eth_get_dev()) {
355                 memcpy(net_ethaddr, eth_get_ethaddr(), 6);
356
357                 if (IS_ENABLED(CONFIG_IPV6)) {
358                         ip6_make_lladdr(&net_link_local_ip6, net_ethaddr);
359                         if (!memcmp(&net_ip6, &net_null_addr_ip6,
360                                     sizeof(struct in6_addr)))
361                                 memcpy(&net_ip6, &net_link_local_ip6,
362                                        sizeof(struct in6_addr));
363                 }
364         }
365         else
366                 /*
367                  * Not ideal, but there's no way to get the actual error, and I
368                  * don't feel like fixing all the users of eth_get_dev to deal
369                  * with errors.
370                  */
371                 return -ENONET;
372
373         if (IS_ENABLED(CONFIG_IPV6_ROUTER_DISCOVERY))
374                 if (first_call && use_ip6) {
375                         first_call = false;
376                         srand_mac(); /* This is for rand used in ip6_send_rs. */
377                         net_loop(RS);
378                 }
379         return 0;
380 }
381
382 static void net_clear_handlers(void)
383 {
384         net_set_udp_handler(NULL);
385         net_set_arp_handler(NULL);
386         net_set_timeout_handler(0, NULL);
387 }
388
389 static void net_cleanup_loop(void)
390 {
391         net_clear_handlers();
392 }
393
394 int net_init(void)
395 {
396         static int first_call = 1;
397
398         if (first_call) {
399                 /*
400                  *      Setup packet buffers, aligned correctly.
401                  */
402                 int i;
403
404                 net_tx_packet = &net_pkt_buf[0] + (PKTALIGN - 1);
405                 net_tx_packet -= (ulong)net_tx_packet % PKTALIGN;
406                 for (i = 0; i < PKTBUFSRX; i++) {
407                         net_rx_packets[i] = net_tx_packet +
408                                 (i + 1) * PKTSIZE_ALIGN;
409                 }
410                 arp_init();
411                 ndisc_init();
412                 net_clear_handlers();
413
414                 /* Only need to setup buffer pointers once. */
415                 first_call = 0;
416                 if (IS_ENABLED(CONFIG_PROT_TCP))
417                         tcp_set_tcp_state(TCP_CLOSED);
418         }
419
420         return net_init_loop();
421 }
422
423 /**********************************************************************/
424 /*
425  *      Main network processing loop.
426  */
427
428 int net_loop(enum proto_t protocol)
429 {
430         int ret = -EINVAL;
431         enum net_loop_state prev_net_state = net_state;
432
433 #if defined(CONFIG_CMD_PING)
434         if (protocol != PING)
435                 net_ping_ip.s_addr = 0;
436 #endif
437         net_restarted = 0;
438         net_dev_exists = 0;
439         net_try_count = 1;
440         debug_cond(DEBUG_INT_STATE, "--- net_loop Entry\n");
441
442 #ifdef CONFIG_PHY_NCSI
443         if (phy_interface_is_ncsi() && protocol != NCSI && !ncsi_active()) {
444                 printf("%s: configuring NCSI first\n", __func__);
445                 if (net_loop(NCSI) < 0)
446                         return ret;
447                 eth_init_state_only();
448                 goto restart;
449         }
450 #endif
451
452         bootstage_mark_name(BOOTSTAGE_ID_ETH_START, "eth_start");
453         net_init();
454         if (eth_is_on_demand_init()) {
455                 eth_halt();
456                 eth_set_current();
457                 ret = eth_init();
458                 if (ret < 0) {
459                         eth_halt();
460                         return ret;
461                 }
462         } else {
463                 eth_init_state_only();
464         }
465
466 restart:
467 #ifdef CONFIG_USB_KEYBOARD
468         net_busy_flag = 0;
469 #endif
470         net_set_state(NETLOOP_CONTINUE);
471
472         /*
473          *      Start the ball rolling with the given start function.  From
474          *      here on, this code is a state machine driven by received
475          *      packets and timer events.
476          */
477         debug_cond(DEBUG_INT_STATE, "--- net_loop Init\n");
478         net_init_loop();
479
480         if (!test_eth_enabled())
481                 return 0;
482
483         switch (net_check_prereq(protocol)) {
484         case 1:
485                 /* network not configured */
486                 eth_halt();
487                 net_set_state(prev_net_state);
488                 return -ENODEV;
489
490         case 2:
491                 /* network device not configured */
492                 break;
493
494         case 0:
495                 net_dev_exists = 1;
496                 net_boot_file_size = 0;
497                 switch (protocol) {
498 #ifdef CONFIG_CMD_TFTPBOOT
499                 case TFTPGET:
500 #ifdef CONFIG_CMD_TFTPPUT
501                 case TFTPPUT:
502 #endif
503                         /* always use ARP to get server ethernet address */
504                         tftp_start(protocol);
505                         break;
506 #endif
507 #ifdef CONFIG_CMD_TFTPSRV
508                 case TFTPSRV:
509                         tftp_start_server();
510                         break;
511 #endif
512 #if CONFIG_IS_ENABLED(UDP_FUNCTION_FASTBOOT)
513                 case FASTBOOT_UDP:
514                         fastboot_udp_start_server();
515                         break;
516 #endif
517 #if CONFIG_IS_ENABLED(TCP_FUNCTION_FASTBOOT)
518                 case FASTBOOT_TCP:
519                         fastboot_tcp_start_server();
520                         break;
521 #endif
522 #if defined(CONFIG_CMD_DHCP)
523                 case DHCP:
524                         bootp_reset();
525                         net_ip.s_addr = 0;
526                         dhcp_request();         /* Basically same as BOOTP */
527                         break;
528 #endif
529                 case DHCP6:
530                         if (IS_ENABLED(CONFIG_CMD_DHCP6))
531                                 dhcp6_start();
532                         break;
533 #if defined(CONFIG_CMD_BOOTP)
534                 case BOOTP:
535                         bootp_reset();
536                         net_ip.s_addr = 0;
537                         bootp_request();
538                         break;
539 #endif
540 #if defined(CONFIG_CMD_RARP)
541                 case RARP:
542                         rarp_try = 0;
543                         net_ip.s_addr = 0;
544                         rarp_request();
545                         break;
546 #endif
547 #if defined(CONFIG_CMD_PING)
548                 case PING:
549                         ping_start();
550                         break;
551 #endif
552 #if defined(CONFIG_CMD_PING6)
553                 case PING6:
554                         ping6_start();
555                         break;
556 #endif
557 #if defined(CONFIG_CMD_NFS) && !defined(CONFIG_SPL_BUILD)
558                 case NFS:
559                         nfs_start();
560                         break;
561 #endif
562 #if defined(CONFIG_CMD_WGET)
563                 case WGET:
564                         wget_start();
565                         break;
566 #endif
567 #if defined(CONFIG_CMD_CDP)
568                 case CDP:
569                         cdp_start();
570                         break;
571 #endif
572 #if defined(CONFIG_NETCONSOLE) && !defined(CONFIG_SPL_BUILD)
573                 case NETCONS:
574                         nc_start();
575                         break;
576 #endif
577 #if defined(CONFIG_CMD_DNS)
578                 case DNS:
579                         dns_start();
580                         break;
581 #endif
582 #if defined(CONFIG_CMD_LINK_LOCAL)
583                 case LINKLOCAL:
584                         link_local_start();
585                         break;
586 #endif
587 #if defined(CONFIG_CMD_WOL)
588                 case WOL:
589                         wol_start();
590                         break;
591 #endif
592 #if defined(CONFIG_PHY_NCSI)
593                 case NCSI:
594                         ncsi_probe_packages();
595                         break;
596 #endif
597                 case RS:
598                         if (IS_ENABLED(CONFIG_IPV6_ROUTER_DISCOVERY))
599                                 ip6_send_rs();
600                         break;
601                 default:
602                         break;
603                 }
604
605                 if (IS_ENABLED(CONFIG_PROT_UDP) && protocol == UDP)
606                         udp_start();
607
608                 break;
609         }
610
611 #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
612 #if     defined(CONFIG_SYS_FAULT_ECHO_LINK_DOWN)        && \
613         defined(CONFIG_LED_STATUS)                      && \
614         defined(CONFIG_LED_STATUS_RED)
615         /*
616          * Echo the inverted link state to the fault LED.
617          */
618         if (miiphy_link(eth_get_dev()->name, CONFIG_SYS_FAULT_MII_ADDR))
619                 status_led_set(CONFIG_LED_STATUS_RED, CONFIG_LED_STATUS_OFF);
620         else
621                 status_led_set(CONFIG_LED_STATUS_RED, CONFIG_LED_STATUS_ON);
622 #endif /* CONFIG_SYS_FAULT_ECHO_LINK_DOWN, ... */
623 #endif /* CONFIG_MII, ... */
624 #ifdef CONFIG_USB_KEYBOARD
625         net_busy_flag = 1;
626 #endif
627
628         /*
629          *      Main packet reception loop.  Loop receiving packets until
630          *      someone sets `net_state' to a state that terminates.
631          */
632         for (;;) {
633                 schedule();
634                 if (arp_timeout_check() > 0)
635                         time_start = get_timer(0);
636
637                 if (IS_ENABLED(CONFIG_IPV6)) {
638                         if (use_ip6 && (ndisc_timeout_check() > 0))
639                                 time_start = get_timer(0);
640                 }
641
642                 /*
643                  *      Check the ethernet for a new packet.  The ethernet
644                  *      receive routine will process it.
645                  *      Most drivers return the most recent packet size, but not
646                  *      errors that may have happened.
647                  */
648                 eth_rx();
649
650                 /*
651                  *      Abort if ctrl-c was pressed.
652                  */
653                 if (ctrlc()) {
654                         /* cancel any ARP that may not have completed */
655                         net_arp_wait_packet_ip.s_addr = 0;
656
657                         net_cleanup_loop();
658                         eth_halt();
659                         /* Invalidate the last protocol */
660                         eth_set_last_protocol(BOOTP);
661
662                         puts("\nAbort\n");
663                         /* include a debug print as well incase the debug
664                            messages are directed to stderr */
665                         debug_cond(DEBUG_INT_STATE, "--- net_loop Abort!\n");
666                         ret = -EINTR;
667                         goto done;
668                 }
669
670                 /*
671                  *      Check for a timeout, and run the timeout handler
672                  *      if we have one.
673                  */
674                 if (time_handler &&
675                     ((get_timer(0) - time_start) > time_delta)) {
676                         thand_f *x;
677
678 #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
679 #if     defined(CONFIG_SYS_FAULT_ECHO_LINK_DOWN)        && \
680         defined(CONFIG_LED_STATUS)                      && \
681         defined(CONFIG_LED_STATUS_RED)
682                         /*
683                          * Echo the inverted link state to the fault LED.
684                          */
685                         if (miiphy_link(eth_get_dev()->name,
686                                         CONFIG_SYS_FAULT_MII_ADDR))
687                                 status_led_set(CONFIG_LED_STATUS_RED,
688                                                CONFIG_LED_STATUS_OFF);
689                         else
690                                 status_led_set(CONFIG_LED_STATUS_RED,
691                                                CONFIG_LED_STATUS_ON);
692 #endif /* CONFIG_SYS_FAULT_ECHO_LINK_DOWN, ... */
693 #endif /* CONFIG_MII, ... */
694                         debug_cond(DEBUG_INT_STATE, "--- net_loop timeout\n");
695                         x = time_handler;
696                         time_handler = (thand_f *)0;
697                         (*x)();
698                 } else if (IS_ENABLED(CONFIG_IPV6_ROUTER_DISCOVERY))
699                         if (time_handler && protocol == RS)
700                                 if (!ip6_is_unspecified_addr(&net_gateway6) &&
701                                     net_prefix_length != 0) {
702                                         net_set_state(NETLOOP_SUCCESS);
703                                         net_set_timeout_handler(0, NULL);
704                                 }
705
706                 if (net_state == NETLOOP_FAIL)
707                         ret = net_start_again();
708
709                 switch (net_state) {
710                 case NETLOOP_RESTART:
711                         net_restarted = 1;
712                         goto restart;
713
714                 case NETLOOP_SUCCESS:
715                         net_cleanup_loop();
716                         if (net_boot_file_size > 0) {
717                                 printf("Bytes transferred = %u (%x hex)\n",
718                                        net_boot_file_size, net_boot_file_size);
719                                 env_set_hex("filesize", net_boot_file_size);
720                                 env_set_hex("fileaddr", image_load_addr);
721                         }
722                         if (protocol != NETCONS && protocol != NCSI)
723                                 eth_halt();
724                         else
725                                 eth_halt_state_only();
726
727                         eth_set_last_protocol(protocol);
728
729                         ret = net_boot_file_size;
730                         debug_cond(DEBUG_INT_STATE, "--- net_loop Success!\n");
731                         goto done;
732
733                 case NETLOOP_FAIL:
734                         net_cleanup_loop();
735                         /* Invalidate the last protocol */
736                         eth_set_last_protocol(BOOTP);
737                         debug_cond(DEBUG_INT_STATE, "--- net_loop Fail!\n");
738                         ret = -ENONET;
739                         goto done;
740
741                 case NETLOOP_CONTINUE:
742                         continue;
743                 }
744         }
745
746 done:
747 #ifdef CONFIG_USB_KEYBOARD
748         net_busy_flag = 0;
749 #endif
750 #ifdef CONFIG_CMD_TFTPPUT
751         /* Clear out the handlers */
752         net_set_udp_handler(NULL);
753         net_set_icmp_handler(NULL);
754 #endif
755         net_set_state(prev_net_state);
756
757 #if defined(CONFIG_CMD_PCAP)
758         if (pcap_active())
759                 pcap_print_status();
760 #endif
761         return ret;
762 }
763
764 /**********************************************************************/
765
766 static void start_again_timeout_handler(void)
767 {
768         net_set_state(NETLOOP_RESTART);
769 }
770
771 int net_start_again(void)
772 {
773         char *nretry;
774         int retry_forever = 0;
775         unsigned long retrycnt = 0;
776         int ret;
777
778         nretry = env_get("netretry");
779         if (nretry) {
780                 if (!strcmp(nretry, "yes"))
781                         retry_forever = 1;
782                 else if (!strcmp(nretry, "no"))
783                         retrycnt = 0;
784                 else if (!strcmp(nretry, "once"))
785                         retrycnt = 1;
786                 else
787                         retrycnt = simple_strtoul(nretry, NULL, 0);
788         } else {
789                 retrycnt = 0;
790                 retry_forever = 0;
791         }
792
793         if ((!retry_forever) && (net_try_count > retrycnt)) {
794                 eth_halt();
795                 net_set_state(NETLOOP_FAIL);
796                 /*
797                  * We don't provide a way for the protocol to return an error,
798                  * but this is almost always the reason.
799                  */
800                 return -ETIMEDOUT;
801         }
802
803         net_try_count++;
804
805         eth_halt();
806 #if !defined(CONFIG_NET_DO_NOT_TRY_ANOTHER)
807         eth_try_another(!net_restarted);
808 #endif
809         ret = eth_init();
810         if (net_restart_wrap) {
811                 net_restart_wrap = 0;
812                 if (net_dev_exists) {
813                         net_set_timeout_handler(10000UL,
814                                                 start_again_timeout_handler);
815                         net_set_udp_handler(NULL);
816                 } else {
817                         net_set_state(NETLOOP_FAIL);
818                 }
819         } else {
820                 net_set_state(NETLOOP_RESTART);
821         }
822         return ret;
823 }
824
825 /**********************************************************************/
826 /*
827  *      Miscelaneous bits.
828  */
829
830 static void dummy_handler(uchar *pkt, unsigned dport,
831                         struct in_addr sip, unsigned sport,
832                         unsigned len)
833 {
834 }
835
836 rxhand_f *net_get_udp_handler(void)
837 {
838         return udp_packet_handler;
839 }
840
841 void net_set_udp_handler(rxhand_f *f)
842 {
843         debug_cond(DEBUG_INT_STATE, "--- net_loop UDP handler set (%p)\n", f);
844         if (f == NULL)
845                 udp_packet_handler = dummy_handler;
846         else
847                 udp_packet_handler = f;
848 }
849
850 rxhand_f *net_get_arp_handler(void)
851 {
852         return arp_packet_handler;
853 }
854
855 void net_set_arp_handler(rxhand_f *f)
856 {
857         debug_cond(DEBUG_INT_STATE, "--- net_loop ARP handler set (%p)\n", f);
858         if (f == NULL)
859                 arp_packet_handler = dummy_handler;
860         else
861                 arp_packet_handler = f;
862 }
863
864 #ifdef CONFIG_CMD_TFTPPUT
865 void net_set_icmp_handler(rxhand_icmp_f *f)
866 {
867         packet_icmp_handler = f;
868 }
869 #endif
870
871 void net_set_timeout_handler(ulong iv, thand_f *f)
872 {
873         if (iv == 0) {
874                 debug_cond(DEBUG_INT_STATE,
875                            "--- net_loop timeout handler cancelled\n");
876                 time_handler = (thand_f *)0;
877         } else {
878                 debug_cond(DEBUG_INT_STATE,
879                            "--- net_loop timeout handler set (%p)\n", f);
880                 time_handler = f;
881                 time_start = get_timer(0);
882                 time_delta = iv * CONFIG_SYS_HZ / 1000;
883         }
884 }
885
886 uchar *net_get_async_tx_pkt_buf(void)
887 {
888         if (arp_is_waiting())
889                 return arp_tx_packet; /* If we are waiting, we already sent */
890         else
891                 return net_tx_packet;
892 }
893
894 int net_send_udp_packet(uchar *ether, struct in_addr dest, int dport, int sport,
895                 int payload_len)
896 {
897         return net_send_ip_packet(ether, dest, dport, sport, payload_len,
898                                   IPPROTO_UDP, 0, 0, 0);
899 }
900
901 #if defined(CONFIG_PROT_TCP)
902 int net_send_tcp_packet(int payload_len, int dport, int sport, u8 action,
903                         u32 tcp_seq_num, u32 tcp_ack_num)
904 {
905         return net_send_ip_packet(net_server_ethaddr, net_server_ip, dport,
906                                   sport, payload_len, IPPROTO_TCP, action,
907                                   tcp_seq_num, tcp_ack_num);
908 }
909 #endif
910
911 int net_send_ip_packet(uchar *ether, struct in_addr dest, int dport, int sport,
912                        int payload_len, int proto, u8 action, u32 tcp_seq_num,
913                        u32 tcp_ack_num)
914 {
915         uchar *pkt;
916         int eth_hdr_size;
917         int pkt_hdr_size;
918
919         /* make sure the net_tx_packet is initialized (net_init() was called) */
920         assert(net_tx_packet != NULL);
921         if (net_tx_packet == NULL)
922                 return -1;
923
924         /* convert to new style broadcast */
925         if (dest.s_addr == 0)
926                 dest.s_addr = 0xFFFFFFFF;
927
928         /* if broadcast, make the ether address a broadcast and don't do ARP */
929         if (dest.s_addr == 0xFFFFFFFF)
930                 ether = (uchar *)net_bcast_ethaddr;
931
932         pkt = (uchar *)net_tx_packet;
933
934         eth_hdr_size = net_set_ether(pkt, ether, PROT_IP);
935
936         switch (proto) {
937         case IPPROTO_UDP:
938                 net_set_udp_header(pkt + eth_hdr_size, dest, dport, sport,
939                                    payload_len);
940                 pkt_hdr_size = eth_hdr_size + IP_UDP_HDR_SIZE;
941                 break;
942 #if defined(CONFIG_PROT_TCP)
943         case IPPROTO_TCP:
944                 pkt_hdr_size = eth_hdr_size
945                         + tcp_set_tcp_header(pkt + eth_hdr_size, dport, sport,
946                                              payload_len, action, tcp_seq_num,
947                                              tcp_ack_num);
948                 break;
949 #endif
950         default:
951                 return -EINVAL;
952         }
953
954         /* if MAC address was not discovered yet, do an ARP request */
955         if (memcmp(ether, net_null_ethaddr, 6) == 0) {
956                 debug_cond(DEBUG_DEV_PKT, "sending ARP for %pI4\n", &dest);
957
958                 /* save the ip and eth addr for the packet to send after arp */
959                 net_arp_wait_packet_ip = dest;
960                 arp_wait_packet_ethaddr = ether;
961
962                 /* size of the waiting packet */
963                 arp_wait_tx_packet_size = pkt_hdr_size + payload_len;
964
965                 /* and do the ARP request */
966                 arp_wait_try = 1;
967                 arp_wait_timer_start = get_timer(0);
968                 arp_request();
969                 return 1;       /* waiting */
970         } else {
971                 debug_cond(DEBUG_DEV_PKT, "sending UDP to %pI4/%pM\n",
972                            &dest, ether);
973                 net_send_packet(net_tx_packet, pkt_hdr_size + payload_len);
974                 return 0;       /* transmitted */
975         }
976 }
977
978 #ifdef CONFIG_IP_DEFRAG
979 /*
980  * This function collects fragments in a single packet, according
981  * to the algorithm in RFC815. It returns NULL or the pointer to
982  * a complete packet, in static storage
983  */
984 #define IP_PKTSIZE (CONFIG_NET_MAXDEFRAG)
985
986 #define IP_MAXUDP (IP_PKTSIZE - IP_HDR_SIZE)
987
988 /*
989  * this is the packet being assembled, either data or frag control.
990  * Fragments go by 8 bytes, so this union must be 8 bytes long
991  */
992 struct hole {
993         /* first_byte is address of this structure */
994         u16 last_byte;  /* last byte in this hole + 1 (begin of next hole) */
995         u16 next_hole;  /* index of next (in 8-b blocks), 0 == none */
996         u16 prev_hole;  /* index of prev, 0 == none */
997         u16 unused;
998 };
999
1000 static struct ip_udp_hdr *__net_defragment(struct ip_udp_hdr *ip, int *lenp)
1001 {
1002         static uchar pkt_buff[IP_PKTSIZE] __aligned(PKTALIGN);
1003         static u16 first_hole, total_len;
1004         struct hole *payload, *thisfrag, *h, *newh;
1005         struct ip_udp_hdr *localip = (struct ip_udp_hdr *)pkt_buff;
1006         uchar *indata = (uchar *)ip;
1007         int offset8, start, len, done = 0;
1008         u16 ip_off = ntohs(ip->ip_off);
1009
1010         /*
1011          * Calling code already rejected <, but we don't have to deal
1012          * with an IP fragment with no payload.
1013          */
1014         if (ntohs(ip->ip_len) <= IP_HDR_SIZE)
1015                 return NULL;
1016
1017         /* payload starts after IP header, this fragment is in there */
1018         payload = (struct hole *)(pkt_buff + IP_HDR_SIZE);
1019         offset8 =  (ip_off & IP_OFFS);
1020         thisfrag = payload + offset8;
1021         start = offset8 * 8;
1022         len = ntohs(ip->ip_len) - IP_HDR_SIZE;
1023
1024         /* All but last fragment must have a multiple-of-8 payload. */
1025         if ((len & 7) && (ip_off & IP_FLAGS_MFRAG))
1026                 return NULL;
1027
1028         if (start + len > IP_MAXUDP) /* fragment extends too far */
1029                 return NULL;
1030
1031         if (!total_len || localip->ip_id != ip->ip_id) {
1032                 /* new (or different) packet, reset structs */
1033                 total_len = 0xffff;
1034                 payload[0].last_byte = ~0;
1035                 payload[0].next_hole = 0;
1036                 payload[0].prev_hole = 0;
1037                 first_hole = 0;
1038                 /* any IP header will work, copy the first we received */
1039                 memcpy(localip, ip, IP_HDR_SIZE);
1040         }
1041
1042         /*
1043          * What follows is the reassembly algorithm. We use the payload
1044          * array as a linked list of hole descriptors, as each hole starts
1045          * at a multiple of 8 bytes. However, last byte can be whatever value,
1046          * so it is represented as byte count, not as 8-byte blocks.
1047          */
1048
1049         h = payload + first_hole;
1050         while (h->last_byte < start) {
1051                 if (!h->next_hole) {
1052                         /* no hole that far away */
1053                         return NULL;
1054                 }
1055                 h = payload + h->next_hole;
1056         }
1057
1058         /* last fragment may be 1..7 bytes, the "+7" forces acceptance */
1059         if (offset8 + ((len + 7) / 8) <= h - payload) {
1060                 /* no overlap with holes (dup fragment?) */
1061                 return NULL;
1062         }
1063
1064         if (!(ip_off & IP_FLAGS_MFRAG)) {
1065                 /* no more fragmentss: truncate this (last) hole */
1066                 total_len = start + len;
1067                 h->last_byte = start + len;
1068         }
1069
1070         /*
1071          * There is some overlap: fix the hole list. This code deals
1072          * with a fragment that overlaps with two different holes
1073          * (thus being a superset of a previously-received fragment)
1074          * by only using the part of the fragment that fits in the
1075          * first hole.
1076          */
1077         if (h->last_byte < start + len)
1078                 len = h->last_byte - start;
1079
1080         if ((h >= thisfrag) && (h->last_byte <= start + len)) {
1081                 /* complete overlap with hole: remove hole */
1082                 if (!h->prev_hole && !h->next_hole) {
1083                         /* last remaining hole */
1084                         done = 1;
1085                 } else if (!h->prev_hole) {
1086                         /* first hole */
1087                         first_hole = h->next_hole;
1088                         payload[h->next_hole].prev_hole = 0;
1089                 } else if (!h->next_hole) {
1090                         /* last hole */
1091                         payload[h->prev_hole].next_hole = 0;
1092                 } else {
1093                         /* in the middle of the list */
1094                         payload[h->next_hole].prev_hole = h->prev_hole;
1095                         payload[h->prev_hole].next_hole = h->next_hole;
1096                 }
1097
1098         } else if (h->last_byte <= start + len) {
1099                 /* overlaps with final part of the hole: shorten this hole */
1100                 h->last_byte = start;
1101
1102         } else if (h >= thisfrag) {
1103                 /* overlaps with initial part of the hole: move this hole */
1104                 newh = thisfrag + (len / 8);
1105                 *newh = *h;
1106                 h = newh;
1107                 if (h->next_hole)
1108                         payload[h->next_hole].prev_hole = (h - payload);
1109                 if (h->prev_hole)
1110                         payload[h->prev_hole].next_hole = (h - payload);
1111                 else
1112                         first_hole = (h - payload);
1113
1114         } else {
1115                 /* fragment sits in the middle: split the hole */
1116                 newh = thisfrag + (len / 8);
1117                 *newh = *h;
1118                 h->last_byte = start;
1119                 h->next_hole = (newh - payload);
1120                 newh->prev_hole = (h - payload);
1121                 if (newh->next_hole)
1122                         payload[newh->next_hole].prev_hole = (newh - payload);
1123         }
1124
1125         /* finally copy this fragment and possibly return whole packet */
1126         memcpy((uchar *)thisfrag, indata + IP_HDR_SIZE, len);
1127         if (!done)
1128                 return NULL;
1129
1130         *lenp = total_len + IP_HDR_SIZE;
1131         localip->ip_len = htons(*lenp);
1132         return localip;
1133 }
1134
1135 static inline struct ip_udp_hdr *net_defragment(struct ip_udp_hdr *ip,
1136         int *lenp)
1137 {
1138         u16 ip_off = ntohs(ip->ip_off);
1139         if (!(ip_off & (IP_OFFS | IP_FLAGS_MFRAG)))
1140                 return ip; /* not a fragment */
1141         return __net_defragment(ip, lenp);
1142 }
1143
1144 #else /* !CONFIG_IP_DEFRAG */
1145
1146 static inline struct ip_udp_hdr *net_defragment(struct ip_udp_hdr *ip,
1147         int *lenp)
1148 {
1149         u16 ip_off = ntohs(ip->ip_off);
1150         if (!(ip_off & (IP_OFFS | IP_FLAGS_MFRAG)))
1151                 return ip; /* not a fragment */
1152         return NULL;
1153 }
1154 #endif
1155
1156 /**
1157  * Receive an ICMP packet. We deal with REDIRECT and PING here, and silently
1158  * drop others.
1159  *
1160  * @parma ip    IP packet containing the ICMP
1161  */
1162 static void receive_icmp(struct ip_udp_hdr *ip, int len,
1163                         struct in_addr src_ip, struct ethernet_hdr *et)
1164 {
1165         struct icmp_hdr *icmph = (struct icmp_hdr *)&ip->udp_src;
1166
1167         switch (icmph->type) {
1168         case ICMP_REDIRECT:
1169                 if (icmph->code != ICMP_REDIR_HOST)
1170                         return;
1171                 printf(" ICMP Host Redirect to %pI4 ",
1172                        &icmph->un.gateway);
1173                 break;
1174         default:
1175 #if defined(CONFIG_CMD_PING)
1176                 ping_receive(et, ip, len);
1177 #endif
1178 #ifdef CONFIG_CMD_TFTPPUT
1179                 if (packet_icmp_handler)
1180                         packet_icmp_handler(icmph->type, icmph->code,
1181                                             ntohs(ip->udp_dst), src_ip,
1182                                             ntohs(ip->udp_src), icmph->un.data,
1183                                             ntohs(ip->udp_len));
1184 #endif
1185                 break;
1186         }
1187 }
1188
1189 void net_process_received_packet(uchar *in_packet, int len)
1190 {
1191         struct ethernet_hdr *et;
1192         struct ip_udp_hdr *ip;
1193         struct in_addr dst_ip;
1194         struct in_addr src_ip;
1195         int eth_proto;
1196 #if defined(CONFIG_CMD_CDP)
1197         int iscdp;
1198 #endif
1199         ushort cti = 0, vlanid = VLAN_NONE, myvlanid, mynvlanid;
1200
1201         debug_cond(DEBUG_NET_PKT, "packet received\n");
1202         if (DEBUG_NET_PKT_TRACE)
1203                 print_hex_dump_bytes("rx: ", DUMP_PREFIX_OFFSET, in_packet,
1204                                      len);
1205
1206 #if defined(CONFIG_CMD_PCAP)
1207         pcap_post(in_packet, len, false);
1208 #endif
1209         net_rx_packet = in_packet;
1210         net_rx_packet_len = len;
1211         et = (struct ethernet_hdr *)in_packet;
1212
1213         /* too small packet? */
1214         if (len < ETHER_HDR_SIZE)
1215                 return;
1216
1217 #if defined(CONFIG_API) || defined(CONFIG_EFI_LOADER)
1218         if (push_packet) {
1219                 (*push_packet)(in_packet, len);
1220                 return;
1221         }
1222 #endif
1223
1224 #if defined(CONFIG_CMD_CDP)
1225         /* keep track if packet is CDP */
1226         iscdp = is_cdp_packet(et->et_dest);
1227 #endif
1228
1229         myvlanid = ntohs(net_our_vlan);
1230         if (myvlanid == (ushort)-1)
1231                 myvlanid = VLAN_NONE;
1232         mynvlanid = ntohs(net_native_vlan);
1233         if (mynvlanid == (ushort)-1)
1234                 mynvlanid = VLAN_NONE;
1235
1236         eth_proto = ntohs(et->et_protlen);
1237
1238         if (eth_proto < 1514) {
1239                 struct e802_hdr *et802 = (struct e802_hdr *)et;
1240                 /*
1241                  *      Got a 802.2 packet.  Check the other protocol field.
1242                  *      XXX VLAN over 802.2+SNAP not implemented!
1243                  */
1244                 eth_proto = ntohs(et802->et_prot);
1245
1246                 ip = (struct ip_udp_hdr *)(in_packet + E802_HDR_SIZE);
1247                 len -= E802_HDR_SIZE;
1248
1249         } else if (eth_proto != PROT_VLAN) {    /* normal packet */
1250                 ip = (struct ip_udp_hdr *)(in_packet + ETHER_HDR_SIZE);
1251                 len -= ETHER_HDR_SIZE;
1252
1253         } else {                        /* VLAN packet */
1254                 struct vlan_ethernet_hdr *vet =
1255                         (struct vlan_ethernet_hdr *)et;
1256
1257                 debug_cond(DEBUG_NET_PKT, "VLAN packet received\n");
1258
1259                 /* too small packet? */
1260                 if (len < VLAN_ETHER_HDR_SIZE)
1261                         return;
1262
1263                 /* if no VLAN active */
1264                 if ((ntohs(net_our_vlan) & VLAN_IDMASK) == VLAN_NONE
1265 #if defined(CONFIG_CMD_CDP)
1266                                 && iscdp == 0
1267 #endif
1268                                 )
1269                         return;
1270
1271                 cti = ntohs(vet->vet_tag);
1272                 vlanid = cti & VLAN_IDMASK;
1273                 eth_proto = ntohs(vet->vet_type);
1274
1275                 ip = (struct ip_udp_hdr *)(in_packet + VLAN_ETHER_HDR_SIZE);
1276                 len -= VLAN_ETHER_HDR_SIZE;
1277         }
1278
1279         debug_cond(DEBUG_NET_PKT, "Receive from protocol 0x%x\n", eth_proto);
1280
1281 #if defined(CONFIG_CMD_CDP)
1282         if (iscdp) {
1283                 cdp_receive((uchar *)ip, len);
1284                 return;
1285         }
1286 #endif
1287
1288         if ((myvlanid & VLAN_IDMASK) != VLAN_NONE) {
1289                 if (vlanid == VLAN_NONE)
1290                         vlanid = (mynvlanid & VLAN_IDMASK);
1291                 /* not matched? */
1292                 if (vlanid != (myvlanid & VLAN_IDMASK))
1293                         return;
1294         }
1295
1296         switch (eth_proto) {
1297         case PROT_ARP:
1298                 arp_receive(et, ip, len);
1299                 break;
1300
1301 #ifdef CONFIG_CMD_RARP
1302         case PROT_RARP:
1303                 rarp_receive(ip, len);
1304                 break;
1305 #endif
1306 #if IS_ENABLED(CONFIG_IPV6)
1307         case PROT_IP6:
1308                 net_ip6_handler(et, (struct ip6_hdr *)ip, len);
1309                 break;
1310 #endif
1311         case PROT_IP:
1312                 debug_cond(DEBUG_NET_PKT, "Got IP\n");
1313                 /* Before we start poking the header, make sure it is there */
1314                 if (len < IP_HDR_SIZE) {
1315                         debug("len bad %d < %lu\n", len,
1316                               (ulong)IP_HDR_SIZE);
1317                         return;
1318                 }
1319                 /* Check the packet length */
1320                 if (len < ntohs(ip->ip_len)) {
1321                         debug("len bad %d < %d\n", len, ntohs(ip->ip_len));
1322                         return;
1323                 }
1324                 len = ntohs(ip->ip_len);
1325                 if (len < IP_HDR_SIZE) {
1326                         debug("bad ip->ip_len %d < %d\n", len, (int)IP_HDR_SIZE);
1327                         return;
1328                 }
1329                 debug_cond(DEBUG_NET_PKT, "len=%d, v=%02x\n",
1330                            len, ip->ip_hl_v & 0xff);
1331
1332                 /* Can't deal with anything except IPv4 */
1333                 if ((ip->ip_hl_v & 0xf0) != 0x40)
1334                         return;
1335                 /* Can't deal with IP options (headers != 20 bytes) */
1336                 if ((ip->ip_hl_v & 0x0f) != 0x05)
1337                         return;
1338                 /* Check the Checksum of the header */
1339                 if (!ip_checksum_ok((uchar *)ip, IP_HDR_SIZE)) {
1340                         debug("checksum bad\n");
1341                         return;
1342                 }
1343                 /* If it is not for us, ignore it */
1344                 dst_ip = net_read_ip(&ip->ip_dst);
1345                 if (net_ip.s_addr && dst_ip.s_addr != net_ip.s_addr &&
1346                     dst_ip.s_addr != 0xFFFFFFFF) {
1347                                 return;
1348                 }
1349                 /* Read source IP address for later use */
1350                 src_ip = net_read_ip(&ip->ip_src);
1351                 /*
1352                  * The function returns the unchanged packet if it's not
1353                  * a fragment, and either the complete packet or NULL if
1354                  * it is a fragment (if !CONFIG_IP_DEFRAG, it returns NULL)
1355                  */
1356                 ip = net_defragment(ip, &len);
1357                 if (!ip)
1358                         return;
1359                 /*
1360                  * watch for ICMP host redirects
1361                  *
1362                  * There is no real handler code (yet). We just watch
1363                  * for ICMP host redirect messages. In case anybody
1364                  * sees these messages: please contact me
1365                  * ([email protected]), or - even better - send me the
1366                  * necessary fixes :-)
1367                  *
1368                  * Note: in all cases where I have seen this so far
1369                  * it was a problem with the router configuration,
1370                  * for instance when a router was configured in the
1371                  * BOOTP reply, but the TFTP server was on the same
1372                  * subnet. So this is probably a warning that your
1373                  * configuration might be wrong. But I'm not really
1374                  * sure if there aren't any other situations.
1375                  *
1376                  * Simon Glass <[email protected]>: We get an ICMP when
1377                  * we send a tftp packet to a dead connection, or when
1378                  * there is no server at the other end.
1379                  */
1380                 if (ip->ip_p == IPPROTO_ICMP) {
1381                         receive_icmp(ip, len, src_ip, et);
1382                         return;
1383 #if defined(CONFIG_PROT_TCP)
1384                 } else if (ip->ip_p == IPPROTO_TCP) {
1385                         debug_cond(DEBUG_DEV_PKT,
1386                                    "TCP PH (to=%pI4, from=%pI4, len=%d)\n",
1387                                    &dst_ip, &src_ip, len);
1388
1389                         rxhand_tcp_f((union tcp_build_pkt *)ip, len);
1390                         return;
1391 #endif
1392                 } else if (ip->ip_p != IPPROTO_UDP) {   /* Only UDP packets */
1393                         return;
1394                 }
1395
1396                 if (ntohs(ip->udp_len) < UDP_HDR_SIZE || ntohs(ip->udp_len) > len - IP_HDR_SIZE)
1397                         return;
1398
1399                 debug_cond(DEBUG_DEV_PKT,
1400                            "received UDP (to=%pI4, from=%pI4, len=%d)\n",
1401                            &dst_ip, &src_ip, len);
1402
1403                 if (IS_ENABLED(CONFIG_UDP_CHECKSUM) && ip->udp_xsum != 0) {
1404                         ulong   xsum;
1405                         u8 *sumptr;
1406                         ushort  sumlen;
1407
1408                         xsum  = ip->ip_p;
1409                         xsum += (ntohs(ip->udp_len));
1410                         xsum += (ntohl(ip->ip_src.s_addr) >> 16) & 0x0000ffff;
1411                         xsum += (ntohl(ip->ip_src.s_addr) >>  0) & 0x0000ffff;
1412                         xsum += (ntohl(ip->ip_dst.s_addr) >> 16) & 0x0000ffff;
1413                         xsum += (ntohl(ip->ip_dst.s_addr) >>  0) & 0x0000ffff;
1414
1415                         sumlen = ntohs(ip->udp_len);
1416                         sumptr = (u8 *)&ip->udp_src;
1417
1418                         while (sumlen > 1) {
1419                                 /* inlined ntohs() to avoid alignment errors */
1420                                 xsum += (sumptr[0] << 8) + sumptr[1];
1421                                 sumptr += 2;
1422                                 sumlen -= 2;
1423                         }
1424                         if (sumlen > 0)
1425                                 xsum += (sumptr[0] << 8) + sumptr[0];
1426                         while ((xsum >> 16) != 0) {
1427                                 xsum = (xsum & 0x0000ffff) +
1428                                        ((xsum >> 16) & 0x0000ffff);
1429                         }
1430                         if ((xsum != 0x00000000) && (xsum != 0x0000ffff)) {
1431                                 printf(" UDP wrong checksum %08lx %08x\n",
1432                                        xsum, ntohs(ip->udp_xsum));
1433                                 return;
1434                         }
1435                 }
1436
1437 #if defined(CONFIG_NETCONSOLE) && !defined(CONFIG_SPL_BUILD)
1438                 nc_input_packet((uchar *)ip + IP_UDP_HDR_SIZE,
1439                                 src_ip,
1440                                 ntohs(ip->udp_dst),
1441                                 ntohs(ip->udp_src),
1442                                 ntohs(ip->udp_len) - UDP_HDR_SIZE);
1443 #endif
1444                 /*
1445                  * IP header OK.  Pass the packet to the current handler.
1446                  */
1447                 (*udp_packet_handler)((uchar *)ip + IP_UDP_HDR_SIZE,
1448                                       ntohs(ip->udp_dst),
1449                                       src_ip,
1450                                       ntohs(ip->udp_src),
1451                                       ntohs(ip->udp_len) - UDP_HDR_SIZE);
1452                 break;
1453 #ifdef CONFIG_CMD_WOL
1454         case PROT_WOL:
1455                 wol_receive(ip, len);
1456                 break;
1457 #endif
1458 #ifdef CONFIG_PHY_NCSI
1459         case PROT_NCSI:
1460                 ncsi_receive(et, ip, len);
1461                 break;
1462 #endif
1463         }
1464 }
1465
1466 /**********************************************************************/
1467
1468 static int net_check_prereq(enum proto_t protocol)
1469 {
1470         switch (protocol) {
1471                 /* Fall through */
1472 #if defined(CONFIG_CMD_PING)
1473         case PING:
1474                 if (net_ping_ip.s_addr == 0) {
1475                         puts("*** ERROR: ping address not given\n");
1476                         return 1;
1477                 }
1478                 goto common;
1479 #endif
1480 #if defined(CONFIG_CMD_PING6)
1481         case PING6:
1482                 if (ip6_is_unspecified_addr(&net_ping_ip6)) {
1483                         puts("*** ERROR: ping address not given\n");
1484                         return 1;
1485                 }
1486                 goto common;
1487 #endif
1488 #if defined(CONFIG_CMD_DNS)
1489         case DNS:
1490                 if (net_dns_server.s_addr == 0) {
1491                         puts("*** ERROR: DNS server address not given\n");
1492                         return 1;
1493                 }
1494                 goto common;
1495 #endif
1496 #if defined(CONFIG_PROT_UDP)
1497         case UDP:
1498                 if (udp_prereq())
1499                         return 1;
1500                 goto common;
1501 #endif
1502
1503 #if defined(CONFIG_CMD_NFS)
1504         case NFS:
1505 #endif
1506                 /* Fall through */
1507         case TFTPGET:
1508         case TFTPPUT:
1509                 if (IS_ENABLED(CONFIG_IPV6) && use_ip6) {
1510                         if (!memcmp(&net_server_ip6, &net_null_addr_ip6,
1511                                     sizeof(struct in6_addr)) &&
1512                                     !strchr(net_boot_file_name, '[')) {
1513                                 puts("*** ERROR: `serverip6' not set\n");
1514                                 return 1;
1515                         }
1516                 } else if (net_server_ip.s_addr == 0 && !is_serverip_in_cmd()) {
1517                         puts("*** ERROR: `serverip' not set\n");
1518                         return 1;
1519                 }
1520 #if     defined(CONFIG_CMD_PING) || \
1521         defined(CONFIG_CMD_DNS) || defined(CONFIG_PROT_UDP)
1522 common:
1523 #endif
1524                 /* Fall through */
1525
1526         case NETCONS:
1527         case FASTBOOT_UDP:
1528         case FASTBOOT_TCP:
1529         case TFTPSRV:
1530                 if (IS_ENABLED(CONFIG_IPV6) && use_ip6) {
1531                         if (!memcmp(&net_link_local_ip6, &net_null_addr_ip6,
1532                                     sizeof(struct in6_addr))) {
1533                                 puts("*** ERROR: `ip6addr` not set\n");
1534                                 return 1;
1535                         }
1536                 } else if (net_ip.s_addr == 0) {
1537                         puts("*** ERROR: `ipaddr' not set\n");
1538                         return 1;
1539                 }
1540                 /* Fall through */
1541
1542 #ifdef CONFIG_CMD_RARP
1543         case RARP:
1544 #endif
1545 #ifdef CONFIG_PHY_NCSI
1546         case NCSI:
1547 #endif
1548         case BOOTP:
1549         case CDP:
1550         case DHCP:
1551         case LINKLOCAL:
1552                 if (memcmp(net_ethaddr, "\0\0\0\0\0\0", 6) == 0) {
1553                         int num = eth_get_dev_index();
1554
1555                         switch (num) {
1556                         case -1:
1557                                 puts("*** ERROR: No ethernet found.\n");
1558                                 return 1;
1559                         case 0:
1560                                 puts("*** ERROR: `ethaddr' not set\n");
1561                                 break;
1562                         default:
1563                                 printf("*** ERROR: `eth%daddr' not set\n",
1564                                        num);
1565                                 break;
1566                         }
1567
1568                         net_start_again();
1569                         return 2;
1570                 }
1571                 /* Fall through */
1572         default:
1573                 return 0;
1574         }
1575         return 0;               /* OK */
1576 }
1577 /**********************************************************************/
1578
1579 int
1580 net_eth_hdr_size(void)
1581 {
1582         ushort myvlanid;
1583
1584         myvlanid = ntohs(net_our_vlan);
1585         if (myvlanid == (ushort)-1)
1586                 myvlanid = VLAN_NONE;
1587
1588         return ((myvlanid & VLAN_IDMASK) == VLAN_NONE) ? ETHER_HDR_SIZE :
1589                 VLAN_ETHER_HDR_SIZE;
1590 }
1591
1592 int net_set_ether(uchar *xet, const uchar *dest_ethaddr, uint prot)
1593 {
1594         struct ethernet_hdr *et = (struct ethernet_hdr *)xet;
1595         ushort myvlanid;
1596
1597         myvlanid = ntohs(net_our_vlan);
1598         if (myvlanid == (ushort)-1)
1599                 myvlanid = VLAN_NONE;
1600
1601         memcpy(et->et_dest, dest_ethaddr, 6);
1602         memcpy(et->et_src, net_ethaddr, 6);
1603         if ((myvlanid & VLAN_IDMASK) == VLAN_NONE) {
1604                 et->et_protlen = htons(prot);
1605                 return ETHER_HDR_SIZE;
1606         } else {
1607                 struct vlan_ethernet_hdr *vet =
1608                         (struct vlan_ethernet_hdr *)xet;
1609
1610                 vet->vet_vlan_type = htons(PROT_VLAN);
1611                 vet->vet_tag = htons((0 << 5) | (myvlanid & VLAN_IDMASK));
1612                 vet->vet_type = htons(prot);
1613                 return VLAN_ETHER_HDR_SIZE;
1614         }
1615 }
1616
1617 int net_update_ether(struct ethernet_hdr *et, uchar *addr, uint prot)
1618 {
1619         ushort protlen;
1620
1621         memcpy(et->et_dest, addr, 6);
1622         memcpy(et->et_src, net_ethaddr, 6);
1623         protlen = ntohs(et->et_protlen);
1624         if (protlen == PROT_VLAN) {
1625                 struct vlan_ethernet_hdr *vet =
1626                         (struct vlan_ethernet_hdr *)et;
1627                 vet->vet_type = htons(prot);
1628                 return VLAN_ETHER_HDR_SIZE;
1629         } else if (protlen > 1514) {
1630                 et->et_protlen = htons(prot);
1631                 return ETHER_HDR_SIZE;
1632         } else {
1633                 /* 802.2 + SNAP */
1634                 struct e802_hdr *et802 = (struct e802_hdr *)et;
1635                 et802->et_prot = htons(prot);
1636                 return E802_HDR_SIZE;
1637         }
1638 }
1639
1640 void net_set_ip_header(uchar *pkt, struct in_addr dest, struct in_addr source,
1641                        u16 pkt_len, u8 proto)
1642 {
1643         struct ip_udp_hdr *ip = (struct ip_udp_hdr *)pkt;
1644
1645         /*
1646          *      Construct an IP header.
1647          */
1648         /* IP_HDR_SIZE / 4 (not including UDP) */
1649         ip->ip_hl_v  = 0x45;
1650         ip->ip_tos   = 0;
1651         ip->ip_len   = htons(pkt_len);
1652         ip->ip_p     = proto;
1653         ip->ip_id    = htons(net_ip_id++);
1654         ip->ip_off   = htons(IP_FLAGS_DFRAG);   /* Don't fragment */
1655         ip->ip_ttl   = 255;
1656         ip->ip_sum   = 0;
1657         /* already in network byte order */
1658         net_copy_ip((void *)&ip->ip_src, &source);
1659         /* already in network byte order */
1660         net_copy_ip((void *)&ip->ip_dst, &dest);
1661
1662         ip->ip_sum   = compute_ip_checksum(ip, IP_HDR_SIZE);
1663 }
1664
1665 void net_set_udp_header(uchar *pkt, struct in_addr dest, int dport, int sport,
1666                         int len)
1667 {
1668         struct ip_udp_hdr *ip = (struct ip_udp_hdr *)pkt;
1669
1670         /*
1671          *      If the data is an odd number of bytes, zero the
1672          *      byte after the last byte so that the checksum
1673          *      will work.
1674          */
1675         if (len & 1)
1676                 pkt[IP_UDP_HDR_SIZE + len] = 0;
1677
1678         net_set_ip_header(pkt, dest, net_ip, IP_UDP_HDR_SIZE + len,
1679                           IPPROTO_UDP);
1680
1681         ip->udp_src  = htons(sport);
1682         ip->udp_dst  = htons(dport);
1683         ip->udp_len  = htons(UDP_HDR_SIZE + len);
1684         ip->udp_xsum = 0;
1685 }
1686
1687 void copy_filename(char *dst, const char *src, int size)
1688 {
1689         if (src && *src && (*src == '"')) {
1690                 ++src;
1691                 --size;
1692         }
1693
1694         while ((--size > 0) && src && *src && (*src != '"'))
1695                 *dst++ = *src++;
1696         *dst = '\0';
1697 }
1698
1699 int is_serverip_in_cmd(void)
1700 {
1701         return !!strchr(net_boot_file_name, ':');
1702 }
1703
1704 int net_parse_bootfile(struct in_addr *ipaddr, char *filename, int max_len)
1705 {
1706         char *colon;
1707         struct in_addr ip;
1708         ip.s_addr = 0;
1709
1710         if (net_boot_file_name[0] == '\0')
1711                 return 0;
1712
1713         colon = strchr(net_boot_file_name, ':');
1714         if (colon) {
1715                 ip = string_to_ip(net_boot_file_name);
1716                 if (ipaddr && ip.s_addr)
1717                         *ipaddr = ip;
1718         }
1719         if (ip.s_addr) {
1720                 strncpy(filename, colon + 1, max_len);
1721         } else {
1722                 strncpy(filename, net_boot_file_name, max_len);
1723         }
1724         filename[max_len - 1] = '\0';
1725
1726         return 1;
1727 }
1728
1729 void ip_to_string(struct in_addr x, char *s)
1730 {
1731         x.s_addr = ntohl(x.s_addr);
1732         sprintf(s, "%d.%d.%d.%d",
1733                 (int) ((x.s_addr >> 24) & 0xff),
1734                 (int) ((x.s_addr >> 16) & 0xff),
1735                 (int) ((x.s_addr >> 8) & 0xff),
1736                 (int) ((x.s_addr >> 0) & 0xff)
1737         );
1738 }
1739
1740 void vlan_to_string(ushort x, char *s)
1741 {
1742         x = ntohs(x);
1743
1744         if (x == (ushort)-1)
1745                 x = VLAN_NONE;
1746
1747         if (x == VLAN_NONE)
1748                 strcpy(s, "none");
1749         else
1750                 sprintf(s, "%d", x & VLAN_IDMASK);
1751 }
1752
1753 ushort string_to_vlan(const char *s)
1754 {
1755         ushort id;
1756
1757         if (s == NULL)
1758                 return htons(VLAN_NONE);
1759
1760         if (*s < '0' || *s > '9')
1761                 id = VLAN_NONE;
1762         else
1763                 id = (ushort)dectoul(s, NULL);
1764
1765         return htons(id);
1766 }
1767
1768 ushort env_get_vlan(char *var)
1769 {
1770         return string_to_vlan(env_get(var));
1771 }
This page took 0.130354 seconds and 4 git commands to generate.