]> bbs.cooldavid.org Git - net-next-2.6.git/blob - net/core/fib_rules.c
[NET] fib_rules: Mark rules detached from the device
[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 jumped:
136                 if (!fib_rule_match(rule, ops, fl, flags))
137                         continue;
138
139                 if (rule->action == FR_ACT_GOTO) {
140                         struct fib_rule *target;
141
142                         target = rcu_dereference(rule->ctarget);
143                         if (target == NULL) {
144                                 continue;
145                         } else {
146                                 rule = target;
147                                 goto jumped;
148                         }
149                 } else
150                         err = ops->action(rule, fl, flags, arg);
151
152                 if (err != -EAGAIN) {
153                         fib_rule_get(rule);
154                         arg->rule = rule;
155                         goto out;
156                 }
157         }
158
159         err = -ESRCH;
160 out:
161         rcu_read_unlock();
162
163         return err;
164 }
165
166 EXPORT_SYMBOL_GPL(fib_rules_lookup);
167
168 static int validate_rulemsg(struct fib_rule_hdr *frh, struct nlattr **tb,
169                             struct fib_rules_ops *ops)
170 {
171         int err = -EINVAL;
172
173         if (frh->src_len)
174                 if (tb[FRA_SRC] == NULL ||
175                     frh->src_len > (ops->addr_size * 8) ||
176                     nla_len(tb[FRA_SRC]) != ops->addr_size)
177                         goto errout;
178
179         if (frh->dst_len)
180                 if (tb[FRA_DST] == NULL ||
181                     frh->dst_len > (ops->addr_size * 8) ||
182                     nla_len(tb[FRA_DST]) != ops->addr_size)
183                         goto errout;
184
185         err = 0;
186 errout:
187         return err;
188 }
189
190 static int fib_nl_newrule(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
191 {
192         struct fib_rule_hdr *frh = nlmsg_data(nlh);
193         struct fib_rules_ops *ops = NULL;
194         struct fib_rule *rule, *r, *last = NULL;
195         struct nlattr *tb[FRA_MAX+1];
196         int err = -EINVAL, unresolved = 0;
197
198         if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*frh)))
199                 goto errout;
200
201         ops = lookup_rules_ops(frh->family);
202         if (ops == NULL) {
203                 err = EAFNOSUPPORT;
204                 goto errout;
205         }
206
207         err = nlmsg_parse(nlh, sizeof(*frh), tb, FRA_MAX, ops->policy);
208         if (err < 0)
209                 goto errout;
210
211         err = validate_rulemsg(frh, tb, ops);
212         if (err < 0)
213                 goto errout;
214
215         rule = kzalloc(ops->rule_size, GFP_KERNEL);
216         if (rule == NULL) {
217                 err = -ENOMEM;
218                 goto errout;
219         }
220
221         if (tb[FRA_PRIORITY])
222                 rule->pref = nla_get_u32(tb[FRA_PRIORITY]);
223
224         if (tb[FRA_IFNAME]) {
225                 struct net_device *dev;
226
227                 rule->ifindex = -1;
228                 nla_strlcpy(rule->ifname, tb[FRA_IFNAME], IFNAMSIZ);
229                 dev = __dev_get_by_name(rule->ifname);
230                 if (dev)
231                         rule->ifindex = dev->ifindex;
232         }
233
234         if (tb[FRA_FWMARK]) {
235                 rule->mark = nla_get_u32(tb[FRA_FWMARK]);
236                 if (rule->mark)
237                         /* compatibility: if the mark value is non-zero all bits
238                          * are compared unless a mask is explicitly specified.
239                          */
240                         rule->mark_mask = 0xFFFFFFFF;
241         }
242
243         if (tb[FRA_FWMASK])
244                 rule->mark_mask = nla_get_u32(tb[FRA_FWMASK]);
245
246         rule->action = frh->action;
247         rule->flags = frh->flags;
248         rule->table = frh_get_table(frh, tb);
249
250         if (!rule->pref && ops->default_pref)
251                 rule->pref = ops->default_pref();
252
253         err = -EINVAL;
254         if (tb[FRA_GOTO]) {
255                 if (rule->action != FR_ACT_GOTO)
256                         goto errout_free;
257
258                 rule->target = nla_get_u32(tb[FRA_GOTO]);
259                 /* Backward jumps are prohibited to avoid endless loops */
260                 if (rule->target <= rule->pref)
261                         goto errout_free;
262
263                 list_for_each_entry(r, ops->rules_list, list) {
264                         if (r->pref == rule->target) {
265                                 rule->ctarget = r;
266                                 break;
267                         }
268                 }
269
270                 if (rule->ctarget == NULL)
271                         unresolved = 1;
272         } else if (rule->action == FR_ACT_GOTO)
273                 goto errout_free;
274
275         err = ops->configure(rule, skb, nlh, frh, tb);
276         if (err < 0)
277                 goto errout_free;
278
279         list_for_each_entry(r, ops->rules_list, list) {
280                 if (r->pref > rule->pref)
281                         break;
282                 last = r;
283         }
284
285         fib_rule_get(rule);
286
287         if (ops->unresolved_rules) {
288                 /*
289                  * There are unresolved goto rules in the list, check if
290                  * any of them are pointing to this new rule.
291                  */
292                 list_for_each_entry(r, ops->rules_list, list) {
293                         if (r->action == FR_ACT_GOTO &&
294                             r->target == rule->pref) {
295                                 BUG_ON(r->ctarget != NULL);
296                                 rcu_assign_pointer(r->ctarget, rule);
297                                 if (--ops->unresolved_rules == 0)
298                                         break;
299                         }
300                 }
301         }
302
303         if (rule->action == FR_ACT_GOTO)
304                 ops->nr_goto_rules++;
305
306         if (unresolved)
307                 ops->unresolved_rules++;
308
309         if (last)
310                 list_add_rcu(&rule->list, &last->list);
311         else
312                 list_add_rcu(&rule->list, ops->rules_list);
313
314         notify_rule_change(RTM_NEWRULE, rule, ops, nlh, NETLINK_CB(skb).pid);
315         rules_ops_put(ops);
316         return 0;
317
318 errout_free:
319         kfree(rule);
320 errout:
321         rules_ops_put(ops);
322         return err;
323 }
324
325 static int fib_nl_delrule(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
326 {
327         struct fib_rule_hdr *frh = nlmsg_data(nlh);
328         struct fib_rules_ops *ops = NULL;
329         struct fib_rule *rule, *tmp;
330         struct nlattr *tb[FRA_MAX+1];
331         int err = -EINVAL;
332
333         if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*frh)))
334                 goto errout;
335
336         ops = lookup_rules_ops(frh->family);
337         if (ops == NULL) {
338                 err = EAFNOSUPPORT;
339                 goto errout;
340         }
341
342         err = nlmsg_parse(nlh, sizeof(*frh), tb, FRA_MAX, ops->policy);
343         if (err < 0)
344                 goto errout;
345
346         err = validate_rulemsg(frh, tb, ops);
347         if (err < 0)
348                 goto errout;
349
350         list_for_each_entry(rule, ops->rules_list, list) {
351                 if (frh->action && (frh->action != rule->action))
352                         continue;
353
354                 if (frh->table && (frh_get_table(frh, tb) != rule->table))
355                         continue;
356
357                 if (tb[FRA_PRIORITY] &&
358                     (rule->pref != nla_get_u32(tb[FRA_PRIORITY])))
359                         continue;
360
361                 if (tb[FRA_IFNAME] &&
362                     nla_strcmp(tb[FRA_IFNAME], rule->ifname))
363                         continue;
364
365                 if (tb[FRA_FWMARK] &&
366                     (rule->mark != nla_get_u32(tb[FRA_FWMARK])))
367                         continue;
368
369                 if (tb[FRA_FWMASK] &&
370                     (rule->mark_mask != nla_get_u32(tb[FRA_FWMASK])))
371                         continue;
372
373                 if (!ops->compare(rule, frh, tb))
374                         continue;
375
376                 if (rule->flags & FIB_RULE_PERMANENT) {
377                         err = -EPERM;
378                         goto errout;
379                 }
380
381                 list_del_rcu(&rule->list);
382
383                 if (rule->action == FR_ACT_GOTO)
384                         ops->nr_goto_rules--;
385
386                 /*
387                  * Check if this rule is a target to any of them. If so,
388                  * disable them. As this operation is eventually very
389                  * expensive, it is only performed if goto rules have
390                  * actually been added.
391                  */
392                 if (ops->nr_goto_rules > 0) {
393                         list_for_each_entry(tmp, ops->rules_list, list) {
394                                 if (tmp->ctarget == rule) {
395                                         rcu_assign_pointer(tmp->ctarget, NULL);
396                                         ops->unresolved_rules++;
397                                 }
398                         }
399                 }
400
401                 synchronize_rcu();
402                 notify_rule_change(RTM_DELRULE, rule, ops, nlh,
403                                    NETLINK_CB(skb).pid);
404                 fib_rule_put(rule);
405                 rules_ops_put(ops);
406                 return 0;
407         }
408
409         err = -ENOENT;
410 errout:
411         rules_ops_put(ops);
412         return err;
413 }
414
415 static inline size_t fib_rule_nlmsg_size(struct fib_rules_ops *ops,
416                                          struct fib_rule *rule)
417 {
418         size_t payload = NLMSG_ALIGN(sizeof(struct fib_rule_hdr))
419                          + nla_total_size(IFNAMSIZ) /* FRA_IFNAME */
420                          + nla_total_size(4) /* FRA_PRIORITY */
421                          + nla_total_size(4) /* FRA_TABLE */
422                          + nla_total_size(4) /* FRA_FWMARK */
423                          + nla_total_size(4); /* FRA_FWMASK */
424
425         if (ops->nlmsg_payload)
426                 payload += ops->nlmsg_payload(rule);
427
428         return payload;
429 }
430
431 static int fib_nl_fill_rule(struct sk_buff *skb, struct fib_rule *rule,
432                             u32 pid, u32 seq, int type, int flags,
433                             struct fib_rules_ops *ops)
434 {
435         struct nlmsghdr *nlh;
436         struct fib_rule_hdr *frh;
437
438         nlh = nlmsg_put(skb, pid, seq, type, sizeof(*frh), flags);
439         if (nlh == NULL)
440                 return -EMSGSIZE;
441
442         frh = nlmsg_data(nlh);
443         frh->table = rule->table;
444         NLA_PUT_U32(skb, FRA_TABLE, rule->table);
445         frh->res1 = 0;
446         frh->res2 = 0;
447         frh->action = rule->action;
448         frh->flags = rule->flags;
449
450         if (rule->action == FR_ACT_GOTO && rule->ctarget == NULL)
451                 frh->flags |= FIB_RULE_UNRESOLVED;
452
453         if (rule->ifname[0]) {
454                 NLA_PUT_STRING(skb, FRA_IFNAME, rule->ifname);
455
456                 if (rule->ifindex == -1)
457                         frh->flags |= FIB_RULE_DEV_DETACHED;
458         }
459
460         if (rule->pref)
461                 NLA_PUT_U32(skb, FRA_PRIORITY, rule->pref);
462
463         if (rule->mark)
464                 NLA_PUT_U32(skb, FRA_FWMARK, rule->mark);
465
466         if (rule->mark_mask || rule->mark)
467                 NLA_PUT_U32(skb, FRA_FWMASK, rule->mark_mask);
468
469         if (rule->target)
470                 NLA_PUT_U32(skb, FRA_GOTO, rule->target);
471
472         if (ops->fill(rule, skb, nlh, frh) < 0)
473                 goto nla_put_failure;
474
475         return nlmsg_end(skb, nlh);
476
477 nla_put_failure:
478         nlmsg_cancel(skb, nlh);
479         return -EMSGSIZE;
480 }
481
482 static int dump_rules(struct sk_buff *skb, struct netlink_callback *cb,
483                       struct fib_rules_ops *ops)
484 {
485         int idx = 0;
486         struct fib_rule *rule;
487
488         rcu_read_lock();
489         list_for_each_entry_rcu(rule, ops->rules_list, list) {
490                 if (idx < cb->args[1])
491                         goto skip;
492
493                 if (fib_nl_fill_rule(skb, rule, NETLINK_CB(cb->skb).pid,
494                                      cb->nlh->nlmsg_seq, RTM_NEWRULE,
495                                      NLM_F_MULTI, ops) < 0)
496                         break;
497 skip:
498                 idx++;
499         }
500         rcu_read_unlock();
501         cb->args[1] = idx;
502         rules_ops_put(ops);
503
504         return skb->len;
505 }
506
507 static int fib_nl_dumprule(struct sk_buff *skb, struct netlink_callback *cb)
508 {
509         struct fib_rules_ops *ops;
510         int idx = 0, family;
511
512         family = rtnl_msg_family(cb->nlh);
513         if (family != AF_UNSPEC) {
514                 /* Protocol specific dump request */
515                 ops = lookup_rules_ops(family);
516                 if (ops == NULL)
517                         return -EAFNOSUPPORT;
518
519                 return dump_rules(skb, cb, ops);
520         }
521
522         rcu_read_lock();
523         list_for_each_entry_rcu(ops, &rules_ops, list) {
524                 if (idx < cb->args[0] || !try_module_get(ops->owner))
525                         goto skip;
526
527                 if (dump_rules(skb, cb, ops) < 0)
528                         break;
529
530                 cb->args[1] = 0;
531         skip:
532                 idx++;
533         }
534         rcu_read_unlock();
535         cb->args[0] = idx;
536
537         return skb->len;
538 }
539
540 static void notify_rule_change(int event, struct fib_rule *rule,
541                                struct fib_rules_ops *ops, struct nlmsghdr *nlh,
542                                u32 pid)
543 {
544         struct sk_buff *skb;
545         int err = -ENOBUFS;
546
547         skb = nlmsg_new(fib_rule_nlmsg_size(ops, rule), GFP_KERNEL);
548         if (skb == NULL)
549                 goto errout;
550
551         err = fib_nl_fill_rule(skb, rule, pid, nlh->nlmsg_seq, event, 0, ops);
552         if (err < 0) {
553                 /* -EMSGSIZE implies BUG in fib_rule_nlmsg_size() */
554                 WARN_ON(err == -EMSGSIZE);
555                 kfree_skb(skb);
556                 goto errout;
557         }
558         err = rtnl_notify(skb, pid, ops->nlgroup, nlh, GFP_KERNEL);
559 errout:
560         if (err < 0)
561                 rtnl_set_sk_err(ops->nlgroup, err);
562 }
563
564 static void attach_rules(struct list_head *rules, struct net_device *dev)
565 {
566         struct fib_rule *rule;
567
568         list_for_each_entry(rule, rules, list) {
569                 if (rule->ifindex == -1 &&
570                     strcmp(dev->name, rule->ifname) == 0)
571                         rule->ifindex = dev->ifindex;
572         }
573 }
574
575 static void detach_rules(struct list_head *rules, struct net_device *dev)
576 {
577         struct fib_rule *rule;
578
579         list_for_each_entry(rule, rules, list)
580                 if (rule->ifindex == dev->ifindex)
581                         rule->ifindex = -1;
582 }
583
584
585 static int fib_rules_event(struct notifier_block *this, unsigned long event,
586                             void *ptr)
587 {
588         struct net_device *dev = ptr;
589         struct fib_rules_ops *ops;
590
591         ASSERT_RTNL();
592         rcu_read_lock();
593
594         switch (event) {
595         case NETDEV_REGISTER:
596                 list_for_each_entry(ops, &rules_ops, list)
597                         attach_rules(ops->rules_list, dev);
598                 break;
599
600         case NETDEV_UNREGISTER:
601                 list_for_each_entry(ops, &rules_ops, list)
602                         detach_rules(ops->rules_list, dev);
603                 break;
604         }
605
606         rcu_read_unlock();
607
608         return NOTIFY_DONE;
609 }
610
611 static struct notifier_block fib_rules_notifier = {
612         .notifier_call = fib_rules_event,
613 };
614
615 static int __init fib_rules_init(void)
616 {
617         rtnl_register(PF_UNSPEC, RTM_NEWRULE, fib_nl_newrule, NULL);
618         rtnl_register(PF_UNSPEC, RTM_DELRULE, fib_nl_delrule, NULL);
619         rtnl_register(PF_UNSPEC, RTM_GETRULE, NULL, fib_nl_dumprule);
620
621         return register_netdevice_notifier(&fib_rules_notifier);
622 }
623
624 subsys_initcall(fib_rules_init);