macvlan.c 26 KB

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