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