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