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