1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
|
/* $OpenBSD: relayd.h,v 1.19 2007/02/06 08:45:46 pyr Exp $ */
/*
* Copyright (c) 2006 Pierre-Yves Ritschard <pyr@spootnik.org>
* Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#define CONF_FILE "/etc/hoststated.conf"
#define HOSTSTATED_SOCKET "/var/run/hoststated.sock"
#define PF_SOCKET "/dev/pf"
#define HOSTSTATED_USER "_hoststated"
#define HOSTSTATED_ANCHOR "hoststated"
#define CHECK_TIMEOUT 200
#define CHECK_INTERVAL 10
#define EMPTY_TABLE UINT_MAX
#define EMPTY_ID UINT_MAX
#define TABLE_NAME_SIZE 32
#define TAG_NAME_SIZE 64
#define SRV_NAME_SIZE 64
#define MAX_NAME_SIZE 64
#define SRV_MAX_VIRTS 16
#define SMALL_READ_BUF_SIZE 1024
#define READ_BUF_SIZE 65535
#define ICMP_BUF_SIZE 64
/* buffer */
struct buf {
TAILQ_ENTRY(buf) entry;
u_char *buf;
size_t size;
size_t max;
size_t wpos;
size_t rpos;
};
struct msgbuf {
TAILQ_HEAD(, buf) bufs;
u_int32_t queued;
int fd;
};
#define IMSG_HEADER_SIZE sizeof(struct imsg_hdr)
#define MAX_IMSGSIZE 8192
struct buf_read {
u_char buf[READ_BUF_SIZE];
u_char *rptr;
size_t wpos;
};
struct imsgbuf {
TAILQ_HEAD(, imsg_fd) fds;
struct buf_read r;
struct msgbuf w;
struct event ev;
void (*handler)(int, short, void *);
int fd;
pid_t pid;
short events;
};
enum imsg_type {
IMSG_NONE,
IMSG_CTL_OK, /* answer to hoststatectl requests */
IMSG_CTL_FAIL,
IMSG_CTL_END,
IMSG_CTL_SERVICE,
IMSG_CTL_TABLE,
IMSG_CTL_HOST,
IMSG_CTL_TABLE_CHANGED,
IMSG_CTL_PULL_RULESET,
IMSG_CTL_PUSH_RULESET,
IMSG_CTL_SHOW_SUM, /* hoststatectl requests */
IMSG_CTL_SERVICE_ENABLE,
IMSG_CTL_SERVICE_DISABLE,
IMSG_CTL_TABLE_ENABLE,
IMSG_CTL_TABLE_DISABLE,
IMSG_CTL_HOST_ENABLE,
IMSG_CTL_HOST_DISABLE,
IMSG_CTL_SHUTDOWN,
IMSG_CTL_RELOAD,
IMSG_CTL_NOTIFY,
IMSG_SERVICE_ENABLE, /* notifies from pfe to hce */
IMSG_SERVICE_DISABLE,
IMSG_TABLE_ENABLE,
IMSG_TABLE_DISABLE,
IMSG_HOST_ENABLE,
IMSG_HOST_DISABLE,
IMSG_HOST_STATUS, /* notifies from hce to pfe */
IMSG_SYNC
};
struct imsg_hdr {
enum imsg_type type;
u_int16_t len;
u_int32_t peerid;
pid_t pid;
};
struct imsg {
struct imsg_hdr hdr;
void *data;
};
typedef u_int32_t objid_t;
struct ctl_status {
objid_t id;
int up;
};
struct ctl_id {
objid_t id;
char name[MAX_NAME_SIZE];
};
struct ctl_icmp_event {
struct hoststated *env;
int s;
int af;
int last_up;
struct event ev;
struct timeval tv_start;
};
struct ctl_tcp_event {
int s;
char *req;
struct buf *buf;
struct host *host;
struct table *table;
struct timeval tv_start;
struct event ev;
int (*validate_read)(struct ctl_tcp_event *);
int (*validate_close)(struct ctl_tcp_event *);
SSL *ssl;
char rbuf[SMALL_READ_BUF_SIZE];
};
struct address {
struct sockaddr_storage ss;
in_port_t port;
char ifname[IFNAMSIZ];
TAILQ_ENTRY(address) entry;
};
TAILQ_HEAD(addresslist, address);
#define F_DISABLE 0x0001
#define F_BACKUP 0x0002
#define F_USED 0x0004
#define F_DOWN 0x0008
#define F_ADD 0x0010
#define F_DEL 0x0020
#define F_CHANGED 0x0040
#define F_STICKY 0x0080
#define F_CHECK_DONE 0x0100
#define F_ACTIVE_RULESET 0x0200
#define F_CHECK_SENT 0x0400
#define F_SSL 0x0800
struct host {
u_int16_t flags;
objid_t id;
objid_t tableid;
char *tablename;
char name[MAXHOSTNAMELEN];
int up;
int last_up;
struct sockaddr_storage ss;
struct ctl_tcp_event cte;
TAILQ_ENTRY(host) entry;
};
TAILQ_HEAD(hostlist, host);
#define HOST_DOWN -1
#define HOST_UNKNOWN 0
#define HOST_UP 1
struct table {
objid_t id;
objid_t serviceid;
u_int16_t flags;
int check;
int up;
in_port_t port;
int retcode;
struct timeval timeout;
char name[TABLE_NAME_SIZE];
char path[MAXPATHLEN];
char *sendbuf;
char exbuf[64];
char digest[41]; /* length of sha1 digest * 2 */
SSL_CTX *ssl_ctx;
struct hostlist hosts;
TAILQ_ENTRY(table) entry;
};
TAILQ_HEAD(tablelist, table);
#define CHECK_NOCHECK 0
#define CHECK_ICMP 1
#define CHECK_TCP 2
#define CHECK_HTTP_CODE 3
#define CHECK_HTTP_DIGEST 4
#define CHECK_SEND_EXPECT 5
struct service {
objid_t id;
u_int16_t flags;
in_port_t port;
char name[SRV_NAME_SIZE];
char tag[TAG_NAME_SIZE];
struct addresslist virts;
struct table *table;
struct table *backup; /* use this if no host up */
TAILQ_ENTRY(service) entry;
};
TAILQ_HEAD(servicelist, service);
enum {
PROC_MAIN,
PROC_PFE,
PROC_HCE
} hoststated_process;
struct hoststated {
u_int8_t opts;
u_int16_t flags;
struct pfdata *pf;
int tablecount;
int servicecount;
struct timeval interval;
struct timeval timeout;
struct table empty_table;
struct event ev;
struct tablelist tables;
struct servicelist services;
u_int16_t id;
int has_icmp;
int has_icmp6;
struct ctl_icmp_event icmp_send;
struct ctl_icmp_event icmp_recv;
struct ctl_icmp_event icmp6_send;
struct ctl_icmp_event icmp6_recv;
};
#define HOSTSTATED_OPT_VERBOSE 0x01
#define HOSTSTATED_OPT_NOACTION 0x04
/* initially control.h */
struct {
struct event ev;
int fd;
} control_state;
enum blockmodes {
BM_NORMAL,
BM_NONBLOCK
};
struct ctl_conn {
TAILQ_ENTRY(ctl_conn) entry;
u_int8_t flags;
#define CTL_CONN_NOTIFY 0x01
struct imsgbuf ibuf;
};
TAILQ_HEAD(ctl_connlist, ctl_conn);
/* control.c */
int control_init(void);
int control_listen(void);
void control_accept(int, short, void *);
void control_dispatch_imsg(int, short, void *);
int control_imsg_relay(struct imsg *);
void control_imsg_forward(struct imsg *);
void control_cleanup(void);
void session_socket_blockmode(int, enum blockmodes);
extern struct ctl_connlist ctl_conns;
/* parse.y */
int parse_config(struct hoststated *, const char *, int);
/* log.c */
void log_init(int);
void log_warn(const char *, ...);
void log_warnx(const char *, ...);
void log_info(const char *, ...);
void log_debug(const char *, ...);
void fatal(const char *);
void fatalx(const char *);
/* buffer.c */
struct buf *buf_open(size_t);
struct buf *buf_dynamic(size_t, size_t);
int buf_add(struct buf *, void *, size_t);
void *buf_reserve(struct buf *, size_t);
void *buf_seek(struct buf *, size_t, size_t);
int buf_close(struct msgbuf *, struct buf *);
void buf_free(struct buf *);
void msgbuf_init(struct msgbuf *);
void msgbuf_clear(struct msgbuf *);
int msgbuf_write(struct msgbuf *);
/* imsg.c */
void imsg_init(struct imsgbuf *, int, void (*)(int, short, void *));
ssize_t imsg_read(struct imsgbuf *);
ssize_t imsg_get(struct imsgbuf *, struct imsg *);
int imsg_compose(struct imsgbuf *, enum imsg_type, u_int32_t, pid_t,
void *, u_int16_t);
struct buf *imsg_create(struct imsgbuf *, enum imsg_type, u_int32_t, pid_t,
u_int16_t);
int imsg_add(struct buf *, void *, u_int16_t);
int imsg_close(struct imsgbuf *, struct buf *);
void imsg_free(struct imsg *);
void imsg_event_add(struct imsgbuf *); /* needs to be provided externally */
/* pfe.c */
pid_t pfe(struct hoststated *, int [2], int [2], int [2]);
void show(struct ctl_conn *);
int enable_service(struct ctl_conn *, struct ctl_id *);
int enable_table(struct ctl_conn *, struct ctl_id *);
int enable_host(struct ctl_conn *, struct ctl_id *);
int disable_service(struct ctl_conn *, struct ctl_id *);
int disable_table(struct ctl_conn *, struct ctl_id *);
int disable_host(struct ctl_conn *, struct ctl_id *);
/* pfe_filter.c */
void init_filter(struct hoststated *);
void init_tables(struct hoststated *);
void flush_table(struct hoststated *, struct service *);
void sync_table(struct hoststated *, struct service *, struct table *);
void sync_ruleset(struct hoststated *, struct service *, int);
void flush_rulesets(struct hoststated *);
/* hce.c */
pid_t hce(struct hoststated *, int [2], int [2], int [2]);
void hce_notify_done(struct host *, const char *);
/* check_icmp.c */
void icmp_init(struct hoststated *);
void schedule_icmp(struct hoststated *, struct host *);
void check_icmp(struct hoststated *, struct timeval *);
/* check_tcp.c */
void check_tcp(struct ctl_tcp_event *);
/* check_http.c */
int check_http_code(struct ctl_tcp_event *);
int check_http_digest(struct ctl_tcp_event *);
/* check_send_expect.c */
int check_send_expect(struct ctl_tcp_event *);
/* ssl.c */
void ssl_init(struct hoststated *);
void ssl_transaction(struct ctl_tcp_event *);
SSL_CTX *ssl_ctx_create(struct hoststated *);
/* hoststated.c */
struct host *host_find(struct hoststated *, objid_t);
struct table *table_find(struct hoststated *, objid_t);
struct service *service_find(struct hoststated *, objid_t);
struct host *host_findbyname(struct hoststated *, const char *);
struct table *table_findbyname(struct hoststated *, const char *);
struct service *service_findbyname(struct hoststated *, const char *);
void event_again(struct event *, int, short,
void (*)(int, short, void *),
struct timeval *, struct timeval *, void *);
|