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