summaryrefslogtreecommitdiff
path: root/module/bsnmp-jails.c
blob: cfd0008df7fbf957d7d8c042ccc6aa65a1ef1e28 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
/*
 * Copyright (c) 2008, Stefan Walter
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 *     * Redistributions of source code must retain the above
 *       copyright notice, this list of conditions and the
 *       following disclaimer.
 *     * Redistributions in binary form must reproduce the
 *       above copyright notice, this list of conditions and
 *       the following disclaimer in the documentation and/or
 *       other materials provided with the distribution.
 *     * The names of contributors to this software may not be
 *       used to endorse or promote products derived from this
 *       software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
 * DAMAGE.
 *
 *
 * CONTRIBUTORS
 *  Stefan Walter <stef@memberwebs.com>
 */

#include "usuals.h"

#include <sys/param.h>
#include <sys/types.h>

#include <sys/jail.h>
#include <sys/limits.h>
#include <sys/proc.h>
#include <sys/queue.h>
#include <sys/socket.h>
#include <sys/select.h>
#include <sys/sockio.h>
#include <sys/sysctl.h>
#include <sys/user.h>
#include <sys/wait.h>

#include <netinet/in.h>
#include <netinet/in_systm.h>
#include <netinet/ip.h>
#include <arpa/inet.h>

#include <ctype.h>
#include <fcntl.h>
#include <limits.h>
#include <kvm.h>
#include <paths.h>
#include <syslog.h>
#include <stdarg.h>
#include <unistd.h>

#include <bsnmp/snmpmod.h>
#include <pcap.h>

#include "common/hash.h"

#include "jails_tree.h"
#include "jails_oid.h"

/* -------------------------------------------------------------------
 * COMPAT
 */

/*
 * First jail structure kernel version in
 * FreeBSD 4.x to 7.1 (unpatched)
 */

#ifndef HAVE_XPRISON_V1

struct xprison_v1 {
        int             pr_version;
        int             pr_id;
        char            pr_path[MAXPATHLEN];
        char            pr_host[MAXHOSTNAMELEN];
        u_int32_t       pr_ip;
};

/* It's now defined */
#define HAVE_XPRISON_V1 1

#endif /* HAVE_XPRISON_V1 */


/*
 * Second jail structure kernel version, used by multiple
 * IP jail patches that applied to 5.x, 6.x and 7.0
 */

#ifndef JAIL_MAX_IPS
#define JAIL_MAX_IPS    256
#endif

struct xprison_v2 {
        int             pr_version;
        int             pr_id;
        char            pr_path[MAXPATHLEN];
        char            pr_host[MAXHOSTNAMELEN];
        u_int32_t       pr_ips[JAIL_MAX_IPS];
        u_int           pr_nips;
};

/* This one is always defined */
#define HAVE_XPRISON_V2 1


/*
 * Third jail structure kernel version, used by FreeBSD
 * 7.2 and later.
 */

#if XPRISON_VERSION == 3

/* The system definition is V3 */
#define xprison_v3 xprison
#define HAVE_XPRISON_V3 1

#endif


/* -------------------------------------------------------------------
 * DECLARATIONS
 */

#define SNAP_LEN 64

/* our module handle */
static struct lmodule *module;

/* OIDs */
static const struct asn_oid oid_jails = OIDX_jails;

/* the Object Resource registration index */
static u_int reg_index = 0;

struct monitor {
	TAILQ_ENTRY(monitor)	link;

	int 			refs;

	char                    *device;
	pcap_t                  *handle;
	void                    *watch;
	struct bpf_program      filter;
	int                     filter_valid;
};

TAILQ_HEAD(monitor_list, monitor);

/* list of monitor structures */
static struct monitor_list monitors = TAILQ_HEAD_INITIALIZER (monitors);

/* Represents a 'top level' process */
struct ptop {
	pid_t 			pid;
	uint32_t		cpu_time;

	#define PROC_GONE    0
	#define PROC_EXISTS  1
	#define PROC_NEW     2
	int			status;
};

struct jaildat {
	uint32_t                index;
	TAILQ_ENTRY(jaildat)    link;
	int                     mark;

	/* Configuration */
	int                     jailid;
	char			*host;
	char                    *path;

	/* Monitor pointers for each address */
	char                    n_addrs;
	struct {
		struct sockaddr_in ip;
		struct monitor  *monitor;
	} addrs[JAIL_MAX_IPS];

	/* Stats gathered */
	uint64_t		in_octets;
	uint64_t		in_packets;
	uint64_t		out_octets;
	uint64_t 		out_packets;
	uint64_t		disk_space;
	uint64_t		disk_files;
	uint32_t		cpu_time_total;
	uint32_t		cpu_time_offset;
	uint32_t                n_processes;
	uint32_t		n_threads;

	/* Top process information */
	uint32_t		n_ptops;
	struct ptop		*ptops;
};

TAILQ_HEAD(jaildat_list, jaildat);

/* list of jail structures */
static struct jaildat_list jaildats = TAILQ_HEAD_INITIALIZER (jaildats);

/* number of if jail structures */
static u_int jaildat_count = 0;
static u_int jaildat_index = 0;

/* Hash of jail structures by id */
static hsh_t *jaildat_by_host = NULL;

/* Hash of jail structures by path */
static hsh_t *jaildat_by_path = NULL;

/* Hash of jail structures by address */
static hsh_t *jaildat_by_address = NULL;

/* Hash of jail structures by id */
static hsh_t *jaildat_by_id = NULL;

/* Timer for refreshing the jaildat info */
static void *timer_refresh = NULL;

/* Refresh interval in SNMP ticks */
static int refresh_interval = 3 * 100;

/* Timer for measuring the jails */
static void *timer_measure = NULL;

/* Measure interval in SNMP ticks */
static int measure_interval = 1800 * 100;

/* The monitor network filter */
static u_char *network_filter = NULL;

/* The KVM interface handle */
static kvm_t *kvm_handle = NULL;

/* -----------------------------------------------------------------------------
 * HELPERS
 */

static void
emsg(const char *format, ...)
{
	va_list va;
	va_start (va, format);
	vsyslog (LOG_ERR, format, va);
	vfprintf (stderr, format, va);
	fputc ('\n', stderr);
	va_end (va);
}

#if 0
static void
msg(const char *format, ...)
{
	va_list va;
	va_start (va, format);
	vfprintf (stderr, format, va);
	fputc ('\n', stderr);
	va_end (va);
}
#endif

typedef void* (*if_enumerator) (struct ifreq *ifr, void* data);

static void*
enumerate_ifs (if_enumerator func, void* data)
{
	int sockfd = -1;
	struct ifconf ifc;
	struct ifreq *ifr = NULL;
	unsigned char *ptr;
	unsigned char *buf = NULL;
	void *result = NULL;

	ASSERT (func);

	ifc.ifc_len = 32768;
	buf = malloc (ifc.ifc_len);
	ifc.ifc_req = (struct ifreq*)buf;
	if (!buf) {
		emsg ("couldn't allocate buffer to list interfaces: out of memory");
		goto cleanup;
	}

	sockfd = socket (AF_INET, SOCK_DGRAM, 0);
	if (sockfd < 0) {
		emsg ("couldn't create socket to list interfaces: %s", strerror (errno));
		goto cleanup;
	}

	if (ioctl (sockfd, SIOCGIFCONF, &ifc) < 0) {
		emsg ("couldn't list interfaces: %s", strerror (errno));
		goto cleanup;
	}

	#define IFR_SIZE(ifr)      \
		(max ((ifr)->ifr_addr.sa_len, sizeof((ifr)->ifr_addr)) + sizeof((ifr)->ifr_name))

	for (ptr = buf; ptr < buf + ifc.ifc_len; ) {
		ifr = (struct ifreq*)ptr;
		ptr += IFR_SIZE (ifr);

		result = (func) (ifr, data);
		if (result)
			break;
	}

cleanup:
	if (sockfd >= 0)
		close (sockfd);
	if (buf)
		free (buf);
	return result;
}

/* -----------------------------------------------------------------------------
 * MONITORING
 */

#pragma pack(1)

/* Ethernet header */
struct ethhdr {
	#define ETHER_ADDR_LEN	6
	u_char dhost[ETHER_ADDR_LEN]; /* Destination host address */
	u_char shost[ETHER_ADDR_LEN]; /* Source host address */
	u_short type; /* IP? ARP? RARP? etc */
};

/* IP4 header */
struct ip4hdr {
	uint8_t vhl;		/* version << 4 | header length >> 2 */
	uint8_t tos;		/* type of service */
	uint16_t len;		/* total length */
	uint16_t id;		/* identification */
	uint16_t off;		/* fragment offset field */
	#define IP_RF 0x8000		/* reserved fragment flag */
	#define IP_DF 0x4000		/* dont fragment flag */
	#define IP_MF 0x2000		/* more fragments flag */
	#define IP_OFFMASK 0x1fff	/* mask for fragmenting bits */
	uint8_t ttl;		/* time to live */
	uint8_t proto;		/* protocol */
	uint16_t sum;		/* checksum */
	struct in_addr src, dst; /* source and dest address */
};

/* IP6 header */
struct ip6hdr {
	int32_t flow;
	int16_t payload;
	int8_t next;
	int8_t hops;
	struct in6_addr src, dst;
};

#pragma pack()

static void
calculate_ip4 (const struct ip4hdr *hdr, uint32_t octets)
{
	struct sockaddr_in addr;
	struct jaildat *jail;

	ASSERT (hdr);

	/* Try incoming */
	memset (&addr, 0, sizeof (addr));
	addr.sin_family = AF_INET;
	addr.sin_len = sizeof (addr);
	addr.sin_port = 0;
	memcpy (&addr.sin_addr, &hdr->dst, sizeof (addr.sin_addr));

	jail = hsh_get (jaildat_by_address, &addr, addr.sin_len);
	if (jail) {
		jail->in_octets += octets;
		++jail->in_packets;
		return;
	}

	/* Try outgoing */
	memcpy (&addr.sin_addr, &hdr->src, sizeof (addr.sin_addr));

	jail = hsh_get (jaildat_by_address, &addr, addr.sin_len);
	if (jail) {
		jail->out_octets += octets;
		++jail->out_packets;
		return;
	}
}

static void
calculate_ip6 (const struct ip6hdr *hdr, uint32_t octets)
{
	struct sockaddr_in6 addr;
	struct jaildat *jail;

	ASSERT (hdr);

	/* Try incoming */
	memset (&addr, 0, sizeof (addr));
	addr.sin6_family = AF_INET6;
	addr.sin6_len = sizeof (addr);
	addr.sin6_port = 0;
	memcpy (&addr.sin6_addr, &hdr->dst, sizeof (addr.sin6_addr));

	jail = hsh_get (jaildat_by_address, &addr, addr.sin6_len);
	if (jail) {
		jail->in_octets += octets;
		++jail->in_packets;
		return;
	}

	/* Try outgoing */
	memcpy (&addr.sin6_addr, &hdr->src, sizeof (addr.sin6_addr));

	jail = hsh_get (jaildat_by_address, &addr, addr.sin6_len);
	if (jail) {
		jail->out_octets += octets;
		++jail->out_packets;
		return;
	}
}

static void
monitor_packet (u_char *data, const struct pcap_pkthdr *hdr, const u_char *bytes)
{
	struct ethhdr *eth;
	int minlen, octets, type;

	/* Short packet, don't care */
	if (hdr->len < sizeof (struct ethhdr))
		return;

	eth = (struct ethhdr*)bytes;
	bytes += sizeof (struct ethhdr);
	octets = hdr->len - sizeof (struct ethhdr);
	type = ntohs (eth->type);

	/* IPv4 packet? */
	if (type == 0x0800) {
		minlen = (sizeof (struct ethhdr) + sizeof (struct ip4hdr));
		if (hdr->len >= minlen && hdr->caplen >= minlen)
			calculate_ip4 ((const struct ip4hdr*)bytes, octets);

	/* IPv6 packet? */
	} else if (type == 0x86DD) {
		minlen = (sizeof (struct ethhdr) + sizeof (struct ip6hdr));
		if (hdr->len >= minlen && hdr->caplen >= minlen)
			calculate_ip6 ((const struct ip6hdr*)bytes, octets);
	}
}

static void
monitor_io (int fd, void *data)
{
	struct monitor* mon = (struct monitor*)data;
	int n_packets;

	n_packets = pcap_dispatch (mon->handle, -1, monitor_packet, (u_char*)mon);
	if (n_packets < 0)
		emsg ("couldn't capture packets in monitor: %s", pcap_geterr (mon->handle));
}

static void
monitor_free (struct monitor *mon)
{
	ASSERT (mon);

	if (mon->device)
		free (mon->device);
	if (mon->watch)
		fd_deselect (mon->watch);
	if (mon->handle)
		pcap_close (mon->handle);
	if (mon->filter_valid)
		pcap_freecode (&mon->filter);

	TAILQ_REMOVE (&monitors, mon, link);
	free (mon);
}

static struct monitor*
monitor_create (const char *device)
{
	char errbuf[PCAP_ERRBUF_SIZE];
	struct monitor* mon = NULL;
	int success = 0;
	int fd;

	ASSERT (device);
	ASSERT (network_filter);

	mon = calloc (1, sizeof (struct monitor));
	if (!mon) {
		emsg ("couldn't allocate monitor: out of memory");
		goto cleanup;
	}

	TAILQ_INSERT_TAIL(&monitors, mon, link);
	mon->device = strdup (device);

	ASSERT (SNAP_LEN >= sizeof (struct ethhdr) + sizeof (struct ip4hdr));
	ASSERT (SNAP_LEN >= sizeof (struct ethhdr) + sizeof (struct ip6hdr));

	mon->handle = pcap_open_live (mon->device, SNAP_LEN, 1, 100, errbuf);
	if (!mon->handle) {
		emsg ("couldn't open monitor on %s: %s", mon->device, errbuf);
		goto cleanup;
	}

	if (pcap_compile (mon->handle, &mon->filter, (const char*)network_filter, 1, 0) < 0) {
		emsg ("couldn't compile monitor expression: %s", pcap_geterr (mon->handle));
		goto cleanup;
	}

	mon->filter_valid = 1;
	if (pcap_setfilter (mon->handle, &mon->filter) < 0) {
		emsg ("couldn't setup monitor expression: %s", pcap_geterr (mon->handle));
		goto cleanup;
	}

	if (pcap_setnonblock (mon->handle, 1, errbuf) < 0) {
		emsg ("couldn't set monitor in non-block mode: %s", errbuf);
		goto cleanup;
	}

	fd = pcap_get_selectable_fd (mon->handle);
	if (fd < 0) {
		emsg ("couldn't get selectable monitor: %s", pcap_geterr (mon->handle));
		goto cleanup;
	}

	mon->watch = fd_select (fd, monitor_io, mon, module);
	if (!mon->watch) {
		emsg ("couldn't listen to monitor: %s", strerror (errno));
		goto cleanup;
	}

	success = 1;

cleanup:
	if (!success && mon) {
		monitor_free (mon);
		mon = NULL;
	}

	return mon;
}

static void
monitor_ref (struct monitor *mon)
{
	ASSERT (mon);
	ASSERT (mon->refs >= 0);
	++mon->refs;
}

static void
monitor_unref (struct monitor *mon)
{
	ASSERT (mon);

	--mon->refs;
	ASSERT (mon->refs >= 0);

	if (mon->refs == 0)
		monitor_free (mon);
}

static struct monitor*
monitor_for_device (const char *device)
{
	struct monitor *mon;

	ASSERT (device);

	/* Mark and prepare for sweep below */
	TAILQ_FOREACH (mon, &monitors, link) {
		ASSERT (mon->device);
		if (strcmp (mon->device, device) == 0)
			return mon;
	}

	return monitor_create (device);
}

static void*
monitor_addr_enumerator (struct ifreq *ifr, void *data)
{
	struct sockaddr *addr = (struct sockaddr*)data;
	int match = 0;

	ASSERT (ifr);
	ASSERT (addr);

	switch (ifr->ifr_addr.sa_family) {
	case AF_INET:
		if (addr->sa_family == AF_INET) {
			struct sockaddr_in *sin = (struct sockaddr_in*)addr;
			if (memcmp (&(sin->sin_addr), &(((struct sockaddr_in*)&ifr->ifr_addr)->sin_addr),
			            sizeof (sin->sin_addr)) == 0)
				match = 1;
		}
		break;

	case AF_INET6:
		if (addr->sa_family == AF_INET6) {
			struct sockaddr_in6 *sin6 = (struct sockaddr_in6*)addr;
			if (memcmp (&sin6->sin6_addr, &(((struct sockaddr_in6*)&ifr->ifr_addr)->sin6_addr),
			            sizeof (sin6->sin6_addr)) == 0)
				match = 1;
		}
		break;

	default:
		break;
	};

	if (!match)
		return NULL;

	return monitor_for_device (ifr->ifr_name);
}

static struct monitor*
monitor_for_address (struct sockaddr *addr)
{
	return (struct monitor*)enumerate_ifs (monitor_addr_enumerator, addr);
}

static int
monitor_test_filter (const char *filter)
{
	struct bpf_program bpf;
	int ret;

	ret = pcap_compile_nopcap (SNAP_LEN, DLT_EN10MB, &bpf, (char*)filter, 1, 0);
	if (ret >= 0)
		pcap_freecode (&bpf);

	return ret;
}

/* -----------------------------------------------------------------------------
 * MEASURE JAILS
 */

static void *measure_out_watch = NULL;
static int measure_out_fd = -1;

static void
measure_parse (char *line)
{
	unsigned long long value;
	struct jaildat *jail;
	char *p;

	/* Find the first colon */
	p = strchr (line, ':');
	if (!p) {
		emsg ("invalid output from measure process: %s", line);
		return;
	}

	/* Skip past the colon and spaces */
	*p = 0;
	++p;
	while (isspace (*p))
		++p;

	/* Parse the number */
	value = strtoull (p, &p, 10);

	/* Skip past any more spaces */
	while (isspace (*p))
		++p;

	/* Find a jail with this path */
	jail = hsh_get (jaildat_by_path, p, HSH_KEY_STRING);
	if (!jail)
		return;

	/* Update the various values */
	if (strcmp (line, "jail-space") == 0)
		jail->disk_space = value;
	else if (strcmp (line, "jail-files") == 0)
		jail->disk_files = value;
}

static void
measure_io (int fd, void *unused)
{
	char buffer[2048];
	char *line, *next;
	int r;

	ASSERT (measure_out_fd == fd);

	r = read (fd, buffer, sizeof (buffer) - 1);

	/* Error conditions */
	if (r < 0) {
		if (errno == EAGAIN || errno == EINTR)
			return;
		emsg ("couldn't read from jail measure: %s", strerror (errno));
	}

	/* Close and disconnect */
	if (r <= 0) {
		fd_deselect (measure_out_watch);
		measure_out_watch = NULL;
		close (measure_out_fd);
		measure_out_fd = -1;
		return;
	}

	/* Null terminate the line */
	ASSERT (r < sizeof (buffer));
	buffer[r] = 0;
	line = buffer;

	/* And parse each line received */
	while (line) {
		next = strchr (line, '\n');
		if (next)
			*(next++) = 0;
		while (isspace (*line))
			++line;
		if (*line)
			measure_parse (line);
		line = next;
	}
}

static char**
measure_get_command (void)
{
	struct jaildat *jail;
	char **args;
	int i;

	if (!jaildat_count)
		return NULL;

	args = calloc (jaildat_count + 4, sizeof (char*));
	if (!args) {
		emsg ("out of memory");
		return NULL;
	}

	args[0] = JAIL_MEASURE_PATH;
	args[1] = "-l";
	args[2] = "11"; /* ie: LOG_ERR | LOG_DAEMON */

	i = 3;
	TAILQ_FOREACH (jail, &jaildats, link)
		args[i++] = jail->path;

	return args;
}

static void
measure_all (void *unused)
{
	char **args;
	int pip[2];
	pid_t pid;
	int status;

	if (measure_out_watch) {
		ASSERT (measure_out_fd != -1);
		emsg ("jail measure already in progress, skipping");
		return;
	}

	args = measure_get_command ();
	if (!args)
		return;

	if (pipe (pip) < 0) {
		emsg ("couldn't create pipe to do jail measure: %s", strerror (errno));
		free (args);
		return;
	}

	pid = fork ();
	switch (pid) {
	case -1:
		emsg ("couldn't fork a measure process: %s", strerror (errno));
		free (args);
		close (pip[0]);
		close (pip[1]);
		return;

	case 0:
		/* Detach this new process */
		if (daemon (1, 1) < 0) {
			emsg ("couldn't daemonize the measure process: %s", strerror (errno));
			exit (1);
		}

		/* Dup pipe into stdout for process */
		close (pip[0]);
		dup2 (pip[1], 1);

		/* Run the measure process */
		execv (args[0], args);
		emsg ("couldn't execute the measure process: %s", strerror (errno));
		exit (1);
		break;

	default:
		break;
	};

	/* We no longer need this end */
	waitpid (pid, &status, 0);
	close (pip[1]);
	free (args);

	measure_out_watch = fd_select (pip[0], measure_io, NULL, module);
	if (!measure_out_watch) {
		emsg ("couldn't watch the measure process output: %s", strerror (errno));
		close (pip[0]);
	}

	measure_out_fd = pip[0];
}

static void
measure_start (void *unused)
{
	ASSERT (timer_measure);
	timer_measure = timer_start_repeat (measure_interval, measure_interval,
	                                    measure_all, NULL, module);
	measure_all (NULL);
}

/* -----------------------------------------------------------------------------
 * PROCESS LOOKUPS
 */

static int
process_compar_kp_pid (const void *a, const void *b)
{
	pid_t pida, pidb;

	pida = ((const struct kinfo_proc*)a)->ki_pid;
	pidb = ((const struct kinfo_proc*)b)->ki_pid;

	if (pida == pidb)
		return 0;

	/* Note we're sorting in reverse */
	return (pida < pidb) ? 1 : -1;
}

static int
process_compapre_kp_ppid_pid (const void *a, const void *b)
{
	pid_t pida, pidb;

	pida = ((const struct kinfo_proc*)a)->ki_ppid;
	pidb = ((const struct kinfo_proc*)b)->ki_pid;

	if (pida == pidb)
		return 0;

	/* Note we're sorting in reverse */
	return (pida < pidb) ? 1 : -1;
}

static int
process_compare_kp_ptop_pid (const void *a, const void *b)
{
	pid_t pida, pidb;

	pida = ((const struct kinfo_proc*)a)->ki_pid;
	pidb = ((const struct ptop*)b)->pid;

	if (pida == pidb)
		return 0;

	/* Note we're sorting in reverse */
	return (pida < pidb) ? 1 : -1;
}

static int
process_compare_ptop_pid (const void *a, const void *b)
{
	pid_t pida, pidb;

	pida = ((const struct ptop*)a)->pid;
	pidb = ((const struct ptop*)b)->pid;

	if (pida == pidb)
		return 0;

	/* Note we're sorting in reverse */
	return (pida < pidb) ? 1 : -1;
}

static void
process_refresh_all (void)
{
	struct kinfo_proc *kp, *tkp, *k;
	struct jaildat *jail;
	struct ptop *ptop;
	int nentries, i, jid;
	uint32_t cpu_time;
	void *alloc;
	int pagesize;

	/* Get a process listing */
	kp = kvm_getprocs (kvm_handle, KERN_PROC_PROC, 0, &nentries);
	if (kp == NULL) {
		emsg ("couldn't lookup process list: %s", kvm_geterr (kvm_handle));
		return;
	}

	/* Sort the input we get, in reverse */
	qsort (kp, nentries, sizeof (*kp), process_compar_kp_pid);
	pagesize = getpagesize ();

	/* Mark all processes in the jail for later sweep */
	TAILQ_FOREACH (jail, &jaildats, link) {
		for (i = 0; i < jail->n_ptops; ++i)
			jail->ptops[i].status = PROC_GONE;

		jail->n_processes = 0;
		jail->n_threads = 0;
		jail->cpu_time_total = jail->cpu_time_offset;
	}

	/* Okay now loop and add to each process's jail */
	for (i = 0; i < nentries; i++) {

		jid = kp[i].ki_jid;

		/* No jail? */
		if (jid == 0)
			continue;

		jail = hsh_get (jaildat_by_id, &jid, sizeof (jid));
		if (jail == NULL)
			continue;

		jail->n_processes += 1;
		jail->n_threads += kp[i].ki_numthreads;


		/* Find the top level process within jail to account to */
		tkp = &kp[i];
		for (;;) {
			if (tkp->ki_pid == tkp->ki_ppid)
				break;
			k = bsearch (tkp, kp, nentries, sizeof (*kp), process_compapre_kp_ppid_pid);
			if (k == NULL || k->ki_jid != jid)
				break;
			tkp = k;
		}

		/* Find top process for that pid */
		ptop = bsearch (tkp, jail->ptops, jail->n_ptops,
		                sizeof (struct ptop), process_compare_kp_ptop_pid);
		if (ptop == NULL) {
			alloc = realloc (jail->ptops, (jail->n_ptops + 1) * sizeof (struct ptop));
			if (alloc == NULL) {
				emsg ("out of memory");
				continue;
			}

			jail->ptops = alloc;
			ptop = jail->ptops + jail->n_ptops;
			jail->n_ptops += 1;

			ptop->pid = tkp->ki_pid;
			ptop->cpu_time = 0;
			ptop->status = PROC_NEW;
		}

		/* Account CPU time to this process */
		cpu_time = kp[i].ki_runtime + kp[i].ki_childtime.tv_usec;
		cpu_time /= 10000;
		cpu_time += kp[i].ki_childtime.tv_sec * 100;

		if (ptop->status == PROC_GONE) {
			ptop->cpu_time = 0;
			ptop->status = PROC_EXISTS;
		}

		ptop->cpu_time += cpu_time;
		jail->cpu_time_total += cpu_time;

		/* Sort the array if added */
		qsort (jail->ptops, jail->n_ptops, sizeof (struct ptop), process_compare_ptop_pid);
	}

	TAILQ_FOREACH (jail, &jaildats, link) {

		for (i = 0; i < jail->n_ptops; ++i) {
			if (jail->ptops[i].status != PROC_GONE)
				continue;

			/*
			 * Add time to the cpu_time_offset if it's a 'top level'
			 * process that's going away.
			 */

			jail->cpu_time_offset += jail->ptops[i].cpu_time;
			jail->cpu_time_total += jail->ptops[i].cpu_time;

			/* Remove nonexistant ptop */
			memmove (jail->ptops + i, jail->ptops + i + 1,
			         (jail->n_ptops - i) * sizeof (struct ptop));
			jail->n_ptops -= 1;
			i -= 1;
		}
	}
}

/* -----------------------------------------------------------------------------
 * JAIL LOOKUPS
 */

static void
jail_free (struct jaildat *jail)
{
	int i;

	ASSERT (jail);

	if (jail->jailid)
		hsh_rem (jaildat_by_id, &jail->jailid, sizeof (jail->jailid));

	if (jail->host) {
		hsh_rem (jaildat_by_host, jail->host, HSH_KEY_STRING);
		free (jail->host);
		jail->host = NULL;
	}

	/* Remove all addresses and release monitors */
	for (i = 0; i < jail->n_addrs; ++i) {
		hsh_rem (jaildat_by_address, &(jail->addrs[i].ip),
		         jail->addrs[i].ip.sin_len);
		if (jail->addrs[i].monitor)
			monitor_unref (jail->addrs[i].monitor);
		memset (&jail->addrs[i], 0, sizeof (jail->addrs[i]));
	}

	if (jail->path) {
		hsh_rem (jaildat_by_path, jail->path, HSH_KEY_STRING);
		free (jail->path);
	}
	jail->path = NULL;

	if (jail->index) {
		TAILQ_REMOVE (&jaildats, jail, link);
		jaildat_count--;
	}

	free (jail->ptops);
	jail->ptops = NULL;

	free (jail);
}

static int
jail_update (struct jaildat *jail, int jailid, const char *host,
             const char *path, struct in_addr *ips, u_int n_ips)
{
	struct monitor *mon = NULL;
	struct sockaddr_in addr;
	char *dup;
	int i, top;

	ASSERT (jail);

	/* Do the jail id */
	if (jail->jailid != jailid) {
		hsh_rem (jaildat_by_id, &jail->jailid, sizeof (jail->jailid));
		jail->jailid = jailid;
		hsh_set (jaildat_by_id, &jail->jailid, sizeof (jail->jailid), jail);
	}

	/* Do the host */
	if (!host)
		host = "";

	if (!jail->host || strcmp (jail->host, host) != 0) {
		dup = strdup (host);
		if (!dup)
			return -1;
		if (jail->host) {
			hsh_rem (jaildat_by_host, jail->host, HSH_KEY_STRING);
			free (jail->host);
		}
		jail->host = dup;
		hsh_set (jaildat_by_host, jail->host, HSH_KEY_STRING, jail);
	}

	/* Do the path */
	if (!path)
		path = "";

	if (!jail->path || strcmp (jail->path, path) != 0) {
		dup = strdup (path);
		if (!dup)
			return -1;
		if (jail->path) {
			hsh_rem (jaildat_by_path, jail->path, HSH_KEY_STRING);
			free (jail->path);
		}
		jail->path = dup;
		hsh_set (jaildat_by_path, jail->path, HSH_KEY_STRING, jail);
	}

	/* Do the addresses */
	top = (jail->n_addrs > n_ips ? jail->n_addrs : n_ips);
	for (i = 0; i < top; ++i) {

		/* Setup the address */
		if (i < n_ips) {
			memset (&addr, 0, sizeof (addr));
			addr.sin_family = AF_INET;
			addr.sin_len = sizeof (addr);
			addr.sin_port = 0;
			addr.sin_addr.s_addr = ips[i].s_addr;

			/* Same? skip. */
			if (i < jail->n_addrs &&
			    memcmp (&jail->addrs[i].ip, &addr, sizeof (addr)) == 0)
				continue;
		}

		/* Remove old address */
		if (i < jail->n_addrs) {
			hsh_rem (jaildat_by_address, &(jail->addrs[i].ip),
			         jail->addrs[i].ip.sin_len);
			memset (&jail->addrs[i].ip, 0, sizeof (jail->addrs[i].ip));
		}

		/* Add new address */
		if (i < n_ips) {
			memcpy (&jail->addrs[i].ip, &addr, sizeof (addr));
			hsh_set (jaildat_by_address, &(jail->addrs[i].ip),
			         jail->addrs[i].ip.sin_len, jail);
		}

		/* Register new monitor */
		mon = NULL;
		if (i < n_ips) {
			mon = monitor_for_address ((struct sockaddr*)&addr);
			if (mon)
				monitor_ref (mon);
		}

		/* Free the old monitor */
		if (i < jail->n_addrs && jail->addrs[i].monitor)
			monitor_unref (jail->addrs[i].monitor);

		/* Assign the monitor */
		jail->addrs[i].monitor = mon;
	}

	jail->n_addrs = n_ips;

	return 0;
}

static struct jaildat*
jail_alloc (int jailid, const char *host, const char *path,
            struct in_addr *ips, u_int n_ips)
{
	struct jaildat *jail;

	jail = (struct jaildat*)calloc (1, sizeof (struct jaildat));
	if (!jail)
		return NULL;

	if (jail_update (jail, jailid, host, path, ips, n_ips) < 0) {
		jail_free (jail);
		return NULL;
	}

	jaildat_count++;
	jail->index = ++jaildat_index;
	INSERT_OBJECT_INT (jail, &jaildats);

	return jail;
}

#ifdef HAVE_XPRISON_V1

static void
jail_refresh_v1 (struct xprison_v1 *xp, size_t len)
{
	struct jaildat *jail;
	struct in_addr in;
	int i;

	/* Make sure its kosher */
	if (len < sizeof (*xp) || len % sizeof (*xp)) {
		emsg ("not a valid v1 xprison. kernel and userland out of sync?");
		return;
	}

	/*
	 * Allocate new jails, and update old ones. We iterate backwards
	 * so that if there are multiple jails with the same host name
	 * (some possibly zombies), then we'll see the most recent one.
	 */
	for (i = (len / sizeof (*xp)) - 1; i >= 0; --i) {
		in.s_addr = htonl (xp[i].pr_ip);
		jail = hsh_get (jaildat_by_host, xp[i].pr_host, HSH_KEY_STRING);
		if (!jail)
			jail = jail_alloc (xp[i].pr_id, xp[i].pr_host, xp[i].pr_path,
			                   &in, 1);
		else
			jail_update (jail, xp[i].pr_id, xp[i].pr_host, xp[i].pr_path,
			             &in, 1);
		if (jail)
			jail->mark = 0;
	}
}

#endif /* HAVE_XPRISON_V1 */

#ifdef HAVE_XPRISON_V2

static void
jail_refresh_v2 (struct xprison_v2 *xp, size_t len)
{
	struct in_addr ins[JAIL_MAX_IPS];
	struct jaildat *jail;
	int i, j;

	/* Make sure its kosher */
	if (len < sizeof (*xp) || len % sizeof (*xp)) {
		emsg ("not a valid v2 xprison. kernel and userland out of sync?");
		return;
	}

	/*
	 * Allocate new jails, and update old ones. We iterate backwards
	 * so that if there are multiple jails with the same host name
	 * (some possibly zombies), then we'll see the most recent one.
	 */
	for (i = (len / sizeof (*xp)) - 1; i >= 0; --i) {
		for (j = 0; j < xp[i].pr_nips; ++j)
			ins[j].s_addr = htonl (xp[i].pr_ips[j]);
		jail = hsh_get (jaildat_by_host, xp[i].pr_host, HSH_KEY_STRING);
		if (!jail)
			jail = jail_alloc (xp[i].pr_id, xp[i].pr_host, xp[i].pr_path,
			                   ins, xp[i].pr_nips);
		else
			jail_update (jail, xp[i].pr_id, xp[i].pr_host, xp[i].pr_path,
			             ins, xp[i].pr_nips);
		if (jail)
			jail->mark = 0;
	}
}

#endif /* HAVE_XPRISON_V2 */

#ifdef HAVE_XPRISON_V3

static void
jail_refresh_v3 (struct xprison_v3 *xp, size_t len)
{
	struct jaildat *jail;
	size_t xlen;

	while (len > 0) {

		/* Make sure its kosher */
		if (len < sizeof (*xp)) {
			emsg ("not a valid v3 xprison. kernel and userland out of sync?");
			return;
		}

		/* The length of this block */
		xlen = sizeof (*xp) + (xp->pr_ip4s * sizeof (struct in_addr)) +
		       (xp->pr_ip6s * sizeof (struct in6_addr));

		/* Make sure its kosher */
		if (len < xlen) {
			emsg ("not a valid v3 xprison struct. kernel and userland out of sync?");
			return;
		}

		if (xp->pr_state == PRISON_STATE_ALIVE) {
			jail = hsh_get (jaildat_by_host, xp->pr_host, HSH_KEY_STRING);
			if (!jail)
				jail = jail_alloc (xp->pr_id, xp->pr_host, xp->pr_path,
				                   (struct in_addr*)(xp + 1), xp->pr_ip4s);
			else
				jail_update (jail, xp->pr_id, xp->pr_host, xp->pr_path,
				             (struct in_addr*)(xp + 1), xp->pr_ip4s);

			if (jail)
				jail->mark = 0;
		}

		xp = (struct xprison_v3*)(((unsigned char*)xp) + xlen);
		len -= xlen;
	}
}

#endif /* HAVE_XPRISON_V3 */

static void
jail_refresh_all (void* unused)
{
        struct xprison *sxp = NULL;
	struct jaildat *jail, *tmp;
        size_t len;
	int i;

	/* Get the length of the list */
        if (sysctlbyname ("security.jail.list", NULL, &len, NULL, 0) == -1) {
		emsg ("couldn't lookup jail list: %s", strerror (errno));
		return;
	}

	/* Retrieve actual data */
	for (i = 0; i < 4; i++) {
		if (len <= 0)
			break;

		sxp = malloc (len);
		if (sxp == NULL) {
			emsg ("out of memory");
			return;
		}

                if (sysctlbyname ("security.jail.list", sxp, &len, NULL, 0) == -1) {
			free (sxp);
			sxp = NULL;
			if (errno == ENOMEM)
				continue;
                }

		if (sxp == NULL) {
			emsg ("couldn't retrieve jail list: %s", strerror (errno));
			return;
		}

                break;
        }

	/* Mark and prepare for sweep below */
	TAILQ_FOREACH (jail, &jaildats, link)
		jail->mark = 1;

	if (len > 0) {
		switch (sxp->pr_version) {

#ifdef HAVE_XPRISON_V1
		case 1:
			jail_refresh_v1 ((struct xprison_v1*)sxp, len);
			break;
#endif

#ifdef HAVE_XPRISON_V2
		case 2:
			jail_refresh_v2 ((struct xprison_v2*)sxp, len);
			break;
#endif

#ifdef HAVE_XPRISON_V3
		case 3:
			jail_refresh_v3 ((struct xprison_v3*)sxp, len);
			break;
#endif

		default:
			emsg ("unsupported kernel structure version: %d\n", sxp->pr_version);
		}

		free (sxp);
	}

	/* Sweep any jails that are no longer */
	TAILQ_FOREACH_SAFE (jail, &jaildats, link, tmp) {
		if (jail->mark)
			jail_free (jail);
	}

	process_refresh_all ();

	/* Remove any jails that have no processes (kernel anomally) */
	TAILQ_FOREACH_SAFE (jail, &jaildats, link, tmp) {
		if (!jail->n_processes)
			jail_free (jail);
	}
}

/* -----------------------------------------------------------------------------
 * CALLBACKS/CONFIG
 */

int
op_jailconfig (struct snmp_context *ctx, struct snmp_value *value,
               u_int sub, u_int iidx, enum snmp_op op)
{
	asn_subid_t which = value->var.subs[sub - 1];
	int r = SNMP_ERR_NOERROR;

	if (op == SNMP_OP_GET) {
		switch (which) {
		case LEAF_jailNetworkFilter:
			return string_get (value, network_filter, -1);
		case LEAF_jailRefreshInterval:
			value->v.uint32 = refresh_interval;
			return SNMP_ERR_NOERROR;
		case LEAF_jailMeasureInterval:
			value->v.uint32 = measure_interval;
			return SNMP_ERR_NOERROR;
		default:
			ASSERT (0);
			return -1;
		};
	}

	/* Remainder only at initialization */
	if (community != COMM_INITIALIZE)
		return SNMP_ERR_NOT_WRITEABLE;

	switch (which) {
	case LEAF_jailNetworkFilter:
		switch (op) {
		case SNMP_OP_SET:
			if ((r = string_save (value, ctx, -1, &network_filter)) == SNMP_ERR_NOERROR) {
				if (monitor_test_filter ((const char*)network_filter) < 0)
					r = SNMP_ERR_GENERR;
			}
			if (r != SNMP_ERR_NOERROR)
				string_rollback (ctx, &network_filter);
			break;
		case SNMP_OP_COMMIT:
			string_commit (ctx);
			break;
		case SNMP_OP_ROLLBACK:
			string_rollback (ctx, &network_filter);
			break;
		default:
			ASSERT (0);
			return SNMP_ERR_GENERR;
		};
		break;

	case LEAF_jailRefreshInterval:
		switch (op) {
		case SNMP_OP_SET:
			ctx->scratch->int1 = refresh_interval;
			refresh_interval = value->v.uint32;
			return SNMP_ERR_NOERROR;
		case SNMP_OP_ROLLBACK:
			refresh_interval = ctx->scratch->int1;
			return SNMP_ERR_NOERROR;
		case SNMP_OP_COMMIT:
			return SNMP_ERR_NOERROR;
		default:
			ASSERT (0);
			return SNMP_ERR_GENERR;
		};
		break;

	case LEAF_jailMeasureInterval:
		switch (op) {
		case SNMP_OP_SET:
			ctx->scratch->int1 = measure_interval;
			measure_interval = value->v.uint32;
			return SNMP_ERR_NOERROR;
		case SNMP_OP_ROLLBACK:
			measure_interval = ctx->scratch->int1;
			return SNMP_ERR_NOERROR;
		case SNMP_OP_COMMIT:
			return SNMP_ERR_NOERROR;
		default:
			ASSERT (0);
			return SNMP_ERR_GENERR;
		};
		break;
    	default:
		ASSERT (0);
		return SNMP_ERR_GENERR;
	};

	return r;
}

int
op_jailentry (struct snmp_context *ctx, struct snmp_value *value,
              u_int sub, u_int iidx, enum snmp_op op)
{
	asn_subid_t which = value->var.subs[sub - 1];
	struct jaildat *jail = NULL;

	switch (op) {
	case SNMP_OP_GETNEXT:
		jail = NEXT_OBJECT_INT (&jaildats, &value->var, sub);
		if (jail == NULL)
			return SNMP_ERR_NOSUCHNAME;
		value->var.len = sub + 1;
		value->var.subs[sub] = jail->index;
		break;

	case SNMP_OP_GET:
		jail = FIND_OBJECT_INT (&jaildats, &value->var, sub);
		if (jail == NULL)
			return SNMP_ERR_NOSUCHNAME;
		break;

	default:
		jail = FIND_OBJECT_INT (&jaildats, &value->var, sub);
		break;
	};

	if (op != SNMP_OP_GET && op != SNMP_OP_GETNEXT)
		return SNMP_ERR_NOT_WRITEABLE;

	switch (which) {
	case LEAF_jailIndex:
		value->v.integer = jail->index;
		return SNMP_ERR_NOERROR;
	case LEAF_jailHost:
		return string_get (value, (u_char*)jail->host, -1);
	case LEAF_jailInOctets:
		value->v.counter64 = jail->in_octets;
		return SNMP_ERR_NOERROR;
	case LEAF_jailInPackets:
		value->v.counter64 = jail->in_packets;
		return SNMP_ERR_NOERROR;
	case LEAF_jailOutOctets:
		value->v.counter64 = jail->out_octets;
		return SNMP_ERR_NOERROR;
	case LEAF_jailOutPackets:
		value->v.counter64 = jail->out_packets;
		return SNMP_ERR_NOERROR;
	case LEAF_jailProcesses:
		value->v.integer = jail->n_processes;
		return SNMP_ERR_NOERROR;
	case LEAF_jailThreads:
		value->v.integer = jail->n_threads;
		return SNMP_ERR_NOERROR;
	case LEAF_jailCpuTime:
		value->v.integer = jail->cpu_time_total;
		return SNMP_ERR_NOERROR;
	case LEAF_jailDiskSpace:
		value->v.counter64 = jail->disk_space;
		return SNMP_ERR_NOERROR;
	case LEAF_jailDiskFiles:
		value->v.counter64 = jail->disk_files;
		return SNMP_ERR_NOERROR;
	default:
		ASSERT (0);
		return SNMP_ERR_NOSUCHNAME;
	};
}

int
op_jail (struct snmp_context *ctx, struct snmp_value *value,
         u_int sub, u_int iidx, enum snmp_op op)
{
	asn_subid_t which = value->var.subs[sub - 1];

	switch (op) {
	case SNMP_OP_GET:
		break;

	case SNMP_OP_SET:
		return SNMP_ERR_NOT_WRITEABLE;

	case SNMP_OP_ROLLBACK:
	case SNMP_OP_COMMIT:
		return SNMP_ERR_NOERROR;

	default:
		ASSERT(0);
		break;
    	};

	switch (which) {
	case LEAF_jailCount:
		value->v.integer = jaildat_count;
		break;

	default:
		ASSERT(0);
		break;
	};

	return SNMP_ERR_NOERROR;
}


/* -----------------------------------------------------------------------------
 * MODULE
 */

/* Called, when the module is to be unloaded after it was successfully loaded */
static int
module_fini (void)
{
	struct jaildat *jail;
	struct monitor *mon;

	if (reg_index)
		or_unregister (reg_index);

	if (kvm_handle)
		kvm_close (kvm_handle);

	if (network_filter)
		free (network_filter);
	network_filter = NULL;

	if (jaildat_by_id)
		hsh_free (jaildat_by_id);
	jaildat_by_id = NULL;

	if (jaildat_by_address)
		hsh_free (jaildat_by_address);
	jaildat_by_address = NULL;

	if (jaildat_by_host)
		hsh_free (jaildat_by_host);
	jaildat_by_host = NULL;

	if (jaildat_by_path)
		hsh_free (jaildat_by_path);
	jaildat_by_path = NULL;

	while ((jail = TAILQ_FIRST(&jaildats)) != NULL)
		jail_free (jail);

	while ((mon = TAILQ_FIRST(&monitors)) != NULL)
		monitor_free (mon);

	if (timer_refresh)
		timer_stop (timer_refresh);
	timer_refresh = NULL;

	if (timer_measure)
		timer_stop (timer_measure);
	timer_measure = NULL;

	return 0;
}

/* the initialisation function */
static int
module_init (struct lmodule *mod, int argc, char *argv[])
{
	int success = 0;
	char errbuf[_POSIX2_LINE_MAX];

	module = mod;

	if (argc != 0) {
		syslog (LOG_ERR, "bad number of arguments for %s", __func__);
		return EINVAL;
	}

	/* Open the kernel interface */
	kvm_handle = kvm_openfiles (_PATH_DEVNULL, _PATH_DEVNULL, _PATH_DEVNULL,
	                            O_RDONLY, errbuf);
	if (kvm_handle == NULL) {
		syslog (LOG_ERR, "couldn't open kvm interface: %s", errbuf);
		return ENOENT;
	}

	/* Setup all our own structures */

	network_filter = (u_char*)strdup ("ip or ip6");
	if (!network_filter)
		goto cleanup;

	jaildat_by_id = hsh_create ();
	if (!jaildat_by_id)
		goto cleanup;

	jaildat_by_host = hsh_create ();
	if (!jaildat_by_host)
		goto cleanup;

	jaildat_by_path = hsh_create ();
	if (!jaildat_by_path)
		goto cleanup;

	jaildat_by_address = hsh_create ();
	if (!jaildat_by_address)
		goto cleanup;

	success = 1;

cleanup:
	if (!success) {
		emsg ("error initializing: out of memory");
		module_fini ();
		return ENOMEM;
	}

	return 0;
}

/* Module is started */
static void
module_start (void)
{
	reg_index = or_register (&oid_jails, "The MIB for jail interface data.", module);

	jail_refresh_all (NULL);

	timer_refresh = timer_start_repeat (refresh_interval, refresh_interval,
	                                    jail_refresh_all, NULL, module);
	if (!timer_refresh)
		emsg ("couldn't setup timer to refresh jails");

	/* Wait for some time before first measure */
	timer_measure = timer_start (measure_interval / 5, measure_start, NULL, module);
	if (!timer_measure)
		emsg ("couldn't setup timer to measure jails");
}

const struct snmp_module config = {
	.comment =      "This module implements SNMP monitoring of jails",
	.init =         module_init,
	.start =        module_start,
	.fini =         module_fini,
	.tree =         jails_ctree,
	.tree_size =    jails_CTREE_SIZE,
};