Driver core: allow to delay the uevent at device creation time
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / net / decnet / dn_rules.c
blobe32d0c3d5a96a3e3ec5b040ef4b1d365e68b4125
2 /*
3 * DECnet An implementation of the DECnet protocol suite for the LINUX
4 * operating system. DECnet is implemented using the BSD Socket
5 * interface as the means of communication with the user level.
7 * DECnet Routing Forwarding Information Base (Rules)
9 * Author: Steve Whitehouse <SteveW@ACM.org>
10 * Mostly copied from Alexey Kuznetsov's ipv4/fib_rules.c
13 * Changes:
14 * Steve Whitehouse <steve@chygwyn.com>
15 * Updated for Thomas Graf's generic rules
18 #include <linux/net.h>
19 #include <linux/init.h>
20 #include <linux/netlink.h>
21 #include <linux/rtnetlink.h>
22 #include <linux/netdevice.h>
23 #include <linux/spinlock.h>
24 #include <linux/list.h>
25 #include <linux/rcupdate.h>
26 #include <net/neighbour.h>
27 #include <net/dst.h>
28 #include <net/flow.h>
29 #include <net/fib_rules.h>
30 #include <net/dn.h>
31 #include <net/dn_fib.h>
32 #include <net/dn_neigh.h>
33 #include <net/dn_dev.h>
35 static struct fib_rules_ops dn_fib_rules_ops;
37 struct dn_fib_rule
39 struct fib_rule common;
40 unsigned char dst_len;
41 unsigned char src_len;
42 __le16 src;
43 __le16 srcmask;
44 __le16 dst;
45 __le16 dstmask;
46 __le16 srcmap;
47 u8 flags;
50 static struct dn_fib_rule default_rule = {
51 .common = {
52 .refcnt = ATOMIC_INIT(2),
53 .pref = 0x7fff,
54 .table = RT_TABLE_MAIN,
55 .action = FR_ACT_TO_TBL,
59 static LIST_HEAD(dn_fib_rules);
62 int dn_fib_lookup(struct flowi *flp, struct dn_fib_res *res)
64 struct fib_lookup_arg arg = {
65 .result = res,
67 int err;
69 err = fib_rules_lookup(&dn_fib_rules_ops, flp, 0, &arg);
70 res->r = arg.rule;
72 return err;
75 static int dn_fib_rule_action(struct fib_rule *rule, struct flowi *flp,
76 int flags, struct fib_lookup_arg *arg)
78 int err = -EAGAIN;
79 struct dn_fib_table *tbl;
81 switch(rule->action) {
82 case FR_ACT_TO_TBL:
83 break;
85 case FR_ACT_UNREACHABLE:
86 err = -ENETUNREACH;
87 goto errout;
89 case FR_ACT_PROHIBIT:
90 err = -EACCES;
91 goto errout;
93 case FR_ACT_BLACKHOLE:
94 default:
95 err = -EINVAL;
96 goto errout;
99 tbl = dn_fib_get_table(rule->table, 0);
100 if (tbl == NULL)
101 goto errout;
103 err = tbl->lookup(tbl, flp, (struct dn_fib_res *)arg->result);
104 if (err > 0)
105 err = -EAGAIN;
106 errout:
107 return err;
110 static struct nla_policy dn_fib_rule_policy[FRA_MAX+1] __read_mostly = {
111 FRA_GENERIC_POLICY,
112 [FRA_SRC] = { .type = NLA_U16 },
113 [FRA_DST] = { .type = NLA_U16 },
116 static int dn_fib_rule_match(struct fib_rule *rule, struct flowi *fl, int flags)
118 struct dn_fib_rule *r = (struct dn_fib_rule *)rule;
119 __le16 daddr = fl->fld_dst;
120 __le16 saddr = fl->fld_src;
122 if (((saddr ^ r->src) & r->srcmask) ||
123 ((daddr ^ r->dst) & r->dstmask))
124 return 0;
126 return 1;
129 static int dn_fib_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
130 struct nlmsghdr *nlh, struct fib_rule_hdr *frh,
131 struct nlattr **tb)
133 int err = -EINVAL;
134 struct dn_fib_rule *r = (struct dn_fib_rule *)rule;
136 if (frh->src_len > 16 || frh->dst_len > 16 || frh->tos)
137 goto errout;
139 if (rule->table == RT_TABLE_UNSPEC) {
140 if (rule->action == FR_ACT_TO_TBL) {
141 struct dn_fib_table *table;
143 table = dn_fib_empty_table();
144 if (table == NULL) {
145 err = -ENOBUFS;
146 goto errout;
149 rule->table = table->n;
153 if (tb[FRA_SRC])
154 r->src = nla_get_u16(tb[FRA_SRC]);
156 if (tb[FRA_DST])
157 r->dst = nla_get_u16(tb[FRA_DST]);
159 r->src_len = frh->src_len;
160 r->srcmask = dnet_make_mask(r->src_len);
161 r->dst_len = frh->dst_len;
162 r->dstmask = dnet_make_mask(r->dst_len);
163 err = 0;
164 errout:
165 return err;
168 static int dn_fib_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh,
169 struct nlattr **tb)
171 struct dn_fib_rule *r = (struct dn_fib_rule *)rule;
173 if (frh->src_len && (r->src_len != frh->src_len))
174 return 0;
176 if (frh->dst_len && (r->dst_len != frh->dst_len))
177 return 0;
179 if (tb[FRA_SRC] && (r->src != nla_get_u16(tb[FRA_SRC])))
180 return 0;
182 if (tb[FRA_DST] && (r->dst != nla_get_u16(tb[FRA_DST])))
183 return 0;
185 return 1;
188 unsigned dnet_addr_type(__le16 addr)
190 struct flowi fl = { .nl_u = { .dn_u = { .daddr = addr } } };
191 struct dn_fib_res res;
192 unsigned ret = RTN_UNICAST;
193 struct dn_fib_table *tb = dn_fib_get_table(RT_TABLE_LOCAL, 0);
195 res.r = NULL;
197 if (tb) {
198 if (!tb->lookup(tb, &fl, &res)) {
199 ret = res.type;
200 dn_fib_res_put(&res);
203 return ret;
206 static int dn_fib_rule_fill(struct fib_rule *rule, struct sk_buff *skb,
207 struct nlmsghdr *nlh, struct fib_rule_hdr *frh)
209 struct dn_fib_rule *r = (struct dn_fib_rule *)rule;
211 frh->family = AF_DECnet;
212 frh->dst_len = r->dst_len;
213 frh->src_len = r->src_len;
214 frh->tos = 0;
216 if (r->dst_len)
217 NLA_PUT_U16(skb, FRA_DST, r->dst);
218 if (r->src_len)
219 NLA_PUT_U16(skb, FRA_SRC, r->src);
221 return 0;
223 nla_put_failure:
224 return -ENOBUFS;
227 static u32 dn_fib_rule_default_pref(void)
229 struct list_head *pos;
230 struct fib_rule *rule;
232 if (!list_empty(&dn_fib_rules)) {
233 pos = dn_fib_rules.next;
234 if (pos->next != &dn_fib_rules) {
235 rule = list_entry(pos->next, struct fib_rule, list);
236 if (rule->pref)
237 return rule->pref - 1;
241 return 0;
244 int dn_fib_dump_rules(struct sk_buff *skb, struct netlink_callback *cb)
246 return fib_rules_dump(skb, cb, AF_DECnet);
249 static struct fib_rules_ops dn_fib_rules_ops = {
250 .family = AF_DECnet,
251 .rule_size = sizeof(struct dn_fib_rule),
252 .action = dn_fib_rule_action,
253 .match = dn_fib_rule_match,
254 .configure = dn_fib_rule_configure,
255 .compare = dn_fib_rule_compare,
256 .fill = dn_fib_rule_fill,
257 .default_pref = dn_fib_rule_default_pref,
258 .nlgroup = RTNLGRP_DECnet_RULE,
259 .policy = dn_fib_rule_policy,
260 .rules_list = &dn_fib_rules,
261 .owner = THIS_MODULE,
264 void __init dn_fib_rules_init(void)
266 list_add_tail(&default_rule.common.list, &dn_fib_rules);
267 fib_rules_register(&dn_fib_rules_ops);
270 void __exit dn_fib_rules_cleanup(void)
272 fib_rules_unregister(&dn_fib_rules_ops);