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