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