ipvlan_main.c 28 KB

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