1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Generic HDLC support routines for Linux 4 * X.25 support 5 * 6 * Copyright (C) 1999 - 2006 Krzysztof Halasa <khc@pm.waw.pl> 7 */ 8 9 #include <linux/errno.h> 10 #include <linux/gfp.h> 11 #include <linux/hdlc.h> 12 #include <linux/if_arp.h> 13 #include <linux/inetdevice.h> 14 #include <linux/init.h> 15 #include <linux/kernel.h> 16 #include <linux/lapb.h> 17 #include <linux/module.h> 18 #include <linux/pkt_sched.h> 19 #include <linux/poll.h> 20 #include <linux/rtnetlink.h> 21 #include <linux/skbuff.h> 22 #include <net/x25device.h> 23 24 struct x25_state { 25 x25_hdlc_proto settings; 26 }; 27 28 static int x25_ioctl(struct net_device *dev, struct ifreq *ifr); 29 30 static struct x25_state *state(hdlc_device *hdlc) 31 { 32 return hdlc->state; 33 } 34 35 /* These functions are callbacks called by LAPB layer */ 36 37 static void x25_connect_disconnect(struct net_device *dev, int reason, int code) 38 { 39 struct sk_buff *skb; 40 unsigned char *ptr; 41 42 if ((skb = dev_alloc_skb(1)) == NULL) { 43 netdev_err(dev, "out of memory\n"); 44 return; 45 } 46 47 ptr = skb_put(skb, 1); 48 *ptr = code; 49 50 skb->protocol = x25_type_trans(skb, dev); 51 netif_rx(skb); 52 } 53 54 55 56 static void x25_connected(struct net_device *dev, int reason) 57 { 58 x25_connect_disconnect(dev, reason, X25_IFACE_CONNECT); 59 } 60 61 62 63 static void x25_disconnected(struct net_device *dev, int reason) 64 { 65 x25_connect_disconnect(dev, reason, X25_IFACE_DISCONNECT); 66 } 67 68 69 70 static int x25_data_indication(struct net_device *dev, struct sk_buff *skb) 71 { 72 unsigned char *ptr; 73 74 if (skb_cow(skb, 1)) { 75 kfree_skb(skb); 76 return NET_RX_DROP; 77 } 78 79 skb_push(skb, 1); 80 skb_reset_network_header(skb); 81 82 ptr = skb->data; 83 *ptr = X25_IFACE_DATA; 84 85 skb->protocol = x25_type_trans(skb, dev); 86 return netif_rx(skb); 87 } 88 89 90 91 static void x25_data_transmit(struct net_device *dev, struct sk_buff *skb) 92 { 93 hdlc_device *hdlc = dev_to_hdlc(dev); 94 95 skb_reset_network_header(skb); 96 skb->protocol = hdlc_type_trans(skb, dev); 97 98 if (dev_nit_active(dev)) 99 dev_queue_xmit_nit(skb, dev); 100 101 hdlc->xmit(skb, dev); /* Ignore return value :-( */ 102 } 103 104 105 106 static netdev_tx_t x25_xmit(struct sk_buff *skb, struct net_device *dev) 107 { 108 int result; 109 110 /* There should be a pseudo header of 1 byte added by upper layers. 111 * Check to make sure it is there before reading it. 112 */ 113 if (skb->len < 1) { 114 kfree_skb(skb); 115 return NETDEV_TX_OK; 116 } 117 118 switch (skb->data[0]) { 119 case X25_IFACE_DATA: /* Data to be transmitted */ 120 skb_pull(skb, 1); 121 skb_reset_network_header(skb); 122 if ((result = lapb_data_request(dev, skb)) != LAPB_OK) 123 dev_kfree_skb(skb); 124 return NETDEV_TX_OK; 125 126 case X25_IFACE_CONNECT: 127 if ((result = lapb_connect_request(dev))!= LAPB_OK) { 128 if (result == LAPB_CONNECTED) 129 /* Send connect confirm. msg to level 3 */ 130 x25_connected(dev, 0); 131 else 132 netdev_err(dev, "LAPB connect request failed, error code = %i\n", 133 result); 134 } 135 break; 136 137 case X25_IFACE_DISCONNECT: 138 if ((result = lapb_disconnect_request(dev)) != LAPB_OK) { 139 if (result == LAPB_NOTCONNECTED) 140 /* Send disconnect confirm. msg to level 3 */ 141 x25_disconnected(dev, 0); 142 else 143 netdev_err(dev, "LAPB disconnect request failed, error code = %i\n", 144 result); 145 } 146 break; 147 148 default: /* to be defined */ 149 break; 150 } 151 152 dev_kfree_skb(skb); 153 return NETDEV_TX_OK; 154 } 155 156 157 158 static int x25_open(struct net_device *dev) 159 { 160 static const struct lapb_register_struct cb = { 161 .connect_confirmation = x25_connected, 162 .connect_indication = x25_connected, 163 .disconnect_confirmation = x25_disconnected, 164 .disconnect_indication = x25_disconnected, 165 .data_indication = x25_data_indication, 166 .data_transmit = x25_data_transmit, 167 }; 168 hdlc_device *hdlc = dev_to_hdlc(dev); 169 struct lapb_parms_struct params; 170 int result; 171 172 result = lapb_register(dev, &cb); 173 if (result != LAPB_OK) 174 return result; 175 176 result = lapb_getparms(dev, ¶ms); 177 if (result != LAPB_OK) 178 return result; 179 180 if (state(hdlc)->settings.dce) 181 params.mode = params.mode | LAPB_DCE; 182 183 if (state(hdlc)->settings.modulo == 128) 184 params.mode = params.mode | LAPB_EXTENDED; 185 186 params.window = state(hdlc)->settings.window; 187 params.t1 = state(hdlc)->settings.t1; 188 params.t2 = state(hdlc)->settings.t2; 189 params.n2 = state(hdlc)->settings.n2; 190 191 result = lapb_setparms(dev, ¶ms); 192 if (result != LAPB_OK) 193 return result; 194 195 return 0; 196 } 197 198 199 200 static void x25_close(struct net_device *dev) 201 { 202 lapb_unregister(dev); 203 } 204 205 206 207 static int x25_rx(struct sk_buff *skb) 208 { 209 struct net_device *dev = skb->dev; 210 211 if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL) { 212 dev->stats.rx_dropped++; 213 return NET_RX_DROP; 214 } 215 216 if (lapb_data_received(dev, skb) == LAPB_OK) 217 return NET_RX_SUCCESS; 218 219 dev->stats.rx_errors++; 220 dev_kfree_skb_any(skb); 221 return NET_RX_DROP; 222 } 223 224 225 static struct hdlc_proto proto = { 226 .open = x25_open, 227 .close = x25_close, 228 .ioctl = x25_ioctl, 229 .netif_rx = x25_rx, 230 .xmit = x25_xmit, 231 .module = THIS_MODULE, 232 }; 233 234 235 static int x25_ioctl(struct net_device *dev, struct ifreq *ifr) 236 { 237 x25_hdlc_proto __user *x25_s = ifr->ifr_settings.ifs_ifsu.x25; 238 const size_t size = sizeof(x25_hdlc_proto); 239 hdlc_device *hdlc = dev_to_hdlc(dev); 240 x25_hdlc_proto new_settings; 241 int result; 242 243 switch (ifr->ifr_settings.type) { 244 case IF_GET_PROTO: 245 if (dev_to_hdlc(dev)->proto != &proto) 246 return -EINVAL; 247 ifr->ifr_settings.type = IF_PROTO_X25; 248 if (ifr->ifr_settings.size < size) { 249 ifr->ifr_settings.size = size; /* data size wanted */ 250 return -ENOBUFS; 251 } 252 if (copy_to_user(x25_s, &state(hdlc)->settings, size)) 253 return -EFAULT; 254 return 0; 255 256 case IF_PROTO_X25: 257 if (!capable(CAP_NET_ADMIN)) 258 return -EPERM; 259 260 if (dev->flags & IFF_UP) 261 return -EBUSY; 262 263 /* backward compatibility */ 264 if (ifr->ifr_settings.size == 0) { 265 new_settings.dce = 0; 266 new_settings.modulo = 8; 267 new_settings.window = 7; 268 new_settings.t1 = 3; 269 new_settings.t2 = 1; 270 new_settings.n2 = 10; 271 } 272 else { 273 if (copy_from_user(&new_settings, x25_s, size)) 274 return -EFAULT; 275 276 if ((new_settings.dce != 0 && 277 new_settings.dce != 1) || 278 (new_settings.modulo != 8 && 279 new_settings.modulo != 128) || 280 new_settings.window < 1 || 281 (new_settings.modulo == 8 && 282 new_settings.window > 7) || 283 (new_settings.modulo == 128 && 284 new_settings.window > 127) || 285 new_settings.t1 < 1 || 286 new_settings.t1 > 255 || 287 new_settings.t2 < 1 || 288 new_settings.t2 > 255 || 289 new_settings.n2 < 1 || 290 new_settings.n2 > 255) 291 return -EINVAL; 292 } 293 294 result=hdlc->attach(dev, ENCODING_NRZ,PARITY_CRC16_PR1_CCITT); 295 if (result) 296 return result; 297 298 if ((result = attach_hdlc_protocol(dev, &proto, 299 sizeof(struct x25_state)))) 300 return result; 301 302 memcpy(&state(hdlc)->settings, &new_settings, size); 303 304 /* There's no header_ops so hard_header_len should be 0. */ 305 dev->hard_header_len = 0; 306 /* When transmitting data: 307 * first we'll remove a pseudo header of 1 byte, 308 * then we'll prepend an LAPB header of at most 3 bytes. 309 */ 310 dev->needed_headroom = 3 - 1; 311 312 dev->type = ARPHRD_X25; 313 call_netdevice_notifiers(NETDEV_POST_TYPE_CHANGE, dev); 314 netif_dormant_off(dev); 315 return 0; 316 } 317 318 return -EINVAL; 319 } 320 321 322 static int __init mod_init(void) 323 { 324 register_hdlc_protocol(&proto); 325 return 0; 326 } 327 328 329 330 static void __exit mod_exit(void) 331 { 332 unregister_hdlc_protocol(&proto); 333 } 334 335 336 module_init(mod_init); 337 module_exit(mod_exit); 338 339 MODULE_AUTHOR("Krzysztof Halasa <khc@pm.waw.pl>"); 340 MODULE_DESCRIPTION("X.25 protocol support for generic HDLC"); 341 MODULE_LICENSE("GPL v2"); 342