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>
53 #include <asm/system.h>
56 #include <asm/uaccess.h>
58 static const char version
[] = "DLCI driver v0.35, 4 Jan 1997, mike.mclagan@linux.org";
60 static LIST_HEAD(dlci_devs
);
62 static void dlci_setup(struct net_device
*);
65 * these encapsulate the RFC 1490 requirements as well as
66 * deal with packet transmission and reception, working with
67 * the upper network layers
70 static int dlci_header(struct sk_buff
*skb
, struct net_device
*dev
,
71 unsigned short type
, const void *daddr
,
72 const void *saddr
, unsigned len
)
75 struct dlci_local
*dlp
;
79 dlp
= netdev_priv(dev
);
81 hdr
.control
= FRAD_I_UI
;
85 hdr
.IP_NLPID
= FRAD_P_IP
;
86 hlen
= sizeof(hdr
.control
) + sizeof(hdr
.IP_NLPID
);
89 /* feel free to add other types, if necessary */
92 hdr
.pad
= FRAD_P_PADDING
;
93 hdr
.NLPID
= FRAD_P_SNAP
;
94 memset(hdr
.OUI
, 0, sizeof(hdr
.OUI
));
95 hdr
.PID
= htons(type
);
100 dest
= skb_push(skb
, hlen
);
104 memcpy(dest
, &hdr
, hlen
);
109 static void dlci_receive(struct sk_buff
*skb
, struct net_device
*dev
)
111 struct dlci_local
*dlp
;
115 dlp
= netdev_priv(dev
);
116 if (!pskb_may_pull(skb
, sizeof(*hdr
))) {
117 netdev_notice(dev
, "invalid data no header\n");
118 dev
->stats
.rx_errors
++;
123 hdr
= (struct frhdr
*) skb
->data
;
128 if (hdr
->control
!= FRAD_I_UI
)
130 netdev_notice(dev
, "Invalid header flag 0x%02X\n",
132 dev
->stats
.rx_errors
++;
135 switch (hdr
->IP_NLPID
)
138 if (hdr
->NLPID
!= FRAD_P_SNAP
)
140 netdev_notice(dev
, "Unsupported NLPID 0x%02X\n",
142 dev
->stats
.rx_errors
++;
146 if (hdr
->OUI
[0] + hdr
->OUI
[1] + hdr
->OUI
[2] != 0)
148 netdev_notice(dev
, "Unsupported organizationally unique identifier 0x%02X-%02X-%02X\n",
152 dev
->stats
.rx_errors
++;
156 /* at this point, it's an EtherType frame */
157 header
= sizeof(struct frhdr
);
158 /* Already in network order ! */
159 skb
->protocol
= hdr
->PID
;
164 header
= sizeof(hdr
->control
) + sizeof(hdr
->IP_NLPID
);
165 skb
->protocol
= htons(ETH_P_IP
);
172 netdev_notice(dev
, "Unsupported NLPID 0x%02X\n",
174 dev
->stats
.rx_errors
++;
178 netdev_notice(dev
, "Invalid pad byte 0x%02X\n",
180 dev
->stats
.rx_errors
++;
186 /* we've set up the protocol, so discard the header */
187 skb_reset_mac_header(skb
);
188 skb_pull(skb
, header
);
189 dev
->stats
.rx_bytes
+= skb
->len
;
191 dev
->stats
.rx_packets
++;
197 static netdev_tx_t
dlci_transmit(struct sk_buff
*skb
, struct net_device
*dev
)
199 struct dlci_local
*dlp
= netdev_priv(dev
);
202 dlp
->slave
->netdev_ops
->ndo_start_xmit(skb
, dlp
->slave
);
206 static int dlci_config(struct net_device
*dev
, struct dlci_conf __user
*conf
, int get
)
208 struct dlci_conf config
;
209 struct dlci_local
*dlp
;
210 struct frad_local
*flp
;
213 dlp
= netdev_priv(dev
);
215 flp
= netdev_priv(dlp
->slave
);
219 if (copy_from_user(&config
, conf
, sizeof(struct dlci_conf
)))
221 if (config
.flags
& ~DLCI_VALID_FLAGS
)
223 memcpy(&dlp
->config
, &config
, sizeof(struct dlci_conf
));
227 err
= (*flp
->dlci_conf
)(dlp
->slave
, dev
, get
);
233 if (copy_to_user(conf
, &dlp
->config
, sizeof(struct dlci_conf
)))
240 static int dlci_dev_ioctl(struct net_device
*dev
, struct ifreq
*ifr
, int cmd
)
242 struct dlci_local
*dlp
;
244 if (!capable(CAP_NET_ADMIN
))
247 dlp
= netdev_priv(dev
);
252 if (!*(short *)(dev
->dev_addr
))
255 strncpy(ifr
->ifr_slave
, dlp
->slave
->name
, sizeof(ifr
->ifr_slave
));
260 if (!*(short *)(dev
->dev_addr
))
263 return dlci_config(dev
, ifr
->ifr_data
, cmd
== DLCI_GET_CONF
);
272 static int dlci_change_mtu(struct net_device
*dev
, int new_mtu
)
274 struct dlci_local
*dlp
= netdev_priv(dev
);
276 return dev_set_mtu(dlp
->slave
, new_mtu
);
279 static int dlci_open(struct net_device
*dev
)
281 struct dlci_local
*dlp
;
282 struct frad_local
*flp
;
285 dlp
= netdev_priv(dev
);
287 if (!*(short *)(dev
->dev_addr
))
290 if (!netif_running(dlp
->slave
))
293 flp
= netdev_priv(dlp
->slave
);
294 err
= (*flp
->activate
)(dlp
->slave
, dev
);
298 netif_start_queue(dev
);
303 static int dlci_close(struct net_device
*dev
)
305 struct dlci_local
*dlp
;
306 struct frad_local
*flp
;
309 netif_stop_queue(dev
);
311 dlp
= netdev_priv(dev
);
313 flp
= netdev_priv(dlp
->slave
);
314 err
= (*flp
->deactivate
)(dlp
->slave
, dev
);
319 static int dlci_add(struct dlci_add
*dlci
)
321 struct net_device
*master
, *slave
;
322 struct dlci_local
*dlp
;
323 struct frad_local
*flp
;
327 /* validate slave device */
328 slave
= dev_get_by_name(&init_net
, dlci
->devname
);
332 if (slave
->type
!= ARPHRD_FRAD
|| netdev_priv(slave
) == NULL
)
335 /* create device name */
336 master
= alloc_netdev( sizeof(struct dlci_local
), "dlci%d",
343 /* make sure same slave not already registered */
345 list_for_each_entry(dlp
, &dlci_devs
, list
) {
346 if (dlp
->slave
== slave
) {
352 *(short *)(master
->dev_addr
) = dlci
->dlci
;
354 dlp
= netdev_priv(master
);
356 dlp
->master
= master
;
358 flp
= netdev_priv(slave
);
359 err
= (*flp
->assoc
)(slave
, master
);
363 err
= register_netdevice(master
);
367 strcpy(dlci
->devname
, master
->name
);
369 list_add(&dlp
->list
, &dlci_devs
);
382 static int dlci_del(struct dlci_add
*dlci
)
384 struct dlci_local
*dlp
;
385 struct frad_local
*flp
;
386 struct net_device
*master
, *slave
;
389 /* validate slave device */
390 master
= __dev_get_by_name(&init_net
, dlci
->devname
);
394 if (netif_running(master
)) {
398 dlp
= netdev_priv(master
);
400 flp
= netdev_priv(slave
);
403 err
= (*flp
->deassoc
)(slave
, master
);
405 list_del(&dlp
->list
);
407 unregister_netdevice(master
);
416 static int dlci_ioctl(unsigned int cmd
, void __user
*arg
)
421 if (!capable(CAP_NET_ADMIN
))
424 if (copy_from_user(&add
, arg
, sizeof(struct dlci_add
)))
430 err
= dlci_add(&add
);
433 if (copy_to_user(arg
, &add
, sizeof(struct dlci_add
)))
438 err
= dlci_del(&add
);
448 static const struct header_ops dlci_header_ops
= {
449 .create
= dlci_header
,
452 static const struct net_device_ops dlci_netdev_ops
= {
453 .ndo_open
= dlci_open
,
454 .ndo_stop
= dlci_close
,
455 .ndo_do_ioctl
= dlci_dev_ioctl
,
456 .ndo_start_xmit
= dlci_transmit
,
457 .ndo_change_mtu
= dlci_change_mtu
,
460 static void dlci_setup(struct net_device
*dev
)
462 struct dlci_local
*dlp
= netdev_priv(dev
);
465 dev
->header_ops
= &dlci_header_ops
;
466 dev
->netdev_ops
= &dlci_netdev_ops
;
467 dev
->destructor
= free_netdev
;
469 dlp
->receive
= dlci_receive
;
471 dev
->type
= ARPHRD_DLCI
;
472 dev
->hard_header_len
= sizeof(struct frhdr
);
473 dev
->addr_len
= sizeof(short);
477 /* if slave is unregistering, then cleanup master */
478 static int dlci_dev_event(struct notifier_block
*unused
,
479 unsigned long event
, void *ptr
)
481 struct net_device
*dev
= (struct net_device
*) ptr
;
483 if (dev_net(dev
) != &init_net
)
486 if (event
== NETDEV_UNREGISTER
) {
487 struct dlci_local
*dlp
;
489 list_for_each_entry(dlp
, &dlci_devs
, list
) {
490 if (dlp
->slave
== dev
) {
491 list_del(&dlp
->list
);
492 unregister_netdevice(dlp
->master
);
501 static struct notifier_block dlci_notifier
= {
502 .notifier_call
= dlci_dev_event
,
505 static int __init
init_dlci(void)
507 dlci_ioctl_set(dlci_ioctl
);
508 register_netdevice_notifier(&dlci_notifier
);
510 printk("%s.\n", version
);
515 static void __exit
dlci_exit(void)
517 struct dlci_local
*dlp
, *nxt
;
519 dlci_ioctl_set(NULL
);
520 unregister_netdevice_notifier(&dlci_notifier
);
523 list_for_each_entry_safe(dlp
, nxt
, &dlci_devs
, list
) {
524 unregister_netdevice(dlp
->master
);
530 module_init(init_dlci
);
531 module_exit(dlci_exit
);
533 MODULE_AUTHOR("Mike McLagan");
534 MODULE_DESCRIPTION("Frame Relay DLCI layer");
535 MODULE_LICENSE("GPL");