af_link.c revision 1.1 1 /* $NetBSD: af_link.c,v 1.1 2008/05/12 22:06:13 dyoung Exp $ */
2
3 /*-
4 * Copyright (c) 2008 David Young. 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 AUTHOR 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 AUTHOR 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/cdefs.h>
29 #ifndef lint
30 __RCSID("$NetBSD: af_link.c,v 1.1 2008/05/12 22:06:13 dyoung Exp $");
31 #endif /* not lint */
32
33 #include <sys/param.h>
34 #include <sys/ioctl.h>
35 #include <sys/socket.h>
36
37 #include <net/if.h>
38 #include <net/if_dl.h>
39
40 #include <assert.h>
41 #include <err.h>
42 #include <errno.h>
43 #include <ifaddrs.h>
44 #include <netdb.h>
45 #include <string.h>
46 #include <stdlib.h>
47 #include <stdio.h>
48 #include <util.h>
49
50 #include "env.h"
51 #include "extern.h"
52 #include "af_link.h"
53 #include "af_inetany.h"
54
55 void
56 link_status(prop_dictionary_t env, prop_dictionary_t oenv, bool force)
57 {
58 const char *delim, *ifname;
59 int i, s;
60 struct ifaddrs *ifa, *ifap;
61 const struct sockaddr_dl *sdl;
62 struct if_laddrreq iflr;
63 const uint8_t *octets;
64
65 if ((ifname = getifname(env)) == NULL)
66 err(EXIT_FAILURE, "%s: getifname", __func__);
67
68 if ((s = getsock(AF_UNSPEC)) == -1)
69 err(EXIT_FAILURE, "%s: getsock", __func__);
70
71 if (getifaddrs(&ifap) == -1)
72 err(EXIT_FAILURE, "%s: getifaddrs", __func__);
73
74 memset(&iflr, 0, sizeof(iflr));
75
76 strlcpy(iflr.iflr_name, ifname, sizeof(iflr.iflr_name));
77
78 for (ifa = ifap; ifa != NULL; ifa = ifa->ifa_next) {
79 if (strcmp(ifname, ifa->ifa_name) != 0)
80 continue;
81 if (ifa->ifa_addr->sa_family != AF_LINK)
82 continue;
83 if (ifa->ifa_data != NULL)
84 continue;
85
86 sdl = satocsdl(ifa->ifa_addr);
87
88 memcpy(&iflr.addr, ifa->ifa_addr, MIN(ifa->ifa_addr->sa_len,
89 sizeof(iflr.addr)));
90 iflr.flags = IFLR_PREFIX;
91 iflr.prefixlen = sdl->sdl_alen * NBBY;
92
93 if (ioctl(s, SIOCGLIFADDR, &iflr) == -1)
94 err(EXIT_FAILURE, "%s: ioctl", __func__);
95
96 if ((iflr.flags & IFLR_ACTIVE) != 0)
97 continue;
98
99 octets = (const uint8_t *)&sdl->sdl_data[sdl->sdl_nlen];
100
101 delim = "\tlink ";
102 for (i = 0; i < sdl->sdl_alen; i++) {
103 printf("%s%02" PRIx8, delim, octets[i]);
104 delim = ":";
105 }
106 printf("\n");
107 }
108 }
109
110 static int
111 link_pre_aifaddr(prop_dictionary_t env, struct afparam *param)
112 {
113 bool active;
114 struct if_laddrreq *iflr = param->req.buf;
115
116 if (prop_dictionary_get_bool(env, "active", &active) && active)
117 iflr->flags |= IFLR_ACTIVE;
118
119 return 0;
120 }
121
122 void
123 link_commit_address(prop_dictionary_t env, prop_dictionary_t oenv)
124 {
125 struct if_laddrreq dgreq = {
126 .addr = {
127 .ss_family = AF_LINK,
128 .ss_len = sizeof(dgreq.addr),
129 },
130 };
131 struct if_laddrreq req = {
132 .addr = {
133 .ss_family = AF_LINK,
134 .ss_len = sizeof(req.addr),
135 }
136 };
137 struct afparam linkparam = {
138 .req = BUFPARAM(req)
139 , .dgreq = BUFPARAM(dgreq)
140 , .name = {
141 {.buf = dgreq.iflr_name,
142 .buflen = sizeof(dgreq.iflr_name)},
143 {.buf = req.iflr_name,
144 .buflen = sizeof(req.iflr_name)}
145 }
146 , .dgaddr = BUFPARAM(dgreq.addr)
147 , .addr = BUFPARAM(req.addr)
148 , .aifaddr = IFADDR_PARAM(SIOCALIFADDR)
149 , .difaddr = IFADDR_PARAM(SIOCDLIFADDR)
150 , .gifaddr = IFADDR_PARAM(0)
151 , .pre_aifaddr = link_pre_aifaddr
152 };
153 commit_address(env, oenv, &linkparam);
154 }
155