macvlan.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848
  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_link.h>
  30. #include <linux/if_macvlan.h>
  31. #include <net/rtnetlink.h>
  32. #include <net/xfrm.h>
  33. #define MACVLAN_HASH_SIZE (1 << BITS_PER_BYTE)
  34. struct macvlan_port {
  35. struct net_device *dev;
  36. struct hlist_head vlan_hash[MACVLAN_HASH_SIZE];
  37. struct list_head vlans;
  38. struct rcu_head rcu;
  39. bool passthru;
  40. int count;
  41. };
  42. static void macvlan_port_destroy(struct net_device *dev);
  43. #define macvlan_port_get_rcu(dev) \
  44. ((struct macvlan_port *) rcu_dereference(dev->rx_handler_data))
  45. #define macvlan_port_get(dev) ((struct macvlan_port *) dev->rx_handler_data)
  46. #define macvlan_port_exists(dev) (dev->priv_flags & IFF_MACVLAN_PORT)
  47. static struct macvlan_dev *macvlan_hash_lookup(const struct macvlan_port *port,
  48. const unsigned char *addr)
  49. {
  50. struct macvlan_dev *vlan;
  51. struct hlist_node *n;
  52. hlist_for_each_entry_rcu(vlan, n, &port->vlan_hash[addr[5]], hlist) {
  53. if (!compare_ether_addr_64bits(vlan->dev->dev_addr, addr))
  54. return vlan;
  55. }
  56. return NULL;
  57. }
  58. static void macvlan_hash_add(struct macvlan_dev *vlan)
  59. {
  60. struct macvlan_port *port = vlan->port;
  61. const unsigned char *addr = vlan->dev->dev_addr;
  62. hlist_add_head_rcu(&vlan->hlist, &port->vlan_hash[addr[5]]);
  63. }
  64. static void macvlan_hash_del(struct macvlan_dev *vlan, bool sync)
  65. {
  66. hlist_del_rcu(&vlan->hlist);
  67. if (sync)
  68. synchronize_rcu();
  69. }
  70. static void macvlan_hash_change_addr(struct macvlan_dev *vlan,
  71. const unsigned char *addr)
  72. {
  73. macvlan_hash_del(vlan, true);
  74. /* Now that we are unhashed it is safe to change the device
  75. * address without confusing packet delivery.
  76. */
  77. memcpy(vlan->dev->dev_addr, addr, ETH_ALEN);
  78. macvlan_hash_add(vlan);
  79. }
  80. static int macvlan_addr_busy(const struct macvlan_port *port,
  81. const unsigned char *addr)
  82. {
  83. /* Test to see if the specified multicast address is
  84. * currently in use by the underlying device or
  85. * another macvlan.
  86. */
  87. if (!compare_ether_addr_64bits(port->dev->dev_addr, addr))
  88. return 1;
  89. if (macvlan_hash_lookup(port, addr))
  90. return 1;
  91. return 0;
  92. }
  93. static int macvlan_broadcast_one(struct sk_buff *skb,
  94. const struct macvlan_dev *vlan,
  95. const struct ethhdr *eth, bool local)
  96. {
  97. struct net_device *dev = vlan->dev;
  98. if (!skb)
  99. return NET_RX_DROP;
  100. if (local)
  101. return vlan->forward(dev, skb);
  102. skb->dev = dev;
  103. if (!compare_ether_addr_64bits(eth->h_dest,
  104. dev->broadcast))
  105. skb->pkt_type = PACKET_BROADCAST;
  106. else
  107. skb->pkt_type = PACKET_MULTICAST;
  108. return vlan->receive(skb);
  109. }
  110. static void macvlan_broadcast(struct sk_buff *skb,
  111. const struct macvlan_port *port,
  112. struct net_device *src,
  113. enum macvlan_mode mode)
  114. {
  115. const struct ethhdr *eth = eth_hdr(skb);
  116. const struct macvlan_dev *vlan;
  117. struct hlist_node *n;
  118. struct sk_buff *nskb;
  119. unsigned int i;
  120. int err;
  121. if (skb->protocol == htons(ETH_P_PAUSE))
  122. return;
  123. for (i = 0; i < MACVLAN_HASH_SIZE; i++) {
  124. hlist_for_each_entry_rcu(vlan, n, &port->vlan_hash[i], hlist) {
  125. if (vlan->dev == src || !(vlan->mode & mode))
  126. continue;
  127. nskb = skb_clone(skb, GFP_ATOMIC);
  128. err = macvlan_broadcast_one(nskb, vlan, eth,
  129. mode == MACVLAN_MODE_BRIDGE);
  130. macvlan_count_rx(vlan, skb->len + ETH_HLEN,
  131. err == NET_RX_SUCCESS, 1);
  132. }
  133. }
  134. }
  135. /* called under rcu_read_lock() from netif_receive_skb */
  136. static rx_handler_result_t macvlan_handle_frame(struct sk_buff **pskb)
  137. {
  138. struct macvlan_port *port;
  139. struct sk_buff *skb = *pskb;
  140. const struct ethhdr *eth = eth_hdr(skb);
  141. const struct macvlan_dev *vlan;
  142. const struct macvlan_dev *src;
  143. struct net_device *dev;
  144. unsigned int len = 0;
  145. int ret = NET_RX_DROP;
  146. port = macvlan_port_get_rcu(skb->dev);
  147. if (is_multicast_ether_addr(eth->h_dest)) {
  148. src = macvlan_hash_lookup(port, eth->h_source);
  149. if (!src)
  150. /* frame comes from an external address */
  151. macvlan_broadcast(skb, port, NULL,
  152. MACVLAN_MODE_PRIVATE |
  153. MACVLAN_MODE_VEPA |
  154. MACVLAN_MODE_PASSTHRU|
  155. MACVLAN_MODE_BRIDGE);
  156. else if (src->mode == MACVLAN_MODE_VEPA)
  157. /* flood to everyone except source */
  158. macvlan_broadcast(skb, port, src->dev,
  159. MACVLAN_MODE_VEPA |
  160. MACVLAN_MODE_BRIDGE);
  161. else if (src->mode == MACVLAN_MODE_BRIDGE)
  162. /*
  163. * flood only to VEPA ports, bridge ports
  164. * already saw the frame on the way out.
  165. */
  166. macvlan_broadcast(skb, port, src->dev,
  167. MACVLAN_MODE_VEPA);
  168. return RX_HANDLER_PASS;
  169. }
  170. if (port->passthru)
  171. vlan = list_first_entry(&port->vlans, struct macvlan_dev, list);
  172. else
  173. vlan = macvlan_hash_lookup(port, eth->h_dest);
  174. if (vlan == NULL)
  175. return RX_HANDLER_PASS;
  176. dev = vlan->dev;
  177. if (unlikely(!(dev->flags & IFF_UP))) {
  178. kfree_skb(skb);
  179. return RX_HANDLER_CONSUMED;
  180. }
  181. len = skb->len + ETH_HLEN;
  182. skb = skb_share_check(skb, GFP_ATOMIC);
  183. if (!skb)
  184. goto out;
  185. skb->dev = dev;
  186. skb->pkt_type = PACKET_HOST;
  187. ret = vlan->receive(skb);
  188. out:
  189. macvlan_count_rx(vlan, len, ret == NET_RX_SUCCESS, 0);
  190. return RX_HANDLER_CONSUMED;
  191. }
  192. static int macvlan_queue_xmit(struct sk_buff *skb, struct net_device *dev)
  193. {
  194. const struct macvlan_dev *vlan = netdev_priv(dev);
  195. const struct macvlan_port *port = vlan->port;
  196. const struct macvlan_dev *dest;
  197. __u8 ip_summed = skb->ip_summed;
  198. if (vlan->mode == MACVLAN_MODE_BRIDGE) {
  199. const struct ethhdr *eth = (void *)skb->data;
  200. skb->ip_summed = CHECKSUM_UNNECESSARY;
  201. /* send to other bridge ports directly */
  202. if (is_multicast_ether_addr(eth->h_dest)) {
  203. macvlan_broadcast(skb, port, dev, MACVLAN_MODE_BRIDGE);
  204. goto xmit_world;
  205. }
  206. dest = macvlan_hash_lookup(port, eth->h_dest);
  207. if (dest && dest->mode == MACVLAN_MODE_BRIDGE) {
  208. /* send to lowerdev first for its network taps */
  209. dev_forward_skb(vlan->lowerdev, skb);
  210. return NET_XMIT_SUCCESS;
  211. }
  212. }
  213. xmit_world:
  214. skb->ip_summed = ip_summed;
  215. skb_set_dev(skb, vlan->lowerdev);
  216. return dev_queue_xmit(skb);
  217. }
  218. netdev_tx_t macvlan_start_xmit(struct sk_buff *skb,
  219. struct net_device *dev)
  220. {
  221. unsigned int len = skb->len;
  222. int ret;
  223. const struct macvlan_dev *vlan = netdev_priv(dev);
  224. ret = macvlan_queue_xmit(skb, dev);
  225. if (likely(ret == NET_XMIT_SUCCESS || ret == NET_XMIT_CN)) {
  226. struct macvlan_pcpu_stats *pcpu_stats;
  227. pcpu_stats = this_cpu_ptr(vlan->pcpu_stats);
  228. u64_stats_update_begin(&pcpu_stats->syncp);
  229. pcpu_stats->tx_packets++;
  230. pcpu_stats->tx_bytes += len;
  231. u64_stats_update_end(&pcpu_stats->syncp);
  232. } else {
  233. this_cpu_inc(vlan->pcpu_stats->tx_dropped);
  234. }
  235. return ret;
  236. }
  237. EXPORT_SYMBOL_GPL(macvlan_start_xmit);
  238. static int macvlan_hard_header(struct sk_buff *skb, struct net_device *dev,
  239. unsigned short type, const void *daddr,
  240. const void *saddr, unsigned len)
  241. {
  242. const struct macvlan_dev *vlan = netdev_priv(dev);
  243. struct net_device *lowerdev = vlan->lowerdev;
  244. return dev_hard_header(skb, lowerdev, type, daddr,
  245. saddr ? : dev->dev_addr, len);
  246. }
  247. static const struct header_ops macvlan_hard_header_ops = {
  248. .create = macvlan_hard_header,
  249. .rebuild = eth_rebuild_header,
  250. .parse = eth_header_parse,
  251. .cache = eth_header_cache,
  252. .cache_update = eth_header_cache_update,
  253. };
  254. static int macvlan_open(struct net_device *dev)
  255. {
  256. struct macvlan_dev *vlan = netdev_priv(dev);
  257. struct net_device *lowerdev = vlan->lowerdev;
  258. int err;
  259. if (vlan->port->passthru) {
  260. dev_set_promiscuity(lowerdev, 1);
  261. goto hash_add;
  262. }
  263. err = -EBUSY;
  264. if (macvlan_addr_busy(vlan->port, dev->dev_addr))
  265. goto out;
  266. err = dev_uc_add(lowerdev, dev->dev_addr);
  267. if (err < 0)
  268. goto out;
  269. if (dev->flags & IFF_ALLMULTI) {
  270. err = dev_set_allmulti(lowerdev, 1);
  271. if (err < 0)
  272. goto del_unicast;
  273. }
  274. hash_add:
  275. macvlan_hash_add(vlan);
  276. return 0;
  277. del_unicast:
  278. dev_uc_del(lowerdev, dev->dev_addr);
  279. out:
  280. return err;
  281. }
  282. static int macvlan_stop(struct net_device *dev)
  283. {
  284. struct macvlan_dev *vlan = netdev_priv(dev);
  285. struct net_device *lowerdev = vlan->lowerdev;
  286. if (vlan->port->passthru) {
  287. dev_set_promiscuity(lowerdev, -1);
  288. goto hash_del;
  289. }
  290. dev_mc_unsync(lowerdev, dev);
  291. if (dev->flags & IFF_ALLMULTI)
  292. dev_set_allmulti(lowerdev, -1);
  293. dev_uc_del(lowerdev, dev->dev_addr);
  294. hash_del:
  295. macvlan_hash_del(vlan, !dev->dismantle);
  296. return 0;
  297. }
  298. static int macvlan_set_mac_address(struct net_device *dev, void *p)
  299. {
  300. struct macvlan_dev *vlan = netdev_priv(dev);
  301. struct net_device *lowerdev = vlan->lowerdev;
  302. struct sockaddr *addr = p;
  303. int err;
  304. if (!is_valid_ether_addr(addr->sa_data))
  305. return -EADDRNOTAVAIL;
  306. if (!(dev->flags & IFF_UP)) {
  307. /* Just copy in the new address */
  308. memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN);
  309. } else {
  310. /* Rehash and update the device filters */
  311. if (macvlan_addr_busy(vlan->port, addr->sa_data))
  312. return -EBUSY;
  313. err = dev_uc_add(lowerdev, addr->sa_data);
  314. if (err)
  315. return err;
  316. dev_uc_del(lowerdev, dev->dev_addr);
  317. macvlan_hash_change_addr(vlan, addr->sa_data);
  318. }
  319. return 0;
  320. }
  321. static void macvlan_change_rx_flags(struct net_device *dev, int change)
  322. {
  323. struct macvlan_dev *vlan = netdev_priv(dev);
  324. struct net_device *lowerdev = vlan->lowerdev;
  325. if (change & IFF_ALLMULTI)
  326. dev_set_allmulti(lowerdev, dev->flags & IFF_ALLMULTI ? 1 : -1);
  327. }
  328. static void macvlan_set_multicast_list(struct net_device *dev)
  329. {
  330. struct macvlan_dev *vlan = netdev_priv(dev);
  331. dev_mc_sync(vlan->lowerdev, dev);
  332. }
  333. static int macvlan_change_mtu(struct net_device *dev, int new_mtu)
  334. {
  335. struct macvlan_dev *vlan = netdev_priv(dev);
  336. if (new_mtu < 68 || vlan->lowerdev->mtu < new_mtu)
  337. return -EINVAL;
  338. dev->mtu = new_mtu;
  339. return 0;
  340. }
  341. /*
  342. * macvlan network devices have devices nesting below it and are a special
  343. * "super class" of normal network devices; split their locks off into a
  344. * separate class since they always nest.
  345. */
  346. static struct lock_class_key macvlan_netdev_xmit_lock_key;
  347. static struct lock_class_key macvlan_netdev_addr_lock_key;
  348. #define MACVLAN_FEATURES \
  349. (NETIF_F_SG | NETIF_F_ALL_CSUM | NETIF_F_HIGHDMA | NETIF_F_FRAGLIST | \
  350. NETIF_F_GSO | NETIF_F_TSO | NETIF_F_UFO | NETIF_F_GSO_ROBUST | \
  351. NETIF_F_TSO_ECN | NETIF_F_TSO6 | NETIF_F_GRO | NETIF_F_RXCSUM)
  352. #define MACVLAN_STATE_MASK \
  353. ((1<<__LINK_STATE_NOCARRIER) | (1<<__LINK_STATE_DORMANT))
  354. static void macvlan_set_lockdep_class_one(struct net_device *dev,
  355. struct netdev_queue *txq,
  356. void *_unused)
  357. {
  358. lockdep_set_class(&txq->_xmit_lock,
  359. &macvlan_netdev_xmit_lock_key);
  360. }
  361. static void macvlan_set_lockdep_class(struct net_device *dev)
  362. {
  363. lockdep_set_class(&dev->addr_list_lock,
  364. &macvlan_netdev_addr_lock_key);
  365. netdev_for_each_tx_queue(dev, macvlan_set_lockdep_class_one, NULL);
  366. }
  367. static int macvlan_init(struct net_device *dev)
  368. {
  369. struct macvlan_dev *vlan = netdev_priv(dev);
  370. const struct net_device *lowerdev = vlan->lowerdev;
  371. dev->state = (dev->state & ~MACVLAN_STATE_MASK) |
  372. (lowerdev->state & MACVLAN_STATE_MASK);
  373. dev->features = lowerdev->features & MACVLAN_FEATURES;
  374. dev->features |= NETIF_F_LLTX;
  375. dev->gso_max_size = lowerdev->gso_max_size;
  376. dev->iflink = lowerdev->ifindex;
  377. dev->hard_header_len = lowerdev->hard_header_len;
  378. macvlan_set_lockdep_class(dev);
  379. vlan->pcpu_stats = alloc_percpu(struct macvlan_pcpu_stats);
  380. if (!vlan->pcpu_stats)
  381. return -ENOMEM;
  382. return 0;
  383. }
  384. static void macvlan_uninit(struct net_device *dev)
  385. {
  386. struct macvlan_dev *vlan = netdev_priv(dev);
  387. struct macvlan_port *port = vlan->port;
  388. free_percpu(vlan->pcpu_stats);
  389. port->count -= 1;
  390. if (!port->count)
  391. macvlan_port_destroy(port->dev);
  392. }
  393. static struct rtnl_link_stats64 *macvlan_dev_get_stats64(struct net_device *dev,
  394. struct rtnl_link_stats64 *stats)
  395. {
  396. struct macvlan_dev *vlan = netdev_priv(dev);
  397. if (vlan->pcpu_stats) {
  398. struct macvlan_pcpu_stats *p;
  399. u64 rx_packets, rx_bytes, rx_multicast, tx_packets, tx_bytes;
  400. u32 rx_errors = 0, tx_dropped = 0;
  401. unsigned int start;
  402. int i;
  403. for_each_possible_cpu(i) {
  404. p = per_cpu_ptr(vlan->pcpu_stats, i);
  405. do {
  406. start = u64_stats_fetch_begin_bh(&p->syncp);
  407. rx_packets = p->rx_packets;
  408. rx_bytes = p->rx_bytes;
  409. rx_multicast = p->rx_multicast;
  410. tx_packets = p->tx_packets;
  411. tx_bytes = p->tx_bytes;
  412. } while (u64_stats_fetch_retry_bh(&p->syncp, start));
  413. stats->rx_packets += rx_packets;
  414. stats->rx_bytes += rx_bytes;
  415. stats->multicast += rx_multicast;
  416. stats->tx_packets += tx_packets;
  417. stats->tx_bytes += tx_bytes;
  418. /* rx_errors & tx_dropped are u32, updated
  419. * without syncp protection.
  420. */
  421. rx_errors += p->rx_errors;
  422. tx_dropped += p->tx_dropped;
  423. }
  424. stats->rx_errors = rx_errors;
  425. stats->rx_dropped = rx_errors;
  426. stats->tx_dropped = tx_dropped;
  427. }
  428. return stats;
  429. }
  430. static void macvlan_ethtool_get_drvinfo(struct net_device *dev,
  431. struct ethtool_drvinfo *drvinfo)
  432. {
  433. snprintf(drvinfo->driver, 32, "macvlan");
  434. snprintf(drvinfo->version, 32, "0.1");
  435. }
  436. static int macvlan_ethtool_get_settings(struct net_device *dev,
  437. struct ethtool_cmd *cmd)
  438. {
  439. const struct macvlan_dev *vlan = netdev_priv(dev);
  440. return dev_ethtool_get_settings(vlan->lowerdev, cmd);
  441. }
  442. static const struct ethtool_ops macvlan_ethtool_ops = {
  443. .get_link = ethtool_op_get_link,
  444. .get_settings = macvlan_ethtool_get_settings,
  445. .get_drvinfo = macvlan_ethtool_get_drvinfo,
  446. };
  447. static const struct net_device_ops macvlan_netdev_ops = {
  448. .ndo_init = macvlan_init,
  449. .ndo_uninit = macvlan_uninit,
  450. .ndo_open = macvlan_open,
  451. .ndo_stop = macvlan_stop,
  452. .ndo_start_xmit = macvlan_start_xmit,
  453. .ndo_change_mtu = macvlan_change_mtu,
  454. .ndo_change_rx_flags = macvlan_change_rx_flags,
  455. .ndo_set_mac_address = macvlan_set_mac_address,
  456. .ndo_set_multicast_list = macvlan_set_multicast_list,
  457. .ndo_get_stats64 = macvlan_dev_get_stats64,
  458. .ndo_validate_addr = eth_validate_addr,
  459. };
  460. void macvlan_common_setup(struct net_device *dev)
  461. {
  462. ether_setup(dev);
  463. dev->priv_flags &= ~(IFF_XMIT_DST_RELEASE | IFF_TX_SKB_SHARING);
  464. dev->netdev_ops = &macvlan_netdev_ops;
  465. dev->destructor = free_netdev;
  466. dev->header_ops = &macvlan_hard_header_ops,
  467. dev->ethtool_ops = &macvlan_ethtool_ops;
  468. }
  469. EXPORT_SYMBOL_GPL(macvlan_common_setup);
  470. static void macvlan_setup(struct net_device *dev)
  471. {
  472. macvlan_common_setup(dev);
  473. dev->tx_queue_len = 0;
  474. }
  475. static int macvlan_port_create(struct net_device *dev)
  476. {
  477. struct macvlan_port *port;
  478. unsigned int i;
  479. int err;
  480. if (dev->type != ARPHRD_ETHER || dev->flags & IFF_LOOPBACK)
  481. return -EINVAL;
  482. port = kzalloc(sizeof(*port), GFP_KERNEL);
  483. if (port == NULL)
  484. return -ENOMEM;
  485. port->passthru = false;
  486. port->dev = dev;
  487. INIT_LIST_HEAD(&port->vlans);
  488. for (i = 0; i < MACVLAN_HASH_SIZE; i++)
  489. INIT_HLIST_HEAD(&port->vlan_hash[i]);
  490. err = netdev_rx_handler_register(dev, macvlan_handle_frame, port);
  491. if (err)
  492. kfree(port);
  493. else
  494. dev->priv_flags |= IFF_MACVLAN_PORT;
  495. return err;
  496. }
  497. static void macvlan_port_destroy(struct net_device *dev)
  498. {
  499. struct macvlan_port *port = macvlan_port_get(dev);
  500. dev->priv_flags &= ~IFF_MACVLAN_PORT;
  501. netdev_rx_handler_unregister(dev);
  502. kfree_rcu(port, rcu);
  503. }
  504. static int macvlan_validate(struct nlattr *tb[], struct nlattr *data[])
  505. {
  506. if (tb[IFLA_ADDRESS]) {
  507. if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
  508. return -EINVAL;
  509. if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
  510. return -EADDRNOTAVAIL;
  511. }
  512. if (data && data[IFLA_MACVLAN_MODE]) {
  513. switch (nla_get_u32(data[IFLA_MACVLAN_MODE])) {
  514. case MACVLAN_MODE_PRIVATE:
  515. case MACVLAN_MODE_VEPA:
  516. case MACVLAN_MODE_BRIDGE:
  517. case MACVLAN_MODE_PASSTHRU:
  518. break;
  519. default:
  520. return -EINVAL;
  521. }
  522. }
  523. return 0;
  524. }
  525. int macvlan_common_newlink(struct net *src_net, struct net_device *dev,
  526. struct nlattr *tb[], struct nlattr *data[],
  527. int (*receive)(struct sk_buff *skb),
  528. int (*forward)(struct net_device *dev,
  529. struct sk_buff *skb))
  530. {
  531. struct macvlan_dev *vlan = netdev_priv(dev);
  532. struct macvlan_port *port;
  533. struct net_device *lowerdev;
  534. int err;
  535. if (!tb[IFLA_LINK])
  536. return -EINVAL;
  537. lowerdev = __dev_get_by_index(src_net, nla_get_u32(tb[IFLA_LINK]));
  538. if (lowerdev == NULL)
  539. return -ENODEV;
  540. /* When creating macvlans on top of other macvlans - use
  541. * the real device as the lowerdev.
  542. */
  543. if (lowerdev->rtnl_link_ops == dev->rtnl_link_ops) {
  544. struct macvlan_dev *lowervlan = netdev_priv(lowerdev);
  545. lowerdev = lowervlan->lowerdev;
  546. }
  547. if (!tb[IFLA_MTU])
  548. dev->mtu = lowerdev->mtu;
  549. else if (dev->mtu > lowerdev->mtu)
  550. return -EINVAL;
  551. if (!tb[IFLA_ADDRESS])
  552. random_ether_addr(dev->dev_addr);
  553. if (!macvlan_port_exists(lowerdev)) {
  554. err = macvlan_port_create(lowerdev);
  555. if (err < 0)
  556. return err;
  557. }
  558. port = macvlan_port_get(lowerdev);
  559. /* Only 1 macvlan device can be created in passthru mode */
  560. if (port->passthru)
  561. return -EINVAL;
  562. vlan->lowerdev = lowerdev;
  563. vlan->dev = dev;
  564. vlan->port = port;
  565. vlan->receive = receive;
  566. vlan->forward = forward;
  567. vlan->mode = MACVLAN_MODE_VEPA;
  568. if (data && data[IFLA_MACVLAN_MODE])
  569. vlan->mode = nla_get_u32(data[IFLA_MACVLAN_MODE]);
  570. if (vlan->mode == MACVLAN_MODE_PASSTHRU) {
  571. if (port->count)
  572. return -EINVAL;
  573. port->passthru = true;
  574. memcpy(dev->dev_addr, lowerdev->dev_addr, ETH_ALEN);
  575. }
  576. port->count += 1;
  577. err = register_netdevice(dev);
  578. if (err < 0)
  579. goto destroy_port;
  580. list_add_tail(&vlan->list, &port->vlans);
  581. netif_stacked_transfer_operstate(lowerdev, dev);
  582. return 0;
  583. destroy_port:
  584. port->count -= 1;
  585. if (!port->count)
  586. macvlan_port_destroy(lowerdev);
  587. return err;
  588. }
  589. EXPORT_SYMBOL_GPL(macvlan_common_newlink);
  590. static int macvlan_newlink(struct net *src_net, struct net_device *dev,
  591. struct nlattr *tb[], struct nlattr *data[])
  592. {
  593. return macvlan_common_newlink(src_net, dev, tb, data,
  594. netif_rx,
  595. dev_forward_skb);
  596. }
  597. void macvlan_dellink(struct net_device *dev, struct list_head *head)
  598. {
  599. struct macvlan_dev *vlan = netdev_priv(dev);
  600. list_del(&vlan->list);
  601. unregister_netdevice_queue(dev, head);
  602. }
  603. EXPORT_SYMBOL_GPL(macvlan_dellink);
  604. static int macvlan_changelink(struct net_device *dev,
  605. struct nlattr *tb[], struct nlattr *data[])
  606. {
  607. struct macvlan_dev *vlan = netdev_priv(dev);
  608. if (data && data[IFLA_MACVLAN_MODE])
  609. vlan->mode = nla_get_u32(data[IFLA_MACVLAN_MODE]);
  610. return 0;
  611. }
  612. static size_t macvlan_get_size(const struct net_device *dev)
  613. {
  614. return nla_total_size(4);
  615. }
  616. static int macvlan_fill_info(struct sk_buff *skb,
  617. const struct net_device *dev)
  618. {
  619. struct macvlan_dev *vlan = netdev_priv(dev);
  620. NLA_PUT_U32(skb, IFLA_MACVLAN_MODE, vlan->mode);
  621. return 0;
  622. nla_put_failure:
  623. return -EMSGSIZE;
  624. }
  625. static const struct nla_policy macvlan_policy[IFLA_MACVLAN_MAX + 1] = {
  626. [IFLA_MACVLAN_MODE] = { .type = NLA_U32 },
  627. };
  628. int macvlan_link_register(struct rtnl_link_ops *ops)
  629. {
  630. /* common fields */
  631. ops->priv_size = sizeof(struct macvlan_dev);
  632. ops->validate = macvlan_validate;
  633. ops->maxtype = IFLA_MACVLAN_MAX;
  634. ops->policy = macvlan_policy;
  635. ops->changelink = macvlan_changelink;
  636. ops->get_size = macvlan_get_size;
  637. ops->fill_info = macvlan_fill_info;
  638. return rtnl_link_register(ops);
  639. };
  640. EXPORT_SYMBOL_GPL(macvlan_link_register);
  641. static struct rtnl_link_ops macvlan_link_ops = {
  642. .kind = "macvlan",
  643. .setup = macvlan_setup,
  644. .newlink = macvlan_newlink,
  645. .dellink = macvlan_dellink,
  646. };
  647. static int macvlan_device_event(struct notifier_block *unused,
  648. unsigned long event, void *ptr)
  649. {
  650. struct net_device *dev = ptr;
  651. struct macvlan_dev *vlan, *next;
  652. struct macvlan_port *port;
  653. LIST_HEAD(list_kill);
  654. if (!macvlan_port_exists(dev))
  655. return NOTIFY_DONE;
  656. port = macvlan_port_get(dev);
  657. switch (event) {
  658. case NETDEV_CHANGE:
  659. list_for_each_entry(vlan, &port->vlans, list)
  660. netif_stacked_transfer_operstate(vlan->lowerdev,
  661. vlan->dev);
  662. break;
  663. case NETDEV_FEAT_CHANGE:
  664. list_for_each_entry(vlan, &port->vlans, list) {
  665. vlan->dev->features = dev->features & MACVLAN_FEATURES;
  666. vlan->dev->gso_max_size = dev->gso_max_size;
  667. netdev_features_change(vlan->dev);
  668. }
  669. break;
  670. case NETDEV_UNREGISTER:
  671. /* twiddle thumbs on netns device moves */
  672. if (dev->reg_state != NETREG_UNREGISTERING)
  673. break;
  674. list_for_each_entry_safe(vlan, next, &port->vlans, list)
  675. vlan->dev->rtnl_link_ops->dellink(vlan->dev, &list_kill);
  676. unregister_netdevice_many(&list_kill);
  677. list_del(&list_kill);
  678. break;
  679. case NETDEV_PRE_TYPE_CHANGE:
  680. /* Forbid underlaying device to change its type. */
  681. return NOTIFY_BAD;
  682. }
  683. return NOTIFY_DONE;
  684. }
  685. static struct notifier_block macvlan_notifier_block __read_mostly = {
  686. .notifier_call = macvlan_device_event,
  687. };
  688. static int __init macvlan_init_module(void)
  689. {
  690. int err;
  691. register_netdevice_notifier(&macvlan_notifier_block);
  692. err = macvlan_link_register(&macvlan_link_ops);
  693. if (err < 0)
  694. goto err1;
  695. return 0;
  696. err1:
  697. unregister_netdevice_notifier(&macvlan_notifier_block);
  698. return err;
  699. }
  700. static void __exit macvlan_cleanup_module(void)
  701. {
  702. rtnl_link_unregister(&macvlan_link_ops);
  703. unregister_netdevice_notifier(&macvlan_notifier_block);
  704. }
  705. module_init(macvlan_init_module);
  706. module_exit(macvlan_cleanup_module);
  707. MODULE_LICENSE("GPL");
  708. MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
  709. MODULE_DESCRIPTION("Driver for MAC address based VLANs");
  710. MODULE_ALIAS_RTNL_LINK("macvlan");