2 * if_ppp.c - Point-to-Point Protocol (PPP) Asynchronous driver.
4 * Copyright (c) 1984-2000 Carnegie Mellon University. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
18 * 3. The name "Carnegie Mellon University" must not be used to
19 * endorse or promote products derived from this software without
20 * prior written permission. For permission or any legal
21 * details, please contact
22 * Office of Technology Transfer
23 * Carnegie Mellon University
25 * Pittsburgh, PA 15213-3890
26 * (412) 268-4387, fax: (412) 268-7395
27 * tech-transfer@andrew.cmu.edu
29 * 4. Redistributions of any form whatsoever must retain the following
31 * "This product includes software developed by Computing Services
32 * at Carnegie Mellon University (http://www.cmu.edu/computing/)."
34 * CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
35 * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
36 * AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE
37 * FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
38 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
39 * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
40 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
43 * Carnegie Mellon University
45 * Pittsburgh, PA 15213
50 * @(#)if_sl.c 7.6.1.2 (Berkeley) 2/15/89
52 * Copyright (c) 1987 Regents of the University of California.
53 * All rights reserved.
55 * Redistribution and use in source and binary forms are permitted
56 * provided that the above copyright notice and this paragraph are
57 * duplicated in all such forms and that any documentation,
58 * advertising materials, and other materials related to such
59 * distribution and use acknowledge that the software was developed
60 * by the University of California, Berkeley. The name of the
61 * University may not be used to endorse or promote products derived
62 * from this software without specific prior written permission.
63 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
64 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
65 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
67 * Serial Line interface
70 * Center for Seismic Studies
71 * 1300 N 17th Street, Suite 1450
72 * Arlington, Virginia 22209
77 * Pounded on heavily by Chris Torek (chris@mimsy.umd.edu, umcp-cs!chris).
78 * Converted to 4.3BSD Beta by Chris Torek.
79 * Other changes made at Berkeley, based in part on code by Kirk Smith.
81 * Converted to 4.3BSD+ 386BSD by Brad Parker (brad@cayman.com)
82 * Added VJ tcp header compression; more unified ioctls
84 * Extensively modified by Paul Mackerras (paulus@cs.anu.edu.au).
85 * Cleaned up a lot of the mbuf-related code to fix bugs that
86 * caused system crashes and packet corruption. Changed pppstart
87 * so that it doesn't just give up with a collision if the whole
88 * packet doesn't fit in the output ring buffer.
90 * Added priority queueing for interactive IP packets, following
91 * the model of if_sl.c, plus hooks for bpf.
92 * Paul Mackerras (paulus@cs.anu.edu.au).
95 /* $Id: if_ppp.c,v 1.15 2002/12/06 09:49:15 paulus Exp $ */
96 /* from if_sl.c,v 1.11 84/10/04 12:54:47 rick Exp */
97 /* from NetBSD: if_ppp.c,v 1.15.2.2 1994/07/28 05:17:58 cgd Exp */
105 #include <sys/param.h>
106 #include <sys/systm.h>
107 #include <sys/proc.h>
108 #include <sys/mbuf.h>
109 #include <sys/socket.h>
110 #include <sys/ioctl.h>
111 #include <sys/kernel.h>
112 #include <sys/time.h>
113 #include <sys/malloc.h>
116 #include <net/if_types.h>
117 #include <net/netisr.h>
118 #include <net/route.h>
124 #include <netinet/in.h>
125 #include <netinet/in_systm.h>
126 #include <netinet/in_var.h>
127 #include <netinet/ip.h>
130 #include "bpfilter.h"
136 #include <net/pppcompress.h>
139 #include <net/ppp_defs.h>
140 #include <net/if_ppp.h>
141 #include <net/if_pppvar.h>
142 #include <machine/cpu.h>
144 #define splsoftnet splnet
147 /* This definition should be moved to net/netisr.h */
148 #define NETISR_PPP 26 /* PPP software interrupt */
152 #define PACKETPTR struct mbuf *
153 #include <net/ppp-comp.h>
156 static int pppsioctl
__P((struct ifnet
*, int, caddr_t
));
157 static void ppp_requeue
__P((struct ppp_softc
*));
158 static void ppp_ccp
__P((struct ppp_softc
*, struct mbuf
*m
, int rcvd
));
159 static void ppp_ccp_closed
__P((struct ppp_softc
*));
160 static void ppp_inproc
__P((struct ppp_softc
*, struct mbuf
*));
161 static void pppdumpm
__P((struct mbuf
*m0
));
164 * Some useful mbuf macros not in mbuf.h.
166 #define M_IS_CLUSTER(m) ((m)->m_flags & M_EXT)
168 #define M_DATASTART(m) \
169 (M_IS_CLUSTER(m) ? (m)->m_ext.ext_buf : \
170 (m)->m_flags & M_PKTHDR ? (m)->m_pktdat : (m)->m_dat)
172 #define M_DATASIZE(m) \
173 (M_IS_CLUSTER(m) ? (m)->m_ext.ext_size : \
174 (m)->m_flags & M_PKTHDR ? MHLEN: MLEN)
177 * We steal two bits in the mbuf m_flags, to mark high-priority packets
178 * for output, and received packets following lost/corrupted packets.
180 #define M_HIGHPRI 0x2000 /* output packet for sc_fastq */
181 #define M_ERRMARK 0x4000 /* steal a bit in mbuf m_flags */
186 * List of compressors we know about.
187 * We leave some space so maybe we can modload compressors.
190 extern struct compressor ppp_bsd_compress
;
191 extern struct compressor ppp_deflate
, ppp_deflate_draft
;
193 struct compressor
*ppp_compressors
[8] = {
203 #endif /* PPP_COMPRESS */
205 TEXT_SET(pseudo_set
, pppattach
);
208 * Called from boot code to establish ppp interfaces.
213 register struct ppp_softc
*sc
;
215 extern void (*netisrs
[])__P((void));
217 for (sc
= ppp_softc
; i
< NPPP
; sc
++) {
218 sc
->sc_if
.if_name
= "ppp";
219 sc
->sc_if
.if_unit
= i
++;
220 sc
->sc_if
.if_mtu
= PPP_MTU
;
221 sc
->sc_if
.if_flags
= IFF_POINTOPOINT
| IFF_MULTICAST
;
222 sc
->sc_if
.if_type
= IFT_PPP
;
223 sc
->sc_if
.if_hdrlen
= PPP_HDRLEN
;
224 sc
->sc_if
.if_ioctl
= pppsioctl
;
225 sc
->sc_if
.if_output
= pppoutput
;
226 sc
->sc_if
.if_snd
.ifq_maxlen
= IFQ_MAXLEN
;
227 sc
->sc_inq
.ifq_maxlen
= IFQ_MAXLEN
;
228 sc
->sc_fastq
.ifq_maxlen
= IFQ_MAXLEN
;
229 sc
->sc_rawq
.ifq_maxlen
= IFQ_MAXLEN
;
230 if_attach(&sc
->sc_if
);
232 bpfattach(&sc
->sc_bpf
, &sc
->sc_if
, DLT_PPP
, PPP_HDRLEN
);
235 netisrs
[NETISR_PPP
] = pppintr
;
239 * Allocate a ppp interface unit and initialize it.
246 struct ppp_softc
*sc
;
248 for (nppp
= 0, sc
= ppp_softc
; nppp
< NPPP
; nppp
++, sc
++)
249 if (sc
->sc_xfer
== pid
) {
253 for (nppp
= 0, sc
= ppp_softc
; nppp
< NPPP
; nppp
++, sc
++)
254 if (sc
->sc_devp
== NULL
)
260 sc
->sc_mru
= PPP_MRU
;
261 sc
->sc_relinq
= NULL
;
262 bzero((char *)&sc
->sc_stats
, sizeof(sc
->sc_stats
));
264 MALLOC(sc
->sc_comp
, struct vjcompress
*, sizeof(struct vjcompress
),
267 vj_compress_init(sc
->sc_comp
, -1);
270 sc
->sc_xc_state
= NULL
;
271 sc
->sc_rc_state
= NULL
;
272 #endif /* PPP_COMPRESS */
273 for (i
= 0; i
< NUM_NP
; ++i
)
274 sc
->sc_npmode
[i
] = NPMODE_ERROR
;
275 sc
->sc_npqueue
= NULL
;
276 sc
->sc_npqtail
= &sc
->sc_npqueue
;
277 sc
->sc_last_sent
= sc
->sc_last_recv
= time
.tv_sec
;
283 * Deallocate a ppp unit. Must be called at splsoftnet or higher.
287 struct ppp_softc
*sc
;
292 sc
->sc_if
.if_flags
&= ~(IFF_UP
|IFF_RUNNING
);
296 IF_DEQUEUE(&sc
->sc_rawq
, m
);
302 IF_DEQUEUE(&sc
->sc_inq
, m
);
308 IF_DEQUEUE(&sc
->sc_fastq
, m
);
313 while ((m
= sc
->sc_npqueue
) != NULL
) {
314 sc
->sc_npqueue
= m
->m_nextpkt
;
317 if (sc
->sc_togo
!= NULL
) {
318 m_freem(sc
->sc_togo
);
323 sc
->sc_xc_state
= NULL
;
324 sc
->sc_rc_state
= NULL
;
325 #endif /* PPP_COMPRESS */
327 if (sc
->sc_pass_filt
.bf_insns
!= 0) {
328 FREE(sc
->sc_pass_filt
.bf_insns
, M_DEVBUF
);
329 sc
->sc_pass_filt
.bf_insns
= 0;
330 sc
->sc_pass_filt
.bf_len
= 0;
332 if (sc
->sc_active_filt
.bf_insns
!= 0) {
333 FREE(sc
->sc_active_filt
.bf_insns
, M_DEVBUF
);
334 sc
->sc_active_filt
.bf_insns
= 0;
335 sc
->sc_active_filt
.bf_len
= 0;
337 #endif /* PPP_FILTER */
339 if (sc
->sc_comp
!= 0) {
340 FREE(sc
->sc_comp
, M_DEVBUF
);
347 * Ioctl routine for generic ppp devices.
350 pppioctl(sc
, cmd
, data
, flag
, p
)
351 struct ppp_softc
*sc
;
357 int s
, error
, flags
, mru
, nb
, npx
;
358 struct ppp_option_data
*odp
;
359 struct compressor
**cp
;
363 struct bpf_program
*bp
, *nbp
;
364 struct bpf_insn
*newcode
, *oldcode
;
366 #endif /* PPP_FILTER */
368 u_char ccp_option
[CCP_MAX_OPTION_LENGTH
];
373 *(int *)data
= sc
->sc_inq
.ifq_len
;
377 *(int *)data
= sc
->sc_if
.if_unit
;
381 *(u_int
*)data
= sc
->sc_flags
;
385 if ((error
= suser(p
->p_ucred
, &p
->p_acflag
)) != 0)
387 flags
= *(int *)data
& SC_MASK
;
390 if (sc
->sc_flags
& SC_CCP_OPEN
&& !(flags
& SC_CCP_OPEN
))
394 sc
->sc_flags
= (sc
->sc_flags
& ~SC_MASK
) | flags
;
399 if ((error
= suser(p
->p_ucred
, &p
->p_acflag
)) != 0)
402 if (mru
>= PPP_MRU
&& mru
<= PPP_MAXMRU
)
407 *(int *)data
= sc
->sc_mru
;
412 if ((error
= suser(p
->p_ucred
, &p
->p_acflag
)) != 0)
416 vj_compress_init(sc
->sc_comp
, *(int *)data
);
423 if ((error
= suser(p
->p_ucred
, &p
->p_acflag
)) != 0)
425 sc
->sc_xfer
= p
->p_pid
;
429 case PPPIOCSCOMPRESS
:
430 if ((error
= suser(p
->p_ucred
, &p
->p_acflag
)) != 0)
432 odp
= (struct ppp_option_data
*) data
;
434 if (nb
> sizeof(ccp_option
))
435 nb
= sizeof(ccp_option
);
436 if ((error
= copyin(odp
->ptr
, ccp_option
, nb
)) != 0)
438 if (ccp_option
[1] < 2) /* preliminary check on the length byte */
440 for (cp
= ppp_compressors
; *cp
!= NULL
; ++cp
)
441 if ((*cp
)->compress_proto
== ccp_option
[0]) {
443 * Found a handler for the protocol - try to allocate
444 * a compressor or decompressor.
449 if (sc
->sc_xc_state
!= NULL
)
450 (*sc
->sc_xcomp
->comp_free
)(sc
->sc_xc_state
);
452 sc
->sc_xc_state
= (*cp
)->comp_alloc(ccp_option
, nb
);
453 if (sc
->sc_xc_state
== NULL
) {
454 if (sc
->sc_flags
& SC_DEBUG
)
455 printf("ppp%d: comp_alloc failed\n",
460 sc
->sc_flags
&= ~SC_COMP_RUN
;
464 if (sc
->sc_rc_state
!= NULL
)
465 (*sc
->sc_rcomp
->decomp_free
)(sc
->sc_rc_state
);
467 sc
->sc_rc_state
= (*cp
)->decomp_alloc(ccp_option
, nb
);
468 if (sc
->sc_rc_state
== NULL
) {
469 if (sc
->sc_flags
& SC_DEBUG
)
470 printf("ppp%d: decomp_alloc failed\n",
475 sc
->sc_flags
&= ~SC_DECOMP_RUN
;
480 if (sc
->sc_flags
& SC_DEBUG
)
481 printf("ppp%d: no compressor for [%x %x %x], %x\n",
482 sc
->sc_if
.if_unit
, ccp_option
[0], ccp_option
[1],
484 return (EINVAL
); /* no handler found */
485 #endif /* PPP_COMPRESS */
489 npi
= (struct npioctl
*) data
;
490 switch (npi
->protocol
) {
497 if (cmd
== PPPIOCGNPMODE
) {
498 npi
->mode
= sc
->sc_npmode
[npx
];
500 if ((error
= suser(p
->p_ucred
, &p
->p_acflag
)) != 0)
502 if (npi
->mode
!= sc
->sc_npmode
[npx
]) {
504 sc
->sc_npmode
[npx
] = npi
->mode
;
505 if (npi
->mode
!= NPMODE_QUEUE
) {
517 ((struct ppp_idle
*)data
)->xmit_idle
= t
- sc
->sc_last_sent
;
518 ((struct ppp_idle
*)data
)->recv_idle
= t
- sc
->sc_last_recv
;
525 nbp
= (struct bpf_program
*) data
;
526 if ((unsigned) nbp
->bf_len
> BPF_MAXINSNS
)
528 newcodelen
= nbp
->bf_len
* sizeof(struct bpf_insn
);
529 if (newcodelen
!= 0) {
530 MALLOC(newcode
, struct bpf_insn
*, newcodelen
, M_DEVBUF
, M_WAITOK
);
532 return EINVAL
; /* or sumpin */
534 if ((error
= copyin((caddr_t
)nbp
->bf_insns
, (caddr_t
)newcode
,
536 FREE(newcode
, M_DEVBUF
);
539 if (!bpf_validate(newcode
, nbp
->bf_len
)) {
540 FREE(newcode
, M_DEVBUF
);
545 bp
= (cmd
== PPPIOCSPASS
)? &sc
->sc_pass_filt
: &sc
->sc_active_filt
;
546 oldcode
= bp
->bf_insns
;
548 bp
->bf_len
= nbp
->bf_len
;
549 bp
->bf_insns
= newcode
;
552 FREE(oldcode
, M_DEVBUF
);
563 * Process an ioctl request to the ppp network interface.
566 pppsioctl(ifp
, cmd
, data
)
567 register struct ifnet
*ifp
;
571 struct proc
*p
= curproc
; /* XXX */
572 register struct ppp_softc
*sc
= &ppp_softc
[ifp
->if_unit
];
573 register struct ifaddr
*ifa
= (struct ifaddr
*)data
;
574 register struct ifreq
*ifr
= (struct ifreq
*)data
;
575 struct ppp_stats
*psp
;
577 struct ppp_comp_stats
*pcp
;
579 int s
= splimp(), error
= 0;
583 if ((ifp
->if_flags
& IFF_RUNNING
) == 0)
584 ifp
->if_flags
&= ~IFF_UP
;
588 if (ifa
->ifa_addr
->sa_family
!= AF_INET
)
589 error
= EAFNOSUPPORT
;
593 if (ifa
->ifa_addr
->sa_family
!= AF_INET
)
594 error
= EAFNOSUPPORT
;
598 if ((error
= suser(p
->p_ucred
, &p
->p_acflag
)) != 0)
600 sc
->sc_if
.if_mtu
= ifr
->ifr_mtu
;
604 ifr
->ifr_mtu
= sc
->sc_if
.if_mtu
;
610 error
= EAFNOSUPPORT
;
613 switch(ifr
->ifr_addr
.sa_family
) {
619 error
= EAFNOSUPPORT
;
625 psp
= &((struct ifpppstatsreq
*) data
)->stats
;
626 bzero(psp
, sizeof(*psp
));
627 psp
->p
= sc
->sc_stats
;
628 #if defined(VJC) && !defined(SL_NO_STATS)
630 psp
->vj
.vjs_packets
= sc
->sc_comp
->sls_packets
;
631 psp
->vj
.vjs_compressed
= sc
->sc_comp
->sls_compressed
;
632 psp
->vj
.vjs_searches
= sc
->sc_comp
->sls_searches
;
633 psp
->vj
.vjs_misses
= sc
->sc_comp
->sls_misses
;
634 psp
->vj
.vjs_uncompressedin
= sc
->sc_comp
->sls_uncompressedin
;
635 psp
->vj
.vjs_compressedin
= sc
->sc_comp
->sls_compressedin
;
636 psp
->vj
.vjs_errorin
= sc
->sc_comp
->sls_errorin
;
637 psp
->vj
.vjs_tossed
= sc
->sc_comp
->sls_tossed
;
644 pcp
= &((struct ifpppcstatsreq
*) data
)->stats
;
645 bzero(pcp
, sizeof(*pcp
));
646 if (sc
->sc_xc_state
!= NULL
)
647 (*sc
->sc_xcomp
->comp_stat
)(sc
->sc_xc_state
, &pcp
->c
);
648 if (sc
->sc_rc_state
!= NULL
)
649 (*sc
->sc_rcomp
->decomp_stat
)(sc
->sc_rc_state
, &pcp
->d
);
651 #endif /* PPP_COMPRESS */
661 * Queue a packet. Start transmission if not active.
662 * Packet is placed in Information field of PPP frame.
665 pppoutput(ifp
, m0
, dst
, rtp
)
668 struct sockaddr
*dst
;
671 register struct ppp_softc
*sc
= &ppp_softc
[ifp
->if_unit
];
672 int protocol
, address
, control
;
681 if (sc
->sc_devp
== NULL
|| (ifp
->if_flags
& IFF_RUNNING
) == 0
682 || ((ifp
->if_flags
& IFF_UP
) == 0 && dst
->sa_family
!= AF_UNSPEC
)) {
683 error
= ENETDOWN
; /* sort of */
688 * Compute PPP header.
690 m0
->m_flags
&= ~M_HIGHPRI
;
691 switch (dst
->sa_family
) {
694 address
= PPP_ALLSTATIONS
;
697 mode
= sc
->sc_npmode
[NP_IP
];
700 * If this packet has the "low delay" bit set in the IP header,
701 * put it on the fastq instead.
703 ip
= mtod(m0
, struct ip
*);
704 if (ip
->ip_tos
& IPTOS_LOWDELAY
)
705 m0
->m_flags
|= M_HIGHPRI
;
709 address
= PPP_ADDRESS(dst
->sa_data
);
710 control
= PPP_CONTROL(dst
->sa_data
);
711 protocol
= PPP_PROTOCOL(dst
->sa_data
);
715 printf("ppp%d: af%d not supported\n", ifp
->if_unit
, dst
->sa_family
);
716 error
= EAFNOSUPPORT
;
721 * Drop this packet, or return an error, if necessary.
723 if (mode
== NPMODE_ERROR
) {
727 if (mode
== NPMODE_DROP
) {
733 * Add PPP header. If no space in first mbuf, allocate another.
734 * (This assumes M_LEADINGSPACE is always 0 for a cluster mbuf.)
736 if (M_LEADINGSPACE(m0
) < PPP_HDRLEN
) {
737 m0
= m_prepend(m0
, PPP_HDRLEN
, M_DONTWAIT
);
744 m0
->m_data
-= PPP_HDRLEN
;
746 cp
= mtod(m0
, u_char
*);
749 *cp
++ = protocol
>> 8;
750 *cp
++ = protocol
& 0xff;
751 m0
->m_len
+= PPP_HDRLEN
;
754 for (m
= m0
; m
!= 0; m
= m
->m_next
)
757 if (sc
->sc_flags
& SC_LOG_OUTPKT
) {
758 printf("ppp%d output: ", ifp
->if_unit
);
762 if ((protocol
& 0x8000) == 0) {
765 * Apply the pass and active filters to the packet,
766 * but only if it is a data packet.
768 *mtod(m0
, u_char
*) = 1; /* indicates outbound */
769 if (sc
->sc_pass_filt
.bf_insns
!= 0
770 && bpf_filter(sc
->sc_pass_filt
.bf_insns
, (u_char
*) m0
,
772 error
= 0; /* drop this packet */
777 * Update the time we sent the most recent packet.
779 if (sc
->sc_active_filt
.bf_insns
== 0
780 || bpf_filter(sc
->sc_active_filt
.bf_insns
, (u_char
*) m0
, len
, 0))
781 sc
->sc_last_sent
= time
.tv_sec
;
783 *mtod(m0
, u_char
*) = address
;
786 * Update the time we sent the most recent data packet.
788 sc
->sc_last_sent
= time
.tv_sec
;
789 #endif /* PPP_FILTER */
794 * See if bpf wants to look at the packet.
797 bpf_mtap(sc
->sc_bpf
, m0
);
801 * Put the packet on the appropriate queue.
804 if (mode
== NPMODE_QUEUE
) {
805 /* XXX we should limit the number of packets on this queue */
806 *sc
->sc_npqtail
= m0
;
807 m0
->m_nextpkt
= NULL
;
808 sc
->sc_npqtail
= &m0
->m_nextpkt
;
810 ifq
= (m0
->m_flags
& M_HIGHPRI
)? &sc
->sc_fastq
: &ifp
->if_snd
;
811 if (IF_QFULL(ifq
) && dst
->sa_family
!= AF_UNSPEC
) {
814 sc
->sc_if
.if_oerrors
++;
815 sc
->sc_stats
.ppp_oerrors
++;
822 ifp
->if_lastchange
= time
;
824 ifp
->if_obytes
+= len
;
835 * After a change in the NPmode for some NP, move packets from the
836 * npqueue to the send queue or the fast queue as appropriate.
837 * Should be called at splsoftnet.
841 struct ppp_softc
*sc
;
843 struct mbuf
*m
, **mpp
;
847 for (mpp
= &sc
->sc_npqueue
; (m
= *mpp
) != NULL
; ) {
848 switch (PPP_PROTOCOL(mtod(m
, u_char
*))) {
850 mode
= sc
->sc_npmode
[NP_IP
];
859 * This packet can now go on one of the queues to be sent.
863 ifq
= (m
->m_flags
& M_HIGHPRI
)? &sc
->sc_fastq
: &sc
->sc_if
.if_snd
;
866 sc
->sc_if
.if_oerrors
++;
867 sc
->sc_stats
.ppp_oerrors
++;
883 sc
->sc_npqtail
= mpp
;
887 * Transmitter has finished outputting some stuff;
888 * remember to call sc->sc_start later at splsoftnet.
892 struct ppp_softc
*sc
;
896 sc
->sc_flags
&= ~SC_TBUSY
;
897 schednetisr(NETISR_PPP
);
902 * Get a packet to send. This procedure is intended to be called at
903 * splsoftnet, since it may involve time-consuming operations such as
904 * applying VJ compression, packet compression, address/control and/or
905 * protocol field compression to the packet.
909 struct ppp_softc
*sc
;
913 int address
, control
, protocol
;
916 * Grab a packet to send: first try the fast queue, then the
919 IF_DEQUEUE(&sc
->sc_fastq
, m
);
921 IF_DEQUEUE(&sc
->sc_if
.if_snd
, m
);
925 ++sc
->sc_stats
.ppp_opackets
;
928 * Extract the ppp header of the new packet.
929 * The ppp header will be in one mbuf.
931 cp
= mtod(m
, u_char
*);
932 address
= PPP_ADDRESS(cp
);
933 control
= PPP_CONTROL(cp
);
934 protocol
= PPP_PROTOCOL(cp
);
940 * If the packet is a TCP/IP packet, see if we can compress it.
942 if ((sc
->sc_flags
& SC_COMP_TCP
) && sc
->sc_comp
!= NULL
) {
947 ip
= (struct ip
*) (cp
+ PPP_HDRLEN
);
948 if (mp
->m_len
<= PPP_HDRLEN
) {
952 ip
= mtod(mp
, struct ip
*);
954 /* this code assumes the IP/TCP header is in one non-shared mbuf */
955 if (ip
->ip_p
== IPPROTO_TCP
) {
956 type
= vj_compress_tcp(mp
, ip
, sc
->sc_comp
,
957 !(sc
->sc_flags
& SC_NO_TCP_CCID
));
959 case TYPE_UNCOMPRESSED_TCP
:
960 protocol
= PPP_VJC_UNCOMP
;
962 case TYPE_COMPRESSED_TCP
:
963 protocol
= PPP_VJC_COMP
;
964 cp
= mtod(m
, u_char
*);
965 cp
[0] = address
; /* header has moved */
970 cp
[3] = protocol
; /* update protocol in PPP header */
980 #endif /* PPP_COMPRESS */
984 if (protocol
!= PPP_LCP
&& protocol
!= PPP_CCP
985 && sc
->sc_xc_state
&& (sc
->sc_flags
& SC_COMP_RUN
)) {
986 struct mbuf
*mcomp
= NULL
;
990 for (mp
= m
; mp
!= NULL
; mp
= mp
->m_next
)
992 clen
= (*sc
->sc_xcomp
->compress
)
993 (sc
->sc_xc_state
, &mcomp
, m
, slen
, sc
->sc_if
.if_mtu
+ PPP_HDRLEN
);
995 if (sc
->sc_flags
& SC_CCP_UP
) {
996 /* Send the compressed packet instead of the original. */
999 cp
= mtod(m
, u_char
*);
1002 /* Can't transmit compressed packets until CCP is up. */
1007 #endif /* PPP_COMPRESS */
1010 * Compress the address/control and protocol, if possible.
1012 if (sc
->sc_flags
& SC_COMP_AC
&& address
== PPP_ALLSTATIONS
&&
1013 control
== PPP_UI
&& protocol
!= PPP_ALLSTATIONS
&&
1014 protocol
!= PPP_LCP
) {
1015 /* can compress address/control */
1019 if (sc
->sc_flags
& SC_COMP_PROT
&& protocol
< 0xFF) {
1020 /* can compress protocol */
1021 if (mtod(m
, u_char
*) == cp
) {
1022 cp
[2] = cp
[1]; /* move address/control up */
1033 * Software interrupt routine, called at splsoftnet.
1038 struct ppp_softc
*sc
;
1044 for (i
= 0; i
< NPPP
; ++i
, ++sc
) {
1045 if (!(sc
->sc_flags
& SC_TBUSY
)
1046 && (sc
->sc_if
.if_snd
.ifq_head
|| sc
->sc_fastq
.ifq_head
)) {
1048 sc
->sc_flags
|= SC_TBUSY
;
1050 (*sc
->sc_start
)(sc
);
1054 IF_DEQUEUE(&sc
->sc_rawq
, m
);
1066 * Handle a CCP packet. `rcvd' is 1 if the packet was received,
1067 * 0 if it is about to be transmitted.
1070 ppp_ccp(sc
, m
, rcvd
)
1071 struct ppp_softc
*sc
;
1080 * Get a pointer to the data after the PPP header.
1082 if (m
->m_len
<= PPP_HDRLEN
) {
1086 dp
= (mp
!= NULL
)? mtod(mp
, u_char
*): NULL
;
1089 dp
= mtod(mp
, u_char
*) + PPP_HDRLEN
;
1092 ep
= mtod(mp
, u_char
*) + mp
->m_len
;
1093 if (dp
+ CCP_HDRLEN
> ep
)
1095 slen
= CCP_LENGTH(dp
);
1096 if (dp
+ slen
> ep
) {
1097 if (sc
->sc_flags
& SC_DEBUG
)
1098 printf("if_ppp/ccp: not enough data in mbuf (%p+%x > %p+%x)\n",
1099 dp
, slen
, mtod(mp
, u_char
*), mp
->m_len
);
1103 switch (CCP_CODE(dp
)) {
1107 /* CCP must be going down - disable compression */
1108 if (sc
->sc_flags
& SC_CCP_UP
) {
1110 sc
->sc_flags
&= ~(SC_CCP_UP
| SC_COMP_RUN
| SC_DECOMP_RUN
);
1116 if (sc
->sc_flags
& SC_CCP_OPEN
&& !(sc
->sc_flags
& SC_CCP_UP
)
1117 && slen
>= CCP_HDRLEN
+ CCP_OPT_MINLEN
1118 && slen
>= CCP_OPT_LENGTH(dp
+ CCP_HDRLEN
) + CCP_HDRLEN
) {
1120 /* we're agreeing to send compressed packets. */
1121 if (sc
->sc_xc_state
!= NULL
1122 && (*sc
->sc_xcomp
->comp_init
)
1123 (sc
->sc_xc_state
, dp
+ CCP_HDRLEN
, slen
- CCP_HDRLEN
,
1124 sc
->sc_if
.if_unit
, 0, sc
->sc_flags
& SC_DEBUG
)) {
1126 sc
->sc_flags
|= SC_COMP_RUN
;
1130 /* peer is agreeing to send compressed packets. */
1131 if (sc
->sc_rc_state
!= NULL
1132 && (*sc
->sc_rcomp
->decomp_init
)
1133 (sc
->sc_rc_state
, dp
+ CCP_HDRLEN
, slen
- CCP_HDRLEN
,
1134 sc
->sc_if
.if_unit
, 0, sc
->sc_mru
,
1135 sc
->sc_flags
& SC_DEBUG
)) {
1137 sc
->sc_flags
|= SC_DECOMP_RUN
;
1138 sc
->sc_flags
&= ~(SC_DC_ERROR
| SC_DC_FERROR
);
1146 if (sc
->sc_flags
& SC_CCP_UP
) {
1148 if (sc
->sc_xc_state
&& (sc
->sc_flags
& SC_COMP_RUN
))
1149 (*sc
->sc_xcomp
->comp_reset
)(sc
->sc_xc_state
);
1151 if (sc
->sc_rc_state
&& (sc
->sc_flags
& SC_DECOMP_RUN
)) {
1152 (*sc
->sc_rcomp
->decomp_reset
)(sc
->sc_rc_state
);
1154 sc
->sc_flags
&= ~SC_DC_ERROR
;
1164 * CCP is down; free (de)compressor state if necessary.
1168 struct ppp_softc
*sc
;
1170 if (sc
->sc_xc_state
) {
1171 (*sc
->sc_xcomp
->comp_free
)(sc
->sc_xc_state
);
1172 sc
->sc_xc_state
= NULL
;
1174 if (sc
->sc_rc_state
) {
1175 (*sc
->sc_rcomp
->decomp_free
)(sc
->sc_rc_state
);
1176 sc
->sc_rc_state
= NULL
;
1179 #endif /* PPP_COMPRESS */
1182 * PPP packet input routine.
1183 * The caller has checked and removed the FCS and has inserted
1184 * the address/control bytes and the protocol high byte if they
1188 ppppktin(sc
, m
, lost
)
1189 struct ppp_softc
*sc
;
1196 m
->m_flags
|= M_ERRMARK
;
1197 IF_ENQUEUE(&sc
->sc_rawq
, m
);
1198 schednetisr(NETISR_PPP
);
1203 * Process a received PPP packet, doing decompression as necessary.
1204 * Should be called at splsoftnet.
1206 #define COMPTYPE(proto) ((proto) == PPP_VJC_COMP? TYPE_COMPRESSED_TCP: \
1207 TYPE_UNCOMPRESSED_TCP)
1211 struct ppp_softc
*sc
;
1214 struct ifnet
*ifp
= &sc
->sc_if
;
1215 struct ifqueue
*inq
;
1216 int s
, ilen
, xlen
, proto
, rv
;
1217 u_char
*cp
, adrs
, ctrl
;
1218 struct mbuf
*mp
, *dmp
= NULL
;
1222 sc
->sc_stats
.ppp_ipackets
++;
1224 if (sc
->sc_flags
& SC_LOG_INPKT
) {
1226 for (mp
= m
; mp
!= NULL
; mp
= mp
->m_next
)
1228 printf("ppp%d: got %d bytes\n", ifp
->if_unit
, ilen
);
1232 cp
= mtod(m
, u_char
*);
1233 adrs
= PPP_ADDRESS(cp
);
1234 ctrl
= PPP_CONTROL(cp
);
1235 proto
= PPP_PROTOCOL(cp
);
1237 if (m
->m_flags
& M_ERRMARK
) {
1238 m
->m_flags
&= ~M_ERRMARK
;
1240 sc
->sc_flags
|= SC_VJ_RESET
;
1246 * Decompress this packet if necessary, update the receiver's
1247 * dictionary, or take appropriate action on a CCP packet.
1249 if (proto
== PPP_COMP
&& sc
->sc_rc_state
&& (sc
->sc_flags
& SC_DECOMP_RUN
)
1250 && !(sc
->sc_flags
& SC_DC_ERROR
) && !(sc
->sc_flags
& SC_DC_FERROR
)) {
1251 /* decompress this packet */
1252 rv
= (*sc
->sc_rcomp
->decompress
)(sc
->sc_rc_state
, m
, &dmp
);
1253 if (rv
== DECOMP_OK
) {
1256 /* no error, but no decompressed packet produced */
1260 cp
= mtod(m
, u_char
*);
1261 proto
= PPP_PROTOCOL(cp
);
1265 * An error has occurred in decompression.
1266 * Pass the compressed packet up to pppd, which may take
1267 * CCP down or issue a Reset-Req.
1269 if (sc
->sc_flags
& SC_DEBUG
)
1270 printf("ppp%d: decompress failed %d\n", ifp
->if_unit
, rv
);
1272 sc
->sc_flags
|= SC_VJ_RESET
;
1273 if (rv
== DECOMP_ERROR
)
1274 sc
->sc_flags
|= SC_DC_ERROR
;
1276 sc
->sc_flags
|= SC_DC_FERROR
;
1281 if (sc
->sc_rc_state
&& (sc
->sc_flags
& SC_DECOMP_RUN
)) {
1282 (*sc
->sc_rcomp
->incomp
)(sc
->sc_rc_state
, m
);
1284 if (proto
== PPP_CCP
) {
1291 for (mp
= m
; mp
!= NULL
; mp
= mp
->m_next
)
1295 if (sc
->sc_flags
& SC_VJ_RESET
) {
1297 * If we've missed a packet, we must toss subsequent compressed
1298 * packets which don't have an explicit connection ID.
1301 vj_uncompress_tcp(NULL
, 0, TYPE_ERROR
, sc
->sc_comp
);
1303 sc
->sc_flags
&= ~SC_VJ_RESET
;
1308 * See if we have a VJ-compressed packet to uncompress.
1310 if (proto
== PPP_VJC_COMP
) {
1311 if ((sc
->sc_flags
& SC_REJ_COMP_TCP
) || sc
->sc_comp
== 0)
1314 xlen
= vj_uncompress_tcp_core(cp
+ PPP_HDRLEN
, m
->m_len
- PPP_HDRLEN
,
1315 ilen
- PPP_HDRLEN
, TYPE_COMPRESSED_TCP
,
1316 sc
->sc_comp
, &iphdr
, &hlen
);
1319 if (sc
->sc_flags
& SC_DEBUG
)
1320 printf("ppp%d: VJ uncompress failed on type comp\n",
1325 /* Copy the PPP and IP headers into a new mbuf. */
1326 MGETHDR(mp
, M_DONTWAIT
, MT_DATA
);
1331 if (hlen
+ PPP_HDRLEN
> MHLEN
) {
1332 MCLGET(mp
, M_DONTWAIT
);
1333 if (M_TRAILINGSPACE(mp
) < hlen
+ PPP_HDRLEN
) {
1335 goto bad
; /* lose if big headers and no clusters */
1338 cp
= mtod(mp
, u_char
*);
1344 bcopy(iphdr
, cp
+ PPP_HDRLEN
, hlen
);
1345 mp
->m_len
= hlen
+ PPP_HDRLEN
;
1348 * Trim the PPP and VJ headers off the old mbuf
1349 * and stick the new and old mbufs together.
1351 m
->m_data
+= PPP_HDRLEN
+ xlen
;
1352 m
->m_len
-= PPP_HDRLEN
+ xlen
;
1353 if (m
->m_len
<= M_TRAILINGSPACE(mp
)) {
1354 bcopy(mtod(m
, u_char
*), mtod(mp
, u_char
*) + mp
->m_len
, m
->m_len
);
1355 mp
->m_len
+= m
->m_len
;
1356 MFREE(m
, mp
->m_next
);
1360 ilen
+= hlen
- xlen
;
1362 } else if (proto
== PPP_VJC_UNCOMP
) {
1363 if ((sc
->sc_flags
& SC_REJ_COMP_TCP
) || sc
->sc_comp
== 0)
1366 xlen
= vj_uncompress_tcp_core(cp
+ PPP_HDRLEN
, m
->m_len
- PPP_HDRLEN
,
1367 ilen
- PPP_HDRLEN
, TYPE_UNCOMPRESSED_TCP
,
1368 sc
->sc_comp
, &iphdr
, &hlen
);
1371 if (sc
->sc_flags
& SC_DEBUG
)
1372 printf("ppp%d: VJ uncompress failed on type uncomp\n",
1383 * If the packet will fit in a header mbuf, don't waste a
1384 * whole cluster on it.
1386 if (ilen
<= MHLEN
&& M_IS_CLUSTER(m
)) {
1387 MGETHDR(mp
, M_DONTWAIT
, MT_DATA
);
1389 m_copydata(m
, 0, ilen
, mtod(mp
, caddr_t
));
1395 m
->m_pkthdr
.len
= ilen
;
1396 m
->m_pkthdr
.rcvif
= ifp
;
1398 if ((proto
& 0x8000) == 0) {
1401 * See whether we want to pass this packet, and
1402 * if it counts as link activity.
1404 adrs
= *mtod(m
, u_char
*); /* save address field */
1405 *mtod(m
, u_char
*) = 0; /* indicate inbound */
1406 if (sc
->sc_pass_filt
.bf_insns
!= 0
1407 && bpf_filter(sc
->sc_pass_filt
.bf_insns
, (u_char
*) m
,
1409 /* drop this packet */
1413 if (sc
->sc_active_filt
.bf_insns
== 0
1414 || bpf_filter(sc
->sc_active_filt
.bf_insns
, (u_char
*) m
, ilen
, 0))
1415 sc
->sc_last_recv
= time
.tv_sec
;
1417 *mtod(m
, u_char
*) = adrs
;
1420 * Record the time that we received this packet.
1422 sc
->sc_last_recv
= time
.tv_sec
;
1423 #endif /* PPP_FILTER */
1427 /* See if bpf wants to look at the packet. */
1429 bpf_mtap(sc
->sc_bpf
, m
);
1437 * IP packet - take off the ppp header and pass it up to IP.
1439 if ((ifp
->if_flags
& IFF_UP
) == 0
1440 || sc
->sc_npmode
[NP_IP
] != NPMODE_PASS
) {
1441 /* interface is down - drop the packet. */
1445 m
->m_pkthdr
.len
-= PPP_HDRLEN
;
1446 m
->m_data
+= PPP_HDRLEN
;
1447 m
->m_len
-= PPP_HDRLEN
;
1448 schednetisr(NETISR_IP
);
1455 * Some other protocol - place on input queue for read().
1463 * Put the packet on the appropriate input queue.
1466 if (IF_QFULL(inq
)) {
1469 if (sc
->sc_flags
& SC_DEBUG
)
1470 printf("ppp%d: input queue full\n", ifp
->if_unit
);
1477 ifp
->if_ibytes
+= ilen
;
1478 ifp
->if_lastchange
= time
;
1487 sc
->sc_if
.if_ierrors
++;
1488 sc
->sc_stats
.ppp_ierrors
++;
1491 #define MAX_DUMP_BYTES 128
1497 char buf
[3*MAX_DUMP_BYTES
+4];
1500 static char digits
[] = "0123456789abcdef";
1502 for (m
= m0
; m
; m
= m
->m_next
) {
1504 u_char
*rptr
= (u_char
*)m
->m_data
;
1507 if (bp
> buf
+ sizeof(buf
) - 4)
1509 *bp
++ = digits
[*rptr
>> 4]; /* convert byte to ascii hex */
1510 *bp
++ = digits
[*rptr
++ & 0xf];
1514 if (bp
> buf
+ sizeof(buf
) - 3)
1524 printf("%s\n", buf
);
1527 #endif /* NPPP > 0 */