2019-05-28 17:10:09 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
2005-04-16 22:20:36 +00:00
|
|
|
/*
|
|
|
|
* Generic HDLC support routines for Linux
|
|
|
|
* HDLC support
|
|
|
|
*
|
2006-09-26 21:23:45 +00:00
|
|
|
* Copyright (C) 1999 - 2006 Krzysztof Halasa <khc@pm.waw.pl>
|
2005-04-16 22:20:36 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/errno.h>
|
2008-06-30 17:06:40 +00:00
|
|
|
#include <linux/hdlc.h>
|
2005-04-16 22:20:36 +00:00
|
|
|
#include <linux/if_arp.h>
|
2008-06-30 17:06:40 +00:00
|
|
|
#include <linux/inetdevice.h>
|
2005-04-16 22:20:36 +00:00
|
|
|
#include <linux/init.h>
|
2008-06-30 17:06:40 +00:00
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/module.h>
|
2005-04-16 22:20:36 +00:00
|
|
|
#include <linux/pkt_sched.h>
|
2008-06-30 17:06:40 +00:00
|
|
|
#include <linux/poll.h>
|
2005-04-16 22:20:36 +00:00
|
|
|
#include <linux/rtnetlink.h>
|
2008-06-30 17:06:40 +00:00
|
|
|
#include <linux/skbuff.h>
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
|
2021-07-27 13:45:14 +00:00
|
|
|
static int raw_ioctl(struct net_device *dev, struct if_settings *ifs);
|
2006-09-26 21:23:45 +00:00
|
|
|
|
2005-07-12 19:08:43 +00:00
|
|
|
static __be16 raw_type_trans(struct sk_buff *skb, struct net_device *dev)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2009-02-01 08:45:17 +00:00
|
|
|
return cpu_to_be16(ETH_P_IP);
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
2006-09-26 21:23:45 +00:00
|
|
|
static struct hdlc_proto proto = {
|
|
|
|
.type_trans = raw_type_trans,
|
|
|
|
.ioctl = raw_ioctl,
|
|
|
|
.module = THIS_MODULE,
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2021-07-27 13:45:14 +00:00
|
|
|
static int raw_ioctl(struct net_device *dev, struct if_settings *ifs)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2021-07-27 13:45:14 +00:00
|
|
|
raw_hdlc_proto __user *raw_s = ifs->ifs_ifsu.raw_hdlc;
|
2005-04-16 22:20:36 +00:00
|
|
|
const size_t size = sizeof(raw_hdlc_proto);
|
|
|
|
raw_hdlc_proto new_settings;
|
|
|
|
hdlc_device *hdlc = dev_to_hdlc(dev);
|
|
|
|
int result;
|
|
|
|
|
2021-07-27 13:45:14 +00:00
|
|
|
switch (ifs->type) {
|
2005-04-16 22:20:36 +00:00
|
|
|
case IF_GET_PROTO:
|
2006-09-26 21:23:45 +00:00
|
|
|
if (dev_to_hdlc(dev)->proto != &proto)
|
|
|
|
return -EINVAL;
|
2021-07-27 13:45:14 +00:00
|
|
|
ifs->type = IF_PROTO_HDLC;
|
|
|
|
if (ifs->size < size) {
|
|
|
|
ifs->size = size; /* data size wanted */
|
2005-04-16 22:20:36 +00:00
|
|
|
return -ENOBUFS;
|
|
|
|
}
|
2006-09-26 21:23:45 +00:00
|
|
|
if (copy_to_user(raw_s, hdlc->state, size))
|
2005-04-16 22:20:36 +00:00
|
|
|
return -EFAULT;
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
case IF_PROTO_HDLC:
|
|
|
|
if (!capable(CAP_NET_ADMIN))
|
|
|
|
return -EPERM;
|
|
|
|
|
|
|
|
if (dev->flags & IFF_UP)
|
|
|
|
return -EBUSY;
|
|
|
|
|
|
|
|
if (copy_from_user(&new_settings, raw_s, size))
|
|
|
|
return -EFAULT;
|
|
|
|
|
|
|
|
if (new_settings.encoding == ENCODING_DEFAULT)
|
|
|
|
new_settings.encoding = ENCODING_NRZ;
|
|
|
|
|
|
|
|
if (new_settings.parity == PARITY_DEFAULT)
|
|
|
|
new_settings.parity = PARITY_CRC16_PR1_CCITT;
|
|
|
|
|
|
|
|
result = hdlc->attach(dev, new_settings.encoding,
|
|
|
|
new_settings.parity);
|
|
|
|
if (result)
|
|
|
|
return result;
|
|
|
|
|
2008-02-01 21:37:12 +00:00
|
|
|
result = attach_hdlc_protocol(dev, &proto,
|
2006-09-26 21:23:45 +00:00
|
|
|
sizeof(raw_hdlc_proto));
|
|
|
|
if (result)
|
|
|
|
return result;
|
|
|
|
memcpy(hdlc->state, &new_settings, size);
|
2005-04-16 22:20:36 +00:00
|
|
|
dev->type = ARPHRD_RAWHDLC;
|
2015-12-03 20:12:31 +00:00
|
|
|
call_netdevice_notifiers(NETDEV_POST_TYPE_CHANGE, dev);
|
2006-07-21 21:41:01 +00:00
|
|
|
netif_dormant_off(dev);
|
2005-04-16 22:20:36 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
2006-09-26 21:23:45 +00:00
|
|
|
|
|
|
|
|
2021-07-11 22:31:47 +00:00
|
|
|
static int __init hdlc_raw_init(void)
|
2006-09-26 21:23:45 +00:00
|
|
|
{
|
|
|
|
register_hdlc_protocol(&proto);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-07-11 22:31:47 +00:00
|
|
|
static void __exit hdlc_raw_exit(void)
|
2006-09-26 21:23:45 +00:00
|
|
|
{
|
|
|
|
unregister_hdlc_protocol(&proto);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-07-11 22:31:47 +00:00
|
|
|
module_init(hdlc_raw_init);
|
|
|
|
module_exit(hdlc_raw_exit);
|
2006-09-26 21:23:45 +00:00
|
|
|
|
|
|
|
MODULE_AUTHOR("Krzysztof Halasa <khc@pm.waw.pl>");
|
|
|
|
MODULE_DESCRIPTION("Raw HDLC protocol support for generic HDLC");
|
|
|
|
MODULE_LICENSE("GPL v2");
|