blob: e94952e799fea3a70fa19626f687d2bfc3733f83 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * MKISS Driver
3 *
4 * This module:
5 * This module is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version
8 * 2 of the License, or (at your option) any later version.
9 *
10 * This module implements the AX.25 protocol for kernel-based
11 * devices like TTYs. It interfaces between a raw TTY, and the
12 * kernel's AX.25 protocol layers, just like slip.c.
13 * AX.25 needs to be separated from slip.c while slip.c is no
14 * longer a static kernel device since it is a module.
15 * This method clears the way to implement other kiss protocols
16 * like mkiss smack g8bpq ..... so far only mkiss is implemented.
17 *
18 * Hans Alblas <hans@esrac.ele.tue.nl>
19 *
20 * History
21 * Jonathan (G4KLX) Fixed to match Linux networking changes - 2.1.15.
22 * Matthias (DG2FEF) Added support for FlexNet CRC (on special request)
23 * Fixed bug in ax25_close(): dev_lock_wait() was
24 * called twice, causing a deadlock.
25 * Jeroen (PE1RXQ) Removed old MKISS_MAGIC stuff and calls to
26 * MOD_*_USE_COUNT
27 * Remove cli() and fix rtnl lock usage.
28 */
29
30#include <linux/config.h>
31#include <linux/module.h>
32#include <asm/system.h>
33#include <linux/bitops.h>
34#include <asm/uaccess.h>
35#include <linux/string.h>
36#include <linux/mm.h>
37#include <linux/interrupt.h>
38#include <linux/in.h>
39#include <linux/inet.h>
40#include <linux/tty.h>
41#include <linux/errno.h>
42#include <linux/netdevice.h>
43#include <linux/major.h>
44#include <linux/init.h>
45#include <linux/rtnetlink.h>
46#include <linux/etherdevice.h>
47#include <linux/skbuff.h>
48#include <linux/if_arp.h>
Marcelo Feitoza Parisicd8749b2005-07-15 11:16:42 +010049#include <linux/jiffies.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
51#include <net/ax25.h>
52
53#include "mkiss.h"
54
55#ifdef CONFIG_INET
56#include <linux/ip.h>
57#include <linux/tcp.h>
58#endif
59
60static char banner[] __initdata = KERN_INFO "mkiss: AX.25 Multikiss, Hans Albas PE1AYX\n";
61
62typedef struct ax25_ctrl {
63 struct ax_disp ctrl; /* */
64 struct net_device dev; /* the device */
65} ax25_ctrl_t;
66
67static ax25_ctrl_t **ax25_ctrls;
68
69int ax25_maxdev = AX25_MAXDEV; /* Can be overridden with insmod! */
70
71static struct tty_ldisc ax_ldisc;
72
73static int ax25_init(struct net_device *);
74static int kiss_esc(unsigned char *, unsigned char *, int);
75static int kiss_esc_crc(unsigned char *, unsigned char *, unsigned short, int);
76static void kiss_unesc(struct ax_disp *, unsigned char);
77
78/*---------------------------------------------------------------------------*/
79
80static const unsigned short Crc_flex_table[] = {
81 0x0f87, 0x1e0e, 0x2c95, 0x3d1c, 0x49a3, 0x582a, 0x6ab1, 0x7b38,
82 0x83cf, 0x9246, 0xa0dd, 0xb154, 0xc5eb, 0xd462, 0xe6f9, 0xf770,
83 0x1f06, 0x0e8f, 0x3c14, 0x2d9d, 0x5922, 0x48ab, 0x7a30, 0x6bb9,
84 0x934e, 0x82c7, 0xb05c, 0xa1d5, 0xd56a, 0xc4e3, 0xf678, 0xe7f1,
85 0x2e85, 0x3f0c, 0x0d97, 0x1c1e, 0x68a1, 0x7928, 0x4bb3, 0x5a3a,
86 0xa2cd, 0xb344, 0x81df, 0x9056, 0xe4e9, 0xf560, 0xc7fb, 0xd672,
87 0x3e04, 0x2f8d, 0x1d16, 0x0c9f, 0x7820, 0x69a9, 0x5b32, 0x4abb,
88 0xb24c, 0xa3c5, 0x915e, 0x80d7, 0xf468, 0xe5e1, 0xd77a, 0xc6f3,
89 0x4d83, 0x5c0a, 0x6e91, 0x7f18, 0x0ba7, 0x1a2e, 0x28b5, 0x393c,
90 0xc1cb, 0xd042, 0xe2d9, 0xf350, 0x87ef, 0x9666, 0xa4fd, 0xb574,
91 0x5d02, 0x4c8b, 0x7e10, 0x6f99, 0x1b26, 0x0aaf, 0x3834, 0x29bd,
92 0xd14a, 0xc0c3, 0xf258, 0xe3d1, 0x976e, 0x86e7, 0xb47c, 0xa5f5,
93 0x6c81, 0x7d08, 0x4f93, 0x5e1a, 0x2aa5, 0x3b2c, 0x09b7, 0x183e,
94 0xe0c9, 0xf140, 0xc3db, 0xd252, 0xa6ed, 0xb764, 0x85ff, 0x9476,
95 0x7c00, 0x6d89, 0x5f12, 0x4e9b, 0x3a24, 0x2bad, 0x1936, 0x08bf,
96 0xf048, 0xe1c1, 0xd35a, 0xc2d3, 0xb66c, 0xa7e5, 0x957e, 0x84f7,
97 0x8b8f, 0x9a06, 0xa89d, 0xb914, 0xcdab, 0xdc22, 0xeeb9, 0xff30,
98 0x07c7, 0x164e, 0x24d5, 0x355c, 0x41e3, 0x506a, 0x62f1, 0x7378,
99 0x9b0e, 0x8a87, 0xb81c, 0xa995, 0xdd2a, 0xcca3, 0xfe38, 0xefb1,
100 0x1746, 0x06cf, 0x3454, 0x25dd, 0x5162, 0x40eb, 0x7270, 0x63f9,
101 0xaa8d, 0xbb04, 0x899f, 0x9816, 0xeca9, 0xfd20, 0xcfbb, 0xde32,
102 0x26c5, 0x374c, 0x05d7, 0x145e, 0x60e1, 0x7168, 0x43f3, 0x527a,
103 0xba0c, 0xab85, 0x991e, 0x8897, 0xfc28, 0xeda1, 0xdf3a, 0xceb3,
104 0x3644, 0x27cd, 0x1556, 0x04df, 0x7060, 0x61e9, 0x5372, 0x42fb,
105 0xc98b, 0xd802, 0xea99, 0xfb10, 0x8faf, 0x9e26, 0xacbd, 0xbd34,
106 0x45c3, 0x544a, 0x66d1, 0x7758, 0x03e7, 0x126e, 0x20f5, 0x317c,
107 0xd90a, 0xc883, 0xfa18, 0xeb91, 0x9f2e, 0x8ea7, 0xbc3c, 0xadb5,
108 0x5542, 0x44cb, 0x7650, 0x67d9, 0x1366, 0x02ef, 0x3074, 0x21fd,
109 0xe889, 0xf900, 0xcb9b, 0xda12, 0xaead, 0xbf24, 0x8dbf, 0x9c36,
110 0x64c1, 0x7548, 0x47d3, 0x565a, 0x22e5, 0x336c, 0x01f7, 0x107e,
111 0xf808, 0xe981, 0xdb1a, 0xca93, 0xbe2c, 0xafa5, 0x9d3e, 0x8cb7,
112 0x7440, 0x65c9, 0x5752, 0x46db, 0x3264, 0x23ed, 0x1176, 0x00ff
113};
114
115/*---------------------------------------------------------------------------*/
116
117static unsigned short calc_crc_flex(unsigned char *cp, int size)
118{
119 unsigned short crc = 0xffff;
120
121 while (size--)
122 crc = (crc << 8) ^ Crc_flex_table[((crc >> 8) ^ *cp++) & 0xff];
123
124 return crc;
125}
126
127/*---------------------------------------------------------------------------*/
128
129static int check_crc_flex(unsigned char *cp, int size)
130{
131 unsigned short crc = 0xffff;
132
133 if (size < 3)
134 return -1;
135
136 while (size--)
137 crc = (crc << 8) ^ Crc_flex_table[((crc >> 8) ^ *cp++) & 0xff];
138
139 if ((crc & 0xffff) != 0x7070)
140 return -1;
141
142 return 0;
143}
144
145/*---------------------------------------------------------------------------*/
146
147/* Find a free channel, and link in this `tty' line. */
148static inline struct ax_disp *ax_alloc(void)
149{
150 ax25_ctrl_t *axp=NULL;
151 int i;
152
153 for (i = 0; i < ax25_maxdev; i++) {
154 axp = ax25_ctrls[i];
155
156 /* Not allocated ? */
157 if (axp == NULL)
158 break;
159
160 /* Not in use ? */
161 if (!test_and_set_bit(AXF_INUSE, &axp->ctrl.flags))
162 break;
163 }
164
165 /* Sorry, too many, all slots in use */
166 if (i >= ax25_maxdev)
167 return NULL;
168
169 /* If no channels are available, allocate one */
170 if (axp == NULL && (ax25_ctrls[i] = kmalloc(sizeof(ax25_ctrl_t), GFP_KERNEL)) != NULL) {
171 axp = ax25_ctrls[i];
172 }
173 memset(axp, 0, sizeof(ax25_ctrl_t));
174
175 /* Initialize channel control data */
176 set_bit(AXF_INUSE, &axp->ctrl.flags);
177 sprintf(axp->dev.name, "ax%d", i++);
178 axp->ctrl.tty = NULL;
179 axp->dev.base_addr = i;
180 axp->dev.priv = (void *)&axp->ctrl;
181 axp->dev.next = NULL;
182 axp->dev.init = ax25_init;
183
184 if (axp != NULL) {
185 /*
186 * register device so that it can be ifconfig'ed
187 * ax25_init() will be called as a side-effect
188 * SIDE-EFFECT WARNING: ax25_init() CLEARS axp->ctrl !
189 */
190 if (register_netdev(&axp->dev) == 0) {
191 /* (Re-)Set the INUSE bit. Very Important! */
192 set_bit(AXF_INUSE, &axp->ctrl.flags);
193 axp->ctrl.dev = &axp->dev;
194 axp->dev.priv = (void *) &axp->ctrl;
195
196 return &axp->ctrl;
197 } else {
198 clear_bit(AXF_INUSE,&axp->ctrl.flags);
199 printk(KERN_ERR "mkiss: ax_alloc() - register_netdev() failure.\n");
200 }
201 }
202
203 return NULL;
204}
205
206/* Free an AX25 channel. */
207static inline void ax_free(struct ax_disp *ax)
208{
209 /* Free all AX25 frame buffers. */
210 if (ax->rbuff)
211 kfree(ax->rbuff);
212 ax->rbuff = NULL;
213 if (ax->xbuff)
214 kfree(ax->xbuff);
215 ax->xbuff = NULL;
216 if (!test_and_clear_bit(AXF_INUSE, &ax->flags))
217 printk(KERN_ERR "mkiss: %s: ax_free for already free unit.\n", ax->dev->name);
218}
219
220static void ax_changedmtu(struct ax_disp *ax)
221{
222 struct net_device *dev = ax->dev;
223 unsigned char *xbuff, *rbuff, *oxbuff, *orbuff;
224 int len;
225
226 len = dev->mtu * 2;
227
228 /*
229 * allow for arrival of larger UDP packets, even if we say not to
230 * also fixes a bug in which SunOS sends 512-byte packets even with
231 * an MSS of 128
232 */
233 if (len < 576 * 2)
234 len = 576 * 2;
235
236 xbuff = kmalloc(len + 4, GFP_ATOMIC);
237 rbuff = kmalloc(len + 4, GFP_ATOMIC);
238
239 if (xbuff == NULL || rbuff == NULL) {
240 printk(KERN_ERR "mkiss: %s: unable to grow ax25 buffers, MTU change cancelled.\n",
241 ax->dev->name);
242 dev->mtu = ax->mtu;
243 if (xbuff != NULL)
244 kfree(xbuff);
245 if (rbuff != NULL)
246 kfree(rbuff);
247 return;
248 }
249
250 spin_lock_bh(&ax->buflock);
251
252 oxbuff = ax->xbuff;
253 ax->xbuff = xbuff;
254 orbuff = ax->rbuff;
255 ax->rbuff = rbuff;
256
257 if (ax->xleft) {
258 if (ax->xleft <= len) {
259 memcpy(ax->xbuff, ax->xhead, ax->xleft);
260 } else {
261 ax->xleft = 0;
262 ax->tx_dropped++;
263 }
264 }
265
266 ax->xhead = ax->xbuff;
267
268 if (ax->rcount) {
269 if (ax->rcount <= len) {
270 memcpy(ax->rbuff, orbuff, ax->rcount);
271 } else {
272 ax->rcount = 0;
273 ax->rx_over_errors++;
274 set_bit(AXF_ERROR, &ax->flags);
275 }
276 }
277
278 ax->mtu = dev->mtu + 73;
279 ax->buffsize = len;
280
281 spin_unlock_bh(&ax->buflock);
282
283 if (oxbuff != NULL)
284 kfree(oxbuff);
285 if (orbuff != NULL)
286 kfree(orbuff);
287}
288
289
290/* Set the "sending" flag. This must be atomic. */
291static inline void ax_lock(struct ax_disp *ax)
292{
293 netif_stop_queue(ax->dev);
294}
295
296
297/* Clear the "sending" flag. This must be atomic. */
298static inline void ax_unlock(struct ax_disp *ax)
299{
300 netif_start_queue(ax->dev);
301}
302
303/* Send one completely decapsulated AX.25 packet to the AX.25 layer. */
304static void ax_bump(struct ax_disp *ax)
305{
306 struct sk_buff *skb;
307 int count;
308
309 spin_lock_bh(&ax->buflock);
310 if (ax->rbuff[0] > 0x0f) {
311 if (ax->rbuff[0] & 0x20) {
312 ax->crcmode = CRC_MODE_FLEX;
313 if (check_crc_flex(ax->rbuff, ax->rcount) < 0) {
314 ax->rx_errors++;
315 return;
316 }
317 ax->rcount -= 2;
318 /* dl9sau bugfix: the trailling two bytes flexnet crc
319 * will not be passed to the kernel. thus we have
320 * to correct the kissparm signature, because it
321 * indicates a crc but there's none
322 */
323 *ax->rbuff &= ~0x20;
324 }
325 }
326 spin_unlock_bh(&ax->buflock);
327
328 count = ax->rcount;
329
330 if ((skb = dev_alloc_skb(count)) == NULL) {
331 printk(KERN_ERR "mkiss: %s: memory squeeze, dropping packet.\n", ax->dev->name);
332 ax->rx_dropped++;
333 return;
334 }
335
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 spin_lock_bh(&ax->buflock);
337 memcpy(skb_put(skb,count), ax->rbuff, count);
338 spin_unlock_bh(&ax->buflock);
Arnaldo Carvalho de Melo56cb5152005-04-24 18:53:06 -0700339 skb->protocol = ax25_type_trans(skb, ax->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 netif_rx(skb);
341 ax->dev->last_rx = jiffies;
342 ax->rx_packets++;
343 ax->rx_bytes+=count;
344}
345
346/* Encapsulate one AX.25 packet and stuff into a TTY queue. */
347static void ax_encaps(struct ax_disp *ax, unsigned char *icp, int len)
348{
349 unsigned char *p;
350 int actual, count;
351
352 if (ax->mtu != ax->dev->mtu + 73) /* Someone has been ifconfigging */
353 ax_changedmtu(ax);
354
355 if (len > ax->mtu) { /* Sigh, shouldn't occur BUT ... */
356 len = ax->mtu;
357 printk(KERN_ERR "mkiss: %s: truncating oversized transmit packet!\n", ax->dev->name);
358 ax->tx_dropped++;
359 ax_unlock(ax);
360 return;
361 }
362
363 p = icp;
364
365 spin_lock_bh(&ax->buflock);
366 switch (ax->crcmode) {
367 unsigned short crc;
368
369 case CRC_MODE_FLEX:
370 *p |= 0x20;
371 crc = calc_crc_flex(p, len);
372 count = kiss_esc_crc(p, (unsigned char *)ax->xbuff, crc, len+2);
373 break;
374
375 default:
376 count = kiss_esc(p, (unsigned char *)ax->xbuff, len);
377 break;
378 }
379
380 ax->tty->flags |= (1 << TTY_DO_WRITE_WAKEUP);
381 actual = ax->tty->driver->write(ax->tty, ax->xbuff, count);
382 ax->tx_packets++;
383 ax->tx_bytes+=actual;
384 ax->dev->trans_start = jiffies;
385 ax->xleft = count - actual;
386 ax->xhead = ax->xbuff + actual;
387
388 spin_unlock_bh(&ax->buflock);
389}
390
391/*
392 * Called by the driver when there's room for more data. If we have
393 * more packets to send, we send them here.
394 */
395static void ax25_write_wakeup(struct tty_struct *tty)
396{
397 int actual;
398 struct ax_disp *ax = (struct ax_disp *) tty->disc_data;
399
400 /* First make sure we're connected. */
401 if (ax == NULL || ax->magic != AX25_MAGIC || !netif_running(ax->dev))
402 return;
403 if (ax->xleft <= 0) {
404 /* Now serial buffer is almost free & we can start
405 * transmission of another packet
406 */
407 tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP);
408
409 netif_wake_queue(ax->dev);
410 return;
411 }
412
413 actual = tty->driver->write(tty, ax->xhead, ax->xleft);
414 ax->xleft -= actual;
415 ax->xhead += actual;
416}
417
418/* Encapsulate an AX.25 packet and kick it into a TTY queue. */
419static int ax_xmit(struct sk_buff *skb, struct net_device *dev)
420{
421 struct ax_disp *ax = netdev_priv(dev);
422
423 if (!netif_running(dev)) {
424 printk(KERN_ERR "mkiss: %s: xmit call when iface is down\n", dev->name);
425 return 1;
426 }
427
428 if (netif_queue_stopped(dev)) {
429 /*
430 * May be we must check transmitter timeout here ?
431 * 14 Oct 1994 Dmitry Gorodchanin.
432 */
Marcelo Feitoza Parisicd8749b2005-07-15 11:16:42 +0100433 if (time_before(jiffies, dev->trans_start + 20 * HZ)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 /* 20 sec timeout not reached */
435 return 1;
436 }
437
438 printk(KERN_ERR "mkiss: %s: transmit timed out, %s?\n", dev->name,
439 (ax->tty->driver->chars_in_buffer(ax->tty) || ax->xleft) ?
440 "bad line quality" : "driver error");
441
442 ax->xleft = 0;
443 ax->tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP);
444 ax_unlock(ax);
445 }
446
447 /* We were not busy, so we are now... :-) */
448 if (skb != NULL) {
449 ax_lock(ax);
450 ax_encaps(ax, skb->data, skb->len);
451 kfree_skb(skb);
452 }
453
454 return 0;
455}
456
457#if defined(CONFIG_AX25) || defined(CONFIG_AX25_MODULE)
458
459/* Return the frame type ID */
460static int ax_header(struct sk_buff *skb, struct net_device *dev, unsigned short type,
461 void *daddr, void *saddr, unsigned len)
462{
463#ifdef CONFIG_INET
464 if (type != htons(ETH_P_AX25))
465 return ax25_encapsulate(skb, dev, type, daddr, saddr, len);
466#endif
467 return 0;
468}
469
470
471static int ax_rebuild_header(struct sk_buff *skb)
472{
473#ifdef CONFIG_INET
474 return ax25_rebuild_header(skb);
475#else
476 return 0;
477#endif
478}
479
480#endif /* CONFIG_{AX25,AX25_MODULE} */
481
482/* Open the low-level part of the AX25 channel. Easy! */
483static int ax_open(struct net_device *dev)
484{
485 struct ax_disp *ax = netdev_priv(dev);
486 unsigned long len;
487
488 if (ax->tty == NULL)
489 return -ENODEV;
490
491 /*
492 * Allocate the frame buffers:
493 *
494 * rbuff Receive buffer.
495 * xbuff Transmit buffer.
496 */
497 len = dev->mtu * 2;
498
499 /*
500 * allow for arrival of larger UDP packets, even if we say not to
501 * also fixes a bug in which SunOS sends 512-byte packets even with
502 * an MSS of 128
503 */
504 if (len < 576 * 2)
505 len = 576 * 2;
506
507 if ((ax->rbuff = kmalloc(len + 4, GFP_KERNEL)) == NULL)
508 goto norbuff;
509
510 if ((ax->xbuff = kmalloc(len + 4, GFP_KERNEL)) == NULL)
511 goto noxbuff;
512
513 ax->mtu = dev->mtu + 73;
514 ax->buffsize = len;
515 ax->rcount = 0;
516 ax->xleft = 0;
517
518 ax->flags &= (1 << AXF_INUSE); /* Clear ESCAPE & ERROR flags */
519
520 spin_lock_init(&ax->buflock);
521
522 netif_start_queue(dev);
523 return 0;
524
525noxbuff:
526 kfree(ax->rbuff);
527
528norbuff:
529 return -ENOMEM;
530}
531
532
533/* Close the low-level part of the AX25 channel. Easy! */
534static int ax_close(struct net_device *dev)
535{
536 struct ax_disp *ax = netdev_priv(dev);
537
538 if (ax->tty == NULL)
539 return -EBUSY;
540
541 ax->tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP);
542
543 netif_stop_queue(dev);
544
545 return 0;
546}
547
548static int ax25_receive_room(struct tty_struct *tty)
549{
550 return 65536; /* We can handle an infinite amount of data. :-) */
551}
552
553/*
554 * Handle the 'receiver data ready' interrupt.
555 * This function is called by the 'tty_io' module in the kernel when
556 * a block of data has been received, which can now be decapsulated
557 * and sent on to the AX.25 layer for further processing.
558 */
559static void ax25_receive_buf(struct tty_struct *tty, const unsigned char *cp, char *fp, int count)
560{
561 struct ax_disp *ax = (struct ax_disp *) tty->disc_data;
562
563 if (ax == NULL || ax->magic != AX25_MAGIC || !netif_running(ax->dev))
564 return;
565
566 /*
567 * Argh! mtu change time! - costs us the packet part received
568 * at the change
569 */
570 if (ax->mtu != ax->dev->mtu + 73)
571 ax_changedmtu(ax);
572
573 /* Read the characters out of the buffer */
574 while (count--) {
575 if (fp != NULL && *fp++) {
576 if (!test_and_set_bit(AXF_ERROR, &ax->flags))
577 ax->rx_errors++;
578 cp++;
579 continue;
580 }
581
582 kiss_unesc(ax, *cp++);
583 }
584}
585
586static int ax25_open(struct tty_struct *tty)
587{
588 struct ax_disp *ax = (struct ax_disp *) tty->disc_data;
589 int err;
590
591 /* First make sure we're not already connected. */
592 if (ax && ax->magic == AX25_MAGIC)
593 return -EEXIST;
594
595 /* OK. Find a free AX25 channel to use. */
596 if ((ax = ax_alloc()) == NULL)
597 return -ENFILE;
598
599 ax->tty = tty;
600 tty->disc_data = ax;
601
602 if (tty->driver->flush_buffer)
603 tty->driver->flush_buffer(tty);
604
605 /* Restore default settings */
606 ax->dev->type = ARPHRD_AX25;
607
608 /* Perform the low-level AX25 initialization. */
609 if ((err = ax_open(ax->dev)))
610 return err;
611
612 /* Done. We have linked the TTY line to a channel. */
613 return ax->dev->base_addr;
614}
615
616static void ax25_close(struct tty_struct *tty)
617{
618 struct ax_disp *ax = (struct ax_disp *) tty->disc_data;
619
620 /* First make sure we're connected. */
621 if (ax == NULL || ax->magic != AX25_MAGIC)
622 return;
623
624 unregister_netdev(ax->dev);
625
626 tty->disc_data = NULL;
627 ax->tty = NULL;
628
629 ax_free(ax);
630}
631
632
633static struct net_device_stats *ax_get_stats(struct net_device *dev)
634{
635 static struct net_device_stats stats;
636 struct ax_disp *ax = netdev_priv(dev);
637
638 memset(&stats, 0, sizeof(struct net_device_stats));
639
640 stats.rx_packets = ax->rx_packets;
641 stats.tx_packets = ax->tx_packets;
642 stats.rx_bytes = ax->rx_bytes;
643 stats.tx_bytes = ax->tx_bytes;
644 stats.rx_dropped = ax->rx_dropped;
645 stats.tx_dropped = ax->tx_dropped;
646 stats.tx_errors = ax->tx_errors;
647 stats.rx_errors = ax->rx_errors;
648 stats.rx_over_errors = ax->rx_over_errors;
649
650 return &stats;
651}
652
653
654/************************************************************************
655 * STANDARD ENCAPSULATION *
656 ************************************************************************/
657
658static int kiss_esc(unsigned char *s, unsigned char *d, int len)
659{
660 unsigned char *ptr = d;
661 unsigned char c;
662
663 /*
664 * Send an initial END character to flush out any
665 * data that may have accumulated in the receiver
666 * due to line noise.
667 */
668
669 *ptr++ = END;
670
671 while (len-- > 0) {
672 switch (c = *s++) {
673 case END:
674 *ptr++ = ESC;
675 *ptr++ = ESC_END;
676 break;
677 case ESC:
678 *ptr++ = ESC;
679 *ptr++ = ESC_ESC;
680 break;
681 default:
682 *ptr++ = c;
683 break;
684 }
685 }
686
687 *ptr++ = END;
688
689 return ptr - d;
690}
691
692/*
693 * MW:
694 * OK its ugly, but tell me a better solution without copying the
695 * packet to a temporary buffer :-)
696 */
697static int kiss_esc_crc(unsigned char *s, unsigned char *d, unsigned short crc, int len)
698{
699 unsigned char *ptr = d;
700 unsigned char c=0;
701
702 *ptr++ = END;
703 while (len > 0) {
704 if (len > 2)
705 c = *s++;
706 else if (len > 1)
707 c = crc >> 8;
708 else if (len > 0)
709 c = crc & 0xff;
710
711 len--;
712
713 switch (c) {
714 case END:
715 *ptr++ = ESC;
716 *ptr++ = ESC_END;
717 break;
718 case ESC:
719 *ptr++ = ESC;
720 *ptr++ = ESC_ESC;
721 break;
722 default:
723 *ptr++ = c;
724 break;
725 }
726 }
727 *ptr++ = END;
728 return ptr - d;
729}
730
731static void kiss_unesc(struct ax_disp *ax, unsigned char s)
732{
733 switch (s) {
734 case END:
735 /* drop keeptest bit = VSV */
736 if (test_bit(AXF_KEEPTEST, &ax->flags))
737 clear_bit(AXF_KEEPTEST, &ax->flags);
738
739 if (!test_and_clear_bit(AXF_ERROR, &ax->flags) && (ax->rcount > 2))
740 ax_bump(ax);
741
742 clear_bit(AXF_ESCAPE, &ax->flags);
743 ax->rcount = 0;
744 return;
745
746 case ESC:
747 set_bit(AXF_ESCAPE, &ax->flags);
748 return;
749 case ESC_ESC:
750 if (test_and_clear_bit(AXF_ESCAPE, &ax->flags))
751 s = ESC;
752 break;
753 case ESC_END:
754 if (test_and_clear_bit(AXF_ESCAPE, &ax->flags))
755 s = END;
756 break;
757 }
758
759 spin_lock_bh(&ax->buflock);
760 if (!test_bit(AXF_ERROR, &ax->flags)) {
761 if (ax->rcount < ax->buffsize) {
762 ax->rbuff[ax->rcount++] = s;
763 spin_unlock_bh(&ax->buflock);
764 return;
765 }
766
767 ax->rx_over_errors++;
768 set_bit(AXF_ERROR, &ax->flags);
769 }
770 spin_unlock_bh(&ax->buflock);
771}
772
773
774static int ax_set_mac_address(struct net_device *dev, void __user *addr)
775{
776 if (copy_from_user(dev->dev_addr, addr, AX25_ADDR_LEN))
777 return -EFAULT;
778 return 0;
779}
780
781static int ax_set_dev_mac_address(struct net_device *dev, void *addr)
782{
783 struct sockaddr *sa = addr;
784
785 memcpy(dev->dev_addr, sa->sa_data, AX25_ADDR_LEN);
786
787 return 0;
788}
789
790
791/* Perform I/O control on an active ax25 channel. */
792static int ax25_disp_ioctl(struct tty_struct *tty, void *file, int cmd, void __user *arg)
793{
794 struct ax_disp *ax = (struct ax_disp *) tty->disc_data;
795 unsigned int tmp;
796
797 /* First make sure we're connected. */
798 if (ax == NULL || ax->magic != AX25_MAGIC)
799 return -EINVAL;
800
801 switch (cmd) {
802 case SIOCGIFNAME:
803 if (copy_to_user(arg, ax->dev->name, strlen(ax->dev->name) + 1))
804 return -EFAULT;
805 return 0;
806
807 case SIOCGIFENCAP:
808 return put_user(4, (int __user *)arg);
809
810 case SIOCSIFENCAP:
811 if (get_user(tmp, (int __user *)arg))
812 return -EFAULT;
813 ax->mode = tmp;
814 ax->dev->addr_len = AX25_ADDR_LEN; /* sizeof an AX.25 addr */
815 ax->dev->hard_header_len = AX25_KISS_HEADER_LEN + AX25_MAX_HEADER_LEN + 3;
816 ax->dev->type = ARPHRD_AX25;
817 return 0;
818
819 case SIOCSIFHWADDR:
820 return ax_set_mac_address(ax->dev, arg);
821
822 default:
823 return -ENOIOCTLCMD;
824 }
825}
826
827static int ax_open_dev(struct net_device *dev)
828{
829 struct ax_disp *ax = netdev_priv(dev);
830
831 if (ax->tty == NULL)
832 return -ENODEV;
833
834 return 0;
835}
836
837
838/* Initialize the driver. Called by network startup. */
839static int ax25_init(struct net_device *dev)
840{
841 struct ax_disp *ax = netdev_priv(dev);
842
843 static char ax25_bcast[AX25_ADDR_LEN] =
844 {'Q'<<1,'S'<<1,'T'<<1,' '<<1,' '<<1,' '<<1,'0'<<1};
845 static char ax25_test[AX25_ADDR_LEN] =
846 {'L'<<1,'I'<<1,'N'<<1,'U'<<1,'X'<<1,' '<<1,'1'<<1};
847
848 if (ax == NULL) /* Allocation failed ?? */
849 return -ENODEV;
850
851 /* Set up the "AX25 Control Block". (And clear statistics) */
852 memset(ax, 0, sizeof (struct ax_disp));
853 ax->magic = AX25_MAGIC;
854 ax->dev = dev;
855
856 /* Finish setting up the DEVICE info. */
857 dev->mtu = AX_MTU;
858 dev->hard_start_xmit = ax_xmit;
859 dev->open = ax_open_dev;
860 dev->stop = ax_close;
861 dev->get_stats = ax_get_stats;
862 dev->set_mac_address = ax_set_dev_mac_address;
863 dev->hard_header_len = 0;
864 dev->addr_len = 0;
865 dev->type = ARPHRD_AX25;
866 dev->tx_queue_len = 10;
867 dev->hard_header = ax_header;
868 dev->rebuild_header = ax_rebuild_header;
869
870 memcpy(dev->broadcast, ax25_bcast, AX25_ADDR_LEN);
871 memcpy(dev->dev_addr, ax25_test, AX25_ADDR_LEN);
872
873 /* New-style flags. */
874 dev->flags = IFF_BROADCAST | IFF_MULTICAST;
875
876 return 0;
877}
878
879
880/* ******************************************************************** */
881/* * Init MKISS driver * */
882/* ******************************************************************** */
883
884static int __init mkiss_init_driver(void)
885{
886 int status;
887
888 printk(banner);
889
890 if (ax25_maxdev < 4)
891 ax25_maxdev = 4; /* Sanity */
892
893 if ((ax25_ctrls = kmalloc(sizeof(void *) * ax25_maxdev, GFP_KERNEL)) == NULL) {
894 printk(KERN_ERR "mkiss: Can't allocate ax25_ctrls[] array!\n");
895 return -ENOMEM;
896 }
897
898 /* Clear the pointer array, we allocate devices when we need them */
899 memset(ax25_ctrls, 0, sizeof(void*) * ax25_maxdev); /* Pointers */
900
901 /* Fill in our line protocol discipline, and register it */
902 ax_ldisc.magic = TTY_LDISC_MAGIC;
903 ax_ldisc.name = "mkiss";
904 ax_ldisc.open = ax25_open;
905 ax_ldisc.close = ax25_close;
906 ax_ldisc.ioctl = (int (*)(struct tty_struct *, struct file *,
907 unsigned int, unsigned long))ax25_disp_ioctl;
908 ax_ldisc.receive_buf = ax25_receive_buf;
909 ax_ldisc.receive_room = ax25_receive_room;
910 ax_ldisc.write_wakeup = ax25_write_wakeup;
911
912 if ((status = tty_register_ldisc(N_AX25, &ax_ldisc)) != 0) {
913 printk(KERN_ERR "mkiss: can't register line discipline (err = %d)\n", status);
914 kfree(ax25_ctrls);
915 }
916 return status;
917}
918
919static void __exit mkiss_exit_driver(void)
920{
921 int i;
922
923 for (i = 0; i < ax25_maxdev; i++) {
924 if (ax25_ctrls[i]) {
925 /*
926 * VSV = if dev->start==0, then device
927 * unregistered while close proc.
928 */
929 if (netif_running(&ax25_ctrls[i]->dev))
930 unregister_netdev(&ax25_ctrls[i]->dev);
931 kfree(ax25_ctrls[i]);
932 }
933 }
934
935 kfree(ax25_ctrls);
936 ax25_ctrls = NULL;
937
Alexey Dobriyan64ccd712005-06-23 00:10:33 -0700938 if ((i = tty_unregister_ldisc(N_AX25)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 printk(KERN_ERR "mkiss: can't unregister line discipline (err = %d)\n", i);
940}
941
942MODULE_AUTHOR("Hans Albas PE1AYX <hans@esrac.ele.tue.nl>");
943MODULE_DESCRIPTION("KISS driver for AX.25 over TTYs");
944MODULE_PARM(ax25_maxdev, "i");
945MODULE_PARM_DESC(ax25_maxdev, "number of MKISS devices");
946MODULE_LICENSE("GPL");
947MODULE_ALIAS_LDISC(N_AX25);
948module_init(mkiss_init_driver);
949module_exit(mkiss_exit_driver);
950