-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathrfc3442.htm
409 lines (318 loc) · 14.2 KB
/
rfc3442.htm
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
<html>
<head>
<title>PfSense RFC 3442 Builder</title>
</head>
<body>
<div style="width: 800px; display: grid; grid-template-columns: 400px 400px; position: absolute; top: 20px; left: 50px; right: 0;">
<div style="grid-column: 1 / 3; grid-row: 1;">
<h2>RFC 3442 Builder for PfSense</h2>
</div>
<div style="grid-column: 1; grid-row: 2;">
<div>
<div>Network:</div>
<div style="padding-left: 30px;">
<span>
<select id="ddlSubnet" name="ddlSubnet" onchange="SubnetSelected();"></select>
</span>
<span style="display: inline-block; width: 12px; text-align: center; font-weight: bold;">/</span>
<span>
<select id="ddlOctet1" name="ddlOctet1"></select>
<select id="ddlOctet2" name="ddlOctet2"></select>
<select id="ddlOctet3" name="ddlOctet3"></select>
<select id="ddlOctet4" name="ddlOctet4"></select>
</span>
</div>
</div>
<div style="margin-top: 7px;">
<div>Gateway: </div>
<div style="padding-left: 30px;">
<span>
<select id="ddlGateway1" name="ddlGateway1"></select>
<select id="ddlGateway2" name="ddlGateway2"></select>
<select id="ddlGateway3" name="ddlGateway3"></select>
<select id="ddlGateway4" name="ddlGateway4"></select>
</span>
<span style="display: inline-block; width: 12px;"></span>
<span>
<button id="btnAdd" onclick="AddRoute();">Add</button>
</span>
</div>
</div>
</div>
<div style="grid-column: 2; grid-row: 2;">
<div style="width: 100%; display: grid; grid-template-columns: 300px 100px;">
<div style="grid-column: 1; grid-row: 1;">
<select id="routes" name="routes" size="5" style="width: 300px;">
</select>
</div>
<div style="grid-column: 2; grid-row: 1; padding-left: 5px;">
<button id="btnDel" onclick="RemoveRoute();">Remove<br/>Selected</button>
</div>
</div>
</div>
<div style="grid-column: 1; grid-row: 3"><!-- This space intentionally left blank --></div>
<div style="grid-column: 2; grid-row: 3; padding-top: 5px;">
<button onclick="Build();">Build</button>
</div>
<div style="grid-column: 1 / 3; grid-row: 4">
<div style="padding-top: 40px;">
<span>
<input id="Result" type="text" style="width: 700px;" />
</span>
<span style="padding-left: 5px;">
<button onclick="Import();">Import</button>
</span>
</div>
</div>
<div style="grid-column: 1 / 3; grid-row: 5; padding-top: 50px;">
<a href="https://www.rfc-editor.org/rfc/rfc3442">RFC 3442</a>
</div>
</div>
</body>
<script type="text/javascript">
let oct1 = document.querySelector("#ddlOctet1");
let oct2 = document.querySelector("#ddlOctet2");
let oct3 = document.querySelector("#ddlOctet3");
let oct4 = document.querySelector("#ddlOctet4");
let sub = document.querySelector("#ddlSubnet");
let gate1 = document.querySelector("#ddlGateway1");
let gate2 = document.querySelector("#ddlGateway2");
let gate3 = document.querySelector("#ddlGateway3");
let gate4 = document.querySelector("#ddlGateway4");
let routesBox = document.querySelector("#routes");
function SubnetSelected() {
let bits = parseInt(sub.value);
if(bits >= 25) {
oct1.disabled = "";
oct2.disabled = "";
oct3.disabled = "";
oct4.disabled = "";
}
else if(bits >= 17) {
oct1.disabled = "";
oct2.disabled = "";
oct3.disabled = "";
oct4.disabled = "disabled";
oct4.value = "0";
}
else if(bits >= 9) {
oct1.disabled = "";
oct2.disabled = "";
oct3.disabled = "disabled";
oct4.disabled = "disabled";
oct3.value = "0";
oct4.value = "0";
}
else if(bits >= 1) {
oct1.disabled = "";
oct2.disabled = "disabled";
oct3.disabled = "disabled";
oct4.disabled = "disabled";
oct2.value = "0";
oct3.value = "0";
oct4.value = "0";
}
else {
oct1.disabled = "disabled";
oct2.disabled = "disabled";
oct3.disabled = "disabled";
oct4.disabled = "disabled";
oct1.value = "0";
oct2.value = "0";
oct3.value = "0";
oct4.value = "0";
}
}
function AddRoute() {
let route = `${oct1.value}.${oct2.value}.${oct3.value}.${oct4.value} / ${sub.value} -> ${gate1.value}.${gate2.value}.${gate3.value}.${gate4.value}`
oct1.value = 0;
oct2.value = 0;
oct3.value = 0;
oct4.value = 0;
sub.value = 0;
gate1.value = 0;
gate2.value = 0;
gate3.value = 0;
gate4.value = 0;
let o = document.createElement("option");
o.text = route;
routesBox.appendChild(o);
SubnetSelected();
sub.focus();
}
function RemoveRoute() {
routesBox.remove(routesBox.selectedIndex);
}
function Build() {
let theHex = Array.apply(null, routesBox.options).map(function (r) {
let route = r.text.replace(" / ", ",").replace(" -> ", ",").split(",");
//Mask goes first
let mask = parseInt(route[1]);
let meaningfulOctets = 0;
if(mask >= 25) {
meaningfulOctets = 4;
}
else if (mask >= 17) {
meaningfulOctets = 3;
}
else if (mask >= 9) {
meaningfulOctets = 2;
}
else if (mask >= 1) {
meaningfulOctets = 1;
}
let hex = parseInt(route[1]).toString(16).padStart(2, "0");
//Followed by network (with significant octets only)
if(meaningfulOctets !== 0) {
hex += ":" + route[0].split(".").slice(0, meaningfulOctets).map(function (octet) {
return parseInt(octet).toString(16).padStart(2, "0");
}).join(":");
}
//Finally, the Gateway
hex += ":" + route[2].split(".").map(function (octet) {
return parseInt(octet).toString(16).padStart(2, "0");
}).join(":");
return hex;
}).join(":");
document.querySelector("#Result").value = theHex;
}
const octetRegex = /^[\dabcdef][\dabcdef]$/;
function Import() {
let data = document.querySelector("#Result").value.toLowerCase();
if(data.startsWith("0x")) {
data = data.slice(2);
}
if(data.indexOf(":") == -1) {
data = data.match(/.{2}/g).reverse();
} else {
data = data.split(":").reverse();
}
if(data.length < 6){
alert("Invalid input");
return;
}
let position = -1;
let routes = new Array();
while(data.length > 0) {
position++;
console.log(`Position ${position}. Attempting to parse network mask`);
//Mask is first
let mask = data.pop();
if(!/^(?:[01][\dabcdef])|(?:20)$/.test(mask)) {
alert(`Invalid input. Looking for a network mask at position ${position} but found invalid value '${mask}'. Check console log for debugging help`);
return;
}
console.log(`Found network mask: ${mask} (hex)`);
mask = parseInt(mask, 16).toString();
let network = new Array();
{
//Determine how many meaningful octets we expect based on the mask
let meaningfulOctets = 0;
if(mask >= 25) {
meaningfulOctets = 4;
}
else if (mask >= 17) {
meaningfulOctets = 3;
}
else if (mask >= 9) {
meaningfulOctets = 2;
}
else if (mask >= 1) {
meaningfulOctets = 1
}
if(mask == 0) {
console.log("Found default route.");
}
else {
console.log(`Based on mask of '${mask}' (dec), attempting to load ${meaningfulOctets} network octet(s)`);
}
if(data.length < meaningfulOctets) {
console.log(`Position: ${position}. Expected to load ${meaningfulOctets} but there are only ${data.length} data segments remaining.`)
alert(`Invalid input. Check console log for debugging help`);
return;
}
//Read the octets from the data stream
while(meaningfulOctets > 0) {
let octet = data.pop();
position++;
console.log(`Found ${octet}`);
if(!octetRegex.test(octet)) {
console.log(`Position: ${position}. ${octet} is not a valid ipv4 octet value!`);
alert("Invalid input. Check console log for debugging help");
return;
}
network.push(parseInt(octet, 16).toString());
meaningfulOctets--;
}
}
//Append zeroes until network is 4 octets
while(network.length < 4) {
network.push(0);
}
//convert to string
network = network.join(".");
console.log(`Loaded network: ${network}`);
//Now load the gateway (always 4 octets)
let gateway = new Array();
{
console.log("Attempting to load gateway from data stream")
if(data.length < 4) {
console.log(`Position: ${position}. Expected 4 octets for the gateway but found only ${data.length} data fields remaining`);
alert("Invalid input. Check console log for debugging help");
return;
}
for(let g = 0; g < 4; g++) {
let gOctet = data.pop();
position++;
console.log(`Found ${gOctet}`);
if(!octetRegex.test(gOctet)) {
console.log(`Position: ${position}. ${gOctet} is not a valid ipv4 octet value!`);
alert("Invalid input. Check console log for debugging help");
return;
}
gateway.push(parseInt(gOctet, 16).toString());
}
}
gateway = gateway.join(".");
console.log(`Loaded gateway: ${gateway}`);
let fullRoute = network + " / " + mask + " -> " + gateway;
console.log(`Done loading: ${fullRoute}`);
routes.push(fullRoute);
}
routesBox.innerHTML = "";
routes.forEach(function (route) {
let o = document.createElement("option");
o.text = route;
routesBox.appendChild(o);
});
}
function Start() {
function addOption(selectBox) {
var opt = document.createElement("option");
opt.value = i;
opt.text = i;
selectBox.appendChild(opt);
}
let i = 0;
while(i <= 255) {
if(i <= 223) {
addOption(oct1);
addOption(gate1);
}
if(i <= 32) {
addOption(sub);
}
addOption(oct2);
addOption(oct3);
addOption(oct4);
addOption(gate2);
addOption(gate3);
addOption(gate4);
i = i + 1;
}
SubnetSelected();
sub.focus();
}
window.addEventListener("load", (event) => { Start(); });
</script>
</html>