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