This repository has been archived by the owner on Oct 22, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
sethdlc-new.c
702 lines (557 loc) · 14.9 KB
/
sethdlc-new.c
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
/*
* sethdlc.c
*
* Copyright (C) 1999 - 2002 Krzysztof Halasa <[email protected]>
*
*/
/*
* See http://www.asterisk.org for more information about
* the Asterisk project. Please do not directly contact
* any of the maintainers of this project for assistance;
* the project provides a web site, mailing lists and IRC
* channels for your use.
*
* This program is free software, distributed under the terms of
* the GNU General Public License Version 2 as published by the
* Free Software Foundation. See the LICENSE file included with
* this program for more details.
*/
#include <errno.h>
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <asm/types.h>
#include <linux/hdlc.h>
#include <netinet/in.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <linux/if.h>
#include <linux/sockios.h>
#include "kernel/zaptel.h"
#if GENERIC_HDLC_VERSION != 4
#error Generic HDLC layer version mismatch, please get correct sethdlc.c
#endif
#if !defined(IF_PROTO_HDLC_ETH) || !defined(IF_PROTO_FR_ETH_PVC)
#warning "No kernel support for Ethernet over Frame Relay / HDLC, skipping it"
#endif
static struct ifreq req; /* for ioctl */
static int argc;
static char **argv;
int sock;
static void error(const char *format, ...) __attribute__ ((noreturn));
static void error(const char *format, ...)
{
va_list args;
va_start(args, format);
fprintf(stderr, "%s: ", req.ifr_name);
vfprintf(stderr, format, args);
va_end(args);
exit(1);
}
typedef struct {
const char *name;
const unsigned int value;
} parsertab;
static int checkkey(const char* name)
{
if (argc < 1)
return -1; /* no enough parameters */
if (strcmp(name, argv[0]))
return -1;
argc--;
argv++;
return 0;
}
static int checktab(parsertab *tab, unsigned int *value)
{
int i;
if (argc < 1)
return -1; /* no enough parameters */
for (i = 0; tab[i].name; i++)
if (!strcmp(tab[i].name, argv[0])) {
argc--;
argv++;
*value = tab[i].value;
return 0;
}
return -1; /* Not found */
}
static const char* tabstr(unsigned int value, parsertab *tab,
const char* unknown)
{
int i;
for (i = 0; tab[i].name; i++)
if (tab[i].value == value)
return tab[i].name;
return unknown; /* Not found */
}
static unsigned int match(const char* name, unsigned int *value,
unsigned int minimum, unsigned int maximum)
{
char test;
if (argc < 1)
return -1; /* no enough parameters */
if (name) {
if (strcmp(name, argv[0]))
return -1;
argc--;
argv++;
}
if (argc < 1)
error("Missing parameter\n");
if (sscanf(argv[0], "%u%c", value, &test) != 1)
error("Invalid parameter: %s\n", argv[0]);
if ((*value > maximum) || (*value < minimum))
error("Parameter out of range [%u - %u]: %u\n",
minimum, maximum, *value);
argc--;
argv++;
return 0;
}
static parsertab ifaces[] = {{ "v35", IF_IFACE_V35 },
{ "v24", IF_IFACE_V24 },
{ "x21", IF_IFACE_X21 },
{ "e1", IF_IFACE_E1 },
{ "t1", IF_IFACE_T1 },
{ NULL, 0 }};
static parsertab clocks[] = {{ "int", CLOCK_INT },
{ "ext", CLOCK_EXT },
{ "txint", CLOCK_TXINT },
{ "txfromrx", CLOCK_TXFROMRX },
{ NULL, 0 }};
static parsertab protos[] = {{ "hdlc", IF_PROTO_HDLC},
{ "cisco", IF_PROTO_CISCO},
{ "fr", IF_PROTO_FR},
{ "ppp", IF_PROTO_PPP},
{ "x25", IF_PROTO_X25},
#ifdef IF_PROTO_HDLC_ETH
{ "hdlc-eth", IF_PROTO_HDLC_ETH},
#endif
{ NULL, 0 }};
static parsertab hdlc_enc[] = {{ "nrz", ENCODING_NRZ },
{ "nrzi", ENCODING_NRZI },
{ "fm-mark", ENCODING_FM_MARK },
{ "fm-space", ENCODING_FM_SPACE },
{ "manchester", ENCODING_MANCHESTER },
{ NULL, 0 }};
static parsertab hdlc_par[] = {{ "no-parity", PARITY_NONE },
{ "crc16", PARITY_CRC16_PR1 },
{ "crc16-pr0", PARITY_CRC16_PR0 },
{ "crc16-itu", PARITY_CRC16_PR1_CCITT },
{ "crc16-itu-pr0", PARITY_CRC16_PR0_CCITT },
{ "crc32-itu", PARITY_CRC32_PR1_CCITT },
{ NULL, 0 }};
static parsertab lmi[] = {{ "none", LMI_NONE },
{ "ansi", LMI_ANSI },
{ "ccitt", LMI_CCITT },
{ NULL, 0 }};
static void set_iface(void)
{
int orig_argc = argc;
te1_settings te1;
memset(&te1, 0, sizeof(te1));
req.ifr_settings.type = IF_IFACE_SYNC_SERIAL;
while (argc > 0) {
if (req.ifr_settings.type == IF_IFACE_SYNC_SERIAL)
if (!checktab(ifaces, &req.ifr_settings.type))
continue;
if (!te1.clock_type)
if (!checkkey("clock")) {
if (!checktab(clocks, &te1.clock_type))
continue;
error("Invalid clock type\n");
}
if (!te1.clock_rate &&
(te1.clock_type == CLOCK_INT ||
te1.clock_type == CLOCK_TXINT))
if (!match("rate", &te1.clock_rate, 1, 0xFFFFFFFF))
continue;
if (!te1.loopback) {
if (!checkkey("loopback") ||
!checkkey("lb")) {
te1.loopback = 1;
continue;
}
}
/* slotmap goes here */
if (orig_argc == argc)
return; /* not an iface definition */
error("Invalid parameter: %s\n", argv[0]);
}
if (!te1.clock_rate &&
(te1.clock_type == CLOCK_INT ||
te1.clock_type == CLOCK_TXINT))
te1.clock_rate = 64000;
/* FIXME stupid hack, will remove it later */
req.ifr_settings.ifs_ifsu.te1 = &te1;
if (req.ifr_settings.type == IF_IFACE_E1 ||
req.ifr_settings.type == IF_IFACE_T1)
req.ifr_settings.size = sizeof(te1_settings);
else
req.ifr_settings.size = sizeof(sync_serial_settings);
if (ioctl(sock, SIOCWANDEV, &req))
error("Unable to set interface information: %s\n",
strerror(errno));
exit(0);
}
static void set_proto_fr(void)
{
unsigned int lmi_type = 0;
fr_proto fr;
memset(&fr, 0, sizeof(fr));
while (argc > 0) {
if (!lmi_type)
if (!checkkey("lmi")) {
if (!checktab(lmi, &lmi_type))
continue;
error("Invalid LMI type: %s\n",
argv[0]);
}
if (lmi_type && lmi_type != LMI_NONE) {
if (!fr.dce)
if (!checkkey("dce")) {
fr.dce = 1;
continue;
}
if (!fr.t391)
if (!match("t391", &fr.t391,
1, 1000))
continue;
if (!fr.t392)
if (!match("t392", &fr.t392,
1, 1000))
continue;
if (!fr.n391)
if (!match("n391", &fr.n391,
1, 1000))
continue;
if (!fr.n392)
if (!match("n392", &fr.n392,
1, 1000))
continue;
if (!fr.n393)
if (!match("n393", &fr.n393,
1, 1000))
continue;
}
error("Invalid parameter: %s\n", argv[0]);
}
/* polling verification timer*/
if (!fr.t391) fr.t391 = 10;
/* link integrity verification polling timer */
if (!fr.t392) fr.t392 = 15;
/* full status polling counter*/
if (!fr.n391) fr.n391 = 6;
/* error threshold */
if (!fr.n392) fr.n392 = 3;
/* monitored events count */
if (!fr.n393) fr.n393 = 4;
if (!lmi_type)
fr.lmi = LMI_DEFAULT;
else
fr.lmi = lmi_type;
req.ifr_settings.ifs_ifsu.fr = &fr;
req.ifr_settings.size = sizeof(fr);
if (ioctl(sock, SIOCWANDEV, &req))
error("Unable to set FR protocol information: %s\n",
strerror(errno));
}
static void set_proto_hdlc(int eth)
{
unsigned int enc = 0, par = 0;
raw_hdlc_proto raw;
memset(&raw, 0, sizeof(raw));
while (argc > 0) {
if (!enc)
if (!checktab(hdlc_enc, &enc))
continue;
if (!par)
if (!checktab(hdlc_par, &par))
continue;
error("Invalid parameter: %s\n", argv[0]);
}
if (!enc)
raw.encoding = ENCODING_DEFAULT;
else
raw.encoding = enc;
if (!par)
raw.parity = ENCODING_DEFAULT;
else
raw.parity = par;
req.ifr_settings.ifs_ifsu.raw_hdlc = &raw;
req.ifr_settings.size = sizeof(raw);
if (ioctl(sock, SIOCWANDEV, &req))
error("Unable to set HDLC%s protocol information: %s\n",
eth ? "-ETH" : "", strerror(errno));
}
static void set_proto_cisco(void)
{
cisco_proto cisco;
memset(&cisco, 0, sizeof(cisco));
while (argc > 0) {
if (!cisco.interval)
if (!match("interval", &cisco.interval,
1, 100))
continue;
if (!cisco.timeout)
if (!match("timeout", &cisco.timeout,
1, 100))
continue;
error("Invalid parameter: %s\n",
argv[0]);
}
if (!cisco.interval)
cisco.interval = 10;
if (!cisco.timeout)
cisco.timeout = 25;
req.ifr_settings.ifs_ifsu.cisco = &cisco;
req.ifr_settings.size = sizeof(cisco);
if (ioctl(sock, SIOCWANDEV, &req))
error("Unable to set Cisco HDLC protocol information: %s\n",
strerror(errno));
}
static void set_proto(void)
{
if (checktab(protos, &req.ifr_settings.type))
return;
switch(req.ifr_settings.type) {
case IF_PROTO_HDLC: set_proto_hdlc(0); break;
#ifdef IF_PROTO_HDLC_ETH
case IF_PROTO_HDLC_ETH: set_proto_hdlc(1); break;
#endif
case IF_PROTO_CISCO: set_proto_cisco(); break;
case IF_PROTO_FR: set_proto_fr(); break;
case IF_PROTO_PPP:
case IF_PROTO_X25:
req.ifr_settings.ifs_ifsu.sync = NULL; /* FIXME */
req.ifr_settings.size = 0;
if (!ioctl(sock, SIOCWANDEV, &req))
break;
error("Unable to set %s protocol information: %s\n",
req.ifr_settings.type == IF_PROTO_PPP
? "PPP" : "X.25", strerror(errno));
default: error("Unknown protocol %u\n", req.ifr_settings.type);
}
if (argc > 0)
error("Unexpected parameter: %s\n", argv[0]);
close(sock);
exit(0);
}
static void set_pvc(void)
{
char *op = argv[0];
parsertab ops[] = {{ "create", IF_PROTO_FR_ADD_PVC },
{ "delete", IF_PROTO_FR_DEL_PVC },
{ NULL, 0 }};
fr_proto_pvc pvc;
memset(&pvc, 0, sizeof(pvc));
if (checktab(ops, &req.ifr_settings.type))
return;
#ifdef IF_PROTO_FR_ETH_PVC
if (!match("ether", &pvc.dlci, 0, 1023)) {
if (req.ifr_settings.type == IF_PROTO_FR_ADD_PVC)
req.ifr_settings.type = IF_PROTO_FR_ADD_ETH_PVC;
else
req.ifr_settings.type = IF_PROTO_FR_DEL_ETH_PVC;
} else
#endif
if (match(NULL, &pvc.dlci, 0, 1023))
return;
if (argc != 0)
return;
req.ifr_settings.ifs_ifsu.fr_pvc = &pvc;
req.ifr_settings.size = sizeof(pvc);
if (ioctl(sock, SIOCWANDEV, &req))
error("Unable to %s PVC: %s\n", op, strerror(errno));
exit(0);
}
static void private(void)
{
if (argc < 1)
return;
if (!strcmp(argv[0], "private")) {
if (argc != 1)
return;
if (ioctl(sock, SIOCDEVPRIVATE, &req))
error("SIOCDEVPRIVATE: %s\n", strerror(errno));
exit(0);
}
}
static void show_port(void)
{
const char *s;
char buffer[128];
const te1_settings *te1 = (void*)buffer;
const raw_hdlc_proto *raw = (void*)buffer;
const cisco_proto *cisco = (void*)buffer;
const fr_proto *fr = (void*)buffer;
#ifdef IF_PROTO_FR_PVC
const fr_proto_pvc_info *pvc = (void*)buffer;
#endif
req.ifr_settings.ifs_ifsu.sync = (void*)buffer; /* FIXME */
printf("%s: ", req.ifr_name);
req.ifr_settings.size = sizeof(buffer);
req.ifr_settings.type = IF_GET_IFACE;
if (ioctl(sock, SIOCWANDEV, &req))
if (errno != EINVAL) {
printf("unable to get interface information: %s\n",
strerror(errno));
close(sock);
exit(1);
}
/* Get and print physical interface settings */
if (req.ifr_settings.type == IF_IFACE_SYNC_SERIAL)
s = ""; /* Unspecified serial interface */
else
s = tabstr(req.ifr_settings.type, ifaces, NULL);
if (!s)
printf("unknown interface 0x%x\n", req.ifr_settings.type);
else {
if (*s)
printf("interface %s ", s);
printf("clock %s", tabstr(te1->clock_type, clocks,
"type unknown"));
if (te1->clock_type == CLOCK_INT ||
te1->clock_type == CLOCK_TXINT)
printf(" rate %u", te1->clock_rate);
if (te1->loopback)
printf(" loopback");
if (req.ifr_settings.type == IF_IFACE_E1 ||
req.ifr_settings.type == IF_IFACE_T1) {
unsigned int u;
printf(" slotmap ");
for (u = te1->slot_map; u != 0; u /= 2)
printf("%u", u % 2);
}
printf("\n");
}
/* Get and print protocol settings */
do {
printf("\t");
req.ifr_settings.size = sizeof(buffer);
req.ifr_settings.type = IF_GET_PROTO;
if (ioctl(sock, SIOCWANDEV, &req)) {
if (errno == EINVAL)
printf("no protocol set\n");
else
printf("unable to get protocol information: "
"%s\n", strerror(errno));
break;
}
switch(req.ifr_settings.type) {
case IF_PROTO_FR:
printf("protocol fr lmi %s",
tabstr(fr->lmi, lmi, "unknown"));
if (fr->lmi == LMI_ANSI ||
fr->lmi == LMI_CCITT)
printf("%s t391 %u t392 %u n391 %u n392 %u "
"n393 %u\n",
fr->dce ? " dce" : "",
fr->t391,
fr->t392,
fr->n391,
fr->n392,
fr->n393);
else
putchar('\n');
break;
#ifdef IF_PROTO_FR_PVC
case IF_PROTO_FR_PVC:
printf("Frame-Relay PVC: DLCI %u, master device %s\n",
pvc->dlci, pvc->master);
break;
#endif
#ifdef IF_PROTO_FR_ETH_PVC
case IF_PROTO_FR_ETH_PVC:
printf("Frame-Relay PVC (Ethernet emulation): DLCI %u,"
" master device %s\n", pvc->dlci, pvc->master);
break;
#endif
case IF_PROTO_HDLC:
printf("protocol hdlc %s %s\n",
tabstr(raw->encoding, hdlc_enc, "unknown"),
tabstr(raw->parity, hdlc_par, "unknown"));
break;
#ifdef IF_PROTO_HDLC_ETH
case IF_PROTO_HDLC_ETH:
printf("protocol hdlc-eth %s %s\n",
tabstr(raw->encoding, hdlc_enc, "unknown"),
tabstr(raw->parity, hdlc_par, "unknown"));
break;
#endif
case IF_PROTO_CISCO:
printf("protocol cisco interval %u timeout %u\n",
cisco->interval,
cisco->timeout);
break;
case IF_PROTO_PPP:
printf("protocol ppp\n");
break;
case IF_PROTO_X25:
printf("protocol x25\n");
break;
default:
printf("unknown protocol %u\n", req.ifr_settings.type);
}
}while(0);
close(sock);
exit(0);
}
static void usage(void)
{
fprintf(stderr, "sethdlc version 1.15\n"
"Copyright (C) 2000 - 2003 Krzysztof Halasa <[email protected]>\n"
"\n"
"Usage: sethdlc INTERFACE [PHYSICAL] [clock CLOCK] [LOOPBACK] "
"[slotmap SLOTMAP]\n"
" sethdlc INTERFACE [PROTOCOL]\n"
" sethdlc INTERFACE create | delete"
#ifdef IF_PROTO_FR_ETH_PVC
" [ether]"
#endif
" DLCI\n"
" sethdlc INTERFACE private...\n"
"\n"
"PHYSICAL := v24 | v35 | x21 | e1 | t1\n"
"CLOCK := int [rate RATE] | ext | txint [rate RATE] | txfromrx\n"
"LOOPBACK := loopback | lb\n"
"\n"
"PROTOCOL := hdlc [ENCODING] [PARITY] |\n"
#ifdef IF_PROTO_HDLC_ETH
" hdlc-eth [ENCODING] [PARITY] |\n"
#endif
" cisco [interval val] [timeout val] |\n"
" fr [lmi LMI] |\n"
" ppp |\n"
" x25\n"
"\n"
"ENCODING := nrz | nrzi | fm-mark | fm-space | manchester\n"
"PARITY := no-parity | crc16 | crc16-pr0 | crc16-itu | crc16-itu-pr0 | crc32-itu\n"
"LMI := none | ansi [LMI_SPEC] | ccitt [LMI_SPEC]\n"
"LMI_SPEC := [dce] [t391 val] [t392 val] [n391 val] [n392 val] [n393 val]\n");
exit(0);
}
int main(int arg_c, char *arg_v[])
{
argc = arg_c;
argv = arg_v;
if (argc <= 1)
usage();
sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
if (sock < 0)
error("Unable to create socket: %s\n", strerror(errno));
zap_copy_string(req.ifr_name, argv[1], sizeof(req.ifr_name)); /* Device name */
if (argc == 2)
show_port();
argc -= 2;
argv += 2;
set_iface();
set_proto();
set_pvc();
private();
close(sock);
usage();
exit(0);
}