treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / net / wan / hdlc_x25.c
blobc84536b03aa84e5cce1637238ce065512dc7b390
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 return NET_RX_DROP;
77 skb_push(skb, 1);
78 skb_reset_network_header(skb);
80 ptr = skb->data;
81 *ptr = X25_IFACE_DATA;
83 skb->protocol = x25_type_trans(skb, dev);
84 return netif_rx(skb);
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)
106 int result;
109 /* X.25 to LAPB */
110 switch (skb->data[0]) {
111 case X25_IFACE_DATA: /* Data to be transmitted */
112 skb_pull(skb, 1);
113 skb_reset_network_header(skb);
114 if ((result = lapb_data_request(dev, skb)) != LAPB_OK)
115 dev_kfree_skb(skb);
116 return NETDEV_TX_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);
123 else
124 netdev_err(dev, "LAPB connect request failed, error code = %i\n",
125 result);
127 break;
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);
134 else
135 netdev_err(dev, "LAPB disconnect request failed, error code = %i\n",
136 result);
138 break;
140 default: /* to be defined */
141 break;
144 dev_kfree_skb(skb);
145 return NETDEV_TX_OK;
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;
162 int result;
164 result = lapb_register(dev, &cb);
165 if (result != LAPB_OK)
166 return result;
168 result = lapb_getparms(dev, &params);
169 if (result != LAPB_OK)
170 return result;
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, &params);
184 if (result != LAPB_OK)
185 return result;
187 return 0;
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++;
205 return NET_RX_DROP;
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);
213 return NET_RX_DROP;
217 static struct hdlc_proto proto = {
218 .open = x25_open,
219 .close = x25_close,
220 .ioctl = x25_ioctl,
221 .netif_rx = x25_rx,
222 .xmit = x25_xmit,
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;
233 int result;
235 switch (ifr->ifr_settings.type) {
236 case IF_GET_PROTO:
237 if (dev_to_hdlc(dev)->proto != &proto)
238 return -EINVAL;
239 ifr->ifr_settings.type = IF_PROTO_X25;
240 if (ifr->ifr_settings.size < size) {
241 ifr->ifr_settings.size = size; /* data size wanted */
242 return -ENOBUFS;
244 if (copy_to_user(x25_s, &state(hdlc)->settings, size))
245 return -EFAULT;
246 return 0;
248 case IF_PROTO_X25:
249 if (!capable(CAP_NET_ADMIN))
250 return -EPERM;
252 if (dev->flags & IFF_UP)
253 return -EBUSY;
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;
260 new_settings.t1 = 3;
261 new_settings.t2 = 1;
262 new_settings.n2 = 10;
264 else {
265 if (copy_from_user(&new_settings, x25_s, size))
266 return -EFAULT;
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)
283 return -EINVAL;
286 result=hdlc->attach(dev, ENCODING_NRZ,PARITY_CRC16_PR1_CCITT);
287 if (result)
288 return result;
290 if ((result = attach_hdlc_protocol(dev, &proto,
291 sizeof(struct x25_state))))
292 return result;
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);
298 return 0;
301 return -EINVAL;
305 static int __init mod_init(void)
307 register_hdlc_protocol(&proto);
308 return 0;
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");