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 which 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/config.h> /* for CONFIG_DLCI_COUNT */
32 #include <linux/module.h>
33 #include <linux/kernel.h>
34 #include <linux/types.h>
35 #include <linux/fcntl.h>
36 #include <linux/interrupt.h>
37 #include <linux/ptrace.h>
38 #include <linux/ioport.h>
40 #include <linux/init.h>
41 #include <linux/slab.h>
42 #include <linux/string.h>
43 #include <linux/errno.h>
44 #include <linux/netdevice.h>
45 #include <linux/skbuff.h>
46 #include <linux/if_arp.h>
47 #include <linux/if_frad.h>
48 #include <linux/bitops.h>
52 #include <asm/system.h>
55 #include <asm/uaccess.h>
57 static const char version[] = "DLCI driver v0.35, 4 Jan 1997, mike.mclagan@linux.org";
59 static LIST_HEAD(dlci_devs);
61 static void dlci_setup(struct net_device *);
64 * these encapsulate the RFC 1490 requirements as well as
65 * deal with packet transmission and reception, working with
66 * the upper network layers
69 static int dlci_header(struct sk_buff *skb, struct net_device *dev,
70 unsigned short type, void *daddr, void *saddr,
74 struct dlci_local *dlp;
80 hdr.control = FRAD_I_UI;
84 hdr.IP_NLPID = FRAD_P_IP;
85 hlen = sizeof(hdr.control) + sizeof(hdr.IP_NLPID);
88 /* feel free to add other types, if necessary */
91 hdr.pad = FRAD_P_PADDING;
92 hdr.NLPID = FRAD_P_SNAP;
93 memset(hdr.OUI, 0, sizeof(hdr.OUI));
94 hdr.PID = htons(type);
99 dest = skb_push(skb, hlen);
103 memcpy(dest, &hdr, hlen);
108 static void dlci_receive(struct sk_buff *skb, struct net_device *dev)
110 struct dlci_local *dlp;
115 if (!pskb_may_pull(skb, sizeof(*hdr))) {
116 printk(KERN_NOTICE "%s: invalid data no header\n",
118 dlp->stats.rx_errors++;
123 hdr = (struct frhdr *) skb->data;
128 if (hdr->control != FRAD_I_UI)
130 printk(KERN_NOTICE "%s: Invalid header flag 0x%02X.\n", dev->name, hdr->control);
131 dlp->stats.rx_errors++;
134 switch(hdr->IP_NLPID)
137 if (hdr->NLPID != FRAD_P_SNAP)
139 printk(KERN_NOTICE "%s: Unsupported NLPID 0x%02X.\n", dev->name, hdr->NLPID);
140 dlp->stats.rx_errors++;
144 if (hdr->OUI[0] + hdr->OUI[1] + hdr->OUI[2] != 0)
146 printk(KERN_NOTICE "%s: Unsupported organizationally unique identifier 0x%02X-%02X-%02X.\n", dev->name, hdr->OUI[0], hdr->OUI[1], hdr->OUI[2]);
147 dlp->stats.rx_errors++;
151 /* at this point, it's an EtherType frame */
152 header = sizeof(struct frhdr);
153 /* Already in network order ! */
154 skb->protocol = hdr->PID;
159 header = sizeof(hdr->control) + sizeof(hdr->IP_NLPID);
160 skb->protocol = htons(ETH_P_IP);
167 printk(KERN_NOTICE "%s: Unsupported NLPID 0x%02X.\n", dev->name, hdr->pad);
168 dlp->stats.rx_errors++;
172 printk(KERN_NOTICE "%s: Invalid pad byte 0x%02X.\n", dev->name, hdr->pad);
173 dlp->stats.rx_errors++;
179 /* we've set up the protocol, so discard the header */
180 skb->mac.raw = skb->data;
181 skb_pull(skb, header);
182 dlp->stats.rx_bytes += skb->len;
184 dlp->stats.rx_packets++;
185 dev->last_rx = jiffies;
191 static int dlci_transmit(struct sk_buff *skb, struct net_device *dev)
193 struct dlci_local *dlp;
203 netif_stop_queue(dev);
205 ret = dlp->slave->hard_start_xmit(skb, dlp->slave);
209 dlp->stats.tx_packets++;
213 dlp->stats.tx_errors++;
217 dlp->stats.tx_dropped++;
221 /* Alan Cox recommends always returning 0, and always freeing the packet */
222 /* experience suggest a slightly more conservative approach */
227 netif_wake_queue(dev);
232 static int dlci_config(struct net_device *dev, struct dlci_conf __user *conf, int get)
234 struct dlci_conf config;
235 struct dlci_local *dlp;
236 struct frad_local *flp;
241 flp = dlp->slave->priv;
245 if(copy_from_user(&config, conf, sizeof(struct dlci_conf)))
247 if (config.flags & ~DLCI_VALID_FLAGS)
249 memcpy(&dlp->config, &config, sizeof(struct dlci_conf));
253 err = (*flp->dlci_conf)(dlp->slave, dev, get);
259 if(copy_to_user(conf, &dlp->config, sizeof(struct dlci_conf)))
266 static int dlci_dev_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
268 struct dlci_local *dlp;
270 if (!capable(CAP_NET_ADMIN))
278 if (!*(short *)(dev->dev_addr))
281 strncpy(ifr->ifr_slave, dlp->slave->name, sizeof(ifr->ifr_slave));
286 if (!*(short *)(dev->dev_addr))
289 return(dlci_config(dev, ifr->ifr_data, cmd == DLCI_GET_CONF));
298 static int dlci_change_mtu(struct net_device *dev, int new_mtu)
300 struct dlci_local *dlp;
304 return((*dlp->slave->change_mtu)(dlp->slave, new_mtu));
307 static int dlci_open(struct net_device *dev)
309 struct dlci_local *dlp;
310 struct frad_local *flp;
315 if (!*(short *)(dev->dev_addr))
318 if (!netif_running(dlp->slave))
321 flp = dlp->slave->priv;
322 err = (*flp->activate)(dlp->slave, dev);
326 netif_start_queue(dev);
331 static int dlci_close(struct net_device *dev)
333 struct dlci_local *dlp;
334 struct frad_local *flp;
337 netif_stop_queue(dev);
341 flp = dlp->slave->priv;
342 err = (*flp->deactivate)(dlp->slave, dev);
347 static struct net_device_stats *dlci_get_stats(struct net_device *dev)
349 struct dlci_local *dlp;
356 static int dlci_add(struct dlci_add *dlci)
358 struct net_device *master, *slave;
359 struct dlci_local *dlp;
360 struct frad_local *flp;
364 /* validate slave device */
365 slave = dev_get_by_name(dlci->devname);
369 if (slave->type != ARPHRD_FRAD || slave->priv == NULL)
372 /* create device name */
373 master = alloc_netdev( sizeof(struct dlci_local), "dlci%d",
380 /* make sure same slave not already registered */
382 list_for_each_entry(dlp, &dlci_devs, list) {
383 if (dlp->slave == slave) {
389 err = dev_alloc_name(master, master->name);
393 *(short *)(master->dev_addr) = dlci->dlci;
395 dlp = (struct dlci_local *) master->priv;
397 dlp->master = master;
400 err = (*flp->assoc)(slave, master);
404 err = register_netdevice(master);
408 strcpy(dlci->devname, master->name);
410 list_add(&dlp->list, &dlci_devs);
423 static int dlci_del(struct dlci_add *dlci)
425 struct dlci_local *dlp;
426 struct frad_local *flp;
427 struct net_device *master, *slave;
430 /* validate slave device */
431 master = __dev_get_by_name(dlci->devname);
435 if (netif_running(master)) {
444 err = (*flp->deassoc)(slave, master);
446 list_del(&dlp->list);
448 unregister_netdevice(master);
457 static int dlci_ioctl(unsigned int cmd, void __user *arg)
462 if (!capable(CAP_NET_ADMIN))
465 if(copy_from_user(&add, arg, sizeof(struct dlci_add)))
471 err = dlci_add(&add);
474 if(copy_to_user(arg, &add, sizeof(struct dlci_add)))
479 err = dlci_del(&add);
489 static void dlci_setup(struct net_device *dev)
491 struct dlci_local *dlp = dev->priv;
494 dev->open = dlci_open;
495 dev->stop = dlci_close;
496 dev->do_ioctl = dlci_dev_ioctl;
497 dev->hard_start_xmit = dlci_transmit;
498 dev->hard_header = dlci_header;
499 dev->get_stats = dlci_get_stats;
500 dev->change_mtu = dlci_change_mtu;
501 dev->destructor = free_netdev;
503 dlp->receive = dlci_receive;
505 dev->type = ARPHRD_DLCI;
506 dev->hard_header_len = sizeof(struct frhdr);
507 dev->addr_len = sizeof(short);
511 /* if slave is unregistering, then cleanup master */
512 static int dlci_dev_event(struct notifier_block *unused,
513 unsigned long event, void *ptr)
515 struct net_device *dev = (struct net_device *) ptr;
517 if (event == NETDEV_UNREGISTER) {
518 struct dlci_local *dlp;
520 list_for_each_entry(dlp, &dlci_devs, list) {
521 if (dlp->slave == dev) {
522 list_del(&dlp->list);
523 unregister_netdevice(dlp->master);
532 static struct notifier_block dlci_notifier = {
533 .notifier_call = dlci_dev_event,
536 static int __init init_dlci(void)
538 dlci_ioctl_set(dlci_ioctl);
539 register_netdevice_notifier(&dlci_notifier);
541 printk("%s.\n", version);
546 static void __exit dlci_exit(void)
548 struct dlci_local *dlp, *nxt;
550 dlci_ioctl_set(NULL);
551 unregister_netdevice_notifier(&dlci_notifier);
554 list_for_each_entry_safe(dlp, nxt, &dlci_devs, list) {
555 unregister_netdevice(dlp->master);
561 module_init(init_dlci);
562 module_exit(dlci_exit);
564 MODULE_AUTHOR("Mike McLagan");
565 MODULE_DESCRIPTION("Frame Relay DLCI layer");
566 MODULE_LICENSE("GPL");