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