⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 if_ppp.c

📁 unix and linux net driver
💻 C
📖 第 1 页 / 共 3 页
字号:
/* * 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). * * Rewritten for NextStep's funky kernel functions, I/O threads, * and netbufs (instead of real mbufs).  Also, ifnets don't install * into the kernel under NS as they do under BSD.  We have tried to * make the code remain as similar to the NetBSD version without * incurring too much hassle.  This code is the merge of  * Philip Prindeville's <philipp@res.enst.fr>/Pete French's <pete@ohm.york.ac.uk> * and Stephen Perkins'  <perkins@cps.msu.edu> independent ports. * *//* from if_sl.c,v 1.11 84/10/04 12:54:47 rick Exp */#if !defined(lint)static char sccsid[] = "$Revision: 1.12 $ ($Date: 1999/12/23 01:48:44 $)";#endif /* not lint*/#define KERNEL 1#define KERNEL_FEATURES 1#define INET 1#if NS_TARGET >= 40#if NS_TARGET >= 41#include <kernserv/clock_timer.h>#include <kernserv/lock.h>#else#include <kern/lock.h>#endif /* NS_TARGET */#endif /* NS_TARGET */#include <sys/param.h>#if NS_TARGET >= 41typedef simple_lock_data_t lock_data_t;		/* XXX */#endif /* NS_TARGET */#include <sys/proc.h>#include "netbuf.h"#include <sys/socket.h>#include <sys/conf.h>#include <sys/time.h>#include <sys/ioctl.h>#include <sys/errno.h>#if !(NS_TARGET >= 40)#include <kernserv/prototypes.h>#endif#if defined(m68k)#import "spl.h"#else#include <driverkit/generalFuncs.h>#import <kernserv/machine/spl.h>#endif#if defined(sparc) || defined(m68k)#include <machine/psl.h>#endif#include <kernserv/kern_server_types.h>#include <net/if.h>#include <net/route.h>#if INET#include <netinet/in.h>#include <netinet/in_systm.h>#include <netinet/in_var.h>#include <netinet/ip.h>#include <netinet/tcp.h>#endif#include <net/ppp_defs.h>#ifdef	VJC#include <net/vjcompress.h>#endif#include <net/if_ppp.h>#include "NeXT_Version.h"#include "if_pppvar.h"struct	ppp_softc ppp_softc[NUM_PPP];#ifdef	PPP_COMPRESS#define	PACKETPTR	NETBUF_T#include <net/ppp-comp.h>#endif/* * The max number of NETBUF_Ts we wish to compress and cache for * sending. */#define COMPRESS_CACHE_LEN 1#include "inlines.h"/* * Necessary to avoid redefinition warnings or bogus type casts later. */int	pppoutput __P((netif_t ifp, netbuf_t m, void *arg));int	pppsioctl __P((netif_t ifp, int cmd, caddr_t data));int	pppcontrol __P((netif_t ifp, const char *cmd, void *data));void	pppintr_comp __P((void *arg));void	pppintr_decomp __P((void *arg));void	pppfillfreeq __P((void *arg));void	pppgetm __P((register struct ppp_softc *sc));static void	ppp_requeue __P((struct ppp_softc *));static void	ppp_outpkt __P((struct ppp_softc *));static void	ppp_ccp __P((struct ppp_softc *, NETBUF_T, int rcvd));static void	ppp_ccp_closed __P((struct ppp_softc *));static void	ppp_inproc __P((struct ppp_softc *, NETBUF_T));static void	pppdumpm __P((NETBUF_T));extern int	install_ppp_ld __P((void));extern int	tty_ld_remove __P((int));/* * 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 *//* * 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,};enum { QFREE, QRAW, QFAST, QSLOW, QIN, QNP, QCACHE };static struct qparms qparms[] = {	{20, 40, 50, "free"},		/* freeq */	{5, 20, 25, "raw"},		/* rawq */	{5, 20, 25, "fast"},		/* fastq */	{5, 20, 25, "slow"},		/* slowq */	{5, 20, 25, "in"},		/* inq */	{5, 20, 25, "np"},		/* npq */	{0, COMPRESS_CACHE_LEN, COMPRESS_CACHE_LEN, "cache"}	/* cache */};#define INTERACTIVE(p)	(interactive_ports[(p) & 7] == (p))#ifndef	IPTOS_LOWDELAY#define	IPTOS_LOWDELAY	0x10#endif#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;struct compressor *ppp_compressors[8] = {#if DO_BSD_COMPRESS    &ppp_bsd_compress,#endif    NULL};#endif /* PPP_COMPRESS *//* yeah, we sometimes have to change the MTU after having created the * device.  Let's hope this doesn't break anything!!! */#define	if_mtu_set(ifn,mtu)  (((struct ifnet *)ifn)->if_mtu = mtu)extern int ipforwarding;extern int ipsendredirects;kern_server_t instance;/* * Sigh.  Should be defined in <net/if.h> but isn't... */union ifr_ifru {	short	ifru_flags;	short   ifru_mtu;	u_long  ifru_asyncmap;	int     ifru_metric;	caddr_t ifru_data;};/* * Returns a new "outgoing" netbuf. *   * Must return an actual netbuf_t since other protocols * use this to get our buffers.  Before releasing, save * any space we may need when the buffer returns. */netbuf_tpppgetbuf(netif_t ifp){    NETBUF_T nb;    int len = MAX(if_mtu(ifp), PPP_MTU) + PPP_HDRLEN + PPP_FCSLEN;    nb = NB_ALLOC(len);    if (nb != NULL)      {	NB_SHRINK_TOP(nb, PPP_HDRLEN);	NB_SHRINK_BOT(nb, PPP_FCSLEN);          /* grown by pppstart() */      }    return NB_TO_nb(nb);}/* * Called from boot code to establish ppp interfaces. */voidpppattach(){    register struct ppp_softc *sc;    register int i = 0;        IOLog("\nPPP version 2.3.11-%s for NeXTSTEP and OPENSTEP\n", PPPVERSION);    IOLog("by  Stephen Perkins, Philip Prindeville, and Pete French\n");    if (install_ppp_ld() < 0) {	IOLog("ppp: Could not install line discipline\n");    }        for (sc = ppp_softc; i < NUM_PPP; sc++, i++) {	sc->sc_if = if_attach(NULL, NULL, pppoutput, 			      pppgetbuf, pppcontrol, "ppp", i, "Serial line PPP", 			      PPP_MTU, IFF_POINTOPOINT, NETIFCLASS_VIRTUAL, (void *) sc);	nbq_init(&sc->sc_freeq, &qparms[QFREE]);	nbq_init(&sc->sc_rawq, &qparms[QRAW]);	nbq_init(&sc->sc_fastq, &qparms[QFAST]);	nbq_init(&sc->sc_slowq, &qparms[QSLOW]);	nbq_init(&sc->sc_inq, &qparms[QIN]);	nbq_init(&sc->sc_npq, &qparms[QNP]);	nbq_init(&sc->sc_compq, &qparms[QCACHE]);	IOLog("     ppp%d successfully attached.\n", i);    }    ipforwarding = 1;    ipsendredirects = 1;    IOLog("PPP Successfully Installed.\n\n");}intpppdetach(){    struct ppp_softc *sc;    int i;    IOLog("Removing PPP on Line Discipline %d\n", PPPDISC);    if (!tty_ld_remove(PPPDISC))	IOLog("ppp: Could not remove line discipline\n");    IOLog("Removing interfaces:\n");    for (sc = ppp_softc, i = 0; i < NUM_PPP; sc++, i++) {	nbq_free(&sc->sc_freeq);	nbq_free(&sc->sc_rawq);	nbq_free(&sc->sc_fastq);	nbq_free(&sc->sc_slowq);	nbq_free(&sc->sc_inq);	nbq_free(&sc->sc_npq);	nbq_free(&sc->sc_compq);	if_detach(sc->sc_if);	/* no idea why we need this, but... */	bzero(sc->sc_if, sizeof(netif_t));	IOLog("     ppp%d successfully detached.\n", i);    }    IOLog("PPP-2.3 Successfully Removed.\n\n");    return 0;}/* * Allocate a ppp interface unit and initialize it. */struct ppp_softc *pppalloc(pid)    pid_t pid;{    int nppp, i;    struct ppp_softc *sc;#if NS_TARGET >= 40    struct timeval tv_time;#endif /* NS_TARGET */    for (nppp = 0, sc = ppp_softc; nppp < NUM_PPP; nppp++, sc++)	if (sc->sc_xfer == pid) {	    IOLogDbg("ppp%d: alloc'ing unit %d to proc %d\n", nppp, nppp, pid);	    sc->sc_xfer = 0;	    return sc;	}    for (nppp = 0, sc = ppp_softc; nppp < NUM_PPP; nppp++, sc++)	if (sc->sc_devp == NULL)	    break;    if (nppp >= NUM_PPP)	return NULL;    sc->sc_flags = 0;    sc->sc_mru = PPP_MRU;    sc->sc_relinq = NULL;#ifdef VJC    vj_compress_init(&sc->sc_comp, -1);#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;    /* XXX - I'm not sure why the npqueue was zapped here... */#if NS_TARGET >= 40    ns_time_to_timeval(clock_value(System), &tv_time);    sc->sc_last_sent = sc->sc_last_recv = tv_time.tv_sec;#else    sc->sc_last_sent = sc->sc_last_recv = time.tv_sec;#endif    sc->sc_compsched = 0;    sc->sc_decompsched = 0;    /*     * XXX -- We need to get packets here, and we don't care if we do block...     * We do this after we set the sc_mru.     */    pppfillfreeq((void *) sc);    return sc;}/* * Deallocate a ppp unit.  Must be called at splnet or higher. */voidpppdealloc(sc)    struct ppp_softc *sc;{    if_flags_set(sc->sc_if, if_flags(sc->sc_if) & ~(IFF_UP|IFF_RUNNING));    sc->sc_devp = NULL;    sc->sc_xfer = 0;    nbq_flush(&sc->sc_freeq);    nbq_flush(&sc->sc_rawq);    nbq_flush(&sc->sc_inq);    nbq_flush(&sc->sc_fastq);    nbq_flush(&sc->sc_slowq);    nbq_flush(&sc->sc_npq);    nbq_flush(&sc->sc_compq);#ifdef PPP_COMPRESS    ppp_ccp_closed(sc);    sc->sc_xc_state = NULL;    sc->sc_rc_state = NULL;#endif /* PPP_COMPRESS */}/* * Ioctl routine for generic ppp devices. */intpppioctl(sc, cmd, data, flag)    struct ppp_softc *sc;    void *data;    u_long cmd;    int flag;{    struct proc *p = curproc;    int s, error, flags, mru, nb, npx, oldflags;    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    NETBUF_T m;#ifdef	HAS_BROKEN_TIOCSPGRP    struct tty *tp = sc->sc_devp;#endif#if NS_TARGET >= 40	struct timeval tv_time;#endif /* NS_TARGET */    switch (cmd) {    case FIONREAD:	s = splimp();		/* paranoid; splnet probably ok */	if ((m = nbq_peek(&sc->sc_inq)) != NULL)	    *(int *)data = NB_SIZE(m);	else	    *(int *)data = 0;	splx(s);	break;    case PPPIOCGUNIT:	*(int *)data = if_unit(sc->sc_if);	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();	oldflags = sc->sc_flags;	sc->sc_flags = (sc->sc_flags & ~SC_MASK) | flags;	splx(s);	break;    case PPPIOCSMRU:	if (! suser())	    return EPERM;	mru = *(int *)data;	IOLogDbg("ppp%d: setting mru %d\n", if_unit(sc->sc_if), mru);	if (mru >= PPP_MRU && mru <= PPP_MAXMRU) {	    /* To make sure we handle the received packet	     * correctly, we do two things.  First, we	     * empty out the free_q.  We then remove	     * the current input buffer, set the input length	     * to zero, and set the flush flag.	     */	    s = splimp();	    nbq_flush(&sc->sc_freeq);	/* get rid of old buffers */	    sc->sc_mru = mru;	    if (sc->sc_m){	      NB_FREE(sc->sc_m);	      sc->sc_m = NULL;	      if (sc->sc_ilen != 0)		sc->sc_flags |= SC_FLUSH;	    }	    sc->sc_ilen = 0;	    splx(s);    	    pppfillfreeq((void *) sc);	/* and make a queue of new ones */	    pppgetm(sc);	}	break;    case PPPIOCGMRU:	*(int *)data = sc->sc_mru;	break;#ifdef	VJC    case PPPIOCSMAXCID:	if (! suser())	    return EPERM;	s = splnet();	vj_compress_init(&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))	    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;		s = splnet();		if (odp->transmit) {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -