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