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, <waltje@uWalt.NL.Mugnet.ORG> 13 * 14 * Relocated to include/linux where it belongs by Alan Cox 15 * <gw4pts@gw4pts.ampr.org> 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 <linux/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 int platform_get_ethdev_address(struct device *dev, struct net_device *netdev); 33 unsigned char *arch_get_platform_mac_address(void); 34 int nvmem_get_mac_address(struct device *dev, void *addrbuf); 35 int device_get_mac_address(struct device *dev, char *addr); 36 int device_get_ethdev_address(struct device *dev, struct net_device *netdev); 37 int fwnode_get_mac_address(struct fwnode_handle *fwnode, char *addr); 38 39 u32 eth_get_headlen(const struct net_device *dev, const void *data, u32 len); 40 __be16 eth_type_trans(struct sk_buff *skb, struct net_device *dev); 41 42 /** 43 * eth_type_trans_careful - eth_type_trans() for skbs coming from the stack 44 * @skb: buffer, possibly with less than ETH_HLEN bytes in its linear area 45 * @dev: receiving network device 46 * 47 * eth_type_trans() expects ETH_HLEN bytes in skb->head, which NIC drivers 48 * provide. Devices looping skbs from their transmit path back to the 49 * receive path (loopback, OVS internal ports) get skbs built by the stack, 50 * whose link-layer header might have been popped by tc or OVS actions. 51 * 52 * Sets skb->protocol on success. 53 * 54 * Return: SKB_NOT_DROPPED_YET on success, or a drop reason. In the latter 55 * case, the caller must free @skb. 56 */ 57 static inline enum skb_drop_reason 58 eth_type_trans_careful(struct sk_buff *skb, struct net_device *dev) 59 { 60 enum skb_drop_reason reason = pskb_may_pull_reason(skb, ETH_HLEN); 61 62 if (unlikely(reason)) 63 return reason; 64 65 skb->protocol = eth_type_trans(skb, dev); 66 return SKB_NOT_DROPPED_YET; 67 } 68 69 extern const struct header_ops eth_header_ops; 70 71 int eth_header(struct sk_buff *skb, struct net_device *dev, unsigned short type, 72 const void *daddr, const void *saddr, unsigned len); 73 int eth_header_parse(const struct sk_buff *skb, const struct net_device *dev, 74 unsigned char *haddr); 75 int eth_header_cache(const struct neighbour *neigh, struct hh_cache *hh, 76 __be16 type); 77 void eth_header_cache_update(struct hh_cache *hh, const struct net_device *dev, 78 const unsigned char *haddr); 79 __be16 eth_header_parse_protocol(const struct sk_buff *skb); 80 int eth_prepare_mac_addr_change(struct net_device *dev, void *p); 81 void eth_commit_mac_addr_change(struct net_device *dev, void *p); 82 int eth_mac_addr(struct net_device *dev, void *p); 83 int eth_validate_addr(struct net_device *dev); 84 85 struct net_device *alloc_etherdev_mqs(int sizeof_priv, unsigned int txqs, 86 unsigned int rxqs); 87 #define alloc_etherdev(sizeof_priv) alloc_etherdev_mq(sizeof_priv, 1) 88 #define alloc_etherdev_mq(sizeof_priv, count) alloc_etherdev_mqs(sizeof_priv, count, count) 89 90 struct net_device *devm_alloc_etherdev_mqs(struct device *dev, int sizeof_priv, 91 unsigned int txqs, 92 unsigned int rxqs); 93 #define devm_alloc_etherdev(dev, sizeof_priv) devm_alloc_etherdev_mqs(dev, sizeof_priv, 1, 1) 94 95 struct sk_buff *eth_gro_receive(struct list_head *head, struct sk_buff *skb); 96 int eth_gro_complete(struct sk_buff *skb, int nhoff); 97 98 /* Reserved Ethernet Addresses per IEEE 802.1Q */ 99 static const u8 eth_reserved_addr_base[ETH_ALEN] __aligned(2) = 100 { 0x01, 0x80, 0xc2, 0x00, 0x00, 0x00 }; 101 #define eth_stp_addr eth_reserved_addr_base 102 103 static const u8 eth_ipv4_mcast_addr_base[ETH_ALEN] __aligned(2) = 104 { 0x01, 0x00, 0x5e, 0x00, 0x00, 0x00 }; 105 106 static const u8 eth_ipv6_mcast_addr_base[ETH_ALEN] __aligned(2) = 107 { 0x33, 0x33, 0x00, 0x00, 0x00, 0x00 }; 108 109 /** 110 * is_link_local_ether_addr - Determine if given Ethernet address is link-local 111 * @addr: Pointer to a six-byte array containing the Ethernet address 112 * 113 * Return: true if address is link local reserved addr (01:80:c2:00:00:0X) per 114 * IEEE 802.1Q 8.6.3 Frame filtering. 115 * 116 * Please note: addr must be aligned to u16. 117 */ 118 static inline bool is_link_local_ether_addr(const u8 *addr) 119 { 120 __be16 *a = (__be16 *)addr; 121 static const __be16 *b = (const __be16 *)eth_reserved_addr_base; 122 static const __be16 m = cpu_to_be16(0xfff0); 123 124 #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) 125 return (((*(const u32 *)addr) ^ (*(const u32 *)b)) | 126 (__force int)((a[2] ^ b[2]) & m)) == 0; 127 #else 128 return ((a[0] ^ b[0]) | (a[1] ^ b[1]) | ((a[2] ^ b[2]) & m)) == 0; 129 #endif 130 } 131 132 /** 133 * is_zero_ether_addr - Determine if give Ethernet address is all zeros. 134 * @addr: Pointer to a six-byte array containing the Ethernet address 135 * 136 * Return: true if the address is all zeroes. 137 * 138 * Please note: addr must be aligned to u16. 139 */ 140 static inline bool is_zero_ether_addr(const u8 *addr) 141 { 142 #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) 143 return ((*(const u32 *)addr) | (*(const u16 *)(addr + 4))) == 0; 144 #else 145 return (*(const u16 *)(addr + 0) | 146 *(const u16 *)(addr + 2) | 147 *(const u16 *)(addr + 4)) == 0; 148 #endif 149 } 150 151 /** 152 * is_multicast_ether_addr - Determine if the Ethernet address is a multicast. 153 * @addr: Pointer to a six-byte array containing the Ethernet address 154 * 155 * Return: true if the address is a multicast address. 156 * By definition the broadcast address is also a multicast address. 157 */ 158 static inline bool is_multicast_ether_addr(const u8 *addr) 159 { 160 #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) 161 u32 a = *(const u32 *)addr; 162 #else 163 u16 a = *(const u16 *)addr; 164 #endif 165 #ifdef __BIG_ENDIAN 166 return 0x01 & (a >> ((sizeof(a) * 8) - 8)); 167 #else 168 return 0x01 & a; 169 #endif 170 } 171 172 static inline bool is_multicast_ether_addr_64bits(const u8 *addr) 173 { 174 #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) && BITS_PER_LONG == 64 175 #ifdef __BIG_ENDIAN 176 return 0x01 & ((*(const u64 *)addr) >> 56); 177 #else 178 return 0x01 & (*(const u64 *)addr); 179 #endif 180 #else 181 return is_multicast_ether_addr(addr); 182 #endif 183 } 184 185 /** 186 * is_local_ether_addr - Determine if the Ethernet address is locally-assigned one (IEEE 802). 187 * @addr: Pointer to a six-byte array containing the Ethernet address 188 * 189 * Return: true if the address is a local address. 190 */ 191 static inline bool is_local_ether_addr(const u8 *addr) 192 { 193 return 0x02 & addr[0]; 194 } 195 196 /** 197 * is_broadcast_ether_addr - Determine if the Ethernet address is broadcast 198 * @addr: Pointer to a six-byte array containing the Ethernet address 199 * 200 * Return: true if the address is the broadcast address. 201 * 202 * Please note: addr must be aligned to u16. 203 */ 204 static inline bool is_broadcast_ether_addr(const u8 *addr) 205 { 206 return (*(const u16 *)(addr + 0) & 207 *(const u16 *)(addr + 2) & 208 *(const u16 *)(addr + 4)) == 0xffff; 209 } 210 211 /** 212 * is_unicast_ether_addr - Determine if the Ethernet address is unicast 213 * @addr: Pointer to a six-byte array containing the Ethernet address 214 * 215 * Return: true if the address is a unicast address. 216 */ 217 static inline bool is_unicast_ether_addr(const u8 *addr) 218 { 219 return !is_multicast_ether_addr(addr); 220 } 221 222 /** 223 * is_valid_ether_addr - Determine if the given Ethernet address is valid 224 * @addr: Pointer to a six-byte array containing the Ethernet address 225 * 226 * Check that the Ethernet address (MAC) is not 00:00:00:00:00:00, is not 227 * a multicast address, and is not FF:FF:FF:FF:FF:FF. 228 * 229 * Return: true if the address is valid. 230 * 231 * Please note: addr must be aligned to u16. 232 */ 233 static inline bool is_valid_ether_addr(const u8 *addr) 234 { 235 /* FF:FF:FF:FF:FF:FF is a multicast address so we don't need to 236 * explicitly check for it here. */ 237 return !is_multicast_ether_addr(addr) && !is_zero_ether_addr(addr); 238 } 239 240 /** 241 * eth_proto_is_802_3 - Determine if a given Ethertype/length is a protocol 242 * @proto: Ethertype/length value to be tested 243 * 244 * Check that the value from the Ethertype/length field is a valid Ethertype. 245 * 246 * Return: true if the valid is an 802.3 supported Ethertype. 247 */ 248 static inline bool eth_proto_is_802_3(__be16 proto) 249 { 250 #ifndef __BIG_ENDIAN 251 /* if CPU is little endian mask off bits representing LSB */ 252 proto &= htons(0xFF00); 253 #endif 254 /* cast both to u16 and compare since LSB can be ignored */ 255 return (__force u16)proto >= (__force u16)htons(ETH_P_802_3_MIN); 256 } 257 258 /** 259 * eth_random_addr - Generate software assigned random Ethernet address 260 * @addr: Pointer to a six-byte array containing the Ethernet address 261 * 262 * Generate a random Ethernet address (MAC) that is not multicast 263 * and has the local assigned bit set. 264 */ 265 static inline void eth_random_addr(u8 *addr) 266 { 267 get_random_bytes(addr, ETH_ALEN); 268 addr[0] &= 0xfe; /* clear multicast bit */ 269 addr[0] |= 0x02; /* set local assignment bit (IEEE802) */ 270 } 271 272 /** 273 * eth_broadcast_addr - Assign broadcast address 274 * @addr: Pointer to a six-byte array containing the Ethernet address 275 * 276 * Assign the broadcast address to the given address array. 277 */ 278 static inline void eth_broadcast_addr(u8 *addr) 279 { 280 memset(addr, 0xff, ETH_ALEN); 281 } 282 283 /** 284 * eth_zero_addr - Assign zero address 285 * @addr: Pointer to a six-byte array containing the Ethernet address 286 * 287 * Assign the zero address to the given address array. 288 */ 289 static inline void eth_zero_addr(u8 *addr) 290 { 291 memset(addr, 0x00, ETH_ALEN); 292 } 293 294 /** 295 * eth_hw_addr_random - Generate software assigned random Ethernet and 296 * set device flag 297 * @dev: pointer to net_device structure 298 * 299 * Generate a random Ethernet address (MAC) to be used by a net device 300 * and set addr_assign_type so the state can be read by sysfs and be 301 * used by userspace. 302 */ 303 static inline void eth_hw_addr_random(struct net_device *dev) 304 { 305 u8 addr[ETH_ALEN]; 306 307 eth_random_addr(addr); 308 __dev_addr_set(dev, addr, ETH_ALEN); 309 dev->addr_assign_type = NET_ADDR_RANDOM; 310 } 311 312 /** 313 * eth_hw_addr_crc - Calculate CRC from netdev_hw_addr 314 * @ha: pointer to hardware address 315 * 316 * Calculate CRC from a hardware address as basis for filter hashes. 317 */ 318 static inline u32 eth_hw_addr_crc(struct netdev_hw_addr *ha) 319 { 320 return ether_crc(ETH_ALEN, ha->addr); 321 } 322 323 /** 324 * ether_addr_copy - Copy an Ethernet address 325 * @dst: Pointer to a six-byte array Ethernet address destination 326 * @src: Pointer to a six-byte array Ethernet address source 327 * 328 * Please note: dst & src must both be aligned to u16. 329 */ 330 static inline void ether_addr_copy(u8 *dst, const u8 *src) 331 { 332 #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) 333 *(u32 *)dst = *(const u32 *)src; 334 *(u16 *)(dst + 4) = *(const u16 *)(src + 4); 335 #else 336 u16 *a = (u16 *)dst; 337 const u16 *b = (const u16 *)src; 338 339 a[0] = b[0]; 340 a[1] = b[1]; 341 a[2] = b[2]; 342 #endif 343 } 344 345 /** 346 * eth_hw_addr_set - Assign Ethernet address to a net_device 347 * @dev: pointer to net_device structure 348 * @addr: address to assign 349 * 350 * Assign given address to the net_device, addr_assign_type is not changed. 351 */ 352 static inline void eth_hw_addr_set(struct net_device *dev, const u8 *addr) 353 { 354 __dev_addr_set(dev, addr, ETH_ALEN); 355 } 356 357 /** 358 * eth_hw_addr_inherit - Copy dev_addr from another net_device 359 * @dst: pointer to net_device to copy dev_addr to 360 * @src: pointer to net_device to copy dev_addr from 361 * 362 * Copy the Ethernet address from one net_device to another along with 363 * the address attributes (addr_assign_type). 364 */ 365 static inline void eth_hw_addr_inherit(struct net_device *dst, 366 struct net_device *src) 367 { 368 dst->addr_assign_type = src->addr_assign_type; 369 eth_hw_addr_set(dst, src->dev_addr); 370 } 371 372 /** 373 * ether_addr_equal - Compare two Ethernet addresses 374 * @addr1: Pointer to a six-byte array containing the Ethernet address 375 * @addr2: Pointer other six-byte array containing the Ethernet address 376 * 377 * Compare two Ethernet addresses, returns true if equal 378 * 379 * Please note: addr1 & addr2 must both be aligned to u16. 380 */ 381 static inline bool ether_addr_equal(const u8 *addr1, const u8 *addr2) 382 { 383 #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) 384 u32 fold = ((*(const u32 *)addr1) ^ (*(const u32 *)addr2)) | 385 ((*(const u16 *)(addr1 + 4)) ^ (*(const u16 *)(addr2 + 4))); 386 387 return fold == 0; 388 #else 389 const u16 *a = (const u16 *)addr1; 390 const u16 *b = (const u16 *)addr2; 391 392 return ((a[0] ^ b[0]) | (a[1] ^ b[1]) | (a[2] ^ b[2])) == 0; 393 #endif 394 } 395 396 /** 397 * ether_addr_equal_64bits - Compare two Ethernet addresses 398 * @addr1: Pointer to an array of 8 bytes 399 * @addr2: Pointer to an other array of 8 bytes 400 * 401 * Compare two Ethernet addresses, returns true if equal, false otherwise. 402 * 403 * The function doesn't need any conditional branches and possibly uses 404 * word memory accesses on CPU allowing cheap unaligned memory reads. 405 * arrays = { byte1, byte2, byte3, byte4, byte5, byte6, pad1, pad2 } 406 * 407 * Please note that alignment of addr1 & addr2 are only guaranteed to be 16 bits. 408 */ 409 410 static inline bool ether_addr_equal_64bits(const u8 *addr1, const u8 *addr2) 411 { 412 #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) && BITS_PER_LONG == 64 413 u64 fold = (*(const u64 *)addr1) ^ (*(const u64 *)addr2); 414 415 #ifdef __BIG_ENDIAN 416 return (fold >> 16) == 0; 417 #else 418 return (fold << 16) == 0; 419 #endif 420 #else 421 return ether_addr_equal(addr1, addr2); 422 #endif 423 } 424 425 /** 426 * ether_addr_equal_unaligned - Compare two not u16 aligned Ethernet addresses 427 * @addr1: Pointer to a six-byte array containing the Ethernet address 428 * @addr2: Pointer other six-byte array containing the Ethernet address 429 * 430 * Compare two Ethernet addresses, returns true if equal 431 * 432 * Please note: Use only when any Ethernet address may not be u16 aligned. 433 */ 434 static inline bool ether_addr_equal_unaligned(const u8 *addr1, const u8 *addr2) 435 { 436 #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) 437 return ether_addr_equal(addr1, addr2); 438 #else 439 return memcmp(addr1, addr2, ETH_ALEN) == 0; 440 #endif 441 } 442 443 /** 444 * ether_addr_equal_masked - Compare two Ethernet addresses with a mask 445 * @addr1: Pointer to a six-byte array containing the 1st Ethernet address 446 * @addr2: Pointer to a six-byte array containing the 2nd Ethernet address 447 * @mask: Pointer to a six-byte array containing the Ethernet address bitmask 448 * 449 * Compare two Ethernet addresses with a mask, returns true if for every bit 450 * set in the bitmask the equivalent bits in the ethernet addresses are equal. 451 * Using a mask with all bits set is a slower ether_addr_equal. 452 */ 453 static inline bool ether_addr_equal_masked(const u8 *addr1, const u8 *addr2, 454 const u8 *mask) 455 { 456 int i; 457 458 for (i = 0; i < ETH_ALEN; i++) { 459 if ((addr1[i] ^ addr2[i]) & mask[i]) 460 return false; 461 } 462 463 return true; 464 } 465 466 static inline bool ether_addr_is_ipv4_mcast(const u8 *addr) 467 { 468 u8 mask[ETH_ALEN] = { 0xff, 0xff, 0xff, 0x80, 0x00, 0x00 }; 469 470 return ether_addr_equal_masked(addr, eth_ipv4_mcast_addr_base, mask); 471 } 472 473 static inline bool ether_addr_is_ipv6_mcast(const u8 *addr) 474 { 475 u8 mask[ETH_ALEN] = { 0xff, 0xff, 0x00, 0x00, 0x00, 0x00 }; 476 477 return ether_addr_equal_masked(addr, eth_ipv6_mcast_addr_base, mask); 478 } 479 480 static inline bool ether_addr_is_ip_mcast(const u8 *addr) 481 { 482 return ether_addr_is_ipv4_mcast(addr) || 483 ether_addr_is_ipv6_mcast(addr); 484 } 485 486 /** 487 * ether_addr_to_u64 - Convert an Ethernet address into a u64 value. 488 * @addr: Pointer to a six-byte array containing the Ethernet address 489 * 490 * Return: a u64 value of the address 491 */ 492 static inline u64 ether_addr_to_u64(const u8 *addr) 493 { 494 u64 u = 0; 495 int i; 496 497 for (i = 0; i < ETH_ALEN; i++) 498 u = u << 8 | addr[i]; 499 500 return u; 501 } 502 503 /** 504 * u64_to_ether_addr - Convert a u64 to an Ethernet address. 505 * @u: u64 to convert to an Ethernet MAC address 506 * @addr: Pointer to a six-byte array to contain the Ethernet address 507 */ 508 static inline void u64_to_ether_addr(u64 u, u8 *addr) 509 { 510 int i; 511 512 for (i = ETH_ALEN - 1; i >= 0; i--) { 513 addr[i] = u & 0xff; 514 u = u >> 8; 515 } 516 } 517 518 /** 519 * eth_addr_dec - Decrement the given MAC address 520 * 521 * @addr: Pointer to a six-byte array containing Ethernet address to decrement 522 */ 523 static inline void eth_addr_dec(u8 *addr) 524 { 525 u64 u = ether_addr_to_u64(addr); 526 527 u--; 528 u64_to_ether_addr(u, addr); 529 } 530 531 /** 532 * eth_addr_inc() - Increment the given MAC address. 533 * @addr: Pointer to a six-byte array containing Ethernet address to increment. 534 */ 535 static inline void eth_addr_inc(u8 *addr) 536 { 537 u64 u = ether_addr_to_u64(addr); 538 539 u++; 540 u64_to_ether_addr(u, addr); 541 } 542 543 /** 544 * eth_addr_add() - Add (or subtract) an offset to/from the given MAC address. 545 * 546 * @offset: Offset to add. 547 * @addr: Pointer to a six-byte array containing Ethernet address to increment. 548 */ 549 static inline void eth_addr_add(u8 *addr, long offset) 550 { 551 u64 u = ether_addr_to_u64(addr); 552 553 u += offset; 554 u64_to_ether_addr(u, addr); 555 } 556 557 /** 558 * is_etherdev_addr - Tell if given Ethernet address belongs to the device. 559 * @dev: Pointer to a device structure 560 * @addr: Pointer to a six-byte array containing the Ethernet address 561 * 562 * Compare passed address with all addresses of the device. Return true if the 563 * address if one of the device addresses. 564 * 565 * Note that this function calls ether_addr_equal_64bits() so take care of 566 * the right padding. 567 */ 568 static inline bool is_etherdev_addr(const struct net_device *dev, 569 const u8 addr[6 + 2]) 570 { 571 struct netdev_hw_addr *ha; 572 bool res = false; 573 574 rcu_read_lock(); 575 for_each_dev_addr(dev, ha) { 576 res = ether_addr_equal_64bits(addr, ha->addr); 577 if (res) 578 break; 579 } 580 rcu_read_unlock(); 581 return res; 582 } 583 #endif /* __KERNEL__ */ 584 585 /** 586 * compare_ether_header - Compare two Ethernet headers 587 * @a: Pointer to Ethernet header 588 * @b: Pointer to Ethernet header 589 * 590 * Compare two Ethernet headers, returns 0 if equal. 591 * This assumes that the network header (i.e., IP header) is 4-byte 592 * aligned OR the platform can handle unaligned access. This is the 593 * case for all packets coming into netif_receive_skb or similar 594 * entry points. 595 */ 596 597 static inline unsigned long compare_ether_header(const void *a, const void *b) 598 { 599 #if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) && BITS_PER_LONG == 64 600 unsigned long fold; 601 602 /* 603 * We want to compare 14 bytes: 604 * [a0 ... a13] ^ [b0 ... b13] 605 * Use two long XOR, ORed together, with an overlap of two bytes. 606 * [a0 a1 a2 a3 a4 a5 a6 a7 ] ^ [b0 b1 b2 b3 b4 b5 b6 b7 ] | 607 * [a6 a7 a8 a9 a10 a11 a12 a13] ^ [b6 b7 b8 b9 b10 b11 b12 b13] 608 * This means the [a6 a7] ^ [b6 b7] part is done two times. 609 */ 610 fold = *(unsigned long *)a ^ *(unsigned long *)b; 611 fold |= *(unsigned long *)(a + 6) ^ *(unsigned long *)(b + 6); 612 return fold; 613 #else 614 u32 *a32 = (u32 *)((u8 *)a + 2); 615 u32 *b32 = (u32 *)((u8 *)b + 2); 616 617 return (*(u16 *)a ^ *(u16 *)b) | (a32[0] ^ b32[0]) | 618 (a32[1] ^ b32[1]) | (a32[2] ^ b32[2]); 619 #endif 620 } 621 622 /** 623 * eth_hw_addr_gen - Generate and assign Ethernet address to a port 624 * @dev: pointer to port's net_device structure 625 * @base_addr: base Ethernet address 626 * @id: offset to add to the base address 627 * 628 * Generate a MAC address using a base address and an offset and assign it 629 * to a net_device. Commonly used by switch drivers which need to compute 630 * addresses for all their ports. addr_assign_type is not changed. 631 */ 632 static inline void eth_hw_addr_gen(struct net_device *dev, const u8 *base_addr, 633 unsigned int id) 634 { 635 u64 u = ether_addr_to_u64(base_addr); 636 u8 addr[ETH_ALEN]; 637 638 u += id; 639 u64_to_ether_addr(u, addr); 640 eth_hw_addr_set(dev, addr); 641 } 642 643 /** 644 * eth_skb_pkt_type - Assign packet type if destination address does not match 645 * @skb: Assigned a packet type if address does not match @dev address 646 * @dev: Network device used to compare packet address against 647 * 648 * If the destination MAC address of the packet does not match the network 649 * device address, assign an appropriate packet type. 650 */ 651 static inline void eth_skb_pkt_type(struct sk_buff *skb, 652 const struct net_device *dev) 653 { 654 const struct ethhdr *eth = eth_hdr(skb); 655 656 if (unlikely(!ether_addr_equal_64bits(eth->h_dest, dev->dev_addr))) { 657 if (unlikely(is_multicast_ether_addr_64bits(eth->h_dest))) { 658 if (ether_addr_equal_64bits(eth->h_dest, dev->broadcast)) 659 skb->pkt_type = PACKET_BROADCAST; 660 else 661 skb->pkt_type = PACKET_MULTICAST; 662 } else { 663 skb->pkt_type = PACKET_OTHERHOST; 664 } 665 } 666 } 667 668 static inline struct ethhdr *eth_skb_pull_mac(struct sk_buff *skb) 669 { 670 struct ethhdr *eth = (struct ethhdr *)skb->data; 671 672 skb_pull_inline(skb, ETH_HLEN); 673 return eth; 674 } 675 676 /** 677 * eth_skb_pad - Pad buffer to minimum number of octets for Ethernet frame 678 * @skb: Buffer to pad 679 * 680 * An Ethernet frame should have a minimum size of 60 bytes. This function 681 * takes short frames and pads them with zeros up to the 60 byte limit. 682 */ 683 static inline int eth_skb_pad(struct sk_buff *skb) 684 { 685 return skb_put_padto(skb, ETH_ZLEN); 686 } 687 688 #endif /* _LINUX_ETHERDEVICE_H */ 689