af_inetany.c revision 1.5 1 1.5 dyoung /* $NetBSD: af_inetany.c,v 1.5 2008/05/08 07:13:20 dyoung Exp $ */
2 1.1 dyoung
3 1.1 dyoung /*
4 1.1 dyoung * Copyright (c) 1983, 1993
5 1.1 dyoung * The Regents of the University of California. All rights reserved.
6 1.1 dyoung *
7 1.1 dyoung * Redistribution and use in source and binary forms, with or without
8 1.1 dyoung * modification, are permitted provided that the following conditions
9 1.1 dyoung * are met:
10 1.1 dyoung * 1. Redistributions of source code must retain the above copyright
11 1.1 dyoung * notice, this list of conditions and the following disclaimer.
12 1.1 dyoung * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 dyoung * notice, this list of conditions and the following disclaimer in the
14 1.1 dyoung * documentation and/or other materials provided with the distribution.
15 1.1 dyoung * 3. Neither the name of the University nor the names of its contributors
16 1.1 dyoung * may be used to endorse or promote products derived from this software
17 1.1 dyoung * without specific prior written permission.
18 1.1 dyoung *
19 1.1 dyoung * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 1.1 dyoung * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 1.1 dyoung * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 1.1 dyoung * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 1.1 dyoung * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 1.1 dyoung * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 1.1 dyoung * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 1.1 dyoung * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 1.1 dyoung * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 1.1 dyoung * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 1.1 dyoung * SUCH DAMAGE.
30 1.1 dyoung */
31 1.1 dyoung
32 1.1 dyoung #include <sys/cdefs.h>
33 1.1 dyoung #ifndef lint
34 1.5 dyoung __RCSID("$NetBSD: af_inetany.c,v 1.5 2008/05/08 07:13:20 dyoung Exp $");
35 1.1 dyoung #endif /* not lint */
36 1.1 dyoung
37 1.1 dyoung #include <sys/param.h>
38 1.1 dyoung #include <sys/ioctl.h>
39 1.1 dyoung #include <sys/socket.h>
40 1.1 dyoung
41 1.1 dyoung #include <net/if.h>
42 1.1 dyoung #include <netinet/in.h>
43 1.1 dyoung #include <netinet/in_var.h>
44 1.1 dyoung #include <netinet6/nd6.h>
45 1.1 dyoung
46 1.1 dyoung #include <arpa/inet.h>
47 1.1 dyoung
48 1.1 dyoung #include <assert.h>
49 1.1 dyoung #include <err.h>
50 1.1 dyoung #include <errno.h>
51 1.1 dyoung #include <ifaddrs.h>
52 1.1 dyoung #include <netdb.h>
53 1.1 dyoung #include <string.h>
54 1.1 dyoung #include <stdlib.h>
55 1.1 dyoung #include <stdio.h>
56 1.1 dyoung #include <util.h>
57 1.1 dyoung
58 1.1 dyoung #include "env.h"
59 1.1 dyoung #include "extern.h"
60 1.1 dyoung #include "af_inet.h"
61 1.2 dyoung #include "af_inet6.h"
62 1.5 dyoung #include "af_inetany.h"
63 1.2 dyoung
64 1.1 dyoung static void *
65 1.2 dyoung loadbuf(struct apbuf *b, const struct paddr_prefix *pfx)
66 1.1 dyoung {
67 1.1 dyoung return memcpy(b->buf, &pfx->pfx_addr,
68 1.1 dyoung MIN(b->buflen, pfx->pfx_addr.sa_len));
69 1.1 dyoung }
70 1.1 dyoung
71 1.1 dyoung void
72 1.5 dyoung commit_address(prop_dictionary_t env, prop_dictionary_t oenv,
73 1.5 dyoung struct afparam *param)
74 1.1 dyoung {
75 1.1 dyoung const char *ifname;
76 1.1 dyoung int af, rc, s;
77 1.4 dyoung bool alias, delete, replace;
78 1.1 dyoung prop_data_t d;
79 1.1 dyoung const struct paddr_prefix *addr, *brd, *dst, *mask;
80 1.1 dyoung unsigned short flags;
81 1.1 dyoung
82 1.1 dyoung if ((af = getaf(env)) == -1)
83 1.1 dyoung af = AF_INET;
84 1.1 dyoung
85 1.1 dyoung if ((s = getsock(af)) == -1)
86 1.1 dyoung err(EXIT_FAILURE, "%s: getsock", __func__);
87 1.1 dyoung
88 1.3 dyoung if ((ifname = getifinfo(env, oenv, &flags)) == NULL)
89 1.1 dyoung return;
90 1.1 dyoung
91 1.1 dyoung if ((d = (prop_data_t)prop_dictionary_get(env, "address")) != NULL)
92 1.1 dyoung addr = prop_data_data_nocopy(d);
93 1.1 dyoung else
94 1.1 dyoung return;
95 1.1 dyoung
96 1.1 dyoung if ((d = (prop_data_t)prop_dictionary_get(env, "dst")) != NULL)
97 1.1 dyoung dst = prop_data_data_nocopy(d);
98 1.1 dyoung else
99 1.1 dyoung dst = NULL;
100 1.1 dyoung
101 1.1 dyoung if ((d = (prop_data_t)prop_dictionary_get(env, "netmask")) != NULL)
102 1.1 dyoung mask = prop_data_data_nocopy(d);
103 1.1 dyoung else
104 1.1 dyoung mask = NULL;
105 1.1 dyoung
106 1.1 dyoung if ((d = (prop_data_t)prop_dictionary_get(env, "broadcast")) != NULL)
107 1.1 dyoung brd = prop_data_data_nocopy(d);
108 1.1 dyoung else
109 1.1 dyoung brd = NULL;
110 1.1 dyoung
111 1.4 dyoung if (!prop_dictionary_get_bool(env, "alias", &alias)) {
112 1.1 dyoung delete = false;
113 1.2 dyoung replace = (param->gifaddr.cmd != 0);
114 1.1 dyoung } else {
115 1.1 dyoung replace = false;
116 1.4 dyoung delete = !alias;
117 1.1 dyoung }
118 1.1 dyoung
119 1.1 dyoung memset(param->req.buf, 0, param->req.buflen);
120 1.1 dyoung memset(param->dgreq.buf, 0, param->dgreq.buflen);
121 1.1 dyoung
122 1.1 dyoung strlcpy(param->name[0].buf, ifname, param->name[0].buflen);
123 1.1 dyoung strlcpy(param->name[1].buf, ifname, param->name[1].buflen);
124 1.1 dyoung
125 1.1 dyoung loadbuf(¶m->addr, addr);
126 1.1 dyoung
127 1.1 dyoung /* TBD: read matching ifaddr from kernel, use the netmask as default
128 1.1 dyoung * TBD: handle preference
129 1.1 dyoung */
130 1.1 dyoung switch (flags & (IFF_BROADCAST|IFF_POINTOPOINT)) {
131 1.1 dyoung case 0:
132 1.1 dyoung break;
133 1.1 dyoung case IFF_BROADCAST:
134 1.1 dyoung if (mask != NULL)
135 1.1 dyoung loadbuf(¶m->mask, mask);
136 1.2 dyoung else if (param->defmask.buf != NULL) {
137 1.2 dyoung memcpy(param->mask.buf, param->defmask.buf,
138 1.2 dyoung MIN(param->mask.buflen, param->defmask.buflen));
139 1.2 dyoung }
140 1.1 dyoung if (brd != NULL)
141 1.1 dyoung loadbuf(¶m->brd, brd);
142 1.1 dyoung break;
143 1.1 dyoung case IFF_POINTOPOINT:
144 1.1 dyoung if (dst == NULL) {
145 1.1 dyoung errx(EXIT_FAILURE, "no point-to-point "
146 1.1 dyoung "destination address");
147 1.1 dyoung }
148 1.1 dyoung if (brd != NULL) {
149 1.1 dyoung errx(EXIT_FAILURE, "%s is not a broadcast interface",
150 1.1 dyoung ifname);
151 1.1 dyoung }
152 1.1 dyoung loadbuf(¶m->dst, dst);
153 1.1 dyoung break;
154 1.1 dyoung case IFF_BROADCAST|IFF_POINTOPOINT:
155 1.1 dyoung errx(EXIT_FAILURE, "unsupported interface flags");
156 1.1 dyoung }
157 1.1 dyoung if (replace) {
158 1.1 dyoung if (ioctl(s, param->gifaddr.cmd, param->dgreq.buf) == 0) {
159 1.1 dyoung rc = ioctl(s, param->difaddr.cmd, param->dgreq.buf);
160 1.1 dyoung if (rc == -1)
161 1.1 dyoung err(EXIT_FAILURE, param->difaddr.desc);
162 1.1 dyoung } else if (errno == EADDRNOTAVAIL)
163 1.1 dyoung ; /* No address was assigned yet. */
164 1.1 dyoung else
165 1.1 dyoung err(EXIT_FAILURE, param->gifaddr.desc);
166 1.1 dyoung } else if (delete) {
167 1.1 dyoung loadbuf(¶m->dgaddr, addr);
168 1.1 dyoung if (ioctl(s, param->difaddr.cmd, param->dgreq.buf) == -1)
169 1.1 dyoung err(EXIT_FAILURE, param->difaddr.desc);
170 1.1 dyoung return;
171 1.1 dyoung }
172 1.2 dyoung if (param->pre_aifaddr != NULL &&
173 1.2 dyoung (*param->pre_aifaddr)(env, param) == -1)
174 1.2 dyoung err(EXIT_FAILURE, "pre-%s", param->aifaddr.desc);
175 1.1 dyoung if (ioctl(s, param->aifaddr.cmd, param->req.buf) == -1)
176 1.1 dyoung err(EXIT_FAILURE, param->aifaddr.desc);
177 1.1 dyoung }
178