usb: gadget: r8a66597-udc: use dev_*() instead of printk()
[deliverable/linux.git] / net / decnet / dn_rules.c
CommitLineData
1da177e4
LT
1
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.
6 *
7 * DECnet Routing Forwarding Information Base (Rules)
8 *
9 * Author: Steve Whitehouse <SteveW@ACM.org>
10 * Mostly copied from Alexey Kuznetsov's ipv4/fib_rules.c
11 *
12 *
13 * Changes:
a8731cbf
SW
14 * Steve Whitehouse <steve@chygwyn.com>
15 * Updated for Thomas Graf's generic rules
1da177e4
LT
16 *
17 */
1da177e4 18#include <linux/net.h>
1da177e4 19#include <linux/init.h>
1da177e4
LT
20#include <linux/netlink.h>
21#include <linux/rtnetlink.h>
1da177e4 22#include <linux/netdevice.h>
1da177e4 23#include <linux/spinlock.h>
ecba320f
SW
24#include <linux/list.h>
25#include <linux/rcupdate.h>
1da177e4
LT
26#include <net/neighbour.h>
27#include <net/dst.h>
28#include <net/flow.h>
a8731cbf 29#include <net/fib_rules.h>
1da177e4
LT
30#include <net/dn.h>
31#include <net/dn_fib.h>
32#include <net/dn_neigh.h>
33#include <net/dn_dev.h>
73417f61 34#include <net/dn_route.h>
1da177e4 35
e9c5158a 36static struct fib_rules_ops *dn_fib_rules_ops;
a8731cbf 37
1da177e4
LT
38struct dn_fib_rule
39{
a8731cbf
SW
40 struct fib_rule common;
41 unsigned char dst_len;
42 unsigned char src_len;
43 __le16 src;
44 __le16 srcmask;
45 __le16 dst;
46 __le16 dstmask;
47 __le16 srcmap;
48 u8 flags;
1da177e4
LT
49};
50
1da177e4 51
bef55aeb 52int dn_fib_lookup(struct flowidn *flp, struct dn_fib_res *res)
1da177e4 53{
a8731cbf
SW
54 struct fib_lookup_arg arg = {
55 .result = res,
56 };
57 int err;
58
bef55aeb
DM
59 err = fib_rules_lookup(dn_fib_rules_ops,
60 flowidn_to_flowi(flp), 0, &arg);
a8731cbf 61 res->r = arg.rule;
1da177e4
LT
62
63 return err;
64}
65
2aa7f36c
AB
66static int dn_fib_rule_action(struct fib_rule *rule, struct flowi *flp,
67 int flags, struct fib_lookup_arg *arg)
ecba320f 68{
bef55aeb 69 struct flowidn *fld = &flp->u.dn;
a8731cbf
SW
70 int err = -EAGAIN;
71 struct dn_fib_table *tbl;
ecba320f 72
a8731cbf
SW
73 switch(rule->action) {
74 case FR_ACT_TO_TBL:
75 break;
76
77 case FR_ACT_UNREACHABLE:
78 err = -ENETUNREACH;
79 goto errout;
80
81 case FR_ACT_PROHIBIT:
82 err = -EACCES;
83 goto errout;
84
85 case FR_ACT_BLACKHOLE:
86 default:
87 err = -EINVAL;
88 goto errout;
1da177e4 89 }
a8731cbf
SW
90
91 tbl = dn_fib_get_table(rule->table, 0);
92 if (tbl == NULL)
93 goto errout;
94
bef55aeb 95 err = tbl->lookup(tbl, fld, (struct dn_fib_res *)arg->result);
a8731cbf
SW
96 if (err > 0)
97 err = -EAGAIN;
98errout:
99 return err;
1da177e4
LT
100}
101
ef7c79ed 102static const struct nla_policy dn_fib_rule_policy[FRA_MAX+1] = {
1f6c9557 103 FRA_GENERIC_POLICY,
a8731cbf 104};
1da177e4 105
a8731cbf 106static int dn_fib_rule_match(struct fib_rule *rule, struct flowi *fl, int flags)
1da177e4 107{
a8731cbf 108 struct dn_fib_rule *r = (struct dn_fib_rule *)rule;
bef55aeb
DM
109 struct flowidn *fld = &fl->u.dn;
110 __le16 daddr = fld->daddr;
111 __le16 saddr = fld->saddr;
a8731cbf
SW
112
113 if (((saddr ^ r->src) & r->srcmask) ||
114 ((daddr ^ r->dst) & r->dstmask))
115 return 0;
1da177e4 116
a8731cbf
SW
117 return 1;
118}
119
120static int dn_fib_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
8b3521ee 121 struct fib_rule_hdr *frh,
a8731cbf
SW
122 struct nlattr **tb)
123{
124 int err = -EINVAL;
125 struct dn_fib_rule *r = (struct dn_fib_rule *)rule;
126
e1701c68 127 if (frh->tos)
a8731cbf
SW
128 goto errout;
129
130 if (rule->table == RT_TABLE_UNSPEC) {
131 if (rule->action == FR_ACT_TO_TBL) {
132 struct dn_fib_table *table;
133
134 table = dn_fib_empty_table();
135 if (table == NULL) {
136 err = -ENOBUFS;
137 goto errout;
138 }
139
140 rule->table = table->n;
1da177e4
LT
141 }
142 }
143
e1701c68 144 if (frh->src_len)
2835fdfa 145 r->src = nla_get_le16(tb[FRA_SRC]);
ecba320f 146
e1701c68 147 if (frh->dst_len)
2835fdfa 148 r->dst = nla_get_le16(tb[FRA_DST]);
1da177e4 149
a8731cbf
SW
150 r->src_len = frh->src_len;
151 r->srcmask = dnet_make_mask(r->src_len);
152 r->dst_len = frh->dst_len;
153 r->dstmask = dnet_make_mask(r->dst_len);
154 err = 0;
155errout:
156 return err;
157}
1da177e4 158
a8731cbf
SW
159static int dn_fib_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh,
160 struct nlattr **tb)
1da177e4 161{
a8731cbf
SW
162 struct dn_fib_rule *r = (struct dn_fib_rule *)rule;
163
164 if (frh->src_len && (r->src_len != frh->src_len))
165 return 0;
1da177e4 166
a8731cbf
SW
167 if (frh->dst_len && (r->dst_len != frh->dst_len))
168 return 0;
ecba320f 169
e1701c68 170 if (frh->src_len && (r->src != nla_get_le16(tb[FRA_SRC])))
a8731cbf
SW
171 return 0;
172
e1701c68 173 if (frh->dst_len && (r->dst != nla_get_le16(tb[FRA_DST])))
a8731cbf 174 return 0;
1da177e4 175
a8731cbf 176 return 1;
1da177e4
LT
177}
178
c4ea94ab 179unsigned dnet_addr_type(__le16 addr)
1da177e4 180{
bef55aeb 181 struct flowidn fld = { .daddr = addr };
1da177e4
LT
182 struct dn_fib_res res;
183 unsigned ret = RTN_UNICAST;
abcab268 184 struct dn_fib_table *tb = dn_fib_get_table(RT_TABLE_LOCAL, 0);
1da177e4
LT
185
186 res.r = NULL;
187
188 if (tb) {
bef55aeb 189 if (!tb->lookup(tb, &fld, &res)) {
1da177e4
LT
190 ret = res.type;
191 dn_fib_res_put(&res);
192 }
193 }
194 return ret;
195}
196
a8731cbf 197static int dn_fib_rule_fill(struct fib_rule *rule, struct sk_buff *skb,
04af8cf6 198 struct fib_rule_hdr *frh)
1da177e4 199{
a8731cbf 200 struct dn_fib_rule *r = (struct dn_fib_rule *)rule;
1da177e4 201
a8731cbf
SW
202 frh->dst_len = r->dst_len;
203 frh->src_len = r->src_len;
204 frh->tos = 0;
1da177e4 205
a8731cbf 206 if (r->dst_len)
2835fdfa 207 NLA_PUT_LE16(skb, FRA_DST, r->dst);
a8731cbf 208 if (r->src_len)
2835fdfa 209 NLA_PUT_LE16(skb, FRA_SRC, r->src);
1da177e4 210
a8731cbf 211 return 0;
1da177e4 212
a8731cbf
SW
213nla_put_failure:
214 return -ENOBUFS;
1da177e4
LT
215}
216
ae299fc0 217static void dn_fib_rule_flush_cache(struct fib_rules_ops *ops)
73417f61 218{
4b19ca44 219 dn_rt_cache_flush(-1);
73417f61
TG
220}
221
3d0c9c4e 222static const struct fib_rules_ops __net_initdata dn_fib_rules_ops_template = {
25239cee 223 .family = AF_DECnet,
a8731cbf 224 .rule_size = sizeof(struct dn_fib_rule),
e1701c68 225 .addr_size = sizeof(u16),
a8731cbf
SW
226 .action = dn_fib_rule_action,
227 .match = dn_fib_rule_match,
228 .configure = dn_fib_rule_configure,
229 .compare = dn_fib_rule_compare,
230 .fill = dn_fib_rule_fill,
d8a566be 231 .default_pref = fib_default_rule_pref,
73417f61 232 .flush_cache = dn_fib_rule_flush_cache,
a8731cbf
SW
233 .nlgroup = RTNLGRP_DECnet_RULE,
234 .policy = dn_fib_rule_policy,
a8731cbf 235 .owner = THIS_MODULE,
03592383 236 .fro_net = &init_net,
a8731cbf
SW
237};
238
1da177e4
LT
239void __init dn_fib_rules_init(void)
240{
e9c5158a
EB
241 dn_fib_rules_ops =
242 fib_rules_register(&dn_fib_rules_ops_template, &init_net);
243 BUG_ON(IS_ERR(dn_fib_rules_ops));
244 BUG_ON(fib_default_rule_add(dn_fib_rules_ops, 0x7fff,
2994c638 245 RT_TABLE_MAIN, 0));
1da177e4
LT
246}
247
248void __exit dn_fib_rules_cleanup(void)
249{
e9c5158a
EB
250 fib_rules_unregister(dn_fib_rules_ops);
251 rcu_barrier();
1da177e4
LT
252}
253
254
This page took 0.580577 seconds and 5 git commands to generate.