ipvlan_main.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /* Copyright (c) 2014 Mahesh Bandewar <maheshb@google.com>
  3. */
  4. #include <linux/ethtool.h>
  5. #include "ipvlan.h"
  6. static int ipvlan_set_port_mode(struct ipvl_port *port, u16 nval,
  7. struct netlink_ext_ack *extack)
  8. {
  9. struct ipvl_dev *ipvlan;
  10. unsigned int flags;
  11. int err;
  12. ASSERT_RTNL();
  13. if (port->mode != nval) {
  14. list_for_each_entry(ipvlan, &port->ipvlans, pnode) {
  15. flags = ipvlan->dev->flags;
  16. if (nval == IPVLAN_MODE_L3 || nval == IPVLAN_MODE_L3S) {
  17. err = dev_change_flags(ipvlan->dev,
  18. flags | IFF_NOARP,
  19. extack);
  20. } else {
  21. err = dev_change_flags(ipvlan->dev,
  22. flags & ~IFF_NOARP,
  23. extack);
  24. }
  25. if (unlikely(err))
  26. goto fail;
  27. }
  28. if (nval == IPVLAN_MODE_L3S) {
  29. /* New mode is L3S */
  30. err = ipvlan_l3s_register(port);
  31. if (err)
  32. goto fail;
  33. } else if (port->mode == IPVLAN_MODE_L3S) {
  34. /* Old mode was L3S */
  35. ipvlan_l3s_unregister(port);
  36. }
  37. port->mode = nval;
  38. }
  39. return 0;
  40. fail:
  41. /* Undo the flags changes that have been done so far. */
  42. list_for_each_entry_continue_reverse(ipvlan, &port->ipvlans, pnode) {
  43. flags = ipvlan->dev->flags;
  44. if (port->mode == IPVLAN_MODE_L3 ||
  45. port->mode == IPVLAN_MODE_L3S)
  46. dev_change_flags(ipvlan->dev, flags | IFF_NOARP,
  47. NULL);
  48. else
  49. dev_change_flags(ipvlan->dev, flags & ~IFF_NOARP,
  50. NULL);
  51. }
  52. return err;
  53. }
  54. static int ipvlan_port_create(struct net_device *dev)
  55. {
  56. struct ipvl_port *port;
  57. int err, idx;
  58. port = kzalloc(sizeof(struct ipvl_port), GFP_KERNEL);
  59. if (!port)
  60. return -ENOMEM;
  61. write_pnet(&port->pnet, dev_net(dev));
  62. port->dev = dev;
  63. port->mode = IPVLAN_MODE_L3;
  64. INIT_LIST_HEAD(&port->ipvlans);
  65. for (idx = 0; idx < IPVLAN_HASH_SIZE; idx++)
  66. INIT_HLIST_HEAD(&port->hlhead[idx]);
  67. skb_queue_head_init(&port->backlog);
  68. INIT_WORK(&port->wq, ipvlan_process_multicast);
  69. ida_init(&port->ida);
  70. port->dev_id_start = 1;
  71. err = netdev_rx_handler_register(dev, ipvlan_handle_frame, port);
  72. if (err)
  73. goto err;
  74. netdev_hold(dev, &port->dev_tracker, GFP_KERNEL);
  75. return 0;
  76. err:
  77. kfree(port);
  78. return err;
  79. }
  80. static void ipvlan_port_destroy(struct net_device *dev)
  81. {
  82. struct ipvl_port *port = ipvlan_port_get_rtnl(dev);
  83. struct sk_buff *skb;
  84. netdev_put(dev, &port->dev_tracker);
  85. if (port->mode == IPVLAN_MODE_L3S)
  86. ipvlan_l3s_unregister(port);
  87. netdev_rx_handler_unregister(dev);
  88. cancel_work_sync(&port->wq);
  89. while ((skb = __skb_dequeue(&port->backlog)) != NULL) {
  90. dev_put(skb->dev);
  91. kfree_skb(skb);
  92. }
  93. ida_destroy(&port->ida);
  94. kfree(port);
  95. }
  96. #define IPVLAN_ALWAYS_ON_OFLOADS \
  97. (NETIF_F_SG | NETIF_F_HW_CSUM | \
  98. NETIF_F_GSO_ROBUST | NETIF_F_GSO_SOFTWARE | NETIF_F_GSO_ENCAP_ALL)
  99. #define IPVLAN_ALWAYS_ON \
  100. (IPVLAN_ALWAYS_ON_OFLOADS | NETIF_F_VLAN_CHALLENGED)
  101. #define IPVLAN_FEATURES \
  102. (NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_HIGHDMA | NETIF_F_FRAGLIST | \
  103. NETIF_F_GSO | NETIF_F_ALL_TSO | NETIF_F_GSO_ROBUST | \
  104. NETIF_F_GRO | NETIF_F_RXCSUM | \
  105. NETIF_F_HW_VLAN_CTAG_FILTER | NETIF_F_HW_VLAN_STAG_FILTER)
  106. /* NETIF_F_GSO_ENCAP_ALL NETIF_F_GSO_SOFTWARE Newly added */
  107. #define IPVLAN_STATE_MASK \
  108. ((1<<__LINK_STATE_NOCARRIER) | (1<<__LINK_STATE_DORMANT))
  109. static int ipvlan_init(struct net_device *dev)
  110. {
  111. struct ipvl_dev *ipvlan = netdev_priv(dev);
  112. struct net_device *phy_dev = ipvlan->phy_dev;
  113. struct ipvl_port *port;
  114. int err;
  115. dev->state = (dev->state & ~IPVLAN_STATE_MASK) |
  116. (phy_dev->state & IPVLAN_STATE_MASK);
  117. dev->features = phy_dev->features & IPVLAN_FEATURES;
  118. dev->features |= IPVLAN_ALWAYS_ON;
  119. dev->vlan_features = phy_dev->vlan_features & IPVLAN_FEATURES;
  120. dev->vlan_features |= IPVLAN_ALWAYS_ON_OFLOADS;
  121. dev->hw_enc_features |= dev->features;
  122. dev->lltx = true;
  123. netif_inherit_tso_max(dev, phy_dev);
  124. dev->hard_header_len = phy_dev->hard_header_len;
  125. netdev_lockdep_set_classes(dev);
  126. ipvlan->pcpu_stats = netdev_alloc_pcpu_stats(struct ipvl_pcpu_stats);
  127. if (!ipvlan->pcpu_stats)
  128. return -ENOMEM;
  129. if (!netif_is_ipvlan_port(phy_dev)) {
  130. err = ipvlan_port_create(phy_dev);
  131. if (err < 0) {
  132. free_percpu(ipvlan->pcpu_stats);
  133. return err;
  134. }
  135. }
  136. port = ipvlan_port_get_rtnl(phy_dev);
  137. port->count += 1;
  138. return 0;
  139. }
  140. static void ipvlan_uninit(struct net_device *dev)
  141. {
  142. struct ipvl_dev *ipvlan = netdev_priv(dev);
  143. struct net_device *phy_dev = ipvlan->phy_dev;
  144. struct ipvl_port *port;
  145. free_percpu(ipvlan->pcpu_stats);
  146. port = ipvlan_port_get_rtnl(phy_dev);
  147. port->count -= 1;
  148. if (!port->count)
  149. ipvlan_port_destroy(port->dev);
  150. }
  151. static int ipvlan_open(struct net_device *dev)
  152. {
  153. struct ipvl_dev *ipvlan = netdev_priv(dev);
  154. struct ipvl_addr *addr;
  155. if (ipvlan->port->mode == IPVLAN_MODE_L3 ||
  156. ipvlan->port->mode == IPVLAN_MODE_L3S)
  157. dev->flags |= IFF_NOARP;
  158. else
  159. dev->flags &= ~IFF_NOARP;
  160. rcu_read_lock();
  161. list_for_each_entry_rcu(addr, &ipvlan->addrs, anode)
  162. ipvlan_ht_addr_add(ipvlan, addr);
  163. rcu_read_unlock();
  164. return 0;
  165. }
  166. static int ipvlan_stop(struct net_device *dev)
  167. {
  168. struct ipvl_dev *ipvlan = netdev_priv(dev);
  169. struct net_device *phy_dev = ipvlan->phy_dev;
  170. struct ipvl_addr *addr;
  171. dev_uc_unsync(phy_dev, dev);
  172. dev_mc_unsync(phy_dev, dev);
  173. rcu_read_lock();
  174. list_for_each_entry_rcu(addr, &ipvlan->addrs, anode)
  175. ipvlan_ht_addr_del(addr);
  176. rcu_read_unlock();
  177. return 0;
  178. }
  179. static netdev_tx_t ipvlan_start_xmit(struct sk_buff *skb,
  180. struct net_device *dev)
  181. {
  182. const struct ipvl_dev *ipvlan = netdev_priv(dev);
  183. int skblen = skb->len;
  184. int ret;
  185. ret = ipvlan_queue_xmit(skb, dev);
  186. if (likely(ret == NET_XMIT_SUCCESS || ret == NET_XMIT_CN)) {
  187. struct ipvl_pcpu_stats *pcptr;
  188. pcptr = this_cpu_ptr(ipvlan->pcpu_stats);
  189. u64_stats_update_begin(&pcptr->syncp);
  190. u64_stats_inc(&pcptr->tx_pkts);
  191. u64_stats_add(&pcptr->tx_bytes, skblen);
  192. u64_stats_update_end(&pcptr->syncp);
  193. } else {
  194. this_cpu_inc(ipvlan->pcpu_stats->tx_drps);
  195. }
  196. return ret;
  197. }
  198. static netdev_features_t ipvlan_fix_features(struct net_device *dev,
  199. netdev_features_t features)
  200. {
  201. struct ipvl_dev *ipvlan = netdev_priv(dev);
  202. features |= NETIF_F_ALL_FOR_ALL;
  203. features &= (ipvlan->sfeatures | ~IPVLAN_FEATURES);
  204. features = netdev_increment_features(ipvlan->phy_dev->features,
  205. features, features);
  206. features |= IPVLAN_ALWAYS_ON;
  207. features &= (IPVLAN_FEATURES | IPVLAN_ALWAYS_ON);
  208. return features;
  209. }
  210. static void ipvlan_change_rx_flags(struct net_device *dev, int change)
  211. {
  212. struct ipvl_dev *ipvlan = netdev_priv(dev);
  213. struct net_device *phy_dev = ipvlan->phy_dev;
  214. if (change & IFF_ALLMULTI)
  215. dev_set_allmulti(phy_dev, dev->flags & IFF_ALLMULTI? 1 : -1);
  216. }
  217. static void ipvlan_set_multicast_mac_filter(struct net_device *dev)
  218. {
  219. struct ipvl_dev *ipvlan = netdev_priv(dev);
  220. if (dev->flags & (IFF_PROMISC | IFF_ALLMULTI)) {
  221. bitmap_fill(ipvlan->mac_filters, IPVLAN_MAC_FILTER_SIZE);
  222. } else {
  223. struct netdev_hw_addr *ha;
  224. DECLARE_BITMAP(mc_filters, IPVLAN_MAC_FILTER_SIZE);
  225. bitmap_zero(mc_filters, IPVLAN_MAC_FILTER_SIZE);
  226. netdev_for_each_mc_addr(ha, dev)
  227. __set_bit(ipvlan_mac_hash(ha->addr), mc_filters);
  228. /* Turn-on broadcast bit irrespective of address family,
  229. * since broadcast is deferred to a work-queue, hence no
  230. * impact on fast-path processing.
  231. */
  232. __set_bit(ipvlan_mac_hash(dev->broadcast), mc_filters);
  233. bitmap_copy(ipvlan->mac_filters, mc_filters,
  234. IPVLAN_MAC_FILTER_SIZE);
  235. }
  236. dev_uc_sync(ipvlan->phy_dev, dev);
  237. dev_mc_sync(ipvlan->phy_dev, dev);
  238. }
  239. static void ipvlan_get_stats64(struct net_device *dev,
  240. struct rtnl_link_stats64 *s)
  241. {
  242. struct ipvl_dev *ipvlan = netdev_priv(dev);
  243. if (ipvlan->pcpu_stats) {
  244. struct ipvl_pcpu_stats *pcptr;
  245. u64 rx_pkts, rx_bytes, rx_mcast, tx_pkts, tx_bytes;
  246. u32 rx_errs = 0, tx_drps = 0;
  247. u32 strt;
  248. int idx;
  249. for_each_possible_cpu(idx) {
  250. pcptr = per_cpu_ptr(ipvlan->pcpu_stats, idx);
  251. do {
  252. strt = u64_stats_fetch_begin(&pcptr->syncp);
  253. rx_pkts = u64_stats_read(&pcptr->rx_pkts);
  254. rx_bytes = u64_stats_read(&pcptr->rx_bytes);
  255. rx_mcast = u64_stats_read(&pcptr->rx_mcast);
  256. tx_pkts = u64_stats_read(&pcptr->tx_pkts);
  257. tx_bytes = u64_stats_read(&pcptr->tx_bytes);
  258. } while (u64_stats_fetch_retry(&pcptr->syncp,
  259. strt));
  260. s->rx_packets += rx_pkts;
  261. s->rx_bytes += rx_bytes;
  262. s->multicast += rx_mcast;
  263. s->tx_packets += tx_pkts;
  264. s->tx_bytes += tx_bytes;
  265. /* u32 values are updated without syncp protection. */
  266. rx_errs += READ_ONCE(pcptr->rx_errs);
  267. tx_drps += READ_ONCE(pcptr->tx_drps);
  268. }
  269. s->rx_errors = rx_errs;
  270. s->rx_dropped = rx_errs;
  271. s->tx_dropped = tx_drps;
  272. }
  273. s->tx_errors = DEV_STATS_READ(dev, tx_errors);
  274. }
  275. static int ipvlan_vlan_rx_add_vid(struct net_device *dev, __be16 proto, u16 vid)
  276. {
  277. struct ipvl_dev *ipvlan = netdev_priv(dev);
  278. struct net_device *phy_dev = ipvlan->phy_dev;
  279. return vlan_vid_add(phy_dev, proto, vid);
  280. }
  281. static int ipvlan_vlan_rx_kill_vid(struct net_device *dev, __be16 proto,
  282. u16 vid)
  283. {
  284. struct ipvl_dev *ipvlan = netdev_priv(dev);
  285. struct net_device *phy_dev = ipvlan->phy_dev;
  286. vlan_vid_del(phy_dev, proto, vid);
  287. return 0;
  288. }
  289. static int ipvlan_get_iflink(const struct net_device *dev)
  290. {
  291. struct ipvl_dev *ipvlan = netdev_priv(dev);
  292. return READ_ONCE(ipvlan->phy_dev->ifindex);
  293. }
  294. static const struct net_device_ops ipvlan_netdev_ops = {
  295. .ndo_init = ipvlan_init,
  296. .ndo_uninit = ipvlan_uninit,
  297. .ndo_open = ipvlan_open,
  298. .ndo_stop = ipvlan_stop,
  299. .ndo_start_xmit = ipvlan_start_xmit,
  300. .ndo_fix_features = ipvlan_fix_features,
  301. .ndo_change_rx_flags = ipvlan_change_rx_flags,
  302. .ndo_set_rx_mode = ipvlan_set_multicast_mac_filter,
  303. .ndo_get_stats64 = ipvlan_get_stats64,
  304. .ndo_vlan_rx_add_vid = ipvlan_vlan_rx_add_vid,
  305. .ndo_vlan_rx_kill_vid = ipvlan_vlan_rx_kill_vid,
  306. .ndo_get_iflink = ipvlan_get_iflink,
  307. };
  308. static int ipvlan_hard_header(struct sk_buff *skb, struct net_device *dev,
  309. unsigned short type, const void *daddr,
  310. const void *saddr, unsigned len)
  311. {
  312. const struct ipvl_dev *ipvlan = netdev_priv(dev);
  313. struct net_device *phy_dev = ipvlan->phy_dev;
  314. /* TODO Probably use a different field than dev_addr so that the
  315. * mac-address on the virtual device is portable and can be carried
  316. * while the packets use the mac-addr on the physical device.
  317. */
  318. return dev_hard_header(skb, phy_dev, type, daddr,
  319. saddr ? : phy_dev->dev_addr, len);
  320. }
  321. static const struct header_ops ipvlan_header_ops = {
  322. .create = ipvlan_hard_header,
  323. .parse = eth_header_parse,
  324. .cache = eth_header_cache,
  325. .cache_update = eth_header_cache_update,
  326. .parse_protocol = eth_header_parse_protocol,
  327. };
  328. static void ipvlan_adjust_mtu(struct ipvl_dev *ipvlan, struct net_device *dev)
  329. {
  330. ipvlan->dev->mtu = dev->mtu;
  331. }
  332. static bool netif_is_ipvlan(const struct net_device *dev)
  333. {
  334. /* both ipvlan and ipvtap devices use the same netdev_ops */
  335. return dev->netdev_ops == &ipvlan_netdev_ops;
  336. }
  337. static int ipvlan_ethtool_get_link_ksettings(struct net_device *dev,
  338. struct ethtool_link_ksettings *cmd)
  339. {
  340. const struct ipvl_dev *ipvlan = netdev_priv(dev);
  341. return __ethtool_get_link_ksettings(ipvlan->phy_dev, cmd);
  342. }
  343. static void ipvlan_ethtool_get_drvinfo(struct net_device *dev,
  344. struct ethtool_drvinfo *drvinfo)
  345. {
  346. strscpy(drvinfo->driver, IPVLAN_DRV, sizeof(drvinfo->driver));
  347. strscpy(drvinfo->version, IPV_DRV_VER, sizeof(drvinfo->version));
  348. }
  349. static u32 ipvlan_ethtool_get_msglevel(struct net_device *dev)
  350. {
  351. const struct ipvl_dev *ipvlan = netdev_priv(dev);
  352. return ipvlan->msg_enable;
  353. }
  354. static void ipvlan_ethtool_set_msglevel(struct net_device *dev, u32 value)
  355. {
  356. struct ipvl_dev *ipvlan = netdev_priv(dev);
  357. ipvlan->msg_enable = value;
  358. }
  359. static const struct ethtool_ops ipvlan_ethtool_ops = {
  360. .get_link = ethtool_op_get_link,
  361. .get_link_ksettings = ipvlan_ethtool_get_link_ksettings,
  362. .get_drvinfo = ipvlan_ethtool_get_drvinfo,
  363. .get_msglevel = ipvlan_ethtool_get_msglevel,
  364. .set_msglevel = ipvlan_ethtool_set_msglevel,
  365. };
  366. static int ipvlan_nl_changelink(struct net_device *dev,
  367. struct nlattr *tb[], struct nlattr *data[],
  368. struct netlink_ext_ack *extack)
  369. {
  370. struct ipvl_dev *ipvlan = netdev_priv(dev);
  371. struct ipvl_port *port = ipvlan_port_get_rtnl(ipvlan->phy_dev);
  372. int err = 0;
  373. if (!data)
  374. return 0;
  375. if (!ns_capable(dev_net(ipvlan->phy_dev)->user_ns, CAP_NET_ADMIN))
  376. return -EPERM;
  377. if (data[IFLA_IPVLAN_MODE]) {
  378. u16 nmode = nla_get_u16(data[IFLA_IPVLAN_MODE]);
  379. err = ipvlan_set_port_mode(port, nmode, extack);
  380. }
  381. if (!err && data[IFLA_IPVLAN_FLAGS]) {
  382. u16 flags = nla_get_u16(data[IFLA_IPVLAN_FLAGS]);
  383. if (flags & IPVLAN_F_PRIVATE)
  384. ipvlan_mark_private(port);
  385. else
  386. ipvlan_clear_private(port);
  387. if (flags & IPVLAN_F_VEPA)
  388. ipvlan_mark_vepa(port);
  389. else
  390. ipvlan_clear_vepa(port);
  391. }
  392. return err;
  393. }
  394. static size_t ipvlan_nl_getsize(const struct net_device *dev)
  395. {
  396. return (0
  397. + nla_total_size(2) /* IFLA_IPVLAN_MODE */
  398. + nla_total_size(2) /* IFLA_IPVLAN_FLAGS */
  399. );
  400. }
  401. static int ipvlan_nl_validate(struct nlattr *tb[], struct nlattr *data[],
  402. struct netlink_ext_ack *extack)
  403. {
  404. if (!data)
  405. return 0;
  406. if (data[IFLA_IPVLAN_MODE]) {
  407. u16 mode = nla_get_u16(data[IFLA_IPVLAN_MODE]);
  408. if (mode >= IPVLAN_MODE_MAX)
  409. return -EINVAL;
  410. }
  411. if (data[IFLA_IPVLAN_FLAGS]) {
  412. u16 flags = nla_get_u16(data[IFLA_IPVLAN_FLAGS]);
  413. /* Only two bits are used at this moment. */
  414. if (flags & ~(IPVLAN_F_PRIVATE | IPVLAN_F_VEPA))
  415. return -EINVAL;
  416. /* Also both flags can't be active at the same time. */
  417. if ((flags & (IPVLAN_F_PRIVATE | IPVLAN_F_VEPA)) ==
  418. (IPVLAN_F_PRIVATE | IPVLAN_F_VEPA))
  419. return -EINVAL;
  420. }
  421. return 0;
  422. }
  423. static int ipvlan_nl_fillinfo(struct sk_buff *skb,
  424. const struct net_device *dev)
  425. {
  426. struct ipvl_dev *ipvlan = netdev_priv(dev);
  427. struct ipvl_port *port = ipvlan_port_get_rtnl(ipvlan->phy_dev);
  428. int ret = -EINVAL;
  429. if (!port)
  430. goto err;
  431. ret = -EMSGSIZE;
  432. if (nla_put_u16(skb, IFLA_IPVLAN_MODE, port->mode))
  433. goto err;
  434. if (nla_put_u16(skb, IFLA_IPVLAN_FLAGS, port->flags))
  435. goto err;
  436. return 0;
  437. err:
  438. return ret;
  439. }
  440. int ipvlan_link_new(struct net *src_net, struct net_device *dev,
  441. struct nlattr *tb[], struct nlattr *data[],
  442. struct netlink_ext_ack *extack)
  443. {
  444. struct ipvl_dev *ipvlan = netdev_priv(dev);
  445. struct ipvl_port *port;
  446. struct net_device *phy_dev;
  447. int err;
  448. u16 mode = IPVLAN_MODE_L3;
  449. if (!tb[IFLA_LINK])
  450. return -EINVAL;
  451. phy_dev = __dev_get_by_index(src_net, nla_get_u32(tb[IFLA_LINK]));
  452. if (!phy_dev)
  453. return -ENODEV;
  454. if (netif_is_ipvlan(phy_dev)) {
  455. struct ipvl_dev *tmp = netdev_priv(phy_dev);
  456. phy_dev = tmp->phy_dev;
  457. if (!ns_capable(dev_net(phy_dev)->user_ns, CAP_NET_ADMIN))
  458. return -EPERM;
  459. } else if (!netif_is_ipvlan_port(phy_dev)) {
  460. /* Exit early if the underlying link is invalid or busy */
  461. if (phy_dev->type != ARPHRD_ETHER ||
  462. phy_dev->flags & IFF_LOOPBACK) {
  463. netdev_err(phy_dev,
  464. "Master is either lo or non-ether device\n");
  465. return -EINVAL;
  466. }
  467. if (netdev_is_rx_handler_busy(phy_dev)) {
  468. netdev_err(phy_dev, "Device is already in use.\n");
  469. return -EBUSY;
  470. }
  471. }
  472. ipvlan->phy_dev = phy_dev;
  473. ipvlan->dev = dev;
  474. ipvlan->sfeatures = IPVLAN_FEATURES;
  475. if (!tb[IFLA_MTU])
  476. ipvlan_adjust_mtu(ipvlan, phy_dev);
  477. INIT_LIST_HEAD(&ipvlan->addrs);
  478. spin_lock_init(&ipvlan->addrs_lock);
  479. /* TODO Probably put random address here to be presented to the
  480. * world but keep using the physical-dev address for the outgoing
  481. * packets.
  482. */
  483. eth_hw_addr_set(dev, phy_dev->dev_addr);
  484. dev->priv_flags |= IFF_NO_RX_HANDLER;
  485. err = register_netdevice(dev);
  486. if (err < 0)
  487. return err;
  488. /* ipvlan_init() would have created the port, if required */
  489. port = ipvlan_port_get_rtnl(phy_dev);
  490. ipvlan->port = port;
  491. /* If the port-id base is at the MAX value, then wrap it around and
  492. * begin from 0x1 again. This may be due to a busy system where lots
  493. * of slaves are getting created and deleted.
  494. */
  495. if (port->dev_id_start == 0xFFFE)
  496. port->dev_id_start = 0x1;
  497. /* Since L2 address is shared among all IPvlan slaves including
  498. * master, use unique 16 bit dev-ids to differentiate among them.
  499. * Assign IDs between 0x1 and 0xFFFE (used by the master) to each
  500. * slave link [see addrconf_ifid_eui48()].
  501. */
  502. err = ida_alloc_range(&port->ida, port->dev_id_start, 0xFFFD,
  503. GFP_KERNEL);
  504. if (err < 0)
  505. err = ida_alloc_range(&port->ida, 0x1, port->dev_id_start - 1,
  506. GFP_KERNEL);
  507. if (err < 0)
  508. goto unregister_netdev;
  509. dev->dev_id = err;
  510. /* Increment id-base to the next slot for the future assignment */
  511. port->dev_id_start = err + 1;
  512. err = netdev_upper_dev_link(phy_dev, dev, extack);
  513. if (err)
  514. goto remove_ida;
  515. /* Flags are per port and latest update overrides. User has
  516. * to be consistent in setting it just like the mode attribute.
  517. */
  518. if (data && data[IFLA_IPVLAN_FLAGS])
  519. port->flags = nla_get_u16(data[IFLA_IPVLAN_FLAGS]);
  520. if (data && data[IFLA_IPVLAN_MODE])
  521. mode = nla_get_u16(data[IFLA_IPVLAN_MODE]);
  522. err = ipvlan_set_port_mode(port, mode, extack);
  523. if (err)
  524. goto unlink_netdev;
  525. list_add_tail_rcu(&ipvlan->pnode, &port->ipvlans);
  526. netif_stacked_transfer_operstate(phy_dev, dev);
  527. return 0;
  528. unlink_netdev:
  529. netdev_upper_dev_unlink(phy_dev, dev);
  530. remove_ida:
  531. ida_free(&port->ida, dev->dev_id);
  532. unregister_netdev:
  533. unregister_netdevice(dev);
  534. return err;
  535. }
  536. EXPORT_SYMBOL_GPL(ipvlan_link_new);
  537. void ipvlan_link_delete(struct net_device *dev, struct list_head *head)
  538. {
  539. struct ipvl_dev *ipvlan = netdev_priv(dev);
  540. struct ipvl_addr *addr, *next;
  541. spin_lock_bh(&ipvlan->addrs_lock);
  542. list_for_each_entry_safe(addr, next, &ipvlan->addrs, anode) {
  543. ipvlan_ht_addr_del(addr);
  544. list_del_rcu(&addr->anode);
  545. kfree_rcu(addr, rcu);
  546. }
  547. spin_unlock_bh(&ipvlan->addrs_lock);
  548. ida_free(&ipvlan->port->ida, dev->dev_id);
  549. list_del_rcu(&ipvlan->pnode);
  550. unregister_netdevice_queue(dev, head);
  551. netdev_upper_dev_unlink(ipvlan->phy_dev, dev);
  552. }
  553. EXPORT_SYMBOL_GPL(ipvlan_link_delete);
  554. void ipvlan_link_setup(struct net_device *dev)
  555. {
  556. ether_setup(dev);
  557. dev->max_mtu = ETH_MAX_MTU;
  558. dev->priv_flags &= ~(IFF_XMIT_DST_RELEASE | IFF_TX_SKB_SHARING);
  559. dev->priv_flags |= IFF_UNICAST_FLT | IFF_NO_QUEUE;
  560. dev->netdev_ops = &ipvlan_netdev_ops;
  561. dev->needs_free_netdev = true;
  562. dev->header_ops = &ipvlan_header_ops;
  563. dev->ethtool_ops = &ipvlan_ethtool_ops;
  564. }
  565. EXPORT_SYMBOL_GPL(ipvlan_link_setup);
  566. static const struct nla_policy ipvlan_nl_policy[IFLA_IPVLAN_MAX + 1] =
  567. {
  568. [IFLA_IPVLAN_MODE] = { .type = NLA_U16 },
  569. [IFLA_IPVLAN_FLAGS] = { .type = NLA_U16 },
  570. };
  571. static struct net *ipvlan_get_link_net(const struct net_device *dev)
  572. {
  573. struct ipvl_dev *ipvlan = netdev_priv(dev);
  574. return dev_net(ipvlan->phy_dev);
  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. .get_link_net = ipvlan_get_link_net,
  583. };
  584. int ipvlan_link_register(struct rtnl_link_ops *ops)
  585. {
  586. ops->get_size = ipvlan_nl_getsize;
  587. ops->policy = ipvlan_nl_policy;
  588. ops->validate = ipvlan_nl_validate;
  589. ops->fill_info = ipvlan_nl_fillinfo;
  590. ops->changelink = ipvlan_nl_changelink;
  591. ops->maxtype = IFLA_IPVLAN_MAX;
  592. return rtnl_link_register(ops);
  593. }
  594. EXPORT_SYMBOL_GPL(ipvlan_link_register);
  595. static int ipvlan_device_event(struct notifier_block *unused,
  596. unsigned long event, void *ptr)
  597. {
  598. struct netlink_ext_ack *extack = netdev_notifier_info_to_extack(ptr);
  599. struct netdev_notifier_pre_changeaddr_info *prechaddr_info;
  600. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  601. struct ipvl_dev *ipvlan, *next;
  602. struct ipvl_port *port;
  603. LIST_HEAD(lst_kill);
  604. int err;
  605. if (!netif_is_ipvlan_port(dev))
  606. return NOTIFY_DONE;
  607. port = ipvlan_port_get_rtnl(dev);
  608. switch (event) {
  609. case NETDEV_UP:
  610. case NETDEV_DOWN:
  611. case NETDEV_CHANGE:
  612. list_for_each_entry(ipvlan, &port->ipvlans, pnode)
  613. netif_stacked_transfer_operstate(ipvlan->phy_dev,
  614. ipvlan->dev);
  615. break;
  616. case NETDEV_REGISTER: {
  617. struct net *oldnet, *newnet = dev_net(dev);
  618. oldnet = read_pnet(&port->pnet);
  619. if (net_eq(newnet, oldnet))
  620. break;
  621. write_pnet(&port->pnet, newnet);
  622. if (port->mode == IPVLAN_MODE_L3S)
  623. ipvlan_migrate_l3s_hook(oldnet, newnet);
  624. break;
  625. }
  626. case NETDEV_UNREGISTER:
  627. if (dev->reg_state != NETREG_UNREGISTERING)
  628. break;
  629. list_for_each_entry_safe(ipvlan, next, &port->ipvlans, pnode)
  630. ipvlan->dev->rtnl_link_ops->dellink(ipvlan->dev,
  631. &lst_kill);
  632. unregister_netdevice_many(&lst_kill);
  633. break;
  634. case NETDEV_FEAT_CHANGE:
  635. list_for_each_entry(ipvlan, &port->ipvlans, pnode) {
  636. netif_inherit_tso_max(ipvlan->dev, dev);
  637. netdev_update_features(ipvlan->dev);
  638. }
  639. break;
  640. case NETDEV_CHANGEMTU:
  641. list_for_each_entry(ipvlan, &port->ipvlans, pnode)
  642. ipvlan_adjust_mtu(ipvlan, dev);
  643. break;
  644. case NETDEV_PRE_CHANGEADDR:
  645. prechaddr_info = ptr;
  646. list_for_each_entry(ipvlan, &port->ipvlans, pnode) {
  647. err = dev_pre_changeaddr_notify(ipvlan->dev,
  648. prechaddr_info->dev_addr,
  649. extack);
  650. if (err)
  651. return notifier_from_errno(err);
  652. }
  653. break;
  654. case NETDEV_CHANGEADDR:
  655. list_for_each_entry(ipvlan, &port->ipvlans, pnode) {
  656. eth_hw_addr_set(ipvlan->dev, 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 int __init ipvlan_init_module(void)
  847. {
  848. int err;
  849. ipvlan_init_secret();
  850. register_netdevice_notifier(&ipvlan_notifier_block);
  851. #if IS_ENABLED(CONFIG_IPV6)
  852. register_inet6addr_notifier(&ipvlan_addr6_notifier_block);
  853. register_inet6addr_validator_notifier(
  854. &ipvlan_addr6_vtor_notifier_block);
  855. #endif
  856. register_inetaddr_notifier(&ipvlan_addr4_notifier_block);
  857. register_inetaddr_validator_notifier(&ipvlan_addr4_vtor_notifier_block);
  858. err = ipvlan_l3s_init();
  859. if (err < 0)
  860. goto error;
  861. err = ipvlan_link_register(&ipvlan_link_ops);
  862. if (err < 0) {
  863. ipvlan_l3s_cleanup();
  864. goto error;
  865. }
  866. return 0;
  867. error:
  868. unregister_inetaddr_notifier(&ipvlan_addr4_notifier_block);
  869. unregister_inetaddr_validator_notifier(
  870. &ipvlan_addr4_vtor_notifier_block);
  871. #if IS_ENABLED(CONFIG_IPV6)
  872. unregister_inet6addr_notifier(&ipvlan_addr6_notifier_block);
  873. unregister_inet6addr_validator_notifier(
  874. &ipvlan_addr6_vtor_notifier_block);
  875. #endif
  876. unregister_netdevice_notifier(&ipvlan_notifier_block);
  877. return err;
  878. }
  879. static void __exit ipvlan_cleanup_module(void)
  880. {
  881. rtnl_link_unregister(&ipvlan_link_ops);
  882. ipvlan_l3s_cleanup();
  883. unregister_netdevice_notifier(&ipvlan_notifier_block);
  884. unregister_inetaddr_notifier(&ipvlan_addr4_notifier_block);
  885. unregister_inetaddr_validator_notifier(
  886. &ipvlan_addr4_vtor_notifier_block);
  887. #if IS_ENABLED(CONFIG_IPV6)
  888. unregister_inet6addr_notifier(&ipvlan_addr6_notifier_block);
  889. unregister_inet6addr_validator_notifier(
  890. &ipvlan_addr6_vtor_notifier_block);
  891. #endif
  892. }
  893. module_init(ipvlan_init_module);
  894. module_exit(ipvlan_cleanup_module);
  895. MODULE_LICENSE("GPL");
  896. MODULE_AUTHOR("Mahesh Bandewar <maheshb@google.com>");
  897. MODULE_DESCRIPTION("Driver for L3 (IPv6/IPv4) based VLANs");
  898. MODULE_ALIAS_RTNL_LINK("ipvlan");