]> bbs.cooldavid.org Git - net-next-2.6.git/blob - net/ipv4/fib_rules.c
[NET]: Rethink mark field in struct flowi
[net-next-2.6.git] / net / ipv4 / fib_rules.c
1 /*
2  * INET         An implementation of the TCP/IP protocol suite for the LINUX
3  *              operating system.  INET is implemented using the  BSD Socket
4  *              interface as the means of communication with the user level.
5  *
6  *              IPv4 Forwarding Information Base: policy rules.
7  *
8  * Authors:     Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
9  *              Thomas Graf <tgraf@suug.ch>
10  *
11  *              This program is free software; you can redistribute it and/or
12  *              modify it under the terms of the GNU General Public License
13  *              as published by the Free Software Foundation; either version
14  *              2 of the License, or (at your option) any later version.
15  *
16  * Fixes:
17  *              Rani Assaf      :       local_rule cannot be deleted
18  *              Marc Boucher    :       routing by fwmark
19  */
20
21 #include <linux/types.h>
22 #include <linux/kernel.h>
23 #include <linux/netdevice.h>
24 #include <linux/netlink.h>
25 #include <linux/inetdevice.h>
26 #include <linux/init.h>
27 #include <linux/list.h>
28 #include <linux/rcupdate.h>
29 #include <net/ip.h>
30 #include <net/route.h>
31 #include <net/tcp.h>
32 #include <net/ip_fib.h>
33 #include <net/fib_rules.h>
34
35 static struct fib_rules_ops fib4_rules_ops;
36
37 struct fib4_rule
38 {
39         struct fib_rule         common;
40         u8                      dst_len;
41         u8                      src_len;
42         u8                      tos;
43         __be32                  src;
44         __be32                  srcmask;
45         __be32                  dst;
46         __be32                  dstmask;
47         u32                     fwmark;
48         u32                     fwmask;
49 #ifdef CONFIG_NET_CLS_ROUTE
50         u32                     tclassid;
51 #endif
52 };
53
54 static struct fib4_rule default_rule = {
55         .common = {
56                 .refcnt =       ATOMIC_INIT(2),
57                 .pref =         0x7FFF,
58                 .table =        RT_TABLE_DEFAULT,
59                 .action =       FR_ACT_TO_TBL,
60         },
61 };
62
63 static struct fib4_rule main_rule = {
64         .common = {
65                 .refcnt =       ATOMIC_INIT(2),
66                 .pref =         0x7FFE,
67                 .table =        RT_TABLE_MAIN,
68                 .action =       FR_ACT_TO_TBL,
69         },
70 };
71
72 static struct fib4_rule local_rule = {
73         .common = {
74                 .refcnt =       ATOMIC_INIT(2),
75                 .table =        RT_TABLE_LOCAL,
76                 .action =       FR_ACT_TO_TBL,
77                 .flags =        FIB_RULE_PERMANENT,
78         },
79 };
80
81 static LIST_HEAD(fib4_rules);
82
83 #ifdef CONFIG_NET_CLS_ROUTE
84 u32 fib_rules_tclass(struct fib_result *res)
85 {
86         return res->r ? ((struct fib4_rule *) res->r)->tclassid : 0;
87 }
88 #endif
89
90 int fib_lookup(struct flowi *flp, struct fib_result *res)
91 {
92         struct fib_lookup_arg arg = {
93                 .result = res,
94         };
95         int err;
96
97         err = fib_rules_lookup(&fib4_rules_ops, flp, 0, &arg);
98         res->r = arg.rule;
99
100         return err;
101 }
102
103 static int fib4_rule_action(struct fib_rule *rule, struct flowi *flp,
104                             int flags, struct fib_lookup_arg *arg)
105 {
106         int err = -EAGAIN;
107         struct fib_table *tbl;
108
109         switch (rule->action) {
110         case FR_ACT_TO_TBL:
111                 break;
112
113         case FR_ACT_UNREACHABLE:
114                 err = -ENETUNREACH;
115                 goto errout;
116
117         case FR_ACT_PROHIBIT:
118                 err = -EACCES;
119                 goto errout;
120
121         case FR_ACT_BLACKHOLE:
122         default:
123                 err = -EINVAL;
124                 goto errout;
125         }
126
127         if ((tbl = fib_get_table(rule->table)) == NULL)
128                 goto errout;
129
130         err = tbl->tb_lookup(tbl, flp, (struct fib_result *) arg->result);
131         if (err > 0)
132                 err = -EAGAIN;
133 errout:
134         return err;
135 }
136
137
138 void fib_select_default(const struct flowi *flp, struct fib_result *res)
139 {
140         if (res->r && res->r->action == FR_ACT_TO_TBL &&
141             FIB_RES_GW(*res) && FIB_RES_NH(*res).nh_scope == RT_SCOPE_LINK) {
142                 struct fib_table *tb;
143                 if ((tb = fib_get_table(res->r->table)) != NULL)
144                         tb->tb_select_default(tb, flp, res);
145         }
146 }
147
148 static int fib4_rule_match(struct fib_rule *rule, struct flowi *fl, int flags)
149 {
150         struct fib4_rule *r = (struct fib4_rule *) rule;
151         __be32 daddr = fl->fl4_dst;
152         __be32 saddr = fl->fl4_src;
153
154         if (((saddr ^ r->src) & r->srcmask) ||
155             ((daddr ^ r->dst) & r->dstmask))
156                 return 0;
157
158         if (r->tos && (r->tos != fl->fl4_tos))
159                 return 0;
160
161         if ((r->fwmark ^ fl->mark) & r->fwmask)
162                 return 0;
163
164         return 1;
165 }
166
167 static struct fib_table *fib_empty_table(void)
168 {
169         u32 id;
170
171         for (id = 1; id <= RT_TABLE_MAX; id++)
172                 if (fib_get_table(id) == NULL)
173                         return fib_new_table(id);
174         return NULL;
175 }
176
177 static struct nla_policy fib4_rule_policy[FRA_MAX+1] __read_mostly = {
178         [FRA_IFNAME]    = { .type = NLA_STRING, .len = IFNAMSIZ - 1 },
179         [FRA_PRIORITY]  = { .type = NLA_U32 },
180         [FRA_SRC]       = { .type = NLA_U32 },
181         [FRA_DST]       = { .type = NLA_U32 },
182         [FRA_FWMARK]    = { .type = NLA_U32 },
183         [FRA_FWMASK]    = { .type = NLA_U32 },
184         [FRA_FLOW]      = { .type = NLA_U32 },
185         [FRA_TABLE]     = { .type = NLA_U32 },
186 };
187
188 static int fib4_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
189                                struct nlmsghdr *nlh, struct fib_rule_hdr *frh,
190                                struct nlattr **tb)
191 {
192         int err = -EINVAL;
193         struct fib4_rule *rule4 = (struct fib4_rule *) rule;
194
195         if (frh->src_len > 32 || frh->dst_len > 32 ||
196             (frh->tos & ~IPTOS_TOS_MASK))
197                 goto errout;
198
199         if (rule->table == RT_TABLE_UNSPEC) {
200                 if (rule->action == FR_ACT_TO_TBL) {
201                         struct fib_table *table;
202
203                         table = fib_empty_table();
204                         if (table == NULL) {
205                                 err = -ENOBUFS;
206                                 goto errout;
207                         }
208
209                         rule->table = table->tb_id;
210                 }
211         }
212
213         if (tb[FRA_SRC])
214                 rule4->src = nla_get_be32(tb[FRA_SRC]);
215
216         if (tb[FRA_DST])
217                 rule4->dst = nla_get_be32(tb[FRA_DST]);
218
219         if (tb[FRA_FWMARK]) {
220                 rule4->fwmark = nla_get_u32(tb[FRA_FWMARK]);
221                 if (rule4->fwmark)
222                         /* compatibility: if the mark value is non-zero all bits
223                          * are compared unless a mask is explicitly specified.
224                          */
225                         rule4->fwmask = 0xFFFFFFFF;
226         }
227
228         if (tb[FRA_FWMASK])
229                 rule4->fwmask = nla_get_u32(tb[FRA_FWMASK]);
230
231 #ifdef CONFIG_NET_CLS_ROUTE
232         if (tb[FRA_FLOW])
233                 rule4->tclassid = nla_get_u32(tb[FRA_FLOW]);
234 #endif
235
236         rule4->src_len = frh->src_len;
237         rule4->srcmask = inet_make_mask(rule4->src_len);
238         rule4->dst_len = frh->dst_len;
239         rule4->dstmask = inet_make_mask(rule4->dst_len);
240         rule4->tos = frh->tos;
241
242         err = 0;
243 errout:
244         return err;
245 }
246
247 static int fib4_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh,
248                              struct nlattr **tb)
249 {
250         struct fib4_rule *rule4 = (struct fib4_rule *) rule;
251
252         if (frh->src_len && (rule4->src_len != frh->src_len))
253                 return 0;
254
255         if (frh->dst_len && (rule4->dst_len != frh->dst_len))
256                 return 0;
257
258         if (frh->tos && (rule4->tos != frh->tos))
259                 return 0;
260
261         if (tb[FRA_FWMARK] && (rule4->fwmark != nla_get_u32(tb[FRA_FWMARK])))
262                 return 0;
263
264         if (tb[FRA_FWMASK] && (rule4->fwmask != nla_get_u32(tb[FRA_FWMASK])))
265                 return 0;
266
267 #ifdef CONFIG_NET_CLS_ROUTE
268         if (tb[FRA_FLOW] && (rule4->tclassid != nla_get_u32(tb[FRA_FLOW])))
269                 return 0;
270 #endif
271
272         if (tb[FRA_SRC] && (rule4->src != nla_get_be32(tb[FRA_SRC])))
273                 return 0;
274
275         if (tb[FRA_DST] && (rule4->dst != nla_get_be32(tb[FRA_DST])))
276                 return 0;
277
278         return 1;
279 }
280
281 static int fib4_rule_fill(struct fib_rule *rule, struct sk_buff *skb,
282                           struct nlmsghdr *nlh, struct fib_rule_hdr *frh)
283 {
284         struct fib4_rule *rule4 = (struct fib4_rule *) rule;
285
286         frh->family = AF_INET;
287         frh->dst_len = rule4->dst_len;
288         frh->src_len = rule4->src_len;
289         frh->tos = rule4->tos;
290
291         if (rule4->fwmark)
292                 NLA_PUT_U32(skb, FRA_FWMARK, rule4->fwmark);
293
294         if (rule4->fwmask || rule4->fwmark)
295                 NLA_PUT_U32(skb, FRA_FWMASK, rule4->fwmask);
296
297         if (rule4->dst_len)
298                 NLA_PUT_BE32(skb, FRA_DST, rule4->dst);
299
300         if (rule4->src_len)
301                 NLA_PUT_BE32(skb, FRA_SRC, rule4->src);
302
303 #ifdef CONFIG_NET_CLS_ROUTE
304         if (rule4->tclassid)
305                 NLA_PUT_U32(skb, FRA_FLOW, rule4->tclassid);
306 #endif
307         return 0;
308
309 nla_put_failure:
310         return -ENOBUFS;
311 }
312
313 int fib4_rules_dump(struct sk_buff *skb, struct netlink_callback *cb)
314 {
315         return fib_rules_dump(skb, cb, AF_INET);
316 }
317
318 static u32 fib4_rule_default_pref(void)
319 {
320         struct list_head *pos;
321         struct fib_rule *rule;
322
323         if (!list_empty(&fib4_rules)) {
324                 pos = fib4_rules.next;
325                 if (pos->next != &fib4_rules) {
326                         rule = list_entry(pos->next, struct fib_rule, list);
327                         if (rule->pref)
328                                 return rule->pref - 1;
329                 }
330         }
331
332         return 0;
333 }
334
335 static struct fib_rules_ops fib4_rules_ops = {
336         .family         = AF_INET,
337         .rule_size      = sizeof(struct fib4_rule),
338         .action         = fib4_rule_action,
339         .match          = fib4_rule_match,
340         .configure      = fib4_rule_configure,
341         .compare        = fib4_rule_compare,
342         .fill           = fib4_rule_fill,
343         .default_pref   = fib4_rule_default_pref,
344         .nlgroup        = RTNLGRP_IPV4_RULE,
345         .policy         = fib4_rule_policy,
346         .rules_list     = &fib4_rules,
347         .owner          = THIS_MODULE,
348 };
349
350 void __init fib4_rules_init(void)
351 {
352         list_add_tail(&local_rule.common.list, &fib4_rules);
353         list_add_tail(&main_rule.common.list, &fib4_rules);
354         list_add_tail(&default_rule.common.list, &fib4_rules);
355
356         fib_rules_register(&fib4_rules_ops);
357 }