ipvlan_main.c 26 KB

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