ip6addrctl.c revision 1.1 1 1.1 christos /* $KAME: ip6addrctl.c,v 1.3 2003/12/16 08:14:28 suz Exp $ */
2 1.1 christos
3 1.1 christos /*
4 1.1 christos * Copyright (C) 2001 WIDE Project.
5 1.1 christos * All rights reserved.
6 1.1 christos *
7 1.1 christos * Redistribution and use in source and binary forms, with or without
8 1.1 christos * modification, are permitted provided that the following conditions
9 1.1 christos * are met:
10 1.1 christos * 1. Redistributions of source code must retain the above copyright
11 1.1 christos * notice, this list of conditions and the following disclaimer.
12 1.1 christos * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 christos * notice, this list of conditions and the following disclaimer in the
14 1.1 christos * documentation and/or other materials provided with the distribution.
15 1.1 christos * 3. Neither the name of the project nor the names of its contributors
16 1.1 christos * may be used to endorse or promote products derived from this software
17 1.1 christos * without specific prior written permission.
18 1.1 christos *
19 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20 1.1 christos * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 1.1 christos * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 1.1 christos * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23 1.1 christos * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 1.1 christos * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 1.1 christos * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 1.1 christos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 1.1 christos * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 1.1 christos * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 1.1 christos * SUCH DAMAGE.
30 1.1 christos *
31 1.1 christos * $FreeBSD: head/usr.sbin/ip6addrctl/ip6addrctl.c 281143 2015-04-06 09:42:23Z glebius $
32 1.1 christos */
33 1.1 christos #include <sys/cdefs.h>
34 1.1 christos __RCSID("$NetBSD: ip6addrctl.c,v 1.1 2015/12/12 23:35:56 christos Exp $");
35 1.1 christos
36 1.1 christos #include <sys/types.h>
37 1.1 christos #include <sys/socket.h>
38 1.1 christos #include <sys/queue.h>
39 1.1 christos #include <sys/param.h>
40 1.1 christos #include <sys/ioctl.h>
41 1.1 christos #include <sys/sysctl.h>
42 1.1 christos
43 1.1 christos #include <net/if.h>
44 1.1 christos
45 1.1 christos #include <netinet/in.h>
46 1.1 christos #include <netinet6/in6_var.h>
47 1.1 christos
48 1.1 christos #include <stdlib.h>
49 1.1 christos #include <netdb.h>
50 1.1 christos #include <stdio.h>
51 1.1 christos #include <unistd.h>
52 1.1 christos #include <limits.h>
53 1.1 christos #include <string.h>
54 1.1 christos #include <err.h>
55 1.1 christos
56 1.1 christos static char *configfile;
57 1.1 christos
58 1.1 christos struct policyqueue {
59 1.1 christos TAILQ_ENTRY(policyqueue) pc_entry;
60 1.1 christos struct in6_addrpolicy pc_policy;
61 1.1 christos };
62 1.1 christos TAILQ_HEAD(policyhead, policyqueue);
63 1.1 christos static struct policyhead policyhead;
64 1.1 christos
65 1.1 christos static void usage(void);
66 1.1 christos static void get_policy(void);
67 1.1 christos static void dump_policy(void);
68 1.1 christos static int mask2plen(struct sockaddr_in6 *);
69 1.1 christos static int parse_prefix(const char *, struct in6_addrpolicy *);
70 1.1 christos static void make_policy_fromfile(char *);
71 1.1 christos static void plen2mask(struct sockaddr_in6 *, int);
72 1.1 christos static void set_policy(void);
73 1.1 christos static void add_policy(char *, char *, char *);
74 1.1 christos static void delete_policy(char *);
75 1.1 christos static void flush_policy(void);
76 1.1 christos
77 1.1 christos int
78 1.1 christos main(int argc, char *argv[])
79 1.1 christos {
80 1.1 christos TAILQ_INIT(&policyhead);
81 1.1 christos
82 1.1 christos if (argc == 1 || strcasecmp(argv[1], "show") == 0) {
83 1.1 christos get_policy();
84 1.1 christos dump_policy();
85 1.1 christos } else if (strcasecmp(argv[1], "add") == 0) {
86 1.1 christos if (argc < 5)
87 1.1 christos usage();
88 1.1 christos add_policy(argv[2], argv[3], argv[4]);
89 1.1 christos } else if (strcasecmp(argv[1], "delete") == 0) {
90 1.1 christos if (argc < 3)
91 1.1 christos usage();
92 1.1 christos delete_policy(argv[2]);
93 1.1 christos } else if (strcasecmp(argv[1], "flush") == 0) {
94 1.1 christos get_policy();
95 1.1 christos flush_policy();
96 1.1 christos } else if (strcasecmp(argv[1], "install") == 0) {
97 1.1 christos if (argc < 3)
98 1.1 christos usage();
99 1.1 christos configfile = argv[2];
100 1.1 christos make_policy_fromfile(configfile);
101 1.1 christos set_policy();
102 1.1 christos } else
103 1.1 christos usage();
104 1.1 christos
105 1.1 christos exit(0);
106 1.1 christos }
107 1.1 christos
108 1.1 christos static void
109 1.1 christos get_policy(void)
110 1.1 christos {
111 1.1 christos int mib[] = { CTL_NET, PF_INET6, IPPROTO_IPV6, IPV6CTL_ADDRCTLPOLICY };
112 1.1 christos size_t l;
113 1.1 christos struct in6_addrpolicy *buf;
114 1.1 christos struct in6_addrpolicy *pol, *ep;
115 1.1 christos
116 1.1 christos if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), NULL, &l, NULL, 0) < 0) {
117 1.1 christos err(1, "sysctl(IPV6CTL_ADDRCTLPOLICY)");
118 1.1 christos /* NOTREACHED */
119 1.1 christos }
120 1.1 christos if (l == 0) {
121 1.1 christos printf("no source-address-selection policy is installed\n");
122 1.1 christos return;
123 1.1 christos }
124 1.1 christos if ((buf = malloc(l)) == NULL) {
125 1.1 christos errx(1, "malloc failed");
126 1.1 christos /* NOTREACHED */
127 1.1 christos }
128 1.1 christos if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), buf, &l, NULL, 0) < 0) {
129 1.1 christos err(1, "sysctl(IPV6CTL_ADDRCTLPOLICY)");
130 1.1 christos /* NOTREACHED */
131 1.1 christos }
132 1.1 christos
133 1.1 christos ep = buf + l/sizeof(*buf);
134 1.1 christos for (pol = buf; pol + 1 <= ep; pol++) {
135 1.1 christos struct policyqueue *new;
136 1.1 christos
137 1.1 christos if ((new = malloc(sizeof(*new))) == NULL)
138 1.1 christos errx(1, "malloc failed\n");
139 1.1 christos new->pc_policy = *pol;
140 1.1 christos TAILQ_INSERT_TAIL(&policyhead, new, pc_entry);
141 1.1 christos }
142 1.1 christos
143 1.1 christos free(buf);
144 1.1 christos }
145 1.1 christos
146 1.1 christos static void
147 1.1 christos dump_policy(void)
148 1.1 christos {
149 1.1 christos size_t addrlen;
150 1.1 christos char addrbuf[NI_MAXHOST];
151 1.1 christos struct in6_addrpolicy *pol;
152 1.1 christos struct policyqueue *ent;
153 1.1 christos int plen, first = 1;
154 1.1 christos
155 1.1 christos for (ent = TAILQ_FIRST(&policyhead); ent;
156 1.1 christos ent = TAILQ_NEXT(ent, pc_entry)) {
157 1.1 christos pol = &ent->pc_policy;
158 1.1 christos if (first) {
159 1.1 christos printf("%-30s %5s %5s %8s\n",
160 1.1 christos "Prefix", "Prec", "Label", "Use");
161 1.1 christos first = 0;
162 1.1 christos }
163 1.1 christos
164 1.1 christos if ((getnameinfo((struct sockaddr *)&pol->addr,
165 1.1 christos sizeof(pol->addr), addrbuf, sizeof(addrbuf),
166 1.1 christos NULL, 0, NI_NUMERICHOST))) {
167 1.1 christos warnx("getnameinfo for prefix address failed");
168 1.1 christos continue;
169 1.1 christos }
170 1.1 christos if ((plen = mask2plen(&pol->addrmask)) < 0) {
171 1.1 christos warnx("invalid address mask");
172 1.1 christos continue;
173 1.1 christos }
174 1.1 christos addrlen = strlen(addrbuf);
175 1.1 christos if (addrlen + sizeof("/128") < sizeof(addrbuf)) {
176 1.1 christos snprintf(&addrbuf[addrlen],
177 1.1 christos sizeof(addrbuf) - addrlen - 1,
178 1.1 christos "/%d", plen);
179 1.1 christos printf("%-30s", addrbuf);
180 1.1 christos } else /* XXX */
181 1.1 christos printf("%s/%d", addrbuf, plen);
182 1.1 christos printf(" %5d %5d %8llu\n", pol->preced, pol->label,
183 1.1 christos (unsigned long long)pol->use);
184 1.1 christos }
185 1.1 christos }
186 1.1 christos
187 1.1 christos #define SKIP_WHITE(p, emptyok) \
188 1.1 christos do { \
189 1.1 christos while((*(p) == ' ' || *(p) == '\t')) \
190 1.1 christos (p)++; \
191 1.1 christos if ((*(p) == '\0' || (*(p) == '\n')) && !(emptyok)) \
192 1.1 christos goto bad; \
193 1.1 christos } while (0);
194 1.1 christos #define SKIP_WORD(p) \
195 1.1 christos do { \
196 1.1 christos while(*(p) != ' ' && *(p) != '\t') \
197 1.1 christos (p)++; \
198 1.1 christos if (*(p) == '\0' || *(p) == '\n') \
199 1.1 christos goto bad; \
200 1.1 christos } while (0);
201 1.1 christos
202 1.1 christos static void
203 1.1 christos make_policy_fromfile(char *conf)
204 1.1 christos {
205 1.1 christos char line[_POSIX2_LINE_MAX], *cp;
206 1.1 christos char *addrstr;
207 1.1 christos FILE *fp;
208 1.1 christos int count = 0;
209 1.1 christos struct in6_addrpolicy pol0;
210 1.1 christos struct policyqueue *new;
211 1.1 christos
212 1.1 christos if ((fp = fopen(conf, "r")) == NULL)
213 1.1 christos err(1, "fopen: %s", conf);
214 1.1 christos
215 1.1 christos while(fgets(line, sizeof(line), fp)) {
216 1.1 christos count++;
217 1.1 christos cp = line;
218 1.1 christos
219 1.1 christos memset(&pol0, 0, sizeof(pol0));
220 1.1 christos
221 1.1 christos /* get prefix */
222 1.1 christos SKIP_WHITE(cp, 1);
223 1.1 christos if (*cp == '\n') /* empty line */
224 1.1 christos continue;
225 1.1 christos if (*cp == '#')
226 1.1 christos continue;
227 1.1 christos addrstr = cp;
228 1.1 christos if (parse_prefix((const char *)addrstr, &pol0))
229 1.1 christos goto bad;
230 1.1 christos
231 1.1 christos /* get precedence value */
232 1.1 christos SKIP_WORD(cp);
233 1.1 christos SKIP_WHITE(cp, 0);
234 1.1 christos pol0.preced = atoi(cp);
235 1.1 christos
236 1.1 christos /* get label */
237 1.1 christos SKIP_WORD(cp);
238 1.1 christos SKIP_WHITE(cp, 0);
239 1.1 christos pol0.label = atoi(cp);
240 1.1 christos
241 1.1 christos /* parse succeeded. make a control buffer entry. */
242 1.1 christos if ((new = malloc(sizeof(*new))) == NULL)
243 1.1 christos errx(1, "malloc failed\n");
244 1.1 christos memset(new, 0, sizeof(*new));
245 1.1 christos new->pc_policy = pol0;
246 1.1 christos TAILQ_INSERT_TAIL(&policyhead, new, pc_entry);
247 1.1 christos }
248 1.1 christos
249 1.1 christos fclose(fp);
250 1.1 christos return;
251 1.1 christos
252 1.1 christos bad:
253 1.1 christos errx(1, "parse failed at line %d", count);
254 1.1 christos /* NOTREACHED */
255 1.1 christos }
256 1.1 christos
257 1.1 christos static int
258 1.1 christos parse_prefix(const char *prefix0, struct in6_addrpolicy *pol)
259 1.1 christos {
260 1.1 christos int e = 0, plen;
261 1.1 christos char *prefix, *plenstr;
262 1.1 christos struct addrinfo hints, *res;
263 1.1 christos
264 1.1 christos if ((prefix = strdup(prefix0)) == NULL)
265 1.1 christos errx(1, "strdup failed");
266 1.1 christos
267 1.1 christos if ((plenstr = strchr(prefix, '/')) == NULL) {
268 1.1 christos e = -1;
269 1.1 christos goto end;
270 1.1 christos }
271 1.1 christos *plenstr = '\0';
272 1.1 christos
273 1.1 christos memset(&hints, 0, sizeof(hints));
274 1.1 christos hints.ai_flags = AI_NUMERICHOST;
275 1.1 christos hints.ai_family = AF_INET6;
276 1.1 christos
277 1.1 christos if ((e = getaddrinfo(prefix, NULL, &hints, &res)) != 0) {
278 1.1 christos warnx("getaddrinfo failed for %s: %s", prefix,
279 1.1 christos gai_strerror(e));
280 1.1 christos goto end;
281 1.1 christos }
282 1.1 christos memcpy(&pol->addr, res->ai_addr, res->ai_addrlen);
283 1.1 christos freeaddrinfo(res);
284 1.1 christos plen = atoi(plenstr + 1);
285 1.1 christos if (plen < 0 || plen > 128) {
286 1.1 christos warnx("invalid prefix length: %d", plen);
287 1.1 christos e = -1;
288 1.1 christos goto end;
289 1.1 christos }
290 1.1 christos plen2mask(&pol->addrmask, plen);
291 1.1 christos
292 1.1 christos end:
293 1.1 christos free(prefix);
294 1.1 christos return(e);
295 1.1 christos }
296 1.1 christos
297 1.1 christos static void
298 1.1 christos plen2mask(struct sockaddr_in6 *mask, int plen)
299 1.1 christos {
300 1.1 christos u_char *cp = (unsigned char *)&mask->sin6_addr;
301 1.1 christos
302 1.1 christos memset(mask, 0, sizeof(*mask));
303 1.1 christos mask->sin6_family = AF_INET6; /* just in case */
304 1.1 christos mask->sin6_len = sizeof(*mask);
305 1.1 christos
306 1.1 christos for(; plen >= 8; plen -= 8)
307 1.1 christos *cp++ = 0xff;
308 1.1 christos if (plen > 0)
309 1.1 christos *cp = (0xff << (8 - plen));
310 1.1 christos }
311 1.1 christos
312 1.1 christos static void
313 1.1 christos set_policy(void)
314 1.1 christos {
315 1.1 christos struct policyqueue *ent;
316 1.1 christos int s;
317 1.1 christos
318 1.1 christos if ((s = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP)) < 0)
319 1.1 christos err(1, "socket(UDP)");
320 1.1 christos
321 1.1 christos for (ent = TAILQ_FIRST(&policyhead); ent;
322 1.1 christos ent = TAILQ_NEXT(ent, pc_entry)) {
323 1.1 christos if (ioctl(s, SIOCAADDRCTL_POLICY, &ent->pc_policy))
324 1.1 christos warn("ioctl(SIOCAADDRCTL_POLICY)");
325 1.1 christos }
326 1.1 christos
327 1.1 christos close(s);
328 1.1 christos }
329 1.1 christos
330 1.1 christos static int
331 1.1 christos mask2plen(struct sockaddr_in6 *mask)
332 1.1 christos {
333 1.1 christos int masklen, final = 0;
334 1.1 christos u_char *p, *lim;
335 1.1 christos
336 1.1 christos masklen = 0;
337 1.1 christos lim = (u_char *)(mask + 1);
338 1.1 christos for (p = (u_char *)(&mask->sin6_addr); p < lim; p++) {
339 1.1 christos if (final && *p) {
340 1.1 christos goto bad;
341 1.1 christos }
342 1.1 christos
343 1.1 christos switch (*p & 0xff) {
344 1.1 christos case 0xff:
345 1.1 christos masklen += 8;
346 1.1 christos break;
347 1.1 christos case 0xfe:
348 1.1 christos masklen += 7;
349 1.1 christos final++;
350 1.1 christos break;
351 1.1 christos case 0xfc:
352 1.1 christos masklen += 6;
353 1.1 christos final++;
354 1.1 christos break;
355 1.1 christos case 0xf8:
356 1.1 christos masklen += 5;
357 1.1 christos final++;
358 1.1 christos break;
359 1.1 christos case 0xf0:
360 1.1 christos masklen += 4;
361 1.1 christos final++;
362 1.1 christos break;
363 1.1 christos case 0xe0:
364 1.1 christos masklen += 3;
365 1.1 christos final++;
366 1.1 christos break;
367 1.1 christos case 0xc0:
368 1.1 christos masklen += 2;
369 1.1 christos final++;
370 1.1 christos break;
371 1.1 christos case 0x80:
372 1.1 christos masklen += 1;
373 1.1 christos final++;
374 1.1 christos break;
375 1.1 christos case 0x00:
376 1.1 christos final++;
377 1.1 christos break;
378 1.1 christos default:
379 1.1 christos goto bad;
380 1.1 christos break;
381 1.1 christos }
382 1.1 christos }
383 1.1 christos return(masklen);
384 1.1 christos
385 1.1 christos bad:
386 1.1 christos return(-1);
387 1.1 christos }
388 1.1 christos
389 1.1 christos static void
390 1.1 christos add_policy(char *prefix, char *prec, char *label)
391 1.1 christos {
392 1.1 christos struct in6_addrpolicy p;
393 1.1 christos int s;
394 1.1 christos
395 1.1 christos memset(&p, 0, sizeof(p));
396 1.1 christos
397 1.1 christos if (parse_prefix((const char *)prefix, &p))
398 1.1 christos errx(1, "bad prefix: %s", prefix);
399 1.1 christos p.preced = atoi(prec);
400 1.1 christos p.label = atoi(label);
401 1.1 christos
402 1.1 christos if ((s = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP)) < 0)
403 1.1 christos err(1, "socket(UDP)");
404 1.1 christos if (ioctl(s, SIOCAADDRCTL_POLICY, &p))
405 1.1 christos err(1, "ioctl(SIOCAADDRCTL_POLICY)");
406 1.1 christos
407 1.1 christos close(s);
408 1.1 christos }
409 1.1 christos
410 1.1 christos static void
411 1.1 christos delete_policy(char *prefix)
412 1.1 christos {
413 1.1 christos struct in6_addrpolicy p;
414 1.1 christos int s;
415 1.1 christos
416 1.1 christos memset(&p, 0, sizeof(p));
417 1.1 christos
418 1.1 christos if (parse_prefix((const char *)prefix, &p))
419 1.1 christos errx(1, "bad prefix: %s", prefix);
420 1.1 christos
421 1.1 christos if ((s = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP)) < 0)
422 1.1 christos err(1, "socket(UDP)");
423 1.1 christos if (ioctl(s, SIOCDADDRCTL_POLICY, &p))
424 1.1 christos err(1, "ioctl(SIOCDADDRCTL_POLICY)");
425 1.1 christos
426 1.1 christos close(s);
427 1.1 christos }
428 1.1 christos
429 1.1 christos static void
430 1.1 christos flush_policy(void)
431 1.1 christos {
432 1.1 christos struct policyqueue *ent;
433 1.1 christos int s;
434 1.1 christos
435 1.1 christos if ((s = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP)) < 0)
436 1.1 christos err(1, "socket(UDP)");
437 1.1 christos
438 1.1 christos for (ent = TAILQ_FIRST(&policyhead); ent;
439 1.1 christos ent = TAILQ_NEXT(ent, pc_entry)) {
440 1.1 christos if (ioctl(s, SIOCDADDRCTL_POLICY, &ent->pc_policy))
441 1.1 christos warn("ioctl(SIOCDADDRCTL_POLICY)");
442 1.1 christos }
443 1.1 christos
444 1.1 christos close(s);
445 1.1 christos }
446 1.1 christos
447 1.1 christos static void
448 1.1 christos usage(void)
449 1.1 christos {
450 1.1 christos fprintf(stderr, "usage: ip6addrctl [show]\n");
451 1.1 christos fprintf(stderr, " ip6addrctl add "
452 1.1 christos "<prefix> <precedence> <label>\n");
453 1.1 christos fprintf(stderr, " ip6addrctl delete <prefix>\n");
454 1.1 christos fprintf(stderr, " ip6addrctl flush\n");
455 1.1 christos fprintf(stderr, " ip6addrctl install <configfile>\n");
456 1.1 christos
457 1.1 christos exit(1);
458 1.1 christos }
459