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