^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Generic HDLC support routines for Linux
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * HDLC support
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Copyright (C) 1999 - 2006 Krzysztof Halasa <khc@pm.waw.pl>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/hdlc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/if_arp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/inetdevice.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/pkt_sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/poll.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/rtnetlink.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/skbuff.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) static int raw_ioctl(struct net_device *dev, struct ifreq *ifr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) static __be16 raw_type_trans(struct sk_buff *skb, struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) return cpu_to_be16(ETH_P_IP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) static struct hdlc_proto proto = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) .type_trans = raw_type_trans,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) .ioctl = raw_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) .module = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) static int raw_ioctl(struct net_device *dev, struct ifreq *ifr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) raw_hdlc_proto __user *raw_s = ifr->ifr_settings.ifs_ifsu.raw_hdlc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) const size_t size = sizeof(raw_hdlc_proto);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) raw_hdlc_proto new_settings;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) hdlc_device *hdlc = dev_to_hdlc(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) int result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) switch (ifr->ifr_settings.type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) case IF_GET_PROTO:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) if (dev_to_hdlc(dev)->proto != &proto)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) ifr->ifr_settings.type = IF_PROTO_HDLC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) if (ifr->ifr_settings.size < size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) ifr->ifr_settings.size = size; /* data size wanted */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) return -ENOBUFS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) if (copy_to_user(raw_s, hdlc->state, size))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) case IF_PROTO_HDLC:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) if (!capable(CAP_NET_ADMIN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) if (dev->flags & IFF_UP)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) if (copy_from_user(&new_settings, raw_s, size))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) if (new_settings.encoding == ENCODING_DEFAULT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) new_settings.encoding = ENCODING_NRZ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) if (new_settings.parity == PARITY_DEFAULT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) new_settings.parity = PARITY_CRC16_PR1_CCITT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) result = hdlc->attach(dev, new_settings.encoding,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) new_settings.parity);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) if (result)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) return result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) result = attach_hdlc_protocol(dev, &proto,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) sizeof(raw_hdlc_proto));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) if (result)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) return result;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) memcpy(hdlc->state, &new_settings, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) dev->type = ARPHRD_RAWHDLC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) call_netdevice_notifiers(NETDEV_POST_TYPE_CHANGE, dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) netif_dormant_off(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) static int __init mod_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) register_hdlc_protocol(&proto);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) static void __exit mod_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) unregister_hdlc_protocol(&proto);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) module_init(mod_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) module_exit(mod_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) MODULE_AUTHOR("Krzysztof Halasa <khc@pm.waw.pl>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) MODULE_DESCRIPTION("Raw HDLC protocol support for generic HDLC");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) MODULE_LICENSE("GPL v2");