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