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