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