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