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