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