blob: 9cb2aab09ded2cc28e63e6066463d4263342dbbf [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>
wdenk2d966952002-10-31 22:12:35 +0000121
wdenk2d966952002-10-31 22:12:35 +0000122/** BOOTP EXTENTIONS **/
123
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000124/* Our subnet mask (0=unknown) */
Joe Hershberger049a95a2015-04-08 01:41:01 -0500125struct in_addr net_netmask;
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000126/* Our gateways IP address */
Joe Hershberger049a95a2015-04-08 01:41:01 -0500127struct in_addr net_gateway;
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000128/* Our DNS IP address */
Joe Hershberger049a95a2015-04-08 01:41:01 -0500129struct in_addr net_dns_server;
Jon Loeliger1fe80d72007-07-09 22:08:34 -0500130#if defined(CONFIG_BOOTP_DNS2)
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000131/* Our 2nd DNS IP address */
Joe Hershberger049a95a2015-04-08 01:41:01 -0500132struct in_addr net_dns_server2;
stroesefe389a82003-08-28 14:17:32 +0000133#endif
wdenk2d966952002-10-31 22:12:35 +0000134
135/** END OF BOOTP EXTENTIONS **/
136
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000137/* Our ethernet address */
Joe Hershberger0adb5b72015-04-08 01:41:04 -0500138u8 net_ethaddr[6];
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000139/* Boot server enet address */
Joe Hershberger0adb5b72015-04-08 01:41:04 -0500140u8 net_server_ethaddr[6];
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000141/* Our IP addr (0 = unknown) */
Joe Hershberger049a95a2015-04-08 01:41:01 -0500142struct in_addr net_ip;
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000143/* Server IP addr (0 = unknown) */
Joe Hershberger049a95a2015-04-08 01:41:01 -0500144struct in_addr net_server_ip;
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000145/* Current receive packet */
Joe Hershberger1203fcc2015-04-08 01:41:05 -0500146uchar *net_rx_packet;
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000147/* Current rx packet length */
Joe Hershberger1203fcc2015-04-08 01:41:05 -0500148int net_rx_packet_len;
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000149/* IP packet ID */
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500150static unsigned net_ip_id;
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000151/* Ethernet bcast address */
Joe Hershberger0adb5b72015-04-08 01:41:04 -0500152const u8 net_bcast_ethaddr[6] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
153const u8 net_null_ethaddr[6];
Alexander Graf0efe1bc2016-05-06 21:01:01 +0200154#if defined(CONFIG_API) || defined(CONFIG_EFI_LOADER)
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500155void (*push_packet)(void *, int len) = 0;
Rafal Jaworowskif85b6072007-12-27 18:19:02 +0100156#endif
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000157/* Network loop state */
Joe Hershberger22f6e992012-05-23 07:59:14 +0000158enum net_loop_state net_state;
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000159/* Tried all network devices */
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500160int net_restart_wrap;
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000161/* Network loop restarted */
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500162static int net_restarted;
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000163/* At least one device configured */
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500164static int net_dev_exists;
wdenk2d966952002-10-31 22:12:35 +0000165
wdenk6e592382004-04-18 17:39:38 +0000166/* XXX in both little & big endian machines 0xFFFF == ntohs(-1) */
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000167/* default is without VLAN */
Joe Hershberger4fd50552015-04-08 01:41:17 -0500168ushort net_our_vlan = 0xFFFF;
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000169/* ditto */
Joe Hershberger4fd50552015-04-08 01:41:17 -0500170ushort net_native_vlan = 0xFFFF;
wdenka3d991b2004-04-15 21:48:45 +0000171
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000172/* Boot File name */
Jacob Stiffler11a69ff2015-09-30 10:12:05 -0400173char net_boot_file_name[1024];
Alexander Graf449312c2018-06-15 10:29:27 +0200174/* Indicates whether the file name was specified on the command line */
175bool net_boot_file_name_explicit;
Joe Hershberger14111572015-04-08 01:41:02 -0500176/* The actual transferred size of the bootfile (in bytes) */
177u32 net_boot_file_size;
178/* Boot file size in blocks as reported by the DHCP server */
179u32 net_boot_file_expected_size_in_blocks;
wdenk2d966952002-10-31 22:12:35 +0000180
Joe Hershberger1203fcc2015-04-08 01:41:05 -0500181static uchar net_pkt_buf[(PKTBUFSRX+1) * PKTSIZE_ALIGN + PKTALIGN];
Joe Hershberger2a504df2015-03-22 17:09:11 -0500182/* Receive packets */
183uchar *net_rx_packets[PKTBUFSRX];
Joe Hershbergerece223b2012-05-23 07:59:15 +0000184/* Current UDP RX packet handler */
185static rxhand_f *udp_packet_handler;
186/* Current ARP RX packet handler */
187static rxhand_f *arp_packet_handler;
Simon Glass39bccd22011-10-26 14:18:38 +0000188#ifdef CONFIG_CMD_TFTPPUT
Joe Hershbergerece223b2012-05-23 07:59:15 +0000189/* Current ICMP rx handler */
190static rxhand_icmp_f *packet_icmp_handler;
Simon Glass39bccd22011-10-26 14:18:38 +0000191#endif
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000192/* Current timeout handler */
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500193static thand_f *time_handler;
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000194/* Time base value */
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500195static ulong time_start;
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000196/* Current timeout value */
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500197static ulong time_delta;
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000198/* THE transmit packet */
Joe Hershberger1203fcc2015-04-08 01:41:05 -0500199uchar *net_tx_packet;
wdenk2d966952002-10-31 22:12:35 +0000200
Simon Glasse4bf0c52011-10-24 18:00:02 +0000201static int net_check_prereq(enum proto_t protocol);
wdenk2d966952002-10-31 22:12:35 +0000202
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500203static int net_try_count;
Remy Bohmer67b96e82009-10-28 22:13:39 +0100204
Jim Linb63056d2013-08-13 19:03:05 +0800205int __maybe_unused net_busy_flag;
206
wdenk2d966952002-10-31 22:12:35 +0000207/**********************************************************************/
wdenk73a8b272003-06-05 19:27:42 +0000208
Joe Hershbergerfd305632015-05-20 14:27:23 -0500209static int on_ipaddr(const char *name, const char *value, enum env_op op,
210 int flags)
211{
212 if (flags & H_PROGRAMMATIC)
213 return 0;
214
215 net_ip = string_to_ip(value);
216
217 return 0;
218}
219U_BOOT_ENV_CALLBACK(ipaddr, on_ipaddr);
220
221static int on_gatewayip(const char *name, const char *value, enum env_op op,
222 int flags)
223{
224 if (flags & H_PROGRAMMATIC)
225 return 0;
226
227 net_gateway = string_to_ip(value);
228
229 return 0;
230}
231U_BOOT_ENV_CALLBACK(gatewayip, on_gatewayip);
232
233static int on_netmask(const char *name, const char *value, enum env_op op,
234 int flags)
235{
236 if (flags & H_PROGRAMMATIC)
237 return 0;
238
239 net_netmask = string_to_ip(value);
240
241 return 0;
242}
243U_BOOT_ENV_CALLBACK(netmask, on_netmask);
244
245static int on_serverip(const char *name, const char *value, enum env_op op,
246 int flags)
247{
248 if (flags & H_PROGRAMMATIC)
249 return 0;
250
251 net_server_ip = string_to_ip(value);
252
253 return 0;
254}
255U_BOOT_ENV_CALLBACK(serverip, on_serverip);
256
257static int on_nvlan(const char *name, const char *value, enum env_op op,
258 int flags)
259{
260 if (flags & H_PROGRAMMATIC)
261 return 0;
262
263 net_native_vlan = string_to_vlan(value);
264
265 return 0;
266}
267U_BOOT_ENV_CALLBACK(nvlan, on_nvlan);
268
269static int on_vlan(const char *name, const char *value, enum env_op op,
270 int flags)
271{
272 if (flags & H_PROGRAMMATIC)
273 return 0;
274
275 net_our_vlan = string_to_vlan(value);
276
277 return 0;
278}
279U_BOOT_ENV_CALLBACK(vlan, on_vlan);
280
281#if defined(CONFIG_CMD_DNS)
282static int on_dnsip(const char *name, const char *value, enum env_op op,
283 int flags)
284{
285 if (flags & H_PROGRAMMATIC)
286 return 0;
287
288 net_dns_server = string_to_ip(value);
289
290 return 0;
291}
292U_BOOT_ENV_CALLBACK(dnsip, on_dnsip);
293#endif
294
Simon Glasse4a3d572011-10-27 06:24:32 +0000295/*
296 * Check if autoload is enabled. If so, use either NFS or TFTP to download
297 * the boot file.
298 */
299void net_auto_load(void)
300{
Tom Rinia6ab4b52019-12-05 19:35:07 -0500301#if defined(CONFIG_CMD_NFS) && !defined(CONFIG_SPL_BUILD)
Simon Glass00caae62017-08-03 12:22:12 -0600302 const char *s = env_get("autoload");
Simon Glasse4a3d572011-10-27 06:24:32 +0000303
Joe Hershbergerec8a2522012-12-11 22:16:22 -0600304 if (s != NULL && strcmp(s, "NFS") == 0) {
Joe Hershberger3855cad2018-07-03 19:36:40 -0500305 if (net_check_prereq(NFS)) {
306/* We aren't expecting to get a serverip, so just accept the assigned IP */
Simon Glass43407532021-12-18 11:27:52 -0700307 if (IS_ENABLED(CONFIG_BOOTP_SERVERIP)) {
308 net_set_state(NETLOOP_SUCCESS);
309 } else {
310 printf("Cannot autoload with NFS\n");
311 net_set_state(NETLOOP_FAIL);
312 }
Joe Hershberger3855cad2018-07-03 19:36:40 -0500313 return;
314 }
Joe Hershbergerec8a2522012-12-11 22:16:22 -0600315 /*
316 * Use NFS to load the bootfile.
317 */
Joe Hershberger68c76a32015-04-08 01:41:10 -0500318 nfs_start();
Joe Hershbergerec8a2522012-12-11 22:16:22 -0600319 return;
320 }
Simon Glasse4a3d572011-10-27 06:24:32 +0000321#endif
Simon Glassbfebc8c2017-08-03 12:22:13 -0600322 if (env_get_yesno("autoload") == 0) {
Joe Hershbergerec8a2522012-12-11 22:16:22 -0600323 /*
324 * Just use BOOTP/RARP to configure system;
325 * Do not use TFTP to load the bootfile.
326 */
327 net_set_state(NETLOOP_SUCCESS);
328 return;
Simon Glasse4a3d572011-10-27 06:24:32 +0000329 }
Joe Hershberger3855cad2018-07-03 19:36:40 -0500330 if (net_check_prereq(TFTPGET)) {
331/* We aren't expecting to get a serverip, so just accept the assigned IP */
Simon Glass43407532021-12-18 11:27:52 -0700332 if (IS_ENABLED(CONFIG_BOOTP_SERVERIP)) {
333 net_set_state(NETLOOP_SUCCESS);
334 } else {
335 printf("Cannot autoload with TFTPGET\n");
336 net_set_state(NETLOOP_FAIL);
337 }
Joe Hershberger3855cad2018-07-03 19:36:40 -0500338 return;
339 }
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500340 tftp_start(TFTPGET);
Simon Glasse4a3d572011-10-27 06:24:32 +0000341}
342
Sean Andersonc3f02782020-09-12 17:45:43 -0400343static int net_init_loop(void)
Heiko Schocher2f70c492009-02-10 09:38:52 +0100344{
Jim Lin7315cfd2013-05-17 17:41:03 +0800345 if (eth_get_dev())
Joe Hershberger0adb5b72015-04-08 01:41:04 -0500346 memcpy(net_ethaddr, eth_get_ethaddr(), 6);
Sean Andersonc3f02782020-09-12 17:45:43 -0400347 else
348 /*
349 * Not ideal, but there's no way to get the actual error, and I
350 * don't feel like fixing all the users of eth_get_dev to deal
351 * with errors.
352 */
353 return -ENONET;
Michael Zaidman3c172c42009-04-04 01:43:00 +0300354
Sean Andersonc3f02782020-09-12 17:45:43 -0400355 return 0;
Heiko Schocher2f70c492009-02-10 09:38:52 +0100356}
357
Joe Hershbergerece223b2012-05-23 07:59:15 +0000358static void net_clear_handlers(void)
359{
360 net_set_udp_handler(NULL);
361 net_set_arp_handler(NULL);
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500362 net_set_timeout_handler(0, NULL);
Joe Hershbergerece223b2012-05-23 07:59:15 +0000363}
364
365static void net_cleanup_loop(void)
366{
367 net_clear_handlers();
368}
369
Sean Andersonc3f02782020-09-12 17:45:43 -0400370int net_init(void)
Joe Hershberger46c495d2012-05-23 07:59:22 +0000371{
372 static int first_call = 1;
373
374 if (first_call) {
375 /*
376 * Setup packet buffers, aligned correctly.
377 */
378 int i;
379
Joe Hershberger1203fcc2015-04-08 01:41:05 -0500380 net_tx_packet = &net_pkt_buf[0] + (PKTALIGN - 1);
381 net_tx_packet -= (ulong)net_tx_packet % PKTALIGN;
Joe Hershberger2a504df2015-03-22 17:09:11 -0500382 for (i = 0; i < PKTBUFSRX; i++) {
Joe Hershberger1203fcc2015-04-08 01:41:05 -0500383 net_rx_packets[i] = net_tx_packet +
384 (i + 1) * PKTSIZE_ALIGN;
Joe Hershberger2a504df2015-03-22 17:09:11 -0500385 }
Joe Hershberger85d25e02015-04-08 01:41:08 -0500386 arp_init();
Joe Hershberger46c495d2012-05-23 07:59:22 +0000387 net_clear_handlers();
388
389 /* Only need to setup buffer pointers once. */
390 first_call = 0;
Ying-Chun Liu (PaulLiu)a3bf1932022-11-08 14:17:28 +0800391 if (IS_ENABLED(CONFIG_PROT_TCP))
392 tcp_set_tcp_state(TCP_CLOSED);
Joe Hershberger46c495d2012-05-23 07:59:22 +0000393 }
394
Sean Andersonc3f02782020-09-12 17:45:43 -0400395 return net_init_loop();
Joe Hershberger46c495d2012-05-23 07:59:22 +0000396}
397
wdenk73a8b272003-06-05 19:27:42 +0000398/**********************************************************************/
wdenk2d966952002-10-31 22:12:35 +0000399/*
400 * Main network processing loop.
401 */
402
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500403int net_loop(enum proto_t protocol)
wdenk2d966952002-10-31 22:12:35 +0000404{
Joe Hershberger60304592015-03-22 17:09:24 -0500405 int ret = -EINVAL;
Leonid Iziumtsev60177b22018-05-08 15:55:50 +0200406 enum net_loop_state prev_net_state = net_state;
wdenk2d966952002-10-31 22:12:35 +0000407
Marek Szyprowskidef7a5c2020-06-15 11:15:57 +0200408#if defined(CONFIG_CMD_PING)
409 if (protocol != PING)
410 net_ping_ip.s_addr = 0;
411#endif
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500412 net_restarted = 0;
413 net_dev_exists = 0;
414 net_try_count = 1;
415 debug_cond(DEBUG_INT_STATE, "--- net_loop Entry\n");
wdenk73a8b272003-06-05 19:27:42 +0000416
Samuel Mendoza-Jonas09bd3d02022-08-08 21:46:03 +0930417#ifdef CONFIG_PHY_NCSI
418 if (phy_interface_is_ncsi() && protocol != NCSI && !ncsi_active()) {
419 printf("%s: configuring NCSI first\n", __func__);
420 if (net_loop(NCSI) < 0)
421 return ret;
422 eth_init_state_only();
423 goto restart;
424 }
425#endif
426
Simon Glass573f14f2011-12-10 11:08:06 +0000427 bootstage_mark_name(BOOTSTAGE_ID_ETH_START, "eth_start");
Joe Hershberger46c495d2012-05-23 07:59:22 +0000428 net_init();
Yang Liud9506cd2020-12-21 14:44:39 +1100429 if (eth_is_on_demand_init()) {
wdenkb1bf6f22005-04-03 14:52:59 +0000430 eth_halt();
Joe Hershbergerf8be7d62012-08-03 10:59:08 +0000431 eth_set_current();
Joe Hershberger60304592015-03-22 17:09:24 -0500432 ret = eth_init();
433 if (ret < 0) {
Joe Hershbergerf8be7d62012-08-03 10:59:08 +0000434 eth_halt();
Joe Hershberger60304592015-03-22 17:09:24 -0500435 return ret;
Joe Hershbergerf8be7d62012-08-03 10:59:08 +0000436 }
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500437 } else {
Joe Hershbergerd2eaec62015-03-22 17:09:06 -0500438 eth_init_state_only();
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500439 }
Samuel Mendoza-Jonas4b290d42022-08-08 21:46:04 +0930440
wdenk2d966952002-10-31 22:12:35 +0000441restart:
Jim Linb63056d2013-08-13 19:03:05 +0800442#ifdef CONFIG_USB_KEYBOARD
443 net_busy_flag = 0;
444#endif
Joe Hershberger22f6e992012-05-23 07:59:14 +0000445 net_set_state(NETLOOP_CONTINUE);
wdenk2d966952002-10-31 22:12:35 +0000446
447 /*
448 * Start the ball rolling with the given start function. From
449 * here on, this code is a state machine driven by received
450 * packets and timer events.
451 */
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500452 debug_cond(DEBUG_INT_STATE, "--- net_loop Init\n");
453 net_init_loop();
wdenk2d966952002-10-31 22:12:35 +0000454
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000455 switch (net_check_prereq(protocol)) {
wdenk2d966952002-10-31 22:12:35 +0000456 case 1:
457 /* network not configured */
wdenkb1bf6f22005-04-03 14:52:59 +0000458 eth_halt();
Leonid Iziumtsev60177b22018-05-08 15:55:50 +0200459 net_set_state(prev_net_state);
Joe Hershberger60304592015-03-22 17:09:24 -0500460 return -ENODEV;
wdenk2d966952002-10-31 22:12:35 +0000461
wdenk2d966952002-10-31 22:12:35 +0000462 case 2:
463 /* network device not configured */
464 break;
wdenk2d966952002-10-31 22:12:35 +0000465
466 case 0:
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500467 net_dev_exists = 1;
Joe Hershberger14111572015-04-08 01:41:02 -0500468 net_boot_file_size = 0;
wdenk2d966952002-10-31 22:12:35 +0000469 switch (protocol) {
Krebs, Olaf808f13d2020-03-09 14:27:55 +0000470#ifdef CONFIG_CMD_TFTPBOOT
Simon Glasse4bf0c52011-10-24 18:00:02 +0000471 case TFTPGET:
Simon Glass1fb7cd42011-10-24 18:00:07 +0000472#ifdef CONFIG_CMD_TFTPPUT
473 case TFTPPUT:
474#endif
wdenk2d966952002-10-31 22:12:35 +0000475 /* always use ARP to get server ethernet address */
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500476 tftp_start(protocol);
wdenk2d966952002-10-31 22:12:35 +0000477 break;
Krebs, Olaf808f13d2020-03-09 14:27:55 +0000478#endif
Luca Ceresoli7a83af02011-05-17 00:03:40 +0000479#ifdef CONFIG_CMD_TFTPSRV
480 case TFTPSRV:
Joe Hershberger8885c5f2015-04-08 01:41:07 -0500481 tftp_start_server();
Luca Ceresoli7a83af02011-05-17 00:03:40 +0000482 break;
483#endif
Alex Kiernanf73a7df2018-05-29 15:30:53 +0000484#ifdef CONFIG_UDP_FUNCTION_FASTBOOT
485 case FASTBOOT:
486 fastboot_start_server();
487 break;
488#endif
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500489#if defined(CONFIG_CMD_DHCP)
wdenk2d966952002-10-31 22:12:35 +0000490 case DHCP:
Joe Hershberger7044c6b2015-04-08 01:41:09 -0500491 bootp_reset();
Joe Hershberger049a95a2015-04-08 01:41:01 -0500492 net_ip.s_addr = 0;
Joe Hershberger7044c6b2015-04-08 01:41:09 -0500493 dhcp_request(); /* Basically same as BOOTP */
wdenk2d966952002-10-31 22:12:35 +0000494 break;
Jon Loeliger610f2e92007-07-10 11:05:02 -0500495#endif
Krebs, Olaf808f13d2020-03-09 14:27:55 +0000496#if defined(CONFIG_CMD_BOOTP)
wdenk2d966952002-10-31 22:12:35 +0000497 case BOOTP:
Joe Hershberger7044c6b2015-04-08 01:41:09 -0500498 bootp_reset();
Joe Hershberger049a95a2015-04-08 01:41:01 -0500499 net_ip.s_addr = 0;
Joe Hershberger7044c6b2015-04-08 01:41:09 -0500500 bootp_request();
wdenk2d966952002-10-31 22:12:35 +0000501 break;
Krebs, Olaf808f13d2020-03-09 14:27:55 +0000502#endif
Peter Tyserbf6cb242010-09-30 11:25:48 -0500503#if defined(CONFIG_CMD_RARP)
wdenk2d966952002-10-31 22:12:35 +0000504 case RARP:
Joe Hershberger698d78e2015-04-08 01:41:11 -0500505 rarp_try = 0;
Joe Hershberger049a95a2015-04-08 01:41:01 -0500506 net_ip.s_addr = 0;
Joe Hershberger698d78e2015-04-08 01:41:11 -0500507 rarp_request();
wdenk2d966952002-10-31 22:12:35 +0000508 break;
Peter Tyserbf6cb242010-09-30 11:25:48 -0500509#endif
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500510#if defined(CONFIG_CMD_PING)
wdenk73a8b272003-06-05 19:27:42 +0000511 case PING:
Joe Hershbergera36b12f2012-05-23 07:58:02 +0000512 ping_start();
wdenk73a8b272003-06-05 19:27:42 +0000513 break;
514#endif
Tom Rinia6ab4b52019-12-05 19:35:07 -0500515#if defined(CONFIG_CMD_NFS) && !defined(CONFIG_SPL_BUILD)
wdenkcbd8a352004-02-24 02:00:03 +0000516 case NFS:
Joe Hershberger68c76a32015-04-08 01:41:10 -0500517 nfs_start();
wdenkcbd8a352004-02-24 02:00:03 +0000518 break;
519#endif
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500520#if defined(CONFIG_CMD_CDP)
wdenka3d991b2004-04-15 21:48:45 +0000521 case CDP:
Joe Hershberger6aede5b2015-04-08 01:41:14 -0500522 cdp_start();
wdenka3d991b2004-04-15 21:48:45 +0000523 break;
524#endif
Holger Dengler66c89ee2017-07-20 10:10:55 +0200525#if defined(CONFIG_NETCONSOLE) && !defined(CONFIG_SPL_BUILD)
wdenk68ceb292004-08-02 21:11:11 +0000526 case NETCONS:
Joe Hershberger6a38a5f2015-04-08 01:41:16 -0500527 nc_start();
wdenk68ceb292004-08-02 21:11:11 +0000528 break;
529#endif
Robin Getz1a32bf42009-07-20 14:53:54 -0400530#if defined(CONFIG_CMD_DNS)
531 case DNS:
Joe Hershberger786eac52015-04-08 01:41:15 -0500532 dns_start();
Robin Getz1a32bf42009-07-20 14:53:54 -0400533 break;
534#endif
Joe Hershbergerd22c3382012-05-23 08:00:12 +0000535#if defined(CONFIG_CMD_LINK_LOCAL)
536 case LINKLOCAL:
537 link_local_start();
538 break;
539#endif
Lothar Feltend8970da2018-06-22 22:29:54 +0200540#if defined(CONFIG_CMD_WOL)
541 case WOL:
542 wol_start();
543 break;
544#endif
Samuel Mendoza-Jonas09bd3d02022-08-08 21:46:03 +0930545#if defined(CONFIG_PHY_NCSI)
546 case NCSI:
547 ncsi_probe_packages();
548 break;
549#endif
wdenk2d966952002-10-31 22:12:35 +0000550 default:
551 break;
552 }
553
Philippe Reynesb43ea1b2020-09-18 14:13:00 +0200554 if (IS_ENABLED(CONFIG_PROT_UDP) && protocol == UDP)
555 udp_start();
556
wdenk2d966952002-10-31 22:12:35 +0000557 break;
558 }
559
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500560#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
Luca Ceresoli3e38e422011-05-11 03:59:54 +0000561#if defined(CONFIG_SYS_FAULT_ECHO_LINK_DOWN) && \
Uri Mashiach2d8d1902017-01-19 10:51:45 +0200562 defined(CONFIG_LED_STATUS) && \
563 defined(CONFIG_LED_STATUS_RED)
wdenkfc3e2162003-10-08 22:33:00 +0000564 /*
wdenk42d1f032003-10-15 23:53:47 +0000565 * Echo the inverted link state to the fault LED.
wdenkfc3e2162003-10-08 22:33:00 +0000566 */
Luca Ceresolid3c65b02011-05-04 02:40:43 +0000567 if (miiphy_link(eth_get_dev()->name, CONFIG_SYS_FAULT_MII_ADDR))
Uri Mashiach2d8d1902017-01-19 10:51:45 +0200568 status_led_set(CONFIG_LED_STATUS_RED, CONFIG_LED_STATUS_OFF);
Luca Ceresolid3c65b02011-05-04 02:40:43 +0000569 else
Uri Mashiach2d8d1902017-01-19 10:51:45 +0200570 status_led_set(CONFIG_LED_STATUS_RED, CONFIG_LED_STATUS_ON);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200571#endif /* CONFIG_SYS_FAULT_ECHO_LINK_DOWN, ... */
wdenkfc3e2162003-10-08 22:33:00 +0000572#endif /* CONFIG_MII, ... */
Jim Linb63056d2013-08-13 19:03:05 +0800573#ifdef CONFIG_USB_KEYBOARD
574 net_busy_flag = 1;
575#endif
wdenk2d966952002-10-31 22:12:35 +0000576
577 /*
578 * Main packet reception loop. Loop receiving packets until
Joe Hershberger22f6e992012-05-23 07:59:14 +0000579 * someone sets `net_state' to a state that terminates.
wdenk2d966952002-10-31 22:12:35 +0000580 */
581 for (;;) {
Stefan Roese29caf932022-09-02 14:10:46 +0200582 schedule();
Joe Hershbergerc5a75332015-12-21 16:31:35 -0600583 if (arp_timeout_check() > 0)
584 time_start = get_timer(0);
585
wdenk2d966952002-10-31 22:12:35 +0000586 /*
587 * Check the ethernet for a new packet. The ethernet
588 * receive routine will process it.
Joe Hershberger60304592015-03-22 17:09:24 -0500589 * Most drivers return the most recent packet size, but not
590 * errors that may have happened.
wdenk2d966952002-10-31 22:12:35 +0000591 */
Guennadi Liakhovetski40cb90e2008-04-03 17:04:19 +0200592 eth_rx();
wdenk2d966952002-10-31 22:12:35 +0000593
594 /*
595 * Abort if ctrl-c was pressed.
596 */
597 if (ctrlc()) {
Joe Hershbergere94070c2012-05-23 07:59:24 +0000598 /* cancel any ARP that may not have completed */
Joe Hershberger049a95a2015-04-08 01:41:01 -0500599 net_arp_wait_packet_ip.s_addr = 0;
Joe Hershbergere94070c2012-05-23 07:59:24 +0000600
Joe Hershbergerece223b2012-05-23 07:59:15 +0000601 net_cleanup_loop();
wdenk8bde7f72003-06-27 21:31:46 +0000602 eth_halt();
Joe Hershbergerf8be7d62012-08-03 10:59:08 +0000603 /* Invalidate the last protocol */
604 eth_set_last_protocol(BOOTP);
605
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000606 puts("\nAbort\n");
Joe Hershberger4ef8d532012-05-23 08:01:04 +0000607 /* include a debug print as well incase the debug
608 messages are directed to stderr */
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500609 debug_cond(DEBUG_INT_STATE, "--- net_loop Abort!\n");
Michal Simek19a4fba2015-08-21 08:49:48 +0200610 ret = -EINTR;
Simon Glass4793ee62011-10-24 18:00:01 +0000611 goto done;
wdenk2d966952002-10-31 22:12:35 +0000612 }
613
wdenk2d966952002-10-31 22:12:35 +0000614 /*
615 * Check for a timeout, and run the timeout handler
616 * if we have one.
617 */
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500618 if (time_handler &&
619 ((get_timer(0) - time_start) > time_delta)) {
wdenk2d966952002-10-31 22:12:35 +0000620 thand_f *x;
621
Jon Loeliger643d1ab2007-07-09 17:45:14 -0500622#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000623#if defined(CONFIG_SYS_FAULT_ECHO_LINK_DOWN) && \
Uri Mashiach2d8d1902017-01-19 10:51:45 +0200624 defined(CONFIG_LED_STATUS) && \
625 defined(CONFIG_LED_STATUS_RED)
wdenkfc3e2162003-10-08 22:33:00 +0000626 /*
wdenk42d1f032003-10-15 23:53:47 +0000627 * Echo the inverted link state to the fault LED.
wdenkfc3e2162003-10-08 22:33:00 +0000628 */
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000629 if (miiphy_link(eth_get_dev()->name,
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500630 CONFIG_SYS_FAULT_MII_ADDR))
Uri Mashiach2d8d1902017-01-19 10:51:45 +0200631 status_led_set(CONFIG_LED_STATUS_RED,
632 CONFIG_LED_STATUS_OFF);
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500633 else
Uri Mashiach2d8d1902017-01-19 10:51:45 +0200634 status_led_set(CONFIG_LED_STATUS_RED,
635 CONFIG_LED_STATUS_ON);
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000636#endif /* CONFIG_SYS_FAULT_ECHO_LINK_DOWN, ... */
wdenkfc3e2162003-10-08 22:33:00 +0000637#endif /* CONFIG_MII, ... */
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500638 debug_cond(DEBUG_INT_STATE, "--- net_loop timeout\n");
639 x = time_handler;
640 time_handler = (thand_f *)0;
wdenk2d966952002-10-31 22:12:35 +0000641 (*x)();
642 }
643
Joe Hershberger5c421332015-03-22 17:09:07 -0500644 if (net_state == NETLOOP_FAIL)
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500645 ret = net_start_again();
wdenk2d966952002-10-31 22:12:35 +0000646
Joe Hershberger22f6e992012-05-23 07:59:14 +0000647 switch (net_state) {
wdenk2d966952002-10-31 22:12:35 +0000648 case NETLOOP_RESTART:
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500649 net_restarted = 1;
wdenk2d966952002-10-31 22:12:35 +0000650 goto restart;
651
652 case NETLOOP_SUCCESS:
Joe Hershbergerece223b2012-05-23 07:59:15 +0000653 net_cleanup_loop();
Joe Hershberger14111572015-04-08 01:41:02 -0500654 if (net_boot_file_size > 0) {
655 printf("Bytes transferred = %d (%x hex)\n",
656 net_boot_file_size, net_boot_file_size);
Simon Glass018f5302017-08-03 12:22:10 -0600657 env_set_hex("filesize", net_boot_file_size);
Simon Glassbb872dd2019-12-28 10:45:02 -0700658 env_set_hex("fileaddr", image_load_addr);
wdenk2d966952002-10-31 22:12:35 +0000659 }
Samuel Mendoza-Jonas09bd3d02022-08-08 21:46:03 +0930660 if (protocol != NETCONS && protocol != NCSI)
Joe Hershbergerf8be7d62012-08-03 10:59:08 +0000661 eth_halt();
662 else
663 eth_halt_state_only();
664
665 eth_set_last_protocol(protocol);
666
Joe Hershberger14111572015-04-08 01:41:02 -0500667 ret = net_boot_file_size;
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500668 debug_cond(DEBUG_INT_STATE, "--- net_loop Success!\n");
Simon Glass4793ee62011-10-24 18:00:01 +0000669 goto done;
wdenk2d966952002-10-31 22:12:35 +0000670
671 case NETLOOP_FAIL:
Joe Hershbergerece223b2012-05-23 07:59:15 +0000672 net_cleanup_loop();
Joe Hershbergerf8be7d62012-08-03 10:59:08 +0000673 /* Invalidate the last protocol */
674 eth_set_last_protocol(BOOTP);
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500675 debug_cond(DEBUG_INT_STATE, "--- net_loop Fail!\n");
Thomas RIENOESSLa735e6e2018-11-21 15:56:07 +0100676 ret = -ENONET;
Simon Glass4793ee62011-10-24 18:00:01 +0000677 goto done;
Joe Hershberger22f6e992012-05-23 07:59:14 +0000678
679 case NETLOOP_CONTINUE:
680 continue;
wdenk2d966952002-10-31 22:12:35 +0000681 }
682 }
Simon Glass4793ee62011-10-24 18:00:01 +0000683
684done:
Jim Linb63056d2013-08-13 19:03:05 +0800685#ifdef CONFIG_USB_KEYBOARD
686 net_busy_flag = 0;
687#endif
Simon Glass39bccd22011-10-26 14:18:38 +0000688#ifdef CONFIG_CMD_TFTPPUT
Simon Glass4793ee62011-10-24 18:00:01 +0000689 /* Clear out the handlers */
Joe Hershbergerece223b2012-05-23 07:59:15 +0000690 net_set_udp_handler(NULL);
Simon Glass4793ee62011-10-24 18:00:01 +0000691 net_set_icmp_handler(NULL);
Simon Glass39bccd22011-10-26 14:18:38 +0000692#endif
Leonid Iziumtsev60177b22018-05-08 15:55:50 +0200693 net_set_state(prev_net_state);
Ramon Fried3eaac632019-07-18 21:43:30 +0300694
695#if defined(CONFIG_CMD_PCAP)
696 if (pcap_active())
697 pcap_print_status();
698#endif
Simon Glass4793ee62011-10-24 18:00:01 +0000699 return ret;
wdenk2d966952002-10-31 22:12:35 +0000700}
701
702/**********************************************************************/
703
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500704static void start_again_timeout_handler(void)
wdenk2d966952002-10-31 22:12:35 +0000705{
Joe Hershberger22f6e992012-05-23 07:59:14 +0000706 net_set_state(NETLOOP_RESTART);
wdenk2d966952002-10-31 22:12:35 +0000707}
708
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500709int net_start_again(void)
wdenk2d966952002-10-31 22:12:35 +0000710{
wdenk6e592382004-04-18 17:39:38 +0000711 char *nretry;
Remy Bohmer67b96e82009-10-28 22:13:39 +0100712 int retry_forever = 0;
713 unsigned long retrycnt = 0;
Joe Hershberger60304592015-03-22 17:09:24 -0500714 int ret;
wdenka3d991b2004-04-15 21:48:45 +0000715
Simon Glass00caae62017-08-03 12:22:12 -0600716 nretry = env_get("netretry");
Remy Bohmer67b96e82009-10-28 22:13:39 +0100717 if (nretry) {
718 if (!strcmp(nretry, "yes"))
719 retry_forever = 1;
720 else if (!strcmp(nretry, "no"))
721 retrycnt = 0;
722 else if (!strcmp(nretry, "once"))
723 retrycnt = 1;
724 else
725 retrycnt = simple_strtoul(nretry, NULL, 0);
Joe Hershberger5c421332015-03-22 17:09:07 -0500726 } else {
727 retrycnt = 0;
728 retry_forever = 0;
729 }
Remy Bohmer67b96e82009-10-28 22:13:39 +0100730
Leonid Iziumtsev17d413b2018-03-09 15:29:06 +0100731 if ((!retry_forever) && (net_try_count > retrycnt)) {
Remy Bohmer67b96e82009-10-28 22:13:39 +0100732 eth_halt();
Joe Hershberger22f6e992012-05-23 07:59:14 +0000733 net_set_state(NETLOOP_FAIL);
Joe Hershberger60304592015-03-22 17:09:24 -0500734 /*
735 * We don't provide a way for the protocol to return an error,
736 * but this is almost always the reason.
737 */
738 return -ETIMEDOUT;
wdenka3d991b2004-04-15 21:48:45 +0000739 }
Remy Bohmer67b96e82009-10-28 22:13:39 +0100740
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500741 net_try_count++;
Remy Bohmer67b96e82009-10-28 22:13:39 +0100742
Luca Ceresoli4f63acd2011-05-11 03:59:56 +0000743 eth_halt();
Matthias Fuchs8b0c5c12007-12-27 16:58:41 +0100744#if !defined(CONFIG_NET_DO_NOT_TRY_ANOTHER)
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500745 eth_try_another(!net_restarted);
Matthias Fuchs8b0c5c12007-12-27 16:58:41 +0100746#endif
Joe Hershberger60304592015-03-22 17:09:24 -0500747 ret = eth_init();
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500748 if (net_restart_wrap) {
749 net_restart_wrap = 0;
750 if (net_dev_exists) {
751 net_set_timeout_handler(10000UL,
752 start_again_timeout_handler);
Joe Hershbergerece223b2012-05-23 07:59:15 +0000753 net_set_udp_handler(NULL);
wdenk6e592382004-04-18 17:39:38 +0000754 } else {
Joe Hershberger22f6e992012-05-23 07:59:14 +0000755 net_set_state(NETLOOP_FAIL);
wdenk2d966952002-10-31 22:12:35 +0000756 }
wdenk6e592382004-04-18 17:39:38 +0000757 } else {
Joe Hershberger22f6e992012-05-23 07:59:14 +0000758 net_set_state(NETLOOP_RESTART);
wdenk2d966952002-10-31 22:12:35 +0000759 }
Joe Hershberger60304592015-03-22 17:09:24 -0500760 return ret;
wdenk2d966952002-10-31 22:12:35 +0000761}
762
763/**********************************************************************/
764/*
765 * Miscelaneous bits.
766 */
767
Joe Hershbergerece223b2012-05-23 07:59:15 +0000768static void dummy_handler(uchar *pkt, unsigned dport,
Joe Hershberger049a95a2015-04-08 01:41:01 -0500769 struct in_addr sip, unsigned sport,
Joe Hershbergerece223b2012-05-23 07:59:15 +0000770 unsigned len)
Joe Hershbergerd280d3f2012-05-23 07:58:01 +0000771{
Joe Hershbergerd280d3f2012-05-23 07:58:01 +0000772}
773
Joe Hershbergerece223b2012-05-23 07:59:15 +0000774rxhand_f *net_get_udp_handler(void)
wdenk2d966952002-10-31 22:12:35 +0000775{
Joe Hershbergerece223b2012-05-23 07:59:15 +0000776 return udp_packet_handler;
777}
778
779void net_set_udp_handler(rxhand_f *f)
780{
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500781 debug_cond(DEBUG_INT_STATE, "--- net_loop UDP handler set (%p)\n", f);
Joe Hershbergerece223b2012-05-23 07:59:15 +0000782 if (f == NULL)
783 udp_packet_handler = dummy_handler;
784 else
785 udp_packet_handler = f;
786}
787
788rxhand_f *net_get_arp_handler(void)
789{
790 return arp_packet_handler;
791}
792
793void net_set_arp_handler(rxhand_f *f)
794{
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500795 debug_cond(DEBUG_INT_STATE, "--- net_loop ARP handler set (%p)\n", f);
Joe Hershbergerece223b2012-05-23 07:59:15 +0000796 if (f == NULL)
797 arp_packet_handler = dummy_handler;
798 else
799 arp_packet_handler = f;
wdenk2d966952002-10-31 22:12:35 +0000800}
801
Simon Glass39bccd22011-10-26 14:18:38 +0000802#ifdef CONFIG_CMD_TFTPPUT
Simon Glass4793ee62011-10-24 18:00:01 +0000803void net_set_icmp_handler(rxhand_icmp_f *f)
804{
805 packet_icmp_handler = f;
806}
Simon Glass39bccd22011-10-26 14:18:38 +0000807#endif
wdenk2d966952002-10-31 22:12:35 +0000808
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500809void net_set_timeout_handler(ulong iv, thand_f *f)
wdenk2d966952002-10-31 22:12:35 +0000810{
811 if (iv == 0) {
Joe Hershberger4ef8d532012-05-23 08:01:04 +0000812 debug_cond(DEBUG_INT_STATE,
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500813 "--- net_loop timeout handler cancelled\n");
814 time_handler = (thand_f *)0;
wdenk2d966952002-10-31 22:12:35 +0000815 } else {
Joe Hershberger4ef8d532012-05-23 08:01:04 +0000816 debug_cond(DEBUG_INT_STATE,
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500817 "--- net_loop timeout handler set (%p)\n", f);
818 time_handler = f;
819 time_start = get_timer(0);
820 time_delta = iv * CONFIG_SYS_HZ / 1000;
wdenk2d966952002-10-31 22:12:35 +0000821 }
822}
823
Joe Hershbergerac3f26c2018-09-26 16:49:02 -0500824uchar *net_get_async_tx_pkt_buf(void)
825{
826 if (arp_is_waiting())
827 return arp_tx_packet; /* If we are waiting, we already sent */
828 else
829 return net_tx_packet;
830}
831
Joe Hershberger1203fcc2015-04-08 01:41:05 -0500832int net_send_udp_packet(uchar *ether, struct in_addr dest, int dport, int sport,
Joe Hershberger206d07f2012-05-23 07:58:10 +0000833 int payload_len)
wdenk73a8b272003-06-05 19:27:42 +0000834{
Duncan Hare5d457ec2018-06-24 15:40:41 -0700835 return net_send_ip_packet(ether, dest, dport, sport, payload_len,
836 IPPROTO_UDP, 0, 0, 0);
837}
838
Ying-Chun Liu (PaulLiu)a3bf1932022-11-08 14:17:28 +0800839#if defined(CONFIG_PROT_TCP)
840int net_send_tcp_packet(int payload_len, int dport, int sport, u8 action,
841 u32 tcp_seq_num, u32 tcp_ack_num)
842{
843 return net_send_ip_packet(net_server_ethaddr, net_server_ip, dport,
844 sport, payload_len, IPPROTO_TCP, action,
845 tcp_seq_num, tcp_ack_num);
846}
847#endif
848
Duncan Hare5d457ec2018-06-24 15:40:41 -0700849int net_send_ip_packet(uchar *ether, struct in_addr dest, int dport, int sport,
850 int payload_len, int proto, u8 action, u32 tcp_seq_num,
851 u32 tcp_ack_num)
852{
wdenka3d991b2004-04-15 21:48:45 +0000853 uchar *pkt;
Joe Hershberger92146372012-05-23 07:59:08 +0000854 int eth_hdr_size;
855 int pkt_hdr_size;
wdenka3d991b2004-04-15 21:48:45 +0000856
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500857 /* make sure the net_tx_packet is initialized (net_init() was called) */
Joe Hershberger1203fcc2015-04-08 01:41:05 -0500858 assert(net_tx_packet != NULL);
859 if (net_tx_packet == NULL)
Joe Hershberger46c495d2012-05-23 07:59:22 +0000860 return -1;
861
wdenk73a8b272003-06-05 19:27:42 +0000862 /* convert to new style broadcast */
Joe Hershberger049a95a2015-04-08 01:41:01 -0500863 if (dest.s_addr == 0)
864 dest.s_addr = 0xFFFFFFFF;
wdenk2d966952002-10-31 22:12:35 +0000865
wdenk73a8b272003-06-05 19:27:42 +0000866 /* if broadcast, make the ether address a broadcast and don't do ARP */
Joe Hershberger049a95a2015-04-08 01:41:01 -0500867 if (dest.s_addr == 0xFFFFFFFF)
Joe Hershberger0adb5b72015-04-08 01:41:04 -0500868 ether = (uchar *)net_bcast_ethaddr;
wdenk73a8b272003-06-05 19:27:42 +0000869
Joe Hershberger1203fcc2015-04-08 01:41:05 -0500870 pkt = (uchar *)net_tx_packet;
wdenk73a8b272003-06-05 19:27:42 +0000871
Joe Hershberger1203fcc2015-04-08 01:41:05 -0500872 eth_hdr_size = net_set_ether(pkt, ether, PROT_IP);
Duncan Hare5d457ec2018-06-24 15:40:41 -0700873
874 switch (proto) {
875 case IPPROTO_UDP:
876 net_set_udp_header(pkt + eth_hdr_size, dest, dport, sport,
877 payload_len);
878 pkt_hdr_size = eth_hdr_size + IP_UDP_HDR_SIZE;
879 break;
Ying-Chun Liu (PaulLiu)a3bf1932022-11-08 14:17:28 +0800880#if defined(CONFIG_PROT_TCP)
881 case IPPROTO_TCP:
882 pkt_hdr_size = eth_hdr_size
883 + tcp_set_tcp_header(pkt + eth_hdr_size, dport, sport,
884 payload_len, action, tcp_seq_num,
885 tcp_ack_num);
886 break;
887#endif
Duncan Hare5d457ec2018-06-24 15:40:41 -0700888 default:
889 return -EINVAL;
890 }
Robin Getz0ebf04c2009-07-23 03:01:03 -0400891
Joe Hershbergere94070c2012-05-23 07:59:24 +0000892 /* if MAC address was not discovered yet, do an ARP request */
Joe Hershberger0adb5b72015-04-08 01:41:04 -0500893 if (memcmp(ether, net_null_ethaddr, 6) == 0) {
Joe Hershberger4ef8d532012-05-23 08:01:04 +0000894 debug_cond(DEBUG_DEV_PKT, "sending ARP for %pI4\n", &dest);
Joe Hershberger92146372012-05-23 07:59:08 +0000895
896 /* save the ip and eth addr for the packet to send after arp */
Joe Hershberger049a95a2015-04-08 01:41:01 -0500897 net_arp_wait_packet_ip = dest;
Joe Hershberger85d25e02015-04-08 01:41:08 -0500898 arp_wait_packet_ethaddr = ether;
wdenka3d991b2004-04-15 21:48:45 +0000899
wdenk73a8b272003-06-05 19:27:42 +0000900 /* size of the waiting packet */
Joe Hershberger85d25e02015-04-08 01:41:08 -0500901 arp_wait_tx_packet_size = pkt_hdr_size + payload_len;
wdenk73a8b272003-06-05 19:27:42 +0000902
903 /* and do the ARP request */
Joe Hershberger85d25e02015-04-08 01:41:08 -0500904 arp_wait_try = 1;
905 arp_wait_timer_start = get_timer(0);
906 arp_request();
wdenk73a8b272003-06-05 19:27:42 +0000907 return 1; /* waiting */
Joe Hershberger92146372012-05-23 07:59:08 +0000908 } else {
Joe Hershberger4ef8d532012-05-23 08:01:04 +0000909 debug_cond(DEBUG_DEV_PKT, "sending UDP to %pI4/%pM\n",
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500910 &dest, ether);
Joe Hershberger1203fcc2015-04-08 01:41:05 -0500911 net_send_packet(net_tx_packet, pkt_hdr_size + payload_len);
Joe Hershberger92146372012-05-23 07:59:08 +0000912 return 0; /* transmitted */
wdenk73a8b272003-06-05 19:27:42 +0000913 }
wdenk73a8b272003-06-05 19:27:42 +0000914}
915
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +0200916#ifdef CONFIG_IP_DEFRAG
917/*
918 * This function collects fragments in a single packet, according
919 * to the algorithm in RFC815. It returns NULL or the pointer to
920 * a complete packet, in static storage
921 */
Joe Hershbergeraa7a6482016-08-15 14:42:15 -0500922#define IP_PKTSIZE (CONFIG_NET_MAXDEFRAG)
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +0200923
Joe Hershbergerc5c59df2012-05-23 07:58:05 +0000924#define IP_MAXUDP (IP_PKTSIZE - IP_HDR_SIZE)
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +0200925
926/*
927 * this is the packet being assembled, either data or frag control.
928 * Fragments go by 8 bytes, so this union must be 8 bytes long
929 */
930struct hole {
931 /* first_byte is address of this structure */
932 u16 last_byte; /* last byte in this hole + 1 (begin of next hole) */
933 u16 next_hole; /* index of next (in 8-b blocks), 0 == none */
934 u16 prev_hole; /* index of prev, 0 == none */
935 u16 unused;
936};
937
Joe Hershbergerbc0571f2015-04-08 01:41:21 -0500938static struct ip_udp_hdr *__net_defragment(struct ip_udp_hdr *ip, int *lenp)
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +0200939{
Joe Hershberger48522bb2012-05-15 08:59:08 +0000940 static uchar pkt_buff[IP_PKTSIZE] __aligned(PKTALIGN);
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +0200941 static u16 first_hole, total_len;
942 struct hole *payload, *thisfrag, *h, *newh;
Joe Hershberger594c26f2012-05-23 07:58:04 +0000943 struct ip_udp_hdr *localip = (struct ip_udp_hdr *)pkt_buff;
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +0200944 uchar *indata = (uchar *)ip;
945 int offset8, start, len, done = 0;
946 u16 ip_off = ntohs(ip->ip_off);
947
Rasmus Villemoes1817c382022-10-14 19:43:39 +0200948 /*
949 * Calling code already rejected <, but we don't have to deal
950 * with an IP fragment with no payload.
951 */
952 if (ntohs(ip->ip_len) <= IP_HDR_SIZE)
Fabio Estevamb85d1302022-05-26 11:14:37 -0300953 return NULL;
954
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +0200955 /* payload starts after IP header, this fragment is in there */
Joe Hershbergerc5c59df2012-05-23 07:58:05 +0000956 payload = (struct hole *)(pkt_buff + IP_HDR_SIZE);
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +0200957 offset8 = (ip_off & IP_OFFS);
958 thisfrag = payload + offset8;
959 start = offset8 * 8;
Joe Hershbergerc5c59df2012-05-23 07:58:05 +0000960 len = ntohs(ip->ip_len) - IP_HDR_SIZE;
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +0200961
Rasmus Villemoes1817c382022-10-14 19:43:39 +0200962 /* All but last fragment must have a multiple-of-8 payload. */
963 if ((len & 7) && (ip_off & IP_FLAGS_MFRAG))
964 return NULL;
965
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +0200966 if (start + len > IP_MAXUDP) /* fragment extends too far */
967 return NULL;
968
969 if (!total_len || localip->ip_id != ip->ip_id) {
970 /* new (or different) packet, reset structs */
971 total_len = 0xffff;
972 payload[0].last_byte = ~0;
973 payload[0].next_hole = 0;
974 payload[0].prev_hole = 0;
975 first_hole = 0;
976 /* any IP header will work, copy the first we received */
Joe Hershbergerc5c59df2012-05-23 07:58:05 +0000977 memcpy(localip, ip, IP_HDR_SIZE);
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +0200978 }
979
980 /*
981 * What follows is the reassembly algorithm. We use the payload
982 * array as a linked list of hole descriptors, as each hole starts
983 * at a multiple of 8 bytes. However, last byte can be whatever value,
984 * so it is represented as byte count, not as 8-byte blocks.
985 */
986
987 h = payload + first_hole;
988 while (h->last_byte < start) {
989 if (!h->next_hole) {
990 /* no hole that far away */
991 return NULL;
992 }
993 h = payload + h->next_hole;
994 }
995
Fillod Stephanee397e592010-06-11 19:26:43 +0200996 /* last fragment may be 1..7 bytes, the "+7" forces acceptance */
997 if (offset8 + ((len + 7) / 8) <= h - payload) {
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +0200998 /* no overlap with holes (dup fragment?) */
999 return NULL;
1000 }
1001
1002 if (!(ip_off & IP_FLAGS_MFRAG)) {
1003 /* no more fragmentss: truncate this (last) hole */
1004 total_len = start + len;
1005 h->last_byte = start + len;
1006 }
1007
1008 /*
Rasmus Villemoes06869682022-10-17 09:52:51 +02001009 * There is some overlap: fix the hole list. This code deals
1010 * with a fragment that overlaps with two different holes
1011 * (thus being a superset of a previously-received fragment)
1012 * by only using the part of the fragment that fits in the
1013 * first hole.
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +02001014 */
Rasmus Villemoes06869682022-10-17 09:52:51 +02001015 if (h->last_byte < start + len)
1016 len = h->last_byte - start;
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +02001017
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001018 if ((h >= thisfrag) && (h->last_byte <= start + len)) {
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +02001019 /* complete overlap with hole: remove hole */
1020 if (!h->prev_hole && !h->next_hole) {
1021 /* last remaining hole */
1022 done = 1;
1023 } else if (!h->prev_hole) {
1024 /* first hole */
1025 first_hole = h->next_hole;
1026 payload[h->next_hole].prev_hole = 0;
1027 } else if (!h->next_hole) {
1028 /* last hole */
1029 payload[h->prev_hole].next_hole = 0;
1030 } else {
1031 /* in the middle of the list */
1032 payload[h->next_hole].prev_hole = h->prev_hole;
1033 payload[h->prev_hole].next_hole = h->next_hole;
1034 }
1035
1036 } else if (h->last_byte <= start + len) {
1037 /* overlaps with final part of the hole: shorten this hole */
1038 h->last_byte = start;
1039
1040 } else if (h >= thisfrag) {
1041 /* overlaps with initial part of the hole: move this hole */
1042 newh = thisfrag + (len / 8);
1043 *newh = *h;
1044 h = newh;
1045 if (h->next_hole)
1046 payload[h->next_hole].prev_hole = (h - payload);
1047 if (h->prev_hole)
1048 payload[h->prev_hole].next_hole = (h - payload);
1049 else
1050 first_hole = (h - payload);
1051
1052 } else {
1053 /* fragment sits in the middle: split the hole */
1054 newh = thisfrag + (len / 8);
1055 *newh = *h;
1056 h->last_byte = start;
1057 h->next_hole = (newh - payload);
1058 newh->prev_hole = (h - payload);
1059 if (newh->next_hole)
1060 payload[newh->next_hole].prev_hole = (newh - payload);
1061 }
1062
1063 /* finally copy this fragment and possibly return whole packet */
Joe Hershbergerc5c59df2012-05-23 07:58:05 +00001064 memcpy((uchar *)thisfrag, indata + IP_HDR_SIZE, len);
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +02001065 if (!done)
1066 return NULL;
1067
Joe Hershbergerc5c59df2012-05-23 07:58:05 +00001068 *lenp = total_len + IP_HDR_SIZE;
Rasmus Villemoes06653c72022-10-14 19:43:40 +02001069 localip->ip_len = htons(*lenp);
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +02001070 return localip;
1071}
1072
Joe Hershbergerbc0571f2015-04-08 01:41:21 -05001073static inline struct ip_udp_hdr *net_defragment(struct ip_udp_hdr *ip,
1074 int *lenp)
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +02001075{
1076 u16 ip_off = ntohs(ip->ip_off);
1077 if (!(ip_off & (IP_OFFS | IP_FLAGS_MFRAG)))
1078 return ip; /* not a fragment */
Joe Hershbergerbc0571f2015-04-08 01:41:21 -05001079 return __net_defragment(ip, lenp);
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +02001080}
1081
1082#else /* !CONFIG_IP_DEFRAG */
1083
Joe Hershbergerbc0571f2015-04-08 01:41:21 -05001084static inline struct ip_udp_hdr *net_defragment(struct ip_udp_hdr *ip,
1085 int *lenp)
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +02001086{
1087 u16 ip_off = ntohs(ip->ip_off);
1088 if (!(ip_off & (IP_OFFS | IP_FLAGS_MFRAG)))
1089 return ip; /* not a fragment */
1090 return NULL;
1091}
1092#endif
wdenka3d991b2004-04-15 21:48:45 +00001093
Simon Glass8f79bb12011-10-24 18:00:00 +00001094/**
1095 * Receive an ICMP packet. We deal with REDIRECT and PING here, and silently
1096 * drop others.
1097 *
1098 * @parma ip IP packet containing the ICMP
1099 */
Joe Hershberger594c26f2012-05-23 07:58:04 +00001100static void receive_icmp(struct ip_udp_hdr *ip, int len,
Joe Hershberger049a95a2015-04-08 01:41:01 -05001101 struct in_addr src_ip, struct ethernet_hdr *et)
Simon Glass8f79bb12011-10-24 18:00:00 +00001102{
Joe Hershbergere0a63072012-05-23 07:58:09 +00001103 struct icmp_hdr *icmph = (struct icmp_hdr *)&ip->udp_src;
Simon Glass8f79bb12011-10-24 18:00:00 +00001104
1105 switch (icmph->type) {
1106 case ICMP_REDIRECT:
1107 if (icmph->code != ICMP_REDIR_HOST)
1108 return;
1109 printf(" ICMP Host Redirect to %pI4 ",
Joe Hershbergerbc0571f2015-04-08 01:41:21 -05001110 &icmph->un.gateway);
Simon Glass8f79bb12011-10-24 18:00:00 +00001111 break;
Simon Glass8f79bb12011-10-24 18:00:00 +00001112 default:
Joe Hershbergera36b12f2012-05-23 07:58:02 +00001113#if defined(CONFIG_CMD_PING)
1114 ping_receive(et, ip, len);
1115#endif
Simon Glass39bccd22011-10-26 14:18:38 +00001116#ifdef CONFIG_CMD_TFTPPUT
Simon Glass4793ee62011-10-24 18:00:01 +00001117 if (packet_icmp_handler)
1118 packet_icmp_handler(icmph->type, icmph->code,
Joe Hershbergerbc0571f2015-04-08 01:41:21 -05001119 ntohs(ip->udp_dst), src_ip,
1120 ntohs(ip->udp_src), icmph->un.data,
1121 ntohs(ip->udp_len));
Simon Glass39bccd22011-10-26 14:18:38 +00001122#endif
Simon Glass8f79bb12011-10-24 18:00:00 +00001123 break;
1124 }
1125}
1126
Joe Hershberger2a504df2015-03-22 17:09:11 -05001127void net_process_received_packet(uchar *in_packet, int len)
wdenk2d966952002-10-31 22:12:35 +00001128{
Joe Hershbergercb487f52012-05-23 07:58:06 +00001129 struct ethernet_hdr *et;
Joe Hershberger594c26f2012-05-23 07:58:04 +00001130 struct ip_udp_hdr *ip;
Joe Hershberger049a95a2015-04-08 01:41:01 -05001131 struct in_addr dst_ip;
1132 struct in_addr src_ip;
Joe Hershberger8d353eb2012-05-23 07:58:12 +00001133 int eth_proto;
Jon Loeliger643d1ab2007-07-09 17:45:14 -05001134#if defined(CONFIG_CMD_CDP)
wdenka3d991b2004-04-15 21:48:45 +00001135 int iscdp;
1136#endif
1137 ushort cti = 0, vlanid = VLAN_NONE, myvlanid, mynvlanid;
wdenk2d966952002-10-31 22:12:35 +00001138
Joe Hershberger4ef8d532012-05-23 08:01:04 +00001139 debug_cond(DEBUG_NET_PKT, "packet received\n");
wdenka3d991b2004-04-15 21:48:45 +00001140
Ramon Fried3eaac632019-07-18 21:43:30 +03001141#if defined(CONFIG_CMD_PCAP)
1142 pcap_post(in_packet, len, false);
1143#endif
Joe Hershberger1203fcc2015-04-08 01:41:05 -05001144 net_rx_packet = in_packet;
1145 net_rx_packet_len = len;
Joe Hershberger2a504df2015-03-22 17:09:11 -05001146 et = (struct ethernet_hdr *)in_packet;
wdenka3d991b2004-04-15 21:48:45 +00001147
1148 /* too small packet? */
1149 if (len < ETHER_HDR_SIZE)
1150 return;
1151
Alexander Graf0efe1bc2016-05-06 21:01:01 +02001152#if defined(CONFIG_API) || defined(CONFIG_EFI_LOADER)
Rafal Jaworowskif85b6072007-12-27 18:19:02 +01001153 if (push_packet) {
Joe Hershberger2a504df2015-03-22 17:09:11 -05001154 (*push_packet)(in_packet, len);
Rafal Jaworowskif85b6072007-12-27 18:19:02 +01001155 return;
1156 }
1157#endif
1158
Jon Loeliger643d1ab2007-07-09 17:45:14 -05001159#if defined(CONFIG_CMD_CDP)
wdenka3d991b2004-04-15 21:48:45 +00001160 /* keep track if packet is CDP */
Joe Hershberger17351882012-05-23 07:58:00 +00001161 iscdp = is_cdp_packet(et->et_dest);
wdenka3d991b2004-04-15 21:48:45 +00001162#endif
1163
Joe Hershberger4fd50552015-04-08 01:41:17 -05001164 myvlanid = ntohs(net_our_vlan);
wdenka3d991b2004-04-15 21:48:45 +00001165 if (myvlanid == (ushort)-1)
1166 myvlanid = VLAN_NONE;
Joe Hershberger4fd50552015-04-08 01:41:17 -05001167 mynvlanid = ntohs(net_native_vlan);
wdenka3d991b2004-04-15 21:48:45 +00001168 if (mynvlanid == (ushort)-1)
1169 mynvlanid = VLAN_NONE;
wdenk2d966952002-10-31 22:12:35 +00001170
Joe Hershberger8d353eb2012-05-23 07:58:12 +00001171 eth_proto = ntohs(et->et_protlen);
wdenk2d966952002-10-31 22:12:35 +00001172
Joe Hershberger8d353eb2012-05-23 07:58:12 +00001173 if (eth_proto < 1514) {
Joe Hershbergercb487f52012-05-23 07:58:06 +00001174 struct e802_hdr *et802 = (struct e802_hdr *)et;
wdenk2d966952002-10-31 22:12:35 +00001175 /*
Joe Hershbergerda5ebe22012-05-23 07:58:11 +00001176 * Got a 802.2 packet. Check the other protocol field.
1177 * XXX VLAN over 802.2+SNAP not implemented!
wdenk2d966952002-10-31 22:12:35 +00001178 */
Joe Hershberger8d353eb2012-05-23 07:58:12 +00001179 eth_proto = ntohs(et802->et_prot);
wdenka3d991b2004-04-15 21:48:45 +00001180
Joe Hershberger2a504df2015-03-22 17:09:11 -05001181 ip = (struct ip_udp_hdr *)(in_packet + E802_HDR_SIZE);
wdenk2d966952002-10-31 22:12:35 +00001182 len -= E802_HDR_SIZE;
wdenka3d991b2004-04-15 21:48:45 +00001183
Joe Hershberger8d353eb2012-05-23 07:58:12 +00001184 } else if (eth_proto != PROT_VLAN) { /* normal packet */
Joe Hershberger2a504df2015-03-22 17:09:11 -05001185 ip = (struct ip_udp_hdr *)(in_packet + ETHER_HDR_SIZE);
wdenk2d966952002-10-31 22:12:35 +00001186 len -= ETHER_HDR_SIZE;
wdenka3d991b2004-04-15 21:48:45 +00001187
1188 } else { /* VLAN packet */
Joe Hershbergerc68cca32012-05-23 07:58:07 +00001189 struct vlan_ethernet_hdr *vet =
1190 (struct vlan_ethernet_hdr *)et;
wdenka3d991b2004-04-15 21:48:45 +00001191
Joe Hershberger4ef8d532012-05-23 08:01:04 +00001192 debug_cond(DEBUG_NET_PKT, "VLAN packet received\n");
Robin Getz0ebf04c2009-07-23 03:01:03 -04001193
wdenka3d991b2004-04-15 21:48:45 +00001194 /* too small packet? */
1195 if (len < VLAN_ETHER_HDR_SIZE)
1196 return;
1197
1198 /* if no VLAN active */
Joe Hershberger4fd50552015-04-08 01:41:17 -05001199 if ((ntohs(net_our_vlan) & VLAN_IDMASK) == VLAN_NONE
Jon Loeliger643d1ab2007-07-09 17:45:14 -05001200#if defined(CONFIG_CMD_CDP)
wdenka3d991b2004-04-15 21:48:45 +00001201 && iscdp == 0
1202#endif
1203 )
1204 return;
1205
1206 cti = ntohs(vet->vet_tag);
1207 vlanid = cti & VLAN_IDMASK;
Joe Hershberger8d353eb2012-05-23 07:58:12 +00001208 eth_proto = ntohs(vet->vet_type);
wdenka3d991b2004-04-15 21:48:45 +00001209
Joe Hershberger2a504df2015-03-22 17:09:11 -05001210 ip = (struct ip_udp_hdr *)(in_packet + VLAN_ETHER_HDR_SIZE);
wdenka3d991b2004-04-15 21:48:45 +00001211 len -= VLAN_ETHER_HDR_SIZE;
wdenk2d966952002-10-31 22:12:35 +00001212 }
1213
Joe Hershberger4ef8d532012-05-23 08:01:04 +00001214 debug_cond(DEBUG_NET_PKT, "Receive from protocol 0x%x\n", eth_proto);
wdenk2d966952002-10-31 22:12:35 +00001215
Jon Loeliger643d1ab2007-07-09 17:45:14 -05001216#if defined(CONFIG_CMD_CDP)
wdenka3d991b2004-04-15 21:48:45 +00001217 if (iscdp) {
Joe Hershberger0b4c5ff2012-05-23 07:58:13 +00001218 cdp_receive((uchar *)ip, len);
wdenka3d991b2004-04-15 21:48:45 +00001219 return;
1220 }
1221#endif
1222
1223 if ((myvlanid & VLAN_IDMASK) != VLAN_NONE) {
1224 if (vlanid == VLAN_NONE)
1225 vlanid = (mynvlanid & VLAN_IDMASK);
1226 /* not matched? */
1227 if (vlanid != (myvlanid & VLAN_IDMASK))
1228 return;
1229 }
1230
Joe Hershberger8d353eb2012-05-23 07:58:12 +00001231 switch (eth_proto) {
wdenk2d966952002-10-31 22:12:35 +00001232 case PROT_ARP:
Joe Hershberger85d25e02015-04-08 01:41:08 -05001233 arp_receive(et, ip, len);
wdenk289f9322005-01-12 00:15:14 +00001234 break;
wdenk2d966952002-10-31 22:12:35 +00001235
Peter Tyserbf6cb242010-09-30 11:25:48 -05001236#ifdef CONFIG_CMD_RARP
wdenk2d966952002-10-31 22:12:35 +00001237 case PROT_RARP:
Joe Hershberger8b9c5322012-05-23 07:58:03 +00001238 rarp_receive(ip, len);
wdenk2d966952002-10-31 22:12:35 +00001239 break;
Peter Tyserbf6cb242010-09-30 11:25:48 -05001240#endif
wdenk2d966952002-10-31 22:12:35 +00001241 case PROT_IP:
Joe Hershberger4ef8d532012-05-23 08:01:04 +00001242 debug_cond(DEBUG_NET_PKT, "Got IP\n");
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +02001243 /* Before we start poking the header, make sure it is there */
Rasmus Villemoesad359d82022-10-14 19:43:38 +02001244 if (len < IP_HDR_SIZE) {
Joe Hershberger594c26f2012-05-23 07:58:04 +00001245 debug("len bad %d < %lu\n", len,
Rasmus Villemoesad359d82022-10-14 19:43:38 +02001246 (ulong)IP_HDR_SIZE);
wdenk2d966952002-10-31 22:12:35 +00001247 return;
1248 }
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +02001249 /* Check the packet length */
wdenk2d966952002-10-31 22:12:35 +00001250 if (len < ntohs(ip->ip_len)) {
Joe Hershberger4ef8d532012-05-23 08:01:04 +00001251 debug("len bad %d < %d\n", len, ntohs(ip->ip_len));
wdenk2d966952002-10-31 22:12:35 +00001252 return;
1253 }
1254 len = ntohs(ip->ip_len);
Rasmus Villemoesad359d82022-10-14 19:43:38 +02001255 if (len < IP_HDR_SIZE) {
1256 debug("bad ip->ip_len %d < %d\n", len, (int)IP_HDR_SIZE);
1257 return;
1258 }
Joe Hershberger4ef8d532012-05-23 08:01:04 +00001259 debug_cond(DEBUG_NET_PKT, "len=%d, v=%02x\n",
Joe Hershbergerbc0571f2015-04-08 01:41:21 -05001260 len, ip->ip_hl_v & 0xff);
Robin Getz0ebf04c2009-07-23 03:01:03 -04001261
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +02001262 /* Can't deal with anything except IPv4 */
Luca Ceresolid3c65b02011-05-04 02:40:43 +00001263 if ((ip->ip_hl_v & 0xf0) != 0x40)
wdenk2d966952002-10-31 22:12:35 +00001264 return;
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +02001265 /* Can't deal with IP options (headers != 20 bytes) */
Rasmus Villemoesb0fcc482022-10-14 19:43:37 +02001266 if ((ip->ip_hl_v & 0x0f) != 0x05)
Remy Bohmer6b52cfe2008-06-03 15:48:17 +02001267 return;
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +02001268 /* Check the Checksum of the header */
Simon Glass0da0fcd2015-01-19 22:16:08 -07001269 if (!ip_checksum_ok((uchar *)ip, IP_HDR_SIZE)) {
Joe Hershberger4ef8d532012-05-23 08:01:04 +00001270 debug("checksum bad\n");
wdenk2d966952002-10-31 22:12:35 +00001271 return;
1272 }
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +02001273 /* If it is not for us, ignore it */
Joe Hershberger049a95a2015-04-08 01:41:01 -05001274 dst_ip = net_read_ip(&ip->ip_dst);
1275 if (net_ip.s_addr && dst_ip.s_addr != net_ip.s_addr &&
1276 dst_ip.s_addr != 0xFFFFFFFF) {
Luca Ceresolic819abe2011-05-04 02:40:46 +00001277 return;
wdenk2d966952002-10-31 22:12:35 +00001278 }
Luca Ceresoli03eb1292011-04-18 06:19:50 +00001279 /* Read source IP address for later use */
Joe Hershberger049a95a2015-04-08 01:41:01 -05001280 src_ip = net_read_ip(&ip->ip_src);
wdenk2d966952002-10-31 22:12:35 +00001281 /*
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +02001282 * The function returns the unchanged packet if it's not
1283 * a fragment, and either the complete packet or NULL if
1284 * it is a fragment (if !CONFIG_IP_DEFRAG, it returns NULL)
1285 */
Joe Hershbergerbc0571f2015-04-08 01:41:21 -05001286 ip = net_defragment(ip, &len);
Luca Ceresoliccb9ebe2011-05-04 02:40:47 +00001287 if (!ip)
Alessandro Rubini5cfaa4e2009-08-07 13:58:56 +02001288 return;
1289 /*
wdenk2d966952002-10-31 22:12:35 +00001290 * watch for ICMP host redirects
1291 *
wdenk8bde7f72003-06-27 21:31:46 +00001292 * There is no real handler code (yet). We just watch
1293 * for ICMP host redirect messages. In case anybody
1294 * sees these messages: please contact me
1295 * (wd@denx.de), or - even better - send me the
1296 * necessary fixes :-)
wdenk2d966952002-10-31 22:12:35 +00001297 *
wdenk8bde7f72003-06-27 21:31:46 +00001298 * Note: in all cases where I have seen this so far
1299 * it was a problem with the router configuration,
1300 * for instance when a router was configured in the
1301 * BOOTP reply, but the TFTP server was on the same
1302 * subnet. So this is probably a warning that your
1303 * configuration might be wrong. But I'm not really
1304 * sure if there aren't any other situations.
Simon Glass4793ee62011-10-24 18:00:01 +00001305 *
1306 * Simon Glass <sjg@chromium.org>: We get an ICMP when
1307 * we send a tftp packet to a dead connection, or when
1308 * there is no server at the other end.
wdenk2d966952002-10-31 22:12:35 +00001309 */
1310 if (ip->ip_p == IPPROTO_ICMP) {
Simon Glass8f79bb12011-10-24 18:00:00 +00001311 receive_icmp(ip, len, src_ip, et);
1312 return;
Ying-Chun Liu (PaulLiu)a3bf1932022-11-08 14:17:28 +08001313#if defined(CONFIG_PROT_TCP)
1314 } else if (ip->ip_p == IPPROTO_TCP) {
1315 debug_cond(DEBUG_DEV_PKT,
1316 "TCP PH (to=%pI4, from=%pI4, len=%d)\n",
1317 &dst_ip, &src_ip, len);
1318
1319 rxhand_tcp_f((union tcp_build_pkt *)ip, len);
1320 return;
1321#endif
wdenk2d966952002-10-31 22:12:35 +00001322 } else if (ip->ip_p != IPPROTO_UDP) { /* Only UDP packets */
1323 return;
1324 }
1325
Rasmus Villemoes06653c72022-10-14 19:43:40 +02001326 if (ntohs(ip->udp_len) < UDP_HDR_SIZE || ntohs(ip->udp_len) > len - IP_HDR_SIZE)
liucheng (G)fe728802019-08-29 13:47:33 +00001327 return;
1328
Joe Hershberger4ef8d532012-05-23 08:01:04 +00001329 debug_cond(DEBUG_DEV_PKT,
Joe Hershbergerbc0571f2015-04-08 01:41:21 -05001330 "received UDP (to=%pI4, from=%pI4, len=%d)\n",
1331 &dst_ip, &src_ip, len);
Joe Hershberger4ef8d532012-05-23 08:01:04 +00001332
Simon Glass4b37fd12021-12-18 11:27:49 -07001333 if (IS_ENABLED(CONFIG_UDP_CHECKSUM) && ip->udp_xsum != 0) {
Wolfgang Denkb2f50802005-08-12 23:43:12 +02001334 ulong xsum;
Heinrich Schuchardt85244232019-11-05 12:48:19 +01001335 u8 *sumptr;
Stefan Roese8534bf92005-08-12 20:06:52 +02001336 ushort sumlen;
1337
1338 xsum = ip->ip_p;
1339 xsum += (ntohs(ip->udp_len));
Joe Hershberger049a95a2015-04-08 01:41:01 -05001340 xsum += (ntohl(ip->ip_src.s_addr) >> 16) & 0x0000ffff;
1341 xsum += (ntohl(ip->ip_src.s_addr) >> 0) & 0x0000ffff;
1342 xsum += (ntohl(ip->ip_dst.s_addr) >> 16) & 0x0000ffff;
1343 xsum += (ntohl(ip->ip_dst.s_addr) >> 0) & 0x0000ffff;
Stefan Roese8534bf92005-08-12 20:06:52 +02001344
1345 sumlen = ntohs(ip->udp_len);
Heinrich Schuchardt85244232019-11-05 12:48:19 +01001346 sumptr = (u8 *)&ip->udp_src;
Stefan Roese8534bf92005-08-12 20:06:52 +02001347
1348 while (sumlen > 1) {
Heinrich Schuchardt85244232019-11-05 12:48:19 +01001349 /* inlined ntohs() to avoid alignment errors */
1350 xsum += (sumptr[0] << 8) + sumptr[1];
1351 sumptr += 2;
Stefan Roese8534bf92005-08-12 20:06:52 +02001352 sumlen -= 2;
1353 }
Heinrich Schuchardt85244232019-11-05 12:48:19 +01001354 if (sumlen > 0)
1355 xsum += (sumptr[0] << 8) + sumptr[0];
Stefan Roese8534bf92005-08-12 20:06:52 +02001356 while ((xsum >> 16) != 0) {
Luca Ceresoli3e38e422011-05-11 03:59:54 +00001357 xsum = (xsum & 0x0000ffff) +
1358 ((xsum >> 16) & 0x0000ffff);
Stefan Roese8534bf92005-08-12 20:06:52 +02001359 }
1360 if ((xsum != 0x00000000) && (xsum != 0x0000ffff)) {
Wolfgang Denk9b55a252008-07-11 01:16:00 +02001361 printf(" UDP wrong checksum %08lx %08x\n",
Joe Hershbergerbc0571f2015-04-08 01:41:21 -05001362 xsum, ntohs(ip->udp_xsum));
Stefan Roese8534bf92005-08-12 20:06:52 +02001363 return;
1364 }
1365 }
Stefan Roese8534bf92005-08-12 20:06:52 +02001366
Holger Dengler66c89ee2017-07-20 10:10:55 +02001367#if defined(CONFIG_NETCONSOLE) && !defined(CONFIG_SPL_BUILD)
Joe Hershberger594c26f2012-05-23 07:58:04 +00001368 nc_input_packet((uchar *)ip + IP_UDP_HDR_SIZE,
Joe Hershbergerece223b2012-05-23 07:59:15 +00001369 src_ip,
Joe Hershbergerbc0571f2015-04-08 01:41:21 -05001370 ntohs(ip->udp_dst),
Joe Hershbergerece223b2012-05-23 07:59:15 +00001371 ntohs(ip->udp_src),
1372 ntohs(ip->udp_len) - UDP_HDR_SIZE);
Joe Hershbergerbc0571f2015-04-08 01:41:21 -05001373#endif
1374 /*
1375 * IP header OK. Pass the packet to the current handler.
1376 */
1377 (*udp_packet_handler)((uchar *)ip + IP_UDP_HDR_SIZE,
1378 ntohs(ip->udp_dst),
1379 src_ip,
1380 ntohs(ip->udp_src),
1381 ntohs(ip->udp_len) - UDP_HDR_SIZE);
wdenk2d966952002-10-31 22:12:35 +00001382 break;
Lothar Feltend8970da2018-06-22 22:29:54 +02001383#ifdef CONFIG_CMD_WOL
1384 case PROT_WOL:
1385 wol_receive(ip, len);
1386 break;
1387#endif
Samuel Mendoza-Jonas09bd3d02022-08-08 21:46:03 +09301388#ifdef CONFIG_PHY_NCSI
1389 case PROT_NCSI:
1390 ncsi_receive(et, ip, len);
1391 break;
1392#endif
wdenk2d966952002-10-31 22:12:35 +00001393 }
1394}
1395
wdenk2d966952002-10-31 22:12:35 +00001396/**********************************************************************/
1397
Simon Glasse4bf0c52011-10-24 18:00:02 +00001398static int net_check_prereq(enum proto_t protocol)
wdenk2d966952002-10-31 22:12:35 +00001399{
1400 switch (protocol) {
wdenk6e592382004-04-18 17:39:38 +00001401 /* Fall through */
Jon Loeliger643d1ab2007-07-09 17:45:14 -05001402#if defined(CONFIG_CMD_PING)
wdenk73a8b272003-06-05 19:27:42 +00001403 case PING:
Joe Hershberger049a95a2015-04-08 01:41:01 -05001404 if (net_ping_ip.s_addr == 0) {
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001405 puts("*** ERROR: ping address not given\n");
Luca Ceresoli92895de2011-05-04 02:40:45 +00001406 return 1;
wdenk6e592382004-04-18 17:39:38 +00001407 }
1408 goto common;
wdenk73a8b272003-06-05 19:27:42 +00001409#endif
Robin Getz1a32bf42009-07-20 14:53:54 -04001410#if defined(CONFIG_CMD_DNS)
1411 case DNS:
Joe Hershberger049a95a2015-04-08 01:41:01 -05001412 if (net_dns_server.s_addr == 0) {
Robin Getz1a32bf42009-07-20 14:53:54 -04001413 puts("*** ERROR: DNS server address not given\n");
1414 return 1;
1415 }
1416 goto common;
1417#endif
Philippe Reynesb43ea1b2020-09-18 14:13:00 +02001418#if defined(CONFIG_PROT_UDP)
1419 case UDP:
1420 if (udp_prereq())
1421 return 1;
1422 goto common;
1423#endif
1424
Jon Loeliger643d1ab2007-07-09 17:45:14 -05001425#if defined(CONFIG_CMD_NFS)
wdenkcbd8a352004-02-24 02:00:03 +00001426 case NFS:
1427#endif
Joe Hershbergerbc0571f2015-04-08 01:41:21 -05001428 /* Fall through */
Simon Glasse4bf0c52011-10-24 18:00:02 +00001429 case TFTPGET:
Simon Glass1fb7cd42011-10-24 18:00:07 +00001430 case TFTPPUT:
Joe Hershberger3a66fcb2018-07-03 19:36:39 -05001431 if (net_server_ip.s_addr == 0 && !is_serverip_in_cmd()) {
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001432 puts("*** ERROR: `serverip' not set\n");
Luca Ceresoli92895de2011-05-04 02:40:45 +00001433 return 1;
wdenk6e592382004-04-18 17:39:38 +00001434 }
Philippe Reynes912ece42020-09-18 14:13:02 +02001435#if defined(CONFIG_CMD_PING) || \
Philippe Reynesb43ea1b2020-09-18 14:13:00 +02001436 defined(CONFIG_CMD_DNS) || defined(CONFIG_PROT_UDP)
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001437common:
wdenk73a8b272003-06-05 19:27:42 +00001438#endif
Simon Guinot8b6bbe12011-05-01 23:38:40 +00001439 /* Fall through */
wdenk73a8b272003-06-05 19:27:42 +00001440
Simon Guinot8b6bbe12011-05-01 23:38:40 +00001441 case NETCONS:
Alex Kiernanf73a7df2018-05-29 15:30:53 +00001442 case FASTBOOT:
Luca Ceresoli7a83af02011-05-17 00:03:40 +00001443 case TFTPSRV:
Joe Hershberger049a95a2015-04-08 01:41:01 -05001444 if (net_ip.s_addr == 0) {
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001445 puts("*** ERROR: `ipaddr' not set\n");
Luca Ceresoli92895de2011-05-04 02:40:45 +00001446 return 1;
wdenk6e592382004-04-18 17:39:38 +00001447 }
1448 /* Fall through */
wdenk2d966952002-10-31 22:12:35 +00001449
Peter Tyserbf6cb242010-09-30 11:25:48 -05001450#ifdef CONFIG_CMD_RARP
wdenk2d966952002-10-31 22:12:35 +00001451 case RARP:
Peter Tyserbf6cb242010-09-30 11:25:48 -05001452#endif
Samuel Mendoza-Jonas09bd3d02022-08-08 21:46:03 +09301453#ifdef CONFIG_PHY_NCSI
1454 case NCSI:
1455#endif
wdenk2d966952002-10-31 22:12:35 +00001456 case BOOTP:
wdenka3d991b2004-04-15 21:48:45 +00001457 case CDP:
Peter Tyserbf6cb242010-09-30 11:25:48 -05001458 case DHCP:
Joe Hershbergerd22c3382012-05-23 08:00:12 +00001459 case LINKLOCAL:
Joe Hershberger0adb5b72015-04-08 01:41:04 -05001460 if (memcmp(net_ethaddr, "\0\0\0\0\0\0", 6) == 0) {
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001461 int num = eth_get_dev_index();
wdenk2d966952002-10-31 22:12:35 +00001462
wdenk6e592382004-04-18 17:39:38 +00001463 switch (num) {
1464 case -1:
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001465 puts("*** ERROR: No ethernet found.\n");
Luca Ceresoli92895de2011-05-04 02:40:45 +00001466 return 1;
wdenk6e592382004-04-18 17:39:38 +00001467 case 0:
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001468 puts("*** ERROR: `ethaddr' not set\n");
wdenk2d966952002-10-31 22:12:35 +00001469 break;
wdenk6e592382004-04-18 17:39:38 +00001470 default:
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001471 printf("*** ERROR: `eth%daddr' not set\n",
Joe Hershbergerbc0571f2015-04-08 01:41:21 -05001472 num);
wdenk2d966952002-10-31 22:12:35 +00001473 break;
wdenk2d966952002-10-31 22:12:35 +00001474 }
wdenk6e592382004-04-18 17:39:38 +00001475
Joe Hershbergerbc0571f2015-04-08 01:41:21 -05001476 net_start_again();
Luca Ceresoli92895de2011-05-04 02:40:45 +00001477 return 2;
wdenk6e592382004-04-18 17:39:38 +00001478 }
1479 /* Fall through */
1480 default:
Luca Ceresoli92895de2011-05-04 02:40:45 +00001481 return 0;
wdenk2d966952002-10-31 22:12:35 +00001482 }
Luca Ceresoli92895de2011-05-04 02:40:45 +00001483 return 0; /* OK */
wdenk2d966952002-10-31 22:12:35 +00001484}
1485/**********************************************************************/
1486
1487int
Joe Hershberger1203fcc2015-04-08 01:41:05 -05001488net_eth_hdr_size(void)
wdenka3d991b2004-04-15 21:48:45 +00001489{
1490 ushort myvlanid;
wdenk2d966952002-10-31 22:12:35 +00001491
Joe Hershberger4fd50552015-04-08 01:41:17 -05001492 myvlanid = ntohs(net_our_vlan);
wdenka3d991b2004-04-15 21:48:45 +00001493 if (myvlanid == (ushort)-1)
1494 myvlanid = VLAN_NONE;
1495
Luca Ceresoli3e38e422011-05-11 03:59:54 +00001496 return ((myvlanid & VLAN_IDMASK) == VLAN_NONE) ? ETHER_HDR_SIZE :
1497 VLAN_ETHER_HDR_SIZE;
wdenka3d991b2004-04-15 21:48:45 +00001498}
1499
Joe Hershberger1203fcc2015-04-08 01:41:05 -05001500int net_set_ether(uchar *xet, const uchar *dest_ethaddr, uint prot)
wdenk2d966952002-10-31 22:12:35 +00001501{
Joe Hershbergercb487f52012-05-23 07:58:06 +00001502 struct ethernet_hdr *et = (struct ethernet_hdr *)xet;
wdenka3d991b2004-04-15 21:48:45 +00001503 ushort myvlanid;
1504
Joe Hershberger4fd50552015-04-08 01:41:17 -05001505 myvlanid = ntohs(net_our_vlan);
wdenka3d991b2004-04-15 21:48:45 +00001506 if (myvlanid == (ushort)-1)
1507 myvlanid = VLAN_NONE;
wdenk2d966952002-10-31 22:12:35 +00001508
Joe Hershberger0adb5b72015-04-08 01:41:04 -05001509 memcpy(et->et_dest, dest_ethaddr, 6);
1510 memcpy(et->et_src, net_ethaddr, 6);
wdenka3d991b2004-04-15 21:48:45 +00001511 if ((myvlanid & VLAN_IDMASK) == VLAN_NONE) {
Luca Ceresolic819abe2011-05-04 02:40:46 +00001512 et->et_protlen = htons(prot);
wdenka3d991b2004-04-15 21:48:45 +00001513 return ETHER_HDR_SIZE;
1514 } else {
Joe Hershbergerc68cca32012-05-23 07:58:07 +00001515 struct vlan_ethernet_hdr *vet =
1516 (struct vlan_ethernet_hdr *)xet;
wdenk2d966952002-10-31 22:12:35 +00001517
wdenka3d991b2004-04-15 21:48:45 +00001518 vet->vet_vlan_type = htons(PROT_VLAN);
1519 vet->vet_tag = htons((0 << 5) | (myvlanid & VLAN_IDMASK));
1520 vet->vet_type = htons(prot);
1521 return VLAN_ETHER_HDR_SIZE;
1522 }
1523}
wdenk2d966952002-10-31 22:12:35 +00001524
Joe Hershbergere7111012012-05-23 07:59:16 +00001525int net_update_ether(struct ethernet_hdr *et, uchar *addr, uint prot)
1526{
1527 ushort protlen;
1528
1529 memcpy(et->et_dest, addr, 6);
Joe Hershberger0adb5b72015-04-08 01:41:04 -05001530 memcpy(et->et_src, net_ethaddr, 6);
Joe Hershbergere7111012012-05-23 07:59:16 +00001531 protlen = ntohs(et->et_protlen);
1532 if (protlen == PROT_VLAN) {
1533 struct vlan_ethernet_hdr *vet =
1534 (struct vlan_ethernet_hdr *)et;
1535 vet->vet_type = htons(prot);
1536 return VLAN_ETHER_HDR_SIZE;
1537 } else if (protlen > 1514) {
1538 et->et_protlen = htons(prot);
1539 return ETHER_HDR_SIZE;
1540 } else {
1541 /* 802.2 + SNAP */
1542 struct e802_hdr *et802 = (struct e802_hdr *)et;
1543 et802->et_prot = htons(prot);
1544 return E802_HDR_SIZE;
1545 }
1546}
1547
Duncan Hare5d457ec2018-06-24 15:40:41 -07001548void net_set_ip_header(uchar *pkt, struct in_addr dest, struct in_addr source,
1549 u16 pkt_len, u8 proto)
wdenk2d966952002-10-31 22:12:35 +00001550{
Joe Hershberger4b11c912012-05-23 07:59:07 +00001551 struct ip_udp_hdr *ip = (struct ip_udp_hdr *)pkt;
1552
1553 /*
1554 * Construct an IP header.
1555 */
1556 /* IP_HDR_SIZE / 4 (not including UDP) */
1557 ip->ip_hl_v = 0x45;
1558 ip->ip_tos = 0;
Duncan Hare5d457ec2018-06-24 15:40:41 -07001559 ip->ip_len = htons(pkt_len);
1560 ip->ip_p = proto;
Joe Hershbergerbc0571f2015-04-08 01:41:21 -05001561 ip->ip_id = htons(net_ip_id++);
Joe Hershberger4b11c912012-05-23 07:59:07 +00001562 ip->ip_off = htons(IP_FLAGS_DFRAG); /* Don't fragment */
1563 ip->ip_ttl = 255;
1564 ip->ip_sum = 0;
1565 /* already in network byte order */
Joe Hershberger049a95a2015-04-08 01:41:01 -05001566 net_copy_ip((void *)&ip->ip_src, &source);
Joe Hershberger4b11c912012-05-23 07:59:07 +00001567 /* already in network byte order */
Joe Hershberger049a95a2015-04-08 01:41:01 -05001568 net_copy_ip((void *)&ip->ip_dst, &dest);
Duncan Hare5d457ec2018-06-24 15:40:41 -07001569
1570 ip->ip_sum = compute_ip_checksum(ip, IP_HDR_SIZE);
Joe Hershberger4b11c912012-05-23 07:59:07 +00001571}
1572
Joe Hershberger049a95a2015-04-08 01:41:01 -05001573void net_set_udp_header(uchar *pkt, struct in_addr dest, int dport, int sport,
Joe Hershberger4b11c912012-05-23 07:59:07 +00001574 int len)
1575{
1576 struct ip_udp_hdr *ip = (struct ip_udp_hdr *)pkt;
wdenk2d966952002-10-31 22:12:35 +00001577
1578 /*
1579 * If the data is an odd number of bytes, zero the
1580 * byte after the last byte so that the checksum
1581 * will work.
1582 */
1583 if (len & 1)
Joe Hershberger4b11c912012-05-23 07:59:07 +00001584 pkt[IP_UDP_HDR_SIZE + len] = 0;
wdenk2d966952002-10-31 22:12:35 +00001585
Duncan Hare5d457ec2018-06-24 15:40:41 -07001586 net_set_ip_header(pkt, dest, net_ip, IP_UDP_HDR_SIZE + len,
1587 IPPROTO_UDP);
Joe Hershberger4b11c912012-05-23 07:59:07 +00001588
wdenk2d966952002-10-31 22:12:35 +00001589 ip->udp_src = htons(sport);
1590 ip->udp_dst = htons(dport);
Joe Hershberger594c26f2012-05-23 07:58:04 +00001591 ip->udp_len = htons(UDP_HDR_SIZE + len);
wdenk2d966952002-10-31 22:12:35 +00001592 ip->udp_xsum = 0;
wdenk2d966952002-10-31 22:12:35 +00001593}
1594
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001595void copy_filename(char *dst, const char *src, int size)
wdenk2d966952002-10-31 22:12:35 +00001596{
Joe Hershberger16cf1452018-07-03 19:36:41 -05001597 if (src && *src && (*src == '"')) {
wdenk2d966952002-10-31 22:12:35 +00001598 ++src;
1599 --size;
1600 }
1601
Joe Hershberger16cf1452018-07-03 19:36:41 -05001602 while ((--size > 0) && src && *src && (*src != '"'))
wdenk2d966952002-10-31 22:12:35 +00001603 *dst++ = *src++;
wdenk2d966952002-10-31 22:12:35 +00001604 *dst = '\0';
1605}
1606
Joe Hershberger3a66fcb2018-07-03 19:36:39 -05001607int is_serverip_in_cmd(void)
1608{
1609 return !!strchr(net_boot_file_name, ':');
1610}
1611
Joe Hershberger6ab12832018-07-03 19:36:43 -05001612int net_parse_bootfile(struct in_addr *ipaddr, char *filename, int max_len)
1613{
1614 char *colon;
Lyle Franklin85f8e032022-04-16 11:36:43 -04001615 struct in_addr ip;
1616 ip.s_addr = 0;
Joe Hershberger6ab12832018-07-03 19:36:43 -05001617
1618 if (net_boot_file_name[0] == '\0')
1619 return 0;
1620
1621 colon = strchr(net_boot_file_name, ':');
1622 if (colon) {
Lyle Franklin85f8e032022-04-16 11:36:43 -04001623 ip = string_to_ip(net_boot_file_name);
1624 if (ipaddr && ip.s_addr)
1625 *ipaddr = ip;
1626 }
1627 if (ip.s_addr) {
Joe Hershberger6ab12832018-07-03 19:36:43 -05001628 strncpy(filename, colon + 1, max_len);
1629 } else {
1630 strncpy(filename, net_boot_file_name, max_len);
1631 }
1632 filename[max_len - 1] = '\0';
1633
1634 return 1;
1635}
1636
Joe Hershberger049a95a2015-04-08 01:41:01 -05001637void ip_to_string(struct in_addr x, char *s)
wdenk2d966952002-10-31 22:12:35 +00001638{
Joe Hershberger049a95a2015-04-08 01:41:01 -05001639 x.s_addr = ntohl(x.s_addr);
Luca Ceresoli4f63acd2011-05-11 03:59:56 +00001640 sprintf(s, "%d.%d.%d.%d",
Joe Hershberger049a95a2015-04-08 01:41:01 -05001641 (int) ((x.s_addr >> 24) & 0xff),
1642 (int) ((x.s_addr >> 16) & 0xff),
1643 (int) ((x.s_addr >> 8) & 0xff),
1644 (int) ((x.s_addr >> 0) & 0xff)
wdenk6e592382004-04-18 17:39:38 +00001645 );
wdenk2d966952002-10-31 22:12:35 +00001646}
1647
Joe Hershberger4fd50552015-04-08 01:41:17 -05001648void vlan_to_string(ushort x, char *s)
wdenka3d991b2004-04-15 21:48:45 +00001649{
1650 x = ntohs(x);
1651
1652 if (x == (ushort)-1)
1653 x = VLAN_NONE;
1654
1655 if (x == VLAN_NONE)
1656 strcpy(s, "none");
1657 else
1658 sprintf(s, "%d", x & VLAN_IDMASK);
1659}
1660
Joe Hershberger4fd50552015-04-08 01:41:17 -05001661ushort string_to_vlan(const char *s)
wdenka3d991b2004-04-15 21:48:45 +00001662{
1663 ushort id;
1664
1665 if (s == NULL)
wdenkb9711de2004-04-25 13:18:40 +00001666 return htons(VLAN_NONE);
wdenka3d991b2004-04-15 21:48:45 +00001667
1668 if (*s < '0' || *s > '9')
1669 id = VLAN_NONE;
1670 else
Simon Glass0b1284e2021-07-24 09:03:30 -06001671 id = (ushort)dectoul(s, NULL);
wdenka3d991b2004-04-15 21:48:45 +00001672
wdenkb9711de2004-04-25 13:18:40 +00001673 return htons(id);
wdenka3d991b2004-04-15 21:48:45 +00001674}
1675
Simon Glass723806c2017-08-03 12:22:15 -06001676ushort env_get_vlan(char *var)
wdenka3d991b2004-04-15 21:48:45 +00001677{
Simon Glass00caae62017-08-03 12:22:12 -06001678 return string_to_vlan(env_get(var));
wdenka3d991b2004-04-15 21:48:45 +00001679}