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 occured. */
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);
205 const char *ifconfig_media_get_type(int ifmw);
206 const char *ifconfig_media_get_subtype(int ifmw);
207 const char *ifconfig_media_get_status(const struct ifmediareq *ifmr);
208 void ifconfig_media_get_options_string(int ifmw, char *buf, size_t buflen);
210 int ifconfig_carp_get_info(ifconfig_handle_t *h, const char *name,
211 struct carpreq *carpr, int ncarpr);
213 /** Retrieve additional information about an inet address
214 * @param h An open ifconfig state object
215 * @param name The interface name
216 * @param ifa Pointer to the the address structure of interest
217 * @param addr Return argument. It will be filled with additional information
219 * @return 0 on success, nonzero on failure.
221 int ifconfig_inet_get_addrinfo(ifconfig_handle_t *h,
222 const char *name, struct ifaddrs *ifa, struct ifconfig_inet_addr *addr);
224 /** Retrieve additional information about an inet6 address
225 * @param h An open ifconfig state object
226 * @param name The interface name
227 * @param ifa Pointer to the the address structure of interest
228 * @param addr Return argument. It will be filled with additional information
230 * @return 0 on success, nonzero on failure.
232 int ifconfig_inet6_get_addrinfo(ifconfig_handle_t *h,
233 const char *name, struct ifaddrs *ifa, struct ifconfig_inet6_addr *addr);
235 /** Retrieve additional information about a bridge(4) interface */
236 int ifconfig_bridge_get_bridge_status(ifconfig_handle_t *h,
237 const char *name, struct ifconfig_bridge_status **bridge);
239 /** Frees the structure returned by ifconfig_bridge_get_bridge_status. Does
240 * nothing if the argument is NULL
241 * @param bridge Pointer to the structure to free
243 void ifconfig_bridge_free_bridge_status(struct ifconfig_bridge_status *bridge);
245 /** Retrieve additional information about a lagg(4) interface */
246 int ifconfig_lagg_get_lagg_status(ifconfig_handle_t *h,
247 const char *name, struct ifconfig_lagg_status **lagg_status);
249 /** Retrieve additional information about a member of a lagg(4) interface */
250 int ifconfig_lagg_get_laggport_status(ifconfig_handle_t *h,
251 const char *name, struct lagg_reqport *rp);
253 /** Frees the structure returned by ifconfig_lagg_get_lagg_status. Does
254 * nothing if the argument is NULL
255 * @param laggstat Pointer to the structure to free
257 void ifconfig_lagg_free_lagg_status(struct ifconfig_lagg_status *laggstat);
259 /** Destroy a virtual interface
260 * @param name Interface to destroy
262 int ifconfig_destroy_interface(ifconfig_handle_t *h, const char *name);
264 /** Creates a (virtual) interface
265 * @param name Name of interface to create. Example: bridge or bridge42
266 * @param name ifname Is set to actual name of created interface
268 int ifconfig_create_interface(ifconfig_handle_t *h, const char *name,
271 /** Creates a (virtual) interface
272 * @param name Name of interface to create. Example: vlan0 or ix0.50
273 * @param name ifname Is set to actual name of created interface
274 * @param vlandev Name of interface to attach to
275 * @param vlanid VLAN ID/Tag. Must not be 0.
277 int ifconfig_create_interface_vlan(ifconfig_handle_t *h, const char *name,
278 char **ifname, const char *vlandev, const unsigned short vlantag);
280 int ifconfig_set_vlantag(ifconfig_handle_t *h, const char *name,
281 const char *vlandev, const unsigned short vlantag);
283 /** Gets the names of all interface cloners available on the system
284 * @param bufp Set to the address of the names buffer on success or NULL
285 * if an error occurs. This buffer must be freed when done.
286 * @param lenp Set to the number of names in the returned buffer or 0
287 * if an error occurs. Each name is contained within an
288 * IFNAMSIZ length slice of the buffer, for a total buffer
289 * length of *lenp * IFNAMSIZ bytes.
291 int ifconfig_list_cloners(ifconfig_handle_t *h, char **bufp, size_t *lenp);