Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/freebsd-src
Path: blob/main/sbin/ifconfig/af_nd6.c
39475 views
1
/*-
2
* SPDX-License-Identifier: BSD-2-Clause
3
*
4
* Copyright (c) 2009 Hiroki Sato. All rights reserved.
5
*
6
* Redistribution and use in source and binary forms, with or without
7
* modification, are permitted provided that the following conditions
8
* are met:
9
* 1. Redistributions of source code must retain the above copyright
10
* notice, this list of conditions and the following disclaimer.
11
* 2. Redistributions in binary form must reproduce the above copyright
12
* notice, this list of conditions and the following disclaimer in the
13
* documentation and/or other materials provided with the distribution.
14
*
15
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
16
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
19
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25
* SUCH DAMAGE.
26
*/
27
28
#include <sys/param.h>
29
#include <sys/ioctl.h>
30
#include <sys/socket.h>
31
#include <sys/sysctl.h>
32
#include <net/if.h>
33
#include <net/route.h>
34
35
#include <err.h>
36
#include <errno.h>
37
#include <stdio.h>
38
#include <stdlib.h>
39
#include <string.h>
40
#include <unistd.h>
41
#include <ifaddrs.h>
42
43
#include <arpa/inet.h>
44
45
#include <netinet/in.h>
46
#include <netinet/in_var.h>
47
#include <arpa/inet.h>
48
#include <netdb.h>
49
50
#include <netinet6/nd6.h>
51
52
#include "ifconfig.h"
53
54
#define MAX_SYSCTL_TRY 5
55
static const char *ND6BITS[] = {
56
[0] = "PERFORMNUD",
57
[1] = "ACCEPT_RTADV",
58
[2] = "PREFER_SOURCE",
59
[3] = "IFDISABLED",
60
[4] = "DONT_SET_IFROUTE",
61
[5] = "AUTO_LINKLOCAL",
62
[6] = "NO_RADR",
63
[7] = "NO_PREFER_IFACE",
64
[8] = "NO_DAD",
65
#ifdef DRAFT_IETF_6MAN_IPV6ONLY_FLAG
66
[9] = "IPV6_ONLY",
67
[10] = "IPV6_ONLY_MANUAL",
68
#endif
69
[11] = "STABLEADDR",
70
[15] = "DEFAULTIF",
71
};
72
73
static int isnd6defif(if_ctx *ctx, int s);
74
void setnd6flags(if_ctx *, const char *, int);
75
void setnd6defif(if_ctx *,const char *, int);
76
void nd6_status(if_ctx *);
77
78
void
79
setnd6flags(if_ctx *ctx, const char *dummyaddr __unused, int d)
80
{
81
struct in6_ndireq nd = {};
82
int error;
83
84
strlcpy(nd.ifname, ctx->ifname, sizeof(nd.ifname));
85
error = ioctl_ctx(ctx, SIOCGIFINFO_IN6, &nd);
86
if (error) {
87
warn("ioctl(SIOCGIFINFO_IN6)");
88
return;
89
}
90
if (d < 0)
91
nd.ndi.flags &= ~(-d);
92
else
93
nd.ndi.flags |= d;
94
error = ioctl_ctx(ctx, SIOCSIFINFO_IN6, (caddr_t)&nd);
95
if (error)
96
warn("ioctl(SIOCSIFINFO_IN6)");
97
}
98
99
void
100
setnd6defif(if_ctx *ctx, const char *dummyaddr __unused, int d)
101
{
102
struct in6_ndifreq ndifreq = {};
103
int ifindex;
104
int error;
105
106
strlcpy(ndifreq.ifname, ctx->ifname, sizeof(ndifreq.ifname));
107
108
if (d < 0) {
109
if (isnd6defif(ctx, ctx->io_s)) {
110
/* ifindex = 0 means to remove default if */
111
ifindex = 0;
112
} else
113
return;
114
} else if ((ifindex = if_nametoindex(ndifreq.ifname)) == 0) {
115
warn("if_nametoindex(%s)", ndifreq.ifname);
116
return;
117
}
118
119
ndifreq.ifindex = ifindex;
120
error = ioctl_ctx(ctx, SIOCSDEFIFACE_IN6, (caddr_t)&ndifreq);
121
if (error)
122
warn("ioctl(SIOCSDEFIFACE_IN6)");
123
}
124
125
static int
126
isnd6defif(if_ctx *ctx, int s)
127
{
128
struct in6_ndifreq ndifreq = {};
129
unsigned int ifindex;
130
int error;
131
132
strlcpy(ndifreq.ifname, ctx->ifname, sizeof(ndifreq.ifname));
133
134
ifindex = if_nametoindex(ndifreq.ifname);
135
error = ioctl(s, SIOCGDEFIFACE_IN6, (caddr_t)&ndifreq);
136
if (error) {
137
warn("ioctl(SIOCGDEFIFACE_IN6)");
138
return (error);
139
}
140
return (ndifreq.ifindex == ifindex);
141
}
142
143
void
144
nd6_status(if_ctx *ctx)
145
{
146
struct in6_ndireq nd = {};
147
int s6;
148
int error;
149
int isdefif;
150
uint32_t bits;
151
152
strlcpy(nd.ifname, ctx->ifname, sizeof(nd.ifname));
153
if ((s6 = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
154
if (errno != EAFNOSUPPORT && errno != EPROTONOSUPPORT)
155
warn("socket(AF_INET6, SOCK_DGRAM)");
156
return;
157
}
158
error = ioctl(s6, SIOCGIFINFO_IN6, &nd);
159
if (error) {
160
if (errno != EPFNOSUPPORT)
161
warn("ioctl(SIOCGIFINFO_IN6)");
162
close(s6);
163
return;
164
}
165
isdefif = isnd6defif(ctx, s6);
166
close(s6);
167
if (nd.ndi.flags == 0 && !isdefif)
168
return;
169
bits = (nd.ndi.flags | (isdefif << 15));
170
printf("\tnd6 options=%x", bits);
171
print_bits("options", &bits, 1, ND6BITS, nitems(ND6BITS));
172
putchar('\n');
173
}
174
175