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