Thomas Graf | 14c0b97 | 2006-08-04 03:38:38 -0700 | [diff] [blame] | 1 | /* |
| 2 | * net/core/fib_rules.c Generic Routing Rules |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License as |
| 6 | * published by the Free Software Foundation, version 2. |
| 7 | * |
| 8 | * Authors: Thomas Graf <tgraf@suug.ch> |
| 9 | */ |
| 10 | |
Thomas Graf | 14c0b97 | 2006-08-04 03:38:38 -0700 | [diff] [blame] | 11 | #include <linux/types.h> |
| 12 | #include <linux/kernel.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 13 | #include <linux/slab.h> |
Thomas Graf | 14c0b97 | 2006-08-04 03:38:38 -0700 | [diff] [blame] | 14 | #include <linux/list.h> |
Paul Gortmaker | 3a9a231 | 2011-05-27 09:12:25 -0400 | [diff] [blame] | 15 | #include <linux/module.h> |
Eric W. Biederman | e9dc865 | 2007-09-12 13:02:17 +0200 | [diff] [blame] | 16 | #include <net/net_namespace.h> |
Eric W. Biederman | 881d966 | 2007-09-17 11:56:21 -0700 | [diff] [blame] | 17 | #include <net/sock.h> |
Thomas Graf | 14c0b97 | 2006-08-04 03:38:38 -0700 | [diff] [blame] | 18 | #include <net/fib_rules.h> |
Thomas Graf | e703087 | 2015-07-21 10:44:01 +0200 | [diff] [blame] | 19 | #include <net/ip_tunnels.h> |
Thomas Graf | 14c0b97 | 2006-08-04 03:38:38 -0700 | [diff] [blame] | 20 | |
Lorenzo Colitti | 622ec2c | 2016-11-04 02:23:42 +0900 | [diff] [blame] | 21 | static const struct fib_kuid_range fib_kuid_range_unset = { |
| 22 | KUIDT_INIT(0), |
| 23 | KUIDT_INIT(~0), |
| 24 | }; |
| 25 | |
Ido Schimmel | 3c71006 | 2017-03-16 09:08:12 +0100 | [diff] [blame] | 26 | bool fib_rule_matchall(const struct fib_rule *rule) |
| 27 | { |
| 28 | if (rule->iifindex || rule->oifindex || rule->mark || rule->tun_id || |
| 29 | rule->flags) |
| 30 | return false; |
| 31 | if (rule->suppress_ifgroup != -1 || rule->suppress_prefixlen != -1) |
| 32 | return false; |
| 33 | if (!uid_eq(rule->uid_range.start, fib_kuid_range_unset.start) || |
| 34 | !uid_eq(rule->uid_range.end, fib_kuid_range_unset.end)) |
| 35 | return false; |
| 36 | return true; |
| 37 | } |
| 38 | EXPORT_SYMBOL_GPL(fib_rule_matchall); |
| 39 | |
Denis V. Lunev | 2994c63 | 2007-11-10 22:12:03 -0800 | [diff] [blame] | 40 | int fib_default_rule_add(struct fib_rules_ops *ops, |
| 41 | u32 pref, u32 table, u32 flags) |
| 42 | { |
| 43 | struct fib_rule *r; |
| 44 | |
| 45 | r = kzalloc(ops->rule_size, GFP_KERNEL); |
| 46 | if (r == NULL) |
| 47 | return -ENOMEM; |
| 48 | |
| 49 | atomic_set(&r->refcnt, 1); |
| 50 | r->action = FR_ACT_TO_TBL; |
| 51 | r->pref = pref; |
| 52 | r->table = table; |
| 53 | r->flags = flags; |
Eric W. Biederman | efd7ef1 | 2015-03-11 23:04:08 -0500 | [diff] [blame] | 54 | r->fr_net = ops->fro_net; |
Lorenzo Colitti | 622ec2c | 2016-11-04 02:23:42 +0900 | [diff] [blame] | 55 | r->uid_range = fib_kuid_range_unset; |
Denis V. Lunev | 2994c63 | 2007-11-10 22:12:03 -0800 | [diff] [blame] | 56 | |
Stefan Tomanek | 73f5698 | 2013-08-03 14:14:43 +0200 | [diff] [blame] | 57 | r->suppress_prefixlen = -1; |
| 58 | r->suppress_ifgroup = -1; |
| 59 | |
Denis V. Lunev | 2994c63 | 2007-11-10 22:12:03 -0800 | [diff] [blame] | 60 | /* The lock is not required here, the list in unreacheable |
| 61 | * at the moment this function is called */ |
| 62 | list_add_tail(&r->list, &ops->rules_list); |
| 63 | return 0; |
| 64 | } |
| 65 | EXPORT_SYMBOL(fib_default_rule_add); |
| 66 | |
Phil Sutter | f53de1e | 2015-09-09 14:20:56 +0200 | [diff] [blame] | 67 | static u32 fib_default_rule_pref(struct fib_rules_ops *ops) |
Patrick McHardy | d8a566b | 2010-04-13 05:03:15 +0000 | [diff] [blame] | 68 | { |
| 69 | struct list_head *pos; |
| 70 | struct fib_rule *rule; |
| 71 | |
| 72 | if (!list_empty(&ops->rules_list)) { |
| 73 | pos = ops->rules_list.next; |
| 74 | if (pos->next != &ops->rules_list) { |
| 75 | rule = list_entry(pos->next, struct fib_rule, list); |
| 76 | if (rule->pref) |
| 77 | return rule->pref - 1; |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | return 0; |
| 82 | } |
Patrick McHardy | d8a566b | 2010-04-13 05:03:15 +0000 | [diff] [blame] | 83 | |
Denis V. Lunev | 9e3a548 | 2008-01-20 16:46:41 -0800 | [diff] [blame] | 84 | static void notify_rule_change(int event, struct fib_rule *rule, |
Thomas Graf | c17084d | 2006-08-15 00:32:48 -0700 | [diff] [blame] | 85 | struct fib_rules_ops *ops, struct nlmsghdr *nlh, |
| 86 | u32 pid); |
Thomas Graf | 14c0b97 | 2006-08-04 03:38:38 -0700 | [diff] [blame] | 87 | |
Denis V. Lunev | 5fd30ee | 2008-01-10 03:20:28 -0800 | [diff] [blame] | 88 | static struct fib_rules_ops *lookup_rules_ops(struct net *net, int family) |
Thomas Graf | 14c0b97 | 2006-08-04 03:38:38 -0700 | [diff] [blame] | 89 | { |
| 90 | struct fib_rules_ops *ops; |
| 91 | |
| 92 | rcu_read_lock(); |
Denis V. Lunev | 5fd30ee | 2008-01-10 03:20:28 -0800 | [diff] [blame] | 93 | list_for_each_entry_rcu(ops, &net->rules_ops, list) { |
Thomas Graf | 14c0b97 | 2006-08-04 03:38:38 -0700 | [diff] [blame] | 94 | if (ops->family == family) { |
| 95 | if (!try_module_get(ops->owner)) |
| 96 | ops = NULL; |
| 97 | rcu_read_unlock(); |
| 98 | return ops; |
| 99 | } |
| 100 | } |
| 101 | rcu_read_unlock(); |
| 102 | |
| 103 | return NULL; |
| 104 | } |
| 105 | |
| 106 | static void rules_ops_put(struct fib_rules_ops *ops) |
| 107 | { |
| 108 | if (ops) |
| 109 | module_put(ops->owner); |
| 110 | } |
| 111 | |
Thomas Graf | 73417f6 | 2007-03-27 13:56:52 -0700 | [diff] [blame] | 112 | static void flush_route_cache(struct fib_rules_ops *ops) |
| 113 | { |
| 114 | if (ops->flush_cache) |
Denis V. Lunev | ae299fc | 2008-07-05 19:01:28 -0700 | [diff] [blame] | 115 | ops->flush_cache(ops); |
Thomas Graf | 73417f6 | 2007-03-27 13:56:52 -0700 | [diff] [blame] | 116 | } |
| 117 | |
Eric W. Biederman | e9c5158 | 2009-12-03 12:22:55 -0800 | [diff] [blame] | 118 | static int __fib_rules_register(struct fib_rules_ops *ops) |
Thomas Graf | 14c0b97 | 2006-08-04 03:38:38 -0700 | [diff] [blame] | 119 | { |
| 120 | int err = -EEXIST; |
| 121 | struct fib_rules_ops *o; |
Denis V. Lunev | 9e3a548 | 2008-01-20 16:46:41 -0800 | [diff] [blame] | 122 | struct net *net; |
| 123 | |
| 124 | net = ops->fro_net; |
Thomas Graf | 14c0b97 | 2006-08-04 03:38:38 -0700 | [diff] [blame] | 125 | |
| 126 | if (ops->rule_size < sizeof(struct fib_rule)) |
| 127 | return -EINVAL; |
| 128 | |
| 129 | if (ops->match == NULL || ops->configure == NULL || |
| 130 | ops->compare == NULL || ops->fill == NULL || |
| 131 | ops->action == NULL) |
| 132 | return -EINVAL; |
| 133 | |
Denis V. Lunev | 5fd30ee | 2008-01-10 03:20:28 -0800 | [diff] [blame] | 134 | spin_lock(&net->rules_mod_lock); |
| 135 | list_for_each_entry(o, &net->rules_ops, list) |
Thomas Graf | 14c0b97 | 2006-08-04 03:38:38 -0700 | [diff] [blame] | 136 | if (ops->family == o->family) |
| 137 | goto errout; |
| 138 | |
Denis V. Lunev | 5fd30ee | 2008-01-10 03:20:28 -0800 | [diff] [blame] | 139 | list_add_tail_rcu(&ops->list, &net->rules_ops); |
Thomas Graf | 14c0b97 | 2006-08-04 03:38:38 -0700 | [diff] [blame] | 140 | err = 0; |
| 141 | errout: |
Denis V. Lunev | 5fd30ee | 2008-01-10 03:20:28 -0800 | [diff] [blame] | 142 | spin_unlock(&net->rules_mod_lock); |
Thomas Graf | 14c0b97 | 2006-08-04 03:38:38 -0700 | [diff] [blame] | 143 | |
| 144 | return err; |
| 145 | } |
| 146 | |
Eric W. Biederman | e9c5158 | 2009-12-03 12:22:55 -0800 | [diff] [blame] | 147 | struct fib_rules_ops * |
Patrick McHardy | 3d0c9c4 | 2010-04-26 16:02:04 +0200 | [diff] [blame] | 148 | fib_rules_register(const struct fib_rules_ops *tmpl, struct net *net) |
Eric W. Biederman | e9c5158 | 2009-12-03 12:22:55 -0800 | [diff] [blame] | 149 | { |
| 150 | struct fib_rules_ops *ops; |
| 151 | int err; |
| 152 | |
Eric Dumazet | 2fb3573 | 2010-03-09 20:03:38 +0000 | [diff] [blame] | 153 | ops = kmemdup(tmpl, sizeof(*ops), GFP_KERNEL); |
Eric W. Biederman | e9c5158 | 2009-12-03 12:22:55 -0800 | [diff] [blame] | 154 | if (ops == NULL) |
| 155 | return ERR_PTR(-ENOMEM); |
| 156 | |
| 157 | INIT_LIST_HEAD(&ops->rules_list); |
| 158 | ops->fro_net = net; |
| 159 | |
| 160 | err = __fib_rules_register(ops); |
| 161 | if (err) { |
| 162 | kfree(ops); |
| 163 | ops = ERR_PTR(err); |
| 164 | } |
| 165 | |
| 166 | return ops; |
| 167 | } |
Thomas Graf | 14c0b97 | 2006-08-04 03:38:38 -0700 | [diff] [blame] | 168 | EXPORT_SYMBOL_GPL(fib_rules_register); |
| 169 | |
stephen hemminger | 1df9916 | 2010-10-04 20:14:17 +0000 | [diff] [blame] | 170 | static void fib_rules_cleanup_ops(struct fib_rules_ops *ops) |
Thomas Graf | 14c0b97 | 2006-08-04 03:38:38 -0700 | [diff] [blame] | 171 | { |
| 172 | struct fib_rule *rule, *tmp; |
| 173 | |
Denis V. Lunev | 76c72d4 | 2007-09-16 15:44:27 -0700 | [diff] [blame] | 174 | list_for_each_entry_safe(rule, tmp, &ops->rules_list, list) { |
Thomas Graf | 14c0b97 | 2006-08-04 03:38:38 -0700 | [diff] [blame] | 175 | list_del_rcu(&rule->list); |
David S. Miller | 7a9bc9b | 2012-06-29 01:32:45 -0700 | [diff] [blame] | 176 | if (ops->delete) |
| 177 | ops->delete(rule); |
Thomas Graf | 14c0b97 | 2006-08-04 03:38:38 -0700 | [diff] [blame] | 178 | fib_rule_put(rule); |
| 179 | } |
| 180 | } |
| 181 | |
Denis V. Lunev | 9e3a548 | 2008-01-20 16:46:41 -0800 | [diff] [blame] | 182 | void fib_rules_unregister(struct fib_rules_ops *ops) |
Thomas Graf | 14c0b97 | 2006-08-04 03:38:38 -0700 | [diff] [blame] | 183 | { |
Denis V. Lunev | 9e3a548 | 2008-01-20 16:46:41 -0800 | [diff] [blame] | 184 | struct net *net = ops->fro_net; |
Thomas Graf | 14c0b97 | 2006-08-04 03:38:38 -0700 | [diff] [blame] | 185 | |
Denis V. Lunev | 5fd30ee | 2008-01-10 03:20:28 -0800 | [diff] [blame] | 186 | spin_lock(&net->rules_mod_lock); |
Denis V. Lunev | 72132c1b | 2008-01-14 22:59:30 -0800 | [diff] [blame] | 187 | list_del_rcu(&ops->list); |
Denis V. Lunev | 5fd30ee | 2008-01-10 03:20:28 -0800 | [diff] [blame] | 188 | spin_unlock(&net->rules_mod_lock); |
Thomas Graf | 14c0b97 | 2006-08-04 03:38:38 -0700 | [diff] [blame] | 189 | |
WANG Cong | 419df12 | 2015-03-31 11:01:46 -0700 | [diff] [blame] | 190 | fib_rules_cleanup_ops(ops); |
Eric W. Biederman | efd7ef1 | 2015-03-11 23:04:08 -0500 | [diff] [blame] | 191 | kfree_rcu(ops, rcu); |
Thomas Graf | 14c0b97 | 2006-08-04 03:38:38 -0700 | [diff] [blame] | 192 | } |
Thomas Graf | 14c0b97 | 2006-08-04 03:38:38 -0700 | [diff] [blame] | 193 | EXPORT_SYMBOL_GPL(fib_rules_unregister); |
| 194 | |
Lorenzo Colitti | 622ec2c | 2016-11-04 02:23:42 +0900 | [diff] [blame] | 195 | static int uid_range_set(struct fib_kuid_range *range) |
| 196 | { |
| 197 | return uid_valid(range->start) && uid_valid(range->end); |
| 198 | } |
| 199 | |
| 200 | static struct fib_kuid_range nla_get_kuid_range(struct nlattr **tb) |
| 201 | { |
| 202 | struct fib_rule_uid_range *in; |
| 203 | struct fib_kuid_range out; |
| 204 | |
| 205 | in = (struct fib_rule_uid_range *)nla_data(tb[FRA_UID_RANGE]); |
| 206 | |
| 207 | out.start = make_kuid(current_user_ns(), in->start); |
| 208 | out.end = make_kuid(current_user_ns(), in->end); |
| 209 | |
| 210 | return out; |
| 211 | } |
| 212 | |
| 213 | static int nla_put_uid_range(struct sk_buff *skb, struct fib_kuid_range *range) |
| 214 | { |
| 215 | struct fib_rule_uid_range out = { |
| 216 | from_kuid_munged(current_user_ns(), range->start), |
| 217 | from_kuid_munged(current_user_ns(), range->end) |
| 218 | }; |
| 219 | |
| 220 | return nla_put(skb, FRA_UID_RANGE, sizeof(out), &out); |
| 221 | } |
| 222 | |
Thomas Graf | 3dfbcc4 | 2006-11-09 15:23:20 -0800 | [diff] [blame] | 223 | static int fib_rule_match(struct fib_rule *rule, struct fib_rules_ops *ops, |
David Ahern | 96c63fa | 2016-06-08 10:55:39 -0700 | [diff] [blame] | 224 | struct flowi *fl, int flags, |
| 225 | struct fib_lookup_arg *arg) |
Thomas Graf | 3dfbcc4 | 2006-11-09 15:23:20 -0800 | [diff] [blame] | 226 | { |
| 227 | int ret = 0; |
| 228 | |
David S. Miller | 1d28f42 | 2011-03-12 00:29:39 -0500 | [diff] [blame] | 229 | if (rule->iifindex && (rule->iifindex != fl->flowi_iif)) |
Thomas Graf | 3dfbcc4 | 2006-11-09 15:23:20 -0800 | [diff] [blame] | 230 | goto out; |
| 231 | |
David S. Miller | 1d28f42 | 2011-03-12 00:29:39 -0500 | [diff] [blame] | 232 | if (rule->oifindex && (rule->oifindex != fl->flowi_oif)) |
Patrick McHardy | 1b038a5 | 2009-12-03 01:25:56 +0000 | [diff] [blame] | 233 | goto out; |
| 234 | |
David S. Miller | 1d28f42 | 2011-03-12 00:29:39 -0500 | [diff] [blame] | 235 | if ((rule->mark ^ fl->flowi_mark) & rule->mark_mask) |
Thomas Graf | 3dfbcc4 | 2006-11-09 15:23:20 -0800 | [diff] [blame] | 236 | goto out; |
| 237 | |
Thomas Graf | e703087 | 2015-07-21 10:44:01 +0200 | [diff] [blame] | 238 | if (rule->tun_id && (rule->tun_id != fl->flowi_tun_key.tun_id)) |
| 239 | goto out; |
| 240 | |
David Ahern | 96c63fa | 2016-06-08 10:55:39 -0700 | [diff] [blame] | 241 | if (rule->l3mdev && !l3mdev_fib_rule_match(rule->fr_net, fl, arg)) |
| 242 | goto out; |
| 243 | |
Lorenzo Colitti | 622ec2c | 2016-11-04 02:23:42 +0900 | [diff] [blame] | 244 | if (uid_lt(fl->flowi_uid, rule->uid_range.start) || |
| 245 | uid_gt(fl->flowi_uid, rule->uid_range.end)) |
| 246 | goto out; |
| 247 | |
Thomas Graf | 3dfbcc4 | 2006-11-09 15:23:20 -0800 | [diff] [blame] | 248 | ret = ops->match(rule, fl, flags); |
| 249 | out: |
| 250 | return (rule->flags & FIB_RULE_INVERT) ? !ret : ret; |
| 251 | } |
| 252 | |
Thomas Graf | 14c0b97 | 2006-08-04 03:38:38 -0700 | [diff] [blame] | 253 | int fib_rules_lookup(struct fib_rules_ops *ops, struct flowi *fl, |
| 254 | int flags, struct fib_lookup_arg *arg) |
| 255 | { |
| 256 | struct fib_rule *rule; |
| 257 | int err; |
| 258 | |
| 259 | rcu_read_lock(); |
| 260 | |
Denis V. Lunev | 76c72d4 | 2007-09-16 15:44:27 -0700 | [diff] [blame] | 261 | list_for_each_entry_rcu(rule, &ops->rules_list, list) { |
Thomas Graf | 0947c9fe | 2007-03-26 17:14:15 -0700 | [diff] [blame] | 262 | jumped: |
David Ahern | 96c63fa | 2016-06-08 10:55:39 -0700 | [diff] [blame] | 263 | if (!fib_rule_match(rule, ops, fl, flags, arg)) |
Thomas Graf | 14c0b97 | 2006-08-04 03:38:38 -0700 | [diff] [blame] | 264 | continue; |
| 265 | |
Thomas Graf | 0947c9fe | 2007-03-26 17:14:15 -0700 | [diff] [blame] | 266 | if (rule->action == FR_ACT_GOTO) { |
| 267 | struct fib_rule *target; |
| 268 | |
| 269 | target = rcu_dereference(rule->ctarget); |
| 270 | if (target == NULL) { |
| 271 | continue; |
| 272 | } else { |
| 273 | rule = target; |
| 274 | goto jumped; |
| 275 | } |
Thomas Graf | fa0b2d1 | 2007-03-26 17:38:53 -0700 | [diff] [blame] | 276 | } else if (rule->action == FR_ACT_NOP) |
| 277 | continue; |
| 278 | else |
Thomas Graf | 0947c9fe | 2007-03-26 17:14:15 -0700 | [diff] [blame] | 279 | err = ops->action(rule, fl, flags, arg); |
| 280 | |
Stefan Tomanek | 7764a45 | 2013-08-01 02:17:15 +0200 | [diff] [blame] | 281 | if (!err && ops->suppress && ops->suppress(rule, arg)) |
| 282 | continue; |
| 283 | |
Thomas Graf | 14c0b97 | 2006-08-04 03:38:38 -0700 | [diff] [blame] | 284 | if (err != -EAGAIN) { |
Eric Dumazet | ebc0ffa | 2010-10-05 10:41:36 +0000 | [diff] [blame] | 285 | if ((arg->flags & FIB_LOOKUP_NOREF) || |
| 286 | likely(atomic_inc_not_zero(&rule->refcnt))) { |
Eric Dumazet | 7fa7cb7 | 2010-09-27 04:18:27 +0000 | [diff] [blame] | 287 | arg->rule = rule; |
| 288 | goto out; |
| 289 | } |
| 290 | break; |
Thomas Graf | 14c0b97 | 2006-08-04 03:38:38 -0700 | [diff] [blame] | 291 | } |
| 292 | } |
| 293 | |
Steven Whitehouse | 83886b6 | 2007-03-30 13:34:27 -0700 | [diff] [blame] | 294 | err = -ESRCH; |
Thomas Graf | 14c0b97 | 2006-08-04 03:38:38 -0700 | [diff] [blame] | 295 | out: |
| 296 | rcu_read_unlock(); |
| 297 | |
| 298 | return err; |
| 299 | } |
Thomas Graf | 14c0b97 | 2006-08-04 03:38:38 -0700 | [diff] [blame] | 300 | EXPORT_SYMBOL_GPL(fib_rules_lookup); |
| 301 | |
Thomas Graf | e1701c6 | 2007-03-24 12:46:02 -0700 | [diff] [blame] | 302 | static int validate_rulemsg(struct fib_rule_hdr *frh, struct nlattr **tb, |
| 303 | struct fib_rules_ops *ops) |
| 304 | { |
| 305 | int err = -EINVAL; |
| 306 | |
| 307 | if (frh->src_len) |
| 308 | if (tb[FRA_SRC] == NULL || |
| 309 | frh->src_len > (ops->addr_size * 8) || |
| 310 | nla_len(tb[FRA_SRC]) != ops->addr_size) |
| 311 | goto errout; |
| 312 | |
| 313 | if (frh->dst_len) |
| 314 | if (tb[FRA_DST] == NULL || |
| 315 | frh->dst_len > (ops->addr_size * 8) || |
| 316 | nla_len(tb[FRA_DST]) != ops->addr_size) |
| 317 | goto errout; |
| 318 | |
| 319 | err = 0; |
| 320 | errout: |
| 321 | return err; |
| 322 | } |
| 323 | |
Mateusz Bajorski | 153380e | 2016-06-29 09:22:10 +0200 | [diff] [blame] | 324 | static int rule_exists(struct fib_rules_ops *ops, struct fib_rule_hdr *frh, |
| 325 | struct nlattr **tb, struct fib_rule *rule) |
| 326 | { |
| 327 | struct fib_rule *r; |
| 328 | |
| 329 | list_for_each_entry(r, &ops->rules_list, list) { |
| 330 | if (r->action != rule->action) |
| 331 | continue; |
| 332 | |
| 333 | if (r->table != rule->table) |
| 334 | continue; |
| 335 | |
| 336 | if (r->pref != rule->pref) |
| 337 | continue; |
| 338 | |
| 339 | if (memcmp(r->iifname, rule->iifname, IFNAMSIZ)) |
| 340 | continue; |
| 341 | |
| 342 | if (memcmp(r->oifname, rule->oifname, IFNAMSIZ)) |
| 343 | continue; |
| 344 | |
| 345 | if (r->mark != rule->mark) |
| 346 | continue; |
| 347 | |
| 348 | if (r->mark_mask != rule->mark_mask) |
| 349 | continue; |
| 350 | |
| 351 | if (r->tun_id != rule->tun_id) |
| 352 | continue; |
| 353 | |
| 354 | if (r->fr_net != rule->fr_net) |
| 355 | continue; |
| 356 | |
| 357 | if (r->l3mdev != rule->l3mdev) |
| 358 | continue; |
| 359 | |
Lorenzo Colitti | 35b8073 | 2016-11-07 00:16:25 +0900 | [diff] [blame] | 360 | if (!uid_eq(r->uid_range.start, rule->uid_range.start) || |
| 361 | !uid_eq(r->uid_range.end, rule->uid_range.end)) |
| 362 | continue; |
| 363 | |
Mateusz Bajorski | 153380e | 2016-06-29 09:22:10 +0200 | [diff] [blame] | 364 | if (!ops->compare(r, frh, tb)) |
| 365 | continue; |
| 366 | return 1; |
| 367 | } |
| 368 | return 0; |
| 369 | } |
| 370 | |
David Ahern | 96c63fa | 2016-06-08 10:55:39 -0700 | [diff] [blame] | 371 | int fib_nl_newrule(struct sk_buff *skb, struct nlmsghdr *nlh) |
Thomas Graf | 14c0b97 | 2006-08-04 03:38:38 -0700 | [diff] [blame] | 372 | { |
YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 373 | struct net *net = sock_net(skb->sk); |
Thomas Graf | 14c0b97 | 2006-08-04 03:38:38 -0700 | [diff] [blame] | 374 | struct fib_rule_hdr *frh = nlmsg_data(nlh); |
| 375 | struct fib_rules_ops *ops = NULL; |
| 376 | struct fib_rule *rule, *r, *last = NULL; |
| 377 | struct nlattr *tb[FRA_MAX+1]; |
Thomas Graf | 0947c9fe | 2007-03-26 17:14:15 -0700 | [diff] [blame] | 378 | int err = -EINVAL, unresolved = 0; |
Thomas Graf | 14c0b97 | 2006-08-04 03:38:38 -0700 | [diff] [blame] | 379 | |
| 380 | if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*frh))) |
| 381 | goto errout; |
| 382 | |
Denis V. Lunev | 5fd30ee | 2008-01-10 03:20:28 -0800 | [diff] [blame] | 383 | ops = lookup_rules_ops(net, frh->family); |
Thomas Graf | 14c0b97 | 2006-08-04 03:38:38 -0700 | [diff] [blame] | 384 | if (ops == NULL) { |
Patrick McHardy | 2fe195c | 2008-07-01 19:59:37 -0700 | [diff] [blame] | 385 | err = -EAFNOSUPPORT; |
Thomas Graf | 14c0b97 | 2006-08-04 03:38:38 -0700 | [diff] [blame] | 386 | goto errout; |
| 387 | } |
| 388 | |
Johannes Berg | fceb643 | 2017-04-12 14:34:07 +0200 | [diff] [blame] | 389 | err = nlmsg_parse(nlh, sizeof(*frh), tb, FRA_MAX, ops->policy, NULL); |
Thomas Graf | 14c0b97 | 2006-08-04 03:38:38 -0700 | [diff] [blame] | 390 | if (err < 0) |
| 391 | goto errout; |
| 392 | |
Thomas Graf | e1701c6 | 2007-03-24 12:46:02 -0700 | [diff] [blame] | 393 | err = validate_rulemsg(frh, tb, ops); |
| 394 | if (err < 0) |
| 395 | goto errout; |
| 396 | |
Thomas Graf | 14c0b97 | 2006-08-04 03:38:38 -0700 | [diff] [blame] | 397 | rule = kzalloc(ops->rule_size, GFP_KERNEL); |
| 398 | if (rule == NULL) { |
| 399 | err = -ENOMEM; |
| 400 | goto errout; |
| 401 | } |
Eric W. Biederman | efd7ef1 | 2015-03-11 23:04:08 -0500 | [diff] [blame] | 402 | rule->fr_net = net; |
Thomas Graf | 14c0b97 | 2006-08-04 03:38:38 -0700 | [diff] [blame] | 403 | |
Phil Sutter | f53de1e | 2015-09-09 14:20:56 +0200 | [diff] [blame] | 404 | rule->pref = tb[FRA_PRIORITY] ? nla_get_u32(tb[FRA_PRIORITY]) |
| 405 | : fib_default_rule_pref(ops); |
Thomas Graf | 14c0b97 | 2006-08-04 03:38:38 -0700 | [diff] [blame] | 406 | |
Patrick McHardy | 491deb2 | 2009-12-03 01:25:54 +0000 | [diff] [blame] | 407 | if (tb[FRA_IIFNAME]) { |
Thomas Graf | 14c0b97 | 2006-08-04 03:38:38 -0700 | [diff] [blame] | 408 | struct net_device *dev; |
| 409 | |
Patrick McHardy | 491deb2 | 2009-12-03 01:25:54 +0000 | [diff] [blame] | 410 | rule->iifindex = -1; |
| 411 | nla_strlcpy(rule->iifname, tb[FRA_IIFNAME], IFNAMSIZ); |
| 412 | dev = __dev_get_by_name(net, rule->iifname); |
Thomas Graf | 14c0b97 | 2006-08-04 03:38:38 -0700 | [diff] [blame] | 413 | if (dev) |
Patrick McHardy | 491deb2 | 2009-12-03 01:25:54 +0000 | [diff] [blame] | 414 | rule->iifindex = dev->ifindex; |
Thomas Graf | 14c0b97 | 2006-08-04 03:38:38 -0700 | [diff] [blame] | 415 | } |
| 416 | |
Patrick McHardy | 1b038a5 | 2009-12-03 01:25:56 +0000 | [diff] [blame] | 417 | if (tb[FRA_OIFNAME]) { |
| 418 | struct net_device *dev; |
| 419 | |
| 420 | rule->oifindex = -1; |
| 421 | nla_strlcpy(rule->oifname, tb[FRA_OIFNAME], IFNAMSIZ); |
| 422 | dev = __dev_get_by_name(net, rule->oifname); |
| 423 | if (dev) |
| 424 | rule->oifindex = dev->ifindex; |
| 425 | } |
| 426 | |
Thomas Graf | b8964ed | 2006-11-09 15:22:18 -0800 | [diff] [blame] | 427 | if (tb[FRA_FWMARK]) { |
| 428 | rule->mark = nla_get_u32(tb[FRA_FWMARK]); |
| 429 | if (rule->mark) |
| 430 | /* compatibility: if the mark value is non-zero all bits |
| 431 | * are compared unless a mask is explicitly specified. |
| 432 | */ |
| 433 | rule->mark_mask = 0xFFFFFFFF; |
| 434 | } |
| 435 | |
| 436 | if (tb[FRA_FWMASK]) |
| 437 | rule->mark_mask = nla_get_u32(tb[FRA_FWMASK]); |
| 438 | |
Thomas Graf | e703087 | 2015-07-21 10:44:01 +0200 | [diff] [blame] | 439 | if (tb[FRA_TUN_ID]) |
| 440 | rule->tun_id = nla_get_be64(tb[FRA_TUN_ID]); |
| 441 | |
David Ahern | 96c63fa | 2016-06-08 10:55:39 -0700 | [diff] [blame] | 442 | if (tb[FRA_L3MDEV]) { |
| 443 | #ifdef CONFIG_NET_L3_MASTER_DEV |
| 444 | rule->l3mdev = nla_get_u8(tb[FRA_L3MDEV]); |
| 445 | if (rule->l3mdev != 1) |
| 446 | #endif |
| 447 | goto errout_free; |
| 448 | } |
| 449 | |
Thomas Graf | 14c0b97 | 2006-08-04 03:38:38 -0700 | [diff] [blame] | 450 | rule->action = frh->action; |
| 451 | rule->flags = frh->flags; |
Patrick McHardy | 9e762a4 | 2006-08-10 23:09:48 -0700 | [diff] [blame] | 452 | rule->table = frh_get_table(frh, tb); |
Stefan Tomanek | 73f5698 | 2013-08-03 14:14:43 +0200 | [diff] [blame] | 453 | if (tb[FRA_SUPPRESS_PREFIXLEN]) |
| 454 | rule->suppress_prefixlen = nla_get_u32(tb[FRA_SUPPRESS_PREFIXLEN]); |
| 455 | else |
| 456 | rule->suppress_prefixlen = -1; |
Thomas Graf | 14c0b97 | 2006-08-04 03:38:38 -0700 | [diff] [blame] | 457 | |
Stefan Tomanek | 6ef94cf | 2013-08-02 17:19:56 +0200 | [diff] [blame] | 458 | if (tb[FRA_SUPPRESS_IFGROUP]) |
| 459 | rule->suppress_ifgroup = nla_get_u32(tb[FRA_SUPPRESS_IFGROUP]); |
Stefan Tomanek | 73f5698 | 2013-08-03 14:14:43 +0200 | [diff] [blame] | 460 | else |
| 461 | rule->suppress_ifgroup = -1; |
Stefan Tomanek | 6ef94cf | 2013-08-02 17:19:56 +0200 | [diff] [blame] | 462 | |
Thomas Graf | 0947c9fe | 2007-03-26 17:14:15 -0700 | [diff] [blame] | 463 | err = -EINVAL; |
| 464 | if (tb[FRA_GOTO]) { |
| 465 | if (rule->action != FR_ACT_GOTO) |
| 466 | goto errout_free; |
| 467 | |
| 468 | rule->target = nla_get_u32(tb[FRA_GOTO]); |
| 469 | /* Backward jumps are prohibited to avoid endless loops */ |
| 470 | if (rule->target <= rule->pref) |
| 471 | goto errout_free; |
| 472 | |
Denis V. Lunev | 76c72d4 | 2007-09-16 15:44:27 -0700 | [diff] [blame] | 473 | list_for_each_entry(r, &ops->rules_list, list) { |
Thomas Graf | 0947c9fe | 2007-03-26 17:14:15 -0700 | [diff] [blame] | 474 | if (r->pref == rule->target) { |
Eric Dumazet | 7a2b03c | 2010-10-26 09:24:55 +0000 | [diff] [blame] | 475 | RCU_INIT_POINTER(rule->ctarget, r); |
Thomas Graf | 0947c9fe | 2007-03-26 17:14:15 -0700 | [diff] [blame] | 476 | break; |
| 477 | } |
| 478 | } |
| 479 | |
Eric Dumazet | 7a2b03c | 2010-10-26 09:24:55 +0000 | [diff] [blame] | 480 | if (rcu_dereference_protected(rule->ctarget, 1) == NULL) |
Thomas Graf | 0947c9fe | 2007-03-26 17:14:15 -0700 | [diff] [blame] | 481 | unresolved = 1; |
| 482 | } else if (rule->action == FR_ACT_GOTO) |
| 483 | goto errout_free; |
| 484 | |
David Ahern | 96c63fa | 2016-06-08 10:55:39 -0700 | [diff] [blame] | 485 | if (rule->l3mdev && rule->table) |
| 486 | goto errout_free; |
| 487 | |
Lorenzo Colitti | 622ec2c | 2016-11-04 02:23:42 +0900 | [diff] [blame] | 488 | if (tb[FRA_UID_RANGE]) { |
| 489 | if (current_user_ns() != net->user_ns) { |
| 490 | err = -EPERM; |
| 491 | goto errout_free; |
| 492 | } |
| 493 | |
| 494 | rule->uid_range = nla_get_kuid_range(tb); |
| 495 | |
| 496 | if (!uid_range_set(&rule->uid_range) || |
| 497 | !uid_lte(rule->uid_range.start, rule->uid_range.end)) |
| 498 | goto errout_free; |
| 499 | } else { |
| 500 | rule->uid_range = fib_kuid_range_unset; |
| 501 | } |
| 502 | |
Mateusz Bajorski | 153380e | 2016-06-29 09:22:10 +0200 | [diff] [blame] | 503 | if ((nlh->nlmsg_flags & NLM_F_EXCL) && |
| 504 | rule_exists(ops, frh, tb, rule)) { |
| 505 | err = -EEXIST; |
| 506 | goto errout_free; |
| 507 | } |
| 508 | |
Rami Rosen | 8b3521e | 2009-05-11 05:52:49 +0000 | [diff] [blame] | 509 | err = ops->configure(rule, skb, frh, tb); |
Thomas Graf | 14c0b97 | 2006-08-04 03:38:38 -0700 | [diff] [blame] | 510 | if (err < 0) |
| 511 | goto errout_free; |
| 512 | |
Denis V. Lunev | 76c72d4 | 2007-09-16 15:44:27 -0700 | [diff] [blame] | 513 | list_for_each_entry(r, &ops->rules_list, list) { |
Thomas Graf | 14c0b97 | 2006-08-04 03:38:38 -0700 | [diff] [blame] | 514 | if (r->pref > rule->pref) |
| 515 | break; |
| 516 | last = r; |
| 517 | } |
| 518 | |
| 519 | fib_rule_get(rule); |
| 520 | |
Eric Dumazet | ebb9fed | 2010-10-23 09:44:25 +0000 | [diff] [blame] | 521 | if (last) |
| 522 | list_add_rcu(&rule->list, &last->list); |
| 523 | else |
| 524 | list_add_rcu(&rule->list, &ops->rules_list); |
| 525 | |
Thomas Graf | 0947c9fe | 2007-03-26 17:14:15 -0700 | [diff] [blame] | 526 | if (ops->unresolved_rules) { |
| 527 | /* |
| 528 | * There are unresolved goto rules in the list, check if |
| 529 | * any of them are pointing to this new rule. |
| 530 | */ |
Denis V. Lunev | 76c72d4 | 2007-09-16 15:44:27 -0700 | [diff] [blame] | 531 | list_for_each_entry(r, &ops->rules_list, list) { |
Thomas Graf | 0947c9fe | 2007-03-26 17:14:15 -0700 | [diff] [blame] | 532 | if (r->action == FR_ACT_GOTO && |
Gao feng | 561dac2 | 2011-09-11 15:36:05 +0000 | [diff] [blame] | 533 | r->target == rule->pref && |
| 534 | rtnl_dereference(r->ctarget) == NULL) { |
Thomas Graf | 0947c9fe | 2007-03-26 17:14:15 -0700 | [diff] [blame] | 535 | rcu_assign_pointer(r->ctarget, rule); |
| 536 | if (--ops->unresolved_rules == 0) |
| 537 | break; |
| 538 | } |
| 539 | } |
| 540 | } |
| 541 | |
| 542 | if (rule->action == FR_ACT_GOTO) |
| 543 | ops->nr_goto_rules++; |
| 544 | |
| 545 | if (unresolved) |
| 546 | ops->unresolved_rules++; |
| 547 | |
Thomas Graf | e703087 | 2015-07-21 10:44:01 +0200 | [diff] [blame] | 548 | if (rule->tun_id) |
| 549 | ip_tunnel_need_metadata(); |
| 550 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 551 | notify_rule_change(RTM_NEWRULE, rule, ops, nlh, NETLINK_CB(skb).portid); |
Thomas Graf | 73417f6 | 2007-03-27 13:56:52 -0700 | [diff] [blame] | 552 | flush_route_cache(ops); |
Thomas Graf | 14c0b97 | 2006-08-04 03:38:38 -0700 | [diff] [blame] | 553 | rules_ops_put(ops); |
| 554 | return 0; |
| 555 | |
| 556 | errout_free: |
| 557 | kfree(rule); |
| 558 | errout: |
| 559 | rules_ops_put(ops); |
| 560 | return err; |
| 561 | } |
David Ahern | 96c63fa | 2016-06-08 10:55:39 -0700 | [diff] [blame] | 562 | EXPORT_SYMBOL_GPL(fib_nl_newrule); |
Thomas Graf | 14c0b97 | 2006-08-04 03:38:38 -0700 | [diff] [blame] | 563 | |
David Ahern | 96c63fa | 2016-06-08 10:55:39 -0700 | [diff] [blame] | 564 | int fib_nl_delrule(struct sk_buff *skb, struct nlmsghdr *nlh) |
Thomas Graf | 14c0b97 | 2006-08-04 03:38:38 -0700 | [diff] [blame] | 565 | { |
YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 566 | struct net *net = sock_net(skb->sk); |
Thomas Graf | 14c0b97 | 2006-08-04 03:38:38 -0700 | [diff] [blame] | 567 | struct fib_rule_hdr *frh = nlmsg_data(nlh); |
| 568 | struct fib_rules_ops *ops = NULL; |
Thomas Graf | 0947c9fe | 2007-03-26 17:14:15 -0700 | [diff] [blame] | 569 | struct fib_rule *rule, *tmp; |
Thomas Graf | 14c0b97 | 2006-08-04 03:38:38 -0700 | [diff] [blame] | 570 | struct nlattr *tb[FRA_MAX+1]; |
Lorenzo Colitti | 622ec2c | 2016-11-04 02:23:42 +0900 | [diff] [blame] | 571 | struct fib_kuid_range range; |
Thomas Graf | 14c0b97 | 2006-08-04 03:38:38 -0700 | [diff] [blame] | 572 | int err = -EINVAL; |
| 573 | |
| 574 | if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*frh))) |
| 575 | goto errout; |
| 576 | |
Denis V. Lunev | 5fd30ee | 2008-01-10 03:20:28 -0800 | [diff] [blame] | 577 | ops = lookup_rules_ops(net, frh->family); |
Thomas Graf | 14c0b97 | 2006-08-04 03:38:38 -0700 | [diff] [blame] | 578 | if (ops == NULL) { |
Patrick McHardy | 2fe195c | 2008-07-01 19:59:37 -0700 | [diff] [blame] | 579 | err = -EAFNOSUPPORT; |
Thomas Graf | 14c0b97 | 2006-08-04 03:38:38 -0700 | [diff] [blame] | 580 | goto errout; |
| 581 | } |
| 582 | |
Johannes Berg | fceb643 | 2017-04-12 14:34:07 +0200 | [diff] [blame] | 583 | err = nlmsg_parse(nlh, sizeof(*frh), tb, FRA_MAX, ops->policy, NULL); |
Thomas Graf | 14c0b97 | 2006-08-04 03:38:38 -0700 | [diff] [blame] | 584 | if (err < 0) |
| 585 | goto errout; |
| 586 | |
Thomas Graf | e1701c6 | 2007-03-24 12:46:02 -0700 | [diff] [blame] | 587 | err = validate_rulemsg(frh, tb, ops); |
| 588 | if (err < 0) |
| 589 | goto errout; |
| 590 | |
Lorenzo Colitti | 622ec2c | 2016-11-04 02:23:42 +0900 | [diff] [blame] | 591 | if (tb[FRA_UID_RANGE]) { |
| 592 | range = nla_get_kuid_range(tb); |
| 593 | if (!uid_range_set(&range)) |
| 594 | goto errout; |
| 595 | } else { |
| 596 | range = fib_kuid_range_unset; |
| 597 | } |
| 598 | |
Denis V. Lunev | 76c72d4 | 2007-09-16 15:44:27 -0700 | [diff] [blame] | 599 | list_for_each_entry(rule, &ops->rules_list, list) { |
Thomas Graf | 14c0b97 | 2006-08-04 03:38:38 -0700 | [diff] [blame] | 600 | if (frh->action && (frh->action != rule->action)) |
| 601 | continue; |
| 602 | |
Andreas Henriksson | 13eb2ab | 2013-11-07 18:26:38 +0100 | [diff] [blame] | 603 | if (frh_get_table(frh, tb) && |
| 604 | (frh_get_table(frh, tb) != rule->table)) |
Thomas Graf | 14c0b97 | 2006-08-04 03:38:38 -0700 | [diff] [blame] | 605 | continue; |
| 606 | |
| 607 | if (tb[FRA_PRIORITY] && |
| 608 | (rule->pref != nla_get_u32(tb[FRA_PRIORITY]))) |
| 609 | continue; |
| 610 | |
Patrick McHardy | 491deb2 | 2009-12-03 01:25:54 +0000 | [diff] [blame] | 611 | if (tb[FRA_IIFNAME] && |
| 612 | nla_strcmp(tb[FRA_IIFNAME], rule->iifname)) |
Thomas Graf | 14c0b97 | 2006-08-04 03:38:38 -0700 | [diff] [blame] | 613 | continue; |
| 614 | |
Patrick McHardy | 1b038a5 | 2009-12-03 01:25:56 +0000 | [diff] [blame] | 615 | if (tb[FRA_OIFNAME] && |
| 616 | nla_strcmp(tb[FRA_OIFNAME], rule->oifname)) |
| 617 | continue; |
| 618 | |
Thomas Graf | b8964ed | 2006-11-09 15:22:18 -0800 | [diff] [blame] | 619 | if (tb[FRA_FWMARK] && |
| 620 | (rule->mark != nla_get_u32(tb[FRA_FWMARK]))) |
| 621 | continue; |
| 622 | |
| 623 | if (tb[FRA_FWMASK] && |
| 624 | (rule->mark_mask != nla_get_u32(tb[FRA_FWMASK]))) |
| 625 | continue; |
| 626 | |
Thomas Graf | e703087 | 2015-07-21 10:44:01 +0200 | [diff] [blame] | 627 | if (tb[FRA_TUN_ID] && |
| 628 | (rule->tun_id != nla_get_be64(tb[FRA_TUN_ID]))) |
| 629 | continue; |
| 630 | |
David Ahern | 96c63fa | 2016-06-08 10:55:39 -0700 | [diff] [blame] | 631 | if (tb[FRA_L3MDEV] && |
| 632 | (rule->l3mdev != nla_get_u8(tb[FRA_L3MDEV]))) |
| 633 | continue; |
| 634 | |
Lorenzo Colitti | 622ec2c | 2016-11-04 02:23:42 +0900 | [diff] [blame] | 635 | if (uid_range_set(&range) && |
| 636 | (!uid_eq(rule->uid_range.start, range.start) || |
| 637 | !uid_eq(rule->uid_range.end, range.end))) |
| 638 | continue; |
| 639 | |
Thomas Graf | 14c0b97 | 2006-08-04 03:38:38 -0700 | [diff] [blame] | 640 | if (!ops->compare(rule, frh, tb)) |
| 641 | continue; |
| 642 | |
| 643 | if (rule->flags & FIB_RULE_PERMANENT) { |
| 644 | err = -EPERM; |
| 645 | goto errout; |
| 646 | } |
| 647 | |
Alexander Duyck | 0ddcf43 | 2015-03-06 13:47:00 -0800 | [diff] [blame] | 648 | if (ops->delete) { |
| 649 | err = ops->delete(rule); |
| 650 | if (err) |
| 651 | goto errout; |
| 652 | } |
| 653 | |
Thomas Graf | e703087 | 2015-07-21 10:44:01 +0200 | [diff] [blame] | 654 | if (rule->tun_id) |
| 655 | ip_tunnel_unneed_metadata(); |
| 656 | |
Thomas Graf | 14c0b97 | 2006-08-04 03:38:38 -0700 | [diff] [blame] | 657 | list_del_rcu(&rule->list); |
Thomas Graf | 0947c9fe | 2007-03-26 17:14:15 -0700 | [diff] [blame] | 658 | |
Yan, Zheng | afaef73 | 2011-10-17 15:20:28 +0000 | [diff] [blame] | 659 | if (rule->action == FR_ACT_GOTO) { |
Thomas Graf | 0947c9fe | 2007-03-26 17:14:15 -0700 | [diff] [blame] | 660 | ops->nr_goto_rules--; |
Yan, Zheng | afaef73 | 2011-10-17 15:20:28 +0000 | [diff] [blame] | 661 | if (rtnl_dereference(rule->ctarget) == NULL) |
| 662 | ops->unresolved_rules--; |
| 663 | } |
Thomas Graf | 0947c9fe | 2007-03-26 17:14:15 -0700 | [diff] [blame] | 664 | |
| 665 | /* |
| 666 | * Check if this rule is a target to any of them. If so, |
| 667 | * disable them. As this operation is eventually very |
| 668 | * expensive, it is only performed if goto rules have |
| 669 | * actually been added. |
| 670 | */ |
| 671 | if (ops->nr_goto_rules > 0) { |
Denis V. Lunev | 76c72d4 | 2007-09-16 15:44:27 -0700 | [diff] [blame] | 672 | list_for_each_entry(tmp, &ops->rules_list, list) { |
Eric Dumazet | 7a2b03c | 2010-10-26 09:24:55 +0000 | [diff] [blame] | 673 | if (rtnl_dereference(tmp->ctarget) == rule) { |
Stephen Hemminger | a9b3cd7 | 2011-08-01 16:19:00 +0000 | [diff] [blame] | 674 | RCU_INIT_POINTER(tmp->ctarget, NULL); |
Thomas Graf | 0947c9fe | 2007-03-26 17:14:15 -0700 | [diff] [blame] | 675 | ops->unresolved_rules++; |
| 676 | } |
| 677 | } |
| 678 | } |
| 679 | |
Denis V. Lunev | 9e3a548 | 2008-01-20 16:46:41 -0800 | [diff] [blame] | 680 | notify_rule_change(RTM_DELRULE, rule, ops, nlh, |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 681 | NETLINK_CB(skb).portid); |
Thomas Graf | 14c0b97 | 2006-08-04 03:38:38 -0700 | [diff] [blame] | 682 | fib_rule_put(rule); |
Thomas Graf | 73417f6 | 2007-03-27 13:56:52 -0700 | [diff] [blame] | 683 | flush_route_cache(ops); |
Thomas Graf | 14c0b97 | 2006-08-04 03:38:38 -0700 | [diff] [blame] | 684 | rules_ops_put(ops); |
| 685 | return 0; |
| 686 | } |
| 687 | |
| 688 | err = -ENOENT; |
| 689 | errout: |
| 690 | rules_ops_put(ops); |
| 691 | return err; |
| 692 | } |
David Ahern | 96c63fa | 2016-06-08 10:55:39 -0700 | [diff] [blame] | 693 | EXPORT_SYMBOL_GPL(fib_nl_delrule); |
Thomas Graf | 14c0b97 | 2006-08-04 03:38:38 -0700 | [diff] [blame] | 694 | |
Thomas Graf | 339bf98 | 2006-11-10 14:10:15 -0800 | [diff] [blame] | 695 | static inline size_t fib_rule_nlmsg_size(struct fib_rules_ops *ops, |
| 696 | struct fib_rule *rule) |
| 697 | { |
| 698 | size_t payload = NLMSG_ALIGN(sizeof(struct fib_rule_hdr)) |
Patrick McHardy | 491deb2 | 2009-12-03 01:25:54 +0000 | [diff] [blame] | 699 | + nla_total_size(IFNAMSIZ) /* FRA_IIFNAME */ |
Patrick McHardy | 1b038a5 | 2009-12-03 01:25:56 +0000 | [diff] [blame] | 700 | + nla_total_size(IFNAMSIZ) /* FRA_OIFNAME */ |
Thomas Graf | 339bf98 | 2006-11-10 14:10:15 -0800 | [diff] [blame] | 701 | + nla_total_size(4) /* FRA_PRIORITY */ |
| 702 | + nla_total_size(4) /* FRA_TABLE */ |
Stefan Tomanek | 73f5698 | 2013-08-03 14:14:43 +0200 | [diff] [blame] | 703 | + nla_total_size(4) /* FRA_SUPPRESS_PREFIXLEN */ |
Stefan Tomanek | 6ef94cf | 2013-08-02 17:19:56 +0200 | [diff] [blame] | 704 | + nla_total_size(4) /* FRA_SUPPRESS_IFGROUP */ |
Thomas Graf | 339bf98 | 2006-11-10 14:10:15 -0800 | [diff] [blame] | 705 | + nla_total_size(4) /* FRA_FWMARK */ |
Thomas Graf | e703087 | 2015-07-21 10:44:01 +0200 | [diff] [blame] | 706 | + nla_total_size(4) /* FRA_FWMASK */ |
Lorenzo Colitti | 622ec2c | 2016-11-04 02:23:42 +0900 | [diff] [blame] | 707 | + nla_total_size_64bit(8) /* FRA_TUN_ID */ |
| 708 | + nla_total_size(sizeof(struct fib_kuid_range)); |
Thomas Graf | 339bf98 | 2006-11-10 14:10:15 -0800 | [diff] [blame] | 709 | |
| 710 | if (ops->nlmsg_payload) |
| 711 | payload += ops->nlmsg_payload(rule); |
| 712 | |
| 713 | return payload; |
| 714 | } |
| 715 | |
Thomas Graf | 14c0b97 | 2006-08-04 03:38:38 -0700 | [diff] [blame] | 716 | static int fib_nl_fill_rule(struct sk_buff *skb, struct fib_rule *rule, |
| 717 | u32 pid, u32 seq, int type, int flags, |
| 718 | struct fib_rules_ops *ops) |
| 719 | { |
| 720 | struct nlmsghdr *nlh; |
| 721 | struct fib_rule_hdr *frh; |
| 722 | |
| 723 | nlh = nlmsg_put(skb, pid, seq, type, sizeof(*frh), flags); |
| 724 | if (nlh == NULL) |
Patrick McHardy | 2693256 | 2007-01-31 23:16:40 -0800 | [diff] [blame] | 725 | return -EMSGSIZE; |
Thomas Graf | 14c0b97 | 2006-08-04 03:38:38 -0700 | [diff] [blame] | 726 | |
| 727 | frh = nlmsg_data(nlh); |
Patrick McHardy | 28bb172 | 2010-04-13 05:03:16 +0000 | [diff] [blame] | 728 | frh->family = ops->family; |
Thomas Graf | 14c0b97 | 2006-08-04 03:38:38 -0700 | [diff] [blame] | 729 | frh->table = rule->table; |
David S. Miller | 0e3cea7 | 2012-04-01 20:47:01 -0400 | [diff] [blame] | 730 | if (nla_put_u32(skb, FRA_TABLE, rule->table)) |
| 731 | goto nla_put_failure; |
Stefan Tomanek | 73f5698 | 2013-08-03 14:14:43 +0200 | [diff] [blame] | 732 | if (nla_put_u32(skb, FRA_SUPPRESS_PREFIXLEN, rule->suppress_prefixlen)) |
Stefan Tomanek | 7764a45 | 2013-08-01 02:17:15 +0200 | [diff] [blame] | 733 | goto nla_put_failure; |
Thomas Graf | 14c0b97 | 2006-08-04 03:38:38 -0700 | [diff] [blame] | 734 | frh->res1 = 0; |
| 735 | frh->res2 = 0; |
| 736 | frh->action = rule->action; |
| 737 | frh->flags = rule->flags; |
| 738 | |
Eric Dumazet | 7a2b03c | 2010-10-26 09:24:55 +0000 | [diff] [blame] | 739 | if (rule->action == FR_ACT_GOTO && |
Eric Dumazet | 33d480c | 2011-08-11 19:30:52 +0000 | [diff] [blame] | 740 | rcu_access_pointer(rule->ctarget) == NULL) |
Thomas Graf | 0947c9fe | 2007-03-26 17:14:15 -0700 | [diff] [blame] | 741 | frh->flags |= FIB_RULE_UNRESOLVED; |
| 742 | |
Patrick McHardy | 491deb2 | 2009-12-03 01:25:54 +0000 | [diff] [blame] | 743 | if (rule->iifname[0]) { |
David S. Miller | 0e3cea7 | 2012-04-01 20:47:01 -0400 | [diff] [blame] | 744 | if (nla_put_string(skb, FRA_IIFNAME, rule->iifname)) |
| 745 | goto nla_put_failure; |
Patrick McHardy | 491deb2 | 2009-12-03 01:25:54 +0000 | [diff] [blame] | 746 | if (rule->iifindex == -1) |
| 747 | frh->flags |= FIB_RULE_IIF_DETACHED; |
Thomas Graf | 2b44368 | 2007-03-26 17:37:59 -0700 | [diff] [blame] | 748 | } |
| 749 | |
Patrick McHardy | 1b038a5 | 2009-12-03 01:25:56 +0000 | [diff] [blame] | 750 | if (rule->oifname[0]) { |
David S. Miller | 0e3cea7 | 2012-04-01 20:47:01 -0400 | [diff] [blame] | 751 | if (nla_put_string(skb, FRA_OIFNAME, rule->oifname)) |
| 752 | goto nla_put_failure; |
Patrick McHardy | 1b038a5 | 2009-12-03 01:25:56 +0000 | [diff] [blame] | 753 | if (rule->oifindex == -1) |
| 754 | frh->flags |= FIB_RULE_OIF_DETACHED; |
| 755 | } |
| 756 | |
David S. Miller | 0e3cea7 | 2012-04-01 20:47:01 -0400 | [diff] [blame] | 757 | if ((rule->pref && |
| 758 | nla_put_u32(skb, FRA_PRIORITY, rule->pref)) || |
| 759 | (rule->mark && |
| 760 | nla_put_u32(skb, FRA_FWMARK, rule->mark)) || |
| 761 | ((rule->mark_mask || rule->mark) && |
| 762 | nla_put_u32(skb, FRA_FWMASK, rule->mark_mask)) || |
| 763 | (rule->target && |
Thomas Graf | e703087 | 2015-07-21 10:44:01 +0200 | [diff] [blame] | 764 | nla_put_u32(skb, FRA_GOTO, rule->target)) || |
| 765 | (rule->tun_id && |
David Ahern | 96c63fa | 2016-06-08 10:55:39 -0700 | [diff] [blame] | 766 | nla_put_be64(skb, FRA_TUN_ID, rule->tun_id, FRA_PAD)) || |
| 767 | (rule->l3mdev && |
Lorenzo Colitti | 622ec2c | 2016-11-04 02:23:42 +0900 | [diff] [blame] | 768 | nla_put_u8(skb, FRA_L3MDEV, rule->l3mdev)) || |
| 769 | (uid_range_set(&rule->uid_range) && |
| 770 | nla_put_uid_range(skb, &rule->uid_range))) |
David S. Miller | 0e3cea7 | 2012-04-01 20:47:01 -0400 | [diff] [blame] | 771 | goto nla_put_failure; |
Stefan Tomanek | 6ef94cf | 2013-08-02 17:19:56 +0200 | [diff] [blame] | 772 | |
| 773 | if (rule->suppress_ifgroup != -1) { |
| 774 | if (nla_put_u32(skb, FRA_SUPPRESS_IFGROUP, rule->suppress_ifgroup)) |
| 775 | goto nla_put_failure; |
| 776 | } |
| 777 | |
Rami Rosen | 04af8cf | 2009-05-20 17:26:23 -0700 | [diff] [blame] | 778 | if (ops->fill(rule, skb, frh) < 0) |
Thomas Graf | 14c0b97 | 2006-08-04 03:38:38 -0700 | [diff] [blame] | 779 | goto nla_put_failure; |
| 780 | |
Johannes Berg | 053c095 | 2015-01-16 22:09:00 +0100 | [diff] [blame] | 781 | nlmsg_end(skb, nlh); |
| 782 | return 0; |
Thomas Graf | 14c0b97 | 2006-08-04 03:38:38 -0700 | [diff] [blame] | 783 | |
| 784 | nla_put_failure: |
Patrick McHardy | 2693256 | 2007-01-31 23:16:40 -0800 | [diff] [blame] | 785 | nlmsg_cancel(skb, nlh); |
| 786 | return -EMSGSIZE; |
Thomas Graf | 14c0b97 | 2006-08-04 03:38:38 -0700 | [diff] [blame] | 787 | } |
| 788 | |
Thomas Graf | c454673 | 2007-03-25 23:24:24 -0700 | [diff] [blame] | 789 | static int dump_rules(struct sk_buff *skb, struct netlink_callback *cb, |
| 790 | struct fib_rules_ops *ops) |
Thomas Graf | 14c0b97 | 2006-08-04 03:38:38 -0700 | [diff] [blame] | 791 | { |
| 792 | int idx = 0; |
| 793 | struct fib_rule *rule; |
Wilson Kok | 41fc014 | 2015-09-22 21:40:22 -0700 | [diff] [blame] | 794 | int err = 0; |
Thomas Graf | 14c0b97 | 2006-08-04 03:38:38 -0700 | [diff] [blame] | 795 | |
Eric Dumazet | e67f88d | 2011-04-27 22:56:07 +0000 | [diff] [blame] | 796 | rcu_read_lock(); |
| 797 | list_for_each_entry_rcu(rule, &ops->rules_list, list) { |
Thomas Graf | c454673 | 2007-03-25 23:24:24 -0700 | [diff] [blame] | 798 | if (idx < cb->args[1]) |
Thomas Graf | 14c0b97 | 2006-08-04 03:38:38 -0700 | [diff] [blame] | 799 | goto skip; |
| 800 | |
Wilson Kok | 41fc014 | 2015-09-22 21:40:22 -0700 | [diff] [blame] | 801 | err = fib_nl_fill_rule(skb, rule, NETLINK_CB(cb->skb).portid, |
| 802 | cb->nlh->nlmsg_seq, RTM_NEWRULE, |
| 803 | NLM_F_MULTI, ops); |
| 804 | if (err) |
Thomas Graf | 14c0b97 | 2006-08-04 03:38:38 -0700 | [diff] [blame] | 805 | break; |
| 806 | skip: |
| 807 | idx++; |
| 808 | } |
Eric Dumazet | 2907c35 | 2011-05-25 07:34:04 +0000 | [diff] [blame] | 809 | rcu_read_unlock(); |
Thomas Graf | c454673 | 2007-03-25 23:24:24 -0700 | [diff] [blame] | 810 | cb->args[1] = idx; |
Thomas Graf | 14c0b97 | 2006-08-04 03:38:38 -0700 | [diff] [blame] | 811 | rules_ops_put(ops); |
| 812 | |
Wilson Kok | 41fc014 | 2015-09-22 21:40:22 -0700 | [diff] [blame] | 813 | return err; |
Thomas Graf | 14c0b97 | 2006-08-04 03:38:38 -0700 | [diff] [blame] | 814 | } |
| 815 | |
Thomas Graf | c454673 | 2007-03-25 23:24:24 -0700 | [diff] [blame] | 816 | static int fib_nl_dumprule(struct sk_buff *skb, struct netlink_callback *cb) |
| 817 | { |
YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 818 | struct net *net = sock_net(skb->sk); |
Thomas Graf | c454673 | 2007-03-25 23:24:24 -0700 | [diff] [blame] | 819 | struct fib_rules_ops *ops; |
| 820 | int idx = 0, family; |
| 821 | |
| 822 | family = rtnl_msg_family(cb->nlh); |
| 823 | if (family != AF_UNSPEC) { |
| 824 | /* Protocol specific dump request */ |
Denis V. Lunev | 5fd30ee | 2008-01-10 03:20:28 -0800 | [diff] [blame] | 825 | ops = lookup_rules_ops(net, family); |
Thomas Graf | c454673 | 2007-03-25 23:24:24 -0700 | [diff] [blame] | 826 | if (ops == NULL) |
| 827 | return -EAFNOSUPPORT; |
| 828 | |
Wilson Kok | 41fc014 | 2015-09-22 21:40:22 -0700 | [diff] [blame] | 829 | dump_rules(skb, cb, ops); |
| 830 | |
| 831 | return skb->len; |
Thomas Graf | c454673 | 2007-03-25 23:24:24 -0700 | [diff] [blame] | 832 | } |
| 833 | |
| 834 | rcu_read_lock(); |
Denis V. Lunev | 5fd30ee | 2008-01-10 03:20:28 -0800 | [diff] [blame] | 835 | list_for_each_entry_rcu(ops, &net->rules_ops, list) { |
Thomas Graf | c454673 | 2007-03-25 23:24:24 -0700 | [diff] [blame] | 836 | if (idx < cb->args[0] || !try_module_get(ops->owner)) |
| 837 | goto skip; |
| 838 | |
| 839 | if (dump_rules(skb, cb, ops) < 0) |
| 840 | break; |
| 841 | |
| 842 | cb->args[1] = 0; |
Eric Dumazet | 2fb3573 | 2010-03-09 20:03:38 +0000 | [diff] [blame] | 843 | skip: |
Thomas Graf | c454673 | 2007-03-25 23:24:24 -0700 | [diff] [blame] | 844 | idx++; |
| 845 | } |
| 846 | rcu_read_unlock(); |
| 847 | cb->args[0] = idx; |
| 848 | |
| 849 | return skb->len; |
| 850 | } |
Thomas Graf | 14c0b97 | 2006-08-04 03:38:38 -0700 | [diff] [blame] | 851 | |
Denis V. Lunev | 9e3a548 | 2008-01-20 16:46:41 -0800 | [diff] [blame] | 852 | static void notify_rule_change(int event, struct fib_rule *rule, |
Thomas Graf | c17084d | 2006-08-15 00:32:48 -0700 | [diff] [blame] | 853 | struct fib_rules_ops *ops, struct nlmsghdr *nlh, |
| 854 | u32 pid) |
Thomas Graf | 14c0b97 | 2006-08-04 03:38:38 -0700 | [diff] [blame] | 855 | { |
Denis V. Lunev | 9e3a548 | 2008-01-20 16:46:41 -0800 | [diff] [blame] | 856 | struct net *net; |
Thomas Graf | c17084d | 2006-08-15 00:32:48 -0700 | [diff] [blame] | 857 | struct sk_buff *skb; |
| 858 | int err = -ENOBUFS; |
Thomas Graf | 14c0b97 | 2006-08-04 03:38:38 -0700 | [diff] [blame] | 859 | |
Denis V. Lunev | 9e3a548 | 2008-01-20 16:46:41 -0800 | [diff] [blame] | 860 | net = ops->fro_net; |
Thomas Graf | 339bf98 | 2006-11-10 14:10:15 -0800 | [diff] [blame] | 861 | skb = nlmsg_new(fib_rule_nlmsg_size(ops, rule), GFP_KERNEL); |
Thomas Graf | 14c0b97 | 2006-08-04 03:38:38 -0700 | [diff] [blame] | 862 | if (skb == NULL) |
Thomas Graf | c17084d | 2006-08-15 00:32:48 -0700 | [diff] [blame] | 863 | goto errout; |
| 864 | |
| 865 | err = fib_nl_fill_rule(skb, rule, pid, nlh->nlmsg_seq, event, 0, ops); |
Patrick McHardy | 2693256 | 2007-01-31 23:16:40 -0800 | [diff] [blame] | 866 | if (err < 0) { |
| 867 | /* -EMSGSIZE implies BUG in fib_rule_nlmsg_size() */ |
| 868 | WARN_ON(err == -EMSGSIZE); |
| 869 | kfree_skb(skb); |
| 870 | goto errout; |
| 871 | } |
Denis V. Lunev | 9e3a548 | 2008-01-20 16:46:41 -0800 | [diff] [blame] | 872 | |
Pablo Neira Ayuso | 1ce85fe | 2009-02-24 23:18:28 -0800 | [diff] [blame] | 873 | rtnl_notify(skb, net, pid, ops->nlgroup, nlh, GFP_KERNEL); |
| 874 | return; |
Thomas Graf | c17084d | 2006-08-15 00:32:48 -0700 | [diff] [blame] | 875 | errout: |
| 876 | if (err < 0) |
Denis V. Lunev | 5fd30ee | 2008-01-10 03:20:28 -0800 | [diff] [blame] | 877 | rtnl_set_sk_err(net, ops->nlgroup, err); |
Thomas Graf | 14c0b97 | 2006-08-04 03:38:38 -0700 | [diff] [blame] | 878 | } |
| 879 | |
| 880 | static void attach_rules(struct list_head *rules, struct net_device *dev) |
| 881 | { |
| 882 | struct fib_rule *rule; |
| 883 | |
| 884 | list_for_each_entry(rule, rules, list) { |
Patrick McHardy | 491deb2 | 2009-12-03 01:25:54 +0000 | [diff] [blame] | 885 | if (rule->iifindex == -1 && |
| 886 | strcmp(dev->name, rule->iifname) == 0) |
| 887 | rule->iifindex = dev->ifindex; |
Patrick McHardy | 1b038a5 | 2009-12-03 01:25:56 +0000 | [diff] [blame] | 888 | if (rule->oifindex == -1 && |
| 889 | strcmp(dev->name, rule->oifname) == 0) |
| 890 | rule->oifindex = dev->ifindex; |
Thomas Graf | 14c0b97 | 2006-08-04 03:38:38 -0700 | [diff] [blame] | 891 | } |
| 892 | } |
| 893 | |
| 894 | static void detach_rules(struct list_head *rules, struct net_device *dev) |
| 895 | { |
| 896 | struct fib_rule *rule; |
| 897 | |
Patrick McHardy | 1b038a5 | 2009-12-03 01:25:56 +0000 | [diff] [blame] | 898 | list_for_each_entry(rule, rules, list) { |
Patrick McHardy | 491deb2 | 2009-12-03 01:25:54 +0000 | [diff] [blame] | 899 | if (rule->iifindex == dev->ifindex) |
| 900 | rule->iifindex = -1; |
Patrick McHardy | 1b038a5 | 2009-12-03 01:25:56 +0000 | [diff] [blame] | 901 | if (rule->oifindex == dev->ifindex) |
| 902 | rule->oifindex = -1; |
| 903 | } |
Thomas Graf | 14c0b97 | 2006-08-04 03:38:38 -0700 | [diff] [blame] | 904 | } |
| 905 | |
| 906 | |
| 907 | static int fib_rules_event(struct notifier_block *this, unsigned long event, |
Jiri Pirko | 351638e | 2013-05-28 01:30:21 +0000 | [diff] [blame] | 908 | void *ptr) |
Thomas Graf | 14c0b97 | 2006-08-04 03:38:38 -0700 | [diff] [blame] | 909 | { |
Jiri Pirko | 351638e | 2013-05-28 01:30:21 +0000 | [diff] [blame] | 910 | struct net_device *dev = netdev_notifier_info_to_dev(ptr); |
YOSHIFUJI Hideaki | c346dca | 2008-03-25 21:47:49 +0900 | [diff] [blame] | 911 | struct net *net = dev_net(dev); |
Thomas Graf | 14c0b97 | 2006-08-04 03:38:38 -0700 | [diff] [blame] | 912 | struct fib_rules_ops *ops; |
| 913 | |
Eric Dumazet | 748e2d9 | 2012-08-22 21:50:59 +0000 | [diff] [blame] | 914 | ASSERT_RTNL(); |
Thomas Graf | 14c0b97 | 2006-08-04 03:38:38 -0700 | [diff] [blame] | 915 | |
| 916 | switch (event) { |
| 917 | case NETDEV_REGISTER: |
Denis V. Lunev | 5fd30ee | 2008-01-10 03:20:28 -0800 | [diff] [blame] | 918 | list_for_each_entry(ops, &net->rules_ops, list) |
Denis V. Lunev | 76c72d4 | 2007-09-16 15:44:27 -0700 | [diff] [blame] | 919 | attach_rules(&ops->rules_list, dev); |
Thomas Graf | 14c0b97 | 2006-08-04 03:38:38 -0700 | [diff] [blame] | 920 | break; |
| 921 | |
Maciej Żenczykowski | 946c032 | 2014-02-07 16:23:48 -0800 | [diff] [blame] | 922 | case NETDEV_CHANGENAME: |
| 923 | list_for_each_entry(ops, &net->rules_ops, list) { |
| 924 | detach_rules(&ops->rules_list, dev); |
| 925 | attach_rules(&ops->rules_list, dev); |
| 926 | } |
| 927 | break; |
| 928 | |
Thomas Graf | 14c0b97 | 2006-08-04 03:38:38 -0700 | [diff] [blame] | 929 | case NETDEV_UNREGISTER: |
Denis V. Lunev | 5fd30ee | 2008-01-10 03:20:28 -0800 | [diff] [blame] | 930 | list_for_each_entry(ops, &net->rules_ops, list) |
Denis V. Lunev | 76c72d4 | 2007-09-16 15:44:27 -0700 | [diff] [blame] | 931 | detach_rules(&ops->rules_list, dev); |
Thomas Graf | 14c0b97 | 2006-08-04 03:38:38 -0700 | [diff] [blame] | 932 | break; |
| 933 | } |
| 934 | |
Thomas Graf | 14c0b97 | 2006-08-04 03:38:38 -0700 | [diff] [blame] | 935 | return NOTIFY_DONE; |
| 936 | } |
| 937 | |
| 938 | static struct notifier_block fib_rules_notifier = { |
| 939 | .notifier_call = fib_rules_event, |
| 940 | }; |
| 941 | |
Alexey Dobriyan | 2c8c1e7 | 2010-01-17 03:35:32 +0000 | [diff] [blame] | 942 | static int __net_init fib_rules_net_init(struct net *net) |
Denis V. Lunev | 5fd30ee | 2008-01-10 03:20:28 -0800 | [diff] [blame] | 943 | { |
| 944 | INIT_LIST_HEAD(&net->rules_ops); |
| 945 | spin_lock_init(&net->rules_mod_lock); |
| 946 | return 0; |
| 947 | } |
| 948 | |
| 949 | static struct pernet_operations fib_rules_net_ops = { |
| 950 | .init = fib_rules_net_init, |
| 951 | }; |
| 952 | |
Thomas Graf | 14c0b97 | 2006-08-04 03:38:38 -0700 | [diff] [blame] | 953 | static int __init fib_rules_init(void) |
| 954 | { |
Denis V. Lunev | 5fd30ee | 2008-01-10 03:20:28 -0800 | [diff] [blame] | 955 | int err; |
Greg Rose | c7ac867 | 2011-06-10 01:27:09 +0000 | [diff] [blame] | 956 | rtnl_register(PF_UNSPEC, RTM_NEWRULE, fib_nl_newrule, NULL, NULL); |
| 957 | rtnl_register(PF_UNSPEC, RTM_DELRULE, fib_nl_delrule, NULL, NULL); |
| 958 | rtnl_register(PF_UNSPEC, RTM_GETRULE, NULL, fib_nl_dumprule, NULL); |
Thomas Graf | 9d9e6a5 | 2007-03-25 23:20:05 -0700 | [diff] [blame] | 959 | |
Eric W. Biederman | 5d6d480 | 2008-11-07 22:52:34 -0800 | [diff] [blame] | 960 | err = register_pernet_subsys(&fib_rules_net_ops); |
Denis V. Lunev | 5fd30ee | 2008-01-10 03:20:28 -0800 | [diff] [blame] | 961 | if (err < 0) |
| 962 | goto fail; |
| 963 | |
Eric W. Biederman | 5d6d480 | 2008-11-07 22:52:34 -0800 | [diff] [blame] | 964 | err = register_netdevice_notifier(&fib_rules_notifier); |
Denis V. Lunev | 5fd30ee | 2008-01-10 03:20:28 -0800 | [diff] [blame] | 965 | if (err < 0) |
| 966 | goto fail_unregister; |
Eric W. Biederman | 5d6d480 | 2008-11-07 22:52:34 -0800 | [diff] [blame] | 967 | |
Denis V. Lunev | 5fd30ee | 2008-01-10 03:20:28 -0800 | [diff] [blame] | 968 | return 0; |
| 969 | |
| 970 | fail_unregister: |
Eric W. Biederman | 5d6d480 | 2008-11-07 22:52:34 -0800 | [diff] [blame] | 971 | unregister_pernet_subsys(&fib_rules_net_ops); |
Denis V. Lunev | 5fd30ee | 2008-01-10 03:20:28 -0800 | [diff] [blame] | 972 | fail: |
| 973 | rtnl_unregister(PF_UNSPEC, RTM_NEWRULE); |
| 974 | rtnl_unregister(PF_UNSPEC, RTM_DELRULE); |
| 975 | rtnl_unregister(PF_UNSPEC, RTM_GETRULE); |
| 976 | return err; |
Thomas Graf | 14c0b97 | 2006-08-04 03:38:38 -0700 | [diff] [blame] | 977 | } |
| 978 | |
| 979 | subsys_initcall(fib_rules_init); |