ipvlan_main.c 28 KB

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