blob: a1b0b0d7391ad45ddc18dc2821ee65acb5bdc0d2 [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
189static int dlci_transmit(struct sk_buff *skb, struct net_device *dev)
190{
191 struct dlci_local *dlp;
192 int ret;
193
194 ret = 0;
195
196 if (!skb || !dev)
197 return(0);
198
Wang Chen8f15ea42008-11-12 23:38:36 -0800199 dlp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
201 netif_stop_queue(dev);
202
203 ret = dlp->slave->hard_start_xmit(skb, dlp->slave);
204 switch (ret)
205 {
206 case DLCI_RET_OK:
Stephen Hemminger07d117c2009-03-20 19:36:14 +0000207 dev->stats.tx_packets++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 ret = 0;
209 break;
210 case DLCI_RET_ERR:
Stephen Hemminger07d117c2009-03-20 19:36:14 +0000211 dev->stats.tx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 ret = 0;
213 break;
214 case DLCI_RET_DROP:
Stephen Hemminger07d117c2009-03-20 19:36:14 +0000215 dev->stats.tx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 ret = 1;
217 break;
218 }
219 /* Alan Cox recommends always returning 0, and always freeing the packet */
220 /* experience suggest a slightly more conservative approach */
221
222 if (!ret)
223 {
224 dev_kfree_skb(skb);
225 netif_wake_queue(dev);
226 }
227 return(ret);
228}
229
230static int dlci_config(struct net_device *dev, struct dlci_conf __user *conf, int get)
231{
232 struct dlci_conf config;
233 struct dlci_local *dlp;
234 struct frad_local *flp;
235 int err;
236
Wang Chen8f15ea42008-11-12 23:38:36 -0800237 dlp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
Wang Chen8f15ea42008-11-12 23:38:36 -0800239 flp = netdev_priv(dlp->slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
241 if (!get)
242 {
243 if(copy_from_user(&config, conf, sizeof(struct dlci_conf)))
244 return -EFAULT;
245 if (config.flags & ~DLCI_VALID_FLAGS)
246 return(-EINVAL);
247 memcpy(&dlp->config, &config, sizeof(struct dlci_conf));
248 dlp->configured = 1;
249 }
250
251 err = (*flp->dlci_conf)(dlp->slave, dev, get);
252 if (err)
253 return(err);
254
255 if (get)
256 {
257 if(copy_to_user(conf, &dlp->config, sizeof(struct dlci_conf)))
258 return -EFAULT;
259 }
260
261 return(0);
262}
263
264static int dlci_dev_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
265{
266 struct dlci_local *dlp;
267
268 if (!capable(CAP_NET_ADMIN))
269 return(-EPERM);
270
Wang Chen8f15ea42008-11-12 23:38:36 -0800271 dlp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
273 switch(cmd)
274 {
275 case DLCI_GET_SLAVE:
276 if (!*(short *)(dev->dev_addr))
277 return(-EINVAL);
278
279 strncpy(ifr->ifr_slave, dlp->slave->name, sizeof(ifr->ifr_slave));
280 break;
281
282 case DLCI_GET_CONF:
283 case DLCI_SET_CONF:
284 if (!*(short *)(dev->dev_addr))
285 return(-EINVAL);
286
287 return(dlci_config(dev, ifr->ifr_data, cmd == DLCI_GET_CONF));
288 break;
289
290 default:
291 return(-EOPNOTSUPP);
292 }
293 return(0);
294}
295
296static int dlci_change_mtu(struct net_device *dev, int new_mtu)
297{
298 struct dlci_local *dlp;
299
Wang Chen8f15ea42008-11-12 23:38:36 -0800300 dlp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
302 return((*dlp->slave->change_mtu)(dlp->slave, new_mtu));
303}
304
305static int dlci_open(struct net_device *dev)
306{
307 struct dlci_local *dlp;
308 struct frad_local *flp;
309 int err;
310
Wang Chen8f15ea42008-11-12 23:38:36 -0800311 dlp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
313 if (!*(short *)(dev->dev_addr))
314 return(-EINVAL);
315
316 if (!netif_running(dlp->slave))
317 return(-ENOTCONN);
318
Wang Chen8f15ea42008-11-12 23:38:36 -0800319 flp = netdev_priv(dlp->slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 err = (*flp->activate)(dlp->slave, dev);
321 if (err)
322 return(err);
323
324 netif_start_queue(dev);
325
326 return 0;
327}
328
329static int dlci_close(struct net_device *dev)
330{
331 struct dlci_local *dlp;
332 struct frad_local *flp;
333 int err;
334
335 netif_stop_queue(dev);
336
Wang Chen8f15ea42008-11-12 23:38:36 -0800337 dlp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
Wang Chen8f15ea42008-11-12 23:38:36 -0800339 flp = netdev_priv(dlp->slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 err = (*flp->deactivate)(dlp->slave, dev);
341
342 return 0;
343}
344
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345static int dlci_add(struct dlci_add *dlci)
346{
347 struct net_device *master, *slave;
348 struct dlci_local *dlp;
349 struct frad_local *flp;
350 int err = -EINVAL;
351
352
353 /* validate slave device */
Eric W. Biederman881d9662007-09-17 11:56:21 -0700354 slave = dev_get_by_name(&init_net, dlci->devname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 if (!slave)
356 return -ENODEV;
357
Wang Chen8f15ea42008-11-12 23:38:36 -0800358 if (slave->type != ARPHRD_FRAD || netdev_priv(slave) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 goto err1;
360
361 /* create device name */
362 master = alloc_netdev( sizeof(struct dlci_local), "dlci%d",
363 dlci_setup);
364 if (!master) {
365 err = -ENOMEM;
366 goto err1;
367 }
368
369 /* make sure same slave not already registered */
370 rtnl_lock();
371 list_for_each_entry(dlp, &dlci_devs, list) {
372 if (dlp->slave == slave) {
373 err = -EBUSY;
374 goto err2;
375 }
376 }
377
378 err = dev_alloc_name(master, master->name);
379 if (err < 0)
380 goto err2;
381
382 *(short *)(master->dev_addr) = dlci->dlci;
383
Wang Chen8f15ea42008-11-12 23:38:36 -0800384 dlp = netdev_priv(master);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 dlp->slave = slave;
386 dlp->master = master;
387
Wang Chen8f15ea42008-11-12 23:38:36 -0800388 flp = netdev_priv(slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 err = (*flp->assoc)(slave, master);
390 if (err < 0)
391 goto err2;
392
393 err = register_netdevice(master);
394 if (err < 0)
395 goto err2;
396
397 strcpy(dlci->devname, master->name);
398
399 list_add(&dlp->list, &dlci_devs);
400 rtnl_unlock();
401
402 return(0);
403
404 err2:
405 rtnl_unlock();
406 free_netdev(master);
407 err1:
408 dev_put(slave);
409 return(err);
410}
411
412static int dlci_del(struct dlci_add *dlci)
413{
414 struct dlci_local *dlp;
415 struct frad_local *flp;
416 struct net_device *master, *slave;
417 int err;
418
419 /* validate slave device */
Eric W. Biederman881d9662007-09-17 11:56:21 -0700420 master = __dev_get_by_name(&init_net, dlci->devname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 if (!master)
422 return(-ENODEV);
423
424 if (netif_running(master)) {
425 return(-EBUSY);
426 }
427
Wang Chen8f15ea42008-11-12 23:38:36 -0800428 dlp = netdev_priv(master);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 slave = dlp->slave;
Wang Chen8f15ea42008-11-12 23:38:36 -0800430 flp = netdev_priv(slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
432 rtnl_lock();
433 err = (*flp->deassoc)(slave, master);
434 if (!err) {
435 list_del(&dlp->list);
436
437 unregister_netdevice(master);
438
439 dev_put(slave);
440 }
441 rtnl_unlock();
442
443 return(err);
444}
445
446static int dlci_ioctl(unsigned int cmd, void __user *arg)
447{
448 struct dlci_add add;
449 int err;
450
451 if (!capable(CAP_NET_ADMIN))
452 return(-EPERM);
453
454 if(copy_from_user(&add, arg, sizeof(struct dlci_add)))
455 return -EFAULT;
456
457 switch (cmd)
458 {
459 case SIOCADDDLCI:
460 err = dlci_add(&add);
461
462 if (!err)
463 if(copy_to_user(arg, &add, sizeof(struct dlci_add)))
464 return -EFAULT;
465 break;
466
467 case SIOCDELDLCI:
468 err = dlci_del(&add);
469 break;
470
471 default:
472 err = -EINVAL;
473 }
474
475 return(err);
476}
477
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -0700478static const struct header_ops dlci_header_ops = {
479 .create = dlci_header,
480};
481
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482static void dlci_setup(struct net_device *dev)
483{
Wang Chen8f15ea42008-11-12 23:38:36 -0800484 struct dlci_local *dlp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
486 dev->flags = 0;
487 dev->open = dlci_open;
488 dev->stop = dlci_close;
489 dev->do_ioctl = dlci_dev_ioctl;
490 dev->hard_start_xmit = dlci_transmit;
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -0700491 dev->header_ops = &dlci_header_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 dev->change_mtu = dlci_change_mtu;
493 dev->destructor = free_netdev;
494
495 dlp->receive = dlci_receive;
496
497 dev->type = ARPHRD_DLCI;
498 dev->hard_header_len = sizeof(struct frhdr);
499 dev->addr_len = sizeof(short);
500
501}
502
503/* if slave is unregistering, then cleanup master */
504static int dlci_dev_event(struct notifier_block *unused,
505 unsigned long event, void *ptr)
506{
507 struct net_device *dev = (struct net_device *) ptr;
508
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +0900509 if (dev_net(dev) != &init_net)
Eric W. Biedermane9dc8652007-09-12 13:02:17 +0200510 return NOTIFY_DONE;
511
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 if (event == NETDEV_UNREGISTER) {
513 struct dlci_local *dlp;
514
515 list_for_each_entry(dlp, &dlci_devs, list) {
516 if (dlp->slave == dev) {
517 list_del(&dlp->list);
518 unregister_netdevice(dlp->master);
519 dev_put(dlp->slave);
520 break;
521 }
522 }
523 }
524 return NOTIFY_DONE;
525}
526
527static struct notifier_block dlci_notifier = {
528 .notifier_call = dlci_dev_event,
529};
530
531static int __init init_dlci(void)
532{
533 dlci_ioctl_set(dlci_ioctl);
534 register_netdevice_notifier(&dlci_notifier);
535
536 printk("%s.\n", version);
537
538 return 0;
539}
540
541static void __exit dlci_exit(void)
542{
543 struct dlci_local *dlp, *nxt;
544
545 dlci_ioctl_set(NULL);
546 unregister_netdevice_notifier(&dlci_notifier);
547
548 rtnl_lock();
549 list_for_each_entry_safe(dlp, nxt, &dlci_devs, list) {
550 unregister_netdevice(dlp->master);
551 dev_put(dlp->slave);
552 }
553 rtnl_unlock();
554}
555
556module_init(init_dlci);
557module_exit(dlci_exit);
558
559MODULE_AUTHOR("Mike McLagan");
560MODULE_DESCRIPTION("Frame Relay DLCI layer");
561MODULE_LICENSE("GPL");