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