1 // SPDX-License-Identifier: GPL-2.0-only
3 * Generic HDLC support routines for Linux
6 * Copyright (C) 1999 - 2006 Krzysztof Halasa <khc@pm.waw.pl>
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>
25 x25_hdlc_proto settings;
27 spinlock_t up_lock; /* Protects "up" */
30 static int x25_ioctl(struct net_device *dev, struct ifreq *ifr);
32 static struct x25_state *state(hdlc_device *hdlc)
37 /* These functions are callbacks called by LAPB layer */
39 static void x25_connect_disconnect(struct net_device *dev, int reason, int code)
44 if ((skb = dev_alloc_skb(1)) == NULL) {
45 netdev_err(dev, "out of memory\n");
49 ptr = skb_put(skb, 1);
52 skb->protocol = x25_type_trans(skb, dev);
58 static void x25_connected(struct net_device *dev, int reason)
60 x25_connect_disconnect(dev, reason, X25_IFACE_CONNECT);
65 static void x25_disconnected(struct net_device *dev, int reason)
67 x25_connect_disconnect(dev, reason, X25_IFACE_DISCONNECT);
72 static int x25_data_indication(struct net_device *dev, struct sk_buff *skb)
76 if (skb_cow(skb, 1)) {
84 *ptr = X25_IFACE_DATA;
86 skb->protocol = x25_type_trans(skb, dev);
92 static void x25_data_transmit(struct net_device *dev, struct sk_buff *skb)
94 hdlc_device *hdlc = dev_to_hdlc(dev);
96 skb_reset_network_header(skb);
97 skb->protocol = hdlc_type_trans(skb, dev);
99 if (dev_nit_active(dev))
100 dev_queue_xmit_nit(skb, dev);
102 hdlc->xmit(skb, dev); /* Ignore return value :-( */
107 static netdev_tx_t x25_xmit(struct sk_buff *skb, struct net_device *dev)
109 hdlc_device *hdlc = dev_to_hdlc(dev);
110 struct x25_state *x25st = state(hdlc);
113 /* There should be a pseudo header of 1 byte added by upper layers.
114 * Check to make sure it is there before reading it.
121 spin_lock_bh(&x25st->up_lock);
123 spin_unlock_bh(&x25st->up_lock);
128 switch (skb->data[0]) {
129 case X25_IFACE_DATA: /* Data to be transmitted */
131 if ((result = lapb_data_request(dev, skb)) != LAPB_OK)
133 spin_unlock_bh(&x25st->up_lock);
136 case X25_IFACE_CONNECT:
137 if ((result = lapb_connect_request(dev))!= LAPB_OK) {
138 if (result == LAPB_CONNECTED)
139 /* Send connect confirm. msg to level 3 */
140 x25_connected(dev, 0);
142 netdev_err(dev, "LAPB connect request failed, error code = %i\n",
147 case X25_IFACE_DISCONNECT:
148 if ((result = lapb_disconnect_request(dev)) != LAPB_OK) {
149 if (result == LAPB_NOTCONNECTED)
150 /* Send disconnect confirm. msg to level 3 */
151 x25_disconnected(dev, 0);
153 netdev_err(dev, "LAPB disconnect request failed, error code = %i\n",
158 default: /* to be defined */
162 spin_unlock_bh(&x25st->up_lock);
169 static int x25_open(struct net_device *dev)
171 static const struct lapb_register_struct cb = {
172 .connect_confirmation = x25_connected,
173 .connect_indication = x25_connected,
174 .disconnect_confirmation = x25_disconnected,
175 .disconnect_indication = x25_disconnected,
176 .data_indication = x25_data_indication,
177 .data_transmit = x25_data_transmit,
179 hdlc_device *hdlc = dev_to_hdlc(dev);
180 struct x25_state *x25st = state(hdlc);
181 struct lapb_parms_struct params;
184 result = lapb_register(dev, &cb);
185 if (result != LAPB_OK)
188 result = lapb_getparms(dev, ¶ms);
189 if (result != LAPB_OK)
192 if (state(hdlc)->settings.dce)
193 params.mode = params.mode | LAPB_DCE;
195 if (state(hdlc)->settings.modulo == 128)
196 params.mode = params.mode | LAPB_EXTENDED;
198 params.window = state(hdlc)->settings.window;
199 params.t1 = state(hdlc)->settings.t1;
200 params.t2 = state(hdlc)->settings.t2;
201 params.n2 = state(hdlc)->settings.n2;
203 result = lapb_setparms(dev, ¶ms);
204 if (result != LAPB_OK)
207 spin_lock_bh(&x25st->up_lock);
209 spin_unlock_bh(&x25st->up_lock);
216 static void x25_close(struct net_device *dev)
218 hdlc_device *hdlc = dev_to_hdlc(dev);
219 struct x25_state *x25st = state(hdlc);
221 spin_lock_bh(&x25st->up_lock);
223 spin_unlock_bh(&x25st->up_lock);
225 lapb_unregister(dev);
230 static int x25_rx(struct sk_buff *skb)
232 struct net_device *dev = skb->dev;
233 hdlc_device *hdlc = dev_to_hdlc(dev);
234 struct x25_state *x25st = state(hdlc);
236 if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL) {
237 dev->stats.rx_dropped++;
241 spin_lock_bh(&x25st->up_lock);
243 spin_unlock_bh(&x25st->up_lock);
245 dev->stats.rx_dropped++;
249 if (lapb_data_received(dev, skb) == LAPB_OK) {
250 spin_unlock_bh(&x25st->up_lock);
251 return NET_RX_SUCCESS;
254 spin_unlock_bh(&x25st->up_lock);
255 dev->stats.rx_errors++;
256 dev_kfree_skb_any(skb);
261 static struct hdlc_proto proto = {
267 .module = THIS_MODULE,
271 static int x25_ioctl(struct net_device *dev, struct ifreq *ifr)
273 x25_hdlc_proto __user *x25_s = ifr->ifr_settings.ifs_ifsu.x25;
274 const size_t size = sizeof(x25_hdlc_proto);
275 hdlc_device *hdlc = dev_to_hdlc(dev);
276 x25_hdlc_proto new_settings;
279 switch (ifr->ifr_settings.type) {
281 if (dev_to_hdlc(dev)->proto != &proto)
283 ifr->ifr_settings.type = IF_PROTO_X25;
284 if (ifr->ifr_settings.size < size) {
285 ifr->ifr_settings.size = size; /* data size wanted */
288 if (copy_to_user(x25_s, &state(hdlc)->settings, size))
293 if (!capable(CAP_NET_ADMIN))
296 if (dev->flags & IFF_UP)
299 /* backward compatibility */
300 if (ifr->ifr_settings.size == 0) {
301 new_settings.dce = 0;
302 new_settings.modulo = 8;
303 new_settings.window = 7;
306 new_settings.n2 = 10;
309 if (copy_from_user(&new_settings, x25_s, size))
312 if ((new_settings.dce != 0 &&
313 new_settings.dce != 1) ||
314 (new_settings.modulo != 8 &&
315 new_settings.modulo != 128) ||
316 new_settings.window < 1 ||
317 (new_settings.modulo == 8 &&
318 new_settings.window > 7) ||
319 (new_settings.modulo == 128 &&
320 new_settings.window > 127) ||
321 new_settings.t1 < 1 ||
322 new_settings.t1 > 255 ||
323 new_settings.t2 < 1 ||
324 new_settings.t2 > 255 ||
325 new_settings.n2 < 1 ||
326 new_settings.n2 > 255)
330 result=hdlc->attach(dev, ENCODING_NRZ,PARITY_CRC16_PR1_CCITT);
334 if ((result = attach_hdlc_protocol(dev, &proto,
335 sizeof(struct x25_state))))
338 memcpy(&state(hdlc)->settings, &new_settings, size);
339 state(hdlc)->up = false;
340 spin_lock_init(&state(hdlc)->up_lock);
342 /* There's no header_ops so hard_header_len should be 0. */
343 dev->hard_header_len = 0;
344 /* When transmitting data:
345 * first we'll remove a pseudo header of 1 byte,
346 * then we'll prepend an LAPB header of at most 3 bytes.
348 dev->needed_headroom = 3 - 1;
350 dev->type = ARPHRD_X25;
351 call_netdevice_notifiers(NETDEV_POST_TYPE_CHANGE, dev);
352 netif_dormant_off(dev);
360 static int __init mod_init(void)
362 register_hdlc_protocol(&proto);
368 static void __exit mod_exit(void)
370 unregister_hdlc_protocol(&proto);
374 module_init(mod_init);
375 module_exit(mod_exit);
377 MODULE_AUTHOR("Krzysztof Halasa <khc@pm.waw.pl>");
378 MODULE_DESCRIPTION("X.25 protocol support for generic HDLC");
379 MODULE_LICENSE("GPL v2");