Project

General

Profile

Download (4.24 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
 * prefixes.php
4
 *
5
 * part of pfSense (https://www.pfsense.org)
6
 * Copyright (c) 2004-2013 BSD Perimeter
7
 * Copyright (c) 2013-2016 Electric Sheep Fencing
8
 * Copyright (c) 2014-2025 Rubicon Communications, LLC (Netgate)
9
 * All rights reserved.
10
 *
11
 * Licensed under the Apache License, Version 2.0 (the "License");
12
 * you may not use this file except in compliance with the License.
13
 * You may obtain a copy of the License at
14
 *
15
 * http://www.apache.org/licenses/LICENSE-2.0
16
 *
17
 * Unless required by applicable law or agreed to in writing, software
18
 * distributed under the License is distributed on an "AS IS" BASIS,
19
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20
 * See the License for the specific language governing permissions and
21
 * limitations under the License.
22
 */
23

    
24
require_once('system.inc');
25
require_once('util.inc');
26

    
27
$leases_file = "/var/dhcpd/var/db/dhcpd6.leases";
28
if (!file_exists($leases_file)) {
29
	exit(1);
30
}
31

    
32
$fd = fopen($leases_file, 'r');
33

    
34
$duid_arr = array();
35
while (( $line = fgets($fd, 4096)) !== false) {
36
	// echo "$line";
37

    
38
	/* Originally: preg_match("/^(ia-[np][ad])[ ]+\"(.*?)\"/i", $line, $duidmatch)
39
	   That is: \"(.*?)\"
40
	   , which is a non-greedy matching. However that does not go well with the legal
41
	   substring \" in the IAID+DUID lease string format of ISC DHCPDv6,
42
	   because it truncates before we reach the end of the IAID+DUID string!
43
	   Instead we use: \"(.*)\"
44
	   (Might fail if content of the lease file is not well formed.)
45

    
46
	   Maybe someone would argue to e.g. use \"(.*?)\"[ \t]*{[ \t]*$
47
	   instead
48
	   (Either we get a valid result or nothing at all.)
49
	   , but I'll leave it to others to decide! */
50
	if (preg_match("/^(ia-[np][ad])[ ]+\"(.*)\"/i", $line, $duidmatch)) {
51
		$type = $duidmatch[1];
52
		$duid = extract_duid($duidmatch[2]);
53
		continue;
54
	}
55

    
56
	/* is it active? otherwise just discard */
57
	if (preg_match("/binding state active/i", $line, $activematch)) {
58
		$active = true;
59
		continue;
60
	}
61

    
62
	if (preg_match("/iaaddr[ ]+([0-9a-f:]+)[ ]+/i", $line, $addressmatch)) {
63
		$ia_na = $addressmatch[1];
64
		continue;
65
	}
66

    
67
	if (preg_match("/iaprefix[ ]+([0-9a-f:\/]+)[ ]+/i", $line, $prefixmatch)) {
68
		$ia_pd = $prefixmatch[1];
69
		continue;
70
	}
71

    
72
	/* closing bracket */
73
	if (preg_match("/^}/i", $line)) {
74
		if (isset($duid) && $duid !== false && $active === true) {
75
			switch ($type) {
76
				case "ia-na":
77
					$duid_arr[$duid][$type] = $ia_na;
78
					break;
79
				case "ia-pd":
80
					$duid_arr[$duid][$type] = $ia_pd;
81
					break;
82
				default:
83
					break;
84
			}
85
		}
86
		unset($type);
87
		unset($duid);
88
		unset($active);
89
		unset($ia_na);
90
		unset($ia_pd);
91
		continue;
92
	}
93
}
94
fclose($fd);
95

    
96
$routes = array();
97
foreach ($duid_arr as $entry) {
98
	if (!empty($entry['ia-pd'])) {
99
		$routes[$entry['ia-na']] = $entry['ia-pd'];
100
	}
101
}
102

    
103
// echo "add routes\n";
104
if (count($routes) > 0) {
105
	foreach ($routes as $address => $prefix) {
106
		route_add_or_change($prefix, $address);
107
	}
108
}
109

    
110
/* get log from dhcpd */
111
$dhcpdlogfile = "/var/log/dhcpd.log";
112
$expires = array();
113
if (file_exists($dhcpdlogfile)) {
114
	$fd = popen(system_log_get_cat() . ' ' . sort_related_log_files($dhcpdlogfile, true, true), 'r');
115
	while (($line = fgets($fd)) !== false) {
116
		//echo $line;
117
		if (preg_match("/releases[ ]+prefix[ ]+([0-9a-f:]+\/[0-9]+)/i", $line, $expire)) {
118
			if (in_array($expire[1], $routes)) {
119
				continue;
120
			}
121
			if (!in_array($expire[1], $expires)) {
122
				$expires[] = $expire[1];
123
			}
124
		}
125
	}
126
	pclose($fd);
127
}
128

    
129
// echo "remove routes\n";
130
if (count($expires) > 0) {
131
	foreach ($expires as $prefix) {
132
		route_del($prefix);
133
	}
134
}
135

    
136
/* handle quotify_buf - https://gitlab.isc.org/isc-projects/dhcp/-/blob/master/common/print.c */
137
function extract_duid($ia_string) {
138
	for ($i = 0, $iaid_counter = 0, $len = strlen($ia_string); $i < $len && $iaid_counter < 4; $i++, $iaid_counter++) {
139
		if ($ia_string[$i] !== '\\') {
140
			continue;
141
		}
142
		else if ($len - $i >= 2) {
143
			if (($ia_string[$i+1] === '\\') || ($ia_string[$i+1] === '"')) {
144
				$i += 1;
145
				continue;
146
			}
147
			else if ($len - $i >= 4) {
148
				if (preg_match('/[0-3][0-7]{2}/', substr($ia_string, $i+1, 3))) {
149
					$i += 3;
150
					continue;
151
				}
152
			}
153
		}
154

    
155
		return false;
156
	}
157

    
158
	/* Return anything after the first 4 octets! */
159
	if ($iaid_counter === 4) {
160
		/* substr returns false when $len == $i */
161
		return substr($ia_string, $i);
162
	}
163

    
164
	return false;
165
}
166

    
167
?>
(26-26/38)