blob: 253340f3c44ddf80cacd94b2ab9ff91dc17b8fb3 [file] [log] [blame]
Tom Rini70df9d62018-05-07 17:02:21 -04001// SPDX-License-Identifier: GPL-2.0
wdenk2d966952002-10-31 22:12:35 +00002/*
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, wd@denx.de
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 *
Joe Hershbergerb35a3a62012-05-23 08:00:12 +000027 * LINK_LOCAL:
28 *
29 * Prerequisites: - own ethernet address
30 * We want: - own IP address
31 * Next step: ARP
32 *
wdenk2d966952002-10-31 22:12:35 +000033 * 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 *
Wolfgang Denk30b87322005-08-12 23:43:12 +020050 * Prerequisites: - own ethernet address
51 * We want: - IP, Netmask, ServerIP, Gateway IP
52 * - bootfilename, lease time
53 * Next step: - TFTP
wdenk2d966952002-10-31 22:12:35 +000054 *
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
wdenkbe9c1cb2004-02-24 02:00:03 +000065 *
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
wdenkb4ad9622005-04-01 00:25:43 +000074 *
Lothar Felten776fc102018-06-22 22:29:54 +020075 *
76 * WOL:
77 *
78 * Prerequisites: - own ethernet address
79 * We want: - magic packet or timeout
80 * Next step: none
wdenk2d966952002-10-31 22:12:35 +000081 */
82
83
84#include <common.h>
Simon Glass1ea97892020-05-10 11:40:00 -060085#include <bootstage.h>
wdenk2d966952002-10-31 22:12:35 +000086#include <command.h>
Simon Glassa73bda42015-11-08 23:47:45 -070087#include <console.h>
Simon Glass313112a2019-08-01 09:46:46 -060088#include <env.h>
Simon Glass9d1f6192019-08-02 09:44:25 -060089#include <env_internal.h>
Joe Hershbergerf4dc96a2015-03-22 17:09:24 -050090#include <errno.h>
Simon Glass85f13782019-12-28 10:45:03 -070091#include <image.h>
Simon Glass0f2af882020-05-10 11:40:05 -060092#include <log.h>
wdenk2d966952002-10-31 22:12:35 +000093#include <net.h>
Viacheslav Mitrofanovda019102022-12-02 12:18:06 +030094#include <net6.h>
95#include <ndisc.h>
Dmitrii Merkurev308252d2023-04-12 19:49:30 +010096#include <net/fastboot_udp.h>
97#include <net/fastboot_tcp.h>
Lukasz Majewski21185922015-08-24 00:21:43 +020098#include <net/tftp.h>
Samuel Mendoza-Jonasc8f4ab02022-08-08 21:46:03 +093099#include <net/ncsi.h>
Ramon Friedac598c12019-07-18 21:43:30 +0300100#if defined(CONFIG_CMD_PCAP)
101#include <net/pcap.h>
102#endif
Philippe Reynes6ec70bc2020-09-18 14:13:00 +0200103#include <net/udp.h>
Uri Mashiach4892d392017-01-19 10:51:45 +0200104#if defined(CONFIG_LED_STATUS)
wdenk49c3f672003-10-08 22:33:00 +0000105#include <miiphy.h>
Joe Hershbergerfee076e2012-05-23 07:58:15 +0000106#include <status_led.h>
wdenkb4ad9622005-04-01 00:25:43 +0000107#endif
Joe Hershbergerfee076e2012-05-23 07:58:15 +0000108#include <watchdog.h>
109#include <linux/compiler.h>
Simon Glass66014cc2023-01-17 10:47:27 -0700110#include <test/test.h>
Sean Edmonde8c43832023-04-11 10:48:46 -0700111#include <net/tcp.h>
112#include <net/wget.h>
Joe Hershbergerfee076e2012-05-23 07:58:15 +0000113#include "arp.h"
114#include "bootp.h"
Joe Hershbergera4215b02012-05-23 07:57:59 +0000115#include "cdp.h"
Robin Getz82f0d232009-07-20 14:53:54 -0400116#if defined(CONFIG_CMD_DNS)
117#include "dns.h"
118#endif
Joe Hershbergerb35a3a62012-05-23 08:00:12 +0000119#include "link_local.h"
Joe Hershbergerfee076e2012-05-23 07:58:15 +0000120#include "nfs.h"
Joe Hershbergerc21bf372012-05-23 07:58:02 +0000121#include "ping.h"
Joe Hershbergerfee076e2012-05-23 07:58:15 +0000122#include "rarp.h"
Lothar Felten776fc102018-06-22 22:29:54 +0200123#if defined(CONFIG_CMD_WOL)
124#include "wol.h"
125#endif
Sean Edmonde8c43832023-04-11 10:48:46 -0700126#include "dhcpv6.h"
wdenk2d966952002-10-31 22:12:35 +0000127
wdenk2d966952002-10-31 22:12:35 +0000128/** BOOTP EXTENTIONS **/
129
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000130/* Our subnet mask (0=unknown) */
Joe Hershberger5874dec2015-04-08 01:41:01 -0500131struct in_addr net_netmask;
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000132/* Our gateways IP address */
Joe Hershberger5874dec2015-04-08 01:41:01 -0500133struct in_addr net_gateway;
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000134/* Our DNS IP address */
Joe Hershberger5874dec2015-04-08 01:41:01 -0500135struct in_addr net_dns_server;
Jon Loeliger5336a762007-07-09 22:08:34 -0500136#if defined(CONFIG_BOOTP_DNS2)
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000137/* Our 2nd DNS IP address */
Joe Hershberger5874dec2015-04-08 01:41:01 -0500138struct in_addr net_dns_server2;
stroesee0aadfb2003-08-28 14:17:32 +0000139#endif
Sean Edmonde8c43832023-04-11 10:48:46 -0700140/* Indicates whether the pxe path prefix / config file was specified in dhcp option */
141char *pxelinux_configfile;
wdenk2d966952002-10-31 22:12:35 +0000142
143/** END OF BOOTP EXTENTIONS **/
144
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000145/* Our ethernet address */
Joe Hershberger8ecdbed2015-04-08 01:41:04 -0500146u8 net_ethaddr[6];
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000147/* Boot server enet address */
Joe Hershberger8ecdbed2015-04-08 01:41:04 -0500148u8 net_server_ethaddr[6];
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000149/* Our IP addr (0 = unknown) */
Joe Hershberger5874dec2015-04-08 01:41:01 -0500150struct in_addr net_ip;
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000151/* Server IP addr (0 = unknown) */
Joe Hershberger5874dec2015-04-08 01:41:01 -0500152struct in_addr net_server_ip;
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000153/* Current receive packet */
Joe Hershbergera8ca4f62015-04-08 01:41:05 -0500154uchar *net_rx_packet;
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000155/* Current rx packet length */
Joe Hershbergera8ca4f62015-04-08 01:41:05 -0500156int net_rx_packet_len;
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000157/* IP packet ID */
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500158static unsigned net_ip_id;
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000159/* Ethernet bcast address */
Joe Hershberger8ecdbed2015-04-08 01:41:04 -0500160const u8 net_bcast_ethaddr[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
161const u8 net_null_ethaddr[6];
Alexander Graf94c4b992016-05-06 21:01:01 +0200162#if defined(CONFIG_API) || defined(CONFIG_EFI_LOADER)
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500163void (*push_packet)(void *, int len) = 0;
Rafal Jaworowski1f2c9a42007-12-27 18:19:02 +0100164#endif
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000165/* Network loop state */
Joe Hershbergerd4bb76a2012-05-23 07:59:14 +0000166enum net_loop_state net_state;
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000167/* Tried all network devices */
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500168int net_restart_wrap;
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000169/* Network loop restarted */
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500170static int net_restarted;
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000171/* At least one device configured */
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500172static int net_dev_exists;
wdenk2d966952002-10-31 22:12:35 +0000173
wdenk05939202004-04-18 17:39:38 +0000174/* XXX in both little & big endian machines 0xFFFF == ntohs(-1) */
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000175/* default is without VLAN */
Joe Hershberger013d3872015-04-08 01:41:17 -0500176ushort net_our_vlan = 0xFFFF;
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000177/* ditto */
Joe Hershberger013d3872015-04-08 01:41:17 -0500178ushort net_native_vlan = 0xFFFF;
wdenk145d2c12004-04-15 21:48:45 +0000179
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000180/* Boot File name */
Jacob Stifflerae80c2e2015-09-30 10:12:05 -0400181char net_boot_file_name[1024];
Alexander Graff43bf5d2018-06-15 10:29:27 +0200182/* Indicates whether the file name was specified on the command line */
183bool net_boot_file_name_explicit;
Joe Hershberger290c8992015-04-08 01:41:02 -0500184/* The actual transferred size of the bootfile (in bytes) */
185u32 net_boot_file_size;
186/* Boot file size in blocks as reported by the DHCP server */
187u32 net_boot_file_expected_size_in_blocks;
wdenk2d966952002-10-31 22:12:35 +0000188
Joe Hershbergera8ca4f62015-04-08 01:41:05 -0500189static uchar net_pkt_buf[(PKTBUFSRX+1) * PKTSIZE_ALIGN + PKTALIGN];
Joe Hershbergerf77abc52015-03-22 17:09:11 -0500190/* Receive packets */
191uchar *net_rx_packets[PKTBUFSRX];
Joe Hershbergerf50357b2012-05-23 07:59:15 +0000192/* Current UDP RX packet handler */
193static rxhand_f *udp_packet_handler;
194/* Current ARP RX packet handler */
195static rxhand_f *arp_packet_handler;
Simon Glass2928cd82011-10-26 14:18:38 +0000196#ifdef CONFIG_CMD_TFTPPUT
Joe Hershbergerf50357b2012-05-23 07:59:15 +0000197/* Current ICMP rx handler */
198static rxhand_icmp_f *packet_icmp_handler;
Simon Glass2928cd82011-10-26 14:18:38 +0000199#endif
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000200/* Current timeout handler */
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500201static thand_f *time_handler;
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000202/* Time base value */
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500203static ulong time_start;
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000204/* Current timeout value */
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500205static ulong time_delta;
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000206/* THE transmit packet */
Joe Hershbergera8ca4f62015-04-08 01:41:05 -0500207uchar *net_tx_packet;
wdenk2d966952002-10-31 22:12:35 +0000208
Simon Glassd6c5f552011-10-24 18:00:02 +0000209static int net_check_prereq(enum proto_t protocol);
wdenk2d966952002-10-31 22:12:35 +0000210
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500211static int net_try_count;
Remy Bohmer0ddb8e82009-10-28 22:13:39 +0100212
Jim Lin6c8921f2013-08-13 19:03:05 +0800213int __maybe_unused net_busy_flag;
214
wdenk2d966952002-10-31 22:12:35 +0000215/**********************************************************************/
wdenke6466f62003-06-05 19:27:42 +0000216
Joe Hershberger875b6bf2015-05-20 14:27:23 -0500217static 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}
227U_BOOT_ENV_CALLBACK(ipaddr, on_ipaddr);
228
229static 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}
239U_BOOT_ENV_CALLBACK(gatewayip, on_gatewayip);
240
241static 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}
251U_BOOT_ENV_CALLBACK(netmask, on_netmask);
252
253static 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}
263U_BOOT_ENV_CALLBACK(serverip, on_serverip);
264
265static 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}
275U_BOOT_ENV_CALLBACK(nvlan, on_nvlan);
276
277static 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}
287U_BOOT_ENV_CALLBACK(vlan, on_vlan);
288
289#if defined(CONFIG_CMD_DNS)
290static 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}
300U_BOOT_ENV_CALLBACK(dnsip, on_dnsip);
301#endif
302
Simon Glass5234ad12011-10-27 06:24:32 +0000303/*
304 * Check if autoload is enabled. If so, use either NFS or TFTP to download
305 * the boot file.
306 */
307void net_auto_load(void)
308{
Tom Rini57256302019-12-05 19:35:07 -0500309#if defined(CONFIG_CMD_NFS) && !defined(CONFIG_SPL_BUILD)
Simon Glass64b723f2017-08-03 12:22:12 -0600310 const char *s = env_get("autoload");
Simon Glass5234ad12011-10-27 06:24:32 +0000311
Joe Hershberger864ec562012-12-11 22:16:22 -0600312 if (s != NULL && strcmp(s, "NFS") == 0) {
Joe Hershberger40aa2082018-07-03 19:36:40 -0500313 if (net_check_prereq(NFS)) {
314/* We aren't expecting to get a serverip, so just accept the assigned IP */
Simon Glass297fc202021-12-18 11:27:52 -0700315 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 }
Joe Hershberger40aa2082018-07-03 19:36:40 -0500321 return;
322 }
Joe Hershberger864ec562012-12-11 22:16:22 -0600323 /*
324 * Use NFS to load the bootfile.
325 */
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500326 nfs_start();
Joe Hershberger864ec562012-12-11 22:16:22 -0600327 return;
328 }
Simon Glass5234ad12011-10-27 06:24:32 +0000329#endif
Simon Glass22c34c22017-08-03 12:22:13 -0600330 if (env_get_yesno("autoload") == 0) {
Joe Hershberger864ec562012-12-11 22:16:22 -0600331 /*
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;
Simon Glass5234ad12011-10-27 06:24:32 +0000337 }
Joe Hershberger40aa2082018-07-03 19:36:40 -0500338 if (net_check_prereq(TFTPGET)) {
339/* We aren't expecting to get a serverip, so just accept the assigned IP */
Simon Glass297fc202021-12-18 11:27:52 -0700340 if (IS_ENABLED(CONFIG_BOOTP_SERVERIP)) {
341 net_set_state(NETLOOP_SUCCESS);
342 } else {
343 printf("Cannot autoload with TFTPGET\n");
344 net_set_state(NETLOOP_FAIL);
345 }
Joe Hershberger40aa2082018-07-03 19:36:40 -0500346 return;
347 }
Joe Hershberger64701592015-04-08 01:41:07 -0500348 tftp_start(TFTPGET);
Simon Glass5234ad12011-10-27 06:24:32 +0000349}
350
Sean Anderson35e82982020-09-12 17:45:43 -0400351static int net_init_loop(void)
Heiko Schocher0c303fa2009-02-10 09:38:52 +0100352{
Viacheslav Mitrofanovda019102022-12-02 12:18:06 +0300353 if (eth_get_dev()) {
Joe Hershberger8ecdbed2015-04-08 01:41:04 -0500354 memcpy(net_ethaddr, eth_get_ethaddr(), 6);
Viacheslav Mitrofanovda019102022-12-02 12:18:06 +0300355
356 if (IS_ENABLED(CONFIG_IPV6)) {
357 ip6_make_lladdr(&net_link_local_ip6, net_ethaddr);
358 if (!memcmp(&net_ip6, &net_null_addr_ip6,
359 sizeof(struct in6_addr)))
360 memcpy(&net_ip6, &net_link_local_ip6,
361 sizeof(struct in6_addr));
362 }
363 }
Sean Anderson35e82982020-09-12 17:45:43 -0400364 else
365 /*
366 * Not ideal, but there's no way to get the actual error, and I
367 * don't feel like fixing all the users of eth_get_dev to deal
368 * with errors.
369 */
370 return -ENONET;
Michael Zaidmanb97bfe42009-04-04 01:43:00 +0300371
Sean Anderson35e82982020-09-12 17:45:43 -0400372 return 0;
Heiko Schocher0c303fa2009-02-10 09:38:52 +0100373}
374
Joe Hershbergerf50357b2012-05-23 07:59:15 +0000375static void net_clear_handlers(void)
376{
377 net_set_udp_handler(NULL);
378 net_set_arp_handler(NULL);
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500379 net_set_timeout_handler(0, NULL);
Joe Hershbergerf50357b2012-05-23 07:59:15 +0000380}
381
382static void net_cleanup_loop(void)
383{
384 net_clear_handlers();
385}
386
Sean Anderson35e82982020-09-12 17:45:43 -0400387int net_init(void)
Joe Hershberger017e5c42012-05-23 07:59:22 +0000388{
389 static int first_call = 1;
390
391 if (first_call) {
392 /*
393 * Setup packet buffers, aligned correctly.
394 */
395 int i;
396
Joe Hershbergera8ca4f62015-04-08 01:41:05 -0500397 net_tx_packet = &net_pkt_buf[0] + (PKTALIGN - 1);
398 net_tx_packet -= (ulong)net_tx_packet % PKTALIGN;
Joe Hershbergerf77abc52015-03-22 17:09:11 -0500399 for (i = 0; i < PKTBUFSRX; i++) {
Joe Hershbergera8ca4f62015-04-08 01:41:05 -0500400 net_rx_packets[i] = net_tx_packet +
401 (i + 1) * PKTSIZE_ALIGN;
Joe Hershbergerf77abc52015-03-22 17:09:11 -0500402 }
Joe Hershberger85ae7762015-04-08 01:41:08 -0500403 arp_init();
Viacheslav Mitrofanovda019102022-12-02 12:18:06 +0300404 ndisc_init();
Joe Hershberger017e5c42012-05-23 07:59:22 +0000405 net_clear_handlers();
406
407 /* Only need to setup buffer pointers once. */
408 first_call = 0;
Ying-Chun Liu (PaulLiu)41efed12022-11-08 14:17:28 +0800409 if (IS_ENABLED(CONFIG_PROT_TCP))
410 tcp_set_tcp_state(TCP_CLOSED);
Joe Hershberger017e5c42012-05-23 07:59:22 +0000411 }
412
Sean Anderson35e82982020-09-12 17:45:43 -0400413 return net_init_loop();
Joe Hershberger017e5c42012-05-23 07:59:22 +0000414}
415
wdenke6466f62003-06-05 19:27:42 +0000416/**********************************************************************/
wdenk2d966952002-10-31 22:12:35 +0000417/*
418 * Main network processing loop.
419 */
420
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500421int net_loop(enum proto_t protocol)
wdenk2d966952002-10-31 22:12:35 +0000422{
Joe Hershbergerf4dc96a2015-03-22 17:09:24 -0500423 int ret = -EINVAL;
Leonid Iziumtsev4b58b052018-05-08 15:55:50 +0200424 enum net_loop_state prev_net_state = net_state;
wdenk2d966952002-10-31 22:12:35 +0000425
Marek Szyprowskib9df7d92020-06-15 11:15:57 +0200426#if defined(CONFIG_CMD_PING)
427 if (protocol != PING)
428 net_ping_ip.s_addr = 0;
429#endif
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500430 net_restarted = 0;
431 net_dev_exists = 0;
432 net_try_count = 1;
433 debug_cond(DEBUG_INT_STATE, "--- net_loop Entry\n");
wdenke6466f62003-06-05 19:27:42 +0000434
Samuel Mendoza-Jonasc8f4ab02022-08-08 21:46:03 +0930435#ifdef CONFIG_PHY_NCSI
436 if (phy_interface_is_ncsi() && protocol != NCSI && !ncsi_active()) {
437 printf("%s: configuring NCSI first\n", __func__);
438 if (net_loop(NCSI) < 0)
439 return ret;
440 eth_init_state_only();
441 goto restart;
442 }
443#endif
444
Simon Glass768cbf02011-12-10 11:08:06 +0000445 bootstage_mark_name(BOOTSTAGE_ID_ETH_START, "eth_start");
Joe Hershberger017e5c42012-05-23 07:59:22 +0000446 net_init();
Yang Liu7c78e2f2020-12-21 14:44:39 +1100447 if (eth_is_on_demand_init()) {
wdenkfa66e932005-04-03 14:52:59 +0000448 eth_halt();
Joe Hershberger9f374062012-08-03 10:59:08 +0000449 eth_set_current();
Joe Hershbergerf4dc96a2015-03-22 17:09:24 -0500450 ret = eth_init();
451 if (ret < 0) {
Joe Hershberger9f374062012-08-03 10:59:08 +0000452 eth_halt();
Joe Hershbergerf4dc96a2015-03-22 17:09:24 -0500453 return ret;
Joe Hershberger9f374062012-08-03 10:59:08 +0000454 }
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500455 } else {
Joe Hershberger3dbe17e2015-03-22 17:09:06 -0500456 eth_init_state_only();
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500457 }
Samuel Mendoza-Jonasfeebd6c2022-08-08 21:46:04 +0930458
wdenk2d966952002-10-31 22:12:35 +0000459restart:
Jim Lin6c8921f2013-08-13 19:03:05 +0800460#ifdef CONFIG_USB_KEYBOARD
461 net_busy_flag = 0;
462#endif
Joe Hershbergerd4bb76a2012-05-23 07:59:14 +0000463 net_set_state(NETLOOP_CONTINUE);
wdenk2d966952002-10-31 22:12:35 +0000464
465 /*
466 * Start the ball rolling with the given start function. From
467 * here on, this code is a state machine driven by received
468 * packets and timer events.
469 */
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500470 debug_cond(DEBUG_INT_STATE, "--- net_loop Init\n");
471 net_init_loop();
wdenk2d966952002-10-31 22:12:35 +0000472
Simon Glass66014cc2023-01-17 10:47:27 -0700473 if (!test_eth_enabled())
474 return 0;
475
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000476 switch (net_check_prereq(protocol)) {
wdenk2d966952002-10-31 22:12:35 +0000477 case 1:
478 /* network not configured */
wdenkfa66e932005-04-03 14:52:59 +0000479 eth_halt();
Leonid Iziumtsev4b58b052018-05-08 15:55:50 +0200480 net_set_state(prev_net_state);
Joe Hershbergerf4dc96a2015-03-22 17:09:24 -0500481 return -ENODEV;
wdenk2d966952002-10-31 22:12:35 +0000482
wdenk2d966952002-10-31 22:12:35 +0000483 case 2:
484 /* network device not configured */
485 break;
wdenk2d966952002-10-31 22:12:35 +0000486
487 case 0:
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500488 net_dev_exists = 1;
Joe Hershberger290c8992015-04-08 01:41:02 -0500489 net_boot_file_size = 0;
wdenk2d966952002-10-31 22:12:35 +0000490 switch (protocol) {
Krebs, Olafd9249382020-03-09 14:27:55 +0000491#ifdef CONFIG_CMD_TFTPBOOT
Simon Glassd6c5f552011-10-24 18:00:02 +0000492 case TFTPGET:
Simon Glass6a398d22011-10-24 18:00:07 +0000493#ifdef CONFIG_CMD_TFTPPUT
494 case TFTPPUT:
495#endif
wdenk2d966952002-10-31 22:12:35 +0000496 /* always use ARP to get server ethernet address */
Joe Hershberger64701592015-04-08 01:41:07 -0500497 tftp_start(protocol);
wdenk2d966952002-10-31 22:12:35 +0000498 break;
Krebs, Olafd9249382020-03-09 14:27:55 +0000499#endif
Luca Ceresoli7aa81a42011-05-17 00:03:40 +0000500#ifdef CONFIG_CMD_TFTPSRV
501 case TFTPSRV:
Joe Hershberger64701592015-04-08 01:41:07 -0500502 tftp_start_server();
Luca Ceresoli7aa81a42011-05-17 00:03:40 +0000503 break;
504#endif
Dmitrii Merkurev308252d2023-04-12 19:49:30 +0100505#if defined(CONFIG_UDP_FUNCTION_FASTBOOT)
506 case FASTBOOT_UDP:
507 fastboot_udp_start_server();
Alex Kiernand5aa57c2018-05-29 15:30:53 +0000508 break;
509#endif
Dmitrii Merkurev308252d2023-04-12 19:49:30 +0100510#if defined(CONFIG_TCP_FUNCTION_FASTBOOT)
511 case FASTBOOT_TCP:
512 fastboot_tcp_start_server();
513 break;
514#endif
Jon Loeliger54f35c22007-07-09 17:45:14 -0500515#if defined(CONFIG_CMD_DHCP)
wdenk2d966952002-10-31 22:12:35 +0000516 case DHCP:
Joe Hershberger2fe81a52015-04-08 01:41:09 -0500517 bootp_reset();
Joe Hershberger5874dec2015-04-08 01:41:01 -0500518 net_ip.s_addr = 0;
Joe Hershberger2fe81a52015-04-08 01:41:09 -0500519 dhcp_request(); /* Basically same as BOOTP */
wdenk2d966952002-10-31 22:12:35 +0000520 break;
Jon Loeligera9807e52007-07-10 11:05:02 -0500521#endif
Sean Edmonde8c43832023-04-11 10:48:46 -0700522 case DHCP6:
523 if (IS_ENABLED(CONFIG_CMD_DHCP6))
524 dhcp6_start();
525 break;
Krebs, Olafd9249382020-03-09 14:27:55 +0000526#if defined(CONFIG_CMD_BOOTP)
wdenk2d966952002-10-31 22:12:35 +0000527 case BOOTP:
Joe Hershberger2fe81a52015-04-08 01:41:09 -0500528 bootp_reset();
Joe Hershberger5874dec2015-04-08 01:41:01 -0500529 net_ip.s_addr = 0;
Joe Hershberger2fe81a52015-04-08 01:41:09 -0500530 bootp_request();
wdenk2d966952002-10-31 22:12:35 +0000531 break;
Krebs, Olafd9249382020-03-09 14:27:55 +0000532#endif
Peter Tyserf7a48ca2010-09-30 11:25:48 -0500533#if defined(CONFIG_CMD_RARP)
wdenk2d966952002-10-31 22:12:35 +0000534 case RARP:
Joe Hershberger8e805bb2015-04-08 01:41:11 -0500535 rarp_try = 0;
Joe Hershberger5874dec2015-04-08 01:41:01 -0500536 net_ip.s_addr = 0;
Joe Hershberger8e805bb2015-04-08 01:41:11 -0500537 rarp_request();
wdenk2d966952002-10-31 22:12:35 +0000538 break;
Peter Tyserf7a48ca2010-09-30 11:25:48 -0500539#endif
Jon Loeliger54f35c22007-07-09 17:45:14 -0500540#if defined(CONFIG_CMD_PING)
wdenke6466f62003-06-05 19:27:42 +0000541 case PING:
Joe Hershbergerc21bf372012-05-23 07:58:02 +0000542 ping_start();
wdenke6466f62003-06-05 19:27:42 +0000543 break;
544#endif
Viacheslav Mitrofanove03c8aa2022-12-02 12:18:08 +0300545#if defined(CONFIG_CMD_PING6)
546 case PING6:
547 ping6_start();
548 break;
549#endif
Tom Rini57256302019-12-05 19:35:07 -0500550#if defined(CONFIG_CMD_NFS) && !defined(CONFIG_SPL_BUILD)
wdenkbe9c1cb2004-02-24 02:00:03 +0000551 case NFS:
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500552 nfs_start();
wdenkbe9c1cb2004-02-24 02:00:03 +0000553 break;
554#endif
Ying-Chun Liu (PaulLiu)cc96a1d2022-11-08 14:17:29 +0800555#if defined(CONFIG_CMD_WGET)
556 case WGET:
557 wget_start();
558 break;
559#endif
Jon Loeliger54f35c22007-07-09 17:45:14 -0500560#if defined(CONFIG_CMD_CDP)
wdenk145d2c12004-04-15 21:48:45 +0000561 case CDP:
Joe Hershberger527336f2015-04-08 01:41:14 -0500562 cdp_start();
wdenk145d2c12004-04-15 21:48:45 +0000563 break;
564#endif
Holger Denglerae85c072017-07-20 10:10:55 +0200565#if defined(CONFIG_NETCONSOLE) && !defined(CONFIG_SPL_BUILD)
wdenkb8fb6192004-08-02 21:11:11 +0000566 case NETCONS:
Joe Hershbergerd02aa6b2015-04-08 01:41:16 -0500567 nc_start();
wdenkb8fb6192004-08-02 21:11:11 +0000568 break;
569#endif
Robin Getz82f0d232009-07-20 14:53:54 -0400570#if defined(CONFIG_CMD_DNS)
571 case DNS:
Joe Hershbergerf725e342015-04-08 01:41:15 -0500572 dns_start();
Robin Getz82f0d232009-07-20 14:53:54 -0400573 break;
574#endif
Joe Hershbergerb35a3a62012-05-23 08:00:12 +0000575#if defined(CONFIG_CMD_LINK_LOCAL)
576 case LINKLOCAL:
577 link_local_start();
578 break;
579#endif
Lothar Felten776fc102018-06-22 22:29:54 +0200580#if defined(CONFIG_CMD_WOL)
581 case WOL:
582 wol_start();
583 break;
584#endif
Samuel Mendoza-Jonasc8f4ab02022-08-08 21:46:03 +0930585#if defined(CONFIG_PHY_NCSI)
586 case NCSI:
587 ncsi_probe_packages();
588 break;
589#endif
wdenk2d966952002-10-31 22:12:35 +0000590 default:
591 break;
592 }
593
Philippe Reynes6ec70bc2020-09-18 14:13:00 +0200594 if (IS_ENABLED(CONFIG_PROT_UDP) && protocol == UDP)
595 udp_start();
596
wdenk2d966952002-10-31 22:12:35 +0000597 break;
598 }
599
Jon Loeliger54f35c22007-07-09 17:45:14 -0500600#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000601#if defined(CONFIG_SYS_FAULT_ECHO_LINK_DOWN) && \
Uri Mashiach4892d392017-01-19 10:51:45 +0200602 defined(CONFIG_LED_STATUS) && \
603 defined(CONFIG_LED_STATUS_RED)
wdenk49c3f672003-10-08 22:33:00 +0000604 /*
wdenk9c53f402003-10-15 23:53:47 +0000605 * Echo the inverted link state to the fault LED.
wdenk49c3f672003-10-08 22:33:00 +0000606 */
Luca Ceresolie8ccbb02011-05-04 02:40:43 +0000607 if (miiphy_link(eth_get_dev()->name, CONFIG_SYS_FAULT_MII_ADDR))
Uri Mashiach4892d392017-01-19 10:51:45 +0200608 status_led_set(CONFIG_LED_STATUS_RED, CONFIG_LED_STATUS_OFF);
Luca Ceresolie8ccbb02011-05-04 02:40:43 +0000609 else
Uri Mashiach4892d392017-01-19 10:51:45 +0200610 status_led_set(CONFIG_LED_STATUS_RED, CONFIG_LED_STATUS_ON);
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200611#endif /* CONFIG_SYS_FAULT_ECHO_LINK_DOWN, ... */
wdenk49c3f672003-10-08 22:33:00 +0000612#endif /* CONFIG_MII, ... */
Jim Lin6c8921f2013-08-13 19:03:05 +0800613#ifdef CONFIG_USB_KEYBOARD
614 net_busy_flag = 1;
615#endif
wdenk2d966952002-10-31 22:12:35 +0000616
617 /*
618 * Main packet reception loop. Loop receiving packets until
Joe Hershbergerd4bb76a2012-05-23 07:59:14 +0000619 * someone sets `net_state' to a state that terminates.
wdenk2d966952002-10-31 22:12:35 +0000620 */
621 for (;;) {
Stefan Roese80877fa2022-09-02 14:10:46 +0200622 schedule();
Joe Hershbergerd6978a42015-12-21 16:31:35 -0600623 if (arp_timeout_check() > 0)
624 time_start = get_timer(0);
625
Viacheslav Mitrofanovda019102022-12-02 12:18:06 +0300626 if (IS_ENABLED(CONFIG_IPV6)) {
627 if (use_ip6 && (ndisc_timeout_check() > 0))
628 time_start = get_timer(0);
629 }
630
wdenk2d966952002-10-31 22:12:35 +0000631 /*
632 * Check the ethernet for a new packet. The ethernet
633 * receive routine will process it.
Joe Hershbergerf4dc96a2015-03-22 17:09:24 -0500634 * Most drivers return the most recent packet size, but not
635 * errors that may have happened.
wdenk2d966952002-10-31 22:12:35 +0000636 */
Guennadi Liakhovetskib38c2b32008-04-03 17:04:19 +0200637 eth_rx();
wdenk2d966952002-10-31 22:12:35 +0000638
639 /*
640 * Abort if ctrl-c was pressed.
641 */
642 if (ctrlc()) {
Joe Hershbergerde8205a2012-05-23 07:59:24 +0000643 /* cancel any ARP that may not have completed */
Joe Hershberger5874dec2015-04-08 01:41:01 -0500644 net_arp_wait_packet_ip.s_addr = 0;
Joe Hershbergerde8205a2012-05-23 07:59:24 +0000645
Joe Hershbergerf50357b2012-05-23 07:59:15 +0000646 net_cleanup_loop();
wdenk57b2d802003-06-27 21:31:46 +0000647 eth_halt();
Joe Hershberger9f374062012-08-03 10:59:08 +0000648 /* Invalidate the last protocol */
649 eth_set_last_protocol(BOOTP);
650
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000651 puts("\nAbort\n");
Joe Hershberger05a377b2012-05-23 08:01:04 +0000652 /* include a debug print as well incase the debug
653 messages are directed to stderr */
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500654 debug_cond(DEBUG_INT_STATE, "--- net_loop Abort!\n");
Michal Simekafd31602015-08-21 08:49:48 +0200655 ret = -EINTR;
Simon Glass230467c2011-10-24 18:00:01 +0000656 goto done;
wdenk2d966952002-10-31 22:12:35 +0000657 }
658
wdenk2d966952002-10-31 22:12:35 +0000659 /*
660 * Check for a timeout, and run the timeout handler
661 * if we have one.
662 */
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500663 if (time_handler &&
664 ((get_timer(0) - time_start) > time_delta)) {
wdenk2d966952002-10-31 22:12:35 +0000665 thand_f *x;
666
Jon Loeliger54f35c22007-07-09 17:45:14 -0500667#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000668#if defined(CONFIG_SYS_FAULT_ECHO_LINK_DOWN) && \
Uri Mashiach4892d392017-01-19 10:51:45 +0200669 defined(CONFIG_LED_STATUS) && \
670 defined(CONFIG_LED_STATUS_RED)
wdenk49c3f672003-10-08 22:33:00 +0000671 /*
wdenk9c53f402003-10-15 23:53:47 +0000672 * Echo the inverted link state to the fault LED.
wdenk49c3f672003-10-08 22:33:00 +0000673 */
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000674 if (miiphy_link(eth_get_dev()->name,
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500675 CONFIG_SYS_FAULT_MII_ADDR))
Uri Mashiach4892d392017-01-19 10:51:45 +0200676 status_led_set(CONFIG_LED_STATUS_RED,
677 CONFIG_LED_STATUS_OFF);
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500678 else
Uri Mashiach4892d392017-01-19 10:51:45 +0200679 status_led_set(CONFIG_LED_STATUS_RED,
680 CONFIG_LED_STATUS_ON);
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000681#endif /* CONFIG_SYS_FAULT_ECHO_LINK_DOWN, ... */
wdenk49c3f672003-10-08 22:33:00 +0000682#endif /* CONFIG_MII, ... */
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500683 debug_cond(DEBUG_INT_STATE, "--- net_loop timeout\n");
684 x = time_handler;
685 time_handler = (thand_f *)0;
wdenk2d966952002-10-31 22:12:35 +0000686 (*x)();
687 }
688
Joe Hershbergere44a0ea2015-03-22 17:09:07 -0500689 if (net_state == NETLOOP_FAIL)
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500690 ret = net_start_again();
wdenk2d966952002-10-31 22:12:35 +0000691
Joe Hershbergerd4bb76a2012-05-23 07:59:14 +0000692 switch (net_state) {
wdenk2d966952002-10-31 22:12:35 +0000693 case NETLOOP_RESTART:
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500694 net_restarted = 1;
wdenk2d966952002-10-31 22:12:35 +0000695 goto restart;
696
697 case NETLOOP_SUCCESS:
Joe Hershbergerf50357b2012-05-23 07:59:15 +0000698 net_cleanup_loop();
Joe Hershberger290c8992015-04-08 01:41:02 -0500699 if (net_boot_file_size > 0) {
700 printf("Bytes transferred = %d (%x hex)\n",
701 net_boot_file_size, net_boot_file_size);
Simon Glass4d949a22017-08-03 12:22:10 -0600702 env_set_hex("filesize", net_boot_file_size);
Simon Glass892265d2019-12-28 10:45:02 -0700703 env_set_hex("fileaddr", image_load_addr);
wdenk2d966952002-10-31 22:12:35 +0000704 }
Samuel Mendoza-Jonasc8f4ab02022-08-08 21:46:03 +0930705 if (protocol != NETCONS && protocol != NCSI)
Joe Hershberger9f374062012-08-03 10:59:08 +0000706 eth_halt();
707 else
708 eth_halt_state_only();
709
710 eth_set_last_protocol(protocol);
711
Joe Hershberger290c8992015-04-08 01:41:02 -0500712 ret = net_boot_file_size;
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500713 debug_cond(DEBUG_INT_STATE, "--- net_loop Success!\n");
Simon Glass230467c2011-10-24 18:00:01 +0000714 goto done;
wdenk2d966952002-10-31 22:12:35 +0000715
716 case NETLOOP_FAIL:
Joe Hershbergerf50357b2012-05-23 07:59:15 +0000717 net_cleanup_loop();
Joe Hershberger9f374062012-08-03 10:59:08 +0000718 /* Invalidate the last protocol */
719 eth_set_last_protocol(BOOTP);
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500720 debug_cond(DEBUG_INT_STATE, "--- net_loop Fail!\n");
Thomas RIENOESSL2dee4192018-11-21 15:56:07 +0100721 ret = -ENONET;
Simon Glass230467c2011-10-24 18:00:01 +0000722 goto done;
Joe Hershbergerd4bb76a2012-05-23 07:59:14 +0000723
724 case NETLOOP_CONTINUE:
725 continue;
wdenk2d966952002-10-31 22:12:35 +0000726 }
727 }
Simon Glass230467c2011-10-24 18:00:01 +0000728
729done:
Jim Lin6c8921f2013-08-13 19:03:05 +0800730#ifdef CONFIG_USB_KEYBOARD
731 net_busy_flag = 0;
732#endif
Simon Glass2928cd82011-10-26 14:18:38 +0000733#ifdef CONFIG_CMD_TFTPPUT
Simon Glass230467c2011-10-24 18:00:01 +0000734 /* Clear out the handlers */
Joe Hershbergerf50357b2012-05-23 07:59:15 +0000735 net_set_udp_handler(NULL);
Simon Glass230467c2011-10-24 18:00:01 +0000736 net_set_icmp_handler(NULL);
Simon Glass2928cd82011-10-26 14:18:38 +0000737#endif
Leonid Iziumtsev4b58b052018-05-08 15:55:50 +0200738 net_set_state(prev_net_state);
Ramon Friedac598c12019-07-18 21:43:30 +0300739
740#if defined(CONFIG_CMD_PCAP)
741 if (pcap_active())
742 pcap_print_status();
743#endif
Simon Glass230467c2011-10-24 18:00:01 +0000744 return ret;
wdenk2d966952002-10-31 22:12:35 +0000745}
746
747/**********************************************************************/
748
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500749static void start_again_timeout_handler(void)
wdenk2d966952002-10-31 22:12:35 +0000750{
Joe Hershbergerd4bb76a2012-05-23 07:59:14 +0000751 net_set_state(NETLOOP_RESTART);
wdenk2d966952002-10-31 22:12:35 +0000752}
753
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500754int net_start_again(void)
wdenk2d966952002-10-31 22:12:35 +0000755{
wdenk05939202004-04-18 17:39:38 +0000756 char *nretry;
Remy Bohmer0ddb8e82009-10-28 22:13:39 +0100757 int retry_forever = 0;
758 unsigned long retrycnt = 0;
Joe Hershbergerf4dc96a2015-03-22 17:09:24 -0500759 int ret;
wdenk145d2c12004-04-15 21:48:45 +0000760
Simon Glass64b723f2017-08-03 12:22:12 -0600761 nretry = env_get("netretry");
Remy Bohmer0ddb8e82009-10-28 22:13:39 +0100762 if (nretry) {
763 if (!strcmp(nretry, "yes"))
764 retry_forever = 1;
765 else if (!strcmp(nretry, "no"))
766 retrycnt = 0;
767 else if (!strcmp(nretry, "once"))
768 retrycnt = 1;
769 else
770 retrycnt = simple_strtoul(nretry, NULL, 0);
Joe Hershbergere44a0ea2015-03-22 17:09:07 -0500771 } else {
772 retrycnt = 0;
773 retry_forever = 0;
774 }
Remy Bohmer0ddb8e82009-10-28 22:13:39 +0100775
Leonid Iziumtsevfb7c94c2018-03-09 15:29:06 +0100776 if ((!retry_forever) && (net_try_count > retrycnt)) {
Remy Bohmer0ddb8e82009-10-28 22:13:39 +0100777 eth_halt();
Joe Hershbergerd4bb76a2012-05-23 07:59:14 +0000778 net_set_state(NETLOOP_FAIL);
Joe Hershbergerf4dc96a2015-03-22 17:09:24 -0500779 /*
780 * We don't provide a way for the protocol to return an error,
781 * but this is almost always the reason.
782 */
783 return -ETIMEDOUT;
wdenk145d2c12004-04-15 21:48:45 +0000784 }
Remy Bohmer0ddb8e82009-10-28 22:13:39 +0100785
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500786 net_try_count++;
Remy Bohmer0ddb8e82009-10-28 22:13:39 +0100787
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000788 eth_halt();
Matthias Fuchsa02d62b2007-12-27 16:58:41 +0100789#if !defined(CONFIG_NET_DO_NOT_TRY_ANOTHER)
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500790 eth_try_another(!net_restarted);
Matthias Fuchsa02d62b2007-12-27 16:58:41 +0100791#endif
Joe Hershbergerf4dc96a2015-03-22 17:09:24 -0500792 ret = eth_init();
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500793 if (net_restart_wrap) {
794 net_restart_wrap = 0;
795 if (net_dev_exists) {
796 net_set_timeout_handler(10000UL,
797 start_again_timeout_handler);
Joe Hershbergerf50357b2012-05-23 07:59:15 +0000798 net_set_udp_handler(NULL);
wdenk05939202004-04-18 17:39:38 +0000799 } else {
Joe Hershbergerd4bb76a2012-05-23 07:59:14 +0000800 net_set_state(NETLOOP_FAIL);
wdenk2d966952002-10-31 22:12:35 +0000801 }
wdenk05939202004-04-18 17:39:38 +0000802 } else {
Joe Hershbergerd4bb76a2012-05-23 07:59:14 +0000803 net_set_state(NETLOOP_RESTART);
wdenk2d966952002-10-31 22:12:35 +0000804 }
Joe Hershbergerf4dc96a2015-03-22 17:09:24 -0500805 return ret;
wdenk2d966952002-10-31 22:12:35 +0000806}
807
808/**********************************************************************/
809/*
810 * Miscelaneous bits.
811 */
812
Joe Hershbergerf50357b2012-05-23 07:59:15 +0000813static void dummy_handler(uchar *pkt, unsigned dport,
Joe Hershberger5874dec2015-04-08 01:41:01 -0500814 struct in_addr sip, unsigned sport,
Joe Hershbergerf50357b2012-05-23 07:59:15 +0000815 unsigned len)
Joe Hershbergeraae05082012-05-23 07:58:01 +0000816{
Joe Hershbergeraae05082012-05-23 07:58:01 +0000817}
818
Joe Hershbergerf50357b2012-05-23 07:59:15 +0000819rxhand_f *net_get_udp_handler(void)
820{
821 return udp_packet_handler;
822}
Joe Hershbergeraae05082012-05-23 07:58:01 +0000823
Joe Hershbergerf50357b2012-05-23 07:59:15 +0000824void net_set_udp_handler(rxhand_f *f)
825{
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500826 debug_cond(DEBUG_INT_STATE, "--- net_loop UDP handler set (%p)\n", f);
Joe Hershbergerf50357b2012-05-23 07:59:15 +0000827 if (f == NULL)
828 udp_packet_handler = dummy_handler;
829 else
830 udp_packet_handler = f;
831}
832
833rxhand_f *net_get_arp_handler(void)
wdenk2d966952002-10-31 22:12:35 +0000834{
Joe Hershbergerf50357b2012-05-23 07:59:15 +0000835 return arp_packet_handler;
wdenk2d966952002-10-31 22:12:35 +0000836}
837
Joe Hershbergerf50357b2012-05-23 07:59:15 +0000838void net_set_arp_handler(rxhand_f *f)
839{
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500840 debug_cond(DEBUG_INT_STATE, "--- net_loop ARP handler set (%p)\n", f);
Joe Hershbergerf50357b2012-05-23 07:59:15 +0000841 if (f == NULL)
842 arp_packet_handler = dummy_handler;
843 else
844 arp_packet_handler = f;
845}
846
Simon Glass2928cd82011-10-26 14:18:38 +0000847#ifdef CONFIG_CMD_TFTPPUT
Simon Glass230467c2011-10-24 18:00:01 +0000848void net_set_icmp_handler(rxhand_icmp_f *f)
849{
850 packet_icmp_handler = f;
851}
Simon Glass2928cd82011-10-26 14:18:38 +0000852#endif
wdenk2d966952002-10-31 22:12:35 +0000853
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500854void net_set_timeout_handler(ulong iv, thand_f *f)
wdenk2d966952002-10-31 22:12:35 +0000855{
856 if (iv == 0) {
Joe Hershberger05a377b2012-05-23 08:01:04 +0000857 debug_cond(DEBUG_INT_STATE,
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500858 "--- net_loop timeout handler cancelled\n");
859 time_handler = (thand_f *)0;
wdenk2d966952002-10-31 22:12:35 +0000860 } else {
Joe Hershberger05a377b2012-05-23 08:01:04 +0000861 debug_cond(DEBUG_INT_STATE,
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500862 "--- net_loop timeout handler set (%p)\n", f);
863 time_handler = f;
864 time_start = get_timer(0);
865 time_delta = iv * CONFIG_SYS_HZ / 1000;
wdenk2d966952002-10-31 22:12:35 +0000866 }
867}
868
Joe Hershbergere79a5182018-09-26 16:49:02 -0500869uchar *net_get_async_tx_pkt_buf(void)
870{
871 if (arp_is_waiting())
872 return arp_tx_packet; /* If we are waiting, we already sent */
873 else
874 return net_tx_packet;
875}
876
Joe Hershbergera8ca4f62015-04-08 01:41:05 -0500877int net_send_udp_packet(uchar *ether, struct in_addr dest, int dport, int sport,
Joe Hershberger1a6b8d82012-05-23 07:58:10 +0000878 int payload_len)
wdenke6466f62003-06-05 19:27:42 +0000879{
Duncan Haref1447c92018-06-24 15:40:41 -0700880 return net_send_ip_packet(ether, dest, dport, sport, payload_len,
881 IPPROTO_UDP, 0, 0, 0);
882}
883
Ying-Chun Liu (PaulLiu)41efed12022-11-08 14:17:28 +0800884#if defined(CONFIG_PROT_TCP)
885int net_send_tcp_packet(int payload_len, int dport, int sport, u8 action,
886 u32 tcp_seq_num, u32 tcp_ack_num)
887{
888 return net_send_ip_packet(net_server_ethaddr, net_server_ip, dport,
889 sport, payload_len, IPPROTO_TCP, action,
890 tcp_seq_num, tcp_ack_num);
891}
892#endif
893
Duncan Haref1447c92018-06-24 15:40:41 -0700894int net_send_ip_packet(uchar *ether, struct in_addr dest, int dport, int sport,
895 int payload_len, int proto, u8 action, u32 tcp_seq_num,
896 u32 tcp_ack_num)
897{
wdenk145d2c12004-04-15 21:48:45 +0000898 uchar *pkt;
Joe Hershberger16be9cb2012-05-23 07:59:08 +0000899 int eth_hdr_size;
900 int pkt_hdr_size;
wdenk145d2c12004-04-15 21:48:45 +0000901
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500902 /* make sure the net_tx_packet is initialized (net_init() was called) */
Joe Hershbergera8ca4f62015-04-08 01:41:05 -0500903 assert(net_tx_packet != NULL);
904 if (net_tx_packet == NULL)
Joe Hershberger017e5c42012-05-23 07:59:22 +0000905 return -1;
906
wdenke6466f62003-06-05 19:27:42 +0000907 /* convert to new style broadcast */
Joe Hershberger5874dec2015-04-08 01:41:01 -0500908 if (dest.s_addr == 0)
909 dest.s_addr = 0xFFFFFFFF;
wdenke6466f62003-06-05 19:27:42 +0000910
911 /* if broadcast, make the ether address a broadcast and don't do ARP */
Joe Hershberger5874dec2015-04-08 01:41:01 -0500912 if (dest.s_addr == 0xFFFFFFFF)
Joe Hershberger8ecdbed2015-04-08 01:41:04 -0500913 ether = (uchar *)net_bcast_ethaddr;
wdenke6466f62003-06-05 19:27:42 +0000914
Joe Hershbergera8ca4f62015-04-08 01:41:05 -0500915 pkt = (uchar *)net_tx_packet;
Joe Hershberger16be9cb2012-05-23 07:59:08 +0000916
Joe Hershbergera8ca4f62015-04-08 01:41:05 -0500917 eth_hdr_size = net_set_ether(pkt, ether, PROT_IP);
Duncan Haref1447c92018-06-24 15:40:41 -0700918
919 switch (proto) {
920 case IPPROTO_UDP:
921 net_set_udp_header(pkt + eth_hdr_size, dest, dport, sport,
922 payload_len);
923 pkt_hdr_size = eth_hdr_size + IP_UDP_HDR_SIZE;
924 break;
Ying-Chun Liu (PaulLiu)41efed12022-11-08 14:17:28 +0800925#if defined(CONFIG_PROT_TCP)
926 case IPPROTO_TCP:
927 pkt_hdr_size = eth_hdr_size
928 + tcp_set_tcp_header(pkt + eth_hdr_size, dport, sport,
929 payload_len, action, tcp_seq_num,
930 tcp_ack_num);
931 break;
932#endif
Duncan Haref1447c92018-06-24 15:40:41 -0700933 default:
934 return -EINVAL;
935 }
wdenke6466f62003-06-05 19:27:42 +0000936
Joe Hershbergerde8205a2012-05-23 07:59:24 +0000937 /* if MAC address was not discovered yet, do an ARP request */
Joe Hershberger8ecdbed2015-04-08 01:41:04 -0500938 if (memcmp(ether, net_null_ethaddr, 6) == 0) {
Joe Hershberger05a377b2012-05-23 08:01:04 +0000939 debug_cond(DEBUG_DEV_PKT, "sending ARP for %pI4\n", &dest);
Robin Getz9e0a4d62009-07-23 03:01:03 -0400940
Joe Hershberger16be9cb2012-05-23 07:59:08 +0000941 /* save the ip and eth addr for the packet to send after arp */
Joe Hershberger5874dec2015-04-08 01:41:01 -0500942 net_arp_wait_packet_ip = dest;
Joe Hershberger85ae7762015-04-08 01:41:08 -0500943 arp_wait_packet_ethaddr = ether;
wdenk145d2c12004-04-15 21:48:45 +0000944
wdenke6466f62003-06-05 19:27:42 +0000945 /* size of the waiting packet */
Joe Hershberger85ae7762015-04-08 01:41:08 -0500946 arp_wait_tx_packet_size = pkt_hdr_size + payload_len;
wdenke6466f62003-06-05 19:27:42 +0000947
948 /* and do the ARP request */
Joe Hershberger85ae7762015-04-08 01:41:08 -0500949 arp_wait_try = 1;
950 arp_wait_timer_start = get_timer(0);
951 arp_request();
wdenke6466f62003-06-05 19:27:42 +0000952 return 1; /* waiting */
Joe Hershberger16be9cb2012-05-23 07:59:08 +0000953 } else {
Joe Hershberger05a377b2012-05-23 08:01:04 +0000954 debug_cond(DEBUG_DEV_PKT, "sending UDP to %pI4/%pM\n",
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500955 &dest, ether);
Joe Hershbergera8ca4f62015-04-08 01:41:05 -0500956 net_send_packet(net_tx_packet, pkt_hdr_size + payload_len);
Joe Hershberger16be9cb2012-05-23 07:59:08 +0000957 return 0; /* transmitted */
wdenke6466f62003-06-05 19:27:42 +0000958 }
wdenke6466f62003-06-05 19:27:42 +0000959}
wdenk145d2c12004-04-15 21:48:45 +0000960
Alessandro Rubinif119e3d2009-08-07 13:58:56 +0200961#ifdef CONFIG_IP_DEFRAG
962/*
963 * This function collects fragments in a single packet, according
964 * to the algorithm in RFC815. It returns NULL or the pointer to
965 * a complete packet, in static storage
966 */
Joe Hershberger9d390302016-08-15 14:42:15 -0500967#define IP_PKTSIZE (CONFIG_NET_MAXDEFRAG)
Alessandro Rubinif119e3d2009-08-07 13:58:56 +0200968
Joe Hershbergerc686fa12012-05-23 07:58:05 +0000969#define IP_MAXUDP (IP_PKTSIZE - IP_HDR_SIZE)
Alessandro Rubinif119e3d2009-08-07 13:58:56 +0200970
971/*
972 * this is the packet being assembled, either data or frag control.
973 * Fragments go by 8 bytes, so this union must be 8 bytes long
974 */
975struct hole {
976 /* first_byte is address of this structure */
977 u16 last_byte; /* last byte in this hole + 1 (begin of next hole) */
978 u16 next_hole; /* index of next (in 8-b blocks), 0 == none */
979 u16 prev_hole; /* index of prev, 0 == none */
980 u16 unused;
981};
982
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500983static struct ip_udp_hdr *__net_defragment(struct ip_udp_hdr *ip, int *lenp)
Alessandro Rubinif119e3d2009-08-07 13:58:56 +0200984{
Joe Hershberger77001b432012-05-15 08:59:08 +0000985 static uchar pkt_buff[IP_PKTSIZE] __aligned(PKTALIGN);
Alessandro Rubinif119e3d2009-08-07 13:58:56 +0200986 static u16 first_hole, total_len;
987 struct hole *payload, *thisfrag, *h, *newh;
Joe Hershberger6fe8b452012-05-23 07:58:04 +0000988 struct ip_udp_hdr *localip = (struct ip_udp_hdr *)pkt_buff;
Alessandro Rubinif119e3d2009-08-07 13:58:56 +0200989 uchar *indata = (uchar *)ip;
990 int offset8, start, len, done = 0;
991 u16 ip_off = ntohs(ip->ip_off);
992
Rasmus Villemoes547e7e62022-10-14 19:43:39 +0200993 /*
994 * Calling code already rejected <, but we don't have to deal
995 * with an IP fragment with no payload.
996 */
997 if (ntohs(ip->ip_len) <= IP_HDR_SIZE)
Fabio Estevama0aaa322022-05-26 11:14:37 -0300998 return NULL;
999
Alessandro Rubinif119e3d2009-08-07 13:58:56 +02001000 /* payload starts after IP header, this fragment is in there */
Joe Hershbergerc686fa12012-05-23 07:58:05 +00001001 payload = (struct hole *)(pkt_buff + IP_HDR_SIZE);
Alessandro Rubinif119e3d2009-08-07 13:58:56 +02001002 offset8 = (ip_off & IP_OFFS);
1003 thisfrag = payload + offset8;
1004 start = offset8 * 8;
Joe Hershbergerc686fa12012-05-23 07:58:05 +00001005 len = ntohs(ip->ip_len) - IP_HDR_SIZE;
Alessandro Rubinif119e3d2009-08-07 13:58:56 +02001006
Rasmus Villemoes547e7e62022-10-14 19:43:39 +02001007 /* All but last fragment must have a multiple-of-8 payload. */
1008 if ((len & 7) && (ip_off & IP_FLAGS_MFRAG))
1009 return NULL;
1010
Alessandro Rubinif119e3d2009-08-07 13:58:56 +02001011 if (start + len > IP_MAXUDP) /* fragment extends too far */
1012 return NULL;
1013
1014 if (!total_len || localip->ip_id != ip->ip_id) {
1015 /* new (or different) packet, reset structs */
1016 total_len = 0xffff;
1017 payload[0].last_byte = ~0;
1018 payload[0].next_hole = 0;
1019 payload[0].prev_hole = 0;
1020 first_hole = 0;
1021 /* any IP header will work, copy the first we received */
Joe Hershbergerc686fa12012-05-23 07:58:05 +00001022 memcpy(localip, ip, IP_HDR_SIZE);
Alessandro Rubinif119e3d2009-08-07 13:58:56 +02001023 }
1024
1025 /*
1026 * What follows is the reassembly algorithm. We use the payload
1027 * array as a linked list of hole descriptors, as each hole starts
1028 * at a multiple of 8 bytes. However, last byte can be whatever value,
1029 * so it is represented as byte count, not as 8-byte blocks.
1030 */
1031
1032 h = payload + first_hole;
1033 while (h->last_byte < start) {
1034 if (!h->next_hole) {
1035 /* no hole that far away */
1036 return NULL;
1037 }
1038 h = payload + h->next_hole;
1039 }
1040
Fillod Stephanee7ade8b2010-06-11 19:26:43 +02001041 /* last fragment may be 1..7 bytes, the "+7" forces acceptance */
1042 if (offset8 + ((len + 7) / 8) <= h - payload) {
Alessandro Rubinif119e3d2009-08-07 13:58:56 +02001043 /* no overlap with holes (dup fragment?) */
1044 return NULL;
1045 }
1046
1047 if (!(ip_off & IP_FLAGS_MFRAG)) {
1048 /* no more fragmentss: truncate this (last) hole */
1049 total_len = start + len;
1050 h->last_byte = start + len;
1051 }
1052
1053 /*
Rasmus Villemoes366587d2022-10-17 09:52:51 +02001054 * There is some overlap: fix the hole list. This code deals
1055 * with a fragment that overlaps with two different holes
1056 * (thus being a superset of a previously-received fragment)
1057 * by only using the part of the fragment that fits in the
1058 * first hole.
Alessandro Rubinif119e3d2009-08-07 13:58:56 +02001059 */
Rasmus Villemoes366587d2022-10-17 09:52:51 +02001060 if (h->last_byte < start + len)
1061 len = h->last_byte - start;
Alessandro Rubinif119e3d2009-08-07 13:58:56 +02001062
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001063 if ((h >= thisfrag) && (h->last_byte <= start + len)) {
Alessandro Rubinif119e3d2009-08-07 13:58:56 +02001064 /* complete overlap with hole: remove hole */
1065 if (!h->prev_hole && !h->next_hole) {
1066 /* last remaining hole */
1067 done = 1;
1068 } else if (!h->prev_hole) {
1069 /* first hole */
1070 first_hole = h->next_hole;
1071 payload[h->next_hole].prev_hole = 0;
1072 } else if (!h->next_hole) {
1073 /* last hole */
1074 payload[h->prev_hole].next_hole = 0;
1075 } else {
1076 /* in the middle of the list */
1077 payload[h->next_hole].prev_hole = h->prev_hole;
1078 payload[h->prev_hole].next_hole = h->next_hole;
1079 }
1080
1081 } else if (h->last_byte <= start + len) {
1082 /* overlaps with final part of the hole: shorten this hole */
1083 h->last_byte = start;
1084
1085 } else if (h >= thisfrag) {
1086 /* overlaps with initial part of the hole: move this hole */
1087 newh = thisfrag + (len / 8);
1088 *newh = *h;
1089 h = newh;
1090 if (h->next_hole)
1091 payload[h->next_hole].prev_hole = (h - payload);
1092 if (h->prev_hole)
1093 payload[h->prev_hole].next_hole = (h - payload);
1094 else
1095 first_hole = (h - payload);
1096
1097 } else {
1098 /* fragment sits in the middle: split the hole */
1099 newh = thisfrag + (len / 8);
1100 *newh = *h;
1101 h->last_byte = start;
1102 h->next_hole = (newh - payload);
1103 newh->prev_hole = (h - payload);
1104 if (newh->next_hole)
1105 payload[newh->next_hole].prev_hole = (newh - payload);
1106 }
1107
1108 /* finally copy this fragment and possibly return whole packet */
Joe Hershbergerc686fa12012-05-23 07:58:05 +00001109 memcpy((uchar *)thisfrag, indata + IP_HDR_SIZE, len);
Alessandro Rubinif119e3d2009-08-07 13:58:56 +02001110 if (!done)
1111 return NULL;
1112
Joe Hershbergerc686fa12012-05-23 07:58:05 +00001113 *lenp = total_len + IP_HDR_SIZE;
Rasmus Villemoes932d4842022-10-14 19:43:40 +02001114 localip->ip_len = htons(*lenp);
Alessandro Rubinif119e3d2009-08-07 13:58:56 +02001115 return localip;
1116}
1117
Joe Hershbergerc80b41b02015-04-08 01:41:21 -05001118static inline struct ip_udp_hdr *net_defragment(struct ip_udp_hdr *ip,
1119 int *lenp)
Alessandro Rubinif119e3d2009-08-07 13:58:56 +02001120{
1121 u16 ip_off = ntohs(ip->ip_off);
1122 if (!(ip_off & (IP_OFFS | IP_FLAGS_MFRAG)))
1123 return ip; /* not a fragment */
Joe Hershbergerc80b41b02015-04-08 01:41:21 -05001124 return __net_defragment(ip, lenp);
Alessandro Rubinif119e3d2009-08-07 13:58:56 +02001125}
1126
1127#else /* !CONFIG_IP_DEFRAG */
1128
Joe Hershbergerc80b41b02015-04-08 01:41:21 -05001129static inline struct ip_udp_hdr *net_defragment(struct ip_udp_hdr *ip,
1130 int *lenp)
Alessandro Rubinif119e3d2009-08-07 13:58:56 +02001131{
1132 u16 ip_off = ntohs(ip->ip_off);
1133 if (!(ip_off & (IP_OFFS | IP_FLAGS_MFRAG)))
1134 return ip; /* not a fragment */
1135 return NULL;
1136}
1137#endif
wdenk2d966952002-10-31 22:12:35 +00001138
Simon Glass43c72962011-10-24 18:00:00 +00001139/**
1140 * Receive an ICMP packet. We deal with REDIRECT and PING here, and silently
1141 * drop others.
1142 *
1143 * @parma ip IP packet containing the ICMP
1144 */
Joe Hershberger6fe8b452012-05-23 07:58:04 +00001145static void receive_icmp(struct ip_udp_hdr *ip, int len,
Joe Hershberger5874dec2015-04-08 01:41:01 -05001146 struct in_addr src_ip, struct ethernet_hdr *et)
Simon Glass43c72962011-10-24 18:00:00 +00001147{
Joe Hershberger78495612012-05-23 07:58:09 +00001148 struct icmp_hdr *icmph = (struct icmp_hdr *)&ip->udp_src;
Simon Glass43c72962011-10-24 18:00:00 +00001149
1150 switch (icmph->type) {
1151 case ICMP_REDIRECT:
1152 if (icmph->code != ICMP_REDIR_HOST)
1153 return;
1154 printf(" ICMP Host Redirect to %pI4 ",
Joe Hershbergerc80b41b02015-04-08 01:41:21 -05001155 &icmph->un.gateway);
Simon Glass43c72962011-10-24 18:00:00 +00001156 break;
Joe Hershbergerc21bf372012-05-23 07:58:02 +00001157 default:
Simon Glass43c72962011-10-24 18:00:00 +00001158#if defined(CONFIG_CMD_PING)
Joe Hershbergerc21bf372012-05-23 07:58:02 +00001159 ping_receive(et, ip, len);
Simon Glass43c72962011-10-24 18:00:00 +00001160#endif
Simon Glass2928cd82011-10-26 14:18:38 +00001161#ifdef CONFIG_CMD_TFTPPUT
Simon Glass230467c2011-10-24 18:00:01 +00001162 if (packet_icmp_handler)
1163 packet_icmp_handler(icmph->type, icmph->code,
Joe Hershbergerc80b41b02015-04-08 01:41:21 -05001164 ntohs(ip->udp_dst), src_ip,
1165 ntohs(ip->udp_src), icmph->un.data,
1166 ntohs(ip->udp_len));
Simon Glass2928cd82011-10-26 14:18:38 +00001167#endif
Simon Glass43c72962011-10-24 18:00:00 +00001168 break;
1169 }
1170}
1171
Joe Hershbergerf77abc52015-03-22 17:09:11 -05001172void net_process_received_packet(uchar *in_packet, int len)
wdenk2d966952002-10-31 22:12:35 +00001173{
Joe Hershberger1178f412012-05-23 07:58:06 +00001174 struct ethernet_hdr *et;
Joe Hershberger6fe8b452012-05-23 07:58:04 +00001175 struct ip_udp_hdr *ip;
Joe Hershberger5874dec2015-04-08 01:41:01 -05001176 struct in_addr dst_ip;
1177 struct in_addr src_ip;
Joe Hershbergerfacf5352012-05-23 07:58:12 +00001178 int eth_proto;
Jon Loeliger54f35c22007-07-09 17:45:14 -05001179#if defined(CONFIG_CMD_CDP)
wdenk145d2c12004-04-15 21:48:45 +00001180 int iscdp;
1181#endif
1182 ushort cti = 0, vlanid = VLAN_NONE, myvlanid, mynvlanid;
wdenk2d966952002-10-31 22:12:35 +00001183
Joe Hershberger05a377b2012-05-23 08:01:04 +00001184 debug_cond(DEBUG_NET_PKT, "packet received\n");
wdenk145d2c12004-04-15 21:48:45 +00001185
Ramon Friedac598c12019-07-18 21:43:30 +03001186#if defined(CONFIG_CMD_PCAP)
1187 pcap_post(in_packet, len, false);
1188#endif
Joe Hershbergera8ca4f62015-04-08 01:41:05 -05001189 net_rx_packet = in_packet;
1190 net_rx_packet_len = len;
Joe Hershbergerf77abc52015-03-22 17:09:11 -05001191 et = (struct ethernet_hdr *)in_packet;
wdenk145d2c12004-04-15 21:48:45 +00001192
1193 /* too small packet? */
1194 if (len < ETHER_HDR_SIZE)
1195 return;
Rafal Jaworowski1f2c9a42007-12-27 18:19:02 +01001196
Alexander Graf94c4b992016-05-06 21:01:01 +02001197#if defined(CONFIG_API) || defined(CONFIG_EFI_LOADER)
Rafal Jaworowski1f2c9a42007-12-27 18:19:02 +01001198 if (push_packet) {
Joe Hershbergerf77abc52015-03-22 17:09:11 -05001199 (*push_packet)(in_packet, len);
Rafal Jaworowski1f2c9a42007-12-27 18:19:02 +01001200 return;
1201 }
1202#endif
wdenk145d2c12004-04-15 21:48:45 +00001203
Jon Loeliger54f35c22007-07-09 17:45:14 -05001204#if defined(CONFIG_CMD_CDP)
wdenk145d2c12004-04-15 21:48:45 +00001205 /* keep track if packet is CDP */
Joe Hershberger00c62a82012-05-23 07:58:00 +00001206 iscdp = is_cdp_packet(et->et_dest);
wdenk145d2c12004-04-15 21:48:45 +00001207#endif
1208
Joe Hershberger013d3872015-04-08 01:41:17 -05001209 myvlanid = ntohs(net_our_vlan);
wdenk145d2c12004-04-15 21:48:45 +00001210 if (myvlanid == (ushort)-1)
1211 myvlanid = VLAN_NONE;
Joe Hershberger013d3872015-04-08 01:41:17 -05001212 mynvlanid = ntohs(net_native_vlan);
wdenk145d2c12004-04-15 21:48:45 +00001213 if (mynvlanid == (ushort)-1)
1214 mynvlanid = VLAN_NONE;
wdenk2d966952002-10-31 22:12:35 +00001215
Joe Hershbergerfacf5352012-05-23 07:58:12 +00001216 eth_proto = ntohs(et->et_protlen);
wdenk2d966952002-10-31 22:12:35 +00001217
Joe Hershbergerfacf5352012-05-23 07:58:12 +00001218 if (eth_proto < 1514) {
Joe Hershberger1178f412012-05-23 07:58:06 +00001219 struct e802_hdr *et802 = (struct e802_hdr *)et;
wdenk2d966952002-10-31 22:12:35 +00001220 /*
Joe Hershbergerc17fa982012-05-23 07:58:11 +00001221 * Got a 802.2 packet. Check the other protocol field.
1222 * XXX VLAN over 802.2+SNAP not implemented!
wdenk2d966952002-10-31 22:12:35 +00001223 */
Joe Hershbergerfacf5352012-05-23 07:58:12 +00001224 eth_proto = ntohs(et802->et_prot);
wdenk145d2c12004-04-15 21:48:45 +00001225
Joe Hershbergerf77abc52015-03-22 17:09:11 -05001226 ip = (struct ip_udp_hdr *)(in_packet + E802_HDR_SIZE);
wdenk2d966952002-10-31 22:12:35 +00001227 len -= E802_HDR_SIZE;
wdenk145d2c12004-04-15 21:48:45 +00001228
Joe Hershbergerfacf5352012-05-23 07:58:12 +00001229 } else if (eth_proto != PROT_VLAN) { /* normal packet */
Joe Hershbergerf77abc52015-03-22 17:09:11 -05001230 ip = (struct ip_udp_hdr *)(in_packet + ETHER_HDR_SIZE);
wdenk2d966952002-10-31 22:12:35 +00001231 len -= ETHER_HDR_SIZE;
wdenk145d2c12004-04-15 21:48:45 +00001232
1233 } else { /* VLAN packet */
Joe Hershbergerb43784c2012-05-23 07:58:07 +00001234 struct vlan_ethernet_hdr *vet =
1235 (struct vlan_ethernet_hdr *)et;
wdenk145d2c12004-04-15 21:48:45 +00001236
Joe Hershberger05a377b2012-05-23 08:01:04 +00001237 debug_cond(DEBUG_NET_PKT, "VLAN packet received\n");
Robin Getz9e0a4d62009-07-23 03:01:03 -04001238
wdenk145d2c12004-04-15 21:48:45 +00001239 /* too small packet? */
1240 if (len < VLAN_ETHER_HDR_SIZE)
1241 return;
1242
1243 /* if no VLAN active */
Joe Hershberger013d3872015-04-08 01:41:17 -05001244 if ((ntohs(net_our_vlan) & VLAN_IDMASK) == VLAN_NONE
Jon Loeliger54f35c22007-07-09 17:45:14 -05001245#if defined(CONFIG_CMD_CDP)
wdenk145d2c12004-04-15 21:48:45 +00001246 && iscdp == 0
1247#endif
1248 )
1249 return;
1250
1251 cti = ntohs(vet->vet_tag);
1252 vlanid = cti & VLAN_IDMASK;
Joe Hershbergerfacf5352012-05-23 07:58:12 +00001253 eth_proto = ntohs(vet->vet_type);
wdenk145d2c12004-04-15 21:48:45 +00001254
Joe Hershbergerf77abc52015-03-22 17:09:11 -05001255 ip = (struct ip_udp_hdr *)(in_packet + VLAN_ETHER_HDR_SIZE);
wdenk145d2c12004-04-15 21:48:45 +00001256 len -= VLAN_ETHER_HDR_SIZE;
wdenk2d966952002-10-31 22:12:35 +00001257 }
1258
Joe Hershberger05a377b2012-05-23 08:01:04 +00001259 debug_cond(DEBUG_NET_PKT, "Receive from protocol 0x%x\n", eth_proto);
wdenk2d966952002-10-31 22:12:35 +00001260
Jon Loeliger54f35c22007-07-09 17:45:14 -05001261#if defined(CONFIG_CMD_CDP)
wdenk145d2c12004-04-15 21:48:45 +00001262 if (iscdp) {
Joe Hershbergerd01a7a02012-05-23 07:58:13 +00001263 cdp_receive((uchar *)ip, len);
wdenk145d2c12004-04-15 21:48:45 +00001264 return;
1265 }
1266#endif
1267
1268 if ((myvlanid & VLAN_IDMASK) != VLAN_NONE) {
1269 if (vlanid == VLAN_NONE)
1270 vlanid = (mynvlanid & VLAN_IDMASK);
1271 /* not matched? */
1272 if (vlanid != (myvlanid & VLAN_IDMASK))
1273 return;
1274 }
1275
Joe Hershbergerfacf5352012-05-23 07:58:12 +00001276 switch (eth_proto) {
wdenk2d966952002-10-31 22:12:35 +00001277 case PROT_ARP:
Joe Hershberger85ae7762015-04-08 01:41:08 -05001278 arp_receive(et, ip, len);
wdenkcb99da52005-01-12 00:15:14 +00001279 break;
wdenk2d966952002-10-31 22:12:35 +00001280
Peter Tyserf7a48ca2010-09-30 11:25:48 -05001281#ifdef CONFIG_CMD_RARP
wdenk2d966952002-10-31 22:12:35 +00001282 case PROT_RARP:
Joe Hershberger61b4de62012-05-23 07:58:03 +00001283 rarp_receive(ip, len);
wdenk2d966952002-10-31 22:12:35 +00001284 break;
Peter Tyserf7a48ca2010-09-30 11:25:48 -05001285#endif
Viacheslav Mitrofanovda019102022-12-02 12:18:06 +03001286#if IS_ENABLED(CONFIG_IPV6)
1287 case PROT_IP6:
1288 net_ip6_handler(et, (struct ip6_hdr *)ip, len);
Viacheslav Mitrofanova1218172022-12-06 10:08:16 +03001289 break;
Viacheslav Mitrofanovda019102022-12-02 12:18:06 +03001290#endif
wdenk2d966952002-10-31 22:12:35 +00001291 case PROT_IP:
Joe Hershberger05a377b2012-05-23 08:01:04 +00001292 debug_cond(DEBUG_NET_PKT, "Got IP\n");
Alessandro Rubinif119e3d2009-08-07 13:58:56 +02001293 /* Before we start poking the header, make sure it is there */
Rasmus Villemoes0007cba2022-10-14 19:43:38 +02001294 if (len < IP_HDR_SIZE) {
Joe Hershberger6fe8b452012-05-23 07:58:04 +00001295 debug("len bad %d < %lu\n", len,
Rasmus Villemoes0007cba2022-10-14 19:43:38 +02001296 (ulong)IP_HDR_SIZE);
wdenk2d966952002-10-31 22:12:35 +00001297 return;
1298 }
Alessandro Rubinif119e3d2009-08-07 13:58:56 +02001299 /* Check the packet length */
wdenk2d966952002-10-31 22:12:35 +00001300 if (len < ntohs(ip->ip_len)) {
Joe Hershberger05a377b2012-05-23 08:01:04 +00001301 debug("len bad %d < %d\n", len, ntohs(ip->ip_len));
wdenk2d966952002-10-31 22:12:35 +00001302 return;
1303 }
1304 len = ntohs(ip->ip_len);
Rasmus Villemoes0007cba2022-10-14 19:43:38 +02001305 if (len < IP_HDR_SIZE) {
1306 debug("bad ip->ip_len %d < %d\n", len, (int)IP_HDR_SIZE);
1307 return;
1308 }
Joe Hershberger05a377b2012-05-23 08:01:04 +00001309 debug_cond(DEBUG_NET_PKT, "len=%d, v=%02x\n",
Joe Hershbergerc80b41b02015-04-08 01:41:21 -05001310 len, ip->ip_hl_v & 0xff);
Robin Getz9e0a4d62009-07-23 03:01:03 -04001311
Alessandro Rubinif119e3d2009-08-07 13:58:56 +02001312 /* Can't deal with anything except IPv4 */
Luca Ceresolie8ccbb02011-05-04 02:40:43 +00001313 if ((ip->ip_hl_v & 0xf0) != 0x40)
wdenk2d966952002-10-31 22:12:35 +00001314 return;
Alessandro Rubinif119e3d2009-08-07 13:58:56 +02001315 /* Can't deal with IP options (headers != 20 bytes) */
Rasmus Villemoes9ac2c5c2022-10-14 19:43:37 +02001316 if ((ip->ip_hl_v & 0x0f) != 0x05)
Remy Bohmerb9535782008-06-03 15:48:17 +02001317 return;
Alessandro Rubinif119e3d2009-08-07 13:58:56 +02001318 /* Check the Checksum of the header */
Simon Glassdfcdcee2015-01-19 22:16:08 -07001319 if (!ip_checksum_ok((uchar *)ip, IP_HDR_SIZE)) {
Joe Hershberger05a377b2012-05-23 08:01:04 +00001320 debug("checksum bad\n");
wdenk2d966952002-10-31 22:12:35 +00001321 return;
1322 }
Alessandro Rubinif119e3d2009-08-07 13:58:56 +02001323 /* If it is not for us, ignore it */
Joe Hershberger5874dec2015-04-08 01:41:01 -05001324 dst_ip = net_read_ip(&ip->ip_dst);
1325 if (net_ip.s_addr && dst_ip.s_addr != net_ip.s_addr &&
1326 dst_ip.s_addr != 0xFFFFFFFF) {
Luca Ceresolib19d0b72011-05-04 02:40:46 +00001327 return;
wdenk2d966952002-10-31 22:12:35 +00001328 }
Luca Ceresoli428ab362011-04-18 06:19:50 +00001329 /* Read source IP address for later use */
Joe Hershberger5874dec2015-04-08 01:41:01 -05001330 src_ip = net_read_ip(&ip->ip_src);
wdenk2d966952002-10-31 22:12:35 +00001331 /*
Alessandro Rubinif119e3d2009-08-07 13:58:56 +02001332 * The function returns the unchanged packet if it's not
1333 * a fragment, and either the complete packet or NULL if
1334 * it is a fragment (if !CONFIG_IP_DEFRAG, it returns NULL)
1335 */
Joe Hershbergerc80b41b02015-04-08 01:41:21 -05001336 ip = net_defragment(ip, &len);
Luca Ceresolidb210822011-05-04 02:40:47 +00001337 if (!ip)
Alessandro Rubinif119e3d2009-08-07 13:58:56 +02001338 return;
1339 /*
wdenk2d966952002-10-31 22:12:35 +00001340 * watch for ICMP host redirects
1341 *
wdenk57b2d802003-06-27 21:31:46 +00001342 * There is no real handler code (yet). We just watch
1343 * for ICMP host redirect messages. In case anybody
1344 * sees these messages: please contact me
1345 * (wd@denx.de), or - even better - send me the
1346 * necessary fixes :-)
wdenk2d966952002-10-31 22:12:35 +00001347 *
wdenk57b2d802003-06-27 21:31:46 +00001348 * Note: in all cases where I have seen this so far
1349 * it was a problem with the router configuration,
1350 * for instance when a router was configured in the
1351 * BOOTP reply, but the TFTP server was on the same
1352 * subnet. So this is probably a warning that your
1353 * configuration might be wrong. But I'm not really
1354 * sure if there aren't any other situations.
Simon Glass230467c2011-10-24 18:00:01 +00001355 *
1356 * Simon Glass <sjg@chromium.org>: We get an ICMP when
1357 * we send a tftp packet to a dead connection, or when
1358 * there is no server at the other end.
wdenk2d966952002-10-31 22:12:35 +00001359 */
1360 if (ip->ip_p == IPPROTO_ICMP) {
Simon Glass43c72962011-10-24 18:00:00 +00001361 receive_icmp(ip, len, src_ip, et);
1362 return;
Ying-Chun Liu (PaulLiu)41efed12022-11-08 14:17:28 +08001363#if defined(CONFIG_PROT_TCP)
1364 } else if (ip->ip_p == IPPROTO_TCP) {
1365 debug_cond(DEBUG_DEV_PKT,
1366 "TCP PH (to=%pI4, from=%pI4, len=%d)\n",
1367 &dst_ip, &src_ip, len);
1368
1369 rxhand_tcp_f((union tcp_build_pkt *)ip, len);
1370 return;
1371#endif
wdenk2d966952002-10-31 22:12:35 +00001372 } else if (ip->ip_p != IPPROTO_UDP) { /* Only UDP packets */
1373 return;
1374 }
1375
Rasmus Villemoes932d4842022-10-14 19:43:40 +02001376 if (ntohs(ip->udp_len) < UDP_HDR_SIZE || ntohs(ip->udp_len) > len - IP_HDR_SIZE)
liucheng (G)c580b292019-08-29 13:47:33 +00001377 return;
1378
Joe Hershberger05a377b2012-05-23 08:01:04 +00001379 debug_cond(DEBUG_DEV_PKT,
Joe Hershbergerc80b41b02015-04-08 01:41:21 -05001380 "received UDP (to=%pI4, from=%pI4, len=%d)\n",
1381 &dst_ip, &src_ip, len);
Joe Hershberger05a377b2012-05-23 08:01:04 +00001382
Simon Glasse0eb4ef2021-12-18 11:27:49 -07001383 if (IS_ENABLED(CONFIG_UDP_CHECKSUM) && ip->udp_xsum != 0) {
Wolfgang Denk30b87322005-08-12 23:43:12 +02001384 ulong xsum;
Heinrich Schuchardt16ffe472019-11-05 12:48:19 +01001385 u8 *sumptr;
Stefan Roesedfced812005-08-12 20:06:52 +02001386 ushort sumlen;
1387
1388 xsum = ip->ip_p;
1389 xsum += (ntohs(ip->udp_len));
Joe Hershberger5874dec2015-04-08 01:41:01 -05001390 xsum += (ntohl(ip->ip_src.s_addr) >> 16) & 0x0000ffff;
1391 xsum += (ntohl(ip->ip_src.s_addr) >> 0) & 0x0000ffff;
1392 xsum += (ntohl(ip->ip_dst.s_addr) >> 16) & 0x0000ffff;
1393 xsum += (ntohl(ip->ip_dst.s_addr) >> 0) & 0x0000ffff;
Stefan Roesedfced812005-08-12 20:06:52 +02001394
1395 sumlen = ntohs(ip->udp_len);
Heinrich Schuchardt16ffe472019-11-05 12:48:19 +01001396 sumptr = (u8 *)&ip->udp_src;
Stefan Roesedfced812005-08-12 20:06:52 +02001397
1398 while (sumlen > 1) {
Heinrich Schuchardt16ffe472019-11-05 12:48:19 +01001399 /* inlined ntohs() to avoid alignment errors */
1400 xsum += (sumptr[0] << 8) + sumptr[1];
1401 sumptr += 2;
Stefan Roesedfced812005-08-12 20:06:52 +02001402 sumlen -= 2;
1403 }
Heinrich Schuchardt16ffe472019-11-05 12:48:19 +01001404 if (sumlen > 0)
1405 xsum += (sumptr[0] << 8) + sumptr[0];
Stefan Roesedfced812005-08-12 20:06:52 +02001406 while ((xsum >> 16) != 0) {
Luca Ceresoli5c83a962011-05-11 03:59:54 +00001407 xsum = (xsum & 0x0000ffff) +
1408 ((xsum >> 16) & 0x0000ffff);
Stefan Roesedfced812005-08-12 20:06:52 +02001409 }
1410 if ((xsum != 0x00000000) && (xsum != 0x0000ffff)) {
Wolfgang Denk12cec0a2008-07-11 01:16:00 +02001411 printf(" UDP wrong checksum %08lx %08x\n",
Joe Hershbergerc80b41b02015-04-08 01:41:21 -05001412 xsum, ntohs(ip->udp_xsum));
Stefan Roesedfced812005-08-12 20:06:52 +02001413 return;
1414 }
1415 }
Stefan Roesedfced812005-08-12 20:06:52 +02001416
Holger Denglerae85c072017-07-20 10:10:55 +02001417#if defined(CONFIG_NETCONSOLE) && !defined(CONFIG_SPL_BUILD)
Joe Hershberger6fe8b452012-05-23 07:58:04 +00001418 nc_input_packet((uchar *)ip + IP_UDP_HDR_SIZE,
Joe Hershbergerc80b41b02015-04-08 01:41:21 -05001419 src_ip,
1420 ntohs(ip->udp_dst),
1421 ntohs(ip->udp_src),
1422 ntohs(ip->udp_len) - UDP_HDR_SIZE);
wdenkb8fb6192004-08-02 21:11:11 +00001423#endif
wdenk2d966952002-10-31 22:12:35 +00001424 /*
Joe Hershbergerc80b41b02015-04-08 01:41:21 -05001425 * IP header OK. Pass the packet to the current handler.
wdenk2d966952002-10-31 22:12:35 +00001426 */
Joe Hershbergerf50357b2012-05-23 07:59:15 +00001427 (*udp_packet_handler)((uchar *)ip + IP_UDP_HDR_SIZE,
Joe Hershbergerc80b41b02015-04-08 01:41:21 -05001428 ntohs(ip->udp_dst),
1429 src_ip,
1430 ntohs(ip->udp_src),
1431 ntohs(ip->udp_len) - UDP_HDR_SIZE);
wdenk2d966952002-10-31 22:12:35 +00001432 break;
Lothar Felten776fc102018-06-22 22:29:54 +02001433#ifdef CONFIG_CMD_WOL
1434 case PROT_WOL:
1435 wol_receive(ip, len);
1436 break;
1437#endif
Samuel Mendoza-Jonasc8f4ab02022-08-08 21:46:03 +09301438#ifdef CONFIG_PHY_NCSI
1439 case PROT_NCSI:
1440 ncsi_receive(et, ip, len);
1441 break;
1442#endif
wdenk2d966952002-10-31 22:12:35 +00001443 }
1444}
1445
wdenk2d966952002-10-31 22:12:35 +00001446/**********************************************************************/
1447
Simon Glassd6c5f552011-10-24 18:00:02 +00001448static int net_check_prereq(enum proto_t protocol)
wdenk2d966952002-10-31 22:12:35 +00001449{
1450 switch (protocol) {
wdenk05939202004-04-18 17:39:38 +00001451 /* Fall through */
Jon Loeliger54f35c22007-07-09 17:45:14 -05001452#if defined(CONFIG_CMD_PING)
wdenke6466f62003-06-05 19:27:42 +00001453 case PING:
Joe Hershberger5874dec2015-04-08 01:41:01 -05001454 if (net_ping_ip.s_addr == 0) {
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001455 puts("*** ERROR: ping address not given\n");
Luca Ceresoli5fe6de22011-05-04 02:40:45 +00001456 return 1;
wdenk05939202004-04-18 17:39:38 +00001457 }
1458 goto common;
wdenke6466f62003-06-05 19:27:42 +00001459#endif
Viacheslav Mitrofanove03c8aa2022-12-02 12:18:08 +03001460#if defined(CONFIG_CMD_PING6)
1461 case PING6:
1462 if (ip6_is_unspecified_addr(&net_ping_ip6)) {
1463 puts("*** ERROR: ping address not given\n");
1464 return 1;
1465 }
1466 goto common;
1467#endif
Robin Getz82f0d232009-07-20 14:53:54 -04001468#if defined(CONFIG_CMD_DNS)
1469 case DNS:
Joe Hershberger5874dec2015-04-08 01:41:01 -05001470 if (net_dns_server.s_addr == 0) {
Robin Getz82f0d232009-07-20 14:53:54 -04001471 puts("*** ERROR: DNS server address not given\n");
1472 return 1;
1473 }
1474 goto common;
1475#endif
Philippe Reynes6ec70bc2020-09-18 14:13:00 +02001476#if defined(CONFIG_PROT_UDP)
1477 case UDP:
1478 if (udp_prereq())
1479 return 1;
1480 goto common;
1481#endif
1482
Jon Loeliger54f35c22007-07-09 17:45:14 -05001483#if defined(CONFIG_CMD_NFS)
wdenkbe9c1cb2004-02-24 02:00:03 +00001484 case NFS:
1485#endif
Joe Hershbergerc80b41b02015-04-08 01:41:21 -05001486 /* Fall through */
Simon Glassd6c5f552011-10-24 18:00:02 +00001487 case TFTPGET:
Simon Glass6a398d22011-10-24 18:00:07 +00001488 case TFTPPUT:
Viacheslav Mitrofanov12b95ae2022-12-02 12:18:07 +03001489 if (IS_ENABLED(CONFIG_IPV6) && use_ip6) {
1490 if (!memcmp(&net_server_ip6, &net_null_addr_ip6,
1491 sizeof(struct in6_addr)) &&
1492 !strchr(net_boot_file_name, '[')) {
1493 puts("*** ERROR: `serverip6' not set\n");
1494 return 1;
1495 }
1496 } else if (net_server_ip.s_addr == 0 && !is_serverip_in_cmd()) {
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001497 puts("*** ERROR: `serverip' not set\n");
Luca Ceresoli5fe6de22011-05-04 02:40:45 +00001498 return 1;
wdenk05939202004-04-18 17:39:38 +00001499 }
Philippe Reynes2829d992020-09-18 14:13:02 +02001500#if defined(CONFIG_CMD_PING) || \
Philippe Reynes6ec70bc2020-09-18 14:13:00 +02001501 defined(CONFIG_CMD_DNS) || defined(CONFIG_PROT_UDP)
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001502common:
wdenke6466f62003-06-05 19:27:42 +00001503#endif
Simon Guinot00dceba2011-05-01 23:38:40 +00001504 /* Fall through */
wdenke6466f62003-06-05 19:27:42 +00001505
Simon Guinot00dceba2011-05-01 23:38:40 +00001506 case NETCONS:
Dmitrii Merkurev308252d2023-04-12 19:49:30 +01001507 case FASTBOOT_UDP:
1508 case FASTBOOT_TCP:
Luca Ceresoli7aa81a42011-05-17 00:03:40 +00001509 case TFTPSRV:
Viacheslav Mitrofanov12b95ae2022-12-02 12:18:07 +03001510 if (IS_ENABLED(CONFIG_IPV6) && use_ip6) {
1511 if (!memcmp(&net_link_local_ip6, &net_null_addr_ip6,
1512 sizeof(struct in6_addr))) {
1513 puts("*** ERROR: `ip6addr` not set\n");
1514 return 1;
1515 }
1516 } else if (net_ip.s_addr == 0) {
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001517 puts("*** ERROR: `ipaddr' not set\n");
Luca Ceresoli5fe6de22011-05-04 02:40:45 +00001518 return 1;
wdenk05939202004-04-18 17:39:38 +00001519 }
1520 /* Fall through */
wdenk2d966952002-10-31 22:12:35 +00001521
Peter Tyserf7a48ca2010-09-30 11:25:48 -05001522#ifdef CONFIG_CMD_RARP
wdenk2d966952002-10-31 22:12:35 +00001523 case RARP:
Peter Tyserf7a48ca2010-09-30 11:25:48 -05001524#endif
Samuel Mendoza-Jonasc8f4ab02022-08-08 21:46:03 +09301525#ifdef CONFIG_PHY_NCSI
1526 case NCSI:
1527#endif
wdenk2d966952002-10-31 22:12:35 +00001528 case BOOTP:
wdenk145d2c12004-04-15 21:48:45 +00001529 case CDP:
Peter Tyserf7a48ca2010-09-30 11:25:48 -05001530 case DHCP:
Joe Hershbergerb35a3a62012-05-23 08:00:12 +00001531 case LINKLOCAL:
Joe Hershberger8ecdbed2015-04-08 01:41:04 -05001532 if (memcmp(net_ethaddr, "\0\0\0\0\0\0", 6) == 0) {
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001533 int num = eth_get_dev_index();
wdenk2d966952002-10-31 22:12:35 +00001534
wdenk05939202004-04-18 17:39:38 +00001535 switch (num) {
1536 case -1:
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001537 puts("*** ERROR: No ethernet found.\n");
Luca Ceresoli5fe6de22011-05-04 02:40:45 +00001538 return 1;
wdenk05939202004-04-18 17:39:38 +00001539 case 0:
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001540 puts("*** ERROR: `ethaddr' not set\n");
wdenk2d966952002-10-31 22:12:35 +00001541 break;
wdenk05939202004-04-18 17:39:38 +00001542 default:
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001543 printf("*** ERROR: `eth%daddr' not set\n",
Joe Hershbergerc80b41b02015-04-08 01:41:21 -05001544 num);
wdenk2d966952002-10-31 22:12:35 +00001545 break;
wdenk05939202004-04-18 17:39:38 +00001546 }
wdenk2d966952002-10-31 22:12:35 +00001547
Joe Hershbergerc80b41b02015-04-08 01:41:21 -05001548 net_start_again();
Luca Ceresoli5fe6de22011-05-04 02:40:45 +00001549 return 2;
wdenk05939202004-04-18 17:39:38 +00001550 }
1551 /* Fall through */
1552 default:
Luca Ceresoli5fe6de22011-05-04 02:40:45 +00001553 return 0;
wdenk2d966952002-10-31 22:12:35 +00001554 }
Luca Ceresoli5fe6de22011-05-04 02:40:45 +00001555 return 0; /* OK */
wdenk2d966952002-10-31 22:12:35 +00001556}
1557/**********************************************************************/
1558
1559int
Joe Hershbergera8ca4f62015-04-08 01:41:05 -05001560net_eth_hdr_size(void)
wdenk145d2c12004-04-15 21:48:45 +00001561{
1562 ushort myvlanid;
1563
Joe Hershberger013d3872015-04-08 01:41:17 -05001564 myvlanid = ntohs(net_our_vlan);
wdenk145d2c12004-04-15 21:48:45 +00001565 if (myvlanid == (ushort)-1)
1566 myvlanid = VLAN_NONE;
wdenk2d966952002-10-31 22:12:35 +00001567
Luca Ceresoli5c83a962011-05-11 03:59:54 +00001568 return ((myvlanid & VLAN_IDMASK) == VLAN_NONE) ? ETHER_HDR_SIZE :
1569 VLAN_ETHER_HDR_SIZE;
wdenk145d2c12004-04-15 21:48:45 +00001570}
1571
Joe Hershbergera8ca4f62015-04-08 01:41:05 -05001572int net_set_ether(uchar *xet, const uchar *dest_ethaddr, uint prot)
wdenk2d966952002-10-31 22:12:35 +00001573{
Joe Hershberger1178f412012-05-23 07:58:06 +00001574 struct ethernet_hdr *et = (struct ethernet_hdr *)xet;
wdenk145d2c12004-04-15 21:48:45 +00001575 ushort myvlanid;
1576
Joe Hershberger013d3872015-04-08 01:41:17 -05001577 myvlanid = ntohs(net_our_vlan);
wdenk145d2c12004-04-15 21:48:45 +00001578 if (myvlanid == (ushort)-1)
1579 myvlanid = VLAN_NONE;
wdenk2d966952002-10-31 22:12:35 +00001580
Joe Hershberger8ecdbed2015-04-08 01:41:04 -05001581 memcpy(et->et_dest, dest_ethaddr, 6);
1582 memcpy(et->et_src, net_ethaddr, 6);
wdenk145d2c12004-04-15 21:48:45 +00001583 if ((myvlanid & VLAN_IDMASK) == VLAN_NONE) {
Luca Ceresolib19d0b72011-05-04 02:40:46 +00001584 et->et_protlen = htons(prot);
wdenk145d2c12004-04-15 21:48:45 +00001585 return ETHER_HDR_SIZE;
1586 } else {
Joe Hershbergerb43784c2012-05-23 07:58:07 +00001587 struct vlan_ethernet_hdr *vet =
1588 (struct vlan_ethernet_hdr *)xet;
wdenk2d966952002-10-31 22:12:35 +00001589
wdenk145d2c12004-04-15 21:48:45 +00001590 vet->vet_vlan_type = htons(PROT_VLAN);
1591 vet->vet_tag = htons((0 << 5) | (myvlanid & VLAN_IDMASK));
1592 vet->vet_type = htons(prot);
1593 return VLAN_ETHER_HDR_SIZE;
1594 }
1595}
wdenk2d966952002-10-31 22:12:35 +00001596
Joe Hershberger530cd6b2012-05-23 07:59:16 +00001597int net_update_ether(struct ethernet_hdr *et, uchar *addr, uint prot)
1598{
1599 ushort protlen;
1600
1601 memcpy(et->et_dest, addr, 6);
Joe Hershberger8ecdbed2015-04-08 01:41:04 -05001602 memcpy(et->et_src, net_ethaddr, 6);
Joe Hershberger530cd6b2012-05-23 07:59:16 +00001603 protlen = ntohs(et->et_protlen);
1604 if (protlen == PROT_VLAN) {
1605 struct vlan_ethernet_hdr *vet =
1606 (struct vlan_ethernet_hdr *)et;
1607 vet->vet_type = htons(prot);
1608 return VLAN_ETHER_HDR_SIZE;
1609 } else if (protlen > 1514) {
1610 et->et_protlen = htons(prot);
1611 return ETHER_HDR_SIZE;
1612 } else {
1613 /* 802.2 + SNAP */
1614 struct e802_hdr *et802 = (struct e802_hdr *)et;
1615 et802->et_prot = htons(prot);
1616 return E802_HDR_SIZE;
1617 }
1618}
1619
Duncan Haref1447c92018-06-24 15:40:41 -07001620void net_set_ip_header(uchar *pkt, struct in_addr dest, struct in_addr source,
1621 u16 pkt_len, u8 proto)
wdenk2d966952002-10-31 22:12:35 +00001622{
Joe Hershberger2ed5b492012-05-23 07:59:07 +00001623 struct ip_udp_hdr *ip = (struct ip_udp_hdr *)pkt;
wdenk2d966952002-10-31 22:12:35 +00001624
1625 /*
Joe Hershberger2ed5b492012-05-23 07:59:07 +00001626 * Construct an IP header.
wdenk2d966952002-10-31 22:12:35 +00001627 */
Luca Ceresoli5c83a962011-05-11 03:59:54 +00001628 /* IP_HDR_SIZE / 4 (not including UDP) */
1629 ip->ip_hl_v = 0x45;
wdenk2d966952002-10-31 22:12:35 +00001630 ip->ip_tos = 0;
Duncan Haref1447c92018-06-24 15:40:41 -07001631 ip->ip_len = htons(pkt_len);
1632 ip->ip_p = proto;
Joe Hershbergerc80b41b02015-04-08 01:41:21 -05001633 ip->ip_id = htons(net_ip_id++);
Peter Tyser0885bf02008-12-01 16:26:20 -06001634 ip->ip_off = htons(IP_FLAGS_DFRAG); /* Don't fragment */
wdenk2d966952002-10-31 22:12:35 +00001635 ip->ip_ttl = 255;
wdenk2d966952002-10-31 22:12:35 +00001636 ip->ip_sum = 0;
Luca Ceresoli5c83a962011-05-11 03:59:54 +00001637 /* already in network byte order */
Joe Hershberger5874dec2015-04-08 01:41:01 -05001638 net_copy_ip((void *)&ip->ip_src, &source);
Joe Hershberger2ed5b492012-05-23 07:59:07 +00001639 /* already in network byte order */
Joe Hershberger5874dec2015-04-08 01:41:01 -05001640 net_copy_ip((void *)&ip->ip_dst, &dest);
Duncan Haref1447c92018-06-24 15:40:41 -07001641
1642 ip->ip_sum = compute_ip_checksum(ip, IP_HDR_SIZE);
Joe Hershberger2ed5b492012-05-23 07:59:07 +00001643}
1644
Joe Hershberger5874dec2015-04-08 01:41:01 -05001645void net_set_udp_header(uchar *pkt, struct in_addr dest, int dport, int sport,
Joe Hershberger2ed5b492012-05-23 07:59:07 +00001646 int len)
1647{
1648 struct ip_udp_hdr *ip = (struct ip_udp_hdr *)pkt;
1649
1650 /*
1651 * If the data is an odd number of bytes, zero the
1652 * byte after the last byte so that the checksum
1653 * will work.
1654 */
1655 if (len & 1)
1656 pkt[IP_UDP_HDR_SIZE + len] = 0;
1657
Duncan Haref1447c92018-06-24 15:40:41 -07001658 net_set_ip_header(pkt, dest, net_ip, IP_UDP_HDR_SIZE + len,
1659 IPPROTO_UDP);
Joe Hershberger2ed5b492012-05-23 07:59:07 +00001660
wdenk2d966952002-10-31 22:12:35 +00001661 ip->udp_src = htons(sport);
1662 ip->udp_dst = htons(dport);
Joe Hershberger6fe8b452012-05-23 07:58:04 +00001663 ip->udp_len = htons(UDP_HDR_SIZE + len);
wdenk2d966952002-10-31 22:12:35 +00001664 ip->udp_xsum = 0;
wdenk2d966952002-10-31 22:12:35 +00001665}
1666
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001667void copy_filename(char *dst, const char *src, int size)
wdenk2d966952002-10-31 22:12:35 +00001668{
Joe Hershbergere20cadd2018-07-03 19:36:41 -05001669 if (src && *src && (*src == '"')) {
wdenk2d966952002-10-31 22:12:35 +00001670 ++src;
1671 --size;
1672 }
1673
Joe Hershbergere20cadd2018-07-03 19:36:41 -05001674 while ((--size > 0) && src && *src && (*src != '"'))
wdenk2d966952002-10-31 22:12:35 +00001675 *dst++ = *src++;
wdenk2d966952002-10-31 22:12:35 +00001676 *dst = '\0';
1677}
1678
Joe Hershbergerf559f1c2018-07-03 19:36:39 -05001679int is_serverip_in_cmd(void)
1680{
1681 return !!strchr(net_boot_file_name, ':');
1682}
1683
Joe Hershberger9cb7b022018-07-03 19:36:43 -05001684int net_parse_bootfile(struct in_addr *ipaddr, char *filename, int max_len)
1685{
1686 char *colon;
Lyle Franklinc1fad502022-04-16 11:36:43 -04001687 struct in_addr ip;
1688 ip.s_addr = 0;
Joe Hershberger9cb7b022018-07-03 19:36:43 -05001689
1690 if (net_boot_file_name[0] == '\0')
1691 return 0;
1692
1693 colon = strchr(net_boot_file_name, ':');
1694 if (colon) {
Lyle Franklinc1fad502022-04-16 11:36:43 -04001695 ip = string_to_ip(net_boot_file_name);
1696 if (ipaddr && ip.s_addr)
1697 *ipaddr = ip;
1698 }
1699 if (ip.s_addr) {
Joe Hershberger9cb7b022018-07-03 19:36:43 -05001700 strncpy(filename, colon + 1, max_len);
1701 } else {
1702 strncpy(filename, net_boot_file_name, max_len);
1703 }
1704 filename[max_len - 1] = '\0';
1705
1706 return 1;
1707}
1708
Joe Hershberger5874dec2015-04-08 01:41:01 -05001709void ip_to_string(struct in_addr x, char *s)
wdenk2d966952002-10-31 22:12:35 +00001710{
Joe Hershberger5874dec2015-04-08 01:41:01 -05001711 x.s_addr = ntohl(x.s_addr);
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001712 sprintf(s, "%d.%d.%d.%d",
Joe Hershberger5874dec2015-04-08 01:41:01 -05001713 (int) ((x.s_addr >> 24) & 0xff),
1714 (int) ((x.s_addr >> 16) & 0xff),
1715 (int) ((x.s_addr >> 8) & 0xff),
1716 (int) ((x.s_addr >> 0) & 0xff)
wdenk05939202004-04-18 17:39:38 +00001717 );
wdenk2d966952002-10-31 22:12:35 +00001718}
1719
Joe Hershberger013d3872015-04-08 01:41:17 -05001720void vlan_to_string(ushort x, char *s)
wdenk145d2c12004-04-15 21:48:45 +00001721{
1722 x = ntohs(x);
1723
1724 if (x == (ushort)-1)
1725 x = VLAN_NONE;
1726
1727 if (x == VLAN_NONE)
1728 strcpy(s, "none");
1729 else
1730 sprintf(s, "%d", x & VLAN_IDMASK);
1731}
1732
Joe Hershberger013d3872015-04-08 01:41:17 -05001733ushort string_to_vlan(const char *s)
wdenk145d2c12004-04-15 21:48:45 +00001734{
1735 ushort id;
1736
1737 if (s == NULL)
wdenk656140b2004-04-25 13:18:40 +00001738 return htons(VLAN_NONE);
wdenk145d2c12004-04-15 21:48:45 +00001739
1740 if (*s < '0' || *s > '9')
1741 id = VLAN_NONE;
1742 else
Simon Glassff9b9032021-07-24 09:03:30 -06001743 id = (ushort)dectoul(s, NULL);
wdenk145d2c12004-04-15 21:48:45 +00001744
wdenk656140b2004-04-25 13:18:40 +00001745 return htons(id);
wdenk145d2c12004-04-15 21:48:45 +00001746}
1747
Simon Glassda1a1342017-08-03 12:22:15 -06001748ushort env_get_vlan(char *var)
wdenk145d2c12004-04-15 21:48:45 +00001749{
Simon Glass64b723f2017-08-03 12:22:12 -06001750 return string_to_vlan(env_get(var));
wdenk145d2c12004-04-15 21:48:45 +00001751}