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