netfilter: conntrack: disable generic tracking for known protocols
[profile/mobile/platform/kernel/linux-3.10-sc7730.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 <linux/export.h>
30 #include <net/ip.h>
31 #include <net/route.h>
32 #include <net/tcp.h>
33 #include <net/ip_fib.h>
34 #include <net/fib_rules.h>
35
36 struct fib4_rule {
37         struct fib_rule         common;
38         u8                      dst_len;
39         u8                      src_len;
40         u8                      tos;
41         __be32                  src;
42         __be32                  srcmask;
43         __be32                  dst;
44         __be32                  dstmask;
45 #ifdef CONFIG_IP_ROUTE_CLASSID
46         u32                     tclassid;
47 #endif
48 };
49
50 int __fib_lookup(struct net *net, struct flowi4 *flp, struct fib_result *res)
51 {
52         struct fib_lookup_arg arg = {
53                 .result = res,
54                 .flags = FIB_LOOKUP_NOREF,
55         };
56         int err;
57
58         err = fib_rules_lookup(net->ipv4.rules_ops, flowi4_to_flowi(flp), 0, &arg);
59 #ifdef CONFIG_IP_ROUTE_CLASSID
60         if (arg.rule)
61                 res->tclassid = ((struct fib4_rule *)arg.rule)->tclassid;
62         else
63                 res->tclassid = 0;
64 #endif
65
66         if (err == -ESRCH)
67                 err = -ENETUNREACH;
68
69         return err;
70 }
71 EXPORT_SYMBOL_GPL(__fib_lookup);
72
73 static int fib4_rule_action(struct fib_rule *rule, struct flowi *flp,
74                             int flags, struct fib_lookup_arg *arg)
75 {
76         int err = -EAGAIN;
77         struct fib_table *tbl;
78
79         switch (rule->action) {
80         case FR_ACT_TO_TBL:
81                 break;
82
83         case FR_ACT_UNREACHABLE:
84                 err = -ENETUNREACH;
85                 goto errout;
86
87         case FR_ACT_PROHIBIT:
88                 err = -EACCES;
89                 goto errout;
90
91         case FR_ACT_BLACKHOLE:
92         default:
93                 err = -EINVAL;
94                 goto errout;
95         }
96
97         tbl = fib_get_table(rule->fr_net, rule->table);
98         if (!tbl)
99                 goto errout;
100
101         err = fib_table_lookup(tbl, &flp->u.ip4, (struct fib_result *) arg->result, arg->flags);
102         if (err > 0)
103                 err = -EAGAIN;
104 errout:
105         return err;
106 }
107
108
109 static int fib4_rule_match(struct fib_rule *rule, struct flowi *fl, int flags)
110 {
111         struct fib4_rule *r = (struct fib4_rule *) rule;
112         struct flowi4 *fl4 = &fl->u.ip4;
113         __be32 daddr = fl4->daddr;
114         __be32 saddr = fl4->saddr;
115
116         if (((saddr ^ r->src) & r->srcmask) ||
117             ((daddr ^ r->dst) & r->dstmask))
118                 return 0;
119
120         if (r->tos && (r->tos != fl4->flowi4_tos))
121                 return 0;
122
123         return 1;
124 }
125
126 static struct fib_table *fib_empty_table(struct net *net)
127 {
128         u32 id;
129
130         for (id = 1; id <= RT_TABLE_MAX; id++)
131                 if (fib_get_table(net, id) == NULL)
132                         return fib_new_table(net, id);
133         return NULL;
134 }
135
136 static const struct nla_policy fib4_rule_policy[FRA_MAX+1] = {
137         FRA_GENERIC_POLICY,
138         [FRA_FLOW]      = { .type = NLA_U32 },
139 };
140
141 static int fib4_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
142                                struct fib_rule_hdr *frh,
143                                struct nlattr **tb)
144 {
145         struct net *net = sock_net(skb->sk);
146         int err = -EINVAL;
147         struct fib4_rule *rule4 = (struct fib4_rule *) rule;
148
149         if (frh->tos & ~IPTOS_TOS_MASK)
150                 goto errout;
151
152         if (rule->table == RT_TABLE_UNSPEC) {
153                 if (rule->action == FR_ACT_TO_TBL) {
154                         struct fib_table *table;
155
156                         table = fib_empty_table(net);
157                         if (table == NULL) {
158                                 err = -ENOBUFS;
159                                 goto errout;
160                         }
161
162                         rule->table = table->tb_id;
163                 }
164         }
165
166         if (frh->src_len)
167                 rule4->src = nla_get_be32(tb[FRA_SRC]);
168
169         if (frh->dst_len)
170                 rule4->dst = nla_get_be32(tb[FRA_DST]);
171
172 #ifdef CONFIG_IP_ROUTE_CLASSID
173         if (tb[FRA_FLOW]) {
174                 rule4->tclassid = nla_get_u32(tb[FRA_FLOW]);
175                 if (rule4->tclassid)
176                         net->ipv4.fib_num_tclassid_users++;
177         }
178 #endif
179
180         rule4->src_len = frh->src_len;
181         rule4->srcmask = inet_make_mask(rule4->src_len);
182         rule4->dst_len = frh->dst_len;
183         rule4->dstmask = inet_make_mask(rule4->dst_len);
184         rule4->tos = frh->tos;
185
186         net->ipv4.fib_has_custom_rules = true;
187         err = 0;
188 errout:
189         return err;
190 }
191
192 static void fib4_rule_delete(struct fib_rule *rule)
193 {
194         struct net *net = rule->fr_net;
195 #ifdef CONFIG_IP_ROUTE_CLASSID
196         struct fib4_rule *rule4 = (struct fib4_rule *) rule;
197
198         if (rule4->tclassid)
199                 net->ipv4.fib_num_tclassid_users--;
200 #endif
201         net->ipv4.fib_has_custom_rules = true;
202 }
203
204 static int fib4_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh,
205                              struct nlattr **tb)
206 {
207         struct fib4_rule *rule4 = (struct fib4_rule *) rule;
208
209         if (frh->src_len && (rule4->src_len != frh->src_len))
210                 return 0;
211
212         if (frh->dst_len && (rule4->dst_len != frh->dst_len))
213                 return 0;
214
215         if (frh->tos && (rule4->tos != frh->tos))
216                 return 0;
217
218 #ifdef CONFIG_IP_ROUTE_CLASSID
219         if (tb[FRA_FLOW] && (rule4->tclassid != nla_get_u32(tb[FRA_FLOW])))
220                 return 0;
221 #endif
222
223         if (frh->src_len && (rule4->src != nla_get_be32(tb[FRA_SRC])))
224                 return 0;
225
226         if (frh->dst_len && (rule4->dst != nla_get_be32(tb[FRA_DST])))
227                 return 0;
228
229         return 1;
230 }
231
232 static int fib4_rule_fill(struct fib_rule *rule, struct sk_buff *skb,
233                           struct fib_rule_hdr *frh)
234 {
235         struct fib4_rule *rule4 = (struct fib4_rule *) rule;
236
237         frh->dst_len = rule4->dst_len;
238         frh->src_len = rule4->src_len;
239         frh->tos = rule4->tos;
240
241         if ((rule4->dst_len &&
242              nla_put_be32(skb, FRA_DST, rule4->dst)) ||
243             (rule4->src_len &&
244              nla_put_be32(skb, FRA_SRC, rule4->src)))
245                 goto nla_put_failure;
246 #ifdef CONFIG_IP_ROUTE_CLASSID
247         if (rule4->tclassid &&
248             nla_put_u32(skb, FRA_FLOW, rule4->tclassid))
249                 goto nla_put_failure;
250 #endif
251         return 0;
252
253 nla_put_failure:
254         return -ENOBUFS;
255 }
256
257 static size_t fib4_rule_nlmsg_payload(struct fib_rule *rule)
258 {
259         return nla_total_size(4) /* dst */
260                + nla_total_size(4) /* src */
261                + nla_total_size(4); /* flow */
262 }
263
264 static void fib4_rule_flush_cache(struct fib_rules_ops *ops)
265 {
266         rt_cache_flush(ops->fro_net);
267 }
268
269 static const struct fib_rules_ops __net_initconst fib4_rules_ops_template = {
270         .family         = AF_INET,
271         .rule_size      = sizeof(struct fib4_rule),
272         .addr_size      = sizeof(u32),
273         .action         = fib4_rule_action,
274         .match          = fib4_rule_match,
275         .configure      = fib4_rule_configure,
276         .delete         = fib4_rule_delete,
277         .compare        = fib4_rule_compare,
278         .fill           = fib4_rule_fill,
279         .default_pref   = fib_default_rule_pref,
280         .nlmsg_payload  = fib4_rule_nlmsg_payload,
281         .flush_cache    = fib4_rule_flush_cache,
282         .nlgroup        = RTNLGRP_IPV4_RULE,
283         .policy         = fib4_rule_policy,
284         .owner          = THIS_MODULE,
285 };
286
287 static int fib_default_rules_init(struct fib_rules_ops *ops)
288 {
289         int err;
290
291         err = fib_default_rule_add(ops, 0, RT_TABLE_LOCAL, 0);
292         if (err < 0)
293                 return err;
294         err = fib_default_rule_add(ops, 0x7FFE, RT_TABLE_MAIN, 0);
295         if (err < 0)
296                 return err;
297         err = fib_default_rule_add(ops, 0x7FFF, RT_TABLE_DEFAULT, 0);
298         if (err < 0)
299                 return err;
300         return 0;
301 }
302
303 int __net_init fib4_rules_init(struct net *net)
304 {
305         int err;
306         struct fib_rules_ops *ops;
307
308         ops = fib_rules_register(&fib4_rules_ops_template, net);
309         if (IS_ERR(ops))
310                 return PTR_ERR(ops);
311
312         err = fib_default_rules_init(ops);
313         if (err < 0)
314                 goto fail;
315         net->ipv4.rules_ops = ops;
316         net->ipv4.fib_has_custom_rules = false;
317         return 0;
318
319 fail:
320         /* also cleans all rules already added */
321         fib_rules_unregister(ops);
322         return err;
323 }
324
325 void __net_exit fib4_rules_exit(struct net *net)
326 {
327         fib_rules_unregister(net->ipv4.rules_ops);
328 }