1 // SPDX-License-Identifier: GPL-2.0-only
3 * Generic HDLC support routines for Linux
4 * HDLC Ethernet emulation support
9 #include <linux/errno.h>
10 #include <linux/etherdevice.h>
11 #include <linux/gfp.h>
12 #include <linux/hdlc.h>
13 #include <linux/if_arp.h>
14 #include <linux/inetdevice.h>
15 #include <linux/init.h>
16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/pkt_sched.h>
19 #include <linux/poll.h>
20 #include <linux/rtnetlink.h>
21 #include <linux/skbuff.h>
23 static int raw_eth_ioctl(struct net_device *dev, struct ifreq *ifr);
25 static netdev_tx_t eth_tx(struct sk_buff *skb, struct net_device *dev)
27 int pad = ETH_ZLEN - skb->len;
28 if (pad > 0) { /* Pad the frame with zeros */
30 if (skb_tailroom(skb) < pad)
31 if (pskb_expand_head(skb, 0, pad, GFP_ATOMIC)) {
32 dev->stats.tx_dropped++;
37 memset(skb->data + len, 0, pad);
39 return dev_to_hdlc(dev)->xmit(skb, dev);
43 static struct hdlc_proto proto = {
44 .type_trans = eth_type_trans,
46 .ioctl = raw_eth_ioctl,
47 .module = THIS_MODULE,
51 static int raw_eth_ioctl(struct net_device *dev, struct ifreq *ifr)
53 raw_hdlc_proto __user *raw_s = ifr->ifr_settings.ifs_ifsu.raw_hdlc;
54 const size_t size = sizeof(raw_hdlc_proto);
55 raw_hdlc_proto new_settings;
56 hdlc_device *hdlc = dev_to_hdlc(dev);
57 unsigned int old_qlen;
60 switch (ifr->ifr_settings.type) {
62 if (dev_to_hdlc(dev)->proto != &proto)
64 ifr->ifr_settings.type = IF_PROTO_HDLC_ETH;
65 if (ifr->ifr_settings.size < size) {
66 ifr->ifr_settings.size = size; /* data size wanted */
69 if (copy_to_user(raw_s, hdlc->state, size))
73 case IF_PROTO_HDLC_ETH:
74 if (!capable(CAP_NET_ADMIN))
77 if (dev->flags & IFF_UP)
80 if (copy_from_user(&new_settings, raw_s, size))
83 if (new_settings.encoding == ENCODING_DEFAULT)
84 new_settings.encoding = ENCODING_NRZ;
86 if (new_settings.parity == PARITY_DEFAULT)
87 new_settings.parity = PARITY_CRC16_PR1_CCITT;
89 result = hdlc->attach(dev, new_settings.encoding,
94 result = attach_hdlc_protocol(dev, &proto,
95 sizeof(raw_hdlc_proto));
98 memcpy(hdlc->state, &new_settings, size);
99 old_qlen = dev->tx_queue_len;
101 dev->tx_queue_len = old_qlen;
102 dev->priv_flags &= ~IFF_TX_SKB_SHARING;
103 eth_hw_addr_random(dev);
104 call_netdevice_notifiers(NETDEV_POST_TYPE_CHANGE, dev);
105 netif_dormant_off(dev);
113 static int __init mod_init(void)
115 register_hdlc_protocol(&proto);
121 static void __exit mod_exit(void)
123 unregister_hdlc_protocol(&proto);
127 module_init(mod_init);
128 module_exit(mod_exit);
131 MODULE_DESCRIPTION("Ethernet encapsulation support for generic HDLC");
132 MODULE_LICENSE("GPL v2");