1 /* $KAME: ip6addrctl.c,v 1.3 2003/12/16 08:14:28 suz Exp $ */
4 * Copyright (C) 2001 WIDE Project.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the project nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 #include <sys/types.h>
35 #include <sys/socket.h>
36 #include <sys/queue.h>
37 #include <sys/param.h>
38 #include <sys/ioctl.h>
39 #include <sys/sysctl.h>
42 #include <net/if_var.h>
44 #include <netinet/in.h>
45 #include <netinet6/in6_var.h>
55 static char *configfile;
58 TAILQ_ENTRY(policyqueue) pc_entry;
59 struct in6_addrpolicy pc_policy;
61 TAILQ_HEAD(policyhead, policyqueue);
62 static struct policyhead policyhead;
64 static void usage(void);
65 static void get_policy(void);
66 static void dump_policy(void);
67 static int mask2plen(struct sockaddr_in6 *);
68 static int parse_prefix(const char *, struct in6_addrpolicy *);
69 static void make_policy_fromfile(char *);
70 static void plen2mask(struct sockaddr_in6 *, int);
71 static void set_policy(void);
72 static void add_policy(char *, char *, char *);
73 static void delete_policy(char *);
74 static void flush_policy(void);
77 main(int argc, char *argv[])
79 TAILQ_INIT(&policyhead);
81 if (argc == 1 || strcasecmp(argv[1], "show") == 0) {
84 } else if (strcasecmp(argv[1], "add") == 0) {
87 add_policy(argv[2], argv[3], argv[4]);
88 } else if (strcasecmp(argv[1], "delete") == 0) {
91 delete_policy(argv[2]);
92 } else if (strcasecmp(argv[1], "flush") == 0) {
95 } else if (strcasecmp(argv[1], "install") == 0) {
99 make_policy_fromfile(configfile);
110 int mib[] = { CTL_NET, PF_INET6, IPPROTO_IPV6, IPV6CTL_ADDRCTLPOLICY };
112 struct in6_addrpolicy *buf;
113 struct in6_addrpolicy *pol, *ep;
115 if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), NULL, &l, NULL, 0) < 0) {
116 err(1, "sysctl(IPV6CTL_ADDRCTLPOLICY)");
120 printf("no source-address-selection policy is installed\n");
123 if ((buf = malloc(l)) == NULL) {
124 errx(1, "malloc failed");
127 if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), buf, &l, NULL, 0) < 0) {
128 err(1, "sysctl(IPV6CTL_ADDRCTLPOLICY)");
132 ep = buf + l/sizeof(*buf);
133 for (pol = buf; pol + 1 <= ep; pol++) {
134 struct policyqueue *new;
136 if ((new = malloc(sizeof(*new))) == NULL)
137 errx(1, "malloc failed\n");
138 new->pc_policy = *pol;
139 TAILQ_INSERT_TAIL(&policyhead, new, pc_entry);
149 char addrbuf[NI_MAXHOST];
150 struct in6_addrpolicy *pol;
151 struct policyqueue *ent;
154 for (ent = TAILQ_FIRST(&policyhead); ent;
155 ent = TAILQ_NEXT(ent, pc_entry)) {
156 pol = &ent->pc_policy;
158 printf("%-30s %5s %5s %8s\n",
159 "Prefix", "Prec", "Label", "Use");
163 if ((getnameinfo((struct sockaddr *)&pol->addr,
164 sizeof(pol->addr), addrbuf, sizeof(addrbuf),
165 NULL, 0, NI_NUMERICHOST))) {
166 warnx("getnameinfo for prefix address failed");
169 if ((plen = mask2plen(&pol->addrmask)) < 0) {
170 warnx("invalid address mask");
173 addrlen = strlen(addrbuf);
174 if (addrlen + sizeof("/128") < sizeof(addrbuf)) {
175 snprintf(&addrbuf[addrlen],
176 sizeof(addrbuf) - addrlen - 1,
178 printf("%-30s", addrbuf);
180 printf("%s/%d", addrbuf, plen);
181 printf(" %5d %5d %8llu\n", pol->preced, pol->label,
182 (unsigned long long)pol->use);
186 #define SKIP_WHITE(p, emptyok) \
188 while((*(p) == ' ' || *(p) == '\t')) \
190 if ((*(p) == '\0' || (*(p) == '\n')) && !(emptyok)) \
193 #define SKIP_WORD(p) \
195 while(*(p) != ' ' && *(p) != '\t') \
197 if (*(p) == '\0' || *(p) == '\n') \
202 make_policy_fromfile(char *conf)
204 char line[_POSIX2_LINE_MAX], *cp;
208 struct in6_addrpolicy pol0;
209 struct policyqueue *new;
211 if ((fp = fopen(conf, "r")) == NULL)
212 err(1, "fopen: %s", conf);
214 while(fgets(line, sizeof(line), fp)) {
218 memset(&pol0, 0, sizeof(pol0));
222 if (*cp == '\n') /* empty line */
227 if (parse_prefix((const char *)addrstr, &pol0))
230 /* get precedence value */
233 pol0.preced = atoi(cp);
238 pol0.label = atoi(cp);
240 /* parse succeeded. make a control buffer entry. */
241 if ((new = malloc(sizeof(*new))) == NULL)
242 errx(1, "malloc failed\n");
243 memset(new, 0, sizeof(*new));
244 new->pc_policy = pol0;
245 TAILQ_INSERT_TAIL(&policyhead, new, pc_entry);
252 errx(1, "parse failed at line %d", count);
257 parse_prefix(const char *prefix0, struct in6_addrpolicy *pol)
260 char *prefix, *plenstr;
261 struct addrinfo hints, *res;
263 if ((prefix = strdup(prefix0)) == NULL)
264 errx(1, "strdup failed");
266 if ((plenstr = strchr(prefix, '/')) == NULL) {
272 memset(&hints, 0, sizeof(hints));
273 hints.ai_flags = AI_NUMERICHOST;
274 hints.ai_family = AF_INET6;
276 if ((e = getaddrinfo(prefix, NULL, &hints, &res)) != 0) {
277 warnx("getaddrinfo failed for %s: %s", prefix,
281 memcpy(&pol->addr, res->ai_addr, res->ai_addrlen);
283 plen = atoi(plenstr + 1);
284 if (plen < 0 || plen > 128) {
285 warnx("invalid prefix length: %d", plen);
289 plen2mask(&pol->addrmask, plen);
297 plen2mask(struct sockaddr_in6 *mask, int plen)
299 u_char *cp = (unsigned char *)&mask->sin6_addr;
301 memset(mask, 0, sizeof(*mask));
302 mask->sin6_family = AF_INET6; /* just in case */
303 mask->sin6_len = sizeof(*mask);
305 for(; plen >= 8; plen -= 8)
308 *cp = (0xff << (8 - plen));
314 struct policyqueue *ent;
317 if ((s = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP)) < 0)
318 err(1, "socket(UDP)");
320 for (ent = TAILQ_FIRST(&policyhead); ent;
321 ent = TAILQ_NEXT(ent, pc_entry)) {
322 if (ioctl(s, SIOCAADDRCTL_POLICY, &ent->pc_policy))
323 warn("ioctl(SIOCAADDRCTL_POLICY)");
330 mask2plen(struct sockaddr_in6 *mask)
332 int masklen, final = 0;
336 lim = (u_char *)(mask + 1);
337 for (p = (u_char *)(&mask->sin6_addr); p < lim; p++) {
389 add_policy(char *prefix, char *prec, char *label)
391 struct in6_addrpolicy p;
394 memset(&p, 0, sizeof(p));
396 if (parse_prefix((const char *)prefix, &p))
397 errx(1, "bad prefix: %s", prefix);
398 p.preced = atoi(prec);
399 p.label = atoi(label);
401 if ((s = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP)) < 0)
402 err(1, "socket(UDP)");
403 if (ioctl(s, SIOCAADDRCTL_POLICY, &p))
404 err(1, "ioctl(SIOCAADDRCTL_POLICY)");
410 delete_policy(char *prefix)
412 struct in6_addrpolicy p;
415 memset(&p, 0, sizeof(p));
417 if (parse_prefix((const char *)prefix, &p))
418 errx(1, "bad prefix: %s", prefix);
420 if ((s = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP)) < 0)
421 err(1, "socket(UDP)");
422 if (ioctl(s, SIOCDADDRCTL_POLICY, &p))
423 err(1, "ioctl(SIOCDADDRCTL_POLICY)");
431 struct policyqueue *ent;
434 if ((s = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP)) < 0)
435 err(1, "socket(UDP)");
437 for (ent = TAILQ_FIRST(&policyhead); ent;
438 ent = TAILQ_NEXT(ent, pc_entry)) {
439 if (ioctl(s, SIOCDADDRCTL_POLICY, &ent->pc_policy))
440 warn("ioctl(SIOCDADDRCTL_POLICY)");
449 fprintf(stderr, "usage: ip6addrctl [show]\n");
450 fprintf(stderr, " ip6addrctl add "
451 "<prefix> <precedence> <label>\n");
452 fprintf(stderr, " ip6addrctl delete <prefix>\n");
453 fprintf(stderr, " ip6addrctl flush\n");
454 fprintf(stderr, " ip6addrctl install <configfile>\n");