/*1* INET An implementation of the TCP/IP protocol suite for the LINUX2* operating system. INET is implemented using the BSD Socket3* interface as the means of communication with the user level.4*5* Ethernet-type device handling.6*7* Version: @(#)eth.c 1.0.7 05/25/938*9* Authors: Ross Biro10* Fred N. van Kempen, <[email protected]>11* Mark Evans, <[email protected]>12* Florian La Roche, <[email protected]>13* Alan Cox, <[email protected]>14*15* Fixes:16* Mr Linux : Arp problems17* Alan Cox : Generic queue tidyup (very tiny here)18* Alan Cox : eth_header ntohs should be htons19* Alan Cox : eth_rebuild_header missing an htons and20* minor other things.21* Tegge : Arp bug fixes.22* Florian : Removed many unnecessary functions, code cleanup23* and changes for new arp and skbuff.24* Alan Cox : Redid header building to reflect new format.25* Alan Cox : ARP only when compiled with CONFIG_INET26* Greg Page : 802.2 and SNAP stuff.27* Alan Cox : MAC layer pointers/new format.28* Paul Gortmaker : eth_copy_and_sum shouldn't csum padding.29* Alan Cox : Protect against forwarding explosions with30* older network drivers and IFF_ALLMULTI.31* Christer Weinigel : Better rebuild header message.32* Andrew Morton : 26Feb01: kill ether_setup() - use netdev_boot_setup().33*34* This program is free software; you can redistribute it and/or35* modify it under the terms of the GNU General Public License36* as published by the Free Software Foundation; either version37* 2 of the License, or (at your option) any later version.38*/39#include <linux/module.h>40#include <linux/types.h>41#include <linux/kernel.h>42#include <linux/string.h>43#include <linux/mm.h>44#include <linux/socket.h>45#include <linux/in.h>46#include <linux/inet.h>47#include <linux/ip.h>48#include <linux/netdevice.h>49#include <linux/etherdevice.h>50#include <linux/skbuff.h>51#include <linux/errno.h>52#include <linux/init.h>53#include <linux/if_ether.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 <asm/uaccess.h>61#include <asm/system.h>6263__setup("ether=", netdev_boot_setup);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 len)81{82struct ethhdr *eth = (struct ethhdr *)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)) {107memset(eth->h_dest, 0, ETH_ALEN);108return ETH_HLEN;109}110111return -ETH_HLEN;112}113EXPORT_SYMBOL(eth_header);114115/**116* eth_rebuild_header- rebuild the Ethernet MAC header.117* @skb: socket buffer to update118*119* This is called after an ARP or IPV6 ndisc it's resolution on this120* sk_buff. We now let protocol (ARP) fill in the other fields.121*122* This routine CANNOT use cached dst->neigh!123* Really, it is used only when dst->neigh is wrong.124*/125int eth_rebuild_header(struct sk_buff *skb)126{127struct ethhdr *eth = (struct ethhdr *)skb->data;128struct net_device *dev = skb->dev;129130switch (eth->h_proto) {131#ifdef CONFIG_INET132case htons(ETH_P_IP):133return arp_find(eth->h_dest, skb);134#endif135default:136printk(KERN_DEBUG137"%s: unable to resolve type %X addresses.\n",138dev->name, ntohs(eth->h_proto));139140memcpy(eth->h_source, dev->dev_addr, ETH_ALEN);141break;142}143144return 0;145}146EXPORT_SYMBOL(eth_rebuild_header);147148/**149* eth_type_trans - determine the packet's protocol ID.150* @skb: received socket data151* @dev: receiving network device152*153* The rule here is that we154* assume 802.3 if the type field is short enough to be a length.155* This is normal practice and works for any 'now in use' protocol.156*/157__be16 eth_type_trans(struct sk_buff *skb, struct net_device *dev)158{159struct ethhdr *eth;160161skb->dev = dev;162skb_reset_mac_header(skb);163skb_pull_inline(skb, ETH_HLEN);164eth = eth_hdr(skb);165166if (unlikely(is_multicast_ether_addr(eth->h_dest))) {167if (!compare_ether_addr_64bits(eth->h_dest, dev->broadcast))168skb->pkt_type = PACKET_BROADCAST;169else170skb->pkt_type = PACKET_MULTICAST;171}172173/*174* This ALLMULTI check should be redundant by 1.4175* so don't forget to remove it.176*177* Seems, you forgot to remove it. All silly devices178* seems to set IFF_PROMISC.179*/180181else if (1 /*dev->flags&IFF_PROMISC */ ) {182if (unlikely(compare_ether_addr_64bits(eth->h_dest, dev->dev_addr)))183skb->pkt_type = PACKET_OTHERHOST;184}185186/*187* Some variants of DSA tagging don't have an ethertype field188* at all, so we check here whether one of those tagging189* variants has been configured on the receiving interface,190* and if so, set skb->protocol without looking at the packet.191*/192if (netdev_uses_dsa_tags(dev))193return htons(ETH_P_DSA);194if (netdev_uses_trailer_tags(dev))195return htons(ETH_P_TRAILER);196197if (ntohs(eth->h_proto) >= 1536)198return eth->h_proto;199200/*201* This is a magic hack to spot IPX packets. Older Novell breaks202* the protocol design and runs IPX over 802.3 without an 802.2 LLC203* layer. We look for FFFF which isn't a used 802.2 SSAP/DSAP. This204* won't work for fault tolerant netware but does for the rest.205*/206if (skb->len >= 2 && *(unsigned short *)(skb->data) == 0xFFFF)207return htons(ETH_P_802_3);208209/*210* Real 802.2 LLC211*/212return htons(ETH_P_802_2);213}214EXPORT_SYMBOL(eth_type_trans);215216/**217* eth_header_parse - extract hardware address from packet218* @skb: packet to extract header from219* @haddr: destination buffer220*/221int eth_header_parse(const struct sk_buff *skb, unsigned char *haddr)222{223const struct ethhdr *eth = eth_hdr(skb);224memcpy(haddr, eth->h_source, ETH_ALEN);225return ETH_ALEN;226}227EXPORT_SYMBOL(eth_header_parse);228229/**230* eth_header_cache - fill cache entry from neighbour231* @neigh: source neighbour232* @hh: destination cache entry233* Create an Ethernet header template from the neighbour.234*/235int eth_header_cache(const struct neighbour *neigh, struct hh_cache *hh)236{237__be16 type = hh->hh_type;238struct ethhdr *eth;239const struct net_device *dev = neigh->dev;240241eth = (struct ethhdr *)242(((u8 *) hh->hh_data) + (HH_DATA_OFF(sizeof(*eth))));243244if (type == htons(ETH_P_802_3))245return -1;246247eth->h_proto = type;248memcpy(eth->h_source, dev->dev_addr, ETH_ALEN);249memcpy(eth->h_dest, neigh->ha, ETH_ALEN);250hh->hh_len = ETH_HLEN;251return 0;252}253EXPORT_SYMBOL(eth_header_cache);254255/**256* eth_header_cache_update - update cache entry257* @hh: destination cache entry258* @dev: network device259* @haddr: new hardware address260*261* Called by Address Resolution module to notify changes in address.262*/263void eth_header_cache_update(struct hh_cache *hh,264const struct net_device *dev,265const unsigned char *haddr)266{267memcpy(((u8 *) hh->hh_data) + HH_DATA_OFF(sizeof(struct ethhdr)),268haddr, ETH_ALEN);269}270EXPORT_SYMBOL(eth_header_cache_update);271272/**273* eth_mac_addr - set new Ethernet hardware address274* @dev: network device275* @p: socket address276* Change hardware address of device.277*278* This doesn't change hardware matching, so needs to be overridden279* for most real devices.280*/281int eth_mac_addr(struct net_device *dev, void *p)282{283struct sockaddr *addr = p;284285if (netif_running(dev))286return -EBUSY;287if (!is_valid_ether_addr(addr->sa_data))288return -EADDRNOTAVAIL;289memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN);290return 0;291}292EXPORT_SYMBOL(eth_mac_addr);293294/**295* eth_change_mtu - set new MTU size296* @dev: network device297* @new_mtu: new Maximum Transfer Unit298*299* Allow changing MTU size. Needs to be overridden for devices300* supporting jumbo frames.301*/302int eth_change_mtu(struct net_device *dev, int new_mtu)303{304if (new_mtu < 68 || new_mtu > ETH_DATA_LEN)305return -EINVAL;306dev->mtu = new_mtu;307return 0;308}309EXPORT_SYMBOL(eth_change_mtu);310311int eth_validate_addr(struct net_device *dev)312{313if (!is_valid_ether_addr(dev->dev_addr))314return -EADDRNOTAVAIL;315316return 0;317}318EXPORT_SYMBOL(eth_validate_addr);319320const struct header_ops eth_header_ops ____cacheline_aligned = {321.create = eth_header,322.parse = eth_header_parse,323.rebuild = eth_rebuild_header,324.cache = eth_header_cache,325.cache_update = eth_header_cache_update,326};327328/**329* ether_setup - setup Ethernet network device330* @dev: network device331* Fill in the fields of the device structure with Ethernet-generic values.332*/333void ether_setup(struct net_device *dev)334{335dev->header_ops = ð_header_ops;336dev->type = ARPHRD_ETHER;337dev->hard_header_len = ETH_HLEN;338dev->mtu = ETH_DATA_LEN;339dev->addr_len = ETH_ALEN;340dev->tx_queue_len = 1000; /* Ethernet wants good queues */341dev->flags = IFF_BROADCAST|IFF_MULTICAST;342343memset(dev->broadcast, 0xFF, ETH_ALEN);344345}346EXPORT_SYMBOL(ether_setup);347348/**349* alloc_etherdev_mqs - Allocates and sets up an Ethernet device350* @sizeof_priv: Size of additional driver-private structure to be allocated351* for this Ethernet device352* @txqs: The number of TX queues this device has.353* @rxqs: The number of RX queues this device has.354*355* Fill in the fields of the device structure with Ethernet-generic356* values. Basically does everything except registering the device.357*358* Constructs a new net device, complete with a private data area of359* size (sizeof_priv). A 32-byte (not bit) alignment is enforced for360* this private data area.361*/362363struct net_device *alloc_etherdev_mqs(int sizeof_priv, unsigned int txqs,364unsigned int rxqs)365{366return alloc_netdev_mqs(sizeof_priv, "eth%d", ether_setup, txqs, rxqs);367}368EXPORT_SYMBOL(alloc_etherdev_mqs);369370static size_t _format_mac_addr(char *buf, int buflen,371const unsigned char *addr, int len)372{373int i;374char *cp = buf;375376for (i = 0; i < len; i++) {377cp += scnprintf(cp, buflen - (cp - buf), "%02x", addr[i]);378if (i == len - 1)379break;380cp += scnprintf(cp, buflen - (cp - buf), ":");381}382return cp - buf;383}384385ssize_t sysfs_format_mac(char *buf, const unsigned char *addr, int len)386{387size_t l;388389l = _format_mac_addr(buf, PAGE_SIZE, addr, len);390l += scnprintf(buf + l, PAGE_SIZE - l, "\n");391return (ssize_t)l;392}393EXPORT_SYMBOL(sysfs_format_mac);394395396