macvlan.c 26 KB

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