macvlan.c 22 KB

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