xref: /freebsd/lib/libifconfig/libifconfig.h (revision b1d757bc1dc29cda592d632b955a706215975f1d)
1ec214349SKristof Provost /*
2*b1d757bcSAlan 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 
31*b1d757bcSAlan Somers #include <netinet/in.h>
32*b1d757bcSAlan Somers #include <netinet6/in6_var.h>
33*b1d757bcSAlan Somers 
34*b1d757bcSAlan Somers #define ND6_IFF_DEFAULTIF    0x8000
35*b1d757bcSAlan Somers 
36ec214349SKristof Provost typedef enum {
37*b1d757bcSAlan Somers 	OK = 0,
38*b1d757bcSAlan Somers 	OTHER,
39*b1d757bcSAlan Somers 	IOCTL,
40*b1d757bcSAlan Somers 	SOCKET
41ec214349SKristof Provost } ifconfig_errtype;
42ec214349SKristof Provost 
43ec214349SKristof Provost /*
44ec214349SKristof Provost  * Opaque definition so calling application can just pass a
45ec214349SKristof Provost  * pointer to it for library use.
46ec214349SKristof Provost  */
47ec214349SKristof Provost struct ifconfig_handle;
48ec214349SKristof Provost typedef struct ifconfig_handle ifconfig_handle_t;
49ec214349SKristof Provost 
50*b1d757bcSAlan Somers struct carpreq;
51*b1d757bcSAlan Somers struct ifaddrs;
52*b1d757bcSAlan Somers struct in6_ndireq;
53*b1d757bcSAlan Somers struct lagg_reqall;
54*b1d757bcSAlan Somers struct lagg_reqflags;
55*b1d757bcSAlan Somers struct lagg_reqopts;
56*b1d757bcSAlan Somers struct lagg_reqport;
57*b1d757bcSAlan Somers 
58ec214349SKristof Provost struct ifconfig_capabilities {
59ec214349SKristof Provost 	/** Current capabilities (ifconfig prints this as 'options')*/
60ec214349SKristof Provost 	int curcap;
61ec214349SKristof Provost 	/** Requested capabilities (ifconfig prints this as 'capabilities')*/
62ec214349SKristof Provost 	int reqcap;
63ec214349SKristof Provost };
64ec214349SKristof Provost 
65*b1d757bcSAlan Somers /** Stores extra info associated with an inet address */
66*b1d757bcSAlan Somers struct ifconfig_inet_addr {
67*b1d757bcSAlan Somers 	const struct sockaddr_in *sin;
68*b1d757bcSAlan Somers 	const struct sockaddr_in *netmask;
69*b1d757bcSAlan Somers 	const struct sockaddr_in *dst;
70*b1d757bcSAlan Somers 	const struct sockaddr_in *broadcast;
71*b1d757bcSAlan Somers 	int prefixlen;
72*b1d757bcSAlan Somers 	uint8_t vhid;
73*b1d757bcSAlan Somers };
74*b1d757bcSAlan Somers 
75*b1d757bcSAlan Somers /** Stores extra info associated with an inet6 address */
76*b1d757bcSAlan Somers struct ifconfig_inet6_addr {
77*b1d757bcSAlan Somers 	struct sockaddr_in6 *sin6;
78*b1d757bcSAlan Somers 	struct sockaddr_in6 *dstin6;
79*b1d757bcSAlan Somers 	struct in6_addrlifetime lifetime;
80*b1d757bcSAlan Somers 	int prefixlen;
81*b1d757bcSAlan Somers 	uint32_t flags;
82*b1d757bcSAlan Somers 	uint8_t vhid;
83*b1d757bcSAlan Somers };
84*b1d757bcSAlan Somers 
85*b1d757bcSAlan Somers /** Stores extra info associated with a lagg(4) interface */
86*b1d757bcSAlan Somers struct ifconfig_lagg_status {
87*b1d757bcSAlan Somers 	struct lagg_reqall *ra;
88*b1d757bcSAlan Somers 	struct lagg_reqopts *ro;
89*b1d757bcSAlan Somers 	struct lagg_reqflags *rf;
90*b1d757bcSAlan Somers };
91*b1d757bcSAlan Somers 
92ec214349SKristof Provost /** Retrieves a new state object for use in other API calls.
93ec214349SKristof Provost  * Example usage:
94ec214349SKristof Provost  *{@code
95ec214349SKristof Provost  * // Create state object
96*b1d757bcSAlan Somers  * ifconfig_handle_t *lifh;
97*b1d757bcSAlan Somers  * lifh = ifconfig_open();
98*b1d757bcSAlan Somers  * if (lifh == NULL) {
99*b1d757bcSAlan Somers  *     // Handle error
100*b1d757bcSAlan Somers  * }
101ec214349SKristof Provost  *
102*b1d757bcSAlan Somers  * // Do stuff with the handle
103ec214349SKristof Provost  *
104ec214349SKristof Provost  * // Dispose of the state object
105ec214349SKristof Provost  * ifconfig_close(lifh);
106ec214349SKristof Provost  * lifh = NULL;
107ec214349SKristof Provost  *}
108ec214349SKristof Provost  */
109ec214349SKristof Provost ifconfig_handle_t *ifconfig_open(void);
110ec214349SKristof Provost 
111ec214349SKristof Provost /** Frees resources held in the provided state object.
112ec214349SKristof Provost  * @param h The state object to close.
113ec214349SKristof Provost  * @see #ifconfig_open(void)
114ec214349SKristof Provost  */
115ec214349SKristof Provost void ifconfig_close(ifconfig_handle_t *h);
116ec214349SKristof Provost 
117ec214349SKristof Provost /** Identifies what kind of error occured. */
118ec214349SKristof Provost ifconfig_errtype ifconfig_err_errtype(ifconfig_handle_t *h);
119ec214349SKristof Provost 
120ec214349SKristof Provost /** Retrieves the errno associated with the error, if any. */
121ec214349SKristof Provost int ifconfig_err_errno(ifconfig_handle_t *h);
122ec214349SKristof Provost 
123*b1d757bcSAlan Somers typedef void (*ifconfig_foreach_func_t)(ifconfig_handle_t *h,
124*b1d757bcSAlan Somers     struct ifaddrs *ifa, void *udata);
125*b1d757bcSAlan Somers 
126*b1d757bcSAlan Somers /** Iterate over every network interface
127*b1d757bcSAlan Somers  * @param h	An open ifconfig state object
128*b1d757bcSAlan Somers  * @param cb	A callback function to call with a pointer to each interface
129*b1d757bcSAlan Somers  * @param udata	An opaque value that will be passed to the callback.
130*b1d757bcSAlan Somers  * @return	0 on success, nonzero if the list could not be iterated
131*b1d757bcSAlan Somers  */
132*b1d757bcSAlan Somers int ifconfig_foreach_iface(ifconfig_handle_t *h, ifconfig_foreach_func_t cb,
133*b1d757bcSAlan Somers     void *udata);
134*b1d757bcSAlan Somers 
135*b1d757bcSAlan Somers /** Iterate over every address on a single network interface
136*b1d757bcSAlan Somers  * @param h	An open ifconfig state object
137*b1d757bcSAlan Somers  * @param ifa	A pointer that was supplied by a previous call to
138*b1d757bcSAlan Somers  *              ifconfig_foreach_iface
139*b1d757bcSAlan Somers  * @param udata	An opaque value that will be passed to the callback.
140*b1d757bcSAlan Somers  * @param cb	A callback function to call with a pointer to each ifaddr
141*b1d757bcSAlan Somers  */
142*b1d757bcSAlan Somers void ifconfig_foreach_ifaddr(ifconfig_handle_t *h, struct ifaddrs *ifa,
143*b1d757bcSAlan Somers     ifconfig_foreach_func_t cb, void *udata);
144*b1d757bcSAlan Somers 
145ec214349SKristof Provost /** If error type was IOCTL, this identifies which request failed. */
146ec214349SKristof Provost unsigned long ifconfig_err_ioctlreq(ifconfig_handle_t *h);
147ec214349SKristof Provost int ifconfig_get_description(ifconfig_handle_t *h, const char *name,
148ec214349SKristof Provost     char **description);
149ec214349SKristof Provost int ifconfig_set_description(ifconfig_handle_t *h, const char *name,
150ec214349SKristof Provost     const char *newdescription);
151ec214349SKristof Provost int ifconfig_unset_description(ifconfig_handle_t *h, const char *name);
1529a2ff315SKristof Provost int ifconfig_set_name(ifconfig_handle_t *h, const char *name,
1539a2ff315SKristof Provost     const char *newname);
154c2803f1aSAndriy Voskoboinyk int ifconfig_get_orig_name(ifconfig_handle_t *h, const char *ifname,
155c2803f1aSAndriy Voskoboinyk     char **orig_name);
156*b1d757bcSAlan Somers int ifconfig_set_fib(ifconfig_handle_t *h, const char *name, int fib);
157*b1d757bcSAlan Somers int ifconfig_get_fib(ifconfig_handle_t *h, const char *name, int *fib);
158ec214349SKristof Provost int ifconfig_set_mtu(ifconfig_handle_t *h, const char *name, const int mtu);
159ec214349SKristof Provost int ifconfig_get_mtu(ifconfig_handle_t *h, const char *name, int *mtu);
160*b1d757bcSAlan Somers int ifconfig_get_nd6(ifconfig_handle_t *h, const char *name,
161*b1d757bcSAlan Somers     struct in6_ndireq *nd);
1629a2ff315SKristof Provost int ifconfig_set_metric(ifconfig_handle_t *h, const char *name,
1639a2ff315SKristof Provost     const int metric);
164ec214349SKristof Provost int ifconfig_get_metric(ifconfig_handle_t *h, const char *name, int *metric);
165ec214349SKristof Provost int ifconfig_set_capability(ifconfig_handle_t *h, const char *name,
166ec214349SKristof Provost     const int capability);
167ec214349SKristof Provost int ifconfig_get_capability(ifconfig_handle_t *h, const char *name,
168ec214349SKristof Provost     struct ifconfig_capabilities *capability);
169ec214349SKristof Provost 
170*b1d757bcSAlan Somers /** Retrieve the list of groups to which this interface belongs
171*b1d757bcSAlan Somers  * @param h	An open ifconfig state object
172*b1d757bcSAlan Somers  * @param name	The interface name
173*b1d757bcSAlan Somers  * @param ifgr	return argument.  The caller is responsible for freeing
174*b1d757bcSAlan Somers  *              ifgr->ifgr_groups
175*b1d757bcSAlan Somers  * @return	0 on success, nonzero on failure
176*b1d757bcSAlan Somers  */
177*b1d757bcSAlan Somers int ifconfig_get_groups(ifconfig_handle_t *h, const char *name,
178*b1d757bcSAlan Somers     struct ifgroupreq *ifgr);
179*b1d757bcSAlan Somers int ifconfig_get_ifstatus(ifconfig_handle_t *h, const char *name,
180*b1d757bcSAlan Somers     struct ifstat *stat);
181*b1d757bcSAlan Somers 
182*b1d757bcSAlan Somers /** Retrieve the interface media information
183*b1d757bcSAlan Somers  * @param h	An open ifconfig state object
184*b1d757bcSAlan Somers  * @param name	The interface name
185*b1d757bcSAlan Somers  * @param ifmr	Return argument.  The caller is responsible for freeing it
186*b1d757bcSAlan Somers  * @return	0 on success, nonzero on failure
187*b1d757bcSAlan Somers  */
188*b1d757bcSAlan Somers int ifconfig_media_get_mediareq(ifconfig_handle_t *h, const char *name,
189*b1d757bcSAlan Somers     struct ifmediareq **ifmr);
190*b1d757bcSAlan Somers const char *ifconfig_media_get_type(int ifmw);
191*b1d757bcSAlan Somers const char *ifconfig_media_get_subtype(int ifmw);
192*b1d757bcSAlan Somers const char *ifconfig_media_get_status(const struct ifmediareq *ifmr);
193*b1d757bcSAlan Somers void ifconfig_media_get_options_string(int ifmw, char *buf, size_t buflen);
194*b1d757bcSAlan Somers 
195*b1d757bcSAlan Somers int ifconfig_carp_get_info(ifconfig_handle_t *h, const char *name,
196*b1d757bcSAlan Somers     struct carpreq *carpr, int ncarpr);
197*b1d757bcSAlan Somers 
198*b1d757bcSAlan Somers /** Retrieve additional information about an inet address
199*b1d757bcSAlan Somers  * @param h	An open ifconfig state object
200*b1d757bcSAlan Somers  * @param name	The interface name
201*b1d757bcSAlan Somers  * @param ifa	Pointer to the the address structure of interest
202*b1d757bcSAlan Somers  * @param addr	Return argument.  It will be filled with additional information
203*b1d757bcSAlan Somers  *              about the address.
204*b1d757bcSAlan Somers  * @return	0 on success, nonzero on failure.
205*b1d757bcSAlan Somers  */
206*b1d757bcSAlan Somers int ifconfig_inet_get_addrinfo(ifconfig_handle_t *h,
207*b1d757bcSAlan Somers     const char *name, struct ifaddrs *ifa, struct ifconfig_inet_addr *addr);
208*b1d757bcSAlan Somers 
209*b1d757bcSAlan Somers /** Retrieve additional information about an inet6 address
210*b1d757bcSAlan Somers  * @param h	An open ifconfig state object
211*b1d757bcSAlan Somers  * @param name	The interface name
212*b1d757bcSAlan Somers  * @param ifa	Pointer to the the address structure of interest
213*b1d757bcSAlan Somers  * @param addr	Return argument.  It will be filled with additional information
214*b1d757bcSAlan Somers  *              about the address.
215*b1d757bcSAlan Somers  * @return	0 on success, nonzero on failure.
216*b1d757bcSAlan Somers  */
217*b1d757bcSAlan Somers int ifconfig_inet6_get_addrinfo(ifconfig_handle_t *h,
218*b1d757bcSAlan Somers     const char *name, struct ifaddrs *ifa, struct ifconfig_inet6_addr *addr);
219*b1d757bcSAlan Somers 
220*b1d757bcSAlan Somers /** Retrieve additional information about a lagg(4) interface */
221*b1d757bcSAlan Somers int ifconfig_lagg_get_lagg_status(ifconfig_handle_t *h,
222*b1d757bcSAlan Somers     const char *name, struct ifconfig_lagg_status **lagg_status);
223*b1d757bcSAlan Somers 
224*b1d757bcSAlan Somers /** Retrieve additional information about a member of a lagg(4) interface */
225*b1d757bcSAlan Somers int ifconfig_lagg_get_laggport_status(ifconfig_handle_t *h,
226*b1d757bcSAlan Somers     const char *name, struct lagg_reqport *rp);
227*b1d757bcSAlan Somers 
228*b1d757bcSAlan Somers /** Frees the structure returned by ifconfig_lagg_get_status.  Does nothing if
229*b1d757bcSAlan Somers  * the argument is NULL
230*b1d757bcSAlan Somers  * @param laggstat	Pointer to the structure to free
231*b1d757bcSAlan Somers  */
232*b1d757bcSAlan Somers void ifconfig_lagg_free_lagg_status(struct ifconfig_lagg_status *laggstat);
233*b1d757bcSAlan Somers 
234ec214349SKristof Provost /** Destroy a virtual interface
235ec214349SKristof Provost  * @param name Interface to destroy
236ec214349SKristof Provost  */
237ec214349SKristof Provost int ifconfig_destroy_interface(ifconfig_handle_t *h, const char *name);
238ec214349SKristof Provost 
239ec214349SKristof Provost /** Creates a (virtual) interface
240ec214349SKristof Provost  * @param name Name of interface to create. Example: bridge or bridge42
241ec214349SKristof Provost  * @param name ifname Is set to actual name of created interface
242ec214349SKristof Provost  */
243ec214349SKristof Provost int ifconfig_create_interface(ifconfig_handle_t *h, const char *name,
244ec214349SKristof Provost     char **ifname);
245*b1d757bcSAlan Somers 
246*b1d757bcSAlan Somers /** Creates a (virtual) interface
247*b1d757bcSAlan Somers  * @param name Name of interface to create. Example: vlan0 or ix0.50
248*b1d757bcSAlan Somers  * @param name ifname Is set to actual name of created interface
249*b1d757bcSAlan Somers  * @param vlandev Name of interface to attach to
250*b1d757bcSAlan Somers  * @param vlanid VLAN ID/Tag. Must not be 0.
251*b1d757bcSAlan Somers  */
252*b1d757bcSAlan Somers int ifconfig_create_interface_vlan(ifconfig_handle_t *h, const char *name,
253*b1d757bcSAlan Somers     char **ifname, const char *vlandev, const unsigned short vlantag);
254*b1d757bcSAlan Somers 
255*b1d757bcSAlan Somers int ifconfig_set_vlantag(ifconfig_handle_t *h, const char *name,
256*b1d757bcSAlan Somers     const char *vlandev, const unsigned short vlantag);
257