Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Generic HDLC support routines for Linux |
| 3 | * X.25 support |
| 4 | * |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 5 | * Copyright (C) 1999 - 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> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 13 | #include <linux/gfp.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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/lapb.h> |
| 20 | #include <linux/module.h> |
| 21 | #include <linux/pkt_sched.h> |
| 22 | #include <linux/poll.h> |
| 23 | #include <linux/rtnetlink.h> |
| 24 | #include <linux/skbuff.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | #include <net/x25device.h> |
| 26 | |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 27 | static int x25_ioctl(struct net_device *dev, struct ifreq *ifr); |
| 28 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | /* These functions are callbacks called by LAPB layer */ |
| 30 | |
| 31 | static void x25_connect_disconnect(struct net_device *dev, int reason, int code) |
| 32 | { |
| 33 | struct sk_buff *skb; |
| 34 | unsigned char *ptr; |
| 35 | |
| 36 | if ((skb = dev_alloc_skb(1)) == NULL) { |
| 37 | printk(KERN_ERR "%s: out of memory\n", dev->name); |
| 38 | return; |
| 39 | } |
| 40 | |
| 41 | ptr = skb_put(skb, 1); |
| 42 | *ptr = code; |
| 43 | |
| 44 | skb->protocol = x25_type_trans(skb, dev); |
| 45 | netif_rx(skb); |
| 46 | } |
| 47 | |
| 48 | |
| 49 | |
| 50 | static void x25_connected(struct net_device *dev, int reason) |
| 51 | { |
andrew hendry | 90b3e03 | 2010-04-19 13:30:24 +0000 | [diff] [blame] | 52 | x25_connect_disconnect(dev, reason, X25_IFACE_CONNECT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | |
| 56 | |
| 57 | static void x25_disconnected(struct net_device *dev, int reason) |
| 58 | { |
andrew hendry | 90b3e03 | 2010-04-19 13:30:24 +0000 | [diff] [blame] | 59 | x25_connect_disconnect(dev, reason, X25_IFACE_DISCONNECT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | |
| 63 | |
| 64 | static int x25_data_indication(struct net_device *dev, struct sk_buff *skb) |
| 65 | { |
| 66 | unsigned char *ptr; |
| 67 | |
| 68 | skb_push(skb, 1); |
| 69 | |
| 70 | if (skb_cow(skb, 1)) |
| 71 | return NET_RX_DROP; |
| 72 | |
| 73 | ptr = skb->data; |
andrew hendry | 90b3e03 | 2010-04-19 13:30:24 +0000 | [diff] [blame] | 74 | *ptr = X25_IFACE_DATA; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | |
| 76 | skb->protocol = x25_type_trans(skb, dev); |
| 77 | return netif_rx(skb); |
| 78 | } |
| 79 | |
| 80 | |
| 81 | |
| 82 | static void x25_data_transmit(struct net_device *dev, struct sk_buff *skb) |
| 83 | { |
| 84 | hdlc_device *hdlc = dev_to_hdlc(dev); |
| 85 | hdlc->xmit(skb, dev); /* Ignore return value :-( */ |
| 86 | } |
| 87 | |
| 88 | |
| 89 | |
Stephen Hemminger | d71a674 | 2009-08-31 19:50:47 +0000 | [diff] [blame] | 90 | static netdev_tx_t x25_xmit(struct sk_buff *skb, struct net_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | { |
| 92 | int result; |
| 93 | |
| 94 | |
| 95 | /* X.25 to LAPB */ |
| 96 | switch (skb->data[0]) { |
andrew hendry | 90b3e03 | 2010-04-19 13:30:24 +0000 | [diff] [blame] | 97 | case X25_IFACE_DATA: /* Data to be transmitted */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | skb_pull(skb, 1); |
| 99 | if ((result = lapb_data_request(dev, skb)) != LAPB_OK) |
| 100 | dev_kfree_skb(skb); |
Stephen Hemminger | d71a674 | 2009-08-31 19:50:47 +0000 | [diff] [blame] | 101 | return NETDEV_TX_OK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | |
andrew hendry | 90b3e03 | 2010-04-19 13:30:24 +0000 | [diff] [blame] | 103 | case X25_IFACE_CONNECT: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | if ((result = lapb_connect_request(dev))!= LAPB_OK) { |
| 105 | if (result == LAPB_CONNECTED) |
| 106 | /* Send connect confirm. msg to level 3 */ |
| 107 | x25_connected(dev, 0); |
| 108 | else |
| 109 | printk(KERN_ERR "%s: LAPB connect request " |
| 110 | "failed, error code = %i\n", |
| 111 | dev->name, result); |
| 112 | } |
| 113 | break; |
| 114 | |
andrew hendry | 90b3e03 | 2010-04-19 13:30:24 +0000 | [diff] [blame] | 115 | case X25_IFACE_DISCONNECT: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | if ((result = lapb_disconnect_request(dev)) != LAPB_OK) { |
| 117 | if (result == LAPB_NOTCONNECTED) |
| 118 | /* Send disconnect confirm. msg to level 3 */ |
| 119 | x25_disconnected(dev, 0); |
| 120 | else |
| 121 | printk(KERN_ERR "%s: LAPB disconnect request " |
| 122 | "failed, error code = %i\n", |
| 123 | dev->name, result); |
| 124 | } |
| 125 | break; |
| 126 | |
| 127 | default: /* to be defined */ |
| 128 | break; |
| 129 | } |
| 130 | |
| 131 | dev_kfree_skb(skb); |
Stephen Hemminger | d71a674 | 2009-08-31 19:50:47 +0000 | [diff] [blame] | 132 | return NETDEV_TX_OK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | |
| 136 | |
| 137 | static int x25_open(struct net_device *dev) |
| 138 | { |
| 139 | struct lapb_register_struct cb; |
| 140 | int result; |
| 141 | |
| 142 | cb.connect_confirmation = x25_connected; |
| 143 | cb.connect_indication = x25_connected; |
| 144 | cb.disconnect_confirmation = x25_disconnected; |
| 145 | cb.disconnect_indication = x25_disconnected; |
| 146 | cb.data_indication = x25_data_indication; |
| 147 | cb.data_transmit = x25_data_transmit; |
| 148 | |
| 149 | result = lapb_register(dev, &cb); |
| 150 | if (result != LAPB_OK) |
| 151 | return result; |
| 152 | return 0; |
| 153 | } |
| 154 | |
| 155 | |
| 156 | |
| 157 | static void x25_close(struct net_device *dev) |
| 158 | { |
| 159 | lapb_unregister(dev); |
| 160 | } |
| 161 | |
| 162 | |
| 163 | |
| 164 | static int x25_rx(struct sk_buff *skb) |
| 165 | { |
Krzysztof Halasa | 5406951 | 2008-09-03 18:21:56 +0200 | [diff] [blame] | 166 | struct net_device *dev = skb->dev; |
| 167 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL) { |
Krzysztof Halasa | 5406951 | 2008-09-03 18:21:56 +0200 | [diff] [blame] | 169 | dev->stats.rx_dropped++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | return NET_RX_DROP; |
| 171 | } |
| 172 | |
Krzysztof Halasa | 5406951 | 2008-09-03 18:21:56 +0200 | [diff] [blame] | 173 | if (lapb_data_received(dev, skb) == LAPB_OK) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | return NET_RX_SUCCESS; |
| 175 | |
Krzysztof Halasa | 5406951 | 2008-09-03 18:21:56 +0200 | [diff] [blame] | 176 | dev->stats.rx_errors++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | dev_kfree_skb_any(skb); |
| 178 | return NET_RX_DROP; |
| 179 | } |
| 180 | |
| 181 | |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 182 | static struct hdlc_proto proto = { |
| 183 | .open = x25_open, |
| 184 | .close = x25_close, |
| 185 | .ioctl = x25_ioctl, |
Krzysztof Halasa | 40d2514 | 2008-02-01 22:37:12 +0100 | [diff] [blame] | 186 | .netif_rx = x25_rx, |
Krzysztof Hałasa | 991990a | 2009-01-08 22:52:11 +0100 | [diff] [blame] | 187 | .xmit = x25_xmit, |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 188 | .module = THIS_MODULE, |
| 189 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 191 | |
| 192 | static int x25_ioctl(struct net_device *dev, struct ifreq *ifr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | { |
| 194 | hdlc_device *hdlc = dev_to_hdlc(dev); |
| 195 | int result; |
| 196 | |
| 197 | switch (ifr->ifr_settings.type) { |
| 198 | case IF_GET_PROTO: |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 199 | if (dev_to_hdlc(dev)->proto != &proto) |
| 200 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | ifr->ifr_settings.type = IF_PROTO_X25; |
| 202 | return 0; /* return protocol only, no settable parameters */ |
| 203 | |
| 204 | case IF_PROTO_X25: |
Rudy Matela | 640462c | 2009-12-09 11:35:40 -0300 | [diff] [blame] | 205 | if (!capable(CAP_NET_ADMIN)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | return -EPERM; |
| 207 | |
Rudy Matela | 640462c | 2009-12-09 11:35:40 -0300 | [diff] [blame] | 208 | if (dev->flags & IFF_UP) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | return -EBUSY; |
| 210 | |
| 211 | result=hdlc->attach(dev, ENCODING_NRZ,PARITY_CRC16_PR1_CCITT); |
| 212 | if (result) |
| 213 | return result; |
| 214 | |
Krzysztof Halasa | 40d2514 | 2008-02-01 22:37:12 +0100 | [diff] [blame] | 215 | if ((result = attach_hdlc_protocol(dev, &proto, 0))) |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 216 | return result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | dev->type = ARPHRD_X25; |
Krzysztof Halasa | 4bc83b4 | 2006-07-21 14:41:01 -0700 | [diff] [blame] | 218 | netif_dormant_off(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | return 0; |
| 220 | } |
| 221 | |
| 222 | return -EINVAL; |
| 223 | } |
Krzysztof Halasa | eb2a2fd | 2006-09-26 23:23:45 +0200 | [diff] [blame] | 224 | |
| 225 | |
| 226 | static int __init mod_init(void) |
| 227 | { |
| 228 | register_hdlc_protocol(&proto); |
| 229 | return 0; |
| 230 | } |
| 231 | |
| 232 | |
| 233 | |
| 234 | static void __exit mod_exit(void) |
| 235 | { |
| 236 | unregister_hdlc_protocol(&proto); |
| 237 | } |
| 238 | |
| 239 | |
| 240 | module_init(mod_init); |
| 241 | module_exit(mod_exit); |
| 242 | |
| 243 | MODULE_AUTHOR("Krzysztof Halasa <khc@pm.waw.pl>"); |
| 244 | MODULE_DESCRIPTION("X.25 protocol support for generic HDLC"); |
| 245 | MODULE_LICENSE("GPL v2"); |