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