source: rtems/cpukit/libnetworking/net/if_ppp.c @ 4c672b93

4.104.114.84.95
Last change on this file since 4c672b93 was a009d94, checked in by Joel Sherrill <joel.sherrill@…>, on 08/31/06 at 01:10:02

2006-08-30 Joel Sherrill <joel@…>

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