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