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 #include <sys/param.h>
30 #include <sys/ioctl.h>
31
32 #include <net/if.h>
33 #include <netinet/ip_carp.h>
34 #include <netinet/ip_carp_nl.h>
35
36 #include <netlink/netlink.h>
37 #include <netlink/netlink_generic.h>
38 #include <netlink/netlink_snl.h>
39 #include <netlink/netlink_snl_generic.h>
40 #include <netlink/netlink_snl_route.h>
41
42 #include <string.h>
43 #include <strings.h>
44
45 #include "libifconfig.h"
46 #include "libifconfig_internal.h"
47
48 #include <stdio.h>
49
50 #define _OUT(_field) offsetof(struct ifconfig_carp, _field)
51 static struct snl_attr_parser ap_carp_get[] = {
52 { .type = CARP_NL_VHID, .off = _OUT(carpr_vhid), .cb = snl_attr_get_uint32 },
53 { .type = CARP_NL_STATE, .off = _OUT(carpr_state), .cb = snl_attr_get_uint32 },
54 { .type = CARP_NL_ADVBASE, .off = _OUT(carpr_advbase), .cb = snl_attr_get_int32 },
55 { .type = CARP_NL_ADVSKEW, .off = _OUT(carpr_advskew), .cb = snl_attr_get_int32 },
56 { .type = CARP_NL_KEY, .off = _OUT(carpr_key), .cb = snl_attr_copy_string, .arg_u32 = CARP_KEY_LEN },
57 { .type = CARP_NL_ADDR, .off = _OUT(carpr_addr), .cb = snl_attr_get_in_addr },
58 { .type = CARP_NL_ADDR6, .off = _OUT(carpr_addr6), .cb = snl_attr_get_in6_addr },
59 { .type = CARP_NL_VERSION, .off = _OUT(carpr_version), .cb = snl_attr_get_uint8 },
60 { .type = CARP_NL_VRRP_PRIORITY, .off = _OUT(carpr_vrrp_prio), .cb = snl_attr_get_uint8 },
61 { .type = CARP_NL_VRRP_ADV_INTER, .off = _OUT(carpr_vrrp_adv_inter), .cb = snl_attr_get_uint16 },
62 };
63 #undef _OUT
64
65 SNL_DECLARE_GENL_PARSER(carp_get_parser, ap_carp_get);
66
67 static int
_ifconfig_carp_get(ifconfig_handle_t * h,const char * name,struct ifconfig_carp * carp,size_t ncarp,uint32_t vhid)68 _ifconfig_carp_get(ifconfig_handle_t *h, const char *name,
69 struct ifconfig_carp *carp, size_t ncarp, uint32_t vhid)
70 {
71 struct snl_state ss = {};
72 struct snl_errmsg_data e = {};
73 struct snl_writer nw;
74 struct nlmsghdr *hdr;
75 size_t i = 0;
76 uint32_t seq_id;
77 int family_id;
78
79 ifconfig_error_clear(h);
80
81 bzero(carp, sizeof(*carp) * ncarp);
82
83 if (! snl_init(&ss, NETLINK_GENERIC)) {
84 ifconfig_error(h, NETLINK, ENOTSUP);
85 return (-1);
86 }
87
88 snl_init_writer(&ss, &nw);
89
90 family_id = snl_get_genl_family(&ss, CARP_NL_FAMILY_NAME);
91 if (family_id == 0) {
92 ifconfig_error(h, NETLINK, EPROTONOSUPPORT);
93 goto out;
94 }
95
96 hdr = snl_create_genl_msg_request(&nw, family_id, CARP_NL_CMD_GET);
97 hdr->nlmsg_flags |= NLM_F_DUMP;
98
99 snl_add_msg_attr_string(&nw, CARP_NL_IFNAME, name);
100
101 if (vhid != 0)
102 snl_add_msg_attr_u32(&nw, CARP_NL_VHID, vhid);
103
104 hdr = snl_finalize_msg(&nw);
105 if (hdr == NULL) {
106 ifconfig_error(h, NETLINK, ENOMEM);
107 goto out;
108 }
109 seq_id = hdr->nlmsg_seq;
110 if (! snl_send_message(&ss, hdr)) {
111 ifconfig_error(h, NETLINK, EIO);
112 goto out;
113 }
114
115 while ((hdr = snl_read_reply_multi(&ss, seq_id, &e)) != NULL) {
116 if (e.error != 0) {
117 ifconfig_error(h, NETLINK, e.error);
118 break;
119 }
120
121 if (i >= ncarp) {
122 ifconfig_error(h, NETLINK, E2BIG);
123 break;
124 }
125
126 memset(&carp[i], 0, sizeof(carp[0]));
127 if (! snl_parse_nlmsg(&ss, hdr, &carp_get_parser, &carp[i]))
128 continue;
129
130 i++;
131 carp[0].carpr_count = i;
132
133 if (i > ncarp) {
134 ifconfig_error(h, NETLINK, E2BIG);
135 break;
136 }
137 }
138
139 out:
140 snl_free(&ss);
141
142 return (h->error.errcode ? -1 : 0);
143 }
144
145 int
ifconfig_carp_set_info(ifconfig_handle_t * h,const char * name,const struct ifconfig_carp * carpr)146 ifconfig_carp_set_info(ifconfig_handle_t *h, const char *name,
147 const struct ifconfig_carp *carpr)
148 {
149 struct snl_state ss = {};
150 struct snl_writer nw;
151 struct nlmsghdr *hdr;
152 int family_id;
153 uint32_t seq_id;
154
155 ifconfig_error_clear(h);
156
157 if (! snl_init(&ss, NETLINK_GENERIC)) {
158 ifconfig_error(h, NETLINK, ENOTSUP);
159 return (-1);
160 }
161
162 snl_init_writer(&ss, &nw);
163
164 family_id = snl_get_genl_family(&ss, CARP_NL_FAMILY_NAME);
165 if (family_id == 0) {
166 ifconfig_error(h, NETLINK, EPROTONOSUPPORT);
167 return (-1);
168 }
169 hdr = snl_create_genl_msg_request(&nw, family_id, CARP_NL_CMD_SET);
170
171 snl_add_msg_attr_u32(&nw, CARP_NL_VHID, carpr->carpr_vhid);
172 snl_add_msg_attr_u32(&nw, CARP_NL_STATE, carpr->carpr_state);
173 snl_add_msg_attr_s32(&nw, CARP_NL_ADVBASE, carpr->carpr_advbase);
174 snl_add_msg_attr_s32(&nw, CARP_NL_ADVSKEW, carpr->carpr_advskew);
175 snl_add_msg_attr_string(&nw, CARP_NL_IFNAME, name);
176 snl_add_msg_attr(&nw, CARP_NL_ADDR, sizeof(carpr->carpr_addr),
177 &carpr->carpr_addr);
178 snl_add_msg_attr(&nw, CARP_NL_ADDR6, sizeof(carpr->carpr_addr6),
179 &carpr->carpr_addr6);
180 snl_add_msg_attr_string(&nw, CARP_NL_KEY, carpr->carpr_key);
181 snl_add_msg_attr_u8(&nw, CARP_NL_VERSION, carpr->carpr_version);
182 snl_add_msg_attr_u8(&nw, CARP_NL_VRRP_PRIORITY, carpr->carpr_vrrp_prio);
183 snl_add_msg_attr_u16(&nw, CARP_NL_VRRP_ADV_INTER, carpr->carpr_vrrp_adv_inter);
184
185 hdr = snl_finalize_msg(&nw);
186 if (hdr == NULL) {
187 ifconfig_error(h, NETLINK, ENOMEM);
188 goto out;
189 }
190
191 seq_id = hdr->nlmsg_seq;
192 if (! snl_send_message(&ss, hdr)) {
193 ifconfig_error(h, NETLINK, EIO);
194 goto out;
195 }
196
197 struct snl_errmsg_data e = { };
198 if (! snl_read_reply_code(&ss, seq_id, &e))
199 ifconfig_error(h, NETLINK, e.error);
200
201 out:
202 snl_free(&ss);
203
204 return (h->error.errcode ? -1 : 0);
205 }
206
207 int
ifconfig_carp_get_vhid(ifconfig_handle_t * h,const char * name,struct ifconfig_carp * carp,uint32_t vhid)208 ifconfig_carp_get_vhid(ifconfig_handle_t *h, const char *name,
209 struct ifconfig_carp *carp, uint32_t vhid)
210 {
211 return (_ifconfig_carp_get(h, name, carp, 1, vhid));
212 }
213
214 int
ifconfig_carp_get_info(ifconfig_handle_t * h,const char * name,struct ifconfig_carp * carp,size_t ncarp)215 ifconfig_carp_get_info(ifconfig_handle_t *h, const char *name,
216 struct ifconfig_carp *carp, size_t ncarp)
217 {
218 return (_ifconfig_carp_get(h, name, carp, ncarp, 0));
219 }
220