xref: /linux-6.15/include/linux/etherdevice.h (revision bc2cda1e)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * INET		An implementation of the TCP/IP protocol suite for the LINUX
31da177e4SLinus Torvalds  *		operating system.  NET  is implemented using the  BSD Socket
41da177e4SLinus Torvalds  *		interface as the means of communication with the user level.
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  *		Definitions for the Ethernet handlers.
71da177e4SLinus Torvalds  *
81da177e4SLinus Torvalds  * Version:	@(#)eth.h	1.0.4	05/13/93
91da177e4SLinus Torvalds  *
1002c30a84SJesper Juhl  * Authors:	Ross Biro
111da177e4SLinus Torvalds  *		Fred N. van Kempen, <[email protected]>
121da177e4SLinus Torvalds  *
131da177e4SLinus Torvalds  *		Relocated to include/linux where it belongs by Alan Cox
141da177e4SLinus Torvalds  *							<[email protected]>
151da177e4SLinus Torvalds  *
161da177e4SLinus Torvalds  *		This program is free software; you can redistribute it and/or
171da177e4SLinus Torvalds  *		modify it under the terms of the GNU General Public License
181da177e4SLinus Torvalds  *		as published by the Free Software Foundation; either version
191da177e4SLinus Torvalds  *		2 of the License, or (at your option) any later version.
201da177e4SLinus Torvalds  *
211da177e4SLinus Torvalds  *	WARNING: This move may well be temporary. This file will get merged with others RSN.
221da177e4SLinus Torvalds  *
231da177e4SLinus Torvalds  */
241da177e4SLinus Torvalds #ifndef _LINUX_ETHERDEVICE_H
251da177e4SLinus Torvalds #define _LINUX_ETHERDEVICE_H
261da177e4SLinus Torvalds 
271da177e4SLinus Torvalds #include <linux/if_ether.h>
28bcd61272SArnd Bergmann #include <linux/netdevice.h>
291da177e4SLinus Torvalds #include <linux/random.h>
301da177e4SLinus Torvalds 
311da177e4SLinus Torvalds #ifdef __KERNEL__
32ab611487SAlexey Dobriyan extern __be16		eth_type_trans(struct sk_buff *skb, struct net_device *dev);
333b04dddeSStephen Hemminger extern const struct header_ops eth_header_ops;
343b04dddeSStephen Hemminger 
353b04dddeSStephen Hemminger extern int eth_header(struct sk_buff *skb, struct net_device *dev,
363b04dddeSStephen Hemminger 		      unsigned short type,
373b04dddeSStephen Hemminger 		      const void *daddr, const void *saddr, unsigned len);
383b04dddeSStephen Hemminger extern int eth_rebuild_header(struct sk_buff *skb);
393b04dddeSStephen Hemminger extern int eth_header_parse(const struct sk_buff *skb, unsigned char *haddr);
403b04dddeSStephen Hemminger extern int eth_header_cache(const struct neighbour *neigh, struct hh_cache *hh);
413b04dddeSStephen Hemminger extern void eth_header_cache_update(struct hh_cache *hh,
423b04dddeSStephen Hemminger 				    const struct net_device *dev,
433b04dddeSStephen Hemminger 				    const unsigned char *haddr);
443b04dddeSStephen Hemminger 
451da177e4SLinus Torvalds 
46f25f4e44SPeter P Waskiewicz Jr extern struct net_device *alloc_etherdev_mq(int sizeof_priv, unsigned int queue_count);
47f25f4e44SPeter P Waskiewicz Jr #define alloc_etherdev(sizeof_priv) alloc_etherdev_mq(sizeof_priv, 1)
481da177e4SLinus Torvalds 
491da177e4SLinus Torvalds /**
50c2da8acaSStephen Hemminger  * is_zero_ether_addr - Determine if give Ethernet address is all zeros.
51c2da8acaSStephen Hemminger  * @addr: Pointer to a six-byte array containing the Ethernet address
52c2da8acaSStephen Hemminger  *
53c2da8acaSStephen Hemminger  * Return true if the address is all zeroes.
541da177e4SLinus Torvalds  */
551da177e4SLinus Torvalds static inline int is_zero_ether_addr(const u8 *addr)
561da177e4SLinus Torvalds {
571da177e4SLinus Torvalds 	return !(addr[0] | addr[1] | addr[2] | addr[3] | addr[4] | addr[5]);
581da177e4SLinus Torvalds }
591da177e4SLinus Torvalds 
601da177e4SLinus Torvalds /**
61c2da8acaSStephen Hemminger  * is_multicast_ether_addr - Determine if the Ethernet address is a multicast.
6279165121SMichael Ellerman  * @addr: Pointer to a six-byte array containing the Ethernet address
6379165121SMichael Ellerman  *
6479165121SMichael Ellerman  * Return true if the address is a multicast address.
6588df8ef5SStephen Hemminger  * By definition the broadcast address is also a multicast address.
6679165121SMichael Ellerman  */
6779165121SMichael Ellerman static inline int is_multicast_ether_addr(const u8 *addr)
6879165121SMichael Ellerman {
6988df8ef5SStephen Hemminger 	return (0x01 & addr[0]);
7079165121SMichael Ellerman }
7179165121SMichael Ellerman 
72c2da8acaSStephen Hemminger /**
73*bc2cda1eSRandy Dunlap  * is_local_ether_addr - Determine if the Ethernet address is locally-assigned one (IEEE 802).
74e024715fSPaolo 'Blaisorblade' Giarrusso  * @addr: Pointer to a six-byte array containing the Ethernet address
75e024715fSPaolo 'Blaisorblade' Giarrusso  *
76e024715fSPaolo 'Blaisorblade' Giarrusso  * Return true if the address is a local address.
77e024715fSPaolo 'Blaisorblade' Giarrusso  */
78e024715fSPaolo 'Blaisorblade' Giarrusso static inline int is_local_ether_addr(const u8 *addr)
79e024715fSPaolo 'Blaisorblade' Giarrusso {
80e024715fSPaolo 'Blaisorblade' Giarrusso 	return (0x02 & addr[0]);
81e024715fSPaolo 'Blaisorblade' Giarrusso }
82e024715fSPaolo 'Blaisorblade' Giarrusso 
83e024715fSPaolo 'Blaisorblade' Giarrusso /**
84c2da8acaSStephen Hemminger  * is_broadcast_ether_addr - Determine if the Ethernet address is broadcast
85c2da8acaSStephen Hemminger  * @addr: Pointer to a six-byte array containing the Ethernet address
86c2da8acaSStephen Hemminger  *
87c2da8acaSStephen Hemminger  * Return true if the address is the broadcast address.
88c2da8acaSStephen Hemminger  */
899bd481f8SJeff Garzik static inline int is_broadcast_ether_addr(const u8 *addr)
909bd481f8SJeff Garzik {
912407534fSStephen Hemminger 	return (addr[0] & addr[1] & addr[2] & addr[3] & addr[4] & addr[5]) == 0xff;
929bd481f8SJeff Garzik }
939bd481f8SJeff Garzik 
9479165121SMichael Ellerman /**
951da177e4SLinus Torvalds  * is_valid_ether_addr - Determine if the given Ethernet address is valid
961da177e4SLinus Torvalds  * @addr: Pointer to a six-byte array containing the Ethernet address
971da177e4SLinus Torvalds  *
981da177e4SLinus Torvalds  * Check that the Ethernet address (MAC) is not 00:00:00:00:00:00, is not
9979165121SMichael Ellerman  * a multicast address, and is not FF:FF:FF:FF:FF:FF.
1001da177e4SLinus Torvalds  *
1011da177e4SLinus Torvalds  * Return true if the address is valid.
1021da177e4SLinus Torvalds  */
1031da177e4SLinus Torvalds static inline int is_valid_ether_addr(const u8 *addr)
1041da177e4SLinus Torvalds {
10579165121SMichael Ellerman 	/* FF:FF:FF:FF:FF:FF is a multicast address so we don't need to
10679165121SMichael Ellerman 	 * explicitly check for it here. */
10779165121SMichael Ellerman 	return !is_multicast_ether_addr(addr) && !is_zero_ether_addr(addr);
1081da177e4SLinus Torvalds }
1091da177e4SLinus Torvalds 
1101da177e4SLinus Torvalds /**
1111da177e4SLinus Torvalds  * random_ether_addr - Generate software assigned random Ethernet address
1121da177e4SLinus Torvalds  * @addr: Pointer to a six-byte array containing the Ethernet address
1131da177e4SLinus Torvalds  *
1141da177e4SLinus Torvalds  * Generate a random Ethernet address (MAC) that is not multicast
1151da177e4SLinus Torvalds  * and has the local assigned bit set.
1161da177e4SLinus Torvalds  */
1171da177e4SLinus Torvalds static inline void random_ether_addr(u8 *addr)
1181da177e4SLinus Torvalds {
1191da177e4SLinus Torvalds 	get_random_bytes (addr, ETH_ALEN);
1201da177e4SLinus Torvalds 	addr [0] &= 0xfe;	/* clear multicast bit */
1211da177e4SLinus Torvalds 	addr [0] |= 0x02;	/* set local assignment bit (IEEE802) */
1221da177e4SLinus Torvalds }
123360ac8e2SStephen Hemminger 
124360ac8e2SStephen Hemminger /**
125360ac8e2SStephen Hemminger  * compare_ether_addr - Compare two Ethernet addresses
126360ac8e2SStephen Hemminger  * @addr1: Pointer to a six-byte array containing the Ethernet address
127c2da8acaSStephen Hemminger  * @addr2: Pointer other six-byte array containing the Ethernet address
128360ac8e2SStephen Hemminger  *
129360ac8e2SStephen Hemminger  * Compare two ethernet addresses, returns 0 if equal
130360ac8e2SStephen Hemminger  */
131c2da8acaSStephen Hemminger static inline unsigned compare_ether_addr(const u8 *addr1, const u8 *addr2)
132360ac8e2SStephen Hemminger {
133c2da8acaSStephen Hemminger 	const u16 *a = (const u16 *) addr1;
134c2da8acaSStephen Hemminger 	const u16 *b = (const u16 *) addr2;
135360ac8e2SStephen Hemminger 
136360ac8e2SStephen Hemminger 	BUILD_BUG_ON(ETH_ALEN != 6);
137360ac8e2SStephen Hemminger 	return ((a[0] ^ b[0]) | (a[1] ^ b[1]) | (a[2] ^ b[2])) != 0;
138360ac8e2SStephen Hemminger }
13979165121SMichael Ellerman #endif	/* __KERNEL__ */
1401da177e4SLinus Torvalds 
1411da177e4SLinus Torvalds #endif	/* _LINUX_ETHERDEVICE_H */
142