xref: /openbsd-src/usr.sbin/ripd/carp.c (revision 370872a69ac401b2fb43beb55a87cd81de70e268)
1*370872a6Sclaudio /*	$OpenBSD: carp.c,v 1.3 2009/09/30 12:22:03 claudio Exp $ */
24d8b14b6Smichele 
34d8b14b6Smichele /*
44d8b14b6Smichele  * Copyright (c) 2006 Henning Brauer <henning@openbsd.org>
54d8b14b6Smichele  *
64d8b14b6Smichele  * Permission to use, copy, modify, and distribute this software for any
74d8b14b6Smichele  * purpose with or without fee is hereby granted, provided that the above
84d8b14b6Smichele  * copyright notice and this permission notice appear in all copies.
94d8b14b6Smichele  *
104d8b14b6Smichele  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
114d8b14b6Smichele  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
124d8b14b6Smichele  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
134d8b14b6Smichele  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
144d8b14b6Smichele  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
154d8b14b6Smichele  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
164d8b14b6Smichele  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
174d8b14b6Smichele  */
184d8b14b6Smichele 
194d8b14b6Smichele #include <sys/types.h>
204d8b14b6Smichele #include <sys/socket.h>
214d8b14b6Smichele #include <sys/ioctl.h>
224d8b14b6Smichele #include <net/if.h>
234d8b14b6Smichele 
244d8b14b6Smichele #include <errno.h>
254d8b14b6Smichele #include <string.h>
264d8b14b6Smichele #include <stdlib.h>
274d8b14b6Smichele #include <unistd.h>
284d8b14b6Smichele 
294d8b14b6Smichele #include "ripd.h"
304d8b14b6Smichele #include "log.h"
314d8b14b6Smichele 
324d8b14b6Smichele struct carpgroup {
334d8b14b6Smichele 	TAILQ_ENTRY(carpgroup)	 entry;
344d8b14b6Smichele 	char			*group;
354d8b14b6Smichele 	int			 do_demote;
364d8b14b6Smichele 	int			 changed_by;
374d8b14b6Smichele };
384d8b14b6Smichele 
394d8b14b6Smichele TAILQ_HEAD(carpgroups, carpgroup)	carpgroups =
404d8b14b6Smichele     TAILQ_HEAD_INITIALIZER(carpgroups);
414d8b14b6Smichele 
424d8b14b6Smichele struct carpgroup	*carp_group_find(char *group);
434d8b14b6Smichele int			 carp_demote_ioctl(char *, int);
444d8b14b6Smichele 
454d8b14b6Smichele struct carpgroup *
carp_group_find(char * group)464d8b14b6Smichele carp_group_find(char *group)
474d8b14b6Smichele {
484d8b14b6Smichele 	struct carpgroup	*c;
494d8b14b6Smichele 
504d8b14b6Smichele 	TAILQ_FOREACH(c, &carpgroups, entry)
514d8b14b6Smichele 		if (!strcmp(c->group, group))
524d8b14b6Smichele 			return (c);
534d8b14b6Smichele 
544d8b14b6Smichele 	return (NULL);
554d8b14b6Smichele }
564d8b14b6Smichele 
574d8b14b6Smichele int
carp_demote_init(char * group,int force)584d8b14b6Smichele carp_demote_init(char *group, int force)
594d8b14b6Smichele {
604d8b14b6Smichele 	struct carpgroup	*c;
614d8b14b6Smichele 	int			 level;
624d8b14b6Smichele 
634d8b14b6Smichele 	if ((c = carp_group_find(group)) == NULL) {
644d8b14b6Smichele 		if ((c = calloc(1, sizeof(struct carpgroup))) == NULL) {
654d8b14b6Smichele 			log_warn("carp_demote_init calloc");
664d8b14b6Smichele 			return (-1);
674d8b14b6Smichele 		}
684d8b14b6Smichele 		if ((c->group = strdup(group)) == NULL) {
694d8b14b6Smichele 			log_warn("carp_demote_init strdup");
704d8b14b6Smichele 			free(c);
714d8b14b6Smichele 			return (-1);
724d8b14b6Smichele 		}
734d8b14b6Smichele 
744d8b14b6Smichele 		/* 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);
784d8b14b6Smichele 			return (-1);
79*370872a6Sclaudio 		}
804d8b14b6Smichele 		if (level > 0 || force)
814d8b14b6Smichele 			c->do_demote = 1;
824d8b14b6Smichele 
834d8b14b6Smichele 		TAILQ_INSERT_TAIL(&carpgroups, c, entry);
844d8b14b6Smichele 	}
854d8b14b6Smichele 
864d8b14b6Smichele 	return (0);
874d8b14b6Smichele }
884d8b14b6Smichele 
894d8b14b6Smichele void
carp_demote_shutdown(void)904d8b14b6Smichele carp_demote_shutdown(void)
914d8b14b6Smichele {
924d8b14b6Smichele 	struct carpgroup	*c;
934d8b14b6Smichele 
944d8b14b6Smichele 	while ((c = TAILQ_FIRST(&carpgroups)) != NULL) {
954d8b14b6Smichele 		TAILQ_REMOVE(&carpgroups, c, entry);
96b0720154Sclaudio 		if (c->do_demote && c->changed_by > 0)
97b0720154Sclaudio 			carp_demote_ioctl(c->group, -c->changed_by);
984d8b14b6Smichele 
994d8b14b6Smichele 		free(c->group);
1004d8b14b6Smichele 		free(c);
1014d8b14b6Smichele 	}
1024d8b14b6Smichele }
1034d8b14b6Smichele 
1044d8b14b6Smichele int
carp_demote_get(char * group)1054d8b14b6Smichele carp_demote_get(char *group)
1064d8b14b6Smichele {
1074d8b14b6Smichele 	int			s;
1084d8b14b6Smichele 	struct ifgroupreq	ifgr;
1094d8b14b6Smichele 
1104d8b14b6Smichele 	if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
1114d8b14b6Smichele 		log_warn("carp_demote_get: socket");
1124d8b14b6Smichele 		return (-1);
1134d8b14b6Smichele 	}
1144d8b14b6Smichele 
1154d8b14b6Smichele 	bzero(&ifgr, sizeof(ifgr));
1164d8b14b6Smichele 	strlcpy(ifgr.ifgr_name, group, sizeof(ifgr.ifgr_name));
1174d8b14b6Smichele 
1184d8b14b6Smichele 	if (ioctl(s, SIOCGIFGATTR, (caddr_t)&ifgr) == -1) {
1194d8b14b6Smichele 		if (errno == ENOENT)
1204d8b14b6Smichele 			log_warnx("group \"%s\" does not exist", group);
1214d8b14b6Smichele 		else
1224d8b14b6Smichele 			log_warn("carp_demote_get: ioctl");
1234d8b14b6Smichele 		close(s);
1244d8b14b6Smichele 		return (-1);
1254d8b14b6Smichele 	}
1264d8b14b6Smichele 
1274d8b14b6Smichele 	close(s);
1284d8b14b6Smichele 	return ((int)ifgr.ifgr_attrib.ifg_carp_demoted);
1294d8b14b6Smichele }
1304d8b14b6Smichele 
1314d8b14b6Smichele int
carp_demote_set(char * group,int demote)1324d8b14b6Smichele carp_demote_set(char *group, int demote)
1334d8b14b6Smichele {
1344d8b14b6Smichele 	struct carpgroup	*c;
1354d8b14b6Smichele 
1364d8b14b6Smichele 	if ((c = carp_group_find(group)) == NULL) {
1374d8b14b6Smichele 		log_warnx("carp_group_find for %s returned NULL?!", group);
1384d8b14b6Smichele 		return (-1);
1394d8b14b6Smichele 	}
1404d8b14b6Smichele 
1414d8b14b6Smichele 	if (c->changed_by + demote < 0) {
1424d8b14b6Smichele 		log_warnx("carp_demote_set: changed_by + demote < 0");
1434d8b14b6Smichele 		return (-1);
1444d8b14b6Smichele 	}
1454d8b14b6Smichele 
1464d8b14b6Smichele 	if (c->do_demote && carp_demote_ioctl(group, demote) == -1)
1474d8b14b6Smichele 		return (-1);
1484d8b14b6Smichele 
1494d8b14b6Smichele 	c->changed_by += demote;
1504d8b14b6Smichele 
1514d8b14b6Smichele 	/* enable demotion when we return to 0, i. e. all sessions up */
1524d8b14b6Smichele 	if (demote < 0 && c->changed_by == 0)
1534d8b14b6Smichele 		c->do_demote = 1;
1544d8b14b6Smichele 
1554d8b14b6Smichele 	return (0);
1564d8b14b6Smichele }
1574d8b14b6Smichele 
1584d8b14b6Smichele int
carp_demote_ioctl(char * group,int demote)1594d8b14b6Smichele carp_demote_ioctl(char *group, int demote)
1604d8b14b6Smichele {
1614d8b14b6Smichele 	int			s, res;
1624d8b14b6Smichele 	struct ifgroupreq	ifgr;
1634d8b14b6Smichele 
1644d8b14b6Smichele 	if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
1654d8b14b6Smichele 		log_warn("carp_demote_get: socket");
1664d8b14b6Smichele 		return (-1);
1674d8b14b6Smichele 	}
1684d8b14b6Smichele 
1694d8b14b6Smichele 	bzero(&ifgr, sizeof(ifgr));
1704d8b14b6Smichele 	strlcpy(ifgr.ifgr_name, group, sizeof(ifgr.ifgr_name));
1714d8b14b6Smichele 	ifgr.ifgr_attrib.ifg_carp_demoted = demote;
1724d8b14b6Smichele 
1734d8b14b6Smichele 	if ((res = ioctl(s, SIOCSIFGATTR, (caddr_t)&ifgr)) == -1)
1744d8b14b6Smichele 		log_warn("unable to %s the demote state "
1754d8b14b6Smichele 		    "of group '%s'", (demote > 0) ? "increment" : "decrement",
1764d8b14b6Smichele 		    group);
1774d8b14b6Smichele 	else
1784d8b14b6Smichele 		log_info("%s the demote state of group '%s'",
1794d8b14b6Smichele 		    (demote > 0) ? "incremented" : "decremented", group);
1804d8b14b6Smichele 
1814d8b14b6Smichele 	close(s);
1824d8b14b6Smichele 	return (res);
1834d8b14b6Smichele }
184