libifconfig.h 14 KB

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