source: rtems/cpukit/libnetworking/net/ppp_tty.c @ 47a8868

4.104.114.84.95
Last change on this file since 47a8868 was 47a8868, checked in by Ralf Corsepius <ralf.corsepius@…>, on 03/28/07 at 02:19:04

Use net/slcompress.h instead of net/pppcompress.h.

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