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
;
28 static int x25_ioctl(struct net_device
*dev
, struct ifreq
*ifr
);
30 static struct x25_state
*state(hdlc_device
*hdlc
)
35 /* These functions are callbacks called by LAPB layer */
37 static void x25_connect_disconnect(struct net_device
*dev
, int reason
, int code
)
42 if ((skb
= dev_alloc_skb(1)) == NULL
) {
43 netdev_err(dev
, "out of memory\n");
47 ptr
= skb_put(skb
, 1);
50 skb
->protocol
= x25_type_trans(skb
, dev
);
56 static void x25_connected(struct net_device
*dev
, int reason
)
58 x25_connect_disconnect(dev
, reason
, X25_IFACE_CONNECT
);
63 static void x25_disconnected(struct net_device
*dev
, int reason
)
65 x25_connect_disconnect(dev
, reason
, X25_IFACE_DISCONNECT
);
70 static int x25_data_indication(struct net_device
*dev
, struct sk_buff
*skb
)
74 if (skb_cow(skb
, 1)) {
82 *ptr
= X25_IFACE_DATA
;
84 skb
->protocol
= x25_type_trans(skb
, dev
);
90 static void x25_data_transmit(struct net_device
*dev
, struct sk_buff
*skb
)
92 hdlc_device
*hdlc
= dev_to_hdlc(dev
);
94 skb_reset_network_header(skb
);
95 skb
->protocol
= hdlc_type_trans(skb
, dev
);
97 if (dev_nit_active(dev
))
98 dev_queue_xmit_nit(skb
, dev
);
100 hdlc
->xmit(skb
, dev
); /* Ignore return value :-( */
105 static netdev_tx_t
x25_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
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.
117 switch (skb
->data
[0]) {
118 case X25_IFACE_DATA
: /* Data to be transmitted */
120 if ((result
= lapb_data_request(dev
, skb
)) != LAPB_OK
)
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);
130 netdev_err(dev
, "LAPB connect request failed, error code = %i\n",
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);
141 netdev_err(dev
, "LAPB disconnect request failed, error code = %i\n",
146 default: /* to be defined */
156 static int x25_open(struct net_device
*dev
)
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
,
166 hdlc_device
*hdlc
= dev_to_hdlc(dev
);
167 struct lapb_parms_struct params
;
170 result
= lapb_register(dev
, &cb
);
171 if (result
!= LAPB_OK
)
174 result
= lapb_getparms(dev
, ¶ms
);
175 if (result
!= LAPB_OK
)
178 if (state(hdlc
)->settings
.dce
)
179 params
.mode
= params
.mode
| LAPB_DCE
;
181 if (state(hdlc
)->settings
.modulo
== 128)
182 params
.mode
= params
.mode
| LAPB_EXTENDED
;
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
;
189 result
= lapb_setparms(dev
, ¶ms
);
190 if (result
!= LAPB_OK
)
198 static void x25_close(struct net_device
*dev
)
200 lapb_unregister(dev
);
205 static int x25_rx(struct sk_buff
*skb
)
207 struct net_device
*dev
= skb
->dev
;
209 if ((skb
= skb_share_check(skb
, GFP_ATOMIC
)) == NULL
) {
210 dev
->stats
.rx_dropped
++;
214 if (lapb_data_received(dev
, skb
) == LAPB_OK
)
215 return NET_RX_SUCCESS
;
217 dev
->stats
.rx_errors
++;
218 dev_kfree_skb_any(skb
);
223 static struct hdlc_proto proto
= {
229 .module
= THIS_MODULE
,
233 static int x25_ioctl(struct net_device
*dev
, struct ifreq
*ifr
)
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
;
241 switch (ifr
->ifr_settings
.type
) {
243 if (dev_to_hdlc(dev
)->proto
!= &proto
)
245 ifr
->ifr_settings
.type
= IF_PROTO_X25
;
246 if (ifr
->ifr_settings
.size
< size
) {
247 ifr
->ifr_settings
.size
= size
; /* data size wanted */
250 if (copy_to_user(x25_s
, &state(hdlc
)->settings
, size
))
255 if (!capable(CAP_NET_ADMIN
))
258 if (dev
->flags
& IFF_UP
)
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;
268 new_settings
.n2
= 10;
271 if (copy_from_user(&new_settings
, x25_s
, size
))
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)
292 result
=hdlc
->attach(dev
, ENCODING_NRZ
,PARITY_CRC16_PR1_CCITT
);
296 if ((result
= attach_hdlc_protocol(dev
, &proto
,
297 sizeof(struct x25_state
))))
300 memcpy(&state(hdlc
)->settings
, &new_settings
, size
);
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.
308 dev
->needed_headroom
= 3 - 1;
310 dev
->type
= ARPHRD_X25
;
311 call_netdevice_notifiers(NETDEV_POST_TYPE_CHANGE
, dev
);
312 netif_dormant_off(dev
);
320 static int __init
mod_init(void)
322 register_hdlc_protocol(&proto
);
328 static void __exit
mod_exit(void)
330 unregister_hdlc_protocol(&proto
);
334 module_init(mod_init
);
335 module_exit(mod_exit
);
337 MODULE_AUTHOR("Krzysztof Halasa <khc@pm.waw.pl>");
338 MODULE_DESCRIPTION("X.25 protocol support for generic HDLC");
339 MODULE_LICENSE("GPL v2");