/
af_inet.c
242 lines (212 loc) · 6.95 KB
/
af_inet.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/* $NetBSD: af_inet.c,v 1.7.2.2 2008/09/18 04:28:24 wrstuden Exp $ */
/*
* Copyright (c) 1983, 1993
* The Regents of the University of California. 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 University 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 REGENTS 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 REGENTS 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.
*/
#include <sys/cdefs.h>
#ifndef lint
__RCSID("$NetBSD: af_inet.c,v 1.7.2.2 2008/09/18 04:28:24 wrstuden Exp $");
#endif /* not lint */
#include <sys/param.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <net/if.h>
#include <netinet/in.h>
#include <netinet/in_var.h>
#include <arpa/inet.h>
#include <assert.h>
#include <err.h>
#include <errno.h>
#include <ifaddrs.h>
#include <netdb.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <util.h>
#include "env.h"
#include "extern.h"
#include "af_inetany.h"
static void in_constructor(void) __attribute__((constructor));
static void in_status(prop_dictionary_t, prop_dictionary_t, bool);
static void in_commit_address(prop_dictionary_t, prop_dictionary_t);
static void in_alias(const char *, prop_dictionary_t, prop_dictionary_t,
struct in_aliasreq *);
static void in_preference(const char *, const struct sockaddr *);
static struct afswtch af = {
.af_name = "inet", .af_af = AF_INET, .af_status = in_status,
.af_addr_commit = in_commit_address
};
static void
in_alias(const char *ifname, prop_dictionary_t env, prop_dictionary_t oenv,
struct in_aliasreq *creq)
{
struct ifreq ifr;
bool alias;
int s;
unsigned short flags;
struct in_aliasreq in_addreq;
if (lflag)
return;
alias = true;
/* Get the non-alias address for this interface. */
if ((s = getsock(AF_INET)) == -1) {
if (errno == EAFNOSUPPORT)
return;
err(EXIT_FAILURE, "socket");
}
memset(&ifr, 0, sizeof(ifr));
estrlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
if (ioctl(s, SIOCGIFADDR, &ifr) == -1) {
if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT)
return;
warn("SIOCGIFADDR");
}
/* If creq and ifr are the same address, this is not an alias. */
if (memcmp(&ifr.ifr_addr, &creq->ifra_addr, sizeof(ifr.ifr_addr)) == 0)
alias = false;
in_addreq = *creq;
if (ioctl(s, SIOCGIFALIAS, &in_addreq) == -1) {
if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT) {
return;
} else
warn("SIOCGIFALIAS");
}
printf("\tinet %s%s", alias ? "alias " : "",
inet_ntoa(in_addreq.ifra_addr.sin_addr));
if (getifflags(env, oenv, &flags) == -1)
err(EXIT_FAILURE, "%s: getifflags", __func__);
if (flags & IFF_POINTOPOINT)
printf(" -> %s", inet_ntoa(in_addreq.ifra_dstaddr.sin_addr));
printf(" netmask 0x%x", ntohl(in_addreq.ifra_mask.sin_addr.s_addr));
if (flags & IFF_BROADCAST) {
printf(" broadcast %s",
inet_ntoa(in_addreq.ifra_broadaddr.sin_addr));
}
}
static int16_t
in_get_preference(const char *ifname, const struct sockaddr *sa)
{
struct if_addrprefreq ifap;
int s;
if ((s = getsock(AF_INET)) == -1) {
if (errno == EPROTONOSUPPORT)
return 0;
err(EXIT_FAILURE, "socket");
}
memset(&ifap, 0, sizeof(ifap));
estrlcpy(ifap.ifap_name, ifname, sizeof(ifap.ifap_name));
memcpy(&ifap.ifap_addr, sa, MIN(sizeof(ifap.ifap_addr), sa->sa_len));
if (ioctl(s, SIOCGIFADDRPREF, &ifap) == -1) {
if (errno == EADDRNOTAVAIL || errno == EAFNOSUPPORT)
return 0;
warn("SIOCGIFADDRPREF");
}
return ifap.ifap_preference;
}
static void
in_preference(const char *ifname, const struct sockaddr *sa)
{
int16_t preference;
if (lflag)
return;
preference = in_get_preference(ifname, sa);
printf(" preference %" PRId16, preference);
}
static void
in_status(prop_dictionary_t env, prop_dictionary_t oenv, bool force)
{
struct ifaddrs *ifap, *ifa;
struct in_aliasreq ifra;
int printprefs = 0;
const char *ifname;
if ((ifname = getifname(env)) == NULL)
err(EXIT_FAILURE, "%s: getifname", __func__);
if (getifaddrs(&ifap) != 0)
err(EXIT_FAILURE, "getifaddrs");
/* Print address preference numbers if any address has a non-zero
* preference assigned.
*/
for (ifa = ifap; ifa != NULL; ifa = ifa->ifa_next) {
if (strcmp(ifname, ifa->ifa_name) != 0)
continue;
if (ifa->ifa_addr->sa_family != AF_INET)
continue;
if (in_get_preference(ifa->ifa_name, ifa->ifa_addr) != 0) {
printprefs = 1;
break;
}
}
for (ifa = ifap; ifa != NULL; ifa = ifa->ifa_next) {
if (strcmp(ifname, ifa->ifa_name) != 0)
continue;
if (ifa->ifa_addr->sa_family != AF_INET)
continue;
if (sizeof(ifra.ifra_addr) < ifa->ifa_addr->sa_len)
continue;
memset(&ifra, 0, sizeof(ifra));
estrlcpy(ifra.ifra_name, ifa->ifa_name, sizeof(ifra.ifra_name));
memcpy(&ifra.ifra_addr, ifa->ifa_addr, ifa->ifa_addr->sa_len);
in_alias(ifa->ifa_name, env, oenv, &ifra);
if (printprefs)
in_preference(ifa->ifa_name, ifa->ifa_addr);
printf("\n");
}
freeifaddrs(ifap);
}
static void
in_commit_address(prop_dictionary_t env, prop_dictionary_t oenv)
{
struct ifreq in_ifr;
struct in_aliasreq in_ifra;
struct afparam inparam = {
.req = BUFPARAM(in_ifra)
, .dgreq = BUFPARAM(in_ifr)
, .name = {
{.buf = in_ifr.ifr_name,
.buflen = sizeof(in_ifr.ifr_name)}
, {.buf = in_ifra.ifra_name,
.buflen = sizeof(in_ifra.ifra_name)}
}
, .dgaddr = BUFPARAM(in_ifr.ifr_addr)
, .addr = BUFPARAM(in_ifra.ifra_addr)
, .dst = BUFPARAM(in_ifra.ifra_dstaddr)
, .brd = BUFPARAM(in_ifra.ifra_broadaddr)
, .mask = BUFPARAM(in_ifra.ifra_mask)
, .aifaddr = IFADDR_PARAM(SIOCAIFADDR)
, .difaddr = IFADDR_PARAM(SIOCDIFADDR)
, .gifaddr = IFADDR_PARAM(SIOCGIFADDR)
, .defmask = {.buf = NULL, .buflen = 0}
};
memset(&in_ifr, 0, sizeof(in_ifr));
memset(&in_ifra, 0, sizeof(in_ifra));
commit_address(env, oenv, &inparam);
}
static void
in_constructor(void)
{
register_family(&af);
}