kmemtrace: SLOB hooks.
[linux-2.6/kmemtrace.git] / drivers / net / wan / hdlc_raw_eth.c
blobd20c685f67111281699dcc1449b366dc22fb04e9
1 /*
2 * Generic HDLC support routines for Linux
3 * HDLC Ethernet emulation support
5 * Copyright (C) 2002-2006 Krzysztof Halasa <khc@pm.waw.pl>
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.
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>
27 static int raw_eth_ioctl(struct net_device *dev, struct ifreq *ifr);
29 static int eth_tx(struct sk_buff *skb, struct net_device *dev)
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;
40 skb_put(skb, pad);
41 memset(skb->data + len, 0, pad);
43 return dev_to_hdlc(dev)->xmit(skb, dev);
47 static struct hdlc_proto proto = {
48 .type_trans = eth_type_trans,
49 .ioctl = raw_eth_ioctl,
50 .module = THIS_MODULE,
54 static int raw_eth_ioctl(struct net_device *dev, struct ifreq *ifr)
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;
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;
73 if (copy_to_user(raw_s, hdlc->state, size))
74 return -EFAULT;
75 return 0;
77 case IF_PROTO_HDLC_ETH:
78 if (!capable(CAP_NET_ADMIN))
79 return -EPERM;
81 if (dev->flags & IFF_UP)
82 return -EBUSY;
84 if (copy_from_user(&new_settings, raw_s, size))
85 return -EFAULT;
87 if (new_settings.encoding == ENCODING_DEFAULT)
88 new_settings.encoding = ENCODING_NRZ;
90 if (new_settings.parity == PARITY_DEFAULT)
91 new_settings.parity = PARITY_CRC16_PR1_CCITT;
93 result = hdlc->attach(dev, new_settings.encoding,
94 new_settings.parity);
95 if (result)
96 return result;
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;
114 return -EINVAL;
118 static int __init mod_init(void)
120 register_hdlc_protocol(&proto);
121 return 0;
126 static void __exit mod_exit(void)
128 unregister_hdlc_protocol(&proto);
132 module_init(mod_init);
133 module_exit(mod_exit);
135 MODULE_AUTHOR("Krzysztof Halasa <khc@pm.waw.pl>");
136 MODULE_DESCRIPTION("Ethernet encapsulation support for generic HDLC");
137 MODULE_LICENSE("GPL v2");