ipvlan_main.c 28 KB

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