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