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