ipvlan_main.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132
  1. /* Copyright (c) 2014 Mahesh Bandewar <maheshb@google.com>
  2. *
  3. * This program is free software; you can redistribute it and/or
  4. * modify it under the terms of the GNU General Public License as
  5. * published by the Free Software Foundation; either version 2 of
  6. * the License, or (at your option) any later version.
  7. *
  8. */
  9. #include "ipvlan.h"
  10. static unsigned int ipvlan_netid __read_mostly;
  11. struct ipvlan_netns {
  12. unsigned int ipvl_nf_hook_refcnt;
  13. };
  14. static const struct nf_hook_ops ipvl_nfops[] = {
  15. {
  16. .hook = ipvlan_nf_input,
  17. .pf = NFPROTO_IPV4,
  18. .hooknum = NF_INET_LOCAL_IN,
  19. .priority = INT_MAX,
  20. },
  21. #if IS_ENABLED(CONFIG_IPV6)
  22. {
  23. .hook = ipvlan_nf_input,
  24. .pf = NFPROTO_IPV6,
  25. .hooknum = NF_INET_LOCAL_IN,
  26. .priority = INT_MAX,
  27. },
  28. #endif
  29. };
  30. static const struct l3mdev_ops ipvl_l3mdev_ops = {
  31. .l3mdev_l3_rcv = ipvlan_l3_rcv,
  32. };
  33. static void ipvlan_adjust_mtu(struct ipvl_dev *ipvlan, struct net_device *dev)
  34. {
  35. ipvlan->dev->mtu = dev->mtu;
  36. }
  37. static int ipvlan_register_nf_hook(struct net *net)
  38. {
  39. struct ipvlan_netns *vnet = net_generic(net, ipvlan_netid);
  40. int err = 0;
  41. if (!vnet->ipvl_nf_hook_refcnt) {
  42. err = nf_register_net_hooks(net, ipvl_nfops,
  43. ARRAY_SIZE(ipvl_nfops));
  44. if (!err)
  45. vnet->ipvl_nf_hook_refcnt = 1;
  46. } else {
  47. vnet->ipvl_nf_hook_refcnt++;
  48. }
  49. return err;
  50. }
  51. static void ipvlan_unregister_nf_hook(struct net *net)
  52. {
  53. struct ipvlan_netns *vnet = net_generic(net, ipvlan_netid);
  54. if (WARN_ON(!vnet->ipvl_nf_hook_refcnt))
  55. return;
  56. vnet->ipvl_nf_hook_refcnt--;
  57. if (!vnet->ipvl_nf_hook_refcnt)
  58. nf_unregister_net_hooks(net, ipvl_nfops,
  59. ARRAY_SIZE(ipvl_nfops));
  60. }
  61. static int ipvlan_set_port_mode(struct ipvl_port *port, u16 nval)
  62. {
  63. struct ipvl_dev *ipvlan;
  64. struct net_device *mdev = port->dev;
  65. unsigned int flags;
  66. int err;
  67. ASSERT_RTNL();
  68. if (port->mode != nval) {
  69. list_for_each_entry(ipvlan, &port->ipvlans, pnode) {
  70. flags = ipvlan->dev->flags;
  71. if (nval == IPVLAN_MODE_L3 || nval == IPVLAN_MODE_L3S) {
  72. err = dev_change_flags(ipvlan->dev,
  73. flags | IFF_NOARP);
  74. } else {
  75. err = dev_change_flags(ipvlan->dev,
  76. flags & ~IFF_NOARP);
  77. }
  78. if (unlikely(err))
  79. goto fail;
  80. }
  81. if (nval == IPVLAN_MODE_L3S) {
  82. /* New mode is L3S */
  83. err = ipvlan_register_nf_hook(read_pnet(&port->pnet));
  84. if (!err) {
  85. mdev->l3mdev_ops = &ipvl_l3mdev_ops;
  86. mdev->priv_flags |= IFF_L3MDEV_RX_HANDLER;
  87. } else
  88. goto fail;
  89. } else if (port->mode == IPVLAN_MODE_L3S) {
  90. /* Old mode was L3S */
  91. mdev->priv_flags &= ~IFF_L3MDEV_RX_HANDLER;
  92. ipvlan_unregister_nf_hook(read_pnet(&port->pnet));
  93. mdev->l3mdev_ops = NULL;
  94. }
  95. port->mode = nval;
  96. }
  97. return 0;
  98. fail:
  99. /* Undo the flags changes that have been done so far. */
  100. list_for_each_entry_continue_reverse(ipvlan, &port->ipvlans, pnode) {
  101. flags = ipvlan->dev->flags;
  102. if (port->mode == IPVLAN_MODE_L3 ||
  103. port->mode == IPVLAN_MODE_L3S)
  104. dev_change_flags(ipvlan->dev, flags | IFF_NOARP);
  105. else
  106. dev_change_flags(ipvlan->dev, flags & ~IFF_NOARP);
  107. }
  108. return err;
  109. }
  110. static int ipvlan_port_create(struct net_device *dev)
  111. {
  112. struct ipvl_port *port;
  113. int err, idx;
  114. port = kzalloc(sizeof(struct ipvl_port), GFP_KERNEL);
  115. if (!port)
  116. return -ENOMEM;
  117. write_pnet(&port->pnet, dev_net(dev));
  118. port->dev = dev;
  119. port->mode = IPVLAN_MODE_L3;
  120. INIT_LIST_HEAD(&port->ipvlans);
  121. for (idx = 0; idx < IPVLAN_HASH_SIZE; idx++)
  122. INIT_HLIST_HEAD(&port->hlhead[idx]);
  123. skb_queue_head_init(&port->backlog);
  124. INIT_WORK(&port->wq, ipvlan_process_multicast);
  125. ida_init(&port->ida);
  126. port->dev_id_start = 1;
  127. err = netdev_rx_handler_register(dev, ipvlan_handle_frame, port);
  128. if (err)
  129. goto err;
  130. return 0;
  131. err:
  132. kfree(port);
  133. return err;
  134. }
  135. static void ipvlan_port_destroy(struct net_device *dev)
  136. {
  137. struct ipvl_port *port = ipvlan_port_get_rtnl(dev);
  138. struct sk_buff *skb;
  139. if (port->mode == IPVLAN_MODE_L3S) {
  140. dev->priv_flags &= ~IFF_L3MDEV_RX_HANDLER;
  141. ipvlan_unregister_nf_hook(dev_net(dev));
  142. dev->l3mdev_ops = NULL;
  143. }
  144. netdev_rx_handler_unregister(dev);
  145. cancel_work_sync(&port->wq);
  146. while ((skb = __skb_dequeue(&port->backlog)) != NULL) {
  147. if (skb->dev)
  148. dev_put(skb->dev);
  149. kfree_skb(skb);
  150. }
  151. ida_destroy(&port->ida);
  152. kfree(port);
  153. }
  154. #define IPVLAN_FEATURES \
  155. (NETIF_F_SG | NETIF_F_CSUM_MASK | NETIF_F_HIGHDMA | NETIF_F_FRAGLIST | \
  156. NETIF_F_GSO | NETIF_F_TSO | NETIF_F_GSO_ROBUST | \
  157. NETIF_F_TSO_ECN | NETIF_F_TSO6 | NETIF_F_GRO | NETIF_F_RXCSUM | \
  158. NETIF_F_HW_VLAN_CTAG_FILTER | NETIF_F_HW_VLAN_STAG_FILTER)
  159. #define IPVLAN_STATE_MASK \
  160. ((1<<__LINK_STATE_NOCARRIER) | (1<<__LINK_STATE_DORMANT))
  161. static int ipvlan_init(struct net_device *dev)
  162. {
  163. struct ipvl_dev *ipvlan = netdev_priv(dev);
  164. struct net_device *phy_dev = ipvlan->phy_dev;
  165. struct ipvl_port *port;
  166. int err;
  167. dev->state = (dev->state & ~IPVLAN_STATE_MASK) |
  168. (phy_dev->state & IPVLAN_STATE_MASK);
  169. dev->features = phy_dev->features & IPVLAN_FEATURES;
  170. dev->features |= NETIF_F_LLTX | NETIF_F_VLAN_CHALLENGED;
  171. dev->gso_max_size = phy_dev->gso_max_size;
  172. dev->gso_max_segs = phy_dev->gso_max_segs;
  173. dev->hard_header_len = phy_dev->hard_header_len;
  174. netdev_lockdep_set_classes(dev);
  175. ipvlan->pcpu_stats = netdev_alloc_pcpu_stats(struct ipvl_pcpu_stats);
  176. if (!ipvlan->pcpu_stats)
  177. return -ENOMEM;
  178. if (!netif_is_ipvlan_port(phy_dev)) {
  179. err = ipvlan_port_create(phy_dev);
  180. if (err < 0) {
  181. free_percpu(ipvlan->pcpu_stats);
  182. return err;
  183. }
  184. }
  185. port = ipvlan_port_get_rtnl(phy_dev);
  186. port->count += 1;
  187. return 0;
  188. }
  189. static void ipvlan_uninit(struct net_device *dev)
  190. {
  191. struct ipvl_dev *ipvlan = netdev_priv(dev);
  192. struct net_device *phy_dev = ipvlan->phy_dev;
  193. struct ipvl_port *port;
  194. free_percpu(ipvlan->pcpu_stats);
  195. port = ipvlan_port_get_rtnl(phy_dev);
  196. port->count -= 1;
  197. if (!port->count)
  198. ipvlan_port_destroy(port->dev);
  199. }
  200. static int ipvlan_open(struct net_device *dev)
  201. {
  202. struct ipvl_dev *ipvlan = netdev_priv(dev);
  203. struct net_device *phy_dev = ipvlan->phy_dev;
  204. struct ipvl_addr *addr;
  205. if (ipvlan->port->mode == IPVLAN_MODE_L3 ||
  206. ipvlan->port->mode == IPVLAN_MODE_L3S)
  207. dev->flags |= IFF_NOARP;
  208. else
  209. dev->flags &= ~IFF_NOARP;
  210. rcu_read_lock();
  211. list_for_each_entry_rcu(addr, &ipvlan->addrs, anode)
  212. ipvlan_ht_addr_add(ipvlan, addr);
  213. rcu_read_unlock();
  214. return dev_uc_add(phy_dev, phy_dev->dev_addr);
  215. }
  216. static int ipvlan_stop(struct net_device *dev)
  217. {
  218. struct ipvl_dev *ipvlan = netdev_priv(dev);
  219. struct net_device *phy_dev = ipvlan->phy_dev;
  220. struct ipvl_addr *addr;
  221. dev_uc_unsync(phy_dev, dev);
  222. dev_mc_unsync(phy_dev, dev);
  223. dev_uc_del(phy_dev, phy_dev->dev_addr);
  224. rcu_read_lock();
  225. list_for_each_entry_rcu(addr, &ipvlan->addrs, anode)
  226. ipvlan_ht_addr_del(addr);
  227. rcu_read_unlock();
  228. return 0;
  229. }
  230. static netdev_tx_t ipvlan_start_xmit(struct sk_buff *skb,
  231. struct net_device *dev)
  232. {
  233. const struct ipvl_dev *ipvlan = netdev_priv(dev);
  234. int skblen = skb->len;
  235. int ret;
  236. ret = ipvlan_queue_xmit(skb, dev);
  237. if (likely(ret == NET_XMIT_SUCCESS || ret == NET_XMIT_CN)) {
  238. struct ipvl_pcpu_stats *pcptr;
  239. pcptr = this_cpu_ptr(ipvlan->pcpu_stats);
  240. u64_stats_update_begin(&pcptr->syncp);
  241. pcptr->tx_pkts++;
  242. pcptr->tx_bytes += skblen;
  243. u64_stats_update_end(&pcptr->syncp);
  244. } else {
  245. this_cpu_inc(ipvlan->pcpu_stats->tx_drps);
  246. }
  247. return ret;
  248. }
  249. static netdev_features_t ipvlan_fix_features(struct net_device *dev,
  250. netdev_features_t features)
  251. {
  252. struct ipvl_dev *ipvlan = netdev_priv(dev);
  253. return features & (ipvlan->sfeatures | ~IPVLAN_FEATURES);
  254. }
  255. static void ipvlan_change_rx_flags(struct net_device *dev, int change)
  256. {
  257. struct ipvl_dev *ipvlan = netdev_priv(dev);
  258. struct net_device *phy_dev = ipvlan->phy_dev;
  259. if (change & IFF_ALLMULTI)
  260. dev_set_allmulti(phy_dev, dev->flags & IFF_ALLMULTI? 1 : -1);
  261. }
  262. static void ipvlan_set_multicast_mac_filter(struct net_device *dev)
  263. {
  264. struct ipvl_dev *ipvlan = netdev_priv(dev);
  265. if (dev->flags & (IFF_PROMISC | IFF_ALLMULTI)) {
  266. bitmap_fill(ipvlan->mac_filters, IPVLAN_MAC_FILTER_SIZE);
  267. } else {
  268. struct netdev_hw_addr *ha;
  269. DECLARE_BITMAP(mc_filters, IPVLAN_MAC_FILTER_SIZE);
  270. bitmap_zero(mc_filters, IPVLAN_MAC_FILTER_SIZE);
  271. netdev_for_each_mc_addr(ha, dev)
  272. __set_bit(ipvlan_mac_hash(ha->addr), mc_filters);
  273. /* Turn-on broadcast bit irrespective of address family,
  274. * since broadcast is deferred to a work-queue, hence no
  275. * impact on fast-path processing.
  276. */
  277. __set_bit(ipvlan_mac_hash(dev->broadcast), mc_filters);
  278. bitmap_copy(ipvlan->mac_filters, mc_filters,
  279. IPVLAN_MAC_FILTER_SIZE);
  280. }
  281. dev_uc_sync(ipvlan->phy_dev, dev);
  282. dev_mc_sync(ipvlan->phy_dev, dev);
  283. }
  284. static void ipvlan_get_stats64(struct net_device *dev,
  285. struct rtnl_link_stats64 *s)
  286. {
  287. struct ipvl_dev *ipvlan = netdev_priv(dev);
  288. if (ipvlan->pcpu_stats) {
  289. struct ipvl_pcpu_stats *pcptr;
  290. u64 rx_pkts, rx_bytes, rx_mcast, tx_pkts, tx_bytes;
  291. u32 rx_errs = 0, tx_drps = 0;
  292. u32 strt;
  293. int idx;
  294. for_each_possible_cpu(idx) {
  295. pcptr = per_cpu_ptr(ipvlan->pcpu_stats, idx);
  296. do {
  297. strt= u64_stats_fetch_begin_irq(&pcptr->syncp);
  298. rx_pkts = pcptr->rx_pkts;
  299. rx_bytes = pcptr->rx_bytes;
  300. rx_mcast = pcptr->rx_mcast;
  301. tx_pkts = pcptr->tx_pkts;
  302. tx_bytes = pcptr->tx_bytes;
  303. } while (u64_stats_fetch_retry_irq(&pcptr->syncp,
  304. strt));
  305. s->rx_packets += rx_pkts;
  306. s->rx_bytes += rx_bytes;
  307. s->multicast += rx_mcast;
  308. s->tx_packets += tx_pkts;
  309. s->tx_bytes += tx_bytes;
  310. /* u32 values are updated without syncp protection. */
  311. rx_errs += pcptr->rx_errs;
  312. tx_drps += pcptr->tx_drps;
  313. }
  314. s->rx_errors = rx_errs;
  315. s->rx_dropped = rx_errs;
  316. s->tx_dropped = tx_drps;
  317. }
  318. }
  319. static int ipvlan_vlan_rx_add_vid(struct net_device *dev, __be16 proto, u16 vid)
  320. {
  321. struct ipvl_dev *ipvlan = netdev_priv(dev);
  322. struct net_device *phy_dev = ipvlan->phy_dev;
  323. return vlan_vid_add(phy_dev, proto, vid);
  324. }
  325. static int ipvlan_vlan_rx_kill_vid(struct net_device *dev, __be16 proto,
  326. u16 vid)
  327. {
  328. struct ipvl_dev *ipvlan = netdev_priv(dev);
  329. struct net_device *phy_dev = ipvlan->phy_dev;
  330. vlan_vid_del(phy_dev, proto, vid);
  331. return 0;
  332. }
  333. static int ipvlan_get_iflink(const struct net_device *dev)
  334. {
  335. struct ipvl_dev *ipvlan = netdev_priv(dev);
  336. return ipvlan->phy_dev->ifindex;
  337. }
  338. static const struct net_device_ops ipvlan_netdev_ops = {
  339. .ndo_init = ipvlan_init,
  340. .ndo_uninit = ipvlan_uninit,
  341. .ndo_open = ipvlan_open,
  342. .ndo_stop = ipvlan_stop,
  343. .ndo_start_xmit = ipvlan_start_xmit,
  344. .ndo_fix_features = ipvlan_fix_features,
  345. .ndo_change_rx_flags = ipvlan_change_rx_flags,
  346. .ndo_set_rx_mode = ipvlan_set_multicast_mac_filter,
  347. .ndo_get_stats64 = ipvlan_get_stats64,
  348. .ndo_vlan_rx_add_vid = ipvlan_vlan_rx_add_vid,
  349. .ndo_vlan_rx_kill_vid = ipvlan_vlan_rx_kill_vid,
  350. .ndo_get_iflink = ipvlan_get_iflink,
  351. };
  352. static int ipvlan_hard_header(struct sk_buff *skb, struct net_device *dev,
  353. unsigned short type, const void *daddr,
  354. const void *saddr, unsigned len)
  355. {
  356. const struct ipvl_dev *ipvlan = netdev_priv(dev);
  357. struct net_device *phy_dev = ipvlan->phy_dev;
  358. /* TODO Probably use a different field than dev_addr so that the
  359. * mac-address on the virtual device is portable and can be carried
  360. * while the packets use the mac-addr on the physical device.
  361. */
  362. return dev_hard_header(skb, phy_dev, type, daddr,
  363. saddr ? : phy_dev->dev_addr, len);
  364. }
  365. static const struct header_ops ipvlan_header_ops = {
  366. .create = ipvlan_hard_header,
  367. .parse = eth_header_parse,
  368. .cache = eth_header_cache,
  369. .cache_update = eth_header_cache_update,
  370. };
  371. static bool netif_is_ipvlan(const struct net_device *dev)
  372. {
  373. /* both ipvlan and ipvtap devices use the same netdev_ops */
  374. return dev->netdev_ops == &ipvlan_netdev_ops;
  375. }
  376. static int ipvlan_ethtool_get_link_ksettings(struct net_device *dev,
  377. struct ethtool_link_ksettings *cmd)
  378. {
  379. const struct ipvl_dev *ipvlan = netdev_priv(dev);
  380. return __ethtool_get_link_ksettings(ipvlan->phy_dev, cmd);
  381. }
  382. static void ipvlan_ethtool_get_drvinfo(struct net_device *dev,
  383. struct ethtool_drvinfo *drvinfo)
  384. {
  385. strlcpy(drvinfo->driver, IPVLAN_DRV, sizeof(drvinfo->driver));
  386. strlcpy(drvinfo->version, IPV_DRV_VER, sizeof(drvinfo->version));
  387. }
  388. static u32 ipvlan_ethtool_get_msglevel(struct net_device *dev)
  389. {
  390. const struct ipvl_dev *ipvlan = netdev_priv(dev);
  391. return ipvlan->msg_enable;
  392. }
  393. static void ipvlan_ethtool_set_msglevel(struct net_device *dev, u32 value)
  394. {
  395. struct ipvl_dev *ipvlan = netdev_priv(dev);
  396. ipvlan->msg_enable = value;
  397. }
  398. static const struct ethtool_ops ipvlan_ethtool_ops = {
  399. .get_link = ethtool_op_get_link,
  400. .get_link_ksettings = ipvlan_ethtool_get_link_ksettings,
  401. .get_drvinfo = ipvlan_ethtool_get_drvinfo,
  402. .get_msglevel = ipvlan_ethtool_get_msglevel,
  403. .set_msglevel = ipvlan_ethtool_set_msglevel,
  404. };
  405. static int ipvlan_nl_changelink(struct net_device *dev,
  406. struct nlattr *tb[], struct nlattr *data[],
  407. struct netlink_ext_ack *extack)
  408. {
  409. struct ipvl_dev *ipvlan = netdev_priv(dev);
  410. struct ipvl_port *port = ipvlan_port_get_rtnl(ipvlan->phy_dev);
  411. int err = 0;
  412. if (!data)
  413. return 0;
  414. if (!ns_capable(dev_net(ipvlan->phy_dev)->user_ns, CAP_NET_ADMIN))
  415. return -EPERM;
  416. if (data[IFLA_IPVLAN_MODE]) {
  417. u16 nmode = nla_get_u16(data[IFLA_IPVLAN_MODE]);
  418. err = ipvlan_set_port_mode(port, nmode);
  419. }
  420. if (!err && data[IFLA_IPVLAN_FLAGS]) {
  421. u16 flags = nla_get_u16(data[IFLA_IPVLAN_FLAGS]);
  422. if (flags & IPVLAN_F_PRIVATE)
  423. ipvlan_mark_private(port);
  424. else
  425. ipvlan_clear_private(port);
  426. if (flags & IPVLAN_F_VEPA)
  427. ipvlan_mark_vepa(port);
  428. else
  429. ipvlan_clear_vepa(port);
  430. }
  431. return err;
  432. }
  433. static size_t ipvlan_nl_getsize(const struct net_device *dev)
  434. {
  435. return (0
  436. + nla_total_size(2) /* IFLA_IPVLAN_MODE */
  437. + nla_total_size(2) /* IFLA_IPVLAN_FLAGS */
  438. );
  439. }
  440. static int ipvlan_nl_validate(struct nlattr *tb[], struct nlattr *data[],
  441. struct netlink_ext_ack *extack)
  442. {
  443. if (!data)
  444. return 0;
  445. if (data[IFLA_IPVLAN_MODE]) {
  446. u16 mode = nla_get_u16(data[IFLA_IPVLAN_MODE]);
  447. if (mode < IPVLAN_MODE_L2 || mode >= IPVLAN_MODE_MAX)
  448. return -EINVAL;
  449. }
  450. if (data[IFLA_IPVLAN_FLAGS]) {
  451. u16 flags = nla_get_u16(data[IFLA_IPVLAN_FLAGS]);
  452. /* Only two bits are used at this moment. */
  453. if (flags & ~(IPVLAN_F_PRIVATE | IPVLAN_F_VEPA))
  454. return -EINVAL;
  455. /* Also both flags can't be active at the same time. */
  456. if ((flags & (IPVLAN_F_PRIVATE | IPVLAN_F_VEPA)) ==
  457. (IPVLAN_F_PRIVATE | IPVLAN_F_VEPA))
  458. return -EINVAL;
  459. }
  460. return 0;
  461. }
  462. static int ipvlan_nl_fillinfo(struct sk_buff *skb,
  463. const struct net_device *dev)
  464. {
  465. struct ipvl_dev *ipvlan = netdev_priv(dev);
  466. struct ipvl_port *port = ipvlan_port_get_rtnl(ipvlan->phy_dev);
  467. int ret = -EINVAL;
  468. if (!port)
  469. goto err;
  470. ret = -EMSGSIZE;
  471. if (nla_put_u16(skb, IFLA_IPVLAN_MODE, port->mode))
  472. goto err;
  473. if (nla_put_u16(skb, IFLA_IPVLAN_FLAGS, port->flags))
  474. goto err;
  475. return 0;
  476. err:
  477. return ret;
  478. }
  479. int ipvlan_link_new(struct net *src_net, struct net_device *dev,
  480. struct nlattr *tb[], struct nlattr *data[],
  481. struct netlink_ext_ack *extack)
  482. {
  483. struct ipvl_dev *ipvlan = netdev_priv(dev);
  484. struct ipvl_port *port;
  485. struct net_device *phy_dev;
  486. int err;
  487. u16 mode = IPVLAN_MODE_L3;
  488. if (!tb[IFLA_LINK])
  489. return -EINVAL;
  490. phy_dev = __dev_get_by_index(src_net, nla_get_u32(tb[IFLA_LINK]));
  491. if (!phy_dev)
  492. return -ENODEV;
  493. if (netif_is_ipvlan(phy_dev)) {
  494. struct ipvl_dev *tmp = netdev_priv(phy_dev);
  495. phy_dev = tmp->phy_dev;
  496. if (!ns_capable(dev_net(phy_dev)->user_ns, CAP_NET_ADMIN))
  497. return -EPERM;
  498. } else if (!netif_is_ipvlan_port(phy_dev)) {
  499. /* Exit early if the underlying link is invalid or busy */
  500. if (phy_dev->type != ARPHRD_ETHER ||
  501. phy_dev->flags & IFF_LOOPBACK) {
  502. netdev_err(phy_dev,
  503. "Master is either lo or non-ether device\n");
  504. return -EINVAL;
  505. }
  506. if (netdev_is_rx_handler_busy(phy_dev)) {
  507. netdev_err(phy_dev, "Device is already in use.\n");
  508. return -EBUSY;
  509. }
  510. }
  511. ipvlan->phy_dev = phy_dev;
  512. ipvlan->dev = dev;
  513. ipvlan->sfeatures = IPVLAN_FEATURES;
  514. if (!tb[IFLA_MTU])
  515. ipvlan_adjust_mtu(ipvlan, phy_dev);
  516. INIT_LIST_HEAD(&ipvlan->addrs);
  517. spin_lock_init(&ipvlan->addrs_lock);
  518. /* TODO Probably put random address here to be presented to the
  519. * world but keep using the physical-dev address for the outgoing
  520. * packets.
  521. */
  522. memcpy(dev->dev_addr, phy_dev->dev_addr, ETH_ALEN);
  523. dev->priv_flags |= IFF_NO_RX_HANDLER;
  524. err = register_netdevice(dev);
  525. if (err < 0)
  526. return err;
  527. /* ipvlan_init() would have created the port, if required */
  528. port = ipvlan_port_get_rtnl(phy_dev);
  529. ipvlan->port = port;
  530. /* If the port-id base is at the MAX value, then wrap it around and
  531. * begin from 0x1 again. This may be due to a busy system where lots
  532. * of slaves are getting created and deleted.
  533. */
  534. if (port->dev_id_start == 0xFFFE)
  535. port->dev_id_start = 0x1;
  536. /* Since L2 address is shared among all IPvlan slaves including
  537. * master, use unique 16 bit dev-ids to diffentiate among them.
  538. * Assign IDs between 0x1 and 0xFFFE (used by the master) to each
  539. * slave link [see addrconf_ifid_eui48()].
  540. */
  541. err = ida_simple_get(&port->ida, port->dev_id_start, 0xFFFE,
  542. GFP_KERNEL);
  543. if (err < 0)
  544. err = ida_simple_get(&port->ida, 0x1, port->dev_id_start,
  545. GFP_KERNEL);
  546. if (err < 0)
  547. goto unregister_netdev;
  548. dev->dev_id = err;
  549. /* Increment id-base to the next slot for the future assignment */
  550. port->dev_id_start = err + 1;
  551. err = netdev_upper_dev_link(phy_dev, dev, extack);
  552. if (err)
  553. goto remove_ida;
  554. /* Flags are per port and latest update overrides. User has
  555. * to be consistent in setting it just like the mode attribute.
  556. */
  557. if (data && data[IFLA_IPVLAN_FLAGS])
  558. port->flags = nla_get_u16(data[IFLA_IPVLAN_FLAGS]);
  559. if (data && data[IFLA_IPVLAN_MODE])
  560. mode = nla_get_u16(data[IFLA_IPVLAN_MODE]);
  561. err = ipvlan_set_port_mode(port, mode);
  562. if (err)
  563. goto unlink_netdev;
  564. list_add_tail_rcu(&ipvlan->pnode, &port->ipvlans);
  565. netif_stacked_transfer_operstate(phy_dev, dev);
  566. return 0;
  567. unlink_netdev:
  568. netdev_upper_dev_unlink(phy_dev, dev);
  569. remove_ida:
  570. ida_simple_remove(&port->ida, dev->dev_id);
  571. unregister_netdev:
  572. unregister_netdevice(dev);
  573. return err;
  574. }
  575. EXPORT_SYMBOL_GPL(ipvlan_link_new);
  576. void ipvlan_link_delete(struct net_device *dev, struct list_head *head)
  577. {
  578. struct ipvl_dev *ipvlan = netdev_priv(dev);
  579. struct ipvl_addr *addr, *next;
  580. spin_lock_bh(&ipvlan->addrs_lock);
  581. list_for_each_entry_safe(addr, next, &ipvlan->addrs, anode) {
  582. ipvlan_ht_addr_del(addr);
  583. list_del_rcu(&addr->anode);
  584. kfree_rcu(addr, rcu);
  585. }
  586. spin_unlock_bh(&ipvlan->addrs_lock);
  587. ida_simple_remove(&ipvlan->port->ida, dev->dev_id);
  588. list_del_rcu(&ipvlan->pnode);
  589. unregister_netdevice_queue(dev, head);
  590. netdev_upper_dev_unlink(ipvlan->phy_dev, dev);
  591. }
  592. EXPORT_SYMBOL_GPL(ipvlan_link_delete);
  593. void ipvlan_link_setup(struct net_device *dev)
  594. {
  595. ether_setup(dev);
  596. dev->max_mtu = ETH_MAX_MTU;
  597. dev->priv_flags &= ~(IFF_XMIT_DST_RELEASE | IFF_TX_SKB_SHARING);
  598. dev->priv_flags |= IFF_UNICAST_FLT | IFF_NO_QUEUE;
  599. dev->netdev_ops = &ipvlan_netdev_ops;
  600. dev->needs_free_netdev = true;
  601. dev->header_ops = &ipvlan_header_ops;
  602. dev->ethtool_ops = &ipvlan_ethtool_ops;
  603. }
  604. EXPORT_SYMBOL_GPL(ipvlan_link_setup);
  605. static const struct nla_policy ipvlan_nl_policy[IFLA_IPVLAN_MAX + 1] =
  606. {
  607. [IFLA_IPVLAN_MODE] = { .type = NLA_U16 },
  608. [IFLA_IPVLAN_FLAGS] = { .type = NLA_U16 },
  609. };
  610. static struct rtnl_link_ops ipvlan_link_ops = {
  611. .kind = "ipvlan",
  612. .priv_size = sizeof(struct ipvl_dev),
  613. .setup = ipvlan_link_setup,
  614. .newlink = ipvlan_link_new,
  615. .dellink = ipvlan_link_delete,
  616. };
  617. int ipvlan_link_register(struct rtnl_link_ops *ops)
  618. {
  619. ops->get_size = ipvlan_nl_getsize;
  620. ops->policy = ipvlan_nl_policy;
  621. ops->validate = ipvlan_nl_validate;
  622. ops->fill_info = ipvlan_nl_fillinfo;
  623. ops->changelink = ipvlan_nl_changelink;
  624. ops->maxtype = IFLA_IPVLAN_MAX;
  625. return rtnl_link_register(ops);
  626. }
  627. EXPORT_SYMBOL_GPL(ipvlan_link_register);
  628. static int ipvlan_device_event(struct notifier_block *unused,
  629. unsigned long event, void *ptr)
  630. {
  631. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  632. struct ipvl_dev *ipvlan, *next;
  633. struct ipvl_port *port;
  634. LIST_HEAD(lst_kill);
  635. if (!netif_is_ipvlan_port(dev))
  636. return NOTIFY_DONE;
  637. port = ipvlan_port_get_rtnl(dev);
  638. switch (event) {
  639. case NETDEV_CHANGE:
  640. list_for_each_entry(ipvlan, &port->ipvlans, pnode)
  641. netif_stacked_transfer_operstate(ipvlan->phy_dev,
  642. ipvlan->dev);
  643. break;
  644. case NETDEV_REGISTER: {
  645. struct net *oldnet, *newnet = dev_net(dev);
  646. struct ipvlan_netns *old_vnet;
  647. oldnet = read_pnet(&port->pnet);
  648. if (net_eq(newnet, oldnet))
  649. break;
  650. write_pnet(&port->pnet, newnet);
  651. old_vnet = net_generic(oldnet, ipvlan_netid);
  652. if (!old_vnet->ipvl_nf_hook_refcnt)
  653. break;
  654. ipvlan_register_nf_hook(newnet);
  655. ipvlan_unregister_nf_hook(oldnet);
  656. break;
  657. }
  658. case NETDEV_UNREGISTER:
  659. if (dev->reg_state != NETREG_UNREGISTERING)
  660. break;
  661. list_for_each_entry_safe(ipvlan, next, &port->ipvlans, pnode)
  662. ipvlan->dev->rtnl_link_ops->dellink(ipvlan->dev,
  663. &lst_kill);
  664. unregister_netdevice_many(&lst_kill);
  665. break;
  666. case NETDEV_FEAT_CHANGE:
  667. list_for_each_entry(ipvlan, &port->ipvlans, pnode) {
  668. ipvlan->dev->features = dev->features & IPVLAN_FEATURES;
  669. ipvlan->dev->gso_max_size = dev->gso_max_size;
  670. ipvlan->dev->gso_max_segs = dev->gso_max_segs;
  671. netdev_features_change(ipvlan->dev);
  672. }
  673. break;
  674. case NETDEV_CHANGEMTU:
  675. list_for_each_entry(ipvlan, &port->ipvlans, pnode)
  676. ipvlan_adjust_mtu(ipvlan, dev);
  677. break;
  678. case NETDEV_CHANGEADDR:
  679. list_for_each_entry(ipvlan, &port->ipvlans, pnode) {
  680. ether_addr_copy(ipvlan->dev->dev_addr, dev->dev_addr);
  681. call_netdevice_notifiers(NETDEV_CHANGEADDR, ipvlan->dev);
  682. }
  683. break;
  684. case NETDEV_PRE_TYPE_CHANGE:
  685. /* Forbid underlying device to change its type. */
  686. return NOTIFY_BAD;
  687. }
  688. return NOTIFY_DONE;
  689. }
  690. /* the caller must held the addrs lock */
  691. static int ipvlan_add_addr(struct ipvl_dev *ipvlan, void *iaddr, bool is_v6)
  692. {
  693. struct ipvl_addr *addr;
  694. addr = kzalloc(sizeof(struct ipvl_addr), GFP_ATOMIC);
  695. if (!addr)
  696. return -ENOMEM;
  697. addr->master = ipvlan;
  698. if (!is_v6) {
  699. memcpy(&addr->ip4addr, iaddr, sizeof(struct in_addr));
  700. addr->atype = IPVL_IPV4;
  701. #if IS_ENABLED(CONFIG_IPV6)
  702. } else {
  703. memcpy(&addr->ip6addr, iaddr, sizeof(struct in6_addr));
  704. addr->atype = IPVL_IPV6;
  705. #endif
  706. }
  707. list_add_tail_rcu(&addr->anode, &ipvlan->addrs);
  708. /* If the interface is not up, the address will be added to the hash
  709. * list by ipvlan_open.
  710. */
  711. if (netif_running(ipvlan->dev))
  712. ipvlan_ht_addr_add(ipvlan, addr);
  713. return 0;
  714. }
  715. static void ipvlan_del_addr(struct ipvl_dev *ipvlan, void *iaddr, bool is_v6)
  716. {
  717. struct ipvl_addr *addr;
  718. spin_lock_bh(&ipvlan->addrs_lock);
  719. addr = ipvlan_find_addr(ipvlan, iaddr, is_v6);
  720. if (!addr) {
  721. spin_unlock_bh(&ipvlan->addrs_lock);
  722. return;
  723. }
  724. ipvlan_ht_addr_del(addr);
  725. list_del_rcu(&addr->anode);
  726. spin_unlock_bh(&ipvlan->addrs_lock);
  727. kfree_rcu(addr, rcu);
  728. }
  729. static bool ipvlan_is_valid_dev(const struct net_device *dev)
  730. {
  731. struct ipvl_dev *ipvlan = netdev_priv(dev);
  732. if (!netif_is_ipvlan(dev))
  733. return false;
  734. if (!ipvlan || !ipvlan->port)
  735. return false;
  736. return true;
  737. }
  738. #if IS_ENABLED(CONFIG_IPV6)
  739. static int ipvlan_add_addr6(struct ipvl_dev *ipvlan, struct in6_addr *ip6_addr)
  740. {
  741. int ret = -EINVAL;
  742. spin_lock_bh(&ipvlan->addrs_lock);
  743. if (ipvlan_addr_busy(ipvlan->port, ip6_addr, true))
  744. netif_err(ipvlan, ifup, ipvlan->dev,
  745. "Failed to add IPv6=%pI6c addr for %s intf\n",
  746. ip6_addr, ipvlan->dev->name);
  747. else
  748. ret = ipvlan_add_addr(ipvlan, ip6_addr, true);
  749. spin_unlock_bh(&ipvlan->addrs_lock);
  750. return ret;
  751. }
  752. static void ipvlan_del_addr6(struct ipvl_dev *ipvlan, struct in6_addr *ip6_addr)
  753. {
  754. return ipvlan_del_addr(ipvlan, ip6_addr, true);
  755. }
  756. static int ipvlan_addr6_event(struct notifier_block *unused,
  757. unsigned long event, void *ptr)
  758. {
  759. struct inet6_ifaddr *if6 = (struct inet6_ifaddr *)ptr;
  760. struct net_device *dev = (struct net_device *)if6->idev->dev;
  761. struct ipvl_dev *ipvlan = netdev_priv(dev);
  762. if (!ipvlan_is_valid_dev(dev))
  763. return NOTIFY_DONE;
  764. switch (event) {
  765. case NETDEV_UP:
  766. if (ipvlan_add_addr6(ipvlan, &if6->addr))
  767. return NOTIFY_BAD;
  768. break;
  769. case NETDEV_DOWN:
  770. ipvlan_del_addr6(ipvlan, &if6->addr);
  771. break;
  772. }
  773. return NOTIFY_OK;
  774. }
  775. static int ipvlan_addr6_validator_event(struct notifier_block *unused,
  776. unsigned long event, void *ptr)
  777. {
  778. struct in6_validator_info *i6vi = (struct in6_validator_info *)ptr;
  779. struct net_device *dev = (struct net_device *)i6vi->i6vi_dev->dev;
  780. struct ipvl_dev *ipvlan = netdev_priv(dev);
  781. if (!ipvlan_is_valid_dev(dev))
  782. return NOTIFY_DONE;
  783. switch (event) {
  784. case NETDEV_UP:
  785. if (ipvlan_addr_busy(ipvlan->port, &i6vi->i6vi_addr, true)) {
  786. NL_SET_ERR_MSG(i6vi->extack,
  787. "Address already assigned to an ipvlan device");
  788. return notifier_from_errno(-EADDRINUSE);
  789. }
  790. break;
  791. }
  792. return NOTIFY_OK;
  793. }
  794. #endif
  795. static int ipvlan_add_addr4(struct ipvl_dev *ipvlan, struct in_addr *ip4_addr)
  796. {
  797. int ret = -EINVAL;
  798. spin_lock_bh(&ipvlan->addrs_lock);
  799. if (ipvlan_addr_busy(ipvlan->port, ip4_addr, false))
  800. netif_err(ipvlan, ifup, ipvlan->dev,
  801. "Failed to add IPv4=%pI4 on %s intf.\n",
  802. ip4_addr, ipvlan->dev->name);
  803. else
  804. ret = ipvlan_add_addr(ipvlan, ip4_addr, false);
  805. spin_unlock_bh(&ipvlan->addrs_lock);
  806. return ret;
  807. }
  808. static void ipvlan_del_addr4(struct ipvl_dev *ipvlan, struct in_addr *ip4_addr)
  809. {
  810. return ipvlan_del_addr(ipvlan, ip4_addr, false);
  811. }
  812. static int ipvlan_addr4_event(struct notifier_block *unused,
  813. unsigned long event, void *ptr)
  814. {
  815. struct in_ifaddr *if4 = (struct in_ifaddr *)ptr;
  816. struct net_device *dev = (struct net_device *)if4->ifa_dev->dev;
  817. struct ipvl_dev *ipvlan = netdev_priv(dev);
  818. struct in_addr ip4_addr;
  819. if (!ipvlan_is_valid_dev(dev))
  820. return NOTIFY_DONE;
  821. switch (event) {
  822. case NETDEV_UP:
  823. ip4_addr.s_addr = if4->ifa_address;
  824. if (ipvlan_add_addr4(ipvlan, &ip4_addr))
  825. return NOTIFY_BAD;
  826. break;
  827. case NETDEV_DOWN:
  828. ip4_addr.s_addr = if4->ifa_address;
  829. ipvlan_del_addr4(ipvlan, &ip4_addr);
  830. break;
  831. }
  832. return NOTIFY_OK;
  833. }
  834. static int ipvlan_addr4_validator_event(struct notifier_block *unused,
  835. unsigned long event, void *ptr)
  836. {
  837. struct in_validator_info *ivi = (struct in_validator_info *)ptr;
  838. struct net_device *dev = (struct net_device *)ivi->ivi_dev->dev;
  839. struct ipvl_dev *ipvlan = netdev_priv(dev);
  840. if (!ipvlan_is_valid_dev(dev))
  841. return NOTIFY_DONE;
  842. switch (event) {
  843. case NETDEV_UP:
  844. if (ipvlan_addr_busy(ipvlan->port, &ivi->ivi_addr, false)) {
  845. NL_SET_ERR_MSG(ivi->extack,
  846. "Address already assigned to an ipvlan device");
  847. return notifier_from_errno(-EADDRINUSE);
  848. }
  849. break;
  850. }
  851. return NOTIFY_OK;
  852. }
  853. static struct notifier_block ipvlan_addr4_notifier_block __read_mostly = {
  854. .notifier_call = ipvlan_addr4_event,
  855. };
  856. static struct notifier_block ipvlan_addr4_vtor_notifier_block __read_mostly = {
  857. .notifier_call = ipvlan_addr4_validator_event,
  858. };
  859. static struct notifier_block ipvlan_notifier_block __read_mostly = {
  860. .notifier_call = ipvlan_device_event,
  861. };
  862. #if IS_ENABLED(CONFIG_IPV6)
  863. static struct notifier_block ipvlan_addr6_notifier_block __read_mostly = {
  864. .notifier_call = ipvlan_addr6_event,
  865. };
  866. static struct notifier_block ipvlan_addr6_vtor_notifier_block __read_mostly = {
  867. .notifier_call = ipvlan_addr6_validator_event,
  868. };
  869. #endif
  870. static void ipvlan_ns_exit(struct net *net)
  871. {
  872. struct ipvlan_netns *vnet = net_generic(net, ipvlan_netid);
  873. if (WARN_ON_ONCE(vnet->ipvl_nf_hook_refcnt)) {
  874. vnet->ipvl_nf_hook_refcnt = 0;
  875. nf_unregister_net_hooks(net, ipvl_nfops,
  876. ARRAY_SIZE(ipvl_nfops));
  877. }
  878. }
  879. static struct pernet_operations ipvlan_net_ops = {
  880. .id = &ipvlan_netid,
  881. .size = sizeof(struct ipvlan_netns),
  882. .exit = ipvlan_ns_exit,
  883. };
  884. static int __init ipvlan_init_module(void)
  885. {
  886. int err;
  887. ipvlan_init_secret();
  888. register_netdevice_notifier(&ipvlan_notifier_block);
  889. #if IS_ENABLED(CONFIG_IPV6)
  890. register_inet6addr_notifier(&ipvlan_addr6_notifier_block);
  891. register_inet6addr_validator_notifier(
  892. &ipvlan_addr6_vtor_notifier_block);
  893. #endif
  894. register_inetaddr_notifier(&ipvlan_addr4_notifier_block);
  895. register_inetaddr_validator_notifier(&ipvlan_addr4_vtor_notifier_block);
  896. err = register_pernet_subsys(&ipvlan_net_ops);
  897. if (err < 0)
  898. goto error;
  899. err = ipvlan_link_register(&ipvlan_link_ops);
  900. if (err < 0) {
  901. unregister_pernet_subsys(&ipvlan_net_ops);
  902. goto error;
  903. }
  904. return 0;
  905. error:
  906. unregister_inetaddr_notifier(&ipvlan_addr4_notifier_block);
  907. unregister_inetaddr_validator_notifier(
  908. &ipvlan_addr4_vtor_notifier_block);
  909. #if IS_ENABLED(CONFIG_IPV6)
  910. unregister_inet6addr_notifier(&ipvlan_addr6_notifier_block);
  911. unregister_inet6addr_validator_notifier(
  912. &ipvlan_addr6_vtor_notifier_block);
  913. #endif
  914. unregister_netdevice_notifier(&ipvlan_notifier_block);
  915. return err;
  916. }
  917. static void __exit ipvlan_cleanup_module(void)
  918. {
  919. rtnl_link_unregister(&ipvlan_link_ops);
  920. unregister_pernet_subsys(&ipvlan_net_ops);
  921. unregister_netdevice_notifier(&ipvlan_notifier_block);
  922. unregister_inetaddr_notifier(&ipvlan_addr4_notifier_block);
  923. unregister_inetaddr_validator_notifier(
  924. &ipvlan_addr4_vtor_notifier_block);
  925. #if IS_ENABLED(CONFIG_IPV6)
  926. unregister_inet6addr_notifier(&ipvlan_addr6_notifier_block);
  927. unregister_inet6addr_validator_notifier(
  928. &ipvlan_addr6_vtor_notifier_block);
  929. #endif
  930. }
  931. module_init(ipvlan_init_module);
  932. module_exit(ipvlan_cleanup_module);
  933. MODULE_LICENSE("GPL");
  934. MODULE_AUTHOR("Mahesh Bandewar <maheshb@google.com>");
  935. MODULE_DESCRIPTION("Driver for L3 (IPv6/IPv4) based VLANs");
  936. MODULE_ALIAS_RTNL_LINK("ipvlan");