1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2018 The FreeBSD Foundation
5 *
6 * This software was developed by Mark Johnston under sponsorship from
7 * the FreeBSD Foundation.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions are
11 * met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the distribution.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
31 #include <sys/types.h>
32 #include <sys/dnv.h>
33 #include <sys/ioctl.h>
34 #include <sys/nv.h>
35 #include <sys/queue.h>
36
37 #include <net/if.h>
38 #include <netinet/in.h>
39 #include <netinet6/in6_var.h>
40
41 #include <errno.h>
42 #include <ifaddrs.h>
43 #include <string.h>
44 #include <unistd.h>
45
46 #include <libcasper.h>
47 #include <libcasper_service.h>
48
49 #include "rtsold.h"
50
51 /*
52 * A service to fetch the flags for the link-local IPv6 address on the specified
53 * interface. This cannot easily be done in capability mode because we need to
54 * use the routing socket sysctl API to find the link-local address of a
55 * particular interface. The SIOCGIFCONF ioctl is one other option, but as
56 * currently implemented it is less flexible (it cannot report the required
57 * buffer length), and hard-codes a buffer length limit.
58 */
59
60 static int
llflags_get(const char * ifname,int * flagsp)61 llflags_get(const char *ifname, int *flagsp)
62 {
63 struct in6_ifreq ifr6;
64 struct ifaddrs *ifap, *ifa;
65 struct sockaddr_in6 *sin6;
66 int error, s;
67
68 s = socket(PF_INET6, SOCK_DGRAM, 0);
69 if (s < 0)
70 return (-1);
71
72 ifap = NULL;
73 if (getifaddrs(&ifap) != 0) {
74 error = errno;
75 goto out;
76 }
77 error = ENOENT;
78 for (ifa = ifap; ifa != NULL; ifa = ifa->ifa_next) {
79 if (strcmp(ifa->ifa_name, ifname) != 0)
80 continue;
81 if (ifa->ifa_addr->sa_family != AF_INET6)
82 continue;
83
84 sin6 = (struct sockaddr_in6 *)(void *)ifa->ifa_addr;
85 if (!IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr))
86 continue;
87
88 memset(&ifr6, 0, sizeof(ifr6));
89 if (strlcpy(ifr6.ifr_name, ifname, sizeof(ifr6.ifr_name)) >=
90 sizeof(ifr6.ifr_name)) {
91 error = EINVAL;
92 goto out;
93 }
94 memcpy(&ifr6.ifr_ifru.ifru_addr, sin6, sin6->sin6_len);
95 if (ioctl(s, SIOCGIFAFLAG_IN6, &ifr6) < 0) {
96 error = errno;
97 goto out;
98 }
99
100 *flagsp = ifr6.ifr_ifru.ifru_flags6;
101 error = 0;
102 break;
103 }
104 out:
105 (void)close(s);
106 if (ifap != NULL)
107 freeifaddrs(ifap);
108 if (error != 0) {
109 errno = error;
110 return (-1);
111 } else {
112 return (0);
113 }
114 }
115
116 int
cap_llflags_get(cap_channel_t * cap,const char * ifname,int * flagsp)117 cap_llflags_get(cap_channel_t *cap, const char *ifname, int *flagsp)
118 {
119 #ifdef WITH_CASPER
120 nvlist_t *nvl;
121 int error;
122
123 nvl = nvlist_create(0);
124 nvlist_add_string(nvl, "cmd", "get");
125 nvlist_add_string(nvl, "ifname", ifname);
126 nvl = cap_xfer_nvlist(cap, nvl);
127 if (nvl == NULL)
128 return (-1);
129 error = (int)dnvlist_get_number(nvl, "error", 0);
130 if (error == 0)
131 *flagsp = (int)nvlist_get_number(nvl, "flags");
132 nvlist_destroy(nvl);
133 if (error != 0)
134 errno = error;
135 return (error == 0 ? 0 : -1);
136 #else
137 (void)cap;
138 return (llflags_get(ifname, flagsp));
139 #endif
140 }
141
142 #ifdef WITH_CASPER
143 static int
llflags_command(const char * cmd,const nvlist_t * limits __unused,nvlist_t * nvlin,nvlist_t * nvlout)144 llflags_command(const char *cmd, const nvlist_t *limits __unused,
145 nvlist_t *nvlin, nvlist_t *nvlout)
146 {
147 const char *ifname;
148 int flags;
149
150 if (strcmp(cmd, "get") != 0)
151 return (EINVAL);
152 ifname = nvlist_get_string(nvlin, "ifname");
153 if (llflags_get(ifname, &flags) != 0)
154 return (errno);
155 nvlist_add_number(nvlout, "flags", flags);
156 return (0);
157 }
158
159 CREATE_SERVICE("rtsold.llflags", NULL, llflags_command, 0);
160 #endif /* WITH_CASPER */
161