Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  *
0004  * Copyright (C) Hans Alblas PE1AYX <hans@esrac.ele.tue.nl>
0005  * Copyright (C) 2004, 05 Ralf Baechle DL5RB <ralf@linux-mips.org>
0006  * Copyright (C) 2004, 05 Thomas Osterried DL9SAU <thomas@x-berg.in-berlin.de>
0007  */
0008 #include <linux/module.h>
0009 #include <linux/bitops.h>
0010 #include <linux/uaccess.h>
0011 #include <linux/crc16.h>
0012 #include <linux/string.h>
0013 #include <linux/mm.h>
0014 #include <linux/interrupt.h>
0015 #include <linux/in.h>
0016 #include <linux/inet.h>
0017 #include <linux/slab.h>
0018 #include <linux/tty.h>
0019 #include <linux/errno.h>
0020 #include <linux/netdevice.h>
0021 #include <linux/major.h>
0022 #include <linux/init.h>
0023 #include <linux/rtnetlink.h>
0024 #include <linux/etherdevice.h>
0025 #include <linux/skbuff.h>
0026 #include <linux/if_arp.h>
0027 #include <linux/jiffies.h>
0028 #include <linux/refcount.h>
0029 
0030 #include <net/ax25.h>
0031 
0032 #define AX_MTU      236
0033 
0034 /* some arch define END as assembly function ending, just undef it */
0035 #undef  END
0036 /* SLIP/KISS protocol characters. */
0037 #define END             0300        /* indicates end of frame   */
0038 #define ESC             0333        /* indicates byte stuffing  */
0039 #define ESC_END         0334        /* ESC ESC_END means END 'data' */
0040 #define ESC_ESC         0335        /* ESC ESC_ESC means ESC 'data' */
0041 
0042 struct mkiss {
0043     struct tty_struct   *tty;   /* ptr to TTY structure     */
0044     struct net_device   *dev;   /* easy for intr handling   */
0045 
0046     /* These are pointers to the malloc()ed frame buffers. */
0047     spinlock_t      buflock;/* lock for rbuf and xbuf */
0048     unsigned char       *rbuff; /* receiver buffer      */
0049     int         rcount; /* received chars counter       */
0050     unsigned char       *xbuff; /* transmitter buffer       */
0051     unsigned char       *xhead; /* pointer to next byte to XMIT */
0052     int         xleft;  /* bytes left in XMIT queue     */
0053 
0054     /* Detailed SLIP statistics. */
0055     int     mtu;        /* Our mtu (to spot changes!)   */
0056     int     buffsize;   /* Max buffers sizes            */
0057 
0058     unsigned long   flags;      /* Flag values/ mode etc    */
0059                     /* long req'd: used by set_bit --RR */
0060 #define AXF_INUSE   0       /* Channel in use               */
0061 #define AXF_ESCAPE  1               /* ESC received                 */
0062 #define AXF_ERROR   2               /* Parity, etc. error           */
0063 #define AXF_KEEPTEST    3       /* Keepalive test flag      */
0064 #define AXF_OUTWAIT 4       /* is outpacket was flag    */
0065 
0066     int     mode;
0067         int     crcmode;    /* MW: for FlexNet, SMACK etc.  */
0068     int     crcauto;    /* CRC auto mode */
0069 
0070 #define CRC_MODE_NONE       0
0071 #define CRC_MODE_FLEX       1
0072 #define CRC_MODE_SMACK      2
0073 #define CRC_MODE_FLEX_TEST  3
0074 #define CRC_MODE_SMACK_TEST 4
0075 
0076     refcount_t      refcnt;
0077     struct completion   dead;
0078 };
0079 
0080 /*---------------------------------------------------------------------------*/
0081 
0082 static const unsigned short crc_flex_table[] = {
0083     0x0f87, 0x1e0e, 0x2c95, 0x3d1c, 0x49a3, 0x582a, 0x6ab1, 0x7b38,
0084     0x83cf, 0x9246, 0xa0dd, 0xb154, 0xc5eb, 0xd462, 0xe6f9, 0xf770,
0085     0x1f06, 0x0e8f, 0x3c14, 0x2d9d, 0x5922, 0x48ab, 0x7a30, 0x6bb9,
0086     0x934e, 0x82c7, 0xb05c, 0xa1d5, 0xd56a, 0xc4e3, 0xf678, 0xe7f1,
0087     0x2e85, 0x3f0c, 0x0d97, 0x1c1e, 0x68a1, 0x7928, 0x4bb3, 0x5a3a,
0088     0xa2cd, 0xb344, 0x81df, 0x9056, 0xe4e9, 0xf560, 0xc7fb, 0xd672,
0089     0x3e04, 0x2f8d, 0x1d16, 0x0c9f, 0x7820, 0x69a9, 0x5b32, 0x4abb,
0090     0xb24c, 0xa3c5, 0x915e, 0x80d7, 0xf468, 0xe5e1, 0xd77a, 0xc6f3,
0091     0x4d83, 0x5c0a, 0x6e91, 0x7f18, 0x0ba7, 0x1a2e, 0x28b5, 0x393c,
0092     0xc1cb, 0xd042, 0xe2d9, 0xf350, 0x87ef, 0x9666, 0xa4fd, 0xb574,
0093     0x5d02, 0x4c8b, 0x7e10, 0x6f99, 0x1b26, 0x0aaf, 0x3834, 0x29bd,
0094     0xd14a, 0xc0c3, 0xf258, 0xe3d1, 0x976e, 0x86e7, 0xb47c, 0xa5f5,
0095     0x6c81, 0x7d08, 0x4f93, 0x5e1a, 0x2aa5, 0x3b2c, 0x09b7, 0x183e,
0096     0xe0c9, 0xf140, 0xc3db, 0xd252, 0xa6ed, 0xb764, 0x85ff, 0x9476,
0097     0x7c00, 0x6d89, 0x5f12, 0x4e9b, 0x3a24, 0x2bad, 0x1936, 0x08bf,
0098     0xf048, 0xe1c1, 0xd35a, 0xc2d3, 0xb66c, 0xa7e5, 0x957e, 0x84f7,
0099     0x8b8f, 0x9a06, 0xa89d, 0xb914, 0xcdab, 0xdc22, 0xeeb9, 0xff30,
0100     0x07c7, 0x164e, 0x24d5, 0x355c, 0x41e3, 0x506a, 0x62f1, 0x7378,
0101     0x9b0e, 0x8a87, 0xb81c, 0xa995, 0xdd2a, 0xcca3, 0xfe38, 0xefb1,
0102     0x1746, 0x06cf, 0x3454, 0x25dd, 0x5162, 0x40eb, 0x7270, 0x63f9,
0103     0xaa8d, 0xbb04, 0x899f, 0x9816, 0xeca9, 0xfd20, 0xcfbb, 0xde32,
0104     0x26c5, 0x374c, 0x05d7, 0x145e, 0x60e1, 0x7168, 0x43f3, 0x527a,
0105     0xba0c, 0xab85, 0x991e, 0x8897, 0xfc28, 0xeda1, 0xdf3a, 0xceb3,
0106     0x3644, 0x27cd, 0x1556, 0x04df, 0x7060, 0x61e9, 0x5372, 0x42fb,
0107     0xc98b, 0xd802, 0xea99, 0xfb10, 0x8faf, 0x9e26, 0xacbd, 0xbd34,
0108     0x45c3, 0x544a, 0x66d1, 0x7758, 0x03e7, 0x126e, 0x20f5, 0x317c,
0109     0xd90a, 0xc883, 0xfa18, 0xeb91, 0x9f2e, 0x8ea7, 0xbc3c, 0xadb5,
0110     0x5542, 0x44cb, 0x7650, 0x67d9, 0x1366, 0x02ef, 0x3074, 0x21fd,
0111     0xe889, 0xf900, 0xcb9b, 0xda12, 0xaead, 0xbf24, 0x8dbf, 0x9c36,
0112     0x64c1, 0x7548, 0x47d3, 0x565a, 0x22e5, 0x336c, 0x01f7, 0x107e,
0113     0xf808, 0xe981, 0xdb1a, 0xca93, 0xbe2c, 0xafa5, 0x9d3e, 0x8cb7,
0114     0x7440, 0x65c9, 0x5752, 0x46db, 0x3264, 0x23ed, 0x1176, 0x00ff
0115 };
0116 
0117 static unsigned short calc_crc_flex(unsigned char *cp, int size)
0118 {
0119     unsigned short crc = 0xffff;
0120 
0121     while (size--)
0122         crc = (crc << 8) ^ crc_flex_table[((crc >> 8) ^ *cp++) & 0xff];
0123 
0124     return crc;
0125 }
0126 
0127 static int check_crc_flex(unsigned char *cp, int size)
0128 {
0129     unsigned short crc = 0xffff;
0130 
0131     if (size < 3)
0132         return -1;
0133 
0134     while (size--)
0135         crc = (crc << 8) ^ crc_flex_table[((crc >> 8) ^ *cp++) & 0xff];
0136 
0137     if ((crc & 0xffff) != 0x7070)
0138         return -1;
0139 
0140     return 0;
0141 }
0142 
0143 static int check_crc_16(unsigned char *cp, int size)
0144 {
0145     unsigned short crc = 0x0000;
0146 
0147     if (size < 3)
0148         return -1;
0149 
0150     crc = crc16(0, cp, size);
0151 
0152     if (crc != 0x0000)
0153         return -1;
0154 
0155     return 0;
0156 }
0157 
0158 /*
0159  * Standard encapsulation
0160  */
0161 
0162 static int kiss_esc(unsigned char *s, unsigned char *d, int len)
0163 {
0164     unsigned char *ptr = d;
0165     unsigned char c;
0166 
0167     /*
0168      * Send an initial END character to flush out any data that may have
0169      * accumulated in the receiver due to line noise.
0170      */
0171 
0172     *ptr++ = END;
0173 
0174     while (len-- > 0) {
0175         switch (c = *s++) {
0176         case END:
0177             *ptr++ = ESC;
0178             *ptr++ = ESC_END;
0179             break;
0180         case ESC:
0181             *ptr++ = ESC;
0182             *ptr++ = ESC_ESC;
0183             break;
0184         default:
0185             *ptr++ = c;
0186             break;
0187         }
0188     }
0189 
0190     *ptr++ = END;
0191 
0192     return ptr - d;
0193 }
0194 
0195 /*
0196  * MW:
0197  * OK its ugly, but tell me a better solution without copying the
0198  * packet to a temporary buffer :-)
0199  */
0200 static int kiss_esc_crc(unsigned char *s, unsigned char *d, unsigned short crc,
0201     int len)
0202 {
0203     unsigned char *ptr = d;
0204     unsigned char c=0;
0205 
0206     *ptr++ = END;
0207     while (len > 0) {
0208         if (len > 2)
0209             c = *s++;
0210         else if (len > 1)
0211             c = crc >> 8;
0212         else
0213             c = crc & 0xff;
0214 
0215         len--;
0216 
0217         switch (c) {
0218         case END:
0219             *ptr++ = ESC;
0220             *ptr++ = ESC_END;
0221             break;
0222         case ESC:
0223             *ptr++ = ESC;
0224             *ptr++ = ESC_ESC;
0225             break;
0226         default:
0227             *ptr++ = c;
0228             break;
0229         }
0230     }
0231     *ptr++ = END;
0232 
0233     return ptr - d;
0234 }
0235 
0236 /* Send one completely decapsulated AX.25 packet to the AX.25 layer. */
0237 static void ax_bump(struct mkiss *ax)
0238 {
0239     struct sk_buff *skb;
0240     int count;
0241 
0242     spin_lock_bh(&ax->buflock);
0243     if (ax->rbuff[0] > 0x0f) {
0244         if (ax->rbuff[0] & 0x80) {
0245             if (check_crc_16(ax->rbuff, ax->rcount) < 0) {
0246                 ax->dev->stats.rx_errors++;
0247                 spin_unlock_bh(&ax->buflock);
0248 
0249                 return;
0250             }
0251             if (ax->crcmode != CRC_MODE_SMACK && ax->crcauto) {
0252                 printk(KERN_INFO
0253                        "mkiss: %s: Switching to crc-smack\n",
0254                        ax->dev->name);
0255                 ax->crcmode = CRC_MODE_SMACK;
0256             }
0257             ax->rcount -= 2;
0258             *ax->rbuff &= ~0x80;
0259         } else if (ax->rbuff[0] & 0x20)  {
0260             if (check_crc_flex(ax->rbuff, ax->rcount) < 0) {
0261                 ax->dev->stats.rx_errors++;
0262                 spin_unlock_bh(&ax->buflock);
0263                 return;
0264             }
0265             if (ax->crcmode != CRC_MODE_FLEX && ax->crcauto) {
0266                 printk(KERN_INFO
0267                        "mkiss: %s: Switching to crc-flexnet\n",
0268                        ax->dev->name);
0269                 ax->crcmode = CRC_MODE_FLEX;
0270             }
0271             ax->rcount -= 2;
0272 
0273             /*
0274              * dl9sau bugfix: the trailling two bytes flexnet crc
0275              * will not be passed to the kernel. thus we have to
0276              * correct the kissparm signature, because it indicates
0277              * a crc but there's none
0278              */
0279             *ax->rbuff &= ~0x20;
0280         }
0281     }
0282 
0283     count = ax->rcount;
0284 
0285     if ((skb = dev_alloc_skb(count)) == NULL) {
0286         printk(KERN_ERR "mkiss: %s: memory squeeze, dropping packet.\n",
0287                ax->dev->name);
0288         ax->dev->stats.rx_dropped++;
0289         spin_unlock_bh(&ax->buflock);
0290         return;
0291     }
0292 
0293     skb_put_data(skb, ax->rbuff, count);
0294     skb->protocol = ax25_type_trans(skb, ax->dev);
0295     netif_rx(skb);
0296     ax->dev->stats.rx_packets++;
0297     ax->dev->stats.rx_bytes += count;
0298     spin_unlock_bh(&ax->buflock);
0299 }
0300 
0301 static void kiss_unesc(struct mkiss *ax, unsigned char s)
0302 {
0303     switch (s) {
0304     case END:
0305         /* drop keeptest bit = VSV */
0306         if (test_bit(AXF_KEEPTEST, &ax->flags))
0307             clear_bit(AXF_KEEPTEST, &ax->flags);
0308 
0309         if (!test_and_clear_bit(AXF_ERROR, &ax->flags) && (ax->rcount > 2))
0310             ax_bump(ax);
0311 
0312         clear_bit(AXF_ESCAPE, &ax->flags);
0313         ax->rcount = 0;
0314         return;
0315 
0316     case ESC:
0317         set_bit(AXF_ESCAPE, &ax->flags);
0318         return;
0319     case ESC_ESC:
0320         if (test_and_clear_bit(AXF_ESCAPE, &ax->flags))
0321             s = ESC;
0322         break;
0323     case ESC_END:
0324         if (test_and_clear_bit(AXF_ESCAPE, &ax->flags))
0325             s = END;
0326         break;
0327     }
0328 
0329     spin_lock_bh(&ax->buflock);
0330     if (!test_bit(AXF_ERROR, &ax->flags)) {
0331         if (ax->rcount < ax->buffsize) {
0332             ax->rbuff[ax->rcount++] = s;
0333             spin_unlock_bh(&ax->buflock);
0334             return;
0335         }
0336 
0337         ax->dev->stats.rx_over_errors++;
0338         set_bit(AXF_ERROR, &ax->flags);
0339     }
0340     spin_unlock_bh(&ax->buflock);
0341 }
0342 
0343 static int ax_set_mac_address(struct net_device *dev, void *addr)
0344 {
0345     struct sockaddr_ax25 *sa = addr;
0346 
0347     netif_tx_lock_bh(dev);
0348     netif_addr_lock(dev);
0349     __dev_addr_set(dev, &sa->sax25_call, AX25_ADDR_LEN);
0350     netif_addr_unlock(dev);
0351     netif_tx_unlock_bh(dev);
0352 
0353     return 0;
0354 }
0355 
0356 /*---------------------------------------------------------------------------*/
0357 
0358 static void ax_changedmtu(struct mkiss *ax)
0359 {
0360     struct net_device *dev = ax->dev;
0361     unsigned char *xbuff, *rbuff, *oxbuff, *orbuff;
0362     int len;
0363 
0364     len = dev->mtu * 2;
0365 
0366     /*
0367      * allow for arrival of larger UDP packets, even if we say not to
0368      * also fixes a bug in which SunOS sends 512-byte packets even with
0369      * an MSS of 128
0370      */
0371     if (len < 576 * 2)
0372         len = 576 * 2;
0373 
0374     xbuff = kmalloc(len + 4, GFP_ATOMIC);
0375     rbuff = kmalloc(len + 4, GFP_ATOMIC);
0376 
0377     if (xbuff == NULL || rbuff == NULL)  {
0378         printk(KERN_ERR "mkiss: %s: unable to grow ax25 buffers, "
0379                "MTU change cancelled.\n",
0380                ax->dev->name);
0381         dev->mtu = ax->mtu;
0382         kfree(xbuff);
0383         kfree(rbuff);
0384         return;
0385     }
0386 
0387     spin_lock_bh(&ax->buflock);
0388 
0389     oxbuff    = ax->xbuff;
0390     ax->xbuff = xbuff;
0391     orbuff    = ax->rbuff;
0392     ax->rbuff = rbuff;
0393 
0394     if (ax->xleft) {
0395         if (ax->xleft <= len) {
0396             memcpy(ax->xbuff, ax->xhead, ax->xleft);
0397         } else  {
0398             ax->xleft = 0;
0399             dev->stats.tx_dropped++;
0400         }
0401     }
0402 
0403     ax->xhead = ax->xbuff;
0404 
0405     if (ax->rcount) {
0406         if (ax->rcount <= len) {
0407             memcpy(ax->rbuff, orbuff, ax->rcount);
0408         } else  {
0409             ax->rcount = 0;
0410             dev->stats.rx_over_errors++;
0411             set_bit(AXF_ERROR, &ax->flags);
0412         }
0413     }
0414 
0415     ax->mtu      = dev->mtu + 73;
0416     ax->buffsize = len;
0417 
0418     spin_unlock_bh(&ax->buflock);
0419 
0420     kfree(oxbuff);
0421     kfree(orbuff);
0422 }
0423 
0424 /* Encapsulate one AX.25 packet and stuff into a TTY queue. */
0425 static void ax_encaps(struct net_device *dev, unsigned char *icp, int len)
0426 {
0427     struct mkiss *ax = netdev_priv(dev);
0428     unsigned char *p;
0429     int actual, count;
0430 
0431     if (ax->mtu != ax->dev->mtu + 73)   /* Someone has been ifconfigging */
0432         ax_changedmtu(ax);
0433 
0434     if (len > ax->mtu) {        /* Sigh, shouldn't occur BUT ... */
0435         printk(KERN_ERR "mkiss: %s: truncating oversized transmit packet!\n", ax->dev->name);
0436         dev->stats.tx_dropped++;
0437         netif_start_queue(dev);
0438         return;
0439     }
0440 
0441     p = icp;
0442 
0443     spin_lock_bh(&ax->buflock);
0444     if ((*p & 0x0f) != 0) {
0445         /* Configuration Command (kissparms(1).
0446          * Protocol spec says: never append CRC.
0447          * This fixes a very old bug in the linux
0448          * kiss driver. -- dl9sau */
0449         switch (*p & 0xff) {
0450         case 0x85:
0451             /* command from userspace especially for us,
0452              * not for delivery to the tnc */
0453             if (len > 1) {
0454                 int cmd = (p[1] & 0xff);
0455                 switch(cmd) {
0456                 case 3:
0457                   ax->crcmode = CRC_MODE_SMACK;
0458                   break;
0459                 case 2:
0460                   ax->crcmode = CRC_MODE_FLEX;
0461                   break;
0462                 case 1:
0463                   ax->crcmode = CRC_MODE_NONE;
0464                   break;
0465                 case 0:
0466                 default:
0467                   ax->crcmode = CRC_MODE_SMACK_TEST;
0468                   cmd = 0;
0469                 }
0470                 ax->crcauto = (cmd ? 0 : 1);
0471                 printk(KERN_INFO "mkiss: %s: crc mode set to %d\n",
0472                        ax->dev->name, cmd);
0473             }
0474             spin_unlock_bh(&ax->buflock);
0475             netif_start_queue(dev);
0476 
0477             return;
0478         default:
0479             count = kiss_esc(p, ax->xbuff, len);
0480         }
0481     } else {
0482         unsigned short crc;
0483         switch (ax->crcmode) {
0484         case CRC_MODE_SMACK_TEST:
0485             ax->crcmode  = CRC_MODE_FLEX_TEST;
0486             printk(KERN_INFO "mkiss: %s: Trying crc-smack\n", ax->dev->name);
0487             fallthrough;
0488         case CRC_MODE_SMACK:
0489             *p |= 0x80;
0490             crc = swab16(crc16(0, p, len));
0491             count = kiss_esc_crc(p, ax->xbuff, crc, len+2);
0492             break;
0493         case CRC_MODE_FLEX_TEST:
0494             ax->crcmode = CRC_MODE_NONE;
0495             printk(KERN_INFO "mkiss: %s: Trying crc-flexnet\n", ax->dev->name);
0496             fallthrough;
0497         case CRC_MODE_FLEX:
0498             *p |= 0x20;
0499             crc = calc_crc_flex(p, len);
0500             count = kiss_esc_crc(p, ax->xbuff, crc, len+2);
0501             break;
0502 
0503         default:
0504             count = kiss_esc(p, ax->xbuff, len);
0505         }
0506     }
0507     spin_unlock_bh(&ax->buflock);
0508 
0509     set_bit(TTY_DO_WRITE_WAKEUP, &ax->tty->flags);
0510     actual = ax->tty->ops->write(ax->tty, ax->xbuff, count);
0511     dev->stats.tx_packets++;
0512     dev->stats.tx_bytes += actual;
0513 
0514     netif_trans_update(ax->dev);
0515     ax->xleft = count - actual;
0516     ax->xhead = ax->xbuff + actual;
0517 }
0518 
0519 /* Encapsulate an AX.25 packet and kick it into a TTY queue. */
0520 static netdev_tx_t ax_xmit(struct sk_buff *skb, struct net_device *dev)
0521 {
0522     struct mkiss *ax = netdev_priv(dev);
0523 
0524     if (skb->protocol == htons(ETH_P_IP))
0525         return ax25_ip_xmit(skb);
0526 
0527     if (!netif_running(dev))  {
0528         printk(KERN_ERR "mkiss: %s: xmit call when iface is down\n", dev->name);
0529         return NETDEV_TX_BUSY;
0530     }
0531 
0532     if (netif_queue_stopped(dev)) {
0533         /*
0534          * May be we must check transmitter timeout here ?
0535          *      14 Oct 1994 Dmitry Gorodchanin.
0536          */
0537         if (time_before(jiffies, dev_trans_start(dev) + 20 * HZ)) {
0538             /* 20 sec timeout not reached */
0539             return NETDEV_TX_BUSY;
0540         }
0541 
0542         printk(KERN_ERR "mkiss: %s: transmit timed out, %s?\n", dev->name,
0543                (tty_chars_in_buffer(ax->tty) || ax->xleft) ?
0544                "bad line quality" : "driver error");
0545 
0546         ax->xleft = 0;
0547         clear_bit(TTY_DO_WRITE_WAKEUP, &ax->tty->flags);
0548         netif_start_queue(dev);
0549     }
0550 
0551     /* We were not busy, so we are now... :-) */
0552     netif_stop_queue(dev);
0553     ax_encaps(dev, skb->data, skb->len);
0554     kfree_skb(skb);
0555 
0556     return NETDEV_TX_OK;
0557 }
0558 
0559 static int ax_open_dev(struct net_device *dev)
0560 {
0561     struct mkiss *ax = netdev_priv(dev);
0562 
0563     if (ax->tty == NULL)
0564         return -ENODEV;
0565 
0566     return 0;
0567 }
0568 
0569 /* Open the low-level part of the AX25 channel. Easy! */
0570 static int ax_open(struct net_device *dev)
0571 {
0572     struct mkiss *ax = netdev_priv(dev);
0573     unsigned long len;
0574 
0575     if (ax->tty == NULL)
0576         return -ENODEV;
0577 
0578     /*
0579      * Allocate the frame buffers:
0580      *
0581      * rbuff    Receive buffer.
0582      * xbuff    Transmit buffer.
0583      */
0584     len = dev->mtu * 2;
0585 
0586     /*
0587      * allow for arrival of larger UDP packets, even if we say not to
0588      * also fixes a bug in which SunOS sends 512-byte packets even with
0589      * an MSS of 128
0590      */
0591     if (len < 576 * 2)
0592         len = 576 * 2;
0593 
0594     if ((ax->rbuff = kmalloc(len + 4, GFP_KERNEL)) == NULL)
0595         goto norbuff;
0596 
0597     if ((ax->xbuff = kmalloc(len + 4, GFP_KERNEL)) == NULL)
0598         goto noxbuff;
0599 
0600     ax->mtu      = dev->mtu + 73;
0601     ax->buffsize = len;
0602     ax->rcount   = 0;
0603     ax->xleft    = 0;
0604 
0605     ax->flags   &= (1 << AXF_INUSE);      /* Clear ESCAPE & ERROR flags */
0606 
0607     spin_lock_init(&ax->buflock);
0608 
0609     return 0;
0610 
0611 noxbuff:
0612     kfree(ax->rbuff);
0613 
0614 norbuff:
0615     return -ENOMEM;
0616 }
0617 
0618 
0619 /* Close the low-level part of the AX25 channel. Easy! */
0620 static int ax_close(struct net_device *dev)
0621 {
0622     struct mkiss *ax = netdev_priv(dev);
0623 
0624     if (ax->tty)
0625         clear_bit(TTY_DO_WRITE_WAKEUP, &ax->tty->flags);
0626 
0627     netif_stop_queue(dev);
0628 
0629     return 0;
0630 }
0631 
0632 static const struct net_device_ops ax_netdev_ops = {
0633     .ndo_open            = ax_open_dev,
0634     .ndo_stop            = ax_close,
0635     .ndo_start_xmit      = ax_xmit,
0636     .ndo_set_mac_address = ax_set_mac_address,
0637 };
0638 
0639 static void ax_setup(struct net_device *dev)
0640 {
0641     /* Finish setting up the DEVICE info. */
0642     dev->mtu             = AX_MTU;
0643     dev->hard_header_len = AX25_MAX_HEADER_LEN;
0644     dev->addr_len        = AX25_ADDR_LEN;
0645     dev->type            = ARPHRD_AX25;
0646     dev->tx_queue_len    = 10;
0647     dev->header_ops      = &ax25_header_ops;
0648     dev->netdev_ops      = &ax_netdev_ops;
0649 
0650 
0651     memcpy(dev->broadcast, &ax25_bcast, AX25_ADDR_LEN);
0652     dev_addr_set(dev, (u8 *)&ax25_defaddr);
0653 
0654     dev->flags      = IFF_BROADCAST | IFF_MULTICAST;
0655 }
0656 
0657 /*
0658  * We have a potential race on dereferencing tty->disc_data, because the tty
0659  * layer provides no locking at all - thus one cpu could be running
0660  * sixpack_receive_buf while another calls sixpack_close, which zeroes
0661  * tty->disc_data and frees the memory that sixpack_receive_buf is using.  The
0662  * best way to fix this is to use a rwlock in the tty struct, but for now we
0663  * use a single global rwlock for all ttys in ppp line discipline.
0664  */
0665 static DEFINE_RWLOCK(disc_data_lock);
0666 
0667 static struct mkiss *mkiss_get(struct tty_struct *tty)
0668 {
0669     struct mkiss *ax;
0670 
0671     read_lock(&disc_data_lock);
0672     ax = tty->disc_data;
0673     if (ax)
0674         refcount_inc(&ax->refcnt);
0675     read_unlock(&disc_data_lock);
0676 
0677     return ax;
0678 }
0679 
0680 static void mkiss_put(struct mkiss *ax)
0681 {
0682     if (refcount_dec_and_test(&ax->refcnt))
0683         complete(&ax->dead);
0684 }
0685 
0686 static int crc_force = 0;   /* Can be overridden with insmod */
0687 
0688 static int mkiss_open(struct tty_struct *tty)
0689 {
0690     struct net_device *dev;
0691     struct mkiss *ax;
0692     int err;
0693 
0694     if (!capable(CAP_NET_ADMIN))
0695         return -EPERM;
0696     if (tty->ops->write == NULL)
0697         return -EOPNOTSUPP;
0698 
0699     dev = alloc_netdev(sizeof(struct mkiss), "ax%d", NET_NAME_UNKNOWN,
0700                ax_setup);
0701     if (!dev) {
0702         err = -ENOMEM;
0703         goto out;
0704     }
0705 
0706     ax = netdev_priv(dev);
0707     ax->dev = dev;
0708 
0709     spin_lock_init(&ax->buflock);
0710     refcount_set(&ax->refcnt, 1);
0711     init_completion(&ax->dead);
0712 
0713     ax->tty = tty;
0714     tty->disc_data = ax;
0715     tty->receive_room = 65535;
0716 
0717     tty_driver_flush_buffer(tty);
0718 
0719     /* Restore default settings */
0720     dev->type = ARPHRD_AX25;
0721 
0722     /* Perform the low-level AX25 initialization. */
0723     err = ax_open(ax->dev);
0724     if (err)
0725         goto out_free_netdev;
0726 
0727     err = register_netdev(dev);
0728     if (err)
0729         goto out_free_buffers;
0730 
0731     /* after register_netdev() - because else printk smashes the kernel */
0732     switch (crc_force) {
0733     case 3:
0734         ax->crcmode  = CRC_MODE_SMACK;
0735         printk(KERN_INFO "mkiss: %s: crc mode smack forced.\n",
0736                ax->dev->name);
0737         break;
0738     case 2:
0739         ax->crcmode  = CRC_MODE_FLEX;
0740         printk(KERN_INFO "mkiss: %s: crc mode flexnet forced.\n",
0741                ax->dev->name);
0742         break;
0743     case 1:
0744         ax->crcmode  = CRC_MODE_NONE;
0745         printk(KERN_INFO "mkiss: %s: crc mode disabled.\n",
0746                ax->dev->name);
0747         break;
0748     case 0:
0749     default:
0750         crc_force = 0;
0751         printk(KERN_INFO "mkiss: %s: crc mode is auto.\n",
0752                ax->dev->name);
0753         ax->crcmode  = CRC_MODE_SMACK_TEST;
0754     }
0755     ax->crcauto = (crc_force ? 0 : 1);
0756 
0757     netif_start_queue(dev);
0758 
0759     /* Done.  We have linked the TTY line to a channel. */
0760     return 0;
0761 
0762 out_free_buffers:
0763     kfree(ax->rbuff);
0764     kfree(ax->xbuff);
0765 
0766 out_free_netdev:
0767     free_netdev(dev);
0768 
0769 out:
0770     return err;
0771 }
0772 
0773 static void mkiss_close(struct tty_struct *tty)
0774 {
0775     struct mkiss *ax;
0776 
0777     write_lock_irq(&disc_data_lock);
0778     ax = tty->disc_data;
0779     tty->disc_data = NULL;
0780     write_unlock_irq(&disc_data_lock);
0781 
0782     if (!ax)
0783         return;
0784 
0785     /*
0786      * We have now ensured that nobody can start using ap from now on, but
0787      * we have to wait for all existing users to finish.
0788      */
0789     if (!refcount_dec_and_test(&ax->refcnt))
0790         wait_for_completion(&ax->dead);
0791     /*
0792      * Halt the transmit queue so that a new transmit cannot scribble
0793      * on our buffers
0794      */
0795     netif_stop_queue(ax->dev);
0796 
0797     unregister_netdev(ax->dev);
0798 
0799     /* Free all AX25 frame buffers after unreg. */
0800     kfree(ax->rbuff);
0801     kfree(ax->xbuff);
0802 
0803     ax->tty = NULL;
0804 
0805     free_netdev(ax->dev);
0806 }
0807 
0808 /* Perform I/O control on an active ax25 channel. */
0809 static int mkiss_ioctl(struct tty_struct *tty, unsigned int cmd,
0810         unsigned long arg)
0811 {
0812     struct mkiss *ax = mkiss_get(tty);
0813     struct net_device *dev;
0814     unsigned int tmp, err;
0815 
0816     /* First make sure we're connected. */
0817     if (ax == NULL)
0818         return -ENXIO;
0819     dev = ax->dev;
0820 
0821     switch (cmd) {
0822     case SIOCGIFNAME:
0823         err = copy_to_user((void __user *) arg, ax->dev->name,
0824                            strlen(ax->dev->name) + 1) ? -EFAULT : 0;
0825         break;
0826 
0827     case SIOCGIFENCAP:
0828         err = put_user(4, (int __user *) arg);
0829         break;
0830 
0831     case SIOCSIFENCAP:
0832         if (get_user(tmp, (int __user *) arg)) {
0833             err = -EFAULT;
0834             break;
0835         }
0836 
0837         ax->mode = tmp;
0838         dev->addr_len        = AX25_ADDR_LEN;
0839         dev->hard_header_len = AX25_KISS_HEADER_LEN +
0840                                AX25_MAX_HEADER_LEN + 3;
0841         dev->type            = ARPHRD_AX25;
0842 
0843         err = 0;
0844         break;
0845 
0846     case SIOCSIFHWADDR: {
0847         char addr[AX25_ADDR_LEN];
0848 
0849         if (copy_from_user(&addr,
0850                            (void __user *) arg, AX25_ADDR_LEN)) {
0851             err = -EFAULT;
0852             break;
0853         }
0854 
0855         netif_tx_lock_bh(dev);
0856         __dev_addr_set(dev, addr, AX25_ADDR_LEN);
0857         netif_tx_unlock_bh(dev);
0858 
0859         err = 0;
0860         break;
0861     }
0862     default:
0863         err = -ENOIOCTLCMD;
0864     }
0865 
0866     mkiss_put(ax);
0867 
0868     return err;
0869 }
0870 
0871 /*
0872  * Handle the 'receiver data ready' interrupt.
0873  * This function is called by the 'tty_io' module in the kernel when
0874  * a block of data has been received, which can now be decapsulated
0875  * and sent on to the AX.25 layer for further processing.
0876  */
0877 static void mkiss_receive_buf(struct tty_struct *tty, const unsigned char *cp,
0878     const char *fp, int count)
0879 {
0880     struct mkiss *ax = mkiss_get(tty);
0881 
0882     if (!ax)
0883         return;
0884 
0885     /*
0886      * Argh! mtu change time! - costs us the packet part received
0887      * at the change
0888      */
0889     if (ax->mtu != ax->dev->mtu + 73)
0890         ax_changedmtu(ax);
0891 
0892     /* Read the characters out of the buffer */
0893     while (count--) {
0894         if (fp != NULL && *fp++) {
0895             if (!test_and_set_bit(AXF_ERROR, &ax->flags))
0896                 ax->dev->stats.rx_errors++;
0897             cp++;
0898             continue;
0899         }
0900 
0901         kiss_unesc(ax, *cp++);
0902     }
0903 
0904     mkiss_put(ax);
0905     tty_unthrottle(tty);
0906 }
0907 
0908 /*
0909  * Called by the driver when there's room for more data.  If we have
0910  * more packets to send, we send them here.
0911  */
0912 static void mkiss_write_wakeup(struct tty_struct *tty)
0913 {
0914     struct mkiss *ax = mkiss_get(tty);
0915     int actual;
0916 
0917     if (!ax)
0918         return;
0919 
0920     if (ax->xleft <= 0)  {
0921         /* Now serial buffer is almost free & we can start
0922          * transmission of another packet
0923          */
0924         clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
0925 
0926         netif_wake_queue(ax->dev);
0927         goto out;
0928     }
0929 
0930     actual = tty->ops->write(tty, ax->xhead, ax->xleft);
0931     ax->xleft -= actual;
0932     ax->xhead += actual;
0933 
0934 out:
0935     mkiss_put(ax);
0936 }
0937 
0938 static struct tty_ldisc_ops ax_ldisc = {
0939     .owner      = THIS_MODULE,
0940     .num        = N_AX25,
0941     .name       = "mkiss",
0942     .open       = mkiss_open,
0943     .close      = mkiss_close,
0944     .ioctl      = mkiss_ioctl,
0945     .receive_buf    = mkiss_receive_buf,
0946     .write_wakeup   = mkiss_write_wakeup
0947 };
0948 
0949 static const char banner[] __initconst = KERN_INFO \
0950     "mkiss: AX.25 Multikiss, Hans Albas PE1AYX\n";
0951 static const char msg_regfail[] __initconst = KERN_ERR \
0952     "mkiss: can't register line discipline (err = %d)\n";
0953 
0954 static int __init mkiss_init_driver(void)
0955 {
0956     int status;
0957 
0958     printk(banner);
0959 
0960     status = tty_register_ldisc(&ax_ldisc);
0961     if (status != 0)
0962         printk(msg_regfail, status);
0963 
0964     return status;
0965 }
0966 
0967 static void __exit mkiss_exit_driver(void)
0968 {
0969     tty_unregister_ldisc(&ax_ldisc);
0970 }
0971 
0972 MODULE_AUTHOR("Ralf Baechle DL5RB <ralf@linux-mips.org>");
0973 MODULE_DESCRIPTION("KISS driver for AX.25 over TTYs");
0974 module_param(crc_force, int, 0);
0975 MODULE_PARM_DESC(crc_force, "crc [0 = auto | 1 = none | 2 = flexnet | 3 = smack]");
0976 MODULE_LICENSE("GPL");
0977 MODULE_ALIAS_LDISC(N_AX25);
0978 
0979 module_init(mkiss_init_driver);
0980 module_exit(mkiss_exit_driver);