sbin/route: Add support for printing link state from RTM_IFINFO
[dragonfly.git] / sbin / route / route.c
CommitLineData
984263bc
MD
1/*
2 * Copyright (c) 1983, 1989, 1991, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
dc71b7ab 13 * 3. Neither the name of the University nor the names of its contributors
984263bc
MD
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
1de703da
MD
28 *
29 * @(#) Copyright (c) 1983, 1989, 1991, 1993 The Regents of the University of California. All rights reserved.
30 * @(#)route.c 8.6 (Berkeley) 4/28/95
31 * $FreeBSD: src/sbin/route/route.c,v 1.40.2.11 2003/02/27 23:10:10 ru Exp $
984263bc
MD
32 */
33
984263bc
MD
34#include <sys/param.h>
35#include <sys/file.h>
36#include <sys/socket.h>
37#include <sys/ioctl.h>
38#include <sys/sysctl.h>
39#include <sys/types.h>
40
41#include <net/if.h>
42#include <net/route.h>
43#include <net/if_dl.h>
44#include <netinet/in.h>
45#include <netinet/if_ether.h>
984263bc
MD
46#include <arpa/inet.h>
47#include <netdb.h>
48
e9a046d0 49#include <netproto/802_11/ieee80211_dragonfly.h>
9b42cabe
NA
50#include <netproto/mpls/mpls.h>
51
984263bc
MD
52#include <ctype.h>
53#include <err.h>
54#include <errno.h>
e9a046d0 55#include <inttypes.h>
984263bc
MD
56#include <paths.h>
57#include <stdio.h>
58#include <stdlib.h>
59#include <string.h>
60#include <sysexits.h>
61#include <unistd.h>
62#include <ifaddrs.h>
63
147a7be0 64#include "extern.h"
984263bc 65
984263bc
MD
66union sockunion {
67 struct sockaddr sa;
68 struct sockaddr_in sin;
69#ifdef INET6
70 struct sockaddr_in6 sin6;
71#endif
9b42cabe 72 struct sockaddr_mpls smpls;
984263bc
MD
73 struct sockaddr_dl sdl;
74 struct sockaddr_inarp sinarp;
75 struct sockaddr_storage ss; /* added to avoid memory overrun */
9b42cabe
NA
76} so_dst, so_gate, so_mask, so_genmask, so_ifa, so_ifp, so_mpls1,
77 so_mpls2, so_mpls3;
984263bc
MD
78
79typedef union sockunion *sup;
147a7be0 80
5af3bab0 81int nflag, wflag;
b06a5e2a 82int cpuflag = -1;
5af3bab0 83
166dccaa
SW
84static struct keytab {
85 const char *kt_cp;
86 int kt_i;
87} const keywords[] = {
88#include "keywords.h"
89 {0, 0}
90};
91
ac6b17f6 92static struct sockaddr_in so_gate_tried;
5af3bab0
CP
93static struct rt_metrics rt_metrics;
94static int pid, rtm_addrs;
95static int s;
96static int forcehost, forcenet, af, qflag, tflag;
97static int iflag, verbose, aflen = sizeof(struct sockaddr_in);
98static int locking, lockrest, debugonly;
9b42cabe 99static int mplsop, popcount, pushcount, swapcount;
5af3bab0
CP
100static u_long rtm_inits;
101static uid_t uid;
984263bc 102
5af3bab0
CP
103static void flushroutes(int, char **);
104static void set_metric(char *, int);
9c15df6b 105static void newroute(int, char **) __dead2;
5af3bab0
CP
106static void inet_makenetandmask(u_long, struct sockaddr_in *, u_long);
107static void interfaces(void);
9c15df6b 108static void monitor(void) __dead2;
ed6c7133 109static void sockaddr(const char *, struct sockaddr *);
5af3bab0
CP
110static void sodump(sup, const char *);
111static void bprintf(FILE *, int, u_char *);
112static void print_getmsg(struct rt_msghdr *, int);
113static void print_rtmsg(struct rt_msghdr *, int);
114static void pmsg_common(struct rt_msghdr *);
115static void pmsg_addrs(char *, int);
116static void mask_addr(void);
ed6c7133 117static int getaddr(int, const char *, struct hostent **);
5af3bab0
CP
118static int rtmsg(int, int);
119static int prefixlen(const char *);
120#ifdef INET6
121static int inet6_makenetandmask(struct sockaddr_in6 *, const char *);
122#endif
984263bc 123
e9a046d0
RM
124#define PRIETHER "02x:%02x:%02x:%02x:%02x:%02x"
125#define PRIETHER_ARGS(__enaddr) (__enaddr)[0], (__enaddr)[1], (__enaddr)[2], \
126 (__enaddr)[3], (__enaddr)[4], (__enaddr)[5]
127
984263bc 128void
b5744197 129usage(const char *cp)
984263bc 130{
bc82d1bc 131 if (cp != NULL)
984263bc 132 warnx("bad keyword: %s", cp);
b69d41e9 133 fprintf(stderr, "usage: route [-dnqtvw] [-c cpu] command [[modifiers] args]\n");
984263bc
MD
134 exit(EX_USAGE);
135 /* NOTREACHED */
136}
137
984263bc 138int
b5744197 139main(int argc, char **argv)
984263bc
MD
140{
141 int ch;
142
143 if (argc < 2)
2038fb68 144 usage(NULL);
984263bc 145
b06a5e2a 146 while ((ch = getopt(argc, argv, "c:wnqdtv")) != -1)
984263bc 147 switch(ch) {
b06a5e2a
MD
148 case 'c':
149 cpuflag = strtol(optarg, NULL, 0);
150 break;
9ddb32ba
MD
151 case 'w':
152 wflag = 1;
153 break;
984263bc
MD
154 case 'n':
155 nflag = 1;
156 break;
157 case 'q':
158 qflag = 1;
159 break;
160 case 'v':
161 verbose = 1;
162 break;
163 case 't':
164 tflag = 1;
165 break;
166 case 'd':
167 debugonly = 1;
168 break;
169 case '?':
170 default:
2038fb68 171 usage(NULL);
984263bc
MD
172 }
173 argc -= optind;
174 argv += optind;
175
176 pid = getpid();
177 uid = geteuid();
178 if (tflag)
179 s = open(_PATH_DEVNULL, O_WRONLY, 0);
2789028f
RM
180 else {
181
984263bc 182 s = socket(PF_ROUTE, SOCK_RAW, 0);
2789028f
RM
183#ifdef SO_RERROR
184 int on = 1;
185 if (setsockopt(s, SOL_SOCKET, SO_RERROR, &on, sizeof(on)) == -1)
186 warn("SO_RERROR");
187#endif
188 }
984263bc
MD
189 if (s < 0)
190 err(EX_OSERR, "socket");
bc82d1bc 191 if (*argv != NULL)
984263bc
MD
192 switch (keyword(*argv)) {
193 case K_GET:
194 uid = 0;
195 /* FALLTHROUGH */
196
197 case K_CHANGE:
198 case K_ADD:
199 case K_DELETE:
200 newroute(argc, argv);
201 /* NOTREACHED */
202
147a7be0
MD
203 case K_SHOW:
204 show(argc, argv);
205 return(0);
206
984263bc
MD
207 case K_MONITOR:
208 monitor();
209 /* NOTREACHED */
210
211 case K_FLUSH:
212 flushroutes(argc, argv);
213 exit(0);
214 /* NOTREACHED */
215 }
216 usage(*argv);
217 /* NOTREACHED */
218}
219
220/*
221 * Purge all entries in the routing tables not
222 * associated with network interfaces.
223 */
5af3bab0 224static void
b5744197 225flushroutes(int argc, char **argv)
984263bc
MD
226{
227 size_t needed;
b06a5e2a
MD
228 int mib[7], rlen, seqno;
229 int miblen;
984263bc 230 char *buf, *next, *lim;
147a7be0 231 struct rt_msghdr *rtm;
984263bc 232
bc82d1bc 233 if (uid != 0) {
984263bc
MD
234 errx(EX_NOPERM, "must be root to alter routing table");
235 }
e70afae7 236 shutdown(s, SHUT_RD); /* Don't want to read back our messages */
984263bc
MD
237 if (argc > 1) {
238 argv++;
239 if (argc == 2 && **argv == '-')
240 switch (keyword(*argv + 1)) {
241 case K_INET:
242 af = AF_INET;
243 break;
244#ifdef INET6
245 case K_INET6:
246 af = AF_INET6;
247 break;
248#endif
984263bc
MD
249 case K_LINK:
250 af = AF_LINK;
251 break;
9b42cabe
NA
252 case K_MPLS:
253 af = AF_MPLS;
254 break;
984263bc
MD
255 default:
256 goto bad;
257 } else
258bad: usage(*argv);
259 }
260 mib[0] = CTL_NET;
261 mib[1] = PF_ROUTE;
262 mib[2] = 0; /* protocol */
263 mib[3] = 0; /* wildcard address family */
264 mib[4] = NET_RT_DUMP;
265 mib[5] = 0; /* no flags */
b06a5e2a
MD
266 if (cpuflag >= 0) {
267 mib[6] = cpuflag;
268 miblen = 7;
269 } else {
270 miblen = 6;
271 }
272 if (sysctl(mib, miblen, NULL, &needed, NULL, 0) < 0)
984263bc
MD
273 err(EX_OSERR, "route-sysctl-estimate");
274 if ((buf = malloc(needed)) == NULL)
275 errx(EX_OSERR, "malloc failed");
b06a5e2a 276 if (sysctl(mib, miblen, buf, &needed, NULL, 0) < 0)
984263bc
MD
277 err(EX_OSERR, "route-sysctl-get");
278 lim = buf + needed;
279 if (verbose)
147a7be0 280 printf("Examining routing table from sysctl\n");
984263bc
MD
281 seqno = 0; /* ??? */
282 for (next = buf; next < lim; next += rtm->rtm_msglen) {
283 rtm = (struct rt_msghdr *)next;
284 if (verbose)
285 print_rtmsg(rtm, rtm->rtm_msglen);
286 if ((rtm->rtm_flags & RTF_GATEWAY) == 0)
287 continue;
bc82d1bc 288 if (af != 0) {
984263bc
MD
289 struct sockaddr *sa = (struct sockaddr *)(rtm + 1);
290
291 if (sa->sa_family != af)
292 continue;
293 }
294 if (debugonly)
295 continue;
296 rtm->rtm_type = RTM_DELETE;
297 rtm->rtm_seq = seqno;
298 rlen = write(s, next, rtm->rtm_msglen);
299 if (rlen < (int)rtm->rtm_msglen) {
300 warn("write to routing socket");
147a7be0 301 printf("got only %d for rlen\n", rlen);
984263bc
MD
302 break;
303 }
304 seqno++;
305 if (qflag)
306 continue;
9ddb32ba 307 if (verbose) {
984263bc 308 print_rtmsg(rtm, rlen);
9ddb32ba 309 } else {
984263bc 310 struct sockaddr *sa = (struct sockaddr *)(rtm + 1);
9ddb32ba
MD
311 if (wflag) {
312 printf("%-20s ", rtm->rtm_flags & RTF_HOST ?
313 routename(sa) : netname(sa));
314 } else {
315 printf("%-20.20s ", rtm->rtm_flags & RTF_HOST ?
316 routename(sa) : netname(sa));
317 }
4ff4d99f 318 sa = (struct sockaddr *)(RT_ROUNDUP(sa->sa_len) +
147a7be0 319 (char *)sa);
9ddb32ba
MD
320 if (wflag) {
321 printf("%-20s ", routename(sa));
322 } else {
323 printf("%-20.20s ", routename(sa));
324 }
147a7be0 325 printf("done\n");
984263bc
MD
326 }
327 }
328}
329
147a7be0 330char *
b5744197 331routename(struct sockaddr *sa)
984263bc 332{
5af3bab0 333 const char *cp;
984263bc
MD
334 static char line[MAXHOSTNAMELEN + 1];
335 struct hostent *hp;
336 static char domain[MAXHOSTNAMELEN + 1];
337 static int first = 1;
984263bc
MD
338
339 if (first) {
340 first = 0;
341 if (gethostname(domain, MAXHOSTNAMELEN) == 0 &&
342 (cp = strchr(domain, '.'))) {
343 domain[MAXHOSTNAMELEN] = '\0';
147a7be0 344 strcpy(domain, cp + 1);
984263bc
MD
345 } else
346 domain[0] = 0;
347 }
348
349 if (sa->sa_len == 0)
350 strcpy(line, "default");
351 else switch (sa->sa_family) {
352
353 case AF_INET:
354 { struct in_addr in;
355 in = ((struct sockaddr_in *)sa)->sin_addr;
356
bc82d1bc 357 cp = NULL;
984263bc
MD
358 if (in.s_addr == INADDR_ANY || sa->sa_len < 4)
359 cp = "default";
bc82d1bc 360 if (cp == NULL && !nflag) {
15b85273 361 hp = gethostbyaddr(&in, sizeof(struct in_addr),
984263bc 362 AF_INET);
bc82d1bc 363 if (hp != NULL) {
5af3bab0
CP
364 char *cptr;
365 cptr = strchr(hp->h_name, '.');
366 if (cptr != NULL && !wflag &&
367 strcmp(cptr + 1, domain) == 0)
368 *cptr = '\0';
984263bc
MD
369 cp = hp->h_name;
370 }
371 }
bc82d1bc 372 if (cp != NULL) {
984263bc
MD
373 strncpy(line, cp, sizeof(line) - 1);
374 line[sizeof(line) - 1] = '\0';
375 } else
147a7be0 376 sprintf(line, "%s", inet_ntoa(in));
984263bc
MD
377 break;
378 }
379
380#ifdef INET6
381 case AF_INET6:
382 {
383 struct sockaddr_in6 sin6; /* use static var for safety */
384 int niflags = 0;
385#ifdef NI_WITHSCOPEID
386 niflags = NI_WITHSCOPEID;
387#endif
388
389 memset(&sin6, 0, sizeof(sin6));
390 memcpy(&sin6, sa, sa->sa_len);
391 sin6.sin6_len = sizeof(struct sockaddr_in6);
392 sin6.sin6_family = AF_INET6;
393#ifdef __KAME__
394 if (sa->sa_len == sizeof(struct sockaddr_in6) &&
395 (IN6_IS_ADDR_LINKLOCAL(&sin6.sin6_addr) ||
396 IN6_IS_ADDR_MC_LINKLOCAL(&sin6.sin6_addr)) &&
397 sin6.sin6_scope_id == 0) {
398 sin6.sin6_scope_id =
399 ntohs(*(u_int16_t *)&sin6.sin6_addr.s6_addr[2]);
400 sin6.sin6_addr.s6_addr[2] = 0;
401 sin6.sin6_addr.s6_addr[3] = 0;
402 }
403#endif
404 if (nflag)
405 niflags |= NI_NUMERICHOST;
406 if (getnameinfo((struct sockaddr *)&sin6, sin6.sin6_len,
407 line, sizeof(line), NULL, 0, niflags) != 0)
408 strncpy(line, "invalid", sizeof(line));
409
410 return(line);
411 }
412#endif
413
984263bc 414 case AF_LINK:
147a7be0 415 return(link_ntoa((struct sockaddr_dl *)sa));
984263bc
MD
416
417 default:
00e3c552
CP
418 {
419 /*
420 * Unknown address family; just render the raw
421 * data in sa->sa_data as hex values.
422 */
fd07f7dc
CP
423 uint8_t *sp = (uint8_t *)sa->sa_data;
424 uint8_t *splim = (uint8_t *)sa + sa->sa_len;
5af3bab0 425 char *cps = line + sprintf(line, "(%d)", sa->sa_family);
984263bc
MD
426 char *cpe = line + sizeof(line);
427
00e3c552 428 while (sp < splim && cps < cpe)
fd28efc5 429 cps += snprintf(cps, cpe - cps, " %02x", *sp++);
984263bc
MD
430 break;
431 }
432 }
147a7be0 433 return(line);
984263bc
MD
434}
435
436/*
437 * Return the name of the network whose address is given.
438 * The address is assumed to be that of a net or subnet, not a host.
439 */
147a7be0 440char *
b5744197 441netname(struct sockaddr *sa)
984263bc 442{
678e8cc6 443 const char *cp = NULL;
984263bc 444 static char line[MAXHOSTNAMELEN + 1];
678e8cc6 445 struct netent *np = NULL;
984263bc 446 u_long net, mask;
147a7be0 447 u_long i;
984263bc 448 int subnetshift;
984263bc
MD
449
450 switch (sa->sa_family) {
451
452 case AF_INET:
453 { struct in_addr in;
454 in = ((struct sockaddr_in *)sa)->sin_addr;
455
456 i = in.s_addr = ntohl(in.s_addr);
457 if (in.s_addr == 0)
458 cp = "default";
459 else if (!nflag) {
460 if (IN_CLASSA(i)) {
461 mask = IN_CLASSA_NET;
462 subnetshift = 8;
463 } else if (IN_CLASSB(i)) {
464 mask = IN_CLASSB_NET;
465 subnetshift = 8;
466 } else {
467 mask = IN_CLASSC_NET;
468 subnetshift = 4;
469 }
470 /*
471 * If there are more bits than the standard mask
472 * would suggest, subnets must be in use.
473 * Guess at the subnet mask, assuming reasonable
474 * width subnet fields.
475 */
476 while (in.s_addr &~ mask)
8a2db35a 477 mask |= mask >> subnetshift;
984263bc
MD
478 net = in.s_addr & mask;
479 while ((mask & 1) == 0)
480 mask >>= 1, net >>= 1;
481 np = getnetbyaddr(net, AF_INET);
bc82d1bc 482 if (np != NULL)
984263bc
MD
483 cp = np->n_name;
484 }
485#define C(x) (unsigned)((x) & 0xff)
bc82d1bc 486 if (cp != NULL)
984263bc
MD
487 strncpy(line, cp, sizeof(line));
488 else if ((in.s_addr & 0xffffff) == 0)
147a7be0 489 sprintf(line, "%u", C(in.s_addr >> 24));
984263bc 490 else if ((in.s_addr & 0xffff) == 0)
147a7be0 491 sprintf(line, "%u.%u", C(in.s_addr >> 24),
984263bc
MD
492 C(in.s_addr >> 16));
493 else if ((in.s_addr & 0xff) == 0)
147a7be0 494 sprintf(line, "%u.%u.%u", C(in.s_addr >> 24),
984263bc
MD
495 C(in.s_addr >> 16), C(in.s_addr >> 8));
496 else
147a7be0 497 sprintf(line, "%u.%u.%u.%u", C(in.s_addr >> 24),
984263bc
MD
498 C(in.s_addr >> 16), C(in.s_addr >> 8),
499 C(in.s_addr));
500#undef C
501 break;
502 }
503
504#ifdef INET6
505 case AF_INET6:
506 {
507 struct sockaddr_in6 sin6; /* use static var for safety */
508 int niflags = 0;
509#ifdef NI_WITHSCOPEID
510 niflags = NI_WITHSCOPEID;
511#endif
512
513 memset(&sin6, 0, sizeof(sin6));
514 memcpy(&sin6, sa, sa->sa_len);
515 sin6.sin6_len = sizeof(struct sockaddr_in6);
516 sin6.sin6_family = AF_INET6;
517#ifdef __KAME__
518 if (sa->sa_len == sizeof(struct sockaddr_in6) &&
519 (IN6_IS_ADDR_LINKLOCAL(&sin6.sin6_addr) ||
520 IN6_IS_ADDR_MC_LINKLOCAL(&sin6.sin6_addr)) &&
521 sin6.sin6_scope_id == 0) {
522 sin6.sin6_scope_id =
523 ntohs(*(u_int16_t *)&sin6.sin6_addr.s6_addr[2]);
524 sin6.sin6_addr.s6_addr[2] = 0;
525 sin6.sin6_addr.s6_addr[3] = 0;
526 }
527#endif
528 if (nflag)
529 niflags |= NI_NUMERICHOST;
530 if (getnameinfo((struct sockaddr *)&sin6, sin6.sin6_len,
531 line, sizeof(line), NULL, 0, niflags) != 0)
532 strncpy(line, "invalid", sizeof(line));
533
534 return(line);
535 }
536#endif
537
984263bc 538 case AF_LINK:
147a7be0 539 return(link_ntoa((struct sockaddr_dl *)sa));
984263bc
MD
540
541
542 default:
00e3c552
CP
543 {
544 /*
545 * Unknown address family; just render the raw
546 * data in sa->sa_data as hex values.
547 */
fd07f7dc
CP
548 uint8_t *sp = (uint8_t *)sa->sa_data;
549 uint8_t *splim = (uint8_t *)sa + sa->sa_len;
5af3bab0 550 char *cps = line + sprintf(line, "af %d:", sa->sa_family);
984263bc
MD
551 char *cpe = line + sizeof(line);
552
5af3bab0 553 while (sp < splim && cps < cpe)
fd28efc5 554 cps += snprintf(cps, cpe - cps, " %02x", *sp++);
984263bc
MD
555 break;
556 }
557 }
147a7be0 558 return(line);
984263bc
MD
559}
560
5af3bab0 561static void
b5744197 562set_metric(char *value, int key)
984263bc
MD
563{
564 int flag = 0;
565 u_long noval, *valp = &noval;
566
567 switch (key) {
568#define caseof(x, y, z) case x: valp = &rt_metrics.z; flag = y; break
569 caseof(K_MTU, RTV_MTU, rmx_mtu);
570 caseof(K_HOPCOUNT, RTV_HOPCOUNT, rmx_hopcount);
571 caseof(K_EXPIRE, RTV_EXPIRE, rmx_expire);
572 caseof(K_RECVPIPE, RTV_RPIPE, rmx_recvpipe);
573 caseof(K_SENDPIPE, RTV_SPIPE, rmx_sendpipe);
574 caseof(K_SSTHRESH, RTV_SSTHRESH, rmx_ssthresh);
575 caseof(K_RTT, RTV_RTT, rmx_rtt);
576 caseof(K_RTTVAR, RTV_RTTVAR, rmx_rttvar);
8ec89e60 577 caseof(K_MSL, RTV_MSL, rmx_msl);
bf522a91
SZ
578 caseof(K_IWMAX, RTV_IWMAXSEGS, rmx_iwmaxsegs);
579 caseof(K_IW, RTV_IWCAPSEGS, rmx_iwcapsegs);
984263bc
MD
580 }
581 rtm_inits |= flag;
582 if (lockrest || locking)
583 rt_metrics.rmx_locks |= flag;
584 if (locking)
585 locking = 0;
586 *valp = atoi(value);
587}
588
5af3bab0 589static void
b5744197 590newroute(int argc, char **argv)
984263bc 591{
5af3bab0
CP
592 char *cmd;
593 const char *err_str, *dest = "", *gateway = "";
984263bc
MD
594 int ishost = 0, proxy = 0, ret, attempts, oerrno, flags = RTF_STATIC;
595 int key;
678e8cc6 596 struct hostent *hp = NULL;
984263bc 597
bc82d1bc 598 if (uid != 0) {
984263bc
MD
599 errx(EX_NOPERM, "must be root to alter routing table");
600 }
601 cmd = argv[0];
602 if (*cmd != 'g')
e70afae7 603 shutdown(s, SHUT_RD); /* Don't want to read back our messages */
984263bc
MD
604 while (--argc > 0) {
605 if (**(++argv)== '-') {
606 switch (key = keyword(1 + *argv)) {
607 case K_LINK:
608 af = AF_LINK;
609 aflen = sizeof(struct sockaddr_dl);
610 break;
611 case K_INET:
612 af = AF_INET;
613 aflen = sizeof(struct sockaddr_in);
614 break;
615#ifdef INET6
616 case K_INET6:
617 af = AF_INET6;
618 aflen = sizeof(struct sockaddr_in6);
619 break;
620#endif
984263bc
MD
621 case K_SA:
622 af = PF_ROUTE;
623 aflen = sizeof(union sockunion);
624 break;
984263bc
MD
625 case K_IFACE:
626 case K_INTERFACE:
627 iflag++;
628 break;
629 case K_NOSTATIC:
630 flags &= ~RTF_STATIC;
631 break;
632 case K_LLINFO:
633 flags |= RTF_LLINFO;
634 break;
635 case K_LOCK:
636 locking = 1;
637 break;
638 case K_LOCKREST:
639 lockrest = 1;
640 break;
641 case K_HOST:
642 forcehost++;
643 break;
644 case K_REJECT:
645 flags |= RTF_REJECT;
646 break;
647 case K_BLACKHOLE:
648 flags |= RTF_BLACKHOLE;
649 break;
650 case K_PROTO1:
651 flags |= RTF_PROTO1;
652 break;
653 case K_PROTO2:
654 flags |= RTF_PROTO2;
655 break;
656 case K_PROXY:
657 proxy = 1;
658 break;
659 case K_CLONING:
660 flags |= RTF_CLONING;
661 break;
662 case K_XRESOLVE:
663 flags |= RTF_XRESOLVE;
664 break;
665 case K_STATIC:
666 flags |= RTF_STATIC;
667 break;
668 case K_IFA:
bc82d1bc 669 if (--argc == 0)
2038fb68 670 usage(NULL);
147a7be0 671 getaddr(RTA_IFA, *++argv, 0);
984263bc
MD
672 break;
673 case K_IFP:
bc82d1bc 674 if (--argc == 0)
2038fb68 675 usage(NULL);
147a7be0 676 getaddr(RTA_IFP, *++argv, 0);
984263bc
MD
677 break;
678 case K_GENMASK:
bc82d1bc 679 if (--argc == 0)
2038fb68 680 usage(NULL);
147a7be0 681 getaddr(RTA_GENMASK, *++argv, 0);
984263bc
MD
682 break;
683 case K_GATEWAY:
bc82d1bc 684 if (--argc == 0)
2038fb68 685 usage(NULL);
147a7be0 686 getaddr(RTA_GATEWAY, *++argv, 0);
984263bc
MD
687 break;
688 case K_DST:
bc82d1bc 689 if (--argc == 0)
2038fb68 690 usage(NULL);
984263bc
MD
691 ishost = getaddr(RTA_DST, *++argv, &hp);
692 dest = *argv;
693 break;
694 case K_NETMASK:
bc82d1bc 695 if (--argc == 0)
2038fb68 696 usage(NULL);
147a7be0 697 getaddr(RTA_NETMASK, *++argv, 0);
984263bc
MD
698 /* FALLTHROUGH */
699 case K_NET:
700 forcenet++;
701 break;
702 case K_PREFIXLEN:
bc82d1bc 703 if (--argc == 0)
2038fb68 704 usage(NULL);
984263bc
MD
705 if (prefixlen(*++argv) == -1) {
706 forcenet = 0;
707 ishost = 1;
708 } else {
709 forcenet = 1;
710 ishost = 0;
711 }
712 break;
9b42cabe
NA
713 case K_MPLS:
714 af = AF_MPLS;
715 aflen = sizeof(struct sockaddr_mpls);
716 break;
717 case K_POP:
718 flags |= RTF_MPLSOPS;
719 af = AF_MPLS;
720 mplsop = MPLSLOP_POP;
721 switch(++popcount){
722 case 1:
723 getaddr(RTA_MPLS1, "", 0);
724 break;
725 case 2:
726 getaddr(RTA_MPLS2, "", 0);
727 break;
728 case 3:
729 getaddr(RTA_MPLS3, "", 0);
730 break;
731 }
732 break;
733 case K_PUSH:
734 case K_SWAP:
735 if (popcount > 0) {
736 warnx("Push or swap after pop. Ignoring.");
737 break;
738 }
739 if (key == K_PUSH) {
740 mplsop = MPLSLOP_PUSH;
741 ++pushcount;
742 } else {
743 if (pushcount > 0) {
744 warnx("Swap after push. Ignoring.");
745 break;
746 }
747 if (swapcount > 0) {
748 warnx("Too many swaps. Ignoring.");
749 break;
750 }
751 mplsop = MPLSLOP_SWAP;
752 ++swapcount;
753 }
754 flags |= RTF_MPLSOPS;
755 af = AF_MPLS;
756 aflen = sizeof(struct sockaddr_mpls);
757 if (--argc == 0)
2038fb68 758 usage(NULL);
9b42cabe
NA
759 switch(pushcount + swapcount){
760 case 1:
761 getaddr(RTA_MPLS1, *++argv, 0);
762 break;
763 case 2:
764 getaddr(RTA_MPLS2, *++argv, 0);
765 break;
766 case 3:
767 getaddr(RTA_MPLS3, *++argv, 0);
768 break;
769 }
770 break;
984263bc
MD
771 case K_MTU:
772 case K_HOPCOUNT:
773 case K_EXPIRE:
774 case K_RECVPIPE:
775 case K_SENDPIPE:
776 case K_SSTHRESH:
777 case K_RTT:
778 case K_RTTVAR:
8ec89e60 779 case K_MSL:
bf522a91
SZ
780 case K_IWMAX:
781 case K_IW:
bc82d1bc 782 if (--argc == 0)
2038fb68 783 usage(NULL);
984263bc
MD
784 set_metric(*++argv, key);
785 break;
786 default:
787 usage(1+*argv);
788 }
789 } else {
790 if ((rtm_addrs & RTA_DST) == 0) {
791 dest = *argv;
792 ishost = getaddr(RTA_DST, *argv, &hp);
793 } else if ((rtm_addrs & RTA_GATEWAY) == 0) {
794 gateway = *argv;
147a7be0 795 getaddr(RTA_GATEWAY, *argv, &hp);
984263bc 796 } else {
147a7be0 797 getaddr(RTA_NETMASK, *argv, 0);
984263bc
MD
798 forcenet = 1;
799 }
800 }
801 }
802 if (forcehost) {
803 ishost = 1;
804#ifdef INET6
805 if (af == AF_INET6) {
806 rtm_addrs &= ~RTA_NETMASK;
807 memset((void *)&so_mask, 0, sizeof(so_mask));
808 }
147a7be0 809#endif
984263bc
MD
810 }
811 if (forcenet)
812 ishost = 0;
813 flags |= RTF_UP;
814 if (ishost)
815 flags |= RTF_HOST;
816 if (iflag == 0)
817 flags |= RTF_GATEWAY;
818 if (proxy) {
819 so_dst.sinarp.sin_other = SIN_PROXY;
820 flags |= RTF_ANNOUNCE;
821 }
822 for (attempts = 1; ; attempts++) {
823 errno = 0;
824 if ((ret = rtmsg(*cmd, flags)) == 0)
825 break;
826 if (errno != ENETUNREACH && errno != ESRCH)
827 break;
bc82d1bc
CP
828 if (af == AF_INET && *gateway != '\0' &&
829 hp != NULL && hp->h_addr_list[1] != NULL) {
ac6b17f6 830 so_gate_tried = so_gate.sin;
984263bc 831 hp->h_addr_list++;
5af3bab0
CP
832 memmove(&so_gate.sin.sin_addr,
833 hp->h_addr_list[0],
834 MIN((size_t)hp->h_length,
835 sizeof(so_gate.sin.sin_addr)));
984263bc
MD
836 } else
837 break;
838 }
839 if (*cmd == 'g')
840 exit(0);
841 if (!qflag) {
842 oerrno = errno;
147a7be0 843 printf("%s %s %s", cmd, ishost? "host" : "net", dest);
bc82d1bc 844 if (*gateway != '\0') {
147a7be0 845 printf(": gateway %s", gateway);
ac6b17f6
SZ
846 if (attempts > 1 && ret == 0 && af == AF_INET) {
847 printf(" (%s)",
848 inet_ntoa(so_gate_tried.sin_addr));
849 }
984263bc
MD
850 }
851 if (ret == 0) {
147a7be0 852 printf("\n");
984263bc
MD
853 } else {
854 switch (oerrno) {
855 case ESRCH:
5af3bab0 856 err_str = "not in table";
984263bc
MD
857 break;
858 case EBUSY:
5af3bab0 859 err_str = "entry in use";
984263bc
MD
860 break;
861 case ENOBUFS:
5af3bab0 862 err_str = "routing table overflow";
984263bc 863 break;
147a7be0
MD
864 case EDQUOT: /* handle recursion avoidance in
865 rt_setgate() */
5af3bab0 866 err_str = "gateway uses the same route";
984263bc
MD
867 break;
868 default:
5af3bab0 869 err_str = strerror(oerrno);
984263bc
MD
870 break;
871 }
5af3bab0 872 printf(": %s\n", err_str);
984263bc
MD
873 }
874 }
875 exit(ret != 0);
876}
877
5af3bab0
CP
878static void
879inet_makenetandmask(u_long net, struct sockaddr_in *in, u_long bits)
984263bc
MD
880{
881 u_long addr, mask = 0;
147a7be0 882 char *cp;
984263bc
MD
883
884 rtm_addrs |= RTA_NETMASK;
855ce2f5
YT
885 /*
886 * XXX: This approach unable to handle 0.0.0.1/32 correctly
887 * as inet_network() converts 0.0.0.1 and 1 equally.
888 */
a80e869f 889 if (net <= 0xff)
855ce2f5 890 addr = net << IN_CLASSA_NSHIFT;
a80e869f 891 else if (net <= 0xffff)
855ce2f5 892 addr = net << IN_CLASSB_NSHIFT;
a80e869f 893 else if (net <= 0xffffff)
855ce2f5 894 addr = net << IN_CLASSC_NSHIFT;
a80e869f
HT
895 else
896 addr = net;
a7c98c34 897
855ce2f5 898 if (bits != 0)
a80e869f 899 mask = 0xffffffff << (32 - bits);
855ce2f5
YT
900 else if (net == 0)
901 mask = 0;
902 else if (IN_CLASSA(addr))
903 mask = IN_CLASSA_NET;
904 else if (IN_CLASSB(addr))
905 mask = IN_CLASSB_NET;
906 else if (IN_CLASSC(addr))
907 mask = IN_CLASSC_NET;
908 else if (IN_MULTICAST(addr))
909 mask = IN_CLASSD_NET;
910 else
911 mask = 0xffffffff;
912
5af3bab0
CP
913 in->sin_addr.s_addr = htonl(addr);
914 in = &so_mask.sin;
915 in->sin_addr.s_addr = htonl(mask);
916 in->sin_len = 0;
917 in->sin_family = 0;
918 cp = (char *)(&in->sin_addr + 1);
919 while (*--cp == 0 && cp > (char *)in)
984263bc 920 ;
5af3bab0 921 in->sin_len = 1 + cp - (char *)in;
984263bc
MD
922}
923
924#ifdef INET6
925/*
926 * XXX the function may need more improvement...
927 */
928static int
5af3bab0 929inet6_makenetandmask(struct sockaddr_in6 *sin6, const char *plen)
984263bc
MD
930{
931 struct in6_addr in6;
932
bc82d1bc 933 if (plen == NULL) {
984263bc
MD
934 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr) &&
935 sin6->sin6_scope_id == 0) {
936 plen = "0";
937 } else if ((sin6->sin6_addr.s6_addr[0] & 0xe0) == 0x20) {
938 /* aggregatable global unicast - RFC2374 */
939 memset(&in6, 0, sizeof(in6));
bc82d1bc
CP
940 if (memcmp(&sin6->sin6_addr.s6_addr[8],
941 &in6.s6_addr[8], 8) == 0)
984263bc
MD
942 plen = "64";
943 }
944 }
945
bc82d1bc 946 if (plen == NULL || strcmp(plen, "128") == 0)
147a7be0 947 return(1);
984263bc 948 rtm_addrs |= RTA_NETMASK;
147a7be0
MD
949 prefixlen(plen);
950 return(0);
984263bc
MD
951}
952#endif
953
954/*
955 * Interpret an argument as a network address of some kind,
956 * returning 1 if a host address, 0 if a network address.
957 */
5af3bab0 958static int
ed6c7133 959getaddr(int which, const char *str, struct hostent **hpp)
984263bc 960{
147a7be0 961 sup su;
984263bc
MD
962 struct hostent *hp;
963 struct netent *np;
964 u_long val;
965 char *q;
966 int afamily; /* local copy of af so we can change it */
967
968 if (af == 0) {
969 af = AF_INET;
970 aflen = sizeof(struct sockaddr_in);
971 }
972 afamily = af;
973 rtm_addrs |= which;
974 switch (which) {
975 case RTA_DST:
976 su = &so_dst;
977 break;
978 case RTA_GATEWAY:
979 su = &so_gate;
980 if (iflag) {
981 struct ifaddrs *ifap, *ifa;
982 struct sockaddr_dl *sdl = NULL;
983
984 if (getifaddrs(&ifap))
985 err(1, "getifaddrs");
986
bc82d1bc 987 for (ifa = ifap; ifa != NULL; ifa = ifa->ifa_next) {
984263bc
MD
988 if (ifa->ifa_addr->sa_family != AF_LINK)
989 continue;
990
bc82d1bc 991 if (strcmp(str, ifa->ifa_name) != 0)
984263bc
MD
992 continue;
993
994 sdl = (struct sockaddr_dl *)ifa->ifa_addr;
995 }
996 /* If we found it, then use it */
bc82d1bc 997 if (sdl != NULL) {
984263bc
MD
998 /*
999 * Copy is safe since we have a
1000 * sockaddr_storage member in sockunion{}.
1001 * Note that we need to copy before calling
1002 * freeifaddrs().
03f869ef
MD
1003 *
1004 * Interface routes are routed only by their
1005 * sdl_index or interface name (we provide
1006 * both here), not by the ARP address of the
1007 * local interface (alen, slen = 0).
984263bc
MD
1008 */
1009 memcpy(&su->sdl, sdl, sdl->sdl_len);
03f869ef
MD
1010 su->sdl.sdl_alen = 0;
1011 su->sdl.sdl_slen = 0;
984263bc
MD
1012 }
1013 freeifaddrs(ifap);
bc82d1bc 1014 if (sdl != NULL)
984263bc
MD
1015 return(1);
1016 }
1017 break;
1018 case RTA_NETMASK:
1019 su = &so_mask;
1020 break;
1021 case RTA_GENMASK:
1022 su = &so_genmask;
1023 break;
1024 case RTA_IFP:
1025 su = &so_ifp;
1026 afamily = AF_LINK;
1027 break;
1028 case RTA_IFA:
1029 su = &so_ifa;
1030 break;
9b42cabe
NA
1031 case RTA_MPLS1:
1032 su = &so_mpls1;
1033 break;
1034 case RTA_MPLS2:
1035 su = &so_mpls2;
1036 break;
1037 case RTA_MPLS3:
1038 su = &so_mpls3;
1039 break;
984263bc
MD
1040 default:
1041 usage("internal error");
1042 /*NOTREACHED*/
1043 }
1044 su->sa.sa_len = aflen;
147a7be0
MD
1045 su->sa.sa_family = afamily; /* cases that don't want it have left
1046 already */
5af3bab0 1047 if (strcmp(str, "default") == 0) {
984263bc 1048 /*
147a7be0 1049 * Default is net 0.0.0.0/0
984263bc
MD
1050 */
1051 switch (which) {
1052 case RTA_DST:
1053 forcenet++;
1054 /* bzero(su, sizeof(*su)); *//* for readability */
5af3bab0 1055 getaddr(RTA_NETMASK, str, 0);
984263bc
MD
1056 break;
1057 case RTA_NETMASK:
1058 case RTA_GENMASK:
1059 /* bzero(su, sizeof(*su)); *//* for readability */
8af0d623 1060 break;
984263bc 1061 }
147a7be0 1062 return(0);
984263bc
MD
1063 }
1064 switch (afamily) {
1065#ifdef INET6
1066 case AF_INET6:
1067 {
1068 struct addrinfo hints, *res;
1069
1070 q = NULL;
5af3bab0 1071 if (which == RTA_DST && (q = strchr(str, '/')) != NULL)
984263bc
MD
1072 *q = '\0';
1073 memset(&hints, 0, sizeof(hints));
1074 hints.ai_family = afamily; /*AF_INET6*/
1075 hints.ai_flags = AI_NUMERICHOST;
1076 hints.ai_socktype = SOCK_DGRAM; /*dummy*/
5af3bab0 1077 if (getaddrinfo(str, "0", &hints, &res) != 0 ||
984263bc
MD
1078 res->ai_family != AF_INET6 ||
1079 res->ai_addrlen != sizeof(su->sin6)) {
5af3bab0 1080 fprintf(stderr, "%s: bad value\n", str);
984263bc
MD
1081 exit(1);
1082 }
1083 memcpy(&su->sin6, res->ai_addr, sizeof(su->sin6));
1084#ifdef __KAME__
1085 if ((IN6_IS_ADDR_LINKLOCAL(&su->sin6.sin6_addr) ||
1086 IN6_IS_ADDR_LINKLOCAL(&su->sin6.sin6_addr)) &&
1087 su->sin6.sin6_scope_id) {
1088 *(u_int16_t *)&su->sin6.sin6_addr.s6_addr[2] =
1089 htons(su->sin6.sin6_scope_id);
1090 su->sin6.sin6_scope_id = 0;
1091 }
1092#endif
1093 freeaddrinfo(res);
1094 if (q != NULL)
1095 *q++ = '/';
1096 if (which == RTA_DST)
147a7be0
MD
1097 return(inet6_makenetandmask(&su->sin6, q));
1098 return(0);
984263bc
MD
1099 }
1100#endif /* INET6 */
1101
984263bc 1102 case AF_LINK:
5af3bab0 1103 link_addr(str, &su->sdl);
147a7be0 1104 return(1);
984263bc 1105
984263bc
MD
1106 case PF_ROUTE:
1107 su->sa.sa_len = sizeof(*su);
5af3bab0 1108 sockaddr(str, &su->sa);
147a7be0 1109 return(1);
984263bc 1110
9b42cabe
NA
1111 case AF_MPLS:
1112 {
1113 mpls_label_t label;
1114
1115 bzero(su, sizeof(*su));
1116 su->sa.sa_len = sizeof(*su);
1117 su->sa.sa_family = AF_MPLS;
1118 su->smpls.smpls_op = mplsop;
1119 if (mplsop != MPLSLOP_POP && mplsop != MPLSLOP_POPALL &&
1120 *str != '\0') {
1121 if (sscanf(str, "%u", &label) != 1)
1122 errx(EX_NOHOST, "bad address: %s", str);
1123 su->smpls.smpls_label = htonl(label);
1124 }
1125 return(1);
1126 }
1127
984263bc
MD
1128 case AF_INET:
1129 default:
1130 break;
1131 }
1132
1133 if (hpp == NULL)
1134 hpp = &hp;
1135 *hpp = NULL;
1136
5af3bab0 1137 q = strchr(str,'/');
bc82d1bc 1138 if (q != NULL && which == RTA_DST) {
984263bc 1139 *q = '\0';
5af3bab0 1140 if ((val = inet_network(str)) != INADDR_NONE) {
984263bc
MD
1141 inet_makenetandmask(
1142 val, &su->sin, strtoul(q+1, 0, 0));
147a7be0 1143 return(0);
984263bc
MD
1144 }
1145 *q = '/';
1146 }
1147 if ((which != RTA_DST || forcenet == 0) &&
5af3bab0 1148 inet_aton(str, &su->sin.sin_addr)) {
984263bc
MD
1149 val = su->sin.sin_addr.s_addr;
1150 if (which != RTA_DST ||
1151 inet_lnaof(su->sin.sin_addr) != INADDR_ANY)
147a7be0 1152 return(1);
984263bc
MD
1153 else {
1154 val = ntohl(val);
1155 goto netdone;
1156 }
1157 }
1158 if (which == RTA_DST && forcehost == 0 &&
5af3bab0
CP
1159 ((val = inet_network(str)) != INADDR_NONE ||
1160 ((np = getnetbyname(str)) != NULL && (val = np->n_net) != 0))) {
984263bc
MD
1161netdone:
1162 inet_makenetandmask(val, &su->sin, 0);
147a7be0 1163 return(0);
984263bc 1164 }
5af3bab0 1165 hp = gethostbyname(str);
bc82d1bc 1166 if (hp != NULL) {
984263bc
MD
1167 *hpp = hp;
1168 su->sin.sin_family = hp->h_addrtype;
1169 memmove((char *)&su->sin.sin_addr, hp->h_addr,
5af3bab0 1170 MIN((size_t)hp->h_length, sizeof(su->sin.sin_addr)));
147a7be0 1171 return(1);
984263bc 1172 }
5af3bab0 1173 errx(EX_NOHOST, "bad address: %s", str);
984263bc
MD
1174}
1175
5af3bab0
CP
1176static int
1177prefixlen(const char *len_str)
984263bc 1178{
5af3bab0 1179 int len = atoi(len_str), q, r;
984263bc
MD
1180 int max;
1181 char *p;
1182
b69d41e9 1183 rtm_addrs |= RTA_NETMASK;
984263bc
MD
1184 switch (af) {
1185#ifdef INET6
1186 case AF_INET6:
1187 max = 128;
1188 p = (char *)&so_mask.sin6.sin6_addr;
1189 break;
1190#endif
1191 case AF_INET:
1192 max = 32;
1193 p = (char *)&so_mask.sin.sin_addr;
1194 break;
1195 default:
147a7be0 1196 fprintf(stderr, "prefixlen not supported in this af\n");
984263bc
MD
1197 exit(1);
1198 /*NOTREACHED*/
1199 }
1200
1201 if (len < 0 || max < len) {
5af3bab0 1202 fprintf(stderr, "%s: bad value\n", len_str);
984263bc
MD
1203 exit(1);
1204 }
b69d41e9 1205
984263bc
MD
1206 q = len >> 3;
1207 r = len & 7;
1208 so_mask.sa.sa_family = af;
1209 so_mask.sa.sa_len = aflen;
1210 memset((void *)p, 0, max / 8);
1211 if (q > 0)
1212 memset((void *)p, 0xff, q);
1213 if (r > 0)
1214 *((u_char *)p + q) = (0xff00 >> r) & 0xff;
1215 if (len == max)
147a7be0 1216 return(-1);
984263bc 1217 else
147a7be0 1218 return(len);
984263bc
MD
1219}
1220
5af3bab0 1221static void
b5744197 1222interfaces(void)
984263bc
MD
1223{
1224 size_t needed;
1225 int mib[6];
1226 char *buf, *lim, *next;
147a7be0 1227 struct rt_msghdr *rtm;
984263bc
MD
1228
1229 mib[0] = CTL_NET;
1230 mib[1] = PF_ROUTE;
1231 mib[2] = 0; /* protocol */
1232 mib[3] = 0; /* wildcard address family */
1233 mib[4] = NET_RT_IFLIST;
1234 mib[5] = 0; /* no flags */
1235 if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
1236 err(EX_OSERR, "route-sysctl-estimate");
1237 if ((buf = malloc(needed)) == NULL)
1238 errx(EX_OSERR, "malloc failed");
1239 if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
1240 err(EX_OSERR, "actual retrieval of interface table");
1241 lim = buf + needed;
1242 for (next = buf; next < lim; next += rtm->rtm_msglen) {
1243 rtm = (struct rt_msghdr *)next;
1244 print_rtmsg(rtm, rtm->rtm_msglen);
1245 }
1246}
1247
5af3bab0 1248static void
b5744197 1249monitor(void)
984263bc 1250{
2789028f 1251 int n, error;
984263bc 1252 char msg[2048];
2789028f 1253 time_t now;
984263bc
MD
1254
1255 verbose = 1;
1256 if (debugonly) {
1257 interfaces();
1258 exit(0);
1259 }
2789028f 1260
147a7be0 1261 for (;;) {
2789028f
RM
1262 n = read(s, msg, sizeof(msg));
1263 error = errno;
984263bc 1264 now = time(NULL);
2789028f
RM
1265 if (n == -1) {
1266 printf("\nsocket error %d on %s: %s\n",
1267 error, ctime(&now), strerror(error));
1268 continue;
1269 }
147a7be0 1270 printf("\ngot message of size %d on %s", n, ctime(&now));
984263bc
MD
1271 print_rtmsg((struct rt_msghdr *)msg, n);
1272 }
1273}
1274
1275struct {
1276 struct rt_msghdr m_rtm;
1277 char m_space[512];
1278} m_rtmsg;
1279
5af3bab0 1280static int
b5744197 1281rtmsg(int cmd, int flags)
984263bc
MD
1282{
1283 static int seq;
1284 int rlen;
147a7be0
MD
1285 char *cp = m_rtmsg.m_space;
1286 int l;
984263bc
MD
1287
1288#define NEXTADDR(w, u) \
1289 if (rtm_addrs & (w)) {\
4ff4d99f 1290 l = RT_ROUNDUP(u.sa.sa_len); memmove(cp, &(u), l); cp += l;\
984263bc
MD
1291 if (verbose) sodump(&(u),"u");\
1292 }
1293
1294 errno = 0;
1295 memset(&m_rtmsg, 0, sizeof(m_rtmsg));
1296 if (cmd == 'a')
1297 cmd = RTM_ADD;
1298 else if (cmd == 'c')
1299 cmd = RTM_CHANGE;
1300 else if (cmd == 'g') {
1301 cmd = RTM_GET;
1302 if (so_ifp.sa.sa_family == 0) {
1303 so_ifp.sa.sa_family = AF_LINK;
1304 so_ifp.sa.sa_len = sizeof(struct sockaddr_dl);
1305 rtm_addrs |= RTA_IFP;
1306 }
1307 } else
1308 cmd = RTM_DELETE;
1309#define rtm m_rtmsg.m_rtm
1310 rtm.rtm_type = cmd;
1311 rtm.rtm_flags = flags;
1312 rtm.rtm_version = RTM_VERSION;
1313 rtm.rtm_seq = ++seq;
1314 rtm.rtm_addrs = rtm_addrs;
1315 rtm.rtm_rmx = rt_metrics;
1316 rtm.rtm_inits = rtm_inits;
1317
1318 if (rtm_addrs & RTA_NETMASK)
1319 mask_addr();
1320 NEXTADDR(RTA_DST, so_dst);
1321 NEXTADDR(RTA_GATEWAY, so_gate);
1322 NEXTADDR(RTA_NETMASK, so_mask);
1323 NEXTADDR(RTA_GENMASK, so_genmask);
1324 NEXTADDR(RTA_IFP, so_ifp);
1325 NEXTADDR(RTA_IFA, so_ifa);
9b42cabe
NA
1326 NEXTADDR(RTA_MPLS1, so_mpls1);
1327 NEXTADDR(RTA_MPLS2, so_mpls2);
1328 NEXTADDR(RTA_MPLS3, so_mpls3);
984263bc
MD
1329 rtm.rtm_msglen = l = cp - (char *)&m_rtmsg;
1330 if (verbose)
1331 print_rtmsg(&rtm, l);
1332 if (debugonly)
147a7be0 1333 return(0);
984263bc
MD
1334 if ((rlen = write(s, (char *)&m_rtmsg, l)) < 0) {
1335 warn("writing to routing socket");
147a7be0 1336 return(-1);
984263bc
MD
1337 }
1338 if (cmd == RTM_GET) {
1339 do {
1340 l = read(s, (char *)&m_rtmsg, sizeof(m_rtmsg));
1341 } while (l > 0 && (rtm.rtm_seq != seq || rtm.rtm_pid != pid));
1342 if (l < 0)
1343 warn("read from routing socket");
1344 else
1345 print_getmsg(&rtm, l);
1346 }
1347#undef rtm
147a7be0 1348 return(0);
984263bc
MD
1349}
1350
5af3bab0 1351static void
b5744197 1352mask_addr(void)
984263bc
MD
1353{
1354 int olen = so_mask.sa.sa_len;
147a7be0 1355 char *cp1 = olen + (char *)&so_mask, *cp2;
984263bc
MD
1356
1357 for (so_mask.sa.sa_len = 0; cp1 > (char *)&so_mask; )
1358 if (*--cp1 != 0) {
1359 so_mask.sa.sa_len = 1 + cp1 - (char *)&so_mask;
1360 break;
1361 }
1362 if ((rtm_addrs & RTA_DST) == 0)
1363 return;
1364 switch (so_dst.sa.sa_family) {
984263bc
MD
1365 case AF_INET:
1366#ifdef INET6
1367 case AF_INET6:
1368#endif
984263bc
MD
1369 case 0:
1370 return;
1371 }
1372 cp1 = so_mask.sa.sa_len + 1 + (char *)&so_dst;
1373 cp2 = so_dst.sa.sa_len + 1 + (char *)&so_dst;
1374 while (cp2 > cp1)
1375 *--cp2 = 0;
1376 cp2 = so_mask.sa.sa_len + 1 + (char *)&so_mask;
1377 while (cp1 > so_dst.sa.sa_data)
1378 *--cp1 &= *--cp2;
1379}
1380
5af3bab0 1381const char *msgtypes[] = {
984263bc
MD
1382 "",
1383 "RTM_ADD: Add Route",
1384 "RTM_DELETE: Delete Route",
1385 "RTM_CHANGE: Change Metrics or flags",
1386 "RTM_GET: Report Metrics",
1387 "RTM_LOSING: Kernel Suspects Partitioning",
1388 "RTM_REDIRECT: Told to use different route",
1389 "RTM_MISS: Lookup failed on this address",
1390 "RTM_LOCK: fix specified metrics",
ac6b17f6
SZ
1391 "0x9: unused",
1392 "0xa: unused",
984263bc
MD
1393 "RTM_RESOLVE: Route created by cloning",
1394 "RTM_NEWADDR: address being added to iface",
1395 "RTM_DELADDR: address being removed from iface",
1396 "RTM_IFINFO: iface status change",
1397 "RTM_NEWMADDR: new multicast group membership on iface",
1398 "RTM_DELMADDR: multicast group membership removed from iface",
1399 "RTM_IFANNOUNCE: interface arrival/departure",
e9a046d0 1400 "RTM_IEEE80211: IEEE80211 wireless event",
984263bc
MD
1401 0,
1402};
1403
1404char metricnames[] =
bf522a91
SZ
1405"\013iw\012iwmax\011msl\010rttvar\7rtt\6ssthresh\5sendpipe\4recvpipe\3expire"
1406"\2hopcount\1mtu";
984263bc
MD
1407char routeflags[] =
1408"\1UP\2GATEWAY\3HOST\4REJECT\5DYNAMIC\6MODIFIED\7DONE\010MASK_PRESENT"
1409"\011CLONING\012XRESOLVE\013LLINFO\014STATIC\015BLACKHOLE\016b016"
1410"\017PROTO2\020PROTO1\021PRCLONING\022WASCLONED\023PROTO3\024CHAINDELETE"
1411"\025PINNED\026LOCAL\027BROADCAST\030MULTICAST";
1412char ifnetflags[] =
1413"\1UP\2BROADCAST\3DEBUG\4LOOPBACK\5PTP\6b6\7RUNNING\010NOARP"
1414"\011PPROMISC\012ALLMULTI\013OACTIVE\014SIMPLEX\015LINK0\016LINK1"
1415"\017LINK2\020MULTICAST";
1416char addrnames[] =
9b42cabe
NA
1417"\1DST\2GATEWAY\3NETMASK\4GENMASK\5IFP\6IFA\7AUTHOR\010BRD"
1418"\011MPLS1\012MPLS2\013MPLS3";
984263bc 1419
4c2c6a9c
RM
1420static const char *
1421linkstate(struct if_msghdr *ifm)
1422{
1423 static char buf[64];
1424
1425 switch (ifm->ifm_data.ifi_link_state) {
1426 case LINK_STATE_UNKNOWN:
1427 return "carrier: unknown";
1428 case LINK_STATE_DOWN:
1429 return "carrier: no carrier";
1430 case LINK_STATE_UP:
1431 return "carrier: active";
1432 default:
1433 (void)snprintf(buf, sizeof(buf), "carrier: 0x%lx",
1434 ifm->ifm_data.ifi_link_state);
1435 return buf;
1436 }
1437}
1438
5af3bab0
CP
1439static void
1440print_rtmsg(struct rt_msghdr *rtm, int msglen __unused)
984263bc
MD
1441{
1442 struct if_msghdr *ifm;
1443 struct ifa_msghdr *ifam;
1444#ifdef RTM_NEWMADDR
1445 struct ifma_msghdr *ifmam;
1446#endif
1447 struct if_announcemsghdr *ifan;
e9a046d0
RM
1448 union {
1449 struct ieee80211_join_event join;
1450 struct ieee80211_leave_event leave;
1451 struct ieee80211_replay_event replay;
1452 struct ieee80211_michael_event michael;
1453 } ev;
1454 size_t evlen = 0;
984263bc
MD
1455
1456 if (verbose == 0)
1457 return;
1458 if (rtm->rtm_version != RTM_VERSION) {
147a7be0 1459 printf("routing message version %d not understood\n",
984263bc
MD
1460 rtm->rtm_version);
1461 return;
1462 }
1463 if (msgtypes[rtm->rtm_type] != NULL)
147a7be0 1464 printf("%s: ", msgtypes[rtm->rtm_type]);
984263bc 1465 else
147a7be0
MD
1466 printf("#%d: ", rtm->rtm_type);
1467 printf("len %d, ", rtm->rtm_msglen);
984263bc
MD
1468 switch (rtm->rtm_type) {
1469 case RTM_IFINFO:
1470 ifm = (struct if_msghdr *)rtm;
4c2c6a9c 1471 printf("if# %d, %s, flags:", ifm->ifm_index, linkstate(ifm));
984263bc
MD
1472 bprintf(stdout, ifm->ifm_flags, ifnetflags);
1473 pmsg_addrs((char *)(ifm + 1), ifm->ifm_addrs);
1474 break;
1475 case RTM_NEWADDR:
1476 case RTM_DELADDR:
1477 ifam = (struct ifa_msghdr *)rtm;
147a7be0 1478 printf("metric %d, flags:", ifam->ifam_metric);
984263bc
MD
1479 bprintf(stdout, ifam->ifam_flags, routeflags);
1480 pmsg_addrs((char *)(ifam + 1), ifam->ifam_addrs);
1481 break;
1482#ifdef RTM_NEWMADDR
1483 case RTM_NEWMADDR:
1484 case RTM_DELMADDR:
1485 ifmam = (struct ifma_msghdr *)rtm;
1486 pmsg_addrs((char *)(ifmam + 1), ifmam->ifmam_addrs);
1487 break;
1488#endif
e9a046d0
RM
1489 case RTM_IEEE80211:
1490 ifan = (struct if_announcemsghdr *)rtm;
1491 (void)printf("if# %d, what: ", ifan->ifan_index);
1492 switch (ifan->ifan_what) {
1493 case RTM_IEEE80211_ASSOC:
1494 printf("associate");
1495 break;
1496 case RTM_IEEE80211_REASSOC:
1497 printf("re-associate");
1498 break;
1499 case RTM_IEEE80211_DISASSOC:
1500 printf("disassociate");
1501 break;
1502 case RTM_IEEE80211_SCAN:
1503 printf("scan complete");
1504 break;
1505 case RTM_IEEE80211_JOIN:
1506 evlen = sizeof(ev.join);
1507 printf("join");
1508 break;
1509 case RTM_IEEE80211_LEAVE:
1510 evlen = sizeof(ev.leave);
1511 printf("leave");
1512 break;
1513 case RTM_IEEE80211_MICHAEL:
1514 evlen = sizeof(ev.michael);
1515 printf("michael");
1516 break;
1517 case RTM_IEEE80211_REPLAY:
1518 evlen = sizeof(ev.replay);
1519 printf("replay");
1520 break;
1521 default:
1522 evlen = 0;
1523 printf("#%d", ifan->ifan_what);
1524 break;
1525 }
1526 if (sizeof(*ifan) + evlen > ifan->ifan_msglen) {
1527 printf(" (truncated)\n");
1528 break;
1529 }
1530 (void)memcpy(&ev, (ifan + 1), evlen);
1531 switch (ifan->ifan_what) {
1532 case RTM_IEEE80211_JOIN:
1533 case RTM_IEEE80211_LEAVE:
1534 printf(" mac %" PRIETHER,
1535 PRIETHER_ARGS(ev.join.iev_addr));
1536 break;
1537 case RTM_IEEE80211_REPLAY:
1538 case RTM_IEEE80211_MICHAEL:
1539 printf(" src %" PRIETHER " dst %" PRIETHER
1540 " cipher %" PRIu8 " keyix %" PRIu8,
1541 PRIETHER_ARGS(ev.replay.iev_src),
1542 PRIETHER_ARGS(ev.replay.iev_dst),
1543 ev.replay.iev_cipher,
1544 ev.replay.iev_keyix);
1545 if (ifan->ifan_what == RTM_IEEE80211_REPLAY) {
1546 printf(" key rsc %#" PRIx64
1547 " frame rsc %#" PRIx64,
1548 ev.replay.iev_keyrsc, ev.replay.iev_rsc);
1549 }
1550 break;
1551 default:
1552 break;
1553 }
1554 printf("\n");
1555 break;
984263bc
MD
1556 case RTM_IFANNOUNCE:
1557 ifan = (struct if_announcemsghdr *)rtm;
147a7be0 1558 printf("if# %d, what: ", ifan->ifan_index);
984263bc
MD
1559 switch (ifan->ifan_what) {
1560 case IFAN_ARRIVAL:
1561 printf("arrival");
1562 break;
1563 case IFAN_DEPARTURE:
1564 printf("departure");
1565 break;
1566 default:
1567 printf("#%d", ifan->ifan_what);
1568 break;
1569 }
1570 printf("\n");
1571 break;
1572
1573 default:
147a7be0 1574 printf("pid: %ld, seq %d, errno %d, flags:",
984263bc
MD
1575 (long)rtm->rtm_pid, rtm->rtm_seq, rtm->rtm_errno);
1576 bprintf(stdout, rtm->rtm_flags, routeflags);
1577 pmsg_common(rtm);
1578 }
1579}
1580
5af3bab0 1581static void
147a7be0 1582print_getmsg(struct rt_msghdr *rtm, int msglen)
984263bc
MD
1583{
1584 struct sockaddr *dst = NULL, *gate = NULL, *mask = NULL;
1585 struct sockaddr_dl *ifp = NULL;
147a7be0
MD
1586 struct sockaddr *sa;
1587 char *cp;
1588 int i;
984263bc 1589
147a7be0 1590 printf(" route to: %s\n", routename(&so_dst.sa));
984263bc
MD
1591 if (rtm->rtm_version != RTM_VERSION) {
1592 warnx("routing message version %d not understood",
1593 rtm->rtm_version);
1594 return;
1595 }
1596 if (rtm->rtm_msglen > msglen) {
1597 warnx("message length mismatch, in packet %d, returned %d",
1598 rtm->rtm_msglen, msglen);
1599 }
bc82d1bc 1600 if (rtm->rtm_errno != 0) {
984263bc
MD
1601 errno = rtm->rtm_errno;
1602 warn("message indicates error %d", errno);
1603 return;
1604 }
1605 cp = ((char *)(rtm + 1));
1606 if (rtm->rtm_addrs)
bc82d1bc 1607 for (i = 1; i != 0; i <<= 1)
984263bc
MD
1608 if (i & rtm->rtm_addrs) {
1609 sa = (struct sockaddr *)cp;
1610 switch (i) {
1611 case RTA_DST:
1612 dst = sa;
1613 break;
1614 case RTA_GATEWAY:
1615 gate = sa;
1616 break;
1617 case RTA_NETMASK:
1618 mask = sa;
1619 break;
1620 case RTA_IFP:
1621 if (sa->sa_family == AF_LINK &&
1622 ((struct sockaddr_dl *)sa)->sdl_nlen)
1623 ifp = (struct sockaddr_dl *)sa;
1624 break;
1625 }
4ff4d99f 1626 RT_ADVANCE(cp, sa);
984263bc 1627 }
bc82d1bc 1628 if (dst != NULL && mask != NULL)
984263bc 1629 mask->sa_family = dst->sa_family; /* XXX */
bc82d1bc 1630 if (dst != NULL)
147a7be0 1631 printf("destination: %s\n", routename(dst));
bc82d1bc 1632 if (mask != NULL) {
984263bc
MD
1633 int savenflag = nflag;
1634
1635 nflag = 1;
147a7be0 1636 printf(" mask: %s\n", routename(mask));
984263bc
MD
1637 nflag = savenflag;
1638 }
bc82d1bc 1639 if (gate != NULL && rtm->rtm_flags & RTF_GATEWAY)
147a7be0 1640 printf(" gateway: %s\n", routename(gate));
bc82d1bc 1641 if (ifp != NULL)
147a7be0 1642 printf(" interface: %.*s\n",
984263bc 1643 ifp->sdl_nlen, ifp->sdl_data);
147a7be0 1644 printf(" flags: ");
984263bc
MD
1645 bprintf(stdout, rtm->rtm_flags, routeflags);
1646
1647#define lock(f) ((rtm->rtm_rmx.rmx_locks & __CONCAT(RTV_,f)) ? 'L' : ' ')
1648#define msec(u) (((u) + 500) / 1000) /* usec to msec */
1649
147a7be0 1650 printf("\n%s\n", "\
bf522a91 1651 recvpipe sendpipe ssthresh rtt,msec rttvar hopcount mtu expire msl,msec iwmax iw");
984263bc
MD
1652 printf("%8ld%c ", rtm->rtm_rmx.rmx_recvpipe, lock(RPIPE));
1653 printf("%8ld%c ", rtm->rtm_rmx.rmx_sendpipe, lock(SPIPE));
1654 printf("%8ld%c ", rtm->rtm_rmx.rmx_ssthresh, lock(SSTHRESH));
1655 printf("%8ld%c ", msec(rtm->rtm_rmx.rmx_rtt), lock(RTT));
1656 printf("%8ld%c ", msec(rtm->rtm_rmx.rmx_rttvar), lock(RTTVAR));
1657 printf("%8ld%c ", rtm->rtm_rmx.rmx_hopcount, lock(HOPCOUNT));
1658 printf("%8ld%c ", rtm->rtm_rmx.rmx_mtu, lock(MTU));
cec73927
MD
1659 if (rtm->rtm_rmx.rmx_expire != 0) {
1660 struct timespec sp;
748810cc
MD
1661 clock_gettime(CLOCK_MONOTONIC, &sp);
1662 rtm->rtm_rmx.rmx_expire -= sp.tv_sec;
cec73927 1663 }
8ec89e60 1664 printf("%8ld%c ", rtm->rtm_rmx.rmx_expire, lock(EXPIRE));
bf522a91
SZ
1665 printf("%8ld ", rtm->rtm_rmx.rmx_msl);
1666 printf("%5ld ", rtm->rtm_rmx.rmx_iwmaxsegs);
1667 printf("%3ld\n", rtm->rtm_rmx.rmx_iwcapsegs);
984263bc
MD
1668#undef lock
1669#undef msec
1670#define RTA_IGN (RTA_DST|RTA_GATEWAY|RTA_NETMASK|RTA_IFP|RTA_IFA|RTA_BRD)
1671 if (verbose)
1672 pmsg_common(rtm);
1673 else if (rtm->rtm_addrs &~ RTA_IGN) {
147a7be0 1674 printf("sockaddrs: ");
984263bc
MD
1675 bprintf(stdout, rtm->rtm_addrs, addrnames);
1676 putchar('\n');
1677 }
1678#undef RTA_IGN
1679}
1680
5af3bab0 1681static void
147a7be0 1682pmsg_common(struct rt_msghdr *rtm)
984263bc 1683{
147a7be0 1684 printf("\nlocks: ");
984263bc 1685 bprintf(stdout, rtm->rtm_rmx.rmx_locks, metricnames);
147a7be0 1686 printf(" inits: ");
984263bc
MD
1687 bprintf(stdout, rtm->rtm_inits, metricnames);
1688 pmsg_addrs(((char *)(rtm + 1)), rtm->rtm_addrs);
1689}
1690
5af3bab0 1691static void
b5744197 1692pmsg_addrs(char *cp, int addrs)
984263bc 1693{
147a7be0 1694 struct sockaddr *sa;
984263bc
MD
1695 int i;
1696
1697 if (addrs == 0) {
147a7be0 1698 putchar('\n');
984263bc
MD
1699 return;
1700 }
147a7be0 1701 printf("\nsockaddrs: ");
984263bc 1702 bprintf(stdout, addrs, addrnames);
147a7be0 1703 putchar('\n');
bc82d1bc 1704 for (i = 1; i != 0; i <<= 1)
984263bc
MD
1705 if (i & addrs) {
1706 sa = (struct sockaddr *)cp;
147a7be0 1707 printf(" %s", routename(sa));
4ff4d99f 1708 RT_ADVANCE(cp, sa);
984263bc 1709 }
147a7be0
MD
1710 putchar('\n');
1711 fflush(stdout);
984263bc
MD
1712}
1713
5af3bab0
CP
1714static void
1715bprintf(FILE *fp, int b, u_char *str)
984263bc 1716{
147a7be0 1717 int i;
984263bc
MD
1718 int gotsome = 0;
1719
1720 if (b == 0)
1721 return;
5af3bab0 1722 while ((i = *str++) != 0) {
984263bc 1723 if (b & (1 << (i-1))) {
bc82d1bc 1724 if (!gotsome)
984263bc
MD
1725 i = '<';
1726 else
1727 i = ',';
147a7be0 1728 putc(i, fp);
984263bc 1729 gotsome = 1;
5af3bab0 1730 for (; (i = *str) > 32; str++)
147a7be0 1731 putc(i, fp);
984263bc 1732 } else
5af3bab0
CP
1733 while (*str > 32)
1734 str++;
984263bc
MD
1735 }
1736 if (gotsome)
147a7be0 1737 putc('>', fp);
984263bc
MD
1738}
1739
1740int
5af3bab0 1741keyword(const char *cp)
984263bc 1742{
166dccaa 1743 const struct keytab *kt = keywords;
984263bc 1744
bc82d1bc 1745 while (kt->kt_cp != NULL && strcmp(kt->kt_cp, cp) != 0)
984263bc 1746 kt++;
147a7be0 1747 return(kt->kt_i);
984263bc
MD
1748}
1749
5af3bab0
CP
1750static void
1751sodump(sup su, const char *which)
984263bc
MD
1752{
1753 switch (su->sa.sa_family) {
1754 case AF_LINK:
147a7be0 1755 printf("%s: link %s; ",
984263bc
MD
1756 which, link_ntoa(&su->sdl));
1757 break;
1758 case AF_INET:
147a7be0 1759 printf("%s: inet %s; ",
984263bc
MD
1760 which, inet_ntoa(su->sin.sin_addr));
1761 break;
984263bc 1762 }
147a7be0 1763 fflush(stdout);
984263bc
MD
1764}
1765
1766/* States*/
1767#define VIRGIN 0
1768#define GOTONE 1
1769#define GOTTWO 2
1770/* Inputs */
1771#define DIGIT (4*0)
1772#define END (4*1)
1773#define DELIM (4*2)
1774
5af3bab0 1775static void
ed6c7133 1776sockaddr(const char *addr, struct sockaddr *sa)
984263bc 1777{
147a7be0 1778 char *cp = (char *)sa;
984263bc
MD
1779 int size = sa->sa_len;
1780 char *cplim = cp + size;
147a7be0 1781 int byte = 0, state = VIRGIN, new = 0 /* foil gcc */;
984263bc
MD
1782
1783 memset(cp, 0, size);
1784 cp++;
1785 do {
1786 if ((*addr >= '0') && (*addr <= '9')) {
1787 new = *addr - '0';
1788 } else if ((*addr >= 'a') && (*addr <= 'f')) {
1789 new = *addr - 'a' + 10;
1790 } else if ((*addr >= 'A') && (*addr <= 'F')) {
1791 new = *addr - 'A' + 10;
bc82d1bc 1792 } else if (*addr == '\0')
984263bc
MD
1793 state |= END;
1794 else
1795 state |= DELIM;
1796 addr++;
1797 switch (state /* | INPUT */) {
1798 case GOTTWO | DIGIT:
1799 *cp++ = byte; /*FALLTHROUGH*/
1800 case VIRGIN | DIGIT:
1801 state = GOTONE; byte = new; continue;
1802 case GOTONE | DIGIT:
1803 state = GOTTWO; byte = new + (byte << 4); continue;
1804 default: /* | DELIM */
1805 state = VIRGIN; *cp++ = byte; byte = 0; continue;
1806 case GOTONE | END:
1807 case GOTTWO | END:
1808 *cp++ = byte; /* FALLTHROUGH */
1809 case VIRGIN | END:
1810 break;
1811 }
1812 break;
1813 } while (cp < cplim);
1814 sa->sa_len = cp - (char *)sa;
1815}