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