From fdfd5971fdda9c24d576337643161018a5281167 Mon Sep 17 00:00:00 2001 From: Apple Date: Wed, 30 Mar 2011 23:06:21 +0000 Subject: [PATCH] network_cmds-356.8.tar.gz --- alias/alias_ftp.c | 47 +- alias/alias_proxy.c | 47 +- alias/alias_smedia.c | 43 +- arp.tproj/arp.8 | 8 +- arp.tproj/arp.c | 223 ++++++- ifconfig.tproj/af_inet.c | 2 + ifconfig.tproj/af_inet6.c | 38 +- ifconfig.tproj/af_link.c | 33 +- ifconfig.tproj/ifconfig.8 | 62 +- ifconfig.tproj/ifconfig.c | 76 ++- ifconfig.tproj/ifmedia.c | 2 +- ifconfig.tproj/ifvlan.c | 6 +- ip6addrctl.tproj/ip6addrctl.8 | 126 ++++ ip6addrctl.tproj/ip6addrctl.c | 467 ++++++++++++++ ip6addrctl.tproj/ip6addrctl.conf | 12 + ip6fw.tproj/ip6fw.8 | 2 +- ip6fw.tproj/ip6fw.c | 42 +- ipfw.tproj/ipfw.8 | 36 +- ipfw.tproj/ipfw2.c | 137 ++-- kdumpd.tproj/kdumpd.c | 12 +- mtest.tproj/COPYING | 27 + mtest.tproj/mtest.8 | 175 ++++++ mtest.tproj/mtest.c | 838 +++++++++++++++++++++++++ mtest.tproj/mtest.plist | 26 + natd.tproj/natd.8 | 58 +- natd.tproj/natd.c | 59 +- ndp.tproj/ndp.8 | 4 +- ndp.tproj/ndp.c | 298 ++++++++- netstat.tproj/if.c | 274 +++++++- netstat.tproj/inet.c | 483 +++++++++++--- netstat.tproj/inet6.c | 28 +- netstat.tproj/main.c | 23 +- netstat.tproj/mbuf.c | 113 +++- netstat.tproj/mcast.c | 804 +++++++++++++++++++----- netstat.tproj/mroute.c | 10 +- netstat.tproj/mroute6.c | 10 +- netstat.tproj/netstat.1 | 26 +- netstat.tproj/netstat.h | 5 +- netstat.tproj/route.c | 29 +- netstat.tproj/unix.c | 6 +- network_cmds.xcodeproj/project.pbxproj | 388 +++++++++++- ping.tproj/ping.8 | 7 +- ping.tproj/ping.c | 86 ++- ping6.tproj/ping6.8 | 31 +- ping6.tproj/ping6.c | 144 ++++- rarpd.tproj/rarpd.c | 6 +- route.tproj/route.8 | 8 +- route.tproj/route.c | 121 ++-- rtadvd.tproj/advcap.c | 34 +- rtadvd.tproj/config.c | 151 ++++- rtadvd.tproj/dump.c | 32 +- rtadvd.tproj/rtadvd.conf.5 | 53 +- rtadvd.tproj/rtadvd.h | 12 + rtsol.tproj/dump.c | 36 +- rtsol.tproj/if.c | 32 +- rtsol.tproj/probe.c | 32 +- traceroute.tproj/findsaddr-socket.c | 42 +- traceroute.tproj/ifaddrlist.c | 42 +- traceroute.tproj/ifaddrlist.h | 30 +- traceroute.tproj/traceroute.8 | 3 +- traceroute.tproj/traceroute.c | 30 +- 61 files changed, 5292 insertions(+), 745 deletions(-) create mode 100644 ip6addrctl.tproj/ip6addrctl.8 create mode 100644 ip6addrctl.tproj/ip6addrctl.c create mode 100644 ip6addrctl.tproj/ip6addrctl.conf create mode 100644 mtest.tproj/COPYING create mode 100644 mtest.tproj/mtest.8 create mode 100644 mtest.tproj/mtest.c create mode 100644 mtest.tproj/mtest.plist diff --git a/alias/alias_ftp.c b/alias/alias_ftp.c index 9c88606..d59fbf3 100644 --- a/alias/alias_ftp.c +++ b/alias/alias_ftp.c @@ -1,24 +1,31 @@ /* - * Copyright (c) 2000-2002 Apple Computer, Inc. All rights reserved. + * Copyright (c) 2000-2009 Apple Inc. All rights reserved. * - * @APPLE_LICENSE_HEADER_START@ - * - * The contents of this file constitute Original Code as defined in and - * are subject to the Apple Public Source License Version 1.1 (the - * "License"). You may not use this file except in compliance with the - * License. Please obtain a copy of the License at - * http://www.apple.com/publicsource and read it before using this file. - * - * This Original Code and all software distributed under the License are - * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER + * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ + * + * This file contains Original Code and/or Modifications of Original Code + * as defined in and that are subject to the Apple Public Source License + * Version 2.0 (the 'License'). You may not use this file except in + * compliance with the License. The rights granted to you under the License + * may not be used to create, or enable the creation or redistribution of, + * unlawful or unlicensed copies of an Apple operating system, or to + * circumvent, violate, or enable the circumvention or violation of, any + * terms of an Apple operating system software license agreement. + * + * Please obtain a copy of the License at + * http://www.opensource.apple.com/apsl/ and read it before using this file. + * + * The Original Code and all software distributed under the License are + * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the - * License for the specific language governing rights and limitations - * under the License. - * - * @APPLE_LICENSE_HEADER_END@ + * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. + * Please see the License for the specific language governing rights and + * limitations under the License. + * + * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ */ + /*- * Copyright (c) 2001 Charles Mott * All rights reserved. @@ -535,23 +542,23 @@ NewFtpMessage(struct ip *pip, if (ftp_message_type == FTP_PORT_COMMAND) { /* Generate PORT command string. */ - sprintf(stemp, "PORT %d,%d,%d,%d,%d,%d\r\n", + snprintf(stemp, sizeof(stemp), "PORT %d,%d,%d,%d,%d,%d\r\n", a1,a2,a3,a4,p1,p2); } else { /* Generate 227 reply string. */ - sprintf(stemp, + snprintf(stemp, sizeof(stemp), "227 Entering Passive Mode (%d,%d,%d,%d,%d,%d)\r\n", a1,a2,a3,a4,p1,p2); } break; case FTP_EPRT_COMMAND: /* Generate EPRT command string. */ - sprintf(stemp, "EPRT |1|%d.%d.%d.%d|%d|\r\n", + snprintf(stemp, sizeof(stemp), "EPRT |1|%d.%d.%d.%d|%d|\r\n", a1,a2,a3,a4,ntohs(alias_port)); break; case FTP_229_REPLY: /* Generate 229 reply string. */ - sprintf(stemp, "229 Entering Extended Passive Mode (|||%d|)\r\n", + snprintf(stemp, sizeof(stemp), "229 Entering Extended Passive Mode (|||%d|)\r\n", ntohs(alias_port)); break; } diff --git a/alias/alias_proxy.c b/alias/alias_proxy.c index 3ba958f..a35ba1d 100644 --- a/alias/alias_proxy.c +++ b/alias/alias_proxy.c @@ -1,24 +1,31 @@ /* - * Copyright (c) 2000-2002 Apple Computer, Inc. All rights reserved. + * Copyright (c) 2000-2009 Apple Inc. All rights reserved. * - * @APPLE_LICENSE_HEADER_START@ - * - * The contents of this file constitute Original Code as defined in and - * are subject to the Apple Public Source License Version 1.1 (the - * "License"). You may not use this file except in compliance with the - * License. Please obtain a copy of the License at - * http://www.apple.com/publicsource and read it before using this file. - * - * This Original Code and all software distributed under the License are - * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER + * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ + * + * This file contains Original Code and/or Modifications of Original Code + * as defined in and that are subject to the Apple Public Source License + * Version 2.0 (the 'License'). You may not use this file except in + * compliance with the License. The rights granted to you under the License + * may not be used to create, or enable the creation or redistribution of, + * unlawful or unlicensed copies of an Apple operating system, or to + * circumvent, violate, or enable the circumvention or violation of, any + * terms of an Apple operating system software license agreement. + * + * Please obtain a copy of the License at + * http://www.opensource.apple.com/apsl/ and read it before using this file. + * + * The Original Code and all software distributed under the License are + * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the - * License for the specific language governing rights and limitations - * under the License. - * - * @APPLE_LICENSE_HEADER_END@ + * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. + * Please see the License for the specific language governing rights and + * limitations under the License. + * + * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ */ + /*- * Copyright (c) 2001 Charles Mott * All rights reserved. @@ -328,11 +335,11 @@ ProxyEncodeTcpStream(struct alias_link *link, switch (slen % 2) { case 0: - strcat(buffer, " \n"); + strlcat(buffer, " \n", sizeof(buffer)); slen += 2; break; case 1: - strcat(buffer, "\n"); + strlcat(buffer, "\n", sizeof(buffer)); slen += 1; } @@ -585,7 +592,7 @@ PacketAliasProxyRule(const char *cmd) cmd_len = strlen(cmd); if (cmd_len > (sizeof(buffer) - 1)) return -1; - strcpy(buffer, cmd); + strlcpy(buffer, cmd, sizeof(buffer)); /* Convert to lower case */ len = strlen(buffer); @@ -661,7 +668,7 @@ PacketAliasProxyRule(const char *cmd) break; case STATE_READ_PORT: - strcpy(str_port, token); + strlcpy(str_port, token, sizeof(str_port)); state = STATE_READ_KEYWORD; break; diff --git a/alias/alias_smedia.c b/alias/alias_smedia.c index fad6f78..14d9f5a 100644 --- a/alias/alias_smedia.c +++ b/alias/alias_smedia.c @@ -1,24 +1,31 @@ /* - * Copyright (c) 2000-2002 Apple Computer, Inc. All rights reserved. + * Copyright (c) 2000-2009 Apple Inc. All rights reserved. * - * @APPLE_LICENSE_HEADER_START@ - * - * The contents of this file constitute Original Code as defined in and - * are subject to the Apple Public Source License Version 1.1 (the - * "License"). You may not use this file except in compliance with the - * License. Please obtain a copy of the License at - * http://www.apple.com/publicsource and read it before using this file. - * - * This Original Code and all software distributed under the License are - * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER + * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ + * + * This file contains Original Code and/or Modifications of Original Code + * as defined in and that are subject to the Apple Public Source License + * Version 2.0 (the 'License'). You may not use this file except in + * compliance with the License. The rights granted to you under the License + * may not be used to create, or enable the creation or redistribution of, + * unlawful or unlicensed copies of an Apple operating system, or to + * circumvent, violate, or enable the circumvention or violation of, any + * terms of an Apple operating system software license agreement. + * + * Please obtain a copy of the License at + * http://www.opensource.apple.com/apsl/ and read it before using this file. + * + * The Original Code and all software distributed under the License are + * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the - * License for the specific language governing rights and limitations - * under the License. - * - * @APPLE_LICENSE_HEADER_END@ + * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. + * Please see the License for the specific language governing rights and + * limitations under the License. + * + * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ */ + /* * alias_smedia.c * @@ -299,7 +306,7 @@ alias_rtsp_out(struct ip *pip, size_t lentmp; /* Copy into IP packet */ - sprintf(stemp, "%d", ntohs(salias)); + snprintf(stemp, sizeof(stemp), "%d", ntohs(salias)); lentmp = strlen(stemp); /* account for ending ';' */ if (port_newdata + lentmp + 1 > newdata + sizeof(newdata)) { @@ -312,7 +319,7 @@ alias_rtsp_out(struct ip *pip, port_newdata += lentmp; if (eport != 0) { - sprintf(stemp, "%d", ntohs(ealias)); + snprintf(stemp, sizeof(stemp), "%d", ntohs(ealias)); lentmp = strlen(stemp); /* account for middle '-' and for ending ';' */ diff --git a/arp.tproj/arp.8 b/arp.tproj/arp.8 index 6b62477..ab31601 100644 --- a/arp.tproj/arp.8 +++ b/arp.tproj/arp.8 @@ -42,6 +42,7 @@ .Nm .Op Fl n .Op Fl i Ar interface +.Op Fl l .Fl a .Nm .Fl d Ar hostname @@ -115,6 +116,8 @@ entries on .Ar interface . Applicable only to the following operations: display one, display all, delete all. +.It Fl l +Show link-layer reachability information. .It Fl n Show network addresses as numbers (normally .Nm @@ -208,9 +211,10 @@ character will mark the rest of the line as a comment. .Sh SEE ALSO .Xr inet 3 , .Xr arp 4 , -.Xr ifconfig 8 +.Xr ifconfig 8 , +.Xr ndp 8 .Sh HISTORY The .Nm utility appeared in -.Bx 4.3 . \ No newline at end of file +.Bx 4.3 . diff --git a/arp.tproj/arp.c b/arp.tproj/arp.c index 0b5c583..665aa94 100644 --- a/arp.tproj/arp.c +++ b/arp.tproj/arp.c @@ -1,3 +1,31 @@ +/* + * Copyright (c) 2003-2011 Apple Inc. All rights reserved. + * + * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ + * + * This file contains Original Code and/or Modifications of Original Code + * as defined in and that are subject to the Apple Public Source License + * Version 2.0 (the 'License'). You may not use this file except in + * compliance with the License. The rights granted to you under the License + * may not be used to create, or enable the creation or redistribution of, + * unlawful or unlicensed copies of an Apple operating system, or to + * circumvent, violate, or enable the circumvention or violation of, any + * terms of an Apple operating system software license agreement. + * + * Please obtain a copy of the License at + * http://www.opensource.apple.com/apsl/ and read it before using this file. + * + * The Original Code and all software distributed under the License are + * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER + * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, + * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. + * Please see the License for the specific language governing rights and + * limitations under the License. + * + * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ + */ + /* * Copyright (c) 1984, 1993 * The Regents of the University of California. All rights reserved. @@ -84,11 +112,16 @@ __FBSDID("$FreeBSD: src/usr.sbin/arp/arp.c,v 1.65.2.1 2008/04/25 16:38:14 sam Ex typedef void (action_fn)(struct sockaddr_dl *sdl, struct sockaddr_inarp *s_in, struct rt_msghdr *rtm); +typedef void (action_ext_fn)(struct sockaddr_dl *sdl, + struct sockaddr_inarp *s_in, struct rt_msghdr_ext *rtm); static int search(in_addr_t addr, action_fn *action); +static int search_ext(in_addr_t addr, action_ext_fn *action); static action_fn print_entry; static action_fn nuke_entry; +static action_ext_fn print_entry_ext; +static char *print_lladdr(struct sockaddr_dl *); static int delete(char *host, int do_proxy); static void usage(void); static int set(int argc, char **argv); @@ -99,6 +132,7 @@ static struct rt_msghdr *rtmsg(int cmd, static int get_ether_addr(in_addr_t ipaddr, struct ether_addr *hwaddr); static struct sockaddr_inarp *getaddr(char *host); static int valid_type(int type); +static char *sec2str(time_t); static int nflag; /* no reverse dns lookups */ static char *rifname; @@ -131,8 +165,10 @@ main(int argc, char *argv[]) int ch, func = 0; int rtn = 0; int aflag = 0; /* do it for all entries */ + int lflag = 0; + uint32_t ifindex = 0; - while ((ch = getopt(argc, argv, "andfsSi:")) != -1) + while ((ch = getopt(argc, argv, "andflsSi:")) != -1) switch((char)ch) { case 'a': aflag = 1; @@ -143,6 +179,9 @@ main(int argc, char *argv[]) case 'n': nflag = 1; break; + case 'l': + lflag = 1; + break; case 'S': SETFUNC(F_REPLACE); break; @@ -167,7 +206,7 @@ main(int argc, char *argv[]) if (rifname) { if (func != F_GET && !(func == F_DELETE && aflag)) errx(1, "-i not applicable to this operation"); - if (if_nametoindex(rifname) == 0) { + if ((ifindex = if_nametoindex(rifname)) == 0) { if (errno == ENXIO) errx(1, "interface %s does not exist", rifname); else @@ -179,7 +218,15 @@ main(int argc, char *argv[]) if (aflag) { if (argc != 0) usage(); - search(0, print_entry); + if (lflag) { + printf("%-23s %-17s %-9.9s %-9.9s %8.8s %4s " + "%4s\n", "Neighbor", "Linklayer Address", + "Expire(O)", "Expire(I)", "Netif", "Refs", + "Prbs"); + search_ext(0, print_entry_ext); + } else { + search(0, print_entry); + } } else { if (argc != 1) usage(); @@ -218,6 +265,8 @@ main(int argc, char *argv[]) usage(); } } + if (i > argc) + usage(); rtn = delete(argv[0], do_proxy); } break; @@ -570,20 +619,23 @@ search(in_addr_t addr, action_fn *action) /* * Stolen and adapted from ifconfig */ -static void +static char * print_lladdr(struct sockaddr_dl *sdl) { + static char buf[256]; char *cp; - int n; + int n, bufsize = sizeof (buf), p = 0; + bzero(buf, sizeof (buf)); cp = (char *)LLADDR(sdl); if ((n = sdl->sdl_alen) > 0) { while (--n >= 0) - printf("%x%s",*cp++ & 0xff, n>0? ":" : ""); + p += snprintf(buf + p, bufsize - p, "%x%s", + *cp++ & 0xff, n > 0 ? ":" : ""); } + return (buf); } - /* * Display an arp entry */ @@ -614,7 +666,7 @@ print_entry(struct sockaddr_dl *sdl, printf("%s (%s) at ", host, inet_ntoa(addr->sin_addr)); if (sdl->sdl_alen) { #if 1 - print_lladdr(sdl); + printf("%s", print_lladdr(sdl)); #else if ((sdl->sdl_type == IFT_ETHER || sdl->sdl_type == IFT_L2VLAN || @@ -692,12 +744,18 @@ print_entry(struct sockaddr_dl *sdl, */ static void nuke_entry(struct sockaddr_dl *sdl __unused, - struct sockaddr_inarp *addr, struct rt_msghdr *rtm __unused) + struct sockaddr_inarp *addr, struct rt_msghdr *rtm) { char ip[20]; snprintf(ip, sizeof(ip), "%s", inet_ntoa(addr->sin_addr)); + /* + * When deleting all entries, specify the interface scope of each entry + */ + if ((rtm->rtm_flags & RTF_IFSCOPE)) + ifscope = rtm->rtm_index; (void)delete(ip, 0); + ifscope = 0; } static void @@ -705,7 +763,7 @@ usage(void) { fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n", "usage: arp [-n] [-i interface] hostname", - " arp [-n] [-i interface] -a", + " arp [-n] [-i interface] [-l] -a", " arp -d hostname [pub] [ifscope interface]", " arp -d [-i interface] -a", " arp -s hostname ether_addr [temp] [reject] [blackhole] [pub [only]] [ifscope interface]", @@ -907,3 +965,148 @@ done: close(sock); return (retval); } + +static char * +sec2str(total) + time_t total; +{ + static char result[256]; + int days, hours, mins, secs; + int first = 1; + char *p = result; + + days = total / 3600 / 24; + hours = (total / 3600) % 24; + mins = (total / 60) % 60; + secs = total % 60; + + if (days) { + first = 0; + p += snprintf(p, sizeof(result) - (p - result), "%dd", days); + } + if (!first || hours) { + first = 0; + p += snprintf(p, sizeof(result) - (p - result), "%dh", hours); + } + if (!first || mins) { + first = 0; + p += snprintf(p, sizeof(result) - (p - result), "%dm", mins); + } + snprintf(p, sizeof(result) - (p - result), "%ds", secs); + + return(result); +} + +static int +search_ext(in_addr_t addr, action_ext_fn *action) +{ + int mib[6]; + size_t needed; + char *lim, *buf, *newbuf, *next; + struct rt_msghdr_ext *ertm; + struct sockaddr_inarp *sin2; + struct sockaddr_dl *sdl; + char ifname[IF_NAMESIZE]; + int st, found_entry = 0; + + mib[0] = CTL_NET; + mib[1] = PF_ROUTE; + mib[2] = 0; + mib[3] = AF_INET; + mib[4] = NET_RT_DUMPX_FLAGS; + mib[5] = RTF_LLINFO; + if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0) + err(1, "route-sysctl-estimate"); + if (needed == 0) /* empty table */ + return 0; + buf = NULL; + for (;;) { + newbuf = realloc(buf, needed); + if (newbuf == NULL) { + if (buf != NULL) + free(buf); + errx(1, "could not reallocate memory"); + } + buf = newbuf; + st = sysctl(mib, 6, buf, &needed, NULL, 0); + if (st == 0 || errno != ENOMEM) + break; + needed += needed / 8; + } + if (st == -1) + err(1, "actual retrieval of routing table"); + lim = buf + needed; + for (next = buf; next < lim; next += ertm->rtm_msglen) { + ertm = (struct rt_msghdr_ext *)next; + sin2 = (struct sockaddr_inarp *)(ertm + 1); + sdl = (struct sockaddr_dl *)((char *)sin2 + SA_SIZE(sin2)); + if (rifname && if_indextoname(sdl->sdl_index, ifname) && + strcmp(ifname, rifname)) + continue; + if (addr) { + if (addr != sin2->sin_addr.s_addr) + continue; + found_entry = 1; + } + (*action)(sdl, sin2, ertm); + } + free(buf); + return (found_entry); +} + +static void +print_entry_ext(struct sockaddr_dl *sdl, struct sockaddr_inarp *addr, + struct rt_msghdr_ext *ertm) +{ + const char *host; + struct hostent *hp; + char ifname[IF_NAMESIZE]; + struct timeval time; + + if (nflag == 0) + hp = gethostbyaddr((caddr_t)&(addr->sin_addr), + sizeof (addr->sin_addr), AF_INET); + else + hp = 0; + + if (hp) + host = hp->h_name; + else + host = inet_ntoa(addr->sin_addr); + + printf("%-23s ", host); + + if (sdl->sdl_alen) + printf("%-17s ", print_lladdr(sdl)); + else + printf("%-17s ", "(incomplete)"); + + gettimeofday(&time, 0); + + if (ertm->rtm_ri.ri_refcnt == 0 || ertm->rtm_ri.ri_snd_expire == 0) + printf("%-9.9s ", "(none)"); + else if (ertm->rtm_ri.ri_snd_expire > time.tv_sec) + printf("%-9.9s ", + sec2str(ertm->rtm_ri.ri_snd_expire - time.tv_sec)); + else + printf("%-9.9s ", "expired"); + + if (ertm->rtm_ri.ri_refcnt == 0 || ertm->rtm_ri.ri_rcv_expire == 0) + printf("%-9.9s", "(none)"); + else if (ertm->rtm_ri.ri_rcv_expire > time.tv_sec) + printf("%-9.9s", + sec2str(ertm->rtm_ri.ri_rcv_expire - time.tv_sec)); + else + printf("%-9.9s", "expired"); + + if (if_indextoname(sdl->sdl_index, ifname) == NULL) + snprintf(ifname, sizeof (ifname), "%s", "?"); + printf(" %8.8s", ifname); + + if (ertm->rtm_ri.ri_refcnt) { + printf(" %4d", ertm->rtm_ri.ri_refcnt); + if (ertm->rtm_ri.ri_probes) + printf(" %4d", ertm->rtm_ri.ri_probes); + } + printf("\n"); +} diff --git a/ifconfig.tproj/af_inet.c b/ifconfig.tproj/af_inet.c index 028951c..ccac9b0 100644 --- a/ifconfig.tproj/af_inet.c +++ b/ifconfig.tproj/af_inet.c @@ -97,7 +97,9 @@ static struct sockaddr_in *sintab[] = { static void in_getaddr(const char *s, int which) { +#ifndef MIN #define MIN(a,b) ((a)<(b)?(a):(b)) +#endif /* MIN */ struct sockaddr_in *sin = sintab[which]; struct hostent *hp; struct netent *np; diff --git a/ifconfig.tproj/af_inet6.c b/ifconfig.tproj/af_inet6.c index 54d039a..cd5e34a 100644 --- a/ifconfig.tproj/af_inet6.c +++ b/ifconfig.tproj/af_inet6.c @@ -1,3 +1,31 @@ +/* + * Copyright (c) 2009 Apple Inc. All rights reserved. + * + * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ + * + * This file contains Original Code and/or Modifications of Original Code + * as defined in and that are subject to the Apple Public Source License + * Version 2.0 (the 'License'). You may not use this file except in + * compliance with the License. The rights granted to you under the License + * may not be used to create, or enable the creation or redistribution of, + * unlawful or unlicensed copies of an Apple operating system, or to + * circumvent, violate, or enable the circumvention or violation of, any + * terms of an Apple operating system software license agreement. + * + * Please obtain a copy of the License at + * http://www.opensource.apple.com/apsl/ and read it before using this file. + * + * The Original Code and all software distributed under the License are + * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER + * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, + * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. + * Please see the License for the specific language governing rights and + * limitations under the License. + * + * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ + */ + /* * Copyright (c) 1983, 1993 * The Regents of the University of California. All rights reserved. @@ -412,19 +440,19 @@ sec2str(time_t total) if (days) { first = 0; - p += sprintf(p, "%dd", days); + p += snprintf(p, sizeof(result) - (p - result), "%dd", days); } if (!first || hours) { first = 0; - p += sprintf(p, "%dh", hours); + p += snprintf(p, sizeof(result) - (p - result), "%dh", hours); } if (!first || mins) { first = 0; - p += sprintf(p, "%dm", mins); + p += snprintf(p, sizeof(result) - (p - result), "%dm", mins); } - sprintf(p, "%ds", secs); + snprintf(p, sizeof(result) - (p - result), "%ds", secs); } else - sprintf(result, "%lu", (unsigned long)total); + snprintf(result, sizeof(result), "%lu", (unsigned long)total); return(result); } diff --git a/ifconfig.tproj/af_link.c b/ifconfig.tproj/af_link.c index ea42ff4..b855242 100644 --- a/ifconfig.tproj/af_link.c +++ b/ifconfig.tproj/af_link.c @@ -1,3 +1,31 @@ +/* + * Copyright (c) 2009 Apple Inc. All rights reserved. + * + * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ + * + * This file contains Original Code and/or Modifications of Original Code + * as defined in and that are subject to the Apple Public Source License + * Version 2.0 (the 'License'). You may not use this file except in + * compliance with the License. The rights granted to you under the License + * may not be used to create, or enable the creation or redistribution of, + * unlawful or unlicensed copies of an Apple operating system, or to + * circumvent, violate, or enable the circumvention or violation of, any + * terms of an Apple operating system software license agreement. + * + * Please obtain a copy of the License at + * http://www.opensource.apple.com/apsl/ and read it before using this file. + * + * The Original Code and all software distributed under the License are + * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER + * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, + * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. + * Please see the License for the specific language governing rights and + * limitations under the License. + * + * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ + */ + /* * Copyright (c) 1983, 1993 * The Regents of the University of California. All rights reserved. @@ -89,13 +117,14 @@ link_getaddr(const char *addr, int which) char *temp; struct sockaddr_dl sdl; struct sockaddr *sa = &link_ridreq.ifr_addr; + size_t slen = strlen(addr); if (which != ADDR) errx(1, "can't set link-level netmask or broadcast"); - if ((temp = malloc(strlen(addr) + 2)) == NULL) + if ((temp = malloc(slen + 2)) == NULL) errx(1, "malloc failed"); temp[0] = ':'; - strcpy(temp + 1, addr); + strlcpy(temp + 1, addr, slen + 1); sdl.sdl_len = sizeof(sdl); link_addr(temp, &sdl); free(temp); diff --git a/ifconfig.tproj/ifconfig.8 b/ifconfig.tproj/ifconfig.8 index 1876828..5648409 100644 --- a/ifconfig.tproj/ifconfig.8 +++ b/ifconfig.tproj/ifconfig.8 @@ -120,7 +120,7 @@ slash notation) to include the netmask. That is, one can specify an address like .Li 192.168.0.1/16 . .Pp -For +For the .Dq inet6 family, it is also possible to specify the prefix length using the slash notation, like @@ -266,7 +266,6 @@ When an interface is marked the system will not attempt to transmit messages through that interface. If possible, the interface will be reset to disable reception as well. -This action does not automatically disable routes using the interface. .It Cm ether Another name for the .Cm lladdr @@ -298,15 +297,15 @@ of the interface to .Ar type . Some interfaces support the mutually exclusive use of one of several different physical media connectors. -For example, a 10Mb/s Ethernet +For example, a 10Mbit/s Ethernet interface might support the use of either .Tn AUI or twisted pair connectors. Setting the media type to -.Dq 10base5/AUI +.Cm 10base5/AUI would change the currently active connector to the AUI port. Setting it to -.Dq 10baseT/UTP +.Cm 10baseT/UTP would activate twisted pair. Refer to the interfaces' driver specific documentation or man page for a complete list of the @@ -323,6 +322,59 @@ list of available options. .It Fl mediaopt Ar opts If the driver supports the media selection system, disable the specified media options on the interface. +.It Cm rxcsum , txcsum +If the driver supports user-configurable checksum offloading, +enable receive (or transmit) checksum offloading on the interface. +Some drivers may not be able to enable these flags independently +of each other, so setting one may also set the other. +The driver will offload as much checksum work as it can reliably +support, the exact level of offloading varies between drivers. +.It Fl rxcsum , txcsum +If the driver supports user-configurable checksum offloading, +disable receive (or transmit) checksum offloading on the interface. +These settings may not always be independent of each other. +.It Cm tso +If the driver supports +.Xr tcp 4 +segmentation offloading, enable TSO on the interface. +Some drivers may not be able to support TSO for +.Xr ip 4 +and +.Xr ip6 4 +packets, so they may enable only one of them. +.It Fl tso +If the driver supports +.Xr tcp 4 +segmentation offloading, disable TSO on the interface. +It will always disable TSO for +.Xr ip 4 +and +.Xr ip6 4 . +.It Cm lro +If the driver supports +.Xr tcp 4 +large receive offloading, enable LRO on the interface. +.It Fl lro +If the driver supports +.Xr tcp 4 +large receive offloading, disable LRO on the interface. +.It Cm av +If supported by the driver, enable 802.1 AVB on the interface. +.It Fl av +If supported by the driver, disable 802.1 AVB on the interface. +.It Cm vlanmtu , vlanhwtag +If the driver offers user-configurable VLAN support, enable +reception of extended frames or tag processing in hardware, +respectively. +Note that this must be issued on a physical interface associated with +.Xr vlan 4 , +not on a +.Xr vlan 4 +interface itself. +.It Fl vlanmtu , vlanhwtag +If the driver offers user-configurable VLAN support, disable +reception of extended frames or tag processing in hardware, +respectively. .It Cm create Create the specified network pseudo-device. If the interface is given without a unit number, try to create a new diff --git a/ifconfig.tproj/ifconfig.c b/ifconfig.tproj/ifconfig.c index 62bfd19..9346350 100644 --- a/ifconfig.tproj/ifconfig.c +++ b/ifconfig.tproj/ifconfig.c @@ -1,3 +1,31 @@ +/* + * Copyright (c) 2009 Apple Inc. All rights reserved. + * + * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ + * + * This file contains Original Code and/or Modifications of Original Code + * as defined in and that are subject to the Apple Public Source License + * Version 2.0 (the 'License'). You may not use this file except in + * compliance with the License. The rights granted to you under the License + * may not be used to create, or enable the creation or redistribution of, + * unlawful or unlicensed copies of an Apple operating system, or to + * circumvent, violate, or enable the circumvention or violation of, any + * terms of an Apple operating system software license agreement. + * + * Please obtain a copy of the License at + * http://www.opensource.apple.com/apsl/ and read it before using this file. + * + * The Original Code and all software distributed under the License are + * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER + * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, + * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. + * Please see the License for the specific language governing rights and + * limitations under the License. + * + * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ + */ + /* * Copyright (c) 1983, 1993 * The Regents of the University of California. All rights reserved. @@ -726,7 +754,7 @@ setifflags(const char *vname, int value, int s, const struct afswtch *afp) Perror(vname); } -#ifndef __APPLE__ +#ifdef SIOCGIFCAP void setifcap(const char *vname, int value, int s, const struct afswtch *afp) { @@ -795,11 +823,11 @@ setifname(const char *val, int dummy __unused, int s, #define IFFBITS \ "\020\1UP\2BROADCAST\3DEBUG\4LOOPBACK\5POINTOPOINT\6SMART\7RUNNING" \ "\10NOARP\11PROMISC\12ALLMULTI\13OACTIVE\14SIMPLEX\15LINK0\16LINK1\17LINK2" \ -"\20MULTICAST\22PPROMISC\23MONITOR\24STATICARP\25NEEDSGIANT" +"\20MULTICAST" #define IFCAPBITS \ -"\020\1RXCSUM\2TXCSUM\3NETCONS\4VLAN_MTU\5VLAN_HWTAGGING\6JUMBO_MTU\7POLLING" \ -"\10VLAN_HWCSUM\11TSO4\12TSO6\13LRO\14WOL_UCAST\15WOL_MCAST\16WOL_MAGIC" +"\020\1RXCSUM\2TXCSUM\3VLAN_MTU\4VLAN_HWTAGGING\5JUMBO_MTU" \ +"\6TSO4\7TSO6\10LRO\11AV" /* * Print the status of the interface. If an address family was @@ -839,7 +867,7 @@ status(const struct afswtch *afp, const struct sockaddr_dl *sdl, #endif putchar('\n'); -#ifndef __APPLE__ +#ifdef SIOCGIFCAP if (ioctl(s, SIOCGIFCAP, (caddr_t)&ifr) == 0) { if (ifr.ifr_curcap != 0) { printb("\toptions", ifr.ifr_curcap, IFCAPBITS); @@ -975,7 +1003,7 @@ ifmaybeload(const char *name) } /* turn interface and unit into module name */ - strcpy(ifkind, "if_"); + strlcpy(ifkind, "if_", sizeof(ifkind)); strlcpy(ifkind + MOD_PREFIX_LEN, ifname, sizeof(ifkind) - MOD_PREFIX_LEN); @@ -1012,10 +1040,10 @@ static struct cmd basic_cmds[] = { DEF_CMD("-arp", IFF_NOARP, setifflags), DEF_CMD("debug", IFF_DEBUG, setifflags), DEF_CMD("-debug", -IFF_DEBUG, setifflags), -#ifdef notdef +#ifdef IFF_PPROMISC DEF_CMD("promisc", IFF_PPROMISC, setifflags), DEF_CMD("-promisc", -IFF_PPROMISC, setifflags), -#endif +#endif /* IFF_PPROMISC */ DEF_CMD("add", IFF_UP, notealias), DEF_CMD("alias", IFF_UP, notealias), DEF_CMD("-alias", -IFF_UP, notealias), @@ -1039,39 +1067,65 @@ static struct cmd basic_cmds[] = { DEF_CMD("-link1", -IFF_LINK1, setifflags), DEF_CMD("link2", IFF_LINK2, setifflags), DEF_CMD("-link2", -IFF_LINK2, setifflags), -#ifdef notdef +#ifdef IFF_MONITOR DEF_CMD("monitor", IFF_MONITOR:, setifflags), DEF_CMD("-monitor", -IFF_MONITOR, setifflags), +#endif /* IFF_MONITOR */ +#ifdef IFF_STATICARP DEF_CMD("staticarp", IFF_STATICARP, setifflags), DEF_CMD("-staticarp", -IFF_STATICARP, setifflags), +#endif /* IFF_STATICARP */ +#ifdef IFCAP_RXCSUM DEF_CMD("rxcsum", IFCAP_RXCSUM, setifcap), DEF_CMD("-rxcsum", -IFCAP_RXCSUM, setifcap), +#endif /* IFCAP_RXCSUM */ +#ifdef IFCAP_TXCSUM DEF_CMD("txcsum", IFCAP_TXCSUM, setifcap), DEF_CMD("-txcsum", -IFCAP_TXCSUM, setifcap), +#endif /* IFCAP_TXCSUM */ +#ifdef IFCAP_NETCONS DEF_CMD("netcons", IFCAP_NETCONS, setifcap), DEF_CMD("-netcons", -IFCAP_NETCONS, setifcap), +#endif /* IFCAP_NETCONS */ +#ifdef IFCAP_POLLING DEF_CMD("polling", IFCAP_POLLING, setifcap), DEF_CMD("-polling", -IFCAP_POLLING, setifcap), +#endif /* IFCAP_POLLING */ +#ifdef IFCAP_TSO DEF_CMD("tso", IFCAP_TSO, setifcap), DEF_CMD("-tso", -IFCAP_TSO, setifcap), +#endif /* IFCAP_TSO */ +#ifdef IFCAP_LRO DEF_CMD("lro", IFCAP_LRO, setifcap), DEF_CMD("-lro", -IFCAP_LRO, setifcap), +#endif /* IFCAP_LRO */ +#ifdef IFCAP_WOL DEF_CMD("wol", IFCAP_WOL, setifcap), DEF_CMD("-wol", -IFCAP_WOL, setifcap), +#endif /* IFCAP_WOL */ +#ifdef IFCAP_WOL_UCAST DEF_CMD("wol_ucast", IFCAP_WOL_UCAST, setifcap), DEF_CMD("-wol_ucast", -IFCAP_WOL_UCAST, setifcap), +#endif /* IFCAP_WOL_UCAST */ +#ifdef IFCAP_WOL_MCAST DEF_CMD("wol_mcast", IFCAP_WOL_MCAST, setifcap), DEF_CMD("-wol_mcast", -IFCAP_WOL_MCAST, setifcap), +#endif /* IFCAP_WOL_MCAST */ +#ifdef IFCAP_WOL_MAGIC DEF_CMD("wol_magic", IFCAP_WOL_MAGIC, setifcap), DEF_CMD("-wol_magic", -IFCAP_WOL_MAGIC, setifcap), -#endif +#endif /* IFCAP_WOL_MAGIC */ DEF_CMD("normal", -IFF_LINK0, setifflags), DEF_CMD("compress", IFF_LINK0, setifflags), DEF_CMD("noicmp", IFF_LINK1, setifflags), DEF_CMD_ARG("mtu", setifmtu), #ifdef notdef DEF_CMD_ARG("name", setifname), -#endif +#endif /* notdef */ +#ifdef IFCAP_AV + DEF_CMD("av", IFCAP_AV, setifcap), + DEF_CMD("-av", -IFCAP_AV, setifcap), +#endif /* IFCAP_AV */ }; static __constructor void diff --git a/ifconfig.tproj/ifmedia.c b/ifconfig.tproj/ifmedia.c index f525154..5c36406 100644 --- a/ifconfig.tproj/ifmedia.c +++ b/ifconfig.tproj/ifmedia.c @@ -402,7 +402,7 @@ static struct ifmedia_description ifm_subtype_ieee80211_option_descriptions[] = #ifdef notdef static struct ifmedia_description ifm_subtype_ieee80211_aliases[] = - IFM_SUBTYPE_IEEE80211_ALIASES; +IFM_SUBTYPE_IEEE80211_ALIASES; struct ifmedia_description ifm_subtype_ieee80211_mode_descriptions[] = IFM_SUBTYPE_IEEE80211_MODE_DESCRIPTIONS; diff --git a/ifconfig.tproj/ifvlan.c b/ifconfig.tproj/ifvlan.c index eec3ef1..8e78ccb 100644 --- a/ifconfig.tproj/ifvlan.c +++ b/ifconfig.tproj/ifvlan.c @@ -181,12 +181,14 @@ static struct cmd vlan_cmds[] = { DEF_CLONE_CMD_ARG("vlandev", setvlandev), /* XXX For compatibility. Should become DEF_CMD() some day. */ DEF_CMD_OPTARG("-vlandev", unsetvlandev), -#ifdef notdef +#ifdef IFCAP_VLAN_MTU DEF_CMD("vlanmtu", IFCAP_VLAN_MTU, setifcap), DEF_CMD("-vlanmtu", -IFCAP_VLAN_MTU, setifcap), +#endif /* IFCAP_VLAN_MTU */ +#ifdef IFCAP_VLAN_HWTAGGING DEF_CMD("vlanhwtag", IFCAP_VLAN_HWTAGGING, setifcap), DEF_CMD("-vlanhwtag", -IFCAP_VLAN_HWTAGGING, setifcap), -#endif +#endif /* IFCAP_VLAN_HWTAGGING */ }; static struct afswtch af_vlan = { .af_name = "af_vlan", diff --git a/ip6addrctl.tproj/ip6addrctl.8 b/ip6addrctl.tproj/ip6addrctl.8 new file mode 100644 index 0000000..59154d4 --- /dev/null +++ b/ip6addrctl.tproj/ip6addrctl.8 @@ -0,0 +1,126 @@ +.\" $KAME: ip6addrctl.8,v 1.3 2003/03/22 05:56:41 jinmei Exp $ +.\" +.\" Copyright (C) 2001 WIDE Project. +.\" All rights reserved. +.\" +.\" Redistribution and use in source and binary forms, with or without +.\" modification, are permitted provided that the following conditions +.\" are met: +.\" 1. Redistributions of source code must retain the above copyright +.\" notice, this list of conditions and the following disclaimer. +.\" 2. Redistributions in binary form must reproduce the above copyright +.\" notice, this list of conditions and the following disclaimer in the +.\" documentation and/or other materials provided with the distribution. +.\" 3. Neither the name of the project nor the names of its contributors +.\" may be used to endorse or promote products derived from this software +.\" without specific prior written permission. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND +.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +.\" ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE +.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS +.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY +.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +.\" SUCH DAMAGE. +.\" +.\" $FreeBSD$ +.\" +.Dd September 25, 2001 +.Dt IP6ADDRCTL 8 +.Os +.\" +.Sh NAME +.Nm ip6addrctl +.Nd configure address selection policy for IPv6 and IPv4 +.\" +.Sh SYNOPSIS +.Nm +.Op Cm show +.Nm +.Cm add +.Ar prefix precedence label +.Nm +.Cm delete +.Ar prefix +.Nm +.Cm flush +.Nm +.Cm install +.Ar configfile +.\" +.Sh DESCRIPTION +The +.Nm +utility manages the policy table of source and destination address +selection for outgoing IPv4 and IPv6 packets. +When +.Nm +is invoked without an argument or with a single argument +.Cm show , +it prints the content of the policy table currently installed in the +kernel. +.Pp +To modify the table, the following operations are available: +.Bl -tag -width indent +.It Cm add Ar prefix precedence label +Add a policy entry. +The +.Ar prefix +argument +is an IPv6 prefix, which is a key for the entry. +An IPv4 prefix should be specified with an IPv6 prefix using an +IPv4-mapped IPv6 address. +The +.Ar precedence +and +.Ar label +arguments +are decimal numbers, which specify the precedence and label values +for the entry, respectively. +This operation should be performed without an existing entry for the +prefix. +.It Cm delete Ar prefix +Delete a policy entry specified by +.Ar prefix , +which should be an IPv6 prefix. +A corresponding entry for the prefix should have already been +installed. +.It Cm flush +Delete all existing policy entries in the kernel. +.It Cm install Ar configfile +Install policy entries from a configuration file named +.Ar configfile . +The configuration file should contain a set of policy entries. +Each entry is specified in a single line which contains an IPv6 prefix, +a decimal precedence value, and a decimal label value, separated with +white space or tab characters. +In the configuration file, lines beginning with the pound-sign +.Pq Ql # +are +comments and are ignored. +.El +.\" +.Sh EXIT STATUS +.Ex -std +.\" +.Sh SEE ALSO +.Rs +.%A "Richard Draves" +.%T "Default Address Selection for IPv6" +.%N RFC 3484 +.Re +.\" +.Sh HISTORY +The +.Nm +utility first appeared in the KAME IPv6 protocol stack kit. +The original command name was +.Nm addrselect , +but it was then renamed to the current one so that the name would +describe its function well. +.\" .Sh BUGS +.\" (to be written) diff --git a/ip6addrctl.tproj/ip6addrctl.c b/ip6addrctl.tproj/ip6addrctl.c new file mode 100644 index 0000000..400412a --- /dev/null +++ b/ip6addrctl.tproj/ip6addrctl.c @@ -0,0 +1,467 @@ +/* $KAME: ip6addrctl.c,v 1.3 2003/12/16 08:14:28 suz Exp $ */ + +/* + * Copyright (C) 2001 WIDE Project. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the project nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * $FreeBSD$ + */ + +#include +#include +#include +#include +#include +#include + +#include +#include + +#include +#include + +#include +#include +#include +#include +#include +#include +#include + +static char *configfile; + +struct policyqueue { + TAILQ_ENTRY(policyqueue) pc_entry; + struct in6_addrpolicy pc_policy; +}; +TAILQ_HEAD(policyhead, policyqueue); +struct policyhead policyhead; + +static void usage __P((void)); +static void get_policy __P((void)); +static void dump_policy __P((void)); +static int mask2plen __P((struct sockaddr_in6 *)); +static int parse_prefix __P((const char *, struct in6_addrpolicy *)); +static void make_policy_fromfile __P((char *)); +static void plen2mask __P((struct sockaddr_in6 *, int)); +static void set_policy __P((void)); +static void add_policy __P((char *, char *, char *)); +static void delete_policy __P((char *)); +static void flush_policy __P(()); + +int +main(argc, argv) + int argc; + char *argv[]; +{ + TAILQ_INIT(&policyhead); + + if (argc == 1 || strcasecmp(argv[1], "show") == 0) { + get_policy(); + dump_policy(); + } else if (strcasecmp(argv[1], "add") == 0) { + if (argc < 5) + usage(); + add_policy(argv[2], argv[3], argv[4]); + } else if (strcasecmp(argv[1], "delete") == 0) { + if (argc < 3) + usage(); + delete_policy(argv[2]); + } else if (strcasecmp(argv[1], "flush") == 0) { + get_policy(); + flush_policy(); + } else if (strcasecmp(argv[1], "install") == 0) { + if (argc < 3) + usage(); + configfile = argv[2]; + make_policy_fromfile(configfile); + set_policy(); + } else + usage(); + + exit(0); +} + +static void +get_policy() +{ + int mib[] = { CTL_NET, PF_INET6, IPPROTO_IPV6, IPV6CTL_ADDRCTLPOLICY }; + size_t l; + char *buf; + struct in6_addrpolicy *pol, *ep; + + if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), NULL, &l, NULL, 0) < 0) { + err(1, "sysctl(IPV6CTL_ADDRCTLPOLICY)"); + /* NOTREACHED */ + } + if (l == 0) { + printf("no source-address-selection policy is installed\n"); + return; + } + if ((buf = malloc(l)) == NULL) { + errx(1, "malloc failed"); + /* NOTREACHED */ + } + if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), buf, &l, NULL, 0) < 0) { + err(1, "sysctl(IPV6CTL_ADDRCTLPOLICY)"); + /* NOTREACHED */ + } + + ep = (struct in6_addrpolicy *)(buf + l); + for (pol = (struct in6_addrpolicy *)buf; pol + 1 <= ep; pol++) { + struct policyqueue *new; + + if ((new = malloc(sizeof(*new))) == NULL) + errx(1, "malloc failed\n"); + new->pc_policy = *pol; + TAILQ_INSERT_TAIL(&policyhead, new, pc_entry); + } + + free(buf); +} + +static void +dump_policy() +{ + size_t addrlen; + char addrbuf[NI_MAXHOST]; + struct in6_addrpolicy *pol; + struct policyqueue *ent; + int plen, first = 1; + + for (ent = TAILQ_FIRST(&policyhead); ent; + ent = TAILQ_NEXT(ent, pc_entry)) { + pol = &ent->pc_policy; + if (first) { + printf("%-30s %5s %5s %8s\n", + "Prefix", "Prec", "Label", "Use"); + first = 0; + } + + if ((getnameinfo((struct sockaddr *)&pol->addr, + sizeof(pol->addr), addrbuf, sizeof(addrbuf), + NULL, 0, NI_NUMERICHOST))) { + warnx("getnameinfo for prefix address failed"); + continue; + } + if ((plen = mask2plen(&pol->addrmask)) < 0) { + warnx("invalid address mask"); + continue; + } + addrlen = strlen(addrbuf); + if (addrlen + sizeof("/128") < sizeof(addrbuf)) { + snprintf(&addrbuf[addrlen], + sizeof(addrbuf) - addrlen - 1, + "/%d", plen); + printf("%-30s", addrbuf); + } else /* XXX */ + printf("%s/%d", addrbuf, plen); + printf(" %5d %5d %8llu\n", pol->preced, pol->label, + (unsigned long long)pol->use); + } +} + +#define SKIP_WHITE(p, emptyok) \ + do { \ + while((*(p) == ' ' || *(p) == '\t')) \ + (p)++; \ + if ((*(p) == '\0' || (*(p) == '\n')) && !(emptyok)) \ + goto bad; \ + } while (0); +#define SKIP_WORD(p) \ + do { \ + while(*(p) != ' ' && *(p) != '\t') \ + (p)++; \ + if (*(p) == '\0' || *(p) == '\n') \ + goto bad; \ + } while (0); + +static void +make_policy_fromfile(conf) + char *conf; +{ + char line[_POSIX2_LINE_MAX], *cp; + char *addrstr; + FILE *fp; + int count = 0; + struct in6_addrpolicy pol0; + struct policyqueue *new; + + if ((fp = fopen(conf, "r")) == NULL) + err(1, "fopen: %s", conf); + + while(fgets(line, sizeof(line), fp)) { + count++; + cp = line; + + memset(&pol0, 0, sizeof(pol0)); + + /* get prefix */ + SKIP_WHITE(cp, 1); + if (*cp == '\n') /* empty line */ + continue; + if (*cp == '#') + continue; + addrstr = cp; + if (parse_prefix((const char *)addrstr, &pol0)) + goto bad; + + /* get precedence value */ + SKIP_WORD(cp); + SKIP_WHITE(cp, 0); + pol0.preced = atoi(cp); + + /* get label */ + SKIP_WORD(cp); + SKIP_WHITE(cp, 0); + pol0.label = atoi(cp); + + /* parse succeeded. make a control buffer entry. */ + if ((new = malloc(sizeof(*new))) == NULL) + errx(1, "malloc failed\n"); + memset(new, 0, sizeof(*new)); + new->pc_policy = pol0; + TAILQ_INSERT_TAIL(&policyhead, new, pc_entry); + } + + fclose(fp); + return; + + bad: + errx(1, "parse failed at line %d", count); + /* NOTREACHED */ +} + +static int +parse_prefix(prefix0, pol) + const char *prefix0; + struct in6_addrpolicy *pol; +{ + int e = 0, plen; + char *prefix, *plenstr; + struct addrinfo hints, *res; + + if ((prefix = strdup(prefix0)) == NULL) + errx(1, "strdup failed"); + + if ((plenstr = strchr(prefix, '/')) == NULL) { + e = -1; + goto end; + } + *plenstr = '\0'; + + memset(&hints, 0, sizeof(hints)); + hints.ai_flags = AI_NUMERICHOST; + hints.ai_family = AF_INET6; + + if ((e = getaddrinfo(prefix, NULL, &hints, &res)) != 0) { + warnx("getaddrinfo failed for %s: %s", prefix, + gai_strerror(e)); + goto end; + } + memcpy(&pol->addr, res->ai_addr, res->ai_addrlen); + freeaddrinfo(res); + plen = atoi(plenstr + 1); + if (plen < 0 || plen > 128) { + warnx("invalid prefix length: %d", plen); + e = -1; + goto end; + } + plen2mask(&pol->addrmask, plen); + + end: + free(prefix); + return(e); +} + +static void +plen2mask(mask, plen) + struct sockaddr_in6 *mask; + int plen; +{ + u_char *cp = (unsigned char *)&mask->sin6_addr; + + memset(mask, 0, sizeof(*mask)); + mask->sin6_family = AF_INET6; /* just in case */ + mask->sin6_len = sizeof(*mask); + + for(; plen >= 8; plen -= 8) + *cp++ = 0xff; + if (plen > 0) + *cp = (0xff << (8 - plen)); +} + +static void +set_policy() +{ + struct policyqueue *ent; + int s; + + if ((s = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP)) < 0) + err(1, "socket(UDP)"); + + for (ent = TAILQ_FIRST(&policyhead); ent; + ent = TAILQ_NEXT(ent, pc_entry)) { + if (ioctl(s, SIOCAADDRCTL_POLICY, &ent->pc_policy)) + warn("ioctl(SIOCAADDRCTL_POLICY)"); + } + + close(s); +} + +static int +mask2plen(mask) + struct sockaddr_in6 *mask; +{ + int masklen, final = 0; + u_char *p, *lim; + + masklen = 0; + lim = (u_char *)(mask + 1); + for (p = (u_char *)(&mask->sin6_addr); p < lim; p++) { + if (final && *p) { + goto bad; + } + + switch (*p & 0xff) { + case 0xff: + masklen += 8; + break; + case 0xfe: + masklen += 7; + final++; + break; + case 0xfc: + masklen += 6; + final++; + break; + case 0xf8: + masklen += 5; + final++; + break; + case 0xf0: + masklen += 4; + final++; + break; + case 0xe0: + masklen += 3; + final++; + break; + case 0xc0: + masklen += 2; + final++; + break; + case 0x80: + masklen += 1; + final++; + break; + case 0x00: + final++; + break; + default: + goto bad; + break; + } + } + return(masklen); + + bad: + return(-1); +} + +static void +add_policy(prefix, prec, label) + char *prefix, *prec, *label; +{ + struct in6_addrpolicy p; + int s; + + memset(&p, 0, sizeof(p)); + + if (parse_prefix((const char *)prefix, &p)) + errx(1, "bad prefix: %s", prefix); + p.preced = atoi(prec); + p.label = atoi(label); + + if ((s = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP)) < 0) + err(1, "socket(UDP)"); + if (ioctl(s, SIOCAADDRCTL_POLICY, &p)) + err(1, "ioctl(SIOCAADDRCTL_POLICY)"); + + close(s); +} + +static void +delete_policy(prefix) + char *prefix; +{ + struct in6_addrpolicy p; + int s; + + memset(&p, 0, sizeof(p)); + + if (parse_prefix((const char *)prefix, &p)) + errx(1, "bad prefix: %s", prefix); + + if ((s = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP)) < 0) + err(1, "socket(UDP)"); + if (ioctl(s, SIOCDADDRCTL_POLICY, &p)) + err(1, "ioctl(SIOCDADDRCTL_POLICY)"); + + close(s); +} + +static void +flush_policy() +{ + struct policyqueue *ent; + int s; + + if ((s = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP)) < 0) + err(1, "socket(UDP)"); + + for (ent = TAILQ_FIRST(&policyhead); ent; + ent = TAILQ_NEXT(ent, pc_entry)) { + if (ioctl(s, SIOCDADDRCTL_POLICY, &ent->pc_policy)) + warn("ioctl(SIOCDADDRCTL_POLICY)"); + } + + close(s); +} + +static void +usage() +{ + fprintf(stderr, "usage: ip6addrctl [show]\n"); + fprintf(stderr, " ip6addrctl add " + "