1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * IP multicast routing support for mrouted 3.6/3.8
5 * (c) 1995 Alan Cox, <alan@lxorguk.ukuu.org.uk>
6 * Linux Consultancy and Custom Driver Development
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
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.
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>
30 #include <linux/kernel.h>
31 #include <linux/fcntl.h>
32 #include <linux/stat.h>
33 #include <linux/socket.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>
47 #include <net/protocol.h>
48 #include <linux/skbuff.h>
49 #include <net/route.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>
66 #include <linux/nospec.h>
69 struct fib_rule common
;
76 /* Big lock, protecting vif table, mrt cache and mroute socket state.
77 * Note that the changes are semaphored via rtnl_lock.
80 static DEFINE_RWLOCK(mrt_lock
);
82 /* Multicast router control variables */
84 /* Special spinlock for queue of unresolved entries */
85 static DEFINE_SPINLOCK(mfc_unres_lock
);
87 /* We return to original Alan's scheme. Hash table of resolved
88 * entries is changed only in process context and protected
89 * with weak lock mrt_lock. Queue of unresolved entries is protected
90 * with strong spinlock mfc_unres_lock.
92 * In this case data path is free of exclusive locks at all.
95 static struct kmem_cache
*mrt_cachep __ro_after_init
;
97 static struct mr_table
*ipmr_new_table(struct net
*net
, u32 id
);
98 static void ipmr_free_table(struct mr_table
*mrt
);
100 static void ip_mr_forward(struct net
*net
, struct mr_table
*mrt
,
101 struct net_device
*dev
, struct sk_buff
*skb
,
102 struct mfc_cache
*cache
, int local
);
103 static int ipmr_cache_report(struct mr_table
*mrt
,
104 struct sk_buff
*pkt
, vifi_t vifi
, int assert);
105 static void mroute_netlink_event(struct mr_table
*mrt
, struct mfc_cache
*mfc
,
107 static void igmpmsg_netlink_event(struct mr_table
*mrt
, struct sk_buff
*pkt
);
108 static void mroute_clean_tables(struct mr_table
*mrt
, int flags
);
109 static void ipmr_expire_process(struct timer_list
*t
);
111 #ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
112 #define ipmr_for_each_table(mrt, net) \
113 list_for_each_entry_rcu(mrt, &net->ipv4.mr_tables, list)
115 static struct mr_table
*ipmr_mr_table_iter(struct net
*net
,
116 struct mr_table
*mrt
)
118 struct mr_table
*ret
;
121 ret
= list_entry_rcu(net
->ipv4
.mr_tables
.next
,
122 struct mr_table
, list
);
124 ret
= list_entry_rcu(mrt
->list
.next
,
125 struct mr_table
, list
);
127 if (&ret
->list
== &net
->ipv4
.mr_tables
)
132 static struct mr_table
*ipmr_get_table(struct net
*net
, u32 id
)
134 struct mr_table
*mrt
;
136 ipmr_for_each_table(mrt
, net
) {
143 static int ipmr_fib_lookup(struct net
*net
, struct flowi4
*flp4
,
144 struct mr_table
**mrt
)
147 struct ipmr_result res
;
148 struct fib_lookup_arg arg
= {
150 .flags
= FIB_LOOKUP_NOREF
,
153 /* update flow if oif or iif point to device enslaved to l3mdev */
154 l3mdev_update_flow(net
, flowi4_to_flowi(flp4
));
156 err
= fib_rules_lookup(net
->ipv4
.mr_rules_ops
,
157 flowi4_to_flowi(flp4
), 0, &arg
);
164 static int ipmr_rule_action(struct fib_rule
*rule
, struct flowi
*flp
,
165 int flags
, struct fib_lookup_arg
*arg
)
167 struct ipmr_result
*res
= arg
->result
;
168 struct mr_table
*mrt
;
170 switch (rule
->action
) {
173 case FR_ACT_UNREACHABLE
:
175 case FR_ACT_PROHIBIT
:
177 case FR_ACT_BLACKHOLE
:
182 arg
->table
= fib_rule_get_table(rule
, arg
);
184 mrt
= ipmr_get_table(rule
->fr_net
, arg
->table
);
191 static int ipmr_rule_match(struct fib_rule
*rule
, struct flowi
*fl
, int flags
)
196 static const struct nla_policy ipmr_rule_policy
[FRA_MAX
+ 1] = {
200 static int ipmr_rule_configure(struct fib_rule
*rule
, struct sk_buff
*skb
,
201 struct fib_rule_hdr
*frh
, struct nlattr
**tb
,
202 struct netlink_ext_ack
*extack
)
207 static int ipmr_rule_compare(struct fib_rule
*rule
, struct fib_rule_hdr
*frh
,
213 static int ipmr_rule_fill(struct fib_rule
*rule
, struct sk_buff
*skb
,
214 struct fib_rule_hdr
*frh
)
222 static const struct fib_rules_ops __net_initconst ipmr_rules_ops_template
= {
223 .family
= RTNL_FAMILY_IPMR
,
224 .rule_size
= sizeof(struct ipmr_rule
),
225 .addr_size
= sizeof(u32
),
226 .action
= ipmr_rule_action
,
227 .match
= ipmr_rule_match
,
228 .configure
= ipmr_rule_configure
,
229 .compare
= ipmr_rule_compare
,
230 .fill
= ipmr_rule_fill
,
231 .nlgroup
= RTNLGRP_IPV4_RULE
,
232 .policy
= ipmr_rule_policy
,
233 .owner
= THIS_MODULE
,
236 static int __net_init
ipmr_rules_init(struct net
*net
)
238 struct fib_rules_ops
*ops
;
239 struct mr_table
*mrt
;
242 ops
= fib_rules_register(&ipmr_rules_ops_template
, net
);
246 INIT_LIST_HEAD(&net
->ipv4
.mr_tables
);
248 mrt
= ipmr_new_table(net
, RT_TABLE_DEFAULT
);
254 err
= fib_default_rule_add(ops
, 0x7fff, RT_TABLE_DEFAULT
, 0);
258 net
->ipv4
.mr_rules_ops
= ops
;
262 ipmr_free_table(mrt
);
264 fib_rules_unregister(ops
);
268 static void __net_exit
ipmr_rules_exit(struct net
*net
)
270 struct mr_table
*mrt
, *next
;
273 list_for_each_entry_safe(mrt
, next
, &net
->ipv4
.mr_tables
, list
) {
274 list_del(&mrt
->list
);
275 ipmr_free_table(mrt
);
277 fib_rules_unregister(net
->ipv4
.mr_rules_ops
);
281 static int ipmr_rules_dump(struct net
*net
, struct notifier_block
*nb
,
282 struct netlink_ext_ack
*extack
)
284 return fib_rules_dump(net
, nb
, RTNL_FAMILY_IPMR
, extack
);
287 static unsigned int ipmr_rules_seq_read(struct net
*net
)
289 return fib_rules_seq_read(net
, RTNL_FAMILY_IPMR
);
292 bool ipmr_rule_default(const struct fib_rule
*rule
)
294 return fib_rule_matchall(rule
) && rule
->table
== RT_TABLE_DEFAULT
;
296 EXPORT_SYMBOL(ipmr_rule_default
);
298 #define ipmr_for_each_table(mrt, net) \
299 for (mrt = net->ipv4.mrt; mrt; mrt = NULL)
301 static struct mr_table
*ipmr_mr_table_iter(struct net
*net
,
302 struct mr_table
*mrt
)
305 return net
->ipv4
.mrt
;
309 static struct mr_table
*ipmr_get_table(struct net
*net
, u32 id
)
311 return net
->ipv4
.mrt
;
314 static int ipmr_fib_lookup(struct net
*net
, struct flowi4
*flp4
,
315 struct mr_table
**mrt
)
317 *mrt
= net
->ipv4
.mrt
;
321 static int __net_init
ipmr_rules_init(struct net
*net
)
323 struct mr_table
*mrt
;
325 mrt
= ipmr_new_table(net
, RT_TABLE_DEFAULT
);
332 static void __net_exit
ipmr_rules_exit(struct net
*net
)
335 ipmr_free_table(net
->ipv4
.mrt
);
336 net
->ipv4
.mrt
= NULL
;
340 static int ipmr_rules_dump(struct net
*net
, struct notifier_block
*nb
,
341 struct netlink_ext_ack
*extack
)
346 static unsigned int ipmr_rules_seq_read(struct net
*net
)
351 bool ipmr_rule_default(const struct fib_rule
*rule
)
355 EXPORT_SYMBOL(ipmr_rule_default
);
358 static inline int ipmr_hash_cmp(struct rhashtable_compare_arg
*arg
,
361 const struct mfc_cache_cmp_arg
*cmparg
= arg
->key
;
362 struct mfc_cache
*c
= (struct mfc_cache
*)ptr
;
364 return cmparg
->mfc_mcastgrp
!= c
->mfc_mcastgrp
||
365 cmparg
->mfc_origin
!= c
->mfc_origin
;
368 static const struct rhashtable_params ipmr_rht_params
= {
369 .head_offset
= offsetof(struct mr_mfc
, mnode
),
370 .key_offset
= offsetof(struct mfc_cache
, cmparg
),
371 .key_len
= sizeof(struct mfc_cache_cmp_arg
),
373 .obj_cmpfn
= ipmr_hash_cmp
,
374 .automatic_shrinking
= true,
377 static void ipmr_new_table_set(struct mr_table
*mrt
,
380 #ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
381 list_add_tail_rcu(&mrt
->list
, &net
->ipv4
.mr_tables
);
385 static struct mfc_cache_cmp_arg ipmr_mr_table_ops_cmparg_any
= {
386 .mfc_mcastgrp
= htonl(INADDR_ANY
),
387 .mfc_origin
= htonl(INADDR_ANY
),
390 static struct mr_table_ops ipmr_mr_table_ops
= {
391 .rht_params
= &ipmr_rht_params
,
392 .cmparg_any
= &ipmr_mr_table_ops_cmparg_any
,
395 static struct mr_table
*ipmr_new_table(struct net
*net
, u32 id
)
397 struct mr_table
*mrt
;
399 /* "pimreg%u" should not exceed 16 bytes (IFNAMSIZ) */
400 if (id
!= RT_TABLE_DEFAULT
&& id
>= 1000000000)
401 return ERR_PTR(-EINVAL
);
403 mrt
= ipmr_get_table(net
, id
);
407 return mr_table_alloc(net
, id
, &ipmr_mr_table_ops
,
408 ipmr_expire_process
, ipmr_new_table_set
);
411 static void ipmr_free_table(struct mr_table
*mrt
)
413 del_timer_sync(&mrt
->ipmr_expire_timer
);
414 mroute_clean_tables(mrt
, MRT_FLUSH_VIFS
| MRT_FLUSH_VIFS_STATIC
|
415 MRT_FLUSH_MFC
| MRT_FLUSH_MFC_STATIC
);
416 rhltable_destroy(&mrt
->mfc_hash
);
420 /* Service routines creating virtual interfaces: DVMRP tunnels and PIMREG */
422 static void ipmr_del_tunnel(struct net_device
*dev
, struct vifctl
*v
)
424 struct net
*net
= dev_net(dev
);
428 dev
= __dev_get_by_name(net
, "tunl0");
430 const struct net_device_ops
*ops
= dev
->netdev_ops
;
432 struct ip_tunnel_parm p
;
434 memset(&p
, 0, sizeof(p
));
435 p
.iph
.daddr
= v
->vifc_rmt_addr
.s_addr
;
436 p
.iph
.saddr
= v
->vifc_lcl_addr
.s_addr
;
439 p
.iph
.protocol
= IPPROTO_IPIP
;
440 sprintf(p
.name
, "dvmrp%d", v
->vifc_vifi
);
441 ifr
.ifr_ifru
.ifru_data
= (__force
void __user
*)&p
;
443 if (ops
->ndo_do_ioctl
) {
444 mm_segment_t oldfs
= get_fs();
447 ops
->ndo_do_ioctl(dev
, &ifr
, SIOCDELTUNNEL
);
453 /* Initialize ipmr pimreg/tunnel in_device */
454 static bool ipmr_init_vif_indev(const struct net_device
*dev
)
456 struct in_device
*in_dev
;
460 in_dev
= __in_dev_get_rtnl(dev
);
463 ipv4_devconf_setall(in_dev
);
464 neigh_parms_data_state_setall(in_dev
->arp_parms
);
465 IPV4_DEVCONF(in_dev
->cnf
, RP_FILTER
) = 0;
470 static struct net_device
*ipmr_new_tunnel(struct net
*net
, struct vifctl
*v
)
472 struct net_device
*dev
;
474 dev
= __dev_get_by_name(net
, "tunl0");
477 const struct net_device_ops
*ops
= dev
->netdev_ops
;
480 struct ip_tunnel_parm p
;
482 memset(&p
, 0, sizeof(p
));
483 p
.iph
.daddr
= v
->vifc_rmt_addr
.s_addr
;
484 p
.iph
.saddr
= v
->vifc_lcl_addr
.s_addr
;
487 p
.iph
.protocol
= IPPROTO_IPIP
;
488 sprintf(p
.name
, "dvmrp%d", v
->vifc_vifi
);
489 ifr
.ifr_ifru
.ifru_data
= (__force
void __user
*)&p
;
491 if (ops
->ndo_do_ioctl
) {
492 mm_segment_t oldfs
= get_fs();
495 err
= ops
->ndo_do_ioctl(dev
, &ifr
, SIOCADDTUNNEL
);
503 (dev
= __dev_get_by_name(net
, p
.name
)) != NULL
) {
504 dev
->flags
|= IFF_MULTICAST
;
505 if (!ipmr_init_vif_indev(dev
))
507 if (dev_open(dev
, NULL
))
515 unregister_netdevice(dev
);
519 #if defined(CONFIG_IP_PIMSM_V1) || defined(CONFIG_IP_PIMSM_V2)
520 static netdev_tx_t
reg_vif_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
522 struct net
*net
= dev_net(dev
);
523 struct mr_table
*mrt
;
524 struct flowi4 fl4
= {
525 .flowi4_oif
= dev
->ifindex
,
526 .flowi4_iif
= skb
->skb_iif
? : LOOPBACK_IFINDEX
,
527 .flowi4_mark
= skb
->mark
,
531 err
= ipmr_fib_lookup(net
, &fl4
, &mrt
);
537 read_lock(&mrt_lock
);
538 dev
->stats
.tx_bytes
+= skb
->len
;
539 dev
->stats
.tx_packets
++;
540 ipmr_cache_report(mrt
, skb
, mrt
->mroute_reg_vif_num
, IGMPMSG_WHOLEPKT
);
541 read_unlock(&mrt_lock
);
546 static int reg_vif_get_iflink(const struct net_device
*dev
)
551 static const struct net_device_ops reg_vif_netdev_ops
= {
552 .ndo_start_xmit
= reg_vif_xmit
,
553 .ndo_get_iflink
= reg_vif_get_iflink
,
556 static void reg_vif_setup(struct net_device
*dev
)
558 dev
->type
= ARPHRD_PIMREG
;
559 dev
->mtu
= ETH_DATA_LEN
- sizeof(struct iphdr
) - 8;
560 dev
->flags
= IFF_NOARP
;
561 dev
->netdev_ops
= ®_vif_netdev_ops
;
562 dev
->needs_free_netdev
= true;
563 dev
->features
|= NETIF_F_NETNS_LOCAL
;
566 static struct net_device
*ipmr_reg_vif(struct net
*net
, struct mr_table
*mrt
)
568 struct net_device
*dev
;
571 if (mrt
->id
== RT_TABLE_DEFAULT
)
572 sprintf(name
, "pimreg");
574 sprintf(name
, "pimreg%u", mrt
->id
);
576 dev
= alloc_netdev(0, name
, NET_NAME_UNKNOWN
, reg_vif_setup
);
581 dev_net_set(dev
, net
);
583 if (register_netdevice(dev
)) {
588 if (!ipmr_init_vif_indev(dev
))
590 if (dev_open(dev
, NULL
))
598 unregister_netdevice(dev
);
602 /* called with rcu_read_lock() */
603 static int __pim_rcv(struct mr_table
*mrt
, struct sk_buff
*skb
,
606 struct net_device
*reg_dev
= NULL
;
609 encap
= (struct iphdr
*)(skb_transport_header(skb
) + pimlen
);
611 * a. packet is really sent to a multicast group
612 * b. packet is not a NULL-REGISTER
613 * c. packet is not truncated
615 if (!ipv4_is_multicast(encap
->daddr
) ||
616 encap
->tot_len
== 0 ||
617 ntohs(encap
->tot_len
) + pimlen
> skb
->len
)
620 read_lock(&mrt_lock
);
621 if (mrt
->mroute_reg_vif_num
>= 0)
622 reg_dev
= mrt
->vif_table
[mrt
->mroute_reg_vif_num
].dev
;
623 read_unlock(&mrt_lock
);
628 skb
->mac_header
= skb
->network_header
;
629 skb_pull(skb
, (u8
*)encap
- skb
->data
);
630 skb_reset_network_header(skb
);
631 skb
->protocol
= htons(ETH_P_IP
);
632 skb
->ip_summed
= CHECKSUM_NONE
;
634 skb_tunnel_rx(skb
, reg_dev
, dev_net(reg_dev
));
638 return NET_RX_SUCCESS
;
641 static struct net_device
*ipmr_reg_vif(struct net
*net
, struct mr_table
*mrt
)
647 static int call_ipmr_vif_entry_notifiers(struct net
*net
,
648 enum fib_event_type event_type
,
649 struct vif_device
*vif
,
650 vifi_t vif_index
, u32 tb_id
)
652 return mr_call_vif_notifiers(net
, RTNL_FAMILY_IPMR
, event_type
,
653 vif
, vif_index
, tb_id
,
654 &net
->ipv4
.ipmr_seq
);
657 static int call_ipmr_mfc_entry_notifiers(struct net
*net
,
658 enum fib_event_type event_type
,
659 struct mfc_cache
*mfc
, u32 tb_id
)
661 return mr_call_mfc_notifiers(net
, RTNL_FAMILY_IPMR
, event_type
,
662 &mfc
->_c
, tb_id
, &net
->ipv4
.ipmr_seq
);
666 * vif_delete - Delete a VIF entry
667 * @notify: Set to 1, if the caller is a notifier_call
669 static int vif_delete(struct mr_table
*mrt
, int vifi
, int notify
,
670 struct list_head
*head
)
672 struct net
*net
= read_pnet(&mrt
->net
);
673 struct vif_device
*v
;
674 struct net_device
*dev
;
675 struct in_device
*in_dev
;
677 if (vifi
< 0 || vifi
>= mrt
->maxvif
)
678 return -EADDRNOTAVAIL
;
680 v
= &mrt
->vif_table
[vifi
];
682 if (VIF_EXISTS(mrt
, vifi
))
683 call_ipmr_vif_entry_notifiers(net
, FIB_EVENT_VIF_DEL
, v
, vifi
,
686 write_lock_bh(&mrt_lock
);
691 write_unlock_bh(&mrt_lock
);
692 return -EADDRNOTAVAIL
;
695 if (vifi
== mrt
->mroute_reg_vif_num
)
696 mrt
->mroute_reg_vif_num
= -1;
698 if (vifi
+ 1 == mrt
->maxvif
) {
701 for (tmp
= vifi
- 1; tmp
>= 0; tmp
--) {
702 if (VIF_EXISTS(mrt
, tmp
))
708 write_unlock_bh(&mrt_lock
);
710 dev_set_allmulti(dev
, -1);
712 in_dev
= __in_dev_get_rtnl(dev
);
714 IPV4_DEVCONF(in_dev
->cnf
, MC_FORWARDING
)--;
715 inet_netconf_notify_devconf(dev_net(dev
), RTM_NEWNETCONF
,
716 NETCONFA_MC_FORWARDING
,
717 dev
->ifindex
, &in_dev
->cnf
);
718 ip_rt_multicast_event(in_dev
);
721 if (v
->flags
& (VIFF_TUNNEL
| VIFF_REGISTER
) && !notify
)
722 unregister_netdevice_queue(dev
, head
);
728 static void ipmr_cache_free_rcu(struct rcu_head
*head
)
730 struct mr_mfc
*c
= container_of(head
, struct mr_mfc
, rcu
);
732 kmem_cache_free(mrt_cachep
, (struct mfc_cache
*)c
);
735 static void ipmr_cache_free(struct mfc_cache
*c
)
737 call_rcu(&c
->_c
.rcu
, ipmr_cache_free_rcu
);
740 /* Destroy an unresolved cache entry, killing queued skbs
741 * and reporting error to netlink readers.
743 static void ipmr_destroy_unres(struct mr_table
*mrt
, struct mfc_cache
*c
)
745 struct net
*net
= read_pnet(&mrt
->net
);
749 atomic_dec(&mrt
->cache_resolve_queue_len
);
751 while ((skb
= skb_dequeue(&c
->_c
.mfc_un
.unres
.unresolved
))) {
752 if (ip_hdr(skb
)->version
== 0) {
753 struct nlmsghdr
*nlh
= skb_pull(skb
,
754 sizeof(struct iphdr
));
755 nlh
->nlmsg_type
= NLMSG_ERROR
;
756 nlh
->nlmsg_len
= nlmsg_msg_size(sizeof(struct nlmsgerr
));
757 skb_trim(skb
, nlh
->nlmsg_len
);
759 e
->error
= -ETIMEDOUT
;
760 memset(&e
->msg
, 0, sizeof(e
->msg
));
762 rtnl_unicast(skb
, net
, NETLINK_CB(skb
).portid
);
771 /* Timer process for the unresolved queue. */
772 static void ipmr_expire_process(struct timer_list
*t
)
774 struct mr_table
*mrt
= from_timer(mrt
, t
, ipmr_expire_timer
);
775 struct mr_mfc
*c
, *next
;
776 unsigned long expires
;
779 if (!spin_trylock(&mfc_unres_lock
)) {
780 mod_timer(&mrt
->ipmr_expire_timer
, jiffies
+HZ
/10);
784 if (list_empty(&mrt
->mfc_unres_queue
))
790 list_for_each_entry_safe(c
, next
, &mrt
->mfc_unres_queue
, list
) {
791 if (time_after(c
->mfc_un
.unres
.expires
, now
)) {
792 unsigned long interval
= c
->mfc_un
.unres
.expires
- now
;
793 if (interval
< expires
)
799 mroute_netlink_event(mrt
, (struct mfc_cache
*)c
, RTM_DELROUTE
);
800 ipmr_destroy_unres(mrt
, (struct mfc_cache
*)c
);
803 if (!list_empty(&mrt
->mfc_unres_queue
))
804 mod_timer(&mrt
->ipmr_expire_timer
, jiffies
+ expires
);
807 spin_unlock(&mfc_unres_lock
);
810 /* Fill oifs list. It is called under write locked mrt_lock. */
811 static void ipmr_update_thresholds(struct mr_table
*mrt
, struct mr_mfc
*cache
,
816 cache
->mfc_un
.res
.minvif
= MAXVIFS
;
817 cache
->mfc_un
.res
.maxvif
= 0;
818 memset(cache
->mfc_un
.res
.ttls
, 255, MAXVIFS
);
820 for (vifi
= 0; vifi
< mrt
->maxvif
; vifi
++) {
821 if (VIF_EXISTS(mrt
, vifi
) &&
822 ttls
[vifi
] && ttls
[vifi
] < 255) {
823 cache
->mfc_un
.res
.ttls
[vifi
] = ttls
[vifi
];
824 if (cache
->mfc_un
.res
.minvif
> vifi
)
825 cache
->mfc_un
.res
.minvif
= vifi
;
826 if (cache
->mfc_un
.res
.maxvif
<= vifi
)
827 cache
->mfc_un
.res
.maxvif
= vifi
+ 1;
830 cache
->mfc_un
.res
.lastuse
= jiffies
;
833 static int vif_add(struct net
*net
, struct mr_table
*mrt
,
834 struct vifctl
*vifc
, int mrtsock
)
836 struct netdev_phys_item_id ppid
= { };
837 int vifi
= vifc
->vifc_vifi
;
838 struct vif_device
*v
= &mrt
->vif_table
[vifi
];
839 struct net_device
*dev
;
840 struct in_device
*in_dev
;
844 if (VIF_EXISTS(mrt
, vifi
))
847 switch (vifc
->vifc_flags
) {
849 if (!ipmr_pimsm_enabled())
851 /* Special Purpose VIF in PIM
852 * All the packets will be sent to the daemon
854 if (mrt
->mroute_reg_vif_num
>= 0)
856 dev
= ipmr_reg_vif(net
, mrt
);
859 err
= dev_set_allmulti(dev
, 1);
861 unregister_netdevice(dev
);
867 dev
= ipmr_new_tunnel(net
, vifc
);
870 err
= dev_set_allmulti(dev
, 1);
872 ipmr_del_tunnel(dev
, vifc
);
877 case VIFF_USE_IFINDEX
:
879 if (vifc
->vifc_flags
== VIFF_USE_IFINDEX
) {
880 dev
= dev_get_by_index(net
, vifc
->vifc_lcl_ifindex
);
881 if (dev
&& !__in_dev_get_rtnl(dev
)) {
883 return -EADDRNOTAVAIL
;
886 dev
= ip_dev_find(net
, vifc
->vifc_lcl_addr
.s_addr
);
889 return -EADDRNOTAVAIL
;
890 err
= dev_set_allmulti(dev
, 1);
900 in_dev
= __in_dev_get_rtnl(dev
);
903 return -EADDRNOTAVAIL
;
905 IPV4_DEVCONF(in_dev
->cnf
, MC_FORWARDING
)++;
906 inet_netconf_notify_devconf(net
, RTM_NEWNETCONF
, NETCONFA_MC_FORWARDING
,
907 dev
->ifindex
, &in_dev
->cnf
);
908 ip_rt_multicast_event(in_dev
);
910 /* Fill in the VIF structures */
911 vif_device_init(v
, dev
, vifc
->vifc_rate_limit
,
912 vifc
->vifc_threshold
,
913 vifc
->vifc_flags
| (!mrtsock
? VIFF_STATIC
: 0),
914 (VIFF_TUNNEL
| VIFF_REGISTER
));
916 err
= dev_get_port_parent_id(dev
, &ppid
, true);
918 memcpy(v
->dev_parent_id
.id
, ppid
.id
, ppid
.id_len
);
919 v
->dev_parent_id
.id_len
= ppid
.id_len
;
921 v
->dev_parent_id
.id_len
= 0;
924 v
->local
= vifc
->vifc_lcl_addr
.s_addr
;
925 v
->remote
= vifc
->vifc_rmt_addr
.s_addr
;
927 /* And finish update writing critical data */
928 write_lock_bh(&mrt_lock
);
930 if (v
->flags
& VIFF_REGISTER
)
931 mrt
->mroute_reg_vif_num
= vifi
;
932 if (vifi
+1 > mrt
->maxvif
)
933 mrt
->maxvif
= vifi
+1;
934 write_unlock_bh(&mrt_lock
);
935 call_ipmr_vif_entry_notifiers(net
, FIB_EVENT_VIF_ADD
, v
, vifi
, mrt
->id
);
939 /* called with rcu_read_lock() */
940 static struct mfc_cache
*ipmr_cache_find(struct mr_table
*mrt
,
944 struct mfc_cache_cmp_arg arg
= {
945 .mfc_mcastgrp
= mcastgrp
,
949 return mr_mfc_find(mrt
, &arg
);
952 /* Look for a (*,G) entry */
953 static struct mfc_cache
*ipmr_cache_find_any(struct mr_table
*mrt
,
954 __be32 mcastgrp
, int vifi
)
956 struct mfc_cache_cmp_arg arg
= {
957 .mfc_mcastgrp
= mcastgrp
,
958 .mfc_origin
= htonl(INADDR_ANY
)
961 if (mcastgrp
== htonl(INADDR_ANY
))
962 return mr_mfc_find_any_parent(mrt
, vifi
);
963 return mr_mfc_find_any(mrt
, vifi
, &arg
);
966 /* Look for a (S,G,iif) entry if parent != -1 */
967 static struct mfc_cache
*ipmr_cache_find_parent(struct mr_table
*mrt
,
968 __be32 origin
, __be32 mcastgrp
,
971 struct mfc_cache_cmp_arg arg
= {
972 .mfc_mcastgrp
= mcastgrp
,
973 .mfc_origin
= origin
,
976 return mr_mfc_find_parent(mrt
, &arg
, parent
);
979 /* Allocate a multicast cache entry */
980 static struct mfc_cache
*ipmr_cache_alloc(void)
982 struct mfc_cache
*c
= kmem_cache_zalloc(mrt_cachep
, GFP_KERNEL
);
985 c
->_c
.mfc_un
.res
.last_assert
= jiffies
- MFC_ASSERT_THRESH
- 1;
986 c
->_c
.mfc_un
.res
.minvif
= MAXVIFS
;
987 c
->_c
.free
= ipmr_cache_free_rcu
;
988 refcount_set(&c
->_c
.mfc_un
.res
.refcount
, 1);
993 static struct mfc_cache
*ipmr_cache_alloc_unres(void)
995 struct mfc_cache
*c
= kmem_cache_zalloc(mrt_cachep
, GFP_ATOMIC
);
998 skb_queue_head_init(&c
->_c
.mfc_un
.unres
.unresolved
);
999 c
->_c
.mfc_un
.unres
.expires
= jiffies
+ 10 * HZ
;
1004 /* A cache entry has gone into a resolved state from queued */
1005 static void ipmr_cache_resolve(struct net
*net
, struct mr_table
*mrt
,
1006 struct mfc_cache
*uc
, struct mfc_cache
*c
)
1008 struct sk_buff
*skb
;
1011 /* Play the pending entries through our router */
1012 while ((skb
= __skb_dequeue(&uc
->_c
.mfc_un
.unres
.unresolved
))) {
1013 if (ip_hdr(skb
)->version
== 0) {
1014 struct nlmsghdr
*nlh
= skb_pull(skb
,
1015 sizeof(struct iphdr
));
1017 if (mr_fill_mroute(mrt
, skb
, &c
->_c
,
1018 nlmsg_data(nlh
)) > 0) {
1019 nlh
->nlmsg_len
= skb_tail_pointer(skb
) -
1022 nlh
->nlmsg_type
= NLMSG_ERROR
;
1023 nlh
->nlmsg_len
= nlmsg_msg_size(sizeof(struct nlmsgerr
));
1024 skb_trim(skb
, nlh
->nlmsg_len
);
1025 e
= nlmsg_data(nlh
);
1026 e
->error
= -EMSGSIZE
;
1027 memset(&e
->msg
, 0, sizeof(e
->msg
));
1030 rtnl_unicast(skb
, net
, NETLINK_CB(skb
).portid
);
1032 ip_mr_forward(net
, mrt
, skb
->dev
, skb
, c
, 0);
1037 /* Bounce a cache query up to mrouted and netlink.
1039 * Called under mrt_lock.
1041 static int ipmr_cache_report(struct mr_table
*mrt
,
1042 struct sk_buff
*pkt
, vifi_t vifi
, int assert)
1044 const int ihl
= ip_hdrlen(pkt
);
1045 struct sock
*mroute_sk
;
1046 struct igmphdr
*igmp
;
1047 struct igmpmsg
*msg
;
1048 struct sk_buff
*skb
;
1051 if (assert == IGMPMSG_WHOLEPKT
|| assert == IGMPMSG_WRVIFWHOLE
)
1052 skb
= skb_realloc_headroom(pkt
, sizeof(struct iphdr
));
1054 skb
= alloc_skb(128, GFP_ATOMIC
);
1059 if (assert == IGMPMSG_WHOLEPKT
|| assert == IGMPMSG_WRVIFWHOLE
) {
1060 /* Ugly, but we have no choice with this interface.
1061 * Duplicate old header, fix ihl, length etc.
1062 * And all this only to mangle msg->im_msgtype and
1063 * to set msg->im_mbz to "mbz" :-)
1065 skb_push(skb
, sizeof(struct iphdr
));
1066 skb_reset_network_header(skb
);
1067 skb_reset_transport_header(skb
);
1068 msg
= (struct igmpmsg
*)skb_network_header(skb
);
1069 memcpy(msg
, skb_network_header(pkt
), sizeof(struct iphdr
));
1070 msg
->im_msgtype
= assert;
1072 if (assert == IGMPMSG_WRVIFWHOLE
)
1075 msg
->im_vif
= mrt
->mroute_reg_vif_num
;
1076 ip_hdr(skb
)->ihl
= sizeof(struct iphdr
) >> 2;
1077 ip_hdr(skb
)->tot_len
= htons(ntohs(ip_hdr(pkt
)->tot_len
) +
1078 sizeof(struct iphdr
));
1080 /* Copy the IP header */
1081 skb_set_network_header(skb
, skb
->len
);
1083 skb_copy_to_linear_data(skb
, pkt
->data
, ihl
);
1084 /* Flag to the kernel this is a route add */
1085 ip_hdr(skb
)->protocol
= 0;
1086 msg
= (struct igmpmsg
*)skb_network_header(skb
);
1088 skb_dst_set(skb
, dst_clone(skb_dst(pkt
)));
1089 /* Add our header */
1090 igmp
= skb_put(skb
, sizeof(struct igmphdr
));
1091 igmp
->type
= assert;
1092 msg
->im_msgtype
= assert;
1094 ip_hdr(skb
)->tot_len
= htons(skb
->len
); /* Fix the length */
1095 skb
->transport_header
= skb
->network_header
;
1099 mroute_sk
= rcu_dereference(mrt
->mroute_sk
);
1106 igmpmsg_netlink_event(mrt
, skb
);
1108 /* Deliver to mrouted */
1109 ret
= sock_queue_rcv_skb(mroute_sk
, skb
);
1112 net_warn_ratelimited("mroute: pending queue full, dropping entries\n");
1119 /* Queue a packet for resolution. It gets locked cache entry! */
1120 static int ipmr_cache_unresolved(struct mr_table
*mrt
, vifi_t vifi
,
1121 struct sk_buff
*skb
, struct net_device
*dev
)
1123 const struct iphdr
*iph
= ip_hdr(skb
);
1124 struct mfc_cache
*c
;
1128 spin_lock_bh(&mfc_unres_lock
);
1129 list_for_each_entry(c
, &mrt
->mfc_unres_queue
, _c
.list
) {
1130 if (c
->mfc_mcastgrp
== iph
->daddr
&&
1131 c
->mfc_origin
== iph
->saddr
) {
1138 /* Create a new entry if allowable */
1139 c
= ipmr_cache_alloc_unres();
1141 spin_unlock_bh(&mfc_unres_lock
);
1147 /* Fill in the new cache entry */
1148 c
->_c
.mfc_parent
= -1;
1149 c
->mfc_origin
= iph
->saddr
;
1150 c
->mfc_mcastgrp
= iph
->daddr
;
1152 /* Reflect first query at mrouted. */
1153 err
= ipmr_cache_report(mrt
, skb
, vifi
, IGMPMSG_NOCACHE
);
1156 /* If the report failed throw the cache entry
1159 spin_unlock_bh(&mfc_unres_lock
);
1166 atomic_inc(&mrt
->cache_resolve_queue_len
);
1167 list_add(&c
->_c
.list
, &mrt
->mfc_unres_queue
);
1168 mroute_netlink_event(mrt
, c
, RTM_NEWROUTE
);
1170 if (atomic_read(&mrt
->cache_resolve_queue_len
) == 1)
1171 mod_timer(&mrt
->ipmr_expire_timer
,
1172 c
->_c
.mfc_un
.unres
.expires
);
1175 /* See if we can append the packet */
1176 if (c
->_c
.mfc_un
.unres
.unresolved
.qlen
> 3) {
1182 skb
->skb_iif
= dev
->ifindex
;
1184 skb_queue_tail(&c
->_c
.mfc_un
.unres
.unresolved
, skb
);
1188 spin_unlock_bh(&mfc_unres_lock
);
1192 /* MFC cache manipulation by user space mroute daemon */
1194 static int ipmr_mfc_delete(struct mr_table
*mrt
, struct mfcctl
*mfc
, int parent
)
1196 struct net
*net
= read_pnet(&mrt
->net
);
1197 struct mfc_cache
*c
;
1199 /* The entries are added/deleted only under RTNL */
1201 c
= ipmr_cache_find_parent(mrt
, mfc
->mfcc_origin
.s_addr
,
1202 mfc
->mfcc_mcastgrp
.s_addr
, parent
);
1206 rhltable_remove(&mrt
->mfc_hash
, &c
->_c
.mnode
, ipmr_rht_params
);
1207 list_del_rcu(&c
->_c
.list
);
1208 call_ipmr_mfc_entry_notifiers(net
, FIB_EVENT_ENTRY_DEL
, c
, mrt
->id
);
1209 mroute_netlink_event(mrt
, c
, RTM_DELROUTE
);
1210 mr_cache_put(&c
->_c
);
1215 static int ipmr_mfc_add(struct net
*net
, struct mr_table
*mrt
,
1216 struct mfcctl
*mfc
, int mrtsock
, int parent
)
1218 struct mfc_cache
*uc
, *c
;
1223 if (mfc
->mfcc_parent
>= MAXVIFS
)
1226 /* The entries are added/deleted only under RTNL */
1228 c
= ipmr_cache_find_parent(mrt
, mfc
->mfcc_origin
.s_addr
,
1229 mfc
->mfcc_mcastgrp
.s_addr
, parent
);
1232 write_lock_bh(&mrt_lock
);
1233 c
->_c
.mfc_parent
= mfc
->mfcc_parent
;
1234 ipmr_update_thresholds(mrt
, &c
->_c
, mfc
->mfcc_ttls
);
1236 c
->_c
.mfc_flags
|= MFC_STATIC
;
1237 write_unlock_bh(&mrt_lock
);
1238 call_ipmr_mfc_entry_notifiers(net
, FIB_EVENT_ENTRY_REPLACE
, c
,
1240 mroute_netlink_event(mrt
, c
, RTM_NEWROUTE
);
1244 if (mfc
->mfcc_mcastgrp
.s_addr
!= htonl(INADDR_ANY
) &&
1245 !ipv4_is_multicast(mfc
->mfcc_mcastgrp
.s_addr
))
1248 c
= ipmr_cache_alloc();
1252 c
->mfc_origin
= mfc
->mfcc_origin
.s_addr
;
1253 c
->mfc_mcastgrp
= mfc
->mfcc_mcastgrp
.s_addr
;
1254 c
->_c
.mfc_parent
= mfc
->mfcc_parent
;
1255 ipmr_update_thresholds(mrt
, &c
->_c
, mfc
->mfcc_ttls
);
1257 c
->_c
.mfc_flags
|= MFC_STATIC
;
1259 ret
= rhltable_insert_key(&mrt
->mfc_hash
, &c
->cmparg
, &c
->_c
.mnode
,
1262 pr_err("ipmr: rhtable insert error %d\n", ret
);
1266 list_add_tail_rcu(&c
->_c
.list
, &mrt
->mfc_cache_list
);
1267 /* Check to see if we resolved a queued list. If so we
1268 * need to send on the frames and tidy up.
1271 spin_lock_bh(&mfc_unres_lock
);
1272 list_for_each_entry(_uc
, &mrt
->mfc_unres_queue
, list
) {
1273 uc
= (struct mfc_cache
*)_uc
;
1274 if (uc
->mfc_origin
== c
->mfc_origin
&&
1275 uc
->mfc_mcastgrp
== c
->mfc_mcastgrp
) {
1276 list_del(&_uc
->list
);
1277 atomic_dec(&mrt
->cache_resolve_queue_len
);
1282 if (list_empty(&mrt
->mfc_unres_queue
))
1283 del_timer(&mrt
->ipmr_expire_timer
);
1284 spin_unlock_bh(&mfc_unres_lock
);
1287 ipmr_cache_resolve(net
, mrt
, uc
, c
);
1288 ipmr_cache_free(uc
);
1290 call_ipmr_mfc_entry_notifiers(net
, FIB_EVENT_ENTRY_ADD
, c
, mrt
->id
);
1291 mroute_netlink_event(mrt
, c
, RTM_NEWROUTE
);
1295 /* Close the multicast socket, and clear the vif tables etc */
1296 static void mroute_clean_tables(struct mr_table
*mrt
, int flags
)
1298 struct net
*net
= read_pnet(&mrt
->net
);
1299 struct mr_mfc
*c
, *tmp
;
1300 struct mfc_cache
*cache
;
1304 /* Shut down all active vif entries */
1305 if (flags
& (MRT_FLUSH_VIFS
| MRT_FLUSH_VIFS_STATIC
)) {
1306 for (i
= 0; i
< mrt
->maxvif
; i
++) {
1307 if (((mrt
->vif_table
[i
].flags
& VIFF_STATIC
) &&
1308 !(flags
& MRT_FLUSH_VIFS_STATIC
)) ||
1309 (!(mrt
->vif_table
[i
].flags
& VIFF_STATIC
) && !(flags
& MRT_FLUSH_VIFS
)))
1311 vif_delete(mrt
, i
, 0, &list
);
1313 unregister_netdevice_many(&list
);
1316 /* Wipe the cache */
1317 if (flags
& (MRT_FLUSH_MFC
| MRT_FLUSH_MFC_STATIC
)) {
1318 list_for_each_entry_safe(c
, tmp
, &mrt
->mfc_cache_list
, list
) {
1319 if (((c
->mfc_flags
& MFC_STATIC
) && !(flags
& MRT_FLUSH_MFC_STATIC
)) ||
1320 (!(c
->mfc_flags
& MFC_STATIC
) && !(flags
& MRT_FLUSH_MFC
)))
1322 rhltable_remove(&mrt
->mfc_hash
, &c
->mnode
, ipmr_rht_params
);
1323 list_del_rcu(&c
->list
);
1324 cache
= (struct mfc_cache
*)c
;
1325 call_ipmr_mfc_entry_notifiers(net
, FIB_EVENT_ENTRY_DEL
, cache
,
1327 mroute_netlink_event(mrt
, cache
, RTM_DELROUTE
);
1332 if (flags
& MRT_FLUSH_MFC
) {
1333 if (atomic_read(&mrt
->cache_resolve_queue_len
) != 0) {
1334 spin_lock_bh(&mfc_unres_lock
);
1335 list_for_each_entry_safe(c
, tmp
, &mrt
->mfc_unres_queue
, list
) {
1337 cache
= (struct mfc_cache
*)c
;
1338 mroute_netlink_event(mrt
, cache
, RTM_DELROUTE
);
1339 ipmr_destroy_unres(mrt
, cache
);
1341 spin_unlock_bh(&mfc_unres_lock
);
1346 /* called from ip_ra_control(), before an RCU grace period,
1347 * we dont need to call synchronize_rcu() here
1349 static void mrtsock_destruct(struct sock
*sk
)
1351 struct net
*net
= sock_net(sk
);
1352 struct mr_table
*mrt
;
1355 ipmr_for_each_table(mrt
, net
) {
1356 if (sk
== rtnl_dereference(mrt
->mroute_sk
)) {
1357 IPV4_DEVCONF_ALL(net
, MC_FORWARDING
)--;
1358 inet_netconf_notify_devconf(net
, RTM_NEWNETCONF
,
1359 NETCONFA_MC_FORWARDING
,
1360 NETCONFA_IFINDEX_ALL
,
1361 net
->ipv4
.devconf_all
);
1362 RCU_INIT_POINTER(mrt
->mroute_sk
, NULL
);
1363 mroute_clean_tables(mrt
, MRT_FLUSH_VIFS
| MRT_FLUSH_MFC
);
1369 /* Socket options and virtual interface manipulation. The whole
1370 * virtual interface system is a complete heap, but unfortunately
1371 * that's how BSD mrouted happens to think. Maybe one day with a proper
1372 * MOSPF/PIM router set up we can clean this up.
1375 int ip_mroute_setsockopt(struct sock
*sk
, int optname
, char __user
*optval
,
1376 unsigned int optlen
)
1378 struct net
*net
= sock_net(sk
);
1379 int val
, ret
= 0, parent
= 0;
1380 struct mr_table
*mrt
;
1386 /* There's one exception to the lock - MRT_DONE which needs to unlock */
1388 if (sk
->sk_type
!= SOCK_RAW
||
1389 inet_sk(sk
)->inet_num
!= IPPROTO_IGMP
) {
1394 mrt
= ipmr_get_table(net
, raw_sk(sk
)->ipmr_table
? : RT_TABLE_DEFAULT
);
1399 if (optname
!= MRT_INIT
) {
1400 if (sk
!= rcu_access_pointer(mrt
->mroute_sk
) &&
1401 !ns_capable(net
->user_ns
, CAP_NET_ADMIN
)) {
1409 if (optlen
!= sizeof(int)) {
1413 if (rtnl_dereference(mrt
->mroute_sk
)) {
1418 ret
= ip_ra_control(sk
, 1, mrtsock_destruct
);
1420 rcu_assign_pointer(mrt
->mroute_sk
, sk
);
1421 IPV4_DEVCONF_ALL(net
, MC_FORWARDING
)++;
1422 inet_netconf_notify_devconf(net
, RTM_NEWNETCONF
,
1423 NETCONFA_MC_FORWARDING
,
1424 NETCONFA_IFINDEX_ALL
,
1425 net
->ipv4
.devconf_all
);
1429 if (sk
!= rcu_access_pointer(mrt
->mroute_sk
)) {
1432 /* We need to unlock here because mrtsock_destruct takes
1433 * care of rtnl itself and we can't change that due to
1434 * the IP_ROUTER_ALERT setsockopt which runs without it.
1437 ret
= ip_ra_control(sk
, 0, NULL
);
1443 if (optlen
!= sizeof(vif
)) {
1447 if (copy_from_user(&vif
, optval
, sizeof(vif
))) {
1451 if (vif
.vifc_vifi
>= MAXVIFS
) {
1455 if (optname
== MRT_ADD_VIF
) {
1456 ret
= vif_add(net
, mrt
, &vif
,
1457 sk
== rtnl_dereference(mrt
->mroute_sk
));
1459 ret
= vif_delete(mrt
, vif
.vifc_vifi
, 0, NULL
);
1462 /* Manipulate the forwarding caches. These live
1463 * in a sort of kernel/user symbiosis.
1469 case MRT_ADD_MFC_PROXY
:
1470 case MRT_DEL_MFC_PROXY
:
1471 if (optlen
!= sizeof(mfc
)) {
1475 if (copy_from_user(&mfc
, optval
, sizeof(mfc
))) {
1480 parent
= mfc
.mfcc_parent
;
1481 if (optname
== MRT_DEL_MFC
|| optname
== MRT_DEL_MFC_PROXY
)
1482 ret
= ipmr_mfc_delete(mrt
, &mfc
, parent
);
1484 ret
= ipmr_mfc_add(net
, mrt
, &mfc
,
1485 sk
== rtnl_dereference(mrt
->mroute_sk
),
1489 if (optlen
!= sizeof(val
)) {
1493 if (get_user(val
, (int __user
*)optval
)) {
1497 mroute_clean_tables(mrt
, val
);
1499 /* Control PIM assert. */
1501 if (optlen
!= sizeof(val
)) {
1505 if (get_user(val
, (int __user
*)optval
)) {
1509 mrt
->mroute_do_assert
= val
;
1512 if (!ipmr_pimsm_enabled()) {
1516 if (optlen
!= sizeof(val
)) {
1520 if (get_user(val
, (int __user
*)optval
)) {
1525 do_wrvifwhole
= (val
== IGMPMSG_WRVIFWHOLE
);
1527 if (val
!= mrt
->mroute_do_pim
) {
1528 mrt
->mroute_do_pim
= val
;
1529 mrt
->mroute_do_assert
= val
;
1530 mrt
->mroute_do_wrvifwhole
= do_wrvifwhole
;
1534 if (!IS_BUILTIN(CONFIG_IP_MROUTE_MULTIPLE_TABLES
)) {
1538 if (optlen
!= sizeof(uval
)) {
1542 if (get_user(uval
, (u32 __user
*)optval
)) {
1547 if (sk
== rtnl_dereference(mrt
->mroute_sk
)) {
1550 mrt
= ipmr_new_table(net
, uval
);
1554 raw_sk(sk
)->ipmr_table
= uval
;
1557 /* Spurious command, or MRT_VERSION which you cannot set. */
1567 /* Getsock opt support for the multicast routing system. */
1568 int ip_mroute_getsockopt(struct sock
*sk
, int optname
, char __user
*optval
, int __user
*optlen
)
1572 struct net
*net
= sock_net(sk
);
1573 struct mr_table
*mrt
;
1575 if (sk
->sk_type
!= SOCK_RAW
||
1576 inet_sk(sk
)->inet_num
!= IPPROTO_IGMP
)
1579 mrt
= ipmr_get_table(net
, raw_sk(sk
)->ipmr_table
? : RT_TABLE_DEFAULT
);
1588 if (!ipmr_pimsm_enabled())
1589 return -ENOPROTOOPT
;
1590 val
= mrt
->mroute_do_pim
;
1593 val
= mrt
->mroute_do_assert
;
1596 return -ENOPROTOOPT
;
1599 if (get_user(olr
, optlen
))
1601 olr
= min_t(unsigned int, olr
, sizeof(int));
1604 if (put_user(olr
, optlen
))
1606 if (copy_to_user(optval
, &val
, olr
))
1611 /* The IP multicast ioctl support routines. */
1612 int ipmr_ioctl(struct sock
*sk
, int cmd
, void __user
*arg
)
1614 struct sioc_sg_req sr
;
1615 struct sioc_vif_req vr
;
1616 struct vif_device
*vif
;
1617 struct mfc_cache
*c
;
1618 struct net
*net
= sock_net(sk
);
1619 struct mr_table
*mrt
;
1621 mrt
= ipmr_get_table(net
, raw_sk(sk
)->ipmr_table
? : RT_TABLE_DEFAULT
);
1627 if (copy_from_user(&vr
, arg
, sizeof(vr
)))
1629 if (vr
.vifi
>= mrt
->maxvif
)
1631 vr
.vifi
= array_index_nospec(vr
.vifi
, mrt
->maxvif
);
1632 read_lock(&mrt_lock
);
1633 vif
= &mrt
->vif_table
[vr
.vifi
];
1634 if (VIF_EXISTS(mrt
, vr
.vifi
)) {
1635 vr
.icount
= vif
->pkt_in
;
1636 vr
.ocount
= vif
->pkt_out
;
1637 vr
.ibytes
= vif
->bytes_in
;
1638 vr
.obytes
= vif
->bytes_out
;
1639 read_unlock(&mrt_lock
);
1641 if (copy_to_user(arg
, &vr
, sizeof(vr
)))
1645 read_unlock(&mrt_lock
);
1646 return -EADDRNOTAVAIL
;
1648 if (copy_from_user(&sr
, arg
, sizeof(sr
)))
1652 c
= ipmr_cache_find(mrt
, sr
.src
.s_addr
, sr
.grp
.s_addr
);
1654 sr
.pktcnt
= c
->_c
.mfc_un
.res
.pkt
;
1655 sr
.bytecnt
= c
->_c
.mfc_un
.res
.bytes
;
1656 sr
.wrong_if
= c
->_c
.mfc_un
.res
.wrong_if
;
1659 if (copy_to_user(arg
, &sr
, sizeof(sr
)))
1664 return -EADDRNOTAVAIL
;
1666 return -ENOIOCTLCMD
;
1670 #ifdef CONFIG_COMPAT
1671 struct compat_sioc_sg_req
{
1674 compat_ulong_t pktcnt
;
1675 compat_ulong_t bytecnt
;
1676 compat_ulong_t wrong_if
;
1679 struct compat_sioc_vif_req
{
1680 vifi_t vifi
; /* Which iface */
1681 compat_ulong_t icount
;
1682 compat_ulong_t ocount
;
1683 compat_ulong_t ibytes
;
1684 compat_ulong_t obytes
;
1687 int ipmr_compat_ioctl(struct sock
*sk
, unsigned int cmd
, void __user
*arg
)
1689 struct compat_sioc_sg_req sr
;
1690 struct compat_sioc_vif_req vr
;
1691 struct vif_device
*vif
;
1692 struct mfc_cache
*c
;
1693 struct net
*net
= sock_net(sk
);
1694 struct mr_table
*mrt
;
1696 mrt
= ipmr_get_table(net
, raw_sk(sk
)->ipmr_table
? : RT_TABLE_DEFAULT
);
1702 if (copy_from_user(&vr
, arg
, sizeof(vr
)))
1704 if (vr
.vifi
>= mrt
->maxvif
)
1706 vr
.vifi
= array_index_nospec(vr
.vifi
, mrt
->maxvif
);
1707 read_lock(&mrt_lock
);
1708 vif
= &mrt
->vif_table
[vr
.vifi
];
1709 if (VIF_EXISTS(mrt
, vr
.vifi
)) {
1710 vr
.icount
= vif
->pkt_in
;
1711 vr
.ocount
= vif
->pkt_out
;
1712 vr
.ibytes
= vif
->bytes_in
;
1713 vr
.obytes
= vif
->bytes_out
;
1714 read_unlock(&mrt_lock
);
1716 if (copy_to_user(arg
, &vr
, sizeof(vr
)))
1720 read_unlock(&mrt_lock
);
1721 return -EADDRNOTAVAIL
;
1723 if (copy_from_user(&sr
, arg
, sizeof(sr
)))
1727 c
= ipmr_cache_find(mrt
, sr
.src
.s_addr
, sr
.grp
.s_addr
);
1729 sr
.pktcnt
= c
->_c
.mfc_un
.res
.pkt
;
1730 sr
.bytecnt
= c
->_c
.mfc_un
.res
.bytes
;
1731 sr
.wrong_if
= c
->_c
.mfc_un
.res
.wrong_if
;
1734 if (copy_to_user(arg
, &sr
, sizeof(sr
)))
1739 return -EADDRNOTAVAIL
;
1741 return -ENOIOCTLCMD
;
1746 static int ipmr_device_event(struct notifier_block
*this, unsigned long event
, void *ptr
)
1748 struct net_device
*dev
= netdev_notifier_info_to_dev(ptr
);
1749 struct net
*net
= dev_net(dev
);
1750 struct mr_table
*mrt
;
1751 struct vif_device
*v
;
1754 if (event
!= NETDEV_UNREGISTER
)
1757 ipmr_for_each_table(mrt
, net
) {
1758 v
= &mrt
->vif_table
[0];
1759 for (ct
= 0; ct
< mrt
->maxvif
; ct
++, v
++) {
1761 vif_delete(mrt
, ct
, 1, NULL
);
1767 static struct notifier_block ip_mr_notifier
= {
1768 .notifier_call
= ipmr_device_event
,
1771 /* Encapsulate a packet by attaching a valid IPIP header to it.
1772 * This avoids tunnel drivers and other mess and gives us the speed so
1773 * important for multicast video.
1775 static void ip_encap(struct net
*net
, struct sk_buff
*skb
,
1776 __be32 saddr
, __be32 daddr
)
1779 const struct iphdr
*old_iph
= ip_hdr(skb
);
1781 skb_push(skb
, sizeof(struct iphdr
));
1782 skb
->transport_header
= skb
->network_header
;
1783 skb_reset_network_header(skb
);
1787 iph
->tos
= old_iph
->tos
;
1788 iph
->ttl
= old_iph
->ttl
;
1792 iph
->protocol
= IPPROTO_IPIP
;
1794 iph
->tot_len
= htons(skb
->len
);
1795 ip_select_ident(net
, skb
, NULL
);
1798 memset(&(IPCB(skb
)->opt
), 0, sizeof(IPCB(skb
)->opt
));
1802 static inline int ipmr_forward_finish(struct net
*net
, struct sock
*sk
,
1803 struct sk_buff
*skb
)
1805 struct ip_options
*opt
= &(IPCB(skb
)->opt
);
1807 IP_INC_STATS(net
, IPSTATS_MIB_OUTFORWDATAGRAMS
);
1808 IP_ADD_STATS(net
, IPSTATS_MIB_OUTOCTETS
, skb
->len
);
1810 if (unlikely(opt
->optlen
))
1811 ip_forward_options(skb
);
1813 return dst_output(net
, sk
, skb
);
1816 #ifdef CONFIG_NET_SWITCHDEV
1817 static bool ipmr_forward_offloaded(struct sk_buff
*skb
, struct mr_table
*mrt
,
1818 int in_vifi
, int out_vifi
)
1820 struct vif_device
*out_vif
= &mrt
->vif_table
[out_vifi
];
1821 struct vif_device
*in_vif
= &mrt
->vif_table
[in_vifi
];
1823 if (!skb
->offload_l3_fwd_mark
)
1825 if (!out_vif
->dev_parent_id
.id_len
|| !in_vif
->dev_parent_id
.id_len
)
1827 return netdev_phys_item_id_same(&out_vif
->dev_parent_id
,
1828 &in_vif
->dev_parent_id
);
1831 static bool ipmr_forward_offloaded(struct sk_buff
*skb
, struct mr_table
*mrt
,
1832 int in_vifi
, int out_vifi
)
1838 /* Processing handlers for ipmr_forward */
1840 static void ipmr_queue_xmit(struct net
*net
, struct mr_table
*mrt
,
1841 int in_vifi
, struct sk_buff
*skb
, int vifi
)
1843 const struct iphdr
*iph
= ip_hdr(skb
);
1844 struct vif_device
*vif
= &mrt
->vif_table
[vifi
];
1845 struct net_device
*dev
;
1853 if (vif
->flags
& VIFF_REGISTER
) {
1855 vif
->bytes_out
+= skb
->len
;
1856 vif
->dev
->stats
.tx_bytes
+= skb
->len
;
1857 vif
->dev
->stats
.tx_packets
++;
1858 ipmr_cache_report(mrt
, skb
, vifi
, IGMPMSG_WHOLEPKT
);
1862 if (ipmr_forward_offloaded(skb
, mrt
, in_vifi
, vifi
))
1865 if (vif
->flags
& VIFF_TUNNEL
) {
1866 rt
= ip_route_output_ports(net
, &fl4
, NULL
,
1867 vif
->remote
, vif
->local
,
1870 RT_TOS(iph
->tos
), vif
->link
);
1873 encap
= sizeof(struct iphdr
);
1875 rt
= ip_route_output_ports(net
, &fl4
, NULL
, iph
->daddr
, 0,
1878 RT_TOS(iph
->tos
), vif
->link
);
1885 if (skb
->len
+encap
> dst_mtu(&rt
->dst
) && (ntohs(iph
->frag_off
) & IP_DF
)) {
1886 /* Do not fragment multicasts. Alas, IPv4 does not
1887 * allow to send ICMP, so that packets will disappear
1890 IP_INC_STATS(net
, IPSTATS_MIB_FRAGFAILS
);
1895 encap
+= LL_RESERVED_SPACE(dev
) + rt
->dst
.header_len
;
1897 if (skb_cow(skb
, encap
)) {
1903 vif
->bytes_out
+= skb
->len
;
1906 skb_dst_set(skb
, &rt
->dst
);
1907 ip_decrease_ttl(ip_hdr(skb
));
1909 /* FIXME: forward and output firewalls used to be called here.
1910 * What do we do with netfilter? -- RR
1912 if (vif
->flags
& VIFF_TUNNEL
) {
1913 ip_encap(net
, skb
, vif
->local
, vif
->remote
);
1914 /* FIXME: extra output firewall step used to be here. --RR */
1915 vif
->dev
->stats
.tx_packets
++;
1916 vif
->dev
->stats
.tx_bytes
+= skb
->len
;
1919 IPCB(skb
)->flags
|= IPSKB_FORWARDED
;
1921 /* RFC1584 teaches, that DVMRP/PIM router must deliver packets locally
1922 * not only before forwarding, but after forwarding on all output
1923 * interfaces. It is clear, if mrouter runs a multicasting
1924 * program, it should receive packets not depending to what interface
1925 * program is joined.
1926 * If we will not make it, the program will have to join on all
1927 * interfaces. On the other hand, multihoming host (or router, but
1928 * not mrouter) cannot join to more than one interface - it will
1929 * result in receiving multiple packets.
1931 NF_HOOK(NFPROTO_IPV4
, NF_INET_FORWARD
,
1932 net
, NULL
, skb
, skb
->dev
, dev
,
1933 ipmr_forward_finish
);
1940 static int ipmr_find_vif(struct mr_table
*mrt
, struct net_device
*dev
)
1944 for (ct
= mrt
->maxvif
-1; ct
>= 0; ct
--) {
1945 if (mrt
->vif_table
[ct
].dev
== dev
)
1951 /* "local" means that we should preserve one skb (for local delivery) */
1952 static void ip_mr_forward(struct net
*net
, struct mr_table
*mrt
,
1953 struct net_device
*dev
, struct sk_buff
*skb
,
1954 struct mfc_cache
*c
, int local
)
1956 int true_vifi
= ipmr_find_vif(mrt
, dev
);
1960 vif
= c
->_c
.mfc_parent
;
1961 c
->_c
.mfc_un
.res
.pkt
++;
1962 c
->_c
.mfc_un
.res
.bytes
+= skb
->len
;
1963 c
->_c
.mfc_un
.res
.lastuse
= jiffies
;
1965 if (c
->mfc_origin
== htonl(INADDR_ANY
) && true_vifi
>= 0) {
1966 struct mfc_cache
*cache_proxy
;
1968 /* For an (*,G) entry, we only check that the incomming
1969 * interface is part of the static tree.
1971 cache_proxy
= mr_mfc_find_any_parent(mrt
, vif
);
1973 cache_proxy
->_c
.mfc_un
.res
.ttls
[true_vifi
] < 255)
1977 /* Wrong interface: drop packet and (maybe) send PIM assert. */
1978 if (mrt
->vif_table
[vif
].dev
!= dev
) {
1979 if (rt_is_output_route(skb_rtable(skb
))) {
1980 /* It is our own packet, looped back.
1981 * Very complicated situation...
1983 * The best workaround until routing daemons will be
1984 * fixed is not to redistribute packet, if it was
1985 * send through wrong interface. It means, that
1986 * multicast applications WILL NOT work for
1987 * (S,G), which have default multicast route pointing
1988 * to wrong oif. In any case, it is not a good
1989 * idea to use multicasting applications on router.
1994 c
->_c
.mfc_un
.res
.wrong_if
++;
1996 if (true_vifi
>= 0 && mrt
->mroute_do_assert
&&
1997 /* pimsm uses asserts, when switching from RPT to SPT,
1998 * so that we cannot check that packet arrived on an oif.
1999 * It is bad, but otherwise we would need to move pretty
2000 * large chunk of pimd to kernel. Ough... --ANK
2002 (mrt
->mroute_do_pim
||
2003 c
->_c
.mfc_un
.res
.ttls
[true_vifi
] < 255) &&
2005 c
->_c
.mfc_un
.res
.last_assert
+
2006 MFC_ASSERT_THRESH
)) {
2007 c
->_c
.mfc_un
.res
.last_assert
= jiffies
;
2008 ipmr_cache_report(mrt
, skb
, true_vifi
, IGMPMSG_WRONGVIF
);
2009 if (mrt
->mroute_do_wrvifwhole
)
2010 ipmr_cache_report(mrt
, skb
, true_vifi
,
2011 IGMPMSG_WRVIFWHOLE
);
2017 mrt
->vif_table
[vif
].pkt_in
++;
2018 mrt
->vif_table
[vif
].bytes_in
+= skb
->len
;
2020 /* Forward the frame */
2021 if (c
->mfc_origin
== htonl(INADDR_ANY
) &&
2022 c
->mfc_mcastgrp
== htonl(INADDR_ANY
)) {
2023 if (true_vifi
>= 0 &&
2024 true_vifi
!= c
->_c
.mfc_parent
&&
2026 c
->_c
.mfc_un
.res
.ttls
[c
->_c
.mfc_parent
]) {
2027 /* It's an (*,*) entry and the packet is not coming from
2028 * the upstream: forward the packet to the upstream
2031 psend
= c
->_c
.mfc_parent
;
2036 for (ct
= c
->_c
.mfc_un
.res
.maxvif
- 1;
2037 ct
>= c
->_c
.mfc_un
.res
.minvif
; ct
--) {
2038 /* For (*,G) entry, don't forward to the incoming interface */
2039 if ((c
->mfc_origin
!= htonl(INADDR_ANY
) ||
2041 ip_hdr(skb
)->ttl
> c
->_c
.mfc_un
.res
.ttls
[ct
]) {
2043 struct sk_buff
*skb2
= skb_clone(skb
, GFP_ATOMIC
);
2046 ipmr_queue_xmit(net
, mrt
, true_vifi
,
2055 struct sk_buff
*skb2
= skb_clone(skb
, GFP_ATOMIC
);
2058 ipmr_queue_xmit(net
, mrt
, true_vifi
, skb2
,
2061 ipmr_queue_xmit(net
, mrt
, true_vifi
, skb
, psend
);
2071 static struct mr_table
*ipmr_rt_fib_lookup(struct net
*net
, struct sk_buff
*skb
)
2073 struct rtable
*rt
= skb_rtable(skb
);
2074 struct iphdr
*iph
= ip_hdr(skb
);
2075 struct flowi4 fl4
= {
2076 .daddr
= iph
->daddr
,
2077 .saddr
= iph
->saddr
,
2078 .flowi4_tos
= RT_TOS(iph
->tos
),
2079 .flowi4_oif
= (rt_is_output_route(rt
) ?
2080 skb
->dev
->ifindex
: 0),
2081 .flowi4_iif
= (rt_is_output_route(rt
) ?
2084 .flowi4_mark
= skb
->mark
,
2086 struct mr_table
*mrt
;
2089 err
= ipmr_fib_lookup(net
, &fl4
, &mrt
);
2091 return ERR_PTR(err
);
2095 /* Multicast packets for forwarding arrive here
2096 * Called with rcu_read_lock();
2098 int ip_mr_input(struct sk_buff
*skb
)
2100 struct mfc_cache
*cache
;
2101 struct net
*net
= dev_net(skb
->dev
);
2102 int local
= skb_rtable(skb
)->rt_flags
& RTCF_LOCAL
;
2103 struct mr_table
*mrt
;
2104 struct net_device
*dev
;
2106 /* skb->dev passed in is the loX master dev for vrfs.
2107 * As there are no vifs associated with loopback devices,
2108 * get the proper interface that does have a vif associated with it.
2111 if (netif_is_l3_master(skb
->dev
)) {
2112 dev
= dev_get_by_index_rcu(net
, IPCB(skb
)->iif
);
2119 /* Packet is looped back after forward, it should not be
2120 * forwarded second time, but still can be delivered locally.
2122 if (IPCB(skb
)->flags
& IPSKB_FORWARDED
)
2125 mrt
= ipmr_rt_fib_lookup(net
, skb
);
2128 return PTR_ERR(mrt
);
2131 if (IPCB(skb
)->opt
.router_alert
) {
2132 if (ip_call_ra_chain(skb
))
2134 } else if (ip_hdr(skb
)->protocol
== IPPROTO_IGMP
) {
2135 /* IGMPv1 (and broken IGMPv2 implementations sort of
2136 * Cisco IOS <= 11.2(8)) do not put router alert
2137 * option to IGMP packets destined to routable
2138 * groups. It is very bad, because it means
2139 * that we can forward NO IGMP messages.
2141 struct sock
*mroute_sk
;
2143 mroute_sk
= rcu_dereference(mrt
->mroute_sk
);
2146 raw_rcv(mroute_sk
, skb
);
2152 /* already under rcu_read_lock() */
2153 cache
= ipmr_cache_find(mrt
, ip_hdr(skb
)->saddr
, ip_hdr(skb
)->daddr
);
2155 int vif
= ipmr_find_vif(mrt
, dev
);
2158 cache
= ipmr_cache_find_any(mrt
, ip_hdr(skb
)->daddr
,
2162 /* No usable cache entry */
2167 struct sk_buff
*skb2
= skb_clone(skb
, GFP_ATOMIC
);
2168 ip_local_deliver(skb
);
2174 read_lock(&mrt_lock
);
2175 vif
= ipmr_find_vif(mrt
, dev
);
2177 int err2
= ipmr_cache_unresolved(mrt
, vif
, skb
, dev
);
2178 read_unlock(&mrt_lock
);
2182 read_unlock(&mrt_lock
);
2187 read_lock(&mrt_lock
);
2188 ip_mr_forward(net
, mrt
, dev
, skb
, cache
, local
);
2189 read_unlock(&mrt_lock
);
2192 return ip_local_deliver(skb
);
2198 return ip_local_deliver(skb
);
2203 #ifdef CONFIG_IP_PIMSM_V1
2204 /* Handle IGMP messages of PIMv1 */
2205 int pim_rcv_v1(struct sk_buff
*skb
)
2207 struct igmphdr
*pim
;
2208 struct net
*net
= dev_net(skb
->dev
);
2209 struct mr_table
*mrt
;
2211 if (!pskb_may_pull(skb
, sizeof(*pim
) + sizeof(struct iphdr
)))
2214 pim
= igmp_hdr(skb
);
2216 mrt
= ipmr_rt_fib_lookup(net
, skb
);
2219 if (!mrt
->mroute_do_pim
||
2220 pim
->group
!= PIM_V1_VERSION
|| pim
->code
!= PIM_V1_REGISTER
)
2223 if (__pim_rcv(mrt
, skb
, sizeof(*pim
))) {
2231 #ifdef CONFIG_IP_PIMSM_V2
2232 static int pim_rcv(struct sk_buff
*skb
)
2234 struct pimreghdr
*pim
;
2235 struct net
*net
= dev_net(skb
->dev
);
2236 struct mr_table
*mrt
;
2238 if (!pskb_may_pull(skb
, sizeof(*pim
) + sizeof(struct iphdr
)))
2241 pim
= (struct pimreghdr
*)skb_transport_header(skb
);
2242 if (pim
->type
!= ((PIM_VERSION
<< 4) | (PIM_TYPE_REGISTER
)) ||
2243 (pim
->flags
& PIM_NULL_REGISTER
) ||
2244 (ip_compute_csum((void *)pim
, sizeof(*pim
)) != 0 &&
2245 csum_fold(skb_checksum(skb
, 0, skb
->len
, 0))))
2248 mrt
= ipmr_rt_fib_lookup(net
, skb
);
2251 if (__pim_rcv(mrt
, skb
, sizeof(*pim
))) {
2259 int ipmr_get_route(struct net
*net
, struct sk_buff
*skb
,
2260 __be32 saddr
, __be32 daddr
,
2261 struct rtmsg
*rtm
, u32 portid
)
2263 struct mfc_cache
*cache
;
2264 struct mr_table
*mrt
;
2267 mrt
= ipmr_get_table(net
, RT_TABLE_DEFAULT
);
2272 cache
= ipmr_cache_find(mrt
, saddr
, daddr
);
2273 if (!cache
&& skb
->dev
) {
2274 int vif
= ipmr_find_vif(mrt
, skb
->dev
);
2277 cache
= ipmr_cache_find_any(mrt
, daddr
, vif
);
2280 struct sk_buff
*skb2
;
2282 struct net_device
*dev
;
2286 read_lock(&mrt_lock
);
2288 vif
= ipmr_find_vif(mrt
, dev
);
2290 read_unlock(&mrt_lock
);
2295 skb2
= skb_realloc_headroom(skb
, sizeof(struct iphdr
));
2297 read_unlock(&mrt_lock
);
2302 NETLINK_CB(skb2
).portid
= portid
;
2303 skb_push(skb2
, sizeof(struct iphdr
));
2304 skb_reset_network_header(skb2
);
2306 iph
->ihl
= sizeof(struct iphdr
) >> 2;
2310 err
= ipmr_cache_unresolved(mrt
, vif
, skb2
, dev
);
2311 read_unlock(&mrt_lock
);
2316 read_lock(&mrt_lock
);
2317 err
= mr_fill_mroute(mrt
, skb
, &cache
->_c
, rtm
);
2318 read_unlock(&mrt_lock
);
2323 static int ipmr_fill_mroute(struct mr_table
*mrt
, struct sk_buff
*skb
,
2324 u32 portid
, u32 seq
, struct mfc_cache
*c
, int cmd
,
2327 struct nlmsghdr
*nlh
;
2331 nlh
= nlmsg_put(skb
, portid
, seq
, cmd
, sizeof(*rtm
), flags
);
2335 rtm
= nlmsg_data(nlh
);
2336 rtm
->rtm_family
= RTNL_FAMILY_IPMR
;
2337 rtm
->rtm_dst_len
= 32;
2338 rtm
->rtm_src_len
= 32;
2340 rtm
->rtm_table
= mrt
->id
;
2341 if (nla_put_u32(skb
, RTA_TABLE
, mrt
->id
))
2342 goto nla_put_failure
;
2343 rtm
->rtm_type
= RTN_MULTICAST
;
2344 rtm
->rtm_scope
= RT_SCOPE_UNIVERSE
;
2345 if (c
->_c
.mfc_flags
& MFC_STATIC
)
2346 rtm
->rtm_protocol
= RTPROT_STATIC
;
2348 rtm
->rtm_protocol
= RTPROT_MROUTED
;
2351 if (nla_put_in_addr(skb
, RTA_SRC
, c
->mfc_origin
) ||
2352 nla_put_in_addr(skb
, RTA_DST
, c
->mfc_mcastgrp
))
2353 goto nla_put_failure
;
2354 err
= mr_fill_mroute(mrt
, skb
, &c
->_c
, rtm
);
2355 /* do not break the dump if cache is unresolved */
2356 if (err
< 0 && err
!= -ENOENT
)
2357 goto nla_put_failure
;
2359 nlmsg_end(skb
, nlh
);
2363 nlmsg_cancel(skb
, nlh
);
2367 static int _ipmr_fill_mroute(struct mr_table
*mrt
, struct sk_buff
*skb
,
2368 u32 portid
, u32 seq
, struct mr_mfc
*c
, int cmd
,
2371 return ipmr_fill_mroute(mrt
, skb
, portid
, seq
, (struct mfc_cache
*)c
,
2375 static size_t mroute_msgsize(bool unresolved
, int maxvif
)
2378 NLMSG_ALIGN(sizeof(struct rtmsg
))
2379 + nla_total_size(4) /* RTA_TABLE */
2380 + nla_total_size(4) /* RTA_SRC */
2381 + nla_total_size(4) /* RTA_DST */
2386 + nla_total_size(4) /* RTA_IIF */
2387 + nla_total_size(0) /* RTA_MULTIPATH */
2388 + maxvif
* NLA_ALIGN(sizeof(struct rtnexthop
))
2390 + nla_total_size_64bit(sizeof(struct rta_mfc_stats
))
2396 static void mroute_netlink_event(struct mr_table
*mrt
, struct mfc_cache
*mfc
,
2399 struct net
*net
= read_pnet(&mrt
->net
);
2400 struct sk_buff
*skb
;
2403 skb
= nlmsg_new(mroute_msgsize(mfc
->_c
.mfc_parent
>= MAXVIFS
,
2409 err
= ipmr_fill_mroute(mrt
, skb
, 0, 0, mfc
, cmd
, 0);
2413 rtnl_notify(skb
, net
, 0, RTNLGRP_IPV4_MROUTE
, NULL
, GFP_ATOMIC
);
2419 rtnl_set_sk_err(net
, RTNLGRP_IPV4_MROUTE
, err
);
2422 static size_t igmpmsg_netlink_msgsize(size_t payloadlen
)
2425 NLMSG_ALIGN(sizeof(struct rtgenmsg
))
2426 + nla_total_size(1) /* IPMRA_CREPORT_MSGTYPE */
2427 + nla_total_size(4) /* IPMRA_CREPORT_VIF_ID */
2428 + nla_total_size(4) /* IPMRA_CREPORT_SRC_ADDR */
2429 + nla_total_size(4) /* IPMRA_CREPORT_DST_ADDR */
2430 /* IPMRA_CREPORT_PKT */
2431 + nla_total_size(payloadlen
)
2437 static void igmpmsg_netlink_event(struct mr_table
*mrt
, struct sk_buff
*pkt
)
2439 struct net
*net
= read_pnet(&mrt
->net
);
2440 struct nlmsghdr
*nlh
;
2441 struct rtgenmsg
*rtgenm
;
2442 struct igmpmsg
*msg
;
2443 struct sk_buff
*skb
;
2447 payloadlen
= pkt
->len
- sizeof(struct igmpmsg
);
2448 msg
= (struct igmpmsg
*)skb_network_header(pkt
);
2450 skb
= nlmsg_new(igmpmsg_netlink_msgsize(payloadlen
), GFP_ATOMIC
);
2454 nlh
= nlmsg_put(skb
, 0, 0, RTM_NEWCACHEREPORT
,
2455 sizeof(struct rtgenmsg
), 0);
2458 rtgenm
= nlmsg_data(nlh
);
2459 rtgenm
->rtgen_family
= RTNL_FAMILY_IPMR
;
2460 if (nla_put_u8(skb
, IPMRA_CREPORT_MSGTYPE
, msg
->im_msgtype
) ||
2461 nla_put_u32(skb
, IPMRA_CREPORT_VIF_ID
, msg
->im_vif
) ||
2462 nla_put_in_addr(skb
, IPMRA_CREPORT_SRC_ADDR
,
2463 msg
->im_src
.s_addr
) ||
2464 nla_put_in_addr(skb
, IPMRA_CREPORT_DST_ADDR
,
2465 msg
->im_dst
.s_addr
))
2466 goto nla_put_failure
;
2468 nla
= nla_reserve(skb
, IPMRA_CREPORT_PKT
, payloadlen
);
2469 if (!nla
|| skb_copy_bits(pkt
, sizeof(struct igmpmsg
),
2470 nla_data(nla
), payloadlen
))
2471 goto nla_put_failure
;
2473 nlmsg_end(skb
, nlh
);
2475 rtnl_notify(skb
, net
, 0, RTNLGRP_IPV4_MROUTE_R
, NULL
, GFP_ATOMIC
);
2479 nlmsg_cancel(skb
, nlh
);
2482 rtnl_set_sk_err(net
, RTNLGRP_IPV4_MROUTE_R
, -ENOBUFS
);
2485 static int ipmr_rtm_valid_getroute_req(struct sk_buff
*skb
,
2486 const struct nlmsghdr
*nlh
,
2488 struct netlink_ext_ack
*extack
)
2493 if (nlh
->nlmsg_len
< nlmsg_msg_size(sizeof(*rtm
))) {
2494 NL_SET_ERR_MSG(extack
, "ipv4: Invalid header for multicast route get request");
2498 if (!netlink_strict_get_check(skb
))
2499 return nlmsg_parse_deprecated(nlh
, sizeof(*rtm
), tb
, RTA_MAX
,
2500 rtm_ipv4_policy
, extack
);
2502 rtm
= nlmsg_data(nlh
);
2503 if ((rtm
->rtm_src_len
&& rtm
->rtm_src_len
!= 32) ||
2504 (rtm
->rtm_dst_len
&& rtm
->rtm_dst_len
!= 32) ||
2505 rtm
->rtm_tos
|| rtm
->rtm_table
|| rtm
->rtm_protocol
||
2506 rtm
->rtm_scope
|| rtm
->rtm_type
|| rtm
->rtm_flags
) {
2507 NL_SET_ERR_MSG(extack
, "ipv4: Invalid values in header for multicast route get request");
2511 err
= nlmsg_parse_deprecated_strict(nlh
, sizeof(*rtm
), tb
, RTA_MAX
,
2512 rtm_ipv4_policy
, extack
);
2516 if ((tb
[RTA_SRC
] && !rtm
->rtm_src_len
) ||
2517 (tb
[RTA_DST
] && !rtm
->rtm_dst_len
)) {
2518 NL_SET_ERR_MSG(extack
, "ipv4: rtm_src_len and rtm_dst_len must be 32 for IPv4");
2522 for (i
= 0; i
<= RTA_MAX
; i
++) {
2532 NL_SET_ERR_MSG(extack
, "ipv4: Unsupported attribute in multicast route get request");
2540 static int ipmr_rtm_getroute(struct sk_buff
*in_skb
, struct nlmsghdr
*nlh
,
2541 struct netlink_ext_ack
*extack
)
2543 struct net
*net
= sock_net(in_skb
->sk
);
2544 struct nlattr
*tb
[RTA_MAX
+ 1];
2545 struct sk_buff
*skb
= NULL
;
2546 struct mfc_cache
*cache
;
2547 struct mr_table
*mrt
;
2552 err
= ipmr_rtm_valid_getroute_req(in_skb
, nlh
, tb
, extack
);
2556 src
= tb
[RTA_SRC
] ? nla_get_in_addr(tb
[RTA_SRC
]) : 0;
2557 grp
= tb
[RTA_DST
] ? nla_get_in_addr(tb
[RTA_DST
]) : 0;
2558 tableid
= tb
[RTA_TABLE
] ? nla_get_u32(tb
[RTA_TABLE
]) : 0;
2560 mrt
= ipmr_get_table(net
, tableid
? tableid
: RT_TABLE_DEFAULT
);
2566 /* entries are added/deleted only under RTNL */
2568 cache
= ipmr_cache_find(mrt
, src
, grp
);
2575 skb
= nlmsg_new(mroute_msgsize(false, mrt
->maxvif
), GFP_KERNEL
);
2581 err
= ipmr_fill_mroute(mrt
, skb
, NETLINK_CB(in_skb
).portid
,
2582 nlh
->nlmsg_seq
, cache
,
2587 err
= rtnl_unicast(skb
, net
, NETLINK_CB(in_skb
).portid
);
2597 static int ipmr_rtm_dumproute(struct sk_buff
*skb
, struct netlink_callback
*cb
)
2599 struct fib_dump_filter filter
= {};
2602 if (cb
->strict_check
) {
2603 err
= ip_valid_fib_dump_req(sock_net(skb
->sk
), cb
->nlh
,
2609 if (filter
.table_id
) {
2610 struct mr_table
*mrt
;
2612 mrt
= ipmr_get_table(sock_net(skb
->sk
), filter
.table_id
);
2614 if (filter
.dump_all_families
)
2617 NL_SET_ERR_MSG(cb
->extack
, "ipv4: MR table does not exist");
2620 err
= mr_table_dump(mrt
, skb
, cb
, _ipmr_fill_mroute
,
2621 &mfc_unres_lock
, &filter
);
2622 return skb
->len
? : err
;
2625 return mr_rtm_dumproute(skb
, cb
, ipmr_mr_table_iter
,
2626 _ipmr_fill_mroute
, &mfc_unres_lock
, &filter
);
2629 static const struct nla_policy rtm_ipmr_policy
[RTA_MAX
+ 1] = {
2630 [RTA_SRC
] = { .type
= NLA_U32
},
2631 [RTA_DST
] = { .type
= NLA_U32
},
2632 [RTA_IIF
] = { .type
= NLA_U32
},
2633 [RTA_TABLE
] = { .type
= NLA_U32
},
2634 [RTA_MULTIPATH
] = { .len
= sizeof(struct rtnexthop
) },
2637 static bool ipmr_rtm_validate_proto(unsigned char rtm_protocol
)
2639 switch (rtm_protocol
) {
2641 case RTPROT_MROUTED
:
2647 static int ipmr_nla_get_ttls(const struct nlattr
*nla
, struct mfcctl
*mfcc
)
2649 struct rtnexthop
*rtnh
= nla_data(nla
);
2650 int remaining
= nla_len(nla
), vifi
= 0;
2652 while (rtnh_ok(rtnh
, remaining
)) {
2653 mfcc
->mfcc_ttls
[vifi
] = rtnh
->rtnh_hops
;
2654 if (++vifi
== MAXVIFS
)
2656 rtnh
= rtnh_next(rtnh
, &remaining
);
2659 return remaining
> 0 ? -EINVAL
: vifi
;
2662 /* returns < 0 on error, 0 for ADD_MFC and 1 for ADD_MFC_PROXY */
2663 static int rtm_to_ipmr_mfcc(struct net
*net
, struct nlmsghdr
*nlh
,
2664 struct mfcctl
*mfcc
, int *mrtsock
,
2665 struct mr_table
**mrtret
,
2666 struct netlink_ext_ack
*extack
)
2668 struct net_device
*dev
= NULL
;
2669 u32 tblid
= RT_TABLE_DEFAULT
;
2670 struct mr_table
*mrt
;
2671 struct nlattr
*attr
;
2675 ret
= nlmsg_validate_deprecated(nlh
, sizeof(*rtm
), RTA_MAX
,
2676 rtm_ipmr_policy
, extack
);
2679 rtm
= nlmsg_data(nlh
);
2682 if (rtm
->rtm_family
!= RTNL_FAMILY_IPMR
|| rtm
->rtm_dst_len
!= 32 ||
2683 rtm
->rtm_type
!= RTN_MULTICAST
||
2684 rtm
->rtm_scope
!= RT_SCOPE_UNIVERSE
||
2685 !ipmr_rtm_validate_proto(rtm
->rtm_protocol
))
2688 memset(mfcc
, 0, sizeof(*mfcc
));
2689 mfcc
->mfcc_parent
= -1;
2691 nlmsg_for_each_attr(attr
, nlh
, sizeof(struct rtmsg
), rem
) {
2692 switch (nla_type(attr
)) {
2694 mfcc
->mfcc_origin
.s_addr
= nla_get_be32(attr
);
2697 mfcc
->mfcc_mcastgrp
.s_addr
= nla_get_be32(attr
);
2700 dev
= __dev_get_by_index(net
, nla_get_u32(attr
));
2707 if (ipmr_nla_get_ttls(attr
, mfcc
) < 0) {
2716 tblid
= nla_get_u32(attr
);
2720 mrt
= ipmr_get_table(net
, tblid
);
2726 *mrtsock
= rtm
->rtm_protocol
== RTPROT_MROUTED
? 1 : 0;
2728 mfcc
->mfcc_parent
= ipmr_find_vif(mrt
, dev
);
2734 /* takes care of both newroute and delroute */
2735 static int ipmr_rtm_route(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
2736 struct netlink_ext_ack
*extack
)
2738 struct net
*net
= sock_net(skb
->sk
);
2739 int ret
, mrtsock
, parent
;
2740 struct mr_table
*tbl
;
2745 ret
= rtm_to_ipmr_mfcc(net
, nlh
, &mfcc
, &mrtsock
, &tbl
, extack
);
2749 parent
= ret
? mfcc
.mfcc_parent
: -1;
2750 if (nlh
->nlmsg_type
== RTM_NEWROUTE
)
2751 return ipmr_mfc_add(net
, tbl
, &mfcc
, mrtsock
, parent
);
2753 return ipmr_mfc_delete(tbl
, &mfcc
, parent
);
2756 static bool ipmr_fill_table(struct mr_table
*mrt
, struct sk_buff
*skb
)
2758 u32 queue_len
= atomic_read(&mrt
->cache_resolve_queue_len
);
2760 if (nla_put_u32(skb
, IPMRA_TABLE_ID
, mrt
->id
) ||
2761 nla_put_u32(skb
, IPMRA_TABLE_CACHE_RES_QUEUE_LEN
, queue_len
) ||
2762 nla_put_s32(skb
, IPMRA_TABLE_MROUTE_REG_VIF_NUM
,
2763 mrt
->mroute_reg_vif_num
) ||
2764 nla_put_u8(skb
, IPMRA_TABLE_MROUTE_DO_ASSERT
,
2765 mrt
->mroute_do_assert
) ||
2766 nla_put_u8(skb
, IPMRA_TABLE_MROUTE_DO_PIM
, mrt
->mroute_do_pim
) ||
2767 nla_put_u8(skb
, IPMRA_TABLE_MROUTE_DO_WRVIFWHOLE
,
2768 mrt
->mroute_do_wrvifwhole
))
2774 static bool ipmr_fill_vif(struct mr_table
*mrt
, u32 vifid
, struct sk_buff
*skb
)
2776 struct nlattr
*vif_nest
;
2777 struct vif_device
*vif
;
2779 /* if the VIF doesn't exist just continue */
2780 if (!VIF_EXISTS(mrt
, vifid
))
2783 vif
= &mrt
->vif_table
[vifid
];
2784 vif_nest
= nla_nest_start_noflag(skb
, IPMRA_VIF
);
2787 if (nla_put_u32(skb
, IPMRA_VIFA_IFINDEX
, vif
->dev
->ifindex
) ||
2788 nla_put_u32(skb
, IPMRA_VIFA_VIF_ID
, vifid
) ||
2789 nla_put_u16(skb
, IPMRA_VIFA_FLAGS
, vif
->flags
) ||
2790 nla_put_u64_64bit(skb
, IPMRA_VIFA_BYTES_IN
, vif
->bytes_in
,
2792 nla_put_u64_64bit(skb
, IPMRA_VIFA_BYTES_OUT
, vif
->bytes_out
,
2794 nla_put_u64_64bit(skb
, IPMRA_VIFA_PACKETS_IN
, vif
->pkt_in
,
2796 nla_put_u64_64bit(skb
, IPMRA_VIFA_PACKETS_OUT
, vif
->pkt_out
,
2798 nla_put_be32(skb
, IPMRA_VIFA_LOCAL_ADDR
, vif
->local
) ||
2799 nla_put_be32(skb
, IPMRA_VIFA_REMOTE_ADDR
, vif
->remote
)) {
2800 nla_nest_cancel(skb
, vif_nest
);
2803 nla_nest_end(skb
, vif_nest
);
2808 static int ipmr_valid_dumplink(const struct nlmsghdr
*nlh
,
2809 struct netlink_ext_ack
*extack
)
2811 struct ifinfomsg
*ifm
;
2813 if (nlh
->nlmsg_len
< nlmsg_msg_size(sizeof(*ifm
))) {
2814 NL_SET_ERR_MSG(extack
, "ipv4: Invalid header for ipmr link dump");
2818 if (nlmsg_attrlen(nlh
, sizeof(*ifm
))) {
2819 NL_SET_ERR_MSG(extack
, "Invalid data after header in ipmr link dump");
2823 ifm
= nlmsg_data(nlh
);
2824 if (ifm
->__ifi_pad
|| ifm
->ifi_type
|| ifm
->ifi_flags
||
2825 ifm
->ifi_change
|| ifm
->ifi_index
) {
2826 NL_SET_ERR_MSG(extack
, "Invalid values in header for ipmr link dump request");
2833 static int ipmr_rtm_dumplink(struct sk_buff
*skb
, struct netlink_callback
*cb
)
2835 struct net
*net
= sock_net(skb
->sk
);
2836 struct nlmsghdr
*nlh
= NULL
;
2837 unsigned int t
= 0, s_t
;
2838 unsigned int e
= 0, s_e
;
2839 struct mr_table
*mrt
;
2841 if (cb
->strict_check
) {
2842 int err
= ipmr_valid_dumplink(cb
->nlh
, cb
->extack
);
2851 ipmr_for_each_table(mrt
, net
) {
2852 struct nlattr
*vifs
, *af
;
2853 struct ifinfomsg
*hdr
;
2858 nlh
= nlmsg_put(skb
, NETLINK_CB(cb
->skb
).portid
,
2859 cb
->nlh
->nlmsg_seq
, RTM_NEWLINK
,
2860 sizeof(*hdr
), NLM_F_MULTI
);
2864 hdr
= nlmsg_data(nlh
);
2865 memset(hdr
, 0, sizeof(*hdr
));
2866 hdr
->ifi_family
= RTNL_FAMILY_IPMR
;
2868 af
= nla_nest_start_noflag(skb
, IFLA_AF_SPEC
);
2870 nlmsg_cancel(skb
, nlh
);
2874 if (!ipmr_fill_table(mrt
, skb
)) {
2875 nlmsg_cancel(skb
, nlh
);
2879 vifs
= nla_nest_start_noflag(skb
, IPMRA_TABLE_VIFS
);
2881 nla_nest_end(skb
, af
);
2882 nlmsg_end(skb
, nlh
);
2885 for (i
= 0; i
< mrt
->maxvif
; i
++) {
2888 if (!ipmr_fill_vif(mrt
, i
, skb
)) {
2889 nla_nest_end(skb
, vifs
);
2890 nla_nest_end(skb
, af
);
2891 nlmsg_end(skb
, nlh
);
2899 nla_nest_end(skb
, vifs
);
2900 nla_nest_end(skb
, af
);
2901 nlmsg_end(skb
, nlh
);
2913 #ifdef CONFIG_PROC_FS
2914 /* The /proc interfaces to multicast routing :
2915 * /proc/net/ip_mr_cache & /proc/net/ip_mr_vif
2918 static void *ipmr_vif_seq_start(struct seq_file
*seq
, loff_t
*pos
)
2919 __acquires(mrt_lock
)
2921 struct mr_vif_iter
*iter
= seq
->private;
2922 struct net
*net
= seq_file_net(seq
);
2923 struct mr_table
*mrt
;
2925 mrt
= ipmr_get_table(net
, RT_TABLE_DEFAULT
);
2927 return ERR_PTR(-ENOENT
);
2931 read_lock(&mrt_lock
);
2932 return mr_vif_seq_start(seq
, pos
);
2935 static void ipmr_vif_seq_stop(struct seq_file
*seq
, void *v
)
2936 __releases(mrt_lock
)
2938 read_unlock(&mrt_lock
);
2941 static int ipmr_vif_seq_show(struct seq_file
*seq
, void *v
)
2943 struct mr_vif_iter
*iter
= seq
->private;
2944 struct mr_table
*mrt
= iter
->mrt
;
2946 if (v
== SEQ_START_TOKEN
) {
2948 "Interface BytesIn PktsIn BytesOut PktsOut Flags Local Remote\n");
2950 const struct vif_device
*vif
= v
;
2951 const char *name
= vif
->dev
?
2952 vif
->dev
->name
: "none";
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
);
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
,
2971 static void *ipmr_mfc_seq_start(struct seq_file
*seq
, loff_t
*pos
)
2973 struct net
*net
= seq_file_net(seq
);
2974 struct mr_table
*mrt
;
2976 mrt
= ipmr_get_table(net
, RT_TABLE_DEFAULT
);
2978 return ERR_PTR(-ENOENT
);
2980 return mr_mfc_seq_start(seq
, pos
, mrt
, &mfc_unres_lock
);
2983 static int ipmr_mfc_seq_show(struct seq_file
*seq
, void *v
)
2987 if (v
== SEQ_START_TOKEN
) {
2989 "Group Origin Iif Pkts Bytes Wrong Oifs\n");
2991 const struct mfc_cache
*mfc
= v
;
2992 const struct mr_mfc_iter
*it
= seq
->private;
2993 const struct mr_table
*mrt
= it
->mrt
;
2995 seq_printf(seq
, "%08X %08X %-3hd",
2996 (__force u32
) mfc
->mfc_mcastgrp
,
2997 (__force u32
) mfc
->mfc_origin
,
2998 mfc
->_c
.mfc_parent
);
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)
3011 n
, mfc
->_c
.mfc_un
.res
.ttls
[n
]);
3014 /* unresolved mfc_caches don't contain
3015 * pkt, bytes and wrong_if values
3017 seq_printf(seq
, " %8lu %8lu %8lu", 0ul, 0ul, 0ul);
3019 seq_putc(seq
, '\n');
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
,
3032 #ifdef CONFIG_IP_PIMSM_V2
3033 static const struct net_protocol pim_protocol
= {
3039 static unsigned int ipmr_seq_read(struct net
*net
)
3043 return net
->ipv4
.ipmr_seq
+ ipmr_rules_seq_read(net
);
3046 static int ipmr_dump(struct net
*net
, struct notifier_block
*nb
,
3047 struct netlink_ext_ack
*extack
)
3049 return mr_dump(net
, nb
, RTNL_FAMILY_IPMR
, ipmr_rules_dump
,
3050 ipmr_mr_table_iter
, &mrt_lock
, extack
);
3053 static const struct fib_notifier_ops ipmr_notifier_ops_template
= {
3054 .family
= RTNL_FAMILY_IPMR
,
3055 .fib_seq_read
= ipmr_seq_read
,
3056 .fib_dump
= ipmr_dump
,
3057 .owner
= THIS_MODULE
,
3060 static int __net_init
ipmr_notifier_init(struct net
*net
)
3062 struct fib_notifier_ops
*ops
;
3064 net
->ipv4
.ipmr_seq
= 0;
3066 ops
= fib_notifier_ops_register(&ipmr_notifier_ops_template
, net
);
3068 return PTR_ERR(ops
);
3069 net
->ipv4
.ipmr_notifier_ops
= ops
;
3074 static void __net_exit
ipmr_notifier_exit(struct net
*net
)
3076 fib_notifier_ops_unregister(net
->ipv4
.ipmr_notifier_ops
);
3077 net
->ipv4
.ipmr_notifier_ops
= NULL
;
3080 /* Setup for IP multicast routing */
3081 static int __net_init
ipmr_net_init(struct net
*net
)
3085 err
= ipmr_notifier_init(net
);
3087 goto ipmr_notifier_fail
;
3089 err
= ipmr_rules_init(net
);
3091 goto ipmr_rules_fail
;
3093 #ifdef CONFIG_PROC_FS
3095 if (!proc_create_net("ip_mr_vif", 0, net
->proc_net
, &ipmr_vif_seq_ops
,
3096 sizeof(struct mr_vif_iter
)))
3098 if (!proc_create_net("ip_mr_cache", 0, net
->proc_net
, &ipmr_mfc_seq_ops
,
3099 sizeof(struct mr_mfc_iter
)))
3100 goto proc_cache_fail
;
3104 #ifdef CONFIG_PROC_FS
3106 remove_proc_entry("ip_mr_vif", net
->proc_net
);
3108 ipmr_rules_exit(net
);
3111 ipmr_notifier_exit(net
);
3116 static void __net_exit
ipmr_net_exit(struct net
*net
)
3118 #ifdef CONFIG_PROC_FS
3119 remove_proc_entry("ip_mr_cache", net
->proc_net
);
3120 remove_proc_entry("ip_mr_vif", net
->proc_net
);
3122 ipmr_notifier_exit(net
);
3123 ipmr_rules_exit(net
);
3126 static struct pernet_operations ipmr_net_ops
= {
3127 .init
= ipmr_net_init
,
3128 .exit
= ipmr_net_exit
,
3131 int __init
ip_mr_init(void)
3135 mrt_cachep
= kmem_cache_create("ip_mrt_cache",
3136 sizeof(struct mfc_cache
),
3137 0, SLAB_HWCACHE_ALIGN
| SLAB_PANIC
,
3140 err
= register_pernet_subsys(&ipmr_net_ops
);
3142 goto reg_pernet_fail
;
3144 err
= register_netdevice_notifier(&ip_mr_notifier
);
3146 goto reg_notif_fail
;
3147 #ifdef CONFIG_IP_PIMSM_V2
3148 if (inet_add_protocol(&pim_protocol
, IPPROTO_PIM
) < 0) {
3149 pr_err("%s: can't add PIM protocol\n", __func__
);
3151 goto add_proto_fail
;
3154 rtnl_register(RTNL_FAMILY_IPMR
, RTM_GETROUTE
,
3155 ipmr_rtm_getroute
, ipmr_rtm_dumproute
, 0);
3156 rtnl_register(RTNL_FAMILY_IPMR
, RTM_NEWROUTE
,
3157 ipmr_rtm_route
, NULL
, 0);
3158 rtnl_register(RTNL_FAMILY_IPMR
, RTM_DELROUTE
,
3159 ipmr_rtm_route
, NULL
, 0);
3161 rtnl_register(RTNL_FAMILY_IPMR
, RTM_GETLINK
,
3162 NULL
, ipmr_rtm_dumplink
, 0);
3165 #ifdef CONFIG_IP_PIMSM_V2
3167 unregister_netdevice_notifier(&ip_mr_notifier
);
3170 unregister_pernet_subsys(&ipmr_net_ops
);
3172 kmem_cache_destroy(mrt_cachep
);