blob: d20c685f67111281699dcc1449b366dc22fb04e9 [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
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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#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
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +020027static int raw_eth_ioctl(struct net_device *dev, struct ifreq *ifr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
29static 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 hdlc_stats(dev)->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
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +020047static struct hdlc_proto proto = {
48 .type_trans = eth_type_trans,
49 .ioctl = raw_eth_ioctl,
50 .module = THIS_MODULE,
51};
52
53
54static int raw_eth_ioctl(struct net_device *dev, struct ifreq *ifr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070055{
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;
Al Viro90458402007-12-22 17:52:52 +000061 int (*old_ch_mtu)(struct net_device *, int);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 int old_qlen;
63
64 switch (ifr->ifr_settings.type) {
65 case IF_GET_PROTO:
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +020066 if (dev_to_hdlc(dev)->proto != &proto)
67 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 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 }
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +020073 if (copy_to_user(raw_s, hdlc->state, size))
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 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
Krzysztof Halasa40d25142008-02-01 22:37:12 +010098 result = attach_hdlc_protocol(dev, &proto,
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +020099 sizeof(raw_hdlc_proto));
100 if (result)
101 return result;
102 memcpy(hdlc->state, &new_settings, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 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;
Krzysztof Halasa47eaa262008-02-01 22:39:50 +0100109 random_ether_addr(dev->dev_addr);
Krzysztof Halasa4bc83b42006-07-21 14:41:01 -0700110 netif_dormant_off(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 return 0;
112 }
113
114 return -EINVAL;
115}
Krzysztof Halasaeb2a2fd2006-09-26 23:23:45 +0200116
117
118static int __init mod_init(void)
119{
120 register_hdlc_protocol(&proto);
121 return 0;
122}
123
124
125
126static void __exit mod_exit(void)
127{
128 unregister_hdlc_protocol(&proto);
129}
130
131
132module_init(mod_init);
133module_exit(mod_exit);
134
135MODULE_AUTHOR("Krzysztof Halasa <khc@pm.waw.pl>");
136MODULE_DESCRIPTION("Ethernet encapsulation support for generic HDLC");
137MODULE_LICENSE("GPL v2");