blob: fd7c5e7b4886c537c116ac95bb67718cc0de3d37 [file] [log] [blame]
Jerome Forissier64abd782024-10-16 12:04:00 +02001/* SPDX-License-Identifier: GPL-2.0+ */
2
3#ifndef __NET_COMMON_H__
4#define __NET_COMMON_H__
5
6#include <asm/cache.h>
7#include <command.h>
8#include <env.h>
9#include <hexdump.h>
10#include <linux/if_ether.h>
11#include <linux/types.h>
12#include <rand.h>
13#include <time.h>
14
15#define DEBUG_NET_PKT_TRACE 0 /* Trace all packet data */
16
17/*
18 * The number of receive packet buffers, and the required packet buffer
19 * alignment in memory.
20 *
21 */
22#define PKTBUFSRX CONFIG_SYS_RX_ETH_BUFFER
23#define PKTALIGN ARCH_DMA_MINALIGN
24
25/* IPv4 addresses are always 32 bits in size */
26struct in_addr {
27 __be32 s_addr;
28};
29
30#define PROT_IP 0x0800 /* IP protocol */
31#define PROT_ARP 0x0806 /* IP ARP protocol */
32#define PROT_WOL 0x0842 /* ether-wake WoL protocol */
33#define PROT_RARP 0x8035 /* IP ARP protocol */
34#define PROT_VLAN 0x8100 /* IEEE 802.1q protocol */
35#define PROT_IPV6 0x86dd /* IPv6 over bluebook */
36#define PROT_PPP_SES 0x8864 /* PPPoE session messages */
37#define PROT_NCSI 0x88f8 /* NC-SI control packets */
38
39#define IPPROTO_ICMP 1 /* Internet Control Message Protocol */
40#define IPPROTO_TCP 6 /* Transmission Control Protocol */
41#define IPPROTO_UDP 17 /* User Datagram Protocol */
42
43#define IP_OFFS 0x1fff /* ip offset *= 8 */
44#define IP_FLAGS 0xe000 /* first 3 bits */
45#define IP_FLAGS_RES 0x8000 /* reserved */
46#define IP_FLAGS_DFRAG 0x4000 /* don't fragments */
47#define IP_FLAGS_MFRAG 0x2000 /* more fragments */
48
49#define IP_HDR_SIZE (sizeof(struct ip_hdr))
50
51#define IP_MIN_FRAG_DATAGRAM_SIZE (IP_HDR_SIZE + 8)
52
53/*
54 * Internet Protocol (IP) + UDP header.
55 */
56struct ip_udp_hdr {
57 u8 ip_hl_v; /* header length and version */
58 u8 ip_tos; /* type of service */
59 u16 ip_len; /* total length */
60 u16 ip_id; /* identification */
61 u16 ip_off; /* fragment offset field */
62 u8 ip_ttl; /* time to live */
63 u8 ip_p; /* protocol */
64 u16 ip_sum; /* checksum */
65 struct in_addr ip_src; /* Source IP address */
66 struct in_addr ip_dst; /* Destination IP address */
67 u16 udp_src; /* UDP source port */
68 u16 udp_dst; /* UDP destination port */
69 u16 udp_len; /* Length of UDP packet */
70 u16 udp_xsum; /* Checksum */
71} __attribute__((packed));
72
73#define IP_UDP_HDR_SIZE (sizeof(struct ip_udp_hdr))
74#define UDP_HDR_SIZE (IP_UDP_HDR_SIZE - IP_HDR_SIZE)
75
76/* Number of packets processed together */
77#define ETH_PACKETS_BATCH_RECV 32
78
79/* ARP hardware address length */
80#define ARP_HLEN 6
81/*
82 * The size of a MAC address in string form, each digit requires two chars
83 * and five separator characters to form '00:00:00:00:00:00'.
84 */
85#define ARP_HLEN_ASCII (ARP_HLEN * 2) + (ARP_HLEN - 1)
86
87#define ARP_HDR_SIZE (8+20) /* Size assuming ethernet */
88
89# define ARP_ETHER 1 /* Ethernet hardware address */
90
91/*
92 * Maximum packet size; used to allocate packet storage. Use
93 * the maxium Ethernet frame size as specified by the Ethernet
94 * standard including the 802.1Q tag (VLAN tagging).
95 * maximum packet size = 1522
96 * maximum packet size and multiple of 32 bytes = 1536
97 */
98#define PKTSIZE 1522
99#ifndef CONFIG_DM_DSA
100#define PKTSIZE_ALIGN 1536
101#else
102/* Maximum DSA tagging overhead (headroom and/or tailroom) */
103#define DSA_MAX_OVR 256
104#define PKTSIZE_ALIGN (1536 + DSA_MAX_OVR)
105#endif
106
107/*
108 * Maximum receive ring size; that is, the number of packets
109 * we can buffer before overflow happens. Basically, this just
110 * needs to be enough to prevent a packet being discarded while
111 * we are processing the previous one.
112 * Used only in drivers/net/mvgbe.c.
113 */
114#define RINGSZ 4
115#define RINGSZ_LOG2 2
116
117extern int net_restart_wrap; /* Tried all network devices */
118extern uchar *net_rx_packets[PKTBUFSRX]; /* Receive packets */
119extern const u8 net_bcast_ethaddr[ARP_HLEN]; /* Ethernet broadcast address */
120extern char net_boot_file_name[1024];/* Boot File name */
Jerome Forissier1ff00362024-10-16 12:04:03 +0200121extern struct in_addr net_ip; /* Our IP addr (0 = unknown) */
122/* Indicates whether the pxe path prefix / config file was specified in dhcp option */
123extern char *pxelinux_configfile;
Jerome Forissier64abd782024-10-16 12:04:00 +0200124
125/**
126 * compute_ip_checksum() - Compute IP checksum
127 *
128 * @addr: Address to check (must be 16-bit aligned)
129 * @nbytes: Number of bytes to check (normally a multiple of 2)
130 * Return: 16-bit IP checksum
131 */
132unsigned compute_ip_checksum(const void *addr, unsigned nbytes);
133
134/**
135 * ip_checksum_ok() - check if a checksum is correct
136 *
137 * This works by making sure the checksum sums to 0
138 *
139 * @addr: Address to check (must be 16-bit aligned)
140 * @nbytes: Number of bytes to check (normally a multiple of 2)
141 * Return: true if the checksum matches, false if not
142 */
143int ip_checksum_ok(const void *addr, unsigned nbytes);
144
145/**
146 * add_ip_checksums() - add two IP checksums
147 *
148 * @offset: Offset of first sum (if odd we do a byte-swap)
149 * @sum: First checksum
150 * @new_sum: New checksum to add
151 * Return: updated 16-bit IP checksum
152 */
153unsigned add_ip_checksums(unsigned offset, unsigned sum, unsigned new_sum);
154
155/*
156 * The devname can be either an exact name given by the driver or device tree
157 * or it can be an alias of the form "eth%d"
158 */
159struct udevice *eth_get_dev_by_name(const char *devname);
160int eth_is_active(struct udevice *dev); /* Test device for active state */
161
162/*
163 * Get the hardware address for an ethernet interface .
164 * Args:
165 * base_name - base name for device (normally "eth")
166 * index - device index number (0 for first)
167 * enetaddr - returns 6 byte hardware address
168 * Returns:
169 * Return true if the address is valid.
170 */
171int eth_env_get_enetaddr_by_index(const char *base_name, int index,
172 uchar *enetaddr);
173
174/**
175 * eth_env_set_enetaddr_by_index() - set the MAC address environment variable
176 *
177 * This sets up an environment variable with the given MAC address (@enetaddr).
178 * The environment variable to be set is defined by <@base_name><@index>addr.
179 * If @index is 0 it is omitted. For common Ethernet this means ethaddr,
180 * eth1addr, etc.
181 *
182 * @base_name: Base name for variable, typically "eth"
183 * @index: Index of interface being updated (>=0)
184 * @enetaddr: Pointer to MAC address to put into the variable
185 * Return: 0 if OK, other value on error
186 */
187int eth_env_set_enetaddr_by_index(const char *base_name, int index,
188 uchar *enetaddr);
189
190/*
191 * Initialize USB ethernet device with CONFIG_DM_ETH
192 * Returns:
193 * 0 is success, non-zero is error status.
194 */
195int usb_ether_init(void);
196
197int eth_init(void); /* Initialize the device */
Jerome Forissierc8d070c2024-10-16 12:04:01 +0200198int eth_start_udev(struct udevice *dev); /* ->start() if not already running */
Jerome Forissier64abd782024-10-16 12:04:00 +0200199int eth_send(void *packet, int length); /* Send a packet */
200#if defined(CONFIG_API) || defined(CONFIG_EFI_LOADER)
201int eth_receive(void *packet, int length); /* Receive a packet*/
202extern void (*push_packet)(void *packet, int length);
203#endif
204int eth_rx(void); /* Check for received packets */
205
206/**
207 * reset_phy() - Reset the Ethernet PHY
208 *
209 * This should be implemented by boards if CONFIG_RESET_PHY_R is enabled
210 */
211void reset_phy(void);
212
213#if CONFIG_IS_ENABLED(NET) || CONFIG_IS_ENABLED(NET_LWIP)
214/**
215 * eth_set_enable_bootdevs() - Enable or disable binding of Ethernet bootdevs
216 *
217 * These get in the way of bootstd testing, so are normally disabled by tests.
218 * This provide control of this setting. It only affects binding of Ethernet
219 * devices, so if that has already happened, this flag does nothing.
220 *
221 * @enable: true to enable binding of bootdevs when binding new Ethernet
222 * devices, false to disable it
223 */
224void eth_set_enable_bootdevs(bool enable);
225#else
226static inline void eth_set_enable_bootdevs(bool enable) {}
227#endif
228
229static inline void net_send_packet(uchar *pkt, int len)
230{
231 if (DEBUG_NET_PKT_TRACE)
232 print_hex_dump_bytes("tx: ", DUMP_PREFIX_OFFSET, pkt, len);
233 /* Currently no way to return errors from eth_send() */
234 (void) eth_send(pkt, len);
235}
236
237enum eth_recv_flags {
238 /*
239 * Check hardware device for new packets (otherwise only return those
240 * which are already in the memory buffer ready to process)
241 */
242 ETH_RECV_CHECK_DEVICE = 1 << 0,
243};
244
245/**
246 * struct eth_ops - functions of Ethernet MAC controllers
247 *
248 * start: Prepare the hardware to send and receive packets
249 * send: Send the bytes passed in "packet" as a packet on the wire
250 * recv: Check if the hardware received a packet. If so, set the pointer to the
251 * packet buffer in the packetp parameter. If not, return an error or 0 to
252 * indicate that the hardware receive FIFO is empty. If 0 is returned, the
253 * network stack will not process the empty packet, but free_pkt() will be
254 * called if supplied
255 * free_pkt: Give the driver an opportunity to manage its packet buffer memory
256 * when the network stack is finished processing it. This will only be
257 * called when no error was returned from recv - optional
258 * stop: Stop the hardware from looking for packets - may be called even if
259 * state == PASSIVE
260 * mcast: Join or leave a multicast group (for TFTP) - optional
261 * write_hwaddr: Write a MAC address to the hardware (used to pass it to Linux
262 * on some platforms like ARM). This function expects the
263 * eth_pdata::enetaddr field to be populated. The method can
264 * return -ENOSYS to indicate that this is not implemented for
265 this hardware - optional.
266 * read_rom_hwaddr: Some devices have a backup of the MAC address stored in a
267 * ROM on the board. This is how the driver should expose it
268 * to the network stack. This function should fill in the
269 * eth_pdata::enetaddr field - optional
270 * set_promisc: Enable or Disable promiscuous mode
271 * get_sset_count: Number of statistics counters
272 * get_string: Names of the statistic counters
273 * get_stats: The values of the statistic counters
274 */
275struct eth_ops {
276 int (*start)(struct udevice *dev);
277 int (*send)(struct udevice *dev, void *packet, int length);
278 int (*recv)(struct udevice *dev, int flags, uchar **packetp);
279 int (*free_pkt)(struct udevice *dev, uchar *packet, int length);
280 void (*stop)(struct udevice *dev);
281 int (*mcast)(struct udevice *dev, const u8 *enetaddr, int join);
282 int (*write_hwaddr)(struct udevice *dev);
283 int (*read_rom_hwaddr)(struct udevice *dev);
284 int (*set_promisc)(struct udevice *dev, bool enable);
285 int (*get_sset_count)(struct udevice *dev);
286 void (*get_strings)(struct udevice *dev, u8 *data);
287 void (*get_stats)(struct udevice *dev, u64 *data);
288};
289
290#define eth_get_ops(dev) ((struct eth_ops *)(dev)->driver->ops)
291
292struct udevice *eth_get_dev(void); /* get the current device */
293unsigned char *eth_get_ethaddr(void); /* get the current device MAC */
294int eth_rx(void); /* Check for received packets */
295void eth_halt(void); /* stop SCC */
296const char *eth_get_name(void); /* get name of current device */
297int eth_get_dev_index(void);
298
299int eth_initialize(void); /* Initialize network subsystem */
300void eth_try_another(int first_restart); /* Change the device */
301void eth_set_current(void); /* set nterface to ethcur var */
302
303enum eth_state_t {
304 ETH_STATE_INIT,
305 ETH_STATE_PASSIVE,
306 ETH_STATE_ACTIVE
307};
308
309/**
310 * struct eth_pdata - Platform data for Ethernet MAC controllers
311 *
312 * @iobase: The base address of the hardware registers
313 * @enetaddr: The Ethernet MAC address that is loaded from EEPROM or env
314 * @phy_interface: PHY interface to use - see PHY_INTERFACE_MODE_...
315 * @max_speed: Maximum speed of Ethernet connection supported by MAC
316 * @priv_pdata: device specific plat
317 */
318struct eth_pdata {
319 phys_addr_t iobase;
320 unsigned char enetaddr[ARP_HLEN];
321 int phy_interface;
322 int max_speed;
323 void *priv_pdata;
324};
325
326struct ethernet_hdr {
327 u8 et_dest[ARP_HLEN]; /* Destination node */
328 u8 et_src[ARP_HLEN]; /* Source node */
329 u16 et_protlen; /* Protocol or length */
330} __attribute__((packed));
331
332/* Ethernet header size */
333#define ETHER_HDR_SIZE (sizeof(struct ethernet_hdr))
334
335/**
336 * net_random_ethaddr - Generate software assigned random Ethernet address
337 * @addr: Pointer to a six-byte array containing the Ethernet address
338 *
339 * Generate a random Ethernet address (MAC) that is not multicast
340 * and has the local assigned bit set.
341 */
342static inline void net_random_ethaddr(uchar *addr)
343{
344 int i;
345 unsigned int seed = get_ticks();
346
347 for (i = 0; i < 6; i++)
348 addr[i] = rand_r(&seed);
349
350 addr[0] &= 0xfe; /* clear multicast bit */
351 addr[0] |= 0x02; /* set local assignment bit (IEEE802) */
352}
353
354/**
355 * is_zero_ethaddr - Determine if give Ethernet address is all zeros.
356 * @addr: Pointer to a six-byte array containing the Ethernet address
357 *
358 * Return true if the address is all zeroes.
359 */
360static inline int is_zero_ethaddr(const u8 *addr)
361{
362 return !(addr[0] | addr[1] | addr[2] | addr[3] | addr[4] | addr[5]);
363}
364
365/**
366 * is_multicast_ethaddr - Determine if the Ethernet address is a multicast.
367 * @addr: Pointer to a six-byte array containing the Ethernet address
368 *
369 * Return true if the address is a multicast address.
370 * By definition the broadcast address is also a multicast address.
371 */
372static inline int is_multicast_ethaddr(const u8 *addr)
373{
374 return 0x01 & addr[0];
375}
376
377/*
378 * is_broadcast_ethaddr - Determine if the Ethernet address is broadcast
379 * @addr: Pointer to a six-byte array containing the Ethernet address
380 *
381 * Return true if the address is the broadcast address.
382 */
383static inline int is_broadcast_ethaddr(const u8 *addr)
384{
385 return (addr[0] & addr[1] & addr[2] & addr[3] & addr[4] & addr[5]) ==
386 0xff;
387}
388
389/*
390 * is_valid_ethaddr - Determine if the given Ethernet address is valid
391 * @addr: Pointer to a six-byte array containing the Ethernet address
392 *
393 * Check that the Ethernet address (MAC) is not 00:00:00:00:00:00, is not
394 * a multicast address, and is not FF:FF:FF:FF:FF:FF.
395 *
396 * Return true if the address is valid.
397 */
398static inline int is_valid_ethaddr(const u8 *addr)
399{
400 /* FF:FF:FF:FF:FF:FF is a multicast address so we don't need to
401 * explicitly check for it here. */
402 return !is_multicast_ethaddr(addr) && !is_zero_ethaddr(addr);
403}
404
405/**
406 * string_to_enetaddr() - Parse a MAC address
407 *
408 * Convert a string MAC address
409 *
410 * Implemented in lib/net_utils.c (built unconditionally)
411 *
412 * @addr: MAC address in aa:bb:cc:dd:ee:ff format, where each part is a 2-digit
413 * hex value
414 * @enetaddr: Place to put MAC address (6 bytes)
415 */
416void string_to_enetaddr(const char *addr, uint8_t *enetaddr);
417
418/**
419 * string_to_ip() - Convert a string to ip address
420 *
421 * Implemented in lib/net_utils.c (built unconditionally)
422 *
423 * @s: Input string to parse
424 * @return: in_addr struct containing the parsed IP address
425 */
426struct in_addr string_to_ip(const char *s);
427
428/* copy a filename (allow for "..." notation, limit length) */
429void copy_filename(char *dst, const char *src, int size);
430
431/* Processes a received packet */
432void net_process_received_packet(uchar *in_packet, int len);
433
434/**
435 * update_tftp - Update firmware over TFTP (via DFU)
436 *
437 * This function updates board's firmware via TFTP
438 *
439 * @param addr - memory address where data is stored
440 * @param interface - the DFU medium name - e.g. "mmc"
441 * @param devstring - the DFU medium number - e.g. "1"
442 *
443 * Return: - 0 on success, other value on failure
444 */
445int update_tftp(ulong addr, char *interface, char *devstring);
446
447/**
448 * env_get_ip() - Convert an environment value to to an ip address
449 *
450 * @var: Environment variable to convert. The value of this variable must be
451 * in the format format a.b.c.d, where each value is a decimal number from
452 * 0 to 255
453 * Return: IP address, or 0 if invalid
454 */
455static inline struct in_addr env_get_ip(char *var)
456{
457 return string_to_ip(env_get(var));
458}
459
460int net_init(void);
461
Jerome Forissier6a78e962024-10-16 12:04:05 +0200462/* NET compatibility */
463enum proto_t;
464int net_loop(enum proto_t protocol);
465
Jerome Forissier64abd782024-10-16 12:04:00 +0200466/**
467 * dhcp_run() - Run DHCP on the current ethernet device
468 *
469 * This sets the autoload variable, then puts it back to similar to its original
470 * state (y, n or unset).
471 *
472 * @addr: Address to load the file into (0 if @autoload is false)
473 * @fname: Filename of file to load (NULL if @autoload is false or to use the
474 * default filename)
475 * @autoload: true to load the file, false to just get the network IP
476 * @return 0 if OK, -EINVAL if the environment failed, -ENOENT if ant file was
477 * not found
478 */
479int dhcp_run(ulong addr, const char *fname, bool autoload);
480
481/**
482 * do_tftpb - Run the tftpboot command
483 *
484 * @cmdtp: Command information for tftpboot
485 * @flag: Command flags (CMD_FLAG_...)
486 * @argc: Number of arguments
487 * @argv: List of arguments
488 * Return: result (see enum command_ret_t)
489 */
490int do_tftpb(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]);
491
492/**
493 * wget_with_dns() - runs dns host IP address resulution before wget
494 *
495 * @dst_addr: destination address to download the file
496 * @uri: uri string of target file of wget
497 * Return: downloaded file size, negative if failed
498 */
499int wget_with_dns(ulong dst_addr, char *uri);
500/**
501 * wget_validate_uri() - varidate the uri
502 *
503 * @uri: uri string of target file of wget
504 * Return: true if uri is valid, false if uri is invalid
505 */
506bool wget_validate_uri(char *uri);
507//int do_wget(struct cmd_tbl *cmdtp, int flag, int argc, char * const argv[]);
508
509#endif /* __NET_COMMON_H__ */