vrf.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375
  1. /*
  2. * vrf.c: device driver to encapsulate a VRF space
  3. *
  4. * Copyright (c) 2015 Cumulus Networks. All rights reserved.
  5. * Copyright (c) 2015 Shrijeet Mukherjee <shm@cumulusnetworks.com>
  6. * Copyright (c) 2015 David Ahern <dsa@cumulusnetworks.com>
  7. *
  8. * Based on dummy, team and ipvlan drivers
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. */
  15. #include <linux/module.h>
  16. #include <linux/kernel.h>
  17. #include <linux/netdevice.h>
  18. #include <linux/etherdevice.h>
  19. #include <linux/ip.h>
  20. #include <linux/init.h>
  21. #include <linux/moduleparam.h>
  22. #include <linux/netfilter.h>
  23. #include <linux/rtnetlink.h>
  24. #include <net/rtnetlink.h>
  25. #include <linux/u64_stats_sync.h>
  26. #include <linux/hashtable.h>
  27. #include <linux/inetdevice.h>
  28. #include <net/arp.h>
  29. #include <net/ip.h>
  30. #include <net/ip_fib.h>
  31. #include <net/ip6_fib.h>
  32. #include <net/ip6_route.h>
  33. #include <net/route.h>
  34. #include <net/addrconf.h>
  35. #include <net/l3mdev.h>
  36. #include <net/fib_rules.h>
  37. #define DRV_NAME "vrf"
  38. #define DRV_VERSION "1.0"
  39. #define FIB_RULE_PREF 1000 /* default preference for FIB rules */
  40. static bool add_fib_rules = true;
  41. struct net_vrf {
  42. struct rtable __rcu *rth;
  43. struct rtable __rcu *rth_local;
  44. struct rt6_info __rcu *rt6;
  45. struct rt6_info __rcu *rt6_local;
  46. u32 tb_id;
  47. };
  48. struct pcpu_dstats {
  49. u64 tx_pkts;
  50. u64 tx_bytes;
  51. u64 tx_drps;
  52. u64 rx_pkts;
  53. u64 rx_bytes;
  54. u64 rx_drps;
  55. struct u64_stats_sync syncp;
  56. };
  57. static void vrf_rx_stats(struct net_device *dev, int len)
  58. {
  59. struct pcpu_dstats *dstats = this_cpu_ptr(dev->dstats);
  60. u64_stats_update_begin(&dstats->syncp);
  61. dstats->rx_pkts++;
  62. dstats->rx_bytes += len;
  63. u64_stats_update_end(&dstats->syncp);
  64. }
  65. static void vrf_tx_error(struct net_device *vrf_dev, struct sk_buff *skb)
  66. {
  67. vrf_dev->stats.tx_errors++;
  68. kfree_skb(skb);
  69. }
  70. static struct rtnl_link_stats64 *vrf_get_stats64(struct net_device *dev,
  71. struct rtnl_link_stats64 *stats)
  72. {
  73. int i;
  74. for_each_possible_cpu(i) {
  75. const struct pcpu_dstats *dstats;
  76. u64 tbytes, tpkts, tdrops, rbytes, rpkts;
  77. unsigned int start;
  78. dstats = per_cpu_ptr(dev->dstats, i);
  79. do {
  80. start = u64_stats_fetch_begin_irq(&dstats->syncp);
  81. tbytes = dstats->tx_bytes;
  82. tpkts = dstats->tx_pkts;
  83. tdrops = dstats->tx_drps;
  84. rbytes = dstats->rx_bytes;
  85. rpkts = dstats->rx_pkts;
  86. } while (u64_stats_fetch_retry_irq(&dstats->syncp, start));
  87. stats->tx_bytes += tbytes;
  88. stats->tx_packets += tpkts;
  89. stats->tx_dropped += tdrops;
  90. stats->rx_bytes += rbytes;
  91. stats->rx_packets += rpkts;
  92. }
  93. return stats;
  94. }
  95. /* Local traffic destined to local address. Reinsert the packet to rx
  96. * path, similar to loopback handling.
  97. */
  98. static int vrf_local_xmit(struct sk_buff *skb, struct net_device *dev,
  99. struct dst_entry *dst)
  100. {
  101. int len = skb->len;
  102. skb_orphan(skb);
  103. skb_dst_set(skb, dst);
  104. skb_dst_force(skb);
  105. /* set pkt_type to avoid skb hitting packet taps twice -
  106. * once on Tx and again in Rx processing
  107. */
  108. skb->pkt_type = PACKET_LOOPBACK;
  109. skb->protocol = eth_type_trans(skb, dev);
  110. if (likely(netif_rx(skb) == NET_RX_SUCCESS))
  111. vrf_rx_stats(dev, len);
  112. else
  113. this_cpu_inc(dev->dstats->rx_drps);
  114. return NETDEV_TX_OK;
  115. }
  116. #if IS_ENABLED(CONFIG_IPV6)
  117. static int vrf_ip6_local_out(struct net *net, struct sock *sk,
  118. struct sk_buff *skb)
  119. {
  120. int err;
  121. err = nf_hook(NFPROTO_IPV6, NF_INET_LOCAL_OUT, net,
  122. sk, skb, NULL, skb_dst(skb)->dev, dst_output);
  123. if (likely(err == 1))
  124. err = dst_output(net, sk, skb);
  125. return err;
  126. }
  127. static netdev_tx_t vrf_process_v6_outbound(struct sk_buff *skb,
  128. struct net_device *dev)
  129. {
  130. const struct ipv6hdr *iph = ipv6_hdr(skb);
  131. struct net *net = dev_net(skb->dev);
  132. struct flowi6 fl6 = {
  133. /* needed to match OIF rule */
  134. .flowi6_oif = dev->ifindex,
  135. .flowi6_iif = LOOPBACK_IFINDEX,
  136. .daddr = iph->daddr,
  137. .saddr = iph->saddr,
  138. .flowlabel = ip6_flowinfo(iph),
  139. .flowi6_mark = skb->mark,
  140. .flowi6_proto = iph->nexthdr,
  141. .flowi6_flags = FLOWI_FLAG_SKIP_NH_OIF,
  142. };
  143. int ret = NET_XMIT_DROP;
  144. struct dst_entry *dst;
  145. struct dst_entry *dst_null = &net->ipv6.ip6_null_entry->dst;
  146. dst = ip6_route_output(net, NULL, &fl6);
  147. if (dst == dst_null)
  148. goto err;
  149. skb_dst_drop(skb);
  150. /* if dst.dev is loopback or the VRF device again this is locally
  151. * originated traffic destined to a local address. Short circuit
  152. * to Rx path using our local dst
  153. */
  154. if (dst->dev == net->loopback_dev || dst->dev == dev) {
  155. struct net_vrf *vrf = netdev_priv(dev);
  156. struct rt6_info *rt6_local;
  157. /* release looked up dst and use cached local dst */
  158. dst_release(dst);
  159. rcu_read_lock();
  160. rt6_local = rcu_dereference(vrf->rt6_local);
  161. if (unlikely(!rt6_local)) {
  162. rcu_read_unlock();
  163. goto err;
  164. }
  165. /* Ordering issue: cached local dst is created on newlink
  166. * before the IPv6 initialization. Using the local dst
  167. * requires rt6i_idev to be set so make sure it is.
  168. */
  169. if (unlikely(!rt6_local->rt6i_idev)) {
  170. rt6_local->rt6i_idev = in6_dev_get(dev);
  171. if (!rt6_local->rt6i_idev) {
  172. rcu_read_unlock();
  173. goto err;
  174. }
  175. }
  176. dst = &rt6_local->dst;
  177. dst_hold(dst);
  178. rcu_read_unlock();
  179. return vrf_local_xmit(skb, dev, &rt6_local->dst);
  180. }
  181. skb_dst_set(skb, dst);
  182. /* strip the ethernet header added for pass through VRF device */
  183. __skb_pull(skb, skb_network_offset(skb));
  184. ret = vrf_ip6_local_out(net, skb->sk, skb);
  185. if (unlikely(net_xmit_eval(ret)))
  186. dev->stats.tx_errors++;
  187. else
  188. ret = NET_XMIT_SUCCESS;
  189. return ret;
  190. err:
  191. vrf_tx_error(dev, skb);
  192. return NET_XMIT_DROP;
  193. }
  194. #else
  195. static netdev_tx_t vrf_process_v6_outbound(struct sk_buff *skb,
  196. struct net_device *dev)
  197. {
  198. vrf_tx_error(dev, skb);
  199. return NET_XMIT_DROP;
  200. }
  201. #endif
  202. /* based on ip_local_out; can't use it b/c the dst is switched pointing to us */
  203. static int vrf_ip_local_out(struct net *net, struct sock *sk,
  204. struct sk_buff *skb)
  205. {
  206. int err;
  207. err = nf_hook(NFPROTO_IPV4, NF_INET_LOCAL_OUT, net, sk,
  208. skb, NULL, skb_dst(skb)->dev, dst_output);
  209. if (likely(err == 1))
  210. err = dst_output(net, sk, skb);
  211. return err;
  212. }
  213. static netdev_tx_t vrf_process_v4_outbound(struct sk_buff *skb,
  214. struct net_device *vrf_dev)
  215. {
  216. struct iphdr *ip4h = ip_hdr(skb);
  217. int ret = NET_XMIT_DROP;
  218. struct flowi4 fl4 = {
  219. /* needed to match OIF rule */
  220. .flowi4_oif = vrf_dev->ifindex,
  221. .flowi4_iif = LOOPBACK_IFINDEX,
  222. .flowi4_tos = RT_TOS(ip4h->tos),
  223. .flowi4_flags = FLOWI_FLAG_ANYSRC | FLOWI_FLAG_SKIP_NH_OIF,
  224. .flowi4_proto = ip4h->protocol,
  225. .daddr = ip4h->daddr,
  226. .saddr = ip4h->saddr,
  227. };
  228. struct net *net = dev_net(vrf_dev);
  229. struct rtable *rt;
  230. rt = ip_route_output_flow(net, &fl4, NULL);
  231. if (IS_ERR(rt))
  232. goto err;
  233. skb_dst_drop(skb);
  234. /* if dst.dev is loopback or the VRF device again this is locally
  235. * originated traffic destined to a local address. Short circuit
  236. * to Rx path using our local dst
  237. */
  238. if (rt->dst.dev == net->loopback_dev || rt->dst.dev == vrf_dev) {
  239. struct net_vrf *vrf = netdev_priv(vrf_dev);
  240. struct rtable *rth_local;
  241. struct dst_entry *dst = NULL;
  242. ip_rt_put(rt);
  243. rcu_read_lock();
  244. rth_local = rcu_dereference(vrf->rth_local);
  245. if (likely(rth_local)) {
  246. dst = &rth_local->dst;
  247. dst_hold(dst);
  248. }
  249. rcu_read_unlock();
  250. if (unlikely(!dst))
  251. goto err;
  252. return vrf_local_xmit(skb, vrf_dev, dst);
  253. }
  254. skb_dst_set(skb, &rt->dst);
  255. /* strip the ethernet header added for pass through VRF device */
  256. __skb_pull(skb, skb_network_offset(skb));
  257. if (!ip4h->saddr) {
  258. ip4h->saddr = inet_select_addr(skb_dst(skb)->dev, 0,
  259. RT_SCOPE_LINK);
  260. }
  261. ret = vrf_ip_local_out(dev_net(skb_dst(skb)->dev), skb->sk, skb);
  262. if (unlikely(net_xmit_eval(ret)))
  263. vrf_dev->stats.tx_errors++;
  264. else
  265. ret = NET_XMIT_SUCCESS;
  266. out:
  267. return ret;
  268. err:
  269. vrf_tx_error(vrf_dev, skb);
  270. goto out;
  271. }
  272. static netdev_tx_t is_ip_tx_frame(struct sk_buff *skb, struct net_device *dev)
  273. {
  274. switch (skb->protocol) {
  275. case htons(ETH_P_IP):
  276. return vrf_process_v4_outbound(skb, dev);
  277. case htons(ETH_P_IPV6):
  278. return vrf_process_v6_outbound(skb, dev);
  279. default:
  280. vrf_tx_error(dev, skb);
  281. return NET_XMIT_DROP;
  282. }
  283. }
  284. static netdev_tx_t vrf_xmit(struct sk_buff *skb, struct net_device *dev)
  285. {
  286. netdev_tx_t ret = is_ip_tx_frame(skb, dev);
  287. if (likely(ret == NET_XMIT_SUCCESS || ret == NET_XMIT_CN)) {
  288. struct pcpu_dstats *dstats = this_cpu_ptr(dev->dstats);
  289. u64_stats_update_begin(&dstats->syncp);
  290. dstats->tx_pkts++;
  291. dstats->tx_bytes += skb->len;
  292. u64_stats_update_end(&dstats->syncp);
  293. } else {
  294. this_cpu_inc(dev->dstats->tx_drps);
  295. }
  296. return ret;
  297. }
  298. #if IS_ENABLED(CONFIG_IPV6)
  299. /* modelled after ip6_finish_output2 */
  300. static int vrf_finish_output6(struct net *net, struct sock *sk,
  301. struct sk_buff *skb)
  302. {
  303. struct dst_entry *dst = skb_dst(skb);
  304. struct net_device *dev = dst->dev;
  305. struct neighbour *neigh;
  306. struct in6_addr *nexthop;
  307. int ret;
  308. nf_reset(skb);
  309. skb->protocol = htons(ETH_P_IPV6);
  310. skb->dev = dev;
  311. rcu_read_lock_bh();
  312. nexthop = rt6_nexthop((struct rt6_info *)dst, &ipv6_hdr(skb)->daddr);
  313. neigh = __ipv6_neigh_lookup_noref(dst->dev, nexthop);
  314. if (unlikely(!neigh))
  315. neigh = __neigh_create(&nd_tbl, nexthop, dst->dev, false);
  316. if (!IS_ERR(neigh)) {
  317. ret = dst_neigh_output(dst, neigh, skb);
  318. rcu_read_unlock_bh();
  319. return ret;
  320. }
  321. rcu_read_unlock_bh();
  322. IP6_INC_STATS(dev_net(dst->dev),
  323. ip6_dst_idev(dst), IPSTATS_MIB_OUTNOROUTES);
  324. kfree_skb(skb);
  325. return -EINVAL;
  326. }
  327. /* modelled after ip6_output */
  328. static int vrf_output6(struct net *net, struct sock *sk, struct sk_buff *skb)
  329. {
  330. return NF_HOOK_COND(NFPROTO_IPV6, NF_INET_POST_ROUTING,
  331. net, sk, skb, NULL, skb_dst(skb)->dev,
  332. vrf_finish_output6,
  333. !(IP6CB(skb)->flags & IP6SKB_REROUTED));
  334. }
  335. /* set dst on skb to send packet to us via dev_xmit path. Allows
  336. * packet to go through device based features such as qdisc, netfilter
  337. * hooks and packet sockets with skb->dev set to vrf device.
  338. */
  339. static struct sk_buff *vrf_ip6_out(struct net_device *vrf_dev,
  340. struct sock *sk,
  341. struct sk_buff *skb)
  342. {
  343. struct net_vrf *vrf = netdev_priv(vrf_dev);
  344. struct dst_entry *dst = NULL;
  345. struct rt6_info *rt6;
  346. /* don't divert link scope packets */
  347. if (rt6_need_strict(&ipv6_hdr(skb)->daddr))
  348. return skb;
  349. rcu_read_lock();
  350. rt6 = rcu_dereference(vrf->rt6);
  351. if (likely(rt6)) {
  352. dst = &rt6->dst;
  353. dst_hold(dst);
  354. }
  355. rcu_read_unlock();
  356. if (unlikely(!dst)) {
  357. vrf_tx_error(vrf_dev, skb);
  358. return NULL;
  359. }
  360. skb_dst_drop(skb);
  361. skb_dst_set(skb, dst);
  362. return skb;
  363. }
  364. /* holding rtnl */
  365. static void vrf_rt6_release(struct net_device *dev, struct net_vrf *vrf)
  366. {
  367. struct rt6_info *rt6 = rtnl_dereference(vrf->rt6);
  368. struct rt6_info *rt6_local = rtnl_dereference(vrf->rt6_local);
  369. struct net *net = dev_net(dev);
  370. struct dst_entry *dst;
  371. RCU_INIT_POINTER(vrf->rt6, NULL);
  372. RCU_INIT_POINTER(vrf->rt6_local, NULL);
  373. synchronize_rcu();
  374. /* move dev in dst's to loopback so this VRF device can be deleted
  375. * - based on dst_ifdown
  376. */
  377. if (rt6) {
  378. dst = &rt6->dst;
  379. dev_put(dst->dev);
  380. dst->dev = net->loopback_dev;
  381. dev_hold(dst->dev);
  382. dst_release(dst);
  383. }
  384. if (rt6_local) {
  385. if (rt6_local->rt6i_idev)
  386. in6_dev_put(rt6_local->rt6i_idev);
  387. dst = &rt6_local->dst;
  388. dev_put(dst->dev);
  389. dst->dev = net->loopback_dev;
  390. dev_hold(dst->dev);
  391. dst_release(dst);
  392. }
  393. }
  394. static int vrf_rt6_create(struct net_device *dev)
  395. {
  396. int flags = DST_HOST | DST_NOPOLICY | DST_NOXFRM | DST_NOCACHE;
  397. struct net_vrf *vrf = netdev_priv(dev);
  398. struct net *net = dev_net(dev);
  399. struct fib6_table *rt6i_table;
  400. struct rt6_info *rt6, *rt6_local;
  401. int rc = -ENOMEM;
  402. /* IPv6 can be CONFIG enabled and then disabled runtime */
  403. if (!ipv6_mod_enabled())
  404. return 0;
  405. rt6i_table = fib6_new_table(net, vrf->tb_id);
  406. if (!rt6i_table)
  407. goto out;
  408. /* create a dst for routing packets out a VRF device */
  409. rt6 = ip6_dst_alloc(net, dev, flags);
  410. if (!rt6)
  411. goto out;
  412. dst_hold(&rt6->dst);
  413. rt6->rt6i_table = rt6i_table;
  414. rt6->dst.output = vrf_output6;
  415. /* create a dst for local routing - packets sent locally
  416. * to local address via the VRF device as a loopback
  417. */
  418. rt6_local = ip6_dst_alloc(net, dev, flags);
  419. if (!rt6_local) {
  420. dst_release(&rt6->dst);
  421. goto out;
  422. }
  423. dst_hold(&rt6_local->dst);
  424. rt6_local->rt6i_idev = in6_dev_get(dev);
  425. rt6_local->rt6i_flags = RTF_UP | RTF_NONEXTHOP | RTF_LOCAL;
  426. rt6_local->rt6i_table = rt6i_table;
  427. rt6_local->dst.input = ip6_input;
  428. rcu_assign_pointer(vrf->rt6, rt6);
  429. rcu_assign_pointer(vrf->rt6_local, rt6_local);
  430. rc = 0;
  431. out:
  432. return rc;
  433. }
  434. #else
  435. static struct sk_buff *vrf_ip6_out(struct net_device *vrf_dev,
  436. struct sock *sk,
  437. struct sk_buff *skb)
  438. {
  439. return skb;
  440. }
  441. static void vrf_rt6_release(struct net_device *dev, struct net_vrf *vrf)
  442. {
  443. }
  444. static int vrf_rt6_create(struct net_device *dev)
  445. {
  446. return 0;
  447. }
  448. #endif
  449. /* modelled after ip_finish_output2 */
  450. static int vrf_finish_output(struct net *net, struct sock *sk, struct sk_buff *skb)
  451. {
  452. struct dst_entry *dst = skb_dst(skb);
  453. struct rtable *rt = (struct rtable *)dst;
  454. struct net_device *dev = dst->dev;
  455. unsigned int hh_len = LL_RESERVED_SPACE(dev);
  456. struct neighbour *neigh;
  457. u32 nexthop;
  458. int ret = -EINVAL;
  459. nf_reset(skb);
  460. /* Be paranoid, rather than too clever. */
  461. if (unlikely(skb_headroom(skb) < hh_len && dev->header_ops)) {
  462. struct sk_buff *skb2;
  463. skb2 = skb_realloc_headroom(skb, LL_RESERVED_SPACE(dev));
  464. if (!skb2) {
  465. ret = -ENOMEM;
  466. goto err;
  467. }
  468. if (skb->sk)
  469. skb_set_owner_w(skb2, skb->sk);
  470. consume_skb(skb);
  471. skb = skb2;
  472. }
  473. rcu_read_lock_bh();
  474. nexthop = (__force u32)rt_nexthop(rt, ip_hdr(skb)->daddr);
  475. neigh = __ipv4_neigh_lookup_noref(dev, nexthop);
  476. if (unlikely(!neigh))
  477. neigh = __neigh_create(&arp_tbl, &nexthop, dev, false);
  478. if (!IS_ERR(neigh))
  479. ret = dst_neigh_output(dst, neigh, skb);
  480. rcu_read_unlock_bh();
  481. err:
  482. if (unlikely(ret < 0))
  483. vrf_tx_error(skb->dev, skb);
  484. return ret;
  485. }
  486. static int vrf_output(struct net *net, struct sock *sk, struct sk_buff *skb)
  487. {
  488. struct net_device *dev = skb_dst(skb)->dev;
  489. IP_UPD_PO_STATS(net, IPSTATS_MIB_OUT, skb->len);
  490. skb->dev = dev;
  491. skb->protocol = htons(ETH_P_IP);
  492. return NF_HOOK_COND(NFPROTO_IPV4, NF_INET_POST_ROUTING,
  493. net, sk, skb, NULL, dev,
  494. vrf_finish_output,
  495. !(IPCB(skb)->flags & IPSKB_REROUTED));
  496. }
  497. /* set dst on skb to send packet to us via dev_xmit path. Allows
  498. * packet to go through device based features such as qdisc, netfilter
  499. * hooks and packet sockets with skb->dev set to vrf device.
  500. */
  501. static struct sk_buff *vrf_ip_out(struct net_device *vrf_dev,
  502. struct sock *sk,
  503. struct sk_buff *skb)
  504. {
  505. struct net_vrf *vrf = netdev_priv(vrf_dev);
  506. struct dst_entry *dst = NULL;
  507. struct rtable *rth;
  508. /* don't divert multicast */
  509. if (ipv4_is_multicast(ip_hdr(skb)->daddr))
  510. return skb;
  511. rcu_read_lock();
  512. rth = rcu_dereference(vrf->rth);
  513. if (likely(rth)) {
  514. dst = &rth->dst;
  515. dst_hold(dst);
  516. }
  517. rcu_read_unlock();
  518. if (unlikely(!dst)) {
  519. vrf_tx_error(vrf_dev, skb);
  520. return NULL;
  521. }
  522. skb_dst_drop(skb);
  523. skb_dst_set(skb, dst);
  524. return skb;
  525. }
  526. /* called with rcu lock held */
  527. static struct sk_buff *vrf_l3_out(struct net_device *vrf_dev,
  528. struct sock *sk,
  529. struct sk_buff *skb,
  530. u16 proto)
  531. {
  532. switch (proto) {
  533. case AF_INET:
  534. return vrf_ip_out(vrf_dev, sk, skb);
  535. case AF_INET6:
  536. return vrf_ip6_out(vrf_dev, sk, skb);
  537. }
  538. return skb;
  539. }
  540. /* holding rtnl */
  541. static void vrf_rtable_release(struct net_device *dev, struct net_vrf *vrf)
  542. {
  543. struct rtable *rth = rtnl_dereference(vrf->rth);
  544. struct rtable *rth_local = rtnl_dereference(vrf->rth_local);
  545. struct net *net = dev_net(dev);
  546. struct dst_entry *dst;
  547. RCU_INIT_POINTER(vrf->rth, NULL);
  548. RCU_INIT_POINTER(vrf->rth_local, NULL);
  549. synchronize_rcu();
  550. /* move dev in dst's to loopback so this VRF device can be deleted
  551. * - based on dst_ifdown
  552. */
  553. if (rth) {
  554. dst = &rth->dst;
  555. dev_put(dst->dev);
  556. dst->dev = net->loopback_dev;
  557. dev_hold(dst->dev);
  558. dst_release(dst);
  559. }
  560. if (rth_local) {
  561. dst = &rth_local->dst;
  562. dev_put(dst->dev);
  563. dst->dev = net->loopback_dev;
  564. dev_hold(dst->dev);
  565. dst_release(dst);
  566. }
  567. }
  568. static int vrf_rtable_create(struct net_device *dev)
  569. {
  570. struct net_vrf *vrf = netdev_priv(dev);
  571. struct rtable *rth, *rth_local;
  572. if (!fib_new_table(dev_net(dev), vrf->tb_id))
  573. return -ENOMEM;
  574. /* create a dst for routing packets out through a VRF device */
  575. rth = rt_dst_alloc(dev, 0, RTN_UNICAST, 1, 1, 0);
  576. if (!rth)
  577. return -ENOMEM;
  578. /* create a dst for local ingress routing - packets sent locally
  579. * to local address via the VRF device as a loopback
  580. */
  581. rth_local = rt_dst_alloc(dev, RTCF_LOCAL, RTN_LOCAL, 1, 1, 0);
  582. if (!rth_local) {
  583. dst_release(&rth->dst);
  584. return -ENOMEM;
  585. }
  586. rth->dst.output = vrf_output;
  587. rth->rt_table_id = vrf->tb_id;
  588. rth_local->rt_table_id = vrf->tb_id;
  589. rcu_assign_pointer(vrf->rth, rth);
  590. rcu_assign_pointer(vrf->rth_local, rth_local);
  591. return 0;
  592. }
  593. /**************************** device handling ********************/
  594. /* cycle interface to flush neighbor cache and move routes across tables */
  595. static void cycle_netdev(struct net_device *dev)
  596. {
  597. unsigned int flags = dev->flags;
  598. int ret;
  599. if (!netif_running(dev))
  600. return;
  601. ret = dev_change_flags(dev, flags & ~IFF_UP);
  602. if (ret >= 0)
  603. ret = dev_change_flags(dev, flags);
  604. if (ret < 0) {
  605. netdev_err(dev,
  606. "Failed to cycle device %s; route tables might be wrong!\n",
  607. dev->name);
  608. }
  609. }
  610. static int do_vrf_add_slave(struct net_device *dev, struct net_device *port_dev)
  611. {
  612. int ret;
  613. ret = netdev_master_upper_dev_link(port_dev, dev, NULL, NULL);
  614. if (ret < 0)
  615. return ret;
  616. port_dev->priv_flags |= IFF_L3MDEV_SLAVE;
  617. cycle_netdev(port_dev);
  618. return 0;
  619. }
  620. static int vrf_add_slave(struct net_device *dev, struct net_device *port_dev)
  621. {
  622. if (netif_is_l3_master(port_dev) || netif_is_l3_slave(port_dev))
  623. return -EINVAL;
  624. return do_vrf_add_slave(dev, port_dev);
  625. }
  626. /* inverse of do_vrf_add_slave */
  627. static int do_vrf_del_slave(struct net_device *dev, struct net_device *port_dev)
  628. {
  629. netdev_upper_dev_unlink(port_dev, dev);
  630. port_dev->priv_flags &= ~IFF_L3MDEV_SLAVE;
  631. cycle_netdev(port_dev);
  632. return 0;
  633. }
  634. static int vrf_del_slave(struct net_device *dev, struct net_device *port_dev)
  635. {
  636. return do_vrf_del_slave(dev, port_dev);
  637. }
  638. static void vrf_dev_uninit(struct net_device *dev)
  639. {
  640. struct net_vrf *vrf = netdev_priv(dev);
  641. struct net_device *port_dev;
  642. struct list_head *iter;
  643. vrf_rtable_release(dev, vrf);
  644. vrf_rt6_release(dev, vrf);
  645. netdev_for_each_lower_dev(dev, port_dev, iter)
  646. vrf_del_slave(dev, port_dev);
  647. free_percpu(dev->dstats);
  648. dev->dstats = NULL;
  649. }
  650. static int vrf_dev_init(struct net_device *dev)
  651. {
  652. struct net_vrf *vrf = netdev_priv(dev);
  653. dev->dstats = netdev_alloc_pcpu_stats(struct pcpu_dstats);
  654. if (!dev->dstats)
  655. goto out_nomem;
  656. /* create the default dst which points back to us */
  657. if (vrf_rtable_create(dev) != 0)
  658. goto out_stats;
  659. if (vrf_rt6_create(dev) != 0)
  660. goto out_rth;
  661. dev->flags = IFF_MASTER | IFF_NOARP;
  662. /* MTU is irrelevant for VRF device; set to 64k similar to lo */
  663. dev->mtu = 64 * 1024;
  664. /* similarly, oper state is irrelevant; set to up to avoid confusion */
  665. dev->operstate = IF_OPER_UP;
  666. netdev_lockdep_set_classes(dev);
  667. return 0;
  668. out_rth:
  669. vrf_rtable_release(dev, vrf);
  670. out_stats:
  671. free_percpu(dev->dstats);
  672. dev->dstats = NULL;
  673. out_nomem:
  674. return -ENOMEM;
  675. }
  676. static const struct net_device_ops vrf_netdev_ops = {
  677. .ndo_init = vrf_dev_init,
  678. .ndo_uninit = vrf_dev_uninit,
  679. .ndo_start_xmit = vrf_xmit,
  680. .ndo_get_stats64 = vrf_get_stats64,
  681. .ndo_add_slave = vrf_add_slave,
  682. .ndo_del_slave = vrf_del_slave,
  683. };
  684. static u32 vrf_fib_table(const struct net_device *dev)
  685. {
  686. struct net_vrf *vrf = netdev_priv(dev);
  687. return vrf->tb_id;
  688. }
  689. static int vrf_rcv_finish(struct net *net, struct sock *sk, struct sk_buff *skb)
  690. {
  691. return 0;
  692. }
  693. static struct sk_buff *vrf_rcv_nfhook(u8 pf, unsigned int hook,
  694. struct sk_buff *skb,
  695. struct net_device *dev)
  696. {
  697. struct net *net = dev_net(dev);
  698. if (NF_HOOK(pf, hook, net, NULL, skb, dev, NULL, vrf_rcv_finish) < 0)
  699. skb = NULL; /* kfree_skb(skb) handled by nf code */
  700. return skb;
  701. }
  702. #if IS_ENABLED(CONFIG_IPV6)
  703. /* neighbor handling is done with actual device; do not want
  704. * to flip skb->dev for those ndisc packets. This really fails
  705. * for multiple next protocols (e.g., NEXTHDR_HOP). But it is
  706. * a start.
  707. */
  708. static bool ipv6_ndisc_frame(const struct sk_buff *skb)
  709. {
  710. const struct ipv6hdr *iph = ipv6_hdr(skb);
  711. bool rc = false;
  712. if (iph->nexthdr == NEXTHDR_ICMP) {
  713. const struct icmp6hdr *icmph;
  714. struct icmp6hdr _icmph;
  715. icmph = skb_header_pointer(skb, sizeof(*iph),
  716. sizeof(_icmph), &_icmph);
  717. if (!icmph)
  718. goto out;
  719. switch (icmph->icmp6_type) {
  720. case NDISC_ROUTER_SOLICITATION:
  721. case NDISC_ROUTER_ADVERTISEMENT:
  722. case NDISC_NEIGHBOUR_SOLICITATION:
  723. case NDISC_NEIGHBOUR_ADVERTISEMENT:
  724. case NDISC_REDIRECT:
  725. rc = true;
  726. break;
  727. }
  728. }
  729. out:
  730. return rc;
  731. }
  732. static struct rt6_info *vrf_ip6_route_lookup(struct net *net,
  733. const struct net_device *dev,
  734. struct flowi6 *fl6,
  735. int ifindex,
  736. int flags)
  737. {
  738. struct net_vrf *vrf = netdev_priv(dev);
  739. struct fib6_table *table = NULL;
  740. struct rt6_info *rt6;
  741. rcu_read_lock();
  742. /* fib6_table does not have a refcnt and can not be freed */
  743. rt6 = rcu_dereference(vrf->rt6);
  744. if (likely(rt6))
  745. table = rt6->rt6i_table;
  746. rcu_read_unlock();
  747. if (!table)
  748. return NULL;
  749. return ip6_pol_route(net, table, ifindex, fl6, flags);
  750. }
  751. static void vrf_ip6_input_dst(struct sk_buff *skb, struct net_device *vrf_dev,
  752. int ifindex)
  753. {
  754. const struct ipv6hdr *iph = ipv6_hdr(skb);
  755. struct flowi6 fl6 = {
  756. .daddr = iph->daddr,
  757. .saddr = iph->saddr,
  758. .flowlabel = ip6_flowinfo(iph),
  759. .flowi6_mark = skb->mark,
  760. .flowi6_proto = iph->nexthdr,
  761. .flowi6_iif = ifindex,
  762. };
  763. struct net *net = dev_net(vrf_dev);
  764. struct rt6_info *rt6;
  765. rt6 = vrf_ip6_route_lookup(net, vrf_dev, &fl6, ifindex,
  766. RT6_LOOKUP_F_HAS_SADDR | RT6_LOOKUP_F_IFACE);
  767. if (unlikely(!rt6))
  768. return;
  769. if (unlikely(&rt6->dst == &net->ipv6.ip6_null_entry->dst))
  770. return;
  771. skb_dst_set(skb, &rt6->dst);
  772. }
  773. static struct sk_buff *vrf_ip6_rcv(struct net_device *vrf_dev,
  774. struct sk_buff *skb)
  775. {
  776. int orig_iif = skb->skb_iif;
  777. bool need_strict;
  778. /* loopback traffic; do not push through packet taps again.
  779. * Reset pkt_type for upper layers to process skb
  780. */
  781. if (skb->pkt_type == PACKET_LOOPBACK) {
  782. skb->dev = vrf_dev;
  783. skb->skb_iif = vrf_dev->ifindex;
  784. IP6CB(skb)->flags |= IP6SKB_L3SLAVE;
  785. skb->pkt_type = PACKET_HOST;
  786. goto out;
  787. }
  788. /* if packet is NDISC or addressed to multicast or link-local
  789. * then keep the ingress interface
  790. */
  791. need_strict = rt6_need_strict(&ipv6_hdr(skb)->daddr);
  792. if (!ipv6_ndisc_frame(skb) && !need_strict) {
  793. vrf_rx_stats(vrf_dev, skb->len);
  794. skb->dev = vrf_dev;
  795. skb->skb_iif = vrf_dev->ifindex;
  796. skb_push(skb, skb->mac_len);
  797. dev_queue_xmit_nit(skb, vrf_dev);
  798. skb_pull(skb, skb->mac_len);
  799. IP6CB(skb)->flags |= IP6SKB_L3SLAVE;
  800. }
  801. if (need_strict)
  802. vrf_ip6_input_dst(skb, vrf_dev, orig_iif);
  803. skb = vrf_rcv_nfhook(NFPROTO_IPV6, NF_INET_PRE_ROUTING, skb, vrf_dev);
  804. out:
  805. return skb;
  806. }
  807. #else
  808. static struct sk_buff *vrf_ip6_rcv(struct net_device *vrf_dev,
  809. struct sk_buff *skb)
  810. {
  811. return skb;
  812. }
  813. #endif
  814. static struct sk_buff *vrf_ip_rcv(struct net_device *vrf_dev,
  815. struct sk_buff *skb)
  816. {
  817. skb->dev = vrf_dev;
  818. skb->skb_iif = vrf_dev->ifindex;
  819. IPCB(skb)->flags |= IPSKB_L3SLAVE;
  820. if (ipv4_is_multicast(ip_hdr(skb)->daddr))
  821. goto out;
  822. /* loopback traffic; do not push through packet taps again.
  823. * Reset pkt_type for upper layers to process skb
  824. */
  825. if (skb->pkt_type == PACKET_LOOPBACK) {
  826. skb->pkt_type = PACKET_HOST;
  827. goto out;
  828. }
  829. vrf_rx_stats(vrf_dev, skb->len);
  830. skb_push(skb, skb->mac_len);
  831. dev_queue_xmit_nit(skb, vrf_dev);
  832. skb_pull(skb, skb->mac_len);
  833. skb = vrf_rcv_nfhook(NFPROTO_IPV4, NF_INET_PRE_ROUTING, skb, vrf_dev);
  834. out:
  835. return skb;
  836. }
  837. /* called with rcu lock held */
  838. static struct sk_buff *vrf_l3_rcv(struct net_device *vrf_dev,
  839. struct sk_buff *skb,
  840. u16 proto)
  841. {
  842. switch (proto) {
  843. case AF_INET:
  844. return vrf_ip_rcv(vrf_dev, skb);
  845. case AF_INET6:
  846. return vrf_ip6_rcv(vrf_dev, skb);
  847. }
  848. return skb;
  849. }
  850. #if IS_ENABLED(CONFIG_IPV6)
  851. /* send to link-local or multicast address via interface enslaved to
  852. * VRF device. Force lookup to VRF table without changing flow struct
  853. */
  854. static struct dst_entry *vrf_link_scope_lookup(const struct net_device *dev,
  855. struct flowi6 *fl6)
  856. {
  857. struct net *net = dev_net(dev);
  858. int flags = RT6_LOOKUP_F_IFACE;
  859. struct dst_entry *dst = NULL;
  860. struct rt6_info *rt;
  861. /* VRF device does not have a link-local address and
  862. * sending packets to link-local or mcast addresses over
  863. * a VRF device does not make sense
  864. */
  865. if (fl6->flowi6_oif == dev->ifindex) {
  866. dst = &net->ipv6.ip6_null_entry->dst;
  867. dst_hold(dst);
  868. return dst;
  869. }
  870. if (!ipv6_addr_any(&fl6->saddr))
  871. flags |= RT6_LOOKUP_F_HAS_SADDR;
  872. rt = vrf_ip6_route_lookup(net, dev, fl6, fl6->flowi6_oif, flags);
  873. if (rt)
  874. dst = &rt->dst;
  875. return dst;
  876. }
  877. #endif
  878. static const struct l3mdev_ops vrf_l3mdev_ops = {
  879. .l3mdev_fib_table = vrf_fib_table,
  880. .l3mdev_l3_rcv = vrf_l3_rcv,
  881. .l3mdev_l3_out = vrf_l3_out,
  882. #if IS_ENABLED(CONFIG_IPV6)
  883. .l3mdev_link_scope_lookup = vrf_link_scope_lookup,
  884. #endif
  885. };
  886. static void vrf_get_drvinfo(struct net_device *dev,
  887. struct ethtool_drvinfo *info)
  888. {
  889. strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
  890. strlcpy(info->version, DRV_VERSION, sizeof(info->version));
  891. }
  892. static const struct ethtool_ops vrf_ethtool_ops = {
  893. .get_drvinfo = vrf_get_drvinfo,
  894. };
  895. static inline size_t vrf_fib_rule_nl_size(void)
  896. {
  897. size_t sz;
  898. sz = NLMSG_ALIGN(sizeof(struct fib_rule_hdr));
  899. sz += nla_total_size(sizeof(u8)); /* FRA_L3MDEV */
  900. sz += nla_total_size(sizeof(u32)); /* FRA_PRIORITY */
  901. return sz;
  902. }
  903. static int vrf_fib_rule(const struct net_device *dev, __u8 family, bool add_it)
  904. {
  905. struct fib_rule_hdr *frh;
  906. struct nlmsghdr *nlh;
  907. struct sk_buff *skb;
  908. int err;
  909. if (family == AF_INET6 && !ipv6_mod_enabled())
  910. return 0;
  911. skb = nlmsg_new(vrf_fib_rule_nl_size(), GFP_KERNEL);
  912. if (!skb)
  913. return -ENOMEM;
  914. nlh = nlmsg_put(skb, 0, 0, 0, sizeof(*frh), 0);
  915. if (!nlh)
  916. goto nla_put_failure;
  917. /* rule only needs to appear once */
  918. nlh->nlmsg_flags &= NLM_F_EXCL;
  919. frh = nlmsg_data(nlh);
  920. memset(frh, 0, sizeof(*frh));
  921. frh->family = family;
  922. frh->action = FR_ACT_TO_TBL;
  923. if (nla_put_u32(skb, FRA_L3MDEV, 1))
  924. goto nla_put_failure;
  925. if (nla_put_u32(skb, FRA_PRIORITY, FIB_RULE_PREF))
  926. goto nla_put_failure;
  927. nlmsg_end(skb, nlh);
  928. /* fib_nl_{new,del}rule handling looks for net from skb->sk */
  929. skb->sk = dev_net(dev)->rtnl;
  930. if (add_it) {
  931. err = fib_nl_newrule(skb, nlh);
  932. if (err == -EEXIST)
  933. err = 0;
  934. } else {
  935. err = fib_nl_delrule(skb, nlh);
  936. if (err == -ENOENT)
  937. err = 0;
  938. }
  939. nlmsg_free(skb);
  940. return err;
  941. nla_put_failure:
  942. nlmsg_free(skb);
  943. return -EMSGSIZE;
  944. }
  945. static int vrf_add_fib_rules(const struct net_device *dev)
  946. {
  947. int err;
  948. err = vrf_fib_rule(dev, AF_INET, true);
  949. if (err < 0)
  950. goto out_err;
  951. err = vrf_fib_rule(dev, AF_INET6, true);
  952. if (err < 0)
  953. goto ipv6_err;
  954. #if IS_ENABLED(CONFIG_IP_MROUTE_MULTIPLE_TABLES)
  955. err = vrf_fib_rule(dev, RTNL_FAMILY_IPMR, true);
  956. if (err < 0)
  957. goto ipmr_err;
  958. #endif
  959. return 0;
  960. #if IS_ENABLED(CONFIG_IP_MROUTE_MULTIPLE_TABLES)
  961. ipmr_err:
  962. vrf_fib_rule(dev, AF_INET6, false);
  963. #endif
  964. ipv6_err:
  965. vrf_fib_rule(dev, AF_INET, false);
  966. out_err:
  967. netdev_err(dev, "Failed to add FIB rules.\n");
  968. return err;
  969. }
  970. static void vrf_setup(struct net_device *dev)
  971. {
  972. ether_setup(dev);
  973. /* Initialize the device structure. */
  974. dev->netdev_ops = &vrf_netdev_ops;
  975. dev->l3mdev_ops = &vrf_l3mdev_ops;
  976. dev->ethtool_ops = &vrf_ethtool_ops;
  977. dev->destructor = free_netdev;
  978. /* Fill in device structure with ethernet-generic values. */
  979. eth_hw_addr_random(dev);
  980. /* don't acquire vrf device's netif_tx_lock when transmitting */
  981. dev->features |= NETIF_F_LLTX;
  982. /* don't allow vrf devices to change network namespaces. */
  983. dev->features |= NETIF_F_NETNS_LOCAL;
  984. /* does not make sense for a VLAN to be added to a vrf device */
  985. dev->features |= NETIF_F_VLAN_CHALLENGED;
  986. /* enable offload features */
  987. dev->features |= NETIF_F_GSO_SOFTWARE;
  988. dev->features |= NETIF_F_RXCSUM | NETIF_F_HW_CSUM;
  989. dev->features |= NETIF_F_SG | NETIF_F_FRAGLIST | NETIF_F_HIGHDMA;
  990. dev->hw_features = dev->features;
  991. dev->hw_enc_features = dev->features;
  992. /* default to no qdisc; user can add if desired */
  993. dev->priv_flags |= IFF_NO_QUEUE;
  994. }
  995. static int vrf_validate(struct nlattr *tb[], struct nlattr *data[])
  996. {
  997. if (tb[IFLA_ADDRESS]) {
  998. if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
  999. return -EINVAL;
  1000. if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
  1001. return -EADDRNOTAVAIL;
  1002. }
  1003. return 0;
  1004. }
  1005. static void vrf_dellink(struct net_device *dev, struct list_head *head)
  1006. {
  1007. unregister_netdevice_queue(dev, head);
  1008. }
  1009. static int vrf_newlink(struct net *src_net, struct net_device *dev,
  1010. struct nlattr *tb[], struct nlattr *data[])
  1011. {
  1012. struct net_vrf *vrf = netdev_priv(dev);
  1013. int err;
  1014. if (!data || !data[IFLA_VRF_TABLE])
  1015. return -EINVAL;
  1016. vrf->tb_id = nla_get_u32(data[IFLA_VRF_TABLE]);
  1017. if (vrf->tb_id == RT_TABLE_UNSPEC)
  1018. return -EINVAL;
  1019. dev->priv_flags |= IFF_L3MDEV_MASTER;
  1020. err = register_netdevice(dev);
  1021. if (err)
  1022. goto out;
  1023. if (add_fib_rules) {
  1024. err = vrf_add_fib_rules(dev);
  1025. if (err) {
  1026. unregister_netdevice(dev);
  1027. goto out;
  1028. }
  1029. add_fib_rules = false;
  1030. }
  1031. out:
  1032. return err;
  1033. }
  1034. static size_t vrf_nl_getsize(const struct net_device *dev)
  1035. {
  1036. return nla_total_size(sizeof(u32)); /* IFLA_VRF_TABLE */
  1037. }
  1038. static int vrf_fillinfo(struct sk_buff *skb,
  1039. const struct net_device *dev)
  1040. {
  1041. struct net_vrf *vrf = netdev_priv(dev);
  1042. return nla_put_u32(skb, IFLA_VRF_TABLE, vrf->tb_id);
  1043. }
  1044. static size_t vrf_get_slave_size(const struct net_device *bond_dev,
  1045. const struct net_device *slave_dev)
  1046. {
  1047. return nla_total_size(sizeof(u32)); /* IFLA_VRF_PORT_TABLE */
  1048. }
  1049. static int vrf_fill_slave_info(struct sk_buff *skb,
  1050. const struct net_device *vrf_dev,
  1051. const struct net_device *slave_dev)
  1052. {
  1053. struct net_vrf *vrf = netdev_priv(vrf_dev);
  1054. if (nla_put_u32(skb, IFLA_VRF_PORT_TABLE, vrf->tb_id))
  1055. return -EMSGSIZE;
  1056. return 0;
  1057. }
  1058. static const struct nla_policy vrf_nl_policy[IFLA_VRF_MAX + 1] = {
  1059. [IFLA_VRF_TABLE] = { .type = NLA_U32 },
  1060. };
  1061. static struct rtnl_link_ops vrf_link_ops __read_mostly = {
  1062. .kind = DRV_NAME,
  1063. .priv_size = sizeof(struct net_vrf),
  1064. .get_size = vrf_nl_getsize,
  1065. .policy = vrf_nl_policy,
  1066. .validate = vrf_validate,
  1067. .fill_info = vrf_fillinfo,
  1068. .get_slave_size = vrf_get_slave_size,
  1069. .fill_slave_info = vrf_fill_slave_info,
  1070. .newlink = vrf_newlink,
  1071. .dellink = vrf_dellink,
  1072. .setup = vrf_setup,
  1073. .maxtype = IFLA_VRF_MAX,
  1074. };
  1075. static int vrf_device_event(struct notifier_block *unused,
  1076. unsigned long event, void *ptr)
  1077. {
  1078. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  1079. /* only care about unregister events to drop slave references */
  1080. if (event == NETDEV_UNREGISTER) {
  1081. struct net_device *vrf_dev;
  1082. if (!netif_is_l3_slave(dev))
  1083. goto out;
  1084. vrf_dev = netdev_master_upper_dev_get(dev);
  1085. vrf_del_slave(vrf_dev, dev);
  1086. }
  1087. out:
  1088. return NOTIFY_DONE;
  1089. }
  1090. static struct notifier_block vrf_notifier_block __read_mostly = {
  1091. .notifier_call = vrf_device_event,
  1092. };
  1093. static int __init vrf_init_module(void)
  1094. {
  1095. int rc;
  1096. register_netdevice_notifier(&vrf_notifier_block);
  1097. rc = rtnl_link_register(&vrf_link_ops);
  1098. if (rc < 0)
  1099. goto error;
  1100. return 0;
  1101. error:
  1102. unregister_netdevice_notifier(&vrf_notifier_block);
  1103. return rc;
  1104. }
  1105. module_init(vrf_init_module);
  1106. MODULE_AUTHOR("Shrijeet Mukherjee, David Ahern");
  1107. MODULE_DESCRIPTION("Device driver to instantiate VRF domains");
  1108. MODULE_LICENSE("GPL");
  1109. MODULE_ALIAS_RTNL_LINK(DRV_NAME);
  1110. MODULE_VERSION(DRV_VERSION);