1 |
|
|
/* $OpenBSD: carp.c,v 1.9 2017/01/24 04:22:42 benno Exp $ */ |
2 |
|
|
|
3 |
|
|
/* |
4 |
|
|
* Copyright (c) 2006 Henning Brauer <henning@openbsd.org> |
5 |
|
|
* |
6 |
|
|
* Permission to use, copy, modify, and distribute this software for any |
7 |
|
|
* purpose with or without fee is hereby granted, provided that the above |
8 |
|
|
* copyright notice and this permission notice appear in all copies. |
9 |
|
|
* |
10 |
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
11 |
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
12 |
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
13 |
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
14 |
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
15 |
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
16 |
|
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
17 |
|
|
*/ |
18 |
|
|
|
19 |
|
|
#include <sys/types.h> |
20 |
|
|
#include <sys/socket.h> |
21 |
|
|
#include <sys/ioctl.h> |
22 |
|
|
#include <net/if.h> |
23 |
|
|
|
24 |
|
|
#include <errno.h> |
25 |
|
|
#include <string.h> |
26 |
|
|
#include <stdlib.h> |
27 |
|
|
#include <unistd.h> |
28 |
|
|
|
29 |
|
|
#include "bgpd.h" |
30 |
|
|
#include "session.h" |
31 |
|
|
#include "log.h" |
32 |
|
|
|
33 |
|
|
struct carpgroup { |
34 |
|
|
TAILQ_ENTRY(carpgroup) entry; |
35 |
|
|
char *group; |
36 |
|
|
int do_demote; |
37 |
|
|
int changed_by; |
38 |
|
|
}; |
39 |
|
|
|
40 |
|
|
TAILQ_HEAD(carpgroups, carpgroup) carpgroups = |
41 |
|
|
TAILQ_HEAD_INITIALIZER(carpgroups); |
42 |
|
|
|
43 |
|
|
struct carpgroup *carp_group_find(char *group); |
44 |
|
|
int carp_demote_ioctl(char *, int); |
45 |
|
|
|
46 |
|
|
struct carpgroup * |
47 |
|
|
carp_group_find(char *group) |
48 |
|
|
{ |
49 |
|
|
struct carpgroup *c; |
50 |
|
|
|
51 |
|
|
TAILQ_FOREACH(c, &carpgroups, entry) |
52 |
|
|
if (!strcmp(c->group, group)) |
53 |
|
|
return (c); |
54 |
|
|
|
55 |
|
|
return (NULL); |
56 |
|
|
} |
57 |
|
|
|
58 |
|
|
int |
59 |
|
|
carp_demote_init(char *group, int force) |
60 |
|
|
{ |
61 |
|
|
struct carpgroup *c; |
62 |
|
|
int level; |
63 |
|
|
|
64 |
|
|
if ((c = carp_group_find(group)) == NULL) { |
65 |
|
|
if ((c = calloc(1, sizeof(struct carpgroup))) == NULL) { |
66 |
|
|
log_warn("carp_demote_init calloc"); |
67 |
|
|
return (-1); |
68 |
|
|
} |
69 |
|
|
if ((c->group = strdup(group)) == NULL) { |
70 |
|
|
log_warn("carp_demote_init strdup"); |
71 |
|
|
free(c); |
72 |
|
|
return (-1); |
73 |
|
|
} |
74 |
|
|
|
75 |
|
|
/* only demote if this group already is demoted */ |
76 |
|
|
if ((level = carp_demote_get(group)) == -1) { |
77 |
|
|
free(c->group); |
78 |
|
|
free(c); |
79 |
|
|
return (-1); |
80 |
|
|
} |
81 |
|
|
if (level > 0 || force) |
82 |
|
|
c->do_demote = 1; |
83 |
|
|
|
84 |
|
|
TAILQ_INSERT_TAIL(&carpgroups, c, entry); |
85 |
|
|
} |
86 |
|
|
|
87 |
|
|
return (0); |
88 |
|
|
} |
89 |
|
|
|
90 |
|
|
void |
91 |
|
|
carp_demote_shutdown(void) |
92 |
|
|
{ |
93 |
|
|
struct carpgroup *c; |
94 |
|
|
|
95 |
|
|
while ((c = TAILQ_FIRST(&carpgroups)) != NULL) { |
96 |
|
|
TAILQ_REMOVE(&carpgroups, c, entry); |
97 |
|
|
if (c->do_demote && c->changed_by > 0) |
98 |
|
|
carp_demote_ioctl(c->group, -c->changed_by); |
99 |
|
|
|
100 |
|
|
free(c->group); |
101 |
|
|
free(c); |
102 |
|
|
} |
103 |
|
|
} |
104 |
|
|
|
105 |
|
|
int |
106 |
|
|
carp_demote_get(char *group) |
107 |
|
|
{ |
108 |
|
|
int s; |
109 |
|
|
struct ifgroupreq ifgr; |
110 |
|
|
|
111 |
|
|
if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == -1) { |
112 |
|
|
log_warn("carp_demote_get: socket"); |
113 |
|
|
return (-1); |
114 |
|
|
} |
115 |
|
|
|
116 |
|
|
bzero(&ifgr, sizeof(ifgr)); |
117 |
|
|
strlcpy(ifgr.ifgr_name, group, sizeof(ifgr.ifgr_name)); |
118 |
|
|
|
119 |
|
|
if (ioctl(s, SIOCGIFGATTR, (caddr_t)&ifgr) == -1) { |
120 |
|
|
if (errno == ENOENT) |
121 |
|
|
log_warnx("group \"%s\" does not exist", group); |
122 |
|
|
else |
123 |
|
|
log_warn("carp_demote_get: ioctl"); |
124 |
|
|
close(s); |
125 |
|
|
return (-1); |
126 |
|
|
} |
127 |
|
|
|
128 |
|
|
close(s); |
129 |
|
|
return ((int)ifgr.ifgr_attrib.ifg_carp_demoted); |
130 |
|
|
} |
131 |
|
|
|
132 |
|
|
int |
133 |
|
|
carp_demote_set(char *group, int demote) |
134 |
|
|
{ |
135 |
|
|
struct carpgroup *c; |
136 |
|
|
|
137 |
|
|
if ((c = carp_group_find(group)) == NULL) { |
138 |
|
|
log_warnx("carp_group_find for %s returned NULL?!", group); |
139 |
|
|
return (-1); |
140 |
|
|
} |
141 |
|
|
|
142 |
|
|
if (c->changed_by + demote < 0) { |
143 |
|
|
log_warnx("carp_demote_set: changed_by + demote < 0"); |
144 |
|
|
return (-1); |
145 |
|
|
} |
146 |
|
|
|
147 |
|
|
if (c->do_demote && carp_demote_ioctl(group, demote) == -1) |
148 |
|
|
return (-1); |
149 |
|
|
|
150 |
|
|
c->changed_by += demote; |
151 |
|
|
|
152 |
|
|
/* enable demotion when we return to 0, i. e. all sessions up */ |
153 |
|
|
if (demote < 0 && c->changed_by == 0) |
154 |
|
|
c->do_demote = 1; |
155 |
|
|
|
156 |
|
|
return (0); |
157 |
|
|
} |
158 |
|
|
|
159 |
|
|
int |
160 |
|
|
carp_demote_ioctl(char *group, int demote) |
161 |
|
|
{ |
162 |
|
|
int s, res; |
163 |
|
|
struct ifgroupreq ifgr; |
164 |
|
|
|
165 |
|
|
if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == -1) { |
166 |
|
|
log_warn("%s: socket", __func__); |
167 |
|
|
return (-1); |
168 |
|
|
} |
169 |
|
|
|
170 |
|
|
bzero(&ifgr, sizeof(ifgr)); |
171 |
|
|
strlcpy(ifgr.ifgr_name, group, sizeof(ifgr.ifgr_name)); |
172 |
|
|
ifgr.ifgr_attrib.ifg_carp_demoted = demote; |
173 |
|
|
|
174 |
|
|
if ((res = ioctl(s, SIOCSIFGATTR, (caddr_t)&ifgr)) == -1) |
175 |
|
|
log_warn("unable to %s the demote state " |
176 |
|
|
"of group '%s'", (demote > 0) ? "increment" : "decrement", |
177 |
|
|
group); |
178 |
|
|
else |
179 |
|
|
log_info("%s the demote state of group '%s'", |
180 |
|
|
(demote > 0) ? "incremented" : "decremented", group); |
181 |
|
|
|
182 |
|
|
close(s); |
183 |
|
|
return (res); |
184 |
|
|
} |