ipvlan_main.c 29 KB

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