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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
33 #include <linux/module.h>
34 #include <linux/kernel.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/init.h>
42 #include <linux/slab.h>
43 #include <linux/string.h>
44 #include <linux/errno.h>
45 #include <linux/netdevice.h>
46 #include <linux/skbuff.h>
47 #include <linux/if_arp.h>
48 #include <linux/if_frad.h>
49 #include <linux/bitops.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, const void *daddr,
71 const void *saddr, unsigned len)
74 struct dlci_local *dlp;
78 dlp = netdev_priv(dev);
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;
114 dlp = netdev_priv(dev);
115 if (!pskb_may_pull(skb, sizeof(*hdr))) {
116 netdev_notice(dev, "invalid data no header\n");
117 dev->stats.rx_errors++;
122 hdr = (struct frhdr *) skb->data;
127 if (hdr->control != FRAD_I_UI)
129 netdev_notice(dev, "Invalid header flag 0x%02X\n",
131 dev->stats.rx_errors++;
134 switch (hdr->IP_NLPID)
137 if (hdr->NLPID != FRAD_P_SNAP)
139 netdev_notice(dev, "Unsupported NLPID 0x%02X\n",
141 dev->stats.rx_errors++;
145 if (hdr->OUI[0] + hdr->OUI[1] + hdr->OUI[2] != 0)
147 netdev_notice(dev, "Unsupported organizationally unique identifier 0x%02X-%02X-%02X\n",
151 dev->stats.rx_errors++;
155 /* at this point, it's an EtherType frame */
156 header = sizeof(struct frhdr);
157 /* Already in network order ! */
158 skb->protocol = hdr->PID;
163 header = sizeof(hdr->control) + sizeof(hdr->IP_NLPID);
164 skb->protocol = htons(ETH_P_IP);
171 netdev_notice(dev, "Unsupported NLPID 0x%02X\n",
173 dev->stats.rx_errors++;
177 netdev_notice(dev, "Invalid pad byte 0x%02X\n",
179 dev->stats.rx_errors++;
185 /* we've set up the protocol, so discard the header */
186 skb_reset_mac_header(skb);
187 skb_pull(skb, header);
188 dev->stats.rx_bytes += skb->len;
190 dev->stats.rx_packets++;
196 static netdev_tx_t dlci_transmit(struct sk_buff *skb, struct net_device *dev)
198 struct dlci_local *dlp = netdev_priv(dev);
201 dlp->slave->netdev_ops->ndo_start_xmit(skb, dlp->slave);
205 static int dlci_config(struct net_device *dev, struct dlci_conf __user *conf, int get)
207 struct dlci_conf config;
208 struct dlci_local *dlp;
209 struct frad_local *flp;
212 dlp = netdev_priv(dev);
214 flp = netdev_priv(dlp->slave);
218 if (copy_from_user(&config, conf, sizeof(struct dlci_conf)))
220 if (config.flags & ~DLCI_VALID_FLAGS)
222 memcpy(&dlp->config, &config, sizeof(struct dlci_conf));
226 err = (*flp->dlci_conf)(dlp->slave, dev, get);
232 if (copy_to_user(conf, &dlp->config, sizeof(struct dlci_conf)))
239 static int dlci_dev_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
241 struct dlci_local *dlp;
243 if (!capable(CAP_NET_ADMIN))
246 dlp = netdev_priv(dev);
251 if (!*(short *)(dev->dev_addr))
254 strncpy(ifr->ifr_slave, dlp->slave->name, sizeof(ifr->ifr_slave));
259 if (!*(short *)(dev->dev_addr))
262 return dlci_config(dev, ifr->ifr_data, cmd == DLCI_GET_CONF);
271 static int dlci_change_mtu(struct net_device *dev, int new_mtu)
273 struct dlci_local *dlp = netdev_priv(dev);
275 return dev_set_mtu(dlp->slave, new_mtu);
278 static int dlci_open(struct net_device *dev)
280 struct dlci_local *dlp;
281 struct frad_local *flp;
284 dlp = netdev_priv(dev);
286 if (!*(short *)(dev->dev_addr))
289 if (!netif_running(dlp->slave))
292 flp = netdev_priv(dlp->slave);
293 err = (*flp->activate)(dlp->slave, dev);
297 netif_start_queue(dev);
302 static int dlci_close(struct net_device *dev)
304 struct dlci_local *dlp;
305 struct frad_local *flp;
308 netif_stop_queue(dev);
310 dlp = netdev_priv(dev);
312 flp = netdev_priv(dlp->slave);
313 err = (*flp->deactivate)(dlp->slave, dev);
318 static int dlci_add(struct dlci_add *dlci)
320 struct net_device *master, *slave;
321 struct dlci_local *dlp;
322 struct frad_local *flp;
326 /* validate slave device */
327 slave = dev_get_by_name(&init_net, dlci->devname);
331 if (slave->type != ARPHRD_FRAD || netdev_priv(slave) == NULL)
334 /* create device name */
335 master = alloc_netdev( sizeof(struct dlci_local), "dlci%d",
342 /* make sure same slave not already registered */
344 list_for_each_entry(dlp, &dlci_devs, list) {
345 if (dlp->slave == slave) {
351 *(short *)(master->dev_addr) = dlci->dlci;
353 dlp = netdev_priv(master);
355 dlp->master = master;
357 flp = netdev_priv(slave);
358 err = (*flp->assoc)(slave, master);
362 err = register_netdevice(master);
366 strcpy(dlci->devname, master->name);
368 list_add(&dlp->list, &dlci_devs);
381 static int dlci_del(struct dlci_add *dlci)
383 struct dlci_local *dlp;
384 struct frad_local *flp;
385 struct net_device *master, *slave;
388 /* validate slave device */
389 master = __dev_get_by_name(&init_net, dlci->devname);
393 if (netif_running(master)) {
397 dlp = netdev_priv(master);
399 flp = netdev_priv(slave);
402 err = (*flp->deassoc)(slave, master);
404 list_del(&dlp->list);
406 unregister_netdevice(master);
415 static int dlci_ioctl(unsigned int cmd, void __user *arg)
420 if (!capable(CAP_NET_ADMIN))
423 if (copy_from_user(&add, arg, sizeof(struct dlci_add)))
429 err = dlci_add(&add);
432 if (copy_to_user(arg, &add, sizeof(struct dlci_add)))
437 err = dlci_del(&add);
447 static const struct header_ops dlci_header_ops = {
448 .create = dlci_header,
451 static const struct net_device_ops dlci_netdev_ops = {
452 .ndo_open = dlci_open,
453 .ndo_stop = dlci_close,
454 .ndo_do_ioctl = dlci_dev_ioctl,
455 .ndo_start_xmit = dlci_transmit,
456 .ndo_change_mtu = dlci_change_mtu,
459 static void dlci_setup(struct net_device *dev)
461 struct dlci_local *dlp = netdev_priv(dev);
464 dev->header_ops = &dlci_header_ops;
465 dev->netdev_ops = &dlci_netdev_ops;
466 dev->destructor = free_netdev;
468 dlp->receive = dlci_receive;
470 dev->type = ARPHRD_DLCI;
471 dev->hard_header_len = sizeof(struct frhdr);
472 dev->addr_len = sizeof(short);
476 /* if slave is unregistering, then cleanup master */
477 static int dlci_dev_event(struct notifier_block *unused,
478 unsigned long event, void *ptr)
480 struct net_device *dev = (struct net_device *) ptr;
482 if (dev_net(dev) != &init_net)
485 if (event == NETDEV_UNREGISTER) {
486 struct dlci_local *dlp;
488 list_for_each_entry(dlp, &dlci_devs, list) {
489 if (dlp->slave == dev) {
490 list_del(&dlp->list);
491 unregister_netdevice(dlp->master);
500 static struct notifier_block dlci_notifier = {
501 .notifier_call = dlci_dev_event,
504 static int __init init_dlci(void)
506 dlci_ioctl_set(dlci_ioctl);
507 register_netdevice_notifier(&dlci_notifier);
509 printk("%s.\n", version);
514 static void __exit dlci_exit(void)
516 struct dlci_local *dlp, *nxt;
518 dlci_ioctl_set(NULL);
519 unregister_netdevice_notifier(&dlci_notifier);
522 list_for_each_entry_safe(dlp, nxt, &dlci_devs, list) {
523 unregister_netdevice(dlp->master);
529 module_init(init_dlci);
530 module_exit(dlci_exit);
532 MODULE_AUTHOR("Mike McLagan");
533 MODULE_DESCRIPTION("Frame Relay DLCI layer");
534 MODULE_LICENSE("GPL");