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 12 #include <linux/ethtool.h> 13 #include <linux/module.h> 14 #include <linux/kernel.h> 15 #include <linux/netdevice.h> 16 #include <linux/etherdevice.h> 17 #include <linux/ip.h> 18 #include <linux/init.h> 19 #include <linux/moduleparam.h> 20 #include <linux/netfilter.h> 21 #include <linux/rtnetlink.h> 22 #include <net/rtnetlink.h> 23 #include <linux/u64_stats_sync.h> 24 #include <linux/hashtable.h> 25 #include <linux/spinlock_types.h> 26 27 #include <linux/inetdevice.h> 28 #include <net/arp.h> 29 #include <net/ip.h> 30 #include <net/ip_fib.h> 31 #include <net/ip6_fib.h> 32 #include <net/ip6_route.h> 33 #include <net/route.h> 34 #include <net/addrconf.h> 35 #include <net/l3mdev.h> 36 #include <net/fib_rules.h> 37 #include <net/sch_generic.h> 38 #include <net/netns/generic.h> 39 #include <net/netfilter/nf_conntrack.h> 40 #include <net/inet_dscp.h> 41 42 #define DRV_NAME "vrf" 43 #define DRV_VERSION "1.1" 44 45 #define FIB_RULE_PREF 1000 /* default preference for FIB rules */ 46 47 #define HT_MAP_BITS 4 48 #define HASH_INITVAL ((u32)0xcafef00d) 49 50 struct vrf_map { 51 DECLARE_HASHTABLE(ht, HT_MAP_BITS); 52 spinlock_t vmap_lock; 53 54 /* shared_tables: 55 * count how many distinct tables do not comply with the strict mode 56 * requirement. 57 * shared_tables value must be 0 in order to enable the strict mode. 58 * 59 * example of the evolution of shared_tables: 60 * | time 61 * add vrf0 --> table 100 shared_tables = 0 | t0 62 * add vrf1 --> table 101 shared_tables = 0 | t1 63 * add vrf2 --> table 100 shared_tables = 1 | t2 64 * add vrf3 --> table 100 shared_tables = 1 | t3 65 * add vrf4 --> table 101 shared_tables = 2 v t4 66 * 67 * shared_tables is a "step function" (or "staircase function") 68 * and it is increased by one when the second vrf is associated to a 69 * table. 70 * 71 * at t2, vrf0 and vrf2 are bound to table 100: shared_tables = 1. 72 * 73 * at t3, another dev (vrf3) is bound to the same table 100 but the 74 * value of shared_tables is still 1. 75 * This means that no matter how many new vrfs will register on the 76 * table 100, the shared_tables will not increase (considering only 77 * table 100). 78 * 79 * at t4, vrf4 is bound to table 101, and shared_tables = 2. 80 * 81 * Looking at the value of shared_tables we can immediately know if 82 * the strict_mode can or cannot be enforced. Indeed, strict_mode 83 * can be enforced iff shared_tables = 0. 84 * 85 * Conversely, shared_tables is decreased when a vrf is de-associated 86 * from a table with exactly two associated vrfs. 87 */ 88 u32 shared_tables; 89 90 bool strict_mode; 91 }; 92 93 struct vrf_map_elem { 94 struct hlist_node hnode; 95 struct list_head vrf_list; /* VRFs registered to this table */ 96 97 u32 table_id; 98 int users; 99 int ifindex; 100 }; 101 102 static unsigned int vrf_net_id; 103 104 /* per netns vrf data */ 105 struct netns_vrf { 106 /* protected by rtnl lock */ 107 bool add_fib_rules; 108 109 struct vrf_map vmap; 110 struct ctl_table_header *ctl_hdr; 111 }; 112 113 struct net_vrf { 114 struct rtable __rcu *rth; 115 struct rt6_info __rcu *rt6; 116 #if IS_ENABLED(CONFIG_IPV6) 117 struct fib6_table *fib6_table; 118 #endif 119 u32 tb_id; 120 121 struct list_head me_list; /* entry in vrf_map_elem */ 122 int ifindex; 123 }; 124 125 static void vrf_tx_error(struct net_device *vrf_dev, struct sk_buff *skb) 126 { 127 vrf_dev->stats.tx_errors++; 128 kfree_skb(skb); 129 } 130 131 static struct vrf_map *netns_vrf_map(struct net *net) 132 { 133 struct netns_vrf *nn_vrf = net_generic(net, vrf_net_id); 134 135 return &nn_vrf->vmap; 136 } 137 138 static struct vrf_map *netns_vrf_map_by_dev(struct net_device *dev) 139 { 140 return netns_vrf_map(dev_net(dev)); 141 } 142 143 static int vrf_map_elem_get_vrf_ifindex(struct vrf_map_elem *me) 144 { 145 struct list_head *me_head = &me->vrf_list; 146 struct net_vrf *vrf; 147 148 if (list_empty(me_head)) 149 return -ENODEV; 150 151 vrf = list_first_entry(me_head, struct net_vrf, me_list); 152 153 return vrf->ifindex; 154 } 155 156 static struct vrf_map_elem *vrf_map_elem_alloc(gfp_t flags) 157 { 158 struct vrf_map_elem *me; 159 160 me = kmalloc(sizeof(*me), flags); 161 if (!me) 162 return NULL; 163 164 return me; 165 } 166 167 static void vrf_map_elem_free(struct vrf_map_elem *me) 168 { 169 kfree(me); 170 } 171 172 static void vrf_map_elem_init(struct vrf_map_elem *me, int table_id, 173 int ifindex, int users) 174 { 175 me->table_id = table_id; 176 me->ifindex = ifindex; 177 me->users = users; 178 INIT_LIST_HEAD(&me->vrf_list); 179 } 180 181 static struct vrf_map_elem *vrf_map_lookup_elem(struct vrf_map *vmap, 182 u32 table_id) 183 { 184 struct vrf_map_elem *me; 185 u32 key; 186 187 key = jhash_1word(table_id, HASH_INITVAL); 188 hash_for_each_possible(vmap->ht, me, hnode, key) { 189 if (me->table_id == table_id) 190 return me; 191 } 192 193 return NULL; 194 } 195 196 static void vrf_map_add_elem(struct vrf_map *vmap, struct vrf_map_elem *me) 197 { 198 u32 table_id = me->table_id; 199 u32 key; 200 201 key = jhash_1word(table_id, HASH_INITVAL); 202 hash_add(vmap->ht, &me->hnode, key); 203 } 204 205 static void vrf_map_del_elem(struct vrf_map_elem *me) 206 { 207 hash_del(&me->hnode); 208 } 209 210 static void vrf_map_lock(struct vrf_map *vmap) __acquires(&vmap->vmap_lock) 211 { 212 spin_lock(&vmap->vmap_lock); 213 } 214 215 static void vrf_map_unlock(struct vrf_map *vmap) __releases(&vmap->vmap_lock) 216 { 217 spin_unlock(&vmap->vmap_lock); 218 } 219 220 /* called with rtnl lock held */ 221 static int 222 vrf_map_register_dev(struct net_device *dev, struct netlink_ext_ack *extack) 223 { 224 struct vrf_map *vmap = netns_vrf_map_by_dev(dev); 225 struct net_vrf *vrf = netdev_priv(dev); 226 struct vrf_map_elem *new_me, *me; 227 u32 table_id = vrf->tb_id; 228 bool free_new_me = false; 229 int users; 230 int res; 231 232 /* we pre-allocate elements used in the spin-locked section (so that we 233 * keep the spinlock as short as possible). 234 */ 235 new_me = vrf_map_elem_alloc(GFP_KERNEL); 236 if (!new_me) 237 return -ENOMEM; 238 239 vrf_map_elem_init(new_me, table_id, dev->ifindex, 0); 240 241 vrf_map_lock(vmap); 242 243 me = vrf_map_lookup_elem(vmap, table_id); 244 if (!me) { 245 me = new_me; 246 vrf_map_add_elem(vmap, me); 247 goto link_vrf; 248 } 249 250 /* we already have an entry in the vrf_map, so it means there is (at 251 * least) a vrf registered on the specific table. 252 */ 253 free_new_me = true; 254 if (vmap->strict_mode) { 255 /* vrfs cannot share the same table */ 256 NL_SET_ERR_MSG(extack, "Table is used by another VRF"); 257 res = -EBUSY; 258 goto unlock; 259 } 260 261 link_vrf: 262 users = ++me->users; 263 if (users == 2) 264 ++vmap->shared_tables; 265 266 list_add(&vrf->me_list, &me->vrf_list); 267 268 res = 0; 269 270 unlock: 271 vrf_map_unlock(vmap); 272 273 /* clean-up, if needed */ 274 if (free_new_me) 275 vrf_map_elem_free(new_me); 276 277 return res; 278 } 279 280 /* called with rtnl lock held */ 281 static void vrf_map_unregister_dev(struct net_device *dev) 282 { 283 struct vrf_map *vmap = netns_vrf_map_by_dev(dev); 284 struct net_vrf *vrf = netdev_priv(dev); 285 u32 table_id = vrf->tb_id; 286 struct vrf_map_elem *me; 287 int users; 288 289 vrf_map_lock(vmap); 290 291 me = vrf_map_lookup_elem(vmap, table_id); 292 if (!me) 293 goto unlock; 294 295 list_del(&vrf->me_list); 296 297 users = --me->users; 298 if (users == 1) { 299 --vmap->shared_tables; 300 } else if (users == 0) { 301 vrf_map_del_elem(me); 302 303 /* no one will refer to this element anymore */ 304 vrf_map_elem_free(me); 305 } 306 307 unlock: 308 vrf_map_unlock(vmap); 309 } 310 311 /* return the vrf device index associated with the table_id */ 312 static int vrf_ifindex_lookup_by_table_id(struct net *net, u32 table_id) 313 { 314 struct vrf_map *vmap = netns_vrf_map(net); 315 struct vrf_map_elem *me; 316 int ifindex; 317 318 vrf_map_lock(vmap); 319 320 if (!vmap->strict_mode) { 321 ifindex = -EPERM; 322 goto unlock; 323 } 324 325 me = vrf_map_lookup_elem(vmap, table_id); 326 if (!me) { 327 ifindex = -ENODEV; 328 goto unlock; 329 } 330 331 ifindex = vrf_map_elem_get_vrf_ifindex(me); 332 333 unlock: 334 vrf_map_unlock(vmap); 335 336 return ifindex; 337 } 338 339 /* by default VRF devices do not have a qdisc and are expected 340 * to be created with only a single queue. 341 */ 342 static bool qdisc_tx_is_default(const struct net_device *dev) 343 { 344 struct netdev_queue *txq; 345 struct Qdisc *qdisc; 346 347 if (dev->num_tx_queues > 1) 348 return false; 349 350 txq = netdev_get_tx_queue(dev, 0); 351 qdisc = rcu_access_pointer(txq->qdisc); 352 353 return !qdisc->enqueue; 354 } 355 356 /* Local traffic destined to local address. Reinsert the packet to rx 357 * path, similar to loopback handling. 358 */ 359 static int vrf_local_xmit(struct sk_buff *skb, struct net_device *dev, 360 struct dst_entry *dst) 361 { 362 unsigned int len = skb->len; 363 364 skb_orphan(skb); 365 366 skb_dst_set(skb, dst); 367 368 /* set pkt_type to avoid skb hitting packet taps twice - 369 * once on Tx and again in Rx processing 370 */ 371 skb->pkt_type = PACKET_LOOPBACK; 372 373 skb->protocol = eth_type_trans(skb, dev); 374 375 if (likely(__netif_rx(skb) == NET_RX_SUCCESS)) 376 dev_dstats_rx_add(dev, len); 377 else 378 dev_dstats_rx_dropped(dev); 379 380 return NETDEV_TX_OK; 381 } 382 383 static void vrf_nf_set_untracked(struct sk_buff *skb) 384 { 385 if (skb_get_nfct(skb) == 0) 386 nf_ct_set(skb, NULL, IP_CT_UNTRACKED); 387 } 388 389 static void vrf_nf_reset_ct(struct sk_buff *skb) 390 { 391 if (skb_get_nfct(skb) == IP_CT_UNTRACKED) 392 nf_reset_ct(skb); 393 } 394 395 #if IS_ENABLED(CONFIG_IPV6) 396 static int vrf_ip6_local_out(struct net *net, struct sock *sk, 397 struct sk_buff *skb) 398 { 399 int err; 400 401 vrf_nf_reset_ct(skb); 402 403 err = nf_hook(NFPROTO_IPV6, NF_INET_LOCAL_OUT, net, 404 sk, skb, NULL, skb_dst(skb)->dev, dst_output); 405 406 if (likely(err == 1)) 407 err = dst_output(net, sk, skb); 408 409 return err; 410 } 411 412 static netdev_tx_t vrf_process_v6_outbound(struct sk_buff *skb, 413 struct net_device *dev) 414 { 415 const struct ipv6hdr *iph; 416 struct net *net = dev_net(skb->dev); 417 struct flowi6 fl6; 418 int ret = NET_XMIT_DROP; 419 struct dst_entry *dst; 420 struct dst_entry *dst_null = &net->ipv6.ip6_null_entry->dst; 421 422 if (!pskb_may_pull(skb, ETH_HLEN + sizeof(struct ipv6hdr))) 423 goto err; 424 425 iph = ipv6_hdr(skb); 426 427 memset(&fl6, 0, sizeof(fl6)); 428 /* needed to match OIF rule */ 429 fl6.flowi6_l3mdev = dev->ifindex; 430 fl6.flowi6_iif = LOOPBACK_IFINDEX; 431 fl6.daddr = iph->daddr; 432 fl6.saddr = iph->saddr; 433 fl6.flowlabel = ip6_flowinfo(iph); 434 fl6.flowi6_mark = skb->mark; 435 fl6.flowi6_proto = iph->nexthdr; 436 437 dst = ip6_dst_lookup_flow(net, NULL, &fl6, NULL); 438 if (IS_ERR(dst) || dst == dst_null) 439 goto err; 440 441 skb_dst_drop(skb); 442 443 /* if dst.dev is the VRF device again this is locally originated traffic 444 * destined to a local address. Short circuit to Rx path. 445 */ 446 if (dst->dev == dev) 447 return vrf_local_xmit(skb, dev, dst); 448 449 skb_dst_set(skb, dst); 450 451 /* strip the ethernet header added for pass through VRF device */ 452 __skb_pull(skb, skb_network_offset(skb)); 453 454 memset(IP6CB(skb), 0, sizeof(*IP6CB(skb))); 455 ret = vrf_ip6_local_out(net, skb->sk, skb); 456 if (unlikely(net_xmit_eval(ret))) 457 dev->stats.tx_errors++; 458 else 459 ret = NET_XMIT_SUCCESS; 460 461 return ret; 462 err: 463 vrf_tx_error(dev, skb); 464 return NET_XMIT_DROP; 465 } 466 #else 467 static netdev_tx_t vrf_process_v6_outbound(struct sk_buff *skb, 468 struct net_device *dev) 469 { 470 vrf_tx_error(dev, skb); 471 return NET_XMIT_DROP; 472 } 473 #endif 474 475 /* based on ip_local_out; can't use it b/c the dst is switched pointing to us */ 476 static int vrf_ip_local_out(struct net *net, struct sock *sk, 477 struct sk_buff *skb) 478 { 479 int err; 480 481 vrf_nf_reset_ct(skb); 482 483 err = nf_hook(NFPROTO_IPV4, NF_INET_LOCAL_OUT, net, sk, 484 skb, NULL, skb_dst(skb)->dev, dst_output); 485 if (likely(err == 1)) 486 err = dst_output(net, sk, skb); 487 488 return err; 489 } 490 491 static netdev_tx_t vrf_process_v4_outbound(struct sk_buff *skb, 492 struct net_device *vrf_dev) 493 { 494 struct iphdr *ip4h; 495 int ret = NET_XMIT_DROP; 496 struct flowi4 fl4; 497 struct net *net = dev_net(vrf_dev); 498 struct rtable *rt; 499 500 if (!pskb_may_pull(skb, ETH_HLEN + sizeof(struct iphdr))) 501 goto err; 502 503 ip4h = ip_hdr(skb); 504 505 memset(&fl4, 0, sizeof(fl4)); 506 /* needed to match OIF rule */ 507 fl4.flowi4_l3mdev = vrf_dev->ifindex; 508 fl4.flowi4_iif = LOOPBACK_IFINDEX; 509 fl4.flowi4_tos = inet_dscp_to_dsfield(ip4h_dscp(ip4h)); 510 fl4.flowi4_flags = FLOWI_FLAG_ANYSRC; 511 fl4.flowi4_proto = ip4h->protocol; 512 fl4.daddr = ip4h->daddr; 513 fl4.saddr = ip4h->saddr; 514 515 rt = ip_route_output_flow(net, &fl4, NULL); 516 if (IS_ERR(rt)) 517 goto err; 518 519 skb_dst_drop(skb); 520 521 /* if dst.dev is the VRF device again this is locally originated traffic 522 * destined to a local address. Short circuit to Rx path. 523 */ 524 if (rt->dst.dev == vrf_dev) 525 return vrf_local_xmit(skb, vrf_dev, &rt->dst); 526 527 skb_dst_set(skb, &rt->dst); 528 529 /* strip the ethernet header added for pass through VRF device */ 530 __skb_pull(skb, skb_network_offset(skb)); 531 532 if (!ip4h->saddr) { 533 ip4h->saddr = inet_select_addr(skb_dst(skb)->dev, 0, 534 RT_SCOPE_LINK); 535 } 536 537 memset(IPCB(skb), 0, sizeof(*IPCB(skb))); 538 ret = vrf_ip_local_out(dev_net(skb_dst(skb)->dev), skb->sk, skb); 539 if (unlikely(net_xmit_eval(ret))) 540 vrf_dev->stats.tx_errors++; 541 else 542 ret = NET_XMIT_SUCCESS; 543 544 out: 545 return ret; 546 err: 547 vrf_tx_error(vrf_dev, skb); 548 goto out; 549 } 550 551 static netdev_tx_t is_ip_tx_frame(struct sk_buff *skb, struct net_device *dev) 552 { 553 switch (skb->protocol) { 554 case htons(ETH_P_IP): 555 return vrf_process_v4_outbound(skb, dev); 556 case htons(ETH_P_IPV6): 557 return vrf_process_v6_outbound(skb, dev); 558 default: 559 vrf_tx_error(dev, skb); 560 return NET_XMIT_DROP; 561 } 562 } 563 564 static netdev_tx_t vrf_xmit(struct sk_buff *skb, struct net_device *dev) 565 { 566 unsigned int len = skb->len; 567 netdev_tx_t ret; 568 569 ret = is_ip_tx_frame(skb, dev); 570 if (likely(ret == NET_XMIT_SUCCESS || ret == NET_XMIT_CN)) 571 dev_dstats_tx_add(dev, len); 572 else 573 dev_dstats_tx_dropped(dev); 574 575 return ret; 576 } 577 578 static void vrf_finish_direct(struct sk_buff *skb) 579 { 580 struct net_device *vrf_dev = skb->dev; 581 582 if (!list_empty(&vrf_dev->ptype_all) && 583 likely(skb_headroom(skb) >= ETH_HLEN)) { 584 struct ethhdr *eth = skb_push(skb, ETH_HLEN); 585 586 ether_addr_copy(eth->h_source, vrf_dev->dev_addr); 587 eth_zero_addr(eth->h_dest); 588 eth->h_proto = skb->protocol; 589 590 rcu_read_lock_bh(); 591 dev_queue_xmit_nit(skb, vrf_dev); 592 rcu_read_unlock_bh(); 593 594 skb_pull(skb, ETH_HLEN); 595 } 596 597 vrf_nf_reset_ct(skb); 598 } 599 600 #if IS_ENABLED(CONFIG_IPV6) 601 /* modelled after ip6_finish_output2 */ 602 static int vrf_finish_output6(struct net *net, struct sock *sk, 603 struct sk_buff *skb) 604 { 605 struct dst_entry *dst = skb_dst(skb); 606 struct net_device *dev = dst->dev; 607 const struct in6_addr *nexthop; 608 struct neighbour *neigh; 609 int ret; 610 611 vrf_nf_reset_ct(skb); 612 613 skb->protocol = htons(ETH_P_IPV6); 614 skb->dev = dev; 615 616 rcu_read_lock(); 617 nexthop = rt6_nexthop(dst_rt6_info(dst), &ipv6_hdr(skb)->daddr); 618 neigh = __ipv6_neigh_lookup_noref(dst->dev, nexthop); 619 if (unlikely(!neigh)) 620 neigh = __neigh_create(&nd_tbl, nexthop, dst->dev, false); 621 if (!IS_ERR(neigh)) { 622 sock_confirm_neigh(skb, neigh); 623 ret = neigh_output(neigh, skb, false); 624 rcu_read_unlock(); 625 return ret; 626 } 627 rcu_read_unlock(); 628 629 IP6_INC_STATS(dev_net(dst->dev), 630 ip6_dst_idev(dst), IPSTATS_MIB_OUTNOROUTES); 631 kfree_skb(skb); 632 return -EINVAL; 633 } 634 635 /* modelled after ip6_output */ 636 static int vrf_output6(struct net *net, struct sock *sk, struct sk_buff *skb) 637 { 638 return NF_HOOK_COND(NFPROTO_IPV6, NF_INET_POST_ROUTING, 639 net, sk, skb, NULL, skb_dst(skb)->dev, 640 vrf_finish_output6, 641 !(IP6CB(skb)->flags & IP6SKB_REROUTED)); 642 } 643 644 /* set dst on skb to send packet to us via dev_xmit path. Allows 645 * packet to go through device based features such as qdisc, netfilter 646 * hooks and packet sockets with skb->dev set to vrf device. 647 */ 648 static struct sk_buff *vrf_ip6_out_redirect(struct net_device *vrf_dev, 649 struct sk_buff *skb) 650 { 651 struct net_vrf *vrf = netdev_priv(vrf_dev); 652 struct dst_entry *dst = NULL; 653 struct rt6_info *rt6; 654 655 rcu_read_lock(); 656 657 rt6 = rcu_dereference(vrf->rt6); 658 if (likely(rt6)) { 659 dst = &rt6->dst; 660 dst_hold(dst); 661 } 662 663 rcu_read_unlock(); 664 665 if (unlikely(!dst)) { 666 vrf_tx_error(vrf_dev, skb); 667 return NULL; 668 } 669 670 skb_dst_drop(skb); 671 skb_dst_set(skb, dst); 672 673 return skb; 674 } 675 676 static int vrf_output6_direct_finish(struct net *net, struct sock *sk, 677 struct sk_buff *skb) 678 { 679 vrf_finish_direct(skb); 680 681 return vrf_ip6_local_out(net, sk, skb); 682 } 683 684 static int vrf_output6_direct(struct net *net, struct sock *sk, 685 struct sk_buff *skb) 686 { 687 int err = 1; 688 689 skb->protocol = htons(ETH_P_IPV6); 690 691 if (!(IPCB(skb)->flags & IPSKB_REROUTED)) 692 err = nf_hook(NFPROTO_IPV6, NF_INET_POST_ROUTING, net, sk, skb, 693 NULL, skb->dev, vrf_output6_direct_finish); 694 695 if (likely(err == 1)) 696 vrf_finish_direct(skb); 697 698 return err; 699 } 700 701 static int vrf_ip6_out_direct_finish(struct net *net, struct sock *sk, 702 struct sk_buff *skb) 703 { 704 int err; 705 706 err = vrf_output6_direct(net, sk, skb); 707 if (likely(err == 1)) 708 err = vrf_ip6_local_out(net, sk, skb); 709 710 return err; 711 } 712 713 static struct sk_buff *vrf_ip6_out_direct(struct net_device *vrf_dev, 714 struct sock *sk, 715 struct sk_buff *skb) 716 { 717 struct net *net = dev_net(vrf_dev); 718 int err; 719 720 skb->dev = vrf_dev; 721 722 err = nf_hook(NFPROTO_IPV6, NF_INET_LOCAL_OUT, net, sk, 723 skb, NULL, vrf_dev, vrf_ip6_out_direct_finish); 724 725 if (likely(err == 1)) 726 err = vrf_output6_direct(net, sk, skb); 727 728 if (likely(err == 1)) 729 return skb; 730 731 return NULL; 732 } 733 734 static struct sk_buff *vrf_ip6_out(struct net_device *vrf_dev, 735 struct sock *sk, 736 struct sk_buff *skb) 737 { 738 /* don't divert link scope packets */ 739 if (rt6_need_strict(&ipv6_hdr(skb)->daddr)) 740 return skb; 741 742 vrf_nf_set_untracked(skb); 743 744 if (qdisc_tx_is_default(vrf_dev) || 745 IP6CB(skb)->flags & IP6SKB_XFRM_TRANSFORMED) 746 return vrf_ip6_out_direct(vrf_dev, sk, skb); 747 748 return vrf_ip6_out_redirect(vrf_dev, skb); 749 } 750 751 /* holding rtnl */ 752 static void vrf_rt6_release(struct net_device *dev, struct net_vrf *vrf) 753 { 754 struct rt6_info *rt6 = rtnl_dereference(vrf->rt6); 755 struct net *net = dev_net(dev); 756 struct dst_entry *dst; 757 758 RCU_INIT_POINTER(vrf->rt6, NULL); 759 synchronize_rcu(); 760 761 /* move dev in dst's to loopback so this VRF device can be deleted 762 * - based on dst_ifdown 763 */ 764 if (rt6) { 765 dst = &rt6->dst; 766 netdev_ref_replace(dst->dev, net->loopback_dev, 767 &dst->dev_tracker, GFP_KERNEL); 768 dst->dev = net->loopback_dev; 769 dst_release(dst); 770 } 771 } 772 773 static int vrf_rt6_create(struct net_device *dev) 774 { 775 int flags = DST_NOPOLICY | DST_NOXFRM; 776 struct net_vrf *vrf = netdev_priv(dev); 777 struct net *net = dev_net(dev); 778 struct rt6_info *rt6; 779 int rc = -ENOMEM; 780 781 /* IPv6 can be CONFIG enabled and then disabled runtime */ 782 if (!ipv6_mod_enabled()) 783 return 0; 784 785 vrf->fib6_table = fib6_new_table(net, vrf->tb_id); 786 if (!vrf->fib6_table) 787 goto out; 788 789 /* create a dst for routing packets out a VRF device */ 790 rt6 = ip6_dst_alloc(net, dev, flags); 791 if (!rt6) 792 goto out; 793 794 rt6->dst.output = vrf_output6; 795 796 rcu_assign_pointer(vrf->rt6, rt6); 797 798 rc = 0; 799 out: 800 return rc; 801 } 802 #else 803 static struct sk_buff *vrf_ip6_out(struct net_device *vrf_dev, 804 struct sock *sk, 805 struct sk_buff *skb) 806 { 807 return skb; 808 } 809 810 static void vrf_rt6_release(struct net_device *dev, struct net_vrf *vrf) 811 { 812 } 813 814 static int vrf_rt6_create(struct net_device *dev) 815 { 816 return 0; 817 } 818 #endif 819 820 /* modelled after ip_finish_output2 */ 821 static int vrf_finish_output(struct net *net, struct sock *sk, struct sk_buff *skb) 822 { 823 struct dst_entry *dst = skb_dst(skb); 824 struct rtable *rt = dst_rtable(dst); 825 struct net_device *dev = dst->dev; 826 unsigned int hh_len = LL_RESERVED_SPACE(dev); 827 struct neighbour *neigh; 828 bool is_v6gw = false; 829 830 vrf_nf_reset_ct(skb); 831 832 /* Be paranoid, rather than too clever. */ 833 if (unlikely(skb_headroom(skb) < hh_len && dev->header_ops)) { 834 skb = skb_expand_head(skb, hh_len); 835 if (!skb) { 836 dev->stats.tx_errors++; 837 return -ENOMEM; 838 } 839 } 840 841 rcu_read_lock(); 842 843 neigh = ip_neigh_for_gw(rt, skb, &is_v6gw); 844 if (!IS_ERR(neigh)) { 845 int ret; 846 847 sock_confirm_neigh(skb, neigh); 848 /* if crossing protocols, can not use the cached header */ 849 ret = neigh_output(neigh, skb, is_v6gw); 850 rcu_read_unlock(); 851 return ret; 852 } 853 854 rcu_read_unlock(); 855 vrf_tx_error(skb->dev, skb); 856 return -EINVAL; 857 } 858 859 static int vrf_output(struct net *net, struct sock *sk, struct sk_buff *skb) 860 { 861 struct net_device *dev = skb_dst(skb)->dev; 862 863 IP_UPD_PO_STATS(net, IPSTATS_MIB_OUT, skb->len); 864 865 skb->dev = dev; 866 skb->protocol = htons(ETH_P_IP); 867 868 return NF_HOOK_COND(NFPROTO_IPV4, NF_INET_POST_ROUTING, 869 net, sk, skb, NULL, dev, 870 vrf_finish_output, 871 !(IPCB(skb)->flags & IPSKB_REROUTED)); 872 } 873 874 /* set dst on skb to send packet to us via dev_xmit path. Allows 875 * packet to go through device based features such as qdisc, netfilter 876 * hooks and packet sockets with skb->dev set to vrf device. 877 */ 878 static struct sk_buff *vrf_ip_out_redirect(struct net_device *vrf_dev, 879 struct sk_buff *skb) 880 { 881 struct net_vrf *vrf = netdev_priv(vrf_dev); 882 struct dst_entry *dst = NULL; 883 struct rtable *rth; 884 885 rcu_read_lock(); 886 887 rth = rcu_dereference(vrf->rth); 888 if (likely(rth)) { 889 dst = &rth->dst; 890 dst_hold(dst); 891 } 892 893 rcu_read_unlock(); 894 895 if (unlikely(!dst)) { 896 vrf_tx_error(vrf_dev, skb); 897 return NULL; 898 } 899 900 skb_dst_drop(skb); 901 skb_dst_set(skb, dst); 902 903 return skb; 904 } 905 906 static int vrf_output_direct_finish(struct net *net, struct sock *sk, 907 struct sk_buff *skb) 908 { 909 vrf_finish_direct(skb); 910 911 return vrf_ip_local_out(net, sk, skb); 912 } 913 914 static int vrf_output_direct(struct net *net, struct sock *sk, 915 struct sk_buff *skb) 916 { 917 int err = 1; 918 919 skb->protocol = htons(ETH_P_IP); 920 921 if (!(IPCB(skb)->flags & IPSKB_REROUTED)) 922 err = nf_hook(NFPROTO_IPV4, NF_INET_POST_ROUTING, net, sk, skb, 923 NULL, skb->dev, vrf_output_direct_finish); 924 925 if (likely(err == 1)) 926 vrf_finish_direct(skb); 927 928 return err; 929 } 930 931 static int vrf_ip_out_direct_finish(struct net *net, struct sock *sk, 932 struct sk_buff *skb) 933 { 934 int err; 935 936 err = vrf_output_direct(net, sk, skb); 937 if (likely(err == 1)) 938 err = vrf_ip_local_out(net, sk, skb); 939 940 return err; 941 } 942 943 static struct sk_buff *vrf_ip_out_direct(struct net_device *vrf_dev, 944 struct sock *sk, 945 struct sk_buff *skb) 946 { 947 struct net *net = dev_net(vrf_dev); 948 int err; 949 950 skb->dev = vrf_dev; 951 952 err = nf_hook(NFPROTO_IPV4, NF_INET_LOCAL_OUT, net, sk, 953 skb, NULL, vrf_dev, vrf_ip_out_direct_finish); 954 955 if (likely(err == 1)) 956 err = vrf_output_direct(net, sk, skb); 957 958 if (likely(err == 1)) 959 return skb; 960 961 return NULL; 962 } 963 964 static struct sk_buff *vrf_ip_out(struct net_device *vrf_dev, 965 struct sock *sk, 966 struct sk_buff *skb) 967 { 968 /* don't divert multicast or local broadcast */ 969 if (ipv4_is_multicast(ip_hdr(skb)->daddr) || 970 ipv4_is_lbcast(ip_hdr(skb)->daddr)) 971 return skb; 972 973 vrf_nf_set_untracked(skb); 974 975 if (qdisc_tx_is_default(vrf_dev) || 976 IPCB(skb)->flags & IPSKB_XFRM_TRANSFORMED) 977 return vrf_ip_out_direct(vrf_dev, sk, skb); 978 979 return vrf_ip_out_redirect(vrf_dev, skb); 980 } 981 982 /* called with rcu lock held */ 983 static struct sk_buff *vrf_l3_out(struct net_device *vrf_dev, 984 struct sock *sk, 985 struct sk_buff *skb, 986 u16 proto) 987 { 988 switch (proto) { 989 case AF_INET: 990 return vrf_ip_out(vrf_dev, sk, skb); 991 case AF_INET6: 992 return vrf_ip6_out(vrf_dev, sk, skb); 993 } 994 995 return skb; 996 } 997 998 /* holding rtnl */ 999 static void vrf_rtable_release(struct net_device *dev, struct net_vrf *vrf) 1000 { 1001 struct rtable *rth = rtnl_dereference(vrf->rth); 1002 struct net *net = dev_net(dev); 1003 struct dst_entry *dst; 1004 1005 RCU_INIT_POINTER(vrf->rth, NULL); 1006 synchronize_rcu(); 1007 1008 /* move dev in dst's to loopback so this VRF device can be deleted 1009 * - based on dst_ifdown 1010 */ 1011 if (rth) { 1012 dst = &rth->dst; 1013 netdev_ref_replace(dst->dev, net->loopback_dev, 1014 &dst->dev_tracker, GFP_KERNEL); 1015 dst->dev = net->loopback_dev; 1016 dst_release(dst); 1017 } 1018 } 1019 1020 static int vrf_rtable_create(struct net_device *dev) 1021 { 1022 struct net_vrf *vrf = netdev_priv(dev); 1023 struct rtable *rth; 1024 1025 if (!fib_new_table(dev_net(dev), vrf->tb_id)) 1026 return -ENOMEM; 1027 1028 /* create a dst for routing packets out through a VRF device */ 1029 rth = rt_dst_alloc(dev, 0, RTN_UNICAST, 1); 1030 if (!rth) 1031 return -ENOMEM; 1032 1033 rth->dst.output = vrf_output; 1034 1035 rcu_assign_pointer(vrf->rth, rth); 1036 1037 return 0; 1038 } 1039 1040 /**************************** device handling ********************/ 1041 1042 /* cycle interface to flush neighbor cache and move routes across tables */ 1043 static void cycle_netdev(struct net_device *dev, 1044 struct netlink_ext_ack *extack) 1045 { 1046 unsigned int flags = dev->flags; 1047 int ret; 1048 1049 if (!netif_running(dev)) 1050 return; 1051 1052 ret = dev_change_flags(dev, flags & ~IFF_UP, extack); 1053 if (ret >= 0) 1054 ret = dev_change_flags(dev, flags, extack); 1055 1056 if (ret < 0) { 1057 netdev_err(dev, 1058 "Failed to cycle device %s; route tables might be wrong!\n", 1059 dev->name); 1060 } 1061 } 1062 1063 static int do_vrf_add_slave(struct net_device *dev, struct net_device *port_dev, 1064 struct netlink_ext_ack *extack) 1065 { 1066 int ret; 1067 1068 /* do not allow loopback device to be enslaved to a VRF. 1069 * The vrf device acts as the loopback for the vrf. 1070 */ 1071 if (port_dev == dev_net(dev)->loopback_dev) { 1072 NL_SET_ERR_MSG(extack, 1073 "Can not enslave loopback device to a VRF"); 1074 return -EOPNOTSUPP; 1075 } 1076 1077 port_dev->priv_flags |= IFF_L3MDEV_SLAVE; 1078 ret = netdev_master_upper_dev_link(port_dev, dev, NULL, NULL, extack); 1079 if (ret < 0) 1080 goto err; 1081 1082 cycle_netdev(port_dev, extack); 1083 1084 return 0; 1085 1086 err: 1087 port_dev->priv_flags &= ~IFF_L3MDEV_SLAVE; 1088 return ret; 1089 } 1090 1091 static int vrf_add_slave(struct net_device *dev, struct net_device *port_dev, 1092 struct netlink_ext_ack *extack) 1093 { 1094 if (netif_is_l3_master(port_dev)) { 1095 NL_SET_ERR_MSG(extack, 1096 "Can not enslave an L3 master device to a VRF"); 1097 return -EINVAL; 1098 } 1099 1100 if (netif_is_l3_slave(port_dev)) 1101 return -EINVAL; 1102 1103 return do_vrf_add_slave(dev, port_dev, extack); 1104 } 1105 1106 /* inverse of do_vrf_add_slave */ 1107 static int do_vrf_del_slave(struct net_device *dev, struct net_device *port_dev) 1108 { 1109 netdev_upper_dev_unlink(port_dev, dev); 1110 port_dev->priv_flags &= ~IFF_L3MDEV_SLAVE; 1111 1112 cycle_netdev(port_dev, NULL); 1113 1114 return 0; 1115 } 1116 1117 static int vrf_del_slave(struct net_device *dev, struct net_device *port_dev) 1118 { 1119 return do_vrf_del_slave(dev, port_dev); 1120 } 1121 1122 static void vrf_dev_uninit(struct net_device *dev) 1123 { 1124 struct net_vrf *vrf = netdev_priv(dev); 1125 1126 vrf_rtable_release(dev, vrf); 1127 vrf_rt6_release(dev, vrf); 1128 } 1129 1130 static int vrf_dev_init(struct net_device *dev) 1131 { 1132 struct net_vrf *vrf = netdev_priv(dev); 1133 1134 /* create the default dst which points back to us */ 1135 if (vrf_rtable_create(dev) != 0) 1136 goto out_nomem; 1137 1138 if (vrf_rt6_create(dev) != 0) 1139 goto out_rth; 1140 1141 dev->flags = IFF_MASTER | IFF_NOARP; 1142 1143 /* similarly, oper state is irrelevant; set to up to avoid confusion */ 1144 dev->operstate = IF_OPER_UP; 1145 netdev_lockdep_set_classes(dev); 1146 return 0; 1147 1148 out_rth: 1149 vrf_rtable_release(dev, vrf); 1150 out_nomem: 1151 return -ENOMEM; 1152 } 1153 1154 static const struct net_device_ops vrf_netdev_ops = { 1155 .ndo_init = vrf_dev_init, 1156 .ndo_uninit = vrf_dev_uninit, 1157 .ndo_start_xmit = vrf_xmit, 1158 .ndo_set_mac_address = eth_mac_addr, 1159 .ndo_add_slave = vrf_add_slave, 1160 .ndo_del_slave = vrf_del_slave, 1161 }; 1162 1163 static u32 vrf_fib_table(const struct net_device *dev) 1164 { 1165 struct net_vrf *vrf = netdev_priv(dev); 1166 1167 return vrf->tb_id; 1168 } 1169 1170 static int vrf_rcv_finish(struct net *net, struct sock *sk, struct sk_buff *skb) 1171 { 1172 kfree_skb(skb); 1173 return 0; 1174 } 1175 1176 static struct sk_buff *vrf_rcv_nfhook(u8 pf, unsigned int hook, 1177 struct sk_buff *skb, 1178 struct net_device *dev) 1179 { 1180 struct net *net = dev_net(dev); 1181 1182 if (nf_hook(pf, hook, net, NULL, skb, dev, NULL, vrf_rcv_finish) != 1) 1183 skb = NULL; /* kfree_skb(skb) handled by nf code */ 1184 1185 return skb; 1186 } 1187 1188 static int vrf_prepare_mac_header(struct sk_buff *skb, 1189 struct net_device *vrf_dev, u16 proto) 1190 { 1191 struct ethhdr *eth; 1192 int err; 1193 1194 /* in general, we do not know if there is enough space in the head of 1195 * the packet for hosting the mac header. 1196 */ 1197 err = skb_cow_head(skb, LL_RESERVED_SPACE(vrf_dev)); 1198 if (unlikely(err)) 1199 /* no space in the skb head */ 1200 return -ENOBUFS; 1201 1202 __skb_push(skb, ETH_HLEN); 1203 eth = (struct ethhdr *)skb->data; 1204 1205 skb_reset_mac_header(skb); 1206 skb_reset_mac_len(skb); 1207 1208 /* we set the ethernet destination and the source addresses to the 1209 * address of the VRF device. 1210 */ 1211 ether_addr_copy(eth->h_dest, vrf_dev->dev_addr); 1212 ether_addr_copy(eth->h_source, vrf_dev->dev_addr); 1213 eth->h_proto = htons(proto); 1214 1215 /* the destination address of the Ethernet frame corresponds to the 1216 * address set on the VRF interface; therefore, the packet is intended 1217 * to be processed locally. 1218 */ 1219 skb->protocol = eth->h_proto; 1220 skb->pkt_type = PACKET_HOST; 1221 1222 skb_postpush_rcsum(skb, skb->data, ETH_HLEN); 1223 1224 skb_pull_inline(skb, ETH_HLEN); 1225 1226 return 0; 1227 } 1228 1229 /* prepare and add the mac header to the packet if it was not set previously. 1230 * In this way, packet sniffers such as tcpdump can parse the packet correctly. 1231 * If the mac header was already set, the original mac header is left 1232 * untouched and the function returns immediately. 1233 */ 1234 static int vrf_add_mac_header_if_unset(struct sk_buff *skb, 1235 struct net_device *vrf_dev, 1236 u16 proto, struct net_device *orig_dev) 1237 { 1238 if (skb_mac_header_was_set(skb) && dev_has_header(orig_dev)) 1239 return 0; 1240 1241 return vrf_prepare_mac_header(skb, vrf_dev, proto); 1242 } 1243 1244 #if IS_ENABLED(CONFIG_IPV6) 1245 /* neighbor handling is done with actual device; do not want 1246 * to flip skb->dev for those ndisc packets. This really fails 1247 * for multiple next protocols (e.g., NEXTHDR_HOP). But it is 1248 * a start. 1249 */ 1250 static bool ipv6_ndisc_frame(const struct sk_buff *skb) 1251 { 1252 const struct ipv6hdr *iph = ipv6_hdr(skb); 1253 bool rc = false; 1254 1255 if (iph->nexthdr == NEXTHDR_ICMP) { 1256 const struct icmp6hdr *icmph; 1257 struct icmp6hdr _icmph; 1258 1259 icmph = skb_header_pointer(skb, sizeof(*iph), 1260 sizeof(_icmph), &_icmph); 1261 if (!icmph) 1262 goto out; 1263 1264 switch (icmph->icmp6_type) { 1265 case NDISC_ROUTER_SOLICITATION: 1266 case NDISC_ROUTER_ADVERTISEMENT: 1267 case NDISC_NEIGHBOUR_SOLICITATION: 1268 case NDISC_NEIGHBOUR_ADVERTISEMENT: 1269 case NDISC_REDIRECT: 1270 rc = true; 1271 break; 1272 } 1273 } 1274 1275 out: 1276 return rc; 1277 } 1278 1279 static struct rt6_info *vrf_ip6_route_lookup(struct net *net, 1280 const struct net_device *dev, 1281 struct flowi6 *fl6, 1282 int ifindex, 1283 const struct sk_buff *skb, 1284 int flags) 1285 { 1286 struct net_vrf *vrf = netdev_priv(dev); 1287 1288 return ip6_pol_route(net, vrf->fib6_table, ifindex, fl6, skb, flags); 1289 } 1290 1291 static void vrf_ip6_input_dst(struct sk_buff *skb, struct net_device *vrf_dev, 1292 int ifindex) 1293 { 1294 const struct ipv6hdr *iph = ipv6_hdr(skb); 1295 struct flowi6 fl6 = { 1296 .flowi6_iif = ifindex, 1297 .flowi6_mark = skb->mark, 1298 .flowi6_proto = iph->nexthdr, 1299 .daddr = iph->daddr, 1300 .saddr = iph->saddr, 1301 .flowlabel = ip6_flowinfo(iph), 1302 }; 1303 struct net *net = dev_net(vrf_dev); 1304 struct rt6_info *rt6; 1305 1306 rt6 = vrf_ip6_route_lookup(net, vrf_dev, &fl6, ifindex, skb, 1307 RT6_LOOKUP_F_HAS_SADDR | RT6_LOOKUP_F_IFACE); 1308 if (unlikely(!rt6)) 1309 return; 1310 1311 if (unlikely(&rt6->dst == &net->ipv6.ip6_null_entry->dst)) 1312 return; 1313 1314 skb_dst_set(skb, &rt6->dst); 1315 } 1316 1317 static struct sk_buff *vrf_ip6_rcv(struct net_device *vrf_dev, 1318 struct sk_buff *skb) 1319 { 1320 int orig_iif = skb->skb_iif; 1321 bool need_strict = rt6_need_strict(&ipv6_hdr(skb)->daddr); 1322 bool is_ndisc = ipv6_ndisc_frame(skb); 1323 1324 /* loopback, multicast & non-ND link-local traffic; do not push through 1325 * packet taps again. Reset pkt_type for upper layers to process skb. 1326 * For non-loopback strict packets, determine the dst using the original 1327 * ifindex. 1328 */ 1329 if (skb->pkt_type == PACKET_LOOPBACK || (need_strict && !is_ndisc)) { 1330 skb->dev = vrf_dev; 1331 skb->skb_iif = vrf_dev->ifindex; 1332 IP6CB(skb)->flags |= IP6SKB_L3SLAVE; 1333 1334 if (skb->pkt_type == PACKET_LOOPBACK) 1335 skb->pkt_type = PACKET_HOST; 1336 else 1337 vrf_ip6_input_dst(skb, vrf_dev, orig_iif); 1338 1339 goto out; 1340 } 1341 1342 /* if packet is NDISC then keep the ingress interface */ 1343 if (!is_ndisc) { 1344 struct net_device *orig_dev = skb->dev; 1345 1346 dev_dstats_rx_add(vrf_dev, skb->len); 1347 skb->dev = vrf_dev; 1348 skb->skb_iif = vrf_dev->ifindex; 1349 1350 if (!list_empty(&vrf_dev->ptype_all)) { 1351 int err; 1352 1353 err = vrf_add_mac_header_if_unset(skb, vrf_dev, 1354 ETH_P_IPV6, 1355 orig_dev); 1356 if (likely(!err)) { 1357 skb_push(skb, skb->mac_len); 1358 dev_queue_xmit_nit(skb, vrf_dev); 1359 skb_pull(skb, skb->mac_len); 1360 } 1361 } 1362 1363 IP6CB(skb)->flags |= IP6SKB_L3SLAVE; 1364 } 1365 1366 if (need_strict) 1367 vrf_ip6_input_dst(skb, vrf_dev, orig_iif); 1368 1369 skb = vrf_rcv_nfhook(NFPROTO_IPV6, NF_INET_PRE_ROUTING, skb, vrf_dev); 1370 out: 1371 return skb; 1372 } 1373 1374 #else 1375 static struct sk_buff *vrf_ip6_rcv(struct net_device *vrf_dev, 1376 struct sk_buff *skb) 1377 { 1378 return skb; 1379 } 1380 #endif 1381 1382 static struct sk_buff *vrf_ip_rcv(struct net_device *vrf_dev, 1383 struct sk_buff *skb) 1384 { 1385 struct net_device *orig_dev = skb->dev; 1386 1387 skb->dev = vrf_dev; 1388 skb->skb_iif = vrf_dev->ifindex; 1389 IPCB(skb)->flags |= IPSKB_L3SLAVE; 1390 1391 if (ipv4_is_multicast(ip_hdr(skb)->daddr)) 1392 goto out; 1393 1394 /* loopback traffic; do not push through packet taps again. 1395 * Reset pkt_type for upper layers to process skb 1396 */ 1397 if (skb->pkt_type == PACKET_LOOPBACK) { 1398 skb->pkt_type = PACKET_HOST; 1399 goto out; 1400 } 1401 1402 dev_dstats_rx_add(vrf_dev, skb->len); 1403 1404 if (!list_empty(&vrf_dev->ptype_all)) { 1405 int err; 1406 1407 err = vrf_add_mac_header_if_unset(skb, vrf_dev, ETH_P_IP, 1408 orig_dev); 1409 if (likely(!err)) { 1410 skb_push(skb, skb->mac_len); 1411 dev_queue_xmit_nit(skb, vrf_dev); 1412 skb_pull(skb, skb->mac_len); 1413 } 1414 } 1415 1416 skb = vrf_rcv_nfhook(NFPROTO_IPV4, NF_INET_PRE_ROUTING, skb, vrf_dev); 1417 out: 1418 return skb; 1419 } 1420 1421 /* called with rcu lock held */ 1422 static struct sk_buff *vrf_l3_rcv(struct net_device *vrf_dev, 1423 struct sk_buff *skb, 1424 u16 proto) 1425 { 1426 switch (proto) { 1427 case AF_INET: 1428 return vrf_ip_rcv(vrf_dev, skb); 1429 case AF_INET6: 1430 return vrf_ip6_rcv(vrf_dev, skb); 1431 } 1432 1433 return skb; 1434 } 1435 1436 #if IS_ENABLED(CONFIG_IPV6) 1437 /* send to link-local or multicast address via interface enslaved to 1438 * VRF device. Force lookup to VRF table without changing flow struct 1439 * Note: Caller to this function must hold rcu_read_lock() and no refcnt 1440 * is taken on the dst by this function. 1441 */ 1442 static struct dst_entry *vrf_link_scope_lookup(const struct net_device *dev, 1443 struct flowi6 *fl6) 1444 { 1445 struct net *net = dev_net(dev); 1446 int flags = RT6_LOOKUP_F_IFACE | RT6_LOOKUP_F_DST_NOREF; 1447 struct dst_entry *dst = NULL; 1448 struct rt6_info *rt; 1449 1450 /* VRF device does not have a link-local address and 1451 * sending packets to link-local or mcast addresses over 1452 * a VRF device does not make sense 1453 */ 1454 if (fl6->flowi6_oif == dev->ifindex) { 1455 dst = &net->ipv6.ip6_null_entry->dst; 1456 return dst; 1457 } 1458 1459 if (!ipv6_addr_any(&fl6->saddr)) 1460 flags |= RT6_LOOKUP_F_HAS_SADDR; 1461 1462 rt = vrf_ip6_route_lookup(net, dev, fl6, fl6->flowi6_oif, NULL, flags); 1463 if (rt) 1464 dst = &rt->dst; 1465 1466 return dst; 1467 } 1468 #endif 1469 1470 static const struct l3mdev_ops vrf_l3mdev_ops = { 1471 .l3mdev_fib_table = vrf_fib_table, 1472 .l3mdev_l3_rcv = vrf_l3_rcv, 1473 .l3mdev_l3_out = vrf_l3_out, 1474 #if IS_ENABLED(CONFIG_IPV6) 1475 .l3mdev_link_scope_lookup = vrf_link_scope_lookup, 1476 #endif 1477 }; 1478 1479 static void vrf_get_drvinfo(struct net_device *dev, 1480 struct ethtool_drvinfo *info) 1481 { 1482 strscpy(info->driver, DRV_NAME, sizeof(info->driver)); 1483 strscpy(info->version, DRV_VERSION, sizeof(info->version)); 1484 } 1485 1486 static const struct ethtool_ops vrf_ethtool_ops = { 1487 .get_drvinfo = vrf_get_drvinfo, 1488 }; 1489 1490 static inline size_t vrf_fib_rule_nl_size(void) 1491 { 1492 size_t sz; 1493 1494 sz = NLMSG_ALIGN(sizeof(struct fib_rule_hdr)); 1495 sz += nla_total_size(sizeof(u8)); /* FRA_L3MDEV */ 1496 sz += nla_total_size(sizeof(u32)); /* FRA_PRIORITY */ 1497 sz += nla_total_size(sizeof(u8)); /* FRA_PROTOCOL */ 1498 1499 return sz; 1500 } 1501 1502 static int vrf_fib_rule(const struct net_device *dev, __u8 family, bool add_it) 1503 { 1504 struct fib_rule_hdr *frh; 1505 struct nlmsghdr *nlh; 1506 struct sk_buff *skb; 1507 int err; 1508 1509 if ((family == AF_INET6 || family == RTNL_FAMILY_IP6MR) && 1510 !ipv6_mod_enabled()) 1511 return 0; 1512 1513 skb = nlmsg_new(vrf_fib_rule_nl_size(), GFP_KERNEL); 1514 if (!skb) 1515 return -ENOMEM; 1516 1517 nlh = nlmsg_put(skb, 0, 0, 0, sizeof(*frh), 0); 1518 if (!nlh) 1519 goto nla_put_failure; 1520 1521 /* rule only needs to appear once */ 1522 nlh->nlmsg_flags |= NLM_F_EXCL; 1523 1524 frh = nlmsg_data(nlh); 1525 memset(frh, 0, sizeof(*frh)); 1526 frh->family = family; 1527 frh->action = FR_ACT_TO_TBL; 1528 1529 if (nla_put_u8(skb, FRA_PROTOCOL, RTPROT_KERNEL)) 1530 goto nla_put_failure; 1531 1532 if (nla_put_u8(skb, FRA_L3MDEV, 1)) 1533 goto nla_put_failure; 1534 1535 if (nla_put_u32(skb, FRA_PRIORITY, FIB_RULE_PREF)) 1536 goto nla_put_failure; 1537 1538 nlmsg_end(skb, nlh); 1539 1540 /* fib_nl_{new,del}rule handling looks for net from skb->sk */ 1541 skb->sk = dev_net(dev)->rtnl; 1542 if (add_it) { 1543 err = fib_nl_newrule(skb, nlh, NULL); 1544 if (err == -EEXIST) 1545 err = 0; 1546 } else { 1547 err = fib_nl_delrule(skb, nlh, NULL); 1548 if (err == -ENOENT) 1549 err = 0; 1550 } 1551 nlmsg_free(skb); 1552 1553 return err; 1554 1555 nla_put_failure: 1556 nlmsg_free(skb); 1557 1558 return -EMSGSIZE; 1559 } 1560 1561 static int vrf_add_fib_rules(const struct net_device *dev) 1562 { 1563 int err; 1564 1565 err = vrf_fib_rule(dev, AF_INET, true); 1566 if (err < 0) 1567 goto out_err; 1568 1569 err = vrf_fib_rule(dev, AF_INET6, true); 1570 if (err < 0) 1571 goto ipv6_err; 1572 1573 #if IS_ENABLED(CONFIG_IP_MROUTE_MULTIPLE_TABLES) 1574 err = vrf_fib_rule(dev, RTNL_FAMILY_IPMR, true); 1575 if (err < 0) 1576 goto ipmr_err; 1577 #endif 1578 1579 #if IS_ENABLED(CONFIG_IPV6_MROUTE_MULTIPLE_TABLES) 1580 err = vrf_fib_rule(dev, RTNL_FAMILY_IP6MR, true); 1581 if (err < 0) 1582 goto ip6mr_err; 1583 #endif 1584 1585 return 0; 1586 1587 #if IS_ENABLED(CONFIG_IPV6_MROUTE_MULTIPLE_TABLES) 1588 ip6mr_err: 1589 vrf_fib_rule(dev, RTNL_FAMILY_IPMR, false); 1590 #endif 1591 1592 #if IS_ENABLED(CONFIG_IP_MROUTE_MULTIPLE_TABLES) 1593 ipmr_err: 1594 vrf_fib_rule(dev, AF_INET6, false); 1595 #endif 1596 1597 ipv6_err: 1598 vrf_fib_rule(dev, AF_INET, false); 1599 1600 out_err: 1601 netdev_err(dev, "Failed to add FIB rules.\n"); 1602 return err; 1603 } 1604 1605 static void vrf_setup(struct net_device *dev) 1606 { 1607 ether_setup(dev); 1608 1609 /* Initialize the device structure. */ 1610 dev->netdev_ops = &vrf_netdev_ops; 1611 dev->l3mdev_ops = &vrf_l3mdev_ops; 1612 dev->ethtool_ops = &vrf_ethtool_ops; 1613 dev->needs_free_netdev = true; 1614 1615 /* Fill in device structure with ethernet-generic values. */ 1616 eth_hw_addr_random(dev); 1617 1618 /* don't acquire vrf device's netif_tx_lock when transmitting */ 1619 dev->lltx = true; 1620 1621 /* don't allow vrf devices to change network namespaces. */ 1622 dev->netns_local = true; 1623 1624 /* does not make sense for a VLAN to be added to a vrf device */ 1625 dev->features |= NETIF_F_VLAN_CHALLENGED; 1626 1627 /* enable offload features */ 1628 dev->features |= NETIF_F_GSO_SOFTWARE; 1629 dev->features |= NETIF_F_RXCSUM | NETIF_F_HW_CSUM | NETIF_F_SCTP_CRC; 1630 dev->features |= NETIF_F_SG | NETIF_F_FRAGLIST | NETIF_F_HIGHDMA; 1631 1632 dev->hw_features = dev->features; 1633 dev->hw_enc_features = dev->features; 1634 1635 /* default to no qdisc; user can add if desired */ 1636 dev->priv_flags |= IFF_NO_QUEUE; 1637 dev->priv_flags |= IFF_NO_RX_HANDLER; 1638 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE; 1639 1640 /* VRF devices do not care about MTU, but if the MTU is set 1641 * too low then the ipv4 and ipv6 protocols are disabled 1642 * which breaks networking. 1643 */ 1644 dev->min_mtu = IPV6_MIN_MTU; 1645 dev->max_mtu = IP6_MAX_MTU; 1646 dev->mtu = dev->max_mtu; 1647 1648 dev->pcpu_stat_type = NETDEV_PCPU_STAT_DSTATS; 1649 } 1650 1651 static int vrf_validate(struct nlattr *tb[], struct nlattr *data[], 1652 struct netlink_ext_ack *extack) 1653 { 1654 if (tb[IFLA_ADDRESS]) { 1655 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) { 1656 NL_SET_ERR_MSG(extack, "Invalid hardware address"); 1657 return -EINVAL; 1658 } 1659 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) { 1660 NL_SET_ERR_MSG(extack, "Invalid hardware address"); 1661 return -EADDRNOTAVAIL; 1662 } 1663 } 1664 return 0; 1665 } 1666 1667 static void vrf_dellink(struct net_device *dev, struct list_head *head) 1668 { 1669 struct net_device *port_dev; 1670 struct list_head *iter; 1671 1672 netdev_for_each_lower_dev(dev, port_dev, iter) 1673 vrf_del_slave(dev, port_dev); 1674 1675 vrf_map_unregister_dev(dev); 1676 1677 unregister_netdevice_queue(dev, head); 1678 } 1679 1680 static int vrf_newlink(struct net *src_net, struct net_device *dev, 1681 struct nlattr *tb[], struct nlattr *data[], 1682 struct netlink_ext_ack *extack) 1683 { 1684 struct net_vrf *vrf = netdev_priv(dev); 1685 struct netns_vrf *nn_vrf; 1686 bool *add_fib_rules; 1687 struct net *net; 1688 int err; 1689 1690 if (!data || !data[IFLA_VRF_TABLE]) { 1691 NL_SET_ERR_MSG(extack, "VRF table id is missing"); 1692 return -EINVAL; 1693 } 1694 1695 vrf->tb_id = nla_get_u32(data[IFLA_VRF_TABLE]); 1696 if (vrf->tb_id == RT_TABLE_UNSPEC) { 1697 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_VRF_TABLE], 1698 "Invalid VRF table id"); 1699 return -EINVAL; 1700 } 1701 1702 dev->priv_flags |= IFF_L3MDEV_MASTER; 1703 1704 err = register_netdevice(dev); 1705 if (err) 1706 goto out; 1707 1708 /* mapping between table_id and vrf; 1709 * note: such binding could not be done in the dev init function 1710 * because dev->ifindex id is not available yet. 1711 */ 1712 vrf->ifindex = dev->ifindex; 1713 1714 err = vrf_map_register_dev(dev, extack); 1715 if (err) { 1716 unregister_netdevice(dev); 1717 goto out; 1718 } 1719 1720 net = dev_net(dev); 1721 nn_vrf = net_generic(net, vrf_net_id); 1722 1723 add_fib_rules = &nn_vrf->add_fib_rules; 1724 if (*add_fib_rules) { 1725 err = vrf_add_fib_rules(dev); 1726 if (err) { 1727 vrf_map_unregister_dev(dev); 1728 unregister_netdevice(dev); 1729 goto out; 1730 } 1731 *add_fib_rules = false; 1732 } 1733 1734 out: 1735 return err; 1736 } 1737 1738 static size_t vrf_nl_getsize(const struct net_device *dev) 1739 { 1740 return nla_total_size(sizeof(u32)); /* IFLA_VRF_TABLE */ 1741 } 1742 1743 static int vrf_fillinfo(struct sk_buff *skb, 1744 const struct net_device *dev) 1745 { 1746 struct net_vrf *vrf = netdev_priv(dev); 1747 1748 return nla_put_u32(skb, IFLA_VRF_TABLE, vrf->tb_id); 1749 } 1750 1751 static size_t vrf_get_slave_size(const struct net_device *bond_dev, 1752 const struct net_device *slave_dev) 1753 { 1754 return nla_total_size(sizeof(u32)); /* IFLA_VRF_PORT_TABLE */ 1755 } 1756 1757 static int vrf_fill_slave_info(struct sk_buff *skb, 1758 const struct net_device *vrf_dev, 1759 const struct net_device *slave_dev) 1760 { 1761 struct net_vrf *vrf = netdev_priv(vrf_dev); 1762 1763 if (nla_put_u32(skb, IFLA_VRF_PORT_TABLE, vrf->tb_id)) 1764 return -EMSGSIZE; 1765 1766 return 0; 1767 } 1768 1769 static const struct nla_policy vrf_nl_policy[IFLA_VRF_MAX + 1] = { 1770 [IFLA_VRF_TABLE] = { .type = NLA_U32 }, 1771 }; 1772 1773 static struct rtnl_link_ops vrf_link_ops __read_mostly = { 1774 .kind = DRV_NAME, 1775 .priv_size = sizeof(struct net_vrf), 1776 1777 .get_size = vrf_nl_getsize, 1778 .policy = vrf_nl_policy, 1779 .validate = vrf_validate, 1780 .fill_info = vrf_fillinfo, 1781 1782 .get_slave_size = vrf_get_slave_size, 1783 .fill_slave_info = vrf_fill_slave_info, 1784 1785 .newlink = vrf_newlink, 1786 .dellink = vrf_dellink, 1787 .setup = vrf_setup, 1788 .maxtype = IFLA_VRF_MAX, 1789 }; 1790 1791 static int vrf_device_event(struct notifier_block *unused, 1792 unsigned long event, void *ptr) 1793 { 1794 struct net_device *dev = netdev_notifier_info_to_dev(ptr); 1795 1796 /* only care about unregister events to drop slave references */ 1797 if (event == NETDEV_UNREGISTER) { 1798 struct net_device *vrf_dev; 1799 1800 if (!netif_is_l3_slave(dev)) 1801 goto out; 1802 1803 vrf_dev = netdev_master_upper_dev_get(dev); 1804 vrf_del_slave(vrf_dev, dev); 1805 } 1806 out: 1807 return NOTIFY_DONE; 1808 } 1809 1810 static struct notifier_block vrf_notifier_block __read_mostly = { 1811 .notifier_call = vrf_device_event, 1812 }; 1813 1814 static int vrf_map_init(struct vrf_map *vmap) 1815 { 1816 spin_lock_init(&vmap->vmap_lock); 1817 hash_init(vmap->ht); 1818 1819 vmap->strict_mode = false; 1820 1821 return 0; 1822 } 1823 1824 #ifdef CONFIG_SYSCTL 1825 static bool vrf_strict_mode(struct vrf_map *vmap) 1826 { 1827 bool strict_mode; 1828 1829 vrf_map_lock(vmap); 1830 strict_mode = vmap->strict_mode; 1831 vrf_map_unlock(vmap); 1832 1833 return strict_mode; 1834 } 1835 1836 static int vrf_strict_mode_change(struct vrf_map *vmap, bool new_mode) 1837 { 1838 bool *cur_mode; 1839 int res = 0; 1840 1841 vrf_map_lock(vmap); 1842 1843 cur_mode = &vmap->strict_mode; 1844 if (*cur_mode == new_mode) 1845 goto unlock; 1846 1847 if (*cur_mode) { 1848 /* disable strict mode */ 1849 *cur_mode = false; 1850 } else { 1851 if (vmap->shared_tables) { 1852 /* we cannot allow strict_mode because there are some 1853 * vrfs that share one or more tables. 1854 */ 1855 res = -EBUSY; 1856 goto unlock; 1857 } 1858 1859 /* no tables are shared among vrfs, so we can go back 1860 * to 1:1 association between a vrf with its table. 1861 */ 1862 *cur_mode = true; 1863 } 1864 1865 unlock: 1866 vrf_map_unlock(vmap); 1867 1868 return res; 1869 } 1870 1871 static int vrf_shared_table_handler(const struct ctl_table *table, int write, 1872 void *buffer, size_t *lenp, loff_t *ppos) 1873 { 1874 struct net *net = (struct net *)table->extra1; 1875 struct vrf_map *vmap = netns_vrf_map(net); 1876 int proc_strict_mode = 0; 1877 struct ctl_table tmp = { 1878 .procname = table->procname, 1879 .data = &proc_strict_mode, 1880 .maxlen = sizeof(int), 1881 .mode = table->mode, 1882 .extra1 = SYSCTL_ZERO, 1883 .extra2 = SYSCTL_ONE, 1884 }; 1885 int ret; 1886 1887 if (!write) 1888 proc_strict_mode = vrf_strict_mode(vmap); 1889 1890 ret = proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos); 1891 1892 if (write && ret == 0) 1893 ret = vrf_strict_mode_change(vmap, (bool)proc_strict_mode); 1894 1895 return ret; 1896 } 1897 1898 static const struct ctl_table vrf_table[] = { 1899 { 1900 .procname = "strict_mode", 1901 .data = NULL, 1902 .maxlen = sizeof(int), 1903 .mode = 0644, 1904 .proc_handler = vrf_shared_table_handler, 1905 /* set by the vrf_netns_init */ 1906 .extra1 = NULL, 1907 }, 1908 }; 1909 1910 static int vrf_netns_init_sysctl(struct net *net, struct netns_vrf *nn_vrf) 1911 { 1912 struct ctl_table *table; 1913 1914 table = kmemdup(vrf_table, sizeof(vrf_table), GFP_KERNEL); 1915 if (!table) 1916 return -ENOMEM; 1917 1918 /* init the extra1 parameter with the reference to current netns */ 1919 table[0].extra1 = net; 1920 1921 nn_vrf->ctl_hdr = register_net_sysctl_sz(net, "net/vrf", table, 1922 ARRAY_SIZE(vrf_table)); 1923 if (!nn_vrf->ctl_hdr) { 1924 kfree(table); 1925 return -ENOMEM; 1926 } 1927 1928 return 0; 1929 } 1930 1931 static void vrf_netns_exit_sysctl(struct net *net) 1932 { 1933 struct netns_vrf *nn_vrf = net_generic(net, vrf_net_id); 1934 const struct ctl_table *table; 1935 1936 table = nn_vrf->ctl_hdr->ctl_table_arg; 1937 unregister_net_sysctl_table(nn_vrf->ctl_hdr); 1938 kfree(table); 1939 } 1940 #else 1941 static int vrf_netns_init_sysctl(struct net *net, struct netns_vrf *nn_vrf) 1942 { 1943 return 0; 1944 } 1945 1946 static void vrf_netns_exit_sysctl(struct net *net) 1947 { 1948 } 1949 #endif 1950 1951 /* Initialize per network namespace state */ 1952 static int __net_init vrf_netns_init(struct net *net) 1953 { 1954 struct netns_vrf *nn_vrf = net_generic(net, vrf_net_id); 1955 1956 nn_vrf->add_fib_rules = true; 1957 vrf_map_init(&nn_vrf->vmap); 1958 1959 return vrf_netns_init_sysctl(net, nn_vrf); 1960 } 1961 1962 static void __net_exit vrf_netns_exit(struct net *net) 1963 { 1964 vrf_netns_exit_sysctl(net); 1965 } 1966 1967 static struct pernet_operations vrf_net_ops __net_initdata = { 1968 .init = vrf_netns_init, 1969 .exit = vrf_netns_exit, 1970 .id = &vrf_net_id, 1971 .size = sizeof(struct netns_vrf), 1972 }; 1973 1974 static int __init vrf_init_module(void) 1975 { 1976 int rc; 1977 1978 register_netdevice_notifier(&vrf_notifier_block); 1979 1980 rc = register_pernet_subsys(&vrf_net_ops); 1981 if (rc < 0) 1982 goto error; 1983 1984 rc = l3mdev_table_lookup_register(L3MDEV_TYPE_VRF, 1985 vrf_ifindex_lookup_by_table_id); 1986 if (rc < 0) 1987 goto unreg_pernet; 1988 1989 rc = rtnl_link_register(&vrf_link_ops); 1990 if (rc < 0) 1991 goto table_lookup_unreg; 1992 1993 return 0; 1994 1995 table_lookup_unreg: 1996 l3mdev_table_lookup_unregister(L3MDEV_TYPE_VRF, 1997 vrf_ifindex_lookup_by_table_id); 1998 1999 unreg_pernet: 2000 unregister_pernet_subsys(&vrf_net_ops); 2001 2002 error: 2003 unregister_netdevice_notifier(&vrf_notifier_block); 2004 return rc; 2005 } 2006 2007 module_init(vrf_init_module); 2008 MODULE_AUTHOR("Shrijeet Mukherjee, David Ahern"); 2009 MODULE_DESCRIPTION("Device driver to instantiate VRF domains"); 2010 MODULE_LICENSE("GPL"); 2011 MODULE_ALIAS_RTNL_LINK(DRV_NAME); 2012 MODULE_VERSION(DRV_VERSION); 2013