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