blob: aca20e43b0b71c959543a35d1ae17d2882ec6073 [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>
Alex Kiernanf73a7df2018-05-29 15:30:53 +000094#include <net/fastboot.h>
Lukasz Majewski34696952015-08-24 00:21:43 +020095#include <net/tftp.h>
Samuel Mendoza-Jonas09bd3d02022-08-08 21:46:03 +093096#include <net/ncsi.h>
Ramon Fried3eaac632019-07-18 21:43:30 +030097#if defined(CONFIG_CMD_PCAP)
98#include <net/pcap.h>
99#endif
Philippe Reynesb43ea1b2020-09-18 14:13:00 +0200100#include <net/udp.h>
Uri Mashiach2d8d1902017-01-19 10:51:45 +0200101#if defined(CONFIG_LED_STATUS)
Joe Hershberger4545f4e2012-05-23 07:58:15 +0000102#include <miiphy.h>
103#include <status_led.h>
104#endif
105#include <watchdog.h>
106#include <linux/compiler.h>
Joe Hershbergerd280d3f2012-05-23 07:58:01 +0000107#include "arp.h"
wdenk2d966952002-10-31 22:12:35 +0000108#include "bootp.h"
Joe Hershbergerf575ae12012-05-23 07:57:59 +0000109#include "cdp.h"
Robin Getz1a32bf42009-07-20 14:53:54 -0400110#if defined(CONFIG_CMD_DNS)
111#include "dns.h"
112#endif
Joe Hershbergerd22c3382012-05-23 08:00:12 +0000113#include "link_local.h"
Joe Hershberger4545f4e2012-05-23 07:58:15 +0000114#include "nfs.h"
Joe Hershbergera36b12f2012-05-23 07:58:02 +0000115#include "ping.h"
Joe Hershberger4545f4e2012-05-23 07:58:15 +0000116#include "rarp.h"
Lothar Feltend8970da2018-06-22 22:29:54 +0200117#if defined(CONFIG_CMD_WOL)
118#include "wol.h"
119#endif
Ying-Chun Liu (PaulLiu)a3bf1932022-11-08 14:17:28 +0800120#include <net/tcp.h>
Ying-Chun Liu (PaulLiu)cfbae482022-11-08 14:17:29 +0800121#include <net/wget.h>
wdenk2d966952002-10-31 22:12:35 +0000122
wdenk2d966952002-10-31 22:12:35 +0000123/** BOOTP EXTENTIONS **/
124
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000125/* Our subnet mask (0=unknown) */
Joe Hershberger049a95a2015-04-08 01:41:01 -0500126struct in_addr net_netmask;
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000127/* Our gateways IP address */
Joe Hershberger049a95a2015-04-08 01:41:01 -0500128struct in_addr net_gateway;
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000129/* Our DNS IP address */
Joe Hershberger049a95a2015-04-08 01:41:01 -0500130struct in_addr net_dns_server;
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500131#if defined(CONFIG_BOOTP_DNS2)
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000132/* Our 2nd DNS IP address */
Joe Hershberger049a95a2015-04-08 01:41:01 -0500133struct in_addr net_dns_server2;
stroesefe389a82003-08-28 14:17:32 +0000134#endif
wdenk2d966952002-10-31 22:12:35 +0000135
136/** END OF BOOTP EXTENTIONS **/
137
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000138/* Our ethernet address */
Joe Hershberger0adb5b72015-04-08 01:41:04 -0500139u8 net_ethaddr[6];
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000140/* Boot server enet address */
Joe Hershberger0adb5b72015-04-08 01:41:04 -0500141u8 net_server_ethaddr[6];
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000142/* Our IP addr (0 = unknown) */
Joe Hershberger049a95a2015-04-08 01:41:01 -0500143struct in_addr net_ip;
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000144/* Server IP addr (0 = unknown) */
Joe Hershberger049a95a2015-04-08 01:41:01 -0500145struct in_addr net_server_ip;
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000146/* Current receive packet */
Joe Hershberger1203fcc2015-04-08 01:41:05 -0500147uchar *net_rx_packet;
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000148/* Current rx packet length */
Joe Hershberger1203fcc2015-04-08 01:41:05 -0500149int net_rx_packet_len;
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000150/* IP packet ID */
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500151static unsigned net_ip_id;
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000152/* Ethernet bcast address */
Joe Hershberger0adb5b72015-04-08 01:41:04 -0500153const u8 net_bcast_ethaddr[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
154const u8 net_null_ethaddr[6];
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200155#if defined(CONFIG_API) || defined(CONFIG_EFI_LOADER)
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500156void (*push_packet)(void *, int len) = 0;
Rafal Jaworowskif85b6072007-12-27 18:19:02 +0100157#endif
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000158/* Network loop state */
Joe Hershberger22f6e992012-05-23 07:59:14 +0000159enum net_loop_state net_state;
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000160/* Tried all network devices */
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500161int net_restart_wrap;
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000162/* Network loop restarted */
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500163static int net_restarted;
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000164/* At least one device configured */
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500165static int net_dev_exists;
wdenk2d966952002-10-31 22:12:35 +0000166
wdenk6e592382004-04-18 17:39:38 +0000167/* XXX in both little & big endian machines 0xFFFF == ntohs(-1) */
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000168/* default is without VLAN */
Joe Hershberger4fd50552015-04-08 01:41:17 -0500169ushort net_our_vlan = 0xFFFF;
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000170/* ditto */
Joe Hershberger4fd50552015-04-08 01:41:17 -0500171ushort net_native_vlan = 0xFFFF;
wdenka3d991b2004-04-15 21:48:45 +0000172
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000173/* Boot File name */
Jacob Stiffler11a69ff2015-09-30 10:12:05 -0400174char net_boot_file_name[1024];
Alexander Graf449312c2018-06-15 10:29:27 +0200175/* Indicates whether the file name was specified on the command line */
176bool net_boot_file_name_explicit;
Joe Hershberger14111572015-04-08 01:41:02 -0500177/* The actual transferred size of the bootfile (in bytes) */
178u32 net_boot_file_size;
179/* Boot file size in blocks as reported by the DHCP server */
180u32 net_boot_file_expected_size_in_blocks;
wdenk2d966952002-10-31 22:12:35 +0000181
Joe Hershberger1203fcc2015-04-08 01:41:05 -0500182static uchar net_pkt_buf[(PKTBUFSRX+1) * PKTSIZE_ALIGN + PKTALIGN];
Joe Hershberger2a504df2015-03-22 17:09:11 -0500183/* Receive packets */
184uchar *net_rx_packets[PKTBUFSRX];
Joe Hershbergerece223b2012-05-23 07:59:15 +0000185/* Current UDP RX packet handler */
186static rxhand_f *udp_packet_handler;
187/* Current ARP RX packet handler */
188static rxhand_f *arp_packet_handler;
Simon Glass39bccd22011-10-26 14:18:38 +0000189#ifdef CONFIG_CMD_TFTPPUT
Joe Hershbergerece223b2012-05-23 07:59:15 +0000190/* Current ICMP rx handler */
191static rxhand_icmp_f *packet_icmp_handler;
Simon Glass39bccd22011-10-26 14:18:38 +0000192#endif
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000193/* Current timeout handler */
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500194static thand_f *time_handler;
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000195/* Time base value */
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500196static ulong time_start;
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000197/* Current timeout value */
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500198static ulong time_delta;
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000199/* THE transmit packet */
Joe Hershberger1203fcc2015-04-08 01:41:05 -0500200uchar *net_tx_packet;
wdenk2d966952002-10-31 22:12:35 +0000201
Simon Glasse4bf0c52011-10-24 18:00:02 +0000202static int net_check_prereq(enum proto_t protocol);
wdenk2d966952002-10-31 22:12:35 +0000203
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500204static int net_try_count;
Remy Bohmer67b96e82009-10-28 22:13:39 +0100205
Jim Linb63056d2013-08-13 19:03:05 +0800206int __maybe_unused net_busy_flag;
207
wdenk2d966952002-10-31 22:12:35 +0000208/**********************************************************************/
wdenk73a8b272003-06-05 19:27:42 +0000209
Joe Hershbergerfd305632015-05-20 14:27:23 -0500210static int on_ipaddr(const char *name, const char *value, enum env_op op,
211 int flags)
212{
213 if (flags & H_PROGRAMMATIC)
214 return 0;
215
216 net_ip = string_to_ip(value);
217
218 return 0;
219}
220U_BOOT_ENV_CALLBACK(ipaddr, on_ipaddr);
221
222static int on_gatewayip(const char *name, const char *value, enum env_op op,
223 int flags)
224{
225 if (flags & H_PROGRAMMATIC)
226 return 0;
227
228 net_gateway = string_to_ip(value);
229
230 return 0;
231}
232U_BOOT_ENV_CALLBACK(gatewayip, on_gatewayip);
233
234static int on_netmask(const char *name, const char *value, enum env_op op,
235 int flags)
236{
237 if (flags & H_PROGRAMMATIC)
238 return 0;
239
240 net_netmask = string_to_ip(value);
241
242 return 0;
243}
244U_BOOT_ENV_CALLBACK(netmask, on_netmask);
245
246static int on_serverip(const char *name, const char *value, enum env_op op,
247 int flags)
248{
249 if (flags & H_PROGRAMMATIC)
250 return 0;
251
252 net_server_ip = string_to_ip(value);
253
254 return 0;
255}
256U_BOOT_ENV_CALLBACK(serverip, on_serverip);
257
258static int on_nvlan(const char *name, const char *value, enum env_op op,
259 int flags)
260{
261 if (flags & H_PROGRAMMATIC)
262 return 0;
263
264 net_native_vlan = string_to_vlan(value);
265
266 return 0;
267}
268U_BOOT_ENV_CALLBACK(nvlan, on_nvlan);
269
270static int on_vlan(const char *name, const char *value, enum env_op op,
271 int flags)
272{
273 if (flags & H_PROGRAMMATIC)
274 return 0;
275
276 net_our_vlan = string_to_vlan(value);
277
278 return 0;
279}
280U_BOOT_ENV_CALLBACK(vlan, on_vlan);
281
282#if defined(CONFIG_CMD_DNS)
283static int on_dnsip(const char *name, const char *value, enum env_op op,
284 int flags)
285{
286 if (flags & H_PROGRAMMATIC)
287 return 0;
288
289 net_dns_server = string_to_ip(value);
290
291 return 0;
292}
293U_BOOT_ENV_CALLBACK(dnsip, on_dnsip);
294#endif
295
Simon Glasse4a3d572011-10-27 06:24:32 +0000296/*
297 * Check if autoload is enabled. If so, use either NFS or TFTP to download
298 * the boot file.
299 */
300void net_auto_load(void)
301{
Tom Rinia6ab4b52019-12-05 19:35:07 -0500302#if defined(CONFIG_CMD_NFS) && !defined(CONFIG_SPL_BUILD)
Simon Glass00caae62017-08-03 12:22:12 -0600303 const char *s = env_get("autoload");
Simon Glasse4a3d572011-10-27 06:24:32 +0000304
Joe Hershbergerec8a2522012-12-11 22:16:22 -0600305 if (s != NULL && strcmp(s, "NFS") == 0) {
Joe Hershberger3855cad2018-07-03 19:36:40 -0500306 if (net_check_prereq(NFS)) {
307/* We aren't expecting to get a serverip, so just accept the assigned IP */
Simon Glass43407532021-12-18 11:27:52 -0700308 if (IS_ENABLED(CONFIG_BOOTP_SERVERIP)) {
309 net_set_state(NETLOOP_SUCCESS);
310 } else {
311 printf("Cannot autoload with NFS\n");
312 net_set_state(NETLOOP_FAIL);
313 }
Joe Hershberger3855cad2018-07-03 19:36:40 -0500314 return;
315 }
Joe Hershbergerec8a2522012-12-11 22:16:22 -0600316 /*
317 * Use NFS to load the bootfile.
318 */
Joe Hershberger68c76a32015-04-08 01:41:10 -0500319 nfs_start();
Joe Hershbergerec8a2522012-12-11 22:16:22 -0600320 return;
321 }
Simon Glasse4a3d572011-10-27 06:24:32 +0000322#endif
Simon Glassbfebc8c2017-08-03 12:22:13 -0600323 if (env_get_yesno("autoload") == 0) {
Joe Hershbergerec8a2522012-12-11 22:16:22 -0600324 /*
325 * Just use BOOTP/RARP to configure system;
326 * Do not use TFTP to load the bootfile.
327 */
328 net_set_state(NETLOOP_SUCCESS);
329 return;
Simon Glasse4a3d572011-10-27 06:24:32 +0000330 }
Joe Hershberger3855cad2018-07-03 19:36:40 -0500331 if (net_check_prereq(TFTPGET)) {
332/* We aren't expecting to get a serverip, so just accept the assigned IP */
Simon Glass43407532021-12-18 11:27:52 -0700333 if (IS_ENABLED(CONFIG_BOOTP_SERVERIP)) {
334 net_set_state(NETLOOP_SUCCESS);
335 } else {
336 printf("Cannot autoload with TFTPGET\n");
337 net_set_state(NETLOOP_FAIL);
338 }
Joe Hershberger3855cad2018-07-03 19:36:40 -0500339 return;
340 }
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500341 tftp_start(TFTPGET);
Simon Glasse4a3d572011-10-27 06:24:32 +0000342}
343
Sean Andersonc3f02782020-09-12 17:45:43 -0400344static int net_init_loop(void)
Heiko Schocher2f70c492009-02-10 09:38:52 +0100345{
Jim Lin7315cfd2013-05-17 17:41:03 +0800346 if (eth_get_dev())
Joe Hershberger0adb5b72015-04-08 01:41:04 -0500347 memcpy(net_ethaddr, eth_get_ethaddr(), 6);
Sean Andersonc3f02782020-09-12 17:45:43 -0400348 else
349 /*
350 * Not ideal, but there's no way to get the actual error, and I
351 * don't feel like fixing all the users of eth_get_dev to deal
352 * with errors.
353 */
354 return -ENONET;
Michael Zaidman3c172c42009-04-04 01:43:00 +0300355
Sean Andersonc3f02782020-09-12 17:45:43 -0400356 return 0;
Heiko Schocher2f70c492009-02-10 09:38:52 +0100357}
358
Joe Hershbergerece223b2012-05-23 07:59:15 +0000359static void net_clear_handlers(void)
360{
361 net_set_udp_handler(NULL);
362 net_set_arp_handler(NULL);
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500363 net_set_timeout_handler(0, NULL);
Joe Hershbergerece223b2012-05-23 07:59:15 +0000364}
365
366static void net_cleanup_loop(void)
367{
368 net_clear_handlers();
369}
370
Sean Andersonc3f02782020-09-12 17:45:43 -0400371int net_init(void)
Joe Hershberger46c495d2012-05-23 07:59:22 +0000372{
373 static int first_call = 1;
374
375 if (first_call) {
376 /*
377 * Setup packet buffers, aligned correctly.
378 */
379 int i;
380
Joe Hershberger1203fcc2015-04-08 01:41:05 -0500381 net_tx_packet = &net_pkt_buf[0] + (PKTALIGN - 1);
382 net_tx_packet -= (ulong)net_tx_packet % PKTALIGN;
Joe Hershberger2a504df2015-03-22 17:09:11 -0500383 for (i = 0; i < PKTBUFSRX; i++) {
Joe Hershberger1203fcc2015-04-08 01:41:05 -0500384 net_rx_packets[i] = net_tx_packet +
385 (i + 1) * PKTSIZE_ALIGN;
Joe Hershberger2a504df2015-03-22 17:09:11 -0500386 }
Joe Hershberger85d25e02015-04-08 01:41:08 -0500387 arp_init();
Joe Hershberger46c495d2012-05-23 07:59:22 +0000388 net_clear_handlers();
389
390 /* Only need to setup buffer pointers once. */
391 first_call = 0;
Ying-Chun Liu (PaulLiu)a3bf1932022-11-08 14:17:28 +0800392 if (IS_ENABLED(CONFIG_PROT_TCP))
393 tcp_set_tcp_state(TCP_CLOSED);
Joe Hershberger46c495d2012-05-23 07:59:22 +0000394 }
395
Sean Andersonc3f02782020-09-12 17:45:43 -0400396 return net_init_loop();
Joe Hershberger46c495d2012-05-23 07:59:22 +0000397}
398
wdenk73a8b272003-06-05 19:27:42 +0000399/**********************************************************************/
wdenk2d966952002-10-31 22:12:35 +0000400/*
401 * Main network processing loop.
402 */
403
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500404int net_loop(enum proto_t protocol)
wdenk2d966952002-10-31 22:12:35 +0000405{
Joe Hershberger60304592015-03-22 17:09:24 -0500406 int ret = -EINVAL;
Leonid Iziumtsev60177b22018-05-08 15:55:50 +0200407 enum net_loop_state prev_net_state = net_state;
wdenk2d966952002-10-31 22:12:35 +0000408
Marek Szyprowskidef7a5c2020-06-15 11:15:57 +0200409#if defined(CONFIG_CMD_PING)
410 if (protocol != PING)
411 net_ping_ip.s_addr = 0;
412#endif
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500413 net_restarted = 0;
414 net_dev_exists = 0;
415 net_try_count = 1;
416 debug_cond(DEBUG_INT_STATE, "--- net_loop Entry\n");
wdenk73a8b272003-06-05 19:27:42 +0000417
Samuel Mendoza-Jonas09bd3d02022-08-08 21:46:03 +0930418#ifdef CONFIG_PHY_NCSI
419 if (phy_interface_is_ncsi() && protocol != NCSI && !ncsi_active()) {
420 printf("%s: configuring NCSI first\n", __func__);
421 if (net_loop(NCSI) < 0)
422 return ret;
423 eth_init_state_only();
424 goto restart;
425 }
426#endif
427
Simon Glass573f14f2011-12-10 11:08:06 +0000428 bootstage_mark_name(BOOTSTAGE_ID_ETH_START, "eth_start");
Joe Hershberger46c495d2012-05-23 07:59:22 +0000429 net_init();
Yang Liud9506cd2020-12-21 14:44:39 +1100430 if (eth_is_on_demand_init()) {
wdenkb1bf6f22005-04-03 14:52:59 +0000431 eth_halt();
Joe Hershbergerf8be7d62012-08-03 10:59:08 +0000432 eth_set_current();
Joe Hershberger60304592015-03-22 17:09:24 -0500433 ret = eth_init();
434 if (ret < 0) {
Joe Hershbergerf8be7d62012-08-03 10:59:08 +0000435 eth_halt();
Joe Hershberger60304592015-03-22 17:09:24 -0500436 return ret;
Joe Hershbergerf8be7d62012-08-03 10:59:08 +0000437 }
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500438 } else {
Joe Hershbergerd2eaec62015-03-22 17:09:06 -0500439 eth_init_state_only();
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500440 }
Samuel Mendoza-Jonas4b290d42022-08-08 21:46:04 +0930441
wdenk2d966952002-10-31 22:12:35 +0000442restart:
Jim Linb63056d2013-08-13 19:03:05 +0800443#ifdef CONFIG_USB_KEYBOARD
444 net_busy_flag = 0;
445#endif
Joe Hershberger22f6e992012-05-23 07:59:14 +0000446 net_set_state(NETLOOP_CONTINUE);
wdenk2d966952002-10-31 22:12:35 +0000447
448 /*
449 * Start the ball rolling with the given start function. From
450 * here on, this code is a state machine driven by received
451 * packets and timer events.
452 */
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500453 debug_cond(DEBUG_INT_STATE, "--- net_loop Init\n");
454 net_init_loop();
wdenk2d966952002-10-31 22:12:35 +0000455
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000456 switch (net_check_prereq(protocol)) {
wdenk2d966952002-10-31 22:12:35 +0000457 case 1:
458 /* network not configured */
wdenkb1bf6f22005-04-03 14:52:59 +0000459 eth_halt();
Leonid Iziumtsev60177b22018-05-08 15:55:50 +0200460 net_set_state(prev_net_state);
Joe Hershberger60304592015-03-22 17:09:24 -0500461 return -ENODEV;
wdenk2d966952002-10-31 22:12:35 +0000462
wdenk2d966952002-10-31 22:12:35 +0000463 case 2:
464 /* network device not configured */
465 break;
wdenk2d966952002-10-31 22:12:35 +0000466
467 case 0:
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500468 net_dev_exists = 1;
Joe Hershberger14111572015-04-08 01:41:02 -0500469 net_boot_file_size = 0;
wdenk2d966952002-10-31 22:12:35 +0000470 switch (protocol) {
Krebs, Olaf808f13d2020-03-09 14:27:55 +0000471#ifdef CONFIG_CMD_TFTPBOOT
Simon Glasse4bf0c52011-10-24 18:00:02 +0000472 case TFTPGET:
Simon Glass1fb7cd42011-10-24 18:00:07 +0000473#ifdef CONFIG_CMD_TFTPPUT
474 case TFTPPUT:
475#endif
wdenk2d966952002-10-31 22:12:35 +0000476 /* always use ARP to get server ethernet address */
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500477 tftp_start(protocol);
wdenk2d966952002-10-31 22:12:35 +0000478 break;
Krebs, Olaf808f13d2020-03-09 14:27:55 +0000479#endif
Luca Ceresoli7a83af02011-05-17 00:03:40 +0000480#ifdef CONFIG_CMD_TFTPSRV
481 case TFTPSRV:
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500482 tftp_start_server();
Luca Ceresoli7a83af02011-05-17 00:03:40 +0000483 break;
484#endif
Alex Kiernanf73a7df2018-05-29 15:30:53 +0000485#ifdef CONFIG_UDP_FUNCTION_FASTBOOT
486 case FASTBOOT:
487 fastboot_start_server();
488 break;
489#endif
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500490#if defined(CONFIG_CMD_DHCP)
wdenk2d966952002-10-31 22:12:35 +0000491 case DHCP:
Joe Hershberger7044c6b2015-04-08 01:41:09 -0500492 bootp_reset();
Joe Hershberger049a95a2015-04-08 01:41:01 -0500493 net_ip.s_addr = 0;
Joe Hershberger7044c6b2015-04-08 01:41:09 -0500494 dhcp_request(); /* Basically same as BOOTP */
wdenk2d966952002-10-31 22:12:35 +0000495 break;
Jon Loeliger610f2e92007-07-10 11:05:02 -0500496#endif
Krebs, Olaf808f13d2020-03-09 14:27:55 +0000497#if defined(CONFIG_CMD_BOOTP)
wdenk2d966952002-10-31 22:12:35 +0000498 case BOOTP:
Joe Hershberger7044c6b2015-04-08 01:41:09 -0500499 bootp_reset();
Joe Hershberger049a95a2015-04-08 01:41:01 -0500500 net_ip.s_addr = 0;
Joe Hershberger7044c6b2015-04-08 01:41:09 -0500501 bootp_request();
wdenk2d966952002-10-31 22:12:35 +0000502 break;
Krebs, Olaf808f13d2020-03-09 14:27:55 +0000503#endif
Peter Tyserbf6cb242010-09-30 11:25:48 -0500504#if defined(CONFIG_CMD_RARP)
wdenk2d966952002-10-31 22:12:35 +0000505 case RARP:
Joe Hershberger698d78e2015-04-08 01:41:11 -0500506 rarp_try = 0;
Joe Hershberger049a95a2015-04-08 01:41:01 -0500507 net_ip.s_addr = 0;
Joe Hershberger698d78e2015-04-08 01:41:11 -0500508 rarp_request();
wdenk2d966952002-10-31 22:12:35 +0000509 break;
Peter Tyserbf6cb242010-09-30 11:25:48 -0500510#endif
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500511#if defined(CONFIG_CMD_PING)
wdenk73a8b272003-06-05 19:27:42 +0000512 case PING:
Joe Hershbergera36b12f2012-05-23 07:58:02 +0000513 ping_start();
wdenk73a8b272003-06-05 19:27:42 +0000514 break;
515#endif
Tom Rinia6ab4b52019-12-05 19:35:07 -0500516#if defined(CONFIG_CMD_NFS) && !defined(CONFIG_SPL_BUILD)
wdenkcbd8a352004-02-24 02:00:03 +0000517 case NFS:
Joe Hershberger68c76a32015-04-08 01:41:10 -0500518 nfs_start();
wdenkcbd8a352004-02-24 02:00:03 +0000519 break;
520#endif
Ying-Chun Liu (PaulLiu)cfbae482022-11-08 14:17:29 +0800521#if defined(CONFIG_CMD_WGET)
522 case WGET:
523 wget_start();
524 break;
525#endif
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500526#if defined(CONFIG_CMD_CDP)
wdenka3d991b2004-04-15 21:48:45 +0000527 case CDP:
Joe Hershberger6aede5b2015-04-08 01:41:14 -0500528 cdp_start();
wdenka3d991b2004-04-15 21:48:45 +0000529 break;
530#endif
Holger Dengler66c89ee2017-07-20 10:10:55 +0200531#if defined(CONFIG_NETCONSOLE) && !defined(CONFIG_SPL_BUILD)
wdenk68ceb292004-08-02 21:11:11 +0000532 case NETCONS:
Joe Hershberger6a38a5f2015-04-08 01:41:16 -0500533 nc_start();
wdenk68ceb292004-08-02 21:11:11 +0000534 break;
535#endif
Robin Getz1a32bf42009-07-20 14:53:54 -0400536#if defined(CONFIG_CMD_DNS)
537 case DNS:
Joe Hershberger786eac52015-04-08 01:41:15 -0500538 dns_start();
Robin Getz1a32bf42009-07-20 14:53:54 -0400539 break;
540#endif
Joe Hershbergerd22c3382012-05-23 08:00:12 +0000541#if defined(CONFIG_CMD_LINK_LOCAL)
542 case LINKLOCAL:
543 link_local_start();
544 break;
545#endif
Lothar Feltend8970da2018-06-22 22:29:54 +0200546#if defined(CONFIG_CMD_WOL)
547 case WOL:
548 wol_start();
549 break;
550#endif
Samuel Mendoza-Jonas09bd3d02022-08-08 21:46:03 +0930551#if defined(CONFIG_PHY_NCSI)
552 case NCSI:
553 ncsi_probe_packages();
554 break;
555#endif
wdenk2d966952002-10-31 22:12:35 +0000556 default:
557 break;
558 }
559
Philippe Reynesb43ea1b2020-09-18 14:13:00 +0200560 if (IS_ENABLED(CONFIG_PROT_UDP) && protocol == UDP)
561 udp_start();
562
wdenk2d966952002-10-31 22:12:35 +0000563 break;
564 }
565
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500566#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000567#if defined(CONFIG_SYS_FAULT_ECHO_LINK_DOWN) && \
Uri Mashiach2d8d1902017-01-19 10:51:45 +0200568 defined(CONFIG_LED_STATUS) && \
569 defined(CONFIG_LED_STATUS_RED)
wdenkfc3e2162003-10-08 22:33:00 +0000570 /*
wdenk42d1f032003-10-15 23:53:47 +0000571 * Echo the inverted link state to the fault LED.
wdenkfc3e2162003-10-08 22:33:00 +0000572 */
Luca Ceresolid3c65b02011-05-04 02:40:43 +0000573 if (miiphy_link(eth_get_dev()->name, CONFIG_SYS_FAULT_MII_ADDR))
Uri Mashiach2d8d1902017-01-19 10:51:45 +0200574 status_led_set(CONFIG_LED_STATUS_RED, CONFIG_LED_STATUS_OFF);
Luca Ceresolid3c65b02011-05-04 02:40:43 +0000575 else
Uri Mashiach2d8d1902017-01-19 10:51:45 +0200576 status_led_set(CONFIG_LED_STATUS_RED, CONFIG_LED_STATUS_ON);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200577#endif /* CONFIG_SYS_FAULT_ECHO_LINK_DOWN, ... */
wdenkfc3e2162003-10-08 22:33:00 +0000578#endif /* CONFIG_MII, ... */
Jim Linb63056d2013-08-13 19:03:05 +0800579#ifdef CONFIG_USB_KEYBOARD
580 net_busy_flag = 1;
581#endif
wdenk2d966952002-10-31 22:12:35 +0000582
583 /*
584 * Main packet reception loop. Loop receiving packets until
Joe Hershberger22f6e992012-05-23 07:59:14 +0000585 * someone sets `net_state' to a state that terminates.
wdenk2d966952002-10-31 22:12:35 +0000586 */
587 for (;;) {
Stefan Roese29caf932022-09-02 14:10:46 +0200588 schedule();
Joe Hershbergerc5a75332015-12-21 16:31:35 -0600589 if (arp_timeout_check() > 0)
590 time_start = get_timer(0);
591
wdenk2d966952002-10-31 22:12:35 +0000592 /*
593 * Check the ethernet for a new packet. The ethernet
594 * receive routine will process it.
Joe Hershberger60304592015-03-22 17:09:24 -0500595 * Most drivers return the most recent packet size, but not
596 * errors that may have happened.
wdenk2d966952002-10-31 22:12:35 +0000597 */
Guennadi Liakhovetski40cb90e2008-04-03 17:04:19 +0200598 eth_rx();
wdenk2d966952002-10-31 22:12:35 +0000599
600 /*
601 * Abort if ctrl-c was pressed.
602 */
603 if (ctrlc()) {
Joe Hershbergere94070c2012-05-23 07:59:24 +0000604 /* cancel any ARP that may not have completed */
Joe Hershberger049a95a2015-04-08 01:41:01 -0500605 net_arp_wait_packet_ip.s_addr = 0;
Joe Hershbergere94070c2012-05-23 07:59:24 +0000606
Joe Hershbergerece223b2012-05-23 07:59:15 +0000607 net_cleanup_loop();
wdenk8bde7f72003-06-27 21:31:46 +0000608 eth_halt();
Joe Hershbergerf8be7d62012-08-03 10:59:08 +0000609 /* Invalidate the last protocol */
610 eth_set_last_protocol(BOOTP);
611
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000612 puts("\nAbort\n");
Joe Hershberger4ef8d532012-05-23 08:01:04 +0000613 /* include a debug print as well incase the debug
614 messages are directed to stderr */
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500615 debug_cond(DEBUG_INT_STATE, "--- net_loop Abort!\n");
Michal Simek19a4fba2015-08-21 08:49:48 +0200616 ret = -EINTR;
Simon Glass4793ee62011-10-24 18:00:01 +0000617 goto done;
wdenk2d966952002-10-31 22:12:35 +0000618 }
619
wdenk2d966952002-10-31 22:12:35 +0000620 /*
621 * Check for a timeout, and run the timeout handler
622 * if we have one.
623 */
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500624 if (time_handler &&
625 ((get_timer(0) - time_start) > time_delta)) {
wdenk2d966952002-10-31 22:12:35 +0000626 thand_f *x;
627
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500628#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000629#if defined(CONFIG_SYS_FAULT_ECHO_LINK_DOWN) && \
Uri Mashiach2d8d1902017-01-19 10:51:45 +0200630 defined(CONFIG_LED_STATUS) && \
631 defined(CONFIG_LED_STATUS_RED)
wdenkfc3e2162003-10-08 22:33:00 +0000632 /*
wdenk42d1f032003-10-15 23:53:47 +0000633 * Echo the inverted link state to the fault LED.
wdenkfc3e2162003-10-08 22:33:00 +0000634 */
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000635 if (miiphy_link(eth_get_dev()->name,
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500636 CONFIG_SYS_FAULT_MII_ADDR))
Uri Mashiach2d8d1902017-01-19 10:51:45 +0200637 status_led_set(CONFIG_LED_STATUS_RED,
638 CONFIG_LED_STATUS_OFF);
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500639 else
Uri Mashiach2d8d1902017-01-19 10:51:45 +0200640 status_led_set(CONFIG_LED_STATUS_RED,
641 CONFIG_LED_STATUS_ON);
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000642#endif /* CONFIG_SYS_FAULT_ECHO_LINK_DOWN, ... */
wdenkfc3e2162003-10-08 22:33:00 +0000643#endif /* CONFIG_MII, ... */
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500644 debug_cond(DEBUG_INT_STATE, "--- net_loop timeout\n");
645 x = time_handler;
646 time_handler = (thand_f *)0;
wdenk2d966952002-10-31 22:12:35 +0000647 (*x)();
648 }
649
Joe Hershberger5c421332015-03-22 17:09:07 -0500650 if (net_state == NETLOOP_FAIL)
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500651 ret = net_start_again();
wdenk2d966952002-10-31 22:12:35 +0000652
Joe Hershberger22f6e992012-05-23 07:59:14 +0000653 switch (net_state) {
wdenk2d966952002-10-31 22:12:35 +0000654 case NETLOOP_RESTART:
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500655 net_restarted = 1;
wdenk2d966952002-10-31 22:12:35 +0000656 goto restart;
657
658 case NETLOOP_SUCCESS:
Joe Hershbergerece223b2012-05-23 07:59:15 +0000659 net_cleanup_loop();
Joe Hershberger14111572015-04-08 01:41:02 -0500660 if (net_boot_file_size > 0) {
661 printf("Bytes transferred = %d (%x hex)\n",
662 net_boot_file_size, net_boot_file_size);
Simon Glass018f5302017-08-03 12:22:10 -0600663 env_set_hex("filesize", net_boot_file_size);
Simon Glassbb872dd2019-12-28 10:45:02 -0700664 env_set_hex("fileaddr", image_load_addr);
wdenk2d966952002-10-31 22:12:35 +0000665 }
Samuel Mendoza-Jonas09bd3d02022-08-08 21:46:03 +0930666 if (protocol != NETCONS && protocol != NCSI)
Joe Hershbergerf8be7d62012-08-03 10:59:08 +0000667 eth_halt();
668 else
669 eth_halt_state_only();
670
671 eth_set_last_protocol(protocol);
672
Joe Hershberger14111572015-04-08 01:41:02 -0500673 ret = net_boot_file_size;
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500674 debug_cond(DEBUG_INT_STATE, "--- net_loop Success!\n");
Simon Glass4793ee62011-10-24 18:00:01 +0000675 goto done;
wdenk2d966952002-10-31 22:12:35 +0000676
677 case NETLOOP_FAIL:
Joe Hershbergerece223b2012-05-23 07:59:15 +0000678 net_cleanup_loop();
Joe Hershbergerf8be7d62012-08-03 10:59:08 +0000679 /* Invalidate the last protocol */
680 eth_set_last_protocol(BOOTP);
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500681 debug_cond(DEBUG_INT_STATE, "--- net_loop Fail!\n");
Thomas RIENOESSLa735e6e2018-11-21 15:56:07 +0100682 ret = -ENONET;
Simon Glass4793ee62011-10-24 18:00:01 +0000683 goto done;
Joe Hershberger22f6e992012-05-23 07:59:14 +0000684
685 case NETLOOP_CONTINUE:
686 continue;
wdenk2d966952002-10-31 22:12:35 +0000687 }
688 }
Simon Glass4793ee62011-10-24 18:00:01 +0000689
690done:
Jim Linb63056d2013-08-13 19:03:05 +0800691#ifdef CONFIG_USB_KEYBOARD
692 net_busy_flag = 0;
693#endif
Simon Glass39bccd22011-10-26 14:18:38 +0000694#ifdef CONFIG_CMD_TFTPPUT
Simon Glass4793ee62011-10-24 18:00:01 +0000695 /* Clear out the handlers */
Joe Hershbergerece223b2012-05-23 07:59:15 +0000696 net_set_udp_handler(NULL);
Simon Glass4793ee62011-10-24 18:00:01 +0000697 net_set_icmp_handler(NULL);
Simon Glass39bccd22011-10-26 14:18:38 +0000698#endif
Leonid Iziumtsev60177b22018-05-08 15:55:50 +0200699 net_set_state(prev_net_state);
Ramon Fried3eaac632019-07-18 21:43:30 +0300700
701#if defined(CONFIG_CMD_PCAP)
702 if (pcap_active())
703 pcap_print_status();
704#endif
Simon Glass4793ee62011-10-24 18:00:01 +0000705 return ret;
wdenk2d966952002-10-31 22:12:35 +0000706}
707
708/**********************************************************************/
709
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500710static void start_again_timeout_handler(void)
wdenk2d966952002-10-31 22:12:35 +0000711{
Joe Hershberger22f6e992012-05-23 07:59:14 +0000712 net_set_state(NETLOOP_RESTART);
wdenk2d966952002-10-31 22:12:35 +0000713}
714
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500715int net_start_again(void)
wdenk2d966952002-10-31 22:12:35 +0000716{
wdenk6e592382004-04-18 17:39:38 +0000717 char *nretry;
Remy Bohmer67b96e82009-10-28 22:13:39 +0100718 int retry_forever = 0;
719 unsigned long retrycnt = 0;
Joe Hershberger60304592015-03-22 17:09:24 -0500720 int ret;
wdenka3d991b2004-04-15 21:48:45 +0000721
Simon Glass00caae62017-08-03 12:22:12 -0600722 nretry = env_get("netretry");
Remy Bohmer67b96e82009-10-28 22:13:39 +0100723 if (nretry) {
724 if (!strcmp(nretry, "yes"))
725 retry_forever = 1;
726 else if (!strcmp(nretry, "no"))
727 retrycnt = 0;
728 else if (!strcmp(nretry, "once"))
729 retrycnt = 1;
730 else
731 retrycnt = simple_strtoul(nretry, NULL, 0);
Joe Hershberger5c421332015-03-22 17:09:07 -0500732 } else {
733 retrycnt = 0;
734 retry_forever = 0;
735 }
Remy Bohmer67b96e82009-10-28 22:13:39 +0100736
Leonid Iziumtsev17d413b2018-03-09 15:29:06 +0100737 if ((!retry_forever) && (net_try_count > retrycnt)) {
Remy Bohmer67b96e82009-10-28 22:13:39 +0100738 eth_halt();
Joe Hershberger22f6e992012-05-23 07:59:14 +0000739 net_set_state(NETLOOP_FAIL);
Joe Hershberger60304592015-03-22 17:09:24 -0500740 /*
741 * We don't provide a way for the protocol to return an error,
742 * but this is almost always the reason.
743 */
744 return -ETIMEDOUT;
wdenka3d991b2004-04-15 21:48:45 +0000745 }
Remy Bohmer67b96e82009-10-28 22:13:39 +0100746
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500747 net_try_count++;
Remy Bohmer67b96e82009-10-28 22:13:39 +0100748
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000749 eth_halt();
Matthias Fuchs8b0c5c12007-12-27 16:58:41 +0100750#if !defined(CONFIG_NET_DO_NOT_TRY_ANOTHER)
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500751 eth_try_another(!net_restarted);
Matthias Fuchs8b0c5c12007-12-27 16:58:41 +0100752#endif
Joe Hershberger60304592015-03-22 17:09:24 -0500753 ret = eth_init();
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500754 if (net_restart_wrap) {
755 net_restart_wrap = 0;
756 if (net_dev_exists) {
757 net_set_timeout_handler(10000UL,
758 start_again_timeout_handler);
Joe Hershbergerece223b2012-05-23 07:59:15 +0000759 net_set_udp_handler(NULL);
wdenk6e592382004-04-18 17:39:38 +0000760 } else {
Joe Hershberger22f6e992012-05-23 07:59:14 +0000761 net_set_state(NETLOOP_FAIL);
wdenk2d966952002-10-31 22:12:35 +0000762 }
wdenk6e592382004-04-18 17:39:38 +0000763 } else {
Joe Hershberger22f6e992012-05-23 07:59:14 +0000764 net_set_state(NETLOOP_RESTART);
wdenk2d966952002-10-31 22:12:35 +0000765 }
Joe Hershberger60304592015-03-22 17:09:24 -0500766 return ret;
wdenk2d966952002-10-31 22:12:35 +0000767}
768
769/**********************************************************************/
770/*
771 * Miscelaneous bits.
772 */
773
Joe Hershbergerece223b2012-05-23 07:59:15 +0000774static void dummy_handler(uchar *pkt, unsigned dport,
Joe Hershberger049a95a2015-04-08 01:41:01 -0500775 struct in_addr sip, unsigned sport,
Joe Hershbergerece223b2012-05-23 07:59:15 +0000776 unsigned len)
Joe Hershbergerd280d3f2012-05-23 07:58:01 +0000777{
Joe Hershbergerd280d3f2012-05-23 07:58:01 +0000778}
779
Joe Hershbergerece223b2012-05-23 07:59:15 +0000780rxhand_f *net_get_udp_handler(void)
wdenk2d966952002-10-31 22:12:35 +0000781{
Joe Hershbergerece223b2012-05-23 07:59:15 +0000782 return udp_packet_handler;
783}
784
785void net_set_udp_handler(rxhand_f *f)
786{
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500787 debug_cond(DEBUG_INT_STATE, "--- net_loop UDP handler set (%p)\n", f);
Joe Hershbergerece223b2012-05-23 07:59:15 +0000788 if (f == NULL)
789 udp_packet_handler = dummy_handler;
790 else
791 udp_packet_handler = f;
792}
793
794rxhand_f *net_get_arp_handler(void)
795{
796 return arp_packet_handler;
797}
798
799void net_set_arp_handler(rxhand_f *f)
800{
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500801 debug_cond(DEBUG_INT_STATE, "--- net_loop ARP handler set (%p)\n", f);
Joe Hershbergerece223b2012-05-23 07:59:15 +0000802 if (f == NULL)
803 arp_packet_handler = dummy_handler;
804 else
805 arp_packet_handler = f;
wdenk2d966952002-10-31 22:12:35 +0000806}
807
Simon Glass39bccd22011-10-26 14:18:38 +0000808#ifdef CONFIG_CMD_TFTPPUT
Simon Glass4793ee62011-10-24 18:00:01 +0000809void net_set_icmp_handler(rxhand_icmp_f *f)
810{
811 packet_icmp_handler = f;
812}
Simon Glass39bccd22011-10-26 14:18:38 +0000813#endif
wdenk2d966952002-10-31 22:12:35 +0000814
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500815void net_set_timeout_handler(ulong iv, thand_f *f)
wdenk2d966952002-10-31 22:12:35 +0000816{
817 if (iv == 0) {
Joe Hershberger4ef8d532012-05-23 08:01:04 +0000818 debug_cond(DEBUG_INT_STATE,
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500819 "--- net_loop timeout handler cancelled\n");
820 time_handler = (thand_f *)0;
wdenk2d966952002-10-31 22:12:35 +0000821 } else {
Joe Hershberger4ef8d532012-05-23 08:01:04 +0000822 debug_cond(DEBUG_INT_STATE,
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500823 "--- net_loop timeout handler set (%p)\n", f);
824 time_handler = f;
825 time_start = get_timer(0);
826 time_delta = iv * CONFIG_SYS_HZ / 1000;
wdenk2d966952002-10-31 22:12:35 +0000827 }
828}
829
Joe Hershbergerac3f26c2018-09-26 16:49:02 -0500830uchar *net_get_async_tx_pkt_buf(void)
831{
832 if (arp_is_waiting())
833 return arp_tx_packet; /* If we are waiting, we already sent */
834 else
835 return net_tx_packet;
836}
837
Joe Hershberger1203fcc2015-04-08 01:41:05 -0500838int net_send_udp_packet(uchar *ether, struct in_addr dest, int dport, int sport,
Joe Hershberger206d07f2012-05-23 07:58:10 +0000839 int payload_len)
wdenk73a8b272003-06-05 19:27:42 +0000840{
Duncan Hare5d457ec2018-06-24 15:40:41 -0700841 return net_send_ip_packet(ether, dest, dport, sport, payload_len,
842 IPPROTO_UDP, 0, 0, 0);
843}
844
Ying-Chun Liu (PaulLiu)a3bf1932022-11-08 14:17:28 +0800845#if defined(CONFIG_PROT_TCP)
846int net_send_tcp_packet(int payload_len, int dport, int sport, u8 action,
847 u32 tcp_seq_num, u32 tcp_ack_num)
848{
849 return net_send_ip_packet(net_server_ethaddr, net_server_ip, dport,
850 sport, payload_len, IPPROTO_TCP, action,
851 tcp_seq_num, tcp_ack_num);
852}
853#endif
854
Duncan Hare5d457ec2018-06-24 15:40:41 -0700855int net_send_ip_packet(uchar *ether, struct in_addr dest, int dport, int sport,
856 int payload_len, int proto, u8 action, u32 tcp_seq_num,
857 u32 tcp_ack_num)
858{
wdenka3d991b2004-04-15 21:48:45 +0000859 uchar *pkt;
Joe Hershberger92146372012-05-23 07:59:08 +0000860 int eth_hdr_size;
861 int pkt_hdr_size;
wdenka3d991b2004-04-15 21:48:45 +0000862
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500863 /* make sure the net_tx_packet is initialized (net_init() was called) */
Joe Hershberger1203fcc2015-04-08 01:41:05 -0500864 assert(net_tx_packet != NULL);
865 if (net_tx_packet == NULL)
Joe Hershberger46c495d2012-05-23 07:59:22 +0000866 return -1;
867
wdenk73a8b272003-06-05 19:27:42 +0000868 /* convert to new style broadcast */
Joe Hershberger049a95a2015-04-08 01:41:01 -0500869 if (dest.s_addr == 0)
870 dest.s_addr = 0xFFFFFFFF;
wdenk2d966952002-10-31 22:12:35 +0000871
wdenk73a8b272003-06-05 19:27:42 +0000872 /* if broadcast, make the ether address a broadcast and don't do ARP */
Joe Hershberger049a95a2015-04-08 01:41:01 -0500873 if (dest.s_addr == 0xFFFFFFFF)
Joe Hershberger0adb5b72015-04-08 01:41:04 -0500874 ether = (uchar *)net_bcast_ethaddr;
wdenk73a8b272003-06-05 19:27:42 +0000875
Joe Hershberger1203fcc2015-04-08 01:41:05 -0500876 pkt = (uchar *)net_tx_packet;
wdenk73a8b272003-06-05 19:27:42 +0000877
Joe Hershberger1203fcc2015-04-08 01:41:05 -0500878 eth_hdr_size = net_set_ether(pkt, ether, PROT_IP);
Duncan Hare5d457ec2018-06-24 15:40:41 -0700879
880 switch (proto) {
881 case IPPROTO_UDP:
882 net_set_udp_header(pkt + eth_hdr_size, dest, dport, sport,
883 payload_len);
884 pkt_hdr_size = eth_hdr_size + IP_UDP_HDR_SIZE;
885 break;
Ying-Chun Liu (PaulLiu)a3bf1932022-11-08 14:17:28 +0800886#if defined(CONFIG_PROT_TCP)
887 case IPPROTO_TCP:
888 pkt_hdr_size = eth_hdr_size
889 + tcp_set_tcp_header(pkt + eth_hdr_size, dport, sport,
890 payload_len, action, tcp_seq_num,
891 tcp_ack_num);
892 break;
893#endif
Duncan Hare5d457ec2018-06-24 15:40:41 -0700894 default:
895 return -EINVAL;
896 }
Robin Getz0ebf04c2009-07-23 03:01:03 -0400897
Joe Hershbergere94070c2012-05-23 07:59:24 +0000898 /* if MAC address was not discovered yet, do an ARP request */
Joe Hershberger0adb5b72015-04-08 01:41:04 -0500899 if (memcmp(ether, net_null_ethaddr, 6) == 0) {
Joe Hershberger4ef8d532012-05-23 08:01:04 +0000900 debug_cond(DEBUG_DEV_PKT, "sending ARP for %pI4\n", &dest);
Joe Hershberger92146372012-05-23 07:59:08 +0000901
902 /* save the ip and eth addr for the packet to send after arp */
Joe Hershberger049a95a2015-04-08 01:41:01 -0500903 net_arp_wait_packet_ip = dest;
Joe Hershberger85d25e02015-04-08 01:41:08 -0500904 arp_wait_packet_ethaddr = ether;
wdenka3d991b2004-04-15 21:48:45 +0000905
wdenk73a8b272003-06-05 19:27:42 +0000906 /* size of the waiting packet */
Joe Hershberger85d25e02015-04-08 01:41:08 -0500907 arp_wait_tx_packet_size = pkt_hdr_size + payload_len;
wdenk73a8b272003-06-05 19:27:42 +0000908
909 /* and do the ARP request */
Joe Hershberger85d25e02015-04-08 01:41:08 -0500910 arp_wait_try = 1;
911 arp_wait_timer_start = get_timer(0);
912 arp_request();
wdenk73a8b272003-06-05 19:27:42 +0000913 return 1; /* waiting */
Joe Hershberger92146372012-05-23 07:59:08 +0000914 } else {
Joe Hershberger4ef8d532012-05-23 08:01:04 +0000915 debug_cond(DEBUG_DEV_PKT, "sending UDP to %pI4/%pM\n",
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500916 &dest, ether);
Joe Hershberger1203fcc2015-04-08 01:41:05 -0500917 net_send_packet(net_tx_packet, pkt_hdr_size + payload_len);
Joe Hershberger92146372012-05-23 07:59:08 +0000918 return 0; /* transmitted */
wdenk73a8b272003-06-05 19:27:42 +0000919 }
wdenk73a8b272003-06-05 19:27:42 +0000920}
921
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +0200922#ifdef CONFIG_IP_DEFRAG
923/*
924 * This function collects fragments in a single packet, according
925 * to the algorithm in RFC815. It returns NULL or the pointer to
926 * a complete packet, in static storage
927 */
Joe Hershbergeraa7a6482016-08-15 14:42:15 -0500928#define IP_PKTSIZE (CONFIG_NET_MAXDEFRAG)
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +0200929
Joe Hershbergerc5c59df2012-05-23 07:58:05 +0000930#define IP_MAXUDP (IP_PKTSIZE - IP_HDR_SIZE)
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +0200931
932/*
933 * this is the packet being assembled, either data or frag control.
934 * Fragments go by 8 bytes, so this union must be 8 bytes long
935 */
936struct hole {
937 /* first_byte is address of this structure */
938 u16 last_byte; /* last byte in this hole + 1 (begin of next hole) */
939 u16 next_hole; /* index of next (in 8-b blocks), 0 == none */
940 u16 prev_hole; /* index of prev, 0 == none */
941 u16 unused;
942};
943
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500944static struct ip_udp_hdr *__net_defragment(struct ip_udp_hdr *ip, int *lenp)
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +0200945{
Joe Hershberger48522bb2012-05-15 08:59:08 +0000946 static uchar pkt_buff[IP_PKTSIZE] __aligned(PKTALIGN);
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +0200947 static u16 first_hole, total_len;
948 struct hole *payload, *thisfrag, *h, *newh;
Joe Hershberger594c26f2012-05-23 07:58:04 +0000949 struct ip_udp_hdr *localip = (struct ip_udp_hdr *)pkt_buff;
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +0200950 uchar *indata = (uchar *)ip;
951 int offset8, start, len, done = 0;
952 u16 ip_off = ntohs(ip->ip_off);
953
Rasmus Villemoes1817c382022-10-14 19:43:39 +0200954 /*
955 * Calling code already rejected <, but we don't have to deal
956 * with an IP fragment with no payload.
957 */
958 if (ntohs(ip->ip_len) <= IP_HDR_SIZE)
Fabio Estevamb85d1302022-05-26 11:14:37 -0300959 return NULL;
960
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +0200961 /* payload starts after IP header, this fragment is in there */
Joe Hershbergerc5c59df2012-05-23 07:58:05 +0000962 payload = (struct hole *)(pkt_buff + IP_HDR_SIZE);
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +0200963 offset8 = (ip_off & IP_OFFS);
964 thisfrag = payload + offset8;
965 start = offset8 * 8;
Joe Hershbergerc5c59df2012-05-23 07:58:05 +0000966 len = ntohs(ip->ip_len) - IP_HDR_SIZE;
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +0200967
Rasmus Villemoes1817c382022-10-14 19:43:39 +0200968 /* All but last fragment must have a multiple-of-8 payload. */
969 if ((len & 7) && (ip_off & IP_FLAGS_MFRAG))
970 return NULL;
971
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +0200972 if (start + len > IP_MAXUDP) /* fragment extends too far */
973 return NULL;
974
975 if (!total_len || localip->ip_id != ip->ip_id) {
976 /* new (or different) packet, reset structs */
977 total_len = 0xffff;
978 payload[0].last_byte = ~0;
979 payload[0].next_hole = 0;
980 payload[0].prev_hole = 0;
981 first_hole = 0;
982 /* any IP header will work, copy the first we received */
Joe Hershbergerc5c59df2012-05-23 07:58:05 +0000983 memcpy(localip, ip, IP_HDR_SIZE);
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +0200984 }
985
986 /*
987 * What follows is the reassembly algorithm. We use the payload
988 * array as a linked list of hole descriptors, as each hole starts
989 * at a multiple of 8 bytes. However, last byte can be whatever value,
990 * so it is represented as byte count, not as 8-byte blocks.
991 */
992
993 h = payload + first_hole;
994 while (h->last_byte < start) {
995 if (!h->next_hole) {
996 /* no hole that far away */
997 return NULL;
998 }
999 h = payload + h->next_hole;
1000 }
1001
Fillod Stephanee397e592010-06-11 19:26:43 +02001002 /* last fragment may be 1..7 bytes, the "+7" forces acceptance */
1003 if (offset8 + ((len + 7) / 8) <= h - payload) {
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +02001004 /* no overlap with holes (dup fragment?) */
1005 return NULL;
1006 }
1007
1008 if (!(ip_off & IP_FLAGS_MFRAG)) {
1009 /* no more fragmentss: truncate this (last) hole */
1010 total_len = start + len;
1011 h->last_byte = start + len;
1012 }
1013
1014 /*
Rasmus Villemoes06869682022-10-17 09:52:51 +02001015 * There is some overlap: fix the hole list. This code deals
1016 * with a fragment that overlaps with two different holes
1017 * (thus being a superset of a previously-received fragment)
1018 * by only using the part of the fragment that fits in the
1019 * first hole.
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +02001020 */
Rasmus Villemoes06869682022-10-17 09:52:51 +02001021 if (h->last_byte < start + len)
1022 len = h->last_byte - start;
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +02001023
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001024 if ((h >= thisfrag) && (h->last_byte <= start + len)) {
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +02001025 /* complete overlap with hole: remove hole */
1026 if (!h->prev_hole && !h->next_hole) {
1027 /* last remaining hole */
1028 done = 1;
1029 } else if (!h->prev_hole) {
1030 /* first hole */
1031 first_hole = h->next_hole;
1032 payload[h->next_hole].prev_hole = 0;
1033 } else if (!h->next_hole) {
1034 /* last hole */
1035 payload[h->prev_hole].next_hole = 0;
1036 } else {
1037 /* in the middle of the list */
1038 payload[h->next_hole].prev_hole = h->prev_hole;
1039 payload[h->prev_hole].next_hole = h->next_hole;
1040 }
1041
1042 } else if (h->last_byte <= start + len) {
1043 /* overlaps with final part of the hole: shorten this hole */
1044 h->last_byte = start;
1045
1046 } else if (h >= thisfrag) {
1047 /* overlaps with initial part of the hole: move this hole */
1048 newh = thisfrag + (len / 8);
1049 *newh = *h;
1050 h = newh;
1051 if (h->next_hole)
1052 payload[h->next_hole].prev_hole = (h - payload);
1053 if (h->prev_hole)
1054 payload[h->prev_hole].next_hole = (h - payload);
1055 else
1056 first_hole = (h - payload);
1057
1058 } else {
1059 /* fragment sits in the middle: split the hole */
1060 newh = thisfrag + (len / 8);
1061 *newh = *h;
1062 h->last_byte = start;
1063 h->next_hole = (newh - payload);
1064 newh->prev_hole = (h - payload);
1065 if (newh->next_hole)
1066 payload[newh->next_hole].prev_hole = (newh - payload);
1067 }
1068
1069 /* finally copy this fragment and possibly return whole packet */
Joe Hershbergerc5c59df2012-05-23 07:58:05 +00001070 memcpy((uchar *)thisfrag, indata + IP_HDR_SIZE, len);
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +02001071 if (!done)
1072 return NULL;
1073
Joe Hershbergerc5c59df2012-05-23 07:58:05 +00001074 *lenp = total_len + IP_HDR_SIZE;
Rasmus Villemoes06653c72022-10-14 19:43:40 +02001075 localip->ip_len = htons(*lenp);
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +02001076 return localip;
1077}
1078
Joe Hershbergerbc0571f2015-04-08 01:41:21 -05001079static inline struct ip_udp_hdr *net_defragment(struct ip_udp_hdr *ip,
1080 int *lenp)
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +02001081{
1082 u16 ip_off = ntohs(ip->ip_off);
1083 if (!(ip_off & (IP_OFFS | IP_FLAGS_MFRAG)))
1084 return ip; /* not a fragment */
Joe Hershbergerbc0571f2015-04-08 01:41:21 -05001085 return __net_defragment(ip, lenp);
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +02001086}
1087
1088#else /* !CONFIG_IP_DEFRAG */
1089
Joe Hershbergerbc0571f2015-04-08 01:41:21 -05001090static inline struct ip_udp_hdr *net_defragment(struct ip_udp_hdr *ip,
1091 int *lenp)
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +02001092{
1093 u16 ip_off = ntohs(ip->ip_off);
1094 if (!(ip_off & (IP_OFFS | IP_FLAGS_MFRAG)))
1095 return ip; /* not a fragment */
1096 return NULL;
1097}
1098#endif
wdenka3d991b2004-04-15 21:48:45 +00001099
Simon Glass8f79bb12011-10-24 18:00:00 +00001100/**
1101 * Receive an ICMP packet. We deal with REDIRECT and PING here, and silently
1102 * drop others.
1103 *
1104 * @parma ip IP packet containing the ICMP
1105 */
Joe Hershberger594c26f2012-05-23 07:58:04 +00001106static void receive_icmp(struct ip_udp_hdr *ip, int len,
Joe Hershberger049a95a2015-04-08 01:41:01 -05001107 struct in_addr src_ip, struct ethernet_hdr *et)
Simon Glass8f79bb12011-10-24 18:00:00 +00001108{
Joe Hershbergere0a63072012-05-23 07:58:09 +00001109 struct icmp_hdr *icmph = (struct icmp_hdr *)&ip->udp_src;
Simon Glass8f79bb12011-10-24 18:00:00 +00001110
1111 switch (icmph->type) {
1112 case ICMP_REDIRECT:
1113 if (icmph->code != ICMP_REDIR_HOST)
1114 return;
1115 printf(" ICMP Host Redirect to %pI4 ",
Joe Hershbergerbc0571f2015-04-08 01:41:21 -05001116 &icmph->un.gateway);
Simon Glass8f79bb12011-10-24 18:00:00 +00001117 break;
Simon Glass8f79bb12011-10-24 18:00:00 +00001118 default:
Joe Hershbergera36b12f2012-05-23 07:58:02 +00001119#if defined(CONFIG_CMD_PING)
1120 ping_receive(et, ip, len);
1121#endif
Simon Glass39bccd22011-10-26 14:18:38 +00001122#ifdef CONFIG_CMD_TFTPPUT
Simon Glass4793ee62011-10-24 18:00:01 +00001123 if (packet_icmp_handler)
1124 packet_icmp_handler(icmph->type, icmph->code,
Joe Hershbergerbc0571f2015-04-08 01:41:21 -05001125 ntohs(ip->udp_dst), src_ip,
1126 ntohs(ip->udp_src), icmph->un.data,
1127 ntohs(ip->udp_len));
Simon Glass39bccd22011-10-26 14:18:38 +00001128#endif
Simon Glass8f79bb12011-10-24 18:00:00 +00001129 break;
1130 }
1131}
1132
Joe Hershberger2a504df2015-03-22 17:09:11 -05001133void net_process_received_packet(uchar *in_packet, int len)
wdenk2d966952002-10-31 22:12:35 +00001134{
Joe Hershbergercb487f52012-05-23 07:58:06 +00001135 struct ethernet_hdr *et;
Joe Hershberger594c26f2012-05-23 07:58:04 +00001136 struct ip_udp_hdr *ip;
Joe Hershberger049a95a2015-04-08 01:41:01 -05001137 struct in_addr dst_ip;
1138 struct in_addr src_ip;
Joe Hershberger8d353eb2012-05-23 07:58:12 +00001139 int eth_proto;
Jon Loeliger643d1ab2007-07-09 17:45:14 -05001140#if defined(CONFIG_CMD_CDP)
wdenka3d991b2004-04-15 21:48:45 +00001141 int iscdp;
1142#endif
1143 ushort cti = 0, vlanid = VLAN_NONE, myvlanid, mynvlanid;
wdenk2d966952002-10-31 22:12:35 +00001144
Joe Hershberger4ef8d532012-05-23 08:01:04 +00001145 debug_cond(DEBUG_NET_PKT, "packet received\n");
wdenka3d991b2004-04-15 21:48:45 +00001146
Ramon Fried3eaac632019-07-18 21:43:30 +03001147#if defined(CONFIG_CMD_PCAP)
1148 pcap_post(in_packet, len, false);
1149#endif
Joe Hershberger1203fcc2015-04-08 01:41:05 -05001150 net_rx_packet = in_packet;
1151 net_rx_packet_len = len;
Joe Hershberger2a504df2015-03-22 17:09:11 -05001152 et = (struct ethernet_hdr *)in_packet;
wdenka3d991b2004-04-15 21:48:45 +00001153
1154 /* too small packet? */
1155 if (len < ETHER_HDR_SIZE)
1156 return;
1157
Alexander Graf0efe1bc2016-05-06 21:01:01 +02001158#if defined(CONFIG_API) || defined(CONFIG_EFI_LOADER)
Rafal Jaworowskif85b6072007-12-27 18:19:02 +01001159 if (push_packet) {
Joe Hershberger2a504df2015-03-22 17:09:11 -05001160 (*push_packet)(in_packet, len);
Rafal Jaworowskif85b6072007-12-27 18:19:02 +01001161 return;
1162 }
1163#endif
1164
Jon Loeliger643d1ab2007-07-09 17:45:14 -05001165#if defined(CONFIG_CMD_CDP)
wdenka3d991b2004-04-15 21:48:45 +00001166 /* keep track if packet is CDP */
Joe Hershberger17351882012-05-23 07:58:00 +00001167 iscdp = is_cdp_packet(et->et_dest);
wdenka3d991b2004-04-15 21:48:45 +00001168#endif
1169
Joe Hershberger4fd50552015-04-08 01:41:17 -05001170 myvlanid = ntohs(net_our_vlan);
wdenka3d991b2004-04-15 21:48:45 +00001171 if (myvlanid == (ushort)-1)
1172 myvlanid = VLAN_NONE;
Joe Hershberger4fd50552015-04-08 01:41:17 -05001173 mynvlanid = ntohs(net_native_vlan);
wdenka3d991b2004-04-15 21:48:45 +00001174 if (mynvlanid == (ushort)-1)
1175 mynvlanid = VLAN_NONE;
wdenk2d966952002-10-31 22:12:35 +00001176
Joe Hershberger8d353eb2012-05-23 07:58:12 +00001177 eth_proto = ntohs(et->et_protlen);
wdenk2d966952002-10-31 22:12:35 +00001178
Joe Hershberger8d353eb2012-05-23 07:58:12 +00001179 if (eth_proto < 1514) {
Joe Hershbergercb487f52012-05-23 07:58:06 +00001180 struct e802_hdr *et802 = (struct e802_hdr *)et;
wdenk2d966952002-10-31 22:12:35 +00001181 /*
Joe Hershbergerda5ebe22012-05-23 07:58:11 +00001182 * Got a 802.2 packet. Check the other protocol field.
1183 * XXX VLAN over 802.2+SNAP not implemented!
wdenk2d966952002-10-31 22:12:35 +00001184 */
Joe Hershberger8d353eb2012-05-23 07:58:12 +00001185 eth_proto = ntohs(et802->et_prot);
wdenka3d991b2004-04-15 21:48:45 +00001186
Joe Hershberger2a504df2015-03-22 17:09:11 -05001187 ip = (struct ip_udp_hdr *)(in_packet + E802_HDR_SIZE);
wdenk2d966952002-10-31 22:12:35 +00001188 len -= E802_HDR_SIZE;
wdenka3d991b2004-04-15 21:48:45 +00001189
Joe Hershberger8d353eb2012-05-23 07:58:12 +00001190 } else if (eth_proto != PROT_VLAN) { /* normal packet */
Joe Hershberger2a504df2015-03-22 17:09:11 -05001191 ip = (struct ip_udp_hdr *)(in_packet + ETHER_HDR_SIZE);
wdenk2d966952002-10-31 22:12:35 +00001192 len -= ETHER_HDR_SIZE;
wdenka3d991b2004-04-15 21:48:45 +00001193
1194 } else { /* VLAN packet */
Joe Hershbergerc68cca32012-05-23 07:58:07 +00001195 struct vlan_ethernet_hdr *vet =
1196 (struct vlan_ethernet_hdr *)et;
wdenka3d991b2004-04-15 21:48:45 +00001197
Joe Hershberger4ef8d532012-05-23 08:01:04 +00001198 debug_cond(DEBUG_NET_PKT, "VLAN packet received\n");
Robin Getz0ebf04c2009-07-23 03:01:03 -04001199
wdenka3d991b2004-04-15 21:48:45 +00001200 /* too small packet? */
1201 if (len < VLAN_ETHER_HDR_SIZE)
1202 return;
1203
1204 /* if no VLAN active */
Joe Hershberger4fd50552015-04-08 01:41:17 -05001205 if ((ntohs(net_our_vlan) & VLAN_IDMASK) == VLAN_NONE
Jon Loeliger643d1ab2007-07-09 17:45:14 -05001206#if defined(CONFIG_CMD_CDP)
wdenka3d991b2004-04-15 21:48:45 +00001207 && iscdp == 0
1208#endif
1209 )
1210 return;
1211
1212 cti = ntohs(vet->vet_tag);
1213 vlanid = cti & VLAN_IDMASK;
Joe Hershberger8d353eb2012-05-23 07:58:12 +00001214 eth_proto = ntohs(vet->vet_type);
wdenka3d991b2004-04-15 21:48:45 +00001215
Joe Hershberger2a504df2015-03-22 17:09:11 -05001216 ip = (struct ip_udp_hdr *)(in_packet + VLAN_ETHER_HDR_SIZE);
wdenka3d991b2004-04-15 21:48:45 +00001217 len -= VLAN_ETHER_HDR_SIZE;
wdenk2d966952002-10-31 22:12:35 +00001218 }
1219
Joe Hershberger4ef8d532012-05-23 08:01:04 +00001220 debug_cond(DEBUG_NET_PKT, "Receive from protocol 0x%x\n", eth_proto);
wdenk2d966952002-10-31 22:12:35 +00001221
Jon Loeliger643d1ab2007-07-09 17:45:14 -05001222#if defined(CONFIG_CMD_CDP)
wdenka3d991b2004-04-15 21:48:45 +00001223 if (iscdp) {
Joe Hershberger0b4c5ff2012-05-23 07:58:13 +00001224 cdp_receive((uchar *)ip, len);
wdenka3d991b2004-04-15 21:48:45 +00001225 return;
1226 }
1227#endif
1228
1229 if ((myvlanid & VLAN_IDMASK) != VLAN_NONE) {
1230 if (vlanid == VLAN_NONE)
1231 vlanid = (mynvlanid & VLAN_IDMASK);
1232 /* not matched? */
1233 if (vlanid != (myvlanid & VLAN_IDMASK))
1234 return;
1235 }
1236
Joe Hershberger8d353eb2012-05-23 07:58:12 +00001237 switch (eth_proto) {
wdenk2d966952002-10-31 22:12:35 +00001238 case PROT_ARP:
Joe Hershberger85d25e02015-04-08 01:41:08 -05001239 arp_receive(et, ip, len);
wdenk289f9322005-01-12 00:15:14 +00001240 break;
wdenk2d966952002-10-31 22:12:35 +00001241
Peter Tyserbf6cb242010-09-30 11:25:48 -05001242#ifdef CONFIG_CMD_RARP
wdenk2d966952002-10-31 22:12:35 +00001243 case PROT_RARP:
Joe Hershberger8b9c5322012-05-23 07:58:03 +00001244 rarp_receive(ip, len);
wdenk2d966952002-10-31 22:12:35 +00001245 break;
Peter Tyserbf6cb242010-09-30 11:25:48 -05001246#endif
wdenk2d966952002-10-31 22:12:35 +00001247 case PROT_IP:
Joe Hershberger4ef8d532012-05-23 08:01:04 +00001248 debug_cond(DEBUG_NET_PKT, "Got IP\n");
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +02001249 /* Before we start poking the header, make sure it is there */
Rasmus Villemoesad359d82022-10-14 19:43:38 +02001250 if (len < IP_HDR_SIZE) {
Joe Hershberger594c26f2012-05-23 07:58:04 +00001251 debug("len bad %d < %lu\n", len,
Rasmus Villemoesad359d82022-10-14 19:43:38 +02001252 (ulong)IP_HDR_SIZE);
wdenk2d966952002-10-31 22:12:35 +00001253 return;
1254 }
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +02001255 /* Check the packet length */
wdenk2d966952002-10-31 22:12:35 +00001256 if (len < ntohs(ip->ip_len)) {
Joe Hershberger4ef8d532012-05-23 08:01:04 +00001257 debug("len bad %d < %d\n", len, ntohs(ip->ip_len));
wdenk2d966952002-10-31 22:12:35 +00001258 return;
1259 }
1260 len = ntohs(ip->ip_len);
Rasmus Villemoesad359d82022-10-14 19:43:38 +02001261 if (len < IP_HDR_SIZE) {
1262 debug("bad ip->ip_len %d < %d\n", len, (int)IP_HDR_SIZE);
1263 return;
1264 }
Joe Hershberger4ef8d532012-05-23 08:01:04 +00001265 debug_cond(DEBUG_NET_PKT, "len=%d, v=%02x\n",
Joe Hershbergerbc0571f2015-04-08 01:41:21 -05001266 len, ip->ip_hl_v & 0xff);
Robin Getz0ebf04c2009-07-23 03:01:03 -04001267
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +02001268 /* Can't deal with anything except IPv4 */
Luca Ceresolid3c65b02011-05-04 02:40:43 +00001269 if ((ip->ip_hl_v & 0xf0) != 0x40)
wdenk2d966952002-10-31 22:12:35 +00001270 return;
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +02001271 /* Can't deal with IP options (headers != 20 bytes) */
Rasmus Villemoesb0fcc482022-10-14 19:43:37 +02001272 if ((ip->ip_hl_v & 0x0f) != 0x05)
Remy Bohmer6b52cfe2008-06-03 15:48:17 +02001273 return;
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +02001274 /* Check the Checksum of the header */
Simon Glass0da0fcd2015-01-19 22:16:08 -07001275 if (!ip_checksum_ok((uchar *)ip, IP_HDR_SIZE)) {
Joe Hershberger4ef8d532012-05-23 08:01:04 +00001276 debug("checksum bad\n");
wdenk2d966952002-10-31 22:12:35 +00001277 return;
1278 }
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +02001279 /* If it is not for us, ignore it */
Joe Hershberger049a95a2015-04-08 01:41:01 -05001280 dst_ip = net_read_ip(&ip->ip_dst);
1281 if (net_ip.s_addr && dst_ip.s_addr != net_ip.s_addr &&
1282 dst_ip.s_addr != 0xFFFFFFFF) {
Luca Ceresolic819abe2011-05-04 02:40:46 +00001283 return;
wdenk2d966952002-10-31 22:12:35 +00001284 }
Luca Ceresoli03eb1292011-04-18 06:19:50 +00001285 /* Read source IP address for later use */
Joe Hershberger049a95a2015-04-08 01:41:01 -05001286 src_ip = net_read_ip(&ip->ip_src);
wdenk2d966952002-10-31 22:12:35 +00001287 /*
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +02001288 * The function returns the unchanged packet if it's not
1289 * a fragment, and either the complete packet or NULL if
1290 * it is a fragment (if !CONFIG_IP_DEFRAG, it returns NULL)
1291 */
Joe Hershbergerbc0571f2015-04-08 01:41:21 -05001292 ip = net_defragment(ip, &len);
Luca Ceresoliccb9ebe2011-05-04 02:40:47 +00001293 if (!ip)
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +02001294 return;
1295 /*
wdenk2d966952002-10-31 22:12:35 +00001296 * watch for ICMP host redirects
1297 *
wdenk8bde7f72003-06-27 21:31:46 +00001298 * There is no real handler code (yet). We just watch
1299 * for ICMP host redirect messages. In case anybody
1300 * sees these messages: please contact me
1301 * (wd@denx.de), or - even better - send me the
1302 * necessary fixes :-)
wdenk2d966952002-10-31 22:12:35 +00001303 *
wdenk8bde7f72003-06-27 21:31:46 +00001304 * Note: in all cases where I have seen this so far
1305 * it was a problem with the router configuration,
1306 * for instance when a router was configured in the
1307 * BOOTP reply, but the TFTP server was on the same
1308 * subnet. So this is probably a warning that your
1309 * configuration might be wrong. But I'm not really
1310 * sure if there aren't any other situations.
Simon Glass4793ee62011-10-24 18:00:01 +00001311 *
1312 * Simon Glass <sjg@chromium.org>: We get an ICMP when
1313 * we send a tftp packet to a dead connection, or when
1314 * there is no server at the other end.
wdenk2d966952002-10-31 22:12:35 +00001315 */
1316 if (ip->ip_p == IPPROTO_ICMP) {
Simon Glass8f79bb12011-10-24 18:00:00 +00001317 receive_icmp(ip, len, src_ip, et);
1318 return;
Ying-Chun Liu (PaulLiu)a3bf1932022-11-08 14:17:28 +08001319#if defined(CONFIG_PROT_TCP)
1320 } else if (ip->ip_p == IPPROTO_TCP) {
1321 debug_cond(DEBUG_DEV_PKT,
1322 "TCP PH (to=%pI4, from=%pI4, len=%d)\n",
1323 &dst_ip, &src_ip, len);
1324
1325 rxhand_tcp_f((union tcp_build_pkt *)ip, len);
1326 return;
1327#endif
wdenk2d966952002-10-31 22:12:35 +00001328 } else if (ip->ip_p != IPPROTO_UDP) { /* Only UDP packets */
1329 return;
1330 }
1331
Rasmus Villemoes06653c72022-10-14 19:43:40 +02001332 if (ntohs(ip->udp_len) < UDP_HDR_SIZE || ntohs(ip->udp_len) > len - IP_HDR_SIZE)
liucheng (G)fe728802019-08-29 13:47:33 +00001333 return;
1334
Joe Hershberger4ef8d532012-05-23 08:01:04 +00001335 debug_cond(DEBUG_DEV_PKT,
Joe Hershbergerbc0571f2015-04-08 01:41:21 -05001336 "received UDP (to=%pI4, from=%pI4, len=%d)\n",
1337 &dst_ip, &src_ip, len);
Joe Hershberger4ef8d532012-05-23 08:01:04 +00001338
Simon Glass4b37fd12021-12-18 11:27:49 -07001339 if (IS_ENABLED(CONFIG_UDP_CHECKSUM) && ip->udp_xsum != 0) {
Wolfgang Denkb2f50802005-08-12 23:43:12 +02001340 ulong xsum;
Heinrich Schuchardt85244232019-11-05 12:48:19 +01001341 u8 *sumptr;
Stefan Roese8534bf92005-08-12 20:06:52 +02001342 ushort sumlen;
1343
1344 xsum = ip->ip_p;
1345 xsum += (ntohs(ip->udp_len));
Joe Hershberger049a95a2015-04-08 01:41:01 -05001346 xsum += (ntohl(ip->ip_src.s_addr) >> 16) & 0x0000ffff;
1347 xsum += (ntohl(ip->ip_src.s_addr) >> 0) & 0x0000ffff;
1348 xsum += (ntohl(ip->ip_dst.s_addr) >> 16) & 0x0000ffff;
1349 xsum += (ntohl(ip->ip_dst.s_addr) >> 0) & 0x0000ffff;
Stefan Roese8534bf92005-08-12 20:06:52 +02001350
1351 sumlen = ntohs(ip->udp_len);
Heinrich Schuchardt85244232019-11-05 12:48:19 +01001352 sumptr = (u8 *)&ip->udp_src;
Stefan Roese8534bf92005-08-12 20:06:52 +02001353
1354 while (sumlen > 1) {
Heinrich Schuchardt85244232019-11-05 12:48:19 +01001355 /* inlined ntohs() to avoid alignment errors */
1356 xsum += (sumptr[0] << 8) + sumptr[1];
1357 sumptr += 2;
Stefan Roese8534bf92005-08-12 20:06:52 +02001358 sumlen -= 2;
1359 }
Heinrich Schuchardt85244232019-11-05 12:48:19 +01001360 if (sumlen > 0)
1361 xsum += (sumptr[0] << 8) + sumptr[0];
Stefan Roese8534bf92005-08-12 20:06:52 +02001362 while ((xsum >> 16) != 0) {
Luca Ceresoli3e38e422011-05-11 03:59:54 +00001363 xsum = (xsum & 0x0000ffff) +
1364 ((xsum >> 16) & 0x0000ffff);
Stefan Roese8534bf92005-08-12 20:06:52 +02001365 }
1366 if ((xsum != 0x00000000) && (xsum != 0x0000ffff)) {
Wolfgang Denk9b55a252008-07-11 01:16:00 +02001367 printf(" UDP wrong checksum %08lx %08x\n",
Joe Hershbergerbc0571f2015-04-08 01:41:21 -05001368 xsum, ntohs(ip->udp_xsum));
Stefan Roese8534bf92005-08-12 20:06:52 +02001369 return;
1370 }
1371 }
Stefan Roese8534bf92005-08-12 20:06:52 +02001372
Holger Dengler66c89ee2017-07-20 10:10:55 +02001373#if defined(CONFIG_NETCONSOLE) && !defined(CONFIG_SPL_BUILD)
Joe Hershberger594c26f2012-05-23 07:58:04 +00001374 nc_input_packet((uchar *)ip + IP_UDP_HDR_SIZE,
Joe Hershbergerece223b2012-05-23 07:59:15 +00001375 src_ip,
Joe Hershbergerbc0571f2015-04-08 01:41:21 -05001376 ntohs(ip->udp_dst),
Joe Hershbergerece223b2012-05-23 07:59:15 +00001377 ntohs(ip->udp_src),
1378 ntohs(ip->udp_len) - UDP_HDR_SIZE);
Joe Hershbergerbc0571f2015-04-08 01:41:21 -05001379#endif
1380 /*
1381 * IP header OK. Pass the packet to the current handler.
1382 */
1383 (*udp_packet_handler)((uchar *)ip + IP_UDP_HDR_SIZE,
1384 ntohs(ip->udp_dst),
1385 src_ip,
1386 ntohs(ip->udp_src),
1387 ntohs(ip->udp_len) - UDP_HDR_SIZE);
wdenk2d966952002-10-31 22:12:35 +00001388 break;
Lothar Feltend8970da2018-06-22 22:29:54 +02001389#ifdef CONFIG_CMD_WOL
1390 case PROT_WOL:
1391 wol_receive(ip, len);
1392 break;
1393#endif
Samuel Mendoza-Jonas09bd3d02022-08-08 21:46:03 +09301394#ifdef CONFIG_PHY_NCSI
1395 case PROT_NCSI:
1396 ncsi_receive(et, ip, len);
1397 break;
1398#endif
wdenk2d966952002-10-31 22:12:35 +00001399 }
1400}
1401
wdenk2d966952002-10-31 22:12:35 +00001402/**********************************************************************/
1403
Simon Glasse4bf0c52011-10-24 18:00:02 +00001404static int net_check_prereq(enum proto_t protocol)
wdenk2d966952002-10-31 22:12:35 +00001405{
1406 switch (protocol) {
wdenk6e592382004-04-18 17:39:38 +00001407 /* Fall through */
Jon Loeliger643d1ab2007-07-09 17:45:14 -05001408#if defined(CONFIG_CMD_PING)
wdenk73a8b272003-06-05 19:27:42 +00001409 case PING:
Joe Hershberger049a95a2015-04-08 01:41:01 -05001410 if (net_ping_ip.s_addr == 0) {
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001411 puts("*** ERROR: ping address not given\n");
Luca Ceresoli92895de2011-05-04 02:40:45 +00001412 return 1;
wdenk6e592382004-04-18 17:39:38 +00001413 }
1414 goto common;
wdenk73a8b272003-06-05 19:27:42 +00001415#endif
Robin Getz1a32bf42009-07-20 14:53:54 -04001416#if defined(CONFIG_CMD_DNS)
1417 case DNS:
Joe Hershberger049a95a2015-04-08 01:41:01 -05001418 if (net_dns_server.s_addr == 0) {
Robin Getz1a32bf42009-07-20 14:53:54 -04001419 puts("*** ERROR: DNS server address not given\n");
1420 return 1;
1421 }
1422 goto common;
1423#endif
Philippe Reynesb43ea1b2020-09-18 14:13:00 +02001424#if defined(CONFIG_PROT_UDP)
1425 case UDP:
1426 if (udp_prereq())
1427 return 1;
1428 goto common;
1429#endif
1430
Jon Loeliger643d1ab2007-07-09 17:45:14 -05001431#if defined(CONFIG_CMD_NFS)
wdenkcbd8a352004-02-24 02:00:03 +00001432 case NFS:
1433#endif
Joe Hershbergerbc0571f2015-04-08 01:41:21 -05001434 /* Fall through */
Simon Glasse4bf0c52011-10-24 18:00:02 +00001435 case TFTPGET:
Simon Glass1fb7cd42011-10-24 18:00:07 +00001436 case TFTPPUT:
Joe Hershberger3a66fcb2018-07-03 19:36:39 -05001437 if (net_server_ip.s_addr == 0 && !is_serverip_in_cmd()) {
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001438 puts("*** ERROR: `serverip' not set\n");
Luca Ceresoli92895de2011-05-04 02:40:45 +00001439 return 1;
wdenk6e592382004-04-18 17:39:38 +00001440 }
Philippe Reynes912ece42020-09-18 14:13:02 +02001441#if defined(CONFIG_CMD_PING) || \
Philippe Reynesb43ea1b2020-09-18 14:13:00 +02001442 defined(CONFIG_CMD_DNS) || defined(CONFIG_PROT_UDP)
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001443common:
wdenk73a8b272003-06-05 19:27:42 +00001444#endif
Simon Guinot8b6bbe12011-05-01 23:38:40 +00001445 /* Fall through */
wdenk73a8b272003-06-05 19:27:42 +00001446
Simon Guinot8b6bbe12011-05-01 23:38:40 +00001447 case NETCONS:
Alex Kiernanf73a7df2018-05-29 15:30:53 +00001448 case FASTBOOT:
Luca Ceresoli7a83af02011-05-17 00:03:40 +00001449 case TFTPSRV:
Joe Hershberger049a95a2015-04-08 01:41:01 -05001450 if (net_ip.s_addr == 0) {
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001451 puts("*** ERROR: `ipaddr' not set\n");
Luca Ceresoli92895de2011-05-04 02:40:45 +00001452 return 1;
wdenk6e592382004-04-18 17:39:38 +00001453 }
1454 /* Fall through */
wdenk2d966952002-10-31 22:12:35 +00001455
Peter Tyserbf6cb242010-09-30 11:25:48 -05001456#ifdef CONFIG_CMD_RARP
wdenk2d966952002-10-31 22:12:35 +00001457 case RARP:
Peter Tyserbf6cb242010-09-30 11:25:48 -05001458#endif
Samuel Mendoza-Jonas09bd3d02022-08-08 21:46:03 +09301459#ifdef CONFIG_PHY_NCSI
1460 case NCSI:
1461#endif
wdenk2d966952002-10-31 22:12:35 +00001462 case BOOTP:
wdenka3d991b2004-04-15 21:48:45 +00001463 case CDP:
Peter Tyserbf6cb242010-09-30 11:25:48 -05001464 case DHCP:
Joe Hershbergerd22c3382012-05-23 08:00:12 +00001465 case LINKLOCAL:
Joe Hershberger0adb5b72015-04-08 01:41:04 -05001466 if (memcmp(net_ethaddr, "\0\0\0\0\0\0", 6) == 0) {
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001467 int num = eth_get_dev_index();
wdenk2d966952002-10-31 22:12:35 +00001468
wdenk6e592382004-04-18 17:39:38 +00001469 switch (num) {
1470 case -1:
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001471 puts("*** ERROR: No ethernet found.\n");
Luca Ceresoli92895de2011-05-04 02:40:45 +00001472 return 1;
wdenk6e592382004-04-18 17:39:38 +00001473 case 0:
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001474 puts("*** ERROR: `ethaddr' not set\n");
wdenk2d966952002-10-31 22:12:35 +00001475 break;
wdenk6e592382004-04-18 17:39:38 +00001476 default:
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001477 printf("*** ERROR: `eth%daddr' not set\n",
Joe Hershbergerbc0571f2015-04-08 01:41:21 -05001478 num);
wdenk2d966952002-10-31 22:12:35 +00001479 break;
wdenk2d966952002-10-31 22:12:35 +00001480 }
wdenk6e592382004-04-18 17:39:38 +00001481
Joe Hershbergerbc0571f2015-04-08 01:41:21 -05001482 net_start_again();
Luca Ceresoli92895de2011-05-04 02:40:45 +00001483 return 2;
wdenk6e592382004-04-18 17:39:38 +00001484 }
1485 /* Fall through */
1486 default:
Luca Ceresoli92895de2011-05-04 02:40:45 +00001487 return 0;
wdenk2d966952002-10-31 22:12:35 +00001488 }
Luca Ceresoli92895de2011-05-04 02:40:45 +00001489 return 0; /* OK */
wdenk2d966952002-10-31 22:12:35 +00001490}
1491/**********************************************************************/
1492
1493int
Joe Hershberger1203fcc2015-04-08 01:41:05 -05001494net_eth_hdr_size(void)
wdenka3d991b2004-04-15 21:48:45 +00001495{
1496 ushort myvlanid;
wdenk2d966952002-10-31 22:12:35 +00001497
Joe Hershberger4fd50552015-04-08 01:41:17 -05001498 myvlanid = ntohs(net_our_vlan);
wdenka3d991b2004-04-15 21:48:45 +00001499 if (myvlanid == (ushort)-1)
1500 myvlanid = VLAN_NONE;
1501
Luca Ceresoli3e38e422011-05-11 03:59:54 +00001502 return ((myvlanid & VLAN_IDMASK) == VLAN_NONE) ? ETHER_HDR_SIZE :
1503 VLAN_ETHER_HDR_SIZE;
wdenka3d991b2004-04-15 21:48:45 +00001504}
1505
Joe Hershberger1203fcc2015-04-08 01:41:05 -05001506int net_set_ether(uchar *xet, const uchar *dest_ethaddr, uint prot)
wdenk2d966952002-10-31 22:12:35 +00001507{
Joe Hershbergercb487f52012-05-23 07:58:06 +00001508 struct ethernet_hdr *et = (struct ethernet_hdr *)xet;
wdenka3d991b2004-04-15 21:48:45 +00001509 ushort myvlanid;
1510
Joe Hershberger4fd50552015-04-08 01:41:17 -05001511 myvlanid = ntohs(net_our_vlan);
wdenka3d991b2004-04-15 21:48:45 +00001512 if (myvlanid == (ushort)-1)
1513 myvlanid = VLAN_NONE;
wdenk2d966952002-10-31 22:12:35 +00001514
Joe Hershberger0adb5b72015-04-08 01:41:04 -05001515 memcpy(et->et_dest, dest_ethaddr, 6);
1516 memcpy(et->et_src, net_ethaddr, 6);
wdenka3d991b2004-04-15 21:48:45 +00001517 if ((myvlanid & VLAN_IDMASK) == VLAN_NONE) {
Luca Ceresolic819abe2011-05-04 02:40:46 +00001518 et->et_protlen = htons(prot);
wdenka3d991b2004-04-15 21:48:45 +00001519 return ETHER_HDR_SIZE;
1520 } else {
Joe Hershbergerc68cca32012-05-23 07:58:07 +00001521 struct vlan_ethernet_hdr *vet =
1522 (struct vlan_ethernet_hdr *)xet;
wdenk2d966952002-10-31 22:12:35 +00001523
wdenka3d991b2004-04-15 21:48:45 +00001524 vet->vet_vlan_type = htons(PROT_VLAN);
1525 vet->vet_tag = htons((0 << 5) | (myvlanid & VLAN_IDMASK));
1526 vet->vet_type = htons(prot);
1527 return VLAN_ETHER_HDR_SIZE;
1528 }
1529}
wdenk2d966952002-10-31 22:12:35 +00001530
Joe Hershbergere7111012012-05-23 07:59:16 +00001531int net_update_ether(struct ethernet_hdr *et, uchar *addr, uint prot)
1532{
1533 ushort protlen;
1534
1535 memcpy(et->et_dest, addr, 6);
Joe Hershberger0adb5b72015-04-08 01:41:04 -05001536 memcpy(et->et_src, net_ethaddr, 6);
Joe Hershbergere7111012012-05-23 07:59:16 +00001537 protlen = ntohs(et->et_protlen);
1538 if (protlen == PROT_VLAN) {
1539 struct vlan_ethernet_hdr *vet =
1540 (struct vlan_ethernet_hdr *)et;
1541 vet->vet_type = htons(prot);
1542 return VLAN_ETHER_HDR_SIZE;
1543 } else if (protlen > 1514) {
1544 et->et_protlen = htons(prot);
1545 return ETHER_HDR_SIZE;
1546 } else {
1547 /* 802.2 + SNAP */
1548 struct e802_hdr *et802 = (struct e802_hdr *)et;
1549 et802->et_prot = htons(prot);
1550 return E802_HDR_SIZE;
1551 }
1552}
1553
Duncan Hare5d457ec2018-06-24 15:40:41 -07001554void net_set_ip_header(uchar *pkt, struct in_addr dest, struct in_addr source,
1555 u16 pkt_len, u8 proto)
wdenk2d966952002-10-31 22:12:35 +00001556{
Joe Hershberger4b11c912012-05-23 07:59:07 +00001557 struct ip_udp_hdr *ip = (struct ip_udp_hdr *)pkt;
1558
1559 /*
1560 * Construct an IP header.
1561 */
1562 /* IP_HDR_SIZE / 4 (not including UDP) */
1563 ip->ip_hl_v = 0x45;
1564 ip->ip_tos = 0;
Duncan Hare5d457ec2018-06-24 15:40:41 -07001565 ip->ip_len = htons(pkt_len);
1566 ip->ip_p = proto;
Joe Hershbergerbc0571f2015-04-08 01:41:21 -05001567 ip->ip_id = htons(net_ip_id++);
Joe Hershberger4b11c912012-05-23 07:59:07 +00001568 ip->ip_off = htons(IP_FLAGS_DFRAG); /* Don't fragment */
1569 ip->ip_ttl = 255;
1570 ip->ip_sum = 0;
1571 /* already in network byte order */
Joe Hershberger049a95a2015-04-08 01:41:01 -05001572 net_copy_ip((void *)&ip->ip_src, &source);
Joe Hershberger4b11c912012-05-23 07:59:07 +00001573 /* already in network byte order */
Joe Hershberger049a95a2015-04-08 01:41:01 -05001574 net_copy_ip((void *)&ip->ip_dst, &dest);
Duncan Hare5d457ec2018-06-24 15:40:41 -07001575
1576 ip->ip_sum = compute_ip_checksum(ip, IP_HDR_SIZE);
Joe Hershberger4b11c912012-05-23 07:59:07 +00001577}
1578
Joe Hershberger049a95a2015-04-08 01:41:01 -05001579void net_set_udp_header(uchar *pkt, struct in_addr dest, int dport, int sport,
Joe Hershberger4b11c912012-05-23 07:59:07 +00001580 int len)
1581{
1582 struct ip_udp_hdr *ip = (struct ip_udp_hdr *)pkt;
wdenk2d966952002-10-31 22:12:35 +00001583
1584 /*
1585 * If the data is an odd number of bytes, zero the
1586 * byte after the last byte so that the checksum
1587 * will work.
1588 */
1589 if (len & 1)
Joe Hershberger4b11c912012-05-23 07:59:07 +00001590 pkt[IP_UDP_HDR_SIZE + len] = 0;
wdenk2d966952002-10-31 22:12:35 +00001591
Duncan Hare5d457ec2018-06-24 15:40:41 -07001592 net_set_ip_header(pkt, dest, net_ip, IP_UDP_HDR_SIZE + len,
1593 IPPROTO_UDP);
Joe Hershberger4b11c912012-05-23 07:59:07 +00001594
wdenk2d966952002-10-31 22:12:35 +00001595 ip->udp_src = htons(sport);
1596 ip->udp_dst = htons(dport);
Joe Hershberger594c26f2012-05-23 07:58:04 +00001597 ip->udp_len = htons(UDP_HDR_SIZE + len);
wdenk2d966952002-10-31 22:12:35 +00001598 ip->udp_xsum = 0;
wdenk2d966952002-10-31 22:12:35 +00001599}
1600
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001601void copy_filename(char *dst, const char *src, int size)
wdenk2d966952002-10-31 22:12:35 +00001602{
Joe Hershberger16cf1452018-07-03 19:36:41 -05001603 if (src && *src && (*src == '"')) {
wdenk2d966952002-10-31 22:12:35 +00001604 ++src;
1605 --size;
1606 }
1607
Joe Hershberger16cf1452018-07-03 19:36:41 -05001608 while ((--size > 0) && src && *src && (*src != '"'))
wdenk2d966952002-10-31 22:12:35 +00001609 *dst++ = *src++;
wdenk2d966952002-10-31 22:12:35 +00001610 *dst = '\0';
1611}
1612
Joe Hershberger3a66fcb2018-07-03 19:36:39 -05001613int is_serverip_in_cmd(void)
1614{
1615 return !!strchr(net_boot_file_name, ':');
1616}
1617
Joe Hershberger6ab12832018-07-03 19:36:43 -05001618int net_parse_bootfile(struct in_addr *ipaddr, char *filename, int max_len)
1619{
1620 char *colon;
Lyle Franklin85f8e032022-04-16 11:36:43 -04001621 struct in_addr ip;
1622 ip.s_addr = 0;
Joe Hershberger6ab12832018-07-03 19:36:43 -05001623
1624 if (net_boot_file_name[0] == '\0')
1625 return 0;
1626
1627 colon = strchr(net_boot_file_name, ':');
1628 if (colon) {
Lyle Franklin85f8e032022-04-16 11:36:43 -04001629 ip = string_to_ip(net_boot_file_name);
1630 if (ipaddr && ip.s_addr)
1631 *ipaddr = ip;
1632 }
1633 if (ip.s_addr) {
Joe Hershberger6ab12832018-07-03 19:36:43 -05001634 strncpy(filename, colon + 1, max_len);
1635 } else {
1636 strncpy(filename, net_boot_file_name, max_len);
1637 }
1638 filename[max_len - 1] = '\0';
1639
1640 return 1;
1641}
1642
Joe Hershberger049a95a2015-04-08 01:41:01 -05001643void ip_to_string(struct in_addr x, char *s)
wdenk2d966952002-10-31 22:12:35 +00001644{
Joe Hershberger049a95a2015-04-08 01:41:01 -05001645 x.s_addr = ntohl(x.s_addr);
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001646 sprintf(s, "%d.%d.%d.%d",
Joe Hershberger049a95a2015-04-08 01:41:01 -05001647 (int) ((x.s_addr >> 24) & 0xff),
1648 (int) ((x.s_addr >> 16) & 0xff),
1649 (int) ((x.s_addr >> 8) & 0xff),
1650 (int) ((x.s_addr >> 0) & 0xff)
wdenk6e592382004-04-18 17:39:38 +00001651 );
wdenk2d966952002-10-31 22:12:35 +00001652}
1653
Joe Hershberger4fd50552015-04-08 01:41:17 -05001654void vlan_to_string(ushort x, char *s)
wdenka3d991b2004-04-15 21:48:45 +00001655{
1656 x = ntohs(x);
1657
1658 if (x == (ushort)-1)
1659 x = VLAN_NONE;
1660
1661 if (x == VLAN_NONE)
1662 strcpy(s, "none");
1663 else
1664 sprintf(s, "%d", x & VLAN_IDMASK);
1665}
1666
Joe Hershberger4fd50552015-04-08 01:41:17 -05001667ushort string_to_vlan(const char *s)
wdenka3d991b2004-04-15 21:48:45 +00001668{
1669 ushort id;
1670
1671 if (s == NULL)
wdenkb9711de2004-04-25 13:18:40 +00001672 return htons(VLAN_NONE);
wdenka3d991b2004-04-15 21:48:45 +00001673
1674 if (*s < '0' || *s > '9')
1675 id = VLAN_NONE;
1676 else
Simon Glass0b1284e2021-07-24 09:03:30 -06001677 id = (ushort)dectoul(s, NULL);
wdenka3d991b2004-04-15 21:48:45 +00001678
wdenkb9711de2004-04-25 13:18:40 +00001679 return htons(id);
wdenka3d991b2004-04-15 21:48:45 +00001680}
1681
Simon Glass723806c2017-08-03 12:22:15 -06001682ushort env_get_vlan(char *var)
wdenka3d991b2004-04-15 21:48:45 +00001683{
Simon Glass00caae62017-08-03 12:22:12 -06001684 return string_to_vlan(env_get(var));
wdenka3d991b2004-04-15 21:48:45 +00001685}