ipvlan_main.c 20 KB

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