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