1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (C) 2011 Hiroki Sato <hrs@FreeBSD.org>
5 * 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 *
16 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
23 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
25 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
26 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 */
29
30 #include <sys/queue.h>
31 #include <sys/types.h>
32 #include <sys/socket.h>
33 #include <sys/stat.h>
34 #include <sys/un.h>
35 #include <sys/uio.h>
36 #include <net/if.h>
37 #include <net/if_dl.h>
38 #include <netinet/in.h>
39 #include <netinet/icmp6.h>
40 #include <fcntl.h>
41 #include <errno.h>
42 #include <netdb.h>
43 #include <unistd.h>
44 #include <signal.h>
45 #include <string.h>
46 #include <stdarg.h>
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <syslog.h>
50
51 #include "pathnames.h"
52 #include "rtadvd.h"
53 #include "if.h"
54 #include "control.h"
55 #include "control_client.h"
56
57 int
cm_handler_client(int fd,int state,char * buf_orig)58 cm_handler_client(int fd, int state, char *buf_orig)
59 {
60 char buf[CM_MSG_MAXLEN];
61 struct ctrl_msg_hdr *cm;
62 struct ctrl_msg_hdr *cm_orig;
63 int error;
64 char *msg;
65 char *msg_orig;
66
67 syslog(LOG_DEBUG, "<%s> enter", __func__);
68
69 memset(buf, 0, sizeof(buf));
70 cm = (struct ctrl_msg_hdr *)buf;
71 cm_orig = (struct ctrl_msg_hdr *)buf_orig;
72 msg = (char *)buf + sizeof(*cm);
73 msg_orig = (char *)buf_orig + sizeof(*cm_orig);
74
75 if (cm_orig->cm_len > CM_MSG_MAXLEN) {
76 syslog(LOG_DEBUG, "<%s> msg too long", __func__);
77 close(fd);
78 return (-1);
79 }
80 cm->cm_type = cm_orig->cm_type;
81 if (cm_orig->cm_len > sizeof(*cm_orig)) {
82 memcpy(msg, msg_orig, cm_orig->cm_len - sizeof(*cm));
83 cm->cm_len = cm_orig->cm_len;
84 }
85 while (state != CM_STATE_EOM) {
86 syslog(LOG_DEBUG, "<%s> state = %d", __func__, state);
87
88 switch (state) {
89 case CM_STATE_INIT:
90 state = CM_STATE_EOM;
91 break;
92 case CM_STATE_MSG_DISPATCH:
93 cm->cm_version = CM_VERSION;
94 error = cm_send(fd, buf);
95 if (error) {
96 syslog(LOG_WARNING,
97 "<%s> cm_send()", __func__);
98 return (-1);
99 }
100 state = CM_STATE_ACK_WAIT;
101 break;
102 case CM_STATE_ACK_WAIT:
103 error = cm_recv(fd, buf);
104 if (error) {
105 syslog(LOG_ERR,
106 "<%s> cm_recv()", __func__);
107 close(fd);
108 return (-1);
109 }
110 switch (cm->cm_type) {
111 case CM_TYPE_ACK:
112 syslog(LOG_DEBUG,
113 "<%s> CM_TYPE_ACK", __func__);
114 break;
115 case CM_TYPE_ERR:
116 syslog(LOG_DEBUG,
117 "<%s> CM_TYPE_ERR", __func__);
118 close(fd);
119 return (-1);
120 default:
121 syslog(LOG_DEBUG,
122 "<%s> unknown status", __func__);
123 close(fd);
124 return (-1);
125 }
126 memcpy(buf_orig, buf, cm->cm_len);
127 state = CM_STATE_EOM;
128 break;
129 }
130 }
131 close(fd);
132 return (0);
133 }
134