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