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
)
78 skb_reset_network_header(skb
);
81 *ptr
= X25_IFACE_DATA
;
83 skb
->protocol
= x25_type_trans(skb
, dev
);
89 static void x25_data_transmit(struct net_device
*dev
, struct sk_buff
*skb
)
91 hdlc_device
*hdlc
= dev_to_hdlc(dev
);
93 skb_reset_network_header(skb
);
94 skb
->protocol
= hdlc_type_trans(skb
, dev
);
96 if (dev_nit_active(dev
))
97 dev_queue_xmit_nit(skb
, dev
);
99 hdlc
->xmit(skb
, dev
); /* Ignore return value :-( */
104 static netdev_tx_t
x25_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
110 switch (skb
->data
[0]) {
111 case X25_IFACE_DATA
: /* Data to be transmitted */
113 skb_reset_network_header(skb
);
114 if ((result
= lapb_data_request(dev
, skb
)) != LAPB_OK
)
118 case X25_IFACE_CONNECT
:
119 if ((result
= lapb_connect_request(dev
))!= LAPB_OK
) {
120 if (result
== LAPB_CONNECTED
)
121 /* Send connect confirm. msg to level 3 */
122 x25_connected(dev
, 0);
124 netdev_err(dev
, "LAPB connect request failed, error code = %i\n",
129 case X25_IFACE_DISCONNECT
:
130 if ((result
= lapb_disconnect_request(dev
)) != LAPB_OK
) {
131 if (result
== LAPB_NOTCONNECTED
)
132 /* Send disconnect confirm. msg to level 3 */
133 x25_disconnected(dev
, 0);
135 netdev_err(dev
, "LAPB disconnect request failed, error code = %i\n",
140 default: /* to be defined */
150 static int x25_open(struct net_device
*dev
)
152 static const struct lapb_register_struct cb
= {
153 .connect_confirmation
= x25_connected
,
154 .connect_indication
= x25_connected
,
155 .disconnect_confirmation
= x25_disconnected
,
156 .disconnect_indication
= x25_disconnected
,
157 .data_indication
= x25_data_indication
,
158 .data_transmit
= x25_data_transmit
,
160 hdlc_device
*hdlc
= dev_to_hdlc(dev
);
161 struct lapb_parms_struct params
;
164 result
= lapb_register(dev
, &cb
);
165 if (result
!= LAPB_OK
)
168 result
= lapb_getparms(dev
, ¶ms
);
169 if (result
!= LAPB_OK
)
172 if (state(hdlc
)->settings
.dce
)
173 params
.mode
= params
.mode
| LAPB_DCE
;
175 if (state(hdlc
)->settings
.modulo
== 128)
176 params
.mode
= params
.mode
| LAPB_EXTENDED
;
178 params
.window
= state(hdlc
)->settings
.window
;
179 params
.t1
= state(hdlc
)->settings
.t1
;
180 params
.t2
= state(hdlc
)->settings
.t2
;
181 params
.n2
= state(hdlc
)->settings
.n2
;
183 result
= lapb_setparms(dev
, ¶ms
);
184 if (result
!= LAPB_OK
)
192 static void x25_close(struct net_device
*dev
)
194 lapb_unregister(dev
);
199 static int x25_rx(struct sk_buff
*skb
)
201 struct net_device
*dev
= skb
->dev
;
203 if ((skb
= skb_share_check(skb
, GFP_ATOMIC
)) == NULL
) {
204 dev
->stats
.rx_dropped
++;
208 if (lapb_data_received(dev
, skb
) == LAPB_OK
)
209 return NET_RX_SUCCESS
;
211 dev
->stats
.rx_errors
++;
212 dev_kfree_skb_any(skb
);
217 static struct hdlc_proto proto
= {
223 .module
= THIS_MODULE
,
227 static int x25_ioctl(struct net_device
*dev
, struct ifreq
*ifr
)
229 x25_hdlc_proto __user
*x25_s
= ifr
->ifr_settings
.ifs_ifsu
.x25
;
230 const size_t size
= sizeof(x25_hdlc_proto
);
231 hdlc_device
*hdlc
= dev_to_hdlc(dev
);
232 x25_hdlc_proto new_settings
;
235 switch (ifr
->ifr_settings
.type
) {
237 if (dev_to_hdlc(dev
)->proto
!= &proto
)
239 ifr
->ifr_settings
.type
= IF_PROTO_X25
;
240 if (ifr
->ifr_settings
.size
< size
) {
241 ifr
->ifr_settings
.size
= size
; /* data size wanted */
244 if (copy_to_user(x25_s
, &state(hdlc
)->settings
, size
))
249 if (!capable(CAP_NET_ADMIN
))
252 if (dev
->flags
& IFF_UP
)
255 /* backward compatibility */
256 if (ifr
->ifr_settings
.size
== 0) {
257 new_settings
.dce
= 0;
258 new_settings
.modulo
= 8;
259 new_settings
.window
= 7;
262 new_settings
.n2
= 10;
265 if (copy_from_user(&new_settings
, x25_s
, size
))
268 if ((new_settings
.dce
!= 0 &&
269 new_settings
.dce
!= 1) ||
270 (new_settings
.modulo
!= 8 &&
271 new_settings
.modulo
!= 128) ||
272 new_settings
.window
< 1 ||
273 (new_settings
.modulo
== 8 &&
274 new_settings
.window
> 7) ||
275 (new_settings
.modulo
== 128 &&
276 new_settings
.window
> 127) ||
277 new_settings
.t1
< 1 ||
278 new_settings
.t1
> 255 ||
279 new_settings
.t2
< 1 ||
280 new_settings
.t2
> 255 ||
281 new_settings
.n2
< 1 ||
282 new_settings
.n2
> 255)
286 result
=hdlc
->attach(dev
, ENCODING_NRZ
,PARITY_CRC16_PR1_CCITT
);
290 if ((result
= attach_hdlc_protocol(dev
, &proto
,
291 sizeof(struct x25_state
))))
294 memcpy(&state(hdlc
)->settings
, &new_settings
, size
);
295 dev
->type
= ARPHRD_X25
;
296 call_netdevice_notifiers(NETDEV_POST_TYPE_CHANGE
, dev
);
297 netif_dormant_off(dev
);
305 static int __init
mod_init(void)
307 register_hdlc_protocol(&proto
);
313 static void __exit
mod_exit(void)
315 unregister_hdlc_protocol(&proto
);
319 module_init(mod_init
);
320 module_exit(mod_exit
);
322 MODULE_AUTHOR("Krzysztof Halasa <khc@pm.waw.pl>");
323 MODULE_DESCRIPTION("X.25 protocol support for generic HDLC");
324 MODULE_LICENSE("GPL v2");