if_sl.c revision 1.33 1 1.33 mycroft /* $NetBSD: if_sl.c,v 1.33 1994/12/11 21:39:05 mycroft Exp $ */
2 1.29 cgd
3 1.1 cgd /*
4 1.28 mycroft * Copyright (c) 1987, 1989, 1992, 1993
5 1.28 mycroft * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * Redistribution and use in source and binary forms, with or without
8 1.1 cgd * modification, are permitted provided that the following conditions
9 1.1 cgd * are met:
10 1.1 cgd * 1. Redistributions of source code must retain the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer.
12 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 cgd * notice, this list of conditions and the following disclaimer in the
14 1.1 cgd * documentation and/or other materials provided with the distribution.
15 1.1 cgd * 3. All advertising materials mentioning features or use of this software
16 1.1 cgd * must display the following acknowledgement:
17 1.1 cgd * This product includes software developed by the University of
18 1.1 cgd * California, Berkeley and its contributors.
19 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
20 1.1 cgd * may be used to endorse or promote products derived from this software
21 1.1 cgd * without specific prior written permission.
22 1.1 cgd *
23 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 1.1 cgd * SUCH DAMAGE.
34 1.1 cgd *
35 1.29 cgd * @(#)if_sl.c 8.6 (Berkeley) 2/1/94
36 1.1 cgd */
37 1.1 cgd
38 1.1 cgd /*
39 1.1 cgd * Serial Line interface
40 1.1 cgd *
41 1.1 cgd * Rick Adams
42 1.1 cgd * Center for Seismic Studies
43 1.1 cgd * 1300 N 17th Street, Suite 1450
44 1.1 cgd * Arlington, Virginia 22209
45 1.1 cgd * (703)276-7900
46 1.1 cgd * rick (at) seismo.ARPA
47 1.1 cgd * seismo!rick
48 1.1 cgd *
49 1.1 cgd * Pounded on heavily by Chris Torek (chris (at) mimsy.umd.edu, umcp-cs!chris).
50 1.1 cgd * N.B.: this belongs in netinet, not net, the way it stands now.
51 1.1 cgd * Should have a link-layer type designation, but wouldn't be
52 1.1 cgd * backwards-compatible.
53 1.1 cgd *
54 1.1 cgd * Converted to 4.3BSD Beta by Chris Torek.
55 1.1 cgd * Other changes made at Berkeley, based in part on code by Kirk Smith.
56 1.1 cgd * W. Jolitz added slip abort.
57 1.1 cgd *
58 1.1 cgd * Hacked almost beyond recognition by Van Jacobson (van (at) helios.ee.lbl.gov).
59 1.1 cgd * Added priority queuing for "interactive" traffic; hooks for TCP
60 1.1 cgd * header compression; ICMP filtering (at 2400 baud, some cretin
61 1.1 cgd * pinging you can use up all your bandwidth). Made low clist behavior
62 1.1 cgd * more robust and slightly less likely to hang serial line.
63 1.1 cgd * Sped up a bunch of things.
64 1.1 cgd *
65 1.1 cgd * Note that splimp() is used throughout to block both (tty) input
66 1.1 cgd * interrupts and network activity; thus, splimp must be >= spltty.
67 1.1 cgd */
68 1.1 cgd
69 1.1 cgd #include "sl.h"
70 1.1 cgd #if NSL > 0
71 1.1 cgd
72 1.22 cgd #include "bpfilter.h"
73 1.22 cgd
74 1.20 mycroft #include <sys/param.h>
75 1.20 mycroft #include <sys/proc.h>
76 1.20 mycroft #include <sys/mbuf.h>
77 1.20 mycroft #include <sys/buf.h>
78 1.20 mycroft #include <sys/dkstat.h>
79 1.20 mycroft #include <sys/socket.h>
80 1.20 mycroft #include <sys/ioctl.h>
81 1.20 mycroft #include <sys/file.h>
82 1.20 mycroft #include <sys/tty.h>
83 1.20 mycroft #include <sys/kernel.h>
84 1.20 mycroft #include <sys/conf.h>
85 1.20 mycroft
86 1.22 cgd #include <machine/cpu.h>
87 1.22 cgd
88 1.20 mycroft #include <net/if.h>
89 1.20 mycroft #include <net/if_types.h>
90 1.20 mycroft #include <net/netisr.h>
91 1.20 mycroft #include <net/route.h>
92 1.1 cgd
93 1.1 cgd #if INET
94 1.20 mycroft #include <netinet/in.h>
95 1.20 mycroft #include <netinet/in_systm.h>
96 1.20 mycroft #include <netinet/in_var.h>
97 1.20 mycroft #include <netinet/ip.h>
98 1.1 cgd #else
99 1.1 cgd Huh? Slip without inet?
100 1.1 cgd #endif
101 1.1 cgd
102 1.20 mycroft #include <net/slcompress.h>
103 1.20 mycroft #include <net/if_slvar.h>
104 1.26 cgd #include <net/slip.h>
105 1.3 cgd
106 1.3 cgd #if NBPFILTER > 0
107 1.3 cgd #include <sys/time.h>
108 1.3 cgd #include <net/bpf.h>
109 1.3 cgd #endif
110 1.1 cgd
111 1.1 cgd /*
112 1.1 cgd * SLMAX is a hard limit on input packet size. To simplify the code
113 1.1 cgd * and improve performance, we require that packets fit in an mbuf
114 1.1 cgd * cluster, and if we get a compressed packet, there's enough extra
115 1.1 cgd * room to expand the header into a max length tcp/ip header (128
116 1.1 cgd * bytes). So, SLMAX can be at most
117 1.1 cgd * MCLBYTES - 128
118 1.1 cgd *
119 1.1 cgd * SLMTU is a hard limit on output packet size. To insure good
120 1.1 cgd * interactive response, SLMTU wants to be the smallest size that
121 1.1 cgd * amortizes the header cost. (Remember that even with
122 1.1 cgd * type-of-service queuing, we have to wait for any in-progress
123 1.1 cgd * packet to finish. I.e., we wait, on the average, 1/2 * mtu /
124 1.1 cgd * cps, where cps is the line speed in characters per second.
125 1.1 cgd * E.g., 533ms wait for a 1024 byte MTU on a 9600 baud line. The
126 1.1 cgd * average compressed header size is 6-8 bytes so any MTU > 90
127 1.1 cgd * bytes will give us 90% of the line bandwidth. A 100ms wait is
128 1.1 cgd * tolerable (500ms is not), so want an MTU around 296. (Since TCP
129 1.1 cgd * will send 256 byte segments (to allow for 40 byte headers), the
130 1.1 cgd * typical packet size on the wire will be around 260 bytes). In
131 1.1 cgd * 4.3tahoe+ systems, we can set an MTU in a route so we do that &
132 1.1 cgd * leave the interface MTU relatively high (so we don't IP fragment
133 1.1 cgd * when acting as a gateway to someone using a stupid MTU).
134 1.1 cgd *
135 1.1 cgd * Similar considerations apply to SLIP_HIWAT: It's the amount of
136 1.1 cgd * data that will be queued 'downstream' of us (i.e., in clists
137 1.1 cgd * waiting to be picked up by the tty output interrupt). If we
138 1.1 cgd * queue a lot of data downstream, it's immune to our t.o.s. queuing.
139 1.1 cgd * E.g., if SLIP_HIWAT is 1024, the interactive traffic in mixed
140 1.1 cgd * telnet/ftp will see a 1 sec wait, independent of the mtu (the
141 1.1 cgd * wait is dependent on the ftp window size but that's typically
142 1.1 cgd * 1k - 4k). So, we want SLIP_HIWAT just big enough to amortize
143 1.1 cgd * the cost (in idle time on the wire) of the tty driver running
144 1.1 cgd * off the end of its clists & having to call back slstart for a
145 1.1 cgd * new packet. For a tty interface with any buffering at all, this
146 1.1 cgd * cost will be zero. Even with a totally brain dead interface (like
147 1.1 cgd * the one on a typical workstation), the cost will be <= 1 character
148 1.1 cgd * time. So, setting SLIP_HIWAT to ~100 guarantees that we'll lose
149 1.1 cgd * at most 1% while maintaining good interactive response.
150 1.1 cgd */
151 1.3 cgd #if NBPFILTER > 0
152 1.22 cgd #define BUFOFFSET (128+sizeof(struct ifnet **)+SLIP_HDRLEN)
153 1.3 cgd #else
154 1.22 cgd #define BUFOFFSET (128+sizeof(struct ifnet **))
155 1.3 cgd #endif
156 1.1 cgd #define SLMAX (MCLBYTES - BUFOFFSET)
157 1.1 cgd #define SLBUFSIZE (SLMAX + BUFOFFSET)
158 1.27 cgd #ifndef SLMTU
159 1.22 cgd #define SLMTU 296
160 1.27 cgd #endif
161 1.27 cgd #if (SLMTU < 3)
162 1.27 cgd Huh? SLMTU way too small.
163 1.27 cgd #endif
164 1.1 cgd #define SLIP_HIWAT roundup(50,CBSIZE)
165 1.31 cgd #ifndef NetBSD /* XXX - cgd */
166 1.22 cgd #define CLISTRESERVE 1024 /* Can't let clists get too low */
167 1.31 cgd #endif /* !NetBSD */
168 1.1 cgd
169 1.1 cgd /*
170 1.1 cgd * SLIP ABORT ESCAPE MECHANISM:
171 1.1 cgd * (inspired by HAYES modem escape arrangement)
172 1.1 cgd * 1sec escape 1sec escape 1sec escape { 1sec escape 1sec escape }
173 1.22 cgd * within window time signals a "soft" exit from slip mode by remote end
174 1.22 cgd * if the IFF_DEBUG flag is on.
175 1.1 cgd */
176 1.1 cgd #define ABT_ESC '\033' /* can't be t_intr - distant host must know it*/
177 1.22 cgd #define ABT_IDLE 1 /* in seconds - idle before an escape */
178 1.22 cgd #define ABT_COUNT 3 /* count of escapes for abort */
179 1.22 cgd #define ABT_WINDOW (ABT_COUNT*2+2) /* in seconds - time to count */
180 1.1 cgd
181 1.1 cgd struct sl_softc sl_softc[NSL];
182 1.1 cgd
183 1.1 cgd #define FRAME_END 0xc0 /* Frame End */
184 1.1 cgd #define FRAME_ESCAPE 0xdb /* Frame Esc */
185 1.1 cgd #define TRANS_FRAME_END 0xdc /* transposed frame end */
186 1.1 cgd #define TRANS_FRAME_ESCAPE 0xdd /* transposed frame esc */
187 1.1 cgd
188 1.22 cgd static int slinit __P((struct sl_softc *));
189 1.22 cgd static struct mbuf *sl_btom __P((struct sl_softc *, int));
190 1.1 cgd
191 1.1 cgd /*
192 1.1 cgd * Called from boot code to establish sl interfaces.
193 1.1 cgd */
194 1.22 cgd void
195 1.1 cgd slattach()
196 1.1 cgd {
197 1.1 cgd register struct sl_softc *sc;
198 1.1 cgd register int i = 0;
199 1.1 cgd
200 1.1 cgd for (sc = sl_softc; i < NSL; sc++) {
201 1.1 cgd sc->sc_if.if_name = "sl";
202 1.22 cgd sc->sc_if.if_next = NULL;
203 1.1 cgd sc->sc_if.if_unit = i++;
204 1.1 cgd sc->sc_if.if_mtu = SLMTU;
205 1.22 cgd sc->sc_if.if_flags =
206 1.22 cgd IFF_POINTOPOINT | SC_AUTOCOMP | IFF_MULTICAST;
207 1.1 cgd sc->sc_if.if_type = IFT_SLIP;
208 1.1 cgd sc->sc_if.if_ioctl = slioctl;
209 1.1 cgd sc->sc_if.if_output = sloutput;
210 1.1 cgd sc->sc_if.if_snd.ifq_maxlen = 50;
211 1.1 cgd sc->sc_fastq.ifq_maxlen = 32;
212 1.1 cgd if_attach(&sc->sc_if);
213 1.3 cgd #if NBPFILTER > 0
214 1.22 cgd bpfattach(&sc->sc_bpf, &sc->sc_if, DLT_SLIP, SLIP_HDRLEN);
215 1.3 cgd #endif
216 1.1 cgd }
217 1.1 cgd }
218 1.1 cgd
219 1.1 cgd static int
220 1.1 cgd slinit(sc)
221 1.1 cgd register struct sl_softc *sc;
222 1.1 cgd {
223 1.1 cgd register caddr_t p;
224 1.1 cgd
225 1.1 cgd if (sc->sc_ep == (u_char *) 0) {
226 1.1 cgd MCLALLOC(p, M_WAIT);
227 1.1 cgd if (p)
228 1.1 cgd sc->sc_ep = (u_char *)p + SLBUFSIZE;
229 1.1 cgd else {
230 1.1 cgd printf("sl%d: can't allocate buffer\n", sc - sl_softc);
231 1.1 cgd sc->sc_if.if_flags &= ~IFF_UP;
232 1.1 cgd return (0);
233 1.1 cgd }
234 1.1 cgd }
235 1.1 cgd sc->sc_buf = sc->sc_ep - SLMAX;
236 1.1 cgd sc->sc_mp = sc->sc_buf;
237 1.28 mycroft sl_compress_init(&sc->sc_comp, -1);
238 1.1 cgd return (1);
239 1.1 cgd }
240 1.1 cgd
241 1.1 cgd /*
242 1.1 cgd * Line specific open routine.
243 1.1 cgd * Attach the given tty to the first available sl unit.
244 1.1 cgd */
245 1.1 cgd /* ARGSUSED */
246 1.9 andrew int
247 1.22 cgd slopen(dev, tp)
248 1.22 cgd dev_t dev;
249 1.22 cgd register struct tty *tp;
250 1.1 cgd {
251 1.1 cgd struct proc *p = curproc; /* XXX */
252 1.1 cgd register struct sl_softc *sc;
253 1.1 cgd register int nsl;
254 1.1 cgd int error;
255 1.31 cgd #ifdef NetBSD
256 1.27 cgd int s;
257 1.27 cgd #endif
258 1.1 cgd
259 1.1 cgd if (error = suser(p->p_ucred, &p->p_acflag))
260 1.1 cgd return (error);
261 1.1 cgd
262 1.1 cgd if (tp->t_line == SLIPDISC)
263 1.1 cgd return (0);
264 1.1 cgd
265 1.1 cgd for (nsl = NSL, sc = sl_softc; --nsl >= 0; sc++)
266 1.1 cgd if (sc->sc_ttyp == NULL) {
267 1.1 cgd if (slinit(sc) == 0)
268 1.1 cgd return (ENOBUFS);
269 1.1 cgd tp->t_sc = (caddr_t)sc;
270 1.1 cgd sc->sc_ttyp = tp;
271 1.1 cgd sc->sc_if.if_baudrate = tp->t_ospeed;
272 1.1 cgd ttyflush(tp, FREAD | FWRITE);
273 1.31 cgd #ifdef NetBSD
274 1.27 cgd /*
275 1.27 cgd * make sure tty output queue is large enough
276 1.27 cgd * to hold a full-sized packet (including frame
277 1.27 cgd * end, and a possible extra frame end). full-sized
278 1.27 cgd * packet occupies a max of 2*SLMTU bytes (because
279 1.27 cgd * of possible escapes), and add two on for frame
280 1.27 cgd * ends.
281 1.27 cgd */
282 1.27 cgd s = spltty();
283 1.27 cgd if (tp->t_outq.c_cn < 2*SLMTU+2) {
284 1.27 cgd sc->sc_oldbufsize = tp->t_outq.c_cn;
285 1.27 cgd sc->sc_oldbufquot = tp->t_outq.c_cq != 0;
286 1.27 cgd
287 1.27 cgd clfree(&tp->t_outq);
288 1.27 cgd error = clalloc(&tp->t_outq, 3*SLMTU, 0);
289 1.27 cgd if (error) {
290 1.27 cgd splx(s);
291 1.27 cgd return(error);
292 1.27 cgd }
293 1.27 cgd } else
294 1.27 cgd sc->sc_oldbufsize = sc->sc_oldbufquot = 0;
295 1.27 cgd splx(s);
296 1.31 cgd #endif /* NetBSD */
297 1.1 cgd return (0);
298 1.1 cgd }
299 1.1 cgd return (ENXIO);
300 1.1 cgd }
301 1.1 cgd
302 1.1 cgd /*
303 1.1 cgd * Line specific close routine.
304 1.1 cgd * Detach the tty from the sl unit.
305 1.1 cgd */
306 1.9 andrew void
307 1.22 cgd slclose(tp)
308 1.1 cgd struct tty *tp;
309 1.1 cgd {
310 1.1 cgd register struct sl_softc *sc;
311 1.1 cgd int s;
312 1.1 cgd
313 1.1 cgd ttywflush(tp);
314 1.1 cgd s = splimp(); /* actually, max(spltty, splnet) */
315 1.1 cgd tp->t_line = 0;
316 1.1 cgd sc = (struct sl_softc *)tp->t_sc;
317 1.1 cgd if (sc != NULL) {
318 1.1 cgd if_down(&sc->sc_if);
319 1.1 cgd sc->sc_ttyp = NULL;
320 1.1 cgd tp->t_sc = NULL;
321 1.1 cgd MCLFREE((caddr_t)(sc->sc_ep - SLBUFSIZE));
322 1.1 cgd sc->sc_ep = 0;
323 1.1 cgd sc->sc_mp = 0;
324 1.1 cgd sc->sc_buf = 0;
325 1.1 cgd }
326 1.31 cgd #ifdef NetBSD
327 1.27 cgd /* if necessary, install a new outq buffer of the appropriate size */
328 1.27 cgd if (sc->sc_oldbufsize != 0) {
329 1.27 cgd clfree(&tp->t_outq);
330 1.27 cgd clalloc(&tp->t_outq, sc->sc_oldbufsize, sc->sc_oldbufquot);
331 1.27 cgd }
332 1.27 cgd #endif
333 1.1 cgd splx(s);
334 1.1 cgd }
335 1.1 cgd
336 1.1 cgd /*
337 1.1 cgd * Line specific (tty) ioctl routine.
338 1.1 cgd * Provide a way to get the sl unit number.
339 1.1 cgd */
340 1.1 cgd /* ARGSUSED */
341 1.22 cgd int
342 1.1 cgd sltioctl(tp, cmd, data, flag)
343 1.1 cgd struct tty *tp;
344 1.32 cgd u_long cmd;
345 1.1 cgd caddr_t data;
346 1.22 cgd int flag;
347 1.1 cgd {
348 1.1 cgd struct sl_softc *sc = (struct sl_softc *)tp->t_sc;
349 1.1 cgd
350 1.1 cgd switch (cmd) {
351 1.1 cgd case SLIOCGUNIT:
352 1.1 cgd *(int *)data = sc->sc_if.if_unit;
353 1.1 cgd break;
354 1.1 cgd
355 1.1 cgd default:
356 1.1 cgd return (-1);
357 1.1 cgd }
358 1.1 cgd return (0);
359 1.1 cgd }
360 1.1 cgd
361 1.1 cgd /*
362 1.1 cgd * Queue a packet. Start transmission if not active.
363 1.22 cgd * Compression happens in slstart; if we do it here, IP TOS
364 1.22 cgd * will cause us to not compress "background" packets, because
365 1.28 mycroft * ordering gets trashed. It can be done for all packets in slstart.
366 1.1 cgd */
367 1.22 cgd int
368 1.22 cgd sloutput(ifp, m, dst, rtp)
369 1.1 cgd struct ifnet *ifp;
370 1.1 cgd register struct mbuf *m;
371 1.1 cgd struct sockaddr *dst;
372 1.22 cgd struct rtentry *rtp;
373 1.1 cgd {
374 1.1 cgd register struct sl_softc *sc = &sl_softc[ifp->if_unit];
375 1.1 cgd register struct ip *ip;
376 1.1 cgd register struct ifqueue *ifq;
377 1.1 cgd int s;
378 1.1 cgd
379 1.1 cgd /*
380 1.1 cgd * `Cannot happen' (see slioctl). Someday we will extend
381 1.1 cgd * the line protocol to support other address families.
382 1.1 cgd */
383 1.1 cgd if (dst->sa_family != AF_INET) {
384 1.1 cgd printf("sl%d: af%d not supported\n", sc->sc_if.if_unit,
385 1.1 cgd dst->sa_family);
386 1.1 cgd m_freem(m);
387 1.22 cgd sc->sc_if.if_noproto++;
388 1.1 cgd return (EAFNOSUPPORT);
389 1.1 cgd }
390 1.1 cgd
391 1.1 cgd if (sc->sc_ttyp == NULL) {
392 1.1 cgd m_freem(m);
393 1.1 cgd return (ENETDOWN); /* sort of */
394 1.1 cgd }
395 1.14 mycroft if ((sc->sc_ttyp->t_state & TS_CARR_ON) == 0 &&
396 1.14 mycroft (sc->sc_ttyp->t_cflag & CLOCAL) == 0) {
397 1.1 cgd m_freem(m);
398 1.1 cgd return (EHOSTUNREACH);
399 1.1 cgd }
400 1.1 cgd ifq = &sc->sc_if.if_snd;
401 1.22 cgd ip = mtod(m, struct ip *);
402 1.22 cgd if (sc->sc_if.if_flags & SC_NOICMP && ip->ip_p == IPPROTO_ICMP) {
403 1.1 cgd m_freem(m);
404 1.22 cgd return (ENETRESET); /* XXX ? */
405 1.1 cgd }
406 1.22 cgd if (ip->ip_tos & IPTOS_LOWDELAY)
407 1.22 cgd ifq = &sc->sc_fastq;
408 1.1 cgd s = splimp();
409 1.27 cgd if (sc->sc_oqlen && sc->sc_ttyp->t_outq.c_cc == sc->sc_oqlen) {
410 1.27 cgd struct timeval tv = time;
411 1.27 cgd
412 1.27 cgd /* if output's been stalled for too long, and restart */
413 1.33 mycroft __timersub(&tv, &sc->sc_if.if_lastchange);
414 1.27 cgd if (tv.tv_sec > 0) {
415 1.27 cgd sc->sc_otimeout++;
416 1.27 cgd slstart(sc->sc_ttyp);
417 1.27 cgd }
418 1.27 cgd }
419 1.1 cgd if (IF_QFULL(ifq)) {
420 1.1 cgd IF_DROP(ifq);
421 1.1 cgd m_freem(m);
422 1.1 cgd splx(s);
423 1.1 cgd sc->sc_if.if_oerrors++;
424 1.1 cgd return (ENOBUFS);
425 1.1 cgd }
426 1.1 cgd IF_ENQUEUE(ifq, m);
427 1.1 cgd sc->sc_if.if_lastchange = time;
428 1.27 cgd if ((sc->sc_oqlen = sc->sc_ttyp->t_outq.c_cc) == 0)
429 1.1 cgd slstart(sc->sc_ttyp);
430 1.1 cgd splx(s);
431 1.1 cgd return (0);
432 1.1 cgd }
433 1.1 cgd
434 1.1 cgd /*
435 1.1 cgd * Start output on interface. Get another datagram
436 1.1 cgd * to send from the interface queue and map it to
437 1.1 cgd * the interface before starting output.
438 1.1 cgd */
439 1.9 andrew void
440 1.1 cgd slstart(tp)
441 1.1 cgd register struct tty *tp;
442 1.1 cgd {
443 1.1 cgd register struct sl_softc *sc = (struct sl_softc *)tp->t_sc;
444 1.1 cgd register struct mbuf *m;
445 1.1 cgd register u_char *cp;
446 1.3 cgd register struct ip *ip;
447 1.1 cgd int s;
448 1.1 cgd struct mbuf *m2;
449 1.3 cgd #if NBPFILTER > 0
450 1.3 cgd u_char bpfbuf[SLMTU + SLIP_HDRLEN];
451 1.3 cgd register int len;
452 1.3 cgd #endif
453 1.31 cgd #ifndef NetBSD /* XXX - cgd */
454 1.22 cgd extern int cfreecount;
455 1.22 cgd #endif
456 1.1 cgd
457 1.1 cgd for (;;) {
458 1.1 cgd /*
459 1.1 cgd * If there is more in the output queue, just send it now.
460 1.1 cgd * We are being called in lieu of ttstart and must do what
461 1.1 cgd * it would.
462 1.1 cgd */
463 1.10 mycroft if (tp->t_outq.c_cc != 0) {
464 1.1 cgd (*tp->t_oproc)(tp);
465 1.10 mycroft if (tp->t_outq.c_cc > SLIP_HIWAT)
466 1.1 cgd return;
467 1.1 cgd }
468 1.1 cgd /*
469 1.1 cgd * This happens briefly when the line shuts down.
470 1.1 cgd */
471 1.1 cgd if (sc == NULL)
472 1.1 cgd return;
473 1.1 cgd
474 1.31 cgd #ifdef NetBSD /* XXX - cgd */
475 1.1 cgd /*
476 1.3 cgd * Do not remove the packet from the IP queue if it
477 1.3 cgd * doesn't look like the packet will fit into the
478 1.22 cgd * current serial output queue, with a packet full of
479 1.27 cgd * escapes this could be as bad as SLMTU*2+2.
480 1.3 cgd */
481 1.27 cgd if (tp->t_outq.c_cn - tp->t_outq.c_cc < 2*SLMTU+2)
482 1.3 cgd return;
483 1.31 cgd #endif /* NetBSD */
484 1.3 cgd
485 1.3 cgd /*
486 1.1 cgd * Get a packet and send it to the interface.
487 1.1 cgd */
488 1.1 cgd s = splimp();
489 1.1 cgd IF_DEQUEUE(&sc->sc_fastq, m);
490 1.22 cgd if (m)
491 1.22 cgd sc->sc_if.if_omcasts++; /* XXX */
492 1.22 cgd else
493 1.1 cgd IF_DEQUEUE(&sc->sc_if.if_snd, m);
494 1.1 cgd splx(s);
495 1.1 cgd if (m == NULL)
496 1.1 cgd return;
497 1.22 cgd
498 1.1 cgd /*
499 1.22 cgd * We do the header compression here rather than in sloutput
500 1.3 cgd * because the packets will be out of order if we are using TOS
501 1.22 cgd * queueing, and the connection id compression will get
502 1.22 cgd * munged when this happens.
503 1.1 cgd */
504 1.3 cgd #if NBPFILTER > 0
505 1.3 cgd if (sc->sc_bpf) {
506 1.22 cgd /*
507 1.22 cgd * We need to save the TCP/IP header before it's
508 1.22 cgd * compressed. To avoid complicated code, we just
509 1.22 cgd * copy the entire packet into a stack buffer (since
510 1.22 cgd * this is a serial line, packets should be short
511 1.22 cgd * and/or the copy should be negligible cost compared
512 1.22 cgd * to the packet transmission time).
513 1.22 cgd */
514 1.3 cgd register struct mbuf *m1 = m;
515 1.3 cgd register u_char *cp = bpfbuf + SLIP_HDRLEN;
516 1.22 cgd
517 1.3 cgd len = 0;
518 1.3 cgd do {
519 1.3 cgd register int mlen = m1->m_len;
520 1.3 cgd
521 1.3 cgd bcopy(mtod(m1, caddr_t), cp, mlen);
522 1.3 cgd cp += mlen;
523 1.3 cgd len += mlen;
524 1.3 cgd } while (m1 = m1->m_next);
525 1.3 cgd }
526 1.3 cgd #endif
527 1.22 cgd if ((ip = mtod(m, struct ip *))->ip_p == IPPROTO_TCP) {
528 1.19 cgd if (sc->sc_if.if_flags & SC_COMPRESS)
529 1.22 cgd *mtod(m, u_char *) |= sl_compress_tcp(m, ip,
530 1.22 cgd &sc->sc_comp, 1);
531 1.3 cgd }
532 1.3 cgd #if NBPFILTER > 0
533 1.3 cgd if (sc->sc_bpf) {
534 1.22 cgd /*
535 1.22 cgd * Put the SLIP pseudo-"link header" in place. The
536 1.22 cgd * compressed header is now at the beginning of the
537 1.22 cgd * mbuf.
538 1.22 cgd */
539 1.3 cgd bpfbuf[SLX_DIR] = SLIPDIR_OUT;
540 1.3 cgd bcopy(mtod(m, caddr_t), &bpfbuf[SLX_CHDR], CHDR_LEN);
541 1.3 cgd bpf_tap(sc->sc_bpf, bpfbuf, len + SLIP_HDRLEN);
542 1.1 cgd }
543 1.3 cgd #endif
544 1.3 cgd sc->sc_if.if_lastchange = time;
545 1.1 cgd
546 1.31 cgd #ifndef NetBSD /* XXX - cgd */
547 1.22 cgd /*
548 1.22 cgd * If system is getting low on clists, just flush our
549 1.22 cgd * output queue (if the stuff was important, it'll get
550 1.22 cgd * retransmitted).
551 1.22 cgd */
552 1.22 cgd if (cfreecount < CLISTRESERVE + SLMTU) {
553 1.22 cgd m_freem(m);
554 1.22 cgd sc->sc_if.if_collisions++;
555 1.22 cgd continue;
556 1.22 cgd }
557 1.31 cgd #endif /* !NetBSD */
558 1.1 cgd /*
559 1.1 cgd * The extra FRAME_END will start up a new packet, and thus
560 1.1 cgd * will flush any accumulated garbage. We do this whenever
561 1.1 cgd * the line may have been idle for some time.
562 1.1 cgd */
563 1.10 mycroft if (tp->t_outq.c_cc == 0) {
564 1.22 cgd ++sc->sc_if.if_obytes;
565 1.10 mycroft (void) putc(FRAME_END, &tp->t_outq);
566 1.1 cgd }
567 1.1 cgd
568 1.1 cgd while (m) {
569 1.1 cgd register u_char *ep;
570 1.1 cgd
571 1.1 cgd cp = mtod(m, u_char *); ep = cp + m->m_len;
572 1.1 cgd while (cp < ep) {
573 1.1 cgd /*
574 1.1 cgd * Find out how many bytes in the string we can
575 1.1 cgd * handle without doing something special.
576 1.1 cgd */
577 1.1 cgd register u_char *bp = cp;
578 1.1 cgd
579 1.1 cgd while (cp < ep) {
580 1.1 cgd switch (*cp++) {
581 1.1 cgd case FRAME_ESCAPE:
582 1.1 cgd case FRAME_END:
583 1.1 cgd --cp;
584 1.1 cgd goto out;
585 1.1 cgd }
586 1.1 cgd }
587 1.1 cgd out:
588 1.1 cgd if (cp > bp) {
589 1.1 cgd /*
590 1.10 mycroft * Put n characters at once
591 1.1 cgd * into the tty output queue.
592 1.1 cgd */
593 1.31 cgd #ifdef NetBSD /* XXX - cgd */
594 1.22 cgd if (b_to_q((u_char *)bp, cp - bp,
595 1.22 cgd #else
596 1.22 cgd if (b_to_q((char *)bp, cp - bp,
597 1.22 cgd #endif
598 1.22 cgd &tp->t_outq))
599 1.10 mycroft break;
600 1.22 cgd sc->sc_if.if_obytes += cp - bp;
601 1.1 cgd }
602 1.1 cgd /*
603 1.1 cgd * If there are characters left in the mbuf,
604 1.1 cgd * the first one must be special..
605 1.1 cgd * Put it out in a different form.
606 1.1 cgd */
607 1.1 cgd if (cp < ep) {
608 1.10 mycroft if (putc(FRAME_ESCAPE, &tp->t_outq))
609 1.1 cgd break;
610 1.1 cgd if (putc(*cp++ == FRAME_ESCAPE ?
611 1.1 cgd TRANS_FRAME_ESCAPE : TRANS_FRAME_END,
612 1.10 mycroft &tp->t_outq)) {
613 1.10 mycroft (void) unputc(&tp->t_outq);
614 1.1 cgd break;
615 1.1 cgd }
616 1.22 cgd sc->sc_if.if_obytes += 2;
617 1.1 cgd }
618 1.1 cgd }
619 1.1 cgd MFREE(m, m2);
620 1.1 cgd m = m2;
621 1.1 cgd }
622 1.1 cgd
623 1.10 mycroft if (putc(FRAME_END, &tp->t_outq)) {
624 1.1 cgd /*
625 1.1 cgd * Not enough room. Remove a char to make room
626 1.1 cgd * and end the packet normally.
627 1.1 cgd * If you get many collisions (more than one or two
628 1.1 cgd * a day) you probably do not have enough clists
629 1.1 cgd * and you should increase "nclist" in param.c.
630 1.1 cgd */
631 1.10 mycroft (void) unputc(&tp->t_outq);
632 1.10 mycroft (void) putc(FRAME_END, &tp->t_outq);
633 1.1 cgd sc->sc_if.if_collisions++;
634 1.1 cgd } else {
635 1.22 cgd ++sc->sc_if.if_obytes;
636 1.1 cgd sc->sc_if.if_opackets++;
637 1.1 cgd }
638 1.1 cgd }
639 1.1 cgd }
640 1.1 cgd
641 1.1 cgd /*
642 1.1 cgd * Copy data buffer to mbuf chain; add ifnet pointer.
643 1.1 cgd */
644 1.1 cgd static struct mbuf *
645 1.1 cgd sl_btom(sc, len)
646 1.1 cgd register struct sl_softc *sc;
647 1.1 cgd register int len;
648 1.1 cgd {
649 1.1 cgd register struct mbuf *m;
650 1.1 cgd
651 1.1 cgd MGETHDR(m, M_DONTWAIT, MT_DATA);
652 1.1 cgd if (m == NULL)
653 1.1 cgd return (NULL);
654 1.1 cgd
655 1.1 cgd /*
656 1.1 cgd * If we have more than MHLEN bytes, it's cheaper to
657 1.1 cgd * queue the cluster we just filled & allocate a new one
658 1.1 cgd * for the input buffer. Otherwise, fill the mbuf we
659 1.1 cgd * allocated above. Note that code in the input routine
660 1.1 cgd * guarantees that packet will fit in a cluster.
661 1.1 cgd */
662 1.1 cgd if (len >= MHLEN) {
663 1.1 cgd MCLGET(m, M_DONTWAIT);
664 1.1 cgd if ((m->m_flags & M_EXT) == 0) {
665 1.1 cgd /*
666 1.1 cgd * we couldn't get a cluster - if memory's this
667 1.1 cgd * low, it's time to start dropping packets.
668 1.1 cgd */
669 1.1 cgd (void) m_free(m);
670 1.1 cgd return (NULL);
671 1.1 cgd }
672 1.1 cgd sc->sc_ep = mtod(m, u_char *) + SLBUFSIZE;
673 1.1 cgd m->m_data = (caddr_t)sc->sc_buf;
674 1.1 cgd m->m_ext.ext_buf = (caddr_t)((int)sc->sc_buf &~ MCLOFSET);
675 1.1 cgd } else
676 1.1 cgd bcopy((caddr_t)sc->sc_buf, mtod(m, caddr_t), len);
677 1.1 cgd
678 1.1 cgd m->m_len = len;
679 1.1 cgd m->m_pkthdr.len = len;
680 1.1 cgd m->m_pkthdr.rcvif = &sc->sc_if;
681 1.1 cgd return (m);
682 1.1 cgd }
683 1.1 cgd
684 1.1 cgd /*
685 1.1 cgd * tty interface receiver interrupt.
686 1.1 cgd */
687 1.9 andrew void
688 1.1 cgd slinput(c, tp)
689 1.1 cgd register int c;
690 1.1 cgd register struct tty *tp;
691 1.1 cgd {
692 1.1 cgd register struct sl_softc *sc;
693 1.1 cgd register struct mbuf *m;
694 1.1 cgd register int len;
695 1.1 cgd int s;
696 1.3 cgd #if NBPFILTER > 0
697 1.3 cgd u_char chdr[CHDR_LEN];
698 1.3 cgd #endif
699 1.22 cgd
700 1.1 cgd tk_nin++;
701 1.1 cgd sc = (struct sl_softc *)tp->t_sc;
702 1.1 cgd if (sc == NULL)
703 1.1 cgd return;
704 1.13 mycroft if (c & TTY_ERRORMASK || ((tp->t_state & TS_CARR_ON) == 0 &&
705 1.22 cgd (tp->t_cflag & CLOCAL) == 0)) {
706 1.2 cgd sc->sc_flags |= SC_ERROR;
707 1.1 cgd return;
708 1.2 cgd }
709 1.22 cgd c &= TTY_CHARMASK;
710 1.1 cgd
711 1.1 cgd ++sc->sc_if.if_ibytes;
712 1.1 cgd
713 1.19 cgd if (sc->sc_if.if_flags & IFF_DEBUG) {
714 1.22 cgd if (c == ABT_ESC) {
715 1.22 cgd /*
716 1.22 cgd * If we have a previous abort, see whether
717 1.22 cgd * this one is within the time limit.
718 1.22 cgd */
719 1.22 cgd if (sc->sc_abortcount &&
720 1.22 cgd time.tv_sec >= sc->sc_starttime + ABT_WINDOW)
721 1.1 cgd sc->sc_abortcount = 0;
722 1.22 cgd /*
723 1.22 cgd * If we see an abort after "idle" time, count it;
724 1.22 cgd * record when the first abort escape arrived.
725 1.22 cgd */
726 1.22 cgd if (time.tv_sec >= sc->sc_lasttime + ABT_IDLE) {
727 1.22 cgd if (++sc->sc_abortcount == 1)
728 1.22 cgd sc->sc_starttime = time.tv_sec;
729 1.22 cgd if (sc->sc_abortcount >= ABT_COUNT) {
730 1.22 cgd slclose(tp);
731 1.22 cgd return;
732 1.22 cgd }
733 1.1 cgd }
734 1.22 cgd } else
735 1.22 cgd sc->sc_abortcount = 0;
736 1.1 cgd sc->sc_lasttime = time.tv_sec;
737 1.1 cgd }
738 1.1 cgd
739 1.1 cgd switch (c) {
740 1.1 cgd
741 1.1 cgd case TRANS_FRAME_ESCAPE:
742 1.1 cgd if (sc->sc_escape)
743 1.1 cgd c = FRAME_ESCAPE;
744 1.1 cgd break;
745 1.1 cgd
746 1.1 cgd case TRANS_FRAME_END:
747 1.1 cgd if (sc->sc_escape)
748 1.1 cgd c = FRAME_END;
749 1.1 cgd break;
750 1.1 cgd
751 1.1 cgd case FRAME_ESCAPE:
752 1.1 cgd sc->sc_escape = 1;
753 1.1 cgd return;
754 1.1 cgd
755 1.1 cgd case FRAME_END:
756 1.3 cgd if(sc->sc_flags & SC_ERROR) {
757 1.2 cgd sc->sc_flags &= ~SC_ERROR;
758 1.2 cgd goto newpack;
759 1.2 cgd }
760 1.1 cgd len = sc->sc_mp - sc->sc_buf;
761 1.1 cgd if (len < 3)
762 1.1 cgd /* less than min length packet - ignore */
763 1.1 cgd goto newpack;
764 1.1 cgd
765 1.3 cgd #if NBPFILTER > 0
766 1.22 cgd if (sc->sc_bpf) {
767 1.3 cgd /*
768 1.23 cgd * Save the compressed header, so we
769 1.28 mycroft * can tack it on later. Note that we
770 1.23 cgd * will end up copying garbage in some
771 1.3 cgd * cases but this is okay. We remember
772 1.3 cgd * where the buffer started so we can
773 1.3 cgd * compute the new header length.
774 1.3 cgd */
775 1.3 cgd bcopy(sc->sc_buf, chdr, CHDR_LEN);
776 1.22 cgd }
777 1.3 cgd #endif
778 1.22 cgd
779 1.1 cgd if ((c = (*sc->sc_buf & 0xf0)) != (IPVERSION << 4)) {
780 1.1 cgd if (c & 0x80)
781 1.1 cgd c = TYPE_COMPRESSED_TCP;
782 1.1 cgd else if (c == TYPE_UNCOMPRESSED_TCP)
783 1.1 cgd *sc->sc_buf &= 0x4f; /* XXX */
784 1.1 cgd /*
785 1.1 cgd * We've got something that's not an IP packet.
786 1.1 cgd * If compression is enabled, try to decompress it.
787 1.1 cgd * Otherwise, if `auto-enable' compression is on and
788 1.1 cgd * it's a reasonable packet, decompress it and then
789 1.1 cgd * enable compression. Otherwise, drop it.
790 1.1 cgd */
791 1.19 cgd if (sc->sc_if.if_flags & SC_COMPRESS) {
792 1.1 cgd len = sl_uncompress_tcp(&sc->sc_buf, len,
793 1.1 cgd (u_int)c, &sc->sc_comp);
794 1.1 cgd if (len <= 0)
795 1.1 cgd goto error;
796 1.19 cgd } else if ((sc->sc_if.if_flags & SC_AUTOCOMP) &&
797 1.1 cgd c == TYPE_UNCOMPRESSED_TCP && len >= 40) {
798 1.1 cgd len = sl_uncompress_tcp(&sc->sc_buf, len,
799 1.1 cgd (u_int)c, &sc->sc_comp);
800 1.1 cgd if (len <= 0)
801 1.1 cgd goto error;
802 1.19 cgd sc->sc_if.if_flags |= SC_COMPRESS;
803 1.1 cgd } else
804 1.1 cgd goto error;
805 1.1 cgd }
806 1.3 cgd #if NBPFILTER > 0
807 1.3 cgd if (sc->sc_bpf) {
808 1.3 cgd /*
809 1.3 cgd * Put the SLIP pseudo-"link header" in place.
810 1.3 cgd * We couldn't do this any earlier since
811 1.3 cgd * decompression probably moved the buffer
812 1.3 cgd * pointer. Then, invoke BPF.
813 1.3 cgd */
814 1.3 cgd register u_char *hp = sc->sc_buf - SLIP_HDRLEN;
815 1.3 cgd
816 1.3 cgd hp[SLX_DIR] = SLIPDIR_IN;
817 1.3 cgd bcopy(chdr, &hp[SLX_CHDR], CHDR_LEN);
818 1.3 cgd bpf_tap(sc->sc_bpf, hp, len + SLIP_HDRLEN);
819 1.3 cgd }
820 1.3 cgd #endif
821 1.1 cgd m = sl_btom(sc, len);
822 1.1 cgd if (m == NULL)
823 1.1 cgd goto error;
824 1.1 cgd
825 1.1 cgd sc->sc_if.if_ipackets++;
826 1.1 cgd sc->sc_if.if_lastchange = time;
827 1.1 cgd s = splimp();
828 1.1 cgd if (IF_QFULL(&ipintrq)) {
829 1.1 cgd IF_DROP(&ipintrq);
830 1.1 cgd sc->sc_if.if_ierrors++;
831 1.1 cgd sc->sc_if.if_iqdrops++;
832 1.1 cgd m_freem(m);
833 1.1 cgd } else {
834 1.1 cgd IF_ENQUEUE(&ipintrq, m);
835 1.1 cgd schednetisr(NETISR_IP);
836 1.1 cgd }
837 1.1 cgd splx(s);
838 1.1 cgd goto newpack;
839 1.1 cgd }
840 1.1 cgd if (sc->sc_mp < sc->sc_ep) {
841 1.1 cgd *sc->sc_mp++ = c;
842 1.1 cgd sc->sc_escape = 0;
843 1.1 cgd return;
844 1.1 cgd }
845 1.22 cgd
846 1.22 cgd /* can't put lower; would miss an extra frame */
847 1.3 cgd sc->sc_flags |= SC_ERROR;
848 1.22 cgd
849 1.1 cgd error:
850 1.1 cgd sc->sc_if.if_ierrors++;
851 1.1 cgd newpack:
852 1.1 cgd sc->sc_mp = sc->sc_buf = sc->sc_ep - SLMAX;
853 1.1 cgd sc->sc_escape = 0;
854 1.1 cgd }
855 1.1 cgd
856 1.1 cgd /*
857 1.1 cgd * Process an ioctl request.
858 1.1 cgd */
859 1.22 cgd int
860 1.1 cgd slioctl(ifp, cmd, data)
861 1.1 cgd register struct ifnet *ifp;
862 1.32 cgd u_long cmd;
863 1.1 cgd caddr_t data;
864 1.1 cgd {
865 1.1 cgd register struct ifaddr *ifa = (struct ifaddr *)data;
866 1.18 hpeyerl register struct ifreq *ifr;
867 1.22 cgd register int s = splimp(), error = 0;
868 1.1 cgd
869 1.1 cgd switch (cmd) {
870 1.1 cgd
871 1.1 cgd case SIOCSIFADDR:
872 1.1 cgd if (ifa->ifa_addr->sa_family == AF_INET)
873 1.1 cgd ifp->if_flags |= IFF_UP;
874 1.1 cgd else
875 1.1 cgd error = EAFNOSUPPORT;
876 1.1 cgd break;
877 1.1 cgd
878 1.1 cgd case SIOCSIFDSTADDR:
879 1.1 cgd if (ifa->ifa_addr->sa_family != AF_INET)
880 1.1 cgd error = EAFNOSUPPORT;
881 1.1 cgd break;
882 1.22 cgd
883 1.18 hpeyerl case SIOCADDMULTI:
884 1.18 hpeyerl case SIOCDELMULTI:
885 1.18 hpeyerl ifr = (struct ifreq *)data;
886 1.18 hpeyerl if (ifr == 0) {
887 1.18 hpeyerl error = EAFNOSUPPORT; /* XXX */
888 1.18 hpeyerl break;
889 1.18 hpeyerl }
890 1.18 hpeyerl switch (ifr->ifr_addr.sa_family) {
891 1.18 hpeyerl
892 1.18 hpeyerl #ifdef INET
893 1.18 hpeyerl case AF_INET:
894 1.18 hpeyerl break;
895 1.18 hpeyerl #endif
896 1.22 cgd
897 1.18 hpeyerl default:
898 1.18 hpeyerl error = EAFNOSUPPORT;
899 1.18 hpeyerl break;
900 1.18 hpeyerl }
901 1.18 hpeyerl break;
902 1.22 cgd
903 1.1 cgd default:
904 1.1 cgd error = EINVAL;
905 1.1 cgd }
906 1.1 cgd splx(s);
907 1.1 cgd return (error);
908 1.1 cgd }
909 1.1 cgd #endif
910