ipvlan_main.c 29 KB

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