1a5e10afkp/*
20d688beasomers * Copyright (c) 2016-2017, Marie Helene Kvello-Aune
3a5e10afkp * All rights reserved.
4a5e10afkp *
5a5e10afkp * Redistribution and use in source and binary forms, with or without modification,
6a5e10afkp * are permitted provided that the following conditions are met:
7a5e10afkp *
8a5e10afkp * 1. Redistributions of source code must retain the above copyright notice,
9a5e10afkp * thislist of conditions and the following disclaimer.
10a5e10afkp *
11a5e10afkp * 2. Redistributions in binary form must reproduce the above copyright notice,
12a5e10afkp * this list of conditions and the following disclaimer in the documentation and/or
13a5e10afkp * other materials provided with the distribution.
14a5e10afkp *
15a5e10afkp * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16a5e10afkp * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
17a5e10afkp * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18a5e10afkp * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
19a5e10afkp * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20a5e10afkp * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21a5e10afkp * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22a5e10afkp * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23a5e10afkp * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24a5e10afkp * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25a5e10afkp *
26a5e10afkp * $FreeBSD$
27a5e10afkp */
28a5e10afkp
29a5e10afkp#pragma once
30a5e10afkp
310d688beasomers#include <netinet/in.h>
320d688beasomers#include <netinet6/in6_var.h>
330d688beasomers
340d688beasomers#define ND6_IFF_DEFAULTIF    0x8000
350d688beasomers
36a5e10afkptypedef enum {
370d688beasomers	OK = 0,
380d688beasomers	OTHER,
390d688beasomers	IOCTL,
400d688beasomers	SOCKET
41e5074fakp} ifconfig_errtype;
42a5e10afkp
43a5e10afkp/*
44a5e10afkp * Opaque definition so calling application can just pass a
45a5e10afkp * pointer to it for library use.
46a5e10afkp */
47e5074fakpstruct ifconfig_handle;
480d688beasomerstypedef struct ifconfig_handle ifconfig_handle_t;
490d688beasomers
500d688beasomersstruct carpreq;
510d688beasomersstruct ifaddrs;
520d688beasomersstruct in6_ndireq;
530d688beasomersstruct lagg_reqall;
540d688beasomersstruct lagg_reqflags;
550d688beasomersstruct lagg_reqopts;
560d688beasomersstruct lagg_reqport;
57a5e10afkp
58e5074fakpstruct ifconfig_capabilities {
59a5e10afkp	/** Current capabilities (ifconfig prints this as 'options')*/
60a5e10afkp	int curcap;
61a5e10afkp	/** Requested capabilities (ifconfig prints this as 'capabilities')*/
62a5e10afkp	int reqcap;
63a5e10afkp};
64a5e10afkp
650d688beasomers/** Stores extra info associated with an inet address */
660d688beasomersstruct ifconfig_inet_addr {
670d688beasomers	const struct sockaddr_in *sin;
680d688beasomers	const struct sockaddr_in *netmask;
690d688beasomers	const struct sockaddr_in *dst;
700d688beasomers	const struct sockaddr_in *broadcast;
710d688beasomers	int prefixlen;
720d688beasomers	uint8_t vhid;
730d688beasomers};
740d688beasomers
750d688beasomers/** Stores extra info associated with an inet6 address */
760d688beasomersstruct ifconfig_inet6_addr {
770d688beasomers	struct sockaddr_in6 *sin6;
780d688beasomers	struct sockaddr_in6 *dstin6;
790d688beasomers	struct in6_addrlifetime lifetime;
800d688beasomers	int prefixlen;
810d688beasomers	uint32_t flags;
820d688beasomers	uint8_t vhid;
830d688beasomers};
840d688beasomers
850d688beasomers/** Stores extra info associated with a lagg(4) interface */
860d688beasomersstruct ifconfig_lagg_status {
870d688beasomers	struct lagg_reqall *ra;
880d688beasomers	struct lagg_reqopts *ro;
890d688beasomers	struct lagg_reqflags *rf;
900d688beasomers};
910d688beasomers
92a5e10afkp/** Retrieves a new state object for use in other API calls.
93a5e10afkp * Example usage:
94a5e10afkp *{@code
95a5e10afkp * // Create state object
960d688beasomers * ifconfig_handle_t *lifh;
970d688beasomers * lifh = ifconfig_open();
980d688beasomers * if (lifh == NULL) {
990d688beasomers *     // Handle error
1000d688beasomers * }
101a5e10afkp *
1020d688beasomers * // Do stuff with the handle
103a5e10afkp *
104a5e10afkp * // Dispose of the state object
105e5074fakp * ifconfig_close(lifh);
106a5e10afkp * lifh = NULL;
107a5e10afkp *}
108a5e10afkp */
109e5074fakpifconfig_handle_t *ifconfig_open(void);
110a5e10afkp
111a5e10afkp/** Frees resources held in the provided state object.
112a5e10afkp * @param h The state object to close.
113e5074fakp * @see #ifconfig_open(void)
114a5e10afkp */
115e5074fakpvoid ifconfig_close(ifconfig_handle_t *h);
116a5e10afkp
117a5e10afkp/** Identifies what kind of error occured. */
118e5074fakpifconfig_errtype ifconfig_err_errtype(ifconfig_handle_t *h);
119a5e10afkp
120a5e10afkp/** Retrieves the errno associated with the error, if any. */
121e5074fakpint ifconfig_err_errno(ifconfig_handle_t *h);
122a5e10afkp
1230d688beasomerstypedef void (*ifconfig_foreach_func_t)(ifconfig_handle_t *h,
1240d688beasomers    struct ifaddrs *ifa, void *udata);
1250d688beasomers
1260d688beasomers/** Iterate over every network interface
1270d688beasomers * @param h	An open ifconfig state object
1280d688beasomers * @param cb	A callback function to call with a pointer to each interface
1290d688beasomers * @param udata	An opaque value that will be passed to the callback.
1300d688beasomers * @return	0 on success, nonzero if the list could not be iterated
1310d688beasomers */
1320d688beasomersint ifconfig_foreach_iface(ifconfig_handle_t *h, ifconfig_foreach_func_t cb,
1330d688beasomers    void *udata);
1340d688beasomers
1350d688beasomers/** Iterate over every address on a single network interface
1360d688beasomers * @param h	An open ifconfig state object
1370d688beasomers * @param ifa	A pointer that was supplied by a previous call to
1380d688beasomers *              ifconfig_foreach_iface
1390d688beasomers * @param udata	An opaque value that will be passed to the callback.
1400d688beasomers * @param cb	A callback function to call with a pointer to each ifaddr
1410d688beasomers */
1420d688beasomersvoid ifconfig_foreach_ifaddr(ifconfig_handle_t *h, struct ifaddrs *ifa,
1430d688beasomers    ifconfig_foreach_func_t cb, void *udata);
1440d688beasomers
145a5e10afkp/** If error type was IOCTL, this identifies which request failed. */
146e5074fakpunsigned long ifconfig_err_ioctlreq(ifconfig_handle_t *h);
147e5074fakpint ifconfig_get_description(ifconfig_handle_t *h, const char *name,
148a5e10afkp    char **description);
149e5074fakpint ifconfig_set_description(ifconfig_handle_t *h, const char *name,
150a5e10afkp    const char *newdescription);
151e5074fakpint ifconfig_unset_description(ifconfig_handle_t *h, const char *name);
1520725653kpint ifconfig_set_name(ifconfig_handle_t *h, const char *name,
1530725653kp    const char *newname);
154ef389b0avosint ifconfig_get_orig_name(ifconfig_handle_t *h, const char *ifname,
155ef389b0avos    char **orig_name);
1560d688beasomersint ifconfig_set_fib(ifconfig_handle_t *h, const char *name, int fib);
1570d688beasomersint ifconfig_get_fib(ifconfig_handle_t *h, const char *name, int *fib);
158e5074fakpint ifconfig_set_mtu(ifconfig_handle_t *h, const char *name, const int mtu);
159e5074fakpint ifconfig_get_mtu(ifconfig_handle_t *h, const char *name, int *mtu);
1600d688beasomersint ifconfig_get_nd6(ifconfig_handle_t *h, const char *name,
1610d688beasomers    struct in6_ndireq *nd);
1620725653kpint ifconfig_set_metric(ifconfig_handle_t *h, const char *name,
1630725653kp    const int metric);
164e5074fakpint ifconfig_get_metric(ifconfig_handle_t *h, const char *name, int *metric);
165e5074fakpint ifconfig_set_capability(ifconfig_handle_t *h, const char *name,
166a5e10afkp    const int capability);
167e5074fakpint ifconfig_get_capability(ifconfig_handle_t *h, const char *name,
168e5074fakp    struct ifconfig_capabilities *capability);
169a5e10afkp
1700d688beasomers/** Retrieve the list of groups to which this interface belongs
1710d688beasomers * @param h	An open ifconfig state object
1720d688beasomers * @param name	The interface name
1730d688beasomers * @param ifgr	return argument.  The caller is responsible for freeing
1740d688beasomers *              ifgr->ifgr_groups
1750d688beasomers * @return	0 on success, nonzero on failure
1760d688beasomers */
1770d688beasomersint ifconfig_get_groups(ifconfig_handle_t *h, const char *name,
1780d688beasomers    struct ifgroupreq *ifgr);
1790d688beasomersint ifconfig_get_ifstatus(ifconfig_handle_t *h, const char *name,
1800d688beasomers    struct ifstat *stat);
1810d688beasomers
1820d688beasomers/** Retrieve the interface media information
1830d688beasomers * @param h	An open ifconfig state object
1840d688beasomers * @param name	The interface name
1850d688beasomers * @param ifmr	Return argument.  The caller is responsible for freeing it
1860d688beasomers * @return	0 on success, nonzero on failure
1870d688beasomers */
1880d688beasomersint ifconfig_media_get_mediareq(ifconfig_handle_t *h, const char *name,
1890d688beasomers    struct ifmediareq **ifmr);
1900d688beasomersconst char *ifconfig_media_get_type(int ifmw);
1910d688beasomersconst char *ifconfig_media_get_subtype(int ifmw);
1920d688beasomersconst char *ifconfig_media_get_status(const struct ifmediareq *ifmr);
1930d688beasomersvoid ifconfig_media_get_options_string(int ifmw, char *buf, size_t buflen);
1940d688beasomers
1950d688beasomersint ifconfig_carp_get_info(ifconfig_handle_t *h, const char *name,
1960d688beasomers    struct carpreq *carpr, int ncarpr);
1970d688beasomers
1980d688beasomers/** Retrieve additional information about an inet address
1990d688beasomers * @param h	An open ifconfig state object
2000d688beasomers * @param name	The interface name
2010d688beasomers * @param ifa	Pointer to the the address structure of interest
2020d688beasomers * @param addr	Return argument.  It will be filled with additional information
2030d688beasomers *              about the address.
2040d688beasomers * @return	0 on success, nonzero on failure.
2050d688beasomers */
2060d688beasomersint ifconfig_inet_get_addrinfo(ifconfig_handle_t *h,
2070d688beasomers    const char *name, struct ifaddrs *ifa, struct ifconfig_inet_addr *addr);
2080d688beasomers
2090d688beasomers/** Retrieve additional information about an inet6 address
2100d688beasomers * @param h	An open ifconfig state object
2110d688beasomers * @param name	The interface name
2120d688beasomers * @param ifa	Pointer to the the address structure of interest
2130d688beasomers * @param addr	Return argument.  It will be filled with additional information
2140d688beasomers *              about the address.
2150d688beasomers * @return	0 on success, nonzero on failure.
2160d688beasomers */
2170d688beasomersint ifconfig_inet6_get_addrinfo(ifconfig_handle_t *h,
2180d688beasomers    const char *name, struct ifaddrs *ifa, struct ifconfig_inet6_addr *addr);
2190d688beasomers
2200d688beasomers/** Retrieve additional information about a lagg(4) interface */
2210d688beasomersint ifconfig_lagg_get_lagg_status(ifconfig_handle_t *h,
2220d688beasomers    const char *name, struct ifconfig_lagg_status **lagg_status);
2230d688beasomers
2240d688beasomers/** Retrieve additional information about a member of a lagg(4) interface */
2250d688beasomersint ifconfig_lagg_get_laggport_status(ifconfig_handle_t *h,
2260d688beasomers    const char *name, struct lagg_reqport *rp);
2270d688beasomers
2280d688beasomers/** Frees the structure returned by ifconfig_lagg_get_status.  Does nothing if
2290d688beasomers * the argument is NULL
2300d688beasomers * @param laggstat	Pointer to the structure to free
2310d688beasomers */
2320d688beasomersvoid ifconfig_lagg_free_lagg_status(struct ifconfig_lagg_status *laggstat);
2330d688beasomers
234a5e10afkp/** Destroy a virtual interface
235a5e10afkp * @param name Interface to destroy
236a5e10afkp */
237e5074fakpint ifconfig_destroy_interface(ifconfig_handle_t *h, const char *name);
238a5e10afkp
239a5e10afkp/** Creates a (virtual) interface
240a5e10afkp * @param name Name of interface to create. Example: bridge or bridge42
241a5e10afkp * @param name ifname Is set to actual name of created interface
242a5e10afkp */
243e5074fakpint ifconfig_create_interface(ifconfig_handle_t *h, const char *name,
244a5e10afkp    char **ifname);
2450d688beasomers
2460d688beasomers/** Creates a (virtual) interface
2470d688beasomers * @param name Name of interface to create. Example: vlan0 or ix0.50
2480d688beasomers * @param name ifname Is set to actual name of created interface
2490d688beasomers * @param vlandev Name of interface to attach to
2500d688beasomers * @param vlanid VLAN ID/Tag. Must not be 0.
2510d688beasomers */
2520d688beasomersint ifconfig_create_interface_vlan(ifconfig_handle_t *h, const char *name,
2530d688beasomers    char **ifname, const char *vlandev, const unsigned short vlantag);
2540d688beasomers
2550d688beasomersint ifconfig_set_vlantag(ifconfig_handle_t *h, const char *name,
2560d688beasomers    const char *vlandev, const unsigned short vlantag);
257