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

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

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

~ [ 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