blob: 498f4e907d52664a4c8d5484765ab4e71a529a28 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * IP multicast routing support for mrouted 3.6/3.8
3 *
Alan Cox113aa832008-10-13 19:01:08 -07004 * (c) 1995 Alan Cox, <alan@lxorguk.ukuu.org.uk>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * 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 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 * 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 requrement to work with older peers.
26 *
27 */
28
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <asm/system.h>
30#include <asm/uaccess.h>
31#include <linux/types.h>
Randy Dunlap4fc268d2006-01-11 12:17:47 -080032#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/errno.h>
34#include <linux/timer.h>
35#include <linux/mm.h>
36#include <linux/kernel.h>
37#include <linux/fcntl.h>
38#include <linux/stat.h>
39#include <linux/socket.h>
40#include <linux/in.h>
41#include <linux/inet.h>
42#include <linux/netdevice.h>
43#include <linux/inetdevice.h>
44#include <linux/igmp.h>
45#include <linux/proc_fs.h>
46#include <linux/seq_file.h>
47#include <linux/mroute.h>
48#include <linux/init.h>
Kris Katterjohn46f25df2006-01-05 16:35:42 -080049#include <linux/if_ether.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090050#include <linux/slab.h>
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020051#include <net/net_namespace.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
Patrick McHardy0c122952010-04-13 05:03:22 +000071struct mr_table {
72 struct sock *mroute_sk;
73 struct timer_list ipmr_expire_timer;
74 struct list_head mfc_unres_queue;
75 struct list_head mfc_cache_array[MFC_LINES];
76 struct vif_device vif_table[MAXVIFS];
77 int maxvif;
78 atomic_t cache_resolve_queue_len;
79 int mroute_do_assert;
80 int mroute_do_pim;
81#if defined(CONFIG_IP_PIMSM_V1) || defined(CONFIG_IP_PIMSM_V2)
82 int mroute_reg_vif_num;
83#endif
84};
85
Linus Torvalds1da177e2005-04-16 15:20:36 -070086/* Big lock, protecting vif table, mrt cache and mroute socket state.
87 Note that the changes are semaphored via rtnl_lock.
88 */
89
90static DEFINE_RWLOCK(mrt_lock);
91
92/*
93 * Multicast router control variables
94 */
95
Patrick McHardy0c122952010-04-13 05:03:22 +000096#define VIF_EXISTS(_mrt, _idx) ((_mrt)->vif_table[_idx].dev != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
Linus Torvalds1da177e2005-04-16 15:20:36 -070098/* Special spinlock for queue of unresolved entries */
99static DEFINE_SPINLOCK(mfc_unres_lock);
100
101/* We return to original Alan's scheme. Hash table of resolved
102 entries is changed only in process context and protected
103 with weak lock mrt_lock. Queue of unresolved entries is protected
104 with strong spinlock mfc_unres_lock.
105
106 In this case data path is free of exclusive locks at all.
107 */
108
Christoph Lametere18b8902006-12-06 20:33:20 -0800109static struct kmem_cache *mrt_cachep __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
Patrick McHardy0c122952010-04-13 05:03:22 +0000111static int ip_mr_forward(struct net *net, struct mr_table *mrt,
112 struct sk_buff *skb, struct mfc_cache *cache,
113 int local);
114static int ipmr_cache_report(struct mr_table *mrt,
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000115 struct sk_buff *pkt, vifi_t vifi, int assert);
Patrick McHardy0c122952010-04-13 05:03:22 +0000116static int ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
Patrick McHardyd658f8a2010-04-13 05:03:20 +0000117 struct mfc_cache *c, struct rtmsg *rtm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119/* Service routines creating virtual interfaces: DVMRP tunnels and PIMREG */
120
Wang Chend6070322008-07-14 20:55:26 -0700121static void ipmr_del_tunnel(struct net_device *dev, struct vifctl *v)
122{
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000123 struct net *net = dev_net(dev);
124
Wang Chend6070322008-07-14 20:55:26 -0700125 dev_close(dev);
126
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000127 dev = __dev_get_by_name(net, "tunl0");
Wang Chend6070322008-07-14 20:55:26 -0700128 if (dev) {
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800129 const struct net_device_ops *ops = dev->netdev_ops;
Wang Chend6070322008-07-14 20:55:26 -0700130 struct ifreq ifr;
Wang Chend6070322008-07-14 20:55:26 -0700131 struct ip_tunnel_parm p;
132
133 memset(&p, 0, sizeof(p));
134 p.iph.daddr = v->vifc_rmt_addr.s_addr;
135 p.iph.saddr = v->vifc_lcl_addr.s_addr;
136 p.iph.version = 4;
137 p.iph.ihl = 5;
138 p.iph.protocol = IPPROTO_IPIP;
139 sprintf(p.name, "dvmrp%d", v->vifc_vifi);
140 ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
141
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800142 if (ops->ndo_do_ioctl) {
143 mm_segment_t oldfs = get_fs();
144
145 set_fs(KERNEL_DS);
146 ops->ndo_do_ioctl(dev, &ifr, SIOCDELTUNNEL);
147 set_fs(oldfs);
148 }
Wang Chend6070322008-07-14 20:55:26 -0700149 }
150}
151
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152static
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000153struct net_device *ipmr_new_tunnel(struct net *net, struct vifctl *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154{
155 struct net_device *dev;
156
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000157 dev = __dev_get_by_name(net, "tunl0");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
159 if (dev) {
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800160 const struct net_device_ops *ops = dev->netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 int err;
162 struct ifreq ifr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 struct ip_tunnel_parm p;
164 struct in_device *in_dev;
165
166 memset(&p, 0, sizeof(p));
167 p.iph.daddr = v->vifc_rmt_addr.s_addr;
168 p.iph.saddr = v->vifc_lcl_addr.s_addr;
169 p.iph.version = 4;
170 p.iph.ihl = 5;
171 p.iph.protocol = IPPROTO_IPIP;
172 sprintf(p.name, "dvmrp%d", v->vifc_vifi);
Stephen Hemmingerba93ef72008-01-21 17:28:59 -0800173 ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800175 if (ops->ndo_do_ioctl) {
176 mm_segment_t oldfs = get_fs();
177
178 set_fs(KERNEL_DS);
179 err = ops->ndo_do_ioctl(dev, &ifr, SIOCADDTUNNEL);
180 set_fs(oldfs);
181 } else
182 err = -EOPNOTSUPP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
184 dev = NULL;
185
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000186 if (err == 0 &&
187 (dev = __dev_get_by_name(net, p.name)) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 dev->flags |= IFF_MULTICAST;
189
Herbert Xue5ed6392005-10-03 14:35:55 -0700190 in_dev = __in_dev_get_rtnl(dev);
Herbert Xu71e27da2007-06-04 23:36:06 -0700191 if (in_dev == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 goto failure;
Herbert Xu71e27da2007-06-04 23:36:06 -0700193
194 ipv4_devconf_setall(in_dev);
195 IPV4_DEVCONF(in_dev->cnf, RP_FILTER) = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
197 if (dev_open(dev))
198 goto failure;
Wang Chen7dc00c82008-07-14 20:56:34 -0700199 dev_hold(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 }
201 }
202 return dev;
203
204failure:
205 /* allow the register to be completed before unregistering. */
206 rtnl_unlock();
207 rtnl_lock();
208
209 unregister_netdevice(dev);
210 return NULL;
211}
212
213#ifdef CONFIG_IP_PIMSM
214
Stephen Hemminger6fef4c02009-08-31 19:50:41 +0000215static netdev_tx_t reg_vif_xmit(struct sk_buff *skb, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216{
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000217 struct net *net = dev_net(dev);
Patrick McHardy0c122952010-04-13 05:03:22 +0000218 struct mr_table *mrt = net->ipv4.mrt;
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000219
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 read_lock(&mrt_lock);
Pavel Emelyanovcf3677a2008-05-21 14:17:33 -0700221 dev->stats.tx_bytes += skb->len;
222 dev->stats.tx_packets++;
Patrick McHardy0c122952010-04-13 05:03:22 +0000223 ipmr_cache_report(mrt, skb, mrt->mroute_reg_vif_num, IGMPMSG_WHOLEPKT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 read_unlock(&mrt_lock);
225 kfree_skb(skb);
Patrick McHardy6ed10652009-06-23 06:03:08 +0000226 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227}
228
Stephen Hemminger007c3832008-11-20 20:28:35 -0800229static const struct net_device_ops reg_vif_netdev_ops = {
230 .ndo_start_xmit = reg_vif_xmit,
231};
232
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233static void reg_vif_setup(struct net_device *dev)
234{
235 dev->type = ARPHRD_PIMREG;
Kris Katterjohn46f25df2006-01-05 16:35:42 -0800236 dev->mtu = ETH_DATA_LEN - sizeof(struct iphdr) - 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 dev->flags = IFF_NOARP;
Stephen Hemminger007c3832008-11-20 20:28:35 -0800238 dev->netdev_ops = &reg_vif_netdev_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 dev->destructor = free_netdev;
Tom Goff403dbb92009-06-14 03:16:13 -0700240 dev->features |= NETIF_F_NETNS_LOCAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241}
242
Tom Goff403dbb92009-06-14 03:16:13 -0700243static struct net_device *ipmr_reg_vif(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244{
245 struct net_device *dev;
246 struct in_device *in_dev;
247
Pavel Emelyanovcf3677a2008-05-21 14:17:33 -0700248 dev = alloc_netdev(0, "pimreg", reg_vif_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
250 if (dev == NULL)
251 return NULL;
252
Tom Goff403dbb92009-06-14 03:16:13 -0700253 dev_net_set(dev, net);
254
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 if (register_netdevice(dev)) {
256 free_netdev(dev);
257 return NULL;
258 }
259 dev->iflink = 0;
260
Herbert Xu71e27da2007-06-04 23:36:06 -0700261 rcu_read_lock();
262 if ((in_dev = __in_dev_get_rcu(dev)) == NULL) {
263 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 goto failure;
Herbert Xu71e27da2007-06-04 23:36:06 -0700265 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
Herbert Xu71e27da2007-06-04 23:36:06 -0700267 ipv4_devconf_setall(in_dev);
268 IPV4_DEVCONF(in_dev->cnf, RP_FILTER) = 0;
269 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
271 if (dev_open(dev))
272 goto failure;
273
Wang Chen7dc00c82008-07-14 20:56:34 -0700274 dev_hold(dev);
275
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 return dev;
277
278failure:
279 /* allow the register to be completed before unregistering. */
280 rtnl_unlock();
281 rtnl_lock();
282
283 unregister_netdevice(dev);
284 return NULL;
285}
286#endif
287
288/*
289 * Delete a VIF entry
Wang Chen7dc00c82008-07-14 20:56:34 -0700290 * @notify: Set to 1, if the caller is a notifier_call
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900292
Patrick McHardy0c122952010-04-13 05:03:22 +0000293static int vif_delete(struct mr_table *mrt, int vifi, int notify,
Eric Dumazetd17fa6f2009-10-28 05:21:38 +0000294 struct list_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295{
296 struct vif_device *v;
297 struct net_device *dev;
298 struct in_device *in_dev;
299
Patrick McHardy0c122952010-04-13 05:03:22 +0000300 if (vifi < 0 || vifi >= mrt->maxvif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 return -EADDRNOTAVAIL;
302
Patrick McHardy0c122952010-04-13 05:03:22 +0000303 v = &mrt->vif_table[vifi];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
305 write_lock_bh(&mrt_lock);
306 dev = v->dev;
307 v->dev = NULL;
308
309 if (!dev) {
310 write_unlock_bh(&mrt_lock);
311 return -EADDRNOTAVAIL;
312 }
313
314#ifdef CONFIG_IP_PIMSM
Patrick McHardy0c122952010-04-13 05:03:22 +0000315 if (vifi == mrt->mroute_reg_vif_num)
316 mrt->mroute_reg_vif_num = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317#endif
318
Patrick McHardy0c122952010-04-13 05:03:22 +0000319 if (vifi+1 == mrt->maxvif) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 int tmp;
321 for (tmp=vifi-1; tmp>=0; tmp--) {
Patrick McHardy0c122952010-04-13 05:03:22 +0000322 if (VIF_EXISTS(mrt, tmp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 break;
324 }
Patrick McHardy0c122952010-04-13 05:03:22 +0000325 mrt->maxvif = tmp+1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 }
327
328 write_unlock_bh(&mrt_lock);
329
330 dev_set_allmulti(dev, -1);
331
Herbert Xue5ed6392005-10-03 14:35:55 -0700332 if ((in_dev = __in_dev_get_rtnl(dev)) != NULL) {
Herbert Xu42f811b2007-06-04 23:34:44 -0700333 IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 ip_rt_multicast_event(in_dev);
335 }
336
Wang Chen7dc00c82008-07-14 20:56:34 -0700337 if (v->flags&(VIFF_TUNNEL|VIFF_REGISTER) && !notify)
Eric Dumazetd17fa6f2009-10-28 05:21:38 +0000338 unregister_netdevice_queue(dev, head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
340 dev_put(dev);
341 return 0;
342}
343
Benjamin Thery5c0a66f2009-01-22 04:56:17 +0000344static inline void ipmr_cache_free(struct mfc_cache *c)
345{
Benjamin Thery5c0a66f2009-01-22 04:56:17 +0000346 kmem_cache_free(mrt_cachep, c);
347}
348
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349/* Destroy an unresolved cache entry, killing queued skbs
350 and reporting error to netlink readers.
351 */
352
Patrick McHardy0c122952010-04-13 05:03:22 +0000353static void ipmr_destroy_unres(struct mr_table *mrt, struct mfc_cache *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354{
Patrick McHardy0c122952010-04-13 05:03:22 +0000355 struct net *net = NULL; //mrt->net;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 struct sk_buff *skb;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700357 struct nlmsgerr *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
Patrick McHardy0c122952010-04-13 05:03:22 +0000359 atomic_dec(&mrt->cache_resolve_queue_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
Jianjun Kongc354e122008-11-03 00:28:02 -0800361 while ((skb = skb_dequeue(&c->mfc_un.unres.unresolved))) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700362 if (ip_hdr(skb)->version == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 struct nlmsghdr *nlh = (struct nlmsghdr *)skb_pull(skb, sizeof(struct iphdr));
364 nlh->nlmsg_type = NLMSG_ERROR;
365 nlh->nlmsg_len = NLMSG_LENGTH(sizeof(struct nlmsgerr));
366 skb_trim(skb, nlh->nlmsg_len);
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700367 e = NLMSG_DATA(nlh);
368 e->error = -ETIMEDOUT;
369 memset(&e->msg, 0, sizeof(e->msg));
Thomas Graf2942e902006-08-15 00:30:25 -0700370
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000371 rtnl_unicast(skb, net, NETLINK_CB(skb).pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 } else
373 kfree_skb(skb);
374 }
375
Benjamin Thery5c0a66f2009-01-22 04:56:17 +0000376 ipmr_cache_free(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377}
378
379
Patrick McHardye258beb2010-04-13 05:03:19 +0000380/* Timer process for the unresolved queue. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
Patrick McHardye258beb2010-04-13 05:03:19 +0000382static void ipmr_expire_process(unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383{
Patrick McHardy0c122952010-04-13 05:03:22 +0000384 struct mr_table *mrt = (struct mr_table *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 unsigned long now;
386 unsigned long expires;
Patrick McHardy862465f2010-04-13 05:03:21 +0000387 struct mfc_cache *c, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
389 if (!spin_trylock(&mfc_unres_lock)) {
Patrick McHardy0c122952010-04-13 05:03:22 +0000390 mod_timer(&mrt->ipmr_expire_timer, jiffies+HZ/10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 return;
392 }
393
Patrick McHardy0c122952010-04-13 05:03:22 +0000394 if (list_empty(&mrt->mfc_unres_queue))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 goto out;
396
397 now = jiffies;
398 expires = 10*HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
Patrick McHardy0c122952010-04-13 05:03:22 +0000400 list_for_each_entry_safe(c, next, &mrt->mfc_unres_queue, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 if (time_after(c->mfc_un.unres.expires, now)) {
402 unsigned long interval = c->mfc_un.unres.expires - now;
403 if (interval < expires)
404 expires = interval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 continue;
406 }
407
Patrick McHardy862465f2010-04-13 05:03:21 +0000408 list_del(&c->list);
Patrick McHardy0c122952010-04-13 05:03:22 +0000409 ipmr_destroy_unres(mrt, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 }
411
Patrick McHardy0c122952010-04-13 05:03:22 +0000412 if (!list_empty(&mrt->mfc_unres_queue))
413 mod_timer(&mrt->ipmr_expire_timer, jiffies + expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
415out:
416 spin_unlock(&mfc_unres_lock);
417}
418
419/* Fill oifs list. It is called under write locked mrt_lock. */
420
Patrick McHardy0c122952010-04-13 05:03:22 +0000421static void ipmr_update_thresholds(struct mr_table *mrt, struct mfc_cache *cache,
Patrick McHardyd658f8a2010-04-13 05:03:20 +0000422 unsigned char *ttls)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423{
424 int vifi;
425
426 cache->mfc_un.res.minvif = MAXVIFS;
427 cache->mfc_un.res.maxvif = 0;
428 memset(cache->mfc_un.res.ttls, 255, MAXVIFS);
429
Patrick McHardy0c122952010-04-13 05:03:22 +0000430 for (vifi = 0; vifi < mrt->maxvif; vifi++) {
431 if (VIF_EXISTS(mrt, vifi) &&
Benjamin Therycf958ae32009-01-22 04:56:16 +0000432 ttls[vifi] && ttls[vifi] < 255) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 cache->mfc_un.res.ttls[vifi] = ttls[vifi];
434 if (cache->mfc_un.res.minvif > vifi)
435 cache->mfc_un.res.minvif = vifi;
436 if (cache->mfc_un.res.maxvif <= vifi)
437 cache->mfc_un.res.maxvif = vifi + 1;
438 }
439 }
440}
441
Patrick McHardy0c122952010-04-13 05:03:22 +0000442static int vif_add(struct net *net, struct mr_table *mrt,
443 struct vifctl *vifc, int mrtsock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444{
445 int vifi = vifc->vifc_vifi;
Patrick McHardy0c122952010-04-13 05:03:22 +0000446 struct vif_device *v = &mrt->vif_table[vifi];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 struct net_device *dev;
448 struct in_device *in_dev;
Wang Chend6070322008-07-14 20:55:26 -0700449 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
451 /* Is vif busy ? */
Patrick McHardy0c122952010-04-13 05:03:22 +0000452 if (VIF_EXISTS(mrt, vifi))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 return -EADDRINUSE;
454
455 switch (vifc->vifc_flags) {
456#ifdef CONFIG_IP_PIMSM
457 case VIFF_REGISTER:
458 /*
459 * Special Purpose VIF in PIM
460 * All the packets will be sent to the daemon
461 */
Patrick McHardy0c122952010-04-13 05:03:22 +0000462 if (mrt->mroute_reg_vif_num >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 return -EADDRINUSE;
Tom Goff403dbb92009-06-14 03:16:13 -0700464 dev = ipmr_reg_vif(net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 if (!dev)
466 return -ENOBUFS;
Wang Chend6070322008-07-14 20:55:26 -0700467 err = dev_set_allmulti(dev, 1);
468 if (err) {
469 unregister_netdevice(dev);
Wang Chen7dc00c82008-07-14 20:56:34 -0700470 dev_put(dev);
Wang Chend6070322008-07-14 20:55:26 -0700471 return err;
472 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 break;
474#endif
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900475 case VIFF_TUNNEL:
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000476 dev = ipmr_new_tunnel(net, vifc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 if (!dev)
478 return -ENOBUFS;
Wang Chend6070322008-07-14 20:55:26 -0700479 err = dev_set_allmulti(dev, 1);
480 if (err) {
481 ipmr_del_tunnel(dev, vifc);
Wang Chen7dc00c82008-07-14 20:56:34 -0700482 dev_put(dev);
Wang Chend6070322008-07-14 20:55:26 -0700483 return err;
484 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 break;
Ilia Kee5e81f2009-09-16 05:53:07 +0000486
487 case VIFF_USE_IFINDEX:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 case 0:
Ilia Kee5e81f2009-09-16 05:53:07 +0000489 if (vifc->vifc_flags == VIFF_USE_IFINDEX) {
490 dev = dev_get_by_index(net, vifc->vifc_lcl_ifindex);
491 if (dev && dev->ip_ptr == NULL) {
492 dev_put(dev);
493 return -EADDRNOTAVAIL;
494 }
495 } else
496 dev = ip_dev_find(net, vifc->vifc_lcl_addr.s_addr);
497
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 if (!dev)
499 return -EADDRNOTAVAIL;
Wang Chend6070322008-07-14 20:55:26 -0700500 err = dev_set_allmulti(dev, 1);
Wang Chen7dc00c82008-07-14 20:56:34 -0700501 if (err) {
502 dev_put(dev);
Wang Chend6070322008-07-14 20:55:26 -0700503 return err;
Wang Chen7dc00c82008-07-14 20:56:34 -0700504 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 break;
506 default:
507 return -EINVAL;
508 }
509
Dan Carpenterd0490cf2009-11-11 02:03:54 +0000510 if ((in_dev = __in_dev_get_rtnl(dev)) == NULL) {
511 dev_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 return -EADDRNOTAVAIL;
Dan Carpenterd0490cf2009-11-11 02:03:54 +0000513 }
Herbert Xu42f811b2007-06-04 23:34:44 -0700514 IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 ip_rt_multicast_event(in_dev);
516
517 /*
518 * Fill in the VIF structures
519 */
Jianjun Kongc354e122008-11-03 00:28:02 -0800520 v->rate_limit = vifc->vifc_rate_limit;
521 v->local = vifc->vifc_lcl_addr.s_addr;
522 v->remote = vifc->vifc_rmt_addr.s_addr;
523 v->flags = vifc->vifc_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 if (!mrtsock)
525 v->flags |= VIFF_STATIC;
Jianjun Kongc354e122008-11-03 00:28:02 -0800526 v->threshold = vifc->vifc_threshold;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 v->bytes_in = 0;
528 v->bytes_out = 0;
529 v->pkt_in = 0;
530 v->pkt_out = 0;
531 v->link = dev->ifindex;
532 if (v->flags&(VIFF_TUNNEL|VIFF_REGISTER))
533 v->link = dev->iflink;
534
535 /* And finish update writing critical data */
536 write_lock_bh(&mrt_lock);
Jianjun Kongc354e122008-11-03 00:28:02 -0800537 v->dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538#ifdef CONFIG_IP_PIMSM
539 if (v->flags&VIFF_REGISTER)
Patrick McHardy0c122952010-04-13 05:03:22 +0000540 mrt->mroute_reg_vif_num = vifi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541#endif
Patrick McHardy0c122952010-04-13 05:03:22 +0000542 if (vifi+1 > mrt->maxvif)
543 mrt->maxvif = vifi+1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 write_unlock_bh(&mrt_lock);
545 return 0;
546}
547
Patrick McHardy0c122952010-04-13 05:03:22 +0000548static struct mfc_cache *ipmr_cache_find(struct mr_table *mrt,
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000549 __be32 origin,
550 __be32 mcastgrp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551{
Jianjun Kongc354e122008-11-03 00:28:02 -0800552 int line = MFC_HASH(mcastgrp, origin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 struct mfc_cache *c;
554
Patrick McHardy0c122952010-04-13 05:03:22 +0000555 list_for_each_entry(c, &mrt->mfc_cache_array[line], list) {
Patrick McHardy862465f2010-04-13 05:03:21 +0000556 if (c->mfc_origin == origin && c->mfc_mcastgrp == mcastgrp)
557 return c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 }
Patrick McHardy862465f2010-04-13 05:03:21 +0000559 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560}
561
562/*
563 * Allocate a multicast cache entry
564 */
Patrick McHardyd658f8a2010-04-13 05:03:20 +0000565static struct mfc_cache *ipmr_cache_alloc(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566{
Jianjun Kongc354e122008-11-03 00:28:02 -0800567 struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_KERNEL);
568 if (c == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 c->mfc_un.res.minvif = MAXVIFS;
571 return c;
572}
573
Patrick McHardyd658f8a2010-04-13 05:03:20 +0000574static struct mfc_cache *ipmr_cache_alloc_unres(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575{
Jianjun Kongc354e122008-11-03 00:28:02 -0800576 struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_ATOMIC);
577 if (c == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 skb_queue_head_init(&c->mfc_un.unres.unresolved);
580 c->mfc_un.unres.expires = jiffies + 10*HZ;
581 return c;
582}
583
584/*
585 * A cache entry has gone into a resolved state from queued
586 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900587
Patrick McHardy0c122952010-04-13 05:03:22 +0000588static void ipmr_cache_resolve(struct net *net, struct mr_table *mrt,
589 struct mfc_cache *uc, struct mfc_cache *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590{
591 struct sk_buff *skb;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700592 struct nlmsgerr *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
594 /*
595 * Play the pending entries through our router
596 */
597
Jianjun Kongc354e122008-11-03 00:28:02 -0800598 while ((skb = __skb_dequeue(&uc->mfc_un.unres.unresolved))) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700599 if (ip_hdr(skb)->version == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 struct nlmsghdr *nlh = (struct nlmsghdr *)skb_pull(skb, sizeof(struct iphdr));
601
Patrick McHardy0c122952010-04-13 05:03:22 +0000602 if (ipmr_fill_mroute(mrt, skb, c, NLMSG_DATA(nlh)) > 0) {
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700603 nlh->nlmsg_len = (skb_tail_pointer(skb) -
604 (u8 *)nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 } else {
606 nlh->nlmsg_type = NLMSG_ERROR;
607 nlh->nlmsg_len = NLMSG_LENGTH(sizeof(struct nlmsgerr));
608 skb_trim(skb, nlh->nlmsg_len);
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700609 e = NLMSG_DATA(nlh);
610 e->error = -EMSGSIZE;
611 memset(&e->msg, 0, sizeof(e->msg));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 }
Thomas Graf2942e902006-08-15 00:30:25 -0700613
Patrick McHardyd658f8a2010-04-13 05:03:20 +0000614 rtnl_unicast(skb, net, NETLINK_CB(skb).pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 } else
Patrick McHardy0c122952010-04-13 05:03:22 +0000616 ip_mr_forward(net, mrt, skb, c, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 }
618}
619
620/*
621 * Bounce a cache query up to mrouted. We could use netlink for this but mrouted
622 * expects the following bizarre scheme.
623 *
624 * Called under mrt_lock.
625 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900626
Patrick McHardy0c122952010-04-13 05:03:22 +0000627static int ipmr_cache_report(struct mr_table *mrt,
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000628 struct sk_buff *pkt, vifi_t vifi, int assert)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629{
630 struct sk_buff *skb;
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -0300631 const int ihl = ip_hdrlen(pkt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 struct igmphdr *igmp;
633 struct igmpmsg *msg;
634 int ret;
635
636#ifdef CONFIG_IP_PIMSM
637 if (assert == IGMPMSG_WHOLEPKT)
638 skb = skb_realloc_headroom(pkt, sizeof(struct iphdr));
639 else
640#endif
641 skb = alloc_skb(128, GFP_ATOMIC);
642
Stephen Hemminger132adf52007-03-08 20:44:43 -0800643 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 return -ENOBUFS;
645
646#ifdef CONFIG_IP_PIMSM
647 if (assert == IGMPMSG_WHOLEPKT) {
648 /* Ugly, but we have no choice with this interface.
649 Duplicate old header, fix ihl, length etc.
650 And all this only to mangle msg->im_msgtype and
651 to set msg->im_mbz to "mbz" :-)
652 */
Arnaldo Carvalho de Melo878c8142007-03-11 22:38:29 -0300653 skb_push(skb, sizeof(struct iphdr));
654 skb_reset_network_header(skb);
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -0300655 skb_reset_transport_header(skb);
Arnaldo Carvalho de Melo0272ffc2007-03-12 20:05:39 -0300656 msg = (struct igmpmsg *)skb_network_header(skb);
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700657 memcpy(msg, skb_network_header(pkt), sizeof(struct iphdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 msg->im_msgtype = IGMPMSG_WHOLEPKT;
659 msg->im_mbz = 0;
Patrick McHardy0c122952010-04-13 05:03:22 +0000660 msg->im_vif = mrt->mroute_reg_vif_num;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700661 ip_hdr(skb)->ihl = sizeof(struct iphdr) >> 2;
662 ip_hdr(skb)->tot_len = htons(ntohs(ip_hdr(pkt)->tot_len) +
663 sizeof(struct iphdr));
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900664 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665#endif
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900666 {
667
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 /*
669 * Copy the IP header
670 */
671
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700672 skb->network_header = skb->tail;
Arnaldo Carvalho de Meloddc7b8e2007-03-15 21:42:27 -0300673 skb_put(skb, ihl);
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -0300674 skb_copy_to_linear_data(skb, pkt->data, ihl);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700675 ip_hdr(skb)->protocol = 0; /* Flag to the kernel this is a route add */
676 msg = (struct igmpmsg *)skb_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 msg->im_vif = vifi;
Eric Dumazetadf30902009-06-02 05:19:30 +0000678 skb_dst_set(skb, dst_clone(skb_dst(pkt)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679
680 /*
681 * Add our header
682 */
683
Jianjun Kongc354e122008-11-03 00:28:02 -0800684 igmp=(struct igmphdr *)skb_put(skb, sizeof(struct igmphdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 igmp->type =
686 msg->im_msgtype = assert;
687 igmp->code = 0;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700688 ip_hdr(skb)->tot_len = htons(skb->len); /* Fix the length */
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700689 skb->transport_header = skb->network_header;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900690 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
Patrick McHardy0c122952010-04-13 05:03:22 +0000692 if (mrt->mroute_sk == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 kfree_skb(skb);
694 return -EINVAL;
695 }
696
697 /*
698 * Deliver to mrouted
699 */
Patrick McHardy0c122952010-04-13 05:03:22 +0000700 ret = sock_queue_rcv_skb(mrt->mroute_sk, skb);
Benjamin Thery70a269e2009-01-22 04:56:15 +0000701 if (ret < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 if (net_ratelimit())
703 printk(KERN_WARNING "mroute: pending queue full, dropping entries.\n");
704 kfree_skb(skb);
705 }
706
707 return ret;
708}
709
710/*
711 * Queue a packet for resolution. It gets locked cache entry!
712 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900713
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714static int
Patrick McHardy0c122952010-04-13 05:03:22 +0000715ipmr_cache_unresolved(struct mr_table *mrt, vifi_t vifi, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716{
Patrick McHardy862465f2010-04-13 05:03:21 +0000717 bool found = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 int err;
719 struct mfc_cache *c;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700720 const struct iphdr *iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721
722 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +0000723 list_for_each_entry(c, &mrt->mfc_unres_queue, list) {
Patrick McHardye258beb2010-04-13 05:03:19 +0000724 if (c->mfc_mcastgrp == iph->daddr &&
Patrick McHardy862465f2010-04-13 05:03:21 +0000725 c->mfc_origin == iph->saddr) {
726 found = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 break;
Patrick McHardy862465f2010-04-13 05:03:21 +0000728 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 }
730
Patrick McHardy862465f2010-04-13 05:03:21 +0000731 if (!found) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 /*
733 * Create a new entry if allowable
734 */
735
Patrick McHardy0c122952010-04-13 05:03:22 +0000736 if (atomic_read(&mrt->cache_resolve_queue_len) >= 10 ||
Patrick McHardyd658f8a2010-04-13 05:03:20 +0000737 (c = ipmr_cache_alloc_unres()) == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 spin_unlock_bh(&mfc_unres_lock);
739
740 kfree_skb(skb);
741 return -ENOBUFS;
742 }
743
744 /*
745 * Fill in the new cache entry
746 */
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700747 c->mfc_parent = -1;
748 c->mfc_origin = iph->saddr;
749 c->mfc_mcastgrp = iph->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750
751 /*
752 * Reflect first query at mrouted.
753 */
Patrick McHardy0c122952010-04-13 05:03:22 +0000754 err = ipmr_cache_report(mrt, skb, vifi, IGMPMSG_NOCACHE);
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000755 if (err < 0) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900756 /* If the report failed throw the cache entry
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 out - Brad Parker
758 */
759 spin_unlock_bh(&mfc_unres_lock);
760
Benjamin Thery5c0a66f2009-01-22 04:56:17 +0000761 ipmr_cache_free(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 kfree_skb(skb);
763 return err;
764 }
765
Patrick McHardy0c122952010-04-13 05:03:22 +0000766 atomic_inc(&mrt->cache_resolve_queue_len);
767 list_add(&c->list, &mrt->mfc_unres_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768
Patrick McHardy0c122952010-04-13 05:03:22 +0000769 mod_timer(&mrt->ipmr_expire_timer, c->mfc_un.unres.expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 }
771
772 /*
773 * See if we can append the packet
774 */
775 if (c->mfc_un.unres.unresolved.qlen>3) {
776 kfree_skb(skb);
777 err = -ENOBUFS;
778 } else {
Jianjun Kongc354e122008-11-03 00:28:02 -0800779 skb_queue_tail(&c->mfc_un.unres.unresolved, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 err = 0;
781 }
782
783 spin_unlock_bh(&mfc_unres_lock);
784 return err;
785}
786
787/*
788 * MFC cache manipulation by user space mroute daemon
789 */
790
Patrick McHardy0c122952010-04-13 05:03:22 +0000791static int ipmr_mfc_delete(struct mr_table *mrt, struct mfcctl *mfc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792{
793 int line;
Patrick McHardy862465f2010-04-13 05:03:21 +0000794 struct mfc_cache *c, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795
Jianjun Kongc354e122008-11-03 00:28:02 -0800796 line = MFC_HASH(mfc->mfcc_mcastgrp.s_addr, mfc->mfcc_origin.s_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797
Patrick McHardy0c122952010-04-13 05:03:22 +0000798 list_for_each_entry_safe(c, next, &mrt->mfc_cache_array[line], list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 if (c->mfc_origin == mfc->mfcc_origin.s_addr &&
800 c->mfc_mcastgrp == mfc->mfcc_mcastgrp.s_addr) {
801 write_lock_bh(&mrt_lock);
Patrick McHardy862465f2010-04-13 05:03:21 +0000802 list_del(&c->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 write_unlock_bh(&mrt_lock);
804
Benjamin Thery5c0a66f2009-01-22 04:56:17 +0000805 ipmr_cache_free(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 return 0;
807 }
808 }
809 return -ENOENT;
810}
811
Patrick McHardy0c122952010-04-13 05:03:22 +0000812static int ipmr_mfc_add(struct net *net, struct mr_table *mrt,
813 struct mfcctl *mfc, int mrtsock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814{
Patrick McHardy862465f2010-04-13 05:03:21 +0000815 bool found = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 int line;
Patrick McHardy862465f2010-04-13 05:03:21 +0000817 struct mfc_cache *uc, *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818
Patrick McHardya50436f22010-03-17 06:04:14 +0000819 if (mfc->mfcc_parent >= MAXVIFS)
820 return -ENFILE;
821
Jianjun Kongc354e122008-11-03 00:28:02 -0800822 line = MFC_HASH(mfc->mfcc_mcastgrp.s_addr, mfc->mfcc_origin.s_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823
Patrick McHardy0c122952010-04-13 05:03:22 +0000824 list_for_each_entry(c, &mrt->mfc_cache_array[line], list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 if (c->mfc_origin == mfc->mfcc_origin.s_addr &&
Patrick McHardy862465f2010-04-13 05:03:21 +0000826 c->mfc_mcastgrp == mfc->mfcc_mcastgrp.s_addr) {
827 found = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 break;
Patrick McHardy862465f2010-04-13 05:03:21 +0000829 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 }
831
Patrick McHardy862465f2010-04-13 05:03:21 +0000832 if (found) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 write_lock_bh(&mrt_lock);
834 c->mfc_parent = mfc->mfcc_parent;
Patrick McHardy0c122952010-04-13 05:03:22 +0000835 ipmr_update_thresholds(mrt, c, mfc->mfcc_ttls);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 if (!mrtsock)
837 c->mfc_flags |= MFC_STATIC;
838 write_unlock_bh(&mrt_lock);
839 return 0;
840 }
841
Joe Perchesf97c1e02007-12-16 13:45:43 -0800842 if (!ipv4_is_multicast(mfc->mfcc_mcastgrp.s_addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 return -EINVAL;
844
Patrick McHardyd658f8a2010-04-13 05:03:20 +0000845 c = ipmr_cache_alloc();
Jianjun Kongc354e122008-11-03 00:28:02 -0800846 if (c == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 return -ENOMEM;
848
Jianjun Kongc354e122008-11-03 00:28:02 -0800849 c->mfc_origin = mfc->mfcc_origin.s_addr;
850 c->mfc_mcastgrp = mfc->mfcc_mcastgrp.s_addr;
851 c->mfc_parent = mfc->mfcc_parent;
Patrick McHardy0c122952010-04-13 05:03:22 +0000852 ipmr_update_thresholds(mrt, c, mfc->mfcc_ttls);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 if (!mrtsock)
854 c->mfc_flags |= MFC_STATIC;
855
856 write_lock_bh(&mrt_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +0000857 list_add(&c->list, &mrt->mfc_cache_array[line]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 write_unlock_bh(&mrt_lock);
859
860 /*
861 * Check to see if we resolved a queued list. If so we
862 * need to send on the frames and tidy up.
863 */
864 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +0000865 list_for_each_entry(uc, &mrt->mfc_unres_queue, list) {
Patrick McHardye258beb2010-04-13 05:03:19 +0000866 if (uc->mfc_origin == c->mfc_origin &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 uc->mfc_mcastgrp == c->mfc_mcastgrp) {
Patrick McHardy862465f2010-04-13 05:03:21 +0000868 list_del(&uc->list);
Patrick McHardy0c122952010-04-13 05:03:22 +0000869 atomic_dec(&mrt->cache_resolve_queue_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 break;
871 }
872 }
Patrick McHardy0c122952010-04-13 05:03:22 +0000873 if (list_empty(&mrt->mfc_unres_queue))
874 del_timer(&mrt->ipmr_expire_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 spin_unlock_bh(&mfc_unres_lock);
876
877 if (uc) {
Patrick McHardy0c122952010-04-13 05:03:22 +0000878 ipmr_cache_resolve(net, mrt, uc, c);
Benjamin Thery5c0a66f2009-01-22 04:56:17 +0000879 ipmr_cache_free(uc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 }
881 return 0;
882}
883
884/*
885 * Close the multicast socket, and clear the vif tables etc
886 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900887
Patrick McHardy0c122952010-04-13 05:03:22 +0000888static void mroute_clean_tables(struct mr_table *mrt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889{
890 int i;
Eric Dumazetd17fa6f2009-10-28 05:21:38 +0000891 LIST_HEAD(list);
Patrick McHardy862465f2010-04-13 05:03:21 +0000892 struct mfc_cache *c, *next;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900893
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 /*
895 * Shut down all active vif entries
896 */
Patrick McHardy0c122952010-04-13 05:03:22 +0000897 for (i = 0; i < mrt->maxvif; i++) {
898 if (!(mrt->vif_table[i].flags&VIFF_STATIC))
899 vif_delete(mrt, i, 0, &list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 }
Eric Dumazetd17fa6f2009-10-28 05:21:38 +0000901 unregister_netdevice_many(&list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902
903 /*
904 * Wipe the cache
905 */
Patrick McHardy862465f2010-04-13 05:03:21 +0000906 for (i = 0; i < MFC_LINES; i++) {
Patrick McHardy0c122952010-04-13 05:03:22 +0000907 list_for_each_entry_safe(c, next, &mrt->mfc_cache_array[i], list) {
Patrick McHardy862465f2010-04-13 05:03:21 +0000908 if (c->mfc_flags&MFC_STATIC)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 write_lock_bh(&mrt_lock);
Patrick McHardy862465f2010-04-13 05:03:21 +0000911 list_del(&c->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 write_unlock_bh(&mrt_lock);
913
Benjamin Thery5c0a66f2009-01-22 04:56:17 +0000914 ipmr_cache_free(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 }
916 }
917
Patrick McHardy0c122952010-04-13 05:03:22 +0000918 if (atomic_read(&mrt->cache_resolve_queue_len) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +0000920 list_for_each_entry_safe(c, next, &mrt->mfc_unres_queue, list) {
Patrick McHardy862465f2010-04-13 05:03:21 +0000921 list_del(&c->list);
Patrick McHardy0c122952010-04-13 05:03:22 +0000922 ipmr_destroy_unres(mrt, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 }
924 spin_unlock_bh(&mfc_unres_lock);
925 }
926}
927
928static void mrtsock_destruct(struct sock *sk)
929{
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000930 struct net *net = sock_net(sk);
Patrick McHardy0c122952010-04-13 05:03:22 +0000931 struct mr_table *mrt = net->ipv4.mrt;
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000932
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 rtnl_lock();
Patrick McHardy0c122952010-04-13 05:03:22 +0000934 if (sk == mrt->mroute_sk) {
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000935 IPV4_DEVCONF_ALL(net, MC_FORWARDING)--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936
937 write_lock_bh(&mrt_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +0000938 mrt->mroute_sk = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 write_unlock_bh(&mrt_lock);
940
Patrick McHardy0c122952010-04-13 05:03:22 +0000941 mroute_clean_tables(mrt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 }
943 rtnl_unlock();
944}
945
946/*
947 * Socket options and virtual interface manipulation. The whole
948 * virtual interface system is a complete heap, but unfortunately
949 * that's how BSD mrouted happens to think. Maybe one day with a proper
950 * MOSPF/PIM router set up we can clean this up.
951 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900952
David S. Millerb7058842009-09-30 16:12:20 -0700953int ip_mroute_setsockopt(struct sock *sk, int optname, char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954{
955 int ret;
956 struct vifctl vif;
957 struct mfcctl mfc;
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000958 struct net *net = sock_net(sk);
Patrick McHardy0c122952010-04-13 05:03:22 +0000959 struct mr_table *mrt = net->ipv4.mrt;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900960
Stephen Hemminger132adf52007-03-08 20:44:43 -0800961 if (optname != MRT_INIT) {
Patrick McHardy0c122952010-04-13 05:03:22 +0000962 if (sk != mrt->mroute_sk && !capable(CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 return -EACCES;
964 }
965
Stephen Hemminger132adf52007-03-08 20:44:43 -0800966 switch (optname) {
967 case MRT_INIT:
968 if (sk->sk_type != SOCK_RAW ||
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000969 inet_sk(sk)->inet_num != IPPROTO_IGMP)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800970 return -EOPNOTSUPP;
Jianjun Kongc354e122008-11-03 00:28:02 -0800971 if (optlen != sizeof(int))
Stephen Hemminger132adf52007-03-08 20:44:43 -0800972 return -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973
Stephen Hemminger132adf52007-03-08 20:44:43 -0800974 rtnl_lock();
Patrick McHardy0c122952010-04-13 05:03:22 +0000975 if (mrt->mroute_sk) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 rtnl_unlock();
Stephen Hemminger132adf52007-03-08 20:44:43 -0800977 return -EADDRINUSE;
978 }
979
980 ret = ip_ra_control(sk, 1, mrtsock_destruct);
981 if (ret == 0) {
982 write_lock_bh(&mrt_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +0000983 mrt->mroute_sk = sk;
Stephen Hemminger132adf52007-03-08 20:44:43 -0800984 write_unlock_bh(&mrt_lock);
985
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000986 IPV4_DEVCONF_ALL(net, MC_FORWARDING)++;
Stephen Hemminger132adf52007-03-08 20:44:43 -0800987 }
988 rtnl_unlock();
989 return ret;
990 case MRT_DONE:
Patrick McHardy0c122952010-04-13 05:03:22 +0000991 if (sk != mrt->mroute_sk)
Stephen Hemminger132adf52007-03-08 20:44:43 -0800992 return -EACCES;
993 return ip_ra_control(sk, 0, NULL);
994 case MRT_ADD_VIF:
995 case MRT_DEL_VIF:
Jianjun Kongc354e122008-11-03 00:28:02 -0800996 if (optlen != sizeof(vif))
Stephen Hemminger132adf52007-03-08 20:44:43 -0800997 return -EINVAL;
Jianjun Kongc354e122008-11-03 00:28:02 -0800998 if (copy_from_user(&vif, optval, sizeof(vif)))
Stephen Hemminger132adf52007-03-08 20:44:43 -0800999 return -EFAULT;
1000 if (vif.vifc_vifi >= MAXVIFS)
1001 return -ENFILE;
1002 rtnl_lock();
Jianjun Kongc354e122008-11-03 00:28:02 -08001003 if (optname == MRT_ADD_VIF) {
Patrick McHardy0c122952010-04-13 05:03:22 +00001004 ret = vif_add(net, mrt, &vif, sk == mrt->mroute_sk);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001005 } else {
Patrick McHardy0c122952010-04-13 05:03:22 +00001006 ret = vif_delete(mrt, vif.vifc_vifi, 0, NULL);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001007 }
1008 rtnl_unlock();
1009 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010
1011 /*
1012 * Manipulate the forwarding caches. These live
1013 * in a sort of kernel/user symbiosis.
1014 */
Stephen Hemminger132adf52007-03-08 20:44:43 -08001015 case MRT_ADD_MFC:
1016 case MRT_DEL_MFC:
Jianjun Kongc354e122008-11-03 00:28:02 -08001017 if (optlen != sizeof(mfc))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001018 return -EINVAL;
Jianjun Kongc354e122008-11-03 00:28:02 -08001019 if (copy_from_user(&mfc, optval, sizeof(mfc)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001020 return -EFAULT;
1021 rtnl_lock();
Jianjun Kongc354e122008-11-03 00:28:02 -08001022 if (optname == MRT_DEL_MFC)
Patrick McHardy0c122952010-04-13 05:03:22 +00001023 ret = ipmr_mfc_delete(mrt, &mfc);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001024 else
Patrick McHardy0c122952010-04-13 05:03:22 +00001025 ret = ipmr_mfc_add(net, mrt, &mfc, sk == mrt->mroute_sk);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001026 rtnl_unlock();
1027 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 /*
1029 * Control PIM assert.
1030 */
Stephen Hemminger132adf52007-03-08 20:44:43 -08001031 case MRT_ASSERT:
1032 {
1033 int v;
1034 if (get_user(v,(int __user *)optval))
1035 return -EFAULT;
Patrick McHardy0c122952010-04-13 05:03:22 +00001036 mrt->mroute_do_assert = (v) ? 1 : 0;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001037 return 0;
1038 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039#ifdef CONFIG_IP_PIMSM
Stephen Hemminger132adf52007-03-08 20:44:43 -08001040 case MRT_PIM:
1041 {
Stephen Hemmingerba93ef72008-01-21 17:28:59 -08001042 int v;
1043
Stephen Hemminger132adf52007-03-08 20:44:43 -08001044 if (get_user(v,(int __user *)optval))
1045 return -EFAULT;
Stephen Hemmingerba93ef72008-01-21 17:28:59 -08001046 v = (v) ? 1 : 0;
1047
Stephen Hemminger132adf52007-03-08 20:44:43 -08001048 rtnl_lock();
1049 ret = 0;
Patrick McHardy0c122952010-04-13 05:03:22 +00001050 if (v != mrt->mroute_do_pim) {
1051 mrt->mroute_do_pim = v;
1052 mrt->mroute_do_assert = v;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001054 rtnl_unlock();
1055 return ret;
1056 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057#endif
Stephen Hemminger132adf52007-03-08 20:44:43 -08001058 /*
1059 * Spurious command, or MRT_VERSION which you cannot
1060 * set.
1061 */
1062 default:
1063 return -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 }
1065}
1066
1067/*
1068 * Getsock opt support for the multicast routing system.
1069 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001070
Jianjun Kongc354e122008-11-03 00:28:02 -08001071int ip_mroute_getsockopt(struct sock *sk, int optname, char __user *optval, int __user *optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072{
1073 int olr;
1074 int val;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001075 struct net *net = sock_net(sk);
Patrick McHardy0c122952010-04-13 05:03:22 +00001076 struct mr_table *mrt = net->ipv4.mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077
Jianjun Kongc354e122008-11-03 00:28:02 -08001078 if (optname != MRT_VERSION &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079#ifdef CONFIG_IP_PIMSM
1080 optname!=MRT_PIM &&
1081#endif
1082 optname!=MRT_ASSERT)
1083 return -ENOPROTOOPT;
1084
1085 if (get_user(olr, optlen))
1086 return -EFAULT;
1087
1088 olr = min_t(unsigned int, olr, sizeof(int));
1089 if (olr < 0)
1090 return -EINVAL;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001091
Jianjun Kongc354e122008-11-03 00:28:02 -08001092 if (put_user(olr, optlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 return -EFAULT;
Jianjun Kongc354e122008-11-03 00:28:02 -08001094 if (optname == MRT_VERSION)
1095 val = 0x0305;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096#ifdef CONFIG_IP_PIMSM
Jianjun Kongc354e122008-11-03 00:28:02 -08001097 else if (optname == MRT_PIM)
Patrick McHardy0c122952010-04-13 05:03:22 +00001098 val = mrt->mroute_do_pim;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099#endif
1100 else
Patrick McHardy0c122952010-04-13 05:03:22 +00001101 val = mrt->mroute_do_assert;
Jianjun Kongc354e122008-11-03 00:28:02 -08001102 if (copy_to_user(optval, &val, olr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 return -EFAULT;
1104 return 0;
1105}
1106
1107/*
1108 * The IP multicast ioctl support routines.
1109 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001110
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111int ipmr_ioctl(struct sock *sk, int cmd, void __user *arg)
1112{
1113 struct sioc_sg_req sr;
1114 struct sioc_vif_req vr;
1115 struct vif_device *vif;
1116 struct mfc_cache *c;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001117 struct net *net = sock_net(sk);
Patrick McHardy0c122952010-04-13 05:03:22 +00001118 struct mr_table *mrt = net->ipv4.mrt;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001119
Stephen Hemminger132adf52007-03-08 20:44:43 -08001120 switch (cmd) {
1121 case SIOCGETVIFCNT:
Jianjun Kongc354e122008-11-03 00:28:02 -08001122 if (copy_from_user(&vr, arg, sizeof(vr)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001123 return -EFAULT;
Patrick McHardy0c122952010-04-13 05:03:22 +00001124 if (vr.vifi >= mrt->maxvif)
Stephen Hemminger132adf52007-03-08 20:44:43 -08001125 return -EINVAL;
1126 read_lock(&mrt_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001127 vif = &mrt->vif_table[vr.vifi];
1128 if (VIF_EXISTS(mrt, vr.vifi)) {
Jianjun Kongc354e122008-11-03 00:28:02 -08001129 vr.icount = vif->pkt_in;
1130 vr.ocount = vif->pkt_out;
1131 vr.ibytes = vif->bytes_in;
1132 vr.obytes = vif->bytes_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 read_unlock(&mrt_lock);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001134
Jianjun Kongc354e122008-11-03 00:28:02 -08001135 if (copy_to_user(arg, &vr, sizeof(vr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 return -EFAULT;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001137 return 0;
1138 }
1139 read_unlock(&mrt_lock);
1140 return -EADDRNOTAVAIL;
1141 case SIOCGETSGCNT:
Jianjun Kongc354e122008-11-03 00:28:02 -08001142 if (copy_from_user(&sr, arg, sizeof(sr)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001143 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144
Stephen Hemminger132adf52007-03-08 20:44:43 -08001145 read_lock(&mrt_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001146 c = ipmr_cache_find(mrt, sr.src.s_addr, sr.grp.s_addr);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001147 if (c) {
1148 sr.pktcnt = c->mfc_un.res.pkt;
1149 sr.bytecnt = c->mfc_un.res.bytes;
1150 sr.wrong_if = c->mfc_un.res.wrong_if;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 read_unlock(&mrt_lock);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001152
Jianjun Kongc354e122008-11-03 00:28:02 -08001153 if (copy_to_user(arg, &sr, sizeof(sr)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001154 return -EFAULT;
1155 return 0;
1156 }
1157 read_unlock(&mrt_lock);
1158 return -EADDRNOTAVAIL;
1159 default:
1160 return -ENOIOCTLCMD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 }
1162}
1163
1164
1165static int ipmr_device_event(struct notifier_block *this, unsigned long event, void *ptr)
1166{
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02001167 struct net_device *dev = ptr;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001168 struct net *net = dev_net(dev);
Patrick McHardy0c122952010-04-13 05:03:22 +00001169 struct mr_table *mrt = net->ipv4.mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 struct vif_device *v;
1171 int ct;
Eric Dumazetd17fa6f2009-10-28 05:21:38 +00001172 LIST_HEAD(list);
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02001173
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 if (event != NETDEV_UNREGISTER)
1175 return NOTIFY_DONE;
Patrick McHardy0c122952010-04-13 05:03:22 +00001176 v = &mrt->vif_table[0];
1177 for (ct = 0; ct < mrt->maxvif; ct++, v++) {
Jianjun Kongc354e122008-11-03 00:28:02 -08001178 if (v->dev == dev)
Patrick McHardy0c122952010-04-13 05:03:22 +00001179 vif_delete(mrt, ct, 1, &list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 }
Eric Dumazetd17fa6f2009-10-28 05:21:38 +00001181 unregister_netdevice_many(&list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 return NOTIFY_DONE;
1183}
1184
1185
Jianjun Kongc354e122008-11-03 00:28:02 -08001186static struct notifier_block ip_mr_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 .notifier_call = ipmr_device_event,
1188};
1189
1190/*
1191 * Encapsulate a packet by attaching a valid IPIP header to it.
1192 * This avoids tunnel drivers and other mess and gives us the speed so
1193 * important for multicast video.
1194 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001195
Al Viro114c7842006-09-27 18:39:29 -07001196static void ip_encap(struct sk_buff *skb, __be32 saddr, __be32 daddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197{
Arnaldo Carvalho de Melo8856dfa2007-03-10 19:40:39 -03001198 struct iphdr *iph;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001199 struct iphdr *old_iph = ip_hdr(skb);
Arnaldo Carvalho de Melo8856dfa2007-03-10 19:40:39 -03001200
1201 skb_push(skb, sizeof(struct iphdr));
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07001202 skb->transport_header = skb->network_header;
Arnaldo Carvalho de Melo8856dfa2007-03-10 19:40:39 -03001203 skb_reset_network_header(skb);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001204 iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205
1206 iph->version = 4;
Arnaldo Carvalho de Meloe023dd62007-03-12 20:09:36 -03001207 iph->tos = old_iph->tos;
1208 iph->ttl = old_iph->ttl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 iph->frag_off = 0;
1210 iph->daddr = daddr;
1211 iph->saddr = saddr;
1212 iph->protocol = IPPROTO_IPIP;
1213 iph->ihl = 5;
1214 iph->tot_len = htons(skb->len);
Eric Dumazetadf30902009-06-02 05:19:30 +00001215 ip_select_ident(iph, skb_dst(skb), NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 ip_send_check(iph);
1217
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1219 nf_reset(skb);
1220}
1221
1222static inline int ipmr_forward_finish(struct sk_buff *skb)
1223{
1224 struct ip_options * opt = &(IPCB(skb)->opt);
1225
Eric Dumazetadf30902009-06-02 05:19:30 +00001226 IP_INC_STATS_BH(dev_net(skb_dst(skb)->dev), IPSTATS_MIB_OUTFORWDATAGRAMS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227
1228 if (unlikely(opt->optlen))
1229 ip_forward_options(skb);
1230
1231 return dst_output(skb);
1232}
1233
1234/*
1235 * Processing handlers for ipmr_forward
1236 */
1237
Patrick McHardy0c122952010-04-13 05:03:22 +00001238static void ipmr_queue_xmit(struct net *net, struct mr_table *mrt,
1239 struct sk_buff *skb, struct mfc_cache *c, int vifi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240{
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001241 const struct iphdr *iph = ip_hdr(skb);
Patrick McHardy0c122952010-04-13 05:03:22 +00001242 struct vif_device *vif = &mrt->vif_table[vifi];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 struct net_device *dev;
1244 struct rtable *rt;
1245 int encap = 0;
1246
1247 if (vif->dev == NULL)
1248 goto out_free;
1249
1250#ifdef CONFIG_IP_PIMSM
1251 if (vif->flags & VIFF_REGISTER) {
1252 vif->pkt_out++;
Jianjun Kongc354e122008-11-03 00:28:02 -08001253 vif->bytes_out += skb->len;
Pavel Emelyanovcf3677a2008-05-21 14:17:33 -07001254 vif->dev->stats.tx_bytes += skb->len;
1255 vif->dev->stats.tx_packets++;
Patrick McHardy0c122952010-04-13 05:03:22 +00001256 ipmr_cache_report(mrt, skb, vifi, IGMPMSG_WHOLEPKT);
Ilpo Järvinen69ebbf52009-02-06 23:46:51 -08001257 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 }
1259#endif
1260
1261 if (vif->flags&VIFF_TUNNEL) {
1262 struct flowi fl = { .oif = vif->link,
1263 .nl_u = { .ip4_u =
1264 { .daddr = vif->remote,
1265 .saddr = vif->local,
1266 .tos = RT_TOS(iph->tos) } },
1267 .proto = IPPROTO_IPIP };
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001268 if (ip_route_output_key(net, &rt, &fl))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 goto out_free;
1270 encap = sizeof(struct iphdr);
1271 } else {
1272 struct flowi fl = { .oif = vif->link,
1273 .nl_u = { .ip4_u =
1274 { .daddr = iph->daddr,
1275 .tos = RT_TOS(iph->tos) } },
1276 .proto = IPPROTO_IPIP };
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001277 if (ip_route_output_key(net, &rt, &fl))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 goto out_free;
1279 }
1280
1281 dev = rt->u.dst.dev;
1282
1283 if (skb->len+encap > dst_mtu(&rt->u.dst) && (ntohs(iph->frag_off) & IP_DF)) {
1284 /* Do not fragment multicasts. Alas, IPv4 does not
1285 allow to send ICMP, so that packets will disappear
1286 to blackhole.
1287 */
1288
Pavel Emelyanov7c73a6f2008-07-16 20:20:11 -07001289 IP_INC_STATS_BH(dev_net(dev), IPSTATS_MIB_FRAGFAILS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 ip_rt_put(rt);
1291 goto out_free;
1292 }
1293
1294 encap += LL_RESERVED_SPACE(dev) + rt->u.dst.header_len;
1295
1296 if (skb_cow(skb, encap)) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001297 ip_rt_put(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 goto out_free;
1299 }
1300
1301 vif->pkt_out++;
Jianjun Kongc354e122008-11-03 00:28:02 -08001302 vif->bytes_out += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303
Eric Dumazetadf30902009-06-02 05:19:30 +00001304 skb_dst_drop(skb);
1305 skb_dst_set(skb, &rt->u.dst);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001306 ip_decrease_ttl(ip_hdr(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307
1308 /* FIXME: forward and output firewalls used to be called here.
1309 * What do we do with netfilter? -- RR */
1310 if (vif->flags & VIFF_TUNNEL) {
1311 ip_encap(skb, vif->local, vif->remote);
1312 /* FIXME: extra output firewall step used to be here. --RR */
Pavel Emelyanov2f4c02d2008-05-21 14:16:14 -07001313 vif->dev->stats.tx_packets++;
1314 vif->dev->stats.tx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 }
1316
1317 IPCB(skb)->flags |= IPSKB_FORWARDED;
1318
1319 /*
1320 * RFC1584 teaches, that DVMRP/PIM router must deliver packets locally
1321 * not only before forwarding, but after forwarding on all output
1322 * interfaces. It is clear, if mrouter runs a multicasting
1323 * program, it should receive packets not depending to what interface
1324 * program is joined.
1325 * If we will not make it, the program will have to join on all
1326 * interfaces. On the other hand, multihoming host (or router, but
1327 * not mrouter) cannot join to more than one interface - it will
1328 * result in receiving multiple packets.
1329 */
Patrick McHardy6e23ae22007-11-19 18:53:30 -08001330 NF_HOOK(PF_INET, NF_INET_FORWARD, skb, skb->dev, dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 ipmr_forward_finish);
1332 return;
1333
1334out_free:
1335 kfree_skb(skb);
1336 return;
1337}
1338
Patrick McHardy0c122952010-04-13 05:03:22 +00001339static int ipmr_find_vif(struct mr_table *mrt, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340{
1341 int ct;
Patrick McHardy0c122952010-04-13 05:03:22 +00001342
1343 for (ct = mrt->maxvif-1; ct >= 0; ct--) {
1344 if (mrt->vif_table[ct].dev == dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 break;
1346 }
1347 return ct;
1348}
1349
1350/* "local" means that we should preserve one skb (for local delivery) */
1351
Patrick McHardy0c122952010-04-13 05:03:22 +00001352static int ip_mr_forward(struct net *net, struct mr_table *mrt,
1353 struct sk_buff *skb, struct mfc_cache *cache,
1354 int local)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355{
1356 int psend = -1;
1357 int vif, ct;
1358
1359 vif = cache->mfc_parent;
1360 cache->mfc_un.res.pkt++;
1361 cache->mfc_un.res.bytes += skb->len;
1362
1363 /*
1364 * Wrong interface: drop packet and (maybe) send PIM assert.
1365 */
Patrick McHardy0c122952010-04-13 05:03:22 +00001366 if (mrt->vif_table[vif].dev != skb->dev) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 int true_vifi;
1368
Eric Dumazet511c3f92009-06-02 05:14:27 +00001369 if (skb_rtable(skb)->fl.iif == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 /* It is our own packet, looped back.
1371 Very complicated situation...
1372
1373 The best workaround until routing daemons will be
1374 fixed is not to redistribute packet, if it was
1375 send through wrong interface. It means, that
1376 multicast applications WILL NOT work for
1377 (S,G), which have default multicast route pointing
1378 to wrong oif. In any case, it is not a good
1379 idea to use multicasting applications on router.
1380 */
1381 goto dont_forward;
1382 }
1383
1384 cache->mfc_un.res.wrong_if++;
Patrick McHardy0c122952010-04-13 05:03:22 +00001385 true_vifi = ipmr_find_vif(mrt, skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386
Patrick McHardy0c122952010-04-13 05:03:22 +00001387 if (true_vifi >= 0 && mrt->mroute_do_assert &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 /* pimsm uses asserts, when switching from RPT to SPT,
1389 so that we cannot check that packet arrived on an oif.
1390 It is bad, but otherwise we would need to move pretty
1391 large chunk of pimd to kernel. Ough... --ANK
1392 */
Patrick McHardy0c122952010-04-13 05:03:22 +00001393 (mrt->mroute_do_pim ||
Benjamin Thery6f9374a2009-01-22 04:56:20 +00001394 cache->mfc_un.res.ttls[true_vifi] < 255) &&
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001395 time_after(jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 cache->mfc_un.res.last_assert + MFC_ASSERT_THRESH)) {
1397 cache->mfc_un.res.last_assert = jiffies;
Patrick McHardy0c122952010-04-13 05:03:22 +00001398 ipmr_cache_report(mrt, skb, true_vifi, IGMPMSG_WRONGVIF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 }
1400 goto dont_forward;
1401 }
1402
Patrick McHardy0c122952010-04-13 05:03:22 +00001403 mrt->vif_table[vif].pkt_in++;
1404 mrt->vif_table[vif].bytes_in += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405
1406 /*
1407 * Forward the frame
1408 */
1409 for (ct = cache->mfc_un.res.maxvif-1; ct >= cache->mfc_un.res.minvif; ct--) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001410 if (ip_hdr(skb)->ttl > cache->mfc_un.res.ttls[ct]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 if (psend != -1) {
1412 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
1413 if (skb2)
Patrick McHardy0c122952010-04-13 05:03:22 +00001414 ipmr_queue_xmit(net, mrt, skb2, cache,
1415 psend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 }
Jianjun Kongc354e122008-11-03 00:28:02 -08001417 psend = ct;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 }
1419 }
1420 if (psend != -1) {
1421 if (local) {
1422 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
1423 if (skb2)
Patrick McHardy0c122952010-04-13 05:03:22 +00001424 ipmr_queue_xmit(net, mrt, skb2, cache, psend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 } else {
Patrick McHardy0c122952010-04-13 05:03:22 +00001426 ipmr_queue_xmit(net, mrt, skb, cache, psend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 return 0;
1428 }
1429 }
1430
1431dont_forward:
1432 if (!local)
1433 kfree_skb(skb);
1434 return 0;
1435}
1436
1437
1438/*
1439 * Multicast packets for forwarding arrive here
1440 */
1441
1442int ip_mr_input(struct sk_buff *skb)
1443{
1444 struct mfc_cache *cache;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001445 struct net *net = dev_net(skb->dev);
Patrick McHardy0c122952010-04-13 05:03:22 +00001446 struct mr_table *mrt = net->ipv4.mrt;
Eric Dumazet511c3f92009-06-02 05:14:27 +00001447 int local = skb_rtable(skb)->rt_flags & RTCF_LOCAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448
1449 /* Packet is looped back after forward, it should not be
1450 forwarded second time, but still can be delivered locally.
1451 */
1452 if (IPCB(skb)->flags&IPSKB_FORWARDED)
1453 goto dont_forward;
1454
1455 if (!local) {
1456 if (IPCB(skb)->opt.router_alert) {
1457 if (ip_call_ra_chain(skb))
1458 return 0;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001459 } else if (ip_hdr(skb)->protocol == IPPROTO_IGMP){
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 /* IGMPv1 (and broken IGMPv2 implementations sort of
1461 Cisco IOS <= 11.2(8)) do not put router alert
1462 option to IGMP packets destined to routable
1463 groups. It is very bad, because it means
1464 that we can forward NO IGMP messages.
1465 */
1466 read_lock(&mrt_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001467 if (mrt->mroute_sk) {
Patrick McHardy2715bcf2005-06-21 14:06:24 -07001468 nf_reset(skb);
Patrick McHardy0c122952010-04-13 05:03:22 +00001469 raw_rcv(mrt->mroute_sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 read_unlock(&mrt_lock);
1471 return 0;
1472 }
1473 read_unlock(&mrt_lock);
1474 }
1475 }
1476
1477 read_lock(&mrt_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001478 cache = ipmr_cache_find(mrt, ip_hdr(skb)->saddr, ip_hdr(skb)->daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479
1480 /*
1481 * No usable cache entry
1482 */
Jianjun Kongc354e122008-11-03 00:28:02 -08001483 if (cache == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 int vif;
1485
1486 if (local) {
1487 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
1488 ip_local_deliver(skb);
1489 if (skb2 == NULL) {
1490 read_unlock(&mrt_lock);
1491 return -ENOBUFS;
1492 }
1493 skb = skb2;
1494 }
1495
Patrick McHardy0c122952010-04-13 05:03:22 +00001496 vif = ipmr_find_vif(mrt, skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 if (vif >= 0) {
Patrick McHardy0c122952010-04-13 05:03:22 +00001498 int err = ipmr_cache_unresolved(mrt, vif, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 read_unlock(&mrt_lock);
1500
1501 return err;
1502 }
1503 read_unlock(&mrt_lock);
1504 kfree_skb(skb);
1505 return -ENODEV;
1506 }
1507
Patrick McHardy0c122952010-04-13 05:03:22 +00001508 ip_mr_forward(net, mrt, skb, cache, local);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509
1510 read_unlock(&mrt_lock);
1511
1512 if (local)
1513 return ip_local_deliver(skb);
1514
1515 return 0;
1516
1517dont_forward:
1518 if (local)
1519 return ip_local_deliver(skb);
1520 kfree_skb(skb);
1521 return 0;
1522}
1523
Ilpo Järvinenb1879202008-12-16 01:15:11 -08001524#ifdef CONFIG_IP_PIMSM
1525static int __pim_rcv(struct sk_buff *skb, unsigned int pimlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526{
Ilpo Järvinenb1879202008-12-16 01:15:11 -08001527 struct net_device *reg_dev = NULL;
1528 struct iphdr *encap;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001529 struct net *net = dev_net(skb->dev);
Patrick McHardy0c122952010-04-13 05:03:22 +00001530 struct mr_table *mrt = net->ipv4.mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531
Ilpo Järvinenb1879202008-12-16 01:15:11 -08001532 encap = (struct iphdr *)(skb_transport_header(skb) + pimlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 /*
1534 Check that:
1535 a. packet is really destinted to a multicast group
1536 b. packet is not a NULL-REGISTER
1537 c. packet is not truncated
1538 */
Joe Perchesf97c1e02007-12-16 13:45:43 -08001539 if (!ipv4_is_multicast(encap->daddr) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 encap->tot_len == 0 ||
Ilpo Järvinenb1879202008-12-16 01:15:11 -08001541 ntohs(encap->tot_len) + pimlen > skb->len)
1542 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543
1544 read_lock(&mrt_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001545 if (mrt->mroute_reg_vif_num >= 0)
1546 reg_dev = mrt->vif_table[mrt->mroute_reg_vif_num].dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 if (reg_dev)
1548 dev_hold(reg_dev);
1549 read_unlock(&mrt_lock);
1550
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001551 if (reg_dev == NULL)
Ilpo Järvinenb1879202008-12-16 01:15:11 -08001552 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07001554 skb->mac_header = skb->network_header;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 skb_pull(skb, (u8*)encap - skb->data);
Arnaldo Carvalho de Melo31c77112007-03-10 19:04:55 -03001556 skb_reset_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 skb->dev = reg_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 skb->protocol = htons(ETH_P_IP);
1559 skb->ip_summed = 0;
1560 skb->pkt_type = PACKET_HOST;
Eric Dumazetadf30902009-06-02 05:19:30 +00001561 skb_dst_drop(skb);
Pavel Emelyanovcf3677a2008-05-21 14:17:33 -07001562 reg_dev->stats.rx_bytes += skb->len;
1563 reg_dev->stats.rx_packets++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 nf_reset(skb);
1565 netif_rx(skb);
1566 dev_put(reg_dev);
Ilpo Järvinenb1879202008-12-16 01:15:11 -08001567
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 return 0;
Ilpo Järvinenb1879202008-12-16 01:15:11 -08001569}
1570#endif
1571
1572#ifdef CONFIG_IP_PIMSM_V1
1573/*
1574 * Handle IGMP messages of PIMv1
1575 */
1576
1577int pim_rcv_v1(struct sk_buff * skb)
1578{
1579 struct igmphdr *pim;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001580 struct net *net = dev_net(skb->dev);
Patrick McHardy0c122952010-04-13 05:03:22 +00001581 struct mr_table *mrt = net->ipv4.mrt;
Ilpo Järvinenb1879202008-12-16 01:15:11 -08001582
1583 if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr)))
1584 goto drop;
1585
1586 pim = igmp_hdr(skb);
1587
Patrick McHardy0c122952010-04-13 05:03:22 +00001588 if (!mrt->mroute_do_pim ||
Ilpo Järvinenb1879202008-12-16 01:15:11 -08001589 pim->group != PIM_V1_VERSION || pim->code != PIM_V1_REGISTER)
1590 goto drop;
1591
1592 if (__pim_rcv(skb, sizeof(*pim))) {
1593drop:
1594 kfree_skb(skb);
1595 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 return 0;
1597}
1598#endif
1599
1600#ifdef CONFIG_IP_PIMSM_V2
1601static int pim_rcv(struct sk_buff * skb)
1602{
1603 struct pimreghdr *pim;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604
Ilpo Järvinenb1879202008-12-16 01:15:11 -08001605 if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 goto drop;
1607
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -07001608 pim = (struct pimreghdr *)skb_transport_header(skb);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001609 if (pim->type != ((PIM_VERSION<<4)|(PIM_REGISTER)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 (pim->flags&PIM_NULL_REGISTER) ||
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001611 (ip_compute_csum((void *)pim, sizeof(*pim)) != 0 &&
Al Virod3bc23e2006-11-14 21:24:49 -08001612 csum_fold(skb_checksum(skb, 0, skb->len, 0))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 goto drop;
1614
Ilpo Järvinenb1879202008-12-16 01:15:11 -08001615 if (__pim_rcv(skb, sizeof(*pim))) {
1616drop:
1617 kfree_skb(skb);
1618 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 return 0;
1620}
1621#endif
1622
1623static int
Patrick McHardy0c122952010-04-13 05:03:22 +00001624ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb, struct mfc_cache *c,
Patrick McHardyd658f8a2010-04-13 05:03:20 +00001625 struct rtmsg *rtm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626{
1627 int ct;
1628 struct rtnexthop *nhp;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001629 u8 *b = skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 struct rtattr *mp_head;
1631
Nicolas Dichtel74381892010-03-25 23:45:35 +00001632 /* If cache is unresolved, don't try to parse IIF and OIF */
1633 if (c->mfc_parent > MAXVIFS)
1634 return -ENOENT;
1635
Patrick McHardy0c122952010-04-13 05:03:22 +00001636 if (VIF_EXISTS(mrt, c->mfc_parent))
1637 RTA_PUT(skb, RTA_IIF, 4, &mrt->vif_table[c->mfc_parent].dev->ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638
Jianjun Kongc354e122008-11-03 00:28:02 -08001639 mp_head = (struct rtattr *)skb_put(skb, RTA_LENGTH(0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640
1641 for (ct = c->mfc_un.res.minvif; ct < c->mfc_un.res.maxvif; ct++) {
Patrick McHardy0c122952010-04-13 05:03:22 +00001642 if (VIF_EXISTS(mrt, ct) && c->mfc_un.res.ttls[ct] < 255) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 if (skb_tailroom(skb) < RTA_ALIGN(RTA_ALIGN(sizeof(*nhp)) + 4))
1644 goto rtattr_failure;
Jianjun Kongc354e122008-11-03 00:28:02 -08001645 nhp = (struct rtnexthop *)skb_put(skb, RTA_ALIGN(sizeof(*nhp)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 nhp->rtnh_flags = 0;
1647 nhp->rtnh_hops = c->mfc_un.res.ttls[ct];
Patrick McHardy0c122952010-04-13 05:03:22 +00001648 nhp->rtnh_ifindex = mrt->vif_table[ct].dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 nhp->rtnh_len = sizeof(*nhp);
1650 }
1651 }
1652 mp_head->rta_type = RTA_MULTIPATH;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001653 mp_head->rta_len = skb_tail_pointer(skb) - (u8 *)mp_head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 rtm->rtm_type = RTN_MULTICAST;
1655 return 1;
1656
1657rtattr_failure:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -07001658 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 return -EMSGSIZE;
1660}
1661
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001662int ipmr_get_route(struct net *net,
1663 struct sk_buff *skb, struct rtmsg *rtm, int nowait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664{
1665 int err;
Patrick McHardy0c122952010-04-13 05:03:22 +00001666 struct mr_table *mrt = net->ipv4.mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 struct mfc_cache *cache;
Eric Dumazet511c3f92009-06-02 05:14:27 +00001668 struct rtable *rt = skb_rtable(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669
1670 read_lock(&mrt_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001671 cache = ipmr_cache_find(mrt, rt->rt_src, rt->rt_dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672
Jianjun Kongc354e122008-11-03 00:28:02 -08001673 if (cache == NULL) {
Alexey Kuznetsov72287492006-07-25 16:45:12 -07001674 struct sk_buff *skb2;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001675 struct iphdr *iph;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 struct net_device *dev;
1677 int vif;
1678
1679 if (nowait) {
1680 read_unlock(&mrt_lock);
1681 return -EAGAIN;
1682 }
1683
1684 dev = skb->dev;
Patrick McHardy0c122952010-04-13 05:03:22 +00001685 if (dev == NULL || (vif = ipmr_find_vif(mrt, dev)) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 read_unlock(&mrt_lock);
1687 return -ENODEV;
1688 }
Alexey Kuznetsov72287492006-07-25 16:45:12 -07001689 skb2 = skb_clone(skb, GFP_ATOMIC);
1690 if (!skb2) {
1691 read_unlock(&mrt_lock);
1692 return -ENOMEM;
1693 }
1694
Arnaldo Carvalho de Meloe2d1bca2007-04-10 20:46:21 -07001695 skb_push(skb2, sizeof(struct iphdr));
1696 skb_reset_network_header(skb2);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001697 iph = ip_hdr(skb2);
1698 iph->ihl = sizeof(struct iphdr) >> 2;
1699 iph->saddr = rt->rt_src;
1700 iph->daddr = rt->rt_dst;
1701 iph->version = 0;
Patrick McHardy0c122952010-04-13 05:03:22 +00001702 err = ipmr_cache_unresolved(mrt, vif, skb2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 read_unlock(&mrt_lock);
1704 return err;
1705 }
1706
1707 if (!nowait && (rtm->rtm_flags&RTM_F_NOTIFY))
1708 cache->mfc_flags |= MFC_NOTIFY;
Patrick McHardy0c122952010-04-13 05:03:22 +00001709 err = ipmr_fill_mroute(mrt, skb, cache, rtm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 read_unlock(&mrt_lock);
1711 return err;
1712}
1713
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001714#ifdef CONFIG_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715/*
1716 * The /proc interfaces to multicast routing /proc/ip_mr_cache /proc/ip_mr_vif
1717 */
1718struct ipmr_vif_iter {
Benjamin Theryf6bb4512009-01-22 04:56:22 +00001719 struct seq_net_private p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 int ct;
1721};
1722
Benjamin Theryf6bb4512009-01-22 04:56:22 +00001723static struct vif_device *ipmr_vif_seq_idx(struct net *net,
1724 struct ipmr_vif_iter *iter,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 loff_t pos)
1726{
Patrick McHardy0c122952010-04-13 05:03:22 +00001727 struct mr_table *mrt = net->ipv4.mrt;
1728
1729 for (iter->ct = 0; iter->ct < mrt->maxvif; ++iter->ct) {
1730 if (!VIF_EXISTS(mrt, iter->ct))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 continue;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001732 if (pos-- == 0)
Patrick McHardy0c122952010-04-13 05:03:22 +00001733 return &mrt->vif_table[iter->ct];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 }
1735 return NULL;
1736}
1737
1738static void *ipmr_vif_seq_start(struct seq_file *seq, loff_t *pos)
Stephen Hemmingerba93ef72008-01-21 17:28:59 -08001739 __acquires(mrt_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740{
Benjamin Theryf6bb4512009-01-22 04:56:22 +00001741 struct net *net = seq_file_net(seq);
1742
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 read_lock(&mrt_lock);
Benjamin Theryf6bb4512009-01-22 04:56:22 +00001744 return *pos ? ipmr_vif_seq_idx(net, seq->private, *pos - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 : SEQ_START_TOKEN;
1746}
1747
1748static void *ipmr_vif_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1749{
1750 struct ipmr_vif_iter *iter = seq->private;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00001751 struct net *net = seq_file_net(seq);
Patrick McHardy0c122952010-04-13 05:03:22 +00001752 struct mr_table *mrt = net->ipv4.mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753
1754 ++*pos;
1755 if (v == SEQ_START_TOKEN)
Benjamin Theryf6bb4512009-01-22 04:56:22 +00001756 return ipmr_vif_seq_idx(net, iter, 0);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001757
Patrick McHardy0c122952010-04-13 05:03:22 +00001758 while (++iter->ct < mrt->maxvif) {
1759 if (!VIF_EXISTS(mrt, iter->ct))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 continue;
Patrick McHardy0c122952010-04-13 05:03:22 +00001761 return &mrt->vif_table[iter->ct];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 }
1763 return NULL;
1764}
1765
1766static void ipmr_vif_seq_stop(struct seq_file *seq, void *v)
Stephen Hemmingerba93ef72008-01-21 17:28:59 -08001767 __releases(mrt_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768{
1769 read_unlock(&mrt_lock);
1770}
1771
1772static int ipmr_vif_seq_show(struct seq_file *seq, void *v)
1773{
Benjamin Theryf6bb4512009-01-22 04:56:22 +00001774 struct net *net = seq_file_net(seq);
Patrick McHardy0c122952010-04-13 05:03:22 +00001775 struct mr_table *mrt = net->ipv4.mrt;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00001776
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 if (v == SEQ_START_TOKEN) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001778 seq_puts(seq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 "Interface BytesIn PktsIn BytesOut PktsOut Flags Local Remote\n");
1780 } else {
1781 const struct vif_device *vif = v;
1782 const char *name = vif->dev ? vif->dev->name : "none";
1783
1784 seq_printf(seq,
1785 "%2Zd %-10s %8ld %7ld %8ld %7ld %05X %08X %08X\n",
Patrick McHardy0c122952010-04-13 05:03:22 +00001786 vif - mrt->vif_table,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001787 name, vif->bytes_in, vif->pkt_in,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 vif->bytes_out, vif->pkt_out,
1789 vif->flags, vif->local, vif->remote);
1790 }
1791 return 0;
1792}
1793
Stephen Hemmingerf6908082007-03-12 14:34:29 -07001794static const struct seq_operations ipmr_vif_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 .start = ipmr_vif_seq_start,
1796 .next = ipmr_vif_seq_next,
1797 .stop = ipmr_vif_seq_stop,
1798 .show = ipmr_vif_seq_show,
1799};
1800
1801static int ipmr_vif_open(struct inode *inode, struct file *file)
1802{
Benjamin Theryf6bb4512009-01-22 04:56:22 +00001803 return seq_open_net(inode, file, &ipmr_vif_seq_ops,
1804 sizeof(struct ipmr_vif_iter));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805}
1806
Arjan van de Ven9a321442007-02-12 00:55:35 -08001807static const struct file_operations ipmr_vif_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 .owner = THIS_MODULE,
1809 .open = ipmr_vif_open,
1810 .read = seq_read,
1811 .llseek = seq_lseek,
Benjamin Theryf6bb4512009-01-22 04:56:22 +00001812 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813};
1814
1815struct ipmr_mfc_iter {
Benjamin Theryf6bb4512009-01-22 04:56:22 +00001816 struct seq_net_private p;
Patrick McHardy862465f2010-04-13 05:03:21 +00001817 struct list_head *cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 int ct;
1819};
1820
1821
Benjamin Theryf6bb4512009-01-22 04:56:22 +00001822static struct mfc_cache *ipmr_mfc_seq_idx(struct net *net,
1823 struct ipmr_mfc_iter *it, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824{
Patrick McHardy0c122952010-04-13 05:03:22 +00001825 struct mr_table *mrt = net->ipv4.mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 struct mfc_cache *mfc;
1827
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 read_lock(&mrt_lock);
Patrick McHardy862465f2010-04-13 05:03:21 +00001829 for (it->ct = 0; it->ct < MFC_LINES; it->ct++) {
Patrick McHardy0c122952010-04-13 05:03:22 +00001830 it->cache = &mrt->mfc_cache_array[it->ct];
Patrick McHardy862465f2010-04-13 05:03:21 +00001831 list_for_each_entry(mfc, it->cache, list)
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001832 if (pos-- == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 return mfc;
Patrick McHardy862465f2010-04-13 05:03:21 +00001834 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 read_unlock(&mrt_lock);
1836
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001838 it->cache = &mrt->mfc_unres_queue;
Patrick McHardy862465f2010-04-13 05:03:21 +00001839 list_for_each_entry(mfc, it->cache, list)
Patrick McHardye258beb2010-04-13 05:03:19 +00001840 if (pos-- == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 return mfc;
1842 spin_unlock_bh(&mfc_unres_lock);
1843
1844 it->cache = NULL;
1845 return NULL;
1846}
1847
1848
1849static void *ipmr_mfc_seq_start(struct seq_file *seq, loff_t *pos)
1850{
1851 struct ipmr_mfc_iter *it = seq->private;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00001852 struct net *net = seq_file_net(seq);
1853
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 it->cache = NULL;
1855 it->ct = 0;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00001856 return *pos ? ipmr_mfc_seq_idx(net, seq->private, *pos - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 : SEQ_START_TOKEN;
1858}
1859
1860static void *ipmr_mfc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1861{
1862 struct mfc_cache *mfc = v;
1863 struct ipmr_mfc_iter *it = seq->private;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00001864 struct net *net = seq_file_net(seq);
Patrick McHardy0c122952010-04-13 05:03:22 +00001865 struct mr_table *mrt = net->ipv4.mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866
1867 ++*pos;
1868
1869 if (v == SEQ_START_TOKEN)
Benjamin Theryf6bb4512009-01-22 04:56:22 +00001870 return ipmr_mfc_seq_idx(net, seq->private, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871
Patrick McHardy862465f2010-04-13 05:03:21 +00001872 if (mfc->list.next != it->cache)
1873 return list_entry(mfc->list.next, struct mfc_cache, list);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001874
Patrick McHardy0c122952010-04-13 05:03:22 +00001875 if (it->cache == &mrt->mfc_unres_queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 goto end_of_list;
1877
Patrick McHardy0c122952010-04-13 05:03:22 +00001878 BUG_ON(it->cache != &mrt->mfc_cache_array[it->ct]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879
1880 while (++it->ct < MFC_LINES) {
Patrick McHardy0c122952010-04-13 05:03:22 +00001881 it->cache = &mrt->mfc_cache_array[it->ct];
Patrick McHardy862465f2010-04-13 05:03:21 +00001882 if (list_empty(it->cache))
1883 continue;
1884 return list_first_entry(it->cache, struct mfc_cache, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 }
1886
1887 /* exhausted cache_array, show unresolved */
1888 read_unlock(&mrt_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001889 it->cache = &mrt->mfc_unres_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 it->ct = 0;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001891
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy862465f2010-04-13 05:03:21 +00001893 if (!list_empty(it->cache))
1894 return list_first_entry(it->cache, struct mfc_cache, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895
1896 end_of_list:
1897 spin_unlock_bh(&mfc_unres_lock);
1898 it->cache = NULL;
1899
1900 return NULL;
1901}
1902
1903static void ipmr_mfc_seq_stop(struct seq_file *seq, void *v)
1904{
1905 struct ipmr_mfc_iter *it = seq->private;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00001906 struct net *net = seq_file_net(seq);
Patrick McHardy0c122952010-04-13 05:03:22 +00001907 struct mr_table *mrt = net->ipv4.mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908
Patrick McHardy0c122952010-04-13 05:03:22 +00001909 if (it->cache == &mrt->mfc_unres_queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 spin_unlock_bh(&mfc_unres_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001911 else if (it->cache == &mrt->mfc_cache_array[it->ct])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 read_unlock(&mrt_lock);
1913}
1914
1915static int ipmr_mfc_seq_show(struct seq_file *seq, void *v)
1916{
1917 int n;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00001918 struct net *net = seq_file_net(seq);
Patrick McHardy0c122952010-04-13 05:03:22 +00001919 struct mr_table *mrt = net->ipv4.mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920
1921 if (v == SEQ_START_TOKEN) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001922 seq_puts(seq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 "Group Origin Iif Pkts Bytes Wrong Oifs\n");
1924 } else {
1925 const struct mfc_cache *mfc = v;
1926 const struct ipmr_mfc_iter *it = seq->private;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001927
Benjamin Thery999890b2008-12-03 22:22:16 -08001928 seq_printf(seq, "%08lX %08lX %-3hd",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 (unsigned long) mfc->mfc_mcastgrp,
1930 (unsigned long) mfc->mfc_origin,
Benjamin Thery1ea472e2008-12-03 22:21:47 -08001931 mfc->mfc_parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932
Patrick McHardy0c122952010-04-13 05:03:22 +00001933 if (it->cache != &mrt->mfc_unres_queue) {
Benjamin Thery1ea472e2008-12-03 22:21:47 -08001934 seq_printf(seq, " %8lu %8lu %8lu",
1935 mfc->mfc_un.res.pkt,
1936 mfc->mfc_un.res.bytes,
1937 mfc->mfc_un.res.wrong_if);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001938 for (n = mfc->mfc_un.res.minvif;
1939 n < mfc->mfc_un.res.maxvif; n++ ) {
Patrick McHardy0c122952010-04-13 05:03:22 +00001940 if (VIF_EXISTS(mrt, n) &&
Benjamin Therycf958ae32009-01-22 04:56:16 +00001941 mfc->mfc_un.res.ttls[n] < 255)
1942 seq_printf(seq,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001943 " %2d:%-3d",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 n, mfc->mfc_un.res.ttls[n]);
1945 }
Benjamin Thery1ea472e2008-12-03 22:21:47 -08001946 } else {
1947 /* unresolved mfc_caches don't contain
1948 * pkt, bytes and wrong_if values
1949 */
1950 seq_printf(seq, " %8lu %8lu %8lu", 0ul, 0ul, 0ul);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 }
1952 seq_putc(seq, '\n');
1953 }
1954 return 0;
1955}
1956
Stephen Hemmingerf6908082007-03-12 14:34:29 -07001957static const struct seq_operations ipmr_mfc_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 .start = ipmr_mfc_seq_start,
1959 .next = ipmr_mfc_seq_next,
1960 .stop = ipmr_mfc_seq_stop,
1961 .show = ipmr_mfc_seq_show,
1962};
1963
1964static int ipmr_mfc_open(struct inode *inode, struct file *file)
1965{
Benjamin Theryf6bb4512009-01-22 04:56:22 +00001966 return seq_open_net(inode, file, &ipmr_mfc_seq_ops,
1967 sizeof(struct ipmr_mfc_iter));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968}
1969
Arjan van de Ven9a321442007-02-12 00:55:35 -08001970static const struct file_operations ipmr_mfc_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 .owner = THIS_MODULE,
1972 .open = ipmr_mfc_open,
1973 .read = seq_read,
1974 .llseek = seq_lseek,
Benjamin Theryf6bb4512009-01-22 04:56:22 +00001975 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976};
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001977#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978
1979#ifdef CONFIG_IP_PIMSM_V2
Alexey Dobriyan32613092009-09-14 12:21:47 +00001980static const struct net_protocol pim_protocol = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 .handler = pim_rcv,
Tom Goff403dbb92009-06-14 03:16:13 -07001982 .netns_ok = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983};
1984#endif
1985
1986
1987/*
1988 * Setup for IP multicast routing
1989 */
Benjamin Therycf958ae32009-01-22 04:56:16 +00001990static int __net_init ipmr_net_init(struct net *net)
1991{
Patrick McHardy0c122952010-04-13 05:03:22 +00001992 struct mr_table *mrt;
Patrick McHardy862465f2010-04-13 05:03:21 +00001993 unsigned int i;
Benjamin Therycf958ae32009-01-22 04:56:16 +00001994 int err = 0;
1995
Patrick McHardy0c122952010-04-13 05:03:22 +00001996 mrt = kzalloc(sizeof(*mrt), GFP_KERNEL);
1997 if (mrt == NULL) {
Benjamin Therycf958ae32009-01-22 04:56:16 +00001998 err = -ENOMEM;
1999 goto fail;
2000 }
Benjamin Thery2bb8b262009-01-22 04:56:18 +00002001
2002 /* Forwarding cache */
Patrick McHardy862465f2010-04-13 05:03:21 +00002003 for (i = 0; i < MFC_LINES; i++)
Patrick McHardy0c122952010-04-13 05:03:22 +00002004 INIT_LIST_HEAD(&mrt->mfc_cache_array[i]);
Patrick McHardy862465f2010-04-13 05:03:21 +00002005
Patrick McHardy0c122952010-04-13 05:03:22 +00002006 INIT_LIST_HEAD(&mrt->mfc_unres_queue);
Patrick McHardy862465f2010-04-13 05:03:21 +00002007
Patrick McHardy0c122952010-04-13 05:03:22 +00002008 setup_timer(&mrt->ipmr_expire_timer, ipmr_expire_process,
Patrick McHardye258beb2010-04-13 05:03:19 +00002009 (unsigned long)net);
2010
Benjamin Thery6c5143d2009-01-22 04:56:21 +00002011#ifdef CONFIG_IP_PIMSM
Patrick McHardy0c122952010-04-13 05:03:22 +00002012 mrt->mroute_reg_vif_num = -1;
Benjamin Thery6c5143d2009-01-22 04:56:21 +00002013#endif
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002014
2015#ifdef CONFIG_PROC_FS
2016 err = -ENOMEM;
2017 if (!proc_net_fops_create(net, "ip_mr_vif", 0, &ipmr_vif_fops))
2018 goto proc_vif_fail;
2019 if (!proc_net_fops_create(net, "ip_mr_cache", 0, &ipmr_mfc_fops))
2020 goto proc_cache_fail;
2021#endif
Patrick McHardy0c122952010-04-13 05:03:22 +00002022
2023 net->ipv4.mrt = mrt;
Benjamin Thery2bb8b262009-01-22 04:56:18 +00002024 return 0;
2025
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002026#ifdef CONFIG_PROC_FS
2027proc_cache_fail:
2028 proc_net_remove(net, "ip_mr_vif");
2029proc_vif_fail:
Patrick McHardy0c122952010-04-13 05:03:22 +00002030 kfree(mrt);
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002031#endif
Benjamin Therycf958ae32009-01-22 04:56:16 +00002032fail:
2033 return err;
2034}
2035
2036static void __net_exit ipmr_net_exit(struct net *net)
2037{
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002038#ifdef CONFIG_PROC_FS
2039 proc_net_remove(net, "ip_mr_cache");
2040 proc_net_remove(net, "ip_mr_vif");
2041#endif
Patrick McHardy0c122952010-04-13 05:03:22 +00002042 kfree(net->ipv4.mrt);
Benjamin Therycf958ae32009-01-22 04:56:16 +00002043}
2044
2045static struct pernet_operations ipmr_net_ops = {
2046 .init = ipmr_net_init,
2047 .exit = ipmr_net_exit,
2048};
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002049
Wang Chen03d2f892008-07-03 12:13:36 +08002050int __init ip_mr_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051{
Wang Chen03d2f892008-07-03 12:13:36 +08002052 int err;
2053
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 mrt_cachep = kmem_cache_create("ip_mrt_cache",
2055 sizeof(struct mfc_cache),
Alexey Dobriyane5d679f332006-08-26 19:25:52 -07002056 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09002057 NULL);
Wang Chen03d2f892008-07-03 12:13:36 +08002058 if (!mrt_cachep)
2059 return -ENOMEM;
2060
Benjamin Therycf958ae32009-01-22 04:56:16 +00002061 err = register_pernet_subsys(&ipmr_net_ops);
2062 if (err)
2063 goto reg_pernet_fail;
2064
Wang Chen03d2f892008-07-03 12:13:36 +08002065 err = register_netdevice_notifier(&ip_mr_notifier);
2066 if (err)
2067 goto reg_notif_fail;
Tom Goff403dbb92009-06-14 03:16:13 -07002068#ifdef CONFIG_IP_PIMSM_V2
2069 if (inet_add_protocol(&pim_protocol, IPPROTO_PIM) < 0) {
2070 printk(KERN_ERR "ip_mr_init: can't add PIM protocol\n");
2071 err = -EAGAIN;
2072 goto add_proto_fail;
2073 }
2074#endif
Wang Chen03d2f892008-07-03 12:13:36 +08002075 return 0;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002076
Tom Goff403dbb92009-06-14 03:16:13 -07002077#ifdef CONFIG_IP_PIMSM_V2
2078add_proto_fail:
2079 unregister_netdevice_notifier(&ip_mr_notifier);
2080#endif
Benjamin Theryc3e38892008-11-19 14:07:41 -08002081reg_notif_fail:
Benjamin Therycf958ae32009-01-22 04:56:16 +00002082 unregister_pernet_subsys(&ipmr_net_ops);
2083reg_pernet_fail:
Benjamin Theryc3e38892008-11-19 14:07:41 -08002084 kmem_cache_destroy(mrt_cachep);
Wang Chen03d2f892008-07-03 12:13:36 +08002085 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086}