-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprefix_validator_v6.html
More file actions
433 lines (364 loc) · 16.4 KB
/
prefix_validator_v6.html
File metadata and controls
433 lines (364 loc) · 16.4 KB
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>IPv6 Prefix Validator</title>
<style>
body {
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
background-color: #18191a;
color: #e4e6eb;
display: flex;
justify-content: center;
align-items: flex-start;
min-height: 100vh;
margin: 0;
padding: 2rem;
}
.container {
background-color: #242526;
padding: 2rem;
border-radius: 8px;
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
width: 100%;
max-width: 600px;
text-align: center;
}
h1 {
margin-top: 0;
color: #e4e6eb;
}
.input-group {
margin-bottom: 1rem;
text-align: left;
}
.input-group label {
display: block;
margin-bottom: 0.5rem;
font-weight: 500;
}
input[type="text"] {
width: 100%;
padding: 0.75rem;
border: 1px solid #404040;
border-radius: 6px;
font-size: 1rem;
box-sizing: border-box;
background-color: #3a3b3c;
color: #e4e6eb;
}
button {
width: 100%;
padding: 0.75rem;
border: none;
border-radius: 6px;
background-color: #1877f2;
color: #fff;
font-size: 1.1rem;
font-weight: bold;
cursor: pointer;
transition: background-color 0.2s;
}
button:hover {
background-color: #166fe5;
}
.home-button {
position: absolute;
top: 1rem;
left: 1rem;
padding: 0.5rem 1rem;
background-color: #3a3b3c;
color: #e4e6eb;
text-decoration: none;
border-radius: 6px;
font-size: 0.9rem;
font-weight: 500;
border: 1px solid #404040;
transition: background-color 0.2s;
}
.home-button:hover {
background-color: #4e4f50;
text-decoration: none;
}
#results {
margin-top: 1.5rem;
padding: 1rem;
background-color: #3a3b3c;
border: 1px solid #404040;
border-radius: 6px;
text-align: left;
min-height: 50px;
}
.results-section {
margin-bottom: 1rem;
padding-bottom: 1rem;
border-bottom: 1px solid #404040;
}
.results-section:last-child {
margin-bottom: 0;
padding-bottom: 0;
border-bottom: none;
}
.results-section h4 {
margin-top: 0;
margin-bottom: 0.5rem;
color: #e4e6eb;
}
.ip-list {
list-style-type: none;
padding-left: 0;
margin: 0;
font-family: "Courier New", Courier, monospace;
font-size: 0.9rem;
}
.status {
font-weight: bold;
padding: 0.5rem;
border-radius: 4px;
text-align: center;
}
.status-valid {
background-color: #2e4a2f;
color: #6aba6e;
}
.status-invalid {
background-color: #5a2a2a;
color: #ff8a80;
}
</style>
</head>
<body>
<a href="index.html" class="home-button">← Home</a>
<div class="container">
<h1>IPv6 Prefix Validator</h1>
<div class="input-group">
<label for="prefix">IPv6 Prefix</label>
<input type="text" id="prefix" placeholder="e.g., 2001:db8:1234:5678::/127">
</div>
<button id="checkButton">Check Prefix</button>
<div id="results"></div>div>
</div>
<script>
document.addEventListener('DOMContentLoaded', () => {
const prefixInput = document.getElementById('prefix');
const checkButton = document.getElementById('checkButton');
const resultsDiv = document.getElementById('results');
// --- IPv6 Helper Functions ---
const ipv6ToBigInt = (ip) => {
ip = ip.toLowerCase();
if (ip === '::') {
return 0n;
}
// Split on :: if present
if (ip.includes('::')) {
const parts = ip.split('::');
const left = parts[0] ? parts[0].split(':').filter(p => p !== '') : [];
const right = parts[1] ? parts[1].split(':').filter(p => p !== '') : [];
// Calculate missing zero groups
const missing = 8 - left.length - right.length;
const middle = Array(missing).fill('0');
// Combine all parts
const allParts = [...left, ...middle, ...right];
let result = 0n;
for (let i = 0; i < 8; i++) {
const val = parseInt(allParts[i] || '0', 16);
result = (result << 16n) + BigInt(val);
}
return result;
} else {
// No :: present, split normally
const parts = ip.split(':');
if (parts.length !== 8) return 0n;
let result = 0n;
for (let i = 0; i < 8; i++) {
const val = parseInt(parts[i], 16);
result = (result << 16n) + BigInt(val);
}
return result;
}
};
const bigIntToIPv6 = (bigint) => {
const parts = [];
let temp = bigint;
for (let i = 0; i < 8; i++) {
parts.unshift(Number(temp & 0xFFFFn).toString(16));
temp = temp >> 16n;
}
return parts.join(':');
};
const compressIPv6 = (ip) => {
// Remove leading zeros
let parts = ip.split(':').map(p => p.replace(/^0+/, '') || '0');
// Find longest run of zeros
let maxStart = -1, maxLen = 0;
let curStart = -1, curLen = 0;
for (let i = 0; i < parts.length; i++) {
if (parts[i] === '0') {
if (curStart === -1) curStart = i;
curLen++;
} else {
if (curLen > maxLen && curLen > 1) {
maxStart = curStart;
maxLen = curLen;
}
curStart = -1;
curLen = 0;
}
}
// Check final run
if (curLen > maxLen && curLen > 1) {
maxStart = curStart;
maxLen = curLen;
}
// Apply compression
if (maxLen > 1) {
const before = parts.slice(0, maxStart);
const after = parts.slice(maxStart + maxLen);
if (before.length === 0 && after.length === 0) return '::';
if (before.length === 0) return '::' + after.join(':');
if (after.length === 0) return before.join(':') + '::';
return before.join(':') + '::' + after.join(':');
}
return parts.join(':');
};
const isValidIPv6 = (ip) => {
try {
const bigint = ipv6ToBigInt(ip);
return bigint >= 0n;
} catch (e) {
return false;
}
};
// --- Reserved IPv6 Ranges Logic ---
const testIsReservedIPv6 = (ip) => {
const ipBigInt = ipv6ToBigInt(ip);
// Link-local (fe80::/10)
const linkLocalStart = ipv6ToBigInt('fe80::');
const linkLocalEnd = ipv6ToBigInt('febf:ffff:ffff:ffff:ffff:ffff:ffff:ffff');
if (ipBigInt >= linkLocalStart && ipBigInt <= linkLocalEnd) return "LINK-LOCAL";
// Loopback (::1/128)
if (ipBigInt === 1n) return "LOOPBACK";
// Unspecified (::0/128)
if (ipBigInt === 0n) return "UNSPECIFIED";
// Multicast (ff00::/8)
const multicastStart = ipv6ToBigInt('ff00::');
const multicastEnd = ipv6ToBigInt('ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff');
if (ipBigInt >= multicastStart && ipBigInt <= multicastEnd) return "MULTICAST";
// Documentation (2001:db8::/32)
const docStart = ipv6ToBigInt('2001:db8::');
const docEnd = ipv6ToBigInt('2001:db8:ffff:ffff:ffff:ffff:ffff:ffff');
if (ipBigInt >= docStart && ipBigInt <= docEnd) return "DOCUMENTATION";
// Unique Local (fc00::/7)
const ulStart = ipv6ToBigInt('fc00::');
const ulEnd = ipv6ToBigInt('fdff:ffff:ffff:ffff:ffff:ffff:ffff:ffff');
if (ipBigInt >= ulStart && ipBigInt <= ulEnd) return "UNIQUE-LOCAL";
return "GLOBAL";
};
// --- Main Check Logic ---
checkButton.addEventListener('click', () => {
resultsDiv.innerHTML = ''; // Clear previous results
const input_prefix = prefixInput.value.trim();
// --- Input Validation ---
if (!input_prefix) {
resultsDiv.innerHTML = '<p class="status status-invalid">Error: IPv6 prefix is required.</p>';
return;
}
const prefix_parts = input_prefix.split('/');
if (prefix_parts.length !== 2) {
resultsDiv.innerHTML = '<p class="status status-invalid">Error: Invalid IPv6/PREFIX format.</p>';
return;
}
const input_ip = prefix_parts[0];
const input_cidr = parseInt(prefix_parts[1], 10);
if (!isValidIPv6(input_ip)) {
resultsDiv.innerHTML = '<p class="status status-invalid">Error: Invalid IPv6 address format.</p>';
return;
}
// Check if prefix length is valid for IPv6 prefixes
const validPrefixLengths = [124, 125, 126, 127];
if (!validPrefixLengths.includes(input_cidr)) {
resultsDiv.innerHTML = '<p class="status status-invalid">Error: Invalid IPv6 prefix length. Only /124, /125, /126, and /127 are valid for IPv6 prefixes.</p>';
return;
}
// --- Main Processing ---
const input_ip_bigint = ipv6ToBigInt(input_ip);
// Validate that the input IP is actually a valid network address for the given CIDR
const prefix_mask = ~((1n << BigInt(128 - input_cidr)) - 1n);
const calculated_net_bigint = input_ip_bigint & prefix_mask;
if (input_ip_bigint !== calculated_net_bigint) {
const correct_network = compressIPv6(bigIntToIPv6(calculated_net_bigint));
resultsDiv.innerHTML = `<p class="status status-invalid">Error: Invalid CIDR block. The IP address '${input_ip}' is not a valid network address for /${input_cidr}. The correct network address should be '${correct_network}/${input_cidr}'.</p>`;
return;
}
// Calculate the containing /64 subnet
const subnet_mask = 0xFFFFFFFFFFFFFFFF0000000000000000n; // /64 mask
const containing_net_bigint = input_ip_bigint & subnet_mask;
const containing_net_ip = compressIPv6(bigIntToIPv6(containing_net_bigint));
const prefix_type = testIsReservedIPv6(input_ip);
if (["LOOPBACK", "UNSPECIFIED", "MULTICAST"].includes(prefix_type)) {
resultsDiv.innerHTML = `<p class="status status-invalid">Result: Prefix ${input_prefix} is INVALID because it belongs to a reserved unroutable namespace (${prefix_type}).</p>`;
return;
}
// Use the validated network address
const prefix_net_bigint = calculated_net_bigint;
const prefix_size = 2n ** BigInt(128 - input_cidr);
const prefix_end_bigint = prefix_net_bigint + prefix_size - 1n;
// Azure reserved IPs in /64 subnet
const azure_reserved_ips = [
containing_net_bigint + 0n, // ::0 (network)
containing_net_bigint + 1n, // ::1 (gateway)
containing_net_bigint + 2n, // ::2 (reserved)
containing_net_bigint + 3n, // ::3 (reserved)
containing_net_bigint + 0xffffffn // ::ffffff (broadcast)
];
// Check if any IP in the prefix range conflicts with Azure reserved IPs
let is_invalid = false;
let conflicting_ip = '';
for (let i = prefix_net_bigint; i <= prefix_end_bigint; i++) {
if (azure_reserved_ips.includes(i)) {
is_invalid = true;
conflicting_ip = compressIPv6(bigIntToIPv6(i));
break;
}
}
// Check if prefix is valid for NAT via DHCP (must start immediately after ::3)
const dhcp_start = containing_net_bigint + 4n; // ::4
const is_dhcp_valid = prefix_net_bigint === dhcp_start;
let outputHTML = '';
outputHTML += `<div class="results-section"><h4>Input Details</h4><p><strong>Input Prefix:</strong> ${input_prefix}<br><strong>Containing /64 Subnet:</strong> ${containing_net_ip}/64<br><strong>Prefix Type:</strong> ${prefix_type}</p></div>`;
// Show Azure reserved IPs
const reserved_ips_display = azure_reserved_ips.map(ip => compressIPv6(bigIntToIPv6(ip)));
outputHTML += `<div class="results-section"><h4>Azure Reserved IPs in /64 Subnet</h4><ul class="ip-list"><li>${reserved_ips_display.join('</li><li>')}</li></ul></div>`;
// Generate IP list for the prefix
let scanned_ips_html = '';
const max_ips_to_show = 16; // Limit display for larger prefixes
let ip_count = 0;
for (let i = prefix_net_bigint; i <= prefix_end_bigint && ip_count < max_ips_to_show; i++, ip_count++) {
const current_ip = compressIPv6(bigIntToIPv6(i));
scanned_ips_html += `<li>${current_ip}</li>`;
}
if (prefix_size > BigInt(max_ips_to_show)) {
scanned_ips_html += `<li>... and ${prefix_size - BigInt(max_ips_to_show)} more addresses</li>`;
}
outputHTML += `<div class="results-section"><h4>Prefix Range (${prefix_size} addresses)</h4><ul class="ip-list">${scanned_ips_html}</ul></div>`;
// Final validation result
let final_result_html = '';
if (is_invalid) {
final_result_html = `<p class="status status-invalid">Result: Prefix ${input_prefix} is a ${prefix_type} prefix and is INVALID because it contains Azure reserved IP ${conflicting_ip}.</p>`;
} else {
let dhcp_status = '';
if (is_dhcp_valid) {
dhcp_status = ' and is VALID for NAT via DHCP (starts immediately after ::3)';
} else {
dhcp_status = ' but is NOT VALID for NAT via DHCP (must start at ::4)';
}
final_result_html = `<p class="status status-valid">Result: Prefix ${input_prefix} is a ${prefix_type} prefix and is VALID for assignment${dhcp_status}.</p>`;
}
outputHTML += `<div class="results-section"><h4>Final Result</h4>${final_result_html}</div>`;
resultsDiv.innerHTML = outputHTML;
});
});
</script>
</body>
</html>