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.24 2005/02/11 22:25:57 joerg 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)
202 struct mbuf *m = ((struct netmsg_packet *)msg)->nm_packet;
203 struct ppp_softc *sc;
207 for (i = 0; i < NPPP; ++i, ++sc) {
209 if (!(sc->sc_flags & SC_TBUSY)
210 && (!ifq_is_empty(&sc->sc_if.if_snd) || !IF_QEMPTY(&sc->sc_fastq))) {
211 sc->sc_flags |= SC_TBUSY;
218 IF_DEQUEUE(&sc->sc_rawq, m);
225 lwkt_replymsg(&msg->nm_lmsg, 0);
230 * Called from boot code to establish ppp interfaces.
236 struct ppp_softc *sc;
239 for (sc = ppp_softc; i < NPPP; sc++) {
240 if_initname(&(sc->sc_if), "ppp", i++);
241 sc->sc_if.if_mtu = PPP_MTU;
242 sc->sc_if.if_flags = IFF_POINTOPOINT | IFF_MULTICAST;
243 sc->sc_if.if_type = IFT_PPP;
244 sc->sc_if.if_hdrlen = PPP_HDRLEN;
245 sc->sc_if.if_ioctl = pppsioctl;
246 sc->sc_if.if_output = pppoutput;
247 sc->sc_if.if_start = ppp_ifstart;
248 ifq_set_maxlen(&sc->sc_if.if_snd, IFQ_MAXLEN);
249 ifq_set_ready(&sc->sc_if.if_snd);
250 sc->sc_inq.ifq_maxlen = IFQ_MAXLEN;
251 sc->sc_fastq.ifq_maxlen = IFQ_MAXLEN;
252 sc->sc_rawq.ifq_maxlen = IFQ_MAXLEN;
253 callout_init(&sc->sc_timeout);
254 if_attach(&sc->sc_if);
255 bpfattach(&sc->sc_if, DLT_PPP, PPP_HDRLEN);
257 netisr_register(NETISR_PPP, cpu0_portfn, pppintr);
259 * XXX layering violation - if_ppp can work over any lower level
260 * transport that cares to attach to it.
262 pppasyncattach(dummy);
266 * Allocate a ppp interface unit and initialize it.
269 pppalloc(struct thread *td)
272 struct ppp_softc *sc;
274 for (nppp = 0, sc = ppp_softc; nppp < NPPP; nppp++, sc++)
275 if (sc->sc_xfer == td) {
279 for (nppp = 0, sc = ppp_softc; nppp < NPPP; nppp++, sc++)
280 if (sc->sc_devp == NULL)
286 sc->sc_mru = PPP_MRU;
287 sc->sc_relinq = NULL;
288 bzero((char *)&sc->sc_stats, sizeof(sc->sc_stats));
290 MALLOC(sc->sc_comp, struct slcompress *, sizeof(struct slcompress),
292 sl_compress_init(sc->sc_comp, -1);
295 sc->sc_xc_state = NULL;
296 sc->sc_rc_state = NULL;
297 #endif /* PPP_COMPRESS */
298 for (i = 0; i < NUM_NP; ++i)
299 sc->sc_npmode[i] = NPMODE_ERROR;
300 sc->sc_npqueue = NULL;
301 sc->sc_npqtail = &sc->sc_npqueue;
302 sc->sc_last_sent = sc->sc_last_recv = time_second;
308 * Deallocate a ppp unit. Must be called at splsoftnet or higher.
312 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 free(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 free(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 free(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 thread *td)
375 int s, 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(td)) != 0)
406 flags = *(int *)data & SC_MASK;
409 if (sc->sc_flags & SC_CCP_OPEN && !(flags & SC_CCP_OPEN))
413 sc->sc_flags = (sc->sc_flags & ~SC_MASK) | flags;
418 if ((error = suser(td)) != 0)
421 if (mru >= PPP_MRU && mru <= PPP_MAXMRU)
426 *(int *)data = sc->sc_mru;
431 if ((error = suser(td)) != 0)
435 sl_compress_init(sc->sc_comp, *(int *)data);
442 if ((error = suser(td)) != 0)
448 case PPPIOCSCOMPRESS:
449 if ((error = suser(td)) != 0)
451 odp = (struct ppp_option_data *) data;
453 if (nb > sizeof(ccp_option))
454 nb = sizeof(ccp_option);
455 if ((error = copyin(odp->ptr, ccp_option, nb)) != 0)
457 if (ccp_option[1] < 2) /* preliminary check on the length byte */
459 for (cp = ppp_compressors; *cp != NULL; ++cp)
460 if ((*cp)->compress_proto == ccp_option[0]) {
462 * Found a handler for the protocol - try to allocate
463 * a compressor or decompressor.
468 if (sc->sc_xc_state != NULL)
469 (*sc->sc_xcomp->comp_free)(sc->sc_xc_state);
471 sc->sc_xc_state = (*cp)->comp_alloc(ccp_option, nb);
472 if (sc->sc_xc_state == NULL) {
473 if (sc->sc_flags & SC_DEBUG)
474 printf("%s: comp_alloc failed\n",
479 sc->sc_flags &= ~SC_COMP_RUN;
483 if (sc->sc_rc_state != NULL)
484 (*sc->sc_rcomp->decomp_free)(sc->sc_rc_state);
486 sc->sc_rc_state = (*cp)->decomp_alloc(ccp_option, nb);
487 if (sc->sc_rc_state == NULL) {
488 if (sc->sc_flags & SC_DEBUG)
489 printf("%s: decomp_alloc failed\n",
494 sc->sc_flags &= ~SC_DECOMP_RUN;
499 if (sc->sc_flags & SC_DEBUG)
500 printf("%s: no compressor for [%x %x %x], %x\n",
501 sc->sc_if.if_xname, ccp_option[0], ccp_option[1],
503 return (EINVAL); /* no handler found */
504 #endif /* PPP_COMPRESS */
508 npi = (struct npioctl *) data;
509 switch (npi->protocol) {
516 if (cmd == PPPIOCGNPMODE) {
517 npi->mode = sc->sc_npmode[npx];
519 if ((error = suser(td)) != 0)
521 if (npi->mode != sc->sc_npmode[npx]) {
523 sc->sc_npmode[npx] = npi->mode;
524 if (npi->mode != NPMODE_QUEUE) {
536 ((struct ppp_idle *)data)->xmit_idle = t - sc->sc_last_sent;
537 ((struct ppp_idle *)data)->recv_idle = t - sc->sc_last_recv;
544 nbp = (struct bpf_program *) data;
545 if ((unsigned) nbp->bf_len > BPF_MAXINSNS)
547 newcodelen = nbp->bf_len * sizeof(struct bpf_insn);
548 if (newcodelen != 0) {
549 MALLOC(newcode, struct bpf_insn *, newcodelen, M_DEVBUF, M_WAITOK);
551 return EINVAL; /* or sumpin */
553 if ((error = copyin((caddr_t)nbp->bf_insns, (caddr_t)newcode,
555 free(newcode, M_DEVBUF);
558 if (!bpf_validate(newcode, nbp->bf_len)) {
559 free(newcode, M_DEVBUF);
564 bp = (cmd == PPPIOCSPASS)? &sc->sc_pass_filt: &sc->sc_active_filt;
565 oldcode = bp->bf_insns;
567 bp->bf_len = nbp->bf_len;
568 bp->bf_insns = newcode;
571 free(oldcode, M_DEVBUF);
582 * Process an ioctl request to the ppp network interface.
585 pppsioctl(ifp, cmd, data, cr)
591 struct thread *td = curthread; /* XXX */
592 struct ppp_softc *sc = &ppp_softc[ifp->if_dunit];
593 struct ifaddr *ifa = (struct ifaddr *)data;
594 struct ifreq *ifr = (struct ifreq *)data;
595 struct ppp_stats *psp;
597 struct ppp_comp_stats *pcp;
599 int s = splimp(), error = 0;
603 if ((ifp->if_flags & IFF_RUNNING) == 0)
604 ifp->if_flags &= ~IFF_UP;
609 switch(ifa->ifa_addr->sa_family) {
619 error = EAFNOSUPPORT;
625 switch(ifa->ifa_addr->sa_family) {
635 error = EAFNOSUPPORT;
641 if ((error = suser(td)) != 0)
643 if (ifr->ifr_mtu > PPP_MAXMTU)
646 sc->sc_if.if_mtu = ifr->ifr_mtu;
648 (*sc->sc_setmtu)(sc);
653 ifr->ifr_mtu = sc->sc_if.if_mtu;
659 error = EAFNOSUPPORT;
662 switch(ifr->ifr_addr.sa_family) {
668 error = EAFNOSUPPORT;
674 psp = &((struct ifpppstatsreq *) data)->stats;
675 bzero(psp, sizeof(*psp));
676 psp->p = sc->sc_stats;
677 #if defined(VJC) && !defined(SL_NO_STATS)
679 psp->vj.vjs_packets = sc->sc_comp->sls_packets;
680 psp->vj.vjs_compressed = sc->sc_comp->sls_compressed;
681 psp->vj.vjs_searches = sc->sc_comp->sls_searches;
682 psp->vj.vjs_misses = sc->sc_comp->sls_misses;
683 psp->vj.vjs_uncompressedin = sc->sc_comp->sls_uncompressedin;
684 psp->vj.vjs_compressedin = sc->sc_comp->sls_compressedin;
685 psp->vj.vjs_errorin = sc->sc_comp->sls_errorin;
686 psp->vj.vjs_tossed = sc->sc_comp->sls_tossed;
693 pcp = &((struct ifpppcstatsreq *) data)->stats;
694 bzero(pcp, sizeof(*pcp));
695 if (sc->sc_xc_state != NULL)
696 (*sc->sc_xcomp->comp_stat)(sc->sc_xc_state, &pcp->c);
697 if (sc->sc_rc_state != NULL)
698 (*sc->sc_rcomp->decomp_stat)(sc->sc_rc_state, &pcp->d);
700 #endif /* PPP_COMPRESS */
710 * Queue a packet. Start transmission if not active.
711 * Packet is placed in Information field of PPP frame.
712 * Called at splnet as the if->if_output handler.
713 * Called at splnet from pppwrite().
716 pppoutput(ifp, m0, dst, rtp)
719 struct sockaddr *dst;
722 struct ppp_softc *sc = &ppp_softc[ifp->if_dunit];
723 int protocol, address, control;
731 struct altq_pktattr pktattr;
733 if (sc->sc_devp == NULL || (ifp->if_flags & IFF_RUNNING) == 0
734 || ((ifp->if_flags & IFF_UP) == 0 && dst->sa_family != AF_UNSPEC)) {
735 error = ENETDOWN; /* sort of */
739 ifq_classify(&ifp->if_snd, m0, dst->sa_family, &pktattr);
742 * Compute PPP header.
744 m0->m_flags &= ~M_HIGHPRI;
745 switch (dst->sa_family) {
748 address = PPP_ALLSTATIONS;
751 mode = sc->sc_npmode[NP_IP];
754 * If this packet has the "low delay" bit set in the IP header,
755 * put it on the fastq instead.
757 ip = mtod(m0, struct ip *);
758 if (ip->ip_tos & IPTOS_LOWDELAY)
759 m0->m_flags |= M_HIGHPRI;
765 * This is pretty bogus.. We dont have an ipxcp module in pppd
766 * yet to configure the link parameters. Sigh. I guess a
767 * manual ifconfig would do.... -Peter
769 address = PPP_ALLSTATIONS;
776 address = PPP_ADDRESS(dst->sa_data);
777 control = PPP_CONTROL(dst->sa_data);
778 protocol = PPP_PROTOCOL(dst->sa_data);
782 printf("%s: af%d not supported\n", ifp->if_xname, dst->sa_family);
783 error = EAFNOSUPPORT;
788 * Drop this packet, or return an error, if necessary.
790 if (mode == NPMODE_ERROR) {
794 if (mode == NPMODE_DROP) {
800 * Add PPP header. If no space in first mbuf, allocate another.
801 * (This assumes M_LEADINGSPACE is always 0 for a cluster mbuf.)
803 if (M_LEADINGSPACE(m0) < PPP_HDRLEN) {
804 m0 = m_prepend(m0, PPP_HDRLEN, MB_DONTWAIT);
811 m0->m_data -= PPP_HDRLEN;
813 cp = mtod(m0, u_char *);
816 *cp++ = protocol >> 8;
817 *cp++ = protocol & 0xff;
818 m0->m_len += PPP_HDRLEN;
821 for (m = m0; m != 0; m = m->m_next)
824 if (sc->sc_flags & SC_LOG_OUTPKT) {
825 printf("%s output: ", ifp->if_xname);
829 if ((protocol & 0x8000) == 0) {
832 * Apply the pass and active filters to the packet,
833 * but only if it is a data packet.
835 *mtod(m0, u_char *) = 1; /* indicates outbound */
836 if (sc->sc_pass_filt.bf_insns != 0
837 && bpf_filter(sc->sc_pass_filt.bf_insns, (u_char *) m0,
839 error = 0; /* drop this packet */
844 * Update the time we sent the most recent packet.
846 if (sc->sc_active_filt.bf_insns == 0
847 || bpf_filter(sc->sc_active_filt.bf_insns, (u_char *) m0, len, 0))
848 sc->sc_last_sent = time_second;
850 *mtod(m0, u_char *) = address;
853 * Update the time we sent the most recent data packet.
855 sc->sc_last_sent = time_second;
856 #endif /* PPP_FILTER */
862 * Put the packet on the appropriate queue.
864 s = splsoftnet(); /* redundant */
865 if (mode == NPMODE_QUEUE) {
866 /* XXX we should limit the number of packets on this queue */
867 *sc->sc_npqtail = m0;
868 m0->m_nextpkt = NULL;
869 sc->sc_npqtail = &m0->m_nextpkt;
871 /* fastq and if_snd are emptied at spl[soft]net now */
872 if ((m0->m_flags & M_HIGHPRI) && !ifq_is_enabled(&sc->sc_if.if_snd)) {
874 if (IF_QFULL(ifq) && dst->sa_family != AF_UNSPEC) {
883 error = ifq_enqueue(&sc->sc_if.if_snd, m0, &pktattr);
887 sc->sc_if.if_oerrors++;
888 sc->sc_stats.ppp_oerrors++;
893 getmicrotime(&ifp->if_lastchange);
895 ifp->if_obytes += len;
906 * After a change in the NPmode for some NP, move packets from the
907 * npqueue to the send queue or the fast queue as appropriate.
908 * Should be called at spl[soft]net.
912 struct ppp_softc *sc;
914 struct mbuf *m, **mpp;
919 for (mpp = &sc->sc_npqueue; (m = *mpp) != NULL; ) {
920 switch (PPP_PROTOCOL(mtod(m, u_char *))) {
922 mode = sc->sc_npmode[NP_IP];
931 * This packet can now go on one of the queues to be sent.
935 if ((m->m_flags & M_HIGHPRI) && !ifq_is_enabled(&sc->sc_if.if_snd)) {
945 error = ifq_enqueue(&sc->sc_if.if_snd, m, NULL);
948 sc->sc_if.if_oerrors++;
949 sc->sc_stats.ppp_oerrors++;
964 sc->sc_npqtail = mpp;
968 * Transmitter has finished outputting some stuff;
969 * remember to call sc->sc_start later at splsoftnet.
973 struct ppp_softc *sc;
977 sc->sc_flags &= ~SC_TBUSY;
978 schednetisr(NETISR_PPP);
984 * Get a packet to send. This procedure is intended to be called at
985 * splsoftnet, since it may involve time-consuming operations such as
986 * applying VJ compression, packet compression, address/control and/or
987 * protocol field compression to the packet.
991 struct ppp_softc *sc;
995 int address, control, protocol;
998 * Grab a packet to send: first try the fast queue, then the
1001 IF_DEQUEUE(&sc->sc_fastq, m);
1003 m = ifq_dequeue(&sc->sc_if.if_snd);
1007 ++sc->sc_stats.ppp_opackets;
1010 * Extract the ppp header of the new packet.
1011 * The ppp header will be in one mbuf.
1013 cp = mtod(m, u_char *);
1014 address = PPP_ADDRESS(cp);
1015 control = PPP_CONTROL(cp);
1016 protocol = PPP_PROTOCOL(cp);
1022 * If the packet is a TCP/IP packet, see if we can compress it.
1024 if ((sc->sc_flags & SC_COMP_TCP) && sc->sc_comp != NULL) {
1029 ip = (struct ip *) (cp + PPP_HDRLEN);
1030 if (mp->m_len <= PPP_HDRLEN) {
1034 ip = mtod(mp, struct ip *);
1036 /* this code assumes the IP/TCP header is in one non-shared mbuf */
1037 if (ip->ip_p == IPPROTO_TCP) {
1038 type = sl_compress_tcp(mp, ip, sc->sc_comp,
1039 !(sc->sc_flags & SC_NO_TCP_CCID));
1041 case TYPE_UNCOMPRESSED_TCP:
1042 protocol = PPP_VJC_UNCOMP;
1044 case TYPE_COMPRESSED_TCP:
1045 protocol = PPP_VJC_COMP;
1046 cp = mtod(m, u_char *);
1047 cp[0] = address; /* header has moved */
1052 cp[3] = protocol; /* update protocol in PPP header */
1062 #endif /* PPP_COMPRESS */
1066 if (protocol != PPP_LCP && protocol != PPP_CCP
1067 && sc->sc_xc_state && (sc->sc_flags & SC_COMP_RUN)) {
1068 struct mbuf *mcomp = NULL;
1072 for (mp = m; mp != NULL; mp = mp->m_next)
1074 clen = (*sc->sc_xcomp->compress)
1075 (sc->sc_xc_state, &mcomp, m, slen, sc->sc_if.if_mtu + PPP_HDRLEN);
1076 if (mcomp != NULL) {
1077 if (sc->sc_flags & SC_CCP_UP) {
1078 /* Send the compressed packet instead of the original. */
1081 cp = mtod(m, u_char *);
1084 /* Can't transmit compressed packets until CCP is up. */
1089 #endif /* PPP_COMPRESS */
1092 * Compress the address/control and protocol, if possible.
1094 if (sc->sc_flags & SC_COMP_AC && address == PPP_ALLSTATIONS &&
1095 control == PPP_UI && protocol != PPP_ALLSTATIONS &&
1096 protocol != PPP_LCP) {
1097 /* can compress address/control */
1101 if (sc->sc_flags & SC_COMP_PROT && protocol < 0xFF) {
1102 /* can compress protocol */
1103 if (mtod(m, u_char *) == cp) {
1104 cp[2] = cp[1]; /* move address/control up */
1116 * Handle a CCP packet. `rcvd' is 1 if the packet was received,
1117 * 0 if it is about to be transmitted.
1120 ppp_ccp(sc, m, rcvd)
1121 struct ppp_softc *sc;
1130 * Get a pointer to the data after the PPP header.
1132 if (m->m_len <= PPP_HDRLEN) {
1136 dp = (mp != NULL)? mtod(mp, u_char *): NULL;
1139 dp = mtod(mp, u_char *) + PPP_HDRLEN;
1142 ep = mtod(mp, u_char *) + mp->m_len;
1143 if (dp + CCP_HDRLEN > ep)
1145 slen = CCP_LENGTH(dp);
1146 if (dp + slen > ep) {
1147 if (sc->sc_flags & SC_DEBUG)
1148 printf("if_ppp/ccp: not enough data in mbuf (%p+%x > %p+%x)\n",
1149 dp, slen, mtod(mp, u_char *), mp->m_len);
1153 switch (CCP_CODE(dp)) {
1157 /* CCP must be going down - disable compression */
1158 if (sc->sc_flags & SC_CCP_UP) {
1160 sc->sc_flags &= ~(SC_CCP_UP | SC_COMP_RUN | SC_DECOMP_RUN);
1166 if (sc->sc_flags & SC_CCP_OPEN && !(sc->sc_flags & SC_CCP_UP)
1167 && slen >= CCP_HDRLEN + CCP_OPT_MINLEN
1168 && slen >= CCP_OPT_LENGTH(dp + CCP_HDRLEN) + CCP_HDRLEN) {
1170 /* we're agreeing to send compressed packets. */
1171 if (sc->sc_xc_state != NULL
1172 && (*sc->sc_xcomp->comp_init)
1173 (sc->sc_xc_state, dp + CCP_HDRLEN, slen - CCP_HDRLEN,
1174 sc->sc_if.if_dunit, 0, sc->sc_flags & SC_DEBUG)) {
1176 sc->sc_flags |= SC_COMP_RUN;
1180 /* peer is agreeing to send compressed packets. */
1181 if (sc->sc_rc_state != NULL
1182 && (*sc->sc_rcomp->decomp_init)
1183 (sc->sc_rc_state, dp + CCP_HDRLEN, slen - CCP_HDRLEN,
1184 sc->sc_if.if_dunit, 0, sc->sc_mru,
1185 sc->sc_flags & SC_DEBUG)) {
1187 sc->sc_flags |= SC_DECOMP_RUN;
1188 sc->sc_flags &= ~(SC_DC_ERROR | SC_DC_FERROR);
1196 if (sc->sc_flags & SC_CCP_UP) {
1198 if (sc->sc_xc_state && (sc->sc_flags & SC_COMP_RUN))
1199 (*sc->sc_xcomp->comp_reset)(sc->sc_xc_state);
1201 if (sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)) {
1202 (*sc->sc_rcomp->decomp_reset)(sc->sc_rc_state);
1204 sc->sc_flags &= ~SC_DC_ERROR;
1214 * CCP is down; free (de)compressor state if necessary.
1218 struct ppp_softc *sc;
1220 if (sc->sc_xc_state) {
1221 (*sc->sc_xcomp->comp_free)(sc->sc_xc_state);
1222 sc->sc_xc_state = NULL;
1224 if (sc->sc_rc_state) {
1225 (*sc->sc_rcomp->decomp_free)(sc->sc_rc_state);
1226 sc->sc_rc_state = NULL;
1229 #endif /* PPP_COMPRESS */
1232 * PPP packet input routine.
1233 * The caller has checked and removed the FCS and has inserted
1234 * the address/control bytes and the protocol high byte if they
1238 ppppktin(sc, m, lost)
1239 struct ppp_softc *sc;
1246 m->m_flags |= M_ERRMARK;
1247 IF_ENQUEUE(&sc->sc_rawq, m);
1248 schednetisr(NETISR_PPP);
1253 * Process a received PPP packet, doing decompression as necessary.
1254 * Should be called at splsoftnet.
1256 #define COMPTYPE(proto) ((proto) == PPP_VJC_COMP? TYPE_COMPRESSED_TCP: \
1257 TYPE_UNCOMPRESSED_TCP)
1261 struct ppp_softc *sc;
1264 struct ifnet *ifp = &sc->sc_if;
1266 int s, ilen = 0, xlen, proto, rv;
1267 u_char *cp, adrs, ctrl;
1268 struct mbuf *mp, *dmp = NULL;
1272 sc->sc_stats.ppp_ipackets++;
1274 if (sc->sc_flags & SC_LOG_INPKT) {
1276 for (mp = m; mp != NULL; mp = mp->m_next)
1278 printf("%s: got %d bytes\n", ifp->if_xname, ilen);
1282 cp = mtod(m, u_char *);
1283 adrs = PPP_ADDRESS(cp);
1284 ctrl = PPP_CONTROL(cp);
1285 proto = PPP_PROTOCOL(cp);
1287 if (m->m_flags & M_ERRMARK) {
1288 m->m_flags &= ~M_ERRMARK;
1290 sc->sc_flags |= SC_VJ_RESET;
1296 * Decompress this packet if necessary, update the receiver's
1297 * dictionary, or take appropriate action on a CCP packet.
1299 if (proto == PPP_COMP && sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)
1300 && !(sc->sc_flags & SC_DC_ERROR) && !(sc->sc_flags & SC_DC_FERROR)) {
1301 /* decompress this packet */
1302 rv = (*sc->sc_rcomp->decompress)(sc->sc_rc_state, m, &dmp);
1303 if (rv == DECOMP_OK) {
1306 /* no error, but no decompressed packet produced */
1310 cp = mtod(m, u_char *);
1311 proto = PPP_PROTOCOL(cp);
1315 * An error has occurred in decompression.
1316 * Pass the compressed packet up to pppd, which may take
1317 * CCP down or issue a Reset-Req.
1319 if (sc->sc_flags & SC_DEBUG)
1320 printf("%s: decompress failed %d\n", ifp->if_xname, rv);
1322 sc->sc_flags |= SC_VJ_RESET;
1323 if (rv == DECOMP_ERROR)
1324 sc->sc_flags |= SC_DC_ERROR;
1326 sc->sc_flags |= SC_DC_FERROR;
1331 if (sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)) {
1332 (*sc->sc_rcomp->incomp)(sc->sc_rc_state, m);
1334 if (proto == PPP_CCP) {
1341 for (mp = m; mp != NULL; mp = mp->m_next)
1345 if (sc->sc_flags & SC_VJ_RESET) {
1347 * If we've missed a packet, we must toss subsequent compressed
1348 * packets which don't have an explicit connection ID.
1351 sl_uncompress_tcp(NULL, 0, TYPE_ERROR, sc->sc_comp);
1353 sc->sc_flags &= ~SC_VJ_RESET;
1358 * See if we have a VJ-compressed packet to uncompress.
1360 if (proto == PPP_VJC_COMP) {
1361 if ((sc->sc_flags & SC_REJ_COMP_TCP) || sc->sc_comp == 0)
1364 xlen = sl_uncompress_tcp_core(cp + PPP_HDRLEN, m->m_len - PPP_HDRLEN,
1365 ilen - PPP_HDRLEN, TYPE_COMPRESSED_TCP,
1366 sc->sc_comp, &iphdr, &hlen);
1369 if (sc->sc_flags & SC_DEBUG)
1370 printf("%s: VJ uncompress failed on type comp\n",
1375 /* Copy the PPP and IP headers into a new mbuf. */
1376 MGETHDR(mp, MB_DONTWAIT, MT_DATA);
1381 if (hlen + PPP_HDRLEN > MHLEN) {
1382 MCLGET(mp, MB_DONTWAIT);
1383 if (M_TRAILINGSPACE(mp) < hlen + PPP_HDRLEN) {
1385 goto bad; /* lose if big headers and no clusters */
1388 cp = mtod(mp, u_char *);
1394 bcopy(iphdr, cp + PPP_HDRLEN, hlen);
1395 mp->m_len = hlen + PPP_HDRLEN;
1398 * Trim the PPP and VJ headers off the old mbuf
1399 * and stick the new and old mbufs together.
1401 m->m_data += PPP_HDRLEN + xlen;
1402 m->m_len -= PPP_HDRLEN + xlen;
1403 if (m->m_len <= M_TRAILINGSPACE(mp)) {
1404 bcopy(mtod(m, u_char *), mtod(mp, u_char *) + mp->m_len, m->m_len);
1405 mp->m_len += m->m_len;
1406 mp->m_next = m_free(m);
1411 ilen += hlen - xlen;
1413 } else if (proto == PPP_VJC_UNCOMP) {
1414 if ((sc->sc_flags & SC_REJ_COMP_TCP) || sc->sc_comp == 0)
1417 xlen = sl_uncompress_tcp_core(cp + PPP_HDRLEN, m->m_len - PPP_HDRLEN,
1418 ilen - PPP_HDRLEN, TYPE_UNCOMPRESSED_TCP,
1419 sc->sc_comp, &iphdr, &hlen);
1422 if (sc->sc_flags & SC_DEBUG)
1423 printf("%s: VJ uncompress failed on type uncomp\n",
1434 * If the packet will fit in a header mbuf, don't waste a
1435 * whole cluster on it.
1437 if (ilen <= MHLEN && M_IS_CLUSTER(m)) {
1438 MGETHDR(mp, MB_DONTWAIT, MT_DATA);
1440 m_copydata(m, 0, ilen, mtod(mp, caddr_t));
1446 m->m_pkthdr.len = ilen;
1447 m->m_pkthdr.rcvif = ifp;
1449 if ((proto & 0x8000) == 0) {
1452 * See whether we want to pass this packet, and
1453 * if it counts as link activity.
1455 adrs = *mtod(m, u_char *); /* save address field */
1456 *mtod(m, u_char *) = 0; /* indicate inbound */
1457 if (sc->sc_pass_filt.bf_insns != 0
1458 && bpf_filter(sc->sc_pass_filt.bf_insns, (u_char *) m,
1460 /* drop this packet */
1464 if (sc->sc_active_filt.bf_insns == 0
1465 || bpf_filter(sc->sc_active_filt.bf_insns, (u_char *) m, ilen, 0))
1466 sc->sc_last_recv = time_second;
1468 *mtod(m, u_char *) = adrs;
1471 * Record the time that we received this packet.
1473 sc->sc_last_recv = time_second;
1474 #endif /* PPP_FILTER */
1477 BPF_MTAP(&sc->sc_if, m);
1484 * IP packet - take off the ppp header and pass it up to IP.
1486 if ((ifp->if_flags & IFF_UP) == 0
1487 || sc->sc_npmode[NP_IP] != NPMODE_PASS) {
1488 /* interface is down - drop the packet. */
1492 m->m_pkthdr.len -= PPP_HDRLEN;
1493 m->m_data += PPP_HDRLEN;
1494 m->m_len -= PPP_HDRLEN;
1495 if (ipflow_fastforward(m))
1503 * IPX packet - take off the ppp header and pass it up to IPX.
1505 if ((sc->sc_if.if_flags & IFF_UP) == 0
1506 /* XXX: || sc->sc_npmode[NP_IPX] != NPMODE_PASS*/) {
1507 /* interface is down - drop the packet. */
1511 m->m_pkthdr.len -= PPP_HDRLEN;
1512 m->m_data += PPP_HDRLEN;
1513 m->m_len -= PPP_HDRLEN;
1515 sc->sc_last_recv = time_second; /* update time of last pkt rcvd */
1521 * Some other protocol - place on input queue for read().
1527 * If we found a netproto just pass it to the proto. Otherwise queue
1528 * the packet to the tty (e.g. pppd). Note that sc_ctlp() must be
1529 * called EXACTLY once per packet queued.
1532 rv = IF_HANDOFF(&sc->sc_inq, m, NULL);
1536 rv = (netisr_queue(isr, m) == 0);
1539 if (sc->sc_flags & SC_DEBUG)
1540 printf("%s: input queue full\n", ifp->if_xname);
1546 ifp->if_ibytes += ilen;
1547 getmicrotime(&ifp->if_lastchange);
1553 sc->sc_if.if_ierrors++;
1554 sc->sc_stats.ppp_ierrors++;
1557 #define MAX_DUMP_BYTES 128
1563 char buf[3*MAX_DUMP_BYTES+4];
1567 for (m = m0; m; m = m->m_next) {
1569 u_char *rptr = (u_char *)m->m_data;
1572 if (bp > buf + sizeof(buf) - 4)
1574 *bp++ = hex2ascii(*rptr >> 4);
1575 *bp++ = hex2ascii(*rptr++ & 0xf);
1579 if (bp > buf + sizeof(buf) - 3)
1589 printf("%s\n", buf);
1593 * a wrapper to transmit a packet from if_start since ALTQ uses
1594 * if_start to send a packet.
1597 ppp_ifstart(struct ifnet *ifp)
1599 struct ppp_softc *sc;
1602 (*sc->sc_start)(sc);