Commit | Line | Data |
---|---|---|
7c2732d1 MK |
1 | /* @(#)if_sl.c 5.2 (Berkeley) %G% */ |
2 | ||
41b6e003 MK |
3 | /* |
4 | * Serial Line interface | |
5 | * | |
6 | * Rick Adams | |
7 | * Center for Seismic Studies | |
8 | * 1300 N 17th Street, Suite 1450 | |
9 | * Arlington, Virginia 22209 | |
10 | * (703)276-7900 | |
11 | * rick@seismo.ARPA | |
12 | * seismo!rick | |
13 | * | |
14 | * Some things done here could obviously be done in a better way, | |
15 | * but they were done this way to minimize the number of files | |
16 | * that had to be changed to accomodate this device. | |
41b6e003 MK |
17 | * |
18 | * Pounded on heavily by Chris Torek (chris@mimsy.umd.edu, umcp-cs!chris). | |
19 | * N.B.: this belongs in netinet, not vaxif, the way it stands now. | |
7c2732d1 MK |
20 | * Should have a link-layer type designation, but wouldn't be |
21 | * backwards-compatible. | |
41b6e003 MK |
22 | * |
23 | * Converted to 4.3BSD Beta by Chris Torek. | |
24 | */ | |
25 | ||
26 | /* $Header: if_sl.c,v 1.12 85/12/20 21:54:55 chris Exp $ */ | |
27 | /* from if_sl.c,v 1.11 84/10/04 12:54:47 rick Exp */ | |
28 | ||
29 | #include "sl.h" | |
30 | #if NSL > 0 | |
31 | ||
32 | #include "param.h" | |
33 | #include "mbuf.h" | |
34 | #include "buf.h" | |
35 | #include "socket.h" | |
36 | #include "ioctl.h" | |
37 | #include "tty.h" | |
38 | #include "errno.h" | |
39 | ||
40 | #include "../net/if.h" | |
41 | #include "../net/netisr.h" | |
42 | #include "../net/route.h" | |
43 | #include "../netinet/in.h" | |
44 | #include "../netinet/in_systm.h" | |
45 | #include "../netinet/ip.h" | |
46 | #include "../netinet/ip_var.h" | |
47 | ||
48 | #ifdef vax | |
49 | #include "../vax/mtpr.h" | |
50 | #endif vax | |
51 | ||
52 | /* | |
53 | * N.B.: SLMTU is now a hard limit on input packet size. Some limit | |
54 | * is required, lest we use up all mbufs in the case of deleterious data | |
55 | * dribbling down the line. | |
56 | */ | |
57 | #define SLMTU 1006 | |
58 | ||
59 | struct sl_softc { | |
60 | struct ifnet sc_if; /* network-visible interface */ | |
61 | short sc_flags; /* see below */ | |
62 | short sc_ilen; /* length of input-packet-so-far */ | |
63 | struct tty *sc_ttyp; /* pointer to tty structure */ | |
64 | char *sc_mp; /* pointer to next available buf char */ | |
65 | char sc_buf[SLMTU]; /* input buffer */ | |
66 | } sl_softc[NSL]; | |
67 | ||
68 | /* flags */ | |
69 | #define SC_ESCAPED 0x0001 /* saw a FRAME_ESCAPE */ | |
70 | #define SC_OACTIVE 0x0002 /* output tty is active */ | |
71 | ||
72 | #define FRAME_END 0300 /* Frame End */ | |
73 | #define FRAME_ESCAPE 0333 /* Frame Esc */ | |
74 | #define TRANS_FRAME_END 0334 /* transposed frame end */ | |
75 | #define TRANS_FRAME_ESCAPE 0335 /* transposed frame esc */ | |
76 | ||
77 | #define t_sc T_LINEP | |
78 | ||
79 | int sloutput(), slioctl(), ttrstrt(); | |
80 | ||
81 | /* | |
82 | * Called from boot code to establish sl interfaces. | |
83 | */ | |
84 | slattach() | |
85 | { | |
86 | register struct sl_softc *sc; | |
87 | register int i = 0; | |
88 | ||
89 | for (sc = sl_softc; i < NSL; sc++) { | |
90 | sc->sc_if.if_name = "sl"; | |
91 | sc->sc_if.if_unit = i++; | |
92 | sc->sc_if.if_mtu = SLMTU; | |
93 | sc->sc_if.if_flags = IFF_POINTOPOINT; | |
94 | sc->sc_if.if_ioctl = slioctl; | |
95 | sc->sc_if.if_output = sloutput; | |
96 | sc->sc_if.if_snd.ifq_maxlen = IFQ_MAXLEN; | |
97 | if_attach(&sc->sc_if); | |
98 | } | |
99 | } | |
100 | ||
101 | /* | |
102 | * Line specific open routine. | |
103 | * Attach the given tty to the first available sl unit. | |
104 | */ | |
105 | slopen(dev, tp) | |
106 | dev_t dev; | |
107 | register struct tty *tp; | |
108 | { | |
109 | register struct sl_softc *sc; | |
110 | register int nsl; | |
111 | ||
112 | if (tp->t_sc != NULL) | |
113 | return (EBUSY); | |
114 | ||
115 | for (nsl = 0, sc = sl_softc; nsl < NSL; nsl++, sc++) | |
116 | if (sc->sc_ttyp == NULL) { | |
117 | sc->sc_flags = 0; | |
118 | sc->sc_ilen = 0; | |
119 | sc->sc_mp = sc->sc_buf; | |
120 | tp->t_sc = (caddr_t)sc; | |
121 | sc->sc_ttyp = tp; | |
122 | return (0); | |
123 | } | |
124 | ||
125 | return (ENOSPC); | |
126 | } | |
127 | ||
128 | /* | |
129 | * Line specific close routine. | |
130 | * Detach the tty from the sl unit. | |
131 | * Mimics part of ttyclose(). | |
132 | */ | |
133 | slclose(tp) | |
134 | struct tty *tp; | |
135 | { | |
136 | register struct sl_softc *sc; | |
137 | int s; | |
138 | ||
139 | ttywflush(tp); | |
140 | tp->t_line = 0; | |
141 | s = splimp(); /* paranoid; splnet probably ok */ | |
142 | sc = (struct sl_softc *)tp->t_sc; | |
143 | if (sc != NULL) { | |
144 | if_down(&sc->sc_if); | |
145 | sc->sc_ttyp = NULL; | |
146 | tp->t_sc = NULL; | |
147 | } | |
148 | splx(s); | |
149 | } | |
150 | ||
151 | /* | |
152 | * Line specific (tty) ioctl routine. | |
153 | * Provide a way to get the sl unit number. | |
154 | */ | |
155 | sltioctl(tp, cmd, data, flag) | |
156 | struct tty *tp; | |
157 | caddr_t data; | |
158 | { | |
159 | ||
160 | if (cmd == TIOCGETD) { | |
161 | *(int *)data = ((struct sl_softc *)tp->t_sc)->sc_if.if_unit; | |
162 | return (0); | |
163 | } | |
164 | return (-1); | |
165 | } | |
166 | ||
167 | /* | |
168 | * Queue a packet. Start transmission if not active. | |
169 | */ | |
170 | sloutput(ifp, m, dst) | |
171 | register struct ifnet *ifp; | |
172 | register struct mbuf *m; | |
173 | struct sockaddr *dst; | |
174 | { | |
175 | register struct sl_softc *sc; | |
176 | int s; | |
177 | ||
178 | /* | |
179 | * `Cannot happen' (see slioctl). Someday we will extend | |
180 | * the line protocol to support other address families. | |
181 | */ | |
182 | if (dst->sa_family != AF_INET) { | |
183 | printf("sl%d: af%d not supported\n", ifp->if_unit, | |
184 | dst->sa_family); | |
185 | m_freem(m); | |
186 | return (EAFNOSUPPORT); | |
187 | } | |
188 | ||
189 | sc = &sl_softc[ifp->if_unit]; | |
190 | if (sc->sc_ttyp == NULL) { | |
191 | m_freem(m); | |
192 | return (ENETDOWN); /* sort of */ | |
193 | } | |
194 | s = splimp(); | |
195 | if (IF_QFULL(&ifp->if_snd)) { | |
196 | IF_DROP(&ifp->if_snd); | |
197 | splx(s); | |
198 | m_freem(m); | |
199 | sc->sc_if.if_collisions++; | |
200 | return (ENOBUFS); | |
201 | } | |
202 | IF_ENQUEUE(&ifp->if_snd, m); | |
203 | if ((sc->sc_flags & SC_OACTIVE) == 0) { | |
204 | splx(s); | |
205 | slstart(sc->sc_ttyp); | |
206 | } else | |
207 | splx(s); | |
208 | return (0); | |
209 | } | |
210 | ||
211 | /* | |
212 | * Start output on interface. Get another datagram | |
213 | * to send from the interface queue and map it to | |
214 | * the interface before starting output. | |
215 | */ | |
216 | slstart(tp) | |
217 | register struct tty *tp; | |
218 | { | |
219 | register struct sl_softc *sc = (struct sl_softc *)tp->t_sc; | |
220 | register struct mbuf *m; | |
221 | register int c, len; | |
222 | register u_char *mcp; | |
223 | int flush; | |
224 | ||
225 | /* | |
226 | * If there is more in the output queue, just send it now. | |
227 | * We are being called in lieu of ttstart and must do what | |
228 | * it would. | |
229 | */ | |
230 | if (tp->t_outq.c_cc > 0) { | |
231 | ttstart(tp); | |
232 | return; | |
233 | } | |
234 | ||
235 | /* | |
236 | * This happens briefly when the line shuts down. | |
237 | */ | |
238 | if (sc == NULL) | |
239 | return; | |
240 | ||
241 | /* | |
242 | * Get a packet and map it to the interface. | |
243 | */ | |
244 | c = splimp(); | |
245 | IF_DEQUEUE(&sc->sc_if.if_snd, m); | |
246 | if (m == NULL) { | |
247 | sc->sc_flags &= ~SC_OACTIVE; | |
248 | splx(c); | |
249 | return; | |
250 | } | |
251 | flush = !(sc->sc_flags & SC_OACTIVE); | |
252 | sc->sc_flags |= SC_OACTIVE; | |
253 | splx(c); | |
254 | ||
255 | /* | |
256 | * The extra FRAME_END will start up a new packet, and thus | |
257 | * will flush any accumulated garbage. We do this whenever | |
258 | * the line may have been idle for some time. | |
259 | */ | |
260 | if (flush) | |
261 | (void) putc(FRAME_END, &tp->t_outq); | |
262 | ||
263 | while (m != NULL) { | |
264 | len = m->m_len; | |
265 | mcp = mtod(m, u_char *); | |
266 | while (--len >= 0) { | |
267 | c = *mcp++; | |
268 | if (c == FRAME_ESCAPE || c == FRAME_END) { | |
269 | if (putc(FRAME_ESCAPE, &tp->t_outq)) | |
270 | goto full; | |
271 | c = c == FRAME_ESCAPE ? TRANS_FRAME_ESCAPE : | |
272 | TRANS_FRAME_END; | |
273 | if (putc(c, &tp->t_outq)) { | |
274 | (void) unputc(&tp->t_outq); | |
275 | goto full; | |
276 | } | |
277 | } else | |
278 | if (putc(c, &tp->t_outq)) | |
279 | goto full; | |
280 | } | |
281 | m = m_free(m); | |
282 | } | |
283 | ||
284 | if (putc(FRAME_END, &tp->t_outq)) { | |
285 | full: | |
286 | /* | |
287 | * If you get many oerrors (more than one or two a day) | |
288 | * you probably do not have enough clists and you should | |
289 | * increase "nclist" in param.c. | |
290 | */ | |
291 | (void) unputc(&tp->t_outq); /* make room */ | |
292 | putc(FRAME_END, &tp->t_outq); /* end the packet */ | |
293 | sc->sc_if.if_oerrors++; | |
294 | } else | |
295 | sc->sc_if.if_opackets++; | |
296 | ||
297 | /* | |
298 | * Start transmission. Note that slstart, not ttstart, will be | |
299 | * called when the transmission completes, be that after a single | |
300 | * piece of what we have mapped, or be it after the entire thing | |
301 | * has been sent. That is why we need to check the output queue | |
302 | * count at the top. | |
303 | */ | |
304 | ttstart(tp); | |
305 | } | |
306 | ||
307 | /* | |
308 | * Copy data buffer to mbuf chain; add ifnet pointer ifp. | |
309 | */ | |
310 | struct mbuf * | |
311 | sl_btom(addr, len, ifp) | |
312 | register caddr_t addr; | |
313 | register int len; | |
314 | struct ifnet *ifp; | |
315 | { | |
316 | register struct mbuf *m, **mp; | |
317 | register int count; | |
318 | struct mbuf *top = NULL; | |
319 | ||
320 | mp = ⊤ | |
321 | while (len > 0) { | |
322 | MGET(m, M_DONTWAIT, MT_DATA); | |
323 | if ((*mp = m) == NULL) { | |
324 | m_freem(top); | |
325 | return (NULL); | |
326 | } | |
327 | if (ifp) { | |
328 | m->m_off += sizeof(ifp); | |
329 | count = MIN(len, MLEN - sizeof(ifp)); | |
330 | } else { | |
331 | if (len >= NBPG) { | |
332 | struct mbuf *p; | |
333 | ||
334 | MCLGET(p, 1); | |
335 | if (p != NULL) { | |
336 | count = MIN(len, CLBYTES); | |
337 | m->m_off = (int)p - (int)m; | |
338 | } else | |
339 | count = MIN(len, MLEN); | |
340 | } else | |
341 | count = MIN(len, MLEN); | |
342 | } | |
343 | bcopy(addr, mtod(m, caddr_t), count); | |
344 | m->m_len = count; | |
345 | if (ifp) { | |
346 | m->m_off -= sizeof(ifp); | |
347 | m->m_len += sizeof(ifp); | |
348 | *mtod(m, struct ifnet **) = ifp; | |
349 | ifp = NULL; | |
350 | } | |
351 | addr += count; | |
352 | len -= count; | |
353 | mp = &m->m_next; | |
354 | } | |
355 | return (top); | |
356 | } | |
357 | ||
358 | /* | |
359 | * tty interface receiver interrupt. | |
360 | */ | |
361 | slinput(c, tp) | |
362 | register int c; | |
363 | register struct tty *tp; | |
364 | { | |
365 | register struct sl_softc *sc; | |
366 | register struct mbuf *m; | |
367 | int s; | |
368 | ||
369 | sc = (struct sl_softc *)tp->t_sc; | |
370 | if (sc == NULL) | |
371 | return; | |
372 | ||
373 | c &= 0xff; | |
374 | if (sc->sc_flags & SC_ESCAPED) { | |
375 | sc->sc_flags &= ~SC_ESCAPED; | |
376 | switch (c) { | |
377 | ||
378 | case TRANS_FRAME_ESCAPE: | |
379 | c = FRAME_ESCAPE; | |
380 | break; | |
381 | ||
382 | case TRANS_FRAME_END: | |
383 | c = FRAME_END; | |
384 | break; | |
385 | ||
386 | default: | |
387 | sc->sc_if.if_ierrors++; | |
388 | sc->sc_mp = sc->sc_buf; | |
389 | sc->sc_ilen = 0; | |
390 | return; | |
391 | } | |
392 | } else { | |
393 | switch (c) { | |
394 | ||
395 | case FRAME_END: | |
396 | if (sc->sc_ilen == 0) /* ignore */ | |
397 | return; | |
398 | m = sl_btom(sc->sc_buf, sc->sc_ilen, &sc->sc_if); | |
399 | if (m == NULL) { | |
400 | sc->sc_if.if_ierrors++; | |
401 | return; | |
402 | } | |
403 | sc->sc_mp = sc->sc_buf; | |
404 | sc->sc_ilen = 0; | |
405 | sc->sc_if.if_ipackets++; | |
406 | s = splimp(); | |
407 | if (IF_QFULL(&ipintrq)) { | |
408 | IF_DROP(&ipintrq); | |
409 | sc->sc_if.if_ierrors++; | |
410 | m_freem(m); | |
411 | } else { | |
412 | IF_ENQUEUE(&ipintrq, m); | |
413 | schednetisr(NETISR_IP); | |
414 | } | |
415 | splx(s); | |
416 | return; | |
417 | ||
418 | case FRAME_ESCAPE: | |
419 | sc->sc_flags |= SC_ESCAPED; | |
420 | return; | |
421 | } | |
422 | } | |
423 | if (++sc->sc_ilen >= SLMTU) { | |
424 | sc->sc_if.if_ierrors++; | |
425 | sc->sc_mp = sc->sc_buf; | |
426 | sc->sc_ilen = 0; | |
427 | return; | |
428 | } | |
429 | *sc->sc_mp++ = c; | |
430 | } | |
431 | ||
432 | /* | |
433 | * Process an ioctl request. | |
434 | */ | |
435 | slioctl(ifp, cmd, data) | |
436 | register struct ifnet *ifp; | |
437 | int cmd; | |
438 | caddr_t data; | |
439 | { | |
440 | register struct ifaddr *ifa = (struct ifaddr *)data; | |
441 | int s = splimp(), error = 0; | |
442 | ||
443 | switch (cmd) { | |
444 | ||
445 | case SIOCSIFADDR: | |
446 | if (ifa->ifa_addr.sa_family == AF_INET) | |
447 | ifp->if_flags |= IFF_UP; | |
448 | else | |
449 | error = EAFNOSUPPORT; | |
450 | break; | |
451 | ||
452 | case SIOCSIFDSTADDR: | |
453 | if (ifa->ifa_addr.sa_family != AF_INET) | |
454 | error = EAFNOSUPPORT; | |
455 | break; | |
456 | ||
457 | default: | |
458 | error = EINVAL; | |
459 | } | |
460 | splx(s); | |
461 | return (error); | |
462 | } | |
463 | #endif |