2 * Copyright (c) 2002 Michael Shalayeff. All rights reserved.
3 * Copyright (c) 2003 Ryan McBride. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
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 $
31 #include <sys/param.h>
32 #include <sys/ioctl.h>
33 #include <sys/socket.h>
34 #include <sys/sockio.h>
39 #include <net/ethernet.h>
41 #include <netinet/in.h>
42 #include <netinet/ip_carp.h>
43 #include <net/route.h>
45 #include <arpa/inet.h>
55 static const char *carp_states
[] = { CARP_STATES
};
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
);
69 char devname
[IFNAMSIZ
];
71 memset((char *)&carpr
, 0, sizeof(struct carpreq
));
72 ifr
.ifr_data
= (caddr_t
)&carpr
;
74 if (ioctl(s
, SIOCGVH
, (caddr_t
)&ifr
) == -1)
77 if (carpr
.carpr_vhid
> 0) {
78 if (carpr
.carpr_state
> CARP_MAXSTATE
)
81 state
= carp_states
[carpr
.carpr_state
];
83 printf("\tcarp: %s vhid %d advbase %d advskew %d\n",
84 state
, carpr
.carpr_vhid
, carpr
.carpr_advbase
,
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
);
100 setcarp_passwd(const char *val
, int d
, int s
, const struct afswtch
*afp
)
102 struct carpreq carpr
;
104 memset((char *)&carpr
, 0, sizeof(struct carpreq
));
105 ifr
.ifr_data
= (caddr_t
)&carpr
;
107 if (ioctl(s
, SIOCGVH
, (caddr_t
)&ifr
) == -1)
110 /* XXX Should hash the password into the key here, perhaps? */
111 strlcpy(carpr
.carpr_key
, val
, CARP_KEY_LEN
);
113 if (ioctl(s
, SIOCSVH
, (caddr_t
)&ifr
) == -1)
118 setcarp_vhid(const char *val
, int d
, int s
, const struct afswtch
*afp
)
121 struct carpreq carpr
;
126 errx(1, "vhid must be greater than 0");
128 memset((char *)&carpr
, 0, sizeof(struct carpreq
));
129 ifr
.ifr_data
= (caddr_t
)&carpr
;
131 if (ioctl(s
, SIOCGVH
, (caddr_t
)&ifr
) == -1)
134 carpr
.carpr_vhid
= vhid
;
136 if (ioctl(s
, SIOCSVH
, (caddr_t
)&ifr
) == -1)
141 setcarp_advskew(const char *val
, int d
, int s
, const struct afswtch
*afp
)
144 struct carpreq carpr
;
148 memset((char *)&carpr
, 0, sizeof(struct carpreq
));
149 ifr
.ifr_data
= (caddr_t
)&carpr
;
151 if (ioctl(s
, SIOCGVH
, (caddr_t
)&ifr
) == -1)
154 carpr
.carpr_advskew
= advskew
;
156 if (ioctl(s
, SIOCSVH
, (caddr_t
)&ifr
) == -1)
161 setcarp_advbase(const char *val
, int d
, int s
, const struct afswtch
*afp
)
164 struct carpreq carpr
;
168 memset((char *)&carpr
, 0, sizeof(struct carpreq
));
169 ifr
.ifr_data
= (caddr_t
)&carpr
;
171 if (ioctl(s
, SIOCGVH
, (caddr_t
)&ifr
) == -1)
174 carpr
.carpr_advbase
= advbase
;
176 if (ioctl(s
, SIOCSVH
, (caddr_t
)&ifr
) == -1)
181 getcarp_vhaddr(const char *val
, int d
, int s
, const struct afswtch
*afp
)
183 #define VHADDR_PFMT "%-15s %-15s %s\n"
186 struct ifcarpvhaddr
*carpa
;
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)
194 if (ifd
.ifd_len
!= 0) {
195 carpa
= malloc(ifd
.ifd_len
);
199 ifd
.ifd_cmd
= CARPGVHADDR
;
200 ifd
.ifd_data
= carpa
;
201 if (ioctl(s
, SIOCGDRVSPEC
, &ifd
) < 0) {
208 count
= ifd
.ifd_len
/ sizeof(*carpa
);
210 printf(VHADDR_PFMT
, "virtual addr", "backing addr", "flags");
211 for (i
= 0; i
< count
; ++i
) {
213 char baddr
[INET_ADDRSTRLEN
];
216 memset(flags
, 0, sizeof(flags
));
218 if (carpa
[i
].carpa_flags
& CARP_VHAF_OWNER
)
221 memset(baddr
, 0, sizeof(baddr
));
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
));
228 printf(VHADDR_PFMT
, inet_ntoa(carpa
[i
].carpa_addr
.sin_addr
),
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
)
244 static struct afswtch af_carp
= {
245 .af_name
= "af_carp",
247 .af_other_status
= carp_status
,
250 static __constructor(101) void
253 #define N(a) (sizeof(a) / sizeof(a[0]))
256 for (i
= 0; i
< N(carp_cmds
); i
++)
257 cmd_register(&carp_cmds
[i
]);
258 af_register(&af_carp
);