1 /*-
2  * Copyright (c) 2007 Cisco Systems, Inc.  All rights reserved.
3  * Copyright (c) 2014 Mellanox Technologies, Ltd. All rights reserved.
4  *
5  * This software is available to you under a choice of one of two
6  * licenses.  You may choose to be licensed under the terms of the GNU
7  * General Public License (GPL) Version 2, available from the file
8  * COPYING in the main directory of this source tree, or the
9  * OpenIB.org BSD license below:
10  *
11  *     Redistribution and use in source and binary forms, with or
12  *     without modification, are permitted provided that the following
13  *     conditions are met:
14  *
15  *	- Redistributions of source code must retain the above
16  *	  copyright notice, this list of conditions and the following
17  *	  disclaimer.
18  *
19  *	- Redistributions in binary form must reproduce the above
20  *	  copyright notice, this list of conditions and the following
21  *	  disclaimer in the documentation and/or other materials
22  *	  provided with the distribution.
23  *
24  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
28  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
29  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31  * SOFTWARE.
32  */
33 
34 
35 #ifndef _LINUX_ETHERDEVICE
36 #define _LINUX_ETHERDEVICE
37 
38 #include <linux/types.h>
39 
40 #include <sys/random.h>
41 #include <sys/libkern.h>
42 
43 #define	ETH_MODULE_SFF_8079		1
44 #define	ETH_MODULE_SFF_8079_LEN		256
45 #define	ETH_MODULE_SFF_8472		2
46 #define	ETH_MODULE_SFF_8472_LEN		512
47 #define	ETH_MODULE_SFF_8636		3
48 #define	ETH_MODULE_SFF_8636_LEN		256
49 #define	ETH_MODULE_SFF_8436		4
50 #define	ETH_MODULE_SFF_8436_LEN		256
51 
52 struct ethtool_eeprom {
53 	u32	offset;
54 	u32	len;
55 };
56 
57 struct ethtool_modinfo {
58 	u32	type;
59 	u32	eeprom_len;
60 };
61 
62 /**
63  * is_zero_ether_addr - Determine if give Ethernet address is all zeros.
64  * @addr: Pointer to a six-byte array containing the Ethernet address
65  *
66  * Return true if the address is all zeroes.
67  */
is_zero_ether_addr(const u8 * addr)68 static inline bool is_zero_ether_addr(const u8 *addr)
69 {
70         return !(addr[0] | addr[1] | addr[2] | addr[3] | addr[4] | addr[5]);
71 }
72 
73 
74 
75 /**
76  * is_multicast_ether_addr - Determine if the Ethernet address is a multicast.
77  * @addr: Pointer to a six-byte array containing the Ethernet address
78  *
79  * Return true if the address is a multicast address.
80  * By definition the broadcast address is also a multicast address.
81  */
is_multicast_ether_addr(const u8 * addr)82 static inline bool is_multicast_ether_addr(const u8 *addr)
83 {
84         return (0x01 & addr[0]);
85 }
86 
87 /**
88  * is_broadcast_ether_addr - Determine if the Ethernet address is broadcast
89  * @addr: Pointer to a six-byte array containing the Ethernet address
90  *
91  * Return true if the address is the broadcast address.
92  */
is_broadcast_ether_addr(const u8 * addr)93 static inline bool is_broadcast_ether_addr(const u8 *addr)
94 {
95         return (addr[0] & addr[1] & addr[2] & addr[3] & addr[4] & addr[5]) == 0xff;
96 }
97 
98 /**
99  * is_valid_ether_addr - Determine if the given Ethernet address is valid
100  * @addr: Pointer to a six-byte array containing the Ethernet address
101  *
102  * Check that the Ethernet address (MAC) is not 00:00:00:00:00:00, is not
103  * a multicast address, and is not FF:FF:FF:FF:FF:FF.
104  *
105  * Return true if the address is valid.
106  **/
is_valid_ether_addr(const u8 * addr)107 static inline bool is_valid_ether_addr(const u8 *addr)
108 {
109         /* FF:FF:FF:FF:FF:FF is a multicast address so we don't need to
110         ** explicitly check for it here. */
111         return !is_multicast_ether_addr(addr) && !is_zero_ether_addr(addr);
112 }
113 
ether_addr_copy(u8 * dst,const u8 * src)114 static inline void ether_addr_copy(u8 *dst, const u8 *src)
115 {
116 	memcpy(dst, src, 6);
117 }
118 
119 static inline bool
ether_addr_equal(const u8 * pa,const u8 * pb)120 ether_addr_equal(const u8 *pa, const u8 *pb)
121 {
122 	return (memcmp(pa, pb, 6) == 0);
123 }
124 
125 static inline bool
ether_addr_equal_64bits(const u8 * pa,const u8 * pb)126 ether_addr_equal_64bits(const u8 *pa, const u8 *pb)
127 {
128 	return (memcmp(pa, pb, 6) == 0);
129 }
130 
131 static inline void
eth_broadcast_addr(u8 * pa)132 eth_broadcast_addr(u8 *pa)
133 {
134 	memset(pa, 0xff, 6);
135 }
136 
137 static inline void
random_ether_addr(u8 * dst)138 random_ether_addr(u8 * dst)
139 {
140 	if (read_random(dst, 6) == 0)
141 		arc4rand(dst, 6, 0);
142 
143 	dst[0] &= 0xfe;
144 	dst[0] |= 0x02;
145 }
146 
147 #endif /* _LINUX_ETHERDEVICE */
148