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.15 2007/04/11 18:51:33 dillon 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>
74 struct sockaddr_in sin
;
76 struct sockaddr_in6 sin6
;
78 struct sockaddr_at sat
;
80 struct sockaddr_ns sns
;
82 struct sockaddr_dl sdl
;
83 struct sockaddr_inarp sinarp
;
84 struct sockaddr_storage ss
; /* added to avoid memory overrun */
85 } so_dst
, so_gate
, so_mask
, so_genmask
, so_ifa
, so_ifp
;
87 typedef union sockunion
*sup
;
92 static struct ortentry route
;
93 static struct rt_metrics rt_metrics
;
94 static int pid
, rtm_addrs
;
96 static int forcehost
, forcenet
, af
, qflag
, tflag
;
97 static int iflag
, verbose
, aflen
= sizeof(struct sockaddr_in
);
98 static int locking
, lockrest
, debugonly
;
99 static u_long rtm_inits
;
102 static short ns_bh
[] = {-1,-1,-1};
105 static int atalk_aton(const char *, struct at_addr
*);
106 static char *atalk_ntoa(struct at_addr
);
107 static void flushroutes(int, char **);
108 static void set_metric(char *, int);
109 static void newroute(int, char **);
110 static void inet_makenetandmask(u_long
, struct sockaddr_in
*, u_long
);
111 static void interfaces(void);
112 static void monitor(void);
113 static void sockaddr(char *, struct sockaddr
*);
114 static void sodump(sup
, const char *);
115 static void bprintf(FILE *, int, u_char
*);
116 static void print_getmsg(struct rt_msghdr
*, int);
117 static void print_rtmsg(struct rt_msghdr
*, int);
118 static void pmsg_common(struct rt_msghdr
*);
119 static void pmsg_addrs(char *, int);
120 static void mask_addr(void);
121 static int getaddr(int, char *, struct hostent
**);
122 static int rtmsg(int, int);
123 static int prefixlen(const char *);
125 static int inet6_makenetandmask(struct sockaddr_in6
*, const char *);
129 usage(const char *cp
)
132 warnx("bad keyword: %s", cp
);
133 fprintf(stderr
, "usage: route [-dnqtv] command [[modifiers] args]\n");
139 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
140 #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
143 main(int argc
, char **argv
)
150 while ((ch
= getopt(argc
, argv
, "c:wnqdtv")) != -1)
153 cpuflag
= strtol(optarg
, NULL
, 0);
183 s
= open(_PATH_DEVNULL
, O_WRONLY
, 0);
185 s
= socket(PF_ROUTE
, SOCK_RAW
, 0);
187 err(EX_OSERR
, "socket");
189 switch (keyword(*argv
)) {
197 newroute(argc
, argv
);
209 flushroutes(argc
, argv
);
218 * Purge all entries in the routing tables not
219 * associated with network interfaces.
222 flushroutes(int argc
, char **argv
)
225 int mib
[7], rlen
, seqno
;
227 char *buf
, *next
, *lim
;
228 struct rt_msghdr
*rtm
;
231 errx(EX_NOPERM
, "must be root to alter routing table");
233 shutdown(s
, SHUT_RD
); /* Don't want to read back our messages */
236 if (argc
== 2 && **argv
== '-')
237 switch (keyword(*argv
+ 1)) {
264 mib
[2] = 0; /* protocol */
265 mib
[3] = 0; /* wildcard address family */
266 mib
[4] = NET_RT_DUMP
;
267 mib
[5] = 0; /* no flags */
274 if (sysctl(mib
, miblen
, NULL
, &needed
, NULL
, 0) < 0)
275 err(EX_OSERR
, "route-sysctl-estimate");
276 if ((buf
= malloc(needed
)) == NULL
)
277 errx(EX_OSERR
, "malloc failed");
278 if (sysctl(mib
, miblen
, buf
, &needed
, NULL
, 0) < 0)
279 err(EX_OSERR
, "route-sysctl-get");
282 printf("Examining routing table from sysctl\n");
284 for (next
= buf
; next
< lim
; next
+= rtm
->rtm_msglen
) {
285 rtm
= (struct rt_msghdr
*)next
;
287 print_rtmsg(rtm
, rtm
->rtm_msglen
);
288 if ((rtm
->rtm_flags
& RTF_GATEWAY
) == 0)
291 struct sockaddr
*sa
= (struct sockaddr
*)(rtm
+ 1);
293 if (sa
->sa_family
!= af
)
298 rtm
->rtm_type
= RTM_DELETE
;
299 rtm
->rtm_seq
= seqno
;
300 rlen
= write(s
, next
, rtm
->rtm_msglen
);
301 if (rlen
< (int)rtm
->rtm_msglen
) {
302 warn("write to routing socket");
303 printf("got only %d for rlen\n", rlen
);
310 print_rtmsg(rtm
, rlen
);
312 struct sockaddr
*sa
= (struct sockaddr
*)(rtm
+ 1);
314 printf("%-20s ", rtm
->rtm_flags
& RTF_HOST
?
315 routename(sa
) : netname(sa
));
317 printf("%-20.20s ", rtm
->rtm_flags
& RTF_HOST
?
318 routename(sa
) : netname(sa
));
320 sa
= (struct sockaddr
*)(ROUNDUP(sa
->sa_len
) +
323 printf("%-20s ", routename(sa
));
325 printf("%-20.20s ", routename(sa
));
333 routename(struct sockaddr
*sa
)
336 static char line
[MAXHOSTNAMELEN
+ 1];
338 static char domain
[MAXHOSTNAMELEN
+ 1];
339 static int first
= 1;
343 if (gethostname(domain
, MAXHOSTNAMELEN
) == 0 &&
344 (cp
= strchr(domain
, '.'))) {
345 domain
[MAXHOSTNAMELEN
] = '\0';
346 strcpy(domain
, cp
+ 1);
352 strcpy(line
, "default");
353 else switch (sa
->sa_family
) {
357 in
= ((struct sockaddr_in
*)sa
)->sin_addr
;
360 if (in
.s_addr
== INADDR_ANY
|| sa
->sa_len
< 4)
362 if (cp
== NULL
&& !nflag
) {
363 hp
= gethostbyaddr((char *)&in
, sizeof(struct in_addr
),
367 cptr
= strchr(hp
->h_name
, '.');
368 if (cptr
!= NULL
&& !wflag
&&
369 strcmp(cptr
+ 1, domain
) == 0)
375 strncpy(line
, cp
, sizeof(line
) - 1);
376 line
[sizeof(line
) - 1] = '\0';
378 sprintf(line
, "%s", inet_ntoa(in
));
385 struct sockaddr_in6 sin6
; /* use static var for safety */
387 #ifdef NI_WITHSCOPEID
388 niflags
= NI_WITHSCOPEID
;
391 memset(&sin6
, 0, sizeof(sin6
));
392 memcpy(&sin6
, sa
, sa
->sa_len
);
393 sin6
.sin6_len
= sizeof(struct sockaddr_in6
);
394 sin6
.sin6_family
= AF_INET6
;
396 if (sa
->sa_len
== sizeof(struct sockaddr_in6
) &&
397 (IN6_IS_ADDR_LINKLOCAL(&sin6
.sin6_addr
) ||
398 IN6_IS_ADDR_MC_LINKLOCAL(&sin6
.sin6_addr
)) &&
399 sin6
.sin6_scope_id
== 0) {
401 ntohs(*(u_int16_t
*)&sin6
.sin6_addr
.s6_addr
[2]);
402 sin6
.sin6_addr
.s6_addr
[2] = 0;
403 sin6
.sin6_addr
.s6_addr
[3] = 0;
407 niflags
|= NI_NUMERICHOST
;
408 if (getnameinfo((struct sockaddr
*)&sin6
, sin6
.sin6_len
,
409 line
, sizeof(line
), NULL
, 0, niflags
) != 0)
410 strncpy(line
, "invalid", sizeof(line
));
417 snprintf(line
, sizeof(line
), "atalk %s",
418 atalk_ntoa(((struct sockaddr_at
*)sa
)->sat_addr
));
423 return(ns_print((struct sockaddr_ns
*)sa
));
427 return(link_ntoa((struct sockaddr_dl
*)sa
));
432 * Unknown address family; just render the raw
433 * data in sa->sa_data as hex values.
435 uint8_t *sp
= (uint8_t *)sa
->sa_data
;
436 uint8_t *splim
= (uint8_t *)sa
+ sa
->sa_len
;
437 char *cps
= line
+ sprintf(line
, "(%d)", sa
->sa_family
);
438 char *cpe
= line
+ sizeof(line
);
440 while (sp
< splim
&& cps
< cpe
)
441 cps
+= snprintf(cps
, cpe
- cps
, " %02x", *sp
++);
449 * Return the name of the network whose address is given.
450 * The address is assumed to be that of a net or subnet, not a host.
453 netname(struct sockaddr
*sa
)
456 static char line
[MAXHOSTNAMELEN
+ 1];
457 struct netent
*np
= 0;
462 switch (sa
->sa_family
) {
466 in
= ((struct sockaddr_in
*)sa
)->sin_addr
;
468 i
= in
.s_addr
= ntohl(in
.s_addr
);
473 mask
= IN_CLASSA_NET
;
475 } else if (IN_CLASSB(i
)) {
476 mask
= IN_CLASSB_NET
;
479 mask
= IN_CLASSC_NET
;
483 * If there are more bits than the standard mask
484 * would suggest, subnets must be in use.
485 * Guess at the subnet mask, assuming reasonable
486 * width subnet fields.
488 while (in
.s_addr
&~ mask
)
489 mask
= (long)mask
>> subnetshift
;
490 net
= in
.s_addr
& mask
;
491 while ((mask
& 1) == 0)
492 mask
>>= 1, net
>>= 1;
493 np
= getnetbyaddr(net
, AF_INET
);
497 #define C(x) (unsigned)((x) & 0xff)
499 strncpy(line
, cp
, sizeof(line
));
500 else if ((in
.s_addr
& 0xffffff) == 0)
501 sprintf(line
, "%u", C(in
.s_addr
>> 24));
502 else if ((in
.s_addr
& 0xffff) == 0)
503 sprintf(line
, "%u.%u", C(in
.s_addr
>> 24),
505 else if ((in
.s_addr
& 0xff) == 0)
506 sprintf(line
, "%u.%u.%u", C(in
.s_addr
>> 24),
507 C(in
.s_addr
>> 16), C(in
.s_addr
>> 8));
509 sprintf(line
, "%u.%u.%u.%u", C(in
.s_addr
>> 24),
510 C(in
.s_addr
>> 16), C(in
.s_addr
>> 8),
519 struct sockaddr_in6 sin6
; /* use static var for safety */
521 #ifdef NI_WITHSCOPEID
522 niflags
= NI_WITHSCOPEID
;
525 memset(&sin6
, 0, sizeof(sin6
));
526 memcpy(&sin6
, sa
, sa
->sa_len
);
527 sin6
.sin6_len
= sizeof(struct sockaddr_in6
);
528 sin6
.sin6_family
= AF_INET6
;
530 if (sa
->sa_len
== sizeof(struct sockaddr_in6
) &&
531 (IN6_IS_ADDR_LINKLOCAL(&sin6
.sin6_addr
) ||
532 IN6_IS_ADDR_MC_LINKLOCAL(&sin6
.sin6_addr
)) &&
533 sin6
.sin6_scope_id
== 0) {
535 ntohs(*(u_int16_t
*)&sin6
.sin6_addr
.s6_addr
[2]);
536 sin6
.sin6_addr
.s6_addr
[2] = 0;
537 sin6
.sin6_addr
.s6_addr
[3] = 0;
541 niflags
|= NI_NUMERICHOST
;
542 if (getnameinfo((struct sockaddr
*)&sin6
, sin6
.sin6_len
,
543 line
, sizeof(line
), NULL
, 0, niflags
) != 0)
544 strncpy(line
, "invalid", sizeof(line
));
551 snprintf(line
, sizeof(line
), "atalk %s",
552 atalk_ntoa(((struct sockaddr_at
*)sa
)->sat_addr
));
557 return(ns_print((struct sockaddr_ns
*)sa
));
562 return(link_ntoa((struct sockaddr_dl
*)sa
));
568 * Unknown address family; just render the raw
569 * data in sa->sa_data as hex values.
571 uint8_t *sp
= (uint8_t *)sa
->sa_data
;
572 uint8_t *splim
= (uint8_t *)sa
+ sa
->sa_len
;
573 char *cps
= line
+ sprintf(line
, "af %d:", sa
->sa_family
);
574 char *cpe
= line
+ sizeof(line
);
576 while (sp
< splim
&& cps
< cpe
)
577 cps
+= snprintf(cps
, cpe
- cps
, " %02x", *sp
++);
585 set_metric(char *value
, int key
)
588 u_long noval
, *valp
= &noval
;
591 #define caseof(x, y, z) case x: valp = &rt_metrics.z; flag = y; break
592 caseof(K_MTU
, RTV_MTU
, rmx_mtu
);
593 caseof(K_HOPCOUNT
, RTV_HOPCOUNT
, rmx_hopcount
);
594 caseof(K_EXPIRE
, RTV_EXPIRE
, rmx_expire
);
595 caseof(K_RECVPIPE
, RTV_RPIPE
, rmx_recvpipe
);
596 caseof(K_SENDPIPE
, RTV_SPIPE
, rmx_sendpipe
);
597 caseof(K_SSTHRESH
, RTV_SSTHRESH
, rmx_ssthresh
);
598 caseof(K_RTT
, RTV_RTT
, rmx_rtt
);
599 caseof(K_RTTVAR
, RTV_RTTVAR
, rmx_rttvar
);
602 if (lockrest
|| locking
)
603 rt_metrics
.rmx_locks
|= flag
;
610 newroute(int argc
, char **argv
)
613 const char *err_str
, *dest
= "", *gateway
= "";
614 int ishost
= 0, proxy
= 0, ret
, attempts
, oerrno
, flags
= RTF_STATIC
;
616 struct hostent
*hp
= 0;
619 errx(EX_NOPERM
, "must be root to alter routing table");
623 shutdown(s
, SHUT_RD
); /* Don't want to read back our messages */
625 if (**(++argv
)== '-') {
626 switch (key
= keyword(1 + *argv
)) {
629 aflen
= sizeof(struct sockaddr_dl
);
633 aflen
= sizeof(struct sockaddr_in
);
638 aflen
= sizeof(struct sockaddr_in6
);
643 aflen
= sizeof(struct sockaddr_at
);
647 aflen
= sizeof(union sockunion
);
652 aflen
= sizeof(struct sockaddr_ns
);
660 flags
&= ~RTF_STATIC
;
678 flags
|= RTF_BLACKHOLE
;
690 flags
|= RTF_CLONING
;
693 flags
|= RTF_XRESOLVE
;
701 getaddr(RTA_IFA
, *++argv
, 0);
706 getaddr(RTA_IFP
, *++argv
, 0);
711 getaddr(RTA_GENMASK
, *++argv
, 0);
716 getaddr(RTA_GATEWAY
, *++argv
, 0);
721 ishost
= getaddr(RTA_DST
, *++argv
, &hp
);
727 getaddr(RTA_NETMASK
, *++argv
, 0);
735 if (prefixlen(*++argv
) == -1) {
753 set_metric(*++argv
, key
);
759 if ((rtm_addrs
& RTA_DST
) == 0) {
761 ishost
= getaddr(RTA_DST
, *argv
, &hp
);
762 } else if ((rtm_addrs
& RTA_GATEWAY
) == 0) {
764 getaddr(RTA_GATEWAY
, *argv
, &hp
);
766 getaddr(RTA_NETMASK
, *argv
, 0);
774 if (af
== AF_INET6
) {
775 rtm_addrs
&= ~RTA_NETMASK
;
776 memset((void *)&so_mask
, 0, sizeof(so_mask
));
786 flags
|= RTF_GATEWAY
;
788 so_dst
.sinarp
.sin_other
= SIN_PROXY
;
789 flags
|= RTF_ANNOUNCE
;
791 for (attempts
= 1; ; attempts
++) {
793 if ((ret
= rtmsg(*cmd
, flags
)) == 0)
795 if (errno
!= ENETUNREACH
&& errno
!= ESRCH
)
797 if (af
== AF_INET
&& *gateway
!= '\0' &&
798 hp
!= NULL
&& hp
->h_addr_list
[1] != NULL
) {
800 memmove(&so_gate
.sin
.sin_addr
,
802 MIN((size_t)hp
->h_length
,
803 sizeof(so_gate
.sin
.sin_addr
)));
811 printf("%s %s %s", cmd
, ishost
? "host" : "net", dest
);
812 if (*gateway
!= '\0') {
813 printf(": gateway %s", gateway
);
814 if (attempts
> 1 && ret
== 0 && af
== AF_INET
)
816 inet_ntoa(((struct sockaddr_in
*)&route
.rt_gateway
)->sin_addr
));
823 err_str
= "not in table";
826 err_str
= "entry in use";
829 err_str
= "routing table overflow";
831 case EDQUOT
: /* handle recursion avoidance in
833 err_str
= "gateway uses the same route";
836 err_str
= strerror(oerrno
);
839 printf(": %s\n", err_str
);
846 inet_makenetandmask(u_long net
, struct sockaddr_in
*in
, u_long bits
)
848 u_long addr
, mask
= 0;
851 rtm_addrs
|= RTA_NETMASK
;
854 else if (net
< 128) {
855 addr
= net
<< IN_CLASSA_NSHIFT
;
856 mask
= IN_CLASSA_NET
;
857 } else if (net
< 65536) {
858 addr
= net
<< IN_CLASSB_NSHIFT
;
859 mask
= IN_CLASSB_NET
;
860 } else if (net
< 16777216L) {
861 addr
= net
<< IN_CLASSC_NSHIFT
;
862 mask
= IN_CLASSC_NET
;
865 if ((addr
& IN_CLASSA_HOST
) == 0)
866 mask
= IN_CLASSA_NET
;
867 else if ((addr
& IN_CLASSB_HOST
) == 0)
868 mask
= IN_CLASSB_NET
;
869 else if ((addr
& IN_CLASSC_HOST
) == 0)
870 mask
= IN_CLASSC_NET
;
875 mask
= 0xffffffff << (32 - bits
);
876 in
->sin_addr
.s_addr
= htonl(addr
);
878 in
->sin_addr
.s_addr
= htonl(mask
);
881 cp
= (char *)(&in
->sin_addr
+ 1);
882 while (*--cp
== 0 && cp
> (char *)in
)
884 in
->sin_len
= 1 + cp
- (char *)in
;
889 * XXX the function may need more improvement...
892 inet6_makenetandmask(struct sockaddr_in6
*sin6
, const char *plen
)
897 if (IN6_IS_ADDR_UNSPECIFIED(&sin6
->sin6_addr
) &&
898 sin6
->sin6_scope_id
== 0) {
900 } else if ((sin6
->sin6_addr
.s6_addr
[0] & 0xe0) == 0x20) {
901 /* aggregatable global unicast - RFC2374 */
902 memset(&in6
, 0, sizeof(in6
));
903 if (memcmp(&sin6
->sin6_addr
.s6_addr
[8],
904 &in6
.s6_addr
[8], 8) == 0)
909 if (plen
== NULL
|| strcmp(plen
, "128") == 0)
911 rtm_addrs
|= RTA_NETMASK
;
918 * Interpret an argument as a network address of some kind,
919 * returning 1 if a host address, 0 if a network address.
922 getaddr(int which
, char *str
, struct hostent
**hpp
)
929 int afamily
; /* local copy of af so we can change it */
933 aflen
= sizeof(struct sockaddr_in
);
944 struct ifaddrs
*ifap
, *ifa
;
945 struct sockaddr_dl
*sdl
= NULL
;
947 if (getifaddrs(&ifap
))
948 err(1, "getifaddrs");
950 for (ifa
= ifap
; ifa
!= NULL
; ifa
= ifa
->ifa_next
) {
951 if (ifa
->ifa_addr
->sa_family
!= AF_LINK
)
954 if (strcmp(str
, ifa
->ifa_name
) != 0)
957 sdl
= (struct sockaddr_dl
*)ifa
->ifa_addr
;
959 /* If we found it, then use it */
962 * Copy is safe since we have a
963 * sockaddr_storage member in sockunion{}.
964 * Note that we need to copy before calling
967 * Interface routes are routed only by their
968 * sdl_index or interface name (we provide
969 * both here), not by the ARP address of the
970 * local interface (alen, slen = 0).
972 memcpy(&su
->sdl
, sdl
, sdl
->sdl_len
);
973 su
->sdl
.sdl_alen
= 0;
974 su
->sdl
.sdl_slen
= 0;
995 usage("internal error");
998 su
->sa
.sa_len
= aflen
;
999 su
->sa
.sa_family
= afamily
; /* cases that don't want it have left
1001 if (strcmp(str
, "default") == 0) {
1003 * Default is net 0.0.0.0/0
1008 /* bzero(su, sizeof(*su)); *//* for readability */
1009 getaddr(RTA_NETMASK
, str
, 0);
1013 /* bzero(su, sizeof(*su)); *//* for readability */
1022 struct addrinfo hints
, *res
;
1025 if (which
== RTA_DST
&& (q
= strchr(str
, '/')) != NULL
)
1027 memset(&hints
, 0, sizeof(hints
));
1028 hints
.ai_family
= afamily
; /*AF_INET6*/
1029 hints
.ai_flags
= AI_NUMERICHOST
;
1030 hints
.ai_socktype
= SOCK_DGRAM
; /*dummy*/
1031 if (getaddrinfo(str
, "0", &hints
, &res
) != 0 ||
1032 res
->ai_family
!= AF_INET6
||
1033 res
->ai_addrlen
!= sizeof(su
->sin6
)) {
1034 fprintf(stderr
, "%s: bad value\n", str
);
1037 memcpy(&su
->sin6
, res
->ai_addr
, sizeof(su
->sin6
));
1039 if ((IN6_IS_ADDR_LINKLOCAL(&su
->sin6
.sin6_addr
) ||
1040 IN6_IS_ADDR_LINKLOCAL(&su
->sin6
.sin6_addr
)) &&
1041 su
->sin6
.sin6_scope_id
) {
1042 *(u_int16_t
*)&su
->sin6
.sin6_addr
.s6_addr
[2] =
1043 htons(su
->sin6
.sin6_scope_id
);
1044 su
->sin6
.sin6_scope_id
= 0;
1050 if (which
== RTA_DST
)
1051 return(inet6_makenetandmask(&su
->sin6
, q
));
1058 if (which
== RTA_DST
) {
1059 struct sockaddr_ns
*sms
= &(so_mask
.sns
);
1060 memset(sms
, 0, sizeof(*sms
));
1061 sms
->sns_family
= 0;
1063 sms
->sns_addr
.x_net
= *(union ns_net
*)ns_bh
;
1064 rtm_addrs
|= RTA_NETMASK
;
1066 su
->sns
.sns_addr
= ns_addr(str
);
1067 return(!ns_nullhost(su
->sns
.sns_addr
));
1072 if (!atalk_aton(str
, &su
->sat
.sat_addr
))
1073 errx(EX_NOHOST
, "bad address: %s", str
);
1074 rtm_addrs
|= RTA_NETMASK
;
1075 return(forcehost
|| su
->sat
.sat_addr
.s_node
!= 0);
1078 link_addr(str
, &su
->sdl
);
1083 su
->sa
.sa_len
= sizeof(*su
);
1084 sockaddr(str
, &su
->sa
);
1096 q
= strchr(str
,'/');
1097 if (q
!= NULL
&& which
== RTA_DST
) {
1099 if ((val
= inet_network(str
)) != INADDR_NONE
) {
1100 inet_makenetandmask(
1101 val
, &su
->sin
, strtoul(q
+1, 0, 0));
1106 if ((which
!= RTA_DST
|| forcenet
== 0) &&
1107 inet_aton(str
, &su
->sin
.sin_addr
)) {
1108 val
= su
->sin
.sin_addr
.s_addr
;
1109 if (which
!= RTA_DST
||
1110 inet_lnaof(su
->sin
.sin_addr
) != INADDR_ANY
)
1117 if (which
== RTA_DST
&& forcehost
== 0 &&
1118 ((val
= inet_network(str
)) != INADDR_NONE
||
1119 ((np
= getnetbyname(str
)) != NULL
&& (val
= np
->n_net
) != 0))) {
1121 inet_makenetandmask(val
, &su
->sin
, 0);
1124 hp
= gethostbyname(str
);
1127 su
->sin
.sin_family
= hp
->h_addrtype
;
1128 memmove((char *)&su
->sin
.sin_addr
, hp
->h_addr
,
1129 MIN((size_t)hp
->h_length
, sizeof(su
->sin
.sin_addr
)));
1132 errx(EX_NOHOST
, "bad address: %s", str
);
1136 prefixlen(const char *len_str
)
1138 int len
= atoi(len_str
), q
, r
;
1142 rtm_addrs
|= RTA_NETMASK
;
1147 p
= (char *)&so_mask
.sin6
.sin6_addr
;
1152 p
= (char *)&so_mask
.sin
.sin_addr
;
1155 fprintf(stderr
, "prefixlen not supported in this af\n");
1160 if (len
< 0 || max
< len
) {
1161 fprintf(stderr
, "%s: bad value\n", len_str
);
1167 so_mask
.sa
.sa_family
= af
;
1168 so_mask
.sa
.sa_len
= aflen
;
1169 memset((void *)p
, 0, max
/ 8);
1171 memset((void *)p
, 0xff, q
);
1173 *((u_char
*)p
+ q
) = (0xff00 >> r
) & 0xff;
1181 short ns_nullh
[] = {0,0,0};
1184 ns_print(struct sockaddr_ns
*sns
)
1186 struct ns_addr work
;
1187 union { union ns_net net_e
; u_long long_e
; } net
;
1189 static char mybuf
[50+MAXHOSTNAMELEN
], cport
[10], chost
[25];
1190 const char *host
= "";
1194 work
= sns
->sns_addr
;
1195 port
= ntohs(work
.x_port
);
1197 net
.net_e
= work
.x_net
;
1198 if (ns_nullhost(work
) && net
.long_e
== 0) {
1200 strncpy(mybuf
, "*.*", sizeof(mybuf
));
1202 sprintf(mybuf
, "*.%XH", port
);
1206 if (memcmp(ns_bh
, work
.x_host
.c_host
, 6) == 0)
1208 else if (memcmp(ns_nullh
, work
.x_host
.c_host
, 6) == 0)
1211 q
= work
.x_host
.c_host
;
1212 sprintf(chost
, "%02X%02X%02X%02X%02X%02XH",
1213 q
[0], q
[1], q
[2], q
[3], q
[4], q
[5]);
1214 for (p
= chost
; *p
== '0' && p
< chost
+ 12; p
++)
1219 sprintf(cport
, ".%XH", htons(port
));
1223 snprintf(mybuf
, sizeof(mybuf
), "%lxH.%s%s",
1224 (unsigned long)ntohl(net
.long_e
),
1235 char *buf
, *lim
, *next
;
1236 struct rt_msghdr
*rtm
;
1240 mib
[2] = 0; /* protocol */
1241 mib
[3] = 0; /* wildcard address family */
1242 mib
[4] = NET_RT_IFLIST
;
1243 mib
[5] = 0; /* no flags */
1244 if (sysctl(mib
, 6, NULL
, &needed
, NULL
, 0) < 0)
1245 err(EX_OSERR
, "route-sysctl-estimate");
1246 if ((buf
= malloc(needed
)) == NULL
)
1247 errx(EX_OSERR
, "malloc failed");
1248 if (sysctl(mib
, 6, buf
, &needed
, NULL
, 0) < 0)
1249 err(EX_OSERR
, "actual retrieval of interface table");
1251 for (next
= buf
; next
< lim
; next
+= rtm
->rtm_msglen
) {
1252 rtm
= (struct rt_msghdr
*)next
;
1253 print_rtmsg(rtm
, rtm
->rtm_msglen
);
1270 n
= read(s
, msg
, 2048);
1272 printf("\ngot message of size %d on %s", n
, ctime(&now
));
1273 print_rtmsg((struct rt_msghdr
*)msg
, n
);
1278 struct rt_msghdr m_rtm
;
1283 rtmsg(int cmd
, int flags
)
1287 char *cp
= m_rtmsg
.m_space
;
1290 #define NEXTADDR(w, u) \
1291 if (rtm_addrs & (w)) {\
1292 l = ROUNDUP(u.sa.sa_len); memmove(cp, &(u), l); cp += l;\
1293 if (verbose) sodump(&(u),"u");\
1297 memset(&m_rtmsg
, 0, sizeof(m_rtmsg
));
1300 else if (cmd
== 'c')
1302 else if (cmd
== 'g') {
1304 if (so_ifp
.sa
.sa_family
== 0) {
1305 so_ifp
.sa
.sa_family
= AF_LINK
;
1306 so_ifp
.sa
.sa_len
= sizeof(struct sockaddr_dl
);
1307 rtm_addrs
|= RTA_IFP
;
1311 #define rtm m_rtmsg.m_rtm
1313 rtm
.rtm_flags
= flags
;
1314 rtm
.rtm_version
= RTM_VERSION
;
1315 rtm
.rtm_seq
= ++seq
;
1316 rtm
.rtm_addrs
= rtm_addrs
;
1317 rtm
.rtm_rmx
= rt_metrics
;
1318 rtm
.rtm_inits
= rtm_inits
;
1320 if (rtm_addrs
& RTA_NETMASK
)
1322 NEXTADDR(RTA_DST
, so_dst
);
1323 NEXTADDR(RTA_GATEWAY
, so_gate
);
1324 NEXTADDR(RTA_NETMASK
, so_mask
);
1325 NEXTADDR(RTA_GENMASK
, so_genmask
);
1326 NEXTADDR(RTA_IFP
, so_ifp
);
1327 NEXTADDR(RTA_IFA
, so_ifa
);
1328 rtm
.rtm_msglen
= l
= cp
- (char *)&m_rtmsg
;
1330 print_rtmsg(&rtm
, l
);
1333 if ((rlen
= write(s
, (char *)&m_rtmsg
, l
)) < 0) {
1334 warn("writing to routing socket");
1337 if (cmd
== RTM_GET
) {
1339 l
= read(s
, (char *)&m_rtmsg
, sizeof(m_rtmsg
));
1340 } while (l
> 0 && (rtm
.rtm_seq
!= seq
|| rtm
.rtm_pid
!= pid
));
1342 warn("read from routing socket");
1344 print_getmsg(&rtm
, l
);
1353 int olen
= so_mask
.sa
.sa_len
;
1354 char *cp1
= olen
+ (char *)&so_mask
, *cp2
;
1356 for (so_mask
.sa
.sa_len
= 0; cp1
> (char *)&so_mask
; )
1358 so_mask
.sa
.sa_len
= 1 + cp1
- (char *)&so_mask
;
1361 if ((rtm_addrs
& RTA_DST
) == 0)
1363 switch (so_dst
.sa
.sa_family
) {
1375 cp1
= so_mask
.sa
.sa_len
+ 1 + (char *)&so_dst
;
1376 cp2
= so_dst
.sa
.sa_len
+ 1 + (char *)&so_dst
;
1379 cp2
= so_mask
.sa
.sa_len
+ 1 + (char *)&so_mask
;
1380 while (cp1
> so_dst
.sa
.sa_data
)
1384 const char *msgtypes
[] = {
1386 "RTM_ADD: Add Route",
1387 "RTM_DELETE: Delete Route",
1388 "RTM_CHANGE: Change Metrics or flags",
1389 "RTM_GET: Report Metrics",
1390 "RTM_LOSING: Kernel Suspects Partitioning",
1391 "RTM_REDIRECT: Told to use different route",
1392 "RTM_MISS: Lookup failed on this address",
1393 "RTM_LOCK: fix specified metrics",
1394 "RTM_OLDADD: caused by SIOCADDRT",
1395 "RTM_OLDDEL: caused by SIOCDELRT",
1396 "RTM_RESOLVE: Route created by cloning",
1397 "RTM_NEWADDR: address being added to iface",
1398 "RTM_DELADDR: address being removed from iface",
1399 "RTM_IFINFO: iface status change",
1400 "RTM_NEWMADDR: new multicast group membership on iface",
1401 "RTM_DELMADDR: multicast group membership removed from iface",
1402 "RTM_IFANNOUNCE: interface arrival/departure",
1406 char metricnames
[] =
1407 "\011pksent\010rttvar\7rtt\6ssthresh\5sendpipe\4recvpipe\3expire\2hopcount"
1410 "\1UP\2GATEWAY\3HOST\4REJECT\5DYNAMIC\6MODIFIED\7DONE\010MASK_PRESENT"
1411 "\011CLONING\012XRESOLVE\013LLINFO\014STATIC\015BLACKHOLE\016b016"
1412 "\017PROTO2\020PROTO1\021PRCLONING\022WASCLONED\023PROTO3\024CHAINDELETE"
1413 "\025PINNED\026LOCAL\027BROADCAST\030MULTICAST";
1415 "\1UP\2BROADCAST\3DEBUG\4LOOPBACK\5PTP\6b6\7RUNNING\010NOARP"
1416 "\011PPROMISC\012ALLMULTI\013OACTIVE\014SIMPLEX\015LINK0\016LINK1"
1417 "\017LINK2\020MULTICAST";
1419 "\1DST\2GATEWAY\3NETMASK\4GENMASK\5IFP\6IFA\7AUTHOR\010BRD";
1422 print_rtmsg(struct rt_msghdr
*rtm
, int msglen __unused
)
1424 struct if_msghdr
*ifm
;
1425 struct ifa_msghdr
*ifam
;
1427 struct ifma_msghdr
*ifmam
;
1429 struct if_announcemsghdr
*ifan
;
1433 if (rtm
->rtm_version
!= RTM_VERSION
) {
1434 printf("routing message version %d not understood\n",
1438 if (msgtypes
[rtm
->rtm_type
] != NULL
)
1439 printf("%s: ", msgtypes
[rtm
->rtm_type
]);
1441 printf("#%d: ", rtm
->rtm_type
);
1442 printf("len %d, ", rtm
->rtm_msglen
);
1443 switch (rtm
->rtm_type
) {
1445 ifm
= (struct if_msghdr
*)rtm
;
1446 printf("if# %d, flags:", ifm
->ifm_index
);
1447 bprintf(stdout
, ifm
->ifm_flags
, ifnetflags
);
1448 pmsg_addrs((char *)(ifm
+ 1), ifm
->ifm_addrs
);
1452 ifam
= (struct ifa_msghdr
*)rtm
;
1453 printf("metric %d, flags:", ifam
->ifam_metric
);
1454 bprintf(stdout
, ifam
->ifam_flags
, routeflags
);
1455 pmsg_addrs((char *)(ifam
+ 1), ifam
->ifam_addrs
);
1460 ifmam
= (struct ifma_msghdr
*)rtm
;
1461 pmsg_addrs((char *)(ifmam
+ 1), ifmam
->ifmam_addrs
);
1464 case RTM_IFANNOUNCE
:
1465 ifan
= (struct if_announcemsghdr
*)rtm
;
1466 printf("if# %d, what: ", ifan
->ifan_index
);
1467 switch (ifan
->ifan_what
) {
1471 case IFAN_DEPARTURE
:
1472 printf("departure");
1475 printf("#%d", ifan
->ifan_what
);
1482 printf("pid: %ld, seq %d, errno %d, flags:",
1483 (long)rtm
->rtm_pid
, rtm
->rtm_seq
, rtm
->rtm_errno
);
1484 bprintf(stdout
, rtm
->rtm_flags
, routeflags
);
1490 print_getmsg(struct rt_msghdr
*rtm
, int msglen
)
1492 struct sockaddr
*dst
= NULL
, *gate
= NULL
, *mask
= NULL
;
1493 struct sockaddr_dl
*ifp
= NULL
;
1494 struct sockaddr
*sa
;
1498 printf(" route to: %s\n", routename(&so_dst
.sa
));
1499 if (rtm
->rtm_version
!= RTM_VERSION
) {
1500 warnx("routing message version %d not understood",
1504 if (rtm
->rtm_msglen
> msglen
) {
1505 warnx("message length mismatch, in packet %d, returned %d",
1506 rtm
->rtm_msglen
, msglen
);
1508 if (rtm
->rtm_errno
!= 0) {
1509 errno
= rtm
->rtm_errno
;
1510 warn("message indicates error %d", errno
);
1513 cp
= ((char *)(rtm
+ 1));
1515 for (i
= 1; i
!= 0; i
<<= 1)
1516 if (i
& rtm
->rtm_addrs
) {
1517 sa
= (struct sockaddr
*)cp
;
1529 if (sa
->sa_family
== AF_LINK
&&
1530 ((struct sockaddr_dl
*)sa
)->sdl_nlen
)
1531 ifp
= (struct sockaddr_dl
*)sa
;
1536 if (dst
!= NULL
&& mask
!= NULL
)
1537 mask
->sa_family
= dst
->sa_family
; /* XXX */
1539 printf("destination: %s\n", routename(dst
));
1541 int savenflag
= nflag
;
1544 printf(" mask: %s\n", routename(mask
));
1547 if (gate
!= NULL
&& rtm
->rtm_flags
& RTF_GATEWAY
)
1548 printf(" gateway: %s\n", routename(gate
));
1550 printf(" interface: %.*s\n",
1551 ifp
->sdl_nlen
, ifp
->sdl_data
);
1553 bprintf(stdout
, rtm
->rtm_flags
, routeflags
);
1555 #define lock(f) ((rtm->rtm_rmx.rmx_locks & __CONCAT(RTV_,f)) ? 'L' : ' ')
1556 #define msec(u) (((u) + 500) / 1000) /* usec to msec */
1559 recvpipe sendpipe ssthresh rtt,msec rttvar hopcount mtu expire");
1560 printf("%8ld%c ", rtm
->rtm_rmx
.rmx_recvpipe
, lock(RPIPE
));
1561 printf("%8ld%c ", rtm
->rtm_rmx
.rmx_sendpipe
, lock(SPIPE
));
1562 printf("%8ld%c ", rtm
->rtm_rmx
.rmx_ssthresh
, lock(SSTHRESH
));
1563 printf("%8ld%c ", msec(rtm
->rtm_rmx
.rmx_rtt
), lock(RTT
));
1564 printf("%8ld%c ", msec(rtm
->rtm_rmx
.rmx_rttvar
), lock(RTTVAR
));
1565 printf("%8ld%c ", rtm
->rtm_rmx
.rmx_hopcount
, lock(HOPCOUNT
));
1566 printf("%8ld%c ", rtm
->rtm_rmx
.rmx_mtu
, lock(MTU
));
1567 if (rtm
->rtm_rmx
.rmx_expire
!= 0)
1568 rtm
->rtm_rmx
.rmx_expire
-= time(0);
1569 printf("%8ld%c\n", rtm
->rtm_rmx
.rmx_expire
, lock(EXPIRE
));
1572 #define RTA_IGN (RTA_DST|RTA_GATEWAY|RTA_NETMASK|RTA_IFP|RTA_IFA|RTA_BRD)
1575 else if (rtm
->rtm_addrs
&~ RTA_IGN
) {
1576 printf("sockaddrs: ");
1577 bprintf(stdout
, rtm
->rtm_addrs
, addrnames
);
1584 pmsg_common(struct rt_msghdr
*rtm
)
1586 printf("\nlocks: ");
1587 bprintf(stdout
, rtm
->rtm_rmx
.rmx_locks
, metricnames
);
1589 bprintf(stdout
, rtm
->rtm_inits
, metricnames
);
1590 pmsg_addrs(((char *)(rtm
+ 1)), rtm
->rtm_addrs
);
1594 pmsg_addrs(char *cp
, int addrs
)
1596 struct sockaddr
*sa
;
1603 printf("\nsockaddrs: ");
1604 bprintf(stdout
, addrs
, addrnames
);
1606 for (i
= 1; i
!= 0; i
<<= 1)
1608 sa
= (struct sockaddr
*)cp
;
1609 printf(" %s", routename(sa
));
1617 bprintf(FILE *fp
, int b
, u_char
*str
)
1624 while ((i
= *str
++) != 0) {
1625 if (b
& (1 << (i
-1))) {
1632 for (; (i
= *str
) > 32; str
++)
1643 keyword(const char *cp
)
1645 struct keytab
*kt
= keywords
;
1647 while (kt
->kt_cp
!= NULL
&& strcmp(kt
->kt_cp
, cp
) != 0)
1653 sodump(sup su
, const char *which
)
1655 switch (su
->sa
.sa_family
) {
1657 printf("%s: link %s; ",
1658 which
, link_ntoa(&su
->sdl
));
1661 printf("%s: inet %s; ",
1662 which
, inet_ntoa(su
->sin
.sin_addr
));
1665 printf("%s: atalk %s; ",
1666 which
, atalk_ntoa(su
->sat
.sat_addr
));
1670 printf("%s: xns %s; ",
1671 which
, ns_ntoa(su
->sns
.sns_addr
));
1688 sockaddr(char *addr
, struct sockaddr
*sa
)
1690 char *cp
= (char *)sa
;
1691 int size
= sa
->sa_len
;
1692 char *cplim
= cp
+ size
;
1693 int byte
= 0, state
= VIRGIN
, new = 0 /* foil gcc */;
1695 memset(cp
, 0, size
);
1698 if ((*addr
>= '0') && (*addr
<= '9')) {
1700 } else if ((*addr
>= 'a') && (*addr
<= 'f')) {
1701 new = *addr
- 'a' + 10;
1702 } else if ((*addr
>= 'A') && (*addr
<= 'F')) {
1703 new = *addr
- 'A' + 10;
1704 } else if (*addr
== '\0')
1709 switch (state
/* | INPUT */) {
1710 case GOTTWO
| DIGIT
:
1711 *cp
++ = byte
; /*FALLTHROUGH*/
1712 case VIRGIN
| DIGIT
:
1713 state
= GOTONE
; byte
= new; continue;
1714 case GOTONE
| DIGIT
:
1715 state
= GOTTWO
; byte
= new + (byte
<< 4); continue;
1716 default: /* | DELIM */
1717 state
= VIRGIN
; *cp
++ = byte
; byte
= 0; continue;
1720 *cp
++ = byte
; /* FALLTHROUGH */
1725 } while (cp
< cplim
);
1726 sa
->sa_len
= cp
- (char *)sa
;
1730 atalk_aton(const char *text
, struct at_addr
*addr
)
1734 if (sscanf(text
, "%u.%u", &net
, &node
) != 2
1735 || net
> 0xffff || node
> 0xff)
1737 addr
->s_net
= htons(net
);
1738 addr
->s_node
= node
;
1743 atalk_ntoa(struct at_addr at
)
1745 static char buf
[20];
1747 snprintf(buf
, sizeof(buf
), "%u.%u", ntohs(at
.s_net
), at
.s_node
);