blob: 69d269d32b5b71df1c702b382a6af530ae2e0d08 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * DLCI Implementation of Frame Relay protocol for Linux, according to
3 * RFC 1490. This generic device provides en/decapsulation for an
4 * underlying hardware driver. Routes & IPs are assigned to these
5 * interfaces. Requires 'dlcicfg' program to create usable
6 * interfaces, the initial one, 'dlci' is for IOCTL use only.
7 *
8 * Version: @(#)dlci.c 0.35 4 Jan 1997
9 *
10 * Author: Mike McLagan <mike.mclagan@linux.org>
11 *
12 * Changes:
13 *
14 * 0.15 Mike Mclagan Packet freeing, bug in kmalloc call
15 * DLCI_RET handling
16 * 0.20 Mike McLagan More conservative on which packets
17 * are returned for retry and which are
18 * are dropped. If DLCI_RET_DROP is
19 * returned from the FRAD, the packet is
20 * sent back to Linux for re-transmission
21 * 0.25 Mike McLagan Converted to use SIOC IOCTL calls
22 * 0.30 Jim Freeman Fixed to allow IPX traffic
23 * 0.35 Michael Elizabeth Fixed incorrect memcpy_fromfs
24 *
25 * This program is free software; you can redistribute it and/or
26 * modify it under the terms of the GNU General Public License
27 * as published by the Free Software Foundation; either version
28 * 2 of the License, or (at your option) any later version.
29 */
30
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/module.h>
32#include <linux/kernel.h>
33#include <linux/types.h>
34#include <linux/fcntl.h>
35#include <linux/interrupt.h>
36#include <linux/ptrace.h>
37#include <linux/ioport.h>
38#include <linux/in.h>
39#include <linux/init.h>
40#include <linux/slab.h>
41#include <linux/string.h>
42#include <linux/errno.h>
43#include <linux/netdevice.h>
44#include <linux/skbuff.h>
45#include <linux/if_arp.h>
46#include <linux/if_frad.h>
47#include <linux/bitops.h>
48
49#include <net/sock.h>
50
51#include <asm/system.h>
52#include <asm/io.h>
53#include <asm/dma.h>
54#include <asm/uaccess.h>
55
56static const char version[] = "DLCI driver v0.35, 4 Jan 1997, mike.mclagan@linux.org";
57
58static LIST_HEAD(dlci_devs);
59
60static void dlci_setup(struct net_device *);
61
62/*
63 * these encapsulate the RFC 1490 requirements as well as
64 * deal with packet transmission and reception, working with
65 * the upper network layers
66 */
67
68static int dlci_header(struct sk_buff *skb, struct net_device *dev,
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -070069 unsigned short type, const void *daddr,
70 const void *saddr, unsigned len)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071{
72 struct frhdr hdr;
73 struct dlci_local *dlp;
74 unsigned int hlen;
75 char *dest;
76
Wang Chen8f15ea42008-11-12 23:38:36 -080077 dlp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
79 hdr.control = FRAD_I_UI;
80 switch(type)
81 {
82 case ETH_P_IP:
83 hdr.IP_NLPID = FRAD_P_IP;
84 hlen = sizeof(hdr.control) + sizeof(hdr.IP_NLPID);
85 break;
86
87 /* feel free to add other types, if necessary */
88
89 default:
90 hdr.pad = FRAD_P_PADDING;
91 hdr.NLPID = FRAD_P_SNAP;
92 memset(hdr.OUI, 0, sizeof(hdr.OUI));
93 hdr.PID = htons(type);
94 hlen = sizeof(hdr);
95 break;
96 }
97
98 dest = skb_push(skb, hlen);
99 if (!dest)
100 return(0);
101
102 memcpy(dest, &hdr, hlen);
103
104 return(hlen);
105}
106
107static void dlci_receive(struct sk_buff *skb, struct net_device *dev)
108{
109 struct dlci_local *dlp;
110 struct frhdr *hdr;
111 int process, header;
112
Wang Chen8f15ea42008-11-12 23:38:36 -0800113 dlp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 if (!pskb_may_pull(skb, sizeof(*hdr))) {
115 printk(KERN_NOTICE "%s: invalid data no header\n",
116 dev->name);
Stephen Hemminger07d117c2009-03-20 19:36:14 +0000117 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 kfree_skb(skb);
119 return;
120 }
121
122 hdr = (struct frhdr *) skb->data;
123 process = 0;
124 header = 0;
125 skb->dev = dev;
126
127 if (hdr->control != FRAD_I_UI)
128 {
129 printk(KERN_NOTICE "%s: Invalid header flag 0x%02X.\n", dev->name, hdr->control);
Stephen Hemminger07d117c2009-03-20 19:36:14 +0000130 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 }
132 else
133 switch(hdr->IP_NLPID)
134 {
135 case FRAD_P_PADDING:
136 if (hdr->NLPID != FRAD_P_SNAP)
137 {
138 printk(KERN_NOTICE "%s: Unsupported NLPID 0x%02X.\n", dev->name, hdr->NLPID);
Stephen Hemminger07d117c2009-03-20 19:36:14 +0000139 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 break;
141 }
142
143 if (hdr->OUI[0] + hdr->OUI[1] + hdr->OUI[2] != 0)
144 {
145 printk(KERN_NOTICE "%s: Unsupported organizationally unique identifier 0x%02X-%02X-%02X.\n", dev->name, hdr->OUI[0], hdr->OUI[1], hdr->OUI[2]);
Stephen Hemminger07d117c2009-03-20 19:36:14 +0000146 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 break;
148 }
149
150 /* at this point, it's an EtherType frame */
151 header = sizeof(struct frhdr);
152 /* Already in network order ! */
153 skb->protocol = hdr->PID;
154 process = 1;
155 break;
156
157 case FRAD_P_IP:
158 header = sizeof(hdr->control) + sizeof(hdr->IP_NLPID);
159 skb->protocol = htons(ETH_P_IP);
160 process = 1;
161 break;
162
163 case FRAD_P_SNAP:
164 case FRAD_P_Q933:
165 case FRAD_P_CLNP:
166 printk(KERN_NOTICE "%s: Unsupported NLPID 0x%02X.\n", dev->name, hdr->pad);
Stephen Hemminger07d117c2009-03-20 19:36:14 +0000167 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 break;
169
170 default:
171 printk(KERN_NOTICE "%s: Invalid pad byte 0x%02X.\n", dev->name, hdr->pad);
Stephen Hemminger07d117c2009-03-20 19:36:14 +0000172 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 break;
174 }
175
176 if (process)
177 {
178 /* we've set up the protocol, so discard the header */
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -0700179 skb_reset_mac_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 skb_pull(skb, header);
Stephen Hemminger07d117c2009-03-20 19:36:14 +0000181 dev->stats.rx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 netif_rx(skb);
Stephen Hemminger07d117c2009-03-20 19:36:14 +0000183 dev->stats.rx_packets++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 }
185 else
186 dev_kfree_skb(skb);
187}
188
Stephen Hemmingerd71a6742009-08-31 19:50:47 +0000189static netdev_tx_t dlci_transmit(struct sk_buff *skb,
190 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191{
192 struct dlci_local *dlp;
Stephen Hemmingerd71a6742009-08-31 19:50:47 +0000193 netdev_tx_t ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
195 if (!skb || !dev)
Patrick McHardyec634fe2009-07-05 19:23:38 -0700196 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
Wang Chen8f15ea42008-11-12 23:38:36 -0800198 dlp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
200 netif_stop_queue(dev);
201
Stephen Hemmingerd71a6742009-08-31 19:50:47 +0000202 /* This is hackish, overloads driver specific return values
203 on top of normal transmit return! */
Stephen Hemminger49e8aba2009-03-20 19:36:15 +0000204 ret = dlp->slave->netdev_ops->ndo_start_xmit(skb, dlp->slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 switch (ret)
206 {
207 case DLCI_RET_OK:
Stephen Hemminger07d117c2009-03-20 19:36:14 +0000208 dev->stats.tx_packets++;
Patrick McHardy5b548142009-06-12 06:22:29 +0000209 ret = NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 break;
Stephen Hemmingerd71a6742009-08-31 19:50:47 +0000211 case DLCI_RET_ERR:
Stephen Hemminger07d117c2009-03-20 19:36:14 +0000212 dev->stats.tx_errors++;
Patrick McHardy5b548142009-06-12 06:22:29 +0000213 ret = NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 break;
Stephen Hemmingerd71a6742009-08-31 19:50:47 +0000215 case DLCI_RET_DROP:
Stephen Hemminger07d117c2009-03-20 19:36:14 +0000216 dev->stats.tx_dropped++;
Patrick McHardy5b548142009-06-12 06:22:29 +0000217 ret = NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 break;
219 }
220 /* Alan Cox recommends always returning 0, and always freeing the packet */
221 /* experience suggest a slightly more conservative approach */
222
Patrick McHardyec634fe2009-07-05 19:23:38 -0700223 if (ret == NETDEV_TX_OK)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 {
225 dev_kfree_skb(skb);
226 netif_wake_queue(dev);
227 }
228 return(ret);
229}
230
231static int dlci_config(struct net_device *dev, struct dlci_conf __user *conf, int get)
232{
233 struct dlci_conf config;
234 struct dlci_local *dlp;
235 struct frad_local *flp;
236 int err;
237
Wang Chen8f15ea42008-11-12 23:38:36 -0800238 dlp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
Wang Chen8f15ea42008-11-12 23:38:36 -0800240 flp = netdev_priv(dlp->slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
242 if (!get)
243 {
244 if(copy_from_user(&config, conf, sizeof(struct dlci_conf)))
245 return -EFAULT;
246 if (config.flags & ~DLCI_VALID_FLAGS)
247 return(-EINVAL);
248 memcpy(&dlp->config, &config, sizeof(struct dlci_conf));
249 dlp->configured = 1;
250 }
251
252 err = (*flp->dlci_conf)(dlp->slave, dev, get);
253 if (err)
254 return(err);
255
256 if (get)
257 {
258 if(copy_to_user(conf, &dlp->config, sizeof(struct dlci_conf)))
259 return -EFAULT;
260 }
261
262 return(0);
263}
264
265static int dlci_dev_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
266{
267 struct dlci_local *dlp;
268
269 if (!capable(CAP_NET_ADMIN))
270 return(-EPERM);
271
Wang Chen8f15ea42008-11-12 23:38:36 -0800272 dlp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
274 switch(cmd)
275 {
276 case DLCI_GET_SLAVE:
277 if (!*(short *)(dev->dev_addr))
278 return(-EINVAL);
279
280 strncpy(ifr->ifr_slave, dlp->slave->name, sizeof(ifr->ifr_slave));
281 break;
282
283 case DLCI_GET_CONF:
284 case DLCI_SET_CONF:
285 if (!*(short *)(dev->dev_addr))
286 return(-EINVAL);
287
288 return(dlci_config(dev, ifr->ifr_data, cmd == DLCI_GET_CONF));
289 break;
290
291 default:
292 return(-EOPNOTSUPP);
293 }
294 return(0);
295}
296
297static int dlci_change_mtu(struct net_device *dev, int new_mtu)
298{
Stephen Hemminger49e8aba2009-03-20 19:36:15 +0000299 struct dlci_local *dlp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
Stephen Hemminger49e8aba2009-03-20 19:36:15 +0000301 return dev_set_mtu(dlp->slave, new_mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302}
303
304static int dlci_open(struct net_device *dev)
305{
306 struct dlci_local *dlp;
307 struct frad_local *flp;
308 int err;
309
Wang Chen8f15ea42008-11-12 23:38:36 -0800310 dlp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
312 if (!*(short *)(dev->dev_addr))
313 return(-EINVAL);
314
315 if (!netif_running(dlp->slave))
316 return(-ENOTCONN);
317
Wang Chen8f15ea42008-11-12 23:38:36 -0800318 flp = netdev_priv(dlp->slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 err = (*flp->activate)(dlp->slave, dev);
320 if (err)
321 return(err);
322
323 netif_start_queue(dev);
324
325 return 0;
326}
327
328static int dlci_close(struct net_device *dev)
329{
330 struct dlci_local *dlp;
331 struct frad_local *flp;
332 int err;
333
334 netif_stop_queue(dev);
335
Wang Chen8f15ea42008-11-12 23:38:36 -0800336 dlp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
Wang Chen8f15ea42008-11-12 23:38:36 -0800338 flp = netdev_priv(dlp->slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 err = (*flp->deactivate)(dlp->slave, dev);
340
341 return 0;
342}
343
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344static int dlci_add(struct dlci_add *dlci)
345{
346 struct net_device *master, *slave;
347 struct dlci_local *dlp;
348 struct frad_local *flp;
349 int err = -EINVAL;
350
351
352 /* validate slave device */
Eric W. Biederman881d9662007-09-17 11:56:21 -0700353 slave = dev_get_by_name(&init_net, dlci->devname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 if (!slave)
355 return -ENODEV;
356
Wang Chen8f15ea42008-11-12 23:38:36 -0800357 if (slave->type != ARPHRD_FRAD || netdev_priv(slave) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 goto err1;
359
360 /* create device name */
361 master = alloc_netdev( sizeof(struct dlci_local), "dlci%d",
362 dlci_setup);
363 if (!master) {
364 err = -ENOMEM;
365 goto err1;
366 }
367
368 /* make sure same slave not already registered */
369 rtnl_lock();
370 list_for_each_entry(dlp, &dlci_devs, list) {
371 if (dlp->slave == slave) {
372 err = -EBUSY;
373 goto err2;
374 }
375 }
376
377 err = dev_alloc_name(master, master->name);
378 if (err < 0)
379 goto err2;
380
381 *(short *)(master->dev_addr) = dlci->dlci;
382
Wang Chen8f15ea42008-11-12 23:38:36 -0800383 dlp = netdev_priv(master);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 dlp->slave = slave;
385 dlp->master = master;
386
Wang Chen8f15ea42008-11-12 23:38:36 -0800387 flp = netdev_priv(slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 err = (*flp->assoc)(slave, master);
389 if (err < 0)
390 goto err2;
391
392 err = register_netdevice(master);
393 if (err < 0)
394 goto err2;
395
396 strcpy(dlci->devname, master->name);
397
398 list_add(&dlp->list, &dlci_devs);
399 rtnl_unlock();
400
401 return(0);
402
403 err2:
404 rtnl_unlock();
405 free_netdev(master);
406 err1:
407 dev_put(slave);
408 return(err);
409}
410
411static int dlci_del(struct dlci_add *dlci)
412{
413 struct dlci_local *dlp;
414 struct frad_local *flp;
415 struct net_device *master, *slave;
416 int err;
417
418 /* validate slave device */
Eric W. Biederman881d9662007-09-17 11:56:21 -0700419 master = __dev_get_by_name(&init_net, dlci->devname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 if (!master)
421 return(-ENODEV);
422
423 if (netif_running(master)) {
424 return(-EBUSY);
425 }
426
Wang Chen8f15ea42008-11-12 23:38:36 -0800427 dlp = netdev_priv(master);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 slave = dlp->slave;
Wang Chen8f15ea42008-11-12 23:38:36 -0800429 flp = netdev_priv(slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
431 rtnl_lock();
432 err = (*flp->deassoc)(slave, master);
433 if (!err) {
434 list_del(&dlp->list);
435
436 unregister_netdevice(master);
437
438 dev_put(slave);
439 }
440 rtnl_unlock();
441
442 return(err);
443}
444
445static int dlci_ioctl(unsigned int cmd, void __user *arg)
446{
447 struct dlci_add add;
448 int err;
449
450 if (!capable(CAP_NET_ADMIN))
451 return(-EPERM);
452
453 if(copy_from_user(&add, arg, sizeof(struct dlci_add)))
454 return -EFAULT;
455
456 switch (cmd)
457 {
458 case SIOCADDDLCI:
459 err = dlci_add(&add);
460
461 if (!err)
462 if(copy_to_user(arg, &add, sizeof(struct dlci_add)))
463 return -EFAULT;
464 break;
465
466 case SIOCDELDLCI:
467 err = dlci_del(&add);
468 break;
469
470 default:
471 err = -EINVAL;
472 }
473
474 return(err);
475}
476
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -0700477static const struct header_ops dlci_header_ops = {
478 .create = dlci_header,
479};
480
Stephen Hemminger49e8aba2009-03-20 19:36:15 +0000481static const struct net_device_ops dlci_netdev_ops = {
482 .ndo_open = dlci_open,
483 .ndo_stop = dlci_close,
484 .ndo_do_ioctl = dlci_dev_ioctl,
485 .ndo_start_xmit = dlci_transmit,
486 .ndo_change_mtu = dlci_change_mtu,
487};
488
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489static void dlci_setup(struct net_device *dev)
490{
Wang Chen8f15ea42008-11-12 23:38:36 -0800491 struct dlci_local *dlp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
493 dev->flags = 0;
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -0700494 dev->header_ops = &dlci_header_ops;
Stephen Hemminger49e8aba2009-03-20 19:36:15 +0000495 dev->netdev_ops = &dlci_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 dev->destructor = free_netdev;
497
498 dlp->receive = dlci_receive;
499
500 dev->type = ARPHRD_DLCI;
501 dev->hard_header_len = sizeof(struct frhdr);
502 dev->addr_len = sizeof(short);
503
504}
505
506/* if slave is unregistering, then cleanup master */
507static int dlci_dev_event(struct notifier_block *unused,
508 unsigned long event, void *ptr)
509{
510 struct net_device *dev = (struct net_device *) ptr;
511
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900512 if (dev_net(dev) != &init_net)
Eric W. Biedermane9dc8652007-09-12 13:02:17 +0200513 return NOTIFY_DONE;
514
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 if (event == NETDEV_UNREGISTER) {
516 struct dlci_local *dlp;
517
518 list_for_each_entry(dlp, &dlci_devs, list) {
519 if (dlp->slave == dev) {
520 list_del(&dlp->list);
521 unregister_netdevice(dlp->master);
522 dev_put(dlp->slave);
523 break;
524 }
525 }
526 }
527 return NOTIFY_DONE;
528}
529
530static struct notifier_block dlci_notifier = {
531 .notifier_call = dlci_dev_event,
532};
533
534static int __init init_dlci(void)
535{
536 dlci_ioctl_set(dlci_ioctl);
537 register_netdevice_notifier(&dlci_notifier);
538
539 printk("%s.\n", version);
540
541 return 0;
542}
543
544static void __exit dlci_exit(void)
545{
546 struct dlci_local *dlp, *nxt;
547
548 dlci_ioctl_set(NULL);
549 unregister_netdevice_notifier(&dlci_notifier);
550
551 rtnl_lock();
552 list_for_each_entry_safe(dlp, nxt, &dlci_devs, list) {
553 unregister_netdevice(dlp->master);
554 dev_put(dlp->slave);
555 }
556 rtnl_unlock();
557}
558
559module_init(init_dlci);
560module_exit(dlci_exit);
561
562MODULE_AUTHOR("Mike McLagan");
563MODULE_DESCRIPTION("Frame Relay DLCI layer");
564MODULE_LICENSE("GPL");