1 /* $OpenBSD: carp.c,v 1.1 2007/10/08 10:44:50 norby 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 "ospf6d.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 * 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 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 return (-1); 77 if (level > 0 || force) 78 c->do_demote = 1; 79 80 TAILQ_INSERT_TAIL(&carpgroups, c, entry); 81 } 82 83 return (0); 84 } 85 86 void 87 carp_demote_shutdown(void) 88 { 89 struct carpgroup *c; 90 91 while ((c = TAILQ_FIRST(&carpgroups)) != NULL) { 92 TAILQ_REMOVE(&carpgroups, c, entry); 93 for (; c->changed_by > 0; c->changed_by--) 94 if (c->do_demote) 95 carp_demote_ioctl(c->group, -1); 96 97 free(c->group); 98 free(c); 99 } 100 } 101 102 int 103 carp_demote_get(char *group) 104 { 105 int s; 106 struct ifgroupreq ifgr; 107 108 if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == -1) { 109 log_warn("carp_demote_get: socket"); 110 return (-1); 111 } 112 113 bzero(&ifgr, sizeof(ifgr)); 114 strlcpy(ifgr.ifgr_name, group, sizeof(ifgr.ifgr_name)); 115 116 if (ioctl(s, SIOCGIFGATTR, (caddr_t)&ifgr) == -1) { 117 if (errno == ENOENT) 118 log_warnx("group \"%s\" does not exist", group); 119 else 120 log_warn("carp_demote_get: ioctl"); 121 close(s); 122 return (-1); 123 } 124 125 close(s); 126 return ((int)ifgr.ifgr_attrib.ifg_carp_demoted); 127 } 128 129 int 130 carp_demote_set(char *group, int demote) 131 { 132 struct carpgroup *c; 133 134 if ((c = carp_group_find(group)) == NULL) { 135 log_warnx("carp_group_find for %s returned NULL?!", group); 136 return (-1); 137 } 138 139 if (c->changed_by + demote < 0) { 140 log_warnx("carp_demote_set: changed_by + demote < 0"); 141 return (-1); 142 } 143 144 if (c->do_demote && carp_demote_ioctl(group, demote) == -1) 145 return (-1); 146 147 c->changed_by += demote; 148 149 /* enable demotion when we return to 0, i. e. all sessions up */ 150 if (demote < 0 && c->changed_by == 0) 151 c->do_demote = 1; 152 153 return (0); 154 } 155 156 int 157 carp_demote_ioctl(char *group, int demote) 158 { 159 int s, res; 160 struct ifgroupreq ifgr; 161 162 if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == -1) { 163 log_warn("carp_demote_get: socket"); 164 return (-1); 165 } 166 167 bzero(&ifgr, sizeof(ifgr)); 168 strlcpy(ifgr.ifgr_name, group, sizeof(ifgr.ifgr_name)); 169 ifgr.ifgr_attrib.ifg_carp_demoted = demote; 170 171 if ((res = ioctl(s, SIOCSIFGATTR, (caddr_t)&ifgr)) == -1) 172 log_warn("unable to %s the demote state " 173 "of group '%s'", (demote > 0) ? "increment" : "decrement", 174 group); 175 else 176 log_info("%s the demote state of group '%s'", 177 (demote > 0) ? "incremented" : "decremented", group); 178 179 close(s); 180 return (res); 181 } 182