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