macvlan.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075
  1. /*
  2. * Copyright (c) 2007 Patrick McHardy <kaber@trash.net>
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of
  7. * the License, or (at your option) any later version.
  8. *
  9. * The code this is based on carried the following copyright notice:
  10. * ---
  11. * (C) Copyright 2001-2006
  12. * Alex Zeffertt, Cambridge Broadband Ltd, ajz@cambridgebroadband.com
  13. * Re-worked by Ben Greear <greearb@candelatech.com>
  14. * ---
  15. */
  16. #include <linux/kernel.h>
  17. #include <linux/types.h>
  18. #include <linux/module.h>
  19. #include <linux/init.h>
  20. #include <linux/errno.h>
  21. #include <linux/slab.h>
  22. #include <linux/string.h>
  23. #include <linux/rculist.h>
  24. #include <linux/notifier.h>
  25. #include <linux/netdevice.h>
  26. #include <linux/etherdevice.h>
  27. #include <linux/ethtool.h>
  28. #include <linux/if_arp.h>
  29. #include <linux/if_vlan.h>
  30. #include <linux/if_link.h>
  31. #include <linux/if_macvlan.h>
  32. #include <linux/hash.h>
  33. #include <net/rtnetlink.h>
  34. #include <net/xfrm.h>
  35. #define MACVLAN_HASH_SIZE (1 << BITS_PER_BYTE)
  36. struct macvlan_port {
  37. struct net_device *dev;
  38. struct hlist_head vlan_hash[MACVLAN_HASH_SIZE];
  39. struct list_head vlans;
  40. struct rcu_head rcu;
  41. bool passthru;
  42. int count;
  43. };
  44. static void macvlan_port_destroy(struct net_device *dev);
  45. static struct macvlan_port *macvlan_port_get_rcu(const struct net_device *dev)
  46. {
  47. return rcu_dereference(dev->rx_handler_data);
  48. }
  49. static struct macvlan_port *macvlan_port_get_rtnl(const struct net_device *dev)
  50. {
  51. return rtnl_dereference(dev->rx_handler_data);
  52. }
  53. #define macvlan_port_exists(dev) (dev->priv_flags & IFF_MACVLAN_PORT)
  54. static struct macvlan_dev *macvlan_hash_lookup(const struct macvlan_port *port,
  55. const unsigned char *addr)
  56. {
  57. struct macvlan_dev *vlan;
  58. hlist_for_each_entry_rcu(vlan, &port->vlan_hash[addr[5]], hlist) {
  59. if (ether_addr_equal_64bits(vlan->dev->dev_addr, addr))
  60. return vlan;
  61. }
  62. return NULL;
  63. }
  64. static void macvlan_hash_add(struct macvlan_dev *vlan)
  65. {
  66. struct macvlan_port *port = vlan->port;
  67. const unsigned char *addr = vlan->dev->dev_addr;
  68. hlist_add_head_rcu(&vlan->hlist, &port->vlan_hash[addr[5]]);
  69. }
  70. static void macvlan_hash_del(struct macvlan_dev *vlan, bool sync)
  71. {
  72. hlist_del_rcu(&vlan->hlist);
  73. if (sync)
  74. synchronize_rcu();
  75. }
  76. static void macvlan_hash_change_addr(struct macvlan_dev *vlan,
  77. const unsigned char *addr)
  78. {
  79. macvlan_hash_del(vlan, true);
  80. /* Now that we are unhashed it is safe to change the device
  81. * address without confusing packet delivery.
  82. */
  83. memcpy(vlan->dev->dev_addr, addr, ETH_ALEN);
  84. macvlan_hash_add(vlan);
  85. }
  86. static int macvlan_addr_busy(const struct macvlan_port *port,
  87. const unsigned char *addr)
  88. {
  89. /* Test to see if the specified multicast address is
  90. * currently in use by the underlying device or
  91. * another macvlan.
  92. */
  93. if (ether_addr_equal_64bits(port->dev->dev_addr, addr))
  94. return 1;
  95. if (macvlan_hash_lookup(port, addr))
  96. return 1;
  97. return 0;
  98. }
  99. static int macvlan_broadcast_one(struct sk_buff *skb,
  100. const struct macvlan_dev *vlan,
  101. const struct ethhdr *eth, bool local)
  102. {
  103. struct net_device *dev = vlan->dev;
  104. if (local)
  105. return vlan->forward(dev, skb);
  106. skb->dev = dev;
  107. if (ether_addr_equal_64bits(eth->h_dest, dev->broadcast))
  108. skb->pkt_type = PACKET_BROADCAST;
  109. else
  110. skb->pkt_type = PACKET_MULTICAST;
  111. return vlan->receive(skb);
  112. }
  113. static u32 macvlan_hash_mix(const struct macvlan_dev *vlan)
  114. {
  115. return (u32)(((unsigned long)vlan) >> L1_CACHE_SHIFT);
  116. }
  117. static unsigned int mc_hash(const struct macvlan_dev *vlan,
  118. const unsigned char *addr)
  119. {
  120. u32 val = __get_unaligned_cpu32(addr + 2);
  121. val ^= macvlan_hash_mix(vlan);
  122. return hash_32(val, MACVLAN_MC_FILTER_BITS);
  123. }
  124. static void macvlan_broadcast(struct sk_buff *skb,
  125. const struct macvlan_port *port,
  126. struct net_device *src,
  127. enum macvlan_mode mode)
  128. {
  129. const struct ethhdr *eth = eth_hdr(skb);
  130. const struct macvlan_dev *vlan;
  131. struct sk_buff *nskb;
  132. unsigned int i;
  133. int err;
  134. unsigned int hash;
  135. if (skb->protocol == htons(ETH_P_PAUSE))
  136. return;
  137. for (i = 0; i < MACVLAN_HASH_SIZE; i++) {
  138. hlist_for_each_entry_rcu(vlan, &port->vlan_hash[i], hlist) {
  139. if (vlan->dev == src || !(vlan->mode & mode))
  140. continue;
  141. hash = mc_hash(vlan, eth->h_dest);
  142. if (!test_bit(hash, vlan->mc_filter))
  143. continue;
  144. err = NET_RX_DROP;
  145. nskb = skb_clone(skb, GFP_ATOMIC);
  146. if (likely(nskb))
  147. err = macvlan_broadcast_one(
  148. nskb, vlan, eth,
  149. mode == MACVLAN_MODE_BRIDGE);
  150. macvlan_count_rx(vlan, skb->len + ETH_HLEN,
  151. err == NET_RX_SUCCESS, 1);
  152. }
  153. }
  154. }
  155. /* called under rcu_read_lock() from netif_receive_skb */
  156. static rx_handler_result_t macvlan_handle_frame(struct sk_buff **pskb)
  157. {
  158. struct macvlan_port *port;
  159. struct sk_buff *skb = *pskb;
  160. const struct ethhdr *eth = eth_hdr(skb);
  161. const struct macvlan_dev *vlan;
  162. const struct macvlan_dev *src;
  163. struct net_device *dev;
  164. unsigned int len = 0;
  165. int ret = NET_RX_DROP;
  166. port = macvlan_port_get_rcu(skb->dev);
  167. if (is_multicast_ether_addr(eth->h_dest)) {
  168. skb = ip_check_defrag(skb, IP_DEFRAG_MACVLAN);
  169. if (!skb)
  170. return RX_HANDLER_CONSUMED;
  171. eth = eth_hdr(skb);
  172. src = macvlan_hash_lookup(port, eth->h_source);
  173. if (!src)
  174. /* frame comes from an external address */
  175. macvlan_broadcast(skb, port, NULL,
  176. MACVLAN_MODE_PRIVATE |
  177. MACVLAN_MODE_VEPA |
  178. MACVLAN_MODE_PASSTHRU|
  179. MACVLAN_MODE_BRIDGE);
  180. else if (src->mode == MACVLAN_MODE_VEPA)
  181. /* flood to everyone except source */
  182. macvlan_broadcast(skb, port, src->dev,
  183. MACVLAN_MODE_VEPA |
  184. MACVLAN_MODE_BRIDGE);
  185. else if (src->mode == MACVLAN_MODE_BRIDGE)
  186. /*
  187. * flood only to VEPA ports, bridge ports
  188. * already saw the frame on the way out.
  189. */
  190. macvlan_broadcast(skb, port, src->dev,
  191. MACVLAN_MODE_VEPA);
  192. else {
  193. /* forward to original port. */
  194. vlan = src;
  195. ret = macvlan_broadcast_one(skb, vlan, eth, 0);
  196. goto out;
  197. }
  198. return RX_HANDLER_PASS;
  199. }
  200. if (port->passthru)
  201. vlan = list_first_or_null_rcu(&port->vlans,
  202. struct macvlan_dev, list);
  203. else
  204. vlan = macvlan_hash_lookup(port, eth->h_dest);
  205. if (vlan == NULL)
  206. return RX_HANDLER_PASS;
  207. dev = vlan->dev;
  208. if (unlikely(!(dev->flags & IFF_UP))) {
  209. kfree_skb(skb);
  210. return RX_HANDLER_CONSUMED;
  211. }
  212. len = skb->len + ETH_HLEN;
  213. skb = skb_share_check(skb, GFP_ATOMIC);
  214. if (!skb)
  215. goto out;
  216. skb->dev = dev;
  217. skb->pkt_type = PACKET_HOST;
  218. ret = vlan->receive(skb);
  219. out:
  220. macvlan_count_rx(vlan, len, ret == NET_RX_SUCCESS, 0);
  221. return RX_HANDLER_CONSUMED;
  222. }
  223. static int macvlan_queue_xmit(struct sk_buff *skb, struct net_device *dev)
  224. {
  225. const struct macvlan_dev *vlan = netdev_priv(dev);
  226. const struct macvlan_port *port = vlan->port;
  227. const struct macvlan_dev *dest;
  228. __u8 ip_summed = skb->ip_summed;
  229. if (vlan->mode == MACVLAN_MODE_BRIDGE) {
  230. const struct ethhdr *eth = (void *)skb->data;
  231. skb->ip_summed = CHECKSUM_UNNECESSARY;
  232. /* send to other bridge ports directly */
  233. if (is_multicast_ether_addr(eth->h_dest)) {
  234. macvlan_broadcast(skb, port, dev, MACVLAN_MODE_BRIDGE);
  235. goto xmit_world;
  236. }
  237. dest = macvlan_hash_lookup(port, eth->h_dest);
  238. if (dest && dest->mode == MACVLAN_MODE_BRIDGE) {
  239. /* send to lowerdev first for its network taps */
  240. dev_forward_skb(vlan->lowerdev, skb);
  241. return NET_XMIT_SUCCESS;
  242. }
  243. }
  244. xmit_world:
  245. skb->ip_summed = ip_summed;
  246. skb->dev = vlan->lowerdev;
  247. return dev_queue_xmit(skb);
  248. }
  249. netdev_tx_t macvlan_start_xmit(struct sk_buff *skb,
  250. struct net_device *dev)
  251. {
  252. unsigned int len = skb->len;
  253. int ret;
  254. const struct macvlan_dev *vlan = netdev_priv(dev);
  255. if (vlan->fwd_priv) {
  256. skb->dev = vlan->lowerdev;
  257. ret = dev_hard_start_xmit(skb, skb->dev, NULL, vlan->fwd_priv);
  258. } else {
  259. ret = macvlan_queue_xmit(skb, dev);
  260. }
  261. if (likely(ret == NET_XMIT_SUCCESS || ret == NET_XMIT_CN)) {
  262. struct macvlan_pcpu_stats *pcpu_stats;
  263. pcpu_stats = this_cpu_ptr(vlan->pcpu_stats);
  264. u64_stats_update_begin(&pcpu_stats->syncp);
  265. pcpu_stats->tx_packets++;
  266. pcpu_stats->tx_bytes += len;
  267. u64_stats_update_end(&pcpu_stats->syncp);
  268. } else {
  269. this_cpu_inc(vlan->pcpu_stats->tx_dropped);
  270. }
  271. return ret;
  272. }
  273. EXPORT_SYMBOL_GPL(macvlan_start_xmit);
  274. static int macvlan_hard_header(struct sk_buff *skb, struct net_device *dev,
  275. unsigned short type, const void *daddr,
  276. const void *saddr, unsigned len)
  277. {
  278. const struct macvlan_dev *vlan = netdev_priv(dev);
  279. struct net_device *lowerdev = vlan->lowerdev;
  280. return dev_hard_header(skb, lowerdev, type, daddr,
  281. saddr ? : dev->dev_addr, len);
  282. }
  283. static const struct header_ops macvlan_hard_header_ops = {
  284. .create = macvlan_hard_header,
  285. .rebuild = eth_rebuild_header,
  286. .parse = eth_header_parse,
  287. .cache = eth_header_cache,
  288. .cache_update = eth_header_cache_update,
  289. };
  290. static int macvlan_open(struct net_device *dev)
  291. {
  292. struct macvlan_dev *vlan = netdev_priv(dev);
  293. struct net_device *lowerdev = vlan->lowerdev;
  294. int err;
  295. if (vlan->port->passthru) {
  296. if (!(vlan->flags & MACVLAN_FLAG_NOPROMISC)) {
  297. err = dev_set_promiscuity(lowerdev, 1);
  298. if (err < 0)
  299. goto out;
  300. }
  301. goto hash_add;
  302. }
  303. if (lowerdev->features & NETIF_F_HW_L2FW_DOFFLOAD) {
  304. vlan->fwd_priv =
  305. lowerdev->netdev_ops->ndo_dfwd_add_station(lowerdev, dev);
  306. /* If we get a NULL pointer back, or if we get an error
  307. * then we should just fall through to the non accelerated path
  308. */
  309. if (IS_ERR_OR_NULL(vlan->fwd_priv)) {
  310. vlan->fwd_priv = NULL;
  311. } else {
  312. dev->features &= ~NETIF_F_LLTX;
  313. return 0;
  314. }
  315. }
  316. err = -EBUSY;
  317. if (macvlan_addr_busy(vlan->port, dev->dev_addr))
  318. goto out;
  319. err = dev_uc_add(lowerdev, dev->dev_addr);
  320. if (err < 0)
  321. goto out;
  322. if (dev->flags & IFF_ALLMULTI) {
  323. err = dev_set_allmulti(lowerdev, 1);
  324. if (err < 0)
  325. goto del_unicast;
  326. }
  327. hash_add:
  328. macvlan_hash_add(vlan);
  329. return 0;
  330. del_unicast:
  331. dev_uc_del(lowerdev, dev->dev_addr);
  332. out:
  333. if (vlan->fwd_priv) {
  334. lowerdev->netdev_ops->ndo_dfwd_del_station(lowerdev,
  335. vlan->fwd_priv);
  336. vlan->fwd_priv = NULL;
  337. }
  338. return err;
  339. }
  340. static int macvlan_stop(struct net_device *dev)
  341. {
  342. struct macvlan_dev *vlan = netdev_priv(dev);
  343. struct net_device *lowerdev = vlan->lowerdev;
  344. if (vlan->fwd_priv) {
  345. lowerdev->netdev_ops->ndo_dfwd_del_station(lowerdev,
  346. vlan->fwd_priv);
  347. vlan->fwd_priv = NULL;
  348. return 0;
  349. }
  350. dev_uc_unsync(lowerdev, dev);
  351. dev_mc_unsync(lowerdev, dev);
  352. if (vlan->port->passthru) {
  353. if (!(vlan->flags & MACVLAN_FLAG_NOPROMISC))
  354. dev_set_promiscuity(lowerdev, -1);
  355. goto hash_del;
  356. }
  357. if (dev->flags & IFF_ALLMULTI)
  358. dev_set_allmulti(lowerdev, -1);
  359. dev_uc_del(lowerdev, dev->dev_addr);
  360. hash_del:
  361. macvlan_hash_del(vlan, !dev->dismantle);
  362. return 0;
  363. }
  364. static int macvlan_set_mac_address(struct net_device *dev, void *p)
  365. {
  366. struct macvlan_dev *vlan = netdev_priv(dev);
  367. struct net_device *lowerdev = vlan->lowerdev;
  368. struct sockaddr *addr = p;
  369. int err;
  370. if (!is_valid_ether_addr(addr->sa_data))
  371. return -EADDRNOTAVAIL;
  372. if (!(dev->flags & IFF_UP)) {
  373. /* Just copy in the new address */
  374. memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN);
  375. } else {
  376. /* Rehash and update the device filters */
  377. if (macvlan_addr_busy(vlan->port, addr->sa_data))
  378. return -EBUSY;
  379. err = dev_uc_add(lowerdev, addr->sa_data);
  380. if (err)
  381. return err;
  382. dev_uc_del(lowerdev, dev->dev_addr);
  383. macvlan_hash_change_addr(vlan, addr->sa_data);
  384. }
  385. return 0;
  386. }
  387. static void macvlan_change_rx_flags(struct net_device *dev, int change)
  388. {
  389. struct macvlan_dev *vlan = netdev_priv(dev);
  390. struct net_device *lowerdev = vlan->lowerdev;
  391. if (change & IFF_ALLMULTI)
  392. dev_set_allmulti(lowerdev, dev->flags & IFF_ALLMULTI ? 1 : -1);
  393. }
  394. static void macvlan_set_mac_lists(struct net_device *dev)
  395. {
  396. struct macvlan_dev *vlan = netdev_priv(dev);
  397. if (dev->flags & (IFF_PROMISC | IFF_ALLMULTI)) {
  398. bitmap_fill(vlan->mc_filter, MACVLAN_MC_FILTER_SZ);
  399. } else {
  400. struct netdev_hw_addr *ha;
  401. DECLARE_BITMAP(filter, MACVLAN_MC_FILTER_SZ);
  402. bitmap_zero(filter, MACVLAN_MC_FILTER_SZ);
  403. netdev_for_each_mc_addr(ha, dev) {
  404. __set_bit(mc_hash(vlan, ha->addr), filter);
  405. }
  406. __set_bit(mc_hash(vlan, dev->broadcast), filter);
  407. bitmap_copy(vlan->mc_filter, filter, MACVLAN_MC_FILTER_SZ);
  408. }
  409. dev_uc_sync(vlan->lowerdev, dev);
  410. dev_mc_sync(vlan->lowerdev, dev);
  411. }
  412. static int macvlan_change_mtu(struct net_device *dev, int new_mtu)
  413. {
  414. struct macvlan_dev *vlan = netdev_priv(dev);
  415. if (new_mtu < 68 || vlan->lowerdev->mtu < new_mtu)
  416. return -EINVAL;
  417. dev->mtu = new_mtu;
  418. return 0;
  419. }
  420. /*
  421. * macvlan network devices have devices nesting below it and are a special
  422. * "super class" of normal network devices; split their locks off into a
  423. * separate class since they always nest.
  424. */
  425. static struct lock_class_key macvlan_netdev_xmit_lock_key;
  426. static struct lock_class_key macvlan_netdev_addr_lock_key;
  427. #define MACVLAN_FEATURES \
  428. (NETIF_F_SG | NETIF_F_ALL_CSUM | NETIF_F_HIGHDMA | NETIF_F_FRAGLIST | \
  429. NETIF_F_GSO | NETIF_F_TSO | NETIF_F_UFO | NETIF_F_GSO_ROBUST | \
  430. NETIF_F_TSO_ECN | NETIF_F_TSO6 | NETIF_F_GRO | NETIF_F_RXCSUM | \
  431. NETIF_F_HW_VLAN_CTAG_FILTER | NETIF_F_HW_VLAN_STAG_FILTER)
  432. #define MACVLAN_STATE_MASK \
  433. ((1<<__LINK_STATE_NOCARRIER) | (1<<__LINK_STATE_DORMANT))
  434. static void macvlan_set_lockdep_class_one(struct net_device *dev,
  435. struct netdev_queue *txq,
  436. void *_unused)
  437. {
  438. lockdep_set_class(&txq->_xmit_lock,
  439. &macvlan_netdev_xmit_lock_key);
  440. }
  441. static void macvlan_set_lockdep_class(struct net_device *dev)
  442. {
  443. lockdep_set_class(&dev->addr_list_lock,
  444. &macvlan_netdev_addr_lock_key);
  445. netdev_for_each_tx_queue(dev, macvlan_set_lockdep_class_one, NULL);
  446. }
  447. static int macvlan_init(struct net_device *dev)
  448. {
  449. struct macvlan_dev *vlan = netdev_priv(dev);
  450. const struct net_device *lowerdev = vlan->lowerdev;
  451. int i;
  452. dev->state = (dev->state & ~MACVLAN_STATE_MASK) |
  453. (lowerdev->state & MACVLAN_STATE_MASK);
  454. dev->features = lowerdev->features & MACVLAN_FEATURES;
  455. dev->features |= NETIF_F_LLTX;
  456. dev->gso_max_size = lowerdev->gso_max_size;
  457. dev->iflink = lowerdev->ifindex;
  458. dev->hard_header_len = lowerdev->hard_header_len;
  459. macvlan_set_lockdep_class(dev);
  460. vlan->pcpu_stats = alloc_percpu(struct macvlan_pcpu_stats);
  461. if (!vlan->pcpu_stats)
  462. return -ENOMEM;
  463. for_each_possible_cpu(i) {
  464. struct macvlan_pcpu_stats *mvlstats;
  465. mvlstats = per_cpu_ptr(vlan->pcpu_stats, i);
  466. u64_stats_init(&mvlstats->syncp);
  467. }
  468. return 0;
  469. }
  470. static void macvlan_uninit(struct net_device *dev)
  471. {
  472. struct macvlan_dev *vlan = netdev_priv(dev);
  473. struct macvlan_port *port = vlan->port;
  474. free_percpu(vlan->pcpu_stats);
  475. port->count -= 1;
  476. if (!port->count)
  477. macvlan_port_destroy(port->dev);
  478. }
  479. static struct rtnl_link_stats64 *macvlan_dev_get_stats64(struct net_device *dev,
  480. struct rtnl_link_stats64 *stats)
  481. {
  482. struct macvlan_dev *vlan = netdev_priv(dev);
  483. if (vlan->pcpu_stats) {
  484. struct macvlan_pcpu_stats *p;
  485. u64 rx_packets, rx_bytes, rx_multicast, tx_packets, tx_bytes;
  486. u32 rx_errors = 0, tx_dropped = 0;
  487. unsigned int start;
  488. int i;
  489. for_each_possible_cpu(i) {
  490. p = per_cpu_ptr(vlan->pcpu_stats, i);
  491. do {
  492. start = u64_stats_fetch_begin_bh(&p->syncp);
  493. rx_packets = p->rx_packets;
  494. rx_bytes = p->rx_bytes;
  495. rx_multicast = p->rx_multicast;
  496. tx_packets = p->tx_packets;
  497. tx_bytes = p->tx_bytes;
  498. } while (u64_stats_fetch_retry_bh(&p->syncp, start));
  499. stats->rx_packets += rx_packets;
  500. stats->rx_bytes += rx_bytes;
  501. stats->multicast += rx_multicast;
  502. stats->tx_packets += tx_packets;
  503. stats->tx_bytes += tx_bytes;
  504. /* rx_errors & tx_dropped are u32, updated
  505. * without syncp protection.
  506. */
  507. rx_errors += p->rx_errors;
  508. tx_dropped += p->tx_dropped;
  509. }
  510. stats->rx_errors = rx_errors;
  511. stats->rx_dropped = rx_errors;
  512. stats->tx_dropped = tx_dropped;
  513. }
  514. return stats;
  515. }
  516. static int macvlan_vlan_rx_add_vid(struct net_device *dev,
  517. __be16 proto, u16 vid)
  518. {
  519. struct macvlan_dev *vlan = netdev_priv(dev);
  520. struct net_device *lowerdev = vlan->lowerdev;
  521. return vlan_vid_add(lowerdev, proto, vid);
  522. }
  523. static int macvlan_vlan_rx_kill_vid(struct net_device *dev,
  524. __be16 proto, u16 vid)
  525. {
  526. struct macvlan_dev *vlan = netdev_priv(dev);
  527. struct net_device *lowerdev = vlan->lowerdev;
  528. vlan_vid_del(lowerdev, proto, vid);
  529. return 0;
  530. }
  531. static int macvlan_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
  532. struct net_device *dev,
  533. const unsigned char *addr,
  534. u16 flags)
  535. {
  536. struct macvlan_dev *vlan = netdev_priv(dev);
  537. int err = -EINVAL;
  538. if (!vlan->port->passthru)
  539. return -EOPNOTSUPP;
  540. if (flags & NLM_F_REPLACE)
  541. return -EOPNOTSUPP;
  542. if (is_unicast_ether_addr(addr))
  543. err = dev_uc_add_excl(dev, addr);
  544. else if (is_multicast_ether_addr(addr))
  545. err = dev_mc_add_excl(dev, addr);
  546. return err;
  547. }
  548. static int macvlan_fdb_del(struct ndmsg *ndm, struct nlattr *tb[],
  549. struct net_device *dev,
  550. const unsigned char *addr)
  551. {
  552. struct macvlan_dev *vlan = netdev_priv(dev);
  553. int err = -EINVAL;
  554. if (!vlan->port->passthru)
  555. return -EOPNOTSUPP;
  556. if (is_unicast_ether_addr(addr))
  557. err = dev_uc_del(dev, addr);
  558. else if (is_multicast_ether_addr(addr))
  559. err = dev_mc_del(dev, addr);
  560. return err;
  561. }
  562. static void macvlan_ethtool_get_drvinfo(struct net_device *dev,
  563. struct ethtool_drvinfo *drvinfo)
  564. {
  565. strlcpy(drvinfo->driver, "macvlan", sizeof(drvinfo->driver));
  566. strlcpy(drvinfo->version, "0.1", sizeof(drvinfo->version));
  567. }
  568. static int macvlan_ethtool_get_settings(struct net_device *dev,
  569. struct ethtool_cmd *cmd)
  570. {
  571. const struct macvlan_dev *vlan = netdev_priv(dev);
  572. return __ethtool_get_settings(vlan->lowerdev, cmd);
  573. }
  574. static netdev_features_t macvlan_fix_features(struct net_device *dev,
  575. netdev_features_t features)
  576. {
  577. struct macvlan_dev *vlan = netdev_priv(dev);
  578. return features & (vlan->set_features | ~MACVLAN_FEATURES);
  579. }
  580. static const struct ethtool_ops macvlan_ethtool_ops = {
  581. .get_link = ethtool_op_get_link,
  582. .get_settings = macvlan_ethtool_get_settings,
  583. .get_drvinfo = macvlan_ethtool_get_drvinfo,
  584. };
  585. static const struct net_device_ops macvlan_netdev_ops = {
  586. .ndo_init = macvlan_init,
  587. .ndo_uninit = macvlan_uninit,
  588. .ndo_open = macvlan_open,
  589. .ndo_stop = macvlan_stop,
  590. .ndo_start_xmit = macvlan_start_xmit,
  591. .ndo_change_mtu = macvlan_change_mtu,
  592. .ndo_fix_features = macvlan_fix_features,
  593. .ndo_change_rx_flags = macvlan_change_rx_flags,
  594. .ndo_set_mac_address = macvlan_set_mac_address,
  595. .ndo_set_rx_mode = macvlan_set_mac_lists,
  596. .ndo_get_stats64 = macvlan_dev_get_stats64,
  597. .ndo_validate_addr = eth_validate_addr,
  598. .ndo_vlan_rx_add_vid = macvlan_vlan_rx_add_vid,
  599. .ndo_vlan_rx_kill_vid = macvlan_vlan_rx_kill_vid,
  600. .ndo_fdb_add = macvlan_fdb_add,
  601. .ndo_fdb_del = macvlan_fdb_del,
  602. .ndo_fdb_dump = ndo_dflt_fdb_dump,
  603. };
  604. void macvlan_common_setup(struct net_device *dev)
  605. {
  606. ether_setup(dev);
  607. dev->priv_flags &= ~(IFF_XMIT_DST_RELEASE | IFF_TX_SKB_SHARING);
  608. dev->priv_flags |= IFF_UNICAST_FLT;
  609. dev->netdev_ops = &macvlan_netdev_ops;
  610. dev->destructor = free_netdev;
  611. dev->header_ops = &macvlan_hard_header_ops;
  612. dev->ethtool_ops = &macvlan_ethtool_ops;
  613. }
  614. EXPORT_SYMBOL_GPL(macvlan_common_setup);
  615. static void macvlan_setup(struct net_device *dev)
  616. {
  617. macvlan_common_setup(dev);
  618. dev->tx_queue_len = 0;
  619. }
  620. static int macvlan_port_create(struct net_device *dev)
  621. {
  622. struct macvlan_port *port;
  623. unsigned int i;
  624. int err;
  625. if (dev->type != ARPHRD_ETHER || dev->flags & IFF_LOOPBACK)
  626. return -EINVAL;
  627. port = kzalloc(sizeof(*port), GFP_KERNEL);
  628. if (port == NULL)
  629. return -ENOMEM;
  630. port->passthru = false;
  631. port->dev = dev;
  632. INIT_LIST_HEAD(&port->vlans);
  633. for (i = 0; i < MACVLAN_HASH_SIZE; i++)
  634. INIT_HLIST_HEAD(&port->vlan_hash[i]);
  635. err = netdev_rx_handler_register(dev, macvlan_handle_frame, port);
  636. if (err)
  637. kfree(port);
  638. else
  639. dev->priv_flags |= IFF_MACVLAN_PORT;
  640. return err;
  641. }
  642. static void macvlan_port_destroy(struct net_device *dev)
  643. {
  644. struct macvlan_port *port = macvlan_port_get_rtnl(dev);
  645. dev->priv_flags &= ~IFF_MACVLAN_PORT;
  646. netdev_rx_handler_unregister(dev);
  647. kfree_rcu(port, rcu);
  648. }
  649. static int macvlan_validate(struct nlattr *tb[], struct nlattr *data[])
  650. {
  651. if (tb[IFLA_ADDRESS]) {
  652. if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
  653. return -EINVAL;
  654. if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
  655. return -EADDRNOTAVAIL;
  656. }
  657. if (data && data[IFLA_MACVLAN_FLAGS] &&
  658. nla_get_u16(data[IFLA_MACVLAN_FLAGS]) & ~MACVLAN_FLAG_NOPROMISC)
  659. return -EINVAL;
  660. if (data && data[IFLA_MACVLAN_MODE]) {
  661. switch (nla_get_u32(data[IFLA_MACVLAN_MODE])) {
  662. case MACVLAN_MODE_PRIVATE:
  663. case MACVLAN_MODE_VEPA:
  664. case MACVLAN_MODE_BRIDGE:
  665. case MACVLAN_MODE_PASSTHRU:
  666. break;
  667. default:
  668. return -EINVAL;
  669. }
  670. }
  671. return 0;
  672. }
  673. int macvlan_common_newlink(struct net *src_net, struct net_device *dev,
  674. struct nlattr *tb[], struct nlattr *data[],
  675. int (*receive)(struct sk_buff *skb),
  676. int (*forward)(struct net_device *dev,
  677. struct sk_buff *skb))
  678. {
  679. struct macvlan_dev *vlan = netdev_priv(dev);
  680. struct macvlan_port *port;
  681. struct net_device *lowerdev;
  682. int err;
  683. if (!tb[IFLA_LINK])
  684. return -EINVAL;
  685. lowerdev = __dev_get_by_index(src_net, nla_get_u32(tb[IFLA_LINK]));
  686. if (lowerdev == NULL)
  687. return -ENODEV;
  688. /* When creating macvlans on top of other macvlans - use
  689. * the real device as the lowerdev.
  690. */
  691. if (lowerdev->rtnl_link_ops == dev->rtnl_link_ops) {
  692. struct macvlan_dev *lowervlan = netdev_priv(lowerdev);
  693. lowerdev = lowervlan->lowerdev;
  694. }
  695. if (!tb[IFLA_MTU])
  696. dev->mtu = lowerdev->mtu;
  697. else if (dev->mtu > lowerdev->mtu)
  698. return -EINVAL;
  699. if (!tb[IFLA_ADDRESS])
  700. eth_hw_addr_random(dev);
  701. if (!macvlan_port_exists(lowerdev)) {
  702. err = macvlan_port_create(lowerdev);
  703. if (err < 0)
  704. return err;
  705. }
  706. port = macvlan_port_get_rtnl(lowerdev);
  707. /* Only 1 macvlan device can be created in passthru mode */
  708. if (port->passthru)
  709. return -EINVAL;
  710. vlan->lowerdev = lowerdev;
  711. vlan->dev = dev;
  712. vlan->port = port;
  713. vlan->receive = receive;
  714. vlan->forward = forward;
  715. vlan->set_features = MACVLAN_FEATURES;
  716. vlan->mode = MACVLAN_MODE_VEPA;
  717. if (data && data[IFLA_MACVLAN_MODE])
  718. vlan->mode = nla_get_u32(data[IFLA_MACVLAN_MODE]);
  719. if (data && data[IFLA_MACVLAN_FLAGS])
  720. vlan->flags = nla_get_u16(data[IFLA_MACVLAN_FLAGS]);
  721. if (vlan->mode == MACVLAN_MODE_PASSTHRU) {
  722. if (port->count)
  723. return -EINVAL;
  724. port->passthru = true;
  725. eth_hw_addr_inherit(dev, lowerdev);
  726. }
  727. port->count += 1;
  728. err = register_netdevice(dev);
  729. if (err < 0)
  730. goto destroy_port;
  731. dev->priv_flags |= IFF_MACVLAN;
  732. err = netdev_upper_dev_link(lowerdev, dev);
  733. if (err)
  734. goto destroy_port;
  735. list_add_tail_rcu(&vlan->list, &port->vlans);
  736. netif_stacked_transfer_operstate(lowerdev, dev);
  737. return 0;
  738. destroy_port:
  739. port->count -= 1;
  740. if (!port->count)
  741. macvlan_port_destroy(lowerdev);
  742. return err;
  743. }
  744. EXPORT_SYMBOL_GPL(macvlan_common_newlink);
  745. static int macvlan_newlink(struct net *src_net, struct net_device *dev,
  746. struct nlattr *tb[], struct nlattr *data[])
  747. {
  748. return macvlan_common_newlink(src_net, dev, tb, data,
  749. netif_rx,
  750. dev_forward_skb);
  751. }
  752. void macvlan_dellink(struct net_device *dev, struct list_head *head)
  753. {
  754. struct macvlan_dev *vlan = netdev_priv(dev);
  755. list_del_rcu(&vlan->list);
  756. unregister_netdevice_queue(dev, head);
  757. netdev_upper_dev_unlink(vlan->lowerdev, dev);
  758. }
  759. EXPORT_SYMBOL_GPL(macvlan_dellink);
  760. static int macvlan_changelink(struct net_device *dev,
  761. struct nlattr *tb[], struct nlattr *data[])
  762. {
  763. struct macvlan_dev *vlan = netdev_priv(dev);
  764. enum macvlan_mode mode;
  765. bool set_mode = false;
  766. /* Validate mode, but don't set yet: setting flags may fail. */
  767. if (data && data[IFLA_MACVLAN_MODE]) {
  768. set_mode = true;
  769. mode = nla_get_u32(data[IFLA_MACVLAN_MODE]);
  770. /* Passthrough mode can't be set or cleared dynamically */
  771. if ((mode == MACVLAN_MODE_PASSTHRU) !=
  772. (vlan->mode == MACVLAN_MODE_PASSTHRU))
  773. return -EINVAL;
  774. }
  775. if (data && data[IFLA_MACVLAN_FLAGS]) {
  776. __u16 flags = nla_get_u16(data[IFLA_MACVLAN_FLAGS]);
  777. bool promisc = (flags ^ vlan->flags) & MACVLAN_FLAG_NOPROMISC;
  778. if (vlan->port->passthru && promisc) {
  779. int err;
  780. if (flags & MACVLAN_FLAG_NOPROMISC)
  781. err = dev_set_promiscuity(vlan->lowerdev, -1);
  782. else
  783. err = dev_set_promiscuity(vlan->lowerdev, 1);
  784. if (err < 0)
  785. return err;
  786. }
  787. vlan->flags = flags;
  788. }
  789. if (set_mode)
  790. vlan->mode = mode;
  791. return 0;
  792. }
  793. static size_t macvlan_get_size(const struct net_device *dev)
  794. {
  795. return (0
  796. + nla_total_size(4) /* IFLA_MACVLAN_MODE */
  797. + nla_total_size(2) /* IFLA_MACVLAN_FLAGS */
  798. );
  799. }
  800. static int macvlan_fill_info(struct sk_buff *skb,
  801. const struct net_device *dev)
  802. {
  803. struct macvlan_dev *vlan = netdev_priv(dev);
  804. if (nla_put_u32(skb, IFLA_MACVLAN_MODE, vlan->mode))
  805. goto nla_put_failure;
  806. if (nla_put_u16(skb, IFLA_MACVLAN_FLAGS, vlan->flags))
  807. goto nla_put_failure;
  808. return 0;
  809. nla_put_failure:
  810. return -EMSGSIZE;
  811. }
  812. static const struct nla_policy macvlan_policy[IFLA_MACVLAN_MAX + 1] = {
  813. [IFLA_MACVLAN_MODE] = { .type = NLA_U32 },
  814. [IFLA_MACVLAN_FLAGS] = { .type = NLA_U16 },
  815. };
  816. int macvlan_link_register(struct rtnl_link_ops *ops)
  817. {
  818. /* common fields */
  819. ops->priv_size = sizeof(struct macvlan_dev);
  820. ops->validate = macvlan_validate;
  821. ops->maxtype = IFLA_MACVLAN_MAX;
  822. ops->policy = macvlan_policy;
  823. ops->changelink = macvlan_changelink;
  824. ops->get_size = macvlan_get_size;
  825. ops->fill_info = macvlan_fill_info;
  826. return rtnl_link_register(ops);
  827. };
  828. EXPORT_SYMBOL_GPL(macvlan_link_register);
  829. static struct rtnl_link_ops macvlan_link_ops = {
  830. .kind = "macvlan",
  831. .setup = macvlan_setup,
  832. .newlink = macvlan_newlink,
  833. .dellink = macvlan_dellink,
  834. };
  835. static int macvlan_device_event(struct notifier_block *unused,
  836. unsigned long event, void *ptr)
  837. {
  838. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  839. struct macvlan_dev *vlan, *next;
  840. struct macvlan_port *port;
  841. LIST_HEAD(list_kill);
  842. if (!macvlan_port_exists(dev))
  843. return NOTIFY_DONE;
  844. port = macvlan_port_get_rtnl(dev);
  845. switch (event) {
  846. case NETDEV_CHANGE:
  847. list_for_each_entry(vlan, &port->vlans, list)
  848. netif_stacked_transfer_operstate(vlan->lowerdev,
  849. vlan->dev);
  850. break;
  851. case NETDEV_FEAT_CHANGE:
  852. list_for_each_entry(vlan, &port->vlans, list) {
  853. vlan->dev->features = dev->features & MACVLAN_FEATURES;
  854. vlan->dev->gso_max_size = dev->gso_max_size;
  855. netdev_features_change(vlan->dev);
  856. }
  857. break;
  858. case NETDEV_UNREGISTER:
  859. /* twiddle thumbs on netns device moves */
  860. if (dev->reg_state != NETREG_UNREGISTERING)
  861. break;
  862. list_for_each_entry_safe(vlan, next, &port->vlans, list)
  863. vlan->dev->rtnl_link_ops->dellink(vlan->dev, &list_kill);
  864. unregister_netdevice_many(&list_kill);
  865. list_del(&list_kill);
  866. break;
  867. case NETDEV_PRE_TYPE_CHANGE:
  868. /* Forbid underlaying device to change its type. */
  869. return NOTIFY_BAD;
  870. }
  871. return NOTIFY_DONE;
  872. }
  873. static struct notifier_block macvlan_notifier_block __read_mostly = {
  874. .notifier_call = macvlan_device_event,
  875. };
  876. static int __init macvlan_init_module(void)
  877. {
  878. int err;
  879. register_netdevice_notifier(&macvlan_notifier_block);
  880. err = macvlan_link_register(&macvlan_link_ops);
  881. if (err < 0)
  882. goto err1;
  883. return 0;
  884. err1:
  885. unregister_netdevice_notifier(&macvlan_notifier_block);
  886. return err;
  887. }
  888. static void __exit macvlan_cleanup_module(void)
  889. {
  890. rtnl_link_unregister(&macvlan_link_ops);
  891. unregister_netdevice_notifier(&macvlan_notifier_block);
  892. }
  893. module_init(macvlan_init_module);
  894. module_exit(macvlan_cleanup_module);
  895. MODULE_LICENSE("GPL");
  896. MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
  897. MODULE_DESCRIPTION("Driver for MAC address based VLANs");
  898. MODULE_ALIAS_RTNL_LINK("macvlan");