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