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