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>
43 #include <netinet/in.h>
44 #include <netinet6/in6_var.h>
54 static char *configfile;
57 TAILQ_ENTRY(policyqueue) pc_entry;
58 struct in6_addrpolicy pc_policy;
60 TAILQ_HEAD(policyhead, policyqueue);
61 static struct policyhead policyhead;
63 static void usage(void);
64 static void get_policy(void);
65 static void dump_policy(void);
66 static int mask2plen(struct sockaddr_in6 *);
67 static int parse_prefix(const char *, struct in6_addrpolicy *);
68 static void make_policy_fromfile(char *);
69 static void plen2mask(struct sockaddr_in6 *, int);
70 static void set_policy(void);
71 static void add_policy(char *, char *, char *);
72 static void delete_policy(char *);
73 static void flush_policy(void);
76 main(int argc, char *argv[])
78 TAILQ_INIT(&policyhead);
80 if (argc == 1 || strcasecmp(argv[1], "show") == 0) {
83 } else if (strcasecmp(argv[1], "add") == 0) {
86 add_policy(argv[2], argv[3], argv[4]);
87 } else if (strcasecmp(argv[1], "delete") == 0) {
90 delete_policy(argv[2]);
91 } else if (strcasecmp(argv[1], "flush") == 0) {
94 } else if (strcasecmp(argv[1], "install") == 0) {
98 make_policy_fromfile(configfile);
109 int mib[] = { CTL_NET, PF_INET6, IPPROTO_IPV6, IPV6CTL_ADDRCTLPOLICY };
111 struct in6_addrpolicy *buf;
112 struct in6_addrpolicy *pol, *ep;
114 if (sysctl(mib, nitems(mib), NULL, &l, NULL, 0) < 0) {
115 err(1, "sysctl(IPV6CTL_ADDRCTLPOLICY)");
119 printf("no source-address-selection policy is installed\n");
122 if ((buf = malloc(l)) == NULL) {
123 errx(1, "malloc failed");
126 if (sysctl(mib, nitems(mib), buf, &l, NULL, 0) < 0) {
127 err(1, "sysctl(IPV6CTL_ADDRCTLPOLICY)");
131 ep = buf + l/sizeof(*buf);
132 for (pol = buf; pol + 1 <= ep; pol++) {
133 struct policyqueue *new;
135 if ((new = malloc(sizeof(*new))) == NULL)
136 errx(1, "malloc failed\n");
137 new->pc_policy = *pol;
138 TAILQ_INSERT_TAIL(&policyhead, new, pc_entry);
148 char addrbuf[NI_MAXHOST];
149 struct in6_addrpolicy *pol;
150 struct policyqueue *ent;
153 for (ent = TAILQ_FIRST(&policyhead); ent;
154 ent = TAILQ_NEXT(ent, pc_entry)) {
155 pol = &ent->pc_policy;
157 printf("%-30s %5s %5s %8s\n",
158 "Prefix", "Prec", "Label", "Use");
162 if ((getnameinfo((struct sockaddr *)&pol->addr,
163 sizeof(pol->addr), addrbuf, sizeof(addrbuf),
164 NULL, 0, NI_NUMERICHOST))) {
165 warnx("getnameinfo for prefix address failed");
168 if ((plen = mask2plen(&pol->addrmask)) < 0) {
169 warnx("invalid address mask");
172 addrlen = strlen(addrbuf);
173 if (addrlen + sizeof("/128") < sizeof(addrbuf)) {
174 snprintf(&addrbuf[addrlen],
175 sizeof(addrbuf) - addrlen - 1,
177 printf("%-30s", addrbuf);
179 printf("%s/%d", addrbuf, plen);
180 printf(" %5d %5d %8llu\n", pol->preced, pol->label,
181 (unsigned long long)pol->use);
185 #define SKIP_WHITE(p, emptyok) \
187 while((*(p) == ' ' || *(p) == '\t')) \
189 if ((*(p) == '\0' || (*(p) == '\n')) && !(emptyok)) \
192 #define SKIP_WORD(p) \
194 while(*(p) != ' ' && *(p) != '\t') \
196 if (*(p) == '\0' || *(p) == '\n') \
201 make_policy_fromfile(char *conf)
203 char line[_POSIX2_LINE_MAX], *cp;
207 struct in6_addrpolicy pol0;
208 struct policyqueue *new;
210 if ((fp = fopen(conf, "r")) == NULL)
211 err(1, "fopen: %s", conf);
213 while(fgets(line, sizeof(line), fp)) {
217 memset(&pol0, 0, sizeof(pol0));
221 if (*cp == '\n') /* empty line */
226 if (parse_prefix((const char *)addrstr, &pol0))
229 /* get precedence value */
232 pol0.preced = atoi(cp);
237 pol0.label = atoi(cp);
239 /* parse succeeded. make a control buffer entry. */
240 if ((new = malloc(sizeof(*new))) == NULL)
241 errx(1, "malloc failed\n");
242 memset(new, 0, sizeof(*new));
243 new->pc_policy = pol0;
244 TAILQ_INSERT_TAIL(&policyhead, new, pc_entry);
251 errx(1, "parse failed at line %d", count);
256 parse_prefix(const char *prefix0, struct in6_addrpolicy *pol)
259 char *prefix, *plenstr;
260 struct addrinfo hints, *res;
262 if ((prefix = strdup(prefix0)) == NULL)
263 errx(1, "strdup failed");
265 if ((plenstr = strchr(prefix, '/')) == NULL) {
271 memset(&hints, 0, sizeof(hints));
272 hints.ai_flags = AI_NUMERICHOST;
273 hints.ai_family = AF_INET6;
275 if ((e = getaddrinfo(prefix, NULL, &hints, &res)) != 0) {
276 warnx("getaddrinfo failed for %s: %s", prefix,
280 memcpy(&pol->addr, res->ai_addr, res->ai_addrlen);
282 plen = atoi(plenstr + 1);
283 if (plen < 0 || plen > 128) {
284 warnx("invalid prefix length: %d", plen);
288 plen2mask(&pol->addrmask, plen);
296 plen2mask(struct sockaddr_in6 *mask, int plen)
298 u_char *cp = (unsigned char *)&mask->sin6_addr;
300 memset(mask, 0, sizeof(*mask));
301 mask->sin6_family = AF_INET6; /* just in case */
302 mask->sin6_len = sizeof(*mask);
304 for(; plen >= 8; plen -= 8)
307 *cp = (0xff << (8 - plen));
313 struct policyqueue *ent;
316 if ((s = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP)) < 0)
317 err(1, "socket(UDP)");
319 for (ent = TAILQ_FIRST(&policyhead); ent;
320 ent = TAILQ_NEXT(ent, pc_entry)) {
321 if (ioctl(s, SIOCAADDRCTL_POLICY, &ent->pc_policy))
322 warn("ioctl(SIOCAADDRCTL_POLICY)");
329 mask2plen(struct sockaddr_in6 *mask)
331 int masklen, final = 0;
335 lim = (u_char *)(mask + 1);
336 for (p = (u_char *)(&mask->sin6_addr); p < lim; p++) {
388 add_policy(char *prefix, char *prec, char *label)
390 struct in6_addrpolicy p;
393 memset(&p, 0, sizeof(p));
395 if (parse_prefix((const char *)prefix, &p))
396 errx(1, "bad prefix: %s", prefix);
397 p.preced = atoi(prec);
398 p.label = atoi(label);
400 if ((s = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP)) < 0)
401 err(1, "socket(UDP)");
402 if (ioctl(s, SIOCAADDRCTL_POLICY, &p))
403 err(1, "ioctl(SIOCAADDRCTL_POLICY)");
409 delete_policy(char *prefix)
411 struct in6_addrpolicy p;
414 memset(&p, 0, sizeof(p));
416 if (parse_prefix((const char *)prefix, &p))
417 errx(1, "bad prefix: %s", prefix);
419 if ((s = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP)) < 0)
420 err(1, "socket(UDP)");
421 if (ioctl(s, SIOCDADDRCTL_POLICY, &p))
422 err(1, "ioctl(SIOCDADDRCTL_POLICY)");
430 struct policyqueue *ent;
433 if ((s = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP)) < 0)
434 err(1, "socket(UDP)");
436 for (ent = TAILQ_FIRST(&policyhead); ent;
437 ent = TAILQ_NEXT(ent, pc_entry)) {
438 if (ioctl(s, SIOCDADDRCTL_POLICY, &ent->pc_policy))
439 warn("ioctl(SIOCDADDRCTL_POLICY)");
448 fprintf(stderr, "usage: ip6addrctl [show]\n");
449 fprintf(stderr, " ip6addrctl add "
450 "<prefix> <precedence> <label>\n");
451 fprintf(stderr, " ip6addrctl delete <prefix>\n");
452 fprintf(stderr, " ip6addrctl flush\n");
453 fprintf(stderr, " ip6addrctl install <configfile>\n");