added 2.6.29.6 aldebaran kernel
[nao-ulib.git] / kernel / 2.6.29.6-aldebaran-rt / drivers / net / wan / dlci.c
bloba297e3efa05dd32369d0c1c23c14378675c51436
1 /*
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.
8 * Version: @(#)dlci.c 0.35 4 Jan 1997
10 * Author: Mike McLagan <mike.mclagan@linux.org>
12 * Changes:
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
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.
31 #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>
49 #include <net/sock.h>
51 #include <asm/system.h>
52 #include <asm/io.h>
53 #include <asm/dma.h>
54 #include <asm/uaccess.h>
56 static const char version[] = "DLCI driver v0.35, 4 Jan 1997, mike.mclagan@linux.org";
58 static LIST_HEAD(dlci_devs);
60 static void dlci_setup(struct net_device *);
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
68 static int dlci_header(struct sk_buff *skb, struct net_device *dev,
69 unsigned short type, const void *daddr,
70 const void *saddr, unsigned len)
72 struct frhdr hdr;
73 struct dlci_local *dlp;
74 unsigned int hlen;
75 char *dest;
77 dlp = netdev_priv(dev);
79 hdr.control = FRAD_I_UI;
80 switch(type)
82 case ETH_P_IP:
83 hdr.IP_NLPID = FRAD_P_IP;
84 hlen = sizeof(hdr.control) + sizeof(hdr.IP_NLPID);
85 break;
87 /* feel free to add other types, if necessary */
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;
98 dest = skb_push(skb, hlen);
99 if (!dest)
100 return(0);
102 memcpy(dest, &hdr, hlen);
104 return(hlen);
107 static void dlci_receive(struct sk_buff *skb, struct net_device *dev)
109 struct dlci_local *dlp;
110 struct frhdr *hdr;
111 int process, header;
113 dlp = netdev_priv(dev);
114 if (!pskb_may_pull(skb, sizeof(*hdr))) {
115 printk(KERN_NOTICE "%s: invalid data no header\n",
116 dev->name);
117 dlp->stats.rx_errors++;
118 kfree_skb(skb);
119 return;
122 hdr = (struct frhdr *) skb->data;
123 process = 0;
124 header = 0;
125 skb->dev = dev;
127 if (hdr->control != FRAD_I_UI)
129 printk(KERN_NOTICE "%s: Invalid header flag 0x%02X.\n", dev->name, hdr->control);
130 dlp->stats.rx_errors++;
132 else
133 switch(hdr->IP_NLPID)
135 case FRAD_P_PADDING:
136 if (hdr->NLPID != FRAD_P_SNAP)
138 printk(KERN_NOTICE "%s: Unsupported NLPID 0x%02X.\n", dev->name, hdr->NLPID);
139 dlp->stats.rx_errors++;
140 break;
143 if (hdr->OUI[0] + hdr->OUI[1] + hdr->OUI[2] != 0)
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]);
146 dlp->stats.rx_errors++;
147 break;
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;
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;
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);
167 dlp->stats.rx_errors++;
168 break;
170 default:
171 printk(KERN_NOTICE "%s: Invalid pad byte 0x%02X.\n", dev->name, hdr->pad);
172 dlp->stats.rx_errors++;
173 break;
176 if (process)
178 /* we've set up the protocol, so discard the header */
179 skb_reset_mac_header(skb);
180 skb_pull(skb, header);
181 dlp->stats.rx_bytes += skb->len;
182 netif_rx(skb);
183 dlp->stats.rx_packets++;
185 else
186 dev_kfree_skb(skb);
189 static int dlci_transmit(struct sk_buff *skb, struct net_device *dev)
191 struct dlci_local *dlp;
192 int ret;
194 ret = 0;
196 if (!skb || !dev)
197 return(0);
199 dlp = netdev_priv(dev);
201 netif_stop_queue(dev);
203 ret = dlp->slave->hard_start_xmit(skb, dlp->slave);
204 switch (ret)
206 case DLCI_RET_OK:
207 dlp->stats.tx_packets++;
208 ret = 0;
209 break;
210 case DLCI_RET_ERR:
211 dlp->stats.tx_errors++;
212 ret = 0;
213 break;
214 case DLCI_RET_DROP:
215 dlp->stats.tx_dropped++;
216 ret = 1;
217 break;
219 /* Alan Cox recommends always returning 0, and always freeing the packet */
220 /* experience suggest a slightly more conservative approach */
222 if (!ret)
224 dev_kfree_skb(skb);
225 netif_wake_queue(dev);
227 return(ret);
230 static int dlci_config(struct net_device *dev, struct dlci_conf __user *conf, int get)
232 struct dlci_conf config;
233 struct dlci_local *dlp;
234 struct frad_local *flp;
235 int err;
237 dlp = netdev_priv(dev);
239 flp = netdev_priv(dlp->slave);
241 if (!get)
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;
251 err = (*flp->dlci_conf)(dlp->slave, dev, get);
252 if (err)
253 return(err);
255 if (get)
257 if(copy_to_user(conf, &dlp->config, sizeof(struct dlci_conf)))
258 return -EFAULT;
261 return(0);
264 static int dlci_dev_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
266 struct dlci_local *dlp;
268 if (!capable(CAP_NET_ADMIN))
269 return(-EPERM);
271 dlp = netdev_priv(dev);
273 switch(cmd)
275 case DLCI_GET_SLAVE:
276 if (!*(short *)(dev->dev_addr))
277 return(-EINVAL);
279 strncpy(ifr->ifr_slave, dlp->slave->name, sizeof(ifr->ifr_slave));
280 break;
282 case DLCI_GET_CONF:
283 case DLCI_SET_CONF:
284 if (!*(short *)(dev->dev_addr))
285 return(-EINVAL);
287 return(dlci_config(dev, ifr->ifr_data, cmd == DLCI_GET_CONF));
288 break;
290 default:
291 return(-EOPNOTSUPP);
293 return(0);
296 static int dlci_change_mtu(struct net_device *dev, int new_mtu)
298 struct dlci_local *dlp;
300 dlp = netdev_priv(dev);
302 return((*dlp->slave->change_mtu)(dlp->slave, new_mtu));
305 static int dlci_open(struct net_device *dev)
307 struct dlci_local *dlp;
308 struct frad_local *flp;
309 int err;
311 dlp = netdev_priv(dev);
313 if (!*(short *)(dev->dev_addr))
314 return(-EINVAL);
316 if (!netif_running(dlp->slave))
317 return(-ENOTCONN);
319 flp = netdev_priv(dlp->slave);
320 err = (*flp->activate)(dlp->slave, dev);
321 if (err)
322 return(err);
324 netif_start_queue(dev);
326 return 0;
329 static int dlci_close(struct net_device *dev)
331 struct dlci_local *dlp;
332 struct frad_local *flp;
333 int err;
335 netif_stop_queue(dev);
337 dlp = netdev_priv(dev);
339 flp = netdev_priv(dlp->slave);
340 err = (*flp->deactivate)(dlp->slave, dev);
342 return 0;
345 static struct net_device_stats *dlci_get_stats(struct net_device *dev)
347 struct dlci_local *dlp;
349 dlp = netdev_priv(dev);
351 return(&dlp->stats);
354 static int dlci_add(struct dlci_add *dlci)
356 struct net_device *master, *slave;
357 struct dlci_local *dlp;
358 struct frad_local *flp;
359 int err = -EINVAL;
362 /* validate slave device */
363 slave = dev_get_by_name(&init_net, dlci->devname);
364 if (!slave)
365 return -ENODEV;
367 if (slave->type != ARPHRD_FRAD || netdev_priv(slave) == NULL)
368 goto err1;
370 /* create device name */
371 master = alloc_netdev( sizeof(struct dlci_local), "dlci%d",
372 dlci_setup);
373 if (!master) {
374 err = -ENOMEM;
375 goto err1;
378 /* make sure same slave not already registered */
379 rtnl_lock();
380 list_for_each_entry(dlp, &dlci_devs, list) {
381 if (dlp->slave == slave) {
382 err = -EBUSY;
383 goto err2;
387 err = dev_alloc_name(master, master->name);
388 if (err < 0)
389 goto err2;
391 *(short *)(master->dev_addr) = dlci->dlci;
393 dlp = netdev_priv(master);
394 dlp->slave = slave;
395 dlp->master = master;
397 flp = netdev_priv(slave);
398 err = (*flp->assoc)(slave, master);
399 if (err < 0)
400 goto err2;
402 err = register_netdevice(master);
403 if (err < 0)
404 goto err2;
406 strcpy(dlci->devname, master->name);
408 list_add(&dlp->list, &dlci_devs);
409 rtnl_unlock();
411 return(0);
413 err2:
414 rtnl_unlock();
415 free_netdev(master);
416 err1:
417 dev_put(slave);
418 return(err);
421 static int dlci_del(struct dlci_add *dlci)
423 struct dlci_local *dlp;
424 struct frad_local *flp;
425 struct net_device *master, *slave;
426 int err;
428 /* validate slave device */
429 master = __dev_get_by_name(&init_net, dlci->devname);
430 if (!master)
431 return(-ENODEV);
433 if (netif_running(master)) {
434 return(-EBUSY);
437 dlp = netdev_priv(master);
438 slave = dlp->slave;
439 flp = netdev_priv(slave);
441 rtnl_lock();
442 err = (*flp->deassoc)(slave, master);
443 if (!err) {
444 list_del(&dlp->list);
446 unregister_netdevice(master);
448 dev_put(slave);
450 rtnl_unlock();
452 return(err);
455 static int dlci_ioctl(unsigned int cmd, void __user *arg)
457 struct dlci_add add;
458 int err;
460 if (!capable(CAP_NET_ADMIN))
461 return(-EPERM);
463 if(copy_from_user(&add, arg, sizeof(struct dlci_add)))
464 return -EFAULT;
466 switch (cmd)
468 case SIOCADDDLCI:
469 err = dlci_add(&add);
471 if (!err)
472 if(copy_to_user(arg, &add, sizeof(struct dlci_add)))
473 return -EFAULT;
474 break;
476 case SIOCDELDLCI:
477 err = dlci_del(&add);
478 break;
480 default:
481 err = -EINVAL;
484 return(err);
487 static const struct header_ops dlci_header_ops = {
488 .create = dlci_header,
491 static void dlci_setup(struct net_device *dev)
493 struct dlci_local *dlp = netdev_priv(dev);
495 dev->flags = 0;
496 dev->open = dlci_open;
497 dev->stop = dlci_close;
498 dev->do_ioctl = dlci_dev_ioctl;
499 dev->hard_start_xmit = dlci_transmit;
500 dev->header_ops = &dlci_header_ops;
501 dev->get_stats = dlci_get_stats;
502 dev->change_mtu = dlci_change_mtu;
503 dev->destructor = free_netdev;
505 dlp->receive = dlci_receive;
507 dev->type = ARPHRD_DLCI;
508 dev->hard_header_len = sizeof(struct frhdr);
509 dev->addr_len = sizeof(short);
513 /* if slave is unregistering, then cleanup master */
514 static int dlci_dev_event(struct notifier_block *unused,
515 unsigned long event, void *ptr)
517 struct net_device *dev = (struct net_device *) ptr;
519 if (dev_net(dev) != &init_net)
520 return NOTIFY_DONE;
522 if (event == NETDEV_UNREGISTER) {
523 struct dlci_local *dlp;
525 list_for_each_entry(dlp, &dlci_devs, list) {
526 if (dlp->slave == dev) {
527 list_del(&dlp->list);
528 unregister_netdevice(dlp->master);
529 dev_put(dlp->slave);
530 break;
534 return NOTIFY_DONE;
537 static struct notifier_block dlci_notifier = {
538 .notifier_call = dlci_dev_event,
541 static int __init init_dlci(void)
543 dlci_ioctl_set(dlci_ioctl);
544 register_netdevice_notifier(&dlci_notifier);
546 printk("%s.\n", version);
548 return 0;
551 static void __exit dlci_exit(void)
553 struct dlci_local *dlp, *nxt;
555 dlci_ioctl_set(NULL);
556 unregister_netdevice_notifier(&dlci_notifier);
558 rtnl_lock();
559 list_for_each_entry_safe(dlp, nxt, &dlci_devs, list) {
560 unregister_netdevice(dlp->master);
561 dev_put(dlp->slave);
563 rtnl_unlock();
566 module_init(init_dlci);
567 module_exit(dlci_exit);
569 MODULE_AUTHOR("Mike McLagan");
570 MODULE_DESCRIPTION("Frame Relay DLCI layer");
571 MODULE_LICENSE("GPL");