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