📄 if_ppp.c
字号:
/* * 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). * * Ultrix port by Per Sundstrom <sundstrom@stkhlm.enet.dec.com>, * Robert Olsson <robert@robur.slu.se> and Paul Mackerras. *//* $Id: if_ppp.c,v 1.15 1998/03/25 03:11:35 paulus Exp $ *//* 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 "../h/param.h"#include "../h/user.h"#include "../h/proc.h"#include "../h/mbuf.h"#include "../h/buf.h"#include "../h/socket.h"#include "../h/ioctl.h"#include "../h/systm.h"#include "../h/time.h"#include "../net/net/if.h"#include "../net/net/netisr.h"#include "../net/net/route.h"#if INET#include "../net/netinet/in.h"#include "../net/netinet/in_systm.h"#include "../net/netinet/in_var.h"#include "../net/netinet/ip.h"#endif#ifdef vax#include "../machine/mtpr.h"#endif#ifdef VJC#include "slcompress.h"#endif#include "ppp_defs.h"#include "if_ppp.h"#include "if_pppvar.h"#ifdef PPP_COMPRESS#define PACKETPTR struct mbuf *#include "ppp-comp.h"#endifstatic int pppsioctl(struct ifnet *, int, caddr_t);static void ppp_requeue __P((struct ppp_softc *));static void ppp_outpkt __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_off > MMAXOFF)#define M_TRAILINGSPACE(m) \ ((M_IS_CLUSTER(m) ? (u_int)(m)->m_clptr + M_CLUSTERSZ : MSIZE) \ - ((m)->m_off + (m)->m_len))#define M_OFFSTART(m) \ (M_IS_CLUSTER(m) ? (u_int)(m)->m_clptr : MMINOFF)#define M_DATASIZE(m) \ (M_IS_CLUSTER(m) ? M_CLUSTERSZ : MLEN)/* * The following disgusting hack gets around the problem that IP TOS * can't be set yet. We want to put "interactive" traffic on a high * priority queue. To decide if traffic is interactive, we check that * a) it is TCP and b) one of its ports is telnet, rlogin or ftp control. */static u_short interactive_ports[8] = { 0, 513, 0, 0, 0, 21, 0, 23,};#define INTERACTIVE(p) (interactive_ports[(p) & 7] == (p))#ifdef PPP_COMPRESS/* * List of compressors we know about. */extern struct compressor ppp_bsd_compress;extern struct compressor ppp_deflate;extern struct compressor ppp_deflate_draft;struct compressor *ppp_compressors[] = {#if DO_BSD_COMPRESS &ppp_bsd_compress,#endif#if DO_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_if.if_name = "ppp"; sc->sc_if.if_unit = i++; sc->sc_if.if_mtu = PPP_MTU; sc->sc_if.if_flags = IFF_POINTOPOINT; sc->sc_if.if_type = IFT_PPP; 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 KM_ALLOC(sc->sc_comp, struct slcompress *, sizeof(struct slcompress), KM_DEVBUF, KM_NOARG); 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 splnet 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_act; 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 VJC if (sc->sc_comp != 0) { KM_FREE(sc->sc_comp, KM_DEVBUF); sc->sc_comp = 0; }#endif}/* * Ioctl routine for generic ppp devices. */intpppioctl(sc, cmd, data, flag) struct ppp_softc *sc; caddr_t data; int cmd, flag;{ struct proc *p = u.u_procp; int s, error, flags, mru, mtu, nb, npx; struct ppp_option_data *odp; struct compressor **cp; struct npioctl *npi; time_t t;#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_if.if_unit; break; case PPPIOCGFLAGS: *(u_int *)data = sc->sc_flags; break; case PPPIOCSFLAGS: if (!suser()) return EPERM; flags = *(int *)data & SC_MASK; s = splnet();#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 (!suser()) return EPERM; mru = *(int *)data; if (mru >= PPP_MRU && mru <= PPP_MAXMRU) sc->sc_mru = mru; break; case PPPIOCGMRU: *(int *)data = sc->sc_mru; break; /* * PPPIOC[GS]MTU are implemented here, instead of supporting * SIOC[GS]IFMTU in pppsioctl, because under Ultrix, we can't get an * interface ioctl through to the interface until it has an IP * address set. */ case PPPIOCSMTU: if (!suser()) return EPERM; mtu = *(int *) data; if (mtu <= 0 || mtu > PPP_MAXMRU) return EINVAL; s = splimp(); sc->sc_if.if_mtu = mtu; splx(s); break; case PPPIOCGMTU: *(int *) data = sc->sc_if.if_mtu; break;#ifdef VJC case PPPIOCSMAXCID: if (!suser()) return EPERM; if (sc->sc_comp) { s = splnet(); sl_compress_setup(sc->sc_comp, *(int *)data); splx(s); } break;#endif case PPPIOCXFERUNIT: if (!suser()) return EPERM; sc->sc_xfer = p->p_pid; break;#ifdef PPP_COMPRESS case PPPIOCSCOMPRESS: if (!suser()) return EPERM; 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 = splnet(); 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("ppp%d: comp_alloc failed\n", sc->sc_if.if_unit); error = ENOBUFS; } splimp(); sc->sc_flags &= ~SC_COMP_RUN; splx(s); } else { s = splnet(); 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("ppp%d: decomp_alloc failed\n", sc->sc_if.if_unit); error = ENOBUFS; } splimp(); sc->sc_flags &= ~SC_DECOMP_RUN; splx(s); } return (error); } if (sc->sc_flags & SC_DEBUG) printf("ppp%d: no compressor for [%x %x %x], %x\n", sc->sc_if.if_unit, 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 {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -