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