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)) {
82 skb_reset_network_header(skb);
85 *ptr = X25_IFACE_DATA;
87 skb->protocol = x25_type_trans(skb, dev);
93 static void x25_data_transmit(struct net_device *dev, struct sk_buff *skb)
95 hdlc_device *hdlc = dev_to_hdlc(dev);
97 skb_reset_network_header(skb);
98 skb->protocol = hdlc_type_trans(skb, dev);
100 if (dev_nit_active(dev))
101 dev_queue_xmit_nit(skb, dev);
103 hdlc->xmit(skb, dev); /* Ignore return value :-( */
108 static netdev_tx_t x25_xmit(struct sk_buff *skb, struct net_device *dev)
110 hdlc_device *hdlc = dev_to_hdlc(dev);
111 struct x25_state *x25st = state(hdlc);
114 /* There should be a pseudo header of 1 byte added by upper layers.
115 * Check to make sure it is there before reading it.
122 spin_lock_bh(&x25st->up_lock);
124 spin_unlock_bh(&x25st->up_lock);
129 switch (skb->data[0]) {
130 case X25_IFACE_DATA: /* Data to be transmitted */
132 skb_reset_network_header(skb);
133 if ((result = lapb_data_request(dev, skb)) != LAPB_OK)
135 spin_unlock_bh(&x25st->up_lock);
138 case X25_IFACE_CONNECT:
139 if ((result = lapb_connect_request(dev))!= LAPB_OK) {
140 if (result == LAPB_CONNECTED)
141 /* Send connect confirm. msg to level 3 */
142 x25_connected(dev, 0);
144 netdev_err(dev, "LAPB connect request failed, error code = %i\n",
149 case X25_IFACE_DISCONNECT:
150 if ((result = lapb_disconnect_request(dev)) != LAPB_OK) {
151 if (result == LAPB_NOTCONNECTED)
152 /* Send disconnect confirm. msg to level 3 */
153 x25_disconnected(dev, 0);
155 netdev_err(dev, "LAPB disconnect request failed, error code = %i\n",
160 default: /* to be defined */
164 spin_unlock_bh(&x25st->up_lock);
171 static int x25_open(struct net_device *dev)
173 static const struct lapb_register_struct cb = {
174 .connect_confirmation = x25_connected,
175 .connect_indication = x25_connected,
176 .disconnect_confirmation = x25_disconnected,
177 .disconnect_indication = x25_disconnected,
178 .data_indication = x25_data_indication,
179 .data_transmit = x25_data_transmit,
181 hdlc_device *hdlc = dev_to_hdlc(dev);
182 struct x25_state *x25st = state(hdlc);
183 struct lapb_parms_struct params;
186 result = lapb_register(dev, &cb);
187 if (result != LAPB_OK)
190 result = lapb_getparms(dev, ¶ms);
191 if (result != LAPB_OK)
194 if (state(hdlc)->settings.dce)
195 params.mode = params.mode | LAPB_DCE;
197 if (state(hdlc)->settings.modulo == 128)
198 params.mode = params.mode | LAPB_EXTENDED;
200 params.window = state(hdlc)->settings.window;
201 params.t1 = state(hdlc)->settings.t1;
202 params.t2 = state(hdlc)->settings.t2;
203 params.n2 = state(hdlc)->settings.n2;
205 result = lapb_setparms(dev, ¶ms);
206 if (result != LAPB_OK)
209 spin_lock_bh(&x25st->up_lock);
211 spin_unlock_bh(&x25st->up_lock);
218 static void x25_close(struct net_device *dev)
220 hdlc_device *hdlc = dev_to_hdlc(dev);
221 struct x25_state *x25st = state(hdlc);
223 spin_lock_bh(&x25st->up_lock);
225 spin_unlock_bh(&x25st->up_lock);
227 lapb_unregister(dev);
232 static int x25_rx(struct sk_buff *skb)
234 struct net_device *dev = skb->dev;
235 hdlc_device *hdlc = dev_to_hdlc(dev);
236 struct x25_state *x25st = state(hdlc);
238 if ((skb = skb_share_check(skb, GFP_ATOMIC)) == NULL) {
239 dev->stats.rx_dropped++;
243 spin_lock_bh(&x25st->up_lock);
245 spin_unlock_bh(&x25st->up_lock);
247 dev->stats.rx_dropped++;
251 if (lapb_data_received(dev, skb) == LAPB_OK) {
252 spin_unlock_bh(&x25st->up_lock);
253 return NET_RX_SUCCESS;
256 spin_unlock_bh(&x25st->up_lock);
257 dev->stats.rx_errors++;
258 dev_kfree_skb_any(skb);
263 static struct hdlc_proto proto = {
269 .module = THIS_MODULE,
273 static int x25_ioctl(struct net_device *dev, struct ifreq *ifr)
275 x25_hdlc_proto __user *x25_s = ifr->ifr_settings.ifs_ifsu.x25;
276 const size_t size = sizeof(x25_hdlc_proto);
277 hdlc_device *hdlc = dev_to_hdlc(dev);
278 x25_hdlc_proto new_settings;
281 switch (ifr->ifr_settings.type) {
283 if (dev_to_hdlc(dev)->proto != &proto)
285 ifr->ifr_settings.type = IF_PROTO_X25;
286 if (ifr->ifr_settings.size < size) {
287 ifr->ifr_settings.size = size; /* data size wanted */
290 if (copy_to_user(x25_s, &state(hdlc)->settings, size))
295 if (!capable(CAP_NET_ADMIN))
298 if (dev->flags & IFF_UP)
301 /* backward compatibility */
302 if (ifr->ifr_settings.size == 0) {
303 new_settings.dce = 0;
304 new_settings.modulo = 8;
305 new_settings.window = 7;
308 new_settings.n2 = 10;
311 if (copy_from_user(&new_settings, x25_s, size))
314 if ((new_settings.dce != 0 &&
315 new_settings.dce != 1) ||
316 (new_settings.modulo != 8 &&
317 new_settings.modulo != 128) ||
318 new_settings.window < 1 ||
319 (new_settings.modulo == 8 &&
320 new_settings.window > 7) ||
321 (new_settings.modulo == 128 &&
322 new_settings.window > 127) ||
323 new_settings.t1 < 1 ||
324 new_settings.t1 > 255 ||
325 new_settings.t2 < 1 ||
326 new_settings.t2 > 255 ||
327 new_settings.n2 < 1 ||
328 new_settings.n2 > 255)
332 result=hdlc->attach(dev, ENCODING_NRZ,PARITY_CRC16_PR1_CCITT);
336 if ((result = attach_hdlc_protocol(dev, &proto,
337 sizeof(struct x25_state))))
340 memcpy(&state(hdlc)->settings, &new_settings, size);
341 state(hdlc)->up = false;
342 spin_lock_init(&state(hdlc)->up_lock);
344 /* There's no header_ops so hard_header_len should be 0. */
345 dev->hard_header_len = 0;
346 /* When transmitting data:
347 * first we'll remove a pseudo header of 1 byte,
348 * then we'll prepend an LAPB header of at most 3 bytes.
350 dev->needed_headroom = 3 - 1;
352 dev->type = ARPHRD_X25;
353 call_netdevice_notifiers(NETDEV_POST_TYPE_CHANGE, dev);
354 netif_dormant_off(dev);
362 static int __init mod_init(void)
364 register_hdlc_protocol(&proto);
370 static void __exit mod_exit(void)
372 unregister_hdlc_protocol(&proto);
376 module_init(mod_init);
377 module_exit(mod_exit);
379 MODULE_AUTHOR("Krzysztof Halasa <khc@pm.waw.pl>");
380 MODULE_DESCRIPTION("X.25 protocol support for generic HDLC");
381 MODULE_LICENSE("GPL v2");