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