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