ipvlan_main.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017
  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. {
  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_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. struct netlink_ext_ack *extack)
  378. {
  379. struct ipvl_dev *ipvlan = netdev_priv(dev);
  380. struct ipvl_port *port = ipvlan_port_get_rtnl(ipvlan->phy_dev);
  381. int err = 0;
  382. if (data && data[IFLA_IPVLAN_MODE]) {
  383. u16 nmode = nla_get_u16(data[IFLA_IPVLAN_MODE]);
  384. err = ipvlan_set_port_mode(port, nmode);
  385. }
  386. return err;
  387. }
  388. static size_t ipvlan_nl_getsize(const struct net_device *dev)
  389. {
  390. return (0
  391. + nla_total_size(2) /* IFLA_IPVLAN_MODE */
  392. );
  393. }
  394. static int ipvlan_nl_validate(struct nlattr *tb[], struct nlattr *data[],
  395. struct netlink_ext_ack *extack)
  396. {
  397. if (data && data[IFLA_IPVLAN_MODE]) {
  398. u16 mode = nla_get_u16(data[IFLA_IPVLAN_MODE]);
  399. if (mode < IPVLAN_MODE_L2 || mode >= IPVLAN_MODE_MAX)
  400. return -EINVAL;
  401. }
  402. return 0;
  403. }
  404. static int ipvlan_nl_fillinfo(struct sk_buff *skb,
  405. const struct net_device *dev)
  406. {
  407. struct ipvl_dev *ipvlan = netdev_priv(dev);
  408. struct ipvl_port *port = ipvlan_port_get_rtnl(ipvlan->phy_dev);
  409. int ret = -EINVAL;
  410. if (!port)
  411. goto err;
  412. ret = -EMSGSIZE;
  413. if (nla_put_u16(skb, IFLA_IPVLAN_MODE, port->mode))
  414. goto err;
  415. return 0;
  416. err:
  417. return ret;
  418. }
  419. int ipvlan_link_new(struct net *src_net, struct net_device *dev,
  420. struct nlattr *tb[], struct nlattr *data[],
  421. struct netlink_ext_ack *extack)
  422. {
  423. struct ipvl_dev *ipvlan = netdev_priv(dev);
  424. struct ipvl_port *port;
  425. struct net_device *phy_dev;
  426. int err;
  427. u16 mode = IPVLAN_MODE_L3;
  428. bool create = false;
  429. if (!tb[IFLA_LINK])
  430. return -EINVAL;
  431. phy_dev = __dev_get_by_index(src_net, nla_get_u32(tb[IFLA_LINK]));
  432. if (!phy_dev)
  433. return -ENODEV;
  434. if (netif_is_ipvlan(phy_dev)) {
  435. struct ipvl_dev *tmp = netdev_priv(phy_dev);
  436. phy_dev = tmp->phy_dev;
  437. } else if (!netif_is_ipvlan_port(phy_dev)) {
  438. err = ipvlan_port_create(phy_dev);
  439. if (err < 0)
  440. return err;
  441. create = true;
  442. }
  443. if (data && data[IFLA_IPVLAN_MODE])
  444. mode = nla_get_u16(data[IFLA_IPVLAN_MODE]);
  445. port = ipvlan_port_get_rtnl(phy_dev);
  446. ipvlan->phy_dev = phy_dev;
  447. ipvlan->dev = dev;
  448. ipvlan->port = port;
  449. ipvlan->sfeatures = IPVLAN_FEATURES;
  450. ipvlan_adjust_mtu(ipvlan, phy_dev);
  451. INIT_LIST_HEAD(&ipvlan->addrs);
  452. /* If the port-id base is at the MAX value, then wrap it around and
  453. * begin from 0x1 again. This may be due to a busy system where lots
  454. * of slaves are getting created and deleted.
  455. */
  456. if (port->dev_id_start == 0xFFFE)
  457. port->dev_id_start = 0x1;
  458. /* Since L2 address is shared among all IPvlan slaves including
  459. * master, use unique 16 bit dev-ids to diffentiate among them.
  460. * Assign IDs between 0x1 and 0xFFFE (used by the master) to each
  461. * slave link [see addrconf_ifid_eui48()].
  462. */
  463. err = ida_simple_get(&port->ida, port->dev_id_start, 0xFFFE,
  464. GFP_KERNEL);
  465. if (err < 0)
  466. err = ida_simple_get(&port->ida, 0x1, port->dev_id_start,
  467. GFP_KERNEL);
  468. if (err < 0)
  469. goto destroy_ipvlan_port;
  470. dev->dev_id = err;
  471. /* Increment id-base to the next slot for the future assignment */
  472. port->dev_id_start = err + 1;
  473. /* TODO Probably put random address here to be presented to the
  474. * world but keep using the physical-dev address for the outgoing
  475. * packets.
  476. */
  477. memcpy(dev->dev_addr, phy_dev->dev_addr, ETH_ALEN);
  478. dev->priv_flags |= IFF_IPVLAN_SLAVE;
  479. err = register_netdevice(dev);
  480. if (err < 0)
  481. goto remove_ida;
  482. err = netdev_upper_dev_link(phy_dev, dev);
  483. if (err) {
  484. goto unregister_netdev;
  485. }
  486. err = ipvlan_set_port_mode(port, mode);
  487. if (err) {
  488. goto unlink_netdev;
  489. }
  490. list_add_tail_rcu(&ipvlan->pnode, &port->ipvlans);
  491. netif_stacked_transfer_operstate(phy_dev, dev);
  492. return 0;
  493. unlink_netdev:
  494. netdev_upper_dev_unlink(phy_dev, dev);
  495. unregister_netdev:
  496. unregister_netdevice(dev);
  497. remove_ida:
  498. ida_simple_remove(&port->ida, dev->dev_id);
  499. destroy_ipvlan_port:
  500. if (create)
  501. ipvlan_port_destroy(phy_dev);
  502. return err;
  503. }
  504. EXPORT_SYMBOL_GPL(ipvlan_link_new);
  505. void ipvlan_link_delete(struct net_device *dev, struct list_head *head)
  506. {
  507. struct ipvl_dev *ipvlan = netdev_priv(dev);
  508. struct ipvl_addr *addr, *next;
  509. list_for_each_entry_safe(addr, next, &ipvlan->addrs, anode) {
  510. ipvlan_ht_addr_del(addr);
  511. list_del(&addr->anode);
  512. kfree_rcu(addr, rcu);
  513. }
  514. ida_simple_remove(&ipvlan->port->ida, dev->dev_id);
  515. list_del_rcu(&ipvlan->pnode);
  516. unregister_netdevice_queue(dev, head);
  517. netdev_upper_dev_unlink(ipvlan->phy_dev, dev);
  518. }
  519. EXPORT_SYMBOL_GPL(ipvlan_link_delete);
  520. void ipvlan_link_setup(struct net_device *dev)
  521. {
  522. ether_setup(dev);
  523. dev->priv_flags &= ~(IFF_XMIT_DST_RELEASE | IFF_TX_SKB_SHARING);
  524. dev->priv_flags |= IFF_UNICAST_FLT | IFF_NO_QUEUE;
  525. dev->netdev_ops = &ipvlan_netdev_ops;
  526. dev->needs_free_netdev = true;
  527. dev->header_ops = &ipvlan_header_ops;
  528. dev->ethtool_ops = &ipvlan_ethtool_ops;
  529. }
  530. EXPORT_SYMBOL_GPL(ipvlan_link_setup);
  531. static const struct nla_policy ipvlan_nl_policy[IFLA_IPVLAN_MAX + 1] =
  532. {
  533. [IFLA_IPVLAN_MODE] = { .type = NLA_U16 },
  534. };
  535. static struct rtnl_link_ops ipvlan_link_ops = {
  536. .kind = "ipvlan",
  537. .priv_size = sizeof(struct ipvl_dev),
  538. .setup = ipvlan_link_setup,
  539. .newlink = ipvlan_link_new,
  540. .dellink = ipvlan_link_delete,
  541. };
  542. int ipvlan_link_register(struct rtnl_link_ops *ops)
  543. {
  544. ops->get_size = ipvlan_nl_getsize;
  545. ops->policy = ipvlan_nl_policy;
  546. ops->validate = ipvlan_nl_validate;
  547. ops->fill_info = ipvlan_nl_fillinfo;
  548. ops->changelink = ipvlan_nl_changelink;
  549. ops->maxtype = IFLA_IPVLAN_MAX;
  550. return rtnl_link_register(ops);
  551. }
  552. EXPORT_SYMBOL_GPL(ipvlan_link_register);
  553. static int ipvlan_device_event(struct notifier_block *unused,
  554. unsigned long event, void *ptr)
  555. {
  556. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  557. struct ipvl_dev *ipvlan, *next;
  558. struct ipvl_port *port;
  559. LIST_HEAD(lst_kill);
  560. if (!netif_is_ipvlan_port(dev))
  561. return NOTIFY_DONE;
  562. port = ipvlan_port_get_rtnl(dev);
  563. switch (event) {
  564. case NETDEV_CHANGE:
  565. list_for_each_entry(ipvlan, &port->ipvlans, pnode)
  566. netif_stacked_transfer_operstate(ipvlan->phy_dev,
  567. ipvlan->dev);
  568. break;
  569. case NETDEV_REGISTER: {
  570. struct net *oldnet, *newnet = dev_net(dev);
  571. struct ipvlan_netns *old_vnet;
  572. oldnet = read_pnet(&port->pnet);
  573. if (net_eq(newnet, oldnet))
  574. break;
  575. write_pnet(&port->pnet, newnet);
  576. old_vnet = net_generic(oldnet, ipvlan_netid);
  577. if (!old_vnet->ipvl_nf_hook_refcnt)
  578. break;
  579. ipvlan_register_nf_hook(newnet);
  580. ipvlan_unregister_nf_hook(oldnet);
  581. break;
  582. }
  583. case NETDEV_UNREGISTER:
  584. if (dev->reg_state != NETREG_UNREGISTERING)
  585. break;
  586. list_for_each_entry_safe(ipvlan, next, &port->ipvlans,
  587. pnode)
  588. ipvlan->dev->rtnl_link_ops->dellink(ipvlan->dev,
  589. &lst_kill);
  590. unregister_netdevice_many(&lst_kill);
  591. break;
  592. case NETDEV_FEAT_CHANGE:
  593. list_for_each_entry(ipvlan, &port->ipvlans, pnode) {
  594. ipvlan->dev->features = dev->features & IPVLAN_FEATURES;
  595. ipvlan->dev->gso_max_size = dev->gso_max_size;
  596. ipvlan->dev->gso_max_segs = dev->gso_max_segs;
  597. netdev_features_change(ipvlan->dev);
  598. }
  599. break;
  600. case NETDEV_CHANGEMTU:
  601. list_for_each_entry(ipvlan, &port->ipvlans, pnode)
  602. ipvlan_adjust_mtu(ipvlan, dev);
  603. break;
  604. case NETDEV_PRE_TYPE_CHANGE:
  605. /* Forbid underlying device to change its type. */
  606. return NOTIFY_BAD;
  607. }
  608. return NOTIFY_DONE;
  609. }
  610. static int ipvlan_add_addr(struct ipvl_dev *ipvlan, void *iaddr, bool is_v6)
  611. {
  612. struct ipvl_addr *addr;
  613. addr = kzalloc(sizeof(struct ipvl_addr), GFP_ATOMIC);
  614. if (!addr)
  615. return -ENOMEM;
  616. addr->master = ipvlan;
  617. if (is_v6) {
  618. memcpy(&addr->ip6addr, iaddr, sizeof(struct in6_addr));
  619. addr->atype = IPVL_IPV6;
  620. } else {
  621. memcpy(&addr->ip4addr, iaddr, sizeof(struct in_addr));
  622. addr->atype = IPVL_IPV4;
  623. }
  624. list_add_tail(&addr->anode, &ipvlan->addrs);
  625. /* If the interface is not up, the address will be added to the hash
  626. * list by ipvlan_open.
  627. */
  628. if (netif_running(ipvlan->dev))
  629. ipvlan_ht_addr_add(ipvlan, addr);
  630. return 0;
  631. }
  632. static void ipvlan_del_addr(struct ipvl_dev *ipvlan, void *iaddr, bool is_v6)
  633. {
  634. struct ipvl_addr *addr;
  635. addr = ipvlan_find_addr(ipvlan, iaddr, is_v6);
  636. if (!addr)
  637. return;
  638. ipvlan_ht_addr_del(addr);
  639. list_del(&addr->anode);
  640. kfree_rcu(addr, rcu);
  641. return;
  642. }
  643. static int ipvlan_add_addr6(struct ipvl_dev *ipvlan, struct in6_addr *ip6_addr)
  644. {
  645. if (ipvlan_addr_busy(ipvlan->port, ip6_addr, true)) {
  646. netif_err(ipvlan, ifup, ipvlan->dev,
  647. "Failed to add IPv6=%pI6c addr for %s intf\n",
  648. ip6_addr, ipvlan->dev->name);
  649. return -EINVAL;
  650. }
  651. return ipvlan_add_addr(ipvlan, ip6_addr, true);
  652. }
  653. static void ipvlan_del_addr6(struct ipvl_dev *ipvlan, struct in6_addr *ip6_addr)
  654. {
  655. return ipvlan_del_addr(ipvlan, ip6_addr, true);
  656. }
  657. static int ipvlan_addr6_event(struct notifier_block *unused,
  658. unsigned long event, void *ptr)
  659. {
  660. struct inet6_ifaddr *if6 = (struct inet6_ifaddr *)ptr;
  661. struct net_device *dev = (struct net_device *)if6->idev->dev;
  662. struct ipvl_dev *ipvlan = netdev_priv(dev);
  663. /* FIXME IPv6 autoconf calls us from bh without RTNL */
  664. if (in_softirq())
  665. return NOTIFY_DONE;
  666. if (!netif_is_ipvlan(dev))
  667. return NOTIFY_DONE;
  668. if (!ipvlan || !ipvlan->port)
  669. return NOTIFY_DONE;
  670. switch (event) {
  671. case NETDEV_UP:
  672. if (ipvlan_add_addr6(ipvlan, &if6->addr))
  673. return NOTIFY_BAD;
  674. break;
  675. case NETDEV_DOWN:
  676. ipvlan_del_addr6(ipvlan, &if6->addr);
  677. break;
  678. }
  679. return NOTIFY_OK;
  680. }
  681. static int ipvlan_addr6_validator_event(struct notifier_block *unused,
  682. unsigned long event, void *ptr)
  683. {
  684. struct in6_validator_info *i6vi = (struct in6_validator_info *)ptr;
  685. struct net_device *dev = (struct net_device *)i6vi->i6vi_dev->dev;
  686. struct ipvl_dev *ipvlan = netdev_priv(dev);
  687. /* FIXME IPv6 autoconf calls us from bh without RTNL */
  688. if (in_softirq())
  689. return NOTIFY_DONE;
  690. if (!netif_is_ipvlan(dev))
  691. return NOTIFY_DONE;
  692. if (!ipvlan || !ipvlan->port)
  693. return NOTIFY_DONE;
  694. switch (event) {
  695. case NETDEV_UP:
  696. if (ipvlan_addr_busy(ipvlan->port, &i6vi->i6vi_addr, true))
  697. return notifier_from_errno(-EADDRINUSE);
  698. break;
  699. }
  700. return NOTIFY_OK;
  701. }
  702. static int ipvlan_add_addr4(struct ipvl_dev *ipvlan, struct in_addr *ip4_addr)
  703. {
  704. if (ipvlan_addr_busy(ipvlan->port, ip4_addr, false)) {
  705. netif_err(ipvlan, ifup, ipvlan->dev,
  706. "Failed to add IPv4=%pI4 on %s intf.\n",
  707. ip4_addr, ipvlan->dev->name);
  708. return -EINVAL;
  709. }
  710. return ipvlan_add_addr(ipvlan, ip4_addr, false);
  711. }
  712. static void ipvlan_del_addr4(struct ipvl_dev *ipvlan, struct in_addr *ip4_addr)
  713. {
  714. return ipvlan_del_addr(ipvlan, ip4_addr, false);
  715. }
  716. static int ipvlan_addr4_event(struct notifier_block *unused,
  717. unsigned long event, void *ptr)
  718. {
  719. struct in_ifaddr *if4 = (struct in_ifaddr *)ptr;
  720. struct net_device *dev = (struct net_device *)if4->ifa_dev->dev;
  721. struct ipvl_dev *ipvlan = netdev_priv(dev);
  722. struct in_addr ip4_addr;
  723. if (!netif_is_ipvlan(dev))
  724. return NOTIFY_DONE;
  725. if (!ipvlan || !ipvlan->port)
  726. return NOTIFY_DONE;
  727. switch (event) {
  728. case NETDEV_UP:
  729. ip4_addr.s_addr = if4->ifa_address;
  730. if (ipvlan_add_addr4(ipvlan, &ip4_addr))
  731. return NOTIFY_BAD;
  732. break;
  733. case NETDEV_DOWN:
  734. ip4_addr.s_addr = if4->ifa_address;
  735. ipvlan_del_addr4(ipvlan, &ip4_addr);
  736. break;
  737. }
  738. return NOTIFY_OK;
  739. }
  740. static int ipvlan_addr4_validator_event(struct notifier_block *unused,
  741. unsigned long event, void *ptr)
  742. {
  743. struct in_validator_info *ivi = (struct in_validator_info *)ptr;
  744. struct net_device *dev = (struct net_device *)ivi->ivi_dev->dev;
  745. struct ipvl_dev *ipvlan = netdev_priv(dev);
  746. if (!netif_is_ipvlan(dev))
  747. return NOTIFY_DONE;
  748. if (!ipvlan || !ipvlan->port)
  749. return NOTIFY_DONE;
  750. switch (event) {
  751. case NETDEV_UP:
  752. if (ipvlan_addr_busy(ipvlan->port, &ivi->ivi_addr, false))
  753. return notifier_from_errno(-EADDRINUSE);
  754. break;
  755. }
  756. return NOTIFY_OK;
  757. }
  758. static struct notifier_block ipvlan_addr4_notifier_block __read_mostly = {
  759. .notifier_call = ipvlan_addr4_event,
  760. };
  761. static struct notifier_block ipvlan_addr4_vtor_notifier_block __read_mostly = {
  762. .notifier_call = ipvlan_addr4_validator_event,
  763. };
  764. static struct notifier_block ipvlan_notifier_block __read_mostly = {
  765. .notifier_call = ipvlan_device_event,
  766. };
  767. static struct notifier_block ipvlan_addr6_notifier_block __read_mostly = {
  768. .notifier_call = ipvlan_addr6_event,
  769. };
  770. static struct notifier_block ipvlan_addr6_vtor_notifier_block __read_mostly = {
  771. .notifier_call = ipvlan_addr6_validator_event,
  772. };
  773. static void ipvlan_ns_exit(struct net *net)
  774. {
  775. struct ipvlan_netns *vnet = net_generic(net, ipvlan_netid);
  776. if (WARN_ON_ONCE(vnet->ipvl_nf_hook_refcnt)) {
  777. vnet->ipvl_nf_hook_refcnt = 0;
  778. nf_unregister_net_hooks(net, ipvl_nfops,
  779. ARRAY_SIZE(ipvl_nfops));
  780. }
  781. }
  782. static struct pernet_operations ipvlan_net_ops = {
  783. .id = &ipvlan_netid,
  784. .size = sizeof(struct ipvlan_netns),
  785. .exit = ipvlan_ns_exit,
  786. };
  787. static int __init ipvlan_init_module(void)
  788. {
  789. int err;
  790. ipvlan_init_secret();
  791. register_netdevice_notifier(&ipvlan_notifier_block);
  792. register_inet6addr_notifier(&ipvlan_addr6_notifier_block);
  793. register_inet6addr_validator_notifier(
  794. &ipvlan_addr6_vtor_notifier_block);
  795. register_inetaddr_notifier(&ipvlan_addr4_notifier_block);
  796. register_inetaddr_validator_notifier(&ipvlan_addr4_vtor_notifier_block);
  797. err = register_pernet_subsys(&ipvlan_net_ops);
  798. if (err < 0)
  799. goto error;
  800. err = ipvlan_link_register(&ipvlan_link_ops);
  801. if (err < 0) {
  802. unregister_pernet_subsys(&ipvlan_net_ops);
  803. goto error;
  804. }
  805. return 0;
  806. error:
  807. unregister_inetaddr_notifier(&ipvlan_addr4_notifier_block);
  808. unregister_inetaddr_validator_notifier(
  809. &ipvlan_addr4_vtor_notifier_block);
  810. unregister_inet6addr_notifier(&ipvlan_addr6_notifier_block);
  811. unregister_inet6addr_validator_notifier(
  812. &ipvlan_addr6_vtor_notifier_block);
  813. unregister_netdevice_notifier(&ipvlan_notifier_block);
  814. return err;
  815. }
  816. static void __exit ipvlan_cleanup_module(void)
  817. {
  818. rtnl_link_unregister(&ipvlan_link_ops);
  819. unregister_pernet_subsys(&ipvlan_net_ops);
  820. unregister_netdevice_notifier(&ipvlan_notifier_block);
  821. unregister_inetaddr_notifier(&ipvlan_addr4_notifier_block);
  822. unregister_inetaddr_validator_notifier(
  823. &ipvlan_addr4_vtor_notifier_block);
  824. unregister_inet6addr_notifier(&ipvlan_addr6_notifier_block);
  825. unregister_inet6addr_validator_notifier(
  826. &ipvlan_addr6_vtor_notifier_block);
  827. }
  828. module_init(ipvlan_init_module);
  829. module_exit(ipvlan_cleanup_module);
  830. MODULE_LICENSE("GPL");
  831. MODULE_AUTHOR("Mahesh Bandewar <maheshb@google.com>");
  832. MODULE_DESCRIPTION("Driver for L3 (IPv6/IPv4) based VLANs");
  833. MODULE_ALIAS_RTNL_LINK("ipvlan");