igmp: Avoid zero delay when receiving odd mixture of IGMP queries
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / net / ipv4 / fib_rules.c
blob46339ba7a2d3372b47a5eaeaffa2a71774008d0e
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.
6 * IPv4 Forwarding Information Base: policy rules.
8 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
9 * Thomas Graf <tgraf@suug.ch>
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.
16 * Fixes:
17 * Rani Assaf : local_rule cannot be deleted
18 * Marc Boucher : routing by fwmark
21 #include <linux/types.h>
22 #include <linux/kernel.h>
23 #include <linux/netdevice.h>
24 #include <linux/netlink.h>
25 #include <linux/inetdevice.h>
26 #include <linux/init.h>
27 #include <linux/list.h>
28 #include <linux/rcupdate.h>
29 #include <linux/export.h>
30 #include <net/ip.h>
31 #include <net/route.h>
32 #include <net/tcp.h>
33 #include <net/ip_fib.h>
34 #include <net/fib_rules.h>
36 struct fib4_rule {
37 struct fib_rule common;
38 u8 dst_len;
39 u8 src_len;
40 u8 tos;
41 __be32 src;
42 __be32 srcmask;
43 __be32 dst;
44 __be32 dstmask;
45 #ifdef CONFIG_IP_ROUTE_CLASSID
46 u32 tclassid;
47 #endif
50 #ifdef CONFIG_IP_ROUTE_CLASSID
51 u32 fib_rules_tclass(const struct fib_result *res)
53 return res->r ? ((struct fib4_rule *) res->r)->tclassid : 0;
55 #endif
57 int fib_lookup(struct net *net, struct flowi4 *flp, struct fib_result *res)
59 struct fib_lookup_arg arg = {
60 .result = res,
61 .flags = FIB_LOOKUP_NOREF,
63 int err;
65 err = fib_rules_lookup(net->ipv4.rules_ops, flowi4_to_flowi(flp), 0, &arg);
66 res->r = arg.rule;
68 return err;
71 static int fib4_rule_action(struct fib_rule *rule, struct flowi *flp,
72 int flags, struct fib_lookup_arg *arg)
74 int err = -EAGAIN;
75 struct fib_table *tbl;
77 switch (rule->action) {
78 case FR_ACT_TO_TBL:
79 break;
81 case FR_ACT_UNREACHABLE:
82 err = -ENETUNREACH;
83 goto errout;
85 case FR_ACT_PROHIBIT:
86 err = -EACCES;
87 goto errout;
89 case FR_ACT_BLACKHOLE:
90 default:
91 err = -EINVAL;
92 goto errout;
95 tbl = fib_get_table(rule->fr_net, rule->table);
96 if (!tbl)
97 goto errout;
99 err = fib_table_lookup(tbl, &flp->u.ip4, (struct fib_result *) arg->result, arg->flags);
100 if (err > 0)
101 err = -EAGAIN;
102 errout:
103 return err;
107 static int fib4_rule_match(struct fib_rule *rule, struct flowi *fl, int flags)
109 struct fib4_rule *r = (struct fib4_rule *) rule;
110 struct flowi4 *fl4 = &fl->u.ip4;
111 __be32 daddr = fl4->daddr;
112 __be32 saddr = fl4->saddr;
114 if (((saddr ^ r->src) & r->srcmask) ||
115 ((daddr ^ r->dst) & r->dstmask))
116 return 0;
118 if (r->tos && (r->tos != fl4->flowi4_tos))
119 return 0;
121 return 1;
124 static struct fib_table *fib_empty_table(struct net *net)
126 u32 id;
128 for (id = 1; id <= RT_TABLE_MAX; id++)
129 if (fib_get_table(net, id) == NULL)
130 return fib_new_table(net, id);
131 return NULL;
134 static const struct nla_policy fib4_rule_policy[FRA_MAX+1] = {
135 FRA_GENERIC_POLICY,
136 [FRA_FLOW] = { .type = NLA_U32 },
139 static int fib4_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
140 struct fib_rule_hdr *frh,
141 struct nlattr **tb)
143 struct net *net = sock_net(skb->sk);
144 int err = -EINVAL;
145 struct fib4_rule *rule4 = (struct fib4_rule *) rule;
147 if (frh->tos & ~IPTOS_TOS_MASK)
148 goto errout;
150 if (rule->table == RT_TABLE_UNSPEC) {
151 if (rule->action == FR_ACT_TO_TBL) {
152 struct fib_table *table;
154 table = fib_empty_table(net);
155 if (table == NULL) {
156 err = -ENOBUFS;
157 goto errout;
160 rule->table = table->tb_id;
164 if (frh->src_len)
165 rule4->src = nla_get_be32(tb[FRA_SRC]);
167 if (frh->dst_len)
168 rule4->dst = nla_get_be32(tb[FRA_DST]);
170 #ifdef CONFIG_IP_ROUTE_CLASSID
171 if (tb[FRA_FLOW])
172 rule4->tclassid = nla_get_u32(tb[FRA_FLOW]);
173 #endif
175 rule4->src_len = frh->src_len;
176 rule4->srcmask = inet_make_mask(rule4->src_len);
177 rule4->dst_len = frh->dst_len;
178 rule4->dstmask = inet_make_mask(rule4->dst_len);
179 rule4->tos = frh->tos;
181 err = 0;
182 errout:
183 return err;
186 static int fib4_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh,
187 struct nlattr **tb)
189 struct fib4_rule *rule4 = (struct fib4_rule *) rule;
191 if (frh->src_len && (rule4->src_len != frh->src_len))
192 return 0;
194 if (frh->dst_len && (rule4->dst_len != frh->dst_len))
195 return 0;
197 if (frh->tos && (rule4->tos != frh->tos))
198 return 0;
200 #ifdef CONFIG_IP_ROUTE_CLASSID
201 if (tb[FRA_FLOW] && (rule4->tclassid != nla_get_u32(tb[FRA_FLOW])))
202 return 0;
203 #endif
205 if (frh->src_len && (rule4->src != nla_get_be32(tb[FRA_SRC])))
206 return 0;
208 if (frh->dst_len && (rule4->dst != nla_get_be32(tb[FRA_DST])))
209 return 0;
211 return 1;
214 static int fib4_rule_fill(struct fib_rule *rule, struct sk_buff *skb,
215 struct fib_rule_hdr *frh)
217 struct fib4_rule *rule4 = (struct fib4_rule *) rule;
219 frh->dst_len = rule4->dst_len;
220 frh->src_len = rule4->src_len;
221 frh->tos = rule4->tos;
223 if (rule4->dst_len)
224 NLA_PUT_BE32(skb, FRA_DST, rule4->dst);
226 if (rule4->src_len)
227 NLA_PUT_BE32(skb, FRA_SRC, rule4->src);
229 #ifdef CONFIG_IP_ROUTE_CLASSID
230 if (rule4->tclassid)
231 NLA_PUT_U32(skb, FRA_FLOW, rule4->tclassid);
232 #endif
233 return 0;
235 nla_put_failure:
236 return -ENOBUFS;
239 static size_t fib4_rule_nlmsg_payload(struct fib_rule *rule)
241 return nla_total_size(4) /* dst */
242 + nla_total_size(4) /* src */
243 + nla_total_size(4); /* flow */
246 static void fib4_rule_flush_cache(struct fib_rules_ops *ops)
248 rt_cache_flush(ops->fro_net, -1);
251 static const struct fib_rules_ops __net_initdata fib4_rules_ops_template = {
252 .family = AF_INET,
253 .rule_size = sizeof(struct fib4_rule),
254 .addr_size = sizeof(u32),
255 .action = fib4_rule_action,
256 .match = fib4_rule_match,
257 .configure = fib4_rule_configure,
258 .compare = fib4_rule_compare,
259 .fill = fib4_rule_fill,
260 .default_pref = fib_default_rule_pref,
261 .nlmsg_payload = fib4_rule_nlmsg_payload,
262 .flush_cache = fib4_rule_flush_cache,
263 .nlgroup = RTNLGRP_IPV4_RULE,
264 .policy = fib4_rule_policy,
265 .owner = THIS_MODULE,
268 static int fib_default_rules_init(struct fib_rules_ops *ops)
270 int err;
272 err = fib_default_rule_add(ops, 0, RT_TABLE_LOCAL, 0);
273 if (err < 0)
274 return err;
275 err = fib_default_rule_add(ops, 0x7FFE, RT_TABLE_MAIN, 0);
276 if (err < 0)
277 return err;
278 err = fib_default_rule_add(ops, 0x7FFF, RT_TABLE_DEFAULT, 0);
279 if (err < 0)
280 return err;
281 return 0;
284 int __net_init fib4_rules_init(struct net *net)
286 int err;
287 struct fib_rules_ops *ops;
289 ops = fib_rules_register(&fib4_rules_ops_template, net);
290 if (IS_ERR(ops))
291 return PTR_ERR(ops);
293 err = fib_default_rules_init(ops);
294 if (err < 0)
295 goto fail;
296 net->ipv4.rules_ops = ops;
297 return 0;
299 fail:
300 /* also cleans all rules already added */
301 fib_rules_unregister(ops);
302 return err;
305 void __net_exit fib4_rules_exit(struct net *net)
307 fib_rules_unregister(net->ipv4.rules_ops);