Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * IP multicast routing support for mrouted 3.6/3.8 |
| 3 | * |
Alan Cox | 113aa83 | 2008-10-13 19:01:08 -0700 | [diff] [blame] | 4 | * (c) 1995 Alan Cox, <alan@lxorguk.ukuu.org.uk> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | * Linux Consultancy and Custom Driver Development |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU General Public License |
| 9 | * as published by the Free Software Foundation; either version |
| 10 | * 2 of the License, or (at your option) any later version. |
| 11 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | * Fixes: |
| 13 | * Michael Chastain : Incorrect size of copying. |
| 14 | * Alan Cox : Added the cache manager code |
| 15 | * Alan Cox : Fixed the clone/copy bug and device race. |
| 16 | * Mike McLagan : Routing by source |
| 17 | * Malcolm Beattie : Buffer handling fixes. |
| 18 | * Alexey Kuznetsov : Double buffer free and other fixes. |
| 19 | * SVR Anand : Fixed several multicast bugs and problems. |
| 20 | * Alexey Kuznetsov : Status, optimisations and more. |
| 21 | * Brad Parker : Better behaviour on mrouted upcall |
| 22 | * overflow. |
| 23 | * Carlos Picoto : PIMv1 Support |
| 24 | * Pavlin Ivanov Radoslavov: PIMv2 Registers must checksum only PIM header |
Gilles Espinasse | f77f13e | 2010-03-29 15:41:47 +0200 | [diff] [blame] | 25 | * Relax this requirement to work with older peers. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | * |
| 27 | */ |
| 28 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | #include <asm/uaccess.h> |
| 30 | #include <linux/types.h> |
Randy Dunlap | 4fc268d | 2006-01-11 12:17:47 -0800 | [diff] [blame] | 31 | #include <linux/capability.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | #include <linux/errno.h> |
| 33 | #include <linux/timer.h> |
| 34 | #include <linux/mm.h> |
| 35 | #include <linux/kernel.h> |
| 36 | #include <linux/fcntl.h> |
| 37 | #include <linux/stat.h> |
| 38 | #include <linux/socket.h> |
| 39 | #include <linux/in.h> |
| 40 | #include <linux/inet.h> |
| 41 | #include <linux/netdevice.h> |
| 42 | #include <linux/inetdevice.h> |
| 43 | #include <linux/igmp.h> |
| 44 | #include <linux/proc_fs.h> |
| 45 | #include <linux/seq_file.h> |
| 46 | #include <linux/mroute.h> |
| 47 | #include <linux/init.h> |
Kris Katterjohn | 46f25df | 2006-01-05 16:35:42 -0800 | [diff] [blame] | 48 | #include <linux/if_ether.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 49 | #include <linux/slab.h> |
Eric W. Biederman | 457c4cb | 2007-09-12 12:01:34 +0200 | [diff] [blame] | 50 | #include <net/net_namespace.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | #include <net/ip.h> |
| 52 | #include <net/protocol.h> |
| 53 | #include <linux/skbuff.h> |
Arnaldo Carvalho de Melo | 14c8502 | 2005-12-27 02:43:12 -0200 | [diff] [blame] | 54 | #include <net/route.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | #include <net/sock.h> |
| 56 | #include <net/icmp.h> |
| 57 | #include <net/udp.h> |
| 58 | #include <net/raw.h> |
| 59 | #include <linux/notifier.h> |
| 60 | #include <linux/if_arp.h> |
| 61 | #include <linux/netfilter_ipv4.h> |
Eric W. Biederman | 709b46e | 2011-01-29 16:15:56 +0000 | [diff] [blame] | 62 | #include <linux/compat.h> |
Paul Gortmaker | bc3b2d7 | 2011-07-15 11:47:34 -0400 | [diff] [blame] | 63 | #include <linux/export.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | #include <net/ipip.h> |
| 65 | #include <net/checksum.h> |
Arnaldo Carvalho de Melo | dc5fc57 | 2007-03-25 23:06:12 -0700 | [diff] [blame] | 66 | #include <net/netlink.h> |
Patrick McHardy | f0ad086 | 2010-04-13 05:03:23 +0000 | [diff] [blame] | 67 | #include <net/fib_rules.h> |
Nicolas Dichtel | d67b8c6 | 2012-12-04 01:13:35 +0000 | [diff] [blame] | 68 | #include <linux/netconf.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | |
| 70 | #if defined(CONFIG_IP_PIMSM_V1) || defined(CONFIG_IP_PIMSM_V2) |
| 71 | #define CONFIG_IP_PIMSM 1 |
| 72 | #endif |
| 73 | |
Patrick McHardy | 0c12295 | 2010-04-13 05:03:22 +0000 | [diff] [blame] | 74 | struct mr_table { |
Patrick McHardy | f0ad086 | 2010-04-13 05:03:23 +0000 | [diff] [blame] | 75 | struct list_head list; |
Patrick McHardy | 8de53df | 2010-04-15 13:29:28 +0200 | [diff] [blame] | 76 | #ifdef CONFIG_NET_NS |
| 77 | struct net *net; |
| 78 | #endif |
Patrick McHardy | f0ad086 | 2010-04-13 05:03:23 +0000 | [diff] [blame] | 79 | u32 id; |
Eric Dumazet | 4c968709 | 2010-10-01 16:15:01 +0000 | [diff] [blame] | 80 | struct sock __rcu *mroute_sk; |
Patrick McHardy | 0c12295 | 2010-04-13 05:03:22 +0000 | [diff] [blame] | 81 | struct timer_list ipmr_expire_timer; |
| 82 | struct list_head mfc_unres_queue; |
| 83 | struct list_head mfc_cache_array[MFC_LINES]; |
| 84 | struct vif_device vif_table[MAXVIFS]; |
| 85 | int maxvif; |
| 86 | atomic_t cache_resolve_queue_len; |
Joe Perches | 53d6841 | 2012-11-25 09:35:30 +0000 | [diff] [blame] | 87 | bool mroute_do_assert; |
| 88 | bool mroute_do_pim; |
Patrick McHardy | 0c12295 | 2010-04-13 05:03:22 +0000 | [diff] [blame] | 89 | #if defined(CONFIG_IP_PIMSM_V1) || defined(CONFIG_IP_PIMSM_V2) |
| 90 | int mroute_reg_vif_num; |
| 91 | #endif |
| 92 | }; |
| 93 | |
Patrick McHardy | f0ad086 | 2010-04-13 05:03:23 +0000 | [diff] [blame] | 94 | struct ipmr_rule { |
| 95 | struct fib_rule common; |
| 96 | }; |
| 97 | |
| 98 | struct ipmr_result { |
| 99 | struct mr_table *mrt; |
| 100 | }; |
| 101 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | /* Big lock, protecting vif table, mrt cache and mroute socket state. |
Eric Dumazet | a8cb16d | 2010-10-01 16:15:29 +0000 | [diff] [blame] | 103 | * Note that the changes are semaphored via rtnl_lock. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | */ |
| 105 | |
| 106 | static DEFINE_RWLOCK(mrt_lock); |
| 107 | |
| 108 | /* |
| 109 | * Multicast router control variables |
| 110 | */ |
| 111 | |
Patrick McHardy | 0c12295 | 2010-04-13 05:03:22 +0000 | [diff] [blame] | 112 | #define VIF_EXISTS(_mrt, _idx) ((_mrt)->vif_table[_idx].dev != NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | |
| 114 | /* Special spinlock for queue of unresolved entries */ |
| 115 | static DEFINE_SPINLOCK(mfc_unres_lock); |
| 116 | |
| 117 | /* We return to original Alan's scheme. Hash table of resolved |
Eric Dumazet | a8cb16d | 2010-10-01 16:15:29 +0000 | [diff] [blame] | 118 | * entries is changed only in process context and protected |
| 119 | * with weak lock mrt_lock. Queue of unresolved entries is protected |
| 120 | * with strong spinlock mfc_unres_lock. |
| 121 | * |
| 122 | * In this case data path is free of exclusive locks at all. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | */ |
| 124 | |
Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 125 | static struct kmem_cache *mrt_cachep __read_mostly; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | |
Patrick McHardy | f0ad086 | 2010-04-13 05:03:23 +0000 | [diff] [blame] | 127 | static struct mr_table *ipmr_new_table(struct net *net, u32 id); |
Francesco Ruggeri | acbb219 | 2012-08-24 07:38:35 +0000 | [diff] [blame] | 128 | static void ipmr_free_table(struct mr_table *mrt); |
| 129 | |
Patrick McHardy | 0c12295 | 2010-04-13 05:03:22 +0000 | [diff] [blame] | 130 | static int ip_mr_forward(struct net *net, struct mr_table *mrt, |
| 131 | struct sk_buff *skb, struct mfc_cache *cache, |
| 132 | int local); |
| 133 | static int ipmr_cache_report(struct mr_table *mrt, |
Benjamin Thery | 4feb88e | 2009-01-22 04:56:23 +0000 | [diff] [blame] | 134 | struct sk_buff *pkt, vifi_t vifi, int assert); |
Patrick McHardy | cb6a4e4 | 2010-04-26 16:02:08 +0200 | [diff] [blame] | 135 | static int __ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb, |
| 136 | struct mfc_cache *c, struct rtmsg *rtm); |
Nicolas Dichtel | 8cd3ac9 | 2012-12-04 01:13:40 +0000 | [diff] [blame^] | 137 | static void mroute_netlink_event(struct mr_table *mrt, struct mfc_cache *mfc, |
| 138 | int cmd); |
Francesco Ruggeri | acbb219 | 2012-08-24 07:38:35 +0000 | [diff] [blame] | 139 | static void mroute_clean_tables(struct mr_table *mrt); |
Patrick McHardy | f0ad086 | 2010-04-13 05:03:23 +0000 | [diff] [blame] | 140 | static void ipmr_expire_process(unsigned long arg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | |
Patrick McHardy | f0ad086 | 2010-04-13 05:03:23 +0000 | [diff] [blame] | 142 | #ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES |
| 143 | #define ipmr_for_each_table(mrt, net) \ |
| 144 | list_for_each_entry_rcu(mrt, &net->ipv4.mr_tables, list) |
| 145 | |
| 146 | static struct mr_table *ipmr_get_table(struct net *net, u32 id) |
| 147 | { |
| 148 | struct mr_table *mrt; |
| 149 | |
| 150 | ipmr_for_each_table(mrt, net) { |
| 151 | if (mrt->id == id) |
| 152 | return mrt; |
| 153 | } |
| 154 | return NULL; |
| 155 | } |
| 156 | |
David S. Miller | da91981 | 2011-03-12 02:04:50 -0500 | [diff] [blame] | 157 | static int ipmr_fib_lookup(struct net *net, struct flowi4 *flp4, |
Patrick McHardy | f0ad086 | 2010-04-13 05:03:23 +0000 | [diff] [blame] | 158 | struct mr_table **mrt) |
| 159 | { |
| 160 | struct ipmr_result res; |
| 161 | struct fib_lookup_arg arg = { .result = &res, }; |
| 162 | int err; |
| 163 | |
David S. Miller | da91981 | 2011-03-12 02:04:50 -0500 | [diff] [blame] | 164 | err = fib_rules_lookup(net->ipv4.mr_rules_ops, |
| 165 | flowi4_to_flowi(flp4), 0, &arg); |
Patrick McHardy | f0ad086 | 2010-04-13 05:03:23 +0000 | [diff] [blame] | 166 | if (err < 0) |
| 167 | return err; |
| 168 | *mrt = res.mrt; |
| 169 | return 0; |
| 170 | } |
| 171 | |
| 172 | static int ipmr_rule_action(struct fib_rule *rule, struct flowi *flp, |
| 173 | int flags, struct fib_lookup_arg *arg) |
| 174 | { |
| 175 | struct ipmr_result *res = arg->result; |
| 176 | struct mr_table *mrt; |
| 177 | |
| 178 | switch (rule->action) { |
| 179 | case FR_ACT_TO_TBL: |
| 180 | break; |
| 181 | case FR_ACT_UNREACHABLE: |
| 182 | return -ENETUNREACH; |
| 183 | case FR_ACT_PROHIBIT: |
| 184 | return -EACCES; |
| 185 | case FR_ACT_BLACKHOLE: |
| 186 | default: |
| 187 | return -EINVAL; |
| 188 | } |
| 189 | |
| 190 | mrt = ipmr_get_table(rule->fr_net, rule->table); |
| 191 | if (mrt == NULL) |
| 192 | return -EAGAIN; |
| 193 | res->mrt = mrt; |
| 194 | return 0; |
| 195 | } |
| 196 | |
| 197 | static int ipmr_rule_match(struct fib_rule *rule, struct flowi *fl, int flags) |
| 198 | { |
| 199 | return 1; |
| 200 | } |
| 201 | |
| 202 | static const struct nla_policy ipmr_rule_policy[FRA_MAX + 1] = { |
| 203 | FRA_GENERIC_POLICY, |
| 204 | }; |
| 205 | |
| 206 | static int ipmr_rule_configure(struct fib_rule *rule, struct sk_buff *skb, |
| 207 | struct fib_rule_hdr *frh, struct nlattr **tb) |
| 208 | { |
| 209 | return 0; |
| 210 | } |
| 211 | |
| 212 | static int ipmr_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh, |
| 213 | struct nlattr **tb) |
| 214 | { |
| 215 | return 1; |
| 216 | } |
| 217 | |
| 218 | static int ipmr_rule_fill(struct fib_rule *rule, struct sk_buff *skb, |
| 219 | struct fib_rule_hdr *frh) |
| 220 | { |
| 221 | frh->dst_len = 0; |
| 222 | frh->src_len = 0; |
| 223 | frh->tos = 0; |
| 224 | return 0; |
| 225 | } |
| 226 | |
Andi Kleen | 04a6f82 | 2012-10-04 17:12:11 -0700 | [diff] [blame] | 227 | static const struct fib_rules_ops __net_initconst ipmr_rules_ops_template = { |
Patrick McHardy | 25239ce | 2010-04-26 16:02:05 +0200 | [diff] [blame] | 228 | .family = RTNL_FAMILY_IPMR, |
Patrick McHardy | f0ad086 | 2010-04-13 05:03:23 +0000 | [diff] [blame] | 229 | .rule_size = sizeof(struct ipmr_rule), |
| 230 | .addr_size = sizeof(u32), |
| 231 | .action = ipmr_rule_action, |
| 232 | .match = ipmr_rule_match, |
| 233 | .configure = ipmr_rule_configure, |
| 234 | .compare = ipmr_rule_compare, |
| 235 | .default_pref = fib_default_rule_pref, |
| 236 | .fill = ipmr_rule_fill, |
| 237 | .nlgroup = RTNLGRP_IPV4_RULE, |
| 238 | .policy = ipmr_rule_policy, |
| 239 | .owner = THIS_MODULE, |
| 240 | }; |
| 241 | |
| 242 | static int __net_init ipmr_rules_init(struct net *net) |
| 243 | { |
| 244 | struct fib_rules_ops *ops; |
| 245 | struct mr_table *mrt; |
| 246 | int err; |
| 247 | |
| 248 | ops = fib_rules_register(&ipmr_rules_ops_template, net); |
| 249 | if (IS_ERR(ops)) |
| 250 | return PTR_ERR(ops); |
| 251 | |
| 252 | INIT_LIST_HEAD(&net->ipv4.mr_tables); |
| 253 | |
| 254 | mrt = ipmr_new_table(net, RT_TABLE_DEFAULT); |
| 255 | if (mrt == NULL) { |
| 256 | err = -ENOMEM; |
| 257 | goto err1; |
| 258 | } |
| 259 | |
| 260 | err = fib_default_rule_add(ops, 0x7fff, RT_TABLE_DEFAULT, 0); |
| 261 | if (err < 0) |
| 262 | goto err2; |
| 263 | |
| 264 | net->ipv4.mr_rules_ops = ops; |
| 265 | return 0; |
| 266 | |
| 267 | err2: |
| 268 | kfree(mrt); |
| 269 | err1: |
| 270 | fib_rules_unregister(ops); |
| 271 | return err; |
| 272 | } |
| 273 | |
| 274 | static void __net_exit ipmr_rules_exit(struct net *net) |
| 275 | { |
| 276 | struct mr_table *mrt, *next; |
| 277 | |
Eric Dumazet | 035320d | 2010-06-06 23:48:40 +0000 | [diff] [blame] | 278 | list_for_each_entry_safe(mrt, next, &net->ipv4.mr_tables, list) { |
| 279 | list_del(&mrt->list); |
Francesco Ruggeri | acbb219 | 2012-08-24 07:38:35 +0000 | [diff] [blame] | 280 | ipmr_free_table(mrt); |
Eric Dumazet | 035320d | 2010-06-06 23:48:40 +0000 | [diff] [blame] | 281 | } |
Patrick McHardy | f0ad086 | 2010-04-13 05:03:23 +0000 | [diff] [blame] | 282 | fib_rules_unregister(net->ipv4.mr_rules_ops); |
| 283 | } |
| 284 | #else |
| 285 | #define ipmr_for_each_table(mrt, net) \ |
| 286 | for (mrt = net->ipv4.mrt; mrt; mrt = NULL) |
| 287 | |
| 288 | static struct mr_table *ipmr_get_table(struct net *net, u32 id) |
| 289 | { |
| 290 | return net->ipv4.mrt; |
| 291 | } |
| 292 | |
David S. Miller | da91981 | 2011-03-12 02:04:50 -0500 | [diff] [blame] | 293 | static int ipmr_fib_lookup(struct net *net, struct flowi4 *flp4, |
Patrick McHardy | f0ad086 | 2010-04-13 05:03:23 +0000 | [diff] [blame] | 294 | struct mr_table **mrt) |
| 295 | { |
| 296 | *mrt = net->ipv4.mrt; |
| 297 | return 0; |
| 298 | } |
| 299 | |
| 300 | static int __net_init ipmr_rules_init(struct net *net) |
| 301 | { |
| 302 | net->ipv4.mrt = ipmr_new_table(net, RT_TABLE_DEFAULT); |
| 303 | return net->ipv4.mrt ? 0 : -ENOMEM; |
| 304 | } |
| 305 | |
| 306 | static void __net_exit ipmr_rules_exit(struct net *net) |
| 307 | { |
Francesco Ruggeri | acbb219 | 2012-08-24 07:38:35 +0000 | [diff] [blame] | 308 | ipmr_free_table(net->ipv4.mrt); |
Patrick McHardy | f0ad086 | 2010-04-13 05:03:23 +0000 | [diff] [blame] | 309 | } |
| 310 | #endif |
| 311 | |
| 312 | static struct mr_table *ipmr_new_table(struct net *net, u32 id) |
| 313 | { |
| 314 | struct mr_table *mrt; |
| 315 | unsigned int i; |
| 316 | |
| 317 | mrt = ipmr_get_table(net, id); |
| 318 | if (mrt != NULL) |
| 319 | return mrt; |
| 320 | |
| 321 | mrt = kzalloc(sizeof(*mrt), GFP_KERNEL); |
| 322 | if (mrt == NULL) |
| 323 | return NULL; |
Patrick McHardy | 8de53df | 2010-04-15 13:29:28 +0200 | [diff] [blame] | 324 | write_pnet(&mrt->net, net); |
Patrick McHardy | f0ad086 | 2010-04-13 05:03:23 +0000 | [diff] [blame] | 325 | mrt->id = id; |
| 326 | |
| 327 | /* Forwarding cache */ |
| 328 | for (i = 0; i < MFC_LINES; i++) |
| 329 | INIT_LIST_HEAD(&mrt->mfc_cache_array[i]); |
| 330 | |
| 331 | INIT_LIST_HEAD(&mrt->mfc_unres_queue); |
| 332 | |
| 333 | setup_timer(&mrt->ipmr_expire_timer, ipmr_expire_process, |
| 334 | (unsigned long)mrt); |
| 335 | |
| 336 | #ifdef CONFIG_IP_PIMSM |
| 337 | mrt->mroute_reg_vif_num = -1; |
| 338 | #endif |
| 339 | #ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES |
| 340 | list_add_tail_rcu(&mrt->list, &net->ipv4.mr_tables); |
| 341 | #endif |
| 342 | return mrt; |
| 343 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 344 | |
Francesco Ruggeri | acbb219 | 2012-08-24 07:38:35 +0000 | [diff] [blame] | 345 | static void ipmr_free_table(struct mr_table *mrt) |
| 346 | { |
| 347 | del_timer_sync(&mrt->ipmr_expire_timer); |
| 348 | mroute_clean_tables(mrt); |
| 349 | kfree(mrt); |
| 350 | } |
| 351 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 352 | /* Service routines creating virtual interfaces: DVMRP tunnels and PIMREG */ |
| 353 | |
Wang Chen | d607032 | 2008-07-14 20:55:26 -0700 | [diff] [blame] | 354 | static void ipmr_del_tunnel(struct net_device *dev, struct vifctl *v) |
| 355 | { |
Benjamin Thery | 4feb88e | 2009-01-22 04:56:23 +0000 | [diff] [blame] | 356 | struct net *net = dev_net(dev); |
| 357 | |
Wang Chen | d607032 | 2008-07-14 20:55:26 -0700 | [diff] [blame] | 358 | dev_close(dev); |
| 359 | |
Benjamin Thery | 4feb88e | 2009-01-22 04:56:23 +0000 | [diff] [blame] | 360 | dev = __dev_get_by_name(net, "tunl0"); |
Wang Chen | d607032 | 2008-07-14 20:55:26 -0700 | [diff] [blame] | 361 | if (dev) { |
Stephen Hemminger | 5bc3eb7 | 2008-11-19 21:52:05 -0800 | [diff] [blame] | 362 | const struct net_device_ops *ops = dev->netdev_ops; |
Wang Chen | d607032 | 2008-07-14 20:55:26 -0700 | [diff] [blame] | 363 | struct ifreq ifr; |
Wang Chen | d607032 | 2008-07-14 20:55:26 -0700 | [diff] [blame] | 364 | struct ip_tunnel_parm p; |
| 365 | |
| 366 | memset(&p, 0, sizeof(p)); |
| 367 | p.iph.daddr = v->vifc_rmt_addr.s_addr; |
| 368 | p.iph.saddr = v->vifc_lcl_addr.s_addr; |
| 369 | p.iph.version = 4; |
| 370 | p.iph.ihl = 5; |
| 371 | p.iph.protocol = IPPROTO_IPIP; |
| 372 | sprintf(p.name, "dvmrp%d", v->vifc_vifi); |
| 373 | ifr.ifr_ifru.ifru_data = (__force void __user *)&p; |
| 374 | |
Stephen Hemminger | 5bc3eb7 | 2008-11-19 21:52:05 -0800 | [diff] [blame] | 375 | if (ops->ndo_do_ioctl) { |
| 376 | mm_segment_t oldfs = get_fs(); |
| 377 | |
| 378 | set_fs(KERNEL_DS); |
| 379 | ops->ndo_do_ioctl(dev, &ifr, SIOCDELTUNNEL); |
| 380 | set_fs(oldfs); |
| 381 | } |
Wang Chen | d607032 | 2008-07-14 20:55:26 -0700 | [diff] [blame] | 382 | } |
| 383 | } |
| 384 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | static |
Benjamin Thery | 4feb88e | 2009-01-22 04:56:23 +0000 | [diff] [blame] | 386 | struct net_device *ipmr_new_tunnel(struct net *net, struct vifctl *v) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | { |
| 388 | struct net_device *dev; |
| 389 | |
Benjamin Thery | 4feb88e | 2009-01-22 04:56:23 +0000 | [diff] [blame] | 390 | dev = __dev_get_by_name(net, "tunl0"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 | |
| 392 | if (dev) { |
Stephen Hemminger | 5bc3eb7 | 2008-11-19 21:52:05 -0800 | [diff] [blame] | 393 | const struct net_device_ops *ops = dev->netdev_ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 394 | int err; |
| 395 | struct ifreq ifr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 396 | struct ip_tunnel_parm p; |
| 397 | struct in_device *in_dev; |
| 398 | |
| 399 | memset(&p, 0, sizeof(p)); |
| 400 | p.iph.daddr = v->vifc_rmt_addr.s_addr; |
| 401 | p.iph.saddr = v->vifc_lcl_addr.s_addr; |
| 402 | p.iph.version = 4; |
| 403 | p.iph.ihl = 5; |
| 404 | p.iph.protocol = IPPROTO_IPIP; |
| 405 | sprintf(p.name, "dvmrp%d", v->vifc_vifi); |
Stephen Hemminger | ba93ef7 | 2008-01-21 17:28:59 -0800 | [diff] [blame] | 406 | ifr.ifr_ifru.ifru_data = (__force void __user *)&p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 407 | |
Stephen Hemminger | 5bc3eb7 | 2008-11-19 21:52:05 -0800 | [diff] [blame] | 408 | if (ops->ndo_do_ioctl) { |
| 409 | mm_segment_t oldfs = get_fs(); |
| 410 | |
| 411 | set_fs(KERNEL_DS); |
| 412 | err = ops->ndo_do_ioctl(dev, &ifr, SIOCADDTUNNEL); |
| 413 | set_fs(oldfs); |
Eric Dumazet | a8cb16d | 2010-10-01 16:15:29 +0000 | [diff] [blame] | 414 | } else { |
Stephen Hemminger | 5bc3eb7 | 2008-11-19 21:52:05 -0800 | [diff] [blame] | 415 | err = -EOPNOTSUPP; |
Eric Dumazet | a8cb16d | 2010-10-01 16:15:29 +0000 | [diff] [blame] | 416 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 417 | dev = NULL; |
| 418 | |
Benjamin Thery | 4feb88e | 2009-01-22 04:56:23 +0000 | [diff] [blame] | 419 | if (err == 0 && |
| 420 | (dev = __dev_get_by_name(net, p.name)) != NULL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 421 | dev->flags |= IFF_MULTICAST; |
| 422 | |
Herbert Xu | e5ed639 | 2005-10-03 14:35:55 -0700 | [diff] [blame] | 423 | in_dev = __in_dev_get_rtnl(dev); |
Herbert Xu | 71e27da | 2007-06-04 23:36:06 -0700 | [diff] [blame] | 424 | if (in_dev == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 | goto failure; |
Herbert Xu | 71e27da | 2007-06-04 23:36:06 -0700 | [diff] [blame] | 426 | |
| 427 | ipv4_devconf_setall(in_dev); |
| 428 | IPV4_DEVCONF(in_dev->cnf, RP_FILTER) = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 429 | |
| 430 | if (dev_open(dev)) |
| 431 | goto failure; |
Wang Chen | 7dc00c8 | 2008-07-14 20:56:34 -0700 | [diff] [blame] | 432 | dev_hold(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 433 | } |
| 434 | } |
| 435 | return dev; |
| 436 | |
| 437 | failure: |
| 438 | /* allow the register to be completed before unregistering. */ |
| 439 | rtnl_unlock(); |
| 440 | rtnl_lock(); |
| 441 | |
| 442 | unregister_netdevice(dev); |
| 443 | return NULL; |
| 444 | } |
| 445 | |
| 446 | #ifdef CONFIG_IP_PIMSM |
| 447 | |
Stephen Hemminger | 6fef4c0 | 2009-08-31 19:50:41 +0000 | [diff] [blame] | 448 | static netdev_tx_t reg_vif_xmit(struct sk_buff *skb, struct net_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 449 | { |
Benjamin Thery | 4feb88e | 2009-01-22 04:56:23 +0000 | [diff] [blame] | 450 | struct net *net = dev_net(dev); |
Patrick McHardy | f0ad086 | 2010-04-13 05:03:23 +0000 | [diff] [blame] | 451 | struct mr_table *mrt; |
David S. Miller | da91981 | 2011-03-12 02:04:50 -0500 | [diff] [blame] | 452 | struct flowi4 fl4 = { |
| 453 | .flowi4_oif = dev->ifindex, |
| 454 | .flowi4_iif = skb->skb_iif, |
| 455 | .flowi4_mark = skb->mark, |
Patrick McHardy | f0ad086 | 2010-04-13 05:03:23 +0000 | [diff] [blame] | 456 | }; |
| 457 | int err; |
| 458 | |
David S. Miller | da91981 | 2011-03-12 02:04:50 -0500 | [diff] [blame] | 459 | err = ipmr_fib_lookup(net, &fl4, &mrt); |
Ben Greear | e40dbc5 | 2010-07-15 13:22:33 +0000 | [diff] [blame] | 460 | if (err < 0) { |
| 461 | kfree_skb(skb); |
Patrick McHardy | f0ad086 | 2010-04-13 05:03:23 +0000 | [diff] [blame] | 462 | return err; |
Ben Greear | e40dbc5 | 2010-07-15 13:22:33 +0000 | [diff] [blame] | 463 | } |
Benjamin Thery | 4feb88e | 2009-01-22 04:56:23 +0000 | [diff] [blame] | 464 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 465 | read_lock(&mrt_lock); |
Pavel Emelyanov | cf3677a | 2008-05-21 14:17:33 -0700 | [diff] [blame] | 466 | dev->stats.tx_bytes += skb->len; |
| 467 | dev->stats.tx_packets++; |
Patrick McHardy | 0c12295 | 2010-04-13 05:03:22 +0000 | [diff] [blame] | 468 | ipmr_cache_report(mrt, skb, mrt->mroute_reg_vif_num, IGMPMSG_WHOLEPKT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 469 | read_unlock(&mrt_lock); |
| 470 | kfree_skb(skb); |
Patrick McHardy | 6ed1065 | 2009-06-23 06:03:08 +0000 | [diff] [blame] | 471 | return NETDEV_TX_OK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 472 | } |
| 473 | |
Stephen Hemminger | 007c383 | 2008-11-20 20:28:35 -0800 | [diff] [blame] | 474 | static const struct net_device_ops reg_vif_netdev_ops = { |
| 475 | .ndo_start_xmit = reg_vif_xmit, |
| 476 | }; |
| 477 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 478 | static void reg_vif_setup(struct net_device *dev) |
| 479 | { |
| 480 | dev->type = ARPHRD_PIMREG; |
Kris Katterjohn | 46f25df | 2006-01-05 16:35:42 -0800 | [diff] [blame] | 481 | dev->mtu = ETH_DATA_LEN - sizeof(struct iphdr) - 8; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 482 | dev->flags = IFF_NOARP; |
Stephen Hemminger | 007c383 | 2008-11-20 20:28:35 -0800 | [diff] [blame] | 483 | dev->netdev_ops = ®_vif_netdev_ops, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 484 | dev->destructor = free_netdev; |
Tom Goff | 403dbb9 | 2009-06-14 03:16:13 -0700 | [diff] [blame] | 485 | dev->features |= NETIF_F_NETNS_LOCAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 486 | } |
| 487 | |
Patrick McHardy | f0ad086 | 2010-04-13 05:03:23 +0000 | [diff] [blame] | 488 | static struct net_device *ipmr_reg_vif(struct net *net, struct mr_table *mrt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 489 | { |
| 490 | struct net_device *dev; |
| 491 | struct in_device *in_dev; |
Patrick McHardy | f0ad086 | 2010-04-13 05:03:23 +0000 | [diff] [blame] | 492 | char name[IFNAMSIZ]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 493 | |
Patrick McHardy | f0ad086 | 2010-04-13 05:03:23 +0000 | [diff] [blame] | 494 | if (mrt->id == RT_TABLE_DEFAULT) |
| 495 | sprintf(name, "pimreg"); |
| 496 | else |
| 497 | sprintf(name, "pimreg%u", mrt->id); |
| 498 | |
| 499 | dev = alloc_netdev(0, name, reg_vif_setup); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 500 | |
| 501 | if (dev == NULL) |
| 502 | return NULL; |
| 503 | |
Tom Goff | 403dbb9 | 2009-06-14 03:16:13 -0700 | [diff] [blame] | 504 | dev_net_set(dev, net); |
| 505 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 506 | if (register_netdevice(dev)) { |
| 507 | free_netdev(dev); |
| 508 | return NULL; |
| 509 | } |
| 510 | dev->iflink = 0; |
| 511 | |
Herbert Xu | 71e27da | 2007-06-04 23:36:06 -0700 | [diff] [blame] | 512 | rcu_read_lock(); |
Eric Dumazet | a8cb16d | 2010-10-01 16:15:29 +0000 | [diff] [blame] | 513 | in_dev = __in_dev_get_rcu(dev); |
| 514 | if (!in_dev) { |
Herbert Xu | 71e27da | 2007-06-04 23:36:06 -0700 | [diff] [blame] | 515 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 516 | goto failure; |
Herbert Xu | 71e27da | 2007-06-04 23:36:06 -0700 | [diff] [blame] | 517 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 518 | |
Herbert Xu | 71e27da | 2007-06-04 23:36:06 -0700 | [diff] [blame] | 519 | ipv4_devconf_setall(in_dev); |
| 520 | IPV4_DEVCONF(in_dev->cnf, RP_FILTER) = 0; |
| 521 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 522 | |
| 523 | if (dev_open(dev)) |
| 524 | goto failure; |
| 525 | |
Wang Chen | 7dc00c8 | 2008-07-14 20:56:34 -0700 | [diff] [blame] | 526 | dev_hold(dev); |
| 527 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 528 | return dev; |
| 529 | |
| 530 | failure: |
| 531 | /* allow the register to be completed before unregistering. */ |
| 532 | rtnl_unlock(); |
| 533 | rtnl_lock(); |
| 534 | |
| 535 | unregister_netdevice(dev); |
| 536 | return NULL; |
| 537 | } |
| 538 | #endif |
| 539 | |
Ben Hutchings | 2c53040 | 2012-07-10 10:55:09 +0000 | [diff] [blame] | 540 | /** |
| 541 | * vif_delete - Delete a VIF entry |
Wang Chen | 7dc00c8 | 2008-07-14 20:56:34 -0700 | [diff] [blame] | 542 | * @notify: Set to 1, if the caller is a notifier_call |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 543 | */ |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 544 | |
Patrick McHardy | 0c12295 | 2010-04-13 05:03:22 +0000 | [diff] [blame] | 545 | static int vif_delete(struct mr_table *mrt, int vifi, int notify, |
Eric Dumazet | d17fa6f | 2009-10-28 05:21:38 +0000 | [diff] [blame] | 546 | struct list_head *head) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 547 | { |
| 548 | struct vif_device *v; |
| 549 | struct net_device *dev; |
| 550 | struct in_device *in_dev; |
| 551 | |
Patrick McHardy | 0c12295 | 2010-04-13 05:03:22 +0000 | [diff] [blame] | 552 | if (vifi < 0 || vifi >= mrt->maxvif) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 553 | return -EADDRNOTAVAIL; |
| 554 | |
Patrick McHardy | 0c12295 | 2010-04-13 05:03:22 +0000 | [diff] [blame] | 555 | v = &mrt->vif_table[vifi]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 556 | |
| 557 | write_lock_bh(&mrt_lock); |
| 558 | dev = v->dev; |
| 559 | v->dev = NULL; |
| 560 | |
| 561 | if (!dev) { |
| 562 | write_unlock_bh(&mrt_lock); |
| 563 | return -EADDRNOTAVAIL; |
| 564 | } |
| 565 | |
| 566 | #ifdef CONFIG_IP_PIMSM |
Patrick McHardy | 0c12295 | 2010-04-13 05:03:22 +0000 | [diff] [blame] | 567 | if (vifi == mrt->mroute_reg_vif_num) |
| 568 | mrt->mroute_reg_vif_num = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 569 | #endif |
| 570 | |
Eric Dumazet | a8cb16d | 2010-10-01 16:15:29 +0000 | [diff] [blame] | 571 | if (vifi + 1 == mrt->maxvif) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 572 | int tmp; |
Eric Dumazet | a8cb16d | 2010-10-01 16:15:29 +0000 | [diff] [blame] | 573 | |
| 574 | for (tmp = vifi - 1; tmp >= 0; tmp--) { |
Patrick McHardy | 0c12295 | 2010-04-13 05:03:22 +0000 | [diff] [blame] | 575 | if (VIF_EXISTS(mrt, tmp)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 576 | break; |
| 577 | } |
Patrick McHardy | 0c12295 | 2010-04-13 05:03:22 +0000 | [diff] [blame] | 578 | mrt->maxvif = tmp+1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 579 | } |
| 580 | |
| 581 | write_unlock_bh(&mrt_lock); |
| 582 | |
| 583 | dev_set_allmulti(dev, -1); |
| 584 | |
Eric Dumazet | a8cb16d | 2010-10-01 16:15:29 +0000 | [diff] [blame] | 585 | in_dev = __in_dev_get_rtnl(dev); |
| 586 | if (in_dev) { |
Herbert Xu | 42f811b | 2007-06-04 23:34:44 -0700 | [diff] [blame] | 587 | IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)--; |
Nicolas Dichtel | d67b8c6 | 2012-12-04 01:13:35 +0000 | [diff] [blame] | 588 | inet_netconf_notify_devconf(dev_net(dev), |
| 589 | NETCONFA_MC_FORWARDING, |
| 590 | dev->ifindex, &in_dev->cnf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 591 | ip_rt_multicast_event(in_dev); |
| 592 | } |
| 593 | |
Eric Dumazet | a8cb16d | 2010-10-01 16:15:29 +0000 | [diff] [blame] | 594 | if (v->flags & (VIFF_TUNNEL | VIFF_REGISTER) && !notify) |
Eric Dumazet | d17fa6f | 2009-10-28 05:21:38 +0000 | [diff] [blame] | 595 | unregister_netdevice_queue(dev, head); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 596 | |
| 597 | dev_put(dev); |
| 598 | return 0; |
| 599 | } |
| 600 | |
Eric Dumazet | a8c9486 | 2010-10-01 16:15:08 +0000 | [diff] [blame] | 601 | static void ipmr_cache_free_rcu(struct rcu_head *head) |
| 602 | { |
| 603 | struct mfc_cache *c = container_of(head, struct mfc_cache, rcu); |
| 604 | |
| 605 | kmem_cache_free(mrt_cachep, c); |
| 606 | } |
| 607 | |
Benjamin Thery | 5c0a66f | 2009-01-22 04:56:17 +0000 | [diff] [blame] | 608 | static inline void ipmr_cache_free(struct mfc_cache *c) |
| 609 | { |
Eric Dumazet | a8c9486 | 2010-10-01 16:15:08 +0000 | [diff] [blame] | 610 | call_rcu(&c->rcu, ipmr_cache_free_rcu); |
Benjamin Thery | 5c0a66f | 2009-01-22 04:56:17 +0000 | [diff] [blame] | 611 | } |
| 612 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 613 | /* Destroy an unresolved cache entry, killing queued skbs |
Eric Dumazet | a8cb16d | 2010-10-01 16:15:29 +0000 | [diff] [blame] | 614 | * and reporting error to netlink readers. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 615 | */ |
| 616 | |
Patrick McHardy | 0c12295 | 2010-04-13 05:03:22 +0000 | [diff] [blame] | 617 | static void ipmr_destroy_unres(struct mr_table *mrt, struct mfc_cache *c) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 618 | { |
Patrick McHardy | 8de53df | 2010-04-15 13:29:28 +0200 | [diff] [blame] | 619 | struct net *net = read_pnet(&mrt->net); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 620 | struct sk_buff *skb; |
Patrick McHardy | 9ef1d4c | 2005-06-28 12:55:30 -0700 | [diff] [blame] | 621 | struct nlmsgerr *e; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 622 | |
Patrick McHardy | 0c12295 | 2010-04-13 05:03:22 +0000 | [diff] [blame] | 623 | atomic_dec(&mrt->cache_resolve_queue_len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 624 | |
Jianjun Kong | c354e12 | 2008-11-03 00:28:02 -0800 | [diff] [blame] | 625 | while ((skb = skb_dequeue(&c->mfc_un.unres.unresolved))) { |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 626 | if (ip_hdr(skb)->version == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 627 | struct nlmsghdr *nlh = (struct nlmsghdr *)skb_pull(skb, sizeof(struct iphdr)); |
| 628 | nlh->nlmsg_type = NLMSG_ERROR; |
| 629 | nlh->nlmsg_len = NLMSG_LENGTH(sizeof(struct nlmsgerr)); |
| 630 | skb_trim(skb, nlh->nlmsg_len); |
Patrick McHardy | 9ef1d4c | 2005-06-28 12:55:30 -0700 | [diff] [blame] | 631 | e = NLMSG_DATA(nlh); |
| 632 | e->error = -ETIMEDOUT; |
| 633 | memset(&e->msg, 0, sizeof(e->msg)); |
Thomas Graf | 2942e90 | 2006-08-15 00:30:25 -0700 | [diff] [blame] | 634 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 635 | rtnl_unicast(skb, net, NETLINK_CB(skb).portid); |
Eric Dumazet | a8cb16d | 2010-10-01 16:15:29 +0000 | [diff] [blame] | 636 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 637 | kfree_skb(skb); |
Eric Dumazet | a8cb16d | 2010-10-01 16:15:29 +0000 | [diff] [blame] | 638 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 639 | } |
| 640 | |
Benjamin Thery | 5c0a66f | 2009-01-22 04:56:17 +0000 | [diff] [blame] | 641 | ipmr_cache_free(c); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 642 | } |
| 643 | |
| 644 | |
Patrick McHardy | e258beb | 2010-04-13 05:03:19 +0000 | [diff] [blame] | 645 | /* Timer process for the unresolved queue. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 646 | |
Patrick McHardy | e258beb | 2010-04-13 05:03:19 +0000 | [diff] [blame] | 647 | static void ipmr_expire_process(unsigned long arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 648 | { |
Patrick McHardy | 0c12295 | 2010-04-13 05:03:22 +0000 | [diff] [blame] | 649 | struct mr_table *mrt = (struct mr_table *)arg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 650 | unsigned long now; |
| 651 | unsigned long expires; |
Patrick McHardy | 862465f | 2010-04-13 05:03:21 +0000 | [diff] [blame] | 652 | struct mfc_cache *c, *next; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 653 | |
| 654 | if (!spin_trylock(&mfc_unres_lock)) { |
Patrick McHardy | 0c12295 | 2010-04-13 05:03:22 +0000 | [diff] [blame] | 655 | mod_timer(&mrt->ipmr_expire_timer, jiffies+HZ/10); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 656 | return; |
| 657 | } |
| 658 | |
Patrick McHardy | 0c12295 | 2010-04-13 05:03:22 +0000 | [diff] [blame] | 659 | if (list_empty(&mrt->mfc_unres_queue)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 660 | goto out; |
| 661 | |
| 662 | now = jiffies; |
| 663 | expires = 10*HZ; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 664 | |
Patrick McHardy | 0c12295 | 2010-04-13 05:03:22 +0000 | [diff] [blame] | 665 | list_for_each_entry_safe(c, next, &mrt->mfc_unres_queue, list) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 666 | if (time_after(c->mfc_un.unres.expires, now)) { |
| 667 | unsigned long interval = c->mfc_un.unres.expires - now; |
| 668 | if (interval < expires) |
| 669 | expires = interval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 670 | continue; |
| 671 | } |
| 672 | |
Patrick McHardy | 862465f | 2010-04-13 05:03:21 +0000 | [diff] [blame] | 673 | list_del(&c->list); |
Nicolas Dichtel | 8cd3ac9 | 2012-12-04 01:13:40 +0000 | [diff] [blame^] | 674 | mroute_netlink_event(mrt, c, RTM_DELROUTE); |
Patrick McHardy | 0c12295 | 2010-04-13 05:03:22 +0000 | [diff] [blame] | 675 | ipmr_destroy_unres(mrt, c); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 676 | } |
| 677 | |
Patrick McHardy | 0c12295 | 2010-04-13 05:03:22 +0000 | [diff] [blame] | 678 | if (!list_empty(&mrt->mfc_unres_queue)) |
| 679 | mod_timer(&mrt->ipmr_expire_timer, jiffies + expires); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 680 | |
| 681 | out: |
| 682 | spin_unlock(&mfc_unres_lock); |
| 683 | } |
| 684 | |
| 685 | /* Fill oifs list. It is called under write locked mrt_lock. */ |
| 686 | |
Patrick McHardy | 0c12295 | 2010-04-13 05:03:22 +0000 | [diff] [blame] | 687 | static void ipmr_update_thresholds(struct mr_table *mrt, struct mfc_cache *cache, |
Patrick McHardy | d658f8a | 2010-04-13 05:03:20 +0000 | [diff] [blame] | 688 | unsigned char *ttls) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 689 | { |
| 690 | int vifi; |
| 691 | |
| 692 | cache->mfc_un.res.minvif = MAXVIFS; |
| 693 | cache->mfc_un.res.maxvif = 0; |
| 694 | memset(cache->mfc_un.res.ttls, 255, MAXVIFS); |
| 695 | |
Patrick McHardy | 0c12295 | 2010-04-13 05:03:22 +0000 | [diff] [blame] | 696 | for (vifi = 0; vifi < mrt->maxvif; vifi++) { |
| 697 | if (VIF_EXISTS(mrt, vifi) && |
Benjamin Thery | cf958ae3 | 2009-01-22 04:56:16 +0000 | [diff] [blame] | 698 | ttls[vifi] && ttls[vifi] < 255) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 699 | cache->mfc_un.res.ttls[vifi] = ttls[vifi]; |
| 700 | if (cache->mfc_un.res.minvif > vifi) |
| 701 | cache->mfc_un.res.minvif = vifi; |
| 702 | if (cache->mfc_un.res.maxvif <= vifi) |
| 703 | cache->mfc_un.res.maxvif = vifi + 1; |
| 704 | } |
| 705 | } |
| 706 | } |
| 707 | |
Patrick McHardy | 0c12295 | 2010-04-13 05:03:22 +0000 | [diff] [blame] | 708 | static int vif_add(struct net *net, struct mr_table *mrt, |
| 709 | struct vifctl *vifc, int mrtsock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 710 | { |
| 711 | int vifi = vifc->vifc_vifi; |
Patrick McHardy | 0c12295 | 2010-04-13 05:03:22 +0000 | [diff] [blame] | 712 | struct vif_device *v = &mrt->vif_table[vifi]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 713 | struct net_device *dev; |
| 714 | struct in_device *in_dev; |
Wang Chen | d607032 | 2008-07-14 20:55:26 -0700 | [diff] [blame] | 715 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 716 | |
| 717 | /* Is vif busy ? */ |
Patrick McHardy | 0c12295 | 2010-04-13 05:03:22 +0000 | [diff] [blame] | 718 | if (VIF_EXISTS(mrt, vifi)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 719 | return -EADDRINUSE; |
| 720 | |
| 721 | switch (vifc->vifc_flags) { |
| 722 | #ifdef CONFIG_IP_PIMSM |
| 723 | case VIFF_REGISTER: |
| 724 | /* |
| 725 | * Special Purpose VIF in PIM |
| 726 | * All the packets will be sent to the daemon |
| 727 | */ |
Patrick McHardy | 0c12295 | 2010-04-13 05:03:22 +0000 | [diff] [blame] | 728 | if (mrt->mroute_reg_vif_num >= 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 729 | return -EADDRINUSE; |
Patrick McHardy | f0ad086 | 2010-04-13 05:03:23 +0000 | [diff] [blame] | 730 | dev = ipmr_reg_vif(net, mrt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 731 | if (!dev) |
| 732 | return -ENOBUFS; |
Wang Chen | d607032 | 2008-07-14 20:55:26 -0700 | [diff] [blame] | 733 | err = dev_set_allmulti(dev, 1); |
| 734 | if (err) { |
| 735 | unregister_netdevice(dev); |
Wang Chen | 7dc00c8 | 2008-07-14 20:56:34 -0700 | [diff] [blame] | 736 | dev_put(dev); |
Wang Chen | d607032 | 2008-07-14 20:55:26 -0700 | [diff] [blame] | 737 | return err; |
| 738 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 739 | break; |
| 740 | #endif |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 741 | case VIFF_TUNNEL: |
Benjamin Thery | 4feb88e | 2009-01-22 04:56:23 +0000 | [diff] [blame] | 742 | dev = ipmr_new_tunnel(net, vifc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 743 | if (!dev) |
| 744 | return -ENOBUFS; |
Wang Chen | d607032 | 2008-07-14 20:55:26 -0700 | [diff] [blame] | 745 | err = dev_set_allmulti(dev, 1); |
| 746 | if (err) { |
| 747 | ipmr_del_tunnel(dev, vifc); |
Wang Chen | 7dc00c8 | 2008-07-14 20:56:34 -0700 | [diff] [blame] | 748 | dev_put(dev); |
Wang Chen | d607032 | 2008-07-14 20:55:26 -0700 | [diff] [blame] | 749 | return err; |
| 750 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 751 | break; |
Ilia K | ee5e81f | 2009-09-16 05:53:07 +0000 | [diff] [blame] | 752 | |
| 753 | case VIFF_USE_IFINDEX: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 754 | case 0: |
Ilia K | ee5e81f | 2009-09-16 05:53:07 +0000 | [diff] [blame] | 755 | if (vifc->vifc_flags == VIFF_USE_IFINDEX) { |
| 756 | dev = dev_get_by_index(net, vifc->vifc_lcl_ifindex); |
Eric Dumazet | 95ae6b2 | 2010-09-15 04:04:31 +0000 | [diff] [blame] | 757 | if (dev && __in_dev_get_rtnl(dev) == NULL) { |
Ilia K | ee5e81f | 2009-09-16 05:53:07 +0000 | [diff] [blame] | 758 | dev_put(dev); |
| 759 | return -EADDRNOTAVAIL; |
| 760 | } |
Eric Dumazet | a8cb16d | 2010-10-01 16:15:29 +0000 | [diff] [blame] | 761 | } else { |
Ilia K | ee5e81f | 2009-09-16 05:53:07 +0000 | [diff] [blame] | 762 | dev = ip_dev_find(net, vifc->vifc_lcl_addr.s_addr); |
Eric Dumazet | a8cb16d | 2010-10-01 16:15:29 +0000 | [diff] [blame] | 763 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 764 | if (!dev) |
| 765 | return -EADDRNOTAVAIL; |
Wang Chen | d607032 | 2008-07-14 20:55:26 -0700 | [diff] [blame] | 766 | err = dev_set_allmulti(dev, 1); |
Wang Chen | 7dc00c8 | 2008-07-14 20:56:34 -0700 | [diff] [blame] | 767 | if (err) { |
| 768 | dev_put(dev); |
Wang Chen | d607032 | 2008-07-14 20:55:26 -0700 | [diff] [blame] | 769 | return err; |
Wang Chen | 7dc00c8 | 2008-07-14 20:56:34 -0700 | [diff] [blame] | 770 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 771 | break; |
| 772 | default: |
| 773 | return -EINVAL; |
| 774 | } |
| 775 | |
Eric Dumazet | a8cb16d | 2010-10-01 16:15:29 +0000 | [diff] [blame] | 776 | in_dev = __in_dev_get_rtnl(dev); |
| 777 | if (!in_dev) { |
Dan Carpenter | d0490cf | 2009-11-11 02:03:54 +0000 | [diff] [blame] | 778 | dev_put(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 779 | return -EADDRNOTAVAIL; |
Dan Carpenter | d0490cf | 2009-11-11 02:03:54 +0000 | [diff] [blame] | 780 | } |
Herbert Xu | 42f811b | 2007-06-04 23:34:44 -0700 | [diff] [blame] | 781 | IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)++; |
Nicolas Dichtel | d67b8c6 | 2012-12-04 01:13:35 +0000 | [diff] [blame] | 782 | inet_netconf_notify_devconf(net, NETCONFA_MC_FORWARDING, dev->ifindex, |
| 783 | &in_dev->cnf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 784 | ip_rt_multicast_event(in_dev); |
| 785 | |
Eric Dumazet | a8cb16d | 2010-10-01 16:15:29 +0000 | [diff] [blame] | 786 | /* Fill in the VIF structures */ |
| 787 | |
Jianjun Kong | c354e12 | 2008-11-03 00:28:02 -0800 | [diff] [blame] | 788 | v->rate_limit = vifc->vifc_rate_limit; |
| 789 | v->local = vifc->vifc_lcl_addr.s_addr; |
| 790 | v->remote = vifc->vifc_rmt_addr.s_addr; |
| 791 | v->flags = vifc->vifc_flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 792 | if (!mrtsock) |
| 793 | v->flags |= VIFF_STATIC; |
Jianjun Kong | c354e12 | 2008-11-03 00:28:02 -0800 | [diff] [blame] | 794 | v->threshold = vifc->vifc_threshold; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 795 | v->bytes_in = 0; |
| 796 | v->bytes_out = 0; |
| 797 | v->pkt_in = 0; |
| 798 | v->pkt_out = 0; |
| 799 | v->link = dev->ifindex; |
Eric Dumazet | a8cb16d | 2010-10-01 16:15:29 +0000 | [diff] [blame] | 800 | if (v->flags & (VIFF_TUNNEL | VIFF_REGISTER)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 801 | v->link = dev->iflink; |
| 802 | |
| 803 | /* And finish update writing critical data */ |
| 804 | write_lock_bh(&mrt_lock); |
Jianjun Kong | c354e12 | 2008-11-03 00:28:02 -0800 | [diff] [blame] | 805 | v->dev = dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 806 | #ifdef CONFIG_IP_PIMSM |
Eric Dumazet | a8cb16d | 2010-10-01 16:15:29 +0000 | [diff] [blame] | 807 | if (v->flags & VIFF_REGISTER) |
Patrick McHardy | 0c12295 | 2010-04-13 05:03:22 +0000 | [diff] [blame] | 808 | mrt->mroute_reg_vif_num = vifi; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 809 | #endif |
Patrick McHardy | 0c12295 | 2010-04-13 05:03:22 +0000 | [diff] [blame] | 810 | if (vifi+1 > mrt->maxvif) |
| 811 | mrt->maxvif = vifi+1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 812 | write_unlock_bh(&mrt_lock); |
| 813 | return 0; |
| 814 | } |
| 815 | |
Eric Dumazet | a8c9486 | 2010-10-01 16:15:08 +0000 | [diff] [blame] | 816 | /* called with rcu_read_lock() */ |
Patrick McHardy | 0c12295 | 2010-04-13 05:03:22 +0000 | [diff] [blame] | 817 | static struct mfc_cache *ipmr_cache_find(struct mr_table *mrt, |
Benjamin Thery | 4feb88e | 2009-01-22 04:56:23 +0000 | [diff] [blame] | 818 | __be32 origin, |
| 819 | __be32 mcastgrp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 820 | { |
Jianjun Kong | c354e12 | 2008-11-03 00:28:02 -0800 | [diff] [blame] | 821 | int line = MFC_HASH(mcastgrp, origin); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 822 | struct mfc_cache *c; |
| 823 | |
Eric Dumazet | a8c9486 | 2010-10-01 16:15:08 +0000 | [diff] [blame] | 824 | list_for_each_entry_rcu(c, &mrt->mfc_cache_array[line], list) { |
Patrick McHardy | 862465f | 2010-04-13 05:03:21 +0000 | [diff] [blame] | 825 | if (c->mfc_origin == origin && c->mfc_mcastgrp == mcastgrp) |
| 826 | return c; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 827 | } |
Patrick McHardy | 862465f | 2010-04-13 05:03:21 +0000 | [diff] [blame] | 828 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 829 | } |
| 830 | |
| 831 | /* |
| 832 | * Allocate a multicast cache entry |
| 833 | */ |
Patrick McHardy | d658f8a | 2010-04-13 05:03:20 +0000 | [diff] [blame] | 834 | static struct mfc_cache *ipmr_cache_alloc(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 835 | { |
Jianjun Kong | c354e12 | 2008-11-03 00:28:02 -0800 | [diff] [blame] | 836 | struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_KERNEL); |
Eric Dumazet | a8c9486 | 2010-10-01 16:15:08 +0000 | [diff] [blame] | 837 | |
| 838 | if (c) |
| 839 | c->mfc_un.res.minvif = MAXVIFS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 840 | return c; |
| 841 | } |
| 842 | |
Patrick McHardy | d658f8a | 2010-04-13 05:03:20 +0000 | [diff] [blame] | 843 | static struct mfc_cache *ipmr_cache_alloc_unres(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 844 | { |
Jianjun Kong | c354e12 | 2008-11-03 00:28:02 -0800 | [diff] [blame] | 845 | struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_ATOMIC); |
Eric Dumazet | a8c9486 | 2010-10-01 16:15:08 +0000 | [diff] [blame] | 846 | |
| 847 | if (c) { |
| 848 | skb_queue_head_init(&c->mfc_un.unres.unresolved); |
| 849 | c->mfc_un.unres.expires = jiffies + 10*HZ; |
| 850 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 851 | return c; |
| 852 | } |
| 853 | |
| 854 | /* |
| 855 | * A cache entry has gone into a resolved state from queued |
| 856 | */ |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 857 | |
Patrick McHardy | 0c12295 | 2010-04-13 05:03:22 +0000 | [diff] [blame] | 858 | static void ipmr_cache_resolve(struct net *net, struct mr_table *mrt, |
| 859 | struct mfc_cache *uc, struct mfc_cache *c) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 860 | { |
| 861 | struct sk_buff *skb; |
Patrick McHardy | 9ef1d4c | 2005-06-28 12:55:30 -0700 | [diff] [blame] | 862 | struct nlmsgerr *e; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 863 | |
Eric Dumazet | a8cb16d | 2010-10-01 16:15:29 +0000 | [diff] [blame] | 864 | /* Play the pending entries through our router */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 865 | |
Jianjun Kong | c354e12 | 2008-11-03 00:28:02 -0800 | [diff] [blame] | 866 | while ((skb = __skb_dequeue(&uc->mfc_un.unres.unresolved))) { |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 867 | if (ip_hdr(skb)->version == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 868 | struct nlmsghdr *nlh = (struct nlmsghdr *)skb_pull(skb, sizeof(struct iphdr)); |
| 869 | |
Patrick McHardy | cb6a4e4 | 2010-04-26 16:02:08 +0200 | [diff] [blame] | 870 | if (__ipmr_fill_mroute(mrt, skb, c, NLMSG_DATA(nlh)) > 0) { |
Eric Dumazet | a8cb16d | 2010-10-01 16:15:29 +0000 | [diff] [blame] | 871 | nlh->nlmsg_len = skb_tail_pointer(skb) - |
| 872 | (u8 *)nlh; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 873 | } else { |
| 874 | nlh->nlmsg_type = NLMSG_ERROR; |
| 875 | nlh->nlmsg_len = NLMSG_LENGTH(sizeof(struct nlmsgerr)); |
| 876 | skb_trim(skb, nlh->nlmsg_len); |
Patrick McHardy | 9ef1d4c | 2005-06-28 12:55:30 -0700 | [diff] [blame] | 877 | e = NLMSG_DATA(nlh); |
| 878 | e->error = -EMSGSIZE; |
| 879 | memset(&e->msg, 0, sizeof(e->msg)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 880 | } |
Thomas Graf | 2942e90 | 2006-08-15 00:30:25 -0700 | [diff] [blame] | 881 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 882 | rtnl_unicast(skb, net, NETLINK_CB(skb).portid); |
Eric Dumazet | a8cb16d | 2010-10-01 16:15:29 +0000 | [diff] [blame] | 883 | } else { |
Patrick McHardy | 0c12295 | 2010-04-13 05:03:22 +0000 | [diff] [blame] | 884 | ip_mr_forward(net, mrt, skb, c, 0); |
Eric Dumazet | a8cb16d | 2010-10-01 16:15:29 +0000 | [diff] [blame] | 885 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 886 | } |
| 887 | } |
| 888 | |
| 889 | /* |
| 890 | * Bounce a cache query up to mrouted. We could use netlink for this but mrouted |
| 891 | * expects the following bizarre scheme. |
| 892 | * |
| 893 | * Called under mrt_lock. |
| 894 | */ |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 895 | |
Patrick McHardy | 0c12295 | 2010-04-13 05:03:22 +0000 | [diff] [blame] | 896 | static int ipmr_cache_report(struct mr_table *mrt, |
Benjamin Thery | 4feb88e | 2009-01-22 04:56:23 +0000 | [diff] [blame] | 897 | struct sk_buff *pkt, vifi_t vifi, int assert) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 898 | { |
| 899 | struct sk_buff *skb; |
Arnaldo Carvalho de Melo | c9bdd4b | 2007-03-12 20:09:15 -0300 | [diff] [blame] | 900 | const int ihl = ip_hdrlen(pkt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 901 | struct igmphdr *igmp; |
| 902 | struct igmpmsg *msg; |
Eric Dumazet | 4c968709 | 2010-10-01 16:15:01 +0000 | [diff] [blame] | 903 | struct sock *mroute_sk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 904 | int ret; |
| 905 | |
| 906 | #ifdef CONFIG_IP_PIMSM |
| 907 | if (assert == IGMPMSG_WHOLEPKT) |
| 908 | skb = skb_realloc_headroom(pkt, sizeof(struct iphdr)); |
| 909 | else |
| 910 | #endif |
| 911 | skb = alloc_skb(128, GFP_ATOMIC); |
| 912 | |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 913 | if (!skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 914 | return -ENOBUFS; |
| 915 | |
| 916 | #ifdef CONFIG_IP_PIMSM |
| 917 | if (assert == IGMPMSG_WHOLEPKT) { |
| 918 | /* Ugly, but we have no choice with this interface. |
Eric Dumazet | a8cb16d | 2010-10-01 16:15:29 +0000 | [diff] [blame] | 919 | * Duplicate old header, fix ihl, length etc. |
| 920 | * And all this only to mangle msg->im_msgtype and |
| 921 | * to set msg->im_mbz to "mbz" :-) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 922 | */ |
Arnaldo Carvalho de Melo | 878c814 | 2007-03-11 22:38:29 -0300 | [diff] [blame] | 923 | skb_push(skb, sizeof(struct iphdr)); |
| 924 | skb_reset_network_header(skb); |
Arnaldo Carvalho de Melo | badff6d | 2007-03-13 13:06:52 -0300 | [diff] [blame] | 925 | skb_reset_transport_header(skb); |
Arnaldo Carvalho de Melo | 0272ffc | 2007-03-12 20:05:39 -0300 | [diff] [blame] | 926 | msg = (struct igmpmsg *)skb_network_header(skb); |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 927 | memcpy(msg, skb_network_header(pkt), sizeof(struct iphdr)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 928 | msg->im_msgtype = IGMPMSG_WHOLEPKT; |
| 929 | msg->im_mbz = 0; |
Patrick McHardy | 0c12295 | 2010-04-13 05:03:22 +0000 | [diff] [blame] | 930 | msg->im_vif = mrt->mroute_reg_vif_num; |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 931 | ip_hdr(skb)->ihl = sizeof(struct iphdr) >> 2; |
| 932 | ip_hdr(skb)->tot_len = htons(ntohs(ip_hdr(pkt)->tot_len) + |
| 933 | sizeof(struct iphdr)); |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 934 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 935 | #endif |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 936 | { |
| 937 | |
Eric Dumazet | a8cb16d | 2010-10-01 16:15:29 +0000 | [diff] [blame] | 938 | /* Copy the IP header */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 939 | |
Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame] | 940 | skb->network_header = skb->tail; |
Arnaldo Carvalho de Melo | ddc7b8e | 2007-03-15 21:42:27 -0300 | [diff] [blame] | 941 | skb_put(skb, ihl); |
Arnaldo Carvalho de Melo | 27d7ff4 | 2007-03-31 11:55:19 -0300 | [diff] [blame] | 942 | skb_copy_to_linear_data(skb, pkt->data, ihl); |
Eric Dumazet | a8cb16d | 2010-10-01 16:15:29 +0000 | [diff] [blame] | 943 | ip_hdr(skb)->protocol = 0; /* Flag to the kernel this is a route add */ |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 944 | msg = (struct igmpmsg *)skb_network_header(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 945 | msg->im_vif = vifi; |
Eric Dumazet | adf3090 | 2009-06-02 05:19:30 +0000 | [diff] [blame] | 946 | skb_dst_set(skb, dst_clone(skb_dst(pkt))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 947 | |
Eric Dumazet | a8cb16d | 2010-10-01 16:15:29 +0000 | [diff] [blame] | 948 | /* Add our header */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 949 | |
Eric Dumazet | a8cb16d | 2010-10-01 16:15:29 +0000 | [diff] [blame] | 950 | igmp = (struct igmphdr *)skb_put(skb, sizeof(struct igmphdr)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 951 | igmp->type = |
| 952 | msg->im_msgtype = assert; |
Eric Dumazet | a8cb16d | 2010-10-01 16:15:29 +0000 | [diff] [blame] | 953 | igmp->code = 0; |
| 954 | ip_hdr(skb)->tot_len = htons(skb->len); /* Fix the length */ |
Arnaldo Carvalho de Melo | b0e380b | 2007-04-10 21:21:55 -0700 | [diff] [blame] | 955 | skb->transport_header = skb->network_header; |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 956 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 957 | |
Eric Dumazet | 4c968709 | 2010-10-01 16:15:01 +0000 | [diff] [blame] | 958 | rcu_read_lock(); |
| 959 | mroute_sk = rcu_dereference(mrt->mroute_sk); |
| 960 | if (mroute_sk == NULL) { |
| 961 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 962 | kfree_skb(skb); |
| 963 | return -EINVAL; |
| 964 | } |
| 965 | |
Eric Dumazet | a8cb16d | 2010-10-01 16:15:29 +0000 | [diff] [blame] | 966 | /* Deliver to mrouted */ |
| 967 | |
Eric Dumazet | 4c968709 | 2010-10-01 16:15:01 +0000 | [diff] [blame] | 968 | ret = sock_queue_rcv_skb(mroute_sk, skb); |
| 969 | rcu_read_unlock(); |
Benjamin Thery | 70a269e | 2009-01-22 04:56:15 +0000 | [diff] [blame] | 970 | if (ret < 0) { |
Joe Perches | e87cc47 | 2012-05-13 21:56:26 +0000 | [diff] [blame] | 971 | net_warn_ratelimited("mroute: pending queue full, dropping entries\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 972 | kfree_skb(skb); |
| 973 | } |
| 974 | |
| 975 | return ret; |
| 976 | } |
| 977 | |
| 978 | /* |
| 979 | * Queue a packet for resolution. It gets locked cache entry! |
| 980 | */ |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 981 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 982 | static int |
Patrick McHardy | 0c12295 | 2010-04-13 05:03:22 +0000 | [diff] [blame] | 983 | ipmr_cache_unresolved(struct mr_table *mrt, vifi_t vifi, struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 984 | { |
Patrick McHardy | 862465f | 2010-04-13 05:03:21 +0000 | [diff] [blame] | 985 | bool found = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 986 | int err; |
| 987 | struct mfc_cache *c; |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 988 | const struct iphdr *iph = ip_hdr(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 989 | |
| 990 | spin_lock_bh(&mfc_unres_lock); |
Patrick McHardy | 0c12295 | 2010-04-13 05:03:22 +0000 | [diff] [blame] | 991 | list_for_each_entry(c, &mrt->mfc_unres_queue, list) { |
Patrick McHardy | e258beb | 2010-04-13 05:03:19 +0000 | [diff] [blame] | 992 | if (c->mfc_mcastgrp == iph->daddr && |
Patrick McHardy | 862465f | 2010-04-13 05:03:21 +0000 | [diff] [blame] | 993 | c->mfc_origin == iph->saddr) { |
| 994 | found = true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 995 | break; |
Patrick McHardy | 862465f | 2010-04-13 05:03:21 +0000 | [diff] [blame] | 996 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 997 | } |
| 998 | |
Patrick McHardy | 862465f | 2010-04-13 05:03:21 +0000 | [diff] [blame] | 999 | if (!found) { |
Eric Dumazet | a8cb16d | 2010-10-01 16:15:29 +0000 | [diff] [blame] | 1000 | /* Create a new entry if allowable */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1001 | |
Patrick McHardy | 0c12295 | 2010-04-13 05:03:22 +0000 | [diff] [blame] | 1002 | if (atomic_read(&mrt->cache_resolve_queue_len) >= 10 || |
Patrick McHardy | d658f8a | 2010-04-13 05:03:20 +0000 | [diff] [blame] | 1003 | (c = ipmr_cache_alloc_unres()) == NULL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1004 | spin_unlock_bh(&mfc_unres_lock); |
| 1005 | |
| 1006 | kfree_skb(skb); |
| 1007 | return -ENOBUFS; |
| 1008 | } |
| 1009 | |
Eric Dumazet | a8cb16d | 2010-10-01 16:15:29 +0000 | [diff] [blame] | 1010 | /* Fill in the new cache entry */ |
| 1011 | |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 1012 | c->mfc_parent = -1; |
| 1013 | c->mfc_origin = iph->saddr; |
| 1014 | c->mfc_mcastgrp = iph->daddr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1015 | |
Eric Dumazet | a8cb16d | 2010-10-01 16:15:29 +0000 | [diff] [blame] | 1016 | /* Reflect first query at mrouted. */ |
| 1017 | |
Patrick McHardy | 0c12295 | 2010-04-13 05:03:22 +0000 | [diff] [blame] | 1018 | err = ipmr_cache_report(mrt, skb, vifi, IGMPMSG_NOCACHE); |
Benjamin Thery | 4feb88e | 2009-01-22 04:56:23 +0000 | [diff] [blame] | 1019 | if (err < 0) { |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 1020 | /* If the report failed throw the cache entry |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1021 | out - Brad Parker |
| 1022 | */ |
| 1023 | spin_unlock_bh(&mfc_unres_lock); |
| 1024 | |
Benjamin Thery | 5c0a66f | 2009-01-22 04:56:17 +0000 | [diff] [blame] | 1025 | ipmr_cache_free(c); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1026 | kfree_skb(skb); |
| 1027 | return err; |
| 1028 | } |
| 1029 | |
Patrick McHardy | 0c12295 | 2010-04-13 05:03:22 +0000 | [diff] [blame] | 1030 | atomic_inc(&mrt->cache_resolve_queue_len); |
| 1031 | list_add(&c->list, &mrt->mfc_unres_queue); |
Nicolas Dichtel | 8cd3ac9 | 2012-12-04 01:13:40 +0000 | [diff] [blame^] | 1032 | mroute_netlink_event(mrt, c, RTM_NEWROUTE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1033 | |
David S. Miller | 278554b | 2010-05-12 00:05:35 -0700 | [diff] [blame] | 1034 | if (atomic_read(&mrt->cache_resolve_queue_len) == 1) |
| 1035 | mod_timer(&mrt->ipmr_expire_timer, c->mfc_un.unres.expires); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1036 | } |
| 1037 | |
Eric Dumazet | a8cb16d | 2010-10-01 16:15:29 +0000 | [diff] [blame] | 1038 | /* See if we can append the packet */ |
| 1039 | |
| 1040 | if (c->mfc_un.unres.unresolved.qlen > 3) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1041 | kfree_skb(skb); |
| 1042 | err = -ENOBUFS; |
| 1043 | } else { |
Jianjun Kong | c354e12 | 2008-11-03 00:28:02 -0800 | [diff] [blame] | 1044 | skb_queue_tail(&c->mfc_un.unres.unresolved, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1045 | err = 0; |
| 1046 | } |
| 1047 | |
| 1048 | spin_unlock_bh(&mfc_unres_lock); |
| 1049 | return err; |
| 1050 | } |
| 1051 | |
| 1052 | /* |
| 1053 | * MFC cache manipulation by user space mroute daemon |
| 1054 | */ |
| 1055 | |
Patrick McHardy | 0c12295 | 2010-04-13 05:03:22 +0000 | [diff] [blame] | 1056 | static int ipmr_mfc_delete(struct mr_table *mrt, struct mfcctl *mfc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1057 | { |
| 1058 | int line; |
Patrick McHardy | 862465f | 2010-04-13 05:03:21 +0000 | [diff] [blame] | 1059 | struct mfc_cache *c, *next; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1060 | |
Jianjun Kong | c354e12 | 2008-11-03 00:28:02 -0800 | [diff] [blame] | 1061 | line = MFC_HASH(mfc->mfcc_mcastgrp.s_addr, mfc->mfcc_origin.s_addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1062 | |
Patrick McHardy | 0c12295 | 2010-04-13 05:03:22 +0000 | [diff] [blame] | 1063 | list_for_each_entry_safe(c, next, &mrt->mfc_cache_array[line], list) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1064 | if (c->mfc_origin == mfc->mfcc_origin.s_addr && |
| 1065 | c->mfc_mcastgrp == mfc->mfcc_mcastgrp.s_addr) { |
Eric Dumazet | a8c9486 | 2010-10-01 16:15:08 +0000 | [diff] [blame] | 1066 | list_del_rcu(&c->list); |
Nicolas Dichtel | 8cd3ac9 | 2012-12-04 01:13:40 +0000 | [diff] [blame^] | 1067 | mroute_netlink_event(mrt, c, RTM_DELROUTE); |
Benjamin Thery | 5c0a66f | 2009-01-22 04:56:17 +0000 | [diff] [blame] | 1068 | ipmr_cache_free(c); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1069 | return 0; |
| 1070 | } |
| 1071 | } |
| 1072 | return -ENOENT; |
| 1073 | } |
| 1074 | |
Patrick McHardy | 0c12295 | 2010-04-13 05:03:22 +0000 | [diff] [blame] | 1075 | static int ipmr_mfc_add(struct net *net, struct mr_table *mrt, |
| 1076 | struct mfcctl *mfc, int mrtsock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1077 | { |
Patrick McHardy | 862465f | 2010-04-13 05:03:21 +0000 | [diff] [blame] | 1078 | bool found = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1079 | int line; |
Patrick McHardy | 862465f | 2010-04-13 05:03:21 +0000 | [diff] [blame] | 1080 | struct mfc_cache *uc, *c; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1081 | |
Patrick McHardy | a50436f2 | 2010-03-17 06:04:14 +0000 | [diff] [blame] | 1082 | if (mfc->mfcc_parent >= MAXVIFS) |
| 1083 | return -ENFILE; |
| 1084 | |
Jianjun Kong | c354e12 | 2008-11-03 00:28:02 -0800 | [diff] [blame] | 1085 | line = MFC_HASH(mfc->mfcc_mcastgrp.s_addr, mfc->mfcc_origin.s_addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1086 | |
Patrick McHardy | 0c12295 | 2010-04-13 05:03:22 +0000 | [diff] [blame] | 1087 | list_for_each_entry(c, &mrt->mfc_cache_array[line], list) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1088 | if (c->mfc_origin == mfc->mfcc_origin.s_addr && |
Patrick McHardy | 862465f | 2010-04-13 05:03:21 +0000 | [diff] [blame] | 1089 | c->mfc_mcastgrp == mfc->mfcc_mcastgrp.s_addr) { |
| 1090 | found = true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1091 | break; |
Patrick McHardy | 862465f | 2010-04-13 05:03:21 +0000 | [diff] [blame] | 1092 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1093 | } |
| 1094 | |
Patrick McHardy | 862465f | 2010-04-13 05:03:21 +0000 | [diff] [blame] | 1095 | if (found) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1096 | write_lock_bh(&mrt_lock); |
| 1097 | c->mfc_parent = mfc->mfcc_parent; |
Patrick McHardy | 0c12295 | 2010-04-13 05:03:22 +0000 | [diff] [blame] | 1098 | ipmr_update_thresholds(mrt, c, mfc->mfcc_ttls); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1099 | if (!mrtsock) |
| 1100 | c->mfc_flags |= MFC_STATIC; |
| 1101 | write_unlock_bh(&mrt_lock); |
Nicolas Dichtel | 8cd3ac9 | 2012-12-04 01:13:40 +0000 | [diff] [blame^] | 1102 | mroute_netlink_event(mrt, c, RTM_NEWROUTE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1103 | return 0; |
| 1104 | } |
| 1105 | |
Joe Perches | f97c1e0 | 2007-12-16 13:45:43 -0800 | [diff] [blame] | 1106 | if (!ipv4_is_multicast(mfc->mfcc_mcastgrp.s_addr)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1107 | return -EINVAL; |
| 1108 | |
Patrick McHardy | d658f8a | 2010-04-13 05:03:20 +0000 | [diff] [blame] | 1109 | c = ipmr_cache_alloc(); |
Jianjun Kong | c354e12 | 2008-11-03 00:28:02 -0800 | [diff] [blame] | 1110 | if (c == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1111 | return -ENOMEM; |
| 1112 | |
Jianjun Kong | c354e12 | 2008-11-03 00:28:02 -0800 | [diff] [blame] | 1113 | c->mfc_origin = mfc->mfcc_origin.s_addr; |
| 1114 | c->mfc_mcastgrp = mfc->mfcc_mcastgrp.s_addr; |
| 1115 | c->mfc_parent = mfc->mfcc_parent; |
Patrick McHardy | 0c12295 | 2010-04-13 05:03:22 +0000 | [diff] [blame] | 1116 | ipmr_update_thresholds(mrt, c, mfc->mfcc_ttls); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1117 | if (!mrtsock) |
| 1118 | c->mfc_flags |= MFC_STATIC; |
| 1119 | |
Eric Dumazet | a8c9486 | 2010-10-01 16:15:08 +0000 | [diff] [blame] | 1120 | list_add_rcu(&c->list, &mrt->mfc_cache_array[line]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1121 | |
| 1122 | /* |
| 1123 | * Check to see if we resolved a queued list. If so we |
| 1124 | * need to send on the frames and tidy up. |
| 1125 | */ |
Patrick McHardy | b0ebb73 | 2010-04-15 13:29:28 +0200 | [diff] [blame] | 1126 | found = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1127 | spin_lock_bh(&mfc_unres_lock); |
Patrick McHardy | 0c12295 | 2010-04-13 05:03:22 +0000 | [diff] [blame] | 1128 | list_for_each_entry(uc, &mrt->mfc_unres_queue, list) { |
Patrick McHardy | e258beb | 2010-04-13 05:03:19 +0000 | [diff] [blame] | 1129 | if (uc->mfc_origin == c->mfc_origin && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1130 | uc->mfc_mcastgrp == c->mfc_mcastgrp) { |
Patrick McHardy | 862465f | 2010-04-13 05:03:21 +0000 | [diff] [blame] | 1131 | list_del(&uc->list); |
Patrick McHardy | 0c12295 | 2010-04-13 05:03:22 +0000 | [diff] [blame] | 1132 | atomic_dec(&mrt->cache_resolve_queue_len); |
Patrick McHardy | b0ebb73 | 2010-04-15 13:29:28 +0200 | [diff] [blame] | 1133 | found = true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1134 | break; |
| 1135 | } |
| 1136 | } |
Patrick McHardy | 0c12295 | 2010-04-13 05:03:22 +0000 | [diff] [blame] | 1137 | if (list_empty(&mrt->mfc_unres_queue)) |
| 1138 | del_timer(&mrt->ipmr_expire_timer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1139 | spin_unlock_bh(&mfc_unres_lock); |
| 1140 | |
Patrick McHardy | b0ebb73 | 2010-04-15 13:29:28 +0200 | [diff] [blame] | 1141 | if (found) { |
Patrick McHardy | 0c12295 | 2010-04-13 05:03:22 +0000 | [diff] [blame] | 1142 | ipmr_cache_resolve(net, mrt, uc, c); |
Benjamin Thery | 5c0a66f | 2009-01-22 04:56:17 +0000 | [diff] [blame] | 1143 | ipmr_cache_free(uc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1144 | } |
Nicolas Dichtel | 8cd3ac9 | 2012-12-04 01:13:40 +0000 | [diff] [blame^] | 1145 | mroute_netlink_event(mrt, c, RTM_NEWROUTE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1146 | return 0; |
| 1147 | } |
| 1148 | |
| 1149 | /* |
| 1150 | * Close the multicast socket, and clear the vif tables etc |
| 1151 | */ |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 1152 | |
Patrick McHardy | 0c12295 | 2010-04-13 05:03:22 +0000 | [diff] [blame] | 1153 | static void mroute_clean_tables(struct mr_table *mrt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1154 | { |
| 1155 | int i; |
Eric Dumazet | d17fa6f | 2009-10-28 05:21:38 +0000 | [diff] [blame] | 1156 | LIST_HEAD(list); |
Patrick McHardy | 862465f | 2010-04-13 05:03:21 +0000 | [diff] [blame] | 1157 | struct mfc_cache *c, *next; |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 1158 | |
Eric Dumazet | a8cb16d | 2010-10-01 16:15:29 +0000 | [diff] [blame] | 1159 | /* Shut down all active vif entries */ |
| 1160 | |
Patrick McHardy | 0c12295 | 2010-04-13 05:03:22 +0000 | [diff] [blame] | 1161 | for (i = 0; i < mrt->maxvif; i++) { |
Eric Dumazet | a8cb16d | 2010-10-01 16:15:29 +0000 | [diff] [blame] | 1162 | if (!(mrt->vif_table[i].flags & VIFF_STATIC)) |
Patrick McHardy | 0c12295 | 2010-04-13 05:03:22 +0000 | [diff] [blame] | 1163 | vif_delete(mrt, i, 0, &list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1164 | } |
Eric Dumazet | d17fa6f | 2009-10-28 05:21:38 +0000 | [diff] [blame] | 1165 | unregister_netdevice_many(&list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1166 | |
Eric Dumazet | a8cb16d | 2010-10-01 16:15:29 +0000 | [diff] [blame] | 1167 | /* Wipe the cache */ |
| 1168 | |
Patrick McHardy | 862465f | 2010-04-13 05:03:21 +0000 | [diff] [blame] | 1169 | for (i = 0; i < MFC_LINES; i++) { |
Patrick McHardy | 0c12295 | 2010-04-13 05:03:22 +0000 | [diff] [blame] | 1170 | list_for_each_entry_safe(c, next, &mrt->mfc_cache_array[i], list) { |
Eric Dumazet | a8c9486 | 2010-10-01 16:15:08 +0000 | [diff] [blame] | 1171 | if (c->mfc_flags & MFC_STATIC) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1172 | continue; |
Eric Dumazet | a8c9486 | 2010-10-01 16:15:08 +0000 | [diff] [blame] | 1173 | list_del_rcu(&c->list); |
Nicolas Dichtel | 8cd3ac9 | 2012-12-04 01:13:40 +0000 | [diff] [blame^] | 1174 | mroute_netlink_event(mrt, c, RTM_DELROUTE); |
Benjamin Thery | 5c0a66f | 2009-01-22 04:56:17 +0000 | [diff] [blame] | 1175 | ipmr_cache_free(c); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1176 | } |
| 1177 | } |
| 1178 | |
Patrick McHardy | 0c12295 | 2010-04-13 05:03:22 +0000 | [diff] [blame] | 1179 | if (atomic_read(&mrt->cache_resolve_queue_len) != 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1180 | spin_lock_bh(&mfc_unres_lock); |
Patrick McHardy | 0c12295 | 2010-04-13 05:03:22 +0000 | [diff] [blame] | 1181 | list_for_each_entry_safe(c, next, &mrt->mfc_unres_queue, list) { |
Patrick McHardy | 862465f | 2010-04-13 05:03:21 +0000 | [diff] [blame] | 1182 | list_del(&c->list); |
Nicolas Dichtel | 8cd3ac9 | 2012-12-04 01:13:40 +0000 | [diff] [blame^] | 1183 | mroute_netlink_event(mrt, c, RTM_DELROUTE); |
Patrick McHardy | 0c12295 | 2010-04-13 05:03:22 +0000 | [diff] [blame] | 1184 | ipmr_destroy_unres(mrt, c); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1185 | } |
| 1186 | spin_unlock_bh(&mfc_unres_lock); |
| 1187 | } |
| 1188 | } |
| 1189 | |
Eric Dumazet | 4c968709 | 2010-10-01 16:15:01 +0000 | [diff] [blame] | 1190 | /* called from ip_ra_control(), before an RCU grace period, |
| 1191 | * we dont need to call synchronize_rcu() here |
| 1192 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1193 | static void mrtsock_destruct(struct sock *sk) |
| 1194 | { |
Benjamin Thery | 4feb88e | 2009-01-22 04:56:23 +0000 | [diff] [blame] | 1195 | struct net *net = sock_net(sk); |
Patrick McHardy | f0ad086 | 2010-04-13 05:03:23 +0000 | [diff] [blame] | 1196 | struct mr_table *mrt; |
Benjamin Thery | 4feb88e | 2009-01-22 04:56:23 +0000 | [diff] [blame] | 1197 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1198 | rtnl_lock(); |
Patrick McHardy | f0ad086 | 2010-04-13 05:03:23 +0000 | [diff] [blame] | 1199 | ipmr_for_each_table(mrt, net) { |
Eric Dumazet | 4c968709 | 2010-10-01 16:15:01 +0000 | [diff] [blame] | 1200 | if (sk == rtnl_dereference(mrt->mroute_sk)) { |
Patrick McHardy | f0ad086 | 2010-04-13 05:03:23 +0000 | [diff] [blame] | 1201 | IPV4_DEVCONF_ALL(net, MC_FORWARDING)--; |
Nicolas Dichtel | d67b8c6 | 2012-12-04 01:13:35 +0000 | [diff] [blame] | 1202 | inet_netconf_notify_devconf(net, NETCONFA_MC_FORWARDING, |
| 1203 | NETCONFA_IFINDEX_ALL, |
| 1204 | net->ipv4.devconf_all); |
Stephen Hemminger | a9b3cd7 | 2011-08-01 16:19:00 +0000 | [diff] [blame] | 1205 | RCU_INIT_POINTER(mrt->mroute_sk, NULL); |
Patrick McHardy | f0ad086 | 2010-04-13 05:03:23 +0000 | [diff] [blame] | 1206 | mroute_clean_tables(mrt); |
| 1207 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1208 | } |
| 1209 | rtnl_unlock(); |
| 1210 | } |
| 1211 | |
| 1212 | /* |
| 1213 | * Socket options and virtual interface manipulation. The whole |
| 1214 | * virtual interface system is a complete heap, but unfortunately |
| 1215 | * that's how BSD mrouted happens to think. Maybe one day with a proper |
| 1216 | * MOSPF/PIM router set up we can clean this up. |
| 1217 | */ |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 1218 | |
David S. Miller | b705884 | 2009-09-30 16:12:20 -0700 | [diff] [blame] | 1219 | int ip_mroute_setsockopt(struct sock *sk, int optname, char __user *optval, unsigned int optlen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1220 | { |
| 1221 | int ret; |
| 1222 | struct vifctl vif; |
| 1223 | struct mfcctl mfc; |
Benjamin Thery | 4feb88e | 2009-01-22 04:56:23 +0000 | [diff] [blame] | 1224 | struct net *net = sock_net(sk); |
Patrick McHardy | f0ad086 | 2010-04-13 05:03:23 +0000 | [diff] [blame] | 1225 | struct mr_table *mrt; |
| 1226 | |
Eric Dumazet | 5e1859f | 2012-11-25 06:41:45 +0000 | [diff] [blame] | 1227 | if (sk->sk_type != SOCK_RAW || |
| 1228 | inet_sk(sk)->inet_num != IPPROTO_IGMP) |
| 1229 | return -EOPNOTSUPP; |
| 1230 | |
Patrick McHardy | f0ad086 | 2010-04-13 05:03:23 +0000 | [diff] [blame] | 1231 | mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT); |
| 1232 | if (mrt == NULL) |
| 1233 | return -ENOENT; |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 1234 | |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1235 | if (optname != MRT_INIT) { |
Eric Dumazet | 33d480c | 2011-08-11 19:30:52 +0000 | [diff] [blame] | 1236 | if (sk != rcu_access_pointer(mrt->mroute_sk) && |
Eric W. Biederman | 52e804c | 2012-11-16 03:03:05 +0000 | [diff] [blame] | 1237 | !ns_capable(net->user_ns, CAP_NET_ADMIN)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1238 | return -EACCES; |
| 1239 | } |
| 1240 | |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1241 | switch (optname) { |
| 1242 | case MRT_INIT: |
Jianjun Kong | c354e12 | 2008-11-03 00:28:02 -0800 | [diff] [blame] | 1243 | if (optlen != sizeof(int)) |
Eric Dumazet | 5e1859f | 2012-11-25 06:41:45 +0000 | [diff] [blame] | 1244 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1245 | |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1246 | rtnl_lock(); |
Eric Dumazet | 4c968709 | 2010-10-01 16:15:01 +0000 | [diff] [blame] | 1247 | if (rtnl_dereference(mrt->mroute_sk)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1248 | rtnl_unlock(); |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1249 | return -EADDRINUSE; |
| 1250 | } |
| 1251 | |
| 1252 | ret = ip_ra_control(sk, 1, mrtsock_destruct); |
| 1253 | if (ret == 0) { |
Eric Dumazet | cf778b0 | 2012-01-12 04:41:32 +0000 | [diff] [blame] | 1254 | rcu_assign_pointer(mrt->mroute_sk, sk); |
Benjamin Thery | 4feb88e | 2009-01-22 04:56:23 +0000 | [diff] [blame] | 1255 | IPV4_DEVCONF_ALL(net, MC_FORWARDING)++; |
Nicolas Dichtel | d67b8c6 | 2012-12-04 01:13:35 +0000 | [diff] [blame] | 1256 | inet_netconf_notify_devconf(net, NETCONFA_MC_FORWARDING, |
| 1257 | NETCONFA_IFINDEX_ALL, |
| 1258 | net->ipv4.devconf_all); |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1259 | } |
| 1260 | rtnl_unlock(); |
| 1261 | return ret; |
| 1262 | case MRT_DONE: |
Eric Dumazet | 33d480c | 2011-08-11 19:30:52 +0000 | [diff] [blame] | 1263 | if (sk != rcu_access_pointer(mrt->mroute_sk)) |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1264 | return -EACCES; |
| 1265 | return ip_ra_control(sk, 0, NULL); |
| 1266 | case MRT_ADD_VIF: |
| 1267 | case MRT_DEL_VIF: |
Jianjun Kong | c354e12 | 2008-11-03 00:28:02 -0800 | [diff] [blame] | 1268 | if (optlen != sizeof(vif)) |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1269 | return -EINVAL; |
Jianjun Kong | c354e12 | 2008-11-03 00:28:02 -0800 | [diff] [blame] | 1270 | if (copy_from_user(&vif, optval, sizeof(vif))) |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1271 | return -EFAULT; |
| 1272 | if (vif.vifc_vifi >= MAXVIFS) |
| 1273 | return -ENFILE; |
| 1274 | rtnl_lock(); |
Jianjun Kong | c354e12 | 2008-11-03 00:28:02 -0800 | [diff] [blame] | 1275 | if (optname == MRT_ADD_VIF) { |
Eric Dumazet | 4c968709 | 2010-10-01 16:15:01 +0000 | [diff] [blame] | 1276 | ret = vif_add(net, mrt, &vif, |
| 1277 | sk == rtnl_dereference(mrt->mroute_sk)); |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1278 | } else { |
Patrick McHardy | 0c12295 | 2010-04-13 05:03:22 +0000 | [diff] [blame] | 1279 | ret = vif_delete(mrt, vif.vifc_vifi, 0, NULL); |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1280 | } |
| 1281 | rtnl_unlock(); |
| 1282 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1283 | |
| 1284 | /* |
| 1285 | * Manipulate the forwarding caches. These live |
| 1286 | * in a sort of kernel/user symbiosis. |
| 1287 | */ |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1288 | case MRT_ADD_MFC: |
| 1289 | case MRT_DEL_MFC: |
Jianjun Kong | c354e12 | 2008-11-03 00:28:02 -0800 | [diff] [blame] | 1290 | if (optlen != sizeof(mfc)) |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1291 | return -EINVAL; |
Jianjun Kong | c354e12 | 2008-11-03 00:28:02 -0800 | [diff] [blame] | 1292 | if (copy_from_user(&mfc, optval, sizeof(mfc))) |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1293 | return -EFAULT; |
| 1294 | rtnl_lock(); |
Jianjun Kong | c354e12 | 2008-11-03 00:28:02 -0800 | [diff] [blame] | 1295 | if (optname == MRT_DEL_MFC) |
Patrick McHardy | 0c12295 | 2010-04-13 05:03:22 +0000 | [diff] [blame] | 1296 | ret = ipmr_mfc_delete(mrt, &mfc); |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1297 | else |
Eric Dumazet | 4c968709 | 2010-10-01 16:15:01 +0000 | [diff] [blame] | 1298 | ret = ipmr_mfc_add(net, mrt, &mfc, |
| 1299 | sk == rtnl_dereference(mrt->mroute_sk)); |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1300 | rtnl_unlock(); |
| 1301 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1302 | /* |
| 1303 | * Control PIM assert. |
| 1304 | */ |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1305 | case MRT_ASSERT: |
| 1306 | { |
| 1307 | int v; |
Eric Dumazet | 5e1859f | 2012-11-25 06:41:45 +0000 | [diff] [blame] | 1308 | if (optlen != sizeof(v)) |
| 1309 | return -EINVAL; |
Eric Dumazet | a8cb16d | 2010-10-01 16:15:29 +0000 | [diff] [blame] | 1310 | if (get_user(v, (int __user *)optval)) |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1311 | return -EFAULT; |
Joe Perches | 53d6841 | 2012-11-25 09:35:30 +0000 | [diff] [blame] | 1312 | mrt->mroute_do_assert = v; |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1313 | return 0; |
| 1314 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1315 | #ifdef CONFIG_IP_PIMSM |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1316 | case MRT_PIM: |
| 1317 | { |
Stephen Hemminger | ba93ef7 | 2008-01-21 17:28:59 -0800 | [diff] [blame] | 1318 | int v; |
| 1319 | |
Eric Dumazet | 5e1859f | 2012-11-25 06:41:45 +0000 | [diff] [blame] | 1320 | if (optlen != sizeof(v)) |
| 1321 | return -EINVAL; |
Eric Dumazet | a8cb16d | 2010-10-01 16:15:29 +0000 | [diff] [blame] | 1322 | if (get_user(v, (int __user *)optval)) |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1323 | return -EFAULT; |
Eric Dumazet | 5e1859f | 2012-11-25 06:41:45 +0000 | [diff] [blame] | 1324 | v = !!v; |
Stephen Hemminger | ba93ef7 | 2008-01-21 17:28:59 -0800 | [diff] [blame] | 1325 | |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1326 | rtnl_lock(); |
| 1327 | ret = 0; |
Patrick McHardy | 0c12295 | 2010-04-13 05:03:22 +0000 | [diff] [blame] | 1328 | if (v != mrt->mroute_do_pim) { |
| 1329 | mrt->mroute_do_pim = v; |
| 1330 | mrt->mroute_do_assert = v; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1331 | } |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1332 | rtnl_unlock(); |
| 1333 | return ret; |
| 1334 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1335 | #endif |
Patrick McHardy | f0ad086 | 2010-04-13 05:03:23 +0000 | [diff] [blame] | 1336 | #ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES |
| 1337 | case MRT_TABLE: |
| 1338 | { |
| 1339 | u32 v; |
| 1340 | |
| 1341 | if (optlen != sizeof(u32)) |
| 1342 | return -EINVAL; |
| 1343 | if (get_user(v, (u32 __user *)optval)) |
| 1344 | return -EFAULT; |
Patrick McHardy | f0ad086 | 2010-04-13 05:03:23 +0000 | [diff] [blame] | 1345 | |
Eric Dumazet | b49d3c1 | 2012-11-25 09:44:29 +0000 | [diff] [blame] | 1346 | /* "pimreg%u" should not exceed 16 bytes (IFNAMSIZ) */ |
| 1347 | if (v != RT_TABLE_DEFAULT && v >= 1000000000) |
| 1348 | return -EINVAL; |
| 1349 | |
Patrick McHardy | f0ad086 | 2010-04-13 05:03:23 +0000 | [diff] [blame] | 1350 | rtnl_lock(); |
| 1351 | ret = 0; |
Eric Dumazet | 4c968709 | 2010-10-01 16:15:01 +0000 | [diff] [blame] | 1352 | if (sk == rtnl_dereference(mrt->mroute_sk)) { |
| 1353 | ret = -EBUSY; |
| 1354 | } else { |
| 1355 | if (!ipmr_new_table(net, v)) |
| 1356 | ret = -ENOMEM; |
Eric Dumazet | 5e1859f | 2012-11-25 06:41:45 +0000 | [diff] [blame] | 1357 | else |
| 1358 | raw_sk(sk)->ipmr_table = v; |
Eric Dumazet | 4c968709 | 2010-10-01 16:15:01 +0000 | [diff] [blame] | 1359 | } |
Patrick McHardy | f0ad086 | 2010-04-13 05:03:23 +0000 | [diff] [blame] | 1360 | rtnl_unlock(); |
| 1361 | return ret; |
| 1362 | } |
| 1363 | #endif |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1364 | /* |
| 1365 | * Spurious command, or MRT_VERSION which you cannot |
| 1366 | * set. |
| 1367 | */ |
| 1368 | default: |
| 1369 | return -ENOPROTOOPT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1370 | } |
| 1371 | } |
| 1372 | |
| 1373 | /* |
| 1374 | * Getsock opt support for the multicast routing system. |
| 1375 | */ |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 1376 | |
Jianjun Kong | c354e12 | 2008-11-03 00:28:02 -0800 | [diff] [blame] | 1377 | int ip_mroute_getsockopt(struct sock *sk, int optname, char __user *optval, int __user *optlen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1378 | { |
| 1379 | int olr; |
| 1380 | int val; |
Benjamin Thery | 4feb88e | 2009-01-22 04:56:23 +0000 | [diff] [blame] | 1381 | struct net *net = sock_net(sk); |
Patrick McHardy | f0ad086 | 2010-04-13 05:03:23 +0000 | [diff] [blame] | 1382 | struct mr_table *mrt; |
| 1383 | |
Eric Dumazet | 5e1859f | 2012-11-25 06:41:45 +0000 | [diff] [blame] | 1384 | if (sk->sk_type != SOCK_RAW || |
| 1385 | inet_sk(sk)->inet_num != IPPROTO_IGMP) |
| 1386 | return -EOPNOTSUPP; |
| 1387 | |
Patrick McHardy | f0ad086 | 2010-04-13 05:03:23 +0000 | [diff] [blame] | 1388 | mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT); |
| 1389 | if (mrt == NULL) |
| 1390 | return -ENOENT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1391 | |
Jianjun Kong | c354e12 | 2008-11-03 00:28:02 -0800 | [diff] [blame] | 1392 | if (optname != MRT_VERSION && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1393 | #ifdef CONFIG_IP_PIMSM |
Eric Dumazet | a8cb16d | 2010-10-01 16:15:29 +0000 | [diff] [blame] | 1394 | optname != MRT_PIM && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1395 | #endif |
Eric Dumazet | a8cb16d | 2010-10-01 16:15:29 +0000 | [diff] [blame] | 1396 | optname != MRT_ASSERT) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1397 | return -ENOPROTOOPT; |
| 1398 | |
| 1399 | if (get_user(olr, optlen)) |
| 1400 | return -EFAULT; |
| 1401 | |
| 1402 | olr = min_t(unsigned int, olr, sizeof(int)); |
| 1403 | if (olr < 0) |
| 1404 | return -EINVAL; |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 1405 | |
Jianjun Kong | c354e12 | 2008-11-03 00:28:02 -0800 | [diff] [blame] | 1406 | if (put_user(olr, optlen)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1407 | return -EFAULT; |
Jianjun Kong | c354e12 | 2008-11-03 00:28:02 -0800 | [diff] [blame] | 1408 | if (optname == MRT_VERSION) |
| 1409 | val = 0x0305; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1410 | #ifdef CONFIG_IP_PIMSM |
Jianjun Kong | c354e12 | 2008-11-03 00:28:02 -0800 | [diff] [blame] | 1411 | else if (optname == MRT_PIM) |
Patrick McHardy | 0c12295 | 2010-04-13 05:03:22 +0000 | [diff] [blame] | 1412 | val = mrt->mroute_do_pim; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1413 | #endif |
| 1414 | else |
Patrick McHardy | 0c12295 | 2010-04-13 05:03:22 +0000 | [diff] [blame] | 1415 | val = mrt->mroute_do_assert; |
Jianjun Kong | c354e12 | 2008-11-03 00:28:02 -0800 | [diff] [blame] | 1416 | if (copy_to_user(optval, &val, olr)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1417 | return -EFAULT; |
| 1418 | return 0; |
| 1419 | } |
| 1420 | |
| 1421 | /* |
| 1422 | * The IP multicast ioctl support routines. |
| 1423 | */ |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 1424 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1425 | int ipmr_ioctl(struct sock *sk, int cmd, void __user *arg) |
| 1426 | { |
| 1427 | struct sioc_sg_req sr; |
| 1428 | struct sioc_vif_req vr; |
| 1429 | struct vif_device *vif; |
| 1430 | struct mfc_cache *c; |
Benjamin Thery | 4feb88e | 2009-01-22 04:56:23 +0000 | [diff] [blame] | 1431 | struct net *net = sock_net(sk); |
Patrick McHardy | f0ad086 | 2010-04-13 05:03:23 +0000 | [diff] [blame] | 1432 | struct mr_table *mrt; |
| 1433 | |
| 1434 | mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT); |
| 1435 | if (mrt == NULL) |
| 1436 | return -ENOENT; |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 1437 | |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1438 | switch (cmd) { |
| 1439 | case SIOCGETVIFCNT: |
Jianjun Kong | c354e12 | 2008-11-03 00:28:02 -0800 | [diff] [blame] | 1440 | if (copy_from_user(&vr, arg, sizeof(vr))) |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1441 | return -EFAULT; |
Patrick McHardy | 0c12295 | 2010-04-13 05:03:22 +0000 | [diff] [blame] | 1442 | if (vr.vifi >= mrt->maxvif) |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1443 | return -EINVAL; |
| 1444 | read_lock(&mrt_lock); |
Patrick McHardy | 0c12295 | 2010-04-13 05:03:22 +0000 | [diff] [blame] | 1445 | vif = &mrt->vif_table[vr.vifi]; |
| 1446 | if (VIF_EXISTS(mrt, vr.vifi)) { |
Jianjun Kong | c354e12 | 2008-11-03 00:28:02 -0800 | [diff] [blame] | 1447 | vr.icount = vif->pkt_in; |
| 1448 | vr.ocount = vif->pkt_out; |
| 1449 | vr.ibytes = vif->bytes_in; |
| 1450 | vr.obytes = vif->bytes_out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1451 | read_unlock(&mrt_lock); |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1452 | |
Jianjun Kong | c354e12 | 2008-11-03 00:28:02 -0800 | [diff] [blame] | 1453 | if (copy_to_user(arg, &vr, sizeof(vr))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1454 | return -EFAULT; |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1455 | return 0; |
| 1456 | } |
| 1457 | read_unlock(&mrt_lock); |
| 1458 | return -EADDRNOTAVAIL; |
| 1459 | case SIOCGETSGCNT: |
Jianjun Kong | c354e12 | 2008-11-03 00:28:02 -0800 | [diff] [blame] | 1460 | if (copy_from_user(&sr, arg, sizeof(sr))) |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1461 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1462 | |
Eric Dumazet | a8c9486 | 2010-10-01 16:15:08 +0000 | [diff] [blame] | 1463 | rcu_read_lock(); |
Patrick McHardy | 0c12295 | 2010-04-13 05:03:22 +0000 | [diff] [blame] | 1464 | c = ipmr_cache_find(mrt, sr.src.s_addr, sr.grp.s_addr); |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1465 | if (c) { |
| 1466 | sr.pktcnt = c->mfc_un.res.pkt; |
| 1467 | sr.bytecnt = c->mfc_un.res.bytes; |
| 1468 | sr.wrong_if = c->mfc_un.res.wrong_if; |
Eric Dumazet | a8c9486 | 2010-10-01 16:15:08 +0000 | [diff] [blame] | 1469 | rcu_read_unlock(); |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1470 | |
Jianjun Kong | c354e12 | 2008-11-03 00:28:02 -0800 | [diff] [blame] | 1471 | if (copy_to_user(arg, &sr, sizeof(sr))) |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1472 | return -EFAULT; |
| 1473 | return 0; |
| 1474 | } |
Eric Dumazet | a8c9486 | 2010-10-01 16:15:08 +0000 | [diff] [blame] | 1475 | rcu_read_unlock(); |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 1476 | return -EADDRNOTAVAIL; |
| 1477 | default: |
| 1478 | return -ENOIOCTLCMD; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1479 | } |
| 1480 | } |
| 1481 | |
Eric W. Biederman | 709b46e | 2011-01-29 16:15:56 +0000 | [diff] [blame] | 1482 | #ifdef CONFIG_COMPAT |
| 1483 | struct compat_sioc_sg_req { |
| 1484 | struct in_addr src; |
| 1485 | struct in_addr grp; |
| 1486 | compat_ulong_t pktcnt; |
| 1487 | compat_ulong_t bytecnt; |
| 1488 | compat_ulong_t wrong_if; |
| 1489 | }; |
| 1490 | |
David S. Miller | ca6b8bb0 | 2011-02-03 17:24:28 -0800 | [diff] [blame] | 1491 | struct compat_sioc_vif_req { |
| 1492 | vifi_t vifi; /* Which iface */ |
| 1493 | compat_ulong_t icount; |
| 1494 | compat_ulong_t ocount; |
| 1495 | compat_ulong_t ibytes; |
| 1496 | compat_ulong_t obytes; |
| 1497 | }; |
| 1498 | |
Eric W. Biederman | 709b46e | 2011-01-29 16:15:56 +0000 | [diff] [blame] | 1499 | int ipmr_compat_ioctl(struct sock *sk, unsigned int cmd, void __user *arg) |
| 1500 | { |
David S. Miller | 0033d5a | 2011-02-03 17:21:31 -0800 | [diff] [blame] | 1501 | struct compat_sioc_sg_req sr; |
David S. Miller | ca6b8bb0 | 2011-02-03 17:24:28 -0800 | [diff] [blame] | 1502 | struct compat_sioc_vif_req vr; |
| 1503 | struct vif_device *vif; |
Eric W. Biederman | 709b46e | 2011-01-29 16:15:56 +0000 | [diff] [blame] | 1504 | struct mfc_cache *c; |
| 1505 | struct net *net = sock_net(sk); |
| 1506 | struct mr_table *mrt; |
| 1507 | |
| 1508 | mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT); |
| 1509 | if (mrt == NULL) |
| 1510 | return -ENOENT; |
| 1511 | |
| 1512 | switch (cmd) { |
David S. Miller | ca6b8bb0 | 2011-02-03 17:24:28 -0800 | [diff] [blame] | 1513 | case SIOCGETVIFCNT: |
| 1514 | if (copy_from_user(&vr, arg, sizeof(vr))) |
| 1515 | return -EFAULT; |
| 1516 | if (vr.vifi >= mrt->maxvif) |
| 1517 | return -EINVAL; |
| 1518 | read_lock(&mrt_lock); |
| 1519 | vif = &mrt->vif_table[vr.vifi]; |
| 1520 | if (VIF_EXISTS(mrt, vr.vifi)) { |
| 1521 | vr.icount = vif->pkt_in; |
| 1522 | vr.ocount = vif->pkt_out; |
| 1523 | vr.ibytes = vif->bytes_in; |
| 1524 | vr.obytes = vif->bytes_out; |
| 1525 | read_unlock(&mrt_lock); |
| 1526 | |
| 1527 | if (copy_to_user(arg, &vr, sizeof(vr))) |
| 1528 | return -EFAULT; |
| 1529 | return 0; |
| 1530 | } |
| 1531 | read_unlock(&mrt_lock); |
| 1532 | return -EADDRNOTAVAIL; |
Eric W. Biederman | 709b46e | 2011-01-29 16:15:56 +0000 | [diff] [blame] | 1533 | case SIOCGETSGCNT: |
| 1534 | if (copy_from_user(&sr, arg, sizeof(sr))) |
| 1535 | return -EFAULT; |
| 1536 | |
| 1537 | rcu_read_lock(); |
| 1538 | c = ipmr_cache_find(mrt, sr.src.s_addr, sr.grp.s_addr); |
| 1539 | if (c) { |
| 1540 | sr.pktcnt = c->mfc_un.res.pkt; |
| 1541 | sr.bytecnt = c->mfc_un.res.bytes; |
| 1542 | sr.wrong_if = c->mfc_un.res.wrong_if; |
| 1543 | rcu_read_unlock(); |
| 1544 | |
| 1545 | if (copy_to_user(arg, &sr, sizeof(sr))) |
| 1546 | return -EFAULT; |
| 1547 | return 0; |
| 1548 | } |
| 1549 | rcu_read_unlock(); |
| 1550 | return -EADDRNOTAVAIL; |
| 1551 | default: |
| 1552 | return -ENOIOCTLCMD; |
| 1553 | } |
| 1554 | } |
| 1555 | #endif |
| 1556 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1557 | |
| 1558 | static int ipmr_device_event(struct notifier_block *this, unsigned long event, void *ptr) |
| 1559 | { |
Eric W. Biederman | e9dc865 | 2007-09-12 13:02:17 +0200 | [diff] [blame] | 1560 | struct net_device *dev = ptr; |
Benjamin Thery | 4feb88e | 2009-01-22 04:56:23 +0000 | [diff] [blame] | 1561 | struct net *net = dev_net(dev); |
Patrick McHardy | f0ad086 | 2010-04-13 05:03:23 +0000 | [diff] [blame] | 1562 | struct mr_table *mrt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1563 | struct vif_device *v; |
| 1564 | int ct; |
Eric W. Biederman | e9dc865 | 2007-09-12 13:02:17 +0200 | [diff] [blame] | 1565 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1566 | if (event != NETDEV_UNREGISTER) |
| 1567 | return NOTIFY_DONE; |
Patrick McHardy | f0ad086 | 2010-04-13 05:03:23 +0000 | [diff] [blame] | 1568 | |
| 1569 | ipmr_for_each_table(mrt, net) { |
| 1570 | v = &mrt->vif_table[0]; |
| 1571 | for (ct = 0; ct < mrt->maxvif; ct++, v++) { |
| 1572 | if (v->dev == dev) |
RongQing.Li | e92036a | 2011-11-23 23:10:52 +0000 | [diff] [blame] | 1573 | vif_delete(mrt, ct, 1, NULL); |
Patrick McHardy | f0ad086 | 2010-04-13 05:03:23 +0000 | [diff] [blame] | 1574 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1575 | } |
| 1576 | return NOTIFY_DONE; |
| 1577 | } |
| 1578 | |
| 1579 | |
Jianjun Kong | c354e12 | 2008-11-03 00:28:02 -0800 | [diff] [blame] | 1580 | static struct notifier_block ip_mr_notifier = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1581 | .notifier_call = ipmr_device_event, |
| 1582 | }; |
| 1583 | |
| 1584 | /* |
Eric Dumazet | a8cb16d | 2010-10-01 16:15:29 +0000 | [diff] [blame] | 1585 | * Encapsulate a packet by attaching a valid IPIP header to it. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1586 | * This avoids tunnel drivers and other mess and gives us the speed so |
| 1587 | * important for multicast video. |
| 1588 | */ |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 1589 | |
Al Viro | 114c784 | 2006-09-27 18:39:29 -0700 | [diff] [blame] | 1590 | static void ip_encap(struct sk_buff *skb, __be32 saddr, __be32 daddr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1591 | { |
Arnaldo Carvalho de Melo | 8856dfa | 2007-03-10 19:40:39 -0300 | [diff] [blame] | 1592 | struct iphdr *iph; |
Eric Dumazet | b71d1d4 | 2011-04-22 04:53:02 +0000 | [diff] [blame] | 1593 | const struct iphdr *old_iph = ip_hdr(skb); |
Arnaldo Carvalho de Melo | 8856dfa | 2007-03-10 19:40:39 -0300 | [diff] [blame] | 1594 | |
| 1595 | skb_push(skb, sizeof(struct iphdr)); |
Arnaldo Carvalho de Melo | b0e380b | 2007-04-10 21:21:55 -0700 | [diff] [blame] | 1596 | skb->transport_header = skb->network_header; |
Arnaldo Carvalho de Melo | 8856dfa | 2007-03-10 19:40:39 -0300 | [diff] [blame] | 1597 | skb_reset_network_header(skb); |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 1598 | iph = ip_hdr(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1599 | |
Eric Dumazet | a8cb16d | 2010-10-01 16:15:29 +0000 | [diff] [blame] | 1600 | iph->version = 4; |
Arnaldo Carvalho de Melo | e023dd6 | 2007-03-12 20:09:36 -0300 | [diff] [blame] | 1601 | iph->tos = old_iph->tos; |
| 1602 | iph->ttl = old_iph->ttl; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1603 | iph->frag_off = 0; |
| 1604 | iph->daddr = daddr; |
| 1605 | iph->saddr = saddr; |
| 1606 | iph->protocol = IPPROTO_IPIP; |
| 1607 | iph->ihl = 5; |
| 1608 | iph->tot_len = htons(skb->len); |
Eric Dumazet | adf3090 | 2009-06-02 05:19:30 +0000 | [diff] [blame] | 1609 | ip_select_ident(iph, skb_dst(skb), NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1610 | ip_send_check(iph); |
| 1611 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1612 | memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt)); |
| 1613 | nf_reset(skb); |
| 1614 | } |
| 1615 | |
| 1616 | static inline int ipmr_forward_finish(struct sk_buff *skb) |
| 1617 | { |
Eric Dumazet | a8cb16d | 2010-10-01 16:15:29 +0000 | [diff] [blame] | 1618 | struct ip_options *opt = &(IPCB(skb)->opt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1619 | |
Eric Dumazet | adf3090 | 2009-06-02 05:19:30 +0000 | [diff] [blame] | 1620 | IP_INC_STATS_BH(dev_net(skb_dst(skb)->dev), IPSTATS_MIB_OUTFORWDATAGRAMS); |
Vincent Bernat | 2d8dbb0 | 2012-06-05 03:41:42 +0000 | [diff] [blame] | 1621 | IP_ADD_STATS_BH(dev_net(skb_dst(skb)->dev), IPSTATS_MIB_OUTOCTETS, skb->len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1622 | |
| 1623 | if (unlikely(opt->optlen)) |
| 1624 | ip_forward_options(skb); |
| 1625 | |
| 1626 | return dst_output(skb); |
| 1627 | } |
| 1628 | |
| 1629 | /* |
| 1630 | * Processing handlers for ipmr_forward |
| 1631 | */ |
| 1632 | |
Patrick McHardy | 0c12295 | 2010-04-13 05:03:22 +0000 | [diff] [blame] | 1633 | static void ipmr_queue_xmit(struct net *net, struct mr_table *mrt, |
| 1634 | struct sk_buff *skb, struct mfc_cache *c, int vifi) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1635 | { |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 1636 | const struct iphdr *iph = ip_hdr(skb); |
Patrick McHardy | 0c12295 | 2010-04-13 05:03:22 +0000 | [diff] [blame] | 1637 | struct vif_device *vif = &mrt->vif_table[vifi]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1638 | struct net_device *dev; |
| 1639 | struct rtable *rt; |
David S. Miller | 31e4543d | 2011-05-03 20:25:42 -0700 | [diff] [blame] | 1640 | struct flowi4 fl4; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1641 | int encap = 0; |
| 1642 | |
| 1643 | if (vif->dev == NULL) |
| 1644 | goto out_free; |
| 1645 | |
| 1646 | #ifdef CONFIG_IP_PIMSM |
| 1647 | if (vif->flags & VIFF_REGISTER) { |
| 1648 | vif->pkt_out++; |
Jianjun Kong | c354e12 | 2008-11-03 00:28:02 -0800 | [diff] [blame] | 1649 | vif->bytes_out += skb->len; |
Pavel Emelyanov | cf3677a | 2008-05-21 14:17:33 -0700 | [diff] [blame] | 1650 | vif->dev->stats.tx_bytes += skb->len; |
| 1651 | vif->dev->stats.tx_packets++; |
Patrick McHardy | 0c12295 | 2010-04-13 05:03:22 +0000 | [diff] [blame] | 1652 | ipmr_cache_report(mrt, skb, vifi, IGMPMSG_WHOLEPKT); |
Ilpo Järvinen | 69ebbf5 | 2009-02-06 23:46:51 -0800 | [diff] [blame] | 1653 | goto out_free; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1654 | } |
| 1655 | #endif |
| 1656 | |
Eric Dumazet | a8cb16d | 2010-10-01 16:15:29 +0000 | [diff] [blame] | 1657 | if (vif->flags & VIFF_TUNNEL) { |
David S. Miller | 31e4543d | 2011-05-03 20:25:42 -0700 | [diff] [blame] | 1658 | rt = ip_route_output_ports(net, &fl4, NULL, |
David S. Miller | 78fbfd8 | 2011-03-12 00:00:52 -0500 | [diff] [blame] | 1659 | vif->remote, vif->local, |
| 1660 | 0, 0, |
| 1661 | IPPROTO_IPIP, |
| 1662 | RT_TOS(iph->tos), vif->link); |
David S. Miller | b23dd4f | 2011-03-02 14:31:35 -0800 | [diff] [blame] | 1663 | if (IS_ERR(rt)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1664 | goto out_free; |
| 1665 | encap = sizeof(struct iphdr); |
| 1666 | } else { |
David S. Miller | 31e4543d | 2011-05-03 20:25:42 -0700 | [diff] [blame] | 1667 | rt = ip_route_output_ports(net, &fl4, NULL, iph->daddr, 0, |
David S. Miller | 78fbfd8 | 2011-03-12 00:00:52 -0500 | [diff] [blame] | 1668 | 0, 0, |
| 1669 | IPPROTO_IPIP, |
| 1670 | RT_TOS(iph->tos), vif->link); |
David S. Miller | b23dd4f | 2011-03-02 14:31:35 -0800 | [diff] [blame] | 1671 | if (IS_ERR(rt)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1672 | goto out_free; |
| 1673 | } |
| 1674 | |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 1675 | dev = rt->dst.dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1676 | |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 1677 | if (skb->len+encap > dst_mtu(&rt->dst) && (ntohs(iph->frag_off) & IP_DF)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1678 | /* Do not fragment multicasts. Alas, IPv4 does not |
Eric Dumazet | a8cb16d | 2010-10-01 16:15:29 +0000 | [diff] [blame] | 1679 | * allow to send ICMP, so that packets will disappear |
| 1680 | * to blackhole. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1681 | */ |
| 1682 | |
Pavel Emelyanov | 7c73a6f | 2008-07-16 20:20:11 -0700 | [diff] [blame] | 1683 | IP_INC_STATS_BH(dev_net(dev), IPSTATS_MIB_FRAGFAILS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1684 | ip_rt_put(rt); |
| 1685 | goto out_free; |
| 1686 | } |
| 1687 | |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 1688 | encap += LL_RESERVED_SPACE(dev) + rt->dst.header_len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1689 | |
| 1690 | if (skb_cow(skb, encap)) { |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 1691 | ip_rt_put(rt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1692 | goto out_free; |
| 1693 | } |
| 1694 | |
| 1695 | vif->pkt_out++; |
Jianjun Kong | c354e12 | 2008-11-03 00:28:02 -0800 | [diff] [blame] | 1696 | vif->bytes_out += skb->len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1697 | |
Eric Dumazet | adf3090 | 2009-06-02 05:19:30 +0000 | [diff] [blame] | 1698 | skb_dst_drop(skb); |
Changli Gao | d8d1f30 | 2010-06-10 23:31:35 -0700 | [diff] [blame] | 1699 | skb_dst_set(skb, &rt->dst); |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 1700 | ip_decrease_ttl(ip_hdr(skb)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1701 | |
| 1702 | /* FIXME: forward and output firewalls used to be called here. |
Eric Dumazet | a8cb16d | 2010-10-01 16:15:29 +0000 | [diff] [blame] | 1703 | * What do we do with netfilter? -- RR |
| 1704 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1705 | if (vif->flags & VIFF_TUNNEL) { |
| 1706 | ip_encap(skb, vif->local, vif->remote); |
| 1707 | /* FIXME: extra output firewall step used to be here. --RR */ |
Pavel Emelyanov | 2f4c02d | 2008-05-21 14:16:14 -0700 | [diff] [blame] | 1708 | vif->dev->stats.tx_packets++; |
| 1709 | vif->dev->stats.tx_bytes += skb->len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1710 | } |
| 1711 | |
| 1712 | IPCB(skb)->flags |= IPSKB_FORWARDED; |
| 1713 | |
| 1714 | /* |
| 1715 | * RFC1584 teaches, that DVMRP/PIM router must deliver packets locally |
| 1716 | * not only before forwarding, but after forwarding on all output |
| 1717 | * interfaces. It is clear, if mrouter runs a multicasting |
| 1718 | * program, it should receive packets not depending to what interface |
| 1719 | * program is joined. |
| 1720 | * If we will not make it, the program will have to join on all |
| 1721 | * interfaces. On the other hand, multihoming host (or router, but |
| 1722 | * not mrouter) cannot join to more than one interface - it will |
| 1723 | * result in receiving multiple packets. |
| 1724 | */ |
Jan Engelhardt | 9bbc768 | 2010-03-23 04:07:29 +0100 | [diff] [blame] | 1725 | NF_HOOK(NFPROTO_IPV4, NF_INET_FORWARD, skb, skb->dev, dev, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1726 | ipmr_forward_finish); |
| 1727 | return; |
| 1728 | |
| 1729 | out_free: |
| 1730 | kfree_skb(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1731 | } |
| 1732 | |
Patrick McHardy | 0c12295 | 2010-04-13 05:03:22 +0000 | [diff] [blame] | 1733 | static int ipmr_find_vif(struct mr_table *mrt, struct net_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1734 | { |
| 1735 | int ct; |
Patrick McHardy | 0c12295 | 2010-04-13 05:03:22 +0000 | [diff] [blame] | 1736 | |
| 1737 | for (ct = mrt->maxvif-1; ct >= 0; ct--) { |
| 1738 | if (mrt->vif_table[ct].dev == dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1739 | break; |
| 1740 | } |
| 1741 | return ct; |
| 1742 | } |
| 1743 | |
| 1744 | /* "local" means that we should preserve one skb (for local delivery) */ |
| 1745 | |
Patrick McHardy | 0c12295 | 2010-04-13 05:03:22 +0000 | [diff] [blame] | 1746 | static int ip_mr_forward(struct net *net, struct mr_table *mrt, |
| 1747 | struct sk_buff *skb, struct mfc_cache *cache, |
| 1748 | int local) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1749 | { |
| 1750 | int psend = -1; |
| 1751 | int vif, ct; |
| 1752 | |
| 1753 | vif = cache->mfc_parent; |
| 1754 | cache->mfc_un.res.pkt++; |
| 1755 | cache->mfc_un.res.bytes += skb->len; |
| 1756 | |
| 1757 | /* |
| 1758 | * Wrong interface: drop packet and (maybe) send PIM assert. |
| 1759 | */ |
Patrick McHardy | 0c12295 | 2010-04-13 05:03:22 +0000 | [diff] [blame] | 1760 | if (mrt->vif_table[vif].dev != skb->dev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1761 | int true_vifi; |
| 1762 | |
David S. Miller | c753796 | 2010-11-11 17:07:48 -0800 | [diff] [blame] | 1763 | if (rt_is_output_route(skb_rtable(skb))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1764 | /* It is our own packet, looped back. |
Eric Dumazet | a8cb16d | 2010-10-01 16:15:29 +0000 | [diff] [blame] | 1765 | * Very complicated situation... |
| 1766 | * |
| 1767 | * The best workaround until routing daemons will be |
| 1768 | * fixed is not to redistribute packet, if it was |
| 1769 | * send through wrong interface. It means, that |
| 1770 | * multicast applications WILL NOT work for |
| 1771 | * (S,G), which have default multicast route pointing |
| 1772 | * to wrong oif. In any case, it is not a good |
| 1773 | * idea to use multicasting applications on router. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1774 | */ |
| 1775 | goto dont_forward; |
| 1776 | } |
| 1777 | |
| 1778 | cache->mfc_un.res.wrong_if++; |
Patrick McHardy | 0c12295 | 2010-04-13 05:03:22 +0000 | [diff] [blame] | 1779 | true_vifi = ipmr_find_vif(mrt, skb->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1780 | |
Patrick McHardy | 0c12295 | 2010-04-13 05:03:22 +0000 | [diff] [blame] | 1781 | if (true_vifi >= 0 && mrt->mroute_do_assert && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1782 | /* pimsm uses asserts, when switching from RPT to SPT, |
Eric Dumazet | a8cb16d | 2010-10-01 16:15:29 +0000 | [diff] [blame] | 1783 | * so that we cannot check that packet arrived on an oif. |
| 1784 | * It is bad, but otherwise we would need to move pretty |
| 1785 | * large chunk of pimd to kernel. Ough... --ANK |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1786 | */ |
Patrick McHardy | 0c12295 | 2010-04-13 05:03:22 +0000 | [diff] [blame] | 1787 | (mrt->mroute_do_pim || |
Benjamin Thery | 6f9374a | 2009-01-22 04:56:20 +0000 | [diff] [blame] | 1788 | cache->mfc_un.res.ttls[true_vifi] < 255) && |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 1789 | time_after(jiffies, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1790 | cache->mfc_un.res.last_assert + MFC_ASSERT_THRESH)) { |
| 1791 | cache->mfc_un.res.last_assert = jiffies; |
Patrick McHardy | 0c12295 | 2010-04-13 05:03:22 +0000 | [diff] [blame] | 1792 | ipmr_cache_report(mrt, skb, true_vifi, IGMPMSG_WRONGVIF); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1793 | } |
| 1794 | goto dont_forward; |
| 1795 | } |
| 1796 | |
Patrick McHardy | 0c12295 | 2010-04-13 05:03:22 +0000 | [diff] [blame] | 1797 | mrt->vif_table[vif].pkt_in++; |
| 1798 | mrt->vif_table[vif].bytes_in += skb->len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1799 | |
| 1800 | /* |
| 1801 | * Forward the frame |
| 1802 | */ |
Eric Dumazet | a8cb16d | 2010-10-01 16:15:29 +0000 | [diff] [blame] | 1803 | for (ct = cache->mfc_un.res.maxvif - 1; |
| 1804 | ct >= cache->mfc_un.res.minvif; ct--) { |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 1805 | if (ip_hdr(skb)->ttl > cache->mfc_un.res.ttls[ct]) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1806 | if (psend != -1) { |
| 1807 | struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC); |
Eric Dumazet | a8cb16d | 2010-10-01 16:15:29 +0000 | [diff] [blame] | 1808 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1809 | if (skb2) |
Patrick McHardy | 0c12295 | 2010-04-13 05:03:22 +0000 | [diff] [blame] | 1810 | ipmr_queue_xmit(net, mrt, skb2, cache, |
| 1811 | psend); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1812 | } |
Jianjun Kong | c354e12 | 2008-11-03 00:28:02 -0800 | [diff] [blame] | 1813 | psend = ct; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1814 | } |
| 1815 | } |
| 1816 | if (psend != -1) { |
| 1817 | if (local) { |
| 1818 | struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC); |
Eric Dumazet | a8cb16d | 2010-10-01 16:15:29 +0000 | [diff] [blame] | 1819 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1820 | if (skb2) |
Patrick McHardy | 0c12295 | 2010-04-13 05:03:22 +0000 | [diff] [blame] | 1821 | ipmr_queue_xmit(net, mrt, skb2, cache, psend); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1822 | } else { |
Patrick McHardy | 0c12295 | 2010-04-13 05:03:22 +0000 | [diff] [blame] | 1823 | ipmr_queue_xmit(net, mrt, skb, cache, psend); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1824 | return 0; |
| 1825 | } |
| 1826 | } |
| 1827 | |
| 1828 | dont_forward: |
| 1829 | if (!local) |
| 1830 | kfree_skb(skb); |
| 1831 | return 0; |
| 1832 | } |
| 1833 | |
David S. Miller | 417da66 | 2011-05-03 19:42:43 -0700 | [diff] [blame] | 1834 | static struct mr_table *ipmr_rt_fib_lookup(struct net *net, struct sk_buff *skb) |
David S. Miller | ee3f1aa | 2011-03-09 14:06:20 -0800 | [diff] [blame] | 1835 | { |
David S. Miller | 417da66 | 2011-05-03 19:42:43 -0700 | [diff] [blame] | 1836 | struct rtable *rt = skb_rtable(skb); |
| 1837 | struct iphdr *iph = ip_hdr(skb); |
David S. Miller | da91981 | 2011-03-12 02:04:50 -0500 | [diff] [blame] | 1838 | struct flowi4 fl4 = { |
David S. Miller | 417da66 | 2011-05-03 19:42:43 -0700 | [diff] [blame] | 1839 | .daddr = iph->daddr, |
| 1840 | .saddr = iph->saddr, |
Julian Anastasov | b0fe4a3 | 2011-07-23 02:00:41 +0000 | [diff] [blame] | 1841 | .flowi4_tos = RT_TOS(iph->tos), |
David S. Miller | 4fd551d | 2012-07-17 14:39:44 -0700 | [diff] [blame] | 1842 | .flowi4_oif = (rt_is_output_route(rt) ? |
| 1843 | skb->dev->ifindex : 0), |
| 1844 | .flowi4_iif = (rt_is_output_route(rt) ? |
Pavel Emelyanov | 1fb9489 | 2012-08-08 21:53:36 +0000 | [diff] [blame] | 1845 | LOOPBACK_IFINDEX : |
David S. Miller | 4fd551d | 2012-07-17 14:39:44 -0700 | [diff] [blame] | 1846 | skb->dev->ifindex), |
David Miller | b486988 | 2012-07-01 02:03:01 +0000 | [diff] [blame] | 1847 | .flowi4_mark = skb->mark, |
David S. Miller | ee3f1aa | 2011-03-09 14:06:20 -0800 | [diff] [blame] | 1848 | }; |
| 1849 | struct mr_table *mrt; |
| 1850 | int err; |
| 1851 | |
David S. Miller | da91981 | 2011-03-12 02:04:50 -0500 | [diff] [blame] | 1852 | err = ipmr_fib_lookup(net, &fl4, &mrt); |
David S. Miller | ee3f1aa | 2011-03-09 14:06:20 -0800 | [diff] [blame] | 1853 | if (err) |
| 1854 | return ERR_PTR(err); |
| 1855 | return mrt; |
| 1856 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1857 | |
| 1858 | /* |
| 1859 | * Multicast packets for forwarding arrive here |
Eric Dumazet | 4c968709 | 2010-10-01 16:15:01 +0000 | [diff] [blame] | 1860 | * Called with rcu_read_lock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1861 | */ |
| 1862 | |
| 1863 | int ip_mr_input(struct sk_buff *skb) |
| 1864 | { |
| 1865 | struct mfc_cache *cache; |
Benjamin Thery | 4feb88e | 2009-01-22 04:56:23 +0000 | [diff] [blame] | 1866 | struct net *net = dev_net(skb->dev); |
Eric Dumazet | 511c3f9 | 2009-06-02 05:14:27 +0000 | [diff] [blame] | 1867 | int local = skb_rtable(skb)->rt_flags & RTCF_LOCAL; |
Patrick McHardy | f0ad086 | 2010-04-13 05:03:23 +0000 | [diff] [blame] | 1868 | struct mr_table *mrt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1869 | |
| 1870 | /* Packet is looped back after forward, it should not be |
Eric Dumazet | a8cb16d | 2010-10-01 16:15:29 +0000 | [diff] [blame] | 1871 | * forwarded second time, but still can be delivered locally. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1872 | */ |
Eric Dumazet | 4c968709 | 2010-10-01 16:15:01 +0000 | [diff] [blame] | 1873 | if (IPCB(skb)->flags & IPSKB_FORWARDED) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1874 | goto dont_forward; |
| 1875 | |
David S. Miller | 417da66 | 2011-05-03 19:42:43 -0700 | [diff] [blame] | 1876 | mrt = ipmr_rt_fib_lookup(net, skb); |
David S. Miller | ee3f1aa | 2011-03-09 14:06:20 -0800 | [diff] [blame] | 1877 | if (IS_ERR(mrt)) { |
| 1878 | kfree_skb(skb); |
| 1879 | return PTR_ERR(mrt); |
Ben Greear | e40dbc5 | 2010-07-15 13:22:33 +0000 | [diff] [blame] | 1880 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1881 | if (!local) { |
Eric Dumazet | 4c968709 | 2010-10-01 16:15:01 +0000 | [diff] [blame] | 1882 | if (IPCB(skb)->opt.router_alert) { |
| 1883 | if (ip_call_ra_chain(skb)) |
| 1884 | return 0; |
| 1885 | } else if (ip_hdr(skb)->protocol == IPPROTO_IGMP) { |
| 1886 | /* IGMPv1 (and broken IGMPv2 implementations sort of |
| 1887 | * Cisco IOS <= 11.2(8)) do not put router alert |
| 1888 | * option to IGMP packets destined to routable |
| 1889 | * groups. It is very bad, because it means |
| 1890 | * that we can forward NO IGMP messages. |
| 1891 | */ |
| 1892 | struct sock *mroute_sk; |
| 1893 | |
| 1894 | mroute_sk = rcu_dereference(mrt->mroute_sk); |
| 1895 | if (mroute_sk) { |
| 1896 | nf_reset(skb); |
| 1897 | raw_rcv(mroute_sk, skb); |
| 1898 | return 0; |
| 1899 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1900 | } |
| 1901 | } |
| 1902 | |
Eric Dumazet | a8c9486 | 2010-10-01 16:15:08 +0000 | [diff] [blame] | 1903 | /* already under rcu_read_lock() */ |
Patrick McHardy | 0c12295 | 2010-04-13 05:03:22 +0000 | [diff] [blame] | 1904 | cache = ipmr_cache_find(mrt, ip_hdr(skb)->saddr, ip_hdr(skb)->daddr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1905 | |
| 1906 | /* |
| 1907 | * No usable cache entry |
| 1908 | */ |
Jianjun Kong | c354e12 | 2008-11-03 00:28:02 -0800 | [diff] [blame] | 1909 | if (cache == NULL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1910 | int vif; |
| 1911 | |
| 1912 | if (local) { |
| 1913 | struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC); |
| 1914 | ip_local_deliver(skb); |
Eric Dumazet | a8c9486 | 2010-10-01 16:15:08 +0000 | [diff] [blame] | 1915 | if (skb2 == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1916 | return -ENOBUFS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1917 | skb = skb2; |
| 1918 | } |
| 1919 | |
Eric Dumazet | a8c9486 | 2010-10-01 16:15:08 +0000 | [diff] [blame] | 1920 | read_lock(&mrt_lock); |
Patrick McHardy | 0c12295 | 2010-04-13 05:03:22 +0000 | [diff] [blame] | 1921 | vif = ipmr_find_vif(mrt, skb->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1922 | if (vif >= 0) { |
Eric Dumazet | 0eae88f | 2010-04-20 19:06:52 -0700 | [diff] [blame] | 1923 | int err2 = ipmr_cache_unresolved(mrt, vif, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1924 | read_unlock(&mrt_lock); |
| 1925 | |
Eric Dumazet | 0eae88f | 2010-04-20 19:06:52 -0700 | [diff] [blame] | 1926 | return err2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1927 | } |
| 1928 | read_unlock(&mrt_lock); |
| 1929 | kfree_skb(skb); |
| 1930 | return -ENODEV; |
| 1931 | } |
| 1932 | |
Eric Dumazet | a8c9486 | 2010-10-01 16:15:08 +0000 | [diff] [blame] | 1933 | read_lock(&mrt_lock); |
Patrick McHardy | 0c12295 | 2010-04-13 05:03:22 +0000 | [diff] [blame] | 1934 | ip_mr_forward(net, mrt, skb, cache, local); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1935 | read_unlock(&mrt_lock); |
| 1936 | |
| 1937 | if (local) |
| 1938 | return ip_local_deliver(skb); |
| 1939 | |
| 1940 | return 0; |
| 1941 | |
| 1942 | dont_forward: |
| 1943 | if (local) |
| 1944 | return ip_local_deliver(skb); |
| 1945 | kfree_skb(skb); |
| 1946 | return 0; |
| 1947 | } |
| 1948 | |
Ilpo Järvinen | b187920 | 2008-12-16 01:15:11 -0800 | [diff] [blame] | 1949 | #ifdef CONFIG_IP_PIMSM |
Eric Dumazet | 55747a0 | 2010-10-01 16:14:55 +0000 | [diff] [blame] | 1950 | /* called with rcu_read_lock() */ |
Patrick McHardy | f0ad086 | 2010-04-13 05:03:23 +0000 | [diff] [blame] | 1951 | static int __pim_rcv(struct mr_table *mrt, struct sk_buff *skb, |
| 1952 | unsigned int pimlen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1953 | { |
Ilpo Järvinen | b187920 | 2008-12-16 01:15:11 -0800 | [diff] [blame] | 1954 | struct net_device *reg_dev = NULL; |
| 1955 | struct iphdr *encap; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1956 | |
Ilpo Järvinen | b187920 | 2008-12-16 01:15:11 -0800 | [diff] [blame] | 1957 | encap = (struct iphdr *)(skb_transport_header(skb) + pimlen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1958 | /* |
Eric Dumazet | a8cb16d | 2010-10-01 16:15:29 +0000 | [diff] [blame] | 1959 | * Check that: |
| 1960 | * a. packet is really sent to a multicast group |
| 1961 | * b. packet is not a NULL-REGISTER |
| 1962 | * c. packet is not truncated |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1963 | */ |
Joe Perches | f97c1e0 | 2007-12-16 13:45:43 -0800 | [diff] [blame] | 1964 | if (!ipv4_is_multicast(encap->daddr) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1965 | encap->tot_len == 0 || |
Ilpo Järvinen | b187920 | 2008-12-16 01:15:11 -0800 | [diff] [blame] | 1966 | ntohs(encap->tot_len) + pimlen > skb->len) |
| 1967 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1968 | |
| 1969 | read_lock(&mrt_lock); |
Patrick McHardy | 0c12295 | 2010-04-13 05:03:22 +0000 | [diff] [blame] | 1970 | if (mrt->mroute_reg_vif_num >= 0) |
| 1971 | reg_dev = mrt->vif_table[mrt->mroute_reg_vif_num].dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1972 | read_unlock(&mrt_lock); |
| 1973 | |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 1974 | if (reg_dev == NULL) |
Ilpo Järvinen | b187920 | 2008-12-16 01:15:11 -0800 | [diff] [blame] | 1975 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1976 | |
Arnaldo Carvalho de Melo | b0e380b | 2007-04-10 21:21:55 -0700 | [diff] [blame] | 1977 | skb->mac_header = skb->network_header; |
Eric Dumazet | 55747a0 | 2010-10-01 16:14:55 +0000 | [diff] [blame] | 1978 | skb_pull(skb, (u8 *)encap - skb->data); |
Arnaldo Carvalho de Melo | 31c7711 | 2007-03-10 19:04:55 -0300 | [diff] [blame] | 1979 | skb_reset_network_header(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1980 | skb->protocol = htons(ETH_P_IP); |
Eric Dumazet | 55747a0 | 2010-10-01 16:14:55 +0000 | [diff] [blame] | 1981 | skb->ip_summed = CHECKSUM_NONE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1982 | skb->pkt_type = PACKET_HOST; |
Eric Dumazet | d19d56d | 2010-05-17 22:36:55 -0700 | [diff] [blame] | 1983 | |
| 1984 | skb_tunnel_rx(skb, reg_dev); |
| 1985 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1986 | netif_rx(skb); |
Ilpo Järvinen | b187920 | 2008-12-16 01:15:11 -0800 | [diff] [blame] | 1987 | |
Eric Dumazet | 55747a0 | 2010-10-01 16:14:55 +0000 | [diff] [blame] | 1988 | return NET_RX_SUCCESS; |
Ilpo Järvinen | b187920 | 2008-12-16 01:15:11 -0800 | [diff] [blame] | 1989 | } |
| 1990 | #endif |
| 1991 | |
| 1992 | #ifdef CONFIG_IP_PIMSM_V1 |
| 1993 | /* |
| 1994 | * Handle IGMP messages of PIMv1 |
| 1995 | */ |
| 1996 | |
Eric Dumazet | a8cb16d | 2010-10-01 16:15:29 +0000 | [diff] [blame] | 1997 | int pim_rcv_v1(struct sk_buff *skb) |
Ilpo Järvinen | b187920 | 2008-12-16 01:15:11 -0800 | [diff] [blame] | 1998 | { |
| 1999 | struct igmphdr *pim; |
Benjamin Thery | 4feb88e | 2009-01-22 04:56:23 +0000 | [diff] [blame] | 2000 | struct net *net = dev_net(skb->dev); |
Patrick McHardy | f0ad086 | 2010-04-13 05:03:23 +0000 | [diff] [blame] | 2001 | struct mr_table *mrt; |
Ilpo Järvinen | b187920 | 2008-12-16 01:15:11 -0800 | [diff] [blame] | 2002 | |
| 2003 | if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr))) |
| 2004 | goto drop; |
| 2005 | |
| 2006 | pim = igmp_hdr(skb); |
| 2007 | |
David S. Miller | 417da66 | 2011-05-03 19:42:43 -0700 | [diff] [blame] | 2008 | mrt = ipmr_rt_fib_lookup(net, skb); |
David S. Miller | ee3f1aa | 2011-03-09 14:06:20 -0800 | [diff] [blame] | 2009 | if (IS_ERR(mrt)) |
| 2010 | goto drop; |
Patrick McHardy | 0c12295 | 2010-04-13 05:03:22 +0000 | [diff] [blame] | 2011 | if (!mrt->mroute_do_pim || |
Ilpo Järvinen | b187920 | 2008-12-16 01:15:11 -0800 | [diff] [blame] | 2012 | pim->group != PIM_V1_VERSION || pim->code != PIM_V1_REGISTER) |
| 2013 | goto drop; |
| 2014 | |
Patrick McHardy | f0ad086 | 2010-04-13 05:03:23 +0000 | [diff] [blame] | 2015 | if (__pim_rcv(mrt, skb, sizeof(*pim))) { |
Ilpo Järvinen | b187920 | 2008-12-16 01:15:11 -0800 | [diff] [blame] | 2016 | drop: |
| 2017 | kfree_skb(skb); |
| 2018 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2019 | return 0; |
| 2020 | } |
| 2021 | #endif |
| 2022 | |
| 2023 | #ifdef CONFIG_IP_PIMSM_V2 |
Eric Dumazet | a8cb16d | 2010-10-01 16:15:29 +0000 | [diff] [blame] | 2024 | static int pim_rcv(struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2025 | { |
| 2026 | struct pimreghdr *pim; |
Patrick McHardy | f0ad086 | 2010-04-13 05:03:23 +0000 | [diff] [blame] | 2027 | struct net *net = dev_net(skb->dev); |
| 2028 | struct mr_table *mrt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2029 | |
Ilpo Järvinen | b187920 | 2008-12-16 01:15:11 -0800 | [diff] [blame] | 2030 | if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2031 | goto drop; |
| 2032 | |
Arnaldo Carvalho de Melo | 9c70220 | 2007-04-25 18:04:18 -0700 | [diff] [blame] | 2033 | pim = (struct pimreghdr *)skb_transport_header(skb); |
Eric Dumazet | a8cb16d | 2010-10-01 16:15:29 +0000 | [diff] [blame] | 2034 | if (pim->type != ((PIM_VERSION << 4) | (PIM_REGISTER)) || |
| 2035 | (pim->flags & PIM_NULL_REGISTER) || |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 2036 | (ip_compute_csum((void *)pim, sizeof(*pim)) != 0 && |
Al Viro | d3bc23e | 2006-11-14 21:24:49 -0800 | [diff] [blame] | 2037 | csum_fold(skb_checksum(skb, 0, skb->len, 0)))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2038 | goto drop; |
| 2039 | |
David S. Miller | 417da66 | 2011-05-03 19:42:43 -0700 | [diff] [blame] | 2040 | mrt = ipmr_rt_fib_lookup(net, skb); |
David S. Miller | ee3f1aa | 2011-03-09 14:06:20 -0800 | [diff] [blame] | 2041 | if (IS_ERR(mrt)) |
| 2042 | goto drop; |
Patrick McHardy | f0ad086 | 2010-04-13 05:03:23 +0000 | [diff] [blame] | 2043 | if (__pim_rcv(mrt, skb, sizeof(*pim))) { |
Ilpo Järvinen | b187920 | 2008-12-16 01:15:11 -0800 | [diff] [blame] | 2044 | drop: |
| 2045 | kfree_skb(skb); |
| 2046 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2047 | return 0; |
| 2048 | } |
| 2049 | #endif |
| 2050 | |
Patrick McHardy | cb6a4e4 | 2010-04-26 16:02:08 +0200 | [diff] [blame] | 2051 | static int __ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb, |
| 2052 | struct mfc_cache *c, struct rtmsg *rtm) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2053 | { |
| 2054 | int ct; |
| 2055 | struct rtnexthop *nhp; |
Thomas Graf | 92a395e | 2012-06-26 23:36:13 +0000 | [diff] [blame] | 2056 | struct nlattr *mp_attr; |
Nicolas Dichtel | adfa85e | 2012-12-04 01:13:37 +0000 | [diff] [blame] | 2057 | struct rta_mfc_stats mfcs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2058 | |
Nicolas Dichtel | 7438189 | 2010-03-25 23:45:35 +0000 | [diff] [blame] | 2059 | /* If cache is unresolved, don't try to parse IIF and OIF */ |
Dan Carpenter | ed0f160a | 2010-05-26 00:38:56 -0700 | [diff] [blame] | 2060 | if (c->mfc_parent >= MAXVIFS) |
Nicolas Dichtel | 7438189 | 2010-03-25 23:45:35 +0000 | [diff] [blame] | 2061 | return -ENOENT; |
| 2062 | |
Thomas Graf | 92a395e | 2012-06-26 23:36:13 +0000 | [diff] [blame] | 2063 | if (VIF_EXISTS(mrt, c->mfc_parent) && |
| 2064 | nla_put_u32(skb, RTA_IIF, mrt->vif_table[c->mfc_parent].dev->ifindex) < 0) |
| 2065 | return -EMSGSIZE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2066 | |
Thomas Graf | 92a395e | 2012-06-26 23:36:13 +0000 | [diff] [blame] | 2067 | if (!(mp_attr = nla_nest_start(skb, RTA_MULTIPATH))) |
| 2068 | return -EMSGSIZE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2069 | |
| 2070 | for (ct = c->mfc_un.res.minvif; ct < c->mfc_un.res.maxvif; ct++) { |
Patrick McHardy | 0c12295 | 2010-04-13 05:03:22 +0000 | [diff] [blame] | 2071 | if (VIF_EXISTS(mrt, ct) && c->mfc_un.res.ttls[ct] < 255) { |
Thomas Graf | 92a395e | 2012-06-26 23:36:13 +0000 | [diff] [blame] | 2072 | if (!(nhp = nla_reserve_nohdr(skb, sizeof(*nhp)))) { |
| 2073 | nla_nest_cancel(skb, mp_attr); |
| 2074 | return -EMSGSIZE; |
| 2075 | } |
| 2076 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2077 | nhp->rtnh_flags = 0; |
| 2078 | nhp->rtnh_hops = c->mfc_un.res.ttls[ct]; |
Patrick McHardy | 0c12295 | 2010-04-13 05:03:22 +0000 | [diff] [blame] | 2079 | nhp->rtnh_ifindex = mrt->vif_table[ct].dev->ifindex; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2080 | nhp->rtnh_len = sizeof(*nhp); |
| 2081 | } |
| 2082 | } |
Thomas Graf | 92a395e | 2012-06-26 23:36:13 +0000 | [diff] [blame] | 2083 | |
| 2084 | nla_nest_end(skb, mp_attr); |
| 2085 | |
Nicolas Dichtel | adfa85e | 2012-12-04 01:13:37 +0000 | [diff] [blame] | 2086 | mfcs.mfcs_packets = c->mfc_un.res.pkt; |
| 2087 | mfcs.mfcs_bytes = c->mfc_un.res.bytes; |
| 2088 | mfcs.mfcs_wrong_if = c->mfc_un.res.wrong_if; |
| 2089 | if (nla_put(skb, RTA_MFC_STATS, sizeof(mfcs), &mfcs) < 0) |
| 2090 | return -EMSGSIZE; |
| 2091 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2092 | rtm->rtm_type = RTN_MULTICAST; |
| 2093 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2094 | } |
| 2095 | |
David S. Miller | 9a1b949 | 2011-05-04 12:18:54 -0700 | [diff] [blame] | 2096 | int ipmr_get_route(struct net *net, struct sk_buff *skb, |
| 2097 | __be32 saddr, __be32 daddr, |
| 2098 | struct rtmsg *rtm, int nowait) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2099 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2100 | struct mfc_cache *cache; |
David S. Miller | 9a1b949 | 2011-05-04 12:18:54 -0700 | [diff] [blame] | 2101 | struct mr_table *mrt; |
| 2102 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2103 | |
Patrick McHardy | f0ad086 | 2010-04-13 05:03:23 +0000 | [diff] [blame] | 2104 | mrt = ipmr_get_table(net, RT_TABLE_DEFAULT); |
| 2105 | if (mrt == NULL) |
| 2106 | return -ENOENT; |
| 2107 | |
Eric Dumazet | a8c9486 | 2010-10-01 16:15:08 +0000 | [diff] [blame] | 2108 | rcu_read_lock(); |
David S. Miller | 9a1b949 | 2011-05-04 12:18:54 -0700 | [diff] [blame] | 2109 | cache = ipmr_cache_find(mrt, saddr, daddr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2110 | |
Jianjun Kong | c354e12 | 2008-11-03 00:28:02 -0800 | [diff] [blame] | 2111 | if (cache == NULL) { |
Alexey Kuznetsov | 7228749 | 2006-07-25 16:45:12 -0700 | [diff] [blame] | 2112 | struct sk_buff *skb2; |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 2113 | struct iphdr *iph; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2114 | struct net_device *dev; |
Eric Dumazet | a8cb16d | 2010-10-01 16:15:29 +0000 | [diff] [blame] | 2115 | int vif = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2116 | |
| 2117 | if (nowait) { |
Eric Dumazet | a8c9486 | 2010-10-01 16:15:08 +0000 | [diff] [blame] | 2118 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2119 | return -EAGAIN; |
| 2120 | } |
| 2121 | |
| 2122 | dev = skb->dev; |
Eric Dumazet | a8c9486 | 2010-10-01 16:15:08 +0000 | [diff] [blame] | 2123 | read_lock(&mrt_lock); |
Eric Dumazet | a8cb16d | 2010-10-01 16:15:29 +0000 | [diff] [blame] | 2124 | if (dev) |
| 2125 | vif = ipmr_find_vif(mrt, dev); |
| 2126 | if (vif < 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2127 | read_unlock(&mrt_lock); |
Eric Dumazet | a8c9486 | 2010-10-01 16:15:08 +0000 | [diff] [blame] | 2128 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2129 | return -ENODEV; |
| 2130 | } |
Alexey Kuznetsov | 7228749 | 2006-07-25 16:45:12 -0700 | [diff] [blame] | 2131 | skb2 = skb_clone(skb, GFP_ATOMIC); |
| 2132 | if (!skb2) { |
| 2133 | read_unlock(&mrt_lock); |
Eric Dumazet | a8c9486 | 2010-10-01 16:15:08 +0000 | [diff] [blame] | 2134 | rcu_read_unlock(); |
Alexey Kuznetsov | 7228749 | 2006-07-25 16:45:12 -0700 | [diff] [blame] | 2135 | return -ENOMEM; |
| 2136 | } |
| 2137 | |
Arnaldo Carvalho de Melo | e2d1bca | 2007-04-10 20:46:21 -0700 | [diff] [blame] | 2138 | skb_push(skb2, sizeof(struct iphdr)); |
| 2139 | skb_reset_network_header(skb2); |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 2140 | iph = ip_hdr(skb2); |
| 2141 | iph->ihl = sizeof(struct iphdr) >> 2; |
David S. Miller | 9a1b949 | 2011-05-04 12:18:54 -0700 | [diff] [blame] | 2142 | iph->saddr = saddr; |
| 2143 | iph->daddr = daddr; |
Arnaldo Carvalho de Melo | eddc9ec | 2007-04-20 22:47:35 -0700 | [diff] [blame] | 2144 | iph->version = 0; |
Patrick McHardy | 0c12295 | 2010-04-13 05:03:22 +0000 | [diff] [blame] | 2145 | err = ipmr_cache_unresolved(mrt, vif, skb2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2146 | read_unlock(&mrt_lock); |
Eric Dumazet | a8c9486 | 2010-10-01 16:15:08 +0000 | [diff] [blame] | 2147 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2148 | return err; |
| 2149 | } |
| 2150 | |
Eric Dumazet | a8c9486 | 2010-10-01 16:15:08 +0000 | [diff] [blame] | 2151 | read_lock(&mrt_lock); |
| 2152 | if (!nowait && (rtm->rtm_flags & RTM_F_NOTIFY)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2153 | cache->mfc_flags |= MFC_NOTIFY; |
Patrick McHardy | cb6a4e4 | 2010-04-26 16:02:08 +0200 | [diff] [blame] | 2154 | err = __ipmr_fill_mroute(mrt, skb, cache, rtm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2155 | read_unlock(&mrt_lock); |
Eric Dumazet | a8c9486 | 2010-10-01 16:15:08 +0000 | [diff] [blame] | 2156 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2157 | return err; |
| 2158 | } |
| 2159 | |
Patrick McHardy | cb6a4e4 | 2010-04-26 16:02:08 +0200 | [diff] [blame] | 2160 | static int ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb, |
Nicolas Dichtel | 8cd3ac9 | 2012-12-04 01:13:40 +0000 | [diff] [blame^] | 2161 | u32 portid, u32 seq, struct mfc_cache *c, int cmd) |
Patrick McHardy | cb6a4e4 | 2010-04-26 16:02:08 +0200 | [diff] [blame] | 2162 | { |
| 2163 | struct nlmsghdr *nlh; |
| 2164 | struct rtmsg *rtm; |
Nicolas Dichtel | 1eb99af | 2012-12-04 01:13:39 +0000 | [diff] [blame] | 2165 | int err; |
Patrick McHardy | cb6a4e4 | 2010-04-26 16:02:08 +0200 | [diff] [blame] | 2166 | |
Nicolas Dichtel | 8cd3ac9 | 2012-12-04 01:13:40 +0000 | [diff] [blame^] | 2167 | nlh = nlmsg_put(skb, portid, seq, cmd, sizeof(*rtm), NLM_F_MULTI); |
Patrick McHardy | cb6a4e4 | 2010-04-26 16:02:08 +0200 | [diff] [blame] | 2168 | if (nlh == NULL) |
| 2169 | return -EMSGSIZE; |
| 2170 | |
| 2171 | rtm = nlmsg_data(nlh); |
| 2172 | rtm->rtm_family = RTNL_FAMILY_IPMR; |
| 2173 | rtm->rtm_dst_len = 32; |
| 2174 | rtm->rtm_src_len = 32; |
| 2175 | rtm->rtm_tos = 0; |
| 2176 | rtm->rtm_table = mrt->id; |
David S. Miller | f3756b7 | 2012-04-01 20:39:02 -0400 | [diff] [blame] | 2177 | if (nla_put_u32(skb, RTA_TABLE, mrt->id)) |
| 2178 | goto nla_put_failure; |
Patrick McHardy | cb6a4e4 | 2010-04-26 16:02:08 +0200 | [diff] [blame] | 2179 | rtm->rtm_type = RTN_MULTICAST; |
| 2180 | rtm->rtm_scope = RT_SCOPE_UNIVERSE; |
Nicolas Dichtel | 9a68ac7 | 2012-12-04 01:13:38 +0000 | [diff] [blame] | 2181 | if (c->mfc_flags & MFC_STATIC) |
| 2182 | rtm->rtm_protocol = RTPROT_STATIC; |
| 2183 | else |
| 2184 | rtm->rtm_protocol = RTPROT_MROUTED; |
Patrick McHardy | cb6a4e4 | 2010-04-26 16:02:08 +0200 | [diff] [blame] | 2185 | rtm->rtm_flags = 0; |
| 2186 | |
David S. Miller | f3756b7 | 2012-04-01 20:39:02 -0400 | [diff] [blame] | 2187 | if (nla_put_be32(skb, RTA_SRC, c->mfc_origin) || |
| 2188 | nla_put_be32(skb, RTA_DST, c->mfc_mcastgrp)) |
| 2189 | goto nla_put_failure; |
Nicolas Dichtel | 1eb99af | 2012-12-04 01:13:39 +0000 | [diff] [blame] | 2190 | err = __ipmr_fill_mroute(mrt, skb, c, rtm); |
| 2191 | /* do not break the dump if cache is unresolved */ |
| 2192 | if (err < 0 && err != -ENOENT) |
Patrick McHardy | cb6a4e4 | 2010-04-26 16:02:08 +0200 | [diff] [blame] | 2193 | goto nla_put_failure; |
| 2194 | |
| 2195 | return nlmsg_end(skb, nlh); |
| 2196 | |
| 2197 | nla_put_failure: |
| 2198 | nlmsg_cancel(skb, nlh); |
| 2199 | return -EMSGSIZE; |
| 2200 | } |
| 2201 | |
Nicolas Dichtel | 8cd3ac9 | 2012-12-04 01:13:40 +0000 | [diff] [blame^] | 2202 | static size_t mroute_msgsize(bool unresolved, int maxvif) |
| 2203 | { |
| 2204 | size_t len = |
| 2205 | NLMSG_ALIGN(sizeof(struct rtmsg)) |
| 2206 | + nla_total_size(4) /* RTA_TABLE */ |
| 2207 | + nla_total_size(4) /* RTA_SRC */ |
| 2208 | + nla_total_size(4) /* RTA_DST */ |
| 2209 | ; |
| 2210 | |
| 2211 | if (!unresolved) |
| 2212 | len = len |
| 2213 | + nla_total_size(4) /* RTA_IIF */ |
| 2214 | + nla_total_size(0) /* RTA_MULTIPATH */ |
| 2215 | + maxvif * NLA_ALIGN(sizeof(struct rtnexthop)) |
| 2216 | /* RTA_MFC_STATS */ |
| 2217 | + nla_total_size(sizeof(struct rta_mfc_stats)) |
| 2218 | ; |
| 2219 | |
| 2220 | return len; |
| 2221 | } |
| 2222 | |
| 2223 | static void mroute_netlink_event(struct mr_table *mrt, struct mfc_cache *mfc, |
| 2224 | int cmd) |
| 2225 | { |
| 2226 | struct net *net = read_pnet(&mrt->net); |
| 2227 | struct sk_buff *skb; |
| 2228 | int err = -ENOBUFS; |
| 2229 | |
| 2230 | skb = nlmsg_new(mroute_msgsize(mfc->mfc_parent >= MAXVIFS, mrt->maxvif), |
| 2231 | GFP_ATOMIC); |
| 2232 | if (skb == NULL) |
| 2233 | goto errout; |
| 2234 | |
| 2235 | err = ipmr_fill_mroute(mrt, skb, 0, 0, mfc, cmd); |
| 2236 | if (err < 0) |
| 2237 | goto errout; |
| 2238 | |
| 2239 | rtnl_notify(skb, net, 0, RTNLGRP_IPV4_MROUTE, NULL, GFP_ATOMIC); |
| 2240 | return; |
| 2241 | |
| 2242 | errout: |
| 2243 | kfree_skb(skb); |
| 2244 | if (err < 0) |
| 2245 | rtnl_set_sk_err(net, RTNLGRP_IPV4_MROUTE, err); |
| 2246 | } |
| 2247 | |
Patrick McHardy | cb6a4e4 | 2010-04-26 16:02:08 +0200 | [diff] [blame] | 2248 | static int ipmr_rtm_dumproute(struct sk_buff *skb, struct netlink_callback *cb) |
| 2249 | { |
| 2250 | struct net *net = sock_net(skb->sk); |
| 2251 | struct mr_table *mrt; |
| 2252 | struct mfc_cache *mfc; |
| 2253 | unsigned int t = 0, s_t; |
| 2254 | unsigned int h = 0, s_h; |
| 2255 | unsigned int e = 0, s_e; |
| 2256 | |
| 2257 | s_t = cb->args[0]; |
| 2258 | s_h = cb->args[1]; |
| 2259 | s_e = cb->args[2]; |
| 2260 | |
Eric Dumazet | a8c9486 | 2010-10-01 16:15:08 +0000 | [diff] [blame] | 2261 | rcu_read_lock(); |
Patrick McHardy | cb6a4e4 | 2010-04-26 16:02:08 +0200 | [diff] [blame] | 2262 | ipmr_for_each_table(mrt, net) { |
| 2263 | if (t < s_t) |
| 2264 | goto next_table; |
| 2265 | if (t > s_t) |
| 2266 | s_h = 0; |
| 2267 | for (h = s_h; h < MFC_LINES; h++) { |
Eric Dumazet | a8c9486 | 2010-10-01 16:15:08 +0000 | [diff] [blame] | 2268 | list_for_each_entry_rcu(mfc, &mrt->mfc_cache_array[h], list) { |
Patrick McHardy | cb6a4e4 | 2010-04-26 16:02:08 +0200 | [diff] [blame] | 2269 | if (e < s_e) |
| 2270 | goto next_entry; |
| 2271 | if (ipmr_fill_mroute(mrt, skb, |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 2272 | NETLINK_CB(cb->skb).portid, |
Patrick McHardy | cb6a4e4 | 2010-04-26 16:02:08 +0200 | [diff] [blame] | 2273 | cb->nlh->nlmsg_seq, |
Nicolas Dichtel | 8cd3ac9 | 2012-12-04 01:13:40 +0000 | [diff] [blame^] | 2274 | mfc, RTM_NEWROUTE) < 0) |
Patrick McHardy | cb6a4e4 | 2010-04-26 16:02:08 +0200 | [diff] [blame] | 2275 | goto done; |
| 2276 | next_entry: |
| 2277 | e++; |
| 2278 | } |
| 2279 | e = s_e = 0; |
| 2280 | } |
Nicolas Dichtel | 1eb99af | 2012-12-04 01:13:39 +0000 | [diff] [blame] | 2281 | spin_lock_bh(&mfc_unres_lock); |
| 2282 | list_for_each_entry(mfc, &mrt->mfc_unres_queue, list) { |
| 2283 | if (e < s_e) |
| 2284 | goto next_entry2; |
| 2285 | if (ipmr_fill_mroute(mrt, skb, |
| 2286 | NETLINK_CB(cb->skb).portid, |
| 2287 | cb->nlh->nlmsg_seq, |
Nicolas Dichtel | 8cd3ac9 | 2012-12-04 01:13:40 +0000 | [diff] [blame^] | 2288 | mfc, RTM_NEWROUTE) < 0) { |
Nicolas Dichtel | 1eb99af | 2012-12-04 01:13:39 +0000 | [diff] [blame] | 2289 | spin_unlock_bh(&mfc_unres_lock); |
| 2290 | goto done; |
| 2291 | } |
| 2292 | next_entry2: |
| 2293 | e++; |
| 2294 | } |
| 2295 | spin_unlock_bh(&mfc_unres_lock); |
| 2296 | e = s_e = 0; |
Patrick McHardy | cb6a4e4 | 2010-04-26 16:02:08 +0200 | [diff] [blame] | 2297 | s_h = 0; |
| 2298 | next_table: |
| 2299 | t++; |
| 2300 | } |
| 2301 | done: |
Eric Dumazet | a8c9486 | 2010-10-01 16:15:08 +0000 | [diff] [blame] | 2302 | rcu_read_unlock(); |
Patrick McHardy | cb6a4e4 | 2010-04-26 16:02:08 +0200 | [diff] [blame] | 2303 | |
| 2304 | cb->args[2] = e; |
| 2305 | cb->args[1] = h; |
| 2306 | cb->args[0] = t; |
| 2307 | |
| 2308 | return skb->len; |
| 2309 | } |
| 2310 | |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 2311 | #ifdef CONFIG_PROC_FS |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2312 | /* |
Eric Dumazet | a8cb16d | 2010-10-01 16:15:29 +0000 | [diff] [blame] | 2313 | * The /proc interfaces to multicast routing : |
| 2314 | * /proc/net/ip_mr_cache & /proc/net/ip_mr_vif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2315 | */ |
| 2316 | struct ipmr_vif_iter { |
Benjamin Thery | f6bb451 | 2009-01-22 04:56:22 +0000 | [diff] [blame] | 2317 | struct seq_net_private p; |
Patrick McHardy | f0ad086 | 2010-04-13 05:03:23 +0000 | [diff] [blame] | 2318 | struct mr_table *mrt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2319 | int ct; |
| 2320 | }; |
| 2321 | |
Benjamin Thery | f6bb451 | 2009-01-22 04:56:22 +0000 | [diff] [blame] | 2322 | static struct vif_device *ipmr_vif_seq_idx(struct net *net, |
| 2323 | struct ipmr_vif_iter *iter, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2324 | loff_t pos) |
| 2325 | { |
Patrick McHardy | f0ad086 | 2010-04-13 05:03:23 +0000 | [diff] [blame] | 2326 | struct mr_table *mrt = iter->mrt; |
Patrick McHardy | 0c12295 | 2010-04-13 05:03:22 +0000 | [diff] [blame] | 2327 | |
| 2328 | for (iter->ct = 0; iter->ct < mrt->maxvif; ++iter->ct) { |
| 2329 | if (!VIF_EXISTS(mrt, iter->ct)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2330 | continue; |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 2331 | if (pos-- == 0) |
Patrick McHardy | 0c12295 | 2010-04-13 05:03:22 +0000 | [diff] [blame] | 2332 | return &mrt->vif_table[iter->ct]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2333 | } |
| 2334 | return NULL; |
| 2335 | } |
| 2336 | |
| 2337 | static void *ipmr_vif_seq_start(struct seq_file *seq, loff_t *pos) |
Stephen Hemminger | ba93ef7 | 2008-01-21 17:28:59 -0800 | [diff] [blame] | 2338 | __acquires(mrt_lock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2339 | { |
Patrick McHardy | f0ad086 | 2010-04-13 05:03:23 +0000 | [diff] [blame] | 2340 | struct ipmr_vif_iter *iter = seq->private; |
Benjamin Thery | f6bb451 | 2009-01-22 04:56:22 +0000 | [diff] [blame] | 2341 | struct net *net = seq_file_net(seq); |
Patrick McHardy | f0ad086 | 2010-04-13 05:03:23 +0000 | [diff] [blame] | 2342 | struct mr_table *mrt; |
| 2343 | |
| 2344 | mrt = ipmr_get_table(net, RT_TABLE_DEFAULT); |
| 2345 | if (mrt == NULL) |
| 2346 | return ERR_PTR(-ENOENT); |
| 2347 | |
| 2348 | iter->mrt = mrt; |
Benjamin Thery | f6bb451 | 2009-01-22 04:56:22 +0000 | [diff] [blame] | 2349 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2350 | read_lock(&mrt_lock); |
Benjamin Thery | f6bb451 | 2009-01-22 04:56:22 +0000 | [diff] [blame] | 2351 | return *pos ? ipmr_vif_seq_idx(net, seq->private, *pos - 1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2352 | : SEQ_START_TOKEN; |
| 2353 | } |
| 2354 | |
| 2355 | static void *ipmr_vif_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
| 2356 | { |
| 2357 | struct ipmr_vif_iter *iter = seq->private; |
Benjamin Thery | f6bb451 | 2009-01-22 04:56:22 +0000 | [diff] [blame] | 2358 | struct net *net = seq_file_net(seq); |
Patrick McHardy | f0ad086 | 2010-04-13 05:03:23 +0000 | [diff] [blame] | 2359 | struct mr_table *mrt = iter->mrt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2360 | |
| 2361 | ++*pos; |
| 2362 | if (v == SEQ_START_TOKEN) |
Benjamin Thery | f6bb451 | 2009-01-22 04:56:22 +0000 | [diff] [blame] | 2363 | return ipmr_vif_seq_idx(net, iter, 0); |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 2364 | |
Patrick McHardy | 0c12295 | 2010-04-13 05:03:22 +0000 | [diff] [blame] | 2365 | while (++iter->ct < mrt->maxvif) { |
| 2366 | if (!VIF_EXISTS(mrt, iter->ct)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2367 | continue; |
Patrick McHardy | 0c12295 | 2010-04-13 05:03:22 +0000 | [diff] [blame] | 2368 | return &mrt->vif_table[iter->ct]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2369 | } |
| 2370 | return NULL; |
| 2371 | } |
| 2372 | |
| 2373 | static void ipmr_vif_seq_stop(struct seq_file *seq, void *v) |
Stephen Hemminger | ba93ef7 | 2008-01-21 17:28:59 -0800 | [diff] [blame] | 2374 | __releases(mrt_lock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2375 | { |
| 2376 | read_unlock(&mrt_lock); |
| 2377 | } |
| 2378 | |
| 2379 | static int ipmr_vif_seq_show(struct seq_file *seq, void *v) |
| 2380 | { |
Patrick McHardy | f0ad086 | 2010-04-13 05:03:23 +0000 | [diff] [blame] | 2381 | struct ipmr_vif_iter *iter = seq->private; |
| 2382 | struct mr_table *mrt = iter->mrt; |
Benjamin Thery | f6bb451 | 2009-01-22 04:56:22 +0000 | [diff] [blame] | 2383 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2384 | if (v == SEQ_START_TOKEN) { |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 2385 | seq_puts(seq, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2386 | "Interface BytesIn PktsIn BytesOut PktsOut Flags Local Remote\n"); |
| 2387 | } else { |
| 2388 | const struct vif_device *vif = v; |
| 2389 | const char *name = vif->dev ? vif->dev->name : "none"; |
| 2390 | |
| 2391 | seq_printf(seq, |
| 2392 | "%2Zd %-10s %8ld %7ld %8ld %7ld %05X %08X %08X\n", |
Patrick McHardy | 0c12295 | 2010-04-13 05:03:22 +0000 | [diff] [blame] | 2393 | vif - mrt->vif_table, |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 2394 | name, vif->bytes_in, vif->pkt_in, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2395 | vif->bytes_out, vif->pkt_out, |
| 2396 | vif->flags, vif->local, vif->remote); |
| 2397 | } |
| 2398 | return 0; |
| 2399 | } |
| 2400 | |
Stephen Hemminger | f690808 | 2007-03-12 14:34:29 -0700 | [diff] [blame] | 2401 | static const struct seq_operations ipmr_vif_seq_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2402 | .start = ipmr_vif_seq_start, |
| 2403 | .next = ipmr_vif_seq_next, |
| 2404 | .stop = ipmr_vif_seq_stop, |
| 2405 | .show = ipmr_vif_seq_show, |
| 2406 | }; |
| 2407 | |
| 2408 | static int ipmr_vif_open(struct inode *inode, struct file *file) |
| 2409 | { |
Benjamin Thery | f6bb451 | 2009-01-22 04:56:22 +0000 | [diff] [blame] | 2410 | return seq_open_net(inode, file, &ipmr_vif_seq_ops, |
| 2411 | sizeof(struct ipmr_vif_iter)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2412 | } |
| 2413 | |
Arjan van de Ven | 9a32144 | 2007-02-12 00:55:35 -0800 | [diff] [blame] | 2414 | static const struct file_operations ipmr_vif_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2415 | .owner = THIS_MODULE, |
| 2416 | .open = ipmr_vif_open, |
| 2417 | .read = seq_read, |
| 2418 | .llseek = seq_lseek, |
Benjamin Thery | f6bb451 | 2009-01-22 04:56:22 +0000 | [diff] [blame] | 2419 | .release = seq_release_net, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2420 | }; |
| 2421 | |
| 2422 | struct ipmr_mfc_iter { |
Benjamin Thery | f6bb451 | 2009-01-22 04:56:22 +0000 | [diff] [blame] | 2423 | struct seq_net_private p; |
Patrick McHardy | f0ad086 | 2010-04-13 05:03:23 +0000 | [diff] [blame] | 2424 | struct mr_table *mrt; |
Patrick McHardy | 862465f | 2010-04-13 05:03:21 +0000 | [diff] [blame] | 2425 | struct list_head *cache; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2426 | int ct; |
| 2427 | }; |
| 2428 | |
| 2429 | |
Benjamin Thery | f6bb451 | 2009-01-22 04:56:22 +0000 | [diff] [blame] | 2430 | static struct mfc_cache *ipmr_mfc_seq_idx(struct net *net, |
| 2431 | struct ipmr_mfc_iter *it, loff_t pos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2432 | { |
Patrick McHardy | f0ad086 | 2010-04-13 05:03:23 +0000 | [diff] [blame] | 2433 | struct mr_table *mrt = it->mrt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2434 | struct mfc_cache *mfc; |
| 2435 | |
Eric Dumazet | a8c9486 | 2010-10-01 16:15:08 +0000 | [diff] [blame] | 2436 | rcu_read_lock(); |
Patrick McHardy | 862465f | 2010-04-13 05:03:21 +0000 | [diff] [blame] | 2437 | for (it->ct = 0; it->ct < MFC_LINES; it->ct++) { |
Patrick McHardy | 0c12295 | 2010-04-13 05:03:22 +0000 | [diff] [blame] | 2438 | it->cache = &mrt->mfc_cache_array[it->ct]; |
Eric Dumazet | a8c9486 | 2010-10-01 16:15:08 +0000 | [diff] [blame] | 2439 | list_for_each_entry_rcu(mfc, it->cache, list) |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 2440 | if (pos-- == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2441 | return mfc; |
Patrick McHardy | 862465f | 2010-04-13 05:03:21 +0000 | [diff] [blame] | 2442 | } |
Eric Dumazet | a8c9486 | 2010-10-01 16:15:08 +0000 | [diff] [blame] | 2443 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2444 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2445 | spin_lock_bh(&mfc_unres_lock); |
Patrick McHardy | 0c12295 | 2010-04-13 05:03:22 +0000 | [diff] [blame] | 2446 | it->cache = &mrt->mfc_unres_queue; |
Patrick McHardy | 862465f | 2010-04-13 05:03:21 +0000 | [diff] [blame] | 2447 | list_for_each_entry(mfc, it->cache, list) |
Patrick McHardy | e258beb | 2010-04-13 05:03:19 +0000 | [diff] [blame] | 2448 | if (pos-- == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2449 | return mfc; |
| 2450 | spin_unlock_bh(&mfc_unres_lock); |
| 2451 | |
| 2452 | it->cache = NULL; |
| 2453 | return NULL; |
| 2454 | } |
| 2455 | |
| 2456 | |
| 2457 | static void *ipmr_mfc_seq_start(struct seq_file *seq, loff_t *pos) |
| 2458 | { |
| 2459 | struct ipmr_mfc_iter *it = seq->private; |
Benjamin Thery | f6bb451 | 2009-01-22 04:56:22 +0000 | [diff] [blame] | 2460 | struct net *net = seq_file_net(seq); |
Patrick McHardy | f0ad086 | 2010-04-13 05:03:23 +0000 | [diff] [blame] | 2461 | struct mr_table *mrt; |
Benjamin Thery | f6bb451 | 2009-01-22 04:56:22 +0000 | [diff] [blame] | 2462 | |
Patrick McHardy | f0ad086 | 2010-04-13 05:03:23 +0000 | [diff] [blame] | 2463 | mrt = ipmr_get_table(net, RT_TABLE_DEFAULT); |
| 2464 | if (mrt == NULL) |
| 2465 | return ERR_PTR(-ENOENT); |
| 2466 | |
| 2467 | it->mrt = mrt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2468 | it->cache = NULL; |
| 2469 | it->ct = 0; |
Benjamin Thery | f6bb451 | 2009-01-22 04:56:22 +0000 | [diff] [blame] | 2470 | return *pos ? ipmr_mfc_seq_idx(net, seq->private, *pos - 1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2471 | : SEQ_START_TOKEN; |
| 2472 | } |
| 2473 | |
| 2474 | static void *ipmr_mfc_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
| 2475 | { |
| 2476 | struct mfc_cache *mfc = v; |
| 2477 | struct ipmr_mfc_iter *it = seq->private; |
Benjamin Thery | f6bb451 | 2009-01-22 04:56:22 +0000 | [diff] [blame] | 2478 | struct net *net = seq_file_net(seq); |
Patrick McHardy | f0ad086 | 2010-04-13 05:03:23 +0000 | [diff] [blame] | 2479 | struct mr_table *mrt = it->mrt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2480 | |
| 2481 | ++*pos; |
| 2482 | |
| 2483 | if (v == SEQ_START_TOKEN) |
Benjamin Thery | f6bb451 | 2009-01-22 04:56:22 +0000 | [diff] [blame] | 2484 | return ipmr_mfc_seq_idx(net, seq->private, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2485 | |
Patrick McHardy | 862465f | 2010-04-13 05:03:21 +0000 | [diff] [blame] | 2486 | if (mfc->list.next != it->cache) |
| 2487 | return list_entry(mfc->list.next, struct mfc_cache, list); |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 2488 | |
Patrick McHardy | 0c12295 | 2010-04-13 05:03:22 +0000 | [diff] [blame] | 2489 | if (it->cache == &mrt->mfc_unres_queue) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2490 | goto end_of_list; |
| 2491 | |
Patrick McHardy | 0c12295 | 2010-04-13 05:03:22 +0000 | [diff] [blame] | 2492 | BUG_ON(it->cache != &mrt->mfc_cache_array[it->ct]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2493 | |
| 2494 | while (++it->ct < MFC_LINES) { |
Patrick McHardy | 0c12295 | 2010-04-13 05:03:22 +0000 | [diff] [blame] | 2495 | it->cache = &mrt->mfc_cache_array[it->ct]; |
Patrick McHardy | 862465f | 2010-04-13 05:03:21 +0000 | [diff] [blame] | 2496 | if (list_empty(it->cache)) |
| 2497 | continue; |
| 2498 | return list_first_entry(it->cache, struct mfc_cache, list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2499 | } |
| 2500 | |
| 2501 | /* exhausted cache_array, show unresolved */ |
Eric Dumazet | a8c9486 | 2010-10-01 16:15:08 +0000 | [diff] [blame] | 2502 | rcu_read_unlock(); |
Patrick McHardy | 0c12295 | 2010-04-13 05:03:22 +0000 | [diff] [blame] | 2503 | it->cache = &mrt->mfc_unres_queue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2504 | it->ct = 0; |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 2505 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2506 | spin_lock_bh(&mfc_unres_lock); |
Patrick McHardy | 862465f | 2010-04-13 05:03:21 +0000 | [diff] [blame] | 2507 | if (!list_empty(it->cache)) |
| 2508 | return list_first_entry(it->cache, struct mfc_cache, list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2509 | |
Eric Dumazet | a8cb16d | 2010-10-01 16:15:29 +0000 | [diff] [blame] | 2510 | end_of_list: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2511 | spin_unlock_bh(&mfc_unres_lock); |
| 2512 | it->cache = NULL; |
| 2513 | |
| 2514 | return NULL; |
| 2515 | } |
| 2516 | |
| 2517 | static void ipmr_mfc_seq_stop(struct seq_file *seq, void *v) |
| 2518 | { |
| 2519 | struct ipmr_mfc_iter *it = seq->private; |
Patrick McHardy | f0ad086 | 2010-04-13 05:03:23 +0000 | [diff] [blame] | 2520 | struct mr_table *mrt = it->mrt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2521 | |
Patrick McHardy | 0c12295 | 2010-04-13 05:03:22 +0000 | [diff] [blame] | 2522 | if (it->cache == &mrt->mfc_unres_queue) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2523 | spin_unlock_bh(&mfc_unres_lock); |
Patrick McHardy | 0c12295 | 2010-04-13 05:03:22 +0000 | [diff] [blame] | 2524 | else if (it->cache == &mrt->mfc_cache_array[it->ct]) |
Eric Dumazet | a8c9486 | 2010-10-01 16:15:08 +0000 | [diff] [blame] | 2525 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2526 | } |
| 2527 | |
| 2528 | static int ipmr_mfc_seq_show(struct seq_file *seq, void *v) |
| 2529 | { |
| 2530 | int n; |
| 2531 | |
| 2532 | if (v == SEQ_START_TOKEN) { |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 2533 | seq_puts(seq, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2534 | "Group Origin Iif Pkts Bytes Wrong Oifs\n"); |
| 2535 | } else { |
| 2536 | const struct mfc_cache *mfc = v; |
| 2537 | const struct ipmr_mfc_iter *it = seq->private; |
Patrick McHardy | f0ad086 | 2010-04-13 05:03:23 +0000 | [diff] [blame] | 2538 | const struct mr_table *mrt = it->mrt; |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 2539 | |
Eric Dumazet | 0eae88f | 2010-04-20 19:06:52 -0700 | [diff] [blame] | 2540 | seq_printf(seq, "%08X %08X %-3hd", |
| 2541 | (__force u32) mfc->mfc_mcastgrp, |
| 2542 | (__force u32) mfc->mfc_origin, |
Benjamin Thery | 1ea472e | 2008-12-03 22:21:47 -0800 | [diff] [blame] | 2543 | mfc->mfc_parent); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2544 | |
Patrick McHardy | 0c12295 | 2010-04-13 05:03:22 +0000 | [diff] [blame] | 2545 | if (it->cache != &mrt->mfc_unres_queue) { |
Benjamin Thery | 1ea472e | 2008-12-03 22:21:47 -0800 | [diff] [blame] | 2546 | seq_printf(seq, " %8lu %8lu %8lu", |
| 2547 | mfc->mfc_un.res.pkt, |
| 2548 | mfc->mfc_un.res.bytes, |
| 2549 | mfc->mfc_un.res.wrong_if); |
Stephen Hemminger | 132adf5 | 2007-03-08 20:44:43 -0800 | [diff] [blame] | 2550 | for (n = mfc->mfc_un.res.minvif; |
Eric Dumazet | a8cb16d | 2010-10-01 16:15:29 +0000 | [diff] [blame] | 2551 | n < mfc->mfc_un.res.maxvif; n++) { |
Patrick McHardy | 0c12295 | 2010-04-13 05:03:22 +0000 | [diff] [blame] | 2552 | if (VIF_EXISTS(mrt, n) && |
Benjamin Thery | cf958ae3 | 2009-01-22 04:56:16 +0000 | [diff] [blame] | 2553 | mfc->mfc_un.res.ttls[n] < 255) |
| 2554 | seq_printf(seq, |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 2555 | " %2d:%-3d", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2556 | n, mfc->mfc_un.res.ttls[n]); |
| 2557 | } |
Benjamin Thery | 1ea472e | 2008-12-03 22:21:47 -0800 | [diff] [blame] | 2558 | } else { |
| 2559 | /* unresolved mfc_caches don't contain |
| 2560 | * pkt, bytes and wrong_if values |
| 2561 | */ |
| 2562 | seq_printf(seq, " %8lu %8lu %8lu", 0ul, 0ul, 0ul); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2563 | } |
| 2564 | seq_putc(seq, '\n'); |
| 2565 | } |
| 2566 | return 0; |
| 2567 | } |
| 2568 | |
Stephen Hemminger | f690808 | 2007-03-12 14:34:29 -0700 | [diff] [blame] | 2569 | static const struct seq_operations ipmr_mfc_seq_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2570 | .start = ipmr_mfc_seq_start, |
| 2571 | .next = ipmr_mfc_seq_next, |
| 2572 | .stop = ipmr_mfc_seq_stop, |
| 2573 | .show = ipmr_mfc_seq_show, |
| 2574 | }; |
| 2575 | |
| 2576 | static int ipmr_mfc_open(struct inode *inode, struct file *file) |
| 2577 | { |
Benjamin Thery | f6bb451 | 2009-01-22 04:56:22 +0000 | [diff] [blame] | 2578 | return seq_open_net(inode, file, &ipmr_mfc_seq_ops, |
| 2579 | sizeof(struct ipmr_mfc_iter)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2580 | } |
| 2581 | |
Arjan van de Ven | 9a32144 | 2007-02-12 00:55:35 -0800 | [diff] [blame] | 2582 | static const struct file_operations ipmr_mfc_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2583 | .owner = THIS_MODULE, |
| 2584 | .open = ipmr_mfc_open, |
| 2585 | .read = seq_read, |
| 2586 | .llseek = seq_lseek, |
Benjamin Thery | f6bb451 | 2009-01-22 04:56:22 +0000 | [diff] [blame] | 2587 | .release = seq_release_net, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2588 | }; |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 2589 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2590 | |
| 2591 | #ifdef CONFIG_IP_PIMSM_V2 |
Alexey Dobriyan | 3261309 | 2009-09-14 12:21:47 +0000 | [diff] [blame] | 2592 | static const struct net_protocol pim_protocol = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2593 | .handler = pim_rcv, |
Tom Goff | 403dbb9 | 2009-06-14 03:16:13 -0700 | [diff] [blame] | 2594 | .netns_ok = 1, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2595 | }; |
| 2596 | #endif |
| 2597 | |
| 2598 | |
| 2599 | /* |
| 2600 | * Setup for IP multicast routing |
| 2601 | */ |
Benjamin Thery | cf958ae3 | 2009-01-22 04:56:16 +0000 | [diff] [blame] | 2602 | static int __net_init ipmr_net_init(struct net *net) |
| 2603 | { |
Patrick McHardy | f0ad086 | 2010-04-13 05:03:23 +0000 | [diff] [blame] | 2604 | int err; |
Benjamin Thery | cf958ae3 | 2009-01-22 04:56:16 +0000 | [diff] [blame] | 2605 | |
Patrick McHardy | f0ad086 | 2010-04-13 05:03:23 +0000 | [diff] [blame] | 2606 | err = ipmr_rules_init(net); |
| 2607 | if (err < 0) |
Benjamin Thery | cf958ae3 | 2009-01-22 04:56:16 +0000 | [diff] [blame] | 2608 | goto fail; |
Benjamin Thery | f6bb451 | 2009-01-22 04:56:22 +0000 | [diff] [blame] | 2609 | |
| 2610 | #ifdef CONFIG_PROC_FS |
| 2611 | err = -ENOMEM; |
| 2612 | if (!proc_net_fops_create(net, "ip_mr_vif", 0, &ipmr_vif_fops)) |
| 2613 | goto proc_vif_fail; |
| 2614 | if (!proc_net_fops_create(net, "ip_mr_cache", 0, &ipmr_mfc_fops)) |
| 2615 | goto proc_cache_fail; |
| 2616 | #endif |
Benjamin Thery | 2bb8b26 | 2009-01-22 04:56:18 +0000 | [diff] [blame] | 2617 | return 0; |
| 2618 | |
Benjamin Thery | f6bb451 | 2009-01-22 04:56:22 +0000 | [diff] [blame] | 2619 | #ifdef CONFIG_PROC_FS |
| 2620 | proc_cache_fail: |
| 2621 | proc_net_remove(net, "ip_mr_vif"); |
| 2622 | proc_vif_fail: |
Patrick McHardy | f0ad086 | 2010-04-13 05:03:23 +0000 | [diff] [blame] | 2623 | ipmr_rules_exit(net); |
Benjamin Thery | f6bb451 | 2009-01-22 04:56:22 +0000 | [diff] [blame] | 2624 | #endif |
Benjamin Thery | cf958ae3 | 2009-01-22 04:56:16 +0000 | [diff] [blame] | 2625 | fail: |
| 2626 | return err; |
| 2627 | } |
| 2628 | |
| 2629 | static void __net_exit ipmr_net_exit(struct net *net) |
| 2630 | { |
Benjamin Thery | f6bb451 | 2009-01-22 04:56:22 +0000 | [diff] [blame] | 2631 | #ifdef CONFIG_PROC_FS |
| 2632 | proc_net_remove(net, "ip_mr_cache"); |
| 2633 | proc_net_remove(net, "ip_mr_vif"); |
| 2634 | #endif |
Patrick McHardy | f0ad086 | 2010-04-13 05:03:23 +0000 | [diff] [blame] | 2635 | ipmr_rules_exit(net); |
Benjamin Thery | cf958ae3 | 2009-01-22 04:56:16 +0000 | [diff] [blame] | 2636 | } |
| 2637 | |
| 2638 | static struct pernet_operations ipmr_net_ops = { |
| 2639 | .init = ipmr_net_init, |
| 2640 | .exit = ipmr_net_exit, |
| 2641 | }; |
YOSHIFUJI Hideaki | e905a9e | 2007-02-09 23:24:47 +0900 | [diff] [blame] | 2642 | |
Wang Chen | 03d2f89 | 2008-07-03 12:13:36 +0800 | [diff] [blame] | 2643 | int __init ip_mr_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2644 | { |
Wang Chen | 03d2f89 | 2008-07-03 12:13:36 +0800 | [diff] [blame] | 2645 | int err; |
| 2646 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2647 | mrt_cachep = kmem_cache_create("ip_mrt_cache", |
| 2648 | sizeof(struct mfc_cache), |
Eric Dumazet | a8c9486 | 2010-10-01 16:15:08 +0000 | [diff] [blame] | 2649 | 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC, |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 2650 | NULL); |
Wang Chen | 03d2f89 | 2008-07-03 12:13:36 +0800 | [diff] [blame] | 2651 | if (!mrt_cachep) |
| 2652 | return -ENOMEM; |
| 2653 | |
Benjamin Thery | cf958ae3 | 2009-01-22 04:56:16 +0000 | [diff] [blame] | 2654 | err = register_pernet_subsys(&ipmr_net_ops); |
| 2655 | if (err) |
| 2656 | goto reg_pernet_fail; |
| 2657 | |
Wang Chen | 03d2f89 | 2008-07-03 12:13:36 +0800 | [diff] [blame] | 2658 | err = register_netdevice_notifier(&ip_mr_notifier); |
| 2659 | if (err) |
| 2660 | goto reg_notif_fail; |
Tom Goff | 403dbb9 | 2009-06-14 03:16:13 -0700 | [diff] [blame] | 2661 | #ifdef CONFIG_IP_PIMSM_V2 |
| 2662 | if (inet_add_protocol(&pim_protocol, IPPROTO_PIM) < 0) { |
Joe Perches | 058bd4d | 2012-03-11 18:36:11 +0000 | [diff] [blame] | 2663 | pr_err("%s: can't add PIM protocol\n", __func__); |
Tom Goff | 403dbb9 | 2009-06-14 03:16:13 -0700 | [diff] [blame] | 2664 | err = -EAGAIN; |
| 2665 | goto add_proto_fail; |
| 2666 | } |
| 2667 | #endif |
Greg Rose | c7ac867 | 2011-06-10 01:27:09 +0000 | [diff] [blame] | 2668 | rtnl_register(RTNL_FAMILY_IPMR, RTM_GETROUTE, |
| 2669 | NULL, ipmr_rtm_dumproute, NULL); |
Wang Chen | 03d2f89 | 2008-07-03 12:13:36 +0800 | [diff] [blame] | 2670 | return 0; |
Benjamin Thery | f6bb451 | 2009-01-22 04:56:22 +0000 | [diff] [blame] | 2671 | |
Tom Goff | 403dbb9 | 2009-06-14 03:16:13 -0700 | [diff] [blame] | 2672 | #ifdef CONFIG_IP_PIMSM_V2 |
| 2673 | add_proto_fail: |
| 2674 | unregister_netdevice_notifier(&ip_mr_notifier); |
| 2675 | #endif |
Benjamin Thery | c3e3889 | 2008-11-19 14:07:41 -0800 | [diff] [blame] | 2676 | reg_notif_fail: |
Benjamin Thery | cf958ae3 | 2009-01-22 04:56:16 +0000 | [diff] [blame] | 2677 | unregister_pernet_subsys(&ipmr_net_ops); |
| 2678 | reg_pernet_fail: |
Benjamin Thery | c3e3889 | 2008-11-19 14:07:41 -0800 | [diff] [blame] | 2679 | kmem_cache_destroy(mrt_cachep); |
Wang Chen | 03d2f89 | 2008-07-03 12:13:36 +0800 | [diff] [blame] | 2680 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2681 | } |