Commit | Line | Data |
---|---|---|
5b519e94 | 1 | /* |
270f7144 | 2 | * Copyright (c) 1987, 1989 Regents of the University of California. |
5b519e94 KB |
3 | * All rights reserved. |
4 | * | |
dbf0c423 | 5 | * %sccs.include.redist.c% |
5b519e94 | 6 | * |
0e97cde8 | 7 | * @(#)if_sl.c 7.23 (Berkeley) %G% |
5b519e94 | 8 | */ |
7c2732d1 | 9 | |
41b6e003 MK |
10 | /* |
11 | * Serial Line interface | |
12 | * | |
13 | * Rick Adams | |
14 | * Center for Seismic Studies | |
15 | * 1300 N 17th Street, Suite 1450 | |
16 | * Arlington, Virginia 22209 | |
17 | * (703)276-7900 | |
18 | * rick@seismo.ARPA | |
19 | * seismo!rick | |
20 | * | |
41b6e003 | 21 | * Pounded on heavily by Chris Torek (chris@mimsy.umd.edu, umcp-cs!chris). |
8011f5df | 22 | * N.B.: this belongs in netinet, not net, the way it stands now. |
7c2732d1 MK |
23 | * Should have a link-layer type designation, but wouldn't be |
24 | * backwards-compatible. | |
41b6e003 MK |
25 | * |
26 | * Converted to 4.3BSD Beta by Chris Torek. | |
8011f5df | 27 | * Other changes made at Berkeley, based in part on code by Kirk Smith. |
270f7144 MK |
28 | * W. Jolitz added slip abort. |
29 | * | |
30 | * Hacked almost beyond recognition by Van Jacobson (van@helios.ee.lbl.gov). | |
31 | * Added priority queuing for "interactive" traffic; hooks for TCP | |
32 | * header compression; ICMP filtering (at 2400 baud, some cretin | |
33 | * pinging you can use up all your bandwidth). Made low clist behavior | |
34 | * more robust and slightly less likely to hang serial line. | |
35 | * Sped up a bunch of things. | |
36 | * | |
37 | * Note that splimp() is used throughout to block both (tty) input | |
38 | * interrupts and network activity; thus, splimp must be >= spltty. | |
41b6e003 MK |
39 | */ |
40 | ||
0e97cde8 | 41 | /* $Header: /usr/src/sys/net/RCS/if_sl.c,v 1.1 91/08/30 11:14:57 william Exp Locker: william $ */ |
41b6e003 MK |
42 | /* from if_sl.c,v 1.11 84/10/04 12:54:47 rick Exp */ |
43 | ||
44 | #include "sl.h" | |
45 | #if NSL > 0 | |
46 | ||
47 | #include "param.h" | |
06242857 | 48 | #include "proc.h" |
41b6e003 MK |
49 | #include "mbuf.h" |
50 | #include "buf.h" | |
8672a1f0 | 51 | #include "dk.h" |
41b6e003 MK |
52 | #include "socket.h" |
53 | #include "ioctl.h" | |
8011f5df | 54 | #include "file.h" |
41b6e003 | 55 | #include "tty.h" |
4a23a8aa WN |
56 | #include "kernel.h" |
57 | #include "conf.h" | |
41b6e003 | 58 | |
8011f5df | 59 | #include "if.h" |
4dcb4b06 | 60 | #include "if_types.h" |
8011f5df MK |
61 | #include "netisr.h" |
62 | #include "route.h" | |
63 | #if INET | |
06242857 MK |
64 | #include "netinet/in.h" |
65 | #include "netinet/in_systm.h" | |
66 | #include "netinet/in_var.h" | |
67 | #include "netinet/ip.h" | |
68 | #else | |
69 | Huh? Slip without inet? | |
8011f5df | 70 | #endif |
41b6e003 | 71 | |
0e6c0944 | 72 | #include "machine/mtpr.h" |
41b6e003 | 73 | |
270f7144 MK |
74 | #include "slcompress.h" |
75 | #include "if_slvar.h" | |
76 | ||
41b6e003 | 77 | /* |
af951bc0 | 78 | * SLMAX is a hard limit on input packet size. To simplify the code |
270f7144 | 79 | * and improve performance, we require that packets fit in an mbuf |
af951bc0 SL |
80 | * cluster, and if we get a compressed packet, there's enough extra |
81 | * room to expand the header into a max length tcp/ip header (128 | |
82 | * bytes). So, SLMAX can be at most | |
83 | * MCLBYTES - 128 | |
270f7144 | 84 | * |
af951bc0 SL |
85 | * SLMTU is a hard limit on output packet size. To insure good |
86 | * interactive response, SLMTU wants to be the smallest size that | |
87 | * amortizes the header cost. (Remember that even with | |
88 | * type-of-service queuing, we have to wait for any in-progress | |
89 | * packet to finish. I.e., we wait, on the average, 1/2 * mtu / | |
90 | * cps, where cps is the line speed in characters per second. | |
91 | * E.g., 533ms wait for a 1024 byte MTU on a 9600 baud line. The | |
92 | * average compressed header size is 6-8 bytes so any MTU > 90 | |
93 | * bytes will give us 90% of the line bandwidth. A 100ms wait is | |
94 | * tolerable (500ms is not), so want an MTU around 296. (Since TCP | |
95 | * will send 256 byte segments (to allow for 40 byte headers), the | |
96 | * typical packet size on the wire will be around 260 bytes). In | |
97 | * 4.3tahoe+ systems, we can set an MTU in a route so we do that & | |
98 | * leave the interface MTU relatively high (so we don't IP fragment | |
99 | * when acting as a gateway to someone using a stupid MTU). | |
100 | * | |
101 | * Similar considerations apply to SLIP_HIWAT: It's the amount of | |
102 | * data that will be queued 'downstream' of us (i.e., in clists | |
103 | * waiting to be picked up by the tty output interrupt). If we | |
104 | * queue a lot of data downstream, it's immune to our t.o.s. queuing. | |
105 | * E.g., if SLIP_HIWAT is 1024, the interactive traffic in mixed | |
106 | * telnet/ftp will see a 1 sec wait, independent of the mtu (the | |
107 | * wait is dependent on the ftp window size but that's typically | |
108 | * 1k - 4k). So, we want SLIP_HIWAT just big enough to amortize | |
109 | * the cost (in idle time on the wire) of the tty driver running | |
110 | * off the end of its clists & having to call back slstart for a | |
111 | * new packet. For a tty interface with any buffering at all, this | |
112 | * cost will be zero. Even with a totally brain dead interface (like | |
113 | * the one on a typical workstation), the cost will be <= 1 character | |
114 | * time. So, setting SLIP_HIWAT to ~100 guarantees that we'll lose | |
115 | * at most 1% while maintaining good interactive response. | |
41b6e003 | 116 | */ |
b25c0eeb | 117 | #define BUFOFFSET 128 |
af951bc0 SL |
118 | #define SLMAX (MCLBYTES - BUFOFFSET) |
119 | #define SLBUFSIZE (SLMAX + BUFOFFSET) | |
120 | #define SLMTU 296 | |
121 | #define SLIP_HIWAT roundup(50,CBSIZE) | |
270f7144 | 122 | #define CLISTRESERVE 1024 /* Can't let clists get too low */ |
b25c0eeb | 123 | |
4a23a8aa WN |
124 | /* |
125 | * SLIP ABORT ESCAPE MECHANISM: | |
126 | * (inspired by HAYES modem escape arrangement) | |
127 | * 1sec escape 1sec escape 1sec escape { 1sec escape 1sec escape } | |
128 | * signals a "soft" exit from slip mode by usermode process | |
4a23a8aa WN |
129 | */ |
130 | ||
131 | #define ABT_ESC '\033' /* can't be t_intr - distant host must know it*/ | |
132 | #define ABT_WAIT 1 /* in seconds - idle before an escape & after */ | |
133 | #define ABT_RECYCLE (5*2+2) /* in seconds - time window processing abort */ | |
134 | ||
4a23a8aa | 135 | #define ABT_SOFT 3 /* count of escapes */ |
41b6e003 | 136 | |
4a23a8aa | 137 | /* |
270f7144 | 138 | * The following disgusting hack gets around the problem that IP TOS |
b25c0eeb MK |
139 | * can't be set yet. We want to put "interactive" traffic on a high |
140 | * priority queue. To decide if traffic is interactive, we check that | |
141 | * a) it is TCP and b) one of its ports is telnet, rlogin or ftp control. | |
4a23a8aa | 142 | */ |
270f7144 MK |
143 | static u_short interactive_ports[8] = { |
144 | 0, 513, 0, 0, | |
145 | 0, 21, 0, 23, | |
146 | }; | |
147 | #define INTERACTIVE(p) (interactive_ports[(p) & 7] == (p)) | |
4a23a8aa | 148 | |
270f7144 | 149 | struct sl_softc sl_softc[NSL]; |
4a23a8aa | 150 | |
270f7144 MK |
151 | #define FRAME_END 0xc0 /* Frame End */ |
152 | #define FRAME_ESCAPE 0xdb /* Frame Esc */ | |
153 | #define TRANS_FRAME_END 0xdc /* transposed frame end */ | |
154 | #define TRANS_FRAME_ESCAPE 0xdd /* transposed frame esc */ | |
41b6e003 MK |
155 | |
156 | #define t_sc T_LINEP | |
157 | ||
158 | int sloutput(), slioctl(), ttrstrt(); | |
4dcb4b06 | 159 | extern struct timeval time; |
41b6e003 MK |
160 | |
161 | /* | |
162 | * Called from boot code to establish sl interfaces. | |
163 | */ | |
164 | slattach() | |
165 | { | |
166 | register struct sl_softc *sc; | |
167 | register int i = 0; | |
168 | ||
169 | for (sc = sl_softc; i < NSL; sc++) { | |
170 | sc->sc_if.if_name = "sl"; | |
171 | sc->sc_if.if_unit = i++; | |
172 | sc->sc_if.if_mtu = SLMTU; | |
173 | sc->sc_if.if_flags = IFF_POINTOPOINT; | |
4dcb4b06 | 174 | sc->sc_if.if_type = IFT_SLIP; |
41b6e003 MK |
175 | sc->sc_if.if_ioctl = slioctl; |
176 | sc->sc_if.if_output = sloutput; | |
270f7144 MK |
177 | sc->sc_if.if_snd.ifq_maxlen = 50; |
178 | sc->sc_fastq.ifq_maxlen = 32; | |
41b6e003 MK |
179 | if_attach(&sc->sc_if); |
180 | } | |
181 | } | |
182 | ||
270f7144 MK |
183 | static int |
184 | slinit(sc) | |
185 | register struct sl_softc *sc; | |
186 | { | |
187 | register caddr_t p; | |
188 | ||
189 | if (sc->sc_ep == (u_char *) 0) { | |
190 | MCLALLOC(p, M_WAIT); | |
191 | if (p) | |
b25c0eeb | 192 | sc->sc_ep = (u_char *)p + SLBUFSIZE; |
270f7144 MK |
193 | else { |
194 | printf("sl%d: can't allocate buffer\n", sc - sl_softc); | |
195 | sc->sc_if.if_flags &= ~IFF_UP; | |
196 | return (0); | |
197 | } | |
198 | } | |
af951bc0 | 199 | sc->sc_buf = sc->sc_ep - SLMAX; |
270f7144 MK |
200 | sc->sc_mp = sc->sc_buf; |
201 | sl_compress_init(&sc->sc_comp); | |
202 | return (1); | |
203 | } | |
204 | ||
41b6e003 MK |
205 | /* |
206 | * Line specific open routine. | |
207 | * Attach the given tty to the first available sl unit. | |
208 | */ | |
8011f5df | 209 | /* ARGSUSED */ |
41b6e003 MK |
210 | slopen(dev, tp) |
211 | dev_t dev; | |
212 | register struct tty *tp; | |
213 | { | |
06242857 | 214 | struct proc *p = curproc; /* XXX */ |
41b6e003 MK |
215 | register struct sl_softc *sc; |
216 | register int nsl; | |
06c16dfa | 217 | int error; |
41b6e003 | 218 | |
06242857 | 219 | if (error = suser(p->p_ucred, &p->p_acflag)) |
06c16dfa | 220 | return (error); |
270f7144 | 221 | |
8011f5df | 222 | if (tp->t_line == SLIPDISC) |
b25c0eeb | 223 | return (0); |
41b6e003 | 224 | |
270f7144 | 225 | for (nsl = NSL, sc = sl_softc; --nsl >= 0; sc++) |
41b6e003 | 226 | if (sc->sc_ttyp == NULL) { |
8011f5df MK |
227 | if (slinit(sc) == 0) |
228 | return (ENOBUFS); | |
41b6e003 MK |
229 | tp->t_sc = (caddr_t)sc; |
230 | sc->sc_ttyp = tp; | |
4dcb4b06 | 231 | sc->sc_if.if_baudrate = tp->t_ospeed; |
8011f5df | 232 | ttyflush(tp, FREAD | FWRITE); |
41b6e003 MK |
233 | return (0); |
234 | } | |
8011f5df | 235 | return (ENXIO); |
41b6e003 MK |
236 | } |
237 | ||
238 | /* | |
239 | * Line specific close routine. | |
240 | * Detach the tty from the sl unit. | |
241 | * Mimics part of ttyclose(). | |
242 | */ | |
243 | slclose(tp) | |
244 | struct tty *tp; | |
245 | { | |
246 | register struct sl_softc *sc; | |
247 | int s; | |
248 | ||
249 | ttywflush(tp); | |
270f7144 | 250 | s = splimp(); /* actually, max(spltty, splnet) */ |
41b6e003 | 251 | tp->t_line = 0; |
41b6e003 MK |
252 | sc = (struct sl_softc *)tp->t_sc; |
253 | if (sc != NULL) { | |
254 | if_down(&sc->sc_if); | |
255 | sc->sc_ttyp = NULL; | |
256 | tp->t_sc = NULL; | |
b25c0eeb | 257 | MCLFREE((caddr_t)(sc->sc_ep - SLBUFSIZE)); |
270f7144 MK |
258 | sc->sc_ep = 0; |
259 | sc->sc_mp = 0; | |
8011f5df | 260 | sc->sc_buf = 0; |
41b6e003 MK |
261 | } |
262 | splx(s); | |
263 | } | |
264 | ||
265 | /* | |
266 | * Line specific (tty) ioctl routine. | |
267 | * Provide a way to get the sl unit number. | |
268 | */ | |
8011f5df | 269 | /* ARGSUSED */ |
41b6e003 MK |
270 | sltioctl(tp, cmd, data, flag) |
271 | struct tty *tp; | |
272 | caddr_t data; | |
273 | { | |
4a23a8aa WN |
274 | struct sl_softc *sc = (struct sl_softc *)tp->t_sc; |
275 | int s; | |
41b6e003 | 276 | |
4a23a8aa | 277 | switch (cmd) { |
06242857 | 278 | case SLIOCGUNIT: |
4a23a8aa WN |
279 | *(int *)data = sc->sc_if.if_unit; |
280 | break; | |
4a23a8aa | 281 | |
4a23a8aa | 282 | case SLIOCGFLAGS: |
0e97cde8 | 283 | *(int *)data = sc->sc_if.if_flags; |
4a23a8aa | 284 | break; |
270f7144 | 285 | |
4a23a8aa | 286 | case SLIOCSFLAGS: |
0e97cde8 | 287 | #define LLC_MASK (IFF_LLC0|IFF_LLC1|IFF_LLC2) |
4a23a8aa | 288 | s = splimp(); |
0e97cde8 WN |
289 | sc->sc_if.if_flags = |
290 | (sc->sc_if.if_flags &~ LLC_MASK) | |
291 | | ((*(int *)data) & LLC_MASK); | |
4a23a8aa WN |
292 | splx(s); |
293 | break; | |
270f7144 | 294 | |
4a23a8aa WN |
295 | default: |
296 | return (-1); | |
fc62c320 | 297 | } |
4a23a8aa | 298 | return (0); |
41b6e003 MK |
299 | } |
300 | ||
301 | /* | |
302 | * Queue a packet. Start transmission if not active. | |
303 | */ | |
304 | sloutput(ifp, m, dst) | |
b25c0eeb | 305 | struct ifnet *ifp; |
41b6e003 MK |
306 | register struct mbuf *m; |
307 | struct sockaddr *dst; | |
308 | { | |
b25c0eeb | 309 | register struct sl_softc *sc = &sl_softc[ifp->if_unit]; |
270f7144 MK |
310 | register struct ip *ip; |
311 | register struct ifqueue *ifq; | |
41b6e003 MK |
312 | int s; |
313 | ||
314 | /* | |
315 | * `Cannot happen' (see slioctl). Someday we will extend | |
316 | * the line protocol to support other address families. | |
317 | */ | |
318 | if (dst->sa_family != AF_INET) { | |
b25c0eeb | 319 | printf("sl%d: af%d not supported\n", sc->sc_if.if_unit, |
41b6e003 MK |
320 | dst->sa_family); |
321 | m_freem(m); | |
322 | return (EAFNOSUPPORT); | |
323 | } | |
324 | ||
41b6e003 MK |
325 | if (sc->sc_ttyp == NULL) { |
326 | m_freem(m); | |
327 | return (ENETDOWN); /* sort of */ | |
328 | } | |
eadd9817 MK |
329 | if ((sc->sc_ttyp->t_state & TS_CARR_ON) == 0) { |
330 | m_freem(m); | |
331 | return (EHOSTUNREACH); | |
332 | } | |
b25c0eeb | 333 | ifq = &sc->sc_if.if_snd; |
270f7144 MK |
334 | if ((ip = mtod(m, struct ip *))->ip_p == IPPROTO_TCP) { |
335 | register int p = ((int *)ip)[ip->ip_hl]; | |
336 | ||
af951bc0 | 337 | if (INTERACTIVE(p & 0xffff) || INTERACTIVE(p >> 16)) { |
270f7144 | 338 | ifq = &sc->sc_fastq; |
af951bc0 SL |
339 | p = 1; |
340 | } else | |
341 | p = 0; | |
270f7144 | 342 | |
0e97cde8 | 343 | if (sc->sc_if.if_flags & SC_COMPRESS) { |
af951bc0 SL |
344 | /* |
345 | * The last parameter turns off connection id | |
346 | * compression for background traffic: Since | |
347 | * fastq traffic can jump ahead of the background | |
348 | * traffic, we don't know what order packets will | |
349 | * go on the line. | |
350 | */ | |
351 | p = sl_compress_tcp(m, ip, &sc->sc_comp, p); | |
270f7144 | 352 | *mtod(m, u_char *) |= p; |
4a23a8aa | 353 | } |
0e97cde8 | 354 | } else if (sc->sc_if.if_flags & SC_NOICMP && ip->ip_p == IPPROTO_ICMP) { |
270f7144 MK |
355 | m_freem(m); |
356 | return (0); | |
4a23a8aa | 357 | } |
270f7144 MK |
358 | s = splimp(); |
359 | if (IF_QFULL(ifq)) { | |
360 | IF_DROP(ifq); | |
41b6e003 | 361 | m_freem(m); |
270f7144 | 362 | splx(s); |
8011f5df | 363 | sc->sc_if.if_oerrors++; |
41b6e003 MK |
364 | return (ENOBUFS); |
365 | } | |
270f7144 | 366 | IF_ENQUEUE(ifq, m); |
4dcb4b06 | 367 | sc->sc_if.if_lastchange = time; |
b25c0eeb | 368 | if (sc->sc_ttyp->t_outq.c_cc == 0) |
41b6e003 | 369 | slstart(sc->sc_ttyp); |
b25c0eeb | 370 | splx(s); |
41b6e003 MK |
371 | return (0); |
372 | } | |
373 | ||
374 | /* | |
375 | * Start output on interface. Get another datagram | |
376 | * to send from the interface queue and map it to | |
377 | * the interface before starting output. | |
378 | */ | |
379 | slstart(tp) | |
380 | register struct tty *tp; | |
381 | { | |
382 | register struct sl_softc *sc = (struct sl_softc *)tp->t_sc; | |
383 | register struct mbuf *m; | |
8011f5df | 384 | register u_char *cp; |
270f7144 | 385 | int s; |
8011f5df MK |
386 | struct mbuf *m2; |
387 | extern int cfreecount; | |
41b6e003 | 388 | |
8011f5df MK |
389 | for (;;) { |
390 | /* | |
391 | * If there is more in the output queue, just send it now. | |
392 | * We are being called in lieu of ttstart and must do what | |
393 | * it would. | |
394 | */ | |
270f7144 MK |
395 | if (tp->t_outq.c_cc != 0) { |
396 | (*tp->t_oproc)(tp); | |
397 | if (tp->t_outq.c_cc > SLIP_HIWAT) | |
398 | return; | |
399 | } | |
8011f5df MK |
400 | /* |
401 | * This happens briefly when the line shuts down. | |
402 | */ | |
403 | if (sc == NULL) | |
404 | return; | |
41b6e003 | 405 | |
8011f5df MK |
406 | /* |
407 | * Get a packet and send it to the interface. | |
408 | */ | |
409 | s = splimp(); | |
270f7144 MK |
410 | IF_DEQUEUE(&sc->sc_fastq, m); |
411 | if (m == NULL) | |
412 | IF_DEQUEUE(&sc->sc_if.if_snd, m); | |
8011f5df | 413 | splx(s); |
e8e27e8e MK |
414 | if (m == NULL) |
415 | return; | |
4dcb4b06 | 416 | sc->sc_if.if_lastchange = time; |
0e97cde8 | 417 | |
270f7144 MK |
418 | /* |
419 | * If system is getting low on clists, just flush our | |
420 | * output queue (if the stuff was important, it'll get | |
421 | * retransmitted). | |
422 | */ | |
423 | if (cfreecount < CLISTRESERVE + SLMTU) { | |
424 | m_freem(m); | |
425 | sc->sc_if.if_collisions++; | |
426 | continue; | |
427 | } | |
41b6e003 | 428 | /* |
8011f5df MK |
429 | * The extra FRAME_END will start up a new packet, and thus |
430 | * will flush any accumulated garbage. We do this whenever | |
431 | * the line may have been idle for some time. | |
41b6e003 | 432 | */ |
270f7144 MK |
433 | if (tp->t_outq.c_cc == 0) { |
434 | ++sc->sc_bytessent; | |
8011f5df | 435 | (void) putc(FRAME_END, &tp->t_outq); |
270f7144 | 436 | } |
8011f5df MK |
437 | |
438 | while (m) { | |
270f7144 MK |
439 | register u_char *ep; |
440 | ||
441 | cp = mtod(m, u_char *); ep = cp + m->m_len; | |
442 | while (cp < ep) { | |
8011f5df MK |
443 | /* |
444 | * Find out how many bytes in the string we can | |
445 | * handle without doing something special. | |
446 | */ | |
270f7144 MK |
447 | register u_char *bp = cp; |
448 | ||
449 | while (cp < ep) { | |
450 | switch (*cp++) { | |
451 | case FRAME_ESCAPE: | |
452 | case FRAME_END: | |
453 | --cp; | |
454 | goto out; | |
455 | } | |
456 | } | |
457 | out: | |
458 | if (cp > bp) { | |
8011f5df MK |
459 | /* |
460 | * Put n characters at once | |
461 | * into the tty output queue. | |
462 | */ | |
270f7144 | 463 | if (b_to_q((char *)bp, cp - bp, &tp->t_outq)) |
8011f5df | 464 | break; |
270f7144 | 465 | sc->sc_bytessent += cp - bp; |
8011f5df MK |
466 | } |
467 | /* | |
468 | * If there are characters left in the mbuf, | |
469 | * the first one must be special.. | |
470 | * Put it out in a different form. | |
471 | */ | |
270f7144 | 472 | if (cp < ep) { |
8011f5df MK |
473 | if (putc(FRAME_ESCAPE, &tp->t_outq)) |
474 | break; | |
270f7144 | 475 | if (putc(*cp++ == FRAME_ESCAPE ? |
8011f5df MK |
476 | TRANS_FRAME_ESCAPE : TRANS_FRAME_END, |
477 | &tp->t_outq)) { | |
478 | (void) unputc(&tp->t_outq); | |
479 | break; | |
480 | } | |
270f7144 | 481 | sc->sc_bytessent += 2; |
8011f5df MK |
482 | } |
483 | } | |
484 | MFREE(m, m2); | |
485 | m = m2; | |
486 | } | |
41b6e003 | 487 | |
8011f5df MK |
488 | if (putc(FRAME_END, &tp->t_outq)) { |
489 | /* | |
490 | * Not enough room. Remove a char to make room | |
491 | * and end the packet normally. | |
492 | * If you get many collisions (more than one or two | |
493 | * a day) you probably do not have enough clists | |
494 | * and you should increase "nclist" in param.c. | |
495 | */ | |
496 | (void) unputc(&tp->t_outq); | |
497 | (void) putc(FRAME_END, &tp->t_outq); | |
498 | sc->sc_if.if_collisions++; | |
8011f5df | 499 | } else { |
270f7144 MK |
500 | ++sc->sc_bytessent; |
501 | sc->sc_if.if_opackets++; | |
8011f5df | 502 | } |
4dcb4b06 | 503 | sc->sc_if.if_obytes = sc->sc_bytessent; |
8011f5df | 504 | } |
41b6e003 MK |
505 | } |
506 | ||
507 | /* | |
270f7144 | 508 | * Copy data buffer to mbuf chain; add ifnet pointer. |
41b6e003 | 509 | */ |
270f7144 MK |
510 | static struct mbuf * |
511 | sl_btom(sc, len) | |
512 | register struct sl_softc *sc; | |
41b6e003 | 513 | register int len; |
41b6e003 | 514 | { |
270f7144 | 515 | register struct mbuf *m; |
41b6e003 | 516 | |
270f7144 MK |
517 | MGETHDR(m, M_DONTWAIT, MT_DATA); |
518 | if (m == NULL) | |
519 | return (NULL); | |
fc62c320 | 520 | |
270f7144 | 521 | /* |
b25c0eeb | 522 | * If we have more than MHLEN bytes, it's cheaper to |
270f7144 MK |
523 | * queue the cluster we just filled & allocate a new one |
524 | * for the input buffer. Otherwise, fill the mbuf we | |
525 | * allocated above. Note that code in the input routine | |
526 | * guarantees that packet will fit in a cluster. | |
527 | */ | |
270f7144 MK |
528 | if (len >= MHLEN) { |
529 | MCLGET(m, M_DONTWAIT); | |
530 | if ((m->m_flags & M_EXT) == 0) { | |
b25c0eeb MK |
531 | /* |
532 | * we couldn't get a cluster - if memory's this | |
533 | * low, it's time to start dropping packets. | |
534 | */ | |
535 | (void) m_free(m); | |
41b6e003 MK |
536 | return (NULL); |
537 | } | |
b25c0eeb MK |
538 | sc->sc_ep = mtod(m, u_char *) + SLBUFSIZE; |
539 | m->m_data = (caddr_t)sc->sc_buf; | |
540 | m->m_ext.ext_buf = (caddr_t)((int)sc->sc_buf &~ MCLOFSET); | |
270f7144 | 541 | } else |
b25c0eeb | 542 | bcopy((caddr_t)sc->sc_buf, mtod(m, caddr_t), len); |
270f7144 MK |
543 | |
544 | m->m_len = len; | |
545 | m->m_pkthdr.len = len; | |
546 | m->m_pkthdr.rcvif = &sc->sc_if; | |
547 | return (m); | |
41b6e003 MK |
548 | } |
549 | ||
550 | /* | |
551 | * tty interface receiver interrupt. | |
552 | */ | |
553 | slinput(c, tp) | |
554 | register int c; | |
555 | register struct tty *tp; | |
556 | { | |
557 | register struct sl_softc *sc; | |
558 | register struct mbuf *m; | |
270f7144 | 559 | register int len; |
41b6e003 MK |
560 | int s; |
561 | ||
8011f5df | 562 | tk_nin++; |
41b6e003 MK |
563 | sc = (struct sl_softc *)tp->t_sc; |
564 | if (sc == NULL) | |
565 | return; | |
270f7144 | 566 | if (!(tp->t_state&TS_CARR_ON)) /* XXX */ |
4a23a8aa | 567 | return; |
41b6e003 | 568 | |
270f7144 | 569 | ++sc->sc_bytesrcvd; |
4dcb4b06 | 570 | ++sc->sc_if.if_ibytes; |
270f7144 MK |
571 | c &= 0xff; /* XXX */ |
572 | ||
573 | #ifdef ABT_ESC | |
0e97cde8 | 574 | { |
270f7144 MK |
575 | /* if we see an abort after "idle" time, count it */ |
576 | if (c == ABT_ESC && time.tv_sec >= sc->sc_lasttime + ABT_WAIT) { | |
577 | sc->sc_abortcount++; | |
578 | /* record when the first abort escape arrived */ | |
579 | if (sc->sc_abortcount == 1) | |
580 | sc->sc_starttime = time.tv_sec; | |
581 | } | |
582 | /* | |
583 | * if we have an abort, see that we have not run out of time, | |
584 | * or that we have an "idle" time after the complete escape | |
585 | * sequence | |
586 | */ | |
587 | if (sc->sc_abortcount) { | |
588 | if (time.tv_sec >= sc->sc_starttime + ABT_RECYCLE) | |
589 | sc->sc_abortcount = 0; | |
590 | if (sc->sc_abortcount >= ABT_SOFT && | |
591 | time.tv_sec >= sc->sc_lasttime + ABT_WAIT) { | |
592 | slclose(tp); | |
593 | return; | |
594 | } | |
595 | } | |
596 | sc->sc_lasttime = time.tv_sec; | |
4a23a8aa | 597 | } |
270f7144 | 598 | #endif |
4a23a8aa | 599 | |
270f7144 | 600 | switch (c) { |
41b6e003 | 601 | |
270f7144 MK |
602 | case TRANS_FRAME_ESCAPE: |
603 | if (sc->sc_escape) | |
41b6e003 | 604 | c = FRAME_ESCAPE; |
270f7144 | 605 | break; |
41b6e003 | 606 | |
270f7144 MK |
607 | case TRANS_FRAME_END: |
608 | if (sc->sc_escape) | |
41b6e003 | 609 | c = FRAME_END; |
270f7144 | 610 | break; |
41b6e003 | 611 | |
270f7144 MK |
612 | case FRAME_ESCAPE: |
613 | sc->sc_escape = 1; | |
614 | return; | |
41b6e003 | 615 | |
270f7144 MK |
616 | case FRAME_END: |
617 | len = sc->sc_mp - sc->sc_buf; | |
618 | if (len < 3) | |
619 | /* less than min length packet - ignore */ | |
620 | goto newpack; | |
621 | ||
622 | if ((c = (*sc->sc_buf & 0xf0)) != (IPVERSION << 4)) { | |
623 | if (c & 0x80) | |
624 | c = TYPE_COMPRESSED_TCP; | |
625 | else if (c == TYPE_UNCOMPRESSED_TCP) | |
626 | *sc->sc_buf &= 0x4f; /* XXX */ | |
af951bc0 SL |
627 | /* |
628 | * We've got something that's not an IP packet. | |
629 | * If compression is enabled, try to decompress it. | |
630 | * Otherwise, if `auto-enable' compression is on and | |
631 | * it's a reasonable packet, decompress it and then | |
632 | * enable compression. Otherwise, drop it. | |
633 | */ | |
0e97cde8 | 634 | if (sc->sc_if.if_flags & SC_COMPRESS) { |
af951bc0 SL |
635 | len = sl_uncompress_tcp(&sc->sc_buf, len, |
636 | (u_int)c, &sc->sc_comp); | |
637 | if (len <= 0) | |
638 | goto error; | |
0e97cde8 | 639 | } else if ((sc->sc_if.if_flags & SC_AUTOCOMP) && |
af951bc0 SL |
640 | c == TYPE_UNCOMPRESSED_TCP && len >= 40) { |
641 | len = sl_uncompress_tcp(&sc->sc_buf, len, | |
642 | (u_int)c, &sc->sc_comp); | |
643 | if (len <= 0) | |
644 | goto error; | |
0e97cde8 | 645 | sc->sc_if.if_flags |= SC_COMPRESS; |
af951bc0 | 646 | } else |
270f7144 MK |
647 | goto error; |
648 | } | |
649 | m = sl_btom(sc, len); | |
650 | if (m == NULL) | |
651 | goto error; | |
41b6e003 | 652 | |
270f7144 | 653 | sc->sc_if.if_ipackets++; |
4dcb4b06 | 654 | sc->sc_if.if_lastchange = time; |
270f7144 MK |
655 | s = splimp(); |
656 | if (IF_QFULL(&ipintrq)) { | |
657 | IF_DROP(&ipintrq); | |
658 | sc->sc_if.if_ierrors++; | |
4dcb4b06 | 659 | sc->sc_if.if_iqdrops++; |
270f7144 MK |
660 | m_freem(m); |
661 | } else { | |
662 | IF_ENQUEUE(&ipintrq, m); | |
663 | schednetisr(NETISR_IP); | |
41b6e003 | 664 | } |
270f7144 MK |
665 | splx(s); |
666 | goto newpack; | |
41b6e003 | 667 | } |
270f7144 MK |
668 | if (sc->sc_mp < sc->sc_ep) { |
669 | *sc->sc_mp++ = c; | |
670 | sc->sc_escape = 0; | |
41b6e003 MK |
671 | return; |
672 | } | |
270f7144 MK |
673 | error: |
674 | sc->sc_if.if_ierrors++; | |
675 | newpack: | |
af951bc0 | 676 | sc->sc_mp = sc->sc_buf = sc->sc_ep - SLMAX; |
270f7144 | 677 | sc->sc_escape = 0; |
41b6e003 MK |
678 | } |
679 | ||
680 | /* | |
681 | * Process an ioctl request. | |
682 | */ | |
683 | slioctl(ifp, cmd, data) | |
684 | register struct ifnet *ifp; | |
685 | int cmd; | |
686 | caddr_t data; | |
687 | { | |
688 | register struct ifaddr *ifa = (struct ifaddr *)data; | |
689 | int s = splimp(), error = 0; | |
690 | ||
691 | switch (cmd) { | |
692 | ||
693 | case SIOCSIFADDR: | |
b72a6efb | 694 | if (ifa->ifa_addr->sa_family == AF_INET) |
41b6e003 MK |
695 | ifp->if_flags |= IFF_UP; |
696 | else | |
697 | error = EAFNOSUPPORT; | |
698 | break; | |
699 | ||
700 | case SIOCSIFDSTADDR: | |
b72a6efb | 701 | if (ifa->ifa_addr->sa_family != AF_INET) |
41b6e003 MK |
702 | error = EAFNOSUPPORT; |
703 | break; | |
704 | ||
705 | default: | |
706 | error = EINVAL; | |
707 | } | |
708 | splx(s); | |
709 | return (error); | |
710 | } | |
711 | #endif |