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