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