fsm.h revision 94894
131921Sbrian/*-
231921Sbrian * Copyright (c) 1996 - 2001 Brian Somers <brian@Awfulhak.org>
331921Sbrian *          based on work by Toshiharu OHNO <tony-o@iij.ad.jp>
431921Sbrian *                           Internet Initiative Japan, Inc (IIJ)
531921Sbrian * All rights reserved.
631921Sbrian *
731921Sbrian * Redistribution and use in source and binary forms, with or without
831921Sbrian * modification, are permitted provided that the following conditions
931921Sbrian * are met:
1031921Sbrian * 1. Redistributions of source code must retain the above copyright
1131921Sbrian *    notice, this list of conditions and the following disclaimer.
1231921Sbrian * 2. Redistributions in binary form must reproduce the above copyright
1331921Sbrian *    notice, this list of conditions and the following disclaimer in the
1431921Sbrian *    documentation and/or other materials provided with the distribution.
1531921Sbrian *
1631921Sbrian * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1731921Sbrian * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1831921Sbrian * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1931921Sbrian * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2031921Sbrian * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2131921Sbrian * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2231921Sbrian * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2331921Sbrian * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2431921Sbrian * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2531921Sbrian * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2650479Speter * SUCH DAMAGE.
2730715Sbrian *
2830715Sbrian * $FreeBSD: head/usr.sbin/ppp/fsm.h 94894 2002-04-16 23:57:09Z brian $
2931196Sbrian */
3044279Sbrian
3144279Sbrian/*
3244279Sbrian *  State of machine
3344279Sbrian */
3444279Sbrian#define	ST_INITIAL	0
3544279Sbrian#define	ST_STARTING	1
3644279Sbrian#define	ST_CLOSED	2
3746085Sbrian#define	ST_STOPPED	3
3864698Sbrian#define	ST_CLOSING	4
3964698Sbrian#define	ST_STOPPING	5
4064698Sbrian#define	ST_REQSENT	6
4164698Sbrian#define	ST_ACKRCVD	7
4264698Sbrian#define	ST_ACKSENT	8
4358034Sbrian#define	ST_OPENED	9
4430715Sbrian
4531121Sbrian#define	ST_MAX		10
4646686Sbrian#define	ST_UNDEF	-1
4737192Sbrian
4834539Sbrian#define	MODE_REQ	0
4937192Sbrian#define	MODE_NAK	1
5031196Sbrian#define	MODE_REJ	2
5130715Sbrian#define	MODE_NOP	3
5230715Sbrian#define	MODE_ACK	4	/* pseudo mode for ccp negotiations */
5330715Sbrian
5446686Sbrian#define	OPEN_PASSIVE	-1
5546686Sbrian
5630715Sbrian#define FSM_REQ_TIMER	1
5730715Sbrian#define FSM_TRM_TIMER	2
5830715Sbrian
5965210Sbrian#define FSM_OPTLEN	100
6037010Sbrian
6130715Sbrianstruct fsm;
6230715Sbrian
6330715Sbrianstruct fsm_retry {
6430715Sbrian  u_int timeout;                             /* FSM retry frequency */
6530715Sbrian  u_int maxreq;                              /* Max Config REQ retries */
6631343Sbrian  u_int maxtrm;                              /* Max Term REQ retries */
6736285Sbrian};
6831343Sbrian
6930715Sbrianstruct fsm_decode {
7031196Sbrian  u_char ack[FSM_OPTLEN], *ackend;
7136285Sbrian  u_char nak[FSM_OPTLEN], *nakend;
7236285Sbrian  u_char rej[FSM_OPTLEN], *rejend;
7336285Sbrian};
7436285Sbrian
7531196Sbrianstruct fsm_callbacks {
7636285Sbrian  int (*LayerUp)(struct fsm *);                 /* Layer is now up (tlu) */
7736285Sbrian  void (*LayerDown)(struct fsm *);              /* About to come down (tld) */
7836285Sbrian  void (*LayerStart)(struct fsm *);             /* Layer about to start (tls) */
7936285Sbrian  void (*LayerFinish)(struct fsm *);            /* Layer now down (tlf) */
8036285Sbrian  void (*InitRestartCounter)(struct fsm *, int);/* Set fsm timer load */
8136285Sbrian  void (*SendConfigReq)(struct fsm *);          /* Send REQ please */
8236285Sbrian  void (*SentTerminateReq)(struct fsm *);       /* Term REQ just sent */
8336285Sbrian  void (*SendTerminateAck)(struct fsm *, u_char); /* Send Term ACK please */
8436285Sbrian  void (*DecodeConfig)(struct fsm *, u_char *, u_char *, int,
8536285Sbrian                       struct fsm_decode *);    /* Deal with incoming data */
8636285Sbrian  int (*RecvResetReq)(struct fsm *fp);          /* Reset output */
8736285Sbrian  void (*RecvResetAck)(struct fsm *fp, u_char); /* Reset input */
8836285Sbrian};
8936285Sbrian
9036285Sbrianstruct fsm_parent {
9136285Sbrian  void (*LayerStart) (void *, struct fsm *);         /* tls */
9236285Sbrian  void (*LayerUp) (void *, struct fsm *);            /* tlu */
9336465Sbrian  void (*LayerDown) (void *, struct fsm *);          /* tld */
9436465Sbrian  void (*LayerFinish) (void *, struct fsm *);        /* tlf */
9536285Sbrian  void *object;
9636285Sbrian};
9736465Sbrian
9836465Sbrianstruct link;
9953830Sbrianstruct bundle;
10036285Sbrian
10136285Sbrianstruct fsm {
10236285Sbrian  const char *name;		/* Name of protocol */
10336285Sbrian  u_short proto;		/* Protocol number */
10436285Sbrian  u_short min_code;
10536285Sbrian  u_short max_code;
10631196Sbrian  int open_mode;		/* Delay before config REQ (-1 forever) */
10736285Sbrian  int state;			/* State of the machine */
10831196Sbrian  u_char reqid;			/* Next request id */
10936285Sbrian  int restart;			/* Restart counter value */
11036285Sbrian
11136285Sbrian  struct {
11231196Sbrian    int reqs;			/* Max config REQs before a close() */
11336285Sbrian    int naks;			/* Max config NAKs before a close() */
11431196Sbrian    int rejs;			/* Max config REJs before a close() */
11531203Sbrian  } more;
11636285Sbrian
11736285Sbrian  struct pppTimer FsmTimer;	/* Restart Timer */
11831203Sbrian  struct pppTimer OpenTimer;	/* Delay before opening */
11936285Sbrian
12031203Sbrian  /*
12136285Sbrian   * This timer times the ST_STOPPED state out after the given value
12236285Sbrian   * (specified via "set stopped ...").  Although this isn't specified in the
12336285Sbrian   * rfc, the rfc *does* say that "the application may use higher level
12436285Sbrian   * timers to avoid deadlock". The StoppedTimer takes effect when the other
12536285Sbrian   * side ABENDs rather than going into ST_ACKSENT (and sending the ACK),
12636285Sbrian   * causing ppp to time out and drop into ST_STOPPED.  At this point,
12736285Sbrian   * nothing will change this state :-(
12836285Sbrian   */
12936285Sbrian  struct pppTimer StoppedTimer;
13036285Sbrian  int LogLevel;
13136285Sbrian
13236285Sbrian  /* The link layer active with this FSM (may be our bundle below) */
13331203Sbrian  struct link *link;
13444279Sbrian
13544279Sbrian  /* Our high-level link */
13644279Sbrian  struct bundle *bundle;
13744279Sbrian
13844279Sbrian  const struct fsm_parent *parent;
13944279Sbrian  const struct fsm_callbacks *fn;
14044279Sbrian};
14144279Sbrian
14244279Sbrianstruct fsmheader {
14344279Sbrian  u_char code;			/* Request code */
14444279Sbrian  u_char id;			/* Identification */
14544279Sbrian  u_short length;		/* Length of packet */
14644279Sbrian};
14744279Sbrian
14844279Sbrian#define	CODE_CONFIGREQ	1
14944279Sbrian#define	CODE_CONFIGACK	2
15044279Sbrian#define	CODE_CONFIGNAK	3
15144279Sbrian#define	CODE_CONFIGREJ	4
15244279Sbrian#define	CODE_TERMREQ	5
15344279Sbrian#define	CODE_TERMACK	6
15444279Sbrian#define	CODE_CODEREJ	7
15544279Sbrian#define	CODE_PROTOREJ	8
15644279Sbrian#define	CODE_ECHOREQ	9	/* Used in LCP */
15744279Sbrian#define	CODE_ECHOREP	10	/* Used in LCP */
15844279Sbrian#define	CODE_DISCREQ	11
15944279Sbrian#define	CODE_IDENT	12	/* Used in LCP Extension */
16044279Sbrian#define	CODE_TIMEREM	13	/* Used in LCP Extension */
16144279Sbrian#define	CODE_RESETREQ	14	/* Used in CCP */
16244279Sbrian#define	CODE_RESETACK	15	/* Used in CCP */
16344279Sbrian
16446686Sbrianstruct fsm_opt_hdr {
16546686Sbrian  u_char id;
16646686Sbrian  u_char len;
16746686Sbrian};
16846686Sbrian
16946686Sbrian#define MAX_FSM_OPT_LEN 20
17046686Sbrianstruct fsm_opt {
17146686Sbrian  struct fsm_opt_hdr hdr;
17246686Sbrian  u_char data[MAX_FSM_OPT_LEN-2];
17346686Sbrian};
17446686Sbrian
17546686Sbrian#define INC_FSM_OPT(ty, length, o)                      \
17646686Sbrian  do {                                                  \
17746686Sbrian    (o)->hdr.id = (ty);                                 \
17846686Sbrian    (o)->hdr.len = (length);                            \
17946686Sbrian    (o) = (struct fsm_opt *)((u_char *)(o) + (length)); \
18046686Sbrian  } while (0)
18146686Sbrian
18246686Sbrian
18346686Sbrianextern void fsm_Init(struct fsm *, const char *, u_short, int, int, int,
18446686Sbrian                     struct bundle *, struct link *, const  struct fsm_parent *,
18546686Sbrian                     struct fsm_callbacks *, const char * const [3]);
18646686Sbrianextern void fsm_Output(struct fsm *, u_int, u_int, u_char *, int, int);
18746686Sbrianextern void fsm_Open(struct fsm *);
18846686Sbrianextern void fsm_Up(struct fsm *);
18946686Sbrianextern void fsm_Down(struct fsm *);
19046686Sbrianextern void fsm_Input(struct fsm *, struct mbuf *);
19146686Sbrianextern void fsm_Close(struct fsm *);
19246686Sbrianextern int fsm_NullRecvResetReq(struct fsm *);
19346686Sbrianextern void fsm_NullRecvResetAck(struct fsm *, u_char);
19446686Sbrianextern void fsm_Reopen(struct fsm *);
19546686Sbrianextern void fsm2initial(struct fsm *);
19646686Sbrianextern const char *State2Nam(u_int);
19746686Sbrianextern struct fsm_opt *fsm_readopt(u_char **);
19846686Sbrianextern void fsm_rej(struct fsm_decode *, const struct fsm_opt *);
19946686Sbrianextern void fsm_ack(struct fsm_decode *, const struct fsm_opt *);
20046686Sbrianextern void fsm_nak(struct fsm_decode *, const struct fsm_opt *);
20146686Sbrianextern void fsm_opt_normalise(struct fsm_decode *);
20246686Sbrian