vrf.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992
  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. #define RT_FL_TOS(oldflp4) \
  37. ((oldflp4)->flowi4_tos & (IPTOS_RT_MASK | RTO_ONLINK))
  38. #define DRV_NAME "vrf"
  39. #define DRV_VERSION "1.0"
  40. #define vrf_master_get_rcu(dev) \
  41. ((struct net_device *)rcu_dereference(dev->rx_handler_data))
  42. struct net_vrf {
  43. struct rtable *rth;
  44. struct rt6_info *rt6;
  45. u32 tb_id;
  46. };
  47. struct pcpu_dstats {
  48. u64 tx_pkts;
  49. u64 tx_bytes;
  50. u64 tx_drps;
  51. u64 rx_pkts;
  52. u64 rx_bytes;
  53. struct u64_stats_sync syncp;
  54. };
  55. static struct dst_entry *vrf_ip_check(struct dst_entry *dst, u32 cookie)
  56. {
  57. return dst;
  58. }
  59. static int vrf_ip_local_out(struct net *net, struct sock *sk, struct sk_buff *skb)
  60. {
  61. return ip_local_out(net, sk, skb);
  62. }
  63. static unsigned int vrf_v4_mtu(const struct dst_entry *dst)
  64. {
  65. /* TO-DO: return max ethernet size? */
  66. return dst->dev->mtu;
  67. }
  68. static void vrf_dst_destroy(struct dst_entry *dst)
  69. {
  70. /* our dst lives forever - or until the device is closed */
  71. }
  72. static unsigned int vrf_default_advmss(const struct dst_entry *dst)
  73. {
  74. return 65535 - 40;
  75. }
  76. static struct dst_ops vrf_dst_ops = {
  77. .family = AF_INET,
  78. .local_out = vrf_ip_local_out,
  79. .check = vrf_ip_check,
  80. .mtu = vrf_v4_mtu,
  81. .destroy = vrf_dst_destroy,
  82. .default_advmss = vrf_default_advmss,
  83. };
  84. /* neighbor handling is done with actual device; do not want
  85. * to flip skb->dev for those ndisc packets. This really fails
  86. * for multiple next protocols (e.g., NEXTHDR_HOP). But it is
  87. * a start.
  88. */
  89. #if IS_ENABLED(CONFIG_IPV6)
  90. static bool check_ipv6_frame(const struct sk_buff *skb)
  91. {
  92. const struct ipv6hdr *ipv6h;
  93. struct ipv6hdr _ipv6h;
  94. bool rc = true;
  95. ipv6h = skb_header_pointer(skb, 0, sizeof(_ipv6h), &_ipv6h);
  96. if (!ipv6h)
  97. goto out;
  98. if (ipv6h->nexthdr == NEXTHDR_ICMP) {
  99. const struct icmp6hdr *icmph;
  100. struct icmp6hdr _icmph;
  101. icmph = skb_header_pointer(skb, sizeof(_ipv6h),
  102. sizeof(_icmph), &_icmph);
  103. if (!icmph)
  104. goto out;
  105. switch (icmph->icmp6_type) {
  106. case NDISC_ROUTER_SOLICITATION:
  107. case NDISC_ROUTER_ADVERTISEMENT:
  108. case NDISC_NEIGHBOUR_SOLICITATION:
  109. case NDISC_NEIGHBOUR_ADVERTISEMENT:
  110. case NDISC_REDIRECT:
  111. rc = false;
  112. break;
  113. }
  114. }
  115. out:
  116. return rc;
  117. }
  118. #else
  119. static bool check_ipv6_frame(const struct sk_buff *skb)
  120. {
  121. return false;
  122. }
  123. #endif
  124. static bool is_ip_rx_frame(struct sk_buff *skb)
  125. {
  126. switch (skb->protocol) {
  127. case htons(ETH_P_IP):
  128. return true;
  129. case htons(ETH_P_IPV6):
  130. return check_ipv6_frame(skb);
  131. }
  132. return false;
  133. }
  134. static void vrf_tx_error(struct net_device *vrf_dev, struct sk_buff *skb)
  135. {
  136. vrf_dev->stats.tx_errors++;
  137. kfree_skb(skb);
  138. }
  139. /* note: already called with rcu_read_lock */
  140. static rx_handler_result_t vrf_handle_frame(struct sk_buff **pskb)
  141. {
  142. struct sk_buff *skb = *pskb;
  143. if (is_ip_rx_frame(skb)) {
  144. struct net_device *dev = vrf_master_get_rcu(skb->dev);
  145. struct pcpu_dstats *dstats = this_cpu_ptr(dev->dstats);
  146. u64_stats_update_begin(&dstats->syncp);
  147. dstats->rx_pkts++;
  148. dstats->rx_bytes += skb->len;
  149. u64_stats_update_end(&dstats->syncp);
  150. skb->dev = dev;
  151. return RX_HANDLER_ANOTHER;
  152. }
  153. return RX_HANDLER_PASS;
  154. }
  155. static struct rtnl_link_stats64 *vrf_get_stats64(struct net_device *dev,
  156. struct rtnl_link_stats64 *stats)
  157. {
  158. int i;
  159. for_each_possible_cpu(i) {
  160. const struct pcpu_dstats *dstats;
  161. u64 tbytes, tpkts, tdrops, rbytes, rpkts;
  162. unsigned int start;
  163. dstats = per_cpu_ptr(dev->dstats, i);
  164. do {
  165. start = u64_stats_fetch_begin_irq(&dstats->syncp);
  166. tbytes = dstats->tx_bytes;
  167. tpkts = dstats->tx_pkts;
  168. tdrops = dstats->tx_drps;
  169. rbytes = dstats->rx_bytes;
  170. rpkts = dstats->rx_pkts;
  171. } while (u64_stats_fetch_retry_irq(&dstats->syncp, start));
  172. stats->tx_bytes += tbytes;
  173. stats->tx_packets += tpkts;
  174. stats->tx_dropped += tdrops;
  175. stats->rx_bytes += rbytes;
  176. stats->rx_packets += rpkts;
  177. }
  178. return stats;
  179. }
  180. #if IS_ENABLED(CONFIG_IPV6)
  181. static netdev_tx_t vrf_process_v6_outbound(struct sk_buff *skb,
  182. struct net_device *dev)
  183. {
  184. const struct ipv6hdr *iph = ipv6_hdr(skb);
  185. struct net *net = dev_net(skb->dev);
  186. struct flowi6 fl6 = {
  187. /* needed to match OIF rule */
  188. .flowi6_oif = dev->ifindex,
  189. .flowi6_iif = LOOPBACK_IFINDEX,
  190. .daddr = iph->daddr,
  191. .saddr = iph->saddr,
  192. .flowlabel = ip6_flowinfo(iph),
  193. .flowi6_mark = skb->mark,
  194. .flowi6_proto = iph->nexthdr,
  195. .flowi6_flags = FLOWI_FLAG_L3MDEV_SRC | FLOWI_FLAG_SKIP_NH_OIF,
  196. };
  197. int ret = NET_XMIT_DROP;
  198. struct dst_entry *dst;
  199. struct dst_entry *dst_null = &net->ipv6.ip6_null_entry->dst;
  200. dst = ip6_route_output(net, NULL, &fl6);
  201. if (dst == dst_null)
  202. goto err;
  203. skb_dst_drop(skb);
  204. skb_dst_set(skb, dst);
  205. ret = ip6_local_out(net, skb->sk, skb);
  206. if (unlikely(net_xmit_eval(ret)))
  207. dev->stats.tx_errors++;
  208. else
  209. ret = NET_XMIT_SUCCESS;
  210. return ret;
  211. err:
  212. vrf_tx_error(dev, skb);
  213. return NET_XMIT_DROP;
  214. }
  215. #else
  216. static netdev_tx_t vrf_process_v6_outbound(struct sk_buff *skb,
  217. struct net_device *dev)
  218. {
  219. vrf_tx_error(dev, skb);
  220. return NET_XMIT_DROP;
  221. }
  222. #endif
  223. static int vrf_send_v4_prep(struct sk_buff *skb, struct flowi4 *fl4,
  224. struct net_device *vrf_dev)
  225. {
  226. struct rtable *rt;
  227. int err = 1;
  228. rt = ip_route_output_flow(dev_net(vrf_dev), fl4, NULL);
  229. if (IS_ERR(rt))
  230. goto out;
  231. /* TO-DO: what about broadcast ? */
  232. if (rt->rt_type != RTN_UNICAST && rt->rt_type != RTN_LOCAL) {
  233. ip_rt_put(rt);
  234. goto out;
  235. }
  236. skb_dst_drop(skb);
  237. skb_dst_set(skb, &rt->dst);
  238. err = 0;
  239. out:
  240. return err;
  241. }
  242. static netdev_tx_t vrf_process_v4_outbound(struct sk_buff *skb,
  243. struct net_device *vrf_dev)
  244. {
  245. struct iphdr *ip4h = ip_hdr(skb);
  246. int ret = NET_XMIT_DROP;
  247. struct flowi4 fl4 = {
  248. /* needed to match OIF rule */
  249. .flowi4_oif = vrf_dev->ifindex,
  250. .flowi4_iif = LOOPBACK_IFINDEX,
  251. .flowi4_tos = RT_TOS(ip4h->tos),
  252. .flowi4_flags = FLOWI_FLAG_ANYSRC | FLOWI_FLAG_L3MDEV_SRC |
  253. FLOWI_FLAG_SKIP_NH_OIF,
  254. .daddr = ip4h->daddr,
  255. };
  256. if (vrf_send_v4_prep(skb, &fl4, vrf_dev))
  257. goto err;
  258. if (!ip4h->saddr) {
  259. ip4h->saddr = inet_select_addr(skb_dst(skb)->dev, 0,
  260. RT_SCOPE_LINK);
  261. }
  262. ret = ip_local_out(dev_net(skb_dst(skb)->dev), skb->sk, skb);
  263. if (unlikely(net_xmit_eval(ret)))
  264. vrf_dev->stats.tx_errors++;
  265. else
  266. ret = NET_XMIT_SUCCESS;
  267. out:
  268. return ret;
  269. err:
  270. vrf_tx_error(vrf_dev, skb);
  271. goto out;
  272. }
  273. static netdev_tx_t is_ip_tx_frame(struct sk_buff *skb, struct net_device *dev)
  274. {
  275. /* strip the ethernet header added for pass through VRF device */
  276. __skb_pull(skb, skb_network_offset(skb));
  277. switch (skb->protocol) {
  278. case htons(ETH_P_IP):
  279. return vrf_process_v4_outbound(skb, dev);
  280. case htons(ETH_P_IPV6):
  281. return vrf_process_v6_outbound(skb, dev);
  282. default:
  283. vrf_tx_error(dev, skb);
  284. return NET_XMIT_DROP;
  285. }
  286. }
  287. static netdev_tx_t vrf_xmit(struct sk_buff *skb, struct net_device *dev)
  288. {
  289. netdev_tx_t ret = is_ip_tx_frame(skb, dev);
  290. if (likely(ret == NET_XMIT_SUCCESS || ret == NET_XMIT_CN)) {
  291. struct pcpu_dstats *dstats = this_cpu_ptr(dev->dstats);
  292. u64_stats_update_begin(&dstats->syncp);
  293. dstats->tx_pkts++;
  294. dstats->tx_bytes += skb->len;
  295. u64_stats_update_end(&dstats->syncp);
  296. } else {
  297. this_cpu_inc(dev->dstats->tx_drps);
  298. }
  299. return ret;
  300. }
  301. #if IS_ENABLED(CONFIG_IPV6)
  302. static struct dst_entry *vrf_ip6_check(struct dst_entry *dst, u32 cookie)
  303. {
  304. return dst;
  305. }
  306. static struct dst_ops vrf_dst_ops6 = {
  307. .family = AF_INET6,
  308. .local_out = ip6_local_out,
  309. .check = vrf_ip6_check,
  310. .mtu = vrf_v4_mtu,
  311. .destroy = vrf_dst_destroy,
  312. .default_advmss = vrf_default_advmss,
  313. };
  314. static int init_dst_ops6_kmem_cachep(void)
  315. {
  316. vrf_dst_ops6.kmem_cachep = kmem_cache_create("vrf_ip6_dst_cache",
  317. sizeof(struct rt6_info),
  318. 0,
  319. SLAB_HWCACHE_ALIGN,
  320. NULL);
  321. if (!vrf_dst_ops6.kmem_cachep)
  322. return -ENOMEM;
  323. return 0;
  324. }
  325. static void free_dst_ops6_kmem_cachep(void)
  326. {
  327. kmem_cache_destroy(vrf_dst_ops6.kmem_cachep);
  328. }
  329. static int vrf_input6(struct sk_buff *skb)
  330. {
  331. skb->dev->stats.rx_errors++;
  332. kfree_skb(skb);
  333. return 0;
  334. }
  335. /* modelled after ip6_finish_output2 */
  336. static int vrf_finish_output6(struct net *net, struct sock *sk,
  337. struct sk_buff *skb)
  338. {
  339. struct dst_entry *dst = skb_dst(skb);
  340. struct net_device *dev = dst->dev;
  341. struct neighbour *neigh;
  342. struct in6_addr *nexthop;
  343. int ret;
  344. skb->protocol = htons(ETH_P_IPV6);
  345. skb->dev = dev;
  346. rcu_read_lock_bh();
  347. nexthop = rt6_nexthop((struct rt6_info *)dst, &ipv6_hdr(skb)->daddr);
  348. neigh = __ipv6_neigh_lookup_noref(dst->dev, nexthop);
  349. if (unlikely(!neigh))
  350. neigh = __neigh_create(&nd_tbl, nexthop, dst->dev, false);
  351. if (!IS_ERR(neigh)) {
  352. ret = dst_neigh_output(dst, neigh, skb);
  353. rcu_read_unlock_bh();
  354. return ret;
  355. }
  356. rcu_read_unlock_bh();
  357. IP6_INC_STATS(dev_net(dst->dev),
  358. ip6_dst_idev(dst), IPSTATS_MIB_OUTNOROUTES);
  359. kfree_skb(skb);
  360. return -EINVAL;
  361. }
  362. /* modelled after ip6_output */
  363. static int vrf_output6(struct net *net, struct sock *sk, struct sk_buff *skb)
  364. {
  365. return NF_HOOK_COND(NFPROTO_IPV6, NF_INET_POST_ROUTING,
  366. net, sk, skb, NULL, skb_dst(skb)->dev,
  367. vrf_finish_output6,
  368. !(IP6CB(skb)->flags & IP6SKB_REROUTED));
  369. }
  370. static void vrf_rt6_destroy(struct net_vrf *vrf)
  371. {
  372. dst_destroy(&vrf->rt6->dst);
  373. free_percpu(vrf->rt6->rt6i_pcpu);
  374. vrf->rt6 = NULL;
  375. }
  376. static int vrf_rt6_create(struct net_device *dev)
  377. {
  378. struct net_vrf *vrf = netdev_priv(dev);
  379. struct dst_entry *dst;
  380. struct rt6_info *rt6;
  381. int cpu;
  382. int rc = -ENOMEM;
  383. rt6 = dst_alloc(&vrf_dst_ops6, dev, 0,
  384. DST_OBSOLETE_NONE,
  385. (DST_HOST | DST_NOPOLICY | DST_NOXFRM));
  386. if (!rt6)
  387. goto out;
  388. dst = &rt6->dst;
  389. rt6->rt6i_pcpu = alloc_percpu_gfp(struct rt6_info *, GFP_KERNEL);
  390. if (!rt6->rt6i_pcpu) {
  391. dst_destroy(dst);
  392. goto out;
  393. }
  394. for_each_possible_cpu(cpu) {
  395. struct rt6_info **p = per_cpu_ptr(rt6->rt6i_pcpu, cpu);
  396. *p = NULL;
  397. }
  398. memset(dst + 1, 0, sizeof(*rt6) - sizeof(*dst));
  399. INIT_LIST_HEAD(&rt6->rt6i_siblings);
  400. INIT_LIST_HEAD(&rt6->rt6i_uncached);
  401. rt6->dst.input = vrf_input6;
  402. rt6->dst.output = vrf_output6;
  403. rt6->rt6i_table = fib6_get_table(dev_net(dev), vrf->tb_id);
  404. atomic_set(&rt6->dst.__refcnt, 2);
  405. vrf->rt6 = rt6;
  406. rc = 0;
  407. out:
  408. return rc;
  409. }
  410. #else
  411. static int init_dst_ops6_kmem_cachep(void)
  412. {
  413. return 0;
  414. }
  415. static void free_dst_ops6_kmem_cachep(void)
  416. {
  417. }
  418. static void vrf_rt6_destroy(struct net_vrf *vrf)
  419. {
  420. }
  421. static int vrf_rt6_create(struct net_device *dev)
  422. {
  423. return 0;
  424. }
  425. #endif
  426. /* modelled after ip_finish_output2 */
  427. static int vrf_finish_output(struct net *net, struct sock *sk, struct sk_buff *skb)
  428. {
  429. struct dst_entry *dst = skb_dst(skb);
  430. struct rtable *rt = (struct rtable *)dst;
  431. struct net_device *dev = dst->dev;
  432. unsigned int hh_len = LL_RESERVED_SPACE(dev);
  433. struct neighbour *neigh;
  434. u32 nexthop;
  435. int ret = -EINVAL;
  436. /* Be paranoid, rather than too clever. */
  437. if (unlikely(skb_headroom(skb) < hh_len && dev->header_ops)) {
  438. struct sk_buff *skb2;
  439. skb2 = skb_realloc_headroom(skb, LL_RESERVED_SPACE(dev));
  440. if (!skb2) {
  441. ret = -ENOMEM;
  442. goto err;
  443. }
  444. if (skb->sk)
  445. skb_set_owner_w(skb2, skb->sk);
  446. consume_skb(skb);
  447. skb = skb2;
  448. }
  449. rcu_read_lock_bh();
  450. nexthop = (__force u32)rt_nexthop(rt, ip_hdr(skb)->daddr);
  451. neigh = __ipv4_neigh_lookup_noref(dev, nexthop);
  452. if (unlikely(!neigh))
  453. neigh = __neigh_create(&arp_tbl, &nexthop, dev, false);
  454. if (!IS_ERR(neigh))
  455. ret = dst_neigh_output(dst, neigh, skb);
  456. rcu_read_unlock_bh();
  457. err:
  458. if (unlikely(ret < 0))
  459. vrf_tx_error(skb->dev, skb);
  460. return ret;
  461. }
  462. static int vrf_output(struct net *net, struct sock *sk, struct sk_buff *skb)
  463. {
  464. struct net_device *dev = skb_dst(skb)->dev;
  465. IP_UPD_PO_STATS(net, IPSTATS_MIB_OUT, skb->len);
  466. skb->dev = dev;
  467. skb->protocol = htons(ETH_P_IP);
  468. return NF_HOOK_COND(NFPROTO_IPV4, NF_INET_POST_ROUTING,
  469. net, sk, skb, NULL, dev,
  470. vrf_finish_output,
  471. !(IPCB(skb)->flags & IPSKB_REROUTED));
  472. }
  473. static void vrf_rtable_destroy(struct net_vrf *vrf)
  474. {
  475. struct dst_entry *dst = (struct dst_entry *)vrf->rth;
  476. dst_destroy(dst);
  477. vrf->rth = NULL;
  478. }
  479. static struct rtable *vrf_rtable_create(struct net_device *dev)
  480. {
  481. struct net_vrf *vrf = netdev_priv(dev);
  482. struct rtable *rth;
  483. rth = dst_alloc(&vrf_dst_ops, dev, 2,
  484. DST_OBSOLETE_NONE,
  485. (DST_HOST | DST_NOPOLICY | DST_NOXFRM));
  486. if (rth) {
  487. rth->dst.output = vrf_output;
  488. rth->rt_genid = rt_genid_ipv4(dev_net(dev));
  489. rth->rt_flags = 0;
  490. rth->rt_type = RTN_UNICAST;
  491. rth->rt_is_input = 0;
  492. rth->rt_iif = 0;
  493. rth->rt_pmtu = 0;
  494. rth->rt_gateway = 0;
  495. rth->rt_uses_gateway = 0;
  496. rth->rt_table_id = vrf->tb_id;
  497. INIT_LIST_HEAD(&rth->rt_uncached);
  498. rth->rt_uncached_list = NULL;
  499. }
  500. return rth;
  501. }
  502. /**************************** device handling ********************/
  503. /* cycle interface to flush neighbor cache and move routes across tables */
  504. static void cycle_netdev(struct net_device *dev)
  505. {
  506. unsigned int flags = dev->flags;
  507. int ret;
  508. if (!netif_running(dev))
  509. return;
  510. ret = dev_change_flags(dev, flags & ~IFF_UP);
  511. if (ret >= 0)
  512. ret = dev_change_flags(dev, flags);
  513. if (ret < 0) {
  514. netdev_err(dev,
  515. "Failed to cycle device %s; route tables might be wrong!\n",
  516. dev->name);
  517. }
  518. }
  519. static int do_vrf_add_slave(struct net_device *dev, struct net_device *port_dev)
  520. {
  521. int ret;
  522. /* register the packet handler for slave ports */
  523. ret = netdev_rx_handler_register(port_dev, vrf_handle_frame, dev);
  524. if (ret) {
  525. netdev_err(port_dev,
  526. "Device %s failed to register rx_handler\n",
  527. port_dev->name);
  528. goto out_fail;
  529. }
  530. ret = netdev_master_upper_dev_link(port_dev, dev, NULL, NULL);
  531. if (ret < 0)
  532. goto out_unregister;
  533. port_dev->priv_flags |= IFF_L3MDEV_SLAVE;
  534. cycle_netdev(port_dev);
  535. return 0;
  536. out_unregister:
  537. netdev_rx_handler_unregister(port_dev);
  538. out_fail:
  539. return ret;
  540. }
  541. static int vrf_add_slave(struct net_device *dev, struct net_device *port_dev)
  542. {
  543. if (netif_is_l3_master(port_dev) || netif_is_l3_slave(port_dev))
  544. return -EINVAL;
  545. return do_vrf_add_slave(dev, port_dev);
  546. }
  547. /* inverse of do_vrf_add_slave */
  548. static int do_vrf_del_slave(struct net_device *dev, struct net_device *port_dev)
  549. {
  550. netdev_upper_dev_unlink(port_dev, dev);
  551. port_dev->priv_flags &= ~IFF_L3MDEV_SLAVE;
  552. netdev_rx_handler_unregister(port_dev);
  553. cycle_netdev(port_dev);
  554. return 0;
  555. }
  556. static int vrf_del_slave(struct net_device *dev, struct net_device *port_dev)
  557. {
  558. return do_vrf_del_slave(dev, port_dev);
  559. }
  560. static void vrf_dev_uninit(struct net_device *dev)
  561. {
  562. struct net_vrf *vrf = netdev_priv(dev);
  563. struct net_device *port_dev;
  564. struct list_head *iter;
  565. vrf_rtable_destroy(vrf);
  566. vrf_rt6_destroy(vrf);
  567. netdev_for_each_lower_dev(dev, port_dev, iter)
  568. vrf_del_slave(dev, port_dev);
  569. free_percpu(dev->dstats);
  570. dev->dstats = NULL;
  571. }
  572. static int vrf_dev_init(struct net_device *dev)
  573. {
  574. struct net_vrf *vrf = netdev_priv(dev);
  575. dev->dstats = netdev_alloc_pcpu_stats(struct pcpu_dstats);
  576. if (!dev->dstats)
  577. goto out_nomem;
  578. /* create the default dst which points back to us */
  579. vrf->rth = vrf_rtable_create(dev);
  580. if (!vrf->rth)
  581. goto out_stats;
  582. if (vrf_rt6_create(dev) != 0)
  583. goto out_rth;
  584. dev->flags = IFF_MASTER | IFF_NOARP;
  585. return 0;
  586. out_rth:
  587. vrf_rtable_destroy(vrf);
  588. out_stats:
  589. free_percpu(dev->dstats);
  590. dev->dstats = NULL;
  591. out_nomem:
  592. return -ENOMEM;
  593. }
  594. static const struct net_device_ops vrf_netdev_ops = {
  595. .ndo_init = vrf_dev_init,
  596. .ndo_uninit = vrf_dev_uninit,
  597. .ndo_start_xmit = vrf_xmit,
  598. .ndo_get_stats64 = vrf_get_stats64,
  599. .ndo_add_slave = vrf_add_slave,
  600. .ndo_del_slave = vrf_del_slave,
  601. };
  602. static u32 vrf_fib_table(const struct net_device *dev)
  603. {
  604. struct net_vrf *vrf = netdev_priv(dev);
  605. return vrf->tb_id;
  606. }
  607. static struct rtable *vrf_get_rtable(const struct net_device *dev,
  608. const struct flowi4 *fl4)
  609. {
  610. struct rtable *rth = NULL;
  611. if (!(fl4->flowi4_flags & FLOWI_FLAG_L3MDEV_SRC)) {
  612. struct net_vrf *vrf = netdev_priv(dev);
  613. rth = vrf->rth;
  614. atomic_inc(&rth->dst.__refcnt);
  615. }
  616. return rth;
  617. }
  618. /* called under rcu_read_lock */
  619. static int vrf_get_saddr(struct net_device *dev, struct flowi4 *fl4)
  620. {
  621. struct fib_result res = { .tclassid = 0 };
  622. struct net *net = dev_net(dev);
  623. u32 orig_tos = fl4->flowi4_tos;
  624. u8 flags = fl4->flowi4_flags;
  625. u8 scope = fl4->flowi4_scope;
  626. u8 tos = RT_FL_TOS(fl4);
  627. int rc;
  628. if (unlikely(!fl4->daddr))
  629. return 0;
  630. fl4->flowi4_flags |= FLOWI_FLAG_SKIP_NH_OIF;
  631. fl4->flowi4_iif = LOOPBACK_IFINDEX;
  632. fl4->flowi4_tos = tos & IPTOS_RT_MASK;
  633. fl4->flowi4_scope = ((tos & RTO_ONLINK) ?
  634. RT_SCOPE_LINK : RT_SCOPE_UNIVERSE);
  635. rc = fib_lookup(net, fl4, &res, 0);
  636. if (!rc) {
  637. if (res.type == RTN_LOCAL)
  638. fl4->saddr = res.fi->fib_prefsrc ? : fl4->daddr;
  639. else
  640. fib_select_path(net, &res, fl4, -1);
  641. }
  642. fl4->flowi4_flags = flags;
  643. fl4->flowi4_tos = orig_tos;
  644. fl4->flowi4_scope = scope;
  645. return rc;
  646. }
  647. #if IS_ENABLED(CONFIG_IPV6)
  648. static struct dst_entry *vrf_get_rt6_dst(const struct net_device *dev,
  649. const struct flowi6 *fl6)
  650. {
  651. struct rt6_info *rt = NULL;
  652. if (!(fl6->flowi6_flags & FLOWI_FLAG_L3MDEV_SRC)) {
  653. struct net_vrf *vrf = netdev_priv(dev);
  654. rt = vrf->rt6;
  655. atomic_inc(&rt->dst.__refcnt);
  656. }
  657. return (struct dst_entry *)rt;
  658. }
  659. #endif
  660. static const struct l3mdev_ops vrf_l3mdev_ops = {
  661. .l3mdev_fib_table = vrf_fib_table,
  662. .l3mdev_get_rtable = vrf_get_rtable,
  663. .l3mdev_get_saddr = vrf_get_saddr,
  664. #if IS_ENABLED(CONFIG_IPV6)
  665. .l3mdev_get_rt6_dst = vrf_get_rt6_dst,
  666. #endif
  667. };
  668. static void vrf_get_drvinfo(struct net_device *dev,
  669. struct ethtool_drvinfo *info)
  670. {
  671. strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
  672. strlcpy(info->version, DRV_VERSION, sizeof(info->version));
  673. }
  674. static const struct ethtool_ops vrf_ethtool_ops = {
  675. .get_drvinfo = vrf_get_drvinfo,
  676. };
  677. static void vrf_setup(struct net_device *dev)
  678. {
  679. ether_setup(dev);
  680. /* Initialize the device structure. */
  681. dev->netdev_ops = &vrf_netdev_ops;
  682. dev->l3mdev_ops = &vrf_l3mdev_ops;
  683. dev->ethtool_ops = &vrf_ethtool_ops;
  684. dev->destructor = free_netdev;
  685. /* Fill in device structure with ethernet-generic values. */
  686. eth_hw_addr_random(dev);
  687. /* don't acquire vrf device's netif_tx_lock when transmitting */
  688. dev->features |= NETIF_F_LLTX;
  689. /* don't allow vrf devices to change network namespaces. */
  690. dev->features |= NETIF_F_NETNS_LOCAL;
  691. }
  692. static int vrf_validate(struct nlattr *tb[], struct nlattr *data[])
  693. {
  694. if (tb[IFLA_ADDRESS]) {
  695. if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
  696. return -EINVAL;
  697. if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
  698. return -EADDRNOTAVAIL;
  699. }
  700. return 0;
  701. }
  702. static void vrf_dellink(struct net_device *dev, struct list_head *head)
  703. {
  704. unregister_netdevice_queue(dev, head);
  705. }
  706. static int vrf_newlink(struct net *src_net, struct net_device *dev,
  707. struct nlattr *tb[], struct nlattr *data[])
  708. {
  709. struct net_vrf *vrf = netdev_priv(dev);
  710. if (!data || !data[IFLA_VRF_TABLE])
  711. return -EINVAL;
  712. vrf->tb_id = nla_get_u32(data[IFLA_VRF_TABLE]);
  713. dev->priv_flags |= IFF_L3MDEV_MASTER;
  714. return register_netdevice(dev);
  715. }
  716. static size_t vrf_nl_getsize(const struct net_device *dev)
  717. {
  718. return nla_total_size(sizeof(u32)); /* IFLA_VRF_TABLE */
  719. }
  720. static int vrf_fillinfo(struct sk_buff *skb,
  721. const struct net_device *dev)
  722. {
  723. struct net_vrf *vrf = netdev_priv(dev);
  724. return nla_put_u32(skb, IFLA_VRF_TABLE, vrf->tb_id);
  725. }
  726. static size_t vrf_get_slave_size(const struct net_device *bond_dev,
  727. const struct net_device *slave_dev)
  728. {
  729. return nla_total_size(sizeof(u32)); /* IFLA_VRF_PORT_TABLE */
  730. }
  731. static int vrf_fill_slave_info(struct sk_buff *skb,
  732. const struct net_device *vrf_dev,
  733. const struct net_device *slave_dev)
  734. {
  735. struct net_vrf *vrf = netdev_priv(vrf_dev);
  736. if (nla_put_u32(skb, IFLA_VRF_PORT_TABLE, vrf->tb_id))
  737. return -EMSGSIZE;
  738. return 0;
  739. }
  740. static const struct nla_policy vrf_nl_policy[IFLA_VRF_MAX + 1] = {
  741. [IFLA_VRF_TABLE] = { .type = NLA_U32 },
  742. };
  743. static struct rtnl_link_ops vrf_link_ops __read_mostly = {
  744. .kind = DRV_NAME,
  745. .priv_size = sizeof(struct net_vrf),
  746. .get_size = vrf_nl_getsize,
  747. .policy = vrf_nl_policy,
  748. .validate = vrf_validate,
  749. .fill_info = vrf_fillinfo,
  750. .get_slave_size = vrf_get_slave_size,
  751. .fill_slave_info = vrf_fill_slave_info,
  752. .newlink = vrf_newlink,
  753. .dellink = vrf_dellink,
  754. .setup = vrf_setup,
  755. .maxtype = IFLA_VRF_MAX,
  756. };
  757. static int vrf_device_event(struct notifier_block *unused,
  758. unsigned long event, void *ptr)
  759. {
  760. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  761. /* only care about unregister events to drop slave references */
  762. if (event == NETDEV_UNREGISTER) {
  763. struct net_device *vrf_dev;
  764. if (!netif_is_l3_slave(dev))
  765. goto out;
  766. vrf_dev = netdev_master_upper_dev_get(dev);
  767. vrf_del_slave(vrf_dev, dev);
  768. }
  769. out:
  770. return NOTIFY_DONE;
  771. }
  772. static struct notifier_block vrf_notifier_block __read_mostly = {
  773. .notifier_call = vrf_device_event,
  774. };
  775. static int __init vrf_init_module(void)
  776. {
  777. int rc;
  778. vrf_dst_ops.kmem_cachep =
  779. kmem_cache_create("vrf_ip_dst_cache",
  780. sizeof(struct rtable), 0,
  781. SLAB_HWCACHE_ALIGN,
  782. NULL);
  783. if (!vrf_dst_ops.kmem_cachep)
  784. return -ENOMEM;
  785. rc = init_dst_ops6_kmem_cachep();
  786. if (rc != 0)
  787. goto error2;
  788. register_netdevice_notifier(&vrf_notifier_block);
  789. rc = rtnl_link_register(&vrf_link_ops);
  790. if (rc < 0)
  791. goto error;
  792. return 0;
  793. error:
  794. unregister_netdevice_notifier(&vrf_notifier_block);
  795. free_dst_ops6_kmem_cachep();
  796. error2:
  797. kmem_cache_destroy(vrf_dst_ops.kmem_cachep);
  798. return rc;
  799. }
  800. static void __exit vrf_cleanup_module(void)
  801. {
  802. rtnl_link_unregister(&vrf_link_ops);
  803. unregister_netdevice_notifier(&vrf_notifier_block);
  804. kmem_cache_destroy(vrf_dst_ops.kmem_cachep);
  805. free_dst_ops6_kmem_cachep();
  806. }
  807. module_init(vrf_init_module);
  808. module_exit(vrf_cleanup_module);
  809. MODULE_AUTHOR("Shrijeet Mukherjee, David Ahern");
  810. MODULE_DESCRIPTION("Device driver to instantiate VRF domains");
  811. MODULE_LICENSE("GPL");
  812. MODULE_ALIAS_RTNL_LINK(DRV_NAME);
  813. MODULE_VERSION(DRV_VERSION);