Thomas Gleixner | 2874c5f | 2019-05-27 08:55:01 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 2 | /* |
| 3 | * Linux IPv6 multicast routing support for BSD pim6sd |
| 4 | * Based on net/ipv4/ipmr.c. |
| 5 | * |
| 6 | * (c) 2004 Mickael Hoerdt, <hoerdt@clarinet.u-strasbg.fr> |
| 7 | * LSIIT Laboratory, Strasbourg, France |
| 8 | * (c) 2004 Jean-Philippe Andriot, <jean-philippe.andriot@6WIND.com> |
| 9 | * 6WIND, Paris, France |
| 10 | * Copyright (C)2007,2008 USAGI/WIDE Project |
| 11 | * YOSHIFUJI Hideaki <yoshfuji@linux-ipv6.org> |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 12 | */ |
| 13 | |
Linus Torvalds | 7c0f6ba | 2016-12-24 11:46:01 -0800 | [diff] [blame] | 14 | #include <linux/uaccess.h> |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 15 | #include <linux/types.h> |
| 16 | #include <linux/sched.h> |
| 17 | #include <linux/errno.h> |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 18 | #include <linux/mm.h> |
| 19 | #include <linux/kernel.h> |
| 20 | #include <linux/fcntl.h> |
| 21 | #include <linux/stat.h> |
| 22 | #include <linux/socket.h> |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 23 | #include <linux/inet.h> |
| 24 | #include <linux/netdevice.h> |
| 25 | #include <linux/inetdevice.h> |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 26 | #include <linux/proc_fs.h> |
| 27 | #include <linux/seq_file.h> |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 28 | #include <linux/init.h> |
David S. Miller | e2d5776 | 2011-02-03 17:59:32 -0800 | [diff] [blame] | 29 | #include <linux/compat.h> |
NeilBrown | 0eb71a9 | 2018-06-18 12:52:50 +1000 | [diff] [blame] | 30 | #include <linux/rhashtable.h> |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 31 | #include <net/protocol.h> |
| 32 | #include <linux/skbuff.h> |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 33 | #include <net/raw.h> |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 34 | #include <linux/notifier.h> |
| 35 | #include <linux/if_arp.h> |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 36 | #include <net/checksum.h> |
| 37 | #include <net/netlink.h> |
Patrick McHardy | d1db275 | 2010-05-11 14:40:55 +0200 | [diff] [blame] | 38 | #include <net/fib_rules.h> |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 39 | |
| 40 | #include <net/ipv6.h> |
| 41 | #include <net/ip6_route.h> |
| 42 | #include <linux/mroute6.h> |
YOSHIFUJI Hideaki | 14fb64e | 2008-04-03 09:22:54 +0900 | [diff] [blame] | 43 | #include <linux/pim.h> |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 44 | #include <net/addrconf.h> |
| 45 | #include <linux/netfilter_ipv6.h> |
Paul Gortmaker | bc3b2d7 | 2011-07-15 11:47:34 -0400 | [diff] [blame] | 46 | #include <linux/export.h> |
Dave Jones | 5d6e430 | 2009-01-31 00:51:49 -0800 | [diff] [blame] | 47 | #include <net/ip6_checksum.h> |
Nicolas Dichtel | d67b8c6 | 2012-12-04 01:13:35 +0000 | [diff] [blame] | 48 | #include <linux/netconf.h> |
Willem de Bruijn | cb9f1b7 | 2018-12-30 17:24:36 -0500 | [diff] [blame] | 49 | #include <net/ip_tunnels.h> |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 50 | |
Gustavo A. R. Silva | 69d2c86 | 2018-12-11 14:10:08 -0600 | [diff] [blame] | 51 | #include <linux/nospec.h> |
| 52 | |
Patrick McHardy | d1db275 | 2010-05-11 14:40:55 +0200 | [diff] [blame] | 53 | struct ip6mr_rule { |
| 54 | struct fib_rule common; |
| 55 | }; |
| 56 | |
| 57 | struct ip6mr_result { |
Yuval Mintz | b70432f | 2018-02-28 23:29:32 +0200 | [diff] [blame] | 58 | struct mr_table *mrt; |
Patrick McHardy | d1db275 | 2010-05-11 14:40:55 +0200 | [diff] [blame] | 59 | }; |
| 60 | |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 61 | /* Big lock, protecting vif table, mrt cache and mroute socket state. |
| 62 | Note that the changes are semaphored via rtnl_lock. |
| 63 | */ |
| 64 | |
| 65 | static DEFINE_RWLOCK(mrt_lock); |
| 66 | |
Yuval Mintz | b70432f | 2018-02-28 23:29:32 +0200 | [diff] [blame] | 67 | /* Multicast router control variables */ |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 68 | |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 69 | /* Special spinlock for queue of unresolved entries */ |
| 70 | static DEFINE_SPINLOCK(mfc_unres_lock); |
| 71 | |
| 72 | /* We return to original Alan's scheme. Hash table of resolved |
| 73 | entries is changed only in process context and protected |
| 74 | with weak lock mrt_lock. Queue of unresolved entries is protected |
| 75 | with strong spinlock mfc_unres_lock. |
| 76 | |
| 77 | In this case data path is free of exclusive locks at all. |
| 78 | */ |
| 79 | |
| 80 | static struct kmem_cache *mrt_cachep __read_mostly; |
| 81 | |
Yuval Mintz | b70432f | 2018-02-28 23:29:32 +0200 | [diff] [blame] | 82 | static struct mr_table *ip6mr_new_table(struct net *net, u32 id); |
| 83 | static void ip6mr_free_table(struct mr_table *mrt); |
Patrick McHardy | d1db275 | 2010-05-11 14:40:55 +0200 | [diff] [blame] | 84 | |
Yuval Mintz | b70432f | 2018-02-28 23:29:32 +0200 | [diff] [blame] | 85 | static void ip6_mr_forward(struct net *net, struct mr_table *mrt, |
Patrick Ruddy | e4a38c0 | 2018-10-01 09:41:27 +0100 | [diff] [blame] | 86 | struct net_device *dev, struct sk_buff *skb, |
| 87 | struct mfc6_cache *cache); |
Yuval Mintz | b70432f | 2018-02-28 23:29:32 +0200 | [diff] [blame] | 88 | static int ip6mr_cache_report(struct mr_table *mrt, struct sk_buff *pkt, |
Benjamin Thery | 8229efd | 2008-12-10 16:30:15 -0800 | [diff] [blame] | 89 | mifi_t mifi, int assert); |
Yuval Mintz | b70432f | 2018-02-28 23:29:32 +0200 | [diff] [blame] | 90 | static void mr6_netlink_event(struct mr_table *mrt, struct mfc6_cache *mfc, |
Nicolas Dichtel | 812e44d | 2012-12-04 01:13:41 +0000 | [diff] [blame] | 91 | int cmd); |
Yuval Mintz | b70432f | 2018-02-28 23:29:32 +0200 | [diff] [blame] | 92 | static void mrt6msg_netlink_event(struct mr_table *mrt, struct sk_buff *pkt); |
Patrick McHardy | 5b285ca | 2010-05-11 14:40:56 +0200 | [diff] [blame] | 93 | static int ip6mr_rtm_dumproute(struct sk_buff *skb, |
| 94 | struct netlink_callback *cb); |
Callum Sinclair | ca8d479 | 2019-02-18 10:07:52 +1300 | [diff] [blame] | 95 | static void mroute_clean_tables(struct mr_table *mrt, int flags); |
Kees Cook | e99e88a | 2017-10-16 14:43:17 -0700 | [diff] [blame] | 96 | static void ipmr_expire_process(struct timer_list *t); |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 97 | |
Patrick McHardy | d1db275 | 2010-05-11 14:40:55 +0200 | [diff] [blame] | 98 | #ifdef CONFIG_IPV6_MROUTE_MULTIPLE_TABLES |
Eric Dumazet | 8ffb335 | 2010-06-06 15:34:40 -0700 | [diff] [blame] | 99 | #define ip6mr_for_each_table(mrt, net) \ |
Amol Grover | 28b380e | 2020-02-22 22:27:27 +0530 | [diff] [blame] | 100 | list_for_each_entry_rcu(mrt, &net->ipv6.mr6_tables, list, \ |
Madhuparna Bhowmik | b6dd5ac | 2020-05-16 13:15:15 +0530 | [diff] [blame] | 101 | lockdep_rtnl_is_held() || \ |
| 102 | list_empty(&net->ipv6.mr6_tables)) |
Patrick McHardy | d1db275 | 2010-05-11 14:40:55 +0200 | [diff] [blame] | 103 | |
Yuval Mintz | 7b0db85 | 2018-02-28 23:29:39 +0200 | [diff] [blame] | 104 | static struct mr_table *ip6mr_mr_table_iter(struct net *net, |
| 105 | struct mr_table *mrt) |
| 106 | { |
| 107 | struct mr_table *ret; |
| 108 | |
| 109 | if (!mrt) |
| 110 | ret = list_entry_rcu(net->ipv6.mr6_tables.next, |
| 111 | struct mr_table, list); |
| 112 | else |
| 113 | ret = list_entry_rcu(mrt->list.next, |
| 114 | struct mr_table, list); |
| 115 | |
| 116 | if (&ret->list == &net->ipv6.mr6_tables) |
| 117 | return NULL; |
| 118 | return ret; |
| 119 | } |
| 120 | |
Yuval Mintz | b70432f | 2018-02-28 23:29:32 +0200 | [diff] [blame] | 121 | static struct mr_table *ip6mr_get_table(struct net *net, u32 id) |
Patrick McHardy | d1db275 | 2010-05-11 14:40:55 +0200 | [diff] [blame] | 122 | { |
Yuval Mintz | b70432f | 2018-02-28 23:29:32 +0200 | [diff] [blame] | 123 | struct mr_table *mrt; |
Patrick McHardy | d1db275 | 2010-05-11 14:40:55 +0200 | [diff] [blame] | 124 | |
| 125 | ip6mr_for_each_table(mrt, net) { |
| 126 | if (mrt->id == id) |
| 127 | return mrt; |
| 128 | } |
| 129 | return NULL; |
| 130 | } |
| 131 | |
David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 132 | static int ip6mr_fib_lookup(struct net *net, struct flowi6 *flp6, |
Yuval Mintz | b70432f | 2018-02-28 23:29:32 +0200 | [diff] [blame] | 133 | struct mr_table **mrt) |
Patrick McHardy | d1db275 | 2010-05-11 14:40:55 +0200 | [diff] [blame] | 134 | { |
Patrick McHardy | d1db275 | 2010-05-11 14:40:55 +0200 | [diff] [blame] | 135 | int err; |
Hannes Frederic Sowa | 95f4a45 | 2014-01-13 02:45:22 +0100 | [diff] [blame] | 136 | struct ip6mr_result res; |
| 137 | struct fib_lookup_arg arg = { |
| 138 | .result = &res, |
| 139 | .flags = FIB_LOOKUP_NOREF, |
| 140 | }; |
Patrick McHardy | d1db275 | 2010-05-11 14:40:55 +0200 | [diff] [blame] | 141 | |
Patrick Ruddy | e4a38c0 | 2018-10-01 09:41:27 +0100 | [diff] [blame] | 142 | /* update flow if oif or iif point to device enslaved to l3mdev */ |
| 143 | l3mdev_update_flow(net, flowi6_to_flowi(flp6)); |
| 144 | |
David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 145 | err = fib_rules_lookup(net->ipv6.mr6_rules_ops, |
| 146 | flowi6_to_flowi(flp6), 0, &arg); |
Patrick McHardy | d1db275 | 2010-05-11 14:40:55 +0200 | [diff] [blame] | 147 | if (err < 0) |
| 148 | return err; |
| 149 | *mrt = res.mrt; |
| 150 | return 0; |
| 151 | } |
| 152 | |
| 153 | static int ip6mr_rule_action(struct fib_rule *rule, struct flowi *flp, |
| 154 | int flags, struct fib_lookup_arg *arg) |
| 155 | { |
| 156 | struct ip6mr_result *res = arg->result; |
Yuval Mintz | b70432f | 2018-02-28 23:29:32 +0200 | [diff] [blame] | 157 | struct mr_table *mrt; |
Patrick McHardy | d1db275 | 2010-05-11 14:40:55 +0200 | [diff] [blame] | 158 | |
| 159 | switch (rule->action) { |
| 160 | case FR_ACT_TO_TBL: |
| 161 | break; |
| 162 | case FR_ACT_UNREACHABLE: |
| 163 | return -ENETUNREACH; |
| 164 | case FR_ACT_PROHIBIT: |
| 165 | return -EACCES; |
| 166 | case FR_ACT_BLACKHOLE: |
| 167 | default: |
| 168 | return -EINVAL; |
| 169 | } |
| 170 | |
Patrick Ruddy | e4a38c0 | 2018-10-01 09:41:27 +0100 | [diff] [blame] | 171 | arg->table = fib_rule_get_table(rule, arg); |
| 172 | |
| 173 | mrt = ip6mr_get_table(rule->fr_net, arg->table); |
Ian Morris | 63159f2 | 2015-03-29 14:00:04 +0100 | [diff] [blame] | 174 | if (!mrt) |
Patrick McHardy | d1db275 | 2010-05-11 14:40:55 +0200 | [diff] [blame] | 175 | return -EAGAIN; |
| 176 | res->mrt = mrt; |
| 177 | return 0; |
| 178 | } |
| 179 | |
| 180 | static int ip6mr_rule_match(struct fib_rule *rule, struct flowi *flp, int flags) |
| 181 | { |
| 182 | return 1; |
| 183 | } |
| 184 | |
Patrick McHardy | d1db275 | 2010-05-11 14:40:55 +0200 | [diff] [blame] | 185 | static int ip6mr_rule_configure(struct fib_rule *rule, struct sk_buff *skb, |
Roopa Prabhu | b16fb41 | 2018-04-21 09:41:31 -0700 | [diff] [blame] | 186 | struct fib_rule_hdr *frh, struct nlattr **tb, |
| 187 | struct netlink_ext_ack *extack) |
Patrick McHardy | d1db275 | 2010-05-11 14:40:55 +0200 | [diff] [blame] | 188 | { |
| 189 | return 0; |
| 190 | } |
| 191 | |
| 192 | static int ip6mr_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh, |
| 193 | struct nlattr **tb) |
| 194 | { |
| 195 | return 1; |
| 196 | } |
| 197 | |
| 198 | static int ip6mr_rule_fill(struct fib_rule *rule, struct sk_buff *skb, |
| 199 | struct fib_rule_hdr *frh) |
| 200 | { |
| 201 | frh->dst_len = 0; |
| 202 | frh->src_len = 0; |
| 203 | frh->tos = 0; |
| 204 | return 0; |
| 205 | } |
| 206 | |
Andi Kleen | 04a6f82 | 2012-10-04 17:12:11 -0700 | [diff] [blame] | 207 | static const struct fib_rules_ops __net_initconst ip6mr_rules_ops_template = { |
Patrick McHardy | d1db275 | 2010-05-11 14:40:55 +0200 | [diff] [blame] | 208 | .family = RTNL_FAMILY_IP6MR, |
| 209 | .rule_size = sizeof(struct ip6mr_rule), |
| 210 | .addr_size = sizeof(struct in6_addr), |
| 211 | .action = ip6mr_rule_action, |
| 212 | .match = ip6mr_rule_match, |
| 213 | .configure = ip6mr_rule_configure, |
| 214 | .compare = ip6mr_rule_compare, |
Patrick McHardy | d1db275 | 2010-05-11 14:40:55 +0200 | [diff] [blame] | 215 | .fill = ip6mr_rule_fill, |
| 216 | .nlgroup = RTNLGRP_IPV6_RULE, |
Patrick McHardy | d1db275 | 2010-05-11 14:40:55 +0200 | [diff] [blame] | 217 | .owner = THIS_MODULE, |
| 218 | }; |
| 219 | |
| 220 | static int __net_init ip6mr_rules_init(struct net *net) |
| 221 | { |
| 222 | struct fib_rules_ops *ops; |
Yuval Mintz | b70432f | 2018-02-28 23:29:32 +0200 | [diff] [blame] | 223 | struct mr_table *mrt; |
Patrick McHardy | d1db275 | 2010-05-11 14:40:55 +0200 | [diff] [blame] | 224 | int err; |
| 225 | |
| 226 | ops = fib_rules_register(&ip6mr_rules_ops_template, net); |
| 227 | if (IS_ERR(ops)) |
| 228 | return PTR_ERR(ops); |
| 229 | |
| 230 | INIT_LIST_HEAD(&net->ipv6.mr6_tables); |
| 231 | |
| 232 | mrt = ip6mr_new_table(net, RT6_TABLE_DFLT); |
Sabrina Dubroca | e783bb0 | 2018-06-05 15:02:00 +0200 | [diff] [blame] | 233 | if (IS_ERR(mrt)) { |
| 234 | err = PTR_ERR(mrt); |
Patrick McHardy | d1db275 | 2010-05-11 14:40:55 +0200 | [diff] [blame] | 235 | goto err1; |
| 236 | } |
| 237 | |
| 238 | err = fib_default_rule_add(ops, 0x7fff, RT6_TABLE_DFLT, 0); |
| 239 | if (err < 0) |
| 240 | goto err2; |
| 241 | |
| 242 | net->ipv6.mr6_rules_ops = ops; |
| 243 | return 0; |
| 244 | |
| 245 | err2: |
WANG Cong | f243e5a | 2015-03-25 14:45:03 -0700 | [diff] [blame] | 246 | ip6mr_free_table(mrt); |
Patrick McHardy | d1db275 | 2010-05-11 14:40:55 +0200 | [diff] [blame] | 247 | err1: |
| 248 | fib_rules_unregister(ops); |
| 249 | return err; |
| 250 | } |
| 251 | |
| 252 | static void __net_exit ip6mr_rules_exit(struct net *net) |
| 253 | { |
Yuval Mintz | b70432f | 2018-02-28 23:29:32 +0200 | [diff] [blame] | 254 | struct mr_table *mrt, *next; |
Patrick McHardy | d1db275 | 2010-05-11 14:40:55 +0200 | [diff] [blame] | 255 | |
Hannes Frederic Sowa | 905a6f9 | 2013-07-22 23:45:53 +0200 | [diff] [blame] | 256 | rtnl_lock(); |
Eric Dumazet | 035320d | 2010-06-06 23:48:40 +0000 | [diff] [blame] | 257 | list_for_each_entry_safe(mrt, next, &net->ipv6.mr6_tables, list) { |
| 258 | list_del(&mrt->list); |
Patrick McHardy | d1db275 | 2010-05-11 14:40:55 +0200 | [diff] [blame] | 259 | ip6mr_free_table(mrt); |
Eric Dumazet | 035320d | 2010-06-06 23:48:40 +0000 | [diff] [blame] | 260 | } |
Patrick McHardy | d1db275 | 2010-05-11 14:40:55 +0200 | [diff] [blame] | 261 | fib_rules_unregister(net->ipv6.mr6_rules_ops); |
WANG Cong | 419df12 | 2015-03-31 11:01:46 -0700 | [diff] [blame] | 262 | rtnl_unlock(); |
Patrick McHardy | d1db275 | 2010-05-11 14:40:55 +0200 | [diff] [blame] | 263 | } |
Yuval Mintz | 088aa3e | 2018-03-26 15:01:34 +0300 | [diff] [blame] | 264 | |
Jiri Pirko | b7a5955 | 2019-10-03 11:49:30 +0200 | [diff] [blame] | 265 | static int ip6mr_rules_dump(struct net *net, struct notifier_block *nb, |
| 266 | struct netlink_ext_ack *extack) |
Yuval Mintz | 088aa3e | 2018-03-26 15:01:34 +0300 | [diff] [blame] | 267 | { |
Jiri Pirko | b7a5955 | 2019-10-03 11:49:30 +0200 | [diff] [blame] | 268 | return fib_rules_dump(net, nb, RTNL_FAMILY_IP6MR, extack); |
Yuval Mintz | 088aa3e | 2018-03-26 15:01:34 +0300 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | static unsigned int ip6mr_rules_seq_read(struct net *net) |
| 272 | { |
| 273 | return fib_rules_seq_read(net, RTNL_FAMILY_IP6MR); |
| 274 | } |
Yuval Mintz | d3c07e5 | 2018-03-26 15:01:35 +0300 | [diff] [blame] | 275 | |
| 276 | bool ip6mr_rule_default(const struct fib_rule *rule) |
| 277 | { |
| 278 | return fib_rule_matchall(rule) && rule->action == FR_ACT_TO_TBL && |
| 279 | rule->table == RT6_TABLE_DFLT && !rule->l3mdev; |
| 280 | } |
| 281 | EXPORT_SYMBOL(ip6mr_rule_default); |
Patrick McHardy | d1db275 | 2010-05-11 14:40:55 +0200 | [diff] [blame] | 282 | #else |
| 283 | #define ip6mr_for_each_table(mrt, net) \ |
| 284 | for (mrt = net->ipv6.mrt6; mrt; mrt = NULL) |
| 285 | |
Yuval Mintz | 7b0db85 | 2018-02-28 23:29:39 +0200 | [diff] [blame] | 286 | static struct mr_table *ip6mr_mr_table_iter(struct net *net, |
| 287 | struct mr_table *mrt) |
| 288 | { |
| 289 | if (!mrt) |
| 290 | return net->ipv6.mrt6; |
| 291 | return NULL; |
| 292 | } |
| 293 | |
Yuval Mintz | b70432f | 2018-02-28 23:29:32 +0200 | [diff] [blame] | 294 | static struct mr_table *ip6mr_get_table(struct net *net, u32 id) |
Patrick McHardy | d1db275 | 2010-05-11 14:40:55 +0200 | [diff] [blame] | 295 | { |
| 296 | return net->ipv6.mrt6; |
| 297 | } |
| 298 | |
David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 299 | static int ip6mr_fib_lookup(struct net *net, struct flowi6 *flp6, |
Yuval Mintz | b70432f | 2018-02-28 23:29:32 +0200 | [diff] [blame] | 300 | struct mr_table **mrt) |
Patrick McHardy | d1db275 | 2010-05-11 14:40:55 +0200 | [diff] [blame] | 301 | { |
| 302 | *mrt = net->ipv6.mrt6; |
| 303 | return 0; |
| 304 | } |
| 305 | |
| 306 | static int __net_init ip6mr_rules_init(struct net *net) |
| 307 | { |
Sabrina Dubroca | e783bb0 | 2018-06-05 15:02:00 +0200 | [diff] [blame] | 308 | struct mr_table *mrt; |
| 309 | |
| 310 | mrt = ip6mr_new_table(net, RT6_TABLE_DFLT); |
| 311 | if (IS_ERR(mrt)) |
| 312 | return PTR_ERR(mrt); |
| 313 | net->ipv6.mrt6 = mrt; |
| 314 | return 0; |
Patrick McHardy | d1db275 | 2010-05-11 14:40:55 +0200 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | static void __net_exit ip6mr_rules_exit(struct net *net) |
| 318 | { |
Hannes Frederic Sowa | 905a6f9 | 2013-07-22 23:45:53 +0200 | [diff] [blame] | 319 | rtnl_lock(); |
Patrick McHardy | d1db275 | 2010-05-11 14:40:55 +0200 | [diff] [blame] | 320 | ip6mr_free_table(net->ipv6.mrt6); |
Hannes Frederic Sowa | 905a6f9 | 2013-07-22 23:45:53 +0200 | [diff] [blame] | 321 | net->ipv6.mrt6 = NULL; |
| 322 | rtnl_unlock(); |
Patrick McHardy | d1db275 | 2010-05-11 14:40:55 +0200 | [diff] [blame] | 323 | } |
Yuval Mintz | 088aa3e | 2018-03-26 15:01:34 +0300 | [diff] [blame] | 324 | |
Jiri Pirko | b7a5955 | 2019-10-03 11:49:30 +0200 | [diff] [blame] | 325 | static int ip6mr_rules_dump(struct net *net, struct notifier_block *nb, |
| 326 | struct netlink_ext_ack *extack) |
Yuval Mintz | 088aa3e | 2018-03-26 15:01:34 +0300 | [diff] [blame] | 327 | { |
| 328 | return 0; |
| 329 | } |
| 330 | |
| 331 | static unsigned int ip6mr_rules_seq_read(struct net *net) |
| 332 | { |
| 333 | return 0; |
| 334 | } |
Patrick McHardy | d1db275 | 2010-05-11 14:40:55 +0200 | [diff] [blame] | 335 | #endif |
| 336 | |
Yuval Mintz | 87c418b | 2018-02-28 23:29:31 +0200 | [diff] [blame] | 337 | static int ip6mr_hash_cmp(struct rhashtable_compare_arg *arg, |
| 338 | const void *ptr) |
| 339 | { |
| 340 | const struct mfc6_cache_cmp_arg *cmparg = arg->key; |
| 341 | struct mfc6_cache *c = (struct mfc6_cache *)ptr; |
| 342 | |
| 343 | return !ipv6_addr_equal(&c->mf6c_mcastgrp, &cmparg->mf6c_mcastgrp) || |
| 344 | !ipv6_addr_equal(&c->mf6c_origin, &cmparg->mf6c_origin); |
| 345 | } |
| 346 | |
| 347 | static const struct rhashtable_params ip6mr_rht_params = { |
Yuval Mintz | 494fff5 | 2018-02-28 23:29:34 +0200 | [diff] [blame] | 348 | .head_offset = offsetof(struct mr_mfc, mnode), |
Yuval Mintz | 87c418b | 2018-02-28 23:29:31 +0200 | [diff] [blame] | 349 | .key_offset = offsetof(struct mfc6_cache, cmparg), |
| 350 | .key_len = sizeof(struct mfc6_cache_cmp_arg), |
| 351 | .nelem_hint = 3, |
Yuval Mintz | 87c418b | 2018-02-28 23:29:31 +0200 | [diff] [blame] | 352 | .obj_cmpfn = ip6mr_hash_cmp, |
| 353 | .automatic_shrinking = true, |
| 354 | }; |
| 355 | |
Yuval Mintz | 0bbbf0e | 2018-02-28 23:29:33 +0200 | [diff] [blame] | 356 | static void ip6mr_new_table_set(struct mr_table *mrt, |
| 357 | struct net *net) |
| 358 | { |
| 359 | #ifdef CONFIG_IPV6_MROUTE_MULTIPLE_TABLES |
| 360 | list_add_tail_rcu(&mrt->list, &net->ipv6.mr6_tables); |
| 361 | #endif |
| 362 | } |
| 363 | |
Yuval Mintz | 845c9a7 | 2018-02-28 23:29:35 +0200 | [diff] [blame] | 364 | static struct mfc6_cache_cmp_arg ip6mr_mr_table_ops_cmparg_any = { |
| 365 | .mf6c_origin = IN6ADDR_ANY_INIT, |
| 366 | .mf6c_mcastgrp = IN6ADDR_ANY_INIT, |
| 367 | }; |
| 368 | |
| 369 | static struct mr_table_ops ip6mr_mr_table_ops = { |
| 370 | .rht_params = &ip6mr_rht_params, |
| 371 | .cmparg_any = &ip6mr_mr_table_ops_cmparg_any, |
| 372 | }; |
| 373 | |
Yuval Mintz | b70432f | 2018-02-28 23:29:32 +0200 | [diff] [blame] | 374 | static struct mr_table *ip6mr_new_table(struct net *net, u32 id) |
Patrick McHardy | d1db275 | 2010-05-11 14:40:55 +0200 | [diff] [blame] | 375 | { |
Yuval Mintz | b70432f | 2018-02-28 23:29:32 +0200 | [diff] [blame] | 376 | struct mr_table *mrt; |
Patrick McHardy | d1db275 | 2010-05-11 14:40:55 +0200 | [diff] [blame] | 377 | |
| 378 | mrt = ip6mr_get_table(net, id); |
Ian Morris | 53b24b8 | 2015-03-29 14:00:05 +0100 | [diff] [blame] | 379 | if (mrt) |
Patrick McHardy | d1db275 | 2010-05-11 14:40:55 +0200 | [diff] [blame] | 380 | return mrt; |
| 381 | |
Yuval Mintz | 845c9a7 | 2018-02-28 23:29:35 +0200 | [diff] [blame] | 382 | return mr_table_alloc(net, id, &ip6mr_mr_table_ops, |
Yuval Mintz | 0bbbf0e | 2018-02-28 23:29:33 +0200 | [diff] [blame] | 383 | ipmr_expire_process, ip6mr_new_table_set); |
Patrick McHardy | d1db275 | 2010-05-11 14:40:55 +0200 | [diff] [blame] | 384 | } |
| 385 | |
Yuval Mintz | b70432f | 2018-02-28 23:29:32 +0200 | [diff] [blame] | 386 | static void ip6mr_free_table(struct mr_table *mrt) |
Patrick McHardy | d1db275 | 2010-05-11 14:40:55 +0200 | [diff] [blame] | 387 | { |
WANG Cong | 7ba0c47 | 2015-03-31 11:01:47 -0700 | [diff] [blame] | 388 | del_timer_sync(&mrt->ipmr_expire_timer); |
Callum Sinclair | ca8d479 | 2019-02-18 10:07:52 +1300 | [diff] [blame] | 389 | mroute_clean_tables(mrt, MRT6_FLUSH_MIFS | MRT6_FLUSH_MIFS_STATIC | |
| 390 | MRT6_FLUSH_MFC | MRT6_FLUSH_MFC_STATIC); |
Yuval Mintz | b70432f | 2018-02-28 23:29:32 +0200 | [diff] [blame] | 391 | rhltable_destroy(&mrt->mfc_hash); |
Patrick McHardy | d1db275 | 2010-05-11 14:40:55 +0200 | [diff] [blame] | 392 | kfree(mrt); |
| 393 | } |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 394 | |
| 395 | #ifdef CONFIG_PROC_FS |
Yuval Mintz | c8d6196 | 2018-02-28 23:29:36 +0200 | [diff] [blame] | 396 | /* The /proc interfaces to multicast routing |
| 397 | * /proc/ip6_mr_cache /proc/ip6_mr_vif |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 398 | */ |
| 399 | |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 400 | static void *ip6mr_vif_seq_start(struct seq_file *seq, loff_t *pos) |
| 401 | __acquires(mrt_lock) |
| 402 | { |
Yuval Mintz | 3feda6b | 2018-02-28 23:29:37 +0200 | [diff] [blame] | 403 | struct mr_vif_iter *iter = seq->private; |
Benjamin Thery | 8b90fc7 | 2008-12-10 16:29:48 -0800 | [diff] [blame] | 404 | struct net *net = seq_file_net(seq); |
Yuval Mintz | b70432f | 2018-02-28 23:29:32 +0200 | [diff] [blame] | 405 | struct mr_table *mrt; |
Patrick McHardy | d1db275 | 2010-05-11 14:40:55 +0200 | [diff] [blame] | 406 | |
| 407 | mrt = ip6mr_get_table(net, RT6_TABLE_DFLT); |
Ian Morris | 63159f2 | 2015-03-29 14:00:04 +0100 | [diff] [blame] | 408 | if (!mrt) |
Patrick McHardy | d1db275 | 2010-05-11 14:40:55 +0200 | [diff] [blame] | 409 | return ERR_PTR(-ENOENT); |
| 410 | |
| 411 | iter->mrt = mrt; |
Benjamin Thery | 8b90fc7 | 2008-12-10 16:29:48 -0800 | [diff] [blame] | 412 | |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 413 | read_lock(&mrt_lock); |
Yuval Mintz | 3feda6b | 2018-02-28 23:29:37 +0200 | [diff] [blame] | 414 | return mr_vif_seq_start(seq, pos); |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 415 | } |
| 416 | |
| 417 | static void ip6mr_vif_seq_stop(struct seq_file *seq, void *v) |
| 418 | __releases(mrt_lock) |
| 419 | { |
| 420 | read_unlock(&mrt_lock); |
| 421 | } |
| 422 | |
| 423 | static int ip6mr_vif_seq_show(struct seq_file *seq, void *v) |
| 424 | { |
Yuval Mintz | 3feda6b | 2018-02-28 23:29:37 +0200 | [diff] [blame] | 425 | struct mr_vif_iter *iter = seq->private; |
Yuval Mintz | b70432f | 2018-02-28 23:29:32 +0200 | [diff] [blame] | 426 | struct mr_table *mrt = iter->mrt; |
Benjamin Thery | 8b90fc7 | 2008-12-10 16:29:48 -0800 | [diff] [blame] | 427 | |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 428 | if (v == SEQ_START_TOKEN) { |
| 429 | seq_puts(seq, |
| 430 | "Interface BytesIn PktsIn BytesOut PktsOut Flags\n"); |
| 431 | } else { |
Yuval Mintz | 6853f21 | 2018-02-28 23:29:29 +0200 | [diff] [blame] | 432 | const struct vif_device *vif = v; |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 433 | const char *name = vif->dev ? vif->dev->name : "none"; |
| 434 | |
| 435 | seq_printf(seq, |
Al Viro | d430a22 | 2008-06-02 10:59:02 +0100 | [diff] [blame] | 436 | "%2td %-10s %8ld %7ld %8ld %7ld %05X\n", |
Yuval Mintz | b70432f | 2018-02-28 23:29:32 +0200 | [diff] [blame] | 437 | vif - mrt->vif_table, |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 438 | name, vif->bytes_in, vif->pkt_in, |
| 439 | vif->bytes_out, vif->pkt_out, |
| 440 | vif->flags); |
| 441 | } |
| 442 | return 0; |
| 443 | } |
| 444 | |
Stephen Hemminger | 98147d5 | 2009-09-01 19:25:02 +0000 | [diff] [blame] | 445 | static const struct seq_operations ip6mr_vif_seq_ops = { |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 446 | .start = ip6mr_vif_seq_start, |
Yuval Mintz | 3feda6b | 2018-02-28 23:29:37 +0200 | [diff] [blame] | 447 | .next = mr_vif_seq_next, |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 448 | .stop = ip6mr_vif_seq_stop, |
| 449 | .show = ip6mr_vif_seq_show, |
| 450 | }; |
| 451 | |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 452 | static void *ipmr_mfc_seq_start(struct seq_file *seq, loff_t *pos) |
| 453 | { |
Benjamin Thery | 8b90fc7 | 2008-12-10 16:29:48 -0800 | [diff] [blame] | 454 | struct net *net = seq_file_net(seq); |
Yuval Mintz | b70432f | 2018-02-28 23:29:32 +0200 | [diff] [blame] | 455 | struct mr_table *mrt; |
Benjamin Thery | 8b90fc7 | 2008-12-10 16:29:48 -0800 | [diff] [blame] | 456 | |
Patrick McHardy | d1db275 | 2010-05-11 14:40:55 +0200 | [diff] [blame] | 457 | mrt = ip6mr_get_table(net, RT6_TABLE_DFLT); |
Ian Morris | 63159f2 | 2015-03-29 14:00:04 +0100 | [diff] [blame] | 458 | if (!mrt) |
Patrick McHardy | d1db275 | 2010-05-11 14:40:55 +0200 | [diff] [blame] | 459 | return ERR_PTR(-ENOENT); |
| 460 | |
Yuval Mintz | c8d6196 | 2018-02-28 23:29:36 +0200 | [diff] [blame] | 461 | return mr_mfc_seq_start(seq, pos, mrt, &mfc_unres_lock); |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 462 | } |
| 463 | |
| 464 | static int ipmr_mfc_seq_show(struct seq_file *seq, void *v) |
| 465 | { |
| 466 | int n; |
| 467 | |
| 468 | if (v == SEQ_START_TOKEN) { |
| 469 | seq_puts(seq, |
| 470 | "Group " |
| 471 | "Origin " |
| 472 | "Iif Pkts Bytes Wrong Oifs\n"); |
| 473 | } else { |
| 474 | const struct mfc6_cache *mfc = v; |
Yuval Mintz | c8d6196 | 2018-02-28 23:29:36 +0200 | [diff] [blame] | 475 | const struct mr_mfc_iter *it = seq->private; |
Yuval Mintz | b70432f | 2018-02-28 23:29:32 +0200 | [diff] [blame] | 476 | struct mr_table *mrt = it->mrt; |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 477 | |
Benjamin Thery | 999890b | 2008-12-03 22:22:16 -0800 | [diff] [blame] | 478 | seq_printf(seq, "%pI6 %pI6 %-3hd", |
Harvey Harrison | 0c6ce78 | 2008-10-28 16:09:23 -0700 | [diff] [blame] | 479 | &mfc->mf6c_mcastgrp, &mfc->mf6c_origin, |
Yuval Mintz | 494fff5 | 2018-02-28 23:29:34 +0200 | [diff] [blame] | 480 | mfc->_c.mfc_parent); |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 481 | |
Yuval Mintz | b70432f | 2018-02-28 23:29:32 +0200 | [diff] [blame] | 482 | if (it->cache != &mrt->mfc_unres_queue) { |
Benjamin Thery | 1ea472e | 2008-12-03 22:21:47 -0800 | [diff] [blame] | 483 | seq_printf(seq, " %8lu %8lu %8lu", |
Yuval Mintz | 494fff5 | 2018-02-28 23:29:34 +0200 | [diff] [blame] | 484 | mfc->_c.mfc_un.res.pkt, |
| 485 | mfc->_c.mfc_un.res.bytes, |
| 486 | mfc->_c.mfc_un.res.wrong_if); |
| 487 | for (n = mfc->_c.mfc_un.res.minvif; |
| 488 | n < mfc->_c.mfc_un.res.maxvif; n++) { |
Yuval Mintz | b70432f | 2018-02-28 23:29:32 +0200 | [diff] [blame] | 489 | if (VIF_EXISTS(mrt, n) && |
Yuval Mintz | 494fff5 | 2018-02-28 23:29:34 +0200 | [diff] [blame] | 490 | mfc->_c.mfc_un.res.ttls[n] < 255) |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 491 | seq_printf(seq, |
Yuval Mintz | 494fff5 | 2018-02-28 23:29:34 +0200 | [diff] [blame] | 492 | " %2d:%-3d", n, |
| 493 | mfc->_c.mfc_un.res.ttls[n]); |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 494 | } |
Benjamin Thery | 1ea472e | 2008-12-03 22:21:47 -0800 | [diff] [blame] | 495 | } else { |
| 496 | /* unresolved mfc_caches don't contain |
| 497 | * pkt, bytes and wrong_if values |
| 498 | */ |
| 499 | seq_printf(seq, " %8lu %8lu %8lu", 0ul, 0ul, 0ul); |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 500 | } |
| 501 | seq_putc(seq, '\n'); |
| 502 | } |
| 503 | return 0; |
| 504 | } |
| 505 | |
James Morris | 88e9d34 | 2009-09-22 16:43:43 -0700 | [diff] [blame] | 506 | static const struct seq_operations ipmr_mfc_seq_ops = { |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 507 | .start = ipmr_mfc_seq_start, |
Yuval Mintz | c8d6196 | 2018-02-28 23:29:36 +0200 | [diff] [blame] | 508 | .next = mr_mfc_seq_next, |
| 509 | .stop = mr_mfc_seq_stop, |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 510 | .show = ipmr_mfc_seq_show, |
| 511 | }; |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 512 | #endif |
| 513 | |
YOSHIFUJI Hideaki | 14fb64e | 2008-04-03 09:22:54 +0900 | [diff] [blame] | 514 | #ifdef CONFIG_IPV6_PIMSM_V2 |
YOSHIFUJI Hideaki | 14fb64e | 2008-04-03 09:22:54 +0900 | [diff] [blame] | 515 | |
| 516 | static int pim6_rcv(struct sk_buff *skb) |
| 517 | { |
| 518 | struct pimreghdr *pim; |
| 519 | struct ipv6hdr *encap; |
| 520 | struct net_device *reg_dev = NULL; |
Benjamin Thery | 8229efd | 2008-12-10 16:30:15 -0800 | [diff] [blame] | 521 | struct net *net = dev_net(skb->dev); |
Yuval Mintz | b70432f | 2018-02-28 23:29:32 +0200 | [diff] [blame] | 522 | struct mr_table *mrt; |
David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 523 | struct flowi6 fl6 = { |
| 524 | .flowi6_iif = skb->dev->ifindex, |
| 525 | .flowi6_mark = skb->mark, |
Patrick McHardy | d1db275 | 2010-05-11 14:40:55 +0200 | [diff] [blame] | 526 | }; |
| 527 | int reg_vif_num; |
YOSHIFUJI Hideaki | 14fb64e | 2008-04-03 09:22:54 +0900 | [diff] [blame] | 528 | |
| 529 | if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(*encap))) |
| 530 | goto drop; |
| 531 | |
| 532 | pim = (struct pimreghdr *)skb_transport_header(skb); |
Nikolay Aleksandrov | 56245ca | 2016-10-31 13:21:04 +0100 | [diff] [blame] | 533 | if (pim->type != ((PIM_VERSION << 4) | PIM_TYPE_REGISTER) || |
YOSHIFUJI Hideaki | 14fb64e | 2008-04-03 09:22:54 +0900 | [diff] [blame] | 534 | (pim->flags & PIM_NULL_REGISTER) || |
Thomas Goff | 1d6e55f | 2009-01-27 22:39:59 -0800 | [diff] [blame] | 535 | (csum_ipv6_magic(&ipv6_hdr(skb)->saddr, &ipv6_hdr(skb)->daddr, |
| 536 | sizeof(*pim), IPPROTO_PIM, |
| 537 | csum_partial((void *)pim, sizeof(*pim), 0)) && |
Al Viro | ec6b486 | 2008-04-26 22:28:58 -0700 | [diff] [blame] | 538 | csum_fold(skb_checksum(skb, 0, skb->len, 0)))) |
YOSHIFUJI Hideaki | 14fb64e | 2008-04-03 09:22:54 +0900 | [diff] [blame] | 539 | goto drop; |
| 540 | |
| 541 | /* check if the inner packet is destined to mcast group */ |
| 542 | encap = (struct ipv6hdr *)(skb_transport_header(skb) + |
| 543 | sizeof(*pim)); |
| 544 | |
| 545 | if (!ipv6_addr_is_multicast(&encap->daddr) || |
| 546 | encap->payload_len == 0 || |
| 547 | ntohs(encap->payload_len) + sizeof(*pim) > skb->len) |
| 548 | goto drop; |
| 549 | |
David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 550 | if (ip6mr_fib_lookup(net, &fl6, &mrt) < 0) |
Patrick McHardy | d1db275 | 2010-05-11 14:40:55 +0200 | [diff] [blame] | 551 | goto drop; |
| 552 | reg_vif_num = mrt->mroute_reg_vif_num; |
| 553 | |
YOSHIFUJI Hideaki | 14fb64e | 2008-04-03 09:22:54 +0900 | [diff] [blame] | 554 | read_lock(&mrt_lock); |
| 555 | if (reg_vif_num >= 0) |
Yuval Mintz | b70432f | 2018-02-28 23:29:32 +0200 | [diff] [blame] | 556 | reg_dev = mrt->vif_table[reg_vif_num].dev; |
Yajun Deng | 1160dfa | 2021-08-05 19:55:27 +0800 | [diff] [blame] | 557 | dev_hold(reg_dev); |
YOSHIFUJI Hideaki | 14fb64e | 2008-04-03 09:22:54 +0900 | [diff] [blame] | 558 | read_unlock(&mrt_lock); |
| 559 | |
Ian Morris | 63159f2 | 2015-03-29 14:00:04 +0100 | [diff] [blame] | 560 | if (!reg_dev) |
YOSHIFUJI Hideaki | 14fb64e | 2008-04-03 09:22:54 +0900 | [diff] [blame] | 561 | goto drop; |
| 562 | |
| 563 | skb->mac_header = skb->network_header; |
| 564 | skb_pull(skb, (u8 *)encap - skb->data); |
| 565 | skb_reset_network_header(skb); |
Thomas Goff | 1d6e55f | 2009-01-27 22:39:59 -0800 | [diff] [blame] | 566 | skb->protocol = htons(ETH_P_IPV6); |
Cesar Eduardo Barros | 3e49e6d | 2011-03-26 05:10:30 +0000 | [diff] [blame] | 567 | skb->ip_summed = CHECKSUM_NONE; |
Eric Dumazet | d19d56d | 2010-05-17 22:36:55 -0700 | [diff] [blame] | 568 | |
Nicolas Dichtel | ea23192 | 2013-09-02 15:34:58 +0200 | [diff] [blame] | 569 | skb_tunnel_rx(skb, reg_dev, dev_net(reg_dev)); |
Eric Dumazet | d19d56d | 2010-05-17 22:36:55 -0700 | [diff] [blame] | 570 | |
Eric Dumazet | caf586e | 2010-09-30 21:06:55 +0000 | [diff] [blame] | 571 | netif_rx(skb); |
Eric Dumazet | 8990f46 | 2010-09-20 00:12:11 +0000 | [diff] [blame] | 572 | |
YOSHIFUJI Hideaki | 14fb64e | 2008-04-03 09:22:54 +0900 | [diff] [blame] | 573 | dev_put(reg_dev); |
| 574 | return 0; |
| 575 | drop: |
| 576 | kfree_skb(skb); |
| 577 | return 0; |
| 578 | } |
| 579 | |
Alexey Dobriyan | 41135cc | 2009-09-14 12:22:28 +0000 | [diff] [blame] | 580 | static const struct inet6_protocol pim6_protocol = { |
YOSHIFUJI Hideaki | 14fb64e | 2008-04-03 09:22:54 +0900 | [diff] [blame] | 581 | .handler = pim6_rcv, |
| 582 | }; |
| 583 | |
| 584 | /* Service routines creating virtual interfaces: PIMREG */ |
| 585 | |
Stephen Hemminger | 6fef4c0 | 2009-08-31 19:50:41 +0000 | [diff] [blame] | 586 | static netdev_tx_t reg_vif_xmit(struct sk_buff *skb, |
| 587 | struct net_device *dev) |
YOSHIFUJI Hideaki | 14fb64e | 2008-04-03 09:22:54 +0900 | [diff] [blame] | 588 | { |
Benjamin Thery | 8229efd | 2008-12-10 16:30:15 -0800 | [diff] [blame] | 589 | struct net *net = dev_net(dev); |
Yuval Mintz | b70432f | 2018-02-28 23:29:32 +0200 | [diff] [blame] | 590 | struct mr_table *mrt; |
David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 591 | struct flowi6 fl6 = { |
| 592 | .flowi6_oif = dev->ifindex, |
Cong Wang | 6a66271 | 2014-04-15 16:25:34 -0700 | [diff] [blame] | 593 | .flowi6_iif = skb->skb_iif ? : LOOPBACK_IFINDEX, |
David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 594 | .flowi6_mark = skb->mark, |
Patrick McHardy | d1db275 | 2010-05-11 14:40:55 +0200 | [diff] [blame] | 595 | }; |
Patrick McHardy | d1db275 | 2010-05-11 14:40:55 +0200 | [diff] [blame] | 596 | |
Willem de Bruijn | cb9f1b7 | 2018-12-30 17:24:36 -0500 | [diff] [blame] | 597 | if (!pskb_inet_may_pull(skb)) |
| 598 | goto tx_err; |
| 599 | |
| 600 | if (ip6mr_fib_lookup(net, &fl6, &mrt) < 0) |
| 601 | goto tx_err; |
Benjamin Thery | 8229efd | 2008-12-10 16:30:15 -0800 | [diff] [blame] | 602 | |
YOSHIFUJI Hideaki | 14fb64e | 2008-04-03 09:22:54 +0900 | [diff] [blame] | 603 | read_lock(&mrt_lock); |
Pavel Emelyanov | dc58c78 | 2008-05-21 14:17:54 -0700 | [diff] [blame] | 604 | dev->stats.tx_bytes += skb->len; |
| 605 | dev->stats.tx_packets++; |
Patrick McHardy | 6bd5214 | 2010-05-11 14:40:53 +0200 | [diff] [blame] | 606 | ip6mr_cache_report(mrt, skb, mrt->mroute_reg_vif_num, MRT6MSG_WHOLEPKT); |
YOSHIFUJI Hideaki | 14fb64e | 2008-04-03 09:22:54 +0900 | [diff] [blame] | 607 | read_unlock(&mrt_lock); |
| 608 | kfree_skb(skb); |
Patrick McHardy | 6ed1065 | 2009-06-23 06:03:08 +0000 | [diff] [blame] | 609 | return NETDEV_TX_OK; |
Willem de Bruijn | cb9f1b7 | 2018-12-30 17:24:36 -0500 | [diff] [blame] | 610 | |
| 611 | tx_err: |
| 612 | dev->stats.tx_errors++; |
| 613 | kfree_skb(skb); |
| 614 | return NETDEV_TX_OK; |
YOSHIFUJI Hideaki | 14fb64e | 2008-04-03 09:22:54 +0900 | [diff] [blame] | 615 | } |
| 616 | |
Nicolas Dichtel | ee9b959 | 2015-04-02 17:07:03 +0200 | [diff] [blame] | 617 | static int reg_vif_get_iflink(const struct net_device *dev) |
| 618 | { |
| 619 | return 0; |
| 620 | } |
| 621 | |
Stephen Hemminger | 007c383 | 2008-11-20 20:28:35 -0800 | [diff] [blame] | 622 | static const struct net_device_ops reg_vif_netdev_ops = { |
| 623 | .ndo_start_xmit = reg_vif_xmit, |
Nicolas Dichtel | ee9b959 | 2015-04-02 17:07:03 +0200 | [diff] [blame] | 624 | .ndo_get_iflink = reg_vif_get_iflink, |
Stephen Hemminger | 007c383 | 2008-11-20 20:28:35 -0800 | [diff] [blame] | 625 | }; |
| 626 | |
YOSHIFUJI Hideaki | 14fb64e | 2008-04-03 09:22:54 +0900 | [diff] [blame] | 627 | static void reg_vif_setup(struct net_device *dev) |
| 628 | { |
| 629 | dev->type = ARPHRD_PIMREG; |
| 630 | dev->mtu = 1500 - sizeof(struct ipv6hdr) - 8; |
| 631 | dev->flags = IFF_NOARP; |
Stephen Hemminger | 007c383 | 2008-11-20 20:28:35 -0800 | [diff] [blame] | 632 | dev->netdev_ops = ®_vif_netdev_ops; |
David S. Miller | cf124db | 2017-05-08 12:52:56 -0400 | [diff] [blame] | 633 | dev->needs_free_netdev = true; |
Tom Goff | 403dbb9 | 2009-06-14 03:16:13 -0700 | [diff] [blame] | 634 | dev->features |= NETIF_F_NETNS_LOCAL; |
YOSHIFUJI Hideaki | 14fb64e | 2008-04-03 09:22:54 +0900 | [diff] [blame] | 635 | } |
| 636 | |
Yuval Mintz | b70432f | 2018-02-28 23:29:32 +0200 | [diff] [blame] | 637 | static struct net_device *ip6mr_reg_vif(struct net *net, struct mr_table *mrt) |
YOSHIFUJI Hideaki | 14fb64e | 2008-04-03 09:22:54 +0900 | [diff] [blame] | 638 | { |
| 639 | struct net_device *dev; |
Patrick McHardy | d1db275 | 2010-05-11 14:40:55 +0200 | [diff] [blame] | 640 | char name[IFNAMSIZ]; |
YOSHIFUJI Hideaki | 14fb64e | 2008-04-03 09:22:54 +0900 | [diff] [blame] | 641 | |
Patrick McHardy | d1db275 | 2010-05-11 14:40:55 +0200 | [diff] [blame] | 642 | if (mrt->id == RT6_TABLE_DFLT) |
| 643 | sprintf(name, "pim6reg"); |
| 644 | else |
| 645 | sprintf(name, "pim6reg%u", mrt->id); |
| 646 | |
Tom Gundersen | c835a67 | 2014-07-14 16:37:24 +0200 | [diff] [blame] | 647 | dev = alloc_netdev(0, name, NET_NAME_UNKNOWN, reg_vif_setup); |
Ian Morris | 63159f2 | 2015-03-29 14:00:04 +0100 | [diff] [blame] | 648 | if (!dev) |
YOSHIFUJI Hideaki | 14fb64e | 2008-04-03 09:22:54 +0900 | [diff] [blame] | 649 | return NULL; |
| 650 | |
Benjamin Thery | 8229efd | 2008-12-10 16:30:15 -0800 | [diff] [blame] | 651 | dev_net_set(dev, net); |
| 652 | |
YOSHIFUJI Hideaki | 14fb64e | 2008-04-03 09:22:54 +0900 | [diff] [blame] | 653 | if (register_netdevice(dev)) { |
| 654 | free_netdev(dev); |
| 655 | return NULL; |
| 656 | } |
YOSHIFUJI Hideaki | 14fb64e | 2008-04-03 09:22:54 +0900 | [diff] [blame] | 657 | |
Petr Machata | 00f54e6 | 2018-12-06 17:05:36 +0000 | [diff] [blame] | 658 | if (dev_open(dev, NULL)) |
YOSHIFUJI Hideaki | 14fb64e | 2008-04-03 09:22:54 +0900 | [diff] [blame] | 659 | goto failure; |
| 660 | |
Wang Chen | 7af3db7 | 2008-07-14 20:54:54 -0700 | [diff] [blame] | 661 | dev_hold(dev); |
YOSHIFUJI Hideaki | 14fb64e | 2008-04-03 09:22:54 +0900 | [diff] [blame] | 662 | return dev; |
| 663 | |
| 664 | failure: |
YOSHIFUJI Hideaki | 14fb64e | 2008-04-03 09:22:54 +0900 | [diff] [blame] | 665 | unregister_netdevice(dev); |
| 666 | return NULL; |
| 667 | } |
| 668 | #endif |
| 669 | |
Yuval Mintz | 088aa3e | 2018-03-26 15:01:34 +0300 | [diff] [blame] | 670 | static int call_ip6mr_vif_entry_notifiers(struct net *net, |
| 671 | enum fib_event_type event_type, |
| 672 | struct vif_device *vif, |
| 673 | mifi_t vif_index, u32 tb_id) |
| 674 | { |
| 675 | return mr_call_vif_notifiers(net, RTNL_FAMILY_IP6MR, event_type, |
| 676 | vif, vif_index, tb_id, |
| 677 | &net->ipv6.ipmr_seq); |
| 678 | } |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 679 | |
Yuval Mintz | 088aa3e | 2018-03-26 15:01:34 +0300 | [diff] [blame] | 680 | static int call_ip6mr_mfc_entry_notifiers(struct net *net, |
| 681 | enum fib_event_type event_type, |
| 682 | struct mfc6_cache *mfc, u32 tb_id) |
| 683 | { |
| 684 | return mr_call_mfc_notifiers(net, RTNL_FAMILY_IP6MR, event_type, |
| 685 | &mfc->_c, tb_id, &net->ipv6.ipmr_seq); |
| 686 | } |
| 687 | |
| 688 | /* Delete a VIF entry */ |
Yuval Mintz | b70432f | 2018-02-28 23:29:32 +0200 | [diff] [blame] | 689 | static int mif6_delete(struct mr_table *mrt, int vifi, int notify, |
Nikolay Aleksandrov | 723b929 | 2017-04-21 20:42:16 +0300 | [diff] [blame] | 690 | struct list_head *head) |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 691 | { |
Yuval Mintz | 6853f21 | 2018-02-28 23:29:29 +0200 | [diff] [blame] | 692 | struct vif_device *v; |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 693 | struct net_device *dev; |
Thomas Goff | 1d6e55f | 2009-01-27 22:39:59 -0800 | [diff] [blame] | 694 | struct inet6_dev *in6_dev; |
Patrick McHardy | 6bd5214 | 2010-05-11 14:40:53 +0200 | [diff] [blame] | 695 | |
| 696 | if (vifi < 0 || vifi >= mrt->maxvif) |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 697 | return -EADDRNOTAVAIL; |
| 698 | |
Yuval Mintz | b70432f | 2018-02-28 23:29:32 +0200 | [diff] [blame] | 699 | v = &mrt->vif_table[vifi]; |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 700 | |
Yuval Mintz | 088aa3e | 2018-03-26 15:01:34 +0300 | [diff] [blame] | 701 | if (VIF_EXISTS(mrt, vifi)) |
| 702 | call_ip6mr_vif_entry_notifiers(read_pnet(&mrt->net), |
| 703 | FIB_EVENT_VIF_DEL, v, vifi, |
| 704 | mrt->id); |
| 705 | |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 706 | write_lock_bh(&mrt_lock); |
| 707 | dev = v->dev; |
| 708 | v->dev = NULL; |
| 709 | |
| 710 | if (!dev) { |
| 711 | write_unlock_bh(&mrt_lock); |
| 712 | return -EADDRNOTAVAIL; |
| 713 | } |
| 714 | |
YOSHIFUJI Hideaki | 14fb64e | 2008-04-03 09:22:54 +0900 | [diff] [blame] | 715 | #ifdef CONFIG_IPV6_PIMSM_V2 |
Patrick McHardy | 6bd5214 | 2010-05-11 14:40:53 +0200 | [diff] [blame] | 716 | if (vifi == mrt->mroute_reg_vif_num) |
| 717 | mrt->mroute_reg_vif_num = -1; |
YOSHIFUJI Hideaki | 14fb64e | 2008-04-03 09:22:54 +0900 | [diff] [blame] | 718 | #endif |
| 719 | |
Patrick McHardy | 6bd5214 | 2010-05-11 14:40:53 +0200 | [diff] [blame] | 720 | if (vifi + 1 == mrt->maxvif) { |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 721 | int tmp; |
| 722 | for (tmp = vifi - 1; tmp >= 0; tmp--) { |
Yuval Mintz | b70432f | 2018-02-28 23:29:32 +0200 | [diff] [blame] | 723 | if (VIF_EXISTS(mrt, tmp)) |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 724 | break; |
| 725 | } |
Patrick McHardy | 6bd5214 | 2010-05-11 14:40:53 +0200 | [diff] [blame] | 726 | mrt->maxvif = tmp + 1; |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 727 | } |
| 728 | |
| 729 | write_unlock_bh(&mrt_lock); |
| 730 | |
| 731 | dev_set_allmulti(dev, -1); |
| 732 | |
Thomas Goff | 1d6e55f | 2009-01-27 22:39:59 -0800 | [diff] [blame] | 733 | in6_dev = __in6_dev_get(dev); |
Nicolas Dichtel | d67b8c6 | 2012-12-04 01:13:35 +0000 | [diff] [blame] | 734 | if (in6_dev) { |
Thomas Goff | 1d6e55f | 2009-01-27 22:39:59 -0800 | [diff] [blame] | 735 | in6_dev->cnf.mc_forwarding--; |
David Ahern | 85b3daa | 2017-03-28 14:28:04 -0700 | [diff] [blame] | 736 | inet6_netconf_notify_devconf(dev_net(dev), RTM_NEWNETCONF, |
Nicolas Dichtel | d67b8c6 | 2012-12-04 01:13:35 +0000 | [diff] [blame] | 737 | NETCONFA_MC_FORWARDING, |
| 738 | dev->ifindex, &in6_dev->cnf); |
| 739 | } |
Thomas Goff | 1d6e55f | 2009-01-27 22:39:59 -0800 | [diff] [blame] | 740 | |
Nikolay Aleksandrov | 723b929 | 2017-04-21 20:42:16 +0300 | [diff] [blame] | 741 | if ((v->flags & MIFF_REGISTER) && !notify) |
Eric Dumazet | c871e66 | 2009-10-28 04:48:11 +0000 | [diff] [blame] | 742 | unregister_netdevice_queue(dev, head); |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 743 | |
Eric Dumazet | 42120a8 | 2021-12-04 20:22:16 -0800 | [diff] [blame] | 744 | dev_put_track(dev, &v->dev_tracker); |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 745 | return 0; |
| 746 | } |
| 747 | |
Yuval Mintz | 87c418b | 2018-02-28 23:29:31 +0200 | [diff] [blame] | 748 | static inline void ip6mr_cache_free_rcu(struct rcu_head *head) |
| 749 | { |
Yuval Mintz | 494fff5 | 2018-02-28 23:29:34 +0200 | [diff] [blame] | 750 | struct mr_mfc *c = container_of(head, struct mr_mfc, rcu); |
Yuval Mintz | 87c418b | 2018-02-28 23:29:31 +0200 | [diff] [blame] | 751 | |
Yuval Mintz | 494fff5 | 2018-02-28 23:29:34 +0200 | [diff] [blame] | 752 | kmem_cache_free(mrt_cachep, (struct mfc6_cache *)c); |
Yuval Mintz | 87c418b | 2018-02-28 23:29:31 +0200 | [diff] [blame] | 753 | } |
| 754 | |
Benjamin Thery | 58701ad | 2008-12-10 16:22:34 -0800 | [diff] [blame] | 755 | static inline void ip6mr_cache_free(struct mfc6_cache *c) |
| 756 | { |
Yuval Mintz | 494fff5 | 2018-02-28 23:29:34 +0200 | [diff] [blame] | 757 | call_rcu(&c->_c.rcu, ip6mr_cache_free_rcu); |
Benjamin Thery | 58701ad | 2008-12-10 16:22:34 -0800 | [diff] [blame] | 758 | } |
| 759 | |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 760 | /* Destroy an unresolved cache entry, killing queued skbs |
| 761 | and reporting error to netlink readers. |
| 762 | */ |
| 763 | |
Yuval Mintz | b70432f | 2018-02-28 23:29:32 +0200 | [diff] [blame] | 764 | static void ip6mr_destroy_unres(struct mr_table *mrt, struct mfc6_cache *c) |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 765 | { |
Patrick McHardy | 6bd5214 | 2010-05-11 14:40:53 +0200 | [diff] [blame] | 766 | struct net *net = read_pnet(&mrt->net); |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 767 | struct sk_buff *skb; |
| 768 | |
Patrick McHardy | 6bd5214 | 2010-05-11 14:40:53 +0200 | [diff] [blame] | 769 | atomic_dec(&mrt->cache_resolve_queue_len); |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 770 | |
Yuval Mintz | 494fff5 | 2018-02-28 23:29:34 +0200 | [diff] [blame] | 771 | while ((skb = skb_dequeue(&c->_c.mfc_un.unres.unresolved)) != NULL) { |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 772 | if (ipv6_hdr(skb)->version == 0) { |
Johannes Berg | af72868 | 2017-06-16 14:29:22 +0200 | [diff] [blame] | 773 | struct nlmsghdr *nlh = skb_pull(skb, |
| 774 | sizeof(struct ipv6hdr)); |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 775 | nlh->nlmsg_type = NLMSG_ERROR; |
Hong zhi guo | 573ce26 | 2013-03-27 06:47:04 +0000 | [diff] [blame] | 776 | nlh->nlmsg_len = nlmsg_msg_size(sizeof(struct nlmsgerr)); |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 777 | skb_trim(skb, nlh->nlmsg_len); |
Hong zhi guo | 573ce26 | 2013-03-27 06:47:04 +0000 | [diff] [blame] | 778 | ((struct nlmsgerr *)nlmsg_data(nlh))->error = -ETIMEDOUT; |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 779 | rtnl_unicast(skb, net, NETLINK_CB(skb).portid); |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 780 | } else |
| 781 | kfree_skb(skb); |
| 782 | } |
| 783 | |
Benjamin Thery | 58701ad | 2008-12-10 16:22:34 -0800 | [diff] [blame] | 784 | ip6mr_cache_free(c); |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 785 | } |
| 786 | |
| 787 | |
Patrick McHardy | c476efb | 2010-05-11 14:40:48 +0200 | [diff] [blame] | 788 | /* Timer process for all the unresolved queue. */ |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 789 | |
Yuval Mintz | b70432f | 2018-02-28 23:29:32 +0200 | [diff] [blame] | 790 | static void ipmr_do_expire_process(struct mr_table *mrt) |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 791 | { |
| 792 | unsigned long now = jiffies; |
| 793 | unsigned long expires = 10 * HZ; |
Yuval Mintz | 494fff5 | 2018-02-28 23:29:34 +0200 | [diff] [blame] | 794 | struct mr_mfc *c, *next; |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 795 | |
Yuval Mintz | b70432f | 2018-02-28 23:29:32 +0200 | [diff] [blame] | 796 | list_for_each_entry_safe(c, next, &mrt->mfc_unres_queue, list) { |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 797 | if (time_after(c->mfc_un.unres.expires, now)) { |
| 798 | /* not yet... */ |
| 799 | unsigned long interval = c->mfc_un.unres.expires - now; |
| 800 | if (interval < expires) |
| 801 | expires = interval; |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 802 | continue; |
| 803 | } |
| 804 | |
Patrick McHardy | f30a7784 | 2010-05-11 14:40:51 +0200 | [diff] [blame] | 805 | list_del(&c->list); |
Yuval Mintz | 494fff5 | 2018-02-28 23:29:34 +0200 | [diff] [blame] | 806 | mr6_netlink_event(mrt, (struct mfc6_cache *)c, RTM_DELROUTE); |
| 807 | ip6mr_destroy_unres(mrt, (struct mfc6_cache *)c); |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 808 | } |
| 809 | |
Yuval Mintz | b70432f | 2018-02-28 23:29:32 +0200 | [diff] [blame] | 810 | if (!list_empty(&mrt->mfc_unres_queue)) |
Patrick McHardy | 6bd5214 | 2010-05-11 14:40:53 +0200 | [diff] [blame] | 811 | mod_timer(&mrt->ipmr_expire_timer, jiffies + expires); |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 812 | } |
| 813 | |
Kees Cook | e99e88a | 2017-10-16 14:43:17 -0700 | [diff] [blame] | 814 | static void ipmr_expire_process(struct timer_list *t) |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 815 | { |
Yuval Mintz | b70432f | 2018-02-28 23:29:32 +0200 | [diff] [blame] | 816 | struct mr_table *mrt = from_timer(mrt, t, ipmr_expire_timer); |
Patrick McHardy | c476efb | 2010-05-11 14:40:48 +0200 | [diff] [blame] | 817 | |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 818 | if (!spin_trylock(&mfc_unres_lock)) { |
Patrick McHardy | 6bd5214 | 2010-05-11 14:40:53 +0200 | [diff] [blame] | 819 | mod_timer(&mrt->ipmr_expire_timer, jiffies + 1); |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 820 | return; |
| 821 | } |
| 822 | |
Yuval Mintz | b70432f | 2018-02-28 23:29:32 +0200 | [diff] [blame] | 823 | if (!list_empty(&mrt->mfc_unres_queue)) |
Patrick McHardy | 6bd5214 | 2010-05-11 14:40:53 +0200 | [diff] [blame] | 824 | ipmr_do_expire_process(mrt); |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 825 | |
| 826 | spin_unlock(&mfc_unres_lock); |
| 827 | } |
| 828 | |
| 829 | /* Fill oifs list. It is called under write locked mrt_lock. */ |
| 830 | |
Yuval Mintz | b70432f | 2018-02-28 23:29:32 +0200 | [diff] [blame] | 831 | static void ip6mr_update_thresholds(struct mr_table *mrt, |
Yuval Mintz | 494fff5 | 2018-02-28 23:29:34 +0200 | [diff] [blame] | 832 | struct mr_mfc *cache, |
Patrick McHardy | b5aa30b | 2010-05-11 14:40:50 +0200 | [diff] [blame] | 833 | unsigned char *ttls) |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 834 | { |
| 835 | int vifi; |
| 836 | |
Rami Rosen | 6ac7eb0 | 2008-04-10 12:40:10 +0300 | [diff] [blame] | 837 | cache->mfc_un.res.minvif = MAXMIFS; |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 838 | cache->mfc_un.res.maxvif = 0; |
Rami Rosen | 6ac7eb0 | 2008-04-10 12:40:10 +0300 | [diff] [blame] | 839 | memset(cache->mfc_un.res.ttls, 255, MAXMIFS); |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 840 | |
Patrick McHardy | 6bd5214 | 2010-05-11 14:40:53 +0200 | [diff] [blame] | 841 | for (vifi = 0; vifi < mrt->maxvif; vifi++) { |
Yuval Mintz | b70432f | 2018-02-28 23:29:32 +0200 | [diff] [blame] | 842 | if (VIF_EXISTS(mrt, vifi) && |
Benjamin Thery | 4e16880 | 2008-12-10 16:15:08 -0800 | [diff] [blame] | 843 | ttls[vifi] && ttls[vifi] < 255) { |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 844 | cache->mfc_un.res.ttls[vifi] = ttls[vifi]; |
| 845 | if (cache->mfc_un.res.minvif > vifi) |
| 846 | cache->mfc_un.res.minvif = vifi; |
| 847 | if (cache->mfc_un.res.maxvif <= vifi) |
| 848 | cache->mfc_un.res.maxvif = vifi + 1; |
| 849 | } |
| 850 | } |
Nikolay Aleksandrov | 90b5ca1 | 2016-07-26 18:54:52 +0200 | [diff] [blame] | 851 | cache->mfc_un.res.lastuse = jiffies; |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 852 | } |
| 853 | |
Yuval Mintz | b70432f | 2018-02-28 23:29:32 +0200 | [diff] [blame] | 854 | static int mif6_add(struct net *net, struct mr_table *mrt, |
Patrick McHardy | 6bd5214 | 2010-05-11 14:40:53 +0200 | [diff] [blame] | 855 | struct mif6ctl *vifc, int mrtsock) |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 856 | { |
| 857 | int vifi = vifc->mif6c_mifi; |
Yuval Mintz | b70432f | 2018-02-28 23:29:32 +0200 | [diff] [blame] | 858 | struct vif_device *v = &mrt->vif_table[vifi]; |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 859 | struct net_device *dev; |
Thomas Goff | 1d6e55f | 2009-01-27 22:39:59 -0800 | [diff] [blame] | 860 | struct inet6_dev *in6_dev; |
Wang Chen | 5ae7b44 | 2008-07-14 20:54:23 -0700 | [diff] [blame] | 861 | int err; |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 862 | |
| 863 | /* Is vif busy ? */ |
Yuval Mintz | b70432f | 2018-02-28 23:29:32 +0200 | [diff] [blame] | 864 | if (VIF_EXISTS(mrt, vifi)) |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 865 | return -EADDRINUSE; |
| 866 | |
| 867 | switch (vifc->mif6c_flags) { |
YOSHIFUJI Hideaki | 14fb64e | 2008-04-03 09:22:54 +0900 | [diff] [blame] | 868 | #ifdef CONFIG_IPV6_PIMSM_V2 |
| 869 | case MIFF_REGISTER: |
| 870 | /* |
| 871 | * Special Purpose VIF in PIM |
| 872 | * All the packets will be sent to the daemon |
| 873 | */ |
Patrick McHardy | 6bd5214 | 2010-05-11 14:40:53 +0200 | [diff] [blame] | 874 | if (mrt->mroute_reg_vif_num >= 0) |
YOSHIFUJI Hideaki | 14fb64e | 2008-04-03 09:22:54 +0900 | [diff] [blame] | 875 | return -EADDRINUSE; |
Patrick McHardy | d1db275 | 2010-05-11 14:40:55 +0200 | [diff] [blame] | 876 | dev = ip6mr_reg_vif(net, mrt); |
YOSHIFUJI Hideaki | 14fb64e | 2008-04-03 09:22:54 +0900 | [diff] [blame] | 877 | if (!dev) |
| 878 | return -ENOBUFS; |
Wang Chen | 5ae7b44 | 2008-07-14 20:54:23 -0700 | [diff] [blame] | 879 | err = dev_set_allmulti(dev, 1); |
| 880 | if (err) { |
| 881 | unregister_netdevice(dev); |
Wang Chen | 7af3db7 | 2008-07-14 20:54:54 -0700 | [diff] [blame] | 882 | dev_put(dev); |
Wang Chen | 5ae7b44 | 2008-07-14 20:54:23 -0700 | [diff] [blame] | 883 | return err; |
| 884 | } |
YOSHIFUJI Hideaki | 14fb64e | 2008-04-03 09:22:54 +0900 | [diff] [blame] | 885 | break; |
| 886 | #endif |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 887 | case 0: |
Benjamin Thery | 8229efd | 2008-12-10 16:30:15 -0800 | [diff] [blame] | 888 | dev = dev_get_by_index(net, vifc->mif6c_pifi); |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 889 | if (!dev) |
| 890 | return -EADDRNOTAVAIL; |
Wang Chen | 5ae7b44 | 2008-07-14 20:54:23 -0700 | [diff] [blame] | 891 | err = dev_set_allmulti(dev, 1); |
Wang Chen | 7af3db7 | 2008-07-14 20:54:54 -0700 | [diff] [blame] | 892 | if (err) { |
| 893 | dev_put(dev); |
Wang Chen | 5ae7b44 | 2008-07-14 20:54:23 -0700 | [diff] [blame] | 894 | return err; |
Wang Chen | 7af3db7 | 2008-07-14 20:54:54 -0700 | [diff] [blame] | 895 | } |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 896 | break; |
| 897 | default: |
| 898 | return -EINVAL; |
| 899 | } |
| 900 | |
Thomas Goff | 1d6e55f | 2009-01-27 22:39:59 -0800 | [diff] [blame] | 901 | in6_dev = __in6_dev_get(dev); |
Nicolas Dichtel | d67b8c6 | 2012-12-04 01:13:35 +0000 | [diff] [blame] | 902 | if (in6_dev) { |
Thomas Goff | 1d6e55f | 2009-01-27 22:39:59 -0800 | [diff] [blame] | 903 | in6_dev->cnf.mc_forwarding++; |
David Ahern | 85b3daa | 2017-03-28 14:28:04 -0700 | [diff] [blame] | 904 | inet6_netconf_notify_devconf(dev_net(dev), RTM_NEWNETCONF, |
Nicolas Dichtel | d67b8c6 | 2012-12-04 01:13:35 +0000 | [diff] [blame] | 905 | NETCONFA_MC_FORWARDING, |
| 906 | dev->ifindex, &in6_dev->cnf); |
| 907 | } |
Thomas Goff | 1d6e55f | 2009-01-27 22:39:59 -0800 | [diff] [blame] | 908 | |
Yuval Mintz | 6853f21 | 2018-02-28 23:29:29 +0200 | [diff] [blame] | 909 | /* Fill in the VIF structures */ |
| 910 | vif_device_init(v, dev, vifc->vifc_rate_limit, vifc->vifc_threshold, |
| 911 | vifc->mif6c_flags | (!mrtsock ? VIFF_STATIC : 0), |
| 912 | MIFF_REGISTER); |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 913 | |
| 914 | /* And finish update writing critical data */ |
| 915 | write_lock_bh(&mrt_lock); |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 916 | v->dev = dev; |
Eric Dumazet | 42120a8 | 2021-12-04 20:22:16 -0800 | [diff] [blame] | 917 | netdev_tracker_alloc(dev, &v->dev_tracker, GFP_ATOMIC); |
YOSHIFUJI Hideaki | 14fb64e | 2008-04-03 09:22:54 +0900 | [diff] [blame] | 918 | #ifdef CONFIG_IPV6_PIMSM_V2 |
| 919 | if (v->flags & MIFF_REGISTER) |
Patrick McHardy | 6bd5214 | 2010-05-11 14:40:53 +0200 | [diff] [blame] | 920 | mrt->mroute_reg_vif_num = vifi; |
YOSHIFUJI Hideaki | 14fb64e | 2008-04-03 09:22:54 +0900 | [diff] [blame] | 921 | #endif |
Patrick McHardy | 6bd5214 | 2010-05-11 14:40:53 +0200 | [diff] [blame] | 922 | if (vifi + 1 > mrt->maxvif) |
| 923 | mrt->maxvif = vifi + 1; |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 924 | write_unlock_bh(&mrt_lock); |
Yuval Mintz | 088aa3e | 2018-03-26 15:01:34 +0300 | [diff] [blame] | 925 | call_ip6mr_vif_entry_notifiers(net, FIB_EVENT_VIF_ADD, |
| 926 | v, vifi, mrt->id); |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 927 | return 0; |
| 928 | } |
| 929 | |
Yuval Mintz | b70432f | 2018-02-28 23:29:32 +0200 | [diff] [blame] | 930 | static struct mfc6_cache *ip6mr_cache_find(struct mr_table *mrt, |
Eric Dumazet | b71d1d4 | 2011-04-22 04:53:02 +0000 | [diff] [blame] | 931 | const struct in6_addr *origin, |
| 932 | const struct in6_addr *mcastgrp) |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 933 | { |
Yuval Mintz | 87c418b | 2018-02-28 23:29:31 +0200 | [diff] [blame] | 934 | struct mfc6_cache_cmp_arg arg = { |
| 935 | .mf6c_origin = *origin, |
| 936 | .mf6c_mcastgrp = *mcastgrp, |
| 937 | }; |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 938 | |
Yuval Mintz | 845c9a7 | 2018-02-28 23:29:35 +0200 | [diff] [blame] | 939 | return mr_mfc_find(mrt, &arg); |
Nicolas Dichtel | 660b26d | 2013-01-21 06:00:26 +0000 | [diff] [blame] | 940 | } |
| 941 | |
| 942 | /* Look for a (*,G) entry */ |
Yuval Mintz | b70432f | 2018-02-28 23:29:32 +0200 | [diff] [blame] | 943 | static struct mfc6_cache *ip6mr_cache_find_any(struct mr_table *mrt, |
Nicolas Dichtel | 660b26d | 2013-01-21 06:00:26 +0000 | [diff] [blame] | 944 | struct in6_addr *mcastgrp, |
| 945 | mifi_t mifi) |
| 946 | { |
Yuval Mintz | 87c418b | 2018-02-28 23:29:31 +0200 | [diff] [blame] | 947 | struct mfc6_cache_cmp_arg arg = { |
| 948 | .mf6c_origin = in6addr_any, |
| 949 | .mf6c_mcastgrp = *mcastgrp, |
| 950 | }; |
Nicolas Dichtel | 660b26d | 2013-01-21 06:00:26 +0000 | [diff] [blame] | 951 | |
| 952 | if (ipv6_addr_any(mcastgrp)) |
Yuval Mintz | 845c9a7 | 2018-02-28 23:29:35 +0200 | [diff] [blame] | 953 | return mr_mfc_find_any_parent(mrt, mifi); |
| 954 | return mr_mfc_find_any(mrt, mifi, &arg); |
Nicolas Dichtel | 660b26d | 2013-01-21 06:00:26 +0000 | [diff] [blame] | 955 | } |
| 956 | |
Yuval Mintz | 87c418b | 2018-02-28 23:29:31 +0200 | [diff] [blame] | 957 | /* Look for a (S,G,iif) entry if parent != -1 */ |
| 958 | static struct mfc6_cache * |
Yuval Mintz | b70432f | 2018-02-28 23:29:32 +0200 | [diff] [blame] | 959 | ip6mr_cache_find_parent(struct mr_table *mrt, |
Yuval Mintz | 87c418b | 2018-02-28 23:29:31 +0200 | [diff] [blame] | 960 | const struct in6_addr *origin, |
| 961 | const struct in6_addr *mcastgrp, |
| 962 | int parent) |
| 963 | { |
| 964 | struct mfc6_cache_cmp_arg arg = { |
| 965 | .mf6c_origin = *origin, |
| 966 | .mf6c_mcastgrp = *mcastgrp, |
| 967 | }; |
Yuval Mintz | 87c418b | 2018-02-28 23:29:31 +0200 | [diff] [blame] | 968 | |
Yuval Mintz | 845c9a7 | 2018-02-28 23:29:35 +0200 | [diff] [blame] | 969 | return mr_mfc_find_parent(mrt, &arg, parent); |
Yuval Mintz | 87c418b | 2018-02-28 23:29:31 +0200 | [diff] [blame] | 970 | } |
| 971 | |
Yuval Mintz | 845c9a7 | 2018-02-28 23:29:35 +0200 | [diff] [blame] | 972 | /* Allocate a multicast cache entry */ |
Patrick McHardy | b5aa30b | 2010-05-11 14:40:50 +0200 | [diff] [blame] | 973 | static struct mfc6_cache *ip6mr_cache_alloc(void) |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 974 | { |
Joe Perches | 36cbac5 | 2008-12-03 22:27:25 -0800 | [diff] [blame] | 975 | struct mfc6_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_KERNEL); |
Ian Morris | 63159f2 | 2015-03-29 14:00:04 +0100 | [diff] [blame] | 976 | if (!c) |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 977 | return NULL; |
Yuval Mintz | 494fff5 | 2018-02-28 23:29:34 +0200 | [diff] [blame] | 978 | c->_c.mfc_un.res.last_assert = jiffies - MFC_ASSERT_THRESH - 1; |
| 979 | c->_c.mfc_un.res.minvif = MAXMIFS; |
Yuval Mintz | 8c13af2 | 2018-03-26 15:01:36 +0300 | [diff] [blame] | 980 | c->_c.free = ip6mr_cache_free_rcu; |
| 981 | refcount_set(&c->_c.mfc_un.res.refcount, 1); |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 982 | return c; |
| 983 | } |
| 984 | |
Patrick McHardy | b5aa30b | 2010-05-11 14:40:50 +0200 | [diff] [blame] | 985 | static struct mfc6_cache *ip6mr_cache_alloc_unres(void) |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 986 | { |
Joe Perches | 36cbac5 | 2008-12-03 22:27:25 -0800 | [diff] [blame] | 987 | struct mfc6_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_ATOMIC); |
Ian Morris | 63159f2 | 2015-03-29 14:00:04 +0100 | [diff] [blame] | 988 | if (!c) |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 989 | return NULL; |
Yuval Mintz | 494fff5 | 2018-02-28 23:29:34 +0200 | [diff] [blame] | 990 | skb_queue_head_init(&c->_c.mfc_un.unres.unresolved); |
| 991 | c->_c.mfc_un.unres.expires = jiffies + 10 * HZ; |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 992 | return c; |
| 993 | } |
| 994 | |
| 995 | /* |
| 996 | * A cache entry has gone into a resolved state from queued |
| 997 | */ |
| 998 | |
Yuval Mintz | b70432f | 2018-02-28 23:29:32 +0200 | [diff] [blame] | 999 | static void ip6mr_cache_resolve(struct net *net, struct mr_table *mrt, |
Patrick McHardy | 6bd5214 | 2010-05-11 14:40:53 +0200 | [diff] [blame] | 1000 | struct mfc6_cache *uc, struct mfc6_cache *c) |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 1001 | { |
| 1002 | struct sk_buff *skb; |
| 1003 | |
| 1004 | /* |
| 1005 | * Play the pending entries through our router |
| 1006 | */ |
| 1007 | |
Yuval Mintz | 494fff5 | 2018-02-28 23:29:34 +0200 | [diff] [blame] | 1008 | while ((skb = __skb_dequeue(&uc->_c.mfc_un.unres.unresolved))) { |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 1009 | if (ipv6_hdr(skb)->version == 0) { |
Johannes Berg | af72868 | 2017-06-16 14:29:22 +0200 | [diff] [blame] | 1010 | struct nlmsghdr *nlh = skb_pull(skb, |
| 1011 | sizeof(struct ipv6hdr)); |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 1012 | |
Yuval Mintz | 7b0db85 | 2018-02-28 23:29:39 +0200 | [diff] [blame] | 1013 | if (mr_fill_mroute(mrt, skb, &c->_c, |
| 1014 | nlmsg_data(nlh)) > 0) { |
YOSHIFUJI Hideaki | 549e028 | 2008-04-05 22:17:39 +0900 | [diff] [blame] | 1015 | nlh->nlmsg_len = skb_tail_pointer(skb) - (u8 *)nlh; |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 1016 | } else { |
| 1017 | nlh->nlmsg_type = NLMSG_ERROR; |
Hong zhi guo | 573ce26 | 2013-03-27 06:47:04 +0000 | [diff] [blame] | 1018 | nlh->nlmsg_len = nlmsg_msg_size(sizeof(struct nlmsgerr)); |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 1019 | skb_trim(skb, nlh->nlmsg_len); |
Hong zhi guo | 573ce26 | 2013-03-27 06:47:04 +0000 | [diff] [blame] | 1020 | ((struct nlmsgerr *)nlmsg_data(nlh))->error = -EMSGSIZE; |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 1021 | } |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 1022 | rtnl_unicast(skb, net, NETLINK_CB(skb).portid); |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 1023 | } else |
Patrick Ruddy | e4a38c0 | 2018-10-01 09:41:27 +0100 | [diff] [blame] | 1024 | ip6_mr_forward(net, mrt, skb->dev, skb, c); |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 1025 | } |
| 1026 | } |
| 1027 | |
| 1028 | /* |
Julien Gomes | dd12d15c | 2017-06-20 13:54:18 -0700 | [diff] [blame] | 1029 | * Bounce a cache query up to pim6sd and netlink. |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 1030 | * |
| 1031 | * Called under mrt_lock. |
| 1032 | */ |
| 1033 | |
Yuval Mintz | b70432f | 2018-02-28 23:29:32 +0200 | [diff] [blame] | 1034 | static int ip6mr_cache_report(struct mr_table *mrt, struct sk_buff *pkt, |
Patrick McHardy | 6bd5214 | 2010-05-11 14:40:53 +0200 | [diff] [blame] | 1035 | mifi_t mifi, int assert) |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 1036 | { |
Yuval Mintz | 8571ab4 | 2018-02-28 23:29:30 +0200 | [diff] [blame] | 1037 | struct sock *mroute6_sk; |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 1038 | struct sk_buff *skb; |
| 1039 | struct mrt6msg *msg; |
| 1040 | int ret; |
| 1041 | |
YOSHIFUJI Hideaki | 14fb64e | 2008-04-03 09:22:54 +0900 | [diff] [blame] | 1042 | #ifdef CONFIG_IPV6_PIMSM_V2 |
| 1043 | if (assert == MRT6MSG_WHOLEPKT) |
| 1044 | skb = skb_realloc_headroom(pkt, -skb_network_offset(pkt) |
| 1045 | +sizeof(*msg)); |
| 1046 | else |
| 1047 | #endif |
| 1048 | skb = alloc_skb(sizeof(struct ipv6hdr) + sizeof(*msg), GFP_ATOMIC); |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 1049 | |
| 1050 | if (!skb) |
| 1051 | return -ENOBUFS; |
| 1052 | |
| 1053 | /* I suppose that internal messages |
| 1054 | * do not require checksums */ |
| 1055 | |
| 1056 | skb->ip_summed = CHECKSUM_UNNECESSARY; |
| 1057 | |
YOSHIFUJI Hideaki | 14fb64e | 2008-04-03 09:22:54 +0900 | [diff] [blame] | 1058 | #ifdef CONFIG_IPV6_PIMSM_V2 |
| 1059 | if (assert == MRT6MSG_WHOLEPKT) { |
| 1060 | /* Ugly, but we have no choice with this interface. |
| 1061 | Duplicate old header, fix length etc. |
| 1062 | And all this only to mangle msg->im6_msgtype and |
| 1063 | to set msg->im6_mbz to "mbz" :-) |
| 1064 | */ |
| 1065 | skb_push(skb, -skb_network_offset(pkt)); |
| 1066 | |
| 1067 | skb_push(skb, sizeof(*msg)); |
| 1068 | skb_reset_transport_header(skb); |
| 1069 | msg = (struct mrt6msg *)skb_transport_header(skb); |
| 1070 | msg->im6_mbz = 0; |
| 1071 | msg->im6_msgtype = MRT6MSG_WHOLEPKT; |
Patrick McHardy | 6bd5214 | 2010-05-11 14:40:53 +0200 | [diff] [blame] | 1072 | msg->im6_mif = mrt->mroute_reg_vif_num; |
YOSHIFUJI Hideaki | 14fb64e | 2008-04-03 09:22:54 +0900 | [diff] [blame] | 1073 | msg->im6_pad = 0; |
Alexey Dobriyan | 4e3fd7a | 2011-11-21 03:39:03 +0000 | [diff] [blame] | 1074 | msg->im6_src = ipv6_hdr(pkt)->saddr; |
| 1075 | msg->im6_dst = ipv6_hdr(pkt)->daddr; |
YOSHIFUJI Hideaki | 14fb64e | 2008-04-03 09:22:54 +0900 | [diff] [blame] | 1076 | |
| 1077 | skb->ip_summed = CHECKSUM_UNNECESSARY; |
| 1078 | } else |
| 1079 | #endif |
| 1080 | { |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 1081 | /* |
| 1082 | * Copy the IP header |
| 1083 | */ |
| 1084 | |
| 1085 | skb_put(skb, sizeof(struct ipv6hdr)); |
| 1086 | skb_reset_network_header(skb); |
| 1087 | skb_copy_to_linear_data(skb, ipv6_hdr(pkt), sizeof(struct ipv6hdr)); |
| 1088 | |
| 1089 | /* |
| 1090 | * Add our header |
| 1091 | */ |
| 1092 | skb_put(skb, sizeof(*msg)); |
| 1093 | skb_reset_transport_header(skb); |
| 1094 | msg = (struct mrt6msg *)skb_transport_header(skb); |
| 1095 | |
| 1096 | msg->im6_mbz = 0; |
| 1097 | msg->im6_msgtype = assert; |
Rami Rosen | 6ac7eb0 | 2008-04-10 12:40:10 +0300 | [diff] [blame] | 1098 | msg->im6_mif = mifi; |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 1099 | msg->im6_pad = 0; |
Alexey Dobriyan | 4e3fd7a | 2011-11-21 03:39:03 +0000 | [diff] [blame] | 1100 | msg->im6_src = ipv6_hdr(pkt)->saddr; |
| 1101 | msg->im6_dst = ipv6_hdr(pkt)->daddr; |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 1102 | |
Eric Dumazet | adf3090 | 2009-06-02 05:19:30 +0000 | [diff] [blame] | 1103 | skb_dst_set(skb, dst_clone(skb_dst(pkt))); |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 1104 | skb->ip_summed = CHECKSUM_UNNECESSARY; |
YOSHIFUJI Hideaki | 14fb64e | 2008-04-03 09:22:54 +0900 | [diff] [blame] | 1105 | } |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 1106 | |
Yuval Mintz | 8571ab4 | 2018-02-28 23:29:30 +0200 | [diff] [blame] | 1107 | rcu_read_lock(); |
Yuval Mintz | b70432f | 2018-02-28 23:29:32 +0200 | [diff] [blame] | 1108 | mroute6_sk = rcu_dereference(mrt->mroute_sk); |
Yuval Mintz | 8571ab4 | 2018-02-28 23:29:30 +0200 | [diff] [blame] | 1109 | if (!mroute6_sk) { |
| 1110 | rcu_read_unlock(); |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 1111 | kfree_skb(skb); |
| 1112 | return -EINVAL; |
| 1113 | } |
| 1114 | |
Julien Gomes | dd12d15c | 2017-06-20 13:54:18 -0700 | [diff] [blame] | 1115 | mrt6msg_netlink_event(mrt, skb); |
| 1116 | |
Yuval Mintz | 8571ab4 | 2018-02-28 23:29:30 +0200 | [diff] [blame] | 1117 | /* Deliver to user space multicast routing algorithms */ |
| 1118 | ret = sock_queue_rcv_skb(mroute6_sk, skb); |
| 1119 | rcu_read_unlock(); |
Benjamin Thery | bd91b8b | 2008-12-10 16:07:08 -0800 | [diff] [blame] | 1120 | if (ret < 0) { |
Joe Perches | e87cc47 | 2012-05-13 21:56:26 +0000 | [diff] [blame] | 1121 | net_warn_ratelimited("mroute6: pending queue full, dropping entries\n"); |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 1122 | kfree_skb(skb); |
| 1123 | } |
| 1124 | |
| 1125 | return ret; |
| 1126 | } |
| 1127 | |
Yuval Mintz | 494fff5 | 2018-02-28 23:29:34 +0200 | [diff] [blame] | 1128 | /* Queue a packet for resolution. It gets locked cache entry! */ |
| 1129 | static int ip6mr_cache_unresolved(struct mr_table *mrt, mifi_t mifi, |
Patrick Ruddy | e4a38c0 | 2018-10-01 09:41:27 +0100 | [diff] [blame] | 1130 | struct sk_buff *skb, struct net_device *dev) |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 1131 | { |
Yuval Mintz | 494fff5 | 2018-02-28 23:29:34 +0200 | [diff] [blame] | 1132 | struct mfc6_cache *c; |
Patrick McHardy | f30a7784 | 2010-05-11 14:40:51 +0200 | [diff] [blame] | 1133 | bool found = false; |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 1134 | int err; |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 1135 | |
| 1136 | spin_lock_bh(&mfc_unres_lock); |
Yuval Mintz | 494fff5 | 2018-02-28 23:29:34 +0200 | [diff] [blame] | 1137 | list_for_each_entry(c, &mrt->mfc_unres_queue, _c.list) { |
Patrick McHardy | c476efb | 2010-05-11 14:40:48 +0200 | [diff] [blame] | 1138 | if (ipv6_addr_equal(&c->mf6c_mcastgrp, &ipv6_hdr(skb)->daddr) && |
Patrick McHardy | f30a7784 | 2010-05-11 14:40:51 +0200 | [diff] [blame] | 1139 | ipv6_addr_equal(&c->mf6c_origin, &ipv6_hdr(skb)->saddr)) { |
| 1140 | found = true; |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 1141 | break; |
Patrick McHardy | f30a7784 | 2010-05-11 14:40:51 +0200 | [diff] [blame] | 1142 | } |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 1143 | } |
| 1144 | |
Patrick McHardy | f30a7784 | 2010-05-11 14:40:51 +0200 | [diff] [blame] | 1145 | if (!found) { |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 1146 | /* |
| 1147 | * Create a new entry if allowable |
| 1148 | */ |
| 1149 | |
Hangbin Liu | 0079ad8 | 2019-09-06 15:36:01 +0800 | [diff] [blame] | 1150 | c = ip6mr_cache_alloc_unres(); |
| 1151 | if (!c) { |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 1152 | spin_unlock_bh(&mfc_unres_lock); |
| 1153 | |
| 1154 | kfree_skb(skb); |
| 1155 | return -ENOBUFS; |
| 1156 | } |
| 1157 | |
Yuval Mintz | 494fff5 | 2018-02-28 23:29:34 +0200 | [diff] [blame] | 1158 | /* Fill in the new cache entry */ |
| 1159 | c->_c.mfc_parent = -1; |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 1160 | c->mf6c_origin = ipv6_hdr(skb)->saddr; |
| 1161 | c->mf6c_mcastgrp = ipv6_hdr(skb)->daddr; |
| 1162 | |
| 1163 | /* |
| 1164 | * Reflect first query at pim6sd |
| 1165 | */ |
Patrick McHardy | 6bd5214 | 2010-05-11 14:40:53 +0200 | [diff] [blame] | 1166 | err = ip6mr_cache_report(mrt, skb, mifi, MRT6MSG_NOCACHE); |
Benjamin Thery | 8229efd | 2008-12-10 16:30:15 -0800 | [diff] [blame] | 1167 | if (err < 0) { |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 1168 | /* If the report failed throw the cache entry |
| 1169 | out - Brad Parker |
| 1170 | */ |
| 1171 | spin_unlock_bh(&mfc_unres_lock); |
| 1172 | |
Benjamin Thery | 58701ad | 2008-12-10 16:22:34 -0800 | [diff] [blame] | 1173 | ip6mr_cache_free(c); |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 1174 | kfree_skb(skb); |
| 1175 | return err; |
| 1176 | } |
| 1177 | |
Patrick McHardy | 6bd5214 | 2010-05-11 14:40:53 +0200 | [diff] [blame] | 1178 | atomic_inc(&mrt->cache_resolve_queue_len); |
Yuval Mintz | 494fff5 | 2018-02-28 23:29:34 +0200 | [diff] [blame] | 1179 | list_add(&c->_c.list, &mrt->mfc_unres_queue); |
Nicolas Dichtel | 812e44d | 2012-12-04 01:13:41 +0000 | [diff] [blame] | 1180 | mr6_netlink_event(mrt, c, RTM_NEWROUTE); |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 1181 | |
Patrick McHardy | 6bd5214 | 2010-05-11 14:40:53 +0200 | [diff] [blame] | 1182 | ipmr_do_expire_process(mrt); |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 1183 | } |
| 1184 | |
Yuval Mintz | 494fff5 | 2018-02-28 23:29:34 +0200 | [diff] [blame] | 1185 | /* See if we can append the packet */ |
| 1186 | if (c->_c.mfc_un.unres.unresolved.qlen > 3) { |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 1187 | kfree_skb(skb); |
| 1188 | err = -ENOBUFS; |
| 1189 | } else { |
Patrick Ruddy | e4a38c0 | 2018-10-01 09:41:27 +0100 | [diff] [blame] | 1190 | if (dev) { |
| 1191 | skb->dev = dev; |
| 1192 | skb->skb_iif = dev->ifindex; |
| 1193 | } |
Yuval Mintz | 494fff5 | 2018-02-28 23:29:34 +0200 | [diff] [blame] | 1194 | skb_queue_tail(&c->_c.mfc_un.unres.unresolved, skb); |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 1195 | err = 0; |
| 1196 | } |
| 1197 | |
| 1198 | spin_unlock_bh(&mfc_unres_lock); |
| 1199 | return err; |
| 1200 | } |
| 1201 | |
| 1202 | /* |
| 1203 | * MFC6 cache manipulation by user space |
| 1204 | */ |
| 1205 | |
Yuval Mintz | b70432f | 2018-02-28 23:29:32 +0200 | [diff] [blame] | 1206 | static int ip6mr_mfc_delete(struct mr_table *mrt, struct mf6cctl *mfc, |
Nicolas Dichtel | 660b26d | 2013-01-21 06:00:26 +0000 | [diff] [blame] | 1207 | int parent) |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 1208 | { |
Yuval Mintz | 87c418b | 2018-02-28 23:29:31 +0200 | [diff] [blame] | 1209 | struct mfc6_cache *c; |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 1210 | |
Yuval Mintz | 87c418b | 2018-02-28 23:29:31 +0200 | [diff] [blame] | 1211 | /* The entries are added/deleted only under RTNL */ |
| 1212 | rcu_read_lock(); |
| 1213 | c = ip6mr_cache_find_parent(mrt, &mfc->mf6cc_origin.sin6_addr, |
| 1214 | &mfc->mf6cc_mcastgrp.sin6_addr, parent); |
| 1215 | rcu_read_unlock(); |
| 1216 | if (!c) |
| 1217 | return -ENOENT; |
Yuval Mintz | 494fff5 | 2018-02-28 23:29:34 +0200 | [diff] [blame] | 1218 | rhltable_remove(&mrt->mfc_hash, &c->_c.mnode, ip6mr_rht_params); |
| 1219 | list_del_rcu(&c->_c.list); |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 1220 | |
Yuval Mintz | 088aa3e | 2018-03-26 15:01:34 +0300 | [diff] [blame] | 1221 | call_ip6mr_mfc_entry_notifiers(read_pnet(&mrt->net), |
| 1222 | FIB_EVENT_ENTRY_DEL, c, mrt->id); |
Yuval Mintz | 87c418b | 2018-02-28 23:29:31 +0200 | [diff] [blame] | 1223 | mr6_netlink_event(mrt, c, RTM_DELROUTE); |
Yuval Mintz | 8c13af2 | 2018-03-26 15:01:36 +0300 | [diff] [blame] | 1224 | mr_cache_put(&c->_c); |
Yuval Mintz | 87c418b | 2018-02-28 23:29:31 +0200 | [diff] [blame] | 1225 | return 0; |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 1226 | } |
| 1227 | |
| 1228 | static int ip6mr_device_event(struct notifier_block *this, |
| 1229 | unsigned long event, void *ptr) |
| 1230 | { |
Jiri Pirko | 351638e | 2013-05-28 01:30:21 +0000 | [diff] [blame] | 1231 | struct net_device *dev = netdev_notifier_info_to_dev(ptr); |
Benjamin Thery | 8229efd | 2008-12-10 16:30:15 -0800 | [diff] [blame] | 1232 | struct net *net = dev_net(dev); |
Yuval Mintz | b70432f | 2018-02-28 23:29:32 +0200 | [diff] [blame] | 1233 | struct mr_table *mrt; |
Yuval Mintz | 6853f21 | 2018-02-28 23:29:29 +0200 | [diff] [blame] | 1234 | struct vif_device *v; |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 1235 | int ct; |
| 1236 | |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 1237 | if (event != NETDEV_UNREGISTER) |
| 1238 | return NOTIFY_DONE; |
| 1239 | |
Patrick McHardy | d1db275 | 2010-05-11 14:40:55 +0200 | [diff] [blame] | 1240 | ip6mr_for_each_table(mrt, net) { |
Yuval Mintz | b70432f | 2018-02-28 23:29:32 +0200 | [diff] [blame] | 1241 | v = &mrt->vif_table[0]; |
Patrick McHardy | d1db275 | 2010-05-11 14:40:55 +0200 | [diff] [blame] | 1242 | for (ct = 0; ct < mrt->maxvif; ct++, v++) { |
| 1243 | if (v->dev == dev) |
Nikolay Aleksandrov | 723b929 | 2017-04-21 20:42:16 +0300 | [diff] [blame] | 1244 | mif6_delete(mrt, ct, 1, NULL); |
Patrick McHardy | d1db275 | 2010-05-11 14:40:55 +0200 | [diff] [blame] | 1245 | } |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 1246 | } |
Eric Dumazet | c871e66 | 2009-10-28 04:48:11 +0000 | [diff] [blame] | 1247 | |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 1248 | return NOTIFY_DONE; |
| 1249 | } |
| 1250 | |
Yuval Mintz | 088aa3e | 2018-03-26 15:01:34 +0300 | [diff] [blame] | 1251 | static unsigned int ip6mr_seq_read(struct net *net) |
| 1252 | { |
| 1253 | ASSERT_RTNL(); |
| 1254 | |
| 1255 | return net->ipv6.ipmr_seq + ip6mr_rules_seq_read(net); |
| 1256 | } |
| 1257 | |
Jiri Pirko | b7a5955 | 2019-10-03 11:49:30 +0200 | [diff] [blame] | 1258 | static int ip6mr_dump(struct net *net, struct notifier_block *nb, |
| 1259 | struct netlink_ext_ack *extack) |
Yuval Mintz | 088aa3e | 2018-03-26 15:01:34 +0300 | [diff] [blame] | 1260 | { |
| 1261 | return mr_dump(net, nb, RTNL_FAMILY_IP6MR, ip6mr_rules_dump, |
Jiri Pirko | b7a5955 | 2019-10-03 11:49:30 +0200 | [diff] [blame] | 1262 | ip6mr_mr_table_iter, &mrt_lock, extack); |
Yuval Mintz | 088aa3e | 2018-03-26 15:01:34 +0300 | [diff] [blame] | 1263 | } |
| 1264 | |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 1265 | static struct notifier_block ip6_mr_notifier = { |
| 1266 | .notifier_call = ip6mr_device_event |
| 1267 | }; |
| 1268 | |
Yuval Mintz | 088aa3e | 2018-03-26 15:01:34 +0300 | [diff] [blame] | 1269 | static const struct fib_notifier_ops ip6mr_notifier_ops_template = { |
| 1270 | .family = RTNL_FAMILY_IP6MR, |
| 1271 | .fib_seq_read = ip6mr_seq_read, |
| 1272 | .fib_dump = ip6mr_dump, |
| 1273 | .owner = THIS_MODULE, |
| 1274 | }; |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 1275 | |
Yuval Mintz | 088aa3e | 2018-03-26 15:01:34 +0300 | [diff] [blame] | 1276 | static int __net_init ip6mr_notifier_init(struct net *net) |
| 1277 | { |
| 1278 | struct fib_notifier_ops *ops; |
| 1279 | |
| 1280 | net->ipv6.ipmr_seq = 0; |
| 1281 | |
| 1282 | ops = fib_notifier_ops_register(&ip6mr_notifier_ops_template, net); |
| 1283 | if (IS_ERR(ops)) |
| 1284 | return PTR_ERR(ops); |
| 1285 | |
| 1286 | net->ipv6.ip6mr_notifier_ops = ops; |
| 1287 | |
| 1288 | return 0; |
| 1289 | } |
| 1290 | |
| 1291 | static void __net_exit ip6mr_notifier_exit(struct net *net) |
| 1292 | { |
| 1293 | fib_notifier_ops_unregister(net->ipv6.ip6mr_notifier_ops); |
| 1294 | net->ipv6.ip6mr_notifier_ops = NULL; |
| 1295 | } |
| 1296 | |
| 1297 | /* Setup for IP multicast routing */ |
Benjamin Thery | 4e16880 | 2008-12-10 16:15:08 -0800 | [diff] [blame] | 1298 | static int __net_init ip6mr_net_init(struct net *net) |
| 1299 | { |
Patrick McHardy | d1db275 | 2010-05-11 14:40:55 +0200 | [diff] [blame] | 1300 | int err; |
Patrick McHardy | f30a7784 | 2010-05-11 14:40:51 +0200 | [diff] [blame] | 1301 | |
Yuval Mintz | 088aa3e | 2018-03-26 15:01:34 +0300 | [diff] [blame] | 1302 | err = ip6mr_notifier_init(net); |
| 1303 | if (err) |
| 1304 | return err; |
| 1305 | |
Patrick McHardy | d1db275 | 2010-05-11 14:40:55 +0200 | [diff] [blame] | 1306 | err = ip6mr_rules_init(net); |
| 1307 | if (err < 0) |
Yuval Mintz | 088aa3e | 2018-03-26 15:01:34 +0300 | [diff] [blame] | 1308 | goto ip6mr_rules_fail; |
Benjamin Thery | 8b90fc7 | 2008-12-10 16:29:48 -0800 | [diff] [blame] | 1309 | |
| 1310 | #ifdef CONFIG_PROC_FS |
| 1311 | err = -ENOMEM; |
Christoph Hellwig | c350637 | 2018-04-10 19:42:55 +0200 | [diff] [blame] | 1312 | if (!proc_create_net("ip6_mr_vif", 0, net->proc_net, &ip6mr_vif_seq_ops, |
| 1313 | sizeof(struct mr_vif_iter))) |
Benjamin Thery | 8b90fc7 | 2008-12-10 16:29:48 -0800 | [diff] [blame] | 1314 | goto proc_vif_fail; |
Christoph Hellwig | c350637 | 2018-04-10 19:42:55 +0200 | [diff] [blame] | 1315 | if (!proc_create_net("ip6_mr_cache", 0, net->proc_net, &ipmr_mfc_seq_ops, |
| 1316 | sizeof(struct mr_mfc_iter))) |
Benjamin Thery | 8b90fc7 | 2008-12-10 16:29:48 -0800 | [diff] [blame] | 1317 | goto proc_cache_fail; |
| 1318 | #endif |
Patrick McHardy | 6bd5214 | 2010-05-11 14:40:53 +0200 | [diff] [blame] | 1319 | |
Benjamin Thery | 4a6258a | 2008-12-10 16:24:07 -0800 | [diff] [blame] | 1320 | return 0; |
| 1321 | |
Benjamin Thery | 8b90fc7 | 2008-12-10 16:29:48 -0800 | [diff] [blame] | 1322 | #ifdef CONFIG_PROC_FS |
| 1323 | proc_cache_fail: |
Gao feng | ece31ff | 2013-02-18 01:34:56 +0000 | [diff] [blame] | 1324 | remove_proc_entry("ip6_mr_vif", net->proc_net); |
Benjamin Thery | 8b90fc7 | 2008-12-10 16:29:48 -0800 | [diff] [blame] | 1325 | proc_vif_fail: |
Patrick McHardy | d1db275 | 2010-05-11 14:40:55 +0200 | [diff] [blame] | 1326 | ip6mr_rules_exit(net); |
Benjamin Thery | 8b90fc7 | 2008-12-10 16:29:48 -0800 | [diff] [blame] | 1327 | #endif |
Yuval Mintz | 088aa3e | 2018-03-26 15:01:34 +0300 | [diff] [blame] | 1328 | ip6mr_rules_fail: |
| 1329 | ip6mr_notifier_exit(net); |
Benjamin Thery | 4e16880 | 2008-12-10 16:15:08 -0800 | [diff] [blame] | 1330 | return err; |
| 1331 | } |
| 1332 | |
| 1333 | static void __net_exit ip6mr_net_exit(struct net *net) |
| 1334 | { |
Benjamin Thery | 8b90fc7 | 2008-12-10 16:29:48 -0800 | [diff] [blame] | 1335 | #ifdef CONFIG_PROC_FS |
Gao feng | ece31ff | 2013-02-18 01:34:56 +0000 | [diff] [blame] | 1336 | remove_proc_entry("ip6_mr_cache", net->proc_net); |
| 1337 | remove_proc_entry("ip6_mr_vif", net->proc_net); |
Benjamin Thery | 8b90fc7 | 2008-12-10 16:29:48 -0800 | [diff] [blame] | 1338 | #endif |
Patrick McHardy | d1db275 | 2010-05-11 14:40:55 +0200 | [diff] [blame] | 1339 | ip6mr_rules_exit(net); |
Yuval Mintz | 088aa3e | 2018-03-26 15:01:34 +0300 | [diff] [blame] | 1340 | ip6mr_notifier_exit(net); |
Benjamin Thery | 4e16880 | 2008-12-10 16:15:08 -0800 | [diff] [blame] | 1341 | } |
| 1342 | |
| 1343 | static struct pernet_operations ip6mr_net_ops = { |
| 1344 | .init = ip6mr_net_init, |
| 1345 | .exit = ip6mr_net_exit, |
| 1346 | }; |
| 1347 | |
Wang Chen | 623d1a1 | 2008-07-03 12:13:30 +0800 | [diff] [blame] | 1348 | int __init ip6_mr_init(void) |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 1349 | { |
Wang Chen | 623d1a1 | 2008-07-03 12:13:30 +0800 | [diff] [blame] | 1350 | int err; |
| 1351 | |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 1352 | mrt_cachep = kmem_cache_create("ip6_mrt_cache", |
| 1353 | sizeof(struct mfc6_cache), |
| 1354 | 0, SLAB_HWCACHE_ALIGN, |
| 1355 | NULL); |
| 1356 | if (!mrt_cachep) |
Wang Chen | 623d1a1 | 2008-07-03 12:13:30 +0800 | [diff] [blame] | 1357 | return -ENOMEM; |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 1358 | |
Benjamin Thery | 4e16880 | 2008-12-10 16:15:08 -0800 | [diff] [blame] | 1359 | err = register_pernet_subsys(&ip6mr_net_ops); |
| 1360 | if (err) |
| 1361 | goto reg_pernet_fail; |
| 1362 | |
Wang Chen | 623d1a1 | 2008-07-03 12:13:30 +0800 | [diff] [blame] | 1363 | err = register_netdevice_notifier(&ip6_mr_notifier); |
| 1364 | if (err) |
| 1365 | goto reg_notif_fail; |
Tom Goff | 403dbb9 | 2009-06-14 03:16:13 -0700 | [diff] [blame] | 1366 | #ifdef CONFIG_IPV6_PIMSM_V2 |
| 1367 | if (inet6_add_protocol(&pim6_protocol, IPPROTO_PIM) < 0) { |
Joe Perches | f321383 | 2012-05-15 14:11:53 +0000 | [diff] [blame] | 1368 | pr_err("%s: can't add PIM protocol\n", __func__); |
Tom Goff | 403dbb9 | 2009-06-14 03:16:13 -0700 | [diff] [blame] | 1369 | err = -EAGAIN; |
| 1370 | goto add_proto_fail; |
| 1371 | } |
| 1372 | #endif |
Florian Westphal | a3fde2a | 2017-12-04 19:19:18 +0100 | [diff] [blame] | 1373 | err = rtnl_register_module(THIS_MODULE, RTNL_FAMILY_IP6MR, RTM_GETROUTE, |
| 1374 | NULL, ip6mr_rtm_dumproute, 0); |
| 1375 | if (err == 0) |
| 1376 | return 0; |
| 1377 | |
Tom Goff | 403dbb9 | 2009-06-14 03:16:13 -0700 | [diff] [blame] | 1378 | #ifdef CONFIG_IPV6_PIMSM_V2 |
Florian Westphal | a3fde2a | 2017-12-04 19:19:18 +0100 | [diff] [blame] | 1379 | inet6_del_protocol(&pim6_protocol, IPPROTO_PIM); |
Tom Goff | 403dbb9 | 2009-06-14 03:16:13 -0700 | [diff] [blame] | 1380 | add_proto_fail: |
| 1381 | unregister_netdevice_notifier(&ip6_mr_notifier); |
| 1382 | #endif |
Benjamin Thery | 87b30a6 | 2008-11-10 16:34:11 -0800 | [diff] [blame] | 1383 | reg_notif_fail: |
Benjamin Thery | 4e16880 | 2008-12-10 16:15:08 -0800 | [diff] [blame] | 1384 | unregister_pernet_subsys(&ip6mr_net_ops); |
| 1385 | reg_pernet_fail: |
Benjamin Thery | 87b30a6 | 2008-11-10 16:34:11 -0800 | [diff] [blame] | 1386 | kmem_cache_destroy(mrt_cachep); |
Wang Chen | 623d1a1 | 2008-07-03 12:13:30 +0800 | [diff] [blame] | 1387 | return err; |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 1388 | } |
| 1389 | |
Wang Chen | 623d1a1 | 2008-07-03 12:13:30 +0800 | [diff] [blame] | 1390 | void ip6_mr_cleanup(void) |
| 1391 | { |
Duan Jiong | ffb1388 | 2014-11-19 09:35:39 +0800 | [diff] [blame] | 1392 | rtnl_unregister(RTNL_FAMILY_IP6MR, RTM_GETROUTE); |
| 1393 | #ifdef CONFIG_IPV6_PIMSM_V2 |
| 1394 | inet6_del_protocol(&pim6_protocol, IPPROTO_PIM); |
| 1395 | #endif |
Wang Chen | 623d1a1 | 2008-07-03 12:13:30 +0800 | [diff] [blame] | 1396 | unregister_netdevice_notifier(&ip6_mr_notifier); |
Benjamin Thery | 4e16880 | 2008-12-10 16:15:08 -0800 | [diff] [blame] | 1397 | unregister_pernet_subsys(&ip6mr_net_ops); |
Wang Chen | 623d1a1 | 2008-07-03 12:13:30 +0800 | [diff] [blame] | 1398 | kmem_cache_destroy(mrt_cachep); |
| 1399 | } |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 1400 | |
Yuval Mintz | b70432f | 2018-02-28 23:29:32 +0200 | [diff] [blame] | 1401 | static int ip6mr_mfc_add(struct net *net, struct mr_table *mrt, |
Nicolas Dichtel | 660b26d | 2013-01-21 06:00:26 +0000 | [diff] [blame] | 1402 | struct mf6cctl *mfc, int mrtsock, int parent) |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 1403 | { |
Rami Rosen | 6ac7eb0 | 2008-04-10 12:40:10 +0300 | [diff] [blame] | 1404 | unsigned char ttls[MAXMIFS]; |
Yuval Mintz | 87c418b | 2018-02-28 23:29:31 +0200 | [diff] [blame] | 1405 | struct mfc6_cache *uc, *c; |
Yuval Mintz | 494fff5 | 2018-02-28 23:29:34 +0200 | [diff] [blame] | 1406 | struct mr_mfc *_uc; |
Yuval Mintz | 87c418b | 2018-02-28 23:29:31 +0200 | [diff] [blame] | 1407 | bool found; |
| 1408 | int i, err; |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 1409 | |
Patrick McHardy | a50436f2 | 2010-03-17 06:04:14 +0000 | [diff] [blame] | 1410 | if (mfc->mf6cc_parent >= MAXMIFS) |
| 1411 | return -ENFILE; |
| 1412 | |
Rami Rosen | 6ac7eb0 | 2008-04-10 12:40:10 +0300 | [diff] [blame] | 1413 | memset(ttls, 255, MAXMIFS); |
| 1414 | for (i = 0; i < MAXMIFS; i++) { |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 1415 | if (IF_ISSET(i, &mfc->mf6cc_ifset)) |
| 1416 | ttls[i] = 1; |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 1417 | } |
| 1418 | |
Yuval Mintz | 87c418b | 2018-02-28 23:29:31 +0200 | [diff] [blame] | 1419 | /* The entries are added/deleted only under RTNL */ |
| 1420 | rcu_read_lock(); |
| 1421 | c = ip6mr_cache_find_parent(mrt, &mfc->mf6cc_origin.sin6_addr, |
| 1422 | &mfc->mf6cc_mcastgrp.sin6_addr, parent); |
| 1423 | rcu_read_unlock(); |
| 1424 | if (c) { |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 1425 | write_lock_bh(&mrt_lock); |
Yuval Mintz | 494fff5 | 2018-02-28 23:29:34 +0200 | [diff] [blame] | 1426 | c->_c.mfc_parent = mfc->mf6cc_parent; |
| 1427 | ip6mr_update_thresholds(mrt, &c->_c, ttls); |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 1428 | if (!mrtsock) |
Yuval Mintz | 494fff5 | 2018-02-28 23:29:34 +0200 | [diff] [blame] | 1429 | c->_c.mfc_flags |= MFC_STATIC; |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 1430 | write_unlock_bh(&mrt_lock); |
Yuval Mintz | 088aa3e | 2018-03-26 15:01:34 +0300 | [diff] [blame] | 1431 | call_ip6mr_mfc_entry_notifiers(net, FIB_EVENT_ENTRY_REPLACE, |
| 1432 | c, mrt->id); |
Nicolas Dichtel | 812e44d | 2012-12-04 01:13:41 +0000 | [diff] [blame] | 1433 | mr6_netlink_event(mrt, c, RTM_NEWROUTE); |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 1434 | return 0; |
| 1435 | } |
| 1436 | |
Nicolas Dichtel | 660b26d | 2013-01-21 06:00:26 +0000 | [diff] [blame] | 1437 | if (!ipv6_addr_any(&mfc->mf6cc_mcastgrp.sin6_addr) && |
| 1438 | !ipv6_addr_is_multicast(&mfc->mf6cc_mcastgrp.sin6_addr)) |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 1439 | return -EINVAL; |
| 1440 | |
Patrick McHardy | b5aa30b | 2010-05-11 14:40:50 +0200 | [diff] [blame] | 1441 | c = ip6mr_cache_alloc(); |
Ian Morris | 63159f2 | 2015-03-29 14:00:04 +0100 | [diff] [blame] | 1442 | if (!c) |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 1443 | return -ENOMEM; |
| 1444 | |
| 1445 | c->mf6c_origin = mfc->mf6cc_origin.sin6_addr; |
| 1446 | c->mf6c_mcastgrp = mfc->mf6cc_mcastgrp.sin6_addr; |
Yuval Mintz | 494fff5 | 2018-02-28 23:29:34 +0200 | [diff] [blame] | 1447 | c->_c.mfc_parent = mfc->mf6cc_parent; |
| 1448 | ip6mr_update_thresholds(mrt, &c->_c, ttls); |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 1449 | if (!mrtsock) |
Yuval Mintz | 494fff5 | 2018-02-28 23:29:34 +0200 | [diff] [blame] | 1450 | c->_c.mfc_flags |= MFC_STATIC; |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 1451 | |
Yuval Mintz | 494fff5 | 2018-02-28 23:29:34 +0200 | [diff] [blame] | 1452 | err = rhltable_insert_key(&mrt->mfc_hash, &c->cmparg, &c->_c.mnode, |
Yuval Mintz | 87c418b | 2018-02-28 23:29:31 +0200 | [diff] [blame] | 1453 | ip6mr_rht_params); |
| 1454 | if (err) { |
| 1455 | pr_err("ip6mr: rhtable insert error %d\n", err); |
| 1456 | ip6mr_cache_free(c); |
| 1457 | return err; |
| 1458 | } |
Yuval Mintz | 494fff5 | 2018-02-28 23:29:34 +0200 | [diff] [blame] | 1459 | list_add_tail_rcu(&c->_c.list, &mrt->mfc_cache_list); |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 1460 | |
Yuval Mintz | 87c418b | 2018-02-28 23:29:31 +0200 | [diff] [blame] | 1461 | /* Check to see if we resolved a queued list. If so we |
| 1462 | * need to send on the frames and tidy up. |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 1463 | */ |
Patrick McHardy | f30a7784 | 2010-05-11 14:40:51 +0200 | [diff] [blame] | 1464 | found = false; |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 1465 | spin_lock_bh(&mfc_unres_lock); |
Yuval Mintz | 494fff5 | 2018-02-28 23:29:34 +0200 | [diff] [blame] | 1466 | list_for_each_entry(_uc, &mrt->mfc_unres_queue, list) { |
| 1467 | uc = (struct mfc6_cache *)_uc; |
Patrick McHardy | c476efb | 2010-05-11 14:40:48 +0200 | [diff] [blame] | 1468 | if (ipv6_addr_equal(&uc->mf6c_origin, &c->mf6c_origin) && |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 1469 | ipv6_addr_equal(&uc->mf6c_mcastgrp, &c->mf6c_mcastgrp)) { |
Yuval Mintz | 494fff5 | 2018-02-28 23:29:34 +0200 | [diff] [blame] | 1470 | list_del(&_uc->list); |
Patrick McHardy | 6bd5214 | 2010-05-11 14:40:53 +0200 | [diff] [blame] | 1471 | atomic_dec(&mrt->cache_resolve_queue_len); |
Patrick McHardy | f30a7784 | 2010-05-11 14:40:51 +0200 | [diff] [blame] | 1472 | found = true; |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 1473 | break; |
| 1474 | } |
| 1475 | } |
Yuval Mintz | b70432f | 2018-02-28 23:29:32 +0200 | [diff] [blame] | 1476 | if (list_empty(&mrt->mfc_unres_queue)) |
Patrick McHardy | 6bd5214 | 2010-05-11 14:40:53 +0200 | [diff] [blame] | 1477 | del_timer(&mrt->ipmr_expire_timer); |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 1478 | spin_unlock_bh(&mfc_unres_lock); |
| 1479 | |
Patrick McHardy | f30a7784 | 2010-05-11 14:40:51 +0200 | [diff] [blame] | 1480 | if (found) { |
Patrick McHardy | 6bd5214 | 2010-05-11 14:40:53 +0200 | [diff] [blame] | 1481 | ip6mr_cache_resolve(net, mrt, uc, c); |
Benjamin Thery | 58701ad | 2008-12-10 16:22:34 -0800 | [diff] [blame] | 1482 | ip6mr_cache_free(uc); |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 1483 | } |
Yuval Mintz | 088aa3e | 2018-03-26 15:01:34 +0300 | [diff] [blame] | 1484 | call_ip6mr_mfc_entry_notifiers(net, FIB_EVENT_ENTRY_ADD, |
| 1485 | c, mrt->id); |
Nicolas Dichtel | 812e44d | 2012-12-04 01:13:41 +0000 | [diff] [blame] | 1486 | mr6_netlink_event(mrt, c, RTM_NEWROUTE); |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 1487 | return 0; |
| 1488 | } |
| 1489 | |
| 1490 | /* |
| 1491 | * Close the multicast socket, and clear the vif tables etc |
| 1492 | */ |
| 1493 | |
Callum Sinclair | ca8d479 | 2019-02-18 10:07:52 +1300 | [diff] [blame] | 1494 | static void mroute_clean_tables(struct mr_table *mrt, int flags) |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 1495 | { |
Yuval Mintz | 494fff5 | 2018-02-28 23:29:34 +0200 | [diff] [blame] | 1496 | struct mr_mfc *c, *tmp; |
Eric Dumazet | c871e66 | 2009-10-28 04:48:11 +0000 | [diff] [blame] | 1497 | LIST_HEAD(list); |
Yuval Mintz | 87c418b | 2018-02-28 23:29:31 +0200 | [diff] [blame] | 1498 | int i; |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 1499 | |
Yuval Mintz | 87c418b | 2018-02-28 23:29:31 +0200 | [diff] [blame] | 1500 | /* Shut down all active vif entries */ |
Callum Sinclair | ca8d479 | 2019-02-18 10:07:52 +1300 | [diff] [blame] | 1501 | if (flags & (MRT6_FLUSH_MIFS | MRT6_FLUSH_MIFS_STATIC)) { |
| 1502 | for (i = 0; i < mrt->maxvif; i++) { |
| 1503 | if (((mrt->vif_table[i].flags & VIFF_STATIC) && |
| 1504 | !(flags & MRT6_FLUSH_MIFS_STATIC)) || |
| 1505 | (!(mrt->vif_table[i].flags & VIFF_STATIC) && !(flags & MRT6_FLUSH_MIFS))) |
| 1506 | continue; |
| 1507 | mif6_delete(mrt, i, 0, &list); |
| 1508 | } |
| 1509 | unregister_netdevice_many(&list); |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 1510 | } |
| 1511 | |
Yuval Mintz | 87c418b | 2018-02-28 23:29:31 +0200 | [diff] [blame] | 1512 | /* Wipe the cache */ |
Callum Sinclair | ca8d479 | 2019-02-18 10:07:52 +1300 | [diff] [blame] | 1513 | if (flags & (MRT6_FLUSH_MFC | MRT6_FLUSH_MFC_STATIC)) { |
| 1514 | list_for_each_entry_safe(c, tmp, &mrt->mfc_cache_list, list) { |
| 1515 | if (((c->mfc_flags & MFC_STATIC) && !(flags & MRT6_FLUSH_MFC_STATIC)) || |
| 1516 | (!(c->mfc_flags & MFC_STATIC) && !(flags & MRT6_FLUSH_MFC))) |
| 1517 | continue; |
| 1518 | rhltable_remove(&mrt->mfc_hash, &c->mnode, ip6mr_rht_params); |
| 1519 | list_del_rcu(&c->list); |
| 1520 | call_ip6mr_mfc_entry_notifiers(read_pnet(&mrt->net), |
| 1521 | FIB_EVENT_ENTRY_DEL, |
| 1522 | (struct mfc6_cache *)c, mrt->id); |
| 1523 | mr6_netlink_event(mrt, (struct mfc6_cache *)c, RTM_DELROUTE); |
| 1524 | mr_cache_put(c); |
| 1525 | } |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 1526 | } |
| 1527 | |
Callum Sinclair | ca8d479 | 2019-02-18 10:07:52 +1300 | [diff] [blame] | 1528 | if (flags & MRT6_FLUSH_MFC) { |
| 1529 | if (atomic_read(&mrt->cache_resolve_queue_len) != 0) { |
| 1530 | spin_lock_bh(&mfc_unres_lock); |
| 1531 | list_for_each_entry_safe(c, tmp, &mrt->mfc_unres_queue, list) { |
| 1532 | list_del(&c->list); |
| 1533 | mr6_netlink_event(mrt, (struct mfc6_cache *)c, |
| 1534 | RTM_DELROUTE); |
| 1535 | ip6mr_destroy_unres(mrt, (struct mfc6_cache *)c); |
| 1536 | } |
| 1537 | spin_unlock_bh(&mfc_unres_lock); |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 1538 | } |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 1539 | } |
| 1540 | } |
| 1541 | |
Yuval Mintz | b70432f | 2018-02-28 23:29:32 +0200 | [diff] [blame] | 1542 | static int ip6mr_sk_init(struct mr_table *mrt, struct sock *sk) |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 1543 | { |
| 1544 | int err = 0; |
Benjamin Thery | 8229efd | 2008-12-10 16:30:15 -0800 | [diff] [blame] | 1545 | struct net *net = sock_net(sk); |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 1546 | |
| 1547 | rtnl_lock(); |
| 1548 | write_lock_bh(&mrt_lock); |
Yuval Mintz | b70432f | 2018-02-28 23:29:32 +0200 | [diff] [blame] | 1549 | if (rtnl_dereference(mrt->mroute_sk)) { |
Eric Dumazet | 927265b | 2016-07-08 05:46:04 +0200 | [diff] [blame] | 1550 | err = -EADDRINUSE; |
Yuval Mintz | 8571ab4 | 2018-02-28 23:29:30 +0200 | [diff] [blame] | 1551 | } else { |
Yuval Mintz | b70432f | 2018-02-28 23:29:32 +0200 | [diff] [blame] | 1552 | rcu_assign_pointer(mrt->mroute_sk, sk); |
Eric Dumazet | a366e30 | 2018-03-07 08:43:19 -0800 | [diff] [blame] | 1553 | sock_set_flag(sk, SOCK_RCU_FREE); |
Yuval Mintz | 8571ab4 | 2018-02-28 23:29:30 +0200 | [diff] [blame] | 1554 | net->ipv6.devconf_all->mc_forwarding++; |
Eric Dumazet | 927265b | 2016-07-08 05:46:04 +0200 | [diff] [blame] | 1555 | } |
| 1556 | write_unlock_bh(&mrt_lock); |
| 1557 | |
| 1558 | if (!err) |
David Ahern | 85b3daa | 2017-03-28 14:28:04 -0700 | [diff] [blame] | 1559 | inet6_netconf_notify_devconf(net, RTM_NEWNETCONF, |
| 1560 | NETCONFA_MC_FORWARDING, |
Nicolas Dichtel | d67b8c6 | 2012-12-04 01:13:35 +0000 | [diff] [blame] | 1561 | NETCONFA_IFINDEX_ALL, |
| 1562 | net->ipv6.devconf_all); |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 1563 | rtnl_unlock(); |
| 1564 | |
| 1565 | return err; |
| 1566 | } |
| 1567 | |
| 1568 | int ip6mr_sk_done(struct sock *sk) |
| 1569 | { |
Patrick McHardy | d1db275 | 2010-05-11 14:40:55 +0200 | [diff] [blame] | 1570 | int err = -EACCES; |
Benjamin Thery | 8229efd | 2008-12-10 16:30:15 -0800 | [diff] [blame] | 1571 | struct net *net = sock_net(sk); |
Yuval Mintz | b70432f | 2018-02-28 23:29:32 +0200 | [diff] [blame] | 1572 | struct mr_table *mrt; |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 1573 | |
Francesco Ruggeri | 338d182 | 2017-11-08 11:23:46 -0800 | [diff] [blame] | 1574 | if (sk->sk_type != SOCK_RAW || |
| 1575 | inet_sk(sk)->inet_num != IPPROTO_ICMPV6) |
| 1576 | return err; |
| 1577 | |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 1578 | rtnl_lock(); |
Patrick McHardy | d1db275 | 2010-05-11 14:40:55 +0200 | [diff] [blame] | 1579 | ip6mr_for_each_table(mrt, net) { |
Yuval Mintz | b70432f | 2018-02-28 23:29:32 +0200 | [diff] [blame] | 1580 | if (sk == rtnl_dereference(mrt->mroute_sk)) { |
Patrick McHardy | d1db275 | 2010-05-11 14:40:55 +0200 | [diff] [blame] | 1581 | write_lock_bh(&mrt_lock); |
Yuval Mintz | b70432f | 2018-02-28 23:29:32 +0200 | [diff] [blame] | 1582 | RCU_INIT_POINTER(mrt->mroute_sk, NULL); |
Eric Dumazet | a366e30 | 2018-03-07 08:43:19 -0800 | [diff] [blame] | 1583 | /* Note that mroute_sk had SOCK_RCU_FREE set, |
| 1584 | * so the RCU grace period before sk freeing |
| 1585 | * is guaranteed by sk_destruct() |
| 1586 | */ |
Patrick McHardy | d1db275 | 2010-05-11 14:40:55 +0200 | [diff] [blame] | 1587 | net->ipv6.devconf_all->mc_forwarding--; |
Eric Dumazet | 927265b | 2016-07-08 05:46:04 +0200 | [diff] [blame] | 1588 | write_unlock_bh(&mrt_lock); |
David Ahern | 85b3daa | 2017-03-28 14:28:04 -0700 | [diff] [blame] | 1589 | inet6_netconf_notify_devconf(net, RTM_NEWNETCONF, |
Nicolas Dichtel | d67b8c6 | 2012-12-04 01:13:35 +0000 | [diff] [blame] | 1590 | NETCONFA_MC_FORWARDING, |
| 1591 | NETCONFA_IFINDEX_ALL, |
| 1592 | net->ipv6.devconf_all); |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 1593 | |
Callum Sinclair | ca8d479 | 2019-02-18 10:07:52 +1300 | [diff] [blame] | 1594 | mroute_clean_tables(mrt, MRT6_FLUSH_MIFS | MRT6_FLUSH_MFC); |
Patrick McHardy | d1db275 | 2010-05-11 14:40:55 +0200 | [diff] [blame] | 1595 | err = 0; |
| 1596 | break; |
| 1597 | } |
| 1598 | } |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 1599 | rtnl_unlock(); |
| 1600 | |
| 1601 | return err; |
| 1602 | } |
| 1603 | |
Yuval Mintz | 8571ab4 | 2018-02-28 23:29:30 +0200 | [diff] [blame] | 1604 | bool mroute6_is_socket(struct net *net, struct sk_buff *skb) |
Patrick McHardy | 6bd5214 | 2010-05-11 14:40:53 +0200 | [diff] [blame] | 1605 | { |
Yuval Mintz | b70432f | 2018-02-28 23:29:32 +0200 | [diff] [blame] | 1606 | struct mr_table *mrt; |
David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 1607 | struct flowi6 fl6 = { |
Julian Anastasov | e374c61 | 2014-04-28 10:51:56 +0300 | [diff] [blame] | 1608 | .flowi6_iif = skb->skb_iif ? : LOOPBACK_IFINDEX, |
David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 1609 | .flowi6_oif = skb->dev->ifindex, |
| 1610 | .flowi6_mark = skb->mark, |
Patrick McHardy | d1db275 | 2010-05-11 14:40:55 +0200 | [diff] [blame] | 1611 | }; |
| 1612 | |
David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 1613 | if (ip6mr_fib_lookup(net, &fl6, &mrt) < 0) |
Patrick McHardy | d1db275 | 2010-05-11 14:40:55 +0200 | [diff] [blame] | 1614 | return NULL; |
Patrick McHardy | 6bd5214 | 2010-05-11 14:40:53 +0200 | [diff] [blame] | 1615 | |
Yuval Mintz | b70432f | 2018-02-28 23:29:32 +0200 | [diff] [blame] | 1616 | return rcu_access_pointer(mrt->mroute_sk); |
Patrick McHardy | 6bd5214 | 2010-05-11 14:40:53 +0200 | [diff] [blame] | 1617 | } |
Yuval Mintz | 8571ab4 | 2018-02-28 23:29:30 +0200 | [diff] [blame] | 1618 | EXPORT_SYMBOL(mroute6_is_socket); |
Patrick McHardy | 6bd5214 | 2010-05-11 14:40:53 +0200 | [diff] [blame] | 1619 | |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 1620 | /* |
| 1621 | * Socket options and virtual interface manipulation. The whole |
| 1622 | * virtual interface system is a complete heap, but unfortunately |
| 1623 | * that's how BSD mrouted happens to think. Maybe one day with a proper |
| 1624 | * MOSPF/PIM router set up we can clean this up. |
| 1625 | */ |
| 1626 | |
Christoph Hellwig | b43c615 | 2020-07-23 08:08:59 +0200 | [diff] [blame] | 1627 | int ip6_mroute_setsockopt(struct sock *sk, int optname, sockptr_t optval, |
| 1628 | unsigned int optlen) |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 1629 | { |
Nicolas Dichtel | 660b26d | 2013-01-21 06:00:26 +0000 | [diff] [blame] | 1630 | int ret, parent = 0; |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 1631 | struct mif6ctl vif; |
| 1632 | struct mf6cctl mfc; |
| 1633 | mifi_t mifi; |
Benjamin Thery | 8229efd | 2008-12-10 16:30:15 -0800 | [diff] [blame] | 1634 | struct net *net = sock_net(sk); |
Yuval Mintz | b70432f | 2018-02-28 23:29:32 +0200 | [diff] [blame] | 1635 | struct mr_table *mrt; |
Patrick McHardy | d1db275 | 2010-05-11 14:40:55 +0200 | [diff] [blame] | 1636 | |
Xin Long | 99253eb | 2017-02-24 16:29:06 +0800 | [diff] [blame] | 1637 | if (sk->sk_type != SOCK_RAW || |
| 1638 | inet_sk(sk)->inet_num != IPPROTO_ICMPV6) |
| 1639 | return -EOPNOTSUPP; |
| 1640 | |
Patrick McHardy | d1db275 | 2010-05-11 14:40:55 +0200 | [diff] [blame] | 1641 | mrt = ip6mr_get_table(net, raw6_sk(sk)->ip6mr_table ? : RT6_TABLE_DFLT); |
Ian Morris | 63159f2 | 2015-03-29 14:00:04 +0100 | [diff] [blame] | 1642 | if (!mrt) |
Patrick McHardy | d1db275 | 2010-05-11 14:40:55 +0200 | [diff] [blame] | 1643 | return -ENOENT; |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 1644 | |
| 1645 | if (optname != MRT6_INIT) { |
Yuval Mintz | b70432f | 2018-02-28 23:29:32 +0200 | [diff] [blame] | 1646 | if (sk != rcu_access_pointer(mrt->mroute_sk) && |
Yuval Mintz | 8571ab4 | 2018-02-28 23:29:30 +0200 | [diff] [blame] | 1647 | !ns_capable(net->user_ns, CAP_NET_ADMIN)) |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 1648 | return -EACCES; |
| 1649 | } |
| 1650 | |
| 1651 | switch (optname) { |
| 1652 | case MRT6_INIT: |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 1653 | if (optlen < sizeof(int)) |
| 1654 | return -EINVAL; |
| 1655 | |
Patrick McHardy | 6bd5214 | 2010-05-11 14:40:53 +0200 | [diff] [blame] | 1656 | return ip6mr_sk_init(mrt, sk); |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 1657 | |
| 1658 | case MRT6_DONE: |
| 1659 | return ip6mr_sk_done(sk); |
| 1660 | |
| 1661 | case MRT6_ADD_MIF: |
| 1662 | if (optlen < sizeof(vif)) |
| 1663 | return -EINVAL; |
Christoph Hellwig | b43c615 | 2020-07-23 08:08:59 +0200 | [diff] [blame] | 1664 | if (copy_from_sockptr(&vif, optval, sizeof(vif))) |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 1665 | return -EFAULT; |
Rami Rosen | 6ac7eb0 | 2008-04-10 12:40:10 +0300 | [diff] [blame] | 1666 | if (vif.mif6c_mifi >= MAXMIFS) |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 1667 | return -ENFILE; |
| 1668 | rtnl_lock(); |
Yuval Mintz | 8571ab4 | 2018-02-28 23:29:30 +0200 | [diff] [blame] | 1669 | ret = mif6_add(net, mrt, &vif, |
Yuval Mintz | b70432f | 2018-02-28 23:29:32 +0200 | [diff] [blame] | 1670 | sk == rtnl_dereference(mrt->mroute_sk)); |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 1671 | rtnl_unlock(); |
| 1672 | return ret; |
| 1673 | |
| 1674 | case MRT6_DEL_MIF: |
| 1675 | if (optlen < sizeof(mifi_t)) |
| 1676 | return -EINVAL; |
Christoph Hellwig | b43c615 | 2020-07-23 08:08:59 +0200 | [diff] [blame] | 1677 | if (copy_from_sockptr(&mifi, optval, sizeof(mifi_t))) |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 1678 | return -EFAULT; |
| 1679 | rtnl_lock(); |
Nikolay Aleksandrov | 723b929 | 2017-04-21 20:42:16 +0300 | [diff] [blame] | 1680 | ret = mif6_delete(mrt, mifi, 0, NULL); |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 1681 | rtnl_unlock(); |
| 1682 | return ret; |
| 1683 | |
| 1684 | /* |
| 1685 | * Manipulate the forwarding caches. These live |
| 1686 | * in a sort of kernel/user symbiosis. |
| 1687 | */ |
| 1688 | case MRT6_ADD_MFC: |
| 1689 | case MRT6_DEL_MFC: |
Nicolas Dichtel | 660b26d | 2013-01-21 06:00:26 +0000 | [diff] [blame] | 1690 | parent = -1; |
Joe Perches | a8eceea | 2020-03-12 15:50:22 -0700 | [diff] [blame] | 1691 | fallthrough; |
Nicolas Dichtel | 660b26d | 2013-01-21 06:00:26 +0000 | [diff] [blame] | 1692 | case MRT6_ADD_MFC_PROXY: |
| 1693 | case MRT6_DEL_MFC_PROXY: |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 1694 | if (optlen < sizeof(mfc)) |
| 1695 | return -EINVAL; |
Christoph Hellwig | b43c615 | 2020-07-23 08:08:59 +0200 | [diff] [blame] | 1696 | if (copy_from_sockptr(&mfc, optval, sizeof(mfc))) |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 1697 | return -EFAULT; |
Nicolas Dichtel | 660b26d | 2013-01-21 06:00:26 +0000 | [diff] [blame] | 1698 | if (parent == 0) |
| 1699 | parent = mfc.mf6cc_parent; |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 1700 | rtnl_lock(); |
Nicolas Dichtel | 660b26d | 2013-01-21 06:00:26 +0000 | [diff] [blame] | 1701 | if (optname == MRT6_DEL_MFC || optname == MRT6_DEL_MFC_PROXY) |
| 1702 | ret = ip6mr_mfc_delete(mrt, &mfc, parent); |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 1703 | else |
Nicolas Dichtel | 660b26d | 2013-01-21 06:00:26 +0000 | [diff] [blame] | 1704 | ret = ip6mr_mfc_add(net, mrt, &mfc, |
Yuval Mintz | 8571ab4 | 2018-02-28 23:29:30 +0200 | [diff] [blame] | 1705 | sk == |
Yuval Mintz | b70432f | 2018-02-28 23:29:32 +0200 | [diff] [blame] | 1706 | rtnl_dereference(mrt->mroute_sk), |
Yuval Mintz | 8571ab4 | 2018-02-28 23:29:30 +0200 | [diff] [blame] | 1707 | parent); |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 1708 | rtnl_unlock(); |
| 1709 | return ret; |
| 1710 | |
Callum Sinclair | ca8d479 | 2019-02-18 10:07:52 +1300 | [diff] [blame] | 1711 | case MRT6_FLUSH: |
| 1712 | { |
| 1713 | int flags; |
| 1714 | |
| 1715 | if (optlen != sizeof(flags)) |
| 1716 | return -EINVAL; |
Christoph Hellwig | b43c615 | 2020-07-23 08:08:59 +0200 | [diff] [blame] | 1717 | if (copy_from_sockptr(&flags, optval, sizeof(flags))) |
Callum Sinclair | ca8d479 | 2019-02-18 10:07:52 +1300 | [diff] [blame] | 1718 | return -EFAULT; |
| 1719 | rtnl_lock(); |
| 1720 | mroute_clean_tables(mrt, flags); |
| 1721 | rtnl_unlock(); |
| 1722 | return 0; |
| 1723 | } |
| 1724 | |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 1725 | /* |
YOSHIFUJI Hideaki | 14fb64e | 2008-04-03 09:22:54 +0900 | [diff] [blame] | 1726 | * Control PIM assert (to activate pim will activate assert) |
| 1727 | */ |
| 1728 | case MRT6_ASSERT: |
| 1729 | { |
| 1730 | int v; |
Joe Perches | 03f52a0 | 2012-11-25 18:26:34 +0000 | [diff] [blame] | 1731 | |
| 1732 | if (optlen != sizeof(v)) |
| 1733 | return -EINVAL; |
Christoph Hellwig | b43c615 | 2020-07-23 08:08:59 +0200 | [diff] [blame] | 1734 | if (copy_from_sockptr(&v, optval, sizeof(v))) |
YOSHIFUJI Hideaki | 14fb64e | 2008-04-03 09:22:54 +0900 | [diff] [blame] | 1735 | return -EFAULT; |
Joe Perches | 53d6841 | 2012-11-25 09:35:30 +0000 | [diff] [blame] | 1736 | mrt->mroute_do_assert = v; |
YOSHIFUJI Hideaki | 14fb64e | 2008-04-03 09:22:54 +0900 | [diff] [blame] | 1737 | return 0; |
| 1738 | } |
| 1739 | |
| 1740 | #ifdef CONFIG_IPV6_PIMSM_V2 |
| 1741 | case MRT6_PIM: |
| 1742 | { |
YOSHIFUJI Hideaki | a9f83bf | 2008-04-10 15:41:28 +0900 | [diff] [blame] | 1743 | int v; |
Joe Perches | 03f52a0 | 2012-11-25 18:26:34 +0000 | [diff] [blame] | 1744 | |
| 1745 | if (optlen != sizeof(v)) |
| 1746 | return -EINVAL; |
Christoph Hellwig | b43c615 | 2020-07-23 08:08:59 +0200 | [diff] [blame] | 1747 | if (copy_from_sockptr(&v, optval, sizeof(v))) |
YOSHIFUJI Hideaki | 14fb64e | 2008-04-03 09:22:54 +0900 | [diff] [blame] | 1748 | return -EFAULT; |
| 1749 | v = !!v; |
| 1750 | rtnl_lock(); |
| 1751 | ret = 0; |
Patrick McHardy | 6bd5214 | 2010-05-11 14:40:53 +0200 | [diff] [blame] | 1752 | if (v != mrt->mroute_do_pim) { |
| 1753 | mrt->mroute_do_pim = v; |
| 1754 | mrt->mroute_do_assert = v; |
YOSHIFUJI Hideaki | 14fb64e | 2008-04-03 09:22:54 +0900 | [diff] [blame] | 1755 | } |
| 1756 | rtnl_unlock(); |
| 1757 | return ret; |
| 1758 | } |
| 1759 | |
| 1760 | #endif |
Patrick McHardy | d1db275 | 2010-05-11 14:40:55 +0200 | [diff] [blame] | 1761 | #ifdef CONFIG_IPV6_MROUTE_MULTIPLE_TABLES |
| 1762 | case MRT6_TABLE: |
| 1763 | { |
| 1764 | u32 v; |
| 1765 | |
| 1766 | if (optlen != sizeof(u32)) |
| 1767 | return -EINVAL; |
Christoph Hellwig | b43c615 | 2020-07-23 08:08:59 +0200 | [diff] [blame] | 1768 | if (copy_from_sockptr(&v, optval, sizeof(v))) |
Patrick McHardy | d1db275 | 2010-05-11 14:40:55 +0200 | [diff] [blame] | 1769 | return -EFAULT; |
Dan Carpenter | 75356a8 | 2013-01-23 20:38:34 +0000 | [diff] [blame] | 1770 | /* "pim6reg%u" should not exceed 16 bytes (IFNAMSIZ) */ |
| 1771 | if (v != RT_TABLE_DEFAULT && v >= 100000000) |
| 1772 | return -EINVAL; |
Yuval Mintz | b70432f | 2018-02-28 23:29:32 +0200 | [diff] [blame] | 1773 | if (sk == rcu_access_pointer(mrt->mroute_sk)) |
Patrick McHardy | d1db275 | 2010-05-11 14:40:55 +0200 | [diff] [blame] | 1774 | return -EBUSY; |
| 1775 | |
| 1776 | rtnl_lock(); |
| 1777 | ret = 0; |
Sabrina Dubroca | e783bb0 | 2018-06-05 15:02:00 +0200 | [diff] [blame] | 1778 | mrt = ip6mr_new_table(net, v); |
| 1779 | if (IS_ERR(mrt)) |
| 1780 | ret = PTR_ERR(mrt); |
Sabrina Dubroca | 848235e | 2018-06-05 15:01:59 +0200 | [diff] [blame] | 1781 | else |
| 1782 | raw6_sk(sk)->ip6mr_table = v; |
Patrick McHardy | d1db275 | 2010-05-11 14:40:55 +0200 | [diff] [blame] | 1783 | rtnl_unlock(); |
| 1784 | return ret; |
| 1785 | } |
| 1786 | #endif |
YOSHIFUJI Hideaki | 14fb64e | 2008-04-03 09:22:54 +0900 | [diff] [blame] | 1787 | /* |
Rami Rosen | 7d120c5 | 2008-04-23 14:35:13 +0300 | [diff] [blame] | 1788 | * Spurious command, or MRT6_VERSION which you cannot |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 1789 | * set. |
| 1790 | */ |
| 1791 | default: |
| 1792 | return -ENOPROTOOPT; |
| 1793 | } |
| 1794 | } |
| 1795 | |
| 1796 | /* |
| 1797 | * Getsock opt support for the multicast routing system. |
| 1798 | */ |
| 1799 | |
| 1800 | int ip6_mroute_getsockopt(struct sock *sk, int optname, char __user *optval, |
| 1801 | int __user *optlen) |
| 1802 | { |
| 1803 | int olr; |
| 1804 | int val; |
Benjamin Thery | 8229efd | 2008-12-10 16:30:15 -0800 | [diff] [blame] | 1805 | struct net *net = sock_net(sk); |
Yuval Mintz | b70432f | 2018-02-28 23:29:32 +0200 | [diff] [blame] | 1806 | struct mr_table *mrt; |
Patrick McHardy | d1db275 | 2010-05-11 14:40:55 +0200 | [diff] [blame] | 1807 | |
Xin Long | 99253eb | 2017-02-24 16:29:06 +0800 | [diff] [blame] | 1808 | if (sk->sk_type != SOCK_RAW || |
| 1809 | inet_sk(sk)->inet_num != IPPROTO_ICMPV6) |
| 1810 | return -EOPNOTSUPP; |
| 1811 | |
Patrick McHardy | d1db275 | 2010-05-11 14:40:55 +0200 | [diff] [blame] | 1812 | mrt = ip6mr_get_table(net, raw6_sk(sk)->ip6mr_table ? : RT6_TABLE_DFLT); |
Ian Morris | 63159f2 | 2015-03-29 14:00:04 +0100 | [diff] [blame] | 1813 | if (!mrt) |
Patrick McHardy | d1db275 | 2010-05-11 14:40:55 +0200 | [diff] [blame] | 1814 | return -ENOENT; |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 1815 | |
| 1816 | switch (optname) { |
| 1817 | case MRT6_VERSION: |
| 1818 | val = 0x0305; |
| 1819 | break; |
YOSHIFUJI Hideaki | 14fb64e | 2008-04-03 09:22:54 +0900 | [diff] [blame] | 1820 | #ifdef CONFIG_IPV6_PIMSM_V2 |
| 1821 | case MRT6_PIM: |
Patrick McHardy | 6bd5214 | 2010-05-11 14:40:53 +0200 | [diff] [blame] | 1822 | val = mrt->mroute_do_pim; |
YOSHIFUJI Hideaki | 14fb64e | 2008-04-03 09:22:54 +0900 | [diff] [blame] | 1823 | break; |
| 1824 | #endif |
| 1825 | case MRT6_ASSERT: |
Patrick McHardy | 6bd5214 | 2010-05-11 14:40:53 +0200 | [diff] [blame] | 1826 | val = mrt->mroute_do_assert; |
YOSHIFUJI Hideaki | 14fb64e | 2008-04-03 09:22:54 +0900 | [diff] [blame] | 1827 | break; |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 1828 | default: |
| 1829 | return -ENOPROTOOPT; |
| 1830 | } |
| 1831 | |
| 1832 | if (get_user(olr, optlen)) |
| 1833 | return -EFAULT; |
| 1834 | |
| 1835 | olr = min_t(int, olr, sizeof(int)); |
| 1836 | if (olr < 0) |
| 1837 | return -EINVAL; |
| 1838 | |
| 1839 | if (put_user(olr, optlen)) |
| 1840 | return -EFAULT; |
| 1841 | if (copy_to_user(optval, &val, olr)) |
| 1842 | return -EFAULT; |
| 1843 | return 0; |
| 1844 | } |
| 1845 | |
| 1846 | /* |
| 1847 | * The IP multicast ioctl support routines. |
| 1848 | */ |
| 1849 | |
| 1850 | int ip6mr_ioctl(struct sock *sk, int cmd, void __user *arg) |
| 1851 | { |
| 1852 | struct sioc_sg_req6 sr; |
| 1853 | struct sioc_mif_req6 vr; |
Yuval Mintz | 6853f21 | 2018-02-28 23:29:29 +0200 | [diff] [blame] | 1854 | struct vif_device *vif; |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 1855 | struct mfc6_cache *c; |
Benjamin Thery | 8229efd | 2008-12-10 16:30:15 -0800 | [diff] [blame] | 1856 | struct net *net = sock_net(sk); |
Yuval Mintz | b70432f | 2018-02-28 23:29:32 +0200 | [diff] [blame] | 1857 | struct mr_table *mrt; |
Patrick McHardy | d1db275 | 2010-05-11 14:40:55 +0200 | [diff] [blame] | 1858 | |
| 1859 | mrt = ip6mr_get_table(net, raw6_sk(sk)->ip6mr_table ? : RT6_TABLE_DFLT); |
Ian Morris | 63159f2 | 2015-03-29 14:00:04 +0100 | [diff] [blame] | 1860 | if (!mrt) |
Patrick McHardy | d1db275 | 2010-05-11 14:40:55 +0200 | [diff] [blame] | 1861 | return -ENOENT; |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 1862 | |
| 1863 | switch (cmd) { |
| 1864 | case SIOCGETMIFCNT_IN6: |
| 1865 | if (copy_from_user(&vr, arg, sizeof(vr))) |
| 1866 | return -EFAULT; |
Patrick McHardy | 6bd5214 | 2010-05-11 14:40:53 +0200 | [diff] [blame] | 1867 | if (vr.mifi >= mrt->maxvif) |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 1868 | return -EINVAL; |
Gustavo A. R. Silva | 69d2c86 | 2018-12-11 14:10:08 -0600 | [diff] [blame] | 1869 | vr.mifi = array_index_nospec(vr.mifi, mrt->maxvif); |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 1870 | read_lock(&mrt_lock); |
Yuval Mintz | b70432f | 2018-02-28 23:29:32 +0200 | [diff] [blame] | 1871 | vif = &mrt->vif_table[vr.mifi]; |
| 1872 | if (VIF_EXISTS(mrt, vr.mifi)) { |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 1873 | vr.icount = vif->pkt_in; |
| 1874 | vr.ocount = vif->pkt_out; |
| 1875 | vr.ibytes = vif->bytes_in; |
| 1876 | vr.obytes = vif->bytes_out; |
| 1877 | read_unlock(&mrt_lock); |
| 1878 | |
| 1879 | if (copy_to_user(arg, &vr, sizeof(vr))) |
| 1880 | return -EFAULT; |
| 1881 | return 0; |
| 1882 | } |
| 1883 | read_unlock(&mrt_lock); |
| 1884 | return -EADDRNOTAVAIL; |
| 1885 | case SIOCGETSGCNT_IN6: |
| 1886 | if (copy_from_user(&sr, arg, sizeof(sr))) |
| 1887 | return -EFAULT; |
| 1888 | |
Yuval Mintz | 87c418b | 2018-02-28 23:29:31 +0200 | [diff] [blame] | 1889 | rcu_read_lock(); |
Patrick McHardy | 6bd5214 | 2010-05-11 14:40:53 +0200 | [diff] [blame] | 1890 | c = ip6mr_cache_find(mrt, &sr.src.sin6_addr, &sr.grp.sin6_addr); |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 1891 | if (c) { |
Yuval Mintz | 494fff5 | 2018-02-28 23:29:34 +0200 | [diff] [blame] | 1892 | sr.pktcnt = c->_c.mfc_un.res.pkt; |
| 1893 | sr.bytecnt = c->_c.mfc_un.res.bytes; |
| 1894 | sr.wrong_if = c->_c.mfc_un.res.wrong_if; |
Yuval Mintz | 87c418b | 2018-02-28 23:29:31 +0200 | [diff] [blame] | 1895 | rcu_read_unlock(); |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 1896 | |
| 1897 | if (copy_to_user(arg, &sr, sizeof(sr))) |
| 1898 | return -EFAULT; |
| 1899 | return 0; |
| 1900 | } |
Yuval Mintz | 87c418b | 2018-02-28 23:29:31 +0200 | [diff] [blame] | 1901 | rcu_read_unlock(); |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 1902 | return -EADDRNOTAVAIL; |
| 1903 | default: |
| 1904 | return -ENOIOCTLCMD; |
| 1905 | } |
| 1906 | } |
| 1907 | |
David S. Miller | e2d5776 | 2011-02-03 17:59:32 -0800 | [diff] [blame] | 1908 | #ifdef CONFIG_COMPAT |
| 1909 | struct compat_sioc_sg_req6 { |
| 1910 | struct sockaddr_in6 src; |
| 1911 | struct sockaddr_in6 grp; |
| 1912 | compat_ulong_t pktcnt; |
| 1913 | compat_ulong_t bytecnt; |
| 1914 | compat_ulong_t wrong_if; |
| 1915 | }; |
| 1916 | |
| 1917 | struct compat_sioc_mif_req6 { |
| 1918 | mifi_t mifi; |
| 1919 | compat_ulong_t icount; |
| 1920 | compat_ulong_t ocount; |
| 1921 | compat_ulong_t ibytes; |
| 1922 | compat_ulong_t obytes; |
| 1923 | }; |
| 1924 | |
| 1925 | int ip6mr_compat_ioctl(struct sock *sk, unsigned int cmd, void __user *arg) |
| 1926 | { |
| 1927 | struct compat_sioc_sg_req6 sr; |
| 1928 | struct compat_sioc_mif_req6 vr; |
Yuval Mintz | 6853f21 | 2018-02-28 23:29:29 +0200 | [diff] [blame] | 1929 | struct vif_device *vif; |
David S. Miller | e2d5776 | 2011-02-03 17:59:32 -0800 | [diff] [blame] | 1930 | struct mfc6_cache *c; |
| 1931 | struct net *net = sock_net(sk); |
Yuval Mintz | b70432f | 2018-02-28 23:29:32 +0200 | [diff] [blame] | 1932 | struct mr_table *mrt; |
David S. Miller | e2d5776 | 2011-02-03 17:59:32 -0800 | [diff] [blame] | 1933 | |
| 1934 | mrt = ip6mr_get_table(net, raw6_sk(sk)->ip6mr_table ? : RT6_TABLE_DFLT); |
Ian Morris | 63159f2 | 2015-03-29 14:00:04 +0100 | [diff] [blame] | 1935 | if (!mrt) |
David S. Miller | e2d5776 | 2011-02-03 17:59:32 -0800 | [diff] [blame] | 1936 | return -ENOENT; |
| 1937 | |
| 1938 | switch (cmd) { |
| 1939 | case SIOCGETMIFCNT_IN6: |
| 1940 | if (copy_from_user(&vr, arg, sizeof(vr))) |
| 1941 | return -EFAULT; |
| 1942 | if (vr.mifi >= mrt->maxvif) |
| 1943 | return -EINVAL; |
Gustavo A. R. Silva | 69d2c86 | 2018-12-11 14:10:08 -0600 | [diff] [blame] | 1944 | vr.mifi = array_index_nospec(vr.mifi, mrt->maxvif); |
David S. Miller | e2d5776 | 2011-02-03 17:59:32 -0800 | [diff] [blame] | 1945 | read_lock(&mrt_lock); |
Yuval Mintz | b70432f | 2018-02-28 23:29:32 +0200 | [diff] [blame] | 1946 | vif = &mrt->vif_table[vr.mifi]; |
| 1947 | if (VIF_EXISTS(mrt, vr.mifi)) { |
David S. Miller | e2d5776 | 2011-02-03 17:59:32 -0800 | [diff] [blame] | 1948 | vr.icount = vif->pkt_in; |
| 1949 | vr.ocount = vif->pkt_out; |
| 1950 | vr.ibytes = vif->bytes_in; |
| 1951 | vr.obytes = vif->bytes_out; |
| 1952 | read_unlock(&mrt_lock); |
| 1953 | |
| 1954 | if (copy_to_user(arg, &vr, sizeof(vr))) |
| 1955 | return -EFAULT; |
| 1956 | return 0; |
| 1957 | } |
| 1958 | read_unlock(&mrt_lock); |
| 1959 | return -EADDRNOTAVAIL; |
| 1960 | case SIOCGETSGCNT_IN6: |
| 1961 | if (copy_from_user(&sr, arg, sizeof(sr))) |
| 1962 | return -EFAULT; |
| 1963 | |
Yuval Mintz | 87c418b | 2018-02-28 23:29:31 +0200 | [diff] [blame] | 1964 | rcu_read_lock(); |
David S. Miller | e2d5776 | 2011-02-03 17:59:32 -0800 | [diff] [blame] | 1965 | c = ip6mr_cache_find(mrt, &sr.src.sin6_addr, &sr.grp.sin6_addr); |
| 1966 | if (c) { |
Yuval Mintz | 494fff5 | 2018-02-28 23:29:34 +0200 | [diff] [blame] | 1967 | sr.pktcnt = c->_c.mfc_un.res.pkt; |
| 1968 | sr.bytecnt = c->_c.mfc_un.res.bytes; |
| 1969 | sr.wrong_if = c->_c.mfc_un.res.wrong_if; |
Yuval Mintz | 87c418b | 2018-02-28 23:29:31 +0200 | [diff] [blame] | 1970 | rcu_read_unlock(); |
David S. Miller | e2d5776 | 2011-02-03 17:59:32 -0800 | [diff] [blame] | 1971 | |
| 1972 | if (copy_to_user(arg, &sr, sizeof(sr))) |
| 1973 | return -EFAULT; |
| 1974 | return 0; |
| 1975 | } |
Yuval Mintz | 87c418b | 2018-02-28 23:29:31 +0200 | [diff] [blame] | 1976 | rcu_read_unlock(); |
David S. Miller | e2d5776 | 2011-02-03 17:59:32 -0800 | [diff] [blame] | 1977 | return -EADDRNOTAVAIL; |
| 1978 | default: |
| 1979 | return -ENOIOCTLCMD; |
| 1980 | } |
| 1981 | } |
| 1982 | #endif |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 1983 | |
Eric W. Biederman | 0c4b51f | 2015-09-15 20:04:18 -0500 | [diff] [blame] | 1984 | static inline int ip6mr_forward2_finish(struct net *net, struct sock *sk, struct sk_buff *skb) |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 1985 | { |
Ido Schimmel | 87c11f1 | 2019-03-03 07:34:57 +0000 | [diff] [blame] | 1986 | IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)), |
| 1987 | IPSTATS_MIB_OUTFORWDATAGRAMS); |
| 1988 | IP6_ADD_STATS(net, ip6_dst_idev(skb_dst(skb)), |
| 1989 | IPSTATS_MIB_OUTOCTETS, skb->len); |
Eric W. Biederman | 13206b6 | 2015-10-07 16:48:35 -0500 | [diff] [blame] | 1990 | return dst_output(net, sk, skb); |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 1991 | } |
| 1992 | |
| 1993 | /* |
| 1994 | * Processing handlers for ip6mr_forward |
| 1995 | */ |
| 1996 | |
Yuval Mintz | b70432f | 2018-02-28 23:29:32 +0200 | [diff] [blame] | 1997 | static int ip6mr_forward2(struct net *net, struct mr_table *mrt, |
David Ahern | f5c6dfd | 2018-12-17 15:36:11 -0800 | [diff] [blame] | 1998 | struct sk_buff *skb, int vifi) |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 1999 | { |
| 2000 | struct ipv6hdr *ipv6h; |
Yuval Mintz | b70432f | 2018-02-28 23:29:32 +0200 | [diff] [blame] | 2001 | struct vif_device *vif = &mrt->vif_table[vifi]; |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 2002 | struct net_device *dev; |
| 2003 | struct dst_entry *dst; |
David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 2004 | struct flowi6 fl6; |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 2005 | |
Ian Morris | 63159f2 | 2015-03-29 14:00:04 +0100 | [diff] [blame] | 2006 | if (!vif->dev) |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 2007 | goto out_free; |
| 2008 | |
YOSHIFUJI Hideaki | 14fb64e | 2008-04-03 09:22:54 +0900 | [diff] [blame] | 2009 | #ifdef CONFIG_IPV6_PIMSM_V2 |
| 2010 | if (vif->flags & MIFF_REGISTER) { |
| 2011 | vif->pkt_out++; |
| 2012 | vif->bytes_out += skb->len; |
Pavel Emelyanov | dc58c78 | 2008-05-21 14:17:54 -0700 | [diff] [blame] | 2013 | vif->dev->stats.tx_bytes += skb->len; |
| 2014 | vif->dev->stats.tx_packets++; |
Patrick McHardy | 6bd5214 | 2010-05-11 14:40:53 +0200 | [diff] [blame] | 2015 | ip6mr_cache_report(mrt, skb, vifi, MRT6MSG_WHOLEPKT); |
Ilpo Järvinen | 8da73b7 | 2008-12-14 23:15:49 -0800 | [diff] [blame] | 2016 | goto out_free; |
YOSHIFUJI Hideaki | 14fb64e | 2008-04-03 09:22:54 +0900 | [diff] [blame] | 2017 | } |
| 2018 | #endif |
| 2019 | |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 2020 | ipv6h = ipv6_hdr(skb); |
| 2021 | |
David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 2022 | fl6 = (struct flowi6) { |
| 2023 | .flowi6_oif = vif->link, |
| 2024 | .daddr = ipv6h->daddr, |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 2025 | }; |
| 2026 | |
David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 2027 | dst = ip6_route_output(net, NULL, &fl6); |
RongQing.Li | 5095d64 | 2012-02-21 22:10:49 +0000 | [diff] [blame] | 2028 | if (dst->error) { |
| 2029 | dst_release(dst); |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 2030 | goto out_free; |
RongQing.Li | 5095d64 | 2012-02-21 22:10:49 +0000 | [diff] [blame] | 2031 | } |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 2032 | |
Eric Dumazet | adf3090 | 2009-06-02 05:19:30 +0000 | [diff] [blame] | 2033 | skb_dst_drop(skb); |
| 2034 | skb_dst_set(skb, dst); |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 2035 | |
| 2036 | /* |
| 2037 | * RFC1584 teaches, that DVMRP/PIM router must deliver packets locally |
| 2038 | * not only before forwarding, but after forwarding on all output |
| 2039 | * interfaces. It is clear, if mrouter runs a multicasting |
| 2040 | * program, it should receive packets not depending to what interface |
| 2041 | * program is joined. |
| 2042 | * If we will not make it, the program will have to join on all |
| 2043 | * interfaces. On the other hand, multihoming host (or router, but |
| 2044 | * not mrouter) cannot join to more than one interface - it will |
| 2045 | * result in receiving multiple packets. |
| 2046 | */ |
| 2047 | dev = vif->dev; |
| 2048 | skb->dev = dev; |
| 2049 | vif->pkt_out++; |
| 2050 | vif->bytes_out += skb->len; |
| 2051 | |
| 2052 | /* We are about to write */ |
| 2053 | /* XXX: extension headers? */ |
| 2054 | if (skb_cow(skb, sizeof(*ipv6h) + LL_RESERVED_SPACE(dev))) |
| 2055 | goto out_free; |
| 2056 | |
| 2057 | ipv6h = ipv6_hdr(skb); |
| 2058 | ipv6h->hop_limit--; |
| 2059 | |
| 2060 | IP6CB(skb)->flags |= IP6SKB_FORWARDED; |
| 2061 | |
Eric W. Biederman | 29a26a5 | 2015-09-15 20:04:16 -0500 | [diff] [blame] | 2062 | return NF_HOOK(NFPROTO_IPV6, NF_INET_FORWARD, |
| 2063 | net, NULL, skb, skb->dev, dev, |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 2064 | ip6mr_forward2_finish); |
| 2065 | |
| 2066 | out_free: |
| 2067 | kfree_skb(skb); |
| 2068 | return 0; |
| 2069 | } |
| 2070 | |
Yuval Mintz | b70432f | 2018-02-28 23:29:32 +0200 | [diff] [blame] | 2071 | static int ip6mr_find_vif(struct mr_table *mrt, struct net_device *dev) |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 2072 | { |
| 2073 | int ct; |
Patrick McHardy | 6bd5214 | 2010-05-11 14:40:53 +0200 | [diff] [blame] | 2074 | |
| 2075 | for (ct = mrt->maxvif - 1; ct >= 0; ct--) { |
Yuval Mintz | b70432f | 2018-02-28 23:29:32 +0200 | [diff] [blame] | 2076 | if (mrt->vif_table[ct].dev == dev) |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 2077 | break; |
| 2078 | } |
| 2079 | return ct; |
| 2080 | } |
| 2081 | |
Yuval Mintz | b70432f | 2018-02-28 23:29:32 +0200 | [diff] [blame] | 2082 | static void ip6_mr_forward(struct net *net, struct mr_table *mrt, |
Patrick Ruddy | e4a38c0 | 2018-10-01 09:41:27 +0100 | [diff] [blame] | 2083 | struct net_device *dev, struct sk_buff *skb, |
| 2084 | struct mfc6_cache *c) |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 2085 | { |
| 2086 | int psend = -1; |
| 2087 | int vif, ct; |
Patrick Ruddy | e4a38c0 | 2018-10-01 09:41:27 +0100 | [diff] [blame] | 2088 | int true_vifi = ip6mr_find_vif(mrt, dev); |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 2089 | |
Yuval Mintz | 494fff5 | 2018-02-28 23:29:34 +0200 | [diff] [blame] | 2090 | vif = c->_c.mfc_parent; |
| 2091 | c->_c.mfc_un.res.pkt++; |
| 2092 | c->_c.mfc_un.res.bytes += skb->len; |
| 2093 | c->_c.mfc_un.res.lastuse = jiffies; |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 2094 | |
Yuval Mintz | 494fff5 | 2018-02-28 23:29:34 +0200 | [diff] [blame] | 2095 | if (ipv6_addr_any(&c->mf6c_origin) && true_vifi >= 0) { |
Nicolas Dichtel | 660b26d | 2013-01-21 06:00:26 +0000 | [diff] [blame] | 2096 | struct mfc6_cache *cache_proxy; |
| 2097 | |
Fabian Frederick | 40dc2ca | 2014-10-29 10:00:26 +0100 | [diff] [blame] | 2098 | /* For an (*,G) entry, we only check that the incoming |
Nicolas Dichtel | 660b26d | 2013-01-21 06:00:26 +0000 | [diff] [blame] | 2099 | * interface is part of the static tree. |
| 2100 | */ |
Yuval Mintz | 87c418b | 2018-02-28 23:29:31 +0200 | [diff] [blame] | 2101 | rcu_read_lock(); |
Yuval Mintz | 845c9a7 | 2018-02-28 23:29:35 +0200 | [diff] [blame] | 2102 | cache_proxy = mr_mfc_find_any_parent(mrt, vif); |
Nicolas Dichtel | 660b26d | 2013-01-21 06:00:26 +0000 | [diff] [blame] | 2103 | if (cache_proxy && |
Yuval Mintz | 494fff5 | 2018-02-28 23:29:34 +0200 | [diff] [blame] | 2104 | cache_proxy->_c.mfc_un.res.ttls[true_vifi] < 255) { |
Yuval Mintz | 87c418b | 2018-02-28 23:29:31 +0200 | [diff] [blame] | 2105 | rcu_read_unlock(); |
Nicolas Dichtel | 660b26d | 2013-01-21 06:00:26 +0000 | [diff] [blame] | 2106 | goto forward; |
Yuval Mintz | 87c418b | 2018-02-28 23:29:31 +0200 | [diff] [blame] | 2107 | } |
| 2108 | rcu_read_unlock(); |
Nicolas Dichtel | 660b26d | 2013-01-21 06:00:26 +0000 | [diff] [blame] | 2109 | } |
| 2110 | |
YOSHIFUJI Hideaki | 14fb64e | 2008-04-03 09:22:54 +0900 | [diff] [blame] | 2111 | /* |
| 2112 | * Wrong interface: drop packet and (maybe) send PIM assert. |
| 2113 | */ |
Patrick Ruddy | e4a38c0 | 2018-10-01 09:41:27 +0100 | [diff] [blame] | 2114 | if (mrt->vif_table[vif].dev != dev) { |
Yuval Mintz | 494fff5 | 2018-02-28 23:29:34 +0200 | [diff] [blame] | 2115 | c->_c.mfc_un.res.wrong_if++; |
YOSHIFUJI Hideaki | 14fb64e | 2008-04-03 09:22:54 +0900 | [diff] [blame] | 2116 | |
Patrick McHardy | 6bd5214 | 2010-05-11 14:40:53 +0200 | [diff] [blame] | 2117 | if (true_vifi >= 0 && mrt->mroute_do_assert && |
YOSHIFUJI Hideaki | 14fb64e | 2008-04-03 09:22:54 +0900 | [diff] [blame] | 2118 | /* pimsm uses asserts, when switching from RPT to SPT, |
| 2119 | so that we cannot check that packet arrived on an oif. |
| 2120 | It is bad, but otherwise we would need to move pretty |
| 2121 | large chunk of pimd to kernel. Ough... --ANK |
| 2122 | */ |
Patrick McHardy | 6bd5214 | 2010-05-11 14:40:53 +0200 | [diff] [blame] | 2123 | (mrt->mroute_do_pim || |
Yuval Mintz | 494fff5 | 2018-02-28 23:29:34 +0200 | [diff] [blame] | 2124 | c->_c.mfc_un.res.ttls[true_vifi] < 255) && |
YOSHIFUJI Hideaki | 14fb64e | 2008-04-03 09:22:54 +0900 | [diff] [blame] | 2125 | time_after(jiffies, |
Yuval Mintz | 494fff5 | 2018-02-28 23:29:34 +0200 | [diff] [blame] | 2126 | c->_c.mfc_un.res.last_assert + |
| 2127 | MFC_ASSERT_THRESH)) { |
| 2128 | c->_c.mfc_un.res.last_assert = jiffies; |
Patrick McHardy | 6bd5214 | 2010-05-11 14:40:53 +0200 | [diff] [blame] | 2129 | ip6mr_cache_report(mrt, skb, true_vifi, MRT6MSG_WRONGMIF); |
YOSHIFUJI Hideaki | 14fb64e | 2008-04-03 09:22:54 +0900 | [diff] [blame] | 2130 | } |
| 2131 | goto dont_forward; |
| 2132 | } |
| 2133 | |
Nicolas Dichtel | 660b26d | 2013-01-21 06:00:26 +0000 | [diff] [blame] | 2134 | forward: |
Yuval Mintz | b70432f | 2018-02-28 23:29:32 +0200 | [diff] [blame] | 2135 | mrt->vif_table[vif].pkt_in++; |
| 2136 | mrt->vif_table[vif].bytes_in += skb->len; |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 2137 | |
| 2138 | /* |
| 2139 | * Forward the frame |
| 2140 | */ |
Yuval Mintz | 494fff5 | 2018-02-28 23:29:34 +0200 | [diff] [blame] | 2141 | if (ipv6_addr_any(&c->mf6c_origin) && |
| 2142 | ipv6_addr_any(&c->mf6c_mcastgrp)) { |
Nicolas Dichtel | 660b26d | 2013-01-21 06:00:26 +0000 | [diff] [blame] | 2143 | if (true_vifi >= 0 && |
Yuval Mintz | 494fff5 | 2018-02-28 23:29:34 +0200 | [diff] [blame] | 2144 | true_vifi != c->_c.mfc_parent && |
Nicolas Dichtel | 660b26d | 2013-01-21 06:00:26 +0000 | [diff] [blame] | 2145 | ipv6_hdr(skb)->hop_limit > |
Yuval Mintz | 494fff5 | 2018-02-28 23:29:34 +0200 | [diff] [blame] | 2146 | c->_c.mfc_un.res.ttls[c->_c.mfc_parent]) { |
Nicolas Dichtel | 660b26d | 2013-01-21 06:00:26 +0000 | [diff] [blame] | 2147 | /* It's an (*,*) entry and the packet is not coming from |
| 2148 | * the upstream: forward the packet to the upstream |
| 2149 | * only. |
| 2150 | */ |
Yuval Mintz | 494fff5 | 2018-02-28 23:29:34 +0200 | [diff] [blame] | 2151 | psend = c->_c.mfc_parent; |
Nicolas Dichtel | 660b26d | 2013-01-21 06:00:26 +0000 | [diff] [blame] | 2152 | goto last_forward; |
| 2153 | } |
| 2154 | goto dont_forward; |
| 2155 | } |
Yuval Mintz | 494fff5 | 2018-02-28 23:29:34 +0200 | [diff] [blame] | 2156 | for (ct = c->_c.mfc_un.res.maxvif - 1; |
| 2157 | ct >= c->_c.mfc_un.res.minvif; ct--) { |
Nicolas Dichtel | 660b26d | 2013-01-21 06:00:26 +0000 | [diff] [blame] | 2158 | /* For (*,G) entry, don't forward to the incoming interface */ |
Yuval Mintz | 494fff5 | 2018-02-28 23:29:34 +0200 | [diff] [blame] | 2159 | if ((!ipv6_addr_any(&c->mf6c_origin) || ct != true_vifi) && |
| 2160 | ipv6_hdr(skb)->hop_limit > c->_c.mfc_un.res.ttls[ct]) { |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 2161 | if (psend != -1) { |
| 2162 | struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC); |
| 2163 | if (skb2) |
David Ahern | f5c6dfd | 2018-12-17 15:36:11 -0800 | [diff] [blame] | 2164 | ip6mr_forward2(net, mrt, skb2, psend); |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 2165 | } |
| 2166 | psend = ct; |
| 2167 | } |
| 2168 | } |
Nicolas Dichtel | 660b26d | 2013-01-21 06:00:26 +0000 | [diff] [blame] | 2169 | last_forward: |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 2170 | if (psend != -1) { |
David Ahern | f5c6dfd | 2018-12-17 15:36:11 -0800 | [diff] [blame] | 2171 | ip6mr_forward2(net, mrt, skb, psend); |
Rami Rosen | 2b52c3a | 2013-07-21 03:00:31 +0300 | [diff] [blame] | 2172 | return; |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 2173 | } |
| 2174 | |
YOSHIFUJI Hideaki | 14fb64e | 2008-04-03 09:22:54 +0900 | [diff] [blame] | 2175 | dont_forward: |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 2176 | kfree_skb(skb); |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 2177 | } |
| 2178 | |
| 2179 | |
| 2180 | /* |
| 2181 | * Multicast packets for forwarding arrive here |
| 2182 | */ |
| 2183 | |
| 2184 | int ip6_mr_input(struct sk_buff *skb) |
| 2185 | { |
| 2186 | struct mfc6_cache *cache; |
Benjamin Thery | 8229efd | 2008-12-10 16:30:15 -0800 | [diff] [blame] | 2187 | struct net *net = dev_net(skb->dev); |
Yuval Mintz | b70432f | 2018-02-28 23:29:32 +0200 | [diff] [blame] | 2188 | struct mr_table *mrt; |
David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 2189 | struct flowi6 fl6 = { |
| 2190 | .flowi6_iif = skb->dev->ifindex, |
| 2191 | .flowi6_mark = skb->mark, |
Patrick McHardy | d1db275 | 2010-05-11 14:40:55 +0200 | [diff] [blame] | 2192 | }; |
| 2193 | int err; |
Patrick Ruddy | e4a38c0 | 2018-10-01 09:41:27 +0100 | [diff] [blame] | 2194 | struct net_device *dev; |
| 2195 | |
| 2196 | /* skb->dev passed in is the master dev for vrfs. |
| 2197 | * Get the proper interface that does have a vif associated with it. |
| 2198 | */ |
| 2199 | dev = skb->dev; |
| 2200 | if (netif_is_l3_master(skb->dev)) { |
| 2201 | dev = dev_get_by_index_rcu(net, IPCB(skb)->iif); |
| 2202 | if (!dev) { |
| 2203 | kfree_skb(skb); |
| 2204 | return -ENODEV; |
| 2205 | } |
| 2206 | } |
Patrick McHardy | d1db275 | 2010-05-11 14:40:55 +0200 | [diff] [blame] | 2207 | |
David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 2208 | err = ip6mr_fib_lookup(net, &fl6, &mrt); |
Ben Greear | 2015de5 | 2011-09-27 15:16:08 -0400 | [diff] [blame] | 2209 | if (err < 0) { |
| 2210 | kfree_skb(skb); |
Patrick McHardy | d1db275 | 2010-05-11 14:40:55 +0200 | [diff] [blame] | 2211 | return err; |
Ben Greear | 2015de5 | 2011-09-27 15:16:08 -0400 | [diff] [blame] | 2212 | } |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 2213 | |
| 2214 | read_lock(&mrt_lock); |
Patrick McHardy | 6bd5214 | 2010-05-11 14:40:53 +0200 | [diff] [blame] | 2215 | cache = ip6mr_cache_find(mrt, |
Benjamin Thery | 8229efd | 2008-12-10 16:30:15 -0800 | [diff] [blame] | 2216 | &ipv6_hdr(skb)->saddr, &ipv6_hdr(skb)->daddr); |
Ian Morris | 63159f2 | 2015-03-29 14:00:04 +0100 | [diff] [blame] | 2217 | if (!cache) { |
Patrick Ruddy | e4a38c0 | 2018-10-01 09:41:27 +0100 | [diff] [blame] | 2218 | int vif = ip6mr_find_vif(mrt, dev); |
Nicolas Dichtel | 660b26d | 2013-01-21 06:00:26 +0000 | [diff] [blame] | 2219 | |
| 2220 | if (vif >= 0) |
| 2221 | cache = ip6mr_cache_find_any(mrt, |
| 2222 | &ipv6_hdr(skb)->daddr, |
| 2223 | vif); |
| 2224 | } |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 2225 | |
| 2226 | /* |
| 2227 | * No usable cache entry |
| 2228 | */ |
Ian Morris | 63159f2 | 2015-03-29 14:00:04 +0100 | [diff] [blame] | 2229 | if (!cache) { |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 2230 | int vif; |
| 2231 | |
Patrick Ruddy | e4a38c0 | 2018-10-01 09:41:27 +0100 | [diff] [blame] | 2232 | vif = ip6mr_find_vif(mrt, dev); |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 2233 | if (vif >= 0) { |
Patrick Ruddy | e4a38c0 | 2018-10-01 09:41:27 +0100 | [diff] [blame] | 2234 | int err = ip6mr_cache_unresolved(mrt, vif, skb, dev); |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 2235 | read_unlock(&mrt_lock); |
| 2236 | |
| 2237 | return err; |
| 2238 | } |
| 2239 | read_unlock(&mrt_lock); |
| 2240 | kfree_skb(skb); |
| 2241 | return -ENODEV; |
| 2242 | } |
| 2243 | |
Patrick Ruddy | e4a38c0 | 2018-10-01 09:41:27 +0100 | [diff] [blame] | 2244 | ip6_mr_forward(net, mrt, dev, skb, cache); |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 2245 | |
| 2246 | read_unlock(&mrt_lock); |
| 2247 | |
| 2248 | return 0; |
| 2249 | } |
| 2250 | |
Nikolay Aleksandrov | 2cf7507 | 2016-09-25 23:08:31 +0200 | [diff] [blame] | 2251 | int ip6mr_get_route(struct net *net, struct sk_buff *skb, struct rtmsg *rtm, |
David Ahern | fd61c6b | 2017-01-17 15:51:07 -0800 | [diff] [blame] | 2252 | u32 portid) |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 2253 | { |
| 2254 | int err; |
Yuval Mintz | b70432f | 2018-02-28 23:29:32 +0200 | [diff] [blame] | 2255 | struct mr_table *mrt; |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 2256 | struct mfc6_cache *cache; |
Eric Dumazet | adf3090 | 2009-06-02 05:19:30 +0000 | [diff] [blame] | 2257 | struct rt6_info *rt = (struct rt6_info *)skb_dst(skb); |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 2258 | |
Patrick McHardy | d1db275 | 2010-05-11 14:40:55 +0200 | [diff] [blame] | 2259 | mrt = ip6mr_get_table(net, RT6_TABLE_DFLT); |
Ian Morris | 63159f2 | 2015-03-29 14:00:04 +0100 | [diff] [blame] | 2260 | if (!mrt) |
Patrick McHardy | d1db275 | 2010-05-11 14:40:55 +0200 | [diff] [blame] | 2261 | return -ENOENT; |
| 2262 | |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 2263 | read_lock(&mrt_lock); |
Patrick McHardy | 6bd5214 | 2010-05-11 14:40:53 +0200 | [diff] [blame] | 2264 | cache = ip6mr_cache_find(mrt, &rt->rt6i_src.addr, &rt->rt6i_dst.addr); |
Nicolas Dichtel | 660b26d | 2013-01-21 06:00:26 +0000 | [diff] [blame] | 2265 | if (!cache && skb->dev) { |
| 2266 | int vif = ip6mr_find_vif(mrt, skb->dev); |
| 2267 | |
| 2268 | if (vif >= 0) |
| 2269 | cache = ip6mr_cache_find_any(mrt, &rt->rt6i_dst.addr, |
| 2270 | vif); |
| 2271 | } |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 2272 | |
| 2273 | if (!cache) { |
| 2274 | struct sk_buff *skb2; |
| 2275 | struct ipv6hdr *iph; |
| 2276 | struct net_device *dev; |
| 2277 | int vif; |
| 2278 | |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 2279 | dev = skb->dev; |
Ian Morris | 63159f2 | 2015-03-29 14:00:04 +0100 | [diff] [blame] | 2280 | if (!dev || (vif = ip6mr_find_vif(mrt, dev)) < 0) { |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 2281 | read_unlock(&mrt_lock); |
| 2282 | return -ENODEV; |
| 2283 | } |
| 2284 | |
| 2285 | /* really correct? */ |
| 2286 | skb2 = alloc_skb(sizeof(struct ipv6hdr), GFP_ATOMIC); |
| 2287 | if (!skb2) { |
| 2288 | read_unlock(&mrt_lock); |
| 2289 | return -ENOMEM; |
| 2290 | } |
| 2291 | |
Nikolay Aleksandrov | 2cf7507 | 2016-09-25 23:08:31 +0200 | [diff] [blame] | 2292 | NETLINK_CB(skb2).portid = portid; |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 2293 | skb_reset_transport_header(skb2); |
| 2294 | |
| 2295 | skb_put(skb2, sizeof(struct ipv6hdr)); |
| 2296 | skb_reset_network_header(skb2); |
| 2297 | |
| 2298 | iph = ipv6_hdr(skb2); |
| 2299 | iph->version = 0; |
| 2300 | iph->priority = 0; |
| 2301 | iph->flow_lbl[0] = 0; |
| 2302 | iph->flow_lbl[1] = 0; |
| 2303 | iph->flow_lbl[2] = 0; |
| 2304 | iph->payload_len = 0; |
| 2305 | iph->nexthdr = IPPROTO_NONE; |
| 2306 | iph->hop_limit = 0; |
Alexey Dobriyan | 4e3fd7a | 2011-11-21 03:39:03 +0000 | [diff] [blame] | 2307 | iph->saddr = rt->rt6i_src.addr; |
| 2308 | iph->daddr = rt->rt6i_dst.addr; |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 2309 | |
Patrick Ruddy | e4a38c0 | 2018-10-01 09:41:27 +0100 | [diff] [blame] | 2310 | err = ip6mr_cache_unresolved(mrt, vif, skb2, dev); |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 2311 | read_unlock(&mrt_lock); |
| 2312 | |
| 2313 | return err; |
| 2314 | } |
| 2315 | |
Yuval Mintz | 7b0db85 | 2018-02-28 23:29:39 +0200 | [diff] [blame] | 2316 | err = mr_fill_mroute(mrt, skb, &cache->_c, rtm); |
YOSHIFUJI Hideaki | 7bc570c | 2008-04-03 09:22:53 +0900 | [diff] [blame] | 2317 | read_unlock(&mrt_lock); |
| 2318 | return err; |
| 2319 | } |
| 2320 | |
Yuval Mintz | b70432f | 2018-02-28 23:29:32 +0200 | [diff] [blame] | 2321 | static int ip6mr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb, |
Nicolas Dichtel | f518338 | 2014-03-19 17:47:51 +0100 | [diff] [blame] | 2322 | u32 portid, u32 seq, struct mfc6_cache *c, int cmd, |
| 2323 | int flags) |
Patrick McHardy | 5b285ca | 2010-05-11 14:40:56 +0200 | [diff] [blame] | 2324 | { |
| 2325 | struct nlmsghdr *nlh; |
| 2326 | struct rtmsg *rtm; |
Nicolas Dichtel | 1eb99af | 2012-12-04 01:13:39 +0000 | [diff] [blame] | 2327 | int err; |
Patrick McHardy | 5b285ca | 2010-05-11 14:40:56 +0200 | [diff] [blame] | 2328 | |
Nicolas Dichtel | f518338 | 2014-03-19 17:47:51 +0100 | [diff] [blame] | 2329 | nlh = nlmsg_put(skb, portid, seq, cmd, sizeof(*rtm), flags); |
Ian Morris | 63159f2 | 2015-03-29 14:00:04 +0100 | [diff] [blame] | 2330 | if (!nlh) |
Patrick McHardy | 5b285ca | 2010-05-11 14:40:56 +0200 | [diff] [blame] | 2331 | return -EMSGSIZE; |
| 2332 | |
| 2333 | rtm = nlmsg_data(nlh); |
Nicolas Dichtel | 193c1e4 | 2012-12-04 01:01:49 +0000 | [diff] [blame] | 2334 | rtm->rtm_family = RTNL_FAMILY_IP6MR; |
Patrick McHardy | 5b285ca | 2010-05-11 14:40:56 +0200 | [diff] [blame] | 2335 | rtm->rtm_dst_len = 128; |
| 2336 | rtm->rtm_src_len = 128; |
| 2337 | rtm->rtm_tos = 0; |
| 2338 | rtm->rtm_table = mrt->id; |
David S. Miller | c78679e | 2012-04-01 20:27:33 -0400 | [diff] [blame] | 2339 | if (nla_put_u32(skb, RTA_TABLE, mrt->id)) |
| 2340 | goto nla_put_failure; |
Nicolas Dichtel | 1eb99af | 2012-12-04 01:13:39 +0000 | [diff] [blame] | 2341 | rtm->rtm_type = RTN_MULTICAST; |
Patrick McHardy | 5b285ca | 2010-05-11 14:40:56 +0200 | [diff] [blame] | 2342 | rtm->rtm_scope = RT_SCOPE_UNIVERSE; |
Yuval Mintz | 494fff5 | 2018-02-28 23:29:34 +0200 | [diff] [blame] | 2343 | if (c->_c.mfc_flags & MFC_STATIC) |
Nicolas Dichtel | 9a68ac7 | 2012-12-04 01:13:38 +0000 | [diff] [blame] | 2344 | rtm->rtm_protocol = RTPROT_STATIC; |
| 2345 | else |
| 2346 | rtm->rtm_protocol = RTPROT_MROUTED; |
Patrick McHardy | 5b285ca | 2010-05-11 14:40:56 +0200 | [diff] [blame] | 2347 | rtm->rtm_flags = 0; |
| 2348 | |
Jiri Benc | 930345e | 2015-03-29 16:59:25 +0200 | [diff] [blame] | 2349 | if (nla_put_in6_addr(skb, RTA_SRC, &c->mf6c_origin) || |
| 2350 | nla_put_in6_addr(skb, RTA_DST, &c->mf6c_mcastgrp)) |
David S. Miller | c78679e | 2012-04-01 20:27:33 -0400 | [diff] [blame] | 2351 | goto nla_put_failure; |
Yuval Mintz | 7b0db85 | 2018-02-28 23:29:39 +0200 | [diff] [blame] | 2352 | err = mr_fill_mroute(mrt, skb, &c->_c, rtm); |
Nicolas Dichtel | 1eb99af | 2012-12-04 01:13:39 +0000 | [diff] [blame] | 2353 | /* do not break the dump if cache is unresolved */ |
| 2354 | if (err < 0 && err != -ENOENT) |
Patrick McHardy | 5b285ca | 2010-05-11 14:40:56 +0200 | [diff] [blame] | 2355 | goto nla_put_failure; |
| 2356 | |
Johannes Berg | 053c095 | 2015-01-16 22:09:00 +0100 | [diff] [blame] | 2357 | nlmsg_end(skb, nlh); |
| 2358 | return 0; |
Patrick McHardy | 5b285ca | 2010-05-11 14:40:56 +0200 | [diff] [blame] | 2359 | |
| 2360 | nla_put_failure: |
| 2361 | nlmsg_cancel(skb, nlh); |
| 2362 | return -EMSGSIZE; |
| 2363 | } |
| 2364 | |
Yuval Mintz | 7b0db85 | 2018-02-28 23:29:39 +0200 | [diff] [blame] | 2365 | static int _ip6mr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb, |
| 2366 | u32 portid, u32 seq, struct mr_mfc *c, |
| 2367 | int cmd, int flags) |
| 2368 | { |
| 2369 | return ip6mr_fill_mroute(mrt, skb, portid, seq, (struct mfc6_cache *)c, |
| 2370 | cmd, flags); |
| 2371 | } |
| 2372 | |
Nicolas Dichtel | 812e44d | 2012-12-04 01:13:41 +0000 | [diff] [blame] | 2373 | static int mr6_msgsize(bool unresolved, int maxvif) |
| 2374 | { |
| 2375 | size_t len = |
| 2376 | NLMSG_ALIGN(sizeof(struct rtmsg)) |
| 2377 | + nla_total_size(4) /* RTA_TABLE */ |
| 2378 | + nla_total_size(sizeof(struct in6_addr)) /* RTA_SRC */ |
| 2379 | + nla_total_size(sizeof(struct in6_addr)) /* RTA_DST */ |
| 2380 | ; |
| 2381 | |
| 2382 | if (!unresolved) |
| 2383 | len = len |
| 2384 | + nla_total_size(4) /* RTA_IIF */ |
| 2385 | + nla_total_size(0) /* RTA_MULTIPATH */ |
| 2386 | + maxvif * NLA_ALIGN(sizeof(struct rtnexthop)) |
| 2387 | /* RTA_MFC_STATS */ |
Nicolas Dichtel | 3d6b66c | 2016-04-21 18:58:27 +0200 | [diff] [blame] | 2388 | + nla_total_size_64bit(sizeof(struct rta_mfc_stats)) |
Nicolas Dichtel | 812e44d | 2012-12-04 01:13:41 +0000 | [diff] [blame] | 2389 | ; |
| 2390 | |
| 2391 | return len; |
| 2392 | } |
| 2393 | |
Yuval Mintz | b70432f | 2018-02-28 23:29:32 +0200 | [diff] [blame] | 2394 | static void mr6_netlink_event(struct mr_table *mrt, struct mfc6_cache *mfc, |
Nicolas Dichtel | 812e44d | 2012-12-04 01:13:41 +0000 | [diff] [blame] | 2395 | int cmd) |
| 2396 | { |
| 2397 | struct net *net = read_pnet(&mrt->net); |
| 2398 | struct sk_buff *skb; |
| 2399 | int err = -ENOBUFS; |
| 2400 | |
Yuval Mintz | 494fff5 | 2018-02-28 23:29:34 +0200 | [diff] [blame] | 2401 | skb = nlmsg_new(mr6_msgsize(mfc->_c.mfc_parent >= MAXMIFS, mrt->maxvif), |
Nicolas Dichtel | 812e44d | 2012-12-04 01:13:41 +0000 | [diff] [blame] | 2402 | GFP_ATOMIC); |
Ian Morris | 63159f2 | 2015-03-29 14:00:04 +0100 | [diff] [blame] | 2403 | if (!skb) |
Nicolas Dichtel | 812e44d | 2012-12-04 01:13:41 +0000 | [diff] [blame] | 2404 | goto errout; |
| 2405 | |
Nicolas Dichtel | f518338 | 2014-03-19 17:47:51 +0100 | [diff] [blame] | 2406 | err = ip6mr_fill_mroute(mrt, skb, 0, 0, mfc, cmd, 0); |
Nicolas Dichtel | 812e44d | 2012-12-04 01:13:41 +0000 | [diff] [blame] | 2407 | if (err < 0) |
| 2408 | goto errout; |
| 2409 | |
| 2410 | rtnl_notify(skb, net, 0, RTNLGRP_IPV6_MROUTE, NULL, GFP_ATOMIC); |
| 2411 | return; |
| 2412 | |
| 2413 | errout: |
| 2414 | kfree_skb(skb); |
| 2415 | if (err < 0) |
| 2416 | rtnl_set_sk_err(net, RTNLGRP_IPV6_MROUTE, err); |
| 2417 | } |
| 2418 | |
Julien Gomes | dd12d15c | 2017-06-20 13:54:18 -0700 | [diff] [blame] | 2419 | static size_t mrt6msg_netlink_msgsize(size_t payloadlen) |
| 2420 | { |
| 2421 | size_t len = |
| 2422 | NLMSG_ALIGN(sizeof(struct rtgenmsg)) |
| 2423 | + nla_total_size(1) /* IP6MRA_CREPORT_MSGTYPE */ |
| 2424 | + nla_total_size(4) /* IP6MRA_CREPORT_MIF_ID */ |
| 2425 | /* IP6MRA_CREPORT_SRC_ADDR */ |
| 2426 | + nla_total_size(sizeof(struct in6_addr)) |
| 2427 | /* IP6MRA_CREPORT_DST_ADDR */ |
| 2428 | + nla_total_size(sizeof(struct in6_addr)) |
| 2429 | /* IP6MRA_CREPORT_PKT */ |
| 2430 | + nla_total_size(payloadlen) |
| 2431 | ; |
| 2432 | |
| 2433 | return len; |
| 2434 | } |
| 2435 | |
Yuval Mintz | b70432f | 2018-02-28 23:29:32 +0200 | [diff] [blame] | 2436 | static void mrt6msg_netlink_event(struct mr_table *mrt, struct sk_buff *pkt) |
Julien Gomes | dd12d15c | 2017-06-20 13:54:18 -0700 | [diff] [blame] | 2437 | { |
| 2438 | struct net *net = read_pnet(&mrt->net); |
| 2439 | struct nlmsghdr *nlh; |
| 2440 | struct rtgenmsg *rtgenm; |
| 2441 | struct mrt6msg *msg; |
| 2442 | struct sk_buff *skb; |
| 2443 | struct nlattr *nla; |
| 2444 | int payloadlen; |
| 2445 | |
| 2446 | payloadlen = pkt->len - sizeof(struct mrt6msg); |
| 2447 | msg = (struct mrt6msg *)skb_transport_header(pkt); |
| 2448 | |
| 2449 | skb = nlmsg_new(mrt6msg_netlink_msgsize(payloadlen), GFP_ATOMIC); |
| 2450 | if (!skb) |
| 2451 | goto errout; |
| 2452 | |
| 2453 | nlh = nlmsg_put(skb, 0, 0, RTM_NEWCACHEREPORT, |
| 2454 | sizeof(struct rtgenmsg), 0); |
| 2455 | if (!nlh) |
| 2456 | goto errout; |
| 2457 | rtgenm = nlmsg_data(nlh); |
| 2458 | rtgenm->rtgen_family = RTNL_FAMILY_IP6MR; |
| 2459 | if (nla_put_u8(skb, IP6MRA_CREPORT_MSGTYPE, msg->im6_msgtype) || |
| 2460 | nla_put_u32(skb, IP6MRA_CREPORT_MIF_ID, msg->im6_mif) || |
| 2461 | nla_put_in6_addr(skb, IP6MRA_CREPORT_SRC_ADDR, |
| 2462 | &msg->im6_src) || |
| 2463 | nla_put_in6_addr(skb, IP6MRA_CREPORT_DST_ADDR, |
| 2464 | &msg->im6_dst)) |
| 2465 | goto nla_put_failure; |
| 2466 | |
| 2467 | nla = nla_reserve(skb, IP6MRA_CREPORT_PKT, payloadlen); |
| 2468 | if (!nla || skb_copy_bits(pkt, sizeof(struct mrt6msg), |
| 2469 | nla_data(nla), payloadlen)) |
| 2470 | goto nla_put_failure; |
| 2471 | |
| 2472 | nlmsg_end(skb, nlh); |
| 2473 | |
| 2474 | rtnl_notify(skb, net, 0, RTNLGRP_IPV6_MROUTE_R, NULL, GFP_ATOMIC); |
| 2475 | return; |
| 2476 | |
| 2477 | nla_put_failure: |
| 2478 | nlmsg_cancel(skb, nlh); |
| 2479 | errout: |
| 2480 | kfree_skb(skb); |
| 2481 | rtnl_set_sk_err(net, RTNLGRP_IPV6_MROUTE_R, -ENOBUFS); |
| 2482 | } |
| 2483 | |
Patrick McHardy | 5b285ca | 2010-05-11 14:40:56 +0200 | [diff] [blame] | 2484 | static int ip6mr_rtm_dumproute(struct sk_buff *skb, struct netlink_callback *cb) |
| 2485 | { |
David Ahern | e8ba330 | 2018-10-07 20:16:35 -0700 | [diff] [blame] | 2486 | const struct nlmsghdr *nlh = cb->nlh; |
David Ahern | 4724676 | 2018-10-15 18:56:42 -0700 | [diff] [blame] | 2487 | struct fib_dump_filter filter = {}; |
David Ahern | cb16789 | 2018-10-15 18:56:47 -0700 | [diff] [blame] | 2488 | int err; |
David Ahern | e8ba330 | 2018-10-07 20:16:35 -0700 | [diff] [blame] | 2489 | |
| 2490 | if (cb->strict_check) { |
David Ahern | 4724676 | 2018-10-15 18:56:42 -0700 | [diff] [blame] | 2491 | err = ip_valid_fib_dump_req(sock_net(skb->sk), nlh, |
David Ahern | effe679 | 2018-10-15 18:56:48 -0700 | [diff] [blame] | 2492 | &filter, cb); |
David Ahern | e8ba330 | 2018-10-07 20:16:35 -0700 | [diff] [blame] | 2493 | if (err < 0) |
| 2494 | return err; |
| 2495 | } |
| 2496 | |
David Ahern | cb16789 | 2018-10-15 18:56:47 -0700 | [diff] [blame] | 2497 | if (filter.table_id) { |
| 2498 | struct mr_table *mrt; |
| 2499 | |
| 2500 | mrt = ip6mr_get_table(sock_net(skb->sk), filter.table_id); |
| 2501 | if (!mrt) { |
Sabrina Dubroca | 41b4bd9 | 2020-05-20 11:15:46 +0200 | [diff] [blame] | 2502 | if (rtnl_msg_family(cb->nlh) != RTNL_FAMILY_IP6MR) |
David Ahern | ae677bb | 2018-10-24 12:59:01 -0700 | [diff] [blame] | 2503 | return skb->len; |
| 2504 | |
David Ahern | cb16789 | 2018-10-15 18:56:47 -0700 | [diff] [blame] | 2505 | NL_SET_ERR_MSG_MOD(cb->extack, "MR table does not exist"); |
| 2506 | return -ENOENT; |
| 2507 | } |
| 2508 | err = mr_table_dump(mrt, skb, cb, _ip6mr_fill_mroute, |
| 2509 | &mfc_unres_lock, &filter); |
| 2510 | return skb->len ? : err; |
| 2511 | } |
| 2512 | |
Yuval Mintz | 7b0db85 | 2018-02-28 23:29:39 +0200 | [diff] [blame] | 2513 | return mr_rtm_dumproute(skb, cb, ip6mr_mr_table_iter, |
David Ahern | cb16789 | 2018-10-15 18:56:47 -0700 | [diff] [blame] | 2514 | _ip6mr_fill_mroute, &mfc_unres_lock, &filter); |
Patrick McHardy | 5b285ca | 2010-05-11 14:40:56 +0200 | [diff] [blame] | 2515 | } |