blob: 544c455407460eb4c465d68f9113ab4f7f87c6ee [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * DECnet An implementation of the DECnet protocol suite for the LINUX
3 * operating system. DECnet is implemented using the BSD Socket
4 * interface as the means of communication with the user level.
5 *
6 * DECnet Routing Forwarding Information Base (Routing Tables)
7 *
8 * Author: Steve Whitehouse <SteveW@ACM.org>
9 * Mostly copied from the IPv4 routing code
10 *
11 *
12 * Changes:
13 *
14 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/string.h>
16#include <linux/net.h>
17#include <linux/socket.h>
18#include <linux/sockios.h>
19#include <linux/init.h>
20#include <linux/skbuff.h>
21#include <linux/netlink.h>
22#include <linux/rtnetlink.h>
23#include <linux/proc_fs.h>
24#include <linux/netdevice.h>
25#include <linux/timer.h>
26#include <linux/spinlock.h>
27#include <asm/atomic.h>
28#include <asm/uaccess.h>
29#include <linux/route.h> /* RTF_xxx */
30#include <net/neighbour.h>
31#include <net/dst.h>
32#include <net/flow.h>
Steven Whitehousea8731cb2006-08-09 15:56:46 -070033#include <net/fib_rules.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <net/dn.h>
35#include <net/dn_route.h>
36#include <net/dn_fib.h>
37#include <net/dn_neigh.h>
38#include <net/dn_dev.h>
39
40struct dn_zone
41{
42 struct dn_zone *dz_next;
43 struct dn_fib_node **dz_hash;
44 int dz_nent;
45 int dz_divisor;
46 u32 dz_hashmask;
47#define DZ_HASHMASK(dz) ((dz)->dz_hashmask)
48 int dz_order;
Steven Whitehousec4ea94a2006-03-20 22:42:39 -080049 __le16 dz_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#define DZ_MASK(dz) ((dz)->dz_mask)
51};
52
53struct dn_hash
54{
55 struct dn_zone *dh_zones[17];
56 struct dn_zone *dh_zone_list;
57};
58
59#define dz_key_0(key) ((key).datum = 0)
60#define dz_prefix(key,dz) ((key).datum)
61
62#define for_nexthops(fi) { int nhsel; const struct dn_fib_nh *nh;\
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +090063 for(nhsel = 0, nh = (fi)->fib_nh; nhsel < (fi)->fib_nhs; nh++, nhsel++)
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
65#define endfor_nexthops(fi) }
66
67#define DN_MAX_DIVISOR 1024
68#define DN_S_ZOMBIE 1
69#define DN_S_ACCESSED 2
70
71#define DN_FIB_SCAN(f, fp) \
72for( ; ((f) = *(fp)) != NULL; (fp) = &(f)->fn_next)
73
74#define DN_FIB_SCAN_KEY(f, fp, key) \
75for( ; ((f) = *(fp)) != NULL && dn_key_eq((f)->fn_key, (key)); (fp) = &(f)->fn_next)
76
77#define RT_TABLE_MIN 1
Patrick McHardyabcab262006-08-10 23:11:47 -070078#define DN_FIB_TABLE_HASHSZ 256
79static struct hlist_head dn_fib_table_hash[DN_FIB_TABLE_HASHSZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -070080static DEFINE_RWLOCK(dn_fib_tables_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
Christoph Lametere18b8902006-12-06 20:33:20 -080082static struct kmem_cache *dn_hash_kmem __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083static int dn_fib_hash_zombies;
84
85static inline dn_fib_idx_t dn_hash(dn_fib_key_t key, struct dn_zone *dz)
86{
Steven Whitehousec4ea94a2006-03-20 22:42:39 -080087 u16 h = dn_ntohs(key.datum)>>(16 - dz->dz_order);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 h ^= (h >> 10);
89 h ^= (h >> 6);
90 h &= DZ_HASHMASK(dz);
91 return *(dn_fib_idx_t *)&h;
92}
93
Steven Whitehousec4ea94a2006-03-20 22:42:39 -080094static inline dn_fib_key_t dz_key(__le16 dst, struct dn_zone *dz)
Linus Torvalds1da177e2005-04-16 15:20:36 -070095{
96 dn_fib_key_t k;
97 k.datum = dst & DZ_MASK(dz);
98 return k;
99}
100
101static inline struct dn_fib_node **dn_chain_p(dn_fib_key_t key, struct dn_zone *dz)
102{
103 return &dz->dz_hash[dn_hash(key, dz).datum];
104}
105
106static inline struct dn_fib_node *dz_chain(dn_fib_key_t key, struct dn_zone *dz)
107{
108 return dz->dz_hash[dn_hash(key, dz).datum];
109}
110
111static inline int dn_key_eq(dn_fib_key_t a, dn_fib_key_t b)
112{
113 return a.datum == b.datum;
114}
115
116static inline int dn_key_leq(dn_fib_key_t a, dn_fib_key_t b)
117{
118 return a.datum <= b.datum;
119}
120
121static inline void dn_rebuild_zone(struct dn_zone *dz,
122 struct dn_fib_node **old_ht,
123 int old_divisor)
124{
125 int i;
126 struct dn_fib_node *f, **fp, *next;
127
128 for(i = 0; i < old_divisor; i++) {
129 for(f = old_ht[i]; f; f = f->fn_next) {
130 next = f->fn_next;
131 for(fp = dn_chain_p(f->fn_key, dz);
132 *fp && dn_key_leq((*fp)->fn_key, f->fn_key);
133 fp = &(*fp)->fn_next)
134 /* NOTHING */;
135 f->fn_next = *fp;
136 *fp = f;
137 }
138 }
139}
140
141static void dn_rehash_zone(struct dn_zone *dz)
142{
143 struct dn_fib_node **ht, **old_ht;
144 int old_divisor, new_divisor;
145 u32 new_hashmask;
146
147 old_divisor = dz->dz_divisor;
148
149 switch(old_divisor) {
150 case 16:
151 new_divisor = 256;
152 new_hashmask = 0xFF;
153 break;
154 default:
155 printk(KERN_DEBUG "DECnet: dn_rehash_zone: BUG! %d\n", old_divisor);
156 case 256:
157 new_divisor = 1024;
158 new_hashmask = 0x3FF;
159 break;
160 }
161
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700162 ht = kcalloc(new_divisor, sizeof(struct dn_fib_node*), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 if (ht == NULL)
164 return;
165
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 write_lock_bh(&dn_fib_tables_lock);
167 old_ht = dz->dz_hash;
168 dz->dz_hash = ht;
169 dz->dz_hashmask = new_hashmask;
170 dz->dz_divisor = new_divisor;
171 dn_rebuild_zone(dz, old_ht, old_divisor);
172 write_unlock_bh(&dn_fib_tables_lock);
173 kfree(old_ht);
174}
175
176static void dn_free_node(struct dn_fib_node *f)
177{
178 dn_fib_release_info(DN_FIB_INFO(f));
179 kmem_cache_free(dn_hash_kmem, f);
180}
181
182
183static struct dn_zone *dn_new_zone(struct dn_hash *table, int z)
184{
185 int i;
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700186 struct dn_zone *dz = kzalloc(sizeof(struct dn_zone), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 if (!dz)
188 return NULL;
189
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 if (z) {
191 dz->dz_divisor = 16;
192 dz->dz_hashmask = 0x0F;
193 } else {
194 dz->dz_divisor = 1;
195 dz->dz_hashmask = 0;
196 }
197
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700198 dz->dz_hash = kcalloc(dz->dz_divisor, sizeof(struct dn_fib_node *), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 if (!dz->dz_hash) {
200 kfree(dz);
201 return NULL;
202 }
203
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 dz->dz_order = z;
205 dz->dz_mask = dnet_make_mask(z);
206
207 for(i = z + 1; i <= 16; i++)
208 if (table->dh_zones[i])
209 break;
210
211 write_lock_bh(&dn_fib_tables_lock);
212 if (i>16) {
213 dz->dz_next = table->dh_zone_list;
214 table->dh_zone_list = dz;
215 } else {
216 dz->dz_next = table->dh_zones[i]->dz_next;
217 table->dh_zones[i]->dz_next = dz;
218 }
219 table->dh_zones[z] = dz;
220 write_unlock_bh(&dn_fib_tables_lock);
221 return dz;
222}
223
224
225static int dn_fib_nh_match(struct rtmsg *r, struct nlmsghdr *nlh, struct dn_kern_rta *rta, struct dn_fib_info *fi)
226{
227 struct rtnexthop *nhp;
228 int nhlen;
229
230 if (rta->rta_priority && *rta->rta_priority != fi->fib_priority)
231 return 1;
232
233 if (rta->rta_oif || rta->rta_gw) {
234 if ((!rta->rta_oif || *rta->rta_oif == fi->fib_nh->nh_oif) &&
235 (!rta->rta_gw || memcmp(rta->rta_gw, &fi->fib_nh->nh_gw, 2) == 0))
236 return 0;
237 return 1;
238 }
239
240 if (rta->rta_mp == NULL)
241 return 0;
242
243 nhp = RTA_DATA(rta->rta_mp);
244 nhlen = RTA_PAYLOAD(rta->rta_mp);
245
246 for_nexthops(fi) {
247 int attrlen = nhlen - sizeof(struct rtnexthop);
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800248 __le16 gw;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
250 if (attrlen < 0 || (nhlen -= nhp->rtnh_len) < 0)
251 return -EINVAL;
252 if (nhp->rtnh_ifindex && nhp->rtnh_ifindex != nh->nh_oif)
253 return 1;
254 if (attrlen) {
255 gw = dn_fib_get_attr16(RTNH_DATA(nhp), attrlen, RTA_GATEWAY);
256
257 if (gw && gw != nh->nh_gw)
258 return 1;
259 }
260 nhp = RTNH_NEXT(nhp);
261 } endfor_nexthops(fi);
262
263 return 0;
264}
265
Thomas Graf339bf982006-11-10 14:10:15 -0800266static inline size_t dn_fib_nlmsg_size(struct dn_fib_info *fi)
267{
David S. Miller75356f22006-11-12 23:02:01 -0800268 size_t payload = NLMSG_ALIGN(sizeof(struct rtmsg))
Thomas Graf339bf982006-11-10 14:10:15 -0800269 + nla_total_size(4) /* RTA_TABLE */
270 + nla_total_size(2) /* RTA_DST */
271 + nla_total_size(4); /* RTA_PRIORITY */
272
273 /* space for nested metrics */
274 payload += nla_total_size((RTAX_MAX * nla_total_size(4)));
275
276 if (fi->fib_nhs) {
277 /* Also handles the special case fib_nhs == 1 */
278
279 /* each nexthop is packed in an attribute */
280 size_t nhsize = nla_total_size(sizeof(struct rtnexthop));
281
282 /* may contain a gateway attribute */
283 nhsize += nla_total_size(4);
284
285 /* all nexthops are packed in a nested attribute */
286 payload += nla_total_size(fi->fib_nhs * nhsize);
287 }
288
289 return payload;
290}
291
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292static int dn_fib_dump_info(struct sk_buff *skb, u32 pid, u32 seq, int event,
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900293 u32 tb_id, u8 type, u8 scope, void *dst, int dst_len,
294 struct dn_fib_info *fi, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295{
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900296 struct rtmsg *rtm;
297 struct nlmsghdr *nlh;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700298 unsigned char *b = skb_tail_pointer(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900300 nlh = NLMSG_NEW(skb, pid, seq, event, sizeof(*rtm), flags);
301 rtm = NLMSG_DATA(nlh);
302 rtm->rtm_family = AF_DECnet;
303 rtm->rtm_dst_len = dst_len;
304 rtm->rtm_src_len = 0;
305 rtm->rtm_tos = 0;
306 rtm->rtm_table = tb_id;
Patrick McHardy9e762a42006-08-10 23:09:48 -0700307 RTA_PUT_U32(skb, RTA_TABLE, tb_id);
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900308 rtm->rtm_flags = fi->fib_flags;
309 rtm->rtm_scope = scope;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 rtm->rtm_type = type;
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900311 if (rtm->rtm_dst_len)
312 RTA_PUT(skb, RTA_DST, 2, dst);
313 rtm->rtm_protocol = fi->fib_protocol;
314 if (fi->fib_priority)
315 RTA_PUT(skb, RTA_PRIORITY, 4, &fi->fib_priority);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 if (rtnetlink_put_metrics(skb, fi->fib_metrics) < 0)
317 goto rtattr_failure;
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900318 if (fi->fib_nhs == 1) {
319 if (fi->fib_nh->nh_gw)
320 RTA_PUT(skb, RTA_GATEWAY, 2, &fi->fib_nh->nh_gw);
321 if (fi->fib_nh->nh_oif)
322 RTA_PUT(skb, RTA_OIF, sizeof(int), &fi->fib_nh->nh_oif);
323 }
324 if (fi->fib_nhs > 1) {
325 struct rtnexthop *nhp;
326 struct rtattr *mp_head;
327 if (skb_tailroom(skb) <= RTA_SPACE(0))
328 goto rtattr_failure;
329 mp_head = (struct rtattr *)skb_put(skb, RTA_SPACE(0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900331 for_nexthops(fi) {
332 if (skb_tailroom(skb) < RTA_ALIGN(RTA_ALIGN(sizeof(*nhp)) + 4))
333 goto rtattr_failure;
334 nhp = (struct rtnexthop *)skb_put(skb, RTA_ALIGN(sizeof(*nhp)));
335 nhp->rtnh_flags = nh->nh_flags & 0xFF;
336 nhp->rtnh_hops = nh->nh_weight - 1;
337 nhp->rtnh_ifindex = nh->nh_oif;
338 if (nh->nh_gw)
339 RTA_PUT(skb, RTA_GATEWAY, 2, &nh->nh_gw);
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700340 nhp->rtnh_len = skb_tail_pointer(skb) - (unsigned char *)nhp;
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900341 } endfor_nexthops(fi);
342 mp_head->rta_type = RTA_MULTIPATH;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700343 mp_head->rta_len = skb_tail_pointer(skb) - (u8 *)mp_head;
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900344 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700346 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900347 return skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
349
350nlmsg_failure:
351rtattr_failure:
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900352 skb_trim(skb, b - skb->data);
353 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354}
355
356
Patrick McHardy2dfe55b2006-08-10 23:08:33 -0700357static void dn_rtmsg_fib(int event, struct dn_fib_node *f, int z, u32 tb_id,
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900358 struct nlmsghdr *nlh, struct netlink_skb_parms *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359{
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900360 struct sk_buff *skb;
361 u32 pid = req ? req->pid : 0;
Thomas Grafdc738dd2006-08-15 00:33:35 -0700362 int err = -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900364 skb = nlmsg_new(dn_fib_nlmsg_size(DN_FIB_INFO(f)), GFP_KERNEL);
365 if (skb == NULL)
Thomas Grafdc738dd2006-08-15 00:33:35 -0700366 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900368 err = dn_fib_dump_info(skb, pid, nlh->nlmsg_seq, event, tb_id,
Thomas Grafdc738dd2006-08-15 00:33:35 -0700369 f->fn_type, f->fn_scope, &f->fn_key, z,
370 DN_FIB_INFO(f), 0);
Patrick McHardy26932562007-01-31 23:16:40 -0800371 if (err < 0) {
372 /* -EMSGSIZE implies BUG in dn_fib_nlmsg_size() */
373 WARN_ON(err == -EMSGSIZE);
374 kfree_skb(skb);
375 goto errout;
376 }
Thomas Grafdc738dd2006-08-15 00:33:35 -0700377 err = rtnl_notify(skb, pid, RTNLGRP_DECnet_ROUTE, nlh, GFP_KERNEL);
378errout:
379 if (err < 0)
380 rtnl_set_sk_err(RTNLGRP_DECnet_ROUTE, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381}
382
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900383static __inline__ int dn_hash_dump_bucket(struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 struct netlink_callback *cb,
385 struct dn_fib_table *tb,
386 struct dn_zone *dz,
387 struct dn_fib_node *f)
388{
389 int i, s_i;
390
Patrick McHardyabcab262006-08-10 23:11:47 -0700391 s_i = cb->args[4];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 for(i = 0; f; i++, f = f->fn_next) {
393 if (i < s_i)
394 continue;
395 if (f->fn_state & DN_S_ZOMBIE)
396 continue;
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900397 if (dn_fib_dump_info(skb, NETLINK_CB(cb->skb).pid,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 cb->nlh->nlmsg_seq,
399 RTM_NEWROUTE,
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900400 tb->n,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 (f->fn_state & DN_S_ZOMBIE) ? 0 : f->fn_type,
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900402 f->fn_scope, &f->fn_key, dz->dz_order,
Jamal Hadi Salimb6544c02005-06-18 22:54:12 -0700403 f->fn_info, NLM_F_MULTI) < 0) {
Patrick McHardyabcab262006-08-10 23:11:47 -0700404 cb->args[4] = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 return -1;
406 }
407 }
Patrick McHardyabcab262006-08-10 23:11:47 -0700408 cb->args[4] = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 return skb->len;
410}
411
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900412static __inline__ int dn_hash_dump_zone(struct sk_buff *skb,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 struct netlink_callback *cb,
414 struct dn_fib_table *tb,
415 struct dn_zone *dz)
416{
417 int h, s_h;
418
Patrick McHardyabcab262006-08-10 23:11:47 -0700419 s_h = cb->args[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 for(h = 0; h < dz->dz_divisor; h++) {
421 if (h < s_h)
422 continue;
423 if (h > s_h)
Patrick McHardyabcab262006-08-10 23:11:47 -0700424 memset(&cb->args[4], 0, sizeof(cb->args) - 4*sizeof(cb->args[0]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 if (dz->dz_hash == NULL || dz->dz_hash[h] == NULL)
426 continue;
427 if (dn_hash_dump_bucket(skb, cb, tb, dz, dz->dz_hash[h]) < 0) {
Patrick McHardyabcab262006-08-10 23:11:47 -0700428 cb->args[3] = h;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 return -1;
430 }
431 }
Patrick McHardyabcab262006-08-10 23:11:47 -0700432 cb->args[3] = h;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 return skb->len;
434}
435
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900436static int dn_fib_table_dump(struct dn_fib_table *tb, struct sk_buff *skb,
437 struct netlink_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438{
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900439 int m, s_m;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 struct dn_zone *dz;
441 struct dn_hash *table = (struct dn_hash *)tb->data;
442
Patrick McHardyabcab262006-08-10 23:11:47 -0700443 s_m = cb->args[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 read_lock(&dn_fib_tables_lock);
445 for(dz = table->dh_zone_list, m = 0; dz; dz = dz->dz_next, m++) {
446 if (m < s_m)
447 continue;
448 if (m > s_m)
Patrick McHardyabcab262006-08-10 23:11:47 -0700449 memset(&cb->args[3], 0, sizeof(cb->args) - 3*sizeof(cb->args[0]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
451 if (dn_hash_dump_zone(skb, cb, tb, dz) < 0) {
Patrick McHardyabcab262006-08-10 23:11:47 -0700452 cb->args[2] = m;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 read_unlock(&dn_fib_tables_lock);
454 return -1;
455 }
456 }
457 read_unlock(&dn_fib_tables_lock);
Patrick McHardyabcab262006-08-10 23:11:47 -0700458 cb->args[2] = m;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900460 return skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461}
462
Patrick McHardyabcab262006-08-10 23:11:47 -0700463int dn_fib_dump(struct sk_buff *skb, struct netlink_callback *cb)
464{
465 unsigned int h, s_h;
466 unsigned int e = 0, s_e;
467 struct dn_fib_table *tb;
468 struct hlist_node *node;
469 int dumped = 0;
470
471 if (NLMSG_PAYLOAD(cb->nlh, 0) >= sizeof(struct rtmsg) &&
472 ((struct rtmsg *)NLMSG_DATA(cb->nlh))->rtm_flags&RTM_F_CLONED)
473 return dn_cache_dump(skb, cb);
474
475 s_h = cb->args[0];
476 s_e = cb->args[1];
477
478 for (h = s_h; h < DN_FIB_TABLE_HASHSZ; h++, s_h = 0) {
479 e = 0;
480 hlist_for_each_entry(tb, node, &dn_fib_table_hash[h], hlist) {
481 if (e < s_e)
482 goto next;
483 if (dumped)
484 memset(&cb->args[2], 0, sizeof(cb->args) -
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900485 2 * sizeof(cb->args[0]));
Patrick McHardyabcab262006-08-10 23:11:47 -0700486 if (tb->dump(tb, skb, cb) < 0)
487 goto out;
488 dumped = 1;
489next:
490 e++;
491 }
492 }
493out:
494 cb->args[1] = e;
495 cb->args[0] = h;
496
497 return skb->len;
498}
499
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500static int dn_fib_table_insert(struct dn_fib_table *tb, struct rtmsg *r, struct dn_kern_rta *rta, struct nlmsghdr *n, struct netlink_skb_parms *req)
501{
502 struct dn_hash *table = (struct dn_hash *)tb->data;
503 struct dn_fib_node *new_f, *f, **fp, **del_fp;
504 struct dn_zone *dz;
505 struct dn_fib_info *fi;
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900506 int z = r->rtm_dst_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 int type = r->rtm_type;
508 dn_fib_key_t key;
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900509 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900511 if (z > 16)
512 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
514 dz = table->dh_zones[z];
515 if (!dz && !(dz = dn_new_zone(table, z)))
516 return -ENOBUFS;
517
518 dz_key_0(key);
519 if (rta->rta_dst) {
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800520 __le16 dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 memcpy(&dst, rta->rta_dst, 2);
522 if (dst & ~DZ_MASK(dz))
523 return -EINVAL;
524 key = dz_key(dst, dz);
525 }
526
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900527 if ((fi = dn_fib_create_info(r, rta, n, &err)) == NULL)
528 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
530 if (dz->dz_nent > (dz->dz_divisor << 2) &&
531 dz->dz_divisor > DN_MAX_DIVISOR &&
532 (z==16 || (1<<z) > dz->dz_divisor))
533 dn_rehash_zone(dz);
534
535 fp = dn_chain_p(key, dz);
536
537 DN_FIB_SCAN(f, fp) {
538 if (dn_key_leq(key, f->fn_key))
539 break;
540 }
541
542 del_fp = NULL;
543
544 if (f && (f->fn_state & DN_S_ZOMBIE) &&
545 dn_key_eq(f->fn_key, key)) {
546 del_fp = fp;
547 fp = &f->fn_next;
548 f = *fp;
549 goto create;
550 }
551
552 DN_FIB_SCAN_KEY(f, fp, key) {
553 if (fi->fib_priority <= DN_FIB_INFO(f)->fib_priority)
554 break;
555 }
556
557 if (f && dn_key_eq(f->fn_key, key) &&
558 fi->fib_priority == DN_FIB_INFO(f)->fib_priority) {
559 struct dn_fib_node **ins_fp;
560
561 err = -EEXIST;
562 if (n->nlmsg_flags & NLM_F_EXCL)
563 goto out;
564
565 if (n->nlmsg_flags & NLM_F_REPLACE) {
566 del_fp = fp;
567 fp = &f->fn_next;
568 f = *fp;
569 goto replace;
570 }
571
572 ins_fp = fp;
573 err = -EEXIST;
574
575 DN_FIB_SCAN_KEY(f, fp, key) {
576 if (fi->fib_priority != DN_FIB_INFO(f)->fib_priority)
577 break;
578 if (f->fn_type == type && f->fn_scope == r->rtm_scope
579 && DN_FIB_INFO(f) == fi)
580 goto out;
581 }
582
583 if (!(n->nlmsg_flags & NLM_F_APPEND)) {
584 fp = ins_fp;
585 f = *fp;
586 }
587 }
588
589create:
590 err = -ENOENT;
591 if (!(n->nlmsg_flags & NLM_F_CREATE))
592 goto out;
593
594replace:
595 err = -ENOBUFS;
Robert P. J. Dayc3762222007-02-10 01:45:03 -0800596 new_f = kmem_cache_zalloc(dn_hash_kmem, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 if (new_f == NULL)
598 goto out;
599
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 new_f->fn_key = key;
601 new_f->fn_type = type;
602 new_f->fn_scope = r->rtm_scope;
603 DN_FIB_INFO(new_f) = fi;
604
605 new_f->fn_next = f;
606 write_lock_bh(&dn_fib_tables_lock);
607 *fp = new_f;
608 write_unlock_bh(&dn_fib_tables_lock);
609 dz->dz_nent++;
610
611 if (del_fp) {
612 f = *del_fp;
613 write_lock_bh(&dn_fib_tables_lock);
614 *del_fp = f->fn_next;
615 write_unlock_bh(&dn_fib_tables_lock);
616
617 if (!(f->fn_state & DN_S_ZOMBIE))
618 dn_rtmsg_fib(RTM_DELROUTE, f, z, tb->n, n, req);
619 if (f->fn_state & DN_S_ACCESSED)
620 dn_rt_cache_flush(-1);
621 dn_free_node(f);
622 dz->dz_nent--;
623 } else {
624 dn_rt_cache_flush(-1);
625 }
626
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900627 dn_rtmsg_fib(RTM_NEWROUTE, new_f, z, tb->n, n, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900629 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630out:
631 dn_fib_release_info(fi);
632 return err;
633}
634
635
636static int dn_fib_table_delete(struct dn_fib_table *tb, struct rtmsg *r, struct dn_kern_rta *rta, struct nlmsghdr *n, struct netlink_skb_parms *req)
637{
638 struct dn_hash *table = (struct dn_hash*)tb->data;
639 struct dn_fib_node **fp, **del_fp, *f;
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900640 int z = r->rtm_dst_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 struct dn_zone *dz;
642 dn_fib_key_t key;
643 int matched;
644
645
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900646 if (z > 16)
647 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
649 if ((dz = table->dh_zones[z]) == NULL)
650 return -ESRCH;
651
652 dz_key_0(key);
653 if (rta->rta_dst) {
Steven Whitehousec4ea94a2006-03-20 22:42:39 -0800654 __le16 dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 memcpy(&dst, rta->rta_dst, 2);
656 if (dst & ~DZ_MASK(dz))
657 return -EINVAL;
658 key = dz_key(dst, dz);
659 }
660
661 fp = dn_chain_p(key, dz);
662
663 DN_FIB_SCAN(f, fp) {
664 if (dn_key_eq(f->fn_key, key))
665 break;
666 if (dn_key_leq(key, f->fn_key))
667 return -ESRCH;
668 }
669
670 matched = 0;
671 del_fp = NULL;
672 DN_FIB_SCAN_KEY(f, fp, key) {
673 struct dn_fib_info *fi = DN_FIB_INFO(f);
674
675 if (f->fn_state & DN_S_ZOMBIE)
676 return -ESRCH;
677
678 matched++;
679
680 if (del_fp == NULL &&
681 (!r->rtm_type || f->fn_type == r->rtm_type) &&
682 (r->rtm_scope == RT_SCOPE_NOWHERE || f->fn_scope == r->rtm_scope) &&
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900683 (!r->rtm_protocol ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 fi->fib_protocol == r->rtm_protocol) &&
685 dn_fib_nh_match(r, n, rta, fi) == 0)
686 del_fp = fp;
687 }
688
689 if (del_fp) {
690 f = *del_fp;
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900691 dn_rtmsg_fib(RTM_DELROUTE, f, z, tb->n, n, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
693 if (matched != 1) {
694 write_lock_bh(&dn_fib_tables_lock);
695 *del_fp = f->fn_next;
696 write_unlock_bh(&dn_fib_tables_lock);
697
698 if (f->fn_state & DN_S_ACCESSED)
699 dn_rt_cache_flush(-1);
700 dn_free_node(f);
701 dz->dz_nent--;
702 } else {
703 f->fn_state |= DN_S_ZOMBIE;
704 if (f->fn_state & DN_S_ACCESSED) {
705 f->fn_state &= ~DN_S_ACCESSED;
706 dn_rt_cache_flush(-1);
707 }
708 if (++dn_fib_hash_zombies > 128)
709 dn_fib_flush();
710 }
711
712 return 0;
713 }
714
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900715 return -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716}
717
718static inline int dn_flush_list(struct dn_fib_node **fp, int z, struct dn_hash *table)
719{
720 int found = 0;
721 struct dn_fib_node *f;
722
723 while((f = *fp) != NULL) {
724 struct dn_fib_info *fi = DN_FIB_INFO(f);
725
726 if (fi && ((f->fn_state & DN_S_ZOMBIE) || (fi->fib_flags & RTNH_F_DEAD))) {
727 write_lock_bh(&dn_fib_tables_lock);
728 *fp = f->fn_next;
729 write_unlock_bh(&dn_fib_tables_lock);
730
731 dn_free_node(f);
732 found++;
733 continue;
734 }
735 fp = &f->fn_next;
736 }
737
738 return found;
739}
740
741static int dn_fib_table_flush(struct dn_fib_table *tb)
742{
743 struct dn_hash *table = (struct dn_hash *)tb->data;
744 struct dn_zone *dz;
745 int found = 0;
746
747 dn_fib_hash_zombies = 0;
748 for(dz = table->dh_zone_list; dz; dz = dz->dz_next) {
749 int i;
750 int tmp = 0;
751 for(i = dz->dz_divisor-1; i >= 0; i--)
752 tmp += dn_flush_list(&dz->dz_hash[i], dz->dz_order, table);
753 dz->dz_nent -= tmp;
754 found += tmp;
755 }
756
757 return found;
758}
759
760static int dn_fib_table_lookup(struct dn_fib_table *tb, const struct flowi *flp, struct dn_fib_res *res)
761{
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900762 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 struct dn_zone *dz;
764 struct dn_hash *t = (struct dn_hash *)tb->data;
765
766 read_lock(&dn_fib_tables_lock);
767 for(dz = t->dh_zone_list; dz; dz = dz->dz_next) {
768 struct dn_fib_node *f;
769 dn_fib_key_t k = dz_key(flp->fld_dst, dz);
770
771 for(f = dz_chain(k, dz); f; f = f->fn_next) {
772 if (!dn_key_eq(k, f->fn_key)) {
773 if (dn_key_leq(k, f->fn_key))
774 break;
775 else
776 continue;
777 }
778
779 f->fn_state |= DN_S_ACCESSED;
780
781 if (f->fn_state&DN_S_ZOMBIE)
782 continue;
783
784 if (f->fn_scope < flp->fld_scope)
785 continue;
786
787 err = dn_fib_semantic_match(f->fn_type, DN_FIB_INFO(f), flp, res);
788
789 if (err == 0) {
790 res->type = f->fn_type;
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900791 res->scope = f->fn_scope;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 res->prefixlen = dz->dz_order;
793 goto out;
794 }
795 if (err < 0)
796 goto out;
797 }
798 }
799 err = 1;
800out:
801 read_unlock(&dn_fib_tables_lock);
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900802 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803}
804
805
Patrick McHardy2dfe55b2006-08-10 23:08:33 -0700806struct dn_fib_table *dn_fib_get_table(u32 n, int create)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807{
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900808 struct dn_fib_table *t;
Patrick McHardyabcab262006-08-10 23:11:47 -0700809 struct hlist_node *node;
810 unsigned int h;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900812 if (n < RT_TABLE_MIN)
813 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900815 if (n > RT_TABLE_MAX)
816 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817
Patrick McHardyabcab262006-08-10 23:11:47 -0700818 h = n & (DN_FIB_TABLE_HASHSZ - 1);
819 rcu_read_lock();
820 hlist_for_each_entry_rcu(t, node, &dn_fib_table_hash[h], hlist) {
821 if (t->n == n) {
822 rcu_read_unlock();
823 return t;
824 }
825 }
826 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900828 if (!create)
829 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900831 if (in_interrupt() && net_ratelimit()) {
832 printk(KERN_DEBUG "DECnet: BUG! Attempt to create routing table from interrupt\n");
833 return NULL;
834 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900836 t = kzalloc(sizeof(struct dn_fib_table) + sizeof(struct dn_hash),
Arnaldo Carvalho de Meloe6b61102006-11-21 01:16:24 -0200837 GFP_KERNEL);
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900838 if (t == NULL)
839 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900841 t->n = n;
842 t->insert = dn_fib_table_insert;
843 t->delete = dn_fib_table_delete;
844 t->lookup = dn_fib_table_lookup;
845 t->flush = dn_fib_table_flush;
846 t->dump = dn_fib_table_dump;
Patrick McHardyabcab262006-08-10 23:11:47 -0700847 hlist_add_head_rcu(&t->hlist, &dn_fib_table_hash[h]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900849 return t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850}
851
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852struct dn_fib_table *dn_fib_empty_table(void)
853{
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900854 u32 id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900856 for(id = RT_TABLE_MIN; id <= RT_TABLE_MAX; id++)
Patrick McHardyabcab262006-08-10 23:11:47 -0700857 if (dn_fib_get_table(id, 0) == NULL)
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900858 return dn_fib_get_table(id, 1);
859 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860}
861
Patrick McHardyabcab262006-08-10 23:11:47 -0700862void dn_fib_flush(void)
863{
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900864 int flushed = 0;
865 struct dn_fib_table *tb;
Patrick McHardyabcab262006-08-10 23:11:47 -0700866 struct hlist_node *node;
867 unsigned int h;
868
869 for (h = 0; h < DN_FIB_TABLE_HASHSZ; h++) {
870 hlist_for_each_entry(tb, node, &dn_fib_table_hash[h], hlist)
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900871 flushed += tb->flush(tb);
872 }
Patrick McHardyabcab262006-08-10 23:11:47 -0700873
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900874 if (flushed)
875 dn_rt_cache_flush(-1);
Patrick McHardyabcab262006-08-10 23:11:47 -0700876}
877
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878void __init dn_fib_table_init(void)
879{
880 dn_hash_kmem = kmem_cache_create("dn_fib_info_cache",
881 sizeof(struct dn_fib_info),
882 0, SLAB_HWCACHE_ALIGN,
883 NULL, NULL);
884}
885
886void __exit dn_fib_table_cleanup(void)
887{
Patrick McHardyabcab262006-08-10 23:11:47 -0700888 struct dn_fib_table *t;
889 struct hlist_node *node, *next;
890 unsigned int h;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891
Patrick McHardyabcab262006-08-10 23:11:47 -0700892 write_lock(&dn_fib_tables_lock);
893 for (h = 0; h < DN_FIB_TABLE_HASHSZ; h++) {
894 hlist_for_each_entry_safe(t, node, next, &dn_fib_table_hash[h],
YOSHIFUJI Hideaki429eb0f2007-02-09 23:24:40 +0900895 hlist) {
Patrick McHardyabcab262006-08-10 23:11:47 -0700896 hlist_del(&t->hlist);
897 kfree(t);
898 }
899 }
900 write_unlock(&dn_fib_tables_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901}