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
317577abffreqlabs#include <sys/types.h>
327577abffreqlabs
337577abffreqlabs#include <net/if.h>
347577abffreqlabs
350d688beasomers#include <netinet/in.h>
360d688beasomers#include <netinet6/in6_var.h>
370d688beasomers
380d688beasomers#define ND6_IFF_DEFAULTIF    0x8000
390d688beasomers
40a5e10afkptypedef enum {
410d688beasomers	OK = 0,
420d688beasomers	OTHER,
430d688beasomers	IOCTL,
440d688beasomers	SOCKET
45e5074fakp} ifconfig_errtype;
46a5e10afkp
47a5e10afkp/*
48a5e10afkp * Opaque definition so calling application can just pass a
49a5e10afkp * pointer to it for library use.
50a5e10afkp */
51e5074fakpstruct ifconfig_handle;
520d688beasomerstypedef struct ifconfig_handle ifconfig_handle_t;
530d688beasomers
540d688beasomersstruct carpreq;
550d688beasomersstruct ifaddrs;
56251424cfreqlabsstruct ifbropreq;
57251424cfreqlabsstruct ifbreq;
580d688beasomersstruct in6_ndireq;
590d688beasomersstruct lagg_reqall;
600d688beasomersstruct lagg_reqflags;
610d688beasomersstruct lagg_reqopts;
620d688beasomersstruct lagg_reqport;
63a5e10afkp
64251424cfreqlabs/** Stores extra info associated with a bridge(4) interface */
65251424cfreqlabsstruct ifconfig_bridge_status {
66251424cfreqlabs	struct ifbropreq *params;	/**< current operational parameters */
67251424cfreqlabs	struct ifbreq *members;		/**< list of bridge members */
68251424cfreqlabs	size_t members_count;		/**< how many member interfaces */
69251424cfreqlabs	uint32_t cache_size;		/**< size of address cache */
70251424cfreqlabs	uint32_t cache_lifetime;	/**< address cache entry lifetime */
71251424cfreqlabs};
72251424cfreqlabs
73e5074fakpstruct ifconfig_capabilities {
74a5e10afkp	/** Current capabilities (ifconfig prints this as 'options')*/
75a5e10afkp	int curcap;
76a5e10afkp	/** Requested capabilities (ifconfig prints this as 'capabilities')*/
77a5e10afkp	int reqcap;
78a5e10afkp};
79a5e10afkp
800d688beasomers/** Stores extra info associated with an inet address */
810d688beasomersstruct ifconfig_inet_addr {
820d688beasomers	const struct sockaddr_in *sin;
830d688beasomers	const struct sockaddr_in *netmask;
840d688beasomers	const struct sockaddr_in *dst;
850d688beasomers	const struct sockaddr_in *broadcast;
860d688beasomers	int prefixlen;
870d688beasomers	uint8_t vhid;
880d688beasomers};
890d688beasomers
900d688beasomers/** Stores extra info associated with an inet6 address */
910d688beasomersstruct ifconfig_inet6_addr {
920d688beasomers	struct sockaddr_in6 *sin6;
930d688beasomers	struct sockaddr_in6 *dstin6;
940d688beasomers	struct in6_addrlifetime lifetime;
950d688beasomers	int prefixlen;
960d688beasomers	uint32_t flags;
970d688beasomers	uint8_t vhid;
980d688beasomers};
990d688beasomers
1000d688beasomers/** Stores extra info associated with a lagg(4) interface */
1010d688beasomersstruct ifconfig_lagg_status {
1020d688beasomers	struct lagg_reqall *ra;
1030d688beasomers	struct lagg_reqopts *ro;
1040d688beasomers	struct lagg_reqflags *rf;
1050d688beasomers};
1060d688beasomers
107a5e10afkp/** Retrieves a new state object for use in other API calls.
108a5e10afkp * Example usage:
109a5e10afkp *{@code
110a5e10afkp * // Create state object
1110d688beasomers * ifconfig_handle_t *lifh;
1120d688beasomers * lifh = ifconfig_open();
1130d688beasomers * if (lifh == NULL) {
1140d688beasomers *     // Handle error
1150d688beasomers * }
116a5e10afkp *
1170d688beasomers * // Do stuff with the handle
118a5e10afkp *
119a5e10afkp * // Dispose of the state object
120e5074fakp * ifconfig_close(lifh);
121a5e10afkp * lifh = NULL;
122a5e10afkp *}
123a5e10afkp */
124e5074fakpifconfig_handle_t *ifconfig_open(void);
125a5e10afkp
126a5e10afkp/** Frees resources held in the provided state object.
127a5e10afkp * @param h The state object to close.
128e5074fakp * @see #ifconfig_open(void)
129a5e10afkp */
130e5074fakpvoid ifconfig_close(ifconfig_handle_t *h);
131a5e10afkp
132a5e10afkp/** Identifies what kind of error occured. */
133e5074fakpifconfig_errtype ifconfig_err_errtype(ifconfig_handle_t *h);
134a5e10afkp
135a5e10afkp/** Retrieves the errno associated with the error, if any. */
136e5074fakpint ifconfig_err_errno(ifconfig_handle_t *h);
137a5e10afkp
1380d688beasomerstypedef void (*ifconfig_foreach_func_t)(ifconfig_handle_t *h,
1390d688beasomers    struct ifaddrs *ifa, void *udata);
1400d688beasomers
1410d688beasomers/** Iterate over every network interface
1420d688beasomers * @param h	An open ifconfig state object
1430d688beasomers * @param cb	A callback function to call with a pointer to each interface
1440d688beasomers * @param udata	An opaque value that will be passed to the callback.
1450d688beasomers * @return	0 on success, nonzero if the list could not be iterated
1460d688beasomers */
1470d688beasomersint ifconfig_foreach_iface(ifconfig_handle_t *h, ifconfig_foreach_func_t cb,
1480d688beasomers    void *udata);
1490d688beasomers
1500d688beasomers/** Iterate over every address on a single network interface
1510d688beasomers * @param h	An open ifconfig state object
1520d688beasomers * @param ifa	A pointer that was supplied by a previous call to
1530d688beasomers *              ifconfig_foreach_iface
1540d688beasomers * @param udata	An opaque value that will be passed to the callback.
1550d688beasomers * @param cb	A callback function to call with a pointer to each ifaddr
1560d688beasomers */
1570d688beasomersvoid ifconfig_foreach_ifaddr(ifconfig_handle_t *h, struct ifaddrs *ifa,
1580d688beasomers    ifconfig_foreach_func_t cb, void *udata);
1590d688beasomers
160a5e10afkp/** If error type was IOCTL, this identifies which request failed. */
161e5074fakpunsigned long ifconfig_err_ioctlreq(ifconfig_handle_t *h);
162e5074fakpint ifconfig_get_description(ifconfig_handle_t *h, const char *name,
163a5e10afkp    char **description);
164e5074fakpint ifconfig_set_description(ifconfig_handle_t *h, const char *name,
165a5e10afkp    const char *newdescription);
166e5074fakpint ifconfig_unset_description(ifconfig_handle_t *h, const char *name);
1670725653kpint ifconfig_set_name(ifconfig_handle_t *h, const char *name,
1680725653kp    const char *newname);
169ef389b0avosint ifconfig_get_orig_name(ifconfig_handle_t *h, const char *ifname,
170ef389b0avos    char **orig_name);
1710d688beasomersint ifconfig_set_fib(ifconfig_handle_t *h, const char *name, int fib);
1720d688beasomersint ifconfig_get_fib(ifconfig_handle_t *h, const char *name, int *fib);
173e5074fakpint ifconfig_set_mtu(ifconfig_handle_t *h, const char *name, const int mtu);
174e5074fakpint ifconfig_get_mtu(ifconfig_handle_t *h, const char *name, int *mtu);
1750d688beasomersint ifconfig_get_nd6(ifconfig_handle_t *h, const char *name,
1760d688beasomers    struct in6_ndireq *nd);
1770725653kpint ifconfig_set_metric(ifconfig_handle_t *h, const char *name,
1780725653kp    const int metric);
179e5074fakpint ifconfig_get_metric(ifconfig_handle_t *h, const char *name, int *metric);
180e5074fakpint ifconfig_set_capability(ifconfig_handle_t *h, const char *name,
181a5e10afkp    const int capability);
182e5074fakpint ifconfig_get_capability(ifconfig_handle_t *h, const char *name,
183e5074fakp    struct ifconfig_capabilities *capability);
184a5e10afkp
1850d688beasomers/** Retrieve the list of groups to which this interface belongs
1860d688beasomers * @param h	An open ifconfig state object
1870d688beasomers * @param name	The interface name
1880d688beasomers * @param ifgr	return argument.  The caller is responsible for freeing
1890d688beasomers *              ifgr->ifgr_groups
1900d688beasomers * @return	0 on success, nonzero on failure
1910d688beasomers */
1920d688beasomersint ifconfig_get_groups(ifconfig_handle_t *h, const char *name,
1930d688beasomers    struct ifgroupreq *ifgr);
1940d688beasomersint ifconfig_get_ifstatus(ifconfig_handle_t *h, const char *name,
1950d688beasomers    struct ifstat *stat);
1960d688beasomers
1970d688beasomers/** Retrieve the interface media information
1980d688beasomers * @param h	An open ifconfig state object
1990d688beasomers * @param name	The interface name
2000d688beasomers * @param ifmr	Return argument.  The caller is responsible for freeing it
2010d688beasomers * @return	0 on success, nonzero on failure
2020d688beasomers */
2030d688beasomersint ifconfig_media_get_mediareq(ifconfig_handle_t *h, const char *name,
2040d688beasomers    struct ifmediareq **ifmr);
2050d688beasomersconst char *ifconfig_media_get_type(int ifmw);
2060d688beasomersconst char *ifconfig_media_get_subtype(int ifmw);
2070d688beasomersconst char *ifconfig_media_get_status(const struct ifmediareq *ifmr);
2080d688beasomersvoid ifconfig_media_get_options_string(int ifmw, char *buf, size_t buflen);
2090d688beasomers
2100d688beasomersint ifconfig_carp_get_info(ifconfig_handle_t *h, const char *name,
2110d688beasomers    struct carpreq *carpr, int ncarpr);
2120d688beasomers
2130d688beasomers/** Retrieve additional information about an inet address
2140d688beasomers * @param h	An open ifconfig state object
2150d688beasomers * @param name	The interface name
2160d688beasomers * @param ifa	Pointer to the the address structure of interest
2170d688beasomers * @param addr	Return argument.  It will be filled with additional information
2180d688beasomers *              about the address.
2190d688beasomers * @return	0 on success, nonzero on failure.
2200d688beasomers */
2210d688beasomersint ifconfig_inet_get_addrinfo(ifconfig_handle_t *h,
2220d688beasomers    const char *name, struct ifaddrs *ifa, struct ifconfig_inet_addr *addr);
2230d688beasomers
2240d688beasomers/** Retrieve additional information about an inet6 address
2250d688beasomers * @param h	An open ifconfig state object
2260d688beasomers * @param name	The interface name
2270d688beasomers * @param ifa	Pointer to the the address structure of interest
2280d688beasomers * @param addr	Return argument.  It will be filled with additional information
2290d688beasomers *              about the address.
2300d688beasomers * @return	0 on success, nonzero on failure.
2310d688beasomers */
2320d688beasomersint ifconfig_inet6_get_addrinfo(ifconfig_handle_t *h,
2330d688beasomers    const char *name, struct ifaddrs *ifa, struct ifconfig_inet6_addr *addr);
2340d688beasomers
235251424cfreqlabs/** Retrieve additional information about a bridge(4) interface */
236251424cfreqlabsint ifconfig_bridge_get_bridge_status(ifconfig_handle_t *h,
237251424cfreqlabs    const char *name, struct ifconfig_bridge_status **bridge);
238251424cfreqlabs
239251424cfreqlabs/** Frees the structure returned by ifconfig_bridge_get_bridge_status.  Does
240251424cfreqlabs * nothing if the argument is NULL
241251424cfreqlabs * @param bridge	Pointer to the structure to free
242251424cfreqlabs */
243251424cfreqlabsvoid ifconfig_bridge_free_bridge_status(struct ifconfig_bridge_status *bridge);
244251424cfreqlabs
2450d688beasomers/** Retrieve additional information about a lagg(4) interface */
2460d688beasomersint ifconfig_lagg_get_lagg_status(ifconfig_handle_t *h,
2470d688beasomers    const char *name, struct ifconfig_lagg_status **lagg_status);
2480d688beasomers
2490d688beasomers/** Retrieve additional information about a member of a lagg(4) interface */
2500d688beasomersint ifconfig_lagg_get_laggport_status(ifconfig_handle_t *h,
2510d688beasomers    const char *name, struct lagg_reqport *rp);
2520d688beasomers
253251424cfreqlabs/** Frees the structure returned by ifconfig_lagg_get_lagg_status.  Does
254251424cfreqlabs * nothing if the argument is NULL
2550d688beasomers * @param laggstat	Pointer to the structure to free
2560d688beasomers */
2570d688beasomersvoid ifconfig_lagg_free_lagg_status(struct ifconfig_lagg_status *laggstat);
2580d688beasomers
259a5e10afkp/** Destroy a virtual interface
260a5e10afkp * @param name Interface to destroy
261a5e10afkp */
262e5074fakpint ifconfig_destroy_interface(ifconfig_handle_t *h, const char *name);
263a5e10afkp
264a5e10afkp/** Creates a (virtual) interface
265a5e10afkp * @param name Name of interface to create. Example: bridge or bridge42
266a5e10afkp * @param name ifname Is set to actual name of created interface
267a5e10afkp */
268e5074fakpint ifconfig_create_interface(ifconfig_handle_t *h, const char *name,
269a5e10afkp    char **ifname);
2700d688beasomers
2710d688beasomers/** Creates a (virtual) interface
2720d688beasomers * @param name Name of interface to create. Example: vlan0 or ix0.50
2730d688beasomers * @param name ifname Is set to actual name of created interface
2740d688beasomers * @param vlandev Name of interface to attach to
2750d688beasomers * @param vlanid VLAN ID/Tag. Must not be 0.
2760d688beasomers */
2770d688beasomersint ifconfig_create_interface_vlan(ifconfig_handle_t *h, const char *name,
2780d688beasomers    char **ifname, const char *vlandev, const unsigned short vlantag);
2790d688beasomers
2800d688beasomersint ifconfig_set_vlantag(ifconfig_handle_t *h, const char *name,
2810d688beasomers    const char *vlandev, const unsigned short vlantag);
282