]> bbs.cooldavid.org Git - net-next-2.6.git/blame - net/core/fib_rules.c
[NET] rules: Protocol independant mark selector
[net-next-2.6.git] / net / core / fib_rules.c
CommitLineData
14c0b97d
TG
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
14c0b97d
TG
11#include <linux/types.h>
12#include <linux/kernel.h>
13#include <linux/list.h>
14#include <net/fib_rules.h>
15
16static LIST_HEAD(rules_ops);
17static DEFINE_SPINLOCK(rules_mod_lock);
18
19static void notify_rule_change(int event, struct fib_rule *rule,
c17084d2
TG
20 struct fib_rules_ops *ops, struct nlmsghdr *nlh,
21 u32 pid);
14c0b97d
TG
22
23static struct fib_rules_ops *lookup_rules_ops(int family)
24{
25 struct fib_rules_ops *ops;
26
27 rcu_read_lock();
28 list_for_each_entry_rcu(ops, &rules_ops, list) {
29 if (ops->family == family) {
30 if (!try_module_get(ops->owner))
31 ops = NULL;
32 rcu_read_unlock();
33 return ops;
34 }
35 }
36 rcu_read_unlock();
37
38 return NULL;
39}
40
41static void rules_ops_put(struct fib_rules_ops *ops)
42{
43 if (ops)
44 module_put(ops->owner);
45}
46
47int fib_rules_register(struct fib_rules_ops *ops)
48{
49 int err = -EEXIST;
50 struct fib_rules_ops *o;
51
52 if (ops->rule_size < sizeof(struct fib_rule))
53 return -EINVAL;
54
55 if (ops->match == NULL || ops->configure == NULL ||
56 ops->compare == NULL || ops->fill == NULL ||
57 ops->action == NULL)
58 return -EINVAL;
59
60 spin_lock(&rules_mod_lock);
61 list_for_each_entry(o, &rules_ops, list)
62 if (ops->family == o->family)
63 goto errout;
64
65 list_add_tail_rcu(&ops->list, &rules_ops);
66 err = 0;
67errout:
68 spin_unlock(&rules_mod_lock);
69
70 return err;
71}
72
73EXPORT_SYMBOL_GPL(fib_rules_register);
74
75static void cleanup_ops(struct fib_rules_ops *ops)
76{
77 struct fib_rule *rule, *tmp;
78
79 list_for_each_entry_safe(rule, tmp, ops->rules_list, list) {
80 list_del_rcu(&rule->list);
81 fib_rule_put(rule);
82 }
83}
84
85int fib_rules_unregister(struct fib_rules_ops *ops)
86{
87 int err = 0;
88 struct fib_rules_ops *o;
89
90 spin_lock(&rules_mod_lock);
91 list_for_each_entry(o, &rules_ops, list) {
92 if (o == ops) {
93 list_del_rcu(&o->list);
94 cleanup_ops(ops);
95 goto out;
96 }
97 }
98
99 err = -ENOENT;
100out:
101 spin_unlock(&rules_mod_lock);
102
103 synchronize_rcu();
104
105 return err;
106}
107
108EXPORT_SYMBOL_GPL(fib_rules_unregister);
109
110int fib_rules_lookup(struct fib_rules_ops *ops, struct flowi *fl,
111 int flags, struct fib_lookup_arg *arg)
112{
113 struct fib_rule *rule;
114 int err;
115
116 rcu_read_lock();
117
118 list_for_each_entry_rcu(rule, ops->rules_list, list) {
119 if (rule->ifindex && (rule->ifindex != fl->iif))
120 continue;
121
b8964ed9
TG
122 if ((rule->mark ^ fl->mark) & rule->mark_mask)
123 continue;
124
14c0b97d
TG
125 if (!ops->match(rule, fl, flags))
126 continue;
127
128 err = ops->action(rule, fl, flags, arg);
129 if (err != -EAGAIN) {
130 fib_rule_get(rule);
131 arg->rule = rule;
132 goto out;
133 }
134 }
135
136 err = -ENETUNREACH;
137out:
138 rcu_read_unlock();
139
140 return err;
141}
142
143EXPORT_SYMBOL_GPL(fib_rules_lookup);
144
145int fib_nl_newrule(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
146{
147 struct fib_rule_hdr *frh = nlmsg_data(nlh);
148 struct fib_rules_ops *ops = NULL;
149 struct fib_rule *rule, *r, *last = NULL;
150 struct nlattr *tb[FRA_MAX+1];
151 int err = -EINVAL;
152
153 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*frh)))
154 goto errout;
155
156 ops = lookup_rules_ops(frh->family);
157 if (ops == NULL) {
158 err = EAFNOSUPPORT;
159 goto errout;
160 }
161
162 err = nlmsg_parse(nlh, sizeof(*frh), tb, FRA_MAX, ops->policy);
163 if (err < 0)
164 goto errout;
165
14c0b97d
TG
166 rule = kzalloc(ops->rule_size, GFP_KERNEL);
167 if (rule == NULL) {
168 err = -ENOMEM;
169 goto errout;
170 }
171
172 if (tb[FRA_PRIORITY])
173 rule->pref = nla_get_u32(tb[FRA_PRIORITY]);
174
175 if (tb[FRA_IFNAME]) {
176 struct net_device *dev;
177
178 rule->ifindex = -1;
5176f91e 179 nla_strlcpy(rule->ifname, tb[FRA_IFNAME], IFNAMSIZ);
14c0b97d
TG
180 dev = __dev_get_by_name(rule->ifname);
181 if (dev)
182 rule->ifindex = dev->ifindex;
183 }
184
b8964ed9
TG
185 if (tb[FRA_FWMARK]) {
186 rule->mark = nla_get_u32(tb[FRA_FWMARK]);
187 if (rule->mark)
188 /* compatibility: if the mark value is non-zero all bits
189 * are compared unless a mask is explicitly specified.
190 */
191 rule->mark_mask = 0xFFFFFFFF;
192 }
193
194 if (tb[FRA_FWMASK])
195 rule->mark_mask = nla_get_u32(tb[FRA_FWMASK]);
196
14c0b97d
TG
197 rule->action = frh->action;
198 rule->flags = frh->flags;
9e762a4a 199 rule->table = frh_get_table(frh, tb);
14c0b97d
TG
200
201 if (!rule->pref && ops->default_pref)
202 rule->pref = ops->default_pref();
203
204 err = ops->configure(rule, skb, nlh, frh, tb);
205 if (err < 0)
206 goto errout_free;
207
208 list_for_each_entry(r, ops->rules_list, list) {
209 if (r->pref > rule->pref)
210 break;
211 last = r;
212 }
213
214 fib_rule_get(rule);
215
216 if (last)
217 list_add_rcu(&rule->list, &last->list);
218 else
219 list_add_rcu(&rule->list, ops->rules_list);
220
c17084d2 221 notify_rule_change(RTM_NEWRULE, rule, ops, nlh, NETLINK_CB(skb).pid);
14c0b97d
TG
222 rules_ops_put(ops);
223 return 0;
224
225errout_free:
226 kfree(rule);
227errout:
228 rules_ops_put(ops);
229 return err;
230}
231
232int fib_nl_delrule(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
233{
234 struct fib_rule_hdr *frh = nlmsg_data(nlh);
235 struct fib_rules_ops *ops = NULL;
236 struct fib_rule *rule;
237 struct nlattr *tb[FRA_MAX+1];
238 int err = -EINVAL;
239
240 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*frh)))
241 goto errout;
242
243 ops = lookup_rules_ops(frh->family);
244 if (ops == NULL) {
245 err = EAFNOSUPPORT;
246 goto errout;
247 }
248
249 err = nlmsg_parse(nlh, sizeof(*frh), tb, FRA_MAX, ops->policy);
250 if (err < 0)
251 goto errout;
252
253 list_for_each_entry(rule, ops->rules_list, list) {
254 if (frh->action && (frh->action != rule->action))
255 continue;
256
9e762a4a 257 if (frh->table && (frh_get_table(frh, tb) != rule->table))
14c0b97d
TG
258 continue;
259
260 if (tb[FRA_PRIORITY] &&
261 (rule->pref != nla_get_u32(tb[FRA_PRIORITY])))
262 continue;
263
264 if (tb[FRA_IFNAME] &&
265 nla_strcmp(tb[FRA_IFNAME], rule->ifname))
266 continue;
267
b8964ed9
TG
268 if (tb[FRA_FWMARK] &&
269 (rule->mark != nla_get_u32(tb[FRA_FWMARK])))
270 continue;
271
272 if (tb[FRA_FWMASK] &&
273 (rule->mark_mask != nla_get_u32(tb[FRA_FWMASK])))
274 continue;
275
14c0b97d
TG
276 if (!ops->compare(rule, frh, tb))
277 continue;
278
279 if (rule->flags & FIB_RULE_PERMANENT) {
280 err = -EPERM;
281 goto errout;
282 }
283
284 list_del_rcu(&rule->list);
285 synchronize_rcu();
c17084d2
TG
286 notify_rule_change(RTM_DELRULE, rule, ops, nlh,
287 NETLINK_CB(skb).pid);
14c0b97d
TG
288 fib_rule_put(rule);
289 rules_ops_put(ops);
290 return 0;
291 }
292
293 err = -ENOENT;
294errout:
295 rules_ops_put(ops);
296 return err;
297}
298
299static int fib_nl_fill_rule(struct sk_buff *skb, struct fib_rule *rule,
300 u32 pid, u32 seq, int type, int flags,
301 struct fib_rules_ops *ops)
302{
303 struct nlmsghdr *nlh;
304 struct fib_rule_hdr *frh;
305
306 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*frh), flags);
307 if (nlh == NULL)
308 return -1;
309
310 frh = nlmsg_data(nlh);
311 frh->table = rule->table;
9e762a4a 312 NLA_PUT_U32(skb, FRA_TABLE, rule->table);
14c0b97d
TG
313 frh->res1 = 0;
314 frh->res2 = 0;
315 frh->action = rule->action;
316 frh->flags = rule->flags;
317
318 if (rule->ifname[0])
319 NLA_PUT_STRING(skb, FRA_IFNAME, rule->ifname);
320
321 if (rule->pref)
322 NLA_PUT_U32(skb, FRA_PRIORITY, rule->pref);
323
b8964ed9
TG
324 if (rule->mark)
325 NLA_PUT_U32(skb, FRA_FWMARK, rule->mark);
326
327 if (rule->mark_mask || rule->mark)
328 NLA_PUT_U32(skb, FRA_FWMASK, rule->mark_mask);
329
14c0b97d
TG
330 if (ops->fill(rule, skb, nlh, frh) < 0)
331 goto nla_put_failure;
332
333 return nlmsg_end(skb, nlh);
334
335nla_put_failure:
336 return nlmsg_cancel(skb, nlh);
337}
338
339int fib_rules_dump(struct sk_buff *skb, struct netlink_callback *cb, int family)
340{
341 int idx = 0;
342 struct fib_rule *rule;
343 struct fib_rules_ops *ops;
344
345 ops = lookup_rules_ops(family);
346 if (ops == NULL)
347 return -EAFNOSUPPORT;
348
349 rcu_read_lock();
350 list_for_each_entry(rule, ops->rules_list, list) {
351 if (idx < cb->args[0])
352 goto skip;
353
354 if (fib_nl_fill_rule(skb, rule, NETLINK_CB(cb->skb).pid,
355 cb->nlh->nlmsg_seq, RTM_NEWRULE,
356 NLM_F_MULTI, ops) < 0)
357 break;
358skip:
359 idx++;
360 }
361 rcu_read_unlock();
362 cb->args[0] = idx;
363 rules_ops_put(ops);
364
365 return skb->len;
366}
367
368EXPORT_SYMBOL_GPL(fib_rules_dump);
369
370static void notify_rule_change(int event, struct fib_rule *rule,
c17084d2
TG
371 struct fib_rules_ops *ops, struct nlmsghdr *nlh,
372 u32 pid)
14c0b97d 373{
c17084d2
TG
374 struct sk_buff *skb;
375 int err = -ENOBUFS;
14c0b97d 376
c17084d2 377 skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
14c0b97d 378 if (skb == NULL)
c17084d2
TG
379 goto errout;
380
381 err = fib_nl_fill_rule(skb, rule, pid, nlh->nlmsg_seq, event, 0, ops);
382 if (err < 0) {
14c0b97d 383 kfree_skb(skb);
c17084d2
TG
384 goto errout;
385 }
386
387 err = rtnl_notify(skb, pid, ops->nlgroup, nlh, GFP_KERNEL);
388errout:
389 if (err < 0)
390 rtnl_set_sk_err(ops->nlgroup, err);
14c0b97d
TG
391}
392
393static void attach_rules(struct list_head *rules, struct net_device *dev)
394{
395 struct fib_rule *rule;
396
397 list_for_each_entry(rule, rules, list) {
398 if (rule->ifindex == -1 &&
399 strcmp(dev->name, rule->ifname) == 0)
400 rule->ifindex = dev->ifindex;
401 }
402}
403
404static void detach_rules(struct list_head *rules, struct net_device *dev)
405{
406 struct fib_rule *rule;
407
408 list_for_each_entry(rule, rules, list)
409 if (rule->ifindex == dev->ifindex)
410 rule->ifindex = -1;
411}
412
413
414static int fib_rules_event(struct notifier_block *this, unsigned long event,
415 void *ptr)
416{
417 struct net_device *dev = ptr;
418 struct fib_rules_ops *ops;
419
420 ASSERT_RTNL();
421 rcu_read_lock();
422
423 switch (event) {
424 case NETDEV_REGISTER:
425 list_for_each_entry(ops, &rules_ops, list)
426 attach_rules(ops->rules_list, dev);
427 break;
428
429 case NETDEV_UNREGISTER:
430 list_for_each_entry(ops, &rules_ops, list)
431 detach_rules(ops->rules_list, dev);
432 break;
433 }
434
435 rcu_read_unlock();
436
437 return NOTIFY_DONE;
438}
439
440static struct notifier_block fib_rules_notifier = {
441 .notifier_call = fib_rules_event,
442};
443
444static int __init fib_rules_init(void)
445{
446 return register_netdevice_notifier(&fib_rules_notifier);
447}
448
449subsys_initcall(fib_rules_init);