~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/net/ipv4/ipmr.c

Version: ~ [ linux-5.3-rc1 ] ~ [ linux-5.2.2 ] ~ [ linux-5.1.19 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.60 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.134 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.186 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.186 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.140 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.70 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-3.9.11 ] ~ [ linux-3.8.13 ] ~ [ linux-3.7.10 ] ~ [ linux-3.6.11 ] ~ [ linux-3.5.7 ] ~ [ linux-3.4.113 ] ~ [ linux-3.3.8 ] ~ [ linux-3.2.102 ] ~ [ linux-3.1.10 ] ~ [ linux-3.0.101 ] ~ [ linux-2.6.39.4 ] ~ [ linux-2.6.38.8 ] ~ [ linux-2.6.37.6 ] ~ [ linux-2.6.36.4 ] ~ [ linux-2.6.35.14 ] ~ [ linux-2.6.34.15 ] ~ [ linux-2.6.33.20 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp