KVM: SVM: Intercept RDPMC
[deliverable/linux.git] / net / ipv4 / fib_rules.c
CommitLineData
1da177e4
LT
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 *
1da177e4 8 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
6a31d2a9 9 * Thomas Graf <tgraf@suug.ch>
1da177e4
LT
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:
6a31d2a9 17 * Rani Assaf : local_rule cannot be deleted
1da177e4
LT
18 * Marc Boucher : routing by fwmark
19 */
20
1da177e4
LT
21#include <linux/types.h>
22#include <linux/kernel.h>
1da177e4 23#include <linux/netdevice.h>
1da177e4 24#include <linux/netlink.h>
e1ef4bf2 25#include <linux/inetdevice.h>
1da177e4 26#include <linux/init.h>
7b204afd
RO
27#include <linux/list.h>
28#include <linux/rcupdate.h>
bc3b2d7f 29#include <linux/export.h>
1da177e4 30#include <net/ip.h>
1da177e4
LT
31#include <net/route.h>
32#include <net/tcp.h>
1da177e4 33#include <net/ip_fib.h>
e1ef4bf2 34#include <net/fib_rules.h>
1da177e4 35
6a31d2a9 36struct fib4_rule {
e1ef4bf2
TG
37 struct fib_rule common;
38 u8 dst_len;
39 u8 src_len;
40 u8 tos;
81f7bf6c
AV
41 __be32 src;
42 __be32 srcmask;
43 __be32 dst;
44 __be32 dstmask;
c7066f70 45#ifdef CONFIG_IP_ROUTE_CLASSID
e1ef4bf2 46 u32 tclassid;
1da177e4 47#endif
1da177e4
LT
48};
49
c7066f70 50#ifdef CONFIG_IP_ROUTE_CLASSID
982721f3 51u32 fib_rules_tclass(const struct fib_result *res)
e1ef4bf2
TG
52{
53 return res->r ? ((struct fib4_rule *) res->r)->tclassid : 0;
54}
55#endif
7b204afd 56
22bd5b9b 57int fib_lookup(struct net *net, struct flowi4 *flp, struct fib_result *res)
e1ef4bf2
TG
58{
59 struct fib_lookup_arg arg = {
60 .result = res,
ebc0ffae 61 .flags = FIB_LOOKUP_NOREF,
e1ef4bf2
TG
62 };
63 int err;
1da177e4 64
22bd5b9b 65 err = fib_rules_lookup(net->ipv4.rules_ops, flowi4_to_flowi(flp), 0, &arg);
e1ef4bf2 66 res->r = arg.rule;
a5cdc030 67
e1ef4bf2
TG
68 return err;
69}
70
8ce11e6a
AB
71static int fib4_rule_action(struct fib_rule *rule, struct flowi *flp,
72 int flags, struct fib_lookup_arg *arg)
1da177e4 73{
e1ef4bf2
TG
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;
1da177e4 93 }
e1ef4bf2 94
6a31d2a9
ED
95 tbl = fib_get_table(rule->fr_net, rule->table);
96 if (!tbl)
e1ef4bf2
TG
97 goto errout;
98
22bd5b9b 99 err = fib_table_lookup(tbl, &flp->u.ip4, (struct fib_result *) arg->result, arg->flags);
e1ef4bf2
TG
100 if (err > 0)
101 err = -EAGAIN;
102errout:
1da177e4
LT
103 return err;
104}
105
e1ef4bf2 106
e1ef4bf2
TG
107static int fib4_rule_match(struct fib_rule *rule, struct flowi *fl, int flags)
108{
109 struct fib4_rule *r = (struct fib4_rule *) rule;
9ade2286
DM
110 struct flowi4 *fl4 = &fl->u.ip4;
111 __be32 daddr = fl4->daddr;
112 __be32 saddr = fl4->saddr;
e1ef4bf2
TG
113
114 if (((saddr ^ r->src) & r->srcmask) ||
115 ((daddr ^ r->dst) & r->dstmask))
116 return 0;
117
9ade2286 118 if (r->tos && (r->tos != fl4->flowi4_tos))
e1ef4bf2
TG
119 return 0;
120
e1ef4bf2
TG
121 return 1;
122}
1da177e4 123
8ad4942c 124static struct fib_table *fib_empty_table(struct net *net)
1da177e4 125{
2dfe55b4 126 u32 id;
1da177e4
LT
127
128 for (id = 1; id <= RT_TABLE_MAX; id++)
8ad4942c
DL
129 if (fib_get_table(net, id) == NULL)
130 return fib_new_table(net, id);
1da177e4
LT
131 return NULL;
132}
133
ef7c79ed 134static const struct nla_policy fib4_rule_policy[FRA_MAX+1] = {
1f6c9557 135 FRA_GENERIC_POLICY,
e1ef4bf2
TG
136 [FRA_FLOW] = { .type = NLA_U32 },
137};
7b204afd 138
e1ef4bf2 139static int fib4_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
8b3521ee 140 struct fib_rule_hdr *frh,
e1ef4bf2 141 struct nlattr **tb)
1da177e4 142{
3b1e0a65 143 struct net *net = sock_net(skb->sk);
e1ef4bf2
TG
144 int err = -EINVAL;
145 struct fib4_rule *rule4 = (struct fib4_rule *) rule;
1da177e4 146
e1701c68 147 if (frh->tos & ~IPTOS_TOS_MASK)
e1ef4bf2 148 goto errout;
7b204afd 149
e1ef4bf2
TG
150 if (rule->table == RT_TABLE_UNSPEC) {
151 if (rule->action == FR_ACT_TO_TBL) {
152 struct fib_table *table;
1da177e4 153
e4e4971c 154 table = fib_empty_table(net);
e1ef4bf2
TG
155 if (table == NULL) {
156 err = -ENOBUFS;
157 goto errout;
158 }
1da177e4 159
e1ef4bf2 160 rule->table = table->tb_id;
1da177e4
LT
161 }
162 }
163
e1701c68 164 if (frh->src_len)
45d60b9e 165 rule4->src = nla_get_be32(tb[FRA_SRC]);
7b204afd 166
e1701c68 167 if (frh->dst_len)
45d60b9e 168 rule4->dst = nla_get_be32(tb[FRA_DST]);
7b204afd 169
c7066f70 170#ifdef CONFIG_IP_ROUTE_CLASSID
e1ef4bf2
TG
171 if (tb[FRA_FLOW])
172 rule4->tclassid = nla_get_u32(tb[FRA_FLOW]);
1da177e4
LT
173#endif
174
e1ef4bf2
TG
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;
7b204afd 180
e1ef4bf2
TG
181 err = 0;
182errout:
183 return err;
1da177e4
LT
184}
185
e1ef4bf2
TG
186static int fib4_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh,
187 struct nlattr **tb)
1da177e4 188{
e1ef4bf2 189 struct fib4_rule *rule4 = (struct fib4_rule *) rule;
1da177e4 190
e1ef4bf2
TG
191 if (frh->src_len && (rule4->src_len != frh->src_len))
192 return 0;
1da177e4 193
e1ef4bf2
TG
194 if (frh->dst_len && (rule4->dst_len != frh->dst_len))
195 return 0;
7b204afd 196
e1ef4bf2
TG
197 if (frh->tos && (rule4->tos != frh->tos))
198 return 0;
7b204afd 199
c7066f70 200#ifdef CONFIG_IP_ROUTE_CLASSID
e1ef4bf2
TG
201 if (tb[FRA_FLOW] && (rule4->tclassid != nla_get_u32(tb[FRA_FLOW])))
202 return 0;
203#endif
1da177e4 204
e1701c68 205 if (frh->src_len && (rule4->src != nla_get_be32(tb[FRA_SRC])))
e1ef4bf2 206 return 0;
1da177e4 207
e1701c68 208 if (frh->dst_len && (rule4->dst != nla_get_be32(tb[FRA_DST])))
e1ef4bf2 209 return 0;
1da177e4 210
e1ef4bf2 211 return 1;
1da177e4
LT
212}
213
e1ef4bf2 214static int fib4_rule_fill(struct fib_rule *rule, struct sk_buff *skb,
04af8cf6 215 struct fib_rule_hdr *frh)
e1ef4bf2
TG
216{
217 struct fib4_rule *rule4 = (struct fib4_rule *) rule;
1da177e4 218
e1ef4bf2
TG
219 frh->dst_len = rule4->dst_len;
220 frh->src_len = rule4->src_len;
221 frh->tos = rule4->tos;
1da177e4 222
e1ef4bf2 223 if (rule4->dst_len)
45d60b9e 224 NLA_PUT_BE32(skb, FRA_DST, rule4->dst);
e1ef4bf2
TG
225
226 if (rule4->src_len)
45d60b9e 227 NLA_PUT_BE32(skb, FRA_SRC, rule4->src);
e1ef4bf2 228
c7066f70 229#ifdef CONFIG_IP_ROUTE_CLASSID
e1ef4bf2
TG
230 if (rule4->tclassid)
231 NLA_PUT_U32(skb, FRA_FLOW, rule4->tclassid);
1da177e4 232#endif
e1ef4bf2 233 return 0;
1da177e4 234
e1ef4bf2
TG
235nla_put_failure:
236 return -ENOBUFS;
1da177e4
LT
237}
238
339bf98f
TG
239static 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
ae299fc0 246static void fib4_rule_flush_cache(struct fib_rules_ops *ops)
73417f61 247{
ae299fc0 248 rt_cache_flush(ops->fro_net, -1);
73417f61
TG
249}
250
3d0c9c4e 251static const struct fib_rules_ops __net_initdata fib4_rules_ops_template = {
25239cee 252 .family = AF_INET,
e1ef4bf2 253 .rule_size = sizeof(struct fib4_rule),
e1701c68 254 .addr_size = sizeof(u32),
e1ef4bf2
TG
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,
d8a566be 260 .default_pref = fib_default_rule_pref,
339bf98f 261 .nlmsg_payload = fib4_rule_nlmsg_payload,
73417f61 262 .flush_cache = fib4_rule_flush_cache,
e1ef4bf2
TG
263 .nlgroup = RTNLGRP_IPV4_RULE,
264 .policy = fib4_rule_policy,
e1ef4bf2
TG
265 .owner = THIS_MODULE,
266};
267
e4e4971c 268static int fib_default_rules_init(struct fib_rules_ops *ops)
1da177e4 269{
2994c638
DL
270 int err;
271
5adef180 272 err = fib_default_rule_add(ops, 0, RT_TABLE_LOCAL, 0);
2994c638
DL
273 if (err < 0)
274 return err;
e4e4971c 275 err = fib_default_rule_add(ops, 0x7FFE, RT_TABLE_MAIN, 0);
2994c638
DL
276 if (err < 0)
277 return err;
e4e4971c 278 err = fib_default_rule_add(ops, 0x7FFF, RT_TABLE_DEFAULT, 0);
2994c638
DL
279 if (err < 0)
280 return err;
281 return 0;
282}
1da177e4 283
7b1a74fd 284int __net_init fib4_rules_init(struct net *net)
2994c638 285{
dbb50165 286 int err;
e4e4971c
DL
287 struct fib_rules_ops *ops;
288
e9c5158a
EB
289 ops = fib_rules_register(&fib4_rules_ops_template, net);
290 if (IS_ERR(ops))
291 return PTR_ERR(ops);
dbb50165 292
e4e4971c 293 err = fib_default_rules_init(ops);
dbb50165
DL
294 if (err < 0)
295 goto fail;
e4e4971c 296 net->ipv4.rules_ops = ops;
dbb50165
DL
297 return 0;
298
299fail:
300 /* also cleans all rules already added */
9e3a5487 301 fib_rules_unregister(ops);
dbb50165 302 return err;
1da177e4 303}
7b1a74fd
DL
304
305void __net_exit fib4_rules_exit(struct net *net)
306{
9e3a5487 307 fib_rules_unregister(net->ipv4.rules_ops);
7b1a74fd 308}
This page took 0.631471 seconds and 5 git commands to generate.