63 #define SL_CHECK_TRANSMIT
64 #include <linux/module.h>
67 #include <asm/uaccess.h>
68 #include <linux/bitops.h>
69 #include <linux/sched.h>
70 #include <linux/string.h>
74 #include <linux/tty.h>
75 #include <linux/errno.h>
76 #include <linux/netdevice.h>
79 #include <linux/rtnetlink.h>
80 #include <linux/if_arp.h>
85 #include <linux/slab.h>
89 #include <linux/tcp.h>
93 #define SLIP_VERSION "0.8.4-NET3.019-NEWTTY"
101 static int slip_esc(
unsigned char *
p,
unsigned char *
d,
int len);
102 static void slip_unesc(
struct slip *
sl,
unsigned char c);
103 #ifdef CONFIG_SLIP_MODE_SLIP6
104 static int slip_esc6(
unsigned char *
p,
unsigned char *
d,
int len);
105 static void slip_unesc6(
struct slip *
sl,
unsigned char c);
107 #ifdef CONFIG_SLIP_SMART
108 static void sl_keepalive(
unsigned long sls);
109 static void sl_outfill(
unsigned long sls);
128 static int sl_alloc_bufs(
struct slip *
sl,
int mtu)
134 #ifdef SL_INCLUDE_CSLIP
161 #ifdef SL_INCLUDE_CSLIP
169 spin_lock_bh(&sl->
lock);
171 spin_unlock_bh(&sl->
lock);
181 #ifdef SL_INCLUDE_CSLIP
182 cbuff =
xchg(&sl->cbuff, cbuff);
183 slcomp =
xchg(&sl->slcomp, slcomp);
185 #ifdef CONFIG_SLIP_MODE_SLIP6
189 spin_unlock_bh(&sl->
lock);
194 #ifdef SL_INCLUDE_CSLIP
204 static void sl_free_bufs(
struct slip *sl)
209 #ifdef SL_INCLUDE_CSLIP
219 static int sl_realloc_bufs(
struct slip *sl,
int mtu)
223 unsigned char *xbuff, *rbuff;
224 #ifdef SL_INCLUDE_CSLIP
225 unsigned char *cbuff;
239 #ifdef SL_INCLUDE_CSLIP
244 #ifdef SL_INCLUDE_CSLIP
247 if (xbuff ==
NULL || rbuff ==
NULL) {
256 spin_lock_bh(&sl->
lock);
264 #ifdef SL_INCLUDE_CSLIP
265 cbuff =
xchg(&sl->cbuff, cbuff);
268 if (sl->
xleft <= len) {
272 dev->
stats.tx_dropped++;
282 dev->
stats.rx_over_errors++;
292 spin_unlock_bh(&sl->
lock);
297 #ifdef SL_INCLUDE_CSLIP
305 static inline void sl_lock(
struct slip *sl)
307 netif_stop_queue(sl->
dev);
312 static inline void sl_unlock(
struct slip *sl)
314 netif_wake_queue(sl->
dev);
318 static void sl_bump(
struct slip *sl)
325 #ifdef SL_INCLUDE_CSLIP
327 unsigned char c = sl->
rbuff[0];
337 dev->
stats.rx_over_errors++;
350 sl->
rbuff[0] &= 0x4f;
359 skb = dev_alloc_skb(count);
362 dev->
stats.rx_dropped++;
367 skb_reset_mac_header(skb);
370 dev->
stats.rx_packets++;
374 static void sl_encaps(
struct slip *sl,
unsigned char *icp,
int len)
381 sl->
dev->stats.tx_dropped++;
387 #ifdef SL_INCLUDE_CSLIP
391 #ifdef CONFIG_SLIP_MODE_SLIP6
393 count = slip_esc6(p, sl->
xbuff, len);
396 count = slip_esc(p, sl->
xbuff, len);
407 actual = sl->
tty->ops->write(sl->
tty, sl->
xbuff, count);
408 #ifdef SL_CHECK_TRANSMIT
411 sl->
xleft = count - actual;
413 #ifdef CONFIG_SLIP_SMART
423 static void slip_write_wakeup(
struct tty_struct *tty)
432 if (sl->
xleft <= 0) {
435 sl->
dev->stats.tx_packets++;
446 static void sl_tx_timeout(
struct net_device *dev)
448 struct slip *sl = netdev_priv(dev);
450 spin_lock(&sl->
lock);
452 if (netif_queue_stopped(dev)) {
453 if (!netif_running(dev))
459 #ifdef SL_CHECK_TRANSMIT
467 "bad line quality" :
"driver error");
474 spin_unlock(&sl->
lock);
482 struct slip *sl = netdev_priv(dev);
484 spin_lock(&sl->
lock);
485 if (!netif_running(dev)) {
486 spin_unlock(&sl->
lock);
492 spin_unlock(&sl->
lock);
499 sl_encaps(sl, skb->
data, skb->
len);
500 spin_unlock(&sl->
lock);
516 struct slip *sl = netdev_priv(dev);
518 spin_lock_bh(&sl->
lock);
522 netif_stop_queue(dev);
525 spin_unlock_bh(&sl->
lock);
534 struct slip *sl = netdev_priv(dev);
540 netif_start_queue(dev);
546 static int sl_change_mtu(
struct net_device *dev,
int new_mtu)
548 struct slip *sl = netdev_priv(dev);
550 if (new_mtu < 68 || new_mtu > 65534)
553 if (new_mtu != dev->
mtu)
554 return sl_realloc_bufs(sl, new_mtu);
564 #ifdef SL_INCLUDE_CSLIP
565 struct slip *sl = netdev_priv(dev);
578 #ifdef SL_INCLUDE_CSLIP
598 struct slip *sl = netdev_priv(dev);
606 #ifdef SL_CHECK_TRANSMIT
615 struct slip *sl = netdev_priv(dev);
621 static void sl_free_netdev(
struct net_device *dev)
630 .ndo_uninit = sl_uninit,
632 .ndo_stop = sl_close,
633 .ndo_start_xmit = sl_xmit,
634 .ndo_get_stats64 = sl_get_stats64,
635 .ndo_change_mtu = sl_change_mtu,
636 .ndo_tx_timeout = sl_tx_timeout,
637 #ifdef CONFIG_SLIP_SMART
638 .ndo_do_ioctl = sl_ioctl,
670 static void slip_receive_buf(
struct tty_struct *tty,
const unsigned char *
cp,
682 sl->
dev->stats.rx_errors++;
686 #ifdef CONFIG_SLIP_MODE_SLIP6
688 slip_unesc6(sl, *cp++);
691 slip_unesc(sl, *cp++);
700 static void sl_sync(
void)
706 for (i = 0; i < slip_maxdev; i++) {
711 sl = netdev_priv(dev);
728 for (i = 0; i < slip_maxdev; i++) {
734 if (i >= slip_maxdev)
743 sl = netdev_priv(dev);
750 #ifdef CONFIG_SLIP_SMART
753 sl->keepalive_timer.data = (
unsigned long)sl;
754 sl->keepalive_timer.function = sl_keepalive;
756 sl->outfill_timer.data = (
unsigned long)sl;
757 sl->outfill_timer.function = sl_outfill;
812 err = sl_alloc_bufs(sl,
SL_MTU);
823 #ifdef CONFIG_SLIP_SMART
825 sl->keepalive_timer.expires =
jiffies + sl->keepalive *
HZ;
829 sl->outfill_timer.expires =
jiffies + sl->outfill *
HZ;
864 static void slip_close(
struct tty_struct *tty)
876 #ifdef CONFIG_SLIP_SMART
885 static int slip_hangup(
struct tty_struct *tty)
894 static int slip_esc(
unsigned char *
s,
unsigned char *
d,
int len)
896 unsigned char *
ptr =
d;
931 static void slip_unesc(
struct slip *sl,
unsigned char s)
936 #ifdef CONFIG_SLIP_SMART
966 sl->
dev->stats.rx_over_errors++;
972 #ifdef CONFIG_SLIP_MODE_SLIP6
977 static int slip_esc6(
unsigned char *s,
unsigned char *d,
int len)
979 unsigned char *ptr =
d;
982 unsigned short v = 0;
997 for (i = 0; i < len; ++
i) {
1002 c = 0x30 + ((v >>
bits) & 0x3F);
1007 c = 0x30 + ((v << (6 -
bits)) & 0x3F);
1014 static void slip_unesc6(
struct slip *sl,
unsigned char s)
1019 #ifdef CONFIG_SLIP_SMART
1031 }
else if (s >= 0x30 && s < 0x70) {
1032 sl->xdata = (sl->xdata << 6) | ((s - 0x30) & 0x3F);
1034 if (sl->xbits >= 8) {
1036 c = (
unsigned char)(sl->xdata >> sl->xbits);
1042 sl->
dev->stats.rx_over_errors++;
1052 unsigned int cmd,
unsigned long arg)
1077 #ifndef SL_INCLUDE_CSLIP
1086 #ifndef CONFIG_SLIP_MODE_SLIP6
1097 #ifdef CONFIG_SLIP_SMART
1105 spin_lock_bh(&sl->
lock);
1107 spin_unlock_bh(&sl->
lock);
1110 sl->keepalive = (
u8)tmp;
1111 if (sl->keepalive != 0) {
1113 jiffies + sl->keepalive *
HZ);
1117 spin_unlock_bh(&sl->
lock);
1130 spin_lock_bh(&sl->
lock);
1132 spin_unlock_bh(&sl->
lock);
1135 sl->outfill = (
u8)tmp;
1136 if (sl->outfill != 0) {
1138 jiffies + sl->outfill *
HZ);
1142 spin_unlock_bh(&sl->
lock);
1156 #ifdef CONFIG_COMPAT
1157 static long slip_compat_ioctl(
struct tty_struct *tty,
struct file *file,
1158 unsigned int cmd,
unsigned long arg)
1169 return slip_ioctl(tty, file, cmd,
1170 (
unsigned long)compat_ptr(arg));
1178 #ifdef CONFIG_SLIP_SMART
1185 struct slip *sl = netdev_priv(dev);
1186 unsigned long *p = (
unsigned long *)&rq->
ifr_ifru;
1191 spin_lock_bh(&sl->
lock);
1194 spin_unlock_bh(&sl->
lock);
1201 if ((
unsigned)*p > 255) {
1202 spin_unlock_bh(&sl->
lock);
1205 sl->keepalive = (
u8)*p;
1206 if (sl->keepalive != 0) {
1207 sl->keepalive_timer.expires =
1210 jiffies + sl->keepalive *
HZ);
1221 if ((
unsigned)*p > 255) {
1222 spin_unlock_bh(&sl->
lock);
1225 sl->outfill = (
u8)*p;
1226 if (sl->outfill != 0) {
1228 jiffies + sl->outfill *
HZ);
1244 spin_unlock_bh(&sl->
lock);
1255 spin_unlock_bh(&sl->
lock);
1266 .close = slip_close,
1267 .hangup = slip_hangup,
1268 .ioctl = slip_ioctl,
1269 #ifdef CONFIG_COMPAT
1270 .compat_ioctl = slip_compat_ioctl,
1272 .receive_buf = slip_receive_buf,
1273 .write_wakeup = slip_write_wakeup,
1280 if (slip_maxdev < 4)
1284 #ifdef CONFIG_SLIP_MODE_SLIP6
1285 " (6 bit encapsulation enabled)"
1289 #if defined(SL_INCLUDE_CSLIP)
1290 printk(
KERN_INFO "CSLIP: code copyright 1989 Regents of the University of California.\n");
1292 #ifdef CONFIG_SLIP_SMART
1296 slip_devs = kzalloc(
sizeof(
struct net_device *)*slip_maxdev,
1304 printk(
KERN_ERR "SLIP: can't register line discipline (err = %d)\n", status);
1310 static void __exit slip_exit(
void)
1318 if (slip_devs ==
NULL)
1328 for (i = 0; i < slip_maxdev; i++) {
1332 sl = netdev_priv(dev);
1333 spin_lock_bh(&sl->
lock);
1338 spin_unlock_bh(&sl->
lock);
1345 for (i = 0; i < slip_maxdev; i++) {
1349 slip_devs[
i] =
NULL;
1351 sl = netdev_priv(dev);
1367 printk(
KERN_ERR "SLIP: can't unregister line discipline (err = %d)\n", i);
1373 #ifdef CONFIG_SLIP_SMART
1379 static void sl_outfill(
unsigned long sls)
1381 struct slip *sl = (
struct slip *)sls;
1383 spin_lock(&sl->
lock);
1391 #ifdef CONFIG_SLIP_MODE_SLIP6
1394 unsigned char s =
END;
1397 if (!netif_queue_stopped(sl->
dev)) {
1399 sl->
tty->ops->write(sl->
tty, &s, 1);
1404 mod_timer(&sl->outfill_timer, jiffies+sl->outfill*HZ);
1407 spin_unlock(&sl->
lock);
1410 static void sl_keepalive(
unsigned long sls)
1412 struct slip *sl = (
struct slip *)sls;
1414 spin_lock(&sl->
lock);
1419 if (sl->keepalive) {
1425 printk(
KERN_DEBUG "%s: no packets received during keepalive timeout, hangup.\n", sl->
dev->name);
1433 mod_timer(&sl->keepalive_timer, jiffies+sl->keepalive*HZ);
1436 spin_unlock(&sl->
lock);