2 * if_ppp.c - Point-to-Point Protocol (PPP) Asynchronous driver.
4 * Copyright (c) 1989 Carnegie Mellon University.
7 * Redistribution and use in source and binary forms are permitted
8 * provided that the above copyright notice and this paragraph are
9 * duplicated in all such forms and that any documentation,
10 * advertising materials, and other materials related to such
11 * distribution and use acknowledge that the software was developed
12 * by Carnegie Mellon University. The name of the
13 * University may not be used to endorse or promote products derived
14 * from this software without specific prior written permission.
15 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
17 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20 * Carnegie Mellon University
22 * Pittsburgh, PA 15213
27 * @(#)if_sl.c 7.6.1.2 (Berkeley) 2/15/89
29 * Copyright (c) 1987 Regents of the University of California.
30 * All rights reserved.
32 * Redistribution and use in source and binary forms are permitted
33 * provided that the above copyright notice and this paragraph are
34 * duplicated in all such forms and that any documentation,
35 * advertising materials, and other materials related to such
36 * distribution and use acknowledge that the software was developed
37 * by the University of California, Berkeley. The name of the
38 * University may not be used to endorse or promote products derived
39 * from this software without specific prior written permission.
40 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
41 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
42 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
44 * Serial Line interface
47 * Center for Seismic Studies
48 * 1300 N 17th Street, Suite 1450
49 * Arlington, Virginia 22209
54 * Pounded on heavily by Chris Torek (chris@mimsy.umd.edu, umcp-cs!chris).
55 * Converted to 4.3BSD Beta by Chris Torek.
56 * Other changes made at Berkeley, based in part on code by Kirk Smith.
58 * Converted to 4.3BSD+ 386BSD by Brad Parker (brad@cayman.com)
59 * Added VJ tcp header compression; more unified ioctls
61 * Extensively modified by Paul Mackerras (paulus@cs.anu.edu.au).
62 * Cleaned up a lot of the mbuf-related code to fix bugs that
63 * caused system crashes and packet corruption. Changed pppstart
64 * so that it doesn't just give up with a collision if the whole
65 * packet doesn't fit in the output ring buffer.
67 * Added priority queueing for interactive IP packets, following
68 * the model of if_sl.c, plus hooks for bpf.
69 * Paul Mackerras (paulus@cs.anu.edu.au).
72 /* $FreeBSD: src/sys/net/if_ppp.c,v 1.67.2.4 2002/04/14 21:41:48 luigi Exp $ */
73 /* $DragonFly: src/sys/net/ppp/if_ppp.c,v 1.35 2007/05/23 08:57:10 dillon Exp $ */
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 */
88 #include <sys/param.h>
89 #include <sys/systm.h>
92 #include <sys/socket.h>
93 #include <sys/filio.h>
94 #include <sys/sockio.h>
95 #include <sys/kernel.h>
97 #include <sys/malloc.h>
99 #include <sys/msgport2.h>
102 #include <net/if_types.h>
103 #include <net/ifq_var.h>
104 #include <net/netisr.h>
108 #include <netinet/in.h>
109 #include <netinet/in_systm.h>
110 #include <netinet/in_var.h>
111 #include <netinet/ip.h>
115 #include <netproto/ipx/ipx.h>
116 #include <netproto/ipx/ipx_if.h>
120 #include <net/slcompress.h>
124 #include "if_pppvar.h"
128 #define splsoftnet splnet
132 #define PACKETPTR struct mbuf *
133 #include <net/ppp_layer/ppp_comp.h>
136 struct ppp_softc ppp_softc
[NPPP
];
138 /* XXX layering violation */
139 extern void pppasyncattach (void *);
141 static void pppattach (void *);
142 PSEUDO_SET(pppattach
, if_ppp
);
144 static int pppsioctl (struct ifnet
*ifp
, u_long cmd
, caddr_t data
,
147 static void ppp_requeue (struct ppp_softc
*);
148 static void ppp_ccp (struct ppp_softc
*, struct mbuf
*m
, int rcvd
);
149 static void ppp_ccp_closed (struct ppp_softc
*);
150 static void ppp_inproc (struct ppp_softc
*, struct mbuf
*);
151 static void pppdumpm (struct mbuf
*m0
);
152 static void ppp_ifstart(struct ifnet
*ifp
);
155 * Some useful mbuf macros not in mbuf.h.
157 #define M_IS_CLUSTER(m) ((m)->m_flags & M_EXT)
159 #define M_DATASTART(m) \
160 (M_IS_CLUSTER(m) ? (m)->m_ext.ext_buf : \
161 (m)->m_flags & M_PKTHDR ? (m)->m_pktdat : (m)->m_dat)
163 #define M_DATASIZE(m) \
164 (M_IS_CLUSTER(m) ? (m)->m_ext.ext_size : \
165 (m)->m_flags & M_PKTHDR ? MHLEN: MLEN)
168 * We steal two bits in the mbuf m_flags, to mark high-priority packets
169 * for output, and received packets following lost/corrupted packets.
171 #define M_HIGHPRI M_PROTO2 /* output packet for sc_fastq */
172 #define M_ERRMARK M_PROTO3 /* steal a bit in mbuf m_flags */
177 * List of compressors we know about.
178 * We leave some space so maybe we can modload compressors.
181 extern struct compressor ppp_bsd_compress
;
182 extern struct compressor ppp_deflate
, ppp_deflate_draft
;
184 static struct compressor
*ppp_compressors
[8] = {
185 #if DO_BSD_COMPRESS && defined(PPP_BSDCOMP)
188 #if DO_DEFLATE && defined(PPP_DEFLATE)
194 #endif /* PPP_COMPRESS */
197 * Software interrupt routine, called at spl[soft]net.
200 pppintr(struct netmsg
*msg
)
203 struct ppp_softc
*sc
;
207 * Packets are never sent to this netisr so the message must always
208 * be replied. Interlock processing and notification by replying
211 lwkt_replymsg(&msg
->nm_lmsg
, 0);
214 for (i
= 0; i
< NPPP
; ++i
, ++sc
) {
215 lwkt_serialize_enter(sc
->sc_if
.if_serializer
);
216 if (!(sc
->sc_flags
& SC_TBUSY
)
217 && (!ifq_is_empty(&sc
->sc_if
.if_snd
) || !IF_QEMPTY(&sc
->sc_fastq
))) {
218 sc
->sc_flags
|= SC_TBUSY
;
222 IF_DEQUEUE(&sc
->sc_rawq
, m
);
227 lwkt_serialize_exit(sc
->sc_if
.if_serializer
);
232 * Called from boot code to establish ppp interfaces.
235 pppattach(void *dummy
)
237 struct ppp_softc
*sc
;
240 for (sc
= ppp_softc
; i
< NPPP
; sc
++) {
241 if_initname(&(sc
->sc_if
), "ppp", i
++);
242 sc
->sc_if
.if_mtu
= PPP_MTU
;
243 sc
->sc_if
.if_flags
= IFF_POINTOPOINT
| IFF_MULTICAST
;
244 sc
->sc_if
.if_type
= IFT_PPP
;
245 sc
->sc_if
.if_hdrlen
= PPP_HDRLEN
;
246 sc
->sc_if
.if_ioctl
= pppsioctl
;
247 sc
->sc_if
.if_output
= pppoutput
;
248 sc
->sc_if
.if_start
= ppp_ifstart
;
249 ifq_set_maxlen(&sc
->sc_if
.if_snd
, IFQ_MAXLEN
);
250 ifq_set_ready(&sc
->sc_if
.if_snd
);
251 sc
->sc_inq
.ifq_maxlen
= IFQ_MAXLEN
;
252 sc
->sc_fastq
.ifq_maxlen
= IFQ_MAXLEN
;
253 sc
->sc_rawq
.ifq_maxlen
= IFQ_MAXLEN
;
254 callout_init(&sc
->sc_timeout
);
255 if_attach(&sc
->sc_if
, NULL
);
256 bpfattach(&sc
->sc_if
, DLT_PPP
, PPP_HDRLEN
);
258 netisr_register(NETISR_PPP
, cpu0_portfn
, pppintr
);
260 * XXX layering violation - if_ppp can work over any lower level
261 * transport that cares to attach to it.
263 pppasyncattach(dummy
);
267 * Allocate a ppp interface unit and initialize it.
270 pppalloc(struct thread
*td
)
273 struct ppp_softc
*sc
;
275 for (nppp
= 0, sc
= ppp_softc
; nppp
< NPPP
; nppp
++, sc
++)
276 if (sc
->sc_xfer
== td
) {
280 for (nppp
= 0, sc
= ppp_softc
; nppp
< NPPP
; nppp
++, sc
++)
281 if (sc
->sc_devp
== NULL
)
287 sc
->sc_mru
= PPP_MRU
;
288 sc
->sc_relinq
= NULL
;
289 bzero((char *)&sc
->sc_stats
, sizeof(sc
->sc_stats
));
291 MALLOC(sc
->sc_comp
, struct slcompress
*, sizeof(struct slcompress
),
293 sl_compress_init(sc
->sc_comp
, -1);
296 sc
->sc_xc_state
= NULL
;
297 sc
->sc_rc_state
= NULL
;
298 #endif /* PPP_COMPRESS */
299 for (i
= 0; i
< NUM_NP
; ++i
)
300 sc
->sc_npmode
[i
] = NPMODE_ERROR
;
301 sc
->sc_npqueue
= NULL
;
302 sc
->sc_npqtail
= &sc
->sc_npqueue
;
303 sc
->sc_last_sent
= sc
->sc_last_recv
= time_second
;
309 * Deallocate a ppp unit. Must be called at splsoftnet or higher.
312 pppdealloc(struct ppp_softc
*sc
)
317 sc
->sc_if
.if_flags
&= ~(IFF_UP
|IFF_RUNNING
);
318 getmicrotime(&sc
->sc_if
.if_lastchange
);
322 IF_DEQUEUE(&sc
->sc_rawq
, m
);
328 IF_DEQUEUE(&sc
->sc_inq
, m
);
334 IF_DEQUEUE(&sc
->sc_fastq
, m
);
339 while ((m
= sc
->sc_npqueue
) != NULL
) {
340 sc
->sc_npqueue
= m
->m_nextpkt
;
345 sc
->sc_xc_state
= NULL
;
346 sc
->sc_rc_state
= NULL
;
347 #endif /* PPP_COMPRESS */
349 if (sc
->sc_pass_filt
.bf_insns
!= 0) {
350 kfree(sc
->sc_pass_filt
.bf_insns
, M_DEVBUF
);
351 sc
->sc_pass_filt
.bf_insns
= 0;
352 sc
->sc_pass_filt
.bf_len
= 0;
354 if (sc
->sc_active_filt
.bf_insns
!= 0) {
355 kfree(sc
->sc_active_filt
.bf_insns
, M_DEVBUF
);
356 sc
->sc_active_filt
.bf_insns
= 0;
357 sc
->sc_active_filt
.bf_len
= 0;
359 #endif /* PPP_FILTER */
361 if (sc
->sc_comp
!= 0) {
362 kfree(sc
->sc_comp
, M_DEVBUF
);
369 * Ioctl routine for generic ppp devices.
372 pppioctl(struct ppp_softc
*sc
, u_long cmd
, caddr_t data
,
373 int flag
, struct ucred
*cred
)
375 int error
, flags
, mru
, npx
;
377 struct ppp_option_data
*odp
;
378 struct compressor
**cp
;
382 struct bpf_program
*bp
, *nbp
;
383 struct bpf_insn
*newcode
, *oldcode
;
385 #endif /* PPP_FILTER */
387 u_char ccp_option
[CCP_MAX_OPTION_LENGTH
];
392 *(int *)data
= sc
->sc_inq
.ifq_len
;
396 *(int *)data
= sc
->sc_if
.if_dunit
;
400 *(u_int
*)data
= sc
->sc_flags
;
404 if ((error
= suser_cred(cred
, 0)) != 0)
406 flags
= *(int *)data
& SC_MASK
;
409 if (sc
->sc_flags
& SC_CCP_OPEN
&& !(flags
& SC_CCP_OPEN
))
412 sc
->sc_flags
= (sc
->sc_flags
& ~SC_MASK
) | flags
;
417 if ((error
= suser_cred(cred
, 0)) != 0)
420 if (mru
>= PPP_MRU
&& mru
<= PPP_MAXMRU
)
425 *(int *)data
= sc
->sc_mru
;
430 if ((error
= suser_cred(cred
, 0)) != 0)
434 sl_compress_init(sc
->sc_comp
, *(int *)data
);
441 if ((error
= suser_cred(cred
, 0)) != 0)
443 sc
->sc_xfer
= curthread
;
447 case PPPIOCSCOMPRESS
:
448 if ((error
= suser_cred(cred
, 0)) != 0)
450 odp
= (struct ppp_option_data
*) data
;
452 if (nb
> sizeof(ccp_option
))
453 nb
= sizeof(ccp_option
);
454 if ((error
= copyin(odp
->ptr
, ccp_option
, nb
)) != 0)
456 if (ccp_option
[1] < 2) /* preliminary check on the length byte */
458 for (cp
= ppp_compressors
; *cp
!= NULL
; ++cp
)
459 if ((*cp
)->compress_proto
== ccp_option
[0]) {
461 * Found a handler for the protocol - try to allocate
462 * a compressor or decompressor.
467 if (sc
->sc_xc_state
!= NULL
)
468 (*sc
->sc_xcomp
->comp_free
)(sc
->sc_xc_state
);
470 sc
->sc_xc_state
= (*cp
)->comp_alloc(ccp_option
, nb
);
471 if (sc
->sc_xc_state
== NULL
) {
472 if (sc
->sc_flags
& SC_DEBUG
)
473 kprintf("%s: comp_alloc failed\n",
477 sc
->sc_flags
&= ~SC_COMP_RUN
;
481 if (sc
->sc_rc_state
!= NULL
)
482 (*sc
->sc_rcomp
->decomp_free
)(sc
->sc_rc_state
);
484 sc
->sc_rc_state
= (*cp
)->decomp_alloc(ccp_option
, nb
);
485 if (sc
->sc_rc_state
== NULL
) {
486 if (sc
->sc_flags
& SC_DEBUG
)
487 kprintf("%s: decomp_alloc failed\n",
491 sc
->sc_flags
&= ~SC_DECOMP_RUN
;
496 if (sc
->sc_flags
& SC_DEBUG
)
497 kprintf("%s: no compressor for [%x %x %x], %x\n",
498 sc
->sc_if
.if_xname
, ccp_option
[0], ccp_option
[1],
500 return (EINVAL
); /* no handler found */
501 #endif /* PPP_COMPRESS */
505 npi
= (struct npioctl
*) data
;
506 switch (npi
->protocol
) {
513 if (cmd
== PPPIOCGNPMODE
) {
514 npi
->mode
= sc
->sc_npmode
[npx
];
516 if ((error
= suser_cred(cred
, 0)) != 0)
518 if (npi
->mode
!= sc
->sc_npmode
[npx
]) {
520 sc
->sc_npmode
[npx
] = npi
->mode
;
521 if (npi
->mode
!= NPMODE_QUEUE
) {
533 ((struct ppp_idle
*)data
)->xmit_idle
= t
- sc
->sc_last_sent
;
534 ((struct ppp_idle
*)data
)->recv_idle
= t
- sc
->sc_last_recv
;
541 nbp
= (struct bpf_program
*) data
;
542 if ((unsigned) nbp
->bf_len
> BPF_MAXINSNS
)
544 newcodelen
= nbp
->bf_len
* sizeof(struct bpf_insn
);
545 if (newcodelen
!= 0) {
546 MALLOC(newcode
, struct bpf_insn
*, newcodelen
, M_DEVBUF
, M_WAITOK
);
548 return EINVAL
; /* or sumpin */
550 if ((error
= copyin((caddr_t
)nbp
->bf_insns
, (caddr_t
)newcode
,
552 kfree(newcode
, M_DEVBUF
);
555 if (!bpf_validate(newcode
, nbp
->bf_len
)) {
556 kfree(newcode
, M_DEVBUF
);
561 bp
= (cmd
== PPPIOCSPASS
)? &sc
->sc_pass_filt
: &sc
->sc_active_filt
;
562 oldcode
= bp
->bf_insns
;
564 bp
->bf_len
= nbp
->bf_len
;
565 bp
->bf_insns
= newcode
;
568 kfree(oldcode
, M_DEVBUF
);
579 * Process an ioctl request to the ppp network interface.
582 pppsioctl(struct ifnet
*ifp
, u_long cmd
, caddr_t data
, struct ucred
*cr
)
584 struct ppp_softc
*sc
= &ppp_softc
[ifp
->if_dunit
];
585 struct ifaddr
*ifa
= (struct ifaddr
*)data
;
586 struct ifreq
*ifr
= (struct ifreq
*)data
;
587 struct ppp_stats
*psp
;
589 struct ppp_comp_stats
*pcp
;
597 if ((ifp
->if_flags
& IFF_RUNNING
) == 0)
598 ifp
->if_flags
&= ~IFF_UP
;
603 switch(ifa
->ifa_addr
->sa_family
) {
613 error
= EAFNOSUPPORT
;
619 switch(ifa
->ifa_addr
->sa_family
) {
629 error
= EAFNOSUPPORT
;
635 if ((error
= suser_cred(cr
, 0)) != 0)
637 if (ifr
->ifr_mtu
> PPP_MAXMTU
)
640 sc
->sc_if
.if_mtu
= ifr
->ifr_mtu
;
642 (*sc
->sc_setmtu
)(sc
);
647 ifr
->ifr_mtu
= sc
->sc_if
.if_mtu
;
653 error
= EAFNOSUPPORT
;
656 switch(ifr
->ifr_addr
.sa_family
) {
662 error
= EAFNOSUPPORT
;
668 psp
= &((struct ifpppstatsreq
*) data
)->stats
;
669 bzero(psp
, sizeof(*psp
));
670 psp
->p
= sc
->sc_stats
;
671 #if defined(VJC) && !defined(SL_NO_STATS)
673 psp
->vj
.vjs_packets
= sc
->sc_comp
->sls_packets
;
674 psp
->vj
.vjs_compressed
= sc
->sc_comp
->sls_compressed
;
675 psp
->vj
.vjs_searches
= sc
->sc_comp
->sls_searches
;
676 psp
->vj
.vjs_misses
= sc
->sc_comp
->sls_misses
;
677 psp
->vj
.vjs_uncompressedin
= sc
->sc_comp
->sls_uncompressedin
;
678 psp
->vj
.vjs_compressedin
= sc
->sc_comp
->sls_compressedin
;
679 psp
->vj
.vjs_errorin
= sc
->sc_comp
->sls_errorin
;
680 psp
->vj
.vjs_tossed
= sc
->sc_comp
->sls_tossed
;
687 pcp
= &((struct ifpppcstatsreq
*) data
)->stats
;
688 bzero(pcp
, sizeof(*pcp
));
689 if (sc
->sc_xc_state
!= NULL
)
690 (*sc
->sc_xcomp
->comp_stat
)(sc
->sc_xc_state
, &pcp
->c
);
691 if (sc
->sc_rc_state
!= NULL
)
692 (*sc
->sc_rcomp
->decomp_stat
)(sc
->sc_rc_state
, &pcp
->d
);
694 #endif /* PPP_COMPRESS */
704 * Queue a packet. Start transmission if not active.
705 * Packet is placed in Information field of PPP frame.
706 * Called at splnet as the if->if_output handler.
707 * Called at splnet from pppwrite().
710 pppoutput(struct ifnet
*ifp
, struct mbuf
*m0
, struct sockaddr
*dst
,
713 struct ppp_softc
*sc
= &ppp_softc
[ifp
->if_dunit
];
714 int protocol
, address
, control
;
722 struct altq_pktattr pktattr
;
724 if (sc
->sc_devp
== NULL
|| (ifp
->if_flags
& IFF_RUNNING
) == 0
725 || ((ifp
->if_flags
& IFF_UP
) == 0 && dst
->sa_family
!= AF_UNSPEC
)) {
726 error
= ENETDOWN
; /* sort of */
730 ifq_classify(&ifp
->if_snd
, m0
, dst
->sa_family
, &pktattr
);
733 * Compute PPP header.
735 m0
->m_flags
&= ~M_HIGHPRI
;
736 switch (dst
->sa_family
) {
739 address
= PPP_ALLSTATIONS
;
742 mode
= sc
->sc_npmode
[NP_IP
];
745 * If this packet has the "low delay" bit set in the IP header,
746 * put it on the fastq instead.
748 ip
= mtod(m0
, struct ip
*);
749 if (ip
->ip_tos
& IPTOS_LOWDELAY
)
750 m0
->m_flags
|= M_HIGHPRI
;
756 * This is pretty bogus.. We dont have an ipxcp module in pppd
757 * yet to configure the link parameters. Sigh. I guess a
758 * manual ifconfig would do.... -Peter
760 address
= PPP_ALLSTATIONS
;
767 address
= PPP_ADDRESS(dst
->sa_data
);
768 control
= PPP_CONTROL(dst
->sa_data
);
769 protocol
= PPP_PROTOCOL(dst
->sa_data
);
773 kprintf("%s: af%d not supported\n", ifp
->if_xname
, dst
->sa_family
);
774 error
= EAFNOSUPPORT
;
779 * Drop this packet, or return an error, if necessary.
781 if (mode
== NPMODE_ERROR
) {
785 if (mode
== NPMODE_DROP
) {
791 * Add PPP header. If no space in first mbuf, allocate another.
792 * (This assumes M_LEADINGSPACE is always 0 for a cluster mbuf.)
794 if (M_LEADINGSPACE(m0
) < PPP_HDRLEN
) {
795 m0
= m_prepend(m0
, PPP_HDRLEN
, MB_DONTWAIT
);
802 m0
->m_data
-= PPP_HDRLEN
;
804 cp
= mtod(m0
, u_char
*);
807 *cp
++ = protocol
>> 8;
808 *cp
++ = protocol
& 0xff;
809 m0
->m_len
+= PPP_HDRLEN
;
812 for (m
= m0
; m
!= 0; m
= m
->m_next
)
815 if (sc
->sc_flags
& SC_LOG_OUTPKT
) {
816 kprintf("%s output: ", ifp
->if_xname
);
820 if ((protocol
& 0x8000) == 0) {
823 * Apply the pass and active filters to the packet,
824 * but only if it is a data packet.
826 *mtod(m0
, u_char
*) = 1; /* indicates outbound */
827 if (sc
->sc_pass_filt
.bf_insns
!= 0
828 && bpf_filter(sc
->sc_pass_filt
.bf_insns
, (u_char
*) m0
,
830 error
= 0; /* drop this packet */
835 * Update the time we sent the most recent packet.
837 if (sc
->sc_active_filt
.bf_insns
== 0
838 || bpf_filter(sc
->sc_active_filt
.bf_insns
, (u_char
*) m0
, len
, 0))
839 sc
->sc_last_sent
= time_second
;
841 *mtod(m0
, u_char
*) = address
;
844 * Update the time we sent the most recent data packet.
846 sc
->sc_last_sent
= time_second
;
847 #endif /* PPP_FILTER */
853 * Put the packet on the appropriate queue.
856 if (mode
== NPMODE_QUEUE
) {
857 /* XXX we should limit the number of packets on this queue */
858 *sc
->sc_npqtail
= m0
;
859 m0
->m_nextpkt
= NULL
;
860 sc
->sc_npqtail
= &m0
->m_nextpkt
;
862 /* fastq and if_snd are emptied at spl[soft]net now */
863 if ((m0
->m_flags
& M_HIGHPRI
) && !ifq_is_enabled(&sc
->sc_if
.if_snd
)) {
865 if (IF_QFULL(ifq
) && dst
->sa_family
!= AF_UNSPEC
) {
874 ASSERT_SERIALIZED(sc
->sc_if
.if_serializer
);
875 error
= ifq_enqueue(&sc
->sc_if
.if_snd
, m0
, &pktattr
);
879 sc
->sc_if
.if_oerrors
++;
880 sc
->sc_stats
.ppp_oerrors
++;
885 getmicrotime(&ifp
->if_lastchange
);
887 ifp
->if_obytes
+= len
;
898 * After a change in the NPmode for some NP, move packets from the
899 * npqueue to the send queue or the fast queue as appropriate.
900 * Should be called at spl[soft]net.
903 ppp_requeue(struct ppp_softc
*sc
)
905 struct mbuf
*m
, **mpp
;
910 for (mpp
= &sc
->sc_npqueue
; (m
= *mpp
) != NULL
; ) {
911 switch (PPP_PROTOCOL(mtod(m
, u_char
*))) {
913 mode
= sc
->sc_npmode
[NP_IP
];
922 * This packet can now go on one of the queues to be sent.
926 if ((m
->m_flags
& M_HIGHPRI
) && !ifq_is_enabled(&sc
->sc_if
.if_snd
)) {
936 lwkt_serialize_enter(sc
->sc_if
.if_serializer
);
937 error
= ifq_enqueue(&sc
->sc_if
.if_snd
, m
, NULL
);
938 lwkt_serialize_exit(sc
->sc_if
.if_serializer
);
941 sc
->sc_if
.if_oerrors
++;
942 sc
->sc_stats
.ppp_oerrors
++;
957 sc
->sc_npqtail
= mpp
;
961 * Transmitter has finished outputting some stuff;
962 * remember to call sc->sc_start later at splsoftnet.
965 ppp_restart(struct ppp_softc
*sc
)
968 sc
->sc_flags
&= ~SC_TBUSY
;
969 schednetisr(NETISR_PPP
);
975 * Get a packet to send. This procedure is intended to be called at
976 * splsoftnet, since it may involve time-consuming operations such as
977 * applying VJ compression, packet compression, address/control and/or
978 * protocol field compression to the packet.
981 ppp_dequeue(struct ppp_softc
*sc
)
985 int address
, control
, protocol
;
988 * Grab a packet to send: first try the fast queue, then the
991 IF_DEQUEUE(&sc
->sc_fastq
, m
);
993 m
= ifq_dequeue(&sc
->sc_if
.if_snd
, NULL
);
997 ++sc
->sc_stats
.ppp_opackets
;
1000 * Extract the ppp header of the new packet.
1001 * The ppp header will be in one mbuf.
1003 cp
= mtod(m
, u_char
*);
1004 address
= PPP_ADDRESS(cp
);
1005 control
= PPP_CONTROL(cp
);
1006 protocol
= PPP_PROTOCOL(cp
);
1012 * If the packet is a TCP/IP packet, see if we can compress it.
1014 if ((sc
->sc_flags
& SC_COMP_TCP
) && sc
->sc_comp
!= NULL
) {
1019 ip
= (struct ip
*) (cp
+ PPP_HDRLEN
);
1020 if (mp
->m_len
<= PPP_HDRLEN
) {
1024 ip
= mtod(mp
, struct ip
*);
1026 /* this code assumes the IP/TCP header is in one non-shared mbuf */
1027 if (ip
->ip_p
== IPPROTO_TCP
) {
1028 type
= sl_compress_tcp(mp
, ip
, sc
->sc_comp
,
1029 !(sc
->sc_flags
& SC_NO_TCP_CCID
));
1031 case TYPE_UNCOMPRESSED_TCP
:
1032 protocol
= PPP_VJC_UNCOMP
;
1034 case TYPE_COMPRESSED_TCP
:
1035 protocol
= PPP_VJC_COMP
;
1036 cp
= mtod(m
, u_char
*);
1037 cp
[0] = address
; /* header has moved */
1042 cp
[3] = protocol
; /* update protocol in PPP header */
1052 #endif /* PPP_COMPRESS */
1056 if (protocol
!= PPP_LCP
&& protocol
!= PPP_CCP
1057 && sc
->sc_xc_state
&& (sc
->sc_flags
& SC_COMP_RUN
)) {
1058 struct mbuf
*mcomp
= NULL
;
1062 for (mp
= m
; mp
!= NULL
; mp
= mp
->m_next
)
1064 clen
= (*sc
->sc_xcomp
->compress
)
1065 (sc
->sc_xc_state
, &mcomp
, m
, slen
, sc
->sc_if
.if_mtu
+ PPP_HDRLEN
);
1066 if (mcomp
!= NULL
) {
1067 if (sc
->sc_flags
& SC_CCP_UP
) {
1068 /* Send the compressed packet instead of the original. */
1071 cp
= mtod(m
, u_char
*);
1074 /* Can't transmit compressed packets until CCP is up. */
1079 #endif /* PPP_COMPRESS */
1082 * Compress the address/control and protocol, if possible.
1084 if (sc
->sc_flags
& SC_COMP_AC
&& address
== PPP_ALLSTATIONS
&&
1085 control
== PPP_UI
&& protocol
!= PPP_ALLSTATIONS
&&
1086 protocol
!= PPP_LCP
) {
1087 /* can compress address/control */
1091 if (sc
->sc_flags
& SC_COMP_PROT
&& protocol
< 0xFF) {
1092 /* can compress protocol */
1093 if (mtod(m
, u_char
*) == cp
) {
1094 cp
[2] = cp
[1]; /* move address/control up */
1106 * Handle a CCP packet. `rcvd' is 1 if the packet was received,
1107 * 0 if it is about to be transmitted.
1110 ppp_ccp(struct ppp_softc
*sc
, struct mbuf
*m
, int rcvd
)
1117 * Get a pointer to the data after the PPP header.
1119 if (m
->m_len
<= PPP_HDRLEN
) {
1123 dp
= (mp
!= NULL
)? mtod(mp
, u_char
*): NULL
;
1126 dp
= mtod(mp
, u_char
*) + PPP_HDRLEN
;
1129 ep
= mtod(mp
, u_char
*) + mp
->m_len
;
1130 if (dp
+ CCP_HDRLEN
> ep
)
1132 slen
= CCP_LENGTH(dp
);
1133 if (dp
+ slen
> ep
) {
1134 if (sc
->sc_flags
& SC_DEBUG
)
1135 kprintf("if_ppp/ccp: not enough data in mbuf (%p+%x > %p+%x)\n",
1136 dp
, slen
, mtod(mp
, u_char
*), mp
->m_len
);
1140 switch (CCP_CODE(dp
)) {
1144 /* CCP must be going down - disable compression */
1145 if (sc
->sc_flags
& SC_CCP_UP
) {
1147 sc
->sc_flags
&= ~(SC_CCP_UP
| SC_COMP_RUN
| SC_DECOMP_RUN
);
1153 if (sc
->sc_flags
& SC_CCP_OPEN
&& !(sc
->sc_flags
& SC_CCP_UP
)
1154 && slen
>= CCP_HDRLEN
+ CCP_OPT_MINLEN
1155 && slen
>= CCP_OPT_LENGTH(dp
+ CCP_HDRLEN
) + CCP_HDRLEN
) {
1157 /* we're agreeing to send compressed packets. */
1158 if (sc
->sc_xc_state
!= NULL
1159 && (*sc
->sc_xcomp
->comp_init
)
1160 (sc
->sc_xc_state
, dp
+ CCP_HDRLEN
, slen
- CCP_HDRLEN
,
1161 sc
->sc_if
.if_dunit
, 0, sc
->sc_flags
& SC_DEBUG
)) {
1163 sc
->sc_flags
|= SC_COMP_RUN
;
1167 /* peer is agreeing to send compressed packets. */
1168 if (sc
->sc_rc_state
!= NULL
1169 && (*sc
->sc_rcomp
->decomp_init
)
1170 (sc
->sc_rc_state
, dp
+ CCP_HDRLEN
, slen
- CCP_HDRLEN
,
1171 sc
->sc_if
.if_dunit
, 0, sc
->sc_mru
,
1172 sc
->sc_flags
& SC_DEBUG
)) {
1174 sc
->sc_flags
|= SC_DECOMP_RUN
;
1175 sc
->sc_flags
&= ~(SC_DC_ERROR
| SC_DC_FERROR
);
1183 if (sc
->sc_flags
& SC_CCP_UP
) {
1185 if (sc
->sc_xc_state
&& (sc
->sc_flags
& SC_COMP_RUN
))
1186 (*sc
->sc_xcomp
->comp_reset
)(sc
->sc_xc_state
);
1188 if (sc
->sc_rc_state
&& (sc
->sc_flags
& SC_DECOMP_RUN
)) {
1189 (*sc
->sc_rcomp
->decomp_reset
)(sc
->sc_rc_state
);
1191 sc
->sc_flags
&= ~SC_DC_ERROR
;
1201 * CCP is down; free (de)compressor state if necessary.
1204 ppp_ccp_closed(struct ppp_softc
*sc
)
1206 if (sc
->sc_xc_state
) {
1207 (*sc
->sc_xcomp
->comp_free
)(sc
->sc_xc_state
);
1208 sc
->sc_xc_state
= NULL
;
1210 if (sc
->sc_rc_state
) {
1211 (*sc
->sc_rcomp
->decomp_free
)(sc
->sc_rc_state
);
1212 sc
->sc_rc_state
= NULL
;
1215 #endif /* PPP_COMPRESS */
1218 * PPP packet input routine.
1219 * The caller has checked and removed the FCS and has inserted
1220 * the address/control bytes and the protocol high byte if they
1224 ppppktin(struct ppp_softc
*sc
, struct mbuf
*m
, int lost
)
1229 m
->m_flags
|= M_ERRMARK
;
1230 IF_ENQUEUE(&sc
->sc_rawq
, m
);
1231 schednetisr(NETISR_PPP
);
1237 * Process a received PPP packet, doing decompression as necessary.
1238 * Should be called at splsoftnet.
1240 #define COMPTYPE(proto) ((proto) == PPP_VJC_COMP? TYPE_COMPRESSED_TCP: \
1241 TYPE_UNCOMPRESSED_TCP)
1244 ppp_inproc(struct ppp_softc
*sc
, struct mbuf
*m
)
1246 struct ifnet
*ifp
= &sc
->sc_if
;
1248 int ilen
= 0, xlen
, proto
, rv
;
1249 u_char
*cp
, adrs
, ctrl
;
1250 struct mbuf
*mp
, *dmp
= NULL
;
1254 ASSERT_SERIALIZED(ifp
->if_serializer
);
1256 sc
->sc_stats
.ppp_ipackets
++;
1258 if (sc
->sc_flags
& SC_LOG_INPKT
) {
1260 for (mp
= m
; mp
!= NULL
; mp
= mp
->m_next
)
1262 kprintf("%s: got %d bytes\n", ifp
->if_xname
, ilen
);
1266 cp
= mtod(m
, u_char
*);
1267 adrs
= PPP_ADDRESS(cp
);
1268 ctrl
= PPP_CONTROL(cp
);
1269 proto
= PPP_PROTOCOL(cp
);
1271 if (m
->m_flags
& M_ERRMARK
) {
1272 m
->m_flags
&= ~M_ERRMARK
;
1274 sc
->sc_flags
|= SC_VJ_RESET
;
1280 * Decompress this packet if necessary, update the receiver's
1281 * dictionary, or take appropriate action on a CCP packet.
1283 if (proto
== PPP_COMP
&& sc
->sc_rc_state
&& (sc
->sc_flags
& SC_DECOMP_RUN
)
1284 && !(sc
->sc_flags
& SC_DC_ERROR
) && !(sc
->sc_flags
& SC_DC_FERROR
)) {
1285 /* decompress this packet */
1286 rv
= (*sc
->sc_rcomp
->decompress
)(sc
->sc_rc_state
, m
, &dmp
);
1287 if (rv
== DECOMP_OK
) {
1290 /* no error, but no decompressed packet produced */
1294 cp
= mtod(m
, u_char
*);
1295 proto
= PPP_PROTOCOL(cp
);
1299 * An error has occurred in decompression.
1300 * Pass the compressed packet up to pppd, which may take
1301 * CCP down or issue a Reset-Req.
1303 if (sc
->sc_flags
& SC_DEBUG
)
1304 kprintf("%s: decompress failed %d\n", ifp
->if_xname
, rv
);
1306 sc
->sc_flags
|= SC_VJ_RESET
;
1307 if (rv
== DECOMP_ERROR
)
1308 sc
->sc_flags
|= SC_DC_ERROR
;
1310 sc
->sc_flags
|= SC_DC_FERROR
;
1315 if (sc
->sc_rc_state
&& (sc
->sc_flags
& SC_DECOMP_RUN
)) {
1316 (*sc
->sc_rcomp
->incomp
)(sc
->sc_rc_state
, m
);
1318 if (proto
== PPP_CCP
) {
1325 for (mp
= m
; mp
!= NULL
; mp
= mp
->m_next
)
1329 if (sc
->sc_flags
& SC_VJ_RESET
) {
1331 * If we've missed a packet, we must toss subsequent compressed
1332 * packets which don't have an explicit connection ID.
1335 sl_uncompress_tcp(NULL
, 0, TYPE_ERROR
, sc
->sc_comp
);
1337 sc
->sc_flags
&= ~SC_VJ_RESET
;
1342 * See if we have a VJ-compressed packet to uncompress.
1344 if (proto
== PPP_VJC_COMP
) {
1345 if ((sc
->sc_flags
& SC_REJ_COMP_TCP
) || sc
->sc_comp
== 0)
1348 xlen
= sl_uncompress_tcp_core(cp
+ PPP_HDRLEN
, m
->m_len
- PPP_HDRLEN
,
1349 ilen
- PPP_HDRLEN
, TYPE_COMPRESSED_TCP
,
1350 sc
->sc_comp
, &iphdr
, &hlen
);
1353 if (sc
->sc_flags
& SC_DEBUG
)
1354 kprintf("%s: VJ uncompress failed on type comp\n",
1359 /* Copy the PPP and IP headers into a new mbuf. */
1360 MGETHDR(mp
, MB_DONTWAIT
, MT_DATA
);
1365 if (hlen
+ PPP_HDRLEN
> MHLEN
) {
1366 MCLGET(mp
, MB_DONTWAIT
);
1367 if (M_TRAILINGSPACE(mp
) < hlen
+ PPP_HDRLEN
) {
1369 goto bad
; /* lose if big headers and no clusters */
1372 cp
= mtod(mp
, u_char
*);
1378 bcopy(iphdr
, cp
+ PPP_HDRLEN
, hlen
);
1379 mp
->m_len
= hlen
+ PPP_HDRLEN
;
1382 * Trim the PPP and VJ headers off the old mbuf
1383 * and stick the new and old mbufs together.
1385 m
->m_data
+= PPP_HDRLEN
+ xlen
;
1386 m
->m_len
-= PPP_HDRLEN
+ xlen
;
1387 if (m
->m_len
<= M_TRAILINGSPACE(mp
)) {
1388 bcopy(mtod(m
, u_char
*), mtod(mp
, u_char
*) + mp
->m_len
, m
->m_len
);
1389 mp
->m_len
+= m
->m_len
;
1390 mp
->m_next
= m_free(m
);
1395 ilen
+= hlen
- xlen
;
1397 } else if (proto
== PPP_VJC_UNCOMP
) {
1398 if ((sc
->sc_flags
& SC_REJ_COMP_TCP
) || sc
->sc_comp
== 0)
1401 xlen
= sl_uncompress_tcp_core(cp
+ PPP_HDRLEN
, m
->m_len
- PPP_HDRLEN
,
1402 ilen
- PPP_HDRLEN
, TYPE_UNCOMPRESSED_TCP
,
1403 sc
->sc_comp
, &iphdr
, &hlen
);
1406 if (sc
->sc_flags
& SC_DEBUG
)
1407 kprintf("%s: VJ uncompress failed on type uncomp\n",
1418 * If the packet will fit in a header mbuf, don't waste a
1419 * whole cluster on it.
1421 if (ilen
<= MHLEN
&& M_IS_CLUSTER(m
)) {
1422 MGETHDR(mp
, MB_DONTWAIT
, MT_DATA
);
1424 m_copydata(m
, 0, ilen
, mtod(mp
, caddr_t
));
1430 m
->m_pkthdr
.len
= ilen
;
1431 m
->m_pkthdr
.rcvif
= ifp
;
1433 if ((proto
& 0x8000) == 0) {
1436 * See whether we want to pass this packet, and
1437 * if it counts as link activity.
1439 adrs
= *mtod(m
, u_char
*); /* save address field */
1440 *mtod(m
, u_char
*) = 0; /* indicate inbound */
1441 if (sc
->sc_pass_filt
.bf_insns
!= 0
1442 && bpf_filter(sc
->sc_pass_filt
.bf_insns
, (u_char
*) m
,
1444 /* drop this packet */
1448 if (sc
->sc_active_filt
.bf_insns
== 0
1449 || bpf_filter(sc
->sc_active_filt
.bf_insns
, (u_char
*) m
, ilen
, 0))
1450 sc
->sc_last_recv
= time_second
;
1452 *mtod(m
, u_char
*) = adrs
;
1455 * Record the time that we received this packet.
1457 sc
->sc_last_recv
= time_second
;
1458 #endif /* PPP_FILTER */
1461 BPF_MTAP(&sc
->sc_if
, m
);
1468 * IP packet - take off the ppp header and pass it up to IP.
1470 if ((ifp
->if_flags
& IFF_UP
) == 0
1471 || sc
->sc_npmode
[NP_IP
] != NPMODE_PASS
) {
1472 /* interface is down - drop the packet. */
1476 m
->m_pkthdr
.len
-= PPP_HDRLEN
;
1477 m
->m_data
+= PPP_HDRLEN
;
1478 m
->m_len
-= PPP_HDRLEN
;
1479 if (ipflow_fastforward(m
, ifp
->if_serializer
))
1487 * IPX packet - take off the ppp header and pass it up to IPX.
1489 if ((sc
->sc_if
.if_flags
& IFF_UP
) == 0
1490 /* XXX: || sc->sc_npmode[NP_IPX] != NPMODE_PASS*/) {
1491 /* interface is down - drop the packet. */
1495 m
->m_pkthdr
.len
-= PPP_HDRLEN
;
1496 m
->m_data
+= PPP_HDRLEN
;
1497 m
->m_len
-= PPP_HDRLEN
;
1499 sc
->sc_last_recv
= time_second
; /* update time of last pkt rcvd */
1505 * Some other protocol - place on input queue for read().
1511 * If we found a netproto just pass it to the proto. Otherwise queue
1512 * the packet to the tty (e.g. pppd). Note that sc_ctlp() must be
1513 * called EXACTLY once per packet queued.
1516 rv
= IF_HANDOFF(&sc
->sc_inq
, m
, NULL
);
1520 rv
= (netisr_queue(isr
, m
) == 0);
1523 if (sc
->sc_flags
& SC_DEBUG
)
1524 kprintf("%s: input queue full\n", ifp
->if_xname
);
1530 ifp
->if_ibytes
+= ilen
;
1531 getmicrotime(&ifp
->if_lastchange
);
1537 sc
->sc_if
.if_ierrors
++;
1538 sc
->sc_stats
.ppp_ierrors
++;
1541 #define MAX_DUMP_BYTES 128
1544 pppdumpm(struct mbuf
*m0
)
1546 char buf
[3*MAX_DUMP_BYTES
+4];
1550 for (m
= m0
; m
; m
= m
->m_next
) {
1552 u_char
*rptr
= (u_char
*)m
->m_data
;
1555 if (bp
> buf
+ sizeof(buf
) - 4)
1557 *bp
++ = hex2ascii(*rptr
>> 4);
1558 *bp
++ = hex2ascii(*rptr
++ & 0xf);
1562 if (bp
> buf
+ sizeof(buf
) - 3)
1572 kprintf("%s\n", buf
);
1576 * a wrapper to transmit a packet from if_start since ALTQ uses
1577 * if_start to send a packet.
1580 ppp_ifstart(struct ifnet
*ifp
)
1582 struct ppp_softc
*sc
;
1585 (*sc
->sc_start
)(sc
);