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