macvlan.c 44 KB

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