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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | #include <linux/errno.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | #include <linux/etherdevice.h> |
| 14 | #include <linux/hdlc.h> |
Krzysztof Hałasa | 4dfce40 | 2008-06-30 19:06:40 +0200 | [diff] [blame] | 15 | #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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 26 | static int raw_eth_ioctl(struct net_device *dev, struct ifreq *ifr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | |
Stephen Hemminger | d71a674 | 2009-08-31 19:50:47 +0000 | [diff] [blame] | 28 | static netdev_tx_t eth_tx(struct sk_buff *skb, struct net_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 Halasa | 198191c | 2008-06-30 23:26:53 +0200 | [diff] [blame] | 35 | dev->stats.tx_dropped++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | 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 Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 46 | static struct hdlc_proto proto = { |
| 47 | .type_trans = eth_type_trans, |
Krzysztof Hałasa | 991990a | 2009-01-08 22:52:11 +0100 | [diff] [blame] | 48 | .xmit = eth_tx, |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 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); |
Krzysztof Hałasa | 991990a | 2009-01-08 22:52:11 +0100 | [diff] [blame] | 60 | int result, old_qlen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | |
| 62 | switch (ifr->ifr_settings.type) { |
| 63 | case IF_GET_PROTO: |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 64 | if (dev_to_hdlc(dev)->proto != &proto) |
| 65 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | ifr->ifr_settings.type = IF_PROTO_HDLC_ETH; |
| 67 | if (ifr->ifr_settings.size < size) { |
| 68 | ifr->ifr_settings.size = size; /* data size wanted */ |
| 69 | return -ENOBUFS; |
| 70 | } |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 71 | if (copy_to_user(raw_s, hdlc->state, size)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | return -EFAULT; |
| 73 | return 0; |
| 74 | |
| 75 | case IF_PROTO_HDLC_ETH: |
| 76 | if (!capable(CAP_NET_ADMIN)) |
| 77 | return -EPERM; |
| 78 | |
| 79 | if (dev->flags & IFF_UP) |
| 80 | return -EBUSY; |
| 81 | |
| 82 | if (copy_from_user(&new_settings, raw_s, size)) |
| 83 | return -EFAULT; |
| 84 | |
| 85 | if (new_settings.encoding == ENCODING_DEFAULT) |
| 86 | new_settings.encoding = ENCODING_NRZ; |
| 87 | |
| 88 | if (new_settings.parity == PARITY_DEFAULT) |
| 89 | new_settings.parity = PARITY_CRC16_PR1_CCITT; |
| 90 | |
| 91 | result = hdlc->attach(dev, new_settings.encoding, |
| 92 | new_settings.parity); |
| 93 | if (result) |
| 94 | return result; |
| 95 | |
Krzysztof Halasa | 40d2514 | 2008-02-01 22:37:12 +0100 | [diff] [blame] | 96 | result = attach_hdlc_protocol(dev, &proto, |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 97 | sizeof(raw_hdlc_proto)); |
| 98 | if (result) |
| 99 | return result; |
| 100 | memcpy(hdlc->state, &new_settings, size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | old_qlen = dev->tx_queue_len; |
| 102 | ether_setup(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | dev->tx_queue_len = old_qlen; |
Krzysztof Halasa | 47eaa26 | 2008-02-01 22:39:50 +0100 | [diff] [blame] | 104 | random_ether_addr(dev->dev_addr); |
Krzysztof Halasa | 4bc83b4 | 2006-07-21 14:41:01 -0700 | [diff] [blame] | 105 | netif_dormant_off(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | return 0; |
| 107 | } |
| 108 | |
| 109 | return -EINVAL; |
| 110 | } |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 111 | |
| 112 | |
| 113 | static int __init mod_init(void) |
| 114 | { |
| 115 | register_hdlc_protocol(&proto); |
| 116 | return 0; |
| 117 | } |
| 118 | |
| 119 | |
| 120 | |
| 121 | static void __exit mod_exit(void) |
| 122 | { |
| 123 | unregister_hdlc_protocol(&proto); |
| 124 | } |
| 125 | |
| 126 | |
| 127 | module_init(mod_init); |
| 128 | module_exit(mod_exit); |
| 129 | |
| 130 | MODULE_AUTHOR("Krzysztof Halasa <khc@pm.waw.pl>"); |
| 131 | MODULE_DESCRIPTION("Ethernet encapsulation support for generic HDLC"); |
| 132 | MODULE_LICENSE("GPL v2"); |