ipvlan_main.c 28 KB

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