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