summaryrefslogtreecommitdiffstats
path: root/sbin/atm/atmconfig/natm.c
blob: a383f8fbf9245cce07da335491c6c5bda5c3d33f (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
/*
 * Copyright (c) 2003
 *	Fraunhofer Institute for Open Communication Systems (FhG Fokus).
 * 	All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. 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.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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.
 *
 * Author: Hartmut Brandt <harti@freebsd.org>
 */
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

#include <sys/types.h>
#include <sys/socket.h>
#include <sys/sysctl.h>
#include <net/if.h>
#include <net/if_var.h>
#include <net/if_mib.h>
#include <net/if_atm.h>
#include <net/if_dl.h>
#include <net/route.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include "atmconfig.h"
#include "private.h"
#include "diag.h"

static void natm_add(int, char *[]);
static void natm_delete(int, char *[]);
static void natm_show(int, char *[]);

const struct cmdtab natm_tab[] = {
	{ "add",	NULL,		natm_add },
	{ "delete",	NULL,		natm_delete },
	{ "show",	NULL,		natm_show },
	{ NULL, 	NULL, 		NULL }
};

/*
 * Structure to hold a route
 */
struct natm_route {
	TAILQ_ENTRY(natm_route) link;
	struct in_addr	host;
	struct diagif	*aif;
	u_int		flags;
	int		llcsnap;
	u_int		vpi, vci;
	u_int		traffic;
	u_int		pcr, scr, mbs, icr, mcr;
	u_int		tbe, nrm, trm, adtf, rif, rdf, cdf;
};
static TAILQ_HEAD(, natm_route) natm_route_list =
    TAILQ_HEAD_INITIALIZER(natm_route_list);

static void
store_route(struct rt_msghdr *rtm)
{
	u_int i;
	struct natm_route *r;
	char *cp;
	struct sockaddr *sa;
	struct sockaddr_in *sain;
	struct sockaddr_dl *sdl;
	struct diagif *aif;
	u_int n;

	r = malloc(sizeof(*r));
	if (r == NULL)
		err(1, "allocate route");

	r->flags = rtm->rtm_flags;
	cp = (char *)(rtm + 1);
	for (i = 1; i != 0; i <<= 1) {
		if (rtm->rtm_addrs & i) {
			sa = (struct sockaddr *)cp;
			cp += roundup(sa->sa_len, sizeof(long));
			switch (i) {

			  case RTA_DST:
				if (sa->sa_family != AF_INET) {
					warnx("RTA_DST not AF_INET %u", sa->sa_family);
					goto fail;
				}
				sain = (struct sockaddr_in *)(void *)sa;
				if (sain->sin_len < 4)
					r->host.s_addr = INADDR_ANY;
				else
					r->host = sain->sin_addr;
				break;

			  case RTA_GATEWAY:
				if (sa->sa_family != AF_LINK) {
					warnx("RTA_GATEWAY not AF_LINK");
					goto fail;
				}
				sdl = (struct sockaddr_dl *)(void *)sa;
				TAILQ_FOREACH(aif, &diagif_list, link)
					if (strlen(aif->ifname) ==
					    sdl->sdl_nlen &&
					    strncmp(aif->ifname, sdl->sdl_data,
					    sdl->sdl_nlen) == 0)
						break;
				if (aif == NULL) {
					warnx("interface '%.*s' not found",
					    sdl->sdl_nlen, sdl->sdl_data);
					goto fail;
				}
				r->aif = aif;

				/* parse ATM stuff */

#define	GET3()	(((sdl->sdl_data[n] & 0xff) << 16) |	\
		 ((sdl->sdl_data[n + 1] & 0xff) << 8) |	\
		 ((sdl->sdl_data[n + 2] & 0xff) << 0))
#define	GET2()	(((sdl->sdl_data[n] & 0xff) << 8) |	\
		 ((sdl->sdl_data[n + 1] & 0xff) << 0))
#define	GET1()	(((sdl->sdl_data[n] & 0xff) << 0))

				n = sdl->sdl_nlen;
				if (sdl->sdl_alen < 4) {
					warnx("RTA_GATEWAY alen too short");
					goto fail;
				}
				r->llcsnap = GET1() & ATM_PH_LLCSNAP;
				n++;
				r->vpi = GET1();
				n++;
				r->vci = GET2();
				n += 2;
				if (sdl->sdl_alen == 4) {
					/* old address */
					r->traffic = ATMIO_TRAFFIC_UBR;
					r->pcr = 0;
					break;
				}
				/* new address */
				r->traffic = GET1();
				n++;
				switch (r->traffic) {

				  case ATMIO_TRAFFIC_UBR:
					if (sdl->sdl_alen >= 5 + 3) {
						r->pcr = GET3();
						n += 3;
					} else
						r->pcr = 0;
					break;

				  case ATMIO_TRAFFIC_CBR:
					if (sdl->sdl_alen < 5 + 3) {
						warnx("CBR address too short");
						goto fail;
					}
					r->pcr = GET3();
					n += 3;
					break;

				  case ATMIO_TRAFFIC_VBR:
					if (sdl->sdl_alen < 5 + 3 * 3) {
						warnx("VBR address too short");
						goto fail;
					}
					r->pcr = GET3();
					n += 3;
					r->scr = GET3();
					n += 3;
					r->mbs = GET3();
					n += 3;
					break;

				  case ATMIO_TRAFFIC_ABR:
					if (sdl->sdl_alen < 5 + 4 * 3 + 2 +
					    1 * 2 + 3) {
						warnx("ABR address too short");
						goto fail;
					}
					r->pcr = GET3();
					n += 3;
					r->mcr = GET3();
					n += 3;
					r->icr = GET3();
					n += 3;
					r->tbe = GET3();
					n += 3;
					r->nrm = GET1();
					n++;
					r->trm = GET1();
					n++;
					r->adtf = GET2();
					n += 2;
					r->rif = GET1();
					n++;
					r->rdf = GET1();
					n++;
					r->cdf = GET1();
					n++;
					break;

				  default:
					goto fail;
				}
				break;
			}
		}
	}

	TAILQ_INSERT_TAIL(&natm_route_list, r, link);

	return;
  fail:
	free(r);
}

/*
 * Fetch the INET routes that a ours
 */
static void
natm_route_fetch(void)
{
	int name[6];
	size_t needed;
	u_char *buf, *next;
	struct rt_msghdr *rtm;

	name[0] = CTL_NET;
	name[1] = PF_ROUTE;
	name[2] = 0;
	name[3] = AF_INET;
	name[4] = NET_RT_DUMP;
	name[5] = 0;

	if (sysctl(name, 6, NULL, &needed, NULL, 0) == -1)
		err(1, "rtable estimate");
	needed *= 2;
	if ((buf = malloc(needed)) == NULL)
		err(1, "rtable buffer (%zu)", needed);
	if (sysctl(name, 6, buf, &needed, NULL, 0) == -1)
		err(1, "rtable get");

	next = buf;
	while (next < buf + needed) {
		rtm = (struct rt_msghdr *)(void *)next;
		next += rtm->rtm_msglen;

		if (rtm->rtm_type == RTM_GET) {
			if ((rtm->rtm_flags & (RTF_UP | RTF_HOST |
			    RTF_STATIC)) == (RTF_UP | RTF_HOST | RTF_STATIC) &&
			    (rtm->rtm_addrs & (RTA_DST | RTA_GATEWAY |
			    RTA_IFP)) == (RTA_DST | RTA_GATEWAY | RTA_IFP))
				store_route(rtm);
		}
	}
}

static u_long
parse_num(const char *arg, const char *name, u_long limit)
{
	u_long res;
	char *end;

	errno = 0;
	res = strtoul(arg, &end, 10);
	if (*end != '\0' || end == arg || errno != 0)
		errx(1, "cannot parse %s '%s'", name, arg);
	if (res > limit)
		errx(1, "%s out of range (0...%lu)", name, limit);
	return (res);
}

static void
do_route(u_int type, u_int flags, const struct sockaddr_in *sain,
    const struct sockaddr_dl *sdl)
{
	struct {
		struct rt_msghdr h;
		char	space[512];
	} msg;
	char *ptr;
	int s;
	ssize_t rlen;

	/* create routing message */
	bzero(&msg, sizeof(msg));
	msg.h.rtm_msglen = sizeof(msg.h);
	msg.h.rtm_version = RTM_VERSION;
	msg.h.rtm_type = type;
	msg.h.rtm_index = 0;
	msg.h.rtm_flags = flags;
	msg.h.rtm_addrs = RTA_DST | (sdl != NULL ? RTA_GATEWAY : 0);
	msg.h.rtm_pid = getpid();

	ptr = (char *)&msg + sizeof(msg.h);
	memcpy(ptr, sain, sain->sin_len);
	ptr += roundup(sain->sin_len, sizeof(long));
	msg.h.rtm_msglen += roundup(sain->sin_len, sizeof(long));

	if (sdl != NULL) {
		memcpy(ptr, sdl, sdl->sdl_len);
		ptr += roundup(sdl->sdl_len, sizeof(long));
		msg.h.rtm_msglen += roundup(sdl->sdl_len, sizeof(long));
	}

	/* open socket */
	s = socket(PF_ROUTE, SOCK_RAW, AF_INET);
	if (s == -1)
		err(1, "cannot open routing socket");

	rlen = write(s, &msg, msg.h.rtm_msglen);
	if (rlen == -1)
		err(1, "writing to routing socket");
	if ((size_t)rlen != msg.h.rtm_msglen)
		errx(1, "short write to routing socket: %zu %u",
		    (size_t)rlen, msg.h.rtm_msglen);
	close(s);
}

/*
 * Add a new NATM route
 */
static void
natm_add(int argc, char *argv[])
{
	int opt;
	struct hostent *hp;
	struct sockaddr_in sain;
	struct sockaddr_dl sdl;
	struct diagif *aif;
	u_long num, num1;
	u_int idx;

	static int printonly;

	static const struct option opts[] = {
	    { "printonly", OPT_SIMPLE, &printonly },
	    { NULL, 0, NULL }
	};

	while ((opt = parse_options(&argc, &argv, opts)) != -1)
		switch (opt) {
		}

	if (argc < 5)
		errx(1, "missing arguments for 'natm add'");

	memset(&sdl, 0, sizeof(sdl));
	sdl.sdl_len = sizeof(sdl);
	sdl.sdl_family = AF_LINK;

	/* get the IP address for <dest> */
	memset(&sain, 0, sizeof(sain));
	hp = gethostbyname(argv[0]);
	if (hp == NULL)
		errx(1, "bad hostname %s: %s", argv[0], hstrerror(h_errno));
	if (hp->h_addrtype != AF_INET)
		errx(1, "bad address type for %s", argv[0]);
	sain.sin_len = sizeof(sain);
	sain.sin_family = AF_INET;
	memcpy(&sain.sin_addr, hp->h_addr, sizeof(sain.sin_addr));

	/* find interface */
	diagif_fetch();
	TAILQ_FOREACH(aif, &diagif_list, link)
		if (strcmp(aif->ifname, argv[1]) == 0)
			break;
	if (aif == NULL)
		errx(1, "unknown ATM interface '%s'", argv[1]);
	sdl.sdl_index = aif->index;
	strcpy(sdl.sdl_data, aif->ifname);
	idx = sdl.sdl_nlen = strlen(aif->ifname);
	idx++;

	/* verify VPI/VCI */
	num = parse_num(argv[2], "VPI", (1U << aif->mib.vpi_bits));
	sdl.sdl_data[idx++] = num & 0xff;
	num = parse_num(argv[3], "VCI", (1U << aif->mib.vci_bits));
	if (num == 0)
		errx(1, "VCI may not be 0");
	sdl.sdl_data[idx++] = (num >> 8) & 0xff;
	sdl.sdl_data[idx++] = num & 0xff;

	/* encapsulation */
	if (strcasecmp(argv[4], "llc/snap") == 0) {
		sdl.sdl_data[sdl.sdl_nlen] = ATM_PH_LLCSNAP;
	} else if (strcasecmp(argv[4], "aal5") == 0) {
		sdl.sdl_data[sdl.sdl_nlen] = 0;
	} else
		errx(1, "bad encapsulation type '%s'", argv[4]);

	/* look at the traffic */
	argc -= 5;
	argv += 5;

	if (argc != 0) {
		if (strcasecmp(argv[0], "ubr") == 0) {
			sdl.sdl_data[idx++] = ATMIO_TRAFFIC_UBR;
			if (argc == 1)
				/* ok */;
			else if (argc == 2) {
				num = parse_num(argv[1], "PCR", aif->mib.pcr);
				sdl.sdl_data[idx++] = (num >> 16) & 0xff;
				sdl.sdl_data[idx++] = (num >>  8) & 0xff;
				sdl.sdl_data[idx++] = (num >>  0) & 0xff;
			} else
				errx(1, "too many parameters for UBR");

		} else if (strcasecmp(argv[0], "cbr") == 0) {
			sdl.sdl_data[idx++] = ATMIO_TRAFFIC_CBR;
			if (argc == 1)
				errx(1, "missing PCR for CBR");
			if (argc > 2)
				errx(1, "too many parameters for CBR");
			num = parse_num(argv[1], "PCR", aif->mib.pcr);
			sdl.sdl_data[idx++] = (num >> 16) & 0xff;
			sdl.sdl_data[idx++] = (num >>  8) & 0xff;
			sdl.sdl_data[idx++] = (num >>  0) & 0xff;

		} else if (strcasecmp(argv[0], "vbr") == 0) {
			sdl.sdl_data[idx++] = ATMIO_TRAFFIC_VBR;

			if (argc < 4)
				errx(1, "missing arg(s) for VBR");
			if (argc > 4)
				errx(1, "too many parameters for VBR");

			num = parse_num(argv[1], "PCR", aif->mib.pcr);
			sdl.sdl_data[idx++] = (num >> 16) & 0xff;
			sdl.sdl_data[idx++] = (num >>  8) & 0xff;
			sdl.sdl_data[idx++] = (num >>  0) & 0xff;
			num = parse_num(argv[2], "SCR", num);
			sdl.sdl_data[idx++] = (num >> 16) & 0xff;
			sdl.sdl_data[idx++] = (num >>  8) & 0xff;
			sdl.sdl_data[idx++] = (num >>  0) & 0xff;
			num = parse_num(argv[3], "MBS", 0xffffffLU);
			sdl.sdl_data[idx++] = (num >> 16) & 0xff;
			sdl.sdl_data[idx++] = (num >>  8) & 0xff;
			sdl.sdl_data[idx++] = (num >>  0) & 0xff;

		} else if (strcasecmp(argv[0], "abr") == 0) {
			sdl.sdl_data[idx++] = ATMIO_TRAFFIC_ABR;
			if (argc < 11)
				errx(1, "missing arg(s) for ABR");
			if (argc > 11)
				errx(1, "too many parameters for ABR");

			num = parse_num(argv[1], "PCR", aif->mib.pcr);
			sdl.sdl_data[idx++] = (num >> 16) & 0xff;
			sdl.sdl_data[idx++] = (num >>  8) & 0xff;
			sdl.sdl_data[idx++] = (num >>  0) & 0xff;

			num1 = parse_num(argv[2], "MCR", num);
			sdl.sdl_data[idx++] = (num1 >> 16) & 0xff;
			sdl.sdl_data[idx++] = (num1 >>  8) & 0xff;
			sdl.sdl_data[idx++] = (num1 >>  0) & 0xff;

			num = parse_num(argv[3], "ICR", num);
			sdl.sdl_data[idx++] = (num >> 16) & 0xff;
			sdl.sdl_data[idx++] = (num >>  8) & 0xff;
			sdl.sdl_data[idx++] = (num >>  0) & 0xff;

			if (num < num1)
				errx(1, "ICR must be >= MCR");

			num = parse_num(argv[4], "TBE", 0xffffffUL);
			sdl.sdl_data[idx++] = (num >> 16) & 0xff;
			sdl.sdl_data[idx++] = (num >>  8) & 0xff;
			sdl.sdl_data[idx++] = (num >>  0) & 0xff;

			num = parse_num(argv[5], "NRM", 0x7UL);
			sdl.sdl_data[idx++] = (num >>  0) & 0xff;

			num = parse_num(argv[6], "TRM", 0x7UL);
			sdl.sdl_data[idx++] = (num >>  0) & 0xff;

			num = parse_num(argv[7], "ADTF", 0x3ffUL);
			sdl.sdl_data[idx++] = (num >>  8) & 0xff;
			sdl.sdl_data[idx++] = (num >>  0) & 0xff;

			num = parse_num(argv[8], "RIF", 0xfUL);
			sdl.sdl_data[idx++] = (num >>  0) & 0xff;

			num = parse_num(argv[9], "RDF", 0xfUL);
			sdl.sdl_data[idx++] = (num >>  0) & 0xff;

			num = parse_num(argv[10], "CDF", 0x7UL);
			sdl.sdl_data[idx++] = (num >>  0) & 0xff;

		} else
			errx(1, "bad traffic type '%s'", argv[0]);
	} else
		sdl.sdl_data[idx++] = ATMIO_TRAFFIC_UBR;

	sdl.sdl_alen = idx - sdl.sdl_nlen;
	sdl.sdl_len += sdl.sdl_nlen + sdl.sdl_alen;

	if (printonly) {
		printf("route add -iface %s -link %.*s",
		    inet_ntoa(sain.sin_addr), sdl.sdl_nlen, sdl.sdl_data);
		for (idx = 0; idx < sdl.sdl_alen; idx++)
			printf("%c%x", ".:"[idx == 0],
			    (u_int)sdl.sdl_data[sdl.sdl_nlen + idx] & 0xffU);
		printf("\n");
		exit(0);
	}

	do_route(RTM_ADD, RTF_HOST | RTF_STATIC | RTF_UP, &sain, &sdl);
}

/*
 * Delete an NATM route
 */
static void
natm_delete(int argc, char *argv[])
{
	int opt;
	struct hostent *hp;
	struct sockaddr_in sain;
	u_int vpi, vci;
	struct diagif *aif;
	struct natm_route *r;

	static int printonly;

	static const struct option opts[] = {
	    { "printonly", OPT_SIMPLE, &printonly },
	    { NULL, 0, NULL }
	};

	while ((opt = parse_options(&argc, &argv, opts)) != -1)
		switch (opt) {
		}

	diagif_fetch();
	natm_route_fetch();

	memset(&sain, 0, sizeof(sain));
	sain.sin_len = sizeof(sain);
	sain.sin_family = AF_INET;

	if (argc == 1) {
		/* get the IP address for <dest> */
		hp = gethostbyname(argv[0]);
		if (hp == NULL)
			errx(1, "bad hostname %s: %s", argv[0],
			    hstrerror(h_errno));
		if (hp->h_addrtype != AF_INET)
			errx(1, "bad address type for %s", argv[0]);
		memcpy(&sain.sin_addr, hp->h_addr, sizeof(sain.sin_addr));

		TAILQ_FOREACH(r, &natm_route_list, link)
			if (r->host.s_addr == sain.sin_addr.s_addr)
				break;
		if (r == NULL)
			errx(1, "no NATM route to host '%s' (%s)", argv[0],
			    inet_ntoa(sain.sin_addr));

	} else if (argc == 3) {
		TAILQ_FOREACH(aif, &diagif_list, link)
			if (strcmp(aif->ifname, argv[0]) == 0)
				break;
		if (aif == 0)
			errx(1, "no such interface '%s'", argv[0]);

		vpi = parse_num(argv[1], "VPI", 0xff);
		vci = parse_num(argv[2], "VCI", 0xffff);

		TAILQ_FOREACH(r, &natm_route_list, link)
			if (r->aif == aif && r->vpi == vpi && r->vci == vci)
				break;
		if (r == NULL)
			errx(1, "no such NATM route %s %u %u", argv[0],
			    vpi, vci);
		sain.sin_addr = r->host;

	} else
		errx(1, "bad number of arguments for 'natm delete'");

	if (printonly) {
		printf("route delete %s\n", inet_ntoa(r->host));
		exit(0);
	}

	do_route(RTM_DELETE, r->flags, &sain, NULL);
}

/*
 * Show NATM routes
 */
static void
natm_show(int argc, char *argv[])
{
	int opt;
	struct natm_route *r;
	struct hostent *hp;

	static const char *const traffics[] = {
		[ATMIO_TRAFFIC_UBR] = "UBR",
		[ATMIO_TRAFFIC_CBR] = "CBR",
		[ATMIO_TRAFFIC_VBR] = "VBR",
		[ATMIO_TRAFFIC_ABR] = "ABR"
	};

	static int numeric, abr;

	static const struct option opts[] = {
	    { "abr", OPT_SIMPLE, &abr },
	    { "numeric", OPT_SIMPLE, &numeric },
	    { NULL, 0, NULL }
	};

	static const char head[] =
	    "Destination         Iface       VPI VCI   Encaps   Trf PCR     "
	    "SCR/MCR MBS/ICR\n";
	static const char head_abr[] =
	    "Destination         Iface       VPI VCI   Encaps   Trf PCR     "
	    "SCR/MCR MBS/ICR TBE     NRM TRM ADTF RIF RDF CDF\n";

	while ((opt = parse_options(&argc, &argv, opts)) != -1)
		switch (opt) {
		}

	diagif_fetch();
	natm_route_fetch();

	heading_init();
	TAILQ_FOREACH(r, &natm_route_list, link) {
		heading(abr ? head_abr : head);
		if (numeric)
			printf("%-20s", inet_ntoa(r->host));
		else if (r->host.s_addr == INADDR_ANY)
			printf("%-20s", "default");
		else {
			hp = gethostbyaddr((char *)&r->host, sizeof(r->host),
			    AF_INET);
			if (hp != NULL)
				printf("%-20s", hp->h_name);
			else
				printf("%-20s", inet_ntoa(r->host));
		}
		printf("%-12s%-4u%-6u%-9s%-4s", r->aif->ifname, r->vpi, r->vci,
		    r->llcsnap ? "LLC/SNAP" : "AAL5", traffics[r->traffic]);
		switch (r->traffic) {

		  case ATMIO_TRAFFIC_UBR:
		  case ATMIO_TRAFFIC_CBR:
			printf("%-8u", r->pcr);
			break;

		  case ATMIO_TRAFFIC_VBR:
			printf("%-8u%-8u%-8u", r->pcr, r->scr, r->mbs);
			break;

		  case ATMIO_TRAFFIC_ABR:
			printf("%-8u%-8u%-8u", r->pcr, r->mcr, r->icr);
			if (abr)
				printf("%-8u%-4u%-4u%-5u%-4u%-4u%-4u",
				    r->tbe, r->nrm, r->trm, r->adtf,
				    r->rif, r->rdf, r->cdf);
			break;
		}
		printf("\n");
	}
}
OpenPOWER on IntegriCloud