2 * DLCI Implementation of Frame Relay protocol for Linux, according to
3 * RFC 1490. This generic device provides en/decapsulation for an
4 * underlying hardware driver. Routes & IPs are assigned to these
5 * interfaces. Requires 'dlcicfg' program to create usable
6 * interfaces, the initial one, 'dlci' is for IOCTL use only.
8 * Version: @(#)dlci.c 0.35 4 Jan 1997
10 * Author: Mike McLagan <mike.mclagan@linux.org>
14 * 0.15 Mike Mclagan Packet freeing, bug in kmalloc call
16 * 0.20 Mike McLagan More conservative on which packets
17 * are returned for retry and whic are
18 * are dropped. If DLCI_RET_DROP is
19 * returned from the FRAD, the packet is
20 * sent back to Linux for re-transmission
21 * 0.25 Mike McLagan Converted to use SIOC IOCTL calls
22 * 0.30 Jim Freeman Fixed to allow IPX traffic
23 * 0.35 Michael Elizabeth Fixed incorrect memcpy_fromfs
25 * This program is free software; you can redistribute it and/or
26 * modify it under the terms of the GNU General Public License
27 * as published by the Free Software Foundation; either version
28 * 2 of the License, or (at your option) any later version.
31 #include <linux/module.h>
33 #include <linux/kernel.h>
34 #include <linux/sched.h>
35 #include <linux/types.h>
36 #include <linux/fcntl.h>
37 #include <linux/interrupt.h>
38 #include <linux/ptrace.h>
39 #include <linux/ioport.h>
41 #include <linux/malloc.h>
42 #include <linux/string.h>
43 #include <linux/init.h>
44 #include <asm/system.h>
45 #include <asm/bitops.h>
48 #include <asm/uaccess.h>
50 #include <linux/errno.h>
52 #include <linux/netdevice.h>
53 #include <linux/skbuff.h>
54 #include <linux/if_arp.h>
55 #include <linux/if_frad.h>
59 static const char *devname
= "dlci";
60 static const char *version
= "DLCI driver v0.35, 4 Jan 1997, mike.mclagan@linux.org";
62 static struct device
*open_dev
[CONFIG_DLCI_COUNT
];
64 static char *basename
[16];
66 int dlci_init(struct device
*dev
);
68 /* allow FRAD's to register their name as a valid FRAD */
69 int register_frad(const char *name
)
76 for (i
=0;i
<sizeof(basename
) / sizeof(char *);i
++)
81 /* take care of multiple registrations */
82 if (strcmp(basename
[i
], name
) == 0)
86 if (i
== sizeof(basename
) / sizeof(char *))
89 basename
[i
] = kmalloc(strlen(name
) + 1, GFP_KERNEL
);
93 strcpy(basename
[i
], name
);
98 int unregister_frad(const char *name
)
105 for (i
=0;i
<sizeof(basename
) / sizeof(char *);i
++)
106 if (basename
[i
] && (strcmp(basename
[i
], name
) == 0))
109 if (i
== sizeof(basename
) / sizeof(char *))
119 * these encapsulate the RFC 1490 requirements as well as
120 * deal with packet transmission and reception, working with
121 * the upper network layers
124 static int dlci_header(struct sk_buff
*skb
, struct device
*dev
,
125 unsigned short type
, void *daddr
, void *saddr
,
129 struct dlci_local
*dlp
;
135 hdr
.control
= FRAD_I_UI
;
139 hdr
.IP_NLPID
= FRAD_P_IP
;
140 hlen
= sizeof(hdr
.control
) + sizeof(hdr
.IP_NLPID
);
143 /* feel free to add other types, if necessary */
146 hdr
.pad
= FRAD_P_PADDING
;
147 hdr
.NLPID
= FRAD_P_SNAP
;
148 memset(hdr
.OUI
, 0, sizeof(hdr
.OUI
));
149 hdr
.PID
= htons(type
);
154 dest
= skb_push(skb
, hlen
);
158 memcpy(dest
, &hdr
, hlen
);
163 static void dlci_receive(struct sk_buff
*skb
, struct device
*dev
)
165 struct dlci_local
*dlp
;
170 hdr
= (struct frhdr
*) skb
->data
;
175 if (hdr
->control
!= FRAD_I_UI
)
177 printk(KERN_NOTICE
"%s: Invalid header flag 0x%02X.\n", dev
->name
, hdr
->control
);
178 dlp
->stats
.rx_errors
++;
181 switch(hdr
->IP_NLPID
)
184 if (hdr
->NLPID
!= FRAD_P_SNAP
)
186 printk(KERN_NOTICE
"%s: Unsupported NLPID 0x%02X.\n", dev
->name
, hdr
->NLPID
);
187 dlp
->stats
.rx_errors
++;
191 if (hdr
->OUI
[0] + hdr
->OUI
[1] + hdr
->OUI
[2] != 0)
193 printk(KERN_NOTICE
"%s: Unsupported organizationally unique identifier 0x%02X-%02X-%02X.\n", dev
->name
, hdr
->OUI
[0], hdr
->OUI
[1], hdr
->OUI
[2]);
194 dlp
->stats
.rx_errors
++;
198 /* at this point, it's an EtherType frame */
199 header
= sizeof(struct frhdr
);
200 /* Already in network order ! */
201 skb
->protocol
= hdr
->PID
;
206 header
= sizeof(hdr
->control
) + sizeof(hdr
->IP_NLPID
);
207 skb
->protocol
= htons(ETH_P_IP
);
214 printk(KERN_NOTICE
"%s: Unsupported NLPID 0x%02X.\n", dev
->name
, hdr
->pad
);
215 dlp
->stats
.rx_errors
++;
219 printk(KERN_NOTICE
"%s: Invalid pad byte 0x%02X.\n", dev
->name
, hdr
->pad
);
220 dlp
->stats
.rx_errors
++;
226 /* we've set up the protocol, so discard the header */
227 skb
->mac
.raw
= skb
->data
;
228 skb_pull(skb
, header
);
230 dlp
->stats
.rx_packets
++;
233 dev_kfree_skb(skb
, FREE_WRITE
);
236 static int dlci_transmit(struct sk_buff
*skb
, struct device
*dev
)
238 struct dlci_local
*dlp
;
251 if (test_and_set_bit(0, (void*)&dev
->tbusy
) != 0)
252 printk(KERN_WARNING
"%s: transmitter access conflict.\n", dev
->name
);
255 ret
= dlp
->slave
->hard_start_xmit(skb
, dlp
->slave
);
259 dlp
->stats
.tx_packets
++;
264 dlp
->stats
.tx_errors
++;
269 dlp
->stats
.tx_dropped
++;
274 /* Alan Cox recommends always returning 0, and always freeing the packet */
275 /* experience suggest a slightly more conservative approach */
278 dev_kfree_skb(skb
, FREE_WRITE
);
286 int dlci_config(struct device
*dev
, struct dlci_conf
*conf
, int get
)
288 struct dlci_conf config
;
289 struct dlci_local
*dlp
;
290 struct frad_local
*flp
;
295 flp
= dlp
->slave
->priv
;
299 if(copy_from_user(&config
, conf
, sizeof(struct dlci_conf
)))
301 if (config
.flags
& ~DLCI_VALID_FLAGS
)
303 memcpy(&dlp
->config
, &config
, sizeof(struct dlci_conf
));
307 err
= (*flp
->dlci_conf
)(dlp
->slave
, dev
, get
);
313 if(copy_to_user(conf
, &dlp
->config
, sizeof(struct dlci_conf
)))
320 int dlci_dev_ioctl(struct device
*dev
, struct ifreq
*ifr
, int cmd
)
322 struct dlci_local
*dlp
;
332 if (!*(short *)(dev
->dev_addr
))
335 strncpy(ifr
->ifr_slave
, dlp
->slave
->name
, sizeof(ifr
->ifr_slave
));
340 if (!*(short *)(dev
->dev_addr
))
343 return(dlci_config(dev
, (struct dlci_conf
*) ifr
->ifr_data
, cmd
== DLCI_GET_CONF
));
352 static int dlci_change_mtu(struct device
*dev
, int new_mtu
)
354 struct dlci_local
*dlp
;
358 return((*dlp
->slave
->change_mtu
)(dlp
->slave
, new_mtu
));
361 static int dlci_open(struct device
*dev
)
363 struct dlci_local
*dlp
;
364 struct frad_local
*flp
;
369 if (!*(short *)(dev
->dev_addr
))
372 if (!dlp
->slave
->start
)
380 flp
= dlp
->slave
->priv
;
381 err
= (*flp
->activate
)(dlp
->slave
, dev
);
388 static int dlci_close(struct device
*dev
)
390 struct dlci_local
*dlp
;
391 struct frad_local
*flp
;
396 flp
= dlp
->slave
->priv
;
397 err
= (*flp
->deactivate
)(dlp
->slave
, dev
);
405 static struct net_device_stats
*dlci_get_stats(struct device
*dev
)
407 struct dlci_local
*dlp
;
414 int dlci_add(struct dlci_add
*dlci
)
416 struct device
*master
, *slave
;
417 struct dlci_local
*dlp
;
418 struct frad_local
*flp
;
422 /* validate slave device */
423 slave
= dev_get(dlci
->devname
);
427 if (slave
->type
!= ARPHRD_FRAD
)
430 /* check for registration */
431 for (i
=0;i
<sizeof(basename
) / sizeof(char *); i
++)
433 (strncmp(dlci
->devname
, basename
[i
], strlen(basename
[i
])) == 0) &&
434 (strlen(dlci
->devname
) > strlen(basename
[i
])))
437 if (i
== sizeof(basename
) / sizeof(char *))
440 /* check for too many open devices : should this be dynamic ? */
441 for(i
=0;i
<CONFIG_DLCI_COUNT
;i
++)
445 if (i
== CONFIG_DLCI_COUNT
)
446 return(-ENOSPC
); /* #### Alan: Comments on this?? */
448 /* create device name */
449 sprintf(buf
, "%s%02i", devname
, i
);
451 master
= kmalloc(sizeof(*master
), GFP_KERNEL
);
455 memset(master
, 0, sizeof(*master
));
456 master
->name
= kmalloc(strlen(buf
) + 1, GFP_KERNEL
);
464 strcpy(master
->name
, buf
);
465 master
->init
= dlci_init
;
468 err
= register_netdev(master
);
476 *(short *)(master
->dev_addr
) = dlci
->dlci
;
478 dlp
= (struct dlci_local
*) master
->priv
;
482 err
= flp
? (*flp
->assoc
)(slave
, master
) : -EINVAL
;
485 unregister_netdev(master
);
492 strcpy(dlci
->devname
, buf
);
493 open_dev
[i
] = master
;
498 int dlci_del(struct dlci_add
*dlci
)
500 struct dlci_local
*dlp
;
501 struct frad_local
*flp
;
502 struct device
*master
, *slave
;
505 /* validate slave device */
506 master
= dev_get(dlci
->devname
);
517 err
= (*flp
->deassoc
)(slave
, master
);
521 unregister_netdev(master
);
523 for(i
=0;i
<CONFIG_DLCI_COUNT
;i
++)
524 if (master
== open_dev
[i
])
527 if (i
<CONFIG_DLCI_COUNT
)
539 int dlci_ioctl(unsigned int cmd
, void *arg
)
547 if(copy_from_user(&add
, arg
, sizeof(struct dlci_add
)))
553 err
= dlci_add(&add
);
556 if(copy_to_user(arg
, &add
, sizeof(struct dlci_add
)))
561 err
= dlci_del(&add
);
571 int dlci_init(struct device
*dev
)
573 struct dlci_local
*dlp
;
575 dev
->priv
= kmalloc(sizeof(struct dlci_local
), GFP_KERNEL
);
579 memset(dev
->priv
, 0, sizeof(struct dlci_local
));
583 dev
->open
= dlci_open
;
584 dev
->stop
= dlci_close
;
585 dev
->do_ioctl
= dlci_dev_ioctl
;
586 dev
->hard_start_xmit
= dlci_transmit
;
587 dev
->hard_header
= dlci_header
;
588 dev
->get_stats
= dlci_get_stats
;
589 dev
->change_mtu
= dlci_change_mtu
;
591 dlp
->receive
= dlci_receive
;
593 dev
->type
= ARPHRD_DLCI
;
594 dev
->family
= AF_INET
;
595 dev
->hard_header_len
= sizeof(struct frhdr
);
597 dev
->addr_len
= sizeof(short);
598 memset(dev
->dev_addr
, 0, sizeof(dev
->dev_addr
));
605 dev_init_buffers(dev
);
610 __initfunc(int dlci_setup(void))
614 printk("%s.\n", version
);
616 for(i
=0;i
<CONFIG_DLCI_COUNT
;i
++)
619 for(i
=0;i
<sizeof(basename
) / sizeof(char *);i
++)
627 extern int (*dlci_ioctl_hook
)(unsigned int, void *);
629 int init_module(void)
631 dlci_ioctl_hook
= dlci_ioctl
;
633 return(dlci_setup());
636 void cleanup_module(void)
638 dlci_ioctl_hook
= NULL
;