1 /* SPDX-License-Identifier: GPL-2.0-or-later */ 2 /* 3 * INET An implementation of the TCP/IP protocol suite for the LINUX 4 * operating system. NET is implemented using the BSD Socket 5 * interface as the means of communication with the user level. 6 * 7 * Definitions for the Ethernet handlers. 8 * 9 * Version: @(#)eth.h 1.0.4 05/13/93 10 * 11 * Authors: Ross Biro 12 * Fred N. van Kempen, <[email protected]> 13 * 14 * Relocated to include/linux where it belongs by Alan Cox 15 * <[email protected]> 16 */ 17 #ifndef _LINUX_ETHERDEVICE_H 18 #define _LINUX_ETHERDEVICE_H 19 20 #include <linux/if_ether.h> 21 #include <linux/netdevice.h> 22 #include <linux/random.h> 23 #include <linux/crc32.h> 24 #include <asm/unaligned.h> 25 #include <asm/bitsperlong.h> 26 27 #ifdef __KERNEL__ 28 struct device; 29 struct fwnode_handle; 30 31 int eth_platform_get_mac_address(struct device *dev, u8 *mac_addr); 32 unsigned char *arch_get_platform_mac_address(void); 33 int nvmem_get_mac_address(struct device *dev, void *addrbuf); 34 void *device_get_mac_address(struct device *dev, char *addr, int alen); 35 void *fwnode_get_mac_address(struct fwnode_handle *fwnode, 36 char *addr, int alen); 37 38 u32 eth_get_headlen(const struct net_device *dev, const void *data, u32 len); 39 __be16 eth_type_trans(struct sk_buff *skb, struct net_device *dev); 40 extern const struct header_ops eth_header_ops; 41 42 int eth_header(struct sk_buff *skb, struct net_device *dev, unsigned short type, 43 const void *daddr, const void *saddr, unsigned len); 44 int eth_header_parse(const struct sk_buff *skb, unsigned char *haddr); 45 int eth_header_cache(const struct neighbour *neigh, struct hh_cache *hh, 46 __be16 type); 47 void eth_header_cache_update(struct hh_cache *hh, const struct net_device *dev, 48 const unsigned char *haddr); 49 __be16 eth_header_parse_protocol(const struct sk_buff *skb); 50 int eth_prepare_mac_addr_change(struct net_device *dev, void *p); 51 void eth_commit_mac_addr_change(struct net_device *dev, void *p); 52 int eth_mac_addr(struct net_device *dev, void *p); 53 int eth_validate_addr(struct net_device *dev); 54 55 struct net_device *alloc_etherdev_mqs(int sizeof_priv, unsigned int txqs, 56 unsigned int rxqs); 57 #define alloc_etherdev(sizeof_priv) alloc_etherdev_mq(sizeof_priv, 1) 58 #define alloc_etherdev_mq(sizeof_priv, count) alloc_etherdev_mqs(sizeof_priv, count, count) 59 60 struct net_device *devm_alloc_etherdev_mqs(struct device *dev, int sizeof_priv, 61 unsigned int txqs, 62 unsigned int rxqs); 63 #define devm_alloc_etherdev(dev, sizeof_priv) devm_alloc_etherdev_mqs(dev, sizeof_priv, 1, 1) 64 65 struct sk_buff *eth_gro_receive(struct list_head *head, struct sk_buff *skb); 66 int eth_gro_complete(struct sk_buff *skb, int nhoff); 67 68 /* Reserved Ethernet Addresses per IEEE 802.1Q */ 69 static const u8 eth_reserved_addr_base[ETH_ALEN] __aligned(2) = 70 { 0x01, 0x80, 0xc2, 0x00, 0x00, 0x00 }; 71 #define eth_stp_addr eth_reserved_addr_base 72 73 /** 74 * is_link_local_ether_addr - Determine if given Ethernet address is link-local 75 * @addr: Pointer to a six-byte array containing the Ethernet address 76 * 77 * Return true if address is link local reserved addr (01:80:c2:00:00:0X) per 78 * IEEE 802.1Q 8.6.3 Frame filtering. 79 * 80 * Please note: addr must be aligned to u16. 81 */ 82 static inline bool is_link_local_ether_addr(const u8 *addr) 83 { 84 __be16 *a = (__be16 *)addr; 85 static const __be16 *b = (const __be16 *)eth_reserved_addr_base; 86 static const __be16 m = cpu_to_be16(0xfff0); 87 88 #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) 89 return (((*(const u32 *)addr) ^ (*(const u32 *)b)) | 90 (__force int)((a[2] ^ b[2]) & m)) == 0; 91 #else 92 return ((a[0] ^ b[0]) | (a[1] ^ b[1]) | ((a[2] ^ b[2]) & m)) == 0; 93 #endif 94 } 95 96 /** 97 * is_zero_ether_addr - Determine if give Ethernet address is all zeros. 98 * @addr: Pointer to a six-byte array containing the Ethernet address 99 * 100 * Return true if the address is all zeroes. 101 * 102 * Please note: addr must be aligned to u16. 103 */ 104 static inline bool is_zero_ether_addr(const u8 *addr) 105 { 106 #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) 107 return ((*(const u32 *)addr) | (*(const u16 *)(addr + 4))) == 0; 108 #else 109 return (*(const u16 *)(addr + 0) | 110 *(const u16 *)(addr + 2) | 111 *(const u16 *)(addr + 4)) == 0; 112 #endif 113 } 114 115 /** 116 * is_multicast_ether_addr - Determine if the Ethernet address is a multicast. 117 * @addr: Pointer to a six-byte array containing the Ethernet address 118 * 119 * Return true if the address is a multicast address. 120 * By definition the broadcast address is also a multicast address. 121 */ 122 static inline bool is_multicast_ether_addr(const u8 *addr) 123 { 124 #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) 125 u32 a = *(const u32 *)addr; 126 #else 127 u16 a = *(const u16 *)addr; 128 #endif 129 #ifdef __BIG_ENDIAN 130 return 0x01 & (a >> ((sizeof(a) * 8) - 8)); 131 #else 132 return 0x01 & a; 133 #endif 134 } 135 136 static inline bool is_multicast_ether_addr_64bits(const u8 addr[6+2]) 137 { 138 #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) && BITS_PER_LONG == 64 139 #ifdef __BIG_ENDIAN 140 return 0x01 & ((*(const u64 *)addr) >> 56); 141 #else 142 return 0x01 & (*(const u64 *)addr); 143 #endif 144 #else 145 return is_multicast_ether_addr(addr); 146 #endif 147 } 148 149 /** 150 * is_local_ether_addr - Determine if the Ethernet address is locally-assigned one (IEEE 802). 151 * @addr: Pointer to a six-byte array containing the Ethernet address 152 * 153 * Return true if the address is a local address. 154 */ 155 static inline bool is_local_ether_addr(const u8 *addr) 156 { 157 return 0x02 & addr[0]; 158 } 159 160 /** 161 * is_broadcast_ether_addr - Determine if the Ethernet address is broadcast 162 * @addr: Pointer to a six-byte array containing the Ethernet address 163 * 164 * Return true if the address is the broadcast address. 165 * 166 * Please note: addr must be aligned to u16. 167 */ 168 static inline bool is_broadcast_ether_addr(const u8 *addr) 169 { 170 return (*(const u16 *)(addr + 0) & 171 *(const u16 *)(addr + 2) & 172 *(const u16 *)(addr + 4)) == 0xffff; 173 } 174 175 /** 176 * is_unicast_ether_addr - Determine if the Ethernet address is unicast 177 * @addr: Pointer to a six-byte array containing the Ethernet address 178 * 179 * Return true if the address is a unicast address. 180 */ 181 static inline bool is_unicast_ether_addr(const u8 *addr) 182 { 183 return !is_multicast_ether_addr(addr); 184 } 185 186 /** 187 * is_valid_ether_addr - Determine if the given Ethernet address is valid 188 * @addr: Pointer to a six-byte array containing the Ethernet address 189 * 190 * Check that the Ethernet address (MAC) is not 00:00:00:00:00:00, is not 191 * a multicast address, and is not FF:FF:FF:FF:FF:FF. 192 * 193 * Return true if the address is valid. 194 * 195 * Please note: addr must be aligned to u16. 196 */ 197 static inline bool is_valid_ether_addr(const u8 *addr) 198 { 199 /* FF:FF:FF:FF:FF:FF is a multicast address so we don't need to 200 * explicitly check for it here. */ 201 return !is_multicast_ether_addr(addr) && !is_zero_ether_addr(addr); 202 } 203 204 /** 205 * eth_proto_is_802_3 - Determine if a given Ethertype/length is a protocol 206 * @proto: Ethertype/length value to be tested 207 * 208 * Check that the value from the Ethertype/length field is a valid Ethertype. 209 * 210 * Return true if the valid is an 802.3 supported Ethertype. 211 */ 212 static inline bool eth_proto_is_802_3(__be16 proto) 213 { 214 #ifndef __BIG_ENDIAN 215 /* if CPU is little endian mask off bits representing LSB */ 216 proto &= htons(0xFF00); 217 #endif 218 /* cast both to u16 and compare since LSB can be ignored */ 219 return (__force u16)proto >= (__force u16)htons(ETH_P_802_3_MIN); 220 } 221 222 /** 223 * eth_random_addr - Generate software assigned random Ethernet address 224 * @addr: Pointer to a six-byte array containing the Ethernet address 225 * 226 * Generate a random Ethernet address (MAC) that is not multicast 227 * and has the local assigned bit set. 228 */ 229 static inline void eth_random_addr(u8 *addr) 230 { 231 get_random_bytes(addr, ETH_ALEN); 232 addr[0] &= 0xfe; /* clear multicast bit */ 233 addr[0] |= 0x02; /* set local assignment bit (IEEE802) */ 234 } 235 236 #define random_ether_addr(addr) eth_random_addr(addr) 237 238 /** 239 * eth_broadcast_addr - Assign broadcast address 240 * @addr: Pointer to a six-byte array containing the Ethernet address 241 * 242 * Assign the broadcast address to the given address array. 243 */ 244 static inline void eth_broadcast_addr(u8 *addr) 245 { 246 memset(addr, 0xff, ETH_ALEN); 247 } 248 249 /** 250 * eth_zero_addr - Assign zero address 251 * @addr: Pointer to a six-byte array containing the Ethernet address 252 * 253 * Assign the zero address to the given address array. 254 */ 255 static inline void eth_zero_addr(u8 *addr) 256 { 257 memset(addr, 0x00, ETH_ALEN); 258 } 259 260 /** 261 * eth_hw_addr_random - Generate software assigned random Ethernet and 262 * set device flag 263 * @dev: pointer to net_device structure 264 * 265 * Generate a random Ethernet address (MAC) to be used by a net device 266 * and set addr_assign_type so the state can be read by sysfs and be 267 * used by userspace. 268 */ 269 static inline void eth_hw_addr_random(struct net_device *dev) 270 { 271 dev->addr_assign_type = NET_ADDR_RANDOM; 272 eth_random_addr(dev->dev_addr); 273 } 274 275 /** 276 * eth_hw_addr_crc - Calculate CRC from netdev_hw_addr 277 * @ha: pointer to hardware address 278 * 279 * Calculate CRC from a hardware address as basis for filter hashes. 280 */ 281 static inline u32 eth_hw_addr_crc(struct netdev_hw_addr *ha) 282 { 283 return ether_crc(ETH_ALEN, ha->addr); 284 } 285 286 /** 287 * ether_addr_copy - Copy an Ethernet address 288 * @dst: Pointer to a six-byte array Ethernet address destination 289 * @src: Pointer to a six-byte array Ethernet address source 290 * 291 * Please note: dst & src must both be aligned to u16. 292 */ 293 static inline void ether_addr_copy(u8 *dst, const u8 *src) 294 { 295 #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) 296 *(u32 *)dst = *(const u32 *)src; 297 *(u16 *)(dst + 4) = *(const u16 *)(src + 4); 298 #else 299 u16 *a = (u16 *)dst; 300 const u16 *b = (const u16 *)src; 301 302 a[0] = b[0]; 303 a[1] = b[1]; 304 a[2] = b[2]; 305 #endif 306 } 307 308 /** 309 * eth_hw_addr_set - Assign Ethernet address to a net_device 310 * @dev: pointer to net_device structure 311 * @addr: address to assign 312 * 313 * Assign given address to the net_device, addr_assign_type is not changed. 314 */ 315 static inline void eth_hw_addr_set(struct net_device *dev, const u8 *addr) 316 { 317 ether_addr_copy(dev->dev_addr, addr); 318 } 319 320 /** 321 * eth_hw_addr_inherit - Copy dev_addr from another net_device 322 * @dst: pointer to net_device to copy dev_addr to 323 * @src: pointer to net_device to copy dev_addr from 324 * 325 * Copy the Ethernet address from one net_device to another along with 326 * the address attributes (addr_assign_type). 327 */ 328 static inline void eth_hw_addr_inherit(struct net_device *dst, 329 struct net_device *src) 330 { 331 dst->addr_assign_type = src->addr_assign_type; 332 eth_hw_addr_set(dst, src->dev_addr); 333 } 334 335 /** 336 * ether_addr_equal - Compare two Ethernet addresses 337 * @addr1: Pointer to a six-byte array containing the Ethernet address 338 * @addr2: Pointer other six-byte array containing the Ethernet address 339 * 340 * Compare two Ethernet addresses, returns true if equal 341 * 342 * Please note: addr1 & addr2 must both be aligned to u16. 343 */ 344 static inline bool ether_addr_equal(const u8 *addr1, const u8 *addr2) 345 { 346 #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) 347 u32 fold = ((*(const u32 *)addr1) ^ (*(const u32 *)addr2)) | 348 ((*(const u16 *)(addr1 + 4)) ^ (*(const u16 *)(addr2 + 4))); 349 350 return fold == 0; 351 #else 352 const u16 *a = (const u16 *)addr1; 353 const u16 *b = (const u16 *)addr2; 354 355 return ((a[0] ^ b[0]) | (a[1] ^ b[1]) | (a[2] ^ b[2])) == 0; 356 #endif 357 } 358 359 /** 360 * ether_addr_equal_64bits - Compare two Ethernet addresses 361 * @addr1: Pointer to an array of 8 bytes 362 * @addr2: Pointer to an other array of 8 bytes 363 * 364 * Compare two Ethernet addresses, returns true if equal, false otherwise. 365 * 366 * The function doesn't need any conditional branches and possibly uses 367 * word memory accesses on CPU allowing cheap unaligned memory reads. 368 * arrays = { byte1, byte2, byte3, byte4, byte5, byte6, pad1, pad2 } 369 * 370 * Please note that alignment of addr1 & addr2 are only guaranteed to be 16 bits. 371 */ 372 373 static inline bool ether_addr_equal_64bits(const u8 addr1[6+2], 374 const u8 addr2[6+2]) 375 { 376 #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) && BITS_PER_LONG == 64 377 u64 fold = (*(const u64 *)addr1) ^ (*(const u64 *)addr2); 378 379 #ifdef __BIG_ENDIAN 380 return (fold >> 16) == 0; 381 #else 382 return (fold << 16) == 0; 383 #endif 384 #else 385 return ether_addr_equal(addr1, addr2); 386 #endif 387 } 388 389 /** 390 * ether_addr_equal_unaligned - Compare two not u16 aligned Ethernet addresses 391 * @addr1: Pointer to a six-byte array containing the Ethernet address 392 * @addr2: Pointer other six-byte array containing the Ethernet address 393 * 394 * Compare two Ethernet addresses, returns true if equal 395 * 396 * Please note: Use only when any Ethernet address may not be u16 aligned. 397 */ 398 static inline bool ether_addr_equal_unaligned(const u8 *addr1, const u8 *addr2) 399 { 400 #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) 401 return ether_addr_equal(addr1, addr2); 402 #else 403 return memcmp(addr1, addr2, ETH_ALEN) == 0; 404 #endif 405 } 406 407 /** 408 * ether_addr_equal_masked - Compare two Ethernet addresses with a mask 409 * @addr1: Pointer to a six-byte array containing the 1st Ethernet address 410 * @addr2: Pointer to a six-byte array containing the 2nd Ethernet address 411 * @mask: Pointer to a six-byte array containing the Ethernet address bitmask 412 * 413 * Compare two Ethernet addresses with a mask, returns true if for every bit 414 * set in the bitmask the equivalent bits in the ethernet addresses are equal. 415 * Using a mask with all bits set is a slower ether_addr_equal. 416 */ 417 static inline bool ether_addr_equal_masked(const u8 *addr1, const u8 *addr2, 418 const u8 *mask) 419 { 420 int i; 421 422 for (i = 0; i < ETH_ALEN; i++) { 423 if ((addr1[i] ^ addr2[i]) & mask[i]) 424 return false; 425 } 426 427 return true; 428 } 429 430 /** 431 * ether_addr_to_u64 - Convert an Ethernet address into a u64 value. 432 * @addr: Pointer to a six-byte array containing the Ethernet address 433 * 434 * Return a u64 value of the address 435 */ 436 static inline u64 ether_addr_to_u64(const u8 *addr) 437 { 438 u64 u = 0; 439 int i; 440 441 for (i = 0; i < ETH_ALEN; i++) 442 u = u << 8 | addr[i]; 443 444 return u; 445 } 446 447 /** 448 * u64_to_ether_addr - Convert a u64 to an Ethernet address. 449 * @u: u64 to convert to an Ethernet MAC address 450 * @addr: Pointer to a six-byte array to contain the Ethernet address 451 */ 452 static inline void u64_to_ether_addr(u64 u, u8 *addr) 453 { 454 int i; 455 456 for (i = ETH_ALEN - 1; i >= 0; i--) { 457 addr[i] = u & 0xff; 458 u = u >> 8; 459 } 460 } 461 462 /** 463 * eth_addr_dec - Decrement the given MAC address 464 * 465 * @addr: Pointer to a six-byte array containing Ethernet address to decrement 466 */ 467 static inline void eth_addr_dec(u8 *addr) 468 { 469 u64 u = ether_addr_to_u64(addr); 470 471 u--; 472 u64_to_ether_addr(u, addr); 473 } 474 475 /** 476 * eth_addr_inc() - Increment the given MAC address. 477 * @addr: Pointer to a six-byte array containing Ethernet address to increment. 478 */ 479 static inline void eth_addr_inc(u8 *addr) 480 { 481 u64 u = ether_addr_to_u64(addr); 482 483 u++; 484 u64_to_ether_addr(u, addr); 485 } 486 487 /** 488 * is_etherdev_addr - Tell if given Ethernet address belongs to the device. 489 * @dev: Pointer to a device structure 490 * @addr: Pointer to a six-byte array containing the Ethernet address 491 * 492 * Compare passed address with all addresses of the device. Return true if the 493 * address if one of the device addresses. 494 * 495 * Note that this function calls ether_addr_equal_64bits() so take care of 496 * the right padding. 497 */ 498 static inline bool is_etherdev_addr(const struct net_device *dev, 499 const u8 addr[6 + 2]) 500 { 501 struct netdev_hw_addr *ha; 502 bool res = false; 503 504 rcu_read_lock(); 505 for_each_dev_addr(dev, ha) { 506 res = ether_addr_equal_64bits(addr, ha->addr); 507 if (res) 508 break; 509 } 510 rcu_read_unlock(); 511 return res; 512 } 513 #endif /* __KERNEL__ */ 514 515 /** 516 * compare_ether_header - Compare two Ethernet headers 517 * @a: Pointer to Ethernet header 518 * @b: Pointer to Ethernet header 519 * 520 * Compare two Ethernet headers, returns 0 if equal. 521 * This assumes that the network header (i.e., IP header) is 4-byte 522 * aligned OR the platform can handle unaligned access. This is the 523 * case for all packets coming into netif_receive_skb or similar 524 * entry points. 525 */ 526 527 static inline unsigned long compare_ether_header(const void *a, const void *b) 528 { 529 #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) && BITS_PER_LONG == 64 530 unsigned long fold; 531 532 /* 533 * We want to compare 14 bytes: 534 * [a0 ... a13] ^ [b0 ... b13] 535 * Use two long XOR, ORed together, with an overlap of two bytes. 536 * [a0 a1 a2 a3 a4 a5 a6 a7 ] ^ [b0 b1 b2 b3 b4 b5 b6 b7 ] | 537 * [a6 a7 a8 a9 a10 a11 a12 a13] ^ [b6 b7 b8 b9 b10 b11 b12 b13] 538 * This means the [a6 a7] ^ [b6 b7] part is done two times. 539 */ 540 fold = *(unsigned long *)a ^ *(unsigned long *)b; 541 fold |= *(unsigned long *)(a + 6) ^ *(unsigned long *)(b + 6); 542 return fold; 543 #else 544 u32 *a32 = (u32 *)((u8 *)a + 2); 545 u32 *b32 = (u32 *)((u8 *)b + 2); 546 547 return (*(u16 *)a ^ *(u16 *)b) | (a32[0] ^ b32[0]) | 548 (a32[1] ^ b32[1]) | (a32[2] ^ b32[2]); 549 #endif 550 } 551 552 /** 553 * eth_skb_pad - Pad buffer to mininum number of octets for Ethernet frame 554 * @skb: Buffer to pad 555 * 556 * An Ethernet frame should have a minimum size of 60 bytes. This function 557 * takes short frames and pads them with zeros up to the 60 byte limit. 558 */ 559 static inline int eth_skb_pad(struct sk_buff *skb) 560 { 561 return skb_put_padto(skb, ETH_ZLEN); 562 } 563 564 #endif /* _LINUX_ETHERDEVICE_H */ 565