1 /* 2 * Copyright (c) 2016-2017, Marie Helene Kvello-Aune 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without modification, 6 * are permitted provided that the following conditions are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright notice, 9 * thislist of conditions and the following disclaimer. 10 * 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. 14 * 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. 25 */ 26 27 #pragma once 28 29 #include <sys/types.h> 30 31 #include <net/if.h> 32 33 #include <netinet/in.h> 34 #include <netinet6/in6_var.h> 35 36 #define ND6_IFF_DEFAULTIF 0x8000 37 38 typedef enum { 39 OK = 0, 40 OTHER, 41 IOCTL, 42 SOCKET 43 } ifconfig_errtype; 44 45 /* 46 * Opaque definition so calling application can just pass a 47 * pointer to it for library use. 48 */ 49 struct ifconfig_handle; 50 typedef struct ifconfig_handle ifconfig_handle_t; 51 52 struct carpreq; 53 struct ifaddrs; 54 struct ifbropreq; 55 struct ifbreq; 56 struct in6_ndireq; 57 struct lagg_reqall; 58 struct lagg_reqflags; 59 struct lagg_reqopts; 60 struct lagg_reqport; 61 62 /** Stores extra info associated with a bridge(4) interface */ 63 struct ifconfig_bridge_status { 64 struct ifbropreq *params; /**< current operational parameters */ 65 struct ifbreq *members; /**< list of bridge members */ 66 size_t members_count; /**< how many member interfaces */ 67 uint32_t cache_size; /**< size of address cache */ 68 uint32_t cache_lifetime; /**< address cache entry lifetime */ 69 }; 70 71 struct ifconfig_capabilities { 72 /** Current capabilities (ifconfig prints this as 'options')*/ 73 int curcap; 74 /** Requested capabilities (ifconfig prints this as 'capabilities')*/ 75 int reqcap; 76 }; 77 78 /** Stores extra info associated with an inet address */ 79 struct ifconfig_inet_addr { 80 const struct sockaddr_in *sin; 81 const struct sockaddr_in *netmask; 82 const struct sockaddr_in *dst; 83 const struct sockaddr_in *broadcast; 84 int prefixlen; 85 uint8_t vhid; 86 }; 87 88 /** Stores extra info associated with an inet6 address */ 89 struct ifconfig_inet6_addr { 90 struct sockaddr_in6 *sin6; 91 struct sockaddr_in6 *dstin6; 92 struct in6_addrlifetime lifetime; 93 int prefixlen; 94 uint32_t flags; 95 uint8_t vhid; 96 }; 97 98 /** Stores extra info associated with a lagg(4) interface */ 99 struct ifconfig_lagg_status { 100 struct lagg_reqall *ra; 101 struct lagg_reqopts *ro; 102 struct lagg_reqflags *rf; 103 }; 104 105 /** Retrieves a new state object for use in other API calls. 106 * Example usage: 107 *{@code 108 * // Create state object 109 * ifconfig_handle_t *lifh; 110 * lifh = ifconfig_open(); 111 * if (lifh == NULL) { 112 * // Handle error 113 * } 114 * 115 * // Do stuff with the handle 116 * 117 * // Dispose of the state object 118 * ifconfig_close(lifh); 119 * lifh = NULL; 120 *} 121 */ 122 ifconfig_handle_t *ifconfig_open(void); 123 124 /** Frees resources held in the provided state object. 125 * @param h The state object to close. 126 * @see #ifconfig_open(void) 127 */ 128 void ifconfig_close(ifconfig_handle_t *h); 129 130 /** Identifies what kind of error occurred. */ 131 ifconfig_errtype ifconfig_err_errtype(ifconfig_handle_t *h); 132 133 /** Retrieves the errno associated with the error, if any. */ 134 int ifconfig_err_errno(ifconfig_handle_t *h); 135 136 typedef void (*ifconfig_foreach_func_t)(ifconfig_handle_t *h, 137 struct ifaddrs *ifa, void *udata); 138 139 /** Iterate over every network interface 140 * @param h An open ifconfig state object 141 * @param cb A callback function to call with a pointer to each interface 142 * @param udata An opaque value that will be passed to the callback. 143 * @return 0 on success, nonzero if the list could not be iterated 144 */ 145 int ifconfig_foreach_iface(ifconfig_handle_t *h, ifconfig_foreach_func_t cb, 146 void *udata); 147 148 /** Iterate over every address on a single network interface 149 * @param h An open ifconfig state object 150 * @param ifa A pointer that was supplied by a previous call to 151 * ifconfig_foreach_iface 152 * @param udata An opaque value that will be passed to the callback. 153 * @param cb A callback function to call with a pointer to each ifaddr 154 */ 155 void ifconfig_foreach_ifaddr(ifconfig_handle_t *h, struct ifaddrs *ifa, 156 ifconfig_foreach_func_t cb, void *udata); 157 158 /** If error type was IOCTL, this identifies which request failed. */ 159 unsigned long ifconfig_err_ioctlreq(ifconfig_handle_t *h); 160 int ifconfig_get_description(ifconfig_handle_t *h, const char *name, 161 char **description); 162 int ifconfig_set_description(ifconfig_handle_t *h, const char *name, 163 const char *newdescription); 164 int ifconfig_unset_description(ifconfig_handle_t *h, const char *name); 165 int ifconfig_set_name(ifconfig_handle_t *h, const char *name, 166 const char *newname); 167 int ifconfig_get_orig_name(ifconfig_handle_t *h, const char *ifname, 168 char **orig_name); 169 int ifconfig_set_fib(ifconfig_handle_t *h, const char *name, int fib); 170 int ifconfig_get_fib(ifconfig_handle_t *h, const char *name, int *fib); 171 int ifconfig_set_mtu(ifconfig_handle_t *h, const char *name, const int mtu); 172 int ifconfig_get_mtu(ifconfig_handle_t *h, const char *name, int *mtu); 173 int ifconfig_get_nd6(ifconfig_handle_t *h, const char *name, 174 struct in6_ndireq *nd); 175 int ifconfig_set_metric(ifconfig_handle_t *h, const char *name, 176 const int metric); 177 int ifconfig_get_metric(ifconfig_handle_t *h, const char *name, int *metric); 178 int ifconfig_set_capability(ifconfig_handle_t *h, const char *name, 179 const int capability); 180 int ifconfig_get_capability(ifconfig_handle_t *h, const char *name, 181 struct ifconfig_capabilities *capability); 182 183 /** Retrieve the list of groups to which this interface belongs 184 * @param h An open ifconfig state object 185 * @param name The interface name 186 * @param ifgr return argument. The caller is responsible for freeing 187 * ifgr->ifgr_groups 188 * @return 0 on success, nonzero on failure 189 */ 190 int ifconfig_get_groups(ifconfig_handle_t *h, const char *name, 191 struct ifgroupreq *ifgr); 192 int ifconfig_get_ifstatus(ifconfig_handle_t *h, const char *name, 193 struct ifstat *stat); 194 195 /** Retrieve the interface media information 196 * @param h An open ifconfig state object 197 * @param name The interface name 198 * @param ifmr Return argument. The caller is responsible for freeing it 199 * @return 0 on success, nonzero on failure 200 */ 201 int ifconfig_media_get_mediareq(ifconfig_handle_t *h, const char *name, 202 struct ifmediareq **ifmr); 203 204 const char *ifconfig_media_get_status(const struct ifmediareq *ifmr); 205 206 typedef int ifmedia_t; 207 208 #define INVALID_IFMEDIA ((ifmedia_t)-1) 209 210 /** Retrieve the name of a media type 211 * @param media The media to be named 212 * @return A pointer to the media type name, or NULL on failure 213 */ 214 const char *ifconfig_media_get_type(ifmedia_t media); 215 216 /** Retrieve a media type by its name 217 * @param name The name of a media type 218 * @return The media type value, or INVALID_IFMEDIA on failure 219 */ 220 ifmedia_t ifconfig_media_lookup_type(const char *name); 221 222 /** Retrieve the name of a media subtype 223 * @param media The media subtype to be named 224 * @return A pointer to the media subtype name, or NULL on failure 225 */ 226 const char *ifconfig_media_get_subtype(ifmedia_t media); 227 228 /** Retrieve a media subtype by its name 229 * @param media The top level media type whose subtype we want 230 * @param name The name of a media subtype 231 * @return The media subtype value, or INVALID_IFMEDIA on failure 232 */ 233 ifmedia_t ifconfig_media_lookup_subtype(ifmedia_t media, const char *name); 234 235 /** Retrieve the name of a media mode 236 * @param media The media mode to be named 237 * @return A pointer to the media mode name, or NULL on failure 238 */ 239 const char *ifconfig_media_get_mode(ifmedia_t media); 240 241 /** Retrieve a media mode by its name 242 * @param media The top level media type whose mode we want 243 * @param name The name of a media mode 244 * @return The media mode value, or INVALID_IFMEDIA on failure 245 */ 246 ifmedia_t ifconfig_media_lookup_mode(ifmedia_t media, const char *name); 247 248 /** Retrieve an array of media options 249 * @param media The media for which to obtain the options 250 * @return Pointer to an array of pointers to option names, 251 * terminated by a NULL pointer, or simply NULL on failure. 252 * The caller is responsible for freeing the array but not its 253 * contents. 254 */ 255 const char **ifconfig_media_get_options(ifmedia_t media); 256 257 /** Retrieve an array of media options by names 258 * @param media The top level media type whose options we want 259 * @param opts Pointer to an array of string pointers naming options 260 * @param nopts Number of elements in the opts array 261 * @return Pointer to an array of media options, one for each option named 262 * in opts. NULL is returned instead with errno set to ENOMEM if 263 * allocating the return array fails or EINVAL if media is not 264 * valid. A media option in the array will be INVALID_IFMEDIA 265 * when lookup failed for the option named in that position in 266 * opts. The caller is responsible for freeing the array. 267 */ 268 ifmedia_t *ifconfig_media_lookup_options(ifmedia_t media, const char **opts, 269 size_t nopts); 270 271 /** Retrieve the reason the interface is down 272 * @param h An open ifconfig state object 273 * @param name The interface name 274 * @param ifdr Return argument. 275 * @return 0 on success, nonzero on failure 276 */ 277 int ifconfig_media_get_downreason(ifconfig_handle_t *h, const char *name, 278 struct ifdownreason *ifdr); 279 280 int ifconfig_carp_get_info(ifconfig_handle_t *h, const char *name, 281 struct carpreq *carpr, int ncarpr); 282 283 /** Retrieve additional information about an inet address 284 * @param h An open ifconfig state object 285 * @param name The interface name 286 * @param ifa Pointer to the address structure of interest 287 * @param addr Return argument. It will be filled with additional information 288 * about the address. 289 * @return 0 on success, nonzero on failure. 290 */ 291 int ifconfig_inet_get_addrinfo(ifconfig_handle_t *h, 292 const char *name, struct ifaddrs *ifa, struct ifconfig_inet_addr *addr); 293 294 /** Retrieve additional information about an inet6 address 295 * @param h An open ifconfig state object 296 * @param name The interface name 297 * @param ifa Pointer to the address structure of interest 298 * @param addr Return argument. It will be filled with additional information 299 * about the address. 300 * @return 0 on success, nonzero on failure. 301 */ 302 int ifconfig_inet6_get_addrinfo(ifconfig_handle_t *h, 303 const char *name, struct ifaddrs *ifa, struct ifconfig_inet6_addr *addr); 304 305 /** Retrieve additional information about a bridge(4) interface */ 306 int ifconfig_bridge_get_bridge_status(ifconfig_handle_t *h, 307 const char *name, struct ifconfig_bridge_status **bridge); 308 309 /** Frees the structure returned by ifconfig_bridge_get_bridge_status. Does 310 * nothing if the argument is NULL 311 * @param bridge Pointer to the structure to free 312 */ 313 void ifconfig_bridge_free_bridge_status(struct ifconfig_bridge_status *bridge); 314 315 /** Retrieve additional information about a lagg(4) interface */ 316 int ifconfig_lagg_get_lagg_status(ifconfig_handle_t *h, 317 const char *name, struct ifconfig_lagg_status **lagg_status); 318 319 /** Retrieve additional information about a member of a lagg(4) interface */ 320 int ifconfig_lagg_get_laggport_status(ifconfig_handle_t *h, 321 const char *name, struct lagg_reqport *rp); 322 323 /** Frees the structure returned by ifconfig_lagg_get_lagg_status. Does 324 * nothing if the argument is NULL 325 * @param laggstat Pointer to the structure to free 326 */ 327 void ifconfig_lagg_free_lagg_status(struct ifconfig_lagg_status *laggstat); 328 329 /** Destroy a virtual interface 330 * @param name Interface to destroy 331 */ 332 int ifconfig_destroy_interface(ifconfig_handle_t *h, const char *name); 333 334 /** Creates a (virtual) interface 335 * @param name Name of interface to create. Example: bridge or bridge42 336 * @param name ifname Is set to actual name of created interface 337 */ 338 int ifconfig_create_interface(ifconfig_handle_t *h, const char *name, 339 char **ifname); 340 341 /** Creates a (virtual) interface 342 * @param name Name of interface to create. Example: vlan0 or ix0.50 343 * @param name ifname Is set to actual name of created interface 344 * @param vlandev Name of interface to attach to 345 * @param vlanid VLAN ID/Tag. Must not be 0. 346 */ 347 int ifconfig_create_interface_vlan(ifconfig_handle_t *h, const char *name, 348 char **ifname, const char *vlandev, const unsigned short vlantag); 349 350 int ifconfig_set_vlantag(ifconfig_handle_t *h, const char *name, 351 const char *vlandev, const unsigned short vlantag); 352 353 /** Gets the names of all interface cloners available on the system 354 * @param bufp Set to the address of the names buffer on success or NULL 355 * if an error occurs. This buffer must be freed when done. 356 * @param lenp Set to the number of names in the returned buffer or 0 357 * if an error occurs. Each name is contained within an 358 * IFNAMSIZ length slice of the buffer, for a total buffer 359 * length of *lenp * IFNAMSIZ bytes. 360 */ 361 int ifconfig_list_cloners(ifconfig_handle_t *h, char **bufp, size_t *lenp); 362