macvlan.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618
  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 <linux/workqueue.h>
  34. #include <net/rtnetlink.h>
  35. #include <net/xfrm.h>
  36. #include <linux/netpoll.h>
  37. #define MACVLAN_HASH_BITS 8
  38. #define MACVLAN_HASH_SIZE (1<<MACVLAN_HASH_BITS)
  39. #define MACVLAN_BC_QUEUE_LEN 1000
  40. struct macvlan_port {
  41. struct net_device *dev;
  42. struct hlist_head vlan_hash[MACVLAN_HASH_SIZE];
  43. struct list_head vlans;
  44. struct rcu_head rcu;
  45. struct sk_buff_head bc_queue;
  46. struct work_struct bc_work;
  47. bool passthru;
  48. int count;
  49. struct hlist_head vlan_source_hash[MACVLAN_HASH_SIZE];
  50. };
  51. struct macvlan_source_entry {
  52. struct hlist_node hlist;
  53. struct macvlan_dev *vlan;
  54. unsigned char addr[6+2] __aligned(sizeof(u16));
  55. struct rcu_head rcu;
  56. };
  57. struct macvlan_skb_cb {
  58. const struct macvlan_dev *src;
  59. };
  60. #define MACVLAN_SKB_CB(__skb) ((struct macvlan_skb_cb *)&((__skb)->cb[0]))
  61. static void macvlan_port_destroy(struct net_device *dev);
  62. /* Hash Ethernet address */
  63. static u32 macvlan_eth_hash(const unsigned char *addr)
  64. {
  65. u64 value = get_unaligned((u64 *)addr);
  66. /* only want 6 bytes */
  67. #ifdef __BIG_ENDIAN
  68. value >>= 16;
  69. #else
  70. value <<= 16;
  71. #endif
  72. return hash_64(value, MACVLAN_HASH_BITS);
  73. }
  74. static struct macvlan_port *macvlan_port_get_rcu(const struct net_device *dev)
  75. {
  76. return rcu_dereference(dev->rx_handler_data);
  77. }
  78. static struct macvlan_port *macvlan_port_get_rtnl(const struct net_device *dev)
  79. {
  80. return rtnl_dereference(dev->rx_handler_data);
  81. }
  82. #define macvlan_port_exists(dev) (dev->priv_flags & IFF_MACVLAN_PORT)
  83. static struct macvlan_dev *macvlan_hash_lookup(const struct macvlan_port *port,
  84. const unsigned char *addr)
  85. {
  86. struct macvlan_dev *vlan;
  87. u32 idx = macvlan_eth_hash(addr);
  88. hlist_for_each_entry_rcu(vlan, &port->vlan_hash[idx], hlist) {
  89. if (ether_addr_equal_64bits(vlan->dev->dev_addr, addr))
  90. return vlan;
  91. }
  92. return NULL;
  93. }
  94. static struct macvlan_source_entry *macvlan_hash_lookup_source(
  95. const struct macvlan_dev *vlan,
  96. const unsigned char *addr)
  97. {
  98. struct macvlan_source_entry *entry;
  99. u32 idx = macvlan_eth_hash(addr);
  100. struct hlist_head *h = &vlan->port->vlan_source_hash[idx];
  101. hlist_for_each_entry_rcu(entry, h, hlist) {
  102. if (ether_addr_equal_64bits(entry->addr, addr) &&
  103. entry->vlan == vlan)
  104. return entry;
  105. }
  106. return NULL;
  107. }
  108. static int macvlan_hash_add_source(struct macvlan_dev *vlan,
  109. const unsigned char *addr)
  110. {
  111. struct macvlan_port *port = vlan->port;
  112. struct macvlan_source_entry *entry;
  113. struct hlist_head *h;
  114. entry = macvlan_hash_lookup_source(vlan, addr);
  115. if (entry)
  116. return 0;
  117. entry = kmalloc(sizeof(*entry), GFP_KERNEL);
  118. if (!entry)
  119. return -ENOMEM;
  120. ether_addr_copy(entry->addr, addr);
  121. entry->vlan = vlan;
  122. h = &port->vlan_source_hash[macvlan_eth_hash(addr)];
  123. hlist_add_head_rcu(&entry->hlist, h);
  124. vlan->macaddr_count++;
  125. return 0;
  126. }
  127. static void macvlan_hash_add(struct macvlan_dev *vlan)
  128. {
  129. struct macvlan_port *port = vlan->port;
  130. const unsigned char *addr = vlan->dev->dev_addr;
  131. u32 idx = macvlan_eth_hash(addr);
  132. hlist_add_head_rcu(&vlan->hlist, &port->vlan_hash[idx]);
  133. }
  134. static void macvlan_hash_del_source(struct macvlan_source_entry *entry)
  135. {
  136. hlist_del_rcu(&entry->hlist);
  137. kfree_rcu(entry, rcu);
  138. }
  139. static void macvlan_hash_del(struct macvlan_dev *vlan, bool sync)
  140. {
  141. hlist_del_rcu(&vlan->hlist);
  142. if (sync)
  143. synchronize_rcu();
  144. }
  145. static void macvlan_hash_change_addr(struct macvlan_dev *vlan,
  146. const unsigned char *addr)
  147. {
  148. macvlan_hash_del(vlan, true);
  149. /* Now that we are unhashed it is safe to change the device
  150. * address without confusing packet delivery.
  151. */
  152. memcpy(vlan->dev->dev_addr, addr, ETH_ALEN);
  153. macvlan_hash_add(vlan);
  154. }
  155. static int macvlan_addr_busy(const struct macvlan_port *port,
  156. const unsigned char *addr)
  157. {
  158. /* Test to see if the specified multicast address is
  159. * currently in use by the underlying device or
  160. * another macvlan.
  161. */
  162. if (ether_addr_equal_64bits(port->dev->dev_addr, addr))
  163. return 1;
  164. if (macvlan_hash_lookup(port, addr))
  165. return 1;
  166. return 0;
  167. }
  168. static int macvlan_broadcast_one(struct sk_buff *skb,
  169. const struct macvlan_dev *vlan,
  170. const struct ethhdr *eth, bool local)
  171. {
  172. struct net_device *dev = vlan->dev;
  173. if (local)
  174. return __dev_forward_skb(dev, skb);
  175. skb->dev = dev;
  176. if (ether_addr_equal_64bits(eth->h_dest, dev->broadcast))
  177. skb->pkt_type = PACKET_BROADCAST;
  178. else
  179. skb->pkt_type = PACKET_MULTICAST;
  180. return 0;
  181. }
  182. static u32 macvlan_hash_mix(const struct macvlan_dev *vlan)
  183. {
  184. return (u32)(((unsigned long)vlan) >> L1_CACHE_SHIFT);
  185. }
  186. static unsigned int mc_hash(const struct macvlan_dev *vlan,
  187. const unsigned char *addr)
  188. {
  189. u32 val = __get_unaligned_cpu32(addr + 2);
  190. val ^= macvlan_hash_mix(vlan);
  191. return hash_32(val, MACVLAN_MC_FILTER_BITS);
  192. }
  193. static void macvlan_broadcast(struct sk_buff *skb,
  194. const struct macvlan_port *port,
  195. struct net_device *src,
  196. enum macvlan_mode mode)
  197. {
  198. const struct ethhdr *eth = eth_hdr(skb);
  199. const struct macvlan_dev *vlan;
  200. struct sk_buff *nskb;
  201. unsigned int i;
  202. int err;
  203. unsigned int hash;
  204. if (skb->protocol == htons(ETH_P_PAUSE))
  205. return;
  206. for (i = 0; i < MACVLAN_HASH_SIZE; i++) {
  207. hlist_for_each_entry_rcu(vlan, &port->vlan_hash[i], hlist) {
  208. if (vlan->dev == src || !(vlan->mode & mode))
  209. continue;
  210. hash = mc_hash(vlan, eth->h_dest);
  211. if (!test_bit(hash, vlan->mc_filter))
  212. continue;
  213. err = NET_RX_DROP;
  214. nskb = skb_clone(skb, GFP_ATOMIC);
  215. if (likely(nskb))
  216. err = macvlan_broadcast_one(
  217. nskb, vlan, eth,
  218. mode == MACVLAN_MODE_BRIDGE) ?:
  219. netif_rx_ni(nskb);
  220. macvlan_count_rx(vlan, skb->len + ETH_HLEN,
  221. err == NET_RX_SUCCESS, true);
  222. }
  223. }
  224. }
  225. static void macvlan_process_broadcast(struct work_struct *w)
  226. {
  227. struct macvlan_port *port = container_of(w, struct macvlan_port,
  228. bc_work);
  229. struct sk_buff *skb;
  230. struct sk_buff_head list;
  231. __skb_queue_head_init(&list);
  232. spin_lock_bh(&port->bc_queue.lock);
  233. skb_queue_splice_tail_init(&port->bc_queue, &list);
  234. spin_unlock_bh(&port->bc_queue.lock);
  235. while ((skb = __skb_dequeue(&list))) {
  236. const struct macvlan_dev *src = MACVLAN_SKB_CB(skb)->src;
  237. rcu_read_lock();
  238. if (!src)
  239. /* frame comes from an external address */
  240. macvlan_broadcast(skb, port, NULL,
  241. MACVLAN_MODE_PRIVATE |
  242. MACVLAN_MODE_VEPA |
  243. MACVLAN_MODE_PASSTHRU|
  244. MACVLAN_MODE_BRIDGE);
  245. else if (src->mode == MACVLAN_MODE_VEPA)
  246. /* flood to everyone except source */
  247. macvlan_broadcast(skb, port, src->dev,
  248. MACVLAN_MODE_VEPA |
  249. MACVLAN_MODE_BRIDGE);
  250. else
  251. /*
  252. * flood only to VEPA ports, bridge ports
  253. * already saw the frame on the way out.
  254. */
  255. macvlan_broadcast(skb, port, src->dev,
  256. MACVLAN_MODE_VEPA);
  257. rcu_read_unlock();
  258. kfree_skb(skb);
  259. }
  260. }
  261. static void macvlan_broadcast_enqueue(struct macvlan_port *port,
  262. struct sk_buff *skb)
  263. {
  264. struct sk_buff *nskb;
  265. int err = -ENOMEM;
  266. nskb = skb_clone(skb, GFP_ATOMIC);
  267. if (!nskb)
  268. goto err;
  269. spin_lock(&port->bc_queue.lock);
  270. if (skb_queue_len(&port->bc_queue) < MACVLAN_BC_QUEUE_LEN) {
  271. __skb_queue_tail(&port->bc_queue, nskb);
  272. err = 0;
  273. }
  274. spin_unlock(&port->bc_queue.lock);
  275. if (err)
  276. goto free_nskb;
  277. schedule_work(&port->bc_work);
  278. return;
  279. free_nskb:
  280. kfree_skb(nskb);
  281. err:
  282. atomic_long_inc(&skb->dev->rx_dropped);
  283. }
  284. static void macvlan_flush_sources(struct macvlan_port *port,
  285. struct macvlan_dev *vlan)
  286. {
  287. int i;
  288. for (i = 0; i < MACVLAN_HASH_SIZE; i++) {
  289. struct hlist_node *h, *n;
  290. hlist_for_each_safe(h, n, &port->vlan_source_hash[i]) {
  291. struct macvlan_source_entry *entry;
  292. entry = hlist_entry(h, struct macvlan_source_entry,
  293. hlist);
  294. if (entry->vlan == vlan)
  295. macvlan_hash_del_source(entry);
  296. }
  297. }
  298. vlan->macaddr_count = 0;
  299. }
  300. static void macvlan_forward_source_one(struct sk_buff *skb,
  301. struct macvlan_dev *vlan)
  302. {
  303. struct sk_buff *nskb;
  304. struct net_device *dev;
  305. int len;
  306. int ret;
  307. dev = vlan->dev;
  308. if (unlikely(!(dev->flags & IFF_UP)))
  309. return;
  310. nskb = skb_clone(skb, GFP_ATOMIC);
  311. if (!nskb)
  312. return;
  313. len = nskb->len + ETH_HLEN;
  314. nskb->dev = dev;
  315. nskb->pkt_type = PACKET_HOST;
  316. ret = netif_rx(nskb);
  317. macvlan_count_rx(vlan, len, ret == NET_RX_SUCCESS, false);
  318. }
  319. static void macvlan_forward_source(struct sk_buff *skb,
  320. struct macvlan_port *port,
  321. const unsigned char *addr)
  322. {
  323. struct macvlan_source_entry *entry;
  324. u32 idx = macvlan_eth_hash(addr);
  325. struct hlist_head *h = &port->vlan_source_hash[idx];
  326. hlist_for_each_entry_rcu(entry, h, hlist) {
  327. if (ether_addr_equal_64bits(entry->addr, addr))
  328. if (entry->vlan->dev->flags & IFF_UP)
  329. macvlan_forward_source_one(skb, entry->vlan);
  330. }
  331. }
  332. /* called under rcu_read_lock() from netif_receive_skb */
  333. static rx_handler_result_t macvlan_handle_frame(struct sk_buff **pskb)
  334. {
  335. struct macvlan_port *port;
  336. struct sk_buff *skb = *pskb;
  337. const struct ethhdr *eth = eth_hdr(skb);
  338. const struct macvlan_dev *vlan;
  339. const struct macvlan_dev *src;
  340. struct net_device *dev;
  341. unsigned int len = 0;
  342. int ret;
  343. rx_handler_result_t handle_res;
  344. port = macvlan_port_get_rcu(skb->dev);
  345. if (is_multicast_ether_addr(eth->h_dest)) {
  346. skb = ip_check_defrag(dev_net(skb->dev), skb, IP_DEFRAG_MACVLAN);
  347. if (!skb)
  348. return RX_HANDLER_CONSUMED;
  349. *pskb = skb;
  350. eth = eth_hdr(skb);
  351. macvlan_forward_source(skb, port, eth->h_source);
  352. src = macvlan_hash_lookup(port, eth->h_source);
  353. if (src && src->mode != MACVLAN_MODE_VEPA &&
  354. src->mode != MACVLAN_MODE_BRIDGE) {
  355. /* forward to original port. */
  356. vlan = src;
  357. ret = macvlan_broadcast_one(skb, vlan, eth, 0) ?:
  358. netif_rx(skb);
  359. handle_res = RX_HANDLER_CONSUMED;
  360. goto out;
  361. }
  362. MACVLAN_SKB_CB(skb)->src = src;
  363. macvlan_broadcast_enqueue(port, skb);
  364. return RX_HANDLER_PASS;
  365. }
  366. macvlan_forward_source(skb, port, eth->h_source);
  367. if (port->passthru)
  368. vlan = list_first_or_null_rcu(&port->vlans,
  369. struct macvlan_dev, list);
  370. else
  371. vlan = macvlan_hash_lookup(port, eth->h_dest);
  372. if (!vlan || vlan->mode == MACVLAN_MODE_SOURCE)
  373. return RX_HANDLER_PASS;
  374. dev = vlan->dev;
  375. if (unlikely(!(dev->flags & IFF_UP))) {
  376. kfree_skb(skb);
  377. return RX_HANDLER_CONSUMED;
  378. }
  379. len = skb->len + ETH_HLEN;
  380. skb = skb_share_check(skb, GFP_ATOMIC);
  381. if (!skb) {
  382. ret = NET_RX_DROP;
  383. handle_res = RX_HANDLER_CONSUMED;
  384. goto out;
  385. }
  386. *pskb = skb;
  387. skb->dev = dev;
  388. skb->pkt_type = PACKET_HOST;
  389. ret = NET_RX_SUCCESS;
  390. handle_res = RX_HANDLER_ANOTHER;
  391. out:
  392. macvlan_count_rx(vlan, len, ret == NET_RX_SUCCESS, false);
  393. return handle_res;
  394. }
  395. static int macvlan_queue_xmit(struct sk_buff *skb, struct net_device *dev)
  396. {
  397. const struct macvlan_dev *vlan = netdev_priv(dev);
  398. const struct macvlan_port *port = vlan->port;
  399. const struct macvlan_dev *dest;
  400. if (vlan->mode == MACVLAN_MODE_BRIDGE) {
  401. const struct ethhdr *eth = (void *)skb->data;
  402. /* send to other bridge ports directly */
  403. if (is_multicast_ether_addr(eth->h_dest)) {
  404. macvlan_broadcast(skb, port, dev, MACVLAN_MODE_BRIDGE);
  405. goto xmit_world;
  406. }
  407. dest = macvlan_hash_lookup(port, eth->h_dest);
  408. if (dest && dest->mode == MACVLAN_MODE_BRIDGE) {
  409. /* send to lowerdev first for its network taps */
  410. dev_forward_skb(vlan->lowerdev, skb);
  411. return NET_XMIT_SUCCESS;
  412. }
  413. }
  414. xmit_world:
  415. skb->dev = vlan->lowerdev;
  416. return dev_queue_xmit(skb);
  417. }
  418. static inline netdev_tx_t macvlan_netpoll_send_skb(struct macvlan_dev *vlan, struct sk_buff *skb)
  419. {
  420. #ifdef CONFIG_NET_POLL_CONTROLLER
  421. if (vlan->netpoll)
  422. netpoll_send_skb(vlan->netpoll, skb);
  423. #else
  424. BUG();
  425. #endif
  426. return NETDEV_TX_OK;
  427. }
  428. static netdev_tx_t macvlan_start_xmit(struct sk_buff *skb,
  429. struct net_device *dev)
  430. {
  431. unsigned int len = skb->len;
  432. int ret;
  433. struct macvlan_dev *vlan = netdev_priv(dev);
  434. if (unlikely(netpoll_tx_running(dev)))
  435. return macvlan_netpoll_send_skb(vlan, skb);
  436. if (vlan->fwd_priv) {
  437. skb->dev = vlan->lowerdev;
  438. ret = dev_queue_xmit_accel(skb, vlan->fwd_priv);
  439. } else {
  440. ret = macvlan_queue_xmit(skb, dev);
  441. }
  442. if (likely(ret == NET_XMIT_SUCCESS || ret == NET_XMIT_CN)) {
  443. struct vlan_pcpu_stats *pcpu_stats;
  444. pcpu_stats = this_cpu_ptr(vlan->pcpu_stats);
  445. u64_stats_update_begin(&pcpu_stats->syncp);
  446. pcpu_stats->tx_packets++;
  447. pcpu_stats->tx_bytes += len;
  448. u64_stats_update_end(&pcpu_stats->syncp);
  449. } else {
  450. this_cpu_inc(vlan->pcpu_stats->tx_dropped);
  451. }
  452. return ret;
  453. }
  454. static int macvlan_hard_header(struct sk_buff *skb, struct net_device *dev,
  455. unsigned short type, const void *daddr,
  456. const void *saddr, unsigned len)
  457. {
  458. const struct macvlan_dev *vlan = netdev_priv(dev);
  459. struct net_device *lowerdev = vlan->lowerdev;
  460. return dev_hard_header(skb, lowerdev, type, daddr,
  461. saddr ? : dev->dev_addr, len);
  462. }
  463. static const struct header_ops macvlan_hard_header_ops = {
  464. .create = macvlan_hard_header,
  465. .parse = eth_header_parse,
  466. .cache = eth_header_cache,
  467. .cache_update = eth_header_cache_update,
  468. };
  469. static struct rtnl_link_ops macvlan_link_ops;
  470. static int macvlan_open(struct net_device *dev)
  471. {
  472. struct macvlan_dev *vlan = netdev_priv(dev);
  473. struct net_device *lowerdev = vlan->lowerdev;
  474. int err;
  475. if (vlan->port->passthru) {
  476. if (!(vlan->flags & MACVLAN_FLAG_NOPROMISC)) {
  477. err = dev_set_promiscuity(lowerdev, 1);
  478. if (err < 0)
  479. goto out;
  480. }
  481. goto hash_add;
  482. }
  483. if (lowerdev->features & NETIF_F_HW_L2FW_DOFFLOAD &&
  484. dev->rtnl_link_ops == &macvlan_link_ops) {
  485. vlan->fwd_priv =
  486. lowerdev->netdev_ops->ndo_dfwd_add_station(lowerdev, dev);
  487. /* If we get a NULL pointer back, or if we get an error
  488. * then we should just fall through to the non accelerated path
  489. */
  490. if (IS_ERR_OR_NULL(vlan->fwd_priv)) {
  491. vlan->fwd_priv = NULL;
  492. } else
  493. return 0;
  494. }
  495. err = -EBUSY;
  496. if (macvlan_addr_busy(vlan->port, dev->dev_addr))
  497. goto out;
  498. err = dev_uc_add(lowerdev, dev->dev_addr);
  499. if (err < 0)
  500. goto out;
  501. if (dev->flags & IFF_ALLMULTI) {
  502. err = dev_set_allmulti(lowerdev, 1);
  503. if (err < 0)
  504. goto del_unicast;
  505. }
  506. if (dev->flags & IFF_PROMISC) {
  507. err = dev_set_promiscuity(lowerdev, 1);
  508. if (err < 0)
  509. goto clear_multi;
  510. }
  511. hash_add:
  512. macvlan_hash_add(vlan);
  513. return 0;
  514. clear_multi:
  515. dev_set_allmulti(lowerdev, -1);
  516. del_unicast:
  517. dev_uc_del(lowerdev, dev->dev_addr);
  518. out:
  519. if (vlan->fwd_priv) {
  520. lowerdev->netdev_ops->ndo_dfwd_del_station(lowerdev,
  521. vlan->fwd_priv);
  522. vlan->fwd_priv = NULL;
  523. }
  524. return err;
  525. }
  526. static int macvlan_stop(struct net_device *dev)
  527. {
  528. struct macvlan_dev *vlan = netdev_priv(dev);
  529. struct net_device *lowerdev = vlan->lowerdev;
  530. if (vlan->fwd_priv) {
  531. lowerdev->netdev_ops->ndo_dfwd_del_station(lowerdev,
  532. vlan->fwd_priv);
  533. vlan->fwd_priv = NULL;
  534. return 0;
  535. }
  536. dev_uc_unsync(lowerdev, dev);
  537. dev_mc_unsync(lowerdev, dev);
  538. if (vlan->port->passthru) {
  539. if (!(vlan->flags & MACVLAN_FLAG_NOPROMISC))
  540. dev_set_promiscuity(lowerdev, -1);
  541. goto hash_del;
  542. }
  543. if (dev->flags & IFF_ALLMULTI)
  544. dev_set_allmulti(lowerdev, -1);
  545. if (dev->flags & IFF_PROMISC)
  546. dev_set_promiscuity(lowerdev, -1);
  547. dev_uc_del(lowerdev, dev->dev_addr);
  548. hash_del:
  549. macvlan_hash_del(vlan, !dev->dismantle);
  550. return 0;
  551. }
  552. static int macvlan_sync_address(struct net_device *dev, unsigned char *addr)
  553. {
  554. struct macvlan_dev *vlan = netdev_priv(dev);
  555. struct net_device *lowerdev = vlan->lowerdev;
  556. int err;
  557. if (!(dev->flags & IFF_UP)) {
  558. /* Just copy in the new address */
  559. ether_addr_copy(dev->dev_addr, addr);
  560. } else {
  561. /* Rehash and update the device filters */
  562. if (macvlan_addr_busy(vlan->port, addr))
  563. return -EBUSY;
  564. if (!vlan->port->passthru) {
  565. err = dev_uc_add(lowerdev, addr);
  566. if (err)
  567. return err;
  568. dev_uc_del(lowerdev, dev->dev_addr);
  569. }
  570. macvlan_hash_change_addr(vlan, addr);
  571. }
  572. return 0;
  573. }
  574. static int macvlan_set_mac_address(struct net_device *dev, void *p)
  575. {
  576. struct macvlan_dev *vlan = netdev_priv(dev);
  577. struct sockaddr *addr = p;
  578. if (!is_valid_ether_addr(addr->sa_data))
  579. return -EADDRNOTAVAIL;
  580. if (vlan->mode == MACVLAN_MODE_PASSTHRU) {
  581. dev_set_mac_address(vlan->lowerdev, addr);
  582. return 0;
  583. }
  584. return macvlan_sync_address(dev, addr->sa_data);
  585. }
  586. static void macvlan_change_rx_flags(struct net_device *dev, int change)
  587. {
  588. struct macvlan_dev *vlan = netdev_priv(dev);
  589. struct net_device *lowerdev = vlan->lowerdev;
  590. if (dev->flags & IFF_UP) {
  591. if (change & IFF_ALLMULTI)
  592. dev_set_allmulti(lowerdev, dev->flags & IFF_ALLMULTI ? 1 : -1);
  593. if (change & IFF_PROMISC)
  594. dev_set_promiscuity(lowerdev,
  595. dev->flags & IFF_PROMISC ? 1 : -1);
  596. }
  597. }
  598. static void macvlan_set_mac_lists(struct net_device *dev)
  599. {
  600. struct macvlan_dev *vlan = netdev_priv(dev);
  601. if (dev->flags & (IFF_PROMISC | IFF_ALLMULTI)) {
  602. bitmap_fill(vlan->mc_filter, MACVLAN_MC_FILTER_SZ);
  603. } else {
  604. struct netdev_hw_addr *ha;
  605. DECLARE_BITMAP(filter, MACVLAN_MC_FILTER_SZ);
  606. bitmap_zero(filter, MACVLAN_MC_FILTER_SZ);
  607. netdev_for_each_mc_addr(ha, dev) {
  608. __set_bit(mc_hash(vlan, ha->addr), filter);
  609. }
  610. __set_bit(mc_hash(vlan, dev->broadcast), filter);
  611. bitmap_copy(vlan->mc_filter, filter, MACVLAN_MC_FILTER_SZ);
  612. }
  613. dev_uc_sync(vlan->lowerdev, dev);
  614. dev_mc_sync(vlan->lowerdev, dev);
  615. }
  616. static int macvlan_change_mtu(struct net_device *dev, int new_mtu)
  617. {
  618. struct macvlan_dev *vlan = netdev_priv(dev);
  619. if (new_mtu < 68 || vlan->lowerdev->mtu < new_mtu)
  620. return -EINVAL;
  621. dev->mtu = new_mtu;
  622. return 0;
  623. }
  624. /*
  625. * macvlan network devices have devices nesting below it and are a special
  626. * "super class" of normal network devices; split their locks off into a
  627. * separate class since they always nest.
  628. */
  629. static struct lock_class_key macvlan_netdev_xmit_lock_key;
  630. static struct lock_class_key macvlan_netdev_addr_lock_key;
  631. #define ALWAYS_ON_FEATURES \
  632. (NETIF_F_SG | NETIF_F_GEN_CSUM | NETIF_F_GSO_SOFTWARE | NETIF_F_LLTX | \
  633. NETIF_F_GSO_ROBUST)
  634. #define MACVLAN_FEATURES \
  635. (NETIF_F_SG | NETIF_F_ALL_CSUM | NETIF_F_HIGHDMA | NETIF_F_FRAGLIST | \
  636. NETIF_F_GSO | NETIF_F_TSO | NETIF_F_UFO | NETIF_F_LRO | \
  637. NETIF_F_TSO_ECN | NETIF_F_TSO6 | NETIF_F_GRO | NETIF_F_RXCSUM | \
  638. NETIF_F_HW_VLAN_CTAG_FILTER | NETIF_F_HW_VLAN_STAG_FILTER)
  639. #define MACVLAN_STATE_MASK \
  640. ((1<<__LINK_STATE_NOCARRIER) | (1<<__LINK_STATE_DORMANT))
  641. static int macvlan_get_nest_level(struct net_device *dev)
  642. {
  643. return ((struct macvlan_dev *)netdev_priv(dev))->nest_level;
  644. }
  645. static void macvlan_set_lockdep_class_one(struct net_device *dev,
  646. struct netdev_queue *txq,
  647. void *_unused)
  648. {
  649. lockdep_set_class(&txq->_xmit_lock,
  650. &macvlan_netdev_xmit_lock_key);
  651. }
  652. static void macvlan_set_lockdep_class(struct net_device *dev)
  653. {
  654. lockdep_set_class_and_subclass(&dev->addr_list_lock,
  655. &macvlan_netdev_addr_lock_key,
  656. macvlan_get_nest_level(dev));
  657. netdev_for_each_tx_queue(dev, macvlan_set_lockdep_class_one, NULL);
  658. }
  659. static int macvlan_init(struct net_device *dev)
  660. {
  661. struct macvlan_dev *vlan = netdev_priv(dev);
  662. const struct net_device *lowerdev = vlan->lowerdev;
  663. dev->state = (dev->state & ~MACVLAN_STATE_MASK) |
  664. (lowerdev->state & MACVLAN_STATE_MASK);
  665. dev->features = lowerdev->features & MACVLAN_FEATURES;
  666. dev->features |= ALWAYS_ON_FEATURES;
  667. dev->hw_features |= NETIF_F_LRO;
  668. dev->vlan_features = lowerdev->vlan_features & MACVLAN_FEATURES;
  669. dev->gso_max_size = lowerdev->gso_max_size;
  670. dev->hard_header_len = lowerdev->hard_header_len;
  671. macvlan_set_lockdep_class(dev);
  672. vlan->pcpu_stats = netdev_alloc_pcpu_stats(struct vlan_pcpu_stats);
  673. if (!vlan->pcpu_stats)
  674. return -ENOMEM;
  675. return 0;
  676. }
  677. static void macvlan_uninit(struct net_device *dev)
  678. {
  679. struct macvlan_dev *vlan = netdev_priv(dev);
  680. struct macvlan_port *port = vlan->port;
  681. free_percpu(vlan->pcpu_stats);
  682. macvlan_flush_sources(port, vlan);
  683. port->count -= 1;
  684. if (!port->count)
  685. macvlan_port_destroy(port->dev);
  686. }
  687. static struct rtnl_link_stats64 *macvlan_dev_get_stats64(struct net_device *dev,
  688. struct rtnl_link_stats64 *stats)
  689. {
  690. struct macvlan_dev *vlan = netdev_priv(dev);
  691. if (vlan->pcpu_stats) {
  692. struct vlan_pcpu_stats *p;
  693. u64 rx_packets, rx_bytes, rx_multicast, tx_packets, tx_bytes;
  694. u32 rx_errors = 0, tx_dropped = 0;
  695. unsigned int start;
  696. int i;
  697. for_each_possible_cpu(i) {
  698. p = per_cpu_ptr(vlan->pcpu_stats, i);
  699. do {
  700. start = u64_stats_fetch_begin_irq(&p->syncp);
  701. rx_packets = p->rx_packets;
  702. rx_bytes = p->rx_bytes;
  703. rx_multicast = p->rx_multicast;
  704. tx_packets = p->tx_packets;
  705. tx_bytes = p->tx_bytes;
  706. } while (u64_stats_fetch_retry_irq(&p->syncp, start));
  707. stats->rx_packets += rx_packets;
  708. stats->rx_bytes += rx_bytes;
  709. stats->multicast += rx_multicast;
  710. stats->tx_packets += tx_packets;
  711. stats->tx_bytes += tx_bytes;
  712. /* rx_errors & tx_dropped are u32, updated
  713. * without syncp protection.
  714. */
  715. rx_errors += p->rx_errors;
  716. tx_dropped += p->tx_dropped;
  717. }
  718. stats->rx_errors = rx_errors;
  719. stats->rx_dropped = rx_errors;
  720. stats->tx_dropped = tx_dropped;
  721. }
  722. return stats;
  723. }
  724. static int macvlan_vlan_rx_add_vid(struct net_device *dev,
  725. __be16 proto, u16 vid)
  726. {
  727. struct macvlan_dev *vlan = netdev_priv(dev);
  728. struct net_device *lowerdev = vlan->lowerdev;
  729. return vlan_vid_add(lowerdev, proto, vid);
  730. }
  731. static int macvlan_vlan_rx_kill_vid(struct net_device *dev,
  732. __be16 proto, u16 vid)
  733. {
  734. struct macvlan_dev *vlan = netdev_priv(dev);
  735. struct net_device *lowerdev = vlan->lowerdev;
  736. vlan_vid_del(lowerdev, proto, vid);
  737. return 0;
  738. }
  739. static int macvlan_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
  740. struct net_device *dev,
  741. const unsigned char *addr, u16 vid,
  742. u16 flags)
  743. {
  744. struct macvlan_dev *vlan = netdev_priv(dev);
  745. int err = -EINVAL;
  746. /* Support unicast filter only on passthru devices.
  747. * Multicast filter should be allowed on all devices.
  748. */
  749. if (!vlan->port->passthru && is_unicast_ether_addr(addr))
  750. return -EOPNOTSUPP;
  751. if (flags & NLM_F_REPLACE)
  752. return -EOPNOTSUPP;
  753. if (is_unicast_ether_addr(addr))
  754. err = dev_uc_add_excl(dev, addr);
  755. else if (is_multicast_ether_addr(addr))
  756. err = dev_mc_add_excl(dev, addr);
  757. return err;
  758. }
  759. static int macvlan_fdb_del(struct ndmsg *ndm, struct nlattr *tb[],
  760. struct net_device *dev,
  761. const unsigned char *addr, u16 vid)
  762. {
  763. struct macvlan_dev *vlan = netdev_priv(dev);
  764. int err = -EINVAL;
  765. /* Support unicast filter only on passthru devices.
  766. * Multicast filter should be allowed on all devices.
  767. */
  768. if (!vlan->port->passthru && is_unicast_ether_addr(addr))
  769. return -EOPNOTSUPP;
  770. if (is_unicast_ether_addr(addr))
  771. err = dev_uc_del(dev, addr);
  772. else if (is_multicast_ether_addr(addr))
  773. err = dev_mc_del(dev, addr);
  774. return err;
  775. }
  776. static void macvlan_ethtool_get_drvinfo(struct net_device *dev,
  777. struct ethtool_drvinfo *drvinfo)
  778. {
  779. strlcpy(drvinfo->driver, "macvlan", sizeof(drvinfo->driver));
  780. strlcpy(drvinfo->version, "0.1", sizeof(drvinfo->version));
  781. }
  782. static int macvlan_ethtool_get_settings(struct net_device *dev,
  783. struct ethtool_cmd *cmd)
  784. {
  785. const struct macvlan_dev *vlan = netdev_priv(dev);
  786. return __ethtool_get_settings(vlan->lowerdev, cmd);
  787. }
  788. static netdev_features_t macvlan_fix_features(struct net_device *dev,
  789. netdev_features_t features)
  790. {
  791. struct macvlan_dev *vlan = netdev_priv(dev);
  792. netdev_features_t lowerdev_features = vlan->lowerdev->features;
  793. netdev_features_t mask;
  794. features |= NETIF_F_ALL_FOR_ALL;
  795. features &= (vlan->set_features | ~MACVLAN_FEATURES);
  796. mask = features;
  797. lowerdev_features &= (features | ~NETIF_F_LRO);
  798. features = netdev_increment_features(lowerdev_features, features, mask);
  799. features |= ALWAYS_ON_FEATURES;
  800. features &= ~NETIF_F_NETNS_LOCAL;
  801. return features;
  802. }
  803. #ifdef CONFIG_NET_POLL_CONTROLLER
  804. static void macvlan_dev_poll_controller(struct net_device *dev)
  805. {
  806. return;
  807. }
  808. static int macvlan_dev_netpoll_setup(struct net_device *dev, struct netpoll_info *npinfo)
  809. {
  810. struct macvlan_dev *vlan = netdev_priv(dev);
  811. struct net_device *real_dev = vlan->lowerdev;
  812. struct netpoll *netpoll;
  813. int err = 0;
  814. netpoll = kzalloc(sizeof(*netpoll), GFP_KERNEL);
  815. err = -ENOMEM;
  816. if (!netpoll)
  817. goto out;
  818. err = __netpoll_setup(netpoll, real_dev);
  819. if (err) {
  820. kfree(netpoll);
  821. goto out;
  822. }
  823. vlan->netpoll = netpoll;
  824. out:
  825. return err;
  826. }
  827. static void macvlan_dev_netpoll_cleanup(struct net_device *dev)
  828. {
  829. struct macvlan_dev *vlan = netdev_priv(dev);
  830. struct netpoll *netpoll = vlan->netpoll;
  831. if (!netpoll)
  832. return;
  833. vlan->netpoll = NULL;
  834. __netpoll_free_async(netpoll);
  835. }
  836. #endif /* CONFIG_NET_POLL_CONTROLLER */
  837. static int macvlan_dev_get_iflink(const struct net_device *dev)
  838. {
  839. struct macvlan_dev *vlan = netdev_priv(dev);
  840. return vlan->lowerdev->ifindex;
  841. }
  842. static const struct ethtool_ops macvlan_ethtool_ops = {
  843. .get_link = ethtool_op_get_link,
  844. .get_settings = macvlan_ethtool_get_settings,
  845. .get_drvinfo = macvlan_ethtool_get_drvinfo,
  846. };
  847. static const struct net_device_ops macvlan_netdev_ops = {
  848. .ndo_init = macvlan_init,
  849. .ndo_uninit = macvlan_uninit,
  850. .ndo_open = macvlan_open,
  851. .ndo_stop = macvlan_stop,
  852. .ndo_start_xmit = macvlan_start_xmit,
  853. .ndo_change_mtu = macvlan_change_mtu,
  854. .ndo_fix_features = macvlan_fix_features,
  855. .ndo_change_rx_flags = macvlan_change_rx_flags,
  856. .ndo_set_mac_address = macvlan_set_mac_address,
  857. .ndo_set_rx_mode = macvlan_set_mac_lists,
  858. .ndo_get_stats64 = macvlan_dev_get_stats64,
  859. .ndo_validate_addr = eth_validate_addr,
  860. .ndo_vlan_rx_add_vid = macvlan_vlan_rx_add_vid,
  861. .ndo_vlan_rx_kill_vid = macvlan_vlan_rx_kill_vid,
  862. .ndo_fdb_add = macvlan_fdb_add,
  863. .ndo_fdb_del = macvlan_fdb_del,
  864. .ndo_fdb_dump = ndo_dflt_fdb_dump,
  865. .ndo_get_lock_subclass = macvlan_get_nest_level,
  866. #ifdef CONFIG_NET_POLL_CONTROLLER
  867. .ndo_poll_controller = macvlan_dev_poll_controller,
  868. .ndo_netpoll_setup = macvlan_dev_netpoll_setup,
  869. .ndo_netpoll_cleanup = macvlan_dev_netpoll_cleanup,
  870. #endif
  871. .ndo_get_iflink = macvlan_dev_get_iflink,
  872. .ndo_features_check = passthru_features_check,
  873. };
  874. void macvlan_common_setup(struct net_device *dev)
  875. {
  876. ether_setup(dev);
  877. dev->priv_flags &= ~IFF_TX_SKB_SHARING;
  878. netif_keep_dst(dev);
  879. dev->priv_flags |= IFF_UNICAST_FLT;
  880. dev->netdev_ops = &macvlan_netdev_ops;
  881. dev->destructor = free_netdev;
  882. dev->header_ops = &macvlan_hard_header_ops;
  883. dev->ethtool_ops = &macvlan_ethtool_ops;
  884. }
  885. EXPORT_SYMBOL_GPL(macvlan_common_setup);
  886. static void macvlan_setup(struct net_device *dev)
  887. {
  888. macvlan_common_setup(dev);
  889. dev->tx_queue_len = 0;
  890. }
  891. static int macvlan_port_create(struct net_device *dev)
  892. {
  893. struct macvlan_port *port;
  894. unsigned int i;
  895. int err;
  896. if (dev->type != ARPHRD_ETHER || dev->flags & IFF_LOOPBACK)
  897. return -EINVAL;
  898. if (netif_is_ipvlan_port(dev))
  899. return -EBUSY;
  900. port = kzalloc(sizeof(*port), GFP_KERNEL);
  901. if (port == NULL)
  902. return -ENOMEM;
  903. port->passthru = false;
  904. port->dev = dev;
  905. INIT_LIST_HEAD(&port->vlans);
  906. for (i = 0; i < MACVLAN_HASH_SIZE; i++)
  907. INIT_HLIST_HEAD(&port->vlan_hash[i]);
  908. for (i = 0; i < MACVLAN_HASH_SIZE; i++)
  909. INIT_HLIST_HEAD(&port->vlan_source_hash[i]);
  910. skb_queue_head_init(&port->bc_queue);
  911. INIT_WORK(&port->bc_work, macvlan_process_broadcast);
  912. err = netdev_rx_handler_register(dev, macvlan_handle_frame, port);
  913. if (err)
  914. kfree(port);
  915. else
  916. dev->priv_flags |= IFF_MACVLAN_PORT;
  917. return err;
  918. }
  919. static void macvlan_port_destroy(struct net_device *dev)
  920. {
  921. struct macvlan_port *port = macvlan_port_get_rtnl(dev);
  922. struct sk_buff *skb;
  923. dev->priv_flags &= ~IFF_MACVLAN_PORT;
  924. netdev_rx_handler_unregister(dev);
  925. /* After this point, no packet can schedule bc_work anymore,
  926. * but we need to cancel it and purge left skbs if any.
  927. */
  928. cancel_work_sync(&port->bc_work);
  929. while ((skb = __skb_dequeue(&port->bc_queue))) {
  930. const struct macvlan_dev *src = MACVLAN_SKB_CB(skb)->src;
  931. if (src)
  932. dev_put(src->dev);
  933. kfree_skb(skb);
  934. }
  935. kfree_rcu(port, rcu);
  936. }
  937. static int macvlan_validate(struct nlattr *tb[], struct nlattr *data[])
  938. {
  939. if (tb[IFLA_ADDRESS]) {
  940. if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
  941. return -EINVAL;
  942. if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
  943. return -EADDRNOTAVAIL;
  944. }
  945. if (data && data[IFLA_MACVLAN_FLAGS] &&
  946. nla_get_u16(data[IFLA_MACVLAN_FLAGS]) & ~MACVLAN_FLAG_NOPROMISC)
  947. return -EINVAL;
  948. if (data && data[IFLA_MACVLAN_MODE]) {
  949. switch (nla_get_u32(data[IFLA_MACVLAN_MODE])) {
  950. case MACVLAN_MODE_PRIVATE:
  951. case MACVLAN_MODE_VEPA:
  952. case MACVLAN_MODE_BRIDGE:
  953. case MACVLAN_MODE_PASSTHRU:
  954. case MACVLAN_MODE_SOURCE:
  955. break;
  956. default:
  957. return -EINVAL;
  958. }
  959. }
  960. if (data && data[IFLA_MACVLAN_MACADDR_MODE]) {
  961. switch (nla_get_u32(data[IFLA_MACVLAN_MACADDR_MODE])) {
  962. case MACVLAN_MACADDR_ADD:
  963. case MACVLAN_MACADDR_DEL:
  964. case MACVLAN_MACADDR_FLUSH:
  965. case MACVLAN_MACADDR_SET:
  966. break;
  967. default:
  968. return -EINVAL;
  969. }
  970. }
  971. if (data && data[IFLA_MACVLAN_MACADDR]) {
  972. if (nla_len(data[IFLA_MACVLAN_MACADDR]) != ETH_ALEN)
  973. return -EINVAL;
  974. if (!is_valid_ether_addr(nla_data(data[IFLA_MACVLAN_MACADDR])))
  975. return -EADDRNOTAVAIL;
  976. }
  977. if (data && data[IFLA_MACVLAN_MACADDR_COUNT])
  978. return -EINVAL;
  979. return 0;
  980. }
  981. /**
  982. * reconfigure list of remote source mac address
  983. * (only for macvlan devices in source mode)
  984. * Note regarding alignment: all netlink data is aligned to 4 Byte, which
  985. * suffices for both ether_addr_copy and ether_addr_equal_64bits usage.
  986. */
  987. static int macvlan_changelink_sources(struct macvlan_dev *vlan, u32 mode,
  988. struct nlattr *data[])
  989. {
  990. char *addr = NULL;
  991. int ret, rem, len;
  992. struct nlattr *nla, *head;
  993. struct macvlan_source_entry *entry;
  994. if (data[IFLA_MACVLAN_MACADDR])
  995. addr = nla_data(data[IFLA_MACVLAN_MACADDR]);
  996. if (mode == MACVLAN_MACADDR_ADD) {
  997. if (!addr)
  998. return -EINVAL;
  999. return macvlan_hash_add_source(vlan, addr);
  1000. } else if (mode == MACVLAN_MACADDR_DEL) {
  1001. if (!addr)
  1002. return -EINVAL;
  1003. entry = macvlan_hash_lookup_source(vlan, addr);
  1004. if (entry) {
  1005. macvlan_hash_del_source(entry);
  1006. vlan->macaddr_count--;
  1007. }
  1008. } else if (mode == MACVLAN_MACADDR_FLUSH) {
  1009. macvlan_flush_sources(vlan->port, vlan);
  1010. } else if (mode == MACVLAN_MACADDR_SET) {
  1011. macvlan_flush_sources(vlan->port, vlan);
  1012. if (addr) {
  1013. ret = macvlan_hash_add_source(vlan, addr);
  1014. if (ret)
  1015. return ret;
  1016. }
  1017. if (!data || !data[IFLA_MACVLAN_MACADDR_DATA])
  1018. return 0;
  1019. head = nla_data(data[IFLA_MACVLAN_MACADDR_DATA]);
  1020. len = nla_len(data[IFLA_MACVLAN_MACADDR_DATA]);
  1021. nla_for_each_attr(nla, head, len, rem) {
  1022. if (nla_type(nla) != IFLA_MACVLAN_MACADDR ||
  1023. nla_len(nla) != ETH_ALEN)
  1024. continue;
  1025. addr = nla_data(nla);
  1026. ret = macvlan_hash_add_source(vlan, addr);
  1027. if (ret)
  1028. return ret;
  1029. }
  1030. } else {
  1031. return -EINVAL;
  1032. }
  1033. return 0;
  1034. }
  1035. int macvlan_common_newlink(struct net *src_net, struct net_device *dev,
  1036. struct nlattr *tb[], struct nlattr *data[])
  1037. {
  1038. struct macvlan_dev *vlan = netdev_priv(dev);
  1039. struct macvlan_port *port;
  1040. struct net_device *lowerdev;
  1041. int err;
  1042. int macmode;
  1043. if (!tb[IFLA_LINK])
  1044. return -EINVAL;
  1045. lowerdev = __dev_get_by_index(src_net, nla_get_u32(tb[IFLA_LINK]));
  1046. if (lowerdev == NULL)
  1047. return -ENODEV;
  1048. /* When creating macvlans or macvtaps on top of other macvlans - use
  1049. * the real device as the lowerdev.
  1050. */
  1051. if (netif_is_macvlan(lowerdev))
  1052. lowerdev = macvlan_dev_real_dev(lowerdev);
  1053. if (!tb[IFLA_MTU])
  1054. dev->mtu = lowerdev->mtu;
  1055. else if (dev->mtu > lowerdev->mtu)
  1056. return -EINVAL;
  1057. if (!tb[IFLA_ADDRESS])
  1058. eth_hw_addr_random(dev);
  1059. if (!macvlan_port_exists(lowerdev)) {
  1060. err = macvlan_port_create(lowerdev);
  1061. if (err < 0)
  1062. return err;
  1063. }
  1064. port = macvlan_port_get_rtnl(lowerdev);
  1065. /* Only 1 macvlan device can be created in passthru mode */
  1066. if (port->passthru)
  1067. return -EINVAL;
  1068. vlan->lowerdev = lowerdev;
  1069. vlan->dev = dev;
  1070. vlan->port = port;
  1071. vlan->set_features = MACVLAN_FEATURES;
  1072. vlan->nest_level = dev_get_nest_level(lowerdev, netif_is_macvlan) + 1;
  1073. vlan->mode = MACVLAN_MODE_VEPA;
  1074. if (data && data[IFLA_MACVLAN_MODE])
  1075. vlan->mode = nla_get_u32(data[IFLA_MACVLAN_MODE]);
  1076. if (data && data[IFLA_MACVLAN_FLAGS])
  1077. vlan->flags = nla_get_u16(data[IFLA_MACVLAN_FLAGS]);
  1078. if (vlan->mode == MACVLAN_MODE_PASSTHRU) {
  1079. if (port->count)
  1080. return -EINVAL;
  1081. port->passthru = true;
  1082. eth_hw_addr_inherit(dev, lowerdev);
  1083. }
  1084. if (data && data[IFLA_MACVLAN_MACADDR_MODE]) {
  1085. if (vlan->mode != MACVLAN_MODE_SOURCE)
  1086. return -EINVAL;
  1087. macmode = nla_get_u32(data[IFLA_MACVLAN_MACADDR_MODE]);
  1088. err = macvlan_changelink_sources(vlan, macmode, data);
  1089. if (err)
  1090. return err;
  1091. }
  1092. port->count += 1;
  1093. err = register_netdevice(dev);
  1094. if (err < 0)
  1095. goto destroy_port;
  1096. dev->priv_flags |= IFF_MACVLAN;
  1097. err = netdev_upper_dev_link(lowerdev, dev);
  1098. if (err)
  1099. goto unregister_netdev;
  1100. list_add_tail_rcu(&vlan->list, &port->vlans);
  1101. netif_stacked_transfer_operstate(lowerdev, dev);
  1102. return 0;
  1103. unregister_netdev:
  1104. unregister_netdevice(dev);
  1105. destroy_port:
  1106. port->count -= 1;
  1107. if (!port->count)
  1108. macvlan_port_destroy(lowerdev);
  1109. return err;
  1110. }
  1111. EXPORT_SYMBOL_GPL(macvlan_common_newlink);
  1112. static int macvlan_newlink(struct net *src_net, struct net_device *dev,
  1113. struct nlattr *tb[], struct nlattr *data[])
  1114. {
  1115. return macvlan_common_newlink(src_net, dev, tb, data);
  1116. }
  1117. void macvlan_dellink(struct net_device *dev, struct list_head *head)
  1118. {
  1119. struct macvlan_dev *vlan = netdev_priv(dev);
  1120. if (vlan->mode == MACVLAN_MODE_SOURCE)
  1121. macvlan_flush_sources(vlan->port, vlan);
  1122. list_del_rcu(&vlan->list);
  1123. unregister_netdevice_queue(dev, head);
  1124. netdev_upper_dev_unlink(vlan->lowerdev, dev);
  1125. }
  1126. EXPORT_SYMBOL_GPL(macvlan_dellink);
  1127. static int macvlan_changelink(struct net_device *dev,
  1128. struct nlattr *tb[], struct nlattr *data[])
  1129. {
  1130. struct macvlan_dev *vlan = netdev_priv(dev);
  1131. enum macvlan_mode mode;
  1132. bool set_mode = false;
  1133. enum macvlan_macaddr_mode macmode;
  1134. int ret;
  1135. /* Validate mode, but don't set yet: setting flags may fail. */
  1136. if (data && data[IFLA_MACVLAN_MODE]) {
  1137. set_mode = true;
  1138. mode = nla_get_u32(data[IFLA_MACVLAN_MODE]);
  1139. /* Passthrough mode can't be set or cleared dynamically */
  1140. if ((mode == MACVLAN_MODE_PASSTHRU) !=
  1141. (vlan->mode == MACVLAN_MODE_PASSTHRU))
  1142. return -EINVAL;
  1143. if (vlan->mode == MACVLAN_MODE_SOURCE &&
  1144. vlan->mode != mode)
  1145. macvlan_flush_sources(vlan->port, vlan);
  1146. }
  1147. if (data && data[IFLA_MACVLAN_FLAGS]) {
  1148. __u16 flags = nla_get_u16(data[IFLA_MACVLAN_FLAGS]);
  1149. bool promisc = (flags ^ vlan->flags) & MACVLAN_FLAG_NOPROMISC;
  1150. if (vlan->port->passthru && promisc) {
  1151. int err;
  1152. if (flags & MACVLAN_FLAG_NOPROMISC)
  1153. err = dev_set_promiscuity(vlan->lowerdev, -1);
  1154. else
  1155. err = dev_set_promiscuity(vlan->lowerdev, 1);
  1156. if (err < 0)
  1157. return err;
  1158. }
  1159. vlan->flags = flags;
  1160. }
  1161. if (set_mode)
  1162. vlan->mode = mode;
  1163. if (data && data[IFLA_MACVLAN_MACADDR_MODE]) {
  1164. if (vlan->mode != MACVLAN_MODE_SOURCE)
  1165. return -EINVAL;
  1166. macmode = nla_get_u32(data[IFLA_MACVLAN_MACADDR_MODE]);
  1167. ret = macvlan_changelink_sources(vlan, macmode, data);
  1168. if (ret)
  1169. return ret;
  1170. }
  1171. return 0;
  1172. }
  1173. static size_t macvlan_get_size_mac(const struct macvlan_dev *vlan)
  1174. {
  1175. if (vlan->macaddr_count == 0)
  1176. return 0;
  1177. return nla_total_size(0) /* IFLA_MACVLAN_MACADDR_DATA */
  1178. + vlan->macaddr_count * nla_total_size(sizeof(u8) * ETH_ALEN);
  1179. }
  1180. static size_t macvlan_get_size(const struct net_device *dev)
  1181. {
  1182. struct macvlan_dev *vlan = netdev_priv(dev);
  1183. return (0
  1184. + nla_total_size(4) /* IFLA_MACVLAN_MODE */
  1185. + nla_total_size(2) /* IFLA_MACVLAN_FLAGS */
  1186. + nla_total_size(4) /* IFLA_MACVLAN_MACADDR_COUNT */
  1187. + macvlan_get_size_mac(vlan) /* IFLA_MACVLAN_MACADDR */
  1188. );
  1189. }
  1190. static int macvlan_fill_info_macaddr(struct sk_buff *skb,
  1191. const struct macvlan_dev *vlan,
  1192. const int i)
  1193. {
  1194. struct hlist_head *h = &vlan->port->vlan_source_hash[i];
  1195. struct macvlan_source_entry *entry;
  1196. hlist_for_each_entry_rcu(entry, h, hlist) {
  1197. if (entry->vlan != vlan)
  1198. continue;
  1199. if (nla_put(skb, IFLA_MACVLAN_MACADDR, ETH_ALEN, entry->addr))
  1200. return 1;
  1201. }
  1202. return 0;
  1203. }
  1204. static int macvlan_fill_info(struct sk_buff *skb,
  1205. const struct net_device *dev)
  1206. {
  1207. struct macvlan_dev *vlan = netdev_priv(dev);
  1208. int i;
  1209. struct nlattr *nest;
  1210. if (nla_put_u32(skb, IFLA_MACVLAN_MODE, vlan->mode))
  1211. goto nla_put_failure;
  1212. if (nla_put_u16(skb, IFLA_MACVLAN_FLAGS, vlan->flags))
  1213. goto nla_put_failure;
  1214. if (nla_put_u32(skb, IFLA_MACVLAN_MACADDR_COUNT, vlan->macaddr_count))
  1215. goto nla_put_failure;
  1216. if (vlan->macaddr_count > 0) {
  1217. nest = nla_nest_start(skb, IFLA_MACVLAN_MACADDR_DATA);
  1218. if (nest == NULL)
  1219. goto nla_put_failure;
  1220. for (i = 0; i < MACVLAN_HASH_SIZE; i++) {
  1221. if (macvlan_fill_info_macaddr(skb, vlan, i))
  1222. goto nla_put_failure;
  1223. }
  1224. nla_nest_end(skb, nest);
  1225. }
  1226. return 0;
  1227. nla_put_failure:
  1228. return -EMSGSIZE;
  1229. }
  1230. static const struct nla_policy macvlan_policy[IFLA_MACVLAN_MAX + 1] = {
  1231. [IFLA_MACVLAN_MODE] = { .type = NLA_U32 },
  1232. [IFLA_MACVLAN_FLAGS] = { .type = NLA_U16 },
  1233. [IFLA_MACVLAN_MACADDR_MODE] = { .type = NLA_U32 },
  1234. [IFLA_MACVLAN_MACADDR] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
  1235. [IFLA_MACVLAN_MACADDR_DATA] = { .type = NLA_NESTED },
  1236. [IFLA_MACVLAN_MACADDR_COUNT] = { .type = NLA_U32 },
  1237. };
  1238. int macvlan_link_register(struct rtnl_link_ops *ops)
  1239. {
  1240. /* common fields */
  1241. ops->priv_size = sizeof(struct macvlan_dev);
  1242. ops->validate = macvlan_validate;
  1243. ops->maxtype = IFLA_MACVLAN_MAX;
  1244. ops->policy = macvlan_policy;
  1245. ops->changelink = macvlan_changelink;
  1246. ops->get_size = macvlan_get_size;
  1247. ops->fill_info = macvlan_fill_info;
  1248. return rtnl_link_register(ops);
  1249. };
  1250. EXPORT_SYMBOL_GPL(macvlan_link_register);
  1251. static struct net *macvlan_get_link_net(const struct net_device *dev)
  1252. {
  1253. return dev_net(macvlan_dev_real_dev(dev));
  1254. }
  1255. static struct rtnl_link_ops macvlan_link_ops = {
  1256. .kind = "macvlan",
  1257. .setup = macvlan_setup,
  1258. .newlink = macvlan_newlink,
  1259. .dellink = macvlan_dellink,
  1260. .get_link_net = macvlan_get_link_net,
  1261. };
  1262. static int macvlan_device_event(struct notifier_block *unused,
  1263. unsigned long event, void *ptr)
  1264. {
  1265. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  1266. struct macvlan_dev *vlan, *next;
  1267. struct macvlan_port *port;
  1268. LIST_HEAD(list_kill);
  1269. if (!macvlan_port_exists(dev))
  1270. return NOTIFY_DONE;
  1271. port = macvlan_port_get_rtnl(dev);
  1272. switch (event) {
  1273. case NETDEV_CHANGE:
  1274. list_for_each_entry(vlan, &port->vlans, list)
  1275. netif_stacked_transfer_operstate(vlan->lowerdev,
  1276. vlan->dev);
  1277. break;
  1278. case NETDEV_FEAT_CHANGE:
  1279. list_for_each_entry(vlan, &port->vlans, list) {
  1280. vlan->dev->gso_max_size = dev->gso_max_size;
  1281. netdev_update_features(vlan->dev);
  1282. }
  1283. break;
  1284. case NETDEV_CHANGEMTU:
  1285. list_for_each_entry(vlan, &port->vlans, list) {
  1286. if (vlan->dev->mtu <= dev->mtu)
  1287. continue;
  1288. dev_set_mtu(vlan->dev, dev->mtu);
  1289. }
  1290. break;
  1291. case NETDEV_CHANGEADDR:
  1292. if (!port->passthru)
  1293. return NOTIFY_DONE;
  1294. vlan = list_first_entry_or_null(&port->vlans,
  1295. struct macvlan_dev,
  1296. list);
  1297. if (macvlan_sync_address(vlan->dev, dev->dev_addr))
  1298. return NOTIFY_BAD;
  1299. break;
  1300. case NETDEV_UNREGISTER:
  1301. /* twiddle thumbs on netns device moves */
  1302. if (dev->reg_state != NETREG_UNREGISTERING)
  1303. break;
  1304. list_for_each_entry_safe(vlan, next, &port->vlans, list)
  1305. vlan->dev->rtnl_link_ops->dellink(vlan->dev, &list_kill);
  1306. unregister_netdevice_many(&list_kill);
  1307. break;
  1308. case NETDEV_PRE_TYPE_CHANGE:
  1309. /* Forbid underlaying device to change its type. */
  1310. return NOTIFY_BAD;
  1311. case NETDEV_NOTIFY_PEERS:
  1312. case NETDEV_BONDING_FAILOVER:
  1313. case NETDEV_RESEND_IGMP:
  1314. /* Propagate to all vlans */
  1315. list_for_each_entry(vlan, &port->vlans, list)
  1316. call_netdevice_notifiers(event, vlan->dev);
  1317. }
  1318. return NOTIFY_DONE;
  1319. }
  1320. static struct notifier_block macvlan_notifier_block __read_mostly = {
  1321. .notifier_call = macvlan_device_event,
  1322. };
  1323. static int __init macvlan_init_module(void)
  1324. {
  1325. int err;
  1326. register_netdevice_notifier(&macvlan_notifier_block);
  1327. err = macvlan_link_register(&macvlan_link_ops);
  1328. if (err < 0)
  1329. goto err1;
  1330. return 0;
  1331. err1:
  1332. unregister_netdevice_notifier(&macvlan_notifier_block);
  1333. return err;
  1334. }
  1335. static void __exit macvlan_cleanup_module(void)
  1336. {
  1337. rtnl_link_unregister(&macvlan_link_ops);
  1338. unregister_netdevice_notifier(&macvlan_notifier_block);
  1339. }
  1340. module_init(macvlan_init_module);
  1341. module_exit(macvlan_cleanup_module);
  1342. MODULE_LICENSE("GPL");
  1343. MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
  1344. MODULE_DESCRIPTION("Driver for MAC address based VLANs");
  1345. MODULE_ALIAS_RTNL_LINK("macvlan");