Merge tag 'block-5.11-2021-01-10' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / net / wan / hdlc_x25.c
blobbb164805804e87e08a2f21c74f66eef371cbe64c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Generic HDLC support routines for Linux
4 * X.25 support
6 * Copyright (C) 1999 - 2006 Krzysztof Halasa <khc@pm.waw.pl>
7 */
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>
24 struct x25_state {
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)
32 return hdlc->state;
35 /* These functions are callbacks called by LAPB layer */
37 static void x25_connect_disconnect(struct net_device *dev, int reason, int code)
39 struct sk_buff *skb;
40 unsigned char *ptr;
42 if ((skb = dev_alloc_skb(1)) == NULL) {
43 netdev_err(dev, "out of memory\n");
44 return;
47 ptr = skb_put(skb, 1);
48 *ptr = code;
50 skb->protocol = x25_type_trans(skb, dev);
51 netif_rx(skb);
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)
72 unsigned char *ptr;
74 if (skb_cow(skb, 1)) {
75 kfree_skb(skb);
76 return NET_RX_DROP;
79 skb_push(skb, 1);
81 ptr = skb->data;
82 *ptr = X25_IFACE_DATA;
84 skb->protocol = x25_type_trans(skb, dev);
85 return netif_rx(skb);
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)
107 int result;
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.
112 if (skb->len < 1) {
113 kfree_skb(skb);
114 return NETDEV_TX_OK;
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;
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);
133 break;
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);
144 break;
146 default: /* to be defined */
147 break;
150 dev_kfree_skb(skb);
151 return NETDEV_TX_OK;
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;
168 int result;
170 result = lapb_register(dev, &cb);
171 if (result != LAPB_OK)
172 return result;
174 result = lapb_getparms(dev, &params);
175 if (result != LAPB_OK)
176 return result;
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, &params);
190 if (result != LAPB_OK)
191 return result;
193 return 0;
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++;
211 return NET_RX_DROP;
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);
219 return NET_RX_DROP;
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,
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;
239 int result;
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;
250 if (copy_to_user(x25_s, &state(hdlc)->settings, size))
251 return -EFAULT;
252 return 0;
254 case IF_PROTO_X25:
255 if (!capable(CAP_NET_ADMIN))
256 return -EPERM;
258 if (dev->flags & IFF_UP)
259 return -EBUSY;
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;
270 else {
271 if (copy_from_user(&new_settings, x25_s, size))
272 return -EFAULT;
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;
292 result=hdlc->attach(dev, ENCODING_NRZ,PARITY_CRC16_PR1_CCITT);
293 if (result)
294 return result;
296 if ((result = attach_hdlc_protocol(dev, &proto,
297 sizeof(struct x25_state))))
298 return result;
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);
313 return 0;
316 return -EINVAL;
320 static int __init mod_init(void)
322 register_hdlc_protocol(&proto);
323 return 0;
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");