source: rtems/c/src/libnetworking/net/ppp_tty.c @ 90f30c8

4.104.114.84.95
Last change on this file since 90f30c8 was fd55b7d, checked in by Joel Sherrill <joel.sherrill@…>, on 03/21/02 at 15:01:32

2002-03-21 Ilya Alexeev <ilya@…>

  • net/if_ppp.c, net/ppp_tty.c: Initial preparations for multiple PPPD connections.
  • Property mode set to 100644
File size: 25.4 KB
Line 
1/*
2 * ppp_tty.c - Point-to-Point Protocol (PPP) driver for asynchronous
3 *             tty devices.
4 *
5 * Copyright (c) 1989 Carnegie Mellon University.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms are permitted
9 * provided that the above copyright notice and this paragraph are
10 * duplicated in all such forms and that any documentation,
11 * advertising materials, and other materials related to such
12 * distribution and use acknowledge that the software was developed
13 * by Carnegie Mellon University.  The name of the
14 * University may not be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
18 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
19 *
20 * Drew D. Perkins
21 * Carnegie Mellon University
22 * 4910 Forbes Ave.
23 * Pittsburgh, PA 15213
24 * (412) 268-8576
25 * ddp@andrew.cmu.edu
26 *
27 * Based on:
28 *      @(#)if_sl.c     7.6.1.2 (Berkeley) 2/15/89
29 *
30 * Copyright (c) 1987 Regents of the University of California.
31 * All rights reserved.
32 *
33 * Redistribution and use in source and binary forms are permitted
34 * provided that the above copyright notice and this paragraph are
35 * duplicated in all such forms and that any documentation,
36 * advertising materials, and other materials related to such
37 * distribution and use acknowledge that the software was developed
38 * by the University of California, Berkeley.  The name of the
39 * University may not be used to endorse or promote products derived
40 * from this software without specific prior written permission.
41 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
42 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
43 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
44 *
45 * Serial Line interface
46 *
47 * Rick Adams
48 * Center for Seismic Studies
49 * 1300 N 17th Street, Suite 1450
50 * Arlington, Virginia 22209
51 * (703)276-7900
52 * rick@seismo.ARPA
53 * seismo!rick
54 *
55 * Pounded on heavily by Chris Torek (chris@mimsy.umd.edu, umcp-cs!chris).
56 * Converted to 4.3BSD Beta by Chris Torek.
57 * Other changes made at Berkeley, based in part on code by Kirk Smith.
58 *
59 * Converted to 4.3BSD+ 386BSD by Brad Parker (brad@cayman.com)
60 * Added VJ tcp header compression; more unified ioctls
61 *
62 * Extensively modified by Paul Mackerras (paulus@cs.anu.edu.au).
63 * Cleaned up a lot of the mbuf-related code to fix bugs that
64 * caused system crashes and packet corruption.  Changed pppstart
65 * so that it doesn't just give up with a "collision" if the whole
66 * packet doesn't fit in the output ring buffer.
67 *
68 * Added priority queueing for interactive IP packets, following
69 * the model of if_sl.c, plus hooks for bpf.
70 * Paul Mackerras (paulus@cs.anu.edu.au).
71 */
72
73/* $Id$ */
74/* from if_sl.c,v 1.11 84/10/04 12:54:47 rick Exp */
75/* from NetBSD: if_ppp.c,v 1.15.2.2 1994/07/28 05:17:58 cgd Exp */
76
77#include "ppp.h"
78#if NPPP > 0
79
80#include <sys/param.h>
81#include <sys/systm.h>
82#include <sys/proc.h>
83#include <sys/mbuf.h>
84#include <sys/socket.h>
85#include <sys/ioctl.h>
86#include <sys/file.h>
87#include <sys/kernel.h>
88
89#include <net/if.h>
90#include <net/if_types.h>
91
92#ifdef VJC
93#include <netinet/in.h>
94#include <netinet/in_systm.h>
95#include <netinet/ip.h>
96#include <net/pppcompress.h>
97#endif
98
99#include <rtems.h>
100#include <rtems/libio.h>
101#include <sys/ttycom.h>
102#include <termios.h>
103#include <rtems/termiostypes.h>
104
105#ifdef PPP_FILTER
106#include <net/bpf.h>
107#endif
108#include <net/ppp_defs.h>
109#include <net/if_ppp.h>
110#include <net/if_pppvar.h>
111
112
113void    pppasyncattach __P((void));
114int     pppopen __P((struct rtems_termios_tty *tty));
115int     pppclose __P((struct rtems_termios_tty *tty));
116int     pppread __P((struct rtems_termios_tty *tty, rtems_libio_rw_args_t *rw_args));
117int     pppwrite __P((struct rtems_termios_tty *tty, rtems_libio_rw_args_t *rw_args));
118int     ppptioctl __P((struct rtems_termios_tty *tty, rtems_libio_ioctl_args_t *args));
119int     pppinput __P((int c, struct rtems_termios_tty *tty));
120int     pppstart __P((struct rtems_termios_tty *tp));
121u_short pppfcs __P((u_short fcs, u_char *cp, int len));
122void    pppallocmbuf __P((struct ppp_softc *sc, struct mbuf **mp));
123
124static void pppasyncstart __P((struct ppp_softc *));
125static void pppasyncctlp __P((struct ppp_softc *));
126static void pppasyncrelinq __P((struct ppp_softc *));
127/*static void ppp_timeout __P((void *)); */
128/*static void pppdumpb __P((u_char *b, int l)); */
129/*static void ppplogchar __P((struct ppp_softc *, int)); */
130
131/*
132 * Some useful mbuf macros not in mbuf.h.
133 */
134#define M_IS_CLUSTER(m) ((m)->m_flags & M_EXT)
135
136#define M_DATASTART(m)  \
137        (M_IS_CLUSTER(m) ? (m)->m_ext.ext_buf : \
138            (m)->m_flags & M_PKTHDR ? (m)->m_pktdat : (m)->m_dat)
139
140#define M_DATASIZE(m)   \
141        (M_IS_CLUSTER(m) ? (m)->m_ext.ext_size : \
142            (m)->m_flags & M_PKTHDR ? MHLEN: MLEN)
143
144/*
145 * We steal two bits in the mbuf m_flags, to mark high-priority packets
146 * for output, and received packets following lost/corrupted packets.
147 */
148#define M_HIGHPRI       0x2000  /* output packet for sc_fastq */
149#define M_ERRMARK       0x4000  /* steal a bit in mbuf m_flags */
150
151/*
152 * Does c need to be escaped?
153 */
154#define ESCAPE_P(c)     (sc->sc_asyncmap[(c) >> 5] & (1 << ((c) & 0x1F)))
155
156/*
157 * Procedures for using an async tty interface for PPP.
158 */
159
160/* This is a FreeBSD-2.0 kernel. */
161#define CCOUNT(rb)      (((rb).Size+(rb).Head-(rb).Tail) % (rb).Size)
162#define FCOUNT(rb)      ((rb).Size-CCOUNT(rb)-1)
163#define PPP_LOWAT       100     /* Process more output when < LOWAT on queue */
164#define PPP_HIWAT       400     /* Don't start a new packet if HIWAT on que */
165
166/*
167 * Define the PPP line discipline.
168 */
169
170static struct linesw pppdisc = {
171        pppopen, pppclose, pppread, pppwrite,
172        pppinput, pppstart, ppptioctl, NULL
173};
174
175void
176pppasyncattach()
177{
178    linesw[PPPDISC] = pppdisc;
179}
180
181TEXT_SET(pseudo_set, pppasyncattach);
182
183/*
184 * Line specific open routine for async tty devices.
185 * Attach the given tty to the first available ppp unit.
186 * Called from device open routine or ttioctl.
187 */
188/* ARGSUSED */
189int
190pppopen(struct rtems_termios_tty *tty)
191{
192    int                        i;
193    register struct ppp_softc *sc;
194    struct mbuf *m = (struct mbuf *)0;
195    extern int termios_baud_to_number(int);
196
197    if (tty->t_line == PPPDISC) {
198        sc = (struct ppp_softc *)tty->t_sc;
199        if (sc != NULL && sc->sc_devp == (void *)tty) {
200            return (0);
201        }
202    }
203
204    if ((sc = pppalloc(1)) == NULL) {
205        return ENXIO;
206    }
207
208    if (sc->sc_relinq)
209        (*sc->sc_relinq)(sc);   /* get previous owner to relinquish the unit */
210
211    sc->sc_ilen = 0;
212    sc->sc_m = NULL;
213    bzero(sc->sc_asyncmap, sizeof(sc->sc_asyncmap));
214    sc->sc_asyncmap[0] = 0xffffffff;
215    sc->sc_asyncmap[3] = 0x60000000;
216    sc->sc_rasyncmap = 0;
217    sc->sc_devp = tty;
218    sc->sc_start = pppasyncstart;
219    sc->sc_ctlp = pppasyncctlp;
220    sc->sc_relinq = pppasyncrelinq;
221    sc->sc_outm = NULL;
222    sc->sc_outmc = NULL;
223   
224    /* preallocate mbufs for free queue */
225    rtems_bsdnet_semaphore_obtain();
226    for (i=0; i<NUM_MBUFQ; i++) {
227      pppallocmbuf(sc, &m);
228      if ( i == 0 ) {
229        /* use first mbuf for rx iterrupt handling */
230        sc->sc_m = m;
231      }
232      else {
233        /* enqueue mbuf for later use */
234        IF_ENQUEUE(&sc->sc_freeq, m);
235      }
236      m = (struct mbuf *)0;
237    }
238    rtems_bsdnet_semaphore_release();
239
240    /* initialize values */
241    sc->sc_if.if_flags |= IFF_RUNNING;
242    sc->sc_if.if_baudrate =
243        termios_baud_to_number(tty->termios.c_cflag & CBAUD);
244
245    tty->t_sc = (void *)sc;
246
247    return ( RTEMS_SUCCESSFUL );
248}
249
250/*
251 * Line specific close routine, called from device close routine
252 * and from ttioctl.
253 * Detach the tty from the ppp unit.
254 * Mimics part of ttyclose().
255 */
256int
257pppclose(struct rtems_termios_tty *tty)
258{
259    register struct ppp_softc *sc;
260
261    tty->t_line = 0;
262    sc = (struct ppp_softc *)tty->t_sc;
263    if (sc != NULL) {
264        tty->t_sc = NULL;
265        if (tty == (struct rtems_termios_tty *)sc->sc_devp) {
266            rtems_bsdnet_semaphore_obtain();
267            pppasyncrelinq(sc);
268            pppdealloc(sc);
269            rtems_bsdnet_semaphore_release();
270        }
271    }
272    return ( RTEMS_SUCCESSFUL );
273}
274
275/*
276 * Relinquish the interface unit to another device.
277 */
278static void
279pppasyncrelinq(sc)
280    struct ppp_softc *sc;
281{
282#ifdef XXX_XXX
283    if (sc->sc_outm) {
284        m_freem(sc->sc_outm);
285        sc->sc_outm = NULL;
286    }
287    if (sc->sc_m) {
288        m_freem(sc->sc_m);
289        sc->sc_m = NULL;
290    }
291    if (sc->sc_flags & SC_TIMEOUT) {
292        untimeout(ppp_timeout, (void *) sc);
293        sc->sc_flags &= ~SC_TIMEOUT;
294    }
295#endif
296}
297
298/*
299 * Line specific (tty) read routine.
300 */
301int
302pppread(struct rtems_termios_tty *tty, rtems_libio_rw_args_t *rw_args)
303{
304    rtems_status_code            status  = RTEMS_UNSATISFIED;
305    int                          count   = 0;
306    int                          maximum = rw_args->count;
307    char                        *buffer  = rw_args->buffer;
308    register struct ppp_softc   *sc      = (struct ppp_softc *)tty->t_sc;
309    struct mbuf                 *m;
310    struct mbuf                 *m0;
311    u_char                      *p;
312
313    if (sc == NULL) {
314        return 0;
315    }
316
317    /*
318     * Loop waiting for input, checking that nothing disasterous
319     * happens in the meantime.
320     */
321    if (tty != (struct rtems_termios_tty *)sc->sc_devp || tty->t_line != PPPDISC) {
322        return ( status );
323    }
324    if (sc->sc_inq.ifq_head == NULL) {
325      return ( status );
326    }
327
328    /* Get the packet from the input queue */
329    rtems_bsdnet_semaphore_obtain();
330    IF_DEQUEUE(&sc->sc_inq, m0);
331
332    /* loop over mbuf chain */
333    m = m0;
334    while (( m != NULL ) && ( m->m_len > 0 ) && ( count+m->m_len < maximum )) {
335      /* copy data into buffer */
336      p = mtod(m, u_char *);
337      memcpy(buffer, p, m->m_len);
338      memset(p, 0, m->m_len);
339      count  += m->m_len;
340      buffer += m->m_len;
341
342      /* increment loop index */
343      m = m->m_next;
344    }
345
346    /* free mbuf chain */
347    m_freem(m0);
348    rtems_bsdnet_semaphore_release();
349
350    /* update return values */
351    rw_args->bytes_moved = count;
352    if ( count >= 0 ) {
353      status = RTEMS_SUCCESSFUL;
354    }
355
356    /* check to see if queue is empty */
357    if (sc->sc_inq.ifq_head != NULL) {
358      /* queue is not empty - post another event to ourself */
359      rtems_event_send(sc->sc_pppdtask, PPPD_EVENT);
360    }
361
362    return ( status );
363}
364
365/*
366 * Line specific (tty) write routine.
367 */
368int
369pppwrite(struct rtems_termios_tty *tty, rtems_libio_rw_args_t *rw_args)
370{
371    struct sockaddr               dst;
372    int                           n;
373    int                           len;
374    int                           maximum    = rw_args->count;
375    char                         *out_buffer = rw_args->buffer;
376    register struct ppp_softc    *sc         = (struct ppp_softc *)tty->t_sc;
377    struct mbuf                  *m;
378    struct mbuf                  *m0;
379    struct mbuf                 **mp;
380
381    rtems_bsdnet_semaphore_obtain();
382    for (mp = &m0; maximum; mp = &m->m_next) {
383        MGET(m, M_WAIT, MT_DATA);
384        if ((*mp = m) == NULL) {
385            m_freem(m0);
386            return (ENOBUFS);
387        }
388        m->m_len = 0;
389        if (maximum >= MCLBYTES / 2) {
390            MCLGET(m, M_DONTWAIT);
391        }
392        len = M_TRAILINGSPACE(m);
393        if (len > maximum) {
394          memcpy(mtod(m, u_char *),out_buffer,maximum);
395          m->m_len = maximum;
396          maximum  = 0;
397        }
398        else {
399          memcpy(mtod(m, u_char *),out_buffer,len);
400          m->m_len    = len;
401          maximum    -= len;
402          out_buffer += len;
403        }
404    }
405
406    dst.sa_family = AF_UNSPEC;
407    bcopy(mtod(m0, u_char *), dst.sa_data, PPP_HDRLEN);
408    m0->m_data += PPP_HDRLEN;
409    m0->m_len  -= PPP_HDRLEN;
410
411    n = pppoutput(&sc->sc_if, m0, &dst, (struct rtentry *)0);
412    rtems_bsdnet_semaphore_release();
413
414    return ( n );
415}
416
417/*
418 * Line specific (tty) ioctl routine.
419 * This discipline requires that tty device drivers call
420 * the line specific l_ioctl routine from their ioctl routines.
421 */
422/* ARGSUSED */
423int
424ppptioctl(struct rtems_termios_tty *tty, rtems_libio_ioctl_args_t *args)
425{
426/*    int                 i;    */
427    int                 error = RTEMS_SUCCESSFUL;
428    int                 cmd   = args->command;
429    caddr_t             data  = args->buffer;
430    struct ppp_softc   *sc    = tty->t_sc;
431
432    switch (cmd) {
433    case RTEMS_IO_GET_ATTRIBUTES:
434    case RTEMS_IO_SET_ATTRIBUTES:
435    case RTEMS_IO_TCDRAIN:
436    case RTEMS_IO_SNDWAKEUP:
437    case RTEMS_IO_RCVWAKEUP:
438    case TIOCGETD:
439    case TIOCSETD:
440        error = rtems_termios_ioctl(args);
441        break;
442
443    case PPPIOCSASYNCMAP:
444        sc->sc_asyncmap[0] = *(u_int *)data;
445        break;
446
447    case PPPIOCGASYNCMAP:
448        *(u_int *)data = sc->sc_asyncmap[0];
449        break;
450
451    case PPPIOCSRASYNCMAP:
452        sc->sc_rasyncmap = *(u_int *)data;
453        break;
454
455    case PPPIOCGRASYNCMAP:
456        *(u_int *)data = sc->sc_rasyncmap;
457        break;
458
459    case PPPIOCSXASYNCMAP:
460        bcopy(data, sc->sc_asyncmap, sizeof(sc->sc_asyncmap));
461        sc->sc_asyncmap[1] = 0;             /* mustn't escape 0x20 - 0x3f */
462        sc->sc_asyncmap[2] &= ~0x40000000;  /* mustn't escape 0x5e */
463        sc->sc_asyncmap[3] |= 0x60000000;   /* must escape 0x7d, 0x7e */
464        break;
465
466    case PPPIOCGXASYNCMAP:
467        bcopy(sc->sc_asyncmap, data, sizeof(sc->sc_asyncmap));
468        break;
469
470    default:
471        rtems_bsdnet_semaphore_obtain();
472        error = pppioctl(sc, cmd, data, 0, NULL);
473        rtems_bsdnet_semaphore_release();
474    }
475    return error;
476}
477
478/*
479 * FCS lookup table as calculated by genfcstab.
480 */
481static u_short fcstab[256] = {
482        0x0000, 0x1189, 0x2312, 0x329b, 0x4624, 0x57ad, 0x6536, 0x74bf,
483        0x8c48, 0x9dc1, 0xaf5a, 0xbed3, 0xca6c, 0xdbe5, 0xe97e, 0xf8f7,
484        0x1081, 0x0108, 0x3393, 0x221a, 0x56a5, 0x472c, 0x75b7, 0x643e,
485        0x9cc9, 0x8d40, 0xbfdb, 0xae52, 0xdaed, 0xcb64, 0xf9ff, 0xe876,
486        0x2102, 0x308b, 0x0210, 0x1399, 0x6726, 0x76af, 0x4434, 0x55bd,
487        0xad4a, 0xbcc3, 0x8e58, 0x9fd1, 0xeb6e, 0xfae7, 0xc87c, 0xd9f5,
488        0x3183, 0x200a, 0x1291, 0x0318, 0x77a7, 0x662e, 0x54b5, 0x453c,
489        0xbdcb, 0xac42, 0x9ed9, 0x8f50, 0xfbef, 0xea66, 0xd8fd, 0xc974,
490        0x4204, 0x538d, 0x6116, 0x709f, 0x0420, 0x15a9, 0x2732, 0x36bb,
491        0xce4c, 0xdfc5, 0xed5e, 0xfcd7, 0x8868, 0x99e1, 0xab7a, 0xbaf3,
492        0x5285, 0x430c, 0x7197, 0x601e, 0x14a1, 0x0528, 0x37b3, 0x263a,
493        0xdecd, 0xcf44, 0xfddf, 0xec56, 0x98e9, 0x8960, 0xbbfb, 0xaa72,
494        0x6306, 0x728f, 0x4014, 0x519d, 0x2522, 0x34ab, 0x0630, 0x17b9,
495        0xef4e, 0xfec7, 0xcc5c, 0xddd5, 0xa96a, 0xb8e3, 0x8a78, 0x9bf1,
496        0x7387, 0x620e, 0x5095, 0x411c, 0x35a3, 0x242a, 0x16b1, 0x0738,
497        0xffcf, 0xee46, 0xdcdd, 0xcd54, 0xb9eb, 0xa862, 0x9af9, 0x8b70,
498        0x8408, 0x9581, 0xa71a, 0xb693, 0xc22c, 0xd3a5, 0xe13e, 0xf0b7,
499        0x0840, 0x19c9, 0x2b52, 0x3adb, 0x4e64, 0x5fed, 0x6d76, 0x7cff,
500        0x9489, 0x8500, 0xb79b, 0xa612, 0xd2ad, 0xc324, 0xf1bf, 0xe036,
501        0x18c1, 0x0948, 0x3bd3, 0x2a5a, 0x5ee5, 0x4f6c, 0x7df7, 0x6c7e,
502        0xa50a, 0xb483, 0x8618, 0x9791, 0xe32e, 0xf2a7, 0xc03c, 0xd1b5,
503        0x2942, 0x38cb, 0x0a50, 0x1bd9, 0x6f66, 0x7eef, 0x4c74, 0x5dfd,
504        0xb58b, 0xa402, 0x9699, 0x8710, 0xf3af, 0xe226, 0xd0bd, 0xc134,
505        0x39c3, 0x284a, 0x1ad1, 0x0b58, 0x7fe7, 0x6e6e, 0x5cf5, 0x4d7c,
506        0xc60c, 0xd785, 0xe51e, 0xf497, 0x8028, 0x91a1, 0xa33a, 0xb2b3,
507        0x4a44, 0x5bcd, 0x6956, 0x78df, 0x0c60, 0x1de9, 0x2f72, 0x3efb,
508        0xd68d, 0xc704, 0xf59f, 0xe416, 0x90a9, 0x8120, 0xb3bb, 0xa232,
509        0x5ac5, 0x4b4c, 0x79d7, 0x685e, 0x1ce1, 0x0d68, 0x3ff3, 0x2e7a,
510        0xe70e, 0xf687, 0xc41c, 0xd595, 0xa12a, 0xb0a3, 0x8238, 0x93b1,
511        0x6b46, 0x7acf, 0x4854, 0x59dd, 0x2d62, 0x3ceb, 0x0e70, 0x1ff9,
512        0xf78f, 0xe606, 0xd49d, 0xc514, 0xb1ab, 0xa022, 0x92b9, 0x8330,
513        0x7bc7, 0x6a4e, 0x58d5, 0x495c, 0x3de3, 0x2c6a, 0x1ef1, 0x0f78
514};
515
516/*
517 * Calculate a new FCS given the current FCS and the new data.
518 */
519u_short
520pppfcs(u_short fcs, u_char *cp, int len)
521{
522    while (len--)
523        fcs = PPP_FCS(fcs, *cp++);
524    return (fcs);
525}
526
527/*
528 * This gets called at splsoftnet from if_ppp.c at various times
529 * when there is data ready to be sent.
530 */
531void pppasyncstart(struct ppp_softc *sc)
532{
533  /* check to see if output is not active */
534  if ( sc->sc_outflag == 0 ) {
535    /* mark active and post tx event to daemon */
536    sc->sc_outflag |= SC_TX_PENDING;
537    rtems_event_send(sc->sc_txtask, TX_PACKET);
538  }
539}
540
541/*
542 * This gets called when a received packet is placed on
543 * the inq, at splsoftnet.
544 */
545static void
546pppasyncctlp(sc)
547    struct ppp_softc *sc;
548{
549  /* check to see if task id was set */
550  if ( sc->sc_pppdtask != 0 ) {
551    /* post event to daemon */
552    rtems_event_send(sc->sc_pppdtask, PPPD_EVENT);
553  }
554}
555
556/*
557 * Start output on async tty interface.  If the transmit queue
558 * has drained sufficiently, arrange for pppasyncstart to be
559 * called later at splsoftnet.
560 * Called at spltty or higher.
561 */
562int
563pppstart(struct rtems_termios_tty *tp)
564{
565  char                c;
566  char               *sendBegin;
567  char                cFrame  = (char         )PPP_FLAG;
568  u_char              ioffset = (u_char       )0;
569  struct mbuf        *m       = (struct mbuf *)0;
570  struct ppp_softc   *sc      = tp->t_sc;
571
572  /* ensure input is valid and we are busy */
573  if (( sc != NULL ) && ( sc->sc_outflag & SC_TX_BUSY )) {
574    /* check to see if we need to get the next buffer */
575    if ( sc->sc_outoff >= sc->sc_outlen ) {
576      /* loop to get next non-zero length buffer */
577      if ( sc->sc_outmc != NULL ) {
578        m = sc->sc_outmc->m_next;
579      }
580
581      /* check for next mbuf in chain */
582      if ( m != NULL ) {
583        /* update values to use this mbuf */
584        sc->sc_outmc  = m;
585        sc->sc_outbuf = mtod(m, u_char *);
586        sc->sc_outlen = m->m_len;
587        sc->sc_outoff = (short)0;
588      }
589      else if ( (sc->sc_outflag & SC_TX_FCS) == 0 ) {
590        /* setup to use FCS buffer */
591        sc->sc_outflag |= SC_TX_FCS;
592        sc->sc_outbuf   = sc->sc_outfcsbuf;
593        sc->sc_outlen   = sc->sc_outfcslen;
594        sc->sc_outoff   = (short)0;
595      }
596      else {
597        /* done with this packet */
598        sc->sc_outflag &= ~SC_TX_BUSY;
599        (*tp->device.write)(tp->minor, &cFrame, 1);
600        rtems_event_send(sc->sc_txtask, TX_TRANSMIT);
601      }
602    }
603
604    /* check to see if there is some data to write out */
605    if ( sc->sc_outoff < sc->sc_outlen ) {
606      /* check to see if character needs to be escaped */
607      c = sc->sc_outbuf[sc->sc_outoff];
608      if ( ESCAPE_P(c) ) {
609        if ( sc->sc_outflag & SC_TX_ESCAPE ) {
610          /* last sent character was the escape character */
611          c = c ^ PPP_TRANS;
612
613          /* clear the escape flag and increment the offset */
614          sc->sc_outflag &= ~SC_TX_ESCAPE;
615          ioffset++;
616        }
617        else {
618          /* need to send the escape character */
619          c = PPP_ESCAPE;
620
621          /* set the escape flag */
622          sc->sc_outflag |= SC_TX_ESCAPE;
623        }
624        sendBegin = &c;
625      }
626      else {
627        /* escape not needed - increment the offset as much as possible */
628        while ((!ESCAPE_P(c)) && ((sc->sc_outoff + ioffset) < sc->sc_outlen)) {
629            ioffset++;
630            c = sc->sc_outbuf[sc->sc_outoff + ioffset];
631        }
632        sendBegin = &sc->sc_outbuf[sc->sc_outoff];
633      }
634
635      /* write out the character(s) and update the stats */
636      (*tp->device.write)(tp->minor, sendBegin, (ioffset > 0) ? ioffset : 1);
637      sc->sc_stats.ppp_obytes += (ioffset > 0) ? ioffset : 1;
638      sc->sc_outoff += ioffset;
639    }
640  }
641
642  return ( 0 );
643}
644
645#ifdef XXX_XXX
646/*
647 * Timeout routine - try to start some more output.
648 */
649static void
650ppp_timeout(x)
651    void *x;
652{
653    struct rtems_termios_tty *tty = (struct rtems_termios_tty *)x;
654    struct ppp_softc         *sc  = tty->t_sc;
655/*    struct rtems_termios_tty *tp  = (struct rtems_termios_tty *)sc->sc_devp; */
656
657    sc->sc_flags &= ~SC_TIMEOUT;
658/*    pppstart(tp); */
659}
660#endif
661
662/*
663 * Allocate enough mbuf to handle current MRU.
664 */
665#ifdef XXX_XXX
666static void
667pppgetm(sc)
668    register struct ppp_softc *sc;
669{
670    struct mbuf *m, **mp;
671    int len;
672
673    mp = &sc->sc_m;
674    for (len = sc->sc_mru + PPP_HDRLEN + PPP_FCSLEN; len > 0; ){
675        if ((m = *mp) == NULL) {
676            MGETHDR(m, M_DONTWAIT, MT_DATA);
677            if (m == NULL)
678                break;
679            *mp = m;
680            MCLGET(m, M_DONTWAIT);
681        }
682        len -= M_DATASIZE(m);
683        mp = &m->m_next;
684    }
685}
686#endif
687
688void
689pppallocmbuf(struct ppp_softc *sc, struct mbuf **mp)
690{
691  int            ilen;
692  struct mbuf   *m;
693
694  /* loop over length value */
695  ilen = sc->sc_mru + PPP_HDRLEN + PPP_FCSLEN;
696  while ( ilen > 0 ) {
697    /* see if this is end of the chain */
698    m = *mp;
699    if ( m == NULL ) {
700      /* get mbuf header */
701      MGETHDR(m, M_DONTWAIT, MT_DATA);
702      if ( m == NULL ) {
703        /* error - set condition to break out */
704        printf("pppallocmbuf: MGETHDR failed\n");
705        break;
706      }
707      MCLGET(m, M_DONTWAIT);
708      m->m_next = NULL;
709      *mp = m;
710    }
711
712    /* update loop variables */
713    mp    = &m->m_next;
714    ilen -= M_DATASIZE(m);
715  }
716}
717
718/*
719 * tty interface receiver interrupt.
720 */
721static unsigned paritytab[8] = {
722    0x96696996, 0x69969669, 0x69969669, 0x96696996,
723    0x69969669, 0x96696996, 0x96696996, 0x69969669
724};
725
726int
727pppinput(int c, struct rtems_termios_tty *tp)
728{
729    register struct ppp_softc *sc = tp->t_sc;
730    struct mbuf *m;
731    int ilen;
732
733    if (sc == NULL || tp != (struct rtems_termios_tty *)sc->sc_devp)
734        return 0;
735    if (sc->sc_m == NULL) {
736        rtems_event_send(sc->sc_rxtask, RX_EMPTY);
737        IF_DEQUEUE(&sc->sc_freeq, sc->sc_m);
738        if ( sc->sc_m == NULL ) {
739          return 0;
740        }
741    }
742
743    ++sc->sc_stats.ppp_ibytes;
744
745    c &= 0xff;
746    if (c & 0x80)
747        sc->sc_flags |= SC_RCV_B7_1;
748    else
749        sc->sc_flags |= SC_RCV_B7_0;
750    if (paritytab[c >> 5] & (1 << (c & 0x1F)))
751        sc->sc_flags |= SC_RCV_ODDP;
752    else
753        sc->sc_flags |= SC_RCV_EVNP;
754
755    if (c == PPP_FLAG) {
756        ilen = sc->sc_ilen;
757        sc->sc_ilen = 0;
758
759        /*
760         * If SC_ESCAPED is set, then we've seen the packet
761         * abort sequence "}~".
762         */
763        if (sc->sc_flags & (SC_FLUSH | SC_ESCAPED)
764            || (ilen > 0 && sc->sc_fcs != PPP_GOODFCS)) {
765            sc->sc_flags |= SC_PKTLOST; /* note the dropped packet */
766            if ((sc->sc_flags & (SC_FLUSH | SC_ESCAPED)) == 0){
767                /* bad fcs error */
768                sc->sc_if.if_ierrors++;
769                sc->sc_stats.ppp_ierrors++;
770            } else
771                sc->sc_flags &= ~(SC_FLUSH | SC_ESCAPED);
772            return 0;
773        }
774
775        if (ilen < PPP_HDRLEN + PPP_FCSLEN) {
776            if (ilen) {
777                /* too short error */
778                sc->sc_if.if_ierrors++;
779                sc->sc_stats.ppp_ierrors++;
780                sc->sc_flags |= SC_PKTLOST;
781            }
782            return 0;
783        }
784
785        /* Remove FCS trailer.  Somewhat painful... */
786        ilen -= 2;
787        if (--sc->sc_mc->m_len == 0) {
788            for (m = sc->sc_m; m->m_next != sc->sc_mc; m = m->m_next);
789            sc->sc_mc = m;
790        }
791        sc->sc_mc->m_len--;
792
793        /* excise this mbuf chain - place on raw queue */
794        m = sc->sc_m;
795        if ( sc->sc_flags & SC_PKTLOST ) {
796          m->m_flags   |= M_ERRMARK;
797          sc->sc_flags &= ~SC_PKTLOST;
798        }
799        IF_ENQUEUE(&sc->sc_rawq, m);
800
801        /* setup next mbuf chain */
802        IF_DEQUEUE(&sc->sc_freeq, sc->sc_m);
803
804        /* send rx packet event */
805        rtems_event_send(sc->sc_rxtask, RX_PACKET);
806        return 0;
807    }
808
809    if (c < 0x20 && (sc->sc_rasyncmap & (1 << c)))
810        return 0;
811
812    if (sc->sc_flags & SC_ESCAPED) {
813        sc->sc_flags &= ~SC_ESCAPED;
814        c ^= PPP_TRANS;
815    } else if (c == PPP_ESCAPE) {
816        sc->sc_flags |= SC_ESCAPED;
817        return 0;
818    }
819
820    /*
821     * Initialize buffer on first octet received.
822     * First octet could be address or protocol (when compressing
823     * address/control).
824     * Second octet is control.
825     * Third octet is first or second (when compressing protocol)
826     * octet of protocol.
827     * Fourth octet is second octet of protocol.
828     */
829    if (sc->sc_ilen == 0) {
830        m = sc->sc_m;
831        m->m_len = 0;
832        m->m_data = M_DATASTART(sc->sc_m);
833        sc->sc_mc = m;
834        sc->sc_mp = mtod(m, char *);
835        sc->sc_fcs = PPP_INITFCS;
836        if (c != PPP_ALLSTATIONS) {
837            if (sc->sc_flags & SC_REJ_COMP_AC) {
838                /* garbage received error */
839                goto flush;
840            }
841            *sc->sc_mp++ = PPP_ALLSTATIONS;
842            *sc->sc_mp++ = PPP_UI;
843            sc->sc_ilen += 2;
844            m->m_len += 2;
845        }
846    }
847    if (sc->sc_ilen == 1 && c != PPP_UI) {
848        /* missing UI error */
849        goto flush;
850    }
851    if (sc->sc_ilen == 2 && (c & 1) == 1) {
852        /* a compressed protocol */
853        *sc->sc_mp++ = 0;
854        sc->sc_ilen++;
855        sc->sc_mc->m_len++;
856    }
857    if (sc->sc_ilen == 3 && (c & 1) == 0) {
858        /* bad protocol error */
859        goto flush;
860    }
861
862    /* packet beyond configured mru? */
863    if (++sc->sc_ilen > sc->sc_mru + PPP_HDRLEN + PPP_FCSLEN) {
864        /* packet too big error */
865        goto flush;
866    }
867
868    /* is this mbuf full? */
869    m = sc->sc_mc;
870    if (M_TRAILINGSPACE(m) <= 0) {
871        if (m->m_next == NULL) {
872          /* get next available mbuf for the chain */
873          IF_DEQUEUE(&sc->sc_freeq, m->m_next);
874          if (m->m_next == NULL) {
875            /* too few mbufs */
876            goto flush;
877          }
878          else {
879            /* send rx mbuf event */
880            rtems_event_send(sc->sc_rxtask, RX_MBUF);
881          }
882        }
883        sc->sc_mc = m = m->m_next;
884        m->m_len  = 0;
885        m->m_next = 0;
886        m->m_data = M_DATASTART(m);
887        sc->sc_mp = mtod(m, char *);
888    }
889
890    ++m->m_len;
891    *sc->sc_mp++ = c;
892    sc->sc_fcs = PPP_FCS(sc->sc_fcs, c);
893    return 0;
894
895 flush:
896    if (!(sc->sc_flags & SC_FLUSH)) {
897        sc->sc_if.if_ierrors++;
898        sc->sc_stats.ppp_ierrors++;
899        sc->sc_flags |= SC_FLUSH;
900    }
901    return 0;
902}
903
904#ifdef XXX_XXX
905#define MAX_DUMP_BYTES  128
906
907static void
908ppplogchar(sc, c)
909    struct ppp_softc *sc;
910    int c;
911{
912    if (c >= 0)
913        sc->sc_rawin[sc->sc_rawin_count++] = c;
914    if (sc->sc_rawin_count >= sizeof(sc->sc_rawin)
915        || (c < 0 && sc->sc_rawin_count > 0)) {
916        printf("ppp%d input: ", sc->sc_if.if_unit);
917        pppdumpb(sc->sc_rawin, sc->sc_rawin_count);
918        sc->sc_rawin_count = 0;
919    }
920}
921
922static void
923pppdumpb(b, l)
924    u_char *b;
925    int l;
926{
927    char buf[3*MAX_DUMP_BYTES+4];
928    char *bp = buf;
929    static char digits[] = "0123456789abcdef";
930
931    while (l--) {
932        if (bp >= buf + sizeof(buf) - 3) {
933            *bp++ = '>';
934            break;
935        }
936        *bp++ = digits[*b >> 4]; /* convert byte to ascii hex */
937        *bp++ = digits[*b++ & 0xf];
938        *bp++ = ' ';
939    }
940
941    *bp = 0;
942    printf("%s\n", buf);
943}
944#endif
945
946#endif  /* NPPP > 0 */
Note: See TracBrowser for help on using the repository browser.