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