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