2 * Copyright (c) 1983, 1989, 1991, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
13 * 3. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
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
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 $
34 #include <sys/param.h>
36 #include <sys/socket.h>
37 #include <sys/ioctl.h>
38 #include <sys/sysctl.h>
39 #include <sys/types.h>
42 #include <net/route.h>
43 #include <net/if_dl.h>
44 #include <netinet/in.h>
45 #include <netinet/if_ether.h>
46 #include <arpa/inet.h>
49 #include <netproto/mpls/mpls.h>
67 struct sockaddr_in sin
;
69 struct sockaddr_in6 sin6
;
71 struct sockaddr_mpls smpls
;
72 struct sockaddr_dl sdl
;
73 struct sockaddr_inarp sinarp
;
74 struct sockaddr_storage ss
; /* added to avoid memory overrun */
75 } so_dst
, so_gate
, so_mask
, so_genmask
, so_ifa
, so_ifp
, so_mpls1
,
78 typedef union sockunion
*sup
;
83 static struct sockaddr_in so_gate_tried
;
84 static struct rt_metrics rt_metrics
;
85 static int pid
, rtm_addrs
;
87 static int forcehost
, forcenet
, af
, qflag
, tflag
;
88 static int iflag
, verbose
, aflen
= sizeof(struct sockaddr_in
);
89 static int locking
, lockrest
, debugonly
;
90 static int mplsop
, popcount
, pushcount
, swapcount
;
91 static u_long rtm_inits
;
94 static void flushroutes(int, char **);
95 static void set_metric(char *, int);
96 static void newroute(int, char **);
97 static void inet_makenetandmask(u_long
, struct sockaddr_in
*, u_long
);
98 static void interfaces(void);
99 static void monitor(void);
100 static void sockaddr(const char *, struct sockaddr
*);
101 static void sodump(sup
, const char *);
102 static void bprintf(FILE *, int, u_char
*);
103 static void print_getmsg(struct rt_msghdr
*, int);
104 static void print_rtmsg(struct rt_msghdr
*, int);
105 static void pmsg_common(struct rt_msghdr
*);
106 static void pmsg_addrs(char *, int);
107 static void mask_addr(void);
108 static int getaddr(int, const char *, struct hostent
**);
109 static int rtmsg(int, int);
110 static int prefixlen(const char *);
112 static int inet6_makenetandmask(struct sockaddr_in6
*, const char *);
116 usage(const char *cp
)
119 warnx("bad keyword: %s", cp
);
120 fprintf(stderr
, "usage: route [-dnqtvw] [-c cpu] command [[modifiers] args]\n");
126 main(int argc
, char **argv
)
133 while ((ch
= getopt(argc
, argv
, "c:wnqdtv")) != -1)
136 cpuflag
= strtol(optarg
, NULL
, 0);
166 s
= open(_PATH_DEVNULL
, O_WRONLY
, 0);
168 s
= socket(PF_ROUTE
, SOCK_RAW
, 0);
170 err(EX_OSERR
, "socket");
172 switch (keyword(*argv
)) {
180 newroute(argc
, argv
);
192 flushroutes(argc
, argv
);
201 * Purge all entries in the routing tables not
202 * associated with network interfaces.
205 flushroutes(int argc
, char **argv
)
208 int mib
[7], rlen
, seqno
;
210 char *buf
, *next
, *lim
;
211 struct rt_msghdr
*rtm
;
214 errx(EX_NOPERM
, "must be root to alter routing table");
216 shutdown(s
, SHUT_RD
); /* Don't want to read back our messages */
219 if (argc
== 2 && **argv
== '-')
220 switch (keyword(*argv
+ 1)) {
242 mib
[2] = 0; /* protocol */
243 mib
[3] = 0; /* wildcard address family */
244 mib
[4] = NET_RT_DUMP
;
245 mib
[5] = 0; /* no flags */
252 if (sysctl(mib
, miblen
, NULL
, &needed
, NULL
, 0) < 0)
253 err(EX_OSERR
, "route-sysctl-estimate");
254 if ((buf
= malloc(needed
)) == NULL
)
255 errx(EX_OSERR
, "malloc failed");
256 if (sysctl(mib
, miblen
, buf
, &needed
, NULL
, 0) < 0)
257 err(EX_OSERR
, "route-sysctl-get");
260 printf("Examining routing table from sysctl\n");
262 for (next
= buf
; next
< lim
; next
+= rtm
->rtm_msglen
) {
263 rtm
= (struct rt_msghdr
*)next
;
265 print_rtmsg(rtm
, rtm
->rtm_msglen
);
266 if ((rtm
->rtm_flags
& RTF_GATEWAY
) == 0)
269 struct sockaddr
*sa
= (struct sockaddr
*)(rtm
+ 1);
271 if (sa
->sa_family
!= af
)
276 rtm
->rtm_type
= RTM_DELETE
;
277 rtm
->rtm_seq
= seqno
;
278 rlen
= write(s
, next
, rtm
->rtm_msglen
);
279 if (rlen
< (int)rtm
->rtm_msglen
) {
280 warn("write to routing socket");
281 printf("got only %d for rlen\n", rlen
);
288 print_rtmsg(rtm
, rlen
);
290 struct sockaddr
*sa
= (struct sockaddr
*)(rtm
+ 1);
292 printf("%-20s ", rtm
->rtm_flags
& RTF_HOST
?
293 routename(sa
) : netname(sa
));
295 printf("%-20.20s ", rtm
->rtm_flags
& RTF_HOST
?
296 routename(sa
) : netname(sa
));
298 sa
= (struct sockaddr
*)(RT_ROUNDUP(sa
->sa_len
) +
301 printf("%-20s ", routename(sa
));
303 printf("%-20.20s ", routename(sa
));
311 routename(struct sockaddr
*sa
)
314 static char line
[MAXHOSTNAMELEN
+ 1];
316 static char domain
[MAXHOSTNAMELEN
+ 1];
317 static int first
= 1;
321 if (gethostname(domain
, MAXHOSTNAMELEN
) == 0 &&
322 (cp
= strchr(domain
, '.'))) {
323 domain
[MAXHOSTNAMELEN
] = '\0';
324 strcpy(domain
, cp
+ 1);
330 strcpy(line
, "default");
331 else switch (sa
->sa_family
) {
335 in
= ((struct sockaddr_in
*)sa
)->sin_addr
;
338 if (in
.s_addr
== INADDR_ANY
|| sa
->sa_len
< 4)
340 if (cp
== NULL
&& !nflag
) {
341 hp
= gethostbyaddr(&in
, sizeof(struct in_addr
),
345 cptr
= strchr(hp
->h_name
, '.');
346 if (cptr
!= NULL
&& !wflag
&&
347 strcmp(cptr
+ 1, domain
) == 0)
353 strncpy(line
, cp
, sizeof(line
) - 1);
354 line
[sizeof(line
) - 1] = '\0';
356 sprintf(line
, "%s", inet_ntoa(in
));
363 struct sockaddr_in6 sin6
; /* use static var for safety */
365 #ifdef NI_WITHSCOPEID
366 niflags
= NI_WITHSCOPEID
;
369 memset(&sin6
, 0, sizeof(sin6
));
370 memcpy(&sin6
, sa
, sa
->sa_len
);
371 sin6
.sin6_len
= sizeof(struct sockaddr_in6
);
372 sin6
.sin6_family
= AF_INET6
;
374 if (sa
->sa_len
== sizeof(struct sockaddr_in6
) &&
375 (IN6_IS_ADDR_LINKLOCAL(&sin6
.sin6_addr
) ||
376 IN6_IS_ADDR_MC_LINKLOCAL(&sin6
.sin6_addr
)) &&
377 sin6
.sin6_scope_id
== 0) {
379 ntohs(*(u_int16_t
*)&sin6
.sin6_addr
.s6_addr
[2]);
380 sin6
.sin6_addr
.s6_addr
[2] = 0;
381 sin6
.sin6_addr
.s6_addr
[3] = 0;
385 niflags
|= NI_NUMERICHOST
;
386 if (getnameinfo((struct sockaddr
*)&sin6
, sin6
.sin6_len
,
387 line
, sizeof(line
), NULL
, 0, niflags
) != 0)
388 strncpy(line
, "invalid", sizeof(line
));
395 return(link_ntoa((struct sockaddr_dl
*)sa
));
400 * Unknown address family; just render the raw
401 * data in sa->sa_data as hex values.
403 uint8_t *sp
= (uint8_t *)sa
->sa_data
;
404 uint8_t *splim
= (uint8_t *)sa
+ sa
->sa_len
;
405 char *cps
= line
+ sprintf(line
, "(%d)", sa
->sa_family
);
406 char *cpe
= line
+ sizeof(line
);
408 while (sp
< splim
&& cps
< cpe
)
409 cps
+= snprintf(cps
, cpe
- cps
, " %02x", *sp
++);
417 * Return the name of the network whose address is given.
418 * The address is assumed to be that of a net or subnet, not a host.
421 netname(struct sockaddr
*sa
)
423 const char *cp
= NULL
;
424 static char line
[MAXHOSTNAMELEN
+ 1];
425 struct netent
*np
= NULL
;
430 switch (sa
->sa_family
) {
434 in
= ((struct sockaddr_in
*)sa
)->sin_addr
;
436 i
= in
.s_addr
= ntohl(in
.s_addr
);
441 mask
= IN_CLASSA_NET
;
443 } else if (IN_CLASSB(i
)) {
444 mask
= IN_CLASSB_NET
;
447 mask
= IN_CLASSC_NET
;
451 * If there are more bits than the standard mask
452 * would suggest, subnets must be in use.
453 * Guess at the subnet mask, assuming reasonable
454 * width subnet fields.
456 while (in
.s_addr
&~ mask
)
457 mask
|= mask
>> subnetshift
;
458 net
= in
.s_addr
& mask
;
459 while ((mask
& 1) == 0)
460 mask
>>= 1, net
>>= 1;
461 np
= getnetbyaddr(net
, AF_INET
);
465 #define C(x) (unsigned)((x) & 0xff)
467 strncpy(line
, cp
, sizeof(line
));
468 else if ((in
.s_addr
& 0xffffff) == 0)
469 sprintf(line
, "%u", C(in
.s_addr
>> 24));
470 else if ((in
.s_addr
& 0xffff) == 0)
471 sprintf(line
, "%u.%u", C(in
.s_addr
>> 24),
473 else if ((in
.s_addr
& 0xff) == 0)
474 sprintf(line
, "%u.%u.%u", C(in
.s_addr
>> 24),
475 C(in
.s_addr
>> 16), C(in
.s_addr
>> 8));
477 sprintf(line
, "%u.%u.%u.%u", C(in
.s_addr
>> 24),
478 C(in
.s_addr
>> 16), C(in
.s_addr
>> 8),
487 struct sockaddr_in6 sin6
; /* use static var for safety */
489 #ifdef NI_WITHSCOPEID
490 niflags
= NI_WITHSCOPEID
;
493 memset(&sin6
, 0, sizeof(sin6
));
494 memcpy(&sin6
, sa
, sa
->sa_len
);
495 sin6
.sin6_len
= sizeof(struct sockaddr_in6
);
496 sin6
.sin6_family
= AF_INET6
;
498 if (sa
->sa_len
== sizeof(struct sockaddr_in6
) &&
499 (IN6_IS_ADDR_LINKLOCAL(&sin6
.sin6_addr
) ||
500 IN6_IS_ADDR_MC_LINKLOCAL(&sin6
.sin6_addr
)) &&
501 sin6
.sin6_scope_id
== 0) {
503 ntohs(*(u_int16_t
*)&sin6
.sin6_addr
.s6_addr
[2]);
504 sin6
.sin6_addr
.s6_addr
[2] = 0;
505 sin6
.sin6_addr
.s6_addr
[3] = 0;
509 niflags
|= NI_NUMERICHOST
;
510 if (getnameinfo((struct sockaddr
*)&sin6
, sin6
.sin6_len
,
511 line
, sizeof(line
), NULL
, 0, niflags
) != 0)
512 strncpy(line
, "invalid", sizeof(line
));
519 return(link_ntoa((struct sockaddr_dl
*)sa
));
525 * Unknown address family; just render the raw
526 * data in sa->sa_data as hex values.
528 uint8_t *sp
= (uint8_t *)sa
->sa_data
;
529 uint8_t *splim
= (uint8_t *)sa
+ sa
->sa_len
;
530 char *cps
= line
+ sprintf(line
, "af %d:", sa
->sa_family
);
531 char *cpe
= line
+ sizeof(line
);
533 while (sp
< splim
&& cps
< cpe
)
534 cps
+= snprintf(cps
, cpe
- cps
, " %02x", *sp
++);
542 set_metric(char *value
, int key
)
545 u_long noval
, *valp
= &noval
;
548 #define caseof(x, y, z) case x: valp = &rt_metrics.z; flag = y; break
549 caseof(K_MTU
, RTV_MTU
, rmx_mtu
);
550 caseof(K_HOPCOUNT
, RTV_HOPCOUNT
, rmx_hopcount
);
551 caseof(K_EXPIRE
, RTV_EXPIRE
, rmx_expire
);
552 caseof(K_RECVPIPE
, RTV_RPIPE
, rmx_recvpipe
);
553 caseof(K_SENDPIPE
, RTV_SPIPE
, rmx_sendpipe
);
554 caseof(K_SSTHRESH
, RTV_SSTHRESH
, rmx_ssthresh
);
555 caseof(K_RTT
, RTV_RTT
, rmx_rtt
);
556 caseof(K_RTTVAR
, RTV_RTTVAR
, rmx_rttvar
);
557 caseof(K_MSL
, RTV_MSL
, rmx_msl
);
558 caseof(K_IWMAX
, RTV_IWMAXSEGS
, rmx_iwmaxsegs
);
559 caseof(K_IW
, RTV_IWCAPSEGS
, rmx_iwcapsegs
);
562 if (lockrest
|| locking
)
563 rt_metrics
.rmx_locks
|= flag
;
570 newroute(int argc
, char **argv
)
573 const char *err_str
, *dest
= "", *gateway
= "";
574 int ishost
= 0, proxy
= 0, ret
, attempts
, oerrno
, flags
= RTF_STATIC
;
576 struct hostent
*hp
= NULL
;
579 errx(EX_NOPERM
, "must be root to alter routing table");
583 shutdown(s
, SHUT_RD
); /* Don't want to read back our messages */
585 if (**(++argv
)== '-') {
586 switch (key
= keyword(1 + *argv
)) {
589 aflen
= sizeof(struct sockaddr_dl
);
593 aflen
= sizeof(struct sockaddr_in
);
598 aflen
= sizeof(struct sockaddr_in6
);
603 aflen
= sizeof(union sockunion
);
610 flags
&= ~RTF_STATIC
;
628 flags
|= RTF_BLACKHOLE
;
640 flags
|= RTF_CLONING
;
643 flags
|= RTF_XRESOLVE
;
651 getaddr(RTA_IFA
, *++argv
, 0);
656 getaddr(RTA_IFP
, *++argv
, 0);
661 getaddr(RTA_GENMASK
, *++argv
, 0);
666 getaddr(RTA_GATEWAY
, *++argv
, 0);
671 ishost
= getaddr(RTA_DST
, *++argv
, &hp
);
677 getaddr(RTA_NETMASK
, *++argv
, 0);
685 if (prefixlen(*++argv
) == -1) {
695 aflen
= sizeof(struct sockaddr_mpls
);
698 flags
|= RTF_MPLSOPS
;
700 mplsop
= MPLSLOP_POP
;
703 getaddr(RTA_MPLS1
, "", 0);
706 getaddr(RTA_MPLS2
, "", 0);
709 getaddr(RTA_MPLS3
, "", 0);
716 warnx("Push or swap after pop. Ignoring.");
720 mplsop
= MPLSLOP_PUSH
;
724 warnx("Swap after push. Ignoring.");
728 warnx("Too many swaps. Ignoring.");
731 mplsop
= MPLSLOP_SWAP
;
734 flags
|= RTF_MPLSOPS
;
736 aflen
= sizeof(struct sockaddr_mpls
);
739 switch(pushcount
+ swapcount
){
741 getaddr(RTA_MPLS1
, *++argv
, 0);
744 getaddr(RTA_MPLS2
, *++argv
, 0);
747 getaddr(RTA_MPLS3
, *++argv
, 0);
764 set_metric(*++argv
, key
);
770 if ((rtm_addrs
& RTA_DST
) == 0) {
772 ishost
= getaddr(RTA_DST
, *argv
, &hp
);
773 } else if ((rtm_addrs
& RTA_GATEWAY
) == 0) {
775 getaddr(RTA_GATEWAY
, *argv
, &hp
);
777 getaddr(RTA_NETMASK
, *argv
, 0);
785 if (af
== AF_INET6
) {
786 rtm_addrs
&= ~RTA_NETMASK
;
787 memset((void *)&so_mask
, 0, sizeof(so_mask
));
797 flags
|= RTF_GATEWAY
;
799 so_dst
.sinarp
.sin_other
= SIN_PROXY
;
800 flags
|= RTF_ANNOUNCE
;
802 for (attempts
= 1; ; attempts
++) {
804 if ((ret
= rtmsg(*cmd
, flags
)) == 0)
806 if (errno
!= ENETUNREACH
&& errno
!= ESRCH
)
808 if (af
== AF_INET
&& *gateway
!= '\0' &&
809 hp
!= NULL
&& hp
->h_addr_list
[1] != NULL
) {
810 so_gate_tried
= so_gate
.sin
;
812 memmove(&so_gate
.sin
.sin_addr
,
814 MIN((size_t)hp
->h_length
,
815 sizeof(so_gate
.sin
.sin_addr
)));
823 printf("%s %s %s", cmd
, ishost
? "host" : "net", dest
);
824 if (*gateway
!= '\0') {
825 printf(": gateway %s", gateway
);
826 if (attempts
> 1 && ret
== 0 && af
== AF_INET
) {
828 inet_ntoa(so_gate_tried
.sin_addr
));
836 err_str
= "not in table";
839 err_str
= "entry in use";
842 err_str
= "routing table overflow";
844 case EDQUOT
: /* handle recursion avoidance in
846 err_str
= "gateway uses the same route";
849 err_str
= strerror(oerrno
);
852 printf(": %s\n", err_str
);
859 inet_makenetandmask(u_long net
, struct sockaddr_in
*in
, u_long bits
)
861 u_long addr
, mask
= 0;
864 rtm_addrs
|= RTA_NETMASK
;
866 * XXX: This approach unable to handle 0.0.0.1/32 correctly
867 * as inet_network() converts 0.0.0.1 and 1 equally.
870 addr
= net
<< IN_CLASSA_NSHIFT
;
871 else if (net
<= 0xffff)
872 addr
= net
<< IN_CLASSB_NSHIFT
;
873 else if (net
<= 0xffffff)
874 addr
= net
<< IN_CLASSC_NSHIFT
;
879 mask
= 0xffffffff << (32 - bits
);
882 else if (IN_CLASSA(addr
))
883 mask
= IN_CLASSA_NET
;
884 else if (IN_CLASSB(addr
))
885 mask
= IN_CLASSB_NET
;
886 else if (IN_CLASSC(addr
))
887 mask
= IN_CLASSC_NET
;
888 else if (IN_MULTICAST(addr
))
889 mask
= IN_CLASSD_NET
;
893 in
->sin_addr
.s_addr
= htonl(addr
);
895 in
->sin_addr
.s_addr
= htonl(mask
);
898 cp
= (char *)(&in
->sin_addr
+ 1);
899 while (*--cp
== 0 && cp
> (char *)in
)
901 in
->sin_len
= 1 + cp
- (char *)in
;
906 * XXX the function may need more improvement...
909 inet6_makenetandmask(struct sockaddr_in6
*sin6
, const char *plen
)
914 if (IN6_IS_ADDR_UNSPECIFIED(&sin6
->sin6_addr
) &&
915 sin6
->sin6_scope_id
== 0) {
917 } else if ((sin6
->sin6_addr
.s6_addr
[0] & 0xe0) == 0x20) {
918 /* aggregatable global unicast - RFC2374 */
919 memset(&in6
, 0, sizeof(in6
));
920 if (memcmp(&sin6
->sin6_addr
.s6_addr
[8],
921 &in6
.s6_addr
[8], 8) == 0)
926 if (plen
== NULL
|| strcmp(plen
, "128") == 0)
928 rtm_addrs
|= RTA_NETMASK
;
935 * Interpret an argument as a network address of some kind,
936 * returning 1 if a host address, 0 if a network address.
939 getaddr(int which
, const char *str
, struct hostent
**hpp
)
946 int afamily
; /* local copy of af so we can change it */
950 aflen
= sizeof(struct sockaddr_in
);
961 struct ifaddrs
*ifap
, *ifa
;
962 struct sockaddr_dl
*sdl
= NULL
;
964 if (getifaddrs(&ifap
))
965 err(1, "getifaddrs");
967 for (ifa
= ifap
; ifa
!= NULL
; ifa
= ifa
->ifa_next
) {
968 if (ifa
->ifa_addr
->sa_family
!= AF_LINK
)
971 if (strcmp(str
, ifa
->ifa_name
) != 0)
974 sdl
= (struct sockaddr_dl
*)ifa
->ifa_addr
;
976 /* If we found it, then use it */
979 * Copy is safe since we have a
980 * sockaddr_storage member in sockunion{}.
981 * Note that we need to copy before calling
984 * Interface routes are routed only by their
985 * sdl_index or interface name (we provide
986 * both here), not by the ARP address of the
987 * local interface (alen, slen = 0).
989 memcpy(&su
->sdl
, sdl
, sdl
->sdl_len
);
990 su
->sdl
.sdl_alen
= 0;
991 su
->sdl
.sdl_slen
= 0;
1021 usage("internal error");
1024 su
->sa
.sa_len
= aflen
;
1025 su
->sa
.sa_family
= afamily
; /* cases that don't want it have left
1027 if (strcmp(str
, "default") == 0) {
1029 * Default is net 0.0.0.0/0
1034 /* bzero(su, sizeof(*su)); *//* for readability */
1035 getaddr(RTA_NETMASK
, str
, 0);
1039 /* bzero(su, sizeof(*su)); *//* for readability */
1048 struct addrinfo hints
, *res
;
1051 if (which
== RTA_DST
&& (q
= strchr(str
, '/')) != NULL
)
1053 memset(&hints
, 0, sizeof(hints
));
1054 hints
.ai_family
= afamily
; /*AF_INET6*/
1055 hints
.ai_flags
= AI_NUMERICHOST
;
1056 hints
.ai_socktype
= SOCK_DGRAM
; /*dummy*/
1057 if (getaddrinfo(str
, "0", &hints
, &res
) != 0 ||
1058 res
->ai_family
!= AF_INET6
||
1059 res
->ai_addrlen
!= sizeof(su
->sin6
)) {
1060 fprintf(stderr
, "%s: bad value\n", str
);
1063 memcpy(&su
->sin6
, res
->ai_addr
, sizeof(su
->sin6
));
1065 if ((IN6_IS_ADDR_LINKLOCAL(&su
->sin6
.sin6_addr
) ||
1066 IN6_IS_ADDR_LINKLOCAL(&su
->sin6
.sin6_addr
)) &&
1067 su
->sin6
.sin6_scope_id
) {
1068 *(u_int16_t
*)&su
->sin6
.sin6_addr
.s6_addr
[2] =
1069 htons(su
->sin6
.sin6_scope_id
);
1070 su
->sin6
.sin6_scope_id
= 0;
1076 if (which
== RTA_DST
)
1077 return(inet6_makenetandmask(&su
->sin6
, q
));
1083 link_addr(str
, &su
->sdl
);
1087 su
->sa
.sa_len
= sizeof(*su
);
1088 sockaddr(str
, &su
->sa
);
1095 bzero(su
, sizeof(*su
));
1096 su
->sa
.sa_len
= sizeof(*su
);
1097 su
->sa
.sa_family
= AF_MPLS
;
1098 su
->smpls
.smpls_op
= mplsop
;
1099 if (mplsop
!= MPLSLOP_POP
&& mplsop
!= MPLSLOP_POPALL
&&
1101 if (sscanf(str
, "%u", &label
) != 1)
1102 errx(EX_NOHOST
, "bad address: %s", str
);
1103 su
->smpls
.smpls_label
= htonl(label
);
1117 q
= strchr(str
,'/');
1118 if (q
!= NULL
&& which
== RTA_DST
) {
1120 if ((val
= inet_network(str
)) != INADDR_NONE
) {
1121 inet_makenetandmask(
1122 val
, &su
->sin
, strtoul(q
+1, 0, 0));
1127 if ((which
!= RTA_DST
|| forcenet
== 0) &&
1128 inet_aton(str
, &su
->sin
.sin_addr
)) {
1129 val
= su
->sin
.sin_addr
.s_addr
;
1130 if (which
!= RTA_DST
||
1131 inet_lnaof(su
->sin
.sin_addr
) != INADDR_ANY
)
1138 if (which
== RTA_DST
&& forcehost
== 0 &&
1139 ((val
= inet_network(str
)) != INADDR_NONE
||
1140 ((np
= getnetbyname(str
)) != NULL
&& (val
= np
->n_net
) != 0))) {
1142 inet_makenetandmask(val
, &su
->sin
, 0);
1145 hp
= gethostbyname(str
);
1148 su
->sin
.sin_family
= hp
->h_addrtype
;
1149 memmove((char *)&su
->sin
.sin_addr
, hp
->h_addr
,
1150 MIN((size_t)hp
->h_length
, sizeof(su
->sin
.sin_addr
)));
1153 errx(EX_NOHOST
, "bad address: %s", str
);
1157 prefixlen(const char *len_str
)
1159 int len
= atoi(len_str
), q
, r
;
1163 rtm_addrs
|= RTA_NETMASK
;
1168 p
= (char *)&so_mask
.sin6
.sin6_addr
;
1173 p
= (char *)&so_mask
.sin
.sin_addr
;
1176 fprintf(stderr
, "prefixlen not supported in this af\n");
1181 if (len
< 0 || max
< len
) {
1182 fprintf(stderr
, "%s: bad value\n", len_str
);
1188 so_mask
.sa
.sa_family
= af
;
1189 so_mask
.sa
.sa_len
= aflen
;
1190 memset((void *)p
, 0, max
/ 8);
1192 memset((void *)p
, 0xff, q
);
1194 *((u_char
*)p
+ q
) = (0xff00 >> r
) & 0xff;
1206 char *buf
, *lim
, *next
;
1207 struct rt_msghdr
*rtm
;
1211 mib
[2] = 0; /* protocol */
1212 mib
[3] = 0; /* wildcard address family */
1213 mib
[4] = NET_RT_IFLIST
;
1214 mib
[5] = 0; /* no flags */
1215 if (sysctl(mib
, 6, NULL
, &needed
, NULL
, 0) < 0)
1216 err(EX_OSERR
, "route-sysctl-estimate");
1217 if ((buf
= malloc(needed
)) == NULL
)
1218 errx(EX_OSERR
, "malloc failed");
1219 if (sysctl(mib
, 6, buf
, &needed
, NULL
, 0) < 0)
1220 err(EX_OSERR
, "actual retrieval of interface table");
1222 for (next
= buf
; next
< lim
; next
+= rtm
->rtm_msglen
) {
1223 rtm
= (struct rt_msghdr
*)next
;
1224 print_rtmsg(rtm
, rtm
->rtm_msglen
);
1241 n
= read(s
, msg
, 2048);
1243 printf("\ngot message of size %d on %s", n
, ctime(&now
));
1244 print_rtmsg((struct rt_msghdr
*)msg
, n
);
1249 struct rt_msghdr m_rtm
;
1254 rtmsg(int cmd
, int flags
)
1258 char *cp
= m_rtmsg
.m_space
;
1261 #define NEXTADDR(w, u) \
1262 if (rtm_addrs & (w)) {\
1263 l = RT_ROUNDUP(u.sa.sa_len); memmove(cp, &(u), l); cp += l;\
1264 if (verbose) sodump(&(u),"u");\
1268 memset(&m_rtmsg
, 0, sizeof(m_rtmsg
));
1271 else if (cmd
== 'c')
1273 else if (cmd
== 'g') {
1275 if (so_ifp
.sa
.sa_family
== 0) {
1276 so_ifp
.sa
.sa_family
= AF_LINK
;
1277 so_ifp
.sa
.sa_len
= sizeof(struct sockaddr_dl
);
1278 rtm_addrs
|= RTA_IFP
;
1282 #define rtm m_rtmsg.m_rtm
1284 rtm
.rtm_flags
= flags
;
1285 rtm
.rtm_version
= RTM_VERSION
;
1286 rtm
.rtm_seq
= ++seq
;
1287 rtm
.rtm_addrs
= rtm_addrs
;
1288 rtm
.rtm_rmx
= rt_metrics
;
1289 rtm
.rtm_inits
= rtm_inits
;
1291 if (rtm_addrs
& RTA_NETMASK
)
1293 NEXTADDR(RTA_DST
, so_dst
);
1294 NEXTADDR(RTA_GATEWAY
, so_gate
);
1295 NEXTADDR(RTA_NETMASK
, so_mask
);
1296 NEXTADDR(RTA_GENMASK
, so_genmask
);
1297 NEXTADDR(RTA_IFP
, so_ifp
);
1298 NEXTADDR(RTA_IFA
, so_ifa
);
1299 NEXTADDR(RTA_MPLS1
, so_mpls1
);
1300 NEXTADDR(RTA_MPLS2
, so_mpls2
);
1301 NEXTADDR(RTA_MPLS3
, so_mpls3
);
1302 rtm
.rtm_msglen
= l
= cp
- (char *)&m_rtmsg
;
1304 print_rtmsg(&rtm
, l
);
1307 if ((rlen
= write(s
, (char *)&m_rtmsg
, l
)) < 0) {
1308 warn("writing to routing socket");
1311 if (cmd
== RTM_GET
) {
1313 l
= read(s
, (char *)&m_rtmsg
, sizeof(m_rtmsg
));
1314 } while (l
> 0 && (rtm
.rtm_seq
!= seq
|| rtm
.rtm_pid
!= pid
));
1316 warn("read from routing socket");
1318 print_getmsg(&rtm
, l
);
1327 int olen
= so_mask
.sa
.sa_len
;
1328 char *cp1
= olen
+ (char *)&so_mask
, *cp2
;
1330 for (so_mask
.sa
.sa_len
= 0; cp1
> (char *)&so_mask
; )
1332 so_mask
.sa
.sa_len
= 1 + cp1
- (char *)&so_mask
;
1335 if ((rtm_addrs
& RTA_DST
) == 0)
1337 switch (so_dst
.sa
.sa_family
) {
1345 cp1
= so_mask
.sa
.sa_len
+ 1 + (char *)&so_dst
;
1346 cp2
= so_dst
.sa
.sa_len
+ 1 + (char *)&so_dst
;
1349 cp2
= so_mask
.sa
.sa_len
+ 1 + (char *)&so_mask
;
1350 while (cp1
> so_dst
.sa
.sa_data
)
1354 const char *msgtypes
[] = {
1356 "RTM_ADD: Add Route",
1357 "RTM_DELETE: Delete Route",
1358 "RTM_CHANGE: Change Metrics or flags",
1359 "RTM_GET: Report Metrics",
1360 "RTM_LOSING: Kernel Suspects Partitioning",
1361 "RTM_REDIRECT: Told to use different route",
1362 "RTM_MISS: Lookup failed on this address",
1363 "RTM_LOCK: fix specified metrics",
1366 "RTM_RESOLVE: Route created by cloning",
1367 "RTM_NEWADDR: address being added to iface",
1368 "RTM_DELADDR: address being removed from iface",
1369 "RTM_IFINFO: iface status change",
1370 "RTM_NEWMADDR: new multicast group membership on iface",
1371 "RTM_DELMADDR: multicast group membership removed from iface",
1372 "RTM_IFANNOUNCE: interface arrival/departure",
1376 char metricnames
[] =
1377 "\013iw\012iwmax\011msl\010rttvar\7rtt\6ssthresh\5sendpipe\4recvpipe\3expire"
1380 "\1UP\2GATEWAY\3HOST\4REJECT\5DYNAMIC\6MODIFIED\7DONE\010MASK_PRESENT"
1381 "\011CLONING\012XRESOLVE\013LLINFO\014STATIC\015BLACKHOLE\016b016"
1382 "\017PROTO2\020PROTO1\021PRCLONING\022WASCLONED\023PROTO3\024CHAINDELETE"
1383 "\025PINNED\026LOCAL\027BROADCAST\030MULTICAST";
1385 "\1UP\2BROADCAST\3DEBUG\4LOOPBACK\5PTP\6b6\7RUNNING\010NOARP"
1386 "\011PPROMISC\012ALLMULTI\013OACTIVE\014SIMPLEX\015LINK0\016LINK1"
1387 "\017LINK2\020MULTICAST";
1389 "\1DST\2GATEWAY\3NETMASK\4GENMASK\5IFP\6IFA\7AUTHOR\010BRD"
1390 "\011MPLS1\012MPLS2\013MPLS3";
1393 print_rtmsg(struct rt_msghdr
*rtm
, int msglen __unused
)
1395 struct if_msghdr
*ifm
;
1396 struct ifa_msghdr
*ifam
;
1398 struct ifma_msghdr
*ifmam
;
1400 struct if_announcemsghdr
*ifan
;
1404 if (rtm
->rtm_version
!= RTM_VERSION
) {
1405 printf("routing message version %d not understood\n",
1409 if (msgtypes
[rtm
->rtm_type
] != NULL
)
1410 printf("%s: ", msgtypes
[rtm
->rtm_type
]);
1412 printf("#%d: ", rtm
->rtm_type
);
1413 printf("len %d, ", rtm
->rtm_msglen
);
1414 switch (rtm
->rtm_type
) {
1416 ifm
= (struct if_msghdr
*)rtm
;
1417 printf("if# %d, flags:", ifm
->ifm_index
);
1418 bprintf(stdout
, ifm
->ifm_flags
, ifnetflags
);
1419 pmsg_addrs((char *)(ifm
+ 1), ifm
->ifm_addrs
);
1423 ifam
= (struct ifa_msghdr
*)rtm
;
1424 printf("metric %d, flags:", ifam
->ifam_metric
);
1425 bprintf(stdout
, ifam
->ifam_flags
, routeflags
);
1426 pmsg_addrs((char *)(ifam
+ 1), ifam
->ifam_addrs
);
1431 ifmam
= (struct ifma_msghdr
*)rtm
;
1432 pmsg_addrs((char *)(ifmam
+ 1), ifmam
->ifmam_addrs
);
1435 case RTM_IFANNOUNCE
:
1436 ifan
= (struct if_announcemsghdr
*)rtm
;
1437 printf("if# %d, what: ", ifan
->ifan_index
);
1438 switch (ifan
->ifan_what
) {
1442 case IFAN_DEPARTURE
:
1443 printf("departure");
1446 printf("#%d", ifan
->ifan_what
);
1453 printf("pid: %ld, seq %d, errno %d, flags:",
1454 (long)rtm
->rtm_pid
, rtm
->rtm_seq
, rtm
->rtm_errno
);
1455 bprintf(stdout
, rtm
->rtm_flags
, routeflags
);
1461 print_getmsg(struct rt_msghdr
*rtm
, int msglen
)
1463 struct sockaddr
*dst
= NULL
, *gate
= NULL
, *mask
= NULL
;
1464 struct sockaddr_dl
*ifp
= NULL
;
1465 struct sockaddr
*sa
;
1469 printf(" route to: %s\n", routename(&so_dst
.sa
));
1470 if (rtm
->rtm_version
!= RTM_VERSION
) {
1471 warnx("routing message version %d not understood",
1475 if (rtm
->rtm_msglen
> msglen
) {
1476 warnx("message length mismatch, in packet %d, returned %d",
1477 rtm
->rtm_msglen
, msglen
);
1479 if (rtm
->rtm_errno
!= 0) {
1480 errno
= rtm
->rtm_errno
;
1481 warn("message indicates error %d", errno
);
1484 cp
= ((char *)(rtm
+ 1));
1486 for (i
= 1; i
!= 0; i
<<= 1)
1487 if (i
& rtm
->rtm_addrs
) {
1488 sa
= (struct sockaddr
*)cp
;
1500 if (sa
->sa_family
== AF_LINK
&&
1501 ((struct sockaddr_dl
*)sa
)->sdl_nlen
)
1502 ifp
= (struct sockaddr_dl
*)sa
;
1507 if (dst
!= NULL
&& mask
!= NULL
)
1508 mask
->sa_family
= dst
->sa_family
; /* XXX */
1510 printf("destination: %s\n", routename(dst
));
1512 int savenflag
= nflag
;
1515 printf(" mask: %s\n", routename(mask
));
1518 if (gate
!= NULL
&& rtm
->rtm_flags
& RTF_GATEWAY
)
1519 printf(" gateway: %s\n", routename(gate
));
1521 printf(" interface: %.*s\n",
1522 ifp
->sdl_nlen
, ifp
->sdl_data
);
1524 bprintf(stdout
, rtm
->rtm_flags
, routeflags
);
1526 #define lock(f) ((rtm->rtm_rmx.rmx_locks & __CONCAT(RTV_,f)) ? 'L' : ' ')
1527 #define msec(u) (((u) + 500) / 1000) /* usec to msec */
1530 recvpipe sendpipe ssthresh rtt,msec rttvar hopcount mtu expire msl,msec iwmax iw");
1531 printf("%8ld%c ", rtm
->rtm_rmx
.rmx_recvpipe
, lock(RPIPE
));
1532 printf("%8ld%c ", rtm
->rtm_rmx
.rmx_sendpipe
, lock(SPIPE
));
1533 printf("%8ld%c ", rtm
->rtm_rmx
.rmx_ssthresh
, lock(SSTHRESH
));
1534 printf("%8ld%c ", msec(rtm
->rtm_rmx
.rmx_rtt
), lock(RTT
));
1535 printf("%8ld%c ", msec(rtm
->rtm_rmx
.rmx_rttvar
), lock(RTTVAR
));
1536 printf("%8ld%c ", rtm
->rtm_rmx
.rmx_hopcount
, lock(HOPCOUNT
));
1537 printf("%8ld%c ", rtm
->rtm_rmx
.rmx_mtu
, lock(MTU
));
1538 if (rtm
->rtm_rmx
.rmx_expire
!= 0) {
1540 clock_gettime(CLOCK_MONOTONIC
, &sp
);
1541 rtm
->rtm_rmx
.rmx_expire
-= sp
.tv_sec
;
1543 printf("%8ld%c ", rtm
->rtm_rmx
.rmx_expire
, lock(EXPIRE
));
1544 printf("%8ld ", rtm
->rtm_rmx
.rmx_msl
);
1545 printf("%5ld ", rtm
->rtm_rmx
.rmx_iwmaxsegs
);
1546 printf("%3ld\n", rtm
->rtm_rmx
.rmx_iwcapsegs
);
1549 #define RTA_IGN (RTA_DST|RTA_GATEWAY|RTA_NETMASK|RTA_IFP|RTA_IFA|RTA_BRD)
1552 else if (rtm
->rtm_addrs
&~ RTA_IGN
) {
1553 printf("sockaddrs: ");
1554 bprintf(stdout
, rtm
->rtm_addrs
, addrnames
);
1561 pmsg_common(struct rt_msghdr
*rtm
)
1563 printf("\nlocks: ");
1564 bprintf(stdout
, rtm
->rtm_rmx
.rmx_locks
, metricnames
);
1566 bprintf(stdout
, rtm
->rtm_inits
, metricnames
);
1567 pmsg_addrs(((char *)(rtm
+ 1)), rtm
->rtm_addrs
);
1571 pmsg_addrs(char *cp
, int addrs
)
1573 struct sockaddr
*sa
;
1580 printf("\nsockaddrs: ");
1581 bprintf(stdout
, addrs
, addrnames
);
1583 for (i
= 1; i
!= 0; i
<<= 1)
1585 sa
= (struct sockaddr
*)cp
;
1586 printf(" %s", routename(sa
));
1594 bprintf(FILE *fp
, int b
, u_char
*str
)
1601 while ((i
= *str
++) != 0) {
1602 if (b
& (1 << (i
-1))) {
1609 for (; (i
= *str
) > 32; str
++)
1620 keyword(const char *cp
)
1622 struct keytab
*kt
= keywords
;
1624 while (kt
->kt_cp
!= NULL
&& strcmp(kt
->kt_cp
, cp
) != 0)
1630 sodump(sup su
, const char *which
)
1632 switch (su
->sa
.sa_family
) {
1634 printf("%s: link %s; ",
1635 which
, link_ntoa(&su
->sdl
));
1638 printf("%s: inet %s; ",
1639 which
, inet_ntoa(su
->sin
.sin_addr
));
1655 sockaddr(const char *addr
, struct sockaddr
*sa
)
1657 char *cp
= (char *)sa
;
1658 int size
= sa
->sa_len
;
1659 char *cplim
= cp
+ size
;
1660 int byte
= 0, state
= VIRGIN
, new = 0 /* foil gcc */;
1662 memset(cp
, 0, size
);
1665 if ((*addr
>= '0') && (*addr
<= '9')) {
1667 } else if ((*addr
>= 'a') && (*addr
<= 'f')) {
1668 new = *addr
- 'a' + 10;
1669 } else if ((*addr
>= 'A') && (*addr
<= 'F')) {
1670 new = *addr
- 'A' + 10;
1671 } else if (*addr
== '\0')
1676 switch (state
/* | INPUT */) {
1677 case GOTTWO
| DIGIT
:
1678 *cp
++ = byte
; /*FALLTHROUGH*/
1679 case VIRGIN
| DIGIT
:
1680 state
= GOTONE
; byte
= new; continue;
1681 case GOTONE
| DIGIT
:
1682 state
= GOTTWO
; byte
= new + (byte
<< 4); continue;
1683 default: /* | DELIM */
1684 state
= VIRGIN
; *cp
++ = byte
; byte
= 0; continue;
1687 *cp
++ = byte
; /* FALLTHROUGH */
1692 } while (cp
< cplim
);
1693 sa
->sa_len
= cp
- (char *)sa
;