usbmodeswitch: Updated to v.1.2.6 from shibby's branch.
[tomato.git] / release / src / router / dhcpv6 / dhcp6_ctl.c
blobab1f18d51a2233e41272aca89a74b2ab2fee8422
1 /* $KAME: dhcp6_ctl.c,v 1.4 2004/09/07 05:03:03 jinmei Exp $ */
3 /*
4 * Copyright (C) 2004 WIDE Project.
5 * All rights reserved.
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 project 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.
19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT 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 PROJECT 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.
31 #include <sys/param.h>
32 #include <sys/types.h>
33 #include <sys/queue.h>
34 #include <sys/socket.h>
35 #if TIME_WITH_SYS_TIME
36 # include <sys/time.h>
37 # include <time.h>
38 #else
39 # if HAVE_SYS_TIME_H
40 # include <sys/time.h>
41 # else
42 # include <time.h>
43 # endif
44 #endif
46 #include <netinet/in.h>
48 #include <stdlib.h>
49 #include <stdio.h>
50 #include <unistd.h>
51 #include <string.h>
52 #include <syslog.h>
53 #include <netdb.h>
54 #include <errno.h>
56 #include <dhcp6.h>
57 #include <config.h>
58 #include <common.h>
59 #include <auth.h>
60 #include <base64.h>
61 #include <control.h>
62 #include <dhcp6_ctl.h>
64 TAILQ_HEAD(dhcp6_commandqueue, dhcp6_commandctx);
66 static struct dhcp6_commandqueue commandqueue_head;
67 static int max_commands;
68 static int commands = 0;
70 struct dhcp6_commandctx {
71 TAILQ_ENTRY(dhcp6_commandctx) link;
73 int s; /* communication socket */
74 char inputbuf[1024]; /* input buffer */
75 ssize_t input_len;
76 ssize_t input_filled;
77 int (*callback) __P((char *, ssize_t));
80 int
81 dhcp6_ctl_init(addr, port, max, sockp)
82 char *addr, *port;
83 int max, *sockp;
85 struct addrinfo hints, *res = NULL;
86 int on;
87 int error;
88 int ctlsock = -1;
90 memset(&hints, 0, sizeof(hints));
91 hints.ai_family = AF_INET6;
92 hints.ai_socktype = SOCK_STREAM;
93 hints.ai_protocol = IPPROTO_TCP;
94 error = getaddrinfo(addr, port, &hints, &res);
95 if (error) {
96 dprintf(LOG_ERR, FNAME, "getaddrinfo: %s",
97 gai_strerror(error));
98 return (-1);
100 ctlsock = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
101 if (ctlsock < 0) {
102 dprintf(LOG_ERR, FNAME, "socket(control sock): %s",
103 strerror(errno));
104 goto fail;
106 on = 1;
107 if (setsockopt(ctlsock, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on))
108 < 0) {
109 dprintf(LOG_ERR, FNAME,
110 "setsockopt(control sock, SO_REUSEADDR: %s",
111 strerror(errno));
112 goto fail;
114 if (bind(ctlsock, res->ai_addr, res->ai_addrlen) < 0) {
115 dprintf(LOG_ERR, FNAME, "bind(control sock): %s",
116 strerror(errno));
117 goto fail;
119 freeaddrinfo(res);
120 if (listen(ctlsock, 1)) {
121 dprintf(LOG_ERR, FNAME, "listen(control sock): %s",
122 strerror(errno));
123 goto fail;
126 TAILQ_INIT(&commandqueue_head);
128 if (max <= 0) {
129 dprintf(LOG_ERR, FNAME,
130 "invalid maximum number of commands (%d)", max_commands);
131 goto fail;
133 max_commands = max;
135 *sockp = ctlsock;
136 return (0);
138 fail:
139 if (res != NULL)
140 freeaddrinfo(res);
141 if (ctlsock >= 0)
142 close(ctlsock);
144 return (-1);
148 dhcp6_ctl_authinit(keyfile, keyinfop, digestlenp)
149 char *keyfile;
150 struct keyinfo **keyinfop;
151 int *digestlenp;
153 FILE *fp = NULL;
154 struct keyinfo *ctlkey = NULL;
155 char line[1024], secret[1024];
156 int secretlen;
158 /* Currently, we only support HMAC-MD5 for authentication. */
159 *digestlenp = MD5_DIGESTLENGTH;
161 if ((fp = fopen(keyfile, "r")) == NULL) {
162 dprintf(LOG_ERR, FNAME, "failed to open %s: %s", keyfile,
163 strerror(errno));
164 return (-1);
166 if (fgets(line, sizeof(line), fp) == NULL && ferror(fp)) {
167 dprintf(LOG_ERR, FNAME, "failed to read key file: %s",
168 strerror(errno));
169 goto fail;
171 if ((secretlen = base64_decodestring(line, secret, sizeof(secret)))
172 < 0) {
173 dprintf(LOG_ERR, FNAME, "failed to decode base64 string");
174 goto fail;
176 if ((ctlkey = malloc(sizeof(*ctlkey))) == NULL) {
177 dprintf(LOG_WARNING, FNAME, "failed to allocate control key");
178 goto fail;
180 memset(ctlkey, 0, sizeof(*ctlkey));
181 if ((ctlkey->secret = malloc(secretlen)) == NULL) {
182 dprintf(LOG_WARNING, FNAME, "failed to allocate secret key");
183 goto fail;
185 ctlkey->secretlen = (size_t)secretlen;
186 memcpy(ctlkey->secret, secret, secretlen);
188 fclose(fp);
190 *keyinfop = ctlkey;
191 return (0);
193 fail:
194 if (fp != NULL)
195 fclose(fp);
196 if (ctlkey != NULL && ctlkey->secret != NULL)
197 free(ctlkey->secret);
198 if (ctlkey != NULL)
199 free(ctlkey);
201 return (-1);
205 dhcp6_ctl_acceptcommand(sl, callback)
206 int sl;
207 int (*callback) __P((char *, ssize_t));
209 int s;
210 struct sockaddr_storage from_ss;
211 struct sockaddr *from = (struct sockaddr *)&from_ss;
212 socklen_t fromlen;
213 struct dhcp6_commandctx *ctx, *new;
215 fromlen = sizeof(from_ss);
216 if ((s = accept(sl, from, &fromlen)) < 0) {
217 dprintf(LOG_WARNING, FNAME,
218 "failed to accept control connection: %s",
219 strerror(errno));
220 return (-1);
223 dprintf(LOG_DEBUG, FNAME, "accept control connection from %s",
224 addr2str(from));
226 if (max_commands <= 0) {
227 dprintf(LOG_ERR, FNAME, "command queue is not initialized");
228 close(s);
229 return (-1);
232 new = malloc(sizeof(*new));
233 if (new == NULL) {
234 dprintf(LOG_WARNING, FNAME,
235 "failed to allocate new command context");
236 goto fail;
239 /* if the command queue is full, purge the oldest one */
240 if (commands == max_commands) {
241 ctx = TAILQ_FIRST(&commandqueue_head);
243 dprintf(LOG_INFO, FNAME, "command queue is full. "
244 "drop the oldest one (fd=%d)", ctx->s);
246 TAILQ_REMOVE(&commandqueue_head, ctx, link);
247 dhcp6_ctl_closecommand(ctx);
250 /* insert the next context to the queue */
251 memset(new, 0, sizeof(*new));
252 new->s = s;
253 new->callback = callback;
254 new->input_len = sizeof(struct dhcp6ctl);
255 TAILQ_INSERT_TAIL(&commandqueue_head, new, link);
256 commands++;
258 return (0);
260 fail:
261 close(s);
263 return (-1);
266 void
267 dhcp6_ctl_closecommand(ctx)
268 struct dhcp6_commandctx *ctx;
270 close(ctx->s);
271 free(ctx);
273 if (commands == 0) {
274 dprintf(LOG_ERR, FNAME, "assumption error: "
275 "command queue is empty?");
276 exit(1); /* XXX */
278 commands--;
280 return;
284 dhcp6_ctl_readcommand(read_fds)
285 fd_set *read_fds;
287 struct dhcp6_commandctx *ctx, *ctx_next;
288 char *cp;
289 int cc, resid, result;
290 struct dhcp6ctl *ctlhead;
292 for (ctx = TAILQ_FIRST(&commandqueue_head); ctx != NULL;
293 ctx = ctx_next) {
294 ctx_next = TAILQ_NEXT(ctx, link);
296 if (FD_ISSET(ctx->s, read_fds)) {
297 cp = ctx->inputbuf + ctx->input_filled;
298 resid = ctx->input_len - ctx->input_filled;
300 cc = read(ctx->s, cp, resid);
301 if (cc < 0) {
302 dprintf(LOG_WARNING, FNAME, "read failed: %s",
303 strerror(errno));
304 goto closecommand;
306 if (cc == 0) {
307 dprintf(LOG_INFO, FNAME,
308 "control channel was reset by peer");
309 goto closecommand;
312 ctx->input_filled += cc;
313 if (ctx->input_filled < ctx->input_len)
314 continue; /* we need more data */
315 else if (ctx->input_filled == sizeof(*ctlhead)) {
316 ctlhead = (struct dhcp6ctl *)ctx->inputbuf;
317 ctx->input_len += ntohs(ctlhead->len);
320 if (ctx->input_filled == ctx->input_len) {
321 /* we're done. execute the command. */
322 result = (ctx->callback)(ctx->inputbuf,
323 ctx->input_len);
325 switch (result) {
326 case DHCP6CTL_R_DONE:
327 case DHCP6CTL_R_FAILURE:
328 goto closecommand;
329 default:
330 break;
332 } else if (ctx->input_len > sizeof(ctx->inputbuf)) {
333 dprintf(LOG_INFO, FNAME,
334 "too large command (%d bytes)",
335 ctx->input_len);
336 goto closecommand;
339 continue;
341 closecommand:
342 TAILQ_REMOVE(&commandqueue_head, ctx, link);
343 dhcp6_ctl_closecommand(ctx);
347 return (0);
351 dhcp6_ctl_setreadfds(read_fds, maxfdp)
352 fd_set *read_fds;
353 int *maxfdp;
355 int maxfd = *maxfdp;
356 struct dhcp6_commandctx *ctx;
358 for (ctx = TAILQ_FIRST(&commandqueue_head); ctx != NULL;
359 ctx = TAILQ_NEXT(ctx, link)) {
360 FD_SET(ctx->s, read_fds);
361 if (ctx->s > maxfd)
362 maxfd = ctx->s;
365 *maxfdp = maxfd;
367 return (0);