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