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