xref: /freebsd/lib/libifconfig/libifconfig.h (revision 0710ec8ceff0b30f773ac606f29312275513b910)
1ec214349SKristof Provost /*
2b1d757bcSAlan Somers  * Copyright (c) 2016-2017, Marie Helene Kvello-Aune
3ec214349SKristof Provost  * All rights reserved.
4ec214349SKristof Provost  *
5ec214349SKristof Provost  * Redistribution and use in source and binary forms, with or without modification,
6ec214349SKristof Provost  * are permitted provided that the following conditions are met:
7ec214349SKristof Provost  *
8ec214349SKristof Provost  * 1. Redistributions of source code must retain the above copyright notice,
9ec214349SKristof Provost  * thislist of conditions and the following disclaimer.
10ec214349SKristof Provost  *
11ec214349SKristof Provost  * 2. Redistributions in binary form must reproduce the above copyright notice,
12ec214349SKristof Provost  * this list of conditions and the following disclaimer in the documentation and/or
13ec214349SKristof Provost  * other materials provided with the distribution.
14ec214349SKristof Provost  *
15ec214349SKristof Provost  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16ec214349SKristof Provost  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
17ec214349SKristof Provost  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18ec214349SKristof Provost  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
19ec214349SKristof Provost  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20ec214349SKristof Provost  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21ec214349SKristof Provost  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22ec214349SKristof Provost  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23ec214349SKristof Provost  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24ec214349SKristof Provost  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25ec214349SKristof Provost  *
26ec214349SKristof Provost  * $FreeBSD$
27ec214349SKristof Provost  */
28ec214349SKristof Provost 
29ec214349SKristof Provost #pragma once
30ec214349SKristof Provost 
3194cba803SRyan Moeller #include <sys/types.h>
3294cba803SRyan Moeller 
3394cba803SRyan Moeller #include <net/if.h>
3494cba803SRyan Moeller 
35b1d757bcSAlan Somers #include <netinet/in.h>
36b1d757bcSAlan Somers #include <netinet6/in6_var.h>
37b1d757bcSAlan Somers 
38b1d757bcSAlan Somers #define ND6_IFF_DEFAULTIF    0x8000
39b1d757bcSAlan Somers 
40ec214349SKristof Provost typedef enum {
41b1d757bcSAlan Somers 	OK = 0,
42b1d757bcSAlan Somers 	OTHER,
43b1d757bcSAlan Somers 	IOCTL,
44b1d757bcSAlan Somers 	SOCKET
45ec214349SKristof Provost } ifconfig_errtype;
46ec214349SKristof Provost 
47ec214349SKristof Provost /*
48ec214349SKristof Provost  * Opaque definition so calling application can just pass a
49ec214349SKristof Provost  * pointer to it for library use.
50ec214349SKristof Provost  */
51ec214349SKristof Provost struct ifconfig_handle;
52ec214349SKristof Provost typedef struct ifconfig_handle ifconfig_handle_t;
53ec214349SKristof Provost 
54b1d757bcSAlan Somers struct carpreq;
55b1d757bcSAlan Somers struct ifaddrs;
56e5539fb6SRyan Moeller struct ifbropreq;
57e5539fb6SRyan Moeller struct ifbreq;
58b1d757bcSAlan Somers struct in6_ndireq;
59b1d757bcSAlan Somers struct lagg_reqall;
60b1d757bcSAlan Somers struct lagg_reqflags;
61b1d757bcSAlan Somers struct lagg_reqopts;
62b1d757bcSAlan Somers struct lagg_reqport;
63b1d757bcSAlan Somers 
64e5539fb6SRyan Moeller /** Stores extra info associated with a bridge(4) interface */
65e5539fb6SRyan Moeller struct ifconfig_bridge_status {
66e5539fb6SRyan Moeller 	struct ifbropreq *params;	/**< current operational parameters */
67e5539fb6SRyan Moeller 	struct ifbreq *members;		/**< list of bridge members */
68e5539fb6SRyan Moeller 	size_t members_count;		/**< how many member interfaces */
69e5539fb6SRyan Moeller 	uint32_t cache_size;		/**< size of address cache */
70e5539fb6SRyan Moeller 	uint32_t cache_lifetime;	/**< address cache entry lifetime */
71e5539fb6SRyan Moeller };
72e5539fb6SRyan Moeller 
73ec214349SKristof Provost struct ifconfig_capabilities {
74ec214349SKristof Provost 	/** Current capabilities (ifconfig prints this as 'options')*/
75ec214349SKristof Provost 	int curcap;
76ec214349SKristof Provost 	/** Requested capabilities (ifconfig prints this as 'capabilities')*/
77ec214349SKristof Provost 	int reqcap;
78ec214349SKristof Provost };
79ec214349SKristof Provost 
80b1d757bcSAlan Somers /** Stores extra info associated with an inet address */
81b1d757bcSAlan Somers struct ifconfig_inet_addr {
82b1d757bcSAlan Somers 	const struct sockaddr_in *sin;
83b1d757bcSAlan Somers 	const struct sockaddr_in *netmask;
84b1d757bcSAlan Somers 	const struct sockaddr_in *dst;
85b1d757bcSAlan Somers 	const struct sockaddr_in *broadcast;
86b1d757bcSAlan Somers 	int prefixlen;
87b1d757bcSAlan Somers 	uint8_t vhid;
88b1d757bcSAlan Somers };
89b1d757bcSAlan Somers 
90b1d757bcSAlan Somers /** Stores extra info associated with an inet6 address */
91b1d757bcSAlan Somers struct ifconfig_inet6_addr {
92b1d757bcSAlan Somers 	struct sockaddr_in6 *sin6;
93b1d757bcSAlan Somers 	struct sockaddr_in6 *dstin6;
94b1d757bcSAlan Somers 	struct in6_addrlifetime lifetime;
95b1d757bcSAlan Somers 	int prefixlen;
96b1d757bcSAlan Somers 	uint32_t flags;
97b1d757bcSAlan Somers 	uint8_t vhid;
98b1d757bcSAlan Somers };
99b1d757bcSAlan Somers 
100b1d757bcSAlan Somers /** Stores extra info associated with a lagg(4) interface */
101b1d757bcSAlan Somers struct ifconfig_lagg_status {
102b1d757bcSAlan Somers 	struct lagg_reqall *ra;
103b1d757bcSAlan Somers 	struct lagg_reqopts *ro;
104b1d757bcSAlan Somers 	struct lagg_reqflags *rf;
105b1d757bcSAlan Somers };
106b1d757bcSAlan Somers 
107ec214349SKristof Provost /** Retrieves a new state object for use in other API calls.
108ec214349SKristof Provost  * Example usage:
109ec214349SKristof Provost  *{@code
110ec214349SKristof Provost  * // Create state object
111b1d757bcSAlan Somers  * ifconfig_handle_t *lifh;
112b1d757bcSAlan Somers  * lifh = ifconfig_open();
113b1d757bcSAlan Somers  * if (lifh == NULL) {
114b1d757bcSAlan Somers  *     // Handle error
115b1d757bcSAlan Somers  * }
116ec214349SKristof Provost  *
117b1d757bcSAlan Somers  * // Do stuff with the handle
118ec214349SKristof Provost  *
119ec214349SKristof Provost  * // Dispose of the state object
120ec214349SKristof Provost  * ifconfig_close(lifh);
121ec214349SKristof Provost  * lifh = NULL;
122ec214349SKristof Provost  *}
123ec214349SKristof Provost  */
124ec214349SKristof Provost ifconfig_handle_t *ifconfig_open(void);
125ec214349SKristof Provost 
126ec214349SKristof Provost /** Frees resources held in the provided state object.
127ec214349SKristof Provost  * @param h The state object to close.
128ec214349SKristof Provost  * @see #ifconfig_open(void)
129ec214349SKristof Provost  */
130ec214349SKristof Provost void ifconfig_close(ifconfig_handle_t *h);
131ec214349SKristof Provost 
132ec214349SKristof Provost /** Identifies what kind of error occured. */
133ec214349SKristof Provost ifconfig_errtype ifconfig_err_errtype(ifconfig_handle_t *h);
134ec214349SKristof Provost 
135ec214349SKristof Provost /** Retrieves the errno associated with the error, if any. */
136ec214349SKristof Provost int ifconfig_err_errno(ifconfig_handle_t *h);
137ec214349SKristof Provost 
138b1d757bcSAlan Somers typedef void (*ifconfig_foreach_func_t)(ifconfig_handle_t *h,
139b1d757bcSAlan Somers     struct ifaddrs *ifa, void *udata);
140b1d757bcSAlan Somers 
141b1d757bcSAlan Somers /** Iterate over every network interface
142b1d757bcSAlan Somers  * @param h	An open ifconfig state object
143b1d757bcSAlan Somers  * @param cb	A callback function to call with a pointer to each interface
144b1d757bcSAlan Somers  * @param udata	An opaque value that will be passed to the callback.
145b1d757bcSAlan Somers  * @return	0 on success, nonzero if the list could not be iterated
146b1d757bcSAlan Somers  */
147b1d757bcSAlan Somers int ifconfig_foreach_iface(ifconfig_handle_t *h, ifconfig_foreach_func_t cb,
148b1d757bcSAlan Somers     void *udata);
149b1d757bcSAlan Somers 
150b1d757bcSAlan Somers /** Iterate over every address on a single network interface
151b1d757bcSAlan Somers  * @param h	An open ifconfig state object
152b1d757bcSAlan Somers  * @param ifa	A pointer that was supplied by a previous call to
153b1d757bcSAlan Somers  *              ifconfig_foreach_iface
154b1d757bcSAlan Somers  * @param udata	An opaque value that will be passed to the callback.
155b1d757bcSAlan Somers  * @param cb	A callback function to call with a pointer to each ifaddr
156b1d757bcSAlan Somers  */
157b1d757bcSAlan Somers void ifconfig_foreach_ifaddr(ifconfig_handle_t *h, struct ifaddrs *ifa,
158b1d757bcSAlan Somers     ifconfig_foreach_func_t cb, void *udata);
159b1d757bcSAlan Somers 
160ec214349SKristof Provost /** If error type was IOCTL, this identifies which request failed. */
161ec214349SKristof Provost unsigned long ifconfig_err_ioctlreq(ifconfig_handle_t *h);
162ec214349SKristof Provost int ifconfig_get_description(ifconfig_handle_t *h, const char *name,
163ec214349SKristof Provost     char **description);
164ec214349SKristof Provost int ifconfig_set_description(ifconfig_handle_t *h, const char *name,
165ec214349SKristof Provost     const char *newdescription);
166ec214349SKristof Provost int ifconfig_unset_description(ifconfig_handle_t *h, const char *name);
1679a2ff315SKristof Provost int ifconfig_set_name(ifconfig_handle_t *h, const char *name,
1689a2ff315SKristof Provost     const char *newname);
169c2803f1aSAndriy Voskoboinyk int ifconfig_get_orig_name(ifconfig_handle_t *h, const char *ifname,
170c2803f1aSAndriy Voskoboinyk     char **orig_name);
171b1d757bcSAlan Somers int ifconfig_set_fib(ifconfig_handle_t *h, const char *name, int fib);
172b1d757bcSAlan Somers int ifconfig_get_fib(ifconfig_handle_t *h, const char *name, int *fib);
173ec214349SKristof Provost int ifconfig_set_mtu(ifconfig_handle_t *h, const char *name, const int mtu);
174ec214349SKristof Provost int ifconfig_get_mtu(ifconfig_handle_t *h, const char *name, int *mtu);
175b1d757bcSAlan Somers int ifconfig_get_nd6(ifconfig_handle_t *h, const char *name,
176b1d757bcSAlan Somers     struct in6_ndireq *nd);
1779a2ff315SKristof Provost int ifconfig_set_metric(ifconfig_handle_t *h, const char *name,
1789a2ff315SKristof Provost     const int metric);
179ec214349SKristof Provost int ifconfig_get_metric(ifconfig_handle_t *h, const char *name, int *metric);
180ec214349SKristof Provost int ifconfig_set_capability(ifconfig_handle_t *h, const char *name,
181ec214349SKristof Provost     const int capability);
182ec214349SKristof Provost int ifconfig_get_capability(ifconfig_handle_t *h, const char *name,
183ec214349SKristof Provost     struct ifconfig_capabilities *capability);
184ec214349SKristof Provost 
185b1d757bcSAlan Somers /** Retrieve the list of groups to which this interface belongs
186b1d757bcSAlan Somers  * @param h	An open ifconfig state object
187b1d757bcSAlan Somers  * @param name	The interface name
188b1d757bcSAlan Somers  * @param ifgr	return argument.  The caller is responsible for freeing
189b1d757bcSAlan Somers  *              ifgr->ifgr_groups
190b1d757bcSAlan Somers  * @return	0 on success, nonzero on failure
191b1d757bcSAlan Somers  */
192b1d757bcSAlan Somers int ifconfig_get_groups(ifconfig_handle_t *h, const char *name,
193b1d757bcSAlan Somers     struct ifgroupreq *ifgr);
194b1d757bcSAlan Somers int ifconfig_get_ifstatus(ifconfig_handle_t *h, const char *name,
195b1d757bcSAlan Somers     struct ifstat *stat);
196b1d757bcSAlan Somers 
197b1d757bcSAlan Somers /** Retrieve the interface media information
198b1d757bcSAlan Somers  * @param h	An open ifconfig state object
199b1d757bcSAlan Somers  * @param name	The interface name
200b1d757bcSAlan Somers  * @param ifmr	Return argument.  The caller is responsible for freeing it
201b1d757bcSAlan Somers  * @return	0 on success, nonzero on failure
202b1d757bcSAlan Somers  */
203b1d757bcSAlan Somers int ifconfig_media_get_mediareq(ifconfig_handle_t *h, const char *name,
204b1d757bcSAlan Somers     struct ifmediareq **ifmr);
205b1d757bcSAlan Somers const char *ifconfig_media_get_type(int ifmw);
206b1d757bcSAlan Somers const char *ifconfig_media_get_subtype(int ifmw);
207b1d757bcSAlan Somers const char *ifconfig_media_get_status(const struct ifmediareq *ifmr);
208b1d757bcSAlan Somers void ifconfig_media_get_options_string(int ifmw, char *buf, size_t buflen);
209b1d757bcSAlan Somers 
210b1d757bcSAlan Somers int ifconfig_carp_get_info(ifconfig_handle_t *h, const char *name,
211b1d757bcSAlan Somers     struct carpreq *carpr, int ncarpr);
212b1d757bcSAlan Somers 
213b1d757bcSAlan Somers /** Retrieve additional information about an inet address
214b1d757bcSAlan Somers  * @param h	An open ifconfig state object
215b1d757bcSAlan Somers  * @param name	The interface name
216b1d757bcSAlan Somers  * @param ifa	Pointer to the the address structure of interest
217b1d757bcSAlan Somers  * @param addr	Return argument.  It will be filled with additional information
218b1d757bcSAlan Somers  *              about the address.
219b1d757bcSAlan Somers  * @return	0 on success, nonzero on failure.
220b1d757bcSAlan Somers  */
221b1d757bcSAlan Somers int ifconfig_inet_get_addrinfo(ifconfig_handle_t *h,
222b1d757bcSAlan Somers     const char *name, struct ifaddrs *ifa, struct ifconfig_inet_addr *addr);
223b1d757bcSAlan Somers 
224b1d757bcSAlan Somers /** Retrieve additional information about an inet6 address
225b1d757bcSAlan Somers  * @param h	An open ifconfig state object
226b1d757bcSAlan Somers  * @param name	The interface name
227b1d757bcSAlan Somers  * @param ifa	Pointer to the the address structure of interest
228b1d757bcSAlan Somers  * @param addr	Return argument.  It will be filled with additional information
229b1d757bcSAlan Somers  *              about the address.
230b1d757bcSAlan Somers  * @return	0 on success, nonzero on failure.
231b1d757bcSAlan Somers  */
232b1d757bcSAlan Somers int ifconfig_inet6_get_addrinfo(ifconfig_handle_t *h,
233b1d757bcSAlan Somers     const char *name, struct ifaddrs *ifa, struct ifconfig_inet6_addr *addr);
234b1d757bcSAlan Somers 
235e5539fb6SRyan Moeller /** Retrieve additional information about a bridge(4) interface */
236e5539fb6SRyan Moeller int ifconfig_bridge_get_bridge_status(ifconfig_handle_t *h,
237e5539fb6SRyan Moeller     const char *name, struct ifconfig_bridge_status **bridge);
238e5539fb6SRyan Moeller 
239e5539fb6SRyan Moeller /** Frees the structure returned by ifconfig_bridge_get_bridge_status.  Does
240e5539fb6SRyan Moeller  * nothing if the argument is NULL
241e5539fb6SRyan Moeller  * @param bridge	Pointer to the structure to free
242e5539fb6SRyan Moeller  */
243e5539fb6SRyan Moeller void ifconfig_bridge_free_bridge_status(struct ifconfig_bridge_status *bridge);
244e5539fb6SRyan Moeller 
245b1d757bcSAlan Somers /** Retrieve additional information about a lagg(4) interface */
246b1d757bcSAlan Somers int ifconfig_lagg_get_lagg_status(ifconfig_handle_t *h,
247b1d757bcSAlan Somers     const char *name, struct ifconfig_lagg_status **lagg_status);
248b1d757bcSAlan Somers 
249b1d757bcSAlan Somers /** Retrieve additional information about a member of a lagg(4) interface */
250b1d757bcSAlan Somers int ifconfig_lagg_get_laggport_status(ifconfig_handle_t *h,
251b1d757bcSAlan Somers     const char *name, struct lagg_reqport *rp);
252b1d757bcSAlan Somers 
253e5539fb6SRyan Moeller /** Frees the structure returned by ifconfig_lagg_get_lagg_status.  Does
254e5539fb6SRyan Moeller  * nothing if the argument is NULL
255b1d757bcSAlan Somers  * @param laggstat	Pointer to the structure to free
256b1d757bcSAlan Somers  */
257b1d757bcSAlan Somers void ifconfig_lagg_free_lagg_status(struct ifconfig_lagg_status *laggstat);
258b1d757bcSAlan Somers 
259ec214349SKristof Provost /** Destroy a virtual interface
260ec214349SKristof Provost  * @param name Interface to destroy
261ec214349SKristof Provost  */
262ec214349SKristof Provost int ifconfig_destroy_interface(ifconfig_handle_t *h, const char *name);
263ec214349SKristof Provost 
264ec214349SKristof Provost /** Creates a (virtual) interface
265ec214349SKristof Provost  * @param name Name of interface to create. Example: bridge or bridge42
266ec214349SKristof Provost  * @param name ifname Is set to actual name of created interface
267ec214349SKristof Provost  */
268ec214349SKristof Provost int ifconfig_create_interface(ifconfig_handle_t *h, const char *name,
269ec214349SKristof Provost     char **ifname);
270b1d757bcSAlan Somers 
271b1d757bcSAlan Somers /** Creates a (virtual) interface
272b1d757bcSAlan Somers  * @param name Name of interface to create. Example: vlan0 or ix0.50
273b1d757bcSAlan Somers  * @param name ifname Is set to actual name of created interface
274b1d757bcSAlan Somers  * @param vlandev Name of interface to attach to
275b1d757bcSAlan Somers  * @param vlanid VLAN ID/Tag. Must not be 0.
276b1d757bcSAlan Somers  */
277b1d757bcSAlan Somers int ifconfig_create_interface_vlan(ifconfig_handle_t *h, const char *name,
278b1d757bcSAlan Somers     char **ifname, const char *vlandev, const unsigned short vlantag);
279b1d757bcSAlan Somers 
280b1d757bcSAlan Somers int ifconfig_set_vlantag(ifconfig_handle_t *h, const char *name,
281b1d757bcSAlan Somers     const char *vlandev, const unsigned short vlantag);
282*0710ec8cSRyan Moeller 
283*0710ec8cSRyan Moeller /** Gets the names of all interface cloners available on the system
284*0710ec8cSRyan Moeller  * @param bufp	Set to the address of the names buffer on success or NULL
285*0710ec8cSRyan Moeller  *              if an error occurs.  This buffer must be freed when done.
286*0710ec8cSRyan Moeller  * @param lenp	Set to the number of names in the returned buffer or 0
287*0710ec8cSRyan Moeller  * 		if an error occurs.  Each name is contained within an
288*0710ec8cSRyan Moeller  * 		IFNAMSIZ length slice of the buffer, for a total buffer
289*0710ec8cSRyan Moeller  * 		length of *lenp * IFNAMSIZ bytes.
290*0710ec8cSRyan Moeller  */
291*0710ec8cSRyan Moeller int ifconfig_list_cloners(ifconfig_handle_t *h, char **bufp, size_t *lenp);
292