ifconfig.c revision 1.181.4.1 1 1.181.4.1 mjf /* $NetBSD: ifconfig.c,v 1.181.4.1 2008/06/02 13:21:22 mjf Exp $ */
2 1.24 thorpej
3 1.41 thorpej /*-
4 1.64 thorpej * Copyright (c) 1997, 1998, 2000 The NetBSD Foundation, Inc.
5 1.24 thorpej * All rights reserved.
6 1.24 thorpej *
7 1.41 thorpej * This code is derived from software contributed to The NetBSD Foundation
8 1.41 thorpej * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 1.41 thorpej * NASA Ames Research Center.
10 1.41 thorpej *
11 1.24 thorpej * Redistribution and use in source and binary forms, with or without
12 1.24 thorpej * modification, are permitted provided that the following conditions
13 1.24 thorpej * are met:
14 1.24 thorpej * 1. Redistributions of source code must retain the above copyright
15 1.24 thorpej * notice, this list of conditions and the following disclaimer.
16 1.24 thorpej * 2. Redistributions in binary form must reproduce the above copyright
17 1.24 thorpej * notice, this list of conditions and the following disclaimer in the
18 1.24 thorpej * documentation and/or other materials provided with the distribution.
19 1.24 thorpej *
20 1.41 thorpej * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 1.41 thorpej * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 1.41 thorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 1.41 thorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 1.41 thorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 1.41 thorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 1.41 thorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 1.41 thorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 1.41 thorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 1.41 thorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 1.41 thorpej * POSSIBILITY OF SUCH DAMAGE.
31 1.24 thorpej */
32 1.19 cgd
33 1.1 cgd /*
34 1.13 mycroft * Copyright (c) 1983, 1993
35 1.13 mycroft * The Regents of the University of California. All rights reserved.
36 1.1 cgd *
37 1.1 cgd * Redistribution and use in source and binary forms, with or without
38 1.1 cgd * modification, are permitted provided that the following conditions
39 1.1 cgd * are met:
40 1.1 cgd * 1. Redistributions of source code must retain the above copyright
41 1.1 cgd * notice, this list of conditions and the following disclaimer.
42 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
43 1.1 cgd * notice, this list of conditions and the following disclaimer in the
44 1.1 cgd * documentation and/or other materials provided with the distribution.
45 1.138 agc * 3. Neither the name of the University nor the names of its contributors
46 1.1 cgd * may be used to endorse or promote products derived from this software
47 1.1 cgd * without specific prior written permission.
48 1.1 cgd *
49 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 1.1 cgd * SUCH DAMAGE.
60 1.1 cgd */
61 1.1 cgd
62 1.39 lukem #include <sys/cdefs.h>
63 1.1 cgd #ifndef lint
64 1.39 lukem __COPYRIGHT("@(#) Copyright (c) 1983, 1993\n\
65 1.39 lukem The Regents of the University of California. All rights reserved.\n");
66 1.1 cgd #endif /* not lint */
67 1.1 cgd
68 1.1 cgd #ifndef lint
69 1.19 cgd #if 0
70 1.19 cgd static char sccsid[] = "@(#)ifconfig.c 8.2 (Berkeley) 2/16/94";
71 1.19 cgd #else
72 1.181.4.1 mjf __RCSID("$NetBSD: ifconfig.c,v 1.181.4.1 2008/06/02 13:21:22 mjf Exp $");
73 1.19 cgd #endif
74 1.1 cgd #endif /* not lint */
75 1.1 cgd
76 1.1 cgd #include <sys/param.h>
77 1.1 cgd #include <sys/socket.h>
78 1.1 cgd #include <sys/ioctl.h>
79 1.1 cgd
80 1.1 cgd #include <net/if.h>
81 1.30 thorpej #include <net/if_dl.h>
82 1.24 thorpej #include <net/if_media.h>
83 1.62 chopps #include <net/if_ether.h>
84 1.166 thorpej #include <netinet/in.h> /* XXX */
85 1.166 thorpej #include <netinet/in_var.h> /* XXX */
86 1.181.4.1 mjf
87 1.13 mycroft #include <netdb.h>
88 1.1 cgd
89 1.1 cgd #include <sys/protosw.h>
90 1.1 cgd
91 1.181.4.1 mjf #include <assert.h>
92 1.13 mycroft #include <ctype.h>
93 1.13 mycroft #include <err.h>
94 1.13 mycroft #include <errno.h>
95 1.181.4.1 mjf #include <stdbool.h>
96 1.50 chopps #include <stddef.h>
97 1.1 cgd #include <stdio.h>
98 1.1 cgd #include <stdlib.h>
99 1.1 cgd #include <string.h>
100 1.13 mycroft #include <unistd.h>
101 1.78 itojun #include <ifaddrs.h>
102 1.130 christos #include <util.h>
103 1.1 cgd
104 1.156 thorpej #include "extern.h"
105 1.161 thorpej
106 1.161 thorpej #ifndef INET_ONLY
107 1.161 thorpej #include "af_atalk.h"
108 1.163 thorpej #include "af_iso.h"
109 1.161 thorpej #endif /* ! INET_ONLY */
110 1.166 thorpej #include "af_inet.h"
111 1.164 thorpej #ifdef INET6
112 1.164 thorpej #include "af_inet6.h"
113 1.164 thorpej #endif /* INET6 */
114 1.181.4.1 mjf #include "af_link.h"
115 1.161 thorpej
116 1.153 yamt #include "agr.h"
117 1.171 liamjfoy #include "carp.h"
118 1.159 thorpej #include "ieee80211.h"
119 1.157 thorpej #include "tunnel.h"
120 1.155 thorpej #include "vlan.h"
121 1.181.4.1 mjf #include "parse.h"
122 1.181.4.1 mjf #include "env.h"
123 1.153 yamt
124 1.166 thorpej int setaddr, doalias;
125 1.181.4.1 mjf int clearaddr;
126 1.36 lukem int newaddr = -1;
127 1.173 martin int check_up_state = -1;
128 1.181.4.1 mjf int bflag, dflag, hflag, lflag, mflag, sflag, uflag, vflag, zflag;
129 1.53 itojun #ifdef INET6
130 1.53 itojun int Lflag;
131 1.53 itojun #endif
132 1.1 cgd
133 1.45 thorpej /*
134 1.45 thorpej * Media stuff. Whenever a media command is first performed, the
135 1.45 thorpej * currently select media is grabbed for this interface. If `media'
136 1.45 thorpej * is given, the current media word is modifed. `mediaopt' commands
137 1.45 thorpej * only modify the set and clear words. They then operate on the
138 1.45 thorpej * current media word later.
139 1.45 thorpej */
140 1.45 thorpej int media_current;
141 1.45 thorpej int mediaopt_set;
142 1.45 thorpej int mediaopt_clear;
143 1.45 thorpej
144 1.181.4.1 mjf void check_ifflags_up(prop_dictionary_t);
145 1.45 thorpej
146 1.181.4.1 mjf int notealias(prop_dictionary_t, prop_dictionary_t);
147 1.181.4.1 mjf int notrailers(prop_dictionary_t, prop_dictionary_t);
148 1.181.4.1 mjf int setifaddr(prop_dictionary_t, prop_dictionary_t);
149 1.181.4.1 mjf int setifdstormask(prop_dictionary_t, prop_dictionary_t);
150 1.181.4.1 mjf int setifflags(prop_dictionary_t, prop_dictionary_t);
151 1.181.4.1 mjf static int setifcaps(prop_dictionary_t, prop_dictionary_t);
152 1.181.4.1 mjf int setifbroadaddr(prop_dictionary_t, prop_dictionary_t);
153 1.181.4.1 mjf static int setifmetric(prop_dictionary_t, prop_dictionary_t);
154 1.181.4.1 mjf static int setifmtu(prop_dictionary_t, prop_dictionary_t);
155 1.181.4.1 mjf int setifnetmask(prop_dictionary_t, prop_dictionary_t);
156 1.181.4.1 mjf int setifprefixlen(prop_dictionary_t, prop_dictionary_t);
157 1.181.4.1 mjf int setmedia(prop_dictionary_t, prop_dictionary_t);
158 1.181.4.1 mjf int setmediamode(prop_dictionary_t, prop_dictionary_t);
159 1.181.4.1 mjf int setmediaopt(prop_dictionary_t, prop_dictionary_t);
160 1.181.4.1 mjf int unsetmediaopt(prop_dictionary_t, prop_dictionary_t);
161 1.181.4.1 mjf int setmediainst(prop_dictionary_t, prop_dictionary_t);
162 1.181.4.1 mjf static int clone_command(prop_dictionary_t, prop_dictionary_t);
163 1.181.4.1 mjf static void do_setifpreference(prop_dictionary_t);
164 1.181.4.1 mjf
165 1.181.4.1 mjf static int no_cmds_exec(prop_dictionary_t, prop_dictionary_t);
166 1.181.4.1 mjf static int media_status_exec(prop_dictionary_t, prop_dictionary_t);
167 1.181.4.1 mjf
168 1.181.4.1 mjf int carrier(prop_dictionary_t);
169 1.181.4.1 mjf void printall(const char *, prop_dictionary_t);
170 1.181.4.1 mjf int list_cloners(prop_dictionary_t, prop_dictionary_t);
171 1.181.4.1 mjf void status(const struct sockaddr_dl *, prop_dictionary_t,
172 1.181.4.1 mjf prop_dictionary_t);
173 1.181.4.1 mjf void usage(void);
174 1.181.4.1 mjf
175 1.181.4.1 mjf void print_media_word(int, const char *);
176 1.181.4.1 mjf void process_media_commands(prop_dictionary_t);
177 1.181.4.1 mjf void init_current_media(prop_dictionary_t, prop_dictionary_t);
178 1.181.4.1 mjf
179 1.181.4.1 mjf /* Known address families */
180 1.181.4.1 mjf static const struct afswtch afs[] = {
181 1.181.4.1 mjf {.af_name = "inet", .af_af = AF_INET, .af_status = in_status,
182 1.181.4.1 mjf .af_addr_commit = in_commit_address}
183 1.181.4.1 mjf
184 1.181.4.1 mjf , {.af_name = "link", .af_af = AF_LINK, .af_status = link_status,
185 1.181.4.1 mjf .af_addr_commit = link_commit_address}
186 1.53 itojun #ifdef INET6
187 1.181.4.1 mjf , {.af_name = "inet6", .af_af = AF_INET6, .af_status = in6_status,
188 1.181.4.1 mjf .af_addr_commit = in6_commit_address}
189 1.81 thorpej #endif
190 1.181.4.1 mjf
191 1.181.4.1 mjf #ifndef INET_ONLY /* small version, for boot media */
192 1.181.4.1 mjf , {.af_name = "atalk", .af_af = AF_APPLETALK, .af_status = at_status,
193 1.181.4.1 mjf .af_getaddr = at_getaddr, NULL, .af_difaddr = SIOCDIFADDR,
194 1.181.4.1 mjf .af_aifaddr = SIOCAIFADDR, .af_gifaddr = SIOCGIFADDR,
195 1.181.4.1 mjf .af_ridreq = &at_addreq, .af_addreq = &at_addreq}
196 1.181.4.1 mjf , {.af_name = "iso", .af_af = AF_ISO, .af_status = iso_status,
197 1.181.4.1 mjf .af_getaddr = iso_getaddr, NULL, .af_difaddr = SIOCDIFADDR_ISO,
198 1.181.4.1 mjf .af_aifaddr = SIOCAIFADDR_ISO, .af_gifaddr = SIOCGIFADDR_ISO,
199 1.181.4.1 mjf .af_ridreq = &iso_ridreq, .af_addreq = &iso_addreq}
200 1.181.4.1 mjf #endif /* INET_ONLY */
201 1.181.4.1 mjf
202 1.181.4.1 mjf , {.af_name = NULL} /* sentinel */
203 1.1 cgd };
204 1.1 cgd
205 1.181.4.1 mjf static const struct kwinst ifflagskw[] = {
206 1.181.4.1 mjf IFKW("arp", IFF_NOARP)
207 1.181.4.1 mjf , IFKW("debug", IFF_DEBUG)
208 1.181.4.1 mjf , IFKW("link0", IFF_LINK0)
209 1.181.4.1 mjf , IFKW("link1", IFF_LINK1)
210 1.181.4.1 mjf , IFKW("link2", IFF_LINK2)
211 1.181.4.1 mjf , {.k_word = "down", .k_type = KW_T_INT, .k_int = -IFF_UP}
212 1.181.4.1 mjf , {.k_word = "up", .k_type = KW_T_INT, .k_int = IFF_UP}
213 1.181.4.1 mjf };
214 1.145 dsl
215 1.181.4.1 mjf static const struct kwinst ifcapskw[] = {
216 1.181.4.1 mjf IFKW("ip4csum-tx", IFCAP_CSUM_IPv4_Tx)
217 1.181.4.1 mjf , IFKW("ip4csum-rx", IFCAP_CSUM_IPv4_Rx)
218 1.181.4.1 mjf , IFKW("tcp4csum-tx", IFCAP_CSUM_TCPv4_Tx)
219 1.181.4.1 mjf , IFKW("tcp4csum-rx", IFCAP_CSUM_TCPv4_Rx)
220 1.181.4.1 mjf , IFKW("udp4csum-tx", IFCAP_CSUM_UDPv4_Tx)
221 1.181.4.1 mjf , IFKW("udp4csum-rx", IFCAP_CSUM_UDPv4_Rx)
222 1.181.4.1 mjf , IFKW("tcp6csum-tx", IFCAP_CSUM_TCPv6_Tx)
223 1.181.4.1 mjf , IFKW("tcp6csum-rx", IFCAP_CSUM_TCPv6_Rx)
224 1.181.4.1 mjf , IFKW("udp6csum-tx", IFCAP_CSUM_UDPv6_Tx)
225 1.181.4.1 mjf , IFKW("udp6csum-rx", IFCAP_CSUM_UDPv6_Rx)
226 1.181.4.1 mjf , IFKW("ip4csum", IFCAP_CSUM_IPv4_Tx|IFCAP_CSUM_IPv4_Rx)
227 1.181.4.1 mjf , IFKW("tcp4csum", IFCAP_CSUM_TCPv4_Tx|IFCAP_CSUM_TCPv4_Rx)
228 1.181.4.1 mjf , IFKW("udp4csum", IFCAP_CSUM_UDPv4_Tx|IFCAP_CSUM_UDPv4_Rx)
229 1.181.4.1 mjf , IFKW("tcp6csum", IFCAP_CSUM_TCPv6_Tx|IFCAP_CSUM_TCPv6_Rx)
230 1.181.4.1 mjf , IFKW("udp6csum", IFCAP_CSUM_UDPv6_Tx|IFCAP_CSUM_UDPv6_Rx)
231 1.181.4.1 mjf , IFKW("tso4", IFCAP_TSOv4)
232 1.181.4.1 mjf , IFKW("tso6", IFCAP_TSOv6)
233 1.181.4.1 mjf };
234 1.24 thorpej
235 1.181.4.1 mjf extern struct pbranch command_root;
236 1.181.4.1 mjf extern struct pbranch opt_command;
237 1.181.4.1 mjf extern struct pbranch opt_family, opt_silent_family;
238 1.181.4.1 mjf extern struct pkw cloning, silent_family, family, ifcaps, ifflags, misc;
239 1.181.4.1 mjf
240 1.181.4.1 mjf struct pinteger parse_metric = PINTEGER_INITIALIZER(&parse_metric, "metric", 10,
241 1.181.4.1 mjf setifmetric, "metric", &command_root.pb_parser);
242 1.181.4.1 mjf
243 1.181.4.1 mjf struct pinteger parse_mtu = PINTEGER_INITIALIZER(&parse_mtu, "mtu", 10,
244 1.181.4.1 mjf setifmtu, "mtu", &command_root.pb_parser);
245 1.181.4.1 mjf
246 1.181.4.1 mjf struct pinteger parse_prefixlen = PINTEGER_INITIALIZER(&parse_prefixlen,
247 1.181.4.1 mjf "prefixlen", 10, setifprefixlen, "prefixlen", &command_root.pb_parser);
248 1.181.4.1 mjf
249 1.181.4.1 mjf struct pinteger parse_preference = PINTEGER_INITIALIZER1(&parse_preference,
250 1.181.4.1 mjf "preference", INT16_MIN, INT16_MAX, 10, NULL, "preference",
251 1.181.4.1 mjf &command_root.pb_parser);
252 1.181.4.1 mjf
253 1.181.4.1 mjf struct paddr parse_netmask = PADDR_INITIALIZER(&parse_netmask, "netmask",
254 1.181.4.1 mjf setifnetmask, "dstormask", NULL, NULL, NULL, &command_root.pb_parser);
255 1.181.4.1 mjf
256 1.181.4.1 mjf struct paddr parse_broadcast = PADDR_INITIALIZER(&parse_broadcast,
257 1.181.4.1 mjf "broadcast address",
258 1.181.4.1 mjf setifbroadaddr, "broadcast", NULL, NULL, NULL, &command_root.pb_parser);
259 1.181.4.1 mjf
260 1.181.4.1 mjf struct pstr mediamode = PSTR_INITIALIZER(&mediamode, "mediamode",
261 1.181.4.1 mjf setmediamode, "mediamode", &command_root.pb_parser);
262 1.181.4.1 mjf
263 1.181.4.1 mjf struct pinteger mediainst = PINTEGER_INITIALIZER1(&mediainst, "mediainst",
264 1.181.4.1 mjf 0, IFM_INST_MAX, 10, setmediainst, "mediainst", &command_root.pb_parser);
265 1.181.4.1 mjf
266 1.181.4.1 mjf struct pstr unmediaopt = PSTR_INITIALIZER(&unmediaopt, "-mediaopt",
267 1.181.4.1 mjf unsetmediaopt, "unmediaopt", &command_root.pb_parser);
268 1.181.4.1 mjf
269 1.181.4.1 mjf struct pstr mediaopt = PSTR_INITIALIZER(&mediaopt, "mediaopt",
270 1.181.4.1 mjf setmediaopt, "mediaopt", &command_root.pb_parser);
271 1.181.4.1 mjf
272 1.181.4.1 mjf struct pstr media = PSTR_INITIALIZER(&media, "media",
273 1.181.4.1 mjf setmedia, "media", &command_root.pb_parser);
274 1.181.4.1 mjf
275 1.181.4.1 mjf static const struct kwinst misckw[] = {
276 1.181.4.1 mjf {.k_word = "active", .k_key = "active", .k_type = KW_T_BOOL,
277 1.181.4.1 mjf .k_bool = true, .k_nextparser = &command_root.pb_parser}
278 1.181.4.1 mjf , {.k_word = "alias", .k_key = "alias", .k_deact = "alias",
279 1.181.4.1 mjf .k_type = KW_T_BOOL, .k_neg = true,
280 1.181.4.1 mjf .k_bool = true, .k_negbool = false,
281 1.181.4.1 mjf .k_exec = notealias, .k_nextparser = &command_root.pb_parser}
282 1.181.4.1 mjf , {.k_word = "broadcast", .k_nextparser = &parse_broadcast.pa_parser}
283 1.181.4.1 mjf , {.k_word = "delete", .k_key = "alias", .k_deact = "alias",
284 1.181.4.1 mjf .k_type = KW_T_BOOL, .k_bool = false, .k_exec = notealias,
285 1.181.4.1 mjf .k_nextparser = &command_root.pb_parser}
286 1.181.4.1 mjf , {.k_word = "instance", .k_key = "anymedia", .k_type = KW_T_BOOL,
287 1.181.4.1 mjf .k_bool = true, .k_act = "media", .k_deact = "mediainst",
288 1.181.4.1 mjf .k_nextparser = &mediainst.pi_parser}
289 1.181.4.1 mjf , {.k_word = "inst", .k_key = "anymedia", .k_type = KW_T_BOOL,
290 1.181.4.1 mjf .k_bool = true, .k_act = "media", .k_deact = "mediainst",
291 1.181.4.1 mjf .k_nextparser = &mediainst.pi_parser}
292 1.181.4.1 mjf , {.k_word = "media", .k_key = "anymedia", .k_type = KW_T_BOOL,
293 1.181.4.1 mjf .k_bool = true, .k_deact = "media", .k_altdeact = "anymedia",
294 1.181.4.1 mjf .k_nextparser = &media.ps_parser}
295 1.181.4.1 mjf , {.k_word = "mediaopt", .k_key = "anymedia", .k_type = KW_T_BOOL,
296 1.181.4.1 mjf .k_bool = true, .k_deact = "mediaopt", .k_altdeact = "instance",
297 1.181.4.1 mjf .k_nextparser = &mediaopt.ps_parser}
298 1.181.4.1 mjf , {.k_word = "-mediaopt", .k_key = "anymedia", .k_type = KW_T_BOOL,
299 1.181.4.1 mjf .k_bool = true, .k_deact = "unmediaopt", .k_altdeact = "media",
300 1.181.4.1 mjf .k_nextparser = &unmediaopt.ps_parser}
301 1.181.4.1 mjf , {.k_word = "mode", .k_key = "anymedia", .k_type = KW_T_BOOL,
302 1.181.4.1 mjf .k_bool = true, .k_deact = "mode",
303 1.181.4.1 mjf .k_nextparser = &mediamode.ps_parser}
304 1.181.4.1 mjf , {.k_word = "metric", .k_nextparser = &parse_metric.pi_parser}
305 1.181.4.1 mjf , {.k_word = "mtu", .k_nextparser = &parse_mtu.pi_parser}
306 1.181.4.1 mjf , {.k_word = "netmask", .k_nextparser = &parse_netmask.pa_parser}
307 1.181.4.1 mjf , {.k_word = "preference", .k_act = "address",
308 1.181.4.1 mjf .k_nextparser = &parse_preference.pi_parser}
309 1.181.4.1 mjf , {.k_word = "prefixlen", .k_nextparser = &parse_prefixlen.pi_parser}
310 1.181.4.1 mjf , {.k_word = "trailers", .k_neg = true,
311 1.181.4.1 mjf .k_exec = notrailers, .k_nextparser = &command_root.pb_parser}
312 1.181.4.1 mjf };
313 1.181.4.1 mjf
314 1.181.4.1 mjf /* key: clonecmd */
315 1.181.4.1 mjf static const struct kwinst clonekw[] = {
316 1.181.4.1 mjf {.k_word = "create", .k_type = KW_T_INT, .k_int = SIOCIFCREATE,
317 1.181.4.1 mjf .k_nextparser = &opt_silent_family.pb_parser},
318 1.181.4.1 mjf {.k_word = "destroy", .k_type = KW_T_INT, .k_int = SIOCIFDESTROY}
319 1.181.4.1 mjf };
320 1.181.4.1 mjf
321 1.181.4.1 mjf static const struct kwinst familykw[] = {
322 1.181.4.1 mjf {.k_word = "inet", .k_type = KW_T_INT, .k_int = AF_INET,
323 1.181.4.1 mjf .k_nextparser = NULL}
324 1.181.4.1 mjf , {.k_word = "link", .k_type = KW_T_INT, .k_int = AF_LINK,
325 1.181.4.1 mjf .k_nextparser = NULL}
326 1.53 itojun #ifdef INET6
327 1.181.4.1 mjf , {.k_word = "inet6", .k_type = KW_T_INT, .k_int = AF_INET6,
328 1.181.4.1 mjf .k_nextparser = NULL}
329 1.53 itojun #endif
330 1.21 gwr #ifndef INET_ONLY /* small version, for boot media */
331 1.181.4.1 mjf , {.k_word = "atalk", .k_type = KW_T_INT, .k_int = AF_APPLETALK,
332 1.181.4.1 mjf .k_nextparser = NULL}
333 1.181.4.1 mjf , {.k_word = "iso", .k_type = KW_T_INT, .k_int = AF_ISO,
334 1.181.4.1 mjf .k_nextparser = NULL}
335 1.21 gwr #endif /* INET_ONLY */
336 1.1 cgd };
337 1.1 cgd
338 1.181.4.1 mjf struct pterm cloneterm = PTERM_INITIALIZER(&cloneterm, "list cloners",
339 1.181.4.1 mjf list_cloners, "none");
340 1.181.4.1 mjf
341 1.181.4.1 mjf struct pterm no_cmds = PTERM_INITIALIZER(&no_cmds, "no commands", no_cmds_exec,
342 1.181.4.1 mjf "none");
343 1.181.4.1 mjf
344 1.181.4.1 mjf struct pkw family_only =
345 1.181.4.1 mjf PKW_INITIALIZER(&family_only, "family-only", NULL, "af", familykw,
346 1.181.4.1 mjf __arraycount(familykw), &no_cmds.pt_parser);
347 1.181.4.1 mjf
348 1.181.4.1 mjf struct paddr address = PADDR_INITIALIZER(&address,
349 1.181.4.1 mjf "local address (address 1)",
350 1.181.4.1 mjf setifaddr, "address", "netmask", NULL, "address", &command_root.pb_parser);
351 1.181.4.1 mjf
352 1.181.4.1 mjf struct paddr dstormask = PADDR_INITIALIZER(&dstormask,
353 1.181.4.1 mjf "destination/netmask (address 2)",
354 1.181.4.1 mjf setifdstormask, "dstormask", NULL, "address", "dstormask",
355 1.181.4.1 mjf &command_root.pb_parser);
356 1.181.4.1 mjf
357 1.181.4.1 mjf struct paddr broadcast = PADDR_INITIALIZER(&broadcast,
358 1.181.4.1 mjf "broadcast address (address 3)",
359 1.181.4.1 mjf setifbroadaddr, "broadcast", NULL, "dstormask", "broadcast",
360 1.181.4.1 mjf &command_root.pb_parser);
361 1.181.4.1 mjf
362 1.181.4.1 mjf struct branch opt_clone_brs[] = {
363 1.181.4.1 mjf {.b_nextparser = &cloning.pk_parser}
364 1.181.4.1 mjf , {.b_nextparser = &opt_family.pb_parser}
365 1.181.4.1 mjf }, opt_silent_family_brs[] = {
366 1.181.4.1 mjf {.b_nextparser = &silent_family.pk_parser}
367 1.181.4.1 mjf , {.b_nextparser = &command_root.pb_parser}
368 1.181.4.1 mjf }, opt_family_brs[] = {
369 1.181.4.1 mjf {.b_nextparser = &family.pk_parser}
370 1.181.4.1 mjf , {.b_nextparser = &opt_command.pb_parser}
371 1.181.4.1 mjf }, command_root_brs[] = {
372 1.181.4.1 mjf {.b_nextparser = &ieee80211bool.pk_parser}
373 1.181.4.1 mjf , {.b_nextparser = &ifflags.pk_parser}
374 1.181.4.1 mjf , {.b_nextparser = &ifcaps.pk_parser}
375 1.181.4.1 mjf #ifdef INET6
376 1.181.4.1 mjf , {.b_nextparser = &ia6flags.pk_parser}
377 1.181.4.1 mjf , {.b_nextparser = &inet6.pk_parser}
378 1.181.4.1 mjf #endif /*INET6*/
379 1.181.4.1 mjf , {.b_nextparser = &misc.pk_parser}
380 1.181.4.1 mjf , {.b_nextparser = &tunnel.pk_parser}
381 1.181.4.1 mjf , {.b_nextparser = &vlan.pk_parser}
382 1.181.4.1 mjf , {.b_nextparser = &agr.pk_parser}
383 1.181.4.1 mjf #ifndef INET_ONLY
384 1.181.4.1 mjf , {.b_nextparser = &carp.pk_parser}
385 1.181.4.1 mjf , {.b_nextparser = &atalk.pk_parser}
386 1.181.4.1 mjf , {.b_nextparser = &iso.pk_parser}
387 1.181.4.1 mjf #endif
388 1.181.4.1 mjf , {.b_nextparser = &kw80211.pk_parser}
389 1.181.4.1 mjf , {.b_nextparser = &address.pa_parser}
390 1.181.4.1 mjf , {.b_nextparser = &dstormask.pa_parser}
391 1.181.4.1 mjf , {.b_nextparser = &broadcast.pa_parser}
392 1.181.4.1 mjf , {.b_nextparser = NULL}
393 1.181.4.1 mjf }, opt_command_brs[] = {
394 1.181.4.1 mjf {.b_nextparser = &no_cmds.pt_parser}
395 1.181.4.1 mjf , {.b_nextparser = &command_root.pb_parser}
396 1.181.4.1 mjf };
397 1.181.4.1 mjf
398 1.181.4.1 mjf struct branch opt_family_only_brs[] = {
399 1.181.4.1 mjf {.b_nextparser = &no_cmds.pt_parser}
400 1.181.4.1 mjf , {.b_nextparser = &family_only.pk_parser}
401 1.181.4.1 mjf };
402 1.181.4.1 mjf struct pbranch opt_family_only = PBRANCH_INITIALIZER(&opt_family_only,
403 1.181.4.1 mjf "opt-family-only", opt_family_only_brs,
404 1.181.4.1 mjf __arraycount(opt_family_only_brs), true);
405 1.181.4.1 mjf struct pbranch opt_command = PBRANCH_INITIALIZER(&opt_command,
406 1.181.4.1 mjf "optional command",
407 1.181.4.1 mjf opt_command_brs, __arraycount(opt_command_brs), true);
408 1.181.4.1 mjf
409 1.181.4.1 mjf struct pbranch command_root = PBRANCH_INITIALIZER(&command_root,
410 1.181.4.1 mjf "command-root", command_root_brs, __arraycount(command_root_brs), true);
411 1.181.4.1 mjf
412 1.181.4.1 mjf struct piface iface_opt_family_only =
413 1.181.4.1 mjf PIFACE_INITIALIZER(&iface_opt_family_only, "iface-opt-family-only",
414 1.181.4.1 mjf NULL, "if", &opt_family_only.pb_parser);
415 1.181.4.1 mjf
416 1.181.4.1 mjf struct pkw family = PKW_INITIALIZER(&family, "family", NULL, "af",
417 1.181.4.1 mjf familykw, __arraycount(familykw), &opt_command.pb_parser);
418 1.181.4.1 mjf
419 1.181.4.1 mjf struct pkw silent_family = PKW_INITIALIZER(&silent_family, "silent family",
420 1.181.4.1 mjf NULL, "af", familykw, __arraycount(familykw), &command_root.pb_parser);
421 1.181.4.1 mjf
422 1.181.4.1 mjf struct pkw ifcaps = PKW_INITIALIZER(&ifcaps, "ifcaps", setifcaps,
423 1.181.4.1 mjf "ifcap", ifcapskw, __arraycount(ifcapskw), &command_root.pb_parser);
424 1.181.4.1 mjf
425 1.181.4.1 mjf struct pkw ifflags = PKW_INITIALIZER(&ifflags, "ifflags", setifflags,
426 1.181.4.1 mjf "ifflag", ifflagskw, __arraycount(ifflagskw), &command_root.pb_parser);
427 1.181.4.1 mjf
428 1.181.4.1 mjf struct pkw cloning = PKW_INITIALIZER(&cloning, "cloning", clone_command,
429 1.181.4.1 mjf "clonecmd", clonekw, __arraycount(clonekw), NULL);
430 1.181.4.1 mjf
431 1.181.4.1 mjf struct pkw misc = PKW_INITIALIZER(&misc, "misc", NULL, NULL,
432 1.181.4.1 mjf misckw, __arraycount(misckw), NULL);
433 1.181.4.1 mjf
434 1.181.4.1 mjf struct pbranch opt_clone = PBRANCH_INITIALIZER(&opt_clone,
435 1.181.4.1 mjf "opt-clone", opt_clone_brs, __arraycount(opt_clone_brs), true);
436 1.181.4.1 mjf
437 1.181.4.1 mjf struct pbranch opt_silent_family = PBRANCH_INITIALIZER(&opt_silent_family,
438 1.181.4.1 mjf "optional silent family", opt_silent_family_brs,
439 1.181.4.1 mjf __arraycount(opt_silent_family_brs), true);
440 1.181.4.1 mjf
441 1.181.4.1 mjf struct pbranch opt_family = PBRANCH_INITIALIZER(&opt_family,
442 1.181.4.1 mjf "opt-family", opt_family_brs, __arraycount(opt_family_brs), true);
443 1.181.4.1 mjf
444 1.181.4.1 mjf struct piface iface_start = PIFACE_INITIALIZER(&iface_start,
445 1.181.4.1 mjf "iface-opt-family", NULL, "if", &opt_clone.pb_parser);
446 1.181.4.1 mjf
447 1.181.4.1 mjf struct piface iface_only = PIFACE_INITIALIZER(&iface_only, "iface",
448 1.181.4.1 mjf media_status_exec, "if", NULL);
449 1.181.4.1 mjf
450 1.181.4.1 mjf static struct parser *
451 1.181.4.1 mjf init_parser(void)
452 1.181.4.1 mjf {
453 1.181.4.1 mjf if (parser_init(&iface_opt_family_only.pif_parser) == -1)
454 1.181.4.1 mjf err(EXIT_FAILURE, "parser_init(iface_opt_family_only)");
455 1.181.4.1 mjf if (parser_init(&iface_only.pif_parser) == -1)
456 1.181.4.1 mjf err(EXIT_FAILURE, "parser_init(iface_only)");
457 1.181.4.1 mjf if (parser_init(&iface_start.pif_parser) == -1)
458 1.181.4.1 mjf err(EXIT_FAILURE, "parser_init(iface_start)");
459 1.181.4.1 mjf
460 1.181.4.1 mjf return &iface_start.pif_parser;
461 1.181.4.1 mjf }
462 1.181.4.1 mjf
463 1.181.4.1 mjf static int
464 1.181.4.1 mjf no_cmds_exec(prop_dictionary_t env, prop_dictionary_t xenv)
465 1.181.4.1 mjf {
466 1.181.4.1 mjf const char *ifname;
467 1.181.4.1 mjf unsigned short ignore;
468 1.181.4.1 mjf
469 1.181.4.1 mjf /* ifname == NULL is ok. It indicates 'ifconfig -a'. */
470 1.181.4.1 mjf if ((ifname = getifname(env)) == NULL)
471 1.181.4.1 mjf ;
472 1.181.4.1 mjf else if (getifflags(env, xenv, &ignore) == -1)
473 1.181.4.1 mjf err(EXIT_FAILURE, "SIOCGIFFLAGS %s", ifname);
474 1.181.4.1 mjf
475 1.181.4.1 mjf printall(ifname, env);
476 1.181.4.1 mjf exit(EXIT_SUCCESS);
477 1.181.4.1 mjf }
478 1.181.4.1 mjf
479 1.181.4.1 mjf static int
480 1.181.4.1 mjf media_status_exec(prop_dictionary_t env, prop_dictionary_t xenv)
481 1.181.4.1 mjf {
482 1.181.4.1 mjf const char *ifname;
483 1.181.4.1 mjf unsigned short ignore;
484 1.181.4.1 mjf
485 1.181.4.1 mjf /* ifname == NULL is ok. It indicates 'ifconfig -a'. */
486 1.181.4.1 mjf if ((ifname = getifname(env)) == NULL)
487 1.181.4.1 mjf ;
488 1.181.4.1 mjf else if (getifflags(env, xenv, &ignore) == -1)
489 1.181.4.1 mjf err(EXIT_FAILURE, "SIOCGIFFLAGS %s", ifname);
490 1.181.4.1 mjf
491 1.181.4.1 mjf exit(carrier(env));
492 1.181.4.1 mjf }
493 1.181.4.1 mjf
494 1.181.4.1 mjf static void
495 1.181.4.1 mjf do_setifcaps(prop_dictionary_t env)
496 1.181.4.1 mjf {
497 1.181.4.1 mjf struct ifcapreq ifcr;
498 1.181.4.1 mjf prop_data_t d;
499 1.181.4.1 mjf
500 1.181.4.1 mjf d = (prop_data_t )prop_dictionary_get(env, "ifcaps");
501 1.181.4.1 mjf if (d == NULL)
502 1.181.4.1 mjf return;
503 1.181.4.1 mjf
504 1.181.4.1 mjf assert(sizeof(ifcr) == prop_data_size(d));
505 1.181.4.1 mjf
506 1.181.4.1 mjf memcpy(&ifcr, prop_data_data_nocopy(d), sizeof(ifcr));
507 1.181.4.1 mjf if (direct_ioctl(env, SIOCSIFCAP, &ifcr) == -1)
508 1.181.4.1 mjf err(EXIT_FAILURE, "SIOCSIFCAP");
509 1.181.4.1 mjf }
510 1.1 cgd
511 1.16 cgd int
512 1.181.4.1 mjf main(int argc, char **argv)
513 1.1 cgd {
514 1.181.4.1 mjf const struct afswtch *afp;
515 1.181.4.1 mjf int af, aflag = 0, Cflag = 0, s;
516 1.181.4.1 mjf struct match match[32];
517 1.181.4.1 mjf size_t nmatch;
518 1.181.4.1 mjf struct parser *start;
519 1.181.4.1 mjf int ch, narg = 0, rc;
520 1.181.4.1 mjf prop_dictionary_t env, xenv;
521 1.181.4.1 mjf const char *ifname;
522 1.181.4.1 mjf
523 1.181.4.1 mjf memset(match, 0, sizeof(match));
524 1.181.4.1 mjf
525 1.181.4.1 mjf start = init_parser();
526 1.24 thorpej
527 1.24 thorpej /* Parse command-line options */
528 1.134 perry aflag = mflag = vflag = zflag = 0;
529 1.169 rpaulo while ((ch = getopt(argc, argv, "AabCdhlmsuvz"
530 1.53 itojun #ifdef INET6
531 1.53 itojun "L"
532 1.53 itojun #endif
533 1.53 itojun )) != -1) {
534 1.24 thorpej switch (ch) {
535 1.49 christos case 'A':
536 1.107 itojun warnx("-A is deprecated");
537 1.49 christos break;
538 1.49 christos
539 1.24 thorpej case 'a':
540 1.24 thorpej aflag = 1;
541 1.24 thorpej break;
542 1.24 thorpej
543 1.54 sommerfe case 'b':
544 1.54 sommerfe bflag = 1;
545 1.54 sommerfe break;
546 1.54 sommerfe
547 1.87 thorpej case 'C':
548 1.87 thorpej Cflag = 1;
549 1.87 thorpej break;
550 1.87 thorpej
551 1.34 lukem case 'd':
552 1.34 lukem dflag = 1;
553 1.34 lukem break;
554 1.169 rpaulo case 'h':
555 1.169 rpaulo hflag = 1;
556 1.169 rpaulo break;
557 1.53 itojun #ifdef INET6
558 1.53 itojun case 'L':
559 1.53 itojun Lflag = 1;
560 1.53 itojun break;
561 1.53 itojun #endif
562 1.53 itojun
563 1.31 thorpej case 'l':
564 1.31 thorpej lflag = 1;
565 1.31 thorpej break;
566 1.31 thorpej
567 1.24 thorpej case 'm':
568 1.24 thorpej mflag = 1;
569 1.24 thorpej break;
570 1.1 cgd
571 1.54 sommerfe case 's':
572 1.54 sommerfe sflag = 1;
573 1.54 sommerfe break;
574 1.54 sommerfe
575 1.34 lukem case 'u':
576 1.34 lukem uflag = 1;
577 1.34 lukem break;
578 1.34 lukem
579 1.124 matt case 'v':
580 1.124 matt vflag = 1;
581 1.124 matt break;
582 1.124 matt
583 1.134 perry case 'z':
584 1.134 perry zflag = 1;
585 1.134 perry break;
586 1.134 perry
587 1.54 sommerfe
588 1.24 thorpej default:
589 1.24 thorpej usage();
590 1.24 thorpej /* NOTREACHED */
591 1.24 thorpej }
592 1.181.4.1 mjf switch (ch) {
593 1.181.4.1 mjf case 'a':
594 1.181.4.1 mjf start = &opt_family_only.pb_parser;
595 1.181.4.1 mjf break;
596 1.181.4.1 mjf
597 1.181.4.1 mjf #ifdef INET6
598 1.181.4.1 mjf case 'L':
599 1.181.4.1 mjf #endif
600 1.181.4.1 mjf case 'm':
601 1.181.4.1 mjf case 'v':
602 1.181.4.1 mjf case 'z':
603 1.181.4.1 mjf if (start != &opt_family_only.pb_parser)
604 1.181.4.1 mjf start = &iface_opt_family_only.pif_parser;
605 1.181.4.1 mjf break;
606 1.181.4.1 mjf case 'C':
607 1.181.4.1 mjf start = &cloneterm.pt_parser;
608 1.181.4.1 mjf break;
609 1.181.4.1 mjf case 'l':
610 1.181.4.1 mjf start = &no_cmds.pt_parser;
611 1.181.4.1 mjf break;
612 1.181.4.1 mjf case 's':
613 1.181.4.1 mjf if (start != &no_cmds.pt_parser &&
614 1.181.4.1 mjf start != &opt_family_only.pb_parser)
615 1.181.4.1 mjf start = &iface_only.pif_parser;
616 1.181.4.1 mjf break;
617 1.181.4.1 mjf default:
618 1.181.4.1 mjf break;
619 1.181.4.1 mjf }
620 1.1 cgd }
621 1.24 thorpej argc -= optind;
622 1.24 thorpej argv += optind;
623 1.24 thorpej
624 1.31 thorpej /*
625 1.31 thorpej * -l means "list all interfaces", and is mutally exclusive with
626 1.31 thorpej * all other flags/commands.
627 1.31 thorpej *
628 1.87 thorpej * -C means "list all names of cloners", and it mutually exclusive
629 1.87 thorpej * with all other flags/commands.
630 1.87 thorpej *
631 1.31 thorpej * -a means "print status of all interfaces".
632 1.31 thorpej */
633 1.181.4.1 mjf if ((lflag || Cflag) && (aflag || mflag || vflag || zflag))
634 1.54 sommerfe usage();
635 1.54 sommerfe #ifdef INET6
636 1.87 thorpej if ((lflag || Cflag) && Lflag)
637 1.31 thorpej usage();
638 1.54 sommerfe #endif
639 1.87 thorpej if (lflag && Cflag)
640 1.87 thorpej usage();
641 1.24 thorpej
642 1.181.4.1 mjf nmatch = __arraycount(match);
643 1.181.4.1 mjf
644 1.181.4.1 mjf rc = parse(argc, argv, start, match, &nmatch, &narg);
645 1.181.4.1 mjf if (rc != 0)
646 1.24 thorpej usage();
647 1.24 thorpej
648 1.181.4.1 mjf if ((xenv = prop_dictionary_create()) == NULL)
649 1.181.4.1 mjf err(EXIT_FAILURE, "%s: prop_dictionary_create", __func__);
650 1.81 thorpej
651 1.181.4.1 mjf if (matches_exec(match, xenv, nmatch) == -1)
652 1.181.4.1 mjf err(EXIT_FAILURE, "exec_matches");
653 1.24 thorpej
654 1.181.4.1 mjf argc -= narg;
655 1.181.4.1 mjf argv += narg;
656 1.181.4.1 mjf
657 1.181.4.1 mjf env = (nmatch > 0) ? match[(int)nmatch - 1].m_env : NULL;
658 1.181.4.1 mjf if (env == NULL)
659 1.181.4.1 mjf env = xenv;
660 1.52 thorpej else
661 1.181.4.1 mjf env = prop_dictionary_augment(env, xenv);
662 1.24 thorpej
663 1.181.4.1 mjf /* Process any media commands that may have been issued. */
664 1.181.4.1 mjf process_media_commands(env);
665 1.24 thorpej
666 1.181.4.1 mjf af = getaf(env);
667 1.181.4.1 mjf switch (af) {
668 1.181.4.1 mjf #ifndef INET_ONLY
669 1.181.4.1 mjf case AF_APPLETALK:
670 1.181.4.1 mjf checkatrange(&at_addreq.ifra_addr);
671 1.181.4.1 mjf break;
672 1.181.4.1 mjf #endif /* INET_ONLY */
673 1.181.4.1 mjf default:
674 1.181.4.1 mjf break;
675 1.181.4.1 mjf case -1:
676 1.181.4.1 mjf af = AF_INET;
677 1.181.4.1 mjf break;
678 1.54 sommerfe }
679 1.54 sommerfe
680 1.181.4.1 mjf if ((s = getsock(af)) == -1)
681 1.181.4.1 mjf err(EXIT_FAILURE, "%s: getsock", __func__);
682 1.51 thorpej
683 1.181.4.1 mjf if ((ifname = getifname(env)) == NULL)
684 1.181.4.1 mjf err(EXIT_FAILURE, "%s: getifname", __func__);
685 1.24 thorpej
686 1.181.4.1 mjf if ((afp = lookup_af_bynum(af)) == NULL)
687 1.181.4.1 mjf errx(EXIT_FAILURE, "%s: lookup_af_bynum", __func__);
688 1.1 cgd
689 1.181.4.1 mjf if (afp->af_addr_commit != NULL) {
690 1.181.4.1 mjf (*afp->af_addr_commit)(env, xenv);
691 1.181.4.1 mjf } else {
692 1.181.4.1 mjf if (clearaddr) {
693 1.181.4.1 mjf estrlcpy(afp->af_ridreq, ifname, IFNAMSIZ);
694 1.181.4.1 mjf if (ioctl(s, afp->af_difaddr, afp->af_ridreq) == -1)
695 1.181.4.1 mjf err(EXIT_FAILURE, "SIOCDIFADDR");
696 1.55 darrenr }
697 1.181.4.1 mjf if (newaddr > 0) {
698 1.181.4.1 mjf estrlcpy(afp->af_addreq, ifname, IFNAMSIZ);
699 1.181.4.1 mjf if (ioctl(s, afp->af_aifaddr, afp->af_addreq) == -1)
700 1.181.4.1 mjf warn("SIOCAIFADDR");
701 1.181.4.1 mjf else if (check_up_state < 0)
702 1.181.4.1 mjf check_up_state = 1;
703 1.1 cgd }
704 1.1 cgd }
705 1.21 gwr
706 1.181.4.1 mjf do_setifpreference(env);
707 1.181.4.1 mjf do_setifcaps(env);
708 1.108 thorpej
709 1.173 martin if (check_up_state == 1)
710 1.181.4.1 mjf check_ifflags_up(env);
711 1.173 martin
712 1.181.4.1 mjf exit(EXIT_SUCCESS);
713 1.24 thorpej }
714 1.24 thorpej
715 1.160 thorpej const struct afswtch *
716 1.158 thorpej lookup_af_bynum(int afnum)
717 1.158 thorpej {
718 1.160 thorpej const struct afswtch *a;
719 1.158 thorpej
720 1.158 thorpej for (a = afs; a->af_name != NULL; a++)
721 1.158 thorpej if (a->af_af == afnum)
722 1.158 thorpej return (a);
723 1.158 thorpej return (NULL);
724 1.158 thorpej }
725 1.158 thorpej
726 1.13 mycroft void
727 1.181.4.1 mjf printall(const char *ifname, prop_dictionary_t env0)
728 1.5 deraadt {
729 1.78 itojun struct ifaddrs *ifap, *ifa;
730 1.181.4.1 mjf struct ifreq ifr;
731 1.94 itojun const struct sockaddr_dl *sdl = NULL;
732 1.181.4.1 mjf prop_dictionary_t env, oenv;
733 1.78 itojun int idx;
734 1.78 itojun char *p;
735 1.78 itojun
736 1.181.4.1 mjf if (env0 == NULL)
737 1.181.4.1 mjf env = prop_dictionary_create();
738 1.181.4.1 mjf else
739 1.181.4.1 mjf env = prop_dictionary_copy_mutable(env0);
740 1.181.4.1 mjf
741 1.181.4.1 mjf oenv = prop_dictionary_create();
742 1.181.4.1 mjf
743 1.181.4.1 mjf if (env == NULL || oenv == NULL)
744 1.181.4.1 mjf errx(EXIT_FAILURE, "%s: prop_dictionary_copy/create", __func__);
745 1.181.4.1 mjf
746 1.78 itojun if (getifaddrs(&ifap) != 0)
747 1.120 atatat err(EXIT_FAILURE, "getifaddrs");
748 1.78 itojun p = NULL;
749 1.78 itojun idx = 0;
750 1.78 itojun for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
751 1.181.4.1 mjf memset(&ifr, 0, sizeof(ifr));
752 1.181.4.1 mjf estrlcpy(ifr.ifr_name, ifa->ifa_name, sizeof(ifr.ifr_name));
753 1.181.4.1 mjf if (sizeof(ifr.ifr_addr) >= ifa->ifa_addr->sa_len) {
754 1.181.4.1 mjf memcpy(&ifr.ifr_addr, ifa->ifa_addr,
755 1.78 itojun ifa->ifa_addr->sa_len);
756 1.78 itojun }
757 1.78 itojun
758 1.181.4.1 mjf if (ifname != NULL && strcmp(ifname, ifa->ifa_name) != 0)
759 1.107 itojun continue;
760 1.78 itojun if (ifa->ifa_addr->sa_family == AF_LINK)
761 1.78 itojun sdl = (const struct sockaddr_dl *) ifa->ifa_addr;
762 1.78 itojun if (p && strcmp(p, ifa->ifa_name) == 0)
763 1.78 itojun continue;
764 1.181.4.1 mjf if (!prop_dictionary_set_cstring(env, "if", ifa->ifa_name))
765 1.140 itojun continue;
766 1.78 itojun p = ifa->ifa_name;
767 1.78 itojun
768 1.135 lukem if (bflag && (ifa->ifa_flags & IFF_BROADCAST) == 0)
769 1.78 itojun continue;
770 1.78 itojun if (dflag && (ifa->ifa_flags & IFF_UP) != 0)
771 1.78 itojun continue;
772 1.78 itojun if (uflag && (ifa->ifa_flags & IFF_UP) == 0)
773 1.78 itojun continue;
774 1.78 itojun
775 1.181.4.1 mjf if (sflag && carrier(env))
776 1.78 itojun continue;
777 1.78 itojun idx++;
778 1.78 itojun /*
779 1.78 itojun * Are we just listing the interfaces?
780 1.78 itojun */
781 1.78 itojun if (lflag) {
782 1.78 itojun if (idx > 1)
783 1.147 dsl printf(" ");
784 1.181.4.1 mjf fputs(ifa->ifa_name, stdout);
785 1.78 itojun continue;
786 1.78 itojun }
787 1.78 itojun
788 1.181.4.1 mjf status(sdl, env, oenv);
789 1.119 bjh21 sdl = NULL;
790 1.78 itojun }
791 1.78 itojun if (lflag)
792 1.147 dsl printf("\n");
793 1.181.4.1 mjf prop_object_release((prop_object_t)env);
794 1.181.4.1 mjf prop_object_release((prop_object_t)oenv);
795 1.78 itojun freeifaddrs(ifap);
796 1.87 thorpej }
797 1.87 thorpej
798 1.181.4.1 mjf int
799 1.181.4.1 mjf list_cloners(prop_dictionary_t env, prop_dictionary_t oenv)
800 1.87 thorpej {
801 1.87 thorpej struct if_clonereq ifcr;
802 1.87 thorpej char *cp, *buf;
803 1.181.4.1 mjf int idx, s;
804 1.87 thorpej
805 1.87 thorpej memset(&ifcr, 0, sizeof(ifcr));
806 1.87 thorpej
807 1.181.4.1 mjf s = getsock(AF_INET);
808 1.87 thorpej
809 1.120 atatat if (ioctl(s, SIOCIFGCLONERS, &ifcr) == -1)
810 1.120 atatat err(EXIT_FAILURE, "SIOCIFGCLONERS for count");
811 1.87 thorpej
812 1.87 thorpej buf = malloc(ifcr.ifcr_total * IFNAMSIZ);
813 1.87 thorpej if (buf == NULL)
814 1.120 atatat err(EXIT_FAILURE, "unable to allocate cloner name buffer");
815 1.87 thorpej
816 1.87 thorpej ifcr.ifcr_count = ifcr.ifcr_total;
817 1.87 thorpej ifcr.ifcr_buffer = buf;
818 1.87 thorpej
819 1.120 atatat if (ioctl(s, SIOCIFGCLONERS, &ifcr) == -1)
820 1.120 atatat err(EXIT_FAILURE, "SIOCIFGCLONERS for names");
821 1.87 thorpej
822 1.87 thorpej /*
823 1.87 thorpej * In case some disappeared in the mean time, clamp it down.
824 1.87 thorpej */
825 1.87 thorpej if (ifcr.ifcr_count > ifcr.ifcr_total)
826 1.87 thorpej ifcr.ifcr_count = ifcr.ifcr_total;
827 1.87 thorpej
828 1.87 thorpej for (cp = buf, idx = 0; idx < ifcr.ifcr_count; idx++, cp += IFNAMSIZ) {
829 1.87 thorpej if (idx > 0)
830 1.147 dsl printf(" ");
831 1.87 thorpej printf("%s", cp);
832 1.87 thorpej }
833 1.87 thorpej
834 1.147 dsl printf("\n");
835 1.87 thorpej free(buf);
836 1.181.4.1 mjf exit(EXIT_SUCCESS);
837 1.5 deraadt }
838 1.5 deraadt
839 1.181.4.1 mjf static int
840 1.181.4.1 mjf clone_command(prop_dictionary_t env, prop_dictionary_t xenv)
841 1.81 thorpej {
842 1.181.4.1 mjf int64_t cmd;
843 1.81 thorpej
844 1.181.4.1 mjf if (!prop_dictionary_get_int64(env, "clonecmd", &cmd)) {
845 1.181.4.1 mjf errno = ENOENT;
846 1.181.4.1 mjf return -1;
847 1.181.4.1 mjf }
848 1.81 thorpej
849 1.181.4.1 mjf if (indirect_ioctl(env, (unsigned long)cmd, NULL) == -1) {
850 1.181.4.1 mjf warn("%s", __func__);
851 1.181.4.1 mjf return -1;
852 1.181.4.1 mjf }
853 1.181.4.1 mjf return 0;
854 1.81 thorpej }
855 1.81 thorpej
856 1.81 thorpej /*ARGSUSED*/
857 1.181.4.1 mjf int
858 1.181.4.1 mjf setifaddr(prop_dictionary_t env, prop_dictionary_t xenv)
859 1.81 thorpej {
860 1.181.4.1 mjf struct ifreq *ifr; /* XXX */
861 1.181.4.1 mjf const struct paddr_prefix *pfx0;
862 1.181.4.1 mjf struct paddr_prefix *pfx;
863 1.181.4.1 mjf prop_data_t d;
864 1.181.4.1 mjf const char *ifname;
865 1.181.4.1 mjf int af, s;
866 1.181.4.1 mjf const struct afswtch *afp;
867 1.81 thorpej
868 1.181.4.1 mjf if ((af = getaf(env)) == -1)
869 1.181.4.1 mjf af = AF_INET;
870 1.81 thorpej
871 1.181.4.1 mjf if ((afp = lookup_af_bynum(af)) == NULL)
872 1.181.4.1 mjf return -1;
873 1.181.4.1 mjf
874 1.181.4.1 mjf d = (prop_data_t)prop_dictionary_get(env, "address");
875 1.181.4.1 mjf assert(d != NULL);
876 1.181.4.1 mjf pfx0 = prop_data_data_nocopy(d);
877 1.181.4.1 mjf
878 1.181.4.1 mjf if (pfx0->pfx_len >= 0) {
879 1.181.4.1 mjf pfx = prefixlen_to_mask(af, pfx0->pfx_len);
880 1.181.4.1 mjf if (pfx == NULL)
881 1.181.4.1 mjf err(EXIT_FAILURE, "prefixlen_to_mask");
882 1.181.4.1 mjf
883 1.181.4.1 mjf if (afp->af_getaddr != NULL)
884 1.181.4.1 mjf (*afp->af_getaddr)(pfx, MASK);
885 1.181.4.1 mjf free(pfx);
886 1.181.4.1 mjf }
887 1.181.4.1 mjf
888 1.181.4.1 mjf if (afp->af_addr_commit != NULL)
889 1.181.4.1 mjf return 0;
890 1.181.4.1 mjf
891 1.181.4.1 mjf if ((ifname = getifname(env)) == NULL)
892 1.181.4.1 mjf return -1;
893 1.76 enami
894 1.1 cgd /*
895 1.1 cgd * Delay the ioctl to set the interface addr until flags are all set.
896 1.1 cgd * The address interpretation may depend on the flags,
897 1.1 cgd * and the flags may change when the address is set.
898 1.1 cgd */
899 1.1 cgd setaddr++;
900 1.36 lukem if (newaddr == -1)
901 1.36 lukem newaddr = 1;
902 1.76 enami if (doalias == 0 && afp->af_gifaddr != 0) {
903 1.181.4.1 mjf ifr = (struct ifreq *)afp->af_ridreq;
904 1.181.4.1 mjf estrlcpy(ifr->ifr_name, ifname, sizeof(ifr->ifr_name));
905 1.181.4.1 mjf ifr->ifr_addr.sa_family = af;
906 1.181.4.1 mjf
907 1.181.4.1 mjf if ((s = getsock(af)) == -1)
908 1.181.4.1 mjf err(EXIT_FAILURE, "getsock");
909 1.181.4.1 mjf
910 1.76 enami if (ioctl(s, afp->af_gifaddr, afp->af_ridreq) == 0)
911 1.76 enami clearaddr = 1;
912 1.76 enami else if (errno == EADDRNOTAVAIL)
913 1.76 enami /* No address was assigned yet. */
914 1.76 enami ;
915 1.76 enami else
916 1.120 atatat err(EXIT_FAILURE, "SIOCGIFADDR");
917 1.76 enami }
918 1.76 enami
919 1.181.4.1 mjf #if 0
920 1.181.4.1 mjf int i;
921 1.181.4.1 mjf for (i = 0; i < pfx0->pfx_addr.sa_len; i++)
922 1.181.4.1 mjf printf(" %02x", ((const uint8_t *)&pfx->pfx_addr)[i]);
923 1.181.4.1 mjf printf("\n");
924 1.181.4.1 mjf #endif
925 1.181.4.1 mjf if (afp->af_getaddr != NULL)
926 1.181.4.1 mjf (*afp->af_getaddr)(pfx0, (doalias >= 0 ? ADDR : RIDADDR));
927 1.181.4.1 mjf return 0;
928 1.1 cgd }
929 1.1 cgd
930 1.181.4.1 mjf int
931 1.181.4.1 mjf setifnetmask(prop_dictionary_t env, prop_dictionary_t xenv)
932 1.1 cgd {
933 1.181.4.1 mjf const struct paddr_prefix *pfx;
934 1.181.4.1 mjf prop_data_t d;
935 1.181.4.1 mjf int af;
936 1.181.4.1 mjf const struct afswtch *afp;
937 1.181.4.1 mjf
938 1.181.4.1 mjf if ((af = getaf(env)) == -1)
939 1.181.4.1 mjf af = AF_INET;
940 1.181.4.1 mjf
941 1.181.4.1 mjf if ((afp = lookup_af_bynum(af)) == NULL)
942 1.181.4.1 mjf return -1;
943 1.181.4.1 mjf
944 1.181.4.1 mjf d = (prop_data_t)prop_dictionary_get(env, "dstormask");
945 1.181.4.1 mjf assert(d != NULL);
946 1.181.4.1 mjf pfx = prop_data_data_nocopy(d);
947 1.181.4.1 mjf
948 1.181.4.1 mjf if (!prop_dictionary_set(xenv, "netmask", (prop_object_t)d))
949 1.181.4.1 mjf return -1;
950 1.181.4.1 mjf
951 1.181.4.1 mjf if (afp->af_getaddr != NULL)
952 1.181.4.1 mjf (*afp->af_getaddr)(pfx, MASK);
953 1.181.4.1 mjf return 0;
954 1.1 cgd }
955 1.1 cgd
956 1.181.4.1 mjf int
957 1.181.4.1 mjf setifbroadaddr(prop_dictionary_t env, prop_dictionary_t xenv)
958 1.1 cgd {
959 1.181.4.1 mjf const struct paddr_prefix *pfx;
960 1.181.4.1 mjf prop_data_t d;
961 1.181.4.1 mjf int af;
962 1.181.4.1 mjf const struct afswtch *afp;
963 1.181.4.1 mjf unsigned short flags;
964 1.181.4.1 mjf
965 1.181.4.1 mjf if ((af = getaf(env)) == -1)
966 1.181.4.1 mjf af = AF_INET;
967 1.181.4.1 mjf
968 1.181.4.1 mjf if ((afp = lookup_af_bynum(af)) == NULL)
969 1.181.4.1 mjf return -1;
970 1.181.4.1 mjf
971 1.181.4.1 mjf if (getifflags(env, xenv, &flags) == -1)
972 1.181.4.1 mjf err(EXIT_FAILURE, "%s: getifflags", __func__);
973 1.181.4.1 mjf
974 1.181.4.1 mjf if ((flags & IFF_BROADCAST) == 0)
975 1.181.4.1 mjf errx(EXIT_FAILURE, "not a broadcast interface");
976 1.181.4.1 mjf
977 1.181.4.1 mjf d = (prop_data_t)prop_dictionary_get(env, "broadcast");
978 1.181.4.1 mjf assert(d != NULL);
979 1.181.4.1 mjf pfx = prop_data_data_nocopy(d);
980 1.181.4.1 mjf
981 1.181.4.1 mjf if (!prop_dictionary_set(xenv, "broadcast", (prop_object_t)d))
982 1.181.4.1 mjf return -1;
983 1.181.4.1 mjf
984 1.181.4.1 mjf if (afp->af_getaddr != NULL)
985 1.181.4.1 mjf (*afp->af_getaddr)(pfx, DSTADDR);
986 1.181.4.1 mjf
987 1.181.4.1 mjf return 0;
988 1.1 cgd }
989 1.1 cgd
990 1.181.4.1 mjf #define rqtosa(__afp, __x) (&(((struct ifreq *)(__afp->__x))->ifr_addr))
991 1.181.4.1 mjf
992 1.181.4.1 mjf int
993 1.181.4.1 mjf notealias(prop_dictionary_t env, prop_dictionary_t xenv)
994 1.1 cgd {
995 1.181.4.1 mjf bool alias, delete;
996 1.181.4.1 mjf int af;
997 1.181.4.1 mjf const struct afswtch *afp;
998 1.181.4.1 mjf
999 1.181.4.1 mjf if ((af = getaf(env)) == -1)
1000 1.181.4.1 mjf af = AF_INET;
1001 1.181.4.1 mjf
1002 1.181.4.1 mjf if ((afp = lookup_af_bynum(af)) == NULL)
1003 1.181.4.1 mjf return -1;
1004 1.181.4.1 mjf
1005 1.181.4.1 mjf if (afp->af_addr_commit != NULL)
1006 1.181.4.1 mjf return 0;
1007 1.181.4.1 mjf
1008 1.181.4.1 mjf if (!prop_dictionary_get_bool(env, "alias", &alias)) {
1009 1.181.4.1 mjf errno = ENOENT;
1010 1.181.4.1 mjf return -1;
1011 1.181.4.1 mjf }
1012 1.181.4.1 mjf delete = !alias;
1013 1.181.4.1 mjf if (setaddr && doalias == 0 && delete)
1014 1.181.4.1 mjf memcpy(rqtosa(afp, af_ridreq), rqtosa(afp, af_addreq),
1015 1.181.4.1 mjf rqtosa(afp, af_addreq)->sa_len);
1016 1.181.4.1 mjf doalias = delete ? -1 : 1;
1017 1.181.4.1 mjf if (delete) {
1018 1.1 cgd clearaddr = 1;
1019 1.1 cgd newaddr = 0;
1020 1.113 david } else {
1021 1.1 cgd clearaddr = 0;
1022 1.113 david }
1023 1.181.4.1 mjf return 0;
1024 1.1 cgd }
1025 1.1 cgd
1026 1.1 cgd /*ARGSUSED*/
1027 1.181.4.1 mjf int
1028 1.181.4.1 mjf notrailers(prop_dictionary_t env, prop_dictionary_t xenv)
1029 1.13 mycroft {
1030 1.34 lukem puts("Note: trailers are no longer sent, but always received");
1031 1.181.4.1 mjf return 0;
1032 1.13 mycroft }
1033 1.13 mycroft
1034 1.13 mycroft /*ARGSUSED*/
1035 1.181.4.1 mjf int
1036 1.181.4.1 mjf setifdstormask(prop_dictionary_t env, prop_dictionary_t xenv)
1037 1.1 cgd {
1038 1.181.4.1 mjf const char *key;
1039 1.181.4.1 mjf const struct paddr_prefix *pfx;
1040 1.181.4.1 mjf prop_data_t d;
1041 1.181.4.1 mjf int af, which;
1042 1.181.4.1 mjf const struct afswtch *afp;
1043 1.181.4.1 mjf unsigned short flags;
1044 1.181.4.1 mjf
1045 1.181.4.1 mjf if ((af = getaf(env)) == -1)
1046 1.181.4.1 mjf af = AF_INET;
1047 1.181.4.1 mjf
1048 1.181.4.1 mjf if ((afp = lookup_af_bynum(af)) == NULL)
1049 1.181.4.1 mjf return -1;
1050 1.181.4.1 mjf
1051 1.181.4.1 mjf if (getifflags(env, xenv, &flags) == -1)
1052 1.181.4.1 mjf err(EXIT_FAILURE, "%s: getifflags", __func__);
1053 1.181.4.1 mjf
1054 1.181.4.1 mjf d = (prop_data_t)prop_dictionary_get(env, "dstormask");
1055 1.181.4.1 mjf assert(d != NULL);
1056 1.181.4.1 mjf pfx = prop_data_data_nocopy(d);
1057 1.181.4.1 mjf
1058 1.181.4.1 mjf if ((flags & IFF_BROADCAST) == 0) {
1059 1.181.4.1 mjf key = "dst";
1060 1.181.4.1 mjf which = DSTADDR;
1061 1.181.4.1 mjf } else {
1062 1.181.4.1 mjf key = "netmask";
1063 1.181.4.1 mjf which = MASK;
1064 1.181.4.1 mjf }
1065 1.181.4.1 mjf
1066 1.181.4.1 mjf if (!prop_dictionary_set(xenv, key, (prop_object_t)d))
1067 1.181.4.1 mjf return -1;
1068 1.181.4.1 mjf
1069 1.181.4.1 mjf if (afp->af_getaddr != NULL)
1070 1.181.4.1 mjf (*afp->af_getaddr)(pfx, which);
1071 1.181.4.1 mjf return 0;
1072 1.1 cgd }
1073 1.1 cgd
1074 1.16 cgd void
1075 1.181.4.1 mjf check_ifflags_up(prop_dictionary_t env)
1076 1.173 martin {
1077 1.181.4.1 mjf struct ifreq ifr;
1078 1.173 martin
1079 1.181.4.1 mjf if (direct_ioctl(env, SIOCGIFFLAGS, &ifr) == -1)
1080 1.173 martin err(EXIT_FAILURE, "SIOCGIFFLAGS");
1081 1.181.4.1 mjf if (ifr.ifr_flags & IFF_UP)
1082 1.173 martin return;
1083 1.181.4.1 mjf ifr.ifr_flags |= IFF_UP;
1084 1.181.4.1 mjf if (direct_ioctl(env, SIOCSIFFLAGS, &ifr) == -1)
1085 1.173 martin err(EXIT_FAILURE, "SIOCSIFFLAGS");
1086 1.173 martin }
1087 1.173 martin
1088 1.181.4.1 mjf int
1089 1.181.4.1 mjf setifflags(prop_dictionary_t env, prop_dictionary_t xenv)
1090 1.1 cgd {
1091 1.181.4.1 mjf struct ifreq ifr;
1092 1.181.4.1 mjf int64_t ifflag;
1093 1.181.4.1 mjf bool rc;
1094 1.79 enami
1095 1.181.4.1 mjf rc = prop_dictionary_get_int64(env, "ifflag", &ifflag);
1096 1.181.4.1 mjf assert(rc);
1097 1.1 cgd
1098 1.181.4.1 mjf if (direct_ioctl(env, SIOCGIFFLAGS, &ifr) == -1)
1099 1.181.4.1 mjf return -1;
1100 1.181.4.1 mjf
1101 1.181.4.1 mjf if (ifflag < 0) {
1102 1.181.4.1 mjf ifflag = -ifflag;
1103 1.181.4.1 mjf if (ifflag == IFF_UP)
1104 1.173 martin check_up_state = 0;
1105 1.181.4.1 mjf ifr.ifr_flags &= ~ifflag;
1106 1.1 cgd } else
1107 1.181.4.1 mjf ifr.ifr_flags |= ifflag;
1108 1.181.4.1 mjf
1109 1.181.4.1 mjf if (direct_ioctl(env, SIOCSIFFLAGS, &ifr) == -1)
1110 1.181.4.1 mjf return -1;
1111 1.181.4.1 mjf
1112 1.181.4.1 mjf return 0;
1113 1.1 cgd }
1114 1.1 cgd
1115 1.181.4.1 mjf static int
1116 1.181.4.1 mjf getifcaps(prop_dictionary_t env, prop_dictionary_t oenv, struct ifcapreq *oifcr)
1117 1.108 thorpej {
1118 1.181.4.1 mjf bool rc;
1119 1.181.4.1 mjf struct ifcapreq ifcr;
1120 1.181.4.1 mjf const struct ifcapreq *tmpifcr;
1121 1.181.4.1 mjf prop_data_t capdata;
1122 1.108 thorpej
1123 1.181.4.1 mjf capdata = (prop_data_t)prop_dictionary_get(env, "ifcaps");
1124 1.181.4.1 mjf
1125 1.181.4.1 mjf if (capdata != NULL) {
1126 1.181.4.1 mjf tmpifcr = prop_data_data_nocopy(capdata);
1127 1.181.4.1 mjf *oifcr = *tmpifcr;
1128 1.181.4.1 mjf return 0;
1129 1.181.4.1 mjf }
1130 1.181.4.1 mjf
1131 1.181.4.1 mjf (void)direct_ioctl(env, SIOCGIFCAP, &ifcr);
1132 1.181.4.1 mjf *oifcr = ifcr;
1133 1.181.4.1 mjf
1134 1.181.4.1 mjf capdata = prop_data_create_data(&ifcr, sizeof(ifcr));
1135 1.181.4.1 mjf
1136 1.181.4.1 mjf rc = prop_dictionary_set(oenv, "ifcaps", capdata);
1137 1.181.4.1 mjf
1138 1.181.4.1 mjf prop_object_release((prop_object_t)capdata);
1139 1.108 thorpej
1140 1.181.4.1 mjf return rc ? 0 : -1;
1141 1.108 thorpej }
1142 1.108 thorpej
1143 1.181.4.1 mjf static int
1144 1.181.4.1 mjf setifcaps(prop_dictionary_t env, prop_dictionary_t oenv)
1145 1.1 cgd {
1146 1.181.4.1 mjf int64_t ifcap;
1147 1.181.4.1 mjf int s;
1148 1.181.4.1 mjf bool rc;
1149 1.181.4.1 mjf prop_data_t capdata;
1150 1.181.4.1 mjf const char *ifname;
1151 1.181.4.1 mjf struct ifcapreq ifcr;
1152 1.125 itojun
1153 1.181.4.1 mjf s = getsock(AF_INET);
1154 1.181.4.1 mjf
1155 1.181.4.1 mjf rc = prop_dictionary_get_int64(env, "ifcap", &ifcap);
1156 1.181.4.1 mjf assert(rc);
1157 1.181.4.1 mjf
1158 1.181.4.1 mjf if ((ifname = getifname(env)) == NULL)
1159 1.181.4.1 mjf return -1;
1160 1.181.4.1 mjf
1161 1.181.4.1 mjf if (getifcaps(env, oenv, &ifcr) == -1)
1162 1.181.4.1 mjf return -1;
1163 1.181.4.1 mjf
1164 1.181.4.1 mjf if (ifcap < 0) {
1165 1.181.4.1 mjf ifcap = -ifcap;
1166 1.181.4.1 mjf ifcr.ifcr_capenable &= ~ifcap;
1167 1.181.4.1 mjf } else
1168 1.181.4.1 mjf ifcr.ifcr_capenable |= ifcap;
1169 1.181.4.1 mjf
1170 1.181.4.1 mjf if ((capdata = prop_data_create_data(&ifcr, sizeof(ifcr))) == NULL)
1171 1.181.4.1 mjf return -1;
1172 1.181.4.1 mjf
1173 1.181.4.1 mjf rc = prop_dictionary_set(oenv, "ifcaps", capdata);
1174 1.181.4.1 mjf prop_object_release((prop_object_t)capdata);
1175 1.181.4.1 mjf
1176 1.181.4.1 mjf return rc ? 0 : -1;
1177 1.1 cgd }
1178 1.1 cgd
1179 1.181.4.1 mjf static int
1180 1.181.4.1 mjf setifmetric(prop_dictionary_t env, prop_dictionary_t xenv)
1181 1.178 dyoung {
1182 1.181.4.1 mjf struct ifreq ifr;
1183 1.181.4.1 mjf bool rc;
1184 1.181.4.1 mjf int64_t metric;
1185 1.181.4.1 mjf
1186 1.181.4.1 mjf rc = prop_dictionary_get_int64(env, "metric", &metric);
1187 1.181.4.1 mjf assert(rc);
1188 1.181.4.1 mjf
1189 1.181.4.1 mjf ifr.ifr_metric = metric;
1190 1.181.4.1 mjf if (direct_ioctl(env, SIOCSIFMETRIC, &ifr) == -1)
1191 1.181.4.1 mjf warn("SIOCSIFMETRIC");
1192 1.181.4.1 mjf return 0;
1193 1.178 dyoung }
1194 1.178 dyoung
1195 1.181.4.1 mjf static void
1196 1.181.4.1 mjf do_setifpreference(prop_dictionary_t env)
1197 1.178 dyoung {
1198 1.178 dyoung struct if_addrprefreq ifap;
1199 1.181.4.1 mjf int af;
1200 1.181.4.1 mjf const struct afswtch *afp;
1201 1.181.4.1 mjf prop_data_t d;
1202 1.181.4.1 mjf const struct paddr_prefix *pfx;
1203 1.178 dyoung
1204 1.181.4.1 mjf if ((af = getaf(env)) == -1)
1205 1.181.4.1 mjf af = AF_INET;
1206 1.125 itojun
1207 1.181.4.1 mjf if ((afp = lookup_af_bynum(af)) == NULL)
1208 1.181.4.1 mjf errx(EXIT_FAILURE, "%s: lookup_af_bynum", __func__);
1209 1.33 is
1210 1.181.4.1 mjf memset(&ifap, 0, sizeof(ifap));
1211 1.181.4.1 mjf
1212 1.181.4.1 mjf if (!prop_dictionary_get_int16(env, "preference",
1213 1.181.4.1 mjf &ifap.ifap_preference))
1214 1.181.4.1 mjf return;
1215 1.181.4.1 mjf
1216 1.181.4.1 mjf d = (prop_data_t)prop_dictionary_get(env, "address");
1217 1.181.4.1 mjf assert(d != NULL);
1218 1.181.4.1 mjf
1219 1.181.4.1 mjf pfx = prop_data_data_nocopy(d);
1220 1.181.4.1 mjf
1221 1.181.4.1 mjf memcpy(&ifap.ifap_addr, &pfx->pfx_addr,
1222 1.181.4.1 mjf MIN(sizeof(ifap.ifap_addr), pfx->pfx_addr.sa_len));
1223 1.181.4.1 mjf if (direct_ioctl(env, SIOCSIFADDRPREF, &ifap) == -1)
1224 1.181.4.1 mjf warn("SIOCSIFADDRPREF");
1225 1.88 onoe }
1226 1.88 onoe
1227 1.181.4.1 mjf static int
1228 1.181.4.1 mjf setifmtu(prop_dictionary_t env, prop_dictionary_t xenv)
1229 1.88 onoe {
1230 1.181.4.1 mjf int64_t mtu;
1231 1.181.4.1 mjf bool rc;
1232 1.181.4.1 mjf struct ifreq ifr;
1233 1.88 onoe
1234 1.181.4.1 mjf rc = prop_dictionary_get_int64(env, "mtu", &mtu);
1235 1.181.4.1 mjf assert(rc);
1236 1.181.4.1 mjf
1237 1.181.4.1 mjf ifr.ifr_mtu = mtu;
1238 1.181.4.1 mjf if (direct_ioctl(env, SIOCSIFMTU, &ifr) == -1)
1239 1.181.4.1 mjf warn("SIOCSIFMTU");
1240 1.181.4.1 mjf
1241 1.181.4.1 mjf return 0;
1242 1.88 onoe }
1243 1.88 onoe
1244 1.147 dsl static void
1245 1.147 dsl media_error(int type, const char *val, const char *opt)
1246 1.147 dsl {
1247 1.147 dsl errx(EXIT_FAILURE, "unknown %s media %s: %s",
1248 1.147 dsl get_media_type_string(type), opt, val);
1249 1.147 dsl }
1250 1.147 dsl
1251 1.62 chopps void
1252 1.181.4.1 mjf init_current_media(prop_dictionary_t env, prop_dictionary_t oenv)
1253 1.24 thorpej {
1254 1.181.4.1 mjf const char *ifname;
1255 1.24 thorpej struct ifmediareq ifmr;
1256 1.24 thorpej
1257 1.181.4.1 mjf if ((ifname = getifname(env)) == NULL)
1258 1.181.4.1 mjf err(EXIT_FAILURE, "getifname");
1259 1.181.4.1 mjf
1260 1.45 thorpej /*
1261 1.45 thorpej * If we have not yet done so, grab the currently-selected
1262 1.45 thorpej * media.
1263 1.45 thorpej */
1264 1.45 thorpej
1265 1.181.4.1 mjf if (prop_dictionary_get(env, "initmedia") == NULL) {
1266 1.181.4.1 mjf memset(&ifmr, 0, sizeof(ifmr));
1267 1.181.4.1 mjf
1268 1.181.4.1 mjf if (direct_ioctl(env, SIOCGIFMEDIA, &ifmr) == -1) {
1269 1.45 thorpej /*
1270 1.45 thorpej * If we get E2BIG, the kernel is telling us
1271 1.45 thorpej * that there are more, so we can ignore it.
1272 1.45 thorpej */
1273 1.45 thorpej if (errno != E2BIG)
1274 1.181.4.1 mjf err(EXIT_FAILURE, "SIOCGIFMEDIA");
1275 1.45 thorpej }
1276 1.24 thorpej
1277 1.181.4.1 mjf if (!prop_dictionary_set_bool(oenv, "initmedia", true)) {
1278 1.181.4.1 mjf err(EXIT_FAILURE, "%s: prop_dictionary_set_bool",
1279 1.181.4.1 mjf __func__);
1280 1.181.4.1 mjf }
1281 1.45 thorpej media_current = ifmr.ifm_current;
1282 1.24 thorpej }
1283 1.24 thorpej
1284 1.45 thorpej /* Sanity. */
1285 1.45 thorpej if (IFM_TYPE(media_current) == 0)
1286 1.181.4.1 mjf errx(EXIT_FAILURE, "%s: no link type?", ifname);
1287 1.45 thorpej }
1288 1.45 thorpej
1289 1.45 thorpej void
1290 1.181.4.1 mjf process_media_commands(prop_dictionary_t env)
1291 1.45 thorpej {
1292 1.181.4.1 mjf struct ifreq ifr;
1293 1.45 thorpej
1294 1.181.4.1 mjf if (prop_dictionary_get(env, "media") == NULL &&
1295 1.181.4.1 mjf prop_dictionary_get(env, "mediaopt") == NULL &&
1296 1.181.4.1 mjf prop_dictionary_get(env, "unmediaopt") == NULL &&
1297 1.181.4.1 mjf prop_dictionary_get(env, "mediamode") == NULL) {
1298 1.45 thorpej /* Nothing to do. */
1299 1.45 thorpej return;
1300 1.45 thorpej }
1301 1.24 thorpej
1302 1.24 thorpej /*
1303 1.45 thorpej * Media already set up, and commands sanity-checked. Set/clear
1304 1.45 thorpej * any options, and we're ready to go.
1305 1.24 thorpej */
1306 1.45 thorpej media_current |= mediaopt_set;
1307 1.45 thorpej media_current &= ~mediaopt_clear;
1308 1.24 thorpej
1309 1.181.4.1 mjf memset(&ifr, 0, sizeof(ifr));
1310 1.45 thorpej ifr.ifr_media = media_current;
1311 1.24 thorpej
1312 1.181.4.1 mjf if (direct_ioctl(env, SIOCSIFMEDIA, &ifr) == -1)
1313 1.120 atatat err(EXIT_FAILURE, "SIOCSIFMEDIA");
1314 1.24 thorpej }
1315 1.24 thorpej
1316 1.181.4.1 mjf int
1317 1.181.4.1 mjf setmedia(prop_dictionary_t env, prop_dictionary_t xenv)
1318 1.24 thorpej {
1319 1.45 thorpej int type, subtype, inst;
1320 1.181.4.1 mjf prop_data_t data;
1321 1.181.4.1 mjf char *val;
1322 1.45 thorpej
1323 1.181.4.1 mjf init_current_media(env, xenv);
1324 1.45 thorpej
1325 1.181.4.1 mjf data = (prop_data_t)prop_dictionary_get(env, "media");
1326 1.181.4.1 mjf assert(data != NULL);
1327 1.24 thorpej
1328 1.181.4.1 mjf /* Only one media command may be given. */
1329 1.139 dyoung /* Must not come after mode commands */
1330 1.45 thorpej /* Must not come after mediaopt commands */
1331 1.45 thorpej
1332 1.47 thorpej /*
1333 1.47 thorpej * No need to check if `instance' has been issued; setmediainst()
1334 1.47 thorpej * craps out if `media' has not been specified.
1335 1.47 thorpej */
1336 1.47 thorpej
1337 1.45 thorpej type = IFM_TYPE(media_current);
1338 1.45 thorpej inst = IFM_INST(media_current);
1339 1.45 thorpej
1340 1.181.4.1 mjf val = strndup(prop_data_data_nocopy(data), prop_data_size(data));
1341 1.181.4.1 mjf if (val == NULL)
1342 1.181.4.1 mjf return -1;
1343 1.181.4.1 mjf
1344 1.45 thorpej /* Look up the subtype. */
1345 1.45 thorpej subtype = get_media_subtype(type, val);
1346 1.147 dsl if (subtype == -1)
1347 1.147 dsl media_error(type, val, "subtype");
1348 1.24 thorpej
1349 1.45 thorpej /* Build the new current media word. */
1350 1.45 thorpej media_current = IFM_MAKEWORD(type, subtype, 0, inst);
1351 1.24 thorpej
1352 1.45 thorpej /* Media will be set after other processing is complete. */
1353 1.181.4.1 mjf return 0;
1354 1.24 thorpej }
1355 1.24 thorpej
1356 1.181.4.1 mjf int
1357 1.181.4.1 mjf setmediaopt(prop_dictionary_t env, prop_dictionary_t xenv)
1358 1.24 thorpej {
1359 1.147 dsl char *invalid;
1360 1.181.4.1 mjf prop_data_t data;
1361 1.181.4.1 mjf char *val;
1362 1.24 thorpej
1363 1.181.4.1 mjf init_current_media(env, xenv);
1364 1.24 thorpej
1365 1.181.4.1 mjf data = (prop_data_t)prop_dictionary_get(env, "mediaopt");
1366 1.181.4.1 mjf assert(data != NULL);
1367 1.26 thorpej
1368 1.181.4.1 mjf /* Can only issue `mediaopt' once. */
1369 1.47 thorpej /* Can't issue `mediaopt' if `instance' has already been issued. */
1370 1.181.4.1 mjf
1371 1.181.4.1 mjf val = strndup(prop_data_data_nocopy(data), prop_data_size(data));
1372 1.181.4.1 mjf if (val == NULL)
1373 1.181.4.1 mjf return -1;
1374 1.47 thorpej
1375 1.147 dsl mediaopt_set = get_media_options(media_current, val, &invalid);
1376 1.181.4.1 mjf free(val);
1377 1.147 dsl if (mediaopt_set == -1)
1378 1.147 dsl media_error(media_current, invalid, "option");
1379 1.24 thorpej
1380 1.45 thorpej /* Media will be set after other processing is complete. */
1381 1.181.4.1 mjf return 0;
1382 1.45 thorpej }
1383 1.24 thorpej
1384 1.181.4.1 mjf int
1385 1.181.4.1 mjf unsetmediaopt(prop_dictionary_t env, prop_dictionary_t xenv)
1386 1.45 thorpej {
1387 1.181.4.1 mjf char *invalid, *val;
1388 1.181.4.1 mjf prop_data_t data;
1389 1.181.4.1 mjf
1390 1.181.4.1 mjf init_current_media(env, xenv);
1391 1.26 thorpej
1392 1.181.4.1 mjf data = (prop_data_t)prop_dictionary_get(env, "unmediaopt");
1393 1.181.4.1 mjf if (data == NULL) {
1394 1.181.4.1 mjf errno = ENOENT;
1395 1.181.4.1 mjf return -1;
1396 1.181.4.1 mjf }
1397 1.26 thorpej
1398 1.181.4.1 mjf val = strndup(prop_data_data_nocopy(data), prop_data_size(data));
1399 1.181.4.1 mjf if (val == NULL)
1400 1.181.4.1 mjf return -1;
1401 1.24 thorpej
1402 1.47 thorpej /*
1403 1.47 thorpej * No need to check for A_MEDIAINST, since the test for A_MEDIA
1404 1.47 thorpej * implicitly checks for A_MEDIAINST.
1405 1.47 thorpej */
1406 1.47 thorpej
1407 1.147 dsl mediaopt_clear = get_media_options(media_current, val, &invalid);
1408 1.181.4.1 mjf free(val);
1409 1.147 dsl if (mediaopt_clear == -1)
1410 1.147 dsl media_error(media_current, invalid, "option");
1411 1.24 thorpej
1412 1.45 thorpej /* Media will be set after other processing is complete. */
1413 1.181.4.1 mjf return 0;
1414 1.24 thorpej }
1415 1.24 thorpej
1416 1.181.4.1 mjf int
1417 1.181.4.1 mjf setmediainst(prop_dictionary_t env, prop_dictionary_t xenv)
1418 1.47 thorpej {
1419 1.181.4.1 mjf int type, subtype, options;
1420 1.181.4.1 mjf int64_t inst;
1421 1.181.4.1 mjf bool rc;
1422 1.47 thorpej
1423 1.181.4.1 mjf init_current_media(env, xenv);
1424 1.47 thorpej
1425 1.181.4.1 mjf rc = prop_dictionary_get_int64(env, "mediainst", &inst);
1426 1.181.4.1 mjf assert(rc);
1427 1.47 thorpej
1428 1.181.4.1 mjf /* Can only issue `instance' once. */
1429 1.47 thorpej /* Must have already specified `media' */
1430 1.47 thorpej
1431 1.47 thorpej type = IFM_TYPE(media_current);
1432 1.47 thorpej subtype = IFM_SUBTYPE(media_current);
1433 1.47 thorpej options = IFM_OPTIONS(media_current);
1434 1.47 thorpej
1435 1.47 thorpej media_current = IFM_MAKEWORD(type, subtype, options, inst);
1436 1.47 thorpej
1437 1.47 thorpej /* Media will be set after other processing is complete. */
1438 1.181.4.1 mjf return 0;
1439 1.47 thorpej }
1440 1.47 thorpej
1441 1.181.4.1 mjf int
1442 1.181.4.1 mjf setmediamode(prop_dictionary_t env, prop_dictionary_t xenv)
1443 1.139 dyoung {
1444 1.139 dyoung int type, subtype, options, inst, mode;
1445 1.181.4.1 mjf prop_data_t data;
1446 1.181.4.1 mjf char *val;
1447 1.139 dyoung
1448 1.181.4.1 mjf init_current_media(env, xenv);
1449 1.139 dyoung
1450 1.181.4.1 mjf data = (prop_data_t)prop_dictionary_get(env, "mediamode");
1451 1.181.4.1 mjf assert(data != NULL);
1452 1.139 dyoung
1453 1.139 dyoung type = IFM_TYPE(media_current);
1454 1.139 dyoung subtype = IFM_SUBTYPE(media_current);
1455 1.139 dyoung options = IFM_OPTIONS(media_current);
1456 1.139 dyoung inst = IFM_INST(media_current);
1457 1.139 dyoung
1458 1.181.4.1 mjf val = strndup(prop_data_data_nocopy(data), prop_data_size(data));
1459 1.181.4.1 mjf if (val == NULL)
1460 1.181.4.1 mjf return -1;
1461 1.181.4.1 mjf
1462 1.147 dsl mode = get_media_mode(type, val);
1463 1.147 dsl if (mode == -1)
1464 1.147 dsl media_error(type, val, "mode");
1465 1.139 dyoung
1466 1.181.4.1 mjf free(val);
1467 1.181.4.1 mjf
1468 1.139 dyoung media_current = IFM_MAKEWORD(type, subtype, options, inst) | mode;
1469 1.139 dyoung
1470 1.139 dyoung /* Media will be set after other processing is complete. */
1471 1.181.4.1 mjf return 0;
1472 1.139 dyoung }
1473 1.139 dyoung
1474 1.24 thorpej void
1475 1.147 dsl print_media_word(int ifmw, const char *opt_sep)
1476 1.24 thorpej {
1477 1.147 dsl const char *str;
1478 1.24 thorpej
1479 1.147 dsl printf("%s", get_media_subtype_string(ifmw));
1480 1.139 dyoung
1481 1.139 dyoung /* Find mode. */
1482 1.139 dyoung if (IFM_MODE(ifmw) != 0) {
1483 1.147 dsl str = get_media_mode_string(ifmw);
1484 1.147 dsl if (str != NULL)
1485 1.147 dsl printf(" mode %s", str);
1486 1.139 dyoung }
1487 1.24 thorpej
1488 1.24 thorpej /* Find options. */
1489 1.147 dsl for (; (str = get_media_option_string(&ifmw)) != NULL; opt_sep = ",")
1490 1.147 dsl printf("%s%s", opt_sep, str);
1491 1.147 dsl
1492 1.43 thorpej if (IFM_INST(ifmw) != 0)
1493 1.47 thorpej printf(" instance %d", IFM_INST(ifmw));
1494 1.24 thorpej }
1495 1.54 sommerfe
1496 1.145 dsl int
1497 1.181.4.1 mjf carrier(prop_dictionary_t env)
1498 1.54 sommerfe {
1499 1.54 sommerfe struct ifmediareq ifmr;
1500 1.54 sommerfe
1501 1.181.4.1 mjf memset(&ifmr, 0, sizeof(ifmr));
1502 1.54 sommerfe
1503 1.181.4.1 mjf if (direct_ioctl(env, SIOCGIFMEDIA, &ifmr) == -1) {
1504 1.54 sommerfe /*
1505 1.54 sommerfe * Interface doesn't support SIOC{G,S}IFMEDIA;
1506 1.54 sommerfe * assume ok.
1507 1.54 sommerfe */
1508 1.181.4.1 mjf return EXIT_SUCCESS;
1509 1.54 sommerfe }
1510 1.54 sommerfe if ((ifmr.ifm_status & IFM_AVALID) == 0) {
1511 1.54 sommerfe /*
1512 1.54 sommerfe * Interface doesn't report media-valid status.
1513 1.54 sommerfe * assume ok.
1514 1.54 sommerfe */
1515 1.181.4.1 mjf return EXIT_SUCCESS;
1516 1.54 sommerfe }
1517 1.54 sommerfe /* otherwise, return ok for active, not-ok if not active. */
1518 1.181.4.1 mjf if (ifmr.ifm_status & IFM_ACTIVE)
1519 1.181.4.1 mjf return EXIT_SUCCESS;
1520 1.181.4.1 mjf else
1521 1.181.4.1 mjf return EXIT_FAILURE;
1522 1.54 sommerfe }
1523 1.54 sommerfe
1524 1.63 thorpej const int ifm_status_valid_list[] = IFM_STATUS_VALID_LIST;
1525 1.63 thorpej
1526 1.63 thorpej const struct ifmedia_status_description ifm_status_descriptions[] =
1527 1.63 thorpej IFM_STATUS_DESCRIPTIONS;
1528 1.63 thorpej
1529 1.1 cgd /*
1530 1.1 cgd * Print the status of the interface. If an address family was
1531 1.1 cgd * specified, show it and it only; otherwise, show them all.
1532 1.1 cgd */
1533 1.16 cgd void
1534 1.181.4.1 mjf status(const struct sockaddr_dl *sdl, prop_dictionary_t env,
1535 1.181.4.1 mjf prop_dictionary_t oenv)
1536 1.1 cgd {
1537 1.24 thorpej struct ifmediareq ifmr;
1538 1.124 matt struct ifdatareq ifdr;
1539 1.181.4.1 mjf struct ifreq ifr;
1540 1.24 thorpej int *media_list, i;
1541 1.119 bjh21 char hbuf[NI_MAXHOST];
1542 1.130 christos char fbuf[BUFSIZ];
1543 1.181.4.1 mjf int af, s;
1544 1.181.4.1 mjf const char *ifname;
1545 1.181.4.1 mjf struct ifcapreq ifcr;
1546 1.181.4.1 mjf unsigned short flags;
1547 1.181.4.1 mjf const struct afswtch *afp;
1548 1.181.4.1 mjf
1549 1.181.4.1 mjf if ((af = getaf(env)) == -1) {
1550 1.181.4.1 mjf afp = NULL;
1551 1.181.4.1 mjf af = AF_UNSPEC;
1552 1.181.4.1 mjf } else
1553 1.181.4.1 mjf afp = lookup_af_bynum(af);
1554 1.181.4.1 mjf
1555 1.181.4.1 mjf /* get out early if the family is unsupported by the kernel */
1556 1.181.4.1 mjf if ((s = getsock(af)) == -1)
1557 1.181.4.1 mjf err(EXIT_FAILURE, "%s: getsock", __func__);
1558 1.181.4.1 mjf
1559 1.181.4.1 mjf if ((ifname = getifinfo(env, oenv, &flags)) == NULL)
1560 1.181.4.1 mjf err(EXIT_FAILURE, "%s: getifinfo", __func__);
1561 1.1 cgd
1562 1.130 christos (void)snprintb(fbuf, sizeof(fbuf), IFFBITS, flags);
1563 1.181.4.1 mjf printf("%s: flags=%s", ifname, &fbuf[2]);
1564 1.181.4.1 mjf
1565 1.181.4.1 mjf estrlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
1566 1.181.4.1 mjf if (ioctl(s, SIOCGIFMETRIC, &ifr) == -1)
1567 1.181.4.1 mjf warn("SIOCGIFMETRIC %s", ifr.ifr_name);
1568 1.181.4.1 mjf else if (ifr.ifr_metric != 0)
1569 1.181.4.1 mjf printf(" metric %d", ifr.ifr_metric);
1570 1.181.4.1 mjf
1571 1.181.4.1 mjf estrlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
1572 1.181.4.1 mjf if (ioctl(s, SIOCGIFMTU, &ifr) != -1 && ifr.ifr_mtu != 0)
1573 1.181.4.1 mjf printf(" mtu %d", ifr.ifr_mtu);
1574 1.147 dsl printf("\n");
1575 1.108 thorpej
1576 1.181.4.1 mjf if (getifcaps(env, oenv, &ifcr) == -1)
1577 1.181.4.1 mjf err(EXIT_FAILURE, "%s: getifcaps", __func__);
1578 1.181.4.1 mjf
1579 1.181.4.1 mjf if (ifcr.ifcr_capabilities != 0) {
1580 1.130 christos (void)snprintb(fbuf, sizeof(fbuf), IFCAPBITS,
1581 1.181.4.1 mjf ifcr.ifcr_capabilities);
1582 1.130 christos printf("\tcapabilities=%s\n", &fbuf[2]);
1583 1.130 christos (void)snprintb(fbuf, sizeof(fbuf), IFCAPBITS,
1584 1.181.4.1 mjf ifcr.ifcr_capenable);
1585 1.130 christos printf("\tenabled=%s\n", &fbuf[2]);
1586 1.108 thorpej }
1587 1.65 thorpej
1588 1.181.4.1 mjf ieee80211_status(env, oenv);
1589 1.181.4.1 mjf vlan_status(env, oenv);
1590 1.171 liamjfoy #ifndef INET_ONLY
1591 1.181.4.1 mjf carp_status(env, oenv);
1592 1.171 liamjfoy #endif
1593 1.181.4.1 mjf tunnel_status(env, oenv);
1594 1.181.4.1 mjf agr_status(env, oenv);
1595 1.65 thorpej
1596 1.119 bjh21 if (sdl != NULL &&
1597 1.150 xtraeme getnameinfo((const struct sockaddr *)sdl, sdl->sdl_len,
1598 1.119 bjh21 hbuf, sizeof(hbuf), NULL, 0, NI_NUMERICHOST) == 0 &&
1599 1.119 bjh21 hbuf[0] != '\0')
1600 1.119 bjh21 printf("\taddress: %s\n", hbuf);
1601 1.24 thorpej
1602 1.32 christos (void) memset(&ifmr, 0, sizeof(ifmr));
1603 1.181.4.1 mjf estrlcpy(ifmr.ifm_name, ifname, sizeof(ifmr.ifm_name));
1604 1.24 thorpej
1605 1.144 dsl if (ioctl(s, SIOCGIFMEDIA, &ifmr) == -1) {
1606 1.24 thorpej /*
1607 1.24 thorpej * Interface doesn't support SIOC{G,S}IFMEDIA.
1608 1.24 thorpej */
1609 1.124 matt goto iface_stats;
1610 1.24 thorpej }
1611 1.24 thorpej
1612 1.24 thorpej if (ifmr.ifm_count == 0) {
1613 1.181.4.1 mjf warnx("%s: no media types?", ifname);
1614 1.124 matt goto iface_stats;
1615 1.24 thorpej }
1616 1.24 thorpej
1617 1.24 thorpej media_list = (int *)malloc(ifmr.ifm_count * sizeof(int));
1618 1.24 thorpej if (media_list == NULL)
1619 1.120 atatat err(EXIT_FAILURE, "malloc");
1620 1.24 thorpej ifmr.ifm_ulist = media_list;
1621 1.24 thorpej
1622 1.144 dsl if (ioctl(s, SIOCGIFMEDIA, &ifmr) == -1)
1623 1.120 atatat err(EXIT_FAILURE, "SIOCGIFMEDIA");
1624 1.24 thorpej
1625 1.147 dsl printf("\tmedia: %s ", get_media_type_string(ifmr.ifm_current));
1626 1.147 dsl print_media_word(ifmr.ifm_current, " ");
1627 1.24 thorpej if (ifmr.ifm_active != ifmr.ifm_current) {
1628 1.147 dsl printf(" (");
1629 1.147 dsl print_media_word(ifmr.ifm_active, " ");
1630 1.147 dsl printf(")");
1631 1.24 thorpej }
1632 1.147 dsl printf("\n");
1633 1.24 thorpej
1634 1.63 thorpej if (ifmr.ifm_status & IFM_STATUS_VALID) {
1635 1.63 thorpej const struct ifmedia_status_description *ifms;
1636 1.63 thorpej int bitno, found = 0;
1637 1.63 thorpej
1638 1.42 thorpej printf("\tstatus: ");
1639 1.63 thorpej for (bitno = 0; ifm_status_valid_list[bitno] != 0; bitno++) {
1640 1.63 thorpej for (ifms = ifm_status_descriptions;
1641 1.63 thorpej ifms->ifms_valid != 0; ifms++) {
1642 1.63 thorpej if (ifms->ifms_type !=
1643 1.66 thorpej IFM_TYPE(ifmr.ifm_current) ||
1644 1.63 thorpej ifms->ifms_valid !=
1645 1.63 thorpej ifm_status_valid_list[bitno])
1646 1.63 thorpej continue;
1647 1.63 thorpej printf("%s%s", found ? ", " : "",
1648 1.63 thorpej IFM_STATUS_DESC(ifms, ifmr.ifm_status));
1649 1.63 thorpej found = 1;
1650 1.63 thorpej
1651 1.63 thorpej /*
1652 1.63 thorpej * For each valid indicator bit, there's
1653 1.63 thorpej * only one entry for each media type, so
1654 1.63 thorpej * terminate the inner loop now.
1655 1.63 thorpej */
1656 1.63 thorpej break;
1657 1.63 thorpej }
1658 1.63 thorpej }
1659 1.24 thorpej
1660 1.63 thorpej if (found == 0)
1661 1.42 thorpej printf("unknown");
1662 1.147 dsl printf("\n");
1663 1.24 thorpej }
1664 1.24 thorpej
1665 1.24 thorpej if (mflag) {
1666 1.44 thorpej int type, printed_type;
1667 1.44 thorpej
1668 1.44 thorpej for (type = IFM_NMIN; type <= IFM_NMAX; type += IFM_NMIN) {
1669 1.44 thorpej for (i = 0, printed_type = 0; i < ifmr.ifm_count; i++) {
1670 1.147 dsl if (IFM_TYPE(media_list[i]) != type)
1671 1.147 dsl continue;
1672 1.147 dsl if (printed_type == 0) {
1673 1.147 dsl printf("\tsupported %s media:\n",
1674 1.147 dsl get_media_type_string(type));
1675 1.147 dsl printed_type = 1;
1676 1.44 thorpej }
1677 1.147 dsl printf("\t\tmedia ");
1678 1.147 dsl print_media_word(media_list[i], " mediaopt ");
1679 1.147 dsl printf("\n");
1680 1.44 thorpej }
1681 1.24 thorpej }
1682 1.24 thorpej }
1683 1.24 thorpej
1684 1.24 thorpej free(media_list);
1685 1.24 thorpej
1686 1.124 matt iface_stats:
1687 1.134 perry if (!vflag && !zflag)
1688 1.124 matt goto proto_status;
1689 1.124 matt
1690 1.181.4.1 mjf estrlcpy(ifdr.ifdr_name, ifname, sizeof(ifdr.ifdr_name));
1691 1.124 matt
1692 1.144 dsl if (ioctl(s, zflag ? SIOCZIFDATA:SIOCGIFDATA, &ifdr) == -1) {
1693 1.134 perry err(EXIT_FAILURE, zflag ? "SIOCZIFDATA" : "SIOCGIFDATA");
1694 1.134 perry } else {
1695 1.124 matt struct if_data * const ifi = &ifdr.ifdr_data;
1696 1.169 rpaulo char buf[5];
1697 1.169 rpaulo
1698 1.124 matt #define PLURAL(n) ((n) == 1 ? "" : "s")
1699 1.169 rpaulo #define PLURALSTR(s) ((atof(s)) == 1.0 ? "" : "s")
1700 1.169 rpaulo printf("\tinput: %llu packet%s, ",
1701 1.124 matt (unsigned long long) ifi->ifi_ipackets,
1702 1.169 rpaulo PLURAL(ifi->ifi_ipackets));
1703 1.169 rpaulo if (hflag) {
1704 1.169 rpaulo (void) humanize_number(buf, sizeof(buf),
1705 1.169 rpaulo (int64_t) ifi->ifi_ibytes, "", HN_AUTOSCALE,
1706 1.169 rpaulo HN_NOSPACE | HN_DECIMAL);
1707 1.169 rpaulo printf("%s byte%s", buf,
1708 1.169 rpaulo PLURALSTR(buf));
1709 1.169 rpaulo } else
1710 1.169 rpaulo printf("%llu byte%s",
1711 1.169 rpaulo (unsigned long long) ifi->ifi_ibytes,
1712 1.169 rpaulo PLURAL(ifi->ifi_ibytes));
1713 1.124 matt if (ifi->ifi_imcasts)
1714 1.124 matt printf(", %llu multicast%s",
1715 1.124 matt (unsigned long long) ifi->ifi_imcasts,
1716 1.124 matt PLURAL(ifi->ifi_imcasts));
1717 1.124 matt if (ifi->ifi_ierrors)
1718 1.124 matt printf(", %llu error%s",
1719 1.124 matt (unsigned long long) ifi->ifi_ierrors,
1720 1.124 matt PLURAL(ifi->ifi_ierrors));
1721 1.124 matt if (ifi->ifi_iqdrops)
1722 1.124 matt printf(", %llu queue drop%s",
1723 1.124 matt (unsigned long long) ifi->ifi_iqdrops,
1724 1.124 matt PLURAL(ifi->ifi_iqdrops));
1725 1.124 matt if (ifi->ifi_noproto)
1726 1.124 matt printf(", %llu unknown protocol",
1727 1.124 matt (unsigned long long) ifi->ifi_noproto);
1728 1.169 rpaulo printf("\n\toutput: %llu packet%s, ",
1729 1.124 matt (unsigned long long) ifi->ifi_opackets,
1730 1.169 rpaulo PLURAL(ifi->ifi_opackets));
1731 1.169 rpaulo if (hflag) {
1732 1.169 rpaulo (void) humanize_number(buf, sizeof(buf),
1733 1.169 rpaulo (int64_t) ifi->ifi_obytes, "", HN_AUTOSCALE,
1734 1.169 rpaulo HN_NOSPACE | HN_DECIMAL);
1735 1.169 rpaulo printf("%s byte%s", buf,
1736 1.169 rpaulo PLURALSTR(buf));
1737 1.169 rpaulo } else
1738 1.169 rpaulo printf("%llu byte%s",
1739 1.169 rpaulo (unsigned long long) ifi->ifi_obytes,
1740 1.169 rpaulo PLURAL(ifi->ifi_obytes));
1741 1.124 matt if (ifi->ifi_omcasts)
1742 1.124 matt printf(", %llu multicast%s",
1743 1.124 matt (unsigned long long) ifi->ifi_omcasts,
1744 1.124 matt PLURAL(ifi->ifi_omcasts));
1745 1.124 matt if (ifi->ifi_oerrors)
1746 1.124 matt printf(", %llu error%s",
1747 1.124 matt (unsigned long long) ifi->ifi_oerrors,
1748 1.124 matt PLURAL(ifi->ifi_oerrors));
1749 1.124 matt if (ifi->ifi_collisions)
1750 1.124 matt printf(", %llu collision%s",
1751 1.124 matt (unsigned long long) ifi->ifi_collisions,
1752 1.124 matt PLURAL(ifi->ifi_collisions));
1753 1.124 matt printf("\n");
1754 1.124 matt #undef PLURAL
1755 1.169 rpaulo #undef PLURALSTR
1756 1.124 matt }
1757 1.124 matt
1758 1.181.4.1 mjf ieee80211_statistics(env);
1759 1.149 dyoung
1760 1.24 thorpej proto_status:
1761 1.181.4.1 mjf
1762 1.181.4.1 mjf if (afp != NULL)
1763 1.181.4.1 mjf (*afp->af_status)(env, oenv, true);
1764 1.181.4.1 mjf else for (afp = afs; afp->af_name != NULL; afp++)
1765 1.181.4.1 mjf (*afp->af_status)(env, oenv, false);
1766 1.49 christos }
1767 1.49 christos
1768 1.181.4.1 mjf int
1769 1.181.4.1 mjf setifprefixlen(prop_dictionary_t env, prop_dictionary_t xenv)
1770 1.53 itojun {
1771 1.181.4.1 mjf const char *ifname;
1772 1.181.4.1 mjf bool rc;
1773 1.181.4.1 mjf int64_t plen;
1774 1.181.4.1 mjf int af;
1775 1.181.4.1 mjf const struct afswtch *afp;
1776 1.181.4.1 mjf unsigned short flags;
1777 1.181.4.1 mjf struct paddr_prefix *pfx;
1778 1.181.4.1 mjf prop_data_t d;
1779 1.181.4.1 mjf
1780 1.181.4.1 mjf if ((af = getaf(env)) == -1)
1781 1.181.4.1 mjf af = AF_INET;
1782 1.181.4.1 mjf
1783 1.181.4.1 mjf if ((afp = lookup_af_bynum(af)) == NULL)
1784 1.181.4.1 mjf return -1;
1785 1.181.4.1 mjf
1786 1.181.4.1 mjf if ((ifname = getifinfo(env, xenv, &flags)) == NULL)
1787 1.181.4.1 mjf err(EXIT_FAILURE, "getifinfo");
1788 1.181.4.1 mjf
1789 1.181.4.1 mjf rc = prop_dictionary_get_int64(env, "prefixlen", &plen);
1790 1.181.4.1 mjf assert(rc);
1791 1.181.4.1 mjf
1792 1.181.4.1 mjf pfx = prefixlen_to_mask(af, plen);
1793 1.181.4.1 mjf if (pfx == NULL)
1794 1.181.4.1 mjf err(EXIT_FAILURE, "prefixlen_to_mask");
1795 1.181.4.1 mjf
1796 1.181.4.1 mjf d = prop_data_create_data(pfx,
1797 1.181.4.1 mjf offsetof(struct paddr_prefix, pfx_addr) + pfx->pfx_addr.sa_len);
1798 1.181.4.1 mjf if (d == NULL)
1799 1.181.4.1 mjf err(EXIT_FAILURE, "%s: prop_data_create_data", __func__);
1800 1.181.4.1 mjf
1801 1.181.4.1 mjf if (!prop_dictionary_set(xenv, "netmask", (prop_object_t)d))
1802 1.181.4.1 mjf err(EXIT_FAILURE, "%s: prop_dictionary_set", __func__);
1803 1.181.4.1 mjf
1804 1.181.4.1 mjf if (afp->af_getaddr != NULL)
1805 1.181.4.1 mjf (*afp->af_getaddr)(pfx, MASK);
1806 1.181.4.1 mjf
1807 1.181.4.1 mjf free(pfx);
1808 1.181.4.1 mjf return 0;
1809 1.53 itojun }
1810 1.53 itojun
1811 1.18 glass void
1812 1.145 dsl usage(void)
1813 1.18 glass {
1814 1.99 cgd const char *progname = getprogname();
1815 1.80 thorpej
1816 1.31 thorpej fprintf(stderr,
1817 1.169 rpaulo "usage: %s [-h] [-m] [-v] [-z] "
1818 1.53 itojun #ifdef INET6
1819 1.134 perry "[-L] "
1820 1.53 itojun #endif
1821 1.80 thorpej "interface\n"
1822 1.105 itojun "\t[ af [ address [ dest_addr ] ] [ netmask mask ] [ prefixlen n ]\n"
1823 1.105 itojun "\t\t[ alias | -alias ] ]\n"
1824 1.104 itojun "\t[ up ] [ down ] [ metric n ] [ mtu n ]\n"
1825 1.93 onoe "\t[ nwid network_id ] [ nwkey network_key | -nwkey ]\n"
1826 1.181 degroote "\t[ list scan ]\n"
1827 1.93 onoe "\t[ powersave | -powersave ] [ powersavesleep duration ]\n"
1828 1.170 rpaulo "\t[ hidessid | -hidessid ] [ apbridge | -apbridge ]\n"
1829 1.93 onoe "\t[ [ af ] tunnel src_addr dest_addr ] [ deletetunnel ]\n"
1830 1.80 thorpej "\t[ arp | -arp ]\n"
1831 1.93 onoe "\t[ media type ] [ mediaopt opts ] [ -mediaopt opts ] "
1832 1.93 onoe "[ instance minst ]\n"
1833 1.178 dyoung "\t[ preference n ]\n"
1834 1.89 thorpej "\t[ vlan n vlanif i ]\n"
1835 1.154 yamt "\t[ agrport i ] [ -agrport i ]\n"
1836 1.104 itojun "\t[ anycast | -anycast ] [ deprecated | -deprecated ]\n"
1837 1.127 itojun "\t[ tentative | -tentative ] [ pltime n ] [ vltime n ] [ eui64 ]\n"
1838 1.80 thorpej "\t[ link0 | -link0 ] [ link1 | -link1 ] [ link2 | -link2 ]\n"
1839 1.169 rpaulo " %s -a [-b] [-h] [-m] [-d] [-u] [-v] [-z] [ af ]\n"
1840 1.134 perry " %s -l [-b] [-d] [-u] [-s]\n"
1841 1.101 christos " %s -C\n"
1842 1.81 thorpej " %s interface create\n"
1843 1.81 thorpej " %s interface destroy\n",
1844 1.101 christos progname, progname, progname, progname, progname, progname);
1845 1.181.4.1 mjf exit(EXIT_FAILURE);
1846 1.1 cgd }
1847