af_link.c revision 1.2.4.3 1 1.2.4.2 mjf /* $NetBSD: af_link.c,v 1.2.4.3 2008/09/28 11:17:11 mjf Exp $ */
2 1.2.4.2 mjf
3 1.2.4.2 mjf /*-
4 1.2.4.2 mjf * Copyright (c) 2008 David Young. All rights reserved.
5 1.2.4.2 mjf *
6 1.2.4.2 mjf * Redistribution and use in source and binary forms, with or without
7 1.2.4.2 mjf * modification, are permitted provided that the following conditions
8 1.2.4.2 mjf * are met:
9 1.2.4.2 mjf * 1. Redistributions of source code must retain the above copyright
10 1.2.4.2 mjf * notice, this list of conditions and the following disclaimer.
11 1.2.4.2 mjf * 2. Redistributions in binary form must reproduce the above copyright
12 1.2.4.2 mjf * notice, this list of conditions and the following disclaimer in the
13 1.2.4.2 mjf * documentation and/or other materials provided with the distribution.
14 1.2.4.2 mjf *
15 1.2.4.2 mjf * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 1.2.4.2 mjf * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 1.2.4.2 mjf * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 1.2.4.2 mjf * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 1.2.4.2 mjf * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 1.2.4.2 mjf * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 1.2.4.2 mjf * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 1.2.4.2 mjf * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 1.2.4.2 mjf * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 1.2.4.2 mjf * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 1.2.4.2 mjf * SUCH DAMAGE.
26 1.2.4.2 mjf */
27 1.2.4.2 mjf
28 1.2.4.2 mjf #include <sys/cdefs.h>
29 1.2.4.2 mjf #ifndef lint
30 1.2.4.2 mjf __RCSID("$NetBSD: af_link.c,v 1.2.4.3 2008/09/28 11:17:11 mjf Exp $");
31 1.2.4.2 mjf #endif /* not lint */
32 1.2.4.2 mjf
33 1.2.4.2 mjf #include <sys/param.h>
34 1.2.4.2 mjf #include <sys/ioctl.h>
35 1.2.4.2 mjf #include <sys/socket.h>
36 1.2.4.2 mjf
37 1.2.4.2 mjf #include <net/if.h>
38 1.2.4.2 mjf #include <net/if_dl.h>
39 1.2.4.2 mjf
40 1.2.4.2 mjf #include <assert.h>
41 1.2.4.2 mjf #include <err.h>
42 1.2.4.2 mjf #include <errno.h>
43 1.2.4.2 mjf #include <ifaddrs.h>
44 1.2.4.2 mjf #include <netdb.h>
45 1.2.4.2 mjf #include <string.h>
46 1.2.4.2 mjf #include <stdlib.h>
47 1.2.4.2 mjf #include <stdio.h>
48 1.2.4.2 mjf #include <util.h>
49 1.2.4.2 mjf
50 1.2.4.2 mjf #include "env.h"
51 1.2.4.2 mjf #include "extern.h"
52 1.2.4.2 mjf #include "af_inetany.h"
53 1.2.4.2 mjf
54 1.2.4.3 mjf static void link_status(prop_dictionary_t, prop_dictionary_t, bool);
55 1.2.4.3 mjf static void link_commit_address(prop_dictionary_t, prop_dictionary_t);
56 1.2.4.3 mjf
57 1.2.4.3 mjf static const struct kwinst linkkw[] = {
58 1.2.4.3 mjf {.k_word = "active", .k_key = "active", .k_type = KW_T_BOOL,
59 1.2.4.3 mjf .k_bool = true, .k_nextparser = &command_root.pb_parser}
60 1.2.4.3 mjf };
61 1.2.4.3 mjf
62 1.2.4.3 mjf struct pkw link = PKW_INITIALIZER(&link, "link", NULL, NULL,
63 1.2.4.3 mjf linkkw, __arraycount(linkkw), NULL);
64 1.2.4.3 mjf
65 1.2.4.3 mjf static struct afswtch af = {
66 1.2.4.3 mjf .af_name = "link", .af_af = AF_LINK, .af_status = link_status,
67 1.2.4.3 mjf .af_addr_commit = link_commit_address
68 1.2.4.3 mjf };
69 1.2.4.3 mjf
70 1.2.4.3 mjf static cmdloop_branch_t branch;
71 1.2.4.3 mjf
72 1.2.4.3 mjf static void link_constructor(void) __attribute__((constructor));
73 1.2.4.3 mjf
74 1.2.4.3 mjf static void
75 1.2.4.2 mjf link_status(prop_dictionary_t env, prop_dictionary_t oenv, bool force)
76 1.2.4.2 mjf {
77 1.2.4.2 mjf const char *delim, *ifname;
78 1.2.4.2 mjf int i, s;
79 1.2.4.2 mjf struct ifaddrs *ifa, *ifap;
80 1.2.4.2 mjf const struct sockaddr_dl *sdl;
81 1.2.4.2 mjf struct if_laddrreq iflr;
82 1.2.4.2 mjf const uint8_t *octets;
83 1.2.4.2 mjf
84 1.2.4.2 mjf if ((ifname = getifname(env)) == NULL)
85 1.2.4.2 mjf err(EXIT_FAILURE, "%s: getifname", __func__);
86 1.2.4.2 mjf
87 1.2.4.2 mjf if ((s = getsock(AF_LINK)) == -1)
88 1.2.4.2 mjf err(EXIT_FAILURE, "%s: getsock", __func__);
89 1.2.4.2 mjf
90 1.2.4.2 mjf if (getifaddrs(&ifap) == -1)
91 1.2.4.2 mjf err(EXIT_FAILURE, "%s: getifaddrs", __func__);
92 1.2.4.2 mjf
93 1.2.4.2 mjf memset(&iflr, 0, sizeof(iflr));
94 1.2.4.2 mjf
95 1.2.4.2 mjf strlcpy(iflr.iflr_name, ifname, sizeof(iflr.iflr_name));
96 1.2.4.2 mjf
97 1.2.4.2 mjf for (ifa = ifap; ifa != NULL; ifa = ifa->ifa_next) {
98 1.2.4.2 mjf if (strcmp(ifname, ifa->ifa_name) != 0)
99 1.2.4.2 mjf continue;
100 1.2.4.2 mjf if (ifa->ifa_addr->sa_family != AF_LINK)
101 1.2.4.2 mjf continue;
102 1.2.4.2 mjf if (ifa->ifa_data != NULL)
103 1.2.4.2 mjf continue;
104 1.2.4.2 mjf
105 1.2.4.2 mjf sdl = satocsdl(ifa->ifa_addr);
106 1.2.4.2 mjf
107 1.2.4.2 mjf memcpy(&iflr.addr, ifa->ifa_addr, MIN(ifa->ifa_addr->sa_len,
108 1.2.4.2 mjf sizeof(iflr.addr)));
109 1.2.4.2 mjf iflr.flags = IFLR_PREFIX;
110 1.2.4.2 mjf iflr.prefixlen = sdl->sdl_alen * NBBY;
111 1.2.4.2 mjf
112 1.2.4.2 mjf if (ioctl(s, SIOCGLIFADDR, &iflr) == -1)
113 1.2.4.2 mjf err(EXIT_FAILURE, "%s: ioctl", __func__);
114 1.2.4.2 mjf
115 1.2.4.2 mjf if ((iflr.flags & IFLR_ACTIVE) != 0)
116 1.2.4.2 mjf continue;
117 1.2.4.2 mjf
118 1.2.4.2 mjf octets = (const uint8_t *)&sdl->sdl_data[sdl->sdl_nlen];
119 1.2.4.2 mjf
120 1.2.4.2 mjf delim = "\tlink ";
121 1.2.4.2 mjf for (i = 0; i < sdl->sdl_alen; i++) {
122 1.2.4.2 mjf printf("%s%02" PRIx8, delim, octets[i]);
123 1.2.4.2 mjf delim = ":";
124 1.2.4.2 mjf }
125 1.2.4.2 mjf printf("\n");
126 1.2.4.2 mjf }
127 1.2.4.2 mjf }
128 1.2.4.2 mjf
129 1.2.4.2 mjf static int
130 1.2.4.3 mjf link_pre_aifaddr(prop_dictionary_t env, const struct afparam *param)
131 1.2.4.2 mjf {
132 1.2.4.2 mjf bool active;
133 1.2.4.2 mjf struct if_laddrreq *iflr = param->req.buf;
134 1.2.4.2 mjf
135 1.2.4.2 mjf if (prop_dictionary_get_bool(env, "active", &active) && active)
136 1.2.4.2 mjf iflr->flags |= IFLR_ACTIVE;
137 1.2.4.2 mjf
138 1.2.4.2 mjf return 0;
139 1.2.4.2 mjf }
140 1.2.4.2 mjf
141 1.2.4.3 mjf static void
142 1.2.4.2 mjf link_commit_address(prop_dictionary_t env, prop_dictionary_t oenv)
143 1.2.4.2 mjf {
144 1.2.4.2 mjf struct if_laddrreq dgreq = {
145 1.2.4.2 mjf .addr = {
146 1.2.4.2 mjf .ss_family = AF_LINK,
147 1.2.4.2 mjf .ss_len = sizeof(dgreq.addr),
148 1.2.4.2 mjf },
149 1.2.4.2 mjf };
150 1.2.4.2 mjf struct if_laddrreq req = {
151 1.2.4.2 mjf .addr = {
152 1.2.4.2 mjf .ss_family = AF_LINK,
153 1.2.4.2 mjf .ss_len = sizeof(req.addr),
154 1.2.4.2 mjf }
155 1.2.4.2 mjf };
156 1.2.4.2 mjf struct afparam linkparam = {
157 1.2.4.2 mjf .req = BUFPARAM(req)
158 1.2.4.2 mjf , .dgreq = BUFPARAM(dgreq)
159 1.2.4.2 mjf , .name = {
160 1.2.4.2 mjf {.buf = dgreq.iflr_name,
161 1.2.4.2 mjf .buflen = sizeof(dgreq.iflr_name)},
162 1.2.4.2 mjf {.buf = req.iflr_name,
163 1.2.4.2 mjf .buflen = sizeof(req.iflr_name)}
164 1.2.4.2 mjf }
165 1.2.4.2 mjf , .dgaddr = BUFPARAM(dgreq.addr)
166 1.2.4.2 mjf , .addr = BUFPARAM(req.addr)
167 1.2.4.2 mjf , .aifaddr = IFADDR_PARAM(SIOCALIFADDR)
168 1.2.4.2 mjf , .difaddr = IFADDR_PARAM(SIOCDLIFADDR)
169 1.2.4.2 mjf , .gifaddr = IFADDR_PARAM(0)
170 1.2.4.2 mjf , .pre_aifaddr = link_pre_aifaddr
171 1.2.4.2 mjf };
172 1.2.4.2 mjf commit_address(env, oenv, &linkparam);
173 1.2.4.2 mjf }
174 1.2.4.3 mjf
175 1.2.4.3 mjf static void
176 1.2.4.3 mjf link_constructor(void)
177 1.2.4.3 mjf {
178 1.2.4.3 mjf register_family(&af);
179 1.2.4.3 mjf cmdloop_branch_init(&branch, &link.pk_parser);
180 1.2.4.3 mjf register_cmdloop_branch(&branch);
181 1.2.4.3 mjf }
182