xref: /freebsd/sbin/ifconfig/ifconfig.h (revision 924226fba12cc9a228c73b956e1b7fa24c60b055)
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 
43 #define	__constructor	__attribute__((constructor))
44 
45 struct afswtch;
46 struct cmd;
47 
48 typedef	void c_func(const char *cmd, int arg, int s, const struct afswtch *afp);
49 typedef	void c_func2(const char *arg1, const char *arg2, int s,
50     const struct afswtch *afp);
51 typedef	void c_func3(const char *cmd, const char *arg, int s,
52     const struct afswtch *afp);
53 
54 struct cmd {
55 	const char *c_name;
56 	int	c_parameter;
57 #define	NEXTARG		0xffffff	/* has following arg */
58 #define	NEXTARG2	0xfffffe	/* has 2 following args */
59 #define	OPTARG		0xfffffd	/* has optional following arg */
60 #define	SPARAM		0xfffffc	/* parameter is string c_sparameter */
61 	const char *c_sparameter;
62 	union {
63 		c_func	*c_func;
64 		c_func2	*c_func2;
65 		c_func3	*c_func3;
66 	} c_u;
67 	int	c_iscloneop;
68 	struct cmd *c_next;
69 };
70 void	cmd_register(struct cmd *);
71 
72 typedef	void callback_func(int s, void *);
73 void	callback_register(callback_func *, void *);
74 
75 /*
76  * Macros for declaring command functions and initializing entries.
77  */
78 #define	DECL_CMD_FUNC(name, cmd, arg) \
79 	void name(const char *cmd, int arg, int s, const struct afswtch *afp)
80 #define	DECL_CMD_FUNC2(name, arg1, arg2) \
81 	void name(const char *arg1, const char *arg2, int s, \
82 	    const struct afswtch *afp)
83 
84 #define	DEF_CMD(name, param, func) {		\
85     .c_name = (name),				\
86     .c_parameter = (param),			\
87     .c_u = { .c_func = (func) },		\
88     .c_iscloneop = 0,				\
89     .c_next = NULL,				\
90 }
91 #define	DEF_CMD_ARG(name, func) {		\
92     .c_name = (name),				\
93     .c_parameter = NEXTARG,			\
94     .c_u = { .c_func = (func) },		\
95     .c_iscloneop = 0,				\
96     .c_next = NULL,				\
97 }
98 #define	DEF_CMD_OPTARG(name, func) {		\
99     .c_name = (name),				\
100     .c_parameter = OPTARG,			\
101     .c_u = { .c_func = (func) },		\
102     .c_iscloneop = 0,				\
103     .c_next = NULL,				\
104 }
105 #define	DEF_CMD_ARG2(name, func) {		\
106     .c_name = (name),				\
107     .c_parameter = NEXTARG2,			\
108     .c_u = { .c_func2 = (func) },		\
109     .c_iscloneop = 0,				\
110     .c_next = NULL,				\
111 }
112 #define	DEF_CMD_SARG(name, sparam, func) {	\
113     .c_name = (name),				\
114     .c_parameter = SPARAM,			\
115     .c_sparameter = (sparam),			\
116     .c_u = { .c_func3 = (func) },		\
117     .c_iscloneop = 0,				\
118     .c_next = NULL,				\
119 }
120 #define	DEF_CLONE_CMD(name, param, func) {	\
121     .c_name = (name),				\
122     .c_parameter = (param),			\
123     .c_u = { .c_func = (func) },		\
124     .c_iscloneop = 1,				\
125     .c_next = NULL,				\
126 }
127 #define	DEF_CLONE_CMD_ARG(name, func) {		\
128     .c_name = (name),				\
129     .c_parameter = NEXTARG,			\
130     .c_u = { .c_func = (func) },		\
131     .c_iscloneop = 1,				\
132     .c_next = NULL,				\
133 }
134 #define	DEF_CLONE_CMD_ARG2(name, func) {	\
135     .c_name = (name),				\
136     .c_parameter = NEXTARG2,			\
137     .c_u = { .c_func2 = (func) },		\
138     .c_iscloneop = 1,				\
139     .c_next = NULL,				\
140 }
141 
142 struct ifaddrs;
143 struct addrinfo;
144 
145 enum {
146 	RIDADDR,
147 	ADDR,
148 	MASK,
149 	DSTADDR,
150 };
151 
152 struct afswtch {
153 	const char	*af_name;	/* as given on cmd line, e.g. "inet" */
154 	short		af_af;		/* AF_* */
155 	/*
156 	 * Status is handled one of two ways; if there is an
157 	 * address associated with the interface then the
158 	 * associated address family af_status method is invoked
159 	 * with the appropriate addressin info.  Otherwise, if
160 	 * all possible info is to be displayed and af_other_status
161 	 * is defined then it is invoked after all address status
162 	 * is presented.
163 	 */
164 	void		(*af_status)(int, const struct ifaddrs *);
165 	void		(*af_other_status)(int);
166 					/* parse address method */
167 	void		(*af_getaddr)(const char *, int);
168 					/* parse prefix method (IPv6) */
169 	void		(*af_getprefix)(const char *, int);
170 	void		(*af_postproc)(int s, const struct afswtch *,
171 			    int newaddr, int ifflags);
172 	u_long		af_difaddr;	/* set dst if address ioctl */
173 	u_long		af_aifaddr;	/* set if address ioctl */
174 	void		*af_ridreq;	/* */
175 	void		*af_addreq;	/* */
176 	struct afswtch	*af_next;
177 
178 	/* XXX doesn't fit model */
179 	void		(*af_status_tunnel)(int);
180 	void		(*af_settunnel)(int s, struct addrinfo *srcres,
181 				struct addrinfo *dstres);
182 };
183 void	af_register(struct afswtch *);
184 
185 struct option {
186 	const char *opt;
187 	const char *opt_usage;
188 	void	(*cb)(const char *arg);
189 	struct option *next;
190 };
191 void	opt_register(struct option *);
192 
193 extern	ifconfig_handle_t *lifh;
194 extern	struct ifreq ifr;
195 extern	char name[IFNAMSIZ];	/* name of interface */
196 extern	int allmedia;
197 extern	int supmedia;
198 extern	int printkeys;
199 extern	int newaddr;
200 extern	int verbose;
201 extern	int printifname;
202 extern	int exit_code;
203 
204 void	setifcap(const char *, int value, int s, const struct afswtch *);
205 void	setifcapnv(const char *vname, const char *arg, int s,
206 	    const struct afswtch *afp);
207 
208 void	Perror(const char *cmd);
209 void	printb(const char *s, unsigned value, const char *bits);
210 
211 void	ifmaybeload(const char *name);
212 
213 typedef int  clone_match_func(const char *);
214 typedef void clone_callback_func(int, struct ifreq *);
215 void	clone_setdefcallback_prefix(const char *, clone_callback_func *);
216 void	clone_setdefcallback_filter(clone_match_func *, clone_callback_func *);
217 
218 void	sfp_status(int s, struct ifreq *ifr, int verbose);
219 
220 /*
221  * XXX expose this so modules that neeed to know of any pending
222  * operations on ifmedia can avoid cmd line ordering confusion.
223  */
224 struct ifmediareq *ifmedia_getstate(void);
225 
226 void print_vhid(const struct ifaddrs *, const char *);
227 
228 void ioctl_ifcreate(int s, struct ifreq *);
229