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