xref: /freebsd/sbin/ifconfig/ifconfig.h (revision e6bfd18d21b225af6a0ed67ceeaf1293b7b9eba5)
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  * $FreeBSD$
37  */
38 
39 #pragma once
40 
41 #include <libifconfig.h>
42 #include <stdbool.h>
43 
44 #define	__constructor	__attribute__((constructor))
45 
46 #ifdef WITHOUT_NETLINK
47 #define	__netlink_used		__unused
48 #define	__netlink_unused
49 #else
50 #define	__netlink_used
51 #define	__netlink_unused	__unused
52 #endif
53 
54 struct afswtch;
55 struct cmd;
56 struct ifconfig_context;
57 
58 typedef	void c_func(const struct ifconfig_context *ctx, const char *cmd, int arg);
59 typedef	void c_func2(const struct ifconfig_context *ctx, const char *arg1,
60     const char *arg2);
61 typedef	void c_func3(const struct ifconfig_context *ctx, const char *cmd,
62     const char *arg);
63 
64 struct cmd {
65 	const char *c_name;
66 	int	c_parameter;
67 #define	NEXTARG		0xffffff	/* has following arg */
68 #define	NEXTARG2	0xfffffe	/* has 2 following args */
69 #define	OPTARG		0xfffffd	/* has optional following arg */
70 #define	SPARAM		0xfffffc	/* parameter is string c_sparameter */
71 	const char *c_sparameter;
72 	union {
73 		c_func	*c_func;
74 		c_func2	*c_func2;
75 		c_func3	*c_func3;
76 	} c_u;
77 	int	c_iscloneop;
78 	struct cmd *c_next;
79 };
80 void	cmd_register(struct cmd *);
81 
82 typedef	void callback_func(int s, void *);
83 void	callback_register(callback_func *, void *);
84 
85 /*
86  * Macros for initializing command handlers.
87  */
88 
89 #define	DEF_CMD(name, param, func) {		\
90     .c_name = (name),				\
91     .c_parameter = (param),			\
92     .c_u = { .c_func = (func) },		\
93     .c_iscloneop = 0,				\
94     .c_next = NULL,				\
95 }
96 #define	DEF_CMD_ARG(name, func) {		\
97     .c_name = (name),				\
98     .c_parameter = NEXTARG,			\
99     .c_u = { .c_func = (func) },		\
100     .c_iscloneop = 0,				\
101     .c_next = NULL,				\
102 }
103 #define	DEF_CMD_OPTARG(name, func) {		\
104     .c_name = (name),				\
105     .c_parameter = OPTARG,			\
106     .c_u = { .c_func = (func) },		\
107     .c_iscloneop = 0,				\
108     .c_next = NULL,				\
109 }
110 #define	DEF_CMD_ARG2(name, func) {		\
111     .c_name = (name),				\
112     .c_parameter = NEXTARG2,			\
113     .c_u = { .c_func2 = (func) },		\
114     .c_iscloneop = 0,				\
115     .c_next = NULL,				\
116 }
117 #define	DEF_CMD_SARG(name, sparam, func) {	\
118     .c_name = (name),				\
119     .c_parameter = SPARAM,			\
120     .c_sparameter = (sparam),			\
121     .c_u = { .c_func3 = (func) },		\
122     .c_iscloneop = 0,				\
123     .c_next = NULL,				\
124 }
125 #define	DEF_CLONE_CMD(name, param, func) {	\
126     .c_name = (name),				\
127     .c_parameter = (param),			\
128     .c_u = { .c_func = (func) },		\
129     .c_iscloneop = 1,				\
130     .c_next = NULL,				\
131 }
132 #define	DEF_CLONE_CMD_ARG(name, func) {		\
133     .c_name = (name),				\
134     .c_parameter = NEXTARG,			\
135     .c_u = { .c_func = (func) },		\
136     .c_iscloneop = 1,				\
137     .c_next = NULL,				\
138 }
139 #define	DEF_CLONE_CMD_ARG2(name, func) {	\
140     .c_name = (name),				\
141     .c_parameter = NEXTARG2,			\
142     .c_u = { .c_func2 = (func) },		\
143     .c_iscloneop = 1,				\
144     .c_next = NULL,				\
145 }
146 
147 struct snl_state;
148 struct ifconfig_args;
149 struct ifconfig_context {
150 	struct ifconfig_args	*args;
151 	const struct afswtch	*afp;
152 	int			io_s;	/* fd to use for ioctl() */
153 	struct snl_state	*io_ss;	/* NETLINK_ROUTE socket */
154 };
155 typedef const struct ifconfig_context if_ctx;
156 
157 #define	ioctl_ctx(ctx, _req, ...)	ioctl((ctx)->io_s, _req, ## __VA_ARGS__)
158 
159 struct ifaddrs;
160 struct addrinfo;
161 
162 enum {
163 	RIDADDR = 0,
164 	ADDR = 1,
165 	MASK = 2,
166 	DSTADDR = 3,
167 #ifdef WITHOUT_NETLINK
168 	BRDADDR = 3,
169 #else
170 	BRDADDR = 4,
171 #endif
172 };
173 
174 struct snl_parsed_addr;
175 struct snl_parsed_link;
176 typedef struct snl_parsed_link if_link_t;
177 typedef struct snl_parsed_addr if_addr_t;
178 
179 typedef void af_setvhid_f(int vhid);
180 typedef	void af_status_nl_f(if_ctx *ctx, if_link_t *link, if_addr_t *ifa);
181 typedef void af_status_f(if_ctx *ctx, const struct ifaddrs *);
182 typedef void af_other_status_f(if_ctx *ctx);
183 typedef void af_postproc_f(if_ctx *ctx, int newaddr, int ifflags);
184 typedef	int af_exec_f(if_ctx *ctx, unsigned long action, void *data);
185 typedef void af_copyaddr_f(if_ctx *ctx, int to, int from);
186 
187 struct afswtch {
188 	const char	*af_name;	/* as given on cmd line, e.g. "inet" */
189 	short		af_af;		/* AF_* */
190 	/*
191 	 * Status is handled one of two ways; if there is an
192 	 * address associated with the interface then the
193 	 * associated address family af_status method is invoked
194 	 * with the appropriate addressin info.  Otherwise, if
195 	 * all possible info is to be displayed and af_other_status
196 	 * is defined then it is invoked after all address status
197 	 * is presented.
198 	 */
199 #ifndef WITHOUT_NETLINK
200 	af_status_nl_f	*af_status;
201 #else
202 	af_status_f	*af_status;
203 #endif
204 	af_other_status_f	*af_other_status;
205 	void		(*af_getaddr)(const char *, int);
206 	af_copyaddr_f	*af_copyaddr;	/* Copy address between <RID|*>ADDR */
207 					/* parse prefix method (IPv6) */
208 	void		(*af_getprefix)(const char *, int);
209 	af_postproc_f	*af_postproc;
210 	af_setvhid_f	*af_setvhid;	/* Set CARP vhid for an address */
211 	af_exec_f	*af_exec;	/* Handler to interact with kernel */
212 	u_long		af_difaddr;	/* set dst if address ioctl */
213 	u_long		af_aifaddr;	/* set if address ioctl */
214 	void		*af_ridreq;	/* */
215 	void		*af_addreq;	/* */
216 	struct afswtch	*af_next;
217 
218 	/* XXX doesn't fit model */
219 	void		(*af_status_tunnel)(int);
220 	void		(*af_settunnel)(int s, struct addrinfo *srcres,
221 				struct addrinfo *dstres);
222 };
223 void	af_register(struct afswtch *);
224 int	af_exec_ioctl(if_ctx *ctx, unsigned long action, void *data);
225 
226 struct ifconfig_args {
227 	bool all;		/* Match everything */
228 	bool downonly;		/* Down-only items */
229 	bool uponly;		/* Up-only items */
230 	bool namesonly;		/* Output only names */
231 	bool noload;		/* Do not load relevant kernel modules */
232 	bool supmedia;		/* Supported media */
233 	bool printkeys;		/* Print security keys */
234 	bool allfamilies;	/* Print all families */
235 	int verbose;		/* verbosity level */
236 	int argc;
237 	char **argv;
238 	const char *ifname;	/* Requested interface name */
239 	const char *matchgroup;		/* Group name to match */
240 	const char *nogroup;		/* Group name to exclude */
241 	const struct afswtch *afp;	/* AF we're operating on */
242 	const char *jail_name;	/* Jail name or jail id specified */
243 };
244 
245 struct option {
246 	const char *opt;
247 	const char *opt_usage;
248 	void	(*cb)(const char *arg);
249 	struct option *next;
250 };
251 void	opt_register(struct option *);
252 
253 extern	ifconfig_handle_t *lifh;
254 extern	struct ifreq ifr;
255 extern	char name[IFNAMSIZ];	/* name of interface */
256 extern	int allmedia;
257 extern	int printkeys;
258 extern	int newaddr;
259 extern	int verbose;
260 extern	int printifname;
261 extern	int exit_code;
262 extern struct ifconfig_args global_args;
263 extern	char *f_inet, *f_inet6, *f_ether, *f_addr;
264 
265 void	setifcap(if_ctx *ctx, const char *, int value);
266 void	setifcapnv(if_ctx *ctx, const char *vname, const char *arg);
267 
268 void	Perror(const char *cmd);
269 void	printb(const char *s, unsigned value, const char *bits);
270 
271 void	ifmaybeload(struct ifconfig_args *args, const char *name);
272 
273 typedef int  clone_match_func(const char *);
274 typedef void clone_callback_func(int, struct ifreq *);
275 void	clone_setdefcallback_prefix(const char *, clone_callback_func *);
276 void	clone_setdefcallback_filter(clone_match_func *, clone_callback_func *);
277 
278 void	sfp_status(if_ctx *ctx);
279 
280 struct sockaddr_dl;
281 bool	match_ether(const struct sockaddr_dl *sdl);
282 bool	match_if_flags(struct ifconfig_args *args, int if_flags);
283 int	ifconfig(if_ctx *ctx, int iscreate, const struct afswtch *uafp);
284 bool	group_member(const char *ifname, const char *match, const char *nomatch);
285 void	print_ifcap(struct ifconfig_args *args, int s);
286 void	tunnel_status(int s);
287 struct afswtch	*af_getbyfamily(int af);
288 void	af_other_status(if_ctx *ctx);
289 void	print_ifstatus(int s);
290 void	print_metric(int s);
291 
292 /* Netlink-related functions */
293 void	list_interfaces_nl(struct ifconfig_args *args);
294 int	ifconfig_wrapper_nl(struct ifconfig_args *args, int iscreate,
295 		const struct afswtch *uafp);
296 uint32_t if_nametoindex_nl(struct snl_state *ss, const char *ifname);
297 
298 /*
299  * XXX expose this so modules that need to know of any pending
300  * operations on ifmedia can avoid cmd line ordering confusion.
301  */
302 struct ifmediareq *ifmedia_getstate(void);
303 
304 void print_vhid(const struct ifaddrs *, const char *);
305 
306 void ioctl_ifcreate(int s, struct ifreq *);
307 
308 /* Helpers */
309 struct sockaddr_in;
310 struct sockaddr_in6;
311 struct sockaddr;
312 
313 static inline struct sockaddr_in6 *
314 satosin6(struct sockaddr *sa)
315 {
316 	return ((struct sockaddr_in6 *)(void *)sa);
317 }
318 
319 static inline struct sockaddr_in *
320 satosin(struct sockaddr *sa)
321 {
322 	return ((struct sockaddr_in *)(void *)sa);
323 }
324