Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * slip.c This module implements the SLIP protocol for kernel-based |
| 3 | * devices like TTY. It interfaces between a raw TTY, and the |
| 4 | * kernel's INET protocol layers. |
| 5 | * |
| 6 | * Version: @(#)slip.c 0.8.3 12/24/94 |
| 7 | * |
| 8 | * Authors: Laurence Culhane, <loz@holmes.demon.co.uk> |
| 9 | * Fred N. van Kempen, <waltje@uwalt.nl.mugnet.org> |
| 10 | * |
| 11 | * Fixes: |
| 12 | * Alan Cox : Sanity checks and avoid tx overruns. |
| 13 | * Has a new sl->mtu field. |
| 14 | * Alan Cox : Found cause of overrun. ifconfig sl0 mtu upwards. |
| 15 | * Driver now spots this and grows/shrinks its buffers(hack!). |
| 16 | * Memory leak if you run out of memory setting up a slip driver fixed. |
| 17 | * Matt Dillon : Printable slip (borrowed from NET2E) |
| 18 | * Pauline Middelink : Slip driver fixes. |
| 19 | * Alan Cox : Honours the old SL_COMPRESSED flag |
| 20 | * Alan Cox : KISS AX.25 and AXUI IP support |
| 21 | * Michael Riepe : Automatic CSLIP recognition added |
| 22 | * Charles Hedrick : CSLIP header length problem fix. |
| 23 | * Alan Cox : Corrected non-IP cases of the above. |
| 24 | * Alan Cox : Now uses hardware type as per FvK. |
| 25 | * Alan Cox : Default to 192.168.0.0 (RFC 1597) |
| 26 | * A.N.Kuznetsov : dev_tint() recursion fix. |
| 27 | * Dmitry Gorodchanin : SLIP memory leaks |
| 28 | * Dmitry Gorodchanin : Code cleanup. Reduce tty driver |
| 29 | * buffering from 4096 to 256 bytes. |
| 30 | * Improving SLIP response time. |
| 31 | * CONFIG_SLIP_MODE_SLIP6. |
| 32 | * ifconfig sl? up & down now works correctly. |
| 33 | * Modularization. |
| 34 | * Alan Cox : Oops - fix AX.25 buffer lengths |
| 35 | * Dmitry Gorodchanin : Even more cleanups. Preserve CSLIP |
| 36 | * statistics. Include CSLIP code only |
| 37 | * if it really needed. |
| 38 | * Alan Cox : Free slhc buffers in the right place. |
| 39 | * Alan Cox : Allow for digipeated IP over AX.25 |
| 40 | * Matti Aarnio : Dynamic SLIP devices, with ideas taken |
| 41 | * from Jim Freeman's <jfree@caldera.com> |
| 42 | * dynamic PPP devices. We do NOT kfree() |
| 43 | * device entries, just reg./unreg. them |
| 44 | * as they are needed. We kfree() them |
| 45 | * at module cleanup. |
| 46 | * With MODULE-loading ``insmod'', user can |
| 47 | * issue parameter: slip_maxdev=1024 |
| 48 | * (Or how much he/she wants.. Default is 256) |
| 49 | * * Stanislav Voronyi : Slip line checking, with ideas taken |
| 50 | * from multislip BSDI driver which was written |
| 51 | * by Igor Chechik, RELCOM Corp. Only algorithms |
| 52 | * have been ported to Linux SLIP driver. |
| 53 | * Vitaly E. Lavrov : Sane behaviour on tty hangup. |
| 54 | * Alexey Kuznetsov : Cleanup interfaces to tty&netdevice modules. |
| 55 | */ |
| 56 | |
| 57 | #define SL_CHECK_TRANSMIT |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | #include <linux/module.h> |
| 59 | #include <linux/moduleparam.h> |
| 60 | |
| 61 | #include <asm/system.h> |
| 62 | #include <asm/uaccess.h> |
| 63 | #include <linux/bitops.h> |
| 64 | #include <linux/string.h> |
| 65 | #include <linux/mm.h> |
| 66 | #include <linux/interrupt.h> |
| 67 | #include <linux/in.h> |
| 68 | #include <linux/tty.h> |
| 69 | #include <linux/errno.h> |
| 70 | #include <linux/netdevice.h> |
| 71 | #include <linux/etherdevice.h> |
| 72 | #include <linux/skbuff.h> |
| 73 | #include <linux/rtnetlink.h> |
| 74 | #include <linux/if_arp.h> |
| 75 | #include <linux/if_slip.h> |
David S. Miller | 12dc2fd | 2005-06-28 16:27:32 -0700 | [diff] [blame] | 76 | #include <linux/delay.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | #include <linux/init.h> |
| 78 | #include "slip.h" |
| 79 | #ifdef CONFIG_INET |
| 80 | #include <linux/ip.h> |
| 81 | #include <linux/tcp.h> |
| 82 | #include <net/slhc_vj.h> |
| 83 | #endif |
| 84 | |
| 85 | #define SLIP_VERSION "0.8.4-NET3.019-NEWTTY" |
| 86 | |
| 87 | static struct net_device **slip_devs; |
| 88 | |
| 89 | static int slip_maxdev = SL_NRUNIT; |
| 90 | module_param(slip_maxdev, int, 0); |
| 91 | MODULE_PARM_DESC(slip_maxdev, "Maximum number of slip devices"); |
| 92 | |
| 93 | static int slip_esc(unsigned char *p, unsigned char *d, int len); |
| 94 | static void slip_unesc(struct slip *sl, unsigned char c); |
| 95 | #ifdef CONFIG_SLIP_MODE_SLIP6 |
| 96 | static int slip_esc6(unsigned char *p, unsigned char *d, int len); |
| 97 | static void slip_unesc6(struct slip *sl, unsigned char c); |
| 98 | #endif |
| 99 | #ifdef CONFIG_SLIP_SMART |
| 100 | static void sl_keepalive(unsigned long sls); |
| 101 | static void sl_outfill(unsigned long sls); |
| 102 | static int sl_ioctl(struct net_device *dev,struct ifreq *rq,int cmd); |
| 103 | #endif |
| 104 | |
| 105 | /******************************** |
| 106 | * Buffer administration routines: |
| 107 | * sl_alloc_bufs() |
| 108 | * sl_free_bufs() |
| 109 | * sl_realloc_bufs() |
| 110 | * |
| 111 | * NOTE: sl_realloc_bufs != sl_free_bufs + sl_alloc_bufs, because |
| 112 | * sl_realloc_bufs provides strong atomicity and reallocation |
| 113 | * on actively running device. |
| 114 | *********************************/ |
| 115 | |
Jeff Garzik | 6aa20a2 | 2006-09-13 13:24:59 -0400 | [diff] [blame^] | 116 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | Allocate channel buffers. |
| 118 | */ |
| 119 | |
| 120 | static int |
| 121 | sl_alloc_bufs(struct slip *sl, int mtu) |
| 122 | { |
| 123 | int err = -ENOBUFS; |
| 124 | unsigned long len; |
| 125 | char * rbuff = NULL; |
| 126 | char * xbuff = NULL; |
| 127 | #ifdef SL_INCLUDE_CSLIP |
| 128 | char * cbuff = NULL; |
| 129 | struct slcompress *slcomp = NULL; |
| 130 | #endif |
| 131 | |
| 132 | /* |
| 133 | * Allocate the SLIP frame buffers: |
| 134 | * |
| 135 | * rbuff Receive buffer. |
| 136 | * xbuff Transmit buffer. |
| 137 | * cbuff Temporary compression buffer. |
| 138 | */ |
| 139 | len = mtu * 2; |
| 140 | |
| 141 | /* |
| 142 | * allow for arrival of larger UDP packets, even if we say not to |
| 143 | * also fixes a bug in which SunOS sends 512-byte packets even with |
| 144 | * an MSS of 128 |
| 145 | */ |
| 146 | if (len < 576 * 2) |
| 147 | len = 576 * 2; |
| 148 | rbuff = kmalloc(len + 4, GFP_KERNEL); |
| 149 | if (rbuff == NULL) |
| 150 | goto err_exit; |
| 151 | xbuff = kmalloc(len + 4, GFP_KERNEL); |
| 152 | if (xbuff == NULL) |
| 153 | goto err_exit; |
| 154 | #ifdef SL_INCLUDE_CSLIP |
| 155 | cbuff = kmalloc(len + 4, GFP_KERNEL); |
| 156 | if (cbuff == NULL) |
| 157 | goto err_exit; |
| 158 | slcomp = slhc_init(16, 16); |
| 159 | if (slcomp == NULL) |
| 160 | goto err_exit; |
| 161 | #endif |
| 162 | spin_lock_bh(&sl->lock); |
| 163 | if (sl->tty == NULL) { |
| 164 | spin_unlock_bh(&sl->lock); |
| 165 | err = -ENODEV; |
| 166 | goto err_exit; |
| 167 | } |
| 168 | sl->mtu = mtu; |
| 169 | sl->buffsize = len; |
| 170 | sl->rcount = 0; |
| 171 | sl->xleft = 0; |
| 172 | rbuff = xchg(&sl->rbuff, rbuff); |
| 173 | xbuff = xchg(&sl->xbuff, xbuff); |
| 174 | #ifdef SL_INCLUDE_CSLIP |
| 175 | cbuff = xchg(&sl->cbuff, cbuff); |
| 176 | slcomp = xchg(&sl->slcomp, slcomp); |
| 177 | #ifdef CONFIG_SLIP_MODE_SLIP6 |
| 178 | sl->xdata = 0; |
| 179 | sl->xbits = 0; |
| 180 | #endif |
| 181 | #endif |
| 182 | spin_unlock_bh(&sl->lock); |
| 183 | err = 0; |
| 184 | |
| 185 | /* Cleanup */ |
| 186 | err_exit: |
| 187 | #ifdef SL_INCLUDE_CSLIP |
Jesper Juhl | 158a0e4 | 2005-04-24 18:59:30 -0700 | [diff] [blame] | 188 | kfree(cbuff); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | if (slcomp) |
| 190 | slhc_free(slcomp); |
| 191 | #endif |
Jesper Juhl | 158a0e4 | 2005-04-24 18:59:30 -0700 | [diff] [blame] | 192 | kfree(xbuff); |
| 193 | kfree(rbuff); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | return err; |
| 195 | } |
| 196 | |
| 197 | /* Free a SLIP channel buffers. */ |
| 198 | static void |
| 199 | sl_free_bufs(struct slip *sl) |
| 200 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | /* Free all SLIP frame buffers. */ |
Jesper Juhl | 9b200b0 | 2005-06-23 21:06:56 -0700 | [diff] [blame] | 202 | kfree(xchg(&sl->rbuff, NULL)); |
| 203 | kfree(xchg(&sl->xbuff, NULL)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | #ifdef SL_INCLUDE_CSLIP |
Jesper Juhl | 9b200b0 | 2005-06-23 21:06:56 -0700 | [diff] [blame] | 205 | kfree(xchg(&sl->cbuff, NULL)); |
| 206 | slhc_free(xchg(&sl->slcomp, NULL)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | #endif |
| 208 | } |
| 209 | |
Jeff Garzik | 6aa20a2 | 2006-09-13 13:24:59 -0400 | [diff] [blame^] | 210 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | Reallocate slip channel buffers. |
| 212 | */ |
| 213 | |
| 214 | static int sl_realloc_bufs(struct slip *sl, int mtu) |
| 215 | { |
| 216 | int err = 0; |
| 217 | struct net_device *dev = sl->dev; |
| 218 | unsigned char *xbuff, *rbuff; |
| 219 | #ifdef SL_INCLUDE_CSLIP |
| 220 | unsigned char *cbuff; |
| 221 | #endif |
| 222 | int len = mtu * 2; |
| 223 | |
| 224 | /* |
| 225 | * allow for arrival of larger UDP packets, even if we say not to |
| 226 | * also fixes a bug in which SunOS sends 512-byte packets even with |
| 227 | * an MSS of 128 |
| 228 | */ |
| 229 | if (len < 576 * 2) |
| 230 | len = 576 * 2; |
| 231 | |
| 232 | xbuff = (unsigned char *) kmalloc (len + 4, GFP_ATOMIC); |
| 233 | rbuff = (unsigned char *) kmalloc (len + 4, GFP_ATOMIC); |
| 234 | #ifdef SL_INCLUDE_CSLIP |
| 235 | cbuff = (unsigned char *) kmalloc (len + 4, GFP_ATOMIC); |
| 236 | #endif |
| 237 | |
| 238 | |
| 239 | #ifdef SL_INCLUDE_CSLIP |
| 240 | if (xbuff == NULL || rbuff == NULL || cbuff == NULL) { |
| 241 | #else |
| 242 | if (xbuff == NULL || rbuff == NULL) { |
| 243 | #endif |
| 244 | if (mtu >= sl->mtu) { |
| 245 | printk(KERN_WARNING "%s: unable to grow slip buffers, MTU change cancelled.\n", |
| 246 | dev->name); |
| 247 | err = -ENOBUFS; |
| 248 | } |
| 249 | goto done; |
| 250 | } |
| 251 | |
| 252 | spin_lock_bh(&sl->lock); |
| 253 | |
| 254 | err = -ENODEV; |
| 255 | if (sl->tty == NULL) |
| 256 | goto done_on_bh; |
| 257 | |
| 258 | xbuff = xchg(&sl->xbuff, xbuff); |
| 259 | rbuff = xchg(&sl->rbuff, rbuff); |
| 260 | #ifdef SL_INCLUDE_CSLIP |
| 261 | cbuff = xchg(&sl->cbuff, cbuff); |
| 262 | #endif |
| 263 | if (sl->xleft) { |
| 264 | if (sl->xleft <= len) { |
| 265 | memcpy(sl->xbuff, sl->xhead, sl->xleft); |
| 266 | } else { |
| 267 | sl->xleft = 0; |
| 268 | sl->tx_dropped++; |
| 269 | } |
| 270 | } |
| 271 | sl->xhead = sl->xbuff; |
| 272 | |
| 273 | if (sl->rcount) { |
| 274 | if (sl->rcount <= len) { |
| 275 | memcpy(sl->rbuff, rbuff, sl->rcount); |
| 276 | } else { |
| 277 | sl->rcount = 0; |
| 278 | sl->rx_over_errors++; |
| 279 | set_bit(SLF_ERROR, &sl->flags); |
| 280 | } |
| 281 | } |
| 282 | sl->mtu = mtu; |
| 283 | dev->mtu = mtu; |
| 284 | sl->buffsize = len; |
| 285 | err = 0; |
| 286 | |
| 287 | done_on_bh: |
| 288 | spin_unlock_bh(&sl->lock); |
| 289 | |
| 290 | done: |
Jesper Juhl | 158a0e4 | 2005-04-24 18:59:30 -0700 | [diff] [blame] | 291 | kfree(xbuff); |
| 292 | kfree(rbuff); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | #ifdef SL_INCLUDE_CSLIP |
Jesper Juhl | 158a0e4 | 2005-04-24 18:59:30 -0700 | [diff] [blame] | 294 | kfree(cbuff); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | #endif |
| 296 | return err; |
| 297 | } |
| 298 | |
| 299 | |
| 300 | /* Set the "sending" flag. This must be atomic hence the set_bit. */ |
| 301 | static inline void |
| 302 | sl_lock(struct slip *sl) |
| 303 | { |
| 304 | netif_stop_queue(sl->dev); |
| 305 | } |
| 306 | |
| 307 | |
| 308 | /* Clear the "sending" flag. This must be atomic, hence the ASM. */ |
| 309 | static inline void |
| 310 | sl_unlock(struct slip *sl) |
| 311 | { |
| 312 | netif_wake_queue(sl->dev); |
| 313 | } |
| 314 | |
| 315 | /* Send one completely decapsulated IP datagram to the IP layer. */ |
| 316 | static void |
| 317 | sl_bump(struct slip *sl) |
| 318 | { |
| 319 | struct sk_buff *skb; |
| 320 | int count; |
| 321 | |
| 322 | count = sl->rcount; |
| 323 | #ifdef SL_INCLUDE_CSLIP |
| 324 | if (sl->mode & (SL_MODE_ADAPTIVE | SL_MODE_CSLIP)) { |
| 325 | unsigned char c; |
| 326 | if ((c = sl->rbuff[0]) & SL_TYPE_COMPRESSED_TCP) { |
| 327 | /* ignore compressed packets when CSLIP is off */ |
| 328 | if (!(sl->mode & SL_MODE_CSLIP)) { |
| 329 | printk(KERN_WARNING "%s: compressed packet ignored\n", sl->dev->name); |
| 330 | return; |
| 331 | } |
| 332 | /* make sure we've reserved enough space for uncompress to use */ |
| 333 | if (count + 80 > sl->buffsize) { |
| 334 | sl->rx_over_errors++; |
| 335 | return; |
| 336 | } |
| 337 | count = slhc_uncompress(sl->slcomp, sl->rbuff, count); |
| 338 | if (count <= 0) { |
| 339 | return; |
| 340 | } |
| 341 | } else if (c >= SL_TYPE_UNCOMPRESSED_TCP) { |
| 342 | if (!(sl->mode & SL_MODE_CSLIP)) { |
| 343 | /* turn on header compression */ |
| 344 | sl->mode |= SL_MODE_CSLIP; |
| 345 | sl->mode &= ~SL_MODE_ADAPTIVE; |
| 346 | printk(KERN_INFO "%s: header compression turned on\n", sl->dev->name); |
| 347 | } |
| 348 | sl->rbuff[0] &= 0x4f; |
| 349 | if (slhc_remember(sl->slcomp, sl->rbuff, count) <= 0) { |
| 350 | return; |
| 351 | } |
| 352 | } |
| 353 | } |
| 354 | #endif /* SL_INCLUDE_CSLIP */ |
| 355 | |
| 356 | sl->rx_bytes+=count; |
Jeff Garzik | 6aa20a2 | 2006-09-13 13:24:59 -0400 | [diff] [blame^] | 357 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 358 | skb = dev_alloc_skb(count); |
| 359 | if (skb == NULL) { |
| 360 | printk(KERN_WARNING "%s: memory squeeze, dropping packet.\n", sl->dev->name); |
| 361 | sl->rx_dropped++; |
| 362 | return; |
| 363 | } |
| 364 | skb->dev = sl->dev; |
| 365 | memcpy(skb_put(skb,count), sl->rbuff, count); |
| 366 | skb->mac.raw=skb->data; |
| 367 | skb->protocol=htons(ETH_P_IP); |
| 368 | netif_rx(skb); |
| 369 | sl->dev->last_rx = jiffies; |
| 370 | sl->rx_packets++; |
| 371 | } |
| 372 | |
| 373 | /* Encapsulate one IP datagram and stuff into a TTY queue. */ |
| 374 | static void |
| 375 | sl_encaps(struct slip *sl, unsigned char *icp, int len) |
| 376 | { |
| 377 | unsigned char *p; |
| 378 | int actual, count; |
| 379 | |
| 380 | if (len > sl->mtu) { /* Sigh, shouldn't occur BUT ... */ |
| 381 | printk(KERN_WARNING "%s: truncating oversized transmit packet!\n", sl->dev->name); |
| 382 | sl->tx_dropped++; |
| 383 | sl_unlock(sl); |
| 384 | return; |
| 385 | } |
| 386 | |
| 387 | p = icp; |
| 388 | #ifdef SL_INCLUDE_CSLIP |
| 389 | if (sl->mode & SL_MODE_CSLIP) { |
| 390 | len = slhc_compress(sl->slcomp, p, len, sl->cbuff, &p, 1); |
| 391 | } |
| 392 | #endif |
| 393 | #ifdef CONFIG_SLIP_MODE_SLIP6 |
| 394 | if(sl->mode & SL_MODE_SLIP6) |
| 395 | count = slip_esc6(p, (unsigned char *) sl->xbuff, len); |
| 396 | else |
| 397 | #endif |
| 398 | count = slip_esc(p, (unsigned char *) sl->xbuff, len); |
| 399 | |
| 400 | /* Order of next two lines is *very* important. |
| 401 | * When we are sending a little amount of data, |
| 402 | * the transfer may be completed inside driver.write() |
| 403 | * routine, because it's running with interrupts enabled. |
| 404 | * In this case we *never* got WRITE_WAKEUP event, |
| 405 | * if we did not request it before write operation. |
| 406 | * 14 Oct 1994 Dmitry Gorodchanin. |
| 407 | */ |
| 408 | sl->tty->flags |= (1 << TTY_DO_WRITE_WAKEUP); |
| 409 | actual = sl->tty->driver->write(sl->tty, sl->xbuff, count); |
| 410 | #ifdef SL_CHECK_TRANSMIT |
| 411 | sl->dev->trans_start = jiffies; |
| 412 | #endif |
| 413 | sl->xleft = count - actual; |
| 414 | sl->xhead = sl->xbuff + actual; |
| 415 | #ifdef CONFIG_SLIP_SMART |
| 416 | /* VSV */ |
| 417 | clear_bit(SLF_OUTWAIT, &sl->flags); /* reset outfill flag */ |
| 418 | #endif |
| 419 | } |
| 420 | |
| 421 | /* |
| 422 | * Called by the driver when there's room for more data. If we have |
| 423 | * more packets to send, we send them here. |
| 424 | */ |
| 425 | static void slip_write_wakeup(struct tty_struct *tty) |
| 426 | { |
| 427 | int actual; |
| 428 | struct slip *sl = (struct slip *) tty->disc_data; |
| 429 | |
| 430 | /* First make sure we're connected. */ |
| 431 | if (!sl || sl->magic != SLIP_MAGIC || !netif_running(sl->dev)) { |
| 432 | return; |
| 433 | } |
| 434 | if (sl->xleft <= 0) { |
| 435 | /* Now serial buffer is almost free & we can start |
| 436 | * transmission of another packet */ |
| 437 | sl->tx_packets++; |
| 438 | tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP); |
| 439 | sl_unlock(sl); |
| 440 | return; |
| 441 | } |
| 442 | |
| 443 | actual = tty->driver->write(tty, sl->xhead, sl->xleft); |
| 444 | sl->xleft -= actual; |
| 445 | sl->xhead += actual; |
| 446 | } |
| 447 | |
| 448 | static void sl_tx_timeout(struct net_device *dev) |
| 449 | { |
| 450 | struct slip *sl = netdev_priv(dev); |
| 451 | |
| 452 | spin_lock(&sl->lock); |
| 453 | |
| 454 | if (netif_queue_stopped(dev)) { |
| 455 | if (!netif_running(dev)) |
| 456 | goto out; |
| 457 | |
| 458 | /* May be we must check transmitter timeout here ? |
| 459 | * 14 Oct 1994 Dmitry Gorodchanin. |
| 460 | */ |
| 461 | #ifdef SL_CHECK_TRANSMIT |
| 462 | if (time_before(jiffies, dev->trans_start + 20 * HZ)) { |
| 463 | /* 20 sec timeout not reached */ |
| 464 | goto out; |
| 465 | } |
| 466 | printk(KERN_WARNING "%s: transmit timed out, %s?\n", dev->name, |
| 467 | (sl->tty->driver->chars_in_buffer(sl->tty) || sl->xleft) ? |
| 468 | "bad line quality" : "driver error"); |
| 469 | sl->xleft = 0; |
| 470 | sl->tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP); |
| 471 | sl_unlock(sl); |
| 472 | #endif |
| 473 | } |
| 474 | |
| 475 | out: |
| 476 | spin_unlock(&sl->lock); |
| 477 | } |
| 478 | |
| 479 | |
| 480 | /* Encapsulate an IP datagram and kick it into a TTY queue. */ |
| 481 | static int |
| 482 | sl_xmit(struct sk_buff *skb, struct net_device *dev) |
| 483 | { |
| 484 | struct slip *sl = netdev_priv(dev); |
| 485 | |
| 486 | spin_lock(&sl->lock); |
| 487 | if (!netif_running(dev)) { |
| 488 | spin_unlock(&sl->lock); |
| 489 | printk(KERN_WARNING "%s: xmit call when iface is down\n", dev->name); |
| 490 | dev_kfree_skb(skb); |
| 491 | return 0; |
| 492 | } |
| 493 | if (sl->tty == NULL) { |
| 494 | spin_unlock(&sl->lock); |
| 495 | dev_kfree_skb(skb); |
| 496 | return 0; |
| 497 | } |
| 498 | |
| 499 | sl_lock(sl); |
| 500 | sl->tx_bytes+=skb->len; |
| 501 | sl_encaps(sl, skb->data, skb->len); |
| 502 | spin_unlock(&sl->lock); |
| 503 | |
| 504 | dev_kfree_skb(skb); |
| 505 | return 0; |
| 506 | } |
| 507 | |
| 508 | |
| 509 | /****************************************** |
| 510 | * Routines looking at netdevice side. |
| 511 | ******************************************/ |
| 512 | |
| 513 | /* Netdevice UP -> DOWN routine */ |
| 514 | |
| 515 | static int |
| 516 | sl_close(struct net_device *dev) |
| 517 | { |
| 518 | struct slip *sl = netdev_priv(dev); |
| 519 | |
| 520 | spin_lock_bh(&sl->lock); |
| 521 | if (sl->tty) { |
| 522 | /* TTY discipline is running. */ |
| 523 | sl->tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP); |
| 524 | } |
| 525 | netif_stop_queue(dev); |
| 526 | sl->rcount = 0; |
| 527 | sl->xleft = 0; |
| 528 | spin_unlock_bh(&sl->lock); |
| 529 | |
| 530 | return 0; |
| 531 | } |
| 532 | |
| 533 | /* Netdevice DOWN -> UP routine */ |
| 534 | |
| 535 | static int sl_open(struct net_device *dev) |
| 536 | { |
| 537 | struct slip *sl = netdev_priv(dev); |
| 538 | |
| 539 | if (sl->tty==NULL) |
| 540 | return -ENODEV; |
| 541 | |
| 542 | sl->flags &= (1 << SLF_INUSE); |
| 543 | netif_start_queue(dev); |
| 544 | return 0; |
| 545 | } |
| 546 | |
| 547 | /* Netdevice change MTU request */ |
| 548 | |
| 549 | static int sl_change_mtu(struct net_device *dev, int new_mtu) |
| 550 | { |
| 551 | struct slip *sl = netdev_priv(dev); |
| 552 | |
| 553 | if (new_mtu < 68 || new_mtu > 65534) |
| 554 | return -EINVAL; |
| 555 | |
| 556 | if (new_mtu != dev->mtu) |
| 557 | return sl_realloc_bufs(sl, new_mtu); |
| 558 | return 0; |
| 559 | } |
| 560 | |
| 561 | /* Netdevice get statistics request */ |
| 562 | |
| 563 | static struct net_device_stats * |
| 564 | sl_get_stats(struct net_device *dev) |
| 565 | { |
| 566 | static struct net_device_stats stats; |
| 567 | struct slip *sl = netdev_priv(dev); |
| 568 | #ifdef SL_INCLUDE_CSLIP |
| 569 | struct slcompress *comp; |
| 570 | #endif |
| 571 | |
| 572 | memset(&stats, 0, sizeof(struct net_device_stats)); |
| 573 | |
| 574 | stats.rx_packets = sl->rx_packets; |
| 575 | stats.tx_packets = sl->tx_packets; |
| 576 | stats.rx_bytes = sl->rx_bytes; |
| 577 | stats.tx_bytes = sl->tx_bytes; |
| 578 | stats.rx_dropped = sl->rx_dropped; |
| 579 | stats.tx_dropped = sl->tx_dropped; |
| 580 | stats.tx_errors = sl->tx_errors; |
| 581 | stats.rx_errors = sl->rx_errors; |
| 582 | stats.rx_over_errors = sl->rx_over_errors; |
| 583 | #ifdef SL_INCLUDE_CSLIP |
| 584 | stats.rx_fifo_errors = sl->rx_compressed; |
| 585 | stats.tx_fifo_errors = sl->tx_compressed; |
| 586 | stats.collisions = sl->tx_misses; |
| 587 | comp = sl->slcomp; |
| 588 | if (comp) { |
| 589 | stats.rx_fifo_errors += comp->sls_i_compressed; |
| 590 | stats.rx_dropped += comp->sls_i_tossed; |
| 591 | stats.tx_fifo_errors += comp->sls_o_compressed; |
| 592 | stats.collisions += comp->sls_o_misses; |
| 593 | } |
| 594 | #endif /* CONFIG_INET */ |
| 595 | return (&stats); |
| 596 | } |
| 597 | |
| 598 | /* Netdevice register callback */ |
| 599 | |
| 600 | static int sl_init(struct net_device *dev) |
| 601 | { |
| 602 | struct slip *sl = netdev_priv(dev); |
| 603 | |
| 604 | /* |
Jeff Garzik | 6aa20a2 | 2006-09-13 13:24:59 -0400 | [diff] [blame^] | 605 | * Finish setting up the DEVICE info. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 606 | */ |
| 607 | |
| 608 | dev->mtu = sl->mtu; |
| 609 | dev->type = ARPHRD_SLIP + sl->mode; |
| 610 | #ifdef SL_CHECK_TRANSMIT |
| 611 | dev->tx_timeout = sl_tx_timeout; |
| 612 | dev->watchdog_timeo = 20*HZ; |
| 613 | #endif |
| 614 | return 0; |
| 615 | } |
| 616 | |
| 617 | |
| 618 | static void sl_uninit(struct net_device *dev) |
| 619 | { |
| 620 | struct slip *sl = netdev_priv(dev); |
| 621 | |
| 622 | sl_free_bufs(sl); |
| 623 | } |
| 624 | |
| 625 | static void sl_setup(struct net_device *dev) |
| 626 | { |
| 627 | dev->init = sl_init; |
| 628 | dev->uninit = sl_uninit; |
| 629 | dev->open = sl_open; |
| 630 | dev->destructor = free_netdev; |
| 631 | dev->stop = sl_close; |
| 632 | dev->get_stats = sl_get_stats; |
| 633 | dev->change_mtu = sl_change_mtu; |
| 634 | dev->hard_start_xmit = sl_xmit; |
| 635 | #ifdef CONFIG_SLIP_SMART |
| 636 | dev->do_ioctl = sl_ioctl; |
| 637 | #endif |
| 638 | dev->hard_header_len = 0; |
| 639 | dev->addr_len = 0; |
| 640 | dev->tx_queue_len = 10; |
| 641 | |
| 642 | SET_MODULE_OWNER(dev); |
| 643 | |
| 644 | /* New-style flags. */ |
| 645 | dev->flags = IFF_NOARP|IFF_POINTOPOINT|IFF_MULTICAST; |
| 646 | } |
| 647 | |
| 648 | /****************************************** |
| 649 | Routines looking at TTY side. |
| 650 | ******************************************/ |
| 651 | |
| 652 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 653 | /* |
| 654 | * Handle the 'receiver data ready' interrupt. |
| 655 | * This function is called by the 'tty_io' module in the kernel when |
| 656 | * a block of SLIP data has been received, which can now be decapsulated |
| 657 | * and sent on to some IP layer for further processing. This will not |
| 658 | * be re-entered while running but other ldisc functions may be called |
| 659 | * in parallel |
| 660 | */ |
Jeff Garzik | 6aa20a2 | 2006-09-13 13:24:59 -0400 | [diff] [blame^] | 661 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 662 | static void slip_receive_buf(struct tty_struct *tty, const unsigned char *cp, char *fp, int count) |
| 663 | { |
| 664 | struct slip *sl = (struct slip *) tty->disc_data; |
| 665 | |
| 666 | if (!sl || sl->magic != SLIP_MAGIC || |
| 667 | !netif_running(sl->dev)) |
| 668 | return; |
| 669 | |
| 670 | /* Read the characters out of the buffer */ |
| 671 | while (count--) { |
| 672 | if (fp && *fp++) { |
| 673 | if (!test_and_set_bit(SLF_ERROR, &sl->flags)) { |
| 674 | sl->rx_errors++; |
| 675 | } |
| 676 | cp++; |
| 677 | continue; |
| 678 | } |
| 679 | #ifdef CONFIG_SLIP_MODE_SLIP6 |
| 680 | if (sl->mode & SL_MODE_SLIP6) |
| 681 | slip_unesc6(sl, *cp++); |
| 682 | else |
| 683 | #endif |
| 684 | slip_unesc(sl, *cp++); |
| 685 | } |
| 686 | } |
| 687 | |
| 688 | /************************************ |
| 689 | * slip_open helper routines. |
| 690 | ************************************/ |
| 691 | |
| 692 | /* Collect hanged up channels */ |
| 693 | |
| 694 | static void sl_sync(void) |
| 695 | { |
| 696 | int i; |
| 697 | struct net_device *dev; |
| 698 | struct slip *sl; |
| 699 | |
| 700 | for (i = 0; i < slip_maxdev; i++) { |
| 701 | if ((dev = slip_devs[i]) == NULL) |
| 702 | break; |
| 703 | |
| 704 | sl = netdev_priv(dev); |
| 705 | if (sl->tty || sl->leased) |
| 706 | continue; |
| 707 | if (dev->flags&IFF_UP) |
| 708 | dev_close(dev); |
| 709 | } |
| 710 | } |
| 711 | |
| 712 | |
| 713 | /* Find a free SLIP channel, and link in this `tty' line. */ |
| 714 | static struct slip * |
| 715 | sl_alloc(dev_t line) |
| 716 | { |
| 717 | int i; |
| 718 | int sel = -1; |
| 719 | int score = -1; |
| 720 | struct net_device *dev = NULL; |
| 721 | struct slip *sl; |
| 722 | |
Jeff Garzik | 6aa20a2 | 2006-09-13 13:24:59 -0400 | [diff] [blame^] | 723 | if (slip_devs == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 724 | return NULL; /* Master array missing ! */ |
| 725 | |
| 726 | for (i = 0; i < slip_maxdev; i++) { |
| 727 | dev = slip_devs[i]; |
| 728 | if (dev == NULL) |
| 729 | break; |
| 730 | |
| 731 | sl = netdev_priv(dev); |
| 732 | if (sl->leased) { |
| 733 | if (sl->line != line) |
| 734 | continue; |
| 735 | if (sl->tty) |
| 736 | return NULL; |
| 737 | |
| 738 | /* Clear ESCAPE & ERROR flags */ |
| 739 | sl->flags &= (1 << SLF_INUSE); |
| 740 | return sl; |
| 741 | } |
| 742 | |
| 743 | if (sl->tty) |
| 744 | continue; |
| 745 | |
| 746 | if (current->pid == sl->pid) { |
| 747 | if (sl->line == line && score < 3) { |
| 748 | sel = i; |
| 749 | score = 3; |
| 750 | continue; |
| 751 | } |
| 752 | if (score < 2) { |
| 753 | sel = i; |
| 754 | score = 2; |
| 755 | } |
| 756 | continue; |
| 757 | } |
| 758 | if (sl->line == line && score < 1) { |
| 759 | sel = i; |
| 760 | score = 1; |
| 761 | continue; |
| 762 | } |
| 763 | if (score < 0) { |
| 764 | sel = i; |
| 765 | score = 0; |
| 766 | } |
| 767 | } |
| 768 | |
| 769 | if (sel >= 0) { |
| 770 | i = sel; |
| 771 | dev = slip_devs[i]; |
| 772 | if (score > 1) { |
| 773 | sl = netdev_priv(dev); |
| 774 | sl->flags &= (1 << SLF_INUSE); |
| 775 | return sl; |
| 776 | } |
| 777 | } |
| 778 | |
| 779 | /* Sorry, too many, all slots in use */ |
| 780 | if (i >= slip_maxdev) |
| 781 | return NULL; |
| 782 | |
| 783 | if (dev) { |
| 784 | sl = netdev_priv(dev); |
| 785 | if (test_bit(SLF_INUSE, &sl->flags)) { |
| 786 | unregister_netdevice(dev); |
| 787 | dev = NULL; |
| 788 | slip_devs[i] = NULL; |
| 789 | } |
| 790 | } |
Jeff Garzik | 6aa20a2 | 2006-09-13 13:24:59 -0400 | [diff] [blame^] | 791 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 792 | if (!dev) { |
| 793 | char name[IFNAMSIZ]; |
| 794 | sprintf(name, "sl%d", i); |
| 795 | |
| 796 | dev = alloc_netdev(sizeof(*sl), name, sl_setup); |
| 797 | if (!dev) |
| 798 | return NULL; |
| 799 | dev->base_addr = i; |
| 800 | } |
| 801 | |
| 802 | sl = netdev_priv(dev); |
| 803 | |
| 804 | /* Initialize channel control data */ |
| 805 | sl->magic = SLIP_MAGIC; |
| 806 | sl->dev = dev; |
| 807 | spin_lock_init(&sl->lock); |
| 808 | sl->mode = SL_MODE_DEFAULT; |
| 809 | #ifdef CONFIG_SLIP_SMART |
| 810 | init_timer(&sl->keepalive_timer); /* initialize timer_list struct */ |
| 811 | sl->keepalive_timer.data=(unsigned long)sl; |
| 812 | sl->keepalive_timer.function=sl_keepalive; |
| 813 | init_timer(&sl->outfill_timer); |
| 814 | sl->outfill_timer.data=(unsigned long)sl; |
| 815 | sl->outfill_timer.function=sl_outfill; |
| 816 | #endif |
| 817 | slip_devs[i] = dev; |
Jeff Garzik | 6aa20a2 | 2006-09-13 13:24:59 -0400 | [diff] [blame^] | 818 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 819 | return sl; |
| 820 | } |
| 821 | |
| 822 | /* |
| 823 | * Open the high-level part of the SLIP channel. |
| 824 | * This function is called by the TTY module when the |
| 825 | * SLIP line discipline is called for. Because we are |
| 826 | * sure the tty line exists, we only have to link it to |
| 827 | * a free SLIP channel... |
| 828 | * |
| 829 | * Called in process context serialized from other ldisc calls. |
| 830 | */ |
| 831 | |
| 832 | static int slip_open(struct tty_struct *tty) |
| 833 | { |
| 834 | struct slip *sl; |
| 835 | int err; |
| 836 | |
| 837 | if(!capable(CAP_NET_ADMIN)) |
| 838 | return -EPERM; |
Jeff Garzik | 6aa20a2 | 2006-09-13 13:24:59 -0400 | [diff] [blame^] | 839 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 840 | /* RTnetlink lock is misused here to serialize concurrent |
| 841 | opens of slip channels. There are better ways, but it is |
| 842 | the simplest one. |
| 843 | */ |
| 844 | rtnl_lock(); |
| 845 | |
| 846 | /* Collect hanged up channels. */ |
| 847 | sl_sync(); |
| 848 | |
| 849 | sl = (struct slip *) tty->disc_data; |
| 850 | |
| 851 | err = -EEXIST; |
| 852 | /* First make sure we're not already connected. */ |
| 853 | if (sl && sl->magic == SLIP_MAGIC) |
| 854 | goto err_exit; |
| 855 | |
| 856 | /* OK. Find a free SLIP channel to use. */ |
| 857 | err = -ENFILE; |
| 858 | if ((sl = sl_alloc(tty_devnum(tty))) == NULL) |
| 859 | goto err_exit; |
| 860 | |
| 861 | sl->tty = tty; |
| 862 | tty->disc_data = sl; |
| 863 | sl->line = tty_devnum(tty); |
| 864 | sl->pid = current->pid; |
Jeff Garzik | 6aa20a2 | 2006-09-13 13:24:59 -0400 | [diff] [blame^] | 865 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 866 | if (!test_bit(SLF_INUSE, &sl->flags)) { |
| 867 | /* Perform the low-level SLIP initialization. */ |
| 868 | if ((err = sl_alloc_bufs(sl, SL_MTU)) != 0) |
| 869 | goto err_free_chan; |
| 870 | |
| 871 | set_bit(SLF_INUSE, &sl->flags); |
| 872 | |
| 873 | if ((err = register_netdevice(sl->dev))) |
| 874 | goto err_free_bufs; |
| 875 | } |
| 876 | |
| 877 | #ifdef CONFIG_SLIP_SMART |
| 878 | if (sl->keepalive) { |
| 879 | sl->keepalive_timer.expires=jiffies+sl->keepalive*HZ; |
| 880 | add_timer (&sl->keepalive_timer); |
| 881 | } |
| 882 | if (sl->outfill) { |
| 883 | sl->outfill_timer.expires=jiffies+sl->outfill*HZ; |
| 884 | add_timer (&sl->outfill_timer); |
| 885 | } |
| 886 | #endif |
| 887 | |
| 888 | /* Done. We have linked the TTY line to a channel. */ |
| 889 | rtnl_unlock(); |
Alan Cox | 33f0f88 | 2006-01-09 20:54:13 -0800 | [diff] [blame] | 890 | tty->receive_room = 65536; /* We don't flow control */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 891 | return sl->dev->base_addr; |
| 892 | |
| 893 | err_free_bufs: |
| 894 | sl_free_bufs(sl); |
| 895 | |
| 896 | err_free_chan: |
| 897 | sl->tty = NULL; |
| 898 | tty->disc_data = NULL; |
| 899 | clear_bit(SLF_INUSE, &sl->flags); |
| 900 | |
| 901 | err_exit: |
| 902 | rtnl_unlock(); |
| 903 | |
| 904 | /* Count references from TTY module */ |
| 905 | return err; |
| 906 | } |
| 907 | |
| 908 | /* |
| 909 | |
| 910 | FIXME: 1,2 are fixed 3 was never true anyway. |
Jeff Garzik | 6aa20a2 | 2006-09-13 13:24:59 -0400 | [diff] [blame^] | 911 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 912 | Let me to blame a bit. |
| 913 | 1. TTY module calls this funstion on soft interrupt. |
| 914 | 2. TTY module calls this function WITH MASKED INTERRUPTS! |
| 915 | 3. TTY module does not notify us about line discipline |
| 916 | shutdown, |
| 917 | |
| 918 | Seems, now it is clean. The solution is to consider netdevice and |
| 919 | line discipline sides as two independent threads. |
| 920 | |
| 921 | By-product (not desired): sl? does not feel hangups and remains open. |
| 922 | It is supposed, that user level program (dip, diald, slattach...) |
Jeff Garzik | 6aa20a2 | 2006-09-13 13:24:59 -0400 | [diff] [blame^] | 923 | will catch SIGHUP and make the rest of work. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 924 | |
| 925 | I see no way to make more with current tty code. --ANK |
| 926 | */ |
| 927 | |
| 928 | /* |
| 929 | * Close down a SLIP channel. |
| 930 | * This means flushing out any pending queues, and then returning. This |
| 931 | * call is serialized against other ldisc functions. |
| 932 | */ |
| 933 | static void |
| 934 | slip_close(struct tty_struct *tty) |
| 935 | { |
| 936 | struct slip *sl = (struct slip *) tty->disc_data; |
| 937 | |
| 938 | /* First make sure we're connected. */ |
| 939 | if (!sl || sl->magic != SLIP_MAGIC || sl->tty != tty) |
| 940 | return; |
| 941 | |
| 942 | tty->disc_data = NULL; |
| 943 | sl->tty = NULL; |
| 944 | if (!sl->leased) |
| 945 | sl->line = 0; |
| 946 | |
| 947 | /* VSV = very important to remove timers */ |
| 948 | #ifdef CONFIG_SLIP_SMART |
| 949 | del_timer_sync(&sl->keepalive_timer); |
| 950 | del_timer_sync(&sl->outfill_timer); |
| 951 | #endif |
| 952 | |
| 953 | /* Count references from TTY module */ |
| 954 | } |
| 955 | |
| 956 | /************************************************************************ |
| 957 | * STANDARD SLIP ENCAPSULATION * |
| 958 | ************************************************************************/ |
| 959 | |
| 960 | int |
| 961 | slip_esc(unsigned char *s, unsigned char *d, int len) |
| 962 | { |
| 963 | unsigned char *ptr = d; |
| 964 | unsigned char c; |
| 965 | |
| 966 | /* |
| 967 | * Send an initial END character to flush out any |
| 968 | * data that may have accumulated in the receiver |
| 969 | * due to line noise. |
| 970 | */ |
| 971 | |
| 972 | *ptr++ = END; |
| 973 | |
| 974 | /* |
| 975 | * For each byte in the packet, send the appropriate |
| 976 | * character sequence, according to the SLIP protocol. |
| 977 | */ |
| 978 | |
| 979 | while (len-- > 0) { |
| 980 | switch(c = *s++) { |
| 981 | case END: |
| 982 | *ptr++ = ESC; |
| 983 | *ptr++ = ESC_END; |
| 984 | break; |
| 985 | case ESC: |
| 986 | *ptr++ = ESC; |
| 987 | *ptr++ = ESC_ESC; |
| 988 | break; |
| 989 | default: |
| 990 | *ptr++ = c; |
| 991 | break; |
| 992 | } |
| 993 | } |
| 994 | *ptr++ = END; |
| 995 | return (ptr - d); |
| 996 | } |
| 997 | |
| 998 | static void slip_unesc(struct slip *sl, unsigned char s) |
| 999 | { |
| 1000 | |
| 1001 | switch(s) { |
| 1002 | case END: |
| 1003 | #ifdef CONFIG_SLIP_SMART |
| 1004 | /* drop keeptest bit = VSV */ |
| 1005 | if (test_bit(SLF_KEEPTEST, &sl->flags)) |
| 1006 | clear_bit(SLF_KEEPTEST, &sl->flags); |
| 1007 | #endif |
| 1008 | |
| 1009 | if (!test_and_clear_bit(SLF_ERROR, &sl->flags) && (sl->rcount > 2)) { |
| 1010 | sl_bump(sl); |
| 1011 | } |
| 1012 | clear_bit(SLF_ESCAPE, &sl->flags); |
| 1013 | sl->rcount = 0; |
| 1014 | return; |
| 1015 | |
| 1016 | case ESC: |
| 1017 | set_bit(SLF_ESCAPE, &sl->flags); |
| 1018 | return; |
| 1019 | case ESC_ESC: |
| 1020 | if (test_and_clear_bit(SLF_ESCAPE, &sl->flags)) { |
| 1021 | s = ESC; |
| 1022 | } |
| 1023 | break; |
| 1024 | case ESC_END: |
| 1025 | if (test_and_clear_bit(SLF_ESCAPE, &sl->flags)) { |
| 1026 | s = END; |
| 1027 | } |
| 1028 | break; |
| 1029 | } |
| 1030 | if (!test_bit(SLF_ERROR, &sl->flags)) { |
| 1031 | if (sl->rcount < sl->buffsize) { |
| 1032 | sl->rbuff[sl->rcount++] = s; |
| 1033 | return; |
| 1034 | } |
| 1035 | sl->rx_over_errors++; |
| 1036 | set_bit(SLF_ERROR, &sl->flags); |
| 1037 | } |
| 1038 | } |
| 1039 | |
| 1040 | |
| 1041 | #ifdef CONFIG_SLIP_MODE_SLIP6 |
| 1042 | /************************************************************************ |
| 1043 | * 6 BIT SLIP ENCAPSULATION * |
| 1044 | ************************************************************************/ |
| 1045 | |
| 1046 | int |
| 1047 | slip_esc6(unsigned char *s, unsigned char *d, int len) |
| 1048 | { |
| 1049 | unsigned char *ptr = d; |
| 1050 | unsigned char c; |
| 1051 | int i; |
| 1052 | unsigned short v = 0; |
| 1053 | short bits = 0; |
| 1054 | |
| 1055 | /* |
| 1056 | * Send an initial END character to flush out any |
| 1057 | * data that may have accumulated in the receiver |
| 1058 | * due to line noise. |
| 1059 | */ |
| 1060 | |
| 1061 | *ptr++ = 0x70; |
| 1062 | |
| 1063 | /* |
| 1064 | * Encode the packet into printable ascii characters |
| 1065 | */ |
| 1066 | |
| 1067 | for (i = 0; i < len; ++i) { |
| 1068 | v = (v << 8) | s[i]; |
| 1069 | bits += 8; |
| 1070 | while (bits >= 6) { |
| 1071 | bits -= 6; |
| 1072 | c = 0x30 + ((v >> bits) & 0x3F); |
| 1073 | *ptr++ = c; |
| 1074 | } |
| 1075 | } |
| 1076 | if (bits) { |
| 1077 | c = 0x30 + ((v << (6 - bits)) & 0x3F); |
| 1078 | *ptr++ = c; |
| 1079 | } |
| 1080 | *ptr++ = 0x70; |
| 1081 | return ptr - d; |
| 1082 | } |
| 1083 | |
| 1084 | void |
| 1085 | slip_unesc6(struct slip *sl, unsigned char s) |
| 1086 | { |
| 1087 | unsigned char c; |
| 1088 | |
| 1089 | if (s == 0x70) { |
| 1090 | #ifdef CONFIG_SLIP_SMART |
| 1091 | /* drop keeptest bit = VSV */ |
| 1092 | if (test_bit(SLF_KEEPTEST, &sl->flags)) |
| 1093 | clear_bit(SLF_KEEPTEST, &sl->flags); |
| 1094 | #endif |
| 1095 | |
| 1096 | if (!test_and_clear_bit(SLF_ERROR, &sl->flags) && (sl->rcount > 2)) { |
| 1097 | sl_bump(sl); |
| 1098 | } |
| 1099 | sl->rcount = 0; |
| 1100 | sl->xbits = 0; |
| 1101 | sl->xdata = 0; |
| 1102 | } else if (s >= 0x30 && s < 0x70) { |
| 1103 | sl->xdata = (sl->xdata << 6) | ((s - 0x30) & 0x3F); |
| 1104 | sl->xbits += 6; |
| 1105 | if (sl->xbits >= 8) { |
| 1106 | sl->xbits -= 8; |
| 1107 | c = (unsigned char)(sl->xdata >> sl->xbits); |
| 1108 | if (!test_bit(SLF_ERROR, &sl->flags)) { |
| 1109 | if (sl->rcount < sl->buffsize) { |
| 1110 | sl->rbuff[sl->rcount++] = c; |
| 1111 | return; |
| 1112 | } |
| 1113 | sl->rx_over_errors++; |
| 1114 | set_bit(SLF_ERROR, &sl->flags); |
| 1115 | } |
| 1116 | } |
| 1117 | } |
| 1118 | } |
| 1119 | #endif /* CONFIG_SLIP_MODE_SLIP6 */ |
| 1120 | |
| 1121 | /* Perform I/O control on an active SLIP channel. */ |
| 1122 | static int slip_ioctl(struct tty_struct *tty, struct file *file, unsigned int cmd, unsigned long arg) |
| 1123 | { |
| 1124 | struct slip *sl = (struct slip *) tty->disc_data; |
| 1125 | unsigned int tmp; |
| 1126 | int __user *p = (int __user *)arg; |
| 1127 | |
| 1128 | /* First make sure we're connected. */ |
| 1129 | if (!sl || sl->magic != SLIP_MAGIC) { |
| 1130 | return -EINVAL; |
| 1131 | } |
| 1132 | |
| 1133 | switch(cmd) { |
| 1134 | case SIOCGIFNAME: |
| 1135 | tmp = strlen(sl->dev->name) + 1; |
| 1136 | if (copy_to_user((void __user *)arg, sl->dev->name, tmp)) |
| 1137 | return -EFAULT; |
| 1138 | return 0; |
| 1139 | |
| 1140 | case SIOCGIFENCAP: |
| 1141 | if (put_user(sl->mode, p)) |
| 1142 | return -EFAULT; |
| 1143 | return 0; |
| 1144 | |
| 1145 | case SIOCSIFENCAP: |
| 1146 | if (get_user(tmp, p)) |
| 1147 | return -EFAULT; |
| 1148 | #ifndef SL_INCLUDE_CSLIP |
| 1149 | if (tmp & (SL_MODE_CSLIP|SL_MODE_ADAPTIVE)) { |
| 1150 | return -EINVAL; |
| 1151 | } |
| 1152 | #else |
| 1153 | if ((tmp & (SL_MODE_ADAPTIVE | SL_MODE_CSLIP)) == |
| 1154 | (SL_MODE_ADAPTIVE | SL_MODE_CSLIP)) { |
| 1155 | /* return -EINVAL; */ |
| 1156 | tmp &= ~SL_MODE_ADAPTIVE; |
| 1157 | } |
| 1158 | #endif |
| 1159 | #ifndef CONFIG_SLIP_MODE_SLIP6 |
| 1160 | if (tmp & SL_MODE_SLIP6) { |
| 1161 | return -EINVAL; |
| 1162 | } |
| 1163 | #endif |
| 1164 | sl->mode = tmp; |
| 1165 | sl->dev->type = ARPHRD_SLIP+sl->mode; |
| 1166 | return 0; |
| 1167 | |
| 1168 | case SIOCSIFHWADDR: |
| 1169 | return -EINVAL; |
| 1170 | |
| 1171 | #ifdef CONFIG_SLIP_SMART |
| 1172 | /* VSV changes start here */ |
| 1173 | case SIOCSKEEPALIVE: |
| 1174 | if (get_user(tmp, p)) |
| 1175 | return -EFAULT; |
| 1176 | if (tmp > 255) /* max for unchar */ |
| 1177 | return -EINVAL; |
| 1178 | |
| 1179 | spin_lock_bh(&sl->lock); |
| 1180 | if (!sl->tty) { |
| 1181 | spin_unlock_bh(&sl->lock); |
| 1182 | return -ENODEV; |
| 1183 | } |
| 1184 | if ((sl->keepalive = (unchar) tmp) != 0) { |
| 1185 | mod_timer(&sl->keepalive_timer, jiffies+sl->keepalive*HZ); |
| 1186 | set_bit(SLF_KEEPTEST, &sl->flags); |
| 1187 | } else { |
| 1188 | del_timer (&sl->keepalive_timer); |
| 1189 | } |
| 1190 | spin_unlock_bh(&sl->lock); |
| 1191 | return 0; |
| 1192 | |
| 1193 | case SIOCGKEEPALIVE: |
| 1194 | if (put_user(sl->keepalive, p)) |
| 1195 | return -EFAULT; |
| 1196 | return 0; |
| 1197 | |
| 1198 | case SIOCSOUTFILL: |
| 1199 | if (get_user(tmp, p)) |
| 1200 | return -EFAULT; |
| 1201 | if (tmp > 255) /* max for unchar */ |
| 1202 | return -EINVAL; |
| 1203 | spin_lock_bh(&sl->lock); |
| 1204 | if (!sl->tty) { |
| 1205 | spin_unlock_bh(&sl->lock); |
| 1206 | return -ENODEV; |
| 1207 | } |
| 1208 | if ((sl->outfill = (unchar) tmp) != 0){ |
| 1209 | mod_timer(&sl->outfill_timer, jiffies+sl->outfill*HZ); |
| 1210 | set_bit(SLF_OUTWAIT, &sl->flags); |
| 1211 | } else { |
| 1212 | del_timer (&sl->outfill_timer); |
| 1213 | } |
| 1214 | spin_unlock_bh(&sl->lock); |
| 1215 | return 0; |
| 1216 | |
| 1217 | case SIOCGOUTFILL: |
| 1218 | if (put_user(sl->outfill, p)) |
| 1219 | return -EFAULT; |
| 1220 | return 0; |
| 1221 | /* VSV changes end */ |
| 1222 | #endif |
| 1223 | |
| 1224 | /* Allow stty to read, but not set, the serial port */ |
| 1225 | case TCGETS: |
| 1226 | case TCGETA: |
| 1227 | return n_tty_ioctl(tty, file, cmd, arg); |
| 1228 | |
| 1229 | default: |
| 1230 | return -ENOIOCTLCMD; |
| 1231 | } |
| 1232 | } |
| 1233 | |
| 1234 | /* VSV changes start here */ |
| 1235 | #ifdef CONFIG_SLIP_SMART |
| 1236 | /* function do_ioctl called from net/core/dev.c |
| 1237 | to allow get/set outfill/keepalive parameter |
| 1238 | by ifconfig */ |
| 1239 | |
| 1240 | static int sl_ioctl(struct net_device *dev,struct ifreq *rq,int cmd) |
| 1241 | { |
| 1242 | struct slip *sl = netdev_priv(dev); |
| 1243 | unsigned long *p = (unsigned long *)&rq->ifr_ifru; |
| 1244 | |
| 1245 | if (sl == NULL) /* Allocation failed ?? */ |
| 1246 | return -ENODEV; |
| 1247 | |
| 1248 | spin_lock_bh(&sl->lock); |
| 1249 | |
| 1250 | if (!sl->tty) { |
| 1251 | spin_unlock_bh(&sl->lock); |
| 1252 | return -ENODEV; |
| 1253 | } |
| 1254 | |
| 1255 | switch(cmd){ |
| 1256 | case SIOCSKEEPALIVE: |
| 1257 | /* max for unchar */ |
| 1258 | if ((unsigned)*p > 255) { |
| 1259 | spin_unlock_bh(&sl->lock); |
| 1260 | return -EINVAL; |
| 1261 | } |
| 1262 | sl->keepalive = (unchar) *p; |
| 1263 | if (sl->keepalive != 0) { |
| 1264 | sl->keepalive_timer.expires=jiffies+sl->keepalive*HZ; |
| 1265 | mod_timer(&sl->keepalive_timer, jiffies+sl->keepalive*HZ); |
| 1266 | set_bit(SLF_KEEPTEST, &sl->flags); |
| 1267 | } else { |
| 1268 | del_timer(&sl->keepalive_timer); |
| 1269 | } |
| 1270 | break; |
| 1271 | |
| 1272 | case SIOCGKEEPALIVE: |
| 1273 | *p = sl->keepalive; |
| 1274 | break; |
| 1275 | |
| 1276 | case SIOCSOUTFILL: |
| 1277 | if ((unsigned)*p > 255) { /* max for unchar */ |
| 1278 | spin_unlock_bh(&sl->lock); |
| 1279 | return -EINVAL; |
| 1280 | } |
| 1281 | if ((sl->outfill = (unchar)*p) != 0){ |
| 1282 | mod_timer(&sl->outfill_timer, jiffies+sl->outfill*HZ); |
| 1283 | set_bit(SLF_OUTWAIT, &sl->flags); |
| 1284 | } else { |
| 1285 | del_timer (&sl->outfill_timer); |
| 1286 | } |
| 1287 | break; |
| 1288 | |
| 1289 | case SIOCGOUTFILL: |
| 1290 | *p = sl->outfill; |
| 1291 | break; |
| 1292 | |
| 1293 | case SIOCSLEASE: |
Jeff Garzik | 6aa20a2 | 2006-09-13 13:24:59 -0400 | [diff] [blame^] | 1294 | /* Resolve race condition, when ioctl'ing hanged up |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1295 | and opened by another process device. |
| 1296 | */ |
| 1297 | if (sl->tty != current->signal->tty && sl->pid != current->pid) { |
| 1298 | spin_unlock_bh(&sl->lock); |
| 1299 | return -EPERM; |
| 1300 | } |
| 1301 | sl->leased = 0; |
| 1302 | if (*p) |
| 1303 | sl->leased = 1; |
| 1304 | break; |
| 1305 | |
| 1306 | case SIOCGLEASE: |
| 1307 | *p = sl->leased; |
| 1308 | }; |
| 1309 | spin_unlock_bh(&sl->lock); |
| 1310 | return 0; |
| 1311 | } |
| 1312 | #endif |
| 1313 | /* VSV changes end */ |
| 1314 | |
| 1315 | static struct tty_ldisc sl_ldisc = { |
| 1316 | .owner = THIS_MODULE, |
| 1317 | .magic = TTY_LDISC_MAGIC, |
| 1318 | .name = "slip", |
| 1319 | .open = slip_open, |
| 1320 | .close = slip_close, |
| 1321 | .ioctl = slip_ioctl, |
| 1322 | .receive_buf = slip_receive_buf, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1323 | .write_wakeup = slip_write_wakeup, |
| 1324 | }; |
| 1325 | |
| 1326 | static int __init slip_init(void) |
| 1327 | { |
| 1328 | int status; |
| 1329 | |
| 1330 | if (slip_maxdev < 4) |
| 1331 | slip_maxdev = 4; /* Sanity */ |
| 1332 | |
| 1333 | printk(KERN_INFO "SLIP: version %s (dynamic channels, max=%d)" |
| 1334 | #ifdef CONFIG_SLIP_MODE_SLIP6 |
| 1335 | " (6 bit encapsulation enabled)" |
| 1336 | #endif |
| 1337 | ".\n", |
| 1338 | SLIP_VERSION, slip_maxdev ); |
| 1339 | #if defined(SL_INCLUDE_CSLIP) |
| 1340 | printk(KERN_INFO "CSLIP: code copyright 1989 Regents of the University of California.\n"); |
| 1341 | #endif |
| 1342 | #ifdef CONFIG_SLIP_SMART |
| 1343 | printk(KERN_INFO "SLIP linefill/keepalive option.\n"); |
| 1344 | #endif |
| 1345 | |
| 1346 | slip_devs = kmalloc(sizeof(struct net_device *)*slip_maxdev, GFP_KERNEL); |
| 1347 | if (!slip_devs) { |
| 1348 | printk(KERN_ERR "SLIP: Can't allocate slip devices array! Uaargh! (-> No SLIP available)\n"); |
| 1349 | return -ENOMEM; |
| 1350 | } |
| 1351 | |
| 1352 | /* Clear the pointer array, we allocate devices when we need them */ |
Jeff Garzik | 6aa20a2 | 2006-09-13 13:24:59 -0400 | [diff] [blame^] | 1353 | memset(slip_devs, 0, sizeof(struct net_device *)*slip_maxdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1354 | |
| 1355 | /* Fill in our line protocol discipline, and register it */ |
| 1356 | if ((status = tty_register_ldisc(N_SLIP, &sl_ldisc)) != 0) { |
| 1357 | printk(KERN_ERR "SLIP: can't register line discipline (err = %d)\n", status); |
| 1358 | kfree(slip_devs); |
| 1359 | } |
| 1360 | return status; |
| 1361 | } |
| 1362 | |
| 1363 | static void __exit slip_exit(void) |
| 1364 | { |
| 1365 | int i; |
| 1366 | struct net_device *dev; |
| 1367 | struct slip *sl; |
| 1368 | unsigned long timeout = jiffies + HZ; |
| 1369 | int busy = 0; |
| 1370 | |
Jeff Garzik | 6aa20a2 | 2006-09-13 13:24:59 -0400 | [diff] [blame^] | 1371 | if (slip_devs == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1372 | return; |
| 1373 | |
| 1374 | /* First of all: check for active disciplines and hangup them. |
| 1375 | */ |
| 1376 | do { |
Nishanth Aravamudan | a9fc251 | 2005-05-01 23:34:57 -0700 | [diff] [blame] | 1377 | if (busy) |
| 1378 | msleep_interruptible(100); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1379 | |
| 1380 | busy = 0; |
| 1381 | for (i = 0; i < slip_maxdev; i++) { |
| 1382 | dev = slip_devs[i]; |
| 1383 | if (!dev) |
| 1384 | continue; |
| 1385 | sl = netdev_priv(dev); |
| 1386 | spin_lock_bh(&sl->lock); |
| 1387 | if (sl->tty) { |
| 1388 | busy++; |
| 1389 | tty_hangup(sl->tty); |
| 1390 | } |
| 1391 | spin_unlock_bh(&sl->lock); |
| 1392 | } |
| 1393 | } while (busy && time_before(jiffies, timeout)); |
| 1394 | |
| 1395 | |
| 1396 | for (i = 0; i < slip_maxdev; i++) { |
| 1397 | dev = slip_devs[i]; |
| 1398 | if (!dev) |
| 1399 | continue; |
| 1400 | slip_devs[i] = NULL; |
| 1401 | |
| 1402 | sl = netdev_priv(dev); |
| 1403 | if (sl->tty) { |
| 1404 | printk(KERN_ERR "%s: tty discipline still running\n", |
| 1405 | dev->name); |
| 1406 | /* Intentionally leak the control block. */ |
| 1407 | dev->destructor = NULL; |
Jeff Garzik | 6aa20a2 | 2006-09-13 13:24:59 -0400 | [diff] [blame^] | 1408 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1409 | |
| 1410 | unregister_netdev(dev); |
| 1411 | } |
| 1412 | |
| 1413 | kfree(slip_devs); |
| 1414 | slip_devs = NULL; |
| 1415 | |
Alexey Dobriyan | 64ccd71 | 2005-06-23 00:10:33 -0700 | [diff] [blame] | 1416 | if ((i = tty_unregister_ldisc(N_SLIP))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1417 | { |
| 1418 | printk(KERN_ERR "SLIP: can't unregister line discipline (err = %d)\n", i); |
| 1419 | } |
| 1420 | } |
| 1421 | |
| 1422 | module_init(slip_init); |
| 1423 | module_exit(slip_exit); |
| 1424 | |
| 1425 | #ifdef CONFIG_SLIP_SMART |
| 1426 | /* |
| 1427 | * This is start of the code for multislip style line checking |
| 1428 | * added by Stanislav Voronyi. All changes before marked VSV |
| 1429 | */ |
| 1430 | |
| 1431 | static void sl_outfill(unsigned long sls) |
| 1432 | { |
| 1433 | struct slip *sl=(struct slip *)sls; |
| 1434 | |
| 1435 | spin_lock(&sl->lock); |
| 1436 | |
| 1437 | if (sl->tty == NULL) |
| 1438 | goto out; |
| 1439 | |
| 1440 | if(sl->outfill) |
| 1441 | { |
| 1442 | if( test_bit(SLF_OUTWAIT, &sl->flags) ) |
| 1443 | { |
| 1444 | /* no packets were transmitted, do outfill */ |
| 1445 | #ifdef CONFIG_SLIP_MODE_SLIP6 |
| 1446 | unsigned char s = (sl->mode & SL_MODE_SLIP6)?0x70:END; |
| 1447 | #else |
| 1448 | unsigned char s = END; |
| 1449 | #endif |
| 1450 | /* put END into tty queue. Is it right ??? */ |
| 1451 | if (!netif_queue_stopped(sl->dev)) |
| 1452 | { |
| 1453 | /* if device busy no outfill */ |
| 1454 | sl->tty->driver->write(sl->tty, &s, 1); |
| 1455 | } |
| 1456 | } |
| 1457 | else |
| 1458 | set_bit(SLF_OUTWAIT, &sl->flags); |
| 1459 | |
| 1460 | mod_timer(&sl->outfill_timer, jiffies+sl->outfill*HZ); |
| 1461 | } |
| 1462 | out: |
| 1463 | spin_unlock(&sl->lock); |
| 1464 | } |
| 1465 | |
| 1466 | static void sl_keepalive(unsigned long sls) |
| 1467 | { |
| 1468 | struct slip *sl=(struct slip *)sls; |
| 1469 | |
| 1470 | spin_lock(&sl->lock); |
| 1471 | |
| 1472 | if (sl->tty == NULL) |
| 1473 | goto out; |
| 1474 | |
| 1475 | if( sl->keepalive) |
| 1476 | { |
| 1477 | if(test_bit(SLF_KEEPTEST, &sl->flags)) |
| 1478 | { |
| 1479 | /* keepalive still high :(, we must hangup */ |
| 1480 | if( sl->outfill ) /* outfill timer must be deleted too */ |
| 1481 | (void)del_timer(&sl->outfill_timer); |
| 1482 | printk(KERN_DEBUG "%s: no packets received during keepalive timeout, hangup.\n", sl->dev->name); |
| 1483 | tty_hangup(sl->tty); /* this must hangup tty & close slip */ |
| 1484 | /* I think we need not something else */ |
| 1485 | goto out; |
| 1486 | } |
| 1487 | else |
| 1488 | set_bit(SLF_KEEPTEST, &sl->flags); |
| 1489 | |
| 1490 | mod_timer(&sl->keepalive_timer, jiffies+sl->keepalive*HZ); |
| 1491 | } |
| 1492 | |
| 1493 | out: |
| 1494 | spin_unlock(&sl->lock); |
| 1495 | } |
| 1496 | |
| 1497 | #endif |
| 1498 | MODULE_LICENSE("GPL"); |
| 1499 | MODULE_ALIAS_LDISC(N_SLIP); |