xref: /linux/drivers/net/wan/hdlc_raw_eth.c (revision 1da177e4c3f41524e886b7f1b8a0c1fc7321cac2)
1*1da177e4SLinus Torvalds /*
2*1da177e4SLinus Torvalds  * Generic HDLC support routines for Linux
3*1da177e4SLinus Torvalds  * HDLC Ethernet emulation support
4*1da177e4SLinus Torvalds  *
5*1da177e4SLinus Torvalds  * Copyright (C) 2002-2003 Krzysztof Halasa <khc@pm.waw.pl>
6*1da177e4SLinus Torvalds  *
7*1da177e4SLinus Torvalds  * This program is free software; you can redistribute it and/or modify it
8*1da177e4SLinus Torvalds  * under the terms of version 2 of the GNU General Public License
9*1da177e4SLinus Torvalds  * as published by the Free Software Foundation.
10*1da177e4SLinus Torvalds  */
11*1da177e4SLinus Torvalds 
12*1da177e4SLinus Torvalds #include <linux/module.h>
13*1da177e4SLinus Torvalds #include <linux/kernel.h>
14*1da177e4SLinus Torvalds #include <linux/slab.h>
15*1da177e4SLinus Torvalds #include <linux/poll.h>
16*1da177e4SLinus Torvalds #include <linux/errno.h>
17*1da177e4SLinus Torvalds #include <linux/if_arp.h>
18*1da177e4SLinus Torvalds #include <linux/init.h>
19*1da177e4SLinus Torvalds #include <linux/skbuff.h>
20*1da177e4SLinus Torvalds #include <linux/pkt_sched.h>
21*1da177e4SLinus Torvalds #include <linux/random.h>
22*1da177e4SLinus Torvalds #include <linux/inetdevice.h>
23*1da177e4SLinus Torvalds #include <linux/lapb.h>
24*1da177e4SLinus Torvalds #include <linux/rtnetlink.h>
25*1da177e4SLinus Torvalds #include <linux/etherdevice.h>
26*1da177e4SLinus Torvalds #include <linux/hdlc.h>
27*1da177e4SLinus Torvalds 
28*1da177e4SLinus Torvalds 
29*1da177e4SLinus Torvalds static int eth_tx(struct sk_buff *skb, struct net_device *dev)
30*1da177e4SLinus Torvalds {
31*1da177e4SLinus Torvalds 	int pad = ETH_ZLEN - skb->len;
32*1da177e4SLinus Torvalds 	if (pad > 0) {		/* Pad the frame with zeros */
33*1da177e4SLinus Torvalds 		int len = skb->len;
34*1da177e4SLinus Torvalds 		if (skb_tailroom(skb) < pad)
35*1da177e4SLinus Torvalds 			if (pskb_expand_head(skb, 0, pad, GFP_ATOMIC)) {
36*1da177e4SLinus Torvalds 				hdlc_stats(dev)->tx_dropped++;
37*1da177e4SLinus Torvalds 				dev_kfree_skb(skb);
38*1da177e4SLinus Torvalds 				return 0;
39*1da177e4SLinus Torvalds 			}
40*1da177e4SLinus Torvalds 		skb_put(skb, pad);
41*1da177e4SLinus Torvalds 		memset(skb->data + len, 0, pad);
42*1da177e4SLinus Torvalds 	}
43*1da177e4SLinus Torvalds 	return dev_to_hdlc(dev)->xmit(skb, dev);
44*1da177e4SLinus Torvalds }
45*1da177e4SLinus Torvalds 
46*1da177e4SLinus Torvalds 
47*1da177e4SLinus Torvalds int hdlc_raw_eth_ioctl(struct net_device *dev, struct ifreq *ifr)
48*1da177e4SLinus Torvalds {
49*1da177e4SLinus Torvalds 	raw_hdlc_proto __user *raw_s = ifr->ifr_settings.ifs_ifsu.raw_hdlc;
50*1da177e4SLinus Torvalds 	const size_t size = sizeof(raw_hdlc_proto);
51*1da177e4SLinus Torvalds 	raw_hdlc_proto new_settings;
52*1da177e4SLinus Torvalds 	hdlc_device *hdlc = dev_to_hdlc(dev);
53*1da177e4SLinus Torvalds 	int result;
54*1da177e4SLinus Torvalds 	void *old_ch_mtu;
55*1da177e4SLinus Torvalds 	int old_qlen;
56*1da177e4SLinus Torvalds 
57*1da177e4SLinus Torvalds 	switch (ifr->ifr_settings.type) {
58*1da177e4SLinus Torvalds 	case IF_GET_PROTO:
59*1da177e4SLinus Torvalds 		ifr->ifr_settings.type = IF_PROTO_HDLC_ETH;
60*1da177e4SLinus Torvalds 		if (ifr->ifr_settings.size < size) {
61*1da177e4SLinus Torvalds 			ifr->ifr_settings.size = size; /* data size wanted */
62*1da177e4SLinus Torvalds 			return -ENOBUFS;
63*1da177e4SLinus Torvalds 		}
64*1da177e4SLinus Torvalds 		if (copy_to_user(raw_s, &hdlc->state.raw_hdlc.settings, size))
65*1da177e4SLinus Torvalds 			return -EFAULT;
66*1da177e4SLinus Torvalds 		return 0;
67*1da177e4SLinus Torvalds 
68*1da177e4SLinus Torvalds 	case IF_PROTO_HDLC_ETH:
69*1da177e4SLinus Torvalds 		if (!capable(CAP_NET_ADMIN))
70*1da177e4SLinus Torvalds 			return -EPERM;
71*1da177e4SLinus Torvalds 
72*1da177e4SLinus Torvalds 		if (dev->flags & IFF_UP)
73*1da177e4SLinus Torvalds 			return -EBUSY;
74*1da177e4SLinus Torvalds 
75*1da177e4SLinus Torvalds 		if (copy_from_user(&new_settings, raw_s, size))
76*1da177e4SLinus Torvalds 			return -EFAULT;
77*1da177e4SLinus Torvalds 
78*1da177e4SLinus Torvalds 		if (new_settings.encoding == ENCODING_DEFAULT)
79*1da177e4SLinus Torvalds 			new_settings.encoding = ENCODING_NRZ;
80*1da177e4SLinus Torvalds 
81*1da177e4SLinus Torvalds 		if (new_settings.parity == PARITY_DEFAULT)
82*1da177e4SLinus Torvalds 			new_settings.parity = PARITY_CRC16_PR1_CCITT;
83*1da177e4SLinus Torvalds 
84*1da177e4SLinus Torvalds 		result = hdlc->attach(dev, new_settings.encoding,
85*1da177e4SLinus Torvalds 				      new_settings.parity);
86*1da177e4SLinus Torvalds 		if (result)
87*1da177e4SLinus Torvalds 			return result;
88*1da177e4SLinus Torvalds 
89*1da177e4SLinus Torvalds 		hdlc_proto_detach(hdlc);
90*1da177e4SLinus Torvalds 		memcpy(&hdlc->state.raw_hdlc.settings, &new_settings, size);
91*1da177e4SLinus Torvalds 		memset(&hdlc->proto, 0, sizeof(hdlc->proto));
92*1da177e4SLinus Torvalds 
93*1da177e4SLinus Torvalds 		hdlc->proto.type_trans = eth_type_trans;
94*1da177e4SLinus Torvalds 		hdlc->proto.id = IF_PROTO_HDLC_ETH;
95*1da177e4SLinus Torvalds 		dev->hard_start_xmit = eth_tx;
96*1da177e4SLinus Torvalds 		old_ch_mtu = dev->change_mtu;
97*1da177e4SLinus Torvalds 		old_qlen = dev->tx_queue_len;
98*1da177e4SLinus Torvalds 		ether_setup(dev);
99*1da177e4SLinus Torvalds 		dev->change_mtu = old_ch_mtu;
100*1da177e4SLinus Torvalds 		dev->tx_queue_len = old_qlen;
101*1da177e4SLinus Torvalds 		memcpy(dev->dev_addr, "\x00\x01", 2);
102*1da177e4SLinus Torvalds                 get_random_bytes(dev->dev_addr + 2, ETH_ALEN - 2);
103*1da177e4SLinus Torvalds 		return 0;
104*1da177e4SLinus Torvalds 	}
105*1da177e4SLinus Torvalds 
106*1da177e4SLinus Torvalds 	return -EINVAL;
107*1da177e4SLinus Torvalds }
108