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.2 2003/06/17 04:28:48 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>
100 #include <net/if_types.h>
101 #include <net/netisr.h>
105 #include <netinet/in.h>
106 #include <netinet/in_systm.h>
107 #include <netinet/in_var.h>
108 #include <netinet/ip.h>
112 #include <netipx/ipx.h>
113 #include <netipx/ipx_if.h>
117 #include <net/slcompress.h>
120 #include <net/if_ppp.h>
121 #include <net/if_pppvar.h>
125 #define splsoftnet splnet
129 #define PACKETPTR struct mbuf *
130 #include <net/ppp_comp.h>
133 struct ppp_softc ppp_softc[NPPP];
135 /* XXX layering violation */
136 extern void pppasyncattach __P((void *));
138 static void pppattach __P((void *));
139 PSEUDO_SET(pppattach, if_ppp);
141 static int pppsioctl __P((struct ifnet *ifp, u_long cmd, caddr_t data));
142 static void pppintr __P((void));
144 static void ppp_requeue __P((struct ppp_softc *));
145 static void ppp_ccp __P((struct ppp_softc *, struct mbuf *m, int rcvd));
146 static void ppp_ccp_closed __P((struct ppp_softc *));
147 static void ppp_inproc __P((struct ppp_softc *, struct mbuf *));
148 static void pppdumpm __P((struct mbuf *m0));
151 * Some useful mbuf macros not in mbuf.h.
153 #define M_IS_CLUSTER(m) ((m)->m_flags & M_EXT)
155 #define M_DATASTART(m) \
156 (M_IS_CLUSTER(m) ? (m)->m_ext.ext_buf : \
157 (m)->m_flags & M_PKTHDR ? (m)->m_pktdat : (m)->m_dat)
159 #define M_DATASIZE(m) \
160 (M_IS_CLUSTER(m) ? (m)->m_ext.ext_size : \
161 (m)->m_flags & M_PKTHDR ? MHLEN: MLEN)
164 * We steal two bits in the mbuf m_flags, to mark high-priority packets
165 * for output, and received packets following lost/corrupted packets.
167 #define M_HIGHPRI 0x2000 /* output packet for sc_fastq */
168 #define M_ERRMARK 0x4000 /* steal a bit in mbuf m_flags */
173 * List of compressors we know about.
174 * We leave some space so maybe we can modload compressors.
177 extern struct compressor ppp_bsd_compress;
178 extern struct compressor ppp_deflate, ppp_deflate_draft;
180 static struct compressor *ppp_compressors[8] = {
181 #if DO_BSD_COMPRESS && defined(PPP_BSDCOMP)
184 #if DO_DEFLATE && defined(PPP_DEFLATE)
190 #endif /* PPP_COMPRESS */
193 * Called from boot code to establish ppp interfaces.
199 register struct ppp_softc *sc;
202 for (sc = ppp_softc; i < NPPP; sc++) {
203 sc->sc_if.if_name = "ppp";
204 sc->sc_if.if_unit = i++;
205 sc->sc_if.if_mtu = PPP_MTU;
206 sc->sc_if.if_flags = IFF_POINTOPOINT | IFF_MULTICAST;
207 sc->sc_if.if_type = IFT_PPP;
208 sc->sc_if.if_hdrlen = PPP_HDRLEN;
209 sc->sc_if.if_ioctl = pppsioctl;
210 sc->sc_if.if_output = pppoutput;
211 sc->sc_if.if_snd.ifq_maxlen = IFQ_MAXLEN;
212 sc->sc_inq.ifq_maxlen = IFQ_MAXLEN;
213 sc->sc_fastq.ifq_maxlen = IFQ_MAXLEN;
214 sc->sc_rawq.ifq_maxlen = IFQ_MAXLEN;
215 if_attach(&sc->sc_if);
216 bpfattach(&sc->sc_if, DLT_PPP, PPP_HDRLEN);
218 register_netisr(NETISR_PPP, pppintr);
220 * XXX layering violation - if_ppp can work over any lower level
221 * transport that cares to attach to it.
223 pppasyncattach(dummy);
227 * Allocate a ppp interface unit and initialize it.
234 struct ppp_softc *sc;
236 for (nppp = 0, sc = ppp_softc; nppp < NPPP; nppp++, sc++)
237 if (sc->sc_xfer == pid) {
241 for (nppp = 0, sc = ppp_softc; nppp < NPPP; nppp++, sc++)
242 if (sc->sc_devp == NULL)
248 sc->sc_mru = PPP_MRU;
249 sc->sc_relinq = NULL;
250 bzero((char *)&sc->sc_stats, sizeof(sc->sc_stats));
252 MALLOC(sc->sc_comp, struct slcompress *, sizeof(struct slcompress),
255 sl_compress_init(sc->sc_comp, -1);
258 sc->sc_xc_state = NULL;
259 sc->sc_rc_state = NULL;
260 #endif /* PPP_COMPRESS */
261 for (i = 0; i < NUM_NP; ++i)
262 sc->sc_npmode[i] = NPMODE_ERROR;
263 sc->sc_npqueue = NULL;
264 sc->sc_npqtail = &sc->sc_npqueue;
265 sc->sc_last_sent = sc->sc_last_recv = time_second;
271 * Deallocate a ppp unit. Must be called at splsoftnet or higher.
275 struct ppp_softc *sc;
280 sc->sc_if.if_flags &= ~(IFF_UP|IFF_RUNNING);
281 getmicrotime(&sc->sc_if.if_lastchange);
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;
308 sc->sc_xc_state = NULL;
309 sc->sc_rc_state = NULL;
310 #endif /* PPP_COMPRESS */
312 if (sc->sc_pass_filt.bf_insns != 0) {
313 free(sc->sc_pass_filt.bf_insns, M_DEVBUF);
314 sc->sc_pass_filt.bf_insns = 0;
315 sc->sc_pass_filt.bf_len = 0;
317 if (sc->sc_active_filt.bf_insns != 0) {
318 free(sc->sc_active_filt.bf_insns, M_DEVBUF);
319 sc->sc_active_filt.bf_insns = 0;
320 sc->sc_active_filt.bf_len = 0;
322 #endif /* PPP_FILTER */
324 if (sc->sc_comp != 0) {
325 free(sc->sc_comp, M_DEVBUF);
332 * Ioctl routine for generic ppp devices.
335 pppioctl(sc, cmd, data, flag, p)
336 struct ppp_softc *sc;
342 int s, error, flags, mru, npx;
344 struct ppp_option_data *odp;
345 struct compressor **cp;
349 struct bpf_program *bp, *nbp;
350 struct bpf_insn *newcode, *oldcode;
352 #endif /* PPP_FILTER */
354 u_char ccp_option[CCP_MAX_OPTION_LENGTH];
359 *(int *)data = sc->sc_inq.ifq_len;
363 *(int *)data = sc->sc_if.if_unit;
367 *(u_int *)data = sc->sc_flags;
371 if ((error = suser(p)) != 0)
373 flags = *(int *)data & SC_MASK;
376 if (sc->sc_flags & SC_CCP_OPEN && !(flags & SC_CCP_OPEN))
380 sc->sc_flags = (sc->sc_flags & ~SC_MASK) | flags;
385 if ((error = suser(p)) != 0)
388 if (mru >= PPP_MRU && mru <= PPP_MAXMRU)
393 *(int *)data = sc->sc_mru;
398 if ((error = suser(p)) != 0)
402 sl_compress_init(sc->sc_comp, *(int *)data);
409 if ((error = suser(p)) != 0)
411 sc->sc_xfer = p->p_pid;
415 case PPPIOCSCOMPRESS:
416 if ((error = suser(p)) != 0)
418 odp = (struct ppp_option_data *) data;
420 if (nb > sizeof(ccp_option))
421 nb = sizeof(ccp_option);
422 if ((error = copyin(odp->ptr, ccp_option, nb)) != 0)
424 if (ccp_option[1] < 2) /* preliminary check on the length byte */
426 for (cp = ppp_compressors; *cp != NULL; ++cp)
427 if ((*cp)->compress_proto == ccp_option[0]) {
429 * Found a handler for the protocol - try to allocate
430 * a compressor or decompressor.
435 if (sc->sc_xc_state != NULL)
436 (*sc->sc_xcomp->comp_free)(sc->sc_xc_state);
438 sc->sc_xc_state = (*cp)->comp_alloc(ccp_option, nb);
439 if (sc->sc_xc_state == NULL) {
440 if (sc->sc_flags & SC_DEBUG)
441 printf("ppp%d: comp_alloc failed\n",
446 sc->sc_flags &= ~SC_COMP_RUN;
450 if (sc->sc_rc_state != NULL)
451 (*sc->sc_rcomp->decomp_free)(sc->sc_rc_state);
453 sc->sc_rc_state = (*cp)->decomp_alloc(ccp_option, nb);
454 if (sc->sc_rc_state == NULL) {
455 if (sc->sc_flags & SC_DEBUG)
456 printf("ppp%d: decomp_alloc failed\n",
461 sc->sc_flags &= ~SC_DECOMP_RUN;
466 if (sc->sc_flags & SC_DEBUG)
467 printf("ppp%d: no compressor for [%x %x %x], %x\n",
468 sc->sc_if.if_unit, ccp_option[0], ccp_option[1],
470 return (EINVAL); /* no handler found */
471 #endif /* PPP_COMPRESS */
475 npi = (struct npioctl *) data;
476 switch (npi->protocol) {
483 if (cmd == PPPIOCGNPMODE) {
484 npi->mode = sc->sc_npmode[npx];
486 if ((error = suser(p)) != 0)
488 if (npi->mode != sc->sc_npmode[npx]) {
490 sc->sc_npmode[npx] = npi->mode;
491 if (npi->mode != NPMODE_QUEUE) {
503 ((struct ppp_idle *)data)->xmit_idle = t - sc->sc_last_sent;
504 ((struct ppp_idle *)data)->recv_idle = t - sc->sc_last_recv;
511 nbp = (struct bpf_program *) data;
512 if ((unsigned) nbp->bf_len > BPF_MAXINSNS)
514 newcodelen = nbp->bf_len * sizeof(struct bpf_insn);
515 if (newcodelen != 0) {
516 MALLOC(newcode, struct bpf_insn *, newcodelen, M_DEVBUF, M_WAITOK);
518 return EINVAL; /* or sumpin */
520 if ((error = copyin((caddr_t)nbp->bf_insns, (caddr_t)newcode,
522 free(newcode, M_DEVBUF);
525 if (!bpf_validate(newcode, nbp->bf_len)) {
526 free(newcode, M_DEVBUF);
531 bp = (cmd == PPPIOCSPASS)? &sc->sc_pass_filt: &sc->sc_active_filt;
532 oldcode = bp->bf_insns;
534 bp->bf_len = nbp->bf_len;
535 bp->bf_insns = newcode;
538 free(oldcode, M_DEVBUF);
549 * Process an ioctl request to the ppp network interface.
552 pppsioctl(ifp, cmd, data)
553 register struct ifnet *ifp;
557 struct proc *p = curproc; /* XXX */
558 register struct ppp_softc *sc = &ppp_softc[ifp->if_unit];
559 register struct ifaddr *ifa = (struct ifaddr *)data;
560 register struct ifreq *ifr = (struct ifreq *)data;
561 struct ppp_stats *psp;
563 struct ppp_comp_stats *pcp;
565 int s = splimp(), error = 0;
569 if ((ifp->if_flags & IFF_RUNNING) == 0)
570 ifp->if_flags &= ~IFF_UP;
575 switch(ifa->ifa_addr->sa_family) {
585 error = EAFNOSUPPORT;
591 switch(ifa->ifa_addr->sa_family) {
601 error = EAFNOSUPPORT;
607 if ((error = suser(p)) != 0)
609 if (ifr->ifr_mtu > PPP_MAXMTU)
612 sc->sc_if.if_mtu = ifr->ifr_mtu;
614 (*sc->sc_setmtu)(sc);
619 ifr->ifr_mtu = sc->sc_if.if_mtu;
625 error = EAFNOSUPPORT;
628 switch(ifr->ifr_addr.sa_family) {
634 error = EAFNOSUPPORT;
640 psp = &((struct ifpppstatsreq *) data)->stats;
641 bzero(psp, sizeof(*psp));
642 psp->p = sc->sc_stats;
643 #if defined(VJC) && !defined(SL_NO_STATS)
645 psp->vj.vjs_packets = sc->sc_comp->sls_packets;
646 psp->vj.vjs_compressed = sc->sc_comp->sls_compressed;
647 psp->vj.vjs_searches = sc->sc_comp->sls_searches;
648 psp->vj.vjs_misses = sc->sc_comp->sls_misses;
649 psp->vj.vjs_uncompressedin = sc->sc_comp->sls_uncompressedin;
650 psp->vj.vjs_compressedin = sc->sc_comp->sls_compressedin;
651 psp->vj.vjs_errorin = sc->sc_comp->sls_errorin;
652 psp->vj.vjs_tossed = sc->sc_comp->sls_tossed;
659 pcp = &((struct ifpppcstatsreq *) data)->stats;
660 bzero(pcp, sizeof(*pcp));
661 if (sc->sc_xc_state != NULL)
662 (*sc->sc_xcomp->comp_stat)(sc->sc_xc_state, &pcp->c);
663 if (sc->sc_rc_state != NULL)
664 (*sc->sc_rcomp->decomp_stat)(sc->sc_rc_state, &pcp->d);
666 #endif /* PPP_COMPRESS */
676 * Queue a packet. Start transmission if not active.
677 * Packet is placed in Information field of PPP frame.
678 * Called at splnet as the if->if_output handler.
679 * Called at splnet from pppwrite().
682 pppoutput(ifp, m0, dst, rtp)
685 struct sockaddr *dst;
688 register struct ppp_softc *sc = &ppp_softc[ifp->if_unit];
689 int protocol, address, control;
698 if (sc->sc_devp == NULL || (ifp->if_flags & IFF_RUNNING) == 0
699 || ((ifp->if_flags & IFF_UP) == 0 && dst->sa_family != AF_UNSPEC)) {
700 error = ENETDOWN; /* sort of */
705 * Compute PPP header.
707 m0->m_flags &= ~M_HIGHPRI;
708 switch (dst->sa_family) {
711 address = PPP_ALLSTATIONS;
714 mode = sc->sc_npmode[NP_IP];
717 * If this packet has the "low delay" bit set in the IP header,
718 * put it on the fastq instead.
720 ip = mtod(m0, struct ip *);
721 if (ip->ip_tos & IPTOS_LOWDELAY)
722 m0->m_flags |= M_HIGHPRI;
728 * This is pretty bogus.. We dont have an ipxcp module in pppd
729 * yet to configure the link parameters. Sigh. I guess a
730 * manual ifconfig would do.... -Peter
732 address = PPP_ALLSTATIONS;
739 address = PPP_ADDRESS(dst->sa_data);
740 control = PPP_CONTROL(dst->sa_data);
741 protocol = PPP_PROTOCOL(dst->sa_data);
745 printf("ppp%d: af%d not supported\n", ifp->if_unit, dst->sa_family);
746 error = EAFNOSUPPORT;
751 * Drop this packet, or return an error, if necessary.
753 if (mode == NPMODE_ERROR) {
757 if (mode == NPMODE_DROP) {
763 * Add PPP header. If no space in first mbuf, allocate another.
764 * (This assumes M_LEADINGSPACE is always 0 for a cluster mbuf.)
766 if (M_LEADINGSPACE(m0) < PPP_HDRLEN) {
767 m0 = m_prepend(m0, PPP_HDRLEN, M_DONTWAIT);
774 m0->m_data -= PPP_HDRLEN;
776 cp = mtod(m0, u_char *);
779 *cp++ = protocol >> 8;
780 *cp++ = protocol & 0xff;
781 m0->m_len += PPP_HDRLEN;
784 for (m = m0; m != 0; m = m->m_next)
787 if (sc->sc_flags & SC_LOG_OUTPKT) {
788 printf("ppp%d output: ", ifp->if_unit);
792 if ((protocol & 0x8000) == 0) {
795 * Apply the pass and active filters to the packet,
796 * but only if it is a data packet.
798 *mtod(m0, u_char *) = 1; /* indicates outbound */
799 if (sc->sc_pass_filt.bf_insns != 0
800 && bpf_filter(sc->sc_pass_filt.bf_insns, (u_char *) m0,
802 error = 0; /* drop this packet */
807 * Update the time we sent the most recent packet.
809 if (sc->sc_active_filt.bf_insns == 0
810 || bpf_filter(sc->sc_active_filt.bf_insns, (u_char *) m0, len, 0))
811 sc->sc_last_sent = time_second;
813 *mtod(m0, u_char *) = address;
816 * Update the time we sent the most recent data packet.
818 sc->sc_last_sent = time_second;
819 #endif /* PPP_FILTER */
823 * See if bpf wants to look at the packet.
829 * Put the packet on the appropriate queue.
831 s = splsoftnet(); /* redundant */
832 if (mode == NPMODE_QUEUE) {
833 /* XXX we should limit the number of packets on this queue */
834 *sc->sc_npqtail = m0;
835 m0->m_nextpkt = NULL;
836 sc->sc_npqtail = &m0->m_nextpkt;
838 /* fastq and if_snd are emptied at spl[soft]net now */
839 ifq = (m0->m_flags & M_HIGHPRI)? &sc->sc_fastq: &ifp->if_snd;
840 if (IF_QFULL(ifq) && dst->sa_family != AF_UNSPEC) {
843 sc->sc_if.if_oerrors++;
844 sc->sc_stats.ppp_oerrors++;
851 getmicrotime(&ifp->if_lastchange);
853 ifp->if_obytes += len;
864 * After a change in the NPmode for some NP, move packets from the
865 * npqueue to the send queue or the fast queue as appropriate.
866 * Should be called at spl[soft]net.
870 struct ppp_softc *sc;
872 struct mbuf *m, **mpp;
876 for (mpp = &sc->sc_npqueue; (m = *mpp) != NULL; ) {
877 switch (PPP_PROTOCOL(mtod(m, u_char *))) {
879 mode = sc->sc_npmode[NP_IP];
888 * This packet can now go on one of the queues to be sent.
892 ifq = (m->m_flags & M_HIGHPRI)? &sc->sc_fastq: &sc->sc_if.if_snd;
895 sc->sc_if.if_oerrors++;
896 sc->sc_stats.ppp_oerrors++;
912 sc->sc_npqtail = mpp;
916 * Transmitter has finished outputting some stuff;
917 * remember to call sc->sc_start later at splsoftnet.
921 struct ppp_softc *sc;
925 sc->sc_flags &= ~SC_TBUSY;
926 schednetisr(NETISR_PPP);
932 * Get a packet to send. This procedure is intended to be called at
933 * splsoftnet, since it may involve time-consuming operations such as
934 * applying VJ compression, packet compression, address/control and/or
935 * protocol field compression to the packet.
939 struct ppp_softc *sc;
943 int address, control, protocol;
946 * Grab a packet to send: first try the fast queue, then the
949 IF_DEQUEUE(&sc->sc_fastq, m);
951 IF_DEQUEUE(&sc->sc_if.if_snd, m);
955 ++sc->sc_stats.ppp_opackets;
958 * Extract the ppp header of the new packet.
959 * The ppp header will be in one mbuf.
961 cp = mtod(m, u_char *);
962 address = PPP_ADDRESS(cp);
963 control = PPP_CONTROL(cp);
964 protocol = PPP_PROTOCOL(cp);
970 * If the packet is a TCP/IP packet, see if we can compress it.
972 if ((sc->sc_flags & SC_COMP_TCP) && sc->sc_comp != NULL) {
977 ip = (struct ip *) (cp + PPP_HDRLEN);
978 if (mp->m_len <= PPP_HDRLEN) {
982 ip = mtod(mp, struct ip *);
984 /* this code assumes the IP/TCP header is in one non-shared mbuf */
985 if (ip->ip_p == IPPROTO_TCP) {
986 type = sl_compress_tcp(mp, ip, sc->sc_comp,
987 !(sc->sc_flags & SC_NO_TCP_CCID));
989 case TYPE_UNCOMPRESSED_TCP:
990 protocol = PPP_VJC_UNCOMP;
992 case TYPE_COMPRESSED_TCP:
993 protocol = PPP_VJC_COMP;
994 cp = mtod(m, u_char *);
995 cp[0] = address; /* header has moved */
1000 cp[3] = protocol; /* update protocol in PPP header */
1010 #endif /* PPP_COMPRESS */
1014 if (protocol != PPP_LCP && protocol != PPP_CCP
1015 && sc->sc_xc_state && (sc->sc_flags & SC_COMP_RUN)) {
1016 struct mbuf *mcomp = NULL;
1020 for (mp = m; mp != NULL; mp = mp->m_next)
1022 clen = (*sc->sc_xcomp->compress)
1023 (sc->sc_xc_state, &mcomp, m, slen, sc->sc_if.if_mtu + PPP_HDRLEN);
1024 if (mcomp != NULL) {
1025 if (sc->sc_flags & SC_CCP_UP) {
1026 /* Send the compressed packet instead of the original. */
1029 cp = mtod(m, u_char *);
1032 /* Can't transmit compressed packets until CCP is up. */
1037 #endif /* PPP_COMPRESS */
1040 * Compress the address/control and protocol, if possible.
1042 if (sc->sc_flags & SC_COMP_AC && address == PPP_ALLSTATIONS &&
1043 control == PPP_UI && protocol != PPP_ALLSTATIONS &&
1044 protocol != PPP_LCP) {
1045 /* can compress address/control */
1049 if (sc->sc_flags & SC_COMP_PROT && protocol < 0xFF) {
1050 /* can compress protocol */
1051 if (mtod(m, u_char *) == cp) {
1052 cp[2] = cp[1]; /* move address/control up */
1063 * Software interrupt routine, called at spl[soft]net.
1068 struct ppp_softc *sc;
1073 for (i = 0; i < NPPP; ++i, ++sc) {
1075 if (!(sc->sc_flags & SC_TBUSY)
1076 && (sc->sc_if.if_snd.ifq_head || sc->sc_fastq.ifq_head)) {
1077 sc->sc_flags |= SC_TBUSY;
1079 (*sc->sc_start)(sc);
1084 IF_DEQUEUE(&sc->sc_rawq, m);
1095 * Handle a CCP packet. `rcvd' is 1 if the packet was received,
1096 * 0 if it is about to be transmitted.
1099 ppp_ccp(sc, m, rcvd)
1100 struct ppp_softc *sc;
1109 * Get a pointer to the data after the PPP header.
1111 if (m->m_len <= PPP_HDRLEN) {
1115 dp = (mp != NULL)? mtod(mp, u_char *): NULL;
1118 dp = mtod(mp, u_char *) + PPP_HDRLEN;
1121 ep = mtod(mp, u_char *) + mp->m_len;
1122 if (dp + CCP_HDRLEN > ep)
1124 slen = CCP_LENGTH(dp);
1125 if (dp + slen > ep) {
1126 if (sc->sc_flags & SC_DEBUG)
1127 printf("if_ppp/ccp: not enough data in mbuf (%p+%x > %p+%x)\n",
1128 dp, slen, mtod(mp, u_char *), mp->m_len);
1132 switch (CCP_CODE(dp)) {
1136 /* CCP must be going down - disable compression */
1137 if (sc->sc_flags & SC_CCP_UP) {
1139 sc->sc_flags &= ~(SC_CCP_UP | SC_COMP_RUN | SC_DECOMP_RUN);
1145 if (sc->sc_flags & SC_CCP_OPEN && !(sc->sc_flags & SC_CCP_UP)
1146 && slen >= CCP_HDRLEN + CCP_OPT_MINLEN
1147 && slen >= CCP_OPT_LENGTH(dp + CCP_HDRLEN) + CCP_HDRLEN) {
1149 /* we're agreeing to send compressed packets. */
1150 if (sc->sc_xc_state != NULL
1151 && (*sc->sc_xcomp->comp_init)
1152 (sc->sc_xc_state, dp + CCP_HDRLEN, slen - CCP_HDRLEN,
1153 sc->sc_if.if_unit, 0, sc->sc_flags & SC_DEBUG)) {
1155 sc->sc_flags |= SC_COMP_RUN;
1159 /* peer is agreeing to send compressed packets. */
1160 if (sc->sc_rc_state != NULL
1161 && (*sc->sc_rcomp->decomp_init)
1162 (sc->sc_rc_state, dp + CCP_HDRLEN, slen - CCP_HDRLEN,
1163 sc->sc_if.if_unit, 0, sc->sc_mru,
1164 sc->sc_flags & SC_DEBUG)) {
1166 sc->sc_flags |= SC_DECOMP_RUN;
1167 sc->sc_flags &= ~(SC_DC_ERROR | SC_DC_FERROR);
1175 if (sc->sc_flags & SC_CCP_UP) {
1177 if (sc->sc_xc_state && (sc->sc_flags & SC_COMP_RUN))
1178 (*sc->sc_xcomp->comp_reset)(sc->sc_xc_state);
1180 if (sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)) {
1181 (*sc->sc_rcomp->decomp_reset)(sc->sc_rc_state);
1183 sc->sc_flags &= ~SC_DC_ERROR;
1193 * CCP is down; free (de)compressor state if necessary.
1197 struct ppp_softc *sc;
1199 if (sc->sc_xc_state) {
1200 (*sc->sc_xcomp->comp_free)(sc->sc_xc_state);
1201 sc->sc_xc_state = NULL;
1203 if (sc->sc_rc_state) {
1204 (*sc->sc_rcomp->decomp_free)(sc->sc_rc_state);
1205 sc->sc_rc_state = NULL;
1208 #endif /* PPP_COMPRESS */
1211 * PPP packet input routine.
1212 * The caller has checked and removed the FCS and has inserted
1213 * the address/control bytes and the protocol high byte if they
1217 ppppktin(sc, m, lost)
1218 struct ppp_softc *sc;
1225 m->m_flags |= M_ERRMARK;
1226 IF_ENQUEUE(&sc->sc_rawq, m);
1227 schednetisr(NETISR_PPP);
1232 * Process a received PPP packet, doing decompression as necessary.
1233 * Should be called at splsoftnet.
1235 #define COMPTYPE(proto) ((proto) == PPP_VJC_COMP? TYPE_COMPRESSED_TCP: \
1236 TYPE_UNCOMPRESSED_TCP)
1240 struct ppp_softc *sc;
1243 struct ifnet *ifp = &sc->sc_if;
1244 struct ifqueue *inq;
1245 int s, ilen = 0, xlen, proto, rv;
1246 u_char *cp, adrs, ctrl;
1247 struct mbuf *mp, *dmp = NULL;
1251 sc->sc_stats.ppp_ipackets++;
1253 if (sc->sc_flags & SC_LOG_INPKT) {
1255 for (mp = m; mp != NULL; mp = mp->m_next)
1257 printf("ppp%d: got %d bytes\n", ifp->if_unit, ilen);
1261 cp = mtod(m, u_char *);
1262 adrs = PPP_ADDRESS(cp);
1263 ctrl = PPP_CONTROL(cp);
1264 proto = PPP_PROTOCOL(cp);
1266 if (m->m_flags & M_ERRMARK) {
1267 m->m_flags &= ~M_ERRMARK;
1269 sc->sc_flags |= SC_VJ_RESET;
1275 * Decompress this packet if necessary, update the receiver's
1276 * dictionary, or take appropriate action on a CCP packet.
1278 if (proto == PPP_COMP && sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)
1279 && !(sc->sc_flags & SC_DC_ERROR) && !(sc->sc_flags & SC_DC_FERROR)) {
1280 /* decompress this packet */
1281 rv = (*sc->sc_rcomp->decompress)(sc->sc_rc_state, m, &dmp);
1282 if (rv == DECOMP_OK) {
1285 /* no error, but no decompressed packet produced */
1289 cp = mtod(m, u_char *);
1290 proto = PPP_PROTOCOL(cp);
1294 * An error has occurred in decompression.
1295 * Pass the compressed packet up to pppd, which may take
1296 * CCP down or issue a Reset-Req.
1298 if (sc->sc_flags & SC_DEBUG)
1299 printf("ppp%d: decompress failed %d\n", ifp->if_unit, rv);
1301 sc->sc_flags |= SC_VJ_RESET;
1302 if (rv == DECOMP_ERROR)
1303 sc->sc_flags |= SC_DC_ERROR;
1305 sc->sc_flags |= SC_DC_FERROR;
1310 if (sc->sc_rc_state && (sc->sc_flags & SC_DECOMP_RUN)) {
1311 (*sc->sc_rcomp->incomp)(sc->sc_rc_state, m);
1313 if (proto == PPP_CCP) {
1320 for (mp = m; mp != NULL; mp = mp->m_next)
1324 if (sc->sc_flags & SC_VJ_RESET) {
1326 * If we've missed a packet, we must toss subsequent compressed
1327 * packets which don't have an explicit connection ID.
1330 sl_uncompress_tcp(NULL, 0, TYPE_ERROR, sc->sc_comp);
1332 sc->sc_flags &= ~SC_VJ_RESET;
1337 * See if we have a VJ-compressed packet to uncompress.
1339 if (proto == PPP_VJC_COMP) {
1340 if ((sc->sc_flags & SC_REJ_COMP_TCP) || sc->sc_comp == 0)
1343 xlen = sl_uncompress_tcp_core(cp + PPP_HDRLEN, m->m_len - PPP_HDRLEN,
1344 ilen - PPP_HDRLEN, TYPE_COMPRESSED_TCP,
1345 sc->sc_comp, &iphdr, &hlen);
1348 if (sc->sc_flags & SC_DEBUG)
1349 printf("ppp%d: VJ uncompress failed on type comp\n",
1354 /* Copy the PPP and IP headers into a new mbuf. */
1355 MGETHDR(mp, M_DONTWAIT, MT_DATA);
1360 if (hlen + PPP_HDRLEN > MHLEN) {
1361 MCLGET(mp, M_DONTWAIT);
1362 if (M_TRAILINGSPACE(mp) < hlen + PPP_HDRLEN) {
1364 goto bad; /* lose if big headers and no clusters */
1367 cp = mtod(mp, u_char *);
1373 bcopy(iphdr, cp + PPP_HDRLEN, hlen);
1374 mp->m_len = hlen + PPP_HDRLEN;
1377 * Trim the PPP and VJ headers off the old mbuf
1378 * and stick the new and old mbufs together.
1380 m->m_data += PPP_HDRLEN + xlen;
1381 m->m_len -= PPP_HDRLEN + xlen;
1382 if (m->m_len <= M_TRAILINGSPACE(mp)) {
1383 bcopy(mtod(m, u_char *), mtod(mp, u_char *) + mp->m_len, m->m_len);
1384 mp->m_len += m->m_len;
1385 mp->m_next = m_free(m);
1390 ilen += hlen - xlen;
1392 } else if (proto == PPP_VJC_UNCOMP) {
1393 if ((sc->sc_flags & SC_REJ_COMP_TCP) || sc->sc_comp == 0)
1396 xlen = sl_uncompress_tcp_core(cp + PPP_HDRLEN, m->m_len - PPP_HDRLEN,
1397 ilen - PPP_HDRLEN, TYPE_UNCOMPRESSED_TCP,
1398 sc->sc_comp, &iphdr, &hlen);
1401 if (sc->sc_flags & SC_DEBUG)
1402 printf("ppp%d: VJ uncompress failed on type uncomp\n",
1413 * If the packet will fit in a header mbuf, don't waste a
1414 * whole cluster on it.
1416 if (ilen <= MHLEN && M_IS_CLUSTER(m)) {
1417 MGETHDR(mp, M_DONTWAIT, MT_DATA);
1419 m_copydata(m, 0, ilen, mtod(mp, caddr_t));
1425 m->m_pkthdr.len = ilen;
1426 m->m_pkthdr.rcvif = ifp;
1428 if ((proto & 0x8000) == 0) {
1431 * See whether we want to pass this packet, and
1432 * if it counts as link activity.
1434 adrs = *mtod(m, u_char *); /* save address field */
1435 *mtod(m, u_char *) = 0; /* indicate inbound */
1436 if (sc->sc_pass_filt.bf_insns != 0
1437 && bpf_filter(sc->sc_pass_filt.bf_insns, (u_char *) m,
1439 /* drop this packet */
1443 if (sc->sc_active_filt.bf_insns == 0
1444 || bpf_filter(sc->sc_active_filt.bf_insns, (u_char *) m, ilen, 0))
1445 sc->sc_last_recv = time_second;
1447 *mtod(m, u_char *) = adrs;
1450 * Record the time that we received this packet.
1452 sc->sc_last_recv = time_second;
1453 #endif /* PPP_FILTER */
1456 /* See if bpf wants to look at the packet. */
1457 if (sc->sc_if.if_bpf)
1458 bpf_mtap(&sc->sc_if, m);
1465 * IP packet - take off the ppp header and pass it up to IP.
1467 if ((ifp->if_flags & IFF_UP) == 0
1468 || sc->sc_npmode[NP_IP] != NPMODE_PASS) {
1469 /* interface is down - drop the packet. */
1473 m->m_pkthdr.len -= PPP_HDRLEN;
1474 m->m_data += PPP_HDRLEN;
1475 m->m_len -= PPP_HDRLEN;
1476 if (ipflow_fastforward(m))
1478 schednetisr(NETISR_IP);
1485 * IPX packet - take off the ppp header and pass it up to IPX.
1487 if ((sc->sc_if.if_flags & IFF_UP) == 0
1488 /* XXX: || sc->sc_npmode[NP_IPX] != NPMODE_PASS*/) {
1489 /* interface is down - drop the packet. */
1493 m->m_pkthdr.len -= PPP_HDRLEN;
1494 m->m_data += PPP_HDRLEN;
1495 m->m_len -= PPP_HDRLEN;
1496 schednetisr(NETISR_IPX);
1498 sc->sc_last_recv = time_second; /* update time of last pkt rcvd */
1504 * Some other protocol - place on input queue for read().
1512 * Put the packet on the appropriate input queue.
1515 if (IF_QFULL(inq)) {
1518 if (sc->sc_flags & SC_DEBUG)
1519 printf("ppp%d: input queue full\n", ifp->if_unit);
1526 ifp->if_ibytes += ilen;
1527 getmicrotime(&ifp->if_lastchange);
1536 sc->sc_if.if_ierrors++;
1537 sc->sc_stats.ppp_ierrors++;
1540 #define MAX_DUMP_BYTES 128
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 printf("%s\n", buf);