xref: /openbsd/usr.sbin/ifstated/ifstated.h (revision 5e48d115)
1 /*	$OpenBSD: ifstated.h,v 1.4 2004/03/10 00:13:38 deraadt Exp $	*/
2 
3 /*
4  * Copyright (c) 2004 Ryan McBride
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR OR HIS RELATIVES BE LIABLE FOR ANY DIRECT,
20  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22  * SERVICES; LOSS OF MIND, USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
24  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
25  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
26  * THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include <sys/types.h>
30 #include <sys/queue.h>
31 
32 
33 struct ifsd_expression;
34 TAILQ_HEAD(ifsd_expression_list, ifsd_expression);
35 
36 struct ifsd_ifstate {
37 	TAILQ_ENTRY(ifsd_ifstate)	 entries;
38 	struct ifsd_expression_list	 expressions;
39 	int				 ifstate;
40 #define IFSD_LINKUNKNOWN	0	/* LINK_STATE_UNKNOWN */
41 #define IFSD_LINKDOWN		1	/* LINK_STATE_DOWN */
42 #define IFSD_LINKUP		2	/* LINK_STATE_UP */
43 	int				 prevstate;
44 	u_int32_t			 refcount;
45 	u_short				 ifindex;
46 };
47 
48 struct ifsd_external {
49 	TAILQ_ENTRY(ifsd_external)	 entries;
50 	struct event			 ev;
51 	struct ifsd_expression_list	 expressions;
52 	char				*command;
53 	int				 prevstatus;
54 	u_int32_t			 frequency;
55 	u_int32_t			 refcount;
56 	u_int32_t			 lastexec;
57 	pid_t				 pid;
58 };
59 
60 struct ifsd_action;
61 TAILQ_HEAD(ifsd_action_list, ifsd_action);
62 
63 struct ifsd_action {
64 	TAILQ_ENTRY(ifsd_action)	 entries;
65 	struct ifsd_action		*parent;
66 	union {
67 		char			*logmessage;
68 		char			*command;
69 		struct ifsd_state	*nextstate;
70 		char			*statename;
71 		struct {
72 			struct ifsd_action_list	 actions;
73 			struct ifsd_expression	*expression;
74 			u_int8_t		 ignore_init;
75 		} c;
76 	} act;
77 	u_int32_t			 type;
78 #define IFSD_ACTION_LOG			0
79 #define IFSD_ACTION_COMMAND		1
80 #define IFSD_ACTION_CHANGESTATE		2
81 #define IFSD_ACTION_CONDITION		3
82 };
83 
84 
85 struct ifsd_expression {
86 	TAILQ_ENTRY(ifsd_expression)	 entries;
87 	TAILQ_ENTRY(ifsd_expression)	 eval;
88 	struct ifsd_expression		*parent;
89 	struct ifsd_action		*action;
90 	struct ifsd_expression		*left;
91 	struct ifsd_expression		*right;
92 	union {
93 		struct ifsd_ifstate		*ifstate;
94 		struct ifsd_external		*external;
95 	} u;
96 	int				 depth;
97 	u_int32_t			 type;
98 #define IFSD_OPER_AND	1
99 #define IFSD_OPER_OR	2
100 #define IFSD_OPER_NOT	3
101 #define IFSD_OPER_EXTERNAL	4
102 #define IFSD_OPER_IFSTATE	5
103 	u_int8_t			 truth;
104 };
105 
106 TAILQ_HEAD(ifsd_ifstate_list, ifsd_ifstate);
107 TAILQ_HEAD(ifsd_external_list, ifsd_external);
108 
109 struct ifsd_state {
110 	struct event			 ev;
111 	struct ifsd_ifstate_list	 interface_states;
112 	struct ifsd_external_list	 external_tests;
113 	TAILQ_ENTRY(ifsd_state)		 entries;
114 	struct ifsd_action		*init;
115 	struct ifsd_action		*always;
116 	u_int32_t			 entered;
117 	char				*name;
118 };
119 
120 TAILQ_HEAD(ifsd_state_list, ifsd_state);
121 
122 struct ifsd_config {
123 	struct ifsd_state		 always;
124 	struct ifsd_state_list		 states;
125 	struct ifsd_state		*curstate;
126 	struct ifsd_state		*nextstate;
127 	u_int32_t			 opts;
128 #define IFSD_OPT_VERBOSE	0x00000001
129 #define IFSD_OPT_VERBOSE2	0x00000002
130 #define IFSD_OPT_NOACTION	0x00000004
131 	int				 maxdepth;
132 	u_int8_t			 loglevel;
133 #define IFSD_LOG_NONE		0
134 #define IFSD_LOG_QUIET		1
135 #define IFSD_LOG_NORMAL		2
136 #define IFSD_LOG_VERBOSE	3
137 #define IFSD_LOG_DEBUG		4
138 };
139 
140 enum	{ IFSD_EVTIMER_ADD, IFSD_EVTIMER_DEL };
141 struct ifsd_config *parse_config(char *, int);
142 int	cmdline_symset(char *);
143 void	clear_config(struct ifsd_config *);
144