treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / net / wan / dlci.c
blob7bcee41905cfb4cf02e7e436b75be1900de74e32
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * DLCI Implementation of Frame Relay protocol for Linux, according to
4 * RFC 1490. This generic device provides en/decapsulation for an
5 * underlying hardware driver. Routes & IPs are assigned to these
6 * interfaces. Requires 'dlcicfg' program to create usable
7 * interfaces, the initial one, 'dlci' is for IOCTL use only.
9 * Version: @(#)dlci.c 0.35 4 Jan 1997
11 * Author: Mike McLagan <mike.mclagan@linux.org>
13 * Changes:
15 * 0.15 Mike Mclagan Packet freeing, bug in kmalloc call
16 * DLCI_RET handling
17 * 0.20 Mike McLagan More conservative on which packets
18 * are returned for retry and which are
19 * are dropped. If DLCI_RET_DROP is
20 * returned from the FRAD, the packet is
21 * sent back to Linux for re-transmission
22 * 0.25 Mike McLagan Converted to use SIOC IOCTL calls
23 * 0.30 Jim Freeman Fixed to allow IPX traffic
24 * 0.35 Michael Elizabeth Fixed incorrect memcpy_fromfs
27 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
29 #include <linux/module.h>
30 #include <linux/kernel.h>
31 #include <linux/types.h>
32 #include <linux/fcntl.h>
33 #include <linux/interrupt.h>
34 #include <linux/ptrace.h>
35 #include <linux/ioport.h>
36 #include <linux/in.h>
37 #include <linux/init.h>
38 #include <linux/slab.h>
39 #include <linux/string.h>
40 #include <linux/errno.h>
41 #include <linux/netdevice.h>
42 #include <linux/skbuff.h>
43 #include <linux/if_arp.h>
44 #include <linux/if_frad.h>
45 #include <linux/bitops.h>
47 #include <net/sock.h>
49 #include <asm/io.h>
50 #include <asm/dma.h>
51 #include <linux/uaccess.h>
53 static const char version[] = "DLCI driver v0.35, 4 Jan 1997, mike.mclagan@linux.org";
55 static LIST_HEAD(dlci_devs);
57 static void dlci_setup(struct net_device *);
59 /*
60 * these encapsulate the RFC 1490 requirements as well as
61 * deal with packet transmission and reception, working with
62 * the upper network layers
65 static int dlci_header(struct sk_buff *skb, struct net_device *dev,
66 unsigned short type, const void *daddr,
67 const void *saddr, unsigned len)
69 struct frhdr hdr;
70 unsigned int hlen;
71 char *dest;
73 hdr.control = FRAD_I_UI;
74 switch (type)
76 case ETH_P_IP:
77 hdr.IP_NLPID = FRAD_P_IP;
78 hlen = sizeof(hdr.control) + sizeof(hdr.IP_NLPID);
79 break;
81 /* feel free to add other types, if necessary */
83 default:
84 hdr.pad = FRAD_P_PADDING;
85 hdr.NLPID = FRAD_P_SNAP;
86 memset(hdr.OUI, 0, sizeof(hdr.OUI));
87 hdr.PID = htons(type);
88 hlen = sizeof(hdr);
89 break;
92 dest = skb_push(skb, hlen);
93 if (!dest)
94 return 0;
96 memcpy(dest, &hdr, hlen);
98 return hlen;
101 static void dlci_receive(struct sk_buff *skb, struct net_device *dev)
103 struct frhdr *hdr;
104 int process, header;
106 if (!pskb_may_pull(skb, sizeof(*hdr))) {
107 netdev_notice(dev, "invalid data no header\n");
108 dev->stats.rx_errors++;
109 kfree_skb(skb);
110 return;
113 hdr = (struct frhdr *) skb->data;
114 process = 0;
115 header = 0;
116 skb->dev = dev;
118 if (hdr->control != FRAD_I_UI)
120 netdev_notice(dev, "Invalid header flag 0x%02X\n",
121 hdr->control);
122 dev->stats.rx_errors++;
124 else
125 switch (hdr->IP_NLPID)
127 case FRAD_P_PADDING:
128 if (hdr->NLPID != FRAD_P_SNAP)
130 netdev_notice(dev, "Unsupported NLPID 0x%02X\n",
131 hdr->NLPID);
132 dev->stats.rx_errors++;
133 break;
136 if (hdr->OUI[0] + hdr->OUI[1] + hdr->OUI[2] != 0)
138 netdev_notice(dev, "Unsupported organizationally unique identifier 0x%02X-%02X-%02X\n",
139 hdr->OUI[0],
140 hdr->OUI[1],
141 hdr->OUI[2]);
142 dev->stats.rx_errors++;
143 break;
146 /* at this point, it's an EtherType frame */
147 header = sizeof(struct frhdr);
148 /* Already in network order ! */
149 skb->protocol = hdr->PID;
150 process = 1;
151 break;
153 case FRAD_P_IP:
154 header = sizeof(hdr->control) + sizeof(hdr->IP_NLPID);
155 skb->protocol = htons(ETH_P_IP);
156 process = 1;
157 break;
159 case FRAD_P_SNAP:
160 case FRAD_P_Q933:
161 case FRAD_P_CLNP:
162 netdev_notice(dev, "Unsupported NLPID 0x%02X\n",
163 hdr->pad);
164 dev->stats.rx_errors++;
165 break;
167 default:
168 netdev_notice(dev, "Invalid pad byte 0x%02X\n",
169 hdr->pad);
170 dev->stats.rx_errors++;
171 break;
174 if (process)
176 /* we've set up the protocol, so discard the header */
177 skb_reset_mac_header(skb);
178 skb_pull(skb, header);
179 dev->stats.rx_bytes += skb->len;
180 netif_rx(skb);
181 dev->stats.rx_packets++;
183 else
184 dev_kfree_skb(skb);
187 static netdev_tx_t dlci_transmit(struct sk_buff *skb, struct net_device *dev)
189 struct dlci_local *dlp = netdev_priv(dev);
191 if (skb) {
192 struct netdev_queue *txq = skb_get_tx_queue(dev, skb);
193 netdev_start_xmit(skb, dlp->slave, txq, false);
195 return NETDEV_TX_OK;
198 static int dlci_config(struct net_device *dev, struct dlci_conf __user *conf, int get)
200 struct dlci_conf config;
201 struct dlci_local *dlp;
202 struct frad_local *flp;
203 int err;
205 dlp = netdev_priv(dev);
207 flp = netdev_priv(dlp->slave);
209 if (!get)
211 if (copy_from_user(&config, conf, sizeof(struct dlci_conf)))
212 return -EFAULT;
213 if (config.flags & ~DLCI_VALID_FLAGS)
214 return -EINVAL;
215 memcpy(&dlp->config, &config, sizeof(struct dlci_conf));
216 dlp->configured = 1;
219 err = (*flp->dlci_conf)(dlp->slave, dev, get);
220 if (err)
221 return err;
223 if (get)
225 if (copy_to_user(conf, &dlp->config, sizeof(struct dlci_conf)))
226 return -EFAULT;
229 return 0;
232 static int dlci_dev_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
234 struct dlci_local *dlp;
236 if (!capable(CAP_NET_ADMIN))
237 return -EPERM;
239 dlp = netdev_priv(dev);
241 switch (cmd)
243 case DLCI_GET_SLAVE:
244 if (!*(short *)(dev->dev_addr))
245 return -EINVAL;
247 strncpy(ifr->ifr_slave, dlp->slave->name, sizeof(ifr->ifr_slave));
248 break;
250 case DLCI_GET_CONF:
251 case DLCI_SET_CONF:
252 if (!*(short *)(dev->dev_addr))
253 return -EINVAL;
255 return dlci_config(dev, ifr->ifr_data, cmd == DLCI_GET_CONF);
257 default:
258 return -EOPNOTSUPP;
260 return 0;
263 static int dlci_change_mtu(struct net_device *dev, int new_mtu)
265 struct dlci_local *dlp = netdev_priv(dev);
267 return dev_set_mtu(dlp->slave, new_mtu);
270 static int dlci_open(struct net_device *dev)
272 struct dlci_local *dlp;
273 struct frad_local *flp;
274 int err;
276 dlp = netdev_priv(dev);
278 if (!*(short *)(dev->dev_addr))
279 return -EINVAL;
281 if (!netif_running(dlp->slave))
282 return -ENOTCONN;
284 flp = netdev_priv(dlp->slave);
285 err = (*flp->activate)(dlp->slave, dev);
286 if (err)
287 return err;
289 netif_start_queue(dev);
291 return 0;
294 static int dlci_close(struct net_device *dev)
296 struct dlci_local *dlp;
297 struct frad_local *flp;
298 int err;
300 netif_stop_queue(dev);
302 dlp = netdev_priv(dev);
304 flp = netdev_priv(dlp->slave);
305 err = (*flp->deactivate)(dlp->slave, dev);
307 return 0;
310 static int dlci_add(struct dlci_add *dlci)
312 struct net_device *master, *slave;
313 struct dlci_local *dlp;
314 struct frad_local *flp;
315 int err = -EINVAL;
318 /* validate slave device */
319 slave = dev_get_by_name(&init_net, dlci->devname);
320 if (!slave)
321 return -ENODEV;
323 if (slave->type != ARPHRD_FRAD || netdev_priv(slave) == NULL)
324 goto err1;
326 /* create device name */
327 master = alloc_netdev(sizeof(struct dlci_local), "dlci%d",
328 NET_NAME_UNKNOWN, dlci_setup);
329 if (!master) {
330 err = -ENOMEM;
331 goto err1;
334 /* make sure same slave not already registered */
335 rtnl_lock();
336 list_for_each_entry(dlp, &dlci_devs, list) {
337 if (dlp->slave == slave) {
338 err = -EBUSY;
339 goto err2;
343 *(short *)(master->dev_addr) = dlci->dlci;
345 dlp = netdev_priv(master);
346 dlp->slave = slave;
347 dlp->master = master;
349 flp = netdev_priv(slave);
350 err = (*flp->assoc)(slave, master);
351 if (err < 0)
352 goto err2;
354 err = register_netdevice(master);
355 if (err < 0)
356 goto err2;
358 strcpy(dlci->devname, master->name);
360 list_add(&dlp->list, &dlci_devs);
361 rtnl_unlock();
363 return 0;
365 err2:
366 rtnl_unlock();
367 free_netdev(master);
368 err1:
369 dev_put(slave);
370 return err;
373 static int dlci_del(struct dlci_add *dlci)
375 struct dlci_local *dlp;
376 struct frad_local *flp;
377 struct net_device *master, *slave;
378 int err;
379 bool found = false;
381 rtnl_lock();
383 /* validate slave device */
384 master = __dev_get_by_name(&init_net, dlci->devname);
385 if (!master) {
386 err = -ENODEV;
387 goto out;
390 list_for_each_entry(dlp, &dlci_devs, list) {
391 if (dlp->master == master) {
392 found = true;
393 break;
396 if (!found) {
397 err = -ENODEV;
398 goto out;
401 if (netif_running(master)) {
402 err = -EBUSY;
403 goto out;
406 dlp = netdev_priv(master);
407 slave = dlp->slave;
408 flp = netdev_priv(slave);
410 err = (*flp->deassoc)(slave, master);
411 if (!err) {
412 list_del(&dlp->list);
414 unregister_netdevice(master);
416 dev_put(slave);
418 out:
419 rtnl_unlock();
420 return err;
423 static int dlci_ioctl(unsigned int cmd, void __user *arg)
425 struct dlci_add add;
426 int err;
428 if (!capable(CAP_NET_ADMIN))
429 return -EPERM;
431 if (copy_from_user(&add, arg, sizeof(struct dlci_add)))
432 return -EFAULT;
434 switch (cmd)
436 case SIOCADDDLCI:
437 err = dlci_add(&add);
439 if (!err)
440 if (copy_to_user(arg, &add, sizeof(struct dlci_add)))
441 return -EFAULT;
442 break;
444 case SIOCDELDLCI:
445 err = dlci_del(&add);
446 break;
448 default:
449 err = -EINVAL;
452 return err;
455 static const struct header_ops dlci_header_ops = {
456 .create = dlci_header,
459 static const struct net_device_ops dlci_netdev_ops = {
460 .ndo_open = dlci_open,
461 .ndo_stop = dlci_close,
462 .ndo_do_ioctl = dlci_dev_ioctl,
463 .ndo_start_xmit = dlci_transmit,
464 .ndo_change_mtu = dlci_change_mtu,
467 static void dlci_setup(struct net_device *dev)
469 struct dlci_local *dlp = netdev_priv(dev);
471 dev->flags = 0;
472 dev->header_ops = &dlci_header_ops;
473 dev->netdev_ops = &dlci_netdev_ops;
474 dev->needs_free_netdev = true;
476 dlp->receive = dlci_receive;
478 dev->type = ARPHRD_DLCI;
479 dev->hard_header_len = sizeof(struct frhdr);
480 dev->addr_len = sizeof(short);
484 /* if slave is unregistering, then cleanup master */
485 static int dlci_dev_event(struct notifier_block *unused,
486 unsigned long event, void *ptr)
488 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
490 if (dev_net(dev) != &init_net)
491 return NOTIFY_DONE;
493 if (event == NETDEV_UNREGISTER) {
494 struct dlci_local *dlp;
496 list_for_each_entry(dlp, &dlci_devs, list) {
497 if (dlp->slave == dev) {
498 list_del(&dlp->list);
499 unregister_netdevice(dlp->master);
500 dev_put(dlp->slave);
501 break;
505 return NOTIFY_DONE;
508 static struct notifier_block dlci_notifier = {
509 .notifier_call = dlci_dev_event,
512 static int __init init_dlci(void)
514 dlci_ioctl_set(dlci_ioctl);
515 register_netdevice_notifier(&dlci_notifier);
517 printk("%s.\n", version);
519 return 0;
522 static void __exit dlci_exit(void)
524 struct dlci_local *dlp, *nxt;
526 dlci_ioctl_set(NULL);
527 unregister_netdevice_notifier(&dlci_notifier);
529 rtnl_lock();
530 list_for_each_entry_safe(dlp, nxt, &dlci_devs, list) {
531 unregister_netdevice(dlp->master);
532 dev_put(dlp->slave);
534 rtnl_unlock();
537 module_init(init_dlci);
538 module_exit(dlci_exit);
540 MODULE_AUTHOR("Mike McLagan");
541 MODULE_DESCRIPTION("Frame Relay DLCI layer");
542 MODULE_LICENSE("GPL");