ipvlan_main.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067
  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 unsigned int ipvlan_netid __read_mostly;
  11. struct ipvlan_netns {
  12. unsigned int ipvl_nf_hook_refcnt;
  13. };
  14. static const struct nf_hook_ops ipvl_nfops[] = {
  15. {
  16. .hook = ipvlan_nf_input,
  17. .pf = NFPROTO_IPV4,
  18. .hooknum = NF_INET_LOCAL_IN,
  19. .priority = INT_MAX,
  20. },
  21. {
  22. .hook = ipvlan_nf_input,
  23. .pf = NFPROTO_IPV6,
  24. .hooknum = NF_INET_LOCAL_IN,
  25. .priority = INT_MAX,
  26. },
  27. };
  28. static const struct l3mdev_ops ipvl_l3mdev_ops = {
  29. .l3mdev_l3_rcv = ipvlan_l3_rcv,
  30. };
  31. static void ipvlan_adjust_mtu(struct ipvl_dev *ipvlan, struct net_device *dev)
  32. {
  33. ipvlan->dev->mtu = dev->mtu;
  34. }
  35. static int ipvlan_register_nf_hook(struct net *net)
  36. {
  37. struct ipvlan_netns *vnet = net_generic(net, ipvlan_netid);
  38. int err = 0;
  39. if (!vnet->ipvl_nf_hook_refcnt) {
  40. err = nf_register_net_hooks(net, ipvl_nfops,
  41. ARRAY_SIZE(ipvl_nfops));
  42. if (!err)
  43. vnet->ipvl_nf_hook_refcnt = 1;
  44. } else {
  45. vnet->ipvl_nf_hook_refcnt++;
  46. }
  47. return err;
  48. }
  49. static void ipvlan_unregister_nf_hook(struct net *net)
  50. {
  51. struct ipvlan_netns *vnet = net_generic(net, ipvlan_netid);
  52. if (WARN_ON(!vnet->ipvl_nf_hook_refcnt))
  53. return;
  54. vnet->ipvl_nf_hook_refcnt--;
  55. if (!vnet->ipvl_nf_hook_refcnt)
  56. nf_unregister_net_hooks(net, ipvl_nfops,
  57. ARRAY_SIZE(ipvl_nfops));
  58. }
  59. static int ipvlan_set_port_mode(struct ipvl_port *port, u16 nval)
  60. {
  61. struct ipvl_dev *ipvlan;
  62. struct net_device *mdev = port->dev;
  63. int err = 0;
  64. ASSERT_RTNL();
  65. if (port->mode != nval) {
  66. if (nval == IPVLAN_MODE_L3S) {
  67. /* New mode is L3S */
  68. err = ipvlan_register_nf_hook(read_pnet(&port->pnet));
  69. if (!err) {
  70. mdev->l3mdev_ops = &ipvl_l3mdev_ops;
  71. mdev->priv_flags |= IFF_L3MDEV_MASTER;
  72. } else
  73. return err;
  74. } else if (port->mode == IPVLAN_MODE_L3S) {
  75. /* Old mode was L3S */
  76. mdev->priv_flags &= ~IFF_L3MDEV_MASTER;
  77. ipvlan_unregister_nf_hook(read_pnet(&port->pnet));
  78. mdev->l3mdev_ops = NULL;
  79. }
  80. list_for_each_entry(ipvlan, &port->ipvlans, pnode) {
  81. if (nval == IPVLAN_MODE_L3 || nval == IPVLAN_MODE_L3S)
  82. ipvlan->dev->flags |= IFF_NOARP;
  83. else
  84. ipvlan->dev->flags &= ~IFF_NOARP;
  85. }
  86. port->mode = nval;
  87. }
  88. return err;
  89. }
  90. static int ipvlan_port_create(struct net_device *dev)
  91. {
  92. struct ipvl_port *port;
  93. int err, idx;
  94. if (dev->type != ARPHRD_ETHER || dev->flags & IFF_LOOPBACK) {
  95. netdev_err(dev, "Master is either lo or non-ether device\n");
  96. return -EINVAL;
  97. }
  98. if (netdev_is_rx_handler_busy(dev)) {
  99. netdev_err(dev, "Device is already in use.\n");
  100. return -EBUSY;
  101. }
  102. port = kzalloc(sizeof(struct ipvl_port), GFP_KERNEL);
  103. if (!port)
  104. return -ENOMEM;
  105. write_pnet(&port->pnet, dev_net(dev));
  106. port->dev = dev;
  107. port->mode = IPVLAN_MODE_L3;
  108. INIT_LIST_HEAD(&port->ipvlans);
  109. for (idx = 0; idx < IPVLAN_HASH_SIZE; idx++)
  110. INIT_HLIST_HEAD(&port->hlhead[idx]);
  111. skb_queue_head_init(&port->backlog);
  112. INIT_WORK(&port->wq, ipvlan_process_multicast);
  113. ida_init(&port->ida);
  114. port->dev_id_start = 1;
  115. err = netdev_rx_handler_register(dev, ipvlan_handle_frame, port);
  116. if (err)
  117. goto err;
  118. dev->priv_flags |= IFF_IPVLAN_MASTER;
  119. return 0;
  120. err:
  121. kfree(port);
  122. return err;
  123. }
  124. static void ipvlan_port_destroy(struct net_device *dev)
  125. {
  126. struct ipvl_port *port = ipvlan_port_get_rtnl(dev);
  127. struct sk_buff *skb;
  128. dev->priv_flags &= ~IFF_IPVLAN_MASTER;
  129. if (port->mode == IPVLAN_MODE_L3S) {
  130. dev->priv_flags &= ~IFF_L3MDEV_MASTER;
  131. ipvlan_unregister_nf_hook(dev_net(dev));
  132. dev->l3mdev_ops = NULL;
  133. }
  134. netdev_rx_handler_unregister(dev);
  135. cancel_work_sync(&port->wq);
  136. while ((skb = __skb_dequeue(&port->backlog)) != NULL) {
  137. if (skb->dev)
  138. dev_put(skb->dev);
  139. kfree_skb(skb);
  140. }
  141. ida_destroy(&port->ida);
  142. kfree(port);
  143. }
  144. #define IPVLAN_FEATURES \
  145. (NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_HIGHDMA | NETIF_F_FRAGLIST | \
  146. NETIF_F_GSO | NETIF_F_TSO | NETIF_F_GSO_ROBUST | \
  147. NETIF_F_TSO_ECN | NETIF_F_TSO6 | NETIF_F_GRO | NETIF_F_RXCSUM | \
  148. NETIF_F_HW_VLAN_CTAG_FILTER | NETIF_F_HW_VLAN_STAG_FILTER)
  149. #define IPVLAN_STATE_MASK \
  150. ((1<<__LINK_STATE_NOCARRIER) | (1<<__LINK_STATE_DORMANT))
  151. static int ipvlan_init(struct net_device *dev)
  152. {
  153. struct ipvl_dev *ipvlan = netdev_priv(dev);
  154. const struct net_device *phy_dev = ipvlan->phy_dev;
  155. struct ipvl_port *port = ipvlan->port;
  156. dev->state = (dev->state & ~IPVLAN_STATE_MASK) |
  157. (phy_dev->state & IPVLAN_STATE_MASK);
  158. dev->features = phy_dev->features & IPVLAN_FEATURES;
  159. dev->features |= NETIF_F_LLTX;
  160. dev->gso_max_size = phy_dev->gso_max_size;
  161. dev->gso_max_segs = phy_dev->gso_max_segs;
  162. dev->hard_header_len = phy_dev->hard_header_len;
  163. netdev_lockdep_set_classes(dev);
  164. ipvlan->pcpu_stats = netdev_alloc_pcpu_stats(struct ipvl_pcpu_stats);
  165. if (!ipvlan->pcpu_stats)
  166. return -ENOMEM;
  167. port->count += 1;
  168. return 0;
  169. }
  170. static void ipvlan_uninit(struct net_device *dev)
  171. {
  172. struct ipvl_dev *ipvlan = netdev_priv(dev);
  173. struct ipvl_port *port = ipvlan->port;
  174. free_percpu(ipvlan->pcpu_stats);
  175. port->count -= 1;
  176. if (!port->count)
  177. ipvlan_port_destroy(port->dev);
  178. }
  179. static int ipvlan_open(struct net_device *dev)
  180. {
  181. struct ipvl_dev *ipvlan = netdev_priv(dev);
  182. struct net_device *phy_dev = ipvlan->phy_dev;
  183. struct ipvl_addr *addr;
  184. if (ipvlan->port->mode == IPVLAN_MODE_L3 ||
  185. ipvlan->port->mode == IPVLAN_MODE_L3S)
  186. dev->flags |= IFF_NOARP;
  187. else
  188. dev->flags &= ~IFF_NOARP;
  189. list_for_each_entry(addr, &ipvlan->addrs, anode)
  190. ipvlan_ht_addr_add(ipvlan, addr);
  191. return dev_uc_add(phy_dev, phy_dev->dev_addr);
  192. }
  193. static int ipvlan_stop(struct net_device *dev)
  194. {
  195. struct ipvl_dev *ipvlan = netdev_priv(dev);
  196. struct net_device *phy_dev = ipvlan->phy_dev;
  197. struct ipvl_addr *addr;
  198. dev_uc_unsync(phy_dev, dev);
  199. dev_mc_unsync(phy_dev, dev);
  200. dev_uc_del(phy_dev, phy_dev->dev_addr);
  201. list_for_each_entry(addr, &ipvlan->addrs, anode)
  202. ipvlan_ht_addr_del(addr);
  203. return 0;
  204. }
  205. static netdev_tx_t ipvlan_start_xmit(struct sk_buff *skb,
  206. struct net_device *dev)
  207. {
  208. const struct ipvl_dev *ipvlan = netdev_priv(dev);
  209. int skblen = skb->len;
  210. int ret;
  211. ret = ipvlan_queue_xmit(skb, dev);
  212. if (likely(ret == NET_XMIT_SUCCESS || ret == NET_XMIT_CN)) {
  213. struct ipvl_pcpu_stats *pcptr;
  214. pcptr = this_cpu_ptr(ipvlan->pcpu_stats);
  215. u64_stats_update_begin(&pcptr->syncp);
  216. pcptr->tx_pkts++;
  217. pcptr->tx_bytes += skblen;
  218. u64_stats_update_end(&pcptr->syncp);
  219. } else {
  220. this_cpu_inc(ipvlan->pcpu_stats->tx_drps);
  221. }
  222. return ret;
  223. }
  224. static netdev_features_t ipvlan_fix_features(struct net_device *dev,
  225. netdev_features_t features)
  226. {
  227. struct ipvl_dev *ipvlan = netdev_priv(dev);
  228. return features & (ipvlan->sfeatures | ~IPVLAN_FEATURES);
  229. }
  230. static void ipvlan_change_rx_flags(struct net_device *dev, int change)
  231. {
  232. struct ipvl_dev *ipvlan = netdev_priv(dev);
  233. struct net_device *phy_dev = ipvlan->phy_dev;
  234. if (change & IFF_ALLMULTI)
  235. dev_set_allmulti(phy_dev, dev->flags & IFF_ALLMULTI? 1 : -1);
  236. }
  237. static void ipvlan_set_multicast_mac_filter(struct net_device *dev)
  238. {
  239. struct ipvl_dev *ipvlan = netdev_priv(dev);
  240. if (dev->flags & (IFF_PROMISC | IFF_ALLMULTI)) {
  241. bitmap_fill(ipvlan->mac_filters, IPVLAN_MAC_FILTER_SIZE);
  242. } else {
  243. struct netdev_hw_addr *ha;
  244. DECLARE_BITMAP(mc_filters, IPVLAN_MAC_FILTER_SIZE);
  245. bitmap_zero(mc_filters, IPVLAN_MAC_FILTER_SIZE);
  246. netdev_for_each_mc_addr(ha, dev)
  247. __set_bit(ipvlan_mac_hash(ha->addr), mc_filters);
  248. /* Turn-on broadcast bit irrespective of address family,
  249. * since broadcast is deferred to a work-queue, hence no
  250. * impact on fast-path processing.
  251. */
  252. __set_bit(ipvlan_mac_hash(dev->broadcast), mc_filters);
  253. bitmap_copy(ipvlan->mac_filters, mc_filters,
  254. IPVLAN_MAC_FILTER_SIZE);
  255. }
  256. dev_uc_sync(ipvlan->phy_dev, dev);
  257. dev_mc_sync(ipvlan->phy_dev, dev);
  258. }
  259. static void ipvlan_get_stats64(struct net_device *dev,
  260. struct rtnl_link_stats64 *s)
  261. {
  262. struct ipvl_dev *ipvlan = netdev_priv(dev);
  263. if (ipvlan->pcpu_stats) {
  264. struct ipvl_pcpu_stats *pcptr;
  265. u64 rx_pkts, rx_bytes, rx_mcast, tx_pkts, tx_bytes;
  266. u32 rx_errs = 0, tx_drps = 0;
  267. u32 strt;
  268. int idx;
  269. for_each_possible_cpu(idx) {
  270. pcptr = per_cpu_ptr(ipvlan->pcpu_stats, idx);
  271. do {
  272. strt= u64_stats_fetch_begin_irq(&pcptr->syncp);
  273. rx_pkts = pcptr->rx_pkts;
  274. rx_bytes = pcptr->rx_bytes;
  275. rx_mcast = pcptr->rx_mcast;
  276. tx_pkts = pcptr->tx_pkts;
  277. tx_bytes = pcptr->tx_bytes;
  278. } while (u64_stats_fetch_retry_irq(&pcptr->syncp,
  279. strt));
  280. s->rx_packets += rx_pkts;
  281. s->rx_bytes += rx_bytes;
  282. s->multicast += rx_mcast;
  283. s->tx_packets += tx_pkts;
  284. s->tx_bytes += tx_bytes;
  285. /* u32 values are updated without syncp protection. */
  286. rx_errs += pcptr->rx_errs;
  287. tx_drps += pcptr->tx_drps;
  288. }
  289. s->rx_errors = rx_errs;
  290. s->rx_dropped = rx_errs;
  291. s->tx_dropped = tx_drps;
  292. }
  293. }
  294. static int ipvlan_vlan_rx_add_vid(struct net_device *dev, __be16 proto, u16 vid)
  295. {
  296. struct ipvl_dev *ipvlan = netdev_priv(dev);
  297. struct net_device *phy_dev = ipvlan->phy_dev;
  298. return vlan_vid_add(phy_dev, proto, vid);
  299. }
  300. static int ipvlan_vlan_rx_kill_vid(struct net_device *dev, __be16 proto,
  301. u16 vid)
  302. {
  303. struct ipvl_dev *ipvlan = netdev_priv(dev);
  304. struct net_device *phy_dev = ipvlan->phy_dev;
  305. vlan_vid_del(phy_dev, proto, vid);
  306. return 0;
  307. }
  308. static int ipvlan_get_iflink(const struct net_device *dev)
  309. {
  310. struct ipvl_dev *ipvlan = netdev_priv(dev);
  311. return ipvlan->phy_dev->ifindex;
  312. }
  313. static const struct net_device_ops ipvlan_netdev_ops = {
  314. .ndo_init = ipvlan_init,
  315. .ndo_uninit = ipvlan_uninit,
  316. .ndo_open = ipvlan_open,
  317. .ndo_stop = ipvlan_stop,
  318. .ndo_start_xmit = ipvlan_start_xmit,
  319. .ndo_fix_features = ipvlan_fix_features,
  320. .ndo_change_rx_flags = ipvlan_change_rx_flags,
  321. .ndo_set_rx_mode = ipvlan_set_multicast_mac_filter,
  322. .ndo_get_stats64 = ipvlan_get_stats64,
  323. .ndo_vlan_rx_add_vid = ipvlan_vlan_rx_add_vid,
  324. .ndo_vlan_rx_kill_vid = ipvlan_vlan_rx_kill_vid,
  325. .ndo_get_iflink = ipvlan_get_iflink,
  326. };
  327. static int ipvlan_hard_header(struct sk_buff *skb, struct net_device *dev,
  328. unsigned short type, const void *daddr,
  329. const void *saddr, unsigned len)
  330. {
  331. const struct ipvl_dev *ipvlan = netdev_priv(dev);
  332. struct net_device *phy_dev = ipvlan->phy_dev;
  333. /* TODO Probably use a different field than dev_addr so that the
  334. * mac-address on the virtual device is portable and can be carried
  335. * while the packets use the mac-addr on the physical device.
  336. */
  337. return dev_hard_header(skb, phy_dev, type, daddr,
  338. saddr ? : phy_dev->dev_addr, len);
  339. }
  340. static const struct header_ops ipvlan_header_ops = {
  341. .create = ipvlan_hard_header,
  342. .parse = eth_header_parse,
  343. .cache = eth_header_cache,
  344. .cache_update = eth_header_cache_update,
  345. };
  346. static int ipvlan_ethtool_get_link_ksettings(struct net_device *dev,
  347. struct ethtool_link_ksettings *cmd)
  348. {
  349. const struct ipvl_dev *ipvlan = netdev_priv(dev);
  350. return __ethtool_get_link_ksettings(ipvlan->phy_dev, cmd);
  351. }
  352. static void ipvlan_ethtool_get_drvinfo(struct net_device *dev,
  353. struct ethtool_drvinfo *drvinfo)
  354. {
  355. strlcpy(drvinfo->driver, IPVLAN_DRV, sizeof(drvinfo->driver));
  356. strlcpy(drvinfo->version, IPV_DRV_VER, sizeof(drvinfo->version));
  357. }
  358. static u32 ipvlan_ethtool_get_msglevel(struct net_device *dev)
  359. {
  360. const struct ipvl_dev *ipvlan = netdev_priv(dev);
  361. return ipvlan->msg_enable;
  362. }
  363. static void ipvlan_ethtool_set_msglevel(struct net_device *dev, u32 value)
  364. {
  365. struct ipvl_dev *ipvlan = netdev_priv(dev);
  366. ipvlan->msg_enable = value;
  367. }
  368. static const struct ethtool_ops ipvlan_ethtool_ops = {
  369. .get_link = ethtool_op_get_link,
  370. .get_link_ksettings = ipvlan_ethtool_get_link_ksettings,
  371. .get_drvinfo = ipvlan_ethtool_get_drvinfo,
  372. .get_msglevel = ipvlan_ethtool_get_msglevel,
  373. .set_msglevel = ipvlan_ethtool_set_msglevel,
  374. };
  375. static int ipvlan_nl_changelink(struct net_device *dev,
  376. struct nlattr *tb[], struct nlattr *data[],
  377. struct netlink_ext_ack *extack)
  378. {
  379. struct ipvl_dev *ipvlan = netdev_priv(dev);
  380. struct ipvl_port *port = ipvlan_port_get_rtnl(ipvlan->phy_dev);
  381. int err = 0;
  382. if (!data)
  383. return 0;
  384. if (data[IFLA_IPVLAN_MODE]) {
  385. u16 nmode = nla_get_u16(data[IFLA_IPVLAN_MODE]);
  386. err = ipvlan_set_port_mode(port, nmode);
  387. }
  388. if (!err && data[IFLA_IPVLAN_FLAGS]) {
  389. u16 flags = nla_get_u16(data[IFLA_IPVLAN_FLAGS]);
  390. if (flags & IPVLAN_F_PRIVATE)
  391. ipvlan_mark_private(port);
  392. else
  393. ipvlan_clear_private(port);
  394. if (flags & IPVLAN_F_VEPA)
  395. ipvlan_mark_vepa(port);
  396. else
  397. ipvlan_clear_vepa(port);
  398. }
  399. return err;
  400. }
  401. static size_t ipvlan_nl_getsize(const struct net_device *dev)
  402. {
  403. return (0
  404. + nla_total_size(2) /* IFLA_IPVLAN_MODE */
  405. + nla_total_size(2) /* IFLA_IPVLAN_FLAGS */
  406. );
  407. }
  408. static int ipvlan_nl_validate(struct nlattr *tb[], struct nlattr *data[],
  409. struct netlink_ext_ack *extack)
  410. {
  411. if (!data)
  412. return 0;
  413. if (data[IFLA_IPVLAN_MODE]) {
  414. u16 mode = nla_get_u16(data[IFLA_IPVLAN_MODE]);
  415. if (mode < IPVLAN_MODE_L2 || mode >= IPVLAN_MODE_MAX)
  416. return -EINVAL;
  417. }
  418. if (data[IFLA_IPVLAN_FLAGS]) {
  419. u16 flags = nla_get_u16(data[IFLA_IPVLAN_FLAGS]);
  420. /* Only two bits are used at this moment. */
  421. if (flags & ~(IPVLAN_F_PRIVATE | IPVLAN_F_VEPA))
  422. return -EINVAL;
  423. /* Also both flags can't be active at the same time. */
  424. if ((flags & (IPVLAN_F_PRIVATE | IPVLAN_F_VEPA)) ==
  425. (IPVLAN_F_PRIVATE | IPVLAN_F_VEPA))
  426. return -EINVAL;
  427. }
  428. return 0;
  429. }
  430. static int ipvlan_nl_fillinfo(struct sk_buff *skb,
  431. const struct net_device *dev)
  432. {
  433. struct ipvl_dev *ipvlan = netdev_priv(dev);
  434. struct ipvl_port *port = ipvlan_port_get_rtnl(ipvlan->phy_dev);
  435. int ret = -EINVAL;
  436. if (!port)
  437. goto err;
  438. ret = -EMSGSIZE;
  439. if (nla_put_u16(skb, IFLA_IPVLAN_MODE, port->mode))
  440. goto err;
  441. if (nla_put_u16(skb, IFLA_IPVLAN_FLAGS, port->flags))
  442. goto err;
  443. return 0;
  444. err:
  445. return ret;
  446. }
  447. int ipvlan_link_new(struct net *src_net, struct net_device *dev,
  448. struct nlattr *tb[], struct nlattr *data[],
  449. struct netlink_ext_ack *extack)
  450. {
  451. struct ipvl_dev *ipvlan = netdev_priv(dev);
  452. struct ipvl_port *port;
  453. struct net_device *phy_dev;
  454. int err;
  455. u16 mode = IPVLAN_MODE_L3;
  456. bool create = false;
  457. if (!tb[IFLA_LINK])
  458. return -EINVAL;
  459. phy_dev = __dev_get_by_index(src_net, nla_get_u32(tb[IFLA_LINK]));
  460. if (!phy_dev)
  461. return -ENODEV;
  462. if (netif_is_ipvlan(phy_dev)) {
  463. struct ipvl_dev *tmp = netdev_priv(phy_dev);
  464. phy_dev = tmp->phy_dev;
  465. } else if (!netif_is_ipvlan_port(phy_dev)) {
  466. err = ipvlan_port_create(phy_dev);
  467. if (err < 0)
  468. return err;
  469. create = true;
  470. }
  471. if (data && data[IFLA_IPVLAN_MODE])
  472. mode = nla_get_u16(data[IFLA_IPVLAN_MODE]);
  473. port = ipvlan_port_get_rtnl(phy_dev);
  474. ipvlan->phy_dev = phy_dev;
  475. ipvlan->dev = dev;
  476. ipvlan->port = port;
  477. ipvlan->sfeatures = IPVLAN_FEATURES;
  478. ipvlan_adjust_mtu(ipvlan, phy_dev);
  479. INIT_LIST_HEAD(&ipvlan->addrs);
  480. /* Flags are per port and latest update overrides. User has
  481. * to be consistent in setting it just like the mode attribute.
  482. */
  483. if (data && data[IFLA_IPVLAN_FLAGS])
  484. ipvlan->port->flags = nla_get_u16(data[IFLA_IPVLAN_FLAGS]);
  485. /* If the port-id base is at the MAX value, then wrap it around and
  486. * begin from 0x1 again. This may be due to a busy system where lots
  487. * of slaves are getting created and deleted.
  488. */
  489. if (port->dev_id_start == 0xFFFE)
  490. port->dev_id_start = 0x1;
  491. /* Since L2 address is shared among all IPvlan slaves including
  492. * master, use unique 16 bit dev-ids to diffentiate among them.
  493. * Assign IDs between 0x1 and 0xFFFE (used by the master) to each
  494. * slave link [see addrconf_ifid_eui48()].
  495. */
  496. err = ida_simple_get(&port->ida, port->dev_id_start, 0xFFFE,
  497. GFP_KERNEL);
  498. if (err < 0)
  499. err = ida_simple_get(&port->ida, 0x1, port->dev_id_start,
  500. GFP_KERNEL);
  501. if (err < 0)
  502. goto destroy_ipvlan_port;
  503. dev->dev_id = err;
  504. /* Increment id-base to the next slot for the future assignment */
  505. port->dev_id_start = err + 1;
  506. /* TODO Probably put random address here to be presented to the
  507. * world but keep using the physical-dev address for the outgoing
  508. * packets.
  509. */
  510. memcpy(dev->dev_addr, phy_dev->dev_addr, ETH_ALEN);
  511. dev->priv_flags |= IFF_IPVLAN_SLAVE;
  512. err = register_netdevice(dev);
  513. if (err < 0)
  514. goto remove_ida;
  515. err = netdev_upper_dev_link(phy_dev, dev, extack);
  516. if (err) {
  517. goto unregister_netdev;
  518. }
  519. err = ipvlan_set_port_mode(port, mode);
  520. if (err) {
  521. goto unlink_netdev;
  522. }
  523. list_add_tail_rcu(&ipvlan->pnode, &port->ipvlans);
  524. netif_stacked_transfer_operstate(phy_dev, dev);
  525. return 0;
  526. unlink_netdev:
  527. netdev_upper_dev_unlink(phy_dev, dev);
  528. unregister_netdev:
  529. unregister_netdevice(dev);
  530. remove_ida:
  531. ida_simple_remove(&port->ida, dev->dev_id);
  532. destroy_ipvlan_port:
  533. if (create)
  534. ipvlan_port_destroy(phy_dev);
  535. return err;
  536. }
  537. EXPORT_SYMBOL_GPL(ipvlan_link_new);
  538. void ipvlan_link_delete(struct net_device *dev, struct list_head *head)
  539. {
  540. struct ipvl_dev *ipvlan = netdev_priv(dev);
  541. struct ipvl_addr *addr, *next;
  542. list_for_each_entry_safe(addr, next, &ipvlan->addrs, anode) {
  543. ipvlan_ht_addr_del(addr);
  544. list_del(&addr->anode);
  545. kfree_rcu(addr, rcu);
  546. }
  547. ida_simple_remove(&ipvlan->port->ida, dev->dev_id);
  548. list_del_rcu(&ipvlan->pnode);
  549. unregister_netdevice_queue(dev, head);
  550. netdev_upper_dev_unlink(ipvlan->phy_dev, dev);
  551. }
  552. EXPORT_SYMBOL_GPL(ipvlan_link_delete);
  553. void ipvlan_link_setup(struct net_device *dev)
  554. {
  555. ether_setup(dev);
  556. dev->priv_flags &= ~(IFF_XMIT_DST_RELEASE | IFF_TX_SKB_SHARING);
  557. dev->priv_flags |= IFF_UNICAST_FLT | IFF_NO_QUEUE;
  558. dev->netdev_ops = &ipvlan_netdev_ops;
  559. dev->needs_free_netdev = true;
  560. dev->header_ops = &ipvlan_header_ops;
  561. dev->ethtool_ops = &ipvlan_ethtool_ops;
  562. }
  563. EXPORT_SYMBOL_GPL(ipvlan_link_setup);
  564. static const struct nla_policy ipvlan_nl_policy[IFLA_IPVLAN_MAX + 1] =
  565. {
  566. [IFLA_IPVLAN_MODE] = { .type = NLA_U16 },
  567. [IFLA_IPVLAN_FLAGS] = { .type = NLA_U16 },
  568. };
  569. static struct rtnl_link_ops ipvlan_link_ops = {
  570. .kind = "ipvlan",
  571. .priv_size = sizeof(struct ipvl_dev),
  572. .setup = ipvlan_link_setup,
  573. .newlink = ipvlan_link_new,
  574. .dellink = ipvlan_link_delete,
  575. };
  576. int ipvlan_link_register(struct rtnl_link_ops *ops)
  577. {
  578. ops->get_size = ipvlan_nl_getsize;
  579. ops->policy = ipvlan_nl_policy;
  580. ops->validate = ipvlan_nl_validate;
  581. ops->fill_info = ipvlan_nl_fillinfo;
  582. ops->changelink = ipvlan_nl_changelink;
  583. ops->maxtype = IFLA_IPVLAN_MAX;
  584. return rtnl_link_register(ops);
  585. }
  586. EXPORT_SYMBOL_GPL(ipvlan_link_register);
  587. static int ipvlan_device_event(struct notifier_block *unused,
  588. unsigned long event, void *ptr)
  589. {
  590. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  591. struct ipvl_dev *ipvlan, *next;
  592. struct ipvl_port *port;
  593. LIST_HEAD(lst_kill);
  594. if (!netif_is_ipvlan_port(dev))
  595. return NOTIFY_DONE;
  596. port = ipvlan_port_get_rtnl(dev);
  597. switch (event) {
  598. case NETDEV_CHANGE:
  599. list_for_each_entry(ipvlan, &port->ipvlans, pnode)
  600. netif_stacked_transfer_operstate(ipvlan->phy_dev,
  601. ipvlan->dev);
  602. break;
  603. case NETDEV_REGISTER: {
  604. struct net *oldnet, *newnet = dev_net(dev);
  605. struct ipvlan_netns *old_vnet;
  606. oldnet = read_pnet(&port->pnet);
  607. if (net_eq(newnet, oldnet))
  608. break;
  609. write_pnet(&port->pnet, newnet);
  610. old_vnet = net_generic(oldnet, ipvlan_netid);
  611. if (!old_vnet->ipvl_nf_hook_refcnt)
  612. break;
  613. ipvlan_register_nf_hook(newnet);
  614. ipvlan_unregister_nf_hook(oldnet);
  615. break;
  616. }
  617. case NETDEV_UNREGISTER:
  618. if (dev->reg_state != NETREG_UNREGISTERING)
  619. break;
  620. list_for_each_entry_safe(ipvlan, next, &port->ipvlans,
  621. pnode)
  622. ipvlan->dev->rtnl_link_ops->dellink(ipvlan->dev,
  623. &lst_kill);
  624. unregister_netdevice_many(&lst_kill);
  625. break;
  626. case NETDEV_FEAT_CHANGE:
  627. list_for_each_entry(ipvlan, &port->ipvlans, pnode) {
  628. ipvlan->dev->features = dev->features & IPVLAN_FEATURES;
  629. ipvlan->dev->gso_max_size = dev->gso_max_size;
  630. ipvlan->dev->gso_max_segs = dev->gso_max_segs;
  631. netdev_features_change(ipvlan->dev);
  632. }
  633. break;
  634. case NETDEV_CHANGEMTU:
  635. list_for_each_entry(ipvlan, &port->ipvlans, pnode)
  636. ipvlan_adjust_mtu(ipvlan, dev);
  637. break;
  638. case NETDEV_CHANGEADDR:
  639. list_for_each_entry(ipvlan, &port->ipvlans, pnode)
  640. ether_addr_copy(ipvlan->dev->dev_addr, dev->dev_addr);
  641. break;
  642. case NETDEV_PRE_TYPE_CHANGE:
  643. /* Forbid underlying device to change its type. */
  644. return NOTIFY_BAD;
  645. }
  646. return NOTIFY_DONE;
  647. }
  648. static int ipvlan_add_addr(struct ipvl_dev *ipvlan, void *iaddr, bool is_v6)
  649. {
  650. struct ipvl_addr *addr;
  651. addr = kzalloc(sizeof(struct ipvl_addr), GFP_ATOMIC);
  652. if (!addr)
  653. return -ENOMEM;
  654. addr->master = ipvlan;
  655. if (is_v6) {
  656. memcpy(&addr->ip6addr, iaddr, sizeof(struct in6_addr));
  657. addr->atype = IPVL_IPV6;
  658. } else {
  659. memcpy(&addr->ip4addr, iaddr, sizeof(struct in_addr));
  660. addr->atype = IPVL_IPV4;
  661. }
  662. list_add_tail(&addr->anode, &ipvlan->addrs);
  663. /* If the interface is not up, the address will be added to the hash
  664. * list by ipvlan_open.
  665. */
  666. if (netif_running(ipvlan->dev))
  667. ipvlan_ht_addr_add(ipvlan, addr);
  668. return 0;
  669. }
  670. static void ipvlan_del_addr(struct ipvl_dev *ipvlan, void *iaddr, bool is_v6)
  671. {
  672. struct ipvl_addr *addr;
  673. addr = ipvlan_find_addr(ipvlan, iaddr, is_v6);
  674. if (!addr)
  675. return;
  676. ipvlan_ht_addr_del(addr);
  677. list_del(&addr->anode);
  678. kfree_rcu(addr, rcu);
  679. return;
  680. }
  681. static int ipvlan_add_addr6(struct ipvl_dev *ipvlan, struct in6_addr *ip6_addr)
  682. {
  683. if (ipvlan_addr_busy(ipvlan->port, ip6_addr, true)) {
  684. netif_err(ipvlan, ifup, ipvlan->dev,
  685. "Failed to add IPv6=%pI6c addr for %s intf\n",
  686. ip6_addr, ipvlan->dev->name);
  687. return -EINVAL;
  688. }
  689. return ipvlan_add_addr(ipvlan, ip6_addr, true);
  690. }
  691. static void ipvlan_del_addr6(struct ipvl_dev *ipvlan, struct in6_addr *ip6_addr)
  692. {
  693. return ipvlan_del_addr(ipvlan, ip6_addr, true);
  694. }
  695. static int ipvlan_addr6_event(struct notifier_block *unused,
  696. unsigned long event, void *ptr)
  697. {
  698. struct inet6_ifaddr *if6 = (struct inet6_ifaddr *)ptr;
  699. struct net_device *dev = (struct net_device *)if6->idev->dev;
  700. struct ipvl_dev *ipvlan = netdev_priv(dev);
  701. if (!netif_is_ipvlan(dev))
  702. return NOTIFY_DONE;
  703. if (!ipvlan || !ipvlan->port)
  704. return NOTIFY_DONE;
  705. switch (event) {
  706. case NETDEV_UP:
  707. if (ipvlan_add_addr6(ipvlan, &if6->addr))
  708. return NOTIFY_BAD;
  709. break;
  710. case NETDEV_DOWN:
  711. ipvlan_del_addr6(ipvlan, &if6->addr);
  712. break;
  713. }
  714. return NOTIFY_OK;
  715. }
  716. static int ipvlan_addr6_validator_event(struct notifier_block *unused,
  717. unsigned long event, void *ptr)
  718. {
  719. struct in6_validator_info *i6vi = (struct in6_validator_info *)ptr;
  720. struct net_device *dev = (struct net_device *)i6vi->i6vi_dev->dev;
  721. struct ipvl_dev *ipvlan = netdev_priv(dev);
  722. /* FIXME IPv6 autoconf calls us from bh without RTNL */
  723. if (in_softirq())
  724. return NOTIFY_DONE;
  725. if (!netif_is_ipvlan(dev))
  726. return NOTIFY_DONE;
  727. if (!ipvlan || !ipvlan->port)
  728. return NOTIFY_DONE;
  729. switch (event) {
  730. case NETDEV_UP:
  731. if (ipvlan_addr_busy(ipvlan->port, &i6vi->i6vi_addr, true)) {
  732. NL_SET_ERR_MSG(i6vi->extack,
  733. "Address already assigned to an ipvlan device");
  734. return notifier_from_errno(-EADDRINUSE);
  735. }
  736. break;
  737. }
  738. return NOTIFY_OK;
  739. }
  740. static int ipvlan_add_addr4(struct ipvl_dev *ipvlan, struct in_addr *ip4_addr)
  741. {
  742. if (ipvlan_addr_busy(ipvlan->port, ip4_addr, false)) {
  743. netif_err(ipvlan, ifup, ipvlan->dev,
  744. "Failed to add IPv4=%pI4 on %s intf.\n",
  745. ip4_addr, ipvlan->dev->name);
  746. return -EINVAL;
  747. }
  748. return ipvlan_add_addr(ipvlan, ip4_addr, false);
  749. }
  750. static void ipvlan_del_addr4(struct ipvl_dev *ipvlan, struct in_addr *ip4_addr)
  751. {
  752. return ipvlan_del_addr(ipvlan, ip4_addr, false);
  753. }
  754. static int ipvlan_addr4_event(struct notifier_block *unused,
  755. unsigned long event, void *ptr)
  756. {
  757. struct in_ifaddr *if4 = (struct in_ifaddr *)ptr;
  758. struct net_device *dev = (struct net_device *)if4->ifa_dev->dev;
  759. struct ipvl_dev *ipvlan = netdev_priv(dev);
  760. struct in_addr ip4_addr;
  761. if (!netif_is_ipvlan(dev))
  762. return NOTIFY_DONE;
  763. if (!ipvlan || !ipvlan->port)
  764. return NOTIFY_DONE;
  765. switch (event) {
  766. case NETDEV_UP:
  767. ip4_addr.s_addr = if4->ifa_address;
  768. if (ipvlan_add_addr4(ipvlan, &ip4_addr))
  769. return NOTIFY_BAD;
  770. break;
  771. case NETDEV_DOWN:
  772. ip4_addr.s_addr = if4->ifa_address;
  773. ipvlan_del_addr4(ipvlan, &ip4_addr);
  774. break;
  775. }
  776. return NOTIFY_OK;
  777. }
  778. static int ipvlan_addr4_validator_event(struct notifier_block *unused,
  779. unsigned long event, void *ptr)
  780. {
  781. struct in_validator_info *ivi = (struct in_validator_info *)ptr;
  782. struct net_device *dev = (struct net_device *)ivi->ivi_dev->dev;
  783. struct ipvl_dev *ipvlan = netdev_priv(dev);
  784. if (!netif_is_ipvlan(dev))
  785. return NOTIFY_DONE;
  786. if (!ipvlan || !ipvlan->port)
  787. return NOTIFY_DONE;
  788. switch (event) {
  789. case NETDEV_UP:
  790. if (ipvlan_addr_busy(ipvlan->port, &ivi->ivi_addr, false)) {
  791. NL_SET_ERR_MSG(ivi->extack,
  792. "Address already assigned to an ipvlan device");
  793. return notifier_from_errno(-EADDRINUSE);
  794. }
  795. break;
  796. }
  797. return NOTIFY_OK;
  798. }
  799. static struct notifier_block ipvlan_addr4_notifier_block __read_mostly = {
  800. .notifier_call = ipvlan_addr4_event,
  801. };
  802. static struct notifier_block ipvlan_addr4_vtor_notifier_block __read_mostly = {
  803. .notifier_call = ipvlan_addr4_validator_event,
  804. };
  805. static struct notifier_block ipvlan_notifier_block __read_mostly = {
  806. .notifier_call = ipvlan_device_event,
  807. };
  808. static struct notifier_block ipvlan_addr6_notifier_block __read_mostly = {
  809. .notifier_call = ipvlan_addr6_event,
  810. };
  811. static struct notifier_block ipvlan_addr6_vtor_notifier_block __read_mostly = {
  812. .notifier_call = ipvlan_addr6_validator_event,
  813. };
  814. static void ipvlan_ns_exit(struct net *net)
  815. {
  816. struct ipvlan_netns *vnet = net_generic(net, ipvlan_netid);
  817. if (WARN_ON_ONCE(vnet->ipvl_nf_hook_refcnt)) {
  818. vnet->ipvl_nf_hook_refcnt = 0;
  819. nf_unregister_net_hooks(net, ipvl_nfops,
  820. ARRAY_SIZE(ipvl_nfops));
  821. }
  822. }
  823. static struct pernet_operations ipvlan_net_ops = {
  824. .id = &ipvlan_netid,
  825. .size = sizeof(struct ipvlan_netns),
  826. .exit = ipvlan_ns_exit,
  827. };
  828. static int __init ipvlan_init_module(void)
  829. {
  830. int err;
  831. ipvlan_init_secret();
  832. register_netdevice_notifier(&ipvlan_notifier_block);
  833. register_inet6addr_notifier(&ipvlan_addr6_notifier_block);
  834. register_inet6addr_validator_notifier(
  835. &ipvlan_addr6_vtor_notifier_block);
  836. register_inetaddr_notifier(&ipvlan_addr4_notifier_block);
  837. register_inetaddr_validator_notifier(&ipvlan_addr4_vtor_notifier_block);
  838. err = register_pernet_subsys(&ipvlan_net_ops);
  839. if (err < 0)
  840. goto error;
  841. err = ipvlan_link_register(&ipvlan_link_ops);
  842. if (err < 0) {
  843. unregister_pernet_subsys(&ipvlan_net_ops);
  844. goto error;
  845. }
  846. return 0;
  847. error:
  848. unregister_inetaddr_notifier(&ipvlan_addr4_notifier_block);
  849. unregister_inetaddr_validator_notifier(
  850. &ipvlan_addr4_vtor_notifier_block);
  851. unregister_inet6addr_notifier(&ipvlan_addr6_notifier_block);
  852. unregister_inet6addr_validator_notifier(
  853. &ipvlan_addr6_vtor_notifier_block);
  854. unregister_netdevice_notifier(&ipvlan_notifier_block);
  855. return err;
  856. }
  857. static void __exit ipvlan_cleanup_module(void)
  858. {
  859. rtnl_link_unregister(&ipvlan_link_ops);
  860. unregister_pernet_subsys(&ipvlan_net_ops);
  861. unregister_netdevice_notifier(&ipvlan_notifier_block);
  862. unregister_inetaddr_notifier(&ipvlan_addr4_notifier_block);
  863. unregister_inetaddr_validator_notifier(
  864. &ipvlan_addr4_vtor_notifier_block);
  865. unregister_inet6addr_notifier(&ipvlan_addr6_notifier_block);
  866. unregister_inet6addr_validator_notifier(
  867. &ipvlan_addr6_vtor_notifier_block);
  868. }
  869. module_init(ipvlan_init_module);
  870. module_exit(ipvlan_cleanup_module);
  871. MODULE_LICENSE("GPL");
  872. MODULE_AUTHOR("Mahesh Bandewar <maheshb@google.com>");
  873. MODULE_DESCRIPTION("Driver for L3 (IPv6/IPv4) based VLANs");
  874. MODULE_ALIAS_RTNL_LINK("ipvlan");