af_inetany.c revision 1.5 1 /* $NetBSD: af_inetany.c,v 1.5 2008/05/08 07:13:20 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.5 2008/05/08 07:13:20 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 #include "af_inetany.h"
63
64 static void *
65 loadbuf(struct apbuf *b, const struct paddr_prefix *pfx)
66 {
67 return memcpy(b->buf, &pfx->pfx_addr,
68 MIN(b->buflen, pfx->pfx_addr.sa_len));
69 }
70
71 void
72 commit_address(prop_dictionary_t env, prop_dictionary_t oenv,
73 struct afparam *param)
74 {
75 const char *ifname;
76 int af, rc, s;
77 bool alias, delete, replace;
78 prop_data_t d;
79 const struct paddr_prefix *addr, *brd, *dst, *mask;
80 unsigned short flags;
81
82 if ((af = getaf(env)) == -1)
83 af = AF_INET;
84
85 if ((s = getsock(af)) == -1)
86 err(EXIT_FAILURE, "%s: getsock", __func__);
87
88 if ((ifname = getifinfo(env, oenv, &flags)) == NULL)
89 return;
90
91 if ((d = (prop_data_t)prop_dictionary_get(env, "address")) != NULL)
92 addr = prop_data_data_nocopy(d);
93 else
94 return;
95
96 if ((d = (prop_data_t)prop_dictionary_get(env, "dst")) != NULL)
97 dst = prop_data_data_nocopy(d);
98 else
99 dst = NULL;
100
101 if ((d = (prop_data_t)prop_dictionary_get(env, "netmask")) != NULL)
102 mask = prop_data_data_nocopy(d);
103 else
104 mask = NULL;
105
106 if ((d = (prop_data_t)prop_dictionary_get(env, "broadcast")) != NULL)
107 brd = prop_data_data_nocopy(d);
108 else
109 brd = NULL;
110
111 if (!prop_dictionary_get_bool(env, "alias", &alias)) {
112 delete = false;
113 replace = (param->gifaddr.cmd != 0);
114 } else {
115 replace = false;
116 delete = !alias;
117 }
118
119 memset(param->req.buf, 0, param->req.buflen);
120 memset(param->dgreq.buf, 0, param->dgreq.buflen);
121
122 strlcpy(param->name[0].buf, ifname, param->name[0].buflen);
123 strlcpy(param->name[1].buf, ifname, param->name[1].buflen);
124
125 loadbuf(¶m->addr, addr);
126
127 /* TBD: read matching ifaddr from kernel, use the netmask as default
128 * TBD: handle preference
129 */
130 switch (flags & (IFF_BROADCAST|IFF_POINTOPOINT)) {
131 case 0:
132 break;
133 case IFF_BROADCAST:
134 if (mask != NULL)
135 loadbuf(¶m->mask, mask);
136 else if (param->defmask.buf != NULL) {
137 memcpy(param->mask.buf, param->defmask.buf,
138 MIN(param->mask.buflen, param->defmask.buflen));
139 }
140 if (brd != NULL)
141 loadbuf(¶m->brd, brd);
142 break;
143 case IFF_POINTOPOINT:
144 if (dst == NULL) {
145 errx(EXIT_FAILURE, "no point-to-point "
146 "destination address");
147 }
148 if (brd != NULL) {
149 errx(EXIT_FAILURE, "%s is not a broadcast interface",
150 ifname);
151 }
152 loadbuf(¶m->dst, dst);
153 break;
154 case IFF_BROADCAST|IFF_POINTOPOINT:
155 errx(EXIT_FAILURE, "unsupported interface flags");
156 }
157 if (replace) {
158 if (ioctl(s, param->gifaddr.cmd, param->dgreq.buf) == 0) {
159 rc = ioctl(s, param->difaddr.cmd, param->dgreq.buf);
160 if (rc == -1)
161 err(EXIT_FAILURE, param->difaddr.desc);
162 } else if (errno == EADDRNOTAVAIL)
163 ; /* No address was assigned yet. */
164 else
165 err(EXIT_FAILURE, param->gifaddr.desc);
166 } else if (delete) {
167 loadbuf(¶m->dgaddr, addr);
168 if (ioctl(s, param->difaddr.cmd, param->dgreq.buf) == -1)
169 err(EXIT_FAILURE, param->difaddr.desc);
170 return;
171 }
172 if (param->pre_aifaddr != NULL &&
173 (*param->pre_aifaddr)(env, param) == -1)
174 err(EXIT_FAILURE, "pre-%s", param->aifaddr.desc);
175 if (ioctl(s, param->aifaddr.cmd, param->req.buf) == -1)
176 err(EXIT_FAILURE, param->aifaddr.desc);
177 }
178