cluster - Major kernel component work (diskiocom, xdisk, kdmsg)
[dragonfly.git] / sbin / ifconfig / ifcarp.c
1 /*
2  * Copyright (c) 2002 Michael Shalayeff. All rights reserved.
3  * Copyright (c) 2003 Ryan McBride. 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  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL THE AUTHOR OR HIS RELATIVES BE LIABLE FOR ANY DIRECT,
18  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
20  * SERVICES; LOSS OF MIND, USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
22  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
23  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
24  * THE POSSIBILITY OF SUCH DAMAGE.
25  */
26 /*
27  * $FreeBSD: src/sbin/ifconfig/ifcarp.c,v 1.2 2005/02/22 14:07:47 glebius Exp $
28  * $OpenBSD: ifconfig.c,v 1.82 2003/10/19 05:43:35 mcbride Exp $
29  */
30
31 #include <sys/param.h>
32 #include <sys/ioctl.h>
33 #include <sys/socket.h>
34 #include <sys/sockio.h>
35
36 #include <stdlib.h>
37 #include <unistd.h>
38
39 #include <net/ethernet.h>
40 #include <net/if.h>
41 #include <netinet/in.h>
42 #include <netinet/ip_carp.h>
43 #include <net/route.h>
44
45 #include <arpa/inet.h>
46
47 #include <ctype.h>
48 #include <stdio.h>
49 #include <string.h>
50 #include <err.h>
51 #include <errno.h>
52
53 #include "ifconfig.h"
54
55 static const char *carp_states[] = { CARP_STATES };
56
57 void carp_status(int s);
58 void setcarp_advbase(const char *,int, int, const struct afswtch *rafp);
59 void setcarp_advskew(const char *, int, int, const struct afswtch *rafp);
60 void setcarp_passwd(const char *, int, int, const struct afswtch *rafp);
61 void setcarp_vhid(const char *, int, int, const struct afswtch *rafp);
62
63 void
64 carp_status(int s)
65 {
66         const char *state;
67         struct carpreq carpr;
68         struct ifdrv ifd;
69         char devname[IFNAMSIZ];
70
71         memset((char *)&carpr, 0, sizeof(struct carpreq));
72         ifr.ifr_data = (caddr_t)&carpr;
73
74         if (ioctl(s, SIOCGVH, (caddr_t)&ifr) == -1)
75                 return;
76
77         if (carpr.carpr_vhid > 0) {
78                 if (carpr.carpr_state > CARP_MAXSTATE)
79                         state = "<UNKNOWN>";
80                 else
81                         state = carp_states[carpr.carpr_state];
82
83                 printf("\tcarp: %s vhid %d advbase %d advskew %d\n",
84                     state, carpr.carpr_vhid, carpr.carpr_advbase,
85                     carpr.carpr_advskew);
86         }
87
88         memset(&ifd, 0, sizeof(ifd));
89         strncpy(ifd.ifd_name, ifr.ifr_name, sizeof(ifd.ifd_name));
90         ifd.ifd_cmd = CARPGDEVNAME;
91         ifd.ifd_len = sizeof(devname);
92         ifd.ifd_data = devname;
93         if (ioctl(s, SIOCGDRVSPEC, &ifd) < 0)
94                 strlcpy(devname, "none", sizeof(devname));
95         if (devname[0] != '\0')
96                 printf("\tcarpdev: %s\n", devname);
97 }
98
99 void
100 setcarp_passwd(const char *val, int d, int s, const struct afswtch *afp)
101 {
102         struct carpreq carpr;
103
104         memset((char *)&carpr, 0, sizeof(struct carpreq));
105         ifr.ifr_data = (caddr_t)&carpr;
106
107         if (ioctl(s, SIOCGVH, (caddr_t)&ifr) == -1)
108                 err(1, "SIOCGVH");
109
110         /* XXX Should hash the password into the key here, perhaps? */
111         strlcpy(carpr.carpr_key, val, CARP_KEY_LEN);
112
113         if (ioctl(s, SIOCSVH, (caddr_t)&ifr) == -1)
114                 err(1, "SIOCSVH");
115 }
116
117 void
118 setcarp_vhid(const char *val, int d, int s, const struct afswtch *afp)
119 {
120         int vhid;
121         struct carpreq carpr;
122
123         vhid = atoi(val);
124
125         if (vhid <= 0)
126                 errx(1, "vhid must be greater than 0");
127
128         memset((char *)&carpr, 0, sizeof(struct carpreq));
129         ifr.ifr_data = (caddr_t)&carpr;
130
131         if (ioctl(s, SIOCGVH, (caddr_t)&ifr) == -1)
132                 err(1, "SIOCGVH");
133
134         carpr.carpr_vhid = vhid;
135
136         if (ioctl(s, SIOCSVH, (caddr_t)&ifr) == -1)
137                 err(1, "SIOCSVH");
138 }
139
140 void
141 setcarp_advskew(const char *val, int d, int s, const struct afswtch *afp)
142 {
143         int advskew;
144         struct carpreq carpr;
145
146         advskew = atoi(val);
147
148         memset((char *)&carpr, 0, sizeof(struct carpreq));
149         ifr.ifr_data = (caddr_t)&carpr;
150
151         if (ioctl(s, SIOCGVH, (caddr_t)&ifr) == -1)
152                 err(1, "SIOCGVH");
153
154         carpr.carpr_advskew = advskew;
155
156         if (ioctl(s, SIOCSVH, (caddr_t)&ifr) == -1)
157                 err(1, "SIOCSVH");
158 }
159
160 void
161 setcarp_advbase(const char *val, int d, int s, const struct afswtch *afp)
162 {
163         int advbase;
164         struct carpreq carpr;
165
166         advbase = atoi(val);
167
168         memset((char *)&carpr, 0, sizeof(struct carpreq));
169         ifr.ifr_data = (caddr_t)&carpr;
170
171         if (ioctl(s, SIOCGVH, (caddr_t)&ifr) == -1)
172                 err(1, "SIOCGVH");
173
174         carpr.carpr_advbase = advbase;
175
176         if (ioctl(s, SIOCSVH, (caddr_t)&ifr) == -1)
177                 err(1, "SIOCSVH");
178 }
179
180 static void
181 getcarp_vhaddr(const char *val, int d, int s, const struct afswtch *afp)
182 {
183 #define VHADDR_PFMT     "%-15s %-15s %s\n"
184
185         struct ifdrv ifd;
186         struct ifcarpvhaddr *carpa;
187         int count, i;
188
189         memset(&ifd, 0, sizeof(ifd));
190         strncpy(ifd.ifd_name, ifr.ifr_name, sizeof(ifd.ifd_name));
191         ifd.ifd_cmd = CARPGVHADDR;
192         if (ioctl(s, SIOCGDRVSPEC, &ifd) < 0)
193                 return;
194         if (ifd.ifd_len != 0) {
195                 carpa = malloc(ifd.ifd_len);
196                 if (carpa == NULL)
197                         return;
198
199                 ifd.ifd_cmd = CARPGVHADDR;
200                 ifd.ifd_data = carpa;
201                 if (ioctl(s, SIOCGDRVSPEC, &ifd) < 0) {
202                         free(carpa);
203                         return;
204                 }
205         } else {
206                 carpa = NULL;
207         }
208         count = ifd.ifd_len / sizeof(*carpa);
209         if (count != 0)
210                 printf(VHADDR_PFMT, "virtual addr", "backing addr", "flags");
211         for (i = 0; i < count; ++i) {
212                 char flags[16];
213                 char baddr[INET_ADDRSTRLEN];
214                 int a = 0;
215
216                 memset(flags, 0, sizeof(flags));
217                 flags[a] = '*';
218                 if (carpa[i].carpa_flags & CARP_VHAF_OWNER)
219                         flags[a++] = 'O';
220
221                 memset(baddr, 0, sizeof(baddr));
222                 baddr[0] = '*';
223                 if (carpa[i].carpa_baddr.sin_addr.s_addr != INADDR_ANY) {
224                         inet_ntop(AF_INET, &carpa[i].carpa_baddr.sin_addr,
225                                   baddr, sizeof(baddr));
226                 }
227
228                 printf(VHADDR_PFMT, inet_ntoa(carpa[i].carpa_addr.sin_addr),
229                        baddr, flags);
230         }
231         if (carpa != NULL)
232                 free(carpa);
233
234 #undef VHADDR_PFMT
235 }
236
237 static struct cmd carp_cmds[] = {
238         DEF_CMD_ARG("advbase",  setcarp_advbase),
239         DEF_CMD_ARG("advskew",  setcarp_advskew),
240         DEF_CMD_ARG("pass",     setcarp_passwd),
241         DEF_CMD_ARG("vhid",     setcarp_vhid),
242         DEF_CMD("vhaddr", 1,    getcarp_vhaddr)
243 };
244 static struct afswtch af_carp = {
245         .af_name        = "af_carp",
246         .af_af          = AF_UNSPEC,
247         .af_other_status = carp_status,
248 };
249
250 static __constructor(101) void
251 carp_ctor(void)
252 {
253 #define N(a)    (sizeof(a) / sizeof(a[0]))
254         int i;
255
256         for (i = 0; i < N(carp_cmds);  i++)
257                 cmd_register(&carp_cmds[i]);
258         af_register(&af_carp);
259 #undef N
260 }