]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - sbin/ifconfig/ifcarp.c
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.git] / sbin / ifconfig / ifcarp.c
1 /*      $FreeBSD$ */
2 /*      from $OpenBSD: ifconfig.c,v 1.82 2003/10/19 05:43:35 mcbride Exp $ */
3
4 /*
5  * Copyright (c) 2002 Michael Shalayeff. All rights reserved.
6  * Copyright (c) 2003 Ryan McBride. All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR OR HIS RELATIVES BE LIABLE FOR ANY DIRECT,
21  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23  * SERVICES; LOSS OF MIND, USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
25  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
26  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
27  * THE POSSIBILITY OF SUCH DAMAGE.
28  */
29
30 #include <sys/param.h>
31 #include <sys/ioctl.h>
32 #include <sys/socket.h>
33 #include <sys/sockio.h>
34
35 #include <stdlib.h>
36 #include <unistd.h>
37
38 #include <net/ethernet.h>
39 #include <net/if.h>
40 #include <netinet/ip_carp.h>
41 #include <net/route.h>
42
43 #include <ctype.h>
44 #include <stdio.h>
45 #include <string.h>
46 #include <stdlib.h>
47 #include <unistd.h>
48 #include <err.h>
49 #include <errno.h>
50
51 #include "ifconfig.h"
52
53 static const char *carp_states[] = { CARP_STATES };
54
55 void carp_status(int s);
56 void setcarp_advbase(const char *,int, int, const struct afswtch *rafp);
57 void setcarp_advskew(const char *, int, int, const struct afswtch *rafp);
58 void setcarp_passwd(const char *, int, int, const struct afswtch *rafp);
59 void setcarp_vhid(const char *, int, int, const struct afswtch *rafp);
60
61 void
62 carp_status(int s)
63 {
64         const char *state;
65         struct carpreq carpr;
66
67         memset((char *)&carpr, 0, sizeof(struct carpreq));
68         ifr.ifr_data = (caddr_t)&carpr;
69
70         if (ioctl(s, SIOCGVH, (caddr_t)&ifr) == -1)
71                 return;
72
73         if (carpr.carpr_vhid > 0) {
74                 if (carpr.carpr_state > CARP_MAXSTATE)
75                         state = "<UNKNOWN>";
76                 else
77                         state = carp_states[carpr.carpr_state];
78
79                 printf("\tcarp: %s vhid %d advbase %d advskew %d\n",
80                     state, carpr.carpr_vhid, carpr.carpr_advbase,
81                     carpr.carpr_advskew);
82         }
83
84         return;
85
86 }
87
88 void
89 setcarp_passwd(const char *val, int d, int s, const struct afswtch *afp)
90 {
91         struct carpreq carpr;
92
93         memset((char *)&carpr, 0, sizeof(struct carpreq));
94         ifr.ifr_data = (caddr_t)&carpr;
95
96         if (ioctl(s, SIOCGVH, (caddr_t)&ifr) == -1)
97                 err(1, "SIOCGVH");
98
99         /* XXX Should hash the password into the key here, perhaps? */
100         strlcpy(carpr.carpr_key, val, CARP_KEY_LEN);
101
102         if (ioctl(s, SIOCSVH, (caddr_t)&ifr) == -1)
103                 err(1, "SIOCSVH");
104
105         return;
106 }
107
108 void
109 setcarp_vhid(const char *val, int d, int s, const struct afswtch *afp)
110 {
111         int vhid;
112         struct carpreq carpr;
113
114         vhid = atoi(val);
115
116         if (vhid <= 0)
117                 errx(1, "vhid must be greater than 0");
118
119         memset((char *)&carpr, 0, sizeof(struct carpreq));
120         ifr.ifr_data = (caddr_t)&carpr;
121
122         if (ioctl(s, SIOCGVH, (caddr_t)&ifr) == -1)
123                 err(1, "SIOCGVH");
124
125         carpr.carpr_vhid = vhid;
126
127         if (ioctl(s, SIOCSVH, (caddr_t)&ifr) == -1)
128                 err(1, "SIOCSVH");
129
130         return;
131 }
132
133 void
134 setcarp_advskew(const char *val, int d, int s, const struct afswtch *afp)
135 {
136         int advskew;
137         struct carpreq carpr;
138
139         advskew = atoi(val);
140
141         memset((char *)&carpr, 0, sizeof(struct carpreq));
142         ifr.ifr_data = (caddr_t)&carpr;
143
144         if (ioctl(s, SIOCGVH, (caddr_t)&ifr) == -1)
145                 err(1, "SIOCGVH");
146
147         carpr.carpr_advskew = advskew;
148
149         if (ioctl(s, SIOCSVH, (caddr_t)&ifr) == -1)
150                 err(1, "SIOCSVH");
151
152         return;
153 }
154
155 void
156 setcarp_advbase(const char *val, int d, int s, const struct afswtch *afp)
157 {
158         int advbase;
159         struct carpreq carpr;
160
161         advbase = atoi(val);
162
163         memset((char *)&carpr, 0, sizeof(struct carpreq));
164         ifr.ifr_data = (caddr_t)&carpr;
165
166         if (ioctl(s, SIOCGVH, (caddr_t)&ifr) == -1)
167                 err(1, "SIOCGVH");
168
169         carpr.carpr_advbase = advbase;
170
171         if (ioctl(s, SIOCSVH, (caddr_t)&ifr) == -1)
172                 err(1, "SIOCSVH");
173
174         return;
175 }
176
177 static struct cmd carp_cmds[] = {
178         DEF_CMD_ARG("advbase",  setcarp_advbase),
179         DEF_CMD_ARG("advskew",  setcarp_advskew),
180         DEF_CMD_ARG("pass",     setcarp_passwd),
181         DEF_CMD_ARG("vhid",     setcarp_vhid),
182 };
183 static struct afswtch af_carp = {
184         .af_name        = "af_carp",
185         .af_af          = AF_UNSPEC,
186         .af_other_status = carp_status,
187 };
188
189 static __constructor void
190 carp_ctor(void)
191 {
192 #define N(a)    (sizeof(a) / sizeof(a[0]))
193         int i;
194
195         for (i = 0; i < N(carp_cmds);  i++)
196                 cmd_register(&carp_cmds[i]);
197         af_register(&af_carp);
198 #undef N
199 }