blob: a13fc320752047cbdf0dfdb5f4c4ee685688aa73 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Generic HDLC support routines for Linux
3 * HDLC Ethernet emulation support
4 *
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +02005 * Copyright (C) 2002-2006 Krzysztof Halasa <khc@pm.waw.pl>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/errno.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/etherdevice.h>
14#include <linux/hdlc.h>
Krzysztof HaƂasa4dfce402008-06-30 19:06:40 +020015#include <linux/if_arp.h>
16#include <linux/inetdevice.h>
17#include <linux/init.h>
18#include <linux/kernel.h>
19#include <linux/module.h>
20#include <linux/pkt_sched.h>
21#include <linux/poll.h>
22#include <linux/rtnetlink.h>
23#include <linux/skbuff.h>
24#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +020026static int raw_eth_ioctl(struct net_device *dev, struct ifreq *ifr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
28static int eth_tx(struct sk_buff *skb, struct net_device *dev)
29{
30 int pad = ETH_ZLEN - skb->len;
31 if (pad > 0) { /* Pad the frame with zeros */
32 int len = skb->len;
33 if (skb_tailroom(skb) < pad)
34 if (pskb_expand_head(skb, 0, pad, GFP_ATOMIC)) {
Krzysztof Halasa198191c2008-06-30 23:26:53 +020035 dev->stats.tx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 dev_kfree_skb(skb);
37 return 0;
38 }
39 skb_put(skb, pad);
40 memset(skb->data + len, 0, pad);
41 }
42 return dev_to_hdlc(dev)->xmit(skb, dev);
43}
44
45
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +020046static struct hdlc_proto proto = {
47 .type_trans = eth_type_trans,
48 .ioctl = raw_eth_ioctl,
49 .module = THIS_MODULE,
50};
51
52
53static int raw_eth_ioctl(struct net_device *dev, struct ifreq *ifr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070054{
55 raw_hdlc_proto __user *raw_s = ifr->ifr_settings.ifs_ifsu.raw_hdlc;
56 const size_t size = sizeof(raw_hdlc_proto);
57 raw_hdlc_proto new_settings;
58 hdlc_device *hdlc = dev_to_hdlc(dev);
59 int result;
Al Viro90458402007-12-22 17:52:52 +000060 int (*old_ch_mtu)(struct net_device *, int);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 int old_qlen;
62
63 switch (ifr->ifr_settings.type) {
64 case IF_GET_PROTO:
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +020065 if (dev_to_hdlc(dev)->proto != &proto)
66 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 ifr->ifr_settings.type = IF_PROTO_HDLC_ETH;
68 if (ifr->ifr_settings.size < size) {
69 ifr->ifr_settings.size = size; /* data size wanted */
70 return -ENOBUFS;
71 }
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +020072 if (copy_to_user(raw_s, hdlc->state, size))
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 return -EFAULT;
74 return 0;
75
76 case IF_PROTO_HDLC_ETH:
77 if (!capable(CAP_NET_ADMIN))
78 return -EPERM;
79
80 if (dev->flags & IFF_UP)
81 return -EBUSY;
82
83 if (copy_from_user(&new_settings, raw_s, size))
84 return -EFAULT;
85
86 if (new_settings.encoding == ENCODING_DEFAULT)
87 new_settings.encoding = ENCODING_NRZ;
88
89 if (new_settings.parity == PARITY_DEFAULT)
90 new_settings.parity = PARITY_CRC16_PR1_CCITT;
91
92 result = hdlc->attach(dev, new_settings.encoding,
93 new_settings.parity);
94 if (result)
95 return result;
96
Krzysztof Halasa40d25142008-02-01 22:37:12 +010097 result = attach_hdlc_protocol(dev, &proto,
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +020098 sizeof(raw_hdlc_proto));
99 if (result)
100 return result;
101 memcpy(hdlc->state, &new_settings, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 dev->hard_start_xmit = eth_tx;
103 old_ch_mtu = dev->change_mtu;
104 old_qlen = dev->tx_queue_len;
105 ether_setup(dev);
106 dev->change_mtu = old_ch_mtu;
107 dev->tx_queue_len = old_qlen;
Krzysztof Halasa47eaa262008-02-01 22:39:50 +0100108 random_ether_addr(dev->dev_addr);
Krzysztof Halasa4bc83b42006-07-21 14:41:01 -0700109 netif_dormant_off(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 return 0;
111 }
112
113 return -EINVAL;
114}
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200115
116
117static int __init mod_init(void)
118{
119 register_hdlc_protocol(&proto);
120 return 0;
121}
122
123
124
125static void __exit mod_exit(void)
126{
127 unregister_hdlc_protocol(&proto);
128}
129
130
131module_init(mod_init);
132module_exit(mod_exit);
133
134MODULE_AUTHOR("Krzysztof Halasa <khc@pm.waw.pl>");
135MODULE_DESCRIPTION("Ethernet encapsulation support for generic HDLC");
136MODULE_LICENSE("GPL v2");