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