Deleted Added
full compact
carp.c (203486) carp.c (228571)
1/* $FreeBSD: head/sbin/ifconfig/ifcarp.c 203486 2010-02-04 11:43:22Z ru $ */
1/* $FreeBSD: head/sbin/ifconfig/ifcarp.c 228571 2011-12-16 12:16:56Z glebius $ */
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

--- 20 unchanged lines hidden (view full) ---

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
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

--- 20 unchanged lines hidden (view full) ---

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>
38#include <net/if.h>
39#include <net/if_var.h>
40#include <netinet/in.h>
41#include <netinet/in_var.h>
40#include <netinet/ip_carp.h>
42#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
53static const char *carp_states[] = { CARP_STATES };
54
43
44#include <ctype.h>
45#include <stdio.h>
46#include <string.h>
47#include <stdlib.h>
48#include <unistd.h>
49#include <err.h>
50#include <errno.h>
51
52#include "ifconfig.h"
53
54static const char *carp_states[] = { CARP_STATES };
55
55void carp_status(int s);
56void setcarp_advbase(const char *,int, int, const struct afswtch *rafp);
57void setcarp_advskew(const char *, int, int, const struct afswtch *rafp);
58void setcarp_passwd(const char *, int, int, const struct afswtch *rafp);
59void setcarp_vhid(const char *, int, int, const struct afswtch *rafp);
56static void carp_status(int s);
57static void setcarp_vhid(const char *, int, int, const struct afswtch *rafp);
58static void setcarp_callback(int, void *);
59static void setcarp_advbase(const char *,int, int, const struct afswtch *rafp);
60static void setcarp_advskew(const char *, int, int, const struct afswtch *rafp);
61static void setcarp_passwd(const char *, int, int, const struct afswtch *rafp);
60
62
61void
63static int carpr_vhid = -1;
64static int carpr_advskew = -1;
65static int carpr_advbase = -1;
66static int carpr_state = -1;
67static unsigned char const *carpr_key;
68
69static void
62carp_status(int s)
63{
70carp_status(int s)
71{
64 const char *state;
65 struct carpreq carpr;
72 struct carpreq carpr[CARP_MAXVHID];
73 int i;
66
74
67 memset((char *)&carpr, 0, sizeof(struct carpreq));
75 bzero(carpr, sizeof(struct carpreq) * CARP_MAXVHID);
76 carpr[0].carpr_count = CARP_MAXVHID;
68 ifr.ifr_data = (caddr_t)&carpr;
69
70 if (ioctl(s, SIOCGVH, (caddr_t)&ifr) == -1)
71 return;
72
77 ifr.ifr_data = (caddr_t)&carpr;
78
79 if (ioctl(s, SIOCGVH, (caddr_t)&ifr) == -1)
80 return;
81
73 if (carpr.carpr_vhid > 0) {
74 if (carpr.carpr_state > CARP_MAXSTATE)
75 state = "<UNKNOWN>";
82 for (i = 0; i < carpr[0].carpr_count; i++) {
83 printf("\tcarp: %s vhid %d advbase %d advskew %d",
84 carp_states[carpr[i].carpr_state], carpr[i].carpr_vhid,
85 carpr[i].carpr_advbase, carpr[i].carpr_advskew);
86 if (printkeys && carpr[i].carpr_key[0] != '\0')
87 printf(" key \"%s\"\n", carpr[i].carpr_key);
76 else
88 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);
89 printf("\n");
82 }
90 }
83
84 return;
85
86}
87
91}
92
88void
89setcarp_passwd(const char *val, int d, int s, const struct afswtch *afp)
93static void
94setcarp_vhid(const char *val, int d, int s, const struct afswtch *afp)
90{
95{
91 struct carpreq carpr;
92
96
93 memset((char *)&carpr, 0, sizeof(struct carpreq));
94 ifr.ifr_data = (caddr_t)&carpr;
97 carpr_vhid = atoi(val);
95
98
96 if (ioctl(s, SIOCGVH, (caddr_t)&ifr) == -1)
97 err(1, "SIOCGVH");
99 if (carpr_vhid <= 0 || carpr_vhid > CARP_MAXVHID)
100 errx(1, "vhid must be greater than 0 and less than %u",
101 CARP_MAXVHID);
98
102
99 memset(carpr.carpr_key, 0, sizeof(carpr.carpr_key));
100 /* XXX Should hash the password into the key here, perhaps? */
101 strlcpy(carpr.carpr_key, val, CARP_KEY_LEN);
103 switch (afp->af_af) {
104#ifdef INET
105 case AF_INET:
106 {
107 struct in_aliasreq *ifra;
102
108
103 if (ioctl(s, SIOCSVH, (caddr_t)&ifr) == -1)
104 err(1, "SIOCSVH");
109 ifra = (struct in_aliasreq *)afp->af_addreq;
110 ifra->ifra_vhid = carpr_vhid;
111 break;
112 }
113#endif
114#ifdef INET6
115 case AF_INET6:
116 {
117 struct in6_aliasreq *ifra;
105
118
106 return;
119 ifra = (struct in6_aliasreq *)afp->af_addreq;
120 ifra->ifra_vhid = carpr_vhid;
121 break;
122 }
123#endif
124 default:
125 errx(1, "%s doesn't support carp(4)", afp->af_name);
126 }
127
128 callback_register(setcarp_callback, NULL);
107}
108
129}
130
109void
110setcarp_vhid(const char *val, int d, int s, const struct afswtch *afp)
131static void
132setcarp_callback(int s, void *arg __unused)
111{
133{
112 int vhid;
113 struct carpreq carpr;
114
134 struct carpreq carpr;
135
115 vhid = atoi(val);
116
117 if (vhid <= 0)
118 errx(1, "vhid must be greater than 0");
119
120 memset((char *)&carpr, 0, sizeof(struct carpreq));
136 bzero(&carpr, sizeof(struct carpreq));
137 carpr.carpr_vhid = carpr_vhid;
138 carpr.carpr_count = 1;
121 ifr.ifr_data = (caddr_t)&carpr;
122
139 ifr.ifr_data = (caddr_t)&carpr;
140
123 if (ioctl(s, SIOCGVH, (caddr_t)&ifr) == -1)
141 if (ioctl(s, SIOCGVH, (caddr_t)&ifr) == -1 && errno != ENOENT)
124 err(1, "SIOCGVH");
125
142 err(1, "SIOCGVH");
143
126 carpr.carpr_vhid = vhid;
144 if (carpr_key != NULL)
145 /* XXX Should hash the password into the key here? */
146 strlcpy(carpr.carpr_key, carpr_key, CARP_KEY_LEN);
147 if (carpr_advskew > -1)
148 carpr.carpr_advskew = carpr_advskew;
149 if (carpr_advbase > -1)
150 carpr.carpr_advbase = carpr_advbase;
151 if (carpr_state > -1)
152 carpr.carpr_state = carpr_state;
127
128 if (ioctl(s, SIOCSVH, (caddr_t)&ifr) == -1)
129 err(1, "SIOCSVH");
153
154 if (ioctl(s, SIOCSVH, (caddr_t)&ifr) == -1)
155 err(1, "SIOCSVH");
130
131 return;
132}
133
156}
157
134void
135setcarp_advskew(const char *val, int d, int s, const struct afswtch *afp)
158static void
159setcarp_passwd(const char *val, int d, int s, const struct afswtch *afp)
136{
160{
137 int advskew;
138 struct carpreq carpr;
139
161
140 advskew = atoi(val);
162 if (carpr_vhid == -1)
163 errx(1, "passwd requires vhid");
141
164
142 memset((char *)&carpr, 0, sizeof(struct carpreq));
143 ifr.ifr_data = (caddr_t)&carpr;
165 carpr_key = val;
166}
144
167
145 if (ioctl(s, SIOCGVH, (caddr_t)&ifr) == -1)
146 err(1, "SIOCGVH");
168static void
169setcarp_advskew(const char *val, int d, int s, const struct afswtch *afp)
170{
147
171
148 carpr.carpr_advskew = advskew;
172 if (carpr_vhid == -1)
173 errx(1, "advskew requires vhid");
149
174
150 if (ioctl(s, SIOCSVH, (caddr_t)&ifr) == -1)
151 err(1, "SIOCSVH");
152
153 return;
175 carpr_advskew = atoi(val);
154}
155
176}
177
156void
178static void
157setcarp_advbase(const char *val, int d, int s, const struct afswtch *afp)
158{
179setcarp_advbase(const char *val, int d, int s, const struct afswtch *afp)
180{
159 int advbase;
160 struct carpreq carpr;
161
181
162 advbase = atoi(val);
182 if (carpr_vhid == -1)
183 errx(1, "advbase requires vhid");
163
184
164 memset((char *)&carpr, 0, sizeof(struct carpreq));
165 ifr.ifr_data = (caddr_t)&carpr;
185 carpr_advbase = atoi(val);
186}
166
187
167 if (ioctl(s, SIOCGVH, (caddr_t)&ifr) == -1)
168 err(1, "SIOCGVH");
188static void
189setcarp_state(const char *val, int d, int s, const struct afswtch *afp)
190{
191 int i;
169
192
170 carpr.carpr_advbase = advbase;
193 if (carpr_vhid == -1)
194 errx(1, "state requires vhid");
171
195
172 if (ioctl(s, SIOCSVH, (caddr_t)&ifr) == -1)
173 err(1, "SIOCSVH");
196 for (i = 0; i <= CARP_MAXSTATE; i++)
197 if (strcasecmp(carp_states[i], val) == 0) {
198 carpr_state = i;
199 return;
200 }
174
201
175 return;
202 errx(1, "unknown state");
176}
177
178static struct cmd carp_cmds[] = {
179 DEF_CMD_ARG("advbase", setcarp_advbase),
180 DEF_CMD_ARG("advskew", setcarp_advskew),
181 DEF_CMD_ARG("pass", setcarp_passwd),
182 DEF_CMD_ARG("vhid", setcarp_vhid),
203}
204
205static struct cmd carp_cmds[] = {
206 DEF_CMD_ARG("advbase", setcarp_advbase),
207 DEF_CMD_ARG("advskew", setcarp_advskew),
208 DEF_CMD_ARG("pass", setcarp_passwd),
209 DEF_CMD_ARG("vhid", setcarp_vhid),
210 DEF_CMD_ARG("state", setcarp_state),
183};
184static struct afswtch af_carp = {
185 .af_name = "af_carp",
186 .af_af = AF_UNSPEC,
187 .af_other_status = carp_status,
188};
189
190static __constructor void
191carp_ctor(void)
192{
193#define N(a) (sizeof(a) / sizeof(a[0]))
194 int i;
195
196 for (i = 0; i < N(carp_cmds); i++)
197 cmd_register(&carp_cmds[i]);
198 af_register(&af_carp);
199#undef N
200}
211};
212static struct afswtch af_carp = {
213 .af_name = "af_carp",
214 .af_af = AF_UNSPEC,
215 .af_other_status = carp_status,
216};
217
218static __constructor void
219carp_ctor(void)
220{
221#define N(a) (sizeof(a) / sizeof(a[0]))
222 int i;
223
224 for (i = 0; i < N(carp_cmds); i++)
225 cmd_register(&carp_cmds[i]);
226 af_register(&af_carp);
227#undef N
228}