ipvlan_main.c 20 KB

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