125763b3cSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds * Generic HDLC support routines for Linux
41da177e4SLinus Torvalds * HDLC Ethernet emulation support
51da177e4SLinus Torvalds *
6eb2a2fd9SKrzysztof Halasa * Copyright (C) 2002-2006 Krzysztof Halasa <khc@pm.waw.pl>
71da177e4SLinus Torvalds */
81da177e4SLinus Torvalds
91da177e4SLinus Torvalds #include <linux/errno.h>
101da177e4SLinus Torvalds #include <linux/etherdevice.h>
115a0e3ad6STejun Heo #include <linux/gfp.h>
121da177e4SLinus Torvalds #include <linux/hdlc.h>
134dfce407SKrzysztof Hałasa #include <linux/if_arp.h>
144dfce407SKrzysztof Hałasa #include <linux/inetdevice.h>
154dfce407SKrzysztof Hałasa #include <linux/init.h>
164dfce407SKrzysztof Hałasa #include <linux/kernel.h>
174dfce407SKrzysztof Hałasa #include <linux/module.h>
184dfce407SKrzysztof Hałasa #include <linux/pkt_sched.h>
194dfce407SKrzysztof Hałasa #include <linux/poll.h>
204dfce407SKrzysztof Hałasa #include <linux/rtnetlink.h>
214dfce407SKrzysztof Hałasa #include <linux/skbuff.h>
221da177e4SLinus Torvalds
23*ad7eab2aSArnd Bergmann static int raw_eth_ioctl(struct net_device *dev, struct if_settings *ifs);
241da177e4SLinus Torvalds
eth_tx(struct sk_buff * skb,struct net_device * dev)25d71a6749SStephen Hemminger static netdev_tx_t eth_tx(struct sk_buff *skb, struct net_device *dev)
261da177e4SLinus Torvalds {
271da177e4SLinus Torvalds int pad = ETH_ZLEN - skb->len;
281da177e4SLinus Torvalds if (pad > 0) { /* Pad the frame with zeros */
291da177e4SLinus Torvalds int len = skb->len;
301da177e4SLinus Torvalds if (skb_tailroom(skb) < pad)
311da177e4SLinus Torvalds if (pskb_expand_head(skb, 0, pad, GFP_ATOMIC)) {
32198191c4SKrzysztof Halasa dev->stats.tx_dropped++;
331da177e4SLinus Torvalds dev_kfree_skb(skb);
341da177e4SLinus Torvalds return 0;
351da177e4SLinus Torvalds }
361da177e4SLinus Torvalds skb_put(skb, pad);
371da177e4SLinus Torvalds memset(skb->data + len, 0, pad);
381da177e4SLinus Torvalds }
391da177e4SLinus Torvalds return dev_to_hdlc(dev)->xmit(skb, dev);
401da177e4SLinus Torvalds }
411da177e4SLinus Torvalds
421da177e4SLinus Torvalds
43eb2a2fd9SKrzysztof Halasa static struct hdlc_proto proto = {
44eb2a2fd9SKrzysztof Halasa .type_trans = eth_type_trans,
45991990a1SKrzysztof Hałasa .xmit = eth_tx,
46eb2a2fd9SKrzysztof Halasa .ioctl = raw_eth_ioctl,
47eb2a2fd9SKrzysztof Halasa .module = THIS_MODULE,
48eb2a2fd9SKrzysztof Halasa };
49eb2a2fd9SKrzysztof Halasa
50eb2a2fd9SKrzysztof Halasa
raw_eth_ioctl(struct net_device * dev,struct if_settings * ifs)51*ad7eab2aSArnd Bergmann static int raw_eth_ioctl(struct net_device *dev, struct if_settings *ifs)
521da177e4SLinus Torvalds {
53*ad7eab2aSArnd Bergmann raw_hdlc_proto __user *raw_s = ifs->ifs_ifsu.raw_hdlc;
541da177e4SLinus Torvalds const size_t size = sizeof(raw_hdlc_proto);
551da177e4SLinus Torvalds raw_hdlc_proto new_settings;
561da177e4SLinus Torvalds hdlc_device *hdlc = dev_to_hdlc(dev);
570cd29503SAlexey Dobriyan unsigned int old_qlen;
580cd29503SAlexey Dobriyan int result;
591da177e4SLinus Torvalds
60*ad7eab2aSArnd Bergmann switch (ifs->type) {
611da177e4SLinus Torvalds case IF_GET_PROTO:
62eb2a2fd9SKrzysztof Halasa if (dev_to_hdlc(dev)->proto != &proto)
63eb2a2fd9SKrzysztof Halasa return -EINVAL;
64*ad7eab2aSArnd Bergmann ifs->type = IF_PROTO_HDLC_ETH;
65*ad7eab2aSArnd Bergmann if (ifs->size < size) {
66*ad7eab2aSArnd Bergmann ifs->size = size; /* data size wanted */
671da177e4SLinus Torvalds return -ENOBUFS;
681da177e4SLinus Torvalds }
69eb2a2fd9SKrzysztof Halasa if (copy_to_user(raw_s, hdlc->state, size))
701da177e4SLinus Torvalds return -EFAULT;
711da177e4SLinus Torvalds return 0;
721da177e4SLinus Torvalds
731da177e4SLinus Torvalds case IF_PROTO_HDLC_ETH:
741da177e4SLinus Torvalds if (!capable(CAP_NET_ADMIN))
751da177e4SLinus Torvalds return -EPERM;
761da177e4SLinus Torvalds
771da177e4SLinus Torvalds if (dev->flags & IFF_UP)
781da177e4SLinus Torvalds return -EBUSY;
791da177e4SLinus Torvalds
801da177e4SLinus Torvalds if (copy_from_user(&new_settings, raw_s, size))
811da177e4SLinus Torvalds return -EFAULT;
821da177e4SLinus Torvalds
831da177e4SLinus Torvalds if (new_settings.encoding == ENCODING_DEFAULT)
841da177e4SLinus Torvalds new_settings.encoding = ENCODING_NRZ;
851da177e4SLinus Torvalds
861da177e4SLinus Torvalds if (new_settings.parity == PARITY_DEFAULT)
871da177e4SLinus Torvalds new_settings.parity = PARITY_CRC16_PR1_CCITT;
881da177e4SLinus Torvalds
891da177e4SLinus Torvalds result = hdlc->attach(dev, new_settings.encoding,
901da177e4SLinus Torvalds new_settings.parity);
911da177e4SLinus Torvalds if (result)
921da177e4SLinus Torvalds return result;
931da177e4SLinus Torvalds
9440d25142SKrzysztof Halasa result = attach_hdlc_protocol(dev, &proto,
95eb2a2fd9SKrzysztof Halasa sizeof(raw_hdlc_proto));
96eb2a2fd9SKrzysztof Halasa if (result)
97eb2a2fd9SKrzysztof Halasa return result;
98eb2a2fd9SKrzysztof Halasa memcpy(hdlc->state, &new_settings, size);
991da177e4SLinus Torvalds old_qlen = dev->tx_queue_len;
1001da177e4SLinus Torvalds ether_setup(dev);
1011da177e4SLinus Torvalds dev->tx_queue_len = old_qlen;
1025fce1e43SXie He dev->priv_flags &= ~IFF_TX_SKB_SHARING;
103f2cedb63SDanny Kukawka eth_hw_addr_random(dev);
1042f8364a2SAndrew Lunn call_netdevice_notifiers(NETDEV_POST_TYPE_CHANGE, dev);
1054bc83b4dSKrzysztof Halasa netif_dormant_off(dev);
1061da177e4SLinus Torvalds return 0;
1071da177e4SLinus Torvalds }
1081da177e4SLinus Torvalds
1091da177e4SLinus Torvalds return -EINVAL;
1101da177e4SLinus Torvalds }
111eb2a2fd9SKrzysztof Halasa
112eb2a2fd9SKrzysztof Halasa
hdlc_eth_init(void)113a1739c30SRandy Dunlap static int __init hdlc_eth_init(void)
114eb2a2fd9SKrzysztof Halasa {
115eb2a2fd9SKrzysztof Halasa register_hdlc_protocol(&proto);
116eb2a2fd9SKrzysztof Halasa return 0;
117eb2a2fd9SKrzysztof Halasa }
118eb2a2fd9SKrzysztof Halasa
119eb2a2fd9SKrzysztof Halasa
120eb2a2fd9SKrzysztof Halasa
hdlc_eth_exit(void)121a1739c30SRandy Dunlap static void __exit hdlc_eth_exit(void)
122eb2a2fd9SKrzysztof Halasa {
123eb2a2fd9SKrzysztof Halasa unregister_hdlc_protocol(&proto);
124eb2a2fd9SKrzysztof Halasa }
125eb2a2fd9SKrzysztof Halasa
126eb2a2fd9SKrzysztof Halasa
127a1739c30SRandy Dunlap module_init(hdlc_eth_init);
128a1739c30SRandy Dunlap module_exit(hdlc_eth_exit);
129eb2a2fd9SKrzysztof Halasa
130eb2a2fd9SKrzysztof Halasa MODULE_AUTHOR("Krzysztof Halasa <khc@pm.waw.pl>");
131eb2a2fd9SKrzysztof Halasa MODULE_DESCRIPTION("Ethernet encapsulation support for generic HDLC");
132eb2a2fd9SKrzysztof Halasa MODULE_LICENSE("GPL v2");
133