Home | History | Annotate | Line # | Download | only in net
if_spppsubr.c revision 1.1
      1  1.1  explorer /*
      2  1.1  explorer  * Synchronous PPP/Cisco link level subroutines.
      3  1.1  explorer  * Keepalive protocol implemented in both Cisco and PPP modes.
      4  1.1  explorer  *
      5  1.1  explorer  * Copyright (C) 1994 Cronyx Ltd.
      6  1.1  explorer  * Author: Serge Vakulenko, <vak (at) zebub.msk.su>
      7  1.1  explorer  *
      8  1.1  explorer  * This software is distributed with NO WARRANTIES, not even the implied
      9  1.1  explorer  * warranties for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
     10  1.1  explorer  *
     11  1.1  explorer  * Authors grant any other persons or organisations permission to use
     12  1.1  explorer  * or modify this software as long as this message is kept with the software,
     13  1.1  explorer  * all derivative works or modified versions.
     14  1.1  explorer  *
     15  1.1  explorer  * Version 1.9, Wed Oct  4 18:58:15 MSK 1995
     16  1.1  explorer  *
     17  1.1  explorer  * $Id: if_spppsubr.c,v 1.1 1999/03/25 03:38:00 explorer Exp $
     18  1.1  explorer  */
     19  1.1  explorer #undef DEBUG
     20  1.1  explorer 
     21  1.1  explorer #include "opt_inet.h"
     22  1.1  explorer #include "opt_ns.h"
     23  1.1  explorer #include "opt_iso.h"
     24  1.1  explorer #include "opt_ipx.h"
     25  1.1  explorer 
     26  1.1  explorer #include <sys/param.h>
     27  1.1  explorer #include <sys/systm.h>
     28  1.1  explorer #include <sys/kernel.h>
     29  1.1  explorer #include <sys/ioctl.h>
     30  1.1  explorer #include <sys/socket.h>
     31  1.1  explorer #include <sys/mbuf.h>
     32  1.1  explorer 
     33  1.1  explorer #include <net/if.h>
     34  1.1  explorer #include <net/netisr.h>
     35  1.1  explorer #include <net/if_types.h>
     36  1.1  explorer 
     37  1.1  explorer #ifdef INET
     38  1.1  explorer #include <netinet/in.h>
     39  1.1  explorer #include <netinet/in_systm.h>
     40  1.1  explorer #include <netinet/in_var.h>
     41  1.1  explorer #include <netinet/ip.h>
     42  1.1  explorer #include <netinet/tcp.h>
     43  1.1  explorer #endif
     44  1.1  explorer 
     45  1.1  explorer #if defined(__NetBSD__)
     46  1.1  explorer #include <net/if_ether.h>
     47  1.1  explorer #elif defined(__FreeBSD__)
     48  1.1  explorer #include <netinet/if_ether.h>
     49  1.1  explorer #endif
     50  1.1  explorer 
     51  1.1  explorer #ifdef IPX
     52  1.1  explorer #include <netipx/ipx.h>
     53  1.1  explorer #include <netipx/ipx_if.h>
     54  1.1  explorer #endif
     55  1.1  explorer 
     56  1.1  explorer #ifdef NS
     57  1.1  explorer #include <netns/ns.h>
     58  1.1  explorer #include <netns/ns_if.h>
     59  1.1  explorer #endif
     60  1.1  explorer 
     61  1.1  explorer #ifdef ISO
     62  1.1  explorer #include <netiso/argo_debug.h>
     63  1.1  explorer #include <netiso/iso.h>
     64  1.1  explorer #include <netiso/iso_var.h>
     65  1.1  explorer #include <netiso/iso_snpac.h>
     66  1.1  explorer #endif
     67  1.1  explorer 
     68  1.1  explorer #include <net/if_sppp.h>
     69  1.1  explorer 
     70  1.1  explorer #ifdef DEBUG
     71  1.1  explorer #define print(s)        printf s
     72  1.1  explorer #else
     73  1.1  explorer #define print(s)        {/*void*/}
     74  1.1  explorer #endif
     75  1.1  explorer 
     76  1.1  explorer #if defined(__NetBSD__)
     77  1.1  explorer #define SPPP_PRINTF_FMT		"%s"
     78  1.1  explorer #define SPPP_PRINTF_ARG(x)	(x->if_xname)
     79  1.1  explorer #elif defined(__FreeBSD__)
     80  1.1  explorer #define SPPP_PRINTF_FMT		"%s%d"
     81  1.1  explorer #define SPPP_PRINTF_ARG(x)	(x->if_name), (x->if_unit)
     82  1.1  explorer #endif
     83  1.1  explorer 
     84  1.1  explorer #define MAXALIVECNT     3               /* max. alive packets */
     85  1.1  explorer 
     86  1.1  explorer #define PPP_ALLSTATIONS 0xff            /* All-Stations broadcast address */
     87  1.1  explorer #define PPP_UI          0x03            /* Unnumbered Information */
     88  1.1  explorer #define PPP_IP          0x0021          /* Internet Protocol */
     89  1.1  explorer #define PPP_ISO         0x0023          /* ISO OSI Protocol */
     90  1.1  explorer #define PPP_XNS         0x0025          /* Xerox NS Protocol */
     91  1.1  explorer #define PPP_IPX         0x002b          /* Novell IPX Protocol */
     92  1.1  explorer #define PPP_LCP         0xc021          /* Link Control Protocol */
     93  1.1  explorer #define PPP_IPCP        0x8021          /* Internet Protocol Control Protocol */
     94  1.1  explorer 
     95  1.1  explorer #define LCP_CONF_REQ    1               /* PPP LCP configure request */
     96  1.1  explorer #define LCP_CONF_ACK    2               /* PPP LCP configure acknowledge */
     97  1.1  explorer #define LCP_CONF_NAK    3               /* PPP LCP configure negative ack */
     98  1.1  explorer #define LCP_CONF_REJ    4               /* PPP LCP configure reject */
     99  1.1  explorer #define LCP_TERM_REQ    5               /* PPP LCP terminate request */
    100  1.1  explorer #define LCP_TERM_ACK    6               /* PPP LCP terminate acknowledge */
    101  1.1  explorer #define LCP_CODE_REJ    7               /* PPP LCP code reject */
    102  1.1  explorer #define LCP_PROTO_REJ   8               /* PPP LCP protocol reject */
    103  1.1  explorer #define LCP_ECHO_REQ    9               /* PPP LCP echo request */
    104  1.1  explorer #define LCP_ECHO_REPLY  10              /* PPP LCP echo reply */
    105  1.1  explorer #define LCP_DISC_REQ    11              /* PPP LCP discard request */
    106  1.1  explorer 
    107  1.1  explorer #define LCP_OPT_MRU             1       /* maximum receive unit */
    108  1.1  explorer #define LCP_OPT_ASYNC_MAP       2       /* async control character map */
    109  1.1  explorer #define LCP_OPT_AUTH_PROTO      3       /* authentication protocol */
    110  1.1  explorer #define LCP_OPT_QUAL_PROTO      4       /* quality protocol */
    111  1.1  explorer #define LCP_OPT_MAGIC           5       /* magic number */
    112  1.1  explorer #define LCP_OPT_RESERVED        6       /* reserved */
    113  1.1  explorer #define LCP_OPT_PROTO_COMP      7       /* protocol field compression */
    114  1.1  explorer #define LCP_OPT_ADDR_COMP       8       /* address/control field compression */
    115  1.1  explorer 
    116  1.1  explorer #define IPCP_CONF_REQ   LCP_CONF_REQ    /* PPP IPCP configure request */
    117  1.1  explorer #define IPCP_CONF_ACK   LCP_CONF_ACK    /* PPP IPCP configure acknowledge */
    118  1.1  explorer #define IPCP_CONF_NAK   LCP_CONF_NAK    /* PPP IPCP configure negative ack */
    119  1.1  explorer #define IPCP_CONF_REJ   LCP_CONF_REJ    /* PPP IPCP configure reject */
    120  1.1  explorer #define IPCP_TERM_REQ   LCP_TERM_REQ    /* PPP IPCP terminate request */
    121  1.1  explorer #define IPCP_TERM_ACK   LCP_TERM_ACK    /* PPP IPCP terminate acknowledge */
    122  1.1  explorer #define IPCP_CODE_REJ   LCP_CODE_REJ    /* PPP IPCP code reject */
    123  1.1  explorer 
    124  1.1  explorer #define CISCO_MULTICAST         0x8f    /* Cisco multicast address */
    125  1.1  explorer #define CISCO_UNICAST           0x0f    /* Cisco unicast address */
    126  1.1  explorer #define CISCO_KEEPALIVE         0x8035  /* Cisco keepalive protocol */
    127  1.1  explorer #define CISCO_ADDR_REQ          0       /* Cisco address request */
    128  1.1  explorer #define CISCO_ADDR_REPLY        1       /* Cisco address reply */
    129  1.1  explorer #define CISCO_KEEPALIVE_REQ     2       /* Cisco keepalive request */
    130  1.1  explorer 
    131  1.1  explorer struct ppp_header {
    132  1.1  explorer 	u_int8_t address;
    133  1.1  explorer 	u_int8_t control;
    134  1.1  explorer 	u_int16_t protocol;
    135  1.1  explorer };
    136  1.1  explorer #define PPP_HEADER_LEN          sizeof (struct ppp_header)
    137  1.1  explorer 
    138  1.1  explorer struct lcp_header {
    139  1.1  explorer 	u_int8_t type;
    140  1.1  explorer 	u_int8_t ident;
    141  1.1  explorer 	u_int16_t len;
    142  1.1  explorer };
    143  1.1  explorer #define LCP_HEADER_LEN          sizeof (struct lcp_header)
    144  1.1  explorer 
    145  1.1  explorer struct cisco_packet {
    146  1.1  explorer 	u_int32_t type;
    147  1.1  explorer 	u_int32_t par1;
    148  1.1  explorer 	u_int32_t par2;
    149  1.1  explorer 	u_int16_t rel;
    150  1.1  explorer 	u_int16_t time0;
    151  1.1  explorer 	u_int16_t time1;
    152  1.1  explorer };
    153  1.1  explorer #define CISCO_PACKET_LEN 18
    154  1.1  explorer 
    155  1.1  explorer static struct sppp *spppq;
    156  1.1  explorer 
    157  1.1  explorer /*
    158  1.1  explorer  * The following disgusting hack gets around the problem that IP TOS
    159  1.1  explorer  * can't be set yet.  We want to put "interactive" traffic on a high
    160  1.1  explorer  * priority queue.  To decide if traffic is interactive, we check that
    161  1.1  explorer  * a) it is TCP and b) one of its ports is telnet, rlogin or ftp control.
    162  1.1  explorer  */
    163  1.1  explorer static u_int16_t interactive_ports[8] = {
    164  1.1  explorer 	0,	513,	0,	0,
    165  1.1  explorer 	0,	21,	0,	23,
    166  1.1  explorer };
    167  1.1  explorer #define INTERACTIVE(p) (interactive_ports[(p) & 7] == (p))
    168  1.1  explorer 
    169  1.1  explorer /*
    170  1.1  explorer  * Timeout routine activation macros.
    171  1.1  explorer  */
    172  1.1  explorer #define TIMO(p,s) if (! ((p)->pp_flags & PP_TIMO)) { \
    173  1.1  explorer 			timeout (sppp_cp_timeout, (void*) (p), (s)*hz); \
    174  1.1  explorer 			(p)->pp_flags |= PP_TIMO; }
    175  1.1  explorer #define UNTIMO(p) if ((p)->pp_flags & PP_TIMO) { \
    176  1.1  explorer 			untimeout (sppp_cp_timeout, (void*) (p)); \
    177  1.1  explorer 			(p)->pp_flags &= ~PP_TIMO; }
    178  1.1  explorer 
    179  1.1  explorer static void sppp_keepalive (void *dummy);
    180  1.1  explorer static void sppp_cp_send (struct sppp *sp, u_int16_t proto, u_int8_t type,
    181  1.1  explorer 	u_int8_t ident, u_int16_t len, void *data);
    182  1.1  explorer static void sppp_cisco_send (struct sppp *sp, int type, int32_t par1, int32_t par2);
    183  1.1  explorer static void sppp_lcp_input (struct sppp *sp, struct mbuf *m);
    184  1.1  explorer static void sppp_cisco_input (struct sppp *sp, struct mbuf *m);
    185  1.1  explorer static void sppp_ipcp_input (struct sppp *sp, struct mbuf *m);
    186  1.1  explorer static void sppp_lcp_open (struct sppp *sp);
    187  1.1  explorer static void sppp_ipcp_open (struct sppp *sp);
    188  1.1  explorer static int sppp_lcp_conf_parse_options (struct sppp *sp, struct lcp_header *h,
    189  1.1  explorer 	int len, u_int32_t *magic);
    190  1.1  explorer static void sppp_cp_timeout (void *arg);
    191  1.1  explorer static char *sppp_lcp_type_name (u_int8_t type);
    192  1.1  explorer static char *sppp_ipcp_type_name (u_int8_t type);
    193  1.1  explorer static void sppp_print_bytes (u_int8_t *p, u_int16_t len);
    194  1.1  explorer static int sppp_output (struct ifnet *ifp, struct mbuf *m,
    195  1.1  explorer 	struct sockaddr *dst, struct rtentry *rt);
    196  1.1  explorer 
    197  1.1  explorer #if defined(__NetBSD__)
    198  1.1  explorer void
    199  1.1  explorer spppattach()
    200  1.1  explorer {
    201  1.1  explorer }
    202  1.1  explorer #endif
    203  1.1  explorer 
    204  1.1  explorer /*
    205  1.1  explorer  * Flush interface queue.
    206  1.1  explorer  */
    207  1.1  explorer static void qflush (struct ifqueue *ifq)
    208  1.1  explorer {
    209  1.1  explorer 	struct mbuf *m, *n;
    210  1.1  explorer 
    211  1.1  explorer 	n = ifq->ifq_head;
    212  1.1  explorer 	while ((m = n)) {
    213  1.1  explorer 		n = m->m_act;
    214  1.1  explorer 		m_freem (m);
    215  1.1  explorer 	}
    216  1.1  explorer 	ifq->ifq_head = 0;
    217  1.1  explorer 	ifq->ifq_tail = 0;
    218  1.1  explorer 	ifq->ifq_len = 0;
    219  1.1  explorer }
    220  1.1  explorer 
    221  1.1  explorer /*
    222  1.1  explorer  * Process the received packet.
    223  1.1  explorer  */
    224  1.1  explorer void sppp_input (struct ifnet *ifp, struct mbuf *m)
    225  1.1  explorer {
    226  1.1  explorer 	struct ppp_header *h;
    227  1.1  explorer 	struct sppp *sp = (struct sppp*) ifp;
    228  1.1  explorer 	struct ifqueue *inq = 0;
    229  1.1  explorer 	int s;
    230  1.1  explorer 
    231  1.1  explorer 	if (ifp->if_flags & IFF_UP)
    232  1.1  explorer 		/* Count received bytes, add FCS and one flag */
    233  1.1  explorer 		ifp->if_ibytes += m->m_pkthdr.len + 3;
    234  1.1  explorer 
    235  1.1  explorer 	if (m->m_pkthdr.len <= PPP_HEADER_LEN) {
    236  1.1  explorer 		/* Too small packet, drop it. */
    237  1.1  explorer 		if (ifp->if_flags & IFF_DEBUG)
    238  1.1  explorer 			printf (SPPP_PRINTF_FMT
    239  1.1  explorer 				": input packet is too small, %d bytes\n",
    240  1.1  explorer 				SPPP_PRINTF_ARG(ifp), m->m_pkthdr.len);
    241  1.1  explorer drop:           ++ifp->if_iqdrops;
    242  1.1  explorer 		m_freem (m);
    243  1.1  explorer 		return;
    244  1.1  explorer 	}
    245  1.1  explorer 
    246  1.1  explorer 	/* Get PPP header. */
    247  1.1  explorer 	h = mtod (m, struct ppp_header*);
    248  1.1  explorer 	m_adj (m, PPP_HEADER_LEN);
    249  1.1  explorer 
    250  1.1  explorer 	switch (h->address) {
    251  1.1  explorer 	default:        /* Invalid PPP packet. */
    252  1.1  explorer invalid:        if (ifp->if_flags & IFF_DEBUG)
    253  1.1  explorer 			printf (SPPP_PRINTF_FMT
    254  1.1  explorer 				": invalid input packet <0x%x 0x%x 0x%x>\n",
    255  1.1  explorer 				SPPP_PRINTF_ARG(ifp),
    256  1.1  explorer 				h->address, h->control, ntohs (h->protocol));
    257  1.1  explorer 		goto drop;
    258  1.1  explorer 	case PPP_ALLSTATIONS:
    259  1.1  explorer 		if (h->control != PPP_UI)
    260  1.1  explorer 			goto invalid;
    261  1.1  explorer 		if (sp->pp_flags & PP_CISCO) {
    262  1.1  explorer 			if (ifp->if_flags & IFF_DEBUG)
    263  1.1  explorer 				printf (SPPP_PRINTF_FMT ": PPP packet in Cisco mode <0x%x 0x%x 0x%x>\n",
    264  1.1  explorer 					SPPP_PRINTF_ARG(ifp),
    265  1.1  explorer 					h->address, h->control, ntohs (h->protocol));
    266  1.1  explorer 			goto drop;
    267  1.1  explorer 		}
    268  1.1  explorer 		switch (ntohs (h->protocol)) {
    269  1.1  explorer 		default:
    270  1.1  explorer 			if (sp->lcp.state == LCP_STATE_OPENED)
    271  1.1  explorer 				sppp_cp_send (sp, PPP_LCP, LCP_PROTO_REJ,
    272  1.1  explorer 					++sp->pp_seq, m->m_pkthdr.len + 2,
    273  1.1  explorer 					&h->protocol);
    274  1.1  explorer 			if (ifp->if_flags & IFF_DEBUG)
    275  1.1  explorer 				printf (SPPP_PRINTF_FMT ": invalid input protocol <0x%x 0x%x 0x%x>\n",
    276  1.1  explorer 					SPPP_PRINTF_ARG(ifp),
    277  1.1  explorer 					h->address, h->control, ntohs (h->protocol));
    278  1.1  explorer 			++ifp->if_noproto;
    279  1.1  explorer 			goto drop;
    280  1.1  explorer 		case PPP_LCP:
    281  1.1  explorer 			sppp_lcp_input ((struct sppp*) ifp, m);
    282  1.1  explorer 			m_freem (m);
    283  1.1  explorer 			return;
    284  1.1  explorer #ifdef INET
    285  1.1  explorer 		case PPP_IPCP:
    286  1.1  explorer 			if (sp->lcp.state == LCP_STATE_OPENED)
    287  1.1  explorer 				sppp_ipcp_input ((struct sppp*) ifp, m);
    288  1.1  explorer 			m_freem (m);
    289  1.1  explorer 			return;
    290  1.1  explorer 		case PPP_IP:
    291  1.1  explorer 			if (sp->ipcp.state == IPCP_STATE_OPENED) {
    292  1.1  explorer 				schednetisr (NETISR_IP);
    293  1.1  explorer 				inq = &ipintrq;
    294  1.1  explorer 			}
    295  1.1  explorer 			break;
    296  1.1  explorer #endif
    297  1.1  explorer #ifdef IPX
    298  1.1  explorer 		case PPP_IPX:
    299  1.1  explorer 			/* IPX IPXCP not implemented yet */
    300  1.1  explorer 			if (sp->lcp.state == LCP_STATE_OPENED) {
    301  1.1  explorer 				schednetisr (NETISR_IPX);
    302  1.1  explorer 				inq = &ipxintrq;
    303  1.1  explorer 			}
    304  1.1  explorer 			break;
    305  1.1  explorer #endif
    306  1.1  explorer #ifdef NS
    307  1.1  explorer 		case PPP_XNS:
    308  1.1  explorer 			/* XNS IDPCP not implemented yet */
    309  1.1  explorer 			if (sp->lcp.state == LCP_STATE_OPENED) {
    310  1.1  explorer 				schednetisr (NETISR_NS);
    311  1.1  explorer 				inq = &nsintrq;
    312  1.1  explorer 			}
    313  1.1  explorer 			break;
    314  1.1  explorer #endif
    315  1.1  explorer #ifdef ISO
    316  1.1  explorer 		case PPP_ISO:
    317  1.1  explorer 			/* OSI NLCP not implemented yet */
    318  1.1  explorer 			if (sp->lcp.state == LCP_STATE_OPENED) {
    319  1.1  explorer 				schednetisr (NETISR_ISO);
    320  1.1  explorer 				inq = &clnlintrq;
    321  1.1  explorer 			}
    322  1.1  explorer 			break;
    323  1.1  explorer #endif
    324  1.1  explorer 		}
    325  1.1  explorer 		break;
    326  1.1  explorer 	case CISCO_MULTICAST:
    327  1.1  explorer 	case CISCO_UNICAST:
    328  1.1  explorer 		/* Don't check the control field here (RFC 1547). */
    329  1.1  explorer 		if (! (sp->pp_flags & PP_CISCO)) {
    330  1.1  explorer 			if (ifp->if_flags & IFF_DEBUG)
    331  1.1  explorer 				printf (SPPP_PRINTF_FMT ": Cisco packet in PPP mode <0x%x 0x%x 0x%x>\n",
    332  1.1  explorer 					SPPP_PRINTF_ARG(ifp),
    333  1.1  explorer 					h->address, h->control, ntohs (h->protocol));
    334  1.1  explorer 			goto drop;
    335  1.1  explorer 		}
    336  1.1  explorer 		switch (ntohs (h->protocol)) {
    337  1.1  explorer 		default:
    338  1.1  explorer 			++ifp->if_noproto;
    339  1.1  explorer 			goto invalid;
    340  1.1  explorer 		case CISCO_KEEPALIVE:
    341  1.1  explorer 			sppp_cisco_input ((struct sppp*) ifp, m);
    342  1.1  explorer 			m_freem (m);
    343  1.1  explorer 			return;
    344  1.1  explorer #ifdef INET
    345  1.1  explorer 		case ETHERTYPE_IP:
    346  1.1  explorer 			schednetisr (NETISR_IP);
    347  1.1  explorer 			inq = &ipintrq;
    348  1.1  explorer 			break;
    349  1.1  explorer #endif
    350  1.1  explorer #ifdef IPX
    351  1.1  explorer 		case ETHERTYPE_IPX:
    352  1.1  explorer 			schednetisr (NETISR_IPX);
    353  1.1  explorer 			inq = &ipxintrq;
    354  1.1  explorer 			break;
    355  1.1  explorer #endif
    356  1.1  explorer #ifdef NS
    357  1.1  explorer 		case ETHERTYPE_NS:
    358  1.1  explorer 			schednetisr (NETISR_NS);
    359  1.1  explorer 			inq = &nsintrq;
    360  1.1  explorer 			break;
    361  1.1  explorer #endif
    362  1.1  explorer 		}
    363  1.1  explorer 		break;
    364  1.1  explorer 	}
    365  1.1  explorer 
    366  1.1  explorer 	if (! (ifp->if_flags & IFF_UP) || ! inq)
    367  1.1  explorer 		goto drop;
    368  1.1  explorer 
    369  1.1  explorer 	/* Check queue. */
    370  1.1  explorer 	s = splimp ();
    371  1.1  explorer 	if (IF_QFULL (inq)) {
    372  1.1  explorer 		/* Queue overflow. */
    373  1.1  explorer 		IF_DROP (inq);
    374  1.1  explorer 		splx (s);
    375  1.1  explorer 		if (ifp->if_flags & IFF_DEBUG)
    376  1.1  explorer 			printf (SPPP_PRINTF_FMT ": protocol queue overflow\n",
    377  1.1  explorer 				SPPP_PRINTF_ARG(ifp));
    378  1.1  explorer 		goto drop;
    379  1.1  explorer 	}
    380  1.1  explorer 	IF_ENQUEUE (inq, m);
    381  1.1  explorer 	splx (s);
    382  1.1  explorer }
    383  1.1  explorer 
    384  1.1  explorer /*
    385  1.1  explorer  * Enqueue transmit packet.
    386  1.1  explorer  */
    387  1.1  explorer static int
    388  1.1  explorer sppp_output (struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst, struct rtentry *rt)
    389  1.1  explorer {
    390  1.1  explorer 	struct sppp *sp = (struct sppp*) ifp;
    391  1.1  explorer 	struct ppp_header *h;
    392  1.1  explorer 	struct ifqueue *ifq;
    393  1.1  explorer 	int s = splimp ();
    394  1.1  explorer 
    395  1.1  explorer 	if (! (ifp->if_flags & IFF_UP) || ! (ifp->if_flags & IFF_RUNNING)) {
    396  1.1  explorer 		m_freem (m);
    397  1.1  explorer 		splx (s);
    398  1.1  explorer 		return (ENETDOWN);
    399  1.1  explorer 	}
    400  1.1  explorer 
    401  1.1  explorer 	ifq = &ifp->if_snd;
    402  1.1  explorer #ifdef INET
    403  1.1  explorer 	/*
    404  1.1  explorer 	 * Put low delay, telnet, rlogin and ftp control packets
    405  1.1  explorer 	 * in front of the queue.
    406  1.1  explorer 	 */
    407  1.1  explorer 	if (dst->sa_family == AF_INET) {
    408  1.1  explorer 		struct ip *ip = mtod (m, struct ip*);
    409  1.1  explorer 		struct tcphdr *tcp = (struct tcphdr*) ((int32_t*)ip + ip->ip_hl);
    410  1.1  explorer 
    411  1.1  explorer 		if (!IF_QFULL(&sp->pp_fastq) &&
    412  1.1  explorer 		    ((ip->ip_tos & IPTOS_LOWDELAY) ||
    413  1.1  explorer 	    	    (ip->ip_p == IPPROTO_TCP &&
    414  1.1  explorer 	    	    m->m_len >= sizeof (struct ip) + sizeof (struct tcphdr) &&
    415  1.1  explorer 	    	    (INTERACTIVE (ntohs (tcp->th_sport)) ||
    416  1.1  explorer 	    	    INTERACTIVE (ntohs (tcp->th_dport))))))
    417  1.1  explorer 			ifq = &sp->pp_fastq;
    418  1.1  explorer 	}
    419  1.1  explorer #endif
    420  1.1  explorer 
    421  1.1  explorer 	/*
    422  1.1  explorer 	 * Prepend general data packet PPP header. For now, IP only.
    423  1.1  explorer 	 */
    424  1.1  explorer 	M_PREPEND (m, PPP_HEADER_LEN, M_DONTWAIT);
    425  1.1  explorer 	if (! m) {
    426  1.1  explorer 		if (ifp->if_flags & IFF_DEBUG)
    427  1.1  explorer 			printf (SPPP_PRINTF_FMT ": no memory for transmit header\n",
    428  1.1  explorer 				SPPP_PRINTF_ARG(ifp));
    429  1.1  explorer 		splx (s);
    430  1.1  explorer 		return (ENOBUFS);
    431  1.1  explorer 	}
    432  1.1  explorer 	h = mtod (m, struct ppp_header*);
    433  1.1  explorer 	if (sp->pp_flags & PP_CISCO) {
    434  1.1  explorer 		h->address = CISCO_UNICAST;        /* unicast address */
    435  1.1  explorer 		h->control = 0;
    436  1.1  explorer 	} else {
    437  1.1  explorer 		h->address = PPP_ALLSTATIONS;        /* broadcast address */
    438  1.1  explorer 		h->control = PPP_UI;                 /* Unnumbered Info */
    439  1.1  explorer 	}
    440  1.1  explorer 
    441  1.1  explorer 	switch (dst->sa_family) {
    442  1.1  explorer #ifdef INET
    443  1.1  explorer 	case AF_INET:   /* Internet Protocol */
    444  1.1  explorer 		if (sp->pp_flags & PP_CISCO)
    445  1.1  explorer 			h->protocol = htons (ETHERTYPE_IP);
    446  1.1  explorer 		else if (sp->ipcp.state == IPCP_STATE_OPENED)
    447  1.1  explorer 			h->protocol = htons (PPP_IP);
    448  1.1  explorer 		else {
    449  1.1  explorer 			m_freem (m);
    450  1.1  explorer 			splx (s);
    451  1.1  explorer 			return (ENETDOWN);
    452  1.1  explorer 		}
    453  1.1  explorer 		break;
    454  1.1  explorer #endif
    455  1.1  explorer #ifdef NS
    456  1.1  explorer 	case AF_NS:     /* Xerox NS Protocol */
    457  1.1  explorer 		h->protocol = htons ((sp->pp_flags & PP_CISCO) ?
    458  1.1  explorer 			ETHERTYPE_NS : PPP_XNS);
    459  1.1  explorer 		break;
    460  1.1  explorer #endif
    461  1.1  explorer #ifdef IPX
    462  1.1  explorer 	case AF_IPX:     /* Novell IPX Protocol */
    463  1.1  explorer 		h->protocol = htons ((sp->pp_flags & PP_CISCO) ?
    464  1.1  explorer 			ETHERTYPE_IPX : PPP_IPX);
    465  1.1  explorer 		break;
    466  1.1  explorer #endif
    467  1.1  explorer #ifdef ISO
    468  1.1  explorer 	case AF_ISO:    /* ISO OSI Protocol */
    469  1.1  explorer 		if (sp->pp_flags & PP_CISCO)
    470  1.1  explorer 			goto nosupport;
    471  1.1  explorer 		h->protocol = htons (PPP_ISO);
    472  1.1  explorer 		break;
    473  1.1  explorer nosupport:
    474  1.1  explorer #endif
    475  1.1  explorer 	default:
    476  1.1  explorer 		m_freem (m);
    477  1.1  explorer 		splx (s);
    478  1.1  explorer 		return (EAFNOSUPPORT);
    479  1.1  explorer 	}
    480  1.1  explorer 
    481  1.1  explorer 	/*
    482  1.1  explorer 	 * Queue message on interface, and start output if interface
    483  1.1  explorer 	 * not yet active.
    484  1.1  explorer 	 */
    485  1.1  explorer 	if (IF_QFULL (ifq)) {
    486  1.1  explorer 		IF_DROP (&ifp->if_snd);
    487  1.1  explorer 		m_freem (m);
    488  1.1  explorer 		splx (s);
    489  1.1  explorer 		return (ENOBUFS);
    490  1.1  explorer 	}
    491  1.1  explorer 	IF_ENQUEUE (ifq, m);
    492  1.1  explorer 	if (! (ifp->if_flags & IFF_OACTIVE))
    493  1.1  explorer 		(*ifp->if_start) (ifp);
    494  1.1  explorer 
    495  1.1  explorer 	/*
    496  1.1  explorer 	 * Count output packets and bytes.
    497  1.1  explorer 	 * The packet length includes header, FCS and 1 flag,
    498  1.1  explorer 	 * according to RFC 1333.
    499  1.1  explorer 	 */
    500  1.1  explorer 	ifp->if_obytes += m->m_pkthdr.len + 3;
    501  1.1  explorer 	splx (s);
    502  1.1  explorer 	return (0);
    503  1.1  explorer }
    504  1.1  explorer 
    505  1.1  explorer void sppp_attach (struct ifnet *ifp)
    506  1.1  explorer {
    507  1.1  explorer 	struct sppp *sp = (struct sppp*) ifp;
    508  1.1  explorer 
    509  1.1  explorer 	/* Initialize keepalive handler. */
    510  1.1  explorer 	if (! spppq)
    511  1.1  explorer 		timeout (sppp_keepalive, 0, hz * 10);
    512  1.1  explorer 
    513  1.1  explorer 	/* Insert new entry into the keepalive list. */
    514  1.1  explorer 	sp->pp_next = spppq;
    515  1.1  explorer 	spppq = sp;
    516  1.1  explorer 
    517  1.1  explorer 	sp->pp_if.if_type = IFT_PPP;
    518  1.1  explorer 	sp->pp_if.if_output = sppp_output;
    519  1.1  explorer 	sp->pp_fastq.ifq_maxlen = 32;
    520  1.1  explorer 	sp->pp_loopcnt = 0;
    521  1.1  explorer 	sp->pp_alivecnt = 0;
    522  1.1  explorer 	sp->pp_seq = 0;
    523  1.1  explorer 	sp->pp_rseq = 0;
    524  1.1  explorer 	sp->lcp.magic = 0;
    525  1.1  explorer 	sp->lcp.state = LCP_STATE_CLOSED;
    526  1.1  explorer 	sp->ipcp.state = IPCP_STATE_CLOSED;
    527  1.1  explorer }
    528  1.1  explorer 
    529  1.1  explorer void
    530  1.1  explorer sppp_detach (struct ifnet *ifp)
    531  1.1  explorer {
    532  1.1  explorer 	struct sppp **q, *p, *sp = (struct sppp*) ifp;
    533  1.1  explorer 
    534  1.1  explorer 	/* Remove the entry from the keepalive list. */
    535  1.1  explorer 	for (q = &spppq; (p = *q); q = &p->pp_next)
    536  1.1  explorer 		if (p == sp) {
    537  1.1  explorer 			*q = p->pp_next;
    538  1.1  explorer 			break;
    539  1.1  explorer 		}
    540  1.1  explorer 
    541  1.1  explorer 	/* Stop keepalive handler. */
    542  1.1  explorer 	if (! spppq)
    543  1.1  explorer 		untimeout (sppp_keepalive, 0);
    544  1.1  explorer 	UNTIMO (sp);
    545  1.1  explorer }
    546  1.1  explorer 
    547  1.1  explorer /*
    548  1.1  explorer  * Flush the interface output queue.
    549  1.1  explorer  */
    550  1.1  explorer void sppp_flush (struct ifnet *ifp)
    551  1.1  explorer {
    552  1.1  explorer 	struct sppp *sp = (struct sppp*) ifp;
    553  1.1  explorer 
    554  1.1  explorer 	qflush (&sp->pp_if.if_snd);
    555  1.1  explorer 	qflush (&sp->pp_fastq);
    556  1.1  explorer }
    557  1.1  explorer 
    558  1.1  explorer /*
    559  1.1  explorer  * Check if the output queue is empty.
    560  1.1  explorer  */
    561  1.1  explorer int
    562  1.1  explorer sppp_isempty (struct ifnet *ifp)
    563  1.1  explorer {
    564  1.1  explorer 	struct sppp *sp = (struct sppp*) ifp;
    565  1.1  explorer 	int empty, s = splimp ();
    566  1.1  explorer 
    567  1.1  explorer 	empty = !sp->pp_fastq.ifq_head && !sp->pp_if.if_snd.ifq_head;
    568  1.1  explorer 	splx (s);
    569  1.1  explorer 	return (empty);
    570  1.1  explorer }
    571  1.1  explorer 
    572  1.1  explorer /*
    573  1.1  explorer  * Get next packet to send.
    574  1.1  explorer  */
    575  1.1  explorer struct mbuf *sppp_dequeue (struct ifnet *ifp)
    576  1.1  explorer {
    577  1.1  explorer 	struct sppp *sp = (struct sppp*) ifp;
    578  1.1  explorer 	struct mbuf *m;
    579  1.1  explorer 	int s = splimp ();
    580  1.1  explorer 
    581  1.1  explorer 	IF_DEQUEUE (&sp->pp_fastq, m);
    582  1.1  explorer 	if (! m)
    583  1.1  explorer 		IF_DEQUEUE (&sp->pp_if.if_snd, m);
    584  1.1  explorer 	splx (s);
    585  1.1  explorer 	return (m);
    586  1.1  explorer }
    587  1.1  explorer 
    588  1.1  explorer /*
    589  1.1  explorer  * Send keepalive packets, every 10 seconds.
    590  1.1  explorer  */
    591  1.1  explorer void sppp_keepalive (void *dummy)
    592  1.1  explorer {
    593  1.1  explorer 	struct sppp *sp;
    594  1.1  explorer 	int s = splimp ();
    595  1.1  explorer 
    596  1.1  explorer 	for (sp=spppq; sp; sp=sp->pp_next) {
    597  1.1  explorer 		struct ifnet *ifp = &sp->pp_if;
    598  1.1  explorer 
    599  1.1  explorer 		/* Keepalive mode disabled or channel down? */
    600  1.1  explorer 		if (! (sp->pp_flags & PP_KEEPALIVE) ||
    601  1.1  explorer 		    ! (ifp->if_flags & IFF_RUNNING))
    602  1.1  explorer 			continue;
    603  1.1  explorer 
    604  1.1  explorer 		/* No keepalive in PPP mode if LCP not opened yet. */
    605  1.1  explorer 		if (! (sp->pp_flags & PP_CISCO) &&
    606  1.1  explorer 		    sp->lcp.state != LCP_STATE_OPENED)
    607  1.1  explorer 			continue;
    608  1.1  explorer 
    609  1.1  explorer 		if (sp->pp_alivecnt == MAXALIVECNT) {
    610  1.1  explorer 			/* No keepalive packets got.  Stop the interface. */
    611  1.1  explorer 			printf (SPPP_PRINTF_FMT ": down\n", SPPP_PRINTF_ARG(ifp));
    612  1.1  explorer 			if_down (ifp);
    613  1.1  explorer 			qflush (&sp->pp_fastq);
    614  1.1  explorer 			if (! (sp->pp_flags & PP_CISCO)) {
    615  1.1  explorer 				/* Shut down the PPP link. */
    616  1.1  explorer 				sp->lcp.state = LCP_STATE_CLOSED;
    617  1.1  explorer 				sp->ipcp.state = IPCP_STATE_CLOSED;
    618  1.1  explorer 				UNTIMO (sp);
    619  1.1  explorer 				/* Initiate negotiation. */
    620  1.1  explorer 				sppp_lcp_open (sp);
    621  1.1  explorer 			}
    622  1.1  explorer 		}
    623  1.1  explorer 		if (sp->pp_alivecnt <= MAXALIVECNT)
    624  1.1  explorer 			++sp->pp_alivecnt;
    625  1.1  explorer 		if (sp->pp_flags & PP_CISCO)
    626  1.1  explorer 			sppp_cisco_send (sp, CISCO_KEEPALIVE_REQ, ++sp->pp_seq,
    627  1.1  explorer 				sp->pp_rseq);
    628  1.1  explorer 		else if (sp->lcp.state == LCP_STATE_OPENED) {
    629  1.1  explorer 			int32_t nmagic = htonl (sp->lcp.magic);
    630  1.1  explorer 			sp->lcp.echoid = ++sp->pp_seq;
    631  1.1  explorer 			sppp_cp_send (sp, PPP_LCP, LCP_ECHO_REQ,
    632  1.1  explorer 				sp->lcp.echoid, 4, &nmagic);
    633  1.1  explorer 		}
    634  1.1  explorer 	}
    635  1.1  explorer 	splx (s);
    636  1.1  explorer 	timeout (sppp_keepalive, 0, hz * 10);
    637  1.1  explorer }
    638  1.1  explorer 
    639  1.1  explorer /*
    640  1.1  explorer  * Handle incoming PPP Link Control Protocol packets.
    641  1.1  explorer  */
    642  1.1  explorer void sppp_lcp_input (struct sppp *sp, struct mbuf *m)
    643  1.1  explorer {
    644  1.1  explorer 	struct lcp_header *h;
    645  1.1  explorer 	struct ifnet *ifp = &sp->pp_if;
    646  1.1  explorer 	int len = m->m_pkthdr.len;
    647  1.1  explorer 	u_int8_t *p, opt[6];
    648  1.1  explorer 	u_int32_t rmagic;
    649  1.1  explorer 
    650  1.1  explorer 	if (len < 4) {
    651  1.1  explorer 		if (ifp->if_flags & IFF_DEBUG)
    652  1.1  explorer 			printf (SPPP_PRINTF_FMT ": invalid lcp packet length: %d bytes\n",
    653  1.1  explorer 				SPPP_PRINTF_ARG(ifp), len);
    654  1.1  explorer 		return;
    655  1.1  explorer 	}
    656  1.1  explorer 	h = mtod (m, struct lcp_header*);
    657  1.1  explorer 	if (ifp->if_flags & IFF_DEBUG) {
    658  1.1  explorer 		char state = '?';
    659  1.1  explorer 		switch (sp->lcp.state) {
    660  1.1  explorer 		case LCP_STATE_CLOSED:   state = 'C'; break;
    661  1.1  explorer 		case LCP_STATE_ACK_RCVD: state = 'R'; break;
    662  1.1  explorer 		case LCP_STATE_ACK_SENT: state = 'S'; break;
    663  1.1  explorer 		case LCP_STATE_OPENED:   state = 'O'; break;
    664  1.1  explorer 		}
    665  1.1  explorer 		printf (SPPP_PRINTF_FMT ": lcp input(%c): %d bytes <%s id=%xh len=%xh",
    666  1.1  explorer 			SPPP_PRINTF_ARG(ifp), state, len,
    667  1.1  explorer 			sppp_lcp_type_name (h->type), h->ident, ntohs (h->len));
    668  1.1  explorer 		if (len > 4)
    669  1.1  explorer 			sppp_print_bytes ((u_int8_t*) (h+1), len-4);
    670  1.1  explorer 		printf (">\n");
    671  1.1  explorer 	}
    672  1.1  explorer 	if (len > ntohs (h->len))
    673  1.1  explorer 		len = ntohs (h->len);
    674  1.1  explorer 	switch (h->type) {
    675  1.1  explorer 	default:
    676  1.1  explorer 		/* Unknown packet type -- send Code-Reject packet. */
    677  1.1  explorer 		sppp_cp_send (sp, PPP_LCP, LCP_CODE_REJ, ++sp->pp_seq,
    678  1.1  explorer 			m->m_pkthdr.len, h);
    679  1.1  explorer 		break;
    680  1.1  explorer 	case LCP_CONF_REQ:
    681  1.1  explorer 		if (len < 4) {
    682  1.1  explorer 			if (ifp->if_flags & IFF_DEBUG)
    683  1.1  explorer 				printf (SPPP_PRINTF_FMT ": invalid lcp configure request packet length: %d bytes\n",
    684  1.1  explorer 					SPPP_PRINTF_ARG(ifp), len);
    685  1.1  explorer 			break;
    686  1.1  explorer 		}
    687  1.1  explorer 		if (len>4 && !sppp_lcp_conf_parse_options (sp, h, len, &rmagic))
    688  1.1  explorer 			goto badreq;
    689  1.1  explorer 		if (rmagic == sp->lcp.magic) {
    690  1.1  explorer 			/* Local and remote magics equal -- loopback? */
    691  1.1  explorer 			if (sp->pp_loopcnt >= MAXALIVECNT*5) {
    692  1.1  explorer 				printf (SPPP_PRINTF_FMT ": loopback\n",
    693  1.1  explorer 					SPPP_PRINTF_ARG(ifp));
    694  1.1  explorer 				sp->pp_loopcnt = 0;
    695  1.1  explorer 				if (ifp->if_flags & IFF_UP) {
    696  1.1  explorer 					if_down (ifp);
    697  1.1  explorer 					qflush (&sp->pp_fastq);
    698  1.1  explorer 				}
    699  1.1  explorer 			} else if (ifp->if_flags & IFF_DEBUG)
    700  1.1  explorer 				printf (SPPP_PRINTF_FMT ": conf req: magic glitch\n",
    701  1.1  explorer 					SPPP_PRINTF_ARG(ifp));
    702  1.1  explorer 			++sp->pp_loopcnt;
    703  1.1  explorer 
    704  1.1  explorer 			/* MUST send Conf-Nack packet. */
    705  1.1  explorer 			rmagic = ~sp->lcp.magic;
    706  1.1  explorer 			opt[0] = LCP_OPT_MAGIC;
    707  1.1  explorer 			opt[1] = sizeof (opt);
    708  1.1  explorer 			opt[2] = rmagic >> 24;
    709  1.1  explorer 			opt[3] = rmagic >> 16;
    710  1.1  explorer 			opt[4] = rmagic >> 8;
    711  1.1  explorer 			opt[5] = rmagic;
    712  1.1  explorer 			sppp_cp_send (sp, PPP_LCP, LCP_CONF_NAK,
    713  1.1  explorer 				h->ident, sizeof (opt), &opt);
    714  1.1  explorer badreq:
    715  1.1  explorer 			switch (sp->lcp.state) {
    716  1.1  explorer 			case LCP_STATE_OPENED:
    717  1.1  explorer 				/* Initiate renegotiation. */
    718  1.1  explorer 				sppp_lcp_open (sp);
    719  1.1  explorer 				/* fall through... */
    720  1.1  explorer 			case LCP_STATE_ACK_SENT:
    721  1.1  explorer 				/* Go to closed state. */
    722  1.1  explorer 				sp->lcp.state = LCP_STATE_CLOSED;
    723  1.1  explorer 				sp->ipcp.state = IPCP_STATE_CLOSED;
    724  1.1  explorer 			}
    725  1.1  explorer 			break;
    726  1.1  explorer 		}
    727  1.1  explorer 		/* Send Configure-Ack packet. */
    728  1.1  explorer 		sp->pp_loopcnt = 0;
    729  1.1  explorer 		sppp_cp_send (sp, PPP_LCP, LCP_CONF_ACK,
    730  1.1  explorer 				h->ident, len-4, h+1);
    731  1.1  explorer 		/* Change the state. */
    732  1.1  explorer 		switch (sp->lcp.state) {
    733  1.1  explorer 		case LCP_STATE_CLOSED:
    734  1.1  explorer 			sp->lcp.state = LCP_STATE_ACK_SENT;
    735  1.1  explorer 			break;
    736  1.1  explorer 		case LCP_STATE_ACK_RCVD:
    737  1.1  explorer 			sp->lcp.state = LCP_STATE_OPENED;
    738  1.1  explorer 			sppp_ipcp_open (sp);
    739  1.1  explorer 			break;
    740  1.1  explorer 		case LCP_STATE_OPENED:
    741  1.1  explorer 			/* Remote magic changed -- close session. */
    742  1.1  explorer 			sp->lcp.state = LCP_STATE_CLOSED;
    743  1.1  explorer 			sp->ipcp.state = IPCP_STATE_CLOSED;
    744  1.1  explorer 			/* Initiate renegotiation. */
    745  1.1  explorer 			sppp_lcp_open (sp);
    746  1.1  explorer 			/* An ACK has already been sent. */
    747  1.1  explorer 			sp->lcp.state = LCP_STATE_ACK_SENT;
    748  1.1  explorer 			break;
    749  1.1  explorer 		}
    750  1.1  explorer 		break;
    751  1.1  explorer 	case LCP_CONF_ACK:
    752  1.1  explorer 		if (h->ident != sp->lcp.confid)
    753  1.1  explorer 			break;
    754  1.1  explorer 		UNTIMO (sp);
    755  1.1  explorer 		if (! (ifp->if_flags & IFF_UP) &&
    756  1.1  explorer 		    (ifp->if_flags & IFF_RUNNING)) {
    757  1.1  explorer 			/* Coming out of loopback mode. */
    758  1.1  explorer 			ifp->if_flags |= IFF_UP;
    759  1.1  explorer 			printf (SPPP_PRINTF_FMT ": up\n", SPPP_PRINTF_ARG(ifp));
    760  1.1  explorer 		}
    761  1.1  explorer 		switch (sp->lcp.state) {
    762  1.1  explorer 		case LCP_STATE_CLOSED:
    763  1.1  explorer 			sp->lcp.state = LCP_STATE_ACK_RCVD;
    764  1.1  explorer 			TIMO (sp, 5);
    765  1.1  explorer 			break;
    766  1.1  explorer 		case LCP_STATE_ACK_SENT:
    767  1.1  explorer 			sp->lcp.state = LCP_STATE_OPENED;
    768  1.1  explorer 			sppp_ipcp_open (sp);
    769  1.1  explorer 			break;
    770  1.1  explorer 		}
    771  1.1  explorer 		break;
    772  1.1  explorer 	case LCP_CONF_NAK:
    773  1.1  explorer 		if (h->ident != sp->lcp.confid)
    774  1.1  explorer 			break;
    775  1.1  explorer 		p = (u_int8_t*) (h+1);
    776  1.1  explorer 		if (len>=10 && p[0] == LCP_OPT_MAGIC && p[1] >= 4) {
    777  1.1  explorer 			rmagic = (u_int32_t)p[2] << 24 |
    778  1.1  explorer 				(u_int32_t)p[3] << 16 | p[4] << 8 | p[5];
    779  1.1  explorer 			if (rmagic == ~sp->lcp.magic) {
    780  1.1  explorer 				if (ifp->if_flags & IFF_DEBUG)
    781  1.1  explorer 					printf (SPPP_PRINTF_FMT ": conf nak: magic glitch\n",
    782  1.1  explorer 						SPPP_PRINTF_ARG(ifp));
    783  1.1  explorer 				sp->lcp.magic += time.tv_sec + time.tv_usec;
    784  1.1  explorer 			} else
    785  1.1  explorer 				sp->lcp.magic = rmagic;
    786  1.1  explorer 			}
    787  1.1  explorer 		if (sp->lcp.state != LCP_STATE_ACK_SENT) {
    788  1.1  explorer 			/* Go to closed state. */
    789  1.1  explorer 			sp->lcp.state = LCP_STATE_CLOSED;
    790  1.1  explorer 			sp->ipcp.state = IPCP_STATE_CLOSED;
    791  1.1  explorer 		}
    792  1.1  explorer 		/* The link will be renegotiated after timeout,
    793  1.1  explorer 		 * to avoid endless req-nack loop. */
    794  1.1  explorer 		UNTIMO (sp);
    795  1.1  explorer 		TIMO (sp, 2);
    796  1.1  explorer 		break;
    797  1.1  explorer 	case LCP_CONF_REJ:
    798  1.1  explorer 		if (h->ident != sp->lcp.confid)
    799  1.1  explorer 			break;
    800  1.1  explorer 		UNTIMO (sp);
    801  1.1  explorer 		/* Initiate renegotiation. */
    802  1.1  explorer 		sppp_lcp_open (sp);
    803  1.1  explorer 		if (sp->lcp.state != LCP_STATE_ACK_SENT) {
    804  1.1  explorer 			/* Go to closed state. */
    805  1.1  explorer 			sp->lcp.state = LCP_STATE_CLOSED;
    806  1.1  explorer 			sp->ipcp.state = IPCP_STATE_CLOSED;
    807  1.1  explorer 		}
    808  1.1  explorer 		break;
    809  1.1  explorer 	case LCP_TERM_REQ:
    810  1.1  explorer 		UNTIMO (sp);
    811  1.1  explorer 		/* Send Terminate-Ack packet. */
    812  1.1  explorer 		sppp_cp_send (sp, PPP_LCP, LCP_TERM_ACK, h->ident, 0, 0);
    813  1.1  explorer 		/* Go to closed state. */
    814  1.1  explorer 		sp->lcp.state = LCP_STATE_CLOSED;
    815  1.1  explorer 		sp->ipcp.state = IPCP_STATE_CLOSED;
    816  1.1  explorer 		/* Initiate renegotiation. */
    817  1.1  explorer 		sppp_lcp_open (sp);
    818  1.1  explorer 		break;
    819  1.1  explorer 	case LCP_TERM_ACK:
    820  1.1  explorer 	case LCP_CODE_REJ:
    821  1.1  explorer 	case LCP_PROTO_REJ:
    822  1.1  explorer 		/* Ignore for now. */
    823  1.1  explorer 		break;
    824  1.1  explorer 	case LCP_DISC_REQ:
    825  1.1  explorer 		/* Discard the packet. */
    826  1.1  explorer 		break;
    827  1.1  explorer 	case LCP_ECHO_REQ:
    828  1.1  explorer 		if (sp->lcp.state != LCP_STATE_OPENED)
    829  1.1  explorer 			break;
    830  1.1  explorer 		if (len < 8) {
    831  1.1  explorer 			if (ifp->if_flags & IFF_DEBUG)
    832  1.1  explorer 				printf (SPPP_PRINTF_FMT ": invalid lcp echo request packet length: %d bytes\n",
    833  1.1  explorer 					SPPP_PRINTF_ARG(ifp), len);
    834  1.1  explorer 			break;
    835  1.1  explorer 		}
    836  1.1  explorer 		if (ntohl (*(int32_t*)(h+1)) == sp->lcp.magic) {
    837  1.1  explorer 			/* Line loopback mode detected. */
    838  1.1  explorer 			printf (SPPP_PRINTF_FMT ": loopback\n", SPPP_PRINTF_ARG(ifp));
    839  1.1  explorer 			if_down (ifp);
    840  1.1  explorer 			qflush (&sp->pp_fastq);
    841  1.1  explorer 
    842  1.1  explorer 			/* Shut down the PPP link. */
    843  1.1  explorer 			sp->lcp.state = LCP_STATE_CLOSED;
    844  1.1  explorer 			sp->ipcp.state = IPCP_STATE_CLOSED;
    845  1.1  explorer 			UNTIMO (sp);
    846  1.1  explorer 			/* Initiate negotiation. */
    847  1.1  explorer 			sppp_lcp_open (sp);
    848  1.1  explorer 			break;
    849  1.1  explorer 		}
    850  1.1  explorer 		*(int32_t*)(h+1) = htonl (sp->lcp.magic);
    851  1.1  explorer 		sppp_cp_send (sp, PPP_LCP, LCP_ECHO_REPLY, h->ident, len-4, h+1);
    852  1.1  explorer 		break;
    853  1.1  explorer 	case LCP_ECHO_REPLY:
    854  1.1  explorer 		if (h->ident != sp->lcp.echoid)
    855  1.1  explorer 			break;
    856  1.1  explorer 		if (len < 8) {
    857  1.1  explorer 			if (ifp->if_flags & IFF_DEBUG)
    858  1.1  explorer 				printf (SPPP_PRINTF_FMT ": invalid lcp echo reply packet length: %d bytes\n",
    859  1.1  explorer 					SPPP_PRINTF_ARG(ifp), len);
    860  1.1  explorer 			break;
    861  1.1  explorer 		}
    862  1.1  explorer 		if (ntohl (*(int32_t*)(h+1)) != sp->lcp.magic)
    863  1.1  explorer 		sp->pp_alivecnt = 0;
    864  1.1  explorer 		break;
    865  1.1  explorer 	}
    866  1.1  explorer }
    867  1.1  explorer 
    868  1.1  explorer /*
    869  1.1  explorer  * Handle incoming Cisco keepalive protocol packets.
    870  1.1  explorer  */
    871  1.1  explorer static void
    872  1.1  explorer sppp_cisco_input (struct sppp *sp, struct mbuf *m)
    873  1.1  explorer {
    874  1.1  explorer 	struct cisco_packet *h;
    875  1.1  explorer #if defined(__FreeBSD__)
    876  1.1  explorer 	struct ifaddr *ifa;
    877  1.1  explorer #endif
    878  1.1  explorer 	struct ifnet *ifp = &sp->pp_if;
    879  1.1  explorer 
    880  1.1  explorer 	if (m->m_pkthdr.len < CISCO_PACKET_LEN) {
    881  1.1  explorer 		if (ifp->if_flags & IFF_DEBUG)
    882  1.1  explorer 			printf (SPPP_PRINTF_FMT ": invalid cisco packet length: %d bytes\n",
    883  1.1  explorer 				SPPP_PRINTF_ARG(ifp), m->m_pkthdr.len);
    884  1.1  explorer 		return;
    885  1.1  explorer 	}
    886  1.1  explorer 	h = mtod (m, struct cisco_packet*);
    887  1.1  explorer 	if (ifp->if_flags & IFF_DEBUG)
    888  1.1  explorer 		printf (SPPP_PRINTF_FMT ": cisco input: %d bytes <%xh %xh %xh %xh %xh-%xh>\n",
    889  1.1  explorer 			SPPP_PRINTF_ARG(ifp), m->m_pkthdr.len,
    890  1.1  explorer 			ntohl (h->type), h->par1, h->par2, h->rel,
    891  1.1  explorer 			h->time0, h->time1);
    892  1.1  explorer 	switch (ntohl (h->type)) {
    893  1.1  explorer 	default:
    894  1.1  explorer 		if (ifp->if_flags & IFF_DEBUG)
    895  1.1  explorer 			printf (SPPP_PRINTF_FMT ": unknown cisco packet type: 0x%x\n",
    896  1.1  explorer 				SPPP_PRINTF_ARG(ifp), ntohl (h->type));
    897  1.1  explorer 		break;
    898  1.1  explorer 	case CISCO_ADDR_REPLY:
    899  1.1  explorer 		/* Reply on address request, ignore */
    900  1.1  explorer 		break;
    901  1.1  explorer 	case CISCO_KEEPALIVE_REQ:
    902  1.1  explorer 		sp->pp_alivecnt = 0;
    903  1.1  explorer 		sp->pp_rseq = ntohl (h->par1);
    904  1.1  explorer 		if (sp->pp_seq == sp->pp_rseq) {
    905  1.1  explorer 			/* Local and remote sequence numbers are equal.
    906  1.1  explorer 			 * Probably, the line is in loopback mode. */
    907  1.1  explorer 			if (sp->pp_loopcnt >= MAXALIVECNT) {
    908  1.1  explorer 				printf (SPPP_PRINTF_FMT ": loopback\n",
    909  1.1  explorer 					SPPP_PRINTF_ARG(ifp));
    910  1.1  explorer 				sp->pp_loopcnt = 0;
    911  1.1  explorer 				if (ifp->if_flags & IFF_UP) {
    912  1.1  explorer 					if_down (ifp);
    913  1.1  explorer 					qflush (&sp->pp_fastq);
    914  1.1  explorer 				}
    915  1.1  explorer 			}
    916  1.1  explorer 			++sp->pp_loopcnt;
    917  1.1  explorer 
    918  1.1  explorer 			/* Generate new local sequence number */
    919  1.1  explorer 			sp->pp_seq ^= time.tv_sec ^ time.tv_usec;
    920  1.1  explorer 			break;
    921  1.1  explorer 		}
    922  1.1  explorer 			sp->pp_loopcnt = 0;
    923  1.1  explorer 		if (! (ifp->if_flags & IFF_UP) &&
    924  1.1  explorer 		    (ifp->if_flags & IFF_RUNNING)) {
    925  1.1  explorer 			ifp->if_flags |= IFF_UP;
    926  1.1  explorer 			printf (SPPP_PRINTF_FMT ": up\n", SPPP_PRINTF_ARG(ifp));
    927  1.1  explorer 		}
    928  1.1  explorer 		break;
    929  1.1  explorer 	case CISCO_ADDR_REQ:
    930  1.1  explorer #if defined(__FreeBSD__) /* not yet...  This is a quick port XXXMLG */
    931  1.1  explorer 		for (ifa = ifp->if_addrlist; ifa; ifa = ifa->ifa_next)
    932  1.1  explorer 			if (ifa->ifa_addr->sa_family == AF_INET)
    933  1.1  explorer 				break;
    934  1.1  explorer 		if (! ifa) {
    935  1.1  explorer 			if (ifp->if_flags & IFF_DEBUG)
    936  1.1  explorer 				printf (SPPP_PRINTF_FMT ": unknown address for cisco request\n",
    937  1.1  explorer 					SPPP_PRINTF_ARG(ifp));
    938  1.1  explorer 			return;
    939  1.1  explorer 		}
    940  1.1  explorer 		sppp_cisco_send (sp, CISCO_ADDR_REPLY,
    941  1.1  explorer 			ntohl (((struct sockaddr_in*)ifa->ifa_addr)->sin_addr.s_addr),
    942  1.1  explorer 			ntohl (((struct sockaddr_in*)ifa->ifa_netmask)->sin_addr.s_addr));
    943  1.1  explorer #endif /* FreeBSD above, NetBSD can't do this (yet) -- different structs */
    944  1.1  explorer 		break;
    945  1.1  explorer 	}
    946  1.1  explorer }
    947  1.1  explorer 
    948  1.1  explorer /*
    949  1.1  explorer  * Send PPP LCP packet.
    950  1.1  explorer  */
    951  1.1  explorer static void
    952  1.1  explorer sppp_cp_send (struct sppp *sp, u_int16_t proto, u_int8_t type,
    953  1.1  explorer 	u_int8_t ident, u_int16_t len, void *data)
    954  1.1  explorer {
    955  1.1  explorer 	struct ppp_header *h;
    956  1.1  explorer 	struct lcp_header *lh;
    957  1.1  explorer 	struct mbuf *m;
    958  1.1  explorer 	struct ifnet *ifp = &sp->pp_if;
    959  1.1  explorer 
    960  1.1  explorer 	if (len > MHLEN - PPP_HEADER_LEN - LCP_HEADER_LEN)
    961  1.1  explorer 		len = MHLEN - PPP_HEADER_LEN - LCP_HEADER_LEN;
    962  1.1  explorer 	MGETHDR (m, M_DONTWAIT, MT_DATA);
    963  1.1  explorer 	if (! m)
    964  1.1  explorer 		return;
    965  1.1  explorer 	m->m_pkthdr.len = m->m_len = PPP_HEADER_LEN + LCP_HEADER_LEN + len;
    966  1.1  explorer 	m->m_pkthdr.rcvif = 0;
    967  1.1  explorer 
    968  1.1  explorer 	h = mtod (m, struct ppp_header*);
    969  1.1  explorer 	h->address = PPP_ALLSTATIONS;        /* broadcast address */
    970  1.1  explorer 	h->control = PPP_UI;                 /* Unnumbered Info */
    971  1.1  explorer 	h->protocol = htons (proto);         /* Link Control Protocol */
    972  1.1  explorer 
    973  1.1  explorer 	lh = (struct lcp_header*) (h + 1);
    974  1.1  explorer 	lh->type = type;
    975  1.1  explorer 	lh->ident = ident;
    976  1.1  explorer 	lh->len = htons (LCP_HEADER_LEN + len);
    977  1.1  explorer 	if (len)
    978  1.1  explorer 		bcopy (data, lh+1, len);
    979  1.1  explorer 
    980  1.1  explorer 	if (ifp->if_flags & IFF_DEBUG) {
    981  1.1  explorer 		printf (SPPP_PRINTF_FMT ": %s output <%s id=%xh len=%xh",
    982  1.1  explorer 			SPPP_PRINTF_ARG(ifp),
    983  1.1  explorer 			proto==PPP_LCP ? "lcp" : "ipcp",
    984  1.1  explorer 			proto==PPP_LCP ? sppp_lcp_type_name (lh->type) :
    985  1.1  explorer 			sppp_ipcp_type_name (lh->type), lh->ident,
    986  1.1  explorer 			ntohs (lh->len));
    987  1.1  explorer 		if (len)
    988  1.1  explorer 			sppp_print_bytes ((u_int8_t*) (lh+1), len);
    989  1.1  explorer 		printf (">\n");
    990  1.1  explorer 	}
    991  1.1  explorer 	if (IF_QFULL (&sp->pp_fastq)) {
    992  1.1  explorer 		IF_DROP (&ifp->if_snd);
    993  1.1  explorer 		m_freem (m);
    994  1.1  explorer 	} else
    995  1.1  explorer 		IF_ENQUEUE (&sp->pp_fastq, m);
    996  1.1  explorer 	if (! (ifp->if_flags & IFF_OACTIVE))
    997  1.1  explorer 		(*ifp->if_start) (ifp);
    998  1.1  explorer 	ifp->if_obytes += m->m_pkthdr.len + 3;
    999  1.1  explorer }
   1000  1.1  explorer 
   1001  1.1  explorer /*
   1002  1.1  explorer  * Send Cisco keepalive packet.
   1003  1.1  explorer  */
   1004  1.1  explorer static void
   1005  1.1  explorer sppp_cisco_send (struct sppp *sp, int type, int32_t par1, int32_t par2)
   1006  1.1  explorer {
   1007  1.1  explorer 	struct ppp_header *h;
   1008  1.1  explorer 	struct cisco_packet *ch;
   1009  1.1  explorer 	struct mbuf *m;
   1010  1.1  explorer 	struct ifnet *ifp = &sp->pp_if;
   1011  1.1  explorer 	u_int32_t t = (time.tv_sec - boottime.tv_sec) * 1000;
   1012  1.1  explorer 
   1013  1.1  explorer 	MGETHDR (m, M_DONTWAIT, MT_DATA);
   1014  1.1  explorer 	if (! m)
   1015  1.1  explorer 		return;
   1016  1.1  explorer 	m->m_pkthdr.len = m->m_len = PPP_HEADER_LEN + CISCO_PACKET_LEN;
   1017  1.1  explorer 	m->m_pkthdr.rcvif = 0;
   1018  1.1  explorer 
   1019  1.1  explorer 	h = mtod (m, struct ppp_header*);
   1020  1.1  explorer 	h->address = CISCO_MULTICAST;
   1021  1.1  explorer 	h->control = 0;
   1022  1.1  explorer 	h->protocol = htons (CISCO_KEEPALIVE);
   1023  1.1  explorer 
   1024  1.1  explorer 	ch = (struct cisco_packet*) (h + 1);
   1025  1.1  explorer 	ch->type = htonl (type);
   1026  1.1  explorer 	ch->par1 = htonl (par1);
   1027  1.1  explorer 	ch->par2 = htonl (par2);
   1028  1.1  explorer 	ch->rel = -1;
   1029  1.1  explorer 	ch->time0 = htons ((u_int16_t) (t >> 16));
   1030  1.1  explorer 	ch->time1 = htons ((u_int16_t) t);
   1031  1.1  explorer 
   1032  1.1  explorer 	if (ifp->if_flags & IFF_DEBUG)
   1033  1.1  explorer 		printf (SPPP_PRINTF_FMT ": cisco output: <%xh %xh %xh %xh %xh-%xh>\n",
   1034  1.1  explorer 			SPPP_PRINTF_ARG(ifp), ntohl (ch->type), ch->par1,
   1035  1.1  explorer 			ch->par2, ch->rel, ch->time0, ch->time1);
   1036  1.1  explorer 
   1037  1.1  explorer 	if (IF_QFULL (&sp->pp_fastq)) {
   1038  1.1  explorer 		IF_DROP (&ifp->if_snd);
   1039  1.1  explorer 		m_freem (m);
   1040  1.1  explorer 	} else
   1041  1.1  explorer 		IF_ENQUEUE (&sp->pp_fastq, m);
   1042  1.1  explorer 	if (! (ifp->if_flags & IFF_OACTIVE))
   1043  1.1  explorer 		(*ifp->if_start) (ifp);
   1044  1.1  explorer 	ifp->if_obytes += m->m_pkthdr.len + 3;
   1045  1.1  explorer }
   1046  1.1  explorer 
   1047  1.1  explorer /*
   1048  1.1  explorer  * Process an ioctl request.  Called on low priority level.
   1049  1.1  explorer  */
   1050  1.1  explorer int
   1051  1.1  explorer sppp_ioctl (struct ifnet *ifp, int cmd, void *data)
   1052  1.1  explorer {
   1053  1.1  explorer 	struct ifreq *ifr = (struct ifreq*) data;
   1054  1.1  explorer 	struct sppp *sp = (struct sppp*) ifp;
   1055  1.1  explorer 	int s, going_up, going_down;
   1056  1.1  explorer 
   1057  1.1  explorer 	switch (cmd) {
   1058  1.1  explorer 	default:
   1059  1.1  explorer 		return (EINVAL);
   1060  1.1  explorer 
   1061  1.1  explorer 	case SIOCAIFADDR:
   1062  1.1  explorer 	case SIOCSIFDSTADDR:
   1063  1.1  explorer 		break;
   1064  1.1  explorer 
   1065  1.1  explorer 	case SIOCSIFADDR:
   1066  1.1  explorer 		ifp->if_flags |= IFF_UP;
   1067  1.1  explorer 		/* fall through... */
   1068  1.1  explorer 
   1069  1.1  explorer 	case SIOCSIFFLAGS:
   1070  1.1  explorer 		s = splimp ();
   1071  1.1  explorer 		going_up   = (ifp->if_flags & IFF_UP) &&
   1072  1.1  explorer 			     ! (ifp->if_flags & IFF_RUNNING);
   1073  1.1  explorer 		going_down = ! (ifp->if_flags & IFF_UP) &&
   1074  1.1  explorer 			      (ifp->if_flags & IFF_RUNNING);
   1075  1.1  explorer 		if (going_up || going_down) {
   1076  1.1  explorer 			/* Shut down the PPP link. */
   1077  1.1  explorer 			ifp->if_flags &= ~IFF_RUNNING;
   1078  1.1  explorer 			sp->lcp.state = LCP_STATE_CLOSED;
   1079  1.1  explorer 			sp->ipcp.state = IPCP_STATE_CLOSED;
   1080  1.1  explorer 			UNTIMO (sp);
   1081  1.1  explorer 		}
   1082  1.1  explorer 		if (going_up) {
   1083  1.1  explorer 			/* Interface is starting -- initiate negotiation. */
   1084  1.1  explorer 			ifp->if_flags |= IFF_RUNNING;
   1085  1.1  explorer 			if (!(sp->pp_flags & PP_CISCO))
   1086  1.1  explorer 				sppp_lcp_open (sp);
   1087  1.1  explorer 		}
   1088  1.1  explorer 		splx (s);
   1089  1.1  explorer 		break;
   1090  1.1  explorer 
   1091  1.1  explorer #ifdef SIOCSIFMTU
   1092  1.1  explorer #ifndef ifr_mtu
   1093  1.1  explorer #define ifr_mtu ifr_metric
   1094  1.1  explorer #endif
   1095  1.1  explorer 	case SIOCSIFMTU:
   1096  1.1  explorer 		if (ifr->ifr_mtu < 128 || ifr->ifr_mtu > PP_MTU)
   1097  1.1  explorer 			return (EINVAL);
   1098  1.1  explorer 		ifp->if_mtu = ifr->ifr_mtu;
   1099  1.1  explorer 		break;
   1100  1.1  explorer #endif
   1101  1.1  explorer #ifdef SLIOCSETMTU
   1102  1.1  explorer 	case SLIOCSETMTU:
   1103  1.1  explorer 		if (*(short*)data < 128 || *(short*)data > PP_MTU)
   1104  1.1  explorer 			return (EINVAL);
   1105  1.1  explorer 		ifp->if_mtu = *(short*)data;
   1106  1.1  explorer 		break;
   1107  1.1  explorer #endif
   1108  1.1  explorer #ifdef SIOCGIFMTU
   1109  1.1  explorer 	case SIOCGIFMTU:
   1110  1.1  explorer 		ifr->ifr_mtu = ifp->if_mtu;
   1111  1.1  explorer 		break;
   1112  1.1  explorer #endif
   1113  1.1  explorer #ifdef SLIOCGETMTU
   1114  1.1  explorer 	case SLIOCGETMTU:
   1115  1.1  explorer 		*(short*)data = ifp->if_mtu;
   1116  1.1  explorer 		break;
   1117  1.1  explorer #endif
   1118  1.1  explorer 	case SIOCADDMULTI:
   1119  1.1  explorer 	case SIOCDELMULTI:
   1120  1.1  explorer 		break;
   1121  1.1  explorer 	}
   1122  1.1  explorer 	return (0);
   1123  1.1  explorer }
   1124  1.1  explorer 
   1125  1.1  explorer /*
   1126  1.1  explorer  * Analyze the LCP Configure-Request options list
   1127  1.1  explorer  * for the presence of unknown options.
   1128  1.1  explorer  * If the request contains unknown options, build and
   1129  1.1  explorer  * send Configure-reject packet, containing only unknown options.
   1130  1.1  explorer  */
   1131  1.1  explorer static int
   1132  1.1  explorer sppp_lcp_conf_parse_options (struct sppp *sp, struct lcp_header *h,
   1133  1.1  explorer 	int len, u_int32_t *magic)
   1134  1.1  explorer {
   1135  1.1  explorer 	u_int8_t *buf, *r, *p;
   1136  1.1  explorer 	int rlen;
   1137  1.1  explorer 
   1138  1.1  explorer 	len -= 4;
   1139  1.1  explorer 	buf = r = malloc (len, M_TEMP, M_NOWAIT);
   1140  1.1  explorer 	if (! buf)
   1141  1.1  explorer 		return (0);
   1142  1.1  explorer 
   1143  1.1  explorer 	p = (void*) (h+1);
   1144  1.1  explorer 	for (rlen=0; len>1 && p[1]; len-=p[1], p+=p[1]) {
   1145  1.1  explorer 		switch (*p) {
   1146  1.1  explorer 		case LCP_OPT_MAGIC:
   1147  1.1  explorer 			/* Magic number -- extract. */
   1148  1.1  explorer 			if (len >= 6 && p[1] == 6) {
   1149  1.1  explorer 				*magic = (u_int32_t)p[2] << 24 |
   1150  1.1  explorer 					(u_int32_t)p[3] << 16 | p[4] << 8 | p[5];
   1151  1.1  explorer 				continue;
   1152  1.1  explorer 			}
   1153  1.1  explorer 			break;
   1154  1.1  explorer 		case LCP_OPT_ASYNC_MAP:
   1155  1.1  explorer 			/* Async control character map -- check to be zero. */
   1156  1.1  explorer 			if (len >= 6 && p[1] == 6 && ! p[2] && ! p[3] &&
   1157  1.1  explorer 			    ! p[4] && ! p[5])
   1158  1.1  explorer 				continue;
   1159  1.1  explorer 			break;
   1160  1.1  explorer 		case LCP_OPT_MRU:
   1161  1.1  explorer 			/* Maximum receive unit -- always OK. */
   1162  1.1  explorer 			continue;
   1163  1.1  explorer 		default:
   1164  1.1  explorer 			/* Others not supported. */
   1165  1.1  explorer 			break;
   1166  1.1  explorer 		}
   1167  1.1  explorer 		/* Add the option to rejected list. */
   1168  1.1  explorer 			bcopy (p, r, p[1]);
   1169  1.1  explorer 			r += p[1];
   1170  1.1  explorer 		rlen += p[1];
   1171  1.1  explorer 	}
   1172  1.1  explorer 	if (rlen)
   1173  1.1  explorer 	sppp_cp_send (sp, PPP_LCP, LCP_CONF_REJ, h->ident, rlen, buf);
   1174  1.1  explorer 	free (buf, M_TEMP);
   1175  1.1  explorer 	return (rlen == 0);
   1176  1.1  explorer }
   1177  1.1  explorer 
   1178  1.1  explorer static void
   1179  1.1  explorer sppp_ipcp_input (struct sppp *sp, struct mbuf *m)
   1180  1.1  explorer {
   1181  1.1  explorer 	struct lcp_header *h;
   1182  1.1  explorer 	struct ifnet *ifp = &sp->pp_if;
   1183  1.1  explorer 	int len = m->m_pkthdr.len;
   1184  1.1  explorer 
   1185  1.1  explorer 	if (len < 4) {
   1186  1.1  explorer 		/* if (ifp->if_flags & IFF_DEBUG) */
   1187  1.1  explorer 			printf (SPPP_PRINTF_FMT ": invalid ipcp packet length: %d bytes\n",
   1188  1.1  explorer 				SPPP_PRINTF_ARG(ifp), len);
   1189  1.1  explorer 		return;
   1190  1.1  explorer 	}
   1191  1.1  explorer 	h = mtod (m, struct lcp_header*);
   1192  1.1  explorer 	if (ifp->if_flags & IFF_DEBUG) {
   1193  1.1  explorer 		printf (SPPP_PRINTF_FMT ": ipcp input: %d bytes <%s id=%xh len=%xh",
   1194  1.1  explorer 			SPPP_PRINTF_ARG(ifp), len,
   1195  1.1  explorer 			sppp_ipcp_type_name (h->type), h->ident, ntohs (h->len));
   1196  1.1  explorer 		if (len > 4)
   1197  1.1  explorer 			sppp_print_bytes ((u_int8_t*) (h+1), len-4);
   1198  1.1  explorer 		printf (">\n");
   1199  1.1  explorer 	}
   1200  1.1  explorer 	if (len > ntohs (h->len))
   1201  1.1  explorer 		len = ntohs (h->len);
   1202  1.1  explorer 	switch (h->type) {
   1203  1.1  explorer 	default:
   1204  1.1  explorer 		/* Unknown packet type -- send Code-Reject packet. */
   1205  1.1  explorer 		sppp_cp_send (sp, PPP_IPCP, IPCP_CODE_REJ, ++sp->pp_seq, len, h);
   1206  1.1  explorer 		break;
   1207  1.1  explorer 	case IPCP_CONF_REQ:
   1208  1.1  explorer 		if (len < 4) {
   1209  1.1  explorer 			if (ifp->if_flags & IFF_DEBUG)
   1210  1.1  explorer 				printf (SPPP_PRINTF_FMT ": invalid ipcp configure request packet length: %d bytes\n",
   1211  1.1  explorer 					SPPP_PRINTF_ARG(ifp), len);
   1212  1.1  explorer 			return;
   1213  1.1  explorer 		}
   1214  1.1  explorer 		if (len > 4) {
   1215  1.1  explorer 			sppp_cp_send (sp, PPP_IPCP, LCP_CONF_REJ, h->ident,
   1216  1.1  explorer 				len-4, h+1);
   1217  1.1  explorer 
   1218  1.1  explorer 			switch (sp->ipcp.state) {
   1219  1.1  explorer 			case IPCP_STATE_OPENED:
   1220  1.1  explorer 				/* Initiate renegotiation. */
   1221  1.1  explorer 				sppp_ipcp_open (sp);
   1222  1.1  explorer 				/* fall through... */
   1223  1.1  explorer 			case IPCP_STATE_ACK_SENT:
   1224  1.1  explorer 				/* Go to closed state. */
   1225  1.1  explorer 				sp->ipcp.state = IPCP_STATE_CLOSED;
   1226  1.1  explorer 			}
   1227  1.1  explorer 		} else {
   1228  1.1  explorer 			/* Send Configure-Ack packet. */
   1229  1.1  explorer 			sppp_cp_send (sp, PPP_IPCP, IPCP_CONF_ACK, h->ident,
   1230  1.1  explorer 				0, 0);
   1231  1.1  explorer 			/* Change the state. */
   1232  1.1  explorer 			if (sp->ipcp.state == IPCP_STATE_ACK_RCVD)
   1233  1.1  explorer 				sp->ipcp.state = IPCP_STATE_OPENED;
   1234  1.1  explorer 			else
   1235  1.1  explorer 				sp->ipcp.state = IPCP_STATE_ACK_SENT;
   1236  1.1  explorer 		}
   1237  1.1  explorer 		break;
   1238  1.1  explorer 	case IPCP_CONF_ACK:
   1239  1.1  explorer 		if (h->ident != sp->ipcp.confid)
   1240  1.1  explorer 			break;
   1241  1.1  explorer 		UNTIMO (sp);
   1242  1.1  explorer 		switch (sp->ipcp.state) {
   1243  1.1  explorer 		case IPCP_STATE_CLOSED:
   1244  1.1  explorer 			sp->ipcp.state = IPCP_STATE_ACK_RCVD;
   1245  1.1  explorer 			TIMO (sp, 5);
   1246  1.1  explorer 			break;
   1247  1.1  explorer 		case IPCP_STATE_ACK_SENT:
   1248  1.1  explorer 			sp->ipcp.state = IPCP_STATE_OPENED;
   1249  1.1  explorer 			break;
   1250  1.1  explorer 		}
   1251  1.1  explorer 		break;
   1252  1.1  explorer 	case IPCP_CONF_NAK:
   1253  1.1  explorer 	case IPCP_CONF_REJ:
   1254  1.1  explorer 		if (h->ident != sp->ipcp.confid)
   1255  1.1  explorer 			break;
   1256  1.1  explorer 		UNTIMO (sp);
   1257  1.1  explorer 			/* Initiate renegotiation. */
   1258  1.1  explorer 			sppp_ipcp_open (sp);
   1259  1.1  explorer 		if (sp->ipcp.state != IPCP_STATE_ACK_SENT)
   1260  1.1  explorer 			/* Go to closed state. */
   1261  1.1  explorer 			sp->ipcp.state = IPCP_STATE_CLOSED;
   1262  1.1  explorer 		break;
   1263  1.1  explorer 	case IPCP_TERM_REQ:
   1264  1.1  explorer 		/* Send Terminate-Ack packet. */
   1265  1.1  explorer 		sppp_cp_send (sp, PPP_IPCP, IPCP_TERM_ACK, h->ident, 0, 0);
   1266  1.1  explorer 		/* Go to closed state. */
   1267  1.1  explorer 		sp->ipcp.state = IPCP_STATE_CLOSED;
   1268  1.1  explorer 			/* Initiate renegotiation. */
   1269  1.1  explorer 			sppp_ipcp_open (sp);
   1270  1.1  explorer 		break;
   1271  1.1  explorer 	case IPCP_TERM_ACK:
   1272  1.1  explorer 		/* Ignore for now. */
   1273  1.1  explorer 	case IPCP_CODE_REJ:
   1274  1.1  explorer 		/* Ignore for now. */
   1275  1.1  explorer 		break;
   1276  1.1  explorer 	}
   1277  1.1  explorer }
   1278  1.1  explorer 
   1279  1.1  explorer static void
   1280  1.1  explorer sppp_lcp_open (struct sppp *sp)
   1281  1.1  explorer {
   1282  1.1  explorer 	char opt[6];
   1283  1.1  explorer 
   1284  1.1  explorer 	if (! sp->lcp.magic)
   1285  1.1  explorer 	sp->lcp.magic = time.tv_sec + time.tv_usec;
   1286  1.1  explorer 	opt[0] = LCP_OPT_MAGIC;
   1287  1.1  explorer 	opt[1] = sizeof (opt);
   1288  1.1  explorer 	opt[2] = sp->lcp.magic >> 24;
   1289  1.1  explorer 	opt[3] = sp->lcp.magic >> 16;
   1290  1.1  explorer 	opt[4] = sp->lcp.magic >> 8;
   1291  1.1  explorer 	opt[5] = sp->lcp.magic;
   1292  1.1  explorer 	sp->lcp.confid = ++sp->pp_seq;
   1293  1.1  explorer 	sppp_cp_send (sp, PPP_LCP, LCP_CONF_REQ, sp->lcp.confid,
   1294  1.1  explorer 		sizeof (opt), &opt);
   1295  1.1  explorer 	TIMO (sp, 2);
   1296  1.1  explorer }
   1297  1.1  explorer 
   1298  1.1  explorer static void
   1299  1.1  explorer sppp_ipcp_open (struct sppp *sp)
   1300  1.1  explorer {
   1301  1.1  explorer 	sp->ipcp.confid = ++sp->pp_seq;
   1302  1.1  explorer 	sppp_cp_send (sp, PPP_IPCP, IPCP_CONF_REQ, sp->ipcp.confid, 0, 0);
   1303  1.1  explorer 	TIMO (sp, 2);
   1304  1.1  explorer }
   1305  1.1  explorer 
   1306  1.1  explorer /*
   1307  1.1  explorer  * Process PPP control protocol timeouts.
   1308  1.1  explorer  */
   1309  1.1  explorer static void
   1310  1.1  explorer sppp_cp_timeout (void *arg)
   1311  1.1  explorer {
   1312  1.1  explorer 	struct sppp *sp = (struct sppp*) arg;
   1313  1.1  explorer 	int s = splimp ();
   1314  1.1  explorer 
   1315  1.1  explorer 	sp->pp_flags &= ~PP_TIMO;
   1316  1.1  explorer 	if (! (sp->pp_if.if_flags & IFF_RUNNING) || (sp->pp_flags & PP_CISCO)) {
   1317  1.1  explorer 		splx (s);
   1318  1.1  explorer 		return;
   1319  1.1  explorer 	}
   1320  1.1  explorer 	switch (sp->lcp.state) {
   1321  1.1  explorer 	case LCP_STATE_CLOSED:
   1322  1.1  explorer 		/* No ACK for Configure-Request, retry. */
   1323  1.1  explorer 		sppp_lcp_open (sp);
   1324  1.1  explorer 		break;
   1325  1.1  explorer 	case LCP_STATE_ACK_RCVD:
   1326  1.1  explorer 		/* ACK got, but no Configure-Request for peer, retry. */
   1327  1.1  explorer 		sppp_lcp_open (sp);
   1328  1.1  explorer 		sp->lcp.state = LCP_STATE_CLOSED;
   1329  1.1  explorer 		break;
   1330  1.1  explorer 	case LCP_STATE_ACK_SENT:
   1331  1.1  explorer 		/* ACK sent but no ACK for Configure-Request, retry. */
   1332  1.1  explorer 		sppp_lcp_open (sp);
   1333  1.1  explorer 		break;
   1334  1.1  explorer 	case LCP_STATE_OPENED:
   1335  1.1  explorer 		/* LCP is already OK, try IPCP. */
   1336  1.1  explorer 		switch (sp->ipcp.state) {
   1337  1.1  explorer 		case IPCP_STATE_CLOSED:
   1338  1.1  explorer 			/* No ACK for Configure-Request, retry. */
   1339  1.1  explorer 			sppp_ipcp_open (sp);
   1340  1.1  explorer 			break;
   1341  1.1  explorer 		case IPCP_STATE_ACK_RCVD:
   1342  1.1  explorer 			/* ACK got, but no Configure-Request for peer, retry. */
   1343  1.1  explorer 			sppp_ipcp_open (sp);
   1344  1.1  explorer 			sp->ipcp.state = IPCP_STATE_CLOSED;
   1345  1.1  explorer 			break;
   1346  1.1  explorer 		case IPCP_STATE_ACK_SENT:
   1347  1.1  explorer 			/* ACK sent but no ACK for Configure-Request, retry. */
   1348  1.1  explorer 			sppp_ipcp_open (sp);
   1349  1.1  explorer 			break;
   1350  1.1  explorer 		case IPCP_STATE_OPENED:
   1351  1.1  explorer 			/* IPCP is OK. */
   1352  1.1  explorer 			break;
   1353  1.1  explorer 		}
   1354  1.1  explorer 		break;
   1355  1.1  explorer 	}
   1356  1.1  explorer 	splx (s);
   1357  1.1  explorer }
   1358  1.1  explorer 
   1359  1.1  explorer static char
   1360  1.1  explorer *sppp_lcp_type_name (u_int8_t type)
   1361  1.1  explorer {
   1362  1.1  explorer 	static char buf [8];
   1363  1.1  explorer 	switch (type) {
   1364  1.1  explorer 	case LCP_CONF_REQ:   return ("conf-req");
   1365  1.1  explorer 	case LCP_CONF_ACK:   return ("conf-ack");
   1366  1.1  explorer 	case LCP_CONF_NAK:   return ("conf-nack");
   1367  1.1  explorer 	case LCP_CONF_REJ:   return ("conf-rej");
   1368  1.1  explorer 	case LCP_TERM_REQ:   return ("term-req");
   1369  1.1  explorer 	case LCP_TERM_ACK:   return ("term-ack");
   1370  1.1  explorer 	case LCP_CODE_REJ:   return ("code-rej");
   1371  1.1  explorer 	case LCP_PROTO_REJ:  return ("proto-rej");
   1372  1.1  explorer 	case LCP_ECHO_REQ:   return ("echo-req");
   1373  1.1  explorer 	case LCP_ECHO_REPLY: return ("echo-reply");
   1374  1.1  explorer 	case LCP_DISC_REQ:   return ("discard-req");
   1375  1.1  explorer 	}
   1376  1.1  explorer 	sprintf (buf, "%xh", type);
   1377  1.1  explorer 	return (buf);
   1378  1.1  explorer }
   1379  1.1  explorer 
   1380  1.1  explorer static char
   1381  1.1  explorer *sppp_ipcp_type_name (u_int8_t type)
   1382  1.1  explorer {
   1383  1.1  explorer 	static char buf [8];
   1384  1.1  explorer 	switch (type) {
   1385  1.1  explorer 	case IPCP_CONF_REQ:   return ("conf-req");
   1386  1.1  explorer 	case IPCP_CONF_ACK:   return ("conf-ack");
   1387  1.1  explorer 	case IPCP_CONF_NAK:   return ("conf-nack");
   1388  1.1  explorer 	case IPCP_CONF_REJ:   return ("conf-rej");
   1389  1.1  explorer 	case IPCP_TERM_REQ:   return ("term-req");
   1390  1.1  explorer 	case IPCP_TERM_ACK:   return ("term-ack");
   1391  1.1  explorer 	case IPCP_CODE_REJ:   return ("code-rej");
   1392  1.1  explorer 	}
   1393  1.1  explorer 	sprintf (buf, "%xh", type);
   1394  1.1  explorer 	return (buf);
   1395  1.1  explorer }
   1396  1.1  explorer 
   1397  1.1  explorer static void
   1398  1.1  explorer sppp_print_bytes (u_int8_t *p, u_int16_t len)
   1399  1.1  explorer {
   1400  1.1  explorer 	printf (" %x", *p++);
   1401  1.1  explorer 	while (--len > 0)
   1402  1.1  explorer 		printf ("-%x", *p++);
   1403  1.1  explorer }
   1404