af_inet.c revision 1.10 1 /* $NetBSD: af_inet.c,v 1.10 2008/05/11 23:25:46 dyoung Exp $ */
2
3 /*
4 * Copyright (c) 1983, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h>
33 #ifndef lint
34 __RCSID("$NetBSD: af_inet.c,v 1.10 2008/05/11 23:25:46 dyoung Exp $");
35 #endif /* not lint */
36
37 #include <sys/param.h>
38 #include <sys/ioctl.h>
39 #include <sys/socket.h>
40
41 #include <net/if.h>
42 #include <netinet/in.h>
43 #include <netinet/in_var.h>
44
45 #include <arpa/inet.h>
46
47 #include <assert.h>
48 #include <err.h>
49 #include <errno.h>
50 #include <ifaddrs.h>
51 #include <netdb.h>
52 #include <string.h>
53 #include <stdlib.h>
54 #include <stdio.h>
55 #include <util.h>
56
57 #include "env.h"
58 #include "extern.h"
59 #include "af_inet.h"
60 #include "af_inetany.h"
61
62 static void in_alias(const char *, prop_dictionary_t, prop_dictionary_t,
63 struct in_aliasreq *);
64 static void in_preference(const char *, const struct sockaddr *);
65
66 void
67 in_alias(const char *ifname, prop_dictionary_t env, prop_dictionary_t oenv,
68 struct in_aliasreq *creq)
69 {
70 struct ifreq ifr;
71 int alias, s;
72 unsigned short flags;
73 struct in_aliasreq in_addreq;
74
75 if (lflag)
76 return;
77
78 alias = 1;
79
80 /* Get the non-alias address for this interface. */
81 if ((s = getsock(AF_INET)) == -1) {
82 if (errno == EAFNOSUPPORT)
83 return;
84 err(EXIT_FAILURE, "socket");
85 }
86 memset(&ifr, 0, sizeof(ifr));
87 estrlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
88 if (ioctl(s, SIOCGIFADDR, &ifr) == -1) {
89 if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT)
90 return;
91 warn("SIOCGIFADDR");
92 }
93 /* If creq and ifr are the same address, this is not an alias. */
94 if (memcmp(&ifr.ifr_addr, &creq->ifra_addr, sizeof(ifr.ifr_addr)) == 0)
95 alias = 0;
96 in_addreq = *creq;
97 if (ioctl(s, SIOCGIFALIAS, &in_addreq) == -1) {
98 if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT) {
99 return;
100 } else
101 warn("SIOCGIFALIAS");
102 }
103
104 printf("\tinet %s%s", alias ? "alias " : "",
105 inet_ntoa(in_addreq.ifra_addr.sin_addr));
106
107 if (getifflags(env, oenv, &flags) == -1)
108 err(EXIT_FAILURE, "%s: getifflags", __func__);
109
110 if (flags & IFF_POINTOPOINT)
111 printf(" -> %s", inet_ntoa(in_addreq.ifra_dstaddr.sin_addr));
112
113 printf(" netmask 0x%x", ntohl(in_addreq.ifra_mask.sin_addr.s_addr));
114
115 if (flags & IFF_BROADCAST) {
116 printf(" broadcast %s",
117 inet_ntoa(in_addreq.ifra_broadaddr.sin_addr));
118 }
119 }
120
121 static int16_t
122 in_get_preference(const char *ifname, const struct sockaddr *sa)
123 {
124 struct if_addrprefreq ifap;
125 int s;
126
127 if ((s = getsock(AF_INET)) == -1) {
128 if (errno == EPROTONOSUPPORT)
129 return 0;
130 err(EXIT_FAILURE, "socket");
131 }
132 memset(&ifap, 0, sizeof(ifap));
133 estrlcpy(ifap.ifap_name, ifname, sizeof(ifap.ifap_name));
134 memcpy(&ifap.ifap_addr, sa, MIN(sizeof(ifap.ifap_addr), sa->sa_len));
135 if (ioctl(s, SIOCGIFADDRPREF, &ifap) == -1) {
136 if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT)
137 return 0;
138 warn("SIOCGIFADDRPREF");
139 }
140 return ifap.ifap_preference;
141 }
142
143 static void
144 in_preference(const char *ifname, const struct sockaddr *sa)
145 {
146 int16_t preference;
147
148 if (lflag)
149 return;
150
151 preference = in_get_preference(ifname, sa);
152 printf(" preference %" PRId16, preference);
153 }
154
155 void
156 in_status(prop_dictionary_t env, prop_dictionary_t oenv, bool force)
157 {
158 struct ifaddrs *ifap, *ifa;
159 struct in_aliasreq ifra;
160 int printprefs = 0;
161 const char *ifname;
162
163 if ((ifname = getifname(env)) == NULL)
164 err(EXIT_FAILURE, "%s: getifname", __func__);
165
166 if (getifaddrs(&ifap) != 0)
167 err(EXIT_FAILURE, "getifaddrs");
168
169 /* Print address preference numbers if any address has a non-zero
170 * preference assigned.
171 */
172 for (ifa = ifap; ifa != NULL; ifa = ifa->ifa_next) {
173 if (strcmp(ifname, ifa->ifa_name) != 0)
174 continue;
175 if (ifa->ifa_addr->sa_family != AF_INET)
176 continue;
177 if (in_get_preference(ifa->ifa_name, ifa->ifa_addr) != 0) {
178 printprefs = 1;
179 break;
180 }
181 }
182 for (ifa = ifap; ifa != NULL; ifa = ifa->ifa_next) {
183 if (strcmp(ifname, ifa->ifa_name) != 0)
184 continue;
185 if (ifa->ifa_addr->sa_family != AF_INET)
186 continue;
187 if (sizeof(ifra.ifra_addr) < ifa->ifa_addr->sa_len)
188 continue;
189
190 memset(&ifra, 0, sizeof(ifra));
191 estrlcpy(ifra.ifra_name, ifa->ifa_name, sizeof(ifra.ifra_name));
192 memcpy(&ifra.ifra_addr, ifa->ifa_addr, ifa->ifa_addr->sa_len);
193 in_alias(ifa->ifa_name, env, oenv, &ifra);
194 if (printprefs)
195 in_preference(ifa->ifa_name, ifa->ifa_addr);
196 printf("\n");
197 }
198 freeifaddrs(ifap);
199 }
200
201 void
202 in_commit_address(prop_dictionary_t env, prop_dictionary_t oenv)
203 {
204 struct ifreq in_ifr;
205 struct in_aliasreq in_ifra;
206 struct afparam inparam = {
207 .req = BUFPARAM(in_ifra)
208 , .dgreq = BUFPARAM(in_ifr)
209 , .name = {
210 {.buf = in_ifr.ifr_name,
211 .buflen = sizeof(in_ifr.ifr_name)}
212 , {.buf = in_ifra.ifra_name,
213 .buflen = sizeof(in_ifra.ifra_name)}
214 }
215 , .dgaddr = BUFPARAM(in_ifr.ifr_addr)
216 , .addr = BUFPARAM(in_ifra.ifra_addr)
217 , .dst = BUFPARAM(in_ifra.ifra_dstaddr)
218 , .brd = BUFPARAM(in_ifra.ifra_broadaddr)
219 , .mask = BUFPARAM(in_ifra.ifra_mask)
220 , .aifaddr = IFADDR_PARAM(SIOCAIFADDR)
221 , .difaddr = IFADDR_PARAM(SIOCDIFADDR)
222 , .gifaddr = IFADDR_PARAM(SIOCGIFADDR)
223 , .defmask = {.buf = NULL, .buflen = 0}
224 };
225 memset(&in_ifr, 0, sizeof(in_ifr));
226 memset(&in_ifra, 0, sizeof(in_ifra));
227 commit_address(env, oenv, &inparam);
228 }
229