xref: /kernel/linux/linux-6.6/net/ethernet/eth.c (revision 62306a36)
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.  INET is implemented using the  BSD Socket
5 *		interface as the means of communication with the user level.
6 *
7 *		Ethernet-type device handling.
8 *
9 * Version:	@(#)eth.c	1.0.7	05/25/93
10 *
11 * Authors:	Ross Biro
12 *		Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
13 *		Mark Evans, <evansmp@uhura.aston.ac.uk>
14 *		Florian  La Roche, <rzsfl@rz.uni-sb.de>
15 *		Alan Cox, <gw4pts@gw4pts.ampr.org>
16 *
17 * Fixes:
18 *		Mr Linux	: Arp problems
19 *		Alan Cox	: Generic queue tidyup (very tiny here)
20 *		Alan Cox	: eth_header ntohs should be htons
21 *		Alan Cox	: eth_rebuild_header missing an htons and
22 *				  minor other things.
23 *		Tegge		: Arp bug fixes.
24 *		Florian		: Removed many unnecessary functions, code cleanup
25 *				  and changes for new arp and skbuff.
26 *		Alan Cox	: Redid header building to reflect new format.
27 *		Alan Cox	: ARP only when compiled with CONFIG_INET
28 *		Greg Page	: 802.2 and SNAP stuff.
29 *		Alan Cox	: MAC layer pointers/new format.
30 *		Paul Gortmaker	: eth_copy_and_sum shouldn't csum padding.
31 *		Alan Cox	: Protect against forwarding explosions with
32 *				  older network drivers and IFF_ALLMULTI.
33 *	Christer Weinigel	: Better rebuild header message.
34 *             Andrew Morton    : 26Feb01: kill ether_setup() - use netdev_boot_setup().
35 */
36#include <linux/module.h>
37#include <linux/types.h>
38#include <linux/kernel.h>
39#include <linux/string.h>
40#include <linux/mm.h>
41#include <linux/socket.h>
42#include <linux/in.h>
43#include <linux/inet.h>
44#include <linux/ip.h>
45#include <linux/netdevice.h>
46#include <linux/nvmem-consumer.h>
47#include <linux/etherdevice.h>
48#include <linux/skbuff.h>
49#include <linux/errno.h>
50#include <linux/init.h>
51#include <linux/if_ether.h>
52#include <linux/of_net.h>
53#include <linux/pci.h>
54#include <linux/property.h>
55#include <net/dst.h>
56#include <net/arp.h>
57#include <net/sock.h>
58#include <net/ipv6.h>
59#include <net/ip.h>
60#include <net/dsa.h>
61#include <net/flow_dissector.h>
62#include <net/gro.h>
63#include <linux/uaccess.h>
64#include <net/pkt_sched.h>
65
66/**
67 * eth_header - create the Ethernet header
68 * @skb:	buffer to alter
69 * @dev:	source device
70 * @type:	Ethernet type field
71 * @daddr: destination address (NULL leave destination address)
72 * @saddr: source address (NULL use device source address)
73 * @len:   packet length (<= skb->len)
74 *
75 *
76 * Set the protocol type. For a packet of type ETH_P_802_3/2 we put the length
77 * in here instead.
78 */
79int eth_header(struct sk_buff *skb, struct net_device *dev,
80	       unsigned short type,
81	       const void *daddr, const void *saddr, unsigned int len)
82{
83	struct ethhdr *eth = skb_push(skb, ETH_HLEN);
84
85	if (type != ETH_P_802_3 && type != ETH_P_802_2)
86		eth->h_proto = htons(type);
87	else
88		eth->h_proto = htons(len);
89
90	/*
91	 *      Set the source hardware address.
92	 */
93
94	if (!saddr)
95		saddr = dev->dev_addr;
96	memcpy(eth->h_source, saddr, ETH_ALEN);
97
98	if (daddr) {
99		memcpy(eth->h_dest, daddr, ETH_ALEN);
100		return ETH_HLEN;
101	}
102
103	/*
104	 *      Anyway, the loopback-device should never use this function...
105	 */
106
107	if (dev->flags & (IFF_LOOPBACK | IFF_NOARP)) {
108		eth_zero_addr(eth->h_dest);
109		return ETH_HLEN;
110	}
111
112	return -ETH_HLEN;
113}
114EXPORT_SYMBOL(eth_header);
115
116/**
117 * eth_get_headlen - determine the length of header for an ethernet frame
118 * @dev: pointer to network device
119 * @data: pointer to start of frame
120 * @len: total length of frame
121 *
122 * Make a best effort attempt to pull the length for all of the headers for
123 * a given frame in a linear buffer.
124 */
125u32 eth_get_headlen(const struct net_device *dev, const void *data, u32 len)
126{
127	const unsigned int flags = FLOW_DISSECTOR_F_PARSE_1ST_FRAG;
128	const struct ethhdr *eth = (const struct ethhdr *)data;
129	struct flow_keys_basic keys;
130
131	/* this should never happen, but better safe than sorry */
132	if (unlikely(len < sizeof(*eth)))
133		return len;
134
135	/* parse any remaining L2/L3 headers, check for L4 */
136	if (!skb_flow_dissect_flow_keys_basic(dev_net(dev), NULL, &keys, data,
137					      eth->h_proto, sizeof(*eth),
138					      len, flags))
139		return max_t(u32, keys.control.thoff, sizeof(*eth));
140
141	/* parse for any L4 headers */
142	return min_t(u32, __skb_get_poff(NULL, data, &keys, len), len);
143}
144EXPORT_SYMBOL(eth_get_headlen);
145
146/**
147 * eth_type_trans - determine the packet's protocol ID.
148 * @skb: received socket data
149 * @dev: receiving network device
150 *
151 * The rule here is that we
152 * assume 802.3 if the type field is short enough to be a length.
153 * This is normal practice and works for any 'now in use' protocol.
154 */
155__be16 eth_type_trans(struct sk_buff *skb, struct net_device *dev)
156{
157	unsigned short _service_access_point;
158	const unsigned short *sap;
159	const struct ethhdr *eth;
160
161	skb->dev = dev;
162	skb_reset_mac_header(skb);
163
164	eth = (struct ethhdr *)skb->data;
165	skb_pull_inline(skb, ETH_HLEN);
166
167	if (unlikely(!ether_addr_equal_64bits(eth->h_dest,
168					      dev->dev_addr))) {
169		if (unlikely(is_multicast_ether_addr_64bits(eth->h_dest))) {
170			if (ether_addr_equal_64bits(eth->h_dest, dev->broadcast))
171				skb->pkt_type = PACKET_BROADCAST;
172			else
173				skb->pkt_type = PACKET_MULTICAST;
174		} else {
175			skb->pkt_type = PACKET_OTHERHOST;
176		}
177	}
178
179	/*
180	 * Some variants of DSA tagging don't have an ethertype field
181	 * at all, so we check here whether one of those tagging
182	 * variants has been configured on the receiving interface,
183	 * and if so, set skb->protocol without looking at the packet.
184	 */
185	if (unlikely(netdev_uses_dsa(dev)))
186		return htons(ETH_P_XDSA);
187
188	if (likely(eth_proto_is_802_3(eth->h_proto)))
189		return eth->h_proto;
190
191	/*
192	 *      This is a magic hack to spot IPX packets. Older Novell breaks
193	 *      the protocol design and runs IPX over 802.3 without an 802.2 LLC
194	 *      layer. We look for FFFF which isn't a used 802.2 SSAP/DSAP. This
195	 *      won't work for fault tolerant netware but does for the rest.
196	 */
197	sap = skb_header_pointer(skb, 0, sizeof(*sap), &_service_access_point);
198	if (sap && *sap == 0xFFFF)
199		return htons(ETH_P_802_3);
200
201	/*
202	 *      Real 802.2 LLC
203	 */
204	return htons(ETH_P_802_2);
205}
206EXPORT_SYMBOL(eth_type_trans);
207
208/**
209 * eth_header_parse - extract hardware address from packet
210 * @skb: packet to extract header from
211 * @haddr: destination buffer
212 */
213int eth_header_parse(const struct sk_buff *skb, unsigned char *haddr)
214{
215	const struct ethhdr *eth = eth_hdr(skb);
216	memcpy(haddr, eth->h_source, ETH_ALEN);
217	return ETH_ALEN;
218}
219EXPORT_SYMBOL(eth_header_parse);
220
221/**
222 * eth_header_cache - fill cache entry from neighbour
223 * @neigh: source neighbour
224 * @hh: destination cache entry
225 * @type: Ethernet type field
226 *
227 * Create an Ethernet header template from the neighbour.
228 */
229int eth_header_cache(const struct neighbour *neigh, struct hh_cache *hh, __be16 type)
230{
231	struct ethhdr *eth;
232	const struct net_device *dev = neigh->dev;
233
234	eth = (struct ethhdr *)
235	    (((u8 *) hh->hh_data) + (HH_DATA_OFF(sizeof(*eth))));
236
237	if (type == htons(ETH_P_802_3))
238		return -1;
239
240	eth->h_proto = type;
241	memcpy(eth->h_source, dev->dev_addr, ETH_ALEN);
242	memcpy(eth->h_dest, neigh->ha, ETH_ALEN);
243
244	/* Pairs with READ_ONCE() in neigh_resolve_output(),
245	 * neigh_hh_output() and neigh_update_hhs().
246	 */
247	smp_store_release(&hh->hh_len, ETH_HLEN);
248
249	return 0;
250}
251EXPORT_SYMBOL(eth_header_cache);
252
253/**
254 * eth_header_cache_update - update cache entry
255 * @hh: destination cache entry
256 * @dev: network device
257 * @haddr: new hardware address
258 *
259 * Called by Address Resolution module to notify changes in address.
260 */
261void eth_header_cache_update(struct hh_cache *hh,
262			     const struct net_device *dev,
263			     const unsigned char *haddr)
264{
265	memcpy(((u8 *) hh->hh_data) + HH_DATA_OFF(sizeof(struct ethhdr)),
266	       haddr, ETH_ALEN);
267}
268EXPORT_SYMBOL(eth_header_cache_update);
269
270/**
271 * eth_header_parse_protocol - extract protocol from L2 header
272 * @skb: packet to extract protocol from
273 */
274__be16 eth_header_parse_protocol(const struct sk_buff *skb)
275{
276	const struct ethhdr *eth = eth_hdr(skb);
277
278	return eth->h_proto;
279}
280EXPORT_SYMBOL(eth_header_parse_protocol);
281
282/**
283 * eth_prepare_mac_addr_change - prepare for mac change
284 * @dev: network device
285 * @p: socket address
286 */
287int eth_prepare_mac_addr_change(struct net_device *dev, void *p)
288{
289	struct sockaddr *addr = p;
290
291	if (!(dev->priv_flags & IFF_LIVE_ADDR_CHANGE) && netif_running(dev))
292		return -EBUSY;
293	if (!is_valid_ether_addr(addr->sa_data))
294		return -EADDRNOTAVAIL;
295	return 0;
296}
297EXPORT_SYMBOL(eth_prepare_mac_addr_change);
298
299/**
300 * eth_commit_mac_addr_change - commit mac change
301 * @dev: network device
302 * @p: socket address
303 */
304void eth_commit_mac_addr_change(struct net_device *dev, void *p)
305{
306	struct sockaddr *addr = p;
307
308	eth_hw_addr_set(dev, addr->sa_data);
309}
310EXPORT_SYMBOL(eth_commit_mac_addr_change);
311
312/**
313 * eth_mac_addr - set new Ethernet hardware address
314 * @dev: network device
315 * @p: socket address
316 *
317 * Change hardware address of device.
318 *
319 * This doesn't change hardware matching, so needs to be overridden
320 * for most real devices.
321 */
322int eth_mac_addr(struct net_device *dev, void *p)
323{
324	int ret;
325
326	ret = eth_prepare_mac_addr_change(dev, p);
327	if (ret < 0)
328		return ret;
329	eth_commit_mac_addr_change(dev, p);
330	return 0;
331}
332EXPORT_SYMBOL(eth_mac_addr);
333
334int eth_validate_addr(struct net_device *dev)
335{
336	if (!is_valid_ether_addr(dev->dev_addr))
337		return -EADDRNOTAVAIL;
338
339	return 0;
340}
341EXPORT_SYMBOL(eth_validate_addr);
342
343const struct header_ops eth_header_ops ____cacheline_aligned = {
344	.create		= eth_header,
345	.parse		= eth_header_parse,
346	.cache		= eth_header_cache,
347	.cache_update	= eth_header_cache_update,
348	.parse_protocol	= eth_header_parse_protocol,
349};
350
351/**
352 * ether_setup - setup Ethernet network device
353 * @dev: network device
354 *
355 * Fill in the fields of the device structure with Ethernet-generic values.
356 */
357void ether_setup(struct net_device *dev)
358{
359	dev->header_ops		= &eth_header_ops;
360	dev->type		= ARPHRD_ETHER;
361	dev->hard_header_len 	= ETH_HLEN;
362	dev->min_header_len	= ETH_HLEN;
363	dev->mtu		= ETH_DATA_LEN;
364	dev->min_mtu		= ETH_MIN_MTU;
365	dev->max_mtu		= ETH_DATA_LEN;
366	dev->addr_len		= ETH_ALEN;
367	dev->tx_queue_len	= DEFAULT_TX_QUEUE_LEN;
368	dev->flags		= IFF_BROADCAST|IFF_MULTICAST;
369	dev->priv_flags		|= IFF_TX_SKB_SHARING;
370
371	eth_broadcast_addr(dev->broadcast);
372
373}
374EXPORT_SYMBOL(ether_setup);
375
376/**
377 * alloc_etherdev_mqs - Allocates and sets up an Ethernet device
378 * @sizeof_priv: Size of additional driver-private structure to be allocated
379 *	for this Ethernet device
380 * @txqs: The number of TX queues this device has.
381 * @rxqs: The number of RX queues this device has.
382 *
383 * Fill in the fields of the device structure with Ethernet-generic
384 * values. Basically does everything except registering the device.
385 *
386 * Constructs a new net device, complete with a private data area of
387 * size (sizeof_priv).  A 32-byte (not bit) alignment is enforced for
388 * this private data area.
389 */
390
391struct net_device *alloc_etherdev_mqs(int sizeof_priv, unsigned int txqs,
392				      unsigned int rxqs)
393{
394	return alloc_netdev_mqs(sizeof_priv, "eth%d", NET_NAME_ENUM,
395				ether_setup, txqs, rxqs);
396}
397EXPORT_SYMBOL(alloc_etherdev_mqs);
398
399ssize_t sysfs_format_mac(char *buf, const unsigned char *addr, int len)
400{
401	return sysfs_emit(buf, "%*phC\n", len, addr);
402}
403EXPORT_SYMBOL(sysfs_format_mac);
404
405struct sk_buff *eth_gro_receive(struct list_head *head, struct sk_buff *skb)
406{
407	const struct packet_offload *ptype;
408	unsigned int hlen, off_eth;
409	struct sk_buff *pp = NULL;
410	struct ethhdr *eh, *eh2;
411	struct sk_buff *p;
412	__be16 type;
413	int flush = 1;
414
415	off_eth = skb_gro_offset(skb);
416	hlen = off_eth + sizeof(*eh);
417	eh = skb_gro_header(skb, hlen, off_eth);
418	if (unlikely(!eh))
419		goto out;
420
421	flush = 0;
422
423	list_for_each_entry(p, head, list) {
424		if (!NAPI_GRO_CB(p)->same_flow)
425			continue;
426
427		eh2 = (struct ethhdr *)(p->data + off_eth);
428		if (compare_ether_header(eh, eh2)) {
429			NAPI_GRO_CB(p)->same_flow = 0;
430			continue;
431		}
432	}
433
434	type = eh->h_proto;
435
436	ptype = gro_find_receive_by_type(type);
437	if (ptype == NULL) {
438		flush = 1;
439		goto out;
440	}
441
442	skb_gro_pull(skb, sizeof(*eh));
443	skb_gro_postpull_rcsum(skb, eh, sizeof(*eh));
444
445	pp = indirect_call_gro_receive_inet(ptype->callbacks.gro_receive,
446					    ipv6_gro_receive, inet_gro_receive,
447					    head, skb);
448
449out:
450	skb_gro_flush_final(skb, pp, flush);
451
452	return pp;
453}
454EXPORT_SYMBOL(eth_gro_receive);
455
456int eth_gro_complete(struct sk_buff *skb, int nhoff)
457{
458	struct ethhdr *eh = (struct ethhdr *)(skb->data + nhoff);
459	__be16 type = eh->h_proto;
460	struct packet_offload *ptype;
461	int err = -ENOSYS;
462
463	if (skb->encapsulation)
464		skb_set_inner_mac_header(skb, nhoff);
465
466	ptype = gro_find_complete_by_type(type);
467	if (ptype != NULL)
468		err = INDIRECT_CALL_INET(ptype->callbacks.gro_complete,
469					 ipv6_gro_complete, inet_gro_complete,
470					 skb, nhoff + sizeof(*eh));
471
472	return err;
473}
474EXPORT_SYMBOL(eth_gro_complete);
475
476static struct packet_offload eth_packet_offload __read_mostly = {
477	.type = cpu_to_be16(ETH_P_TEB),
478	.priority = 10,
479	.callbacks = {
480		.gro_receive = eth_gro_receive,
481		.gro_complete = eth_gro_complete,
482	},
483};
484
485static int __init eth_offload_init(void)
486{
487	dev_add_offload(&eth_packet_offload);
488
489	return 0;
490}
491
492fs_initcall(eth_offload_init);
493
494unsigned char * __weak arch_get_platform_mac_address(void)
495{
496	return NULL;
497}
498
499int eth_platform_get_mac_address(struct device *dev, u8 *mac_addr)
500{
501	unsigned char *addr;
502	int ret;
503
504	ret = of_get_mac_address(dev->of_node, mac_addr);
505	if (!ret)
506		return 0;
507
508	addr = arch_get_platform_mac_address();
509	if (!addr)
510		return -ENODEV;
511
512	ether_addr_copy(mac_addr, addr);
513
514	return 0;
515}
516EXPORT_SYMBOL(eth_platform_get_mac_address);
517
518/**
519 * platform_get_ethdev_address - Set netdev's MAC address from a given device
520 * @dev:	Pointer to the device
521 * @netdev:	Pointer to netdev to write the address to
522 *
523 * Wrapper around eth_platform_get_mac_address() which writes the address
524 * directly to netdev->dev_addr.
525 */
526int platform_get_ethdev_address(struct device *dev, struct net_device *netdev)
527{
528	u8 addr[ETH_ALEN] __aligned(2);
529	int ret;
530
531	ret = eth_platform_get_mac_address(dev, addr);
532	if (!ret)
533		eth_hw_addr_set(netdev, addr);
534	return ret;
535}
536EXPORT_SYMBOL(platform_get_ethdev_address);
537
538/**
539 * nvmem_get_mac_address - Obtain the MAC address from an nvmem cell named
540 * 'mac-address' associated with given device.
541 *
542 * @dev:	Device with which the mac-address cell is associated.
543 * @addrbuf:	Buffer to which the MAC address will be copied on success.
544 *
545 * Returns 0 on success or a negative error number on failure.
546 */
547int nvmem_get_mac_address(struct device *dev, void *addrbuf)
548{
549	struct nvmem_cell *cell;
550	const void *mac;
551	size_t len;
552
553	cell = nvmem_cell_get(dev, "mac-address");
554	if (IS_ERR(cell))
555		return PTR_ERR(cell);
556
557	mac = nvmem_cell_read(cell, &len);
558	nvmem_cell_put(cell);
559
560	if (IS_ERR(mac))
561		return PTR_ERR(mac);
562
563	if (len != ETH_ALEN || !is_valid_ether_addr(mac)) {
564		kfree(mac);
565		return -EINVAL;
566	}
567
568	ether_addr_copy(addrbuf, mac);
569	kfree(mac);
570
571	return 0;
572}
573
574static int fwnode_get_mac_addr(struct fwnode_handle *fwnode,
575			       const char *name, char *addr)
576{
577	int ret;
578
579	ret = fwnode_property_read_u8_array(fwnode, name, addr, ETH_ALEN);
580	if (ret)
581		return ret;
582
583	if (!is_valid_ether_addr(addr))
584		return -EINVAL;
585	return 0;
586}
587
588/**
589 * fwnode_get_mac_address - Get the MAC from the firmware node
590 * @fwnode:	Pointer to the firmware node
591 * @addr:	Address of buffer to store the MAC in
592 *
593 * Search the firmware node for the best MAC address to use.  'mac-address' is
594 * checked first, because that is supposed to contain to "most recent" MAC
595 * address. If that isn't set, then 'local-mac-address' is checked next,
596 * because that is the default address.  If that isn't set, then the obsolete
597 * 'address' is checked, just in case we're using an old device tree.
598 *
599 * Note that the 'address' property is supposed to contain a virtual address of
600 * the register set, but some DTS files have redefined that property to be the
601 * MAC address.
602 *
603 * All-zero MAC addresses are rejected, because those could be properties that
604 * exist in the firmware tables, but were not updated by the firmware.  For
605 * example, the DTS could define 'mac-address' and 'local-mac-address', with
606 * zero MAC addresses.  Some older U-Boots only initialized 'local-mac-address'.
607 * In this case, the real MAC is in 'local-mac-address', and 'mac-address'
608 * exists but is all zeros.
609 */
610int fwnode_get_mac_address(struct fwnode_handle *fwnode, char *addr)
611{
612	if (!fwnode_get_mac_addr(fwnode, "mac-address", addr) ||
613	    !fwnode_get_mac_addr(fwnode, "local-mac-address", addr) ||
614	    !fwnode_get_mac_addr(fwnode, "address", addr))
615		return 0;
616
617	return -ENOENT;
618}
619EXPORT_SYMBOL(fwnode_get_mac_address);
620
621/**
622 * device_get_mac_address - Get the MAC for a given device
623 * @dev:	Pointer to the device
624 * @addr:	Address of buffer to store the MAC in
625 */
626int device_get_mac_address(struct device *dev, char *addr)
627{
628	return fwnode_get_mac_address(dev_fwnode(dev), addr);
629}
630EXPORT_SYMBOL(device_get_mac_address);
631
632/**
633 * device_get_ethdev_address - Set netdev's MAC address from a given device
634 * @dev:	Pointer to the device
635 * @netdev:	Pointer to netdev to write the address to
636 *
637 * Wrapper around device_get_mac_address() which writes the address
638 * directly to netdev->dev_addr.
639 */
640int device_get_ethdev_address(struct device *dev, struct net_device *netdev)
641{
642	u8 addr[ETH_ALEN];
643	int ret;
644
645	ret = device_get_mac_address(dev, addr);
646	if (!ret)
647		eth_hw_addr_set(netdev, addr);
648	return ret;
649}
650EXPORT_SYMBOL(device_get_ethdev_address);
651