125763b3cSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds * Generic HDLC support routines for Linux
41da177e4SLinus Torvalds * HDLC support
51da177e4SLinus Torvalds *
6eb2a2fd9SKrzysztof Halasa * Copyright (C) 1999 - 2006 Krzysztof Halasa <khc@pm.waw.pl>
71da177e4SLinus Torvalds */
81da177e4SLinus Torvalds
91da177e4SLinus Torvalds #include <linux/errno.h>
101da177e4SLinus Torvalds #include <linux/hdlc.h>
114dfce407SKrzysztof Hałasa #include <linux/if_arp.h>
124dfce407SKrzysztof Hałasa #include <linux/inetdevice.h>
134dfce407SKrzysztof Hałasa #include <linux/init.h>
144dfce407SKrzysztof Hałasa #include <linux/kernel.h>
154dfce407SKrzysztof Hałasa #include <linux/module.h>
164dfce407SKrzysztof Hałasa #include <linux/pkt_sched.h>
174dfce407SKrzysztof Hałasa #include <linux/poll.h>
184dfce407SKrzysztof Hałasa #include <linux/rtnetlink.h>
194dfce407SKrzysztof Hałasa #include <linux/skbuff.h>
201da177e4SLinus Torvalds
211da177e4SLinus Torvalds
22*ad7eab2aSArnd Bergmann static int raw_ioctl(struct net_device *dev, struct if_settings *ifs);
23eb2a2fd9SKrzysztof Halasa
raw_type_trans(struct sk_buff * skb,struct net_device * dev)24ab611487SAlexey Dobriyan static __be16 raw_type_trans(struct sk_buff *skb, struct net_device *dev)
251da177e4SLinus Torvalds {
2609640e63SHarvey Harrison return cpu_to_be16(ETH_P_IP);
271da177e4SLinus Torvalds }
281da177e4SLinus Torvalds
29eb2a2fd9SKrzysztof Halasa static struct hdlc_proto proto = {
30eb2a2fd9SKrzysztof Halasa .type_trans = raw_type_trans,
31eb2a2fd9SKrzysztof Halasa .ioctl = raw_ioctl,
32eb2a2fd9SKrzysztof Halasa .module = THIS_MODULE,
33eb2a2fd9SKrzysztof Halasa };
34eb2a2fd9SKrzysztof Halasa
35eb2a2fd9SKrzysztof Halasa
raw_ioctl(struct net_device * dev,struct if_settings * ifs)36*ad7eab2aSArnd Bergmann static int raw_ioctl(struct net_device *dev, struct if_settings *ifs)
371da177e4SLinus Torvalds {
38*ad7eab2aSArnd Bergmann raw_hdlc_proto __user *raw_s = ifs->ifs_ifsu.raw_hdlc;
391da177e4SLinus Torvalds const size_t size = sizeof(raw_hdlc_proto);
401da177e4SLinus Torvalds raw_hdlc_proto new_settings;
411da177e4SLinus Torvalds hdlc_device *hdlc = dev_to_hdlc(dev);
421da177e4SLinus Torvalds int result;
431da177e4SLinus Torvalds
44*ad7eab2aSArnd Bergmann switch (ifs->type) {
451da177e4SLinus Torvalds case IF_GET_PROTO:
46eb2a2fd9SKrzysztof Halasa if (dev_to_hdlc(dev)->proto != &proto)
47eb2a2fd9SKrzysztof Halasa return -EINVAL;
48*ad7eab2aSArnd Bergmann ifs->type = IF_PROTO_HDLC;
49*ad7eab2aSArnd Bergmann if (ifs->size < size) {
50*ad7eab2aSArnd Bergmann ifs->size = size; /* data size wanted */
511da177e4SLinus Torvalds return -ENOBUFS;
521da177e4SLinus Torvalds }
53eb2a2fd9SKrzysztof Halasa if (copy_to_user(raw_s, hdlc->state, size))
541da177e4SLinus Torvalds return -EFAULT;
551da177e4SLinus Torvalds return 0;
561da177e4SLinus Torvalds
571da177e4SLinus Torvalds case IF_PROTO_HDLC:
581da177e4SLinus Torvalds if (!capable(CAP_NET_ADMIN))
591da177e4SLinus Torvalds return -EPERM;
601da177e4SLinus Torvalds
611da177e4SLinus Torvalds if (dev->flags & IFF_UP)
621da177e4SLinus Torvalds return -EBUSY;
631da177e4SLinus Torvalds
641da177e4SLinus Torvalds if (copy_from_user(&new_settings, raw_s, size))
651da177e4SLinus Torvalds return -EFAULT;
661da177e4SLinus Torvalds
671da177e4SLinus Torvalds if (new_settings.encoding == ENCODING_DEFAULT)
681da177e4SLinus Torvalds new_settings.encoding = ENCODING_NRZ;
691da177e4SLinus Torvalds
701da177e4SLinus Torvalds if (new_settings.parity == PARITY_DEFAULT)
711da177e4SLinus Torvalds new_settings.parity = PARITY_CRC16_PR1_CCITT;
721da177e4SLinus Torvalds
731da177e4SLinus Torvalds result = hdlc->attach(dev, new_settings.encoding,
741da177e4SLinus Torvalds new_settings.parity);
751da177e4SLinus Torvalds if (result)
761da177e4SLinus Torvalds return result;
771da177e4SLinus Torvalds
7840d25142SKrzysztof Halasa result = attach_hdlc_protocol(dev, &proto,
79eb2a2fd9SKrzysztof Halasa sizeof(raw_hdlc_proto));
80eb2a2fd9SKrzysztof Halasa if (result)
81eb2a2fd9SKrzysztof Halasa return result;
82eb2a2fd9SKrzysztof Halasa memcpy(hdlc->state, &new_settings, size);
831da177e4SLinus Torvalds dev->type = ARPHRD_RAWHDLC;
842f8364a2SAndrew Lunn call_netdevice_notifiers(NETDEV_POST_TYPE_CHANGE, dev);
854bc83b4dSKrzysztof Halasa netif_dormant_off(dev);
861da177e4SLinus Torvalds return 0;
871da177e4SLinus Torvalds }
881da177e4SLinus Torvalds
891da177e4SLinus Torvalds return -EINVAL;
901da177e4SLinus Torvalds }
91eb2a2fd9SKrzysztof Halasa
92eb2a2fd9SKrzysztof Halasa
hdlc_raw_init(void)93a1739c30SRandy Dunlap static int __init hdlc_raw_init(void)
94eb2a2fd9SKrzysztof Halasa {
95eb2a2fd9SKrzysztof Halasa register_hdlc_protocol(&proto);
96eb2a2fd9SKrzysztof Halasa return 0;
97eb2a2fd9SKrzysztof Halasa }
98eb2a2fd9SKrzysztof Halasa
99eb2a2fd9SKrzysztof Halasa
100eb2a2fd9SKrzysztof Halasa
hdlc_raw_exit(void)101a1739c30SRandy Dunlap static void __exit hdlc_raw_exit(void)
102eb2a2fd9SKrzysztof Halasa {
103eb2a2fd9SKrzysztof Halasa unregister_hdlc_protocol(&proto);
104eb2a2fd9SKrzysztof Halasa }
105eb2a2fd9SKrzysztof Halasa
106eb2a2fd9SKrzysztof Halasa
107a1739c30SRandy Dunlap module_init(hdlc_raw_init);
108a1739c30SRandy Dunlap module_exit(hdlc_raw_exit);
109eb2a2fd9SKrzysztof Halasa
110eb2a2fd9SKrzysztof Halasa MODULE_AUTHOR("Krzysztof Halasa <khc@pm.waw.pl>");
111eb2a2fd9SKrzysztof Halasa MODULE_DESCRIPTION("Raw HDLC protocol support for generic HDLC");
112eb2a2fd9SKrzysztof Halasa MODULE_LICENSE("GPL v2");
113