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