xref: /openbmc/linux/drivers/net/wan/hdlc_raw_eth.c (revision 93dc544c)
1 /*
2  * Generic HDLC support routines for Linux
3  * HDLC Ethernet emulation support
4  *
5  * Copyright (C) 2002-2006 Krzysztof Halasa <khc@pm.waw.pl>
6  *
7  * This program is free software; you can redistribute it and/or modify it
8  * under the terms of version 2 of the GNU General Public License
9  * as published by the Free Software Foundation.
10  */
11 
12 #include <linux/module.h>
13 #include <linux/kernel.h>
14 #include <linux/slab.h>
15 #include <linux/poll.h>
16 #include <linux/errno.h>
17 #include <linux/if_arp.h>
18 #include <linux/init.h>
19 #include <linux/skbuff.h>
20 #include <linux/pkt_sched.h>
21 #include <linux/inetdevice.h>
22 #include <linux/lapb.h>
23 #include <linux/rtnetlink.h>
24 #include <linux/etherdevice.h>
25 #include <linux/hdlc.h>
26 
27 static int raw_eth_ioctl(struct net_device *dev, struct ifreq *ifr);
28 
29 static int eth_tx(struct sk_buff *skb, struct net_device *dev)
30 {
31 	int pad = ETH_ZLEN - skb->len;
32 	if (pad > 0) {		/* Pad the frame with zeros */
33 		int len = skb->len;
34 		if (skb_tailroom(skb) < pad)
35 			if (pskb_expand_head(skb, 0, pad, GFP_ATOMIC)) {
36 				dev->stats.tx_dropped++;
37 				dev_kfree_skb(skb);
38 				return 0;
39 			}
40 		skb_put(skb, pad);
41 		memset(skb->data + len, 0, pad);
42 	}
43 	return dev_to_hdlc(dev)->xmit(skb, dev);
44 }
45 
46 
47 static struct hdlc_proto proto = {
48 	.type_trans	= eth_type_trans,
49 	.ioctl		= raw_eth_ioctl,
50 	.module		= THIS_MODULE,
51 };
52 
53 
54 static int raw_eth_ioctl(struct net_device *dev, struct ifreq *ifr)
55 {
56 	raw_hdlc_proto __user *raw_s = ifr->ifr_settings.ifs_ifsu.raw_hdlc;
57 	const size_t size = sizeof(raw_hdlc_proto);
58 	raw_hdlc_proto new_settings;
59 	hdlc_device *hdlc = dev_to_hdlc(dev);
60 	int result;
61 	int (*old_ch_mtu)(struct net_device *, int);
62 	int old_qlen;
63 
64 	switch (ifr->ifr_settings.type) {
65 	case IF_GET_PROTO:
66 		if (dev_to_hdlc(dev)->proto != &proto)
67 			return -EINVAL;
68 		ifr->ifr_settings.type = IF_PROTO_HDLC_ETH;
69 		if (ifr->ifr_settings.size < size) {
70 			ifr->ifr_settings.size = size; /* data size wanted */
71 			return -ENOBUFS;
72 		}
73 		if (copy_to_user(raw_s, hdlc->state, size))
74 			return -EFAULT;
75 		return 0;
76 
77 	case IF_PROTO_HDLC_ETH:
78 		if (!capable(CAP_NET_ADMIN))
79 			return -EPERM;
80 
81 		if (dev->flags & IFF_UP)
82 			return -EBUSY;
83 
84 		if (copy_from_user(&new_settings, raw_s, size))
85 			return -EFAULT;
86 
87 		if (new_settings.encoding == ENCODING_DEFAULT)
88 			new_settings.encoding = ENCODING_NRZ;
89 
90 		if (new_settings.parity == PARITY_DEFAULT)
91 			new_settings.parity = PARITY_CRC16_PR1_CCITT;
92 
93 		result = hdlc->attach(dev, new_settings.encoding,
94 				      new_settings.parity);
95 		if (result)
96 			return result;
97 
98 		result = attach_hdlc_protocol(dev, &proto,
99 					      sizeof(raw_hdlc_proto));
100 		if (result)
101 			return result;
102 		memcpy(hdlc->state, &new_settings, size);
103 		dev->hard_start_xmit = eth_tx;
104 		old_ch_mtu = dev->change_mtu;
105 		old_qlen = dev->tx_queue_len;
106 		ether_setup(dev);
107 		dev->change_mtu = old_ch_mtu;
108 		dev->tx_queue_len = old_qlen;
109 		random_ether_addr(dev->dev_addr);
110 		netif_dormant_off(dev);
111 		return 0;
112 	}
113 
114 	return -EINVAL;
115 }
116 
117 
118 static int __init mod_init(void)
119 {
120 	register_hdlc_protocol(&proto);
121 	return 0;
122 }
123 
124 
125 
126 static void __exit mod_exit(void)
127 {
128 	unregister_hdlc_protocol(&proto);
129 }
130 
131 
132 module_init(mod_init);
133 module_exit(mod_exit);
134 
135 MODULE_AUTHOR("Krzysztof Halasa <khc@pm.waw.pl>");
136 MODULE_DESCRIPTION("Ethernet encapsulation support for generic HDLC");
137 MODULE_LICENSE("GPL v2");
138