source: rtems/cpukit/libnetworking/net/if_ppp.c @ a02cdc8

4.104.115
Last change on this file since a02cdc8 was a02cdc8, checked in by Ralf Corsepius <ralf.corsepius@…>, on 02/04/09 at 15:47:09

Eliminate pppintr (Unused).

  • Property mode set to 100644
File size: 43.3 KB
Line 
1/*
2 * if_ppp.c - Point-to-Point Protocol (PPP) Asynchronous driver.
3 */
4
5/*-
6 * Copyright (c) 1989 Carnegie Mellon University.
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms are permitted
10 * provided that the above copyright notice and this paragraph are
11 * duplicated in all such forms and that any documentation,
12 * advertising materials, and other materials related to such
13 * distribution and use acknowledge that the software was developed
14 * by Carnegie Mellon University.  The name of the
15 * University may not be used to endorse or promote products derived
16 * from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
19 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20 *
21 * Drew D. Perkins
22 * Carnegie Mellon University
23 * 4910 Forbes Ave.
24 * Pittsburgh, PA 15213
25 * (412) 268-8576
26 * ddp@andrew.cmu.edu
27 *
28 * Based on:
29 *      @(#)if_sl.c     7.6.1.2 (Berkeley) 2/15/89
30 *
31 * Copyright (c) 1987 Regents of the University of California.
32 * All rights reserved.
33 *
34 * Redistribution and use in source and binary forms are permitted
35 * provided that the above copyright notice and this paragraph are
36 * duplicated in all such forms and that any documentation,
37 * advertising materials, and other materials related to such
38 * distribution and use acknowledge that the software was developed
39 * by the University of California, Berkeley.  The name of the
40 * University may not be used to endorse or promote products derived
41 * from this software without specific prior written permission.
42 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
43 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
44 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
45 *
46 * Serial Line interface
47 *
48 * Rick Adams
49 * Center for Seismic Studies
50 * 1300 N 17th Street, Suite 1450
51 * Arlington, Virginia 22209
52 * (703)276-7900
53 * rick@seismo.ARPA
54 * seismo!rick
55 *
56 * Pounded on heavily by Chris Torek (chris@mimsy.umd.edu, umcp-cs!chris).
57 * Converted to 4.3BSD Beta by Chris Torek.
58 * Other changes made at Berkeley, based in part on code by Kirk Smith.
59 *
60 * Converted to 4.3BSD+ 386BSD by Brad Parker (brad@cayman.com)
61 * Added VJ tcp header compression; more unified ioctls
62 *
63 * Extensively modified by Paul Mackerras (paulus@cs.anu.edu.au).
64 * Cleaned up a lot of the mbuf-related code to fix bugs that
65 * caused system crashes and packet corruption.  Changed pppstart
66 * so that it doesn't just give up with a collision if the whole
67 * packet doesn't fit in the output ring buffer.
68 *
69 * Added priority queueing for interactive IP packets, following
70 * the model of if_sl.c, plus hooks for bpf.
71 * Paul Mackerras (paulus@cs.anu.edu.au).
72 */
73
74/* $FreeBSD: src/sys/net/if_ppp.c,v 1.109 2005/10/12 19:52:16 thompsa Exp $ */
75/* from if_sl.c,v 1.11 84/10/04 12:54:47 rick Exp */
76/* from NetBSD: if_ppp.c,v 1.15.2.2 1994/07/28 05:17:58 cgd Exp */
77
78/* $Id$ */
79
80#include "opt_inet.h"
81#include "opt_ipx.h"
82#include "opt_mac.h"
83#include "opt_ppp.h"
84
85#if NPPP > 0
86
87#include <termios.h>
88#include <rtems/termiostypes.h>
89#include <rtems/rtems_bsdnet.h>
90#include <sys/param.h>
91#include <sys/systm.h>
92#include <sys/proc.h>
93#include <sys/mbuf.h>
94#include <sys/socket.h>
95#include <sys/ioctl.h>
96#include <sys/kernel.h>
97#include <sys/time.h>
98#include <sys/malloc.h>
99
100#include <net/if.h>
101#include <net/if_types.h>
102#include <net/netisr.h>
103#include <net/route.h>
104#ifdef PPP_FILTER
105#include <net/bpf.h>
106#endif
107
108#if INET
109#include <netinet/in.h>
110#include <netinet/in_systm.h>
111#include <netinet/in_var.h>
112#include <netinet/ip.h>
113#endif
114
115#if NBPFILTER > 0
116#include <net/bpf.h>
117#endif
118
119#ifdef VJC
120#include <net/slcompress.h>
121#endif
122
123#include <net/ppp_defs.h>
124#include <net/if_ppp.h>
125#include <net/if_pppvar.h>
126#include <machine/cpu.h>
127
128#define splsoftnet      splnet
129
130#ifdef PPP_COMPRESS
131#define PACKETPTR       struct mbuf *
132#include <net/ppp-comp.h>
133#endif
134
135static struct   ppp_softc ppp_softc[NPPP];
136
137static int      pppsioctl(struct ifnet *ifp, ioctl_command_t cmd, caddr_t data);
138static void     ppp_requeue(struct ppp_softc *);
139#ifdef PPP_COMPRESS
140static void     ppp_ccp(struct ppp_softc *, struct mbuf *m, int rcvd);
141static void     ppp_ccp_closed(struct ppp_softc *);
142#endif
143static struct mbuf *ppp_inproc(struct ppp_softc *, struct mbuf *);
144static void     pppdumpm(struct mbuf *m0);
145
146/*
147 * Some useful mbuf macros not in mbuf.h.
148 */
149#define M_IS_CLUSTER(m) ((m)->m_flags & M_EXT)
150
151#define M_DATASTART(m)  \
152        (M_IS_CLUSTER(m) ? (m)->m_ext.ext_buf : \
153            (m)->m_flags & M_PKTHDR ? (m)->m_pktdat : (m)->m_dat)
154
155#define M_DATASIZE(m)   \
156        (M_IS_CLUSTER(m) ? (m)->m_ext.ext_size : \
157            (m)->m_flags & M_PKTHDR ? MHLEN: MLEN)
158
159/*
160 * We steal two bits in the mbuf m_flags, to mark high-priority packets
161 * for output, and received packets following lost/corrupted packets.
162 */
163#define M_HIGHPRI       0x2000  /* output packet for sc_fastq */
164#define M_ERRMARK       0x4000  /* steal a bit in mbuf m_flags */
165
166
167#ifdef PPP_COMPRESS
168/*
169 * List of compressors we know about.
170 * We leave some space so maybe we can modload compressors.
171 */
172
173extern struct compressor ppp_bsd_compress;
174extern struct compressor ppp_deflate, ppp_deflate_draft;
175
176struct compressor *ppp_compressors[8] = {
177#if DO_BSD_COMPRESS
178    &ppp_bsd_compress,
179#endif
180#if DO_DEFLATE
181    &ppp_deflate,
182    &ppp_deflate_draft,
183#endif
184    NULL
185};
186#endif /* PPP_COMPRESS */
187
188extern struct ifqueue    ipintrq;
189static struct timeval    ppp_time;
190
191#ifndef __rtems__
192TEXT_SET(pseudo_set, ppp_rxdaemon);
193#endif
194
195static rtems_task ppp_rxdaemon(rtems_task_argument arg)
196{
197  rtems_event_set             events;
198  rtems_interrupt_level       level;
199  struct ppp_softc           *sc = (struct ppp_softc *)arg;
200  struct mbuf                *mp = (struct mbuf      *)0;
201  struct mbuf                *m;
202
203  /* enter processing loop */
204  while ( 1 ) {
205    /* wait for event */
206    rtems_event_receive(RX_PACKET|RX_MBUF|RX_EMPTY,RTEMS_WAIT|RTEMS_EVENT_ANY,RTEMS_NO_TIMEOUT,&events);
207    if ( events & RX_EMPTY ) {
208      printf("RX: QUEUE is EMPTY\n");
209      events &= ~RX_EMPTY;
210    }
211
212    if ( events ) {
213      /* get the network semaphore */
214      rtems_bsdnet_semaphore_obtain();
215
216      /* check to see if new packet was received */
217      if ( events & RX_PACKET ) {
218        /* get received packet mbuf chain */
219        rtems_interrupt_disable(level);
220        IF_DEQUEUE(&sc->sc_rawq, m);
221        rtems_interrupt_enable(level);
222
223        /* ensure packet was retrieved */
224        if ( m != (struct mbuf *)0 ) {
225          /* process the received packet */
226          mp = ppp_inproc(sc, m);
227        }
228      }
229
230      /* allocate a new mbuf to replace one */
231      if ( mp == NULL ) {
232        pppallocmbuf(sc, &mp);
233      }
234
235      /* place mbuf on freeq */
236      rtems_interrupt_disable(level);
237      IF_ENQUEUE(&sc->sc_freeq, mp);
238      rtems_interrupt_enable(level);
239      mp = (struct mbuf *)0;
240
241      /* release the network semaphore */
242      rtems_bsdnet_semaphore_release();
243
244      /* check to see if queue is empty */
245      if ( sc->sc_rawq.ifq_head ) {
246        /* queue is not empty - post another event */
247        rtems_event_send(sc->sc_rxtask, RX_PACKET);
248      }
249    }
250  }
251}
252
253static rtems_task ppp_txdaemon(rtems_task_argument arg)
254{
255  rtems_event_set             events;
256  int                         iprocess = (int               )0;
257  struct ppp_softc           *sc       = (struct ppp_softc *)arg;
258  struct mbuf                *mp;
259  struct mbuf                *mf;
260  struct mbuf                *m;
261  struct rtems_termios_tty   *tp;
262 
263  int frag;
264
265  /* enter processing loop */
266  while ( 1 ) {
267    /* wait for event */
268    rtems_event_receive(TX_PACKET|TX_TRANSMIT,RTEMS_WAIT|RTEMS_EVENT_ANY,RTEMS_NO_TIMEOUT,&events);
269    if ( events & TX_TRANSMIT ) {
270      /* received event from interrupt handler - free current mbuf */
271      rtems_bsdnet_semaphore_obtain();
272
273      m_freem(sc->sc_outm);
274     
275      rtems_bsdnet_semaphore_release();
276
277      /* chain is done - clear the values */
278      sc->sc_outm  = (struct mbuf *)0;
279      sc->sc_outmc = (struct mbuf *)0;
280
281      /* now set flag to fake receive of TX_PACKET event */
282      /* this will check to see if we have any pending packets */
283      events |= TX_PACKET;
284    }
285
286    /* received event from pppasyncstart */
287    if ( events & TX_PACKET ) {
288      /* ensure we are not busy */
289      if ( sc->sc_outm == (struct mbuf *)0 ) {
290        /* try dequeuing a packet */
291        sc->sc_outm = ppp_dequeue(sc);
292        if ( sc->sc_outm == NULL ) {
293          /* clear output flags */
294          sc->sc_outflag      = 0;
295          sc->sc_if.if_flags &= ~IFF_OACTIVE;
296        }
297        else {
298          /* set flag to start process */
299          iprocess            = 1;
300          sc->sc_outflag      = SC_TX_BUSY;
301          sc->sc_if.if_flags |= IFF_OACTIVE;
302        }
303      }
304    }
305
306    /* check to see if there is any processing required */
307    if ( iprocess ) {
308      /* clear process flag */
309      iprocess = (int)0;
310      frag=0;
311
312      /* initialize output values */
313      sc->sc_outfcs    = PPP_INITFCS;
314      sc->sc_outbuf    = (u_char *)0;
315      sc->sc_outlen    = (short   )0;
316      sc->sc_outoff    = (short   )0;
317      sc->sc_outfcslen = (short   )0;
318
319/*        printf("Start Transmit Packet..\n"); */
320
321      /* loop over all mbufs in chain */
322      mf     = NULL;
323      mp     = NULL;
324      m      = sc->sc_outm;
325
326      sc->sc_outmc  = m;
327      sc->sc_outlen = m->m_len;
328      sc->sc_outbuf = mtod(m, u_char *);
329
330      while (( m != (struct mbuf *)0 ) && ( m->m_len > 0 )) {
331        frag++;
332
333        /* update the FCS value and then check next packet length */
334        if(m->m_len){
335         sc->sc_outfcs = pppfcs(sc->sc_outfcs, mtod(m, u_char *), m->m_len);
336        }
337
338        if(( m->m_next != NULL ) && ( m->m_next->m_len == 0 )) {
339                        if(mf){
340                                printf(" if_ppp.c : MBUF Error !!!!\n");
341                        }
342                        else{
343                mf = m->m_next;
344                        m->m_next = NULL;
345                        }
346                }
347
348#ifdef LALL_X
349        /* check next packet to see if it is empty */
350        while (( m->m_next != NULL ) && ( m->m_next->m_len == 0 )) {
351          /* next mbuf is zero length */
352          /* add empty mbuf to free chain */
353          if ( mp == NULL ) {
354            /* item is head of free list */
355            mf = m->m_next;
356            mp = mf;
357          }
358          else {
359            /* add item to end of the free list */
360            mp->m_next = m->m_next;
361            mp         = m->m_next;
362          }
363
364          /* remove empty item from process chain */
365          m->m_next  = m->m_next->m_next;
366          mp->m_next = NULL;
367        }
368#endif
369        /* move to next packet */
370        m = m->m_next;
371      }
372
373      /* ensure there is data to be sent out */
374      tp = (struct rtems_termios_tty *)sc->sc_devp;
375      if (( tp != NULL ) && ( sc->sc_outmc != (struct mbuf *)0 )) {
376        /* place FCS value into buffer */
377        sc->sc_outfcsbuf[sc->sc_outfcslen++] = ~sc->sc_outfcs & 0xff;
378        sc->sc_outfcsbuf[sc->sc_outfcslen++] = (~sc->sc_outfcs >> 8) & 0xff;
379        microtime(&sc->sc_if.if_lastchange);
380 
381        /* write out frame byte to start the transmission */
382                sc->sc_outchar = (u_char)PPP_FLAG;
383        (*tp->device.write)(tp->minor, (char *)&sc->sc_outchar, 1);
384      }
385
386      /* check to see if we need to free some empty mbufs */
387      if ( mf != (struct mbuf *)0 ) {
388        /* free empty mbufs */
389        rtems_bsdnet_semaphore_obtain();
390        m_freem(mf);
391        rtems_bsdnet_semaphore_release();
392      }
393    }
394  }
395}
396
397static void ppp_init(struct ppp_softc *sc)
398{
399  rtems_status_code   status;
400  uint32_t      priority = 100;
401
402  /* determine priority value */
403  if ( rtems_bsdnet_config.network_task_priority ) {
404    priority = rtems_bsdnet_config.network_task_priority;
405  }
406
407  /* check to see if we need to start up daemons */
408  if ( sc->sc_rxtask == 0 ) {
409    /* start rx daemon task */
410    status = rtems_task_create(rtems_build_name('R','x','P','0'+sc->sc_if.if_unit), priority, 2048,
411                               RTEMS_PREEMPT|RTEMS_NO_TIMESLICE|RTEMS_NO_ASR|RTEMS_INTERRUPT_LEVEL(0),
412                               RTEMS_NO_FLOATING_POINT|RTEMS_LOCAL,
413                               &sc->sc_rxtask);
414    if (status != RTEMS_SUCCESSFUL) {
415      rtems_fatal_error_occurred(status);
416    }
417    else {
418      status = rtems_task_start(sc->sc_rxtask, ppp_rxdaemon, (rtems_task_argument)sc);
419      if (status != RTEMS_SUCCESSFUL) {
420        rtems_fatal_error_occurred(status);
421      }
422    }
423
424    /* start tx daemon task */
425    status = rtems_task_create(rtems_build_name('T','x','P','0'+sc->sc_if.if_unit), priority, 2048,
426                               RTEMS_PREEMPT|RTEMS_NO_TIMESLICE|RTEMS_NO_ASR|RTEMS_INTERRUPT_LEVEL(0),
427                               RTEMS_NO_FLOATING_POINT|RTEMS_LOCAL,
428                               &sc->sc_txtask);
429    if (status != RTEMS_SUCCESSFUL) {
430      rtems_fatal_error_occurred(status);
431    }
432    else {
433      status = rtems_task_start(sc->sc_txtask, ppp_txdaemon, (rtems_task_argument)sc);
434      if (status != RTEMS_SUCCESSFUL) {
435        rtems_fatal_error_occurred(status);
436      }
437    }
438  }
439
440  /* mark driver running and output inactive */
441  /* ilya: IFF_RUNNING flag will be marked after the IPCP goes up */
442/*  sc->sc_if.if_flags |= IFF_RUNNING;  */
443}
444
445/*
446 * Called from boot code to establish ppp interfaces.
447 */
448int rtems_ppp_driver_attach(struct rtems_bsdnet_ifconfig *config, int attaching)
449{
450/*    int                 i = (int)0;   */
451    struct ppp_softc   *sc;
452    char               *name;
453    int                 number;
454   
455   
456    number = rtems_bsdnet_parse_driver_name (config, &name);
457   
458    if (!attaching || (number >= NPPP))
459        return 0;
460       
461    sc = &ppp_softc[number];
462   
463    if (sc->sc_if.if_name != NULL)
464        return 0;       /* interface is already attached */
465   
466/*    for (sc = ppp_softc; i < NPPP; sc++) {    */
467        sc->sc_if.if_name = name /*"ppp"*/;
468        sc->sc_if.if_unit = number /*i++*/;
469        sc->sc_if.if_mtu = PPP_MTU;
470        sc->sc_if.if_flags = IFF_POINTOPOINT | IFF_MULTICAST;
471        sc->sc_if.if_type = IFT_PPP;
472        sc->sc_if.if_hdrlen = PPP_HDRLEN;
473        sc->sc_if.if_ioctl = pppsioctl;
474        sc->sc_if.if_output = pppoutput;
475        sc->sc_if.if_snd.ifq_maxlen = IFQ_MAXLEN;
476        sc->sc_inq.ifq_maxlen = IFQ_MAXLEN;
477        sc->sc_fastq.ifq_maxlen = IFQ_MAXLEN;
478        sc->sc_rawq.ifq_maxlen = IFQ_MAXLEN;
479        sc->sc_freeq.ifq_maxlen = NUM_MBUFQ;
480
481        /* initialize and attach */
482        ppp_init(sc);
483        if_attach(&sc->sc_if);
484#if NBPFILTER > 0
485        bpfattach(&sc->sc_bpf, &sc->sc_if, DLT_PPP, PPP_HDRLEN);
486#endif
487/*    } */
488
489    return ( 1 );
490}
491
492/*
493 * Allocate a ppp interface unit and initialize it.
494 */
495struct ppp_softc *
496pppalloc(pid_t pid)
497{
498    int nppp, i;
499    struct ppp_softc *sc;
500
501    for (nppp = 0, sc = ppp_softc; nppp < NPPP; nppp++, sc++)
502        if (sc->sc_xfer == pid) {
503            sc->sc_xfer = 0;
504            return sc;
505        }
506    for (nppp = 0, sc = ppp_softc; nppp < NPPP; nppp++, sc++)
507        if (sc->sc_devp == NULL)
508            break;
509    if (nppp >= NPPP)
510        return NULL;
511
512    sc->sc_flags = 0;
513    sc->sc_mru = PPP_MRU;
514    sc->sc_relinq = NULL;
515    bzero((char *)&sc->sc_stats, sizeof(sc->sc_stats));
516#ifdef VJC
517    MALLOC(sc->sc_comp, struct vjcompress *, sizeof(struct vjcompress),
518           M_DEVBUF, M_NOWAIT);
519    if (sc->sc_comp)
520        sl_compress_init(sc->sc_comp, -1);
521#endif
522#ifdef PPP_COMPRESS
523    sc->sc_xc_state = NULL;
524    sc->sc_rc_state = NULL;
525#endif /* PPP_COMPRESS */
526    for (i = 0; i < NUM_NP; ++i)
527        sc->sc_npmode[i] = NPMODE_ERROR;
528    sc->sc_npqueue = NULL;
529    sc->sc_npqtail = &sc->sc_npqueue;
530    microtime(&ppp_time);
531    sc->sc_last_sent = sc->sc_last_recv = ppp_time.tv_sec;
532
533    return sc;
534}
535
536/*
537 * Deallocate a ppp unit.  Must be called at splsoftnet or higher.
538 */
539void
540pppdealloc(struct ppp_softc *sc)
541{
542    struct mbuf *m;
543    rtems_interrupt_level       level;
544
545    if_down(&sc->sc_if);
546    sc->sc_if.if_flags &= ~(IFF_UP|IFF_RUNNING);
547    sc->sc_devp = NULL;
548    sc->sc_xfer = 0;
549
550    rtems_interrupt_disable(level);
551    if ( sc->sc_m != NULL ) {
552        m_freem(sc->sc_m);
553        sc->sc_m = (struct mbuf *)0;
554    }
555    if ( sc->sc_outm != NULL ) {
556        m_freem(sc->sc_outm);
557        sc->sc_outm    = (struct mbuf *)0;
558        sc->sc_outmc   = (struct mbuf *)0;
559        sc->sc_outflag = 0;
560    }
561    do {
562      IF_DEQUEUE(&sc->sc_freeq, m);
563      if (m != NULL) {
564        m_freem(m);
565      }
566    } while ( m != NULL );
567    do {
568      IF_DEQUEUE(&sc->sc_rawq, m);
569      if (m != NULL) {
570        m_freem(m);
571      }
572    } while ( m != NULL );
573    rtems_interrupt_enable(level);
574
575    for (;;) {
576        IF_DEQUEUE(&sc->sc_inq, m);
577        if (m == NULL)
578            break;
579        m_freem(m);
580    }
581    for (;;) {
582        IF_DEQUEUE(&sc->sc_fastq, m);
583        if (m == NULL)
584            break;
585        m_freem(m);
586    }
587    while ((m = sc->sc_npqueue) != NULL) {
588        sc->sc_npqueue = m->m_nextpkt;
589        m_freem(m);
590    }
591#ifdef PPP_COMPRESS
592    ppp_ccp_closed(sc);
593    sc->sc_xc_state = NULL;
594    sc->sc_rc_state = NULL;
595#endif /* PPP_COMPRESS */
596#ifdef PPP_FILTER
597    if (sc->sc_pass_filt.bf_insns != 0) {
598        FREE(sc->sc_pass_filt.bf_insns, M_DEVBUF);
599        sc->sc_pass_filt.bf_insns = 0;
600        sc->sc_pass_filt.bf_len = 0;
601    }
602    if (sc->sc_active_filt.bf_insns != 0) {
603        FREE(sc->sc_active_filt.bf_insns, M_DEVBUF);
604        sc->sc_active_filt.bf_insns = 0;
605        sc->sc_active_filt.bf_len = 0;
606    }
607#endif /* PPP_FILTER */
608#ifdef VJC
609    if (sc->sc_comp != 0) {
610        FREE(sc->sc_comp, M_DEVBUF);
611        sc->sc_comp = 0;
612    }
613#endif
614}
615
616/*
617 * Ioctl routine for generic ppp devices.
618 */
619int
620pppioctl(struct ppp_softc *sc, ioctl_command_t cmd, caddr_t data,
621    int flag, struct proc *p)
622{
623    int s, flags, mru, npx, taskid;
624    struct npioctl *npi;
625    time_t t;
626#ifdef PPP_FILTER
627    int error;
628    struct bpf_program *bp, *nbp;
629    struct bpf_insn *newcode, *oldcode;
630    int newcodelen;
631#endif /* PPP_FILTER */
632#ifdef  PPP_COMPRESS
633    int nb;
634    struct ppp_option_data *odp;
635    struct compressor **cp;
636    u_char ccp_option[CCP_MAX_OPTION_LENGTH];
637#endif
638
639    switch (cmd) {
640    case FIONREAD:
641        *(int *)data = sc->sc_inq.ifq_len;
642        break;
643
644    case PPPIOCSTASK:
645        taskid = *(int *)data;
646        sc->sc_pppdtask = taskid;
647        break;
648
649    case PPPIOCGUNIT:
650        *(int *)data = sc->sc_if.if_unit;
651        break;
652
653    case PPPIOCGFLAGS:
654        *(u_int *)data = sc->sc_flags;
655        break;
656
657    case PPPIOCSFLAGS:
658        flags = *(int *)data & SC_MASK;
659        s = splsoftnet();
660#ifdef PPP_COMPRESS
661        if (sc->sc_flags & SC_CCP_OPEN && !(flags & SC_CCP_OPEN))
662            ppp_ccp_closed(sc);
663#endif
664        s = splimp();
665        sc->sc_flags = (sc->sc_flags & ~SC_MASK) | flags;
666        splx(s);
667        break;
668
669    case PPPIOCSMRU:
670        mru = *(int *)data;
671        if ( mru >= MCLBYTES ) {
672          /* error - currently only handle 1 culster sized MRU */
673          /* if we want to handle up to PPP_MAXMRU then we */
674          /*   need to reallocate all mbufs on the freeq */
675          /*   this can only be done with iterrupts disabled */
676          return ( -1 );
677        }
678        else if ( mru >= PPP_MRU ) {
679          /* update the size */
680          sc->sc_mru = mru;
681        }
682        break;
683
684    case PPPIOCGMRU:
685        *(int *)data = sc->sc_mru;
686        break;
687
688#ifdef VJC
689    case PPPIOCSMAXCID:
690        if (sc->sc_comp) {
691            s = splsoftnet();
692            sl_compress_init(sc->sc_comp, *(int *)data);
693            splx(s);
694        }
695        break;
696#endif
697
698    case PPPIOCXFERUNIT:
699        sc->sc_xfer = 0; /* Always root p->p_pid;*/
700        break;
701
702#ifdef PPP_COMPRESS
703    case PPPIOCSCOMPRESS:
704        odp = (struct ppp_option_data *) data;
705        nb = odp->length;
706        if (nb > sizeof(ccp_option))
707            nb = sizeof(ccp_option);
708        if ((error = copyin(odp->ptr, ccp_option, nb)) != 0)
709            return (error);
710        if (ccp_option[1] < 2)  /* preliminary check on the length byte */
711            return (EINVAL);
712        for (cp = ppp_compressors; *cp != NULL; ++cp)
713            if ((*cp)->compress_proto == ccp_option[0]) {
714                /*
715                 * Found a handler for the protocol - try to allocate
716                 * a compressor or decompressor.
717                 */
718                error = 0;
719                if (odp->transmit) {
720                    s = splsoftnet();
721                    if (sc->sc_xc_state != NULL)
722                        (*sc->sc_xcomp->comp_free)(sc->sc_xc_state);
723                    sc->sc_xcomp = *cp;
724                    sc->sc_xc_state = (*cp)->comp_alloc(ccp_option, nb);
725                    if (sc->sc_xc_state == NULL) {
726                        if (sc->sc_flags & SC_DEBUG)
727                            printf("ppp%d: comp_alloc failed\n",
728                               sc->sc_if.if_unit);
729                        error = ENOBUFS;
730                    }
731                    splimp();
732                    sc->sc_flags &= ~SC_COMP_RUN;
733                    splx(s);
734                } else {
735                    s = splsoftnet();
736                    if (sc->sc_rc_state != NULL)
737                        (*sc->sc_rcomp->decomp_free)(sc->sc_rc_state);
738                    sc->sc_rcomp = *cp;
739                    sc->sc_rc_state = (*cp)->decomp_alloc(ccp_option, nb);
740                    if (sc->sc_rc_state == NULL) {
741                        if (sc->sc_flags & SC_DEBUG)
742                            printf("ppp%d: decomp_alloc failed\n",
743                               sc->sc_if.if_unit);
744                        error = ENOBUFS;
745                    }
746                    splimp();
747                    sc->sc_flags &= ~SC_DECOMP_RUN;
748                    splx(s);
749                }
750                return (error);
751            }
752        if (sc->sc_flags & SC_DEBUG)
753            printf("ppp%d: no compressor for [%x %x %x], %x\n",
754                   sc->sc_if.if_unit, ccp_option[0], ccp_option[1],
755                   ccp_option[2], nb);
756        return (EINVAL);        /* no handler found */
757#endif /* PPP_COMPRESS */
758
759    case PPPIOCGNPMODE:
760    case PPPIOCSNPMODE:
761        npi = (struct npioctl *) data;
762        switch (npi->protocol) {
763        case PPP_IP:
764            npx = NP_IP;
765            break;
766        default:
767            return EINVAL;
768        }
769        if (cmd == PPPIOCGNPMODE) {
770            npi->mode = sc->sc_npmode[npx];
771        } else {
772            if (npi->mode != sc->sc_npmode[npx]) {
773                s = splsoftnet();
774                sc->sc_npmode[npx] = npi->mode;
775                if (npi->mode != NPMODE_QUEUE) {
776                    ppp_requeue(sc);
777                    (*sc->sc_start)(sc);
778                }
779                splx(s);
780            }
781        }
782        break;
783
784    case PPPIOCGIDLE:
785        s = splsoftnet();
786        microtime(&ppp_time);
787        t = ppp_time.tv_sec;
788        ((struct ppp_idle *)data)->xmit_idle = t - sc->sc_last_sent;
789        ((struct ppp_idle *)data)->recv_idle = t - sc->sc_last_recv;
790        splx(s);
791        break;
792
793#ifdef PPP_FILTER
794    case PPPIOCSPASS:
795    case PPPIOCSACTIVE:
796        nbp = (struct bpf_program *) data;
797        if ((unsigned) nbp->bf_len > BPF_MAXINSNS)
798            return EINVAL;
799        newcodelen = nbp->bf_len * sizeof(struct bpf_insn);
800        if (newcodelen != 0) {
801            MALLOC(newcode, struct bpf_insn *, newcodelen, M_DEVBUF, M_WAITOK);
802            if (newcode == 0) {
803                return EINVAL;          /* or sumpin */
804            }
805            if ((error = copyin((caddr_t)nbp->bf_insns, (caddr_t)newcode,
806                               newcodelen)) != 0) {
807                FREE(newcode, M_DEVBUF);
808                return error;
809            }
810            if (!bpf_validate(newcode, nbp->bf_len)) {
811                FREE(newcode, M_DEVBUF);
812                return EINVAL;
813            }
814        } else
815            newcode = 0;
816        bp = (cmd == PPPIOCSPASS)? &sc->sc_pass_filt: &sc->sc_active_filt;
817        oldcode = bp->bf_insns;
818        s = splimp();
819        bp->bf_len = nbp->bf_len;
820        bp->bf_insns = newcode;
821        splx(s);
822        if (oldcode != 0)
823            FREE(oldcode, M_DEVBUF);
824        break;
825#endif
826
827    default:
828        return (-1);
829    }
830    return (0);
831}
832
833/*
834 * Process an ioctl request to the ppp network interface.
835 */
836static int
837pppsioctl(struct ifnet *ifp, ioctl_command_t cmd, caddr_t data)
838{
839    /*struct proc *p = curproc;*/       /* XXX */
840    register struct ppp_softc *sc = &ppp_softc[ifp->if_unit];
841    register struct ifaddr *ifa = (struct ifaddr *)data;
842    register struct ifreq *ifr = (struct ifreq *)data;
843    struct ppp_stats *psp;
844#ifdef  PPP_COMPRESS
845    struct ppp_comp_stats *pcp;
846#endif
847    int s = splimp(), error = 0;
848
849    switch (cmd) {
850    case SIOCSIFFLAGS:
851        if ((ifp->if_flags & IFF_RUNNING) == 0)
852            ifp->if_flags &= ~IFF_UP;
853        break;
854
855    case SIOCSIFADDR:
856        if (ifa->ifa_addr->sa_family != AF_INET)
857            error = EAFNOSUPPORT;
858        break;
859
860    case SIOCSIFDSTADDR:
861        if (ifa->ifa_addr->sa_family != AF_INET)
862            error = EAFNOSUPPORT;
863        break;
864
865    case SIOCSIFMTU:
866        sc->sc_if.if_mtu = ifr->ifr_mtu;
867        break;
868
869    case SIOCGIFMTU:
870        ifr->ifr_mtu = sc->sc_if.if_mtu;
871        break;
872
873    case SIOCADDMULTI:
874    case SIOCDELMULTI:
875        if (ifr == 0) {
876            error = EAFNOSUPPORT;
877            break;
878        }
879        switch(ifr->ifr_addr.sa_family) {
880#ifdef INET
881        case AF_INET:
882            break;
883#endif
884        default:
885            error = EAFNOSUPPORT;
886            break;
887        }
888        break;
889
890    case SIO_RTEMS_SHOW_STATS:
891        printf("              MRU:%-8u",   sc->sc_mru);
892        printf("   Bytes received:%-8u",   sc->sc_stats.ppp_ibytes);
893        printf(" Packets received:%-8u",   sc->sc_stats.ppp_ipackets);
894        printf("   Receive errors:%-8u\n", sc->sc_stats.ppp_ierrors);
895        printf("       Bytes sent:%-8u",   sc->sc_stats.ppp_obytes);
896        printf("     Packets sent:%-8u",   sc->sc_stats.ppp_opackets);
897        printf("  Transmit errors:%-8u\n", sc->sc_stats.ppp_oerrors);
898        break;
899
900    case SIOCGPPPSTATS:
901        psp = &((struct ifpppstatsreq *) data)->stats;
902        bzero(psp, sizeof(*psp));
903        psp->p = sc->sc_stats;
904#if defined(VJC) && !defined(SL_NO_STATS)
905        if (sc->sc_comp) {
906            psp->vj.vjs_packets = sc->sc_comp->sls_packets;
907            psp->vj.vjs_compressed = sc->sc_comp->sls_compressed;
908            psp->vj.vjs_searches = sc->sc_comp->sls_searches;
909            psp->vj.vjs_misses = sc->sc_comp->sls_misses;
910            psp->vj.vjs_uncompressedin = sc->sc_comp->sls_uncompressedin;
911            psp->vj.vjs_compressedin = sc->sc_comp->sls_compressedin;
912            psp->vj.vjs_errorin = sc->sc_comp->sls_errorin;
913            psp->vj.vjs_tossed = sc->sc_comp->sls_tossed;
914        }
915#endif /* VJC */
916        break;
917
918#ifdef PPP_COMPRESS
919    case SIOCGPPPCSTATS:
920        pcp = &((struct ifpppcstatsreq *) data)->stats;
921        bzero(pcp, sizeof(*pcp));
922        if (sc->sc_xc_state != NULL)
923            (*sc->sc_xcomp->comp_stat)(sc->sc_xc_state, &pcp->c);
924        if (sc->sc_rc_state != NULL)
925            (*sc->sc_rcomp->decomp_stat)(sc->sc_rc_state, &pcp->d);
926        break;
927#endif /* PPP_COMPRESS */
928
929    default:
930        error = EINVAL;
931    }
932    splx(s);
933    return (error);
934}
935
936/*
937 * Queue a packet.  Start transmission if not active.
938 * Packet is placed in Information field of PPP frame.
939 */
940int
941pppoutput(struct ifnet *ifp, struct mbuf *m0, struct sockaddr *dst,
942    struct rtentry *rtp)
943{
944    register struct ppp_softc *sc = &ppp_softc[ifp->if_unit];
945    int protocol, address, control;
946    u_char *cp;
947    int s, error;
948    struct ip *ip;
949    struct ifqueue *ifq;
950    enum NPmode mode;
951    int len;
952    struct mbuf *m;
953
954    if (sc->sc_devp == NULL || (ifp->if_flags & IFF_RUNNING) == 0
955        || ((ifp->if_flags & IFF_UP) == 0 && dst->sa_family != AF_UNSPEC)) {
956        error = ENETDOWN;       /* sort of */
957        goto bad;
958    }
959
960    /*
961     * Compute PPP header.
962     */
963    m0->m_flags &= ~M_HIGHPRI;
964    switch (dst->sa_family) {
965#ifdef INET
966    case AF_INET:
967        address = PPP_ALLSTATIONS;
968        control = PPP_UI;
969        protocol = PPP_IP;
970        mode = sc->sc_npmode[NP_IP];
971
972        /*
973         * If this packet has the "low delay" bit set in the IP header,
974         * put it on the fastq instead.
975         */
976        ip = mtod(m0, struct ip *);
977        if (ip->ip_tos & IPTOS_LOWDELAY)
978            m0->m_flags |= M_HIGHPRI;
979        break;
980#endif
981    case AF_UNSPEC:
982        address = PPP_ADDRESS(dst->sa_data);
983        control = PPP_CONTROL(dst->sa_data);
984        protocol = PPP_PROTOCOL(dst->sa_data);
985        mode = NPMODE_PASS;
986        break;
987    default:
988        printf("ppp%d: af%d not supported\n", ifp->if_unit, dst->sa_family);
989        error = EAFNOSUPPORT;
990        goto bad;
991    }
992
993    /*
994     * Drop this packet, or return an error, if necessary.
995     */
996    if (mode == NPMODE_ERROR) {
997        error = ENETDOWN;
998        goto bad;
999    }
1000    if (mode == NPMODE_DROP) {
1001        error = 0;
1002        goto bad;
1003    }
1004
1005    /*
1006     * Add PPP header.  If no space in first mbuf, allocate another.
1007     * (This assumes M_LEADINGSPACE is always 0 for a cluster mbuf.)
1008     */
1009    if (M_LEADINGSPACE(m0) < PPP_HDRLEN) {
1010        m0 = m_prepend(m0, PPP_HDRLEN, M_DONTWAIT);
1011        if (m0 == 0) {
1012            error = ENOBUFS;
1013            goto bad;
1014        }
1015        m0->m_len = 0;
1016    } else
1017        m0->m_data -= PPP_HDRLEN;
1018
1019    cp = mtod(m0, u_char *);
1020    *cp++ = address;
1021    *cp++ = control;
1022    *cp++ = protocol >> 8;
1023    *cp++ = protocol & 0xff;
1024    m0->m_len += PPP_HDRLEN;
1025
1026    len = 0;
1027    for (m = m0; m != 0; m = m->m_next)
1028        len += m->m_len;
1029
1030    if (sc->sc_flags & SC_LOG_OUTPKT) {
1031        printf("ppp%d output: ", ifp->if_unit);
1032        pppdumpm(m0);
1033    }
1034
1035    if ((protocol & 0x8000) == 0) {
1036#ifdef PPP_FILTER
1037        /*
1038         * Apply the pass and active filters to the packet,
1039         * but only if it is a data packet.
1040         */
1041        *mtod(m0, u_char *) = 1;        /* indicates outbound */
1042        if (sc->sc_pass_filt.bf_insns != 0
1043            && bpf_filter(sc->sc_pass_filt.bf_insns, (u_char *) m0,
1044                          len, 0) == 0) {
1045            error = 0;          /* drop this packet */
1046            goto bad;
1047        }
1048
1049        /*
1050         * Update the time we sent the most recent packet.
1051         */
1052        if (sc->sc_active_filt.bf_insns == 0
1053            || bpf_filter(sc->sc_active_filt.bf_insns, (u_char *) m0, len, 0))
1054            sc->sc_last_sent = time.tv_sec;
1055
1056        *mtod(m0, u_char *) = address;
1057#else
1058        /*
1059         * Update the time we sent the most recent data packet.
1060         */
1061        microtime(&ppp_time);
1062        sc->sc_last_sent = ppp_time.tv_sec;
1063#endif /* PPP_FILTER */
1064    }
1065
1066#if NBPFILTER > 0
1067    /*
1068     * See if bpf wants to look at the packet.
1069     */
1070    if (sc->sc_bpf)
1071        bpf_mtap(sc->sc_bpf, m0);
1072#endif
1073
1074    /*
1075     * Put the packet on the appropriate queue.
1076     */
1077    s = splsoftnet();
1078    if (mode == NPMODE_QUEUE) {
1079        /* XXX we should limit the number of packets on this queue */
1080        *sc->sc_npqtail = m0;
1081        m0->m_nextpkt = NULL;
1082        sc->sc_npqtail = &m0->m_nextpkt;
1083    } else {
1084        ifq = (m0->m_flags & M_HIGHPRI)? &sc->sc_fastq: &ifp->if_snd;
1085        if (IF_QFULL(ifq) && dst->sa_family != AF_UNSPEC) {
1086            IF_DROP(ifq);
1087            splx(s);
1088            sc->sc_if.if_oerrors++;
1089            sc->sc_stats.ppp_oerrors++;
1090            error = ENOBUFS;
1091            goto bad;
1092        }
1093        IF_ENQUEUE(ifq, m0);
1094        (*sc->sc_start)(sc);
1095    }
1096    ifp->if_lastchange = ppp_time;
1097    ifp->if_opackets++;
1098    ifp->if_obytes += len;
1099
1100    splx(s);
1101    return (0);
1102
1103bad:
1104    m_freem(m0);
1105    return (error);
1106}
1107
1108/*
1109 * After a change in the NPmode for some NP, move packets from the
1110 * npqueue to the send queue or the fast queue as appropriate.
1111 * Should be called at spl[soft]net.
1112 */
1113static void
1114ppp_requeue(struct ppp_softc *sc)
1115{
1116    struct mbuf *m, **mpp;
1117    struct ifqueue *ifq;
1118    enum NPmode mode;
1119
1120    for (mpp = &sc->sc_npqueue; (m = *mpp) != NULL; ) {
1121        switch (PPP_PROTOCOL(mtod(m, u_char *))) {
1122        case PPP_IP:
1123            mode = sc->sc_npmode[NP_IP];
1124            break;
1125        default:
1126            mode = NPMODE_PASS;
1127        }
1128
1129        switch (mode) {
1130        case NPMODE_PASS:
1131            /*
1132             * This packet can now go on one of the queues to be sent.
1133             */
1134            *mpp = m->m_nextpkt;
1135            m->m_nextpkt = NULL;
1136            ifq = (m->m_flags & M_HIGHPRI)? &sc->sc_fastq: &sc->sc_if.if_snd;
1137            if (IF_QFULL(ifq)) {
1138                IF_DROP(ifq);
1139                sc->sc_if.if_oerrors++;
1140                sc->sc_stats.ppp_oerrors++;
1141            } else
1142                IF_ENQUEUE(ifq, m);
1143            break;
1144
1145        case NPMODE_DROP:
1146        case NPMODE_ERROR:
1147            *mpp = m->m_nextpkt;
1148            m_freem(m);
1149            break;
1150
1151        case NPMODE_QUEUE:
1152            mpp = &m->m_nextpkt;
1153            break;
1154        }
1155    }
1156    sc->sc_npqtail = mpp;
1157}
1158
1159/*
1160 * Get a packet to send.  This procedure is intended to be called at
1161 * splsoftnet, since it may involve time-consuming operations such as
1162 * applying VJ compression, packet compression, address/control and/or
1163 * protocol field compression to the packet.
1164 */
1165struct mbuf *
1166ppp_dequeue(struct ppp_softc *sc)
1167{
1168#ifdef PPP_COMPRESS
1169    struct mbuf *mp;
1170#endif
1171    struct mbuf *m;
1172    u_char *cp;
1173    int address, control, protocol;
1174
1175    /*
1176     * Grab a packet to send: first try the fast queue, then the
1177     * normal queue.
1178     */
1179    rtems_bsdnet_semaphore_obtain();
1180    IF_DEQUEUE(&sc->sc_fastq, m);
1181    if (m == NULL)
1182        IF_DEQUEUE(&sc->sc_if.if_snd, m);
1183    rtems_bsdnet_semaphore_release();
1184
1185    if (m == NULL)
1186        return NULL;
1187
1188    ++sc->sc_stats.ppp_opackets;
1189
1190    /*
1191     * Extract the ppp header of the new packet.
1192     * The ppp header will be in one mbuf.
1193     */
1194    cp = mtod(m, u_char *);
1195    address = PPP_ADDRESS(cp);
1196    control = PPP_CONTROL(cp);
1197    protocol = PPP_PROTOCOL(cp);
1198
1199    switch (protocol) {
1200    case PPP_IP:
1201#ifdef VJC
1202        /*
1203         * If the packet is a TCP/IP packet, see if we can compress it.
1204         */
1205        if ((sc->sc_flags & SC_COMP_TCP) && sc->sc_comp != NULL) {
1206            struct ip *ip;
1207            int type;
1208
1209            mp = m;
1210            ip = (struct ip *) (cp + PPP_HDRLEN);
1211            if (mp->m_len <= PPP_HDRLEN) {
1212                mp = mp->m_next;
1213                if (mp == NULL)
1214                    break;
1215                ip = mtod(mp, struct ip *);
1216            }
1217            /* this code assumes the IP/TCP header is in one non-shared mbuf */
1218            if (ip->ip_p == IPPROTO_TCP) {
1219                type = sl_compress_tcp(mp, ip, sc->sc_comp,
1220                                       !(sc->sc_flags & SC_NO_TCP_CCID));
1221                switch (type) {
1222                case TYPE_UNCOMPRESSED_TCP:
1223                    protocol = PPP_VJC_UNCOMP;
1224                    break;
1225                case TYPE_COMPRESSED_TCP:
1226                    protocol = PPP_VJC_COMP;
1227                    cp = mtod(m, u_char *);
1228                    cp[0] = address;    /* header has moved */
1229                    cp[1] = control;
1230                    cp[2] = 0;
1231                    break;
1232                }
1233                cp[3] = protocol;       /* update protocol in PPP header */
1234            }
1235        }
1236#endif  /* VJC */
1237        break;
1238
1239#ifdef PPP_COMPRESS
1240    case PPP_CCP:
1241        ppp_ccp(sc, m, 0);
1242        break;
1243#endif  /* PPP_COMPRESS */
1244    }
1245
1246#ifdef PPP_COMPRESS
1247    if (protocol != PPP_LCP && protocol != PPP_CCP
1248        && sc->sc_xc_state && (sc->sc_flags & SC_COMP_RUN)) {
1249        struct mbuf *mcomp = NULL;
1250        int slen, clen;
1251
1252        slen = 0;
1253        for (mp = m; mp != NULL; mp = mp->m_next)
1254            slen += mp->m_len;
1255        clen = (*sc->sc_xcomp->compress)
1256            (sc->sc_xc_state, &mcomp, m, slen, sc->sc_if.if_mtu + PPP_HDRLEN);
1257        if (mcomp != NULL) {
1258            if (sc->sc_flags & SC_CCP_UP) {
1259                /* Send the compressed packet instead of the original. */
1260                m_freem(m);
1261                m = mcomp;
1262                cp = mtod(m, u_char *);
1263                protocol = cp[3];
1264            } else {
1265                /* Can't transmit compressed packets until CCP is up. */
1266                m_freem(mcomp);
1267            }
1268        }
1269    }
1270#endif  /* PPP_COMPRESS */
1271
1272    /*
1273     * Compress the address/control and protocol, if possible.
1274     */
1275    if (sc->sc_flags & SC_COMP_AC && address == PPP_ALLSTATIONS &&
1276        control == PPP_UI && protocol != PPP_ALLSTATIONS &&
1277        protocol != PPP_LCP) {
1278        /* can compress address/control */
1279        m->m_data += 2;
1280        m->m_len -= 2;
1281    }
1282    if (sc->sc_flags & SC_COMP_PROT && protocol < 0xFF) {
1283        /* can compress protocol */
1284        if (mtod(m, u_char *) == cp) {
1285            cp[2] = cp[1];      /* move address/control up */
1286            cp[1] = cp[0];
1287        }
1288        ++m->m_data;
1289        --m->m_len;
1290    }
1291
1292    return m;
1293}
1294
1295#ifdef PPP_COMPRESS
1296/*
1297 * Handle a CCP packet.  `rcvd' is 1 if the packet was received,
1298 * 0 if it is about to be transmitted.
1299 */
1300static void
1301ppp_ccp(struct ppp_softc *sc, struct mbuf *m, int rcvd)
1302{
1303    u_char *dp, *ep;
1304    struct mbuf *mp;
1305    int slen, s;
1306
1307    /*
1308     * Get a pointer to the data after the PPP header.
1309     */
1310    if (m->m_len <= PPP_HDRLEN) {
1311        mp = m->m_next;
1312        if (mp == NULL)
1313            return;
1314        dp = (mp != NULL)? mtod(mp, u_char *): NULL;
1315    } else {
1316        mp = m;
1317        dp = mtod(mp, u_char *) + PPP_HDRLEN;
1318    }
1319
1320    ep = mtod(mp, u_char *) + mp->m_len;
1321    if (dp + CCP_HDRLEN > ep)
1322        return;
1323    slen = CCP_LENGTH(dp);
1324    if (dp + slen > ep) {
1325        if (sc->sc_flags & SC_DEBUG)
1326            printf("if_ppp/ccp: not enough data in mbuf (%p+%x > %p+%x)\n",
1327                   dp, slen, mtod(mp, u_char *), mp->m_len);
1328        return;
1329    }
1330
1331    switch (CCP_CODE(dp)) {
1332    case CCP_CONFREQ:
1333    case CCP_TERMREQ:
1334    case CCP_TERMACK:
1335        /* CCP must be going down - disable compression */
1336        if (sc->sc_flags & SC_CCP_UP) {
1337            s = splimp();
1338            sc->sc_flags &= ~(SC_CCP_UP | SC_COMP_RUN | SC_DECOMP_RUN);
1339            splx(s);
1340        }
1341        break;
1342
1343    case CCP_CONFACK:
1344        if (sc->sc_flags & SC_CCP_OPEN && !(sc->sc_flags & SC_CCP_UP)
1345            && slen >= CCP_HDRLEN + CCP_OPT_MINLEN
1346            && slen >= CCP_OPT_LENGTH(dp + CCP_HDRLEN) + CCP_HDRLEN) {
1347            if (!rcvd) {
1348                /* we're agreeing to send compressed packets. */
1349                if (sc->sc_xc_state != NULL
1350                    && (*sc->sc_xcomp->comp_init)
1351                        (sc->sc_xc_state, dp + CCP_HDRLEN, slen - CCP_HDRLEN,
1352                         sc->sc_if.if_unit, 0, sc->sc_flags & SC_DEBUG)) {
1353                    s = splimp();
1354                    sc->sc_flags |= SC_COMP_RUN;
1355                    splx(s);
1356                }
1357            } else {
1358                /* peer is agreeing to send compressed packets. */
1359                if (sc->sc_rc_state != NULL
1360                    && (*sc->sc_rcomp->decomp_init)
1361                        (sc->sc_rc_state, dp + CCP_HDRLEN, slen - CCP_HDRLEN,
1362                         sc->sc_if.if_unit, 0, sc->sc_mru,
1363                         sc->sc_flags & SC_DEBUG)) {
1364                    s = splimp();
1365                    sc->sc_flags |= SC_DECOMP_RUN;
1366                    sc->sc_flags &= ~(SC_DC_ERROR | SC_DC_FERROR);
1367                    splx(s);
1368                }
1369            }
1370        }
1371        break;
1372
1373    case CCP_RESETACK:
1374        if (sc->sc_flags & SC_CCP_UP) {
1375            if (!rcvd) {
1376                if (sc->sc_xc_state && (sc->sc_flags & SC_COMP_RUN))
1377                    (*sc->sc_xcomp->comp_reset)(sc->sc_xc_state);
1378            } else {
1379                if (sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)) {
1380                    (*sc->sc_rcomp->decomp_reset)(sc->sc_rc_state);
1381                    s = splimp();
1382                    sc->sc_flags &= ~SC_DC_ERROR;
1383                    splx(s);
1384                }
1385            }
1386        }
1387        break;
1388    }
1389}
1390
1391/*
1392 * CCP is down; free (de)compressor state if necessary.
1393 */
1394static void
1395ppp_ccp_closed(struct ppp_softc *sc)
1396{
1397    if (sc->sc_xc_state) {
1398        (*sc->sc_xcomp->comp_free)(sc->sc_xc_state);
1399        sc->sc_xc_state = NULL;
1400    }
1401    if (sc->sc_rc_state) {
1402        (*sc->sc_rcomp->decomp_free)(sc->sc_rc_state);
1403        sc->sc_rc_state = NULL;
1404    }
1405}
1406#endif /* PPP_COMPRESS */
1407
1408/*
1409 * Process a received PPP packet, doing decompression as necessary.
1410 * Should be called at splsoftnet.
1411 */
1412#define COMPTYPE(proto) ((proto) == PPP_VJC_COMP? TYPE_COMPRESSED_TCP: \
1413                         TYPE_UNCOMPRESSED_TCP)
1414
1415static struct mbuf *
1416ppp_inproc(struct ppp_softc *sc, struct mbuf *m)
1417{
1418    struct mbuf  *mf = (struct mbuf *)0;
1419    struct ifnet *ifp = &sc->sc_if;
1420    struct ifqueue *inq;
1421    int s, ilen, proto, rv;
1422    u_char *cp, adrs, ctrl;
1423    struct mbuf *mp;
1424#ifdef PPP_COMPRESS
1425    struct mbuf *dmp = NULL;
1426#endif
1427#ifdef VJC
1428    u_char *iphdr;
1429    u_int hlen;
1430    int xlen;
1431#endif
1432
1433    sc->sc_stats.ppp_ipackets++;
1434
1435    if (sc->sc_flags & SC_LOG_INPKT) {
1436        ilen = 0;
1437        for (mp = m; mp != NULL; mp = mp->m_next)
1438            ilen += mp->m_len;
1439        printf("ppp%d: got %d bytes\n", ifp->if_unit, ilen);
1440        pppdumpm(m);
1441    }
1442
1443    cp = mtod(m, u_char *);
1444    adrs = PPP_ADDRESS(cp);
1445    ctrl = PPP_CONTROL(cp);
1446    proto = PPP_PROTOCOL(cp);
1447
1448    if (m->m_flags & M_ERRMARK) {
1449        m->m_flags &= ~M_ERRMARK;
1450        s = splimp();
1451        sc->sc_flags |= SC_VJ_RESET;
1452        splx(s);
1453    }
1454
1455#ifdef PPP_COMPRESS
1456    /*
1457     * Decompress this packet if necessary, update the receiver's
1458     * dictionary, or take appropriate action on a CCP packet.
1459     */
1460    if (proto == PPP_COMP && sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)
1461        && !(sc->sc_flags & SC_DC_ERROR) && !(sc->sc_flags & SC_DC_FERROR)) {
1462        /* decompress this packet */
1463        rv = (*sc->sc_rcomp->decompress)(sc->sc_rc_state, m, &dmp);
1464        if (rv == DECOMP_OK) {
1465            m_freem(m);
1466            if (dmp == NULL) {
1467                /* no error, but no decompressed packet produced */
1468                return mf;
1469            }
1470            m = dmp;
1471            cp = mtod(m, u_char *);
1472            proto = PPP_PROTOCOL(cp);
1473
1474        } else {
1475            /*
1476             * An error has occurred in decompression.
1477             * Pass the compressed packet up to pppd, which may take
1478             * CCP down or issue a Reset-Req.
1479             */
1480            if (sc->sc_flags & SC_DEBUG)
1481                printf("ppp%d: decompress failed %d\n", ifp->if_unit, rv);
1482            s = splimp();
1483            sc->sc_flags |= SC_VJ_RESET;
1484            if (rv == DECOMP_ERROR)
1485                sc->sc_flags |= SC_DC_ERROR;
1486            else
1487                sc->sc_flags |= SC_DC_FERROR;
1488            splx(s);
1489        }
1490
1491    } else {
1492        if (sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)) {
1493            (*sc->sc_rcomp->incomp)(sc->sc_rc_state, m);
1494        }
1495        if (proto == PPP_CCP) {
1496            ppp_ccp(sc, m, 1);
1497        }
1498    }
1499#endif
1500
1501    ilen = 0;
1502    for (mp = m; mp != NULL; mp = mp->m_next)
1503        ilen += mp->m_len;
1504
1505#ifdef VJC
1506    if (sc->sc_flags & SC_VJ_RESET) {
1507        /*
1508         * If we've missed a packet, we must toss subsequent compressed
1509         * packets which don't have an explicit connection ID.
1510         */
1511        if (sc->sc_comp)
1512            sl_uncompress_tcp(NULL, 0, TYPE_ERROR, sc->sc_comp);
1513        s = splimp();
1514        sc->sc_flags &= ~SC_VJ_RESET;
1515        splx(s);
1516    }
1517
1518    /*
1519     * See if we have a VJ-compressed packet to uncompress.
1520     */
1521    if (proto == PPP_VJC_COMP) {
1522        if ((sc->sc_flags & SC_REJ_COMP_TCP) || sc->sc_comp == 0)
1523            goto bad;
1524
1525        xlen = sl_uncompress_tcp_core(cp + PPP_HDRLEN, m->m_len - PPP_HDRLEN,
1526                                      ilen - PPP_HDRLEN, TYPE_COMPRESSED_TCP,
1527                                      sc->sc_comp, &iphdr, &hlen);
1528
1529        if (xlen <= 0) {
1530            if (sc->sc_flags & SC_DEBUG)
1531                printf("ppp%d: VJ uncompress failed on type comp\n",
1532                        ifp->if_unit);
1533            goto bad;
1534        }
1535
1536        /* Copy the PPP and IP headers into a new mbuf. */
1537        MGETHDR(mp, M_DONTWAIT, MT_DATA);
1538        if (mp == NULL)
1539            goto bad;
1540        mp->m_len = 0;
1541        mp->m_next = NULL;
1542        if (hlen + PPP_HDRLEN > MHLEN) {
1543            MCLGET(mp, M_DONTWAIT);
1544            if (M_TRAILINGSPACE(mp) < hlen + PPP_HDRLEN) {
1545                m_freem(mp);
1546                goto bad;       /* lose if big headers and no clusters */
1547            }
1548        }
1549#ifdef MAC
1550        mac_create_mbuf_from_mbuf(m, mp);
1551#endif
1552        cp = mtod(mp, u_char *);
1553        cp[0] = adrs;
1554        cp[1] = ctrl;
1555        cp[2] = 0;
1556        cp[3] = PPP_IP;
1557        proto = PPP_IP;
1558        bcopy(iphdr, cp + PPP_HDRLEN, hlen);
1559        mp->m_len = hlen + PPP_HDRLEN;
1560
1561        /*
1562         * Trim the PPP and VJ headers off the old mbuf
1563         * and stick the new and old mbufs together.
1564         */
1565        m->m_data += PPP_HDRLEN + xlen;
1566        m->m_len -= PPP_HDRLEN + xlen;
1567        if (m->m_len <= M_TRAILINGSPACE(mp)) {
1568            bcopy(mtod(m, u_char *), mtod(mp, u_char *) + mp->m_len, m->m_len);
1569            mp->m_len += m->m_len;
1570            MFREE(m, mp->m_next);
1571        } else
1572            mp->m_next = m;
1573        m = mp;
1574        ilen += hlen - xlen;
1575
1576    } else if (proto == PPP_VJC_UNCOMP) {
1577        if ((sc->sc_flags & SC_REJ_COMP_TCP) || sc->sc_comp == 0)
1578            goto bad;
1579
1580        xlen = sl_uncompress_tcp_core(cp + PPP_HDRLEN, m->m_len - PPP_HDRLEN,
1581                                      ilen - PPP_HDRLEN, TYPE_UNCOMPRESSED_TCP,
1582                                      sc->sc_comp, &iphdr, &hlen);
1583
1584        if (xlen < 0) {
1585            if (sc->sc_flags & SC_DEBUG)
1586                printf("ppp%d: VJ uncompress failed on type uncomp\n",
1587                        ifp->if_unit);
1588            goto bad;
1589        }
1590
1591        proto = PPP_IP;
1592        cp[3] = PPP_IP;
1593    }
1594#endif /* VJC */
1595
1596    /*
1597     * If the packet will fit in a header mbuf, don't waste a
1598     * whole cluster on it.
1599     */
1600    if (ilen <= MHLEN && M_IS_CLUSTER(m)) {
1601        MGETHDR(mp, M_DONTWAIT, MT_DATA);
1602        if (mp != NULL) {
1603            m_copydata(m, 0, ilen, mtod(mp, caddr_t));
1604            /* instead of freeing - return cluster mbuf so it can be reused */
1605            /* m_freem(m); */
1606            mf = m;
1607            m = mp;
1608            m->m_len = ilen;
1609        }
1610    }
1611    m->m_pkthdr.len = ilen;
1612    m->m_pkthdr.rcvif = ifp;
1613
1614    if ((proto & 0x8000) == 0) {
1615#ifdef PPP_FILTER
1616        /*
1617         * See whether we want to pass this packet, and
1618         * if it counts as link activity.
1619         */
1620        adrs = *mtod(m, u_char *);      /* save address field */
1621        *mtod(m, u_char *) = 0;         /* indicate inbound */
1622        if (sc->sc_pass_filt.bf_insns != 0
1623            && bpf_filter(sc->sc_pass_filt.bf_insns, (u_char *) m,
1624                          ilen, 0) == 0) {
1625            /* drop this packet */
1626            m_freem(m);
1627            return mf;
1628        }
1629        if (sc->sc_active_filt.bf_insns == 0
1630            || bpf_filter(sc->sc_active_filt.bf_insns, (u_char *) m, ilen, 0))
1631            sc->sc_last_recv = time.tv_sec;
1632
1633        *mtod(m, u_char *) = adrs;
1634#else
1635        /*
1636         * Record the time that we received this packet.
1637         */
1638        microtime(&ppp_time);
1639        sc->sc_last_recv = ppp_time.tv_sec;
1640#endif /* PPP_FILTER */
1641    }
1642
1643#if NBPFILTER > 0
1644    /* See if bpf wants to look at the packet. */
1645    if (sc->sc_bpf)
1646        bpf_mtap(sc->sc_bpf, m);
1647#endif
1648
1649    rv = 0;
1650    switch (proto) {
1651#ifdef INET
1652    case PPP_IP:
1653        /*
1654         * IP packet - take off the ppp header and pass it up to IP.
1655         */
1656        if ((ifp->if_flags & IFF_UP) == 0
1657            || sc->sc_npmode[NP_IP] != NPMODE_PASS) {
1658            /* interface is down - drop the packet. */
1659            m_freem(m);
1660            return mf;
1661        }
1662        m->m_pkthdr.len -= PPP_HDRLEN;
1663        m->m_data += PPP_HDRLEN;
1664        m->m_len -= PPP_HDRLEN;
1665        schednetisr(NETISR_IP);
1666        inq = &ipintrq;
1667        break;
1668#endif
1669
1670    default:
1671        /*
1672         * Some other protocol - place on input queue for read().
1673         */
1674        inq = &sc->sc_inq;
1675        rv = 1;
1676        break;
1677    }
1678
1679    /*
1680     * Put the packet on the appropriate input queue.
1681     */
1682    s = splimp();
1683    if (IF_QFULL(inq)) {
1684        IF_DROP(inq);
1685        splx(s);
1686        if (sc->sc_flags & SC_DEBUG)
1687            printf("ppp%d: input queue full\n", ifp->if_unit);
1688        ifp->if_iqdrops++;
1689        goto bad;
1690    }
1691    IF_ENQUEUE(inq, m);
1692    splx(s);
1693
1694    ifp->if_ipackets++;
1695    ifp->if_ibytes += ilen;
1696    microtime(&ppp_time);
1697    ifp->if_lastchange = ppp_time;
1698
1699    if (rv) {
1700      (*sc->sc_ctlp)(sc);
1701    }
1702
1703    return mf;
1704
1705 bad:
1706    m_freem(m);
1707    sc->sc_if.if_ierrors++;
1708    sc->sc_stats.ppp_ierrors++;
1709    return mf;
1710}
1711
1712#define MAX_DUMP_BYTES  128
1713
1714static void
1715pppdumpm(struct mbuf *m0)
1716{
1717    char buf[3*MAX_DUMP_BYTES+4];
1718    char *bp = buf;
1719    struct mbuf *m;
1720    static char digits[] = "0123456789abcdef";
1721
1722    for (m = m0; m; m = m->m_next) {
1723        int l = m->m_len;
1724        u_char *rptr = (u_char *)m->m_data;
1725
1726        while (l--) {
1727            if (bp > buf + sizeof(buf) - 4)
1728                goto done;
1729            *bp++ = digits[*rptr >> 4]; /* convert byte to ascii hex */
1730            *bp++ = digits[*rptr++ & 0xf];
1731        }
1732
1733        if (m->m_next) {
1734            if (bp > buf + sizeof(buf) - 3)
1735                goto done;
1736            *bp++ = '|';
1737        } else
1738            *bp++ = ' ';
1739    }
1740done:
1741    if (m)
1742        *bp++ = '>';
1743    *bp = 0;
1744    printf("%s\n", buf);
1745}
1746
1747#endif  /* NPPP > 0 */
Note: See TracBrowser for help on using the repository browser.