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