af_inetany.c revision 1.4 1 /* $NetBSD: af_inetany.c,v 1.4 2008/05/06 21:16:52 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_inetany.c,v 1.4 2008/05/06 21:16:52 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 #include <netinet6/nd6.h>
45
46 #include <arpa/inet.h>
47
48 #include <assert.h>
49 #include <err.h>
50 #include <errno.h>
51 #include <ifaddrs.h>
52 #include <netdb.h>
53 #include <string.h>
54 #include <stdlib.h>
55 #include <stdio.h>
56 #include <util.h>
57
58 #include "env.h"
59 #include "extern.h"
60 #include "af_inet.h"
61 #include "af_inet6.h"
62
63 #define IFADDR_PARAM(__arg) {.cmd = (__arg), .desc = #__arg}
64 #define BUFPARAM(__arg) {.buf = &(__arg), .buflen = sizeof(__arg)}
65
66 struct apbuf {
67 void *buf;
68 size_t buflen;
69 };
70
71 struct afparam {
72 struct {
73 char *buf;
74 size_t buflen;
75 } name[2];
76 struct apbuf dgaddr, addr, brd, dst, mask, req, dgreq, defmask,
77 pre_aifaddr_arg;
78 struct {
79 unsigned long cmd;
80 const char *desc;
81 } aifaddr, difaddr, gifaddr;
82 int (*pre_aifaddr)(prop_dictionary_t, void *);
83 };
84
85 static void *
86 loadbuf(struct apbuf *b, const struct paddr_prefix *pfx)
87 {
88 return memcpy(b->buf, &pfx->pfx_addr,
89 MIN(b->buflen, pfx->pfx_addr.sa_len));
90 }
91
92 static int
93 in6_pre_aifaddr(prop_dictionary_t env, void *arg)
94 {
95 struct in6_aliasreq *ifra = arg;
96
97 setia6eui64_impl(env, ifra);
98 setia6vltime_impl(env, ifra);
99 setia6pltime_impl(env, ifra);
100 setia6flags_impl(env, ifra);
101
102 return 0;
103 }
104
105 void
106 commit_address(prop_dictionary_t env, prop_dictionary_t oenv)
107 {
108 const char *ifname;
109 struct ifreq in_ifr;
110 struct in_aliasreq in_ifra;
111 struct in6_ifreq in6_ifr;
112 #if 0
113 = {
114 .ifr_addr = {
115 .sin6_family = AF_INET6,
116 .sin6_addr = {
117 .s6_addr =
118 {0xff, 0xff, 0xff, 0xff,
119 0xff, 0xff, 0xff, 0xff}
120 }
121 }
122 };
123 #endif
124 static struct sockaddr_in6 in6_defmask = {
125 .sin6_addr = {
126 .s6_addr = {0xff, 0xff, 0xff, 0xff,
127 0xff, 0xff, 0xff, 0xff}
128 }
129 };
130
131 struct in6_aliasreq in6_ifra;
132 #if 0
133 = {
134 .ifra_prefixmask = {
135 .sin6_addr = {
136 .s6_addr =
137 {0xff, 0xff, 0xff, 0xff,
138 0xff, 0xff, 0xff, 0xff}}},
139 .ifra_lifetime = {
140 .ia6t_pltime = ND6_INFINITE_LIFETIME
141 , .ia6t_vltime = ND6_INFINITE_LIFETIME
142 }
143 };
144 #endif
145 int af, rc, s;
146 bool alias, delete, replace;
147 prop_data_t d;
148 const struct paddr_prefix *addr, *brd, *dst, *mask;
149 unsigned short flags;
150 struct afparam inparam = {
151 .req = BUFPARAM(in_ifra)
152 , .dgreq = BUFPARAM(in_ifr)
153 , .name = {
154 {.buf = in_ifr.ifr_name,
155 .buflen = sizeof(in_ifr.ifr_name)}
156 , {.buf = in_ifra.ifra_name,
157 .buflen = sizeof(in_ifra.ifra_name)}
158 }
159 , .dgaddr = BUFPARAM(in_ifr.ifr_addr)
160 , .addr = BUFPARAM(in_ifra.ifra_addr)
161 , .dst = BUFPARAM(in_ifra.ifra_dstaddr)
162 , .brd = BUFPARAM(in_ifra.ifra_broadaddr)
163 , .mask = BUFPARAM(in_ifra.ifra_mask)
164 , .aifaddr = IFADDR_PARAM(SIOCAIFADDR)
165 , .difaddr = IFADDR_PARAM(SIOCDIFADDR)
166 , .gifaddr = IFADDR_PARAM(SIOCGIFADDR)
167 , .defmask = {.buf = NULL, .buflen = 0}
168 }, in6param = {
169 .req = BUFPARAM(in6_ifra)
170 , .dgreq = BUFPARAM(in6_ifr)
171 , .name = {
172 {.buf = in6_ifr.ifr_name,
173 .buflen = sizeof(in6_ifr.ifr_name)},
174 {.buf = in6_ifra.ifra_name,
175 .buflen = sizeof(in6_ifra.ifra_name)}
176 }
177 , .dgaddr = BUFPARAM(in6_ifr.ifr_addr)
178 , .addr = BUFPARAM(in6_ifra.ifra_addr)
179 , .dst = BUFPARAM(in6_ifra.ifra_dstaddr)
180 , .brd = BUFPARAM(in6_ifra.ifra_broadaddr)
181 , .mask = BUFPARAM(in6_ifra.ifra_prefixmask)
182 , .aifaddr = IFADDR_PARAM(SIOCAIFADDR_IN6)
183 , .difaddr = IFADDR_PARAM(SIOCDIFADDR_IN6)
184 , .gifaddr = IFADDR_PARAM(SIOCGIFADDR_IN6)
185 , .defmask = BUFPARAM(in6_defmask)
186 , .pre_aifaddr = in6_pre_aifaddr
187 , .pre_aifaddr_arg = BUFPARAM(in6_ifra)
188 }, *param;
189
190 if ((af = getaf(env)) == -1)
191 af = AF_INET;
192
193 switch (af) {
194 case AF_INET:
195 param = &inparam;
196 break;
197 case AF_INET6:
198 param = &in6param;
199 break;
200 default:
201 errx(EXIT_FAILURE, "%s: unknown address family %d", __func__,
202 af);
203 break;
204 }
205 if ((s = getsock(af)) == -1)
206 err(EXIT_FAILURE, "%s: getsock", __func__);
207
208 if ((ifname = getifinfo(env, oenv, &flags)) == NULL)
209 return;
210
211 if ((d = (prop_data_t)prop_dictionary_get(env, "address")) != NULL)
212 addr = prop_data_data_nocopy(d);
213 else
214 return;
215
216 if ((d = (prop_data_t)prop_dictionary_get(env, "dst")) != NULL)
217 dst = prop_data_data_nocopy(d);
218 else
219 dst = NULL;
220
221 if ((d = (prop_data_t)prop_dictionary_get(env, "netmask")) != NULL)
222 mask = prop_data_data_nocopy(d);
223 else
224 mask = NULL;
225
226 if ((d = (prop_data_t)prop_dictionary_get(env, "broadcast")) != NULL)
227 brd = prop_data_data_nocopy(d);
228 else
229 brd = NULL;
230
231 if (!prop_dictionary_get_bool(env, "alias", &alias)) {
232 delete = false;
233 replace = (param->gifaddr.cmd != 0);
234 } else {
235 replace = false;
236 delete = !alias;
237 }
238
239 memset(param->req.buf, 0, param->req.buflen);
240 memset(param->dgreq.buf, 0, param->dgreq.buflen);
241
242 strlcpy(param->name[0].buf, ifname, param->name[0].buflen);
243 strlcpy(param->name[1].buf, ifname, param->name[1].buflen);
244
245 loadbuf(¶m->addr, addr);
246
247 /* TBD: read matching ifaddr from kernel, use the netmask as default
248 * TBD: handle preference
249 */
250 switch (flags & (IFF_BROADCAST|IFF_POINTOPOINT)) {
251 case 0:
252 break;
253 case IFF_BROADCAST:
254 if (mask != NULL)
255 loadbuf(¶m->mask, mask);
256 else if (param->defmask.buf != NULL) {
257 memcpy(param->mask.buf, param->defmask.buf,
258 MIN(param->mask.buflen, param->defmask.buflen));
259 }
260 if (brd != NULL)
261 loadbuf(¶m->brd, brd);
262 break;
263 case IFF_POINTOPOINT:
264 if (dst == NULL) {
265 errx(EXIT_FAILURE, "no point-to-point "
266 "destination address");
267 }
268 if (brd != NULL) {
269 errx(EXIT_FAILURE, "%s is not a broadcast interface",
270 ifname);
271 }
272 loadbuf(¶m->dst, dst);
273 break;
274 case IFF_BROADCAST|IFF_POINTOPOINT:
275 errx(EXIT_FAILURE, "unsupported interface flags");
276 }
277 if (replace) {
278 if (ioctl(s, param->gifaddr.cmd, param->dgreq.buf) == 0) {
279 rc = ioctl(s, param->difaddr.cmd, param->dgreq.buf);
280 if (rc == -1)
281 err(EXIT_FAILURE, param->difaddr.desc);
282 } else if (errno == EADDRNOTAVAIL)
283 ; /* No address was assigned yet. */
284 else
285 err(EXIT_FAILURE, param->gifaddr.desc);
286 } else if (delete) {
287 loadbuf(¶m->dgaddr, addr);
288 if (ioctl(s, param->difaddr.cmd, param->dgreq.buf) == -1)
289 err(EXIT_FAILURE, param->difaddr.desc);
290 return;
291 }
292 if (param->pre_aifaddr != NULL &&
293 (*param->pre_aifaddr)(env, param) == -1)
294 err(EXIT_FAILURE, "pre-%s", param->aifaddr.desc);
295 if (ioctl(s, param->aifaddr.cmd, param->req.buf) == -1)
296 err(EXIT_FAILURE, param->aifaddr.desc);
297 }
298