Projet

Général

Profil

Télécharger (35,2 ko) Statistiques
| Branche: | Révision:

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

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