| 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
 | $OpenBSD: patch-sf-gencode_c,v 1.1 2008/06/13 00:38:12 canacar Exp $
--- sf-gencode.c.orig	Tue Nov  6 23:34:18 2007
+++ sf-gencode.c	Wed Jun 11 19:50:10 2008
@@ -474,9 +474,107 @@ gen_proto(int proto)
 			(bpf_int32)proto));
 }
 
+#ifdef HAVE_PFSYNC_KEY
 static struct block *
 gen_hostop(bpf_u_int32 addr, bpf_u_int32 mask, int dir)
 {
+	struct block *b0, *b1, *b2, *bi, *bo;
+	const static int isrc_off = offsetof(pf_state_t, key[PF_SK_STACK].addr[0].v4);
+	const static int osrc_off = offsetof(pf_state_t, key[PF_SK_WIRE].addr[1].v4);
+	const static int idst_off = offsetof(pf_state_t, key[PF_SK_STACK].addr[1].v4);
+	const static int odst_off = offsetof(pf_state_t, key[PF_SK_WIRE].addr[0].v4);
+
+	const static int igwy1_off = offsetof(pf_state_t, key[PF_SK_WIRE].addr[0].v4);
+	const static int ogwy1_off = offsetof(pf_state_t, key[PF_SK_STACK].addr[1].v4);
+	const static int igwy2_off = offsetof(pf_state_t, key[PF_SK_WIRE].addr[1].v4);
+	const static int ogwy2_off = offsetof(pf_state_t, key[PF_SK_STACK].addr[0].v4);
+
+	addr = ntohl(addr);
+	mask = ntohl(mask);
+
+	bi = gen_cmp(offsetof(pf_state_t, direction), BPF_B, (bpf_int32)PF_IN);
+	bo = gen_cmp(offsetof(pf_state_t, direction), BPF_B, (bpf_int32)PF_OUT);
+
+	switch (dir) {
+
+	case Q_SRC:
+		b1 = gen_mcmp(osrc_off, BPF_W, addr, mask);
+		gen_and(bo, b1);
+		b0 = gen_mcmp(isrc_off, BPF_W, addr, mask);
+		gen_and(bi, b0);
+		gen_or(b0, b1);
+		break;
+
+	case Q_DST:
+		b1 = gen_mcmp(odst_off, BPF_W, addr, mask);
+		gen_and(bo, b1);
+		b0 = gen_mcmp(idst_off, BPF_W, addr, mask);
+		gen_and(bi, b0);
+		gen_or(b0, b1);
+		break;
+
+	case Q_GATEWAY:
+		/* (in && (addr == igwy1 || addr == igwy2)) ||
+		   (out && (addr == ogwy1 || addr == ogwy2))  phew! */
+		b1 = gen_mcmp(igwy1_off, BPF_W, addr, mask);
+		b0 = gen_mcmp(igwy2_off, BPF_W, addr, mask);
+		gen_or(b0, b1);
+		gen_and(bi, b1);
+		b2 = gen_mcmp(ogwy1_off, BPF_W, addr, mask);
+		b0 = gen_mcmp(ogwy2_off, BPF_W, addr, mask);
+		gen_or(b2, b0);
+		gen_and(bo, b0);
+		gen_or(b0, b1);
+		break;
+
+	case Q_AND:
+		b1 = gen_mcmp(isrc_off, BPF_W, addr, mask);
+		b0 = gen_mcmp(idst_off, BPF_W, addr, mask);
+		gen_and(b0, b1);
+		gen_and(bi, b1);
+		b2 = gen_mcmp(osrc_off, BPF_W, addr, mask);
+		b0 = gen_mcmp(odst_off, BPF_W, addr, mask);
+		gen_and(b2, b0);
+		gen_and(bo, b0);
+		gen_or(b0, b1);
+		break;
+
+	case Q_OR:
+		b1 = gen_mcmp(isrc_off, BPF_W, addr, mask);
+		b0 = gen_mcmp(idst_off, BPF_W, addr, mask);
+		gen_or(b0, b1);
+		gen_and(bi, b1);
+		b2 = gen_mcmp(osrc_off, BPF_W, addr, mask);
+		b0 = gen_mcmp(odst_off, BPF_W, addr, mask);
+		gen_or(b2, b0);
+		gen_and(bo, b0);
+		gen_or(b0, b1);
+		break;
+
+	case Q_DEFAULT:
+		b1 = gen_mcmp(isrc_off, BPF_W, addr, mask);
+		b0 = gen_mcmp(idst_off, BPF_W, addr, mask);
+		gen_or(b0, b1);
+		b0 = gen_mcmp(osrc_off, BPF_W, addr, mask);
+		gen_or(b0, b1);
+		b0 = gen_mcmp(odst_off, BPF_W, addr, mask);
+		gen_or(b0, b1);
+		break;
+
+	default:
+		sf_error("Internal error: Invalid direcion specifier: %d", dir);
+	}
+
+	b0 = gen_linktype(ETHERTYPE_IP);
+	gen_and(b0, b1);
+
+	return b1;
+}
+
+#else
+static struct block *
+gen_hostop(bpf_u_int32 addr, bpf_u_int32 mask, int dir)
+{
 	struct block *b0, *b1, *b2;
 	const static int lan_off = offsetof(pf_state_t, lan.addr.v4);
 	const static int gwy_off = offsetof(pf_state_t, gwy.addr.v4);
@@ -542,6 +640,7 @@ gen_hostop(bpf_u_int32 addr, bpf_u_int32 mask, int dir
 
 	return b1;
 }
+#endif
 
 static struct block *
 gen_hostcmp6(u_int off, u_int32_t *a, u_int32_t *m)
@@ -560,9 +659,108 @@ gen_hostcmp6(u_int off, u_int32_t *a, u_int32_t *m)
 	return b1;
 }
 
+#ifdef HAVE_PFSYNC_KEY
 static struct block *
 gen_hostop6(struct in6_addr *addr, struct in6_addr *mask, int dir)
+
 {
+	struct block *b0, *b1, *b2, *bi, *bo;
+	u_int32_t *a, *m;
+	const static int isrc_off = offsetof(pf_state_t, key[PF_SK_STACK].addr[0].v6);
+	const static int osrc_off = offsetof(pf_state_t, key[PF_SK_WIRE].addr[1].v6);
+	const static int idst_off = offsetof(pf_state_t, key[PF_SK_STACK].addr[1].v6);
+	const static int odst_off = offsetof(pf_state_t, key[PF_SK_WIRE].addr[0].v6);
+
+	const static int igwy1_off = offsetof(pf_state_t, key[PF_SK_WIRE].addr[0].v6);
+	const static int ogwy1_off = offsetof(pf_state_t, key[PF_SK_STACK].addr[1].v6);
+	const static int igwy2_off = offsetof(pf_state_t, key[PF_SK_WIRE].addr[1].v6);
+	const static int ogwy2_off = offsetof(pf_state_t, key[PF_SK_STACK].addr[0].v6);
+
+	a = (u_int32_t *)addr;
+	m = (u_int32_t *)mask;
+
+	bi = gen_cmp(offsetof(pf_state_t, direction), BPF_B, (bpf_int32)PF_IN);
+	bo = gen_cmp(offsetof(pf_state_t, direction), BPF_B, (bpf_int32)PF_OUT);
+
+	switch (dir) {
+
+	case Q_SRC:
+		b1 = gen_hostcmp6(osrc_off, a, m);
+		gen_and(bo, b1);
+		b0 = gen_hostcmp6(isrc_off, a, m);
+		gen_and(bi, b0);
+		gen_or(b0, b1);
+		break;
+
+	case Q_DST:
+		b1 = gen_hostcmp6(odst_off, a, m);
+		gen_and(bo, b1);
+		b0 = gen_hostcmp6(idst_off, a, m);
+		gen_and(bi, b0);
+		gen_or(b0, b1);
+		break;
+
+	case Q_GATEWAY:
+		/* (in && (addr == igwy1 || addr == igwy2)) ||
+		   (out && (addr == ogwy1 || addr == ogwy2))  phew! */
+		b1 = gen_hostcmp6(igwy1_off, a, m);
+		b0 = gen_hostcmp6(igwy2_off, a, m);
+		gen_or(b0, b1);
+		gen_and(bi, b1);
+		b2 = gen_hostcmp6(ogwy1_off, a, m);
+		b0 = gen_hostcmp6(ogwy2_off, a, m);
+		gen_or(b2, b0);
+		gen_and(bo, b0);
+		gen_or(b0, b1);
+		break;
+
+	case Q_AND:
+		b1 = gen_hostcmp6(isrc_off, a, m);
+		b0 = gen_hostcmp6(idst_off, a, m);
+		gen_and(b0, b1);
+		gen_and(bi, b1);
+		b2 = gen_hostcmp6(osrc_off, a, m);
+		b0 = gen_hostcmp6(odst_off, a, m);
+		gen_and(b2, b0);
+		gen_and(bo, b0);
+		gen_or(b0, b1);
+		break;
+
+	case Q_OR:
+		b1 = gen_hostcmp6(isrc_off, a, m);
+		b0 = gen_hostcmp6(idst_off, a, m);
+		gen_or(b0, b1);
+		gen_and(bi, b1);
+		b2 = gen_hostcmp6(osrc_off, a, m);
+		b0 = gen_hostcmp6(odst_off, a, m);
+		gen_or(b2, b0);
+		gen_and(bo, b0);
+		gen_or(b0, b1);
+		break;
+
+	case Q_DEFAULT:
+		b1 = gen_hostcmp6(isrc_off, a, m);
+		b0 = gen_hostcmp6(idst_off, a, m);
+		gen_or(b0, b1);
+		b0 = gen_hostcmp6(osrc_off, a, m);
+		gen_or(b0, b1);
+		b0 = gen_hostcmp6(odst_off, a, m);
+		gen_or(b0, b1);
+		break;
+
+	default:
+		sf_error("Internal error: Invalid direcion specifier: %d", dir);
+	}
+
+	b0 = gen_linktype(ETHERTYPE_IPV6);
+	gen_and(b0, b1);
+
+	return b1;
+}
+#else
+static struct block *
+gen_hostop6(struct in6_addr *addr, struct in6_addr *mask, int dir)
+{
 	struct block *b0, *b1, *b2;
 	u_int32_t *a, *m;
 
@@ -630,6 +828,7 @@ gen_hostop6(struct in6_addr *addr, struct in6_addr *ma
 	gen_and(b0, b1);
 	return b1;
 }
+#endif
 
 static const char *
 get_modifier_by_id(int id)
@@ -748,9 +947,107 @@ gen_proto_abbrev(proto)
 	return b1;
 }
 
+#ifdef HAVE_PFSYNC_KEY
 struct block *
 gen_portop(int port, int proto, int dir)
 {
+	struct block *b0, *b1, *b2, *bi, *bo;
+	const static int isrc_off = offsetof(pf_state_t, key[PF_SK_STACK].port[0]);
+	const static int osrc_off = offsetof(pf_state_t, key[PF_SK_WIRE].port[1]);
+	const static int idst_off = offsetof(pf_state_t, key[PF_SK_STACK].port[1]);
+	const static int odst_off = offsetof(pf_state_t, key[PF_SK_WIRE].port[0]);
+
+	const static int igwy1_off = offsetof(pf_state_t, key[PF_SK_WIRE].port[0]);
+	const static int ogwy1_off = offsetof(pf_state_t, key[PF_SK_STACK].port[1]);
+	const static int igwy2_off = offsetof(pf_state_t, key[PF_SK_WIRE].port[1]);
+	const static int ogwy2_off = offsetof(pf_state_t, key[PF_SK_STACK].port[0]);
+
+	port = ntohs(port);
+
+	bi = gen_cmp(offsetof(pf_state_t, direction), BPF_B, (bpf_int32)PF_IN);
+	bo = gen_cmp(offsetof(pf_state_t, direction), BPF_B, (bpf_int32)PF_OUT);
+
+	switch (dir) {
+
+	case Q_SRC:
+		b1 = gen_cmp(osrc_off, BPF_H, (bpf_int32)port);
+		gen_and(bo, b1);
+		b0 = gen_cmp(isrc_off, BPF_H, (bpf_int32)port);
+		gen_and(bi, b0);
+		gen_or(b0, b1);
+		break;
+
+	case Q_DST:
+		b1 = gen_cmp(odst_off, BPF_H, (bpf_int32)port);
+		gen_and(bo, b1);
+		b0 = gen_cmp(idst_off, BPF_H, (bpf_int32)port);
+		gen_and(bi, b0);
+		gen_or(b0, b1);
+		break;
+
+	case Q_GATEWAY:
+		/* (in && (addr == igwy1 || addr == igwy2)) ||
+		   (out && (addr == ogwy1 || addr == ogwy2))  phew! */
+		b1 = gen_cmp(igwy1_off, BPF_H, (bpf_int32)port);
+		b0 = gen_cmp(igwy2_off, BPF_H, (bpf_int32)port);
+		gen_or(b0, b1);
+		gen_and(bi, b1);
+		b2 = gen_cmp(ogwy1_off, BPF_H, (bpf_int32)port);
+		b0 = gen_cmp(ogwy2_off, BPF_H, (bpf_int32)port);
+		gen_or(b2, b0);
+		gen_and(bo, b0);
+		gen_or(b0, b1);
+		break;
+
+	case Q_AND:
+		b1 = gen_cmp(isrc_off, BPF_H, (bpf_int32)port);
+		b0 = gen_cmp(idst_off, BPF_H, (bpf_int32)port);
+		gen_and(b0, b1);
+		gen_and(bi, b1);
+		b2 = gen_cmp(osrc_off, BPF_H, (bpf_int32)port);
+		b0 = gen_cmp(odst_off, BPF_H, (bpf_int32)port);
+		gen_and(b2, b0);
+		gen_and(bo, b0);
+		gen_or(b0, b1);
+		break;
+
+	case Q_OR:
+		b1 = gen_cmp(isrc_off, BPF_H, (bpf_int32)port);
+		b0 = gen_cmp(idst_off, BPF_H, (bpf_int32)port);
+		gen_or(b0, b1);
+		gen_and(bi, b1);
+		b2 = gen_cmp(osrc_off, BPF_H, (bpf_int32)port);
+		b0 = gen_cmp(odst_off, BPF_H, (bpf_int32)port);
+		gen_or(b2, b0);
+		gen_and(bo, b0);
+		gen_or(b0, b1);
+		break;
+
+	case Q_DEFAULT:
+		b1 = gen_cmp(isrc_off, BPF_H, (bpf_int32)port);
+		b0 = gen_cmp(idst_off, BPF_H, (bpf_int32)port);
+		gen_or(b0, b1);
+		b0 = gen_cmp(osrc_off, BPF_H, (bpf_int32)port);
+		gen_or(b0, b1);
+		b0 = gen_cmp(odst_off, BPF_H, (bpf_int32)port);
+		gen_or(b0, b1);
+		break;
+
+	default:
+		sf_error("Internal error: Invalid direcion specifier: %d", dir);
+	}
+
+
+
+	b0 = gen_proto(proto);
+	gen_and(b0, b1);
+
+	return b1;
+}
+#else
+struct block *
+gen_portop(int port, int proto, int dir)
+{
 	struct block *b0, *b1, *b2;
 	const static int lan_off = offsetof(pf_state_t, lan.port);
 	const static int gwy_off = offsetof(pf_state_t, gwy.port);
@@ -815,6 +1112,7 @@ gen_portop(int port, int proto, int dir)
 
 	return b1;
 }
+#endif
 
 static struct block *
 gen_port(int port, int ip_proto, int dir)
 |