Version:  2.0.40 2.2.26 2.4.37 3.4 3.5 3.6 3.7 3.8 3.9 3.10 3.11 3.12 3.13 3.14 3.15 3.16 3.17 3.18 3.19 4.0

Linux/net/ipv4/ipmr.c

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

This page was automatically generated by LXR 0.3.1 (source).  •  Linux is a registered trademark of Linus Torvalds  •  Contact us