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. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * @(#) Copyright (c) 1983, 1989, 1991, 1993 The Regents of the University of California. All rights reserved.
34 * @(#)route.c 8.6 (Berkeley) 4/28/95
35 * $FreeBSD: src/sbin/route/route.c,v 1.40.2.11 2003/02/27 23:10:10 ru Exp $
36 * $DragonFly: src/sbin/route/route.c,v 1.17 2008/07/07 22:02:09 nant Exp $
39 #include <sys/param.h>
41 #include <sys/socket.h>
42 #include <sys/ioctl.h>
43 #include <sys/sysctl.h>
44 #include <sys/types.h>
47 #include <net/route.h>
48 #include <net/if_dl.h>
49 #include <netinet/in.h>
50 #include <netinet/if_ether.h>
51 #include <netatalk/at.h>
55 #include <arpa/inet.h>
58 #include <netproto/mpls/mpls.h>
76 struct sockaddr_in sin
;
78 struct sockaddr_in6 sin6
;
80 struct sockaddr_at sat
;
82 struct sockaddr_ns sns
;
84 struct sockaddr_mpls smpls
;
85 struct sockaddr_dl sdl
;
86 struct sockaddr_inarp sinarp
;
87 struct sockaddr_storage ss
; /* added to avoid memory overrun */
88 } so_dst
, so_gate
, so_mask
, so_genmask
, so_ifa
, so_ifp
, so_mpls1
,
91 typedef union sockunion
*sup
;
96 static struct ortentry route
;
97 static struct rt_metrics rt_metrics
;
98 static int pid
, rtm_addrs
;
100 static int forcehost
, forcenet
, af
, qflag
, tflag
;
101 static int iflag
, verbose
, aflen
= sizeof(struct sockaddr_in
);
102 static int locking
, lockrest
, debugonly
;
103 static int mplsop
, popcount
, pushcount
, swapcount
;
104 static u_long rtm_inits
;
107 static short ns_bh
[] = {-1,-1,-1};
110 static int atalk_aton(const char *, struct at_addr
*);
111 static char *atalk_ntoa(struct at_addr
);
112 static void flushroutes(int, char **);
113 static void set_metric(char *, int);
114 static void newroute(int, char **);
115 static void inet_makenetandmask(u_long
, struct sockaddr_in
*, u_long
);
116 static void interfaces(void);
117 static void monitor(void);
118 static void sockaddr(char *, struct sockaddr
*);
119 static void sodump(sup
, const char *);
120 static void bprintf(FILE *, int, u_char
*);
121 static void print_getmsg(struct rt_msghdr
*, int);
122 static void print_rtmsg(struct rt_msghdr
*, int);
123 static void pmsg_common(struct rt_msghdr
*);
124 static void pmsg_addrs(char *, int);
125 static void mask_addr(void);
126 static int getaddr(int, char *, struct hostent
**);
127 static int rtmsg(int, int);
128 static int prefixlen(const char *);
130 static int inet6_makenetandmask(struct sockaddr_in6
*, const char *);
134 usage(const char *cp
)
137 warnx("bad keyword: %s", cp
);
138 fprintf(stderr
, "usage: route [-dnqtv] command [[modifiers] args]\n");
144 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
145 #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
148 main(int argc
, char **argv
)
155 while ((ch
= getopt(argc
, argv
, "c:wnqdtv")) != -1)
158 cpuflag
= strtol(optarg
, NULL
, 0);
188 s
= open(_PATH_DEVNULL
, O_WRONLY
, 0);
190 s
= socket(PF_ROUTE
, SOCK_RAW
, 0);
192 err(EX_OSERR
, "socket");
194 switch (keyword(*argv
)) {
202 newroute(argc
, argv
);
214 flushroutes(argc
, argv
);
223 * Purge all entries in the routing tables not
224 * associated with network interfaces.
227 flushroutes(int argc
, char **argv
)
230 int mib
[7], rlen
, seqno
;
232 char *buf
, *next
, *lim
;
233 struct rt_msghdr
*rtm
;
236 errx(EX_NOPERM
, "must be root to alter routing table");
238 shutdown(s
, SHUT_RD
); /* Don't want to read back our messages */
241 if (argc
== 2 && **argv
== '-')
242 switch (keyword(*argv
+ 1)) {
272 mib
[2] = 0; /* protocol */
273 mib
[3] = 0; /* wildcard address family */
274 mib
[4] = NET_RT_DUMP
;
275 mib
[5] = 0; /* no flags */
282 if (sysctl(mib
, miblen
, NULL
, &needed
, NULL
, 0) < 0)
283 err(EX_OSERR
, "route-sysctl-estimate");
284 if ((buf
= malloc(needed
)) == NULL
)
285 errx(EX_OSERR
, "malloc failed");
286 if (sysctl(mib
, miblen
, buf
, &needed
, NULL
, 0) < 0)
287 err(EX_OSERR
, "route-sysctl-get");
290 printf("Examining routing table from sysctl\n");
292 for (next
= buf
; next
< lim
; next
+= rtm
->rtm_msglen
) {
293 rtm
= (struct rt_msghdr
*)next
;
295 print_rtmsg(rtm
, rtm
->rtm_msglen
);
296 if ((rtm
->rtm_flags
& RTF_GATEWAY
) == 0)
299 struct sockaddr
*sa
= (struct sockaddr
*)(rtm
+ 1);
301 if (sa
->sa_family
!= af
)
306 rtm
->rtm_type
= RTM_DELETE
;
307 rtm
->rtm_seq
= seqno
;
308 rlen
= write(s
, next
, rtm
->rtm_msglen
);
309 if (rlen
< (int)rtm
->rtm_msglen
) {
310 warn("write to routing socket");
311 printf("got only %d for rlen\n", rlen
);
318 print_rtmsg(rtm
, rlen
);
320 struct sockaddr
*sa
= (struct sockaddr
*)(rtm
+ 1);
322 printf("%-20s ", rtm
->rtm_flags
& RTF_HOST
?
323 routename(sa
) : netname(sa
));
325 printf("%-20.20s ", rtm
->rtm_flags
& RTF_HOST
?
326 routename(sa
) : netname(sa
));
328 sa
= (struct sockaddr
*)(ROUNDUP(sa
->sa_len
) +
331 printf("%-20s ", routename(sa
));
333 printf("%-20.20s ", routename(sa
));
341 routename(struct sockaddr
*sa
)
344 static char line
[MAXHOSTNAMELEN
+ 1];
346 static char domain
[MAXHOSTNAMELEN
+ 1];
347 static int first
= 1;
351 if (gethostname(domain
, MAXHOSTNAMELEN
) == 0 &&
352 (cp
= strchr(domain
, '.'))) {
353 domain
[MAXHOSTNAMELEN
] = '\0';
354 strcpy(domain
, cp
+ 1);
360 strcpy(line
, "default");
361 else switch (sa
->sa_family
) {
365 in
= ((struct sockaddr_in
*)sa
)->sin_addr
;
368 if (in
.s_addr
== INADDR_ANY
|| sa
->sa_len
< 4)
370 if (cp
== NULL
&& !nflag
) {
371 hp
= gethostbyaddr(&in
, sizeof(struct in_addr
),
375 cptr
= strchr(hp
->h_name
, '.');
376 if (cptr
!= NULL
&& !wflag
&&
377 strcmp(cptr
+ 1, domain
) == 0)
383 strncpy(line
, cp
, sizeof(line
) - 1);
384 line
[sizeof(line
) - 1] = '\0';
386 sprintf(line
, "%s", inet_ntoa(in
));
393 struct sockaddr_in6 sin6
; /* use static var for safety */
395 #ifdef NI_WITHSCOPEID
396 niflags
= NI_WITHSCOPEID
;
399 memset(&sin6
, 0, sizeof(sin6
));
400 memcpy(&sin6
, sa
, sa
->sa_len
);
401 sin6
.sin6_len
= sizeof(struct sockaddr_in6
);
402 sin6
.sin6_family
= AF_INET6
;
404 if (sa
->sa_len
== sizeof(struct sockaddr_in6
) &&
405 (IN6_IS_ADDR_LINKLOCAL(&sin6
.sin6_addr
) ||
406 IN6_IS_ADDR_MC_LINKLOCAL(&sin6
.sin6_addr
)) &&
407 sin6
.sin6_scope_id
== 0) {
409 ntohs(*(u_int16_t
*)&sin6
.sin6_addr
.s6_addr
[2]);
410 sin6
.sin6_addr
.s6_addr
[2] = 0;
411 sin6
.sin6_addr
.s6_addr
[3] = 0;
415 niflags
|= NI_NUMERICHOST
;
416 if (getnameinfo((struct sockaddr
*)&sin6
, sin6
.sin6_len
,
417 line
, sizeof(line
), NULL
, 0, niflags
) != 0)
418 strncpy(line
, "invalid", sizeof(line
));
425 snprintf(line
, sizeof(line
), "atalk %s",
426 atalk_ntoa(((struct sockaddr_at
*)sa
)->sat_addr
));
431 return(ns_print((struct sockaddr_ns
*)sa
));
435 return(link_ntoa((struct sockaddr_dl
*)sa
));
440 * Unknown address family; just render the raw
441 * data in sa->sa_data as hex values.
443 uint8_t *sp
= (uint8_t *)sa
->sa_data
;
444 uint8_t *splim
= (uint8_t *)sa
+ sa
->sa_len
;
445 char *cps
= line
+ sprintf(line
, "(%d)", sa
->sa_family
);
446 char *cpe
= line
+ sizeof(line
);
448 while (sp
< splim
&& cps
< cpe
)
449 cps
+= snprintf(cps
, cpe
- cps
, " %02x", *sp
++);
457 * Return the name of the network whose address is given.
458 * The address is assumed to be that of a net or subnet, not a host.
461 netname(struct sockaddr
*sa
)
464 static char line
[MAXHOSTNAMELEN
+ 1];
465 struct netent
*np
= 0;
470 switch (sa
->sa_family
) {
474 in
= ((struct sockaddr_in
*)sa
)->sin_addr
;
476 i
= in
.s_addr
= ntohl(in
.s_addr
);
481 mask
= IN_CLASSA_NET
;
483 } else if (IN_CLASSB(i
)) {
484 mask
= IN_CLASSB_NET
;
487 mask
= IN_CLASSC_NET
;
491 * If there are more bits than the standard mask
492 * would suggest, subnets must be in use.
493 * Guess at the subnet mask, assuming reasonable
494 * width subnet fields.
496 while (in
.s_addr
&~ mask
)
497 mask
= (long)mask
>> subnetshift
;
498 net
= in
.s_addr
& mask
;
499 while ((mask
& 1) == 0)
500 mask
>>= 1, net
>>= 1;
501 np
= getnetbyaddr(net
, AF_INET
);
505 #define C(x) (unsigned)((x) & 0xff)
507 strncpy(line
, cp
, sizeof(line
));
508 else if ((in
.s_addr
& 0xffffff) == 0)
509 sprintf(line
, "%u", C(in
.s_addr
>> 24));
510 else if ((in
.s_addr
& 0xffff) == 0)
511 sprintf(line
, "%u.%u", C(in
.s_addr
>> 24),
513 else if ((in
.s_addr
& 0xff) == 0)
514 sprintf(line
, "%u.%u.%u", C(in
.s_addr
>> 24),
515 C(in
.s_addr
>> 16), C(in
.s_addr
>> 8));
517 sprintf(line
, "%u.%u.%u.%u", C(in
.s_addr
>> 24),
518 C(in
.s_addr
>> 16), C(in
.s_addr
>> 8),
527 struct sockaddr_in6 sin6
; /* use static var for safety */
529 #ifdef NI_WITHSCOPEID
530 niflags
= NI_WITHSCOPEID
;
533 memset(&sin6
, 0, sizeof(sin6
));
534 memcpy(&sin6
, sa
, sa
->sa_len
);
535 sin6
.sin6_len
= sizeof(struct sockaddr_in6
);
536 sin6
.sin6_family
= AF_INET6
;
538 if (sa
->sa_len
== sizeof(struct sockaddr_in6
) &&
539 (IN6_IS_ADDR_LINKLOCAL(&sin6
.sin6_addr
) ||
540 IN6_IS_ADDR_MC_LINKLOCAL(&sin6
.sin6_addr
)) &&
541 sin6
.sin6_scope_id
== 0) {
543 ntohs(*(u_int16_t
*)&sin6
.sin6_addr
.s6_addr
[2]);
544 sin6
.sin6_addr
.s6_addr
[2] = 0;
545 sin6
.sin6_addr
.s6_addr
[3] = 0;
549 niflags
|= NI_NUMERICHOST
;
550 if (getnameinfo((struct sockaddr
*)&sin6
, sin6
.sin6_len
,
551 line
, sizeof(line
), NULL
, 0, niflags
) != 0)
552 strncpy(line
, "invalid", sizeof(line
));
559 snprintf(line
, sizeof(line
), "atalk %s",
560 atalk_ntoa(((struct sockaddr_at
*)sa
)->sat_addr
));
565 return(ns_print((struct sockaddr_ns
*)sa
));
570 return(link_ntoa((struct sockaddr_dl
*)sa
));
576 * Unknown address family; just render the raw
577 * data in sa->sa_data as hex values.
579 uint8_t *sp
= (uint8_t *)sa
->sa_data
;
580 uint8_t *splim
= (uint8_t *)sa
+ sa
->sa_len
;
581 char *cps
= line
+ sprintf(line
, "af %d:", sa
->sa_family
);
582 char *cpe
= line
+ sizeof(line
);
584 while (sp
< splim
&& cps
< cpe
)
585 cps
+= snprintf(cps
, cpe
- cps
, " %02x", *sp
++);
593 set_metric(char *value
, int key
)
596 u_long noval
, *valp
= &noval
;
599 #define caseof(x, y, z) case x: valp = &rt_metrics.z; flag = y; break
600 caseof(K_MTU
, RTV_MTU
, rmx_mtu
);
601 caseof(K_HOPCOUNT
, RTV_HOPCOUNT
, rmx_hopcount
);
602 caseof(K_EXPIRE
, RTV_EXPIRE
, rmx_expire
);
603 caseof(K_RECVPIPE
, RTV_RPIPE
, rmx_recvpipe
);
604 caseof(K_SENDPIPE
, RTV_SPIPE
, rmx_sendpipe
);
605 caseof(K_SSTHRESH
, RTV_SSTHRESH
, rmx_ssthresh
);
606 caseof(K_RTT
, RTV_RTT
, rmx_rtt
);
607 caseof(K_RTTVAR
, RTV_RTTVAR
, rmx_rttvar
);
610 if (lockrest
|| locking
)
611 rt_metrics
.rmx_locks
|= flag
;
618 newroute(int argc
, char **argv
)
621 const char *err_str
, *dest
= "", *gateway
= "";
622 int ishost
= 0, proxy
= 0, ret
, attempts
, oerrno
, flags
= RTF_STATIC
;
624 struct hostent
*hp
= 0;
627 errx(EX_NOPERM
, "must be root to alter routing table");
631 shutdown(s
, SHUT_RD
); /* Don't want to read back our messages */
633 if (**(++argv
)== '-') {
634 switch (key
= keyword(1 + *argv
)) {
637 aflen
= sizeof(struct sockaddr_dl
);
641 aflen
= sizeof(struct sockaddr_in
);
646 aflen
= sizeof(struct sockaddr_in6
);
651 aflen
= sizeof(struct sockaddr_at
);
655 aflen
= sizeof(union sockunion
);
660 aflen
= sizeof(struct sockaddr_ns
);
668 flags
&= ~RTF_STATIC
;
686 flags
|= RTF_BLACKHOLE
;
698 flags
|= RTF_CLONING
;
701 flags
|= RTF_XRESOLVE
;
709 getaddr(RTA_IFA
, *++argv
, 0);
714 getaddr(RTA_IFP
, *++argv
, 0);
719 getaddr(RTA_GENMASK
, *++argv
, 0);
724 getaddr(RTA_GATEWAY
, *++argv
, 0);
729 ishost
= getaddr(RTA_DST
, *++argv
, &hp
);
735 getaddr(RTA_NETMASK
, *++argv
, 0);
743 if (prefixlen(*++argv
) == -1) {
753 aflen
= sizeof(struct sockaddr_mpls
);
756 flags
|= RTF_MPLSOPS
;
758 mplsop
= MPLSLOP_POP
;
761 getaddr(RTA_MPLS1
, "", 0);
764 getaddr(RTA_MPLS2
, "", 0);
767 getaddr(RTA_MPLS3
, "", 0);
774 warnx("Push or swap after pop. Ignoring.");
778 mplsop
= MPLSLOP_PUSH
;
782 warnx("Swap after push. Ignoring.");
786 warnx("Too many swaps. Ignoring.");
789 mplsop
= MPLSLOP_SWAP
;
792 flags
|= RTF_MPLSOPS
;
794 aflen
= sizeof(struct sockaddr_mpls
);
797 switch(pushcount
+ swapcount
){
799 getaddr(RTA_MPLS1
, *++argv
, 0);
802 getaddr(RTA_MPLS2
, *++argv
, 0);
805 getaddr(RTA_MPLS3
, *++argv
, 0);
819 set_metric(*++argv
, key
);
825 if ((rtm_addrs
& RTA_DST
) == 0) {
827 ishost
= getaddr(RTA_DST
, *argv
, &hp
);
828 } else if ((rtm_addrs
& RTA_GATEWAY
) == 0) {
830 getaddr(RTA_GATEWAY
, *argv
, &hp
);
832 getaddr(RTA_NETMASK
, *argv
, 0);
840 if (af
== AF_INET6
) {
841 rtm_addrs
&= ~RTA_NETMASK
;
842 memset((void *)&so_mask
, 0, sizeof(so_mask
));
852 flags
|= RTF_GATEWAY
;
854 so_dst
.sinarp
.sin_other
= SIN_PROXY
;
855 flags
|= RTF_ANNOUNCE
;
857 for (attempts
= 1; ; attempts
++) {
859 if ((ret
= rtmsg(*cmd
, flags
)) == 0)
861 if (errno
!= ENETUNREACH
&& errno
!= ESRCH
)
863 if (af
== AF_INET
&& *gateway
!= '\0' &&
864 hp
!= NULL
&& hp
->h_addr_list
[1] != NULL
) {
866 memmove(&so_gate
.sin
.sin_addr
,
868 MIN((size_t)hp
->h_length
,
869 sizeof(so_gate
.sin
.sin_addr
)));
877 printf("%s %s %s", cmd
, ishost
? "host" : "net", dest
);
878 if (*gateway
!= '\0') {
879 printf(": gateway %s", gateway
);
880 if (attempts
> 1 && ret
== 0 && af
== AF_INET
)
882 inet_ntoa(((struct sockaddr_in
*)&route
.rt_gateway
)->sin_addr
));
889 err_str
= "not in table";
892 err_str
= "entry in use";
895 err_str
= "routing table overflow";
897 case EDQUOT
: /* handle recursion avoidance in
899 err_str
= "gateway uses the same route";
902 err_str
= strerror(oerrno
);
905 printf(": %s\n", err_str
);
912 inet_makenetandmask(u_long net
, struct sockaddr_in
*in
, u_long bits
)
914 u_long addr
, mask
= 0;
917 rtm_addrs
|= RTA_NETMASK
;
919 * XXX: This approach unable to handle 0.0.0.1/32 correctly
920 * as inet_network() converts 0.0.0.1 and 1 equally.
923 addr
= net
<< IN_CLASSA_NSHIFT
;
924 else if (net
<= 0xffff)
925 addr
= net
<< IN_CLASSB_NSHIFT
;
926 else if (net
<= 0xffffff)
927 addr
= net
<< IN_CLASSC_NSHIFT
;
932 mask
= 0xffffffff << (32 - bits
);
935 else if (IN_CLASSA(addr
))
936 mask
= IN_CLASSA_NET
;
937 else if (IN_CLASSB(addr
))
938 mask
= IN_CLASSB_NET
;
939 else if (IN_CLASSC(addr
))
940 mask
= IN_CLASSC_NET
;
941 else if (IN_MULTICAST(addr
))
942 mask
= IN_CLASSD_NET
;
946 in
->sin_addr
.s_addr
= htonl(addr
);
948 in
->sin_addr
.s_addr
= htonl(mask
);
951 cp
= (char *)(&in
->sin_addr
+ 1);
952 while (*--cp
== 0 && cp
> (char *)in
)
954 in
->sin_len
= 1 + cp
- (char *)in
;
959 * XXX the function may need more improvement...
962 inet6_makenetandmask(struct sockaddr_in6
*sin6
, const char *plen
)
967 if (IN6_IS_ADDR_UNSPECIFIED(&sin6
->sin6_addr
) &&
968 sin6
->sin6_scope_id
== 0) {
970 } else if ((sin6
->sin6_addr
.s6_addr
[0] & 0xe0) == 0x20) {
971 /* aggregatable global unicast - RFC2374 */
972 memset(&in6
, 0, sizeof(in6
));
973 if (memcmp(&sin6
->sin6_addr
.s6_addr
[8],
974 &in6
.s6_addr
[8], 8) == 0)
979 if (plen
== NULL
|| strcmp(plen
, "128") == 0)
981 rtm_addrs
|= RTA_NETMASK
;
988 * Interpret an argument as a network address of some kind,
989 * returning 1 if a host address, 0 if a network address.
992 getaddr(int which
, char *str
, struct hostent
**hpp
)
999 int afamily
; /* local copy of af so we can change it */
1003 aflen
= sizeof(struct sockaddr_in
);
1014 struct ifaddrs
*ifap
, *ifa
;
1015 struct sockaddr_dl
*sdl
= NULL
;
1017 if (getifaddrs(&ifap
))
1018 err(1, "getifaddrs");
1020 for (ifa
= ifap
; ifa
!= NULL
; ifa
= ifa
->ifa_next
) {
1021 if (ifa
->ifa_addr
->sa_family
!= AF_LINK
)
1024 if (strcmp(str
, ifa
->ifa_name
) != 0)
1027 sdl
= (struct sockaddr_dl
*)ifa
->ifa_addr
;
1029 /* If we found it, then use it */
1032 * Copy is safe since we have a
1033 * sockaddr_storage member in sockunion{}.
1034 * Note that we need to copy before calling
1037 * Interface routes are routed only by their
1038 * sdl_index or interface name (we provide
1039 * both here), not by the ARP address of the
1040 * local interface (alen, slen = 0).
1042 memcpy(&su
->sdl
, sdl
, sdl
->sdl_len
);
1043 su
->sdl
.sdl_alen
= 0;
1044 su
->sdl
.sdl_slen
= 0;
1074 usage("internal error");
1077 su
->sa
.sa_len
= aflen
;
1078 su
->sa
.sa_family
= afamily
; /* cases that don't want it have left
1080 if (strcmp(str
, "default") == 0) {
1082 * Default is net 0.0.0.0/0
1087 /* bzero(su, sizeof(*su)); *//* for readability */
1088 getaddr(RTA_NETMASK
, str
, 0);
1092 /* bzero(su, sizeof(*su)); *//* for readability */
1101 struct addrinfo hints
, *res
;
1104 if (which
== RTA_DST
&& (q
= strchr(str
, '/')) != NULL
)
1106 memset(&hints
, 0, sizeof(hints
));
1107 hints
.ai_family
= afamily
; /*AF_INET6*/
1108 hints
.ai_flags
= AI_NUMERICHOST
;
1109 hints
.ai_socktype
= SOCK_DGRAM
; /*dummy*/
1110 if (getaddrinfo(str
, "0", &hints
, &res
) != 0 ||
1111 res
->ai_family
!= AF_INET6
||
1112 res
->ai_addrlen
!= sizeof(su
->sin6
)) {
1113 fprintf(stderr
, "%s: bad value\n", str
);
1116 memcpy(&su
->sin6
, res
->ai_addr
, sizeof(su
->sin6
));
1118 if ((IN6_IS_ADDR_LINKLOCAL(&su
->sin6
.sin6_addr
) ||
1119 IN6_IS_ADDR_LINKLOCAL(&su
->sin6
.sin6_addr
)) &&
1120 su
->sin6
.sin6_scope_id
) {
1121 *(u_int16_t
*)&su
->sin6
.sin6_addr
.s6_addr
[2] =
1122 htons(su
->sin6
.sin6_scope_id
);
1123 su
->sin6
.sin6_scope_id
= 0;
1129 if (which
== RTA_DST
)
1130 return(inet6_makenetandmask(&su
->sin6
, q
));
1137 if (which
== RTA_DST
) {
1138 struct sockaddr_ns
*sms
= &(so_mask
.sns
);
1139 memset(sms
, 0, sizeof(*sms
));
1140 sms
->sns_family
= 0;
1142 sms
->sns_addr
.x_net
= *(union ns_net
*)ns_bh
;
1143 rtm_addrs
|= RTA_NETMASK
;
1145 su
->sns
.sns_addr
= ns_addr(str
);
1146 return(!ns_nullhost(su
->sns
.sns_addr
));
1150 if (!atalk_aton(str
, &su
->sat
.sat_addr
))
1151 errx(EX_NOHOST
, "bad address: %s", str
);
1152 rtm_addrs
|= RTA_NETMASK
;
1153 return(forcehost
|| su
->sat
.sat_addr
.s_node
!= 0);
1156 link_addr(str
, &su
->sdl
);
1160 su
->sa
.sa_len
= sizeof(*su
);
1161 sockaddr(str
, &su
->sa
);
1168 bzero(su
, sizeof(*su
));
1169 su
->sa
.sa_len
= sizeof(*su
);
1170 su
->sa
.sa_family
= AF_MPLS
;
1171 su
->smpls
.smpls_op
= mplsop
;
1172 if (mplsop
!= MPLSLOP_POP
&& mplsop
!= MPLSLOP_POPALL
&&
1174 if (sscanf(str
, "%u", &label
) != 1)
1175 errx(EX_NOHOST
, "bad address: %s", str
);
1176 su
->smpls
.smpls_label
= htonl(label
);
1190 q
= strchr(str
,'/');
1191 if (q
!= NULL
&& which
== RTA_DST
) {
1193 if ((val
= inet_network(str
)) != INADDR_NONE
) {
1194 inet_makenetandmask(
1195 val
, &su
->sin
, strtoul(q
+1, 0, 0));
1200 if ((which
!= RTA_DST
|| forcenet
== 0) &&
1201 inet_aton(str
, &su
->sin
.sin_addr
)) {
1202 val
= su
->sin
.sin_addr
.s_addr
;
1203 if (which
!= RTA_DST
||
1204 inet_lnaof(su
->sin
.sin_addr
) != INADDR_ANY
)
1211 if (which
== RTA_DST
&& forcehost
== 0 &&
1212 ((val
= inet_network(str
)) != INADDR_NONE
||
1213 ((np
= getnetbyname(str
)) != NULL
&& (val
= np
->n_net
) != 0))) {
1215 inet_makenetandmask(val
, &su
->sin
, 0);
1218 hp
= gethostbyname(str
);
1221 su
->sin
.sin_family
= hp
->h_addrtype
;
1222 memmove((char *)&su
->sin
.sin_addr
, hp
->h_addr
,
1223 MIN((size_t)hp
->h_length
, sizeof(su
->sin
.sin_addr
)));
1226 errx(EX_NOHOST
, "bad address: %s", str
);
1230 prefixlen(const char *len_str
)
1232 int len
= atoi(len_str
), q
, r
;
1236 rtm_addrs
|= RTA_NETMASK
;
1241 p
= (char *)&so_mask
.sin6
.sin6_addr
;
1246 p
= (char *)&so_mask
.sin
.sin_addr
;
1249 fprintf(stderr
, "prefixlen not supported in this af\n");
1254 if (len
< 0 || max
< len
) {
1255 fprintf(stderr
, "%s: bad value\n", len_str
);
1261 so_mask
.sa
.sa_family
= af
;
1262 so_mask
.sa
.sa_len
= aflen
;
1263 memset((void *)p
, 0, max
/ 8);
1265 memset((void *)p
, 0xff, q
);
1267 *((u_char
*)p
+ q
) = (0xff00 >> r
) & 0xff;
1275 short ns_nullh
[] = {0,0,0};
1278 ns_print(struct sockaddr_ns
*sns
)
1280 struct ns_addr work
;
1281 union { union ns_net net_e
; u_long long_e
; } net
;
1283 static char mybuf
[50+MAXHOSTNAMELEN
], cport
[10], chost
[25];
1284 const char *host
= "";
1288 work
= sns
->sns_addr
;
1289 port
= ntohs(work
.x_port
);
1291 net
.net_e
= work
.x_net
;
1292 if (ns_nullhost(work
) && net
.long_e
== 0) {
1294 strncpy(mybuf
, "*.*", sizeof(mybuf
));
1296 sprintf(mybuf
, "*.%XH", port
);
1300 if (memcmp(ns_bh
, work
.x_host
.c_host
, 6) == 0)
1302 else if (memcmp(ns_nullh
, work
.x_host
.c_host
, 6) == 0)
1305 q
= work
.x_host
.c_host
;
1306 sprintf(chost
, "%02X%02X%02X%02X%02X%02XH",
1307 q
[0], q
[1], q
[2], q
[3], q
[4], q
[5]);
1308 for (p
= chost
; *p
== '0' && p
< chost
+ 12; p
++)
1313 sprintf(cport
, ".%XH", htons(port
));
1317 snprintf(mybuf
, sizeof(mybuf
), "%lxH.%s%s",
1318 (unsigned long)ntohl(net
.long_e
),
1329 char *buf
, *lim
, *next
;
1330 struct rt_msghdr
*rtm
;
1334 mib
[2] = 0; /* protocol */
1335 mib
[3] = 0; /* wildcard address family */
1336 mib
[4] = NET_RT_IFLIST
;
1337 mib
[5] = 0; /* no flags */
1338 if (sysctl(mib
, 6, NULL
, &needed
, NULL
, 0) < 0)
1339 err(EX_OSERR
, "route-sysctl-estimate");
1340 if ((buf
= malloc(needed
)) == NULL
)
1341 errx(EX_OSERR
, "malloc failed");
1342 if (sysctl(mib
, 6, buf
, &needed
, NULL
, 0) < 0)
1343 err(EX_OSERR
, "actual retrieval of interface table");
1345 for (next
= buf
; next
< lim
; next
+= rtm
->rtm_msglen
) {
1346 rtm
= (struct rt_msghdr
*)next
;
1347 print_rtmsg(rtm
, rtm
->rtm_msglen
);
1364 n
= read(s
, msg
, 2048);
1366 printf("\ngot message of size %d on %s", n
, ctime(&now
));
1367 print_rtmsg((struct rt_msghdr
*)msg
, n
);
1372 struct rt_msghdr m_rtm
;
1377 rtmsg(int cmd
, int flags
)
1381 char *cp
= m_rtmsg
.m_space
;
1384 #define NEXTADDR(w, u) \
1385 if (rtm_addrs & (w)) {\
1386 l = ROUNDUP(u.sa.sa_len); memmove(cp, &(u), l); cp += l;\
1387 if (verbose) sodump(&(u),"u");\
1391 memset(&m_rtmsg
, 0, sizeof(m_rtmsg
));
1394 else if (cmd
== 'c')
1396 else if (cmd
== 'g') {
1398 if (so_ifp
.sa
.sa_family
== 0) {
1399 so_ifp
.sa
.sa_family
= AF_LINK
;
1400 so_ifp
.sa
.sa_len
= sizeof(struct sockaddr_dl
);
1401 rtm_addrs
|= RTA_IFP
;
1405 #define rtm m_rtmsg.m_rtm
1407 rtm
.rtm_flags
= flags
;
1408 rtm
.rtm_version
= RTM_VERSION
;
1409 rtm
.rtm_seq
= ++seq
;
1410 rtm
.rtm_addrs
= rtm_addrs
;
1411 rtm
.rtm_rmx
= rt_metrics
;
1412 rtm
.rtm_inits
= rtm_inits
;
1414 if (rtm_addrs
& RTA_NETMASK
)
1416 NEXTADDR(RTA_DST
, so_dst
);
1417 NEXTADDR(RTA_GATEWAY
, so_gate
);
1418 NEXTADDR(RTA_NETMASK
, so_mask
);
1419 NEXTADDR(RTA_GENMASK
, so_genmask
);
1420 NEXTADDR(RTA_IFP
, so_ifp
);
1421 NEXTADDR(RTA_IFA
, so_ifa
);
1422 NEXTADDR(RTA_MPLS1
, so_mpls1
);
1423 NEXTADDR(RTA_MPLS2
, so_mpls2
);
1424 NEXTADDR(RTA_MPLS3
, so_mpls3
);
1425 rtm
.rtm_msglen
= l
= cp
- (char *)&m_rtmsg
;
1427 print_rtmsg(&rtm
, l
);
1430 if ((rlen
= write(s
, (char *)&m_rtmsg
, l
)) < 0) {
1431 warn("writing to routing socket");
1434 if (cmd
== RTM_GET
) {
1436 l
= read(s
, (char *)&m_rtmsg
, sizeof(m_rtmsg
));
1437 } while (l
> 0 && (rtm
.rtm_seq
!= seq
|| rtm
.rtm_pid
!= pid
));
1439 warn("read from routing socket");
1441 print_getmsg(&rtm
, l
);
1450 int olen
= so_mask
.sa
.sa_len
;
1451 char *cp1
= olen
+ (char *)&so_mask
, *cp2
;
1453 for (so_mask
.sa
.sa_len
= 0; cp1
> (char *)&so_mask
; )
1455 so_mask
.sa
.sa_len
= 1 + cp1
- (char *)&so_mask
;
1458 if ((rtm_addrs
& RTA_DST
) == 0)
1460 switch (so_dst
.sa
.sa_family
) {
1472 cp1
= so_mask
.sa
.sa_len
+ 1 + (char *)&so_dst
;
1473 cp2
= so_dst
.sa
.sa_len
+ 1 + (char *)&so_dst
;
1476 cp2
= so_mask
.sa
.sa_len
+ 1 + (char *)&so_mask
;
1477 while (cp1
> so_dst
.sa
.sa_data
)
1481 const char *msgtypes
[] = {
1483 "RTM_ADD: Add Route",
1484 "RTM_DELETE: Delete Route",
1485 "RTM_CHANGE: Change Metrics or flags",
1486 "RTM_GET: Report Metrics",
1487 "RTM_LOSING: Kernel Suspects Partitioning",
1488 "RTM_REDIRECT: Told to use different route",
1489 "RTM_MISS: Lookup failed on this address",
1490 "RTM_LOCK: fix specified metrics",
1491 "RTM_OLDADD: caused by SIOCADDRT",
1492 "RTM_OLDDEL: caused by SIOCDELRT",
1493 "RTM_RESOLVE: Route created by cloning",
1494 "RTM_NEWADDR: address being added to iface",
1495 "RTM_DELADDR: address being removed from iface",
1496 "RTM_IFINFO: iface status change",
1497 "RTM_NEWMADDR: new multicast group membership on iface",
1498 "RTM_DELMADDR: multicast group membership removed from iface",
1499 "RTM_IFANNOUNCE: interface arrival/departure",
1503 char metricnames
[] =
1504 "\011pksent\010rttvar\7rtt\6ssthresh\5sendpipe\4recvpipe\3expire\2hopcount"
1507 "\1UP\2GATEWAY\3HOST\4REJECT\5DYNAMIC\6MODIFIED\7DONE\010MASK_PRESENT"
1508 "\011CLONING\012XRESOLVE\013LLINFO\014STATIC\015BLACKHOLE\016b016"
1509 "\017PROTO2\020PROTO1\021PRCLONING\022WASCLONED\023PROTO3\024CHAINDELETE"
1510 "\025PINNED\026LOCAL\027BROADCAST\030MULTICAST";
1512 "\1UP\2BROADCAST\3DEBUG\4LOOPBACK\5PTP\6b6\7RUNNING\010NOARP"
1513 "\011PPROMISC\012ALLMULTI\013OACTIVE\014SIMPLEX\015LINK0\016LINK1"
1514 "\017LINK2\020MULTICAST";
1516 "\1DST\2GATEWAY\3NETMASK\4GENMASK\5IFP\6IFA\7AUTHOR\010BRD"
1517 "\011MPLS1\012MPLS2\013MPLS3";
1520 print_rtmsg(struct rt_msghdr
*rtm
, int msglen __unused
)
1522 struct if_msghdr
*ifm
;
1523 struct ifa_msghdr
*ifam
;
1525 struct ifma_msghdr
*ifmam
;
1527 struct if_announcemsghdr
*ifan
;
1531 if (rtm
->rtm_version
!= RTM_VERSION
) {
1532 printf("routing message version %d not understood\n",
1536 if (msgtypes
[rtm
->rtm_type
] != NULL
)
1537 printf("%s: ", msgtypes
[rtm
->rtm_type
]);
1539 printf("#%d: ", rtm
->rtm_type
);
1540 printf("len %d, ", rtm
->rtm_msglen
);
1541 switch (rtm
->rtm_type
) {
1543 ifm
= (struct if_msghdr
*)rtm
;
1544 printf("if# %d, flags:", ifm
->ifm_index
);
1545 bprintf(stdout
, ifm
->ifm_flags
, ifnetflags
);
1546 pmsg_addrs((char *)(ifm
+ 1), ifm
->ifm_addrs
);
1550 ifam
= (struct ifa_msghdr
*)rtm
;
1551 printf("metric %d, flags:", ifam
->ifam_metric
);
1552 bprintf(stdout
, ifam
->ifam_flags
, routeflags
);
1553 pmsg_addrs((char *)(ifam
+ 1), ifam
->ifam_addrs
);
1558 ifmam
= (struct ifma_msghdr
*)rtm
;
1559 pmsg_addrs((char *)(ifmam
+ 1), ifmam
->ifmam_addrs
);
1562 case RTM_IFANNOUNCE
:
1563 ifan
= (struct if_announcemsghdr
*)rtm
;
1564 printf("if# %d, what: ", ifan
->ifan_index
);
1565 switch (ifan
->ifan_what
) {
1569 case IFAN_DEPARTURE
:
1570 printf("departure");
1573 printf("#%d", ifan
->ifan_what
);
1580 printf("pid: %ld, seq %d, errno %d, flags:",
1581 (long)rtm
->rtm_pid
, rtm
->rtm_seq
, rtm
->rtm_errno
);
1582 bprintf(stdout
, rtm
->rtm_flags
, routeflags
);
1588 print_getmsg(struct rt_msghdr
*rtm
, int msglen
)
1590 struct sockaddr
*dst
= NULL
, *gate
= NULL
, *mask
= NULL
;
1591 struct sockaddr_dl
*ifp
= NULL
;
1592 struct sockaddr
*sa
;
1596 printf(" route to: %s\n", routename(&so_dst
.sa
));
1597 if (rtm
->rtm_version
!= RTM_VERSION
) {
1598 warnx("routing message version %d not understood",
1602 if (rtm
->rtm_msglen
> msglen
) {
1603 warnx("message length mismatch, in packet %d, returned %d",
1604 rtm
->rtm_msglen
, msglen
);
1606 if (rtm
->rtm_errno
!= 0) {
1607 errno
= rtm
->rtm_errno
;
1608 warn("message indicates error %d", errno
);
1611 cp
= ((char *)(rtm
+ 1));
1613 for (i
= 1; i
!= 0; i
<<= 1)
1614 if (i
& rtm
->rtm_addrs
) {
1615 sa
= (struct sockaddr
*)cp
;
1627 if (sa
->sa_family
== AF_LINK
&&
1628 ((struct sockaddr_dl
*)sa
)->sdl_nlen
)
1629 ifp
= (struct sockaddr_dl
*)sa
;
1634 if (dst
!= NULL
&& mask
!= NULL
)
1635 mask
->sa_family
= dst
->sa_family
; /* XXX */
1637 printf("destination: %s\n", routename(dst
));
1639 int savenflag
= nflag
;
1642 printf(" mask: %s\n", routename(mask
));
1645 if (gate
!= NULL
&& rtm
->rtm_flags
& RTF_GATEWAY
)
1646 printf(" gateway: %s\n", routename(gate
));
1648 printf(" interface: %.*s\n",
1649 ifp
->sdl_nlen
, ifp
->sdl_data
);
1651 bprintf(stdout
, rtm
->rtm_flags
, routeflags
);
1653 #define lock(f) ((rtm->rtm_rmx.rmx_locks & __CONCAT(RTV_,f)) ? 'L' : ' ')
1654 #define msec(u) (((u) + 500) / 1000) /* usec to msec */
1657 recvpipe sendpipe ssthresh rtt,msec rttvar hopcount mtu expire");
1658 printf("%8ld%c ", rtm
->rtm_rmx
.rmx_recvpipe
, lock(RPIPE
));
1659 printf("%8ld%c ", rtm
->rtm_rmx
.rmx_sendpipe
, lock(SPIPE
));
1660 printf("%8ld%c ", rtm
->rtm_rmx
.rmx_ssthresh
, lock(SSTHRESH
));
1661 printf("%8ld%c ", msec(rtm
->rtm_rmx
.rmx_rtt
), lock(RTT
));
1662 printf("%8ld%c ", msec(rtm
->rtm_rmx
.rmx_rttvar
), lock(RTTVAR
));
1663 printf("%8ld%c ", rtm
->rtm_rmx
.rmx_hopcount
, lock(HOPCOUNT
));
1664 printf("%8ld%c ", rtm
->rtm_rmx
.rmx_mtu
, lock(MTU
));
1665 if (rtm
->rtm_rmx
.rmx_expire
!= 0)
1666 rtm
->rtm_rmx
.rmx_expire
-= time(0);
1667 printf("%8ld%c\n", rtm
->rtm_rmx
.rmx_expire
, lock(EXPIRE
));
1670 #define RTA_IGN (RTA_DST|RTA_GATEWAY|RTA_NETMASK|RTA_IFP|RTA_IFA|RTA_BRD)
1673 else if (rtm
->rtm_addrs
&~ RTA_IGN
) {
1674 printf("sockaddrs: ");
1675 bprintf(stdout
, rtm
->rtm_addrs
, addrnames
);
1682 pmsg_common(struct rt_msghdr
*rtm
)
1684 printf("\nlocks: ");
1685 bprintf(stdout
, rtm
->rtm_rmx
.rmx_locks
, metricnames
);
1687 bprintf(stdout
, rtm
->rtm_inits
, metricnames
);
1688 pmsg_addrs(((char *)(rtm
+ 1)), rtm
->rtm_addrs
);
1692 pmsg_addrs(char *cp
, int addrs
)
1694 struct sockaddr
*sa
;
1701 printf("\nsockaddrs: ");
1702 bprintf(stdout
, addrs
, addrnames
);
1704 for (i
= 1; i
!= 0; i
<<= 1)
1706 sa
= (struct sockaddr
*)cp
;
1707 printf(" %s", routename(sa
));
1715 bprintf(FILE *fp
, int b
, u_char
*str
)
1722 while ((i
= *str
++) != 0) {
1723 if (b
& (1 << (i
-1))) {
1730 for (; (i
= *str
) > 32; str
++)
1741 keyword(const char *cp
)
1743 struct keytab
*kt
= keywords
;
1745 while (kt
->kt_cp
!= NULL
&& strcmp(kt
->kt_cp
, cp
) != 0)
1751 sodump(sup su
, const char *which
)
1753 switch (su
->sa
.sa_family
) {
1755 printf("%s: link %s; ",
1756 which
, link_ntoa(&su
->sdl
));
1759 printf("%s: inet %s; ",
1760 which
, inet_ntoa(su
->sin
.sin_addr
));
1763 printf("%s: atalk %s; ",
1764 which
, atalk_ntoa(su
->sat
.sat_addr
));
1768 printf("%s: xns %s; ",
1769 which
, ns_ntoa(su
->sns
.sns_addr
));
1786 sockaddr(char *addr
, struct sockaddr
*sa
)
1788 char *cp
= (char *)sa
;
1789 int size
= sa
->sa_len
;
1790 char *cplim
= cp
+ size
;
1791 int byte
= 0, state
= VIRGIN
, new = 0 /* foil gcc */;
1793 memset(cp
, 0, size
);
1796 if ((*addr
>= '0') && (*addr
<= '9')) {
1798 } else if ((*addr
>= 'a') && (*addr
<= 'f')) {
1799 new = *addr
- 'a' + 10;
1800 } else if ((*addr
>= 'A') && (*addr
<= 'F')) {
1801 new = *addr
- 'A' + 10;
1802 } else if (*addr
== '\0')
1807 switch (state
/* | INPUT */) {
1808 case GOTTWO
| DIGIT
:
1809 *cp
++ = byte
; /*FALLTHROUGH*/
1810 case VIRGIN
| DIGIT
:
1811 state
= GOTONE
; byte
= new; continue;
1812 case GOTONE
| DIGIT
:
1813 state
= GOTTWO
; byte
= new + (byte
<< 4); continue;
1814 default: /* | DELIM */
1815 state
= VIRGIN
; *cp
++ = byte
; byte
= 0; continue;
1818 *cp
++ = byte
; /* FALLTHROUGH */
1823 } while (cp
< cplim
);
1824 sa
->sa_len
= cp
- (char *)sa
;
1828 atalk_aton(const char *text
, struct at_addr
*addr
)
1832 if (sscanf(text
, "%u.%u", &net
, &node
) != 2
1833 || net
> 0xffff || node
> 0xff)
1835 addr
->s_net
= htons(net
);
1836 addr
->s_node
= node
;
1841 atalk_ntoa(struct at_addr at
)
1843 static char buf
[20];
1845 snprintf(buf
, sizeof(buf
), "%u.%u", ntohs(at
.s_net
), at
.s_node
);