2 * Copyright (c) 2016-2017, Marie Helene Kvello-Aune
5 * Redistribution and use in source and binary forms, with or without modification,
6 * are permitted provided that the following conditions are met:
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * thislist of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation and/or
13 * other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
17 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #include <sys/types.h>
35 #include <netinet/in.h>
36 #include <netinet6/in6_var.h>
38 #define ND6_IFF_DEFAULTIF 0x8000
48 * Opaque definition so calling application can just pass a
49 * pointer to it for library use.
51 struct ifconfig_handle;
52 typedef struct ifconfig_handle ifconfig_handle_t;
64 /** Stores extra info associated with a bridge(4) interface */
65 struct ifconfig_bridge_status {
66 struct ifbropreq *params; /**< current operational parameters */
67 struct ifbreq *members; /**< list of bridge members */
68 size_t members_count; /**< how many member interfaces */
69 uint32_t cache_size; /**< size of address cache */
70 uint32_t cache_lifetime; /**< address cache entry lifetime */
73 struct ifconfig_capabilities {
74 /** Current capabilities (ifconfig prints this as 'options')*/
76 /** Requested capabilities (ifconfig prints this as 'capabilities')*/
80 /** Stores extra info associated with an inet address */
81 struct ifconfig_inet_addr {
82 const struct sockaddr_in *sin;
83 const struct sockaddr_in *netmask;
84 const struct sockaddr_in *dst;
85 const struct sockaddr_in *broadcast;
90 /** Stores extra info associated with an inet6 address */
91 struct ifconfig_inet6_addr {
92 struct sockaddr_in6 *sin6;
93 struct sockaddr_in6 *dstin6;
94 struct in6_addrlifetime lifetime;
100 /** Stores extra info associated with a lagg(4) interface */
101 struct ifconfig_lagg_status {
102 struct lagg_reqall *ra;
103 struct lagg_reqopts *ro;
104 struct lagg_reqflags *rf;
107 /** Retrieves a new state object for use in other API calls.
110 * // Create state object
111 * ifconfig_handle_t *lifh;
112 * lifh = ifconfig_open();
113 * if (lifh == NULL) {
117 * // Do stuff with the handle
119 * // Dispose of the state object
120 * ifconfig_close(lifh);
124 ifconfig_handle_t *ifconfig_open(void);
126 /** Frees resources held in the provided state object.
127 * @param h The state object to close.
128 * @see #ifconfig_open(void)
130 void ifconfig_close(ifconfig_handle_t *h);
132 /** Identifies what kind of error occurred. */
133 ifconfig_errtype ifconfig_err_errtype(ifconfig_handle_t *h);
135 /** Retrieves the errno associated with the error, if any. */
136 int ifconfig_err_errno(ifconfig_handle_t *h);
138 typedef void (*ifconfig_foreach_func_t)(ifconfig_handle_t *h,
139 struct ifaddrs *ifa, void *udata);
141 /** Iterate over every network interface
142 * @param h An open ifconfig state object
143 * @param cb A callback function to call with a pointer to each interface
144 * @param udata An opaque value that will be passed to the callback.
145 * @return 0 on success, nonzero if the list could not be iterated
147 int ifconfig_foreach_iface(ifconfig_handle_t *h, ifconfig_foreach_func_t cb,
150 /** Iterate over every address on a single network interface
151 * @param h An open ifconfig state object
152 * @param ifa A pointer that was supplied by a previous call to
153 * ifconfig_foreach_iface
154 * @param udata An opaque value that will be passed to the callback.
155 * @param cb A callback function to call with a pointer to each ifaddr
157 void ifconfig_foreach_ifaddr(ifconfig_handle_t *h, struct ifaddrs *ifa,
158 ifconfig_foreach_func_t cb, void *udata);
160 /** If error type was IOCTL, this identifies which request failed. */
161 unsigned long ifconfig_err_ioctlreq(ifconfig_handle_t *h);
162 int ifconfig_get_description(ifconfig_handle_t *h, const char *name,
164 int ifconfig_set_description(ifconfig_handle_t *h, const char *name,
165 const char *newdescription);
166 int ifconfig_unset_description(ifconfig_handle_t *h, const char *name);
167 int ifconfig_set_name(ifconfig_handle_t *h, const char *name,
168 const char *newname);
169 int ifconfig_get_orig_name(ifconfig_handle_t *h, const char *ifname,
171 int ifconfig_set_fib(ifconfig_handle_t *h, const char *name, int fib);
172 int ifconfig_get_fib(ifconfig_handle_t *h, const char *name, int *fib);
173 int ifconfig_set_mtu(ifconfig_handle_t *h, const char *name, const int mtu);
174 int ifconfig_get_mtu(ifconfig_handle_t *h, const char *name, int *mtu);
175 int ifconfig_get_nd6(ifconfig_handle_t *h, const char *name,
176 struct in6_ndireq *nd);
177 int ifconfig_set_metric(ifconfig_handle_t *h, const char *name,
179 int ifconfig_get_metric(ifconfig_handle_t *h, const char *name, int *metric);
180 int ifconfig_set_capability(ifconfig_handle_t *h, const char *name,
181 const int capability);
182 int ifconfig_get_capability(ifconfig_handle_t *h, const char *name,
183 struct ifconfig_capabilities *capability);
185 /** Retrieve the list of groups to which this interface belongs
186 * @param h An open ifconfig state object
187 * @param name The interface name
188 * @param ifgr return argument. The caller is responsible for freeing
190 * @return 0 on success, nonzero on failure
192 int ifconfig_get_groups(ifconfig_handle_t *h, const char *name,
193 struct ifgroupreq *ifgr);
194 int ifconfig_get_ifstatus(ifconfig_handle_t *h, const char *name,
195 struct ifstat *stat);
197 /** Retrieve the interface media information
198 * @param h An open ifconfig state object
199 * @param name The interface name
200 * @param ifmr Return argument. The caller is responsible for freeing it
201 * @return 0 on success, nonzero on failure
203 int ifconfig_media_get_mediareq(ifconfig_handle_t *h, const char *name,
204 struct ifmediareq **ifmr);
206 const char *ifconfig_media_get_status(const struct ifmediareq *ifmr);
208 typedef int ifmedia_t;
210 #define INVALID_IFMEDIA ((ifmedia_t)-1)
212 /** Retrieve the name of a media type
213 * @param media The media to be named
214 * @return A pointer to the media type name, or NULL on failure
216 const char *ifconfig_media_get_type(ifmedia_t media);
218 /** Retrieve a media type by its name
219 * @param name The name of a media type
220 * @return The media type value, or INVALID_IFMEDIA on failure
222 ifmedia_t ifconfig_media_lookup_type(const char *name);
224 /** Retrieve the name of a media subtype
225 * @param media The media subtype to be named
226 * @return A pointer to the media subtype name, or NULL on failure
228 const char *ifconfig_media_get_subtype(ifmedia_t media);
230 /** Retrieve a media subtype by its name
231 * @param media The top level media type whose subtype we want
232 * @param name The name of a media subtype
233 * @return The media subtype value, or INVALID_IFMEDIA on failure
235 ifmedia_t ifconfig_media_lookup_subtype(ifmedia_t media, const char *name);
237 /** Retrieve the name of a media mode
238 * @param media The media mode to be named
239 * @return A pointer to the media mode name, or NULL on failure
241 const char *ifconfig_media_get_mode(ifmedia_t media);
243 /** Retrieve a media mode by its name
244 * @param media The top level media type whose mode we want
245 * @param name The name of a media mode
246 * @return The media mode value, or INVALID_IFMEDIA on failure
248 ifmedia_t ifconfig_media_lookup_mode(ifmedia_t media, const char *name);
250 /** Retrieve an array of media options
251 * @param media The media for which to obtain the options
252 * @return Pointer to an array of pointers to option names,
253 * terminated by a NULL pointer, or simply NULL on failure.
254 * The caller is responsible for freeing the array but not its
257 const char **ifconfig_media_get_options(ifmedia_t media);
259 /** Retrieve an array of media options by names
260 * @param media The top level media type whose options we want
261 * @param opts Pointer to an array of string pointers naming options
262 * @param nopts Number of elements in the opts array
263 * @return Pointer to an array of media options, one for each option named
264 * in opts. NULL is returned instead with errno set to ENOMEM if
265 * allocating the return array fails or EINVAL if media is not
266 * valid. A media option in the array will be INVALID_IFMEDIA
267 * when lookup failed for the option named in that position in
268 * opts. The caller is responsible for freeing the array.
270 ifmedia_t *ifconfig_media_lookup_options(ifmedia_t media, const char **opts,
273 /** Retrieve the reason the interface is down
274 * @param h An open ifconfig state object
275 * @param name The interface name
276 * @param ifdr Return argument.
277 * @return 0 on success, nonzero on failure
279 int ifconfig_media_get_downreason(ifconfig_handle_t *h, const char *name,
280 struct ifdownreason *ifdr);
282 int ifconfig_carp_get_info(ifconfig_handle_t *h, const char *name,
283 struct carpreq *carpr, int ncarpr);
285 /** Retrieve additional information about an inet address
286 * @param h An open ifconfig state object
287 * @param name The interface name
288 * @param ifa Pointer to the address structure of interest
289 * @param addr Return argument. It will be filled with additional information
291 * @return 0 on success, nonzero on failure.
293 int ifconfig_inet_get_addrinfo(ifconfig_handle_t *h,
294 const char *name, struct ifaddrs *ifa, struct ifconfig_inet_addr *addr);
296 /** Retrieve additional information about an inet6 address
297 * @param h An open ifconfig state object
298 * @param name The interface name
299 * @param ifa Pointer to the address structure of interest
300 * @param addr Return argument. It will be filled with additional information
302 * @return 0 on success, nonzero on failure.
304 int ifconfig_inet6_get_addrinfo(ifconfig_handle_t *h,
305 const char *name, struct ifaddrs *ifa, struct ifconfig_inet6_addr *addr);
307 /** Retrieve additional information about a bridge(4) interface */
308 int ifconfig_bridge_get_bridge_status(ifconfig_handle_t *h,
309 const char *name, struct ifconfig_bridge_status **bridge);
311 /** Frees the structure returned by ifconfig_bridge_get_bridge_status. Does
312 * nothing if the argument is NULL
313 * @param bridge Pointer to the structure to free
315 void ifconfig_bridge_free_bridge_status(struct ifconfig_bridge_status *bridge);
317 /** Retrieve additional information about a lagg(4) interface */
318 int ifconfig_lagg_get_lagg_status(ifconfig_handle_t *h,
319 const char *name, struct ifconfig_lagg_status **lagg_status);
321 /** Retrieve additional information about a member of a lagg(4) interface */
322 int ifconfig_lagg_get_laggport_status(ifconfig_handle_t *h,
323 const char *name, struct lagg_reqport *rp);
325 /** Frees the structure returned by ifconfig_lagg_get_lagg_status. Does
326 * nothing if the argument is NULL
327 * @param laggstat Pointer to the structure to free
329 void ifconfig_lagg_free_lagg_status(struct ifconfig_lagg_status *laggstat);
331 /** Destroy a virtual interface
332 * @param name Interface to destroy
334 int ifconfig_destroy_interface(ifconfig_handle_t *h, const char *name);
336 /** Creates a (virtual) interface
337 * @param name Name of interface to create. Example: bridge or bridge42
338 * @param name ifname Is set to actual name of created interface
340 int ifconfig_create_interface(ifconfig_handle_t *h, const char *name,
343 /** Creates a (virtual) interface
344 * @param name Name of interface to create. Example: vlan0 or ix0.50
345 * @param name ifname Is set to actual name of created interface
346 * @param vlandev Name of interface to attach to
347 * @param vlanid VLAN ID/Tag. Must not be 0.
349 int ifconfig_create_interface_vlan(ifconfig_handle_t *h, const char *name,
350 char **ifname, const char *vlandev, const unsigned short vlantag);
352 int ifconfig_set_vlantag(ifconfig_handle_t *h, const char *name,
353 const char *vlandev, const unsigned short vlantag);
355 /** Gets the names of all interface cloners available on the system
356 * @param bufp Set to the address of the names buffer on success or NULL
357 * if an error occurs. This buffer must be freed when done.
358 * @param lenp Set to the number of names in the returned buffer or 0
359 * if an error occurs. Each name is contained within an
360 * IFNAMSIZ length slice of the buffer, for a total buffer
361 * length of *lenp * IFNAMSIZ bytes.
363 int ifconfig_list_cloners(ifconfig_handle_t *h, char **bufp, size_t *lenp);