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