vrf.c 22 KB

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