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