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 /* from if_sl.c,v 1.11 84/10/04 12:54:47 rick Exp */
74 /* from NetBSD: if_ppp.c,v 1.15.2.2 1994/07/28 05:17:58 cgd Exp */
87 #include <sys/param.h>
88 #include <sys/systm.h>
91 #include <sys/socket.h>
92 #include <sys/filio.h>
93 #include <sys/sockio.h>
94 #include <sys/kernel.h>
96 #include <sys/malloc.h>
99 #include <net/if_types.h>
100 #include <net/netisr.h>
104 #include <netinet/in.h>
105 #include <netinet/in_systm.h>
106 #include <netinet/in_var.h>
107 #include <netinet/ip.h>
111 #include <netipx/ipx.h>
112 #include <netipx/ipx_if.h>
116 #include <net/slcompress.h>
119 #include <net/if_ppp.h>
120 #include <net/if_pppvar.h>
124 #define splsoftnet splnet
128 #define PACKETPTR struct mbuf *
129 #include <net/ppp_comp.h>
132 struct ppp_softc ppp_softc[NPPP];
134 /* XXX layering violation */
135 extern void pppasyncattach __P((void *));
137 static void pppattach __P((void *));
138 PSEUDO_SET(pppattach, if_ppp);
140 static int pppsioctl __P((struct ifnet *ifp, u_long cmd, caddr_t data));
141 static void pppintr __P((void));
143 static void ppp_requeue __P((struct ppp_softc *));
144 static void ppp_ccp __P((struct ppp_softc *, struct mbuf *m, int rcvd));
145 static void ppp_ccp_closed __P((struct ppp_softc *));
146 static void ppp_inproc __P((struct ppp_softc *, struct mbuf *));
147 static void pppdumpm __P((struct mbuf *m0));
150 * Some useful mbuf macros not in mbuf.h.
152 #define M_IS_CLUSTER(m) ((m)->m_flags & M_EXT)
154 #define M_DATASTART(m) \
155 (M_IS_CLUSTER(m) ? (m)->m_ext.ext_buf : \
156 (m)->m_flags & M_PKTHDR ? (m)->m_pktdat : (m)->m_dat)
158 #define M_DATASIZE(m) \
159 (M_IS_CLUSTER(m) ? (m)->m_ext.ext_size : \
160 (m)->m_flags & M_PKTHDR ? MHLEN: MLEN)
163 * We steal two bits in the mbuf m_flags, to mark high-priority packets
164 * for output, and received packets following lost/corrupted packets.
166 #define M_HIGHPRI 0x2000 /* output packet for sc_fastq */
167 #define M_ERRMARK 0x4000 /* steal a bit in mbuf m_flags */
172 * List of compressors we know about.
173 * We leave some space so maybe we can modload compressors.
176 extern struct compressor ppp_bsd_compress;
177 extern struct compressor ppp_deflate, ppp_deflate_draft;
179 static struct compressor *ppp_compressors[8] = {
180 #if DO_BSD_COMPRESS && defined(PPP_BSDCOMP)
183 #if DO_DEFLATE && defined(PPP_DEFLATE)
189 #endif /* PPP_COMPRESS */
192 * Called from boot code to establish ppp interfaces.
198 register struct ppp_softc *sc;
201 for (sc = ppp_softc; i < NPPP; sc++) {
202 sc->sc_if.if_name = "ppp";
203 sc->sc_if.if_unit = i++;
204 sc->sc_if.if_mtu = PPP_MTU;
205 sc->sc_if.if_flags = IFF_POINTOPOINT | IFF_MULTICAST;
206 sc->sc_if.if_type = IFT_PPP;
207 sc->sc_if.if_hdrlen = PPP_HDRLEN;
208 sc->sc_if.if_ioctl = pppsioctl;
209 sc->sc_if.if_output = pppoutput;
210 sc->sc_if.if_snd.ifq_maxlen = IFQ_MAXLEN;
211 sc->sc_inq.ifq_maxlen = IFQ_MAXLEN;
212 sc->sc_fastq.ifq_maxlen = IFQ_MAXLEN;
213 sc->sc_rawq.ifq_maxlen = IFQ_MAXLEN;
214 if_attach(&sc->sc_if);
215 bpfattach(&sc->sc_if, DLT_PPP, PPP_HDRLEN);
217 register_netisr(NETISR_PPP, pppintr);
219 * XXX layering violation - if_ppp can work over any lower level
220 * transport that cares to attach to it.
222 pppasyncattach(dummy);
226 * Allocate a ppp interface unit and initialize it.
233 struct ppp_softc *sc;
235 for (nppp = 0, sc = ppp_softc; nppp < NPPP; nppp++, sc++)
236 if (sc->sc_xfer == pid) {
240 for (nppp = 0, sc = ppp_softc; nppp < NPPP; nppp++, sc++)
241 if (sc->sc_devp == NULL)
247 sc->sc_mru = PPP_MRU;
248 sc->sc_relinq = NULL;
249 bzero((char *)&sc->sc_stats, sizeof(sc->sc_stats));
251 MALLOC(sc->sc_comp, struct slcompress *, sizeof(struct slcompress),
254 sl_compress_init(sc->sc_comp, -1);
257 sc->sc_xc_state = NULL;
258 sc->sc_rc_state = NULL;
259 #endif /* PPP_COMPRESS */
260 for (i = 0; i < NUM_NP; ++i)
261 sc->sc_npmode[i] = NPMODE_ERROR;
262 sc->sc_npqueue = NULL;
263 sc->sc_npqtail = &sc->sc_npqueue;
264 sc->sc_last_sent = sc->sc_last_recv = time_second;
270 * Deallocate a ppp unit. Must be called at splsoftnet or higher.
274 struct ppp_softc *sc;
279 sc->sc_if.if_flags &= ~(IFF_UP|IFF_RUNNING);
280 getmicrotime(&sc->sc_if.if_lastchange);
284 IF_DEQUEUE(&sc->sc_rawq, m);
290 IF_DEQUEUE(&sc->sc_inq, m);
296 IF_DEQUEUE(&sc->sc_fastq, m);
301 while ((m = sc->sc_npqueue) != NULL) {
302 sc->sc_npqueue = m->m_nextpkt;
307 sc->sc_xc_state = NULL;
308 sc->sc_rc_state = NULL;
309 #endif /* PPP_COMPRESS */
311 if (sc->sc_pass_filt.bf_insns != 0) {
312 free(sc->sc_pass_filt.bf_insns, M_DEVBUF);
313 sc->sc_pass_filt.bf_insns = 0;
314 sc->sc_pass_filt.bf_len = 0;
316 if (sc->sc_active_filt.bf_insns != 0) {
317 free(sc->sc_active_filt.bf_insns, M_DEVBUF);
318 sc->sc_active_filt.bf_insns = 0;
319 sc->sc_active_filt.bf_len = 0;
321 #endif /* PPP_FILTER */
323 if (sc->sc_comp != 0) {
324 free(sc->sc_comp, M_DEVBUF);
331 * Ioctl routine for generic ppp devices.
334 pppioctl(sc, cmd, data, flag, p)
335 struct ppp_softc *sc;
341 int s, error, flags, mru, npx;
343 struct ppp_option_data *odp;
344 struct compressor **cp;
348 struct bpf_program *bp, *nbp;
349 struct bpf_insn *newcode, *oldcode;
351 #endif /* PPP_FILTER */
353 u_char ccp_option[CCP_MAX_OPTION_LENGTH];
358 *(int *)data = sc->sc_inq.ifq_len;
362 *(int *)data = sc->sc_if.if_unit;
366 *(u_int *)data = sc->sc_flags;
370 if ((error = suser(p)) != 0)
372 flags = *(int *)data & SC_MASK;
375 if (sc->sc_flags & SC_CCP_OPEN && !(flags & SC_CCP_OPEN))
379 sc->sc_flags = (sc->sc_flags & ~SC_MASK) | flags;
384 if ((error = suser(p)) != 0)
387 if (mru >= PPP_MRU && mru <= PPP_MAXMRU)
392 *(int *)data = sc->sc_mru;
397 if ((error = suser(p)) != 0)
401 sl_compress_init(sc->sc_comp, *(int *)data);
408 if ((error = suser(p)) != 0)
410 sc->sc_xfer = p->p_pid;
414 case PPPIOCSCOMPRESS:
415 if ((error = suser(p)) != 0)
417 odp = (struct ppp_option_data *) data;
419 if (nb > sizeof(ccp_option))
420 nb = sizeof(ccp_option);
421 if ((error = copyin(odp->ptr, ccp_option, nb)) != 0)
423 if (ccp_option[1] < 2) /* preliminary check on the length byte */
425 for (cp = ppp_compressors; *cp != NULL; ++cp)
426 if ((*cp)->compress_proto == ccp_option[0]) {
428 * Found a handler for the protocol - try to allocate
429 * a compressor or decompressor.
434 if (sc->sc_xc_state != NULL)
435 (*sc->sc_xcomp->comp_free)(sc->sc_xc_state);
437 sc->sc_xc_state = (*cp)->comp_alloc(ccp_option, nb);
438 if (sc->sc_xc_state == NULL) {
439 if (sc->sc_flags & SC_DEBUG)
440 printf("ppp%d: comp_alloc failed\n",
445 sc->sc_flags &= ~SC_COMP_RUN;
449 if (sc->sc_rc_state != NULL)
450 (*sc->sc_rcomp->decomp_free)(sc->sc_rc_state);
452 sc->sc_rc_state = (*cp)->decomp_alloc(ccp_option, nb);
453 if (sc->sc_rc_state == NULL) {
454 if (sc->sc_flags & SC_DEBUG)
455 printf("ppp%d: decomp_alloc failed\n",
460 sc->sc_flags &= ~SC_DECOMP_RUN;
465 if (sc->sc_flags & SC_DEBUG)
466 printf("ppp%d: no compressor for [%x %x %x], %x\n",
467 sc->sc_if.if_unit, ccp_option[0], ccp_option[1],
469 return (EINVAL); /* no handler found */
470 #endif /* PPP_COMPRESS */
474 npi = (struct npioctl *) data;
475 switch (npi->protocol) {
482 if (cmd == PPPIOCGNPMODE) {
483 npi->mode = sc->sc_npmode[npx];
485 if ((error = suser(p)) != 0)
487 if (npi->mode != sc->sc_npmode[npx]) {
489 sc->sc_npmode[npx] = npi->mode;
490 if (npi->mode != NPMODE_QUEUE) {
502 ((struct ppp_idle *)data)->xmit_idle = t - sc->sc_last_sent;
503 ((struct ppp_idle *)data)->recv_idle = t - sc->sc_last_recv;
510 nbp = (struct bpf_program *) data;
511 if ((unsigned) nbp->bf_len > BPF_MAXINSNS)
513 newcodelen = nbp->bf_len * sizeof(struct bpf_insn);
514 if (newcodelen != 0) {
515 MALLOC(newcode, struct bpf_insn *, newcodelen, M_DEVBUF, M_WAITOK);
517 return EINVAL; /* or sumpin */
519 if ((error = copyin((caddr_t)nbp->bf_insns, (caddr_t)newcode,
521 free(newcode, M_DEVBUF);
524 if (!bpf_validate(newcode, nbp->bf_len)) {
525 free(newcode, M_DEVBUF);
530 bp = (cmd == PPPIOCSPASS)? &sc->sc_pass_filt: &sc->sc_active_filt;
531 oldcode = bp->bf_insns;
533 bp->bf_len = nbp->bf_len;
534 bp->bf_insns = newcode;
537 free(oldcode, M_DEVBUF);
548 * Process an ioctl request to the ppp network interface.
551 pppsioctl(ifp, cmd, data)
552 register struct ifnet *ifp;
556 struct proc *p = curproc; /* XXX */
557 register struct ppp_softc *sc = &ppp_softc[ifp->if_unit];
558 register struct ifaddr *ifa = (struct ifaddr *)data;
559 register struct ifreq *ifr = (struct ifreq *)data;
560 struct ppp_stats *psp;
562 struct ppp_comp_stats *pcp;
564 int s = splimp(), error = 0;
568 if ((ifp->if_flags & IFF_RUNNING) == 0)
569 ifp->if_flags &= ~IFF_UP;
574 switch(ifa->ifa_addr->sa_family) {
584 error = EAFNOSUPPORT;
590 switch(ifa->ifa_addr->sa_family) {
600 error = EAFNOSUPPORT;
606 if ((error = suser(p)) != 0)
608 if (ifr->ifr_mtu > PPP_MAXMTU)
611 sc->sc_if.if_mtu = ifr->ifr_mtu;
613 (*sc->sc_setmtu)(sc);
618 ifr->ifr_mtu = sc->sc_if.if_mtu;
624 error = EAFNOSUPPORT;
627 switch(ifr->ifr_addr.sa_family) {
633 error = EAFNOSUPPORT;
639 psp = &((struct ifpppstatsreq *) data)->stats;
640 bzero(psp, sizeof(*psp));
641 psp->p = sc->sc_stats;
642 #if defined(VJC) && !defined(SL_NO_STATS)
644 psp->vj.vjs_packets = sc->sc_comp->sls_packets;
645 psp->vj.vjs_compressed = sc->sc_comp->sls_compressed;
646 psp->vj.vjs_searches = sc->sc_comp->sls_searches;
647 psp->vj.vjs_misses = sc->sc_comp->sls_misses;
648 psp->vj.vjs_uncompressedin = sc->sc_comp->sls_uncompressedin;
649 psp->vj.vjs_compressedin = sc->sc_comp->sls_compressedin;
650 psp->vj.vjs_errorin = sc->sc_comp->sls_errorin;
651 psp->vj.vjs_tossed = sc->sc_comp->sls_tossed;
658 pcp = &((struct ifpppcstatsreq *) data)->stats;
659 bzero(pcp, sizeof(*pcp));
660 if (sc->sc_xc_state != NULL)
661 (*sc->sc_xcomp->comp_stat)(sc->sc_xc_state, &pcp->c);
662 if (sc->sc_rc_state != NULL)
663 (*sc->sc_rcomp->decomp_stat)(sc->sc_rc_state, &pcp->d);
665 #endif /* PPP_COMPRESS */
675 * Queue a packet. Start transmission if not active.
676 * Packet is placed in Information field of PPP frame.
677 * Called at splnet as the if->if_output handler.
678 * Called at splnet from pppwrite().
681 pppoutput(ifp, m0, dst, rtp)
684 struct sockaddr *dst;
687 register struct ppp_softc *sc = &ppp_softc[ifp->if_unit];
688 int protocol, address, control;
697 if (sc->sc_devp == NULL || (ifp->if_flags & IFF_RUNNING) == 0
698 || ((ifp->if_flags & IFF_UP) == 0 && dst->sa_family != AF_UNSPEC)) {
699 error = ENETDOWN; /* sort of */
704 * Compute PPP header.
706 m0->m_flags &= ~M_HIGHPRI;
707 switch (dst->sa_family) {
710 address = PPP_ALLSTATIONS;
713 mode = sc->sc_npmode[NP_IP];
716 * If this packet has the "low delay" bit set in the IP header,
717 * put it on the fastq instead.
719 ip = mtod(m0, struct ip *);
720 if (ip->ip_tos & IPTOS_LOWDELAY)
721 m0->m_flags |= M_HIGHPRI;
727 * This is pretty bogus.. We dont have an ipxcp module in pppd
728 * yet to configure the link parameters. Sigh. I guess a
729 * manual ifconfig would do.... -Peter
731 address = PPP_ALLSTATIONS;
738 address = PPP_ADDRESS(dst->sa_data);
739 control = PPP_CONTROL(dst->sa_data);
740 protocol = PPP_PROTOCOL(dst->sa_data);
744 printf("ppp%d: af%d not supported\n", ifp->if_unit, dst->sa_family);
745 error = EAFNOSUPPORT;
750 * Drop this packet, or return an error, if necessary.
752 if (mode == NPMODE_ERROR) {
756 if (mode == NPMODE_DROP) {
762 * Add PPP header. If no space in first mbuf, allocate another.
763 * (This assumes M_LEADINGSPACE is always 0 for a cluster mbuf.)
765 if (M_LEADINGSPACE(m0) < PPP_HDRLEN) {
766 m0 = m_prepend(m0, PPP_HDRLEN, M_DONTWAIT);
773 m0->m_data -= PPP_HDRLEN;
775 cp = mtod(m0, u_char *);
778 *cp++ = protocol >> 8;
779 *cp++ = protocol & 0xff;
780 m0->m_len += PPP_HDRLEN;
783 for (m = m0; m != 0; m = m->m_next)
786 if (sc->sc_flags & SC_LOG_OUTPKT) {
787 printf("ppp%d output: ", ifp->if_unit);
791 if ((protocol & 0x8000) == 0) {
794 * Apply the pass and active filters to the packet,
795 * but only if it is a data packet.
797 *mtod(m0, u_char *) = 1; /* indicates outbound */
798 if (sc->sc_pass_filt.bf_insns != 0
799 && bpf_filter(sc->sc_pass_filt.bf_insns, (u_char *) m0,
801 error = 0; /* drop this packet */
806 * Update the time we sent the most recent packet.
808 if (sc->sc_active_filt.bf_insns == 0
809 || bpf_filter(sc->sc_active_filt.bf_insns, (u_char *) m0, len, 0))
810 sc->sc_last_sent = time_second;
812 *mtod(m0, u_char *) = address;
815 * Update the time we sent the most recent data packet.
817 sc->sc_last_sent = time_second;
818 #endif /* PPP_FILTER */
822 * See if bpf wants to look at the packet.
828 * Put the packet on the appropriate queue.
830 s = splsoftnet(); /* redundant */
831 if (mode == NPMODE_QUEUE) {
832 /* XXX we should limit the number of packets on this queue */
833 *sc->sc_npqtail = m0;
834 m0->m_nextpkt = NULL;
835 sc->sc_npqtail = &m0->m_nextpkt;
837 /* fastq and if_snd are emptied at spl[soft]net now */
838 ifq = (m0->m_flags & M_HIGHPRI)? &sc->sc_fastq: &ifp->if_snd;
839 if (IF_QFULL(ifq) && dst->sa_family != AF_UNSPEC) {
842 sc->sc_if.if_oerrors++;
843 sc->sc_stats.ppp_oerrors++;
850 getmicrotime(&ifp->if_lastchange);
852 ifp->if_obytes += len;
863 * After a change in the NPmode for some NP, move packets from the
864 * npqueue to the send queue or the fast queue as appropriate.
865 * Should be called at spl[soft]net.
869 struct ppp_softc *sc;
871 struct mbuf *m, **mpp;
875 for (mpp = &sc->sc_npqueue; (m = *mpp) != NULL; ) {
876 switch (PPP_PROTOCOL(mtod(m, u_char *))) {
878 mode = sc->sc_npmode[NP_IP];
887 * This packet can now go on one of the queues to be sent.
891 ifq = (m->m_flags & M_HIGHPRI)? &sc->sc_fastq: &sc->sc_if.if_snd;
894 sc->sc_if.if_oerrors++;
895 sc->sc_stats.ppp_oerrors++;
911 sc->sc_npqtail = mpp;
915 * Transmitter has finished outputting some stuff;
916 * remember to call sc->sc_start later at splsoftnet.
920 struct ppp_softc *sc;
924 sc->sc_flags &= ~SC_TBUSY;
925 schednetisr(NETISR_PPP);
931 * Get a packet to send. This procedure is intended to be called at
932 * splsoftnet, since it may involve time-consuming operations such as
933 * applying VJ compression, packet compression, address/control and/or
934 * protocol field compression to the packet.
938 struct ppp_softc *sc;
942 int address, control, protocol;
945 * Grab a packet to send: first try the fast queue, then the
948 IF_DEQUEUE(&sc->sc_fastq, m);
950 IF_DEQUEUE(&sc->sc_if.if_snd, m);
954 ++sc->sc_stats.ppp_opackets;
957 * Extract the ppp header of the new packet.
958 * The ppp header will be in one mbuf.
960 cp = mtod(m, u_char *);
961 address = PPP_ADDRESS(cp);
962 control = PPP_CONTROL(cp);
963 protocol = PPP_PROTOCOL(cp);
969 * If the packet is a TCP/IP packet, see if we can compress it.
971 if ((sc->sc_flags & SC_COMP_TCP) && sc->sc_comp != NULL) {
976 ip = (struct ip *) (cp + PPP_HDRLEN);
977 if (mp->m_len <= PPP_HDRLEN) {
981 ip = mtod(mp, struct ip *);
983 /* this code assumes the IP/TCP header is in one non-shared mbuf */
984 if (ip->ip_p == IPPROTO_TCP) {
985 type = sl_compress_tcp(mp, ip, sc->sc_comp,
986 !(sc->sc_flags & SC_NO_TCP_CCID));
988 case TYPE_UNCOMPRESSED_TCP:
989 protocol = PPP_VJC_UNCOMP;
991 case TYPE_COMPRESSED_TCP:
992 protocol = PPP_VJC_COMP;
993 cp = mtod(m, u_char *);
994 cp[0] = address; /* header has moved */
999 cp[3] = protocol; /* update protocol in PPP header */
1009 #endif /* PPP_COMPRESS */
1013 if (protocol != PPP_LCP && protocol != PPP_CCP
1014 && sc->sc_xc_state && (sc->sc_flags & SC_COMP_RUN)) {
1015 struct mbuf *mcomp = NULL;
1019 for (mp = m; mp != NULL; mp = mp->m_next)
1021 clen = (*sc->sc_xcomp->compress)
1022 (sc->sc_xc_state, &mcomp, m, slen, sc->sc_if.if_mtu + PPP_HDRLEN);
1023 if (mcomp != NULL) {
1024 if (sc->sc_flags & SC_CCP_UP) {
1025 /* Send the compressed packet instead of the original. */
1028 cp = mtod(m, u_char *);
1031 /* Can't transmit compressed packets until CCP is up. */
1036 #endif /* PPP_COMPRESS */
1039 * Compress the address/control and protocol, if possible.
1041 if (sc->sc_flags & SC_COMP_AC && address == PPP_ALLSTATIONS &&
1042 control == PPP_UI && protocol != PPP_ALLSTATIONS &&
1043 protocol != PPP_LCP) {
1044 /* can compress address/control */
1048 if (sc->sc_flags & SC_COMP_PROT && protocol < 0xFF) {
1049 /* can compress protocol */
1050 if (mtod(m, u_char *) == cp) {
1051 cp[2] = cp[1]; /* move address/control up */
1062 * Software interrupt routine, called at spl[soft]net.
1067 struct ppp_softc *sc;
1072 for (i = 0; i < NPPP; ++i, ++sc) {
1074 if (!(sc->sc_flags & SC_TBUSY)
1075 && (sc->sc_if.if_snd.ifq_head || sc->sc_fastq.ifq_head)) {
1076 sc->sc_flags |= SC_TBUSY;
1078 (*sc->sc_start)(sc);
1083 IF_DEQUEUE(&sc->sc_rawq, m);
1094 * Handle a CCP packet. `rcvd' is 1 if the packet was received,
1095 * 0 if it is about to be transmitted.
1098 ppp_ccp(sc, m, rcvd)
1099 struct ppp_softc *sc;
1108 * Get a pointer to the data after the PPP header.
1110 if (m->m_len <= PPP_HDRLEN) {
1114 dp = (mp != NULL)? mtod(mp, u_char *): NULL;
1117 dp = mtod(mp, u_char *) + PPP_HDRLEN;
1120 ep = mtod(mp, u_char *) + mp->m_len;
1121 if (dp + CCP_HDRLEN > ep)
1123 slen = CCP_LENGTH(dp);
1124 if (dp + slen > ep) {
1125 if (sc->sc_flags & SC_DEBUG)
1126 printf("if_ppp/ccp: not enough data in mbuf (%p+%x > %p+%x)\n",
1127 dp, slen, mtod(mp, u_char *), mp->m_len);
1131 switch (CCP_CODE(dp)) {
1135 /* CCP must be going down - disable compression */
1136 if (sc->sc_flags & SC_CCP_UP) {
1138 sc->sc_flags &= ~(SC_CCP_UP | SC_COMP_RUN | SC_DECOMP_RUN);
1144 if (sc->sc_flags & SC_CCP_OPEN && !(sc->sc_flags & SC_CCP_UP)
1145 && slen >= CCP_HDRLEN + CCP_OPT_MINLEN
1146 && slen >= CCP_OPT_LENGTH(dp + CCP_HDRLEN) + CCP_HDRLEN) {
1148 /* we're agreeing to send compressed packets. */
1149 if (sc->sc_xc_state != NULL
1150 && (*sc->sc_xcomp->comp_init)
1151 (sc->sc_xc_state, dp + CCP_HDRLEN, slen - CCP_HDRLEN,
1152 sc->sc_if.if_unit, 0, sc->sc_flags & SC_DEBUG)) {
1154 sc->sc_flags |= SC_COMP_RUN;
1158 /* peer is agreeing to send compressed packets. */
1159 if (sc->sc_rc_state != NULL
1160 && (*sc->sc_rcomp->decomp_init)
1161 (sc->sc_rc_state, dp + CCP_HDRLEN, slen - CCP_HDRLEN,
1162 sc->sc_if.if_unit, 0, sc->sc_mru,
1163 sc->sc_flags & SC_DEBUG)) {
1165 sc->sc_flags |= SC_DECOMP_RUN;
1166 sc->sc_flags &= ~(SC_DC_ERROR | SC_DC_FERROR);
1174 if (sc->sc_flags & SC_CCP_UP) {
1176 if (sc->sc_xc_state && (sc->sc_flags & SC_COMP_RUN))
1177 (*sc->sc_xcomp->comp_reset)(sc->sc_xc_state);
1179 if (sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)) {
1180 (*sc->sc_rcomp->decomp_reset)(sc->sc_rc_state);
1182 sc->sc_flags &= ~SC_DC_ERROR;
1192 * CCP is down; free (de)compressor state if necessary.
1196 struct ppp_softc *sc;
1198 if (sc->sc_xc_state) {
1199 (*sc->sc_xcomp->comp_free)(sc->sc_xc_state);
1200 sc->sc_xc_state = NULL;
1202 if (sc->sc_rc_state) {
1203 (*sc->sc_rcomp->decomp_free)(sc->sc_rc_state);
1204 sc->sc_rc_state = NULL;
1207 #endif /* PPP_COMPRESS */
1210 * PPP packet input routine.
1211 * The caller has checked and removed the FCS and has inserted
1212 * the address/control bytes and the protocol high byte if they
1216 ppppktin(sc, m, lost)
1217 struct ppp_softc *sc;
1224 m->m_flags |= M_ERRMARK;
1225 IF_ENQUEUE(&sc->sc_rawq, m);
1226 schednetisr(NETISR_PPP);
1231 * Process a received PPP packet, doing decompression as necessary.
1232 * Should be called at splsoftnet.
1234 #define COMPTYPE(proto) ((proto) == PPP_VJC_COMP? TYPE_COMPRESSED_TCP: \
1235 TYPE_UNCOMPRESSED_TCP)
1239 struct ppp_softc *sc;
1242 struct ifnet *ifp = &sc->sc_if;
1243 struct ifqueue *inq;
1244 int s, ilen = 0, xlen, proto, rv;
1245 u_char *cp, adrs, ctrl;
1246 struct mbuf *mp, *dmp = NULL;
1250 sc->sc_stats.ppp_ipackets++;
1252 if (sc->sc_flags & SC_LOG_INPKT) {
1254 for (mp = m; mp != NULL; mp = mp->m_next)
1256 printf("ppp%d: got %d bytes\n", ifp->if_unit, ilen);
1260 cp = mtod(m, u_char *);
1261 adrs = PPP_ADDRESS(cp);
1262 ctrl = PPP_CONTROL(cp);
1263 proto = PPP_PROTOCOL(cp);
1265 if (m->m_flags & M_ERRMARK) {
1266 m->m_flags &= ~M_ERRMARK;
1268 sc->sc_flags |= SC_VJ_RESET;
1274 * Decompress this packet if necessary, update the receiver's
1275 * dictionary, or take appropriate action on a CCP packet.
1277 if (proto == PPP_COMP && sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)
1278 && !(sc->sc_flags & SC_DC_ERROR) && !(sc->sc_flags & SC_DC_FERROR)) {
1279 /* decompress this packet */
1280 rv = (*sc->sc_rcomp->decompress)(sc->sc_rc_state, m, &dmp);
1281 if (rv == DECOMP_OK) {
1284 /* no error, but no decompressed packet produced */
1288 cp = mtod(m, u_char *);
1289 proto = PPP_PROTOCOL(cp);
1293 * An error has occurred in decompression.
1294 * Pass the compressed packet up to pppd, which may take
1295 * CCP down or issue a Reset-Req.
1297 if (sc->sc_flags & SC_DEBUG)
1298 printf("ppp%d: decompress failed %d\n", ifp->if_unit, rv);
1300 sc->sc_flags |= SC_VJ_RESET;
1301 if (rv == DECOMP_ERROR)
1302 sc->sc_flags |= SC_DC_ERROR;
1304 sc->sc_flags |= SC_DC_FERROR;
1309 if (sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)) {
1310 (*sc->sc_rcomp->incomp)(sc->sc_rc_state, m);
1312 if (proto == PPP_CCP) {
1319 for (mp = m; mp != NULL; mp = mp->m_next)
1323 if (sc->sc_flags & SC_VJ_RESET) {
1325 * If we've missed a packet, we must toss subsequent compressed
1326 * packets which don't have an explicit connection ID.
1329 sl_uncompress_tcp(NULL, 0, TYPE_ERROR, sc->sc_comp);
1331 sc->sc_flags &= ~SC_VJ_RESET;
1336 * See if we have a VJ-compressed packet to uncompress.
1338 if (proto == PPP_VJC_COMP) {
1339 if ((sc->sc_flags & SC_REJ_COMP_TCP) || sc->sc_comp == 0)
1342 xlen = sl_uncompress_tcp_core(cp + PPP_HDRLEN, m->m_len - PPP_HDRLEN,
1343 ilen - PPP_HDRLEN, TYPE_COMPRESSED_TCP,
1344 sc->sc_comp, &iphdr, &hlen);
1347 if (sc->sc_flags & SC_DEBUG)
1348 printf("ppp%d: VJ uncompress failed on type comp\n",
1353 /* Copy the PPP and IP headers into a new mbuf. */
1354 MGETHDR(mp, M_DONTWAIT, MT_DATA);
1359 if (hlen + PPP_HDRLEN > MHLEN) {
1360 MCLGET(mp, M_DONTWAIT);
1361 if (M_TRAILINGSPACE(mp) < hlen + PPP_HDRLEN) {
1363 goto bad; /* lose if big headers and no clusters */
1366 cp = mtod(mp, u_char *);
1372 bcopy(iphdr, cp + PPP_HDRLEN, hlen);
1373 mp->m_len = hlen + PPP_HDRLEN;
1376 * Trim the PPP and VJ headers off the old mbuf
1377 * and stick the new and old mbufs together.
1379 m->m_data += PPP_HDRLEN + xlen;
1380 m->m_len -= PPP_HDRLEN + xlen;
1381 if (m->m_len <= M_TRAILINGSPACE(mp)) {
1382 bcopy(mtod(m, u_char *), mtod(mp, u_char *) + mp->m_len, m->m_len);
1383 mp->m_len += m->m_len;
1384 mp->m_next = m_free(m);
1389 ilen += hlen - xlen;
1391 } else if (proto == PPP_VJC_UNCOMP) {
1392 if ((sc->sc_flags & SC_REJ_COMP_TCP) || sc->sc_comp == 0)
1395 xlen = sl_uncompress_tcp_core(cp + PPP_HDRLEN, m->m_len - PPP_HDRLEN,
1396 ilen - PPP_HDRLEN, TYPE_UNCOMPRESSED_TCP,
1397 sc->sc_comp, &iphdr, &hlen);
1400 if (sc->sc_flags & SC_DEBUG)
1401 printf("ppp%d: VJ uncompress failed on type uncomp\n",
1412 * If the packet will fit in a header mbuf, don't waste a
1413 * whole cluster on it.
1415 if (ilen <= MHLEN && M_IS_CLUSTER(m)) {
1416 MGETHDR(mp, M_DONTWAIT, MT_DATA);
1418 m_copydata(m, 0, ilen, mtod(mp, caddr_t));
1424 m->m_pkthdr.len = ilen;
1425 m->m_pkthdr.rcvif = ifp;
1427 if ((proto & 0x8000) == 0) {
1430 * See whether we want to pass this packet, and
1431 * if it counts as link activity.
1433 adrs = *mtod(m, u_char *); /* save address field */
1434 *mtod(m, u_char *) = 0; /* indicate inbound */
1435 if (sc->sc_pass_filt.bf_insns != 0
1436 && bpf_filter(sc->sc_pass_filt.bf_insns, (u_char *) m,
1438 /* drop this packet */
1442 if (sc->sc_active_filt.bf_insns == 0
1443 || bpf_filter(sc->sc_active_filt.bf_insns, (u_char *) m, ilen, 0))
1444 sc->sc_last_recv = time_second;
1446 *mtod(m, u_char *) = adrs;
1449 * Record the time that we received this packet.
1451 sc->sc_last_recv = time_second;
1452 #endif /* PPP_FILTER */
1455 /* See if bpf wants to look at the packet. */
1456 if (sc->sc_if.if_bpf)
1457 bpf_mtap(&sc->sc_if, m);
1464 * IP packet - take off the ppp header and pass it up to IP.
1466 if ((ifp->if_flags & IFF_UP) == 0
1467 || sc->sc_npmode[NP_IP] != NPMODE_PASS) {
1468 /* interface is down - drop the packet. */
1472 m->m_pkthdr.len -= PPP_HDRLEN;
1473 m->m_data += PPP_HDRLEN;
1474 m->m_len -= PPP_HDRLEN;
1475 if (ipflow_fastforward(m))
1477 schednetisr(NETISR_IP);
1484 * IPX packet - take off the ppp header and pass it up to IPX.
1486 if ((sc->sc_if.if_flags & IFF_UP) == 0
1487 /* XXX: || sc->sc_npmode[NP_IPX] != 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 schednetisr(NETISR_IPX);
1497 sc->sc_last_recv = time_second; /* update time of last pkt rcvd */
1503 * Some other protocol - place on input queue for read().
1511 * Put the packet on the appropriate input queue.
1514 if (IF_QFULL(inq)) {
1517 if (sc->sc_flags & SC_DEBUG)
1518 printf("ppp%d: input queue full\n", ifp->if_unit);
1525 ifp->if_ibytes += ilen;
1526 getmicrotime(&ifp->if_lastchange);
1535 sc->sc_if.if_ierrors++;
1536 sc->sc_stats.ppp_ierrors++;
1539 #define MAX_DUMP_BYTES 128
1545 char buf[3*MAX_DUMP_BYTES+4];
1549 for (m = m0; m; m = m->m_next) {
1551 u_char *rptr = (u_char *)m->m_data;
1554 if (bp > buf + sizeof(buf) - 4)
1556 *bp++ = hex2ascii(*rptr >> 4);
1557 *bp++ = hex2ascii(*rptr++ & 0xf);
1561 if (bp > buf + sizeof(buf) - 3)
1571 printf("%s\n", buf);