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