0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036 #include <linux/module.h>
0037 #include <linux/types.h>
0038 #include <linux/kernel.h>
0039 #include <linux/string.h>
0040 #include <linux/mm.h>
0041 #include <linux/socket.h>
0042 #include <linux/in.h>
0043 #include <linux/inet.h>
0044 #include <linux/ip.h>
0045 #include <linux/netdevice.h>
0046 #include <linux/nvmem-consumer.h>
0047 #include <linux/etherdevice.h>
0048 #include <linux/skbuff.h>
0049 #include <linux/errno.h>
0050 #include <linux/init.h>
0051 #include <linux/if_ether.h>
0052 #include <linux/of_net.h>
0053 #include <linux/pci.h>
0054 #include <linux/property.h>
0055 #include <net/dst.h>
0056 #include <net/arp.h>
0057 #include <net/sock.h>
0058 #include <net/ipv6.h>
0059 #include <net/ip.h>
0060 #include <net/dsa.h>
0061 #include <net/flow_dissector.h>
0062 #include <net/gro.h>
0063 #include <linux/uaccess.h>
0064 #include <net/pkt_sched.h>
0065
0066
0067
0068
0069
0070
0071
0072
0073
0074
0075
0076
0077
0078
0079 int eth_header(struct sk_buff *skb, struct net_device *dev,
0080 unsigned short type,
0081 const void *daddr, const void *saddr, unsigned int len)
0082 {
0083 struct ethhdr *eth = skb_push(skb, ETH_HLEN);
0084
0085 if (type != ETH_P_802_3 && type != ETH_P_802_2)
0086 eth->h_proto = htons(type);
0087 else
0088 eth->h_proto = htons(len);
0089
0090
0091
0092
0093
0094 if (!saddr)
0095 saddr = dev->dev_addr;
0096 memcpy(eth->h_source, saddr, ETH_ALEN);
0097
0098 if (daddr) {
0099 memcpy(eth->h_dest, daddr, ETH_ALEN);
0100 return ETH_HLEN;
0101 }
0102
0103
0104
0105
0106
0107 if (dev->flags & (IFF_LOOPBACK | IFF_NOARP)) {
0108 eth_zero_addr(eth->h_dest);
0109 return ETH_HLEN;
0110 }
0111
0112 return -ETH_HLEN;
0113 }
0114 EXPORT_SYMBOL(eth_header);
0115
0116
0117
0118
0119
0120
0121
0122
0123
0124
0125 u32 eth_get_headlen(const struct net_device *dev, const void *data, u32 len)
0126 {
0127 const unsigned int flags = FLOW_DISSECTOR_F_PARSE_1ST_FRAG;
0128 const struct ethhdr *eth = (const struct ethhdr *)data;
0129 struct flow_keys_basic keys;
0130
0131
0132 if (unlikely(len < sizeof(*eth)))
0133 return len;
0134
0135
0136 if (!skb_flow_dissect_flow_keys_basic(dev_net(dev), NULL, &keys, data,
0137 eth->h_proto, sizeof(*eth),
0138 len, flags))
0139 return max_t(u32, keys.control.thoff, sizeof(*eth));
0140
0141
0142 return min_t(u32, __skb_get_poff(NULL, data, &keys, len), len);
0143 }
0144 EXPORT_SYMBOL(eth_get_headlen);
0145
0146
0147
0148
0149
0150
0151
0152
0153
0154
0155 __be16 eth_type_trans(struct sk_buff *skb, struct net_device *dev)
0156 {
0157 unsigned short _service_access_point;
0158 const unsigned short *sap;
0159 const struct ethhdr *eth;
0160
0161 skb->dev = dev;
0162 skb_reset_mac_header(skb);
0163
0164 eth = (struct ethhdr *)skb->data;
0165 skb_pull_inline(skb, ETH_HLEN);
0166
0167 if (unlikely(!ether_addr_equal_64bits(eth->h_dest,
0168 dev->dev_addr))) {
0169 if (unlikely(is_multicast_ether_addr_64bits(eth->h_dest))) {
0170 if (ether_addr_equal_64bits(eth->h_dest, dev->broadcast))
0171 skb->pkt_type = PACKET_BROADCAST;
0172 else
0173 skb->pkt_type = PACKET_MULTICAST;
0174 } else {
0175 skb->pkt_type = PACKET_OTHERHOST;
0176 }
0177 }
0178
0179
0180
0181
0182
0183
0184
0185 if (unlikely(netdev_uses_dsa(dev)))
0186 return htons(ETH_P_XDSA);
0187
0188 if (likely(eth_proto_is_802_3(eth->h_proto)))
0189 return eth->h_proto;
0190
0191
0192
0193
0194
0195
0196
0197 sap = skb_header_pointer(skb, 0, sizeof(*sap), &_service_access_point);
0198 if (sap && *sap == 0xFFFF)
0199 return htons(ETH_P_802_3);
0200
0201
0202
0203
0204 return htons(ETH_P_802_2);
0205 }
0206 EXPORT_SYMBOL(eth_type_trans);
0207
0208
0209
0210
0211
0212
0213 int eth_header_parse(const struct sk_buff *skb, unsigned char *haddr)
0214 {
0215 const struct ethhdr *eth = eth_hdr(skb);
0216 memcpy(haddr, eth->h_source, ETH_ALEN);
0217 return ETH_ALEN;
0218 }
0219 EXPORT_SYMBOL(eth_header_parse);
0220
0221
0222
0223
0224
0225
0226
0227
0228
0229 int eth_header_cache(const struct neighbour *neigh, struct hh_cache *hh, __be16 type)
0230 {
0231 struct ethhdr *eth;
0232 const struct net_device *dev = neigh->dev;
0233
0234 eth = (struct ethhdr *)
0235 (((u8 *) hh->hh_data) + (HH_DATA_OFF(sizeof(*eth))));
0236
0237 if (type == htons(ETH_P_802_3))
0238 return -1;
0239
0240 eth->h_proto = type;
0241 memcpy(eth->h_source, dev->dev_addr, ETH_ALEN);
0242 memcpy(eth->h_dest, neigh->ha, ETH_ALEN);
0243
0244
0245
0246
0247 smp_store_release(&hh->hh_len, ETH_HLEN);
0248
0249 return 0;
0250 }
0251 EXPORT_SYMBOL(eth_header_cache);
0252
0253
0254
0255
0256
0257
0258
0259
0260
0261 void eth_header_cache_update(struct hh_cache *hh,
0262 const struct net_device *dev,
0263 const unsigned char *haddr)
0264 {
0265 memcpy(((u8 *) hh->hh_data) + HH_DATA_OFF(sizeof(struct ethhdr)),
0266 haddr, ETH_ALEN);
0267 }
0268 EXPORT_SYMBOL(eth_header_cache_update);
0269
0270
0271
0272
0273
0274 __be16 eth_header_parse_protocol(const struct sk_buff *skb)
0275 {
0276 const struct ethhdr *eth = eth_hdr(skb);
0277
0278 return eth->h_proto;
0279 }
0280 EXPORT_SYMBOL(eth_header_parse_protocol);
0281
0282
0283
0284
0285
0286
0287 int eth_prepare_mac_addr_change(struct net_device *dev, void *p)
0288 {
0289 struct sockaddr *addr = p;
0290
0291 if (!(dev->priv_flags & IFF_LIVE_ADDR_CHANGE) && netif_running(dev))
0292 return -EBUSY;
0293 if (!is_valid_ether_addr(addr->sa_data))
0294 return -EADDRNOTAVAIL;
0295 return 0;
0296 }
0297 EXPORT_SYMBOL(eth_prepare_mac_addr_change);
0298
0299
0300
0301
0302
0303
0304 void eth_commit_mac_addr_change(struct net_device *dev, void *p)
0305 {
0306 struct sockaddr *addr = p;
0307
0308 eth_hw_addr_set(dev, addr->sa_data);
0309 }
0310 EXPORT_SYMBOL(eth_commit_mac_addr_change);
0311
0312
0313
0314
0315
0316
0317
0318
0319
0320
0321
0322 int eth_mac_addr(struct net_device *dev, void *p)
0323 {
0324 int ret;
0325
0326 ret = eth_prepare_mac_addr_change(dev, p);
0327 if (ret < 0)
0328 return ret;
0329 eth_commit_mac_addr_change(dev, p);
0330 return 0;
0331 }
0332 EXPORT_SYMBOL(eth_mac_addr);
0333
0334 int eth_validate_addr(struct net_device *dev)
0335 {
0336 if (!is_valid_ether_addr(dev->dev_addr))
0337 return -EADDRNOTAVAIL;
0338
0339 return 0;
0340 }
0341 EXPORT_SYMBOL(eth_validate_addr);
0342
0343 const struct header_ops eth_header_ops ____cacheline_aligned = {
0344 .create = eth_header,
0345 .parse = eth_header_parse,
0346 .cache = eth_header_cache,
0347 .cache_update = eth_header_cache_update,
0348 .parse_protocol = eth_header_parse_protocol,
0349 };
0350
0351
0352
0353
0354
0355
0356
0357 void ether_setup(struct net_device *dev)
0358 {
0359 dev->header_ops = ð_header_ops;
0360 dev->type = ARPHRD_ETHER;
0361 dev->hard_header_len = ETH_HLEN;
0362 dev->min_header_len = ETH_HLEN;
0363 dev->mtu = ETH_DATA_LEN;
0364 dev->min_mtu = ETH_MIN_MTU;
0365 dev->max_mtu = ETH_DATA_LEN;
0366 dev->addr_len = ETH_ALEN;
0367 dev->tx_queue_len = DEFAULT_TX_QUEUE_LEN;
0368 dev->flags = IFF_BROADCAST|IFF_MULTICAST;
0369 dev->priv_flags |= IFF_TX_SKB_SHARING;
0370
0371 eth_broadcast_addr(dev->broadcast);
0372
0373 }
0374 EXPORT_SYMBOL(ether_setup);
0375
0376
0377
0378
0379
0380
0381
0382
0383
0384
0385
0386
0387
0388
0389
0390
0391 struct net_device *alloc_etherdev_mqs(int sizeof_priv, unsigned int txqs,
0392 unsigned int rxqs)
0393 {
0394 return alloc_netdev_mqs(sizeof_priv, "eth%d", NET_NAME_ENUM,
0395 ether_setup, txqs, rxqs);
0396 }
0397 EXPORT_SYMBOL(alloc_etherdev_mqs);
0398
0399 ssize_t sysfs_format_mac(char *buf, const unsigned char *addr, int len)
0400 {
0401 return scnprintf(buf, PAGE_SIZE, "%*phC\n", len, addr);
0402 }
0403 EXPORT_SYMBOL(sysfs_format_mac);
0404
0405 struct sk_buff *eth_gro_receive(struct list_head *head, struct sk_buff *skb)
0406 {
0407 const struct packet_offload *ptype;
0408 unsigned int hlen, off_eth;
0409 struct sk_buff *pp = NULL;
0410 struct ethhdr *eh, *eh2;
0411 struct sk_buff *p;
0412 __be16 type;
0413 int flush = 1;
0414
0415 off_eth = skb_gro_offset(skb);
0416 hlen = off_eth + sizeof(*eh);
0417 eh = skb_gro_header_fast(skb, off_eth);
0418 if (skb_gro_header_hard(skb, hlen)) {
0419 eh = skb_gro_header_slow(skb, hlen, off_eth);
0420 if (unlikely(!eh))
0421 goto out;
0422 }
0423
0424 flush = 0;
0425
0426 list_for_each_entry(p, head, list) {
0427 if (!NAPI_GRO_CB(p)->same_flow)
0428 continue;
0429
0430 eh2 = (struct ethhdr *)(p->data + off_eth);
0431 if (compare_ether_header(eh, eh2)) {
0432 NAPI_GRO_CB(p)->same_flow = 0;
0433 continue;
0434 }
0435 }
0436
0437 type = eh->h_proto;
0438
0439 ptype = gro_find_receive_by_type(type);
0440 if (ptype == NULL) {
0441 flush = 1;
0442 goto out;
0443 }
0444
0445 skb_gro_pull(skb, sizeof(*eh));
0446 skb_gro_postpull_rcsum(skb, eh, sizeof(*eh));
0447
0448 pp = indirect_call_gro_receive_inet(ptype->callbacks.gro_receive,
0449 ipv6_gro_receive, inet_gro_receive,
0450 head, skb);
0451
0452 out:
0453 skb_gro_flush_final(skb, pp, flush);
0454
0455 return pp;
0456 }
0457 EXPORT_SYMBOL(eth_gro_receive);
0458
0459 int eth_gro_complete(struct sk_buff *skb, int nhoff)
0460 {
0461 struct ethhdr *eh = (struct ethhdr *)(skb->data + nhoff);
0462 __be16 type = eh->h_proto;
0463 struct packet_offload *ptype;
0464 int err = -ENOSYS;
0465
0466 if (skb->encapsulation)
0467 skb_set_inner_mac_header(skb, nhoff);
0468
0469 ptype = gro_find_complete_by_type(type);
0470 if (ptype != NULL)
0471 err = INDIRECT_CALL_INET(ptype->callbacks.gro_complete,
0472 ipv6_gro_complete, inet_gro_complete,
0473 skb, nhoff + sizeof(*eh));
0474
0475 return err;
0476 }
0477 EXPORT_SYMBOL(eth_gro_complete);
0478
0479 static struct packet_offload eth_packet_offload __read_mostly = {
0480 .type = cpu_to_be16(ETH_P_TEB),
0481 .priority = 10,
0482 .callbacks = {
0483 .gro_receive = eth_gro_receive,
0484 .gro_complete = eth_gro_complete,
0485 },
0486 };
0487
0488 static int __init eth_offload_init(void)
0489 {
0490 dev_add_offload(ð_packet_offload);
0491
0492 return 0;
0493 }
0494
0495 fs_initcall(eth_offload_init);
0496
0497 unsigned char * __weak arch_get_platform_mac_address(void)
0498 {
0499 return NULL;
0500 }
0501
0502 int eth_platform_get_mac_address(struct device *dev, u8 *mac_addr)
0503 {
0504 unsigned char *addr;
0505 int ret;
0506
0507 ret = of_get_mac_address(dev->of_node, mac_addr);
0508 if (!ret)
0509 return 0;
0510
0511 addr = arch_get_platform_mac_address();
0512 if (!addr)
0513 return -ENODEV;
0514
0515 ether_addr_copy(mac_addr, addr);
0516
0517 return 0;
0518 }
0519 EXPORT_SYMBOL(eth_platform_get_mac_address);
0520
0521
0522
0523
0524
0525
0526
0527
0528
0529 int platform_get_ethdev_address(struct device *dev, struct net_device *netdev)
0530 {
0531 u8 addr[ETH_ALEN] __aligned(2);
0532 int ret;
0533
0534 ret = eth_platform_get_mac_address(dev, addr);
0535 if (!ret)
0536 eth_hw_addr_set(netdev, addr);
0537 return ret;
0538 }
0539 EXPORT_SYMBOL(platform_get_ethdev_address);
0540
0541
0542
0543
0544
0545
0546
0547
0548
0549
0550 int nvmem_get_mac_address(struct device *dev, void *addrbuf)
0551 {
0552 struct nvmem_cell *cell;
0553 const void *mac;
0554 size_t len;
0555
0556 cell = nvmem_cell_get(dev, "mac-address");
0557 if (IS_ERR(cell))
0558 return PTR_ERR(cell);
0559
0560 mac = nvmem_cell_read(cell, &len);
0561 nvmem_cell_put(cell);
0562
0563 if (IS_ERR(mac))
0564 return PTR_ERR(mac);
0565
0566 if (len != ETH_ALEN || !is_valid_ether_addr(mac)) {
0567 kfree(mac);
0568 return -EINVAL;
0569 }
0570
0571 ether_addr_copy(addrbuf, mac);
0572 kfree(mac);
0573
0574 return 0;
0575 }
0576
0577 static int fwnode_get_mac_addr(struct fwnode_handle *fwnode,
0578 const char *name, char *addr)
0579 {
0580 int ret;
0581
0582 ret = fwnode_property_read_u8_array(fwnode, name, addr, ETH_ALEN);
0583 if (ret)
0584 return ret;
0585
0586 if (!is_valid_ether_addr(addr))
0587 return -EINVAL;
0588 return 0;
0589 }
0590
0591
0592
0593
0594
0595
0596
0597
0598
0599
0600
0601
0602
0603
0604
0605
0606
0607
0608
0609
0610
0611
0612
0613 int fwnode_get_mac_address(struct fwnode_handle *fwnode, char *addr)
0614 {
0615 if (!fwnode_get_mac_addr(fwnode, "mac-address", addr) ||
0616 !fwnode_get_mac_addr(fwnode, "local-mac-address", addr) ||
0617 !fwnode_get_mac_addr(fwnode, "address", addr))
0618 return 0;
0619
0620 return -ENOENT;
0621 }
0622 EXPORT_SYMBOL(fwnode_get_mac_address);
0623
0624
0625
0626
0627
0628
0629 int device_get_mac_address(struct device *dev, char *addr)
0630 {
0631 return fwnode_get_mac_address(dev_fwnode(dev), addr);
0632 }
0633 EXPORT_SYMBOL(device_get_mac_address);
0634
0635
0636
0637
0638
0639
0640
0641
0642
0643 int device_get_ethdev_address(struct device *dev, struct net_device *netdev)
0644 {
0645 u8 addr[ETH_ALEN];
0646 int ret;
0647
0648 ret = device_get_mac_address(dev, addr);
0649 if (!ret)
0650 eth_hw_addr_set(netdev, addr);
0651 return ret;
0652 }
0653 EXPORT_SYMBOL(device_get_ethdev_address);