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

5
Last change on this file since cb68253 was cb68253, checked in by Sebastian Huber <sebastian.huber@…>, on 09/07/18 at 04:19:02

network: Use kernel/user space header files

Add and use <machine/rtems-bsd-kernel-space.h> and
<machine/rtems-bsd-user-space.h> similar to the libbsd to avoid command
line defines and defines scattered throught the code base.

Simplify cpukit/libnetworking/Makefile.am.

Update #3375.

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