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