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