[NETLINK]: Do precise netlink message allocations where possible
[safe/jmp/linux-2.6] / net / ipv6 / fib6_rules.c
1 /*
2  * net/ipv6/fib6_rules.c        IPv6 Routing Policy Rules
3  *
4  * Copyright (C)2003-2006 Helsinki University of Technology
5  * Copyright (C)2003-2006 USAGI/WIDE Project
6  *
7  *      This program is free software; you can redistribute it and/or
8  *      modify it under the terms of the GNU General Public License as
9  *      published by the Free Software Foundation, version 2.
10  *
11  * Authors
12  *      Thomas Graf             <tgraf@suug.ch>
13  *      Ville Nuorvala          <vnuorval@tcs.hut.fi>
14  */
15
16 #include <linux/netdevice.h>
17
18 #include <net/fib_rules.h>
19 #include <net/ipv6.h>
20 #include <net/ip6_route.h>
21 #include <net/netlink.h>
22
23 struct fib6_rule
24 {
25         struct fib_rule         common;
26         struct rt6key           src;
27         struct rt6key           dst;
28         u8                      tclass;
29 };
30
31 static struct fib_rules_ops fib6_rules_ops;
32
33 static struct fib6_rule main_rule = {
34         .common = {
35                 .refcnt =       ATOMIC_INIT(2),
36                 .pref =         0x7FFE,
37                 .action =       FR_ACT_TO_TBL,
38                 .table =        RT6_TABLE_MAIN,
39         },
40 };
41
42 static struct fib6_rule local_rule = {
43         .common = {
44                 .refcnt =       ATOMIC_INIT(2),
45                 .pref =         0,
46                 .action =       FR_ACT_TO_TBL,
47                 .table =        RT6_TABLE_LOCAL,
48                 .flags =        FIB_RULE_PERMANENT,
49         },
50 };
51
52 static LIST_HEAD(fib6_rules);
53
54 struct dst_entry *fib6_rule_lookup(struct flowi *fl, int flags,
55                                    pol_lookup_t lookup)
56 {
57         struct fib_lookup_arg arg = {
58                 .lookup_ptr = lookup,
59         };
60
61         fib_rules_lookup(&fib6_rules_ops, fl, flags, &arg);
62         if (arg.rule)
63                 fib_rule_put(arg.rule);
64
65         if (arg.result)
66                 return arg.result;
67
68         dst_hold(&ip6_null_entry.u.dst);
69         return &ip6_null_entry.u.dst;
70 }
71
72 static int fib6_rule_action(struct fib_rule *rule, struct flowi *flp,
73                             int flags, struct fib_lookup_arg *arg)
74 {
75         struct rt6_info *rt = NULL;
76         struct fib6_table *table;
77         pol_lookup_t lookup = arg->lookup_ptr;
78
79         switch (rule->action) {
80         case FR_ACT_TO_TBL:
81                 break;
82         case FR_ACT_UNREACHABLE:
83                 rt = &ip6_null_entry;
84                 goto discard_pkt;
85         default:
86         case FR_ACT_BLACKHOLE:
87                 rt = &ip6_blk_hole_entry;
88                 goto discard_pkt;
89         case FR_ACT_PROHIBIT:
90                 rt = &ip6_prohibit_entry;
91                 goto discard_pkt;
92         }
93
94         table = fib6_get_table(rule->table);
95         if (table)
96                 rt = lookup(table, flp, flags);
97
98         if (rt != &ip6_null_entry)
99                 goto out;
100         dst_release(&rt->u.dst);
101         rt = NULL;
102         goto out;
103
104 discard_pkt:
105         dst_hold(&rt->u.dst);
106 out:
107         arg->result = rt;
108         return rt == NULL ? -EAGAIN : 0;
109 }
110
111
112 static int fib6_rule_match(struct fib_rule *rule, struct flowi *fl, int flags)
113 {
114         struct fib6_rule *r = (struct fib6_rule *) rule;
115
116         if (r->dst.plen &&
117             !ipv6_prefix_equal(&fl->fl6_dst, &r->dst.addr, r->dst.plen))
118                 return 0;
119
120         if (r->src.plen) {
121                 if (!(flags & RT6_LOOKUP_F_HAS_SADDR) ||
122                     !ipv6_prefix_equal(&fl->fl6_src, &r->src.addr, r->src.plen))
123                         return 0;
124         }
125
126         if (r->tclass && r->tclass != ((ntohl(fl->fl6_flowlabel) >> 20) & 0xff))
127                 return 0;
128
129         return 1;
130 }
131
132 static struct nla_policy fib6_rule_policy[FRA_MAX+1] __read_mostly = {
133         FRA_GENERIC_POLICY,
134         [FRA_SRC]       = { .len = sizeof(struct in6_addr) },
135         [FRA_DST]       = { .len = sizeof(struct in6_addr) },
136 };
137
138 static int fib6_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
139                                struct nlmsghdr *nlh, struct fib_rule_hdr *frh,
140                                struct nlattr **tb)
141 {
142         int err = -EINVAL;
143         struct fib6_rule *rule6 = (struct fib6_rule *) rule;
144
145         if (frh->src_len > 128 || frh->dst_len > 128 ||
146             (frh->tos & ~IPV6_FLOWINFO_MASK))
147                 goto errout;
148
149         if (rule->action == FR_ACT_TO_TBL) {
150                 if (rule->table == RT6_TABLE_UNSPEC)
151                         goto errout;
152
153                 if (fib6_new_table(rule->table) == NULL) {
154                         err = -ENOBUFS;
155                         goto errout;
156                 }
157         }
158
159         if (tb[FRA_SRC])
160                 nla_memcpy(&rule6->src.addr, tb[FRA_SRC],
161                            sizeof(struct in6_addr));
162
163         if (tb[FRA_DST])
164                 nla_memcpy(&rule6->dst.addr, tb[FRA_DST],
165                            sizeof(struct in6_addr));
166
167         rule6->src.plen = frh->src_len;
168         rule6->dst.plen = frh->dst_len;
169         rule6->tclass = frh->tos;
170
171         err = 0;
172 errout:
173         return err;
174 }
175
176 static int fib6_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh,
177                              struct nlattr **tb)
178 {
179         struct fib6_rule *rule6 = (struct fib6_rule *) rule;
180
181         if (frh->src_len && (rule6->src.plen != frh->src_len))
182                 return 0;
183
184         if (frh->dst_len && (rule6->dst.plen != frh->dst_len))
185                 return 0;
186
187         if (frh->tos && (rule6->tclass != frh->tos))
188                 return 0;
189
190         if (tb[FRA_SRC] &&
191             nla_memcmp(tb[FRA_SRC], &rule6->src.addr, sizeof(struct in6_addr)))
192                 return 0;
193
194         if (tb[FRA_DST] &&
195             nla_memcmp(tb[FRA_DST], &rule6->dst.addr, sizeof(struct in6_addr)))
196                 return 0;
197
198         return 1;
199 }
200
201 static int fib6_rule_fill(struct fib_rule *rule, struct sk_buff *skb,
202                           struct nlmsghdr *nlh, struct fib_rule_hdr *frh)
203 {
204         struct fib6_rule *rule6 = (struct fib6_rule *) rule;
205
206         frh->family = AF_INET6;
207         frh->dst_len = rule6->dst.plen;
208         frh->src_len = rule6->src.plen;
209         frh->tos = rule6->tclass;
210
211         if (rule6->dst.plen)
212                 NLA_PUT(skb, FRA_DST, sizeof(struct in6_addr),
213                         &rule6->dst.addr);
214
215         if (rule6->src.plen)
216                 NLA_PUT(skb, FRA_SRC, sizeof(struct in6_addr),
217                         &rule6->src.addr);
218
219         return 0;
220
221 nla_put_failure:
222         return -ENOBUFS;
223 }
224
225 int fib6_rules_dump(struct sk_buff *skb, struct netlink_callback *cb)
226 {
227         return fib_rules_dump(skb, cb, AF_INET6);
228 }
229
230 static u32 fib6_rule_default_pref(void)
231 {
232         return 0x3FFF;
233 }
234
235 static size_t fib6_rule_nlmsg_payload(struct fib_rule *rule)
236 {
237         return nla_total_size(16) /* dst */
238                + nla_total_size(16); /* src */
239 }
240
241 static struct fib_rules_ops fib6_rules_ops = {
242         .family                 = AF_INET6,
243         .rule_size              = sizeof(struct fib6_rule),
244         .action                 = fib6_rule_action,
245         .match                  = fib6_rule_match,
246         .configure              = fib6_rule_configure,
247         .compare                = fib6_rule_compare,
248         .fill                   = fib6_rule_fill,
249         .default_pref           = fib6_rule_default_pref,
250         .nlmsg_payload          = fib6_rule_nlmsg_payload,
251         .nlgroup                = RTNLGRP_IPV6_RULE,
252         .policy                 = fib6_rule_policy,
253         .rules_list             = &fib6_rules,
254         .owner                  = THIS_MODULE,
255 };
256
257 void __init fib6_rules_init(void)
258 {
259         list_add_tail(&local_rule.common.list, &fib6_rules);
260         list_add_tail(&main_rule.common.list, &fib6_rules);
261
262         fib_rules_register(&fib6_rules_ops);
263 }
264
265 void fib6_rules_cleanup(void)
266 {
267         fib_rules_unregister(&fib6_rules_ops);
268 }