source: rtems/cpukit/libnetworking/net/if_ppp.c @ 456eab7d

4.115
Last change on this file since 456eab7d was 805360b, checked in by Sebastian Huber <sebastian.huber@…>, on 09/23/14 at 12:20:35

pppd: Fix warnings

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