ipvlan_main.c 21 KB

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