Home | History | Annotate | Line # | Download | only in net
if_spppsubr.c revision 1.202
      1  1.202  yamaguch /*	$NetBSD: if_spppsubr.c,v 1.202 2020/11/25 09:55:01 yamaguchi Exp $	 */
      2    1.4  explorer 
      3    1.1  explorer /*
      4    1.1  explorer  * Synchronous PPP/Cisco link level subroutines.
      5    1.1  explorer  * Keepalive protocol implemented in both Cisco and PPP modes.
      6    1.1  explorer  *
      7    1.3  explorer  * Copyright (C) 1994-1996 Cronyx Engineering Ltd.
      8    1.3  explorer  * Author: Serge Vakulenko, <vak (at) cronyx.ru>
      9    1.3  explorer  *
     10    1.3  explorer  * Heavily revamped to conform to RFC 1661.
     11    1.3  explorer  * Copyright (C) 1997, Joerg Wunsch.
     12    1.1  explorer  *
     13    1.9    itojun  * RFC2472 IPv6CP support.
     14    1.9    itojun  * Copyright (C) 2000, Jun-ichiro itojun Hagino <itojun (at) iijlab.net>.
     15    1.9    itojun  *
     16   1.60    itojun  * Redistribution and use in source and binary forms, with or without
     17   1.60    itojun  * modification, are permitted provided that the following conditions are met:
     18   1.60    itojun  * 1. Redistributions of source code must retain the above copyright notice,
     19   1.60    itojun  *    this list of conditions and the following disclaimer.
     20   1.60    itojun  * 2. Redistributions in binary form must reproduce the above copyright notice,
     21   1.60    itojun  *    this list of conditions and the following disclaimer in the documentation
     22   1.60    itojun  *    and/or other materials provided with the distribution.
     23   1.60    itojun  *
     24   1.82     perry  * THIS SOFTWARE IS PROVIDED BY THE FREEBSD PROJECT ``AS IS'' AND ANY
     25   1.82     perry  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     26   1.60    itojun  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27   1.60    itojun  * ARE DISCLAIMED. IN NO EVENT SHALL THE FREEBSD PROJECT OR CONTRIBUTORS BE
     28   1.60    itojun  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     29   1.60    itojun  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     30   1.60    itojun  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     31   1.60    itojun  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     32   1.60    itojun  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     33   1.82     perry  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     34   1.60    itojun  * POSSIBILITY OF SUCH DAMAGE.
     35    1.1  explorer  *
     36    1.3  explorer  * From: Version 2.4, Thu Apr 30 17:17:21 MSD 1997
     37    1.3  explorer  *
     38    1.3  explorer  * From: if_spppsubr.c,v 1.39 1998/04/04 13:26:03 phk Exp
     39    1.3  explorer  *
     40    1.4  explorer  * From: Id: if_spppsubr.c,v 1.23 1999/02/23 14:47:50 hm Exp
     41    1.1  explorer  */
     42   1.29     lukem 
     43   1.29     lukem #include <sys/cdefs.h>
     44  1.202  yamaguch __KERNEL_RCSID(0, "$NetBSD: if_spppsubr.c,v 1.202 2020/11/25 09:55:01 yamaguchi Exp $");
     45    1.1  explorer 
     46  1.120  jmcneill #if defined(_KERNEL_OPT)
     47    1.1  explorer #include "opt_inet.h"
     48  1.120  jmcneill #include "opt_modular.h"
     49  1.120  jmcneill #include "opt_compat_netbsd.h"
     50  1.157  knakahar #include "opt_net_mpsafe.h"
     51  1.120  jmcneill #endif
     52  1.120  jmcneill 
     53    1.1  explorer 
     54    1.1  explorer #include <sys/param.h>
     55   1.36    martin #include <sys/proc.h>
     56    1.1  explorer #include <sys/systm.h>
     57    1.1  explorer #include <sys/kernel.h>
     58    1.3  explorer #include <sys/sockio.h>
     59    1.1  explorer #include <sys/socket.h>
     60    1.3  explorer #include <sys/syslog.h>
     61    1.3  explorer #include <sys/malloc.h>
     62    1.1  explorer #include <sys/mbuf.h>
     63   1.36    martin #include <sys/callout.h>
     64    1.3  explorer #include <sys/md5.h>
     65   1.63      tron #include <sys/inttypes.h>
     66   1.90      elad #include <sys/kauth.h>
     67  1.124       tls #include <sys/cprng.h>
     68  1.147  pgoyette #include <sys/module.h>
     69  1.159  knakahar #include <sys/workqueue.h>
     70  1.159  knakahar #include <sys/atomic.h>
     71  1.181  pgoyette #include <sys/compat_stub.h>
     72    1.3  explorer 
     73    1.1  explorer #include <net/if.h>
     74    1.1  explorer #include <net/netisr.h>
     75    1.1  explorer #include <net/if_types.h>
     76    1.3  explorer #include <net/route.h>
     77   1.11  sommerfe #include <net/ppp_defs.h>
     78    1.3  explorer 
     79    1.1  explorer #include <netinet/in.h>
     80    1.1  explorer #include <netinet/in_systm.h>
     81    1.1  explorer #include <netinet/in_var.h>
     82   1.12    itojun #ifdef INET
     83    1.1  explorer #include <netinet/ip.h>
     84    1.1  explorer #include <netinet/tcp.h>
     85   1.12    itojun #endif
     86    1.3  explorer #include <net/ethertypes.h>
     87    1.1  explorer 
     88   1.87    rpaulo #ifdef INET6
     89   1.87    rpaulo #include <netinet6/scope6_var.h>
     90   1.87    rpaulo #endif
     91   1.87    rpaulo 
     92    1.1  explorer #include <net/if_sppp.h>
     93   1.36    martin #include <net/if_spppvar.h>
     94    1.1  explorer 
     95  1.163  knakahar #ifdef NET_MPSAFE
     96  1.163  knakahar #define SPPPSUBR_MPSAFE	1
     97  1.163  knakahar #endif
     98  1.163  knakahar 
     99   1.68    martin #define	LCP_KEEPALIVE_INTERVAL		10	/* seconds between checks */
    100   1.68    martin #define LOOPALIVECNT     		3	/* loopback detection tries */
    101   1.68    martin #define DEFAULT_MAXALIVECNT    		3	/* max. missed alive packets */
    102   1.68    martin #define	DEFAULT_NORECV_TIME		15	/* before we get worried */
    103   1.39    martin #define DEFAULT_MAX_AUTH_FAILURES	5	/* max. auth. failures */
    104    1.1  explorer 
    105    1.3  explorer /*
    106    1.3  explorer  * Interface flags that can be set in an ifconfig command.
    107    1.3  explorer  *
    108    1.3  explorer  * Setting link0 will make the link passive, i.e. it will be marked
    109    1.3  explorer  * as being administrative openable, but won't be opened to begin
    110    1.3  explorer  * with.  Incoming calls will be answered, or subsequent calls with
    111    1.3  explorer  * -link1 will cause the administrative open of the LCP layer.
    112    1.3  explorer  *
    113    1.3  explorer  * Setting link1 will cause the link to auto-dial only as packets
    114    1.3  explorer  * arrive to be sent.
    115    1.3  explorer  *
    116    1.3  explorer  * Setting IFF_DEBUG will syslog the option negotiation and state
    117    1.3  explorer  * transitions at level kern.debug.  Note: all logs consistently look
    118    1.3  explorer  * like
    119    1.3  explorer  *
    120    1.3  explorer  *   <if-name><unit>: <proto-name> <additional info...>
    121    1.3  explorer  *
    122    1.3  explorer  * with <if-name><unit> being something like "bppp0", and <proto-name>
    123    1.3  explorer  * being one of "lcp", "ipcp", "cisco", "chap", "pap", etc.
    124    1.3  explorer  */
    125    1.3  explorer 
    126    1.3  explorer #define IFF_PASSIVE	IFF_LINK0	/* wait passively for connection */
    127    1.3  explorer #define IFF_AUTO	IFF_LINK1	/* auto-dial on output */
    128    1.3  explorer 
    129    1.3  explorer #define CONF_REQ	1		/* PPP configure request */
    130    1.3  explorer #define CONF_ACK	2		/* PPP configure acknowledge */
    131    1.3  explorer #define CONF_NAK	3		/* PPP configure negative ack */
    132    1.3  explorer #define CONF_REJ	4		/* PPP configure reject */
    133    1.3  explorer #define TERM_REQ	5		/* PPP terminate request */
    134    1.3  explorer #define TERM_ACK	6		/* PPP terminate acknowledge */
    135    1.3  explorer #define CODE_REJ	7		/* PPP code reject */
    136    1.3  explorer #define PROTO_REJ	8		/* PPP protocol reject */
    137    1.3  explorer #define ECHO_REQ	9		/* PPP echo request */
    138    1.3  explorer #define ECHO_REPLY	10		/* PPP echo reply */
    139    1.3  explorer #define DISC_REQ	11		/* PPP discard request */
    140    1.3  explorer 
    141    1.3  explorer #define LCP_OPT_MRU		1	/* maximum receive unit */
    142    1.3  explorer #define LCP_OPT_ASYNC_MAP	2	/* async control character map */
    143    1.3  explorer #define LCP_OPT_AUTH_PROTO	3	/* authentication protocol */
    144    1.3  explorer #define LCP_OPT_QUAL_PROTO	4	/* quality protocol */
    145    1.3  explorer #define LCP_OPT_MAGIC		5	/* magic number */
    146    1.3  explorer #define LCP_OPT_RESERVED	6	/* reserved */
    147    1.3  explorer #define LCP_OPT_PROTO_COMP	7	/* protocol field compression */
    148    1.3  explorer #define LCP_OPT_ADDR_COMP	8	/* address/control field compression */
    149  1.188        is #define LCP_OPT_FCS_ALTS	9	/* FCS alternatives */
    150  1.188        is #define LCP_OPT_SELF_DESC_PAD	10	/* self-describing padding */
    151  1.188        is #define LCP_OPT_CALL_BACK	13	/* callback */
    152  1.188        is #define LCP_OPT_COMPOUND_FRMS	15	/* compound frames */
    153  1.188        is #define LCP_OPT_MP_MRRU		17	/* multilink MRRU */
    154  1.188        is #define LCP_OPT_MP_SSNHF	18	/* multilink short seq. numbers */
    155  1.188        is #define LCP_OPT_MP_EID		19	/* multilink endpoint discriminator */
    156    1.3  explorer 
    157    1.3  explorer #define IPCP_OPT_ADDRESSES	1	/* both IP addresses; deprecated */
    158   1.14    itojun #define IPCP_OPT_COMPRESSION	2	/* IP compression protocol */
    159    1.3  explorer #define IPCP_OPT_ADDRESS	3	/* local IP address */
    160   1.31    martin #define	IPCP_OPT_PRIMDNS	129	/* primary remote dns address */
    161   1.31    martin #define	IPCP_OPT_SECDNS		131	/* secondary remote dns address */
    162    1.3  explorer 
    163  1.161  knakahar #define IPCP_UPDATE_LIMIT	8	/* limit of pending IP updating job */
    164  1.161  knakahar #define IPCP_SET_ADDRS		1	/* marker for IP address setting job */
    165  1.161  knakahar #define IPCP_CLEAR_ADDRS	2	/* marker for IP address clearing job */
    166  1.161  knakahar 
    167    1.9    itojun #define IPV6CP_OPT_IFID		1	/* interface identifier */
    168    1.9    itojun #define IPV6CP_OPT_COMPRESSION	2	/* IPv6 compression protocol */
    169    1.9    itojun 
    170    1.3  explorer #define PAP_REQ			1	/* PAP name/password request */
    171    1.3  explorer #define PAP_ACK			2	/* PAP acknowledge */
    172    1.3  explorer #define PAP_NAK			3	/* PAP fail */
    173    1.3  explorer 
    174    1.3  explorer #define CHAP_CHALLENGE		1	/* CHAP challenge request */
    175    1.3  explorer #define CHAP_RESPONSE		2	/* CHAP challenge response */
    176    1.3  explorer #define CHAP_SUCCESS		3	/* CHAP response ok */
    177    1.3  explorer #define CHAP_FAILURE		4	/* CHAP response failed */
    178    1.3  explorer 
    179    1.3  explorer #define CHAP_MD5		5	/* hash algorithm - MD5 */
    180    1.3  explorer 
    181    1.3  explorer #define CISCO_MULTICAST		0x8f	/* Cisco multicast address */
    182    1.3  explorer #define CISCO_UNICAST		0x0f	/* Cisco unicast address */
    183    1.3  explorer #define CISCO_KEEPALIVE		0x8035	/* Cisco keepalive protocol */
    184    1.3  explorer #define CISCO_ADDR_REQ		0	/* Cisco address request */
    185    1.3  explorer #define CISCO_ADDR_REPLY	1	/* Cisco address reply */
    186    1.3  explorer #define CISCO_KEEPALIVE_REQ	2	/* Cisco keepalive request */
    187    1.3  explorer 
    188    1.3  explorer /* states are named and numbered according to RFC 1661 */
    189    1.3  explorer #define STATE_INITIAL	0
    190    1.3  explorer #define STATE_STARTING	1
    191    1.3  explorer #define STATE_CLOSED	2
    192    1.3  explorer #define STATE_STOPPED	3
    193    1.3  explorer #define STATE_CLOSING	4
    194    1.3  explorer #define STATE_STOPPING	5
    195    1.3  explorer #define STATE_REQ_SENT	6
    196    1.3  explorer #define STATE_ACK_RCVD	7
    197    1.3  explorer #define STATE_ACK_SENT	8
    198    1.3  explorer #define STATE_OPENED	9
    199    1.1  explorer 
    200    1.1  explorer struct ppp_header {
    201  1.109      matt 	uint8_t address;
    202  1.109      matt 	uint8_t control;
    203  1.109      matt 	uint16_t protocol;
    204  1.107     perry } __packed;
    205    1.1  explorer #define PPP_HEADER_LEN          sizeof (struct ppp_header)
    206    1.1  explorer 
    207    1.1  explorer struct lcp_header {
    208  1.109      matt 	uint8_t type;
    209  1.109      matt 	uint8_t ident;
    210  1.109      matt 	uint16_t len;
    211  1.107     perry } __packed;
    212    1.1  explorer #define LCP_HEADER_LEN          sizeof (struct lcp_header)
    213    1.1  explorer 
    214    1.1  explorer struct cisco_packet {
    215  1.109      matt 	uint32_t type;
    216  1.109      matt 	uint32_t par1;
    217  1.109      matt 	uint32_t par2;
    218  1.109      matt 	uint16_t rel;
    219  1.109      matt 	uint16_t time0;
    220  1.109      matt 	uint16_t time1;
    221  1.107     perry } __packed;
    222    1.1  explorer #define CISCO_PACKET_LEN 18
    223    1.1  explorer 
    224    1.3  explorer /*
    225    1.3  explorer  * We follow the spelling and capitalization of RFC 1661 here, to make
    226    1.3  explorer  * it easier comparing with the standard.  Please refer to this RFC in
    227    1.3  explorer  * case you can't make sense out of these abbreviation; it will also
    228    1.3  explorer  * explain the semantics related to the various events and actions.
    229    1.3  explorer  */
    230    1.3  explorer struct cp {
    231    1.3  explorer 	u_short	proto;		/* PPP control protocol number */
    232    1.3  explorer 	u_char protoidx;	/* index into state table in struct sppp */
    233    1.3  explorer 	u_char flags;
    234    1.3  explorer #define CP_LCP		0x01	/* this is the LCP */
    235    1.3  explorer #define CP_AUTH		0x02	/* this is an authentication protocol */
    236    1.3  explorer #define CP_NCP		0x04	/* this is a NCP */
    237    1.3  explorer #define CP_QUAL		0x08	/* this is a quality reporting protocol */
    238    1.3  explorer 	const char *name;	/* name of this control protocol */
    239    1.3  explorer 	/* event handlers */
    240  1.198  yamaguch 	void	(*Up)(struct sppp *, void *);
    241  1.198  yamaguch 	void	(*Down)(struct sppp *, void *);
    242  1.198  yamaguch 	void	(*Open)(struct sppp *, void *);
    243  1.198  yamaguch 	void	(*Close)(struct sppp *, void *);
    244  1.198  yamaguch 	void	(*TO)(struct sppp *, void *);
    245  1.191  yamaguch 	int	(*RCR)(struct sppp *, struct lcp_header *, int);
    246  1.191  yamaguch 	void	(*RCN_rej)(struct sppp *, struct lcp_header *, int);
    247  1.191  yamaguch 	void	(*RCN_nak)(struct sppp *, struct lcp_header *, int);
    248    1.3  explorer 	/* actions */
    249  1.202  yamaguch 	void	(*tlu)(struct sppp *);
    250  1.202  yamaguch 	void	(*tld)(struct sppp *);
    251  1.202  yamaguch 	void	(*tls)(const struct cp *, struct sppp *);
    252  1.202  yamaguch 	void	(*tlf)(const struct cp *, struct sppp *);
    253  1.202  yamaguch 	void	(*scr)(struct sppp *);
    254  1.194  yamaguch 	void	(*scan)(const struct cp *, struct sppp *);
    255    1.3  explorer };
    256    1.3  explorer 
    257  1.201  yamaguch enum auth_role {
    258  1.201  yamaguch 	SPPP_AUTH_NOROLE = 0,
    259  1.201  yamaguch 	SPPP_AUTH_SERV = __BIT(0),
    260  1.201  yamaguch 	SPPP_AUTH_PEER = __BIT(1),
    261  1.201  yamaguch };
    262  1.201  yamaguch 
    263    1.1  explorer static struct sppp *spppq;
    264  1.163  knakahar static kmutex_t *spppq_lock = NULL;
    265  1.106        ad static callout_t keepalive_ch;
    266    1.3  explorer 
    267  1.163  knakahar #define SPPPQ_LOCK()	if (spppq_lock) \
    268  1.163  knakahar 				mutex_enter(spppq_lock);
    269  1.163  knakahar #define SPPPQ_UNLOCK()	if (spppq_lock) \
    270  1.163  knakahar 				mutex_exit(spppq_lock);
    271  1.163  knakahar 
    272  1.170  knakahar #define SPPP_LOCK(_sp, _op)	rw_enter(&(_sp)->pp_lock, (_op))
    273  1.170  knakahar #define SPPP_UNLOCK(_sp)	rw_exit(&(_sp)->pp_lock)
    274  1.170  knakahar #define SPPP_WLOCKED(_sp)	rw_write_held(&(_sp)->pp_lock)
    275  1.170  knakahar #define SPPP_UPGRADE(_sp)	do{	\
    276  1.170  knakahar 	SPPP_UNLOCK(_sp);		\
    277  1.170  knakahar 	SPPP_LOCK(_sp, RW_WRITER);	\
    278  1.170  knakahar }while (0)
    279  1.170  knakahar #define SPPP_DOWNGRADE(_sp)	rw_downgrade(&(_sp)->pp_lock)
    280  1.198  yamaguch #define SPPP_WQ_SET(_wk, _func, _arg)	\
    281  1.198  yamaguch 	sppp_wq_set((_wk), (_func), __UNCONST((_arg)))
    282  1.170  knakahar 
    283   1.12    itojun #ifdef INET
    284  1.163  knakahar #ifndef SPPPSUBR_MPSAFE
    285    1.1  explorer /*
    286    1.1  explorer  * The following disgusting hack gets around the problem that IP TOS
    287    1.1  explorer  * can't be set yet.  We want to put "interactive" traffic on a high
    288    1.1  explorer  * priority queue.  To decide if traffic is interactive, we check that
    289    1.1  explorer  * a) it is TCP and b) one of its ports is telnet, rlogin or ftp control.
    290    1.3  explorer  *
    291    1.3  explorer  * XXX is this really still necessary?  - joerg -
    292    1.1  explorer  */
    293    1.3  explorer static u_short interactive_ports[8] = {
    294    1.1  explorer 	0,	513,	0,	0,
    295    1.1  explorer 	0,	21,	0,	23,
    296    1.1  explorer };
    297   1.59    itojun #define INTERACTIVE(p)	(interactive_ports[(p) & 7] == (p))
    298  1.163  knakahar #endif /* SPPPSUBR_MPSAFE */
    299   1.12    itojun #endif
    300    1.1  explorer 
    301    1.3  explorer /* almost every function needs these */
    302    1.3  explorer #define STDDCL							\
    303    1.3  explorer 	struct ifnet *ifp = &sp->pp_if;				\
    304    1.3  explorer 	int debug = ifp->if_flags & IFF_DEBUG
    305    1.3  explorer 
    306  1.191  yamaguch static int sppp_output(struct ifnet *, struct mbuf *,
    307  1.191  yamaguch 		       const struct sockaddr *, const struct rtentry *);
    308    1.3  explorer 
    309  1.191  yamaguch static void sppp_cisco_send(struct sppp *, int, int32_t, int32_t);
    310  1.191  yamaguch static void sppp_cisco_input(struct sppp *, struct mbuf *);
    311    1.3  explorer 
    312  1.198  yamaguch static void sppp_cp_init(const struct cp *, struct sppp *);
    313  1.198  yamaguch static void sppp_cp_fini(const struct cp *, struct sppp *);
    314  1.198  yamaguch static void sppp_cp_input(const struct cp *, struct sppp *,
    315  1.198  yamaguch 			  struct mbuf *);
    316  1.191  yamaguch static void sppp_cp_input(const struct cp *, struct sppp *,
    317  1.191  yamaguch 			  struct mbuf *);
    318  1.191  yamaguch static void sppp_cp_send(struct sppp *, u_short, u_char,
    319  1.191  yamaguch 			 u_char, u_short, void *);
    320    1.3  explorer /* static void sppp_cp_timeout(void *arg); */
    321  1.191  yamaguch static void sppp_cp_change_state(const struct cp *, struct sppp *, int);
    322  1.198  yamaguch static struct workqueue *
    323  1.198  yamaguch     sppp_wq_create(struct sppp *, const char *, pri_t, int, int);
    324  1.198  yamaguch static void sppp_wq_destroy(struct sppp *, struct workqueue *);
    325  1.198  yamaguch static void sppp_wq_set(struct sppp_work *,
    326  1.198  yamaguch     void (*)(struct sppp *, void *), void *);
    327  1.198  yamaguch static void sppp_wq_add(struct workqueue *, struct sppp_work *);
    328  1.198  yamaguch static void sppp_wq_wait(struct workqueue *, struct sppp_work *);
    329  1.198  yamaguch static void sppp_cp_to_lcp(void *);
    330  1.198  yamaguch static void sppp_cp_to_ipcp(void *);
    331  1.198  yamaguch static void sppp_cp_to_ipv6cp(void *);
    332  1.191  yamaguch static void sppp_auth_send(const struct cp *, struct sppp *,
    333  1.191  yamaguch 			    unsigned int, unsigned int, ...);
    334  1.201  yamaguch static int sppp_auth_role(const struct cp *, struct sppp *);
    335  1.202  yamaguch static void sppp_auth_to_event(struct sppp *, void *);
    336  1.202  yamaguch static void sppp_auth_sca_scn(const struct cp *, struct sppp *);
    337  1.191  yamaguch 
    338  1.198  yamaguch static void sppp_up_event(struct sppp *, void *);
    339  1.198  yamaguch static void sppp_down_event(struct sppp *, void *);
    340  1.198  yamaguch static void sppp_open_event(struct sppp *, void *);
    341  1.198  yamaguch static void sppp_close_event(struct sppp *, void *);
    342  1.198  yamaguch static void sppp_to_event(struct sppp *, void *);
    343  1.198  yamaguch static void sppp_rcr_event(struct sppp *, void *);
    344  1.198  yamaguch static void sppp_rca_event(struct sppp *, void *);
    345  1.198  yamaguch static void sppp_rcn_event(struct sppp *, void *);
    346  1.198  yamaguch static void sppp_rtr_event(struct sppp *, void *);
    347  1.198  yamaguch static void sppp_rta_event(struct sppp *, void *);
    348  1.198  yamaguch static void sppp_rxj_event(struct sppp *, void *);
    349  1.191  yamaguch 
    350  1.191  yamaguch static void sppp_null(struct sppp *);
    351  1.202  yamaguch static void sppp_tls(const struct cp *, struct sppp *);
    352  1.202  yamaguch static void sppp_tlf(const struct cp *, struct sppp *);
    353  1.194  yamaguch static void sppp_sca_scn(const struct cp *, struct sppp *);
    354  1.199  yamaguch static void sppp_ifdown(struct sppp *, void *);
    355  1.191  yamaguch 
    356  1.191  yamaguch static void sppp_lcp_init(struct sppp *);
    357  1.198  yamaguch static void sppp_lcp_up(struct sppp *, void *);
    358  1.198  yamaguch static void sppp_lcp_down(struct sppp *, void *);
    359  1.198  yamaguch static void sppp_lcp_open(struct sppp *, void *);
    360  1.191  yamaguch static int sppp_lcp_RCR(struct sppp *, struct lcp_header *, int);
    361  1.191  yamaguch static void sppp_lcp_RCN_rej(struct sppp *, struct lcp_header *, int);
    362  1.191  yamaguch static void sppp_lcp_RCN_nak(struct sppp *, struct lcp_header *, int);
    363  1.191  yamaguch static void sppp_lcp_tlu(struct sppp *);
    364  1.191  yamaguch static void sppp_lcp_tld(struct sppp *);
    365  1.202  yamaguch static void sppp_lcp_tls(const struct cp *, struct sppp *);
    366  1.202  yamaguch static void sppp_lcp_tlf(const struct cp *, struct sppp *);
    367  1.191  yamaguch static void sppp_lcp_scr(struct sppp *);
    368  1.191  yamaguch static void sppp_lcp_check_and_close(struct sppp *);
    369  1.202  yamaguch static int sppp_cp_check(struct sppp *, u_char);
    370  1.191  yamaguch 
    371  1.191  yamaguch static void sppp_ipcp_init(struct sppp *);
    372  1.198  yamaguch static void sppp_ipcp_open(struct sppp *, void *);
    373  1.198  yamaguch static void sppp_ipcp_close(struct sppp *, void *);
    374  1.191  yamaguch static int sppp_ipcp_RCR(struct sppp *, struct lcp_header *, int);
    375  1.191  yamaguch static void sppp_ipcp_RCN_rej(struct sppp *, struct lcp_header *, int);
    376  1.191  yamaguch static void sppp_ipcp_RCN_nak(struct sppp *, struct lcp_header *, int);
    377  1.191  yamaguch static void sppp_ipcp_tlu(struct sppp *);
    378  1.191  yamaguch static void sppp_ipcp_scr(struct sppp *);
    379  1.191  yamaguch 
    380  1.191  yamaguch static void sppp_ipv6cp_init(struct sppp *);
    381  1.198  yamaguch static void sppp_ipv6cp_open(struct sppp *, void *);
    382  1.191  yamaguch static int sppp_ipv6cp_RCR(struct sppp *, struct lcp_header *, int);
    383  1.191  yamaguch static void sppp_ipv6cp_RCN_rej(struct sppp *, struct lcp_header *, int);
    384  1.191  yamaguch static void sppp_ipv6cp_RCN_nak(struct sppp *, struct lcp_header *, int);
    385  1.191  yamaguch static void sppp_ipv6cp_tlu(struct sppp *);
    386  1.191  yamaguch static void sppp_ipv6cp_scr(struct sppp *);
    387  1.191  yamaguch 
    388  1.191  yamaguch static void sppp_pap_input(struct sppp *, struct mbuf *);
    389  1.191  yamaguch static void sppp_pap_init(struct sppp *);
    390  1.191  yamaguch static void sppp_pap_tlu(struct sppp *);
    391  1.191  yamaguch static void sppp_pap_scr(struct sppp *);
    392  1.201  yamaguch static void sppp_pap_scr(struct sppp *);
    393  1.191  yamaguch 
    394  1.191  yamaguch static void sppp_chap_input(struct sppp *, struct mbuf *);
    395  1.191  yamaguch static void sppp_chap_init(struct sppp *);
    396  1.198  yamaguch static void sppp_chap_open(struct sppp *, void *);
    397  1.191  yamaguch static void sppp_chap_tlu(struct sppp *);
    398  1.191  yamaguch static void sppp_chap_scr(struct sppp *);
    399  1.201  yamaguch static void sppp_chap_rcv_challenge_event(struct sppp *, void *);
    400  1.191  yamaguch 
    401  1.191  yamaguch static const char *sppp_auth_type_name(u_short, u_char);
    402  1.191  yamaguch static const char *sppp_cp_type_name(u_char);
    403  1.191  yamaguch static const char *sppp_dotted_quad(uint32_t);
    404  1.191  yamaguch static const char *sppp_ipcp_opt_name(u_char);
    405    1.9    itojun #ifdef INET6
    406  1.191  yamaguch static const char *sppp_ipv6cp_opt_name(u_char);
    407    1.9    itojun #endif
    408  1.191  yamaguch static const char *sppp_lcp_opt_name(u_char);
    409  1.191  yamaguch static const char *sppp_phase_name(int);
    410  1.191  yamaguch static const char *sppp_proto_name(u_short);
    411  1.191  yamaguch static const char *sppp_state_name(int);
    412  1.191  yamaguch static int sppp_params(struct sppp *, u_long, void *);
    413   1.79  christos #ifdef INET
    414  1.191  yamaguch static void sppp_get_ip_addrs(struct sppp *, uint32_t *, uint32_t *, uint32_t *);
    415  1.191  yamaguch static void sppp_set_ip_addrs_work(struct work *, struct sppp *);
    416  1.191  yamaguch static void sppp_set_ip_addrs(struct sppp *);
    417  1.191  yamaguch static void sppp_clear_ip_addrs_work(struct work *, struct sppp *);
    418  1.191  yamaguch static void sppp_clear_ip_addrs(struct sppp *);
    419  1.191  yamaguch static void sppp_update_ip_addrs_work(struct work *, void *);
    420  1.191  yamaguch #endif
    421  1.191  yamaguch static void sppp_keepalive(void *);
    422  1.191  yamaguch static void sppp_phase_network(struct sppp *);
    423  1.191  yamaguch static void sppp_print_bytes(const u_char *, u_short);
    424  1.191  yamaguch static void sppp_print_string(const char *, u_short);
    425    1.9    itojun #ifdef INET6
    426  1.191  yamaguch static void sppp_get_ip6_addrs(struct sppp *, struct in6_addr *,
    427  1.191  yamaguch 				struct in6_addr *, struct in6_addr *);
    428    1.9    itojun #ifdef IPV6CP_MYIFID_DYN
    429  1.191  yamaguch static void sppp_set_ip6_addr(struct sppp *, const struct in6_addr *);
    430  1.191  yamaguch static void sppp_gen_ip6_addr(struct sppp *, const struct in6_addr *);
    431    1.9    itojun #endif
    432  1.191  yamaguch static void sppp_suggest_ip6_addr(struct sppp *, struct in6_addr *);
    433    1.9    itojun #endif
    434    1.3  explorer 
    435  1.170  knakahar static void sppp_notify_up(struct sppp *);
    436  1.170  knakahar static void sppp_notify_down(struct sppp *);
    437  1.170  knakahar static void sppp_notify_tls_wlocked(struct sppp *);
    438  1.170  knakahar static void sppp_notify_tlf_wlocked(struct sppp *);
    439  1.171  knakahar #ifdef INET6
    440  1.170  knakahar static void sppp_notify_con_wlocked(struct sppp *);
    441  1.171  knakahar #endif
    442  1.170  knakahar static void sppp_notify_con(struct sppp *);
    443  1.170  knakahar 
    444  1.170  knakahar static void sppp_notify_chg_wlocked(struct sppp *);
    445  1.170  knakahar 
    446  1.170  knakahar 
    447    1.3  explorer /* our control protocol descriptors */
    448    1.3  explorer static const struct cp lcp = {
    449    1.3  explorer 	PPP_LCP, IDX_LCP, CP_LCP, "lcp",
    450  1.202  yamaguch 	sppp_lcp_up, sppp_lcp_down, sppp_lcp_open, sppp_close_event,
    451  1.202  yamaguch 	sppp_to_event, sppp_lcp_RCR, sppp_lcp_RCN_rej, sppp_lcp_RCN_nak,
    452    1.3  explorer 	sppp_lcp_tlu, sppp_lcp_tld, sppp_lcp_tls, sppp_lcp_tlf,
    453  1.194  yamaguch 	sppp_lcp_scr, sppp_sca_scn
    454    1.3  explorer };
    455    1.3  explorer 
    456    1.3  explorer static const struct cp ipcp = {
    457   1.19    itojun 	PPP_IPCP, IDX_IPCP,
    458   1.19    itojun #ifdef INET
    459   1.19    itojun 	CP_NCP,	/*don't run IPCP if there's no IPv4 support*/
    460   1.19    itojun #else
    461   1.19    itojun 	0,
    462   1.19    itojun #endif
    463   1.19    itojun 	"ipcp",
    464  1.202  yamaguch 	sppp_up_event, sppp_down_event, sppp_ipcp_open, sppp_ipcp_close,
    465  1.202  yamaguch 	sppp_to_event, sppp_ipcp_RCR, sppp_ipcp_RCN_rej, sppp_ipcp_RCN_nak,
    466  1.202  yamaguch 	sppp_ipcp_tlu, sppp_null, sppp_tls, sppp_tlf,
    467  1.194  yamaguch 	sppp_ipcp_scr, sppp_sca_scn
    468    1.3  explorer };
    469    1.3  explorer 
    470    1.9    itojun static const struct cp ipv6cp = {
    471    1.9    itojun 	PPP_IPV6CP, IDX_IPV6CP,
    472    1.9    itojun #ifdef INET6	/*don't run IPv6CP if there's no IPv6 support*/
    473    1.9    itojun 	CP_NCP,
    474    1.9    itojun #else
    475    1.9    itojun 	0,
    476    1.9    itojun #endif
    477    1.9    itojun 	"ipv6cp",
    478  1.202  yamaguch 	sppp_up_event, sppp_down_event, sppp_ipv6cp_open, sppp_close_event,
    479  1.202  yamaguch 	sppp_to_event, sppp_ipv6cp_RCR, sppp_ipv6cp_RCN_rej, sppp_ipv6cp_RCN_nak,
    480  1.202  yamaguch 	sppp_ipv6cp_tlu, sppp_null, sppp_tls, sppp_tlf,
    481  1.194  yamaguch 	sppp_ipv6cp_scr, sppp_sca_scn
    482    1.9    itojun };
    483    1.9    itojun 
    484    1.3  explorer static const struct cp pap = {
    485    1.3  explorer 	PPP_PAP, IDX_PAP, CP_AUTH, "pap",
    486  1.202  yamaguch 	sppp_up_event, sppp_down_event, sppp_open_event, sppp_close_event,
    487  1.202  yamaguch 	sppp_to_event, 0, 0, 0,
    488  1.202  yamaguch 	sppp_pap_tlu, sppp_null, sppp_tls, sppp_tlf,
    489  1.202  yamaguch 	sppp_pap_scr, sppp_auth_sca_scn
    490    1.3  explorer };
    491    1.3  explorer 
    492    1.3  explorer static const struct cp chap = {
    493    1.3  explorer 	PPP_CHAP, IDX_CHAP, CP_AUTH, "chap",
    494  1.202  yamaguch 	sppp_up_event, sppp_down_event, sppp_chap_open, sppp_close_event,
    495  1.202  yamaguch 	sppp_auth_to_event, 0, 0, 0,
    496  1.202  yamaguch 	sppp_chap_tlu, sppp_null, sppp_tls, sppp_tlf,
    497  1.202  yamaguch 	sppp_chap_scr, sppp_auth_sca_scn
    498    1.3  explorer };
    499    1.3  explorer 
    500    1.3  explorer static const struct cp *cps[IDX_COUNT] = {
    501    1.3  explorer 	&lcp,			/* IDX_LCP */
    502    1.3  explorer 	&ipcp,			/* IDX_IPCP */
    503    1.9    itojun 	&ipv6cp,		/* IDX_IPV6CP */
    504    1.3  explorer 	&pap,			/* IDX_PAP */
    505    1.3  explorer 	&chap,			/* IDX_CHAP */
    506    1.3  explorer };
    507    1.1  explorer 
    508  1.149     joerg static void
    509  1.149     joerg sppp_change_phase(struct sppp *sp, int phase)
    510  1.149     joerg {
    511  1.149     joerg 	STDDCL;
    512  1.149     joerg 
    513  1.170  knakahar 	KASSERT(SPPP_WLOCKED(sp));
    514  1.163  knakahar 
    515  1.149     joerg 	if (sp->pp_phase == phase)
    516  1.149     joerg 		return;
    517  1.149     joerg 
    518  1.149     joerg 	sp->pp_phase = phase;
    519  1.149     joerg 
    520  1.149     joerg 	if (phase == SPPP_PHASE_NETWORK)
    521  1.149     joerg 		if_link_state_change(ifp, LINK_STATE_UP);
    522  1.149     joerg 	else
    523  1.149     joerg 		if_link_state_change(ifp, LINK_STATE_DOWN);
    524  1.149     joerg 
    525  1.149     joerg 	if (debug)
    526  1.149     joerg 	{
    527  1.149     joerg 		log(LOG_INFO, "%s: phase %s\n", ifp->if_xname,
    528  1.149     joerg 			sppp_phase_name(sp->pp_phase));
    529  1.149     joerg 	}
    530  1.149     joerg }
    531    1.1  explorer 
    532   1.42  jdolecek /*
    533    1.3  explorer  * Exported functions, comprising our interface to the lower layer.
    534    1.1  explorer  */
    535    1.1  explorer 
    536    1.1  explorer /*
    537    1.1  explorer  * Process the received packet.
    538    1.1  explorer  */
    539    1.3  explorer void
    540    1.3  explorer sppp_input(struct ifnet *ifp, struct mbuf *m)
    541    1.1  explorer {
    542   1.22    martin 	struct ppp_header *h = NULL;
    543  1.129     rmind 	pktqueue_t *pktq = NULL;
    544  1.129     rmind 	struct ifqueue *inq = NULL;
    545  1.109      matt 	uint16_t protocol;
    546    1.3  explorer 	struct sppp *sp = (struct sppp *)ifp;
    547    1.3  explorer 	int debug = ifp->if_flags & IFF_DEBUG;
    548  1.128   msaitoh 	int isr = 0;
    549    1.1  explorer 
    550  1.170  knakahar 	SPPP_LOCK(sp, RW_READER);
    551  1.163  knakahar 
    552   1.68    martin 	if (ifp->if_flags & IFF_UP) {
    553   1.22    martin 		/* Count received bytes, add hardware framing */
    554  1.185   thorpej 		if_statadd(ifp, if_ibytes, m->m_pkthdr.len + sp->pp_framebytes);
    555   1.68    martin 		/* Note time of last receive */
    556   1.92    kardel 		sp->pp_last_receive = time_uptime;
    557   1.68    martin 	}
    558    1.1  explorer 
    559    1.1  explorer 	if (m->m_pkthdr.len <= PPP_HEADER_LEN) {
    560    1.1  explorer 		/* Too small packet, drop it. */
    561    1.3  explorer 		if (debug)
    562    1.3  explorer 			log(LOG_DEBUG,
    563   1.64    itojun 			    "%s: input packet is too small, %d bytes\n",
    564   1.64    itojun 			    ifp->if_xname, m->m_pkthdr.len);
    565    1.3  explorer 	  drop:
    566  1.185   thorpej 		if_statadd2(ifp, if_ierrors, 1, if_iqdrops, 1);
    567   1.59    itojun 		m_freem(m);
    568  1.170  knakahar 		SPPP_UNLOCK(sp);
    569    1.1  explorer 		return;
    570    1.1  explorer 	}
    571    1.1  explorer 
    572   1.22    martin 	if (sp->pp_flags & PP_NOFRAMING) {
    573   1.30      ross 		memcpy(&protocol, mtod(m, void *), 2);
    574   1.22    martin 		protocol = ntohs(protocol);
    575   1.22    martin 		m_adj(m, 2);
    576   1.22    martin 	} else {
    577    1.1  explorer 
    578   1.22    martin 		/* Get PPP header. */
    579   1.59    itojun 		h = mtod(m, struct ppp_header *);
    580   1.59    itojun 		m_adj(m, PPP_HEADER_LEN);
    581   1.22    martin 
    582   1.22    martin 		switch (h->address) {
    583   1.22    martin 		case PPP_ALLSTATIONS:
    584   1.22    martin 			if (h->control != PPP_UI)
    585   1.22    martin 				goto invalid;
    586   1.22    martin 			if (sp->pp_flags & PP_CISCO) {
    587   1.22    martin 				if (debug)
    588   1.22    martin 					log(LOG_DEBUG,
    589   1.64    itojun 					    "%s: PPP packet in Cisco mode "
    590   1.22    martin 					    "<addr=0x%x ctrl=0x%x proto=0x%x>\n",
    591   1.64    itojun 					    ifp->if_xname,
    592   1.22    martin 					    h->address, h->control, ntohs(h->protocol));
    593   1.22    martin 				goto drop;
    594   1.22    martin 			}
    595   1.22    martin 			break;
    596   1.22    martin 		case CISCO_MULTICAST:
    597   1.22    martin 		case CISCO_UNICAST:
    598   1.22    martin 			/* Don't check the control field here (RFC 1547). */
    599   1.22    martin 			if (! (sp->pp_flags & PP_CISCO)) {
    600   1.22    martin 				if (debug)
    601   1.22    martin 					log(LOG_DEBUG,
    602   1.64    itojun 					    "%s: Cisco packet in PPP mode "
    603   1.22    martin 					    "<addr=0x%x ctrl=0x%x proto=0x%x>\n",
    604   1.64    itojun 					    ifp->if_xname,
    605   1.22    martin 					    h->address, h->control, ntohs(h->protocol));
    606   1.22    martin 				goto drop;
    607   1.22    martin 			}
    608   1.59    itojun 			switch (ntohs(h->protocol)) {
    609   1.22    martin 			default:
    610  1.185   thorpej 				if_statinc(ifp, if_noproto);
    611   1.22    martin 				goto invalid;
    612   1.22    martin 			case CISCO_KEEPALIVE:
    613  1.170  knakahar 				SPPP_UNLOCK(sp);
    614   1.59    itojun 				sppp_cisco_input((struct sppp *) ifp, m);
    615   1.59    itojun 				m_freem(m);
    616   1.22    martin 				return;
    617    1.1  explorer #ifdef INET
    618   1.22    martin 			case ETHERTYPE_IP:
    619  1.129     rmind 				pktq = ip_pktq;
    620   1.22    martin 				break;
    621    1.1  explorer #endif
    622    1.8    itojun #ifdef INET6
    623   1.22    martin 			case ETHERTYPE_IPV6:
    624  1.129     rmind 				pktq = ip6_pktq;
    625   1.22    martin 				break;
    626    1.8    itojun #endif
    627    1.1  explorer 			}
    628   1.22    martin 			goto queue_pkt;
    629   1.22    martin 		default:        /* Invalid PPP packet. */
    630   1.22    martin 		  invalid:
    631    1.3  explorer 			if (debug)
    632    1.3  explorer 				log(LOG_DEBUG,
    633   1.64    itojun 				    "%s: invalid input packet "
    634    1.3  explorer 				    "<addr=0x%x ctrl=0x%x proto=0x%x>\n",
    635   1.64    itojun 				    ifp->if_xname,
    636    1.3  explorer 				    h->address, h->control, ntohs(h->protocol));
    637    1.1  explorer 			goto drop;
    638    1.1  explorer 		}
    639   1.59    itojun 		protocol = ntohs(h->protocol);
    640   1.22    martin 	}
    641   1.22    martin 
    642   1.22    martin 	switch (protocol) {
    643   1.22    martin 	default:
    644  1.192  yamaguch 		if (sp->scp[IDX_LCP].state == STATE_OPENED) {
    645  1.109      matt 			uint16_t prot = htons(protocol);
    646  1.170  knakahar 
    647  1.170  knakahar 			SPPP_UPGRADE(sp);
    648   1.59    itojun 			sppp_cp_send(sp, PPP_LCP, PROTO_REJ,
    649  1.192  yamaguch 			    ++sp->scp[IDX_LCP].seq, m->m_pkthdr.len + 2,
    650   1.22    martin 			    &prot);
    651  1.170  knakahar 			SPPP_DOWNGRADE(sp);
    652   1.22    martin 		}
    653   1.22    martin 		if (debug)
    654   1.22    martin 			log(LOG_DEBUG,
    655   1.64    itojun 			    "%s: invalid input protocol "
    656   1.64    itojun 			    "<proto=0x%x>\n", ifp->if_xname, ntohs(protocol));
    657  1.185   thorpej 		if_statinc(ifp, if_noproto);
    658   1.22    martin 		goto drop;
    659   1.22    martin 	case PPP_LCP:
    660  1.170  knakahar 		SPPP_UNLOCK(sp);
    661   1.22    martin 		sppp_cp_input(&lcp, sp, m);
    662   1.59    itojun 		m_freem(m);
    663   1.22    martin 		return;
    664   1.22    martin 	case PPP_PAP:
    665  1.170  knakahar 		SPPP_UNLOCK(sp);
    666  1.163  knakahar 		if (sp->pp_phase >= SPPP_PHASE_AUTHENTICATE) {
    667   1.22    martin 			sppp_pap_input(sp, m);
    668  1.170  knakahar 		}
    669   1.59    itojun 		m_freem(m);
    670   1.22    martin 		return;
    671   1.22    martin 	case PPP_CHAP:
    672  1.170  knakahar 		SPPP_UNLOCK(sp);
    673  1.163  knakahar 		if (sp->pp_phase >= SPPP_PHASE_AUTHENTICATE) {
    674   1.22    martin 			sppp_chap_input(sp, m);
    675  1.170  knakahar 		}
    676   1.59    itojun 		m_freem(m);
    677   1.22    martin 		return;
    678    1.1  explorer #ifdef INET
    679   1.22    martin 	case PPP_IPCP:
    680  1.170  knakahar 		SPPP_UNLOCK(sp);
    681  1.163  knakahar 		if (sp->pp_phase == SPPP_PHASE_NETWORK) {
    682   1.22    martin 			sppp_cp_input(&ipcp, sp, m);
    683  1.170  knakahar 		}
    684   1.59    itojun 		m_freem(m);
    685   1.22    martin 		return;
    686   1.22    martin 	case PPP_IP:
    687  1.192  yamaguch 		if (sp->scp[IDX_IPCP].state == STATE_OPENED) {
    688   1.92    kardel 			sp->pp_last_activity = time_uptime;
    689  1.129     rmind 			pktq = ip_pktq;
    690   1.22    martin 		}
    691   1.22    martin 		break;
    692    1.1  explorer #endif
    693    1.8    itojun #ifdef INET6
    694   1.22    martin 	case PPP_IPV6CP:
    695  1.170  knakahar 		SPPP_UNLOCK(sp);
    696  1.163  knakahar 		if (sp->pp_phase == SPPP_PHASE_NETWORK) {
    697   1.22    martin 			sppp_cp_input(&ipv6cp, sp, m);
    698  1.170  knakahar 		}
    699   1.59    itojun 		m_freem(m);
    700   1.22    martin 		return;
    701   1.22    martin 
    702   1.22    martin 	case PPP_IPV6:
    703  1.192  yamaguch 		if (sp->scp[IDX_IPV6CP].state == STATE_OPENED) {
    704   1.92    kardel 			sp->pp_last_activity = time_uptime;
    705  1.129     rmind 			pktq = ip6_pktq;
    706   1.22    martin 		}
    707   1.22    martin 		break;
    708    1.8    itojun #endif
    709    1.1  explorer 	}
    710    1.1  explorer 
    711   1.22    martin queue_pkt:
    712  1.130     rmind 	if ((ifp->if_flags & IFF_UP) == 0 || (!inq && !pktq)) {
    713    1.1  explorer 		goto drop;
    714  1.130     rmind 	}
    715    1.1  explorer 
    716    1.1  explorer 	/* Check queue. */
    717  1.129     rmind 	if (__predict_true(pktq)) {
    718  1.129     rmind 		if (__predict_false(!pktq_enqueue(pktq, m, 0))) {
    719  1.129     rmind 			goto drop;
    720  1.129     rmind 		}
    721  1.170  knakahar 		SPPP_UNLOCK(sp);
    722  1.129     rmind 		return;
    723  1.129     rmind 	}
    724  1.129     rmind 
    725  1.170  knakahar 	SPPP_UNLOCK(sp);
    726  1.170  knakahar 
    727  1.155     ozaki 	IFQ_LOCK(inq);
    728   1.59    itojun 	if (IF_QFULL(inq)) {
    729    1.1  explorer 		/* Queue overflow. */
    730    1.3  explorer 		IF_DROP(inq);
    731  1.155     ozaki 		IFQ_UNLOCK(inq);
    732    1.3  explorer 		if (debug)
    733   1.64    itojun 			log(LOG_DEBUG, "%s: protocol queue overflow\n",
    734   1.64    itojun 				ifp->if_xname);
    735  1.170  knakahar 
    736  1.170  knakahar 		SPPP_LOCK(sp, RW_READER);
    737    1.1  explorer 		goto drop;
    738    1.1  explorer 	}
    739    1.3  explorer 	IF_ENQUEUE(inq, m);
    740  1.155     ozaki 	IFQ_UNLOCK(inq);
    741  1.128   msaitoh 	schednetisr(isr);
    742    1.1  explorer }
    743    1.1  explorer 
    744    1.1  explorer /*
    745    1.1  explorer  * Enqueue transmit packet.
    746    1.1  explorer  */
    747    1.1  explorer static int
    748    1.3  explorer sppp_output(struct ifnet *ifp, struct mbuf *m,
    749  1.141     ozaki     const struct sockaddr *dst, const struct rtentry *rt)
    750    1.1  explorer {
    751   1.59    itojun 	struct sppp *sp = (struct sppp *) ifp;
    752   1.22    martin 	struct ppp_header *h = NULL;
    753  1.163  knakahar #ifndef SPPPSUBR_MPSAFE
    754   1.15   thorpej 	struct ifqueue *ifq = NULL;		/* XXX */
    755  1.163  knakahar #endif
    756   1.83  christos 	int s, error = 0;
    757  1.109      matt 	uint16_t protocol;
    758  1.169     ozaki 	size_t pktlen;
    759    1.3  explorer 
    760   1.23   thorpej 	s = splnet();
    761  1.170  knakahar 	SPPP_LOCK(sp, RW_READER);
    762    1.1  explorer 
    763   1.92    kardel 	sp->pp_last_activity = time_uptime;
    764   1.38    martin 
    765    1.3  explorer 	if ((ifp->if_flags & IFF_UP) == 0 ||
    766    1.3  explorer 	    (ifp->if_flags & (IFF_RUNNING | IFF_AUTO)) == 0) {
    767  1.170  knakahar 		SPPP_UNLOCK(sp);
    768  1.170  knakahar 		splx(s);
    769  1.170  knakahar 
    770   1.59    itojun 		m_freem(m);
    771  1.170  knakahar 
    772    1.1  explorer 		return (ENETDOWN);
    773    1.1  explorer 	}
    774    1.1  explorer 
    775    1.3  explorer 	if ((ifp->if_flags & (IFF_RUNNING | IFF_AUTO)) == IFF_AUTO) {
    776    1.3  explorer 		/*
    777    1.3  explorer 		 * Interface is not yet running, but auto-dial.  Need
    778    1.3  explorer 		 * to start LCP for it.
    779    1.3  explorer 		 */
    780    1.3  explorer 		ifp->if_flags |= IFF_RUNNING;
    781  1.198  yamaguch 		sppp_wq_add(sp->wq_cp, &sp->scp[IDX_LCP].work_open);
    782    1.3  explorer 	}
    783    1.3  explorer 
    784   1.15   thorpej 	/*
    785   1.15   thorpej 	 * If the queueing discipline needs packet classification,
    786   1.15   thorpej 	 * do it before prepending link headers.
    787   1.15   thorpej 	 */
    788  1.136  knakahar 	IFQ_CLASSIFY(&ifp->if_snd, m, dst->sa_family);
    789    1.3  explorer 
    790    1.1  explorer #ifdef INET
    791   1.48    itojun 	if (dst->sa_family == AF_INET) {
    792   1.48    itojun 		struct ip *ip = NULL;
    793  1.163  knakahar #ifndef SPPPSUBR_MPSAFE
    794   1.48    itojun 		struct tcphdr *th = NULL;
    795  1.163  knakahar #endif
    796   1.48    itojun 
    797   1.48    itojun 		if (m->m_len >= sizeof(struct ip)) {
    798   1.59    itojun 			ip = mtod(m, struct ip *);
    799  1.163  knakahar #ifndef SPPPSUBR_MPSAFE
    800   1.48    itojun 			if (ip->ip_p == IPPROTO_TCP &&
    801   1.48    itojun 			    m->m_len >= sizeof(struct ip) + (ip->ip_hl << 2) +
    802   1.48    itojun 			    sizeof(struct tcphdr)) {
    803   1.48    itojun 				th = (struct tcphdr *)
    804  1.104  christos 				    ((char *)ip + (ip->ip_hl << 2));
    805   1.48    itojun 			}
    806  1.163  knakahar #endif
    807   1.48    itojun 		} else
    808   1.48    itojun 			ip = NULL;
    809    1.1  explorer 
    810    1.3  explorer 		/*
    811    1.3  explorer 		 * When using dynamic local IP address assignment by using
    812    1.3  explorer 		 * 0.0.0.0 as a local address, the first TCP session will
    813    1.3  explorer 		 * not connect because the local TCP checksum is computed
    814    1.3  explorer 		 * using 0.0.0.0 which will later become our real IP address
    815    1.3  explorer 		 * so the TCP checksum computed at the remote end will
    816    1.3  explorer 		 * become invalid. So we
    817    1.3  explorer 		 * - don't let packets with src ip addr 0 thru
    818    1.3  explorer 		 * - we flag TCP packets with src ip 0 as an error
    819   1.82     perry 		 */
    820   1.48    itojun 		if (ip && ip->ip_src.s_addr == INADDR_ANY) {
    821  1.109      matt 			uint8_t proto = ip->ip_p;
    822   1.49      yamt 
    823  1.170  knakahar 			SPPP_UNLOCK(sp);
    824  1.170  knakahar 			splx(s);
    825  1.170  knakahar 
    826    1.3  explorer 			m_freem(m);
    827   1.49      yamt 			if (proto == IPPROTO_TCP)
    828   1.58    itojun 				return (EADDRNOTAVAIL);
    829    1.3  explorer 			else
    830   1.58    itojun 				return (0);
    831    1.3  explorer 		}
    832   1.82     perry 
    833  1.163  knakahar #ifndef SPPPSUBR_MPSAFE
    834    1.3  explorer 		/*
    835    1.3  explorer 		 * Put low delay, telnet, rlogin and ftp control packets
    836    1.3  explorer 		 * in front of the queue.
    837    1.3  explorer 		 */
    838   1.48    itojun 		if (!IF_QFULL(&sp->pp_fastq) &&
    839   1.48    itojun 		    ((ip && (ip->ip_tos & IPTOS_LOWDELAY)) ||
    840   1.48    itojun 		     (th && (INTERACTIVE(ntohs(th->th_sport)) ||
    841   1.48    itojun 		      INTERACTIVE(ntohs(th->th_dport))))))
    842    1.1  explorer 			ifq = &sp->pp_fastq;
    843  1.163  knakahar #endif /* !SPPPSUBR_MPSAFE */
    844    1.1  explorer 	}
    845    1.1  explorer #endif
    846    1.1  explorer 
    847    1.5    itojun #ifdef INET6
    848    1.5    itojun 	if (dst->sa_family == AF_INET6) {
    849    1.5    itojun 		/* XXX do something tricky here? */
    850    1.5    itojun 	}
    851    1.5    itojun #endif
    852    1.5    itojun 
    853   1.22    martin 	if ((sp->pp_flags & PP_NOFRAMING) == 0) {
    854   1.22    martin 		/*
    855   1.22    martin 		 * Prepend general data packet PPP header. For now, IP only.
    856   1.22    martin 		 */
    857   1.59    itojun 		M_PREPEND(m, PPP_HEADER_LEN, M_DONTWAIT);
    858   1.22    martin 		if (! m) {
    859   1.22    martin 			if (ifp->if_flags & IFF_DEBUG)
    860   1.64    itojun 				log(LOG_DEBUG, "%s: no memory for transmit header\n",
    861   1.64    itojun 					ifp->if_xname);
    862  1.185   thorpej 			if_statinc(ifp, if_oerrors);
    863  1.170  knakahar 			SPPP_UNLOCK(sp);
    864   1.59    itojun 			splx(s);
    865   1.22    martin 			return (ENOBUFS);
    866   1.22    martin 		}
    867   1.22    martin 		/*
    868   1.22    martin 		 * May want to check size of packet
    869   1.22    martin 		 * (albeit due to the implementation it's always enough)
    870   1.22    martin 		 */
    871   1.59    itojun 		h = mtod(m, struct ppp_header *);
    872   1.22    martin 		if (sp->pp_flags & PP_CISCO) {
    873   1.22    martin 			h->address = CISCO_UNICAST;        /* unicast address */
    874   1.22    martin 			h->control = 0;
    875   1.22    martin 		} else {
    876   1.22    martin 			h->address = PPP_ALLSTATIONS;        /* broadcast address */
    877   1.22    martin 			h->control = PPP_UI;                 /* Unnumbered Info */
    878   1.22    martin 		}
    879    1.1  explorer 	}
    880    1.1  explorer 
    881    1.1  explorer 	switch (dst->sa_family) {
    882    1.1  explorer #ifdef INET
    883    1.1  explorer 	case AF_INET:   /* Internet Protocol */
    884    1.1  explorer 		if (sp->pp_flags & PP_CISCO)
    885   1.59    itojun 			protocol = htons(ETHERTYPE_IP);
    886    1.1  explorer 		else {
    887    1.3  explorer 			/*
    888    1.3  explorer 			 * Don't choke with an ENETDOWN early.  It's
    889    1.3  explorer 			 * possible that we just started dialing out,
    890    1.3  explorer 			 * so don't drop the packet immediately.  If
    891    1.3  explorer 			 * we notice that we run out of buffer space
    892    1.3  explorer 			 * below, we will however remember that we are
    893    1.3  explorer 			 * not ready to carry IP packets, and return
    894    1.3  explorer 			 * ENETDOWN, as opposed to ENOBUFS.
    895    1.3  explorer 			 */
    896   1.22    martin 			protocol = htons(PPP_IP);
    897  1.192  yamaguch 			if (sp->scp[IDX_IPCP].state != STATE_OPENED)
    898   1.83  christos 				error = ENETDOWN;
    899    1.5    itojun 		}
    900    1.5    itojun 		break;
    901    1.5    itojun #endif
    902    1.5    itojun #ifdef INET6
    903    1.5    itojun 	case AF_INET6:   /* Internet Protocol version 6 */
    904    1.5    itojun 		if (sp->pp_flags & PP_CISCO)
    905   1.59    itojun 			protocol = htons(ETHERTYPE_IPV6);
    906    1.5    itojun 		else {
    907    1.5    itojun 			/*
    908    1.5    itojun 			 * Don't choke with an ENETDOWN early.  It's
    909    1.5    itojun 			 * possible that we just started dialing out,
    910    1.5    itojun 			 * so don't drop the packet immediately.  If
    911    1.5    itojun 			 * we notice that we run out of buffer space
    912    1.5    itojun 			 * below, we will however remember that we are
    913    1.5    itojun 			 * not ready to carry IP packets, and return
    914    1.5    itojun 			 * ENETDOWN, as opposed to ENOBUFS.
    915    1.5    itojun 			 */
    916   1.22    martin 			protocol = htons(PPP_IPV6);
    917  1.192  yamaguch 			if (sp->scp[IDX_IPV6CP].state != STATE_OPENED)
    918   1.83  christos 				error = ENETDOWN;
    919    1.1  explorer 		}
    920    1.1  explorer 		break;
    921    1.1  explorer #endif
    922    1.1  explorer 	default:
    923   1.59    itojun 		m_freem(m);
    924  1.185   thorpej 		if_statinc(ifp, if_oerrors);
    925  1.170  knakahar 		SPPP_UNLOCK(sp);
    926   1.59    itojun 		splx(s);
    927    1.1  explorer 		return (EAFNOSUPPORT);
    928    1.1  explorer 	}
    929    1.1  explorer 
    930   1.22    martin 	if (sp->pp_flags & PP_NOFRAMING) {
    931   1.59    itojun 		M_PREPEND(m, 2, M_DONTWAIT);
    932   1.22    martin 		if (m == NULL) {
    933   1.22    martin 			if (ifp->if_flags & IFF_DEBUG)
    934   1.64    itojun 				log(LOG_DEBUG, "%s: no memory for transmit header\n",
    935   1.64    itojun 					ifp->if_xname);
    936  1.185   thorpej 			if_statinc(ifp, if_oerrors);
    937  1.170  knakahar 			SPPP_UNLOCK(sp);
    938   1.59    itojun 			splx(s);
    939   1.22    martin 			return (ENOBUFS);
    940   1.22    martin 		}
    941  1.109      matt 		*mtod(m, uint16_t *) = protocol;
    942   1.22    martin 	} else {
    943   1.22    martin 		h->protocol = protocol;
    944   1.22    martin 	}
    945   1.22    martin 
    946  1.169     ozaki 	pktlen = m->m_pkthdr.len;
    947  1.163  knakahar #ifdef SPPPSUBR_MPSAFE
    948  1.170  knakahar 	SPPP_UNLOCK(sp);
    949  1.163  knakahar 	error = if_transmit_lock(ifp, m);
    950  1.170  knakahar 	SPPP_LOCK(sp, RW_READER);
    951  1.163  knakahar 	if (error == 0)
    952  1.185   thorpej 		if_statadd(ifp, if_obytes, pktlen + sp->pp_framebytes);
    953  1.163  knakahar #else /* !SPPPSUBR_MPSAFE */
    954  1.136  knakahar 	error = ifq_enqueue2(ifp, ifq, m);
    955    1.1  explorer 
    956   1.83  christos 	if (error == 0) {
    957   1.83  christos 		/*
    958   1.83  christos 		 * Count output packets and bytes.
    959   1.83  christos 		 * The packet length includes header + additional hardware
    960   1.83  christos 		 * framing according to RFC 1333.
    961   1.83  christos 		 */
    962  1.163  knakahar 		if (!(ifp->if_flags & IFF_OACTIVE)) {
    963  1.170  knakahar 			SPPP_UNLOCK(sp);
    964  1.143  knakahar 			if_start_lock(ifp);
    965  1.170  knakahar 			SPPP_LOCK(sp, RW_READER);
    966  1.163  knakahar 		}
    967  1.185   thorpej 		if_statadd(ifp, if_obytes, pktlen + sp->pp_framebytes);
    968   1.83  christos 	}
    969  1.163  knakahar #endif /* !SPPPSUBR_MPSAFE */
    970  1.170  knakahar 	SPPP_UNLOCK(sp);
    971   1.59    itojun 	splx(s);
    972   1.83  christos 	return error;
    973    1.1  explorer }
    974    1.1  explorer 
    975    1.3  explorer void
    976    1.3  explorer sppp_attach(struct ifnet *ifp)
    977    1.1  explorer {
    978   1.59    itojun 	struct sppp *sp = (struct sppp *) ifp;
    979  1.198  yamaguch 	char xnamebuf[MAXCOMLEN];
    980    1.1  explorer 
    981    1.1  explorer 	/* Initialize keepalive handler. */
    982    1.7   thorpej 	if (! spppq) {
    983  1.172  knakahar 		callout_init(&keepalive_ch, CALLOUT_MPSAFE);
    984   1.57    martin 		callout_reset(&keepalive_ch, hz * LCP_KEEPALIVE_INTERVAL, sppp_keepalive, NULL);
    985    1.7   thorpej 	}
    986    1.1  explorer 
    987  1.163  knakahar 	if (! spppq_lock)
    988  1.163  knakahar 		spppq_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_SOFTNET);
    989  1.163  knakahar 
    990    1.1  explorer 	sp->pp_if.if_type = IFT_PPP;
    991    1.1  explorer 	sp->pp_if.if_output = sppp_output;
    992    1.1  explorer 	sp->pp_fastq.ifq_maxlen = 32;
    993    1.3  explorer 	sp->pp_cpq.ifq_maxlen = 20;
    994    1.1  explorer 	sp->pp_loopcnt = 0;
    995    1.1  explorer 	sp->pp_alivecnt = 0;
    996   1.38    martin 	sp->pp_last_activity = 0;
    997   1.68    martin 	sp->pp_last_receive = 0;
    998   1.68    martin 	sp->pp_maxalive = DEFAULT_MAXALIVECNT;
    999   1.68    martin 	sp->pp_max_noreceive = DEFAULT_NORECV_TIME;
   1000   1.38    martin 	sp->pp_idle_timeout = 0;
   1001   1.39    martin 	sp->pp_max_auth_fail = DEFAULT_MAX_AUTH_FAILURES;
   1002   1.36    martin 	sp->pp_phase = SPPP_PHASE_DEAD;
   1003  1.170  knakahar 	sp->pp_up = sppp_notify_up;
   1004  1.170  knakahar 	sp->pp_down = sppp_notify_down;
   1005  1.199  yamaguch 	sppp_wq_set(&sp->work_ifdown, sppp_ifdown, NULL);
   1006  1.192  yamaguch 	memset(sp->scp, 0, sizeof(sp->scp));
   1007  1.170  knakahar 	rw_init(&sp->pp_lock);
   1008    1.3  explorer 
   1009   1.20   thorpej 	if_alloc_sadl(ifp);
   1010   1.20   thorpej 
   1011  1.150       roy 	/* Lets not beat about the bush, we know we're down. */
   1012  1.150       roy 	ifp->if_link_state = LINK_STATE_DOWN;
   1013  1.150       roy 
   1014  1.198  yamaguch 	snprintf(xnamebuf, sizeof(xnamebuf), "%s.wq_cp", ifp->if_xname);
   1015  1.198  yamaguch 	sp->wq_cp = sppp_wq_create(sp, xnamebuf,
   1016  1.198  yamaguch 	    PRI_SOFTNET, IPL_SOFTNET, WQ_MPSAFE);
   1017  1.198  yamaguch 
   1018   1.36    martin 	memset(&sp->myauth, 0, sizeof sp->myauth);
   1019   1.36    martin 	memset(&sp->hisauth, 0, sizeof sp->hisauth);
   1020  1.170  knakahar 	SPPP_LOCK(sp, RW_WRITER);
   1021    1.3  explorer 	sppp_lcp_init(sp);
   1022    1.3  explorer 	sppp_ipcp_init(sp);
   1023    1.9    itojun 	sppp_ipv6cp_init(sp);
   1024    1.3  explorer 	sppp_pap_init(sp);
   1025    1.3  explorer 	sppp_chap_init(sp);
   1026  1.170  knakahar 	SPPP_UNLOCK(sp);
   1027  1.200  yamaguch 
   1028  1.200  yamaguch 	SPPPQ_LOCK();
   1029  1.200  yamaguch 	/* Insert new entry into the keepalive list. */
   1030  1.200  yamaguch 	sp->pp_next = spppq;
   1031  1.200  yamaguch 	spppq = sp;
   1032  1.200  yamaguch 	SPPPQ_UNLOCK();
   1033    1.1  explorer }
   1034    1.1  explorer 
   1035    1.3  explorer void
   1036    1.3  explorer sppp_detach(struct ifnet *ifp)
   1037    1.1  explorer {
   1038   1.59    itojun 	struct sppp **q, *p, *sp = (struct sppp *) ifp;
   1039    1.1  explorer 
   1040    1.1  explorer 	/* Remove the entry from the keepalive list. */
   1041  1.163  knakahar 	SPPPQ_LOCK();
   1042    1.1  explorer 	for (q = &spppq; (p = *q); q = &p->pp_next)
   1043    1.1  explorer 		if (p == sp) {
   1044    1.1  explorer 			*q = p->pp_next;
   1045    1.1  explorer 			break;
   1046    1.1  explorer 		}
   1047  1.170  knakahar 	SPPPQ_UNLOCK();
   1048  1.170  knakahar 
   1049  1.170  knakahar 	if (! spppq) {
   1050  1.170  knakahar 		/* Stop keepalive handler. */
   1051  1.170  knakahar 		callout_stop(&keepalive_ch);
   1052  1.170  knakahar 		mutex_obj_free(spppq_lock);
   1053  1.170  knakahar 		spppq_lock = NULL;
   1054  1.170  knakahar 	}
   1055  1.170  knakahar 
   1056  1.170  knakahar 	SPPP_LOCK(sp, RW_WRITER);
   1057    1.1  explorer 
   1058  1.161  knakahar 	/* to avoid workqueue enqueued */
   1059  1.161  knakahar 	atomic_swap_uint(&sp->ipcp.update_addrs_enqueued, 1);
   1060  1.178     ozaki 	workqueue_wait(sp->ipcp.update_addrs_wq, &sp->ipcp.update_addrs_wk);
   1061  1.161  knakahar 	workqueue_destroy(sp->ipcp.update_addrs_wq);
   1062  1.161  knakahar 	pcq_destroy(sp->ipcp.update_addrs_q);
   1063  1.159  knakahar 
   1064  1.198  yamaguch 	sppp_cp_fini(&lcp, sp);
   1065  1.198  yamaguch 	sppp_cp_fini(&ipcp, sp);
   1066  1.201  yamaguch 	sppp_cp_fini(&pap, sp);
   1067  1.201  yamaguch 	sppp_cp_fini(&chap, sp);
   1068  1.113    martin #ifdef INET6
   1069  1.198  yamaguch 	sppp_cp_fini(&ipv6cp, sp);
   1070  1.113    martin #endif
   1071  1.198  yamaguch 	sppp_wq_destroy(sp, sp->wq_cp);
   1072   1.20   thorpej 
   1073   1.36    martin 	/* free authentication info */
   1074   1.36    martin 	if (sp->myauth.name) free(sp->myauth.name, M_DEVBUF);
   1075   1.36    martin 	if (sp->myauth.secret) free(sp->myauth.secret, M_DEVBUF);
   1076   1.36    martin 	if (sp->hisauth.name) free(sp->hisauth.name, M_DEVBUF);
   1077   1.36    martin 	if (sp->hisauth.secret) free(sp->hisauth.secret, M_DEVBUF);
   1078  1.170  knakahar 	SPPP_UNLOCK(sp);
   1079  1.170  knakahar 	rw_destroy(&sp->pp_lock);
   1080    1.1  explorer }
   1081    1.1  explorer 
   1082    1.1  explorer /*
   1083    1.1  explorer  * Flush the interface output queue.
   1084    1.1  explorer  */
   1085    1.3  explorer void
   1086    1.3  explorer sppp_flush(struct ifnet *ifp)
   1087    1.1  explorer {
   1088   1.59    itojun 	struct sppp *sp = (struct sppp *) ifp;
   1089    1.1  explorer 
   1090  1.170  knakahar 	SPPP_LOCK(sp, RW_WRITER);
   1091   1.59    itojun 	IFQ_PURGE(&sp->pp_if.if_snd);
   1092   1.59    itojun 	IF_PURGE(&sp->pp_fastq);
   1093   1.59    itojun 	IF_PURGE(&sp->pp_cpq);
   1094  1.170  knakahar 	SPPP_UNLOCK(sp);
   1095    1.1  explorer }
   1096    1.1  explorer 
   1097    1.1  explorer /*
   1098    1.1  explorer  * Check if the output queue is empty.
   1099    1.1  explorer  */
   1100    1.1  explorer int
   1101    1.3  explorer sppp_isempty(struct ifnet *ifp)
   1102    1.1  explorer {
   1103   1.59    itojun 	struct sppp *sp = (struct sppp *) ifp;
   1104    1.3  explorer 	int empty, s;
   1105    1.1  explorer 
   1106   1.23   thorpej 	s = splnet();
   1107  1.170  knakahar 	SPPP_LOCK(sp, RW_READER);
   1108   1.44    martin 	empty = IF_IS_EMPTY(&sp->pp_fastq) && IF_IS_EMPTY(&sp->pp_cpq) &&
   1109   1.44    martin 		IFQ_IS_EMPTY(&sp->pp_if.if_snd);
   1110  1.170  knakahar 	SPPP_UNLOCK(sp);
   1111    1.3  explorer 	splx(s);
   1112    1.1  explorer 	return (empty);
   1113    1.1  explorer }
   1114    1.1  explorer 
   1115    1.1  explorer /*
   1116    1.1  explorer  * Get next packet to send.
   1117    1.1  explorer  */
   1118    1.3  explorer struct mbuf *
   1119    1.3  explorer sppp_dequeue(struct ifnet *ifp)
   1120    1.1  explorer {
   1121   1.59    itojun 	struct sppp *sp = (struct sppp *) ifp;
   1122    1.1  explorer 	struct mbuf *m;
   1123    1.3  explorer 	int s;
   1124    1.1  explorer 
   1125   1.23   thorpej 	s = splnet();
   1126  1.170  knakahar 	SPPP_LOCK(sp, RW_WRITER);
   1127    1.3  explorer 	/*
   1128    1.3  explorer 	 * Process only the control protocol queue until we have at
   1129    1.3  explorer 	 * least one NCP open.
   1130    1.3  explorer 	 *
   1131    1.3  explorer 	 * Do always serve all three queues in Cisco mode.
   1132    1.3  explorer 	 */
   1133    1.3  explorer 	IF_DEQUEUE(&sp->pp_cpq, m);
   1134    1.3  explorer 	if (m == NULL &&
   1135  1.202  yamaguch 	    (sppp_cp_check(sp, CP_NCP) || (sp->pp_flags & PP_CISCO) != 0)) {
   1136    1.3  explorer 		IF_DEQUEUE(&sp->pp_fastq, m);
   1137    1.3  explorer 		if (m == NULL)
   1138   1.67    martin 			IFQ_DEQUEUE(&sp->pp_if.if_snd, m);
   1139    1.3  explorer 	}
   1140  1.170  knakahar 	SPPP_UNLOCK(sp);
   1141    1.3  explorer 	splx(s);
   1142    1.3  explorer 	return m;
   1143    1.1  explorer }
   1144    1.1  explorer 
   1145    1.1  explorer /*
   1146    1.3  explorer  * Process an ioctl request.  Called on low priority level.
   1147    1.1  explorer  */
   1148    1.3  explorer int
   1149    1.3  explorer sppp_ioctl(struct ifnet *ifp, u_long cmd, void *data)
   1150    1.1  explorer {
   1151  1.100      elad 	struct lwp *l = curlwp;	/* XXX */
   1152   1.59    itojun 	struct ifreq *ifr = (struct ifreq *) data;
   1153  1.132       roy 	struct ifaddr *ifa = (struct ifaddr *) data;
   1154   1.59    itojun 	struct sppp *sp = (struct sppp *) ifp;
   1155  1.184   msaitoh 	int s, error=0, going_up, going_down;
   1156  1.184   msaitoh 	u_short newmode;
   1157    1.1  explorer 
   1158   1.23   thorpej 	s = splnet();
   1159    1.3  explorer 	switch (cmd) {
   1160  1.115    dyoung 	case SIOCINITIFADDR:
   1161  1.132       roy 		ifa->ifa_rtrequest = p2p_rtrequest;
   1162    1.1  explorer 		break;
   1163    1.3  explorer 
   1164    1.3  explorer 	case SIOCSIFFLAGS:
   1165  1.115    dyoung 		if ((error = ifioctl_common(ifp, cmd, data)) != 0)
   1166  1.115    dyoung 			break;
   1167  1.163  knakahar 
   1168  1.170  knakahar 		SPPP_LOCK(sp, RW_WRITER);
   1169    1.3  explorer 		going_up = ifp->if_flags & IFF_UP &&
   1170    1.3  explorer 			(ifp->if_flags & IFF_RUNNING) == 0;
   1171    1.3  explorer 		going_down = (ifp->if_flags & IFF_UP) == 0 &&
   1172    1.3  explorer 			ifp->if_flags & IFF_RUNNING;
   1173    1.3  explorer 		newmode = ifp->if_flags & (IFF_AUTO | IFF_PASSIVE);
   1174    1.3  explorer 		if (newmode == (IFF_AUTO | IFF_PASSIVE)) {
   1175    1.3  explorer 			/* sanity */
   1176    1.3  explorer 			newmode = IFF_PASSIVE;
   1177    1.3  explorer 			ifp->if_flags &= ~IFF_AUTO;
   1178    1.1  explorer 		}
   1179    1.1  explorer 
   1180  1.163  knakahar 		if (going_up || going_down) {
   1181  1.198  yamaguch 			sp->lcp.reestablish = false;
   1182  1.198  yamaguch 			sppp_wq_add(sp->wq_cp, &sp->scp[IDX_LCP].work_close);
   1183  1.163  knakahar 		}
   1184    1.3  explorer 		if (going_up && newmode == 0) {
   1185    1.3  explorer 			/* neither auto-dial nor passive */
   1186    1.3  explorer 			ifp->if_flags |= IFF_RUNNING;
   1187  1.198  yamaguch 			if (!(sp->pp_flags & PP_CISCO)) {
   1188  1.198  yamaguch 				sppp_wq_add(sp->wq_cp,
   1189  1.198  yamaguch 				    &sp->scp[IDX_LCP].work_open);
   1190  1.198  yamaguch 			}
   1191    1.3  explorer 		} else if (going_down) {
   1192  1.170  knakahar 			SPPP_UNLOCK(sp);
   1193    1.3  explorer 			sppp_flush(ifp);
   1194  1.170  knakahar 			SPPP_LOCK(sp, RW_WRITER);
   1195  1.170  knakahar 
   1196    1.3  explorer 			ifp->if_flags &= ~IFF_RUNNING;
   1197    1.1  explorer 		}
   1198  1.170  knakahar 		SPPP_UNLOCK(sp);
   1199    1.3  explorer 		break;
   1200    1.3  explorer 
   1201    1.3  explorer 	case SIOCSIFMTU:
   1202  1.105       scw 		if (ifr->ifr_mtu < PPP_MINMRU ||
   1203  1.105       scw 		    ifr->ifr_mtu > sp->lcp.their_mru) {
   1204   1.61  kristerw 			error = EINVAL;
   1205   1.61  kristerw 			break;
   1206   1.61  kristerw 		}
   1207  1.108    dyoung 		/*FALLTHROUGH*/
   1208    1.3  explorer 	case SIOCGIFMTU:
   1209  1.108    dyoung 		if ((error = ifioctl_common(ifp, cmd, data)) == ENETRESET)
   1210  1.108    dyoung 			error = 0;
   1211    1.1  explorer 		break;
   1212    1.3  explorer 	case SIOCADDMULTI:
   1213    1.3  explorer 	case SIOCDELMULTI:
   1214    1.1  explorer 		break;
   1215    1.1  explorer 
   1216   1.36    martin 	case SPPPSETAUTHCFG:
   1217   1.36    martin 	case SPPPSETLCPCFG:
   1218   1.38    martin 	case SPPPSETIDLETO:
   1219   1.39    martin 	case SPPPSETAUTHFAILURE:
   1220   1.45    martin 	case SPPPSETDNSOPTS:
   1221   1.68    martin 	case SPPPSETKEEPALIVE:
   1222  1.120  jmcneill #if defined(COMPAT_50) || defined(MODULAR)
   1223  1.120  jmcneill 	case __SPPPSETIDLETO50:
   1224  1.120  jmcneill 	case __SPPPSETKEEPALIVE50:
   1225  1.120  jmcneill #endif /* COMPAT_50 || MODULAR */
   1226  1.100      elad 		error = kauth_authorize_network(l->l_cred,
   1227  1.100      elad 		    KAUTH_NETWORK_INTERFACE,
   1228  1.100      elad 		    KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp, (void *)cmd,
   1229  1.100      elad 		    NULL);
   1230  1.100      elad 		if (error)
   1231  1.100      elad 			break;
   1232  1.100      elad 		error = sppp_params(sp, cmd, data);
   1233  1.100      elad 		break;
   1234   1.36    martin 
   1235  1.100      elad 	case SPPPGETAUTHCFG:
   1236  1.100      elad 	case SPPPGETLCPCFG:
   1237  1.100      elad 	case SPPPGETAUTHFAILURES:
   1238  1.100      elad 		error = kauth_authorize_network(l->l_cred,
   1239  1.100      elad 		    KAUTH_NETWORK_INTERFACE,
   1240  1.100      elad 		    KAUTH_REQ_NETWORK_INTERFACE_GETPRIV, ifp, (void *)cmd,
   1241  1.100      elad 		    NULL);
   1242  1.100      elad 		if (error)
   1243   1.36    martin 			break;
   1244  1.100      elad 		error = sppp_params(sp, cmd, data);
   1245  1.100      elad 		break;
   1246  1.100      elad 
   1247   1.36    martin 	case SPPPGETSTATUS:
   1248   1.75    martin 	case SPPPGETSTATUSNCP:
   1249   1.38    martin 	case SPPPGETIDLETO:
   1250   1.45    martin 	case SPPPGETDNSOPTS:
   1251   1.45    martin 	case SPPPGETDNSADDRS:
   1252   1.68    martin 	case SPPPGETKEEPALIVE:
   1253  1.120  jmcneill #if defined(COMPAT_50) || defined(MODULAR)
   1254  1.120  jmcneill 	case __SPPPGETIDLETO50:
   1255  1.120  jmcneill 	case __SPPPGETKEEPALIVE50:
   1256  1.120  jmcneill #endif /* COMPAT_50 || MODULAR */
   1257   1.42  jdolecek 		error = sppp_params(sp, cmd, data);
   1258    1.1  explorer 		break;
   1259    1.3  explorer 
   1260    1.3  explorer 	default:
   1261  1.115    dyoung 		error = ifioctl_common(ifp, cmd, data);
   1262  1.115    dyoung 		break;
   1263    1.1  explorer 	}
   1264    1.3  explorer 	splx(s);
   1265   1.42  jdolecek 	return (error);
   1266    1.1  explorer }
   1267    1.1  explorer 
   1268    1.3  explorer 
   1269   1.42  jdolecek /*
   1270    1.3  explorer  * Cisco framing implementation.
   1271    1.3  explorer  */
   1272    1.3  explorer 
   1273    1.1  explorer /*
   1274    1.1  explorer  * Handle incoming Cisco keepalive protocol packets.
   1275    1.1  explorer  */
   1276    1.3  explorer static void
   1277    1.3  explorer sppp_cisco_input(struct sppp *sp, struct mbuf *m)
   1278    1.1  explorer {
   1279    1.3  explorer 	STDDCL;
   1280    1.1  explorer 	struct cisco_packet *h;
   1281   1.79  christos #ifdef INET
   1282  1.109      matt 	uint32_t me, mymask = 0;	/* XXX: GCC */
   1283   1.79  christos #endif
   1284    1.1  explorer 
   1285  1.170  knakahar 	SPPP_LOCK(sp, RW_WRITER);
   1286  1.163  knakahar 
   1287    1.1  explorer 	if (m->m_pkthdr.len < CISCO_PACKET_LEN) {
   1288    1.3  explorer 		if (debug)
   1289    1.3  explorer 			log(LOG_DEBUG,
   1290   1.64    itojun 			    "%s: cisco invalid packet length: %d bytes\n",
   1291   1.64    itojun 			    ifp->if_xname, m->m_pkthdr.len);
   1292  1.170  knakahar 		SPPP_UNLOCK(sp);
   1293    1.1  explorer 		return;
   1294    1.1  explorer 	}
   1295   1.59    itojun 	h = mtod(m, struct cisco_packet *);
   1296    1.3  explorer 	if (debug)
   1297    1.3  explorer 		log(LOG_DEBUG,
   1298   1.64    itojun 		    "%s: cisco input: %d bytes "
   1299   1.30      ross 		    "<0x%x 0x%x 0x%x 0x%x 0x%x-0x%x>\n",
   1300   1.64    itojun 		    ifp->if_xname, m->m_pkthdr.len,
   1301   1.80    itojun 		    ntohl(h->type), h->par1, h->par2, (u_int)h->rel,
   1302    1.3  explorer 		    (u_int)h->time0, (u_int)h->time1);
   1303   1.80    itojun 	switch (ntohl(h->type)) {
   1304    1.1  explorer 	default:
   1305    1.3  explorer 		if (debug)
   1306   1.64    itojun 			addlog("%s: cisco unknown packet type: 0x%x\n",
   1307   1.80    itojun 			       ifp->if_xname, ntohl(h->type));
   1308    1.1  explorer 		break;
   1309    1.1  explorer 	case CISCO_ADDR_REPLY:
   1310    1.1  explorer 		/* Reply on address request, ignore */
   1311    1.1  explorer 		break;
   1312    1.1  explorer 	case CISCO_KEEPALIVE_REQ:
   1313    1.1  explorer 		sp->pp_alivecnt = 0;
   1314  1.192  yamaguch 		sp->scp[IDX_LCP].rseq = ntohl(h->par1);
   1315  1.192  yamaguch 		if (sp->scp[IDX_LCP].seq == sp->scp[IDX_LCP].rseq) {
   1316    1.1  explorer 			/* Local and remote sequence numbers are equal.
   1317    1.1  explorer 			 * Probably, the line is in loopback mode. */
   1318   1.68    martin 			if (sp->pp_loopcnt >= LOOPALIVECNT) {
   1319   1.64    itojun 				printf ("%s: loopback\n",
   1320   1.64    itojun 					ifp->if_xname);
   1321    1.1  explorer 				sp->pp_loopcnt = 0;
   1322    1.1  explorer 				if (ifp->if_flags & IFF_UP) {
   1323  1.170  knakahar 					SPPP_UNLOCK(sp);
   1324   1.59    itojun 					if_down(ifp);
   1325  1.170  knakahar 					SPPP_LOCK(sp, RW_WRITER);
   1326  1.170  knakahar 
   1327   1.59    itojun 					IF_PURGE(&sp->pp_cpq);
   1328    1.1  explorer 				}
   1329    1.1  explorer 			}
   1330    1.1  explorer 			++sp->pp_loopcnt;
   1331    1.1  explorer 
   1332    1.1  explorer 			/* Generate new local sequence number */
   1333  1.192  yamaguch 			sp->scp[IDX_LCP].seq = cprng_fast32();
   1334    1.1  explorer 			break;
   1335    1.1  explorer 		}
   1336    1.3  explorer 		sp->pp_loopcnt = 0;
   1337    1.1  explorer 		if (! (ifp->if_flags & IFF_UP) &&
   1338    1.1  explorer 		    (ifp->if_flags & IFF_RUNNING)) {
   1339  1.170  knakahar 			SPPP_UNLOCK(sp);
   1340    1.3  explorer 			if_up(ifp);
   1341  1.170  knakahar 			SPPP_LOCK(sp, RW_WRITER);
   1342    1.1  explorer 		}
   1343    1.1  explorer 		break;
   1344    1.1  explorer 	case CISCO_ADDR_REQ:
   1345   1.79  christos #ifdef INET
   1346    1.3  explorer 		sppp_get_ip_addrs(sp, &me, 0, &mymask);
   1347    1.3  explorer 		if (me != 0L)
   1348    1.3  explorer 			sppp_cisco_send(sp, CISCO_ADDR_REPLY, me, mymask);
   1349   1.79  christos #endif
   1350    1.1  explorer 		break;
   1351    1.1  explorer 	}
   1352  1.170  knakahar 	SPPP_UNLOCK(sp);
   1353    1.1  explorer }
   1354    1.1  explorer 
   1355    1.1  explorer /*
   1356    1.3  explorer  * Send Cisco keepalive packet.
   1357    1.1  explorer  */
   1358    1.1  explorer static void
   1359   1.30      ross sppp_cisco_send(struct sppp *sp, int type, int32_t par1, int32_t par2)
   1360    1.1  explorer {
   1361    1.3  explorer 	STDDCL;
   1362    1.1  explorer 	struct ppp_header *h;
   1363    1.3  explorer 	struct cisco_packet *ch;
   1364    1.1  explorer 	struct mbuf *m;
   1365  1.109      matt 	uint32_t t;
   1366    1.1  explorer 
   1367  1.170  knakahar 	KASSERT(SPPP_WLOCKED(sp));
   1368  1.163  knakahar 
   1369   1.92    kardel 	t = time_uptime * 1000;
   1370   1.59    itojun 	MGETHDR(m, M_DONTWAIT, MT_DATA);
   1371    1.1  explorer 	if (! m)
   1372    1.1  explorer 		return;
   1373    1.3  explorer 	m->m_pkthdr.len = m->m_len = PPP_HEADER_LEN + CISCO_PACKET_LEN;
   1374  1.142     ozaki 	m_reset_rcvif(m);
   1375    1.1  explorer 
   1376   1.59    itojun 	h = mtod(m, struct ppp_header *);
   1377    1.3  explorer 	h->address = CISCO_MULTICAST;
   1378    1.3  explorer 	h->control = 0;
   1379   1.59    itojun 	h->protocol = htons(CISCO_KEEPALIVE);
   1380    1.3  explorer 
   1381   1.59    itojun 	ch = (struct cisco_packet *)(h + 1);
   1382   1.59    itojun 	ch->type = htonl(type);
   1383   1.59    itojun 	ch->par1 = htonl(par1);
   1384   1.59    itojun 	ch->par2 = htonl(par2);
   1385    1.3  explorer 	ch->rel = -1;
   1386    1.3  explorer 
   1387   1.59    itojun 	ch->time0 = htons((u_short)(t >> 16));
   1388   1.59    itojun 	ch->time1 = htons((u_short) t);
   1389    1.1  explorer 
   1390    1.3  explorer 	if (debug)
   1391    1.3  explorer 		log(LOG_DEBUG,
   1392   1.64    itojun 		    "%s: cisco output: <0x%x 0x%x 0x%x 0x%x 0x%x-0x%x>\n",
   1393   1.80    itojun 			ifp->if_xname, ntohl(ch->type), ch->par1,
   1394   1.30      ross 			ch->par2, (u_int)ch->rel, (u_int)ch->time0,
   1395   1.30      ross 			(u_int)ch->time1);
   1396    1.1  explorer 
   1397   1.59    itojun 	if (IF_QFULL(&sp->pp_cpq)) {
   1398   1.59    itojun 		IF_DROP(&sp->pp_fastq);
   1399   1.59    itojun 		IF_DROP(&ifp->if_snd);
   1400   1.59    itojun 		m_freem(m);
   1401  1.185   thorpej 		if_statinc(ifp, if_oerrors);
   1402   1.50      yamt 		return;
   1403  1.169     ozaki 	}
   1404  1.169     ozaki 
   1405  1.185   thorpej 	if_statadd(ifp, if_obytes, m->m_pkthdr.len + sp->pp_framebytes);
   1406  1.169     ozaki 	IF_ENQUEUE(&sp->pp_cpq, m);
   1407  1.169     ozaki 
   1408  1.163  knakahar 	if (! (ifp->if_flags & IFF_OACTIVE)) {
   1409  1.170  knakahar 		SPPP_UNLOCK(sp);
   1410  1.143  knakahar 		if_start_lock(ifp);
   1411  1.170  knakahar 		SPPP_LOCK(sp, RW_WRITER);
   1412  1.163  knakahar 	}
   1413    1.1  explorer }
   1414    1.1  explorer 
   1415   1.42  jdolecek /*
   1416    1.3  explorer  * PPP protocol implementation.
   1417    1.3  explorer  */
   1418    1.3  explorer 
   1419    1.1  explorer /*
   1420    1.3  explorer  * Send PPP control protocol packet.
   1421    1.1  explorer  */
   1422    1.1  explorer static void
   1423    1.3  explorer sppp_cp_send(struct sppp *sp, u_short proto, u_char type,
   1424    1.3  explorer 	     u_char ident, u_short len, void *data)
   1425    1.1  explorer {
   1426    1.3  explorer 	STDDCL;
   1427    1.3  explorer 	struct lcp_header *lh;
   1428    1.1  explorer 	struct mbuf *m;
   1429   1.22    martin 	size_t pkthdrlen;
   1430    1.1  explorer 
   1431  1.170  knakahar 	KASSERT(SPPP_WLOCKED(sp));
   1432  1.163  knakahar 
   1433   1.22    martin 	pkthdrlen = (sp->pp_flags & PP_NOFRAMING) ? 2 : PPP_HEADER_LEN;
   1434   1.22    martin 
   1435   1.22    martin 	if (len > MHLEN - pkthdrlen - LCP_HEADER_LEN)
   1436   1.22    martin 		len = MHLEN - pkthdrlen - LCP_HEADER_LEN;
   1437   1.59    itojun 	MGETHDR(m, M_DONTWAIT, MT_DATA);
   1438  1.170  knakahar 	if (! m) {
   1439    1.1  explorer 		return;
   1440  1.170  knakahar 	}
   1441   1.22    martin 	m->m_pkthdr.len = m->m_len = pkthdrlen + LCP_HEADER_LEN + len;
   1442  1.142     ozaki 	m_reset_rcvif(m);
   1443    1.1  explorer 
   1444   1.22    martin 	if (sp->pp_flags & PP_NOFRAMING) {
   1445  1.109      matt 		*mtod(m, uint16_t *) = htons(proto);
   1446  1.109      matt 		lh = (struct lcp_header *)(mtod(m, uint8_t *) + 2);
   1447   1.22    martin 	} else {
   1448   1.22    martin 		struct ppp_header *h;
   1449   1.59    itojun 		h = mtod(m, struct ppp_header *);
   1450   1.22    martin 		h->address = PPP_ALLSTATIONS;        /* broadcast address */
   1451   1.22    martin 		h->control = PPP_UI;                 /* Unnumbered Info */
   1452   1.59    itojun 		h->protocol = htons(proto);         /* Link Control Protocol */
   1453   1.59    itojun 		lh = (struct lcp_header *)(h + 1);
   1454   1.22    martin 	}
   1455    1.3  explorer 	lh->type = type;
   1456    1.3  explorer 	lh->ident = ident;
   1457   1.59    itojun 	lh->len = htons(LCP_HEADER_LEN + len);
   1458    1.3  explorer 	if (len)
   1459  1.140  christos 		memcpy(lh + 1, data, len);
   1460    1.1  explorer 
   1461    1.3  explorer 	if (debug) {
   1462   1.64    itojun 		log(LOG_DEBUG, "%s: %s output <%s id=0x%x len=%d",
   1463   1.64    itojun 		    ifp->if_xname,
   1464    1.3  explorer 		    sppp_proto_name(proto),
   1465   1.59    itojun 		    sppp_cp_type_name(lh->type), lh->ident, ntohs(lh->len));
   1466    1.3  explorer 		if (len)
   1467   1.59    itojun 			sppp_print_bytes((u_char *)(lh + 1), len);
   1468    1.3  explorer 		addlog(">\n");
   1469    1.3  explorer 	}
   1470   1.59    itojun 	if (IF_QFULL(&sp->pp_cpq)) {
   1471   1.59    itojun 		IF_DROP(&sp->pp_fastq);
   1472   1.59    itojun 		IF_DROP(&ifp->if_snd);
   1473   1.59    itojun 		m_freem(m);
   1474  1.185   thorpej 		if_statinc(ifp, if_oerrors);
   1475   1.50      yamt 		return;
   1476  1.169     ozaki 	}
   1477  1.169     ozaki 
   1478  1.185   thorpej 	if_statadd(ifp, if_obytes, m->m_pkthdr.len + sp->pp_framebytes);
   1479  1.169     ozaki 	IF_ENQUEUE(&sp->pp_cpq, m);
   1480  1.163  knakahar 
   1481  1.170  knakahar 
   1482  1.163  knakahar 	if (! (ifp->if_flags & IFF_OACTIVE)) {
   1483  1.170  knakahar 		SPPP_UNLOCK(sp);
   1484  1.143  knakahar 		if_start_lock(ifp);
   1485  1.170  knakahar 		SPPP_LOCK(sp, RW_WRITER);
   1486  1.163  knakahar 	}
   1487    1.1  explorer }
   1488    1.1  explorer 
   1489  1.198  yamaguch static void
   1490  1.198  yamaguch sppp_cp_to_lcp(void *xsp)
   1491  1.198  yamaguch {
   1492  1.198  yamaguch 	struct sppp *sp = xsp;
   1493  1.198  yamaguch 
   1494  1.198  yamaguch 	sppp_wq_add(sp->wq_cp, &sp->scp[IDX_LCP].work_to);
   1495  1.198  yamaguch }
   1496  1.198  yamaguch 
   1497  1.198  yamaguch static void
   1498  1.198  yamaguch sppp_cp_to_ipcp(void *xsp)
   1499  1.198  yamaguch {
   1500  1.198  yamaguch 	struct sppp *sp = xsp;
   1501  1.198  yamaguch 
   1502  1.198  yamaguch 	sppp_wq_add(sp->wq_cp, &sp->scp[IDX_IPCP].work_to);
   1503  1.198  yamaguch }
   1504  1.198  yamaguch 
   1505  1.198  yamaguch static void
   1506  1.198  yamaguch sppp_cp_to_ipv6cp(void *xsp)
   1507  1.198  yamaguch {
   1508  1.198  yamaguch 	struct sppp *sp = xsp;
   1509  1.198  yamaguch 
   1510  1.198  yamaguch 	sppp_wq_add(sp->wq_cp, &sp->scp[IDX_IPV6CP].work_to);
   1511  1.198  yamaguch }
   1512  1.198  yamaguch 
   1513  1.198  yamaguch static void
   1514  1.201  yamaguch sppp_cp_to_pap(void *xsp)
   1515  1.201  yamaguch {
   1516  1.201  yamaguch 	struct sppp *sp = xsp;
   1517  1.201  yamaguch 
   1518  1.201  yamaguch 	sppp_wq_add(sp->wq_cp, &sp->scp[IDX_PAP].work_to);
   1519  1.201  yamaguch }
   1520  1.201  yamaguch 
   1521  1.201  yamaguch static void
   1522  1.201  yamaguch sppp_cp_to_chap(void *xsp)
   1523  1.201  yamaguch {
   1524  1.201  yamaguch 	struct sppp *sp = xsp;
   1525  1.201  yamaguch 
   1526  1.201  yamaguch 	sppp_wq_add(sp->wq_cp, &sp->scp[IDX_CHAP].work_to);
   1527  1.201  yamaguch }
   1528  1.201  yamaguch 
   1529  1.201  yamaguch 
   1530  1.201  yamaguch static void
   1531  1.198  yamaguch sppp_cp_init(const struct cp *cp, struct sppp *sp)
   1532  1.198  yamaguch {
   1533  1.198  yamaguch 	struct sppp_cp *scp;
   1534  1.198  yamaguch 	typedef void (*sppp_co_cb_t)(void *);
   1535  1.198  yamaguch 	static const sppp_co_cb_t to_cb[IDX_COUNT] = {
   1536  1.198  yamaguch 		[IDX_LCP] = sppp_cp_to_lcp,
   1537  1.198  yamaguch 		[IDX_IPCP] = sppp_cp_to_ipcp,
   1538  1.198  yamaguch 		[IDX_IPV6CP] = sppp_cp_to_ipv6cp,
   1539  1.201  yamaguch 		[IDX_PAP] = sppp_cp_to_pap,
   1540  1.201  yamaguch 		[IDX_CHAP] = sppp_cp_to_chap,
   1541  1.198  yamaguch 	};
   1542  1.198  yamaguch 
   1543  1.198  yamaguch 	scp = &sp->scp[cp->protoidx];
   1544  1.198  yamaguch 
   1545  1.198  yamaguch 	SPPP_WQ_SET(&scp->work_up, cp->Up, cp);
   1546  1.198  yamaguch 	SPPP_WQ_SET(&scp->work_down, cp->Down,  cp);
   1547  1.198  yamaguch 	SPPP_WQ_SET(&scp->work_open, cp->Open, cp);
   1548  1.198  yamaguch 	SPPP_WQ_SET(&scp->work_close, cp->Close, cp);
   1549  1.198  yamaguch 	SPPP_WQ_SET(&scp->work_to, cp->TO, cp);
   1550  1.198  yamaguch 	SPPP_WQ_SET(&scp->work_rcr, sppp_rcr_event, cp);
   1551  1.198  yamaguch 	SPPP_WQ_SET(&scp->work_rca, sppp_rca_event, cp);
   1552  1.198  yamaguch 	SPPP_WQ_SET(&scp->work_rcn, sppp_rcn_event, cp);
   1553  1.198  yamaguch 	SPPP_WQ_SET(&scp->work_rtr, sppp_rtr_event, cp);
   1554  1.198  yamaguch 	SPPP_WQ_SET(&scp->work_rta, sppp_rta_event, cp);
   1555  1.198  yamaguch 	SPPP_WQ_SET(&scp->work_rxj, sppp_rxj_event, cp);
   1556  1.198  yamaguch 
   1557  1.198  yamaguch 	callout_init(&scp->ch, CALLOUT_MPSAFE);
   1558  1.198  yamaguch 	callout_setfunc(&scp->ch, to_cb[cp->protoidx], sp);
   1559  1.198  yamaguch }
   1560  1.198  yamaguch 
   1561  1.198  yamaguch static void
   1562  1.198  yamaguch sppp_cp_fini(const struct cp *cp, struct sppp *sp)
   1563  1.198  yamaguch {
   1564  1.198  yamaguch 	struct sppp_cp *scp;
   1565  1.198  yamaguch 	scp = &sp->scp[cp->protoidx];
   1566  1.198  yamaguch 
   1567  1.198  yamaguch 	sppp_wq_wait(sp->wq_cp, &scp->work_up);
   1568  1.198  yamaguch 	sppp_wq_wait(sp->wq_cp, &scp->work_down);
   1569  1.198  yamaguch 	sppp_wq_wait(sp->wq_cp, &scp->work_open);
   1570  1.198  yamaguch 	sppp_wq_wait(sp->wq_cp, &scp->work_close);
   1571  1.198  yamaguch 	sppp_wq_wait(sp->wq_cp, &scp->work_to);
   1572  1.198  yamaguch 	sppp_wq_wait(sp->wq_cp, &scp->work_rcr);
   1573  1.198  yamaguch 	sppp_wq_wait(sp->wq_cp, &scp->work_rca);
   1574  1.198  yamaguch 	sppp_wq_wait(sp->wq_cp, &scp->work_rcn);
   1575  1.198  yamaguch 	sppp_wq_wait(sp->wq_cp, &scp->work_rtr);
   1576  1.198  yamaguch 	sppp_wq_wait(sp->wq_cp, &scp->work_rta);
   1577  1.198  yamaguch 	sppp_wq_wait(sp->wq_cp, &scp->work_rxj);
   1578  1.198  yamaguch 
   1579  1.198  yamaguch 	callout_halt(&scp->ch, NULL);
   1580  1.198  yamaguch 	callout_destroy(&scp->ch);
   1581  1.198  yamaguch }
   1582  1.198  yamaguch 
   1583    1.1  explorer /*
   1584    1.3  explorer  * Handle incoming PPP control protocol packets.
   1585    1.1  explorer  */
   1586    1.1  explorer static void
   1587    1.3  explorer sppp_cp_input(const struct cp *cp, struct sppp *sp, struct mbuf *m)
   1588    1.1  explorer {
   1589    1.3  explorer 	STDDCL;
   1590    1.1  explorer 	struct lcp_header *h;
   1591   1.96   adrianp 	int printlen, len = m->m_pkthdr.len;
   1592    1.3  explorer 	int rv;
   1593    1.3  explorer 	u_char *p;
   1594  1.109      matt 	uint32_t u32;
   1595    1.1  explorer 
   1596  1.170  knakahar 	SPPP_LOCK(sp, RW_WRITER);
   1597  1.170  knakahar 
   1598    1.1  explorer 	if (len < 4) {
   1599    1.3  explorer 		if (debug)
   1600    1.3  explorer 			log(LOG_DEBUG,
   1601   1.64    itojun 			    "%s: %s invalid packet length: %d bytes\n",
   1602   1.64    itojun 			    ifp->if_xname, cp->name, len);
   1603  1.170  knakahar 		SPPP_UNLOCK(sp);
   1604    1.1  explorer 		return;
   1605    1.1  explorer 	}
   1606   1.59    itojun 	h = mtod(m, struct lcp_header *);
   1607    1.3  explorer 	if (debug) {
   1608   1.96   adrianp 		printlen = ntohs(h->len);
   1609    1.3  explorer 		log(LOG_DEBUG,
   1610   1.64    itojun 		    "%s: %s input(%s): <%s id=0x%x len=%d",
   1611   1.64    itojun 		    ifp->if_xname, cp->name,
   1612  1.192  yamaguch 		    sppp_state_name(sp->scp[cp->protoidx].state),
   1613   1.96   adrianp 		    sppp_cp_type_name(h->type), h->ident, printlen);
   1614   1.96   adrianp 		if (len < printlen)
   1615   1.96   adrianp 			printlen = len;
   1616   1.96   adrianp 		if (printlen > 4)
   1617   1.96   adrianp 			sppp_print_bytes((u_char *)(h + 1), printlen - 4);
   1618    1.3  explorer 		addlog(">\n");
   1619    1.1  explorer 	}
   1620   1.59    itojun 	if (len > ntohs(h->len))
   1621   1.59    itojun 		len = ntohs(h->len);
   1622    1.3  explorer 	p = (u_char *)(h + 1);
   1623    1.1  explorer 	switch (h->type) {
   1624    1.3  explorer 	case CONF_REQ:
   1625    1.1  explorer 		if (len < 4) {
   1626    1.3  explorer 			if (debug)
   1627   1.64    itojun 				addlog("%s: %s invalid conf-req length %d\n",
   1628   1.64    itojun 				       ifp->if_xname, cp->name,
   1629    1.3  explorer 				       len);
   1630  1.185   thorpej 			if_statinc(ifp, if_ierrors);
   1631    1.3  explorer 			break;
   1632    1.3  explorer 		}
   1633    1.3  explorer 		rv = (cp->RCR)(sp, h, len);
   1634   1.96   adrianp 		if (rv < 0) {
   1635   1.96   adrianp 			/* fatal error, shut down */
   1636   1.96   adrianp 			(cp->tld)(sp);
   1637  1.202  yamaguch 			lcp.tlf(&lcp, sp);
   1638  1.170  knakahar 			SPPP_UNLOCK(sp);
   1639   1.96   adrianp 			return;
   1640   1.96   adrianp 		}
   1641  1.194  yamaguch 		sp->scp[cp->protoidx].rconfid = h->ident;
   1642  1.198  yamaguch 		sppp_wq_add(sp->wq_cp, &sp->scp[cp->protoidx].work_rcr);
   1643    1.3  explorer 		break;
   1644    1.3  explorer 	case CONF_ACK:
   1645  1.192  yamaguch 		if (h->ident != sp->scp[cp->protoidx].confid) {
   1646    1.3  explorer 			if (debug)
   1647   1.64    itojun 				addlog("%s: %s id mismatch 0x%x != 0x%x\n",
   1648   1.64    itojun 				       ifp->if_xname, cp->name,
   1649  1.192  yamaguch 				       h->ident, sp->scp[cp->protoidx].confid);
   1650  1.185   thorpej 			if_statinc(ifp, if_ierrors);
   1651    1.3  explorer 			break;
   1652    1.3  explorer 		}
   1653  1.198  yamaguch 		sppp_wq_add(sp->wq_cp, &sp->scp[cp->protoidx].work_rca);
   1654    1.3  explorer 		break;
   1655    1.3  explorer 	case CONF_NAK:
   1656    1.3  explorer 	case CONF_REJ:
   1657  1.192  yamaguch 		if (h->ident != sp->scp[cp->protoidx].confid) {
   1658    1.3  explorer 			if (debug)
   1659   1.64    itojun 				addlog("%s: %s id mismatch 0x%x != 0x%x\n",
   1660   1.64    itojun 				       ifp->if_xname, cp->name,
   1661  1.192  yamaguch 				       h->ident, sp->scp[cp->protoidx].confid);
   1662  1.185   thorpej 			if_statinc(ifp, if_ierrors);
   1663    1.3  explorer 			break;
   1664    1.1  explorer 		}
   1665    1.3  explorer 		if (h->type == CONF_NAK)
   1666    1.3  explorer 			(cp->RCN_nak)(sp, h, len);
   1667    1.3  explorer 		else /* CONF_REJ */
   1668    1.3  explorer 			(cp->RCN_rej)(sp, h, len);
   1669    1.3  explorer 
   1670  1.198  yamaguch 		sppp_wq_add(sp->wq_cp, &sp->scp[cp->protoidx].work_rcn);
   1671    1.3  explorer 		break;
   1672    1.3  explorer 
   1673    1.3  explorer 	case TERM_REQ:
   1674  1.196  yamaguch 		sp->scp[cp->protoidx].rseq = h->ident;
   1675  1.198  yamaguch 		sppp_wq_add(sp->wq_cp, &sp->scp[cp->protoidx].work_rtr);
   1676    1.1  explorer 		break;
   1677    1.3  explorer 	case TERM_ACK:
   1678  1.198  yamaguch 		sppp_wq_add(sp->wq_cp, &sp->scp[cp->protoidx].work_rta);
   1679    1.1  explorer 		break;
   1680    1.3  explorer 	case CODE_REJ:
   1681    1.3  explorer 		/* XXX catastrophic rejects (RXJ-) aren't handled yet. */
   1682    1.3  explorer 		log(LOG_INFO,
   1683   1.64    itojun 		    "%s: %s: ignoring RXJ (%s) for code ?, "
   1684    1.3  explorer 		    "danger will robinson\n",
   1685   1.64    itojun 		    ifp->if_xname, cp->name,
   1686    1.9    itojun 		    sppp_cp_type_name(h->type));
   1687  1.198  yamaguch 		sppp_wq_add(sp->wq_cp, &sp->scp[cp->protoidx].work_rxj);
   1688    1.9    itojun 		break;
   1689    1.9    itojun 	case PROTO_REJ:
   1690    1.9    itojun 	    {
   1691    1.9    itojun 		int catastrophic;
   1692    1.9    itojun 		const struct cp *upper;
   1693    1.9    itojun 		int i;
   1694  1.109      matt 		uint16_t proto;
   1695    1.9    itojun 
   1696    1.9    itojun 		catastrophic = 0;
   1697    1.9    itojun 		upper = NULL;
   1698   1.30      ross 		proto = p[0] << 8 | p[1];
   1699    1.9    itojun 		for (i = 0; i < IDX_COUNT; i++) {
   1700    1.9    itojun 			if (cps[i]->proto == proto) {
   1701    1.9    itojun 				upper = cps[i];
   1702    1.9    itojun 				break;
   1703    1.9    itojun 			}
   1704    1.9    itojun 		}
   1705    1.9    itojun 		if (upper == NULL)
   1706    1.9    itojun 			catastrophic++;
   1707    1.9    itojun 
   1708   1.46    martin 		if (debug)
   1709   1.46    martin 			log(LOG_INFO,
   1710   1.64    itojun 			    "%s: %s: RXJ%c (%s) for proto 0x%x (%s/%s)\n",
   1711   1.64    itojun 			    ifp->if_xname, cp->name, catastrophic ? '-' : '+',
   1712   1.46    martin 			    sppp_cp_type_name(h->type), proto,
   1713   1.46    martin 			    upper ? upper->name : "unknown",
   1714  1.192  yamaguch 			    upper ? sppp_state_name(sp->scp[upper->protoidx].state) : "?");
   1715    1.9    itojun 
   1716    1.9    itojun 		/*
   1717    1.9    itojun 		 * if we got RXJ+ against conf-req, the peer does not implement
   1718    1.9    itojun 		 * this particular protocol type.  terminate the protocol.
   1719    1.9    itojun 		 */
   1720    1.9    itojun 		if (upper && !catastrophic) {
   1721  1.192  yamaguch 			if (sp->scp[upper->protoidx].state == STATE_REQ_SENT) {
   1722  1.198  yamaguch 				sppp_wq_add(sp->wq_cp,
   1723  1.198  yamaguch 				    &sp->scp[upper->protoidx].work_close);
   1724    1.9    itojun 				break;
   1725    1.9    itojun 			}
   1726    1.9    itojun 		}
   1727  1.198  yamaguch 		sppp_wq_add(sp->wq_cp, &sp->scp[cp->protoidx].work_rxj);
   1728    1.1  explorer 		break;
   1729    1.9    itojun 	    }
   1730    1.3  explorer 	case DISC_REQ:
   1731    1.3  explorer 		if (cp->proto != PPP_LCP)
   1732    1.3  explorer 			goto illegal;
   1733    1.3  explorer 		/* Discard the packet. */
   1734    1.3  explorer 		break;
   1735    1.3  explorer 	case ECHO_REQ:
   1736    1.3  explorer 		if (cp->proto != PPP_LCP)
   1737    1.3  explorer 			goto illegal;
   1738  1.192  yamaguch 		if (sp->scp[cp->protoidx].state != STATE_OPENED) {
   1739    1.3  explorer 			if (debug)
   1740   1.64    itojun 				addlog("%s: lcp echo req but lcp closed\n",
   1741   1.64    itojun 				       ifp->if_xname);
   1742  1.185   thorpej 			if_statinc(ifp, if_ierrors);
   1743    1.3  explorer 			break;
   1744    1.3  explorer 		}
   1745    1.3  explorer 		if (len < 8) {
   1746    1.3  explorer 			if (debug)
   1747   1.64    itojun 				addlog("%s: invalid lcp echo request "
   1748    1.3  explorer 				       "packet length: %d bytes\n",
   1749   1.64    itojun 				       ifp->if_xname, len);
   1750    1.3  explorer 			break;
   1751    1.3  explorer 		}
   1752   1.30      ross 		memcpy(&u32, h + 1, sizeof u32);
   1753   1.30      ross 		if (ntohl(u32) == sp->lcp.magic) {
   1754    1.3  explorer 			/* Line loopback mode detected. */
   1755   1.64    itojun 			printf("%s: loopback\n", ifp->if_xname);
   1756  1.170  knakahar 			SPPP_UNLOCK(sp);
   1757   1.59    itojun 			if_down(ifp);
   1758  1.170  knakahar 			SPPP_LOCK(sp, RW_WRITER);
   1759  1.170  knakahar 
   1760   1.59    itojun 			IF_PURGE(&sp->pp_cpq);
   1761    1.3  explorer 
   1762    1.3  explorer 			/* Shut down the PPP link. */
   1763    1.3  explorer 			/* XXX */
   1764  1.198  yamaguch 			sppp_wq_add(sp->wq_cp, &sp->scp[IDX_LCP].work_down);
   1765  1.198  yamaguch 			sppp_wq_add(sp->wq_cp, &sp->scp[IDX_LCP].work_up);
   1766    1.3  explorer 			break;
   1767    1.3  explorer 		}
   1768   1.30      ross 		u32 = htonl(sp->lcp.magic);
   1769   1.30      ross 		memcpy(h + 1, &u32, sizeof u32);
   1770    1.3  explorer 		if (debug)
   1771   1.64    itojun 			addlog("%s: got lcp echo req, sending echo rep\n",
   1772   1.64    itojun 			       ifp->if_xname);
   1773   1.59    itojun 		sppp_cp_send(sp, PPP_LCP, ECHO_REPLY, h->ident, len - 4,
   1774   1.59    itojun 		    h + 1);
   1775    1.1  explorer 		break;
   1776    1.3  explorer 	case ECHO_REPLY:
   1777    1.3  explorer 		if (cp->proto != PPP_LCP)
   1778    1.3  explorer 			goto illegal;
   1779    1.3  explorer 		if (h->ident != sp->lcp.echoid) {
   1780  1.185   thorpej 			if_statinc(ifp, if_ierrors);
   1781    1.3  explorer 			break;
   1782    1.3  explorer 		}
   1783    1.3  explorer 		if (len < 8) {
   1784    1.3  explorer 			if (debug)
   1785   1.64    itojun 				addlog("%s: lcp invalid echo reply "
   1786    1.3  explorer 				       "packet length: %d bytes\n",
   1787   1.64    itojun 				       ifp->if_xname, len);
   1788    1.3  explorer 			break;
   1789    1.3  explorer 		}
   1790    1.3  explorer 		if (debug)
   1791   1.64    itojun 			addlog("%s: lcp got echo rep\n",
   1792   1.64    itojun 			       ifp->if_xname);
   1793   1.30      ross 		memcpy(&u32, h + 1, sizeof u32);
   1794   1.30      ross 		if (ntohl(u32) != sp->lcp.magic)
   1795    1.3  explorer 			sp->pp_alivecnt = 0;
   1796    1.1  explorer 		break;
   1797    1.3  explorer 	default:
   1798    1.3  explorer 		/* Unknown packet type -- send Code-Reject packet. */
   1799    1.3  explorer 	  illegal:
   1800    1.3  explorer 		if (debug)
   1801   1.64    itojun 			addlog("%s: %s send code-rej for 0x%x\n",
   1802   1.64    itojun 			       ifp->if_xname, cp->name, h->type);
   1803    1.9    itojun 		sppp_cp_send(sp, cp->proto, CODE_REJ,
   1804  1.192  yamaguch 		    ++sp->scp[cp->protoidx].seq, m->m_pkthdr.len, h);
   1805  1.185   thorpej 		if_statinc(ifp, if_ierrors);
   1806    1.1  explorer 	}
   1807  1.170  knakahar 
   1808  1.170  knakahar 	SPPP_UNLOCK(sp);
   1809    1.1  explorer }
   1810    1.1  explorer 
   1811    1.3  explorer 
   1812    1.3  explorer /*
   1813    1.3  explorer  * The generic part of all Up/Down/Open/Close/TO event handlers.
   1814    1.3  explorer  * Basically, the state transition handling in the automaton.
   1815    1.3  explorer  */
   1816    1.1  explorer static void
   1817  1.198  yamaguch sppp_up_event(struct sppp *sp, void *xcp)
   1818    1.1  explorer {
   1819  1.198  yamaguch 	const struct cp *cp = xcp;
   1820    1.3  explorer 	STDDCL;
   1821    1.1  explorer 
   1822  1.170  knakahar 	KASSERT(SPPP_WLOCKED(sp));
   1823  1.163  knakahar 
   1824  1.201  yamaguch 	if ((cp->flags & CP_AUTH) != 0 &&
   1825  1.201  yamaguch 	    sppp_auth_role(cp, sp) == SPPP_AUTH_NOROLE)
   1826  1.201  yamaguch 		return;
   1827  1.201  yamaguch 
   1828    1.3  explorer 	if (debug)
   1829   1.64    itojun 		log(LOG_DEBUG, "%s: %s up(%s)\n",
   1830   1.64    itojun 		    ifp->if_xname, cp->name,
   1831  1.192  yamaguch 		    sppp_state_name(sp->scp[cp->protoidx].state));
   1832    1.3  explorer 
   1833  1.192  yamaguch 	switch (sp->scp[cp->protoidx].state) {
   1834    1.3  explorer 	case STATE_INITIAL:
   1835    1.3  explorer 		sppp_cp_change_state(cp, sp, STATE_CLOSED);
   1836    1.3  explorer 		break;
   1837    1.3  explorer 	case STATE_STARTING:
   1838  1.192  yamaguch 		sp->scp[cp->protoidx].rst_counter = sp->lcp.max_configure;
   1839    1.3  explorer 		(cp->scr)(sp);
   1840    1.3  explorer 		sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
   1841    1.3  explorer 		break;
   1842    1.3  explorer 	default:
   1843   1.64    itojun 		printf("%s: %s illegal up in state %s\n",
   1844   1.64    itojun 		       ifp->if_xname, cp->name,
   1845  1.192  yamaguch 		       sppp_state_name(sp->scp[cp->protoidx].state));
   1846    1.3  explorer 	}
   1847    1.1  explorer }
   1848    1.1  explorer 
   1849    1.1  explorer static void
   1850  1.198  yamaguch sppp_down_event(struct sppp *sp, void *xcp)
   1851    1.1  explorer {
   1852  1.198  yamaguch 	const struct cp *cp = xcp;
   1853    1.3  explorer 	STDDCL;
   1854    1.3  explorer 
   1855  1.170  knakahar 	KASSERT(SPPP_WLOCKED(sp));
   1856  1.163  knakahar 
   1857  1.201  yamaguch 	if ((cp->flags & CP_AUTH) != 0 &&
   1858  1.201  yamaguch 	    sppp_auth_role(cp, sp) == SPPP_AUTH_NOROLE)
   1859  1.201  yamaguch 		return;
   1860  1.201  yamaguch 
   1861    1.3  explorer 	if (debug)
   1862   1.64    itojun 		log(LOG_DEBUG, "%s: %s down(%s)\n",
   1863   1.64    itojun 		    ifp->if_xname, cp->name,
   1864  1.192  yamaguch 		    sppp_state_name(sp->scp[cp->protoidx].state));
   1865    1.3  explorer 
   1866  1.192  yamaguch 	switch (sp->scp[cp->protoidx].state) {
   1867    1.3  explorer 	case STATE_CLOSED:
   1868    1.3  explorer 	case STATE_CLOSING:
   1869    1.3  explorer 		sppp_cp_change_state(cp, sp, STATE_INITIAL);
   1870    1.3  explorer 		break;
   1871    1.3  explorer 	case STATE_STOPPED:
   1872  1.202  yamaguch 		(cp->tls)(cp, sp);
   1873    1.3  explorer 		/* fall through */
   1874    1.3  explorer 	case STATE_STOPPING:
   1875    1.3  explorer 	case STATE_REQ_SENT:
   1876    1.3  explorer 	case STATE_ACK_RCVD:
   1877    1.3  explorer 	case STATE_ACK_SENT:
   1878    1.3  explorer 		sppp_cp_change_state(cp, sp, STATE_STARTING);
   1879    1.3  explorer 		break;
   1880    1.3  explorer 	case STATE_OPENED:
   1881    1.3  explorer 		(cp->tld)(sp);
   1882    1.3  explorer 		sppp_cp_change_state(cp, sp, STATE_STARTING);
   1883    1.3  explorer 		break;
   1884    1.3  explorer 	default:
   1885   1.64    itojun 		printf("%s: %s illegal down in state %s\n",
   1886   1.64    itojun 		       ifp->if_xname, cp->name,
   1887  1.192  yamaguch 		       sppp_state_name(sp->scp[cp->protoidx].state));
   1888    1.3  explorer 	}
   1889    1.1  explorer }
   1890    1.1  explorer 
   1891    1.3  explorer 
   1892    1.1  explorer static void
   1893  1.198  yamaguch sppp_open_event(struct sppp *sp, void *xcp)
   1894    1.1  explorer {
   1895  1.198  yamaguch 	const struct cp *cp = xcp;
   1896    1.3  explorer 	STDDCL;
   1897    1.1  explorer 
   1898  1.170  knakahar 	KASSERT(SPPP_WLOCKED(sp));
   1899  1.163  knakahar 
   1900  1.201  yamaguch 	if ((cp->flags & CP_AUTH) != 0 &&
   1901  1.201  yamaguch 	    sppp_auth_role(cp, sp) == SPPP_AUTH_NOROLE)
   1902  1.201  yamaguch 		return;
   1903  1.201  yamaguch 
   1904    1.3  explorer 	if (debug)
   1905   1.64    itojun 		log(LOG_DEBUG, "%s: %s open(%s)\n",
   1906   1.64    itojun 		    ifp->if_xname, cp->name,
   1907  1.192  yamaguch 		    sppp_state_name(sp->scp[cp->protoidx].state));
   1908    1.3  explorer 
   1909  1.192  yamaguch 	switch (sp->scp[cp->protoidx].state) {
   1910    1.3  explorer 	case STATE_INITIAL:
   1911   1.91  christos 		sppp_cp_change_state(cp, sp, STATE_STARTING);
   1912  1.202  yamaguch 		(cp->tls)(cp, sp);
   1913    1.3  explorer 		break;
   1914    1.3  explorer 	case STATE_STARTING:
   1915    1.3  explorer 		break;
   1916    1.3  explorer 	case STATE_CLOSED:
   1917  1.192  yamaguch 		sp->scp[cp->protoidx].rst_counter = sp->lcp.max_configure;
   1918    1.3  explorer 		(cp->scr)(sp);
   1919    1.3  explorer 		sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
   1920    1.3  explorer 		break;
   1921    1.3  explorer 	case STATE_STOPPED:
   1922    1.3  explorer 	case STATE_STOPPING:
   1923    1.3  explorer 	case STATE_REQ_SENT:
   1924    1.3  explorer 	case STATE_ACK_RCVD:
   1925    1.3  explorer 	case STATE_ACK_SENT:
   1926    1.3  explorer 	case STATE_OPENED:
   1927    1.3  explorer 		break;
   1928    1.3  explorer 	case STATE_CLOSING:
   1929    1.3  explorer 		sppp_cp_change_state(cp, sp, STATE_STOPPING);
   1930    1.1  explorer 		break;
   1931    1.1  explorer 	}
   1932    1.1  explorer }
   1933    1.1  explorer 
   1934    1.3  explorer 
   1935    1.3  explorer static void
   1936  1.198  yamaguch sppp_close_event(struct sppp *sp, void *xcp)
   1937    1.1  explorer {
   1938  1.198  yamaguch 	const struct cp *cp = xcp;
   1939    1.3  explorer 	STDDCL;
   1940    1.1  explorer 
   1941  1.170  knakahar 	KASSERT(SPPP_WLOCKED(sp));
   1942  1.163  knakahar 
   1943  1.201  yamaguch 	if ((cp->flags & CP_AUTH) != 0 &&
   1944  1.201  yamaguch 	    sppp_auth_role(cp, sp) == SPPP_AUTH_NOROLE)
   1945  1.201  yamaguch 		return;
   1946  1.201  yamaguch 
   1947    1.3  explorer 	if (debug)
   1948   1.64    itojun 		log(LOG_DEBUG, "%s: %s close(%s)\n",
   1949   1.64    itojun 		    ifp->if_xname, cp->name,
   1950  1.192  yamaguch 		    sppp_state_name(sp->scp[cp->protoidx].state));
   1951    1.3  explorer 
   1952  1.192  yamaguch 	switch (sp->scp[cp->protoidx].state) {
   1953    1.3  explorer 	case STATE_INITIAL:
   1954    1.3  explorer 	case STATE_CLOSED:
   1955    1.3  explorer 	case STATE_CLOSING:
   1956    1.3  explorer 		break;
   1957    1.3  explorer 	case STATE_STARTING:
   1958   1.91  christos 		sppp_cp_change_state(cp, sp, STATE_INITIAL);
   1959  1.202  yamaguch 		(cp->tlf)(cp, sp);
   1960    1.3  explorer 		break;
   1961    1.3  explorer 	case STATE_STOPPED:
   1962    1.3  explorer 		sppp_cp_change_state(cp, sp, STATE_CLOSED);
   1963    1.3  explorer 		break;
   1964    1.3  explorer 	case STATE_STOPPING:
   1965    1.3  explorer 		sppp_cp_change_state(cp, sp, STATE_CLOSING);
   1966    1.3  explorer 		break;
   1967    1.3  explorer 	case STATE_OPENED:
   1968    1.3  explorer 		(cp->tld)(sp);
   1969    1.3  explorer 		/* fall through */
   1970    1.3  explorer 	case STATE_REQ_SENT:
   1971    1.3  explorer 	case STATE_ACK_RCVD:
   1972    1.3  explorer 	case STATE_ACK_SENT:
   1973  1.192  yamaguch 		sp->scp[cp->protoidx].rst_counter = sp->lcp.max_terminate;
   1974  1.201  yamaguch 		if ((cp->flags & CP_AUTH) == 0) {
   1975  1.201  yamaguch 			sppp_cp_send(sp, cp->proto, TERM_REQ,
   1976  1.201  yamaguch 			    ++sp->scp[cp->protoidx].seq, 0, 0);
   1977  1.201  yamaguch 		}
   1978    1.3  explorer 		sppp_cp_change_state(cp, sp, STATE_CLOSING);
   1979    1.3  explorer 		break;
   1980    1.3  explorer 	}
   1981    1.3  explorer }
   1982    1.3  explorer 
   1983    1.3  explorer static void
   1984  1.198  yamaguch sppp_to_event(struct sppp *sp, void *xcp)
   1985    1.3  explorer {
   1986  1.198  yamaguch 	const struct cp *cp = xcp;
   1987  1.198  yamaguch 	int s;
   1988    1.3  explorer 	STDDCL;
   1989    1.3  explorer 
   1990  1.170  knakahar 	KASSERT(SPPP_WLOCKED(sp));
   1991  1.163  knakahar 
   1992   1.23   thorpej 	s = splnet();
   1993  1.163  knakahar 
   1994    1.3  explorer 	if (debug)
   1995   1.64    itojun 		log(LOG_DEBUG, "%s: %s TO(%s) rst_counter = %d\n",
   1996   1.64    itojun 		    ifp->if_xname, cp->name,
   1997  1.192  yamaguch 		    sppp_state_name(sp->scp[cp->protoidx].state),
   1998  1.192  yamaguch 		    sp->scp[cp->protoidx].rst_counter);
   1999    1.3  explorer 
   2000  1.192  yamaguch 	if (--sp->scp[cp->protoidx].rst_counter < 0)
   2001    1.3  explorer 		/* TO- event */
   2002  1.192  yamaguch 		switch (sp->scp[cp->protoidx].state) {
   2003    1.3  explorer 		case STATE_CLOSING:
   2004  1.201  yamaguch 			sppp_cp_change_state(cp, sp, STATE_CLOSED);
   2005  1.202  yamaguch 			(cp->tlf)(cp, sp);
   2006    1.3  explorer 			break;
   2007    1.3  explorer 		case STATE_STOPPING:
   2008  1.201  yamaguch 			sppp_cp_change_state(cp, sp, STATE_STOPPED);
   2009  1.202  yamaguch 			(cp->tlf)(cp, sp);
   2010    1.3  explorer 			break;
   2011    1.3  explorer 		case STATE_REQ_SENT:
   2012    1.3  explorer 		case STATE_ACK_RCVD:
   2013    1.3  explorer 		case STATE_ACK_SENT:
   2014  1.201  yamaguch 			sppp_cp_change_state(cp, sp, STATE_STOPPED);
   2015  1.202  yamaguch 			(cp->tlf)(cp, sp);
   2016    1.3  explorer 			break;
   2017    1.3  explorer 		}
   2018    1.3  explorer 	else
   2019    1.3  explorer 		/* TO+ event */
   2020  1.192  yamaguch 		switch (sp->scp[cp->protoidx].state) {
   2021    1.3  explorer 		case STATE_CLOSING:
   2022    1.3  explorer 		case STATE_STOPPING:
   2023  1.201  yamaguch 			if ((cp->flags & CP_AUTH) == 0) {
   2024  1.201  yamaguch 				sppp_cp_send(sp, cp->proto, TERM_REQ,
   2025  1.201  yamaguch 				    ++sp->scp[cp->protoidx].seq, 0, 0);
   2026  1.201  yamaguch 			}
   2027  1.198  yamaguch 			callout_schedule(&sp->scp[cp->protoidx].ch, sp->lcp.timeout);
   2028    1.3  explorer 			break;
   2029    1.3  explorer 		case STATE_REQ_SENT:
   2030    1.3  explorer 		case STATE_ACK_RCVD:
   2031    1.3  explorer 			(cp->scr)(sp);
   2032    1.3  explorer 			/* sppp_cp_change_state() will restart the timer */
   2033    1.3  explorer 			sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
   2034    1.3  explorer 			break;
   2035    1.3  explorer 		case STATE_ACK_SENT:
   2036    1.3  explorer 			(cp->scr)(sp);
   2037  1.198  yamaguch 			callout_schedule(&sp->scp[cp->protoidx].ch, sp->lcp.timeout);
   2038    1.3  explorer 			break;
   2039    1.3  explorer 		}
   2040    1.3  explorer 
   2041    1.3  explorer 	splx(s);
   2042    1.3  explorer }
   2043    1.3  explorer 
   2044  1.194  yamaguch static void
   2045  1.198  yamaguch sppp_rcr_event(struct sppp *sp, void *xcp)
   2046  1.194  yamaguch {
   2047  1.198  yamaguch 	const struct cp *cp = xcp;
   2048  1.194  yamaguch 	u_char type;
   2049  1.194  yamaguch 	void *buf;
   2050  1.194  yamaguch 	size_t blen;
   2051  1.198  yamaguch 	STDDCL;
   2052  1.194  yamaguch 
   2053  1.194  yamaguch 	type = sp->scp[cp->protoidx].rcr_type;
   2054  1.194  yamaguch 	buf = sp->scp[cp->protoidx].rcr_buf;
   2055  1.194  yamaguch 	blen = sp->scp[cp->protoidx].rcr_blen;
   2056  1.194  yamaguch 
   2057  1.194  yamaguch 	if (type == CONF_ACK) {
   2058  1.194  yamaguch 		/* RCR+ event */
   2059  1.194  yamaguch 		switch (sp->scp[cp->protoidx].state) {
   2060  1.194  yamaguch 		case STATE_OPENED:
   2061  1.194  yamaguch 			sppp_cp_change_state(cp, sp, STATE_ACK_SENT);
   2062  1.194  yamaguch 			cp->tld(sp);
   2063  1.194  yamaguch 			cp->scr(sp);
   2064  1.194  yamaguch 			cp->scan(cp, sp);
   2065  1.194  yamaguch 			break;
   2066  1.194  yamaguch 		case STATE_ACK_SENT:
   2067  1.194  yamaguch 		case STATE_REQ_SENT:
   2068  1.194  yamaguch 			sppp_cp_change_state(cp, sp, STATE_ACK_SENT);
   2069  1.194  yamaguch 			cp->scan(cp, sp);
   2070  1.194  yamaguch 			break;
   2071  1.194  yamaguch 		case STATE_STOPPED:
   2072  1.194  yamaguch 			sppp_cp_change_state(cp, sp, STATE_ACK_SENT);
   2073  1.194  yamaguch 			cp->scr(sp);
   2074  1.194  yamaguch 			cp->scan(cp, sp);
   2075  1.194  yamaguch 			break;
   2076  1.194  yamaguch 		case STATE_ACK_RCVD:
   2077  1.194  yamaguch 			sppp_cp_change_state(cp, sp, STATE_OPENED);
   2078  1.194  yamaguch 			if (debug)
   2079  1.194  yamaguch 				log(LOG_DEBUG, "%s: %s tlu\n",
   2080  1.194  yamaguch 				    ifp->if_xname,
   2081  1.194  yamaguch 				    cp->name);
   2082  1.194  yamaguch 			cp->tlu(sp);
   2083  1.194  yamaguch 			cp->scan(cp, sp);
   2084  1.194  yamaguch 			break;
   2085  1.194  yamaguch 		case STATE_CLOSING:
   2086  1.194  yamaguch 		case STATE_STOPPING:
   2087  1.194  yamaguch 			if (buf != NULL) {
   2088  1.194  yamaguch 				sp->scp[cp->protoidx].rcr_buf = NULL;
   2089  1.194  yamaguch 				sp->scp[cp->protoidx].rcr_blen = 0;
   2090  1.194  yamaguch 				kmem_free(buf, blen);
   2091  1.194  yamaguch 			}
   2092  1.194  yamaguch 			break;
   2093  1.194  yamaguch 		case STATE_CLOSED:
   2094  1.201  yamaguch 			if ((cp->flags & CP_AUTH) == 0) {
   2095  1.201  yamaguch 				sppp_cp_send(sp, cp->proto, TERM_ACK,
   2096  1.201  yamaguch 				    sp->scp[cp->protoidx].rconfid, 0, 0);
   2097  1.201  yamaguch 			}
   2098  1.194  yamaguch 			break;
   2099  1.194  yamaguch 		default:
   2100  1.194  yamaguch 			printf("%s: %s illegal RCR+ in state %s\n",
   2101  1.194  yamaguch 			    ifp->if_xname, cp->name,
   2102  1.194  yamaguch 			    sppp_state_name(sp->scp[cp->protoidx].state));
   2103  1.194  yamaguch 			if_statinc(ifp, if_ierrors);
   2104  1.194  yamaguch 		}
   2105  1.194  yamaguch 	} else {
   2106  1.194  yamaguch 		/* RCR- event */
   2107  1.194  yamaguch 		switch (sp->scp[cp->protoidx].state) {
   2108  1.194  yamaguch 		case STATE_OPENED:
   2109  1.194  yamaguch 			sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
   2110  1.194  yamaguch 			cp->tld(sp);
   2111  1.194  yamaguch 			cp->scr(sp);
   2112  1.194  yamaguch 			cp->scan(cp, sp);
   2113  1.194  yamaguch 			break;
   2114  1.194  yamaguch 		case STATE_ACK_SENT:
   2115  1.194  yamaguch 		case STATE_REQ_SENT:
   2116  1.194  yamaguch 			sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
   2117  1.194  yamaguch 			cp->scan(cp, sp);
   2118  1.194  yamaguch 			break;
   2119  1.194  yamaguch 		case STATE_STOPPED:
   2120  1.194  yamaguch 			sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
   2121  1.194  yamaguch 			cp->scr(sp);
   2122  1.194  yamaguch 			cp->scan(cp, sp);
   2123  1.194  yamaguch 			break;
   2124  1.194  yamaguch 		case STATE_ACK_RCVD:
   2125  1.194  yamaguch 			sppp_cp_change_state(cp, sp, STATE_ACK_RCVD);
   2126  1.194  yamaguch 			cp->scan(cp, sp);
   2127  1.194  yamaguch 			break;
   2128  1.194  yamaguch 		case STATE_CLOSING:
   2129  1.194  yamaguch 		case STATE_STOPPING:
   2130  1.194  yamaguch 			if (buf != NULL) {
   2131  1.194  yamaguch 				sp->scp[cp->protoidx].rcr_buf = NULL;
   2132  1.194  yamaguch 				sp->scp[cp->protoidx].rcr_blen = 0;
   2133  1.194  yamaguch 				kmem_free(buf, blen);
   2134  1.194  yamaguch 			}
   2135  1.194  yamaguch 			break;
   2136  1.194  yamaguch 		case STATE_CLOSED:
   2137  1.194  yamaguch 			sppp_cp_change_state(cp, sp, STATE_CLOSED);
   2138  1.201  yamaguch 			if ((cp->flags & CP_AUTH) == 0) {
   2139  1.201  yamaguch 				sppp_cp_send(sp, cp->proto, TERM_ACK,
   2140  1.201  yamaguch 				    sp->scp[cp->protoidx].rconfid, 0, 0);
   2141  1.201  yamaguch 			}
   2142  1.194  yamaguch 			break;
   2143  1.194  yamaguch 		default:
   2144  1.194  yamaguch 			printf("%s: %s illegal RCR- in state %s\n",
   2145  1.194  yamaguch 			    ifp->if_xname, cp->name,
   2146  1.194  yamaguch 			    sppp_state_name(sp->scp[cp->protoidx].state));
   2147  1.194  yamaguch 			if_statinc(ifp, if_ierrors);
   2148  1.194  yamaguch 		}
   2149  1.194  yamaguch 	}
   2150  1.194  yamaguch }
   2151  1.194  yamaguch 
   2152  1.195  yamaguch static void
   2153  1.198  yamaguch sppp_rca_event(struct sppp *sp, void *xcp)
   2154  1.195  yamaguch {
   2155  1.198  yamaguch 	const struct cp *cp = xcp;
   2156  1.195  yamaguch 	STDDCL;
   2157  1.195  yamaguch 
   2158  1.195  yamaguch 	switch (sp->scp[cp->protoidx].state) {
   2159  1.195  yamaguch 	case STATE_CLOSED:
   2160  1.195  yamaguch 	case STATE_STOPPED:
   2161  1.201  yamaguch 		if ((cp->flags & CP_AUTH) == 0) {
   2162  1.201  yamaguch 			sppp_cp_send(sp, cp->proto, TERM_ACK,
   2163  1.201  yamaguch 			    sp->scp[cp->protoidx].rconfid, 0, 0);
   2164  1.201  yamaguch 		}
   2165  1.195  yamaguch 		break;
   2166  1.195  yamaguch 	case STATE_CLOSING:
   2167  1.195  yamaguch 	case STATE_STOPPING:
   2168  1.195  yamaguch 		break;
   2169  1.195  yamaguch 	case STATE_REQ_SENT:
   2170  1.195  yamaguch 		sp->scp[cp->protoidx].rst_counter = sp->lcp.max_configure;
   2171  1.195  yamaguch 		sppp_cp_change_state(cp, sp, STATE_ACK_RCVD);
   2172  1.195  yamaguch 		break;
   2173  1.195  yamaguch 	case STATE_OPENED:
   2174  1.195  yamaguch 		(cp->tld)(sp);
   2175  1.195  yamaguch 		/* fall through */
   2176  1.195  yamaguch 	case STATE_ACK_RCVD:
   2177  1.195  yamaguch 		(cp->scr)(sp);
   2178  1.195  yamaguch 		sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
   2179  1.195  yamaguch 		break;
   2180  1.195  yamaguch 	case STATE_ACK_SENT:
   2181  1.195  yamaguch 		sppp_cp_change_state(cp, sp, STATE_OPENED);
   2182  1.195  yamaguch 		sp->scp[cp->protoidx].rst_counter = sp->lcp.max_configure;
   2183  1.195  yamaguch 		if (debug)
   2184  1.195  yamaguch 			log(LOG_DEBUG, "%s: %s tlu\n",
   2185  1.195  yamaguch 			       ifp->if_xname, cp->name);
   2186  1.195  yamaguch 		(cp->tlu)(sp);
   2187  1.195  yamaguch 		break;
   2188  1.195  yamaguch 	default:
   2189  1.195  yamaguch 		printf("%s: %s illegal RCA in state %s\n",
   2190  1.195  yamaguch 		       ifp->if_xname, cp->name,
   2191  1.195  yamaguch 		       sppp_state_name(sp->scp[cp->protoidx].state));
   2192  1.195  yamaguch 		if_statinc(ifp, if_ierrors);
   2193  1.195  yamaguch 	}
   2194  1.195  yamaguch }
   2195  1.195  yamaguch 
   2196  1.195  yamaguch static void
   2197  1.198  yamaguch sppp_rcn_event(struct sppp *sp, void *xcp)
   2198  1.195  yamaguch {
   2199  1.198  yamaguch 	const struct cp *cp = xcp;
   2200  1.195  yamaguch 	struct ifnet *ifp = &sp->pp_if;
   2201  1.195  yamaguch 
   2202  1.195  yamaguch 	switch (sp->scp[cp->protoidx].state) {
   2203  1.195  yamaguch 	case STATE_CLOSED:
   2204  1.195  yamaguch 	case STATE_STOPPED:
   2205  1.201  yamaguch 		if ((cp->flags & CP_AUTH) == 0) {
   2206  1.201  yamaguch 			sppp_cp_send(sp, cp->proto, TERM_ACK,
   2207  1.201  yamaguch 			    sp->scp[cp->protoidx].rconfid, 0, 0);
   2208  1.201  yamaguch 		}
   2209  1.195  yamaguch 		break;
   2210  1.195  yamaguch 	case STATE_REQ_SENT:
   2211  1.195  yamaguch 	case STATE_ACK_SENT:
   2212  1.195  yamaguch 		sp->scp[cp->protoidx].rst_counter = sp->lcp.max_configure;
   2213  1.195  yamaguch 		(cp->scr)(sp);
   2214  1.195  yamaguch 		break;
   2215  1.195  yamaguch 	case STATE_OPENED:
   2216  1.195  yamaguch 		(cp->tld)(sp);
   2217  1.195  yamaguch 		/* fall through */
   2218  1.195  yamaguch 	case STATE_ACK_RCVD:
   2219  1.195  yamaguch 		sppp_cp_change_state(cp, sp, STATE_ACK_SENT);
   2220  1.195  yamaguch 		(cp->scr)(sp);
   2221  1.195  yamaguch 		break;
   2222  1.195  yamaguch 	case STATE_CLOSING:
   2223  1.195  yamaguch 	case STATE_STOPPING:
   2224  1.195  yamaguch 		break;
   2225  1.195  yamaguch 	default:
   2226  1.195  yamaguch 		printf("%s: %s illegal RCN in state %s\n",
   2227  1.195  yamaguch 		       ifp->if_xname, cp->name,
   2228  1.195  yamaguch 		       sppp_state_name(sp->scp[cp->protoidx].state));
   2229  1.195  yamaguch 		if_statinc(ifp, if_ierrors);
   2230  1.195  yamaguch 	}
   2231  1.195  yamaguch }
   2232  1.195  yamaguch 
   2233  1.196  yamaguch static void
   2234  1.198  yamaguch sppp_rtr_event(struct sppp *sp, void *xcp)
   2235  1.196  yamaguch {
   2236  1.198  yamaguch 	const struct cp *cp = xcp;
   2237  1.196  yamaguch 	STDDCL;
   2238  1.196  yamaguch 
   2239  1.196  yamaguch 	switch (sp->scp[cp->protoidx].state) {
   2240  1.196  yamaguch 	case STATE_ACK_RCVD:
   2241  1.196  yamaguch 	case STATE_ACK_SENT:
   2242  1.196  yamaguch 		sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
   2243  1.196  yamaguch 		break;
   2244  1.196  yamaguch 	case STATE_CLOSED:
   2245  1.196  yamaguch 	case STATE_STOPPED:
   2246  1.196  yamaguch 	case STATE_CLOSING:
   2247  1.196  yamaguch 	case STATE_STOPPING:
   2248  1.196  yamaguch 	case STATE_REQ_SENT:
   2249  1.196  yamaguch 		break;
   2250  1.196  yamaguch 	case STATE_OPENED:
   2251  1.196  yamaguch 		(cp->tld)(sp);
   2252  1.196  yamaguch 		sp->scp[cp->protoidx].rst_counter = 0;
   2253  1.196  yamaguch 		sppp_cp_change_state(cp, sp, STATE_STOPPING);
   2254  1.196  yamaguch 		break;
   2255  1.196  yamaguch 	default:
   2256  1.196  yamaguch 		printf("%s: %s illegal RTR in state %s\n",
   2257  1.196  yamaguch 		       ifp->if_xname, cp->name,
   2258  1.196  yamaguch 		       sppp_state_name(sp->scp[cp->protoidx].state));
   2259  1.196  yamaguch 		if_statinc(ifp, if_ierrors);
   2260  1.196  yamaguch 		return;
   2261  1.196  yamaguch 	}
   2262  1.196  yamaguch 
   2263  1.196  yamaguch 	/* Send Terminate-Ack packet. */
   2264  1.196  yamaguch 	if (debug)
   2265  1.196  yamaguch 		log(LOG_DEBUG, "%s: %s send terminate-ack\n",
   2266  1.196  yamaguch 		    ifp->if_xname, cp->name);
   2267  1.201  yamaguch 	if ((cp->flags & CP_AUTH) == 0) {
   2268  1.201  yamaguch 		sppp_cp_send(sp, cp->proto, TERM_ACK,
   2269  1.201  yamaguch 		    sp->scp[cp->protoidx].rseq, 0, 0);
   2270  1.201  yamaguch 	}
   2271  1.196  yamaguch }
   2272  1.196  yamaguch 
   2273  1.196  yamaguch static void
   2274  1.198  yamaguch sppp_rta_event(struct sppp *sp, void *xcp)
   2275  1.196  yamaguch {
   2276  1.198  yamaguch 	const struct cp *cp = xcp;
   2277  1.196  yamaguch 	struct ifnet *ifp = &sp->pp_if;
   2278  1.196  yamaguch 
   2279  1.196  yamaguch 	switch (sp->scp[cp->protoidx].state) {
   2280  1.196  yamaguch 	case STATE_CLOSED:
   2281  1.196  yamaguch 	case STATE_STOPPED:
   2282  1.196  yamaguch 	case STATE_REQ_SENT:
   2283  1.196  yamaguch 	case STATE_ACK_SENT:
   2284  1.196  yamaguch 		break;
   2285  1.196  yamaguch 	case STATE_CLOSING:
   2286  1.201  yamaguch 		sppp_cp_change_state(cp, sp, STATE_CLOSED);
   2287  1.202  yamaguch 		(cp->tlf)(cp, sp);
   2288  1.196  yamaguch 		break;
   2289  1.196  yamaguch 	case STATE_STOPPING:
   2290  1.201  yamaguch 		sppp_cp_change_state(cp, sp, STATE_STOPPED);
   2291  1.202  yamaguch 		(cp->tlf)(cp, sp);
   2292  1.196  yamaguch 		break;
   2293  1.196  yamaguch 	case STATE_ACK_RCVD:
   2294  1.196  yamaguch 		sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
   2295  1.196  yamaguch 		break;
   2296  1.196  yamaguch 	case STATE_OPENED:
   2297  1.196  yamaguch 		(cp->tld)(sp);
   2298  1.196  yamaguch 		(cp->scr)(sp);
   2299  1.196  yamaguch 		sppp_cp_change_state(cp, sp, STATE_ACK_RCVD);
   2300  1.196  yamaguch 		break;
   2301  1.196  yamaguch 	default:
   2302  1.196  yamaguch 		printf("%s: %s illegal RTA in state %s\n",
   2303  1.196  yamaguch 		       ifp->if_xname, cp->name,
   2304  1.196  yamaguch 		       sppp_state_name(sp->scp[cp->protoidx].state));
   2305  1.196  yamaguch 		if_statinc(ifp, if_ierrors);
   2306  1.196  yamaguch 	}
   2307  1.196  yamaguch }
   2308  1.196  yamaguch 
   2309  1.197  yamaguch static void
   2310  1.198  yamaguch sppp_rxj_event(struct sppp *sp, void *xcp)
   2311  1.197  yamaguch {
   2312  1.198  yamaguch 	const struct cp *cp = xcp;
   2313  1.197  yamaguch 	struct ifnet *ifp = &sp->pp_if;
   2314  1.197  yamaguch 
   2315  1.197  yamaguch 	/* XXX catastrophic rejects (RXJ-) aren't handled yet. */
   2316  1.197  yamaguch 	switch (sp->scp[cp->protoidx].state) {
   2317  1.197  yamaguch 	case STATE_CLOSED:
   2318  1.197  yamaguch 	case STATE_STOPPED:
   2319  1.197  yamaguch 	case STATE_REQ_SENT:
   2320  1.197  yamaguch 	case STATE_ACK_SENT:
   2321  1.197  yamaguch 	case STATE_CLOSING:
   2322  1.197  yamaguch 	case STATE_STOPPING:
   2323  1.197  yamaguch 	case STATE_OPENED:
   2324  1.197  yamaguch 		break;
   2325  1.197  yamaguch 	case STATE_ACK_RCVD:
   2326  1.197  yamaguch 		sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
   2327  1.197  yamaguch 		break;
   2328  1.197  yamaguch 	default:
   2329  1.197  yamaguch 		printf("%s: %s illegal RXJ- in state %s\n",
   2330  1.197  yamaguch 		       ifp->if_xname, cp->name,
   2331  1.197  yamaguch 		       sppp_state_name(sp->scp[cp->protoidx].state));
   2332  1.197  yamaguch 		if_statinc(ifp, if_ierrors);
   2333  1.197  yamaguch 	}
   2334  1.197  yamaguch }
   2335  1.197  yamaguch 
   2336    1.3  explorer /*
   2337    1.3  explorer  * Change the state of a control protocol in the state automaton.
   2338    1.3  explorer  * Takes care of starting/stopping the restart timer.
   2339    1.3  explorer  */
   2340    1.3  explorer void
   2341    1.3  explorer sppp_cp_change_state(const struct cp *cp, struct sppp *sp, int newstate)
   2342    1.3  explorer {
   2343  1.170  knakahar 
   2344  1.170  knakahar 	KASSERT(SPPP_WLOCKED(sp));
   2345  1.163  knakahar 
   2346  1.192  yamaguch 	sp->scp[cp->protoidx].state = newstate;
   2347  1.192  yamaguch 	callout_stop(&sp->scp[cp->protoidx].ch);
   2348    1.3  explorer 	switch (newstate) {
   2349    1.3  explorer 	case STATE_INITIAL:
   2350    1.3  explorer 	case STATE_STARTING:
   2351    1.3  explorer 	case STATE_CLOSED:
   2352    1.3  explorer 	case STATE_STOPPED:
   2353    1.3  explorer 	case STATE_OPENED:
   2354    1.3  explorer 		break;
   2355    1.3  explorer 	case STATE_CLOSING:
   2356    1.3  explorer 	case STATE_STOPPING:
   2357    1.3  explorer 	case STATE_REQ_SENT:
   2358    1.3  explorer 	case STATE_ACK_RCVD:
   2359    1.3  explorer 	case STATE_ACK_SENT:
   2360  1.198  yamaguch 		callout_schedule(&sp->scp[cp->protoidx].ch, sp->lcp.timeout);
   2361    1.3  explorer 		break;
   2362    1.3  explorer 	}
   2363    1.3  explorer }
   2364   1.42  jdolecek 
   2365   1.42  jdolecek /*
   2366    1.3  explorer  *--------------------------------------------------------------------------*
   2367    1.3  explorer  *                                                                          *
   2368    1.3  explorer  *                         The LCP implementation.                          *
   2369    1.3  explorer  *                                                                          *
   2370    1.3  explorer  *--------------------------------------------------------------------------*
   2371    1.3  explorer  */
   2372    1.3  explorer static void
   2373    1.3  explorer sppp_lcp_init(struct sppp *sp)
   2374    1.3  explorer {
   2375  1.170  knakahar 
   2376  1.170  knakahar 	KASSERT(SPPP_WLOCKED(sp));
   2377  1.170  knakahar 
   2378    1.3  explorer 	sp->lcp.opts = (1 << LCP_OPT_MAGIC);
   2379    1.3  explorer 	sp->lcp.magic = 0;
   2380  1.192  yamaguch 	sp->scp[IDX_LCP].state = STATE_INITIAL;
   2381  1.192  yamaguch 	sp->scp[IDX_LCP].fail_counter = 0;
   2382  1.192  yamaguch 	sp->scp[IDX_LCP].seq = 0;
   2383  1.192  yamaguch 	sp->scp[IDX_LCP].rseq = 0;
   2384    1.3  explorer 	sp->lcp.protos = 0;
   2385    1.3  explorer 
   2386    1.3  explorer 	/*
   2387    1.3  explorer 	 * Initialize counters and timeout values.  Note that we don't
   2388    1.3  explorer 	 * use the 3 seconds suggested in RFC 1661 since we are likely
   2389    1.3  explorer 	 * running on a fast link.  XXX We should probably implement
   2390    1.3  explorer 	 * the exponential backoff option.  Note that these values are
   2391    1.3  explorer 	 * relevant for all control protocols, not just LCP only.
   2392    1.3  explorer 	 */
   2393    1.3  explorer 	sp->lcp.timeout = 1 * hz;
   2394    1.3  explorer 	sp->lcp.max_terminate = 2;
   2395    1.3  explorer 	sp->lcp.max_configure = 10;
   2396    1.3  explorer 	sp->lcp.max_failure = 10;
   2397  1.198  yamaguch 	sppp_cp_init(&lcp, sp);
   2398    1.3  explorer }
   2399    1.3  explorer 
   2400    1.3  explorer static void
   2401  1.198  yamaguch sppp_lcp_up(struct sppp *sp, void *xcp)
   2402    1.3  explorer {
   2403  1.198  yamaguch 	const struct cp *cp = xcp;
   2404  1.198  yamaguch 	int pidx;
   2405    1.3  explorer 	STDDCL;
   2406    1.3  explorer 
   2407  1.170  knakahar 	KASSERT(SPPP_WLOCKED(sp));
   2408  1.163  knakahar 
   2409  1.198  yamaguch 	pidx = cp->protoidx;
   2410   1.40    martin 	/* Initialize activity timestamp: opening a connection is an activity */
   2411   1.92    kardel 	sp->pp_last_receive = sp->pp_last_activity = time_uptime;
   2412   1.40    martin 
   2413    1.3  explorer 	/*
   2414    1.3  explorer 	 * If this interface is passive or dial-on-demand, and we are
   2415    1.3  explorer 	 * still in Initial state, it means we've got an incoming
   2416    1.3  explorer 	 * call.  Activate the interface.
   2417    1.3  explorer 	 */
   2418    1.3  explorer 	if ((ifp->if_flags & (IFF_AUTO | IFF_PASSIVE)) != 0) {
   2419    1.3  explorer 		if (debug)
   2420    1.3  explorer 			log(LOG_DEBUG,
   2421   1.64    itojun 			    "%s: Up event", ifp->if_xname);
   2422    1.3  explorer 		ifp->if_flags |= IFF_RUNNING;
   2423  1.198  yamaguch 		if (sp->scp[pidx].state == STATE_INITIAL) {
   2424    1.3  explorer 			if (debug)
   2425    1.3  explorer 				addlog("(incoming call)\n");
   2426    1.3  explorer 			sp->pp_flags |= PP_CALLIN;
   2427  1.198  yamaguch 			sppp_wq_add(sp->wq_cp, &sp->scp[pidx].work_open);
   2428    1.3  explorer 		} else if (debug)
   2429    1.3  explorer 			addlog("\n");
   2430   1.32    martin 	} else if ((ifp->if_flags & (IFF_AUTO | IFF_PASSIVE)) == 0 &&
   2431  1.192  yamaguch 		   (sp->scp[IDX_LCP].state == STATE_INITIAL)) {
   2432   1.32    martin 			ifp->if_flags |= IFF_RUNNING;
   2433  1.198  yamaguch 			sppp_wq_add(sp->wq_cp, &sp->scp[pidx].work_open);
   2434    1.3  explorer 	}
   2435    1.3  explorer 
   2436  1.198  yamaguch 	sppp_up_event(sp, xcp);
   2437    1.3  explorer }
   2438    1.3  explorer 
   2439    1.3  explorer static void
   2440  1.198  yamaguch sppp_lcp_down(struct sppp *sp, void *xcp)
   2441    1.3  explorer {
   2442  1.202  yamaguch 	const struct cp *cp = xcp;
   2443  1.202  yamaguch 	int pidx;
   2444    1.3  explorer 	STDDCL;
   2445    1.3  explorer 
   2446  1.170  knakahar 	KASSERT(SPPP_WLOCKED(sp));
   2447  1.163  knakahar 
   2448  1.202  yamaguch 	pidx = cp->protoidx;
   2449  1.198  yamaguch 	sppp_down_event(sp, xcp);
   2450    1.3  explorer 
   2451    1.3  explorer 	/*
   2452    1.3  explorer 	 * If this is neither a dial-on-demand nor a passive
   2453    1.3  explorer 	 * interface, simulate an ``ifconfig down'' action, so the
   2454    1.3  explorer 	 * administrator can force a redial by another ``ifconfig
   2455    1.3  explorer 	 * up''.  XXX For leased line operation, should we immediately
   2456    1.3  explorer 	 * try to reopen the connection here?
   2457    1.3  explorer 	 */
   2458    1.3  explorer 	if ((ifp->if_flags & (IFF_AUTO | IFF_PASSIVE)) == 0) {
   2459   1.46    martin 		if (debug)
   2460   1.46    martin 			log(LOG_INFO,
   2461   1.64    itojun 			    "%s: Down event (carrier loss), taking interface down.\n",
   2462   1.64    itojun 			    ifp->if_xname);
   2463  1.170  knakahar 		SPPP_UNLOCK(sp);
   2464    1.3  explorer 		if_down(ifp);
   2465  1.170  knakahar 		SPPP_LOCK(sp, RW_WRITER);
   2466  1.198  yamaguch 
   2467  1.198  yamaguch 		if (sp->lcp.reestablish)
   2468  1.198  yamaguch 			sppp_wq_add(sp->wq_cp, &sp->scp[IDX_LCP].work_open);
   2469    1.3  explorer 	} else {
   2470    1.3  explorer 		if (debug)
   2471    1.3  explorer 			log(LOG_DEBUG,
   2472   1.64    itojun 			    "%s: Down event (carrier loss)\n",
   2473   1.64    itojun 			    ifp->if_xname);
   2474    1.3  explorer 	}
   2475  1.202  yamaguch 	sp->scp[pidx].fail_counter = 0;
   2476    1.3  explorer 	sp->pp_flags &= ~PP_CALLIN;
   2477  1.202  yamaguch 	if (sp->scp[pidx].state != STATE_INITIAL)
   2478  1.198  yamaguch 		sppp_wq_add(sp->wq_cp, &sp->scp[IDX_LCP].work_close);
   2479    1.3  explorer 	ifp->if_flags &= ~IFF_RUNNING;
   2480    1.3  explorer }
   2481    1.3  explorer 
   2482    1.3  explorer static void
   2483  1.198  yamaguch sppp_lcp_open(struct sppp *sp, void *xcp)
   2484    1.3  explorer {
   2485  1.170  knakahar 
   2486  1.170  knakahar 	KASSERT(SPPP_WLOCKED(sp));
   2487  1.163  knakahar 
   2488  1.198  yamaguch 	sp->lcp.reestablish = false;
   2489  1.198  yamaguch 
   2490  1.122       rjs 	if (sp->pp_if.if_mtu < PP_MTU) {
   2491  1.122       rjs 		sp->lcp.mru = sp->pp_if.if_mtu;
   2492  1.122       rjs 		sp->lcp.opts |= (1 << LCP_OPT_MRU);
   2493  1.122       rjs 	} else
   2494  1.122       rjs 		sp->lcp.mru = PP_MTU;
   2495  1.122       rjs 	sp->lcp.their_mru = PP_MTU;
   2496  1.122       rjs 
   2497    1.3  explorer 	/*
   2498    1.3  explorer 	 * If we are authenticator, negotiate LCP_AUTH
   2499    1.3  explorer 	 */
   2500    1.3  explorer 	if (sp->hisauth.proto != 0)
   2501    1.3  explorer 		sp->lcp.opts |= (1 << LCP_OPT_AUTH_PROTO);
   2502    1.3  explorer 	else
   2503    1.3  explorer 		sp->lcp.opts &= ~(1 << LCP_OPT_AUTH_PROTO);
   2504    1.3  explorer 	sp->pp_flags &= ~PP_NEEDAUTH;
   2505  1.198  yamaguch 	sppp_open_event(sp, xcp);
   2506    1.3  explorer }
   2507    1.3  explorer 
   2508    1.3  explorer /*
   2509    1.3  explorer  * Analyze a configure request.  Return true if it was agreeable, and
   2510    1.3  explorer  * caused action sca, false if it has been rejected or nak'ed, and
   2511    1.3  explorer  * caused action scn.  (The return value is used to make the state
   2512    1.3  explorer  * transition decision in the state automaton.)
   2513    1.3  explorer  */
   2514    1.3  explorer static int
   2515  1.193  yamaguch sppp_lcp_RCR(struct sppp *sp, struct lcp_header *h, int origlen)
   2516    1.3  explorer {
   2517    1.3  explorer 	STDDCL;
   2518  1.193  yamaguch 	u_char *buf, *r, *p, l, blen, type;
   2519  1.193  yamaguch 	int len, rlen;
   2520  1.109      matt 	uint32_t nmagic;
   2521    1.3  explorer 	u_short authproto;
   2522    1.3  explorer 
   2523  1.170  knakahar 	KASSERT(SPPP_WLOCKED(sp));
   2524  1.163  knakahar 
   2525  1.193  yamaguch 	if (origlen < sizeof(*h))
   2526  1.193  yamaguch 		return 0;
   2527  1.193  yamaguch 
   2528  1.193  yamaguch 	origlen -= sizeof(*h);
   2529  1.193  yamaguch 	type = 0;
   2530  1.193  yamaguch 
   2531  1.193  yamaguch 	if (origlen <= 0)
   2532  1.193  yamaguch 		return 0;
   2533  1.193  yamaguch 	else
   2534  1.193  yamaguch 		blen = origlen;
   2535  1.193  yamaguch 
   2536  1.193  yamaguch 	buf = kmem_intr_alloc(blen, KM_NOSLEEP);
   2537  1.193  yamaguch 	if (buf == NULL)
   2538  1.193  yamaguch 		return 0;
   2539    1.3  explorer 
   2540    1.3  explorer 	if (debug)
   2541   1.64    itojun 		log(LOG_DEBUG, "%s: lcp parse opts:",
   2542   1.64    itojun 		    ifp->if_xname);
   2543    1.3  explorer 
   2544    1.3  explorer 	/* pass 1: check for things that need to be rejected */
   2545   1.59    itojun 	p = (void *)(h + 1);
   2546  1.193  yamaguch 	r = buf;
   2547  1.193  yamaguch 	rlen = 0;
   2548  1.193  yamaguch 	for (len = origlen; len > 1; len-= l, p += l) {
   2549  1.193  yamaguch 		l = p[1];
   2550  1.193  yamaguch 		if (l == 0)
   2551  1.193  yamaguch 			break;
   2552  1.193  yamaguch 
   2553   1.96   adrianp 		/* Sanity check option length */
   2554  1.140  christos 		if (l > len) {
   2555   1.96   adrianp 			/*
   2556  1.102       wiz 			 * Malicious option - drop immediately.
   2557   1.96   adrianp 			 * XXX Maybe we should just RXJ it?
   2558   1.96   adrianp 			 */
   2559   1.96   adrianp 			addlog("%s: received malicious LCP option 0x%02x, "
   2560   1.96   adrianp 			    "length 0x%02x, (len: 0x%02x) dropping.\n", ifp->if_xname,
   2561  1.140  christos 			    p[0], l, len);
   2562  1.193  yamaguch 			rlen = -1;
   2563  1.193  yamaguch 			goto end;
   2564   1.96   adrianp 		}
   2565    1.3  explorer 		if (debug)
   2566    1.9    itojun 			addlog(" %s", sppp_lcp_opt_name(*p));
   2567  1.193  yamaguch 		switch (p[0]) {
   2568    1.3  explorer 		case LCP_OPT_MAGIC:
   2569    1.3  explorer 			/* Magic number. */
   2570    1.3  explorer 			/* fall through, both are same length */
   2571    1.3  explorer 		case LCP_OPT_ASYNC_MAP:
   2572    1.3  explorer 			/* Async control character map. */
   2573  1.140  christos 			if (len >= 6 || l == 6)
   2574    1.3  explorer 				continue;
   2575    1.3  explorer 			if (debug)
   2576    1.9    itojun 				addlog(" [invalid]");
   2577    1.3  explorer 			break;
   2578  1.188        is 		case LCP_OPT_MP_EID:
   2579  1.188        is 			if (len >= l && l >= 3) {
   2580  1.189        is 				switch (p[2]) {
   2581  1.189        is 				case 0: if (l==3+ 0) continue;break;
   2582  1.189        is 				case 2: if (l==3+ 4) continue;break;
   2583  1.189        is 				case 3: if (l==3+ 6) continue;break;
   2584  1.189        is 				case 6: if (l==3+16) continue;break;
   2585  1.189        is 				case 1: /* FALLTHROUGH */
   2586  1.189        is 				case 4: if (l<=3+20) continue;break;
   2587  1.189        is 				case 5: if (l<=3+15) continue;break;
   2588  1.189        is 				/* XXX should it be default: continue;? */
   2589  1.189        is 				}
   2590  1.188        is 			}
   2591  1.188        is 			if (debug)
   2592  1.189        is 				addlog(" [invalid class %d len %d]", p[2], l);
   2593  1.188        is 			break;
   2594  1.188        is 		case LCP_OPT_MP_SSNHF:
   2595  1.188        is 			if (len >= 2 && l == 2) {
   2596  1.188        is 				if (debug)
   2597  1.188        is 					addlog(" [rej]");
   2598  1.188        is 				break;
   2599  1.188        is 			}
   2600  1.188        is 			if (debug)
   2601  1.188        is 				addlog(" [invalid]");
   2602  1.188        is 			break;
   2603  1.188        is 		case LCP_OPT_MP_MRRU:
   2604  1.188        is 			/* Multilink maximum received reconstructed unit */
   2605  1.188        is 			/* should be fall through, both are same length */
   2606  1.189        is 			/* FALLTHROUGH */
   2607    1.3  explorer 		case LCP_OPT_MRU:
   2608    1.3  explorer 			/* Maximum receive unit. */
   2609  1.140  christos 			if (len >= 4 && l == 4)
   2610    1.3  explorer 				continue;
   2611    1.3  explorer 			if (debug)
   2612    1.9    itojun 				addlog(" [invalid]");
   2613    1.3  explorer 			break;
   2614    1.3  explorer 		case LCP_OPT_AUTH_PROTO:
   2615    1.3  explorer 			if (len < 4) {
   2616    1.3  explorer 				if (debug)
   2617    1.9    itojun 					addlog(" [invalid]");
   2618    1.3  explorer 				break;
   2619    1.3  explorer 			}
   2620    1.3  explorer 			authproto = (p[2] << 8) + p[3];
   2621  1.140  christos 			if (authproto == PPP_CHAP && l != 5) {
   2622    1.3  explorer 				if (debug)
   2623    1.9    itojun 					addlog(" [invalid chap len]");
   2624    1.3  explorer 				break;
   2625    1.3  explorer 			}
   2626    1.3  explorer 			if (sp->myauth.proto == 0) {
   2627    1.3  explorer 				/* we are not configured to do auth */
   2628    1.3  explorer 				if (debug)
   2629    1.9    itojun 					addlog(" [not configured]");
   2630    1.3  explorer 				break;
   2631    1.3  explorer 			}
   2632    1.3  explorer 			/*
   2633    1.3  explorer 			 * Remote want us to authenticate, remember this,
   2634   1.36    martin 			 * so we stay in SPPP_PHASE_AUTHENTICATE after LCP got
   2635    1.3  explorer 			 * up.
   2636    1.3  explorer 			 */
   2637    1.3  explorer 			sp->pp_flags |= PP_NEEDAUTH;
   2638    1.3  explorer 			continue;
   2639    1.3  explorer 		default:
   2640    1.3  explorer 			/* Others not supported. */
   2641    1.3  explorer 			if (debug)
   2642    1.9    itojun 				addlog(" [rej]");
   2643    1.3  explorer 			break;
   2644    1.3  explorer 		}
   2645  1.140  christos 		if (rlen + l > blen) {
   2646  1.140  christos 			if (debug)
   2647  1.140  christos 				addlog(" [overflow]");
   2648  1.140  christos 			continue;
   2649  1.140  christos 		}
   2650    1.3  explorer 		/* Add the option to rejected list. */
   2651  1.140  christos 		memcpy(r, p, l);
   2652  1.140  christos 		r += l;
   2653  1.140  christos 		rlen += l;
   2654    1.3  explorer 	}
   2655  1.193  yamaguch 
   2656  1.193  yamaguch 	if (rlen > 0) {
   2657  1.193  yamaguch 		type = CONF_REJ;
   2658    1.9    itojun 		goto end;
   2659  1.193  yamaguch 	}
   2660  1.193  yamaguch 
   2661  1.193  yamaguch 	if (debug)
   2662    1.3  explorer 		addlog("\n");
   2663    1.3  explorer 
   2664    1.3  explorer 	/*
   2665    1.3  explorer 	 * pass 2: check for option values that are unacceptable and
   2666    1.3  explorer 	 * thus require to be nak'ed.
   2667    1.3  explorer 	 */
   2668    1.3  explorer 	if (debug)
   2669   1.64    itojun 		log(LOG_DEBUG, "%s: lcp parse opt values: ",
   2670   1.64    itojun 		    ifp->if_xname);
   2671    1.3  explorer 
   2672   1.59    itojun 	p = (void *)(h + 1);
   2673  1.193  yamaguch 	r = buf;
   2674  1.193  yamaguch 	rlen = 0;
   2675  1.193  yamaguch 	for (len = origlen; len > 0; len -= l, p += l) {
   2676  1.193  yamaguch 		l = p[1];
   2677  1.193  yamaguch 		if (l == 0)
   2678  1.193  yamaguch 			break;
   2679  1.193  yamaguch 
   2680    1.3  explorer 		if (debug)
   2681    1.9    itojun 			addlog(" %s", sppp_lcp_opt_name(*p));
   2682  1.193  yamaguch 		switch (p[0]) {
   2683    1.3  explorer 		case LCP_OPT_MAGIC:
   2684    1.3  explorer 			/* Magic number -- extract. */
   2685  1.109      matt 			nmagic = (uint32_t)p[2] << 24 |
   2686  1.109      matt 				(uint32_t)p[3] << 16 | p[4] << 8 | p[5];
   2687    1.3  explorer 			if (nmagic != sp->lcp.magic) {
   2688    1.3  explorer 				if (debug)
   2689   1.30      ross 					addlog(" 0x%x", nmagic);
   2690    1.3  explorer 				continue;
   2691    1.3  explorer 			}
   2692    1.3  explorer 			/*
   2693    1.3  explorer 			 * Local and remote magics equal -- loopback?
   2694    1.3  explorer 			 */
   2695   1.68    martin 			if (sp->pp_loopcnt >= LOOPALIVECNT*5) {
   2696   1.64    itojun 				printf ("%s: loopback\n",
   2697   1.64    itojun 					ifp->if_xname);
   2698    1.3  explorer 				sp->pp_loopcnt = 0;
   2699    1.3  explorer 				if (ifp->if_flags & IFF_UP) {
   2700  1.170  knakahar 					SPPP_UNLOCK(sp);
   2701    1.3  explorer 					if_down(ifp);
   2702  1.170  knakahar 					SPPP_LOCK(sp, RW_WRITER);
   2703  1.170  knakahar 
   2704   1.26    itojun 					IF_PURGE(&sp->pp_cpq);
   2705    1.3  explorer 					/* XXX ? */
   2706  1.198  yamaguch 					sppp_wq_add(sp->wq_cp,
   2707  1.198  yamaguch 					    &sp->scp[IDX_LCP].work_down);
   2708  1.198  yamaguch 					sppp_wq_add(sp->wq_cp,
   2709  1.198  yamaguch 					    &sp->scp[IDX_LCP].work_up);
   2710    1.3  explorer 				}
   2711    1.3  explorer 			} else if (debug)
   2712    1.9    itojun 				addlog(" [glitch]");
   2713    1.3  explorer 			++sp->pp_loopcnt;
   2714    1.3  explorer 			/*
   2715    1.3  explorer 			 * We negate our magic here, and NAK it.  If
   2716    1.3  explorer 			 * we see it later in an NAK packet, we
   2717    1.3  explorer 			 * suggest a new one.
   2718    1.3  explorer 			 */
   2719    1.3  explorer 			nmagic = ~sp->lcp.magic;
   2720    1.3  explorer 			/* Gonna NAK it. */
   2721    1.3  explorer 			p[2] = nmagic >> 24;
   2722    1.3  explorer 			p[3] = nmagic >> 16;
   2723    1.3  explorer 			p[4] = nmagic >> 8;
   2724    1.3  explorer 			p[5] = nmagic;
   2725    1.3  explorer 			break;
   2726    1.3  explorer 
   2727    1.3  explorer 		case LCP_OPT_ASYNC_MAP:
   2728   1.56  christos 			/*
   2729   1.56  christos 			 * Async control character map -- just ignore it.
   2730   1.56  christos 			 *
   2731   1.56  christos 			 * Quote from RFC 1662, chapter 6:
   2732   1.56  christos 			 * To enable this functionality, synchronous PPP
   2733   1.56  christos 			 * implementations MUST always respond to the
   2734   1.56  christos 			 * Async-Control-Character-Map Configuration
   2735   1.56  christos 			 * Option with the LCP Configure-Ack.  However,
   2736   1.56  christos 			 * acceptance of the Configuration Option does
   2737   1.56  christos 			 * not imply that the synchronous implementation
   2738   1.56  christos 			 * will do any ACCM mapping.  Instead, all such
   2739   1.56  christos 			 * octet mapping will be performed by the
   2740   1.56  christos 			 * asynchronous-to-synchronous converter.
   2741   1.56  christos 			 */
   2742   1.55  christos 			continue;
   2743    1.3  explorer 
   2744    1.3  explorer 		case LCP_OPT_MRU:
   2745    1.3  explorer 			/*
   2746    1.3  explorer 			 * Maximum receive unit.  Always agreeable,
   2747    1.3  explorer 			 * but ignored by now.
   2748    1.3  explorer 			 */
   2749    1.3  explorer 			sp->lcp.their_mru = p[2] * 256 + p[3];
   2750    1.3  explorer 			if (debug)
   2751    1.9    itojun 				addlog(" %ld", sp->lcp.their_mru);
   2752    1.3  explorer 			continue;
   2753    1.3  explorer 
   2754    1.3  explorer 		case LCP_OPT_AUTH_PROTO:
   2755    1.3  explorer 			authproto = (p[2] << 8) + p[3];
   2756    1.3  explorer 			if (sp->myauth.proto != authproto) {
   2757    1.3  explorer 				/* not agreed, nak */
   2758    1.3  explorer 				if (debug)
   2759    1.9    itojun 					addlog(" [mine %s != his %s]",
   2760   1.71       oki 					       sppp_proto_name(sp->myauth.proto),
   2761    1.3  explorer 					       sppp_proto_name(authproto));
   2762    1.3  explorer 				p[2] = sp->myauth.proto >> 8;
   2763    1.3  explorer 				p[3] = sp->myauth.proto;
   2764    1.3  explorer 				break;
   2765    1.3  explorer 			}
   2766    1.3  explorer 			if (authproto == PPP_CHAP && p[4] != CHAP_MD5) {
   2767    1.3  explorer 				if (debug)
   2768    1.9    itojun 					addlog(" [chap not MD5]");
   2769    1.3  explorer 				p[4] = CHAP_MD5;
   2770    1.3  explorer 				break;
   2771    1.3  explorer 			}
   2772    1.3  explorer 			continue;
   2773  1.189        is 		case LCP_OPT_MP_EID:
   2774  1.189        is 			/*
   2775  1.189        is 			 * Endpoint identification.
   2776  1.189        is 			 * Always agreeable,
   2777  1.189        is 			 * but ignored by now.
   2778  1.189        is 			 */
   2779  1.189        is 			if (debug) {
   2780  1.189        is 				addlog(" type %d", p[2]);
   2781  1.189        is 				sppp_print_bytes(p+3, p[1]-3);
   2782  1.189        is 			}
   2783  1.189        is 			continue;
   2784  1.189        is 		case LCP_OPT_MP_MRRU:
   2785  1.189        is 			/*
   2786  1.189        is 			 * Maximum received reconstructed unit.
   2787  1.189        is 			 * Always agreeable,
   2788  1.189        is 			 * but ignored by now.
   2789  1.189        is 			 */
   2790  1.189        is 			sp->lcp.their_mrru = p[2] * 256 + p[3];
   2791  1.189        is 			if (debug)
   2792  1.189        is 				addlog(" %ld", sp->lcp.their_mrru);
   2793  1.189        is 			continue;
   2794    1.3  explorer 		}
   2795  1.140  christos 		if (rlen + l > blen) {
   2796  1.140  christos 			if (debug)
   2797  1.140  christos 				addlog(" [overflow]");
   2798  1.140  christos 			continue;
   2799  1.140  christos 		}
   2800    1.3  explorer 		/* Add the option to nak'ed list. */
   2801  1.140  christos 		memcpy(r, p, l);
   2802  1.140  christos 		r += l;
   2803  1.140  christos 		rlen += l;
   2804    1.3  explorer 	}
   2805  1.193  yamaguch 
   2806  1.193  yamaguch 	if (rlen > 0) {
   2807  1.192  yamaguch 		if (++sp->scp[IDX_LCP].fail_counter >= sp->lcp.max_failure) {
   2808    1.3  explorer 			if (debug)
   2809  1.193  yamaguch 				addlog(" max_failure (%d) exceeded, ",
   2810  1.193  yamaguch 				    sp->lcp.max_failure);
   2811  1.193  yamaguch 			type = CONF_REJ;
   2812    1.3  explorer 		} else {
   2813  1.193  yamaguch 			type = CONF_NAK;
   2814    1.3  explorer 		}
   2815    1.3  explorer 	} else {
   2816  1.193  yamaguch 		type = CONF_ACK;
   2817  1.193  yamaguch 		rlen = origlen;
   2818  1.193  yamaguch 		memcpy(r, h + 1, rlen);
   2819  1.192  yamaguch 		sp->scp[IDX_LCP].fail_counter = 0;
   2820    1.3  explorer 		sp->pp_loopcnt = 0;
   2821    1.3  explorer 	}
   2822    1.3  explorer 
   2823  1.193  yamaguch end:
   2824  1.193  yamaguch 	if (debug)
   2825  1.193  yamaguch 		addlog("\n");
   2826  1.193  yamaguch 
   2827  1.194  yamaguch 	if (sp->scp[IDX_LCP].rcr_buf != NULL) {
   2828  1.194  yamaguch 		kmem_intr_free(sp->scp[IDX_LCP].rcr_buf,
   2829  1.194  yamaguch 		    sp->scp[IDX_LCP].rcr_blen);
   2830  1.194  yamaguch 	}
   2831   1.96   adrianp 
   2832  1.194  yamaguch 	if (rlen < 0) {
   2833  1.194  yamaguch 		kmem_intr_free(buf, blen);
   2834  1.194  yamaguch 		sp->scp[IDX_IPCP].rcr_buf = NULL;
   2835  1.194  yamaguch 		sp->scp[IDX_IPCP].rcr_rlen = 0;
   2836  1.193  yamaguch 		return -1;
   2837  1.194  yamaguch 	}
   2838  1.194  yamaguch 
   2839  1.194  yamaguch 	sp->scp[IDX_LCP].rcr_type = type;
   2840  1.194  yamaguch 	sp->scp[IDX_LCP].rcr_buf = buf;
   2841  1.194  yamaguch 	sp->scp[IDX_LCP].rcr_blen = blen;
   2842  1.194  yamaguch 	sp->scp[IDX_LCP].rcr_rlen = rlen;
   2843  1.194  yamaguch 
   2844  1.193  yamaguch 	if (type != CONF_ACK)
   2845  1.193  yamaguch 		return 0;
   2846  1.193  yamaguch 	return 1;
   2847    1.3  explorer }
   2848    1.3  explorer 
   2849    1.3  explorer /*
   2850    1.3  explorer  * Analyze the LCP Configure-Reject option list, and adjust our
   2851    1.3  explorer  * negotiation.
   2852    1.3  explorer  */
   2853    1.3  explorer static void
   2854    1.3  explorer sppp_lcp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len)
   2855    1.3  explorer {
   2856    1.3  explorer 	STDDCL;
   2857  1.193  yamaguch 	u_char *p, l;
   2858    1.3  explorer 
   2859  1.170  knakahar 	KASSERT(SPPP_WLOCKED(sp));
   2860  1.163  knakahar 
   2861  1.193  yamaguch 	if (len <= sizeof(*h))
   2862    1.3  explorer 		return;
   2863    1.3  explorer 
   2864  1.193  yamaguch 	len -= sizeof(*h);
   2865  1.193  yamaguch 
   2866    1.3  explorer 	if (debug)
   2867   1.64    itojun 		log(LOG_DEBUG, "%s: lcp rej opts:",
   2868   1.64    itojun 		    ifp->if_xname);
   2869    1.3  explorer 
   2870   1.59    itojun 	p = (void *)(h + 1);
   2871  1.140  christos 	for (; len > 1 && (l = p[1]) != 0; len -= l, p += l) {
   2872   1.96   adrianp 		/* Sanity check option length */
   2873  1.140  christos 		if (l > len) {
   2874   1.96   adrianp 			/*
   2875  1.102       wiz 			 * Malicious option - drop immediately.
   2876   1.96   adrianp 			 * XXX Maybe we should just RXJ it?
   2877   1.96   adrianp 			 */
   2878   1.96   adrianp 			addlog("%s: received malicious LCP option, "
   2879   1.96   adrianp 			    "dropping.\n", ifp->if_xname);
   2880  1.193  yamaguch 			goto end;
   2881   1.96   adrianp 		}
   2882    1.3  explorer 		if (debug)
   2883    1.9    itojun 			addlog(" %s", sppp_lcp_opt_name(*p));
   2884  1.193  yamaguch 		switch (p[0]) {
   2885    1.3  explorer 		case LCP_OPT_MAGIC:
   2886    1.3  explorer 			/* Magic number -- can't use it, use 0 */
   2887    1.3  explorer 			sp->lcp.opts &= ~(1 << LCP_OPT_MAGIC);
   2888    1.3  explorer 			sp->lcp.magic = 0;
   2889    1.3  explorer 			break;
   2890    1.3  explorer 		case LCP_OPT_MRU:
   2891    1.3  explorer 			/*
   2892  1.105       scw 			 * We try to negotiate a lower MRU if the underlying
   2893  1.105       scw 			 * link's MTU is less than PP_MTU (e.g. PPPoE). If the
   2894  1.105       scw 			 * peer rejects this lower rate, fallback to the
   2895  1.105       scw 			 * default.
   2896    1.3  explorer 			 */
   2897  1.105       scw 			if (debug) {
   2898  1.105       scw 				addlog("%s: warning: peer rejected our MRU of "
   2899  1.105       scw 				    "%ld bytes. Defaulting to %d bytes\n",
   2900  1.105       scw 				    ifp->if_xname, sp->lcp.mru, PP_MTU);
   2901  1.105       scw 			}
   2902    1.3  explorer 			sp->lcp.opts &= ~(1 << LCP_OPT_MRU);
   2903  1.105       scw 			sp->lcp.mru = PP_MTU;
   2904    1.3  explorer 			break;
   2905    1.3  explorer 		case LCP_OPT_AUTH_PROTO:
   2906    1.3  explorer 			/*
   2907    1.3  explorer 			 * Peer doesn't want to authenticate himself,
   2908    1.3  explorer 			 * deny unless this is a dialout call, and
   2909   1.36    martin 			 * SPPP_AUTHFLAG_NOCALLOUT is set.
   2910    1.3  explorer 			 */
   2911    1.3  explorer 			if ((sp->pp_flags & PP_CALLIN) == 0 &&
   2912   1.36    martin 			    (sp->hisauth.flags & SPPP_AUTHFLAG_NOCALLOUT) != 0) {
   2913    1.3  explorer 				if (debug)
   2914    1.9    itojun 					addlog(" [don't insist on auth "
   2915    1.3  explorer 					       "for callout]");
   2916    1.3  explorer 				sp->lcp.opts &= ~(1 << LCP_OPT_AUTH_PROTO);
   2917    1.3  explorer 				break;
   2918    1.3  explorer 			}
   2919    1.3  explorer 			if (debug)
   2920    1.3  explorer 				addlog("[access denied]\n");
   2921  1.198  yamaguch 			sppp_wq_add(sp->wq_cp, &sp->scp[IDX_LCP].work_close);
   2922    1.3  explorer 			break;
   2923    1.3  explorer 		}
   2924    1.3  explorer 	}
   2925    1.3  explorer 	if (debug)
   2926    1.3  explorer 		addlog("\n");
   2927  1.193  yamaguch end:
   2928    1.3  explorer 	return;
   2929    1.3  explorer }
   2930    1.3  explorer 
   2931    1.3  explorer /*
   2932    1.3  explorer  * Analyze the LCP Configure-NAK option list, and adjust our
   2933    1.3  explorer  * negotiation.
   2934    1.3  explorer  */
   2935    1.3  explorer static void
   2936    1.3  explorer sppp_lcp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len)
   2937    1.3  explorer {
   2938    1.3  explorer 	STDDCL;
   2939  1.193  yamaguch 	u_char *p, l;
   2940  1.109      matt 	uint32_t magic;
   2941    1.3  explorer 
   2942  1.170  knakahar 	KASSERT(SPPP_WLOCKED(sp));
   2943  1.163  knakahar 
   2944  1.193  yamaguch 	if (len <= sizeof(*h))
   2945    1.3  explorer 		return;
   2946  1.193  yamaguch 	len -= sizeof(*h);
   2947    1.3  explorer 
   2948    1.3  explorer 	if (debug)
   2949   1.64    itojun 		log(LOG_DEBUG, "%s: lcp nak opts:",
   2950   1.64    itojun 		    ifp->if_xname);
   2951    1.3  explorer 
   2952   1.59    itojun 	p = (void *)(h + 1);
   2953  1.140  christos 	for (; len > 1 && (l = p[1]) != 0; len -= l, p += l) {
   2954   1.96   adrianp 		/* Sanity check option length */
   2955  1.140  christos 		if (l > len) {
   2956   1.96   adrianp 			/*
   2957  1.102       wiz 			 * Malicious option - drop immediately.
   2958   1.96   adrianp 			 * XXX Maybe we should just RXJ it?
   2959   1.96   adrianp 			 */
   2960   1.96   adrianp 			addlog("%s: received malicious LCP option, "
   2961   1.96   adrianp 			    "dropping.\n", ifp->if_xname);
   2962  1.193  yamaguch 			goto end;
   2963   1.96   adrianp 		}
   2964    1.3  explorer 		if (debug)
   2965    1.9    itojun 			addlog(" %s", sppp_lcp_opt_name(*p));
   2966  1.193  yamaguch 		switch (p[0]) {
   2967    1.3  explorer 		case LCP_OPT_MAGIC:
   2968    1.3  explorer 			/* Magic number -- renegotiate */
   2969    1.3  explorer 			if ((sp->lcp.opts & (1 << LCP_OPT_MAGIC)) &&
   2970  1.140  christos 			    len >= 6 && l == 6) {
   2971  1.109      matt 				magic = (uint32_t)p[2] << 24 |
   2972  1.109      matt 					(uint32_t)p[3] << 16 | p[4] << 8 | p[5];
   2973    1.3  explorer 				/*
   2974    1.3  explorer 				 * If the remote magic is our negated one,
   2975    1.3  explorer 				 * this looks like a loopback problem.
   2976    1.3  explorer 				 * Suggest a new magic to make sure.
   2977    1.3  explorer 				 */
   2978    1.3  explorer 				if (magic == ~sp->lcp.magic) {
   2979    1.3  explorer 					if (debug)
   2980    1.9    itojun 						addlog(" magic glitch");
   2981  1.124       tls 					sp->lcp.magic = cprng_fast32();
   2982    1.3  explorer 				} else {
   2983    1.3  explorer 					sp->lcp.magic = magic;
   2984    1.3  explorer 					if (debug)
   2985   1.30      ross 						addlog(" %d", magic);
   2986    1.3  explorer 				}
   2987    1.3  explorer 			}
   2988    1.3  explorer 			break;
   2989    1.3  explorer 		case LCP_OPT_MRU:
   2990    1.3  explorer 			/*
   2991    1.3  explorer 			 * Peer wants to advise us to negotiate an MRU.
   2992    1.3  explorer 			 * Agree on it if it's reasonable, or use
   2993    1.3  explorer 			 * default otherwise.
   2994    1.3  explorer 			 */
   2995  1.140  christos 			if (len >= 4 && l == 4) {
   2996    1.3  explorer 				u_int mru = p[2] * 256 + p[3];
   2997    1.3  explorer 				if (debug)
   2998    1.9    itojun 					addlog(" %d", mru);
   2999  1.105       scw 				if (mru < PPP_MINMRU || mru > sp->pp_if.if_mtu)
   3000  1.105       scw 					mru = sp->pp_if.if_mtu;
   3001    1.3  explorer 				sp->lcp.mru = mru;
   3002    1.3  explorer 				sp->lcp.opts |= (1 << LCP_OPT_MRU);
   3003    1.3  explorer 			}
   3004    1.3  explorer 			break;
   3005    1.3  explorer 		case LCP_OPT_AUTH_PROTO:
   3006    1.3  explorer 			/*
   3007    1.3  explorer 			 * Peer doesn't like our authentication method,
   3008    1.3  explorer 			 * deny.
   3009    1.3  explorer 			 */
   3010    1.3  explorer 			if (debug)
   3011    1.3  explorer 				addlog("[access denied]\n");
   3012  1.198  yamaguch 			sppp_wq_add(sp->wq_cp, &sp->scp[IDX_LCP].work_close);
   3013    1.3  explorer 			break;
   3014    1.3  explorer 		}
   3015    1.3  explorer 	}
   3016    1.3  explorer 	if (debug)
   3017    1.3  explorer 		addlog("\n");
   3018  1.193  yamaguch end:
   3019    1.3  explorer 	return;
   3020    1.3  explorer }
   3021    1.3  explorer 
   3022    1.3  explorer static void
   3023    1.3  explorer sppp_lcp_tlu(struct sppp *sp)
   3024    1.3  explorer {
   3025  1.149     joerg 	struct ifnet *ifp = &sp->pp_if;
   3026    1.3  explorer 	int i;
   3027    1.3  explorer 
   3028  1.170  knakahar 	KASSERT(SPPP_WLOCKED(sp));
   3029  1.163  knakahar 
   3030    1.3  explorer 	/* XXX ? */
   3031    1.3  explorer 	if (! (ifp->if_flags & IFF_UP) &&
   3032    1.3  explorer 	    (ifp->if_flags & IFF_RUNNING)) {
   3033    1.3  explorer 		/* Coming out of loopback mode. */
   3034  1.170  knakahar 		SPPP_UNLOCK(sp);
   3035    1.3  explorer 		if_up(ifp);
   3036  1.170  knakahar 		SPPP_LOCK(sp, RW_WRITER);
   3037    1.3  explorer 	}
   3038    1.3  explorer 
   3039    1.3  explorer 	for (i = 0; i < IDX_COUNT; i++)
   3040  1.198  yamaguch 		if ((cps[i])->flags & CP_QUAL) {
   3041  1.198  yamaguch 			(cps[i])->Open(sp, __UNCONST(&cps[i]));
   3042  1.198  yamaguch 		}
   3043    1.3  explorer 
   3044    1.3  explorer 	if ((sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) != 0 ||
   3045    1.3  explorer 	    (sp->pp_flags & PP_NEEDAUTH) != 0)
   3046  1.149     joerg 		sppp_change_phase(sp, SPPP_PHASE_AUTHENTICATE);
   3047    1.3  explorer 	else
   3048  1.149     joerg 		sppp_change_phase(sp, SPPP_PHASE_NETWORK);
   3049    1.3  explorer 
   3050    1.3  explorer 	/*
   3051    1.3  explorer 	 * Open all authentication protocols.  This is even required
   3052    1.3  explorer 	 * if we already proceeded to network phase, since it might be
   3053    1.3  explorer 	 * that remote wants us to authenticate, so we might have to
   3054    1.3  explorer 	 * send a PAP request.  Undesired authentication protocols
   3055    1.3  explorer 	 * don't do anything when they get an Open event.
   3056    1.3  explorer 	 */
   3057    1.3  explorer 	for (i = 0; i < IDX_COUNT; i++)
   3058  1.201  yamaguch 		if ((cps[i])->flags & CP_AUTH) {
   3059  1.201  yamaguch 			sppp_wq_add(sp->wq_cp,
   3060  1.201  yamaguch 			    &sp->scp[(cps[i])->protoidx].work_open);
   3061  1.201  yamaguch 		}
   3062    1.3  explorer 
   3063   1.36    martin 	if (sp->pp_phase == SPPP_PHASE_NETWORK) {
   3064    1.3  explorer 		/* Notify all NCPs. */
   3065    1.3  explorer 		for (i = 0; i < IDX_COUNT; i++)
   3066  1.201  yamaguch 			if ((cps[i])->flags & CP_NCP) {
   3067  1.201  yamaguch 			sppp_wq_add(sp->wq_cp,
   3068  1.201  yamaguch 			    &sp->scp[(cps[i])->protoidx].work_open);
   3069  1.201  yamaguch 			}
   3070  1.163  knakahar 	}
   3071    1.3  explorer 
   3072    1.3  explorer 	/* notify low-level driver of state change */
   3073  1.170  knakahar 	sppp_notify_chg_wlocked(sp);
   3074    1.3  explorer }
   3075    1.3  explorer 
   3076    1.3  explorer static void
   3077    1.3  explorer sppp_lcp_tld(struct sppp *sp)
   3078    1.3  explorer {
   3079  1.201  yamaguch 	int i, pi, phase;
   3080    1.3  explorer 
   3081  1.170  knakahar 	KASSERT(SPPP_WLOCKED(sp));
   3082  1.163  knakahar 
   3083  1.201  yamaguch 	phase = sp->pp_phase;
   3084  1.201  yamaguch 
   3085  1.149     joerg 	sppp_change_phase(sp, SPPP_PHASE_TERMINATE);
   3086    1.3  explorer 
   3087    1.3  explorer 	/*
   3088    1.3  explorer 	 * Take upper layers down.  We send the Down event first and
   3089    1.3  explorer 	 * the Close second to prevent the upper layers from sending
   3090    1.3  explorer 	 * ``a flurry of terminate-request packets'', as the RFC
   3091    1.3  explorer 	 * describes it.
   3092    1.3  explorer 	 */
   3093  1.201  yamaguch 	for (i = 0; i < IDX_COUNT; i++) {
   3094  1.201  yamaguch 		pi = (cps[i])->protoidx;
   3095  1.201  yamaguch 		if (((cps[i])->flags & CP_LCP) == 0) {
   3096  1.201  yamaguch 			/* skip if ncp was not started */
   3097  1.201  yamaguch 			if (phase != SPPP_PHASE_NETWORK &&
   3098  1.201  yamaguch 			    ((cps[i])->flags & CP_NCP) != 0)
   3099  1.201  yamaguch 				continue;
   3100  1.201  yamaguch 
   3101  1.201  yamaguch 			sppp_wq_add(sp->wq_cp, &sp->scp[pi].work_down);
   3102  1.201  yamaguch 			sppp_wq_add(sp->wq_cp, &sp->scp[pi].work_close);
   3103    1.3  explorer 		}
   3104  1.163  knakahar 	}
   3105    1.3  explorer }
   3106    1.3  explorer 
   3107    1.3  explorer static void
   3108  1.202  yamaguch sppp_lcp_tls(const struct cp *cp __unused, struct sppp *sp)
   3109    1.3  explorer {
   3110    1.3  explorer 
   3111  1.170  knakahar 	KASSERT(SPPP_WLOCKED(sp));
   3112  1.170  knakahar 
   3113   1.39    martin 	if (sp->pp_max_auth_fail != 0 && sp->pp_auth_failures >= sp->pp_max_auth_fail) {
   3114  1.163  knakahar 		printf("%s: authentication failed %d times, not retrying again\n",
   3115   1.39    martin 		sp->pp_if.if_xname, sp->pp_auth_failures);
   3116  1.170  knakahar 
   3117  1.170  knakahar 		SPPP_UNLOCK(sp);
   3118  1.163  knakahar 		if_down(&sp->pp_if);
   3119  1.170  knakahar 		SPPP_LOCK(sp, RW_WRITER);
   3120  1.163  knakahar 		return;
   3121   1.39    martin 	}
   3122   1.39    martin 
   3123  1.149     joerg 	sppp_change_phase(sp, SPPP_PHASE_ESTABLISH);
   3124    1.3  explorer 
   3125    1.3  explorer 	/* Notify lower layer if desired. */
   3126  1.170  knakahar 	sppp_notify_tls_wlocked(sp);
   3127    1.3  explorer }
   3128    1.3  explorer 
   3129    1.3  explorer static void
   3130  1.202  yamaguch sppp_lcp_tlf(const struct cp *cp __unused, struct sppp *sp)
   3131    1.3  explorer {
   3132  1.170  knakahar 
   3133  1.170  knakahar 	KASSERT(SPPP_WLOCKED(sp));
   3134    1.3  explorer 
   3135  1.149     joerg 	sppp_change_phase(sp, SPPP_PHASE_DEAD);
   3136    1.3  explorer 
   3137    1.3  explorer 	/* Notify lower layer if desired. */
   3138  1.170  knakahar 	sppp_notify_tlf_wlocked(sp);
   3139    1.3  explorer }
   3140    1.3  explorer 
   3141    1.3  explorer static void
   3142    1.3  explorer sppp_lcp_scr(struct sppp *sp)
   3143    1.3  explorer {
   3144    1.3  explorer 	char opt[6 /* magicnum */ + 4 /* mru */ + 5 /* chap */];
   3145    1.3  explorer 	int i = 0;
   3146    1.3  explorer 	u_short authproto;
   3147    1.3  explorer 
   3148  1.170  knakahar 	KASSERT(SPPP_WLOCKED(sp));
   3149  1.163  knakahar 
   3150    1.3  explorer 	if (sp->lcp.opts & (1 << LCP_OPT_MAGIC)) {
   3151    1.3  explorer 		if (! sp->lcp.magic)
   3152  1.124       tls 			sp->lcp.magic = cprng_fast32();
   3153    1.3  explorer 		opt[i++] = LCP_OPT_MAGIC;
   3154    1.3  explorer 		opt[i++] = 6;
   3155    1.3  explorer 		opt[i++] = sp->lcp.magic >> 24;
   3156    1.3  explorer 		opt[i++] = sp->lcp.magic >> 16;
   3157    1.3  explorer 		opt[i++] = sp->lcp.magic >> 8;
   3158    1.3  explorer 		opt[i++] = sp->lcp.magic;
   3159    1.3  explorer 	}
   3160    1.3  explorer 
   3161    1.3  explorer 	if (sp->lcp.opts & (1 << LCP_OPT_MRU)) {
   3162    1.3  explorer 		opt[i++] = LCP_OPT_MRU;
   3163    1.3  explorer 		opt[i++] = 4;
   3164    1.3  explorer 		opt[i++] = sp->lcp.mru >> 8;
   3165    1.3  explorer 		opt[i++] = sp->lcp.mru;
   3166    1.3  explorer 	}
   3167    1.3  explorer 
   3168    1.3  explorer 	if (sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) {
   3169    1.3  explorer 		authproto = sp->hisauth.proto;
   3170    1.3  explorer 		opt[i++] = LCP_OPT_AUTH_PROTO;
   3171    1.3  explorer 		opt[i++] = authproto == PPP_CHAP? 5: 4;
   3172    1.3  explorer 		opt[i++] = authproto >> 8;
   3173    1.3  explorer 		opt[i++] = authproto;
   3174    1.3  explorer 		if (authproto == PPP_CHAP)
   3175    1.3  explorer 			opt[i++] = CHAP_MD5;
   3176    1.3  explorer 	}
   3177    1.3  explorer 
   3178  1.192  yamaguch 	sp->scp[IDX_LCP].confid = ++sp->scp[IDX_LCP].seq;
   3179  1.192  yamaguch 	sppp_cp_send(sp, PPP_LCP, CONF_REQ, sp->scp[IDX_LCP].confid, i, &opt);
   3180    1.3  explorer }
   3181    1.3  explorer 
   3182    1.3  explorer /*
   3183    1.3  explorer  * Check the open NCPs, return true if at least one NCP is open.
   3184    1.3  explorer  */
   3185    1.3  explorer 
   3186  1.201  yamaguch static int
   3187  1.202  yamaguch sppp_cp_check(struct sppp *sp, u_char cp_flags)
   3188  1.201  yamaguch {
   3189  1.201  yamaguch 	int i, mask;
   3190  1.201  yamaguch 
   3191  1.201  yamaguch 	for (i = 0, mask = 1; i < IDX_COUNT; i++, mask <<= 1)
   3192  1.202  yamaguch 		if ((sp->lcp.protos & mask) && (cps[i])->flags & cp_flags)
   3193  1.201  yamaguch 			return 1;
   3194  1.201  yamaguch 	return 0;
   3195  1.201  yamaguch }
   3196  1.201  yamaguch 
   3197    1.3  explorer /*
   3198    1.3  explorer  * Re-check the open NCPs and see if we should terminate the link.
   3199    1.3  explorer  * Called by the NCPs during their tlf action handling.
   3200    1.3  explorer  */
   3201    1.3  explorer static void
   3202    1.3  explorer sppp_lcp_check_and_close(struct sppp *sp)
   3203    1.3  explorer {
   3204  1.170  knakahar 
   3205  1.170  knakahar 	KASSERT(SPPP_WLOCKED(sp));
   3206    1.3  explorer 
   3207  1.201  yamaguch 	if (sp->pp_phase < SPPP_PHASE_AUTHENTICATE) {
   3208    1.3  explorer 		/* don't bother, we are already going down */
   3209    1.3  explorer 		return;
   3210  1.201  yamaguch 	}
   3211    1.3  explorer 
   3212  1.201  yamaguch 	if (sp->pp_phase == SPPP_PHASE_AUTHENTICATE &&
   3213  1.202  yamaguch 	    sppp_cp_check(sp, CP_AUTH))
   3214    1.3  explorer 		return;
   3215  1.201  yamaguch 
   3216  1.201  yamaguch 	if (sp->pp_phase >= SPPP_PHASE_NETWORK &&
   3217  1.202  yamaguch 	    sppp_cp_check(sp, CP_NCP))
   3218  1.201  yamaguch 		return;
   3219  1.201  yamaguch 
   3220  1.198  yamaguch 	sppp_wq_add(sp->wq_cp, &sp->scp[IDX_LCP].work_close);
   3221    1.3  explorer }
   3222    1.9    itojun 
   3223   1.42  jdolecek /*
   3224    1.3  explorer  *--------------------------------------------------------------------------*
   3225    1.3  explorer  *                                                                          *
   3226    1.3  explorer  *                        The IPCP implementation.                          *
   3227    1.3  explorer  *                                                                          *
   3228    1.3  explorer  *--------------------------------------------------------------------------*
   3229    1.3  explorer  */
   3230    1.3  explorer 
   3231    1.3  explorer static void
   3232    1.3  explorer sppp_ipcp_init(struct sppp *sp)
   3233    1.3  explorer {
   3234  1.159  knakahar 	int error;
   3235  1.159  knakahar 
   3236  1.170  knakahar 	KASSERT(SPPP_WLOCKED(sp));
   3237  1.170  knakahar 
   3238    1.3  explorer 	sp->ipcp.opts = 0;
   3239    1.3  explorer 	sp->ipcp.flags = 0;
   3240  1.192  yamaguch 	sp->scp[IDX_IPCP].state = STATE_INITIAL;
   3241  1.192  yamaguch 	sp->scp[IDX_IPCP].fail_counter = 0;
   3242  1.192  yamaguch 	sp->scp[IDX_IPCP].seq = 0;
   3243  1.192  yamaguch 	sp->scp[IDX_IPCP].rseq = 0;
   3244  1.198  yamaguch 	sppp_cp_init(&ipcp, sp);
   3245  1.159  knakahar 
   3246  1.179  knakahar 	error = workqueue_create(&sp->ipcp.update_addrs_wq, "ipcp_addr",
   3247  1.161  knakahar 	    sppp_update_ip_addrs_work, sp, PRI_SOFTNET, IPL_NET, 0);
   3248  1.159  knakahar 	if (error)
   3249  1.161  knakahar 		panic("%s: update_addrs workqueue_create failed (%d)\n",
   3250  1.159  knakahar 		    __func__, error);
   3251  1.161  knakahar 	sp->ipcp.update_addrs_q = pcq_create(IPCP_UPDATE_LIMIT, KM_SLEEP);
   3252  1.159  knakahar 
   3253  1.161  knakahar 	sp->ipcp.update_addrs_enqueued = 0;
   3254    1.3  explorer }
   3255    1.3  explorer 
   3256    1.3  explorer static void
   3257  1.198  yamaguch sppp_ipcp_open(struct sppp *sp, void *xcp)
   3258    1.3  explorer {
   3259    1.3  explorer 	STDDCL;
   3260  1.109      matt 	uint32_t myaddr, hisaddr;
   3261    1.3  explorer 
   3262  1.170  knakahar 	KASSERT(SPPP_WLOCKED(sp));
   3263  1.163  knakahar 
   3264   1.31    martin 	sp->ipcp.flags &= ~(IPCP_HISADDR_SEEN|IPCP_MYADDR_SEEN|IPCP_MYADDR_DYN|IPCP_HISADDR_DYN);
   3265   1.31    martin 	sp->ipcp.req_myaddr = 0;
   3266   1.31    martin 	sp->ipcp.req_hisaddr = 0;
   3267   1.45    martin 	memset(&sp->dns_addrs, 0, sizeof sp->dns_addrs);
   3268    1.3  explorer 
   3269   1.79  christos #ifdef INET
   3270    1.3  explorer 	sppp_get_ip_addrs(sp, &myaddr, &hisaddr, 0);
   3271   1.79  christos #else
   3272   1.79  christos 	myaddr = hisaddr = 0;
   3273   1.79  christos #endif
   3274    1.3  explorer 	/*
   3275    1.3  explorer 	 * If we don't have his address, this probably means our
   3276    1.3  explorer 	 * interface doesn't want to talk IP at all.  (This could
   3277    1.3  explorer 	 * be the case if somebody wants to speak only IPX, for
   3278    1.3  explorer 	 * example.)  Don't open IPCP in this case.
   3279    1.3  explorer 	 */
   3280   1.79  christos 	if (hisaddr == 0) {
   3281    1.3  explorer 		/* XXX this message should go away */
   3282    1.3  explorer 		if (debug)
   3283   1.64    itojun 			log(LOG_DEBUG, "%s: ipcp_open(): no IP interface\n",
   3284   1.64    itojun 			    ifp->if_xname);
   3285    1.3  explorer 		return;
   3286    1.3  explorer 	}
   3287    1.3  explorer 
   3288   1.31    martin 	if (myaddr == 0) {
   3289    1.3  explorer 		/*
   3290    1.3  explorer 		 * I don't have an assigned address, so i need to
   3291    1.3  explorer 		 * negotiate my address.
   3292    1.3  explorer 		 */
   3293    1.3  explorer 		sp->ipcp.flags |= IPCP_MYADDR_DYN;
   3294    1.3  explorer 		sp->ipcp.opts |= (1 << IPCP_OPT_ADDRESS);
   3295   1.31    martin 	}
   3296   1.31    martin 	if (hisaddr == 1) {
   3297   1.31    martin 		/*
   3298   1.31    martin 		 * XXX - remove this hack!
   3299   1.74       wiz 		 * remote has no valid address, we need to get one assigned.
   3300   1.31    martin 		 */
   3301   1.31    martin 		sp->ipcp.flags |= IPCP_HISADDR_DYN;
   3302   1.31    martin 	}
   3303  1.198  yamaguch 	sppp_open_event(sp, xcp);
   3304    1.3  explorer }
   3305    1.3  explorer 
   3306    1.3  explorer static void
   3307  1.198  yamaguch sppp_ipcp_close(struct sppp *sp, void *xcp)
   3308    1.3  explorer {
   3309   1.63      tron 
   3310  1.170  knakahar 	KASSERT(SPPP_WLOCKED(sp));
   3311  1.170  knakahar 
   3312  1.198  yamaguch 	sppp_close_event(sp, xcp);
   3313  1.163  knakahar 
   3314   1.79  christos #ifdef INET
   3315   1.31    martin 	if (sp->ipcp.flags & (IPCP_MYADDR_DYN|IPCP_HISADDR_DYN))
   3316    1.3  explorer 		/*
   3317   1.31    martin 		 * Some address was dynamic, clear it again.
   3318    1.3  explorer 		 */
   3319   1.31    martin 		sppp_clear_ip_addrs(sp);
   3320   1.79  christos #endif
   3321    1.3  explorer }
   3322    1.3  explorer 
   3323    1.3  explorer /*
   3324    1.3  explorer  * Analyze a configure request.  Return true if it was agreeable, and
   3325    1.3  explorer  * caused action sca, false if it has been rejected or nak'ed, and
   3326    1.3  explorer  * caused action scn.  (The return value is used to make the state
   3327    1.3  explorer  * transition decision in the state automaton.)
   3328    1.3  explorer  */
   3329    1.3  explorer static int
   3330  1.193  yamaguch sppp_ipcp_RCR(struct sppp *sp, struct lcp_header *h, int origlen)
   3331    1.3  explorer {
   3332  1.193  yamaguch 	u_char *buf, *r, *p, l, blen, type;
   3333    1.3  explorer 	struct ifnet *ifp = &sp->pp_if;
   3334  1.193  yamaguch 	int rlen, len, debug = ifp->if_flags & IFF_DEBUG;
   3335  1.109      matt 	uint32_t hisaddr, desiredaddr;
   3336    1.3  explorer 
   3337  1.170  knakahar 	KASSERT(SPPP_WLOCKED(sp));
   3338  1.170  knakahar 
   3339  1.193  yamaguch 	if (origlen < sizeof(*h))
   3340  1.193  yamaguch 		return 0;
   3341  1.193  yamaguch 
   3342  1.193  yamaguch 	origlen -= sizeof(*h);
   3343  1.193  yamaguch 	type = 0;
   3344  1.193  yamaguch 
   3345    1.3  explorer 	/*
   3346    1.3  explorer 	 * Make sure to allocate a buf that can at least hold a
   3347    1.3  explorer 	 * conf-nak with an `address' option.  We might need it below.
   3348    1.3  explorer 	 */
   3349  1.193  yamaguch 	blen = MAX(6, origlen);
   3350  1.193  yamaguch 
   3351  1.193  yamaguch 	buf = kmem_intr_alloc(blen, KM_NOSLEEP);
   3352  1.193  yamaguch 	if (buf == NULL)
   3353  1.193  yamaguch 		return 0;
   3354    1.3  explorer 
   3355    1.3  explorer 	/* pass 1: see if we can recognize them */
   3356    1.3  explorer 	if (debug)
   3357   1.64    itojun 		log(LOG_DEBUG, "%s: ipcp parse opts:",
   3358   1.64    itojun 		    ifp->if_xname);
   3359   1.59    itojun 	p = (void *)(h + 1);
   3360  1.193  yamaguch 	r = buf;
   3361  1.193  yamaguch 	rlen = 0;
   3362  1.193  yamaguch 	for (len = origlen; len > 1; len -= l, p += l) {
   3363  1.193  yamaguch 		l = p[1];
   3364  1.193  yamaguch 		if (l == 0)
   3365  1.193  yamaguch 			break;
   3366  1.193  yamaguch 
   3367   1.96   adrianp 		/* Sanity check option length */
   3368  1.140  christos 		if (l > len) {
   3369   1.96   adrianp 			/* XXX should we just RXJ? */
   3370   1.96   adrianp 			addlog("%s: malicious IPCP option received, dropping\n",
   3371   1.96   adrianp 			    ifp->if_xname);
   3372  1.193  yamaguch 			rlen = -1;
   3373  1.193  yamaguch 			goto end;
   3374   1.96   adrianp 		}
   3375    1.3  explorer 		if (debug)
   3376    1.9    itojun 			addlog(" %s", sppp_ipcp_opt_name(*p));
   3377  1.193  yamaguch 		switch (p[0]) {
   3378    1.3  explorer #ifdef notyet
   3379    1.3  explorer 		case IPCP_OPT_COMPRESSION:
   3380  1.140  christos 			if (len >= 6 && l >= 6) {
   3381    1.3  explorer 				/* correctly formed compress option */
   3382    1.3  explorer 				continue;
   3383    1.3  explorer 			}
   3384    1.3  explorer 			if (debug)
   3385    1.9    itojun 				addlog(" [invalid]");
   3386    1.3  explorer 			break;
   3387    1.3  explorer #endif
   3388    1.3  explorer 		case IPCP_OPT_ADDRESS:
   3389  1.140  christos 			if (len >= 6 && l == 6) {
   3390    1.3  explorer 				/* correctly formed address option */
   3391    1.3  explorer 				continue;
   3392    1.3  explorer 			}
   3393    1.3  explorer 			if (debug)
   3394    1.9    itojun 				addlog(" [invalid]");
   3395    1.3  explorer 			break;
   3396    1.3  explorer 		default:
   3397    1.3  explorer 			/* Others not supported. */
   3398    1.3  explorer 			if (debug)
   3399    1.9    itojun 				addlog(" [rej]");
   3400    1.3  explorer 			break;
   3401    1.3  explorer 		}
   3402    1.3  explorer 		/* Add the option to rejected list. */
   3403  1.140  christos 		if (rlen + l > blen) {
   3404  1.140  christos 			if (debug)
   3405  1.140  christos 				addlog(" [overflow]");
   3406  1.140  christos 			continue;
   3407  1.140  christos 		}
   3408  1.140  christos 		memcpy(r, p, l);
   3409  1.140  christos 		r += l;
   3410  1.140  christos 		rlen += l;
   3411    1.3  explorer 	}
   3412  1.193  yamaguch 
   3413  1.193  yamaguch 	if (rlen > 0) {
   3414  1.193  yamaguch 		type = CONF_REJ;
   3415    1.9    itojun 		goto end;
   3416  1.193  yamaguch 	}
   3417  1.193  yamaguch 
   3418  1.193  yamaguch 	if (debug)
   3419    1.3  explorer 		addlog("\n");
   3420    1.3  explorer 
   3421    1.3  explorer 	/* pass 2: parse option values */
   3422   1.31    martin 	if (sp->ipcp.flags & IPCP_HISADDR_SEEN)
   3423   1.31    martin 		hisaddr = sp->ipcp.req_hisaddr;	/* we already aggreed on that */
   3424   1.31    martin 	else
   3425   1.79  christos #ifdef INET
   3426   1.31    martin 		sppp_get_ip_addrs(sp, 0, &hisaddr, 0);	/* user configuration */
   3427   1.79  christos #else
   3428   1.79  christos 		hisaddr = 0;
   3429   1.79  christos #endif
   3430    1.3  explorer 	if (debug)
   3431   1.64    itojun 		log(LOG_DEBUG, "%s: ipcp parse opt values: ",
   3432   1.64    itojun 		       ifp->if_xname);
   3433   1.59    itojun 	p = (void *)(h + 1);
   3434  1.193  yamaguch 	r = buf;
   3435  1.193  yamaguch 	rlen = 0;
   3436  1.193  yamaguch 	for (len = origlen; len > 1; len -= l, p += l) {
   3437  1.193  yamaguch 		l = p[1];
   3438  1.193  yamaguch 		if (l == 0)
   3439  1.193  yamaguch 			break;
   3440  1.193  yamaguch 
   3441    1.3  explorer 		if (debug)
   3442    1.9    itojun 			addlog(" %s", sppp_ipcp_opt_name(*p));
   3443  1.193  yamaguch 		switch (p[0]) {
   3444    1.3  explorer #ifdef notyet
   3445    1.3  explorer 		case IPCP_OPT_COMPRESSION:
   3446    1.3  explorer 			continue;
   3447    1.3  explorer #endif
   3448    1.3  explorer 		case IPCP_OPT_ADDRESS:
   3449    1.3  explorer 			desiredaddr = p[2] << 24 | p[3] << 16 |
   3450    1.3  explorer 				p[4] << 8 | p[5];
   3451   1.31    martin 			if (desiredaddr == hisaddr ||
   3452   1.31    martin 		    	   ((sp->ipcp.flags & IPCP_HISADDR_DYN) && desiredaddr != 0)) {
   3453    1.3  explorer 				/*
   3454   1.31    martin 			 	* Peer's address is same as our value,
   3455   1.31    martin 			 	* this is agreeable.  Gonna conf-ack
   3456   1.31    martin 			 	* it.
   3457   1.31    martin 			 	*/
   3458    1.3  explorer 				if (debug)
   3459   1.31    martin 					addlog(" %s [ack]",
   3460   1.31    martin 				       		sppp_dotted_quad(hisaddr));
   3461   1.31    martin 				/* record that we've seen it already */
   3462   1.31    martin 				sp->ipcp.flags |= IPCP_HISADDR_SEEN;
   3463   1.31    martin 				sp->ipcp.req_hisaddr = desiredaddr;
   3464   1.31    martin 				hisaddr = desiredaddr;
   3465    1.3  explorer 				continue;
   3466   1.31    martin 			}
   3467   1.31    martin 			/*
   3468   1.31    martin 		 	* The address wasn't agreeable.  This is either
   3469   1.31    martin 		 	* he sent us 0.0.0.0, asking to assign him an
   3470   1.31    martin 		 	* address, or he send us another address not
   3471   1.31    martin 		 	* matching our value.  Either case, we gonna
   3472   1.31    martin 		 	* conf-nak it with our value.
   3473   1.31    martin 		 	*/
   3474   1.31    martin 			if (debug) {
   3475   1.31    martin 				if (desiredaddr == 0)
   3476   1.31    martin 					addlog(" [addr requested]");
   3477   1.31    martin 				else
   3478   1.31    martin 					addlog(" %s [not agreed]",
   3479   1.31    martin 				       		sppp_dotted_quad(desiredaddr));
   3480   1.31    martin 			}
   3481    1.3  explorer 
   3482   1.31    martin 			p[2] = hisaddr >> 24;
   3483   1.31    martin 			p[3] = hisaddr >> 16;
   3484   1.31    martin 			p[4] = hisaddr >> 8;
   3485   1.31    martin 			p[5] = hisaddr;
   3486   1.31    martin 			break;
   3487    1.3  explorer 		}
   3488  1.140  christos 		if (rlen + l > blen) {
   3489  1.140  christos 			if (debug)
   3490  1.140  christos 				addlog(" [overflow]");
   3491  1.140  christos 			continue;
   3492  1.140  christos 		}
   3493    1.3  explorer 		/* Add the option to nak'ed list. */
   3494  1.140  christos 		memcpy(r, p, l);
   3495  1.140  christos 		r += l;
   3496  1.140  christos 		rlen += l;
   3497    1.3  explorer 	}
   3498    1.3  explorer 
   3499  1.193  yamaguch 	if (rlen > 0) {
   3500  1.193  yamaguch 		type = CONF_NAK;
   3501  1.193  yamaguch 	} else {
   3502  1.193  yamaguch 		if ((sp->ipcp.flags & IPCP_HISADDR_SEEN) == 0) {
   3503  1.193  yamaguch 			/*
   3504  1.193  yamaguch 			 * If we are about to conf-ack the request, but haven't seen
   3505  1.193  yamaguch 			 * his address so far, gonna conf-nak it instead, with the
   3506  1.193  yamaguch 			 * `address' option present and our idea of his address being
   3507  1.193  yamaguch 			 * filled in there, to request negotiation of both addresses.
   3508  1.193  yamaguch 			 *
   3509  1.193  yamaguch 			 * XXX This can result in an endless req - nak loop if peer
   3510  1.193  yamaguch 			 * doesn't want to send us his address.  Q: What should we do
   3511  1.193  yamaguch 			 * about it?  XXX  A: implement the max-failure counter.
   3512  1.193  yamaguch 			 */
   3513  1.193  yamaguch 			buf[0] = IPCP_OPT_ADDRESS;
   3514  1.193  yamaguch 			buf[1] = 6;
   3515  1.193  yamaguch 			buf[2] = hisaddr >> 24;
   3516  1.193  yamaguch 			buf[3] = hisaddr >> 16;
   3517  1.193  yamaguch 			buf[4] = hisaddr >> 8;
   3518  1.193  yamaguch 			buf[5] = hisaddr;
   3519  1.193  yamaguch 			rlen = 6;
   3520  1.193  yamaguch 			if (debug)
   3521  1.193  yamaguch 				addlog(" still need hisaddr");
   3522  1.194  yamaguch 			type = CONF_NAK;
   3523  1.194  yamaguch 		} else {
   3524  1.194  yamaguch 			type = CONF_ACK;
   3525  1.194  yamaguch 			rlen = origlen;
   3526  1.194  yamaguch 			memcpy(r, h + 1, rlen);
   3527  1.193  yamaguch 		}
   3528    1.3  explorer 	}
   3529    1.3  explorer 
   3530  1.194  yamaguch end:
   3531  1.194  yamaguch 	if (debug)
   3532  1.194  yamaguch 		addlog("\n");
   3533  1.194  yamaguch 
   3534  1.194  yamaguch 	if (rlen < 0) {
   3535  1.194  yamaguch 		kmem_intr_free(buf, blen);
   3536  1.194  yamaguch 		return -1;
   3537    1.3  explorer 	}
   3538    1.3  explorer 
   3539  1.194  yamaguch 	if (sp->scp[IDX_IPCP].rcr_buf != NULL) {
   3540  1.194  yamaguch 		kmem_intr_free(sp->scp[IDX_IPCP].rcr_buf,
   3541  1.194  yamaguch 		    sp->scp[IDX_IPCP].rcr_blen);
   3542  1.193  yamaguch 	}
   3543  1.193  yamaguch 
   3544  1.194  yamaguch 	sp->scp[IDX_IPCP].rcr_type = type;
   3545  1.194  yamaguch 	sp->scp[IDX_IPCP].rcr_buf = buf;
   3546  1.194  yamaguch 	sp->scp[IDX_IPCP].rcr_blen = blen;
   3547  1.194  yamaguch 	sp->scp[IDX_IPCP].rcr_rlen = rlen;
   3548  1.193  yamaguch 
   3549  1.193  yamaguch 	if (type != CONF_ACK)
   3550  1.193  yamaguch 		return 0;
   3551  1.193  yamaguch 	return 1;
   3552    1.3  explorer }
   3553    1.3  explorer 
   3554    1.3  explorer /*
   3555    1.3  explorer  * Analyze the IPCP Configure-Reject option list, and adjust our
   3556    1.3  explorer  * negotiation.
   3557    1.3  explorer  */
   3558    1.3  explorer static void
   3559    1.3  explorer sppp_ipcp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len)
   3560    1.3  explorer {
   3561  1.193  yamaguch 	u_char *p, l;
   3562    1.3  explorer 	struct ifnet *ifp = &sp->pp_if;
   3563    1.3  explorer 	int debug = ifp->if_flags & IFF_DEBUG;
   3564    1.3  explorer 
   3565  1.170  knakahar 	KASSERT(SPPP_WLOCKED(sp));
   3566  1.163  knakahar 
   3567  1.193  yamaguch 	if (len <= sizeof(*h))
   3568    1.3  explorer 		return;
   3569    1.3  explorer 
   3570  1.193  yamaguch 	len -= sizeof(*h);
   3571  1.193  yamaguch 
   3572    1.3  explorer 	if (debug)
   3573   1.64    itojun 		log(LOG_DEBUG, "%s: ipcp rej opts:",
   3574   1.64    itojun 		    ifp->if_xname);
   3575    1.3  explorer 
   3576   1.59    itojun 	p = (void *)(h + 1);
   3577  1.193  yamaguch 	for (; len > 1; len -= l, p += l) {
   3578  1.193  yamaguch 		l = p[1];
   3579  1.193  yamaguch 		if (l == 0)
   3580  1.193  yamaguch 			break;
   3581  1.193  yamaguch 
   3582   1.96   adrianp 		/* Sanity check option length */
   3583  1.140  christos 		if (l > len) {
   3584   1.96   adrianp 			/* XXX should we just RXJ? */
   3585   1.96   adrianp 			addlog("%s: malicious IPCP option received, dropping\n",
   3586   1.96   adrianp 			    ifp->if_xname);
   3587  1.193  yamaguch 			goto end;
   3588   1.96   adrianp 		}
   3589    1.3  explorer 		if (debug)
   3590    1.9    itojun 			addlog(" %s", sppp_ipcp_opt_name(*p));
   3591  1.193  yamaguch 		switch (p[0]) {
   3592    1.3  explorer 		case IPCP_OPT_ADDRESS:
   3593    1.3  explorer 			/*
   3594    1.3  explorer 			 * Peer doesn't grok address option.  This is
   3595    1.3  explorer 			 * bad.  XXX  Should we better give up here?
   3596    1.3  explorer 			 */
   3597    1.3  explorer 			sp->ipcp.opts &= ~(1 << IPCP_OPT_ADDRESS);
   3598    1.3  explorer 			break;
   3599    1.3  explorer #ifdef notyet
   3600    1.3  explorer 		case IPCP_OPT_COMPRESS:
   3601    1.3  explorer 			sp->ipcp.opts &= ~(1 << IPCP_OPT_COMPRESS);
   3602    1.3  explorer 			break;
   3603    1.3  explorer #endif
   3604    1.3  explorer 		}
   3605    1.3  explorer 	}
   3606    1.3  explorer 	if (debug)
   3607    1.3  explorer 		addlog("\n");
   3608  1.193  yamaguch end:
   3609    1.3  explorer 	return;
   3610    1.3  explorer }
   3611    1.3  explorer 
   3612    1.3  explorer /*
   3613    1.3  explorer  * Analyze the IPCP Configure-NAK option list, and adjust our
   3614    1.3  explorer  * negotiation.
   3615    1.3  explorer  */
   3616    1.3  explorer static void
   3617    1.3  explorer sppp_ipcp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len)
   3618    1.3  explorer {
   3619  1.140  christos 	u_char *p, l;
   3620    1.3  explorer 	struct ifnet *ifp = &sp->pp_if;
   3621    1.3  explorer 	int debug = ifp->if_flags & IFF_DEBUG;
   3622  1.109      matt 	uint32_t wantaddr;
   3623    1.3  explorer 
   3624  1.170  knakahar 	KASSERT(SPPP_WLOCKED(sp));
   3625  1.163  knakahar 
   3626  1.193  yamaguch 	len -= sizeof(*h);
   3627    1.3  explorer 
   3628    1.3  explorer 	if (debug)
   3629   1.64    itojun 		log(LOG_DEBUG, "%s: ipcp nak opts:",
   3630   1.64    itojun 		    ifp->if_xname);
   3631    1.3  explorer 
   3632   1.59    itojun 	p = (void *)(h + 1);
   3633  1.193  yamaguch 	for (; len > 1; len -= l, p += l) {
   3634  1.193  yamaguch 		l = p[1];
   3635  1.193  yamaguch 		if (l == 0)
   3636  1.193  yamaguch 			break;
   3637  1.193  yamaguch 
   3638   1.96   adrianp 		/* Sanity check option length */
   3639  1.140  christos 		if (l > len) {
   3640   1.96   adrianp 			/* XXX should we just RXJ? */
   3641   1.96   adrianp 			addlog("%s: malicious IPCP option received, dropping\n",
   3642   1.96   adrianp 			    ifp->if_xname);
   3643   1.96   adrianp 			return;
   3644   1.96   adrianp 		}
   3645    1.3  explorer 		if (debug)
   3646    1.9    itojun 			addlog(" %s", sppp_ipcp_opt_name(*p));
   3647    1.3  explorer 		switch (*p) {
   3648    1.3  explorer 		case IPCP_OPT_ADDRESS:
   3649    1.3  explorer 			/*
   3650    1.3  explorer 			 * Peer doesn't like our local IP address.  See
   3651    1.3  explorer 			 * if we can do something for him.  We'll drop
   3652    1.3  explorer 			 * him our address then.
   3653    1.3  explorer 			 */
   3654  1.140  christos 			if (len >= 6 && l == 6) {
   3655    1.3  explorer 				wantaddr = p[2] << 24 | p[3] << 16 |
   3656    1.3  explorer 					p[4] << 8 | p[5];
   3657    1.3  explorer 				sp->ipcp.opts |= (1 << IPCP_OPT_ADDRESS);
   3658    1.3  explorer 				if (debug)
   3659    1.9    itojun 					addlog(" [wantaddr %s]",
   3660    1.3  explorer 					       sppp_dotted_quad(wantaddr));
   3661    1.3  explorer 				/*
   3662    1.3  explorer 				 * When doing dynamic address assignment,
   3663    1.3  explorer 				 * we accept his offer.  Otherwise, we
   3664    1.3  explorer 				 * ignore it and thus continue to negotiate
   3665    1.3  explorer 				 * our already existing value.
   3666    1.3  explorer 				 */
   3667    1.3  explorer 				if (sp->ipcp.flags & IPCP_MYADDR_DYN) {
   3668    1.3  explorer 					if (debug)
   3669    1.9    itojun 						addlog(" [agree]");
   3670    1.3  explorer 					sp->ipcp.flags |= IPCP_MYADDR_SEEN;
   3671   1.31    martin 					sp->ipcp.req_myaddr = wantaddr;
   3672    1.3  explorer 				}
   3673    1.3  explorer 			}
   3674    1.3  explorer 			break;
   3675   1.45    martin 
   3676   1.45    martin 		case IPCP_OPT_PRIMDNS:
   3677  1.140  christos 			if (len >= 6 && l == 6) {
   3678   1.45    martin 				sp->dns_addrs[0] = p[2] << 24 | p[3] << 16 |
   3679   1.82     perry 					p[4] << 8 | p[5];
   3680   1.45    martin 			}
   3681   1.45    martin 			break;
   3682   1.45    martin 
   3683   1.45    martin 		case IPCP_OPT_SECDNS:
   3684  1.140  christos 			if (len >= 6 && l == 6) {
   3685   1.45    martin 				sp->dns_addrs[1] = p[2] << 24 | p[3] << 16 |
   3686   1.82     perry 					p[4] << 8 | p[5];
   3687   1.45    martin 			}
   3688   1.45    martin 			break;
   3689    1.3  explorer #ifdef notyet
   3690    1.3  explorer 		case IPCP_OPT_COMPRESS:
   3691    1.3  explorer 			/*
   3692    1.3  explorer 			 * Peer wants different compression parameters.
   3693    1.3  explorer 			 */
   3694    1.3  explorer 			break;
   3695    1.3  explorer #endif
   3696    1.3  explorer 		}
   3697    1.3  explorer 	}
   3698    1.3  explorer 	if (debug)
   3699    1.3  explorer 		addlog("\n");
   3700    1.3  explorer }
   3701    1.3  explorer 
   3702    1.3  explorer static void
   3703    1.3  explorer sppp_ipcp_tlu(struct sppp *sp)
   3704    1.3  explorer {
   3705   1.79  christos #ifdef INET
   3706  1.170  knakahar 	KASSERT(SPPP_WLOCKED(sp));
   3707   1.31    martin 	/* we are up. Set addresses and notify anyone interested */
   3708  1.158  knakahar 	sppp_set_ip_addrs(sp);
   3709   1.79  christos #endif
   3710    1.3  explorer }
   3711    1.3  explorer 
   3712    1.3  explorer static void
   3713    1.3  explorer sppp_ipcp_scr(struct sppp *sp)
   3714    1.3  explorer {
   3715  1.156     joerg 	uint8_t opt[6 /* compression */ + 6 /* address */ + 12 /* dns addresses */];
   3716   1.79  christos #ifdef INET
   3717  1.109      matt 	uint32_t ouraddr;
   3718   1.79  christos #endif
   3719    1.3  explorer 	int i = 0;
   3720    1.3  explorer 
   3721  1.170  knakahar 	KASSERT(SPPP_WLOCKED(sp));
   3722  1.163  knakahar 
   3723    1.3  explorer #ifdef notyet
   3724    1.3  explorer 	if (sp->ipcp.opts & (1 << IPCP_OPT_COMPRESSION)) {
   3725    1.3  explorer 		opt[i++] = IPCP_OPT_COMPRESSION;
   3726    1.3  explorer 		opt[i++] = 6;
   3727    1.3  explorer 		opt[i++] = 0;	/* VJ header compression */
   3728    1.3  explorer 		opt[i++] = 0x2d; /* VJ header compression */
   3729    1.3  explorer 		opt[i++] = max_slot_id;
   3730    1.3  explorer 		opt[i++] = comp_slot_id;
   3731    1.3  explorer 	}
   3732    1.3  explorer #endif
   3733    1.3  explorer 
   3734   1.79  christos #ifdef INET
   3735    1.3  explorer 	if (sp->ipcp.opts & (1 << IPCP_OPT_ADDRESS)) {
   3736   1.31    martin 		if (sp->ipcp.flags & IPCP_MYADDR_SEEN)
   3737   1.31    martin 			ouraddr = sp->ipcp.req_myaddr;	/* not sure if this can ever happen */
   3738   1.31    martin 		else
   3739   1.31    martin 			sppp_get_ip_addrs(sp, &ouraddr, 0, 0);
   3740    1.3  explorer 		opt[i++] = IPCP_OPT_ADDRESS;
   3741    1.3  explorer 		opt[i++] = 6;
   3742    1.3  explorer 		opt[i++] = ouraddr >> 24;
   3743    1.3  explorer 		opt[i++] = ouraddr >> 16;
   3744    1.3  explorer 		opt[i++] = ouraddr >> 8;
   3745    1.3  explorer 		opt[i++] = ouraddr;
   3746    1.3  explorer 	}
   3747   1.79  christos #endif
   3748    1.3  explorer 
   3749   1.45    martin 	if (sp->query_dns & 1) {
   3750   1.45    martin 		opt[i++] = IPCP_OPT_PRIMDNS;
   3751   1.45    martin 		opt[i++] = 6;
   3752   1.45    martin 		opt[i++] = sp->dns_addrs[0] >> 24;
   3753   1.45    martin 		opt[i++] = sp->dns_addrs[0] >> 16;
   3754   1.45    martin 		opt[i++] = sp->dns_addrs[0] >> 8;
   3755   1.45    martin 		opt[i++] = sp->dns_addrs[0];
   3756   1.45    martin 	}
   3757   1.45    martin 	if (sp->query_dns & 2) {
   3758   1.45    martin 		opt[i++] = IPCP_OPT_SECDNS;
   3759   1.45    martin 		opt[i++] = 6;
   3760   1.45    martin 		opt[i++] = sp->dns_addrs[1] >> 24;
   3761   1.45    martin 		opt[i++] = sp->dns_addrs[1] >> 16;
   3762   1.45    martin 		opt[i++] = sp->dns_addrs[1] >> 8;
   3763   1.45    martin 		opt[i++] = sp->dns_addrs[1];
   3764   1.45    martin 	}
   3765   1.45    martin 
   3766  1.192  yamaguch 	sp->scp[IDX_IPCP].confid = ++sp->scp[IDX_IPCP].seq;
   3767  1.192  yamaguch 	sppp_cp_send(sp, PPP_IPCP, CONF_REQ, sp->scp[IDX_IPCP].confid, i, &opt);
   3768    1.3  explorer }
   3769    1.3  explorer 
   3770    1.3  explorer 
   3771   1.42  jdolecek /*
   3772    1.3  explorer  *--------------------------------------------------------------------------*
   3773    1.3  explorer  *                                                                          *
   3774    1.9    itojun  *                      The IPv6CP implementation.                          *
   3775    1.9    itojun  *                                                                          *
   3776    1.9    itojun  *--------------------------------------------------------------------------*
   3777    1.9    itojun  */
   3778    1.9    itojun 
   3779    1.9    itojun #ifdef INET6
   3780    1.9    itojun static void
   3781    1.9    itojun sppp_ipv6cp_init(struct sppp *sp)
   3782    1.9    itojun {
   3783  1.170  knakahar 
   3784  1.170  knakahar 	KASSERT(SPPP_WLOCKED(sp));
   3785  1.170  knakahar 
   3786    1.9    itojun 	sp->ipv6cp.opts = 0;
   3787    1.9    itojun 	sp->ipv6cp.flags = 0;
   3788  1.192  yamaguch 	sp->scp[IDX_IPV6CP].state = STATE_INITIAL;
   3789  1.192  yamaguch 	sp->scp[IDX_IPV6CP].fail_counter = 0;
   3790  1.192  yamaguch 	sp->scp[IDX_IPV6CP].seq = 0;
   3791  1.192  yamaguch 	sp->scp[IDX_IPV6CP].rseq = 0;
   3792  1.198  yamaguch 	sppp_cp_init(&ipv6cp, sp);
   3793    1.9    itojun }
   3794    1.9    itojun 
   3795    1.9    itojun static void
   3796  1.198  yamaguch sppp_ipv6cp_open(struct sppp *sp, void *xcp)
   3797    1.9    itojun {
   3798    1.9    itojun 	STDDCL;
   3799    1.9    itojun 	struct in6_addr myaddr, hisaddr;
   3800    1.9    itojun 
   3801  1.170  knakahar 	KASSERT(SPPP_WLOCKED(sp));
   3802  1.163  knakahar 
   3803    1.9    itojun #ifdef IPV6CP_MYIFID_DYN
   3804    1.9    itojun 	sp->ipv6cp.flags &= ~(IPV6CP_MYIFID_SEEN|IPV6CP_MYIFID_DYN);
   3805    1.9    itojun #else
   3806    1.9    itojun 	sp->ipv6cp.flags &= ~IPV6CP_MYIFID_SEEN;
   3807    1.9    itojun #endif
   3808    1.9    itojun 
   3809    1.9    itojun 	sppp_get_ip6_addrs(sp, &myaddr, &hisaddr, 0);
   3810    1.9    itojun 	/*
   3811    1.9    itojun 	 * If we don't have our address, this probably means our
   3812    1.9    itojun 	 * interface doesn't want to talk IPv6 at all.  (This could
   3813    1.9    itojun 	 * be the case if somebody wants to speak only IPX, for
   3814    1.9    itojun 	 * example.)  Don't open IPv6CP in this case.
   3815    1.9    itojun 	 */
   3816    1.9    itojun 	if (IN6_IS_ADDR_UNSPECIFIED(&myaddr)) {
   3817    1.9    itojun 		/* XXX this message should go away */
   3818    1.9    itojun 		if (debug)
   3819   1.64    itojun 			log(LOG_DEBUG, "%s: ipv6cp_open(): no IPv6 interface\n",
   3820   1.64    itojun 			    ifp->if_xname);
   3821    1.9    itojun 		return;
   3822    1.9    itojun 	}
   3823    1.9    itojun 
   3824    1.9    itojun 	sp->ipv6cp.flags |= IPV6CP_MYIFID_SEEN;
   3825    1.9    itojun 	sp->ipv6cp.opts |= (1 << IPV6CP_OPT_IFID);
   3826  1.198  yamaguch 	sppp_open_event(sp, xcp);
   3827    1.9    itojun }
   3828    1.9    itojun 
   3829    1.9    itojun /*
   3830    1.9    itojun  * Analyze a configure request.  Return true if it was agreeable, and
   3831    1.9    itojun  * caused action sca, false if it has been rejected or nak'ed, and
   3832    1.9    itojun  * caused action scn.  (The return value is used to make the state
   3833    1.9    itojun  * transition decision in the state automaton.)
   3834    1.9    itojun  */
   3835    1.9    itojun static int
   3836  1.193  yamaguch sppp_ipv6cp_RCR(struct sppp *sp, struct lcp_header *h, int origlen)
   3837    1.9    itojun {
   3838  1.140  christos 	u_char *buf, *r, *p, l, blen;
   3839    1.9    itojun 	struct ifnet *ifp = &sp->pp_if;
   3840  1.193  yamaguch 	int rlen, len, debug = ifp->if_flags & IFF_DEBUG;
   3841    1.9    itojun 	struct in6_addr myaddr, desiredaddr, suggestaddr;
   3842    1.9    itojun 	int ifidcount;
   3843    1.9    itojun 	int type;
   3844    1.9    itojun 	int collision, nohisaddr;
   3845  1.166       ryo 	char ip6buf[INET6_ADDRSTRLEN];
   3846    1.9    itojun 
   3847  1.170  knakahar 	KASSERT(SPPP_WLOCKED(sp));
   3848  1.163  knakahar 
   3849  1.194  yamaguch 	if (origlen < sizeof(*h))
   3850  1.193  yamaguch 		return 0;
   3851  1.193  yamaguch 
   3852  1.193  yamaguch 	origlen -= sizeof(*h);
   3853  1.193  yamaguch 	type = 0;
   3854  1.193  yamaguch 
   3855    1.9    itojun 	/*
   3856    1.9    itojun 	 * Make sure to allocate a buf that can at least hold a
   3857    1.9    itojun 	 * conf-nak with an `address' option.  We might need it below.
   3858    1.9    itojun 	 */
   3859  1.193  yamaguch 	blen = MAX(6, origlen);
   3860  1.193  yamaguch 
   3861  1.193  yamaguch 	buf = kmem_intr_alloc(blen, KM_NOSLEEP);
   3862  1.193  yamaguch 	if (buf == NULL)
   3863  1.193  yamaguch 		return 0;
   3864    1.9    itojun 
   3865    1.9    itojun 	/* pass 1: see if we can recognize them */
   3866    1.9    itojun 	if (debug)
   3867   1.64    itojun 		log(LOG_DEBUG, "%s: ipv6cp parse opts:",
   3868   1.64    itojun 		    ifp->if_xname);
   3869   1.59    itojun 	p = (void *)(h + 1);
   3870  1.193  yamaguch 	r = buf;
   3871  1.193  yamaguch 	rlen = 0;
   3872    1.9    itojun 	ifidcount = 0;
   3873  1.193  yamaguch 	for (len = origlen; len > 1; len -= l, p += l) {
   3874  1.193  yamaguch 		l = p[1];
   3875  1.193  yamaguch 		if (l == 0)
   3876  1.193  yamaguch 			break;
   3877  1.193  yamaguch 
   3878   1.96   adrianp 		/* Sanity check option length */
   3879  1.140  christos 		if (l > len) {
   3880   1.96   adrianp 			/* XXX just RXJ? */
   3881   1.96   adrianp 			addlog("%s: received malicious IPCPv6 option, "
   3882   1.96   adrianp 			    "dropping\n", ifp->if_xname);
   3883  1.193  yamaguch 			rlen = -1;
   3884  1.193  yamaguch 			goto end;
   3885   1.96   adrianp 		}
   3886    1.9    itojun 		if (debug)
   3887    1.9    itojun 			addlog(" %s", sppp_ipv6cp_opt_name(*p));
   3888  1.193  yamaguch 		switch (p[0]) {
   3889    1.9    itojun 		case IPV6CP_OPT_IFID:
   3890  1.140  christos 			if (len >= 10 && l == 10 && ifidcount == 0) {
   3891    1.9    itojun 				/* correctly formed address option */
   3892    1.9    itojun 				ifidcount++;
   3893    1.9    itojun 				continue;
   3894    1.9    itojun 			}
   3895    1.9    itojun 			if (debug)
   3896    1.9    itojun 				addlog(" [invalid]");
   3897    1.9    itojun 			break;
   3898    1.9    itojun #ifdef notyet
   3899    1.9    itojun 		case IPV6CP_OPT_COMPRESSION:
   3900  1.140  christos 			if (len >= 4 && l >= 4) {
   3901    1.9    itojun 				/* correctly formed compress option */
   3902    1.9    itojun 				continue;
   3903    1.9    itojun 			}
   3904    1.9    itojun 			if (debug)
   3905    1.9    itojun 				addlog(" [invalid]");
   3906    1.9    itojun 			break;
   3907    1.9    itojun #endif
   3908    1.9    itojun 		default:
   3909    1.9    itojun 			/* Others not supported. */
   3910    1.9    itojun 			if (debug)
   3911    1.9    itojun 				addlog(" [rej]");
   3912    1.9    itojun 			break;
   3913    1.9    itojun 		}
   3914  1.140  christos 		if (rlen + l > blen) {
   3915  1.140  christos 			if (debug)
   3916  1.140  christos 				addlog(" [overflow]");
   3917  1.140  christos 			continue;
   3918  1.140  christos 		}
   3919    1.9    itojun 		/* Add the option to rejected list. */
   3920  1.140  christos 		memcpy(r, p, l);
   3921  1.140  christos 		r += l;
   3922  1.140  christos 		rlen += l;
   3923    1.9    itojun 	}
   3924  1.193  yamaguch 
   3925  1.193  yamaguch 	if (rlen > 0) {
   3926  1.193  yamaguch 		type = CONF_REJ;
   3927    1.9    itojun 		goto end;
   3928  1.193  yamaguch 	}
   3929  1.193  yamaguch 
   3930  1.193  yamaguch 	if (debug)
   3931    1.9    itojun 		addlog("\n");
   3932    1.9    itojun 
   3933    1.9    itojun 	/* pass 2: parse option values */
   3934    1.9    itojun 	sppp_get_ip6_addrs(sp, &myaddr, 0, 0);
   3935    1.9    itojun 	if (debug)
   3936   1.64    itojun 		log(LOG_DEBUG, "%s: ipv6cp parse opt values: ",
   3937   1.64    itojun 		       ifp->if_xname);
   3938   1.59    itojun 	p = (void *)(h + 1);
   3939  1.193  yamaguch 	r = buf;
   3940  1.193  yamaguch 	rlen = 0;
   3941    1.9    itojun 	type = CONF_ACK;
   3942  1.193  yamaguch 	for (len = origlen; len > 1; len -= l, p += l) {
   3943  1.193  yamaguch 		l = p[1];
   3944  1.193  yamaguch 		if (l == 0)
   3945  1.193  yamaguch 			break;
   3946  1.193  yamaguch 
   3947    1.9    itojun 		if (debug)
   3948    1.9    itojun 			addlog(" %s", sppp_ipv6cp_opt_name(*p));
   3949  1.193  yamaguch 		switch (p[0]) {
   3950    1.9    itojun #ifdef notyet
   3951    1.9    itojun 		case IPV6CP_OPT_COMPRESSION:
   3952    1.9    itojun 			continue;
   3953    1.9    itojun #endif
   3954    1.9    itojun 		case IPV6CP_OPT_IFID:
   3955   1.25   thorpej 			memset(&desiredaddr, 0, sizeof(desiredaddr));
   3956  1.118   tsutsui 			memcpy(&desiredaddr.s6_addr[8], &p[2], 8);
   3957   1.36    martin 			collision = (memcmp(&desiredaddr.s6_addr[8],
   3958    1.9    itojun 					&myaddr.s6_addr[8], 8) == 0);
   3959    1.9    itojun 			nohisaddr = IN6_IS_ADDR_UNSPECIFIED(&desiredaddr);
   3960    1.9    itojun 
   3961    1.9    itojun 			desiredaddr.s6_addr16[0] = htons(0xfe80);
   3962   1.87    rpaulo 			(void)in6_setscope(&desiredaddr, &sp->pp_if, NULL);
   3963    1.9    itojun 
   3964    1.9    itojun 			if (!collision && !nohisaddr) {
   3965    1.9    itojun 				/* no collision, hisaddr known - Conf-Ack */
   3966    1.9    itojun 				type = CONF_ACK;
   3967    1.9    itojun 
   3968    1.9    itojun 				if (debug) {
   3969    1.9    itojun 					addlog(" %s [%s]",
   3970  1.167  christos 					    IN6_PRINT(ip6buf, &desiredaddr),
   3971    1.9    itojun 					    sppp_cp_type_name(type));
   3972    1.9    itojun 				}
   3973    1.9    itojun 				continue;
   3974    1.9    itojun 			}
   3975    1.9    itojun 
   3976  1.114     pooka 			memset(&suggestaddr, 0, sizeof(suggestaddr));
   3977    1.9    itojun 			if (collision && nohisaddr) {
   3978    1.9    itojun 				/* collision, hisaddr unknown - Conf-Rej */
   3979    1.9    itojun 				type = CONF_REJ;
   3980   1.25   thorpej 				memset(&p[2], 0, 8);
   3981    1.9    itojun 			} else {
   3982    1.9    itojun 				/*
   3983    1.9    itojun 				 * - no collision, hisaddr unknown, or
   3984    1.9    itojun 				 * - collision, hisaddr known
   3985    1.9    itojun 				 * Conf-Nak, suggest hisaddr
   3986    1.9    itojun 				 */
   3987    1.9    itojun 				type = CONF_NAK;
   3988    1.9    itojun 				sppp_suggest_ip6_addr(sp, &suggestaddr);
   3989  1.118   tsutsui 				memcpy(&p[2], &suggestaddr.s6_addr[8], 8);
   3990    1.9    itojun 			}
   3991    1.9    itojun 			if (debug)
   3992  1.167  christos 				addlog(" %s [%s]", IN6_PRINT(ip6buf, &desiredaddr),
   3993    1.9    itojun 				    sppp_cp_type_name(type));
   3994    1.9    itojun 			break;
   3995    1.9    itojun 		}
   3996  1.140  christos 		if (rlen + l > blen) {
   3997  1.140  christos 			if (debug)
   3998  1.140  christos 				addlog(" [overflow]");
   3999  1.140  christos 			continue;
   4000  1.140  christos 		}
   4001    1.9    itojun 		/* Add the option to nak'ed list. */
   4002  1.140  christos 		memcpy(r, p, l);
   4003  1.140  christos 		r += l;
   4004  1.140  christos 		rlen += l;
   4005    1.9    itojun 	}
   4006    1.9    itojun 
   4007  1.193  yamaguch 	if (rlen > 0) {
   4008  1.193  yamaguch 		if (type != CONF_ACK) {
   4009  1.193  yamaguch 			if (debug) {
   4010  1.193  yamaguch 				addlog(" send %s suggest %s\n",
   4011  1.193  yamaguch 				    sppp_cp_type_name(type),
   4012  1.193  yamaguch 				    IN6_PRINT(ip6buf, &suggestaddr));
   4013  1.193  yamaguch 			}
   4014  1.193  yamaguch 		}
   4015   1.47    itojun #ifdef notdef
   4016    1.9    itojun 		if (type == CONF_ACK)
   4017    1.9    itojun 			panic("IPv6CP RCR: CONF_ACK with non-zero rlen");
   4018    1.9    itojun #endif
   4019  1.193  yamaguch 	} else {
   4020  1.193  yamaguch 		if (type == CONF_ACK) {
   4021  1.193  yamaguch 			rlen = origlen;
   4022  1.193  yamaguch 			memcpy(r, h + 1, rlen);
   4023    1.9    itojun 		}
   4024  1.193  yamaguch 	}
   4025  1.193  yamaguch end:
   4026  1.194  yamaguch 	if (debug)
   4027  1.194  yamaguch 		addlog("\n");
   4028  1.194  yamaguch 
   4029  1.194  yamaguch 	if (rlen < 0) {
   4030  1.194  yamaguch 		kmem_intr_free(buf, blen);
   4031  1.194  yamaguch 		return -1;
   4032    1.9    itojun 	}
   4033    1.9    itojun 
   4034  1.194  yamaguch 	if (sp->scp[IDX_IPV6CP].rcr_buf != NULL) {
   4035  1.194  yamaguch 		kmem_intr_free(sp->scp[IDX_IPV6CP].rcr_buf,
   4036  1.194  yamaguch 		    sp->scp[IDX_IPV6CP].rcr_blen);
   4037  1.194  yamaguch 	}
   4038  1.193  yamaguch 
   4039  1.194  yamaguch 	sp->scp[IDX_IPV6CP].rcr_type = type;
   4040  1.194  yamaguch 	sp->scp[IDX_IPV6CP].rcr_buf = buf;
   4041  1.194  yamaguch 	sp->scp[IDX_IPV6CP].rcr_blen = blen;
   4042  1.194  yamaguch 	sp->scp[IDX_IPV6CP].rcr_rlen = rlen;
   4043  1.193  yamaguch 
   4044  1.193  yamaguch 	if (type != CONF_ACK)
   4045  1.193  yamaguch 		return 0;
   4046  1.193  yamaguch 	return 0;
   4047    1.9    itojun }
   4048    1.9    itojun 
   4049    1.9    itojun /*
   4050    1.9    itojun  * Analyze the IPv6CP Configure-Reject option list, and adjust our
   4051    1.9    itojun  * negotiation.
   4052    1.9    itojun  */
   4053    1.9    itojun static void
   4054    1.9    itojun sppp_ipv6cp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len)
   4055    1.9    itojun {
   4056  1.193  yamaguch 	u_char *p, l;
   4057    1.9    itojun 	struct ifnet *ifp = &sp->pp_if;
   4058    1.9    itojun 	int debug = ifp->if_flags & IFF_DEBUG;
   4059    1.9    itojun 
   4060  1.170  knakahar 	KASSERT(SPPP_WLOCKED(sp));
   4061  1.163  knakahar 
   4062  1.193  yamaguch 	if (len <= sizeof(*h))
   4063    1.9    itojun 		return;
   4064    1.9    itojun 
   4065  1.193  yamaguch 	len -= sizeof(*h);
   4066  1.193  yamaguch 
   4067    1.9    itojun 	if (debug)
   4068   1.64    itojun 		log(LOG_DEBUG, "%s: ipv6cp rej opts:",
   4069   1.64    itojun 		    ifp->if_xname);
   4070    1.9    itojun 
   4071   1.59    itojun 	p = (void *)(h + 1);
   4072  1.193  yamaguch 	for (; len > 1; len -= l, p += l) {
   4073  1.193  yamaguch 		l = p[1];
   4074  1.193  yamaguch 		if (l == 0)
   4075  1.193  yamaguch 			break;
   4076  1.193  yamaguch 
   4077  1.140  christos 		if (l > len) {
   4078   1.96   adrianp 			/* XXX just RXJ? */
   4079   1.96   adrianp 			addlog("%s: received malicious IPCPv6 option, "
   4080   1.96   adrianp 			    "dropping\n", ifp->if_xname);
   4081  1.193  yamaguch 			goto end;
   4082   1.96   adrianp 		}
   4083    1.9    itojun 		if (debug)
   4084    1.9    itojun 			addlog(" %s", sppp_ipv6cp_opt_name(*p));
   4085  1.193  yamaguch 		switch (p[0]) {
   4086    1.9    itojun 		case IPV6CP_OPT_IFID:
   4087    1.9    itojun 			/*
   4088    1.9    itojun 			 * Peer doesn't grok address option.  This is
   4089    1.9    itojun 			 * bad.  XXX  Should we better give up here?
   4090    1.9    itojun 			 */
   4091    1.9    itojun 			sp->ipv6cp.opts &= ~(1 << IPV6CP_OPT_IFID);
   4092    1.9    itojun 			break;
   4093    1.9    itojun #ifdef notyet
   4094    1.9    itojun 		case IPV6CP_OPT_COMPRESS:
   4095    1.9    itojun 			sp->ipv6cp.opts &= ~(1 << IPV6CP_OPT_COMPRESS);
   4096    1.9    itojun 			break;
   4097    1.9    itojun #endif
   4098    1.9    itojun 		}
   4099    1.9    itojun 	}
   4100    1.9    itojun 	if (debug)
   4101    1.9    itojun 		addlog("\n");
   4102  1.193  yamaguch end:
   4103    1.9    itojun 	return;
   4104    1.9    itojun }
   4105    1.9    itojun 
   4106    1.9    itojun /*
   4107    1.9    itojun  * Analyze the IPv6CP Configure-NAK option list, and adjust our
   4108    1.9    itojun  * negotiation.
   4109    1.9    itojun  */
   4110    1.9    itojun static void
   4111    1.9    itojun sppp_ipv6cp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len)
   4112    1.9    itojun {
   4113  1.193  yamaguch 	u_char *p, l;
   4114    1.9    itojun 	struct ifnet *ifp = &sp->pp_if;
   4115    1.9    itojun 	int debug = ifp->if_flags & IFF_DEBUG;
   4116    1.9    itojun 	struct in6_addr suggestaddr;
   4117  1.166       ryo 	char ip6buf[INET6_ADDRSTRLEN];
   4118    1.9    itojun 
   4119  1.170  knakahar 	KASSERT(SPPP_WLOCKED(sp));
   4120  1.163  knakahar 
   4121  1.193  yamaguch 	if (len <= sizeof(*h))
   4122    1.9    itojun 		return;
   4123    1.9    itojun 
   4124  1.193  yamaguch 	len -= sizeof(*h);
   4125  1.193  yamaguch 
   4126    1.9    itojun 	if (debug)
   4127   1.64    itojun 		log(LOG_DEBUG, "%s: ipv6cp nak opts:",
   4128   1.64    itojun 		    ifp->if_xname);
   4129    1.9    itojun 
   4130   1.59    itojun 	p = (void *)(h + 1);
   4131  1.193  yamaguch 	for (; len > 1; len -= l, p += l) {
   4132  1.193  yamaguch 		l = p[1];
   4133  1.193  yamaguch 		if (l == 0)
   4134  1.193  yamaguch 			break;
   4135  1.193  yamaguch 
   4136  1.140  christos 		if (l > len) {
   4137   1.96   adrianp 			/* XXX just RXJ? */
   4138   1.96   adrianp 			addlog("%s: received malicious IPCPv6 option, "
   4139   1.96   adrianp 			    "dropping\n", ifp->if_xname);
   4140  1.193  yamaguch 			goto end;
   4141   1.96   adrianp 		}
   4142    1.9    itojun 		if (debug)
   4143    1.9    itojun 			addlog(" %s", sppp_ipv6cp_opt_name(*p));
   4144  1.193  yamaguch 		switch (p[0]) {
   4145    1.9    itojun 		case IPV6CP_OPT_IFID:
   4146    1.9    itojun 			/*
   4147    1.9    itojun 			 * Peer doesn't like our local ifid.  See
   4148    1.9    itojun 			 * if we can do something for him.  We'll drop
   4149    1.9    itojun 			 * him our address then.
   4150    1.9    itojun 			 */
   4151  1.140  christos 			if (len < 10 || l != 10)
   4152    1.9    itojun 				break;
   4153   1.25   thorpej 			memset(&suggestaddr, 0, sizeof(suggestaddr));
   4154    1.9    itojun 			suggestaddr.s6_addr16[0] = htons(0xfe80);
   4155   1.87    rpaulo 			(void)in6_setscope(&suggestaddr, &sp->pp_if, NULL);
   4156  1.118   tsutsui 			memcpy(&suggestaddr.s6_addr[8], &p[2], 8);
   4157    1.9    itojun 
   4158    1.9    itojun 			sp->ipv6cp.opts |= (1 << IPV6CP_OPT_IFID);
   4159    1.9    itojun 			if (debug)
   4160    1.9    itojun 				addlog(" [suggestaddr %s]",
   4161  1.167  christos 				       IN6_PRINT(ip6buf, &suggestaddr));
   4162    1.9    itojun #ifdef IPV6CP_MYIFID_DYN
   4163    1.9    itojun 			/*
   4164    1.9    itojun 			 * When doing dynamic address assignment,
   4165    1.9    itojun 			 * we accept his offer.
   4166    1.9    itojun 			 */
   4167    1.9    itojun 			if (sp->ipv6cp.flags & IPV6CP_MYIFID_DYN) {
   4168    1.9    itojun 				struct in6_addr lastsuggest;
   4169    1.9    itojun 				/*
   4170    1.9    itojun 				 * If <suggested myaddr from peer> equals to
   4171    1.9    itojun 				 * <hisaddr we have suggested last time>,
   4172    1.9    itojun 				 * we have a collision.  generate new random
   4173    1.9    itojun 				 * ifid.
   4174    1.9    itojun 				 */
   4175    1.9    itojun 				sppp_suggest_ip6_addr(&lastsuggest);
   4176    1.9    itojun 				if (IN6_ARE_ADDR_EQUAL(&suggestaddr,
   4177    1.9    itojun 						 lastsuggest)) {
   4178    1.9    itojun 					if (debug)
   4179    1.9    itojun 						addlog(" [random]");
   4180    1.9    itojun 					sppp_gen_ip6_addr(sp, &suggestaddr);
   4181    1.9    itojun 				}
   4182    1.9    itojun 				sppp_set_ip6_addr(sp, &suggestaddr, 0);
   4183    1.9    itojun 				if (debug)
   4184    1.9    itojun 					addlog(" [agree]");
   4185    1.9    itojun 				sp->ipv6cp.flags |= IPV6CP_MYIFID_SEEN;
   4186    1.9    itojun 			}
   4187    1.9    itojun #else
   4188    1.9    itojun 			/*
   4189    1.9    itojun 			 * Since we do not do dynamic address assignment,
   4190    1.9    itojun 			 * we ignore it and thus continue to negotiate
   4191    1.9    itojun 			 * our already existing value.  This can possibly
   4192    1.9    itojun 			 * go into infinite request-reject loop.
   4193    1.9    itojun 			 *
   4194    1.9    itojun 			 * This is not likely because we normally use
   4195    1.9    itojun 			 * ifid based on MAC-address.
   4196    1.9    itojun 			 * If you have no ethernet card on the node, too bad.
   4197    1.9    itojun 			 * XXX should we use fail_counter?
   4198    1.9    itojun 			 */
   4199    1.9    itojun #endif
   4200    1.9    itojun 			break;
   4201    1.9    itojun #ifdef notyet
   4202    1.9    itojun 		case IPV6CP_OPT_COMPRESS:
   4203    1.9    itojun 			/*
   4204    1.9    itojun 			 * Peer wants different compression parameters.
   4205    1.9    itojun 			 */
   4206    1.9    itojun 			break;
   4207    1.9    itojun #endif
   4208    1.9    itojun 		}
   4209    1.9    itojun 	}
   4210    1.9    itojun 	if (debug)
   4211    1.9    itojun 		addlog("\n");
   4212  1.193  yamaguch end:
   4213    1.9    itojun 	return;
   4214    1.9    itojun }
   4215    1.9    itojun 
   4216    1.9    itojun static void
   4217    1.9    itojun sppp_ipv6cp_tlu(struct sppp *sp)
   4218    1.9    itojun {
   4219  1.170  knakahar 
   4220  1.170  knakahar 	KASSERT(SPPP_WLOCKED(sp));
   4221    1.9    itojun 	/* we are up - notify isdn daemon */
   4222  1.170  knakahar 	sppp_notify_con_wlocked(sp);
   4223    1.9    itojun }
   4224    1.9    itojun 
   4225    1.9    itojun static void
   4226    1.9    itojun sppp_ipv6cp_scr(struct sppp *sp)
   4227    1.9    itojun {
   4228    1.9    itojun 	char opt[10 /* ifid */ + 4 /* compression, minimum */];
   4229    1.9    itojun 	struct in6_addr ouraddr;
   4230    1.9    itojun 	int i = 0;
   4231    1.9    itojun 
   4232  1.170  knakahar 	KASSERT(SPPP_WLOCKED(sp));
   4233  1.163  knakahar 
   4234    1.9    itojun 	if (sp->ipv6cp.opts & (1 << IPV6CP_OPT_IFID)) {
   4235    1.9    itojun 		sppp_get_ip6_addrs(sp, &ouraddr, 0, 0);
   4236    1.9    itojun 		opt[i++] = IPV6CP_OPT_IFID;
   4237    1.9    itojun 		opt[i++] = 10;
   4238  1.118   tsutsui 		memcpy(&opt[i], &ouraddr.s6_addr[8], 8);
   4239    1.9    itojun 		i += 8;
   4240    1.9    itojun 	}
   4241    1.9    itojun 
   4242    1.9    itojun #ifdef notyet
   4243    1.9    itojun 	if (sp->ipv6cp.opts & (1 << IPV6CP_OPT_COMPRESSION)) {
   4244    1.9    itojun 		opt[i++] = IPV6CP_OPT_COMPRESSION;
   4245    1.9    itojun 		opt[i++] = 4;
   4246    1.9    itojun 		opt[i++] = 0;	/* TBD */
   4247    1.9    itojun 		opt[i++] = 0;	/* TBD */
   4248    1.9    itojun 		/* variable length data may follow */
   4249    1.9    itojun 	}
   4250    1.9    itojun #endif
   4251    1.9    itojun 
   4252  1.192  yamaguch 	sp->scp[IDX_IPV6CP].confid = ++sp->scp[IDX_IPV6CP].seq;
   4253  1.192  yamaguch 	sppp_cp_send(sp, PPP_IPV6CP, CONF_REQ, sp->scp[IDX_IPV6CP].confid, i, &opt);
   4254    1.9    itojun }
   4255    1.9    itojun #else /*INET6*/
   4256   1.99    dogcow static void
   4257  1.101  christos sppp_ipv6cp_init(struct sppp *sp)
   4258    1.9    itojun {
   4259  1.170  knakahar 
   4260  1.170  knakahar 	KASSERT(SPPP_WLOCKED(sp));
   4261    1.9    itojun }
   4262    1.9    itojun 
   4263   1.99    dogcow static void
   4264  1.202  yamaguch sppp_ipv6cp_open(struct sppp *sp, void *xcp)
   4265    1.9    itojun {
   4266  1.170  knakahar 
   4267  1.170  knakahar 	KASSERT(SPPP_WLOCKED(sp));
   4268    1.9    itojun }
   4269    1.9    itojun 
   4270   1.99    dogcow static int
   4271  1.101  christos sppp_ipv6cp_RCR(struct sppp *sp, struct lcp_header *h,
   4272  1.101  christos 		int len)
   4273    1.9    itojun {
   4274  1.170  knakahar 
   4275  1.170  knakahar 	KASSERT(SPPP_WLOCKED(sp));
   4276    1.9    itojun 	return 0;
   4277    1.9    itojun }
   4278    1.9    itojun 
   4279   1.99    dogcow static void
   4280  1.101  christos sppp_ipv6cp_RCN_rej(struct sppp *sp, struct lcp_header *h,
   4281  1.101  christos 		    int len)
   4282    1.9    itojun {
   4283  1.170  knakahar 
   4284  1.170  knakahar 	KASSERT(SPPP_WLOCKED(sp));
   4285    1.9    itojun }
   4286    1.9    itojun 
   4287   1.99    dogcow static void
   4288  1.101  christos sppp_ipv6cp_RCN_nak(struct sppp *sp, struct lcp_header *h,
   4289  1.101  christos 		    int len)
   4290    1.9    itojun {
   4291  1.170  knakahar 
   4292  1.170  knakahar 	KASSERT(SPPP_WLOCKED(sp));
   4293    1.9    itojun }
   4294    1.9    itojun 
   4295   1.99    dogcow static void
   4296  1.101  christos sppp_ipv6cp_tlu(struct sppp *sp)
   4297    1.9    itojun {
   4298  1.170  knakahar 
   4299  1.170  knakahar 	KASSERT(SPPP_WLOCKED(sp));
   4300    1.9    itojun }
   4301    1.9    itojun 
   4302   1.99    dogcow static void
   4303  1.101  christos sppp_ipv6cp_scr(struct sppp *sp)
   4304    1.9    itojun {
   4305  1.170  knakahar 
   4306  1.170  knakahar 	KASSERT(SPPP_WLOCKED(sp));
   4307    1.9    itojun }
   4308    1.9    itojun #endif /*INET6*/
   4309    1.9    itojun 
   4310    1.9    itojun 
   4311   1.42  jdolecek /*
   4312    1.9    itojun  *--------------------------------------------------------------------------*
   4313    1.9    itojun  *                                                                          *
   4314    1.3  explorer  *                        The CHAP implementation.                          *
   4315    1.3  explorer  *                                                                          *
   4316    1.3  explorer  *--------------------------------------------------------------------------*
   4317    1.3  explorer  */
   4318    1.3  explorer /*
   4319  1.201  yamaguch  * The authentication protocols is implemented on the state machine for
   4320  1.201  yamaguch  * control protocols. And it uses following actions and events.
   4321    1.3  explorer  *
   4322  1.201  yamaguch  * Actions:
   4323  1.201  yamaguch  *    - scr: send CHAP_CHALLENGE and CHAP_RESPONSE
   4324  1.201  yamaguch  *    - sca: send CHAP_SUCCESS
   4325  1.201  yamaguch  *    - scn: send CHAP_FAILURE and shutdown lcp
   4326  1.201  yamaguch  * Events:
   4327  1.201  yamaguch  *    - RCR+: receive CHAP_RESPONSE containing correct digest
   4328  1.201  yamaguch  *    - RCR-: receive CHAP_RESPONSE containing wrong digest
   4329  1.201  yamaguch  *    - RCA: receive CHAP_SUCCESS
   4330  1.201  yamaguch  *    - RCN: (this event is unused)
   4331  1.201  yamaguch  *    - TO+: re-send CHAP_CHALLENGE and CHAP_RESPONSE
   4332  1.201  yamaguch  *    - TO-: this layer finish
   4333    1.3  explorer  */
   4334    1.3  explorer 
   4335    1.3  explorer /*
   4336    1.3  explorer  * Handle incoming CHAP packets.
   4337    1.3  explorer  */
   4338    1.3  explorer void
   4339    1.3  explorer sppp_chap_input(struct sppp *sp, struct mbuf *m)
   4340    1.3  explorer {
   4341    1.3  explorer 	STDDCL;
   4342    1.3  explorer 	struct lcp_header *h;
   4343    1.3  explorer 	int len, x;
   4344  1.201  yamaguch 	u_char *value, *name, digest[sizeof(sp->chap.challenge)];
   4345    1.3  explorer 	int value_len, name_len;
   4346    1.3  explorer 	MD5_CTX ctx;
   4347    1.3  explorer 
   4348    1.3  explorer 	len = m->m_pkthdr.len;
   4349    1.3  explorer 	if (len < 4) {
   4350    1.3  explorer 		if (debug)
   4351    1.3  explorer 			log(LOG_DEBUG,
   4352   1.64    itojun 			    "%s: chap invalid packet length: %d bytes\n",
   4353   1.64    itojun 			    ifp->if_xname, len);
   4354    1.3  explorer 		return;
   4355    1.3  explorer 	}
   4356   1.59    itojun 	h = mtod(m, struct lcp_header *);
   4357   1.59    itojun 	if (len > ntohs(h->len))
   4358   1.59    itojun 		len = ntohs(h->len);
   4359    1.3  explorer 
   4360  1.170  knakahar 	SPPP_LOCK(sp, RW_WRITER);
   4361  1.170  knakahar 
   4362    1.3  explorer 	switch (h->type) {
   4363    1.3  explorer 	/* challenge, failure and success are his authproto */
   4364    1.3  explorer 	case CHAP_CHALLENGE:
   4365   1.36    martin 		if (sp->myauth.secret == NULL || sp->myauth.name == NULL) {
   4366  1.163  knakahar 			/* can't do anything useful */
   4367  1.163  knakahar 			sp->pp_auth_failures++;
   4368  1.163  knakahar 			printf("%s: chap input without my name and my secret being set\n",
   4369  1.163  knakahar 				ifp->if_xname);
   4370  1.163  knakahar 			break;
   4371   1.36    martin 		}
   4372   1.59    itojun 		value = 1 + (u_char *)(h + 1);
   4373    1.3  explorer 		value_len = value[-1];
   4374    1.3  explorer 		name = value + value_len;
   4375    1.3  explorer 		name_len = len - value_len - 5;
   4376    1.3  explorer 		if (name_len < 0) {
   4377    1.3  explorer 			if (debug) {
   4378    1.3  explorer 				log(LOG_DEBUG,
   4379   1.64    itojun 				    "%s: chap corrupted challenge "
   4380    1.3  explorer 				    "<%s id=0x%x len=%d",
   4381   1.64    itojun 				    ifp->if_xname,
   4382    1.3  explorer 				    sppp_auth_type_name(PPP_CHAP, h->type),
   4383    1.3  explorer 				    h->ident, ntohs(h->len));
   4384    1.3  explorer 				if (len > 4)
   4385   1.59    itojun 					sppp_print_bytes((u_char *)(h + 1),
   4386   1.59    itojun 					    len - 4);
   4387    1.3  explorer 				addlog(">\n");
   4388    1.3  explorer 			}
   4389    1.3  explorer 			break;
   4390    1.3  explorer 		}
   4391   1.82     perry 
   4392    1.3  explorer 		if (debug) {
   4393    1.3  explorer 			log(LOG_DEBUG,
   4394   1.64    itojun 			    "%s: chap input <%s id=0x%x len=%d name=",
   4395   1.64    itojun 			    ifp->if_xname,
   4396    1.3  explorer 			    sppp_auth_type_name(PPP_CHAP, h->type), h->ident,
   4397    1.3  explorer 			    ntohs(h->len));
   4398   1.59    itojun 			sppp_print_string((char *) name, name_len);
   4399    1.3  explorer 			addlog(" value-size=%d value=", value_len);
   4400    1.3  explorer 			sppp_print_bytes(value, value_len);
   4401    1.3  explorer 			addlog(">\n");
   4402    1.3  explorer 		}
   4403    1.3  explorer 
   4404    1.3  explorer 		/* Compute reply value. */
   4405    1.3  explorer 		MD5Init(&ctx);
   4406    1.3  explorer 		MD5Update(&ctx, &h->ident, 1);
   4407   1.54  christos 		MD5Update(&ctx, sp->myauth.secret, sp->myauth.secret_len);
   4408    1.3  explorer 		MD5Update(&ctx, value, value_len);
   4409  1.201  yamaguch 		MD5Final(sp->chap.digest, &ctx);
   4410  1.201  yamaguch 		sp->chap.digest_len = sizeof(sp->chap.digest);
   4411  1.201  yamaguch 		sp->scp[IDX_CHAP].rconfid = h->ident;
   4412    1.3  explorer 
   4413  1.201  yamaguch 		sppp_wq_add(sp->wq_cp, &sp->chap.work_challenge_rcvd);
   4414    1.3  explorer 		break;
   4415    1.3  explorer 
   4416    1.3  explorer 	case CHAP_SUCCESS:
   4417    1.3  explorer 		if (debug) {
   4418   1.64    itojun 			log(LOG_DEBUG, "%s: chap success",
   4419   1.64    itojun 			    ifp->if_xname);
   4420    1.3  explorer 			if (len > 4) {
   4421    1.3  explorer 				addlog(": ");
   4422   1.59    itojun 				sppp_print_string((char *)(h + 1), len - 4);
   4423    1.3  explorer 			}
   4424    1.3  explorer 			addlog("\n");
   4425    1.3  explorer 		}
   4426  1.201  yamaguch 
   4427  1.201  yamaguch 		if (h->ident != sp->scp[IDX_CHAP].rconfid) {
   4428  1.201  yamaguch 			if (debug) {
   4429  1.201  yamaguch 				log(LOG_DEBUG, "%s: %s id mismatch 0x%x != 0x%x\n",
   4430  1.201  yamaguch 				       ifp->if_xname, chap.name,
   4431  1.201  yamaguch 				       h->ident, sp->scp[IDX_CHAP].rconfid);
   4432  1.201  yamaguch 			}
   4433  1.201  yamaguch 			if_statinc(ifp, if_ierrors);
   4434  1.201  yamaguch 			break;
   4435  1.201  yamaguch 		}
   4436  1.201  yamaguch 
   4437  1.201  yamaguch 		if (sp->chap.digest_len == 0) {
   4438  1.201  yamaguch 			if (debug) {
   4439  1.201  yamaguch 				log(LOG_DEBUG,
   4440  1.201  yamaguch 				    "%s: receive CHAP success without challenge\n",
   4441  1.201  yamaguch 				    ifp->if_xname);
   4442  1.201  yamaguch 			}
   4443  1.201  yamaguch 			if_statinc(ifp, if_ierrors);
   4444  1.201  yamaguch 			break;
   4445  1.201  yamaguch 		}
   4446  1.201  yamaguch 
   4447   1.23   thorpej 		x = splnet();
   4448   1.39    martin 		sp->pp_auth_failures = 0;
   4449    1.3  explorer 		sp->pp_flags &= ~PP_NEEDAUTH;
   4450  1.201  yamaguch 		splx(x);
   4451  1.201  yamaguch 		memset(sp->chap.digest, 0, sizeof(sp->chap.digest));
   4452  1.201  yamaguch 		sp->chap.digest_len = 0;
   4453  1.201  yamaguch 
   4454  1.201  yamaguch 		if (!ISSET(sppp_auth_role(&chap, sp), SPPP_AUTH_SERV)) {
   4455    1.3  explorer 			/*
   4456  1.201  yamaguch 			 * we are not authenticator for CHAP,
   4457  1.201  yamaguch 			 * generate a dummy RCR+ event without CHAP_RESPONSE
   4458    1.3  explorer 			 */
   4459  1.201  yamaguch 			sp->scp[IDX_CHAP].rcr_type = CONF_ACK;
   4460  1.201  yamaguch 			sppp_wq_add(sp->wq_cp, &sp->scp[IDX_CHAP].work_rcr);
   4461    1.3  explorer 		}
   4462  1.201  yamaguch 		sppp_wq_add(sp->wq_cp, &sp->scp[IDX_CHAP].work_rca);
   4463    1.3  explorer 		break;
   4464    1.3  explorer 
   4465    1.3  explorer 	case CHAP_FAILURE:
   4466  1.201  yamaguch 		if (h->ident != sp->scp[IDX_CHAP].rconfid) {
   4467  1.201  yamaguch 			if (debug) {
   4468  1.201  yamaguch 				log(LOG_DEBUG, "%s: %s id mismatch 0x%x != 0x%x\n",
   4469  1.201  yamaguch 				       ifp->if_xname, chap.name,
   4470  1.201  yamaguch 				       h->ident, sp->scp[IDX_CHAP].rconfid);
   4471  1.201  yamaguch 			}
   4472  1.201  yamaguch 			if_statinc(ifp, if_ierrors);
   4473  1.201  yamaguch 			break;
   4474  1.201  yamaguch 		}
   4475  1.201  yamaguch 
   4476  1.201  yamaguch 		if (sp->chap.digest_len == 0) {
   4477  1.201  yamaguch 			if (debug) {
   4478  1.201  yamaguch 				log(LOG_DEBUG,
   4479  1.201  yamaguch 				    "%s: receive CHAP failure without challenge\n",
   4480  1.201  yamaguch 				    ifp->if_xname);
   4481  1.201  yamaguch 			}
   4482  1.201  yamaguch 			if_statinc(ifp, if_ierrors);
   4483  1.201  yamaguch 			break;
   4484  1.201  yamaguch 		}
   4485  1.201  yamaguch 
   4486   1.39    martin 		x = splnet();
   4487   1.39    martin 		sp->pp_auth_failures++;
   4488   1.39    martin 		splx(x);
   4489    1.3  explorer 		if (debug) {
   4490   1.64    itojun 			log(LOG_INFO, "%s: chap failure",
   4491   1.64    itojun 			    ifp->if_xname);
   4492    1.3  explorer 			if (len > 4) {
   4493    1.3  explorer 				addlog(": ");
   4494   1.59    itojun 				sppp_print_string((char *)(h + 1), len - 4);
   4495    1.3  explorer 			}
   4496    1.3  explorer 			addlog("\n");
   4497    1.3  explorer 		} else
   4498   1.64    itojun 			log(LOG_INFO, "%s: chap failure\n",
   4499   1.64    itojun 			    ifp->if_xname);
   4500  1.201  yamaguch 
   4501  1.201  yamaguch 		memset(sp->chap.digest, 0, sizeof(sp->chap.digest));
   4502  1.201  yamaguch 		sp->chap.digest_len = 0;
   4503  1.201  yamaguch 		/*
   4504  1.201  yamaguch 		 * await LCP shutdown by authenticator,
   4505  1.201  yamaguch 		 * so we don't have to enqueue sc->scp[IDX_CHAP].work_rcn
   4506  1.201  yamaguch 		 */
   4507    1.3  explorer 		break;
   4508    1.3  explorer 
   4509    1.3  explorer 	/* response is my authproto */
   4510    1.3  explorer 	case CHAP_RESPONSE:
   4511  1.201  yamaguch 		if (sp->hisauth.name == NULL || sp->hisauth.secret == NULL) {
   4512  1.163  knakahar 			/* can't do anything useful */
   4513  1.201  yamaguch 			printf("%s: chap response"
   4514  1.201  yamaguch 			    " without his name and his secret being set\n",
   4515  1.163  knakahar 			    ifp->if_xname);
   4516   1.36    martin 		    break;
   4517   1.36    martin 		}
   4518   1.59    itojun 		value = 1 + (u_char *)(h + 1);
   4519    1.3  explorer 		value_len = value[-1];
   4520    1.3  explorer 		name = value + value_len;
   4521    1.3  explorer 		name_len = len - value_len - 5;
   4522    1.3  explorer 		if (name_len < 0) {
   4523    1.3  explorer 			if (debug) {
   4524    1.3  explorer 				log(LOG_DEBUG,
   4525   1.64    itojun 				    "%s: chap corrupted response "
   4526    1.3  explorer 				    "<%s id=0x%x len=%d",
   4527   1.64    itojun 				    ifp->if_xname,
   4528    1.3  explorer 				    sppp_auth_type_name(PPP_CHAP, h->type),
   4529    1.3  explorer 				    h->ident, ntohs(h->len));
   4530    1.3  explorer 				if (len > 4)
   4531   1.59    itojun 					sppp_print_bytes((u_char *)(h + 1),
   4532   1.59    itojun 					    len - 4);
   4533    1.3  explorer 				addlog(">\n");
   4534    1.3  explorer 			}
   4535    1.3  explorer 			break;
   4536    1.3  explorer 		}
   4537  1.192  yamaguch 		if (h->ident != sp->scp[IDX_CHAP].confid) {
   4538    1.3  explorer 			if (debug)
   4539    1.3  explorer 				log(LOG_DEBUG,
   4540   1.64    itojun 				    "%s: chap dropping response for old ID "
   4541    1.3  explorer 				    "(got %d, expected %d)\n",
   4542   1.64    itojun 				    ifp->if_xname,
   4543  1.192  yamaguch 				    h->ident, sp->scp[IDX_CHAP].confid);
   4544    1.3  explorer 			break;
   4545  1.201  yamaguch 		} else {
   4546  1.201  yamaguch 			sp->scp[IDX_CHAP].rconfid = h->ident;
   4547    1.3  explorer 		}
   4548  1.201  yamaguch 
   4549   1.82     perry 		if (sp->hisauth.name != NULL &&
   4550   1.54  christos 		    (name_len != sp->hisauth.name_len
   4551   1.36    martin 		    || memcmp(name, sp->hisauth.name, name_len) != 0)) {
   4552   1.64    itojun 			log(LOG_INFO, "%s: chap response, his name ",
   4553   1.64    itojun 			    ifp->if_xname);
   4554    1.3  explorer 			sppp_print_string(name, name_len);
   4555    1.3  explorer 			addlog(" != expected ");
   4556    1.3  explorer 			sppp_print_string(sp->hisauth.name,
   4557   1.54  christos 					  sp->hisauth.name_len);
   4558    1.3  explorer 			addlog("\n");
   4559  1.201  yamaguch 
   4560  1.201  yamaguch 			/* generate RCR- event */
   4561  1.201  yamaguch 			sp->scp[IDX_CHAP].rcr_type = CONF_NAK;
   4562  1.201  yamaguch 			sppp_wq_add(sp->wq_cp, &sp->scp[IDX_CHAP].work_rcr);
   4563  1.201  yamaguch 			break;
   4564   1.36    martin 		}
   4565  1.201  yamaguch 
   4566    1.3  explorer 		if (debug) {
   4567   1.64    itojun 			log(LOG_DEBUG, "%s: chap input(%s) "
   4568    1.3  explorer 			    "<%s id=0x%x len=%d name=",
   4569   1.64    itojun 			    ifp->if_xname,
   4570  1.192  yamaguch 			    sppp_state_name(sp->scp[IDX_CHAP].state),
   4571    1.3  explorer 			    sppp_auth_type_name(PPP_CHAP, h->type),
   4572   1.59    itojun 			    h->ident, ntohs(h->len));
   4573   1.59    itojun 			sppp_print_string((char *)name, name_len);
   4574    1.3  explorer 			addlog(" value-size=%d value=", value_len);
   4575    1.3  explorer 			sppp_print_bytes(value, value_len);
   4576    1.3  explorer 			addlog(">\n");
   4577    1.3  explorer 		}
   4578  1.201  yamaguch 
   4579  1.201  yamaguch 		if (value_len == sizeof(sp->chap.challenge) &&
   4580  1.201  yamaguch 		    value_len == sizeof(sp->chap.digest)) {
   4581  1.201  yamaguch 			MD5Init(&ctx);
   4582  1.201  yamaguch 			MD5Update(&ctx, &h->ident, 1);
   4583  1.201  yamaguch 			MD5Update(&ctx, sp->hisauth.secret, sp->hisauth.secret_len);
   4584  1.201  yamaguch 			MD5Update(&ctx, sp->chap.challenge, sizeof(sp->chap.challenge));
   4585  1.201  yamaguch 			MD5Final(digest, &ctx);
   4586  1.201  yamaguch 
   4587  1.201  yamaguch 			if (memcmp(digest, value, value_len) == 0) {
   4588  1.201  yamaguch 				sp->scp[IDX_CHAP].rcr_type = CONF_ACK;
   4589  1.201  yamaguch 				if (!ISSET(sppp_auth_role(&chap, sp), SPPP_AUTH_PEER) ||
   4590  1.201  yamaguch 				    sp->chap.rechallenging) {
   4591  1.201  yamaguch 					/* generate a dummy RCA event*/
   4592  1.201  yamaguch 					sppp_wq_add(sp->wq_cp, &sp->scp[IDX_CHAP].work_rca);
   4593  1.201  yamaguch 				}
   4594  1.201  yamaguch 			} else {
   4595  1.201  yamaguch 				sp->scp[IDX_CHAP].rcr_type = CONF_NAK;
   4596  1.201  yamaguch 			}
   4597  1.201  yamaguch 		} else {
   4598    1.3  explorer 			if (debug)
   4599    1.3  explorer 				log(LOG_DEBUG,
   4600   1.64    itojun 				    "%s: chap bad hash value length: "
   4601  1.148  christos 				    "%d bytes, should be %zu\n",
   4602   1.64    itojun 				    ifp->if_xname, value_len,
   4603  1.201  yamaguch 				    sizeof(sp->chap.challenge));
   4604  1.201  yamaguch 
   4605  1.201  yamaguch 			sp->scp[IDX_CHAP].rcr_type = CONF_NAK;
   4606    1.3  explorer 		}
   4607    1.3  explorer 
   4608  1.201  yamaguch 		sppp_wq_add(sp->wq_cp, &sp->scp[IDX_CHAP].work_rcr);
   4609    1.3  explorer 		break;
   4610    1.3  explorer 
   4611    1.3  explorer 	default:
   4612    1.3  explorer 		/* Unknown CHAP packet type -- ignore. */
   4613    1.3  explorer 		if (debug) {
   4614   1.64    itojun 			log(LOG_DEBUG, "%s: chap unknown input(%s) "
   4615    1.3  explorer 			    "<0x%x id=0x%xh len=%d",
   4616   1.64    itojun 			    ifp->if_xname,
   4617  1.192  yamaguch 			    sppp_state_name(sp->scp[IDX_CHAP].state),
   4618    1.3  explorer 			    h->type, h->ident, ntohs(h->len));
   4619    1.3  explorer 			if (len > 4)
   4620   1.59    itojun 				sppp_print_bytes((u_char *)(h + 1), len - 4);
   4621    1.3  explorer 			addlog(">\n");
   4622    1.3  explorer 		}
   4623    1.3  explorer 		break;
   4624    1.3  explorer 
   4625    1.3  explorer 	}
   4626    1.3  explorer 
   4627  1.170  knakahar 	SPPP_UNLOCK(sp);
   4628  1.163  knakahar }
   4629  1.163  knakahar 
   4630  1.170  knakahar static void
   4631  1.170  knakahar sppp_chap_init(struct sppp *sp)
   4632  1.163  knakahar {
   4633  1.163  knakahar 
   4634  1.170  knakahar 	KASSERT(SPPP_WLOCKED(sp));
   4635  1.163  knakahar 
   4636  1.201  yamaguch 	sp->scp[IDX_CHAP].state = STATE_INITIAL;
   4637  1.192  yamaguch 	sp->scp[IDX_CHAP].fail_counter = 0;
   4638  1.192  yamaguch 	sp->scp[IDX_CHAP].seq = 0;
   4639  1.192  yamaguch 	sp->scp[IDX_CHAP].rseq = 0;
   4640  1.201  yamaguch 	SPPP_WQ_SET(&sp->chap.work_challenge_rcvd,
   4641  1.201  yamaguch 	    sppp_chap_rcv_challenge_event, &chap);
   4642  1.201  yamaguch 	sppp_cp_init(&chap, sp);
   4643    1.3  explorer }
   4644    1.3  explorer 
   4645    1.3  explorer static void
   4646  1.201  yamaguch sppp_chap_open(struct sppp *sp, void *xcp)
   4647    1.3  explorer {
   4648    1.3  explorer 
   4649  1.201  yamaguch 	KASSERT(SPPP_WLOCKED(sp));
   4650  1.201  yamaguch 
   4651  1.201  yamaguch 	memset(sp->chap.digest, 0, sizeof(sp->chap.digest));
   4652  1.201  yamaguch 	sp->chap.digest_len = 0;
   4653  1.201  yamaguch 	sp->chap.rechallenging = false;
   4654  1.201  yamaguch 	sp->chap.response_rcvd = false;
   4655  1.201  yamaguch 	sppp_open_event(sp, xcp);
   4656  1.201  yamaguch }
   4657  1.163  knakahar 
   4658  1.201  yamaguch static void
   4659    1.3  explorer sppp_chap_tlu(struct sppp *sp)
   4660    1.3  explorer {
   4661    1.3  explorer 	STDDCL;
   4662    1.3  explorer 	int i, x;
   4663    1.3  explorer 
   4664  1.170  knakahar 	KASSERT(SPPP_WLOCKED(sp));
   4665  1.170  knakahar 
   4666    1.3  explorer 	i = 0;
   4667  1.192  yamaguch 	sp->scp[IDX_CHAP].rst_counter = sp->lcp.max_configure;
   4668  1.201  yamaguch 	x = splnet();
   4669  1.201  yamaguch 	sp->pp_auth_failures = 0;
   4670  1.201  yamaguch 	splx(x);
   4671  1.201  yamaguch 
   4672  1.201  yamaguch 	log(LOG_DEBUG, "%s: chap %s", ifp->if_xname,
   4673  1.201  yamaguch 	    sp->pp_phase == SPPP_PHASE_NETWORK ? "reconfirmed" : "tlu");
   4674    1.3  explorer 
   4675    1.3  explorer 	/*
   4676    1.3  explorer 	 * Some broken CHAP implementations (Conware CoNet, firmware
   4677    1.3  explorer 	 * 4.0.?) don't want to re-authenticate their CHAP once the
   4678    1.3  explorer 	 * initial challenge-response exchange has taken place.
   4679    1.3  explorer 	 * Provide for an option to avoid rechallenges.
   4680    1.3  explorer 	 */
   4681  1.201  yamaguch 	if (ISSET(sppp_auth_role(&chap, sp), SPPP_AUTH_SERV) &&
   4682  1.201  yamaguch 	    (sp->hisauth.flags & SPPP_AUTHFLAG_NORECHALLENGE) == 0) {
   4683    1.3  explorer 		/*
   4684    1.3  explorer 		 * Compute the re-challenge timeout.  This will yield
   4685    1.3  explorer 		 * a number between 300 and 810 seconds.
   4686    1.3  explorer 		 */
   4687  1.124       tls 		i = 300 + ((unsigned)(cprng_fast32() & 0xff00) >> 7);
   4688  1.198  yamaguch 		callout_schedule(&sp->scp[IDX_CHAP].ch, i * hz);
   4689    1.3  explorer 
   4690  1.201  yamaguch 		if (debug) {
   4691  1.201  yamaguch 			addlog(", next rechallenge in %d seconds", i);
   4692  1.201  yamaguch 		}
   4693    1.3  explorer 	}
   4694    1.3  explorer 
   4695  1.201  yamaguch 	addlog("\n");
   4696    1.3  explorer 
   4697    1.3  explorer 	/*
   4698    1.3  explorer 	 * If we are already in phase network, we are done here.  This
   4699    1.3  explorer 	 * is the case if this is a dummy tlu event after a re-challenge.
   4700    1.3  explorer 	 */
   4701   1.36    martin 	if (sp->pp_phase != SPPP_PHASE_NETWORK)
   4702    1.3  explorer 		sppp_phase_network(sp);
   4703    1.3  explorer }
   4704    1.3  explorer 
   4705    1.3  explorer static void
   4706    1.3  explorer sppp_chap_scr(struct sppp *sp)
   4707    1.3  explorer {
   4708  1.109      matt 	uint32_t *ch;
   4709  1.201  yamaguch 	u_char clen, dsize;
   4710  1.201  yamaguch 	int role;
   4711    1.3  explorer 
   4712  1.170  knakahar 	KASSERT(SPPP_WLOCKED(sp));
   4713  1.163  knakahar 
   4714  1.201  yamaguch 	role = sppp_auth_role(&chap, sp);
   4715  1.201  yamaguch 
   4716  1.201  yamaguch 	if (ISSET(role, SPPP_AUTH_SERV) &&
   4717  1.201  yamaguch 	    !sp->chap.response_rcvd) {
   4718  1.201  yamaguch 		/* we are authenticator for CHAP, send challenge */
   4719  1.201  yamaguch 		ch = (uint32_t *)sp->chap.challenge;
   4720  1.201  yamaguch 		clen = sizeof(sp->chap.challenge);
   4721  1.201  yamaguch 		/* Compute random challenge. */
   4722  1.201  yamaguch 		cprng_strong(kern_cprng, ch, clen, 0);
   4723  1.201  yamaguch 
   4724  1.201  yamaguch 		sp->scp[IDX_CHAP].confid = ++sp->scp[IDX_CHAP].seq;
   4725  1.201  yamaguch 		sppp_auth_send(&chap, sp, CHAP_CHALLENGE, sp->scp[IDX_CHAP].confid,
   4726  1.201  yamaguch 		    sizeof(clen), (const char *)&clen,
   4727  1.201  yamaguch 		    sizeof(sp->chap.challenge), sp->chap.challenge,
   4728  1.201  yamaguch 		    0);
   4729   1.36    martin 	}
   4730   1.36    martin 
   4731  1.201  yamaguch 	if (ISSET(role, SPPP_AUTH_PEER) &&
   4732  1.201  yamaguch 	    sp->chap.digest_len > 0) {
   4733  1.201  yamaguch 		/* we are peer for CHAP, send response */
   4734  1.201  yamaguch 		dsize = sp->chap.digest_len;
   4735    1.3  explorer 
   4736  1.201  yamaguch 		sppp_auth_send(&chap, sp, CHAP_RESPONSE, sp->scp[IDX_CHAP].rconfid,
   4737  1.201  yamaguch 		    sizeof(dsize), (const char *)&dsize,
   4738  1.201  yamaguch 		    sp->chap.digest_len, sp->chap.digest,
   4739  1.201  yamaguch 		    sp->myauth.name_len, sp->myauth.name, 0);
   4740  1.201  yamaguch 	}
   4741  1.201  yamaguch }
   4742  1.201  yamaguch 
   4743  1.201  yamaguch static void
   4744  1.201  yamaguch sppp_chap_rcv_challenge_event(struct sppp *sp, void *xcp)
   4745  1.201  yamaguch {
   4746  1.201  yamaguch 	const struct cp *cp = xcp;
   4747  1.201  yamaguch 
   4748  1.201  yamaguch 	sp->chap.rechallenging = false;
   4749  1.201  yamaguch 
   4750  1.201  yamaguch 	switch (sp->scp[IDX_CHAP].state) {
   4751  1.201  yamaguch 	case STATE_REQ_SENT:
   4752  1.201  yamaguch 		sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
   4753  1.201  yamaguch 		cp->scr(sp);
   4754  1.201  yamaguch 		break;
   4755  1.201  yamaguch 	case STATE_OPENED:
   4756  1.201  yamaguch 		sppp_cp_change_state(cp, sp, STATE_ACK_SENT);
   4757  1.201  yamaguch 		cp->scr(sp);
   4758  1.201  yamaguch 		break;
   4759  1.201  yamaguch 	}
   4760    1.3  explorer }
   4761   1.42  jdolecek 
   4762   1.42  jdolecek /*
   4763    1.3  explorer  *--------------------------------------------------------------------------*
   4764    1.3  explorer  *                                                                          *
   4765    1.3  explorer  *                        The PAP implementation.                           *
   4766    1.3  explorer  *                                                                          *
   4767    1.3  explorer  *--------------------------------------------------------------------------*
   4768    1.3  explorer  */
   4769    1.3  explorer /*
   4770  1.201  yamaguch  * PAP uses following actions and events.
   4771  1.201  yamaguch  * Actions:
   4772  1.201  yamaguch  *    - scr: send PAP_REQ
   4773  1.201  yamaguch  *    - sca: send PAP_ACK
   4774  1.201  yamaguch  *    - scn: send PAP_NAK
   4775  1.201  yamaguch  * Events:
   4776  1.201  yamaguch  *    - RCR+: receive PAP_REQ containing correct username and password
   4777  1.201  yamaguch  *    - RCR-: receive PAP_REQ containing wrong username and password
   4778  1.201  yamaguch  *    - RCA: receive PAP_ACK
   4779  1.201  yamaguch  *    - RCN: (this event is unused)
   4780  1.201  yamaguch  *    - TO+: re-send PAP_REQ
   4781  1.201  yamaguch  *    - TO-: this layer finish
   4782    1.3  explorer  */
   4783    1.3  explorer 
   4784    1.3  explorer /*
   4785    1.3  explorer  * Handle incoming PAP packets.  */
   4786    1.3  explorer static void
   4787    1.3  explorer sppp_pap_input(struct sppp *sp, struct mbuf *m)
   4788    1.3  explorer {
   4789    1.3  explorer 	STDDCL;
   4790    1.3  explorer 	struct lcp_header *h;
   4791    1.3  explorer 	int len, x;
   4792   1.53  christos 	char *name, *secret;
   4793   1.52  christos 	int name_len, secret_len;
   4794    1.3  explorer 
   4795   1.96   adrianp 	/*
   4796   1.96   adrianp 	 * Malicious input might leave this uninitialized, so
   4797   1.96   adrianp 	 * init to an impossible value.
   4798   1.96   adrianp 	 */
   4799   1.96   adrianp 	secret_len = -1;
   4800   1.96   adrianp 
   4801    1.3  explorer 	len = m->m_pkthdr.len;
   4802    1.3  explorer 	if (len < 5) {
   4803    1.3  explorer 		if (debug)
   4804    1.3  explorer 			log(LOG_DEBUG,
   4805   1.64    itojun 			    "%s: pap invalid packet length: %d bytes\n",
   4806   1.64    itojun 			    ifp->if_xname, len);
   4807    1.3  explorer 		return;
   4808    1.3  explorer 	}
   4809   1.59    itojun 	h = mtod(m, struct lcp_header *);
   4810   1.59    itojun 	if (len > ntohs(h->len))
   4811   1.59    itojun 		len = ntohs(h->len);
   4812  1.170  knakahar 
   4813  1.170  knakahar 	SPPP_LOCK(sp, RW_WRITER);
   4814  1.170  knakahar 
   4815    1.3  explorer 	switch (h->type) {
   4816    1.3  explorer 	/* PAP request is my authproto */
   4817    1.3  explorer 	case PAP_REQ:
   4818   1.36    martin 		if (sp->hisauth.name == NULL || sp->hisauth.secret == NULL) {
   4819  1.163  knakahar 			/* can't do anything useful */
   4820  1.201  yamaguch 			printf("%s: pap request"
   4821  1.201  yamaguch 			    " without his name and his secret being set\n",
   4822  1.163  knakahar 			    ifp->if_xname);
   4823  1.163  knakahar 			break;
   4824   1.36    martin 		}
   4825   1.59    itojun 		name = 1 + (u_char *)(h + 1);
   4826    1.3  explorer 		name_len = name[-1];
   4827   1.52  christos 		secret = name + name_len + 1;
   4828    1.3  explorer 		if (name_len > len - 6 ||
   4829   1.52  christos 		    (secret_len = secret[-1]) > len - 6 - name_len) {
   4830    1.3  explorer 			if (debug) {
   4831   1.64    itojun 				log(LOG_DEBUG, "%s: pap corrupted input "
   4832    1.3  explorer 				    "<%s id=0x%x len=%d",
   4833   1.64    itojun 				    ifp->if_xname,
   4834    1.3  explorer 				    sppp_auth_type_name(PPP_PAP, h->type),
   4835    1.3  explorer 				    h->ident, ntohs(h->len));
   4836    1.3  explorer 				if (len > 4)
   4837   1.59    itojun 					sppp_print_bytes((u_char *)(h + 1),
   4838   1.59    itojun 					    len - 4);
   4839    1.3  explorer 				addlog(">\n");
   4840    1.3  explorer 			}
   4841    1.3  explorer 			break;
   4842    1.3  explorer 		}
   4843    1.3  explorer 		if (debug) {
   4844   1.64    itojun 			log(LOG_DEBUG, "%s: pap input(%s) "
   4845    1.3  explorer 			    "<%s id=0x%x len=%d name=",
   4846   1.64    itojun 			    ifp->if_xname,
   4847  1.192  yamaguch 			    sppp_state_name(sp->scp[IDX_PAP].state),
   4848    1.3  explorer 			    sppp_auth_type_name(PPP_PAP, h->type),
   4849    1.3  explorer 			    h->ident, ntohs(h->len));
   4850   1.59    itojun 			sppp_print_string((char *)name, name_len);
   4851   1.52  christos 			addlog(" secret=");
   4852   1.59    itojun 			sppp_print_string((char *)secret, secret_len);
   4853    1.3  explorer 			addlog(">\n");
   4854    1.3  explorer 		}
   4855  1.201  yamaguch 
   4856  1.201  yamaguch 		sp->scp[IDX_PAP].rconfid = h->ident;
   4857  1.201  yamaguch 
   4858  1.201  yamaguch 		if (name_len == sp->hisauth.name_len &&
   4859  1.201  yamaguch 		    memcmp(name, sp->hisauth.name, name_len) == 0 &&
   4860  1.201  yamaguch 		    secret_len == sp->hisauth.secret_len &&
   4861  1.201  yamaguch 		    memcmp(secret, sp->hisauth.secret, secret_len) == 0) {
   4862  1.201  yamaguch 			sp->scp[IDX_PAP].rcr_type = CONF_ACK;
   4863  1.201  yamaguch 			if (!ISSET(sppp_auth_role(&pap, sp), SPPP_AUTH_PEER)) {
   4864  1.201  yamaguch 				/* generate a dummy RCA event*/
   4865  1.201  yamaguch 				sppp_wq_add(sp->wq_cp, &sp->scp[IDX_PAP].work_rca);
   4866  1.201  yamaguch 			}
   4867  1.201  yamaguch 		} else {
   4868  1.201  yamaguch 			sp->scp[IDX_PAP].rcr_type = CONF_NAK;
   4869    1.3  explorer 		}
   4870  1.201  yamaguch 
   4871  1.201  yamaguch 		sppp_wq_add(sp->wq_cp, &sp->scp[IDX_PAP].work_rcr);
   4872    1.3  explorer 		break;
   4873    1.3  explorer 
   4874    1.3  explorer 	/* ack and nak are his authproto */
   4875    1.3  explorer 	case PAP_ACK:
   4876    1.3  explorer 		if (debug) {
   4877   1.64    itojun 			log(LOG_DEBUG, "%s: pap success",
   4878   1.64    itojun 			    ifp->if_xname);
   4879   1.96   adrianp 			name = 1 + (u_char *)(h + 1);
   4880   1.96   adrianp 			name_len = name[-1];
   4881   1.96   adrianp 			if (len > 5 && name_len < len+4) {
   4882    1.3  explorer 				addlog(": ");
   4883   1.96   adrianp 				sppp_print_string(name, name_len);
   4884    1.3  explorer 			}
   4885    1.3  explorer 			addlog("\n");
   4886    1.3  explorer 		}
   4887  1.201  yamaguch 
   4888  1.201  yamaguch 		if (h->ident != sp->scp[IDX_PAP].confid) {
   4889  1.201  yamaguch 			if (debug) {
   4890  1.201  yamaguch 				log(LOG_DEBUG, "%s: %s id mismatch 0x%x != 0x%x\n",
   4891  1.201  yamaguch 				       ifp->if_xname, pap.name,
   4892  1.201  yamaguch 				       h->ident, sp->scp[IDX_PAP].rconfid);
   4893  1.201  yamaguch 			}
   4894  1.201  yamaguch 			if_statinc(ifp, if_ierrors);
   4895  1.201  yamaguch 			break;
   4896  1.201  yamaguch 		}
   4897  1.201  yamaguch 
   4898   1.23   thorpej 		x = splnet();
   4899   1.39    martin 		sp->pp_auth_failures = 0;
   4900    1.3  explorer 		sp->pp_flags &= ~PP_NEEDAUTH;
   4901  1.201  yamaguch 		splx(x);
   4902  1.201  yamaguch 
   4903  1.201  yamaguch 		/* we are not authenticator, generate a dummy RCR+ event */
   4904  1.201  yamaguch 		if (!ISSET(sppp_auth_role(&pap, sp), SPPP_AUTH_SERV)) {
   4905  1.201  yamaguch 			sp->scp[IDX_PAP].rcr_type = CONF_ACK;
   4906  1.201  yamaguch 			sppp_wq_add(sp->wq_cp, &sp->scp[IDX_PAP].work_rcr);
   4907    1.3  explorer 		}
   4908  1.201  yamaguch 
   4909  1.201  yamaguch 		sppp_wq_add(sp->wq_cp, &sp->scp[IDX_PAP].work_rca);
   4910    1.3  explorer 		break;
   4911    1.3  explorer 
   4912    1.3  explorer 	case PAP_NAK:
   4913    1.3  explorer 		if (debug) {
   4914   1.64    itojun 			log(LOG_INFO, "%s: pap failure",
   4915   1.64    itojun 			    ifp->if_xname);
   4916   1.96   adrianp 			name = 1 + (u_char *)(h + 1);
   4917   1.96   adrianp 			name_len = name[-1];
   4918   1.96   adrianp 			if (len > 5 && name_len < len+4) {
   4919    1.3  explorer 				addlog(": ");
   4920   1.96   adrianp 				sppp_print_string(name, name_len);
   4921    1.3  explorer 			}
   4922    1.3  explorer 			addlog("\n");
   4923    1.3  explorer 		} else
   4924   1.64    itojun 			log(LOG_INFO, "%s: pap failure\n",
   4925   1.64    itojun 			    ifp->if_xname);
   4926  1.201  yamaguch 
   4927  1.201  yamaguch 		if (h->ident != sp->scp[IDX_PAP].confid) {
   4928  1.201  yamaguch 			if (debug) {
   4929  1.201  yamaguch 				log(LOG_DEBUG, "%s: %s id mismatch 0x%x != 0x%x\n",
   4930  1.201  yamaguch 				       ifp->if_xname, pap.name,
   4931  1.201  yamaguch 				       h->ident, sp->scp[IDX_PAP].rconfid);
   4932  1.201  yamaguch 			}
   4933  1.201  yamaguch 			if_statinc(ifp, if_ierrors);
   4934  1.201  yamaguch 			break;
   4935  1.201  yamaguch 		}
   4936  1.201  yamaguch 
   4937  1.201  yamaguch 		sp->pp_auth_failures++;
   4938  1.201  yamaguch 		/*
   4939  1.201  yamaguch 		 * await LCP shutdown by authenticator,
   4940  1.201  yamaguch 		 * so we don't have to enqueue sc->scp[IDX_PAP].work_rcn
   4941  1.201  yamaguch 		 */
   4942    1.3  explorer 		break;
   4943    1.3  explorer 
   4944    1.3  explorer 	default:
   4945    1.3  explorer 		/* Unknown PAP packet type -- ignore. */
   4946    1.3  explorer 		if (debug) {
   4947   1.64    itojun 			log(LOG_DEBUG, "%s: pap corrupted input "
   4948    1.3  explorer 			    "<0x%x id=0x%x len=%d",
   4949   1.64    itojun 			    ifp->if_xname,
   4950    1.3  explorer 			    h->type, h->ident, ntohs(h->len));
   4951    1.3  explorer 			if (len > 4)
   4952   1.59    itojun 				sppp_print_bytes((u_char *)(h + 1), len - 4);
   4953    1.3  explorer 			addlog(">\n");
   4954    1.3  explorer 		}
   4955    1.3  explorer 		break;
   4956  1.170  knakahar 	}
   4957    1.3  explorer 
   4958  1.170  knakahar 	SPPP_UNLOCK(sp);
   4959    1.3  explorer }
   4960    1.3  explorer 
   4961    1.3  explorer static void
   4962    1.3  explorer sppp_pap_init(struct sppp *sp)
   4963    1.3  explorer {
   4964  1.170  knakahar 
   4965  1.170  knakahar 	KASSERT(SPPP_WLOCKED(sp));
   4966  1.170  knakahar 
   4967  1.201  yamaguch 	sp->scp[IDX_PAP].state = STATE_INITIAL;
   4968  1.192  yamaguch 	sp->scp[IDX_PAP].fail_counter = 0;
   4969  1.192  yamaguch 	sp->scp[IDX_PAP].seq = 0;
   4970  1.192  yamaguch 	sp->scp[IDX_PAP].rseq = 0;
   4971  1.201  yamaguch 	sppp_cp_init(&pap, sp);
   4972    1.3  explorer }
   4973    1.3  explorer 
   4974    1.3  explorer static void
   4975    1.3  explorer sppp_pap_tlu(struct sppp *sp)
   4976    1.3  explorer {
   4977    1.3  explorer 	STDDCL;
   4978    1.3  explorer 	int x;
   4979    1.3  explorer 
   4980  1.192  yamaguch 	sp->scp[IDX_PAP].rst_counter = sp->lcp.max_configure;
   4981    1.3  explorer 
   4982    1.3  explorer 	if (debug)
   4983   1.64    itojun 		log(LOG_DEBUG, "%s: %s tlu\n",
   4984   1.64    itojun 		    ifp->if_xname, pap.name);
   4985    1.3  explorer 
   4986   1.23   thorpej 	x = splnet();
   4987   1.39    martin 	sp->pp_auth_failures = 0;
   4988  1.201  yamaguch 	splx(x);
   4989    1.3  explorer 
   4990  1.202  yamaguch 	if (sp->pp_phase < SPPP_PHASE_NETWORK)
   4991  1.202  yamaguch 		sppp_phase_network(sp);
   4992    1.3  explorer }
   4993    1.3  explorer 
   4994    1.3  explorer static void
   4995    1.3  explorer sppp_pap_scr(struct sppp *sp)
   4996    1.3  explorer {
   4997    1.3  explorer 	u_char idlen, pwdlen;
   4998    1.3  explorer 
   4999  1.170  knakahar 	KASSERT(SPPP_WLOCKED(sp));
   5000  1.163  knakahar 
   5001  1.201  yamaguch 	if (ISSET(sppp_auth_role(&pap, sp), SPPP_AUTH_PEER) &&
   5002  1.201  yamaguch 	    sp->scp[IDX_PAP].state != STATE_ACK_RCVD) {
   5003  1.201  yamaguch 		if (sp->myauth.secret == NULL || sp->myauth.name == NULL) {
   5004  1.201  yamaguch 			log(LOG_DEBUG, "%s: couldn't send PAP_REQ "
   5005  1.201  yamaguch 			    "because of no name or no secret\n",
   5006  1.201  yamaguch 			    sp->pp_if.if_xname);
   5007  1.201  yamaguch 		} else {
   5008  1.201  yamaguch 			sp->scp[IDX_PAP].confid = ++sp->scp[IDX_PAP].seq;
   5009  1.201  yamaguch 			pwdlen = sp->myauth.secret_len;
   5010  1.201  yamaguch 			idlen = sp->myauth.name_len;
   5011  1.201  yamaguch 
   5012  1.201  yamaguch 			sppp_auth_send(&pap, sp, PAP_REQ, sp->scp[IDX_PAP].confid,
   5013  1.201  yamaguch 			    sizeof idlen, (const char *)&idlen,
   5014  1.201  yamaguch 			    idlen, sp->myauth.name,
   5015  1.201  yamaguch 			    sizeof pwdlen, (const char *)&pwdlen,
   5016  1.201  yamaguch 			    pwdlen, sp->myauth.secret,
   5017  1.201  yamaguch 			    0);
   5018  1.201  yamaguch 		}
   5019  1.201  yamaguch 	}
   5020  1.201  yamaguch }
   5021  1.201  yamaguch 
   5022   1.42  jdolecek /*
   5023    1.3  explorer  * Random miscellaneous functions.
   5024    1.3  explorer  */
   5025    1.3  explorer 
   5026    1.3  explorer /*
   5027    1.3  explorer  * Send a PAP or CHAP proto packet.
   5028    1.3  explorer  *
   5029    1.3  explorer  * Varadic function, each of the elements for the ellipsis is of type
   5030    1.3  explorer  * ``size_t mlen, const u_char *msg''.  Processing will stop iff
   5031    1.3  explorer  * mlen == 0.
   5032    1.3  explorer  * NOTE: never declare variadic functions with types subject to type
   5033    1.3  explorer  * promotion (i.e. u_char). This is asking for big trouble depending
   5034    1.3  explorer  * on the architecture you are on...
   5035    1.3  explorer  */
   5036    1.3  explorer 
   5037    1.3  explorer static void
   5038    1.3  explorer sppp_auth_send(const struct cp *cp, struct sppp *sp,
   5039    1.3  explorer                unsigned int type, unsigned int id,
   5040    1.3  explorer 	       ...)
   5041    1.3  explorer {
   5042    1.3  explorer 	STDDCL;
   5043    1.3  explorer 	struct lcp_header *lh;
   5044    1.3  explorer 	struct mbuf *m;
   5045    1.3  explorer 	u_char *p;
   5046    1.3  explorer 	int len;
   5047   1.22    martin 	size_t pkthdrlen;
   5048    1.3  explorer 	unsigned int mlen;
   5049    1.3  explorer 	const char *msg;
   5050    1.3  explorer 	va_list ap;
   5051    1.3  explorer 
   5052  1.170  knakahar 	KASSERT(SPPP_WLOCKED(sp));
   5053  1.163  knakahar 
   5054   1.59    itojun 	MGETHDR(m, M_DONTWAIT, MT_DATA);
   5055    1.3  explorer 	if (! m)
   5056    1.3  explorer 		return;
   5057  1.142     ozaki 	m_reset_rcvif(m);
   5058    1.3  explorer 
   5059   1.22    martin 	if (sp->pp_flags & PP_NOFRAMING) {
   5060  1.109      matt 		*mtod(m, uint16_t *) = htons(cp->proto);
   5061   1.22    martin 		pkthdrlen = 2;
   5062  1.109      matt 		lh = (struct lcp_header *)(mtod(m, uint8_t *)+2);
   5063   1.22    martin 	} else {
   5064   1.22    martin 		struct ppp_header *h;
   5065   1.59    itojun 		h = mtod(m, struct ppp_header *);
   5066   1.22    martin 		h->address = PPP_ALLSTATIONS;		/* broadcast address */
   5067   1.22    martin 		h->control = PPP_UI;			/* Unnumbered Info */
   5068   1.22    martin 		h->protocol = htons(cp->proto);
   5069   1.22    martin 		pkthdrlen = PPP_HEADER_LEN;
   5070   1.22    martin 
   5071   1.59    itojun 		lh = (struct lcp_header *)(h + 1);
   5072   1.22    martin 	}
   5073    1.3  explorer 
   5074    1.3  explorer 	lh->type = type;
   5075    1.3  explorer 	lh->ident = id;
   5076   1.59    itojun 	p = (u_char *)(lh + 1);
   5077    1.3  explorer 
   5078    1.3  explorer 	va_start(ap, id);
   5079    1.3  explorer 	len = 0;
   5080    1.3  explorer 
   5081    1.3  explorer 	while ((mlen = (unsigned int)va_arg(ap, size_t)) != 0) {
   5082    1.3  explorer 		msg = va_arg(ap, const char *);
   5083    1.3  explorer 		len += mlen;
   5084   1.22    martin 		if (len > MHLEN - pkthdrlen - LCP_HEADER_LEN) {
   5085    1.3  explorer 			va_end(ap);
   5086    1.3  explorer 			m_freem(m);
   5087    1.3  explorer 			return;
   5088    1.3  explorer 		}
   5089    1.3  explorer 
   5090  1.118   tsutsui 		memcpy(p, msg, mlen);
   5091    1.3  explorer 		p += mlen;
   5092    1.3  explorer 	}
   5093    1.3  explorer 	va_end(ap);
   5094    1.3  explorer 
   5095   1.22    martin 	m->m_pkthdr.len = m->m_len = pkthdrlen + LCP_HEADER_LEN + len;
   5096   1.59    itojun 	lh->len = htons(LCP_HEADER_LEN + len);
   5097    1.3  explorer 
   5098    1.3  explorer 	if (debug) {
   5099   1.64    itojun 		log(LOG_DEBUG, "%s: %s output <%s id=0x%x len=%d",
   5100   1.64    itojun 		    ifp->if_xname, cp->name,
   5101    1.3  explorer 		    sppp_auth_type_name(cp->proto, lh->type),
   5102    1.3  explorer 		    lh->ident, ntohs(lh->len));
   5103    1.3  explorer 		if (len)
   5104   1.59    itojun 			sppp_print_bytes((u_char *)(lh + 1), len);
   5105    1.3  explorer 		addlog(">\n");
   5106    1.3  explorer 	}
   5107   1.59    itojun 	if (IF_QFULL(&sp->pp_cpq)) {
   5108   1.59    itojun 		IF_DROP(&sp->pp_fastq);
   5109   1.59    itojun 		IF_DROP(&ifp->if_snd);
   5110   1.59    itojun 		m_freem(m);
   5111  1.185   thorpej 		if_statinc(ifp, if_oerrors);
   5112   1.50      yamt 		return;
   5113  1.169     ozaki 	}
   5114  1.169     ozaki 
   5115  1.185   thorpej 	if_statadd(ifp, if_obytes, m->m_pkthdr.len + sp->pp_framebytes);
   5116  1.169     ozaki 	IF_ENQUEUE(&sp->pp_cpq, m);
   5117  1.169     ozaki 
   5118  1.163  knakahar 	if (! (ifp->if_flags & IFF_OACTIVE)) {
   5119  1.170  knakahar 		SPPP_UNLOCK(sp);
   5120  1.143  knakahar 		if_start_lock(ifp);
   5121  1.170  knakahar 		SPPP_LOCK(sp, RW_WRITER);
   5122  1.163  knakahar 	}
   5123    1.3  explorer }
   5124    1.3  explorer 
   5125  1.201  yamaguch static int
   5126  1.201  yamaguch sppp_auth_role(const struct cp *cp, struct sppp *sp)
   5127  1.201  yamaguch {
   5128  1.201  yamaguch 	int role;
   5129  1.201  yamaguch 
   5130  1.201  yamaguch 	role = SPPP_AUTH_NOROLE;
   5131  1.201  yamaguch 
   5132  1.201  yamaguch 	if (sp->hisauth.proto == cp->proto &&
   5133  1.201  yamaguch 	    (sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) != 0)
   5134  1.201  yamaguch 		SET(role, SPPP_AUTH_SERV);
   5135  1.201  yamaguch 
   5136  1.201  yamaguch 	if (sp->myauth.proto == cp->proto)
   5137  1.201  yamaguch 		SET(role, SPPP_AUTH_PEER);
   5138  1.201  yamaguch 
   5139  1.201  yamaguch 	return role;
   5140  1.201  yamaguch }
   5141  1.201  yamaguch 
   5142  1.201  yamaguch static void
   5143  1.202  yamaguch sppp_auth_to_event(struct sppp *sp, void *xcp)
   5144  1.201  yamaguch {
   5145  1.202  yamaguch 	const struct cp *cp = xcp;
   5146  1.201  yamaguch 	bool override;
   5147  1.201  yamaguch 	int state;
   5148  1.202  yamaguch 	STDDCL;
   5149  1.201  yamaguch 
   5150  1.201  yamaguch 	KASSERT(SPPP_WLOCKED(sp));
   5151  1.201  yamaguch 
   5152  1.201  yamaguch 	override = false;
   5153  1.201  yamaguch 	state = sp->scp[cp->protoidx].state;
   5154  1.201  yamaguch 
   5155  1.201  yamaguch 	if (sp->scp[cp->protoidx].rst_counter > 0) {
   5156  1.201  yamaguch 		/* override TO+ event */
   5157  1.201  yamaguch 		switch (state) {
   5158  1.201  yamaguch 		case STATE_OPENED:
   5159  1.201  yamaguch 			if ((sp->hisauth.flags & SPPP_AUTHFLAG_NORECHALLENGE) == 0) {
   5160  1.201  yamaguch 				override = true;
   5161  1.201  yamaguch 				sp->chap.rechallenging = true;
   5162  1.201  yamaguch 				sp->chap.response_rcvd = false;
   5163  1.201  yamaguch 				sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
   5164  1.201  yamaguch 				cp->scr(sp);
   5165  1.201  yamaguch 			}
   5166  1.201  yamaguch 			break;
   5167  1.201  yamaguch 
   5168  1.201  yamaguch 		case STATE_ACK_RCVD:
   5169  1.201  yamaguch 			override = true;
   5170  1.201  yamaguch 			cp->scr(sp);
   5171  1.201  yamaguch 			callout_schedule(&sp->scp[cp->protoidx].ch, sp->lcp.timeout);
   5172  1.201  yamaguch 			break;
   5173  1.201  yamaguch 		}
   5174  1.201  yamaguch 	}
   5175  1.201  yamaguch 
   5176  1.201  yamaguch 	if (override) {
   5177  1.201  yamaguch 		if (debug)
   5178  1.201  yamaguch 			log(LOG_DEBUG, "%s: %s TO(%s) rst_counter = %d\n",
   5179  1.201  yamaguch 			    ifp->if_xname, cp->name,
   5180  1.201  yamaguch 			    sppp_state_name(state),
   5181  1.201  yamaguch 			    sp->scp[cp->protoidx].rst_counter);
   5182  1.201  yamaguch 		sp->scp[cp->protoidx].rst_counter--;
   5183  1.201  yamaguch 	} else {
   5184  1.202  yamaguch 		sppp_to_event(sp, xcp);
   5185  1.202  yamaguch 	}
   5186  1.202  yamaguch }
   5187  1.202  yamaguch 
   5188  1.202  yamaguch static void
   5189  1.202  yamaguch sppp_auth_sca_scn(const struct cp *cp, struct sppp *sp)
   5190  1.202  yamaguch {
   5191  1.202  yamaguch 	static const char *succmsg = "Welcome!";
   5192  1.202  yamaguch 	static const char *failmsg = "Failed...";
   5193  1.202  yamaguch 	const char *msg;
   5194  1.202  yamaguch 	u_char type, rconfid, mlen;
   5195  1.202  yamaguch 
   5196  1.202  yamaguch 	KASSERT(SPPP_WLOCKED(sp));
   5197  1.202  yamaguch 
   5198  1.202  yamaguch 	if (!ISSET(sppp_auth_role(cp, sp), SPPP_AUTH_SERV))
   5199  1.202  yamaguch 		return;
   5200  1.202  yamaguch 
   5201  1.202  yamaguch 	rconfid = sp->scp[cp->protoidx].rconfid;
   5202  1.202  yamaguch 
   5203  1.202  yamaguch 	if (sp->scp[cp->protoidx].rcr_type == CONF_ACK) {
   5204  1.202  yamaguch 		type = cp->proto == PPP_CHAP ? CHAP_SUCCESS : PAP_ACK;
   5205  1.202  yamaguch 		msg = succmsg;
   5206  1.202  yamaguch 		mlen = sizeof(succmsg) - 1;
   5207  1.202  yamaguch 
   5208  1.202  yamaguch 		sp->pp_auth_failures = 0;
   5209  1.202  yamaguch 	} else {
   5210  1.202  yamaguch 		type = cp->proto == PPP_CHAP ? CHAP_FAILURE : PAP_NAK;
   5211  1.202  yamaguch 		msg = failmsg;
   5212  1.202  yamaguch 		mlen = sizeof(failmsg) - 1;
   5213  1.202  yamaguch 
   5214  1.202  yamaguch 		/* shutdown LCP if auth failed */
   5215  1.202  yamaguch 		sppp_wq_add(sp->wq_cp, &sp->scp[IDX_LCP].work_close);
   5216  1.202  yamaguch 		sp->pp_auth_failures++;
   5217  1.201  yamaguch 	}
   5218  1.202  yamaguch 
   5219  1.202  yamaguch 	sppp_auth_send(cp, sp, type, rconfid,
   5220  1.202  yamaguch 	   mlen, (const u_char *)msg, 0);
   5221  1.201  yamaguch }
   5222  1.201  yamaguch 
   5223    1.3  explorer /*
   5224    1.3  explorer  * Send keepalive packets, every 10 seconds.
   5225    1.3  explorer  */
   5226    1.3  explorer static void
   5227  1.101  christos sppp_keepalive(void *dummy)
   5228    1.3  explorer {
   5229    1.3  explorer 	struct sppp *sp;
   5230    1.3  explorer 	int s;
   5231   1.38    martin 	time_t now;
   5232    1.3  explorer 
   5233  1.163  knakahar 	SPPPQ_LOCK();
   5234  1.163  knakahar 
   5235   1.23   thorpej 	s = splnet();
   5236   1.92    kardel 	now = time_uptime;
   5237    1.3  explorer 	for (sp=spppq; sp; sp=sp->pp_next) {
   5238  1.163  knakahar 		struct ifnet *ifp = NULL;
   5239  1.163  knakahar 
   5240  1.170  knakahar 		SPPP_LOCK(sp, RW_WRITER);
   5241  1.163  knakahar 		ifp = &sp->pp_if;
   5242    1.3  explorer 
   5243   1.38    martin 		/* check idle timeout */
   5244   1.40    martin 		if ((sp->pp_idle_timeout != 0) && (ifp->if_flags & IFF_RUNNING)
   5245   1.40    martin 		    && (sp->pp_phase == SPPP_PHASE_NETWORK)) {
   5246   1.38    martin 		    /* idle timeout is enabled for this interface */
   5247   1.38    martin 		    if ((now-sp->pp_last_activity) >= sp->pp_idle_timeout) {
   5248   1.38    martin 		    	if (ifp->if_flags & IFF_DEBUG)
   5249   1.84    martin 			    printf("%s: no activity for %lu seconds\n",
   5250   1.38    martin 				sp->pp_if.if_xname,
   5251   1.38    martin 				(unsigned long)(now-sp->pp_last_activity));
   5252  1.198  yamaguch 			sppp_wq_add(sp->wq_cp, &sp->scp[IDX_LCP].work_close);
   5253  1.170  knakahar 			SPPP_UNLOCK(sp);
   5254   1.38    martin 			continue;
   5255   1.38    martin 		    }
   5256   1.38    martin 		}
   5257   1.38    martin 
   5258    1.3  explorer 		/* Keepalive mode disabled or channel down? */
   5259    1.3  explorer 		if (! (sp->pp_flags & PP_KEEPALIVE) ||
   5260  1.163  knakahar 		    ! (ifp->if_flags & IFF_RUNNING)) {
   5261  1.170  knakahar 			SPPP_UNLOCK(sp);
   5262    1.3  explorer 			continue;
   5263  1.163  knakahar 		}
   5264  1.163  knakahar 
   5265    1.3  explorer 
   5266    1.3  explorer 		/* No keepalive in PPP mode if LCP not opened yet. */
   5267    1.3  explorer 		if (! (sp->pp_flags & PP_CISCO) &&
   5268  1.163  knakahar 		    sp->pp_phase < SPPP_PHASE_AUTHENTICATE) {
   5269  1.170  knakahar 			SPPP_UNLOCK(sp);
   5270    1.3  explorer 			continue;
   5271  1.163  knakahar 		}
   5272    1.3  explorer 
   5273   1.57    martin 		/* No echo reply, but maybe user data passed through? */
   5274   1.68    martin 		if ((now - sp->pp_last_receive) < sp->pp_max_noreceive) {
   5275   1.57    martin 			sp->pp_alivecnt = 0;
   5276  1.170  knakahar 			SPPP_UNLOCK(sp);
   5277   1.57    martin 			continue;
   5278   1.57    martin 		}
   5279   1.57    martin 
   5280   1.68    martin 		if (sp->pp_alivecnt >= sp->pp_maxalive) {
   5281    1.3  explorer 			/* No keepalive packets got.  Stop the interface. */
   5282  1.199  yamaguch 			sppp_wq_add(sp->wq_cp, &sp->work_ifdown);
   5283  1.170  knakahar 
   5284    1.3  explorer 			if (! (sp->pp_flags & PP_CISCO)) {
   5285   1.33    martin 				printf("%s: LCP keepalive timed out, going to restart the connection\n",
   5286   1.33    martin 					ifp->if_xname);
   5287   1.33    martin 				sp->pp_alivecnt = 0;
   5288   1.33    martin 
   5289   1.33    martin 				/* we are down, close all open protocols */
   5290  1.198  yamaguch 				sppp_wq_add(sp->wq_cp, &sp->scp[IDX_LCP].work_close);
   5291   1.33    martin 
   5292   1.33    martin 				/* And now prepare LCP to reestablish the link, if configured to do so. */
   5293  1.198  yamaguch 				sp->lcp.reestablish = true;
   5294  1.163  knakahar 
   5295  1.170  knakahar 				SPPP_UNLOCK(sp);
   5296   1.33    martin 				continue;
   5297    1.3  explorer 			}
   5298    1.3  explorer 		}
   5299   1.68    martin 		if (sp->pp_alivecnt < sp->pp_maxalive)
   5300    1.3  explorer 			++sp->pp_alivecnt;
   5301    1.3  explorer 		if (sp->pp_flags & PP_CISCO)
   5302   1.59    itojun 			sppp_cisco_send(sp, CISCO_KEEPALIVE_REQ,
   5303  1.192  yamaguch 			    ++sp->scp[IDX_LCP].seq, sp->scp[IDX_LCP].rseq);
   5304   1.36    martin 		else if (sp->pp_phase >= SPPP_PHASE_AUTHENTICATE) {
   5305   1.59    itojun 			int32_t nmagic = htonl(sp->lcp.magic);
   5306  1.192  yamaguch 			sp->lcp.echoid = ++sp->scp[IDX_LCP].seq;
   5307   1.59    itojun 			sppp_cp_send(sp, PPP_LCP, ECHO_REQ,
   5308    1.3  explorer 				sp->lcp.echoid, 4, &nmagic);
   5309    1.3  explorer 		}
   5310  1.163  knakahar 
   5311  1.170  knakahar 		SPPP_UNLOCK(sp);
   5312    1.3  explorer 	}
   5313    1.3  explorer 	splx(s);
   5314   1.57    martin 	callout_reset(&keepalive_ch, hz * LCP_KEEPALIVE_INTERVAL, sppp_keepalive, NULL);
   5315  1.163  knakahar 
   5316  1.163  knakahar 	SPPPQ_UNLOCK();
   5317    1.3  explorer }
   5318    1.3  explorer 
   5319   1.79  christos #ifdef INET
   5320    1.3  explorer /*
   5321    1.3  explorer  * Get both IP addresses.
   5322    1.3  explorer  */
   5323    1.3  explorer static void
   5324  1.109      matt sppp_get_ip_addrs(struct sppp *sp, uint32_t *src, uint32_t *dst, uint32_t *srcmask)
   5325    1.3  explorer {
   5326    1.3  explorer 	struct ifnet *ifp = &sp->pp_if;
   5327    1.3  explorer 	struct ifaddr *ifa;
   5328    1.3  explorer 	struct sockaddr_in *si, *sm;
   5329  1.109      matt 	uint32_t ssrc, ddst;
   5330  1.173     ozaki 	int s;
   5331  1.173     ozaki 	struct psref psref;
   5332    1.3  explorer 
   5333    1.3  explorer 	sm = NULL;
   5334   1.30      ross 	ssrc = ddst = 0;
   5335    1.3  explorer 	/*
   5336    1.3  explorer 	 * Pick the first AF_INET address from the list,
   5337    1.3  explorer 	 * aliases don't make any sense on a p2p link anyway.
   5338    1.3  explorer 	 */
   5339   1.28      matt 	si = 0;
   5340  1.173     ozaki 	s = pserialize_read_enter();
   5341  1.146     ozaki 	IFADDR_READER_FOREACH(ifa, ifp) {
   5342    1.3  explorer 		if (ifa->ifa_addr->sa_family == AF_INET) {
   5343    1.3  explorer 			si = (struct sockaddr_in *)ifa->ifa_addr;
   5344    1.3  explorer 			sm = (struct sockaddr_in *)ifa->ifa_netmask;
   5345  1.173     ozaki 			if (si) {
   5346  1.173     ozaki 				ifa_acquire(ifa, &psref);
   5347    1.3  explorer 				break;
   5348  1.173     ozaki 			}
   5349    1.3  explorer 		}
   5350   1.28      matt 	}
   5351  1.173     ozaki 	pserialize_read_exit(s);
   5352    1.3  explorer 	if (ifa) {
   5353    1.3  explorer 		if (si && si->sin_addr.s_addr) {
   5354    1.3  explorer 			ssrc = si->sin_addr.s_addr;
   5355    1.3  explorer 			if (srcmask)
   5356    1.3  explorer 				*srcmask = ntohl(sm->sin_addr.s_addr);
   5357    1.3  explorer 		}
   5358    1.3  explorer 
   5359    1.3  explorer 		si = (struct sockaddr_in *)ifa->ifa_dstaddr;
   5360    1.3  explorer 		if (si && si->sin_addr.s_addr)
   5361    1.3  explorer 			ddst = si->sin_addr.s_addr;
   5362  1.173     ozaki 		ifa_release(ifa, &psref);
   5363    1.3  explorer 	}
   5364    1.3  explorer 
   5365    1.3  explorer 	if (dst) *dst = ntohl(ddst);
   5366    1.3  explorer 	if (src) *src = ntohl(ssrc);
   5367    1.3  explorer }
   5368    1.3  explorer 
   5369    1.3  explorer /*
   5370   1.31    martin  * Set IP addresses.  Must be called at splnet.
   5371   1.31    martin  * If an address is 0, leave it the way it is.
   5372    1.3  explorer  */
   5373    1.3  explorer static void
   5374  1.161  knakahar sppp_set_ip_addrs_work(struct work *wk, struct sppp *sp)
   5375    1.3  explorer {
   5376    1.3  explorer 	STDDCL;
   5377    1.3  explorer 	struct ifaddr *ifa;
   5378   1.73   mycroft 	struct sockaddr_in *si, *dest;
   5379  1.158  knakahar 	uint32_t myaddr = 0, hisaddr = 0;
   5380  1.159  knakahar 	int s;
   5381  1.159  knakahar 
   5382  1.177     ozaki 	IFNET_LOCK(ifp);
   5383  1.176     ozaki 
   5384    1.3  explorer 	/*
   5385    1.3  explorer 	 * Pick the first AF_INET address from the list,
   5386    1.3  explorer 	 * aliases don't make any sense on a p2p link anyway.
   5387    1.3  explorer 	 */
   5388  1.158  knakahar 	si = dest = NULL;
   5389  1.159  knakahar 	s = pserialize_read_enter();
   5390  1.146     ozaki 	IFADDR_READER_FOREACH(ifa, ifp) {
   5391   1.73   mycroft 		if (ifa->ifa_addr->sa_family == AF_INET) {
   5392    1.3  explorer 			si = (struct sockaddr_in *)ifa->ifa_addr;
   5393   1.31    martin 			dest = (struct sockaddr_in *)ifa->ifa_dstaddr;
   5394  1.158  knakahar 			break;
   5395    1.3  explorer 		}
   5396    1.3  explorer 	}
   5397  1.159  knakahar 	pserialize_read_exit(s);
   5398    1.3  explorer 
   5399  1.158  knakahar 	if ((sp->ipcp.flags & IPCP_MYADDR_DYN) && (sp->ipcp.flags & IPCP_MYADDR_SEEN))
   5400  1.158  knakahar 		myaddr = sp->ipcp.req_myaddr;
   5401  1.158  knakahar 	else if (si != NULL)
   5402  1.158  knakahar 		myaddr = ntohl(si->sin_addr.s_addr);
   5403  1.158  knakahar 
   5404  1.158  knakahar 	if ((sp->ipcp.flags & IPCP_HISADDR_DYN) && (sp->ipcp.flags & IPCP_HISADDR_SEEN))
   5405  1.158  knakahar 		hisaddr = sp->ipcp.req_hisaddr;
   5406  1.158  knakahar 	else if (dest != NULL)
   5407  1.158  knakahar 		hisaddr = ntohl(dest->sin_addr.s_addr);
   5408  1.158  knakahar 
   5409  1.160  knakahar 	if (si != NULL && dest != NULL) {
   5410  1.152       roy 		int error;
   5411    1.3  explorer 		struct sockaddr_in new_sin = *si;
   5412   1.31    martin 		struct sockaddr_in new_dst = *dest;
   5413    1.3  explorer 
   5414  1.152       roy 		if (myaddr != 0)
   5415  1.152       roy 			new_sin.sin_addr.s_addr = htonl(myaddr);
   5416   1.31    martin 		if (hisaddr != 0) {
   5417   1.31    martin 			new_dst.sin_addr.s_addr = htonl(hisaddr);
   5418  1.154       roy 			if (new_dst.sin_addr.s_addr != dest->sin_addr.s_addr)
   5419   1.31    martin 				sp->ipcp.saved_hisaddr = dest->sin_addr.s_addr;
   5420   1.31    martin 		}
   5421  1.145     ozaki 
   5422  1.162  knakahar 		in_addrhash_remove(ifatoia(ifa));
   5423  1.145     ozaki 
   5424  1.154       roy 		error = in_ifinit(ifp, ifatoia(ifa), &new_sin, &new_dst, 0);
   5425  1.145     ozaki 
   5426  1.162  knakahar 		in_addrhash_insert(ifatoia(ifa));
   5427  1.145     ozaki 
   5428   1.70    itojun 		if (debug && error)
   5429    1.3  explorer 		{
   5430  1.153       roy 			log(LOG_DEBUG, "%s: %s: in_ifinit failed, error=%d\n",
   5431  1.153       roy 			    ifp->if_xname, __func__, error);
   5432    1.3  explorer 		}
   5433  1.127     rmind 		if (!error) {
   5434  1.164  christos 			pfil_run_addrhooks(if_pfil, SIOCAIFADDR, ifa);
   5435  1.127     rmind 		}
   5436   1.31    martin 	}
   5437  1.159  knakahar 
   5438  1.159  knakahar 	if (ifp->if_mtu > sp->lcp.their_mru) {
   5439  1.159  knakahar 		sp->pp_saved_mtu = ifp->if_mtu;
   5440  1.159  knakahar 		ifp->if_mtu = sp->lcp.their_mru;
   5441  1.159  knakahar 		if (debug)
   5442  1.159  knakahar 			log(LOG_DEBUG,
   5443  1.159  knakahar 			    "%s: setting MTU to %" PRIu64 " bytes\n",
   5444  1.159  knakahar 			    ifp->if_xname, ifp->if_mtu);
   5445  1.159  knakahar 	}
   5446  1.159  knakahar 
   5447  1.177     ozaki 	IFNET_UNLOCK(ifp);
   5448  1.176     ozaki 
   5449  1.170  knakahar 	sppp_notify_con(sp);
   5450  1.159  knakahar }
   5451  1.159  knakahar 
   5452  1.159  knakahar static void
   5453  1.159  knakahar sppp_set_ip_addrs(struct sppp *sp)
   5454  1.159  knakahar {
   5455  1.161  knakahar 	struct ifnet *ifp = &sp->pp_if;
   5456  1.159  knakahar 
   5457  1.161  knakahar 	if (!pcq_put(sp->ipcp.update_addrs_q, (void *)IPCP_SET_ADDRS)) {
   5458  1.161  knakahar 		log(LOG_WARNING, "%s: cannot enqueued, ignore sppp_clear_ip_addrs\n",
   5459  1.161  knakahar 		    ifp->if_xname);
   5460  1.161  knakahar 		return;
   5461  1.161  knakahar 	}
   5462  1.161  knakahar 
   5463  1.161  knakahar 	if (atomic_swap_uint(&sp->ipcp.update_addrs_enqueued, 1) == 1)
   5464  1.159  knakahar 		return;
   5465  1.159  knakahar 
   5466  1.161  knakahar 	workqueue_enqueue(sp->ipcp.update_addrs_wq, &sp->ipcp.update_addrs_wk, NULL);
   5467   1.82     perry }
   5468   1.31    martin 
   5469   1.31    martin /*
   5470   1.31    martin  * Clear IP addresses.  Must be called at splnet.
   5471   1.31    martin  */
   5472   1.31    martin static void
   5473  1.161  knakahar sppp_clear_ip_addrs_work(struct work *wk, struct sppp *sp)
   5474   1.31    martin {
   5475  1.153       roy 	STDDCL;
   5476   1.31    martin 	struct ifaddr *ifa;
   5477   1.73   mycroft 	struct sockaddr_in *si, *dest;
   5478  1.159  knakahar 	int s;
   5479  1.159  knakahar 
   5480  1.177     ozaki 	IFNET_LOCK(ifp);
   5481  1.176     ozaki 
   5482   1.31    martin 	/*
   5483   1.31    martin 	 * Pick the first AF_INET address from the list,
   5484   1.31    martin 	 * aliases don't make any sense on a p2p link anyway.
   5485   1.31    martin 	 */
   5486  1.158  knakahar 	si = dest = NULL;
   5487  1.159  knakahar 	s = pserialize_read_enter();
   5488  1.146     ozaki 	IFADDR_READER_FOREACH(ifa, ifp) {
   5489   1.73   mycroft 		if (ifa->ifa_addr->sa_family == AF_INET) {
   5490   1.31    martin 			si = (struct sockaddr_in *)ifa->ifa_addr;
   5491   1.31    martin 			dest = (struct sockaddr_in *)ifa->ifa_dstaddr;
   5492  1.158  knakahar 			break;
   5493   1.31    martin 		}
   5494   1.31    martin 	}
   5495  1.159  knakahar 	pserialize_read_exit(s);
   5496   1.31    martin 
   5497  1.158  knakahar 	if (si != NULL) {
   5498   1.31    martin 		struct sockaddr_in new_sin = *si;
   5499  1.154       roy 		struct sockaddr_in new_dst = *dest;
   5500  1.153       roy 		int error;
   5501   1.31    martin 
   5502   1.31    martin 		if (sp->ipcp.flags & IPCP_MYADDR_DYN)
   5503   1.31    martin 			new_sin.sin_addr.s_addr = 0;
   5504   1.31    martin 		if (sp->ipcp.flags & IPCP_HISADDR_DYN)
   5505  1.154       roy 			new_dst.sin_addr.s_addr = sp->ipcp.saved_hisaddr;
   5506  1.145     ozaki 
   5507  1.162  knakahar 		in_addrhash_remove(ifatoia(ifa));
   5508  1.145     ozaki 
   5509  1.154       roy 		error = in_ifinit(ifp, ifatoia(ifa), &new_sin, &new_dst, 0);
   5510  1.145     ozaki 
   5511  1.162  knakahar 		in_addrhash_insert(ifatoia(ifa));
   5512  1.145     ozaki 
   5513  1.153       roy 		if (debug && error)
   5514  1.153       roy 		{
   5515  1.153       roy 			log(LOG_DEBUG, "%s: %s: in_ifinit failed, error=%d\n",
   5516  1.153       roy 			    ifp->if_xname, __func__, error);
   5517  1.153       roy 		}
   5518  1.153       roy 		if (!error) {
   5519  1.165  christos 			pfil_run_addrhooks(if_pfil, SIOCAIFADDR, ifa);
   5520  1.153       roy 		}
   5521    1.3  explorer 	}
   5522  1.159  knakahar 
   5523  1.159  knakahar 	if (sp->pp_saved_mtu > 0) {
   5524  1.159  knakahar 		ifp->if_mtu = sp->pp_saved_mtu;
   5525  1.159  knakahar 		sp->pp_saved_mtu = 0;
   5526  1.159  knakahar 		if (debug)
   5527  1.159  knakahar 			log(LOG_DEBUG,
   5528  1.159  knakahar 			    "%s: resetting MTU to %" PRIu64 " bytes\n",
   5529  1.159  knakahar 			    ifp->if_xname, ifp->if_mtu);
   5530  1.159  knakahar 	}
   5531  1.176     ozaki 
   5532  1.177     ozaki 	IFNET_UNLOCK(ifp);
   5533  1.159  knakahar }
   5534  1.159  knakahar 
   5535  1.159  knakahar static void
   5536  1.159  knakahar sppp_clear_ip_addrs(struct sppp *sp)
   5537  1.159  knakahar {
   5538  1.161  knakahar 	struct ifnet *ifp = &sp->pp_if;
   5539  1.159  knakahar 
   5540  1.161  knakahar 	if (!pcq_put(sp->ipcp.update_addrs_q, (void *)IPCP_CLEAR_ADDRS)) {
   5541  1.161  knakahar 		log(LOG_WARNING, "%s: cannot enqueued, ignore sppp_clear_ip_addrs\n",
   5542  1.161  knakahar 		    ifp->if_xname);
   5543  1.161  knakahar 		return;
   5544  1.161  knakahar 	}
   5545  1.161  knakahar 
   5546  1.161  knakahar 	if (atomic_swap_uint(&sp->ipcp.update_addrs_enqueued, 1) == 1)
   5547  1.159  knakahar 		return;
   5548  1.159  knakahar 
   5549  1.161  knakahar 	workqueue_enqueue(sp->ipcp.update_addrs_wq, &sp->ipcp.update_addrs_wk, NULL);
   5550  1.161  knakahar }
   5551  1.161  knakahar 
   5552  1.161  knakahar static void
   5553  1.161  knakahar sppp_update_ip_addrs_work(struct work *wk, void *arg)
   5554  1.161  knakahar {
   5555  1.161  knakahar 	struct sppp *sp = arg;
   5556  1.161  knakahar 	void *work;
   5557  1.161  knakahar 
   5558  1.161  knakahar 	atomic_swap_uint(&sp->ipcp.update_addrs_enqueued, 0);
   5559  1.161  knakahar 
   5560  1.161  knakahar 	while ((work = pcq_get(sp->ipcp.update_addrs_q)) != NULL) {
   5561  1.161  knakahar 		int update = (intptr_t)work;
   5562  1.161  knakahar 
   5563  1.161  knakahar 		if (update == IPCP_SET_ADDRS)
   5564  1.161  knakahar 			sppp_set_ip_addrs_work(wk, sp);
   5565  1.161  knakahar 		else if (update == IPCP_CLEAR_ADDRS)
   5566  1.161  knakahar 			sppp_clear_ip_addrs_work(wk, sp);
   5567  1.161  knakahar 	}
   5568   1.82     perry }
   5569   1.79  christos #endif
   5570    1.3  explorer 
   5571    1.9    itojun #ifdef INET6
   5572    1.9    itojun /*
   5573    1.9    itojun  * Get both IPv6 addresses.
   5574    1.9    itojun  */
   5575    1.9    itojun static void
   5576    1.9    itojun sppp_get_ip6_addrs(struct sppp *sp, struct in6_addr *src, struct in6_addr *dst,
   5577    1.9    itojun 		   struct in6_addr *srcmask)
   5578    1.9    itojun {
   5579    1.9    itojun 	struct ifnet *ifp = &sp->pp_if;
   5580    1.9    itojun 	struct ifaddr *ifa;
   5581    1.9    itojun 	struct sockaddr_in6 *si, *sm;
   5582    1.9    itojun 	struct in6_addr ssrc, ddst;
   5583  1.173     ozaki 	int s;
   5584  1.173     ozaki 	struct psref psref;
   5585    1.9    itojun 
   5586    1.9    itojun 	sm = NULL;
   5587   1.25   thorpej 	memset(&ssrc, 0, sizeof(ssrc));
   5588   1.25   thorpej 	memset(&ddst, 0, sizeof(ddst));
   5589    1.9    itojun 	/*
   5590    1.9    itojun 	 * Pick the first link-local AF_INET6 address from the list,
   5591    1.9    itojun 	 * aliases don't make any sense on a p2p link anyway.
   5592    1.9    itojun 	 */
   5593   1.28      matt 	si = 0;
   5594  1.173     ozaki 	s = pserialize_read_enter();
   5595  1.173     ozaki 	IFADDR_READER_FOREACH(ifa, ifp) {
   5596    1.9    itojun 		if (ifa->ifa_addr->sa_family == AF_INET6) {
   5597    1.9    itojun 			si = (struct sockaddr_in6 *)ifa->ifa_addr;
   5598    1.9    itojun 			sm = (struct sockaddr_in6 *)ifa->ifa_netmask;
   5599  1.173     ozaki 			if (si && IN6_IS_ADDR_LINKLOCAL(&si->sin6_addr)) {
   5600  1.173     ozaki 				ifa_acquire(ifa, &psref);
   5601    1.9    itojun 				break;
   5602  1.173     ozaki 			}
   5603    1.9    itojun 		}
   5604  1.173     ozaki 	}
   5605  1.173     ozaki 	pserialize_read_exit(s);
   5606  1.173     ozaki 
   5607    1.9    itojun 	if (ifa) {
   5608    1.9    itojun 		if (si && !IN6_IS_ADDR_UNSPECIFIED(&si->sin6_addr)) {
   5609  1.118   tsutsui 			memcpy(&ssrc, &si->sin6_addr, sizeof(ssrc));
   5610    1.9    itojun 			if (srcmask) {
   5611  1.118   tsutsui 				memcpy(srcmask, &sm->sin6_addr,
   5612    1.9    itojun 				    sizeof(*srcmask));
   5613    1.9    itojun 			}
   5614    1.9    itojun 		}
   5615    1.9    itojun 
   5616    1.9    itojun 		si = (struct sockaddr_in6 *)ifa->ifa_dstaddr;
   5617    1.9    itojun 		if (si && !IN6_IS_ADDR_UNSPECIFIED(&si->sin6_addr))
   5618  1.118   tsutsui 			memcpy(&ddst, &si->sin6_addr, sizeof(ddst));
   5619  1.173     ozaki 		ifa_release(ifa, &psref);
   5620    1.9    itojun 	}
   5621    1.9    itojun 
   5622    1.9    itojun 	if (dst)
   5623  1.118   tsutsui 		memcpy(dst, &ddst, sizeof(*dst));
   5624    1.9    itojun 	if (src)
   5625  1.118   tsutsui 		memcpy(src, &ssrc, sizeof(*src));
   5626    1.9    itojun }
   5627    1.9    itojun 
   5628    1.9    itojun #ifdef IPV6CP_MYIFID_DYN
   5629    1.9    itojun /*
   5630    1.9    itojun  * Generate random ifid.
   5631    1.9    itojun  */
   5632    1.9    itojun static void
   5633    1.9    itojun sppp_gen_ip6_addr(struct sppp *sp, struct in6_addr *addr)
   5634    1.9    itojun {
   5635    1.9    itojun 	/* TBD */
   5636    1.9    itojun }
   5637    1.9    itojun 
   5638    1.9    itojun /*
   5639   1.23   thorpej  * Set my IPv6 address.  Must be called at splnet.
   5640    1.9    itojun  */
   5641    1.9    itojun static void
   5642    1.9    itojun sppp_set_ip6_addr(struct sppp *sp, const struct in6_addr *src)
   5643    1.9    itojun {
   5644    1.9    itojun 	STDDCL;
   5645    1.9    itojun 	struct ifaddr *ifa;
   5646    1.9    itojun 	struct sockaddr_in6 *sin6;
   5647  1.174     ozaki 	int s;
   5648  1.174     ozaki 	struct psref psref;
   5649    1.9    itojun 
   5650  1.177     ozaki 	IFNET_LOCK(ifp);
   5651  1.176     ozaki 
   5652    1.9    itojun 	/*
   5653    1.9    itojun 	 * Pick the first link-local AF_INET6 address from the list,
   5654    1.9    itojun 	 * aliases don't make any sense on a p2p link anyway.
   5655    1.9    itojun 	 */
   5656    1.9    itojun 
   5657    1.9    itojun 	sin6 = NULL;
   5658  1.174     ozaki 	s = pserialize_read_enter();
   5659  1.146     ozaki 	IFADDR_READER_FOREACH(ifa, ifp)
   5660    1.9    itojun 	{
   5661    1.9    itojun 		if (ifa->ifa_addr->sa_family == AF_INET6)
   5662    1.9    itojun 		{
   5663    1.9    itojun 			sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
   5664  1.174     ozaki 			if (sin6 && IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
   5665  1.174     ozaki 				ifa_acquire(ifa, &psref);
   5666    1.9    itojun 				break;
   5667  1.174     ozaki 			}
   5668    1.9    itojun 		}
   5669    1.9    itojun 	}
   5670  1.174     ozaki 	pserialize_read_exit(s);
   5671    1.9    itojun 
   5672    1.9    itojun 	if (ifa && sin6)
   5673    1.9    itojun 	{
   5674    1.9    itojun 		int error;
   5675    1.9    itojun 		struct sockaddr_in6 new_sin6 = *sin6;
   5676    1.9    itojun 
   5677  1.118   tsutsui 		memcpy(&new_sin6.sin6_addr, src, sizeof(new_sin6.sin6_addr));
   5678    1.9    itojun 		error = in6_ifinit(ifp, ifatoia6(ifa), &new_sin6, 1);
   5679    1.9    itojun 		if (debug && error)
   5680    1.9    itojun 		{
   5681  1.153       roy 			log(LOG_DEBUG, "%s: %s: in6_ifinit failed, error=%d\n",
   5682  1.153       roy 			    ifp->if_xname, __func__, error);
   5683    1.9    itojun 		}
   5684  1.127     rmind 		if (!error) {
   5685  1.164  christos 			pfil_run_addrhooks(if_pfil, SIOCAIFADDR_IN6, ifa);
   5686  1.127     rmind 		}
   5687  1.174     ozaki 		ifa_release(ifa, &psref);
   5688    1.9    itojun 	}
   5689  1.176     ozaki 
   5690  1.177     ozaki 	IFNET_UNLOCK(ifp);
   5691    1.9    itojun }
   5692    1.9    itojun #endif
   5693    1.9    itojun 
   5694    1.9    itojun /*
   5695    1.9    itojun  * Suggest a candidate address to be used by peer.
   5696    1.9    itojun  */
   5697    1.9    itojun static void
   5698    1.9    itojun sppp_suggest_ip6_addr(struct sppp *sp, struct in6_addr *suggest)
   5699    1.9    itojun {
   5700    1.9    itojun 	struct in6_addr myaddr;
   5701   1.10    itojun 	struct timeval tv;
   5702    1.9    itojun 
   5703    1.9    itojun 	sppp_get_ip6_addrs(sp, &myaddr, 0, 0);
   5704    1.9    itojun 
   5705    1.9    itojun 	myaddr.s6_addr[8] &= ~0x02;	/* u bit to "local" */
   5706   1.10    itojun 	microtime(&tv);
   5707   1.10    itojun 	if ((tv.tv_usec & 0xff) == 0 && (tv.tv_sec & 0xff) == 0) {
   5708   1.10    itojun 		myaddr.s6_addr[14] ^= 0xff;
   5709   1.10    itojun 		myaddr.s6_addr[15] ^= 0xff;
   5710   1.10    itojun 	} else {
   5711   1.10    itojun 		myaddr.s6_addr[14] ^= (tv.tv_usec & 0xff);
   5712   1.10    itojun 		myaddr.s6_addr[15] ^= (tv.tv_sec & 0xff);
   5713   1.10    itojun 	}
   5714    1.9    itojun 	if (suggest)
   5715  1.118   tsutsui 		memcpy(suggest, &myaddr, sizeof(myaddr));
   5716    1.9    itojun }
   5717    1.9    itojun #endif /*INET6*/
   5718    1.9    itojun 
   5719   1.36    martin /*
   5720   1.36    martin  * Process ioctl requests specific to the PPP interface.
   5721   1.36    martin  * Permissions have already been checked.
   5722   1.36    martin  */
   5723    1.3  explorer static int
   5724  1.110  gmcgarry sppp_params(struct sppp *sp, u_long cmd, void *data)
   5725    1.3  explorer {
   5726   1.36    martin 	switch (cmd) {
   5727   1.36    martin 	case SPPPGETAUTHCFG:
   5728   1.36    martin 	    {
   5729   1.42  jdolecek 		struct spppauthcfg *cfg = (struct spppauthcfg *)data;
   5730   1.42  jdolecek 		int error;
   5731   1.42  jdolecek 		size_t len;
   5732   1.42  jdolecek 
   5733  1.170  knakahar 		SPPP_LOCK(sp, RW_READER);
   5734  1.163  knakahar 
   5735   1.36    martin 		cfg->myauthflags = sp->myauth.flags;
   5736   1.36    martin 		cfg->hisauthflags = sp->hisauth.flags;
   5737  1.138  christos 		strlcpy(cfg->ifname, sp->pp_if.if_xname, sizeof(cfg->ifname));
   5738   1.36    martin 		cfg->hisauth = 0;
   5739   1.36    martin 		if (sp->hisauth.proto)
   5740   1.42  jdolecek 		    cfg->hisauth = (sp->hisauth.proto == PPP_PAP) ? SPPP_AUTHPROTO_PAP : SPPP_AUTHPROTO_CHAP;
   5741   1.36    martin 		cfg->myauth = 0;
   5742   1.36    martin 		if (sp->myauth.proto)
   5743   1.42  jdolecek 		    cfg->myauth = (sp->myauth.proto == PPP_PAP) ? SPPP_AUTHPROTO_PAP : SPPP_AUTHPROTO_CHAP;
   5744   1.36    martin 		if (cfg->myname_length == 0) {
   5745   1.36    martin 		    if (sp->myauth.name != NULL)
   5746   1.54  christos 			cfg->myname_length = sp->myauth.name_len + 1;
   5747   1.36    martin 		} else {
   5748   1.41    martin 		    if (sp->myauth.name == NULL) {
   5749   1.41    martin 			cfg->myname_length = 0;
   5750   1.41    martin 		    } else {
   5751   1.54  christos 			len = sp->myauth.name_len + 1;
   5752  1.163  knakahar 
   5753  1.163  knakahar 
   5754  1.170  knakahar 			if (cfg->myname_length < len) {
   5755  1.170  knakahar 				SPPP_UNLOCK(sp);
   5756  1.170  knakahar 				return (ENAMETOOLONG);
   5757  1.170  knakahar 			}
   5758   1.42  jdolecek 			error = copyout(sp->myauth.name, cfg->myname, len);
   5759  1.170  knakahar 			if (error) {
   5760  1.170  knakahar 				SPPP_UNLOCK(sp);
   5761  1.170  knakahar 				return error;
   5762  1.170  knakahar 			}
   5763   1.41    martin 		    }
   5764   1.36    martin 		}
   5765   1.36    martin 		if (cfg->hisname_length == 0) {
   5766   1.70    itojun 		    if (sp->hisauth.name != NULL)
   5767   1.54  christos 			cfg->hisname_length = sp->hisauth.name_len + 1;
   5768   1.36    martin 		} else {
   5769   1.41    martin 		    if (sp->hisauth.name == NULL) {
   5770   1.41    martin 		    	cfg->hisname_length = 0;
   5771   1.41    martin 		    } else {
   5772   1.54  christos 			len = sp->hisauth.name_len + 1;
   5773  1.163  knakahar 
   5774  1.170  knakahar 			if (cfg->hisname_length < len) {
   5775  1.170  knakahar 				SPPP_UNLOCK(sp);
   5776  1.170  knakahar 				return (ENAMETOOLONG);
   5777  1.170  knakahar 			}
   5778   1.42  jdolecek 			error = copyout(sp->hisauth.name, cfg->hisname, len);
   5779  1.170  knakahar 			if (error) {
   5780  1.170  knakahar 				SPPP_UNLOCK(sp);
   5781  1.170  knakahar 				return error;
   5782  1.170  knakahar 			}
   5783   1.41    martin 		    }
   5784    1.3  explorer 		}
   5785  1.170  knakahar 		SPPP_UNLOCK(sp);
   5786   1.36    martin 	    }
   5787   1.36    martin 	    break;
   5788   1.36    martin 	case SPPPSETAUTHCFG:
   5789   1.36    martin 	    {
   5790   1.59    itojun 		struct spppauthcfg *cfg = (struct spppauthcfg *)data;
   5791   1.42  jdolecek 		int error;
   5792    1.3  explorer 
   5793  1.170  knakahar 		SPPP_LOCK(sp, RW_WRITER);
   5794  1.163  knakahar 
   5795   1.42  jdolecek 		if (sp->myauth.name) {
   5796   1.42  jdolecek 			free(sp->myauth.name, M_DEVBUF);
   5797   1.42  jdolecek 			sp->myauth.name = NULL;
   5798   1.42  jdolecek 		}
   5799   1.42  jdolecek 		if (sp->myauth.secret) {
   5800   1.42  jdolecek 			free(sp->myauth.secret, M_DEVBUF);
   5801   1.42  jdolecek 			sp->myauth.secret = NULL;
   5802   1.42  jdolecek 		}
   5803   1.42  jdolecek 		if (sp->hisauth.name) {
   5804   1.42  jdolecek 			free(sp->hisauth.name, M_DEVBUF);
   5805   1.42  jdolecek 			sp->hisauth.name = NULL;
   5806   1.42  jdolecek 		}
   5807   1.42  jdolecek 		if (sp->hisauth.secret) {
   5808   1.42  jdolecek 			free(sp->hisauth.secret, M_DEVBUF);
   5809   1.42  jdolecek 			sp->hisauth.secret = NULL;
   5810   1.42  jdolecek 		}
   5811   1.36    martin 
   5812   1.41    martin 		if (cfg->hisname != NULL && cfg->hisname_length > 0) {
   5813  1.163  knakahar 			if (cfg->hisname_length >= MCLBYTES) {
   5814  1.170  knakahar 				SPPP_UNLOCK(sp);
   5815  1.163  knakahar 				return (ENAMETOOLONG);
   5816  1.163  knakahar 			}
   5817  1.163  knakahar 			sp->hisauth.name = malloc(cfg->hisname_length, M_DEVBUF, M_WAITOK);
   5818  1.163  knakahar 			error = copyin(cfg->hisname, sp->hisauth.name, cfg->hisname_length);
   5819  1.163  knakahar 			if (error) {
   5820  1.163  knakahar 				free(sp->hisauth.name, M_DEVBUF);
   5821  1.163  knakahar 				sp->hisauth.name = NULL;
   5822  1.170  knakahar 				SPPP_UNLOCK(sp);
   5823  1.163  knakahar 				return error;
   5824  1.163  knakahar 			}
   5825  1.163  knakahar 			sp->hisauth.name_len = cfg->hisname_length - 1;
   5826  1.163  knakahar 			sp->hisauth.name[sp->hisauth.name_len] = 0;
   5827   1.36    martin 		}
   5828   1.41    martin 		if (cfg->hissecret != NULL && cfg->hissecret_length > 0) {
   5829  1.163  knakahar 			if (cfg->hissecret_length >= MCLBYTES) {
   5830  1.170  knakahar 				SPPP_UNLOCK(sp);
   5831  1.163  knakahar 				return (ENAMETOOLONG);
   5832  1.163  knakahar 			}
   5833  1.163  knakahar 			sp->hisauth.secret = malloc(cfg->hissecret_length,
   5834  1.163  knakahar 			    M_DEVBUF, M_WAITOK);
   5835  1.163  knakahar 			error = copyin(cfg->hissecret, sp->hisauth.secret,
   5836  1.163  knakahar 			    cfg->hissecret_length);
   5837  1.163  knakahar 			if (error) {
   5838  1.163  knakahar 				free(sp->hisauth.secret, M_DEVBUF);
   5839  1.163  knakahar 				sp->hisauth.secret = NULL;
   5840  1.170  knakahar 				SPPP_UNLOCK(sp);
   5841  1.163  knakahar 				return error;
   5842  1.163  knakahar 			}
   5843  1.163  knakahar 			sp->hisauth.secret_len = cfg->hissecret_length - 1;
   5844  1.163  knakahar 			sp->hisauth.secret[sp->hisauth.secret_len] = 0;
   5845   1.36    martin 		}
   5846   1.41    martin 		if (cfg->myname != NULL && cfg->myname_length > 0) {
   5847  1.163  knakahar 			if (cfg->myname_length >= MCLBYTES) {
   5848  1.170  knakahar 				SPPP_UNLOCK(sp);
   5849  1.163  knakahar 				return (ENAMETOOLONG);
   5850  1.163  knakahar 			}
   5851  1.163  knakahar 			sp->myauth.name = malloc(cfg->myname_length, M_DEVBUF, M_WAITOK);
   5852  1.163  knakahar 			error = copyin(cfg->myname, sp->myauth.name, cfg->myname_length);
   5853  1.163  knakahar 			if (error) {
   5854  1.163  knakahar 				free(sp->myauth.name, M_DEVBUF);
   5855  1.163  knakahar 				sp->myauth.name = NULL;
   5856  1.170  knakahar 				SPPP_UNLOCK(sp);
   5857  1.163  knakahar 				return error;
   5858  1.163  knakahar 			}
   5859  1.163  knakahar 			sp->myauth.name_len = cfg->myname_length - 1;
   5860  1.163  knakahar 			sp->myauth.name[sp->myauth.name_len] = 0;
   5861   1.36    martin 		}
   5862   1.41    martin 		if (cfg->mysecret != NULL && cfg->mysecret_length > 0) {
   5863  1.163  knakahar 			if (cfg->mysecret_length >= MCLBYTES) {
   5864  1.170  knakahar 				SPPP_UNLOCK(sp);
   5865  1.163  knakahar 				return (ENAMETOOLONG);
   5866  1.163  knakahar 			}
   5867  1.163  knakahar 			sp->myauth.secret = malloc(cfg->mysecret_length,
   5868  1.163  knakahar 			    M_DEVBUF, M_WAITOK);
   5869  1.163  knakahar 			error = copyin(cfg->mysecret, sp->myauth.secret,
   5870  1.163  knakahar 			    cfg->mysecret_length);
   5871  1.163  knakahar 			if (error) {
   5872  1.163  knakahar 				free(sp->myauth.secret, M_DEVBUF);
   5873  1.163  knakahar 				sp->myauth.secret = NULL;
   5874  1.170  knakahar 				SPPP_UNLOCK(sp);
   5875  1.163  knakahar 				return error;
   5876  1.163  knakahar 			}
   5877  1.163  knakahar 			sp->myauth.secret_len = cfg->mysecret_length - 1;
   5878  1.163  knakahar 			sp->myauth.secret[sp->myauth.secret_len] = 0;
   5879   1.36    martin 		}
   5880   1.36    martin 		sp->myauth.flags = cfg->myauthflags;
   5881   1.36    martin 		if (cfg->myauth)
   5882   1.42  jdolecek 		    sp->myauth.proto = (cfg->myauth == SPPP_AUTHPROTO_PAP) ? PPP_PAP : PPP_CHAP;
   5883   1.36    martin 		sp->hisauth.flags = cfg->hisauthflags;
   5884   1.36    martin 		if (cfg->hisauth)
   5885   1.42  jdolecek 		    sp->hisauth.proto = (cfg->hisauth == SPPP_AUTHPROTO_PAP) ? PPP_PAP : PPP_CHAP;
   5886   1.39    martin 		sp->pp_auth_failures = 0;
   5887   1.82     perry 		if (sp->hisauth.proto != 0)
   5888   1.55  christos 		    sp->lcp.opts |= (1 << LCP_OPT_AUTH_PROTO);
   5889   1.55  christos 		else
   5890   1.55  christos 		    sp->lcp.opts &= ~(1 << LCP_OPT_AUTH_PROTO);
   5891  1.163  knakahar 
   5892  1.170  knakahar 		SPPP_UNLOCK(sp);
   5893   1.36    martin 	    }
   5894   1.36    martin 	    break;
   5895   1.36    martin 	case SPPPGETLCPCFG:
   5896   1.36    martin 	    {
   5897  1.163  knakahar 		struct sppplcpcfg *lcpp = (struct sppplcpcfg *)data;
   5898  1.170  knakahar 
   5899  1.170  knakahar 		SPPP_LOCK(sp, RW_READER);
   5900  1.163  knakahar 		lcpp->lcp_timeout = sp->lcp.timeout;
   5901  1.170  knakahar 		SPPP_UNLOCK(sp);
   5902   1.36    martin 	    }
   5903   1.36    martin 	    break;
   5904   1.36    martin 	case SPPPSETLCPCFG:
   5905   1.36    martin 	    {
   5906  1.163  knakahar 		struct sppplcpcfg *lcpp = (struct sppplcpcfg *)data;
   5907  1.170  knakahar 
   5908  1.170  knakahar 		SPPP_LOCK(sp, RW_WRITER);
   5909  1.163  knakahar 		sp->lcp.timeout = lcpp->lcp_timeout;
   5910  1.170  knakahar 		SPPP_UNLOCK(sp);
   5911   1.36    martin 	    }
   5912   1.36    martin 	    break;
   5913   1.36    martin 	case SPPPGETSTATUS:
   5914   1.36    martin 	    {
   5915   1.42  jdolecek 		struct spppstatus *status = (struct spppstatus *)data;
   5916  1.170  knakahar 
   5917  1.170  knakahar 		SPPP_LOCK(sp, RW_READER);
   5918   1.36    martin 		status->phase = sp->pp_phase;
   5919  1.170  knakahar 		SPPP_UNLOCK(sp);
   5920   1.38    martin 	    }
   5921   1.38    martin 	    break;
   5922   1.75    martin 	case SPPPGETSTATUSNCP:
   5923   1.75    martin 	    {
   5924   1.75    martin 		struct spppstatusncp *status = (struct spppstatusncp *)data;
   5925  1.170  knakahar 
   5926  1.170  knakahar 		SPPP_LOCK(sp, RW_READER);
   5927   1.75    martin 		status->phase = sp->pp_phase;
   5928  1.202  yamaguch 		status->ncpup = sppp_cp_check(sp, CP_NCP);
   5929  1.170  knakahar 		SPPP_UNLOCK(sp);
   5930   1.75    martin 	    }
   5931   1.75    martin 	    break;
   5932   1.38    martin 	case SPPPGETIDLETO:
   5933   1.38    martin 	    {
   5934   1.42  jdolecek 	    	struct spppidletimeout *to = (struct spppidletimeout *)data;
   5935  1.170  knakahar 
   5936  1.170  knakahar 		SPPP_LOCK(sp, RW_READER);
   5937   1.38    martin 		to->idle_seconds = sp->pp_idle_timeout;
   5938  1.170  knakahar 		SPPP_UNLOCK(sp);
   5939   1.38    martin 	    }
   5940   1.38    martin 	    break;
   5941   1.38    martin 	case SPPPSETIDLETO:
   5942   1.38    martin 	    {
   5943   1.42  jdolecek 	    	struct spppidletimeout *to = (struct spppidletimeout *)data;
   5944  1.170  knakahar 
   5945  1.170  knakahar 		SPPP_LOCK(sp, RW_WRITER);
   5946  1.163  knakahar 		sp->pp_idle_timeout = to->idle_seconds;
   5947  1.170  knakahar 		SPPP_UNLOCK(sp);
   5948   1.39    martin 	    }
   5949   1.39    martin 	    break;
   5950   1.39    martin 	case SPPPSETAUTHFAILURE:
   5951   1.39    martin 	    {
   5952  1.170  knakahar 	    	struct spppauthfailuresettings *afsettings =
   5953  1.170  knakahar 		    (struct spppauthfailuresettings *)data;
   5954  1.170  knakahar 
   5955  1.170  knakahar 		SPPP_LOCK(sp, RW_WRITER);
   5956  1.163  knakahar 		sp->pp_max_auth_fail = afsettings->max_failures;
   5957  1.163  knakahar 		sp->pp_auth_failures = 0;
   5958  1.170  knakahar 		SPPP_UNLOCK(sp);
   5959   1.39    martin 	    }
   5960   1.39    martin 	    break;
   5961   1.39    martin 	case SPPPGETAUTHFAILURES:
   5962   1.39    martin 	    {
   5963   1.42  jdolecek 	    	struct spppauthfailurestats *stats = (struct spppauthfailurestats *)data;
   5964  1.170  knakahar 
   5965  1.170  knakahar 		SPPP_LOCK(sp, RW_READER);
   5966  1.163  knakahar 		stats->auth_failures = sp->pp_auth_failures;
   5967  1.163  knakahar 		stats->max_failures = sp->pp_max_auth_fail;
   5968  1.170  knakahar 		SPPP_UNLOCK(sp);
   5969   1.45    martin 	    }
   5970   1.45    martin 	    break;
   5971   1.45    martin 	case SPPPSETDNSOPTS:
   5972   1.45    martin 	    {
   5973   1.59    itojun 		struct spppdnssettings *req = (struct spppdnssettings *)data;
   5974  1.170  knakahar 
   5975  1.170  knakahar 		SPPP_LOCK(sp, RW_WRITER);
   5976   1.45    martin 		sp->query_dns = req->query_dns & 3;
   5977  1.170  knakahar 		SPPP_UNLOCK(sp);
   5978   1.45    martin 	    }
   5979   1.45    martin 	    break;
   5980   1.45    martin 	case SPPPGETDNSOPTS:
   5981   1.45    martin 	    {
   5982   1.59    itojun 		struct spppdnssettings *req = (struct spppdnssettings *)data;
   5983  1.170  knakahar 
   5984  1.170  knakahar 		SPPP_LOCK(sp, RW_READER);
   5985   1.45    martin 		req->query_dns = sp->query_dns;
   5986  1.170  knakahar 		SPPP_UNLOCK(sp);
   5987   1.45    martin 	    }
   5988   1.45    martin 	    break;
   5989   1.45    martin 	case SPPPGETDNSADDRS:
   5990   1.45    martin 	    {
   5991  1.163  knakahar 		struct spppdnsaddrs *addrs = (struct spppdnsaddrs *)data;
   5992  1.170  knakahar 
   5993  1.170  knakahar 		SPPP_LOCK(sp, RW_READER);
   5994  1.163  knakahar 		memcpy(&addrs->dns, &sp->dns_addrs, sizeof addrs->dns);
   5995  1.170  knakahar 		SPPP_UNLOCK(sp);
   5996   1.68    martin 	    }
   5997   1.68    martin 	    break;
   5998   1.68    martin 	case SPPPGETKEEPALIVE:
   5999   1.68    martin 	    {
   6000   1.68    martin 	    	struct spppkeepalivesettings *settings =
   6001   1.68    martin 		     (struct spppkeepalivesettings*)data;
   6002  1.170  knakahar 
   6003  1.170  knakahar 		SPPP_LOCK(sp, RW_READER);
   6004   1.68    martin 		settings->maxalive = sp->pp_maxalive;
   6005   1.68    martin 		settings->max_noreceive = sp->pp_max_noreceive;
   6006  1.170  knakahar 		SPPP_UNLOCK(sp);
   6007   1.68    martin 	    }
   6008   1.68    martin 	    break;
   6009   1.68    martin 	case SPPPSETKEEPALIVE:
   6010   1.68    martin 	    {
   6011   1.68    martin 	    	struct spppkeepalivesettings *settings =
   6012   1.68    martin 		     (struct spppkeepalivesettings*)data;
   6013  1.170  knakahar 
   6014  1.170  knakahar 		SPPP_LOCK(sp, RW_WRITER);
   6015   1.68    martin 		sp->pp_maxalive = settings->maxalive;
   6016   1.68    martin 		sp->pp_max_noreceive = settings->max_noreceive;
   6017  1.170  knakahar 		SPPP_UNLOCK(sp);
   6018   1.36    martin 	    }
   6019   1.36    martin 	    break;
   6020  1.181  pgoyette 	default:
   6021  1.120  jmcneill 	    {
   6022  1.181  pgoyette 		int ret;
   6023  1.170  knakahar 
   6024  1.182  pgoyette 		MODULE_HOOK_CALL(sppp_params_50_hook, (sp, cmd, data),
   6025  1.181  pgoyette 		    enosys(), ret);
   6026  1.181  pgoyette 		if (ret != ENOSYS)
   6027  1.181  pgoyette 			return ret;
   6028  1.181  pgoyette 		return (EINVAL);
   6029  1.120  jmcneill 	    }
   6030    1.3  explorer 	}
   6031   1.42  jdolecek 	return (0);
   6032    1.3  explorer }
   6033    1.3  explorer 
   6034    1.3  explorer static void
   6035    1.3  explorer sppp_phase_network(struct sppp *sp)
   6036    1.3  explorer {
   6037    1.3  explorer 	int i;
   6038    1.3  explorer 
   6039  1.170  knakahar 	KASSERT(SPPP_WLOCKED(sp));
   6040  1.163  knakahar 
   6041  1.149     joerg 	sppp_change_phase(sp, SPPP_PHASE_NETWORK);
   6042    1.3  explorer 
   6043    1.3  explorer 	/* Notify NCPs now. */
   6044    1.3  explorer 	for (i = 0; i < IDX_COUNT; i++)
   6045    1.3  explorer 		if ((cps[i])->flags & CP_NCP)
   6046  1.201  yamaguch 			sppp_wq_add(sp->wq_cp, &sp->scp[i].work_open);
   6047    1.3  explorer }
   6048   1.82     perry 
   6049    1.3  explorer static const char *
   6050    1.3  explorer sppp_cp_type_name(u_char type)
   6051    1.1  explorer {
   6052    1.3  explorer 	static char buf[12];
   6053    1.1  explorer 	switch (type) {
   6054    1.3  explorer 	case CONF_REQ:   return "conf-req";
   6055    1.3  explorer 	case CONF_ACK:   return "conf-ack";
   6056    1.3  explorer 	case CONF_NAK:   return "conf-nak";
   6057    1.3  explorer 	case CONF_REJ:   return "conf-rej";
   6058    1.3  explorer 	case TERM_REQ:   return "term-req";
   6059    1.3  explorer 	case TERM_ACK:   return "term-ack";
   6060    1.3  explorer 	case CODE_REJ:   return "code-rej";
   6061    1.3  explorer 	case PROTO_REJ:  return "proto-rej";
   6062    1.3  explorer 	case ECHO_REQ:   return "echo-req";
   6063    1.3  explorer 	case ECHO_REPLY: return "echo-reply";
   6064    1.3  explorer 	case DISC_REQ:   return "discard-req";
   6065    1.3  explorer 	}
   6066   1.77    itojun 	snprintf(buf, sizeof(buf), "0x%x", type);
   6067    1.3  explorer 	return buf;
   6068    1.3  explorer }
   6069    1.3  explorer 
   6070    1.3  explorer static const char *
   6071    1.3  explorer sppp_auth_type_name(u_short proto, u_char type)
   6072    1.3  explorer {
   6073  1.139  christos 	static char buf[32];
   6074  1.139  christos 	const char *name;
   6075  1.139  christos 
   6076    1.3  explorer 	switch (proto) {
   6077    1.3  explorer 	case PPP_CHAP:
   6078    1.3  explorer 		switch (type) {
   6079    1.3  explorer 		case CHAP_CHALLENGE:	return "challenge";
   6080    1.3  explorer 		case CHAP_RESPONSE:	return "response";
   6081    1.3  explorer 		case CHAP_SUCCESS:	return "success";
   6082    1.3  explorer 		case CHAP_FAILURE:	return "failure";
   6083  1.139  christos 		default:		name = "chap"; break;
   6084    1.3  explorer 		}
   6085  1.137    martin 		break;
   6086  1.139  christos 
   6087    1.3  explorer 	case PPP_PAP:
   6088    1.3  explorer 		switch (type) {
   6089    1.3  explorer 		case PAP_REQ:		return "req";
   6090    1.3  explorer 		case PAP_ACK:		return "ack";
   6091    1.3  explorer 		case PAP_NAK:		return "nak";
   6092  1.139  christos 		default:		name = "pap";	break;
   6093    1.3  explorer 		}
   6094  1.137    martin 		break;
   6095  1.139  christos 
   6096  1.139  christos 	default:
   6097  1.139  christos 		name = "bad";
   6098  1.139  christos 		break;
   6099    1.3  explorer 	}
   6100  1.139  christos 
   6101  1.139  christos 	snprintf(buf, sizeof(buf), "%s(%#x) %#x", name, proto, type);
   6102    1.3  explorer 	return buf;
   6103    1.3  explorer }
   6104    1.3  explorer 
   6105    1.3  explorer static const char *
   6106    1.3  explorer sppp_lcp_opt_name(u_char opt)
   6107    1.3  explorer {
   6108    1.3  explorer 	static char buf[12];
   6109    1.3  explorer 	switch (opt) {
   6110    1.3  explorer 	case LCP_OPT_MRU:		return "mru";
   6111    1.3  explorer 	case LCP_OPT_ASYNC_MAP:		return "async-map";
   6112    1.3  explorer 	case LCP_OPT_AUTH_PROTO:	return "auth-proto";
   6113    1.3  explorer 	case LCP_OPT_QUAL_PROTO:	return "qual-proto";
   6114    1.3  explorer 	case LCP_OPT_MAGIC:		return "magic";
   6115    1.3  explorer 	case LCP_OPT_PROTO_COMP:	return "proto-comp";
   6116    1.3  explorer 	case LCP_OPT_ADDR_COMP:		return "addr-comp";
   6117  1.188        is 	case LCP_OPT_SELF_DESC_PAD:	return "sdpad";
   6118  1.188        is 	case LCP_OPT_CALL_BACK:		return "callback";
   6119  1.188        is 	case LCP_OPT_COMPOUND_FRMS:	return "cmpd-frms";
   6120  1.188        is 	case LCP_OPT_MP_MRRU:		return "mrru";
   6121  1.188        is 	case LCP_OPT_MP_SSNHF:		return "mp-ssnhf";
   6122  1.188        is 	case LCP_OPT_MP_EID:		return "mp-eid";
   6123    1.3  explorer 	}
   6124   1.77    itojun 	snprintf(buf, sizeof(buf), "0x%x", opt);
   6125    1.3  explorer 	return buf;
   6126    1.3  explorer }
   6127    1.3  explorer 
   6128    1.3  explorer static const char *
   6129    1.3  explorer sppp_ipcp_opt_name(u_char opt)
   6130    1.3  explorer {
   6131    1.3  explorer 	static char buf[12];
   6132    1.3  explorer 	switch (opt) {
   6133    1.3  explorer 	case IPCP_OPT_ADDRESSES:	return "addresses";
   6134    1.3  explorer 	case IPCP_OPT_COMPRESSION:	return "compression";
   6135    1.3  explorer 	case IPCP_OPT_ADDRESS:		return "address";
   6136    1.3  explorer 	}
   6137   1.77    itojun 	snprintf(buf, sizeof(buf), "0x%x", opt);
   6138    1.3  explorer 	return buf;
   6139    1.3  explorer }
   6140    1.3  explorer 
   6141    1.9    itojun #ifdef INET6
   6142    1.9    itojun static const char *
   6143    1.9    itojun sppp_ipv6cp_opt_name(u_char opt)
   6144    1.9    itojun {
   6145    1.9    itojun 	static char buf[12];
   6146    1.9    itojun 	switch (opt) {
   6147    1.9    itojun 	case IPV6CP_OPT_IFID:		return "ifid";
   6148    1.9    itojun 	case IPV6CP_OPT_COMPRESSION:	return "compression";
   6149    1.9    itojun 	}
   6150   1.77    itojun 	snprintf(buf, sizeof(buf), "0x%x", opt);
   6151    1.9    itojun 	return buf;
   6152    1.9    itojun }
   6153    1.9    itojun #endif
   6154    1.9    itojun 
   6155    1.3  explorer static const char *
   6156    1.3  explorer sppp_state_name(int state)
   6157    1.3  explorer {
   6158    1.3  explorer 	switch (state) {
   6159    1.3  explorer 	case STATE_INITIAL:	return "initial";
   6160    1.3  explorer 	case STATE_STARTING:	return "starting";
   6161    1.3  explorer 	case STATE_CLOSED:	return "closed";
   6162    1.3  explorer 	case STATE_STOPPED:	return "stopped";
   6163    1.3  explorer 	case STATE_CLOSING:	return "closing";
   6164    1.3  explorer 	case STATE_STOPPING:	return "stopping";
   6165    1.3  explorer 	case STATE_REQ_SENT:	return "req-sent";
   6166    1.3  explorer 	case STATE_ACK_RCVD:	return "ack-rcvd";
   6167    1.3  explorer 	case STATE_ACK_SENT:	return "ack-sent";
   6168    1.3  explorer 	case STATE_OPENED:	return "opened";
   6169    1.3  explorer 	}
   6170    1.3  explorer 	return "illegal";
   6171    1.3  explorer }
   6172    1.3  explorer 
   6173    1.3  explorer static const char *
   6174   1.36    martin sppp_phase_name(int phase)
   6175    1.3  explorer {
   6176    1.3  explorer 	switch (phase) {
   6177   1.36    martin 	case SPPP_PHASE_DEAD:		return "dead";
   6178   1.36    martin 	case SPPP_PHASE_ESTABLISH:	return "establish";
   6179   1.36    martin 	case SPPP_PHASE_TERMINATE:	return "terminate";
   6180   1.36    martin 	case SPPP_PHASE_AUTHENTICATE: 	return "authenticate";
   6181   1.36    martin 	case SPPP_PHASE_NETWORK:	return "network";
   6182    1.3  explorer 	}
   6183    1.3  explorer 	return "illegal";
   6184    1.3  explorer }
   6185    1.3  explorer 
   6186    1.3  explorer static const char *
   6187    1.3  explorer sppp_proto_name(u_short proto)
   6188    1.3  explorer {
   6189    1.3  explorer 	static char buf[12];
   6190    1.3  explorer 	switch (proto) {
   6191    1.3  explorer 	case PPP_LCP:	return "lcp";
   6192    1.3  explorer 	case PPP_IPCP:	return "ipcp";
   6193    1.3  explorer 	case PPP_PAP:	return "pap";
   6194    1.3  explorer 	case PPP_CHAP:	return "chap";
   6195    1.9    itojun 	case PPP_IPV6CP: return "ipv6cp";
   6196    1.1  explorer 	}
   6197   1.77    itojun 	snprintf(buf, sizeof(buf), "0x%x", (unsigned)proto);
   6198    1.3  explorer 	return buf;
   6199    1.1  explorer }
   6200    1.1  explorer 
   6201    1.1  explorer static void
   6202    1.3  explorer sppp_print_bytes(const u_char *p, u_short len)
   6203    1.1  explorer {
   6204    1.3  explorer 	addlog(" %02x", *p++);
   6205    1.1  explorer 	while (--len > 0)
   6206    1.3  explorer 		addlog("-%02x", *p++);
   6207    1.3  explorer }
   6208    1.3  explorer 
   6209    1.3  explorer static void
   6210    1.3  explorer sppp_print_string(const char *p, u_short len)
   6211    1.3  explorer {
   6212    1.3  explorer 	u_char c;
   6213    1.3  explorer 
   6214    1.3  explorer 	while (len-- > 0) {
   6215    1.3  explorer 		c = *p++;
   6216    1.3  explorer 		/*
   6217    1.3  explorer 		 * Print only ASCII chars directly.  RFC 1994 recommends
   6218    1.3  explorer 		 * using only them, but we don't rely on it.  */
   6219    1.3  explorer 		if (c < ' ' || c > '~')
   6220    1.3  explorer 			addlog("\\x%x", c);
   6221    1.3  explorer 		else
   6222    1.3  explorer 			addlog("%c", c);
   6223    1.3  explorer 	}
   6224    1.3  explorer }
   6225    1.3  explorer 
   6226    1.3  explorer static const char *
   6227  1.109      matt sppp_dotted_quad(uint32_t addr)
   6228    1.3  explorer {
   6229    1.3  explorer 	static char s[16];
   6230   1.77    itojun 	snprintf(s, sizeof(s), "%d.%d.%d.%d",
   6231    1.3  explorer 		(int)((addr >> 24) & 0xff),
   6232    1.3  explorer 		(int)((addr >> 16) & 0xff),
   6233    1.3  explorer 		(int)((addr >> 8) & 0xff),
   6234    1.3  explorer 		(int)(addr & 0xff));
   6235    1.3  explorer 	return s;
   6236    1.3  explorer }
   6237    1.3  explorer 
   6238    1.3  explorer /* a dummy, used to drop uninteresting events */
   6239    1.3  explorer static void
   6240  1.101  christos sppp_null(struct sppp *unused)
   6241    1.3  explorer {
   6242    1.3  explorer 	/* do just nothing */
   6243    1.1  explorer }
   6244  1.194  yamaguch 
   6245  1.194  yamaguch static void
   6246  1.202  yamaguch sppp_tls(const struct cp *cp, struct sppp *sp)
   6247  1.202  yamaguch {
   6248  1.202  yamaguch 
   6249  1.202  yamaguch 	/* notify lcp that is lower layer */
   6250  1.202  yamaguch 	sp->lcp.protos |= (1 << cp->protoidx);
   6251  1.202  yamaguch 
   6252  1.202  yamaguch 	if (sp->scp[IDX_LCP].state == STATE_OPENED)
   6253  1.202  yamaguch 		sppp_wq_add(sp->wq_cp, &sp->scp[cp->protoidx].work_up);
   6254  1.202  yamaguch }
   6255  1.202  yamaguch 
   6256  1.202  yamaguch static void
   6257  1.202  yamaguch sppp_tlf(const struct cp *cp, struct sppp *sp)
   6258  1.202  yamaguch {
   6259  1.202  yamaguch 	STDDCL;
   6260  1.202  yamaguch 
   6261  1.202  yamaguch 	if (debug)
   6262  1.202  yamaguch 		log(LOG_DEBUG, "%s: %s tlf\n", ifp->if_xname, cp->name);
   6263  1.202  yamaguch 
   6264  1.202  yamaguch 	/* notify lcp that is lower layer */
   6265  1.202  yamaguch 	sp->lcp.protos &= ~(1 << cp->protoidx);
   6266  1.202  yamaguch 
   6267  1.202  yamaguch 	/* cleanup */
   6268  1.202  yamaguch 	if (sp->scp[cp->protoidx].rcr_buf != NULL) {
   6269  1.202  yamaguch 		kmem_free(sp->scp[cp->protoidx].rcr_buf,
   6270  1.202  yamaguch 		    sp->scp[cp->protoidx].rcr_blen);
   6271  1.202  yamaguch 	}
   6272  1.202  yamaguch 
   6273  1.202  yamaguch 	sp->scp[cp->protoidx].rcr_buf = NULL;
   6274  1.202  yamaguch 	sp->scp[cp->protoidx].rcr_blen = 0;
   6275  1.202  yamaguch 	sp->scp[cp->protoidx].rcr_rlen = 0;
   6276  1.202  yamaguch 
   6277  1.202  yamaguch 	sppp_lcp_check_and_close(sp);
   6278  1.202  yamaguch }
   6279  1.202  yamaguch 
   6280  1.202  yamaguch static void
   6281  1.194  yamaguch sppp_sca_scn(const struct cp *cp, struct sppp *sp)
   6282  1.194  yamaguch {
   6283  1.194  yamaguch 	STDDCL;
   6284  1.194  yamaguch 	u_char rconfid, type, rlen;
   6285  1.194  yamaguch 	void *buf;
   6286  1.194  yamaguch 	size_t blen;
   6287  1.194  yamaguch 
   6288  1.194  yamaguch 	rconfid = sp->scp[cp->protoidx].rconfid;
   6289  1.194  yamaguch 	type = sp->scp[cp->protoidx].rcr_type;
   6290  1.194  yamaguch 	buf = sp->scp[cp->protoidx].rcr_buf;
   6291  1.194  yamaguch 	rlen = sp->scp[cp->protoidx].rcr_rlen;
   6292  1.194  yamaguch 	blen = sp->scp[cp->protoidx].rcr_blen;
   6293  1.194  yamaguch 
   6294  1.194  yamaguch 	sp->scp[cp->protoidx].rcr_buf = NULL;
   6295  1.194  yamaguch 	sp->scp[cp->protoidx].rcr_blen = 0;
   6296  1.194  yamaguch 
   6297  1.194  yamaguch 	if (buf != NULL) {
   6298  1.194  yamaguch 		if (rlen > 0) {
   6299  1.194  yamaguch 			if (debug) {
   6300  1.194  yamaguch 				log(LOG_DEBUG, "%s: send %s\n",
   6301  1.194  yamaguch 				    ifp->if_xname, sppp_cp_type_name(type));
   6302  1.194  yamaguch 			}
   6303  1.194  yamaguch 			sppp_cp_send(sp, cp->proto, type, rconfid, rlen, buf);
   6304  1.194  yamaguch 		}
   6305  1.194  yamaguch 		kmem_free(buf, blen);
   6306  1.194  yamaguch 	}
   6307  1.194  yamaguch }
   6308  1.199  yamaguch 
   6309  1.199  yamaguch static void
   6310  1.199  yamaguch sppp_ifdown(struct sppp *sp, void *xcp __unused)
   6311  1.199  yamaguch {
   6312  1.199  yamaguch 
   6313  1.199  yamaguch 	SPPP_UNLOCK(sp);
   6314  1.199  yamaguch 	if_down(&sp->pp_if);
   6315  1.199  yamaguch 	IF_PURGE(&sp->pp_cpq);
   6316  1.199  yamaguch 	SPPP_LOCK(sp, RW_WRITER);
   6317  1.199  yamaguch }
   6318  1.199  yamaguch 
   6319    1.3  explorer /*
   6320    1.3  explorer  * This file is large.  Tell emacs to highlight it nevertheless.
   6321    1.3  explorer  *
   6322    1.3  explorer  * Local Variables:
   6323    1.3  explorer  * hilit-auto-highlight-maxout: 120000
   6324    1.3  explorer  * End:
   6325    1.3  explorer  */
   6326  1.147  pgoyette 
   6327  1.147  pgoyette /*
   6328  1.147  pgoyette  * Module glue
   6329  1.147  pgoyette  */
   6330  1.147  pgoyette MODULE(MODULE_CLASS_MISC, sppp_subr, NULL);
   6331  1.147  pgoyette 
   6332  1.147  pgoyette static int
   6333  1.147  pgoyette sppp_subr_modcmd(modcmd_t cmd, void *arg)
   6334  1.147  pgoyette {
   6335  1.147  pgoyette         switch (cmd) {
   6336  1.147  pgoyette         case MODULE_CMD_INIT:
   6337  1.147  pgoyette         case MODULE_CMD_FINI:
   6338  1.147  pgoyette                 return 0;
   6339  1.147  pgoyette         case MODULE_CMD_STAT:
   6340  1.147  pgoyette         case MODULE_CMD_AUTOUNLOAD:
   6341  1.147  pgoyette         default:
   6342  1.147  pgoyette                 return ENOTTY;
   6343  1.147  pgoyette         }
   6344  1.147  pgoyette }
   6345  1.147  pgoyette 
   6346  1.170  knakahar static void
   6347  1.170  knakahar sppp_notify_up(struct sppp *sp)
   6348  1.170  knakahar {
   6349  1.170  knakahar 
   6350  1.198  yamaguch 	sppp_wq_add(sp->wq_cp, &sp->scp[IDX_LCP].work_up);
   6351  1.170  knakahar }
   6352  1.170  knakahar 
   6353  1.170  knakahar static void
   6354  1.170  knakahar sppp_notify_down(struct sppp *sp)
   6355  1.170  knakahar {
   6356  1.170  knakahar 
   6357  1.198  yamaguch 	sppp_wq_add(sp->wq_cp, &sp->scp[IDX_LCP].work_down);
   6358  1.170  knakahar }
   6359  1.170  knakahar 
   6360  1.170  knakahar static void
   6361  1.170  knakahar sppp_notify_tls_wlocked(struct sppp *sp)
   6362  1.170  knakahar {
   6363  1.170  knakahar 
   6364  1.170  knakahar 	KASSERT(SPPP_WLOCKED(sp));
   6365  1.170  knakahar 
   6366  1.170  knakahar 	if (!sp->pp_tls)
   6367  1.170  knakahar 		return;
   6368  1.170  knakahar 
   6369  1.170  knakahar 	SPPP_UNLOCK(sp);
   6370  1.170  knakahar 	sp->pp_tls(sp);
   6371  1.170  knakahar 	SPPP_LOCK(sp, RW_WRITER);
   6372  1.170  knakahar }
   6373  1.170  knakahar 
   6374  1.170  knakahar static void
   6375  1.170  knakahar sppp_notify_tlf_wlocked(struct sppp *sp)
   6376  1.170  knakahar {
   6377  1.170  knakahar 
   6378  1.170  knakahar 	KASSERT(SPPP_WLOCKED(sp));
   6379  1.170  knakahar 
   6380  1.170  knakahar 	if (!sp->pp_tlf)
   6381  1.170  knakahar 		return;
   6382  1.170  knakahar 
   6383  1.170  knakahar 	SPPP_UNLOCK(sp);
   6384  1.170  knakahar 	sp->pp_tlf(sp);
   6385  1.170  knakahar 	SPPP_LOCK(sp, RW_WRITER);
   6386  1.170  knakahar }
   6387  1.170  knakahar 
   6388  1.170  knakahar static void
   6389  1.170  knakahar sppp_notify_con(struct sppp *sp)
   6390  1.170  knakahar {
   6391  1.170  knakahar 
   6392  1.170  knakahar 	if (!sp->pp_con)
   6393  1.170  knakahar 		return;
   6394  1.170  knakahar 
   6395  1.170  knakahar 	sp->pp_con(sp);
   6396  1.170  knakahar }
   6397  1.170  knakahar 
   6398  1.171  knakahar #ifdef INET6
   6399  1.170  knakahar static void
   6400  1.170  knakahar sppp_notify_con_wlocked(struct sppp *sp)
   6401  1.170  knakahar {
   6402  1.170  knakahar 
   6403  1.170  knakahar 	KASSERT(SPPP_WLOCKED(sp));
   6404  1.170  knakahar 
   6405  1.170  knakahar 	SPPP_UNLOCK(sp);
   6406  1.170  knakahar 	sppp_notify_con(sp);
   6407  1.170  knakahar 	SPPP_LOCK(sp, RW_WRITER);
   6408  1.170  knakahar 
   6409  1.170  knakahar }
   6410  1.171  knakahar #endif
   6411  1.170  knakahar 
   6412  1.170  knakahar static void
   6413  1.170  knakahar sppp_notify_chg_wlocked(struct sppp *sp)
   6414  1.170  knakahar {
   6415  1.170  knakahar 
   6416  1.170  knakahar 	KASSERT(SPPP_WLOCKED(sp));
   6417  1.170  knakahar 
   6418  1.170  knakahar 	if (!sp->pp_chg)
   6419  1.170  knakahar 		return;
   6420  1.170  knakahar 
   6421  1.170  knakahar 	SPPP_UNLOCK(sp);
   6422  1.170  knakahar 	sp->pp_chg(sp, sp->pp_phase);
   6423  1.170  knakahar 	SPPP_LOCK(sp, RW_WRITER);
   6424  1.170  knakahar }
   6425  1.198  yamaguch 
   6426  1.198  yamaguch static void
   6427  1.198  yamaguch sppp_wq_work(struct work *wk, void *xsp)
   6428  1.198  yamaguch {
   6429  1.198  yamaguch 	struct sppp *sp;
   6430  1.198  yamaguch 	struct sppp_work *work;
   6431  1.198  yamaguch 
   6432  1.198  yamaguch 	sp = xsp;
   6433  1.198  yamaguch 	work = container_of(wk, struct sppp_work, work);
   6434  1.198  yamaguch 	atomic_cas_uint(&work->state, SPPP_WK_BUSY, SPPP_WK_FREE);
   6435  1.198  yamaguch 
   6436  1.198  yamaguch 	SPPP_LOCK(sp, RW_WRITER);
   6437  1.198  yamaguch 	work->func(sp, work->arg);
   6438  1.198  yamaguch 	SPPP_UNLOCK(sp);
   6439  1.198  yamaguch }
   6440  1.198  yamaguch 
   6441  1.198  yamaguch static struct workqueue *
   6442  1.198  yamaguch sppp_wq_create(struct sppp *sp, const char *xnamebuf, pri_t prio, int ipl, int flags)
   6443  1.198  yamaguch {
   6444  1.198  yamaguch 	struct workqueue *wq;
   6445  1.198  yamaguch 	int error;
   6446  1.198  yamaguch 
   6447  1.198  yamaguch 	error = workqueue_create(&wq, xnamebuf, sppp_wq_work,
   6448  1.198  yamaguch 	    (void *)sp, prio, ipl, flags);
   6449  1.198  yamaguch 	if (error) {
   6450  1.198  yamaguch 		panic("%s: workqueue_create failed [%s, %d]\n",
   6451  1.198  yamaguch 		    sp->pp_if.if_xname, xnamebuf, error);
   6452  1.198  yamaguch 	}
   6453  1.198  yamaguch 
   6454  1.198  yamaguch 	return wq;
   6455  1.198  yamaguch }
   6456  1.198  yamaguch 
   6457  1.198  yamaguch static void
   6458  1.198  yamaguch sppp_wq_destroy(struct sppp *sp __unused, struct workqueue *wq)
   6459  1.198  yamaguch {
   6460  1.198  yamaguch 
   6461  1.198  yamaguch 	workqueue_destroy(wq);
   6462  1.198  yamaguch }
   6463  1.198  yamaguch 
   6464  1.198  yamaguch static void
   6465  1.198  yamaguch sppp_wq_set(struct sppp_work *work,
   6466  1.198  yamaguch     void (*func)(struct sppp *, void *), void *arg)
   6467  1.198  yamaguch {
   6468  1.198  yamaguch 
   6469  1.198  yamaguch 	work->func = func;
   6470  1.198  yamaguch 	work->arg = arg;
   6471  1.198  yamaguch }
   6472  1.198  yamaguch 
   6473  1.198  yamaguch static void
   6474  1.198  yamaguch sppp_wq_add(struct workqueue *wq, struct sppp_work *work)
   6475  1.198  yamaguch {
   6476  1.198  yamaguch 
   6477  1.198  yamaguch 	if (atomic_cas_uint(&work->state, SPPP_WK_FREE, SPPP_WK_BUSY)
   6478  1.198  yamaguch 	    != SPPP_WK_FREE)
   6479  1.198  yamaguch 		return;
   6480  1.198  yamaguch 
   6481  1.198  yamaguch 	KASSERT(work->func != NULL);
   6482  1.198  yamaguch 	kpreempt_disable();
   6483  1.198  yamaguch 	workqueue_enqueue(wq, &work->work, NULL);
   6484  1.198  yamaguch 	kpreempt_enable();
   6485  1.198  yamaguch }
   6486  1.198  yamaguch static void
   6487  1.198  yamaguch sppp_wq_wait(struct workqueue *wq, struct sppp_work *work)
   6488  1.198  yamaguch {
   6489  1.198  yamaguch 
   6490  1.198  yamaguch 	atomic_swap_uint(&work->state, SPPP_WK_UNAVAIL);
   6491  1.198  yamaguch 	workqueue_wait(wq, &work->work);
   6492  1.198  yamaguch }
   6493