ipvlan_main.c 21 KB

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