Merge tag 'omap-for-v3.13/more-fixes-for-merge-window-take2' of git://git.kernel...
[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
f4530fa5 50int __fib_lookup(struct net *net, struct flowi4 *flp, struct fib_result *res)
e1ef4bf2
TG
51{
52 struct fib_lookup_arg arg = {
53 .result = res,
ebc0ffae 54 .flags = FIB_LOOKUP_NOREF,
e1ef4bf2
TG
55 };
56 int err;
1da177e4 57
22bd5b9b 58 err = fib_rules_lookup(net->ipv4.rules_ops, flowi4_to_flowi(flp), 0, &arg);
85b91b03
DM
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
e1ef4bf2
TG
65 return err;
66}
f4530fa5 67EXPORT_SYMBOL_GPL(__fib_lookup);
e1ef4bf2 68
8ce11e6a
AB
69static int fib4_rule_action(struct fib_rule *rule, struct flowi *flp,
70 int flags, struct fib_lookup_arg *arg)
1da177e4 71{
e1ef4bf2
TG
72 int err = -EAGAIN;
73 struct fib_table *tbl;
74
75 switch (rule->action) {
76 case FR_ACT_TO_TBL:
77 break;
78
79 case FR_ACT_UNREACHABLE:
80 err = -ENETUNREACH;
81 goto errout;
82
83 case FR_ACT_PROHIBIT:
84 err = -EACCES;
85 goto errout;
86
87 case FR_ACT_BLACKHOLE:
88 default:
89 err = -EINVAL;
90 goto errout;
1da177e4 91 }
e1ef4bf2 92
6a31d2a9
ED
93 tbl = fib_get_table(rule->fr_net, rule->table);
94 if (!tbl)
e1ef4bf2
TG
95 goto errout;
96
22bd5b9b 97 err = fib_table_lookup(tbl, &flp->u.ip4, (struct fib_result *) arg->result, arg->flags);
e1ef4bf2
TG
98 if (err > 0)
99 err = -EAGAIN;
100errout:
1da177e4
LT
101 return err;
102}
103
7764a45a
ST
104static bool fib4_rule_suppress(struct fib_rule *rule, struct fib_lookup_arg *arg)
105{
6ef94cfa
ST
106 struct fib_result *result = (struct fib_result *) arg->result;
107 struct net_device *dev = result->fi->fib_dev;
108
7764a45a
ST
109 /* do not accept result if the route does
110 * not meet the required prefix length
111 */
73f5698e 112 if (result->prefixlen <= rule->suppress_prefixlen)
6ef94cfa
ST
113 goto suppress_route;
114
115 /* do not accept result if the route uses a device
116 * belonging to a forbidden interface group
117 */
118 if (rule->suppress_ifgroup != -1 && dev && dev->group == rule->suppress_ifgroup)
119 goto suppress_route;
120
7764a45a 121 return false;
6ef94cfa
ST
122
123suppress_route:
124 if (!(arg->flags & FIB_LOOKUP_NOREF))
125 fib_info_put(result->fi);
126 return true;
7764a45a 127}
e1ef4bf2 128
e1ef4bf2
TG
129static int fib4_rule_match(struct fib_rule *rule, struct flowi *fl, int flags)
130{
131 struct fib4_rule *r = (struct fib4_rule *) rule;
9ade2286
DM
132 struct flowi4 *fl4 = &fl->u.ip4;
133 __be32 daddr = fl4->daddr;
134 __be32 saddr = fl4->saddr;
e1ef4bf2
TG
135
136 if (((saddr ^ r->src) & r->srcmask) ||
137 ((daddr ^ r->dst) & r->dstmask))
138 return 0;
139
9ade2286 140 if (r->tos && (r->tos != fl4->flowi4_tos))
e1ef4bf2
TG
141 return 0;
142
e1ef4bf2
TG
143 return 1;
144}
1da177e4 145
8ad4942c 146static struct fib_table *fib_empty_table(struct net *net)
1da177e4 147{
2dfe55b4 148 u32 id;
1da177e4
LT
149
150 for (id = 1; id <= RT_TABLE_MAX; id++)
8ad4942c
DL
151 if (fib_get_table(net, id) == NULL)
152 return fib_new_table(net, id);
1da177e4
LT
153 return NULL;
154}
155
ef7c79ed 156static const struct nla_policy fib4_rule_policy[FRA_MAX+1] = {
1f6c9557 157 FRA_GENERIC_POLICY,
e1ef4bf2
TG
158 [FRA_FLOW] = { .type = NLA_U32 },
159};
7b204afd 160
e1ef4bf2 161static int fib4_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
8b3521ee 162 struct fib_rule_hdr *frh,
e1ef4bf2 163 struct nlattr **tb)
1da177e4 164{
3b1e0a65 165 struct net *net = sock_net(skb->sk);
e1ef4bf2
TG
166 int err = -EINVAL;
167 struct fib4_rule *rule4 = (struct fib4_rule *) rule;
1da177e4 168
e1701c68 169 if (frh->tos & ~IPTOS_TOS_MASK)
e1ef4bf2 170 goto errout;
7b204afd 171
e1ef4bf2
TG
172 if (rule->table == RT_TABLE_UNSPEC) {
173 if (rule->action == FR_ACT_TO_TBL) {
174 struct fib_table *table;
1da177e4 175
e4e4971c 176 table = fib_empty_table(net);
e1ef4bf2
TG
177 if (table == NULL) {
178 err = -ENOBUFS;
179 goto errout;
180 }
1da177e4 181
e1ef4bf2 182 rule->table = table->tb_id;
1da177e4
LT
183 }
184 }
185
e1701c68 186 if (frh->src_len)
45d60b9e 187 rule4->src = nla_get_be32(tb[FRA_SRC]);
7b204afd 188
e1701c68 189 if (frh->dst_len)
45d60b9e 190 rule4->dst = nla_get_be32(tb[FRA_DST]);
7b204afd 191
c7066f70 192#ifdef CONFIG_IP_ROUTE_CLASSID
7a9bc9b8 193 if (tb[FRA_FLOW]) {
e1ef4bf2 194 rule4->tclassid = nla_get_u32(tb[FRA_FLOW]);
7a9bc9b8 195 if (rule4->tclassid)
f4530fa5 196 net->ipv4.fib_num_tclassid_users++;
7a9bc9b8 197 }
1da177e4
LT
198#endif
199
e1ef4bf2
TG
200 rule4->src_len = frh->src_len;
201 rule4->srcmask = inet_make_mask(rule4->src_len);
202 rule4->dst_len = frh->dst_len;
203 rule4->dstmask = inet_make_mask(rule4->dst_len);
204 rule4->tos = frh->tos;
7b204afd 205
f4530fa5 206 net->ipv4.fib_has_custom_rules = true;
e1ef4bf2
TG
207 err = 0;
208errout:
209 return err;
1da177e4
LT
210}
211
7a9bc9b8
DM
212static void fib4_rule_delete(struct fib_rule *rule)
213{
f4530fa5 214 struct net *net = rule->fr_net;
7a9bc9b8
DM
215#ifdef CONFIG_IP_ROUTE_CLASSID
216 struct fib4_rule *rule4 = (struct fib4_rule *) rule;
217
218 if (rule4->tclassid)
f4530fa5 219 net->ipv4.fib_num_tclassid_users--;
7a9bc9b8 220#endif
f4530fa5 221 net->ipv4.fib_has_custom_rules = true;
7a9bc9b8
DM
222}
223
e1ef4bf2
TG
224static int fib4_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh,
225 struct nlattr **tb)
1da177e4 226{
e1ef4bf2 227 struct fib4_rule *rule4 = (struct fib4_rule *) rule;
1da177e4 228
e1ef4bf2
TG
229 if (frh->src_len && (rule4->src_len != frh->src_len))
230 return 0;
1da177e4 231
e1ef4bf2
TG
232 if (frh->dst_len && (rule4->dst_len != frh->dst_len))
233 return 0;
7b204afd 234
e1ef4bf2
TG
235 if (frh->tos && (rule4->tos != frh->tos))
236 return 0;
7b204afd 237
c7066f70 238#ifdef CONFIG_IP_ROUTE_CLASSID
e1ef4bf2
TG
239 if (tb[FRA_FLOW] && (rule4->tclassid != nla_get_u32(tb[FRA_FLOW])))
240 return 0;
241#endif
1da177e4 242
e1701c68 243 if (frh->src_len && (rule4->src != nla_get_be32(tb[FRA_SRC])))
e1ef4bf2 244 return 0;
1da177e4 245
e1701c68 246 if (frh->dst_len && (rule4->dst != nla_get_be32(tb[FRA_DST])))
e1ef4bf2 247 return 0;
1da177e4 248
e1ef4bf2 249 return 1;
1da177e4
LT
250}
251
e1ef4bf2 252static int fib4_rule_fill(struct fib_rule *rule, struct sk_buff *skb,
04af8cf6 253 struct fib_rule_hdr *frh)
e1ef4bf2
TG
254{
255 struct fib4_rule *rule4 = (struct fib4_rule *) rule;
1da177e4 256
e1ef4bf2
TG
257 frh->dst_len = rule4->dst_len;
258 frh->src_len = rule4->src_len;
259 frh->tos = rule4->tos;
1da177e4 260
f3756b79
DM
261 if ((rule4->dst_len &&
262 nla_put_be32(skb, FRA_DST, rule4->dst)) ||
263 (rule4->src_len &&
264 nla_put_be32(skb, FRA_SRC, rule4->src)))
265 goto nla_put_failure;
c7066f70 266#ifdef CONFIG_IP_ROUTE_CLASSID
f3756b79
DM
267 if (rule4->tclassid &&
268 nla_put_u32(skb, FRA_FLOW, rule4->tclassid))
269 goto nla_put_failure;
1da177e4 270#endif
e1ef4bf2 271 return 0;
1da177e4 272
e1ef4bf2
TG
273nla_put_failure:
274 return -ENOBUFS;
1da177e4
LT
275}
276
339bf98f
TG
277static size_t fib4_rule_nlmsg_payload(struct fib_rule *rule)
278{
279 return nla_total_size(4) /* dst */
280 + nla_total_size(4) /* src */
281 + nla_total_size(4); /* flow */
282}
283
ae299fc0 284static void fib4_rule_flush_cache(struct fib_rules_ops *ops)
73417f61 285{
bafa6d9d 286 rt_cache_flush(ops->fro_net);
73417f61
TG
287}
288
04a6f82c 289static const struct fib_rules_ops __net_initconst fib4_rules_ops_template = {
25239cee 290 .family = AF_INET,
e1ef4bf2 291 .rule_size = sizeof(struct fib4_rule),
e1701c68 292 .addr_size = sizeof(u32),
e1ef4bf2 293 .action = fib4_rule_action,
7764a45a 294 .suppress = fib4_rule_suppress,
e1ef4bf2
TG
295 .match = fib4_rule_match,
296 .configure = fib4_rule_configure,
7a9bc9b8 297 .delete = fib4_rule_delete,
e1ef4bf2
TG
298 .compare = fib4_rule_compare,
299 .fill = fib4_rule_fill,
d8a566be 300 .default_pref = fib_default_rule_pref,
339bf98f 301 .nlmsg_payload = fib4_rule_nlmsg_payload,
73417f61 302 .flush_cache = fib4_rule_flush_cache,
e1ef4bf2
TG
303 .nlgroup = RTNLGRP_IPV4_RULE,
304 .policy = fib4_rule_policy,
e1ef4bf2
TG
305 .owner = THIS_MODULE,
306};
307
e4e4971c 308static int fib_default_rules_init(struct fib_rules_ops *ops)
1da177e4 309{
2994c638
DL
310 int err;
311
5adef180 312 err = fib_default_rule_add(ops, 0, RT_TABLE_LOCAL, 0);
2994c638
DL
313 if (err < 0)
314 return err;
e4e4971c 315 err = fib_default_rule_add(ops, 0x7FFE, RT_TABLE_MAIN, 0);
2994c638
DL
316 if (err < 0)
317 return err;
e4e4971c 318 err = fib_default_rule_add(ops, 0x7FFF, RT_TABLE_DEFAULT, 0);
2994c638
DL
319 if (err < 0)
320 return err;
321 return 0;
322}
1da177e4 323
7b1a74fd 324int __net_init fib4_rules_init(struct net *net)
2994c638 325{
dbb50165 326 int err;
e4e4971c
DL
327 struct fib_rules_ops *ops;
328
e9c5158a
EB
329 ops = fib_rules_register(&fib4_rules_ops_template, net);
330 if (IS_ERR(ops))
331 return PTR_ERR(ops);
dbb50165 332
e4e4971c 333 err = fib_default_rules_init(ops);
dbb50165
DL
334 if (err < 0)
335 goto fail;
e4e4971c 336 net->ipv4.rules_ops = ops;
f4530fa5 337 net->ipv4.fib_has_custom_rules = false;
dbb50165
DL
338 return 0;
339
340fail:
341 /* also cleans all rules already added */
9e3a5487 342 fib_rules_unregister(ops);
dbb50165 343 return err;
1da177e4 344}
7b1a74fd
DL
345
346void __net_exit fib4_rules_exit(struct net *net)
347{
9e3a5487 348 fib_rules_unregister(net->ipv4.rules_ops);
7b1a74fd 349}
This page took 0.891212 seconds and 5 git commands to generate.