ipvlan_main.c 20 KB

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