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_get_fib(ifconfig_handle_t *h, const char *name, int *fib); 175 int ifconfig_set_mtu(ifconfig_handle_t *h, const char *name, const int mtu); 176 int ifconfig_get_mtu(ifconfig_handle_t *h, const char *name, int *mtu); 177 int ifconfig_get_nd6(ifconfig_handle_t *h, const char *name, 178 struct in6_ndireq *nd); 179 int ifconfig_set_metric(ifconfig_handle_t *h, const char *name, 180 const int metric); 181 int ifconfig_get_metric(ifconfig_handle_t *h, const char *name, int *metric); 182 int ifconfig_set_capability(ifconfig_handle_t *h, const char *name, 183 const int capability); 184 int ifconfig_get_capability(ifconfig_handle_t *h, const char *name, 185 struct ifconfig_capabilities *capability); 186 187 /** Retrieve the list of groups to which this interface belongs 188 * @param h An open ifconfig state object 189 * @param name The interface name 190 * @param ifgr return argument. The caller is responsible for freeing 191 * ifgr->ifgr_groups 192 * @return 0 on success, nonzero on failure 193 */ 194 int ifconfig_get_groups(ifconfig_handle_t *h, const char *name, 195 struct ifgroupreq *ifgr); 196 int ifconfig_get_ifstatus(ifconfig_handle_t *h, const char *name, 197 struct ifstat *stat); 198 199 /** Retrieve the interface media information 200 * @param h An open ifconfig state object 201 * @param name The interface name 202 * @param ifmr Return argument. The caller is responsible for freeing it 203 * @return 0 on success, nonzero on failure 204 */ 205 int ifconfig_media_get_mediareq(ifconfig_handle_t *h, const char *name, 206 struct ifmediareq **ifmr); 207 208 const char *ifconfig_media_get_status(const struct ifmediareq *ifmr); 209 210 typedef int ifmedia_t; 211 212 #define INVALID_IFMEDIA ((ifmedia_t)-1) 213 214 /** Retrieve the name of a media type 215 * @param media The media to be named 216 * @return A pointer to the media type name, or NULL on failure 217 */ 218 const char *ifconfig_media_get_type(ifmedia_t media); 219 220 /** Retrieve a media type by its name 221 * @param name The name of a media type 222 * @return The media type value, or INVALID_IFMEDIA on failure 223 */ 224 ifmedia_t ifconfig_media_lookup_type(const char *name); 225 226 /** Retrieve the name of a media subtype 227 * @param media The media subtype to be named 228 * @return A pointer to the media subtype name, or NULL on failure 229 */ 230 const char *ifconfig_media_get_subtype(ifmedia_t media); 231 232 /** Retrieve a media subtype by its name 233 * @param media The top level media type whose subtype we want 234 * @param name The name of a media subtype 235 * @return The media subtype value, or INVALID_IFMEDIA on failure 236 */ 237 ifmedia_t ifconfig_media_lookup_subtype(ifmedia_t media, const char *name); 238 239 /** Retrieve the name of a media mode 240 * @param media The media mode to be named 241 * @return A pointer to the media mode name, or NULL on failure 242 */ 243 const char *ifconfig_media_get_mode(ifmedia_t media); 244 245 /** Retrieve a media mode by its name 246 * @param media The top level media type whose mode we want 247 * @param name The name of a media mode 248 * @return The media mode value, or INVALID_IFMEDIA on failure 249 */ 250 ifmedia_t ifconfig_media_lookup_mode(ifmedia_t media, const char *name); 251 252 /** Retrieve an array of media options 253 * @param media The media for which to obtain the options 254 * @return Pointer to an array of pointers to option names, 255 * terminated by a NULL pointer, or simply NULL on failure. 256 * The caller is responsible for freeing the array but not its 257 * contents. 258 */ 259 const char **ifconfig_media_get_options(ifmedia_t media); 260 261 /** Retrieve an array of media options by names 262 * @param media The top level media type whose options we want 263 * @param opts Pointer to an array of string pointers naming options 264 * @param nopts Number of elements in the opts array 265 * @return Pointer to an array of media options, one for each option named 266 * in opts. NULL is returned instead with errno set to ENOMEM if 267 * allocating the return array fails or EINVAL if media is not 268 * valid. A media option in the array will be INVALID_IFMEDIA 269 * when lookup failed for the option named in that position in 270 * opts. The caller is responsible for freeing the array. 271 */ 272 ifmedia_t *ifconfig_media_lookup_options(ifmedia_t media, const char **opts, 273 size_t nopts); 274 275 /** Retrieve the reason the interface is down 276 * @param h An open ifconfig state object 277 * @param name The interface name 278 * @param ifdr Return argument. 279 * @return 0 on success, nonzero on failure 280 */ 281 int ifconfig_media_get_downreason(ifconfig_handle_t *h, const char *name, 282 struct ifdownreason *ifdr); 283 284 struct ifconfig_carp { 285 size_t carpr_count; 286 uint32_t carpr_vhid; 287 uint32_t carpr_state; 288 int32_t carpr_advbase; 289 int32_t carpr_advskew; 290 uint8_t carpr_key[CARP_KEY_LEN]; 291 struct in_addr carpr_addr; 292 struct in6_addr carpr_addr6; 293 carp_version_t carpr_version; 294 uint8_t carpr_vrrp_prio; 295 uint16_t carpr_vrrp_adv_inter; 296 }; 297 298 int ifconfig_carp_get_vhid(ifconfig_handle_t *h, const char *name, 299 struct ifconfig_carp *carpr, uint32_t vhid); 300 int ifconfig_carp_get_info(ifconfig_handle_t *h, const char *name, 301 struct ifconfig_carp *carpr, size_t ncarp); 302 int ifconfig_carp_set_info(ifconfig_handle_t *h, const char *name, 303 const struct ifconfig_carp *carpr); 304 305 /** Retrieve additional information about an inet address 306 * @param h An open ifconfig state object 307 * @param name The interface name 308 * @param ifa Pointer to the address structure of interest 309 * @param addr Return argument. It will be filled with additional information 310 * about the address. 311 * @return 0 on success, nonzero on failure. 312 */ 313 int ifconfig_inet_get_addrinfo(ifconfig_handle_t *h, 314 const char *name, struct ifaddrs *ifa, struct ifconfig_inet_addr *addr); 315 316 /** Retrieve additional information about an inet6 address 317 * @param h An open ifconfig state object 318 * @param name The interface name 319 * @param ifa Pointer to the address structure of interest 320 * @param addr Return argument. It will be filled with additional information 321 * about the address. 322 * @return 0 on success, nonzero on failure. 323 */ 324 int ifconfig_inet6_get_addrinfo(ifconfig_handle_t *h, 325 const char *name, struct ifaddrs *ifa, struct ifconfig_inet6_addr *addr); 326 327 /** Retrieve additional information about a bridge(4) interface */ 328 int ifconfig_bridge_get_bridge_status(ifconfig_handle_t *h, 329 const char *name, struct ifconfig_bridge_status **bridge); 330 331 /** Frees the structure returned by ifconfig_bridge_get_bridge_status. Does 332 * nothing if the argument is NULL 333 * @param bridge Pointer to the structure to free 334 */ 335 void ifconfig_bridge_free_bridge_status(struct ifconfig_bridge_status *bridge); 336 337 /** Retrieve additional information about a lagg(4) interface */ 338 int ifconfig_lagg_get_lagg_status(ifconfig_handle_t *h, 339 const char *name, struct ifconfig_lagg_status **lagg_status); 340 341 /** Retrieve additional information about a member of a lagg(4) interface */ 342 int ifconfig_lagg_get_laggport_status(ifconfig_handle_t *h, 343 const char *name, struct lagg_reqport *rp); 344 345 /** Frees the structure returned by ifconfig_lagg_get_lagg_status. Does 346 * nothing if the argument is NULL 347 * @param laggstat Pointer to the structure to free 348 */ 349 void ifconfig_lagg_free_lagg_status(struct ifconfig_lagg_status *laggstat); 350 351 /** Destroy a virtual interface 352 * @param name Interface to destroy 353 */ 354 int ifconfig_destroy_interface(ifconfig_handle_t *h, const char *name); 355 356 /** Creates a (virtual) interface 357 * @param name Name of interface to create. Example: bridge or bridge42 358 * @param name ifname Is set to actual name of created interface 359 */ 360 int ifconfig_create_interface(ifconfig_handle_t *h, const char *name, 361 char **ifname); 362 363 /** Creates a (virtual) interface 364 * @param name Name of interface to create. Example: vlan0 or ix0.50 365 * @param name ifname Is set to actual name of created interface 366 * @param vlandev Name of interface to attach to 367 * @param vlanid VLAN ID/Tag. Must not be 0. 368 */ 369 int ifconfig_create_interface_vlan(ifconfig_handle_t *h, const char *name, 370 char **ifname, const char *vlandev, const unsigned short vlantag); 371 372 int ifconfig_set_vlantag(ifconfig_handle_t *h, const char *name, 373 const char *vlandev, const unsigned short vlantag); 374 375 /** Gets the names of all interface cloners available on the system 376 * @param bufp Set to the address of the names buffer on success or NULL 377 * if an error occurs. This buffer must be freed when done. 378 * @param lenp Set to the number of names in the returned buffer or 0 379 * if an error occurs. Each name is contained within an 380 * IFNAMSIZ length slice of the buffer, for a total buffer 381 * length of *lenp * IFNAMSIZ bytes. 382 */ 383 int ifconfig_list_cloners(ifconfig_handle_t *h, char **bufp, size_t *lenp); 384