ipvlan_main.c 22 KB

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