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