1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * IP multicast routing support for mrouted 3.6/3.8 4 * 5 * (c) 1995 Alan Cox, <alan@lxorguk.ukuu.org.uk> 6 * Linux Consultancy and Custom Driver Development 7 * 8 * Fixes: 9 * Michael Chastain : Incorrect size of copying. 10 * Alan Cox : Added the cache manager code 11 * Alan Cox : Fixed the clone/copy bug and device race. 12 * Mike McLagan : Routing by source 13 * Malcolm Beattie : Buffer handling fixes. 14 * Alexey Kuznetsov : Double buffer free and other fixes. 15 * SVR Anand : Fixed several multicast bugs and problems. 16 * Alexey Kuznetsov : Status, optimisations and more. 17 * Brad Parker : Better behaviour on mrouted upcall 18 * overflow. 19 * Carlos Picoto : PIMv1 Support 20 * Pavlin Ivanov Radoslavov: PIMv2 Registers must checksum only PIM header 21 * Relax this requirement to work with older peers. 22 */ 23 24 #include <linux/uaccess.h> 25 #include <linux/types.h> 26 #include <linux/cache.h> 27 #include <linux/capability.h> 28 #include <linux/errno.h> 29 #include <linux/mm.h> 30 #include <linux/kernel.h> 31 #include <linux/fcntl.h> 32 #include <linux/stat.h> 33 #include <linux/socket.h> 34 #include <linux/in.h> 35 #include <linux/inet.h> 36 #include <linux/netdevice.h> 37 #include <linux/inetdevice.h> 38 #include <linux/igmp.h> 39 #include <linux/proc_fs.h> 40 #include <linux/seq_file.h> 41 #include <linux/mroute.h> 42 #include <linux/init.h> 43 #include <linux/if_ether.h> 44 #include <linux/slab.h> 45 #include <net/net_namespace.h> 46 #include <net/ip.h> 47 #include <net/protocol.h> 48 #include <linux/skbuff.h> 49 #include <net/route.h> 50 #include <net/icmp.h> 51 #include <net/udp.h> 52 #include <net/raw.h> 53 #include <linux/notifier.h> 54 #include <linux/if_arp.h> 55 #include <linux/netfilter_ipv4.h> 56 #include <linux/compat.h> 57 #include <linux/export.h> 58 #include <linux/rhashtable.h> 59 #include <net/ip_tunnels.h> 60 #include <net/checksum.h> 61 #include <net/netlink.h> 62 #include <net/fib_rules.h> 63 #include <linux/netconf.h> 64 #include <net/rtnh.h> 65 66 #include <linux/nospec.h> 67 68 struct ipmr_rule { 69 struct fib_rule common; 70 }; 71 72 struct ipmr_result { 73 struct mr_table *mrt; 74 }; 75 76 /* Big lock, protecting vif table, mrt cache and mroute socket state. 77 * Note that the changes are semaphored via rtnl_lock. 78 */ 79 80 static DEFINE_RWLOCK(mrt_lock); 81 82 /* Multicast router control variables */ 83 84 /* Special spinlock for queue of unresolved entries */ 85 static DEFINE_SPINLOCK(mfc_unres_lock); 86 87 /* We return to original Alan's scheme. Hash table of resolved 88 * entries is changed only in process context and protected 89 * with weak lock mrt_lock. Queue of unresolved entries is protected 90 * with strong spinlock mfc_unres_lock. 91 * 92 * In this case data path is free of exclusive locks at all. 93 */ 94 95 static struct kmem_cache *mrt_cachep __ro_after_init; 96 97 static struct mr_table *ipmr_new_table(struct net *net, u32 id); 98 static void ipmr_free_table(struct mr_table *mrt); 99 100 static void ip_mr_forward(struct net *net, struct mr_table *mrt, 101 struct net_device *dev, struct sk_buff *skb, 102 struct mfc_cache *cache, int local); 103 static int ipmr_cache_report(struct mr_table *mrt, 104 struct sk_buff *pkt, vifi_t vifi, int assert); 105 static void mroute_netlink_event(struct mr_table *mrt, struct mfc_cache *mfc, 106 int cmd); 107 static void igmpmsg_netlink_event(struct mr_table *mrt, struct sk_buff *pkt); 108 static void mroute_clean_tables(struct mr_table *mrt, int flags); 109 static void ipmr_expire_process(struct timer_list *t); 110 111 #ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES 112 #define ipmr_for_each_table(mrt, net) \ 113 list_for_each_entry_rcu(mrt, &net->ipv4.mr_tables, list, \ 114 lockdep_rtnl_is_held() || \ 115 list_empty(&net->ipv4.mr_tables)) 116 117 static struct mr_table *ipmr_mr_table_iter(struct net *net, 118 struct mr_table *mrt) 119 { 120 struct mr_table *ret; 121 122 if (!mrt) 123 ret = list_entry_rcu(net->ipv4.mr_tables.next, 124 struct mr_table, list); 125 else 126 ret = list_entry_rcu(mrt->list.next, 127 struct mr_table, list); 128 129 if (&ret->list == &net->ipv4.mr_tables) 130 return NULL; 131 return ret; 132 } 133 134 static struct mr_table *ipmr_get_table(struct net *net, u32 id) 135 { 136 struct mr_table *mrt; 137 138 ipmr_for_each_table(mrt, net) { 139 if (mrt->id == id) 140 return mrt; 141 } 142 return NULL; 143 } 144 145 static int ipmr_fib_lookup(struct net *net, struct flowi4 *flp4, 146 struct mr_table **mrt) 147 { 148 int err; 149 struct ipmr_result res; 150 struct fib_lookup_arg arg = { 151 .result = &res, 152 .flags = FIB_LOOKUP_NOREF, 153 }; 154 155 /* update flow if oif or iif point to device enslaved to l3mdev */ 156 l3mdev_update_flow(net, flowi4_to_flowi(flp4)); 157 158 err = fib_rules_lookup(net->ipv4.mr_rules_ops, 159 flowi4_to_flowi(flp4), 0, &arg); 160 if (err < 0) 161 return err; 162 *mrt = res.mrt; 163 return 0; 164 } 165 166 static int ipmr_rule_action(struct fib_rule *rule, struct flowi *flp, 167 int flags, struct fib_lookup_arg *arg) 168 { 169 struct ipmr_result *res = arg->result; 170 struct mr_table *mrt; 171 172 switch (rule->action) { 173 case FR_ACT_TO_TBL: 174 break; 175 case FR_ACT_UNREACHABLE: 176 return -ENETUNREACH; 177 case FR_ACT_PROHIBIT: 178 return -EACCES; 179 case FR_ACT_BLACKHOLE: 180 default: 181 return -EINVAL; 182 } 183 184 arg->table = fib_rule_get_table(rule, arg); 185 186 mrt = ipmr_get_table(rule->fr_net, arg->table); 187 if (!mrt) 188 return -EAGAIN; 189 res->mrt = mrt; 190 return 0; 191 } 192 193 static int ipmr_rule_match(struct fib_rule *rule, struct flowi *fl, int flags) 194 { 195 return 1; 196 } 197 198 static const struct nla_policy ipmr_rule_policy[FRA_MAX + 1] = { 199 FRA_GENERIC_POLICY, 200 }; 201 202 static int ipmr_rule_configure(struct fib_rule *rule, struct sk_buff *skb, 203 struct fib_rule_hdr *frh, struct nlattr **tb, 204 struct netlink_ext_ack *extack) 205 { 206 return 0; 207 } 208 209 static int ipmr_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh, 210 struct nlattr **tb) 211 { 212 return 1; 213 } 214 215 static int ipmr_rule_fill(struct fib_rule *rule, struct sk_buff *skb, 216 struct fib_rule_hdr *frh) 217 { 218 frh->dst_len = 0; 219 frh->src_len = 0; 220 frh->tos = 0; 221 return 0; 222 } 223 224 static const struct fib_rules_ops __net_initconst ipmr_rules_ops_template = { 225 .family = RTNL_FAMILY_IPMR, 226 .rule_size = sizeof(struct ipmr_rule), 227 .addr_size = sizeof(u32), 228 .action = ipmr_rule_action, 229 .match = ipmr_rule_match, 230 .configure = ipmr_rule_configure, 231 .compare = ipmr_rule_compare, 232 .fill = ipmr_rule_fill, 233 .nlgroup = RTNLGRP_IPV4_RULE, 234 .policy = ipmr_rule_policy, 235 .owner = THIS_MODULE, 236 }; 237 238 static int __net_init ipmr_rules_init(struct net *net) 239 { 240 struct fib_rules_ops *ops; 241 struct mr_table *mrt; 242 int err; 243 244 ops = fib_rules_register(&ipmr_rules_ops_template, net); 245 if (IS_ERR(ops)) 246 return PTR_ERR(ops); 247 248 INIT_LIST_HEAD(&net->ipv4.mr_tables); 249 250 mrt = ipmr_new_table(net, RT_TABLE_DEFAULT); 251 if (IS_ERR(mrt)) { 252 err = PTR_ERR(mrt); 253 goto err1; 254 } 255 256 err = fib_default_rule_add(ops, 0x7fff, RT_TABLE_DEFAULT, 0); 257 if (err < 0) 258 goto err2; 259 260 net->ipv4.mr_rules_ops = ops; 261 return 0; 262 263 err2: 264 ipmr_free_table(mrt); 265 err1: 266 fib_rules_unregister(ops); 267 return err; 268 } 269 270 static void __net_exit ipmr_rules_exit(struct net *net) 271 { 272 struct mr_table *mrt, *next; 273 274 rtnl_lock(); 275 list_for_each_entry_safe(mrt, next, &net->ipv4.mr_tables, list) { 276 list_del(&mrt->list); 277 ipmr_free_table(mrt); 278 } 279 fib_rules_unregister(net->ipv4.mr_rules_ops); 280 rtnl_unlock(); 281 } 282 283 static int ipmr_rules_dump(struct net *net, struct notifier_block *nb, 284 struct netlink_ext_ack *extack) 285 { 286 return fib_rules_dump(net, nb, RTNL_FAMILY_IPMR, extack); 287 } 288 289 static unsigned int ipmr_rules_seq_read(struct net *net) 290 { 291 return fib_rules_seq_read(net, RTNL_FAMILY_IPMR); 292 } 293 294 bool ipmr_rule_default(const struct fib_rule *rule) 295 { 296 return fib_rule_matchall(rule) && rule->table == RT_TABLE_DEFAULT; 297 } 298 EXPORT_SYMBOL(ipmr_rule_default); 299 #else 300 #define ipmr_for_each_table(mrt, net) \ 301 for (mrt = net->ipv4.mrt; mrt; mrt = NULL) 302 303 static struct mr_table *ipmr_mr_table_iter(struct net *net, 304 struct mr_table *mrt) 305 { 306 if (!mrt) 307 return net->ipv4.mrt; 308 return NULL; 309 } 310 311 static struct mr_table *ipmr_get_table(struct net *net, u32 id) 312 { 313 return net->ipv4.mrt; 314 } 315 316 static int ipmr_fib_lookup(struct net *net, struct flowi4 *flp4, 317 struct mr_table **mrt) 318 { 319 *mrt = net->ipv4.mrt; 320 return 0; 321 } 322 323 static int __net_init ipmr_rules_init(struct net *net) 324 { 325 struct mr_table *mrt; 326 327 mrt = ipmr_new_table(net, RT_TABLE_DEFAULT); 328 if (IS_ERR(mrt)) 329 return PTR_ERR(mrt); 330 net->ipv4.mrt = mrt; 331 return 0; 332 } 333 334 static void __net_exit ipmr_rules_exit(struct net *net) 335 { 336 rtnl_lock(); 337 ipmr_free_table(net->ipv4.mrt); 338 net->ipv4.mrt = NULL; 339 rtnl_unlock(); 340 } 341 342 static int ipmr_rules_dump(struct net *net, struct notifier_block *nb, 343 struct netlink_ext_ack *extack) 344 { 345 return 0; 346 } 347 348 static unsigned int ipmr_rules_seq_read(struct net *net) 349 { 350 return 0; 351 } 352 353 bool ipmr_rule_default(const struct fib_rule *rule) 354 { 355 return true; 356 } 357 EXPORT_SYMBOL(ipmr_rule_default); 358 #endif 359 360 static inline int ipmr_hash_cmp(struct rhashtable_compare_arg *arg, 361 const void *ptr) 362 { 363 const struct mfc_cache_cmp_arg *cmparg = arg->key; 364 struct mfc_cache *c = (struct mfc_cache *)ptr; 365 366 return cmparg->mfc_mcastgrp != c->mfc_mcastgrp || 367 cmparg->mfc_origin != c->mfc_origin; 368 } 369 370 static const struct rhashtable_params ipmr_rht_params = { 371 .head_offset = offsetof(struct mr_mfc, mnode), 372 .key_offset = offsetof(struct mfc_cache, cmparg), 373 .key_len = sizeof(struct mfc_cache_cmp_arg), 374 .nelem_hint = 3, 375 .obj_cmpfn = ipmr_hash_cmp, 376 .automatic_shrinking = true, 377 }; 378 379 static void ipmr_new_table_set(struct mr_table *mrt, 380 struct net *net) 381 { 382 #ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES 383 list_add_tail_rcu(&mrt->list, &net->ipv4.mr_tables); 384 #endif 385 } 386 387 static struct mfc_cache_cmp_arg ipmr_mr_table_ops_cmparg_any = { 388 .mfc_mcastgrp = htonl(INADDR_ANY), 389 .mfc_origin = htonl(INADDR_ANY), 390 }; 391 392 static struct mr_table_ops ipmr_mr_table_ops = { 393 .rht_params = &ipmr_rht_params, 394 .cmparg_any = &ipmr_mr_table_ops_cmparg_any, 395 }; 396 397 static struct mr_table *ipmr_new_table(struct net *net, u32 id) 398 { 399 struct mr_table *mrt; 400 401 /* "pimreg%u" should not exceed 16 bytes (IFNAMSIZ) */ 402 if (id != RT_TABLE_DEFAULT && id >= 1000000000) 403 return ERR_PTR(-EINVAL); 404 405 mrt = ipmr_get_table(net, id); 406 if (mrt) 407 return mrt; 408 409 return mr_table_alloc(net, id, &ipmr_mr_table_ops, 410 ipmr_expire_process, ipmr_new_table_set); 411 } 412 413 static void ipmr_free_table(struct mr_table *mrt) 414 { 415 del_timer_sync(&mrt->ipmr_expire_timer); 416 mroute_clean_tables(mrt, MRT_FLUSH_VIFS | MRT_FLUSH_VIFS_STATIC | 417 MRT_FLUSH_MFC | MRT_FLUSH_MFC_STATIC); 418 rhltable_destroy(&mrt->mfc_hash); 419 kfree(mrt); 420 } 421 422 /* Service routines creating virtual interfaces: DVMRP tunnels and PIMREG */ 423 424 static void ipmr_del_tunnel(struct net_device *dev, struct vifctl *v) 425 { 426 struct net *net = dev_net(dev); 427 428 dev_close(dev); 429 430 dev = __dev_get_by_name(net, "tunl0"); 431 if (dev) { 432 const struct net_device_ops *ops = dev->netdev_ops; 433 struct ifreq ifr; 434 struct ip_tunnel_parm p; 435 436 memset(&p, 0, sizeof(p)); 437 p.iph.daddr = v->vifc_rmt_addr.s_addr; 438 p.iph.saddr = v->vifc_lcl_addr.s_addr; 439 p.iph.version = 4; 440 p.iph.ihl = 5; 441 p.iph.protocol = IPPROTO_IPIP; 442 sprintf(p.name, "dvmrp%d", v->vifc_vifi); 443 ifr.ifr_ifru.ifru_data = (__force void __user *)&p; 444 445 if (ops->ndo_do_ioctl) { 446 mm_segment_t oldfs = get_fs(); 447 448 set_fs(KERNEL_DS); 449 ops->ndo_do_ioctl(dev, &ifr, SIOCDELTUNNEL); 450 set_fs(oldfs); 451 } 452 } 453 } 454 455 /* Initialize ipmr pimreg/tunnel in_device */ 456 static bool ipmr_init_vif_indev(const struct net_device *dev) 457 { 458 struct in_device *in_dev; 459 460 ASSERT_RTNL(); 461 462 in_dev = __in_dev_get_rtnl(dev); 463 if (!in_dev) 464 return false; 465 ipv4_devconf_setall(in_dev); 466 neigh_parms_data_state_setall(in_dev->arp_parms); 467 IPV4_DEVCONF(in_dev->cnf, RP_FILTER) = 0; 468 469 return true; 470 } 471 472 static struct net_device *ipmr_new_tunnel(struct net *net, struct vifctl *v) 473 { 474 struct net_device *dev; 475 476 dev = __dev_get_by_name(net, "tunl0"); 477 478 if (dev) { 479 const struct net_device_ops *ops = dev->netdev_ops; 480 int err; 481 struct ifreq ifr; 482 struct ip_tunnel_parm p; 483 484 memset(&p, 0, sizeof(p)); 485 p.iph.daddr = v->vifc_rmt_addr.s_addr; 486 p.iph.saddr = v->vifc_lcl_addr.s_addr; 487 p.iph.version = 4; 488 p.iph.ihl = 5; 489 p.iph.protocol = IPPROTO_IPIP; 490 sprintf(p.name, "dvmrp%d", v->vifc_vifi); 491 ifr.ifr_ifru.ifru_data = (__force void __user *)&p; 492 493 if (ops->ndo_do_ioctl) { 494 mm_segment_t oldfs = get_fs(); 495 496 set_fs(KERNEL_DS); 497 err = ops->ndo_do_ioctl(dev, &ifr, SIOCADDTUNNEL); 498 set_fs(oldfs); 499 } else { 500 err = -EOPNOTSUPP; 501 } 502 dev = NULL; 503 504 if (err == 0 && 505 (dev = __dev_get_by_name(net, p.name)) != NULL) { 506 dev->flags |= IFF_MULTICAST; 507 if (!ipmr_init_vif_indev(dev)) 508 goto failure; 509 if (dev_open(dev, NULL)) 510 goto failure; 511 dev_hold(dev); 512 } 513 } 514 return dev; 515 516 failure: 517 unregister_netdevice(dev); 518 return NULL; 519 } 520 521 #if defined(CONFIG_IP_PIMSM_V1) || defined(CONFIG_IP_PIMSM_V2) 522 static netdev_tx_t reg_vif_xmit(struct sk_buff *skb, struct net_device *dev) 523 { 524 struct net *net = dev_net(dev); 525 struct mr_table *mrt; 526 struct flowi4 fl4 = { 527 .flowi4_oif = dev->ifindex, 528 .flowi4_iif = skb->skb_iif ? : LOOPBACK_IFINDEX, 529 .flowi4_mark = skb->mark, 530 }; 531 int err; 532 533 err = ipmr_fib_lookup(net, &fl4, &mrt); 534 if (err < 0) { 535 kfree_skb(skb); 536 return err; 537 } 538 539 read_lock(&mrt_lock); 540 dev->stats.tx_bytes += skb->len; 541 dev->stats.tx_packets++; 542 ipmr_cache_report(mrt, skb, mrt->mroute_reg_vif_num, IGMPMSG_WHOLEPKT); 543 read_unlock(&mrt_lock); 544 kfree_skb(skb); 545 return NETDEV_TX_OK; 546 } 547 548 static int reg_vif_get_iflink(const struct net_device *dev) 549 { 550 return 0; 551 } 552 553 static const struct net_device_ops reg_vif_netdev_ops = { 554 .ndo_start_xmit = reg_vif_xmit, 555 .ndo_get_iflink = reg_vif_get_iflink, 556 }; 557 558 static void reg_vif_setup(struct net_device *dev) 559 { 560 dev->type = ARPHRD_PIMREG; 561 dev->mtu = ETH_DATA_LEN - sizeof(struct iphdr) - 8; 562 dev->flags = IFF_NOARP; 563 dev->netdev_ops = ®_vif_netdev_ops; 564 dev->needs_free_netdev = true; 565 dev->features |= NETIF_F_NETNS_LOCAL; 566 } 567 568 static struct net_device *ipmr_reg_vif(struct net *net, struct mr_table *mrt) 569 { 570 struct net_device *dev; 571 char name[IFNAMSIZ]; 572 573 if (mrt->id == RT_TABLE_DEFAULT) 574 sprintf(name, "pimreg"); 575 else 576 sprintf(name, "pimreg%u", mrt->id); 577 578 dev = alloc_netdev(0, name, NET_NAME_UNKNOWN, reg_vif_setup); 579 580 if (!dev) 581 return NULL; 582 583 dev_net_set(dev, net); 584 585 if (register_netdevice(dev)) { 586 free_netdev(dev); 587 return NULL; 588 } 589 590 if (!ipmr_init_vif_indev(dev)) 591 goto failure; 592 if (dev_open(dev, NULL)) 593 goto failure; 594 595 dev_hold(dev); 596 597 return dev; 598 599 failure: 600 unregister_netdevice(dev); 601 return NULL; 602 } 603 604 /* called with rcu_read_lock() */ 605 static int __pim_rcv(struct mr_table *mrt, struct sk_buff *skb, 606 unsigned int pimlen) 607 { 608 struct net_device *reg_dev = NULL; 609 struct iphdr *encap; 610 611 encap = (struct iphdr *)(skb_transport_header(skb) + pimlen); 612 /* Check that: 613 * a. packet is really sent to a multicast group 614 * b. packet is not a NULL-REGISTER 615 * c. packet is not truncated 616 */ 617 if (!ipv4_is_multicast(encap->daddr) || 618 encap->tot_len == 0 || 619 ntohs(encap->tot_len) + pimlen > skb->len) 620 return 1; 621 622 read_lock(&mrt_lock); 623 if (mrt->mroute_reg_vif_num >= 0) 624 reg_dev = mrt->vif_table[mrt->mroute_reg_vif_num].dev; 625 read_unlock(&mrt_lock); 626 627 if (!reg_dev) 628 return 1; 629 630 skb->mac_header = skb->network_header; 631 skb_pull(skb, (u8 *)encap - skb->data); 632 skb_reset_network_header(skb); 633 skb->protocol = htons(ETH_P_IP); 634 skb->ip_summed = CHECKSUM_NONE; 635 636 skb_tunnel_rx(skb, reg_dev, dev_net(reg_dev)); 637 638 netif_rx(skb); 639 640 return NET_RX_SUCCESS; 641 } 642 #else 643 static struct net_device *ipmr_reg_vif(struct net *net, struct mr_table *mrt) 644 { 645 return NULL; 646 } 647 #endif 648 649 static int call_ipmr_vif_entry_notifiers(struct net *net, 650 enum fib_event_type event_type, 651 struct vif_device *vif, 652 vifi_t vif_index, u32 tb_id) 653 { 654 return mr_call_vif_notifiers(net, RTNL_FAMILY_IPMR, event_type, 655 vif, vif_index, tb_id, 656 &net->ipv4.ipmr_seq); 657 } 658 659 static int call_ipmr_mfc_entry_notifiers(struct net *net, 660 enum fib_event_type event_type, 661 struct mfc_cache *mfc, u32 tb_id) 662 { 663 return mr_call_mfc_notifiers(net, RTNL_FAMILY_IPMR, event_type, 664 &mfc->_c, tb_id, &net->ipv4.ipmr_seq); 665 } 666 667 /** 668 * vif_delete - Delete a VIF entry 669 * @notify: Set to 1, if the caller is a notifier_call 670 */ 671 static int vif_delete(struct mr_table *mrt, int vifi, int notify, 672 struct list_head *head) 673 { 674 struct net *net = read_pnet(&mrt->net); 675 struct vif_device *v; 676 struct net_device *dev; 677 struct in_device *in_dev; 678 679 if (vifi < 0 || vifi >= mrt->maxvif) 680 return -EADDRNOTAVAIL; 681 682 v = &mrt->vif_table[vifi]; 683 684 if (VIF_EXISTS(mrt, vifi)) 685 call_ipmr_vif_entry_notifiers(net, FIB_EVENT_VIF_DEL, v, vifi, 686 mrt->id); 687 688 write_lock_bh(&mrt_lock); 689 dev = v->dev; 690 v->dev = NULL; 691 692 if (!dev) { 693 write_unlock_bh(&mrt_lock); 694 return -EADDRNOTAVAIL; 695 } 696 697 if (vifi == mrt->mroute_reg_vif_num) 698 mrt->mroute_reg_vif_num = -1; 699 700 if (vifi + 1 == mrt->maxvif) { 701 int tmp; 702 703 for (tmp = vifi - 1; tmp >= 0; tmp--) { 704 if (VIF_EXISTS(mrt, tmp)) 705 break; 706 } 707 mrt->maxvif = tmp+1; 708 } 709 710 write_unlock_bh(&mrt_lock); 711 712 dev_set_allmulti(dev, -1); 713 714 in_dev = __in_dev_get_rtnl(dev); 715 if (in_dev) { 716 IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)--; 717 inet_netconf_notify_devconf(dev_net(dev), RTM_NEWNETCONF, 718 NETCONFA_MC_FORWARDING, 719 dev->ifindex, &in_dev->cnf); 720 ip_rt_multicast_event(in_dev); 721 } 722 723 if (v->flags & (VIFF_TUNNEL | VIFF_REGISTER) && !notify) 724 unregister_netdevice_queue(dev, head); 725 726 dev_put(dev); 727 return 0; 728 } 729 730 static void ipmr_cache_free_rcu(struct rcu_head *head) 731 { 732 struct mr_mfc *c = container_of(head, struct mr_mfc, rcu); 733 734 kmem_cache_free(mrt_cachep, (struct mfc_cache *)c); 735 } 736 737 static void ipmr_cache_free(struct mfc_cache *c) 738 { 739 call_rcu(&c->_c.rcu, ipmr_cache_free_rcu); 740 } 741 742 /* Destroy an unresolved cache entry, killing queued skbs 743 * and reporting error to netlink readers. 744 */ 745 static void ipmr_destroy_unres(struct mr_table *mrt, struct mfc_cache *c) 746 { 747 struct net *net = read_pnet(&mrt->net); 748 struct sk_buff *skb; 749 struct nlmsgerr *e; 750 751 atomic_dec(&mrt->cache_resolve_queue_len); 752 753 while ((skb = skb_dequeue(&c->_c.mfc_un.unres.unresolved))) { 754 if (ip_hdr(skb)->version == 0) { 755 struct nlmsghdr *nlh = skb_pull(skb, 756 sizeof(struct iphdr)); 757 nlh->nlmsg_type = NLMSG_ERROR; 758 nlh->nlmsg_len = nlmsg_msg_size(sizeof(struct nlmsgerr)); 759 skb_trim(skb, nlh->nlmsg_len); 760 e = nlmsg_data(nlh); 761 e->error = -ETIMEDOUT; 762 memset(&e->msg, 0, sizeof(e->msg)); 763 764 rtnl_unicast(skb, net, NETLINK_CB(skb).portid); 765 } else { 766 kfree_skb(skb); 767 } 768 } 769 770 ipmr_cache_free(c); 771 } 772 773 /* Timer process for the unresolved queue. */ 774 static void ipmr_expire_process(struct timer_list *t) 775 { 776 struct mr_table *mrt = from_timer(mrt, t, ipmr_expire_timer); 777 struct mr_mfc *c, *next; 778 unsigned long expires; 779 unsigned long now; 780 781 if (!spin_trylock(&mfc_unres_lock)) { 782 mod_timer(&mrt->ipmr_expire_timer, jiffies+HZ/10); 783 return; 784 } 785 786 if (list_empty(&mrt->mfc_unres_queue)) 787 goto out; 788 789 now = jiffies; 790 expires = 10*HZ; 791 792 list_for_each_entry_safe(c, next, &mrt->mfc_unres_queue, list) { 793 if (time_after(c->mfc_un.unres.expires, now)) { 794 unsigned long interval = c->mfc_un.unres.expires - now; 795 if (interval < expires) 796 expires = interval; 797 continue; 798 } 799 800 list_del(&c->list); 801 mroute_netlink_event(mrt, (struct mfc_cache *)c, RTM_DELROUTE); 802 ipmr_destroy_unres(mrt, (struct mfc_cache *)c); 803 } 804 805 if (!list_empty(&mrt->mfc_unres_queue)) 806 mod_timer(&mrt->ipmr_expire_timer, jiffies + expires); 807 808 out: 809 spin_unlock(&mfc_unres_lock); 810 } 811 812 /* Fill oifs list. It is called under write locked mrt_lock. */ 813 static void ipmr_update_thresholds(struct mr_table *mrt, struct mr_mfc *cache, 814 unsigned char *ttls) 815 { 816 int vifi; 817 818 cache->mfc_un.res.minvif = MAXVIFS; 819 cache->mfc_un.res.maxvif = 0; 820 memset(cache->mfc_un.res.ttls, 255, MAXVIFS); 821 822 for (vifi = 0; vifi < mrt->maxvif; vifi++) { 823 if (VIF_EXISTS(mrt, vifi) && 824 ttls[vifi] && ttls[vifi] < 255) { 825 cache->mfc_un.res.ttls[vifi] = ttls[vifi]; 826 if (cache->mfc_un.res.minvif > vifi) 827 cache->mfc_un.res.minvif = vifi; 828 if (cache->mfc_un.res.maxvif <= vifi) 829 cache->mfc_un.res.maxvif = vifi + 1; 830 } 831 } 832 cache->mfc_un.res.lastuse = jiffies; 833 } 834 835 static int vif_add(struct net *net, struct mr_table *mrt, 836 struct vifctl *vifc, int mrtsock) 837 { 838 struct netdev_phys_item_id ppid = { }; 839 int vifi = vifc->vifc_vifi; 840 struct vif_device *v = &mrt->vif_table[vifi]; 841 struct net_device *dev; 842 struct in_device *in_dev; 843 int err; 844 845 /* Is vif busy ? */ 846 if (VIF_EXISTS(mrt, vifi)) 847 return -EADDRINUSE; 848 849 switch (vifc->vifc_flags) { 850 case VIFF_REGISTER: 851 if (!ipmr_pimsm_enabled()) 852 return -EINVAL; 853 /* Special Purpose VIF in PIM 854 * All the packets will be sent to the daemon 855 */ 856 if (mrt->mroute_reg_vif_num >= 0) 857 return -EADDRINUSE; 858 dev = ipmr_reg_vif(net, mrt); 859 if (!dev) 860 return -ENOBUFS; 861 err = dev_set_allmulti(dev, 1); 862 if (err) { 863 unregister_netdevice(dev); 864 dev_put(dev); 865 return err; 866 } 867 break; 868 case VIFF_TUNNEL: 869 dev = ipmr_new_tunnel(net, vifc); 870 if (!dev) 871 return -ENOBUFS; 872 err = dev_set_allmulti(dev, 1); 873 if (err) { 874 ipmr_del_tunnel(dev, vifc); 875 dev_put(dev); 876 return err; 877 } 878 break; 879 case VIFF_USE_IFINDEX: 880 case 0: 881 if (vifc->vifc_flags == VIFF_USE_IFINDEX) { 882 dev = dev_get_by_index(net, vifc->vifc_lcl_ifindex); 883 if (dev && !__in_dev_get_rtnl(dev)) { 884 dev_put(dev); 885 return -EADDRNOTAVAIL; 886 } 887 } else { 888 dev = ip_dev_find(net, vifc->vifc_lcl_addr.s_addr); 889 } 890 if (!dev) 891 return -EADDRNOTAVAIL; 892 err = dev_set_allmulti(dev, 1); 893 if (err) { 894 dev_put(dev); 895 return err; 896 } 897 break; 898 default: 899 return -EINVAL; 900 } 901 902 in_dev = __in_dev_get_rtnl(dev); 903 if (!in_dev) { 904 dev_put(dev); 905 return -EADDRNOTAVAIL; 906 } 907 IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)++; 908 inet_netconf_notify_devconf(net, RTM_NEWNETCONF, NETCONFA_MC_FORWARDING, 909 dev->ifindex, &in_dev->cnf); 910 ip_rt_multicast_event(in_dev); 911 912 /* Fill in the VIF structures */ 913 vif_device_init(v, dev, vifc->vifc_rate_limit, 914 vifc->vifc_threshold, 915 vifc->vifc_flags | (!mrtsock ? VIFF_STATIC : 0), 916 (VIFF_TUNNEL | VIFF_REGISTER)); 917 918 err = dev_get_port_parent_id(dev, &ppid, true); 919 if (err == 0) { 920 memcpy(v->dev_parent_id.id, ppid.id, ppid.id_len); 921 v->dev_parent_id.id_len = ppid.id_len; 922 } else { 923 v->dev_parent_id.id_len = 0; 924 } 925 926 v->local = vifc->vifc_lcl_addr.s_addr; 927 v->remote = vifc->vifc_rmt_addr.s_addr; 928 929 /* And finish update writing critical data */ 930 write_lock_bh(&mrt_lock); 931 v->dev = dev; 932 if (v->flags & VIFF_REGISTER) 933 mrt->mroute_reg_vif_num = vifi; 934 if (vifi+1 > mrt->maxvif) 935 mrt->maxvif = vifi+1; 936 write_unlock_bh(&mrt_lock); 937 call_ipmr_vif_entry_notifiers(net, FIB_EVENT_VIF_ADD, v, vifi, mrt->id); 938 return 0; 939 } 940 941 /* called with rcu_read_lock() */ 942 static struct mfc_cache *ipmr_cache_find(struct mr_table *mrt, 943 __be32 origin, 944 __be32 mcastgrp) 945 { 946 struct mfc_cache_cmp_arg arg = { 947 .mfc_mcastgrp = mcastgrp, 948 .mfc_origin = origin 949 }; 950 951 return mr_mfc_find(mrt, &arg); 952 } 953 954 /* Look for a (*,G) entry */ 955 static struct mfc_cache *ipmr_cache_find_any(struct mr_table *mrt, 956 __be32 mcastgrp, int vifi) 957 { 958 struct mfc_cache_cmp_arg arg = { 959 .mfc_mcastgrp = mcastgrp, 960 .mfc_origin = htonl(INADDR_ANY) 961 }; 962 963 if (mcastgrp == htonl(INADDR_ANY)) 964 return mr_mfc_find_any_parent(mrt, vifi); 965 return mr_mfc_find_any(mrt, vifi, &arg); 966 } 967 968 /* Look for a (S,G,iif) entry if parent != -1 */ 969 static struct mfc_cache *ipmr_cache_find_parent(struct mr_table *mrt, 970 __be32 origin, __be32 mcastgrp, 971 int parent) 972 { 973 struct mfc_cache_cmp_arg arg = { 974 .mfc_mcastgrp = mcastgrp, 975 .mfc_origin = origin, 976 }; 977 978 return mr_mfc_find_parent(mrt, &arg, parent); 979 } 980 981 /* Allocate a multicast cache entry */ 982 static struct mfc_cache *ipmr_cache_alloc(void) 983 { 984 struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_KERNEL); 985 986 if (c) { 987 c->_c.mfc_un.res.last_assert = jiffies - MFC_ASSERT_THRESH - 1; 988 c->_c.mfc_un.res.minvif = MAXVIFS; 989 c->_c.free = ipmr_cache_free_rcu; 990 refcount_set(&c->_c.mfc_un.res.refcount, 1); 991 } 992 return c; 993 } 994 995 static struct mfc_cache *ipmr_cache_alloc_unres(void) 996 { 997 struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_ATOMIC); 998 999 if (c) { 1000 skb_queue_head_init(&c->_c.mfc_un.unres.unresolved); 1001 c->_c.mfc_un.unres.expires = jiffies + 10 * HZ; 1002 } 1003 return c; 1004 } 1005 1006 /* A cache entry has gone into a resolved state from queued */ 1007 static void ipmr_cache_resolve(struct net *net, struct mr_table *mrt, 1008 struct mfc_cache *uc, struct mfc_cache *c) 1009 { 1010 struct sk_buff *skb; 1011 struct nlmsgerr *e; 1012 1013 /* Play the pending entries through our router */ 1014 while ((skb = __skb_dequeue(&uc->_c.mfc_un.unres.unresolved))) { 1015 if (ip_hdr(skb)->version == 0) { 1016 struct nlmsghdr *nlh = skb_pull(skb, 1017 sizeof(struct iphdr)); 1018 1019 if (mr_fill_mroute(mrt, skb, &c->_c, 1020 nlmsg_data(nlh)) > 0) { 1021 nlh->nlmsg_len = skb_tail_pointer(skb) - 1022 (u8 *)nlh; 1023 } else { 1024 nlh->nlmsg_type = NLMSG_ERROR; 1025 nlh->nlmsg_len = nlmsg_msg_size(sizeof(struct nlmsgerr)); 1026 skb_trim(skb, nlh->nlmsg_len); 1027 e = nlmsg_data(nlh); 1028 e->error = -EMSGSIZE; 1029 memset(&e->msg, 0, sizeof(e->msg)); 1030 } 1031 1032 rtnl_unicast(skb, net, NETLINK_CB(skb).portid); 1033 } else { 1034 ip_mr_forward(net, mrt, skb->dev, skb, c, 0); 1035 } 1036 } 1037 } 1038 1039 /* Bounce a cache query up to mrouted and netlink. 1040 * 1041 * Called under mrt_lock. 1042 */ 1043 static int ipmr_cache_report(struct mr_table *mrt, 1044 struct sk_buff *pkt, vifi_t vifi, int assert) 1045 { 1046 const int ihl = ip_hdrlen(pkt); 1047 struct sock *mroute_sk; 1048 struct igmphdr *igmp; 1049 struct igmpmsg *msg; 1050 struct sk_buff *skb; 1051 int ret; 1052 1053 if (assert == IGMPMSG_WHOLEPKT || assert == IGMPMSG_WRVIFWHOLE) 1054 skb = skb_realloc_headroom(pkt, sizeof(struct iphdr)); 1055 else 1056 skb = alloc_skb(128, GFP_ATOMIC); 1057 1058 if (!skb) 1059 return -ENOBUFS; 1060 1061 if (assert == IGMPMSG_WHOLEPKT || assert == IGMPMSG_WRVIFWHOLE) { 1062 /* Ugly, but we have no choice with this interface. 1063 * Duplicate old header, fix ihl, length etc. 1064 * And all this only to mangle msg->im_msgtype and 1065 * to set msg->im_mbz to "mbz" :-) 1066 */ 1067 skb_push(skb, sizeof(struct iphdr)); 1068 skb_reset_network_header(skb); 1069 skb_reset_transport_header(skb); 1070 msg = (struct igmpmsg *)skb_network_header(skb); 1071 memcpy(msg, skb_network_header(pkt), sizeof(struct iphdr)); 1072 msg->im_msgtype = assert; 1073 msg->im_mbz = 0; 1074 if (assert == IGMPMSG_WRVIFWHOLE) 1075 msg->im_vif = vifi; 1076 else 1077 msg->im_vif = mrt->mroute_reg_vif_num; 1078 ip_hdr(skb)->ihl = sizeof(struct iphdr) >> 2; 1079 ip_hdr(skb)->tot_len = htons(ntohs(ip_hdr(pkt)->tot_len) + 1080 sizeof(struct iphdr)); 1081 } else { 1082 /* Copy the IP header */ 1083 skb_set_network_header(skb, skb->len); 1084 skb_put(skb, ihl); 1085 skb_copy_to_linear_data(skb, pkt->data, ihl); 1086 /* Flag to the kernel this is a route add */ 1087 ip_hdr(skb)->protocol = 0; 1088 msg = (struct igmpmsg *)skb_network_header(skb); 1089 msg->im_vif = vifi; 1090 skb_dst_set(skb, dst_clone(skb_dst(pkt))); 1091 /* Add our header */ 1092 igmp = skb_put(skb, sizeof(struct igmphdr)); 1093 igmp->type = assert; 1094 msg->im_msgtype = assert; 1095 igmp->code = 0; 1096 ip_hdr(skb)->tot_len = htons(skb->len); /* Fix the length */ 1097 skb->transport_header = skb->network_header; 1098 } 1099 1100 rcu_read_lock(); 1101 mroute_sk = rcu_dereference(mrt->mroute_sk); 1102 if (!mroute_sk) { 1103 rcu_read_unlock(); 1104 kfree_skb(skb); 1105 return -EINVAL; 1106 } 1107 1108 igmpmsg_netlink_event(mrt, skb); 1109 1110 /* Deliver to mrouted */ 1111 ret = sock_queue_rcv_skb(mroute_sk, skb); 1112 rcu_read_unlock(); 1113 if (ret < 0) { 1114 net_warn_ratelimited("mroute: pending queue full, dropping entries\n"); 1115 kfree_skb(skb); 1116 } 1117 1118 return ret; 1119 } 1120 1121 /* Queue a packet for resolution. It gets locked cache entry! */ 1122 static int ipmr_cache_unresolved(struct mr_table *mrt, vifi_t vifi, 1123 struct sk_buff *skb, struct net_device *dev) 1124 { 1125 const struct iphdr *iph = ip_hdr(skb); 1126 struct mfc_cache *c; 1127 bool found = false; 1128 int err; 1129 1130 spin_lock_bh(&mfc_unres_lock); 1131 list_for_each_entry(c, &mrt->mfc_unres_queue, _c.list) { 1132 if (c->mfc_mcastgrp == iph->daddr && 1133 c->mfc_origin == iph->saddr) { 1134 found = true; 1135 break; 1136 } 1137 } 1138 1139 if (!found) { 1140 /* Create a new entry if allowable */ 1141 c = ipmr_cache_alloc_unres(); 1142 if (!c) { 1143 spin_unlock_bh(&mfc_unres_lock); 1144 1145 kfree_skb(skb); 1146 return -ENOBUFS; 1147 } 1148 1149 /* Fill in the new cache entry */ 1150 c->_c.mfc_parent = -1; 1151 c->mfc_origin = iph->saddr; 1152 c->mfc_mcastgrp = iph->daddr; 1153 1154 /* Reflect first query at mrouted. */ 1155 err = ipmr_cache_report(mrt, skb, vifi, IGMPMSG_NOCACHE); 1156 1157 if (err < 0) { 1158 /* If the report failed throw the cache entry 1159 out - Brad Parker 1160 */ 1161 spin_unlock_bh(&mfc_unres_lock); 1162 1163 ipmr_cache_free(c); 1164 kfree_skb(skb); 1165 return err; 1166 } 1167 1168 atomic_inc(&mrt->cache_resolve_queue_len); 1169 list_add(&c->_c.list, &mrt->mfc_unres_queue); 1170 mroute_netlink_event(mrt, c, RTM_NEWROUTE); 1171 1172 if (atomic_read(&mrt->cache_resolve_queue_len) == 1) 1173 mod_timer(&mrt->ipmr_expire_timer, 1174 c->_c.mfc_un.unres.expires); 1175 } 1176 1177 /* See if we can append the packet */ 1178 if (c->_c.mfc_un.unres.unresolved.qlen > 3) { 1179 kfree_skb(skb); 1180 err = -ENOBUFS; 1181 } else { 1182 if (dev) { 1183 skb->dev = dev; 1184 skb->skb_iif = dev->ifindex; 1185 } 1186 skb_queue_tail(&c->_c.mfc_un.unres.unresolved, skb); 1187 err = 0; 1188 } 1189 1190 spin_unlock_bh(&mfc_unres_lock); 1191 return err; 1192 } 1193 1194 /* MFC cache manipulation by user space mroute daemon */ 1195 1196 static int ipmr_mfc_delete(struct mr_table *mrt, struct mfcctl *mfc, int parent) 1197 { 1198 struct net *net = read_pnet(&mrt->net); 1199 struct mfc_cache *c; 1200 1201 /* The entries are added/deleted only under RTNL */ 1202 rcu_read_lock(); 1203 c = ipmr_cache_find_parent(mrt, mfc->mfcc_origin.s_addr, 1204 mfc->mfcc_mcastgrp.s_addr, parent); 1205 rcu_read_unlock(); 1206 if (!c) 1207 return -ENOENT; 1208 rhltable_remove(&mrt->mfc_hash, &c->_c.mnode, ipmr_rht_params); 1209 list_del_rcu(&c->_c.list); 1210 call_ipmr_mfc_entry_notifiers(net, FIB_EVENT_ENTRY_DEL, c, mrt->id); 1211 mroute_netlink_event(mrt, c, RTM_DELROUTE); 1212 mr_cache_put(&c->_c); 1213 1214 return 0; 1215 } 1216 1217 static int ipmr_mfc_add(struct net *net, struct mr_table *mrt, 1218 struct mfcctl *mfc, int mrtsock, int parent) 1219 { 1220 struct mfc_cache *uc, *c; 1221 struct mr_mfc *_uc; 1222 bool found; 1223 int ret; 1224 1225 if (mfc->mfcc_parent >= MAXVIFS) 1226 return -ENFILE; 1227 1228 /* The entries are added/deleted only under RTNL */ 1229 rcu_read_lock(); 1230 c = ipmr_cache_find_parent(mrt, mfc->mfcc_origin.s_addr, 1231 mfc->mfcc_mcastgrp.s_addr, parent); 1232 rcu_read_unlock(); 1233 if (c) { 1234 write_lock_bh(&mrt_lock); 1235 c->_c.mfc_parent = mfc->mfcc_parent; 1236 ipmr_update_thresholds(mrt, &c->_c, mfc->mfcc_ttls); 1237 if (!mrtsock) 1238 c->_c.mfc_flags |= MFC_STATIC; 1239 write_unlock_bh(&mrt_lock); 1240 call_ipmr_mfc_entry_notifiers(net, FIB_EVENT_ENTRY_REPLACE, c, 1241 mrt->id); 1242 mroute_netlink_event(mrt, c, RTM_NEWROUTE); 1243 return 0; 1244 } 1245 1246 if (mfc->mfcc_mcastgrp.s_addr != htonl(INADDR_ANY) && 1247 !ipv4_is_multicast(mfc->mfcc_mcastgrp.s_addr)) 1248 return -EINVAL; 1249 1250 c = ipmr_cache_alloc(); 1251 if (!c) 1252 return -ENOMEM; 1253 1254 c->mfc_origin = mfc->mfcc_origin.s_addr; 1255 c->mfc_mcastgrp = mfc->mfcc_mcastgrp.s_addr; 1256 c->_c.mfc_parent = mfc->mfcc_parent; 1257 ipmr_update_thresholds(mrt, &c->_c, mfc->mfcc_ttls); 1258 if (!mrtsock) 1259 c->_c.mfc_flags |= MFC_STATIC; 1260 1261 ret = rhltable_insert_key(&mrt->mfc_hash, &c->cmparg, &c->_c.mnode, 1262 ipmr_rht_params); 1263 if (ret) { 1264 pr_err("ipmr: rhtable insert error %d\n", ret); 1265 ipmr_cache_free(c); 1266 return ret; 1267 } 1268 list_add_tail_rcu(&c->_c.list, &mrt->mfc_cache_list); 1269 /* Check to see if we resolved a queued list. If so we 1270 * need to send on the frames and tidy up. 1271 */ 1272 found = false; 1273 spin_lock_bh(&mfc_unres_lock); 1274 list_for_each_entry(_uc, &mrt->mfc_unres_queue, list) { 1275 uc = (struct mfc_cache *)_uc; 1276 if (uc->mfc_origin == c->mfc_origin && 1277 uc->mfc_mcastgrp == c->mfc_mcastgrp) { 1278 list_del(&_uc->list); 1279 atomic_dec(&mrt->cache_resolve_queue_len); 1280 found = true; 1281 break; 1282 } 1283 } 1284 if (list_empty(&mrt->mfc_unres_queue)) 1285 del_timer(&mrt->ipmr_expire_timer); 1286 spin_unlock_bh(&mfc_unres_lock); 1287 1288 if (found) { 1289 ipmr_cache_resolve(net, mrt, uc, c); 1290 ipmr_cache_free(uc); 1291 } 1292 call_ipmr_mfc_entry_notifiers(net, FIB_EVENT_ENTRY_ADD, c, mrt->id); 1293 mroute_netlink_event(mrt, c, RTM_NEWROUTE); 1294 return 0; 1295 } 1296 1297 /* Close the multicast socket, and clear the vif tables etc */ 1298 static void mroute_clean_tables(struct mr_table *mrt, int flags) 1299 { 1300 struct net *net = read_pnet(&mrt->net); 1301 struct mr_mfc *c, *tmp; 1302 struct mfc_cache *cache; 1303 LIST_HEAD(list); 1304 int i; 1305 1306 /* Shut down all active vif entries */ 1307 if (flags & (MRT_FLUSH_VIFS | MRT_FLUSH_VIFS_STATIC)) { 1308 for (i = 0; i < mrt->maxvif; i++) { 1309 if (((mrt->vif_table[i].flags & VIFF_STATIC) && 1310 !(flags & MRT_FLUSH_VIFS_STATIC)) || 1311 (!(mrt->vif_table[i].flags & VIFF_STATIC) && !(flags & MRT_FLUSH_VIFS))) 1312 continue; 1313 vif_delete(mrt, i, 0, &list); 1314 } 1315 unregister_netdevice_many(&list); 1316 } 1317 1318 /* Wipe the cache */ 1319 if (flags & (MRT_FLUSH_MFC | MRT_FLUSH_MFC_STATIC)) { 1320 list_for_each_entry_safe(c, tmp, &mrt->mfc_cache_list, list) { 1321 if (((c->mfc_flags & MFC_STATIC) && !(flags & MRT_FLUSH_MFC_STATIC)) || 1322 (!(c->mfc_flags & MFC_STATIC) && !(flags & MRT_FLUSH_MFC))) 1323 continue; 1324 rhltable_remove(&mrt->mfc_hash, &c->mnode, ipmr_rht_params); 1325 list_del_rcu(&c->list); 1326 cache = (struct mfc_cache *)c; 1327 call_ipmr_mfc_entry_notifiers(net, FIB_EVENT_ENTRY_DEL, cache, 1328 mrt->id); 1329 mroute_netlink_event(mrt, cache, RTM_DELROUTE); 1330 mr_cache_put(c); 1331 } 1332 } 1333 1334 if (flags & MRT_FLUSH_MFC) { 1335 if (atomic_read(&mrt->cache_resolve_queue_len) != 0) { 1336 spin_lock_bh(&mfc_unres_lock); 1337 list_for_each_entry_safe(c, tmp, &mrt->mfc_unres_queue, list) { 1338 list_del(&c->list); 1339 cache = (struct mfc_cache *)c; 1340 mroute_netlink_event(mrt, cache, RTM_DELROUTE); 1341 ipmr_destroy_unres(mrt, cache); 1342 } 1343 spin_unlock_bh(&mfc_unres_lock); 1344 } 1345 } 1346 } 1347 1348 /* called from ip_ra_control(), before an RCU grace period, 1349 * we dont need to call synchronize_rcu() here 1350 */ 1351 static void mrtsock_destruct(struct sock *sk) 1352 { 1353 struct net *net = sock_net(sk); 1354 struct mr_table *mrt; 1355 1356 rtnl_lock(); 1357 ipmr_for_each_table(mrt, net) { 1358 if (sk == rtnl_dereference(mrt->mroute_sk)) { 1359 IPV4_DEVCONF_ALL(net, MC_FORWARDING)--; 1360 inet_netconf_notify_devconf(net, RTM_NEWNETCONF, 1361 NETCONFA_MC_FORWARDING, 1362 NETCONFA_IFINDEX_ALL, 1363 net->ipv4.devconf_all); 1364 RCU_INIT_POINTER(mrt->mroute_sk, NULL); 1365 mroute_clean_tables(mrt, MRT_FLUSH_VIFS | MRT_FLUSH_MFC); 1366 } 1367 } 1368 rtnl_unlock(); 1369 } 1370 1371 /* Socket options and virtual interface manipulation. The whole 1372 * virtual interface system is a complete heap, but unfortunately 1373 * that's how BSD mrouted happens to think. Maybe one day with a proper 1374 * MOSPF/PIM router set up we can clean this up. 1375 */ 1376 1377 int ip_mroute_setsockopt(struct sock *sk, int optname, char __user *optval, 1378 unsigned int optlen) 1379 { 1380 struct net *net = sock_net(sk); 1381 int val, ret = 0, parent = 0; 1382 struct mr_table *mrt; 1383 struct vifctl vif; 1384 struct mfcctl mfc; 1385 bool do_wrvifwhole; 1386 u32 uval; 1387 1388 /* There's one exception to the lock - MRT_DONE which needs to unlock */ 1389 rtnl_lock(); 1390 if (sk->sk_type != SOCK_RAW || 1391 inet_sk(sk)->inet_num != IPPROTO_IGMP) { 1392 ret = -EOPNOTSUPP; 1393 goto out_unlock; 1394 } 1395 1396 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT); 1397 if (!mrt) { 1398 ret = -ENOENT; 1399 goto out_unlock; 1400 } 1401 if (optname != MRT_INIT) { 1402 if (sk != rcu_access_pointer(mrt->mroute_sk) && 1403 !ns_capable(net->user_ns, CAP_NET_ADMIN)) { 1404 ret = -EACCES; 1405 goto out_unlock; 1406 } 1407 } 1408 1409 switch (optname) { 1410 case MRT_INIT: 1411 if (optlen != sizeof(int)) { 1412 ret = -EINVAL; 1413 break; 1414 } 1415 if (rtnl_dereference(mrt->mroute_sk)) { 1416 ret = -EADDRINUSE; 1417 break; 1418 } 1419 1420 ret = ip_ra_control(sk, 1, mrtsock_destruct); 1421 if (ret == 0) { 1422 rcu_assign_pointer(mrt->mroute_sk, sk); 1423 IPV4_DEVCONF_ALL(net, MC_FORWARDING)++; 1424 inet_netconf_notify_devconf(net, RTM_NEWNETCONF, 1425 NETCONFA_MC_FORWARDING, 1426 NETCONFA_IFINDEX_ALL, 1427 net->ipv4.devconf_all); 1428 } 1429 break; 1430 case MRT_DONE: 1431 if (sk != rcu_access_pointer(mrt->mroute_sk)) { 1432 ret = -EACCES; 1433 } else { 1434 /* We need to unlock here because mrtsock_destruct takes 1435 * care of rtnl itself and we can't change that due to 1436 * the IP_ROUTER_ALERT setsockopt which runs without it. 1437 */ 1438 rtnl_unlock(); 1439 ret = ip_ra_control(sk, 0, NULL); 1440 goto out; 1441 } 1442 break; 1443 case MRT_ADD_VIF: 1444 case MRT_DEL_VIF: 1445 if (optlen != sizeof(vif)) { 1446 ret = -EINVAL; 1447 break; 1448 } 1449 if (copy_from_user(&vif, optval, sizeof(vif))) { 1450 ret = -EFAULT; 1451 break; 1452 } 1453 if (vif.vifc_vifi >= MAXVIFS) { 1454 ret = -ENFILE; 1455 break; 1456 } 1457 if (optname == MRT_ADD_VIF) { 1458 ret = vif_add(net, mrt, &vif, 1459 sk == rtnl_dereference(mrt->mroute_sk)); 1460 } else { 1461 ret = vif_delete(mrt, vif.vifc_vifi, 0, NULL); 1462 } 1463 break; 1464 /* Manipulate the forwarding caches. These live 1465 * in a sort of kernel/user symbiosis. 1466 */ 1467 case MRT_ADD_MFC: 1468 case MRT_DEL_MFC: 1469 parent = -1; 1470 fallthrough; 1471 case MRT_ADD_MFC_PROXY: 1472 case MRT_DEL_MFC_PROXY: 1473 if (optlen != sizeof(mfc)) { 1474 ret = -EINVAL; 1475 break; 1476 } 1477 if (copy_from_user(&mfc, optval, sizeof(mfc))) { 1478 ret = -EFAULT; 1479 break; 1480 } 1481 if (parent == 0) 1482 parent = mfc.mfcc_parent; 1483 if (optname == MRT_DEL_MFC || optname == MRT_DEL_MFC_PROXY) 1484 ret = ipmr_mfc_delete(mrt, &mfc, parent); 1485 else 1486 ret = ipmr_mfc_add(net, mrt, &mfc, 1487 sk == rtnl_dereference(mrt->mroute_sk), 1488 parent); 1489 break; 1490 case MRT_FLUSH: 1491 if (optlen != sizeof(val)) { 1492 ret = -EINVAL; 1493 break; 1494 } 1495 if (get_user(val, (int __user *)optval)) { 1496 ret = -EFAULT; 1497 break; 1498 } 1499 mroute_clean_tables(mrt, val); 1500 break; 1501 /* Control PIM assert. */ 1502 case MRT_ASSERT: 1503 if (optlen != sizeof(val)) { 1504 ret = -EINVAL; 1505 break; 1506 } 1507 if (get_user(val, (int __user *)optval)) { 1508 ret = -EFAULT; 1509 break; 1510 } 1511 mrt->mroute_do_assert = val; 1512 break; 1513 case MRT_PIM: 1514 if (!ipmr_pimsm_enabled()) { 1515 ret = -ENOPROTOOPT; 1516 break; 1517 } 1518 if (optlen != sizeof(val)) { 1519 ret = -EINVAL; 1520 break; 1521 } 1522 if (get_user(val, (int __user *)optval)) { 1523 ret = -EFAULT; 1524 break; 1525 } 1526 1527 do_wrvifwhole = (val == IGMPMSG_WRVIFWHOLE); 1528 val = !!val; 1529 if (val != mrt->mroute_do_pim) { 1530 mrt->mroute_do_pim = val; 1531 mrt->mroute_do_assert = val; 1532 mrt->mroute_do_wrvifwhole = do_wrvifwhole; 1533 } 1534 break; 1535 case MRT_TABLE: 1536 if (!IS_BUILTIN(CONFIG_IP_MROUTE_MULTIPLE_TABLES)) { 1537 ret = -ENOPROTOOPT; 1538 break; 1539 } 1540 if (optlen != sizeof(uval)) { 1541 ret = -EINVAL; 1542 break; 1543 } 1544 if (get_user(uval, (u32 __user *)optval)) { 1545 ret = -EFAULT; 1546 break; 1547 } 1548 1549 if (sk == rtnl_dereference(mrt->mroute_sk)) { 1550 ret = -EBUSY; 1551 } else { 1552 mrt = ipmr_new_table(net, uval); 1553 if (IS_ERR(mrt)) 1554 ret = PTR_ERR(mrt); 1555 else 1556 raw_sk(sk)->ipmr_table = uval; 1557 } 1558 break; 1559 /* Spurious command, or MRT_VERSION which you cannot set. */ 1560 default: 1561 ret = -ENOPROTOOPT; 1562 } 1563 out_unlock: 1564 rtnl_unlock(); 1565 out: 1566 return ret; 1567 } 1568 1569 /* Getsock opt support for the multicast routing system. */ 1570 int ip_mroute_getsockopt(struct sock *sk, int optname, char __user *optval, int __user *optlen) 1571 { 1572 int olr; 1573 int val; 1574 struct net *net = sock_net(sk); 1575 struct mr_table *mrt; 1576 1577 if (sk->sk_type != SOCK_RAW || 1578 inet_sk(sk)->inet_num != IPPROTO_IGMP) 1579 return -EOPNOTSUPP; 1580 1581 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT); 1582 if (!mrt) 1583 return -ENOENT; 1584 1585 switch (optname) { 1586 case MRT_VERSION: 1587 val = 0x0305; 1588 break; 1589 case MRT_PIM: 1590 if (!ipmr_pimsm_enabled()) 1591 return -ENOPROTOOPT; 1592 val = mrt->mroute_do_pim; 1593 break; 1594 case MRT_ASSERT: 1595 val = mrt->mroute_do_assert; 1596 break; 1597 default: 1598 return -ENOPROTOOPT; 1599 } 1600 1601 if (get_user(olr, optlen)) 1602 return -EFAULT; 1603 olr = min_t(unsigned int, olr, sizeof(int)); 1604 if (olr < 0) 1605 return -EINVAL; 1606 if (put_user(olr, optlen)) 1607 return -EFAULT; 1608 if (copy_to_user(optval, &val, olr)) 1609 return -EFAULT; 1610 return 0; 1611 } 1612 1613 /* The IP multicast ioctl support routines. */ 1614 int ipmr_ioctl(struct sock *sk, int cmd, void __user *arg) 1615 { 1616 struct sioc_sg_req sr; 1617 struct sioc_vif_req vr; 1618 struct vif_device *vif; 1619 struct mfc_cache *c; 1620 struct net *net = sock_net(sk); 1621 struct mr_table *mrt; 1622 1623 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT); 1624 if (!mrt) 1625 return -ENOENT; 1626 1627 switch (cmd) { 1628 case SIOCGETVIFCNT: 1629 if (copy_from_user(&vr, arg, sizeof(vr))) 1630 return -EFAULT; 1631 if (vr.vifi >= mrt->maxvif) 1632 return -EINVAL; 1633 vr.vifi = array_index_nospec(vr.vifi, mrt->maxvif); 1634 read_lock(&mrt_lock); 1635 vif = &mrt->vif_table[vr.vifi]; 1636 if (VIF_EXISTS(mrt, vr.vifi)) { 1637 vr.icount = vif->pkt_in; 1638 vr.ocount = vif->pkt_out; 1639 vr.ibytes = vif->bytes_in; 1640 vr.obytes = vif->bytes_out; 1641 read_unlock(&mrt_lock); 1642 1643 if (copy_to_user(arg, &vr, sizeof(vr))) 1644 return -EFAULT; 1645 return 0; 1646 } 1647 read_unlock(&mrt_lock); 1648 return -EADDRNOTAVAIL; 1649 case SIOCGETSGCNT: 1650 if (copy_from_user(&sr, arg, sizeof(sr))) 1651 return -EFAULT; 1652 1653 rcu_read_lock(); 1654 c = ipmr_cache_find(mrt, sr.src.s_addr, sr.grp.s_addr); 1655 if (c) { 1656 sr.pktcnt = c->_c.mfc_un.res.pkt; 1657 sr.bytecnt = c->_c.mfc_un.res.bytes; 1658 sr.wrong_if = c->_c.mfc_un.res.wrong_if; 1659 rcu_read_unlock(); 1660 1661 if (copy_to_user(arg, &sr, sizeof(sr))) 1662 return -EFAULT; 1663 return 0; 1664 } 1665 rcu_read_unlock(); 1666 return -EADDRNOTAVAIL; 1667 default: 1668 return -ENOIOCTLCMD; 1669 } 1670 } 1671 1672 #ifdef CONFIG_COMPAT 1673 struct compat_sioc_sg_req { 1674 struct in_addr src; 1675 struct in_addr grp; 1676 compat_ulong_t pktcnt; 1677 compat_ulong_t bytecnt; 1678 compat_ulong_t wrong_if; 1679 }; 1680 1681 struct compat_sioc_vif_req { 1682 vifi_t vifi; /* Which iface */ 1683 compat_ulong_t icount; 1684 compat_ulong_t ocount; 1685 compat_ulong_t ibytes; 1686 compat_ulong_t obytes; 1687 }; 1688 1689 int ipmr_compat_ioctl(struct sock *sk, unsigned int cmd, void __user *arg) 1690 { 1691 struct compat_sioc_sg_req sr; 1692 struct compat_sioc_vif_req vr; 1693 struct vif_device *vif; 1694 struct mfc_cache *c; 1695 struct net *net = sock_net(sk); 1696 struct mr_table *mrt; 1697 1698 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT); 1699 if (!mrt) 1700 return -ENOENT; 1701 1702 switch (cmd) { 1703 case SIOCGETVIFCNT: 1704 if (copy_from_user(&vr, arg, sizeof(vr))) 1705 return -EFAULT; 1706 if (vr.vifi >= mrt->maxvif) 1707 return -EINVAL; 1708 vr.vifi = array_index_nospec(vr.vifi, mrt->maxvif); 1709 read_lock(&mrt_lock); 1710 vif = &mrt->vif_table[vr.vifi]; 1711 if (VIF_EXISTS(mrt, vr.vifi)) { 1712 vr.icount = vif->pkt_in; 1713 vr.ocount = vif->pkt_out; 1714 vr.ibytes = vif->bytes_in; 1715 vr.obytes = vif->bytes_out; 1716 read_unlock(&mrt_lock); 1717 1718 if (copy_to_user(arg, &vr, sizeof(vr))) 1719 return -EFAULT; 1720 return 0; 1721 } 1722 read_unlock(&mrt_lock); 1723 return -EADDRNOTAVAIL; 1724 case SIOCGETSGCNT: 1725 if (copy_from_user(&sr, arg, sizeof(sr))) 1726 return -EFAULT; 1727 1728 rcu_read_lock(); 1729 c = ipmr_cache_find(mrt, sr.src.s_addr, sr.grp.s_addr); 1730 if (c) { 1731 sr.pktcnt = c->_c.mfc_un.res.pkt; 1732 sr.bytecnt = c->_c.mfc_un.res.bytes; 1733 sr.wrong_if = c->_c.mfc_un.res.wrong_if; 1734 rcu_read_unlock(); 1735 1736 if (copy_to_user(arg, &sr, sizeof(sr))) 1737 return -EFAULT; 1738 return 0; 1739 } 1740 rcu_read_unlock(); 1741 return -EADDRNOTAVAIL; 1742 default: 1743 return -ENOIOCTLCMD; 1744 } 1745 } 1746 #endif 1747 1748 static int ipmr_device_event(struct notifier_block *this, unsigned long event, void *ptr) 1749 { 1750 struct net_device *dev = netdev_notifier_info_to_dev(ptr); 1751 struct net *net = dev_net(dev); 1752 struct mr_table *mrt; 1753 struct vif_device *v; 1754 int ct; 1755 1756 if (event != NETDEV_UNREGISTER) 1757 return NOTIFY_DONE; 1758 1759 ipmr_for_each_table(mrt, net) { 1760 v = &mrt->vif_table[0]; 1761 for (ct = 0; ct < mrt->maxvif; ct++, v++) { 1762 if (v->dev == dev) 1763 vif_delete(mrt, ct, 1, NULL); 1764 } 1765 } 1766 return NOTIFY_DONE; 1767 } 1768 1769 static struct notifier_block ip_mr_notifier = { 1770 .notifier_call = ipmr_device_event, 1771 }; 1772 1773 /* Encapsulate a packet by attaching a valid IPIP header to it. 1774 * This avoids tunnel drivers and other mess and gives us the speed so 1775 * important for multicast video. 1776 */ 1777 static void ip_encap(struct net *net, struct sk_buff *skb, 1778 __be32 saddr, __be32 daddr) 1779 { 1780 struct iphdr *iph; 1781 const struct iphdr *old_iph = ip_hdr(skb); 1782 1783 skb_push(skb, sizeof(struct iphdr)); 1784 skb->transport_header = skb->network_header; 1785 skb_reset_network_header(skb); 1786 iph = ip_hdr(skb); 1787 1788 iph->version = 4; 1789 iph->tos = old_iph->tos; 1790 iph->ttl = old_iph->ttl; 1791 iph->frag_off = 0; 1792 iph->daddr = daddr; 1793 iph->saddr = saddr; 1794 iph->protocol = IPPROTO_IPIP; 1795 iph->ihl = 5; 1796 iph->tot_len = htons(skb->len); 1797 ip_select_ident(net, skb, NULL); 1798 ip_send_check(iph); 1799 1800 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt)); 1801 nf_reset_ct(skb); 1802 } 1803 1804 static inline int ipmr_forward_finish(struct net *net, struct sock *sk, 1805 struct sk_buff *skb) 1806 { 1807 struct ip_options *opt = &(IPCB(skb)->opt); 1808 1809 IP_INC_STATS(net, IPSTATS_MIB_OUTFORWDATAGRAMS); 1810 IP_ADD_STATS(net, IPSTATS_MIB_OUTOCTETS, skb->len); 1811 1812 if (unlikely(opt->optlen)) 1813 ip_forward_options(skb); 1814 1815 return dst_output(net, sk, skb); 1816 } 1817 1818 #ifdef CONFIG_NET_SWITCHDEV 1819 static bool ipmr_forward_offloaded(struct sk_buff *skb, struct mr_table *mrt, 1820 int in_vifi, int out_vifi) 1821 { 1822 struct vif_device *out_vif = &mrt->vif_table[out_vifi]; 1823 struct vif_device *in_vif = &mrt->vif_table[in_vifi]; 1824 1825 if (!skb->offload_l3_fwd_mark) 1826 return false; 1827 if (!out_vif->dev_parent_id.id_len || !in_vif->dev_parent_id.id_len) 1828 return false; 1829 return netdev_phys_item_id_same(&out_vif->dev_parent_id, 1830 &in_vif->dev_parent_id); 1831 } 1832 #else 1833 static bool ipmr_forward_offloaded(struct sk_buff *skb, struct mr_table *mrt, 1834 int in_vifi, int out_vifi) 1835 { 1836 return false; 1837 } 1838 #endif 1839 1840 /* Processing handlers for ipmr_forward */ 1841 1842 static void ipmr_queue_xmit(struct net *net, struct mr_table *mrt, 1843 int in_vifi, struct sk_buff *skb, int vifi) 1844 { 1845 const struct iphdr *iph = ip_hdr(skb); 1846 struct vif_device *vif = &mrt->vif_table[vifi]; 1847 struct net_device *dev; 1848 struct rtable *rt; 1849 struct flowi4 fl4; 1850 int encap = 0; 1851 1852 if (!vif->dev) 1853 goto out_free; 1854 1855 if (vif->flags & VIFF_REGISTER) { 1856 vif->pkt_out++; 1857 vif->bytes_out += skb->len; 1858 vif->dev->stats.tx_bytes += skb->len; 1859 vif->dev->stats.tx_packets++; 1860 ipmr_cache_report(mrt, skb, vifi, IGMPMSG_WHOLEPKT); 1861 goto out_free; 1862 } 1863 1864 if (ipmr_forward_offloaded(skb, mrt, in_vifi, vifi)) 1865 goto out_free; 1866 1867 if (vif->flags & VIFF_TUNNEL) { 1868 rt = ip_route_output_ports(net, &fl4, NULL, 1869 vif->remote, vif->local, 1870 0, 0, 1871 IPPROTO_IPIP, 1872 RT_TOS(iph->tos), vif->link); 1873 if (IS_ERR(rt)) 1874 goto out_free; 1875 encap = sizeof(struct iphdr); 1876 } else { 1877 rt = ip_route_output_ports(net, &fl4, NULL, iph->daddr, 0, 1878 0, 0, 1879 IPPROTO_IPIP, 1880 RT_TOS(iph->tos), vif->link); 1881 if (IS_ERR(rt)) 1882 goto out_free; 1883 } 1884 1885 dev = rt->dst.dev; 1886 1887 if (skb->len+encap > dst_mtu(&rt->dst) && (ntohs(iph->frag_off) & IP_DF)) { 1888 /* Do not fragment multicasts. Alas, IPv4 does not 1889 * allow to send ICMP, so that packets will disappear 1890 * to blackhole. 1891 */ 1892 IP_INC_STATS(net, IPSTATS_MIB_FRAGFAILS); 1893 ip_rt_put(rt); 1894 goto out_free; 1895 } 1896 1897 encap += LL_RESERVED_SPACE(dev) + rt->dst.header_len; 1898 1899 if (skb_cow(skb, encap)) { 1900 ip_rt_put(rt); 1901 goto out_free; 1902 } 1903 1904 vif->pkt_out++; 1905 vif->bytes_out += skb->len; 1906 1907 skb_dst_drop(skb); 1908 skb_dst_set(skb, &rt->dst); 1909 ip_decrease_ttl(ip_hdr(skb)); 1910 1911 /* FIXME: forward and output firewalls used to be called here. 1912 * What do we do with netfilter? -- RR 1913 */ 1914 if (vif->flags & VIFF_TUNNEL) { 1915 ip_encap(net, skb, vif->local, vif->remote); 1916 /* FIXME: extra output firewall step used to be here. --RR */ 1917 vif->dev->stats.tx_packets++; 1918 vif->dev->stats.tx_bytes += skb->len; 1919 } 1920 1921 IPCB(skb)->flags |= IPSKB_FORWARDED; 1922 1923 /* RFC1584 teaches, that DVMRP/PIM router must deliver packets locally 1924 * not only before forwarding, but after forwarding on all output 1925 * interfaces. It is clear, if mrouter runs a multicasting 1926 * program, it should receive packets not depending to what interface 1927 * program is joined. 1928 * If we will not make it, the program will have to join on all 1929 * interfaces. On the other hand, multihoming host (or router, but 1930 * not mrouter) cannot join to more than one interface - it will 1931 * result in receiving multiple packets. 1932 */ 1933 NF_HOOK(NFPROTO_IPV4, NF_INET_FORWARD, 1934 net, NULL, skb, skb->dev, dev, 1935 ipmr_forward_finish); 1936 return; 1937 1938 out_free: 1939 kfree_skb(skb); 1940 } 1941 1942 static int ipmr_find_vif(struct mr_table *mrt, struct net_device *dev) 1943 { 1944 int ct; 1945 1946 for (ct = mrt->maxvif-1; ct >= 0; ct--) { 1947 if (mrt->vif_table[ct].dev == dev) 1948 break; 1949 } 1950 return ct; 1951 } 1952 1953 /* "local" means that we should preserve one skb (for local delivery) */ 1954 static void ip_mr_forward(struct net *net, struct mr_table *mrt, 1955 struct net_device *dev, struct sk_buff *skb, 1956 struct mfc_cache *c, int local) 1957 { 1958 int true_vifi = ipmr_find_vif(mrt, dev); 1959 int psend = -1; 1960 int vif, ct; 1961 1962 vif = c->_c.mfc_parent; 1963 c->_c.mfc_un.res.pkt++; 1964 c->_c.mfc_un.res.bytes += skb->len; 1965 c->_c.mfc_un.res.lastuse = jiffies; 1966 1967 if (c->mfc_origin == htonl(INADDR_ANY) && true_vifi >= 0) { 1968 struct mfc_cache *cache_proxy; 1969 1970 /* For an (*,G) entry, we only check that the incomming 1971 * interface is part of the static tree. 1972 */ 1973 cache_proxy = mr_mfc_find_any_parent(mrt, vif); 1974 if (cache_proxy && 1975 cache_proxy->_c.mfc_un.res.ttls[true_vifi] < 255) 1976 goto forward; 1977 } 1978 1979 /* Wrong interface: drop packet and (maybe) send PIM assert. */ 1980 if (mrt->vif_table[vif].dev != dev) { 1981 if (rt_is_output_route(skb_rtable(skb))) { 1982 /* It is our own packet, looped back. 1983 * Very complicated situation... 1984 * 1985 * The best workaround until routing daemons will be 1986 * fixed is not to redistribute packet, if it was 1987 * send through wrong interface. It means, that 1988 * multicast applications WILL NOT work for 1989 * (S,G), which have default multicast route pointing 1990 * to wrong oif. In any case, it is not a good 1991 * idea to use multicasting applications on router. 1992 */ 1993 goto dont_forward; 1994 } 1995 1996 c->_c.mfc_un.res.wrong_if++; 1997 1998 if (true_vifi >= 0 && mrt->mroute_do_assert && 1999 /* pimsm uses asserts, when switching from RPT to SPT, 2000 * so that we cannot check that packet arrived on an oif. 2001 * It is bad, but otherwise we would need to move pretty 2002 * large chunk of pimd to kernel. Ough... --ANK 2003 */ 2004 (mrt->mroute_do_pim || 2005 c->_c.mfc_un.res.ttls[true_vifi] < 255) && 2006 time_after(jiffies, 2007 c->_c.mfc_un.res.last_assert + 2008 MFC_ASSERT_THRESH)) { 2009 c->_c.mfc_un.res.last_assert = jiffies; 2010 ipmr_cache_report(mrt, skb, true_vifi, IGMPMSG_WRONGVIF); 2011 if (mrt->mroute_do_wrvifwhole) 2012 ipmr_cache_report(mrt, skb, true_vifi, 2013 IGMPMSG_WRVIFWHOLE); 2014 } 2015 goto dont_forward; 2016 } 2017 2018 forward: 2019 mrt->vif_table[vif].pkt_in++; 2020 mrt->vif_table[vif].bytes_in += skb->len; 2021 2022 /* Forward the frame */ 2023 if (c->mfc_origin == htonl(INADDR_ANY) && 2024 c->mfc_mcastgrp == htonl(INADDR_ANY)) { 2025 if (true_vifi >= 0 && 2026 true_vifi != c->_c.mfc_parent && 2027 ip_hdr(skb)->ttl > 2028 c->_c.mfc_un.res.ttls[c->_c.mfc_parent]) { 2029 /* It's an (*,*) entry and the packet is not coming from 2030 * the upstream: forward the packet to the upstream 2031 * only. 2032 */ 2033 psend = c->_c.mfc_parent; 2034 goto last_forward; 2035 } 2036 goto dont_forward; 2037 } 2038 for (ct = c->_c.mfc_un.res.maxvif - 1; 2039 ct >= c->_c.mfc_un.res.minvif; ct--) { 2040 /* For (*,G) entry, don't forward to the incoming interface */ 2041 if ((c->mfc_origin != htonl(INADDR_ANY) || 2042 ct != true_vifi) && 2043 ip_hdr(skb)->ttl > c->_c.mfc_un.res.ttls[ct]) { 2044 if (psend != -1) { 2045 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC); 2046 2047 if (skb2) 2048 ipmr_queue_xmit(net, mrt, true_vifi, 2049 skb2, psend); 2050 } 2051 psend = ct; 2052 } 2053 } 2054 last_forward: 2055 if (psend != -1) { 2056 if (local) { 2057 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC); 2058 2059 if (skb2) 2060 ipmr_queue_xmit(net, mrt, true_vifi, skb2, 2061 psend); 2062 } else { 2063 ipmr_queue_xmit(net, mrt, true_vifi, skb, psend); 2064 return; 2065 } 2066 } 2067 2068 dont_forward: 2069 if (!local) 2070 kfree_skb(skb); 2071 } 2072 2073 static struct mr_table *ipmr_rt_fib_lookup(struct net *net, struct sk_buff *skb) 2074 { 2075 struct rtable *rt = skb_rtable(skb); 2076 struct iphdr *iph = ip_hdr(skb); 2077 struct flowi4 fl4 = { 2078 .daddr = iph->daddr, 2079 .saddr = iph->saddr, 2080 .flowi4_tos = RT_TOS(iph->tos), 2081 .flowi4_oif = (rt_is_output_route(rt) ? 2082 skb->dev->ifindex : 0), 2083 .flowi4_iif = (rt_is_output_route(rt) ? 2084 LOOPBACK_IFINDEX : 2085 skb->dev->ifindex), 2086 .flowi4_mark = skb->mark, 2087 }; 2088 struct mr_table *mrt; 2089 int err; 2090 2091 err = ipmr_fib_lookup(net, &fl4, &mrt); 2092 if (err) 2093 return ERR_PTR(err); 2094 return mrt; 2095 } 2096 2097 /* Multicast packets for forwarding arrive here 2098 * Called with rcu_read_lock(); 2099 */ 2100 int ip_mr_input(struct sk_buff *skb) 2101 { 2102 struct mfc_cache *cache; 2103 struct net *net = dev_net(skb->dev); 2104 int local = skb_rtable(skb)->rt_flags & RTCF_LOCAL; 2105 struct mr_table *mrt; 2106 struct net_device *dev; 2107 2108 /* skb->dev passed in is the loX master dev for vrfs. 2109 * As there are no vifs associated with loopback devices, 2110 * get the proper interface that does have a vif associated with it. 2111 */ 2112 dev = skb->dev; 2113 if (netif_is_l3_master(skb->dev)) { 2114 dev = dev_get_by_index_rcu(net, IPCB(skb)->iif); 2115 if (!dev) { 2116 kfree_skb(skb); 2117 return -ENODEV; 2118 } 2119 } 2120 2121 /* Packet is looped back after forward, it should not be 2122 * forwarded second time, but still can be delivered locally. 2123 */ 2124 if (IPCB(skb)->flags & IPSKB_FORWARDED) 2125 goto dont_forward; 2126 2127 mrt = ipmr_rt_fib_lookup(net, skb); 2128 if (IS_ERR(mrt)) { 2129 kfree_skb(skb); 2130 return PTR_ERR(mrt); 2131 } 2132 if (!local) { 2133 if (IPCB(skb)->opt.router_alert) { 2134 if (ip_call_ra_chain(skb)) 2135 return 0; 2136 } else if (ip_hdr(skb)->protocol == IPPROTO_IGMP) { 2137 /* IGMPv1 (and broken IGMPv2 implementations sort of 2138 * Cisco IOS <= 11.2(8)) do not put router alert 2139 * option to IGMP packets destined to routable 2140 * groups. It is very bad, because it means 2141 * that we can forward NO IGMP messages. 2142 */ 2143 struct sock *mroute_sk; 2144 2145 mroute_sk = rcu_dereference(mrt->mroute_sk); 2146 if (mroute_sk) { 2147 nf_reset_ct(skb); 2148 raw_rcv(mroute_sk, skb); 2149 return 0; 2150 } 2151 } 2152 } 2153 2154 /* already under rcu_read_lock() */ 2155 cache = ipmr_cache_find(mrt, ip_hdr(skb)->saddr, ip_hdr(skb)->daddr); 2156 if (!cache) { 2157 int vif = ipmr_find_vif(mrt, dev); 2158 2159 if (vif >= 0) 2160 cache = ipmr_cache_find_any(mrt, ip_hdr(skb)->daddr, 2161 vif); 2162 } 2163 2164 /* No usable cache entry */ 2165 if (!cache) { 2166 int vif; 2167 2168 if (local) { 2169 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC); 2170 ip_local_deliver(skb); 2171 if (!skb2) 2172 return -ENOBUFS; 2173 skb = skb2; 2174 } 2175 2176 read_lock(&mrt_lock); 2177 vif = ipmr_find_vif(mrt, dev); 2178 if (vif >= 0) { 2179 int err2 = ipmr_cache_unresolved(mrt, vif, skb, dev); 2180 read_unlock(&mrt_lock); 2181 2182 return err2; 2183 } 2184 read_unlock(&mrt_lock); 2185 kfree_skb(skb); 2186 return -ENODEV; 2187 } 2188 2189 read_lock(&mrt_lock); 2190 ip_mr_forward(net, mrt, dev, skb, cache, local); 2191 read_unlock(&mrt_lock); 2192 2193 if (local) 2194 return ip_local_deliver(skb); 2195 2196 return 0; 2197 2198 dont_forward: 2199 if (local) 2200 return ip_local_deliver(skb); 2201 kfree_skb(skb); 2202 return 0; 2203 } 2204 2205 #ifdef CONFIG_IP_PIMSM_V1 2206 /* Handle IGMP messages of PIMv1 */ 2207 int pim_rcv_v1(struct sk_buff *skb) 2208 { 2209 struct igmphdr *pim; 2210 struct net *net = dev_net(skb->dev); 2211 struct mr_table *mrt; 2212 2213 if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr))) 2214 goto drop; 2215 2216 pim = igmp_hdr(skb); 2217 2218 mrt = ipmr_rt_fib_lookup(net, skb); 2219 if (IS_ERR(mrt)) 2220 goto drop; 2221 if (!mrt->mroute_do_pim || 2222 pim->group != PIM_V1_VERSION || pim->code != PIM_V1_REGISTER) 2223 goto drop; 2224 2225 if (__pim_rcv(mrt, skb, sizeof(*pim))) { 2226 drop: 2227 kfree_skb(skb); 2228 } 2229 return 0; 2230 } 2231 #endif 2232 2233 #ifdef CONFIG_IP_PIMSM_V2 2234 static int pim_rcv(struct sk_buff *skb) 2235 { 2236 struct pimreghdr *pim; 2237 struct net *net = dev_net(skb->dev); 2238 struct mr_table *mrt; 2239 2240 if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr))) 2241 goto drop; 2242 2243 pim = (struct pimreghdr *)skb_transport_header(skb); 2244 if (pim->type != ((PIM_VERSION << 4) | (PIM_TYPE_REGISTER)) || 2245 (pim->flags & PIM_NULL_REGISTER) || 2246 (ip_compute_csum((void *)pim, sizeof(*pim)) != 0 && 2247 csum_fold(skb_checksum(skb, 0, skb->len, 0)))) 2248 goto drop; 2249 2250 mrt = ipmr_rt_fib_lookup(net, skb); 2251 if (IS_ERR(mrt)) 2252 goto drop; 2253 if (__pim_rcv(mrt, skb, sizeof(*pim))) { 2254 drop: 2255 kfree_skb(skb); 2256 } 2257 return 0; 2258 } 2259 #endif 2260 2261 int ipmr_get_route(struct net *net, struct sk_buff *skb, 2262 __be32 saddr, __be32 daddr, 2263 struct rtmsg *rtm, u32 portid) 2264 { 2265 struct mfc_cache *cache; 2266 struct mr_table *mrt; 2267 int err; 2268 2269 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT); 2270 if (!mrt) 2271 return -ENOENT; 2272 2273 rcu_read_lock(); 2274 cache = ipmr_cache_find(mrt, saddr, daddr); 2275 if (!cache && skb->dev) { 2276 int vif = ipmr_find_vif(mrt, skb->dev); 2277 2278 if (vif >= 0) 2279 cache = ipmr_cache_find_any(mrt, daddr, vif); 2280 } 2281 if (!cache) { 2282 struct sk_buff *skb2; 2283 struct iphdr *iph; 2284 struct net_device *dev; 2285 int vif = -1; 2286 2287 dev = skb->dev; 2288 read_lock(&mrt_lock); 2289 if (dev) 2290 vif = ipmr_find_vif(mrt, dev); 2291 if (vif < 0) { 2292 read_unlock(&mrt_lock); 2293 rcu_read_unlock(); 2294 return -ENODEV; 2295 } 2296 2297 skb2 = skb_realloc_headroom(skb, sizeof(struct iphdr)); 2298 if (!skb2) { 2299 read_unlock(&mrt_lock); 2300 rcu_read_unlock(); 2301 return -ENOMEM; 2302 } 2303 2304 NETLINK_CB(skb2).portid = portid; 2305 skb_push(skb2, sizeof(struct iphdr)); 2306 skb_reset_network_header(skb2); 2307 iph = ip_hdr(skb2); 2308 iph->ihl = sizeof(struct iphdr) >> 2; 2309 iph->saddr = saddr; 2310 iph->daddr = daddr; 2311 iph->version = 0; 2312 err = ipmr_cache_unresolved(mrt, vif, skb2, dev); 2313 read_unlock(&mrt_lock); 2314 rcu_read_unlock(); 2315 return err; 2316 } 2317 2318 read_lock(&mrt_lock); 2319 err = mr_fill_mroute(mrt, skb, &cache->_c, rtm); 2320 read_unlock(&mrt_lock); 2321 rcu_read_unlock(); 2322 return err; 2323 } 2324 2325 static int ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb, 2326 u32 portid, u32 seq, struct mfc_cache *c, int cmd, 2327 int flags) 2328 { 2329 struct nlmsghdr *nlh; 2330 struct rtmsg *rtm; 2331 int err; 2332 2333 nlh = nlmsg_put(skb, portid, seq, cmd, sizeof(*rtm), flags); 2334 if (!nlh) 2335 return -EMSGSIZE; 2336 2337 rtm = nlmsg_data(nlh); 2338 rtm->rtm_family = RTNL_FAMILY_IPMR; 2339 rtm->rtm_dst_len = 32; 2340 rtm->rtm_src_len = 32; 2341 rtm->rtm_tos = 0; 2342 rtm->rtm_table = mrt->id; 2343 if (nla_put_u32(skb, RTA_TABLE, mrt->id)) 2344 goto nla_put_failure; 2345 rtm->rtm_type = RTN_MULTICAST; 2346 rtm->rtm_scope = RT_SCOPE_UNIVERSE; 2347 if (c->_c.mfc_flags & MFC_STATIC) 2348 rtm->rtm_protocol = RTPROT_STATIC; 2349 else 2350 rtm->rtm_protocol = RTPROT_MROUTED; 2351 rtm->rtm_flags = 0; 2352 2353 if (nla_put_in_addr(skb, RTA_SRC, c->mfc_origin) || 2354 nla_put_in_addr(skb, RTA_DST, c->mfc_mcastgrp)) 2355 goto nla_put_failure; 2356 err = mr_fill_mroute(mrt, skb, &c->_c, rtm); 2357 /* do not break the dump if cache is unresolved */ 2358 if (err < 0 && err != -ENOENT) 2359 goto nla_put_failure; 2360 2361 nlmsg_end(skb, nlh); 2362 return 0; 2363 2364 nla_put_failure: 2365 nlmsg_cancel(skb, nlh); 2366 return -EMSGSIZE; 2367 } 2368 2369 static int _ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb, 2370 u32 portid, u32 seq, struct mr_mfc *c, int cmd, 2371 int flags) 2372 { 2373 return ipmr_fill_mroute(mrt, skb, portid, seq, (struct mfc_cache *)c, 2374 cmd, flags); 2375 } 2376 2377 static size_t mroute_msgsize(bool unresolved, int maxvif) 2378 { 2379 size_t len = 2380 NLMSG_ALIGN(sizeof(struct rtmsg)) 2381 + nla_total_size(4) /* RTA_TABLE */ 2382 + nla_total_size(4) /* RTA_SRC */ 2383 + nla_total_size(4) /* RTA_DST */ 2384 ; 2385 2386 if (!unresolved) 2387 len = len 2388 + nla_total_size(4) /* RTA_IIF */ 2389 + nla_total_size(0) /* RTA_MULTIPATH */ 2390 + maxvif * NLA_ALIGN(sizeof(struct rtnexthop)) 2391 /* RTA_MFC_STATS */ 2392 + nla_total_size_64bit(sizeof(struct rta_mfc_stats)) 2393 ; 2394 2395 return len; 2396 } 2397 2398 static void mroute_netlink_event(struct mr_table *mrt, struct mfc_cache *mfc, 2399 int cmd) 2400 { 2401 struct net *net = read_pnet(&mrt->net); 2402 struct sk_buff *skb; 2403 int err = -ENOBUFS; 2404 2405 skb = nlmsg_new(mroute_msgsize(mfc->_c.mfc_parent >= MAXVIFS, 2406 mrt->maxvif), 2407 GFP_ATOMIC); 2408 if (!skb) 2409 goto errout; 2410 2411 err = ipmr_fill_mroute(mrt, skb, 0, 0, mfc, cmd, 0); 2412 if (err < 0) 2413 goto errout; 2414 2415 rtnl_notify(skb, net, 0, RTNLGRP_IPV4_MROUTE, NULL, GFP_ATOMIC); 2416 return; 2417 2418 errout: 2419 kfree_skb(skb); 2420 if (err < 0) 2421 rtnl_set_sk_err(net, RTNLGRP_IPV4_MROUTE, err); 2422 } 2423 2424 static size_t igmpmsg_netlink_msgsize(size_t payloadlen) 2425 { 2426 size_t len = 2427 NLMSG_ALIGN(sizeof(struct rtgenmsg)) 2428 + nla_total_size(1) /* IPMRA_CREPORT_MSGTYPE */ 2429 + nla_total_size(4) /* IPMRA_CREPORT_VIF_ID */ 2430 + nla_total_size(4) /* IPMRA_CREPORT_SRC_ADDR */ 2431 + nla_total_size(4) /* IPMRA_CREPORT_DST_ADDR */ 2432 /* IPMRA_CREPORT_PKT */ 2433 + nla_total_size(payloadlen) 2434 ; 2435 2436 return len; 2437 } 2438 2439 static void igmpmsg_netlink_event(struct mr_table *mrt, struct sk_buff *pkt) 2440 { 2441 struct net *net = read_pnet(&mrt->net); 2442 struct nlmsghdr *nlh; 2443 struct rtgenmsg *rtgenm; 2444 struct igmpmsg *msg; 2445 struct sk_buff *skb; 2446 struct nlattr *nla; 2447 int payloadlen; 2448 2449 payloadlen = pkt->len - sizeof(struct igmpmsg); 2450 msg = (struct igmpmsg *)skb_network_header(pkt); 2451 2452 skb = nlmsg_new(igmpmsg_netlink_msgsize(payloadlen), GFP_ATOMIC); 2453 if (!skb) 2454 goto errout; 2455 2456 nlh = nlmsg_put(skb, 0, 0, RTM_NEWCACHEREPORT, 2457 sizeof(struct rtgenmsg), 0); 2458 if (!nlh) 2459 goto errout; 2460 rtgenm = nlmsg_data(nlh); 2461 rtgenm->rtgen_family = RTNL_FAMILY_IPMR; 2462 if (nla_put_u8(skb, IPMRA_CREPORT_MSGTYPE, msg->im_msgtype) || 2463 nla_put_u32(skb, IPMRA_CREPORT_VIF_ID, msg->im_vif) || 2464 nla_put_in_addr(skb, IPMRA_CREPORT_SRC_ADDR, 2465 msg->im_src.s_addr) || 2466 nla_put_in_addr(skb, IPMRA_CREPORT_DST_ADDR, 2467 msg->im_dst.s_addr)) 2468 goto nla_put_failure; 2469 2470 nla = nla_reserve(skb, IPMRA_CREPORT_PKT, payloadlen); 2471 if (!nla || skb_copy_bits(pkt, sizeof(struct igmpmsg), 2472 nla_data(nla), payloadlen)) 2473 goto nla_put_failure; 2474 2475 nlmsg_end(skb, nlh); 2476 2477 rtnl_notify(skb, net, 0, RTNLGRP_IPV4_MROUTE_R, NULL, GFP_ATOMIC); 2478 return; 2479 2480 nla_put_failure: 2481 nlmsg_cancel(skb, nlh); 2482 errout: 2483 kfree_skb(skb); 2484 rtnl_set_sk_err(net, RTNLGRP_IPV4_MROUTE_R, -ENOBUFS); 2485 } 2486 2487 static int ipmr_rtm_valid_getroute_req(struct sk_buff *skb, 2488 const struct nlmsghdr *nlh, 2489 struct nlattr **tb, 2490 struct netlink_ext_ack *extack) 2491 { 2492 struct rtmsg *rtm; 2493 int i, err; 2494 2495 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*rtm))) { 2496 NL_SET_ERR_MSG(extack, "ipv4: Invalid header for multicast route get request"); 2497 return -EINVAL; 2498 } 2499 2500 if (!netlink_strict_get_check(skb)) 2501 return nlmsg_parse_deprecated(nlh, sizeof(*rtm), tb, RTA_MAX, 2502 rtm_ipv4_policy, extack); 2503 2504 rtm = nlmsg_data(nlh); 2505 if ((rtm->rtm_src_len && rtm->rtm_src_len != 32) || 2506 (rtm->rtm_dst_len && rtm->rtm_dst_len != 32) || 2507 rtm->rtm_tos || rtm->rtm_table || rtm->rtm_protocol || 2508 rtm->rtm_scope || rtm->rtm_type || rtm->rtm_flags) { 2509 NL_SET_ERR_MSG(extack, "ipv4: Invalid values in header for multicast route get request"); 2510 return -EINVAL; 2511 } 2512 2513 err = nlmsg_parse_deprecated_strict(nlh, sizeof(*rtm), tb, RTA_MAX, 2514 rtm_ipv4_policy, extack); 2515 if (err) 2516 return err; 2517 2518 if ((tb[RTA_SRC] && !rtm->rtm_src_len) || 2519 (tb[RTA_DST] && !rtm->rtm_dst_len)) { 2520 NL_SET_ERR_MSG(extack, "ipv4: rtm_src_len and rtm_dst_len must be 32 for IPv4"); 2521 return -EINVAL; 2522 } 2523 2524 for (i = 0; i <= RTA_MAX; i++) { 2525 if (!tb[i]) 2526 continue; 2527 2528 switch (i) { 2529 case RTA_SRC: 2530 case RTA_DST: 2531 case RTA_TABLE: 2532 break; 2533 default: 2534 NL_SET_ERR_MSG(extack, "ipv4: Unsupported attribute in multicast route get request"); 2535 return -EINVAL; 2536 } 2537 } 2538 2539 return 0; 2540 } 2541 2542 static int ipmr_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh, 2543 struct netlink_ext_ack *extack) 2544 { 2545 struct net *net = sock_net(in_skb->sk); 2546 struct nlattr *tb[RTA_MAX + 1]; 2547 struct sk_buff *skb = NULL; 2548 struct mfc_cache *cache; 2549 struct mr_table *mrt; 2550 __be32 src, grp; 2551 u32 tableid; 2552 int err; 2553 2554 err = ipmr_rtm_valid_getroute_req(in_skb, nlh, tb, extack); 2555 if (err < 0) 2556 goto errout; 2557 2558 src = tb[RTA_SRC] ? nla_get_in_addr(tb[RTA_SRC]) : 0; 2559 grp = tb[RTA_DST] ? nla_get_in_addr(tb[RTA_DST]) : 0; 2560 tableid = tb[RTA_TABLE] ? nla_get_u32(tb[RTA_TABLE]) : 0; 2561 2562 mrt = ipmr_get_table(net, tableid ? tableid : RT_TABLE_DEFAULT); 2563 if (!mrt) { 2564 err = -ENOENT; 2565 goto errout_free; 2566 } 2567 2568 /* entries are added/deleted only under RTNL */ 2569 rcu_read_lock(); 2570 cache = ipmr_cache_find(mrt, src, grp); 2571 rcu_read_unlock(); 2572 if (!cache) { 2573 err = -ENOENT; 2574 goto errout_free; 2575 } 2576 2577 skb = nlmsg_new(mroute_msgsize(false, mrt->maxvif), GFP_KERNEL); 2578 if (!skb) { 2579 err = -ENOBUFS; 2580 goto errout_free; 2581 } 2582 2583 err = ipmr_fill_mroute(mrt, skb, NETLINK_CB(in_skb).portid, 2584 nlh->nlmsg_seq, cache, 2585 RTM_NEWROUTE, 0); 2586 if (err < 0) 2587 goto errout_free; 2588 2589 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid); 2590 2591 errout: 2592 return err; 2593 2594 errout_free: 2595 kfree_skb(skb); 2596 goto errout; 2597 } 2598 2599 static int ipmr_rtm_dumproute(struct sk_buff *skb, struct netlink_callback *cb) 2600 { 2601 struct fib_dump_filter filter = {}; 2602 int err; 2603 2604 if (cb->strict_check) { 2605 err = ip_valid_fib_dump_req(sock_net(skb->sk), cb->nlh, 2606 &filter, cb); 2607 if (err < 0) 2608 return err; 2609 } 2610 2611 if (filter.table_id) { 2612 struct mr_table *mrt; 2613 2614 mrt = ipmr_get_table(sock_net(skb->sk), filter.table_id); 2615 if (!mrt) { 2616 if (rtnl_msg_family(cb->nlh) != RTNL_FAMILY_IPMR) 2617 return skb->len; 2618 2619 NL_SET_ERR_MSG(cb->extack, "ipv4: MR table does not exist"); 2620 return -ENOENT; 2621 } 2622 err = mr_table_dump(mrt, skb, cb, _ipmr_fill_mroute, 2623 &mfc_unres_lock, &filter); 2624 return skb->len ? : err; 2625 } 2626 2627 return mr_rtm_dumproute(skb, cb, ipmr_mr_table_iter, 2628 _ipmr_fill_mroute, &mfc_unres_lock, &filter); 2629 } 2630 2631 static const struct nla_policy rtm_ipmr_policy[RTA_MAX + 1] = { 2632 [RTA_SRC] = { .type = NLA_U32 }, 2633 [RTA_DST] = { .type = NLA_U32 }, 2634 [RTA_IIF] = { .type = NLA_U32 }, 2635 [RTA_TABLE] = { .type = NLA_U32 }, 2636 [RTA_MULTIPATH] = { .len = sizeof(struct rtnexthop) }, 2637 }; 2638 2639 static bool ipmr_rtm_validate_proto(unsigned char rtm_protocol) 2640 { 2641 switch (rtm_protocol) { 2642 case RTPROT_STATIC: 2643 case RTPROT_MROUTED: 2644 return true; 2645 } 2646 return false; 2647 } 2648 2649 static int ipmr_nla_get_ttls(const struct nlattr *nla, struct mfcctl *mfcc) 2650 { 2651 struct rtnexthop *rtnh = nla_data(nla); 2652 int remaining = nla_len(nla), vifi = 0; 2653 2654 while (rtnh_ok(rtnh, remaining)) { 2655 mfcc->mfcc_ttls[vifi] = rtnh->rtnh_hops; 2656 if (++vifi == MAXVIFS) 2657 break; 2658 rtnh = rtnh_next(rtnh, &remaining); 2659 } 2660 2661 return remaining > 0 ? -EINVAL : vifi; 2662 } 2663 2664 /* returns < 0 on error, 0 for ADD_MFC and 1 for ADD_MFC_PROXY */ 2665 static int rtm_to_ipmr_mfcc(struct net *net, struct nlmsghdr *nlh, 2666 struct mfcctl *mfcc, int *mrtsock, 2667 struct mr_table **mrtret, 2668 struct netlink_ext_ack *extack) 2669 { 2670 struct net_device *dev = NULL; 2671 u32 tblid = RT_TABLE_DEFAULT; 2672 struct mr_table *mrt; 2673 struct nlattr *attr; 2674 struct rtmsg *rtm; 2675 int ret, rem; 2676 2677 ret = nlmsg_validate_deprecated(nlh, sizeof(*rtm), RTA_MAX, 2678 rtm_ipmr_policy, extack); 2679 if (ret < 0) 2680 goto out; 2681 rtm = nlmsg_data(nlh); 2682 2683 ret = -EINVAL; 2684 if (rtm->rtm_family != RTNL_FAMILY_IPMR || rtm->rtm_dst_len != 32 || 2685 rtm->rtm_type != RTN_MULTICAST || 2686 rtm->rtm_scope != RT_SCOPE_UNIVERSE || 2687 !ipmr_rtm_validate_proto(rtm->rtm_protocol)) 2688 goto out; 2689 2690 memset(mfcc, 0, sizeof(*mfcc)); 2691 mfcc->mfcc_parent = -1; 2692 ret = 0; 2693 nlmsg_for_each_attr(attr, nlh, sizeof(struct rtmsg), rem) { 2694 switch (nla_type(attr)) { 2695 case RTA_SRC: 2696 mfcc->mfcc_origin.s_addr = nla_get_be32(attr); 2697 break; 2698 case RTA_DST: 2699 mfcc->mfcc_mcastgrp.s_addr = nla_get_be32(attr); 2700 break; 2701 case RTA_IIF: 2702 dev = __dev_get_by_index(net, nla_get_u32(attr)); 2703 if (!dev) { 2704 ret = -ENODEV; 2705 goto out; 2706 } 2707 break; 2708 case RTA_MULTIPATH: 2709 if (ipmr_nla_get_ttls(attr, mfcc) < 0) { 2710 ret = -EINVAL; 2711 goto out; 2712 } 2713 break; 2714 case RTA_PREFSRC: 2715 ret = 1; 2716 break; 2717 case RTA_TABLE: 2718 tblid = nla_get_u32(attr); 2719 break; 2720 } 2721 } 2722 mrt = ipmr_get_table(net, tblid); 2723 if (!mrt) { 2724 ret = -ENOENT; 2725 goto out; 2726 } 2727 *mrtret = mrt; 2728 *mrtsock = rtm->rtm_protocol == RTPROT_MROUTED ? 1 : 0; 2729 if (dev) 2730 mfcc->mfcc_parent = ipmr_find_vif(mrt, dev); 2731 2732 out: 2733 return ret; 2734 } 2735 2736 /* takes care of both newroute and delroute */ 2737 static int ipmr_rtm_route(struct sk_buff *skb, struct nlmsghdr *nlh, 2738 struct netlink_ext_ack *extack) 2739 { 2740 struct net *net = sock_net(skb->sk); 2741 int ret, mrtsock, parent; 2742 struct mr_table *tbl; 2743 struct mfcctl mfcc; 2744 2745 mrtsock = 0; 2746 tbl = NULL; 2747 ret = rtm_to_ipmr_mfcc(net, nlh, &mfcc, &mrtsock, &tbl, extack); 2748 if (ret < 0) 2749 return ret; 2750 2751 parent = ret ? mfcc.mfcc_parent : -1; 2752 if (nlh->nlmsg_type == RTM_NEWROUTE) 2753 return ipmr_mfc_add(net, tbl, &mfcc, mrtsock, parent); 2754 else 2755 return ipmr_mfc_delete(tbl, &mfcc, parent); 2756 } 2757 2758 static bool ipmr_fill_table(struct mr_table *mrt, struct sk_buff *skb) 2759 { 2760 u32 queue_len = atomic_read(&mrt->cache_resolve_queue_len); 2761 2762 if (nla_put_u32(skb, IPMRA_TABLE_ID, mrt->id) || 2763 nla_put_u32(skb, IPMRA_TABLE_CACHE_RES_QUEUE_LEN, queue_len) || 2764 nla_put_s32(skb, IPMRA_TABLE_MROUTE_REG_VIF_NUM, 2765 mrt->mroute_reg_vif_num) || 2766 nla_put_u8(skb, IPMRA_TABLE_MROUTE_DO_ASSERT, 2767 mrt->mroute_do_assert) || 2768 nla_put_u8(skb, IPMRA_TABLE_MROUTE_DO_PIM, mrt->mroute_do_pim) || 2769 nla_put_u8(skb, IPMRA_TABLE_MROUTE_DO_WRVIFWHOLE, 2770 mrt->mroute_do_wrvifwhole)) 2771 return false; 2772 2773 return true; 2774 } 2775 2776 static bool ipmr_fill_vif(struct mr_table *mrt, u32 vifid, struct sk_buff *skb) 2777 { 2778 struct nlattr *vif_nest; 2779 struct vif_device *vif; 2780 2781 /* if the VIF doesn't exist just continue */ 2782 if (!VIF_EXISTS(mrt, vifid)) 2783 return true; 2784 2785 vif = &mrt->vif_table[vifid]; 2786 vif_nest = nla_nest_start_noflag(skb, IPMRA_VIF); 2787 if (!vif_nest) 2788 return false; 2789 if (nla_put_u32(skb, IPMRA_VIFA_IFINDEX, vif->dev->ifindex) || 2790 nla_put_u32(skb, IPMRA_VIFA_VIF_ID, vifid) || 2791 nla_put_u16(skb, IPMRA_VIFA_FLAGS, vif->flags) || 2792 nla_put_u64_64bit(skb, IPMRA_VIFA_BYTES_IN, vif->bytes_in, 2793 IPMRA_VIFA_PAD) || 2794 nla_put_u64_64bit(skb, IPMRA_VIFA_BYTES_OUT, vif->bytes_out, 2795 IPMRA_VIFA_PAD) || 2796 nla_put_u64_64bit(skb, IPMRA_VIFA_PACKETS_IN, vif->pkt_in, 2797 IPMRA_VIFA_PAD) || 2798 nla_put_u64_64bit(skb, IPMRA_VIFA_PACKETS_OUT, vif->pkt_out, 2799 IPMRA_VIFA_PAD) || 2800 nla_put_be32(skb, IPMRA_VIFA_LOCAL_ADDR, vif->local) || 2801 nla_put_be32(skb, IPMRA_VIFA_REMOTE_ADDR, vif->remote)) { 2802 nla_nest_cancel(skb, vif_nest); 2803 return false; 2804 } 2805 nla_nest_end(skb, vif_nest); 2806 2807 return true; 2808 } 2809 2810 static int ipmr_valid_dumplink(const struct nlmsghdr *nlh, 2811 struct netlink_ext_ack *extack) 2812 { 2813 struct ifinfomsg *ifm; 2814 2815 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) { 2816 NL_SET_ERR_MSG(extack, "ipv4: Invalid header for ipmr link dump"); 2817 return -EINVAL; 2818 } 2819 2820 if (nlmsg_attrlen(nlh, sizeof(*ifm))) { 2821 NL_SET_ERR_MSG(extack, "Invalid data after header in ipmr link dump"); 2822 return -EINVAL; 2823 } 2824 2825 ifm = nlmsg_data(nlh); 2826 if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags || 2827 ifm->ifi_change || ifm->ifi_index) { 2828 NL_SET_ERR_MSG(extack, "Invalid values in header for ipmr link dump request"); 2829 return -EINVAL; 2830 } 2831 2832 return 0; 2833 } 2834 2835 static int ipmr_rtm_dumplink(struct sk_buff *skb, struct netlink_callback *cb) 2836 { 2837 struct net *net = sock_net(skb->sk); 2838 struct nlmsghdr *nlh = NULL; 2839 unsigned int t = 0, s_t; 2840 unsigned int e = 0, s_e; 2841 struct mr_table *mrt; 2842 2843 if (cb->strict_check) { 2844 int err = ipmr_valid_dumplink(cb->nlh, cb->extack); 2845 2846 if (err < 0) 2847 return err; 2848 } 2849 2850 s_t = cb->args[0]; 2851 s_e = cb->args[1]; 2852 2853 ipmr_for_each_table(mrt, net) { 2854 struct nlattr *vifs, *af; 2855 struct ifinfomsg *hdr; 2856 u32 i; 2857 2858 if (t < s_t) 2859 goto skip_table; 2860 nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, 2861 cb->nlh->nlmsg_seq, RTM_NEWLINK, 2862 sizeof(*hdr), NLM_F_MULTI); 2863 if (!nlh) 2864 break; 2865 2866 hdr = nlmsg_data(nlh); 2867 memset(hdr, 0, sizeof(*hdr)); 2868 hdr->ifi_family = RTNL_FAMILY_IPMR; 2869 2870 af = nla_nest_start_noflag(skb, IFLA_AF_SPEC); 2871 if (!af) { 2872 nlmsg_cancel(skb, nlh); 2873 goto out; 2874 } 2875 2876 if (!ipmr_fill_table(mrt, skb)) { 2877 nlmsg_cancel(skb, nlh); 2878 goto out; 2879 } 2880 2881 vifs = nla_nest_start_noflag(skb, IPMRA_TABLE_VIFS); 2882 if (!vifs) { 2883 nla_nest_end(skb, af); 2884 nlmsg_end(skb, nlh); 2885 goto out; 2886 } 2887 for (i = 0; i < mrt->maxvif; i++) { 2888 if (e < s_e) 2889 goto skip_entry; 2890 if (!ipmr_fill_vif(mrt, i, skb)) { 2891 nla_nest_end(skb, vifs); 2892 nla_nest_end(skb, af); 2893 nlmsg_end(skb, nlh); 2894 goto out; 2895 } 2896 skip_entry: 2897 e++; 2898 } 2899 s_e = 0; 2900 e = 0; 2901 nla_nest_end(skb, vifs); 2902 nla_nest_end(skb, af); 2903 nlmsg_end(skb, nlh); 2904 skip_table: 2905 t++; 2906 } 2907 2908 out: 2909 cb->args[1] = e; 2910 cb->args[0] = t; 2911 2912 return skb->len; 2913 } 2914 2915 #ifdef CONFIG_PROC_FS 2916 /* The /proc interfaces to multicast routing : 2917 * /proc/net/ip_mr_cache & /proc/net/ip_mr_vif 2918 */ 2919 2920 static void *ipmr_vif_seq_start(struct seq_file *seq, loff_t *pos) 2921 __acquires(mrt_lock) 2922 { 2923 struct mr_vif_iter *iter = seq->private; 2924 struct net *net = seq_file_net(seq); 2925 struct mr_table *mrt; 2926 2927 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT); 2928 if (!mrt) 2929 return ERR_PTR(-ENOENT); 2930 2931 iter->mrt = mrt; 2932 2933 read_lock(&mrt_lock); 2934 return mr_vif_seq_start(seq, pos); 2935 } 2936 2937 static void ipmr_vif_seq_stop(struct seq_file *seq, void *v) 2938 __releases(mrt_lock) 2939 { 2940 read_unlock(&mrt_lock); 2941 } 2942 2943 static int ipmr_vif_seq_show(struct seq_file *seq, void *v) 2944 { 2945 struct mr_vif_iter *iter = seq->private; 2946 struct mr_table *mrt = iter->mrt; 2947 2948 if (v == SEQ_START_TOKEN) { 2949 seq_puts(seq, 2950 "Interface BytesIn PktsIn BytesOut PktsOut Flags Local Remote\n"); 2951 } else { 2952 const struct vif_device *vif = v; 2953 const char *name = vif->dev ? 2954 vif->dev->name : "none"; 2955 2956 seq_printf(seq, 2957 "%2td %-10s %8ld %7ld %8ld %7ld %05X %08X %08X\n", 2958 vif - mrt->vif_table, 2959 name, vif->bytes_in, vif->pkt_in, 2960 vif->bytes_out, vif->pkt_out, 2961 vif->flags, vif->local, vif->remote); 2962 } 2963 return 0; 2964 } 2965 2966 static const struct seq_operations ipmr_vif_seq_ops = { 2967 .start = ipmr_vif_seq_start, 2968 .next = mr_vif_seq_next, 2969 .stop = ipmr_vif_seq_stop, 2970 .show = ipmr_vif_seq_show, 2971 }; 2972 2973 static void *ipmr_mfc_seq_start(struct seq_file *seq, loff_t *pos) 2974 { 2975 struct net *net = seq_file_net(seq); 2976 struct mr_table *mrt; 2977 2978 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT); 2979 if (!mrt) 2980 return ERR_PTR(-ENOENT); 2981 2982 return mr_mfc_seq_start(seq, pos, mrt, &mfc_unres_lock); 2983 } 2984 2985 static int ipmr_mfc_seq_show(struct seq_file *seq, void *v) 2986 { 2987 int n; 2988 2989 if (v == SEQ_START_TOKEN) { 2990 seq_puts(seq, 2991 "Group Origin Iif Pkts Bytes Wrong Oifs\n"); 2992 } else { 2993 const struct mfc_cache *mfc = v; 2994 const struct mr_mfc_iter *it = seq->private; 2995 const struct mr_table *mrt = it->mrt; 2996 2997 seq_printf(seq, "%08X %08X %-3hd", 2998 (__force u32) mfc->mfc_mcastgrp, 2999 (__force u32) mfc->mfc_origin, 3000 mfc->_c.mfc_parent); 3001 3002 if (it->cache != &mrt->mfc_unres_queue) { 3003 seq_printf(seq, " %8lu %8lu %8lu", 3004 mfc->_c.mfc_un.res.pkt, 3005 mfc->_c.mfc_un.res.bytes, 3006 mfc->_c.mfc_un.res.wrong_if); 3007 for (n = mfc->_c.mfc_un.res.minvif; 3008 n < mfc->_c.mfc_un.res.maxvif; n++) { 3009 if (VIF_EXISTS(mrt, n) && 3010 mfc->_c.mfc_un.res.ttls[n] < 255) 3011 seq_printf(seq, 3012 " %2d:%-3d", 3013 n, mfc->_c.mfc_un.res.ttls[n]); 3014 } 3015 } else { 3016 /* unresolved mfc_caches don't contain 3017 * pkt, bytes and wrong_if values 3018 */ 3019 seq_printf(seq, " %8lu %8lu %8lu", 0ul, 0ul, 0ul); 3020 } 3021 seq_putc(seq, '\n'); 3022 } 3023 return 0; 3024 } 3025 3026 static const struct seq_operations ipmr_mfc_seq_ops = { 3027 .start = ipmr_mfc_seq_start, 3028 .next = mr_mfc_seq_next, 3029 .stop = mr_mfc_seq_stop, 3030 .show = ipmr_mfc_seq_show, 3031 }; 3032 #endif 3033 3034 #ifdef CONFIG_IP_PIMSM_V2 3035 static const struct net_protocol pim_protocol = { 3036 .handler = pim_rcv, 3037 .netns_ok = 1, 3038 }; 3039 #endif 3040 3041 static unsigned int ipmr_seq_read(struct net *net) 3042 { 3043 ASSERT_RTNL(); 3044 3045 return net->ipv4.ipmr_seq + ipmr_rules_seq_read(net); 3046 } 3047 3048 static int ipmr_dump(struct net *net, struct notifier_block *nb, 3049 struct netlink_ext_ack *extack) 3050 { 3051 return mr_dump(net, nb, RTNL_FAMILY_IPMR, ipmr_rules_dump, 3052 ipmr_mr_table_iter, &mrt_lock, extack); 3053 } 3054 3055 static const struct fib_notifier_ops ipmr_notifier_ops_template = { 3056 .family = RTNL_FAMILY_IPMR, 3057 .fib_seq_read = ipmr_seq_read, 3058 .fib_dump = ipmr_dump, 3059 .owner = THIS_MODULE, 3060 }; 3061 3062 static int __net_init ipmr_notifier_init(struct net *net) 3063 { 3064 struct fib_notifier_ops *ops; 3065 3066 net->ipv4.ipmr_seq = 0; 3067 3068 ops = fib_notifier_ops_register(&ipmr_notifier_ops_template, net); 3069 if (IS_ERR(ops)) 3070 return PTR_ERR(ops); 3071 net->ipv4.ipmr_notifier_ops = ops; 3072 3073 return 0; 3074 } 3075 3076 static void __net_exit ipmr_notifier_exit(struct net *net) 3077 { 3078 fib_notifier_ops_unregister(net->ipv4.ipmr_notifier_ops); 3079 net->ipv4.ipmr_notifier_ops = NULL; 3080 } 3081 3082 /* Setup for IP multicast routing */ 3083 static int __net_init ipmr_net_init(struct net *net) 3084 { 3085 int err; 3086 3087 err = ipmr_notifier_init(net); 3088 if (err) 3089 goto ipmr_notifier_fail; 3090 3091 err = ipmr_rules_init(net); 3092 if (err < 0) 3093 goto ipmr_rules_fail; 3094 3095 #ifdef CONFIG_PROC_FS 3096 err = -ENOMEM; 3097 if (!proc_create_net("ip_mr_vif", 0, net->proc_net, &ipmr_vif_seq_ops, 3098 sizeof(struct mr_vif_iter))) 3099 goto proc_vif_fail; 3100 if (!proc_create_net("ip_mr_cache", 0, net->proc_net, &ipmr_mfc_seq_ops, 3101 sizeof(struct mr_mfc_iter))) 3102 goto proc_cache_fail; 3103 #endif 3104 return 0; 3105 3106 #ifdef CONFIG_PROC_FS 3107 proc_cache_fail: 3108 remove_proc_entry("ip_mr_vif", net->proc_net); 3109 proc_vif_fail: 3110 ipmr_rules_exit(net); 3111 #endif 3112 ipmr_rules_fail: 3113 ipmr_notifier_exit(net); 3114 ipmr_notifier_fail: 3115 return err; 3116 } 3117 3118 static void __net_exit ipmr_net_exit(struct net *net) 3119 { 3120 #ifdef CONFIG_PROC_FS 3121 remove_proc_entry("ip_mr_cache", net->proc_net); 3122 remove_proc_entry("ip_mr_vif", net->proc_net); 3123 #endif 3124 ipmr_notifier_exit(net); 3125 ipmr_rules_exit(net); 3126 } 3127 3128 static struct pernet_operations ipmr_net_ops = { 3129 .init = ipmr_net_init, 3130 .exit = ipmr_net_exit, 3131 }; 3132 3133 int __init ip_mr_init(void) 3134 { 3135 int err; 3136 3137 mrt_cachep = kmem_cache_create("ip_mrt_cache", 3138 sizeof(struct mfc_cache), 3139 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC, 3140 NULL); 3141 3142 err = register_pernet_subsys(&ipmr_net_ops); 3143 if (err) 3144 goto reg_pernet_fail; 3145 3146 err = register_netdevice_notifier(&ip_mr_notifier); 3147 if (err) 3148 goto reg_notif_fail; 3149 #ifdef CONFIG_IP_PIMSM_V2 3150 if (inet_add_protocol(&pim_protocol, IPPROTO_PIM) < 0) { 3151 pr_err("%s: can't add PIM protocol\n", __func__); 3152 err = -EAGAIN; 3153 goto add_proto_fail; 3154 } 3155 #endif 3156 rtnl_register(RTNL_FAMILY_IPMR, RTM_GETROUTE, 3157 ipmr_rtm_getroute, ipmr_rtm_dumproute, 0); 3158 rtnl_register(RTNL_FAMILY_IPMR, RTM_NEWROUTE, 3159 ipmr_rtm_route, NULL, 0); 3160 rtnl_register(RTNL_FAMILY_IPMR, RTM_DELROUTE, 3161 ipmr_rtm_route, NULL, 0); 3162 3163 rtnl_register(RTNL_FAMILY_IPMR, RTM_GETLINK, 3164 NULL, ipmr_rtm_dumplink, 0); 3165 return 0; 3166 3167 #ifdef CONFIG_IP_PIMSM_V2 3168 add_proto_fail: 3169 unregister_netdevice_notifier(&ip_mr_notifier); 3170 #endif 3171 reg_notif_fail: 3172 unregister_pernet_subsys(&ipmr_net_ops); 3173 reg_pernet_fail: 3174 kmem_cache_destroy(mrt_cachep); 3175 return err; 3176 } 3177