blob: a7dbcec1506b701b2873c14f6bd060e765c1979e [file] [log] [blame]
Jerome Forissier64abd782024-10-16 12:04:00 +02001/* SPDX-License-Identifier: GPL-2.0 */
2/*
3 * LiMon Monitor (LiMon) - Network.
4 *
5 * Copyright 1994 - 2000 Neil Russell.
6 * (See License)
7 *
8 * History
9 * 9/16/00 bor adapted to TQM823L/STK8xxL board, RARP/TFTP boot added
10 */
11
12#ifndef __NET_LEGACY_H__
13#define __NET_LEGACY_H__
14
15#include <linux/types.h>
16#include <asm/byteorder.h> /* for nton* / ntoh* stuff */
17#include <log.h>
18#include <time.h>
19#include <linux/if_ether.h>
Simon Glassbda49012025-05-15 17:31:34 -060020#include <linux/string.h>
Jerome Forissier64abd782024-10-16 12:04:00 +020021
22struct bd_info;
23struct cmd_tbl;
24struct udevice;
25
26#define DEBUG_LL_STATE 0 /* Link local state machine changes */
27#define DEBUG_DEV_PKT 0 /* Packets or info directed to the device */
28#define DEBUG_NET_PKT 0 /* Packets on info on the network at large */
29#define DEBUG_INT_STATE 0 /* Internal network state changes */
30
31/* ARP hardware address length */
32#define ARP_HLEN 6
33/*
34 * The size of a MAC address in string form, each digit requires two chars
35 * and five separator characters to form '00:00:00:00:00:00'.
36 */
37#define ARP_HLEN_ASCII (ARP_HLEN * 2) + (ARP_HLEN - 1)
38
39/**
40 * An incoming packet handler.
41 * @param pkt pointer to the application packet
42 * @param dport destination UDP port
43 * @param sip source IP address
44 * @param sport source UDP port
45 * @param len packet length
46 */
Jerome Forissier97083502024-11-07 12:27:57 +010047typedef void rxhand_f(uchar *pkt, unsigned int dport,
48 struct in_addr sip, unsigned int sport,
49 unsigned int len);
Jerome Forissier64abd782024-10-16 12:04:00 +020050
51/**
52 * An incoming ICMP packet handler.
53 * @param type ICMP type
54 * @param code ICMP code
55 * @param dport destination UDP port
56 * @param sip source IP address
57 * @param sport source UDP port
58 * @param pkt pointer to the ICMP packet data
59 * @param len packet length
60 */
Jerome Forissier97083502024-11-07 12:27:57 +010061typedef void rxhand_icmp_f(unsigned type, unsigned int code, unsigned int dport,
62 struct in_addr sip, unsigned int sport, uchar *pkt,
63 unsigned int len);
Jerome Forissier64abd782024-10-16 12:04:00 +020064
65/*
66 * A timeout handler. Called after time interval has expired.
67 */
68typedef void thand_f(void);
69
70/*
71 * The devname can be either an exact name given by the driver or device tree
72 * or it can be an alias of the form "eth%d"
73 */
74struct udevice *eth_get_dev_by_name(const char *devname);
75int eth_init_state_only(void); /* Set active state */
76void eth_halt_state_only(void); /* Set passive state */
77
78/**
79 * eth_env_set_enetaddr_by_index() - set the MAC address environment variable
80 *
81 * This sets up an environment variable with the given MAC address (@enetaddr).
82 * The environment variable to be set is defined by <@base_name><@index>addr.
83 * If @index is 0 it is omitted. For common Ethernet this means ethaddr,
84 * eth1addr, etc.
85 *
86 * @base_name: Base name for variable, typically "eth"
87 * @index: Index of interface being updated (>=0)
88 * @enetaddr: Pointer to MAC address to put into the variable
89 * Return: 0 if OK, other value on error
90 */
91int eth_env_set_enetaddr_by_index(const char *base_name, int index,
Jerome Forissier97083502024-11-07 12:27:57 +010092 uchar *enetaddr);
Jerome Forissier64abd782024-10-16 12:04:00 +020093
94/*
95 * Get the hardware address for an ethernet interface .
96 * Args:
97 * base_name - base name for device (normally "eth")
98 * index - device index number (0 for first)
99 * enetaddr - returns 6 byte hardware address
100 * Returns:
101 * Return true if the address is valid.
102 */
103int eth_env_get_enetaddr_by_index(const char *base_name, int index,
Jerome Forissier97083502024-11-07 12:27:57 +0100104 uchar *enetaddr);
Jerome Forissier64abd782024-10-16 12:04:00 +0200105
106int eth_send(void *packet, int length); /* Send a packet */
107
108#if defined(CONFIG_API) || defined(CONFIG_EFI_LOADER)
109int eth_receive(void *packet, int length); /* Receive a packet*/
110extern void (*push_packet)(void *packet, int length);
111#endif
112int eth_mcast_join(struct in_addr mcast_addr, int join);
113
114/**********************************************************************/
115/*
116 * Protocol headers.
117 */
118
119#define ETH_FCS_LEN 4 /* Octets in the FCS */
120
121struct e802_hdr {
122 u8 et_dest[ARP_HLEN]; /* Destination node */
123 u8 et_src[ARP_HLEN]; /* Source node */
124 u16 et_protlen; /* Protocol or length */
125 u8 et_dsap; /* 802 DSAP */
126 u8 et_ssap; /* 802 SSAP */
127 u8 et_ctl; /* 802 control */
128 u8 et_snap1; /* SNAP */
129 u8 et_snap2;
130 u8 et_snap3;
131 u16 et_prot; /* 802 protocol */
Jerome Forissier97083502024-11-07 12:27:57 +0100132} __packed;
Jerome Forissier64abd782024-10-16 12:04:00 +0200133
134/* 802 + SNAP + ethernet header size */
135#define E802_HDR_SIZE (sizeof(struct e802_hdr))
136
137/*
138 * Virtual LAN Ethernet header
139 */
140struct vlan_ethernet_hdr {
141 u8 vet_dest[ARP_HLEN]; /* Destination node */
142 u8 vet_src[ARP_HLEN]; /* Source node */
143 u16 vet_vlan_type; /* PROT_VLAN */
144 u16 vet_tag; /* TAG of VLAN */
145 u16 vet_type; /* protocol type */
Jerome Forissier97083502024-11-07 12:27:57 +0100146} __packed;
Jerome Forissier64abd782024-10-16 12:04:00 +0200147
148/* VLAN Ethernet header size */
149#define VLAN_ETHER_HDR_SIZE (sizeof(struct vlan_ethernet_hdr))
150
151/*
152 * Internet Protocol (IP) header.
153 */
154struct ip_hdr {
155 u8 ip_hl_v; /* header length and version */
156 u8 ip_tos; /* type of service */
157 u16 ip_len; /* total length */
158 u16 ip_id; /* identification */
159 u16 ip_off; /* fragment offset field */
160 u8 ip_ttl; /* time to live */
161 u8 ip_p; /* protocol */
162 u16 ip_sum; /* checksum */
163 struct in_addr ip_src; /* Source IP address */
164 struct in_addr ip_dst; /* Destination IP address */
Jerome Forissier97083502024-11-07 12:27:57 +0100165} __packed;
Jerome Forissier64abd782024-10-16 12:04:00 +0200166
167#define IP_OFFS 0x1fff /* ip offset *= 8 */
168#define IP_FLAGS 0xe000 /* first 3 bits */
169#define IP_FLAGS_RES 0x8000 /* reserved */
170#define IP_FLAGS_DFRAG 0x4000 /* don't fragments */
171#define IP_FLAGS_MFRAG 0x2000 /* more fragments */
172
173#define IP_HDR_SIZE (sizeof(struct ip_hdr))
174
175#define IP_MIN_FRAG_DATAGRAM_SIZE (IP_HDR_SIZE + 8)
176
177/*
178 * Address Resolution Protocol (ARP) header.
179 */
180struct arp_hdr {
181 u16 ar_hrd; /* Format of hardware address */
182# define ARP_ETHER 1 /* Ethernet hardware address */
183 u16 ar_pro; /* Format of protocol address */
184 u8 ar_hln; /* Length of hardware address */
185 u8 ar_pln; /* Length of protocol address */
186# define ARP_PLEN 4
187 u16 ar_op; /* Operation */
188# define ARPOP_REQUEST 1 /* Request to resolve address */
189# define ARPOP_REPLY 2 /* Response to previous request */
190
191# define RARPOP_REQUEST 3 /* Request to resolve address */
192# define RARPOP_REPLY 4 /* Response to previous request */
193
194 /*
195 * The remaining fields are variable in size, according to
196 * the sizes above, and are defined as appropriate for
197 * specific hardware/protocol combinations.
198 */
199 u8 ar_data[0];
200#define ar_sha ar_data[0]
201#define ar_spa ar_data[ARP_HLEN]
202#define ar_tha ar_data[ARP_HLEN + ARP_PLEN]
203#define ar_tpa ar_data[ARP_HLEN + ARP_PLEN + ARP_HLEN]
204#if 0
205 u8 ar_sha[]; /* Sender hardware address */
206 u8 ar_spa[]; /* Sender protocol address */
207 u8 ar_tha[]; /* Target hardware address */
208 u8 ar_tpa[]; /* Target protocol address */
209#endif /* 0 */
Jerome Forissier97083502024-11-07 12:27:57 +0100210} __packed;
Jerome Forissier64abd782024-10-16 12:04:00 +0200211
212/*
213 * ICMP stuff (just enough to handle (host) redirect messages)
214 */
215#define ICMP_ECHO_REPLY 0 /* Echo reply */
216#define ICMP_NOT_REACH 3 /* Detination unreachable */
217#define ICMP_REDIRECT 5 /* Redirect (change route) */
218#define ICMP_ECHO_REQUEST 8 /* Echo request */
219
220/* Codes for REDIRECT. */
221#define ICMP_REDIR_NET 0 /* Redirect Net */
222#define ICMP_REDIR_HOST 1 /* Redirect Host */
223
224/* Codes for NOT_REACH */
225#define ICMP_NOT_REACH_PORT 3 /* Port unreachable */
226
227struct icmp_hdr {
228 u8 type;
229 u8 code;
230 u16 checksum;
231 union {
232 struct {
233 u16 id;
234 u16 sequence;
235 } echo;
236 u32 gateway;
237 struct {
238 u16 unused;
239 u16 mtu;
240 } frag;
241 u8 data[0];
242 } un;
Jerome Forissier97083502024-11-07 12:27:57 +0100243} __packed;
Jerome Forissier64abd782024-10-16 12:04:00 +0200244
245#define ICMP_HDR_SIZE (sizeof(struct icmp_hdr))
246#define IP_ICMP_HDR_SIZE (IP_HDR_SIZE + ICMP_HDR_SIZE)
247
248/*
249 * Maximum packet size; used to allocate packet storage. Use
Jerome Forissier97083502024-11-07 12:27:57 +0100250 * the maximum Ethernet frame size as specified by the Ethernet
Jerome Forissier64abd782024-10-16 12:04:00 +0200251 * standard including the 802.1Q tag (VLAN tagging).
252 * maximum packet size = 1522
253 * maximum packet size and multiple of 32 bytes = 1536
254 */
255#define PKTSIZE 1522
256#ifndef CONFIG_DM_DSA
257#define PKTSIZE_ALIGN 1536
258#else
259/* Maximum DSA tagging overhead (headroom and/or tailroom) */
260#define DSA_MAX_OVR 256
261#define PKTSIZE_ALIGN (1536 + DSA_MAX_OVR)
262#endif
263
264/**********************************************************************/
265/*
266 * Globals.
267 *
268 * Note:
269 *
270 * All variables of type struct in_addr are stored in NETWORK byte order
271 * (big endian).
272 */
273
274/* net.c */
275/** BOOTP EXTENTIONS **/
276extern struct in_addr net_gateway; /* Our gateway IP address */
277extern struct in_addr net_netmask; /* Our subnet mask (0 = unknown) */
278/* Our Domain Name Server (0 = unknown) */
279extern struct in_addr net_dns_server;
280#if defined(CONFIG_BOOTP_DNS2)
281/* Our 2nd Domain Name Server (0 = unknown) */
282extern struct in_addr net_dns_server2;
283#endif
284extern char net_nis_domain[32]; /* Our IS domain */
285extern char net_hostname[32]; /* Our hostname */
286#ifdef CONFIG_NET
287extern char net_root_path[CONFIG_BOOTP_MAX_ROOT_PATH_LEN]; /* Our root path */
288#endif
Jerome Forissier64abd782024-10-16 12:04:00 +0200289/** END OF BOOTP EXTENTIONS **/
290extern u8 net_ethaddr[ARP_HLEN]; /* Our ethernet address */
291extern u8 net_server_ethaddr[ARP_HLEN]; /* Boot server enet address */
Jerome Forissier64abd782024-10-16 12:04:00 +0200292extern struct in_addr net_server_ip; /* Server IP addr (0 = unknown) */
293extern uchar *net_tx_packet; /* THE transmit packet */
294extern uchar *net_rx_packets[PKTBUFSRX]; /* Receive packets */
295extern uchar *net_rx_packet; /* Current receive packet */
296extern int net_rx_packet_len; /* Current rx packet length */
297extern const u8 net_null_ethaddr[ARP_HLEN];
298
299#define VLAN_NONE 4095 /* untagged */
300#define VLAN_IDMASK 0x0fff /* mask of valid vlan id */
301extern ushort net_our_vlan; /* Our VLAN */
302extern ushort net_native_vlan; /* Our Native VLAN */
303
304extern int net_restart_wrap; /* Tried all network devices */
305
306enum proto_t {
307 BOOTP, RARP, ARP, TFTPGET, DHCP, DHCP6, PING, PING6, DNS, NFS, CDP,
308 NETCONS, SNTP, TFTPSRV, TFTPPUT, LINKLOCAL, FASTBOOT_UDP, FASTBOOT_TCP,
309 WOL, UDP, NCSI, WGET, RS
310};
Jerome Forissier97083502024-11-07 12:27:57 +0100311
Jerome Forissier64abd782024-10-16 12:04:00 +0200312/* Indicates whether the file name was specified on the command line */
313extern bool net_boot_file_name_explicit;
314/* The actual transferred size of the bootfile (in bytes) */
315extern u32 net_boot_file_size;
316/* Boot file size in blocks as reported by the DHCP server */
317extern u32 net_boot_file_expected_size_in_blocks;
318
319#if defined(CONFIG_CMD_DNS)
320extern char *net_dns_resolve; /* The host to resolve */
321extern char *net_dns_env_var; /* the env var to put the ip into */
322#endif
323
324#if defined(CONFIG_CMD_PING)
325extern struct in_addr net_ping_ip; /* the ip address to ping */
326#endif
327
328#if defined(CONFIG_CMD_CDP)
329/* when CDP completes these hold the return values */
330extern ushort cdp_native_vlan; /* CDP returned native VLAN */
331extern ushort cdp_appliance_vlan; /* CDP returned appliance VLAN */
332
333/*
334 * Check for a CDP packet by examining the received MAC address field
335 */
336static inline int is_cdp_packet(const uchar *ethaddr)
337{
338 extern const u8 net_cdp_ethaddr[ARP_HLEN];
339
340 return memcmp(ethaddr, net_cdp_ethaddr, ARP_HLEN) == 0;
341}
342#endif
343
344#if defined(CONFIG_CMD_SNTP)
345extern struct in_addr net_ntp_server; /* the ip address to NTP */
346extern int net_ntp_time_offset; /* offset time from UTC */
347#endif
348
349int net_loop(enum proto_t);
350
Jerome Forissier64abd782024-10-16 12:04:00 +0200351/* Get size of the ethernet header when we send */
352int net_eth_hdr_size(void);
353
354/* Set ethernet header; returns the size of the header */
355int net_set_ether(uchar *xet, const uchar *dest_ethaddr, uint prot);
356int net_update_ether(struct ethernet_hdr *et, uchar *addr, uint prot);
357
358/* Set IP header */
359void net_set_ip_header(uchar *pkt, struct in_addr dest, struct in_addr source,
360 u16 pkt_len, u8 proto);
361void net_set_udp_header(uchar *pkt, struct in_addr dest, int dport,
Jerome Forissier97083502024-11-07 12:27:57 +0100362 int sport, int len);
Jerome Forissier64abd782024-10-16 12:04:00 +0200363
364/* Callbacks */
365rxhand_f *net_get_udp_handler(void); /* Get UDP RX packet handler */
Jerome Forissier97083502024-11-07 12:27:57 +0100366void net_set_udp_handler(rxhand_f *f); /* Set UDP RX packet handler */
Jerome Forissier64abd782024-10-16 12:04:00 +0200367rxhand_f *net_get_arp_handler(void); /* Get ARP RX packet handler */
Jerome Forissier97083502024-11-07 12:27:57 +0100368void net_set_arp_handler(rxhand_f *f); /* Set ARP RX packet handler */
Jerome Forissier64abd782024-10-16 12:04:00 +0200369bool arp_is_waiting(void); /* Waiting for ARP reply? */
370void net_set_icmp_handler(rxhand_icmp_f *f); /* Set ICMP RX handler */
Jerome Forissier97083502024-11-07 12:27:57 +0100371void net_set_timeout_handler(ulong t, thand_f *f);/* Set timeout handler */
Jerome Forissier64abd782024-10-16 12:04:00 +0200372
373/* Network loop state */
374enum net_loop_state {
375 NETLOOP_CONTINUE,
376 NETLOOP_RESTART,
377 NETLOOP_SUCCESS,
378 NETLOOP_FAIL
379};
Jerome Forissier97083502024-11-07 12:27:57 +0100380
Jerome Forissier64abd782024-10-16 12:04:00 +0200381extern enum net_loop_state net_state;
382
383static inline void net_set_state(enum net_loop_state state)
384{
385 debug_cond(DEBUG_INT_STATE, "--- NetState set to %d\n", state);
386 net_state = state;
387}
388
389/*
390 * net_get_async_tx_pkt_buf - Get a packet buffer that is not in use for
391 * sending an asynchronous reply
392 *
393 * returns - ptr to packet buffer
394 */
395uchar * net_get_async_tx_pkt_buf(void);
396
397/**
398 * net_send_ip_packet() - Transmit "net_tx_packet" as UDP or TCP packet,
399 * send ARP request if needed (ether will be populated)
400 * @ether: Raw packet buffer
401 * @dest: IP address to send the datagram to
402 * @dport: Destination UDP port
403 * @sport: Source UDP port
404 * @payload_len: Length of data after the UDP header
405 * @action: TCP action to be performed
406 * @tcp_seq_num: TCP sequence number of this transmission
407 * @tcp_ack_num: TCP stream acknolegement number
408 *
409 * Return: 0 on success, other value on failure
410 */
411int net_send_ip_packet(uchar *ether, struct in_addr dest, int dport, int sport,
412 int payload_len, int proto, u8 action, u32 tcp_seq_num,
413 u32 tcp_ack_num);
414/**
415 * net_send_tcp_packet() - Transmit TCP packet.
416 * @payload_len: length of payload
Mikhail Kshevetskiy215f73f2024-12-28 13:46:30 +0300417 * @dhost: Destination host
Jerome Forissier64abd782024-10-16 12:04:00 +0200418 * @dport: Destination TCP port
419 * @sport: Source TCP port
420 * @action: TCP action to be performed
421 * @tcp_seq_num: TCP sequence number of this transmission
422 * @tcp_ack_num: TCP stream acknolegement number
423 *
424 * Return: 0 on success, other value on failure
425 */
Mikhail Kshevetskiy215f73f2024-12-28 13:46:30 +0300426int net_send_tcp_packet(int payload_len, struct in_addr dhost, int dport,
427 int sport, u8 action, u32 tcp_seq_num, u32 tcp_ack_num);
Jerome Forissier64abd782024-10-16 12:04:00 +0200428int net_send_udp_packet(uchar *ether, struct in_addr dest, int dport,
429 int sport, int payload_len);
430
431#if defined(CONFIG_NETCONSOLE) && !defined(CONFIG_XPL_BUILD)
432void nc_start(void);
Jerome Forissier97083502024-11-07 12:27:57 +0100433int nc_input_packet(uchar *pkt, struct in_addr src_ip, unsigned int dest_port,
434 unsigned int src_port, unsigned int len);
Jerome Forissier64abd782024-10-16 12:04:00 +0200435#endif
436
437static __always_inline int eth_is_on_demand_init(void)
438{
439#if defined(CONFIG_NETCONSOLE) && !defined(CONFIG_XPL_BUILD)
440 extern enum proto_t net_loop_last_protocol;
441
442 return net_loop_last_protocol != NETCONS;
443#else
444 return 1;
445#endif
446}
447
448static inline void eth_set_last_protocol(int protocol)
449{
450#if defined(CONFIG_NETCONSOLE) && !defined(CONFIG_XPL_BUILD)
451 extern enum proto_t net_loop_last_protocol;
452
453 net_loop_last_protocol = protocol;
454#endif
455}
456
457/*
458 * Check if autoload is enabled. If so, use either NFS or TFTP to download
459 * the boot file.
460 */
461void net_auto_load(void);
462
463/*
464 * The following functions are a bit ugly, but necessary to deal with
465 * alignment restrictions on ARM.
466 *
467 * We're using inline functions, which had the smallest memory
468 * footprint in our tests.
469 */
470/* return IP *in network byteorder* */
471static inline struct in_addr net_read_ip(void *from)
472{
473 struct in_addr ip;
474
475 memcpy((void *)&ip, (void *)from, sizeof(ip));
476 return ip;
477}
478
479/* return ulong *in network byteorder* */
480static inline u32 net_read_u32(void *from)
481{
482 u32 l;
483
484 memcpy((void *)&l, (void *)from, sizeof(l));
485 return l;
486}
487
488/* write IP *in network byteorder* */
489static inline void net_write_ip(void *to, struct in_addr ip)
490{
491 memcpy(to, (void *)&ip, sizeof(ip));
492}
493
494/* copy IP */
495static inline void net_copy_ip(void *to, void *from)
496{
497 memcpy((void *)to, from, sizeof(struct in_addr));
498}
499
500/* copy ulong */
501static inline void net_copy_u32(void *to, void *from)
502{
503 memcpy((void *)to, (void *)from, sizeof(u32));
504}
505
506/* Convert an IP address to a string */
507void ip_to_string(struct in_addr x, char *s);
508
509/**
510 * string_to_ip() - Convert a string to ip address
511 *
512 * Implemented in lib/net_utils.c (built unconditionally)
513 *
514 * @s: Input string to parse
515 * @return: in_addr struct containing the parsed IP address
516 */
517struct in_addr string_to_ip(const char *s);
518
519/* Convert a VLAN id to a string */
520void vlan_to_string(ushort x, char *s);
521
522/* Convert a string to a vlan id */
523ushort string_to_vlan(const char *s);
524
525/* read a VLAN id from an environment variable */
Jerome Forissier97083502024-11-07 12:27:57 +0100526ushort env_get_vlan(char *var);
Jerome Forissier64abd782024-10-16 12:04:00 +0200527
528/* check if serverip is specified in filename from the command line */
529int is_serverip_in_cmd(void);
530
531/**
532 * net_parse_bootfile - Parse the bootfile env var / cmd line param
533 *
534 * @param ipaddr - a pointer to the ipaddr to populate if included in bootfile
535 * @param filename - a pointer to the string to save the filename part
536 * @param max_len - The longest - 1 that the filename part can be
537 *
538 * return 1 if parsed, 0 if bootfile is empty
539 */
540int net_parse_bootfile(struct in_addr *ipaddr, char *filename, int max_len);
541
542#endif /* __NET_LEGACY_H__ */