Blame view

net/decnet/dn_rules.c 5.38 KB
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1
2
3
4
5
6
7
8
9
10
11
12
13
  
  /*
   * DECnet       An implementation of the DECnet protocol suite for the LINUX
   *              operating system.  DECnet is implemented using the  BSD Socket
   *              interface as the means of communication with the user level.
   *
   *              DECnet Routing Forwarding Information Base (Rules)
   *
   * Author:      Steve Whitehouse <SteveW@ACM.org>
   *              Mostly copied from Alexey Kuznetsov's ipv4/fib_rules.c
   *
   *
   * Changes:
a8731cbf6   Steven Whitehouse   [DECNET]: Covert ...
14
15
   *              Steve Whitehouse <steve@chygwyn.com>
   *              Updated for Thomas Graf's generic rules
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
16
17
   *
   */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
18
  #include <linux/net.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
19
  #include <linux/init.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
20
21
  #include <linux/netlink.h>
  #include <linux/rtnetlink.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
22
  #include <linux/netdevice.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
23
  #include <linux/spinlock.h>
ecba320f2   Steven Whitehouse   [DECnet]: Use RCU...
24
25
  #include <linux/list.h>
  #include <linux/rcupdate.h>
bc3b2d7fb   Paul Gortmaker   net: Add export.h...
26
  #include <linux/export.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
27
28
29
  #include <net/neighbour.h>
  #include <net/dst.h>
  #include <net/flow.h>
a8731cbf6   Steven Whitehouse   [DECNET]: Covert ...
30
  #include <net/fib_rules.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
31
32
33
34
  #include <net/dn.h>
  #include <net/dn_fib.h>
  #include <net/dn_neigh.h>
  #include <net/dn_dev.h>
73417f617   Thomas Graf   [NET] fib_rules: ...
35
  #include <net/dn_route.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
36

e9c5158ac   Eric W. Biederman   net: Allow fib_ru...
37
  static struct fib_rules_ops *dn_fib_rules_ops;
a8731cbf6   Steven Whitehouse   [DECNET]: Covert ...
38

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
39
40
  struct dn_fib_rule
  {
a8731cbf6   Steven Whitehouse   [DECNET]: Covert ...
41
42
43
44
45
46
47
48
49
  	struct fib_rule		common;
  	unsigned char		dst_len;
  	unsigned char		src_len;
  	__le16			src;
  	__le16			srcmask;
  	__le16			dst;
  	__le16			dstmask;
  	__le16			srcmap;
  	u8			flags;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
50
  };
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
51

bef55aebd   David S. Miller   decnet: Convert t...
52
  int dn_fib_lookup(struct flowidn *flp, struct dn_fib_res *res)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
53
  {
a8731cbf6   Steven Whitehouse   [DECNET]: Covert ...
54
55
56
57
  	struct fib_lookup_arg arg = {
  		.result = res,
  	};
  	int err;
bef55aebd   David S. Miller   decnet: Convert t...
58
59
  	err = fib_rules_lookup(dn_fib_rules_ops,
  			       flowidn_to_flowi(flp), 0, &arg);
a8731cbf6   Steven Whitehouse   [DECNET]: Covert ...
60
  	res->r = arg.rule;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
61
62
63
  
  	return err;
  }
2aa7f36cd   Adrian Bunk   [DECNET]: cleanups
64
65
  static int dn_fib_rule_action(struct fib_rule *rule, struct flowi *flp,
  			      int flags, struct fib_lookup_arg *arg)
ecba320f2   Steven Whitehouse   [DECnet]: Use RCU...
66
  {
bef55aebd   David S. Miller   decnet: Convert t...
67
  	struct flowidn *fld = &flp->u.dn;
a8731cbf6   Steven Whitehouse   [DECNET]: Covert ...
68
69
  	int err = -EAGAIN;
  	struct dn_fib_table *tbl;
ecba320f2   Steven Whitehouse   [DECnet]: Use RCU...
70

a8731cbf6   Steven Whitehouse   [DECNET]: Covert ...
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
  	switch(rule->action) {
  	case FR_ACT_TO_TBL:
  		break;
  
  	case FR_ACT_UNREACHABLE:
  		err = -ENETUNREACH;
  		goto errout;
  
  	case FR_ACT_PROHIBIT:
  		err = -EACCES;
  		goto errout;
  
  	case FR_ACT_BLACKHOLE:
  	default:
  		err = -EINVAL;
  		goto errout;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
87
  	}
a8731cbf6   Steven Whitehouse   [DECNET]: Covert ...
88
89
90
91
  
  	tbl = dn_fib_get_table(rule->table, 0);
  	if (tbl == NULL)
  		goto errout;
bef55aebd   David S. Miller   decnet: Convert t...
92
  	err = tbl->lookup(tbl, fld, (struct dn_fib_res *)arg->result);
a8731cbf6   Steven Whitehouse   [DECNET]: Covert ...
93
94
95
96
  	if (err > 0)
  		err = -EAGAIN;
  errout:
  	return err;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
97
  }
ef7c79ed6   Patrick McHardy   [NETLINK]: Mark n...
98
  static const struct nla_policy dn_fib_rule_policy[FRA_MAX+1] = {
1f6c9557e   Thomas Graf   [NET] rules: Shar...
99
  	FRA_GENERIC_POLICY,
a8731cbf6   Steven Whitehouse   [DECNET]: Covert ...
100
  };
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
101

a8731cbf6   Steven Whitehouse   [DECNET]: Covert ...
102
  static int dn_fib_rule_match(struct fib_rule *rule, struct flowi *fl, int flags)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
103
  {
a8731cbf6   Steven Whitehouse   [DECNET]: Covert ...
104
  	struct dn_fib_rule *r = (struct dn_fib_rule *)rule;
bef55aebd   David S. Miller   decnet: Convert t...
105
106
107
  	struct flowidn *fld = &fl->u.dn;
  	__le16 daddr = fld->daddr;
  	__le16 saddr = fld->saddr;
a8731cbf6   Steven Whitehouse   [DECNET]: Covert ...
108
109
110
111
  
  	if (((saddr ^ r->src) & r->srcmask) ||
  	    ((daddr ^ r->dst) & r->dstmask))
  		return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
112

a8731cbf6   Steven Whitehouse   [DECNET]: Covert ...
113
114
115
116
  	return 1;
  }
  
  static int dn_fib_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
8b3521eeb   Rami Rosen   ipv4: remove an u...
117
  				 struct fib_rule_hdr *frh,
a8731cbf6   Steven Whitehouse   [DECNET]: Covert ...
118
119
120
121
  				 struct nlattr **tb)
  {
  	int err = -EINVAL;
  	struct dn_fib_rule *r = (struct dn_fib_rule *)rule;
e1701c68c   Thomas Graf   [NET]: Fix fib_ru...
122
  	if (frh->tos)
a8731cbf6   Steven Whitehouse   [DECNET]: Covert ...
123
124
125
126
127
128
129
130
131
132
133
134
135
  		goto  errout;
  
  	if (rule->table == RT_TABLE_UNSPEC) {
  		if (rule->action == FR_ACT_TO_TBL) {
  			struct dn_fib_table *table;
  
  			table = dn_fib_empty_table();
  			if (table == NULL) {
  				err = -ENOBUFS;
  				goto errout;
  			}
  
  			rule->table = table->n;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
136
137
  		}
  	}
e1701c68c   Thomas Graf   [NET]: Fix fib_ru...
138
  	if (frh->src_len)
2835fdfa4   Al Viro   [PATCH] FRA_{DST,...
139
  		r->src = nla_get_le16(tb[FRA_SRC]);
ecba320f2   Steven Whitehouse   [DECnet]: Use RCU...
140

e1701c68c   Thomas Graf   [NET]: Fix fib_ru...
141
  	if (frh->dst_len)
2835fdfa4   Al Viro   [PATCH] FRA_{DST,...
142
  		r->dst = nla_get_le16(tb[FRA_DST]);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
143

a8731cbf6   Steven Whitehouse   [DECNET]: Covert ...
144
145
146
147
148
149
150
151
  	r->src_len = frh->src_len;
  	r->srcmask = dnet_make_mask(r->src_len);
  	r->dst_len = frh->dst_len;
  	r->dstmask = dnet_make_mask(r->dst_len);
  	err = 0;
  errout:
  	return err;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
152

a8731cbf6   Steven Whitehouse   [DECNET]: Covert ...
153
154
  static int dn_fib_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh,
  			       struct nlattr **tb)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
155
  {
a8731cbf6   Steven Whitehouse   [DECNET]: Covert ...
156
157
158
159
  	struct dn_fib_rule *r = (struct dn_fib_rule *)rule;
  
  	if (frh->src_len && (r->src_len != frh->src_len))
  		return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
160

a8731cbf6   Steven Whitehouse   [DECNET]: Covert ...
161
162
  	if (frh->dst_len && (r->dst_len != frh->dst_len))
  		return 0;
ecba320f2   Steven Whitehouse   [DECnet]: Use RCU...
163

e1701c68c   Thomas Graf   [NET]: Fix fib_ru...
164
  	if (frh->src_len && (r->src != nla_get_le16(tb[FRA_SRC])))
a8731cbf6   Steven Whitehouse   [DECNET]: Covert ...
165
  		return 0;
e1701c68c   Thomas Graf   [NET]: Fix fib_ru...
166
  	if (frh->dst_len && (r->dst != nla_get_le16(tb[FRA_DST])))
a8731cbf6   Steven Whitehouse   [DECNET]: Covert ...
167
  		return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
168

a8731cbf6   Steven Whitehouse   [DECNET]: Covert ...
169
  	return 1;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
170
  }
c4ea94ab3   Steven Whitehouse   [DECnet]: Endian ...
171
  unsigned dnet_addr_type(__le16 addr)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
172
  {
bef55aebd   David S. Miller   decnet: Convert t...
173
  	struct flowidn fld = { .daddr = addr };
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
174
175
  	struct dn_fib_res res;
  	unsigned ret = RTN_UNICAST;
abcab2683   Patrick McHardy   [DECNET]: Increas...
176
  	struct dn_fib_table *tb = dn_fib_get_table(RT_TABLE_LOCAL, 0);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
177
178
179
180
  
  	res.r = NULL;
  
  	if (tb) {
bef55aebd   David S. Miller   decnet: Convert t...
181
  		if (!tb->lookup(tb, &fld, &res)) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
182
183
184
185
186
187
  			ret = res.type;
  			dn_fib_res_put(&res);
  		}
  	}
  	return ret;
  }
a8731cbf6   Steven Whitehouse   [DECNET]: Covert ...
188
  static int dn_fib_rule_fill(struct fib_rule *rule, struct sk_buff *skb,
04af8cf6f   Rami Rosen   net: Remove unuse...
189
  			    struct fib_rule_hdr *frh)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
190
  {
a8731cbf6   Steven Whitehouse   [DECNET]: Covert ...
191
  	struct dn_fib_rule *r = (struct dn_fib_rule *)rule;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
192

a8731cbf6   Steven Whitehouse   [DECNET]: Covert ...
193
194
195
  	frh->dst_len = r->dst_len;
  	frh->src_len = r->src_len;
  	frh->tos = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
196

a8731cbf6   Steven Whitehouse   [DECNET]: Covert ...
197
  	if (r->dst_len)
2835fdfa4   Al Viro   [PATCH] FRA_{DST,...
198
  		NLA_PUT_LE16(skb, FRA_DST, r->dst);
a8731cbf6   Steven Whitehouse   [DECNET]: Covert ...
199
  	if (r->src_len)
2835fdfa4   Al Viro   [PATCH] FRA_{DST,...
200
  		NLA_PUT_LE16(skb, FRA_SRC, r->src);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
201

a8731cbf6   Steven Whitehouse   [DECNET]: Covert ...
202
  	return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
203

a8731cbf6   Steven Whitehouse   [DECNET]: Covert ...
204
205
  nla_put_failure:
  	return -ENOBUFS;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
206
  }
ae299fc05   Denis V. Lunev   net: add fib_rule...
207
  static void dn_fib_rule_flush_cache(struct fib_rules_ops *ops)
73417f617   Thomas Graf   [NET] fib_rules: ...
208
  {
4b19ca44c   Thomas Graf   [NET] fib_rules: ...
209
  	dn_rt_cache_flush(-1);
73417f617   Thomas Graf   [NET] fib_rules: ...
210
  }
3d0c9c4eb   Patrick McHardy   net: fib_rules: m...
211
  static const struct fib_rules_ops __net_initdata dn_fib_rules_ops_template = {
25239cee7   Patrick McHardy   net: rtnetlink: d...
212
  	.family		= AF_DECnet,
a8731cbf6   Steven Whitehouse   [DECNET]: Covert ...
213
  	.rule_size	= sizeof(struct dn_fib_rule),
e1701c68c   Thomas Graf   [NET]: Fix fib_ru...
214
  	.addr_size	= sizeof(u16),
a8731cbf6   Steven Whitehouse   [DECNET]: Covert ...
215
216
217
218
219
  	.action		= dn_fib_rule_action,
  	.match		= dn_fib_rule_match,
  	.configure	= dn_fib_rule_configure,
  	.compare	= dn_fib_rule_compare,
  	.fill		= dn_fib_rule_fill,
d8a566bea   Patrick McHardy   net: fib_rules: c...
220
  	.default_pref	= fib_default_rule_pref,
73417f617   Thomas Graf   [NET] fib_rules: ...
221
  	.flush_cache	= dn_fib_rule_flush_cache,
a8731cbf6   Steven Whitehouse   [DECNET]: Covert ...
222
223
  	.nlgroup	= RTNLGRP_DECnet_RULE,
  	.policy		= dn_fib_rule_policy,
a8731cbf6   Steven Whitehouse   [DECNET]: Covert ...
224
  	.owner		= THIS_MODULE,
035923833   Denis V. Lunev   [FIB]: Add netns ...
225
  	.fro_net	= &init_net,
a8731cbf6   Steven Whitehouse   [DECNET]: Covert ...
226
  };
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
227
228
  void __init dn_fib_rules_init(void)
  {
e9c5158ac   Eric W. Biederman   net: Allow fib_ru...
229
230
231
232
  	dn_fib_rules_ops =
  		fib_rules_register(&dn_fib_rules_ops_template, &init_net);
  	BUG_ON(IS_ERR(dn_fib_rules_ops));
  	BUG_ON(fib_default_rule_add(dn_fib_rules_ops, 0x7fff,
2994c6386   Denis V. Lunev   [INET]: Small pos...
233
  			            RT_TABLE_MAIN, 0));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
234
235
236
237
  }
  
  void __exit dn_fib_rules_cleanup(void)
  {
e9c5158ac   Eric W. Biederman   net: Allow fib_ru...
238
239
  	fib_rules_unregister(dn_fib_rules_ops);
  	rcu_barrier();
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
240
  }