Projet

Général

Profil

Télécharger (38,1 ko) Statistiques
| Branche: | Révision:

univnautes-tools / patches / stable / 10 / CP_speedup.diff @ ab686552

1
diff --git a/sbin/ipfw/ipfw2.c b/sbin/ipfw/ipfw2.c
2
index 577d644..83207c5 100644
3
--- a/sbin/ipfw/ipfw2.c
4
+++ b/sbin/ipfw/ipfw2.c
5
@@ -4115,8 +4115,9 @@ ipfw_flush(int force)
6
 }
7
 
8
 
9
+static void table_list_entry(ipfw_table_xentry *);
10
 static void table_list(uint16_t num, int need_header);
11
-static void table_fill_xentry(char *arg, ipfw_table_xentry *xent);
12
+static void table_fill_xentry(int ac, char *av[], ipfw_table_xentry *xent);
13
 
14
 /*
15
  * This one handles all table-related commands
16
@@ -4169,29 +4170,9 @@ ipfw_table_handler(int ac, char *av[])
17
 	if (_substrcmp(*av, "add") == 0 ||
18
 	    _substrcmp(*av, "delete") == 0) {
19
 		do_add = **av == 'a';
20
-		ac--; av++;
21
-		if (!ac)
22
-			errx(EX_USAGE, "address required");
23
 
24
-		table_fill_xentry(*av, &xent);
25
+		table_fill_xentry(ac, av, &xent);
26
 
27
-		ac--; av++;
28
-		if (do_add && ac) {
29
-			unsigned int tval;
30
-			/* isdigit is a bit of a hack here.. */
31
-			if (strchr(*av, (int)'.') == NULL && isdigit(**av))  {
32
-				xent.value = strtoul(*av, NULL, 0);
33
-			} else {
34
-				if (lookup_host(*av, (struct in_addr *)&tval) == 0) {
35
-					/* The value must be stored in host order	 *
36
-					 * so that the values < 65k can be distinguished */
37
-		       			xent.value = ntohl(tval);
38
-				} else {
39
-					errx(EX_NOHOST, "hostname ``%s'' unknown", *av);
40
-				}
41
-			}
42
-		} else
43
-			xent.value = 0;
44
 		if (do_setcmd3(do_add ? IP_FW_TABLE_XADD : IP_FW_TABLE_XDEL,
45
 		    &xent, xent.len) < 0) {
46
 			/* If running silent, don't bomb out on these errors. */
47
@@ -4218,19 +4199,41 @@ ipfw_table_handler(int ac, char *av[])
48
 		do {
49
 			table_list(xent.tbl, is_all);
50
 		} while (++xent.tbl < a);
51
+	} else if (_substrcmp(*av, "entrystats") == 0) {
52
+		table_fill_xentry(ac, av, &xent);
53
+
54
+		if (do_setcmd3(IP_FW_TABLE_XLISTENTRY, &xent, xent.len) < 0) {
55
+			/* If running silent, don't bomb out on these errors. */
56
+			if (!(co.do_quiet))
57
+				err(EX_OSERR, "setsockopt(IP_FW_TABLE_XLISTENTRY)");
58
+		} else
59
+			table_list_entry(&xent);
60
+	} else if (_substrcmp(*av, "entryzerostats") == 0) {
61
+		table_fill_xentry(ac, av, &xent);
62
+
63
+		if (do_setcmd3(IP_FW_TABLE_XZEROENTRY, &xent, xent.len) < 0) {
64
+			/* If running silent, don't bomb out on these errors. */
65
+			if (!(co.do_quiet))
66
+				err(EX_OSERR, "setsockopt(IP_FW_TABLE_XZEROENTRY)");
67
+		}
68
 	} else
69
 		errx(EX_USAGE, "invalid table command %s", *av);
70
 }
71
 
72
 static void
73
-table_fill_xentry(char *arg, ipfw_table_xentry *xent)
74
+table_fill_xentry(int ac, char *av[], ipfw_table_xentry *xent)
75
 {
76
-	int addrlen, mask, masklen, type;
77
+	int addrlen, mask, masklen, type, do_add;
78
 	struct in6_addr *paddr;
79
 	uint32_t *pkey;
80
-	char *p;
81
+	char *p, *arg;
82
 	uint32_t key;
83
 
84
+	do_add = **av == 'a';
85
+	ac--; av++;
86
+	if (!ac)
87
+		errx(EX_USAGE, "address required");
88
+
89
 	mask = 0;
90
 	type = 0;
91
 	addrlen = 0;
92
@@ -4245,7 +4248,20 @@ table_fill_xentry(char *arg, ipfw_table_xentry *xent)
93
 	 * 4) port, uid/gid or other u32 key (base 10 format)
94
 	 * 5) hostname
95
 	 */
96
-	paddr = &xent->k.addr6;
97
+	if (ac > 1 && av && _substrcmp(*av, "mac") == 0) {
98
+		uint8_t _mask[8];
99
+
100
+		type = IPFW_TABLE_MIX;
101
+		get_mac_addr_mask(av[1], (uint8_t *)xent->k.mix.mac, _mask);
102
+		ac-=2; av+=2;
103
+		if (ac <= 0)
104
+			errx(EX_DATAERR, "wrong argument passed.");
105
+
106
+		paddr = (struct in6_addr *)&xent->k.mix.addr;
107
+	} else
108
+		paddr = &xent->k.addr6;
109
+
110
+	arg = *av;
111
 	if (ishexnumber(*arg) != 0 || *arg == ':') {
112
 		/* Remove / if exists */
113
 		if ((p = strchr(arg, '/')) != NULL) {
114
@@ -4258,9 +4274,11 @@ table_fill_xentry(char *arg, ipfw_table_xentry *xent)
115
 				errx(EX_DATAERR, "bad IPv4 mask width: %s",
116
 				    p + 1);
117
 
118
-			type = IPFW_TABLE_CIDR;
119
+			if (type == 0)
120
+				type = IPFW_TABLE_CIDR;
121
 			masklen = p ? mask : 32;
122
 			addrlen = sizeof(struct in_addr);
123
+			xent->flags = IPFW_TCF_INET;
124
 		} else if (inet_pton(AF_INET6, arg, paddr) == 1) {
125
 			if (IN6_IS_ADDR_V4COMPAT(paddr))
126
 				errx(EX_DATAERR,
127
@@ -4269,10 +4287,14 @@ table_fill_xentry(char *arg, ipfw_table_xentry *xent)
128
 				errx(EX_DATAERR, "bad IPv6 mask width: %s",
129
 				    p + 1);
130
 
131
-			type = IPFW_TABLE_CIDR;
132
+			if (type == 0)
133
+				type = IPFW_TABLE_CIDR;
134
 			masklen = p ? mask : 128;
135
 			addrlen = sizeof(struct in6_addr);
136
 		} else {
137
+			if (type != 0 && type != IPFW_TABLE_MIX)
138
+				errx(EX_DATAERR, "Wrong value passed as address");
139
+
140
 			/* Port or any other key */
141
 			key = strtol(arg, &p, 10);
142
 			/* Skip non-base 10 entries like 'fa1' */
143
@@ -4302,11 +4324,105 @@ table_fill_xentry(char *arg, ipfw_table_xentry *xent)
144
 		masklen = 32;
145
 		type = IPFW_TABLE_CIDR;
146
 		addrlen = sizeof(struct in_addr);
147
+		xent->flags = IPFW_TCF_INET;
148
+	}
149
+
150
+	ac--; av++;
151
+	if (ac > 1 && av) {
152
+		if (_substrcmp(*av, "mac") == 0)  {
153
+			uint8_t _mask[8];
154
+
155
+			if (type == 0)
156
+				type = IPFW_TABLE_CIDR;
157
+			get_mac_addr_mask(av[1], (uint8_t *)&xent->mac_addr, _mask);
158
+			ac-=2; av+=2;
159
+		}
160
 	}
161
 
162
+	if (do_add && ac > 0) {
163
+		unsigned int tval;
164
+		/* isdigit is a bit of a hack here.. */
165
+		if (strchr(*av, (int)'.') == NULL && isdigit(**av))  {
166
+			xent->value = strtoul(*av, NULL, 0);
167
+		} else {
168
+			if (lookup_host(*av, (struct in_addr *)&tval) == 0) {
169
+				/* The value must be stored in host order	 *
170
+				 * so that the values < 65k can be distinguished */
171
+				xent->value = ntohl(tval);
172
+			} else {
173
+				errx(EX_NOHOST, "hostname ``%s'' unknown", *av);
174
+			}
175
+		}
176
+	} else
177
+		xent->value = 0;
178
+
179
 	xent->type = type;
180
 	xent->masklen = masklen;
181
-	xent->len = offsetof(ipfw_table_xentry, k) + addrlen;
182
+	if (type == IPFW_TABLE_MIX)
183
+		xent->len = offsetof(ipfw_table_xentry, k) + addrlen + ETHER_ADDR_LEN;
184
+	else
185
+		xent->len = offsetof(ipfw_table_xentry, k) + addrlen;
186
+}
187
+
188
+static void
189
+table_list_entry(ipfw_table_xentry *xent)
190
+{
191
+	struct in6_addr *addr6;
192
+	uint32_t tval;
193
+	char tbuf[128];
194
+
195
+	switch (xent->type) {
196
+	case IPFW_TABLE_CIDR:
197
+		/* IPv4 or IPv6 prefixes */
198
+		tval = xent->value;
199
+		addr6 = &xent->k.addr6;
200
+
201
+		if ((xent->flags & IPFW_TCF_INET) != 0) {
202
+			/* IPv4 address */
203
+			inet_ntop(AF_INET, &addr6->s6_addr32[3], tbuf, sizeof(tbuf));
204
+		} else {
205
+			/* IPv6 address */
206
+			inet_ntop(AF_INET6, addr6, tbuf, sizeof(tbuf));
207
+		}
208
+
209
+		if (co.do_value_as_ip) {
210
+			tval = htonl(tval);
211
+			printf("%s/%u %s %d %d %u\n", tbuf, xent->masklen,
212
+			    inet_ntoa(*(struct in_addr *)&tval), pr_u64(&xent->packets, 0), pr_u64(&xent->bytes, 0), xent->timestamp);
213
+		} else
214
+			printf("%s/%u %u %d %d %u\n", tbuf, xent->masklen, tval,
215
+			    pr_u64(&xent->packets, 0), pr_u64(&xent->bytes, 0), xent->timestamp);
216
+		break;
217
+	case IPFW_TABLE_INTERFACE:
218
+		/* Interface names */
219
+		tval = xent->value;
220
+		if (co.do_value_as_ip) {
221
+			tval = htonl(tval);
222
+			printf("%s %u %s %d %d %u\n", xent->k.iface, xent->masklen,
223
+			    inet_ntoa(*(struct in_addr *)&tval), pr_u64(&xent->packets, 0), pr_u64(&xent->bytes, 0), xent->timestamp);
224
+		} else
225
+			printf("%s %u %u %d %d %u\n", xent->k.iface, xent->masklen, tval,
226
+			    pr_u64(&xent->packets, 0), pr_u64(&xent->bytes, 0), xent->timestamp);
227
+
228
+		break;
229
+
230
+	case IPFW_TABLE_MIX:
231
+		/* mix of ip+mac */
232
+		tval = xent->value;
233
+
234
+		/* IPv4 address */
235
+		inet_ntop(AF_INET, &xent->k.mix.addr, tbuf, sizeof(tbuf));
236
+
237
+		if (co.do_value_as_ip) {
238
+			tval = htonl(tval);
239
+			printf("%s/%u %s %s %d %d %u\n", tbuf, xent->masklen - (8 * ETHER_ADDR_LEN), ether_ntoa((struct ether_addr *)xent->k.mix.mac),
240
+			    inet_ntoa(*(struct in_addr *)&tval),
241
+			    pr_u64(&xent->packets, 0), pr_u64(&xent->bytes, 0), xent->timestamp);
242
+		} else
243
+			printf("%s/%u %s %u %d %d %u\n", tbuf, xent->masklen - (8 * ETHER_ADDR_LEN), ether_ntoa((struct ether_addr *)xent->k.mix.mac), tval,
244
+			    pr_u64(&xent->packets, 0), pr_u64(&xent->bytes, 0), xent->timestamp);
245
+		break;
246
+	}
247
 }
248
 
249
 static void
250
@@ -4338,6 +4454,7 @@ table_list(uint16_t num, int need_header)
251
 	l = *a;
252
 	tbl = safe_calloc(1, l);
253
 	tbl->opheader.opcode = IP_FW_TABLE_XLIST;
254
+	tbl->opheader.ctxid = co.ctx;
255
 	tbl->tbl = num;
256
 	if (do_cmd(IP_FW3, tbl, (uintptr_t)&l) < 0)
257
 		err(EX_OSERR, "getsockopt(IP_FW_TABLE_XLIST)");
258
@@ -4352,8 +4469,7 @@ table_list(uint16_t num, int need_header)
259
 			tval = xent->value;
260
 			addr6 = &xent->k.addr6;
261
 
262
-
263
-			if (IN6_IS_ADDR_V4COMPAT(addr6)) {
264
+			if ((xent->flags & IPFW_TCF_INET) != 0) {
265
 				/* IPv4 address */
266
 				inet_ntop(AF_INET, &addr6->s6_addr32[3], tbuf, sizeof(tbuf));
267
 			} else {
268
@@ -4361,12 +4477,16 @@ table_list(uint16_t num, int need_header)
269
 				inet_ntop(AF_INET6, addr6, tbuf, sizeof(tbuf));
270
 			}
271
 
272
+			printf("%s/%u", tbuf, xent->masklen);
273
+			if (xent->mac_addr)
274
+				printf(" mac %s", ether_ntoa((struct ether_addr *)&xent->mac_addr));
275
+
276
 			if (co.do_value_as_ip) {
277
 				tval = htonl(tval);
278
-				printf("%s/%u %s\n", tbuf, xent->masklen,
279
+				printf(" %s\n",
280
 				    inet_ntoa(*(struct in_addr *)&tval));
281
 			} else
282
-				printf("%s/%u %u\n", tbuf, xent->masklen, tval);
283
+				printf(" %u\n", tval);
284
 			break;
285
 		case IPFW_TABLE_INTERFACE:
286
 			/* Interface names */
287
@@ -4377,6 +4497,23 @@ table_list(uint16_t num, int need_header)
288
 				    inet_ntoa(*(struct in_addr *)&tval));
289
 			} else
290
 				printf("%s %u\n", xent->k.iface, tval);
291
+
292
+			break;
293
+
294
+		case IPFW_TABLE_MIX:
295
+			/* mix of ip+mac */
296
+			tval = xent->value;
297
+
298
+			/* IPv4 address */
299
+			inet_ntop(AF_INET, &xent->k.mix.addr, tbuf, sizeof(tbuf));
300
+
301
+			if (co.do_value_as_ip) {
302
+				tval = htonl(tval);
303
+				printf("%s/%u %s %s\n", tbuf, xent->masklen - (8 * ETHER_ADDR_LEN), ether_ntoa((struct ether_addr *)xent->k.mix.mac),
304
+				    inet_ntoa(*(struct in_addr *)&tval));
305
+			} else
306
+				printf("%s/%u %s %u\n", tbuf, xent->masklen - (8 * ETHER_ADDR_LEN), ether_ntoa((struct ether_addr *)xent->k.mix.mac), tval);
307
+			break;
308
 		}
309
 
310
 		if (sz < xent->len)
311
diff --git a/sys/net/if_ethersubr.c b/sys/net/if_ethersubr.c
312
index 1a6ac08..49f678f 100644
313
--- a/sys/net/if_ethersubr.c
314
+++ b/sys/net/if_ethersubr.c
315
@@ -742,7 +742,11 @@ ether_demux(struct ifnet *ifp, struct mbuf *m)
316
 
317
 		if (i != 0 || m == NULL)
318
 			return;
319
-	}
320
+
321
+		/* M_PROTO2 is for M_IP[6]_NEXTHOP */
322
+		i = m->m_flags & (M_FASTFWD_OURS|M_PROTO2);
323
+	} else
324
+		i = 0;
325
 
326
 	eh = mtod(m, struct ether_header *);
327
 	ether_type = ntohs(eh->ether_type);
328
@@ -781,6 +785,8 @@ ether_demux(struct ifnet *ifp, struct mbuf *m)
329
 	 */
330
 	m->m_flags &= ~M_VLANTAG;
331
 	m_clrprotoflags(m);
332
+	if (i)
333
+		m->m_flags |= M_FASTFWD_OURS|M_PROTO2;
334
 	m_adj(m, ETHER_HDR_LEN);
335
 
336
 	/*
337
diff --git a/sys/netinet/ip_fw.h b/sys/netinet/ip_fw.h
338
index 14b08f5..b514ee3 100644
339
--- a/sys/netinet/ip_fw.h
340
+++ b/sys/netinet/ip_fw.h
341
@@ -74,6 +74,8 @@ typedef struct _ip_fw3_opheader {
342
 #define	IP_FW_TABLE_XDEL	87	/* delete entry */
343
 #define	IP_FW_TABLE_XGETSIZE	88	/* get table size */
344
 #define	IP_FW_TABLE_XLIST	89	/* list table contents */
345
+#define	IP_FW_TABLE_XLISTENTRY	90	/* list one table entry contents */
346
+#define	IP_FW_TABLE_XZEROENTRY	91	/* zero one table entry stats */
347
 
348
 /*
349
  * The kernel representation of ipfw rules is made of a list of
350
@@ -600,13 +602,16 @@ struct _ipfw_dyn_rule {
351
 
352
 #define	IPFW_TABLE_CIDR		1	/* Table for holding IPv4/IPv6 prefixes */
353
 #define	IPFW_TABLE_INTERFACE	2	/* Table for holding interface names */
354
-#define	IPFW_TABLE_MAXTYPE	2	/* Maximum valid number */
355
+#define	IPFW_TABLE_MIX		3	/* Table for holding IPv4/mac entries */
356
+#define	IPFW_TABLE_MAC		4	/* Table for holding mac entries */
357
+#define	IPFW_TABLE_MAXTYPE	5	/* Maximum valid number */
358
 
359
 typedef struct	_ipfw_table_entry {
360
 	in_addr_t	addr;		/* network address		*/
361
 	u_int32_t	value;		/* value			*/
362
 	u_int16_t	tbl;		/* table number			*/
363
 	u_int8_t	masklen;	/* mask length			*/
364
+	uint64_t	mac_addr;
365
 } ipfw_table_entry;
366
 
367
 typedef struct	_ipfw_table_xentry {
368
@@ -614,13 +619,32 @@ typedef struct	_ipfw_table_xentry {
369
 	uint8_t		type;		/* entry type			*/
370
 	uint8_t		masklen;	/* mask length			*/
371
 	uint16_t	tbl;		/* table number			*/
372
+	uint16_t	flags;		/* record flags			*/
373
 	uint32_t	value;		/* value			*/
374
+	uint32_t	timestamp;
375
+	uint64_t	mac_addr;
376
+	uint64_t	bytes;
377
+	uint64_t	packets;
378
 	union {
379
 		/* Longest field needs to be aligned by 4-byte boundary	*/
380
+#ifndef ETHER_ADDR_LEN
381
+#define ETHER_ADDR_LEN 6
382
+#endif
383
+#if 0
384
+		struct {
385
+			struct ether_addr addr;
386
+			struct ether_addr mask;
387
+		} mac;
388
+#endif
389
+		struct {
390
+			struct in_addr addr;
391
+			u_char mac[ETHER_ADDR_LEN];
392
+		} mix;
393
 		struct in6_addr	addr6;	/* IPv6 address 		*/
394
 		char	iface[IF_NAMESIZE];	/* interface name	*/
395
 	} k;
396
 } ipfw_table_xentry;
397
+#define	IPFW_TCF_INET	0x01	 /* CIDR flags: IPv4 record	*/
398
 
399
 typedef struct	_ipfw_table {
400
 	u_int32_t	size;		/* size of entries in bytes	*/
401
diff --git a/sys/netpfil/ipfw/ip_fw2.c b/sys/netpfil/ipfw/ip_fw2.c
402
index cd466bd..f2f117e 100644
403
--- a/sys/netpfil/ipfw/ip_fw2.c
404
+++ b/sys/netpfil/ipfw/ip_fw2.c
405
@@ -358,8 +358,8 @@ iface_match(struct ifnet *ifp, ipfw_insn_if *cmd, struct ip_fw_chain *chain, uin
406
 	/* Check by name or by IP address */
407
 	if (cmd->name[0] != '\0') { /* match by name */
408
 		if (cmd->name[0] == '\1') /* use tablearg to match */
409
-			return ipfw_lookup_table_extended(chain, cmd->p.glob,
410
-				ifp->if_xname, tablearg, IPFW_TABLE_INTERFACE);
411
+			return (ipfw_lookup_table_extended(chain, cmd->p.glob,
412
+				ifp->if_xname, tablearg, IPFW_TABLE_INTERFACE, NULL) != NULL);
413
 		/* Check name */
414
 		if (cmd->p.glob) {
415
 			if (fnmatch(cmd->name, ifp->if_xname, 0) == 0)
416
@@ -955,6 +955,7 @@ ipfw_chk(struct ip_fw_args *args)
417
 	int dyn_dir = MATCH_UNKNOWN;
418
 	ipfw_dyn_rule *q = NULL;
419
 	struct ip_fw_chain *chain = &V_layer3_chain;
420
+	void *tblent = NULL;
421
 
422
 	/*
423
 	 * We store in ulp a pointer to the upper layer protocol header.
424
@@ -1288,6 +1289,7 @@ do {								\
425
 			continue;
426
 
427
 		skip_or = 0;
428
+		tblent = NULL;
429
 		for (l = f->cmd_len, cmd = f->cmd ; l > 0 ;
430
 		    l -= cmdlen, cmd += cmdlen) {
431
 			int match;
432
@@ -1402,7 +1404,7 @@ do {								\
433
 				break;
434
 
435
 			case O_IN:	/* "out" is "not in" */
436
-				match = (oif == NULL);
437
+				match = (args->dir == DIR_IN);
438
 				break;
439
 
440
 			case O_LAYER2:
441
@@ -1438,11 +1440,18 @@ do {								\
442
 			case O_IP_SRC_LOOKUP:
443
 			case O_IP_DST_LOOKUP:
444
 				if (is_ipv4) {
445
+					struct ether_addr *ea = NULL;
446
+
447
 				    uint32_t key =
448
 					(cmd->opcode == O_IP_DST_LOOKUP) ?
449
 					    dst_ip.s_addr : src_ip.s_addr;
450
 				    uint32_t v = 0;
451
 
452
+					if (args->eh) {
453
+						ea = (struct ether_addr*)((cmd->opcode == O_IP_DST_LOOKUP) ?
454
+							args->eh->ether_dhost :
455
+							args->eh->ether_shost);
456
+					}
457
 				    if (cmdlen > F_INSN_SIZE(ipfw_insn_u32)) {
458
 					/* generic lookup. The key must be
459
 					 * in 32bit big-endian format.
460
@@ -1484,22 +1493,37 @@ do {								\
461
 					} else
462
 					    break;
463
 				    }
464
-				    match = ipfw_lookup_table(chain,
465
-					cmd->arg1, key, &v);
466
-				    if (!match)
467
+				    tblent = ipfw_lookup_table_extended(chain,
468
+					cmd->arg1, &key, &v, IPFW_TABLE_CIDR, ea);
469
+				    if (tblent == NULL) {
470
+					match = 0;
471
 					break;
472
+				    } else
473
+					match = 1;
474
 				    if (cmdlen == F_INSN_SIZE(ipfw_insn_u32))
475
 					match =
476
 					    ((ipfw_insn_u32 *)cmd)->d[0] == v;
477
-				    else
478
+				    if (match)
479
 					tablearg = v;
480
 				} else if (is_ipv6) {
481
+					struct ether_addr *ea = NULL;
482
 					uint32_t v = 0;
483
+
484
+					if (args->eh) {
485
+						ea = (struct ether_addr*)((cmd->opcode == O_IP_DST_LOOKUP) ?
486
+							args->eh->ether_dhost :
487
+							args->eh->ether_shost);
488
+					}
489
 					void *pkey = (cmd->opcode == O_IP_DST_LOOKUP) ?
490
 						&args->f_id.dst_ip6: &args->f_id.src_ip6;
491
-					match = ipfw_lookup_table_extended(chain,
492
+					tblent = ipfw_lookup_table_extended(chain,
493
 							cmd->arg1, pkey, &v,
494
-							IPFW_TABLE_CIDR);
495
+							IPFW_TABLE_CIDR, ea);
496
+				    if (tblent == NULL) {
497
+					match = 0;
498
+					break;
499
+				    } else
500
+					match = 1;
501
 					if (cmdlen == F_INSN_SIZE(ipfw_insn_u32))
502
 						match = ((ipfw_insn_u32 *)cmd)->d[0] == v;
503
 					if (match)
504
@@ -2314,8 +2338,7 @@ do {								\
505
 				break;
506
 
507
 			case O_FORWARD_IP:
508
-				if (args->eh)	/* not valid on layer2 pkts */
509
-					break;
510
+				if (!args->eh)	{/* not valid on layer2 pkts */
511
 				if (q == NULL || q->rule != f ||
512
 				    dyn_dir == MATCH_FORWARD) {
513
 				    struct sockaddr_in *sa;
514
@@ -2330,6 +2353,48 @@ do {								\
515
 					args->next_hop = sa;
516
 				    }
517
 				}
518
+				} else if (args->eh) {
519
+					struct m_tag *fwd_tag;
520
+					struct sockaddr_in *sa;
521
+					u_short sum;
522
+
523
+					/*
524
+					* Checksum correct? (from ip_fastfwd.c)
525
+					*/
526
+					if (m->m_pkthdr.csum_flags & CSUM_IP_CHECKED)
527
+						sum = !(m->m_pkthdr.csum_flags & CSUM_IP_VALID);
528
+					else {
529
+						if (hlen == sizeof(struct ip))
530
+							sum = in_cksum_hdr(ip);
531
+						else
532
+							sum = in_cksum(m, hlen);
533
+					}
534
+					if (sum) {
535
+						IPSTAT_INC(ips_badsum);
536
+						retval = IP_FW_DENY;
537
+						break;
538
+					}
539
+
540
+					/*
541
+					* Remember that we have checked the IP header and found it valid.
542
+					*/
543
+					m->m_pkthdr.csum_flags |= (CSUM_IP_CHECKED | CSUM_IP_VALID);
544
+
545
+					sa = &(((ipfw_insn_sa *)cmd)->sa);
546
+					fwd_tag = m_tag_get(PACKET_TAG_IPFORWARD,
547
+						sizeof(struct sockaddr_in), M_NOWAIT);
548
+					if (fwd_tag == NULL)
549
+						retval = IP_FW_DENY;
550
+					else {
551
+						bcopy(sa, (fwd_tag+1), sizeof(struct sockaddr_in));
552
+						m_tag_prepend(m, fwd_tag);
553
+
554
+						if (in_localip(sa->sin_addr))
555
+							m->m_flags |= M_FASTFWD_OURS;
556
+						m->m_flags |= M_IP_NEXTHOP;
557
+					}
558
+				}
559
+
560
 				retval = IP_FW_PASS;
561
 				l = 0;          /* exit inner loop */
562
 				done = 1;       /* exit outer loop */
563
@@ -2337,8 +2402,7 @@ do {								\
564
 
565
 #ifdef INET6
566
 			case O_FORWARD_IP6:
567
-				if (args->eh)	/* not valid on layer2 pkts */
568
-					break;
569
+				if (!args->eh) {/* not valid on layer2 pkts */
570
 				if (q == NULL || q->rule != f ||
571
 				    dyn_dir == MATCH_FORWARD) {
572
 					struct sockaddr_in6 *sin6;
573
@@ -2346,6 +2410,24 @@ do {								\
574
 					sin6 = &(((ipfw_insn_sa6 *)cmd)->sa);
575
 					args->next_hop6 = sin6;
576
 				}
577
+				} else if (args->eh) {
578
+					struct m_tag *fwd_tag;
579
+					struct sockaddr_in6 *sin6;
580
+
581
+					sin6 = &(((ipfw_insn_sa6 *)cmd)->sa);
582
+					fwd_tag = m_tag_get(PACKET_TAG_IPFORWARD,
583
+						sizeof(struct sockaddr_in6), M_NOWAIT);
584
+					if (fwd_tag == NULL)
585
+						retval = IP_FW_DENY;
586
+					else {
587
+						bcopy(sin6, (fwd_tag+1), sizeof(struct sockaddr_in6));
588
+						m_tag_prepend(m, fwd_tag);
589
+
590
+						if (in6_localip(&sin6->sin6_addr))
591
+							m->m_flags |= M_FASTFWD_OURS;
592
+						m->m_flags |= M_IP6_NEXTHOP;
593
+					}
594
+				}
595
 				retval = IP_FW_PASS;
596
 				l = 0;		/* exit inner loop */
597
 				done = 1;	/* exit outer loop */
598
@@ -2505,6 +2587,8 @@ do {								\
599
 		struct ip_fw *rule = chain->map[f_pos];
600
 		/* Update statistics */
601
 		IPFW_INC_RULE_COUNTER(rule, pktlen);
602
+		if (tblent != NULL)
603
+			ipfw_count_table_xentry_stats(tblent, pktlen);
604
 	} else {
605
 		retval = IP_FW_DENY;
606
 		printf("ipfw: ouch!, skip past end of rules, denying packet\n");
607
diff --git a/sys/netpfil/ipfw/ip_fw_pfil.c b/sys/netpfil/ipfw/ip_fw_pfil.c
608
index d1202ff..bf225b8 100644
609
--- a/sys/netpfil/ipfw/ip_fw_pfil.c
610
+++ b/sys/netpfil/ipfw/ip_fw_pfil.c
611
@@ -143,8 +143,9 @@ again:
612
 	}
613
 
614
 	args.m = *m0;
615
-	args.oif = dir == DIR_OUT ? ifp : NULL;
616
+	args.oif = ifp;
617
 	args.inp = inp;
618
+	args.dir = dir;
619
 
620
 	ipfw = ipfw_chk(&args);
621
 	*m0 = args.m;
622
@@ -314,9 +315,8 @@ ipfw_check_frame(void *arg, struct mbuf **m0, struct ifnet *dst, int dir,
623
 		/* XXX can we free it after use ? */
624
 		mtag->m_tag_id = PACKET_TAG_NONE;
625
 		r = (struct ipfw_rule_ref *)(mtag + 1);
626
-		if (r->info & IPFW_ONEPASS)
627
-			return (0);
628
-		args.rule = *r;
629
+		m_tag_delete(*m0, mtag);
630
+		return (0);
631
 	}
632
 
633
 	/* I need some amt of data to be contiguous */
634
@@ -333,12 +333,15 @@ ipfw_check_frame(void *arg, struct mbuf **m0, struct ifnet *dst, int dir,
635
 	save_eh = *eh;			/* save copy for restore below */
636
 	m_adj(m, ETHER_HDR_LEN);	/* strip ethernet header */
637
 
638
+	dir = dir == PFIL_IN ? DIR_IN : DIR_OUT;
639
+
640
 	args.m = m;		/* the packet we are looking at		*/
641
-	args.oif = dir == PFIL_OUT ? dst: NULL;	/* destination, if any	*/
642
+	args.oif = dst;		/* destination, if any	*/
643
 	args.next_hop = NULL;	/* we do not support forward yet	*/
644
 	args.next_hop6 = NULL;	/* we do not support forward yet	*/
645
 	args.eh = &save_eh;	/* MAC header for bridged/MAC packets	*/
646
 	args.inp = NULL;	/* used by ipfw uid/gid/jail rules	*/
647
+	args.dir = dir;         /* pfSense addition                     */
648
 	i = ipfw_chk(&args);
649
 	m = args.m;
650
 	if (m != NULL) {
651
@@ -369,13 +372,12 @@ ipfw_check_frame(void *arg, struct mbuf **m0, struct ifnet *dst, int dir,
652
 
653
 	case IP_FW_DUMMYNET:
654
 		ret = EACCES;
655
-		int dir;
656
 
657
 		if (ip_dn_io_ptr == NULL)
658
 			break; /* i.e. drop */
659
 
660
 		*m0 = NULL;
661
-		dir = PROTO_LAYER2 | (dst ? DIR_OUT : DIR_IN);
662
+		dir = PROTO_LAYER2 | dir;
663
 		ip_dn_io_ptr(&m, dir, &args);
664
 		return 0;
665
 
666
diff --git a/sys/netpfil/ipfw/ip_fw_private.h b/sys/netpfil/ipfw/ip_fw_private.h
667
index a8d7eea..4830124 100644
668
--- a/sys/netpfil/ipfw/ip_fw_private.h
669
+++ b/sys/netpfil/ipfw/ip_fw_private.h
670
@@ -101,6 +101,7 @@ struct ip_fw_args {
671
 
672
 	struct ipfw_flow_id f_id;	/* grabbed from IP header	*/
673
 	//uint32_t	cookie;		/* a cookie depending on rule action */
674
+	uint32_t        dir;            /* direction */
675
 	struct inpcb	*inp;
676
 
677
 	struct _ip6dn_args	dummypar; /* dummynet->ip6_output */
678
@@ -304,13 +305,17 @@ void ipfw_reap_rules(struct ip_fw *head);
679
 struct radix_node;
680
 int ipfw_lookup_table(struct ip_fw_chain *ch, uint16_t tbl, in_addr_t addr,
681
     uint32_t *val);
682
-int ipfw_lookup_table_extended(struct ip_fw_chain *ch, uint16_t tbl, void *paddr,
683
-    uint32_t *val, int type);
684
+struct ether_addr;
685
+void *ipfw_lookup_table_extended(struct ip_fw_chain *ch, uint16_t tbl, void *paddr,
686
+    uint32_t *val, int type, struct ether_addr *);
687
+void ipfw_count_table_xentry_stats(void *, int);
688
+int ipfw_zero_table_xentry_stats(struct ip_fw_chain *, ipfw_table_xentry *);
689
+int ipfw_lookup_table_xentry(struct ip_fw_chain *, ipfw_table_xentry *);
690
 int ipfw_init_tables(struct ip_fw_chain *ch);
691
 void ipfw_destroy_tables(struct ip_fw_chain *ch);
692
 int ipfw_flush_table(struct ip_fw_chain *ch, uint16_t tbl);
693
 int ipfw_add_table_entry(struct ip_fw_chain *ch, uint16_t tbl, void *paddr,
694
-    uint8_t plen, uint8_t mlen, uint8_t type, uint32_t value);
695
+    uint8_t plen, uint8_t mlen, uint8_t type, u_int64_t mac_addr, uint32_t value);
696
 int ipfw_del_table_entry(struct ip_fw_chain *ch, uint16_t tbl, void *paddr,
697
     uint8_t plen, uint8_t mlen, uint8_t type);
698
 int ipfw_count_table(struct ip_fw_chain *ch, uint32_t tbl, uint32_t *cnt);
699
diff --git a/sys/netpfil/ipfw/ip_fw_sockopt.c b/sys/netpfil/ipfw/ip_fw_sockopt.c
700
index cb9c89c..d98cac6 100644
701
--- a/sys/netpfil/ipfw/ip_fw_sockopt.c
702
+++ b/sys/netpfil/ipfw/ip_fw_sockopt.c
703
@@ -1124,7 +1124,7 @@ ipfw_ctl(struct sockopt *sopt)
704
 				break;
705
 			error = ipfw_add_table_entry(chain, ent.tbl,
706
 			    &ent.addr, sizeof(ent.addr), ent.masklen, 
707
-			    IPFW_TABLE_CIDR, ent.value);
708
+			    IPFW_TABLE_CIDR, ent.mac_addr, ent.value);
709
 		}
710
 		break;
711
 
712
@@ -1162,7 +1162,7 @@ ipfw_ctl(struct sockopt *sopt)
713
 
714
 			error = (opt == IP_FW_TABLE_XADD) ?
715
 				ipfw_add_table_entry(chain, xent->tbl, &xent->k, 
716
-					len, xent->masklen, xent->type, xent->value) :
717
+					len, xent->masklen, xent->type, xent->mac_addr, xent->value) :
718
 				ipfw_del_table_entry(chain, xent->tbl, &xent->k,
719
 					len, xent->masklen, xent->type);
720
 		}
721
@@ -1245,6 +1245,47 @@ ipfw_ctl(struct sockopt *sopt)
722
 		}
723
 		break;
724
 
725
+	case IP_FW_TABLE_XZEROENTRY: /* IP_FW3 */
726
+		{
727
+			ipfw_table_xentry *xent = (ipfw_table_xentry *)(op3 + 1);
728
+
729
+			/* Check minimum header size */
730
+			if (IP_FW3_OPLENGTH(sopt) < offsetof(ipfw_table_xentry, k)) {
731
+				error = EINVAL;
732
+				break;
733
+			}
734
+
735
+			/* Check if len field is valid */
736
+			if (xent->len > sizeof(ipfw_table_xentry)) {
737
+				error = EINVAL;
738
+				break;
739
+			}
740
+			
741
+			error = ipfw_zero_table_xentry_stats(chain, xent);
742
+		}
743
+		break;
744
+
745
+	case IP_FW_TABLE_XLISTENTRY: /* IP_FW3 */
746
+		{
747
+			ipfw_table_xentry *xent = (ipfw_table_xentry *)(op3 + 1);
748
+
749
+			/* Check minimum header size */
750
+			if (IP_FW3_OPLENGTH(sopt) < offsetof(ipfw_table_xentry, k)) {
751
+				error = EINVAL;
752
+				break;
753
+			}
754
+
755
+			/* Check if len field is valid */
756
+			if (xent->len > sizeof(ipfw_table_xentry)) {
757
+				error = EINVAL;
758
+				break;
759
+			}
760
+			
761
+			error = ipfw_lookup_table_xentry(chain, xent);
762
+			xent->timestamp += boottime.tv_sec;
763
+		}
764
+		break;
765
+
766
 	case IP_FW_TABLE_XLIST: /* IP_FW3 */
767
 		{
768
 			ipfw_xtable *tbl;
769
diff --git a/sys/netpfil/ipfw/ip_fw_table.c b/sys/netpfil/ipfw/ip_fw_table.c
770
index 95cff5c..c1cf6f4 100644
771
--- a/sys/netpfil/ipfw/ip_fw_table.c
772
+++ b/sys/netpfil/ipfw/ip_fw_table.c
773
@@ -59,6 +59,7 @@ __FBSDID("$FreeBSD$");
774
 #include <net/route.h>
775
 #include <net/vnet.h>
776
 
777
+#include <net/ethernet.h>
778
 #include <netinet/in.h>
779
 #include <netinet/ip_var.h>	/* struct ipfw_rule_ref */
780
 #include <netinet/ip_fw.h>
781
@@ -74,7 +75,11 @@ static MALLOC_DEFINE(M_IPFW_TBL, "ipfw_tbl", "IpFw tables");
782
 struct table_entry {
783
 	struct radix_node	rn[2];
784
 	struct sockaddr_in	addr, mask;
785
+	u_int64_t               mac_addr;
786
 	u_int32_t		value;
787
+	u_int32_t               timestamp;
788
+	u_int64_t               bytes;
789
+	u_int64_t               packets;
790
 };
791
 
792
 struct xaddr_iface {
793
@@ -83,6 +88,22 @@ struct xaddr_iface {
794
 	char 		ifname[IF_NAMESIZE];	/* Interface name */
795
 };
796
 
797
+#if 0
798
+struct xaddr_mac {
799
+	uint8_t		mac_len;		/* length of this struct */
800
+	uint8_t		pad[7];		/* Align name */
801
+	struct ether_addr mac;
802
+};
803
+#endif
804
+
805
+struct xaddr_mix {
806
+	uint8_t		mix_len;		/* length of this struct */
807
+	sa_family_t     sin_family;
808
+        uint8_t		pad[6];
809
+        struct  in_addr sin_addr;
810
+	u_char	mac[ETHER_ADDR_LEN];
811
+};
812
+
813
 struct table_xentry {
814
 	struct radix_node	rn[2];
815
 	union {
816
@@ -90,14 +111,26 @@ struct table_xentry {
817
 		struct sockaddr_in6	addr6;
818
 #endif
819
 		struct xaddr_iface	iface;
820
+#if 0
821
+		struct xaddr_mac	mac;
822
+#endif
823
+		struct xaddr_mix	mix;
824
 	} a;
825
 	union {
826
 #ifdef INET6
827
 		struct sockaddr_in6	mask6;
828
 #endif
829
 		struct xaddr_iface	ifmask;
830
+#if 0
831
+		struct xaddr_mac	macmask;
832
+#endif
833
+		struct xaddr_mix	mixmask;
834
 	} m;
835
+	u_int64_t               mac_addr;
836
 	u_int32_t		value;
837
+	u_int32_t               timestamp;
838
+	u_int64_t               bytes;
839
+	u_int64_t               packets;
840
 };
841
 
842
 /*
843
@@ -117,10 +150,17 @@ struct table_xentry {
844
 #define KEY_LEN_INET	(offsetof(struct sockaddr_in, sin_addr) + sizeof(in_addr_t))
845
 #define KEY_LEN_INET6	(offsetof(struct sockaddr_in6, sin6_addr) + sizeof(struct in6_addr))
846
 #define KEY_LEN_IFACE	(offsetof(struct xaddr_iface, ifname))
847
+#define KEY_LEN_MIX	(offsetof(struct xaddr_mix, sin_addr) + sizeof(in_addr_t) + ETHER_ADDR_LEN)
848
+#if 0
849
+#define KEY_LEN_MAC	(offsetof(struct xaddr_mac, mac) + ETHER_ADDR_LEN)
850
+#endif
851
 
852
 #define OFF_LEN_INET	(8 * offsetof(struct sockaddr_in, sin_addr))
853
 #define OFF_LEN_INET6	(8 * offsetof(struct sockaddr_in6, sin6_addr))
854
 #define OFF_LEN_IFACE	(8 * offsetof(struct xaddr_iface, ifname))
855
+#if 0
856
+#define OFF_LEN_MAC	(8 * offsetof(struct xaddr_mac, mac))
857
+#endif
858
 
859
 
860
 #ifdef INET6
861
@@ -137,7 +177,7 @@ ipv6_writemask(struct in6_addr *addr6, uint8_t mask)
862
 
863
 int
864
 ipfw_add_table_entry(struct ip_fw_chain *ch, uint16_t tbl, void *paddr,
865
-    uint8_t plen, uint8_t mlen, uint8_t type, uint32_t value)
866
+    uint8_t plen, uint8_t mlen, uint8_t type, u_int64_t mac_addr, uint32_t value)
867
 {
868
 	struct radix_node_head *rnh, **rnh_ptr;
869
 	struct table_entry *ent;
870
@@ -161,6 +201,7 @@ ipfw_add_table_entry(struct ip_fw_chain *ch, uint16_t tbl, void *paddr,
871
 				return (EINVAL);
872
 			ent = malloc(sizeof(*ent), M_IPFW_TBL, M_WAITOK | M_ZERO);
873
 			ent->value = value;
874
+			ent->mac_addr = mac_addr;
875
 			/* Set 'total' structure length */
876
 			KEY_LEN(ent->addr) = KEY_LEN_INET;
877
 			KEY_LEN(ent->mask) = KEY_LEN_INET;
878
@@ -182,6 +223,7 @@ ipfw_add_table_entry(struct ip_fw_chain *ch, uint16_t tbl, void *paddr,
879
 				return (EINVAL);
880
 			xent = malloc(sizeof(*xent), M_IPFW_TBL, M_WAITOK | M_ZERO);
881
 			xent->value = value;
882
+			xent->mac_addr = mac_addr;
883
 			/* Set 'total' structure length */
884
 			KEY_LEN(xent->a.addr6) = KEY_LEN_INET6;
885
 			KEY_LEN(xent->m.mask6) = KEY_LEN_INET6;
886
@@ -233,6 +275,52 @@ ipfw_add_table_entry(struct ip_fw_chain *ch, uint16_t tbl, void *paddr,
887
 		mask_ptr = NULL;
888
 		break;
889
 
890
+#if 0
891
+	case IPFW_TABLE_MAC:
892
+		int i;
893
+
894
+		xent = malloc(sizeof(*xent), M_IPFW_TBL, M_WAITOK | M_ZERO);
895
+		xent->value = value;
896
+		/* Set 'total' structure length */
897
+		KEY_LEN(xent->a.mac) = KEY_LEN_MAC;
898
+		KEY_LEN(xent->m.macmask) = KEY_LEN_MAC;
899
+		/* Set offset of address in bits */
900
+		offset = OFF_LEN_MAC;
901
+		xent->a.mac = (struct ether_addr)(*paddr);
902
+		xent->m.mac = (struct ether_addr)(*(((struct ether_addr *)paddr) + 1));
903
+		for (i = 0; i < ETHER_ADDR_LEN; i++)
904
+			xent->a.mac.octet[i] &= xent->m.mac.octet[i];
905
+		/* Set pointers */
906
+		rnh_ptr = &ch->xtables[tbl];
907
+		ent_ptr = xent;
908
+		addr_ptr = (struct sockaddr *)&xent->a.mac;
909
+		mask_ptr = (struct sockaddr *)&xent->m.macmask;
910
+		break;
911
+#endif
912
+
913
+	case IPFW_TABLE_MIX:
914
+		if ((plen - ETHER_ADDR_LEN) != sizeof(in_addr_t))
915
+			return (EINVAL);
916
+
917
+		xent = malloc(sizeof(*xent), M_IPFW_TBL, M_WAITOK | M_ZERO);
918
+		xent->value = value;
919
+		/* Set 'total' structure length */
920
+		KEY_LEN(xent->a.mix) = KEY_LEN_MIX;
921
+		KEY_LEN(xent->m.mixmask) = KEY_LEN_MIX;
922
+		/* Set offset of IPv4 address in bits */
923
+		offset = OFF_LEN_INET;
924
+		/* XXX: Needs to be fixed */
925
+		memcpy(&xent->a.mix.sin_addr, paddr, ETHER_ADDR_LEN + sizeof(struct in_addr));
926
+		/* Only full ips /32 and full masks supported for mac */
927
+		memset(&xent->m.mixmask.sin_addr, 0xFF, sizeof(struct in_addr));
928
+		memset(xent->m.mixmask.mac, 0xFF, ETHER_ADDR_LEN);
929
+		/* Set pointers */
930
+		rnh_ptr = &ch->xtables[tbl];
931
+		ent_ptr = xent;
932
+		addr_ptr = (struct sockaddr *)&xent->a.mix;
933
+		mask_ptr = NULL;
934
+		break;
935
+
936
 	default:
937
 		return (EINVAL);
938
 	}
939
@@ -281,6 +369,28 @@ ipfw_add_table_entry(struct ip_fw_chain *ch, uint16_t tbl, void *paddr,
940
 	IPFW_WUNLOCK(ch);
941
 
942
 	if (rn == NULL) {
943
+		if (type == IPFW_TABLE_CIDR) {
944
+			/* Just update if any new value needed */
945
+			if (plen == sizeof(in_addr_t)) {
946
+				ent = (struct table_entry *)(rnh->rnh_lookup(addr_ptr, NULL, rnh));
947
+				if (ent != NULL) {
948
+					if (ent->mac_addr) {
949
+						if (!bcmp(&mac_addr, &ent->mac_addr, ETHER_ADDR_LEN))
950
+							ent->value = value;
951
+					} else
952
+						ent->value = value;
953
+				}
954
+			} else {
955
+				xent = (struct table_xentry *)(rnh->rnh_lookup(addr_ptr, NULL, rnh));
956
+				if (xent != NULL) {
957
+					if (xent->mac_addr) {
958
+						if (!bcmp(&mac_addr, &xent->mac_addr, ETHER_ADDR_LEN))
959
+							xent->value = value;
960
+					} else
961
+						xent->value = value;
962
+				}
963
+			}
964
+		}
965
 		free(ent_ptr, M_IPFW_TBL);
966
 		return (EEXIST);
967
 	}
968
@@ -367,6 +477,41 @@ ipfw_del_table_entry(struct ip_fw_chain *ch, uint16_t tbl, void *paddr,
969
 
970
 		break;
971
 
972
+#if 0
973
+	case IPFW_TABLE_MAC:
974
+		struct xaddr_mac mac, macmask;
975
+		memset(&mac, 0, sizeof(mac));
976
+		memset(&macmask, 0, sizeof(macmask));
977
+
978
+		/* Set 'total' structure length */
979
+                KEY_LEN(mac) = KEY_LEN_MAC;
980
+		mac.mac.mac = (struct ether_addr)(*paddr);
981
+                KEY_LEN(macmask) = KEY_LEN_MAC;
982
+		macmask.mac.macmask = (struct ether_addr)(*(((struct ether_addr *)paddr) + 1));
983
+		for (i = 0; i < ETHER_ADDR_LEN; i++)
984
+			mac.mac.octet[i] &= macmask.mac.octet[i];
985
+		rnh_ptr = &ch->xtables[tbl];
986
+		sa_ptr = (struct sockaddr *)&mac;
987
+		mask_ptr = (struct sockaddr *)&macmask;
988
+
989
+		break;
990
+#endif
991
+
992
+	case IPFW_TABLE_MIX:
993
+		if (mlen > (32 + ETHER_ADDR_LEN))
994
+			return (EINVAL);
995
+		struct xaddr_mix mix;
996
+		memset(&mix, 0, sizeof(mix));
997
+
998
+		/* Set 'total' structure length */
999
+		KEY_LEN(mix) = KEY_LEN_MIX;
1000
+		memcpy(&mix.sin_addr, paddr, sizeof(struct in_addr) + ETHER_ADDR_LEN);
1001
+		rnh_ptr = &ch->xtables[tbl];
1002
+		sa_ptr = (struct sockaddr *)&mix;
1003
+		mask_ptr = NULL;
1004
+
1005
+		break;
1006
+
1007
 	default:
1008
 		return (EINVAL);
1009
 	}
1010
@@ -552,9 +697,199 @@ ipfw_lookup_table(struct ip_fw_chain *ch, uint16_t tbl, in_addr_t addr,
1011
 	return (0);
1012
 }
1013
 
1014
+void
1015
+ipfw_count_table_xentry_stats(void *arg, int pktlen)
1016
+{
1017
+	ipfw_table_xentry *xent= arg;
1018
+
1019
+	xent->packets++;
1020
+	xent->bytes += pktlen;
1021
+	xent->timestamp = time_uptime;
1022
+}
1023
+
1024
 int
1025
+ipfw_zero_table_xentry_stats(struct ip_fw_chain *ch, ipfw_table_xentry *arg)
1026
+{
1027
+	struct radix_node_head *rnh;
1028
+	struct table_xentry *xent;
1029
+	struct sockaddr_in6 sa6;
1030
+	struct xaddr_iface iface;
1031
+	struct xaddr_mix xm;
1032
+
1033
+	if (arg->tbl >= V_fw_tables_max)
1034
+		return (0);
1035
+	if (ch->tables[arg->tbl] != NULL)
1036
+		rnh = ch->tables[arg->tbl];
1037
+	else if (ch->xtables[arg->tbl] != NULL)
1038
+		rnh = ch->xtables[arg->tbl];
1039
+	else
1040
+		return (0);
1041
+
1042
+	switch (arg->type) {
1043
+	case IPFW_TABLE_CIDR:
1044
+		if (ch->tables[arg->tbl] != NULL) {
1045
+			/* XXX: Maybe better by FreeBSD 11!! */
1046
+			struct sockaddr_in sa;
1047
+			struct table_entry *ent;
1048
+
1049
+			KEY_LEN(sa) = KEY_LEN_INET;
1050
+			sa.sin_addr.s_addr = *((in_addr_t *)arg->k);
1051
+			ent = (struct table_entry *)(rnh->rnh_lookup(&sa, NULL, rnh));
1052
+			if (ent == NULL)
1053
+				return (0);
1054
+
1055
+			arg->bytes = 0;
1056
+			arg->packets = 0;
1057
+			arg->value = ent->value;
1058
+			arg->timestamp = time_uptime;
1059
+
1060
+			return (1);
1061
+		} else {
1062
+			KEY_LEN(sa6) = KEY_LEN_INET6;
1063
+			memcpy(&sa6.sin6_addr, &arg->k.addr6, sizeof(struct in6_addr));
1064
+			xent = (struct table_xentry *)(rnh->rnh_lookup(&sa6, NULL, rnh));
1065
+		}
1066
+		break;
1067
+
1068
+	case IPFW_TABLE_INTERFACE:
1069
+		KEY_LEN(iface) = KEY_LEN_IFACE +
1070
+		    strlcpy(iface.ifname, arg->k.iface, IF_NAMESIZE) + 1;
1071
+		/* Assume direct match */
1072
+		/* FIXME: Add interface pattern matching */
1073
+		xent = (struct table_xentry *)(rnh->rnh_lookup(&iface, NULL, rnh));
1074
+		break;
1075
+
1076
+#if 0
1077
+	case IPFW_TABLE_MAC:
1078
+	{
1079
+		struct xaddr_mac mac;
1080
+
1081
+		KEY_LEN(mac) = KEY_LEN_MAC;
1082
+		&mac.mac = arg->k.mac;
1083
+		xent = (struct table_xentry *)(rnh->rnh_lookup(&mac, NULL, rnh));
1084
+	}
1085
+		break;
1086
+#endif
1087
+
1088
+	case IPFW_TABLE_MIX:
1089
+		KEY_LEN(xm) = KEY_LEN_MIX;
1090
+		memcpy(&xm.sin_addr, &arg->k.mix.addr, sizeof(struct in_addr));
1091
+		memcpy(&xm.mac, arg->k.mix.mac, ETHER_ADDR_LEN);
1092
+		xent = (struct table_xentry *)(rnh->rnh_lookup(&xm, NULL, rnh));
1093
+		break;
1094
+
1095
+	default:
1096
+		return (0);
1097
+	}
1098
+
1099
+	if (xent != NULL) {
1100
+		xent->bytes = 0;
1101
+		xent->packets = 0;
1102
+		xent->timestamp = time_uptime;
1103
+		
1104
+		return (1);
1105
+	}
1106
+	return (0);
1107
+}
1108
+
1109
+int
1110
+ipfw_lookup_table_xentry(struct ip_fw_chain *ch, ipfw_table_xentry *arg)
1111
+{
1112
+	struct radix_node_head *rnh;
1113
+	struct table_xentry *xent;
1114
+
1115
+	if (arg->tbl >= V_fw_tables_max)
1116
+		return (0);
1117
+	if (ch->tables[arg->tbl] != NULL)
1118
+		rnh = ch->tables[arg->tbl];
1119
+	else if (ch->xtables[arg->tbl] != NULL)
1120
+		rnh = ch->xtables[arg->tbl];
1121
+	else
1122
+		return (0);
1123
+
1124
+	switch (arg->type) {
1125
+	case IPFW_TABLE_CIDR:
1126
+	{
1127
+		if (ch->tables[arg->tbl] != NULL) {
1128
+			/* XXX: Maybe better by FreeBSD 11!! */
1129
+			struct sockaddr_in sa;
1130
+			struct table_entry *ent;
1131
+
1132
+			KEY_LEN(sa) = KEY_LEN_INET;
1133
+			sa.sin_addr.s_addr = *((in_addr_t *)arg->k);
1134
+			ent = (struct table_entry *)(rnh->rnh_lookup(&sa, NULL, rnh));
1135
+			if (ent == NULL)
1136
+				return (0);
1137
+
1138
+			arg->bytes = ent->bytes;
1139
+			arg->packets = ent->packets;
1140
+			arg->value = ent->value;
1141
+			arg->timestamp = ent->timestamp;
1142
+			arg->mac_addr = ent->mac_addr;		
1143
+			return (1);
1144
+		} else {
1145
+			struct sockaddr_in6 sa6;
1146
+			KEY_LEN(sa6) = KEY_LEN_INET6;
1147
+			memcpy(&sa6.sin6_addr, &arg->k.addr6, sizeof(struct in6_addr));
1148
+			xent = (struct table_xentry *)(rnh->rnh_lookup(&sa6, NULL, rnh));
1149
+		}
1150
+	}
1151
+		break;
1152
+
1153
+	case IPFW_TABLE_INTERFACE:
1154
+	{
1155
+		struct xaddr_iface iface;
1156
+
1157
+		KEY_LEN(iface) = KEY_LEN_IFACE +
1158
+		    strlcpy(iface.ifname, arg->k.iface, IF_NAMESIZE) + 1;
1159
+		/* Assume direct match */
1160
+		/* FIXME: Add interface pattern matching */
1161
+		xent = (struct table_xentry *)(rnh->rnh_lookup(&iface, NULL, rnh));
1162
+	}
1163
+		break;
1164
+
1165
+#if 0
1166
+	case IPFW_TABLE_MAC:
1167
+	{
1168
+		struct xaddr_mac mac;
1169
+
1170
+		KEY_LEN(mac) = KEY_LEN_MAC;
1171
+		mac.mac = arg->k.mac.addr;
1172
+		xent = (struct table_xentry *)(rnh->rnh_lookup(&mac, NULL, rnh));
1173
+	}
1174
+		break;
1175
+#endif
1176
+
1177
+	case IPFW_TABLE_MIX:
1178
+	{
1179
+		struct xaddr_mix xm;
1180
+
1181
+		KEY_LEN(xm) = KEY_LEN_MIX;
1182
+		memcpy(&xm.sin_addr, &arg->k.mix.addr, sizeof(struct in_addr));
1183
+		memcpy(&xm.mac, arg->k.mix.mac, ETHER_ADDR_LEN);
1184
+		xent = (struct table_xentry *)(rnh->rnh_lookup(&xm, NULL, rnh));
1185
+	}
1186
+		break;
1187
+
1188
+	default:
1189
+		return (0);
1190
+	}
1191
+
1192
+	if (xent != NULL) {
1193
+		arg->bytes = xent->bytes;
1194
+		arg->packets = xent->packets;
1195
+		arg->value = xent->value;
1196
+		arg->timestamp = xent->timestamp;
1197
+		arg->mac_addr = xent->mac_addr;
1198
+		
1199
+		return (1);
1200
+	}
1201
+	return (0);
1202
+}
1203
+
1204
+void *
1205
 ipfw_lookup_table_extended(struct ip_fw_chain *ch, uint16_t tbl, void *paddr,
1206
-    uint32_t *val, int type)
1207
+    uint32_t *val, int type, struct ether_addr *ea)
1208
 {
1209
 	struct radix_node_head *rnh;
1210
 	struct table_xentry *xent;
1211
@@ -562,15 +897,21 @@ ipfw_lookup_table_extended(struct ip_fw_chain *ch, uint16_t tbl, void *paddr,
1212
 	struct xaddr_iface iface;
1213
 
1214
 	if (tbl >= V_fw_tables_max)
1215
-		return (0);
1216
+		return (NULL);
1217
 	if ((rnh = ch->xtables[tbl]) == NULL)
1218
-		return (0);
1219
+		return (NULL);
1220
 
1221
 	switch (type) {
1222
 	case IPFW_TABLE_CIDR:
1223
 		KEY_LEN(sa6) = KEY_LEN_INET6;
1224
 		memcpy(&sa6.sin6_addr, paddr, sizeof(struct in6_addr));
1225
 		xent = (struct table_xentry *)(rnh->rnh_lookup(&sa6, NULL, rnh));
1226
+		if (xent != NULL) {
1227
+			if (ea && xent->mac_addr) {
1228
+				if (bcmp((u_char *)&xent->mac_addr, ea->octet, ETHER_ADDR_LEN) != 0)
1229
+					xent = NULL;
1230
+			}
1231
+		}
1232
 		break;
1233
 
1234
 	case IPFW_TABLE_INTERFACE:
1235
@@ -581,15 +922,37 @@ ipfw_lookup_table_extended(struct ip_fw_chain *ch, uint16_t tbl, void *paddr,
1236
 		xent = (struct table_xentry *)(rnh->rnh_lookup(&iface, NULL, rnh));
1237
 		break;
1238
 
1239
+#if 0
1240
+	case IPFW_TABLE_MAC:
1241
+	{
1242
+		struct xaddr_mac mac;
1243
+
1244
+		KEY_LEN(mac) = KEY_LEN_MAC;
1245
+		mac.mac = (struct ether_addr)(*paddr);
1246
+		xent = (struct table_xentry *)(rnh->rnh_lookup(&mac, NULL, rnh));
1247
+	}
1248
+		break;
1249
+#endif
1250
+
1251
+	case IPFW_TABLE_MIX:
1252
+	{
1253
+		struct xaddr_mix xm;
1254
+
1255
+		KEY_LEN(xm) = KEY_LEN_MIX;
1256
+		memcpy(((char *)&xm.sin_addr), paddr, sizeof(struct in_addr) + ETHER_ADDR_LEN);
1257
+		xent = (struct table_xentry *)(rnh->rnh_lookup(&xm, NULL, rnh));
1258
+	}
1259
+		break;
1260
+
1261
 	default:
1262
-		return (0);
1263
+		return (NULL);
1264
 	}
1265
 
1266
 	if (xent != NULL) {
1267
 		*val = xent->value;
1268
-		return (1);
1269
+		return (xent);
1270
 	}
1271
-	return (0);
1272
+	return (NULL);
1273
 }
1274
 
1275
 static int
1276
@@ -697,7 +1060,12 @@ dump_table_xentry_base(struct radix_node *rn, void *arg)
1277
 		xent->masklen = 33 - ffs(ntohl(n->mask.sin_addr.s_addr));
1278
 	/* Save IPv4 address as deprecated IPv6 compatible */
1279
 	xent->k.addr6.s6_addr32[3] = n->addr.sin_addr.s_addr;
1280
+	xent->flags = IPFW_TCF_INET;
1281
 	xent->value = n->value;
1282
+	xent->bytes = n->bytes;
1283
+	xent->packets = n->packets;
1284
+	xent->timestamp = n->timestamp;
1285
+	xent->mac_addr = n->mac_addr;
1286
 	tbl->cnt++;
1287
 	return (0);
1288
 }
1289
@@ -735,12 +1103,31 @@ dump_table_xentry_extended(struct radix_node *rn, void *arg)
1290
 		memcpy(&xent->k, &n->a.iface.ifname, IF_NAMESIZE);
1291
 		break;
1292
 	
1293
+#if 0
1294
+	case IPFW_TABLE_MAC:
1295
+		/* Assume exact mask */
1296
+		xent->masklen = 8 * ETHER_ADDR_LEN;
1297
+		xent->k.mac.addr = n->a.mac.mac;
1298
+		xent->k.mac.mask = n->m.mac.mac;
1299
+		break;
1300
+#endif
1301
+	
1302
+	case IPFW_TABLE_MIX:
1303
+		/* Assume exact mask */
1304
+		xent->masklen = 8 * (ETHER_ADDR_LEN + sizeof(struct in_addr));
1305
+		memcpy(&xent->k.mix.addr, &n->a.mix.sin_addr, sizeof(struct in_addr));
1306
+		memcpy(xent->k.mix.mac, &n->a.mix.mac, ETHER_ADDR_LEN);
1307
+		break;
1308
+
1309
 	default:
1310
 		/* unknown, skip entry */
1311
 		return (0);
1312
 	}
1313
 
1314
 	xent->value = n->value;
1315
+	xent->bytes = n->bytes;
1316
+	xent->packets = n->packets;
1317
+	xent->timestamp = n->timestamp;
1318
 	tbl->cnt++;
1319
 	return (0);
1320
 }
(2-2/65)