ipvlan_main.c 29 KB

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