1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * DLCI Implementation of Frame Relay protocol for Linux, according to
4 * RFC 1490. This generic device provides en/decapsulation for an
5 * underlying hardware driver. Routes & IPs are assigned to these
6 * interfaces. Requires 'dlcicfg' program to create usable
7 * interfaces, the initial one, 'dlci' is for IOCTL use only.
9 * Version: @(#)dlci.c 0.35 4 Jan 1997
11 * Author: Mike McLagan <mike.mclagan@linux.org>
15 * 0.15 Mike Mclagan Packet freeing, bug in kmalloc call
17 * 0.20 Mike McLagan More conservative on which packets
18 * are returned for retry and which are
19 * are dropped. If DLCI_RET_DROP is
20 * returned from the FRAD, the packet is
21 * sent back to Linux for re-transmission
22 * 0.25 Mike McLagan Converted to use SIOC IOCTL calls
23 * 0.30 Jim Freeman Fixed to allow IPX traffic
24 * 0.35 Michael Elizabeth Fixed incorrect memcpy_fromfs
27 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
29 #include <linux/module.h>
30 #include <linux/kernel.h>
31 #include <linux/types.h>
32 #include <linux/fcntl.h>
33 #include <linux/interrupt.h>
34 #include <linux/ptrace.h>
35 #include <linux/ioport.h>
37 #include <linux/init.h>
38 #include <linux/slab.h>
39 #include <linux/string.h>
40 #include <linux/errno.h>
41 #include <linux/netdevice.h>
42 #include <linux/skbuff.h>
43 #include <linux/if_arp.h>
44 #include <linux/if_frad.h>
45 #include <linux/bitops.h>
51 #include <linux/uaccess.h>
53 static const char version
[] = "DLCI driver v0.35, 4 Jan 1997, mike.mclagan@linux.org";
55 static LIST_HEAD(dlci_devs
);
57 static void dlci_setup(struct net_device
*);
60 * these encapsulate the RFC 1490 requirements as well as
61 * deal with packet transmission and reception, working with
62 * the upper network layers
65 static int dlci_header(struct sk_buff
*skb
, struct net_device
*dev
,
66 unsigned short type
, const void *daddr
,
67 const void *saddr
, unsigned len
)
73 hdr
.control
= FRAD_I_UI
;
77 hdr
.IP_NLPID
= FRAD_P_IP
;
78 hlen
= sizeof(hdr
.control
) + sizeof(hdr
.IP_NLPID
);
81 /* feel free to add other types, if necessary */
84 hdr
.pad
= FRAD_P_PADDING
;
85 hdr
.NLPID
= FRAD_P_SNAP
;
86 memset(hdr
.OUI
, 0, sizeof(hdr
.OUI
));
87 hdr
.PID
= htons(type
);
92 dest
= skb_push(skb
, hlen
);
96 memcpy(dest
, &hdr
, hlen
);
101 static void dlci_receive(struct sk_buff
*skb
, struct net_device
*dev
)
106 if (!pskb_may_pull(skb
, sizeof(*hdr
))) {
107 netdev_notice(dev
, "invalid data no header\n");
108 dev
->stats
.rx_errors
++;
113 hdr
= (struct frhdr
*) skb
->data
;
118 if (hdr
->control
!= FRAD_I_UI
)
120 netdev_notice(dev
, "Invalid header flag 0x%02X\n",
122 dev
->stats
.rx_errors
++;
125 switch (hdr
->IP_NLPID
)
128 if (hdr
->NLPID
!= FRAD_P_SNAP
)
130 netdev_notice(dev
, "Unsupported NLPID 0x%02X\n",
132 dev
->stats
.rx_errors
++;
136 if (hdr
->OUI
[0] + hdr
->OUI
[1] + hdr
->OUI
[2] != 0)
138 netdev_notice(dev
, "Unsupported organizationally unique identifier 0x%02X-%02X-%02X\n",
142 dev
->stats
.rx_errors
++;
146 /* at this point, it's an EtherType frame */
147 header
= sizeof(struct frhdr
);
148 /* Already in network order ! */
149 skb
->protocol
= hdr
->PID
;
154 header
= sizeof(hdr
->control
) + sizeof(hdr
->IP_NLPID
);
155 skb
->protocol
= htons(ETH_P_IP
);
162 netdev_notice(dev
, "Unsupported NLPID 0x%02X\n",
164 dev
->stats
.rx_errors
++;
168 netdev_notice(dev
, "Invalid pad byte 0x%02X\n",
170 dev
->stats
.rx_errors
++;
176 /* we've set up the protocol, so discard the header */
177 skb_reset_mac_header(skb
);
178 skb_pull(skb
, header
);
179 dev
->stats
.rx_bytes
+= skb
->len
;
181 dev
->stats
.rx_packets
++;
187 static netdev_tx_t
dlci_transmit(struct sk_buff
*skb
, struct net_device
*dev
)
189 struct dlci_local
*dlp
= netdev_priv(dev
);
192 struct netdev_queue
*txq
= skb_get_tx_queue(dev
, skb
);
193 netdev_start_xmit(skb
, dlp
->slave
, txq
, false);
198 static int dlci_config(struct net_device
*dev
, struct dlci_conf __user
*conf
, int get
)
200 struct dlci_conf config
;
201 struct dlci_local
*dlp
;
202 struct frad_local
*flp
;
205 dlp
= netdev_priv(dev
);
207 flp
= netdev_priv(dlp
->slave
);
211 if (copy_from_user(&config
, conf
, sizeof(struct dlci_conf
)))
213 if (config
.flags
& ~DLCI_VALID_FLAGS
)
215 memcpy(&dlp
->config
, &config
, sizeof(struct dlci_conf
));
219 err
= (*flp
->dlci_conf
)(dlp
->slave
, dev
, get
);
225 if (copy_to_user(conf
, &dlp
->config
, sizeof(struct dlci_conf
)))
232 static int dlci_dev_ioctl(struct net_device
*dev
, struct ifreq
*ifr
, int cmd
)
234 struct dlci_local
*dlp
;
236 if (!capable(CAP_NET_ADMIN
))
239 dlp
= netdev_priv(dev
);
244 if (!*(short *)(dev
->dev_addr
))
247 strncpy(ifr
->ifr_slave
, dlp
->slave
->name
, sizeof(ifr
->ifr_slave
));
252 if (!*(short *)(dev
->dev_addr
))
255 return dlci_config(dev
, ifr
->ifr_data
, cmd
== DLCI_GET_CONF
);
263 static int dlci_change_mtu(struct net_device
*dev
, int new_mtu
)
265 struct dlci_local
*dlp
= netdev_priv(dev
);
267 return dev_set_mtu(dlp
->slave
, new_mtu
);
270 static int dlci_open(struct net_device
*dev
)
272 struct dlci_local
*dlp
;
273 struct frad_local
*flp
;
276 dlp
= netdev_priv(dev
);
278 if (!*(short *)(dev
->dev_addr
))
281 if (!netif_running(dlp
->slave
))
284 flp
= netdev_priv(dlp
->slave
);
285 err
= (*flp
->activate
)(dlp
->slave
, dev
);
289 netif_start_queue(dev
);
294 static int dlci_close(struct net_device
*dev
)
296 struct dlci_local
*dlp
;
297 struct frad_local
*flp
;
300 netif_stop_queue(dev
);
302 dlp
= netdev_priv(dev
);
304 flp
= netdev_priv(dlp
->slave
);
305 err
= (*flp
->deactivate
)(dlp
->slave
, dev
);
310 static int dlci_add(struct dlci_add
*dlci
)
312 struct net_device
*master
, *slave
;
313 struct dlci_local
*dlp
;
314 struct frad_local
*flp
;
318 /* validate slave device */
319 slave
= dev_get_by_name(&init_net
, dlci
->devname
);
323 if (slave
->type
!= ARPHRD_FRAD
|| netdev_priv(slave
) == NULL
)
326 /* create device name */
327 master
= alloc_netdev(sizeof(struct dlci_local
), "dlci%d",
328 NET_NAME_UNKNOWN
, dlci_setup
);
334 /* make sure same slave not already registered */
336 list_for_each_entry(dlp
, &dlci_devs
, list
) {
337 if (dlp
->slave
== slave
) {
343 *(short *)(master
->dev_addr
) = dlci
->dlci
;
345 dlp
= netdev_priv(master
);
347 dlp
->master
= master
;
349 flp
= netdev_priv(slave
);
350 err
= (*flp
->assoc
)(slave
, master
);
354 err
= register_netdevice(master
);
358 strcpy(dlci
->devname
, master
->name
);
360 list_add(&dlp
->list
, &dlci_devs
);
373 static int dlci_del(struct dlci_add
*dlci
)
375 struct dlci_local
*dlp
;
376 struct frad_local
*flp
;
377 struct net_device
*master
, *slave
;
383 /* validate slave device */
384 master
= __dev_get_by_name(&init_net
, dlci
->devname
);
390 list_for_each_entry(dlp
, &dlci_devs
, list
) {
391 if (dlp
->master
== master
) {
401 if (netif_running(master
)) {
406 dlp
= netdev_priv(master
);
408 flp
= netdev_priv(slave
);
410 err
= (*flp
->deassoc
)(slave
, master
);
412 list_del(&dlp
->list
);
414 unregister_netdevice(master
);
423 static int dlci_ioctl(unsigned int cmd
, void __user
*arg
)
428 if (!capable(CAP_NET_ADMIN
))
431 if (copy_from_user(&add
, arg
, sizeof(struct dlci_add
)))
437 err
= dlci_add(&add
);
440 if (copy_to_user(arg
, &add
, sizeof(struct dlci_add
)))
445 err
= dlci_del(&add
);
455 static const struct header_ops dlci_header_ops
= {
456 .create
= dlci_header
,
459 static const struct net_device_ops dlci_netdev_ops
= {
460 .ndo_open
= dlci_open
,
461 .ndo_stop
= dlci_close
,
462 .ndo_do_ioctl
= dlci_dev_ioctl
,
463 .ndo_start_xmit
= dlci_transmit
,
464 .ndo_change_mtu
= dlci_change_mtu
,
467 static void dlci_setup(struct net_device
*dev
)
469 struct dlci_local
*dlp
= netdev_priv(dev
);
472 dev
->header_ops
= &dlci_header_ops
;
473 dev
->netdev_ops
= &dlci_netdev_ops
;
474 dev
->needs_free_netdev
= true;
476 dlp
->receive
= dlci_receive
;
478 dev
->type
= ARPHRD_DLCI
;
479 dev
->hard_header_len
= sizeof(struct frhdr
);
480 dev
->addr_len
= sizeof(short);
484 /* if slave is unregistering, then cleanup master */
485 static int dlci_dev_event(struct notifier_block
*unused
,
486 unsigned long event
, void *ptr
)
488 struct net_device
*dev
= netdev_notifier_info_to_dev(ptr
);
490 if (dev_net(dev
) != &init_net
)
493 if (event
== NETDEV_UNREGISTER
) {
494 struct dlci_local
*dlp
;
496 list_for_each_entry(dlp
, &dlci_devs
, list
) {
497 if (dlp
->slave
== dev
) {
498 list_del(&dlp
->list
);
499 unregister_netdevice(dlp
->master
);
508 static struct notifier_block dlci_notifier
= {
509 .notifier_call
= dlci_dev_event
,
512 static int __init
init_dlci(void)
514 dlci_ioctl_set(dlci_ioctl
);
515 register_netdevice_notifier(&dlci_notifier
);
517 printk("%s.\n", version
);
522 static void __exit
dlci_exit(void)
524 struct dlci_local
*dlp
, *nxt
;
526 dlci_ioctl_set(NULL
);
527 unregister_netdevice_notifier(&dlci_notifier
);
530 list_for_each_entry_safe(dlp
, nxt
, &dlci_devs
, list
) {
531 unregister_netdevice(dlp
->master
);
537 module_init(init_dlci
);
538 module_exit(dlci_exit
);
540 MODULE_AUTHOR("Mike McLagan");
541 MODULE_DESCRIPTION("Frame Relay DLCI layer");
542 MODULE_LICENSE("GPL");