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