ipvlan_main.c 28 KB

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