1 /*-
2 * SPDX-License-Identifier: BSD-4-Clause
3 *
4 * Copyright (c) 1997 Peter Wemm.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed for the FreeBSD Project
18 * by Peter Wemm.
19 * 4. The name of the author may not be used to endorse or promote products
20 * derived from this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 *
34 * so there!
35 */
36
37 #pragma once
38
39 #include <libifconfig.h>
40 #include <stdbool.h>
41
42 #define __constructor __attribute__((constructor))
43
44 #ifdef WITHOUT_NETLINK
45 #define __netlink_used __unused
46 #define __netlink_unused
47 #else
48 #define __netlink_used
49 #define __netlink_unused __unused
50 #endif
51
52 struct afswtch;
53 struct cmd;
54 struct snl_state;
55 struct ifconfig_args;
56 struct ifconfig_context {
57 struct ifconfig_args *args;
58 const struct afswtch *afp;
59 int io_s; /* fd to use for ioctl() */
60 struct snl_state *io_ss; /* NETLINK_ROUTE socket */
61 const char *ifname; /* Current interface name */
62 char _ifname_storage_ioctl[IFNAMSIZ];
63 };
64 typedef struct ifconfig_context if_ctx;
65
66 typedef void c_func(if_ctx *ctx, const char *cmd, int arg);
67 typedef void c_func2(if_ctx *ctx, const char *arg1, const char *arg2);
68 typedef void c_func3(if_ctx *ctx, const char *cmd, const char *arg);
69
70 struct cmd {
71 const char *c_name;
72 int c_parameter;
73 #define NEXTARG 0xffffff /* has following arg */
74 #define NEXTARG2 0xfffffe /* has 2 following args */
75 #define OPTARG 0xfffffd /* has optional following arg */
76 #define SPARAM 0xfffffc /* parameter is string c_sparameter */
77 const char *c_sparameter;
78 union {
79 c_func *c_func;
80 c_func2 *c_func2;
81 c_func3 *c_func3;
82 } c_u;
83 int c_iscloneop;
84 struct cmd *c_next;
85 };
86 void cmd_register(struct cmd *);
87
88 typedef void callback_func(if_ctx *, void *);
89 void callback_register(callback_func *, void *);
90
91 /*
92 * Macros for initializing command handlers.
93 */
94
95 #define DEF_CMD(name, param, func) { \
96 .c_name = (name), \
97 .c_parameter = (param), \
98 .c_u = { .c_func = (func) }, \
99 .c_iscloneop = 0, \
100 .c_next = NULL, \
101 }
102 #define DEF_CMD_ARG(name, func) { \
103 .c_name = (name), \
104 .c_parameter = NEXTARG, \
105 .c_u = { .c_func = (func) }, \
106 .c_iscloneop = 0, \
107 .c_next = NULL, \
108 }
109 #define DEF_CMD_OPTARG(name, func) { \
110 .c_name = (name), \
111 .c_parameter = OPTARG, \
112 .c_u = { .c_func = (func) }, \
113 .c_iscloneop = 0, \
114 .c_next = NULL, \
115 }
116 #define DEF_CMD_ARG2(name, func) { \
117 .c_name = (name), \
118 .c_parameter = NEXTARG2, \
119 .c_u = { .c_func2 = (func) }, \
120 .c_iscloneop = 0, \
121 .c_next = NULL, \
122 }
123 #define DEF_CMD_SARG(name, sparam, func) { \
124 .c_name = (name), \
125 .c_parameter = SPARAM, \
126 .c_sparameter = (sparam), \
127 .c_u = { .c_func3 = (func) }, \
128 .c_iscloneop = 0, \
129 .c_next = NULL, \
130 }
131 #define DEF_CLONE_CMD(name, param, func) { \
132 .c_name = (name), \
133 .c_parameter = (param), \
134 .c_u = { .c_func = (func) }, \
135 .c_iscloneop = 1, \
136 .c_next = NULL, \
137 }
138 #define DEF_CLONE_CMD_ARG(name, func) { \
139 .c_name = (name), \
140 .c_parameter = NEXTARG, \
141 .c_u = { .c_func = (func) }, \
142 .c_iscloneop = 1, \
143 .c_next = NULL, \
144 }
145 #define DEF_CLONE_CMD_ARG2(name, func) { \
146 .c_name = (name), \
147 .c_parameter = NEXTARG2, \
148 .c_u = { .c_func2 = (func) }, \
149 .c_iscloneop = 1, \
150 .c_next = NULL, \
151 }
152
153 #define ioctl_ctx(ctx, _req, ...) ioctl((ctx)->io_s, _req, ## __VA_ARGS__)
154 int ioctl_ctx_ifr(if_ctx *ctx, unsigned long cmd, struct ifreq *ifr);
155
156 struct ifaddrs;
157 struct addrinfo;
158
159 enum {
160 RIDADDR = 0,
161 ADDR = 1,
162 MASK = 2,
163 DSTADDR = 3,
164 #ifdef WITHOUT_NETLINK
165 BRDADDR = 3,
166 #else
167 BRDADDR = 4,
168 #endif
169 };
170
171 struct snl_parsed_addr;
172 struct snl_parsed_link;
173 typedef struct snl_parsed_link if_link_t;
174 typedef struct snl_parsed_addr if_addr_t;
175
176 typedef void af_setvhid_f(int vhid);
177 typedef void af_status_nl_f(if_ctx *ctx, if_link_t *link, if_addr_t *ifa);
178 typedef void af_status_f(if_ctx *ctx, const struct ifaddrs *);
179 typedef void af_other_status_f(if_ctx *ctx);
180 typedef void af_postproc_f(if_ctx *ctx, int newaddr, int ifflags);
181 typedef int af_exec_f(if_ctx *ctx, unsigned long action, void *data);
182 typedef void af_copyaddr_f(if_ctx *ctx, int to, int from);
183 typedef void af_status_tunnel_f(if_ctx *ctx);
184 typedef void af_settunnel_f(if_ctx *ctx, struct addrinfo *srcres, struct addrinfo *dstres);
185
186 struct afswtch {
187 const char *af_name; /* as given on cmd line, e.g. "inet" */
188 short af_af; /* AF_* */
189 /*
190 * Status is handled one of two ways; if there is an
191 * address associated with the interface then the
192 * associated address family af_status method is invoked
193 * with the appropriate addressin info. Otherwise, if
194 * all possible info is to be displayed and af_other_status
195 * is defined then it is invoked after all address status
196 * is presented.
197 */
198 #ifndef WITHOUT_NETLINK
199 af_status_nl_f *af_status;
200 #else
201 af_status_f *af_status;
202 #endif
203 af_other_status_f *af_other_status;
204 void (*af_getaddr)(const char *, int);
205 af_copyaddr_f *af_copyaddr; /* Copy address between <RID|*>ADDR */
206 /* parse prefix method (IPv6) */
207 void (*af_getprefix)(const char *, int);
208 af_postproc_f *af_postproc;
209 af_setvhid_f *af_setvhid; /* Set CARP vhid for an address */
210 af_exec_f *af_exec; /* Handler to interact with kernel */
211 u_long af_difaddr; /* set dst if address ioctl */
212 u_long af_aifaddr; /* set if address ioctl */
213 void *af_ridreq; /* */
214 void *af_addreq; /* */
215 struct afswtch *af_next;
216
217 /* XXX doesn't fit model */
218 af_status_tunnel_f *af_status_tunnel;
219 af_settunnel_f *af_settunnel;
220 };
221 void af_register(struct afswtch *);
222 int af_exec_ioctl(if_ctx *ctx, unsigned long action, void *data);
223
224 struct ifconfig_args {
225 bool all; /* Match everything */
226 bool downonly; /* Down-only items */
227 bool uponly; /* Up-only items */
228 bool namesonly; /* Output only names */
229 bool noload; /* Do not load relevant kernel modules */
230 bool supmedia; /* Supported media */
231 bool printkeys; /* Print security keys */
232 bool allfamilies; /* Print all families */
233 bool drivername; /* Print driver name */
234 int verbose; /* verbosity level */
235 int argc;
236 char **argv;
237 const char *ifname; /* Requested interface name */
238 const char *matchgroup; /* Group name to match */
239 const char *nogroup; /* Group name to exclude */
240 const struct afswtch *afp; /* AF we're operating on */
241 const char *jail_name; /* Jail name or jail id specified */
242 };
243
244 struct option {
245 const char *opt;
246 const char *opt_usage;
247 void (*cb)(const char *arg);
248 struct option *next;
249 };
250 void opt_register(struct option *);
251
252 extern ifconfig_handle_t *lifh;
253 extern int allmedia;
254 extern int exit_code;
255 extern char *f_inet, *f_inet6, *f_ether, *f_addr;
256
257 void clearifcap(if_ctx *ctx, const char *, int value);
258 void setifcap(if_ctx *ctx, const char *, int value);
259 void setifcapnv(if_ctx *ctx, const char *vname, const char *arg);
260
261 void Perror(const char *cmd);
262 void printb(const char *s, unsigned value, const char *bits);
263
264 void ifmaybeload(struct ifconfig_args *args, const char *name);
265
266 typedef int clone_match_func(const char *);
267 typedef void clone_callback_func(if_ctx *, struct ifreq *);
268 void clone_setdefcallback_prefix(const char *, clone_callback_func *);
269 void clone_setdefcallback_filter(clone_match_func *, clone_callback_func *);
270
271 void sfp_status(if_ctx *ctx);
272
273 struct sockaddr_dl;
274 bool match_ether(const struct sockaddr_dl *sdl);
275 bool match_if_flags(struct ifconfig_args *args, int if_flags);
276 int ifconfig_ioctl(if_ctx *ctx, int iscreate, const struct afswtch *uafp);
277 bool group_member(const char *ifname, const char *match, const char *nomatch);
278 void tunnel_status(if_ctx *ctx);
279 struct afswtch *af_getbyfamily(int af);
280 void af_other_status(if_ctx *ctx);
281 void print_ifstatus(if_ctx *ctx);
282 void print_metric(if_ctx *ctx);
283
284 /* Netlink-related functions */
285 void list_interfaces_nl(struct ifconfig_args *args);
286 int ifconfig_nl(if_ctx *ctx, int iscreate,
287 const struct afswtch *uafp);
288 uint32_t if_nametoindex_nl(struct snl_state *ss, const char *ifname);
289
290 /*
291 * XXX expose this so modules that need to know of any pending
292 * operations on ifmedia can avoid cmd line ordering confusion.
293 */
294 struct ifmediareq *ifmedia_getstate(if_ctx *ctx);
295
296 void print_vhid(const struct ifaddrs *);
297
298 void ifcreate_ioctl(if_ctx *ctx, struct ifreq *ifr);
299
300 /* Helpers */
301 struct sockaddr_in;
302 struct sockaddr_in6;
303 struct sockaddr;
304
305 static inline struct sockaddr_in6 *
satosin6(struct sockaddr * sa)306 satosin6(struct sockaddr *sa)
307 {
308 return ((struct sockaddr_in6 *)(void *)sa);
309 }
310
311 static inline struct sockaddr_in *
satosin(struct sockaddr * sa)312 satosin(struct sockaddr *sa)
313 {
314 return ((struct sockaddr_in *)(void *)sa);
315 }
316
317 static inline struct sockaddr_dl *
satosdl(struct sockaddr * sa)318 satosdl(struct sockaddr *sa)
319 {
320 return ((struct sockaddr_dl *)(void *)sa);
321 }
322
323 static inline const struct sockaddr_dl *
satosdl_c(const struct sockaddr * sa)324 satosdl_c(const struct sockaddr *sa)
325 {
326 return ((const struct sockaddr_dl *)(const void *)sa);
327 }
328