]>
git.saurik.com Git - apple/network_cmds.git/blob - route.tproj/route.c
c43f159bf73776465be62ced55b5d37233062bba
2 * Copyright (c) 2008 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 * Copyright (c) 1983, 1989, 1991, 1993
30 * The Regents of the University of California. All rights reserved.
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
35 * 1. Redistributions of source code must retain the above copyright
36 * notice, this list of conditions and the following disclaimer.
37 * 2. Redistributions in binary form must reproduce the above copyright
38 * notice, this list of conditions and the following disclaimer in the
39 * documentation and/or other materials provided with the distribution.
40 * 3. All advertising materials mentioning features or use of this software
41 * must display the following acknowledgement:
42 * This product includes software developed by the University of
43 * California, Berkeley and its contributors.
44 * 4. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61 #include <sys/cdefs.h>
63 __unused
static const char copyright
[] =
64 "@(#) Copyright (c) 1983, 1989, 1991, 1993\n\
65 The Regents of the University of California. All rights reserved.\n";
70 static char sccsid
[] = "@(#)route.c 8.3 (Berkeley) 3/19/94";
72 __unused
static const char rcsid
[] =
73 "$Id: route.c,v 1.4 2006/02/07 06:22:29 lindak Exp $";
76 #include <sys/param.h>
78 #include <sys/socket.h>
79 #include <sys/ioctl.h>
80 #include <sys/sysctl.h>
81 #include <sys/types.h>
84 #include <net/route.h>
85 #include <net/if_dl.h>
86 #include <netinet/in.h>
87 #include <arpa/inet.h>
105 #include "keywords.h"
111 struct sockaddr_in sin
;
113 struct sockaddr_in6 sin6
;
115 struct sockaddr_dl sdl
;
116 struct sockaddr_storage ss
; /* added to avoid memory overrun */
117 } so_dst
, so_gate
, so_mask
, so_genmask
, so_ifa
, so_ifp
;
119 typedef union sockunion
*sup
;
120 int pid
, rtm_addrs
, uid
;
122 int forcehost
, forcenet
, doflush
, nflag
, af
, qflag
, tflag
, keyword();
123 int iflag
, verbose
, aflen
= sizeof (struct sockaddr_in
);
124 int locking
, lockrest
, debugonly
;
125 struct rt_metrics rt_metrics
;
127 unsigned int ifscope
;
128 const char *routename(), *netname();
129 void flushroutes(), newroute(), monitor(), sockaddr(), sodump(), bprintf();
130 void print_getmsg(), print_rtmsg(), pmsg_common(), pmsg_addrs(), mask_addr();
131 int getaddr(), rtmsg(), x25_makemask();
133 extern char *iso_ntoa();
135 void usage
__P((const char *)) __dead2
;
142 warnx("bad keyword: %s", cp
);
143 (void) fprintf(stderr
,
144 "usage: route [-dnqtv] command [[modifiers] args]\n");
150 ((a) > 0 ? (1 + (((a) - 1) | (sizeof(uint32_t) - 1))) : sizeof(uint32_t))
151 #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len))
163 while ((ch
= getopt(argc
, argv
, "nqdtv")) != -1)
190 s
= open(_PATH_DEVNULL
, O_WRONLY
, 0);
192 s
= socket(PF_ROUTE
, SOCK_RAW
, 0);
194 err(EX_OSERR
, "socket");
197 switch (keyword(*argv
)) {
205 newroute(argc
, argv
);
214 flushroutes(argc
, argv
);
223 * Purge all entries in the routing tables not
224 * associated with network interfaces.
227 flushroutes(argc
, argv
)
232 int mib
[6], rlen
, seqno
;
233 char *buf
, *next
, *lim
;
234 register struct rt_msghdr
*rtm
;
237 errx(EX_NOPERM
, "must be root to alter routing table");
239 shutdown(s
, 0); /* Don't want to read back our messages */
242 if (argc
== 2 && **argv
== '-')
243 switch (keyword(*argv
+ 1)) {
262 mib
[2] = 0; /* protocol */
263 mib
[3] = 0; /* wildcard address family */
264 mib
[4] = NET_RT_DUMP
;
265 mib
[5] = 0; /* no flags */
266 if (sysctl(mib
, 6, NULL
, &needed
, NULL
, 0) < 0)
267 err(EX_OSERR
, "route-sysctl-estimate");
268 if ((buf
= malloc(needed
)) == NULL
)
269 errx(EX_OSERR
, "malloc failed");
270 if (sysctl(mib
, 6, buf
, &needed
, NULL
, 0) < 0)
271 err(EX_OSERR
, "route-sysctl-get");
274 (void) printf("Examining routing table from sysctl\n");
276 for (next
= buf
; next
< lim
; next
+= rtm
->rtm_msglen
) {
277 rtm
= (struct rt_msghdr
*)next
;
279 print_rtmsg(rtm
, rtm
->rtm_msglen
);
280 if ((rtm
->rtm_flags
& RTF_GATEWAY
) == 0)
283 struct sockaddr
*sa
= (struct sockaddr
*)(rtm
+ 1);
285 if (sa
->sa_family
!= af
)
290 rtm
->rtm_type
= RTM_DELETE
;
291 rtm
->rtm_seq
= seqno
;
292 rlen
= write(s
, next
, rtm
->rtm_msglen
);
293 if (rlen
< (int)rtm
->rtm_msglen
) {
294 warn("write to routing socket");
295 (void) printf("got only %d for rlen\n", rlen
);
302 print_rtmsg(rtm
, rlen
);
304 struct sockaddr
*sa
= (struct sockaddr
*)(rtm
+ 1);
305 (void) printf("%-20.20s ", rtm
->rtm_flags
& RTF_HOST
?
306 routename(sa
) : netname(sa
));
307 sa
= (struct sockaddr
*)(ROUNDUP(sa
->sa_len
) + (char *)sa
);
308 (void) printf("%-20.20s ", routename(sa
));
309 (void) printf("done\n");
319 static char line
[MAXHOSTNAMELEN
+ 1];
321 static char domain
[MAXHOSTNAMELEN
+ 1];
322 static int first
= 1;
326 if (gethostname(domain
, MAXHOSTNAMELEN
) == 0 &&
327 (cp
= index(domain
, '.'))) {
328 domain
[MAXHOSTNAMELEN
] = '\0';
329 (void) strcpy(domain
, cp
+ 1);
335 strcpy(line
, "default");
336 else switch (sa
->sa_family
) {
340 in
= ((struct sockaddr_in
*)sa
)->sin_addr
;
343 if (in
.s_addr
== INADDR_ANY
|| sa
->sa_len
< 4)
345 if (cp
== 0 && !nflag
) {
346 hp
= gethostbyaddr((char *)&in
, sizeof (struct in_addr
),
349 if ((cp
= index(hp
->h_name
, '.')) &&
350 !strcmp(cp
+ 1, domain
))
356 strncpy(line
, cp
, sizeof(line
) - 1);
357 line
[sizeof(line
) - 1] = '\0';
359 /* XXX - why not inet_ntoa()? */
360 #define C(x) (unsigned)((x) & 0xff)
361 in
.s_addr
= ntohl(in
.s_addr
);
362 (void) sprintf(line
, "%u.%u.%u.%u", C(in
.s_addr
>> 24),
363 C(in
.s_addr
>> 16), C(in
.s_addr
>> 8), C(in
.s_addr
));
371 struct sockaddr_in6 sin6
; /* use static var for safety */
373 #ifdef NI_WITHSCOPEID
374 niflags
= NI_WITHSCOPEID
;
377 memset(&sin6
, 0, sizeof(sin6
));
378 memcpy(&sin6
, sa
, sa
->sa_len
);
379 sin6
.sin6_len
= sizeof(struct sockaddr_in6
);
380 sin6
.sin6_family
= AF_INET6
;
382 if (sa
->sa_len
== sizeof(struct sockaddr_in6
) &&
383 (IN6_IS_ADDR_LINKLOCAL(&sin6
.sin6_addr
) ||
384 IN6_IS_ADDR_MC_LINKLOCAL(&sin6
.sin6_addr
)) &&
385 sin6
.sin6_scope_id
== 0) {
387 ntohs(*(u_int16_t
*)&sin6
.sin6_addr
.s6_addr
[2]);
388 sin6
.sin6_addr
.s6_addr
[2] = 0;
389 sin6
.sin6_addr
.s6_addr
[3] = 0;
393 niflags
|= NI_NUMERICHOST
;
394 if (getnameinfo((struct sockaddr
*)&sin6
, sin6
.sin6_len
,
395 line
, sizeof(line
), NULL
, 0, niflags
) != 0)
396 strncpy(line
, "invalid", sizeof(line
));
403 return (link_ntoa((struct sockaddr_dl
*)sa
));
406 { u_short
*s
= (u_short
*)sa
;
407 u_short
*slim
= s
+ ((sa
->sa_len
+ 1) >> 1);
408 char *cp
= line
+ sprintf(line
, "(%d)", sa
->sa_family
);
409 char *cpe
= line
+ sizeof(line
);
411 while (++s
< slim
&& cp
< cpe
) /* start with sa->sa_data */
412 cp
+= snprintf(cp
, cpe
- cp
, " %x", *s
);
420 * Return the name of the network whose address is given.
421 * The address is assumed to be that of a net or subnet, not a host.
428 static char line
[MAXHOSTNAMELEN
+ 1];
429 struct netent
*np
= 0;
431 register in_addr_t i
;
434 switch (sa
->sa_family
) {
438 in
= ((struct sockaddr_in
*)sa
)->sin_addr
;
440 i
= in
.s_addr
= ntohl(in
.s_addr
);
445 mask
= IN_CLASSA_NET
;
447 } else if (IN_CLASSB(i
)) {
448 mask
= IN_CLASSB_NET
;
451 mask
= IN_CLASSC_NET
;
455 * If there are more bits than the standard mask
456 * would suggest, subnets must be in use.
457 * Guess at the subnet mask, assuming reasonable
458 * width subnet fields.
460 while (in
.s_addr
&~ mask
)
461 mask
= mask
>> subnetshift
;
462 net
= in
.s_addr
& mask
;
463 while ((mask
& 1) == 0)
464 mask
>>= 1, net
>>= 1;
465 np
= getnetbyaddr(net
, AF_INET
);
470 strncpy(line
, cp
, sizeof(line
));
471 else if ((in
.s_addr
& 0xffffff) == 0)
472 (void) sprintf(line
, "%u", C(in
.s_addr
>> 24));
473 else if ((in
.s_addr
& 0xffff) == 0)
474 (void) sprintf(line
, "%u.%u", C(in
.s_addr
>> 24),
476 else if ((in
.s_addr
& 0xff) == 0)
477 (void) sprintf(line
, "%u.%u.%u", C(in
.s_addr
>> 24),
478 C(in
.s_addr
>> 16), C(in
.s_addr
>> 8));
480 (void) sprintf(line
, "%u.%u.%u.%u", C(in
.s_addr
>> 24),
481 C(in
.s_addr
>> 16), C(in
.s_addr
>> 8),
489 struct sockaddr_in6 sin6
; /* use static var for safety */
491 #ifdef NI_WITHSCOPEID
492 niflags
= NI_WITHSCOPEID
;
495 memset(&sin6
, 0, sizeof(sin6
));
496 memcpy(&sin6
, sa
, sa
->sa_len
);
497 sin6
.sin6_len
= sizeof(struct sockaddr_in6
);
498 sin6
.sin6_family
= AF_INET6
;
500 if (sa
->sa_len
== sizeof(struct sockaddr_in6
) &&
501 (IN6_IS_ADDR_LINKLOCAL(&sin6
.sin6_addr
) ||
502 IN6_IS_ADDR_MC_LINKLOCAL(&sin6
.sin6_addr
)) &&
503 sin6
.sin6_scope_id
== 0) {
505 ntohs(*(u_int16_t
*)&sin6
.sin6_addr
.s6_addr
[2]);
506 sin6
.sin6_addr
.s6_addr
[2] = 0;
507 sin6
.sin6_addr
.s6_addr
[3] = 0;
511 niflags
|= NI_NUMERICHOST
;
512 if (getnameinfo((struct sockaddr
*)&sin6
, sin6
.sin6_len
,
513 line
, sizeof(line
), NULL
, 0, niflags
) != 0)
514 strncpy(line
, "invalid", sizeof(line
));
521 return (link_ntoa((struct sockaddr_dl
*)sa
));
525 { u_short
*s
= (u_short
*)sa
->sa_data
;
526 u_short
*slim
= s
+ ((sa
->sa_len
+ 1)>>1);
527 char *cp
= line
+ sprintf(line
, "af %d:", sa
->sa_family
);
528 char *cpe
= line
+ sizeof(line
);
530 while (s
< slim
&& cp
< cpe
)
531 cp
+= snprintf(cp
, cpe
- cp
, " %x", *s
++);
539 set_metric(value
, key
)
544 u_int noval
, *valp
= &noval
;
547 #define caseof(x, y, z) case x: valp = (u_int *)&rt_metrics.z; flag = y; break
548 caseof(K_MTU
, RTV_MTU
, rmx_mtu
);
549 caseof(K_HOPCOUNT
, RTV_HOPCOUNT
, rmx_hopcount
);
550 caseof(K_EXPIRE
, RTV_EXPIRE
, rmx_expire
);
551 caseof(K_RECVPIPE
, RTV_RPIPE
, rmx_recvpipe
);
552 caseof(K_SENDPIPE
, RTV_SPIPE
, rmx_sendpipe
);
553 caseof(K_SSTHRESH
, RTV_SSTHRESH
, rmx_ssthresh
);
554 caseof(K_RTT
, RTV_RTT
, rmx_rtt
);
555 caseof(K_RTTVAR
, RTV_RTTVAR
, rmx_rttvar
);
558 if (lockrest
|| locking
)
559 rt_metrics
.rmx_locks
|= flag
;
568 register char **argv
;
570 char *cmd
, *dest
= "", *gateway
= "", *err
;
571 int ishost
= 0, ret
, attempts
, oerrno
, flags
= RTF_STATIC
;
573 struct hostent
*hp
= 0;
576 errx(EX_NOPERM
, "must be root to alter routing table");
580 shutdown(s
, 0); /* Don't want to read back our messages */
582 if (**(++argv
)== '-') {
583 switch (key
= keyword(1 + *argv
)) {
586 aflen
= sizeof(struct sockaddr_dl
);
590 aflen
= sizeof(struct sockaddr_in
);
595 aflen
= sizeof(struct sockaddr_in6
);
600 aflen
= sizeof(union sockunion
);
607 flags
&= ~RTF_STATIC
;
625 flags
|= RTF_BLACKHOLE
;
634 flags
|= RTF_CLONING
;
637 flags
|= RTF_XRESOLVE
;
645 (void) getaddr(RTA_IFA
, *++argv
, 0);
650 (void) getaddr(RTA_IFP
, *++argv
, 0);
655 (void) getaddr(RTA_GENMASK
, *++argv
, 0);
660 (void) getaddr(RTA_GATEWAY
, *++argv
, 0);
665 ishost
= getaddr(RTA_DST
, *++argv
, &hp
);
671 (void) getaddr(RTA_NETMASK
, *++argv
, 0);
679 if (prefixlen(*++argv
) == -1) {
697 set_metric(*++argv
, key
);
702 if ((ifscope
= if_nametoindex(*++argv
)) != 0)
703 flags
|= RTF_IFSCOPE
;
705 errx(1, "bad interface name");
711 if ((rtm_addrs
& RTA_DST
) == 0) {
713 ishost
= getaddr(RTA_DST
, *argv
, &hp
);
714 } else if ((rtm_addrs
& RTA_GATEWAY
) == 0) {
716 (void) getaddr(RTA_GATEWAY
, *argv
, &hp
);
718 (void) getaddr(RTA_NETMASK
, *argv
, 0);
725 if (af
== AF_INET6
) {
726 rtm_addrs
&= ~RTA_NETMASK
;
727 memset((void *)&so_mask
, 0, sizeof(so_mask
));
737 flags
|= RTF_GATEWAY
;
738 if (so_mask
.sin
.sin_family
== AF_INET
) {
739 // make sure the mask is contiguous
741 for (i
= 0; i
< 32; i
++)
742 if (((so_mask
.sin
.sin_addr
.s_addr
) & ntohl((1 << i
))) != 0)
745 if (((so_mask
.sin
.sin_addr
.s_addr
) & ntohl((1 << i
))) == 0)
746 errx(EX_NOHOST
, "invalid mask: %s", inet_ntoa(so_mask
.sin
.sin_addr
));
748 for (attempts
= 1; ; attempts
++) {
750 if ((ret
= rtmsg(*cmd
, flags
)) == 0)
752 if (errno
!= ENETUNREACH
&& errno
!= ESRCH
)
754 if (af
== AF_INET
&& *gateway
&& hp
&& hp
->h_addr_list
[1]) {
756 bcopy(hp
->h_addr_list
[0], &so_gate
.sin
.sin_addr
,
757 MIN(hp
->h_length
, sizeof(so_gate
.sin
.sin_addr
)));
764 (void) printf("%s %s %s", cmd
, ishost
? "host" : "net", dest
);
766 (void) printf(": gateway %s", gateway
);
767 if (attempts
> 1 && ret
== 0 && af
== AF_INET
)
768 (void) printf(" (%s)", inet_ntoa(so_gate
.sin
.sin_addr
));
775 err
= "not in table";
778 err
= "entry in use";
781 err
= "routing table overflow";
784 err
= strerror(oerrno
);
787 (void) printf(": %s\n", err
);
792 inet_makenetandmask(net
, sin
, bits
)
794 register struct sockaddr_in
*sin
;
796 in_addr_t addr
, mask
= 0;
799 rtm_addrs
|= RTA_NETMASK
;
802 mask
= 0xffffffff << (32 - bits
);
805 else if (net
< 128) {
806 addr
= net
<< IN_CLASSA_NSHIFT
;
807 mask
= IN_CLASSA_NET
;
808 } else if (net
< 65536) {
809 addr
= net
<< IN_CLASSB_NSHIFT
;
810 mask
= IN_CLASSB_NET
;
811 } else if (net
< 16777216L) {
812 addr
= net
<< IN_CLASSC_NSHIFT
;
813 mask
= IN_CLASSC_NET
;
816 if ((addr
& IN_CLASSA_HOST
) == 0)
817 mask
= IN_CLASSA_NET
;
818 else if ((addr
& IN_CLASSB_HOST
) == 0)
819 mask
= IN_CLASSB_NET
;
820 else if ((addr
& IN_CLASSC_HOST
) == 0)
821 mask
= IN_CLASSC_NET
;
825 sin
->sin_addr
.s_addr
= htonl(addr
);
827 sin
->sin_addr
.s_addr
= htonl(mask
);
830 cp
= (char *)(&sin
->sin_addr
+ 1);
831 while (*--cp
== 0 && cp
> (char *)sin
)
833 sin
->sin_len
= 1 + cp
- (char *)sin
;
837 * Interpret an argument as a network address of some kind,
838 * returning 1 if a host address, 0 if a network address.
841 getaddr(which
, s
, hpp
)
844 struct hostent
**hpp
;
846 register sup su
= NULL
;
851 int afamily
; /* local copy of af so we can change it */
855 aflen
= sizeof(struct sockaddr_in
);
866 struct ifaddrs
*ifap
, *ifa
;
867 struct sockaddr_dl
*sdl
= NULL
;
869 if (getifaddrs(&ifap
))
870 err(1, "getifaddrs");
872 for (ifa
= ifap
; ifa
; ifa
= ifa
->ifa_next
) {
873 if (ifa
->ifa_addr
->sa_family
!= AF_LINK
)
876 if (strcmp(s
, ifa
->ifa_name
))
879 sdl
= (struct sockaddr_dl
*)ifa
->ifa_addr
;
881 /* If we found it, then use it */
884 * Copy is safe since we have a
885 * sockaddr_storage member in sockunion{}.
886 * Note that we need to copy before calling
889 memcpy(&su
->sdl
, sdl
, sdl
->sdl_len
);
910 usage("internal error");
913 su
->sa
.sa_len
= aflen
;
914 su
->sa
.sa_family
= afamily
; /* cases that don't want it have left already */
915 if (strcmp(s
, "default") == 0) {
917 * Default is net 0.0.0.0/0
922 /* bzero(su, sizeof(*su)); *//* for readability */
923 (void) getaddr(RTA_NETMASK
, s
, 0);
927 /* bzero(su, sizeof(*su)); *//* for readability */
936 struct addrinfo hints
, *res
;
938 memset(&hints
, 0, sizeof(hints
));
939 hints
.ai_family
= afamily
; /*AF_INET6*/
940 hints
.ai_flags
= AI_NUMERICHOST
;
941 hints
.ai_socktype
= SOCK_DGRAM
; /*dummy*/
942 if (getaddrinfo(s
, "0", &hints
, &res
) != 0 ||
943 res
->ai_family
!= AF_INET6
||
944 res
->ai_addrlen
!= sizeof(su
->sin6
)) {
945 (void) fprintf(stderr
, "%s: bad value\n", s
);
948 memcpy(&su
->sin6
, res
->ai_addr
, sizeof(su
->sin6
));
950 if ((IN6_IS_ADDR_LINKLOCAL(&su
->sin6
.sin6_addr
) ||
951 IN6_IS_ADDR_LINKLOCAL(&su
->sin6
.sin6_addr
)) &&
952 su
->sin6
.sin6_scope_id
) {
953 *(u_int16_t
*)&su
->sin6
.sin6_addr
.s6_addr
[2] =
954 htons(su
->sin6
.sin6_scope_id
);
955 su
->sin6
.sin6_scope_id
= 0;
964 link_addr(s
, &su
->sdl
);
969 su
->sa
.sa_len
= sizeof(*su
);
970 sockaddr(s
, &su
->sa
);
983 if (q
&& which
== RTA_DST
) {
985 if ((val
= inet_addr(s
)) != INADDR_NONE
) {
987 ntohl(val
), &su
->sin
, strtoul(q
+1, 0, 0));
992 if ((which
!= RTA_DST
|| forcenet
== 0) &&
993 (val
= inet_addr(s
)) != INADDR_NONE
) {
994 su
->sin
.sin_addr
.s_addr
= val
;
995 if (which
!= RTA_DST
||
996 inet_lnaof(su
->sin
.sin_addr
) != INADDR_ANY
)
1003 if (which
== RTA_DST
&& forcehost
== 0 &&
1004 ((val
= inet_network(s
)) != INADDR_NONE
||
1005 ((np
= getnetbyname(s
)) != NULL
&& (val
= np
->n_net
) != 0))) {
1007 inet_makenetandmask(val
, &su
->sin
, 0);
1010 hp
= gethostbyname(s
);
1013 su
->sin
.sin_family
= hp
->h_addrtype
;
1014 bcopy(hp
->h_addr
, (char *)&su
->sin
.sin_addr
,
1015 MIN(hp
->h_length
, sizeof(su
->sin
.sin_addr
)));
1018 errx(EX_NOHOST
, "bad address: %s", s
);
1025 int len
= atoi(s
), q
, r
;
1029 rtm_addrs
|= RTA_NETMASK
;
1034 p
= (char *)&so_mask
.sin6
.sin6_addr
;
1039 p
= (char *)&so_mask
.sin
.sin_addr
;
1042 (void) fprintf(stderr
, "prefixlen not supported in this af\n");
1047 if (len
< 0 || max
< len
) {
1048 (void) fprintf(stderr
, "%s: bad value\n", s
);
1054 so_mask
.sa
.sa_family
= af
;
1055 so_mask
.sa
.sa_len
= aflen
;
1056 memset((void *)p
, 0, max
/ 8);
1058 memset((void *)p
, 0xff, q
);
1060 *((u_char
*)p
+ q
) = (0xff00 >> r
) & 0xff;
1072 char *buf
, *lim
, *next
;
1073 register struct rt_msghdr
*rtm
;
1077 mib
[2] = 0; /* protocol */
1078 mib
[3] = 0; /* wildcard address family */
1079 mib
[4] = NET_RT_IFLIST
;
1080 mib
[5] = 0; /* no flags */
1081 if (sysctl(mib
, 6, NULL
, &needed
, NULL
, 0) < 0)
1082 err(EX_OSERR
, "route-sysctl-estimate");
1083 if ((buf
= malloc(needed
)) == NULL
)
1084 errx(EX_OSERR
, "malloc failed");
1085 if (sysctl(mib
, 6, buf
, &needed
, NULL
, 0) < 0)
1086 err(EX_OSERR
, "actual retrieval of interface table");
1088 for (next
= buf
; next
< lim
; next
+= rtm
->rtm_msglen
) {
1089 rtm
= (struct rt_msghdr
*)next
;
1090 print_rtmsg(rtm
, rtm
->rtm_msglen
);
1107 n
= read(s
, msg
, 2048);
1109 (void) printf("\ngot message of size %d on %s", n
, ctime(&now
));
1110 print_rtmsg((struct rt_msghdr
*)msg
, n
);
1115 struct rt_msghdr m_rtm
;
1125 register char *cp
= m_rtmsg
.m_space
;
1128 #define NEXTADDR(w, u) \
1129 if (rtm_addrs & (w)) {\
1130 l = ROUNDUP(u.sa.sa_len); bcopy((char *)&(u), cp, l); cp += l;\
1131 if (verbose) sodump(&(u),"u");\
1135 bzero((char *)&m_rtmsg
, sizeof(m_rtmsg
));
1138 else if (cmd
== 'c')
1140 else if (cmd
== 'g') {
1142 if (so_ifp
.sa
.sa_family
== 0) {
1143 so_ifp
.sa
.sa_family
= AF_LINK
;
1144 so_ifp
.sa
.sa_len
= sizeof(struct sockaddr_dl
);
1145 rtm_addrs
|= RTA_IFP
;
1149 #define rtm m_rtmsg.m_rtm
1151 rtm
.rtm_flags
= flags
;
1152 rtm
.rtm_version
= RTM_VERSION
;
1153 rtm
.rtm_seq
= ++seq
;
1154 rtm
.rtm_addrs
= rtm_addrs
;
1155 rtm
.rtm_rmx
= rt_metrics
;
1156 rtm
.rtm_inits
= rtm_inits
;
1157 rtm
.rtm_index
= ifscope
;
1159 if (rtm_addrs
& RTA_NETMASK
)
1161 NEXTADDR(RTA_DST
, so_dst
);
1162 NEXTADDR(RTA_GATEWAY
, so_gate
);
1163 NEXTADDR(RTA_NETMASK
, so_mask
);
1164 NEXTADDR(RTA_GENMASK
, so_genmask
);
1165 NEXTADDR(RTA_IFP
, so_ifp
);
1166 NEXTADDR(RTA_IFA
, so_ifa
);
1167 rtm
.rtm_msglen
= l
= cp
- (char *)&m_rtmsg
;
1169 print_rtmsg(&rtm
, l
);
1172 if ((rlen
= write(s
, (char *)&m_rtmsg
, l
)) < 0) {
1173 warn("writing to routing socket");
1176 if (cmd
== RTM_GET
) {
1178 l
= read(s
, (char *)&m_rtmsg
, sizeof(m_rtmsg
));
1179 } while (l
> 0 && (rtm
.rtm_seq
!= seq
|| rtm
.rtm_pid
!= pid
));
1181 warn("read from routing socket");
1183 print_getmsg(&rtm
, l
);
1192 int olen
= so_mask
.sa
.sa_len
;
1193 register char *cp1
= olen
+ (char *)&so_mask
, *cp2
;
1195 for (so_mask
.sa
.sa_len
= 0; cp1
> (char *)&so_mask
; )
1197 so_mask
.sa
.sa_len
= 1 + cp1
- (char *)&so_mask
;
1200 if ((rtm_addrs
& RTA_DST
) == 0)
1202 switch (so_dst
.sa
.sa_family
) {
1211 cp1
= so_mask
.sa
.sa_len
+ 1 + (char *)&so_dst
;
1212 cp2
= so_dst
.sa
.sa_len
+ 1 + (char *)&so_dst
;
1215 cp2
= so_mask
.sa
.sa_len
+ 1 + (char *)&so_mask
;
1216 while (cp1
> so_dst
.sa
.sa_data
)
1220 char *msgtypes
[] = {
1222 "RTM_ADD: Add Route",
1223 "RTM_DELETE: Delete Route",
1224 "RTM_CHANGE: Change Metrics or flags",
1225 "RTM_GET: Report Metrics",
1226 "RTM_LOSING: Kernel Suspects Partitioning",
1227 "RTM_REDIRECT: Told to use different route",
1228 "RTM_MISS: Lookup failed on this address",
1229 "RTM_LOCK: fix specified metrics",
1230 "RTM_OLDADD: caused by SIOCADDRT",
1231 "RTM_OLDDEL: caused by SIOCDELRT",
1232 "RTM_RESOLVE: Route created by cloning",
1233 "RTM_NEWADDR: address being added to iface",
1234 "RTM_DELADDR: address being removed from iface",
1235 "RTM_IFINFO: iface status change",
1236 "RTM_NEWMADDR: new multicast group membership on iface",
1237 "RTM_DELMADDR: multicast group membership removed from iface",
1241 char metricnames
[] =
1242 "\011pksent\010rttvar\7rtt\6ssthresh\5sendpipe\4recvpipe\3expire\2hopcount"
1245 "\1UP\2GATEWAY\3HOST\4REJECT\5DYNAMIC\6MODIFIED\7DONE\010DELCLONE"
1246 "\011CLONING\012XRESOLVE\013LLINFO\014STATIC\015BLACKHOLE\016b016"
1247 "\017PROTO2\020PROTO1\021PRCLONING\022WASCLONED\023PROTO3\024b024"
1248 "\025PINNED\026LOCAL\027BROADCAST\030MULTICAST\031IFSCOPE\032CONDEMNED"
1251 "\1UP\2BROADCAST\3DEBUG\4LOOPBACK\5PTP\6b6\7RUNNING\010NOARP"
1252 "\011PPROMISC\012ALLMULTI\013OACTIVE\014SIMPLEX\015LINK0\016LINK1"
1253 "\017LINK2\020MULTICAST";
1255 "\1DST\2GATEWAY\3NETMASK\4GENMASK\5IFP\6IFA\7AUTHOR\010BRD";
1258 print_rtmsg(rtm
, msglen
)
1259 register struct rt_msghdr
*rtm
;
1262 struct if_msghdr
*ifm
;
1263 struct ifa_msghdr
*ifam
;
1265 struct ifma_msghdr
*ifmam
;
1270 if (rtm
->rtm_version
!= RTM_VERSION
) {
1271 (void) printf("routing message version %d not understood\n",
1275 (void)printf("%s: len %d, ", msgtypes
[rtm
->rtm_type
], rtm
->rtm_msglen
);
1276 switch (rtm
->rtm_type
) {
1278 ifm
= (struct if_msghdr
*)rtm
;
1279 (void) printf("if# %d, flags:", ifm
->ifm_index
);
1280 bprintf(stdout
, ifm
->ifm_flags
, ifnetflags
);
1281 pmsg_addrs((char *)(ifm
+ 1), ifm
->ifm_addrs
);
1285 ifam
= (struct ifa_msghdr
*)rtm
;
1286 (void) printf("metric %d, flags:", ifam
->ifam_metric
);
1287 bprintf(stdout
, ifam
->ifam_flags
, routeflags
);
1288 pmsg_addrs((char *)(ifam
+ 1), ifam
->ifam_addrs
);
1293 ifmam
= (struct ifma_msghdr
*)rtm
;
1294 pmsg_addrs((char *)(ifmam
+ 1), ifmam
->ifmam_addrs
);
1298 (void) printf("pid: %ld, seq %d, errno %d, ",
1299 (long)rtm
->rtm_pid
, rtm
->rtm_seq
, rtm
->rtm_errno
);
1300 if (rtm
->rtm_flags
& RTF_IFSCOPE
)
1301 (void) printf("ifscope %d, ", rtm
->rtm_index
);
1303 if (rtm
->rtm_flags
& RTF_IFREF
)
1304 (void) printf("ifref, ");
1305 #endif /* RTF_IFREF */
1306 (void) printf("flags:");
1307 bprintf(stdout
, rtm
->rtm_flags
, routeflags
);
1313 print_getmsg(rtm
, msglen
)
1314 register struct rt_msghdr
*rtm
;
1317 struct sockaddr
*dst
= NULL
, *gate
= NULL
, *mask
= NULL
;
1318 struct sockaddr_dl
*ifp
= NULL
;
1319 register struct sockaddr
*sa
;
1323 (void) printf(" route to: %s\n", routename(&so_dst
));
1324 if (rtm
->rtm_version
!= RTM_VERSION
) {
1325 warnx("routing message version %d not understood",
1329 if (rtm
->rtm_msglen
> msglen
) {
1330 warnx("message length mismatch, in packet %d, returned %d",
1331 rtm
->rtm_msglen
, msglen
);
1333 if (rtm
->rtm_errno
) {
1334 errno
= rtm
->rtm_errno
;
1335 warn("message indicates error %d", errno
);
1338 cp
= ((char *)(rtm
+ 1));
1340 for (i
= 1; i
; i
<<= 1)
1341 if (i
& rtm
->rtm_addrs
) {
1342 sa
= (struct sockaddr
*)cp
;
1354 if (sa
->sa_family
== AF_LINK
&&
1355 ((struct sockaddr_dl
*)sa
)->sdl_nlen
)
1356 ifp
= (struct sockaddr_dl
*)sa
;
1362 mask
->sa_family
= dst
->sa_family
; /* XXX */
1364 (void)printf("destination: %s\n", routename(dst
));
1366 int savenflag
= nflag
;
1369 (void)printf(" mask: %s\n", routename(mask
));
1372 if (gate
&& rtm
->rtm_flags
& RTF_GATEWAY
)
1373 (void)printf(" gateway: %s\n", routename(gate
));
1375 (void)printf(" interface: %.*s\n",
1376 ifp
->sdl_nlen
, ifp
->sdl_data
);
1377 (void)printf(" flags: ");
1378 bprintf(stdout
, rtm
->rtm_flags
, routeflags
);
1380 #define lock(f) ((rtm->rtm_rmx.rmx_locks & __CONCAT(RTV_,f)) ? 'L' : ' ')
1381 #define msec(u) (((u) + 500) / 1000) /* usec to msec */
1383 (void) printf("\n%s\n", "\
1384 recvpipe sendpipe ssthresh rtt,msec rttvar hopcount mtu expire");
1385 printf("%8u%c ", rtm
->rtm_rmx
.rmx_recvpipe
, lock(RPIPE
));
1386 printf("%8u%c ", rtm
->rtm_rmx
.rmx_sendpipe
, lock(SPIPE
));
1387 printf("%8u%c ", rtm
->rtm_rmx
.rmx_ssthresh
, lock(SSTHRESH
));
1388 printf("%8u%c ", msec(rtm
->rtm_rmx
.rmx_rtt
), lock(RTT
));
1389 printf("%8u%c ", msec(rtm
->rtm_rmx
.rmx_rttvar
), lock(RTTVAR
));
1390 printf("%8u%c ", rtm
->rtm_rmx
.rmx_hopcount
, lock(HOPCOUNT
));
1391 printf("%8u%c ", rtm
->rtm_rmx
.rmx_mtu
, lock(MTU
));
1392 if (rtm
->rtm_rmx
.rmx_expire
)
1393 rtm
->rtm_rmx
.rmx_expire
-= time(0);
1394 printf("%8d%c\n", rtm
->rtm_rmx
.rmx_expire
, lock(EXPIRE
));
1397 #define RTA_IGN (RTA_DST|RTA_GATEWAY|RTA_NETMASK|RTA_IFP|RTA_IFA|RTA_BRD)
1400 else if (rtm
->rtm_addrs
&~ RTA_IGN
) {
1401 (void) printf("sockaddrs: ");
1402 bprintf(stdout
, rtm
->rtm_addrs
, addrnames
);
1410 register struct rt_msghdr
*rtm
;
1412 (void) printf("\nlocks: ");
1413 bprintf(stdout
, rtm
->rtm_rmx
.rmx_locks
, metricnames
);
1414 (void) printf(" inits: ");
1415 bprintf(stdout
, rtm
->rtm_inits
, metricnames
);
1416 pmsg_addrs(((char *)(rtm
+ 1)), rtm
->rtm_addrs
);
1420 pmsg_addrs(cp
, addrs
)
1424 register struct sockaddr
*sa
;
1428 (void) putchar('\n');
1431 (void) printf("\nsockaddrs: ");
1432 bprintf(stdout
, addrs
, addrnames
);
1433 (void) putchar('\n');
1434 for (i
= 1; i
; i
<<= 1)
1436 sa
= (struct sockaddr
*)cp
;
1437 (void) printf(" %s", routename(sa
));
1440 (void) putchar('\n');
1441 (void) fflush(stdout
);
1455 while ((i
= *s
++) != 0) {
1456 if (b
& (1 << (i
-1))) {
1463 for (; (i
= *s
) > 32; s
++)
1470 (void) putc('>', fp
);
1477 register struct keytab
*kt
= keywords
;
1479 while (kt
->kt_cp
&& strcmp(kt
->kt_cp
, cp
))
1489 switch (su
->sa
.sa_family
) {
1491 (void) printf("%s: link %s; ",
1492 which
, link_ntoa(&su
->sdl
));
1495 (void) printf("%s: inet %s; ",
1496 which
, inet_ntoa(su
->sin
.sin_addr
));
1499 (void) fflush(stdout
);
1513 register char *addr
;
1514 register struct sockaddr
*sa
;
1516 register char *cp
= (char *)sa
;
1517 int size
= sa
->sa_len
;
1518 char *cplim
= cp
+ size
;
1519 register int byte
= 0, state
= VIRGIN
, new = 0 /* foil gcc */;
1524 if ((*addr
>= '0') && (*addr
<= '9')) {
1526 } else if ((*addr
>= 'a') && (*addr
<= 'f')) {
1527 new = *addr
- 'a' + 10;
1528 } else if ((*addr
>= 'A') && (*addr
<= 'F')) {
1529 new = *addr
- 'A' + 10;
1530 } else if (*addr
== 0)
1535 switch (state
/* | INPUT */) {
1536 case GOTTWO
| DIGIT
:
1537 *cp
++ = byte
; /*FALLTHROUGH*/
1538 case VIRGIN
| DIGIT
:
1539 state
= GOTONE
; byte
= new; continue;
1540 case GOTONE
| DIGIT
:
1541 state
= GOTTWO
; byte
= new + (byte
<< 4); continue;
1542 default: /* | DELIM */
1543 state
= VIRGIN
; *cp
++ = byte
; byte
= 0; continue;
1546 *cp
++ = byte
; /* FALLTHROUGH */
1551 } while (cp
< cplim
);
1552 sa
->sa_len
= cp
- (char *)sa
;