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