xref: /openbsd/usr.sbin/ospfd/carp.c (revision 370872a6)
1 /*	$OpenBSD: carp.c,v 1.3 2009/09/30 12:22:03 claudio 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 "ospfd.h"
30 #include "log.h"
31 
32 struct carpgroup {
33 	TAILQ_ENTRY(carpgroup)	 entry;
34 	char			*group;
35 	int			 do_demote;
36 	int			 changed_by;
37 };
38 
39 TAILQ_HEAD(carpgroups, carpgroup)	carpgroups =
40     TAILQ_HEAD_INITIALIZER(carpgroups);
41 
42 struct carpgroup	*carp_group_find(char *group);
43 int			 carp_demote_ioctl(char *, int);
44 
45 struct carpgroup *
carp_group_find(char * group)46 carp_group_find(char *group)
47 {
48 	struct carpgroup	*c;
49 
50 	TAILQ_FOREACH(c, &carpgroups, entry)
51 		if (!strcmp(c->group, group))
52 			return (c);
53 
54 	return (NULL);
55 }
56 
57 int
carp_demote_init(char * group,int force)58 carp_demote_init(char *group, int force)
59 {
60 	struct carpgroup	*c;
61 	int			 level;
62 
63 	if ((c = carp_group_find(group)) == NULL) {
64 		if ((c = calloc(1, sizeof(struct carpgroup))) == NULL) {
65 			log_warn("carp_demote_init calloc");
66 			return (-1);
67 		}
68 		if ((c->group = strdup(group)) == NULL) {
69 			log_warn("carp_demote_init strdup");
70 			free(c);
71 			return (-1);
72 		}
73 
74 		/* only demote if this group already is demoted */
75 		if ((level = carp_demote_get(group)) == -1) {
76 			free(c->group);
77 			free(c);
78 			return (-1);
79 		}
80 		if (level > 0 || force)
81 			c->do_demote = 1;
82 
83 		TAILQ_INSERT_TAIL(&carpgroups, c, entry);
84 	}
85 
86 	return (0);
87 }
88 
89 void
carp_demote_shutdown(void)90 carp_demote_shutdown(void)
91 {
92 	struct carpgroup	*c;
93 
94 	while ((c = TAILQ_FIRST(&carpgroups)) != NULL) {
95 		TAILQ_REMOVE(&carpgroups, c, entry);
96 		if (c->do_demote && c->changed_by > 0)
97 			carp_demote_ioctl(c->group, -c->changed_by);
98 
99 		free(c->group);
100 		free(c);
101 	}
102 }
103 
104 int
carp_demote_get(char * group)105 carp_demote_get(char *group)
106 {
107 	int			s;
108 	struct ifgroupreq	ifgr;
109 
110 	if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
111 		log_warn("carp_demote_get: socket");
112 		return (-1);
113 	}
114 
115 	bzero(&ifgr, sizeof(ifgr));
116 	strlcpy(ifgr.ifgr_name, group, sizeof(ifgr.ifgr_name));
117 
118 	if (ioctl(s, SIOCGIFGATTR, (caddr_t)&ifgr) == -1) {
119 		if (errno == ENOENT)
120 			log_warnx("group \"%s\" does not exist", group);
121 		else
122 			log_warn("carp_demote_get: ioctl");
123 		close(s);
124 		return (-1);
125 	}
126 
127 	close(s);
128 	return ((int)ifgr.ifgr_attrib.ifg_carp_demoted);
129 }
130 
131 int
carp_demote_set(char * group,int demote)132 carp_demote_set(char *group, int demote)
133 {
134 	struct carpgroup	*c;
135 
136 	if ((c = carp_group_find(group)) == NULL) {
137 		log_warnx("carp_group_find for %s returned NULL?!", group);
138 		return (-1);
139 	}
140 
141 	if (c->changed_by + demote < 0) {
142 		log_warnx("carp_demote_set: changed_by + demote < 0");
143 		return (-1);
144 	}
145 
146 	if (c->do_demote && carp_demote_ioctl(group, demote) == -1)
147 		return (-1);
148 
149 	c->changed_by += demote;
150 
151 	/* enable demotion when we return to 0, i. e. all sessions up */
152 	if (demote < 0 && c->changed_by == 0)
153 		c->do_demote = 1;
154 
155 	return (0);
156 }
157 
158 int
carp_demote_ioctl(char * group,int demote)159 carp_demote_ioctl(char *group, int demote)
160 {
161 	int			s, res;
162 	struct ifgroupreq	ifgr;
163 
164 	if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
165 		log_warn("carp_demote_get: socket");
166 		return (-1);
167 	}
168 
169 	bzero(&ifgr, sizeof(ifgr));
170 	strlcpy(ifgr.ifgr_name, group, sizeof(ifgr.ifgr_name));
171 	ifgr.ifgr_attrib.ifg_carp_demoted = demote;
172 
173 	if ((res = ioctl(s, SIOCSIFGATTR, (caddr_t)&ifgr)) == -1)
174 		log_warn("unable to %s the demote state "
175 		    "of group '%s'", (demote > 0) ? "increment" : "decrement",
176 		    group);
177 	else
178 		log_info("%s the demote state of group '%s'",
179 		    (demote > 0) ? "incremented" : "decremented", group);
180 
181 	close(s);
182 	return (res);
183 }
184