ipvlan_main.c 27 KB

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