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