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