Merge commit 'origin/vendor/PAM_PASSWDQC'
[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  * $DragonFly: src/sbin/ifconfig/ifcarp.c,v 1.1 2007/08/16 20:03:55 dillon Exp $
30  */
31
32 #include <sys/param.h>
33 #include <sys/ioctl.h>
34 #include <sys/socket.h>
35 #include <sys/sockio.h>
36
37 #include <stdlib.h>
38 #include <unistd.h>
39
40 #include <net/ethernet.h>
41 #include <net/if.h>
42 #include <netinet/in.h>
43 #include <netinet/ip_carp.h>
44 #include <net/route.h>
45
46 #include <arpa/inet.h>
47
48 #include <ctype.h>
49 #include <stdio.h>
50 #include <string.h>
51 #include <stdlib.h>
52 #include <unistd.h>
53 #include <err.h>
54 #include <errno.h>
55
56 #include "ifconfig.h"
57
58 static const char *carp_states[] = { CARP_STATES };
59
60 void carp_status(int s);
61 void setcarp_advbase(const char *,int, int, const struct afswtch *rafp);
62 void setcarp_advskew(const char *, int, int, const struct afswtch *rafp);
63 void setcarp_passwd(const char *, int, int, const struct afswtch *rafp);
64 void setcarp_vhid(const char *, int, int, const struct afswtch *rafp);
65
66 void
67 carp_status(int s)
68 {
69         const char *state;
70         struct carpreq carpr;
71         struct ifdrv ifd;
72         char devname[IFNAMSIZ];
73         int count, i;
74
75         memset((char *)&carpr, 0, sizeof(struct carpreq));
76         ifr.ifr_data = (caddr_t)&carpr;
77
78         if (ioctl(s, SIOCGVH, (caddr_t)&ifr) == -1)
79                 return;
80
81         if (carpr.carpr_vhid > 0) {
82                 if (carpr.carpr_state > CARP_MAXSTATE)
83                         state = "<UNKNOWN>";
84                 else
85                         state = carp_states[carpr.carpr_state];
86
87                 printf("\tcarp: %s vhid %d advbase %d advskew %d\n",
88                     state, carpr.carpr_vhid, carpr.carpr_advbase,
89                     carpr.carpr_advskew);
90         }
91
92         memset(&ifd, 0, sizeof(ifd));
93         strncpy(ifd.ifd_name, ifr.ifr_name, sizeof(ifd.ifd_name));
94         ifd.ifd_cmd = CARPGDEVNAME;
95         ifd.ifd_len = sizeof(devname);
96         ifd.ifd_data = devname;
97         if (ioctl(s, SIOCGDRVSPEC, &ifd) < 0)
98                 strlcpy(devname, "none", sizeof(devname));
99         if (devname[0] != '\0')
100                 printf("\tcarpdev: %s\n", devname);
101 }
102
103 void
104 setcarp_passwd(const char *val, int d, int s, const struct afswtch *afp)
105 {
106         struct carpreq carpr;
107
108         memset((char *)&carpr, 0, sizeof(struct carpreq));
109         ifr.ifr_data = (caddr_t)&carpr;
110
111         if (ioctl(s, SIOCGVH, (caddr_t)&ifr) == -1)
112                 err(1, "SIOCGVH");
113
114         /* XXX Should hash the password into the key here, perhaps? */
115         strlcpy(carpr.carpr_key, val, CARP_KEY_LEN);
116
117         if (ioctl(s, SIOCSVH, (caddr_t)&ifr) == -1)
118                 err(1, "SIOCSVH");
119 }
120
121 void
122 setcarp_vhid(const char *val, int d, int s, const struct afswtch *afp)
123 {
124         int vhid;
125         struct carpreq carpr;
126
127         vhid = atoi(val);
128
129         if (vhid <= 0)
130                 errx(1, "vhid must be greater than 0");
131
132         memset((char *)&carpr, 0, sizeof(struct carpreq));
133         ifr.ifr_data = (caddr_t)&carpr;
134
135         if (ioctl(s, SIOCGVH, (caddr_t)&ifr) == -1)
136                 err(1, "SIOCGVH");
137
138         carpr.carpr_vhid = vhid;
139
140         if (ioctl(s, SIOCSVH, (caddr_t)&ifr) == -1)
141                 err(1, "SIOCSVH");
142 }
143
144 void
145 setcarp_advskew(const char *val, int d, int s, const struct afswtch *afp)
146 {
147         int advskew;
148         struct carpreq carpr;
149
150         advskew = atoi(val);
151
152         memset((char *)&carpr, 0, sizeof(struct carpreq));
153         ifr.ifr_data = (caddr_t)&carpr;
154
155         if (ioctl(s, SIOCGVH, (caddr_t)&ifr) == -1)
156                 err(1, "SIOCGVH");
157
158         carpr.carpr_advskew = advskew;
159
160         if (ioctl(s, SIOCSVH, (caddr_t)&ifr) == -1)
161                 err(1, "SIOCSVH");
162 }
163
164 void
165 setcarp_advbase(const char *val, int d, int s, const struct afswtch *afp)
166 {
167         int advbase;
168         struct carpreq carpr;
169
170         advbase = atoi(val);
171
172         memset((char *)&carpr, 0, sizeof(struct carpreq));
173         ifr.ifr_data = (caddr_t)&carpr;
174
175         if (ioctl(s, SIOCGVH, (caddr_t)&ifr) == -1)
176                 err(1, "SIOCGVH");
177
178         carpr.carpr_advbase = advbase;
179
180         if (ioctl(s, SIOCSVH, (caddr_t)&ifr) == -1)
181                 err(1, "SIOCSVH");
182 }
183
184 static void
185 getcarp_vhaddr(const char *val, int d, int s, const struct afswtch *afp)
186 {
187 #define VHADDR_PFMT     "%-15s %-15s %s\n"
188
189         struct ifdrv ifd;
190         struct ifcarpvhaddr *carpa;
191         char devname[IFNAMSIZ];
192         int count, i;
193
194         memset(&ifd, 0, sizeof(ifd));
195         strncpy(ifd.ifd_name, ifr.ifr_name, sizeof(ifd.ifd_name));
196         ifd.ifd_cmd = CARPGVHADDR;
197         if (ioctl(s, SIOCGDRVSPEC, &ifd) < 0)
198                 return;
199         if (ifd.ifd_len != 0) {
200                 carpa = malloc(ifd.ifd_len);
201                 if (carpa == NULL)
202                         return;
203
204                 ifd.ifd_cmd = CARPGVHADDR;
205                 ifd.ifd_data = carpa;
206                 if (ioctl(s, SIOCGDRVSPEC, &ifd) < 0) {
207                         free(carpa);
208                         return;
209                 }
210         } else {
211                 carpa = NULL;
212         }
213         count = ifd.ifd_len / sizeof(*carpa);
214         if (count != 0)
215                 printf(VHADDR_PFMT, "virtual addr", "backing addr", "flags");
216         for (i = 0; i < count; ++i) {
217                 char flags[16];
218                 char baddr[INET_ADDRSTRLEN];
219                 int a = 0;
220
221                 memset(flags, 0, sizeof(flags));
222                 flags[a] = '*';
223                 if (carpa[i].carpa_flags & CARP_VHAF_OWNER)
224                         flags[a++] = 'O';
225
226                 memset(baddr, 0, sizeof(baddr));
227                 baddr[0] = '*';
228                 if (carpa[i].carpa_baddr.sin_addr.s_addr != INADDR_ANY) {
229                         inet_ntop(AF_INET, &carpa[i].carpa_baddr.sin_addr,
230                                   baddr, sizeof(baddr));
231                 }
232
233                 printf(VHADDR_PFMT, inet_ntoa(carpa[i].carpa_addr.sin_addr),
234                        baddr, flags);
235         }
236         if (carpa != NULL)
237                 free(carpa);
238
239 #undef VHADDR_PFMT
240 }
241
242 static struct cmd carp_cmds[] = {
243         DEF_CMD_ARG("advbase",  setcarp_advbase),
244         DEF_CMD_ARG("advskew",  setcarp_advskew),
245         DEF_CMD_ARG("pass",     setcarp_passwd),
246         DEF_CMD_ARG("vhid",     setcarp_vhid),
247         DEF_CMD("vhaddr", 1,    getcarp_vhaddr)
248 };
249 static struct afswtch af_carp = {
250         .af_name        = "af_carp",
251         .af_af          = AF_UNSPEC,
252         .af_other_status = carp_status,
253 };
254
255 static __constructor void
256 carp_ctor(void)
257 {
258 #define N(a)    (sizeof(a) / sizeof(a[0]))
259         int i;
260
261         for (i = 0; i < N(carp_cmds);  i++)
262                 cmd_register(&carp_cmds[i]);
263         af_register(&af_carp);
264 #undef N
265 }