📄 if_ppp.c
字号:
/* $NetBSD: if_ppp.c,v 1.46 1998/08/02 15:09:50 sommerfe Exp $ *//* $Id: if_ppp.c,v 1.5 1998/09/02 21:19:44 christos Exp $ *//* * if_ppp.c - Point-to-Point Protocol (PPP) Asynchronous driver. * * Copyright (c) 1989 Carnegie Mellon University. * All rights reserved. * * Redistribution and use in source and binary forms are permitted * provided that the above copyright notice and this paragraph are * duplicated in all such forms and that any documentation, * advertising materials, and other materials related to such * distribution and use acknowledge that the software was developed * by Carnegie Mellon University. The name of the * University may not be used to endorse or promote products derived * from this software without specific prior written permission. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. * * Drew D. Perkins * Carnegie Mellon University * 4910 Forbes Ave. * Pittsburgh, PA 15213 * (412) 268-8576 * ddp@andrew.cmu.edu * * Based on: * @(#)if_sl.c 7.6.1.2 (Berkeley) 2/15/89 * * Copyright (c) 1987 Regents of the University of California. * All rights reserved. * * Redistribution and use in source and binary forms are permitted * provided that the above copyright notice and this paragraph are * duplicated in all such forms and that any documentation, * advertising materials, and other materials related to such * distribution and use acknowledge that the software was developed * by the University of California, Berkeley. The name of the * University may not be used to endorse or promote products derived * from this software without specific prior written permission. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. * * Serial Line interface * * Rick Adams * Center for Seismic Studies * 1300 N 17th Street, Suite 1450 * Arlington, Virginia 22209 * (703)276-7900 * rick@seismo.ARPA * seismo!rick * * Pounded on heavily by Chris Torek (chris@mimsy.umd.edu, umcp-cs!chris). * Converted to 4.3BSD Beta by Chris Torek. * Other changes made at Berkeley, based in part on code by Kirk Smith. * * Converted to 4.3BSD+ 386BSD by Brad Parker (brad@cayman.com) * Added VJ tcp header compression; more unified ioctls * * Extensively modified by Paul Mackerras (paulus@cs.anu.edu.au). * Cleaned up a lot of the mbuf-related code to fix bugs that * caused system crashes and packet corruption. Changed pppstart * so that it doesn't just give up with a collision if the whole * packet doesn't fit in the output ring buffer. * * Added priority queueing for interactive IP packets, following * the model of if_sl.c, plus hooks for bpf. * Paul Mackerras (paulus@cs.anu.edu.au). *//* from if_sl.c,v 1.11 84/10/04 12:54:47 rick Exp *//* from NetBSD: if_ppp.c,v 1.15.2.2 1994/07/28 05:17:58 cgd Exp */#include "ppp.h"#if NPPP > 0#define VJC#define PPP_COMPRESS#include <sys/param.h>#ifdef __NetBSD_Version__ /* Post 1.3 */#include "opt_inet.h"#include "opt_gateway.h"#endif#include <sys/proc.h>#include <sys/mbuf.h>#include <sys/socket.h>#include <sys/ioctl.h>#include <sys/kernel.h>#include <sys/systm.h>#include <sys/time.h>#include <sys/malloc.h>#include <net/if.h>#include <net/if_types.h>#include <net/netisr.h>#include <net/route.h>#ifdef PPP_FILTER#include <net/bpf.h>#endif#ifdef INET#include <netinet/in.h>#include <netinet/in_systm.h>#include <netinet/in_var.h>#include <netinet/ip.h>#endif#include "bpfilter.h"#if NBPFILTER > 0#include <sys/time.h>#include <net/bpf.h>#endif#ifdef VJC#include <net/slcompress.h>#endif#include <net/ppp_defs.h>#include <net/if_ppp.h>#include <net/if_pppvar.h>#include <machine/cpu.h>#ifdef PPP_COMPRESS#define PACKETPTR struct mbuf *#include <net/ppp-comp.h>#endifstatic int pppsioctl __P((struct ifnet *, u_long, caddr_t));static void ppp_requeue __P((struct ppp_softc *));static void ppp_ccp __P((struct ppp_softc *, struct mbuf *m, int rcvd));static void ppp_ccp_closed __P((struct ppp_softc *));static void ppp_inproc __P((struct ppp_softc *, struct mbuf *));static void pppdumpm __P((struct mbuf *m0));/* * Some useful mbuf macros not in mbuf.h. */#define M_IS_CLUSTER(m) ((m)->m_flags & M_EXT)#define M_DATASTART(m) \ (M_IS_CLUSTER(m) ? (m)->m_ext.ext_buf : \ (m)->m_flags & M_PKTHDR ? (m)->m_pktdat : (m)->m_dat)#define M_DATASIZE(m) \ (M_IS_CLUSTER(m) ? (m)->m_ext.ext_size : \ (m)->m_flags & M_PKTHDR ? MHLEN: MLEN)/* * We steal two bits in the mbuf m_flags, to mark high-priority packets * for output, and received packets following lost/corrupted packets. */#define M_HIGHPRI 0x2000 /* output packet for sc_fastq */#define M_ERRMARK 0x4000 /* steal a bit in mbuf m_flags */#ifdef PPP_COMPRESS/* * List of compressors we know about. * We leave some space so maybe we can modload compressors. */extern struct compressor ppp_bsd_compress;extern struct compressor ppp_deflate, ppp_deflate_draft;struct compressor *ppp_compressors[8] = {#if DO_BSD_COMPRESS && defined(PPP_BSDCOMP) &ppp_bsd_compress,#endif#if DO_DEFLATE && defined(PPP_DEFLATE) &ppp_deflate, &ppp_deflate_draft,#endif NULL};#endif /* PPP_COMPRESS *//* * Called from boot code to establish ppp interfaces. */voidpppattach(){ register struct ppp_softc *sc; register int i = 0; for (sc = ppp_softc; i < NPPP; sc++) { sc->sc_unit = i; /* XXX */ sprintf(sc->sc_if.if_xname, "ppp%d", i++); sc->sc_if.if_softc = sc; sc->sc_if.if_mtu = PPP_MTU; sc->sc_if.if_flags = IFF_POINTOPOINT | IFF_MULTICAST; sc->sc_if.if_type = IFT_PPP; sc->sc_if.if_hdrlen = PPP_HDRLEN; sc->sc_if.if_ioctl = pppsioctl; sc->sc_if.if_output = pppoutput; sc->sc_if.if_snd.ifq_maxlen = IFQ_MAXLEN; sc->sc_inq.ifq_maxlen = IFQ_MAXLEN; sc->sc_fastq.ifq_maxlen = IFQ_MAXLEN; sc->sc_rawq.ifq_maxlen = IFQ_MAXLEN; if_attach(&sc->sc_if);#if NBPFILTER > 0 bpfattach(&sc->sc_bpf, &sc->sc_if, DLT_PPP, PPP_HDRLEN);#endif }}/* * Allocate a ppp interface unit and initialize it. */struct ppp_softc *pppalloc(pid) pid_t pid;{ int nppp, i; struct ppp_softc *sc; for (nppp = 0, sc = ppp_softc; nppp < NPPP; nppp++, sc++) if (sc->sc_xfer == pid) { sc->sc_xfer = 0; return sc; } for (nppp = 0, sc = ppp_softc; nppp < NPPP; nppp++, sc++) if (sc->sc_devp == NULL) break; if (nppp >= NPPP) return NULL; sc->sc_flags = 0; sc->sc_mru = PPP_MRU; sc->sc_relinq = NULL; bzero((char *)&sc->sc_stats, sizeof(sc->sc_stats));#ifdef VJC MALLOC(sc->sc_comp, struct slcompress *, sizeof(struct slcompress), M_DEVBUF, M_NOWAIT); if (sc->sc_comp) sl_compress_init(sc->sc_comp);#endif#ifdef PPP_COMPRESS sc->sc_xc_state = NULL; sc->sc_rc_state = NULL;#endif /* PPP_COMPRESS */ for (i = 0; i < NUM_NP; ++i) sc->sc_npmode[i] = NPMODE_ERROR; sc->sc_npqueue = NULL; sc->sc_npqtail = &sc->sc_npqueue; sc->sc_last_sent = sc->sc_last_recv = time.tv_sec; return sc;}/* * Deallocate a ppp unit. Must be called at splsoftnet or higher. */voidpppdealloc(sc) struct ppp_softc *sc;{ struct mbuf *m; if_down(&sc->sc_if); sc->sc_if.if_flags &= ~(IFF_UP|IFF_RUNNING); sc->sc_devp = NULL; sc->sc_xfer = 0; for (;;) { IF_DEQUEUE(&sc->sc_rawq, m); if (m == NULL) break; m_freem(m); } for (;;) { IF_DEQUEUE(&sc->sc_inq, m); if (m == NULL) break; m_freem(m); } for (;;) { IF_DEQUEUE(&sc->sc_fastq, m); if (m == NULL) break; m_freem(m); } while ((m = sc->sc_npqueue) != NULL) { sc->sc_npqueue = m->m_nextpkt; m_freem(m); } if (sc->sc_togo != NULL) { m_freem(sc->sc_togo); sc->sc_togo = NULL; }#ifdef PPP_COMPRESS ppp_ccp_closed(sc); sc->sc_xc_state = NULL; sc->sc_rc_state = NULL;#endif /* PPP_COMPRESS */#ifdef PPP_FILTER if (sc->sc_pass_filt.bf_insns != 0) { FREE(sc->sc_pass_filt.bf_insns, M_DEVBUF); sc->sc_pass_filt.bf_insns = 0; sc->sc_pass_filt.bf_len = 0; } if (sc->sc_active_filt.bf_insns != 0) { FREE(sc->sc_active_filt.bf_insns, M_DEVBUF); sc->sc_active_filt.bf_insns = 0; sc->sc_active_filt.bf_len = 0; }#endif /* PPP_FILTER */#ifdef VJC if (sc->sc_comp != 0) { FREE(sc->sc_comp, M_DEVBUF); sc->sc_comp = 0; }#endif}/* * Ioctl routine for generic ppp devices. */intpppioctl(sc, cmd, data, flag, p) struct ppp_softc *sc; u_long cmd; caddr_t data; int flag; struct proc *p;{ int s, error, flags, mru, nb, npx; struct ppp_option_data *odp; struct compressor **cp; struct npioctl *npi; time_t t;#ifdef PPP_FILTER struct bpf_program *bp, *nbp; struct bpf_insn *newcode, *oldcode; int newcodelen;#endif /* PPP_FILTER */#ifdef PPP_COMPRESS u_char ccp_option[CCP_MAX_OPTION_LENGTH];#endif switch (cmd) { case FIONREAD: *(int *)data = sc->sc_inq.ifq_len; break; case PPPIOCGUNIT: *(int *)data = sc->sc_unit; /* XXX */ break; case PPPIOCGFLAGS: *(u_int *)data = sc->sc_flags; break; case PPPIOCSFLAGS: if ((error = suser(p->p_ucred, &p->p_acflag)) != 0) return (error); flags = *(int *)data & SC_MASK; s = splsoftnet();#ifdef PPP_COMPRESS if (sc->sc_flags & SC_CCP_OPEN && !(flags & SC_CCP_OPEN)) ppp_ccp_closed(sc);#endif splimp(); sc->sc_flags = (sc->sc_flags & ~SC_MASK) | flags; splx(s); break; case PPPIOCSMRU: if ((error = suser(p->p_ucred, &p->p_acflag)) != 0) return (error); mru = *(int *)data; if (mru >= PPP_MRU && mru <= PPP_MAXMRU) sc->sc_mru = mru; break; case PPPIOCGMRU: *(int *)data = sc->sc_mru; break;#ifdef VJC case PPPIOCSMAXCID: if ((error = suser(p->p_ucred, &p->p_acflag)) != 0) return (error); if (sc->sc_comp) { s = splsoftnet(); sl_compress_setup(sc->sc_comp, *(int *)data); splx(s); } break;#endif case PPPIOCXFERUNIT: if ((error = suser(p->p_ucred, &p->p_acflag)) != 0) return (error); sc->sc_xfer = p->p_pid; break;#ifdef PPP_COMPRESS case PPPIOCSCOMPRESS: if ((error = suser(p->p_ucred, &p->p_acflag)) != 0) return (error); odp = (struct ppp_option_data *) data; nb = odp->length; if (nb > sizeof(ccp_option)) nb = sizeof(ccp_option); if ((error = copyin(odp->ptr, ccp_option, nb)) != 0) return (error); if (ccp_option[1] < 2) /* preliminary check on the length byte */ return (EINVAL); for (cp = ppp_compressors; *cp != NULL; ++cp) if ((*cp)->compress_proto == ccp_option[0]) { /* * Found a handler for the protocol - try to allocate * a compressor or decompressor. */ error = 0; if (odp->transmit) { s = splsoftnet(); if (sc->sc_xc_state != NULL) (*sc->sc_xcomp->comp_free)(sc->sc_xc_state); sc->sc_xcomp = *cp; sc->sc_xc_state = (*cp)->comp_alloc(ccp_option, nb); if (sc->sc_xc_state == NULL) { if (sc->sc_flags & SC_DEBUG) printf("%s: comp_alloc failed\n", sc->sc_if.if_xname); error = ENOBUFS; } splimp(); sc->sc_flags &= ~SC_COMP_RUN; splx(s); } else { s = splsoftnet(); if (sc->sc_rc_state != NULL) (*sc->sc_rcomp->decomp_free)(sc->sc_rc_state); sc->sc_rcomp = *cp; sc->sc_rc_state = (*cp)->decomp_alloc(ccp_option, nb); if (sc->sc_rc_state == NULL) { if (sc->sc_flags & SC_DEBUG) printf("%s: decomp_alloc failed\n", sc->sc_if.if_xname); error = ENOBUFS; } splimp(); sc->sc_flags &= ~SC_DECOMP_RUN; splx(s); } return (error); } if (sc->sc_flags & SC_DEBUG) printf("%s: no compressor for [%x %x %x], %x\n", sc->sc_if.if_xname, ccp_option[0], ccp_option[1], ccp_option[2], nb); return (EINVAL); /* no handler found */#endif /* PPP_COMPRESS */ case PPPIOCGNPMODE: case PPPIOCSNPMODE: npi = (struct npioctl *) data; switch (npi->protocol) { case PPP_IP: npx = NP_IP; break; default: return EINVAL; } if (cmd == PPPIOCGNPMODE) { npi->mode = sc->sc_npmode[npx]; } else { if ((error = suser(p->p_ucred, &p->p_acflag)) != 0) return (error); if (npi->mode != sc->sc_npmode[npx]) { s = splimp(); sc->sc_npmode[npx] = npi->mode; if (npi->mode != NPMODE_QUEUE) { ppp_requeue(sc); ppp_restart(sc); } splx(s); } } break; case PPPIOCGIDLE: s = splsoftnet(); t = time.tv_sec; ((struct ppp_idle *)data)->xmit_idle = t - sc->sc_last_sent; ((struct ppp_idle *)data)->recv_idle = t - sc->sc_last_recv; splx(s); break;#ifdef PPP_FILTER case PPPIOCSPASS: case PPPIOCSACTIVE: nbp = (struct bpf_program *) data; if ((unsigned) nbp->bf_len > BPF_MAXINSNS) return EINVAL; newcodelen = nbp->bf_len * sizeof(struct bpf_insn); if (newcodelen != 0) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -