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