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