vrf.c 35 KB

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