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 * $FreeBSD$ 27 */ 28 29 #pragma once 30 31 #include <sys/types.h> 32 33 #include <net/if.h> 34 35 #include <netinet/in.h> 36 #include <netinet/ip_carp.h> 37 #include <netinet6/in6_var.h> 38 39 #define ND6_IFF_DEFAULTIF 0x8000 40 41 typedef enum { 42 OK = 0, 43 OTHER, 44 IOCTL, 45 SOCKET, 46 NETLINK 47 } ifconfig_errtype; 48 49 /* 50 * Opaque definition so calling application can just pass a 51 * pointer to it for library use. 52 */ 53 struct ifconfig_handle; 54 typedef struct ifconfig_handle ifconfig_handle_t; 55 56 struct ifaddrs; 57 struct ifbropreq; 58 struct ifbreq; 59 struct in6_ndireq; 60 struct lagg_reqall; 61 struct lagg_reqflags; 62 struct lagg_reqopts; 63 struct lagg_reqport; 64 65 /** Stores extra info associated with a bridge(4) interface */ 66 struct ifconfig_bridge_status { 67 struct ifbropreq *params; /**< current operational parameters */ 68 struct ifbreq *members; /**< list of bridge members */ 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 }; 293 294 int ifconfig_carp_get_vhid(ifconfig_handle_t *h, const char *name, 295 struct ifconfig_carp *carpr, uint32_t vhid); 296 int ifconfig_carp_get_info(ifconfig_handle_t *h, const char *name, 297 struct ifconfig_carp *carpr, size_t ncarp); 298 int ifconfig_carp_set_info(ifconfig_handle_t *h, const char *name, 299 const struct ifconfig_carp *carpr); 300 301 /** Retrieve additional information about an inet address 302 * @param h An open ifconfig state object 303 * @param name The interface name 304 * @param ifa Pointer to the address structure of interest 305 * @param addr Return argument. It will be filled with additional information 306 * about the address. 307 * @return 0 on success, nonzero on failure. 308 */ 309 int ifconfig_inet_get_addrinfo(ifconfig_handle_t *h, 310 const char *name, struct ifaddrs *ifa, struct ifconfig_inet_addr *addr); 311 312 /** Retrieve additional information about an inet6 address 313 * @param h An open ifconfig state object 314 * @param name The interface name 315 * @param ifa Pointer to the address structure of interest 316 * @param addr Return argument. It will be filled with additional information 317 * about the address. 318 * @return 0 on success, nonzero on failure. 319 */ 320 int ifconfig_inet6_get_addrinfo(ifconfig_handle_t *h, 321 const char *name, struct ifaddrs *ifa, struct ifconfig_inet6_addr *addr); 322 323 /** Retrieve additional information about a bridge(4) interface */ 324 int ifconfig_bridge_get_bridge_status(ifconfig_handle_t *h, 325 const char *name, struct ifconfig_bridge_status **bridge); 326 327 /** Frees the structure returned by ifconfig_bridge_get_bridge_status. Does 328 * nothing if the argument is NULL 329 * @param bridge Pointer to the structure to free 330 */ 331 void ifconfig_bridge_free_bridge_status(struct ifconfig_bridge_status *bridge); 332 333 /** Retrieve additional information about a lagg(4) interface */ 334 int ifconfig_lagg_get_lagg_status(ifconfig_handle_t *h, 335 const char *name, struct ifconfig_lagg_status **lagg_status); 336 337 /** Retrieve additional information about a member of a lagg(4) interface */ 338 int ifconfig_lagg_get_laggport_status(ifconfig_handle_t *h, 339 const char *name, struct lagg_reqport *rp); 340 341 /** Frees the structure returned by ifconfig_lagg_get_lagg_status. Does 342 * nothing if the argument is NULL 343 * @param laggstat Pointer to the structure to free 344 */ 345 void ifconfig_lagg_free_lagg_status(struct ifconfig_lagg_status *laggstat); 346 347 /** Destroy a virtual interface 348 * @param name Interface to destroy 349 */ 350 int ifconfig_destroy_interface(ifconfig_handle_t *h, const char *name); 351 352 /** Creates a (virtual) interface 353 * @param name Name of interface to create. Example: bridge or bridge42 354 * @param name ifname Is set to actual name of created interface 355 */ 356 int ifconfig_create_interface(ifconfig_handle_t *h, const char *name, 357 char **ifname); 358 359 /** Creates a (virtual) interface 360 * @param name Name of interface to create. Example: vlan0 or ix0.50 361 * @param name ifname Is set to actual name of created interface 362 * @param vlandev Name of interface to attach to 363 * @param vlanid VLAN ID/Tag. Must not be 0. 364 */ 365 int ifconfig_create_interface_vlan(ifconfig_handle_t *h, const char *name, 366 char **ifname, const char *vlandev, const unsigned short vlantag); 367 368 int ifconfig_set_vlantag(ifconfig_handle_t *h, const char *name, 369 const char *vlandev, const unsigned short vlantag); 370 371 /** Gets the names of all interface cloners available on the system 372 * @param bufp Set to the address of the names buffer on success or NULL 373 * if an error occurs. This buffer must be freed when done. 374 * @param lenp Set to the number of names in the returned buffer or 0 375 * if an error occurs. Each name is contained within an 376 * IFNAMSIZ length slice of the buffer, for a total buffer 377 * length of *lenp * IFNAMSIZ bytes. 378 */ 379 int ifconfig_list_cloners(ifconfig_handle_t *h, char **bufp, size_t *lenp); 380