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/module.h>
32 #include <linux/kernel.h>
33 #include <linux/types.h>
34 #include <linux/fcntl.h>
35 #include <linux/interrupt.h>
36 #include <linux/ptrace.h>
37 #include <linux/ioport.h>
39 #include <linux/init.h>
40 #include <linux/slab.h>
41 #include <linux/string.h>
42 #include <linux/errno.h>
43 #include <linux/netdevice.h>
44 #include <linux/skbuff.h>
45 #include <linux/if_arp.h>
46 #include <linux/if_frad.h>
47 #include <linux/bitops.h>
51 #include <asm/system.h>
54 #include <asm/uaccess.h>
56 static const char version
[] = "DLCI driver v0.35, 4 Jan 1997, mike.mclagan@linux.org";
58 static LIST_HEAD(dlci_devs
);
60 static void dlci_setup(struct net_device
*);
63 * these encapsulate the RFC 1490 requirements as well as
64 * deal with packet transmission and reception, working with
65 * the upper network layers
68 static int dlci_header(struct sk_buff
*skb
, struct net_device
*dev
,
69 unsigned short type
, const void *daddr
,
70 const void *saddr
, unsigned len
)
73 struct dlci_local
*dlp
;
77 dlp
= netdev_priv(dev
);
79 hdr
.control
= FRAD_I_UI
;
83 hdr
.IP_NLPID
= FRAD_P_IP
;
84 hlen
= sizeof(hdr
.control
) + sizeof(hdr
.IP_NLPID
);
87 /* feel free to add other types, if necessary */
90 hdr
.pad
= FRAD_P_PADDING
;
91 hdr
.NLPID
= FRAD_P_SNAP
;
92 memset(hdr
.OUI
, 0, sizeof(hdr
.OUI
));
93 hdr
.PID
= htons(type
);
98 dest
= skb_push(skb
, hlen
);
102 memcpy(dest
, &hdr
, hlen
);
107 static void dlci_receive(struct sk_buff
*skb
, struct net_device
*dev
)
109 struct dlci_local
*dlp
;
113 dlp
= netdev_priv(dev
);
114 if (!pskb_may_pull(skb
, sizeof(*hdr
))) {
115 printk(KERN_NOTICE
"%s: invalid data no header\n",
117 dev
->stats
.rx_errors
++;
122 hdr
= (struct frhdr
*) skb
->data
;
127 if (hdr
->control
!= FRAD_I_UI
)
129 printk(KERN_NOTICE
"%s: Invalid header flag 0x%02X.\n", dev
->name
, hdr
->control
);
130 dev
->stats
.rx_errors
++;
133 switch(hdr
->IP_NLPID
)
136 if (hdr
->NLPID
!= FRAD_P_SNAP
)
138 printk(KERN_NOTICE
"%s: Unsupported NLPID 0x%02X.\n", dev
->name
, hdr
->NLPID
);
139 dev
->stats
.rx_errors
++;
143 if (hdr
->OUI
[0] + hdr
->OUI
[1] + hdr
->OUI
[2] != 0)
145 printk(KERN_NOTICE
"%s: Unsupported organizationally unique identifier 0x%02X-%02X-%02X.\n", dev
->name
, hdr
->OUI
[0], hdr
->OUI
[1], hdr
->OUI
[2]);
146 dev
->stats
.rx_errors
++;
150 /* at this point, it's an EtherType frame */
151 header
= sizeof(struct frhdr
);
152 /* Already in network order ! */
153 skb
->protocol
= hdr
->PID
;
158 header
= sizeof(hdr
->control
) + sizeof(hdr
->IP_NLPID
);
159 skb
->protocol
= htons(ETH_P_IP
);
166 printk(KERN_NOTICE
"%s: Unsupported NLPID 0x%02X.\n", dev
->name
, hdr
->pad
);
167 dev
->stats
.rx_errors
++;
171 printk(KERN_NOTICE
"%s: Invalid pad byte 0x%02X.\n", dev
->name
, hdr
->pad
);
172 dev
->stats
.rx_errors
++;
178 /* we've set up the protocol, so discard the header */
179 skb_reset_mac_header(skb
);
180 skb_pull(skb
, header
);
181 dev
->stats
.rx_bytes
+= skb
->len
;
183 dev
->stats
.rx_packets
++;
189 static int dlci_transmit(struct sk_buff
*skb
, struct net_device
*dev
)
191 struct dlci_local
*dlp
;
199 dlp
= netdev_priv(dev
);
201 netif_stop_queue(dev
);
203 ret
= dlp
->slave
->netdev_ops
->ndo_start_xmit(skb
, dlp
->slave
);
207 dev
->stats
.tx_packets
++;
211 dev
->stats
.tx_errors
++;
215 dev
->stats
.tx_dropped
++;
216 ret
= NETDEV_TX_BUSY
;
219 /* Alan Cox recommends always returning 0, and always freeing the packet */
220 /* experience suggest a slightly more conservative approach */
225 netif_wake_queue(dev
);
230 static int dlci_config(struct net_device
*dev
, struct dlci_conf __user
*conf
, int get
)
232 struct dlci_conf config
;
233 struct dlci_local
*dlp
;
234 struct frad_local
*flp
;
237 dlp
= netdev_priv(dev
);
239 flp
= netdev_priv(dlp
->slave
);
243 if(copy_from_user(&config
, conf
, sizeof(struct dlci_conf
)))
245 if (config
.flags
& ~DLCI_VALID_FLAGS
)
247 memcpy(&dlp
->config
, &config
, sizeof(struct dlci_conf
));
251 err
= (*flp
->dlci_conf
)(dlp
->slave
, dev
, get
);
257 if(copy_to_user(conf
, &dlp
->config
, sizeof(struct dlci_conf
)))
264 static int dlci_dev_ioctl(struct net_device
*dev
, struct ifreq
*ifr
, int cmd
)
266 struct dlci_local
*dlp
;
268 if (!capable(CAP_NET_ADMIN
))
271 dlp
= netdev_priv(dev
);
276 if (!*(short *)(dev
->dev_addr
))
279 strncpy(ifr
->ifr_slave
, dlp
->slave
->name
, sizeof(ifr
->ifr_slave
));
284 if (!*(short *)(dev
->dev_addr
))
287 return(dlci_config(dev
, ifr
->ifr_data
, cmd
== DLCI_GET_CONF
));
296 static int dlci_change_mtu(struct net_device
*dev
, int new_mtu
)
298 struct dlci_local
*dlp
= netdev_priv(dev
);
300 return dev_set_mtu(dlp
->slave
, new_mtu
);
303 static int dlci_open(struct net_device
*dev
)
305 struct dlci_local
*dlp
;
306 struct frad_local
*flp
;
309 dlp
= netdev_priv(dev
);
311 if (!*(short *)(dev
->dev_addr
))
314 if (!netif_running(dlp
->slave
))
317 flp
= netdev_priv(dlp
->slave
);
318 err
= (*flp
->activate
)(dlp
->slave
, dev
);
322 netif_start_queue(dev
);
327 static int dlci_close(struct net_device
*dev
)
329 struct dlci_local
*dlp
;
330 struct frad_local
*flp
;
333 netif_stop_queue(dev
);
335 dlp
= netdev_priv(dev
);
337 flp
= netdev_priv(dlp
->slave
);
338 err
= (*flp
->deactivate
)(dlp
->slave
, dev
);
343 static int dlci_add(struct dlci_add
*dlci
)
345 struct net_device
*master
, *slave
;
346 struct dlci_local
*dlp
;
347 struct frad_local
*flp
;
351 /* validate slave device */
352 slave
= dev_get_by_name(&init_net
, dlci
->devname
);
356 if (slave
->type
!= ARPHRD_FRAD
|| netdev_priv(slave
) == NULL
)
359 /* create device name */
360 master
= alloc_netdev( sizeof(struct dlci_local
), "dlci%d",
367 /* make sure same slave not already registered */
369 list_for_each_entry(dlp
, &dlci_devs
, list
) {
370 if (dlp
->slave
== slave
) {
376 err
= dev_alloc_name(master
, master
->name
);
380 *(short *)(master
->dev_addr
) = dlci
->dlci
;
382 dlp
= netdev_priv(master
);
384 dlp
->master
= master
;
386 flp
= netdev_priv(slave
);
387 err
= (*flp
->assoc
)(slave
, master
);
391 err
= register_netdevice(master
);
395 strcpy(dlci
->devname
, master
->name
);
397 list_add(&dlp
->list
, &dlci_devs
);
410 static int dlci_del(struct dlci_add
*dlci
)
412 struct dlci_local
*dlp
;
413 struct frad_local
*flp
;
414 struct net_device
*master
, *slave
;
417 /* validate slave device */
418 master
= __dev_get_by_name(&init_net
, dlci
->devname
);
422 if (netif_running(master
)) {
426 dlp
= netdev_priv(master
);
428 flp
= netdev_priv(slave
);
431 err
= (*flp
->deassoc
)(slave
, master
);
433 list_del(&dlp
->list
);
435 unregister_netdevice(master
);
444 static int dlci_ioctl(unsigned int cmd
, void __user
*arg
)
449 if (!capable(CAP_NET_ADMIN
))
452 if(copy_from_user(&add
, arg
, sizeof(struct dlci_add
)))
458 err
= dlci_add(&add
);
461 if(copy_to_user(arg
, &add
, sizeof(struct dlci_add
)))
466 err
= dlci_del(&add
);
476 static const struct header_ops dlci_header_ops
= {
477 .create
= dlci_header
,
480 static const struct net_device_ops dlci_netdev_ops
= {
481 .ndo_open
= dlci_open
,
482 .ndo_stop
= dlci_close
,
483 .ndo_do_ioctl
= dlci_dev_ioctl
,
484 .ndo_start_xmit
= dlci_transmit
,
485 .ndo_change_mtu
= dlci_change_mtu
,
488 static void dlci_setup(struct net_device
*dev
)
490 struct dlci_local
*dlp
= netdev_priv(dev
);
493 dev
->header_ops
= &dlci_header_ops
;
494 dev
->netdev_ops
= &dlci_netdev_ops
;
495 dev
->destructor
= free_netdev
;
497 dlp
->receive
= dlci_receive
;
499 dev
->type
= ARPHRD_DLCI
;
500 dev
->hard_header_len
= sizeof(struct frhdr
);
501 dev
->addr_len
= sizeof(short);
505 /* if slave is unregistering, then cleanup master */
506 static int dlci_dev_event(struct notifier_block
*unused
,
507 unsigned long event
, void *ptr
)
509 struct net_device
*dev
= (struct net_device
*) ptr
;
511 if (dev_net(dev
) != &init_net
)
514 if (event
== NETDEV_UNREGISTER
) {
515 struct dlci_local
*dlp
;
517 list_for_each_entry(dlp
, &dlci_devs
, list
) {
518 if (dlp
->slave
== dev
) {
519 list_del(&dlp
->list
);
520 unregister_netdevice(dlp
->master
);
529 static struct notifier_block dlci_notifier
= {
530 .notifier_call
= dlci_dev_event
,
533 static int __init
init_dlci(void)
535 dlci_ioctl_set(dlci_ioctl
);
536 register_netdevice_notifier(&dlci_notifier
);
538 printk("%s.\n", version
);
543 static void __exit
dlci_exit(void)
545 struct dlci_local
*dlp
, *nxt
;
547 dlci_ioctl_set(NULL
);
548 unregister_netdevice_notifier(&dlci_notifier
);
551 list_for_each_entry_safe(dlp
, nxt
, &dlci_devs
, list
) {
552 unregister_netdevice(dlp
->master
);
558 module_init(init_dlci
);
559 module_exit(dlci_exit
);
561 MODULE_AUTHOR("Mike McLagan");
562 MODULE_DESCRIPTION("Frame Relay DLCI layer");
563 MODULE_LICENSE("GPL");