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 | #ifdef __rtems__ |
---|
41 | #include <freebsd/netinet/ip_carp.h> |
---|
42 | #else |
---|
43 | #include <netinet/ip_carp.h> |
---|
44 | #endif |
---|
45 | #include <net/route.h> |
---|
46 | |
---|
47 | #include <ctype.h> |
---|
48 | #include <stdio.h> |
---|
49 | #include <string.h> |
---|
50 | #include <stdlib.h> |
---|
51 | #include <unistd.h> |
---|
52 | #include <err.h> |
---|
53 | #include <errno.h> |
---|
54 | |
---|
55 | #include "ifconfig.h" |
---|
56 | |
---|
57 | static const char *carp_states[] = { CARP_STATES }; |
---|
58 | |
---|
59 | void carp_status(int s); |
---|
60 | void setcarp_advbase(const char *,int, int, const struct afswtch *rafp); |
---|
61 | void setcarp_advskew(const char *, int, int, const struct afswtch *rafp); |
---|
62 | void setcarp_passwd(const char *, int, int, const struct afswtch *rafp); |
---|
63 | void setcarp_vhid(const char *, int, int, const struct afswtch *rafp); |
---|
64 | |
---|
65 | void |
---|
66 | carp_status(int s) |
---|
67 | { |
---|
68 | const char *state; |
---|
69 | struct carpreq carpr; |
---|
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 | return; |
---|
89 | |
---|
90 | } |
---|
91 | |
---|
92 | void |
---|
93 | setcarp_passwd(const char *val, int d, int s, const struct afswtch *afp) |
---|
94 | { |
---|
95 | struct carpreq carpr; |
---|
96 | |
---|
97 | memset((char *)&carpr, 0, sizeof(struct carpreq)); |
---|
98 | ifr.ifr_data = (caddr_t)&carpr; |
---|
99 | |
---|
100 | if (ioctl(s, SIOCGVH, (caddr_t)&ifr) == -1) |
---|
101 | err(1, "SIOCGVH"); |
---|
102 | |
---|
103 | memset(carpr.carpr_key, 0, sizeof(carpr.carpr_key)); |
---|
104 | /* XXX Should hash the password into the key here, perhaps? */ |
---|
105 | strlcpy(carpr.carpr_key, val, CARP_KEY_LEN); |
---|
106 | |
---|
107 | if (ioctl(s, SIOCSVH, (caddr_t)&ifr) == -1) |
---|
108 | err(1, "SIOCSVH"); |
---|
109 | |
---|
110 | return; |
---|
111 | } |
---|
112 | |
---|
113 | void |
---|
114 | setcarp_vhid(const char *val, int d, int s, const struct afswtch *afp) |
---|
115 | { |
---|
116 | int vhid; |
---|
117 | struct carpreq carpr; |
---|
118 | |
---|
119 | vhid = atoi(val); |
---|
120 | |
---|
121 | if (vhid <= 0) |
---|
122 | errx(1, "vhid must be greater than 0"); |
---|
123 | |
---|
124 | memset((char *)&carpr, 0, sizeof(struct carpreq)); |
---|
125 | ifr.ifr_data = (caddr_t)&carpr; |
---|
126 | |
---|
127 | if (ioctl(s, SIOCGVH, (caddr_t)&ifr) == -1) |
---|
128 | err(1, "SIOCGVH"); |
---|
129 | |
---|
130 | carpr.carpr_vhid = vhid; |
---|
131 | |
---|
132 | if (ioctl(s, SIOCSVH, (caddr_t)&ifr) == -1) |
---|
133 | err(1, "SIOCSVH"); |
---|
134 | |
---|
135 | return; |
---|
136 | } |
---|
137 | |
---|
138 | void |
---|
139 | setcarp_advskew(const char *val, int d, int s, const struct afswtch *afp) |
---|
140 | { |
---|
141 | int advskew; |
---|
142 | struct carpreq carpr; |
---|
143 | |
---|
144 | advskew = atoi(val); |
---|
145 | |
---|
146 | memset((char *)&carpr, 0, sizeof(struct carpreq)); |
---|
147 | ifr.ifr_data = (caddr_t)&carpr; |
---|
148 | |
---|
149 | if (ioctl(s, SIOCGVH, (caddr_t)&ifr) == -1) |
---|
150 | err(1, "SIOCGVH"); |
---|
151 | |
---|
152 | carpr.carpr_advskew = advskew; |
---|
153 | |
---|
154 | if (ioctl(s, SIOCSVH, (caddr_t)&ifr) == -1) |
---|
155 | err(1, "SIOCSVH"); |
---|
156 | |
---|
157 | return; |
---|
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 | return; |
---|
180 | } |
---|
181 | |
---|
182 | static struct cmd carp_cmds[] = { |
---|
183 | DEF_CMD_ARG("advbase", setcarp_advbase), |
---|
184 | DEF_CMD_ARG("advskew", setcarp_advskew), |
---|
185 | DEF_CMD_ARG("pass", setcarp_passwd), |
---|
186 | DEF_CMD_ARG("vhid", setcarp_vhid), |
---|
187 | }; |
---|
188 | static struct afswtch af_carp = { |
---|
189 | .af_name = "af_carp", |
---|
190 | .af_af = AF_UNSPEC, |
---|
191 | .af_other_status = carp_status, |
---|
192 | }; |
---|
193 | |
---|
194 | static __constructor void |
---|
195 | carp_ctor(void) |
---|
196 | { |
---|
197 | #define N(a) (sizeof(a) / sizeof(a[0])) |
---|
198 | int i; |
---|
199 | |
---|
200 | for (i = 0; i < N(carp_cmds); i++) |
---|
201 | cmd_register(&carp_cmds[i]); |
---|
202 | af_register(&af_carp); |
---|
203 | #undef N |
---|
204 | } |
---|