macvlan.c 27 KB

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