xref: /openbsd-src/usr.sbin/bgpd/carp.c (revision eafe309e35977891f1b9cf639e7c1bffb25da0dd)
1*eafe309eSclaudio /*	$OpenBSD: carp.c,v 1.11 2022/08/17 15:15:25 claudio Exp $ */
276cfc8ebShenning 
376cfc8ebShenning /*
476cfc8ebShenning  * Copyright (c) 2006 Henning Brauer <henning@openbsd.org>
576cfc8ebShenning  *
676cfc8ebShenning  * Permission to use, copy, modify, and distribute this software for any
776cfc8ebShenning  * purpose with or without fee is hereby granted, provided that the above
876cfc8ebShenning  * copyright notice and this permission notice appear in all copies.
976cfc8ebShenning  *
1076cfc8ebShenning  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
1176cfc8ebShenning  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1276cfc8ebShenning  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
1376cfc8ebShenning  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1476cfc8ebShenning  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
1576cfc8ebShenning  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
1676cfc8ebShenning  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1776cfc8ebShenning  */
1876cfc8ebShenning 
1976cfc8ebShenning #include <sys/types.h>
2076cfc8ebShenning #include <sys/socket.h>
2176cfc8ebShenning #include <sys/ioctl.h>
2276cfc8ebShenning #include <net/if.h>
2376cfc8ebShenning 
2476cfc8ebShenning #include <errno.h>
2576cfc8ebShenning #include <string.h>
2676cfc8ebShenning #include <stdlib.h>
2776cfc8ebShenning #include <unistd.h>
2876cfc8ebShenning 
2976cfc8ebShenning #include "bgpd.h"
3076cfc8ebShenning #include "session.h"
315e3f6f95Sbenno #include "log.h"
3276cfc8ebShenning 
3376cfc8ebShenning struct carpgroup {
3476cfc8ebShenning 	TAILQ_ENTRY(carpgroup)	 entry;
3576cfc8ebShenning 	char			*group;
3676cfc8ebShenning 	int			 do_demote;
3776cfc8ebShenning 	int			 changed_by;
3876cfc8ebShenning };
3976cfc8ebShenning 
4076cfc8ebShenning TAILQ_HEAD(carpgroups, carpgroup)	carpgroups =
4176cfc8ebShenning     TAILQ_HEAD_INITIALIZER(carpgroups);
4276cfc8ebShenning 
4376cfc8ebShenning struct carpgroup	*carp_group_find(char *group);
4476cfc8ebShenning int			 carp_demote_ioctl(char *, int);
4576cfc8ebShenning 
4676cfc8ebShenning struct carpgroup *
carp_group_find(char * group)4776cfc8ebShenning carp_group_find(char *group)
4876cfc8ebShenning {
4976cfc8ebShenning 	struct carpgroup	*c;
5076cfc8ebShenning 
5176cfc8ebShenning 	TAILQ_FOREACH(c, &carpgroups, entry)
5276cfc8ebShenning 		if (!strcmp(c->group, group))
5376cfc8ebShenning 			return (c);
5476cfc8ebShenning 
5576cfc8ebShenning 	return (NULL);
5676cfc8ebShenning }
5776cfc8ebShenning 
5876cfc8ebShenning int
carp_demote_init(char * group,int force)5976cfc8ebShenning carp_demote_init(char *group, int force)
6076cfc8ebShenning {
6176cfc8ebShenning 	struct carpgroup	*c;
6276cfc8ebShenning 	int			 level;
6376cfc8ebShenning 
6476cfc8ebShenning 	if ((c = carp_group_find(group)) == NULL) {
6576cfc8ebShenning 		if ((c = calloc(1, sizeof(struct carpgroup))) == NULL) {
6676cfc8ebShenning 			log_warn("carp_demote_init calloc");
6776cfc8ebShenning 			return (-1);
6876cfc8ebShenning 		}
6976cfc8ebShenning 		if ((c->group = strdup(group)) == NULL) {
703f13090aSclaudio 			log_warn("carp_demote_init strdup");
7176cfc8ebShenning 			free(c);
7276cfc8ebShenning 			return (-1);
7376cfc8ebShenning 		}
7476cfc8ebShenning 
7576cfc8ebShenning 		/* only demote if this group already is demoted */
768155cb22Stobias 		if ((level = carp_demote_get(group)) == -1) {
778155cb22Stobias 			free(c->group);
788155cb22Stobias 			free(c);
7976cfc8ebShenning 			return (-1);
808155cb22Stobias 		}
8176cfc8ebShenning 		if (level > 0 || force)
8276cfc8ebShenning 			c->do_demote = 1;
8376cfc8ebShenning 
8476cfc8ebShenning 		TAILQ_INSERT_TAIL(&carpgroups, c, entry);
8576cfc8ebShenning 	}
8676cfc8ebShenning 
8776cfc8ebShenning 	return (0);
8876cfc8ebShenning }
8976cfc8ebShenning 
9076cfc8ebShenning void
carp_demote_shutdown(void)9176cfc8ebShenning carp_demote_shutdown(void)
9276cfc8ebShenning {
9376cfc8ebShenning 	struct carpgroup	*c;
9476cfc8ebShenning 
9576cfc8ebShenning 	while ((c = TAILQ_FIRST(&carpgroups)) != NULL) {
9676cfc8ebShenning 		TAILQ_REMOVE(&carpgroups, c, entry);
9713b0103fSclaudio 		if (c->do_demote && c->changed_by > 0)
9813b0103fSclaudio 			carp_demote_ioctl(c->group, -c->changed_by);
9976cfc8ebShenning 
10076cfc8ebShenning 		free(c->group);
10176cfc8ebShenning 		free(c);
10276cfc8ebShenning 	}
10376cfc8ebShenning }
10476cfc8ebShenning 
10576cfc8ebShenning int
carp_demote_get(char * group)10676cfc8ebShenning carp_demote_get(char *group)
10776cfc8ebShenning {
10876cfc8ebShenning 	int			s;
10976cfc8ebShenning 	struct ifgroupreq	ifgr;
11076cfc8ebShenning 
1113ac77e71Sclaudio 	if ((s = socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0)) == -1) {
11276cfc8ebShenning 		log_warn("carp_demote_get: socket");
11376cfc8ebShenning 		return (-1);
11476cfc8ebShenning 	}
11576cfc8ebShenning 
116*eafe309eSclaudio 	memset(&ifgr, 0, sizeof(ifgr));
11776cfc8ebShenning 	strlcpy(ifgr.ifgr_name, group, sizeof(ifgr.ifgr_name));
11876cfc8ebShenning 
11976cfc8ebShenning 	if (ioctl(s, SIOCGIFGATTR, (caddr_t)&ifgr) == -1) {
12076cfc8ebShenning 		if (errno == ENOENT)
12176cfc8ebShenning 			log_warnx("group \"%s\" does not exist", group);
12276cfc8ebShenning 		else
12376cfc8ebShenning 			log_warn("carp_demote_get: ioctl");
12476cfc8ebShenning 		close(s);
12576cfc8ebShenning 		return (-1);
12676cfc8ebShenning 	}
12776cfc8ebShenning 
12876cfc8ebShenning 	close(s);
12976cfc8ebShenning 	return ((int)ifgr.ifgr_attrib.ifg_carp_demoted);
13076cfc8ebShenning }
13176cfc8ebShenning 
13276cfc8ebShenning int
carp_demote_set(char * group,int demote)13376cfc8ebShenning carp_demote_set(char *group, int demote)
13476cfc8ebShenning {
13576cfc8ebShenning 	struct carpgroup	*c;
13676cfc8ebShenning 
13776cfc8ebShenning 	if ((c = carp_group_find(group)) == NULL) {
13876cfc8ebShenning 		log_warnx("carp_group_find for %s returned NULL?!", group);
13976cfc8ebShenning 		return (-1);
14076cfc8ebShenning 	}
14176cfc8ebShenning 
14276cfc8ebShenning 	if (c->changed_by + demote < 0) {
14376cfc8ebShenning 		log_warnx("carp_demote_set: changed_by + demote < 0");
14476cfc8ebShenning 		return (-1);
14576cfc8ebShenning 	}
14676cfc8ebShenning 
14776cfc8ebShenning 	if (c->do_demote && carp_demote_ioctl(group, demote) == -1)
14876cfc8ebShenning 		return (-1);
14976cfc8ebShenning 
15076cfc8ebShenning 	c->changed_by += demote;
151b882c823Shenning 
152b882c823Shenning 	/* enable demotion when we return to 0, i. e. all sessions up */
153b882c823Shenning 	if (demote < 0 && c->changed_by == 0)
154b882c823Shenning 		c->do_demote = 1;
155b882c823Shenning 
15676cfc8ebShenning 	return (0);
15776cfc8ebShenning }
15876cfc8ebShenning 
15976cfc8ebShenning int
carp_demote_ioctl(char * group,int demote)16076cfc8ebShenning carp_demote_ioctl(char *group, int demote)
16176cfc8ebShenning {
16276cfc8ebShenning 	int			s, res;
16376cfc8ebShenning 	struct ifgroupreq	ifgr;
16476cfc8ebShenning 
1653ac77e71Sclaudio 	if ((s = socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0)) == -1) {
16675f8cafaSbenno 		log_warn("%s: socket", __func__);
16776cfc8ebShenning 		return (-1);
16876cfc8ebShenning 	}
16976cfc8ebShenning 
170*eafe309eSclaudio 	memset(&ifgr, 0, sizeof(ifgr));
17176cfc8ebShenning 	strlcpy(ifgr.ifgr_name, group, sizeof(ifgr.ifgr_name));
17276cfc8ebShenning 	ifgr.ifgr_attrib.ifg_carp_demoted = demote;
17376cfc8ebShenning 
17476cfc8ebShenning 	if ((res = ioctl(s, SIOCSIFGATTR, (caddr_t)&ifgr)) == -1)
17576cfc8ebShenning 		log_warn("unable to %s the demote state "
17676cfc8ebShenning 		    "of group '%s'", (demote > 0) ? "increment" : "decrement",
17776cfc8ebShenning 		    group);
17876cfc8ebShenning 	else
17976cfc8ebShenning 		log_info("%s the demote state of group '%s'",
18076cfc8ebShenning 		    (demote > 0) ? "incremented" : "decremented", group);
18176cfc8ebShenning 
18276cfc8ebShenning 	close(s);
18376cfc8ebShenning 	return (res);
18476cfc8ebShenning }
185