diff options
Diffstat (limited to 'drivers/net/hamradio/mkiss.c')
-rw-r--r-- | drivers/net/hamradio/mkiss.c | 164 |
1 files changed, 141 insertions, 23 deletions
diff --git a/drivers/net/hamradio/mkiss.c b/drivers/net/hamradio/mkiss.c index 21cdbf5c6..4d7b96c40 100644 --- a/drivers/net/hamradio/mkiss.c +++ b/drivers/net/hamradio/mkiss.c @@ -10,7 +10,7 @@ * This module implements the AX.25 protocol for kernel-based * devices like TTYs. It interfaces between a raw TTY, and the * kernel's AX.25 protocol layers, just like slip.c. - * AX.25 needs to be seperated from slip.c while slip.c is no + * AX.25 needs to be separated from slip.c while slip.c is no * longer a static kernel device since it is a module. * This method clears the way to implement other kiss protocols * like mkiss smack g8bpq ..... so far only mkiss is implemented. @@ -19,6 +19,9 @@ * * History * Jonathan (G4KLX) Fixed to match Linux networking changes - 2.1.15. + * Matthias (DG2FEF) Added support for FlexNet CRC (on special request) + * Fixed bug in ax25_close(): dev_lock_wait() was + * called twice, causing a deadlock. */ #include <linux/config.h> @@ -91,8 +94,80 @@ static int ax25_init(struct device *); static int mkiss_init(void); static int mkiss_write(struct tty_struct *, int, const unsigned char *, int); static int kiss_esc(unsigned char *, unsigned char *, int); +static int kiss_esc_crc(unsigned char *, unsigned char *, unsigned short, int); static void kiss_unesc(struct ax_disp *, unsigned char); +/*---------------------------------------------------------------------------*/ + +static const unsigned short Crc_flex_table[] = { + 0x0f87, 0x1e0e, 0x2c95, 0x3d1c, 0x49a3, 0x582a, 0x6ab1, 0x7b38, + 0x83cf, 0x9246, 0xa0dd, 0xb154, 0xc5eb, 0xd462, 0xe6f9, 0xf770, + 0x1f06, 0x0e8f, 0x3c14, 0x2d9d, 0x5922, 0x48ab, 0x7a30, 0x6bb9, + 0x934e, 0x82c7, 0xb05c, 0xa1d5, 0xd56a, 0xc4e3, 0xf678, 0xe7f1, + 0x2e85, 0x3f0c, 0x0d97, 0x1c1e, 0x68a1, 0x7928, 0x4bb3, 0x5a3a, + 0xa2cd, 0xb344, 0x81df, 0x9056, 0xe4e9, 0xf560, 0xc7fb, 0xd672, + 0x3e04, 0x2f8d, 0x1d16, 0x0c9f, 0x7820, 0x69a9, 0x5b32, 0x4abb, + 0xb24c, 0xa3c5, 0x915e, 0x80d7, 0xf468, 0xe5e1, 0xd77a, 0xc6f3, + 0x4d83, 0x5c0a, 0x6e91, 0x7f18, 0x0ba7, 0x1a2e, 0x28b5, 0x393c, + 0xc1cb, 0xd042, 0xe2d9, 0xf350, 0x87ef, 0x9666, 0xa4fd, 0xb574, + 0x5d02, 0x4c8b, 0x7e10, 0x6f99, 0x1b26, 0x0aaf, 0x3834, 0x29bd, + 0xd14a, 0xc0c3, 0xf258, 0xe3d1, 0x976e, 0x86e7, 0xb47c, 0xa5f5, + 0x6c81, 0x7d08, 0x4f93, 0x5e1a, 0x2aa5, 0x3b2c, 0x09b7, 0x183e, + 0xe0c9, 0xf140, 0xc3db, 0xd252, 0xa6ed, 0xb764, 0x85ff, 0x9476, + 0x7c00, 0x6d89, 0x5f12, 0x4e9b, 0x3a24, 0x2bad, 0x1936, 0x08bf, + 0xf048, 0xe1c1, 0xd35a, 0xc2d3, 0xb66c, 0xa7e5, 0x957e, 0x84f7, + 0x8b8f, 0x9a06, 0xa89d, 0xb914, 0xcdab, 0xdc22, 0xeeb9, 0xff30, + 0x07c7, 0x164e, 0x24d5, 0x355c, 0x41e3, 0x506a, 0x62f1, 0x7378, + 0x9b0e, 0x8a87, 0xb81c, 0xa995, 0xdd2a, 0xcca3, 0xfe38, 0xefb1, + 0x1746, 0x06cf, 0x3454, 0x25dd, 0x5162, 0x40eb, 0x7270, 0x63f9, + 0xaa8d, 0xbb04, 0x899f, 0x9816, 0xeca9, 0xfd20, 0xcfbb, 0xde32, + 0x26c5, 0x374c, 0x05d7, 0x145e, 0x60e1, 0x7168, 0x43f3, 0x527a, + 0xba0c, 0xab85, 0x991e, 0x8897, 0xfc28, 0xeda1, 0xdf3a, 0xceb3, + 0x3644, 0x27cd, 0x1556, 0x04df, 0x7060, 0x61e9, 0x5372, 0x42fb, + 0xc98b, 0xd802, 0xea99, 0xfb10, 0x8faf, 0x9e26, 0xacbd, 0xbd34, + 0x45c3, 0x544a, 0x66d1, 0x7758, 0x03e7, 0x126e, 0x20f5, 0x317c, + 0xd90a, 0xc883, 0xfa18, 0xeb91, 0x9f2e, 0x8ea7, 0xbc3c, 0xadb5, + 0x5542, 0x44cb, 0x7650, 0x67d9, 0x1366, 0x02ef, 0x3074, 0x21fd, + 0xe889, 0xf900, 0xcb9b, 0xda12, 0xaead, 0xbf24, 0x8dbf, 0x9c36, + 0x64c1, 0x7548, 0x47d3, 0x565a, 0x22e5, 0x336c, 0x01f7, 0x107e, + 0xf808, 0xe981, 0xdb1a, 0xca93, 0xbe2c, 0xafa5, 0x9d3e, 0x8cb7, + 0x7440, 0x65c9, 0x5752, 0x46db, 0x3264, 0x23ed, 0x1176, 0x00ff +}; + +/*---------------------------------------------------------------------------*/ + +static unsigned short +calc_crc_flex(unsigned char *cp, int size) +{ + unsigned short crc = 0xffff; + + while (size--) + crc = (crc << 8) ^ Crc_flex_table[((crc >> 8) ^ *cp++) & 0xff]; + + return crc; +} + +/*---------------------------------------------------------------------------*/ + +static int +check_crc_flex(unsigned char *cp, int size) +{ + unsigned short crc = 0xffff; + + if (size < 3) + return -1; + + while (size--) + crc = (crc << 8) ^ Crc_flex_table[((crc >> 8) ^ *cp++) & 0xff]; + + if ((crc & 0xffff) != 0x7070) + return -1; + + return 0; +} + +/*---------------------------------------------------------------------------*/ + /* Find a free channel, and link in this `tty' line. */ static inline struct ax_disp *ax_alloc(void) { @@ -272,6 +347,13 @@ static void ax_bump(struct ax_disp *ax) mkiss= ax->mkiss->tty->driver_data; if (mkiss->magic == MKISS_DRIVER_MAGIC) tmp_ax = ax->mkiss; + } else if (ax->rbuff[0] & 0x20) { + ax->crcmode = CRC_MODE_FLEX; + if (check_crc_flex(ax->rbuff, ax->rcount) < 0) { + ax->rx_errors++; + return; + } + ax->rcount -= 2; } } @@ -312,7 +394,19 @@ static void ax_encaps(struct ax_disp *ax, unsigned char *icp, int len) p = icp; if (mkiss->magic != MKISS_DRIVER_MAGIC) { - count = kiss_esc(p, (unsigned char *)ax->xbuff, len); + switch (ax->crcmode) { + unsigned short crc; + + case CRC_MODE_FLEX: + *p |= 0x20; + crc = calc_crc_flex(p, len); + count = kiss_esc_crc(p, (unsigned char *)ax->xbuff, crc, len+2); + break; + + default: + count = kiss_esc(p, (unsigned char *)ax->xbuff, len); + break; + } ax->tty->flags |= (1 << TTY_DO_WRITE_WAKEUP); actual = ax->tty->driver.write(ax->tty, 0, ax->xbuff, count); ax->tx_packets++; @@ -629,12 +723,8 @@ static void ax25_close(struct tty_struct *tty) return; mkiss = ax->mode; - if (ax->dev->flags & IFF_UP) - { - dev_lock_wait(); - dev_close(ax->dev); - dev_unlock_list(); - } + + dev_close(ax->dev); tty->disc_data = 0; ax->tty = NULL; @@ -704,6 +794,45 @@ int kiss_esc(unsigned char *s, unsigned char *d, int len) return ptr - d; } +/* + * MW: + * OK its ugly, but tell me a better solution without copying the + * packet to a temporary buffer :-) + */ +static int kiss_esc_crc(unsigned char *s, unsigned char *d, unsigned short crc, int len) +{ + unsigned char *ptr = d; + unsigned char c; + + *ptr++ = END; + while (len > 0) { + if (len > 2) + c = *s++; + else if (len > 1) + c = crc >> 8; + else if (len > 0) + c = crc & 0xff; + + len--; + + switch (c) { + case END: + *ptr++ = ESC; + *ptr++ = ESC_END; + break; + case ESC: + *ptr++ = ESC; + *ptr++ = ESC_ESC; + break; + default: + *ptr++ = c; + break; + } + } + *ptr++ = END; + return ptr - d; +} + static void kiss_unesc(struct ax_disp *ax, unsigned char s) { switch (s) { @@ -746,14 +875,8 @@ static void kiss_unesc(struct ax_disp *ax, unsigned char s) int ax_set_mac_address(struct device *dev, void *addr) { - int err; - - if ((err = verify_area(VERIFY_READ, addr, AX25_ADDR_LEN)) != 0) - return err; - - /* addr is an AX.25 shifted ASCII mac address */ - copy_from_user(dev->dev_addr, addr, AX25_ADDR_LEN); - + if (copy_from_user(dev->dev_addr, addr, AX25_ADDR_LEN)) + return -EFAULT; return 0; } @@ -780,20 +903,15 @@ static int ax25_disp_ioctl(struct tty_struct *tty, void *file, int cmd, void *ar switch (cmd) { case SIOCGIFNAME: - if ((err = verify_area(VERIFY_WRITE, arg, strlen(ax->dev->name) + 1)) != 0) - return err; - copy_to_user(arg, ax->dev->name, strlen(ax->dev->name) + 1); + if (copy_to_user(arg, ax->dev->name, strlen(ax->dev->name) + 1)) + return -EFAULT; return 0; case SIOCGIFENCAP: - if ((err = verify_area(VERIFY_WRITE, arg, sizeof(int))) != 0) - return err; put_user(4, (int *)arg); return 0; case SIOCSIFENCAP: - if ((err = verify_area(VERIFY_READ, arg, sizeof(int))) != 0) - return err; get_user(tmp, (int *)arg); ax->mode = tmp; ax->dev->addr_len = AX25_ADDR_LEN; /* sizeof an AX.25 addr */ |