blob: d570d3ad40a9def555f021c368e40bf7c9c60744 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * IP multicast routing support for mrouted 3.6/3.8
3 *
4 * (c) 1995 Alan Cox, <alan@redhat.com>
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 * Version: $Id: ipmr.c,v 1.65 2001/10/31 21:55:54 davem Exp $
13 *
14 * Fixes:
15 * Michael Chastain : Incorrect size of copying.
16 * Alan Cox : Added the cache manager code
17 * Alan Cox : Fixed the clone/copy bug and device race.
18 * Mike McLagan : Routing by source
19 * Malcolm Beattie : Buffer handling fixes.
20 * Alexey Kuznetsov : Double buffer free and other fixes.
21 * SVR Anand : Fixed several multicast bugs and problems.
22 * Alexey Kuznetsov : Status, optimisations and more.
23 * Brad Parker : Better behaviour on mrouted upcall
24 * overflow.
25 * Carlos Picoto : PIMv1 Support
26 * Pavlin Ivanov Radoslavov: PIMv2 Registers must checksum only PIM header
27 * Relax this requrement to work with older peers.
28 *
29 */
30
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <asm/system.h>
32#include <asm/uaccess.h>
33#include <linux/types.h>
Randy Dunlap4fc268d2006-01-11 12:17:47 -080034#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <linux/errno.h>
36#include <linux/timer.h>
37#include <linux/mm.h>
38#include <linux/kernel.h>
39#include <linux/fcntl.h>
40#include <linux/stat.h>
41#include <linux/socket.h>
42#include <linux/in.h>
43#include <linux/inet.h>
44#include <linux/netdevice.h>
45#include <linux/inetdevice.h>
46#include <linux/igmp.h>
47#include <linux/proc_fs.h>
48#include <linux/seq_file.h>
49#include <linux/mroute.h>
50#include <linux/init.h>
Kris Katterjohn46f25df2006-01-05 16:35:42 -080051#include <linux/if_ether.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include <net/ip.h>
53#include <net/protocol.h>
54#include <linux/skbuff.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020055#include <net/route.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#include <net/sock.h>
57#include <net/icmp.h>
58#include <net/udp.h>
59#include <net/raw.h>
60#include <linux/notifier.h>
61#include <linux/if_arp.h>
62#include <linux/netfilter_ipv4.h>
63#include <net/ipip.h>
64#include <net/checksum.h>
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -070065#include <net/netlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
67#if defined(CONFIG_IP_PIMSM_V1) || defined(CONFIG_IP_PIMSM_V2)
68#define CONFIG_IP_PIMSM 1
69#endif
70
71static struct sock *mroute_socket;
72
73
74/* Big lock, protecting vif table, mrt cache and mroute socket state.
75 Note that the changes are semaphored via rtnl_lock.
76 */
77
78static DEFINE_RWLOCK(mrt_lock);
79
80/*
81 * Multicast router control variables
82 */
83
84static struct vif_device vif_table[MAXVIFS]; /* Devices */
85static int maxvif;
86
87#define VIF_EXISTS(idx) (vif_table[idx].dev != NULL)
88
89static int mroute_do_assert; /* Set in PIM assert */
90static int mroute_do_pim;
91
92static struct mfc_cache *mfc_cache_array[MFC_LINES]; /* Forwarding cache */
93
94static struct mfc_cache *mfc_unres_queue; /* Queue of unresolved entries */
95static atomic_t cache_resolve_queue_len; /* Size of unresolved */
96
97/* Special spinlock for queue of unresolved entries */
98static DEFINE_SPINLOCK(mfc_unres_lock);
99
100/* We return to original Alan's scheme. Hash table of resolved
101 entries is changed only in process context and protected
102 with weak lock mrt_lock. Queue of unresolved entries is protected
103 with strong spinlock mfc_unres_lock.
104
105 In this case data path is free of exclusive locks at all.
106 */
107
Christoph Lametere18b8902006-12-06 20:33:20 -0800108static struct kmem_cache *mrt_cachep __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
110static int ip_mr_forward(struct sk_buff *skb, struct mfc_cache *cache, int local);
111static int ipmr_cache_report(struct sk_buff *pkt, vifi_t vifi, int assert);
112static int ipmr_fill_mroute(struct sk_buff *skb, struct mfc_cache *c, struct rtmsg *rtm);
113
114#ifdef CONFIG_IP_PIMSM_V2
115static struct net_protocol pim_protocol;
116#endif
117
118static struct timer_list ipmr_expire_timer;
119
120/* Service routines creating virtual interfaces: DVMRP tunnels and PIMREG */
121
122static
123struct net_device *ipmr_new_tunnel(struct vifctl *v)
124{
125 struct net_device *dev;
126
127 dev = __dev_get_by_name("tunl0");
128
129 if (dev) {
130 int err;
131 struct ifreq ifr;
132 mm_segment_t oldfs;
133 struct ip_tunnel_parm p;
134 struct in_device *in_dev;
135
136 memset(&p, 0, sizeof(p));
137 p.iph.daddr = v->vifc_rmt_addr.s_addr;
138 p.iph.saddr = v->vifc_lcl_addr.s_addr;
139 p.iph.version = 4;
140 p.iph.ihl = 5;
141 p.iph.protocol = IPPROTO_IPIP;
142 sprintf(p.name, "dvmrp%d", v->vifc_vifi);
143 ifr.ifr_ifru.ifru_data = (void*)&p;
144
145 oldfs = get_fs(); set_fs(KERNEL_DS);
146 err = dev->do_ioctl(dev, &ifr, SIOCADDTUNNEL);
147 set_fs(oldfs);
148
149 dev = NULL;
150
151 if (err == 0 && (dev = __dev_get_by_name(p.name)) != NULL) {
152 dev->flags |= IFF_MULTICAST;
153
Herbert Xue5ed6392005-10-03 14:35:55 -0700154 in_dev = __in_dev_get_rtnl(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 if (in_dev == NULL && (in_dev = inetdev_init(dev)) == NULL)
156 goto failure;
Herbert Xu42f811b2007-06-04 23:34:44 -0700157 IN_DEV_CONF_SET(in_dev, RP_FILTER, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
159 if (dev_open(dev))
160 goto failure;
161 }
162 }
163 return dev;
164
165failure:
166 /* allow the register to be completed before unregistering. */
167 rtnl_unlock();
168 rtnl_lock();
169
170 unregister_netdevice(dev);
171 return NULL;
172}
173
174#ifdef CONFIG_IP_PIMSM
175
176static int reg_vif_num = -1;
177
178static int reg_vif_xmit(struct sk_buff *skb, struct net_device *dev)
179{
180 read_lock(&mrt_lock);
Patrick McHardy2941a482006-01-08 22:05:26 -0800181 ((struct net_device_stats*)netdev_priv(dev))->tx_bytes += skb->len;
182 ((struct net_device_stats*)netdev_priv(dev))->tx_packets++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 ipmr_cache_report(skb, reg_vif_num, IGMPMSG_WHOLEPKT);
184 read_unlock(&mrt_lock);
185 kfree_skb(skb);
186 return 0;
187}
188
189static struct net_device_stats *reg_vif_get_stats(struct net_device *dev)
190{
Patrick McHardy2941a482006-01-08 22:05:26 -0800191 return (struct net_device_stats*)netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192}
193
194static void reg_vif_setup(struct net_device *dev)
195{
196 dev->type = ARPHRD_PIMREG;
Kris Katterjohn46f25df2006-01-05 16:35:42 -0800197 dev->mtu = ETH_DATA_LEN - sizeof(struct iphdr) - 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 dev->flags = IFF_NOARP;
199 dev->hard_start_xmit = reg_vif_xmit;
200 dev->get_stats = reg_vif_get_stats;
201 dev->destructor = free_netdev;
202}
203
204static struct net_device *ipmr_reg_vif(void)
205{
206 struct net_device *dev;
207 struct in_device *in_dev;
208
209 dev = alloc_netdev(sizeof(struct net_device_stats), "pimreg",
210 reg_vif_setup);
211
212 if (dev == NULL)
213 return NULL;
214
215 if (register_netdevice(dev)) {
216 free_netdev(dev);
217 return NULL;
218 }
219 dev->iflink = 0;
220
221 if ((in_dev = inetdev_init(dev)) == NULL)
222 goto failure;
223
Herbert Xu42f811b2007-06-04 23:34:44 -0700224 IN_DEV_CONF_SET(in_dev, RP_FILTER, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
226 if (dev_open(dev))
227 goto failure;
228
229 return dev;
230
231failure:
232 /* allow the register to be completed before unregistering. */
233 rtnl_unlock();
234 rtnl_lock();
235
236 unregister_netdevice(dev);
237 return NULL;
238}
239#endif
240
241/*
242 * Delete a VIF entry
243 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900244
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245static int vif_delete(int vifi)
246{
247 struct vif_device *v;
248 struct net_device *dev;
249 struct in_device *in_dev;
250
251 if (vifi < 0 || vifi >= maxvif)
252 return -EADDRNOTAVAIL;
253
254 v = &vif_table[vifi];
255
256 write_lock_bh(&mrt_lock);
257 dev = v->dev;
258 v->dev = NULL;
259
260 if (!dev) {
261 write_unlock_bh(&mrt_lock);
262 return -EADDRNOTAVAIL;
263 }
264
265#ifdef CONFIG_IP_PIMSM
266 if (vifi == reg_vif_num)
267 reg_vif_num = -1;
268#endif
269
270 if (vifi+1 == maxvif) {
271 int tmp;
272 for (tmp=vifi-1; tmp>=0; tmp--) {
273 if (VIF_EXISTS(tmp))
274 break;
275 }
276 maxvif = tmp+1;
277 }
278
279 write_unlock_bh(&mrt_lock);
280
281 dev_set_allmulti(dev, -1);
282
Herbert Xue5ed6392005-10-03 14:35:55 -0700283 if ((in_dev = __in_dev_get_rtnl(dev)) != NULL) {
Herbert Xu42f811b2007-06-04 23:34:44 -0700284 IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 ip_rt_multicast_event(in_dev);
286 }
287
288 if (v->flags&(VIFF_TUNNEL|VIFF_REGISTER))
289 unregister_netdevice(dev);
290
291 dev_put(dev);
292 return 0;
293}
294
295/* Destroy an unresolved cache entry, killing queued skbs
296 and reporting error to netlink readers.
297 */
298
299static void ipmr_destroy_unres(struct mfc_cache *c)
300{
301 struct sk_buff *skb;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700302 struct nlmsgerr *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
304 atomic_dec(&cache_resolve_queue_len);
305
Stephen Hemminger132adf52007-03-08 20:44:43 -0800306 while ((skb=skb_dequeue(&c->mfc_un.unres.unresolved))) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700307 if (ip_hdr(skb)->version == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 struct nlmsghdr *nlh = (struct nlmsghdr *)skb_pull(skb, sizeof(struct iphdr));
309 nlh->nlmsg_type = NLMSG_ERROR;
310 nlh->nlmsg_len = NLMSG_LENGTH(sizeof(struct nlmsgerr));
311 skb_trim(skb, nlh->nlmsg_len);
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700312 e = NLMSG_DATA(nlh);
313 e->error = -ETIMEDOUT;
314 memset(&e->msg, 0, sizeof(e->msg));
Thomas Graf2942e902006-08-15 00:30:25 -0700315
316 rtnl_unicast(skb, NETLINK_CB(skb).pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 } else
318 kfree_skb(skb);
319 }
320
321 kmem_cache_free(mrt_cachep, c);
322}
323
324
325/* Single timer process for all the unresolved queue. */
326
327static void ipmr_expire_process(unsigned long dummy)
328{
329 unsigned long now;
330 unsigned long expires;
331 struct mfc_cache *c, **cp;
332
333 if (!spin_trylock(&mfc_unres_lock)) {
334 mod_timer(&ipmr_expire_timer, jiffies+HZ/10);
335 return;
336 }
337
338 if (atomic_read(&cache_resolve_queue_len) == 0)
339 goto out;
340
341 now = jiffies;
342 expires = 10*HZ;
343 cp = &mfc_unres_queue;
344
345 while ((c=*cp) != NULL) {
346 if (time_after(c->mfc_un.unres.expires, now)) {
347 unsigned long interval = c->mfc_un.unres.expires - now;
348 if (interval < expires)
349 expires = interval;
350 cp = &c->next;
351 continue;
352 }
353
354 *cp = c->next;
355
356 ipmr_destroy_unres(c);
357 }
358
359 if (atomic_read(&cache_resolve_queue_len))
360 mod_timer(&ipmr_expire_timer, jiffies + expires);
361
362out:
363 spin_unlock(&mfc_unres_lock);
364}
365
366/* Fill oifs list. It is called under write locked mrt_lock. */
367
Baruch Evend1b04c02005-07-30 17:41:59 -0700368static void ipmr_update_thresholds(struct mfc_cache *cache, unsigned char *ttls)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369{
370 int vifi;
371
372 cache->mfc_un.res.minvif = MAXVIFS;
373 cache->mfc_un.res.maxvif = 0;
374 memset(cache->mfc_un.res.ttls, 255, MAXVIFS);
375
376 for (vifi=0; vifi<maxvif; vifi++) {
377 if (VIF_EXISTS(vifi) && ttls[vifi] && ttls[vifi] < 255) {
378 cache->mfc_un.res.ttls[vifi] = ttls[vifi];
379 if (cache->mfc_un.res.minvif > vifi)
380 cache->mfc_un.res.minvif = vifi;
381 if (cache->mfc_un.res.maxvif <= vifi)
382 cache->mfc_un.res.maxvif = vifi + 1;
383 }
384 }
385}
386
387static int vif_add(struct vifctl *vifc, int mrtsock)
388{
389 int vifi = vifc->vifc_vifi;
390 struct vif_device *v = &vif_table[vifi];
391 struct net_device *dev;
392 struct in_device *in_dev;
393
394 /* Is vif busy ? */
395 if (VIF_EXISTS(vifi))
396 return -EADDRINUSE;
397
398 switch (vifc->vifc_flags) {
399#ifdef CONFIG_IP_PIMSM
400 case VIFF_REGISTER:
401 /*
402 * Special Purpose VIF in PIM
403 * All the packets will be sent to the daemon
404 */
405 if (reg_vif_num >= 0)
406 return -EADDRINUSE;
407 dev = ipmr_reg_vif();
408 if (!dev)
409 return -ENOBUFS;
410 break;
411#endif
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900412 case VIFF_TUNNEL:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 dev = ipmr_new_tunnel(vifc);
414 if (!dev)
415 return -ENOBUFS;
416 break;
417 case 0:
Stephen Hemminger15333062006-03-20 22:32:28 -0800418 dev = ip_dev_find(vifc->vifc_lcl_addr.s_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 if (!dev)
420 return -EADDRNOTAVAIL;
Stephen Hemminger15333062006-03-20 22:32:28 -0800421 dev_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 break;
423 default:
424 return -EINVAL;
425 }
426
Herbert Xue5ed6392005-10-03 14:35:55 -0700427 if ((in_dev = __in_dev_get_rtnl(dev)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 return -EADDRNOTAVAIL;
Herbert Xu42f811b2007-06-04 23:34:44 -0700429 IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 dev_set_allmulti(dev, +1);
431 ip_rt_multicast_event(in_dev);
432
433 /*
434 * Fill in the VIF structures
435 */
436 v->rate_limit=vifc->vifc_rate_limit;
437 v->local=vifc->vifc_lcl_addr.s_addr;
438 v->remote=vifc->vifc_rmt_addr.s_addr;
439 v->flags=vifc->vifc_flags;
440 if (!mrtsock)
441 v->flags |= VIFF_STATIC;
442 v->threshold=vifc->vifc_threshold;
443 v->bytes_in = 0;
444 v->bytes_out = 0;
445 v->pkt_in = 0;
446 v->pkt_out = 0;
447 v->link = dev->ifindex;
448 if (v->flags&(VIFF_TUNNEL|VIFF_REGISTER))
449 v->link = dev->iflink;
450
451 /* And finish update writing critical data */
452 write_lock_bh(&mrt_lock);
453 dev_hold(dev);
454 v->dev=dev;
455#ifdef CONFIG_IP_PIMSM
456 if (v->flags&VIFF_REGISTER)
457 reg_vif_num = vifi;
458#endif
459 if (vifi+1 > maxvif)
460 maxvif = vifi+1;
461 write_unlock_bh(&mrt_lock);
462 return 0;
463}
464
Al Viro114c7842006-09-27 18:39:29 -0700465static struct mfc_cache *ipmr_cache_find(__be32 origin, __be32 mcastgrp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466{
467 int line=MFC_HASH(mcastgrp,origin);
468 struct mfc_cache *c;
469
470 for (c=mfc_cache_array[line]; c; c = c->next) {
471 if (c->mfc_origin==origin && c->mfc_mcastgrp==mcastgrp)
472 break;
473 }
474 return c;
475}
476
477/*
478 * Allocate a multicast cache entry
479 */
480static struct mfc_cache *ipmr_cache_alloc(void)
481{
Robert P. J. Dayc3762222007-02-10 01:45:03 -0800482 struct mfc_cache *c=kmem_cache_zalloc(mrt_cachep, GFP_KERNEL);
Stephen Hemminger132adf52007-03-08 20:44:43 -0800483 if (c==NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 c->mfc_un.res.minvif = MAXVIFS;
486 return c;
487}
488
489static struct mfc_cache *ipmr_cache_alloc_unres(void)
490{
Robert P. J. Dayc3762222007-02-10 01:45:03 -0800491 struct mfc_cache *c=kmem_cache_zalloc(mrt_cachep, GFP_ATOMIC);
Stephen Hemminger132adf52007-03-08 20:44:43 -0800492 if (c==NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 skb_queue_head_init(&c->mfc_un.unres.unresolved);
495 c->mfc_un.unres.expires = jiffies + 10*HZ;
496 return c;
497}
498
499/*
500 * A cache entry has gone into a resolved state from queued
501 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900502
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503static void ipmr_cache_resolve(struct mfc_cache *uc, struct mfc_cache *c)
504{
505 struct sk_buff *skb;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700506 struct nlmsgerr *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
508 /*
509 * Play the pending entries through our router
510 */
511
Stephen Hemminger132adf52007-03-08 20:44:43 -0800512 while ((skb=__skb_dequeue(&uc->mfc_un.unres.unresolved))) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700513 if (ip_hdr(skb)->version == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 struct nlmsghdr *nlh = (struct nlmsghdr *)skb_pull(skb, sizeof(struct iphdr));
515
516 if (ipmr_fill_mroute(skb, c, NLMSG_DATA(nlh)) > 0) {
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700517 nlh->nlmsg_len = (skb_tail_pointer(skb) -
518 (u8 *)nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 } else {
520 nlh->nlmsg_type = NLMSG_ERROR;
521 nlh->nlmsg_len = NLMSG_LENGTH(sizeof(struct nlmsgerr));
522 skb_trim(skb, nlh->nlmsg_len);
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700523 e = NLMSG_DATA(nlh);
524 e->error = -EMSGSIZE;
525 memset(&e->msg, 0, sizeof(e->msg));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 }
Thomas Graf2942e902006-08-15 00:30:25 -0700527
528 rtnl_unicast(skb, NETLINK_CB(skb).pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 } else
530 ip_mr_forward(skb, c, 0);
531 }
532}
533
534/*
535 * Bounce a cache query up to mrouted. We could use netlink for this but mrouted
536 * expects the following bizarre scheme.
537 *
538 * Called under mrt_lock.
539 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900540
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541static int ipmr_cache_report(struct sk_buff *pkt, vifi_t vifi, int assert)
542{
543 struct sk_buff *skb;
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -0300544 const int ihl = ip_hdrlen(pkt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 struct igmphdr *igmp;
546 struct igmpmsg *msg;
547 int ret;
548
549#ifdef CONFIG_IP_PIMSM
550 if (assert == IGMPMSG_WHOLEPKT)
551 skb = skb_realloc_headroom(pkt, sizeof(struct iphdr));
552 else
553#endif
554 skb = alloc_skb(128, GFP_ATOMIC);
555
Stephen Hemminger132adf52007-03-08 20:44:43 -0800556 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 return -ENOBUFS;
558
559#ifdef CONFIG_IP_PIMSM
560 if (assert == IGMPMSG_WHOLEPKT) {
561 /* Ugly, but we have no choice with this interface.
562 Duplicate old header, fix ihl, length etc.
563 And all this only to mangle msg->im_msgtype and
564 to set msg->im_mbz to "mbz" :-)
565 */
Arnaldo Carvalho de Melo878c8142007-03-11 22:38:29 -0300566 skb_push(skb, sizeof(struct iphdr));
567 skb_reset_network_header(skb);
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -0300568 skb_reset_transport_header(skb);
Arnaldo Carvalho de Melo0272ffc2007-03-12 20:05:39 -0300569 msg = (struct igmpmsg *)skb_network_header(skb);
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700570 memcpy(msg, skb_network_header(pkt), sizeof(struct iphdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 msg->im_msgtype = IGMPMSG_WHOLEPKT;
572 msg->im_mbz = 0;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900573 msg->im_vif = reg_vif_num;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700574 ip_hdr(skb)->ihl = sizeof(struct iphdr) >> 2;
575 ip_hdr(skb)->tot_len = htons(ntohs(ip_hdr(pkt)->tot_len) +
576 sizeof(struct iphdr));
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900577 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578#endif
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900579 {
580
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 /*
582 * Copy the IP header
583 */
584
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700585 skb->network_header = skb->tail;
Arnaldo Carvalho de Meloddc7b8e2007-03-15 21:42:27 -0300586 skb_put(skb, ihl);
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -0300587 skb_copy_to_linear_data(skb, pkt->data, ihl);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700588 ip_hdr(skb)->protocol = 0; /* Flag to the kernel this is a route add */
589 msg = (struct igmpmsg *)skb_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 msg->im_vif = vifi;
591 skb->dst = dst_clone(pkt->dst);
592
593 /*
594 * Add our header
595 */
596
597 igmp=(struct igmphdr *)skb_put(skb,sizeof(struct igmphdr));
598 igmp->type =
599 msg->im_msgtype = assert;
600 igmp->code = 0;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700601 ip_hdr(skb)->tot_len = htons(skb->len); /* Fix the length */
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700602 skb->transport_header = skb->network_header;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900603 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
605 if (mroute_socket == NULL) {
606 kfree_skb(skb);
607 return -EINVAL;
608 }
609
610 /*
611 * Deliver to mrouted
612 */
613 if ((ret=sock_queue_rcv_skb(mroute_socket,skb))<0) {
614 if (net_ratelimit())
615 printk(KERN_WARNING "mroute: pending queue full, dropping entries.\n");
616 kfree_skb(skb);
617 }
618
619 return ret;
620}
621
622/*
623 * Queue a packet for resolution. It gets locked cache entry!
624 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900625
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626static int
627ipmr_cache_unresolved(vifi_t vifi, struct sk_buff *skb)
628{
629 int err;
630 struct mfc_cache *c;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700631 const struct iphdr *iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
633 spin_lock_bh(&mfc_unres_lock);
634 for (c=mfc_unres_queue; c; c=c->next) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700635 if (c->mfc_mcastgrp == iph->daddr &&
636 c->mfc_origin == iph->saddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 break;
638 }
639
640 if (c == NULL) {
641 /*
642 * Create a new entry if allowable
643 */
644
645 if (atomic_read(&cache_resolve_queue_len)>=10 ||
646 (c=ipmr_cache_alloc_unres())==NULL) {
647 spin_unlock_bh(&mfc_unres_lock);
648
649 kfree_skb(skb);
650 return -ENOBUFS;
651 }
652
653 /*
654 * Fill in the new cache entry
655 */
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700656 c->mfc_parent = -1;
657 c->mfc_origin = iph->saddr;
658 c->mfc_mcastgrp = iph->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
660 /*
661 * Reflect first query at mrouted.
662 */
663 if ((err = ipmr_cache_report(skb, vifi, IGMPMSG_NOCACHE))<0) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900664 /* If the report failed throw the cache entry
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 out - Brad Parker
666 */
667 spin_unlock_bh(&mfc_unres_lock);
668
669 kmem_cache_free(mrt_cachep, c);
670 kfree_skb(skb);
671 return err;
672 }
673
674 atomic_inc(&cache_resolve_queue_len);
675 c->next = mfc_unres_queue;
676 mfc_unres_queue = c;
677
678 mod_timer(&ipmr_expire_timer, c->mfc_un.unres.expires);
679 }
680
681 /*
682 * See if we can append the packet
683 */
684 if (c->mfc_un.unres.unresolved.qlen>3) {
685 kfree_skb(skb);
686 err = -ENOBUFS;
687 } else {
688 skb_queue_tail(&c->mfc_un.unres.unresolved,skb);
689 err = 0;
690 }
691
692 spin_unlock_bh(&mfc_unres_lock);
693 return err;
694}
695
696/*
697 * MFC cache manipulation by user space mroute daemon
698 */
699
700static int ipmr_mfc_delete(struct mfcctl *mfc)
701{
702 int line;
703 struct mfc_cache *c, **cp;
704
705 line=MFC_HASH(mfc->mfcc_mcastgrp.s_addr, mfc->mfcc_origin.s_addr);
706
707 for (cp=&mfc_cache_array[line]; (c=*cp) != NULL; cp = &c->next) {
708 if (c->mfc_origin == mfc->mfcc_origin.s_addr &&
709 c->mfc_mcastgrp == mfc->mfcc_mcastgrp.s_addr) {
710 write_lock_bh(&mrt_lock);
711 *cp = c->next;
712 write_unlock_bh(&mrt_lock);
713
714 kmem_cache_free(mrt_cachep, c);
715 return 0;
716 }
717 }
718 return -ENOENT;
719}
720
721static int ipmr_mfc_add(struct mfcctl *mfc, int mrtsock)
722{
723 int line;
724 struct mfc_cache *uc, *c, **cp;
725
726 line=MFC_HASH(mfc->mfcc_mcastgrp.s_addr, mfc->mfcc_origin.s_addr);
727
728 for (cp=&mfc_cache_array[line]; (c=*cp) != NULL; cp = &c->next) {
729 if (c->mfc_origin == mfc->mfcc_origin.s_addr &&
730 c->mfc_mcastgrp == mfc->mfcc_mcastgrp.s_addr)
731 break;
732 }
733
734 if (c != NULL) {
735 write_lock_bh(&mrt_lock);
736 c->mfc_parent = mfc->mfcc_parent;
Baruch Evend1b04c02005-07-30 17:41:59 -0700737 ipmr_update_thresholds(c, mfc->mfcc_ttls);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 if (!mrtsock)
739 c->mfc_flags |= MFC_STATIC;
740 write_unlock_bh(&mrt_lock);
741 return 0;
742 }
743
Stephen Hemminger132adf52007-03-08 20:44:43 -0800744 if (!MULTICAST(mfc->mfcc_mcastgrp.s_addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 return -EINVAL;
746
747 c=ipmr_cache_alloc();
748 if (c==NULL)
749 return -ENOMEM;
750
751 c->mfc_origin=mfc->mfcc_origin.s_addr;
752 c->mfc_mcastgrp=mfc->mfcc_mcastgrp.s_addr;
753 c->mfc_parent=mfc->mfcc_parent;
Baruch Evend1b04c02005-07-30 17:41:59 -0700754 ipmr_update_thresholds(c, mfc->mfcc_ttls);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 if (!mrtsock)
756 c->mfc_flags |= MFC_STATIC;
757
758 write_lock_bh(&mrt_lock);
759 c->next = mfc_cache_array[line];
760 mfc_cache_array[line] = c;
761 write_unlock_bh(&mrt_lock);
762
763 /*
764 * Check to see if we resolved a queued list. If so we
765 * need to send on the frames and tidy up.
766 */
767 spin_lock_bh(&mfc_unres_lock);
768 for (cp = &mfc_unres_queue; (uc=*cp) != NULL;
769 cp = &uc->next) {
770 if (uc->mfc_origin == c->mfc_origin &&
771 uc->mfc_mcastgrp == c->mfc_mcastgrp) {
772 *cp = uc->next;
773 if (atomic_dec_and_test(&cache_resolve_queue_len))
774 del_timer(&ipmr_expire_timer);
775 break;
776 }
777 }
778 spin_unlock_bh(&mfc_unres_lock);
779
780 if (uc) {
781 ipmr_cache_resolve(uc, c);
782 kmem_cache_free(mrt_cachep, uc);
783 }
784 return 0;
785}
786
787/*
788 * Close the multicast socket, and clear the vif tables etc
789 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900790
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791static void mroute_clean_tables(struct sock *sk)
792{
793 int i;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900794
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 /*
796 * Shut down all active vif entries
797 */
Stephen Hemminger132adf52007-03-08 20:44:43 -0800798 for (i=0; i<maxvif; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 if (!(vif_table[i].flags&VIFF_STATIC))
800 vif_delete(i);
801 }
802
803 /*
804 * Wipe the cache
805 */
806 for (i=0;i<MFC_LINES;i++) {
807 struct mfc_cache *c, **cp;
808
809 cp = &mfc_cache_array[i];
810 while ((c = *cp) != NULL) {
811 if (c->mfc_flags&MFC_STATIC) {
812 cp = &c->next;
813 continue;
814 }
815 write_lock_bh(&mrt_lock);
816 *cp = c->next;
817 write_unlock_bh(&mrt_lock);
818
819 kmem_cache_free(mrt_cachep, c);
820 }
821 }
822
823 if (atomic_read(&cache_resolve_queue_len) != 0) {
824 struct mfc_cache *c;
825
826 spin_lock_bh(&mfc_unres_lock);
827 while (mfc_unres_queue != NULL) {
828 c = mfc_unres_queue;
829 mfc_unres_queue = c->next;
830 spin_unlock_bh(&mfc_unres_lock);
831
832 ipmr_destroy_unres(c);
833
834 spin_lock_bh(&mfc_unres_lock);
835 }
836 spin_unlock_bh(&mfc_unres_lock);
837 }
838}
839
840static void mrtsock_destruct(struct sock *sk)
841{
842 rtnl_lock();
843 if (sk == mroute_socket) {
Herbert Xu42f811b2007-06-04 23:34:44 -0700844 IPV4_DEVCONF_ALL(MC_FORWARDING)--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845
846 write_lock_bh(&mrt_lock);
847 mroute_socket=NULL;
848 write_unlock_bh(&mrt_lock);
849
850 mroute_clean_tables(sk);
851 }
852 rtnl_unlock();
853}
854
855/*
856 * Socket options and virtual interface manipulation. The whole
857 * virtual interface system is a complete heap, but unfortunately
858 * that's how BSD mrouted happens to think. Maybe one day with a proper
859 * MOSPF/PIM router set up we can clean this up.
860 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900861
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862int ip_mroute_setsockopt(struct sock *sk,int optname,char __user *optval,int optlen)
863{
864 int ret;
865 struct vifctl vif;
866 struct mfcctl mfc;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900867
Stephen Hemminger132adf52007-03-08 20:44:43 -0800868 if (optname != MRT_INIT) {
869 if (sk != mroute_socket && !capable(CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 return -EACCES;
871 }
872
Stephen Hemminger132adf52007-03-08 20:44:43 -0800873 switch (optname) {
874 case MRT_INIT:
875 if (sk->sk_type != SOCK_RAW ||
876 inet_sk(sk)->num != IPPROTO_IGMP)
877 return -EOPNOTSUPP;
878 if (optlen!=sizeof(int))
879 return -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880
Stephen Hemminger132adf52007-03-08 20:44:43 -0800881 rtnl_lock();
882 if (mroute_socket) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 rtnl_unlock();
Stephen Hemminger132adf52007-03-08 20:44:43 -0800884 return -EADDRINUSE;
885 }
886
887 ret = ip_ra_control(sk, 1, mrtsock_destruct);
888 if (ret == 0) {
889 write_lock_bh(&mrt_lock);
890 mroute_socket=sk;
891 write_unlock_bh(&mrt_lock);
892
Herbert Xu42f811b2007-06-04 23:34:44 -0700893 IPV4_DEVCONF_ALL(MC_FORWARDING)++;
Stephen Hemminger132adf52007-03-08 20:44:43 -0800894 }
895 rtnl_unlock();
896 return ret;
897 case MRT_DONE:
898 if (sk!=mroute_socket)
899 return -EACCES;
900 return ip_ra_control(sk, 0, NULL);
901 case MRT_ADD_VIF:
902 case MRT_DEL_VIF:
903 if (optlen!=sizeof(vif))
904 return -EINVAL;
905 if (copy_from_user(&vif,optval,sizeof(vif)))
906 return -EFAULT;
907 if (vif.vifc_vifi >= MAXVIFS)
908 return -ENFILE;
909 rtnl_lock();
910 if (optname==MRT_ADD_VIF) {
911 ret = vif_add(&vif, sk==mroute_socket);
912 } else {
913 ret = vif_delete(vif.vifc_vifi);
914 }
915 rtnl_unlock();
916 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917
918 /*
919 * Manipulate the forwarding caches. These live
920 * in a sort of kernel/user symbiosis.
921 */
Stephen Hemminger132adf52007-03-08 20:44:43 -0800922 case MRT_ADD_MFC:
923 case MRT_DEL_MFC:
924 if (optlen!=sizeof(mfc))
925 return -EINVAL;
926 if (copy_from_user(&mfc,optval, sizeof(mfc)))
927 return -EFAULT;
928 rtnl_lock();
929 if (optname==MRT_DEL_MFC)
930 ret = ipmr_mfc_delete(&mfc);
931 else
932 ret = ipmr_mfc_add(&mfc, sk==mroute_socket);
933 rtnl_unlock();
934 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 /*
936 * Control PIM assert.
937 */
Stephen Hemminger132adf52007-03-08 20:44:43 -0800938 case MRT_ASSERT:
939 {
940 int v;
941 if (get_user(v,(int __user *)optval))
942 return -EFAULT;
943 mroute_do_assert=(v)?1:0;
944 return 0;
945 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946#ifdef CONFIG_IP_PIMSM
Stephen Hemminger132adf52007-03-08 20:44:43 -0800947 case MRT_PIM:
948 {
949 int v, ret;
950 if (get_user(v,(int __user *)optval))
951 return -EFAULT;
952 v = (v)?1:0;
953 rtnl_lock();
954 ret = 0;
955 if (v != mroute_do_pim) {
956 mroute_do_pim = v;
957 mroute_do_assert = v;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958#ifdef CONFIG_IP_PIMSM_V2
Stephen Hemminger132adf52007-03-08 20:44:43 -0800959 if (mroute_do_pim)
960 ret = inet_add_protocol(&pim_protocol,
961 IPPROTO_PIM);
962 else
963 ret = inet_del_protocol(&pim_protocol,
964 IPPROTO_PIM);
965 if (ret < 0)
966 ret = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 }
Stephen Hemminger132adf52007-03-08 20:44:43 -0800969 rtnl_unlock();
970 return ret;
971 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972#endif
Stephen Hemminger132adf52007-03-08 20:44:43 -0800973 /*
974 * Spurious command, or MRT_VERSION which you cannot
975 * set.
976 */
977 default:
978 return -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 }
980}
981
982/*
983 * Getsock opt support for the multicast routing system.
984 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900985
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986int ip_mroute_getsockopt(struct sock *sk,int optname,char __user *optval,int __user *optlen)
987{
988 int olr;
989 int val;
990
Stephen Hemminger132adf52007-03-08 20:44:43 -0800991 if (optname!=MRT_VERSION &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992#ifdef CONFIG_IP_PIMSM
993 optname!=MRT_PIM &&
994#endif
995 optname!=MRT_ASSERT)
996 return -ENOPROTOOPT;
997
998 if (get_user(olr, optlen))
999 return -EFAULT;
1000
1001 olr = min_t(unsigned int, olr, sizeof(int));
1002 if (olr < 0)
1003 return -EINVAL;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001004
Stephen Hemminger132adf52007-03-08 20:44:43 -08001005 if (put_user(olr,optlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 return -EFAULT;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001007 if (optname==MRT_VERSION)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 val=0x0305;
1009#ifdef CONFIG_IP_PIMSM
Stephen Hemminger132adf52007-03-08 20:44:43 -08001010 else if (optname==MRT_PIM)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 val=mroute_do_pim;
1012#endif
1013 else
1014 val=mroute_do_assert;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001015 if (copy_to_user(optval,&val,olr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 return -EFAULT;
1017 return 0;
1018}
1019
1020/*
1021 * The IP multicast ioctl support routines.
1022 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001023
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024int ipmr_ioctl(struct sock *sk, int cmd, void __user *arg)
1025{
1026 struct sioc_sg_req sr;
1027 struct sioc_vif_req vr;
1028 struct vif_device *vif;
1029 struct mfc_cache *c;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001030
Stephen Hemminger132adf52007-03-08 20:44:43 -08001031 switch (cmd) {
1032 case SIOCGETVIFCNT:
1033 if (copy_from_user(&vr,arg,sizeof(vr)))
1034 return -EFAULT;
1035 if (vr.vifi>=maxvif)
1036 return -EINVAL;
1037 read_lock(&mrt_lock);
1038 vif=&vif_table[vr.vifi];
1039 if (VIF_EXISTS(vr.vifi)) {
1040 vr.icount=vif->pkt_in;
1041 vr.ocount=vif->pkt_out;
1042 vr.ibytes=vif->bytes_in;
1043 vr.obytes=vif->bytes_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 read_unlock(&mrt_lock);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001045
1046 if (copy_to_user(arg,&vr,sizeof(vr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 return -EFAULT;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001048 return 0;
1049 }
1050 read_unlock(&mrt_lock);
1051 return -EADDRNOTAVAIL;
1052 case SIOCGETSGCNT:
1053 if (copy_from_user(&sr,arg,sizeof(sr)))
1054 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055
Stephen Hemminger132adf52007-03-08 20:44:43 -08001056 read_lock(&mrt_lock);
1057 c = ipmr_cache_find(sr.src.s_addr, sr.grp.s_addr);
1058 if (c) {
1059 sr.pktcnt = c->mfc_un.res.pkt;
1060 sr.bytecnt = c->mfc_un.res.bytes;
1061 sr.wrong_if = c->mfc_un.res.wrong_if;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 read_unlock(&mrt_lock);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001063
1064 if (copy_to_user(arg,&sr,sizeof(sr)))
1065 return -EFAULT;
1066 return 0;
1067 }
1068 read_unlock(&mrt_lock);
1069 return -EADDRNOTAVAIL;
1070 default:
1071 return -ENOIOCTLCMD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 }
1073}
1074
1075
1076static int ipmr_device_event(struct notifier_block *this, unsigned long event, void *ptr)
1077{
1078 struct vif_device *v;
1079 int ct;
1080 if (event != NETDEV_UNREGISTER)
1081 return NOTIFY_DONE;
1082 v=&vif_table[0];
Stephen Hemminger132adf52007-03-08 20:44:43 -08001083 for (ct=0;ct<maxvif;ct++,v++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 if (v->dev==ptr)
1085 vif_delete(ct);
1086 }
1087 return NOTIFY_DONE;
1088}
1089
1090
1091static struct notifier_block ip_mr_notifier={
1092 .notifier_call = ipmr_device_event,
1093};
1094
1095/*
1096 * Encapsulate a packet by attaching a valid IPIP header to it.
1097 * This avoids tunnel drivers and other mess and gives us the speed so
1098 * important for multicast video.
1099 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001100
Al Viro114c7842006-09-27 18:39:29 -07001101static void ip_encap(struct sk_buff *skb, __be32 saddr, __be32 daddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102{
Arnaldo Carvalho de Melo8856dfa2007-03-10 19:40:39 -03001103 struct iphdr *iph;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001104 struct iphdr *old_iph = ip_hdr(skb);
Arnaldo Carvalho de Melo8856dfa2007-03-10 19:40:39 -03001105
1106 skb_push(skb, sizeof(struct iphdr));
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07001107 skb->transport_header = skb->network_header;
Arnaldo Carvalho de Melo8856dfa2007-03-10 19:40:39 -03001108 skb_reset_network_header(skb);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001109 iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110
1111 iph->version = 4;
Arnaldo Carvalho de Meloe023dd62007-03-12 20:09:36 -03001112 iph->tos = old_iph->tos;
1113 iph->ttl = old_iph->ttl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 iph->frag_off = 0;
1115 iph->daddr = daddr;
1116 iph->saddr = saddr;
1117 iph->protocol = IPPROTO_IPIP;
1118 iph->ihl = 5;
1119 iph->tot_len = htons(skb->len);
1120 ip_select_ident(iph, skb->dst, NULL);
1121 ip_send_check(iph);
1122
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1124 nf_reset(skb);
1125}
1126
1127static inline int ipmr_forward_finish(struct sk_buff *skb)
1128{
1129 struct ip_options * opt = &(IPCB(skb)->opt);
1130
1131 IP_INC_STATS_BH(IPSTATS_MIB_OUTFORWDATAGRAMS);
1132
1133 if (unlikely(opt->optlen))
1134 ip_forward_options(skb);
1135
1136 return dst_output(skb);
1137}
1138
1139/*
1140 * Processing handlers for ipmr_forward
1141 */
1142
1143static void ipmr_queue_xmit(struct sk_buff *skb, struct mfc_cache *c, int vifi)
1144{
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001145 const struct iphdr *iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 struct vif_device *vif = &vif_table[vifi];
1147 struct net_device *dev;
1148 struct rtable *rt;
1149 int encap = 0;
1150
1151 if (vif->dev == NULL)
1152 goto out_free;
1153
1154#ifdef CONFIG_IP_PIMSM
1155 if (vif->flags & VIFF_REGISTER) {
1156 vif->pkt_out++;
1157 vif->bytes_out+=skb->len;
Patrick McHardy2941a482006-01-08 22:05:26 -08001158 ((struct net_device_stats*)netdev_priv(vif->dev))->tx_bytes += skb->len;
1159 ((struct net_device_stats*)netdev_priv(vif->dev))->tx_packets++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 ipmr_cache_report(skb, vifi, IGMPMSG_WHOLEPKT);
1161 kfree_skb(skb);
1162 return;
1163 }
1164#endif
1165
1166 if (vif->flags&VIFF_TUNNEL) {
1167 struct flowi fl = { .oif = vif->link,
1168 .nl_u = { .ip4_u =
1169 { .daddr = vif->remote,
1170 .saddr = vif->local,
1171 .tos = RT_TOS(iph->tos) } },
1172 .proto = IPPROTO_IPIP };
1173 if (ip_route_output_key(&rt, &fl))
1174 goto out_free;
1175 encap = sizeof(struct iphdr);
1176 } else {
1177 struct flowi fl = { .oif = vif->link,
1178 .nl_u = { .ip4_u =
1179 { .daddr = iph->daddr,
1180 .tos = RT_TOS(iph->tos) } },
1181 .proto = IPPROTO_IPIP };
1182 if (ip_route_output_key(&rt, &fl))
1183 goto out_free;
1184 }
1185
1186 dev = rt->u.dst.dev;
1187
1188 if (skb->len+encap > dst_mtu(&rt->u.dst) && (ntohs(iph->frag_off) & IP_DF)) {
1189 /* Do not fragment multicasts. Alas, IPv4 does not
1190 allow to send ICMP, so that packets will disappear
1191 to blackhole.
1192 */
1193
1194 IP_INC_STATS_BH(IPSTATS_MIB_FRAGFAILS);
1195 ip_rt_put(rt);
1196 goto out_free;
1197 }
1198
1199 encap += LL_RESERVED_SPACE(dev) + rt->u.dst.header_len;
1200
1201 if (skb_cow(skb, encap)) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001202 ip_rt_put(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 goto out_free;
1204 }
1205
1206 vif->pkt_out++;
1207 vif->bytes_out+=skb->len;
1208
1209 dst_release(skb->dst);
1210 skb->dst = &rt->u.dst;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001211 ip_decrease_ttl(ip_hdr(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212
1213 /* FIXME: forward and output firewalls used to be called here.
1214 * What do we do with netfilter? -- RR */
1215 if (vif->flags & VIFF_TUNNEL) {
1216 ip_encap(skb, vif->local, vif->remote);
1217 /* FIXME: extra output firewall step used to be here. --RR */
Patrick McHardy2941a482006-01-08 22:05:26 -08001218 ((struct ip_tunnel *)netdev_priv(vif->dev))->stat.tx_packets++;
1219 ((struct ip_tunnel *)netdev_priv(vif->dev))->stat.tx_bytes+=skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 }
1221
1222 IPCB(skb)->flags |= IPSKB_FORWARDED;
1223
1224 /*
1225 * RFC1584 teaches, that DVMRP/PIM router must deliver packets locally
1226 * not only before forwarding, but after forwarding on all output
1227 * interfaces. It is clear, if mrouter runs a multicasting
1228 * program, it should receive packets not depending to what interface
1229 * program is joined.
1230 * If we will not make it, the program will have to join on all
1231 * interfaces. On the other hand, multihoming host (or router, but
1232 * not mrouter) cannot join to more than one interface - it will
1233 * result in receiving multiple packets.
1234 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001235 NF_HOOK(PF_INET, NF_IP_FORWARD, skb, skb->dev, dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 ipmr_forward_finish);
1237 return;
1238
1239out_free:
1240 kfree_skb(skb);
1241 return;
1242}
1243
1244static int ipmr_find_vif(struct net_device *dev)
1245{
1246 int ct;
1247 for (ct=maxvif-1; ct>=0; ct--) {
1248 if (vif_table[ct].dev == dev)
1249 break;
1250 }
1251 return ct;
1252}
1253
1254/* "local" means that we should preserve one skb (for local delivery) */
1255
1256static int ip_mr_forward(struct sk_buff *skb, struct mfc_cache *cache, int local)
1257{
1258 int psend = -1;
1259 int vif, ct;
1260
1261 vif = cache->mfc_parent;
1262 cache->mfc_un.res.pkt++;
1263 cache->mfc_un.res.bytes += skb->len;
1264
1265 /*
1266 * Wrong interface: drop packet and (maybe) send PIM assert.
1267 */
1268 if (vif_table[vif].dev != skb->dev) {
1269 int true_vifi;
1270
1271 if (((struct rtable*)skb->dst)->fl.iif == 0) {
1272 /* It is our own packet, looped back.
1273 Very complicated situation...
1274
1275 The best workaround until routing daemons will be
1276 fixed is not to redistribute packet, if it was
1277 send through wrong interface. It means, that
1278 multicast applications WILL NOT work for
1279 (S,G), which have default multicast route pointing
1280 to wrong oif. In any case, it is not a good
1281 idea to use multicasting applications on router.
1282 */
1283 goto dont_forward;
1284 }
1285
1286 cache->mfc_un.res.wrong_if++;
1287 true_vifi = ipmr_find_vif(skb->dev);
1288
1289 if (true_vifi >= 0 && mroute_do_assert &&
1290 /* pimsm uses asserts, when switching from RPT to SPT,
1291 so that we cannot check that packet arrived on an oif.
1292 It is bad, but otherwise we would need to move pretty
1293 large chunk of pimd to kernel. Ough... --ANK
1294 */
1295 (mroute_do_pim || cache->mfc_un.res.ttls[true_vifi] < 255) &&
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001296 time_after(jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 cache->mfc_un.res.last_assert + MFC_ASSERT_THRESH)) {
1298 cache->mfc_un.res.last_assert = jiffies;
1299 ipmr_cache_report(skb, true_vifi, IGMPMSG_WRONGVIF);
1300 }
1301 goto dont_forward;
1302 }
1303
1304 vif_table[vif].pkt_in++;
1305 vif_table[vif].bytes_in+=skb->len;
1306
1307 /*
1308 * Forward the frame
1309 */
1310 for (ct = cache->mfc_un.res.maxvif-1; ct >= cache->mfc_un.res.minvif; ct--) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001311 if (ip_hdr(skb)->ttl > cache->mfc_un.res.ttls[ct]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 if (psend != -1) {
1313 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
1314 if (skb2)
1315 ipmr_queue_xmit(skb2, cache, psend);
1316 }
1317 psend=ct;
1318 }
1319 }
1320 if (psend != -1) {
1321 if (local) {
1322 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
1323 if (skb2)
1324 ipmr_queue_xmit(skb2, cache, psend);
1325 } else {
1326 ipmr_queue_xmit(skb, cache, psend);
1327 return 0;
1328 }
1329 }
1330
1331dont_forward:
1332 if (!local)
1333 kfree_skb(skb);
1334 return 0;
1335}
1336
1337
1338/*
1339 * Multicast packets for forwarding arrive here
1340 */
1341
1342int ip_mr_input(struct sk_buff *skb)
1343{
1344 struct mfc_cache *cache;
1345 int local = ((struct rtable*)skb->dst)->rt_flags&RTCF_LOCAL;
1346
1347 /* Packet is looped back after forward, it should not be
1348 forwarded second time, but still can be delivered locally.
1349 */
1350 if (IPCB(skb)->flags&IPSKB_FORWARDED)
1351 goto dont_forward;
1352
1353 if (!local) {
1354 if (IPCB(skb)->opt.router_alert) {
1355 if (ip_call_ra_chain(skb))
1356 return 0;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001357 } else if (ip_hdr(skb)->protocol == IPPROTO_IGMP){
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 /* IGMPv1 (and broken IGMPv2 implementations sort of
1359 Cisco IOS <= 11.2(8)) do not put router alert
1360 option to IGMP packets destined to routable
1361 groups. It is very bad, because it means
1362 that we can forward NO IGMP messages.
1363 */
1364 read_lock(&mrt_lock);
1365 if (mroute_socket) {
Patrick McHardy2715bcf2005-06-21 14:06:24 -07001366 nf_reset(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 raw_rcv(mroute_socket, skb);
1368 read_unlock(&mrt_lock);
1369 return 0;
1370 }
1371 read_unlock(&mrt_lock);
1372 }
1373 }
1374
1375 read_lock(&mrt_lock);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001376 cache = ipmr_cache_find(ip_hdr(skb)->saddr, ip_hdr(skb)->daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377
1378 /*
1379 * No usable cache entry
1380 */
1381 if (cache==NULL) {
1382 int vif;
1383
1384 if (local) {
1385 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
1386 ip_local_deliver(skb);
1387 if (skb2 == NULL) {
1388 read_unlock(&mrt_lock);
1389 return -ENOBUFS;
1390 }
1391 skb = skb2;
1392 }
1393
1394 vif = ipmr_find_vif(skb->dev);
1395 if (vif >= 0) {
1396 int err = ipmr_cache_unresolved(vif, skb);
1397 read_unlock(&mrt_lock);
1398
1399 return err;
1400 }
1401 read_unlock(&mrt_lock);
1402 kfree_skb(skb);
1403 return -ENODEV;
1404 }
1405
1406 ip_mr_forward(skb, cache, local);
1407
1408 read_unlock(&mrt_lock);
1409
1410 if (local)
1411 return ip_local_deliver(skb);
1412
1413 return 0;
1414
1415dont_forward:
1416 if (local)
1417 return ip_local_deliver(skb);
1418 kfree_skb(skb);
1419 return 0;
1420}
1421
1422#ifdef CONFIG_IP_PIMSM_V1
1423/*
1424 * Handle IGMP messages of PIMv1
1425 */
1426
1427int pim_rcv_v1(struct sk_buff * skb)
1428{
1429 struct igmphdr *pim;
1430 struct iphdr *encap;
1431 struct net_device *reg_dev = NULL;
1432
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001433 if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(*encap)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 goto drop;
1435
Arnaldo Carvalho de Melod9edf9e2007-03-13 14:19:23 -03001436 pim = igmp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001438 if (!mroute_do_pim ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 skb->len < sizeof(*pim) + sizeof(*encap) ||
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001440 pim->group != PIM_V1_VERSION || pim->code != PIM_V1_REGISTER)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 goto drop;
1442
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -07001443 encap = (struct iphdr *)(skb_transport_header(skb) +
1444 sizeof(struct igmphdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 /*
1446 Check that:
1447 a. packet is really destinted to a multicast group
1448 b. packet is not a NULL-REGISTER
1449 c. packet is not truncated
1450 */
1451 if (!MULTICAST(encap->daddr) ||
1452 encap->tot_len == 0 ||
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001453 ntohs(encap->tot_len) + sizeof(*pim) > skb->len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 goto drop;
1455
1456 read_lock(&mrt_lock);
1457 if (reg_vif_num >= 0)
1458 reg_dev = vif_table[reg_vif_num].dev;
1459 if (reg_dev)
1460 dev_hold(reg_dev);
1461 read_unlock(&mrt_lock);
1462
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001463 if (reg_dev == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 goto drop;
1465
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07001466 skb->mac_header = skb->network_header;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 skb_pull(skb, (u8*)encap - skb->data);
Arnaldo Carvalho de Melo31c77112007-03-10 19:04:55 -03001468 skb_reset_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 skb->dev = reg_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 skb->protocol = htons(ETH_P_IP);
1471 skb->ip_summed = 0;
1472 skb->pkt_type = PACKET_HOST;
1473 dst_release(skb->dst);
1474 skb->dst = NULL;
Patrick McHardy2941a482006-01-08 22:05:26 -08001475 ((struct net_device_stats*)netdev_priv(reg_dev))->rx_bytes += skb->len;
1476 ((struct net_device_stats*)netdev_priv(reg_dev))->rx_packets++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 nf_reset(skb);
1478 netif_rx(skb);
1479 dev_put(reg_dev);
1480 return 0;
1481 drop:
1482 kfree_skb(skb);
1483 return 0;
1484}
1485#endif
1486
1487#ifdef CONFIG_IP_PIMSM_V2
1488static int pim_rcv(struct sk_buff * skb)
1489{
1490 struct pimreghdr *pim;
1491 struct iphdr *encap;
1492 struct net_device *reg_dev = NULL;
1493
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001494 if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(*encap)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 goto drop;
1496
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -07001497 pim = (struct pimreghdr *)skb_transport_header(skb);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001498 if (pim->type != ((PIM_VERSION<<4)|(PIM_REGISTER)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 (pim->flags&PIM_NULL_REGISTER) ||
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001500 (ip_compute_csum((void *)pim, sizeof(*pim)) != 0 &&
Al Virod3bc23e2006-11-14 21:24:49 -08001501 csum_fold(skb_checksum(skb, 0, skb->len, 0))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 goto drop;
1503
1504 /* check if the inner packet is destined to mcast group */
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -07001505 encap = (struct iphdr *)(skb_transport_header(skb) +
1506 sizeof(struct pimreghdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 if (!MULTICAST(encap->daddr) ||
1508 encap->tot_len == 0 ||
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001509 ntohs(encap->tot_len) + sizeof(*pim) > skb->len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 goto drop;
1511
1512 read_lock(&mrt_lock);
1513 if (reg_vif_num >= 0)
1514 reg_dev = vif_table[reg_vif_num].dev;
1515 if (reg_dev)
1516 dev_hold(reg_dev);
1517 read_unlock(&mrt_lock);
1518
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001519 if (reg_dev == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 goto drop;
1521
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07001522 skb->mac_header = skb->network_header;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 skb_pull(skb, (u8*)encap - skb->data);
Arnaldo Carvalho de Melo31c77112007-03-10 19:04:55 -03001524 skb_reset_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 skb->dev = reg_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 skb->protocol = htons(ETH_P_IP);
1527 skb->ip_summed = 0;
1528 skb->pkt_type = PACKET_HOST;
1529 dst_release(skb->dst);
Patrick McHardy2941a482006-01-08 22:05:26 -08001530 ((struct net_device_stats*)netdev_priv(reg_dev))->rx_bytes += skb->len;
1531 ((struct net_device_stats*)netdev_priv(reg_dev))->rx_packets++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 skb->dst = NULL;
1533 nf_reset(skb);
1534 netif_rx(skb);
1535 dev_put(reg_dev);
1536 return 0;
1537 drop:
1538 kfree_skb(skb);
1539 return 0;
1540}
1541#endif
1542
1543static int
1544ipmr_fill_mroute(struct sk_buff *skb, struct mfc_cache *c, struct rtmsg *rtm)
1545{
1546 int ct;
1547 struct rtnexthop *nhp;
1548 struct net_device *dev = vif_table[c->mfc_parent].dev;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001549 u8 *b = skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 struct rtattr *mp_head;
1551
1552 if (dev)
1553 RTA_PUT(skb, RTA_IIF, 4, &dev->ifindex);
1554
1555 mp_head = (struct rtattr*)skb_put(skb, RTA_LENGTH(0));
1556
1557 for (ct = c->mfc_un.res.minvif; ct < c->mfc_un.res.maxvif; ct++) {
1558 if (c->mfc_un.res.ttls[ct] < 255) {
1559 if (skb_tailroom(skb) < RTA_ALIGN(RTA_ALIGN(sizeof(*nhp)) + 4))
1560 goto rtattr_failure;
1561 nhp = (struct rtnexthop*)skb_put(skb, RTA_ALIGN(sizeof(*nhp)));
1562 nhp->rtnh_flags = 0;
1563 nhp->rtnh_hops = c->mfc_un.res.ttls[ct];
1564 nhp->rtnh_ifindex = vif_table[ct].dev->ifindex;
1565 nhp->rtnh_len = sizeof(*nhp);
1566 }
1567 }
1568 mp_head->rta_type = RTA_MULTIPATH;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001569 mp_head->rta_len = skb_tail_pointer(skb) - (u8 *)mp_head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 rtm->rtm_type = RTN_MULTICAST;
1571 return 1;
1572
1573rtattr_failure:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -07001574 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 return -EMSGSIZE;
1576}
1577
1578int ipmr_get_route(struct sk_buff *skb, struct rtmsg *rtm, int nowait)
1579{
1580 int err;
1581 struct mfc_cache *cache;
1582 struct rtable *rt = (struct rtable*)skb->dst;
1583
1584 read_lock(&mrt_lock);
1585 cache = ipmr_cache_find(rt->rt_src, rt->rt_dst);
1586
1587 if (cache==NULL) {
Alexey Kuznetsov72287492006-07-25 16:45:12 -07001588 struct sk_buff *skb2;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001589 struct iphdr *iph;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 struct net_device *dev;
1591 int vif;
1592
1593 if (nowait) {
1594 read_unlock(&mrt_lock);
1595 return -EAGAIN;
1596 }
1597
1598 dev = skb->dev;
1599 if (dev == NULL || (vif = ipmr_find_vif(dev)) < 0) {
1600 read_unlock(&mrt_lock);
1601 return -ENODEV;
1602 }
Alexey Kuznetsov72287492006-07-25 16:45:12 -07001603 skb2 = skb_clone(skb, GFP_ATOMIC);
1604 if (!skb2) {
1605 read_unlock(&mrt_lock);
1606 return -ENOMEM;
1607 }
1608
Arnaldo Carvalho de Meloe2d1bca2007-04-10 20:46:21 -07001609 skb_push(skb2, sizeof(struct iphdr));
1610 skb_reset_network_header(skb2);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001611 iph = ip_hdr(skb2);
1612 iph->ihl = sizeof(struct iphdr) >> 2;
1613 iph->saddr = rt->rt_src;
1614 iph->daddr = rt->rt_dst;
1615 iph->version = 0;
Alexey Kuznetsov72287492006-07-25 16:45:12 -07001616 err = ipmr_cache_unresolved(vif, skb2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 read_unlock(&mrt_lock);
1618 return err;
1619 }
1620
1621 if (!nowait && (rtm->rtm_flags&RTM_F_NOTIFY))
1622 cache->mfc_flags |= MFC_NOTIFY;
1623 err = ipmr_fill_mroute(skb, cache, rtm);
1624 read_unlock(&mrt_lock);
1625 return err;
1626}
1627
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001628#ifdef CONFIG_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629/*
1630 * The /proc interfaces to multicast routing /proc/ip_mr_cache /proc/ip_mr_vif
1631 */
1632struct ipmr_vif_iter {
1633 int ct;
1634};
1635
1636static struct vif_device *ipmr_vif_seq_idx(struct ipmr_vif_iter *iter,
1637 loff_t pos)
1638{
1639 for (iter->ct = 0; iter->ct < maxvif; ++iter->ct) {
Stephen Hemminger132adf52007-03-08 20:44:43 -08001640 if (!VIF_EXISTS(iter->ct))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 continue;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001642 if (pos-- == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 return &vif_table[iter->ct];
1644 }
1645 return NULL;
1646}
1647
1648static void *ipmr_vif_seq_start(struct seq_file *seq, loff_t *pos)
1649{
1650 read_lock(&mrt_lock);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001651 return *pos ? ipmr_vif_seq_idx(seq->private, *pos - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 : SEQ_START_TOKEN;
1653}
1654
1655static void *ipmr_vif_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1656{
1657 struct ipmr_vif_iter *iter = seq->private;
1658
1659 ++*pos;
1660 if (v == SEQ_START_TOKEN)
1661 return ipmr_vif_seq_idx(iter, 0);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001662
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 while (++iter->ct < maxvif) {
Stephen Hemminger132adf52007-03-08 20:44:43 -08001664 if (!VIF_EXISTS(iter->ct))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 continue;
1666 return &vif_table[iter->ct];
1667 }
1668 return NULL;
1669}
1670
1671static void ipmr_vif_seq_stop(struct seq_file *seq, void *v)
1672{
1673 read_unlock(&mrt_lock);
1674}
1675
1676static int ipmr_vif_seq_show(struct seq_file *seq, void *v)
1677{
1678 if (v == SEQ_START_TOKEN) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001679 seq_puts(seq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 "Interface BytesIn PktsIn BytesOut PktsOut Flags Local Remote\n");
1681 } else {
1682 const struct vif_device *vif = v;
1683 const char *name = vif->dev ? vif->dev->name : "none";
1684
1685 seq_printf(seq,
1686 "%2Zd %-10s %8ld %7ld %8ld %7ld %05X %08X %08X\n",
1687 vif - vif_table,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001688 name, vif->bytes_in, vif->pkt_in,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 vif->bytes_out, vif->pkt_out,
1690 vif->flags, vif->local, vif->remote);
1691 }
1692 return 0;
1693}
1694
Stephen Hemmingerf6908082007-03-12 14:34:29 -07001695static const struct seq_operations ipmr_vif_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 .start = ipmr_vif_seq_start,
1697 .next = ipmr_vif_seq_next,
1698 .stop = ipmr_vif_seq_stop,
1699 .show = ipmr_vif_seq_show,
1700};
1701
1702static int ipmr_vif_open(struct inode *inode, struct file *file)
1703{
1704 struct seq_file *seq;
1705 int rc = -ENOMEM;
1706 struct ipmr_vif_iter *s = kmalloc(sizeof(*s), GFP_KERNEL);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001707
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 if (!s)
1709 goto out;
1710
1711 rc = seq_open(file, &ipmr_vif_seq_ops);
1712 if (rc)
1713 goto out_kfree;
1714
1715 s->ct = 0;
1716 seq = file->private_data;
1717 seq->private = s;
1718out:
1719 return rc;
1720out_kfree:
1721 kfree(s);
1722 goto out;
1723
1724}
1725
Arjan van de Ven9a321442007-02-12 00:55:35 -08001726static const struct file_operations ipmr_vif_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 .owner = THIS_MODULE,
1728 .open = ipmr_vif_open,
1729 .read = seq_read,
1730 .llseek = seq_lseek,
1731 .release = seq_release_private,
1732};
1733
1734struct ipmr_mfc_iter {
1735 struct mfc_cache **cache;
1736 int ct;
1737};
1738
1739
1740static struct mfc_cache *ipmr_mfc_seq_idx(struct ipmr_mfc_iter *it, loff_t pos)
1741{
1742 struct mfc_cache *mfc;
1743
1744 it->cache = mfc_cache_array;
1745 read_lock(&mrt_lock);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001746 for (it->ct = 0; it->ct < MFC_LINES; it->ct++)
Stephen Hemminger132adf52007-03-08 20:44:43 -08001747 for (mfc = mfc_cache_array[it->ct]; mfc; mfc = mfc->next)
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001748 if (pos-- == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 return mfc;
1750 read_unlock(&mrt_lock);
1751
1752 it->cache = &mfc_unres_queue;
1753 spin_lock_bh(&mfc_unres_lock);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001754 for (mfc = mfc_unres_queue; mfc; mfc = mfc->next)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 if (pos-- == 0)
1756 return mfc;
1757 spin_unlock_bh(&mfc_unres_lock);
1758
1759 it->cache = NULL;
1760 return NULL;
1761}
1762
1763
1764static void *ipmr_mfc_seq_start(struct seq_file *seq, loff_t *pos)
1765{
1766 struct ipmr_mfc_iter *it = seq->private;
1767 it->cache = NULL;
1768 it->ct = 0;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001769 return *pos ? ipmr_mfc_seq_idx(seq->private, *pos - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 : SEQ_START_TOKEN;
1771}
1772
1773static void *ipmr_mfc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1774{
1775 struct mfc_cache *mfc = v;
1776 struct ipmr_mfc_iter *it = seq->private;
1777
1778 ++*pos;
1779
1780 if (v == SEQ_START_TOKEN)
1781 return ipmr_mfc_seq_idx(seq->private, 0);
1782
1783 if (mfc->next)
1784 return mfc->next;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001785
1786 if (it->cache == &mfc_unres_queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 goto end_of_list;
1788
1789 BUG_ON(it->cache != mfc_cache_array);
1790
1791 while (++it->ct < MFC_LINES) {
1792 mfc = mfc_cache_array[it->ct];
1793 if (mfc)
1794 return mfc;
1795 }
1796
1797 /* exhausted cache_array, show unresolved */
1798 read_unlock(&mrt_lock);
1799 it->cache = &mfc_unres_queue;
1800 it->ct = 0;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001801
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 spin_lock_bh(&mfc_unres_lock);
1803 mfc = mfc_unres_queue;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001804 if (mfc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 return mfc;
1806
1807 end_of_list:
1808 spin_unlock_bh(&mfc_unres_lock);
1809 it->cache = NULL;
1810
1811 return NULL;
1812}
1813
1814static void ipmr_mfc_seq_stop(struct seq_file *seq, void *v)
1815{
1816 struct ipmr_mfc_iter *it = seq->private;
1817
1818 if (it->cache == &mfc_unres_queue)
1819 spin_unlock_bh(&mfc_unres_lock);
1820 else if (it->cache == mfc_cache_array)
1821 read_unlock(&mrt_lock);
1822}
1823
1824static int ipmr_mfc_seq_show(struct seq_file *seq, void *v)
1825{
1826 int n;
1827
1828 if (v == SEQ_START_TOKEN) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001829 seq_puts(seq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 "Group Origin Iif Pkts Bytes Wrong Oifs\n");
1831 } else {
1832 const struct mfc_cache *mfc = v;
1833 const struct ipmr_mfc_iter *it = seq->private;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001834
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 seq_printf(seq, "%08lX %08lX %-3d %8ld %8ld %8ld",
1836 (unsigned long) mfc->mfc_mcastgrp,
1837 (unsigned long) mfc->mfc_origin,
1838 mfc->mfc_parent,
1839 mfc->mfc_un.res.pkt,
1840 mfc->mfc_un.res.bytes,
1841 mfc->mfc_un.res.wrong_if);
1842
1843 if (it->cache != &mfc_unres_queue) {
Stephen Hemminger132adf52007-03-08 20:44:43 -08001844 for (n = mfc->mfc_un.res.minvif;
1845 n < mfc->mfc_un.res.maxvif; n++ ) {
1846 if (VIF_EXISTS(n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 && mfc->mfc_un.res.ttls[n] < 255)
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001848 seq_printf(seq,
1849 " %2d:%-3d",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 n, mfc->mfc_un.res.ttls[n]);
1851 }
1852 }
1853 seq_putc(seq, '\n');
1854 }
1855 return 0;
1856}
1857
Stephen Hemmingerf6908082007-03-12 14:34:29 -07001858static const struct seq_operations ipmr_mfc_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 .start = ipmr_mfc_seq_start,
1860 .next = ipmr_mfc_seq_next,
1861 .stop = ipmr_mfc_seq_stop,
1862 .show = ipmr_mfc_seq_show,
1863};
1864
1865static int ipmr_mfc_open(struct inode *inode, struct file *file)
1866{
1867 struct seq_file *seq;
1868 int rc = -ENOMEM;
1869 struct ipmr_mfc_iter *s = kmalloc(sizeof(*s), GFP_KERNEL);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001870
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 if (!s)
1872 goto out;
1873
1874 rc = seq_open(file, &ipmr_mfc_seq_ops);
1875 if (rc)
1876 goto out_kfree;
1877
1878 seq = file->private_data;
1879 seq->private = s;
1880out:
1881 return rc;
1882out_kfree:
1883 kfree(s);
1884 goto out;
1885
1886}
1887
Arjan van de Ven9a321442007-02-12 00:55:35 -08001888static const struct file_operations ipmr_mfc_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 .owner = THIS_MODULE,
1890 .open = ipmr_mfc_open,
1891 .read = seq_read,
1892 .llseek = seq_lseek,
1893 .release = seq_release_private,
1894};
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001895#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896
1897#ifdef CONFIG_IP_PIMSM_V2
1898static struct net_protocol pim_protocol = {
1899 .handler = pim_rcv,
1900};
1901#endif
1902
1903
1904/*
1905 * Setup for IP multicast routing
1906 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001907
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908void __init ip_mr_init(void)
1909{
1910 mrt_cachep = kmem_cache_create("ip_mrt_cache",
1911 sizeof(struct mfc_cache),
Alexey Dobriyane5d679f2006-08-26 19:25:52 -07001912 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 init_timer(&ipmr_expire_timer);
1915 ipmr_expire_timer.function=ipmr_expire_process;
1916 register_netdevice_notifier(&ip_mr_notifier);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001917#ifdef CONFIG_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 proc_net_fops_create("ip_mr_vif", 0, &ipmr_vif_fops);
1919 proc_net_fops_create("ip_mr_cache", 0, &ipmr_mfc_fops);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001920#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921}