// SPDX-License-Identifier: GPL-2.0-or-later1/*2* INET An implementation of the TCP/IP protocol suite for the LINUX3* operating system. INET is implemented using the BSD Socket4* interface as the means of communication with the user level.5*6* Ethernet-type device handling.7*8* Version: @(#)eth.c 1.0.7 05/25/939*10* Authors: Ross Biro11* Fred N. van Kempen, <[email protected]>12* Mark Evans, <[email protected]>13* Florian La Roche, <[email protected]>14* Alan Cox, <[email protected]>15*16* Fixes:17* Mr Linux : Arp problems18* Alan Cox : Generic queue tidyup (very tiny here)19* Alan Cox : eth_header ntohs should be htons20* Alan Cox : eth_rebuild_header missing an htons and21* minor other things.22* Tegge : Arp bug fixes.23* Florian : Removed many unnecessary functions, code cleanup24* and changes for new arp and skbuff.25* Alan Cox : Redid header building to reflect new format.26* Alan Cox : ARP only when compiled with CONFIG_INET27* Greg Page : 802.2 and SNAP stuff.28* Alan Cox : MAC layer pointers/new format.29* Paul Gortmaker : eth_copy_and_sum shouldn't csum padding.30* Alan Cox : Protect against forwarding explosions with31* older network drivers and IFF_ALLMULTI.32* Christer Weinigel : Better rebuild header message.33* Andrew Morton : 26Feb01: kill ether_setup() - use netdev_boot_setup().34*/35#include <linux/module.h>36#include <linux/types.h>37#include <linux/kernel.h>38#include <linux/string.h>39#include <linux/mm.h>40#include <linux/socket.h>41#include <linux/in.h>42#include <linux/inet.h>43#include <linux/ip.h>44#include <linux/netdevice.h>45#include <linux/nvmem-consumer.h>46#include <linux/etherdevice.h>47#include <linux/skbuff.h>48#include <linux/errno.h>49#include <linux/init.h>50#include <linux/if_ether.h>51#include <linux/of_net.h>52#include <linux/pci.h>53#include <linux/property.h>54#include <net/dst.h>55#include <net/arp.h>56#include <net/sock.h>57#include <net/ipv6.h>58#include <net/ip.h>59#include <net/dsa.h>60#include <net/flow_dissector.h>61#include <net/gro.h>62#include <linux/uaccess.h>63#include <net/pkt_sched.h>6465/**66* eth_header - create the Ethernet header67* @skb: buffer to alter68* @dev: source device69* @type: Ethernet type field70* @daddr: destination address (NULL leave destination address)71* @saddr: source address (NULL use device source address)72* @len: packet length (<= skb->len)73*74*75* Set the protocol type. For a packet of type ETH_P_802_3/2 we put the length76* in here instead.77*/78int eth_header(struct sk_buff *skb, struct net_device *dev,79unsigned short type,80const void *daddr, const void *saddr, unsigned int len)81{82struct ethhdr *eth = skb_push(skb, ETH_HLEN);8384if (type != ETH_P_802_3 && type != ETH_P_802_2)85eth->h_proto = htons(type);86else87eth->h_proto = htons(len);8889/*90* Set the source hardware address.91*/9293if (!saddr)94saddr = dev->dev_addr;95memcpy(eth->h_source, saddr, ETH_ALEN);9697if (daddr) {98memcpy(eth->h_dest, daddr, ETH_ALEN);99return ETH_HLEN;100}101102/*103* Anyway, the loopback-device should never use this function...104*/105106if (dev->flags & (IFF_LOOPBACK | IFF_NOARP)) {107eth_zero_addr(eth->h_dest);108return ETH_HLEN;109}110111return -ETH_HLEN;112}113EXPORT_SYMBOL(eth_header);114115/**116* eth_get_headlen - determine the length of header for an ethernet frame117* @dev: pointer to network device118* @data: pointer to start of frame119* @len: total length of frame120*121* Make a best effort attempt to pull the length for all of the headers for122* a given frame in a linear buffer.123*/124u32 eth_get_headlen(const struct net_device *dev, const void *data, u32 len)125{126const unsigned int flags = FLOW_DISSECTOR_F_PARSE_1ST_FRAG;127const struct ethhdr *eth = (const struct ethhdr *)data;128struct flow_keys_basic keys;129130/* this should never happen, but better safe than sorry */131if (unlikely(len < sizeof(*eth)))132return len;133134/* parse any remaining L2/L3 headers, check for L4 */135if (!skb_flow_dissect_flow_keys_basic(dev_net(dev), NULL, &keys, data,136eth->h_proto, sizeof(*eth),137len, flags))138return max_t(u32, keys.control.thoff, sizeof(*eth));139140/* parse for any L4 headers */141return min_t(u32, __skb_get_poff(NULL, data, &keys, len), len);142}143EXPORT_SYMBOL(eth_get_headlen);144145/**146* eth_type_trans - determine the packet's protocol ID.147* @skb: received socket data148* @dev: receiving network device149*150* The rule here is that we151* assume 802.3 if the type field is short enough to be a length.152* This is normal practice and works for any 'now in use' protocol.153*/154__be16 eth_type_trans(struct sk_buff *skb, struct net_device *dev)155{156unsigned short _service_access_point;157const unsigned short *sap;158const struct ethhdr *eth;159160skb->dev = dev;161skb_reset_mac_header(skb);162163eth = eth_skb_pull_mac(skb);164eth_skb_pkt_type(skb, dev);165166/*167* Some variants of DSA tagging don't have an ethertype field168* at all, so we check here whether one of those tagging169* variants has been configured on the receiving interface,170* and if so, set skb->protocol without looking at the packet.171*/172if (unlikely(netdev_uses_dsa(dev)))173return htons(ETH_P_XDSA);174175if (likely(eth_proto_is_802_3(eth->h_proto)))176return eth->h_proto;177178/*179* This is a magic hack to spot IPX packets. Older Novell breaks180* the protocol design and runs IPX over 802.3 without an 802.2 LLC181* layer. We look for FFFF which isn't a used 802.2 SSAP/DSAP. This182* won't work for fault tolerant netware but does for the rest.183*/184sap = skb_header_pointer(skb, 0, sizeof(*sap), &_service_access_point);185if (sap && *sap == 0xFFFF)186return htons(ETH_P_802_3);187188/*189* Real 802.2 LLC190*/191return htons(ETH_P_802_2);192}193EXPORT_SYMBOL(eth_type_trans);194195/**196* eth_header_parse - extract hardware address from packet197* @skb: packet to extract header from198* @haddr: destination buffer199*/200int eth_header_parse(const struct sk_buff *skb, unsigned char *haddr)201{202const struct ethhdr *eth = eth_hdr(skb);203memcpy(haddr, eth->h_source, ETH_ALEN);204return ETH_ALEN;205}206EXPORT_SYMBOL(eth_header_parse);207208/**209* eth_header_cache - fill cache entry from neighbour210* @neigh: source neighbour211* @hh: destination cache entry212* @type: Ethernet type field213*214* Create an Ethernet header template from the neighbour.215*/216int eth_header_cache(const struct neighbour *neigh, struct hh_cache *hh, __be16 type)217{218struct ethhdr *eth;219const struct net_device *dev = neigh->dev;220221eth = (struct ethhdr *)222(((u8 *) hh->hh_data) + (HH_DATA_OFF(sizeof(*eth))));223224if (type == htons(ETH_P_802_3))225return -1;226227eth->h_proto = type;228memcpy(eth->h_source, dev->dev_addr, ETH_ALEN);229memcpy(eth->h_dest, neigh->ha, ETH_ALEN);230231/* Pairs with READ_ONCE() in neigh_resolve_output(),232* neigh_hh_output() and neigh_update_hhs().233*/234smp_store_release(&hh->hh_len, ETH_HLEN);235236return 0;237}238EXPORT_SYMBOL(eth_header_cache);239240/**241* eth_header_cache_update - update cache entry242* @hh: destination cache entry243* @dev: network device244* @haddr: new hardware address245*246* Called by Address Resolution module to notify changes in address.247*/248void eth_header_cache_update(struct hh_cache *hh,249const struct net_device *dev,250const unsigned char *haddr)251{252memcpy(((u8 *) hh->hh_data) + HH_DATA_OFF(sizeof(struct ethhdr)),253haddr, ETH_ALEN);254}255EXPORT_SYMBOL(eth_header_cache_update);256257/**258* eth_header_parse_protocol - extract protocol from L2 header259* @skb: packet to extract protocol from260*/261__be16 eth_header_parse_protocol(const struct sk_buff *skb)262{263const struct ethhdr *eth = eth_hdr(skb);264265return eth->h_proto;266}267EXPORT_SYMBOL(eth_header_parse_protocol);268269/**270* eth_prepare_mac_addr_change - prepare for mac change271* @dev: network device272* @p: socket address273*/274int eth_prepare_mac_addr_change(struct net_device *dev, void *p)275{276struct sockaddr *addr = p;277278if (!(dev->priv_flags & IFF_LIVE_ADDR_CHANGE) && netif_running(dev))279return -EBUSY;280if (!is_valid_ether_addr(addr->sa_data))281return -EADDRNOTAVAIL;282return 0;283}284EXPORT_SYMBOL(eth_prepare_mac_addr_change);285286/**287* eth_commit_mac_addr_change - commit mac change288* @dev: network device289* @p: socket address290*/291void eth_commit_mac_addr_change(struct net_device *dev, void *p)292{293struct sockaddr *addr = p;294295eth_hw_addr_set(dev, addr->sa_data);296}297EXPORT_SYMBOL(eth_commit_mac_addr_change);298299/**300* eth_mac_addr - set new Ethernet hardware address301* @dev: network device302* @p: socket address303*304* Change hardware address of device.305*306* This doesn't change hardware matching, so needs to be overridden307* for most real devices.308*/309int eth_mac_addr(struct net_device *dev, void *p)310{311int ret;312313ret = eth_prepare_mac_addr_change(dev, p);314if (ret < 0)315return ret;316eth_commit_mac_addr_change(dev, p);317return 0;318}319EXPORT_SYMBOL(eth_mac_addr);320321int eth_validate_addr(struct net_device *dev)322{323if (!is_valid_ether_addr(dev->dev_addr))324return -EADDRNOTAVAIL;325326return 0;327}328EXPORT_SYMBOL(eth_validate_addr);329330const struct header_ops eth_header_ops ____cacheline_aligned = {331.create = eth_header,332.parse = eth_header_parse,333.cache = eth_header_cache,334.cache_update = eth_header_cache_update,335.parse_protocol = eth_header_parse_protocol,336};337338/**339* ether_setup - setup Ethernet network device340* @dev: network device341*342* Fill in the fields of the device structure with Ethernet-generic values.343*/344void ether_setup(struct net_device *dev)345{346dev->header_ops = ð_header_ops;347dev->type = ARPHRD_ETHER;348dev->hard_header_len = ETH_HLEN;349dev->min_header_len = ETH_HLEN;350dev->mtu = ETH_DATA_LEN;351dev->min_mtu = ETH_MIN_MTU;352dev->max_mtu = ETH_DATA_LEN;353dev->addr_len = ETH_ALEN;354dev->tx_queue_len = DEFAULT_TX_QUEUE_LEN;355dev->flags = IFF_BROADCAST|IFF_MULTICAST;356dev->priv_flags |= IFF_TX_SKB_SHARING;357358eth_broadcast_addr(dev->broadcast);359360}361EXPORT_SYMBOL(ether_setup);362363/**364* alloc_etherdev_mqs - Allocates and sets up an Ethernet device365* @sizeof_priv: Size of additional driver-private structure to be allocated366* for this Ethernet device367* @txqs: The number of TX queues this device has.368* @rxqs: The number of RX queues this device has.369*370* Fill in the fields of the device structure with Ethernet-generic371* values. Basically does everything except registering the device.372*373* Constructs a new net device, complete with a private data area of374* size (sizeof_priv). A 32-byte (not bit) alignment is enforced for375* this private data area.376*/377378struct net_device *alloc_etherdev_mqs(int sizeof_priv, unsigned int txqs,379unsigned int rxqs)380{381return alloc_netdev_mqs(sizeof_priv, "eth%d", NET_NAME_ENUM,382ether_setup, txqs, rxqs);383}384EXPORT_SYMBOL(alloc_etherdev_mqs);385386ssize_t sysfs_format_mac(char *buf, const unsigned char *addr, int len)387{388return sysfs_emit(buf, "%*phC\n", len, addr);389}390EXPORT_SYMBOL(sysfs_format_mac);391392struct sk_buff *eth_gro_receive(struct list_head *head, struct sk_buff *skb)393{394const struct packet_offload *ptype;395unsigned int hlen, off_eth;396struct sk_buff *pp = NULL;397struct ethhdr *eh, *eh2;398struct sk_buff *p;399__be16 type;400int flush = 1;401402off_eth = skb_gro_offset(skb);403hlen = off_eth + sizeof(*eh);404eh = skb_gro_header(skb, hlen, off_eth);405if (unlikely(!eh))406goto out;407408flush = 0;409410list_for_each_entry(p, head, list) {411if (!NAPI_GRO_CB(p)->same_flow)412continue;413414eh2 = (struct ethhdr *)(p->data + off_eth);415if (compare_ether_header(eh, eh2)) {416NAPI_GRO_CB(p)->same_flow = 0;417continue;418}419}420421type = eh->h_proto;422423ptype = gro_find_receive_by_type(type);424if (ptype == NULL) {425flush = 1;426goto out;427}428429skb_gro_pull(skb, sizeof(*eh));430skb_gro_postpull_rcsum(skb, eh, sizeof(*eh));431432pp = indirect_call_gro_receive_inet(ptype->callbacks.gro_receive,433ipv6_gro_receive, inet_gro_receive,434head, skb);435436out:437skb_gro_flush_final(skb, pp, flush);438439return pp;440}441EXPORT_SYMBOL(eth_gro_receive);442443int eth_gro_complete(struct sk_buff *skb, int nhoff)444{445struct ethhdr *eh = (struct ethhdr *)(skb->data + nhoff);446__be16 type = eh->h_proto;447struct packet_offload *ptype;448int err = -ENOSYS;449450if (skb->encapsulation)451skb_set_inner_mac_header(skb, nhoff);452453ptype = gro_find_complete_by_type(type);454if (ptype != NULL)455err = INDIRECT_CALL_INET(ptype->callbacks.gro_complete,456ipv6_gro_complete, inet_gro_complete,457skb, nhoff + sizeof(*eh));458459return err;460}461EXPORT_SYMBOL(eth_gro_complete);462463static struct packet_offload eth_packet_offload __read_mostly = {464.type = cpu_to_be16(ETH_P_TEB),465.priority = 10,466.callbacks = {467.gro_receive = eth_gro_receive,468.gro_complete = eth_gro_complete,469},470};471472static int __init eth_offload_init(void)473{474dev_add_offload(ð_packet_offload);475476return 0;477}478479fs_initcall(eth_offload_init);480481unsigned char * __weak arch_get_platform_mac_address(void)482{483return NULL;484}485486int eth_platform_get_mac_address(struct device *dev, u8 *mac_addr)487{488unsigned char *addr;489int ret;490491ret = of_get_mac_address(dev->of_node, mac_addr);492if (!ret)493return 0;494495addr = arch_get_platform_mac_address();496if (!addr)497return -ENODEV;498499ether_addr_copy(mac_addr, addr);500501return 0;502}503EXPORT_SYMBOL(eth_platform_get_mac_address);504505/**506* platform_get_ethdev_address - Set netdev's MAC address from a given device507* @dev: Pointer to the device508* @netdev: Pointer to netdev to write the address to509*510* Wrapper around eth_platform_get_mac_address() which writes the address511* directly to netdev->dev_addr.512*/513int platform_get_ethdev_address(struct device *dev, struct net_device *netdev)514{515u8 addr[ETH_ALEN] __aligned(2);516int ret;517518ret = eth_platform_get_mac_address(dev, addr);519if (!ret)520eth_hw_addr_set(netdev, addr);521return ret;522}523EXPORT_SYMBOL(platform_get_ethdev_address);524525/**526* nvmem_get_mac_address - Obtain the MAC address from an nvmem cell named527* 'mac-address' associated with given device.528*529* @dev: Device with which the mac-address cell is associated.530* @addrbuf: Buffer to which the MAC address will be copied on success.531*532* Returns 0 on success or a negative error number on failure.533*/534int nvmem_get_mac_address(struct device *dev, void *addrbuf)535{536struct nvmem_cell *cell;537const void *mac;538size_t len;539540cell = nvmem_cell_get(dev, "mac-address");541if (IS_ERR(cell))542return PTR_ERR(cell);543544mac = nvmem_cell_read(cell, &len);545nvmem_cell_put(cell);546547if (IS_ERR(mac))548return PTR_ERR(mac);549550if (len != ETH_ALEN || !is_valid_ether_addr(mac)) {551kfree(mac);552return -EINVAL;553}554555ether_addr_copy(addrbuf, mac);556kfree(mac);557558return 0;559}560561static int fwnode_get_mac_addr(struct fwnode_handle *fwnode,562const char *name, char *addr)563{564int ret;565566ret = fwnode_property_read_u8_array(fwnode, name, addr, ETH_ALEN);567if (ret)568return ret;569570if (!is_valid_ether_addr(addr))571return -EINVAL;572return 0;573}574575/**576* fwnode_get_mac_address - Get the MAC from the firmware node577* @fwnode: Pointer to the firmware node578* @addr: Address of buffer to store the MAC in579*580* Search the firmware node for the best MAC address to use. 'mac-address' is581* checked first, because that is supposed to contain to "most recent" MAC582* address. If that isn't set, then 'local-mac-address' is checked next,583* because that is the default address. If that isn't set, then the obsolete584* 'address' is checked, just in case we're using an old device tree.585*586* Note that the 'address' property is supposed to contain a virtual address of587* the register set, but some DTS files have redefined that property to be the588* MAC address.589*590* All-zero MAC addresses are rejected, because those could be properties that591* exist in the firmware tables, but were not updated by the firmware. For592* example, the DTS could define 'mac-address' and 'local-mac-address', with593* zero MAC addresses. Some older U-Boots only initialized 'local-mac-address'.594* In this case, the real MAC is in 'local-mac-address', and 'mac-address'595* exists but is all zeros.596*/597int fwnode_get_mac_address(struct fwnode_handle *fwnode, char *addr)598{599if (!fwnode_get_mac_addr(fwnode, "mac-address", addr) ||600!fwnode_get_mac_addr(fwnode, "local-mac-address", addr) ||601!fwnode_get_mac_addr(fwnode, "address", addr))602return 0;603604return -ENOENT;605}606EXPORT_SYMBOL(fwnode_get_mac_address);607608/**609* device_get_mac_address - Get the MAC for a given device610* @dev: Pointer to the device611* @addr: Address of buffer to store the MAC in612*/613int device_get_mac_address(struct device *dev, char *addr)614{615return fwnode_get_mac_address(dev_fwnode(dev), addr);616}617EXPORT_SYMBOL(device_get_mac_address);618619/**620* device_get_ethdev_address - Set netdev's MAC address from a given device621* @dev: Pointer to the device622* @netdev: Pointer to netdev to write the address to623*624* Wrapper around device_get_mac_address() which writes the address625* directly to netdev->dev_addr.626*/627int device_get_ethdev_address(struct device *dev, struct net_device *netdev)628{629u8 addr[ETH_ALEN];630int ret;631632ret = device_get_mac_address(dev, addr);633if (!ret)634eth_hw_addr_set(netdev, addr);635return ret;636}637EXPORT_SYMBOL(device_get_ethdev_address);638639640