xref: /freebsd/usr.sbin/rtadvd/control_client.c (revision 43a5ec4eb41567cc92586503212743d89686d78f)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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  * $FreeBSD$
29  *
30  */
31 
32 #include <sys/queue.h>
33 #include <sys/types.h>
34 #include <sys/socket.h>
35 #include <sys/stat.h>
36 #include <sys/un.h>
37 #include <sys/uio.h>
38 #include <net/if.h>
39 #include <net/if_dl.h>
40 #include <netinet/in.h>
41 #include <netinet/icmp6.h>
42 #include <fcntl.h>
43 #include <errno.h>
44 #include <netdb.h>
45 #include <unistd.h>
46 #include <signal.h>
47 #include <string.h>
48 #include <stdarg.h>
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <syslog.h>
52 
53 #include "pathnames.h"
54 #include "rtadvd.h"
55 #include "if.h"
56 #include "control.h"
57 #include "control_client.h"
58 
59 int
60 cm_handler_client(int fd, int state, char *buf_orig)
61 {
62 	char buf[CM_MSG_MAXLEN];
63 	struct ctrl_msg_hdr *cm;
64 	struct ctrl_msg_hdr *cm_orig;
65 	int error;
66 	char *msg;
67 	char *msg_orig;
68 
69 	syslog(LOG_DEBUG, "<%s> enter", __func__);
70 
71 	memset(buf, 0, sizeof(buf));
72 	cm = (struct ctrl_msg_hdr *)buf;
73 	cm_orig = (struct ctrl_msg_hdr *)buf_orig;
74 	msg = (char *)buf + sizeof(*cm);
75 	msg_orig = (char *)buf_orig + sizeof(*cm_orig);
76 
77 	if (cm_orig->cm_len > CM_MSG_MAXLEN) {
78 		syslog(LOG_DEBUG, "<%s> msg too long", __func__);
79 		close(fd);
80 		return (-1);
81 	}
82 	cm->cm_type = cm_orig->cm_type;
83 	if (cm_orig->cm_len > sizeof(*cm_orig)) {
84 		memcpy(msg, msg_orig, cm_orig->cm_len - sizeof(*cm));
85 		cm->cm_len = cm_orig->cm_len;
86 	}
87 	while (state != CM_STATE_EOM) {
88 		syslog(LOG_DEBUG, "<%s> state = %d", __func__, state);
89 
90 		switch (state) {
91 		case CM_STATE_INIT:
92 			state = CM_STATE_EOM;
93 			break;
94 		case CM_STATE_MSG_DISPATCH:
95 			cm->cm_version = CM_VERSION;
96 			error = cm_send(fd, buf);
97 			if (error) {
98 				syslog(LOG_WARNING,
99 				    "<%s> cm_send()", __func__);
100 				return (-1);
101 			}
102 			state = CM_STATE_ACK_WAIT;
103 			break;
104 		case CM_STATE_ACK_WAIT:
105 			error = cm_recv(fd, buf);
106 			if (error) {
107 				syslog(LOG_ERR,
108 				    "<%s> cm_recv()", __func__);
109 				close(fd);
110 				return (-1);
111 			}
112 			switch (cm->cm_type) {
113 			case CM_TYPE_ACK:
114 				syslog(LOG_DEBUG,
115 				    "<%s> CM_TYPE_ACK", __func__);
116 				break;
117 			case CM_TYPE_ERR:
118 				syslog(LOG_DEBUG,
119 				    "<%s> CM_TYPE_ERR", __func__);
120 				close(fd);
121 				return (-1);
122 			default:
123 				syslog(LOG_DEBUG,
124 				    "<%s> unknown status", __func__);
125 				close(fd);
126 				return (-1);
127 			}
128 			memcpy(buf_orig, buf, cm->cm_len);
129 			state = CM_STATE_EOM;
130 			break;
131 		}
132 	}
133 	close(fd);
134 	return (0);
135 }
136