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