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