1 /* $OpenBSD: if_pfsync.h,v 1.30 2006/10/31 14:49:01 henning Exp $ */
4 * Copyright (c) 2001 Michael Shalayeff
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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.
29 #ifndef _NET_IF_PFSYNC_H_
30 #define _NET_IF_PFSYNC_H_
33 #define PFSYNC_ID_LEN sizeof(u_int64_t)
35 struct pfsync_state_scrub {
37 u_int8_t pfss_ttl; /* stashed TTL */
38 #define PFSYNC_SCRUB_FLAG_VALID 0x01
40 u_int32_t pfss_ts_mod; /* timestamp modulation */
43 struct pfsync_state_host {
49 struct pfsync_state_peer {
50 struct pfsync_state_scrub scrub; /* state is scrubbed */
51 u_int32_t seqlo; /* Max sequence number sent */
52 u_int32_t seqhi; /* Max the other end ACKd + win */
53 u_int32_t seqdiff; /* Sequence number modulator */
54 u_int16_t max_win; /* largest window (pre scaling) */
55 u_int16_t mss; /* Maximum segment size option */
56 u_int8_t state; /* active state level */
57 u_int8_t wscale; /* window scaling factor */
63 char ifname[IFNAMSIZ];
64 struct pfsync_state_host lan;
65 struct pfsync_state_host gwy;
66 struct pfsync_state_host ext;
67 struct pfsync_state_peer src;
68 struct pfsync_state_peer dst;
69 struct pf_addr rt_addr;
75 u_int32_t packets[2][2];
76 u_int32_t bytes[2][2];
88 #define PFSYNC_FLAG_COMPRESS 0x01
89 #define PFSYNC_FLAG_STALE 0x02
93 union sockaddr_union dst;
101 struct pfsync_state_upd {
103 struct pfsync_state_peer src;
104 struct pfsync_state_peer dst;
112 struct pfsync_state_del {
124 struct pfsync_state_upd_req {
130 struct pfsync_state_clr {
131 char ifname[IFNAMSIZ];
136 struct pfsync_state_bus {
140 #define PFSYNC_BUS_START 1
141 #define PFSYNC_BUS_END 2
148 struct pfsync_state *s;
149 struct pfsync_state_upd *u;
150 struct pfsync_state_del *d;
151 struct pfsync_state_clr *c;
152 struct pfsync_state_bus *b;
153 struct pfsync_state_upd_req *r;
156 union sc_tdb_statep {
157 struct pfsync_tdb *t;
160 extern int pfsync_sync_ok;
162 struct pfsync_softc {
164 struct ifnet *sc_sync_ifp;
166 struct ip_moptions sc_imo;
167 struct timeout sc_tmo;
168 struct timeout sc_tdb_tmo;
169 struct timeout sc_bulk_tmo;
170 struct timeout sc_bulkfail_tmo;
171 struct in_addr sc_sync_peer;
172 struct in_addr sc_sendaddr;
173 struct mbuf *sc_mbuf; /* current cumulative mbuf */
174 struct mbuf *sc_mbuf_net; /* current cumulative mbuf */
175 struct mbuf *sc_mbuf_tdb; /* dito for TDB updates */
176 union sc_statep sc_statep;
177 union sc_statep sc_statep_net;
178 union sc_tdb_statep sc_statep_tdb;
179 u_int32_t sc_ureq_received;
180 u_int32_t sc_ureq_sent;
181 struct pf_state *sc_bulk_send_next;
182 struct pf_state *sc_bulk_terminator;
184 int sc_maxcount; /* number of states in mtu */
185 int sc_maxupdates; /* number of updates/state */
188 extern struct pfsync_softc *pfsyncif;
192 struct pfsync_header {
194 #define PFSYNC_VERSION 3
197 #define PFSYNC_ACT_CLR 0 /* clear all states */
198 #define PFSYNC_ACT_INS 1 /* insert state */
199 #define PFSYNC_ACT_UPD 2 /* update state */
200 #define PFSYNC_ACT_DEL 3 /* delete state */
201 #define PFSYNC_ACT_UPD_C 4 /* "compressed" state update */
202 #define PFSYNC_ACT_DEL_C 5 /* "compressed" state delete */
203 #define PFSYNC_ACT_INS_F 6 /* insert fragment */
204 #define PFSYNC_ACT_DEL_F 7 /* delete fragments */
205 #define PFSYNC_ACT_UREQ 8 /* request "uncompressed" state */
206 #define PFSYNC_ACT_BUS 9 /* Bulk Update Status */
207 #define PFSYNC_ACT_TDB_UPD 10 /* TDB replay counter update */
208 #define PFSYNC_ACT_MAX 11
210 u_int8_t pf_chksum[PF_MD5_DIGEST_LENGTH];
213 #define PFSYNC_BULKPACKETS 1 /* # of packets per timeout */
214 #define PFSYNC_MAX_BULKTRIES 12
215 #define PFSYNC_HDRLEN sizeof(struct pfsync_header)
216 #define PFSYNC_ACTIONS \
217 "CLR ST", "INS ST", "UPD ST", "DEL ST", \
218 "UPD ST COMP", "DEL ST COMP", "INS FR", "DEL FR", \
219 "UPD REQ", "BLK UPD STAT", "TDB UPD"
221 #define PFSYNC_DFLTTL 255
224 u_int64_t pfsyncs_ipackets; /* total input packets, IPv4 */
225 u_int64_t pfsyncs_ipackets6; /* total input packets, IPv6 */
226 u_int64_t pfsyncs_badif; /* not the right interface */
227 u_int64_t pfsyncs_badttl; /* TTL is not PFSYNC_DFLTTL */
228 u_int64_t pfsyncs_hdrops; /* packets shorter than hdr */
229 u_int64_t pfsyncs_badver; /* bad (incl unsupp) version */
230 u_int64_t pfsyncs_badact; /* bad action */
231 u_int64_t pfsyncs_badlen; /* data length does not match */
232 u_int64_t pfsyncs_badauth; /* bad authentication */
233 u_int64_t pfsyncs_stale; /* stale state */
234 u_int64_t pfsyncs_badval; /* bad values */
235 u_int64_t pfsyncs_badstate; /* insert/lookup failed */
237 u_int64_t pfsyncs_opackets; /* total output packets, IPv4 */
238 u_int64_t pfsyncs_opackets6; /* total output packets, IPv6 */
239 u_int64_t pfsyncs_onomem; /* no memory for an mbuf */
240 u_int64_t pfsyncs_oerrors; /* ip output error */
244 * Configuration structure for SIOCSETPFSYNC SIOCGETPFSYNC
247 char pfsyncr_syncdev[IFNAMSIZ];
248 struct in_addr pfsyncr_syncpeer;
249 int pfsyncr_maxupdates;
250 int pfsyncr_authlevel;
254 #define pf_state_peer_hton(s,d) do { \
255 (d)->seqlo = htonl((s)->seqlo); \
256 (d)->seqhi = htonl((s)->seqhi); \
257 (d)->seqdiff = htonl((s)->seqdiff); \
258 (d)->max_win = htons((s)->max_win); \
259 (d)->mss = htons((s)->mss); \
260 (d)->state = (s)->state; \
261 (d)->wscale = (s)->wscale; \
263 (d)->scrub.pfss_flags = \
264 htons((s)->scrub->pfss_flags & PFSS_TIMESTAMP); \
265 (d)->scrub.pfss_ttl = (s)->scrub->pfss_ttl; \
266 (d)->scrub.pfss_ts_mod = htonl((s)->scrub->pfss_ts_mod);\
267 (d)->scrub.scrub_flag = PFSYNC_SCRUB_FLAG_VALID; \
271 #define pf_state_peer_ntoh(s,d) do { \
272 (d)->seqlo = ntohl((s)->seqlo); \
273 (d)->seqhi = ntohl((s)->seqhi); \
274 (d)->seqdiff = ntohl((s)->seqdiff); \
275 (d)->max_win = ntohs((s)->max_win); \
276 (d)->mss = ntohs((s)->mss); \
277 (d)->state = (s)->state; \
278 (d)->wscale = (s)->wscale; \
279 if ((s)->scrub.scrub_flag == PFSYNC_SCRUB_FLAG_VALID && \
280 (d)->scrub != NULL) { \
281 (d)->scrub->pfss_flags = \
282 ntohs((s)->scrub.pfss_flags) & PFSS_TIMESTAMP; \
283 (d)->scrub->pfss_ttl = (s)->scrub.pfss_ttl; \
284 (d)->scrub->pfss_ts_mod = ntohl((s)->scrub.pfss_ts_mod);\
288 #define pf_state_host_hton(s,d) do { \
289 bcopy(&(s)->addr, &(d)->addr, sizeof((d)->addr)); \
290 (d)->port = (s)->port; \
293 #define pf_state_host_ntoh(s,d) do { \
294 bcopy(&(s)->addr, &(d)->addr, sizeof((d)->addr)); \
295 (d)->port = (s)->port; \
298 #define pf_state_counter_hton(s,d) do { \
299 d[0] = htonl((s>>32)&0xffffffff); \
300 d[1] = htonl(s&0xffffffff); \
303 #define pf_state_counter_ntoh(s,d) do { \
310 void pfsync_input(struct mbuf *, ...);
311 int pfsync_clear_states(u_int32_t, char *);
312 int pfsync_pack_state(u_int8_t, struct pf_state *, int);
313 #define pfsync_insert_state(st) do { \
314 if ((st->rule.ptr->rule_flag & PFRULE_NOSYNC) || \
315 (st->proto == IPPROTO_PFSYNC)) \
316 st->sync_flags |= PFSTATE_NOSYNC; \
317 else if (!st->sync_flags) \
318 pfsync_pack_state(PFSYNC_ACT_INS, (st), \
319 PFSYNC_FLAG_COMPRESS); \
320 st->sync_flags &= ~PFSTATE_FROMSYNC; \
322 #define pfsync_update_state(st) do { \
323 if (!st->sync_flags) \
324 pfsync_pack_state(PFSYNC_ACT_UPD, (st), \
325 PFSYNC_FLAG_COMPRESS); \
326 st->sync_flags &= ~PFSTATE_FROMSYNC; \
328 #define pfsync_delete_state(st) do { \
329 if (!st->sync_flags) \
330 pfsync_pack_state(PFSYNC_ACT_DEL, (st), \
331 PFSYNC_FLAG_COMPRESS); \
333 int pfsync_update_tdb(struct tdb *, int);
336 #endif /* _NET_IF_PFSYNC_H_ */