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