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