xref: /illumos-gate/usr/src/lib/libipadm/common/libipadm.h (revision ed1e93792d7c9ea04a0cb44cffe34c24c135b002)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
23  * Copyright (c) 2016, Chris Fraire <cfraire@me.com>.
24  * Copyright 2021, Tintri by DDN. All rights reserved.
25  */
26 #ifndef _LIBIPADM_H
27 #define	_LIBIPADM_H
28 
29 #ifdef	__cplusplus
30 extern "C" {
31 #endif
32 
33 #include <sys/types.h>
34 #include <sys/param.h>
35 #include <sys/socket.h>
36 #include <net/if.h>
37 #include <netdb.h>
38 #include <ifaddrs.h>
39 #include <libnvpair.h>
40 #include <netinet/tcp.h>
41 #include <sys/stropts.h>
42 
43 #define	IPADM_AOBJ_USTRSIZ	32
44 #define	IPADM_AOBJSIZ		(LIFNAMSIZ + IPADM_AOBJ_USTRSIZ)
45 #define	MAXPROPVALLEN		512
46 #define	LOOPBACK_IF		"lo0"
47 
48 /* special timeout values for dhcp operations */
49 #define	IPADM_DHCP_WAIT_DEFAULT	(-1)
50 #define	IPADM_DHCP_WAIT_FOREVER	(-2)
51 
52 /*
53  * Specifies that the string passed to ipadm_str2nvlist() is a string of comma
54  * separated names and that each name does not have values associated with it.
55  */
56 #define	IPADM_NORVAL		0x00000001
57 
58 /* error codes */
59 typedef enum {
60 	IPADM_SUCCESS,		/* No error occurred */
61 	IPADM_FAILURE,		/* Generic failure */
62 	IPADM_EAUTH,		/* Insufficient user authorizations */
63 	IPADM_EPERM,		/* Permission denied */
64 	IPADM_NO_BUFS,		/* No Buffer space available */
65 	IPADM_NO_MEMORY,	/* Insufficient memory */
66 	IPADM_BAD_ADDR,		/* Invalid address */
67 	IPADM_BAD_PROTOCOL,	/* Wrong protocol family for operation */
68 	IPADM_DAD_FOUND,	/* Duplicate address detected */
69 	IPADM_EXISTS,		/* Already exists */
70 	IPADM_IF_EXISTS,	/* Interface already exists */
71 	IPADM_ADDROBJ_EXISTS,	/* Address object already exists */
72 	IPADM_ADDRCONF_EXISTS,	/* Addrconf already in progress */
73 	IPADM_ENXIO,		/* Interface does not exist */
74 	IPADM_GRP_NOTEMPTY,	/* IPMP Group non-empty on unplumb */
75 	IPADM_INVALID_ARG,	/* Invalid argument */
76 	IPADM_INVALID_NAME,	/* Invalid name */
77 	IPADM_DLPI_FAILURE,	/* Could not open DLPI link */
78 	IPADM_DLADM_FAILURE,	/* DLADM error encountered */
79 	IPADM_PROP_UNKNOWN,	/* Unknown property */
80 	IPADM_ERANGE,		/* Value is outside the allowed range */
81 	IPADM_ESRCH,		/* Value does not exist */
82 	IPADM_EOVERFLOW,	/* Number of values exceed the allowed limit */
83 	IPADM_NOTFOUND,		/* Object not found */
84 	IPADM_IF_INUSE,		/* Interface already in use */
85 	IPADM_ADDR_INUSE,	/* Address alrelady in use */
86 	IPADM_BAD_HOSTNAME,	/* hostname maps to multiple IP addresses */
87 	IPADM_ADDR_NOTAVAIL,	/* Can't assign requested address */
88 	IPADM_ALL_ADDRS_NOT_ENABLED, /* All addresses could not be enabled */
89 	IPADM_NDPD_NOT_RUNNING,	/* in.ndpd not running */
90 	IPADM_DHCP_START_ERROR,	/* Cannot start dhcpagent */
91 	IPADM_DHCP_IPC_ERROR,	/* Cannot communicate with dhcpagent */
92 	IPADM_DHCP_IPC_TIMEOUT,	/* Communication with dhcpagent timed out */
93 	IPADM_TEMPORARY_OBJ,	/* Permanent operation on temporary object */
94 	IPADM_IPC_ERROR,	/* Cannot communicate with ipmgmtd */
95 	IPADM_OP_DISABLE_OBJ,	/* Operation on disable object */
96 	IPADM_NOTSUP,		/* Operation not supported */
97 	IPADM_EBADE,		/* Invalid data exchange with ipmgmtd */
98 	IPADM_GZ_PERM		/* Operation not permitted on from-gz intf */
99 } ipadm_status_t;
100 
101 /*
102  * option flags taken by the libipadm functions
103  *
104  *  - IPADM_OPT_PERSIST:
105  *	For all the create/delete/up/down/set/get functions,
106  *	requests to persist the configuration so that it can be
107  *	re-enabled or re-applied on boot.
108  *
109  *  - IPADM_OPT_ACTIVE:
110  *	Requests to apply configuration without persisting it and
111  *	used by show-* subcommands to retrieve current values.
112  *
113  *  - IPADM_OPT_DEFAULT:
114  *	retrieves the default value for a given property
115  *
116  *  - IPADM_OPT_PERM
117  *	retrieves the permission for a given property
118  *
119  *  - IPADM_OPT_POSSIBLE
120  *	retrieves the range of values for a given property
121  *
122  *  - IPADM_OPT_APPEND
123  *	for multi-valued properties, appends a new value.
124  *
125  *  - IPADM_OPT_REMOVE
126  *	for multi-valued properties, removes the specified value
127  *
128  *  - IPADM_OPT_IPMP
129  *	Used in ipadm_create_if() to plumb ipmp interfaces.
130  *
131  *  - IPADM_OPT_GENPPA
132  *	Used in ipadm_create_if() to generate a ppa for the given interface.
133  *
134  *  - IPADM_OPT_ZEROADDR
135  *	return :: or INADDR_ANY
136  *
137  *  - IPADM_OPT_RELEASE
138  *	Used to release the lease on a dhcp address object
139  *
140  *  - IPADM_OPT_INFORM
141  *	Used to perform DHCP_INFORM on a specified static address object
142  *
143  *  - IPADM_OPT_UP
144  *	Used to bring up a static address on creation
145  *
146  *  - IPADM_OPT_V46
147  *	Used to plumb both IPv4 and IPv6 interfaces by ipadm_create_addr()
148  *
149  *  - IPADM_OPT_SET_PROPS
150  *	Used to indicate the update changes the running configuration of
151  *	"props" data on the object. The props are cached there on the parent,
152  *	but the PROPS_ONLY change does not affect the ACTIVE/PERSIST state of
153  *	the parent.
154  *
155  *  - IPADM_OPT_PERSIST_PROPS
156  *	Used when IPADM_OPT_SET_PROPS is active to indicate the update changes
157  *  the persistent configuration of the "props" data on the object.
158  */
159 #define	IPADM_OPT_PERSIST	0x00000001
160 #define	IPADM_OPT_ACTIVE	0x00000002
161 #define	IPADM_OPT_DEFAULT	0x00000004
162 #define	IPADM_OPT_PERM		0x00000008
163 #define	IPADM_OPT_POSSIBLE	0x00000010
164 #define	IPADM_OPT_APPEND	0x00000020
165 #define	IPADM_OPT_REMOVE	0x00000040
166 #define	IPADM_OPT_IPMP		0x00000080
167 #define	IPADM_OPT_GENPPA	0x00000100
168 #define	IPADM_OPT_ZEROADDR	0x00000200
169 #define	IPADM_OPT_RELEASE	0x00000400
170 #define	IPADM_OPT_INFORM	0x00000800
171 #define	IPADM_OPT_UP		0x00001000
172 #define	IPADM_OPT_V46		0x00002000
173 #define	IPADM_OPT_SET_PROPS	0x00004000
174 #define	IPADM_OPT_PERSIST_PROPS		0x00008000
175 
176 /* IPADM property class */
177 #define	IPADMPROP_CLASS_MODULE	0x00000001	/* on 'protocol' only */
178 #define	IPADMPROP_CLASS_IF	0x00000002	/* on 'IP interface' only */
179 #define	IPADMPROP_CLASS_ADDR	0x00000004	/* on 'IP address' only */
180 /* protocol property that can be applied on interface too */
181 #define	IPADMPROP_CLASS_MODIF	(IPADMPROP_CLASS_MODULE | IPADMPROP_CLASS_IF)
182 
183 /* opaque ipadm handle to libipadm functions */
184 struct ipadm_handle;
185 typedef struct ipadm_handle	*ipadm_handle_t;
186 
187 /* ipadm_handle flags */
188 #define	IPH_VRRP		0x00000001	/* Caller is VRRP */
189 #define	IPH_LEGACY		0x00000002	/* Caller is legacy app */
190 #define	IPH_IPMGMTD		0x00000004	/* Caller is ipmgmtd itself */
191 /*
192  * Indicates that the operation being invoked is in 'init' context. This is
193  * a library private flag.
194  */
195 #define	IPH_INIT		0x10000000
196 
197 /* opaque address object structure */
198 typedef struct ipadm_addrobj_s	*ipadm_addrobj_t;
199 
200 /* ipadm_if_info_t states */
201 typedef enum {
202 	IFIS_OK,		/* Interface is usable */
203 	IFIS_DOWN,		/* Interface has no UP addresses */
204 	IFIS_FAILED,		/* Interface has failed. */
205 	IFIS_OFFLINE,		/* Interface has been offlined */
206 	IFIS_DISABLED		/* Interface has been disabled. */
207 } ipadm_if_state_t;
208 
209 /*
210  * Declare ipadm_if_info_list_t as a container for ipadm_if_info_t.
211  *
212  * ipadm_if_info_t used to have a list pointer ifi_next for linking a number
213  * of ipadm_if_info_t's together. Even though this linking wasn't used in the
214  * data exchange between ipmgmtd and libipadm, this meant the structure wasn't
215  * safe for passing through the door between 32bit and 64bit processes.
216  */
217 typedef struct ipadm_if_info_s {
218 	char			ifi_name[LIFNAMSIZ];	/* interface name */
219 	ipadm_if_state_t	ifi_state;		/* see above */
220 	uint_t			ifi_cflags;		/* current flags */
221 	uint_t			ifi_pflags;		/* persistent flags */
222 } ipadm_if_info_t;
223 
224 typedef struct ipadm_if_info_list_s {
225 	struct ipadm_if_info_list_s	*ifil_next;
226 	ipadm_if_info_t			ifil_ifi;
227 } ipadm_if_info_list_t;
228 
229 
230 /* ipadm_if_info_t flags */
231 #define	IFIF_BROADCAST		0x00000001
232 #define	IFIF_MULTICAST		0x00000002
233 #define	IFIF_POINTOPOINT	0x00000004
234 #define	IFIF_VIRTUAL		0x00000008
235 #define	IFIF_IPMP		0x00000010
236 #define	IFIF_STANDBY		0x00000020
237 #define	IFIF_INACTIVE		0x00000040
238 #define	IFIF_VRRP		0x00000080
239 #define	IFIF_NOACCEPT		0x00000100
240 #define	IFIF_IPV4		0x00000200
241 #define	IFIF_IPV6		0x00000400
242 #define	IFIF_L3PROTECT		0x00000800
243 
244 /* ipadm_addr_info_t state */
245 typedef enum {
246 	IFA_DISABLED,		/* Address not in active configuration. */
247 	IFA_DUPLICATE,		/* DAD failed. */
248 	IFA_DOWN,		/* Address is not IFF_UP */
249 	IFA_TENTATIVE,		/* DAD verification initiated */
250 	IFA_OK,			/* Address is usable */
251 	IFA_INACCESSIBLE	/* Interface has failed */
252 } ipadm_addr_state_t;
253 
254 /* possible address types */
255 typedef enum  {
256 	IPADM_ADDR_NONE,
257 	IPADM_ADDR_STATIC,
258 	IPADM_ADDR_IPV6_ADDRCONF,
259 	IPADM_ADDR_DHCP
260 } ipadm_addr_type_t;
261 
262 typedef struct ipadm_addr_info_s {
263 	struct ifaddrs		ia_ifa;		/* list of addresses */
264 	char			ia_sname[NI_MAXHOST];	/* local hostname */
265 	char			ia_dname[NI_MAXHOST];	/* remote hostname */
266 	char			ia_aobjname[IPADM_AOBJSIZ];
267 	uint_t			ia_cflags;	/* active flags */
268 	uint_t			ia_pflags;	/* persistent flags */
269 	ipadm_addr_type_t	ia_atype;	/* see above */
270 	ipadm_addr_state_t	ia_state;	/* see above */
271 } ipadm_addr_info_t;
272 #define	IA_NEXT(ia)		((ipadm_addr_info_t *)(ia->ia_ifa.ifa_next))
273 
274 /* ipadm_addr_info_t flags */
275 #define	IA_UP			0x00000001
276 #define	IA_UNNUMBERED		0x00000002
277 #define	IA_PRIVATE		0x00000004
278 #define	IA_TEMPORARY		0x00000008
279 #define	IA_DEPRECATED		0x00000010
280 
281 /* open/close libipadm handle */
282 extern ipadm_status_t	ipadm_open(ipadm_handle_t *, uint32_t);
283 extern void		ipadm_close(ipadm_handle_t);
284 
285 /* Check authorization for network configuration */
286 extern boolean_t	ipadm_check_auth(void);
287 /*
288  * Interface management functions
289  */
290 extern ipadm_status_t	ipadm_create_if(ipadm_handle_t, char *, sa_family_t,
291 			    uint32_t);
292 extern ipadm_status_t	ipadm_disable_if(ipadm_handle_t, const char *,
293 			    uint32_t);
294 extern ipadm_status_t	ipadm_enable_if(ipadm_handle_t, const char *, uint32_t);
295 extern ipadm_status_t	ipadm_if_info(ipadm_handle_t, const char *,
296 			    ipadm_if_info_list_t **, uint32_t, int64_t);
297 extern void		ipadm_free_if_info(ipadm_if_info_list_t *);
298 extern ipadm_status_t	ipadm_delete_if(ipadm_handle_t, const char *,
299 			    sa_family_t, uint32_t);
300 extern void		ipadm_if_move(ipadm_handle_t, const char *);
301 
302 /*
303  * Address management functions
304  */
305 extern ipadm_status_t	ipadm_create_addr(ipadm_handle_t, ipadm_addrobj_t,
306 			    uint32_t);
307 extern ipadm_status_t	ipadm_disable_addr(ipadm_handle_t, const char *,
308 			    uint32_t);
309 extern ipadm_status_t	ipadm_enable_addr(ipadm_handle_t, const char *,
310 			    uint32_t);
311 extern ipadm_status_t	ipadm_addr_info(ipadm_handle_t, const char *,
312 			    ipadm_addr_info_t **, uint32_t, int64_t);
313 extern void		ipadm_free_addr_info(ipadm_addr_info_t *);
314 extern ipadm_status_t	ipadm_up_addr(ipadm_handle_t, const char *,
315 			    uint32_t);
316 extern ipadm_status_t	ipadm_down_addr(ipadm_handle_t, const char *,
317 			    uint32_t);
318 extern ipadm_status_t	ipadm_refresh_addr(ipadm_handle_t, const char *,
319 			    uint32_t);
320 extern ipadm_status_t	ipadm_delete_addr(ipadm_handle_t, const char *,
321 			    uint32_t);
322 
323 /* Functions related to creating/deleting/modifying opaque address object */
324 extern ipadm_status_t	ipadm_create_addrobj(ipadm_addr_type_t, const char *,
325 			    ipadm_addrobj_t *);
326 extern void		ipadm_destroy_addrobj(ipadm_addrobj_t);
327 extern ipadm_status_t   ipadm_get_aobjname(const ipadm_addrobj_t, char *,
328 			    size_t);
329 
330 /* Functions to set fields in addrobj for static addresses */
331 extern ipadm_status_t	ipadm_set_addr(ipadm_addrobj_t, const char *,
332 			    sa_family_t);
333 extern ipadm_status_t	ipadm_set_dst_addr(ipadm_addrobj_t, const char *,
334 			    sa_family_t);
335 extern ipadm_status_t   ipadm_get_addr(const ipadm_addrobj_t,
336 			    struct sockaddr_storage *);
337 
338 /* Functions to set fields in addrobj for IPv6 addrconf */
339 extern ipadm_status_t	ipadm_set_interface_id(ipadm_addrobj_t, const char *);
340 extern ipadm_status_t	ipadm_set_stateless(ipadm_addrobj_t, boolean_t);
341 extern ipadm_status_t	ipadm_set_stateful(ipadm_addrobj_t, boolean_t);
342 
343 /* Functions to set fields in addrobj for DHCP */
344 extern ipadm_status_t	ipadm_set_primary(ipadm_addrobj_t, boolean_t);
345 extern ipadm_status_t	ipadm_set_wait_time(ipadm_addrobj_t, int32_t);
346 extern ipadm_status_t	ipadm_set_reqhost(ipadm_addrobj_t, const char *);
347 
348 /*
349  * Property management functions
350  */
351 /* call back function for the property walker */
352 typedef boolean_t	ipadm_prop_wfunc_t(void *, const char *, uint_t);
353 extern ipadm_status_t	ipadm_walk_proptbl(uint_t, uint_t, ipadm_prop_wfunc_t *,
354 			    void *);
355 extern ipadm_status_t	ipadm_walk_prop(const char *, uint_t, uint_t,
356 			    ipadm_prop_wfunc_t *, void *);
357 
358 /* Interface property management - set, reset and get */
359 extern ipadm_status_t	ipadm_set_ifprop(ipadm_handle_t, const char *,
360 			    const char *, const char *, uint_t, uint_t);
361 extern ipadm_status_t	ipadm_get_ifprop(ipadm_handle_t, const char *,
362 			    const char *, char *, uint_t *, uint_t, uint_t);
363 
364 /* Address property management - set, reset and get */
365 extern ipadm_status_t	ipadm_set_addrprop(ipadm_handle_t, const char *,
366 			    const char *, const char *, uint_t);
367 extern ipadm_status_t	ipadm_get_addrprop(ipadm_handle_t, const char *, char *,
368 			    uint_t *, const char *, uint_t);
369 
370 /* Protoocl property management - set, reset and get */
371 extern ipadm_status_t	ipadm_set_prop(ipadm_handle_t, const char *,
372 			    const char *, uint_t, uint_t);
373 extern ipadm_status_t	ipadm_get_prop(ipadm_handle_t, const char *, char *,
374 			    uint_t *, uint_t, uint_t);
375 
376 /*
377  * miscellaneous helper functions.
378  */
379 extern const char	*ipadm_status2str(ipadm_status_t);
380 extern int		ipadm_str2nvlist(const char *, nvlist_t **, uint_t);
381 extern size_t		ipadm_nvlist2str(nvlist_t *, char *, size_t);
382 extern char		*ipadm_proto2str(uint_t);
383 extern uint_t		ipadm_str2proto(const char *);
384 extern ipadm_status_t	ipadm_open_arp_on_udp(const char *, int *);
385 extern int		ipadm_legacy2new_propname(const char *, char *,
386 			    uint_t, uint_t *);
387 extern int		ipadm_new2legacy_propname(const char *, char *,
388 			    uint_t, uint_t);
389 extern boolean_t	ipadm_is_valid_hostname(const char *hostname);
390 extern boolean_t	ipadm_is_nil_hostname(const char *hostname);
391 
392 #ifdef	__cplusplus
393 }
394 #endif
395 
396 #endif	/* _LIBIPADM_H */
397