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