ipvlan_main.c 29 KB

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