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.
6 * DECnet Neighbour Functions (Adjacency Database and
9 * Author: Steve Whitehouse <SteveW@ACM.org>
13 * Steve Whitehouse : Fixed router listing routine
14 * Steve Whitehouse : Added error_report functions
15 * Steve Whitehouse : Added default router detection
16 * Steve Whitehouse : Hop counts in outgoing messages
17 * Steve Whitehouse : Fixed src/dst in outgoing messages so
18 * forwarding now stands a good chance of
20 * Steve Whitehouse : Fixed neighbour states (for now anyway).
21 * Steve Whitehouse : Made error_report functions dummies. This
22 * is not the right place to return skbs.
23 * Steve Whitehouse : Convert to seq_file
27 #include <linux/config.h>
28 #include <linux/net.h>
29 #include <linux/socket.h>
30 #include <linux/if_arp.h>
31 #include <linux/if_ether.h>
32 #include <linux/init.h>
33 #include <linux/proc_fs.h>
34 #include <linux/string.h>
35 #include <linux/netfilter_decnet.h>
36 #include <linux/spinlock.h>
37 #include <linux/seq_file.h>
38 #include <asm/atomic.h>
39 #include <net/neighbour.h>
43 #include <net/dn_dev.h>
44 #include <net/dn_neigh.h>
45 #include <net/dn_route.h>
47 static u32 dn_neigh_hash(const void *pkey, const struct net_device *dev);
48 static int dn_neigh_construct(struct neighbour *);
49 static void dn_long_error_report(struct neighbour *, struct sk_buff *);
50 static void dn_short_error_report(struct neighbour *, struct sk_buff *);
51 static int dn_long_output(struct sk_buff *);
52 static int dn_short_output(struct sk_buff *);
53 static int dn_phase3_output(struct sk_buff *);
57 * For talking to broadcast devices: Ethernet & PPP
59 static struct neigh_ops dn_long_ops = {
61 .error_report = dn_long_error_report,
62 .output = dn_long_output,
63 .connected_output = dn_long_output,
64 .hh_output = dev_queue_xmit,
65 .queue_xmit = dev_queue_xmit,
69 * For talking to pointopoint and multidrop devices: DDCMP and X.25
71 static struct neigh_ops dn_short_ops = {
73 .error_report = dn_short_error_report,
74 .output = dn_short_output,
75 .connected_output = dn_short_output,
76 .hh_output = dev_queue_xmit,
77 .queue_xmit = dev_queue_xmit,
81 * For talking to DECnet phase III nodes
83 static struct neigh_ops dn_phase3_ops = {
85 .error_report = dn_short_error_report, /* Can use short version here */
86 .output = dn_phase3_output,
87 .connected_output = dn_phase3_output,
88 .hh_output = dev_queue_xmit,
89 .queue_xmit = dev_queue_xmit
92 struct neigh_table dn_neigh_table = {
94 .entry_size = sizeof(struct dn_neigh),
95 .key_len = sizeof(dn_address),
96 .hash = dn_neigh_hash,
97 .constructor = dn_neigh_construct,
98 .id = "dn_neigh_cache",
100 .tbl = &dn_neigh_table,
102 .base_reachable_time = 30 * HZ,
103 .retrans_time = 1 * HZ,
104 .gc_staletime = 60 * HZ,
105 .reachable_time = 30 * HZ,
106 .delay_probe_time = 5 * HZ,
116 .gc_interval = 30 * HZ,
122 static u32 dn_neigh_hash(const void *pkey, const struct net_device *dev)
126 hash_val = *(dn_address *)pkey;
127 hash_val ^= (hash_val >> 10);
128 hash_val ^= (hash_val >> 3);
130 return hash_val & NEIGH_HASHMASK;
133 static int dn_neigh_construct(struct neighbour *neigh)
135 struct net_device *dev = neigh->dev;
136 struct dn_neigh *dn = (struct dn_neigh *)neigh;
137 struct dn_dev *dn_db = (struct dn_dev *)dev->dn_ptr;
142 if (dn_db->neigh_parms)
143 neigh->parms = dn_db->neigh_parms;
146 neigh->ops = &dn_long_ops;
148 neigh->ops = &dn_short_ops;
150 if (dn->flags & DN_NDFLAG_P3)
151 neigh->ops = &dn_phase3_ops;
153 neigh->nud_state = NUD_NOARP;
154 neigh->output = neigh->ops->connected_output;
156 if ((dev->type == ARPHRD_IPGRE) || (dev->flags & IFF_POINTOPOINT))
157 memcpy(neigh->ha, dev->broadcast, dev->addr_len);
158 else if ((dev->type == ARPHRD_ETHER) || (dev->type == ARPHRD_LOOPBACK))
159 dn_dn2eth(neigh->ha, dn->addr);
162 printk(KERN_DEBUG "Trying to create neigh for hw %d\n", dev->type);
167 * Make an estimate of the remote block size by assuming that its
168 * two less then the device mtu, which it true for ethernet (and
169 * other things which support long format headers) since there is
170 * an extra length field (of 16 bits) which isn't part of the
171 * ethernet headers and which the DECnet specs won't admit is part
172 * of the DECnet routing headers either.
174 * If we over estimate here its no big deal, the NSP negotiations
175 * will prevent us from sending packets which are too large for the
176 * remote node to handle. In any case this figure is normally updated
177 * by a hello message in most cases.
179 dn->blksize = dev->mtu - 2;
184 static void dn_long_error_report(struct neighbour *neigh, struct sk_buff *skb)
186 printk(KERN_DEBUG "dn_long_error_report: called\n");
191 static void dn_short_error_report(struct neighbour *neigh, struct sk_buff *skb)
193 printk(KERN_DEBUG "dn_short_error_report: called\n");
197 static int dn_neigh_output_packet(struct sk_buff *skb)
199 struct dst_entry *dst = skb->dst;
200 struct dn_route *rt = (struct dn_route *)dst;
201 struct neighbour *neigh = dst->neighbour;
202 struct net_device *dev = neigh->dev;
203 char mac_addr[ETH_ALEN];
205 dn_dn2eth(mac_addr, rt->rt_local_src);
206 if (!dev->hard_header || dev->hard_header(skb, dev, ntohs(skb->protocol), neigh->ha, mac_addr, skb->len) >= 0)
207 return neigh->ops->queue_xmit(skb);
210 printk(KERN_DEBUG "dn_neigh_output_packet: oops, can't send packet\n");
216 static int dn_long_output(struct sk_buff *skb)
218 struct dst_entry *dst = skb->dst;
219 struct neighbour *neigh = dst->neighbour;
220 struct net_device *dev = neigh->dev;
221 int headroom = dev->hard_header_len + sizeof(struct dn_long_packet) + 3;
223 struct dn_long_packet *lp;
224 struct dn_skb_cb *cb = DN_SKB_CB(skb);
227 if (skb_headroom(skb) < headroom) {
228 struct sk_buff *skb2 = skb_realloc_headroom(skb, headroom);
231 printk(KERN_CRIT "dn_long_output: no memory\n");
238 printk(KERN_INFO "dn_long_output: Increasing headroom\n");
241 data = skb_push(skb, sizeof(struct dn_long_packet) + 3);
242 lp = (struct dn_long_packet *)(data+3);
244 *((unsigned short *)data) = dn_htons(skb->len - 2);
245 *(data + 2) = 1 | DN_RT_F_PF; /* Padding */
247 lp->msgflg = DN_RT_PKT_LONG|(cb->rt_flags&(DN_RT_F_IE|DN_RT_F_RQR|DN_RT_F_RTS));
248 lp->d_area = lp->d_subarea = 0;
249 dn_dn2eth(lp->d_id, dn_ntohs(cb->dst));
250 lp->s_area = lp->s_subarea = 0;
251 dn_dn2eth(lp->s_id, dn_ntohs(cb->src));
253 lp->visit_ct = cb->hops & 0x3f;
257 skb->nh.raw = skb->data;
259 return NF_HOOK(PF_DECnet, NF_DN_POST_ROUTING, skb, NULL, neigh->dev, dn_neigh_output_packet);
262 static int dn_short_output(struct sk_buff *skb)
264 struct dst_entry *dst = skb->dst;
265 struct neighbour *neigh = dst->neighbour;
266 struct net_device *dev = neigh->dev;
267 int headroom = dev->hard_header_len + sizeof(struct dn_short_packet) + 2;
268 struct dn_short_packet *sp;
270 struct dn_skb_cb *cb = DN_SKB_CB(skb);
273 if (skb_headroom(skb) < headroom) {
274 struct sk_buff *skb2 = skb_realloc_headroom(skb, headroom);
277 printk(KERN_CRIT "dn_short_output: no memory\n");
284 printk(KERN_INFO "dn_short_output: Increasing headroom\n");
287 data = skb_push(skb, sizeof(struct dn_short_packet) + 2);
288 *((unsigned short *)data) = dn_htons(skb->len - 2);
289 sp = (struct dn_short_packet *)(data+2);
291 sp->msgflg = DN_RT_PKT_SHORT|(cb->rt_flags&(DN_RT_F_RQR|DN_RT_F_RTS));
292 sp->dstnode = cb->dst;
293 sp->srcnode = cb->src;
294 sp->forward = cb->hops & 0x3f;
296 skb->nh.raw = skb->data;
298 return NF_HOOK(PF_DECnet, NF_DN_POST_ROUTING, skb, NULL, neigh->dev, dn_neigh_output_packet);
302 * Phase 3 output is the same is short output, execpt that
303 * it clears the area bits before transmission.
305 static int dn_phase3_output(struct sk_buff *skb)
307 struct dst_entry *dst = skb->dst;
308 struct neighbour *neigh = dst->neighbour;
309 struct net_device *dev = neigh->dev;
310 int headroom = dev->hard_header_len + sizeof(struct dn_short_packet) + 2;
311 struct dn_short_packet *sp;
313 struct dn_skb_cb *cb = DN_SKB_CB(skb);
315 if (skb_headroom(skb) < headroom) {
316 struct sk_buff *skb2 = skb_realloc_headroom(skb, headroom);
319 printk(KERN_CRIT "dn_phase3_output: no memory\n");
326 printk(KERN_INFO "dn_phase3_output: Increasing headroom\n");
329 data = skb_push(skb, sizeof(struct dn_short_packet) + 2);
330 *((unsigned short *)data) = dn_htons(skb->len - 2);
331 sp = (struct dn_short_packet *)(data + 2);
333 sp->msgflg = DN_RT_PKT_SHORT|(cb->rt_flags&(DN_RT_F_RQR|DN_RT_F_RTS));
334 sp->dstnode = cb->dst & dn_htons(0x03ff);
335 sp->srcnode = cb->src & dn_htons(0x03ff);
336 sp->forward = cb->hops & 0x3f;
338 skb->nh.raw = skb->data;
340 return NF_HOOK(PF_DECnet, NF_DN_POST_ROUTING, skb, NULL, neigh->dev, dn_neigh_output_packet);
344 * Unfortunately, the neighbour code uses the device in its hash
345 * function, so we don't get any advantage from it. This function
346 * basically does a neigh_lookup(), but without comparing the device
347 * field. This is required for the On-Ethernet cache
349 struct neighbour *dn_neigh_lookup(struct neigh_table *tbl, const void *ptr)
351 struct neighbour *neigh;
354 hash_val = tbl->hash(ptr, NULL);
356 read_lock_bh(&tbl->lock);
357 for(neigh = tbl->hash_buckets[hash_val]; neigh != NULL; neigh = neigh->next) {
358 if (memcmp(neigh->primary_key, ptr, tbl->key_len) == 0) {
359 atomic_inc(&neigh->refcnt);
360 read_unlock_bh(&tbl->lock);
364 read_unlock_bh(&tbl->lock);
371 * Any traffic on a pointopoint link causes the timer to be reset
372 * for the entry in the neighbour table.
374 void dn_neigh_pointopoint_notify(struct sk_buff *skb)
380 * Pointopoint link receives a hello message
382 void dn_neigh_pointopoint_hello(struct sk_buff *skb)
388 * Ethernet router hello message received
390 int dn_neigh_router_hello(struct sk_buff *skb)
392 struct rtnode_hello_message *msg = (struct rtnode_hello_message *)skb->data;
394 struct neighbour *neigh;
396 struct dn_dev *dn_db;
399 src = dn_htons(dn_eth2dn(msg->id));
401 neigh = __neigh_lookup(&dn_neigh_table, &src, skb->dev, 1);
403 dn = (struct dn_neigh *)neigh;
406 write_lock(&neigh->lock);
408 neigh->used = jiffies;
409 dn_db = (struct dn_dev *)neigh->dev->dn_ptr;
411 if (!(neigh->nud_state & NUD_PERMANENT)) {
412 neigh->updated = jiffies;
414 if (neigh->dev->type == ARPHRD_ETHER)
415 memcpy(neigh->ha, &skb->mac.ethernet->h_source, ETH_ALEN);
417 dn->blksize = dn_ntohs(msg->blksize);
418 dn->priority = msg->priority;
420 dn->flags &= ~DN_NDFLAG_P3;
422 switch(msg->iinfo & DN_RT_INFO_TYPE) {
423 case DN_RT_INFO_L1RT:
424 dn->flags &=~DN_NDFLAG_R2;
425 dn->flags |= DN_NDFLAG_R1;
427 case DN_RT_INFO_L2RT:
428 dn->flags |= DN_NDFLAG_R2;
432 if (!dn_db->router) {
433 dn_db->router = neigh_clone(neigh);
435 if (msg->priority > ((struct dn_neigh *)dn_db->router)->priority)
436 neigh_release(xchg(&dn_db->router, neigh_clone(neigh)));
438 write_unlock(&neigh->lock);
439 neigh_release(neigh);
447 * Endnode hello message received
449 int dn_neigh_endnode_hello(struct sk_buff *skb)
451 struct endnode_hello_message *msg = (struct endnode_hello_message *)skb->data;
452 struct neighbour *neigh;
456 src = dn_htons(dn_eth2dn(msg->id));
458 neigh = __neigh_lookup(&dn_neigh_table, &src, skb->dev, 1);
460 dn = (struct dn_neigh *)neigh;
463 write_lock(&neigh->lock);
465 neigh->used = jiffies;
467 if (!(neigh->nud_state & NUD_PERMANENT)) {
468 neigh->updated = jiffies;
470 if (neigh->dev->type == ARPHRD_ETHER)
471 memcpy(neigh->ha, &skb->mac.ethernet->h_source, ETH_ALEN);
472 dn->flags &= ~(DN_NDFLAG_R1 | DN_NDFLAG_R2);
473 dn->blksize = dn_ntohs(msg->blksize);
477 write_unlock(&neigh->lock);
478 neigh_release(neigh);
485 static char *dn_find_slot(char *base, int max, int priority)
488 unsigned char *min = NULL;
490 base += 6; /* skip first id */
492 for(i = 0; i < max; i++) {
493 if (!min || (*base < *min))
495 base += 7; /* find next priority */
501 return (*min < priority) ? (min - 6) : NULL;
504 int dn_neigh_elist(struct net_device *dev, unsigned char *ptr, int n)
508 struct neighbour *neigh;
510 struct neigh_table *tbl = &dn_neigh_table;
511 unsigned char *rs = ptr;
512 struct dn_dev *dn_db = (struct dn_dev *)dev->dn_ptr;
514 read_lock_bh(&tbl->lock);
516 for(i = 0; i < NEIGH_HASHMASK; i++) {
517 for(neigh = tbl->hash_buckets[i]; neigh != NULL; neigh = neigh->next) {
518 if (neigh->dev != dev)
520 dn = (struct dn_neigh *)neigh;
521 if (!(dn->flags & (DN_NDFLAG_R1|DN_NDFLAG_R2)))
523 if (dn_db->parms.forwarding == 1 && (dn->flags & DN_NDFLAG_R2))
526 rs = dn_find_slot(ptr, n, dn->priority);
531 dn_dn2eth(rs, dn->addr);
533 *rs = neigh->nud_state & NUD_CONNECTED ? 0x80 : 0x0;
539 read_unlock_bh(&tbl->lock);
545 #ifdef CONFIG_PROC_FS
547 struct dn_neigh_iter_state {
551 static struct neighbour *neigh_get_first(struct seq_file *seq)
553 struct dn_neigh_iter_state *state = seq->private;
554 struct neighbour *n = NULL;
556 for(state->bucket = 0;
557 state->bucket <= NEIGH_HASHMASK;
559 n = dn_neigh_table.hash_buckets[state->bucket];
567 static struct neighbour *neigh_get_next(struct seq_file *seq,
570 struct dn_neigh_iter_state *state = seq->private;
576 if (++state->bucket > NEIGH_HASHMASK)
578 n = dn_neigh_table.hash_buckets[state->bucket];
584 static struct neighbour *neigh_get_idx(struct seq_file *seq, loff_t *pos)
586 struct neighbour *n = neigh_get_first(seq);
589 while(*pos && (n = neigh_get_next(seq, n)))
591 return *pos ? NULL : n;
594 static void *dn_neigh_get_idx(struct seq_file *seq, loff_t pos)
597 read_lock_bh(&dn_neigh_table.lock);
598 rc = neigh_get_idx(seq, &pos);
600 read_unlock_bh(&dn_neigh_table.lock);
605 static void *dn_neigh_seq_start(struct seq_file *seq, loff_t *pos)
607 return *pos ? dn_neigh_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
610 static void *dn_neigh_seq_next(struct seq_file *seq, void *v, loff_t *pos)
615 if (v == SEQ_START_TOKEN) {
616 rc = dn_neigh_get_idx(seq, 0);
620 rc = neigh_get_next(seq, v);
623 read_unlock_bh(&dn_neigh_table.lock);
629 static void dn_neigh_seq_stop(struct seq_file *seq, void *v)
631 if (v && v != SEQ_START_TOKEN)
632 read_unlock_bh(&dn_neigh_table.lock);
635 static inline void dn_neigh_format_entry(struct seq_file *seq,
638 struct dn_neigh *dn = (struct dn_neigh *)n;
639 char buf[DN_ASCBUF_LEN];
642 seq_printf(seq, "%-7s %s%s%s %02x %02d %07ld %-8s\n",
643 dn_addr2asc(dn_ntohs(dn->addr), buf),
644 (dn->flags&DN_NDFLAG_R1) ? "1" : "-",
645 (dn->flags&DN_NDFLAG_R2) ? "2" : "-",
646 (dn->flags&DN_NDFLAG_P3) ? "3" : "-",
648 atomic_read(&dn->n.refcnt),
650 (dn->n.dev) ? dn->n.dev->name : "?");
651 read_unlock(&n->lock);
654 static int dn_neigh_seq_show(struct seq_file *seq, void *v)
656 if (v == SEQ_START_TOKEN) {
657 seq_puts(seq, "Addr Flags State Use Blksize Dev\n");
659 dn_neigh_format_entry(seq, v);
665 static struct seq_operations dn_neigh_seq_ops = {
666 .start = dn_neigh_seq_start,
667 .next = dn_neigh_seq_next,
668 .stop = dn_neigh_seq_stop,
669 .show = dn_neigh_seq_show,
672 static int dn_neigh_seq_open(struct inode *inode, struct file *file)
674 struct seq_file *seq;
676 struct dn_neigh_iter_state *s = kmalloc(sizeof(*s), GFP_KERNEL);
681 rc = seq_open(file, &dn_neigh_seq_ops);
685 seq = file->private_data;
687 memset(s, 0, sizeof(*s));
695 static struct file_operations dn_neigh_seq_fops = {
696 .owner = THIS_MODULE,
697 .open = dn_neigh_seq_open,
700 .release = seq_release_private,
705 void __init dn_neigh_init(void)
707 neigh_table_init(&dn_neigh_table);
708 proc_net_fops_create("decnet_neigh", S_IRUGO, &dn_neigh_seq_fops);
711 void __exit dn_neigh_cleanup(void)
713 proc_net_remove("decnet_neigh");
714 neigh_table_clear(&dn_neigh_table);