blob: c9a749f6cc82b85fa4c3da514e68b9ced1fd344d [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>
Alex Kiernand5aa57c2018-05-29 15:30:53 +000096#include <net/fastboot.h>
Lukasz Majewski21185922015-08-24 00:21:43 +020097#include <net/tftp.h>
Samuel Mendoza-Jonasc8f4ab02022-08-08 21:46:03 +093098#include <net/ncsi.h>
Ramon Friedac598c12019-07-18 21:43:30 +030099#if defined(CONFIG_CMD_PCAP)
100#include <net/pcap.h>
101#endif
Philippe Reynes6ec70bc2020-09-18 14:13:00 +0200102#include <net/udp.h>
Uri Mashiach4892d392017-01-19 10:51:45 +0200103#if defined(CONFIG_LED_STATUS)
wdenk49c3f672003-10-08 22:33:00 +0000104#include <miiphy.h>
Joe Hershbergerfee076e2012-05-23 07:58:15 +0000105#include <status_led.h>
wdenkb4ad9622005-04-01 00:25:43 +0000106#endif
Joe Hershbergerfee076e2012-05-23 07:58:15 +0000107#include <watchdog.h>
108#include <linux/compiler.h>
Simon Glass66014cc2023-01-17 10:47:27 -0700109#include <test/test.h>
Joe Hershbergerfee076e2012-05-23 07:58:15 +0000110#include "arp.h"
111#include "bootp.h"
Joe Hershbergera4215b02012-05-23 07:57:59 +0000112#include "cdp.h"
Robin Getz82f0d232009-07-20 14:53:54 -0400113#if defined(CONFIG_CMD_DNS)
114#include "dns.h"
115#endif
Joe Hershbergerb35a3a62012-05-23 08:00:12 +0000116#include "link_local.h"
Joe Hershbergerfee076e2012-05-23 07:58:15 +0000117#include "nfs.h"
Joe Hershbergerc21bf372012-05-23 07:58:02 +0000118#include "ping.h"
Joe Hershbergerfee076e2012-05-23 07:58:15 +0000119#include "rarp.h"
Lothar Felten776fc102018-06-22 22:29:54 +0200120#if defined(CONFIG_CMD_WOL)
121#include "wol.h"
122#endif
Ying-Chun Liu (PaulLiu)41efed12022-11-08 14:17:28 +0800123#include <net/tcp.h>
Ying-Chun Liu (PaulLiu)cc96a1d2022-11-08 14:17:29 +0800124#include <net/wget.h>
wdenk2d966952002-10-31 22:12:35 +0000125
wdenk2d966952002-10-31 22:12:35 +0000126/** BOOTP EXTENTIONS **/
127
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000128/* Our subnet mask (0=unknown) */
Joe Hershberger5874dec2015-04-08 01:41:01 -0500129struct in_addr net_netmask;
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000130/* Our gateways IP address */
Joe Hershberger5874dec2015-04-08 01:41:01 -0500131struct in_addr net_gateway;
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000132/* Our DNS IP address */
Joe Hershberger5874dec2015-04-08 01:41:01 -0500133struct in_addr net_dns_server;
Jon Loeliger5336a762007-07-09 22:08:34 -0500134#if defined(CONFIG_BOOTP_DNS2)
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000135/* Our 2nd DNS IP address */
Joe Hershberger5874dec2015-04-08 01:41:01 -0500136struct in_addr net_dns_server2;
stroesee0aadfb2003-08-28 14:17:32 +0000137#endif
wdenk2d966952002-10-31 22:12:35 +0000138
139/** END OF BOOTP EXTENTIONS **/
140
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000141/* Our ethernet address */
Joe Hershberger8ecdbed2015-04-08 01:41:04 -0500142u8 net_ethaddr[6];
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000143/* Boot server enet address */
Joe Hershberger8ecdbed2015-04-08 01:41:04 -0500144u8 net_server_ethaddr[6];
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000145/* Our IP addr (0 = unknown) */
Joe Hershberger5874dec2015-04-08 01:41:01 -0500146struct in_addr net_ip;
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000147/* Server IP addr (0 = unknown) */
Joe Hershberger5874dec2015-04-08 01:41:01 -0500148struct in_addr net_server_ip;
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000149/* Current receive packet */
Joe Hershbergera8ca4f62015-04-08 01:41:05 -0500150uchar *net_rx_packet;
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000151/* Current rx packet length */
Joe Hershbergera8ca4f62015-04-08 01:41:05 -0500152int net_rx_packet_len;
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000153/* IP packet ID */
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500154static unsigned net_ip_id;
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000155/* Ethernet bcast address */
Joe Hershberger8ecdbed2015-04-08 01:41:04 -0500156const u8 net_bcast_ethaddr[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
157const u8 net_null_ethaddr[6];
Alexander Graf94c4b992016-05-06 21:01:01 +0200158#if defined(CONFIG_API) || defined(CONFIG_EFI_LOADER)
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500159void (*push_packet)(void *, int len) = 0;
Rafal Jaworowski1f2c9a42007-12-27 18:19:02 +0100160#endif
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000161/* Network loop state */
Joe Hershbergerd4bb76a2012-05-23 07:59:14 +0000162enum net_loop_state net_state;
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000163/* Tried all network devices */
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500164int net_restart_wrap;
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000165/* Network loop restarted */
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500166static int net_restarted;
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000167/* At least one device configured */
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500168static int net_dev_exists;
wdenk2d966952002-10-31 22:12:35 +0000169
wdenk05939202004-04-18 17:39:38 +0000170/* XXX in both little & big endian machines 0xFFFF == ntohs(-1) */
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000171/* default is without VLAN */
Joe Hershberger013d3872015-04-08 01:41:17 -0500172ushort net_our_vlan = 0xFFFF;
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000173/* ditto */
Joe Hershberger013d3872015-04-08 01:41:17 -0500174ushort net_native_vlan = 0xFFFF;
wdenk145d2c12004-04-15 21:48:45 +0000175
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000176/* Boot File name */
Jacob Stifflerae80c2e2015-09-30 10:12:05 -0400177char net_boot_file_name[1024];
Alexander Graff43bf5d2018-06-15 10:29:27 +0200178/* Indicates whether the file name was specified on the command line */
179bool net_boot_file_name_explicit;
Joe Hershberger290c8992015-04-08 01:41:02 -0500180/* The actual transferred size of the bootfile (in bytes) */
181u32 net_boot_file_size;
182/* Boot file size in blocks as reported by the DHCP server */
183u32 net_boot_file_expected_size_in_blocks;
wdenk2d966952002-10-31 22:12:35 +0000184
Joe Hershbergera8ca4f62015-04-08 01:41:05 -0500185static uchar net_pkt_buf[(PKTBUFSRX+1) * PKTSIZE_ALIGN + PKTALIGN];
Joe Hershbergerf77abc52015-03-22 17:09:11 -0500186/* Receive packets */
187uchar *net_rx_packets[PKTBUFSRX];
Joe Hershbergerf50357b2012-05-23 07:59:15 +0000188/* Current UDP RX packet handler */
189static rxhand_f *udp_packet_handler;
190/* Current ARP RX packet handler */
191static rxhand_f *arp_packet_handler;
Simon Glass2928cd82011-10-26 14:18:38 +0000192#ifdef CONFIG_CMD_TFTPPUT
Joe Hershbergerf50357b2012-05-23 07:59:15 +0000193/* Current ICMP rx handler */
194static rxhand_icmp_f *packet_icmp_handler;
Simon Glass2928cd82011-10-26 14:18:38 +0000195#endif
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000196/* Current timeout handler */
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500197static thand_f *time_handler;
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000198/* Time base value */
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500199static ulong time_start;
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000200/* Current timeout value */
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500201static ulong time_delta;
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000202/* THE transmit packet */
Joe Hershbergera8ca4f62015-04-08 01:41:05 -0500203uchar *net_tx_packet;
wdenk2d966952002-10-31 22:12:35 +0000204
Simon Glassd6c5f552011-10-24 18:00:02 +0000205static int net_check_prereq(enum proto_t protocol);
wdenk2d966952002-10-31 22:12:35 +0000206
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500207static int net_try_count;
Remy Bohmer0ddb8e82009-10-28 22:13:39 +0100208
Jim Lin6c8921f2013-08-13 19:03:05 +0800209int __maybe_unused net_busy_flag;
210
wdenk2d966952002-10-31 22:12:35 +0000211/**********************************************************************/
wdenke6466f62003-06-05 19:27:42 +0000212
Joe Hershberger875b6bf2015-05-20 14:27:23 -0500213static int on_ipaddr(const char *name, const char *value, enum env_op op,
214 int flags)
215{
216 if (flags & H_PROGRAMMATIC)
217 return 0;
218
219 net_ip = string_to_ip(value);
220
221 return 0;
222}
223U_BOOT_ENV_CALLBACK(ipaddr, on_ipaddr);
224
225static int on_gatewayip(const char *name, const char *value, enum env_op op,
226 int flags)
227{
228 if (flags & H_PROGRAMMATIC)
229 return 0;
230
231 net_gateway = string_to_ip(value);
232
233 return 0;
234}
235U_BOOT_ENV_CALLBACK(gatewayip, on_gatewayip);
236
237static int on_netmask(const char *name, const char *value, enum env_op op,
238 int flags)
239{
240 if (flags & H_PROGRAMMATIC)
241 return 0;
242
243 net_netmask = string_to_ip(value);
244
245 return 0;
246}
247U_BOOT_ENV_CALLBACK(netmask, on_netmask);
248
249static int on_serverip(const char *name, const char *value, enum env_op op,
250 int flags)
251{
252 if (flags & H_PROGRAMMATIC)
253 return 0;
254
255 net_server_ip = string_to_ip(value);
256
257 return 0;
258}
259U_BOOT_ENV_CALLBACK(serverip, on_serverip);
260
261static int on_nvlan(const char *name, const char *value, enum env_op op,
262 int flags)
263{
264 if (flags & H_PROGRAMMATIC)
265 return 0;
266
267 net_native_vlan = string_to_vlan(value);
268
269 return 0;
270}
271U_BOOT_ENV_CALLBACK(nvlan, on_nvlan);
272
273static int on_vlan(const char *name, const char *value, enum env_op op,
274 int flags)
275{
276 if (flags & H_PROGRAMMATIC)
277 return 0;
278
279 net_our_vlan = string_to_vlan(value);
280
281 return 0;
282}
283U_BOOT_ENV_CALLBACK(vlan, on_vlan);
284
285#if defined(CONFIG_CMD_DNS)
286static int on_dnsip(const char *name, const char *value, enum env_op op,
287 int flags)
288{
289 if (flags & H_PROGRAMMATIC)
290 return 0;
291
292 net_dns_server = string_to_ip(value);
293
294 return 0;
295}
296U_BOOT_ENV_CALLBACK(dnsip, on_dnsip);
297#endif
298
Simon Glass5234ad12011-10-27 06:24:32 +0000299/*
300 * Check if autoload is enabled. If so, use either NFS or TFTP to download
301 * the boot file.
302 */
303void net_auto_load(void)
304{
Tom Rini57256302019-12-05 19:35:07 -0500305#if defined(CONFIG_CMD_NFS) && !defined(CONFIG_SPL_BUILD)
Simon Glass64b723f2017-08-03 12:22:12 -0600306 const char *s = env_get("autoload");
Simon Glass5234ad12011-10-27 06:24:32 +0000307
Joe Hershberger864ec562012-12-11 22:16:22 -0600308 if (s != NULL && strcmp(s, "NFS") == 0) {
Joe Hershberger40aa2082018-07-03 19:36:40 -0500309 if (net_check_prereq(NFS)) {
310/* We aren't expecting to get a serverip, so just accept the assigned IP */
Simon Glass297fc202021-12-18 11:27:52 -0700311 if (IS_ENABLED(CONFIG_BOOTP_SERVERIP)) {
312 net_set_state(NETLOOP_SUCCESS);
313 } else {
314 printf("Cannot autoload with NFS\n");
315 net_set_state(NETLOOP_FAIL);
316 }
Joe Hershberger40aa2082018-07-03 19:36:40 -0500317 return;
318 }
Joe Hershberger864ec562012-12-11 22:16:22 -0600319 /*
320 * Use NFS to load the bootfile.
321 */
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500322 nfs_start();
Joe Hershberger864ec562012-12-11 22:16:22 -0600323 return;
324 }
Simon Glass5234ad12011-10-27 06:24:32 +0000325#endif
Simon Glass22c34c22017-08-03 12:22:13 -0600326 if (env_get_yesno("autoload") == 0) {
Joe Hershberger864ec562012-12-11 22:16:22 -0600327 /*
328 * Just use BOOTP/RARP to configure system;
329 * Do not use TFTP to load the bootfile.
330 */
331 net_set_state(NETLOOP_SUCCESS);
332 return;
Simon Glass5234ad12011-10-27 06:24:32 +0000333 }
Joe Hershberger40aa2082018-07-03 19:36:40 -0500334 if (net_check_prereq(TFTPGET)) {
335/* We aren't expecting to get a serverip, so just accept the assigned IP */
Simon Glass297fc202021-12-18 11:27:52 -0700336 if (IS_ENABLED(CONFIG_BOOTP_SERVERIP)) {
337 net_set_state(NETLOOP_SUCCESS);
338 } else {
339 printf("Cannot autoload with TFTPGET\n");
340 net_set_state(NETLOOP_FAIL);
341 }
Joe Hershberger40aa2082018-07-03 19:36:40 -0500342 return;
343 }
Joe Hershberger64701592015-04-08 01:41:07 -0500344 tftp_start(TFTPGET);
Simon Glass5234ad12011-10-27 06:24:32 +0000345}
346
Sean Anderson35e82982020-09-12 17:45:43 -0400347static int net_init_loop(void)
Heiko Schocher0c303fa2009-02-10 09:38:52 +0100348{
Viacheslav Mitrofanovda019102022-12-02 12:18:06 +0300349 if (eth_get_dev()) {
Joe Hershberger8ecdbed2015-04-08 01:41:04 -0500350 memcpy(net_ethaddr, eth_get_ethaddr(), 6);
Viacheslav Mitrofanovda019102022-12-02 12:18:06 +0300351
352 if (IS_ENABLED(CONFIG_IPV6)) {
353 ip6_make_lladdr(&net_link_local_ip6, net_ethaddr);
354 if (!memcmp(&net_ip6, &net_null_addr_ip6,
355 sizeof(struct in6_addr)))
356 memcpy(&net_ip6, &net_link_local_ip6,
357 sizeof(struct in6_addr));
358 }
359 }
Sean Anderson35e82982020-09-12 17:45:43 -0400360 else
361 /*
362 * Not ideal, but there's no way to get the actual error, and I
363 * don't feel like fixing all the users of eth_get_dev to deal
364 * with errors.
365 */
366 return -ENONET;
Michael Zaidmanb97bfe42009-04-04 01:43:00 +0300367
Sean Anderson35e82982020-09-12 17:45:43 -0400368 return 0;
Heiko Schocher0c303fa2009-02-10 09:38:52 +0100369}
370
Joe Hershbergerf50357b2012-05-23 07:59:15 +0000371static void net_clear_handlers(void)
372{
373 net_set_udp_handler(NULL);
374 net_set_arp_handler(NULL);
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500375 net_set_timeout_handler(0, NULL);
Joe Hershbergerf50357b2012-05-23 07:59:15 +0000376}
377
378static void net_cleanup_loop(void)
379{
380 net_clear_handlers();
381}
382
Sean Anderson35e82982020-09-12 17:45:43 -0400383int net_init(void)
Joe Hershberger017e5c42012-05-23 07:59:22 +0000384{
385 static int first_call = 1;
386
387 if (first_call) {
388 /*
389 * Setup packet buffers, aligned correctly.
390 */
391 int i;
392
Joe Hershbergera8ca4f62015-04-08 01:41:05 -0500393 net_tx_packet = &net_pkt_buf[0] + (PKTALIGN - 1);
394 net_tx_packet -= (ulong)net_tx_packet % PKTALIGN;
Joe Hershbergerf77abc52015-03-22 17:09:11 -0500395 for (i = 0; i < PKTBUFSRX; i++) {
Joe Hershbergera8ca4f62015-04-08 01:41:05 -0500396 net_rx_packets[i] = net_tx_packet +
397 (i + 1) * PKTSIZE_ALIGN;
Joe Hershbergerf77abc52015-03-22 17:09:11 -0500398 }
Joe Hershberger85ae7762015-04-08 01:41:08 -0500399 arp_init();
Viacheslav Mitrofanovda019102022-12-02 12:18:06 +0300400 ndisc_init();
Joe Hershberger017e5c42012-05-23 07:59:22 +0000401 net_clear_handlers();
402
403 /* Only need to setup buffer pointers once. */
404 first_call = 0;
Ying-Chun Liu (PaulLiu)41efed12022-11-08 14:17:28 +0800405 if (IS_ENABLED(CONFIG_PROT_TCP))
406 tcp_set_tcp_state(TCP_CLOSED);
Joe Hershberger017e5c42012-05-23 07:59:22 +0000407 }
408
Sean Anderson35e82982020-09-12 17:45:43 -0400409 return net_init_loop();
Joe Hershberger017e5c42012-05-23 07:59:22 +0000410}
411
wdenke6466f62003-06-05 19:27:42 +0000412/**********************************************************************/
wdenk2d966952002-10-31 22:12:35 +0000413/*
414 * Main network processing loop.
415 */
416
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500417int net_loop(enum proto_t protocol)
wdenk2d966952002-10-31 22:12:35 +0000418{
Joe Hershbergerf4dc96a2015-03-22 17:09:24 -0500419 int ret = -EINVAL;
Leonid Iziumtsev4b58b052018-05-08 15:55:50 +0200420 enum net_loop_state prev_net_state = net_state;
wdenk2d966952002-10-31 22:12:35 +0000421
Marek Szyprowskib9df7d92020-06-15 11:15:57 +0200422#if defined(CONFIG_CMD_PING)
423 if (protocol != PING)
424 net_ping_ip.s_addr = 0;
425#endif
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500426 net_restarted = 0;
427 net_dev_exists = 0;
428 net_try_count = 1;
429 debug_cond(DEBUG_INT_STATE, "--- net_loop Entry\n");
wdenke6466f62003-06-05 19:27:42 +0000430
Samuel Mendoza-Jonasc8f4ab02022-08-08 21:46:03 +0930431#ifdef CONFIG_PHY_NCSI
432 if (phy_interface_is_ncsi() && protocol != NCSI && !ncsi_active()) {
433 printf("%s: configuring NCSI first\n", __func__);
434 if (net_loop(NCSI) < 0)
435 return ret;
436 eth_init_state_only();
437 goto restart;
438 }
439#endif
440
Simon Glass768cbf02011-12-10 11:08:06 +0000441 bootstage_mark_name(BOOTSTAGE_ID_ETH_START, "eth_start");
Joe Hershberger017e5c42012-05-23 07:59:22 +0000442 net_init();
Yang Liu7c78e2f2020-12-21 14:44:39 +1100443 if (eth_is_on_demand_init()) {
wdenkfa66e932005-04-03 14:52:59 +0000444 eth_halt();
Joe Hershberger9f374062012-08-03 10:59:08 +0000445 eth_set_current();
Joe Hershbergerf4dc96a2015-03-22 17:09:24 -0500446 ret = eth_init();
447 if (ret < 0) {
Joe Hershberger9f374062012-08-03 10:59:08 +0000448 eth_halt();
Joe Hershbergerf4dc96a2015-03-22 17:09:24 -0500449 return ret;
Joe Hershberger9f374062012-08-03 10:59:08 +0000450 }
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500451 } else {
Joe Hershberger3dbe17e2015-03-22 17:09:06 -0500452 eth_init_state_only();
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500453 }
Samuel Mendoza-Jonasfeebd6c2022-08-08 21:46:04 +0930454
wdenk2d966952002-10-31 22:12:35 +0000455restart:
Jim Lin6c8921f2013-08-13 19:03:05 +0800456#ifdef CONFIG_USB_KEYBOARD
457 net_busy_flag = 0;
458#endif
Joe Hershbergerd4bb76a2012-05-23 07:59:14 +0000459 net_set_state(NETLOOP_CONTINUE);
wdenk2d966952002-10-31 22:12:35 +0000460
461 /*
462 * Start the ball rolling with the given start function. From
463 * here on, this code is a state machine driven by received
464 * packets and timer events.
465 */
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500466 debug_cond(DEBUG_INT_STATE, "--- net_loop Init\n");
467 net_init_loop();
wdenk2d966952002-10-31 22:12:35 +0000468
Simon Glass66014cc2023-01-17 10:47:27 -0700469 if (!test_eth_enabled())
470 return 0;
471
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000472 switch (net_check_prereq(protocol)) {
wdenk2d966952002-10-31 22:12:35 +0000473 case 1:
474 /* network not configured */
wdenkfa66e932005-04-03 14:52:59 +0000475 eth_halt();
Leonid Iziumtsev4b58b052018-05-08 15:55:50 +0200476 net_set_state(prev_net_state);
Joe Hershbergerf4dc96a2015-03-22 17:09:24 -0500477 return -ENODEV;
wdenk2d966952002-10-31 22:12:35 +0000478
wdenk2d966952002-10-31 22:12:35 +0000479 case 2:
480 /* network device not configured */
481 break;
wdenk2d966952002-10-31 22:12:35 +0000482
483 case 0:
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500484 net_dev_exists = 1;
Joe Hershberger290c8992015-04-08 01:41:02 -0500485 net_boot_file_size = 0;
wdenk2d966952002-10-31 22:12:35 +0000486 switch (protocol) {
Krebs, Olafd9249382020-03-09 14:27:55 +0000487#ifdef CONFIG_CMD_TFTPBOOT
Simon Glassd6c5f552011-10-24 18:00:02 +0000488 case TFTPGET:
Simon Glass6a398d22011-10-24 18:00:07 +0000489#ifdef CONFIG_CMD_TFTPPUT
490 case TFTPPUT:
491#endif
wdenk2d966952002-10-31 22:12:35 +0000492 /* always use ARP to get server ethernet address */
Joe Hershberger64701592015-04-08 01:41:07 -0500493 tftp_start(protocol);
wdenk2d966952002-10-31 22:12:35 +0000494 break;
Krebs, Olafd9249382020-03-09 14:27:55 +0000495#endif
Luca Ceresoli7aa81a42011-05-17 00:03:40 +0000496#ifdef CONFIG_CMD_TFTPSRV
497 case TFTPSRV:
Joe Hershberger64701592015-04-08 01:41:07 -0500498 tftp_start_server();
Luca Ceresoli7aa81a42011-05-17 00:03:40 +0000499 break;
500#endif
Alex Kiernand5aa57c2018-05-29 15:30:53 +0000501#ifdef CONFIG_UDP_FUNCTION_FASTBOOT
502 case FASTBOOT:
503 fastboot_start_server();
504 break;
505#endif
Jon Loeliger54f35c22007-07-09 17:45:14 -0500506#if defined(CONFIG_CMD_DHCP)
wdenk2d966952002-10-31 22:12:35 +0000507 case DHCP:
Joe Hershberger2fe81a52015-04-08 01:41:09 -0500508 bootp_reset();
Joe Hershberger5874dec2015-04-08 01:41:01 -0500509 net_ip.s_addr = 0;
Joe Hershberger2fe81a52015-04-08 01:41:09 -0500510 dhcp_request(); /* Basically same as BOOTP */
wdenk2d966952002-10-31 22:12:35 +0000511 break;
Jon Loeligera9807e52007-07-10 11:05:02 -0500512#endif
Krebs, Olafd9249382020-03-09 14:27:55 +0000513#if defined(CONFIG_CMD_BOOTP)
wdenk2d966952002-10-31 22:12:35 +0000514 case BOOTP:
Joe Hershberger2fe81a52015-04-08 01:41:09 -0500515 bootp_reset();
Joe Hershberger5874dec2015-04-08 01:41:01 -0500516 net_ip.s_addr = 0;
Joe Hershberger2fe81a52015-04-08 01:41:09 -0500517 bootp_request();
wdenk2d966952002-10-31 22:12:35 +0000518 break;
Krebs, Olafd9249382020-03-09 14:27:55 +0000519#endif
Peter Tyserf7a48ca2010-09-30 11:25:48 -0500520#if defined(CONFIG_CMD_RARP)
wdenk2d966952002-10-31 22:12:35 +0000521 case RARP:
Joe Hershberger8e805bb2015-04-08 01:41:11 -0500522 rarp_try = 0;
Joe Hershberger5874dec2015-04-08 01:41:01 -0500523 net_ip.s_addr = 0;
Joe Hershberger8e805bb2015-04-08 01:41:11 -0500524 rarp_request();
wdenk2d966952002-10-31 22:12:35 +0000525 break;
Peter Tyserf7a48ca2010-09-30 11:25:48 -0500526#endif
Jon Loeliger54f35c22007-07-09 17:45:14 -0500527#if defined(CONFIG_CMD_PING)
wdenke6466f62003-06-05 19:27:42 +0000528 case PING:
Joe Hershbergerc21bf372012-05-23 07:58:02 +0000529 ping_start();
wdenke6466f62003-06-05 19:27:42 +0000530 break;
531#endif
Viacheslav Mitrofanove03c8aa2022-12-02 12:18:08 +0300532#if defined(CONFIG_CMD_PING6)
533 case PING6:
534 ping6_start();
535 break;
536#endif
Tom Rini57256302019-12-05 19:35:07 -0500537#if defined(CONFIG_CMD_NFS) && !defined(CONFIG_SPL_BUILD)
wdenkbe9c1cb2004-02-24 02:00:03 +0000538 case NFS:
Joe Hershberger40d7ca92015-04-08 01:41:10 -0500539 nfs_start();
wdenkbe9c1cb2004-02-24 02:00:03 +0000540 break;
541#endif
Ying-Chun Liu (PaulLiu)cc96a1d2022-11-08 14:17:29 +0800542#if defined(CONFIG_CMD_WGET)
543 case WGET:
544 wget_start();
545 break;
546#endif
Jon Loeliger54f35c22007-07-09 17:45:14 -0500547#if defined(CONFIG_CMD_CDP)
wdenk145d2c12004-04-15 21:48:45 +0000548 case CDP:
Joe Hershberger527336f2015-04-08 01:41:14 -0500549 cdp_start();
wdenk145d2c12004-04-15 21:48:45 +0000550 break;
551#endif
Holger Denglerae85c072017-07-20 10:10:55 +0200552#if defined(CONFIG_NETCONSOLE) && !defined(CONFIG_SPL_BUILD)
wdenkb8fb6192004-08-02 21:11:11 +0000553 case NETCONS:
Joe Hershbergerd02aa6b2015-04-08 01:41:16 -0500554 nc_start();
wdenkb8fb6192004-08-02 21:11:11 +0000555 break;
556#endif
Robin Getz82f0d232009-07-20 14:53:54 -0400557#if defined(CONFIG_CMD_DNS)
558 case DNS:
Joe Hershbergerf725e342015-04-08 01:41:15 -0500559 dns_start();
Robin Getz82f0d232009-07-20 14:53:54 -0400560 break;
561#endif
Joe Hershbergerb35a3a62012-05-23 08:00:12 +0000562#if defined(CONFIG_CMD_LINK_LOCAL)
563 case LINKLOCAL:
564 link_local_start();
565 break;
566#endif
Lothar Felten776fc102018-06-22 22:29:54 +0200567#if defined(CONFIG_CMD_WOL)
568 case WOL:
569 wol_start();
570 break;
571#endif
Samuel Mendoza-Jonasc8f4ab02022-08-08 21:46:03 +0930572#if defined(CONFIG_PHY_NCSI)
573 case NCSI:
574 ncsi_probe_packages();
575 break;
576#endif
wdenk2d966952002-10-31 22:12:35 +0000577 default:
578 break;
579 }
580
Philippe Reynes6ec70bc2020-09-18 14:13:00 +0200581 if (IS_ENABLED(CONFIG_PROT_UDP) && protocol == UDP)
582 udp_start();
583
wdenk2d966952002-10-31 22:12:35 +0000584 break;
585 }
586
Jon Loeliger54f35c22007-07-09 17:45:14 -0500587#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
Luca Ceresoli5c83a962011-05-11 03:59:54 +0000588#if defined(CONFIG_SYS_FAULT_ECHO_LINK_DOWN) && \
Uri Mashiach4892d392017-01-19 10:51:45 +0200589 defined(CONFIG_LED_STATUS) && \
590 defined(CONFIG_LED_STATUS_RED)
wdenk49c3f672003-10-08 22:33:00 +0000591 /*
wdenk9c53f402003-10-15 23:53:47 +0000592 * Echo the inverted link state to the fault LED.
wdenk49c3f672003-10-08 22:33:00 +0000593 */
Luca Ceresolie8ccbb02011-05-04 02:40:43 +0000594 if (miiphy_link(eth_get_dev()->name, CONFIG_SYS_FAULT_MII_ADDR))
Uri Mashiach4892d392017-01-19 10:51:45 +0200595 status_led_set(CONFIG_LED_STATUS_RED, CONFIG_LED_STATUS_OFF);
Luca Ceresolie8ccbb02011-05-04 02:40:43 +0000596 else
Uri Mashiach4892d392017-01-19 10:51:45 +0200597 status_led_set(CONFIG_LED_STATUS_RED, CONFIG_LED_STATUS_ON);
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200598#endif /* CONFIG_SYS_FAULT_ECHO_LINK_DOWN, ... */
wdenk49c3f672003-10-08 22:33:00 +0000599#endif /* CONFIG_MII, ... */
Jim Lin6c8921f2013-08-13 19:03:05 +0800600#ifdef CONFIG_USB_KEYBOARD
601 net_busy_flag = 1;
602#endif
wdenk2d966952002-10-31 22:12:35 +0000603
604 /*
605 * Main packet reception loop. Loop receiving packets until
Joe Hershbergerd4bb76a2012-05-23 07:59:14 +0000606 * someone sets `net_state' to a state that terminates.
wdenk2d966952002-10-31 22:12:35 +0000607 */
608 for (;;) {
Stefan Roese80877fa2022-09-02 14:10:46 +0200609 schedule();
Joe Hershbergerd6978a42015-12-21 16:31:35 -0600610 if (arp_timeout_check() > 0)
611 time_start = get_timer(0);
612
Viacheslav Mitrofanovda019102022-12-02 12:18:06 +0300613 if (IS_ENABLED(CONFIG_IPV6)) {
614 if (use_ip6 && (ndisc_timeout_check() > 0))
615 time_start = get_timer(0);
616 }
617
wdenk2d966952002-10-31 22:12:35 +0000618 /*
619 * Check the ethernet for a new packet. The ethernet
620 * receive routine will process it.
Joe Hershbergerf4dc96a2015-03-22 17:09:24 -0500621 * Most drivers return the most recent packet size, but not
622 * errors that may have happened.
wdenk2d966952002-10-31 22:12:35 +0000623 */
Guennadi Liakhovetskib38c2b32008-04-03 17:04:19 +0200624 eth_rx();
wdenk2d966952002-10-31 22:12:35 +0000625
626 /*
627 * Abort if ctrl-c was pressed.
628 */
629 if (ctrlc()) {
Joe Hershbergerde8205a2012-05-23 07:59:24 +0000630 /* cancel any ARP that may not have completed */
Joe Hershberger5874dec2015-04-08 01:41:01 -0500631 net_arp_wait_packet_ip.s_addr = 0;
Joe Hershbergerde8205a2012-05-23 07:59:24 +0000632
Joe Hershbergerf50357b2012-05-23 07:59:15 +0000633 net_cleanup_loop();
wdenk57b2d802003-06-27 21:31:46 +0000634 eth_halt();
Joe Hershberger9f374062012-08-03 10:59:08 +0000635 /* Invalidate the last protocol */
636 eth_set_last_protocol(BOOTP);
637
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000638 puts("\nAbort\n");
Joe Hershberger05a377b2012-05-23 08:01:04 +0000639 /* include a debug print as well incase the debug
640 messages are directed to stderr */
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500641 debug_cond(DEBUG_INT_STATE, "--- net_loop Abort!\n");
Michal Simekafd31602015-08-21 08:49:48 +0200642 ret = -EINTR;
Simon Glass230467c2011-10-24 18:00:01 +0000643 goto done;
wdenk2d966952002-10-31 22:12:35 +0000644 }
645
wdenk2d966952002-10-31 22:12:35 +0000646 /*
647 * Check for a timeout, and run the timeout handler
648 * if we have one.
649 */
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500650 if (time_handler &&
651 ((get_timer(0) - time_start) > time_delta)) {
wdenk2d966952002-10-31 22:12:35 +0000652 thand_f *x;
653
Jon Loeliger54f35c22007-07-09 17:45:14 -0500654#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000655#if defined(CONFIG_SYS_FAULT_ECHO_LINK_DOWN) && \
Uri Mashiach4892d392017-01-19 10:51:45 +0200656 defined(CONFIG_LED_STATUS) && \
657 defined(CONFIG_LED_STATUS_RED)
wdenk49c3f672003-10-08 22:33:00 +0000658 /*
wdenk9c53f402003-10-15 23:53:47 +0000659 * Echo the inverted link state to the fault LED.
wdenk49c3f672003-10-08 22:33:00 +0000660 */
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000661 if (miiphy_link(eth_get_dev()->name,
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500662 CONFIG_SYS_FAULT_MII_ADDR))
Uri Mashiach4892d392017-01-19 10:51:45 +0200663 status_led_set(CONFIG_LED_STATUS_RED,
664 CONFIG_LED_STATUS_OFF);
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500665 else
Uri Mashiach4892d392017-01-19 10:51:45 +0200666 status_led_set(CONFIG_LED_STATUS_RED,
667 CONFIG_LED_STATUS_ON);
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000668#endif /* CONFIG_SYS_FAULT_ECHO_LINK_DOWN, ... */
wdenk49c3f672003-10-08 22:33:00 +0000669#endif /* CONFIG_MII, ... */
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500670 debug_cond(DEBUG_INT_STATE, "--- net_loop timeout\n");
671 x = time_handler;
672 time_handler = (thand_f *)0;
wdenk2d966952002-10-31 22:12:35 +0000673 (*x)();
674 }
675
Joe Hershbergere44a0ea2015-03-22 17:09:07 -0500676 if (net_state == NETLOOP_FAIL)
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500677 ret = net_start_again();
wdenk2d966952002-10-31 22:12:35 +0000678
Joe Hershbergerd4bb76a2012-05-23 07:59:14 +0000679 switch (net_state) {
wdenk2d966952002-10-31 22:12:35 +0000680 case NETLOOP_RESTART:
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500681 net_restarted = 1;
wdenk2d966952002-10-31 22:12:35 +0000682 goto restart;
683
684 case NETLOOP_SUCCESS:
Joe Hershbergerf50357b2012-05-23 07:59:15 +0000685 net_cleanup_loop();
Joe Hershberger290c8992015-04-08 01:41:02 -0500686 if (net_boot_file_size > 0) {
687 printf("Bytes transferred = %d (%x hex)\n",
688 net_boot_file_size, net_boot_file_size);
Simon Glass4d949a22017-08-03 12:22:10 -0600689 env_set_hex("filesize", net_boot_file_size);
Simon Glass892265d2019-12-28 10:45:02 -0700690 env_set_hex("fileaddr", image_load_addr);
wdenk2d966952002-10-31 22:12:35 +0000691 }
Samuel Mendoza-Jonasc8f4ab02022-08-08 21:46:03 +0930692 if (protocol != NETCONS && protocol != NCSI)
Joe Hershberger9f374062012-08-03 10:59:08 +0000693 eth_halt();
694 else
695 eth_halt_state_only();
696
697 eth_set_last_protocol(protocol);
698
Joe Hershberger290c8992015-04-08 01:41:02 -0500699 ret = net_boot_file_size;
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500700 debug_cond(DEBUG_INT_STATE, "--- net_loop Success!\n");
Simon Glass230467c2011-10-24 18:00:01 +0000701 goto done;
wdenk2d966952002-10-31 22:12:35 +0000702
703 case NETLOOP_FAIL:
Joe Hershbergerf50357b2012-05-23 07:59:15 +0000704 net_cleanup_loop();
Joe Hershberger9f374062012-08-03 10:59:08 +0000705 /* Invalidate the last protocol */
706 eth_set_last_protocol(BOOTP);
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500707 debug_cond(DEBUG_INT_STATE, "--- net_loop Fail!\n");
Thomas RIENOESSL2dee4192018-11-21 15:56:07 +0100708 ret = -ENONET;
Simon Glass230467c2011-10-24 18:00:01 +0000709 goto done;
Joe Hershbergerd4bb76a2012-05-23 07:59:14 +0000710
711 case NETLOOP_CONTINUE:
712 continue;
wdenk2d966952002-10-31 22:12:35 +0000713 }
714 }
Simon Glass230467c2011-10-24 18:00:01 +0000715
716done:
Jim Lin6c8921f2013-08-13 19:03:05 +0800717#ifdef CONFIG_USB_KEYBOARD
718 net_busy_flag = 0;
719#endif
Simon Glass2928cd82011-10-26 14:18:38 +0000720#ifdef CONFIG_CMD_TFTPPUT
Simon Glass230467c2011-10-24 18:00:01 +0000721 /* Clear out the handlers */
Joe Hershbergerf50357b2012-05-23 07:59:15 +0000722 net_set_udp_handler(NULL);
Simon Glass230467c2011-10-24 18:00:01 +0000723 net_set_icmp_handler(NULL);
Simon Glass2928cd82011-10-26 14:18:38 +0000724#endif
Leonid Iziumtsev4b58b052018-05-08 15:55:50 +0200725 net_set_state(prev_net_state);
Ramon Friedac598c12019-07-18 21:43:30 +0300726
727#if defined(CONFIG_CMD_PCAP)
728 if (pcap_active())
729 pcap_print_status();
730#endif
Simon Glass230467c2011-10-24 18:00:01 +0000731 return ret;
wdenk2d966952002-10-31 22:12:35 +0000732}
733
734/**********************************************************************/
735
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500736static void start_again_timeout_handler(void)
wdenk2d966952002-10-31 22:12:35 +0000737{
Joe Hershbergerd4bb76a2012-05-23 07:59:14 +0000738 net_set_state(NETLOOP_RESTART);
wdenk2d966952002-10-31 22:12:35 +0000739}
740
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500741int net_start_again(void)
wdenk2d966952002-10-31 22:12:35 +0000742{
wdenk05939202004-04-18 17:39:38 +0000743 char *nretry;
Remy Bohmer0ddb8e82009-10-28 22:13:39 +0100744 int retry_forever = 0;
745 unsigned long retrycnt = 0;
Joe Hershbergerf4dc96a2015-03-22 17:09:24 -0500746 int ret;
wdenk145d2c12004-04-15 21:48:45 +0000747
Simon Glass64b723f2017-08-03 12:22:12 -0600748 nretry = env_get("netretry");
Remy Bohmer0ddb8e82009-10-28 22:13:39 +0100749 if (nretry) {
750 if (!strcmp(nretry, "yes"))
751 retry_forever = 1;
752 else if (!strcmp(nretry, "no"))
753 retrycnt = 0;
754 else if (!strcmp(nretry, "once"))
755 retrycnt = 1;
756 else
757 retrycnt = simple_strtoul(nretry, NULL, 0);
Joe Hershbergere44a0ea2015-03-22 17:09:07 -0500758 } else {
759 retrycnt = 0;
760 retry_forever = 0;
761 }
Remy Bohmer0ddb8e82009-10-28 22:13:39 +0100762
Leonid Iziumtsevfb7c94c2018-03-09 15:29:06 +0100763 if ((!retry_forever) && (net_try_count > retrycnt)) {
Remy Bohmer0ddb8e82009-10-28 22:13:39 +0100764 eth_halt();
Joe Hershbergerd4bb76a2012-05-23 07:59:14 +0000765 net_set_state(NETLOOP_FAIL);
Joe Hershbergerf4dc96a2015-03-22 17:09:24 -0500766 /*
767 * We don't provide a way for the protocol to return an error,
768 * but this is almost always the reason.
769 */
770 return -ETIMEDOUT;
wdenk145d2c12004-04-15 21:48:45 +0000771 }
Remy Bohmer0ddb8e82009-10-28 22:13:39 +0100772
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500773 net_try_count++;
Remy Bohmer0ddb8e82009-10-28 22:13:39 +0100774
Luca Ceresoli7cbd7482011-05-11 03:59:56 +0000775 eth_halt();
Matthias Fuchsa02d62b2007-12-27 16:58:41 +0100776#if !defined(CONFIG_NET_DO_NOT_TRY_ANOTHER)
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500777 eth_try_another(!net_restarted);
Matthias Fuchsa02d62b2007-12-27 16:58:41 +0100778#endif
Joe Hershbergerf4dc96a2015-03-22 17:09:24 -0500779 ret = eth_init();
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500780 if (net_restart_wrap) {
781 net_restart_wrap = 0;
782 if (net_dev_exists) {
783 net_set_timeout_handler(10000UL,
784 start_again_timeout_handler);
Joe Hershbergerf50357b2012-05-23 07:59:15 +0000785 net_set_udp_handler(NULL);
wdenk05939202004-04-18 17:39:38 +0000786 } else {
Joe Hershbergerd4bb76a2012-05-23 07:59:14 +0000787 net_set_state(NETLOOP_FAIL);
wdenk2d966952002-10-31 22:12:35 +0000788 }
wdenk05939202004-04-18 17:39:38 +0000789 } else {
Joe Hershbergerd4bb76a2012-05-23 07:59:14 +0000790 net_set_state(NETLOOP_RESTART);
wdenk2d966952002-10-31 22:12:35 +0000791 }
Joe Hershbergerf4dc96a2015-03-22 17:09:24 -0500792 return ret;
wdenk2d966952002-10-31 22:12:35 +0000793}
794
795/**********************************************************************/
796/*
797 * Miscelaneous bits.
798 */
799
Joe Hershbergerf50357b2012-05-23 07:59:15 +0000800static void dummy_handler(uchar *pkt, unsigned dport,
Joe Hershberger5874dec2015-04-08 01:41:01 -0500801 struct in_addr sip, unsigned sport,
Joe Hershbergerf50357b2012-05-23 07:59:15 +0000802 unsigned len)
Joe Hershbergeraae05082012-05-23 07:58:01 +0000803{
Joe Hershbergeraae05082012-05-23 07:58:01 +0000804}
805
Joe Hershbergerf50357b2012-05-23 07:59:15 +0000806rxhand_f *net_get_udp_handler(void)
807{
808 return udp_packet_handler;
809}
Joe Hershbergeraae05082012-05-23 07:58:01 +0000810
Joe Hershbergerf50357b2012-05-23 07:59:15 +0000811void net_set_udp_handler(rxhand_f *f)
812{
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500813 debug_cond(DEBUG_INT_STATE, "--- net_loop UDP handler set (%p)\n", f);
Joe Hershbergerf50357b2012-05-23 07:59:15 +0000814 if (f == NULL)
815 udp_packet_handler = dummy_handler;
816 else
817 udp_packet_handler = f;
818}
819
820rxhand_f *net_get_arp_handler(void)
wdenk2d966952002-10-31 22:12:35 +0000821{
Joe Hershbergerf50357b2012-05-23 07:59:15 +0000822 return arp_packet_handler;
wdenk2d966952002-10-31 22:12:35 +0000823}
824
Joe Hershbergerf50357b2012-05-23 07:59:15 +0000825void net_set_arp_handler(rxhand_f *f)
826{
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500827 debug_cond(DEBUG_INT_STATE, "--- net_loop ARP handler set (%p)\n", f);
Joe Hershbergerf50357b2012-05-23 07:59:15 +0000828 if (f == NULL)
829 arp_packet_handler = dummy_handler;
830 else
831 arp_packet_handler = f;
832}
833
Simon Glass2928cd82011-10-26 14:18:38 +0000834#ifdef CONFIG_CMD_TFTPPUT
Simon Glass230467c2011-10-24 18:00:01 +0000835void net_set_icmp_handler(rxhand_icmp_f *f)
836{
837 packet_icmp_handler = f;
838}
Simon Glass2928cd82011-10-26 14:18:38 +0000839#endif
wdenk2d966952002-10-31 22:12:35 +0000840
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500841void net_set_timeout_handler(ulong iv, thand_f *f)
wdenk2d966952002-10-31 22:12:35 +0000842{
843 if (iv == 0) {
Joe Hershberger05a377b2012-05-23 08:01:04 +0000844 debug_cond(DEBUG_INT_STATE,
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500845 "--- net_loop timeout handler cancelled\n");
846 time_handler = (thand_f *)0;
wdenk2d966952002-10-31 22:12:35 +0000847 } else {
Joe Hershberger05a377b2012-05-23 08:01:04 +0000848 debug_cond(DEBUG_INT_STATE,
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500849 "--- net_loop timeout handler set (%p)\n", f);
850 time_handler = f;
851 time_start = get_timer(0);
852 time_delta = iv * CONFIG_SYS_HZ / 1000;
wdenk2d966952002-10-31 22:12:35 +0000853 }
854}
855
Joe Hershbergere79a5182018-09-26 16:49:02 -0500856uchar *net_get_async_tx_pkt_buf(void)
857{
858 if (arp_is_waiting())
859 return arp_tx_packet; /* If we are waiting, we already sent */
860 else
861 return net_tx_packet;
862}
863
Joe Hershbergera8ca4f62015-04-08 01:41:05 -0500864int net_send_udp_packet(uchar *ether, struct in_addr dest, int dport, int sport,
Joe Hershberger1a6b8d82012-05-23 07:58:10 +0000865 int payload_len)
wdenke6466f62003-06-05 19:27:42 +0000866{
Duncan Haref1447c92018-06-24 15:40:41 -0700867 return net_send_ip_packet(ether, dest, dport, sport, payload_len,
868 IPPROTO_UDP, 0, 0, 0);
869}
870
Ying-Chun Liu (PaulLiu)41efed12022-11-08 14:17:28 +0800871#if defined(CONFIG_PROT_TCP)
872int net_send_tcp_packet(int payload_len, int dport, int sport, u8 action,
873 u32 tcp_seq_num, u32 tcp_ack_num)
874{
875 return net_send_ip_packet(net_server_ethaddr, net_server_ip, dport,
876 sport, payload_len, IPPROTO_TCP, action,
877 tcp_seq_num, tcp_ack_num);
878}
879#endif
880
Duncan Haref1447c92018-06-24 15:40:41 -0700881int net_send_ip_packet(uchar *ether, struct in_addr dest, int dport, int sport,
882 int payload_len, int proto, u8 action, u32 tcp_seq_num,
883 u32 tcp_ack_num)
884{
wdenk145d2c12004-04-15 21:48:45 +0000885 uchar *pkt;
Joe Hershberger16be9cb2012-05-23 07:59:08 +0000886 int eth_hdr_size;
887 int pkt_hdr_size;
wdenk145d2c12004-04-15 21:48:45 +0000888
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500889 /* make sure the net_tx_packet is initialized (net_init() was called) */
Joe Hershbergera8ca4f62015-04-08 01:41:05 -0500890 assert(net_tx_packet != NULL);
891 if (net_tx_packet == NULL)
Joe Hershberger017e5c42012-05-23 07:59:22 +0000892 return -1;
893
wdenke6466f62003-06-05 19:27:42 +0000894 /* convert to new style broadcast */
Joe Hershberger5874dec2015-04-08 01:41:01 -0500895 if (dest.s_addr == 0)
896 dest.s_addr = 0xFFFFFFFF;
wdenke6466f62003-06-05 19:27:42 +0000897
898 /* if broadcast, make the ether address a broadcast and don't do ARP */
Joe Hershberger5874dec2015-04-08 01:41:01 -0500899 if (dest.s_addr == 0xFFFFFFFF)
Joe Hershberger8ecdbed2015-04-08 01:41:04 -0500900 ether = (uchar *)net_bcast_ethaddr;
wdenke6466f62003-06-05 19:27:42 +0000901
Joe Hershbergera8ca4f62015-04-08 01:41:05 -0500902 pkt = (uchar *)net_tx_packet;
Joe Hershberger16be9cb2012-05-23 07:59:08 +0000903
Joe Hershbergera8ca4f62015-04-08 01:41:05 -0500904 eth_hdr_size = net_set_ether(pkt, ether, PROT_IP);
Duncan Haref1447c92018-06-24 15:40:41 -0700905
906 switch (proto) {
907 case IPPROTO_UDP:
908 net_set_udp_header(pkt + eth_hdr_size, dest, dport, sport,
909 payload_len);
910 pkt_hdr_size = eth_hdr_size + IP_UDP_HDR_SIZE;
911 break;
Ying-Chun Liu (PaulLiu)41efed12022-11-08 14:17:28 +0800912#if defined(CONFIG_PROT_TCP)
913 case IPPROTO_TCP:
914 pkt_hdr_size = eth_hdr_size
915 + tcp_set_tcp_header(pkt + eth_hdr_size, dport, sport,
916 payload_len, action, tcp_seq_num,
917 tcp_ack_num);
918 break;
919#endif
Duncan Haref1447c92018-06-24 15:40:41 -0700920 default:
921 return -EINVAL;
922 }
wdenke6466f62003-06-05 19:27:42 +0000923
Joe Hershbergerde8205a2012-05-23 07:59:24 +0000924 /* if MAC address was not discovered yet, do an ARP request */
Joe Hershberger8ecdbed2015-04-08 01:41:04 -0500925 if (memcmp(ether, net_null_ethaddr, 6) == 0) {
Joe Hershberger05a377b2012-05-23 08:01:04 +0000926 debug_cond(DEBUG_DEV_PKT, "sending ARP for %pI4\n", &dest);
Robin Getz9e0a4d62009-07-23 03:01:03 -0400927
Joe Hershberger16be9cb2012-05-23 07:59:08 +0000928 /* save the ip and eth addr for the packet to send after arp */
Joe Hershberger5874dec2015-04-08 01:41:01 -0500929 net_arp_wait_packet_ip = dest;
Joe Hershberger85ae7762015-04-08 01:41:08 -0500930 arp_wait_packet_ethaddr = ether;
wdenk145d2c12004-04-15 21:48:45 +0000931
wdenke6466f62003-06-05 19:27:42 +0000932 /* size of the waiting packet */
Joe Hershberger85ae7762015-04-08 01:41:08 -0500933 arp_wait_tx_packet_size = pkt_hdr_size + payload_len;
wdenke6466f62003-06-05 19:27:42 +0000934
935 /* and do the ARP request */
Joe Hershberger85ae7762015-04-08 01:41:08 -0500936 arp_wait_try = 1;
937 arp_wait_timer_start = get_timer(0);
938 arp_request();
wdenke6466f62003-06-05 19:27:42 +0000939 return 1; /* waiting */
Joe Hershberger16be9cb2012-05-23 07:59:08 +0000940 } else {
Joe Hershberger05a377b2012-05-23 08:01:04 +0000941 debug_cond(DEBUG_DEV_PKT, "sending UDP to %pI4/%pM\n",
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500942 &dest, ether);
Joe Hershbergera8ca4f62015-04-08 01:41:05 -0500943 net_send_packet(net_tx_packet, pkt_hdr_size + payload_len);
Joe Hershberger16be9cb2012-05-23 07:59:08 +0000944 return 0; /* transmitted */
wdenke6466f62003-06-05 19:27:42 +0000945 }
wdenke6466f62003-06-05 19:27:42 +0000946}
wdenk145d2c12004-04-15 21:48:45 +0000947
Alessandro Rubinif119e3d2009-08-07 13:58:56 +0200948#ifdef CONFIG_IP_DEFRAG
949/*
950 * This function collects fragments in a single packet, according
951 * to the algorithm in RFC815. It returns NULL or the pointer to
952 * a complete packet, in static storage
953 */
Joe Hershberger9d390302016-08-15 14:42:15 -0500954#define IP_PKTSIZE (CONFIG_NET_MAXDEFRAG)
Alessandro Rubinif119e3d2009-08-07 13:58:56 +0200955
Joe Hershbergerc686fa12012-05-23 07:58:05 +0000956#define IP_MAXUDP (IP_PKTSIZE - IP_HDR_SIZE)
Alessandro Rubinif119e3d2009-08-07 13:58:56 +0200957
958/*
959 * this is the packet being assembled, either data or frag control.
960 * Fragments go by 8 bytes, so this union must be 8 bytes long
961 */
962struct hole {
963 /* first_byte is address of this structure */
964 u16 last_byte; /* last byte in this hole + 1 (begin of next hole) */
965 u16 next_hole; /* index of next (in 8-b blocks), 0 == none */
966 u16 prev_hole; /* index of prev, 0 == none */
967 u16 unused;
968};
969
Joe Hershbergerc80b41b02015-04-08 01:41:21 -0500970static struct ip_udp_hdr *__net_defragment(struct ip_udp_hdr *ip, int *lenp)
Alessandro Rubinif119e3d2009-08-07 13:58:56 +0200971{
Joe Hershberger77001b432012-05-15 08:59:08 +0000972 static uchar pkt_buff[IP_PKTSIZE] __aligned(PKTALIGN);
Alessandro Rubinif119e3d2009-08-07 13:58:56 +0200973 static u16 first_hole, total_len;
974 struct hole *payload, *thisfrag, *h, *newh;
Joe Hershberger6fe8b452012-05-23 07:58:04 +0000975 struct ip_udp_hdr *localip = (struct ip_udp_hdr *)pkt_buff;
Alessandro Rubinif119e3d2009-08-07 13:58:56 +0200976 uchar *indata = (uchar *)ip;
977 int offset8, start, len, done = 0;
978 u16 ip_off = ntohs(ip->ip_off);
979
Rasmus Villemoes547e7e62022-10-14 19:43:39 +0200980 /*
981 * Calling code already rejected <, but we don't have to deal
982 * with an IP fragment with no payload.
983 */
984 if (ntohs(ip->ip_len) <= IP_HDR_SIZE)
Fabio Estevama0aaa322022-05-26 11:14:37 -0300985 return NULL;
986
Alessandro Rubinif119e3d2009-08-07 13:58:56 +0200987 /* payload starts after IP header, this fragment is in there */
Joe Hershbergerc686fa12012-05-23 07:58:05 +0000988 payload = (struct hole *)(pkt_buff + IP_HDR_SIZE);
Alessandro Rubinif119e3d2009-08-07 13:58:56 +0200989 offset8 = (ip_off & IP_OFFS);
990 thisfrag = payload + offset8;
991 start = offset8 * 8;
Joe Hershbergerc686fa12012-05-23 07:58:05 +0000992 len = ntohs(ip->ip_len) - IP_HDR_SIZE;
Alessandro Rubinif119e3d2009-08-07 13:58:56 +0200993
Rasmus Villemoes547e7e62022-10-14 19:43:39 +0200994 /* All but last fragment must have a multiple-of-8 payload. */
995 if ((len & 7) && (ip_off & IP_FLAGS_MFRAG))
996 return NULL;
997
Alessandro Rubinif119e3d2009-08-07 13:58:56 +0200998 if (start + len > IP_MAXUDP) /* fragment extends too far */
999 return NULL;
1000
1001 if (!total_len || localip->ip_id != ip->ip_id) {
1002 /* new (or different) packet, reset structs */
1003 total_len = 0xffff;
1004 payload[0].last_byte = ~0;
1005 payload[0].next_hole = 0;
1006 payload[0].prev_hole = 0;
1007 first_hole = 0;
1008 /* any IP header will work, copy the first we received */
Joe Hershbergerc686fa12012-05-23 07:58:05 +00001009 memcpy(localip, ip, IP_HDR_SIZE);
Alessandro Rubinif119e3d2009-08-07 13:58:56 +02001010 }
1011
1012 /*
1013 * What follows is the reassembly algorithm. We use the payload
1014 * array as a linked list of hole descriptors, as each hole starts
1015 * at a multiple of 8 bytes. However, last byte can be whatever value,
1016 * so it is represented as byte count, not as 8-byte blocks.
1017 */
1018
1019 h = payload + first_hole;
1020 while (h->last_byte < start) {
1021 if (!h->next_hole) {
1022 /* no hole that far away */
1023 return NULL;
1024 }
1025 h = payload + h->next_hole;
1026 }
1027
Fillod Stephanee7ade8b2010-06-11 19:26:43 +02001028 /* last fragment may be 1..7 bytes, the "+7" forces acceptance */
1029 if (offset8 + ((len + 7) / 8) <= h - payload) {
Alessandro Rubinif119e3d2009-08-07 13:58:56 +02001030 /* no overlap with holes (dup fragment?) */
1031 return NULL;
1032 }
1033
1034 if (!(ip_off & IP_FLAGS_MFRAG)) {
1035 /* no more fragmentss: truncate this (last) hole */
1036 total_len = start + len;
1037 h->last_byte = start + len;
1038 }
1039
1040 /*
Rasmus Villemoes366587d2022-10-17 09:52:51 +02001041 * There is some overlap: fix the hole list. This code deals
1042 * with a fragment that overlaps with two different holes
1043 * (thus being a superset of a previously-received fragment)
1044 * by only using the part of the fragment that fits in the
1045 * first hole.
Alessandro Rubinif119e3d2009-08-07 13:58:56 +02001046 */
Rasmus Villemoes366587d2022-10-17 09:52:51 +02001047 if (h->last_byte < start + len)
1048 len = h->last_byte - start;
Alessandro Rubinif119e3d2009-08-07 13:58:56 +02001049
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001050 if ((h >= thisfrag) && (h->last_byte <= start + len)) {
Alessandro Rubinif119e3d2009-08-07 13:58:56 +02001051 /* complete overlap with hole: remove hole */
1052 if (!h->prev_hole && !h->next_hole) {
1053 /* last remaining hole */
1054 done = 1;
1055 } else if (!h->prev_hole) {
1056 /* first hole */
1057 first_hole = h->next_hole;
1058 payload[h->next_hole].prev_hole = 0;
1059 } else if (!h->next_hole) {
1060 /* last hole */
1061 payload[h->prev_hole].next_hole = 0;
1062 } else {
1063 /* in the middle of the list */
1064 payload[h->next_hole].prev_hole = h->prev_hole;
1065 payload[h->prev_hole].next_hole = h->next_hole;
1066 }
1067
1068 } else if (h->last_byte <= start + len) {
1069 /* overlaps with final part of the hole: shorten this hole */
1070 h->last_byte = start;
1071
1072 } else if (h >= thisfrag) {
1073 /* overlaps with initial part of the hole: move this hole */
1074 newh = thisfrag + (len / 8);
1075 *newh = *h;
1076 h = newh;
1077 if (h->next_hole)
1078 payload[h->next_hole].prev_hole = (h - payload);
1079 if (h->prev_hole)
1080 payload[h->prev_hole].next_hole = (h - payload);
1081 else
1082 first_hole = (h - payload);
1083
1084 } else {
1085 /* fragment sits in the middle: split the hole */
1086 newh = thisfrag + (len / 8);
1087 *newh = *h;
1088 h->last_byte = start;
1089 h->next_hole = (newh - payload);
1090 newh->prev_hole = (h - payload);
1091 if (newh->next_hole)
1092 payload[newh->next_hole].prev_hole = (newh - payload);
1093 }
1094
1095 /* finally copy this fragment and possibly return whole packet */
Joe Hershbergerc686fa12012-05-23 07:58:05 +00001096 memcpy((uchar *)thisfrag, indata + IP_HDR_SIZE, len);
Alessandro Rubinif119e3d2009-08-07 13:58:56 +02001097 if (!done)
1098 return NULL;
1099
Joe Hershbergerc686fa12012-05-23 07:58:05 +00001100 *lenp = total_len + IP_HDR_SIZE;
Rasmus Villemoes932d4842022-10-14 19:43:40 +02001101 localip->ip_len = htons(*lenp);
Alessandro Rubinif119e3d2009-08-07 13:58:56 +02001102 return localip;
1103}
1104
Joe Hershbergerc80b41b02015-04-08 01:41:21 -05001105static inline struct ip_udp_hdr *net_defragment(struct ip_udp_hdr *ip,
1106 int *lenp)
Alessandro Rubinif119e3d2009-08-07 13:58:56 +02001107{
1108 u16 ip_off = ntohs(ip->ip_off);
1109 if (!(ip_off & (IP_OFFS | IP_FLAGS_MFRAG)))
1110 return ip; /* not a fragment */
Joe Hershbergerc80b41b02015-04-08 01:41:21 -05001111 return __net_defragment(ip, lenp);
Alessandro Rubinif119e3d2009-08-07 13:58:56 +02001112}
1113
1114#else /* !CONFIG_IP_DEFRAG */
1115
Joe Hershbergerc80b41b02015-04-08 01:41:21 -05001116static inline struct ip_udp_hdr *net_defragment(struct ip_udp_hdr *ip,
1117 int *lenp)
Alessandro Rubinif119e3d2009-08-07 13:58:56 +02001118{
1119 u16 ip_off = ntohs(ip->ip_off);
1120 if (!(ip_off & (IP_OFFS | IP_FLAGS_MFRAG)))
1121 return ip; /* not a fragment */
1122 return NULL;
1123}
1124#endif
wdenk2d966952002-10-31 22:12:35 +00001125
Simon Glass43c72962011-10-24 18:00:00 +00001126/**
1127 * Receive an ICMP packet. We deal with REDIRECT and PING here, and silently
1128 * drop others.
1129 *
1130 * @parma ip IP packet containing the ICMP
1131 */
Joe Hershberger6fe8b452012-05-23 07:58:04 +00001132static void receive_icmp(struct ip_udp_hdr *ip, int len,
Joe Hershberger5874dec2015-04-08 01:41:01 -05001133 struct in_addr src_ip, struct ethernet_hdr *et)
Simon Glass43c72962011-10-24 18:00:00 +00001134{
Joe Hershberger78495612012-05-23 07:58:09 +00001135 struct icmp_hdr *icmph = (struct icmp_hdr *)&ip->udp_src;
Simon Glass43c72962011-10-24 18:00:00 +00001136
1137 switch (icmph->type) {
1138 case ICMP_REDIRECT:
1139 if (icmph->code != ICMP_REDIR_HOST)
1140 return;
1141 printf(" ICMP Host Redirect to %pI4 ",
Joe Hershbergerc80b41b02015-04-08 01:41:21 -05001142 &icmph->un.gateway);
Simon Glass43c72962011-10-24 18:00:00 +00001143 break;
Joe Hershbergerc21bf372012-05-23 07:58:02 +00001144 default:
Simon Glass43c72962011-10-24 18:00:00 +00001145#if defined(CONFIG_CMD_PING)
Joe Hershbergerc21bf372012-05-23 07:58:02 +00001146 ping_receive(et, ip, len);
Simon Glass43c72962011-10-24 18:00:00 +00001147#endif
Simon Glass2928cd82011-10-26 14:18:38 +00001148#ifdef CONFIG_CMD_TFTPPUT
Simon Glass230467c2011-10-24 18:00:01 +00001149 if (packet_icmp_handler)
1150 packet_icmp_handler(icmph->type, icmph->code,
Joe Hershbergerc80b41b02015-04-08 01:41:21 -05001151 ntohs(ip->udp_dst), src_ip,
1152 ntohs(ip->udp_src), icmph->un.data,
1153 ntohs(ip->udp_len));
Simon Glass2928cd82011-10-26 14:18:38 +00001154#endif
Simon Glass43c72962011-10-24 18:00:00 +00001155 break;
1156 }
1157}
1158
Joe Hershbergerf77abc52015-03-22 17:09:11 -05001159void net_process_received_packet(uchar *in_packet, int len)
wdenk2d966952002-10-31 22:12:35 +00001160{
Joe Hershberger1178f412012-05-23 07:58:06 +00001161 struct ethernet_hdr *et;
Joe Hershberger6fe8b452012-05-23 07:58:04 +00001162 struct ip_udp_hdr *ip;
Joe Hershberger5874dec2015-04-08 01:41:01 -05001163 struct in_addr dst_ip;
1164 struct in_addr src_ip;
Joe Hershbergerfacf5352012-05-23 07:58:12 +00001165 int eth_proto;
Jon Loeliger54f35c22007-07-09 17:45:14 -05001166#if defined(CONFIG_CMD_CDP)
wdenk145d2c12004-04-15 21:48:45 +00001167 int iscdp;
1168#endif
1169 ushort cti = 0, vlanid = VLAN_NONE, myvlanid, mynvlanid;
wdenk2d966952002-10-31 22:12:35 +00001170
Joe Hershberger05a377b2012-05-23 08:01:04 +00001171 debug_cond(DEBUG_NET_PKT, "packet received\n");
wdenk145d2c12004-04-15 21:48:45 +00001172
Ramon Friedac598c12019-07-18 21:43:30 +03001173#if defined(CONFIG_CMD_PCAP)
1174 pcap_post(in_packet, len, false);
1175#endif
Joe Hershbergera8ca4f62015-04-08 01:41:05 -05001176 net_rx_packet = in_packet;
1177 net_rx_packet_len = len;
Joe Hershbergerf77abc52015-03-22 17:09:11 -05001178 et = (struct ethernet_hdr *)in_packet;
wdenk145d2c12004-04-15 21:48:45 +00001179
1180 /* too small packet? */
1181 if (len < ETHER_HDR_SIZE)
1182 return;
Rafal Jaworowski1f2c9a42007-12-27 18:19:02 +01001183
Alexander Graf94c4b992016-05-06 21:01:01 +02001184#if defined(CONFIG_API) || defined(CONFIG_EFI_LOADER)
Rafal Jaworowski1f2c9a42007-12-27 18:19:02 +01001185 if (push_packet) {
Joe Hershbergerf77abc52015-03-22 17:09:11 -05001186 (*push_packet)(in_packet, len);
Rafal Jaworowski1f2c9a42007-12-27 18:19:02 +01001187 return;
1188 }
1189#endif
wdenk145d2c12004-04-15 21:48:45 +00001190
Jon Loeliger54f35c22007-07-09 17:45:14 -05001191#if defined(CONFIG_CMD_CDP)
wdenk145d2c12004-04-15 21:48:45 +00001192 /* keep track if packet is CDP */
Joe Hershberger00c62a82012-05-23 07:58:00 +00001193 iscdp = is_cdp_packet(et->et_dest);
wdenk145d2c12004-04-15 21:48:45 +00001194#endif
1195
Joe Hershberger013d3872015-04-08 01:41:17 -05001196 myvlanid = ntohs(net_our_vlan);
wdenk145d2c12004-04-15 21:48:45 +00001197 if (myvlanid == (ushort)-1)
1198 myvlanid = VLAN_NONE;
Joe Hershberger013d3872015-04-08 01:41:17 -05001199 mynvlanid = ntohs(net_native_vlan);
wdenk145d2c12004-04-15 21:48:45 +00001200 if (mynvlanid == (ushort)-1)
1201 mynvlanid = VLAN_NONE;
wdenk2d966952002-10-31 22:12:35 +00001202
Joe Hershbergerfacf5352012-05-23 07:58:12 +00001203 eth_proto = ntohs(et->et_protlen);
wdenk2d966952002-10-31 22:12:35 +00001204
Joe Hershbergerfacf5352012-05-23 07:58:12 +00001205 if (eth_proto < 1514) {
Joe Hershberger1178f412012-05-23 07:58:06 +00001206 struct e802_hdr *et802 = (struct e802_hdr *)et;
wdenk2d966952002-10-31 22:12:35 +00001207 /*
Joe Hershbergerc17fa982012-05-23 07:58:11 +00001208 * Got a 802.2 packet. Check the other protocol field.
1209 * XXX VLAN over 802.2+SNAP not implemented!
wdenk2d966952002-10-31 22:12:35 +00001210 */
Joe Hershbergerfacf5352012-05-23 07:58:12 +00001211 eth_proto = ntohs(et802->et_prot);
wdenk145d2c12004-04-15 21:48:45 +00001212
Joe Hershbergerf77abc52015-03-22 17:09:11 -05001213 ip = (struct ip_udp_hdr *)(in_packet + E802_HDR_SIZE);
wdenk2d966952002-10-31 22:12:35 +00001214 len -= E802_HDR_SIZE;
wdenk145d2c12004-04-15 21:48:45 +00001215
Joe Hershbergerfacf5352012-05-23 07:58:12 +00001216 } else if (eth_proto != PROT_VLAN) { /* normal packet */
Joe Hershbergerf77abc52015-03-22 17:09:11 -05001217 ip = (struct ip_udp_hdr *)(in_packet + ETHER_HDR_SIZE);
wdenk2d966952002-10-31 22:12:35 +00001218 len -= ETHER_HDR_SIZE;
wdenk145d2c12004-04-15 21:48:45 +00001219
1220 } else { /* VLAN packet */
Joe Hershbergerb43784c2012-05-23 07:58:07 +00001221 struct vlan_ethernet_hdr *vet =
1222 (struct vlan_ethernet_hdr *)et;
wdenk145d2c12004-04-15 21:48:45 +00001223
Joe Hershberger05a377b2012-05-23 08:01:04 +00001224 debug_cond(DEBUG_NET_PKT, "VLAN packet received\n");
Robin Getz9e0a4d62009-07-23 03:01:03 -04001225
wdenk145d2c12004-04-15 21:48:45 +00001226 /* too small packet? */
1227 if (len < VLAN_ETHER_HDR_SIZE)
1228 return;
1229
1230 /* if no VLAN active */
Joe Hershberger013d3872015-04-08 01:41:17 -05001231 if ((ntohs(net_our_vlan) & VLAN_IDMASK) == VLAN_NONE
Jon Loeliger54f35c22007-07-09 17:45:14 -05001232#if defined(CONFIG_CMD_CDP)
wdenk145d2c12004-04-15 21:48:45 +00001233 && iscdp == 0
1234#endif
1235 )
1236 return;
1237
1238 cti = ntohs(vet->vet_tag);
1239 vlanid = cti & VLAN_IDMASK;
Joe Hershbergerfacf5352012-05-23 07:58:12 +00001240 eth_proto = ntohs(vet->vet_type);
wdenk145d2c12004-04-15 21:48:45 +00001241
Joe Hershbergerf77abc52015-03-22 17:09:11 -05001242 ip = (struct ip_udp_hdr *)(in_packet + VLAN_ETHER_HDR_SIZE);
wdenk145d2c12004-04-15 21:48:45 +00001243 len -= VLAN_ETHER_HDR_SIZE;
wdenk2d966952002-10-31 22:12:35 +00001244 }
1245
Joe Hershberger05a377b2012-05-23 08:01:04 +00001246 debug_cond(DEBUG_NET_PKT, "Receive from protocol 0x%x\n", eth_proto);
wdenk2d966952002-10-31 22:12:35 +00001247
Jon Loeliger54f35c22007-07-09 17:45:14 -05001248#if defined(CONFIG_CMD_CDP)
wdenk145d2c12004-04-15 21:48:45 +00001249 if (iscdp) {
Joe Hershbergerd01a7a02012-05-23 07:58:13 +00001250 cdp_receive((uchar *)ip, len);
wdenk145d2c12004-04-15 21:48:45 +00001251 return;
1252 }
1253#endif
1254
1255 if ((myvlanid & VLAN_IDMASK) != VLAN_NONE) {
1256 if (vlanid == VLAN_NONE)
1257 vlanid = (mynvlanid & VLAN_IDMASK);
1258 /* not matched? */
1259 if (vlanid != (myvlanid & VLAN_IDMASK))
1260 return;
1261 }
1262
Joe Hershbergerfacf5352012-05-23 07:58:12 +00001263 switch (eth_proto) {
wdenk2d966952002-10-31 22:12:35 +00001264 case PROT_ARP:
Joe Hershberger85ae7762015-04-08 01:41:08 -05001265 arp_receive(et, ip, len);
wdenkcb99da52005-01-12 00:15:14 +00001266 break;
wdenk2d966952002-10-31 22:12:35 +00001267
Peter Tyserf7a48ca2010-09-30 11:25:48 -05001268#ifdef CONFIG_CMD_RARP
wdenk2d966952002-10-31 22:12:35 +00001269 case PROT_RARP:
Joe Hershberger61b4de62012-05-23 07:58:03 +00001270 rarp_receive(ip, len);
wdenk2d966952002-10-31 22:12:35 +00001271 break;
Peter Tyserf7a48ca2010-09-30 11:25:48 -05001272#endif
Viacheslav Mitrofanovda019102022-12-02 12:18:06 +03001273#if IS_ENABLED(CONFIG_IPV6)
1274 case PROT_IP6:
1275 net_ip6_handler(et, (struct ip6_hdr *)ip, len);
Viacheslav Mitrofanova1218172022-12-06 10:08:16 +03001276 break;
Viacheslav Mitrofanovda019102022-12-02 12:18:06 +03001277#endif
wdenk2d966952002-10-31 22:12:35 +00001278 case PROT_IP:
Joe Hershberger05a377b2012-05-23 08:01:04 +00001279 debug_cond(DEBUG_NET_PKT, "Got IP\n");
Alessandro Rubinif119e3d2009-08-07 13:58:56 +02001280 /* Before we start poking the header, make sure it is there */
Rasmus Villemoes0007cba2022-10-14 19:43:38 +02001281 if (len < IP_HDR_SIZE) {
Joe Hershberger6fe8b452012-05-23 07:58:04 +00001282 debug("len bad %d < %lu\n", len,
Rasmus Villemoes0007cba2022-10-14 19:43:38 +02001283 (ulong)IP_HDR_SIZE);
wdenk2d966952002-10-31 22:12:35 +00001284 return;
1285 }
Alessandro Rubinif119e3d2009-08-07 13:58:56 +02001286 /* Check the packet length */
wdenk2d966952002-10-31 22:12:35 +00001287 if (len < ntohs(ip->ip_len)) {
Joe Hershberger05a377b2012-05-23 08:01:04 +00001288 debug("len bad %d < %d\n", len, ntohs(ip->ip_len));
wdenk2d966952002-10-31 22:12:35 +00001289 return;
1290 }
1291 len = ntohs(ip->ip_len);
Rasmus Villemoes0007cba2022-10-14 19:43:38 +02001292 if (len < IP_HDR_SIZE) {
1293 debug("bad ip->ip_len %d < %d\n", len, (int)IP_HDR_SIZE);
1294 return;
1295 }
Joe Hershberger05a377b2012-05-23 08:01:04 +00001296 debug_cond(DEBUG_NET_PKT, "len=%d, v=%02x\n",
Joe Hershbergerc80b41b02015-04-08 01:41:21 -05001297 len, ip->ip_hl_v & 0xff);
Robin Getz9e0a4d62009-07-23 03:01:03 -04001298
Alessandro Rubinif119e3d2009-08-07 13:58:56 +02001299 /* Can't deal with anything except IPv4 */
Luca Ceresolie8ccbb02011-05-04 02:40:43 +00001300 if ((ip->ip_hl_v & 0xf0) != 0x40)
wdenk2d966952002-10-31 22:12:35 +00001301 return;
Alessandro Rubinif119e3d2009-08-07 13:58:56 +02001302 /* Can't deal with IP options (headers != 20 bytes) */
Rasmus Villemoes9ac2c5c2022-10-14 19:43:37 +02001303 if ((ip->ip_hl_v & 0x0f) != 0x05)
Remy Bohmerb9535782008-06-03 15:48:17 +02001304 return;
Alessandro Rubinif119e3d2009-08-07 13:58:56 +02001305 /* Check the Checksum of the header */
Simon Glassdfcdcee2015-01-19 22:16:08 -07001306 if (!ip_checksum_ok((uchar *)ip, IP_HDR_SIZE)) {
Joe Hershberger05a377b2012-05-23 08:01:04 +00001307 debug("checksum bad\n");
wdenk2d966952002-10-31 22:12:35 +00001308 return;
1309 }
Alessandro Rubinif119e3d2009-08-07 13:58:56 +02001310 /* If it is not for us, ignore it */
Joe Hershberger5874dec2015-04-08 01:41:01 -05001311 dst_ip = net_read_ip(&ip->ip_dst);
1312 if (net_ip.s_addr && dst_ip.s_addr != net_ip.s_addr &&
1313 dst_ip.s_addr != 0xFFFFFFFF) {
Luca Ceresolib19d0b72011-05-04 02:40:46 +00001314 return;
wdenk2d966952002-10-31 22:12:35 +00001315 }
Luca Ceresoli428ab362011-04-18 06:19:50 +00001316 /* Read source IP address for later use */
Joe Hershberger5874dec2015-04-08 01:41:01 -05001317 src_ip = net_read_ip(&ip->ip_src);
wdenk2d966952002-10-31 22:12:35 +00001318 /*
Alessandro Rubinif119e3d2009-08-07 13:58:56 +02001319 * The function returns the unchanged packet if it's not
1320 * a fragment, and either the complete packet or NULL if
1321 * it is a fragment (if !CONFIG_IP_DEFRAG, it returns NULL)
1322 */
Joe Hershbergerc80b41b02015-04-08 01:41:21 -05001323 ip = net_defragment(ip, &len);
Luca Ceresolidb210822011-05-04 02:40:47 +00001324 if (!ip)
Alessandro Rubinif119e3d2009-08-07 13:58:56 +02001325 return;
1326 /*
wdenk2d966952002-10-31 22:12:35 +00001327 * watch for ICMP host redirects
1328 *
wdenk57b2d802003-06-27 21:31:46 +00001329 * There is no real handler code (yet). We just watch
1330 * for ICMP host redirect messages. In case anybody
1331 * sees these messages: please contact me
1332 * (wd@denx.de), or - even better - send me the
1333 * necessary fixes :-)
wdenk2d966952002-10-31 22:12:35 +00001334 *
wdenk57b2d802003-06-27 21:31:46 +00001335 * Note: in all cases where I have seen this so far
1336 * it was a problem with the router configuration,
1337 * for instance when a router was configured in the
1338 * BOOTP reply, but the TFTP server was on the same
1339 * subnet. So this is probably a warning that your
1340 * configuration might be wrong. But I'm not really
1341 * sure if there aren't any other situations.
Simon Glass230467c2011-10-24 18:00:01 +00001342 *
1343 * Simon Glass <sjg@chromium.org>: We get an ICMP when
1344 * we send a tftp packet to a dead connection, or when
1345 * there is no server at the other end.
wdenk2d966952002-10-31 22:12:35 +00001346 */
1347 if (ip->ip_p == IPPROTO_ICMP) {
Simon Glass43c72962011-10-24 18:00:00 +00001348 receive_icmp(ip, len, src_ip, et);
1349 return;
Ying-Chun Liu (PaulLiu)41efed12022-11-08 14:17:28 +08001350#if defined(CONFIG_PROT_TCP)
1351 } else if (ip->ip_p == IPPROTO_TCP) {
1352 debug_cond(DEBUG_DEV_PKT,
1353 "TCP PH (to=%pI4, from=%pI4, len=%d)\n",
1354 &dst_ip, &src_ip, len);
1355
1356 rxhand_tcp_f((union tcp_build_pkt *)ip, len);
1357 return;
1358#endif
wdenk2d966952002-10-31 22:12:35 +00001359 } else if (ip->ip_p != IPPROTO_UDP) { /* Only UDP packets */
1360 return;
1361 }
1362
Rasmus Villemoes932d4842022-10-14 19:43:40 +02001363 if (ntohs(ip->udp_len) < UDP_HDR_SIZE || ntohs(ip->udp_len) > len - IP_HDR_SIZE)
liucheng (G)c580b292019-08-29 13:47:33 +00001364 return;
1365
Joe Hershberger05a377b2012-05-23 08:01:04 +00001366 debug_cond(DEBUG_DEV_PKT,
Joe Hershbergerc80b41b02015-04-08 01:41:21 -05001367 "received UDP (to=%pI4, from=%pI4, len=%d)\n",
1368 &dst_ip, &src_ip, len);
Joe Hershberger05a377b2012-05-23 08:01:04 +00001369
Simon Glasse0eb4ef2021-12-18 11:27:49 -07001370 if (IS_ENABLED(CONFIG_UDP_CHECKSUM) && ip->udp_xsum != 0) {
Wolfgang Denk30b87322005-08-12 23:43:12 +02001371 ulong xsum;
Heinrich Schuchardt16ffe472019-11-05 12:48:19 +01001372 u8 *sumptr;
Stefan Roesedfced812005-08-12 20:06:52 +02001373 ushort sumlen;
1374
1375 xsum = ip->ip_p;
1376 xsum += (ntohs(ip->udp_len));
Joe Hershberger5874dec2015-04-08 01:41:01 -05001377 xsum += (ntohl(ip->ip_src.s_addr) >> 16) & 0x0000ffff;
1378 xsum += (ntohl(ip->ip_src.s_addr) >> 0) & 0x0000ffff;
1379 xsum += (ntohl(ip->ip_dst.s_addr) >> 16) & 0x0000ffff;
1380 xsum += (ntohl(ip->ip_dst.s_addr) >> 0) & 0x0000ffff;
Stefan Roesedfced812005-08-12 20:06:52 +02001381
1382 sumlen = ntohs(ip->udp_len);
Heinrich Schuchardt16ffe472019-11-05 12:48:19 +01001383 sumptr = (u8 *)&ip->udp_src;
Stefan Roesedfced812005-08-12 20:06:52 +02001384
1385 while (sumlen > 1) {
Heinrich Schuchardt16ffe472019-11-05 12:48:19 +01001386 /* inlined ntohs() to avoid alignment errors */
1387 xsum += (sumptr[0] << 8) + sumptr[1];
1388 sumptr += 2;
Stefan Roesedfced812005-08-12 20:06:52 +02001389 sumlen -= 2;
1390 }
Heinrich Schuchardt16ffe472019-11-05 12:48:19 +01001391 if (sumlen > 0)
1392 xsum += (sumptr[0] << 8) + sumptr[0];
Stefan Roesedfced812005-08-12 20:06:52 +02001393 while ((xsum >> 16) != 0) {
Luca Ceresoli5c83a962011-05-11 03:59:54 +00001394 xsum = (xsum & 0x0000ffff) +
1395 ((xsum >> 16) & 0x0000ffff);
Stefan Roesedfced812005-08-12 20:06:52 +02001396 }
1397 if ((xsum != 0x00000000) && (xsum != 0x0000ffff)) {
Wolfgang Denk12cec0a2008-07-11 01:16:00 +02001398 printf(" UDP wrong checksum %08lx %08x\n",
Joe Hershbergerc80b41b02015-04-08 01:41:21 -05001399 xsum, ntohs(ip->udp_xsum));
Stefan Roesedfced812005-08-12 20:06:52 +02001400 return;
1401 }
1402 }
Stefan Roesedfced812005-08-12 20:06:52 +02001403
Holger Denglerae85c072017-07-20 10:10:55 +02001404#if defined(CONFIG_NETCONSOLE) && !defined(CONFIG_SPL_BUILD)
Joe Hershberger6fe8b452012-05-23 07:58:04 +00001405 nc_input_packet((uchar *)ip + IP_UDP_HDR_SIZE,
Joe Hershbergerc80b41b02015-04-08 01:41:21 -05001406 src_ip,
1407 ntohs(ip->udp_dst),
1408 ntohs(ip->udp_src),
1409 ntohs(ip->udp_len) - UDP_HDR_SIZE);
wdenkb8fb6192004-08-02 21:11:11 +00001410#endif
wdenk2d966952002-10-31 22:12:35 +00001411 /*
Joe Hershbergerc80b41b02015-04-08 01:41:21 -05001412 * IP header OK. Pass the packet to the current handler.
wdenk2d966952002-10-31 22:12:35 +00001413 */
Joe Hershbergerf50357b2012-05-23 07:59:15 +00001414 (*udp_packet_handler)((uchar *)ip + IP_UDP_HDR_SIZE,
Joe Hershbergerc80b41b02015-04-08 01:41:21 -05001415 ntohs(ip->udp_dst),
1416 src_ip,
1417 ntohs(ip->udp_src),
1418 ntohs(ip->udp_len) - UDP_HDR_SIZE);
wdenk2d966952002-10-31 22:12:35 +00001419 break;
Lothar Felten776fc102018-06-22 22:29:54 +02001420#ifdef CONFIG_CMD_WOL
1421 case PROT_WOL:
1422 wol_receive(ip, len);
1423 break;
1424#endif
Samuel Mendoza-Jonasc8f4ab02022-08-08 21:46:03 +09301425#ifdef CONFIG_PHY_NCSI
1426 case PROT_NCSI:
1427 ncsi_receive(et, ip, len);
1428 break;
1429#endif
wdenk2d966952002-10-31 22:12:35 +00001430 }
1431}
1432
wdenk2d966952002-10-31 22:12:35 +00001433/**********************************************************************/
1434
Simon Glassd6c5f552011-10-24 18:00:02 +00001435static int net_check_prereq(enum proto_t protocol)
wdenk2d966952002-10-31 22:12:35 +00001436{
1437 switch (protocol) {
wdenk05939202004-04-18 17:39:38 +00001438 /* Fall through */
Jon Loeliger54f35c22007-07-09 17:45:14 -05001439#if defined(CONFIG_CMD_PING)
wdenke6466f62003-06-05 19:27:42 +00001440 case PING:
Joe Hershberger5874dec2015-04-08 01:41:01 -05001441 if (net_ping_ip.s_addr == 0) {
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001442 puts("*** ERROR: ping address not given\n");
Luca Ceresoli5fe6de22011-05-04 02:40:45 +00001443 return 1;
wdenk05939202004-04-18 17:39:38 +00001444 }
1445 goto common;
wdenke6466f62003-06-05 19:27:42 +00001446#endif
Viacheslav Mitrofanove03c8aa2022-12-02 12:18:08 +03001447#if defined(CONFIG_CMD_PING6)
1448 case PING6:
1449 if (ip6_is_unspecified_addr(&net_ping_ip6)) {
1450 puts("*** ERROR: ping address not given\n");
1451 return 1;
1452 }
1453 goto common;
1454#endif
Robin Getz82f0d232009-07-20 14:53:54 -04001455#if defined(CONFIG_CMD_DNS)
1456 case DNS:
Joe Hershberger5874dec2015-04-08 01:41:01 -05001457 if (net_dns_server.s_addr == 0) {
Robin Getz82f0d232009-07-20 14:53:54 -04001458 puts("*** ERROR: DNS server address not given\n");
1459 return 1;
1460 }
1461 goto common;
1462#endif
Philippe Reynes6ec70bc2020-09-18 14:13:00 +02001463#if defined(CONFIG_PROT_UDP)
1464 case UDP:
1465 if (udp_prereq())
1466 return 1;
1467 goto common;
1468#endif
1469
Jon Loeliger54f35c22007-07-09 17:45:14 -05001470#if defined(CONFIG_CMD_NFS)
wdenkbe9c1cb2004-02-24 02:00:03 +00001471 case NFS:
1472#endif
Joe Hershbergerc80b41b02015-04-08 01:41:21 -05001473 /* Fall through */
Simon Glassd6c5f552011-10-24 18:00:02 +00001474 case TFTPGET:
Simon Glass6a398d22011-10-24 18:00:07 +00001475 case TFTPPUT:
Viacheslav Mitrofanov12b95ae2022-12-02 12:18:07 +03001476 if (IS_ENABLED(CONFIG_IPV6) && use_ip6) {
1477 if (!memcmp(&net_server_ip6, &net_null_addr_ip6,
1478 sizeof(struct in6_addr)) &&
1479 !strchr(net_boot_file_name, '[')) {
1480 puts("*** ERROR: `serverip6' not set\n");
1481 return 1;
1482 }
1483 } else if (net_server_ip.s_addr == 0 && !is_serverip_in_cmd()) {
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001484 puts("*** ERROR: `serverip' not set\n");
Luca Ceresoli5fe6de22011-05-04 02:40:45 +00001485 return 1;
wdenk05939202004-04-18 17:39:38 +00001486 }
Philippe Reynes2829d992020-09-18 14:13:02 +02001487#if defined(CONFIG_CMD_PING) || \
Philippe Reynes6ec70bc2020-09-18 14:13:00 +02001488 defined(CONFIG_CMD_DNS) || defined(CONFIG_PROT_UDP)
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001489common:
wdenke6466f62003-06-05 19:27:42 +00001490#endif
Simon Guinot00dceba2011-05-01 23:38:40 +00001491 /* Fall through */
wdenke6466f62003-06-05 19:27:42 +00001492
Simon Guinot00dceba2011-05-01 23:38:40 +00001493 case NETCONS:
Alex Kiernand5aa57c2018-05-29 15:30:53 +00001494 case FASTBOOT:
Luca Ceresoli7aa81a42011-05-17 00:03:40 +00001495 case TFTPSRV:
Viacheslav Mitrofanov12b95ae2022-12-02 12:18:07 +03001496 if (IS_ENABLED(CONFIG_IPV6) && use_ip6) {
1497 if (!memcmp(&net_link_local_ip6, &net_null_addr_ip6,
1498 sizeof(struct in6_addr))) {
1499 puts("*** ERROR: `ip6addr` not set\n");
1500 return 1;
1501 }
1502 } else if (net_ip.s_addr == 0) {
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001503 puts("*** ERROR: `ipaddr' not set\n");
Luca Ceresoli5fe6de22011-05-04 02:40:45 +00001504 return 1;
wdenk05939202004-04-18 17:39:38 +00001505 }
1506 /* Fall through */
wdenk2d966952002-10-31 22:12:35 +00001507
Peter Tyserf7a48ca2010-09-30 11:25:48 -05001508#ifdef CONFIG_CMD_RARP
wdenk2d966952002-10-31 22:12:35 +00001509 case RARP:
Peter Tyserf7a48ca2010-09-30 11:25:48 -05001510#endif
Samuel Mendoza-Jonasc8f4ab02022-08-08 21:46:03 +09301511#ifdef CONFIG_PHY_NCSI
1512 case NCSI:
1513#endif
wdenk2d966952002-10-31 22:12:35 +00001514 case BOOTP:
wdenk145d2c12004-04-15 21:48:45 +00001515 case CDP:
Peter Tyserf7a48ca2010-09-30 11:25:48 -05001516 case DHCP:
Joe Hershbergerb35a3a62012-05-23 08:00:12 +00001517 case LINKLOCAL:
Joe Hershberger8ecdbed2015-04-08 01:41:04 -05001518 if (memcmp(net_ethaddr, "\0\0\0\0\0\0", 6) == 0) {
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001519 int num = eth_get_dev_index();
wdenk2d966952002-10-31 22:12:35 +00001520
wdenk05939202004-04-18 17:39:38 +00001521 switch (num) {
1522 case -1:
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001523 puts("*** ERROR: No ethernet found.\n");
Luca Ceresoli5fe6de22011-05-04 02:40:45 +00001524 return 1;
wdenk05939202004-04-18 17:39:38 +00001525 case 0:
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001526 puts("*** ERROR: `ethaddr' not set\n");
wdenk2d966952002-10-31 22:12:35 +00001527 break;
wdenk05939202004-04-18 17:39:38 +00001528 default:
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001529 printf("*** ERROR: `eth%daddr' not set\n",
Joe Hershbergerc80b41b02015-04-08 01:41:21 -05001530 num);
wdenk2d966952002-10-31 22:12:35 +00001531 break;
wdenk05939202004-04-18 17:39:38 +00001532 }
wdenk2d966952002-10-31 22:12:35 +00001533
Joe Hershbergerc80b41b02015-04-08 01:41:21 -05001534 net_start_again();
Luca Ceresoli5fe6de22011-05-04 02:40:45 +00001535 return 2;
wdenk05939202004-04-18 17:39:38 +00001536 }
1537 /* Fall through */
1538 default:
Luca Ceresoli5fe6de22011-05-04 02:40:45 +00001539 return 0;
wdenk2d966952002-10-31 22:12:35 +00001540 }
Luca Ceresoli5fe6de22011-05-04 02:40:45 +00001541 return 0; /* OK */
wdenk2d966952002-10-31 22:12:35 +00001542}
1543/**********************************************************************/
1544
1545int
Joe Hershbergera8ca4f62015-04-08 01:41:05 -05001546net_eth_hdr_size(void)
wdenk145d2c12004-04-15 21:48:45 +00001547{
1548 ushort myvlanid;
1549
Joe Hershberger013d3872015-04-08 01:41:17 -05001550 myvlanid = ntohs(net_our_vlan);
wdenk145d2c12004-04-15 21:48:45 +00001551 if (myvlanid == (ushort)-1)
1552 myvlanid = VLAN_NONE;
wdenk2d966952002-10-31 22:12:35 +00001553
Luca Ceresoli5c83a962011-05-11 03:59:54 +00001554 return ((myvlanid & VLAN_IDMASK) == VLAN_NONE) ? ETHER_HDR_SIZE :
1555 VLAN_ETHER_HDR_SIZE;
wdenk145d2c12004-04-15 21:48:45 +00001556}
1557
Joe Hershbergera8ca4f62015-04-08 01:41:05 -05001558int net_set_ether(uchar *xet, const uchar *dest_ethaddr, uint prot)
wdenk2d966952002-10-31 22:12:35 +00001559{
Joe Hershberger1178f412012-05-23 07:58:06 +00001560 struct ethernet_hdr *et = (struct ethernet_hdr *)xet;
wdenk145d2c12004-04-15 21:48:45 +00001561 ushort myvlanid;
1562
Joe Hershberger013d3872015-04-08 01:41:17 -05001563 myvlanid = ntohs(net_our_vlan);
wdenk145d2c12004-04-15 21:48:45 +00001564 if (myvlanid == (ushort)-1)
1565 myvlanid = VLAN_NONE;
wdenk2d966952002-10-31 22:12:35 +00001566
Joe Hershberger8ecdbed2015-04-08 01:41:04 -05001567 memcpy(et->et_dest, dest_ethaddr, 6);
1568 memcpy(et->et_src, net_ethaddr, 6);
wdenk145d2c12004-04-15 21:48:45 +00001569 if ((myvlanid & VLAN_IDMASK) == VLAN_NONE) {
Luca Ceresolib19d0b72011-05-04 02:40:46 +00001570 et->et_protlen = htons(prot);
wdenk145d2c12004-04-15 21:48:45 +00001571 return ETHER_HDR_SIZE;
1572 } else {
Joe Hershbergerb43784c2012-05-23 07:58:07 +00001573 struct vlan_ethernet_hdr *vet =
1574 (struct vlan_ethernet_hdr *)xet;
wdenk2d966952002-10-31 22:12:35 +00001575
wdenk145d2c12004-04-15 21:48:45 +00001576 vet->vet_vlan_type = htons(PROT_VLAN);
1577 vet->vet_tag = htons((0 << 5) | (myvlanid & VLAN_IDMASK));
1578 vet->vet_type = htons(prot);
1579 return VLAN_ETHER_HDR_SIZE;
1580 }
1581}
wdenk2d966952002-10-31 22:12:35 +00001582
Joe Hershberger530cd6b2012-05-23 07:59:16 +00001583int net_update_ether(struct ethernet_hdr *et, uchar *addr, uint prot)
1584{
1585 ushort protlen;
1586
1587 memcpy(et->et_dest, addr, 6);
Joe Hershberger8ecdbed2015-04-08 01:41:04 -05001588 memcpy(et->et_src, net_ethaddr, 6);
Joe Hershberger530cd6b2012-05-23 07:59:16 +00001589 protlen = ntohs(et->et_protlen);
1590 if (protlen == PROT_VLAN) {
1591 struct vlan_ethernet_hdr *vet =
1592 (struct vlan_ethernet_hdr *)et;
1593 vet->vet_type = htons(prot);
1594 return VLAN_ETHER_HDR_SIZE;
1595 } else if (protlen > 1514) {
1596 et->et_protlen = htons(prot);
1597 return ETHER_HDR_SIZE;
1598 } else {
1599 /* 802.2 + SNAP */
1600 struct e802_hdr *et802 = (struct e802_hdr *)et;
1601 et802->et_prot = htons(prot);
1602 return E802_HDR_SIZE;
1603 }
1604}
1605
Duncan Haref1447c92018-06-24 15:40:41 -07001606void net_set_ip_header(uchar *pkt, struct in_addr dest, struct in_addr source,
1607 u16 pkt_len, u8 proto)
wdenk2d966952002-10-31 22:12:35 +00001608{
Joe Hershberger2ed5b492012-05-23 07:59:07 +00001609 struct ip_udp_hdr *ip = (struct ip_udp_hdr *)pkt;
wdenk2d966952002-10-31 22:12:35 +00001610
1611 /*
Joe Hershberger2ed5b492012-05-23 07:59:07 +00001612 * Construct an IP header.
wdenk2d966952002-10-31 22:12:35 +00001613 */
Luca Ceresoli5c83a962011-05-11 03:59:54 +00001614 /* IP_HDR_SIZE / 4 (not including UDP) */
1615 ip->ip_hl_v = 0x45;
wdenk2d966952002-10-31 22:12:35 +00001616 ip->ip_tos = 0;
Duncan Haref1447c92018-06-24 15:40:41 -07001617 ip->ip_len = htons(pkt_len);
1618 ip->ip_p = proto;
Joe Hershbergerc80b41b02015-04-08 01:41:21 -05001619 ip->ip_id = htons(net_ip_id++);
Peter Tyser0885bf02008-12-01 16:26:20 -06001620 ip->ip_off = htons(IP_FLAGS_DFRAG); /* Don't fragment */
wdenk2d966952002-10-31 22:12:35 +00001621 ip->ip_ttl = 255;
wdenk2d966952002-10-31 22:12:35 +00001622 ip->ip_sum = 0;
Luca Ceresoli5c83a962011-05-11 03:59:54 +00001623 /* already in network byte order */
Joe Hershberger5874dec2015-04-08 01:41:01 -05001624 net_copy_ip((void *)&ip->ip_src, &source);
Joe Hershberger2ed5b492012-05-23 07:59:07 +00001625 /* already in network byte order */
Joe Hershberger5874dec2015-04-08 01:41:01 -05001626 net_copy_ip((void *)&ip->ip_dst, &dest);
Duncan Haref1447c92018-06-24 15:40:41 -07001627
1628 ip->ip_sum = compute_ip_checksum(ip, IP_HDR_SIZE);
Joe Hershberger2ed5b492012-05-23 07:59:07 +00001629}
1630
Joe Hershberger5874dec2015-04-08 01:41:01 -05001631void net_set_udp_header(uchar *pkt, struct in_addr dest, int dport, int sport,
Joe Hershberger2ed5b492012-05-23 07:59:07 +00001632 int len)
1633{
1634 struct ip_udp_hdr *ip = (struct ip_udp_hdr *)pkt;
1635
1636 /*
1637 * If the data is an odd number of bytes, zero the
1638 * byte after the last byte so that the checksum
1639 * will work.
1640 */
1641 if (len & 1)
1642 pkt[IP_UDP_HDR_SIZE + len] = 0;
1643
Duncan Haref1447c92018-06-24 15:40:41 -07001644 net_set_ip_header(pkt, dest, net_ip, IP_UDP_HDR_SIZE + len,
1645 IPPROTO_UDP);
Joe Hershberger2ed5b492012-05-23 07:59:07 +00001646
wdenk2d966952002-10-31 22:12:35 +00001647 ip->udp_src = htons(sport);
1648 ip->udp_dst = htons(dport);
Joe Hershberger6fe8b452012-05-23 07:58:04 +00001649 ip->udp_len = htons(UDP_HDR_SIZE + len);
wdenk2d966952002-10-31 22:12:35 +00001650 ip->udp_xsum = 0;
wdenk2d966952002-10-31 22:12:35 +00001651}
1652
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001653void copy_filename(char *dst, const char *src, int size)
wdenk2d966952002-10-31 22:12:35 +00001654{
Joe Hershbergere20cadd2018-07-03 19:36:41 -05001655 if (src && *src && (*src == '"')) {
wdenk2d966952002-10-31 22:12:35 +00001656 ++src;
1657 --size;
1658 }
1659
Joe Hershbergere20cadd2018-07-03 19:36:41 -05001660 while ((--size > 0) && src && *src && (*src != '"'))
wdenk2d966952002-10-31 22:12:35 +00001661 *dst++ = *src++;
wdenk2d966952002-10-31 22:12:35 +00001662 *dst = '\0';
1663}
1664
Joe Hershbergerf559f1c2018-07-03 19:36:39 -05001665int is_serverip_in_cmd(void)
1666{
1667 return !!strchr(net_boot_file_name, ':');
1668}
1669
Joe Hershberger9cb7b022018-07-03 19:36:43 -05001670int net_parse_bootfile(struct in_addr *ipaddr, char *filename, int max_len)
1671{
1672 char *colon;
Lyle Franklinc1fad502022-04-16 11:36:43 -04001673 struct in_addr ip;
1674 ip.s_addr = 0;
Joe Hershberger9cb7b022018-07-03 19:36:43 -05001675
1676 if (net_boot_file_name[0] == '\0')
1677 return 0;
1678
1679 colon = strchr(net_boot_file_name, ':');
1680 if (colon) {
Lyle Franklinc1fad502022-04-16 11:36:43 -04001681 ip = string_to_ip(net_boot_file_name);
1682 if (ipaddr && ip.s_addr)
1683 *ipaddr = ip;
1684 }
1685 if (ip.s_addr) {
Joe Hershberger9cb7b022018-07-03 19:36:43 -05001686 strncpy(filename, colon + 1, max_len);
1687 } else {
1688 strncpy(filename, net_boot_file_name, max_len);
1689 }
1690 filename[max_len - 1] = '\0';
1691
1692 return 1;
1693}
1694
Joe Hershberger5874dec2015-04-08 01:41:01 -05001695void ip_to_string(struct in_addr x, char *s)
wdenk2d966952002-10-31 22:12:35 +00001696{
Joe Hershberger5874dec2015-04-08 01:41:01 -05001697 x.s_addr = ntohl(x.s_addr);
Luca Ceresoli7cbd7482011-05-11 03:59:56 +00001698 sprintf(s, "%d.%d.%d.%d",
Joe Hershberger5874dec2015-04-08 01:41:01 -05001699 (int) ((x.s_addr >> 24) & 0xff),
1700 (int) ((x.s_addr >> 16) & 0xff),
1701 (int) ((x.s_addr >> 8) & 0xff),
1702 (int) ((x.s_addr >> 0) & 0xff)
wdenk05939202004-04-18 17:39:38 +00001703 );
wdenk2d966952002-10-31 22:12:35 +00001704}
1705
Joe Hershberger013d3872015-04-08 01:41:17 -05001706void vlan_to_string(ushort x, char *s)
wdenk145d2c12004-04-15 21:48:45 +00001707{
1708 x = ntohs(x);
1709
1710 if (x == (ushort)-1)
1711 x = VLAN_NONE;
1712
1713 if (x == VLAN_NONE)
1714 strcpy(s, "none");
1715 else
1716 sprintf(s, "%d", x & VLAN_IDMASK);
1717}
1718
Joe Hershberger013d3872015-04-08 01:41:17 -05001719ushort string_to_vlan(const char *s)
wdenk145d2c12004-04-15 21:48:45 +00001720{
1721 ushort id;
1722
1723 if (s == NULL)
wdenk656140b2004-04-25 13:18:40 +00001724 return htons(VLAN_NONE);
wdenk145d2c12004-04-15 21:48:45 +00001725
1726 if (*s < '0' || *s > '9')
1727 id = VLAN_NONE;
1728 else
Simon Glassff9b9032021-07-24 09:03:30 -06001729 id = (ushort)dectoul(s, NULL);
wdenk145d2c12004-04-15 21:48:45 +00001730
wdenk656140b2004-04-25 13:18:40 +00001731 return htons(id);
wdenk145d2c12004-04-15 21:48:45 +00001732}
1733
Simon Glassda1a1342017-08-03 12:22:15 -06001734ushort env_get_vlan(char *var)
wdenk145d2c12004-04-15 21:48:45 +00001735{
Simon Glass64b723f2017-08-03 12:22:12 -06001736 return string_to_vlan(env_get(var));
wdenk145d2c12004-04-15 21:48:45 +00001737}