Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Generic HDLC support routines for Linux |
| 3 | * HDLC Ethernet emulation support |
| 4 | * |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 5 | * Copyright (C) 2002-2006 Krzysztof Halasa <khc@pm.waw.pl> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 27 | static int raw_eth_ioctl(struct net_device *dev, struct ifreq *ifr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | 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 Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 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) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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; |
Al Viro | 9045840 | 2007-12-22 17:52:52 +0000 | [diff] [blame] | 61 | int (*old_ch_mtu)(struct net_device *, int); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | int old_qlen; |
| 63 | |
| 64 | switch (ifr->ifr_settings.type) { |
| 65 | case IF_GET_PROTO: |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 66 | if (dev_to_hdlc(dev)->proto != &proto) |
| 67 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | } |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 73 | if (copy_to_user(raw_s, hdlc->state, size)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Krzysztof Halasa | 40d2514 | 2008-02-01 22:37:12 +0100 | [diff] [blame] | 98 | result = attach_hdlc_protocol(dev, &proto, |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 99 | sizeof(raw_hdlc_proto)); |
| 100 | if (result) |
| 101 | return result; |
| 102 | memcpy(hdlc->state, &new_settings, size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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; |
Krzysztof Halasa | 47eaa26 | 2008-02-01 22:39:50 +0100 | [diff] [blame] | 109 | random_ether_addr(dev->dev_addr); |
Krzysztof Halasa | 4bc83b4 | 2006-07-21 14:41:01 -0700 | [diff] [blame] | 110 | netif_dormant_off(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | return 0; |
| 112 | } |
| 113 | |
| 114 | return -EINVAL; |
| 115 | } |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 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"); |