1 /* $Id: if_ppp.c,v 1.8 1998/03/25 04:04:37 paulus Exp $ */
4 * if_ppp.c - Point-to-Point Protocol (PPP) Asynchronous driver.
6 * Copyright (c) 1989 Carnegie Mellon University.
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.
22 * Carnegie Mellon University
24 * Pittsburgh, PA 15213
29 * @(#)if_sl.c 7.6.1.2 (Berkeley) 2/15/89
31 * Copyright (c) 1987 Regents of the University of California.
32 * All rights reserved.
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.
46 * Serial Line interface
49 * Center for Seismic Studies
50 * 1300 N 17th Street, Suite 1450
51 * Arlington, Virginia 22209
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.
60 * Converted to 4.3BSD+ 386BSD by Brad Parker (brad@cayman.com)
61 * Added VJ tcp header compression; more unified ioctls
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.
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).
74 /* from if_sl.c,v 1.11 84/10/04 12:54:47 rick Exp */
75 /* from NetBSD: if_ppp.c,v 1.15.2.2 1994/07/28 05:17:58 cgd Exp */
83 #include <sys/param.h>
86 #include <sys/socket.h>
87 #include <sys/ioctl.h>
88 #include <sys/kernel.h>
89 #include <sys/systm.h>
91 #include <sys/malloc.h>
93 #if NetBSD1_0 && defined(i386)
94 #include <machine/psl.h>
98 #include <net/if_types.h>
99 #include <net/netisr.h>
100 #include <net/route.h>
106 #include <netinet/in.h>
107 #include <netinet/in_systm.h>
108 #include <netinet/in_var.h>
109 #include <netinet/ip.h>
112 #include "bpfilter.h"
114 #include <sys/time.h>
119 #include <net/slcompress.h>
122 #include <net/ppp_defs.h>
123 #include <net/if_ppp.h>
124 #include <net/if_pppvar.h>
125 #include <machine/cpu.h>
128 #define splsoftnet splnet
132 #define PACKETPTR struct mbuf *
133 #include <net/ppp-comp.h>
136 static int pppsioctl
__P((struct ifnet
*, u_long
, caddr_t
));
137 static void ppp_requeue
__P((struct ppp_softc
*));
138 static void ppp_ccp
__P((struct ppp_softc
*, struct mbuf
*m
, int rcvd
));
139 static void ppp_ccp_closed
__P((struct ppp_softc
*));
140 static void ppp_inproc
__P((struct ppp_softc
*, struct mbuf
*));
141 static void pppdumpm
__P((struct mbuf
*m0
));
144 * Some useful mbuf macros not in mbuf.h.
146 #define M_IS_CLUSTER(m) ((m)->m_flags & M_EXT)
148 #define M_DATASTART(m) \
149 (M_IS_CLUSTER(m) ? (m)->m_ext.ext_buf : \
150 (m)->m_flags & M_PKTHDR ? (m)->m_pktdat : (m)->m_dat)
152 #define M_DATASIZE(m) \
153 (M_IS_CLUSTER(m) ? (m)->m_ext.ext_size : \
154 (m)->m_flags & M_PKTHDR ? MHLEN: MLEN)
157 * We steal two bits in the mbuf m_flags, to mark high-priority packets
158 * for output, and received packets following lost/corrupted packets.
160 #define M_HIGHPRI 0x2000 /* output packet for sc_fastq */
161 #define M_ERRMARK 0x4000 /* steal a bit in mbuf m_flags */
166 * List of compressors we know about.
167 * We leave some space so maybe we can modload compressors.
170 extern struct compressor ppp_bsd_compress
;
171 extern struct compressor ppp_deflate
, ppp_deflate_draft
;
173 struct compressor
*ppp_compressors
[8] = {
183 #endif /* PPP_COMPRESS */
187 * Called from boot code to establish ppp interfaces.
192 register struct ppp_softc
*sc
;
195 for (sc
= ppp_softc
; i
< NPPP
; sc
++) {
196 sc
->sc_if
.if_name
= "ppp";
197 sc
->sc_if
.if_unit
= i
++;
198 sc
->sc_if
.if_mtu
= PPP_MTU
;
199 sc
->sc_if
.if_flags
= IFF_POINTOPOINT
| IFF_MULTICAST
;
200 sc
->sc_if
.if_type
= IFT_PPP
;
201 sc
->sc_if
.if_hdrlen
= PPP_HDRLEN
;
202 sc
->sc_if
.if_ioctl
= pppsioctl
;
203 sc
->sc_if
.if_output
= pppoutput
;
204 sc
->sc_if
.if_snd
.ifq_maxlen
= IFQ_MAXLEN
;
205 sc
->sc_inq
.ifq_maxlen
= IFQ_MAXLEN
;
206 sc
->sc_fastq
.ifq_maxlen
= IFQ_MAXLEN
;
207 sc
->sc_rawq
.ifq_maxlen
= IFQ_MAXLEN
;
208 if_attach(&sc
->sc_if
);
210 bpfattach(&sc
->sc_bpf
, &sc
->sc_if
, DLT_PPP
, PPP_HDRLEN
);
214 #if NetBSD1_0 && defined(i386)
216 * XXX kludge to fix the bug in the i386 interrupt handling code,
217 * where software interrupts could be taken while hardware
218 * interrupts were blocked.
220 if ((imask
[IPL_TTY
] & (1 << SIR_NET
)) == 0) {
221 imask
[IPL_TTY
] |= (1 << SIR_NET
);
222 intr_calculatemasks();
228 * Allocate a ppp interface unit and initialize it.
235 struct ppp_softc
*sc
;
237 for (nppp
= 0, sc
= ppp_softc
; nppp
< NPPP
; nppp
++, sc
++)
238 if (sc
->sc_xfer
== pid
) {
242 for (nppp
= 0, sc
= ppp_softc
; nppp
< NPPP
; nppp
++, sc
++)
243 if (sc
->sc_devp
== NULL
)
249 sc
->sc_mru
= PPP_MRU
;
250 sc
->sc_relinq
= NULL
;
251 bzero((char *)&sc
->sc_stats
, sizeof(sc
->sc_stats
));
253 MALLOC(sc
->sc_comp
, struct slcompress
*, sizeof(struct slcompress
),
256 sl_compress_init(sc
->sc_comp
, -1);
259 sc
->sc_xc_state
= NULL
;
260 sc
->sc_rc_state
= NULL
;
261 #endif /* PPP_COMPRESS */
262 for (i
= 0; i
< NUM_NP
; ++i
)
263 sc
->sc_npmode
[i
] = NPMODE_ERROR
;
264 sc
->sc_npqueue
= NULL
;
265 sc
->sc_npqtail
= &sc
->sc_npqueue
;
266 sc
->sc_last_sent
= sc
->sc_last_recv
= time
.tv_sec
;
272 * Deallocate a ppp unit. Must be called at splsoftnet or higher.
276 struct ppp_softc
*sc
;
281 sc
->sc_if
.if_flags
&= ~(IFF_UP
|IFF_RUNNING
);
285 IF_DEQUEUE(&sc
->sc_rawq
, m
);
291 IF_DEQUEUE(&sc
->sc_inq
, m
);
297 IF_DEQUEUE(&sc
->sc_fastq
, m
);
302 while ((m
= sc
->sc_npqueue
) != NULL
) {
303 sc
->sc_npqueue
= m
->m_nextpkt
;
306 if (sc
->sc_togo
!= NULL
) {
307 m_freem(sc
->sc_togo
);
312 sc
->sc_xc_state
= NULL
;
313 sc
->sc_rc_state
= NULL
;
314 #endif /* PPP_COMPRESS */
316 if (sc
->sc_pass_filt
.bf_insns
!= 0) {
317 FREE(sc
->sc_pass_filt
.bf_insns
, M_DEVBUF
);
318 sc
->sc_pass_filt
.bf_insns
= 0;
319 sc
->sc_pass_filt
.bf_len
= 0;
321 if (sc
->sc_active_filt
.bf_insns
!= 0) {
322 FREE(sc
->sc_active_filt
.bf_insns
, M_DEVBUF
);
323 sc
->sc_active_filt
.bf_insns
= 0;
324 sc
->sc_active_filt
.bf_len
= 0;
326 #endif /* PPP_FILTER */
328 if (sc
->sc_comp
!= 0) {
329 FREE(sc
->sc_comp
, M_DEVBUF
);
336 * Ioctl routine for generic ppp devices.
339 pppioctl(sc
, cmd
, data
, flag
, p
)
340 struct ppp_softc
*sc
;
346 int s
, error
, flags
, mru
, nb
, npx
;
347 struct ppp_option_data
*odp
;
348 struct compressor
**cp
;
352 struct bpf_program
*bp
, *nbp
;
353 struct bpf_insn
*newcode
, *oldcode
;
355 #endif /* PPP_FILTER */
357 u_char ccp_option
[CCP_MAX_OPTION_LENGTH
];
362 *(int *)data
= sc
->sc_inq
.ifq_len
;
366 *(int *)data
= sc
->sc_if
.if_unit
;
370 *(u_int
*)data
= sc
->sc_flags
;
374 if ((error
= suser(p
->p_ucred
, &p
->p_acflag
)) != 0)
376 flags
= *(int *)data
& SC_MASK
;
379 if (sc
->sc_flags
& SC_CCP_OPEN
&& !(flags
& SC_CCP_OPEN
))
383 sc
->sc_flags
= (sc
->sc_flags
& ~SC_MASK
) | flags
;
388 if ((error
= suser(p
->p_ucred
, &p
->p_acflag
)) != 0)
391 if (mru
>= PPP_MRU
&& mru
<= PPP_MAXMRU
)
396 *(int *)data
= sc
->sc_mru
;
401 if ((error
= suser(p
->p_ucred
, &p
->p_acflag
)) != 0)
405 sl_compress_init(sc
->sc_comp
, *(int *)data
);
412 if ((error
= suser(p
->p_ucred
, &p
->p_acflag
)) != 0)
414 sc
->sc_xfer
= p
->p_pid
;
418 case PPPIOCSCOMPRESS
:
419 if ((error
= suser(p
->p_ucred
, &p
->p_acflag
)) != 0)
421 odp
= (struct ppp_option_data
*) data
;
423 if (nb
> sizeof(ccp_option
))
424 nb
= sizeof(ccp_option
);
425 if ((error
= copyin(odp
->ptr
, ccp_option
, nb
)) != 0)
427 if (ccp_option
[1] < 2) /* preliminary check on the length byte */
429 for (cp
= ppp_compressors
; *cp
!= NULL
; ++cp
)
430 if ((*cp
)->compress_proto
== ccp_option
[0]) {
432 * Found a handler for the protocol - try to allocate
433 * a compressor or decompressor.
438 if (sc
->sc_xc_state
!= NULL
)
439 (*sc
->sc_xcomp
->comp_free
)(sc
->sc_xc_state
);
441 sc
->sc_xc_state
= (*cp
)->comp_alloc(ccp_option
, nb
);
442 if (sc
->sc_xc_state
== NULL
) {
443 if (sc
->sc_flags
& SC_DEBUG
)
444 printf("ppp%d: comp_alloc failed\n",
449 sc
->sc_flags
&= ~SC_COMP_RUN
;
453 if (sc
->sc_rc_state
!= NULL
)
454 (*sc
->sc_rcomp
->decomp_free
)(sc
->sc_rc_state
);
456 sc
->sc_rc_state
= (*cp
)->decomp_alloc(ccp_option
, nb
);
457 if (sc
->sc_rc_state
== NULL
) {
458 if (sc
->sc_flags
& SC_DEBUG
)
459 printf("ppp%d: decomp_alloc failed\n",
464 sc
->sc_flags
&= ~SC_DECOMP_RUN
;
469 if (sc
->sc_flags
& SC_DEBUG
)
470 printf("ppp%d: no compressor for [%x %x %x], %x\n",
471 sc
->sc_if
.if_unit
, ccp_option
[0], ccp_option
[1],
473 return (EINVAL
); /* no handler found */
474 #endif /* PPP_COMPRESS */
478 npi
= (struct npioctl
*) data
;
479 switch (npi
->protocol
) {
486 if (cmd
== PPPIOCGNPMODE
) {
487 npi
->mode
= sc
->sc_npmode
[npx
];
489 if ((error
= suser(p
->p_ucred
, &p
->p_acflag
)) != 0)
491 if (npi
->mode
!= sc
->sc_npmode
[npx
]) {
493 sc
->sc_npmode
[npx
] = npi
->mode
;
494 if (npi
->mode
!= NPMODE_QUEUE
) {
506 ((struct ppp_idle
*)data
)->xmit_idle
= t
- sc
->sc_last_sent
;
507 ((struct ppp_idle
*)data
)->recv_idle
= t
- sc
->sc_last_recv
;
514 nbp
= (struct bpf_program
*) data
;
515 if ((unsigned) nbp
->bf_len
> BPF_MAXINSNS
)
517 newcodelen
= nbp
->bf_len
* sizeof(struct bpf_insn
);
518 if (newcodelen
!= 0) {
519 MALLOC(newcode
, struct bpf_insn
*, newcodelen
, M_DEVBUF
, M_WAITOK
);
521 return EINVAL
; /* or sumpin */
523 if ((error
= copyin((caddr_t
)nbp
->bf_insns
, (caddr_t
)newcode
,
525 FREE(newcode
, M_DEVBUF
);
528 if (!bpf_validate(newcode
, nbp
->bf_len
)) {
529 FREE(newcode
, M_DEVBUF
);
534 bp
= (cmd
== PPPIOCSPASS
)? &sc
->sc_pass_filt
: &sc
->sc_active_filt
;
535 oldcode
= bp
->bf_insns
;
537 bp
->bf_len
= nbp
->bf_len
;
538 bp
->bf_insns
= newcode
;
541 FREE(oldcode
, M_DEVBUF
);
552 * Process an ioctl request to the ppp network interface.
555 pppsioctl(ifp
, cmd
, data
)
556 register struct ifnet
*ifp
;
560 struct proc
*p
= curproc
; /* XXX */
561 register struct ppp_softc
*sc
= &ppp_softc
[ifp
->if_unit
];
562 register struct ifaddr
*ifa
= (struct ifaddr
*)data
;
563 register struct ifreq
*ifr
= (struct ifreq
*)data
;
564 struct ppp_stats
*psp
;
566 struct ppp_comp_stats
*pcp
;
568 int s
= splimp(), error
= 0;
572 if ((ifp
->if_flags
& IFF_RUNNING
) == 0)
573 ifp
->if_flags
&= ~IFF_UP
;
577 if (ifa
->ifa_addr
->sa_family
!= AF_INET
)
578 error
= EAFNOSUPPORT
;
582 if (ifa
->ifa_addr
->sa_family
!= AF_INET
)
583 error
= EAFNOSUPPORT
;
587 if ((error
= suser(p
->p_ucred
, &p
->p_acflag
)) != 0)
589 sc
->sc_if
.if_mtu
= ifr
->ifr_mtu
;
593 ifr
->ifr_mtu
= sc
->sc_if
.if_mtu
;
599 error
= EAFNOSUPPORT
;
602 switch(ifr
->ifr_addr
.sa_family
) {
608 error
= EAFNOSUPPORT
;
614 psp
= &((struct ifpppstatsreq
*) data
)->stats
;
615 bzero(psp
, sizeof(*psp
));
616 psp
->p
= sc
->sc_stats
;
617 #if defined(VJC) && !defined(SL_NO_STATS)
619 psp
->vj
.vjs_packets
= sc
->sc_comp
->sls_packets
;
620 psp
->vj
.vjs_compressed
= sc
->sc_comp
->sls_compressed
;
621 psp
->vj
.vjs_searches
= sc
->sc_comp
->sls_searches
;
622 psp
->vj
.vjs_misses
= sc
->sc_comp
->sls_misses
;
623 psp
->vj
.vjs_uncompressedin
= sc
->sc_comp
->sls_uncompressedin
;
624 psp
->vj
.vjs_compressedin
= sc
->sc_comp
->sls_compressedin
;
625 psp
->vj
.vjs_errorin
= sc
->sc_comp
->sls_errorin
;
626 psp
->vj
.vjs_tossed
= sc
->sc_comp
->sls_tossed
;
633 pcp
= &((struct ifpppcstatsreq
*) data
)->stats
;
634 bzero(pcp
, sizeof(*pcp
));
635 if (sc
->sc_xc_state
!= NULL
)
636 (*sc
->sc_xcomp
->comp_stat
)(sc
->sc_xc_state
, &pcp
->c
);
637 if (sc
->sc_rc_state
!= NULL
)
638 (*sc
->sc_rcomp
->decomp_stat
)(sc
->sc_rc_state
, &pcp
->d
);
640 #endif /* PPP_COMPRESS */
650 * Queue a packet. Start transmission if not active.
651 * Packet is placed in Information field of PPP frame.
654 pppoutput(ifp
, m0
, dst
, rtp
)
657 struct sockaddr
*dst
;
660 register struct ppp_softc
*sc
= &ppp_softc
[ifp
->if_unit
];
661 int protocol
, address
, control
;
670 if (sc
->sc_devp
== NULL
|| (ifp
->if_flags
& IFF_RUNNING
) == 0
671 || ((ifp
->if_flags
& IFF_UP
) == 0 && dst
->sa_family
!= AF_UNSPEC
)) {
672 error
= ENETDOWN
; /* sort of */
677 * Compute PPP header.
679 m0
->m_flags
&= ~M_HIGHPRI
;
680 switch (dst
->sa_family
) {
683 address
= PPP_ALLSTATIONS
;
686 mode
= sc
->sc_npmode
[NP_IP
];
689 * If this packet has the "low delay" bit set in the IP header,
690 * put it on the fastq instead.
692 ip
= mtod(m0
, struct ip
*);
693 if (ip
->ip_tos
& IPTOS_LOWDELAY
)
694 m0
->m_flags
|= M_HIGHPRI
;
698 address
= PPP_ADDRESS(dst
->sa_data
);
699 control
= PPP_CONTROL(dst
->sa_data
);
700 protocol
= PPP_PROTOCOL(dst
->sa_data
);
704 printf("ppp%d: af%d not supported\n", ifp
->if_unit
, dst
->sa_family
);
705 error
= EAFNOSUPPORT
;
710 * Drop this packet, or return an error, if necessary.
712 if (mode
== NPMODE_ERROR
) {
716 if (mode
== NPMODE_DROP
) {
722 * Add PPP header. If no space in first mbuf, allocate another.
723 * (This assumes M_LEADINGSPACE is always 0 for a cluster mbuf.)
725 if (M_LEADINGSPACE(m0
) < PPP_HDRLEN
) {
726 m0
= m_prepend(m0
, PPP_HDRLEN
, M_DONTWAIT
);
733 m0
->m_data
-= PPP_HDRLEN
;
735 cp
= mtod(m0
, u_char
*);
738 *cp
++ = protocol
>> 8;
739 *cp
++ = protocol
& 0xff;
740 m0
->m_len
+= PPP_HDRLEN
;
743 for (m
= m0
; m
!= 0; m
= m
->m_next
)
746 if (sc
->sc_flags
& SC_LOG_OUTPKT
) {
747 printf("ppp%d output: ", ifp
->if_unit
);
751 if ((protocol
& 0x8000) == 0) {
754 * Apply the pass and active filters to the packet,
755 * but only if it is a data packet.
757 *mtod(m0
, u_char
*) = 1; /* indicates outbound */
758 if (sc
->sc_pass_filt
.bf_insns
!= 0
759 && bpf_filter(sc
->sc_pass_filt
.bf_insns
, (u_char
*) m0
,
761 error
= 0; /* drop this packet */
766 * Update the time we sent the most recent packet.
768 if (sc
->sc_active_filt
.bf_insns
== 0
769 || bpf_filter(sc
->sc_active_filt
.bf_insns
, (u_char
*) m0
, len
, 0))
770 sc
->sc_last_sent
= time
.tv_sec
;
772 *mtod(m0
, u_char
*) = address
;
775 * Update the time we sent the most recent data packet.
777 sc
->sc_last_sent
= time
.tv_sec
;
778 #endif /* PPP_FILTER */
783 * See if bpf wants to look at the packet.
786 bpf_mtap(sc
->sc_bpf
, m0
);
790 * Put the packet on the appropriate queue.
793 if (mode
== NPMODE_QUEUE
) {
794 /* XXX we should limit the number of packets on this queue */
795 *sc
->sc_npqtail
= m0
;
796 m0
->m_nextpkt
= NULL
;
797 sc
->sc_npqtail
= &m0
->m_nextpkt
;
799 ifq
= (m0
->m_flags
& M_HIGHPRI
)? &sc
->sc_fastq
: &ifp
->if_snd
;
800 if (IF_QFULL(ifq
) && dst
->sa_family
!= AF_UNSPEC
) {
803 sc
->sc_if
.if_oerrors
++;
804 sc
->sc_stats
.ppp_oerrors
++;
811 ifp
->if_lastchange
= time
;
813 ifp
->if_obytes
+= len
;
824 * After a change in the NPmode for some NP, move packets from the
825 * npqueue to the send queue or the fast queue as appropriate.
826 * Should be called at splsoftnet.
830 struct ppp_softc
*sc
;
832 struct mbuf
*m
, **mpp
;
836 for (mpp
= &sc
->sc_npqueue
; (m
= *mpp
) != NULL
; ) {
837 switch (PPP_PROTOCOL(mtod(m
, u_char
*))) {
839 mode
= sc
->sc_npmode
[NP_IP
];
848 * This packet can now go on one of the queues to be sent.
852 ifq
= (m
->m_flags
& M_HIGHPRI
)? &sc
->sc_fastq
: &sc
->sc_if
.if_snd
;
855 sc
->sc_if
.if_oerrors
++;
856 sc
->sc_stats
.ppp_oerrors
++;
872 sc
->sc_npqtail
= mpp
;
876 * Transmitter has finished outputting some stuff;
877 * remember to call sc->sc_start later at splsoftnet.
881 struct ppp_softc
*sc
;
885 sc
->sc_flags
&= ~SC_TBUSY
;
886 schednetisr(NETISR_PPP
);
891 * Get a packet to send. This procedure is intended to be called at
892 * splsoftnet, since it may involve time-consuming operations such as
893 * applying VJ compression, packet compression, address/control and/or
894 * protocol field compression to the packet.
898 struct ppp_softc
*sc
;
902 int address
, control
, protocol
;
905 * Grab a packet to send: first try the fast queue, then the
908 IF_DEQUEUE(&sc
->sc_fastq
, m
);
910 IF_DEQUEUE(&sc
->sc_if
.if_snd
, m
);
914 ++sc
->sc_stats
.ppp_opackets
;
917 * Extract the ppp header of the new packet.
918 * The ppp header will be in one mbuf.
920 cp
= mtod(m
, u_char
*);
921 address
= PPP_ADDRESS(cp
);
922 control
= PPP_CONTROL(cp
);
923 protocol
= PPP_PROTOCOL(cp
);
929 * If the packet is a TCP/IP packet, see if we can compress it.
931 if ((sc
->sc_flags
& SC_COMP_TCP
) && sc
->sc_comp
!= NULL
) {
936 ip
= (struct ip
*) (cp
+ PPP_HDRLEN
);
937 if (mp
->m_len
<= PPP_HDRLEN
) {
941 ip
= mtod(mp
, struct ip
*);
943 /* this code assumes the IP/TCP header is in one non-shared mbuf */
944 if (ip
->ip_p
== IPPROTO_TCP
) {
945 type
= sl_compress_tcp(mp
, ip
, sc
->sc_comp
,
946 !(sc
->sc_flags
& SC_NO_TCP_CCID
));
948 case TYPE_UNCOMPRESSED_TCP
:
949 protocol
= PPP_VJC_UNCOMP
;
951 case TYPE_COMPRESSED_TCP
:
952 protocol
= PPP_VJC_COMP
;
953 cp
= mtod(m
, u_char
*);
954 cp
[0] = address
; /* header has moved */
959 cp
[3] = protocol
; /* update protocol in PPP header */
969 #endif /* PPP_COMPRESS */
973 if (protocol
!= PPP_LCP
&& protocol
!= PPP_CCP
974 && sc
->sc_xc_state
&& (sc
->sc_flags
& SC_COMP_RUN
)) {
975 struct mbuf
*mcomp
= NULL
;
979 for (mp
= m
; mp
!= NULL
; mp
= mp
->m_next
)
981 clen
= (*sc
->sc_xcomp
->compress
)
982 (sc
->sc_xc_state
, &mcomp
, m
, slen
, sc
->sc_if
.if_mtu
+ PPP_HDRLEN
);
984 if (sc
->sc_flags
& SC_CCP_UP
) {
985 /* Send the compressed packet instead of the original. */
988 cp
= mtod(m
, u_char
*);
991 /* Can't transmit compressed packets until CCP is up. */
996 #endif /* PPP_COMPRESS */
999 * Compress the address/control and protocol, if possible.
1001 if (sc
->sc_flags
& SC_COMP_AC
&& address
== PPP_ALLSTATIONS
&&
1002 control
== PPP_UI
&& protocol
!= PPP_ALLSTATIONS
&&
1003 protocol
!= PPP_LCP
) {
1004 /* can compress address/control */
1008 if (sc
->sc_flags
& SC_COMP_PROT
&& protocol
< 0xFF) {
1009 /* can compress protocol */
1010 if (mtod(m
, u_char
*) == cp
) {
1011 cp
[2] = cp
[1]; /* move address/control up */
1022 * Software interrupt routine, called at splsoftnet.
1027 struct ppp_softc
*sc
;
1033 for (i
= 0; i
< NPPP
; ++i
, ++sc
) {
1034 if (!(sc
->sc_flags
& SC_TBUSY
)
1035 && (sc
->sc_if
.if_snd
.ifq_head
|| sc
->sc_fastq
.ifq_head
)) {
1037 sc
->sc_flags
|= SC_TBUSY
;
1039 (*sc
->sc_start
)(sc
);
1043 IF_DEQUEUE(&sc
->sc_rawq
, m
);
1055 * Handle a CCP packet. `rcvd' is 1 if the packet was received,
1056 * 0 if it is about to be transmitted.
1059 ppp_ccp(sc
, m
, rcvd
)
1060 struct ppp_softc
*sc
;
1069 * Get a pointer to the data after the PPP header.
1071 if (m
->m_len
<= PPP_HDRLEN
) {
1075 dp
= (mp
!= NULL
)? mtod(mp
, u_char
*): NULL
;
1078 dp
= mtod(mp
, u_char
*) + PPP_HDRLEN
;
1081 ep
= mtod(mp
, u_char
*) + mp
->m_len
;
1082 if (dp
+ CCP_HDRLEN
> ep
)
1084 slen
= CCP_LENGTH(dp
);
1085 if (dp
+ slen
> ep
) {
1086 if (sc
->sc_flags
& SC_DEBUG
)
1087 printf("if_ppp/ccp: not enough data in mbuf (%p+%x > %p+%x)\n",
1088 dp
, slen
, mtod(mp
, u_char
*), mp
->m_len
);
1092 switch (CCP_CODE(dp
)) {
1096 /* CCP must be going down - disable compression */
1097 if (sc
->sc_flags
& SC_CCP_UP
) {
1099 sc
->sc_flags
&= ~(SC_CCP_UP
| SC_COMP_RUN
| SC_DECOMP_RUN
);
1105 if (sc
->sc_flags
& SC_CCP_OPEN
&& !(sc
->sc_flags
& SC_CCP_UP
)
1106 && slen
>= CCP_HDRLEN
+ CCP_OPT_MINLEN
1107 && slen
>= CCP_OPT_LENGTH(dp
+ CCP_HDRLEN
) + CCP_HDRLEN
) {
1109 /* we're agreeing to send compressed packets. */
1110 if (sc
->sc_xc_state
!= NULL
1111 && (*sc
->sc_xcomp
->comp_init
)
1112 (sc
->sc_xc_state
, dp
+ CCP_HDRLEN
, slen
- CCP_HDRLEN
,
1113 sc
->sc_if
.if_unit
, 0, sc
->sc_flags
& SC_DEBUG
)) {
1115 sc
->sc_flags
|= SC_COMP_RUN
;
1119 /* peer is agreeing to send compressed packets. */
1120 if (sc
->sc_rc_state
!= NULL
1121 && (*sc
->sc_rcomp
->decomp_init
)
1122 (sc
->sc_rc_state
, dp
+ CCP_HDRLEN
, slen
- CCP_HDRLEN
,
1123 sc
->sc_if
.if_unit
, 0, sc
->sc_mru
,
1124 sc
->sc_flags
& SC_DEBUG
)) {
1126 sc
->sc_flags
|= SC_DECOMP_RUN
;
1127 sc
->sc_flags
&= ~(SC_DC_ERROR
| SC_DC_FERROR
);
1135 if (sc
->sc_flags
& SC_CCP_UP
) {
1137 if (sc
->sc_xc_state
&& (sc
->sc_flags
& SC_COMP_RUN
))
1138 (*sc
->sc_xcomp
->comp_reset
)(sc
->sc_xc_state
);
1140 if (sc
->sc_rc_state
&& (sc
->sc_flags
& SC_DECOMP_RUN
)) {
1141 (*sc
->sc_rcomp
->decomp_reset
)(sc
->sc_rc_state
);
1143 sc
->sc_flags
&= ~SC_DC_ERROR
;
1153 * CCP is down; free (de)compressor state if necessary.
1157 struct ppp_softc
*sc
;
1159 if (sc
->sc_xc_state
) {
1160 (*sc
->sc_xcomp
->comp_free
)(sc
->sc_xc_state
);
1161 sc
->sc_xc_state
= NULL
;
1163 if (sc
->sc_rc_state
) {
1164 (*sc
->sc_rcomp
->decomp_free
)(sc
->sc_rc_state
);
1165 sc
->sc_rc_state
= NULL
;
1168 #endif /* PPP_COMPRESS */
1171 * PPP packet input routine.
1172 * The caller has checked and removed the FCS and has inserted
1173 * the address/control bytes and the protocol high byte if they
1177 ppppktin(sc
, m
, lost
)
1178 struct ppp_softc
*sc
;
1185 m
->m_flags
|= M_ERRMARK
;
1186 IF_ENQUEUE(&sc
->sc_rawq
, m
);
1187 schednetisr(NETISR_PPP
);
1192 * Process a received PPP packet, doing decompression as necessary.
1193 * Should be called at splsoftnet.
1195 #define COMPTYPE(proto) ((proto) == PPP_VJC_COMP? TYPE_COMPRESSED_TCP: \
1196 TYPE_UNCOMPRESSED_TCP)
1200 struct ppp_softc
*sc
;
1203 struct ifnet
*ifp
= &sc
->sc_if
;
1204 struct ifqueue
*inq
;
1205 int s
, ilen
, xlen
, proto
, rv
;
1206 u_char
*cp
, adrs
, ctrl
;
1207 struct mbuf
*mp
, *dmp
= NULL
;
1211 sc
->sc_stats
.ppp_ipackets
++;
1213 if (sc
->sc_flags
& SC_LOG_INPKT
) {
1215 for (mp
= m
; mp
!= NULL
; mp
= mp
->m_next
)
1217 printf("ppp%d: got %d bytes\n", ifp
->if_unit
, ilen
);
1221 cp
= mtod(m
, u_char
*);
1222 adrs
= PPP_ADDRESS(cp
);
1223 ctrl
= PPP_CONTROL(cp
);
1224 proto
= PPP_PROTOCOL(cp
);
1226 if (m
->m_flags
& M_ERRMARK
) {
1227 m
->m_flags
&= ~M_ERRMARK
;
1229 sc
->sc_flags
|= SC_VJ_RESET
;
1235 * Decompress this packet if necessary, update the receiver's
1236 * dictionary, or take appropriate action on a CCP packet.
1238 if (proto
== PPP_COMP
&& sc
->sc_rc_state
&& (sc
->sc_flags
& SC_DECOMP_RUN
)
1239 && !(sc
->sc_flags
& SC_DC_ERROR
) && !(sc
->sc_flags
& SC_DC_FERROR
)) {
1240 /* decompress this packet */
1241 rv
= (*sc
->sc_rcomp
->decompress
)(sc
->sc_rc_state
, m
, &dmp
);
1242 if (rv
== DECOMP_OK
) {
1245 /* no error, but no decompressed packet produced */
1249 cp
= mtod(m
, u_char
*);
1250 proto
= PPP_PROTOCOL(cp
);
1254 * An error has occurred in decompression.
1255 * Pass the compressed packet up to pppd, which may take
1256 * CCP down or issue a Reset-Req.
1258 if (sc
->sc_flags
& SC_DEBUG
)
1259 printf("ppp%d: decompress failed %d\n", ifp
->if_unit
, rv
);
1261 sc
->sc_flags
|= SC_VJ_RESET
;
1262 if (rv
== DECOMP_ERROR
)
1263 sc
->sc_flags
|= SC_DC_ERROR
;
1265 sc
->sc_flags
|= SC_DC_FERROR
;
1270 if (sc
->sc_rc_state
&& (sc
->sc_flags
& SC_DECOMP_RUN
)) {
1271 (*sc
->sc_rcomp
->incomp
)(sc
->sc_rc_state
, m
);
1273 if (proto
== PPP_CCP
) {
1280 for (mp
= m
; mp
!= NULL
; mp
= mp
->m_next
)
1284 if (sc
->sc_flags
& SC_VJ_RESET
) {
1286 * If we've missed a packet, we must toss subsequent compressed
1287 * packets which don't have an explicit connection ID.
1290 sl_uncompress_tcp(NULL
, 0, TYPE_ERROR
, sc
->sc_comp
);
1292 sc
->sc_flags
&= ~SC_VJ_RESET
;
1297 * See if we have a VJ-compressed packet to uncompress.
1299 if (proto
== PPP_VJC_COMP
) {
1300 if ((sc
->sc_flags
& SC_REJ_COMP_TCP
) || sc
->sc_comp
== 0)
1303 xlen
= sl_uncompress_tcp_core(cp
+ PPP_HDRLEN
, m
->m_len
- PPP_HDRLEN
,
1304 ilen
- PPP_HDRLEN
, TYPE_COMPRESSED_TCP
,
1305 sc
->sc_comp
, &iphdr
, &hlen
);
1308 if (sc
->sc_flags
& SC_DEBUG
)
1309 printf("ppp%d: VJ uncompress failed on type comp\n",
1314 /* Copy the PPP and IP headers into a new mbuf. */
1315 MGETHDR(mp
, M_DONTWAIT
, MT_DATA
);
1320 if (hlen
+ PPP_HDRLEN
> MHLEN
) {
1321 MCLGET(mp
, M_DONTWAIT
);
1322 if (M_TRAILINGSPACE(mp
) < hlen
+ PPP_HDRLEN
) {
1324 goto bad
; /* lose if big headers and no clusters */
1327 cp
= mtod(mp
, u_char
*);
1333 bcopy(iphdr
, cp
+ PPP_HDRLEN
, hlen
);
1334 mp
->m_len
= hlen
+ PPP_HDRLEN
;
1337 * Trim the PPP and VJ headers off the old mbuf
1338 * and stick the new and old mbufs together.
1340 m
->m_data
+= PPP_HDRLEN
+ xlen
;
1341 m
->m_len
-= PPP_HDRLEN
+ xlen
;
1342 if (m
->m_len
<= M_TRAILINGSPACE(mp
)) {
1343 bcopy(mtod(m
, u_char
*), mtod(mp
, u_char
*) + mp
->m_len
, m
->m_len
);
1344 mp
->m_len
+= m
->m_len
;
1345 MFREE(m
, mp
->m_next
);
1349 ilen
+= hlen
- xlen
;
1351 } else if (proto
== PPP_VJC_UNCOMP
) {
1352 if ((sc
->sc_flags
& SC_REJ_COMP_TCP
) || sc
->sc_comp
== 0)
1355 xlen
= sl_uncompress_tcp_core(cp
+ PPP_HDRLEN
, m
->m_len
- PPP_HDRLEN
,
1356 ilen
- PPP_HDRLEN
, TYPE_UNCOMPRESSED_TCP
,
1357 sc
->sc_comp
, &iphdr
, &hlen
);
1360 if (sc
->sc_flags
& SC_DEBUG
)
1361 printf("ppp%d: VJ uncompress failed on type uncomp\n",
1372 * If the packet will fit in a header mbuf, don't waste a
1373 * whole cluster on it.
1375 if (ilen
<= MHLEN
&& M_IS_CLUSTER(m
)) {
1376 MGETHDR(mp
, M_DONTWAIT
, MT_DATA
);
1378 m_copydata(m
, 0, ilen
, mtod(mp
, caddr_t
));
1384 m
->m_pkthdr
.len
= ilen
;
1385 m
->m_pkthdr
.rcvif
= ifp
;
1387 if ((proto
& 0x8000) == 0) {
1390 * See whether we want to pass this packet, and
1391 * if it counts as link activity.
1393 adrs
= *mtod(m
, u_char
*); /* save address field */
1394 *mtod(m
, u_char
*) = 0; /* indicate inbound */
1395 if (sc
->sc_pass_filt
.bf_insns
!= 0
1396 && bpf_filter(sc
->sc_pass_filt
.bf_insns
, (u_char
*) m
,
1398 /* drop this packet */
1402 if (sc
->sc_active_filt
.bf_insns
== 0
1403 || bpf_filter(sc
->sc_active_filt
.bf_insns
, (u_char
*) m
, ilen
, 0))
1404 sc
->sc_last_recv
= time
.tv_sec
;
1406 *mtod(m
, u_char
*) = adrs
;
1409 * Record the time that we received this packet.
1411 sc
->sc_last_recv
= time
.tv_sec
;
1412 #endif /* PPP_FILTER */
1416 /* See if bpf wants to look at the packet. */
1418 bpf_mtap(sc
->sc_bpf
, m
);
1426 * IP packet - take off the ppp header and pass it up to IP.
1428 if ((ifp
->if_flags
& IFF_UP
) == 0
1429 || sc
->sc_npmode
[NP_IP
] != NPMODE_PASS
) {
1430 /* interface is down - drop the packet. */
1434 m
->m_pkthdr
.len
-= PPP_HDRLEN
;
1435 m
->m_data
+= PPP_HDRLEN
;
1436 m
->m_len
-= PPP_HDRLEN
;
1437 schednetisr(NETISR_IP
);
1444 * Some other protocol - place on input queue for read().
1452 * Put the packet on the appropriate input queue.
1455 if (IF_QFULL(inq
)) {
1458 if (sc
->sc_flags
& SC_DEBUG
)
1459 printf("ppp%d: input queue full\n", ifp
->if_unit
);
1466 ifp
->if_ibytes
+= ilen
;
1467 ifp
->if_lastchange
= time
;
1476 sc
->sc_if
.if_ierrors
++;
1477 sc
->sc_stats
.ppp_ierrors
++;
1480 #define MAX_DUMP_BYTES 128
1486 char buf
[3*MAX_DUMP_BYTES
+4];
1489 static char digits
[] = "0123456789abcdef";
1491 for (m
= m0
; m
; m
= m
->m_next
) {
1493 u_char
*rptr
= (u_char
*)m
->m_data
;
1496 if (bp
> buf
+ sizeof(buf
) - 4)
1498 *bp
++ = digits
[*rptr
>> 4]; /* convert byte to ascii hex */
1499 *bp
++ = digits
[*rptr
++ & 0xf];
1503 if (bp
> buf
+ sizeof(buf
) - 3)
1513 printf("%s\n", buf
);
1516 #endif /* NPPP > 0 */