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