Projet

Général

Profil

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

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

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