Sat Oct 19 00:39:39 2013 UTC ()
use scopeid functions


(christos)
diff -r1.141 -r1.142 src/sbin/route/route.c

cvs diff -r1.141 -r1.142 src/sbin/route/route.c (expand / switch to unified diff)

--- src/sbin/route/route.c 2013/10/18 21:01:00 1.141
+++ src/sbin/route/route.c 2013/10/19 00:39:39 1.142
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: route.c,v 1.141 2013/10/18 21:01:00 christos Exp $ */ 1/* $NetBSD: route.c,v 1.142 2013/10/19 00:39:39 christos Exp $ */
2 2
3/* 3/*
4 * Copyright (c) 1983, 1989, 1991, 1993 4 * Copyright (c) 1983, 1989, 1991, 1993
5 * The Regents of the University of California. All rights reserved. 5 * The Regents of the University of California. All rights reserved.
6 * 6 *
7 * Redistribution and use in source and binary forms, with or without 7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions 8 * modification, are permitted provided that the following conditions
9 * are met: 9 * are met:
10 * 1. Redistributions of source code must retain the above copyright 10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer. 11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright 12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the 13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution. 14 * documentation and/or other materials provided with the distribution.
@@ -29,27 +29,27 @@ @@ -29,27 +29,27 @@
29 * SUCH DAMAGE. 29 * SUCH DAMAGE.
30 */ 30 */
31 31
32#include <sys/cdefs.h> 32#include <sys/cdefs.h>
33#ifndef lint 33#ifndef lint
34__COPYRIGHT("@(#) Copyright (c) 1983, 1989, 1991, 1993\ 34__COPYRIGHT("@(#) Copyright (c) 1983, 1989, 1991, 1993\
35 The Regents of the University of California. All rights reserved."); 35 The Regents of the University of California. All rights reserved.");
36#endif /* not lint */ 36#endif /* not lint */
37 37
38#ifndef lint 38#ifndef lint
39#if 0 39#if 0
40static char sccsid[] = "@(#)route.c 8.6 (Berkeley) 4/28/95"; 40static char sccsid[] = "@(#)route.c 8.6 (Berkeley) 4/28/95";
41#else 41#else
42__RCSID("$NetBSD: route.c,v 1.141 2013/10/18 21:01:00 christos Exp $"); 42__RCSID("$NetBSD: route.c,v 1.142 2013/10/19 00:39:39 christos Exp $");
43#endif 43#endif
44#endif /* not lint */ 44#endif /* not lint */
45 45
46#include <sys/param.h> 46#include <sys/param.h>
47#include <sys/file.h> 47#include <sys/file.h>
48#include <sys/socket.h> 48#include <sys/socket.h>
49#include <sys/ioctl.h> 49#include <sys/ioctl.h>
50#include <sys/mbuf.h> 50#include <sys/mbuf.h>
51#include <sys/sysctl.h> 51#include <sys/sysctl.h>
52 52
53#include <net/if.h> 53#include <net/if.h>
54#include <net/route.h> 54#include <net/route.h>
55#include <net/if_dl.h> 55#include <net/if_dl.h>
@@ -528,39 +528,27 @@ routename(const struct sockaddr *sa, str @@ -528,39 +528,27 @@ routename(const struct sockaddr *sa, str
528 case AF_INET6: 528 case AF_INET6:
529 { 529 {
530 struct sockaddr_in6 sin6; 530 struct sockaddr_in6 sin6;
531 int niflags; 531 int niflags;
532 char nihost[NI_MAXHOST]; 532 char nihost[NI_MAXHOST];
533 533
534 niflags = 0; 534 niflags = 0;
535 if (nflag) 535 if (nflag)
536 niflags |= NI_NUMERICHOST; 536 niflags |= NI_NUMERICHOST;
537 memset(&sin6, 0, sizeof(sin6)); 537 memset(&sin6, 0, sizeof(sin6));
538 memcpy(&sin6, sa, sa->sa_len); 538 memcpy(&sin6, sa, sa->sa_len);
539 sin6.sin6_len = sizeof(struct sockaddr_in6); 539 sin6.sin6_len = sizeof(struct sockaddr_in6);
540 sin6.sin6_family = AF_INET6; 540 sin6.sin6_family = AF_INET6;
541#ifdef __KAME__ 541 inet6_getscopeid(&sin6, 3);
542 if (sa->sa_len == sizeof(struct sockaddr_in6) && 
543 (IN6_IS_ADDR_LINKLOCAL(&sin6.sin6_addr) || 
544 IN6_IS_ADDR_MC_LINKLOCAL(&sin6.sin6_addr)) && 
545 sin6.sin6_scope_id == 0) { 
546 uint16_t scope; 
547 memcpy(&scope, &sin6.sin6_addr.s6_addr[2], 
548 sizeof(scope)); 
549 sin6.sin6_scope_id = ntohs(scope); 
550 sin6.sin6_addr.s6_addr[2] = 0; 
551 sin6.sin6_addr.s6_addr[3] = 0; 
552 } 
553#endif 
554 nml = netmask_length(nm, AF_INET6); 542 nml = netmask_length(nm, AF_INET6);
555 if (IN6_IS_ADDR_UNSPECIFIED(&sin6.sin6_addr)) { 543 if (IN6_IS_ADDR_UNSPECIFIED(&sin6.sin6_addr)) {
556 if (nml == 0) 544 if (nml == 0)
557 strlcpy(line, "::", sizeof(line)); 545 strlcpy(line, "::", sizeof(line));
558 else 546 else
559 /* noncontiguous never happens in ipv6 */ 547 /* noncontiguous never happens in ipv6 */
560 snprintf(line, sizeof(line), "::/%d", nml); 548 snprintf(line, sizeof(line), "::/%d", nml);
561 } 549 }
562 else if (getnameinfo((struct sockaddr *)&sin6, sin6.sin6_len, 550 else if (getnameinfo((struct sockaddr *)&sin6, sin6.sin6_len,
563 nihost, sizeof(nihost), NULL, 0, niflags) != 0) 551 nihost, sizeof(nihost), NULL, 0, niflags) != 0)
564 strlcpy(line, "invalid", sizeof(line)); 552 strlcpy(line, "invalid", sizeof(line));
565 else { 553 else {
566 char *ccp; 554 char *ccp;
@@ -697,39 +685,27 @@ netname(const struct sockaddr *sa, struc @@ -697,39 +685,27 @@ netname(const struct sockaddr *sa, struc
697#ifdef INET6 685#ifdef INET6
698 case AF_INET6: 686 case AF_INET6:
699 { 687 {
700 struct sockaddr_in6 sin6; 688 struct sockaddr_in6 sin6;
701 int niflags; 689 int niflags;
702 690
703 niflags = 0; 691 niflags = 0;
704 if (nflag) 692 if (nflag)
705 niflags |= NI_NUMERICHOST; 693 niflags |= NI_NUMERICHOST;
706 memset(&sin6, 0, sizeof(sin6)); 694 memset(&sin6, 0, sizeof(sin6));
707 memcpy(&sin6, sa, sa->sa_len); 695 memcpy(&sin6, sa, sa->sa_len);
708 sin6.sin6_len = sizeof(struct sockaddr_in6); 696 sin6.sin6_len = sizeof(struct sockaddr_in6);
709 sin6.sin6_family = AF_INET6; 697 sin6.sin6_family = AF_INET6;
710#ifdef __KAME__ 698 inet6_putscopeid(&sin6, 3);
711 if (sa->sa_len == sizeof(struct sockaddr_in6) && 
712 (IN6_IS_ADDR_LINKLOCAL(&sin6.sin6_addr) || 
713 IN6_IS_ADDR_MC_LINKLOCAL(&sin6.sin6_addr)) && 
714 sin6.sin6_scope_id == 0) { 
715 uint16_t scope; 
716 memcpy(&scope, &sin6.sin6_addr.s6_addr[2], 
717 sizeof(scope)); 
718 sin6.sin6_scope_id = ntohs(scope); 
719 sin6.sin6_addr.s6_addr[2] = 0; 
720 sin6.sin6_addr.s6_addr[3] = 0; 
721 } 
722#endif 
723 nml = netmask_length(nm, AF_INET6); 699 nml = netmask_length(nm, AF_INET6);
724 if (IN6_IS_ADDR_UNSPECIFIED(&sin6.sin6_addr)) { 700 if (IN6_IS_ADDR_UNSPECIFIED(&sin6.sin6_addr)) {
725 if (nml == 0) 701 if (nml == 0)
726 strlcpy(line, "::", sizeof(line)); 702 strlcpy(line, "::", sizeof(line));
727 else 703 else
728 /* noncontiguous never happens in ipv6 */ 704 /* noncontiguous never happens in ipv6 */
729 snprintf(line, sizeof(line), "::/%d", nml); 705 snprintf(line, sizeof(line), "::/%d", nml);
730 } 706 }
731 else if (getnameinfo((struct sockaddr *)&sin6, sin6.sin6_len, 707 else if (getnameinfo((struct sockaddr *)&sin6, sin6.sin6_len,
732 line, sizeof(line), NULL, 0, niflags) != 0) 708 line, sizeof(line), NULL, 0, niflags) != 0)
733 strlcpy(line, "invalid", sizeof(line)); 709 strlcpy(line, "invalid", sizeof(line));
734 break; 710 break;
735 } 711 }
@@ -1249,37 +1225,27 @@ getaddr(int which, const char *s, struct @@ -1249,37 +1225,27 @@ getaddr(int which, const char *s, struct
1249 if (getaddrinfo(s, "0", &hints, &res) != 0) 1225 if (getaddrinfo(s, "0", &hints, &res) != 0)
1250 errx(EXIT_FAILURE, "%s: bad value", s); 1226 errx(EXIT_FAILURE, "%s: bad value", s);
1251 } 1227 }
1252 if (slash) 1228 if (slash)
1253 *slash = '/'; 1229 *slash = '/';
1254 if (sizeof(su->sin6) != res->ai_addrlen) 1230 if (sizeof(su->sin6) != res->ai_addrlen)
1255 errx(EXIT_FAILURE, "%s: bad value", s); 1231 errx(EXIT_FAILURE, "%s: bad value", s);
1256 if (res->ai_next) { 1232 if (res->ai_next) {
1257 errx(EXIT_FAILURE, 1233 errx(EXIT_FAILURE,
1258 "%s: address resolved to multiple values", s); 1234 "%s: address resolved to multiple values", s);
1259 } 1235 }
1260 memcpy(&su->sin6, res->ai_addr, sizeof(su->sin6)); 1236 memcpy(&su->sin6, res->ai_addr, sizeof(su->sin6));
1261 freeaddrinfo(res); 1237 freeaddrinfo(res);
1262#ifdef __KAME__ 1238 inet6_getscopeid(&su->sin6, 3);
1263 if ((IN6_IS_ADDR_LINKLOCAL(&su->sin6.sin6_addr) || 
1264 IN6_IS_ADDR_MC_LINKLOCAL(&su->sin6.sin6_addr)) && 
1265 su->sin6.sin6_scope_id) { 
1266 uint16_t scope; 
1267 scope = htons(su->sin6.sin6_scope_id); 
1268 memcpy(&su->sin6.sin6_addr.s6_addr[2], &scope, 
1269 sizeof(scope)); 
1270 su->sin6.sin6_scope_id = 0; 
1271 } 
1272#endif 
1273 if (hints.ai_flags == AI_NUMERICHOST) { 1239 if (hints.ai_flags == AI_NUMERICHOST) {
1274 if (slash) 1240 if (slash)
1275 return prefixlen(slash + 1, soup); 1241 return prefixlen(slash + 1, soup);
1276 if (which == RTA_DST) 1242 if (which == RTA_DST)
1277 return inet6_makenetandmask(&su->sin6, soup); 1243 return inet6_makenetandmask(&su->sin6, soup);
1278 return 0; 1244 return 0;
1279 } else 1245 } else
1280 return 1; 1246 return 1;
1281 } 1247 }
1282#endif 1248#endif
1283 1249
1284 case PF_ROUTE: 1250 case PF_ROUTE:
1285 su->sa.sa_len = sizeof(*su); 1251 su->sa.sa_len = sizeof(*su);