]>
Commit | Line | Data |
---|---|---|
b7080c8e A |
1 | /* |
2 | * Copyright (c) 1999 Apple Computer, Inc. All rights reserved. | |
3 | * | |
4 | * @APPLE_LICENSE_HEADER_START@ | |
5 | * | |
07f47057 A |
6 | * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved. |
7 | * | |
8 | * This file contains Original Code and/or Modifications of Original Code | |
9 | * as defined in and that are subject to the Apple Public Source License | |
10 | * Version 2.0 (the 'License'). You may not use this file except in | |
11 | * compliance with the License. Please obtain a copy of the License at | |
12 | * http://www.opensource.apple.com/apsl/ and read it before using this | |
13 | * file. | |
b7080c8e A |
14 | * |
15 | * The Original Code and all software distributed under the License are | |
16 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER | |
17 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, | |
18 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
07f47057 A |
19 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. |
20 | * Please see the License for the specific language governing rights and | |
21 | * limitations under the License. | |
b7080c8e A |
22 | * |
23 | * @APPLE_LICENSE_HEADER_END@ | |
24 | */ | |
25 | /* | |
26 | * Copyright (c) 1983, 1988, 1993 | |
27 | * The Regents of the University of California. All rights reserved. | |
28 | * | |
29 | * Redistribution and use in source and binary forms, with or without | |
30 | * modification, are permitted provided that the following conditions | |
31 | * are met: | |
32 | * 1. Redistributions of source code must retain the above copyright | |
33 | * notice, this list of conditions and the following disclaimer. | |
34 | * 2. Redistributions in binary form must reproduce the above copyright | |
35 | * notice, this list of conditions and the following disclaimer in the | |
36 | * documentation and/or other materials provided with the distribution. | |
37 | * 3. All advertising materials mentioning features or use of this software | |
38 | * must display the following acknowledgement: | |
39 | * This product includes software developed by the University of | |
40 | * California, Berkeley and its contributors. | |
41 | * 4. Neither the name of the University nor the names of its contributors | |
42 | * may be used to endorse or promote products derived from this software | |
43 | * without specific prior written permission. | |
44 | * | |
45 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
46 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
47 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
48 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
49 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
50 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
51 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
52 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
53 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
54 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
55 | * SUCH DAMAGE. | |
56 | */ | |
57 | ||
58 | #ifndef lint | |
59 | #if 0 | |
60 | static char sccsid[] = "From: @(#)route.c 8.6 (Berkeley) 4/28/95"; | |
61 | #endif | |
62 | static const char rcsid[] = | |
ac2f15b3 | 63 | "$Id: route.c,v 1.4 2003/07/08 23:03:54 lindak Exp $"; |
b7080c8e A |
64 | #endif /* not lint */ |
65 | ||
66 | #include <sys/param.h> | |
67 | #include <sys/protosw.h> | |
68 | #include <sys/socket.h> | |
69 | #include <sys/time.h> | |
70 | ||
71 | #include <net/if.h> | |
72 | #include <net/if_var.h> | |
73 | #include <net/if_dl.h> | |
74 | #include <net/if_types.h> | |
75 | #include <net/route.h> | |
76 | ||
77 | #include <netinet/in.h> | |
7ba0088d | 78 | #ifndef __APPLE__ |
b7080c8e A |
79 | #include <netipx/ipx.h> |
80 | #include <netatalk/at.h> | |
7ba0088d | 81 | #include <netgraph/ng_socket.h> |
b7080c8e A |
82 | #endif |
83 | ||
84 | #ifdef NS | |
85 | #include <netns/ns.h> | |
86 | #endif | |
87 | ||
88 | #include <sys/sysctl.h> | |
89 | ||
7ba0088d A |
90 | #include <arpa/inet.h> |
91 | #ifndef __APPLE__ | |
92 | #include <libutil.h> | |
93 | #endif | |
b7080c8e A |
94 | #include <netdb.h> |
95 | #include <stdio.h> | |
96 | #include <stdlib.h> | |
97 | #include <string.h> | |
98 | #include <unistd.h> | |
99 | #include <err.h> | |
100 | #include <time.h> | |
101 | #include "netstat.h" | |
102 | ||
103 | #define kget(p, d) (kread((u_long)(p), (char *)&(d), sizeof (d))) | |
104 | ||
7ba0088d A |
105 | |
106 | /* alignment constraint for routing socket */ | |
107 | #define ROUNDUP(a) \ | |
108 | ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long)) | |
109 | #define ADVANCE(x, n) (x += ROUNDUP((n)->sa_len)) | |
110 | ||
b7080c8e A |
111 | /* |
112 | * Definitions for showing gateway flags. | |
113 | */ | |
114 | struct bits { | |
115 | u_long b_mask; | |
116 | char b_val; | |
117 | } bits[] = { | |
118 | { RTF_UP, 'U' }, | |
119 | { RTF_GATEWAY, 'G' }, | |
120 | { RTF_HOST, 'H' }, | |
121 | { RTF_REJECT, 'R' }, | |
122 | { RTF_DYNAMIC, 'D' }, | |
123 | { RTF_MODIFIED, 'M' }, | |
124 | { RTF_DONE, 'd' }, /* Completed -- for routing messages only */ | |
125 | { RTF_CLONING, 'C' }, | |
126 | { RTF_XRESOLVE, 'X' }, | |
127 | { RTF_LLINFO, 'L' }, | |
128 | { RTF_STATIC, 'S' }, | |
129 | { RTF_PROTO1, '1' }, | |
130 | { RTF_PROTO2, '2' }, | |
131 | { RTF_WASCLONED,'W' }, | |
132 | { RTF_PRCLONING,'c' }, | |
133 | { RTF_PROTO3, '3' }, | |
134 | { RTF_BLACKHOLE,'B' }, | |
135 | { RTF_BROADCAST,'b' }, | |
136 | { 0 } | |
137 | }; | |
138 | ||
139 | typedef union { | |
140 | long dummy; /* Helps align structure. */ | |
141 | struct sockaddr u_sa; | |
142 | u_short u_data[128]; | |
143 | } sa_u; | |
144 | ||
145 | static sa_u pt_u; | |
146 | ||
147 | int do_rtent = 0; | |
148 | struct rtentry rtentry; | |
149 | struct radix_node rnode; | |
150 | struct radix_mask rmask; | |
151 | struct radix_node_head *rt_tables[AF_MAX+1]; | |
152 | ||
153 | int NewTree = 0; | |
154 | ||
155 | static struct sockaddr *kgetsa __P((struct sockaddr *)); | |
156 | static void p_tree __P((struct radix_node *)); | |
157 | static void p_rtnode __P((void)); | |
158 | static void ntreestuff __P((void)); | |
159 | static void np_rtentry __P((struct rt_msghdr *)); | |
160 | static void p_sockaddr __P((struct sockaddr *, struct sockaddr *, int, int)); | |
161 | static void p_flags __P((int, char *)); | |
162 | static void p_rtentry __P((struct rtentry *)); | |
163 | static u_long forgemask __P((u_long)); | |
164 | static void domask __P((char *, u_long, u_long)); | |
165 | ||
166 | /* | |
167 | * Print routing tables. | |
168 | */ | |
169 | void | |
7ba0088d | 170 | routepr(u_long rtree) |
b7080c8e A |
171 | { |
172 | struct radix_node_head *rnh, head; | |
173 | int i; | |
174 | ||
175 | printf("Routing tables\n"); | |
176 | ||
177 | if (Aflag == 0 && NewTree) | |
178 | ntreestuff(); | |
179 | else { | |
180 | if (rtree == 0) { | |
181 | printf("rt_tables: symbol not in namelist\n"); | |
182 | return; | |
183 | } | |
184 | ||
185 | kget(rtree, rt_tables); | |
186 | for (i = 0; i <= AF_MAX; i++) { | |
187 | if ((rnh = rt_tables[i]) == 0) | |
188 | continue; | |
189 | kget(rnh, head); | |
190 | if (i == AF_UNSPEC) { | |
191 | if (Aflag && af == 0) { | |
192 | printf("Netmasks:\n"); | |
193 | p_tree(head.rnh_treetop); | |
194 | } | |
195 | } else if (af == AF_UNSPEC || af == i) { | |
196 | pr_family(i); | |
197 | do_rtent = 1; | |
7ba0088d | 198 | pr_rthdr(i); |
b7080c8e A |
199 | p_tree(head.rnh_treetop); |
200 | } | |
201 | } | |
202 | } | |
203 | } | |
204 | ||
205 | /* | |
206 | * Print address family header before a section of the routing table. | |
207 | */ | |
208 | void | |
7ba0088d | 209 | pr_family(int af) |
b7080c8e A |
210 | { |
211 | char *afname; | |
212 | ||
213 | switch (af) { | |
214 | case AF_INET: | |
215 | afname = "Internet"; | |
216 | break; | |
7ba0088d A |
217 | #ifdef INET6 |
218 | case AF_INET6: | |
219 | afname = "Internet6"; | |
220 | break; | |
221 | #endif /*INET6*/ | |
b7080c8e A |
222 | case AF_IPX: |
223 | afname = "IPX"; | |
224 | break; | |
b7080c8e A |
225 | #ifdef NS |
226 | case AF_NS: | |
227 | afname = "XNS"; | |
228 | break; | |
229 | #endif | |
7ba0088d | 230 | #ifndef __APPLE__ |
b7080c8e A |
231 | case AF_ISO: |
232 | afname = "ISO"; | |
233 | break; | |
234 | case AF_APPLETALK: | |
235 | afname = "AppleTalk"; | |
236 | break; | |
237 | case AF_CCITT: | |
238 | afname = "X.25"; | |
239 | break; | |
7ba0088d A |
240 | case AF_NETGRAPH: |
241 | afname = "Netgraph"; | |
242 | break; | |
243 | #endif | |
b7080c8e A |
244 | default: |
245 | afname = NULL; | |
246 | break; | |
247 | } | |
248 | if (afname) | |
249 | printf("\n%s:\n", afname); | |
250 | else | |
251 | printf("\nProtocol Family %d:\n", af); | |
252 | } | |
253 | ||
254 | /* column widths; each followed by one space */ | |
7ba0088d A |
255 | #ifndef INET6 |
256 | #define WID_DST(af) 18 /* width of destination column */ | |
257 | #define WID_GW(af) 18 /* width of gateway column */ | |
258 | #define WID_IF(af) 6 /* width of netif column */ | |
259 | #else | |
260 | #define WID_DST(af) \ | |
261 | ((af) == AF_INET6 ? (lflag ? 39 : (nflag ? 33: 18)) : 18) | |
262 | #define WID_GW(af) \ | |
263 | ((af) == AF_INET6 ? (lflag ? 31 : (nflag ? 29 : 18)) : 18) | |
264 | #define WID_IF(af) ((af) == AF_INET6 ? 8 : 6) | |
265 | #endif /*INET6*/ | |
b7080c8e A |
266 | |
267 | /* | |
268 | * Print header for routing table columns. | |
269 | */ | |
270 | void | |
7ba0088d | 271 | pr_rthdr(int af) |
b7080c8e | 272 | { |
7ba0088d | 273 | |
b7080c8e A |
274 | if (Aflag) |
275 | printf("%-8.8s ","Address"); | |
7ba0088d A |
276 | if (af == AF_INET || lflag) |
277 | if (lflag) | |
278 | printf("%-*.*s %-*.*s %-6.6s %6.6s %8.8s %6.6s %*.*s %6s\n", | |
279 | WID_DST(af), WID_DST(af), "Destination", | |
280 | WID_GW(af), WID_GW(af), "Gateway", | |
281 | "Flags", "Refs", "Use", "Mtu", | |
282 | WID_IF(af), WID_IF(af), "Netif", "Expire"); | |
283 | else | |
284 | printf("%-*.*s %-*.*s %-6.6s %6.6s %8.8s %*.*s %6s\n", | |
285 | WID_DST(af), WID_DST(af), "Destination", | |
286 | WID_GW(af), WID_GW(af), "Gateway", | |
287 | "Flags", "Refs", "Use", | |
288 | WID_IF(af), WID_IF(af), "Netif", "Expire"); | |
289 | else | |
290 | printf("%-*.*s %-*.*s %-6.6s %8.8s %6s\n", | |
291 | WID_DST(af), WID_DST(af), "Destination", | |
292 | WID_GW(af), WID_GW(af), "Gateway", | |
293 | "Flags", "Netif", "Expire"); | |
b7080c8e A |
294 | } |
295 | ||
296 | static struct sockaddr * | |
7ba0088d | 297 | kgetsa(struct sockaddr *dst) |
b7080c8e A |
298 | { |
299 | ||
300 | kget(dst, pt_u.u_sa); | |
301 | if (pt_u.u_sa.sa_len > sizeof (pt_u.u_sa)) | |
302 | kread((u_long)dst, (char *)pt_u.u_data, pt_u.u_sa.sa_len); | |
303 | return (&pt_u.u_sa); | |
304 | } | |
305 | ||
306 | static void | |
7ba0088d | 307 | p_tree(struct radix_node *rn) |
b7080c8e A |
308 | { |
309 | ||
310 | again: | |
311 | kget(rn, rnode); | |
7ba0088d | 312 | if (rnode.rn_bit < 0) { |
b7080c8e A |
313 | if (Aflag) |
314 | printf("%-8.8lx ", (u_long)rn); | |
315 | if (rnode.rn_flags & RNF_ROOT) { | |
316 | if (Aflag) | |
317 | printf("(root node)%s", | |
318 | rnode.rn_dupedkey ? " =>\n" : "\n"); | |
319 | } else if (do_rtent) { | |
320 | kget(rn, rtentry); | |
321 | p_rtentry(&rtentry); | |
322 | if (Aflag) | |
323 | p_rtnode(); | |
324 | } else { | |
325 | p_sockaddr(kgetsa((struct sockaddr *)rnode.rn_key), | |
326 | NULL, 0, 44); | |
327 | putchar('\n'); | |
328 | } | |
329 | if ((rn = rnode.rn_dupedkey)) | |
330 | goto again; | |
331 | } else { | |
332 | if (Aflag && do_rtent) { | |
333 | printf("%-8.8lx ", (u_long)rn); | |
334 | p_rtnode(); | |
335 | } | |
7ba0088d A |
336 | rn = rnode.rn_right; |
337 | p_tree(rnode.rn_left); | |
b7080c8e A |
338 | p_tree(rn); |
339 | } | |
340 | } | |
341 | ||
342 | char nbuf[20]; | |
343 | ||
344 | static void | |
7ba0088d | 345 | p_rtnode(void) |
b7080c8e A |
346 | { |
347 | struct radix_mask *rm = rnode.rn_mklist; | |
348 | ||
7ba0088d | 349 | if (rnode.rn_bit < 0) { |
b7080c8e A |
350 | if (rnode.rn_mask) { |
351 | printf("\t mask "); | |
352 | p_sockaddr(kgetsa((struct sockaddr *)rnode.rn_mask), | |
353 | NULL, 0, -1); | |
354 | } else if (rm == 0) | |
355 | return; | |
356 | } else { | |
7ba0088d A |
357 | sprintf(nbuf, "(%d)", rnode.rn_bit); |
358 | printf("%6.6s %8.8lx : %8.8lx", nbuf, (u_long)rnode.rn_left, (u_long)rnode.rn_right); | |
b7080c8e A |
359 | } |
360 | while (rm) { | |
361 | kget(rm, rmask); | |
362 | sprintf(nbuf, " %d refs, ", rmask.rm_refs); | |
363 | printf(" mk = %8.8lx {(%d),%s", | |
7ba0088d | 364 | (u_long)rm, -1 - rmask.rm_bit, rmask.rm_refs ? nbuf : " "); |
b7080c8e A |
365 | if (rmask.rm_flags & RNF_NORMAL) { |
366 | struct radix_node rnode_aux; | |
367 | printf(" <normal>, "); | |
368 | kget(rmask.rm_leaf, rnode_aux); | |
369 | p_sockaddr(kgetsa((struct sockaddr *)rnode_aux.rn_mask), | |
370 | NULL, 0, -1); | |
371 | } else | |
372 | p_sockaddr(kgetsa((struct sockaddr *)rmask.rm_mask), | |
373 | NULL, 0, -1); | |
374 | putchar('}'); | |
375 | if ((rm = rmask.rm_mklist)) | |
376 | printf(" ->"); | |
377 | } | |
378 | putchar('\n'); | |
379 | } | |
380 | ||
381 | static void | |
7ba0088d | 382 | ntreestuff(void) |
b7080c8e A |
383 | { |
384 | size_t needed; | |
385 | int mib[6]; | |
386 | char *buf, *next, *lim; | |
387 | register struct rt_msghdr *rtm; | |
388 | ||
389 | mib[0] = CTL_NET; | |
390 | mib[1] = PF_ROUTE; | |
391 | mib[2] = 0; | |
392 | mib[3] = 0; | |
393 | mib[4] = NET_RT_DUMP; | |
394 | mib[5] = 0; | |
395 | if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0) { | |
396 | err(1, "sysctl: net.route.0.0.dump estimate"); | |
397 | } | |
398 | ||
399 | if ((buf = malloc(needed)) == 0) { | |
400 | err(2, "malloc(%lu)", (unsigned long)needed); | |
401 | } | |
402 | if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) { | |
403 | err(1, "sysctl: net.route.0.0.dump"); | |
404 | } | |
405 | lim = buf + needed; | |
406 | for (next = buf; next < lim; next += rtm->rtm_msglen) { | |
407 | rtm = (struct rt_msghdr *)next; | |
408 | np_rtentry(rtm); | |
409 | } | |
410 | } | |
411 | ||
412 | static void | |
7ba0088d | 413 | np_rtentry(struct rt_msghdr *rtm) |
b7080c8e A |
414 | { |
415 | register struct sockaddr *sa = (struct sockaddr *)(rtm + 1); | |
416 | #ifdef notdef | |
417 | static int masks_done, banner_printed; | |
418 | #endif | |
419 | static int old_af; | |
420 | int af = 0, interesting = RTF_UP | RTF_GATEWAY | RTF_HOST; | |
421 | ||
422 | #ifdef notdef | |
423 | /* for the moment, netmasks are skipped over */ | |
424 | if (!banner_printed) { | |
425 | printf("Netmasks:\n"); | |
426 | banner_printed = 1; | |
427 | } | |
428 | if (masks_done == 0) { | |
429 | if (rtm->rtm_addrs != RTA_DST ) { | |
430 | masks_done = 1; | |
431 | af = sa->sa_family; | |
432 | } | |
433 | } else | |
434 | #endif | |
435 | af = sa->sa_family; | |
436 | if (af != old_af) { | |
437 | pr_family(af); | |
438 | old_af = af; | |
439 | } | |
440 | if (rtm->rtm_addrs == RTA_DST) | |
441 | p_sockaddr(sa, NULL, 0, 36); | |
442 | else { | |
443 | p_sockaddr(sa, NULL, rtm->rtm_flags, 16); | |
7ba0088d | 444 | sa = (struct sockaddr *)(ROUNDUP(sa->sa_len) + (char *)sa); |
b7080c8e A |
445 | p_sockaddr(sa, NULL, 0, 18); |
446 | } | |
447 | p_flags(rtm->rtm_flags & interesting, "%-6.6s "); | |
448 | putchar('\n'); | |
449 | } | |
450 | ||
451 | static void | |
7ba0088d | 452 | p_sockaddr(struct sockaddr *sa, struct sockaddr *mask, int flags, int width) |
b7080c8e A |
453 | { |
454 | char workbuf[128], *cplim; | |
455 | register char *cp = workbuf; | |
456 | ||
457 | switch(sa->sa_family) { | |
458 | case AF_INET: | |
459 | { | |
460 | register struct sockaddr_in *sin = (struct sockaddr_in *)sa; | |
461 | ||
7ba0088d A |
462 | if ((sin->sin_addr.s_addr == INADDR_ANY) && |
463 | mask && | |
464 | ntohl(((struct sockaddr_in *)mask)->sin_addr.s_addr) | |
465 | ==0L) | |
466 | cp = "default" ; | |
b7080c8e A |
467 | else if (flags & RTF_HOST) |
468 | cp = routename(sin->sin_addr.s_addr); | |
469 | else if (mask) | |
470 | cp = netname(sin->sin_addr.s_addr, | |
471 | ntohl(((struct sockaddr_in *)mask) | |
472 | ->sin_addr.s_addr)); | |
473 | else | |
474 | cp = netname(sin->sin_addr.s_addr, 0L); | |
475 | break; | |
476 | } | |
7ba0088d A |
477 | |
478 | #ifdef INET6 | |
479 | case AF_INET6: | |
480 | { | |
481 | struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)sa; | |
482 | struct in6_addr *in6 = &sa6->sin6_addr; | |
483 | ||
484 | /* | |
485 | * XXX: This is a special workaround for KAME kernels. | |
486 | * sin6_scope_id field of SA should be set in the future. | |
487 | */ | |
488 | if (IN6_IS_ADDR_LINKLOCAL(in6) || | |
489 | IN6_IS_ADDR_MC_LINKLOCAL(in6)) { | |
490 | /* XXX: override is ok? */ | |
491 | sa6->sin6_scope_id = (u_int32_t)ntohs(*(u_short *)&in6->s6_addr[2]); | |
492 | *(u_short *)&in6->s6_addr[2] = 0; | |
493 | } | |
494 | ||
495 | if (flags & RTF_HOST) | |
496 | cp = routename6(sa6); | |
497 | else if (mask) | |
498 | cp = netname6(sa6, | |
499 | &((struct sockaddr_in6 *)mask)->sin6_addr); | |
500 | else { | |
501 | cp = netname6(sa6, NULL); | |
502 | } | |
503 | break; | |
504 | } | |
505 | #endif /*INET6*/ | |
506 | #ifndef __APPLE__ | |
b7080c8e A |
507 | case AF_IPX: |
508 | { | |
509 | struct ipx_addr work = ((struct sockaddr_ipx *)sa)->sipx_addr; | |
510 | if (ipx_nullnet(satoipx_addr(work))) | |
511 | cp = "default"; | |
512 | else | |
513 | cp = ipx_print(sa); | |
514 | break; | |
515 | } | |
b7080c8e A |
516 | case AF_APPLETALK: |
517 | { | |
518 | if (!(flags & RTF_HOST) && mask) | |
519 | cp = atalk_print2(sa,mask,9); | |
520 | else | |
521 | cp = atalk_print(sa,11); | |
522 | break; | |
523 | } | |
7ba0088d A |
524 | case AF_NETGRAPH: |
525 | { | |
526 | printf("%s", ((struct sockaddr_ng *)sa)->sg_data); | |
527 | break; | |
528 | } | |
b7080c8e | 529 | #endif |
b7080c8e A |
530 | #ifdef NS |
531 | case AF_NS: | |
532 | cp = ns_print(sa); | |
533 | break; | |
534 | #endif | |
535 | ||
536 | case AF_LINK: | |
537 | { | |
538 | register struct sockaddr_dl *sdl = (struct sockaddr_dl *)sa; | |
539 | ||
540 | if (sdl->sdl_nlen == 0 && sdl->sdl_alen == 0 && | |
541 | sdl->sdl_slen == 0) | |
542 | (void) sprintf(workbuf, "link#%d", sdl->sdl_index); | |
543 | else | |
544 | switch (sdl->sdl_type) { | |
545 | ||
546 | case IFT_ETHER: | |
547 | { | |
548 | register int i; | |
549 | register u_char *lla = (u_char *)sdl->sdl_data + | |
550 | sdl->sdl_nlen; | |
551 | ||
552 | cplim = ""; | |
553 | for (i = 0; i < sdl->sdl_alen; i++, lla++) { | |
554 | cp += sprintf(cp, "%s%x", cplim, *lla); | |
555 | cplim = ":"; | |
556 | } | |
557 | cp = workbuf; | |
558 | break; | |
559 | } | |
560 | ||
561 | default: | |
562 | cp = link_ntoa(sdl); | |
563 | break; | |
564 | } | |
565 | break; | |
566 | } | |
567 | ||
568 | default: | |
569 | { | |
570 | register u_char *s = (u_char *)sa->sa_data, *slim; | |
571 | ||
572 | slim = sa->sa_len + (u_char *) sa; | |
573 | cplim = cp + sizeof(workbuf) - 6; | |
574 | cp += sprintf(cp, "(%d)", sa->sa_family); | |
575 | while (s < slim && cp < cplim) { | |
576 | cp += sprintf(cp, " %02x", *s++); | |
577 | if (s < slim) | |
578 | cp += sprintf(cp, "%02x", *s++); | |
579 | } | |
580 | cp = workbuf; | |
581 | } | |
582 | } | |
583 | if (width < 0 ) | |
584 | printf("%s ", cp); | |
585 | else { | |
586 | if (nflag) | |
587 | printf("%-*s ", width, cp); | |
588 | else | |
589 | printf("%-*.*s ", width, width, cp); | |
590 | } | |
591 | } | |
592 | ||
593 | static void | |
7ba0088d | 594 | p_flags(int f, char *format) |
b7080c8e A |
595 | { |
596 | char name[33], *flags; | |
597 | register struct bits *p = bits; | |
598 | ||
599 | for (flags = name; p->b_mask; p++) | |
600 | if (p->b_mask & f) | |
601 | *flags++ = p->b_val; | |
602 | *flags = '\0'; | |
603 | printf(format, name); | |
604 | } | |
605 | ||
606 | static void | |
7ba0088d | 607 | p_rtentry(struct rtentry *rt) |
b7080c8e A |
608 | { |
609 | static struct ifnet ifnet, *lastif; | |
7ba0088d | 610 | struct rtentry parent; |
b7080c8e A |
611 | static char name[16]; |
612 | static char prettyname[9]; | |
613 | struct sockaddr *sa; | |
614 | sa_u addr, mask; | |
615 | ||
616 | /* | |
617 | * Don't print protocol-cloned routes unless -a. | |
618 | */ | |
7ba0088d A |
619 | if (rt->rt_flags & RTF_WASCLONED && !aflag) { |
620 | kget(rt->rt_parent, parent); | |
621 | if (parent.rt_flags & RTF_PRCLONING) | |
622 | return; | |
623 | } | |
b7080c8e A |
624 | |
625 | bzero(&addr, sizeof(addr)); | |
626 | if ((sa = kgetsa(rt_key(rt)))) | |
627 | bcopy(sa, &addr, sa->sa_len); | |
628 | bzero(&mask, sizeof(mask)); | |
629 | if (rt_mask(rt) && (sa = kgetsa(rt_mask(rt)))) | |
630 | bcopy(sa, &mask, sa->sa_len); | |
7ba0088d A |
631 | p_sockaddr(&addr.u_sa, &mask.u_sa, rt->rt_flags, |
632 | WID_DST(addr.u_sa.sa_family)); | |
633 | p_sockaddr(kgetsa(rt->rt_gateway), NULL, RTF_HOST, | |
634 | WID_GW(addr.u_sa.sa_family)); | |
b7080c8e | 635 | p_flags(rt->rt_flags, "%-6.6s "); |
7ba0088d A |
636 | if (addr.u_sa.sa_family == AF_INET || lflag) { |
637 | printf("%6ld %8ld ", rt->rt_refcnt, rt->rt_use); | |
638 | if (lflag) { | |
639 | if (rt->rt_rmx.rmx_mtu != 0) | |
640 | printf("%6lu ", rt->rt_rmx.rmx_mtu); | |
641 | else | |
642 | printf("%6s ", ""); | |
643 | } | |
644 | } | |
b7080c8e A |
645 | if (rt->rt_ifp) { |
646 | if (rt->rt_ifp != lastif) { | |
647 | kget(rt->rt_ifp, ifnet); | |
648 | kread((u_long)ifnet.if_name, name, 16); | |
649 | lastif = rt->rt_ifp; | |
650 | snprintf(prettyname, sizeof prettyname, | |
7ba0088d | 651 | "%s%d", name, ifnet.if_unit); |
b7080c8e | 652 | } |
7ba0088d A |
653 | printf("%*.*s", WID_IF(addr.u_sa.sa_family), |
654 | WID_IF(addr.u_sa.sa_family), prettyname); | |
b7080c8e A |
655 | if (rt->rt_rmx.rmx_expire) { |
656 | time_t expire_time; | |
657 | ||
658 | if ((expire_time = | |
659 | rt->rt_rmx.rmx_expire - time((time_t *)0)) > 0) | |
7ba0088d | 660 | printf(" %6d", (int)expire_time); |
b7080c8e | 661 | } |
7ba0088d A |
662 | if (rt->rt_nodes[0].rn_dupedkey) |
663 | printf(" =>"); | |
b7080c8e A |
664 | } |
665 | putchar('\n'); | |
666 | } | |
667 | ||
668 | char * | |
7ba0088d | 669 | routename(u_long in) |
b7080c8e A |
670 | { |
671 | register char *cp; | |
7ba0088d | 672 | static char line[MAXHOSTNAMELEN]; |
b7080c8e A |
673 | struct hostent *hp; |
674 | ||
675 | cp = 0; | |
676 | if (!nflag) { | |
677 | hp = gethostbyaddr((char *)&in, sizeof (struct in_addr), | |
678 | AF_INET); | |
679 | if (hp) { | |
680 | cp = hp->h_name; | |
7ba0088d | 681 | //### trimdomain(cp, strlen(cp)); |
b7080c8e A |
682 | } |
683 | } | |
684 | if (cp) { | |
685 | strncpy(line, cp, sizeof(line) - 1); | |
686 | line[sizeof(line) - 1] = '\0'; | |
687 | } else { | |
688 | #define C(x) ((x) & 0xff) | |
689 | in = ntohl(in); | |
690 | sprintf(line, "%lu.%lu.%lu.%lu", | |
691 | C(in >> 24), C(in >> 16), C(in >> 8), C(in)); | |
692 | } | |
693 | return (line); | |
694 | } | |
695 | ||
696 | static u_long | |
7ba0088d | 697 | forgemask(u_long a) |
b7080c8e A |
698 | { |
699 | u_long m; | |
700 | ||
701 | if (IN_CLASSA(a)) | |
702 | m = IN_CLASSA_NET; | |
703 | else if (IN_CLASSB(a)) | |
704 | m = IN_CLASSB_NET; | |
705 | else | |
706 | m = IN_CLASSC_NET; | |
707 | return (m); | |
708 | } | |
709 | ||
710 | static void | |
7ba0088d | 711 | domask(char *dst, u_long addr, u_long mask) |
b7080c8e A |
712 | { |
713 | register int b, i; | |
714 | ||
715 | if (!mask || (forgemask(addr) == mask)) { | |
716 | *dst = '\0'; | |
717 | return; | |
718 | } | |
719 | i = 0; | |
720 | for (b = 0; b < 32; b++) | |
721 | if (mask & (1 << b)) { | |
722 | register int bb; | |
723 | ||
724 | i = b; | |
725 | for (bb = b+1; bb < 32; bb++) | |
726 | if (!(mask & (1 << bb))) { | |
727 | i = -1; /* noncontig */ | |
728 | break; | |
729 | } | |
730 | break; | |
731 | } | |
732 | if (i == -1) | |
733 | sprintf(dst, "&0x%lx", mask); | |
734 | else | |
735 | sprintf(dst, "/%d", 32-i); | |
736 | } | |
737 | ||
738 | /* | |
739 | * Return the name of the network whose address is given. | |
740 | * The address is assumed to be that of a net or subnet, not a host. | |
741 | */ | |
742 | char * | |
7ba0088d | 743 | netname(u_long in, u_long mask) |
b7080c8e A |
744 | { |
745 | char *cp = 0; | |
7ba0088d | 746 | static char line[MAXHOSTNAMELEN]; |
b7080c8e A |
747 | struct netent *np = 0; |
748 | u_long net, omask, dmask; | |
749 | register u_long i; | |
750 | ||
751 | i = ntohl(in); | |
7ba0088d | 752 | dmask = forgemask(i); |
b7080c8e A |
753 | omask = mask; |
754 | if (!nflag && i) { | |
b7080c8e A |
755 | net = i & dmask; |
756 | if (!(np = getnetbyaddr(i, AF_INET)) && net != i) | |
757 | np = getnetbyaddr(net, AF_INET); | |
758 | if (np) { | |
759 | cp = np->n_name; | |
7ba0088d | 760 | //### trimdomain(cp, strlen(cp)); |
b7080c8e A |
761 | } |
762 | } | |
763 | if (cp) | |
764 | strncpy(line, cp, sizeof(line) - 1); | |
7ba0088d A |
765 | else { |
766 | switch (dmask) { | |
767 | case IN_CLASSA_NET: | |
768 | if ((i & IN_CLASSA_HOST) == 0) { | |
769 | sprintf(line, "%lu", C(i >> 24)); | |
770 | break; | |
771 | } | |
772 | /* FALLTHROUGH */ | |
773 | case IN_CLASSB_NET: | |
774 | if ((i & IN_CLASSB_HOST) == 0) { | |
775 | sprintf(line, "%lu.%lu", | |
776 | C(i >> 24), C(i >> 16)); | |
777 | break; | |
778 | } | |
779 | /* FALLTHROUGH */ | |
780 | case IN_CLASSC_NET: | |
781 | if ((i & IN_CLASSC_HOST) == 0) { | |
782 | sprintf(line, "%lu.%lu.%lu", | |
783 | C(i >> 24), C(i >> 16), C(i >> 8)); | |
784 | break; | |
785 | } | |
786 | /* FALLTHROUGH */ | |
787 | default: | |
788 | sprintf(line, "%lu.%lu.%lu.%lu", | |
789 | C(i >> 24), C(i >> 16), C(i >> 8), C(i)); | |
790 | break; | |
791 | } | |
792 | } | |
b7080c8e A |
793 | domask(line+strlen(line), i, omask); |
794 | return (line); | |
795 | } | |
796 | ||
7ba0088d A |
797 | #ifdef INET6 |
798 | char * | |
799 | netname6(struct sockaddr_in6 *sa6, struct in6_addr *mask) | |
800 | { | |
801 | static char line[MAXHOSTNAMELEN]; | |
802 | u_char *p = (u_char *)mask; | |
803 | u_char *lim; | |
804 | int masklen, illegal = 0, flag = NI_WITHSCOPEID; | |
805 | ||
806 | if (mask) { | |
807 | for (masklen = 0, lim = p + 16; p < lim; p++) { | |
808 | switch (*p) { | |
809 | case 0xff: | |
810 | masklen += 8; | |
811 | break; | |
812 | case 0xfe: | |
813 | masklen += 7; | |
814 | break; | |
815 | case 0xfc: | |
816 | masklen += 6; | |
817 | break; | |
818 | case 0xf8: | |
819 | masklen += 5; | |
820 | break; | |
821 | case 0xf0: | |
822 | masklen += 4; | |
823 | break; | |
824 | case 0xe0: | |
825 | masklen += 3; | |
826 | break; | |
827 | case 0xc0: | |
828 | masklen += 2; | |
829 | break; | |
830 | case 0x80: | |
831 | masklen += 1; | |
832 | break; | |
833 | case 0x00: | |
834 | break; | |
835 | default: | |
836 | illegal ++; | |
837 | break; | |
838 | } | |
839 | } | |
840 | if (illegal) | |
841 | fprintf(stderr, "illegal prefixlen\n"); | |
842 | } | |
843 | else | |
844 | masklen = 128; | |
845 | ||
846 | if (masklen == 0 && IN6_IS_ADDR_UNSPECIFIED(&sa6->sin6_addr)) | |
847 | return("default"); | |
848 | ||
849 | if (nflag) | |
850 | flag |= NI_NUMERICHOST; | |
851 | getnameinfo((struct sockaddr *)sa6, sa6->sin6_len, line, sizeof(line), | |
852 | NULL, 0, flag); | |
853 | ||
854 | if (nflag) | |
855 | sprintf(&line[strlen(line)], "/%d", masklen); | |
856 | ||
857 | return line; | |
858 | } | |
859 | ||
860 | char * | |
861 | routename6(struct sockaddr_in6 *sa6) | |
862 | { | |
863 | static char line[MAXHOSTNAMELEN]; | |
864 | int flag = NI_WITHSCOPEID; | |
865 | /* use local variable for safety */ | |
ac2f15b3 | 866 | struct sockaddr_in6 sa6_local = {sizeof(sa6_local), AF_INET6, }; |
7ba0088d A |
867 | |
868 | sa6_local.sin6_addr = sa6->sin6_addr; | |
869 | sa6_local.sin6_scope_id = sa6->sin6_scope_id; | |
870 | ||
871 | if (nflag) | |
872 | flag |= NI_NUMERICHOST; | |
873 | ||
874 | getnameinfo((struct sockaddr *)&sa6_local, sa6_local.sin6_len, | |
875 | line, sizeof(line), NULL, 0, flag); | |
876 | ||
877 | return line; | |
878 | } | |
879 | #endif /*INET6*/ | |
880 | ||
b7080c8e A |
881 | /* |
882 | * Print routing statistics | |
883 | */ | |
884 | void | |
7ba0088d | 885 | rt_stats(u_long rtsaddr, u_long rttaddr) |
b7080c8e A |
886 | { |
887 | struct rtstat rtstat; | |
7ba0088d | 888 | int rttrash; |
b7080c8e | 889 | |
7ba0088d | 890 | if (rtsaddr == 0) { |
b7080c8e A |
891 | printf("rtstat: symbol not in namelist\n"); |
892 | return; | |
893 | } | |
7ba0088d A |
894 | if (rttaddr == 0) { |
895 | printf("rttrash: symbol not in namelist\n"); | |
896 | return; | |
897 | } | |
898 | kread(rtsaddr, (char *)&rtstat, sizeof (rtstat)); | |
899 | kread(rttaddr, (char *)&rttrash, sizeof (rttrash)); | |
b7080c8e | 900 | printf("routing:\n"); |
b7080c8e | 901 | |
7ba0088d A |
902 | #define p(f, m) if (rtstat.f || sflag <= 1) \ |
903 | printf(m, rtstat.f, plural(rtstat.f)) | |
904 | ||
905 | p(rts_badredirect, "\t%u bad routing redirect%s\n"); | |
906 | p(rts_dynamic, "\t%u dynamically created route%s\n"); | |
907 | p(rts_newgateway, "\t%u new gateway%s due to redirects\n"); | |
908 | p(rts_unreach, "\t%u destination%s found unreachable\n"); | |
909 | p(rts_wildcard, "\t%u use%s of a wildcard route\n"); | |
910 | #undef p | |
b7080c8e | 911 | |
7ba0088d A |
912 | if (rttrash || sflag <= 1) |
913 | printf("\t%u route%s not in table but not freed\n", | |
914 | rttrash, plural(rttrash)); | |
915 | } | |
916 | #ifndef __APPLE__ | |
b7080c8e | 917 | char * |
7ba0088d | 918 | ipx_print(struct sockaddr *sa) |
b7080c8e A |
919 | { |
920 | u_short port; | |
921 | struct servent *sp = 0; | |
922 | char *net = "", *host = ""; | |
923 | register char *p; | |
924 | register u_char *q; | |
925 | struct ipx_addr work = ((struct sockaddr_ipx *)sa)->sipx_addr; | |
926 | static char mybuf[50]; | |
927 | char cport[10], chost[15], cnet[15]; | |
928 | ||
929 | port = ntohs(work.x_port); | |
930 | ||
931 | if (ipx_nullnet(work) && ipx_nullhost(work)) { | |
932 | ||
933 | if (port) { | |
934 | if (sp) | |
935 | sprintf(mybuf, "*.%s", sp->s_name); | |
936 | else | |
937 | sprintf(mybuf, "*.%x", port); | |
938 | } else | |
939 | sprintf(mybuf, "*.*"); | |
940 | ||
941 | return (mybuf); | |
942 | } | |
943 | ||
944 | if (ipx_wildnet(work)) | |
945 | net = "any"; | |
946 | else if (ipx_nullnet(work)) | |
947 | net = "*"; | |
948 | else { | |
949 | q = work.x_net.c_net; | |
950 | sprintf(cnet, "%02x%02x%02x%02x", | |
951 | q[0], q[1], q[2], q[3]); | |
952 | for (p = cnet; *p == '0' && p < cnet + 8; p++) | |
953 | continue; | |
954 | net = p; | |
955 | } | |
956 | ||
957 | if (ipx_wildhost(work)) | |
958 | host = "any"; | |
959 | else if (ipx_nullhost(work)) | |
960 | host = "*"; | |
961 | else { | |
962 | q = work.x_host.c_host; | |
963 | sprintf(chost, "%02x%02x%02x%02x%02x%02x", | |
964 | q[0], q[1], q[2], q[3], q[4], q[5]); | |
965 | for (p = chost; *p == '0' && p < chost + 12; p++) | |
966 | continue; | |
967 | host = p; | |
968 | } | |
969 | ||
970 | if (port) { | |
971 | if (strcmp(host, "*") == 0) | |
972 | host = ""; | |
973 | if (sp) | |
974 | snprintf(cport, sizeof(cport), | |
975 | "%s%s", *host ? "." : "", sp->s_name); | |
976 | else | |
977 | snprintf(cport, sizeof(cport), | |
978 | "%s%x", *host ? "." : "", port); | |
979 | } else | |
980 | *cport = 0; | |
981 | ||
982 | snprintf(mybuf, sizeof(mybuf), "%s.%s%s", net, host, cport); | |
983 | return(mybuf); | |
984 | } | |
985 | ||
986 | char * | |
7ba0088d | 987 | ipx_phost(struct sockaddr *sa) |
b7080c8e A |
988 | { |
989 | register struct sockaddr_ipx *sipx = (struct sockaddr_ipx *)sa; | |
990 | struct sockaddr_ipx work; | |
991 | static union ipx_net ipx_zeronet; | |
992 | char *p; | |
993 | struct ipx_addr in; | |
994 | ||
995 | work = *sipx; | |
996 | in = work.sipx_addr; | |
997 | ||
998 | work.sipx_addr.x_port = 0; | |
999 | work.sipx_addr.x_net = ipx_zeronet; | |
1000 | p = ipx_print((struct sockaddr *)&work); | |
1001 | if (strncmp("*.", p, 2) == 0) p += 2; | |
1002 | ||
1003 | return(p); | |
1004 | } | |
1005 | #endif | |
b7080c8e A |
1006 | #ifdef NS |
1007 | short ns_nullh[] = {0,0,0}; | |
1008 | short ns_bh[] = {-1,-1,-1}; | |
1009 | ||
1010 | char * | |
7ba0088d | 1011 | ns_print(struct sockaddr *sa) |
b7080c8e A |
1012 | { |
1013 | register struct sockaddr_ns *sns = (struct sockaddr_ns*)sa; | |
1014 | struct ns_addr work; | |
1015 | union { union ns_net net_e; u_long long_e; } net; | |
1016 | u_short port; | |
1017 | static char mybuf[50], cport[10], chost[25]; | |
1018 | char *host = ""; | |
1019 | register char *p; register u_char *q; | |
1020 | ||
1021 | work = sns->sns_addr; | |
1022 | port = ntohs(work.x_port); | |
1023 | work.x_port = 0; | |
1024 | net.net_e = work.x_net; | |
1025 | if (ns_nullhost(work) && net.long_e == 0) { | |
1026 | if (port ) { | |
1027 | sprintf(mybuf, "*.%xH", port); | |
1028 | upHex(mybuf); | |
1029 | } else | |
1030 | sprintf(mybuf, "*.*"); | |
1031 | return (mybuf); | |
1032 | } | |
1033 | ||
1034 | if (bcmp(ns_bh, work.x_host.c_host, 6) == 0) { | |
1035 | host = "any"; | |
1036 | } else if (bcmp(ns_nullh, work.x_host.c_host, 6) == 0) { | |
1037 | host = "*"; | |
1038 | } else { | |
1039 | q = work.x_host.c_host; | |
1040 | sprintf(chost, "%02x%02x%02x%02x%02x%02xH", | |
1041 | q[0], q[1], q[2], q[3], q[4], q[5]); | |
1042 | for (p = chost; *p == '0' && p < chost + 12; p++) | |
1043 | continue; | |
1044 | host = p; | |
1045 | } | |
1046 | if (port) | |
1047 | sprintf(cport, ".%xH", htons(port)); | |
1048 | else | |
1049 | *cport = 0; | |
1050 | ||
1051 | sprintf(mybuf,"%xH.%s%s", ntohl(net.long_e), host, cport); | |
1052 | upHex(mybuf); | |
1053 | return(mybuf); | |
1054 | } | |
1055 | ||
1056 | char * | |
7ba0088d | 1057 | ns_phost(struct sockaddr *sa) |
b7080c8e A |
1058 | { |
1059 | register struct sockaddr_ns *sns = (struct sockaddr_ns *)sa; | |
1060 | struct sockaddr_ns work; | |
1061 | static union ns_net ns_zeronet; | |
1062 | char *p; | |
1063 | ||
1064 | work = *sns; | |
1065 | work.sns_addr.x_port = 0; | |
1066 | work.sns_addr.x_net = ns_zeronet; | |
1067 | ||
1068 | p = ns_print((struct sockaddr *)&work); | |
1069 | if (strncmp("0H.", p, 3) == 0) | |
1070 | p += 3; | |
1071 | return(p); | |
1072 | } | |
1073 | #endif | |
1074 | ||
1075 | void | |
7ba0088d | 1076 | upHex(char *p0) |
b7080c8e A |
1077 | { |
1078 | register char *p = p0; | |
1079 | ||
1080 | for (; *p; p++) | |
1081 | switch (*p) { | |
1082 | ||
1083 | case 'a': | |
1084 | case 'b': | |
1085 | case 'c': | |
1086 | case 'd': | |
1087 | case 'e': | |
1088 | case 'f': | |
1089 | *p += ('A' - 'a'); | |
1090 | break; | |
1091 | } | |
1092 | } |