xref: /freebsd/lib/libifconfig/libifconfig_carp.c (revision ae2f0b2611f112b400177db951f9bd992de72b4d)
1 /*
2  * Copyright (c) 1983, 1993
3  *  The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * $FreeBSD$
30  */
31 #include <sys/param.h>
32 #include <sys/ioctl.h>
33 
34 #include <net/if.h>
35 #include <netinet/ip_carp.h>
36 #include <netinet/ip_carp_nl.h>
37 
38 #include <netlink/netlink.h>
39 #include <netlink/netlink_generic.h>
40 #include <netlink/netlink_snl.h>
41 #include <netlink/netlink_snl_generic.h>
42 #include <netlink/netlink_snl_route.h>
43 
44 #include <string.h>
45 #include <strings.h>
46 
47 #include "libifconfig.h"
48 #include "libifconfig_internal.h"
49 
50 #include <stdio.h>
51 
52 #define	_OUT(_field)	offsetof(struct ifconfig_carp, _field)
53 static struct snl_attr_parser ap_carp_get[] = {
54 	{ .type = CARP_NL_VHID, .off = _OUT(carpr_vhid), .cb = snl_attr_get_uint32 },
55 	{ .type = CARP_NL_STATE, .off = _OUT(carpr_state), .cb = snl_attr_get_uint32 },
56 	{ .type = CARP_NL_ADVBASE, .off = _OUT(carpr_advbase), .cb = snl_attr_get_int32 },
57 	{ .type = CARP_NL_ADVSKEW, .off = _OUT(carpr_advskew), .cb = snl_attr_get_int32 },
58 	{ .type = CARP_NL_KEY, .off = _OUT(carpr_key), .cb = snl_attr_get_string },
59 	{ .type = CARP_NL_ADDR, .off = _OUT(carpr_addr), .cb = snl_attr_get_in_addr },
60 	{ .type = CARP_NL_ADDR6, .off = _OUT(carpr_addr6), .cb = snl_attr_get_in6_addr },
61 };
62 #undef _OUT
63 
64 SNL_DECLARE_GENL_PARSER(carp_get_parser, ap_carp_get);
65 
66 static int
67 _ifconfig_carp_get(ifconfig_handle_t *h, const char *name,
68     struct ifconfig_carp *carp, size_t ncarp, uint32_t vhid)
69 {
70 	struct snl_state ss = {};
71 	struct snl_errmsg_data e = {};
72 	struct snl_writer nw;
73 	struct nlmsghdr *hdr;
74 	size_t i = 0;
75 	uint32_t seq_id;
76 	unsigned int ifindex;
77 	int family_id;
78 
79 	ifconfig_error_clear(h);
80 
81 	ifindex = if_nametoindex(name);
82 	if (ifindex == 0) {
83 		ifconfig_error(h, NETLINK, ENOENT);
84 		return (-1);
85 	}
86 
87 	if (! snl_init(&ss, NETLINK_GENERIC)) {
88 		ifconfig_error(h, NETLINK, ENOTSUP);
89 		return (-1);
90 	}
91 
92 	snl_init_writer(&ss, &nw);
93 
94 	family_id = snl_get_genl_family(&ss, CARP_NL_FAMILY_NAME);
95 	if (family_id == 0) {
96 		ifconfig_error(h, NETLINK, EPROTONOSUPPORT);
97 		goto out;
98 	}
99 
100 	hdr = snl_create_genl_msg_request(&nw, family_id, CARP_NL_CMD_GET);
101 	hdr->nlmsg_flags |= NLM_F_DUMP;
102 
103 	snl_add_msg_attr_u32(&nw, CARP_NL_IFINDEX, ifindex);
104 
105 	if (vhid != 0)
106 		snl_add_msg_attr_u32(&nw, CARP_NL_VHID, vhid);
107 
108 	hdr = snl_finalize_msg(&nw);
109 	if (hdr == NULL) {
110 		ifconfig_error(h, NETLINK, ENOMEM);
111 		goto out;
112 	}
113 	seq_id = hdr->nlmsg_seq;
114 	if (! snl_send_message(&ss, hdr)) {
115 		ifconfig_error(h, NETLINK, EIO);
116 		goto out;
117 	}
118 
119 	while ((hdr = snl_read_reply_multi(&ss, seq_id, &e)) != NULL) {
120 		if (e.error != 0) {
121 			ifconfig_error(h, NETLINK, e.error);
122 			break;
123 		}
124 
125 		if (i >= ncarp) {
126 			ifconfig_error(h, NETLINK, E2BIG);
127 			break;
128 		}
129 
130 		memset(&carp[i], 0, sizeof(carp[0]));
131 		if (! snl_parse_nlmsg(&ss, hdr, &carp_get_parser, &carp[i]))
132 			continue;
133 
134 		i++;
135 		carp[0].carpr_count = i;
136 
137 		if (i > ncarp) {
138 			ifconfig_error(h, NETLINK, E2BIG);
139 			break;
140 		}
141 	}
142 
143 out:
144 	snl_free(&ss);
145 
146 	return (h->error.errcode ? -1 : 0);
147 }
148 
149 int
150 ifconfig_carp_set_info(ifconfig_handle_t *h, const char *name,
151     const struct ifconfig_carp *carpr)
152 {
153 	struct snl_state ss = {};
154 	struct snl_writer nw;
155 	struct nlmsghdr *hdr;
156 	unsigned int ifindex;
157 	int family_id;
158 	uint32_t seq_id;
159 
160 	ifconfig_error_clear(h);
161 
162 	ifindex = if_nametoindex(name);
163 	if (ifindex == 0) {
164 		ifconfig_error(h, NETLINK, ENOENT);
165 		return (-1);
166 	}
167 
168 	if (! snl_init(&ss, NETLINK_GENERIC)) {
169 		ifconfig_error(h, NETLINK, ENOTSUP);
170 		return (-1);
171 	}
172 
173 	snl_init_writer(&ss, &nw);
174 
175 	family_id = snl_get_genl_family(&ss, CARP_NL_FAMILY_NAME);
176 	if (family_id == 0) {
177 		ifconfig_error(h, NETLINK, EPROTONOSUPPORT);
178 		return (-1);
179 	}
180 	hdr = snl_create_genl_msg_request(&nw, family_id, CARP_NL_CMD_SET);
181 
182 	snl_add_msg_attr_u32(&nw, CARP_NL_VHID, carpr->carpr_vhid);
183 	snl_add_msg_attr_u32(&nw, CARP_NL_STATE, carpr->carpr_state);
184 	snl_add_msg_attr_s32(&nw, CARP_NL_ADVBASE, carpr->carpr_advbase);
185 	snl_add_msg_attr_s32(&nw, CARP_NL_ADVSKEW, carpr->carpr_advskew);
186 	snl_add_msg_attr_u32(&nw, CARP_NL_IFINDEX, ifindex);
187 	snl_add_msg_attr(&nw, CARP_NL_ADDR, sizeof(carpr->carpr_addr),
188 	    &carpr->carpr_addr);
189 	snl_add_msg_attr(&nw, CARP_NL_ADDR6, sizeof(carpr->carpr_addr6),
190 	    &carpr->carpr_addr6);
191 
192 	hdr = snl_finalize_msg(&nw);
193 	if (hdr == NULL) {
194 		ifconfig_error(h, NETLINK, ENOMEM);
195 		goto out;
196 	}
197 
198 	seq_id = hdr->nlmsg_seq;
199 	if (! snl_send_message(&ss, hdr)) {
200 		ifconfig_error(h, NETLINK, EIO);
201 		goto out;
202 	}
203 
204 	struct snl_errmsg_data e = { };
205 	if (! snl_read_reply_code(&ss, seq_id, &e))
206 		ifconfig_error(h, NETLINK, e.error);
207 
208 out:
209 	snl_free(&ss);
210 
211 	return (h->error.errcode ? -1 : 0);
212 }
213 
214 int
215 ifconfig_carp_get_vhid(ifconfig_handle_t *h, const char *name,
216     struct ifconfig_carp *carp, uint32_t vhid)
217 {
218 	return (_ifconfig_carp_get(h, name, carp, 1, vhid));
219 }
220 
221 int
222 ifconfig_carp_get_info(ifconfig_handle_t *h, const char *name,
223     struct ifconfig_carp *carp, size_t ncarp)
224 {
225 	return (_ifconfig_carp_get(h, name, carp, ncarp, 0));
226 }
227