]>
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> |
e1ef4bf2 | 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: | |
17 | * Rani Assaf : local_rule cannot be deleted | |
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> | |
1da177e4 | 29 | #include <net/ip.h> |
1da177e4 LT |
30 | #include <net/route.h> |
31 | #include <net/tcp.h> | |
1da177e4 | 32 | #include <net/ip_fib.h> |
e1ef4bf2 | 33 | #include <net/fib_rules.h> |
1da177e4 | 34 | |
e1ef4bf2 | 35 | static struct fib_rules_ops fib4_rules_ops; |
1da177e4 | 36 | |
e1ef4bf2 | 37 | struct fib4_rule |
1da177e4 | 38 | { |
e1ef4bf2 TG |
39 | struct fib_rule common; |
40 | u8 dst_len; | |
41 | u8 src_len; | |
42 | u8 tos; | |
81f7bf6c AV |
43 | __be32 src; |
44 | __be32 srcmask; | |
45 | __be32 dst; | |
46 | __be32 dstmask; | |
1da177e4 | 47 | #ifdef CONFIG_NET_CLS_ROUTE |
e1ef4bf2 | 48 | u32 tclassid; |
1da177e4 | 49 | #endif |
1da177e4 LT |
50 | }; |
51 | ||
e1ef4bf2 TG |
52 | static struct fib4_rule default_rule = { |
53 | .common = { | |
54 | .refcnt = ATOMIC_INIT(2), | |
55 | .pref = 0x7FFF, | |
56 | .table = RT_TABLE_DEFAULT, | |
57 | .action = FR_ACT_TO_TBL, | |
58 | }, | |
1da177e4 LT |
59 | }; |
60 | ||
e1ef4bf2 TG |
61 | static struct fib4_rule main_rule = { |
62 | .common = { | |
63 | .refcnt = ATOMIC_INIT(2), | |
64 | .pref = 0x7FFE, | |
65 | .table = RT_TABLE_MAIN, | |
66 | .action = FR_ACT_TO_TBL, | |
67 | }, | |
1da177e4 LT |
68 | }; |
69 | ||
e1ef4bf2 TG |
70 | static struct fib4_rule local_rule = { |
71 | .common = { | |
72 | .refcnt = ATOMIC_INIT(2), | |
73 | .table = RT_TABLE_LOCAL, | |
74 | .action = FR_ACT_TO_TBL, | |
75 | .flags = FIB_RULE_PERMANENT, | |
76 | }, | |
1da177e4 LT |
77 | }; |
78 | ||
e1ef4bf2 TG |
79 | #ifdef CONFIG_NET_CLS_ROUTE |
80 | u32 fib_rules_tclass(struct fib_result *res) | |
81 | { | |
82 | return res->r ? ((struct fib4_rule *) res->r)->tclassid : 0; | |
83 | } | |
84 | #endif | |
7b204afd | 85 | |
e1ef4bf2 TG |
86 | int fib_lookup(struct flowi *flp, struct fib_result *res) |
87 | { | |
88 | struct fib_lookup_arg arg = { | |
89 | .result = res, | |
90 | }; | |
91 | int err; | |
1da177e4 | 92 | |
e1ef4bf2 TG |
93 | err = fib_rules_lookup(&fib4_rules_ops, flp, 0, &arg); |
94 | res->r = arg.rule; | |
a5cdc030 | 95 | |
e1ef4bf2 TG |
96 | return err; |
97 | } | |
98 | ||
8ce11e6a AB |
99 | static int fib4_rule_action(struct fib_rule *rule, struct flowi *flp, |
100 | int flags, struct fib_lookup_arg *arg) | |
1da177e4 | 101 | { |
e1ef4bf2 TG |
102 | int err = -EAGAIN; |
103 | struct fib_table *tbl; | |
104 | ||
105 | switch (rule->action) { | |
106 | case FR_ACT_TO_TBL: | |
107 | break; | |
108 | ||
109 | case FR_ACT_UNREACHABLE: | |
110 | err = -ENETUNREACH; | |
111 | goto errout; | |
112 | ||
113 | case FR_ACT_PROHIBIT: | |
114 | err = -EACCES; | |
115 | goto errout; | |
116 | ||
117 | case FR_ACT_BLACKHOLE: | |
118 | default: | |
119 | err = -EINVAL; | |
120 | goto errout; | |
1da177e4 | 121 | } |
e1ef4bf2 TG |
122 | |
123 | if ((tbl = fib_get_table(rule->table)) == NULL) | |
124 | goto errout; | |
125 | ||
126 | err = tbl->tb_lookup(tbl, flp, (struct fib_result *) arg->result); | |
127 | if (err > 0) | |
128 | err = -EAGAIN; | |
129 | errout: | |
1da177e4 LT |
130 | return err; |
131 | } | |
132 | ||
e1ef4bf2 TG |
133 | |
134 | void fib_select_default(const struct flowi *flp, struct fib_result *res) | |
135 | { | |
136 | if (res->r && res->r->action == FR_ACT_TO_TBL && | |
137 | FIB_RES_GW(*res) && FIB_RES_NH(*res).nh_scope == RT_SCOPE_LINK) { | |
138 | struct fib_table *tb; | |
139 | if ((tb = fib_get_table(res->r->table)) != NULL) | |
140 | tb->tb_select_default(tb, flp, res); | |
141 | } | |
142 | } | |
143 | ||
144 | static int fib4_rule_match(struct fib_rule *rule, struct flowi *fl, int flags) | |
145 | { | |
146 | struct fib4_rule *r = (struct fib4_rule *) rule; | |
81f7bf6c AV |
147 | __be32 daddr = fl->fl4_dst; |
148 | __be32 saddr = fl->fl4_src; | |
e1ef4bf2 TG |
149 | |
150 | if (((saddr ^ r->src) & r->srcmask) || | |
151 | ((daddr ^ r->dst) & r->dstmask)) | |
152 | return 0; | |
153 | ||
154 | if (r->tos && (r->tos != fl->fl4_tos)) | |
155 | return 0; | |
156 | ||
e1ef4bf2 TG |
157 | return 1; |
158 | } | |
1da177e4 LT |
159 | |
160 | static struct fib_table *fib_empty_table(void) | |
161 | { | |
2dfe55b4 | 162 | u32 id; |
1da177e4 LT |
163 | |
164 | for (id = 1; id <= RT_TABLE_MAX; id++) | |
1af5a8c4 PM |
165 | if (fib_get_table(id) == NULL) |
166 | return fib_new_table(id); | |
1da177e4 LT |
167 | return NULL; |
168 | } | |
169 | ||
ef7c79ed | 170 | static const struct nla_policy fib4_rule_policy[FRA_MAX+1] = { |
1f6c9557 | 171 | FRA_GENERIC_POLICY, |
e1ef4bf2 TG |
172 | [FRA_FLOW] = { .type = NLA_U32 }, |
173 | }; | |
7b204afd | 174 | |
e1ef4bf2 TG |
175 | static int fib4_rule_configure(struct fib_rule *rule, struct sk_buff *skb, |
176 | struct nlmsghdr *nlh, struct fib_rule_hdr *frh, | |
177 | struct nlattr **tb) | |
1da177e4 | 178 | { |
e1ef4bf2 TG |
179 | int err = -EINVAL; |
180 | struct fib4_rule *rule4 = (struct fib4_rule *) rule; | |
1da177e4 | 181 | |
e1701c68 | 182 | if (frh->tos & ~IPTOS_TOS_MASK) |
e1ef4bf2 | 183 | goto errout; |
7b204afd | 184 | |
e1ef4bf2 TG |
185 | if (rule->table == RT_TABLE_UNSPEC) { |
186 | if (rule->action == FR_ACT_TO_TBL) { | |
187 | struct fib_table *table; | |
1da177e4 | 188 | |
e1ef4bf2 TG |
189 | table = fib_empty_table(); |
190 | if (table == NULL) { | |
191 | err = -ENOBUFS; | |
192 | goto errout; | |
193 | } | |
1da177e4 | 194 | |
e1ef4bf2 | 195 | rule->table = table->tb_id; |
1da177e4 LT |
196 | } |
197 | } | |
198 | ||
e1701c68 | 199 | if (frh->src_len) |
45d60b9e | 200 | rule4->src = nla_get_be32(tb[FRA_SRC]); |
7b204afd | 201 | |
e1701c68 | 202 | if (frh->dst_len) |
45d60b9e | 203 | rule4->dst = nla_get_be32(tb[FRA_DST]); |
7b204afd | 204 | |
1da177e4 | 205 | #ifdef CONFIG_NET_CLS_ROUTE |
e1ef4bf2 TG |
206 | if (tb[FRA_FLOW]) |
207 | rule4->tclassid = nla_get_u32(tb[FRA_FLOW]); | |
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 | |
e1ef4bf2 TG |
216 | err = 0; |
217 | errout: | |
218 | return err; | |
1da177e4 LT |
219 | } |
220 | ||
e1ef4bf2 TG |
221 | static int fib4_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh, |
222 | struct nlattr **tb) | |
1da177e4 | 223 | { |
e1ef4bf2 | 224 | struct fib4_rule *rule4 = (struct fib4_rule *) rule; |
1da177e4 | 225 | |
e1ef4bf2 TG |
226 | if (frh->src_len && (rule4->src_len != frh->src_len)) |
227 | return 0; | |
1da177e4 | 228 | |
e1ef4bf2 TG |
229 | if (frh->dst_len && (rule4->dst_len != frh->dst_len)) |
230 | return 0; | |
7b204afd | 231 | |
e1ef4bf2 TG |
232 | if (frh->tos && (rule4->tos != frh->tos)) |
233 | return 0; | |
7b204afd | 234 | |
e1ef4bf2 TG |
235 | #ifdef CONFIG_NET_CLS_ROUTE |
236 | if (tb[FRA_FLOW] && (rule4->tclassid != nla_get_u32(tb[FRA_FLOW]))) | |
237 | return 0; | |
238 | #endif | |
1da177e4 | 239 | |
e1701c68 | 240 | if (frh->src_len && (rule4->src != nla_get_be32(tb[FRA_SRC]))) |
e1ef4bf2 | 241 | return 0; |
1da177e4 | 242 | |
e1701c68 | 243 | if (frh->dst_len && (rule4->dst != nla_get_be32(tb[FRA_DST]))) |
e1ef4bf2 | 244 | return 0; |
1da177e4 | 245 | |
e1ef4bf2 | 246 | return 1; |
1da177e4 LT |
247 | } |
248 | ||
e1ef4bf2 TG |
249 | static int fib4_rule_fill(struct fib_rule *rule, struct sk_buff *skb, |
250 | struct nlmsghdr *nlh, struct fib_rule_hdr *frh) | |
251 | { | |
252 | struct fib4_rule *rule4 = (struct fib4_rule *) rule; | |
1da177e4 | 253 | |
e1ef4bf2 TG |
254 | frh->family = AF_INET; |
255 | frh->dst_len = rule4->dst_len; | |
256 | frh->src_len = rule4->src_len; | |
257 | frh->tos = rule4->tos; | |
1da177e4 | 258 | |
e1ef4bf2 | 259 | if (rule4->dst_len) |
45d60b9e | 260 | NLA_PUT_BE32(skb, FRA_DST, rule4->dst); |
e1ef4bf2 TG |
261 | |
262 | if (rule4->src_len) | |
45d60b9e | 263 | NLA_PUT_BE32(skb, FRA_SRC, rule4->src); |
e1ef4bf2 | 264 | |
1da177e4 | 265 | #ifdef CONFIG_NET_CLS_ROUTE |
e1ef4bf2 TG |
266 | if (rule4->tclassid) |
267 | NLA_PUT_U32(skb, FRA_FLOW, rule4->tclassid); | |
1da177e4 | 268 | #endif |
e1ef4bf2 | 269 | return 0; |
1da177e4 | 270 | |
e1ef4bf2 TG |
271 | nla_put_failure: |
272 | return -ENOBUFS; | |
1da177e4 LT |
273 | } |
274 | ||
e1ef4bf2 | 275 | static u32 fib4_rule_default_pref(void) |
a5cdc030 | 276 | { |
e1ef4bf2 TG |
277 | struct list_head *pos; |
278 | struct fib_rule *rule; | |
279 | ||
76c72d4f DL |
280 | if (!list_empty(&fib4_rules_ops.rules_list)) { |
281 | pos = fib4_rules_ops.rules_list.next; | |
282 | if (pos->next != &fib4_rules_ops.rules_list) { | |
e1ef4bf2 TG |
283 | rule = list_entry(pos->next, struct fib_rule, list); |
284 | if (rule->pref) | |
285 | return rule->pref - 1; | |
286 | } | |
a5cdc030 | 287 | } |
e1ef4bf2 TG |
288 | |
289 | return 0; | |
a5cdc030 PM |
290 | } |
291 | ||
339bf98f TG |
292 | static size_t fib4_rule_nlmsg_payload(struct fib_rule *rule) |
293 | { | |
294 | return nla_total_size(4) /* dst */ | |
295 | + nla_total_size(4) /* src */ | |
296 | + nla_total_size(4); /* flow */ | |
297 | } | |
298 | ||
73417f61 TG |
299 | static void fib4_rule_flush_cache(void) |
300 | { | |
4b19ca44 | 301 | rt_cache_flush(-1); |
73417f61 TG |
302 | } |
303 | ||
e1ef4bf2 TG |
304 | static struct fib_rules_ops fib4_rules_ops = { |
305 | .family = AF_INET, | |
306 | .rule_size = sizeof(struct fib4_rule), | |
e1701c68 | 307 | .addr_size = sizeof(u32), |
e1ef4bf2 TG |
308 | .action = fib4_rule_action, |
309 | .match = fib4_rule_match, | |
310 | .configure = fib4_rule_configure, | |
311 | .compare = fib4_rule_compare, | |
312 | .fill = fib4_rule_fill, | |
313 | .default_pref = fib4_rule_default_pref, | |
339bf98f | 314 | .nlmsg_payload = fib4_rule_nlmsg_payload, |
73417f61 | 315 | .flush_cache = fib4_rule_flush_cache, |
e1ef4bf2 TG |
316 | .nlgroup = RTNLGRP_IPV4_RULE, |
317 | .policy = fib4_rule_policy, | |
76c72d4f | 318 | .rules_list = LIST_HEAD_INIT(fib4_rules_ops.rules_list), |
e1ef4bf2 TG |
319 | .owner = THIS_MODULE, |
320 | }; | |
321 | ||
322 | void __init fib4_rules_init(void) | |
1da177e4 | 323 | { |
76c72d4f DL |
324 | list_add_tail(&local_rule.common.list, &fib4_rules_ops.rules_list); |
325 | list_add_tail(&main_rule.common.list, &fib4_rules_ops.rules_list); | |
326 | list_add_tail(&default_rule.common.list, &fib4_rules_ops.rules_list); | |
1da177e4 | 327 | |
e1ef4bf2 | 328 | fib_rules_register(&fib4_rules_ops); |
1da177e4 | 329 | } |