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