blob: c9a749f6cc82b85fa4c3da514e68b9ced1fd344d [file] [log] [blame]
Tom Rinif739fcd2018-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 Hershbergerd22c3382012-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 Denkb2f50802005-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
wdenkcbd8a352004-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
wdenkea287de2005-04-01 00:25:43 +000074 *
Lothar Feltend8970da2018-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 Glass52f24232020-05-10 11:40:00 -060085#include <bootstage.h>
wdenk2d966952002-10-31 22:12:35 +000086#include <command.h>
Simon Glass24b852a2015-11-08 23:47:45 -070087#include <console.h>
Simon Glassc7694dd2019-08-01 09:46:46 -060088#include <env.h>
Simon Glassf3998fd2019-08-02 09:44:25 -060089#include <env_internal.h>
Joe Hershberger60304592015-03-22 17:09:24 -050090#include <errno.h>
Simon Glass8e8ccfe2019-12-28 10:45:03 -070091#include <image.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060092#include <log.h>
wdenk2d966952002-10-31 22:12:35 +000093#include <net.h>
Viacheslav Mitrofanovffdbf3b2022-12-02 12:18:06 +030094#include <net6.h>
95#include <ndisc.h>
Alex Kiernanf73a7df2018-05-29 15:30:53 +000096#include <net/fastboot.h>
Lukasz Majewski34696952015-08-24 00:21:43 +020097#include <net/tftp.h>
Samuel Mendoza-Jonas09bd3d02022-08-08 21:46:03 +093098#include <net/ncsi.h>
Ramon Fried3eaac632019-07-18 21:43:30 +030099#if defined(CONFIG_CMD_PCAP)
100#include <net/pcap.h>
101#endif
Philippe Reynesb43ea1b2020-09-18 14:13:00 +0200102#include <net/udp.h>
Uri Mashiach2d8d1902017-01-19 10:51:45 +0200103#if defined(CONFIG_LED_STATUS)
Joe Hershberger4545f4e2012-05-23 07:58:15 +0000104#include <miiphy.h>
105#include <status_led.h>
106#endif
107#include <watchdog.h>
108#include <linux/compiler.h>
Simon Glassf43b2df2023-01-17 10:47:27 -0700109#include <test/test.h>
Joe Hershbergerd280d3f2012-05-23 07:58:01 +0000110#include "arp.h"
wdenk2d966952002-10-31 22:12:35 +0000111#include "bootp.h"
Joe Hershbergerf575ae12012-05-23 07:57:59 +0000112#include "cdp.h"
Robin Getz1a32bf42009-07-20 14:53:54 -0400113#if defined(CONFIG_CMD_DNS)
114#include "dns.h"
115#endif
Joe Hershbergerd22c3382012-05-23 08:00:12 +0000116#include "link_local.h"
Joe Hershberger4545f4e2012-05-23 07:58:15 +0000117#include "nfs.h"
Joe Hershbergera36b12f2012-05-23 07:58:02 +0000118#include "ping.h"
Joe Hershberger4545f4e2012-05-23 07:58:15 +0000119#include "rarp.h"
Lothar Feltend8970da2018-06-22 22:29:54 +0200120#if defined(CONFIG_CMD_WOL)
121#include "wol.h"
122#endif
Ying-Chun Liu (PaulLiu)a3bf1932022-11-08 14:17:28 +0800123#include <net/tcp.h>
Ying-Chun Liu (PaulLiu)cfbae482022-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 Ceresoli3e38e422011-05-11 03:59:54 +0000128/* Our subnet mask (0=unknown) */
Joe Hershberger049a95a2015-04-08 01:41:01 -0500129struct in_addr net_netmask;
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000130/* Our gateways IP address */
Joe Hershberger049a95a2015-04-08 01:41:01 -0500131struct in_addr net_gateway;
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000132/* Our DNS IP address */
Joe Hershberger049a95a2015-04-08 01:41:01 -0500133struct in_addr net_dns_server;
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500134#if defined(CONFIG_BOOTP_DNS2)
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000135/* Our 2nd DNS IP address */
Joe Hershberger049a95a2015-04-08 01:41:01 -0500136struct in_addr net_dns_server2;
stroesefe389a82003-08-28 14:17:32 +0000137#endif
wdenk2d966952002-10-31 22:12:35 +0000138
139/** END OF BOOTP EXTENTIONS **/
140
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000141/* Our ethernet address */
Joe Hershberger0adb5b72015-04-08 01:41:04 -0500142u8 net_ethaddr[6];
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000143/* Boot server enet address */
Joe Hershberger0adb5b72015-04-08 01:41:04 -0500144u8 net_server_ethaddr[6];
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000145/* Our IP addr (0 = unknown) */
Joe Hershberger049a95a2015-04-08 01:41:01 -0500146struct in_addr net_ip;
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000147/* Server IP addr (0 = unknown) */
Joe Hershberger049a95a2015-04-08 01:41:01 -0500148struct in_addr net_server_ip;
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000149/* Current receive packet */
Joe Hershberger1203fcc2015-04-08 01:41:05 -0500150uchar *net_rx_packet;
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000151/* Current rx packet length */
Joe Hershberger1203fcc2015-04-08 01:41:05 -0500152int net_rx_packet_len;
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000153/* IP packet ID */
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500154static unsigned net_ip_id;
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000155/* Ethernet bcast address */
Joe Hershberger0adb5b72015-04-08 01:41:04 -0500156const u8 net_bcast_ethaddr[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
157const u8 net_null_ethaddr[6];
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200158#if defined(CONFIG_API) || defined(CONFIG_EFI_LOADER)
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500159void (*push_packet)(void *, int len) = 0;
Rafal Jaworowskif85b6072007-12-27 18:19:02 +0100160#endif
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000161/* Network loop state */
Joe Hershberger22f6e992012-05-23 07:59:14 +0000162enum net_loop_state net_state;
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000163/* Tried all network devices */
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500164int net_restart_wrap;
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000165/* Network loop restarted */
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500166static int net_restarted;
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000167/* At least one device configured */
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500168static int net_dev_exists;
wdenk2d966952002-10-31 22:12:35 +0000169
wdenk6e592382004-04-18 17:39:38 +0000170/* XXX in both little & big endian machines 0xFFFF == ntohs(-1) */
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000171/* default is without VLAN */
Joe Hershberger4fd50552015-04-08 01:41:17 -0500172ushort net_our_vlan = 0xFFFF;
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000173/* ditto */
Joe Hershberger4fd50552015-04-08 01:41:17 -0500174ushort net_native_vlan = 0xFFFF;
wdenka3d991b2004-04-15 21:48:45 +0000175
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000176/* Boot File name */
Jacob Stiffler11a69ff2015-09-30 10:12:05 -0400177char net_boot_file_name[1024];
Alexander Graf449312c2018-06-15 10:29:27 +0200178/* Indicates whether the file name was specified on the command line */
179bool net_boot_file_name_explicit;
Joe Hershberger14111572015-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 Hershberger1203fcc2015-04-08 01:41:05 -0500185static uchar net_pkt_buf[(PKTBUFSRX+1) * PKTSIZE_ALIGN + PKTALIGN];
Joe Hershberger2a504df2015-03-22 17:09:11 -0500186/* Receive packets */
187uchar *net_rx_packets[PKTBUFSRX];
Joe Hershbergerece223b2012-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 Glass39bccd22011-10-26 14:18:38 +0000192#ifdef CONFIG_CMD_TFTPPUT
Joe Hershbergerece223b2012-05-23 07:59:15 +0000193/* Current ICMP rx handler */
194static rxhand_icmp_f *packet_icmp_handler;
Simon Glass39bccd22011-10-26 14:18:38 +0000195#endif
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000196/* Current timeout handler */
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500197static thand_f *time_handler;
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000198/* Time base value */
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500199static ulong time_start;
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000200/* Current timeout value */
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500201static ulong time_delta;
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000202/* THE transmit packet */
Joe Hershberger1203fcc2015-04-08 01:41:05 -0500203uchar *net_tx_packet;
wdenk2d966952002-10-31 22:12:35 +0000204
Simon Glasse4bf0c52011-10-24 18:00:02 +0000205static int net_check_prereq(enum proto_t protocol);
wdenk2d966952002-10-31 22:12:35 +0000206
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500207static int net_try_count;
Remy Bohmer67b96e82009-10-28 22:13:39 +0100208
Jim Linb63056d2013-08-13 19:03:05 +0800209int __maybe_unused net_busy_flag;
210
wdenk2d966952002-10-31 22:12:35 +0000211/**********************************************************************/
wdenk73a8b272003-06-05 19:27:42 +0000212
Joe Hershbergerfd305632015-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 Glasse4a3d572011-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 Rinia6ab4b52019-12-05 19:35:07 -0500305#if defined(CONFIG_CMD_NFS) && !defined(CONFIG_SPL_BUILD)
Simon Glass00caae62017-08-03 12:22:12 -0600306 const char *s = env_get("autoload");
Simon Glasse4a3d572011-10-27 06:24:32 +0000307
Joe Hershbergerec8a2522012-12-11 22:16:22 -0600308 if (s != NULL && strcmp(s, "NFS") == 0) {
Joe Hershberger3855cad2018-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 Glass43407532021-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 Hershberger3855cad2018-07-03 19:36:40 -0500317 return;
318 }
Joe Hershbergerec8a2522012-12-11 22:16:22 -0600319 /*
320 * Use NFS to load the bootfile.
321 */
Joe Hershberger68c76a32015-04-08 01:41:10 -0500322 nfs_start();
Joe Hershbergerec8a2522012-12-11 22:16:22 -0600323 return;
324 }
Simon Glasse4a3d572011-10-27 06:24:32 +0000325#endif
Simon Glassbfebc8c2017-08-03 12:22:13 -0600326 if (env_get_yesno("autoload") == 0) {
Joe Hershbergerec8a2522012-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 Glasse4a3d572011-10-27 06:24:32 +0000333 }
Joe Hershberger3855cad2018-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 Glass43407532021-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 Hershberger3855cad2018-07-03 19:36:40 -0500342 return;
343 }
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500344 tftp_start(TFTPGET);
Simon Glasse4a3d572011-10-27 06:24:32 +0000345}
346
Sean Andersonc3f02782020-09-12 17:45:43 -0400347static int net_init_loop(void)
Heiko Schocher2f70c492009-02-10 09:38:52 +0100348{
Viacheslav Mitrofanovffdbf3b2022-12-02 12:18:06 +0300349 if (eth_get_dev()) {
Joe Hershberger0adb5b72015-04-08 01:41:04 -0500350 memcpy(net_ethaddr, eth_get_ethaddr(), 6);
Viacheslav Mitrofanovffdbf3b2022-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 Andersonc3f02782020-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 Zaidman3c172c42009-04-04 01:43:00 +0300367
Sean Andersonc3f02782020-09-12 17:45:43 -0400368 return 0;
Heiko Schocher2f70c492009-02-10 09:38:52 +0100369}
370
Joe Hershbergerece223b2012-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 Hershbergerbc0571f2015-04-08 01:41:21 -0500375 net_set_timeout_handler(0, NULL);
Joe Hershbergerece223b2012-05-23 07:59:15 +0000376}
377
378static void net_cleanup_loop(void)
379{
380 net_clear_handlers();
381}
382
Sean Andersonc3f02782020-09-12 17:45:43 -0400383int net_init(void)
Joe Hershberger46c495d2012-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 Hershberger1203fcc2015-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 Hershberger2a504df2015-03-22 17:09:11 -0500395 for (i = 0; i < PKTBUFSRX; i++) {
Joe Hershberger1203fcc2015-04-08 01:41:05 -0500396 net_rx_packets[i] = net_tx_packet +
397 (i + 1) * PKTSIZE_ALIGN;
Joe Hershberger2a504df2015-03-22 17:09:11 -0500398 }
Joe Hershberger85d25e02015-04-08 01:41:08 -0500399 arp_init();
Viacheslav Mitrofanovffdbf3b2022-12-02 12:18:06 +0300400 ndisc_init();
Joe Hershberger46c495d2012-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)a3bf1932022-11-08 14:17:28 +0800405 if (IS_ENABLED(CONFIG_PROT_TCP))
406 tcp_set_tcp_state(TCP_CLOSED);
Joe Hershberger46c495d2012-05-23 07:59:22 +0000407 }
408
Sean Andersonc3f02782020-09-12 17:45:43 -0400409 return net_init_loop();
Joe Hershberger46c495d2012-05-23 07:59:22 +0000410}
411
wdenk73a8b272003-06-05 19:27:42 +0000412/**********************************************************************/
wdenk2d966952002-10-31 22:12:35 +0000413/*
414 * Main network processing loop.
415 */
416
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500417int net_loop(enum proto_t protocol)
wdenk2d966952002-10-31 22:12:35 +0000418{
Joe Hershberger60304592015-03-22 17:09:24 -0500419 int ret = -EINVAL;
Leonid Iziumtsev60177b22018-05-08 15:55:50 +0200420 enum net_loop_state prev_net_state = net_state;
wdenk2d966952002-10-31 22:12:35 +0000421
Marek Szyprowskidef7a5c2020-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 Hershbergerbc0571f2015-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");
wdenk73a8b272003-06-05 19:27:42 +0000430
Samuel Mendoza-Jonas09bd3d02022-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 Glass573f14f2011-12-10 11:08:06 +0000441 bootstage_mark_name(BOOTSTAGE_ID_ETH_START, "eth_start");
Joe Hershberger46c495d2012-05-23 07:59:22 +0000442 net_init();
Yang Liud9506cd2020-12-21 14:44:39 +1100443 if (eth_is_on_demand_init()) {
wdenkb1bf6f22005-04-03 14:52:59 +0000444 eth_halt();
Joe Hershbergerf8be7d62012-08-03 10:59:08 +0000445 eth_set_current();
Joe Hershberger60304592015-03-22 17:09:24 -0500446 ret = eth_init();
447 if (ret < 0) {
Joe Hershbergerf8be7d62012-08-03 10:59:08 +0000448 eth_halt();
Joe Hershberger60304592015-03-22 17:09:24 -0500449 return ret;
Joe Hershbergerf8be7d62012-08-03 10:59:08 +0000450 }
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500451 } else {
Joe Hershbergerd2eaec62015-03-22 17:09:06 -0500452 eth_init_state_only();
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500453 }
Samuel Mendoza-Jonas4b290d42022-08-08 21:46:04 +0930454
wdenk2d966952002-10-31 22:12:35 +0000455restart:
Jim Linb63056d2013-08-13 19:03:05 +0800456#ifdef CONFIG_USB_KEYBOARD
457 net_busy_flag = 0;
458#endif
Joe Hershberger22f6e992012-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 Hershbergerbc0571f2015-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 Glassf43b2df2023-01-17 10:47:27 -0700469 if (!test_eth_enabled())
470 return 0;
471
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000472 switch (net_check_prereq(protocol)) {
wdenk2d966952002-10-31 22:12:35 +0000473 case 1:
474 /* network not configured */
wdenkb1bf6f22005-04-03 14:52:59 +0000475 eth_halt();
Leonid Iziumtsev60177b22018-05-08 15:55:50 +0200476 net_set_state(prev_net_state);
Joe Hershberger60304592015-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 Hershbergerbc0571f2015-04-08 01:41:21 -0500484 net_dev_exists = 1;
Joe Hershberger14111572015-04-08 01:41:02 -0500485 net_boot_file_size = 0;
wdenk2d966952002-10-31 22:12:35 +0000486 switch (protocol) {
Krebs, Olaf808f13d2020-03-09 14:27:55 +0000487#ifdef CONFIG_CMD_TFTPBOOT
Simon Glasse4bf0c52011-10-24 18:00:02 +0000488 case TFTPGET:
Simon Glass1fb7cd42011-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 Hershberger8885c5f2015-04-08 01:41:07 -0500493 tftp_start(protocol);
wdenk2d966952002-10-31 22:12:35 +0000494 break;
Krebs, Olaf808f13d2020-03-09 14:27:55 +0000495#endif
Luca Ceresoli7a83af02011-05-17 00:03:40 +0000496#ifdef CONFIG_CMD_TFTPSRV
497 case TFTPSRV:
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500498 tftp_start_server();
Luca Ceresoli7a83af02011-05-17 00:03:40 +0000499 break;
500#endif
Alex Kiernanf73a7df2018-05-29 15:30:53 +0000501#ifdef CONFIG_UDP_FUNCTION_FASTBOOT
502 case FASTBOOT:
503 fastboot_start_server();
504 break;
505#endif
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500506#if defined(CONFIG_CMD_DHCP)
wdenk2d966952002-10-31 22:12:35 +0000507 case DHCP:
Joe Hershberger7044c6b2015-04-08 01:41:09 -0500508 bootp_reset();
Joe Hershberger049a95a2015-04-08 01:41:01 -0500509 net_ip.s_addr = 0;
Joe Hershberger7044c6b2015-04-08 01:41:09 -0500510 dhcp_request(); /* Basically same as BOOTP */
wdenk2d966952002-10-31 22:12:35 +0000511 break;
Jon Loeliger610f2e92007-07-10 11:05:02 -0500512#endif
Krebs, Olaf808f13d2020-03-09 14:27:55 +0000513#if defined(CONFIG_CMD_BOOTP)
wdenk2d966952002-10-31 22:12:35 +0000514 case BOOTP:
Joe Hershberger7044c6b2015-04-08 01:41:09 -0500515 bootp_reset();
Joe Hershberger049a95a2015-04-08 01:41:01 -0500516 net_ip.s_addr = 0;
Joe Hershberger7044c6b2015-04-08 01:41:09 -0500517 bootp_request();
wdenk2d966952002-10-31 22:12:35 +0000518 break;
Krebs, Olaf808f13d2020-03-09 14:27:55 +0000519#endif
Peter Tyserbf6cb242010-09-30 11:25:48 -0500520#if defined(CONFIG_CMD_RARP)
wdenk2d966952002-10-31 22:12:35 +0000521 case RARP:
Joe Hershberger698d78e2015-04-08 01:41:11 -0500522 rarp_try = 0;
Joe Hershberger049a95a2015-04-08 01:41:01 -0500523 net_ip.s_addr = 0;
Joe Hershberger698d78e2015-04-08 01:41:11 -0500524 rarp_request();
wdenk2d966952002-10-31 22:12:35 +0000525 break;
Peter Tyserbf6cb242010-09-30 11:25:48 -0500526#endif
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500527#if defined(CONFIG_CMD_PING)
wdenk73a8b272003-06-05 19:27:42 +0000528 case PING:
Joe Hershbergera36b12f2012-05-23 07:58:02 +0000529 ping_start();
wdenk73a8b272003-06-05 19:27:42 +0000530 break;
531#endif
Viacheslav Mitrofanoveeb0a2c2022-12-02 12:18:08 +0300532#if defined(CONFIG_CMD_PING6)
533 case PING6:
534 ping6_start();
535 break;
536#endif
Tom Rinia6ab4b52019-12-05 19:35:07 -0500537#if defined(CONFIG_CMD_NFS) && !defined(CONFIG_SPL_BUILD)
wdenkcbd8a352004-02-24 02:00:03 +0000538 case NFS:
Joe Hershberger68c76a32015-04-08 01:41:10 -0500539 nfs_start();
wdenkcbd8a352004-02-24 02:00:03 +0000540 break;
541#endif
Ying-Chun Liu (PaulLiu)cfbae482022-11-08 14:17:29 +0800542#if defined(CONFIG_CMD_WGET)
543 case WGET:
544 wget_start();
545 break;
546#endif
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500547#if defined(CONFIG_CMD_CDP)
wdenka3d991b2004-04-15 21:48:45 +0000548 case CDP:
Joe Hershberger6aede5b2015-04-08 01:41:14 -0500549 cdp_start();
wdenka3d991b2004-04-15 21:48:45 +0000550 break;
551#endif
Holger Dengler66c89ee2017-07-20 10:10:55 +0200552#if defined(CONFIG_NETCONSOLE) && !defined(CONFIG_SPL_BUILD)
wdenk68ceb292004-08-02 21:11:11 +0000553 case NETCONS:
Joe Hershberger6a38a5f2015-04-08 01:41:16 -0500554 nc_start();
wdenk68ceb292004-08-02 21:11:11 +0000555 break;
556#endif
Robin Getz1a32bf42009-07-20 14:53:54 -0400557#if defined(CONFIG_CMD_DNS)
558 case DNS:
Joe Hershberger786eac52015-04-08 01:41:15 -0500559 dns_start();
Robin Getz1a32bf42009-07-20 14:53:54 -0400560 break;
561#endif
Joe Hershbergerd22c3382012-05-23 08:00:12 +0000562#if defined(CONFIG_CMD_LINK_LOCAL)
563 case LINKLOCAL:
564 link_local_start();
565 break;
566#endif
Lothar Feltend8970da2018-06-22 22:29:54 +0200567#if defined(CONFIG_CMD_WOL)
568 case WOL:
569 wol_start();
570 break;
571#endif
Samuel Mendoza-Jonas09bd3d02022-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 Reynesb43ea1b2020-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 Loeliger643d1ab2007-07-09 17:45:14 -0500587#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000588#if defined(CONFIG_SYS_FAULT_ECHO_LINK_DOWN) && \
Uri Mashiach2d8d1902017-01-19 10:51:45 +0200589 defined(CONFIG_LED_STATUS) && \
590 defined(CONFIG_LED_STATUS_RED)
wdenkfc3e2162003-10-08 22:33:00 +0000591 /*
wdenk42d1f032003-10-15 23:53:47 +0000592 * Echo the inverted link state to the fault LED.
wdenkfc3e2162003-10-08 22:33:00 +0000593 */
Luca Ceresolid3c65b02011-05-04 02:40:43 +0000594 if (miiphy_link(eth_get_dev()->name, CONFIG_SYS_FAULT_MII_ADDR))
Uri Mashiach2d8d1902017-01-19 10:51:45 +0200595 status_led_set(CONFIG_LED_STATUS_RED, CONFIG_LED_STATUS_OFF);
Luca Ceresolid3c65b02011-05-04 02:40:43 +0000596 else
Uri Mashiach2d8d1902017-01-19 10:51:45 +0200597 status_led_set(CONFIG_LED_STATUS_RED, CONFIG_LED_STATUS_ON);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200598#endif /* CONFIG_SYS_FAULT_ECHO_LINK_DOWN, ... */
wdenkfc3e2162003-10-08 22:33:00 +0000599#endif /* CONFIG_MII, ... */
Jim Linb63056d2013-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 Hershberger22f6e992012-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 Roese29caf932022-09-02 14:10:46 +0200609 schedule();
Joe Hershbergerc5a75332015-12-21 16:31:35 -0600610 if (arp_timeout_check() > 0)
611 time_start = get_timer(0);
612
Viacheslav Mitrofanovffdbf3b2022-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 Hershberger60304592015-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 Liakhovetski40cb90e2008-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 Hershbergere94070c2012-05-23 07:59:24 +0000630 /* cancel any ARP that may not have completed */
Joe Hershberger049a95a2015-04-08 01:41:01 -0500631 net_arp_wait_packet_ip.s_addr = 0;
Joe Hershbergere94070c2012-05-23 07:59:24 +0000632
Joe Hershbergerece223b2012-05-23 07:59:15 +0000633 net_cleanup_loop();
wdenk8bde7f72003-06-27 21:31:46 +0000634 eth_halt();
Joe Hershbergerf8be7d62012-08-03 10:59:08 +0000635 /* Invalidate the last protocol */
636 eth_set_last_protocol(BOOTP);
637
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000638 puts("\nAbort\n");
Joe Hershberger4ef8d532012-05-23 08:01:04 +0000639 /* include a debug print as well incase the debug
640 messages are directed to stderr */
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500641 debug_cond(DEBUG_INT_STATE, "--- net_loop Abort!\n");
Michal Simek19a4fba2015-08-21 08:49:48 +0200642 ret = -EINTR;
Simon Glass4793ee62011-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 Hershbergerbc0571f2015-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 Loeliger643d1ab2007-07-09 17:45:14 -0500654#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000655#if defined(CONFIG_SYS_FAULT_ECHO_LINK_DOWN) && \
Uri Mashiach2d8d1902017-01-19 10:51:45 +0200656 defined(CONFIG_LED_STATUS) && \
657 defined(CONFIG_LED_STATUS_RED)
wdenkfc3e2162003-10-08 22:33:00 +0000658 /*
wdenk42d1f032003-10-15 23:53:47 +0000659 * Echo the inverted link state to the fault LED.
wdenkfc3e2162003-10-08 22:33:00 +0000660 */
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000661 if (miiphy_link(eth_get_dev()->name,
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500662 CONFIG_SYS_FAULT_MII_ADDR))
Uri Mashiach2d8d1902017-01-19 10:51:45 +0200663 status_led_set(CONFIG_LED_STATUS_RED,
664 CONFIG_LED_STATUS_OFF);
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500665 else
Uri Mashiach2d8d1902017-01-19 10:51:45 +0200666 status_led_set(CONFIG_LED_STATUS_RED,
667 CONFIG_LED_STATUS_ON);
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000668#endif /* CONFIG_SYS_FAULT_ECHO_LINK_DOWN, ... */
wdenkfc3e2162003-10-08 22:33:00 +0000669#endif /* CONFIG_MII, ... */
Joe Hershbergerbc0571f2015-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 Hershberger5c421332015-03-22 17:09:07 -0500676 if (net_state == NETLOOP_FAIL)
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500677 ret = net_start_again();
wdenk2d966952002-10-31 22:12:35 +0000678
Joe Hershberger22f6e992012-05-23 07:59:14 +0000679 switch (net_state) {
wdenk2d966952002-10-31 22:12:35 +0000680 case NETLOOP_RESTART:
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500681 net_restarted = 1;
wdenk2d966952002-10-31 22:12:35 +0000682 goto restart;
683
684 case NETLOOP_SUCCESS:
Joe Hershbergerece223b2012-05-23 07:59:15 +0000685 net_cleanup_loop();
Joe Hershberger14111572015-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 Glass018f5302017-08-03 12:22:10 -0600689 env_set_hex("filesize", net_boot_file_size);
Simon Glassbb872dd2019-12-28 10:45:02 -0700690 env_set_hex("fileaddr", image_load_addr);
wdenk2d966952002-10-31 22:12:35 +0000691 }
Samuel Mendoza-Jonas09bd3d02022-08-08 21:46:03 +0930692 if (protocol != NETCONS && protocol != NCSI)
Joe Hershbergerf8be7d62012-08-03 10:59:08 +0000693 eth_halt();
694 else
695 eth_halt_state_only();
696
697 eth_set_last_protocol(protocol);
698
Joe Hershberger14111572015-04-08 01:41:02 -0500699 ret = net_boot_file_size;
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500700 debug_cond(DEBUG_INT_STATE, "--- net_loop Success!\n");
Simon Glass4793ee62011-10-24 18:00:01 +0000701 goto done;
wdenk2d966952002-10-31 22:12:35 +0000702
703 case NETLOOP_FAIL:
Joe Hershbergerece223b2012-05-23 07:59:15 +0000704 net_cleanup_loop();
Joe Hershbergerf8be7d62012-08-03 10:59:08 +0000705 /* Invalidate the last protocol */
706 eth_set_last_protocol(BOOTP);
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500707 debug_cond(DEBUG_INT_STATE, "--- net_loop Fail!\n");
Thomas RIENOESSLa735e6e2018-11-21 15:56:07 +0100708 ret = -ENONET;
Simon Glass4793ee62011-10-24 18:00:01 +0000709 goto done;
Joe Hershberger22f6e992012-05-23 07:59:14 +0000710
711 case NETLOOP_CONTINUE:
712 continue;
wdenk2d966952002-10-31 22:12:35 +0000713 }
714 }
Simon Glass4793ee62011-10-24 18:00:01 +0000715
716done:
Jim Linb63056d2013-08-13 19:03:05 +0800717#ifdef CONFIG_USB_KEYBOARD
718 net_busy_flag = 0;
719#endif
Simon Glass39bccd22011-10-26 14:18:38 +0000720#ifdef CONFIG_CMD_TFTPPUT
Simon Glass4793ee62011-10-24 18:00:01 +0000721 /* Clear out the handlers */
Joe Hershbergerece223b2012-05-23 07:59:15 +0000722 net_set_udp_handler(NULL);
Simon Glass4793ee62011-10-24 18:00:01 +0000723 net_set_icmp_handler(NULL);
Simon Glass39bccd22011-10-26 14:18:38 +0000724#endif
Leonid Iziumtsev60177b22018-05-08 15:55:50 +0200725 net_set_state(prev_net_state);
Ramon Fried3eaac632019-07-18 21:43:30 +0300726
727#if defined(CONFIG_CMD_PCAP)
728 if (pcap_active())
729 pcap_print_status();
730#endif
Simon Glass4793ee62011-10-24 18:00:01 +0000731 return ret;
wdenk2d966952002-10-31 22:12:35 +0000732}
733
734/**********************************************************************/
735
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500736static void start_again_timeout_handler(void)
wdenk2d966952002-10-31 22:12:35 +0000737{
Joe Hershberger22f6e992012-05-23 07:59:14 +0000738 net_set_state(NETLOOP_RESTART);
wdenk2d966952002-10-31 22:12:35 +0000739}
740
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500741int net_start_again(void)
wdenk2d966952002-10-31 22:12:35 +0000742{
wdenk6e592382004-04-18 17:39:38 +0000743 char *nretry;
Remy Bohmer67b96e82009-10-28 22:13:39 +0100744 int retry_forever = 0;
745 unsigned long retrycnt = 0;
Joe Hershberger60304592015-03-22 17:09:24 -0500746 int ret;
wdenka3d991b2004-04-15 21:48:45 +0000747
Simon Glass00caae62017-08-03 12:22:12 -0600748 nretry = env_get("netretry");
Remy Bohmer67b96e82009-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 Hershberger5c421332015-03-22 17:09:07 -0500758 } else {
759 retrycnt = 0;
760 retry_forever = 0;
761 }
Remy Bohmer67b96e82009-10-28 22:13:39 +0100762
Leonid Iziumtsev17d413b2018-03-09 15:29:06 +0100763 if ((!retry_forever) && (net_try_count > retrycnt)) {
Remy Bohmer67b96e82009-10-28 22:13:39 +0100764 eth_halt();
Joe Hershberger22f6e992012-05-23 07:59:14 +0000765 net_set_state(NETLOOP_FAIL);
Joe Hershberger60304592015-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;
wdenka3d991b2004-04-15 21:48:45 +0000771 }
Remy Bohmer67b96e82009-10-28 22:13:39 +0100772
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500773 net_try_count++;
Remy Bohmer67b96e82009-10-28 22:13:39 +0100774
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000775 eth_halt();
Matthias Fuchs8b0c5c12007-12-27 16:58:41 +0100776#if !defined(CONFIG_NET_DO_NOT_TRY_ANOTHER)
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500777 eth_try_another(!net_restarted);
Matthias Fuchs8b0c5c12007-12-27 16:58:41 +0100778#endif
Joe Hershberger60304592015-03-22 17:09:24 -0500779 ret = eth_init();
Joe Hershbergerbc0571f2015-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 Hershbergerece223b2012-05-23 07:59:15 +0000785 net_set_udp_handler(NULL);
wdenk6e592382004-04-18 17:39:38 +0000786 } else {
Joe Hershberger22f6e992012-05-23 07:59:14 +0000787 net_set_state(NETLOOP_FAIL);
wdenk2d966952002-10-31 22:12:35 +0000788 }
wdenk6e592382004-04-18 17:39:38 +0000789 } else {
Joe Hershberger22f6e992012-05-23 07:59:14 +0000790 net_set_state(NETLOOP_RESTART);
wdenk2d966952002-10-31 22:12:35 +0000791 }
Joe Hershberger60304592015-03-22 17:09:24 -0500792 return ret;
wdenk2d966952002-10-31 22:12:35 +0000793}
794
795/**********************************************************************/
796/*
797 * Miscelaneous bits.
798 */
799
Joe Hershbergerece223b2012-05-23 07:59:15 +0000800static void dummy_handler(uchar *pkt, unsigned dport,
Joe Hershberger049a95a2015-04-08 01:41:01 -0500801 struct in_addr sip, unsigned sport,
Joe Hershbergerece223b2012-05-23 07:59:15 +0000802 unsigned len)
Joe Hershbergerd280d3f2012-05-23 07:58:01 +0000803{
Joe Hershbergerd280d3f2012-05-23 07:58:01 +0000804}
805
Joe Hershbergerece223b2012-05-23 07:59:15 +0000806rxhand_f *net_get_udp_handler(void)
wdenk2d966952002-10-31 22:12:35 +0000807{
Joe Hershbergerece223b2012-05-23 07:59:15 +0000808 return udp_packet_handler;
809}
810
811void net_set_udp_handler(rxhand_f *f)
812{
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500813 debug_cond(DEBUG_INT_STATE, "--- net_loop UDP handler set (%p)\n", f);
Joe Hershbergerece223b2012-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)
821{
822 return arp_packet_handler;
823}
824
825void net_set_arp_handler(rxhand_f *f)
826{
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500827 debug_cond(DEBUG_INT_STATE, "--- net_loop ARP handler set (%p)\n", f);
Joe Hershbergerece223b2012-05-23 07:59:15 +0000828 if (f == NULL)
829 arp_packet_handler = dummy_handler;
830 else
831 arp_packet_handler = f;
wdenk2d966952002-10-31 22:12:35 +0000832}
833
Simon Glass39bccd22011-10-26 14:18:38 +0000834#ifdef CONFIG_CMD_TFTPPUT
Simon Glass4793ee62011-10-24 18:00:01 +0000835void net_set_icmp_handler(rxhand_icmp_f *f)
836{
837 packet_icmp_handler = f;
838}
Simon Glass39bccd22011-10-26 14:18:38 +0000839#endif
wdenk2d966952002-10-31 22:12:35 +0000840
Joe Hershbergerbc0571f2015-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 Hershberger4ef8d532012-05-23 08:01:04 +0000844 debug_cond(DEBUG_INT_STATE,
Joe Hershbergerbc0571f2015-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 Hershberger4ef8d532012-05-23 08:01:04 +0000848 debug_cond(DEBUG_INT_STATE,
Joe Hershbergerbc0571f2015-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 Hershbergerac3f26c2018-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 Hershberger1203fcc2015-04-08 01:41:05 -0500864int net_send_udp_packet(uchar *ether, struct in_addr dest, int dport, int sport,
Joe Hershberger206d07f2012-05-23 07:58:10 +0000865 int payload_len)
wdenk73a8b272003-06-05 19:27:42 +0000866{
Duncan Hare5d457ec2018-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)a3bf1932022-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 Hare5d457ec2018-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{
wdenka3d991b2004-04-15 21:48:45 +0000885 uchar *pkt;
Joe Hershberger92146372012-05-23 07:59:08 +0000886 int eth_hdr_size;
887 int pkt_hdr_size;
wdenka3d991b2004-04-15 21:48:45 +0000888
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500889 /* make sure the net_tx_packet is initialized (net_init() was called) */
Joe Hershberger1203fcc2015-04-08 01:41:05 -0500890 assert(net_tx_packet != NULL);
891 if (net_tx_packet == NULL)
Joe Hershberger46c495d2012-05-23 07:59:22 +0000892 return -1;
893
wdenk73a8b272003-06-05 19:27:42 +0000894 /* convert to new style broadcast */
Joe Hershberger049a95a2015-04-08 01:41:01 -0500895 if (dest.s_addr == 0)
896 dest.s_addr = 0xFFFFFFFF;
wdenk2d966952002-10-31 22:12:35 +0000897
wdenk73a8b272003-06-05 19:27:42 +0000898 /* if broadcast, make the ether address a broadcast and don't do ARP */
Joe Hershberger049a95a2015-04-08 01:41:01 -0500899 if (dest.s_addr == 0xFFFFFFFF)
Joe Hershberger0adb5b72015-04-08 01:41:04 -0500900 ether = (uchar *)net_bcast_ethaddr;
wdenk73a8b272003-06-05 19:27:42 +0000901
Joe Hershberger1203fcc2015-04-08 01:41:05 -0500902 pkt = (uchar *)net_tx_packet;
wdenk73a8b272003-06-05 19:27:42 +0000903
Joe Hershberger1203fcc2015-04-08 01:41:05 -0500904 eth_hdr_size = net_set_ether(pkt, ether, PROT_IP);
Duncan Hare5d457ec2018-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)a3bf1932022-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 Hare5d457ec2018-06-24 15:40:41 -0700920 default:
921 return -EINVAL;
922 }
Robin Getz0ebf04c2009-07-23 03:01:03 -0400923
Joe Hershbergere94070c2012-05-23 07:59:24 +0000924 /* if MAC address was not discovered yet, do an ARP request */
Joe Hershberger0adb5b72015-04-08 01:41:04 -0500925 if (memcmp(ether, net_null_ethaddr, 6) == 0) {
Joe Hershberger4ef8d532012-05-23 08:01:04 +0000926 debug_cond(DEBUG_DEV_PKT, "sending ARP for %pI4\n", &dest);
Joe Hershberger92146372012-05-23 07:59:08 +0000927
928 /* save the ip and eth addr for the packet to send after arp */
Joe Hershberger049a95a2015-04-08 01:41:01 -0500929 net_arp_wait_packet_ip = dest;
Joe Hershberger85d25e02015-04-08 01:41:08 -0500930 arp_wait_packet_ethaddr = ether;
wdenka3d991b2004-04-15 21:48:45 +0000931
wdenk73a8b272003-06-05 19:27:42 +0000932 /* size of the waiting packet */
Joe Hershberger85d25e02015-04-08 01:41:08 -0500933 arp_wait_tx_packet_size = pkt_hdr_size + payload_len;
wdenk73a8b272003-06-05 19:27:42 +0000934
935 /* and do the ARP request */
Joe Hershberger85d25e02015-04-08 01:41:08 -0500936 arp_wait_try = 1;
937 arp_wait_timer_start = get_timer(0);
938 arp_request();
wdenk73a8b272003-06-05 19:27:42 +0000939 return 1; /* waiting */
Joe Hershberger92146372012-05-23 07:59:08 +0000940 } else {
Joe Hershberger4ef8d532012-05-23 08:01:04 +0000941 debug_cond(DEBUG_DEV_PKT, "sending UDP to %pI4/%pM\n",
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500942 &dest, ether);
Joe Hershberger1203fcc2015-04-08 01:41:05 -0500943 net_send_packet(net_tx_packet, pkt_hdr_size + payload_len);
Joe Hershberger92146372012-05-23 07:59:08 +0000944 return 0; /* transmitted */
wdenk73a8b272003-06-05 19:27:42 +0000945 }
wdenk73a8b272003-06-05 19:27:42 +0000946}
947
Alessandro Rubini5cfaa4e2009-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 Hershbergeraa7a6482016-08-15 14:42:15 -0500954#define IP_PKTSIZE (CONFIG_NET_MAXDEFRAG)
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +0200955
Joe Hershbergerc5c59df2012-05-23 07:58:05 +0000956#define IP_MAXUDP (IP_PKTSIZE - IP_HDR_SIZE)
Alessandro Rubini5cfaa4e2009-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 Hershbergerbc0571f2015-04-08 01:41:21 -0500970static struct ip_udp_hdr *__net_defragment(struct ip_udp_hdr *ip, int *lenp)
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +0200971{
Joe Hershberger48522bb2012-05-15 08:59:08 +0000972 static uchar pkt_buff[IP_PKTSIZE] __aligned(PKTALIGN);
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +0200973 static u16 first_hole, total_len;
974 struct hole *payload, *thisfrag, *h, *newh;
Joe Hershberger594c26f2012-05-23 07:58:04 +0000975 struct ip_udp_hdr *localip = (struct ip_udp_hdr *)pkt_buff;
Alessandro Rubini5cfaa4e2009-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 Villemoes1817c382022-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 Estevamb85d1302022-05-26 11:14:37 -0300985 return NULL;
986
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +0200987 /* payload starts after IP header, this fragment is in there */
Joe Hershbergerc5c59df2012-05-23 07:58:05 +0000988 payload = (struct hole *)(pkt_buff + IP_HDR_SIZE);
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +0200989 offset8 = (ip_off & IP_OFFS);
990 thisfrag = payload + offset8;
991 start = offset8 * 8;
Joe Hershbergerc5c59df2012-05-23 07:58:05 +0000992 len = ntohs(ip->ip_len) - IP_HDR_SIZE;
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +0200993
Rasmus Villemoes1817c382022-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 Rubini5cfaa4e2009-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 Hershbergerc5c59df2012-05-23 07:58:05 +00001009 memcpy(localip, ip, IP_HDR_SIZE);
Alessandro Rubini5cfaa4e2009-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 Stephanee397e592010-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 Rubini5cfaa4e2009-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 Villemoes06869682022-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 Rubini5cfaa4e2009-08-07 13:58:56 +02001046 */
Rasmus Villemoes06869682022-10-17 09:52:51 +02001047 if (h->last_byte < start + len)
1048 len = h->last_byte - start;
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +02001049
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001050 if ((h >= thisfrag) && (h->last_byte <= start + len)) {
Alessandro Rubini5cfaa4e2009-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 Hershbergerc5c59df2012-05-23 07:58:05 +00001096 memcpy((uchar *)thisfrag, indata + IP_HDR_SIZE, len);
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +02001097 if (!done)
1098 return NULL;
1099
Joe Hershbergerc5c59df2012-05-23 07:58:05 +00001100 *lenp = total_len + IP_HDR_SIZE;
Rasmus Villemoes06653c72022-10-14 19:43:40 +02001101 localip->ip_len = htons(*lenp);
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +02001102 return localip;
1103}
1104
Joe Hershbergerbc0571f2015-04-08 01:41:21 -05001105static inline struct ip_udp_hdr *net_defragment(struct ip_udp_hdr *ip,
1106 int *lenp)
Alessandro Rubini5cfaa4e2009-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 Hershbergerbc0571f2015-04-08 01:41:21 -05001111 return __net_defragment(ip, lenp);
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +02001112}
1113
1114#else /* !CONFIG_IP_DEFRAG */
1115
Joe Hershbergerbc0571f2015-04-08 01:41:21 -05001116static inline struct ip_udp_hdr *net_defragment(struct ip_udp_hdr *ip,
1117 int *lenp)
Alessandro Rubini5cfaa4e2009-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
wdenka3d991b2004-04-15 21:48:45 +00001125
Simon Glass8f79bb12011-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 Hershberger594c26f2012-05-23 07:58:04 +00001132static void receive_icmp(struct ip_udp_hdr *ip, int len,
Joe Hershberger049a95a2015-04-08 01:41:01 -05001133 struct in_addr src_ip, struct ethernet_hdr *et)
Simon Glass8f79bb12011-10-24 18:00:00 +00001134{
Joe Hershbergere0a63072012-05-23 07:58:09 +00001135 struct icmp_hdr *icmph = (struct icmp_hdr *)&ip->udp_src;
Simon Glass8f79bb12011-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 Hershbergerbc0571f2015-04-08 01:41:21 -05001142 &icmph->un.gateway);
Simon Glass8f79bb12011-10-24 18:00:00 +00001143 break;
Simon Glass8f79bb12011-10-24 18:00:00 +00001144 default:
Joe Hershbergera36b12f2012-05-23 07:58:02 +00001145#if defined(CONFIG_CMD_PING)
1146 ping_receive(et, ip, len);
1147#endif
Simon Glass39bccd22011-10-26 14:18:38 +00001148#ifdef CONFIG_CMD_TFTPPUT
Simon Glass4793ee62011-10-24 18:00:01 +00001149 if (packet_icmp_handler)
1150 packet_icmp_handler(icmph->type, icmph->code,
Joe Hershbergerbc0571f2015-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 Glass39bccd22011-10-26 14:18:38 +00001154#endif
Simon Glass8f79bb12011-10-24 18:00:00 +00001155 break;
1156 }
1157}
1158
Joe Hershberger2a504df2015-03-22 17:09:11 -05001159void net_process_received_packet(uchar *in_packet, int len)
wdenk2d966952002-10-31 22:12:35 +00001160{
Joe Hershbergercb487f52012-05-23 07:58:06 +00001161 struct ethernet_hdr *et;
Joe Hershberger594c26f2012-05-23 07:58:04 +00001162 struct ip_udp_hdr *ip;
Joe Hershberger049a95a2015-04-08 01:41:01 -05001163 struct in_addr dst_ip;
1164 struct in_addr src_ip;
Joe Hershberger8d353eb2012-05-23 07:58:12 +00001165 int eth_proto;
Jon Loeliger643d1ab2007-07-09 17:45:14 -05001166#if defined(CONFIG_CMD_CDP)
wdenka3d991b2004-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 Hershberger4ef8d532012-05-23 08:01:04 +00001171 debug_cond(DEBUG_NET_PKT, "packet received\n");
wdenka3d991b2004-04-15 21:48:45 +00001172
Ramon Fried3eaac632019-07-18 21:43:30 +03001173#if defined(CONFIG_CMD_PCAP)
1174 pcap_post(in_packet, len, false);
1175#endif
Joe Hershberger1203fcc2015-04-08 01:41:05 -05001176 net_rx_packet = in_packet;
1177 net_rx_packet_len = len;
Joe Hershberger2a504df2015-03-22 17:09:11 -05001178 et = (struct ethernet_hdr *)in_packet;
wdenka3d991b2004-04-15 21:48:45 +00001179
1180 /* too small packet? */
1181 if (len < ETHER_HDR_SIZE)
1182 return;
1183
Alexander Graf0efe1bc2016-05-06 21:01:01 +02001184#if defined(CONFIG_API) || defined(CONFIG_EFI_LOADER)
Rafal Jaworowskif85b6072007-12-27 18:19:02 +01001185 if (push_packet) {
Joe Hershberger2a504df2015-03-22 17:09:11 -05001186 (*push_packet)(in_packet, len);
Rafal Jaworowskif85b6072007-12-27 18:19:02 +01001187 return;
1188 }
1189#endif
1190
Jon Loeliger643d1ab2007-07-09 17:45:14 -05001191#if defined(CONFIG_CMD_CDP)
wdenka3d991b2004-04-15 21:48:45 +00001192 /* keep track if packet is CDP */
Joe Hershberger17351882012-05-23 07:58:00 +00001193 iscdp = is_cdp_packet(et->et_dest);
wdenka3d991b2004-04-15 21:48:45 +00001194#endif
1195
Joe Hershberger4fd50552015-04-08 01:41:17 -05001196 myvlanid = ntohs(net_our_vlan);
wdenka3d991b2004-04-15 21:48:45 +00001197 if (myvlanid == (ushort)-1)
1198 myvlanid = VLAN_NONE;
Joe Hershberger4fd50552015-04-08 01:41:17 -05001199 mynvlanid = ntohs(net_native_vlan);
wdenka3d991b2004-04-15 21:48:45 +00001200 if (mynvlanid == (ushort)-1)
1201 mynvlanid = VLAN_NONE;
wdenk2d966952002-10-31 22:12:35 +00001202
Joe Hershberger8d353eb2012-05-23 07:58:12 +00001203 eth_proto = ntohs(et->et_protlen);
wdenk2d966952002-10-31 22:12:35 +00001204
Joe Hershberger8d353eb2012-05-23 07:58:12 +00001205 if (eth_proto < 1514) {
Joe Hershbergercb487f52012-05-23 07:58:06 +00001206 struct e802_hdr *et802 = (struct e802_hdr *)et;
wdenk2d966952002-10-31 22:12:35 +00001207 /*
Joe Hershbergerda5ebe22012-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 Hershberger8d353eb2012-05-23 07:58:12 +00001211 eth_proto = ntohs(et802->et_prot);
wdenka3d991b2004-04-15 21:48:45 +00001212
Joe Hershberger2a504df2015-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;
wdenka3d991b2004-04-15 21:48:45 +00001215
Joe Hershberger8d353eb2012-05-23 07:58:12 +00001216 } else if (eth_proto != PROT_VLAN) { /* normal packet */
Joe Hershberger2a504df2015-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;
wdenka3d991b2004-04-15 21:48:45 +00001219
1220 } else { /* VLAN packet */
Joe Hershbergerc68cca32012-05-23 07:58:07 +00001221 struct vlan_ethernet_hdr *vet =
1222 (struct vlan_ethernet_hdr *)et;
wdenka3d991b2004-04-15 21:48:45 +00001223
Joe Hershberger4ef8d532012-05-23 08:01:04 +00001224 debug_cond(DEBUG_NET_PKT, "VLAN packet received\n");
Robin Getz0ebf04c2009-07-23 03:01:03 -04001225
wdenka3d991b2004-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 Hershberger4fd50552015-04-08 01:41:17 -05001231 if ((ntohs(net_our_vlan) & VLAN_IDMASK) == VLAN_NONE
Jon Loeliger643d1ab2007-07-09 17:45:14 -05001232#if defined(CONFIG_CMD_CDP)
wdenka3d991b2004-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 Hershberger8d353eb2012-05-23 07:58:12 +00001240 eth_proto = ntohs(vet->vet_type);
wdenka3d991b2004-04-15 21:48:45 +00001241
Joe Hershberger2a504df2015-03-22 17:09:11 -05001242 ip = (struct ip_udp_hdr *)(in_packet + VLAN_ETHER_HDR_SIZE);
wdenka3d991b2004-04-15 21:48:45 +00001243 len -= VLAN_ETHER_HDR_SIZE;
wdenk2d966952002-10-31 22:12:35 +00001244 }
1245
Joe Hershberger4ef8d532012-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 Loeliger643d1ab2007-07-09 17:45:14 -05001248#if defined(CONFIG_CMD_CDP)
wdenka3d991b2004-04-15 21:48:45 +00001249 if (iscdp) {
Joe Hershberger0b4c5ff2012-05-23 07:58:13 +00001250 cdp_receive((uchar *)ip, len);
wdenka3d991b2004-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 Hershberger8d353eb2012-05-23 07:58:12 +00001263 switch (eth_proto) {
wdenk2d966952002-10-31 22:12:35 +00001264 case PROT_ARP:
Joe Hershberger85d25e02015-04-08 01:41:08 -05001265 arp_receive(et, ip, len);
wdenk289f9322005-01-12 00:15:14 +00001266 break;
wdenk2d966952002-10-31 22:12:35 +00001267
Peter Tyserbf6cb242010-09-30 11:25:48 -05001268#ifdef CONFIG_CMD_RARP
wdenk2d966952002-10-31 22:12:35 +00001269 case PROT_RARP:
Joe Hershberger8b9c5322012-05-23 07:58:03 +00001270 rarp_receive(ip, len);
wdenk2d966952002-10-31 22:12:35 +00001271 break;
Peter Tyserbf6cb242010-09-30 11:25:48 -05001272#endif
Viacheslav Mitrofanovffdbf3b2022-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 Mitrofanov0d6d5a42022-12-06 10:08:16 +03001276 break;
Viacheslav Mitrofanovffdbf3b2022-12-02 12:18:06 +03001277#endif
wdenk2d966952002-10-31 22:12:35 +00001278 case PROT_IP:
Joe Hershberger4ef8d532012-05-23 08:01:04 +00001279 debug_cond(DEBUG_NET_PKT, "Got IP\n");
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +02001280 /* Before we start poking the header, make sure it is there */
Rasmus Villemoesad359d82022-10-14 19:43:38 +02001281 if (len < IP_HDR_SIZE) {
Joe Hershberger594c26f2012-05-23 07:58:04 +00001282 debug("len bad %d < %lu\n", len,
Rasmus Villemoesad359d82022-10-14 19:43:38 +02001283 (ulong)IP_HDR_SIZE);
wdenk2d966952002-10-31 22:12:35 +00001284 return;
1285 }
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +02001286 /* Check the packet length */
wdenk2d966952002-10-31 22:12:35 +00001287 if (len < ntohs(ip->ip_len)) {
Joe Hershberger4ef8d532012-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 Villemoesad359d82022-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 Hershberger4ef8d532012-05-23 08:01:04 +00001296 debug_cond(DEBUG_NET_PKT, "len=%d, v=%02x\n",
Joe Hershbergerbc0571f2015-04-08 01:41:21 -05001297 len, ip->ip_hl_v & 0xff);
Robin Getz0ebf04c2009-07-23 03:01:03 -04001298
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +02001299 /* Can't deal with anything except IPv4 */
Luca Ceresolid3c65b02011-05-04 02:40:43 +00001300 if ((ip->ip_hl_v & 0xf0) != 0x40)
wdenk2d966952002-10-31 22:12:35 +00001301 return;
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +02001302 /* Can't deal with IP options (headers != 20 bytes) */
Rasmus Villemoesb0fcc482022-10-14 19:43:37 +02001303 if ((ip->ip_hl_v & 0x0f) != 0x05)
Remy Bohmer6b52cfe2008-06-03 15:48:17 +02001304 return;
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +02001305 /* Check the Checksum of the header */
Simon Glass0da0fcd2015-01-19 22:16:08 -07001306 if (!ip_checksum_ok((uchar *)ip, IP_HDR_SIZE)) {
Joe Hershberger4ef8d532012-05-23 08:01:04 +00001307 debug("checksum bad\n");
wdenk2d966952002-10-31 22:12:35 +00001308 return;
1309 }
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +02001310 /* If it is not for us, ignore it */
Joe Hershberger049a95a2015-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 Ceresolic819abe2011-05-04 02:40:46 +00001314 return;
wdenk2d966952002-10-31 22:12:35 +00001315 }
Luca Ceresoli03eb1292011-04-18 06:19:50 +00001316 /* Read source IP address for later use */
Joe Hershberger049a95a2015-04-08 01:41:01 -05001317 src_ip = net_read_ip(&ip->ip_src);
wdenk2d966952002-10-31 22:12:35 +00001318 /*
Alessandro Rubini5cfaa4e2009-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 Hershbergerbc0571f2015-04-08 01:41:21 -05001323 ip = net_defragment(ip, &len);
Luca Ceresoliccb9ebe2011-05-04 02:40:47 +00001324 if (!ip)
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +02001325 return;
1326 /*
wdenk2d966952002-10-31 22:12:35 +00001327 * watch for ICMP host redirects
1328 *
wdenk8bde7f72003-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 *
wdenk8bde7f72003-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 Glass4793ee62011-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 Glass8f79bb12011-10-24 18:00:00 +00001348 receive_icmp(ip, len, src_ip, et);
1349 return;
Ying-Chun Liu (PaulLiu)a3bf1932022-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 Villemoes06653c72022-10-14 19:43:40 +02001363 if (ntohs(ip->udp_len) < UDP_HDR_SIZE || ntohs(ip->udp_len) > len - IP_HDR_SIZE)
liucheng (G)fe728802019-08-29 13:47:33 +00001364 return;
1365
Joe Hershberger4ef8d532012-05-23 08:01:04 +00001366 debug_cond(DEBUG_DEV_PKT,
Joe Hershbergerbc0571f2015-04-08 01:41:21 -05001367 "received UDP (to=%pI4, from=%pI4, len=%d)\n",
1368 &dst_ip, &src_ip, len);
Joe Hershberger4ef8d532012-05-23 08:01:04 +00001369
Simon Glass4b37fd12021-12-18 11:27:49 -07001370 if (IS_ENABLED(CONFIG_UDP_CHECKSUM) && ip->udp_xsum != 0) {
Wolfgang Denkb2f50802005-08-12 23:43:12 +02001371 ulong xsum;
Heinrich Schuchardt85244232019-11-05 12:48:19 +01001372 u8 *sumptr;
Stefan Roese8534bf92005-08-12 20:06:52 +02001373 ushort sumlen;
1374
1375 xsum = ip->ip_p;
1376 xsum += (ntohs(ip->udp_len));
Joe Hershberger049a95a2015-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 Roese8534bf92005-08-12 20:06:52 +02001381
1382 sumlen = ntohs(ip->udp_len);
Heinrich Schuchardt85244232019-11-05 12:48:19 +01001383 sumptr = (u8 *)&ip->udp_src;
Stefan Roese8534bf92005-08-12 20:06:52 +02001384
1385 while (sumlen > 1) {
Heinrich Schuchardt85244232019-11-05 12:48:19 +01001386 /* inlined ntohs() to avoid alignment errors */
1387 xsum += (sumptr[0] << 8) + sumptr[1];
1388 sumptr += 2;
Stefan Roese8534bf92005-08-12 20:06:52 +02001389 sumlen -= 2;
1390 }
Heinrich Schuchardt85244232019-11-05 12:48:19 +01001391 if (sumlen > 0)
1392 xsum += (sumptr[0] << 8) + sumptr[0];
Stefan Roese8534bf92005-08-12 20:06:52 +02001393 while ((xsum >> 16) != 0) {
Luca Ceresoli3e38e422011-05-11 03:59:54 +00001394 xsum = (xsum & 0x0000ffff) +
1395 ((xsum >> 16) & 0x0000ffff);
Stefan Roese8534bf92005-08-12 20:06:52 +02001396 }
1397 if ((xsum != 0x00000000) && (xsum != 0x0000ffff)) {
Wolfgang Denk9b55a252008-07-11 01:16:00 +02001398 printf(" UDP wrong checksum %08lx %08x\n",
Joe Hershbergerbc0571f2015-04-08 01:41:21 -05001399 xsum, ntohs(ip->udp_xsum));
Stefan Roese8534bf92005-08-12 20:06:52 +02001400 return;
1401 }
1402 }
Stefan Roese8534bf92005-08-12 20:06:52 +02001403
Holger Dengler66c89ee2017-07-20 10:10:55 +02001404#if defined(CONFIG_NETCONSOLE) && !defined(CONFIG_SPL_BUILD)
Joe Hershberger594c26f2012-05-23 07:58:04 +00001405 nc_input_packet((uchar *)ip + IP_UDP_HDR_SIZE,
Joe Hershbergerece223b2012-05-23 07:59:15 +00001406 src_ip,
Joe Hershbergerbc0571f2015-04-08 01:41:21 -05001407 ntohs(ip->udp_dst),
Joe Hershbergerece223b2012-05-23 07:59:15 +00001408 ntohs(ip->udp_src),
1409 ntohs(ip->udp_len) - UDP_HDR_SIZE);
Joe Hershbergerbc0571f2015-04-08 01:41:21 -05001410#endif
1411 /*
1412 * IP header OK. Pass the packet to the current handler.
1413 */
1414 (*udp_packet_handler)((uchar *)ip + IP_UDP_HDR_SIZE,
1415 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 Feltend8970da2018-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-Jonas09bd3d02022-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 Glasse4bf0c52011-10-24 18:00:02 +00001435static int net_check_prereq(enum proto_t protocol)
wdenk2d966952002-10-31 22:12:35 +00001436{
1437 switch (protocol) {
wdenk6e592382004-04-18 17:39:38 +00001438 /* Fall through */
Jon Loeliger643d1ab2007-07-09 17:45:14 -05001439#if defined(CONFIG_CMD_PING)
wdenk73a8b272003-06-05 19:27:42 +00001440 case PING:
Joe Hershberger049a95a2015-04-08 01:41:01 -05001441 if (net_ping_ip.s_addr == 0) {
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001442 puts("*** ERROR: ping address not given\n");
Luca Ceresoli92895de2011-05-04 02:40:45 +00001443 return 1;
wdenk6e592382004-04-18 17:39:38 +00001444 }
1445 goto common;
wdenk73a8b272003-06-05 19:27:42 +00001446#endif
Viacheslav Mitrofanoveeb0a2c2022-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 Getz1a32bf42009-07-20 14:53:54 -04001455#if defined(CONFIG_CMD_DNS)
1456 case DNS:
Joe Hershberger049a95a2015-04-08 01:41:01 -05001457 if (net_dns_server.s_addr == 0) {
Robin Getz1a32bf42009-07-20 14:53:54 -04001458 puts("*** ERROR: DNS server address not given\n");
1459 return 1;
1460 }
1461 goto common;
1462#endif
Philippe Reynesb43ea1b2020-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 Loeliger643d1ab2007-07-09 17:45:14 -05001470#if defined(CONFIG_CMD_NFS)
wdenkcbd8a352004-02-24 02:00:03 +00001471 case NFS:
1472#endif
Joe Hershbergerbc0571f2015-04-08 01:41:21 -05001473 /* Fall through */
Simon Glasse4bf0c52011-10-24 18:00:02 +00001474 case TFTPGET:
Simon Glass1fb7cd42011-10-24 18:00:07 +00001475 case TFTPPUT:
Viacheslav Mitrofanov7fbf2302022-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 Ceresoli4f63acd2011-05-11 03:59:56 +00001484 puts("*** ERROR: `serverip' not set\n");
Luca Ceresoli92895de2011-05-04 02:40:45 +00001485 return 1;
wdenk6e592382004-04-18 17:39:38 +00001486 }
Philippe Reynes912ece42020-09-18 14:13:02 +02001487#if defined(CONFIG_CMD_PING) || \
Philippe Reynesb43ea1b2020-09-18 14:13:00 +02001488 defined(CONFIG_CMD_DNS) || defined(CONFIG_PROT_UDP)
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001489common:
wdenk73a8b272003-06-05 19:27:42 +00001490#endif
Simon Guinot8b6bbe12011-05-01 23:38:40 +00001491 /* Fall through */
wdenk73a8b272003-06-05 19:27:42 +00001492
Simon Guinot8b6bbe12011-05-01 23:38:40 +00001493 case NETCONS:
Alex Kiernanf73a7df2018-05-29 15:30:53 +00001494 case FASTBOOT:
Luca Ceresoli7a83af02011-05-17 00:03:40 +00001495 case TFTPSRV:
Viacheslav Mitrofanov7fbf2302022-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 Ceresoli4f63acd2011-05-11 03:59:56 +00001503 puts("*** ERROR: `ipaddr' not set\n");
Luca Ceresoli92895de2011-05-04 02:40:45 +00001504 return 1;
wdenk6e592382004-04-18 17:39:38 +00001505 }
1506 /* Fall through */
wdenk2d966952002-10-31 22:12:35 +00001507
Peter Tyserbf6cb242010-09-30 11:25:48 -05001508#ifdef CONFIG_CMD_RARP
wdenk2d966952002-10-31 22:12:35 +00001509 case RARP:
Peter Tyserbf6cb242010-09-30 11:25:48 -05001510#endif
Samuel Mendoza-Jonas09bd3d02022-08-08 21:46:03 +09301511#ifdef CONFIG_PHY_NCSI
1512 case NCSI:
1513#endif
wdenk2d966952002-10-31 22:12:35 +00001514 case BOOTP:
wdenka3d991b2004-04-15 21:48:45 +00001515 case CDP:
Peter Tyserbf6cb242010-09-30 11:25:48 -05001516 case DHCP:
Joe Hershbergerd22c3382012-05-23 08:00:12 +00001517 case LINKLOCAL:
Joe Hershberger0adb5b72015-04-08 01:41:04 -05001518 if (memcmp(net_ethaddr, "\0\0\0\0\0\0", 6) == 0) {
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001519 int num = eth_get_dev_index();
wdenk2d966952002-10-31 22:12:35 +00001520
wdenk6e592382004-04-18 17:39:38 +00001521 switch (num) {
1522 case -1:
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001523 puts("*** ERROR: No ethernet found.\n");
Luca Ceresoli92895de2011-05-04 02:40:45 +00001524 return 1;
wdenk6e592382004-04-18 17:39:38 +00001525 case 0:
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001526 puts("*** ERROR: `ethaddr' not set\n");
wdenk2d966952002-10-31 22:12:35 +00001527 break;
wdenk6e592382004-04-18 17:39:38 +00001528 default:
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001529 printf("*** ERROR: `eth%daddr' not set\n",
Joe Hershbergerbc0571f2015-04-08 01:41:21 -05001530 num);
wdenk2d966952002-10-31 22:12:35 +00001531 break;
wdenk2d966952002-10-31 22:12:35 +00001532 }
wdenk6e592382004-04-18 17:39:38 +00001533
Joe Hershbergerbc0571f2015-04-08 01:41:21 -05001534 net_start_again();
Luca Ceresoli92895de2011-05-04 02:40:45 +00001535 return 2;
wdenk6e592382004-04-18 17:39:38 +00001536 }
1537 /* Fall through */
1538 default:
Luca Ceresoli92895de2011-05-04 02:40:45 +00001539 return 0;
wdenk2d966952002-10-31 22:12:35 +00001540 }
Luca Ceresoli92895de2011-05-04 02:40:45 +00001541 return 0; /* OK */
wdenk2d966952002-10-31 22:12:35 +00001542}
1543/**********************************************************************/
1544
1545int
Joe Hershberger1203fcc2015-04-08 01:41:05 -05001546net_eth_hdr_size(void)
wdenka3d991b2004-04-15 21:48:45 +00001547{
1548 ushort myvlanid;
wdenk2d966952002-10-31 22:12:35 +00001549
Joe Hershberger4fd50552015-04-08 01:41:17 -05001550 myvlanid = ntohs(net_our_vlan);
wdenka3d991b2004-04-15 21:48:45 +00001551 if (myvlanid == (ushort)-1)
1552 myvlanid = VLAN_NONE;
1553
Luca Ceresoli3e38e422011-05-11 03:59:54 +00001554 return ((myvlanid & VLAN_IDMASK) == VLAN_NONE) ? ETHER_HDR_SIZE :
1555 VLAN_ETHER_HDR_SIZE;
wdenka3d991b2004-04-15 21:48:45 +00001556}
1557
Joe Hershberger1203fcc2015-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 Hershbergercb487f52012-05-23 07:58:06 +00001560 struct ethernet_hdr *et = (struct ethernet_hdr *)xet;
wdenka3d991b2004-04-15 21:48:45 +00001561 ushort myvlanid;
1562
Joe Hershberger4fd50552015-04-08 01:41:17 -05001563 myvlanid = ntohs(net_our_vlan);
wdenka3d991b2004-04-15 21:48:45 +00001564 if (myvlanid == (ushort)-1)
1565 myvlanid = VLAN_NONE;
wdenk2d966952002-10-31 22:12:35 +00001566
Joe Hershberger0adb5b72015-04-08 01:41:04 -05001567 memcpy(et->et_dest, dest_ethaddr, 6);
1568 memcpy(et->et_src, net_ethaddr, 6);
wdenka3d991b2004-04-15 21:48:45 +00001569 if ((myvlanid & VLAN_IDMASK) == VLAN_NONE) {
Luca Ceresolic819abe2011-05-04 02:40:46 +00001570 et->et_protlen = htons(prot);
wdenka3d991b2004-04-15 21:48:45 +00001571 return ETHER_HDR_SIZE;
1572 } else {
Joe Hershbergerc68cca32012-05-23 07:58:07 +00001573 struct vlan_ethernet_hdr *vet =
1574 (struct vlan_ethernet_hdr *)xet;
wdenk2d966952002-10-31 22:12:35 +00001575
wdenka3d991b2004-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 Hershbergere7111012012-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 Hershberger0adb5b72015-04-08 01:41:04 -05001588 memcpy(et->et_src, net_ethaddr, 6);
Joe Hershbergere7111012012-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 Hare5d457ec2018-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 Hershberger4b11c912012-05-23 07:59:07 +00001609 struct ip_udp_hdr *ip = (struct ip_udp_hdr *)pkt;
1610
1611 /*
1612 * Construct an IP header.
1613 */
1614 /* IP_HDR_SIZE / 4 (not including UDP) */
1615 ip->ip_hl_v = 0x45;
1616 ip->ip_tos = 0;
Duncan Hare5d457ec2018-06-24 15:40:41 -07001617 ip->ip_len = htons(pkt_len);
1618 ip->ip_p = proto;
Joe Hershbergerbc0571f2015-04-08 01:41:21 -05001619 ip->ip_id = htons(net_ip_id++);
Joe Hershberger4b11c912012-05-23 07:59:07 +00001620 ip->ip_off = htons(IP_FLAGS_DFRAG); /* Don't fragment */
1621 ip->ip_ttl = 255;
1622 ip->ip_sum = 0;
1623 /* already in network byte order */
Joe Hershberger049a95a2015-04-08 01:41:01 -05001624 net_copy_ip((void *)&ip->ip_src, &source);
Joe Hershberger4b11c912012-05-23 07:59:07 +00001625 /* already in network byte order */
Joe Hershberger049a95a2015-04-08 01:41:01 -05001626 net_copy_ip((void *)&ip->ip_dst, &dest);
Duncan Hare5d457ec2018-06-24 15:40:41 -07001627
1628 ip->ip_sum = compute_ip_checksum(ip, IP_HDR_SIZE);
Joe Hershberger4b11c912012-05-23 07:59:07 +00001629}
1630
Joe Hershberger049a95a2015-04-08 01:41:01 -05001631void net_set_udp_header(uchar *pkt, struct in_addr dest, int dport, int sport,
Joe Hershberger4b11c912012-05-23 07:59:07 +00001632 int len)
1633{
1634 struct ip_udp_hdr *ip = (struct ip_udp_hdr *)pkt;
wdenk2d966952002-10-31 22:12:35 +00001635
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)
Joe Hershberger4b11c912012-05-23 07:59:07 +00001642 pkt[IP_UDP_HDR_SIZE + len] = 0;
wdenk2d966952002-10-31 22:12:35 +00001643
Duncan Hare5d457ec2018-06-24 15:40:41 -07001644 net_set_ip_header(pkt, dest, net_ip, IP_UDP_HDR_SIZE + len,
1645 IPPROTO_UDP);
Joe Hershberger4b11c912012-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 Hershberger594c26f2012-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 Ceresoli4f63acd2011-05-11 03:59:56 +00001653void copy_filename(char *dst, const char *src, int size)
wdenk2d966952002-10-31 22:12:35 +00001654{
Joe Hershberger16cf1452018-07-03 19:36:41 -05001655 if (src && *src && (*src == '"')) {
wdenk2d966952002-10-31 22:12:35 +00001656 ++src;
1657 --size;
1658 }
1659
Joe Hershberger16cf1452018-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 Hershberger3a66fcb2018-07-03 19:36:39 -05001665int is_serverip_in_cmd(void)
1666{
1667 return !!strchr(net_boot_file_name, ':');
1668}
1669
Joe Hershberger6ab12832018-07-03 19:36:43 -05001670int net_parse_bootfile(struct in_addr *ipaddr, char *filename, int max_len)
1671{
1672 char *colon;
Lyle Franklin85f8e032022-04-16 11:36:43 -04001673 struct in_addr ip;
1674 ip.s_addr = 0;
Joe Hershberger6ab12832018-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 Franklin85f8e032022-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 Hershberger6ab12832018-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 Hershberger049a95a2015-04-08 01:41:01 -05001695void ip_to_string(struct in_addr x, char *s)
wdenk2d966952002-10-31 22:12:35 +00001696{
Joe Hershberger049a95a2015-04-08 01:41:01 -05001697 x.s_addr = ntohl(x.s_addr);
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001698 sprintf(s, "%d.%d.%d.%d",
Joe Hershberger049a95a2015-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)
wdenk6e592382004-04-18 17:39:38 +00001703 );
wdenk2d966952002-10-31 22:12:35 +00001704}
1705
Joe Hershberger4fd50552015-04-08 01:41:17 -05001706void vlan_to_string(ushort x, char *s)
wdenka3d991b2004-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 Hershberger4fd50552015-04-08 01:41:17 -05001719ushort string_to_vlan(const char *s)
wdenka3d991b2004-04-15 21:48:45 +00001720{
1721 ushort id;
1722
1723 if (s == NULL)
wdenkb9711de2004-04-25 13:18:40 +00001724 return htons(VLAN_NONE);
wdenka3d991b2004-04-15 21:48:45 +00001725
1726 if (*s < '0' || *s > '9')
1727 id = VLAN_NONE;
1728 else
Simon Glass0b1284e2021-07-24 09:03:30 -06001729 id = (ushort)dectoul(s, NULL);
wdenka3d991b2004-04-15 21:48:45 +00001730
wdenkb9711de2004-04-25 13:18:40 +00001731 return htons(id);
wdenka3d991b2004-04-15 21:48:45 +00001732}
1733
Simon Glass723806c2017-08-03 12:22:15 -06001734ushort env_get_vlan(char *var)
wdenka3d991b2004-04-15 21:48:45 +00001735{
Simon Glass00caae62017-08-03 12:22:12 -06001736 return string_to_vlan(env_get(var));
wdenka3d991b2004-04-15 21:48:45 +00001737}