blob: 6bc76658d9d6ad003f480687f10329169d692d6f [file] [log] [blame]
Jerome Forissier1d5d2922024-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 */
121
122/**
123 * compute_ip_checksum() - Compute IP checksum
124 *
125 * @addr: Address to check (must be 16-bit aligned)
126 * @nbytes: Number of bytes to check (normally a multiple of 2)
127 * Return: 16-bit IP checksum
128 */
129unsigned compute_ip_checksum(const void *addr, unsigned nbytes);
130
131/**
132 * ip_checksum_ok() - check if a checksum is correct
133 *
134 * This works by making sure the checksum sums to 0
135 *
136 * @addr: Address to check (must be 16-bit aligned)
137 * @nbytes: Number of bytes to check (normally a multiple of 2)
138 * Return: true if the checksum matches, false if not
139 */
140int ip_checksum_ok(const void *addr, unsigned nbytes);
141
142/**
143 * add_ip_checksums() - add two IP checksums
144 *
145 * @offset: Offset of first sum (if odd we do a byte-swap)
146 * @sum: First checksum
147 * @new_sum: New checksum to add
148 * Return: updated 16-bit IP checksum
149 */
150unsigned add_ip_checksums(unsigned offset, unsigned sum, unsigned new_sum);
151
152/*
153 * The devname can be either an exact name given by the driver or device tree
154 * or it can be an alias of the form "eth%d"
155 */
156struct udevice *eth_get_dev_by_name(const char *devname);
157int eth_is_active(struct udevice *dev); /* Test device for active state */
158
159/*
160 * Get the hardware address for an ethernet interface .
161 * Args:
162 * base_name - base name for device (normally "eth")
163 * index - device index number (0 for first)
164 * enetaddr - returns 6 byte hardware address
165 * Returns:
166 * Return true if the address is valid.
167 */
168int eth_env_get_enetaddr_by_index(const char *base_name, int index,
169 uchar *enetaddr);
170
171/**
172 * eth_env_set_enetaddr_by_index() - set the MAC address environment variable
173 *
174 * This sets up an environment variable with the given MAC address (@enetaddr).
175 * The environment variable to be set is defined by <@base_name><@index>addr.
176 * If @index is 0 it is omitted. For common Ethernet this means ethaddr,
177 * eth1addr, etc.
178 *
179 * @base_name: Base name for variable, typically "eth"
180 * @index: Index of interface being updated (>=0)
181 * @enetaddr: Pointer to MAC address to put into the variable
182 * Return: 0 if OK, other value on error
183 */
184int eth_env_set_enetaddr_by_index(const char *base_name, int index,
185 uchar *enetaddr);
186
187/*
188 * Initialize USB ethernet device with CONFIG_DM_ETH
189 * Returns:
190 * 0 is success, non-zero is error status.
191 */
192int usb_ether_init(void);
193
194int eth_init(void); /* Initialize the device */
Jerome Forissier7ad5e872024-10-16 12:04:01 +0200195int eth_start_udev(struct udevice *dev); /* ->start() if not already running */
Jerome Forissier1d5d2922024-10-16 12:04:00 +0200196int eth_send(void *packet, int length); /* Send a packet */
197#if defined(CONFIG_API) || defined(CONFIG_EFI_LOADER)
198int eth_receive(void *packet, int length); /* Receive a packet*/
199extern void (*push_packet)(void *packet, int length);
200#endif
201int eth_rx(void); /* Check for received packets */
202
203/**
204 * reset_phy() - Reset the Ethernet PHY
205 *
206 * This should be implemented by boards if CONFIG_RESET_PHY_R is enabled
207 */
208void reset_phy(void);
209
210#if CONFIG_IS_ENABLED(NET) || CONFIG_IS_ENABLED(NET_LWIP)
211/**
212 * eth_set_enable_bootdevs() - Enable or disable binding of Ethernet bootdevs
213 *
214 * These get in the way of bootstd testing, so are normally disabled by tests.
215 * This provide control of this setting. It only affects binding of Ethernet
216 * devices, so if that has already happened, this flag does nothing.
217 *
218 * @enable: true to enable binding of bootdevs when binding new Ethernet
219 * devices, false to disable it
220 */
221void eth_set_enable_bootdevs(bool enable);
222#else
223static inline void eth_set_enable_bootdevs(bool enable) {}
224#endif
225
226static inline void net_send_packet(uchar *pkt, int len)
227{
228 if (DEBUG_NET_PKT_TRACE)
229 print_hex_dump_bytes("tx: ", DUMP_PREFIX_OFFSET, pkt, len);
230 /* Currently no way to return errors from eth_send() */
231 (void) eth_send(pkt, len);
232}
233
234enum eth_recv_flags {
235 /*
236 * Check hardware device for new packets (otherwise only return those
237 * which are already in the memory buffer ready to process)
238 */
239 ETH_RECV_CHECK_DEVICE = 1 << 0,
240};
241
242/**
243 * struct eth_ops - functions of Ethernet MAC controllers
244 *
245 * start: Prepare the hardware to send and receive packets
246 * send: Send the bytes passed in "packet" as a packet on the wire
247 * recv: Check if the hardware received a packet. If so, set the pointer to the
248 * packet buffer in the packetp parameter. If not, return an error or 0 to
249 * indicate that the hardware receive FIFO is empty. If 0 is returned, the
250 * network stack will not process the empty packet, but free_pkt() will be
251 * called if supplied
252 * free_pkt: Give the driver an opportunity to manage its packet buffer memory
253 * when the network stack is finished processing it. This will only be
254 * called when no error was returned from recv - optional
255 * stop: Stop the hardware from looking for packets - may be called even if
256 * state == PASSIVE
257 * mcast: Join or leave a multicast group (for TFTP) - optional
258 * write_hwaddr: Write a MAC address to the hardware (used to pass it to Linux
259 * on some platforms like ARM). This function expects the
260 * eth_pdata::enetaddr field to be populated. The method can
261 * return -ENOSYS to indicate that this is not implemented for
262 this hardware - optional.
263 * read_rom_hwaddr: Some devices have a backup of the MAC address stored in a
264 * ROM on the board. This is how the driver should expose it
265 * to the network stack. This function should fill in the
266 * eth_pdata::enetaddr field - optional
267 * set_promisc: Enable or Disable promiscuous mode
268 * get_sset_count: Number of statistics counters
269 * get_string: Names of the statistic counters
270 * get_stats: The values of the statistic counters
271 */
272struct eth_ops {
273 int (*start)(struct udevice *dev);
274 int (*send)(struct udevice *dev, void *packet, int length);
275 int (*recv)(struct udevice *dev, int flags, uchar **packetp);
276 int (*free_pkt)(struct udevice *dev, uchar *packet, int length);
277 void (*stop)(struct udevice *dev);
278 int (*mcast)(struct udevice *dev, const u8 *enetaddr, int join);
279 int (*write_hwaddr)(struct udevice *dev);
280 int (*read_rom_hwaddr)(struct udevice *dev);
281 int (*set_promisc)(struct udevice *dev, bool enable);
282 int (*get_sset_count)(struct udevice *dev);
283 void (*get_strings)(struct udevice *dev, u8 *data);
284 void (*get_stats)(struct udevice *dev, u64 *data);
285};
286
287#define eth_get_ops(dev) ((struct eth_ops *)(dev)->driver->ops)
288
289struct udevice *eth_get_dev(void); /* get the current device */
290unsigned char *eth_get_ethaddr(void); /* get the current device MAC */
291int eth_rx(void); /* Check for received packets */
292void eth_halt(void); /* stop SCC */
293const char *eth_get_name(void); /* get name of current device */
294int eth_get_dev_index(void);
295
296int eth_initialize(void); /* Initialize network subsystem */
297void eth_try_another(int first_restart); /* Change the device */
298void eth_set_current(void); /* set nterface to ethcur var */
299
300enum eth_state_t {
301 ETH_STATE_INIT,
302 ETH_STATE_PASSIVE,
303 ETH_STATE_ACTIVE
304};
305
306/**
307 * struct eth_pdata - Platform data for Ethernet MAC controllers
308 *
309 * @iobase: The base address of the hardware registers
310 * @enetaddr: The Ethernet MAC address that is loaded from EEPROM or env
311 * @phy_interface: PHY interface to use - see PHY_INTERFACE_MODE_...
312 * @max_speed: Maximum speed of Ethernet connection supported by MAC
313 * @priv_pdata: device specific plat
314 */
315struct eth_pdata {
316 phys_addr_t iobase;
317 unsigned char enetaddr[ARP_HLEN];
318 int phy_interface;
319 int max_speed;
320 void *priv_pdata;
321};
322
323struct ethernet_hdr {
324 u8 et_dest[ARP_HLEN]; /* Destination node */
325 u8 et_src[ARP_HLEN]; /* Source node */
326 u16 et_protlen; /* Protocol or length */
327} __attribute__((packed));
328
329/* Ethernet header size */
330#define ETHER_HDR_SIZE (sizeof(struct ethernet_hdr))
331
332/**
333 * net_random_ethaddr - Generate software assigned random Ethernet address
334 * @addr: Pointer to a six-byte array containing the Ethernet address
335 *
336 * Generate a random Ethernet address (MAC) that is not multicast
337 * and has the local assigned bit set.
338 */
339static inline void net_random_ethaddr(uchar *addr)
340{
341 int i;
342 unsigned int seed = get_ticks();
343
344 for (i = 0; i < 6; i++)
345 addr[i] = rand_r(&seed);
346
347 addr[0] &= 0xfe; /* clear multicast bit */
348 addr[0] |= 0x02; /* set local assignment bit (IEEE802) */
349}
350
351/**
352 * is_zero_ethaddr - Determine if give Ethernet address is all zeros.
353 * @addr: Pointer to a six-byte array containing the Ethernet address
354 *
355 * Return true if the address is all zeroes.
356 */
357static inline int is_zero_ethaddr(const u8 *addr)
358{
359 return !(addr[0] | addr[1] | addr[2] | addr[3] | addr[4] | addr[5]);
360}
361
362/**
363 * is_multicast_ethaddr - Determine if the Ethernet address is a multicast.
364 * @addr: Pointer to a six-byte array containing the Ethernet address
365 *
366 * Return true if the address is a multicast address.
367 * By definition the broadcast address is also a multicast address.
368 */
369static inline int is_multicast_ethaddr(const u8 *addr)
370{
371 return 0x01 & addr[0];
372}
373
374/*
375 * is_broadcast_ethaddr - Determine if the Ethernet address is broadcast
376 * @addr: Pointer to a six-byte array containing the Ethernet address
377 *
378 * Return true if the address is the broadcast address.
379 */
380static inline int is_broadcast_ethaddr(const u8 *addr)
381{
382 return (addr[0] & addr[1] & addr[2] & addr[3] & addr[4] & addr[5]) ==
383 0xff;
384}
385
386/*
387 * is_valid_ethaddr - Determine if the given Ethernet address is valid
388 * @addr: Pointer to a six-byte array containing the Ethernet address
389 *
390 * Check that the Ethernet address (MAC) is not 00:00:00:00:00:00, is not
391 * a multicast address, and is not FF:FF:FF:FF:FF:FF.
392 *
393 * Return true if the address is valid.
394 */
395static inline int is_valid_ethaddr(const u8 *addr)
396{
397 /* FF:FF:FF:FF:FF:FF is a multicast address so we don't need to
398 * explicitly check for it here. */
399 return !is_multicast_ethaddr(addr) && !is_zero_ethaddr(addr);
400}
401
402/**
403 * string_to_enetaddr() - Parse a MAC address
404 *
405 * Convert a string MAC address
406 *
407 * Implemented in lib/net_utils.c (built unconditionally)
408 *
409 * @addr: MAC address in aa:bb:cc:dd:ee:ff format, where each part is a 2-digit
410 * hex value
411 * @enetaddr: Place to put MAC address (6 bytes)
412 */
413void string_to_enetaddr(const char *addr, uint8_t *enetaddr);
414
415/**
416 * string_to_ip() - Convert a string to ip address
417 *
418 * Implemented in lib/net_utils.c (built unconditionally)
419 *
420 * @s: Input string to parse
421 * @return: in_addr struct containing the parsed IP address
422 */
423struct in_addr string_to_ip(const char *s);
424
425/* copy a filename (allow for "..." notation, limit length) */
426void copy_filename(char *dst, const char *src, int size);
427
428/* Processes a received packet */
429void net_process_received_packet(uchar *in_packet, int len);
430
431/**
432 * update_tftp - Update firmware over TFTP (via DFU)
433 *
434 * This function updates board's firmware via TFTP
435 *
436 * @param addr - memory address where data is stored
437 * @param interface - the DFU medium name - e.g. "mmc"
438 * @param devstring - the DFU medium number - e.g. "1"
439 *
440 * Return: - 0 on success, other value on failure
441 */
442int update_tftp(ulong addr, char *interface, char *devstring);
443
444/**
445 * env_get_ip() - Convert an environment value to to an ip address
446 *
447 * @var: Environment variable to convert. The value of this variable must be
448 * in the format format a.b.c.d, where each value is a decimal number from
449 * 0 to 255
450 * Return: IP address, or 0 if invalid
451 */
452static inline struct in_addr env_get_ip(char *var)
453{
454 return string_to_ip(env_get(var));
455}
456
457int net_init(void);
458
459/**
460 * dhcp_run() - Run DHCP on the current ethernet device
461 *
462 * This sets the autoload variable, then puts it back to similar to its original
463 * state (y, n or unset).
464 *
465 * @addr: Address to load the file into (0 if @autoload is false)
466 * @fname: Filename of file to load (NULL if @autoload is false or to use the
467 * default filename)
468 * @autoload: true to load the file, false to just get the network IP
469 * @return 0 if OK, -EINVAL if the environment failed, -ENOENT if ant file was
470 * not found
471 */
472int dhcp_run(ulong addr, const char *fname, bool autoload);
473
474/**
475 * do_tftpb - Run the tftpboot command
476 *
477 * @cmdtp: Command information for tftpboot
478 * @flag: Command flags (CMD_FLAG_...)
479 * @argc: Number of arguments
480 * @argv: List of arguments
481 * Return: result (see enum command_ret_t)
482 */
483int do_tftpb(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]);
484
485/**
486 * wget_with_dns() - runs dns host IP address resulution before wget
487 *
488 * @dst_addr: destination address to download the file
489 * @uri: uri string of target file of wget
490 * Return: downloaded file size, negative if failed
491 */
492int wget_with_dns(ulong dst_addr, char *uri);
493/**
494 * wget_validate_uri() - varidate the uri
495 *
496 * @uri: uri string of target file of wget
497 * Return: true if uri is valid, false if uri is invalid
498 */
499bool wget_validate_uri(char *uri);
500//int do_wget(struct cmd_tbl *cmdtp, int flag, int argc, char * const argv[]);
501
502#endif /* __NET_COMMON_H__ */