xref: /titanic_41/usr/src/lib/libipadm/common/libipadm.h (revision 1022fd2a9aa2c967697116c2ca51a238a3c550ac)
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  */
24 #ifndef _LIBIPADM_H
25 #define	_LIBIPADM_H
26 
27 #ifdef	__cplusplus
28 extern "C" {
29 #endif
30 
31 #include <sys/types.h>
32 #include <sys/socket.h>
33 #include <net/if.h>
34 #include <netdb.h>
35 #include <ifaddrs.h>
36 #include <libnvpair.h>
37 #include <netinet/tcp.h>
38 #include <sys/stropts.h>
39 
40 #define	IPADM_AOBJ_USTRSIZ	32
41 #define	IPADM_AOBJSIZ		(LIFNAMSIZ + IPADM_AOBJ_USTRSIZ)
42 #define	MAXPROPVALLEN		512
43 #define	LOOPBACK_IF		"lo0"
44 
45 /* special timeout values for dhcp operations */
46 #define	IPADM_DHCP_WAIT_DEFAULT	(-1)
47 #define	IPADM_DHCP_WAIT_FOREVER	(-2)
48 
49 /*
50  * Specifies that the string passed to ipadm_str2nvlist() is a string of comma
51  * separated names and that each name does not have values associated with it.
52  */
53 #define	IPADM_NORVAL		0x00000001
54 
55 /* error codes */
56 typedef enum {
57 	IPADM_SUCCESS,		/* No error occurred */
58 	IPADM_FAILURE,		/* Generic failure */
59 	IPADM_EAUTH,		/* Insufficient user authorizations */
60 	IPADM_EPERM,		/* Permission denied */
61 	IPADM_NO_BUFS,		/* No Buffer space available */
62 	IPADM_NO_MEMORY,	/* Insufficient memory */
63 	IPADM_BAD_ADDR,		/* Invalid address */
64 	IPADM_BAD_PROTOCOL,	/* Wrong protocol family for operation */
65 	IPADM_DAD_FOUND,	/* Duplicate address detected */
66 	IPADM_EXISTS,		/* Already exists */
67 	IPADM_IF_EXISTS,	/* Interface already exists */
68 	IPADM_ADDROBJ_EXISTS,	/* Address object already exists */
69 	IPADM_ADDRCONF_EXISTS,	/* Addrconf already in progress */
70 	IPADM_ENXIO,		/* Interface does not exist */
71 	IPADM_GRP_NOTEMPTY,	/* IPMP Group non-empty on unplumb */
72 	IPADM_INVALID_ARG,	/* Invalid argument */
73 	IPADM_INVALID_NAME,	/* Invalid name */
74 	IPADM_DLPI_FAILURE,	/* Could not open DLPI link */
75 	IPADM_DLADM_FAILURE,	/* DLADM error encountered */
76 	IPADM_PROP_UNKNOWN,	/* Unknown property */
77 	IPADM_ERANGE,		/* Value is outside the allowed range */
78 	IPADM_ESRCH,		/* Value does not exist */
79 	IPADM_EOVERFLOW,	/* Number of values exceed the allowed limit */
80 	IPADM_NOTFOUND,		/* Object not found */
81 	IPADM_IF_INUSE,		/* Interface already in use */
82 	IPADM_ADDR_INUSE,	/* Address alrelady in use */
83 	IPADM_BAD_HOSTNAME,	/* hostname maps to multiple IP addresses */
84 	IPADM_ADDR_NOTAVAIL,	/* Can't assign requested address */
85 	IPADM_ALL_ADDRS_NOT_ENABLED, /* All addresses could not be enabled */
86 	IPADM_NDPD_NOT_RUNNING,	/* in.ndpd not running */
87 	IPADM_DHCP_START_ERROR,	/* Cannot start dhcpagent */
88 	IPADM_DHCP_IPC_ERROR,	/* Cannot communicate with dhcpagent */
89 	IPADM_DHCP_IPC_TIMEOUT,	/* Communication with dhcpagent timed out */
90 	IPADM_TEMPORARY_OBJ,	/* Permanent operation on temporary object */
91 	IPADM_IPC_ERROR,	/* Cannot communicate with ipmgmtd */
92 	IPADM_OP_DISABLE_OBJ,	/* Operation on disable object */
93 	IPADM_NOTSUP,		/* Operation not supported */
94 	IPADM_EBADE		/* Invalid data exchange with ipmgmtd */
95 } ipadm_status_t;
96 
97 /*
98  * option flags taken by the libipadm functions
99  *
100  *  - IPADM_OPT_PERSIST:
101  *	For all the create/delete/up/down/set/get functions,
102  * 	requests to persist the configuration so that it can be
103  *	re-enabled or reapplied on boot.
104  *
105  *  - IPADM_OPT_ACTIVE:
106  *	Requests to apply configuration without persisting it and
107  *	used by show-* subcommands to retrieve current values.
108  *
109  *  - IPADM_OPT_DEFAULT:
110  *	retrieves the default value for a given property
111  *
112  *  - IPADM_OPT_PERM
113  *	retrieves the permission for a given property
114  *
115  *  - IPADM_OPT_POSSIBLE
116  *	retrieves the range of values for a given property
117  *
118  *  - IPADM_OPT_APPEND
119  *	for multi-valued properties, appends a new value.
120  *
121  *  - IPADM_OPT_REMOVE
122  *	for multi-valued properties, removes the specified value
123  *
124  *  - IPADM_OPT_IPMP
125  *	Used in ipadm_create_if() to plumb ipmp interfaces.
126  *
127  *  - IPADM_OPT_GENPPA
128  *	Used in ipadm_create_if() to generate a ppa for the given interface.
129  *
130  *  - IPADM_OPT_ZEROADDR
131  *	return :: or INADDR_ANY
132  *
133  *  - IPADM_OPT_RELEASE
134  *	Used to release the lease on a dhcp address object
135  *
136  *  - IPADM_OPT_INFORM
137  *	Used to perform DHCP_INFORM on a specified static address object
138  *
139  *  - IPADM_OPT_UP
140  *	Used to bring up a static address on creation
141  *
142  *  - IPADM_OPT_V46
143  *      Used to plumb both IPv4 and IPv6 interfaces by ipadm_create_addr()
144  */
145 #define	IPADM_OPT_PERSIST	0x00000001
146 #define	IPADM_OPT_ACTIVE	0x00000002
147 #define	IPADM_OPT_DEFAULT	0x00000004
148 #define	IPADM_OPT_PERM		0x00000008
149 #define	IPADM_OPT_POSSIBLE	0x00000010
150 #define	IPADM_OPT_APPEND	0x00000020
151 #define	IPADM_OPT_REMOVE	0x00000040
152 #define	IPADM_OPT_IPMP		0x00000080
153 #define	IPADM_OPT_GENPPA	0x00000100
154 #define	IPADM_OPT_ZEROADDR	0x00000200
155 #define	IPADM_OPT_RELEASE	0x00000400
156 #define	IPADM_OPT_INFORM	0x00000800
157 #define	IPADM_OPT_UP		0x00001000
158 #define	IPADM_OPT_V46		0x00002000
159 
160 /* IPADM property class */
161 #define	IPADMPROP_CLASS_MODULE	0x00000001	/* on 'protocol' only */
162 #define	IPADMPROP_CLASS_IF	0x00000002	/* on 'IP interface' only */
163 #define	IPADMPROP_CLASS_ADDR	0x00000004	/* on 'IP address' only */
164 /* protocol property that can be applied on interface too */
165 #define	IPADMPROP_CLASS_MODIF	(IPADMPROP_CLASS_MODULE | IPADMPROP_CLASS_IF)
166 
167 /* opaque ipadm handle to libipadm functions */
168 struct ipadm_handle;
169 typedef struct ipadm_handle	*ipadm_handle_t;
170 
171 /* ipadm_handle flags */
172 #define	IPH_VRRP		0x00000001	/* Caller is VRRP */
173 #define	IPH_LEGACY		0x00000002	/* Caller is legacy app */
174 
175 /* opaque address object structure */
176 typedef struct ipadm_addrobj_s	*ipadm_addrobj_t;
177 
178 /* ipadm_if_info_t states */
179 typedef enum {
180 	IFIS_OK,		/* Interface is usable */
181 	IFIS_DOWN,		/* Interface has no UP addresses */
182 	IFIS_FAILED,		/* Interface has failed. */
183 	IFIS_OFFLINE,		/* Interface has been offlined */
184 	IFIS_DISABLED		/* Interface has been disabled. */
185 } ipadm_if_state_t;
186 
187 typedef struct ipadm_if_info_s {
188 	struct ipadm_if_info_s	*ifi_next;
189 	char			ifi_name[LIFNAMSIZ];	/* interface name */
190 	ipadm_if_state_t	ifi_state;		/* see above */
191 	uint_t			ifi_cflags;		/* current flags */
192 	uint_t			ifi_pflags;		/* persistent flags */
193 } ipadm_if_info_t;
194 
195 /* ipadm_if_info_t flags */
196 #define	IFIF_BROADCAST		0x00000001
197 #define	IFIF_MULTICAST		0x00000002
198 #define	IFIF_POINTOPOINT	0x00000004
199 #define	IFIF_VIRTUAL		0x00000008
200 #define	IFIF_IPMP		0x00000010
201 #define	IFIF_STANDBY		0x00000020
202 #define	IFIF_INACTIVE		0x00000040
203 #define	IFIF_VRRP		0x00000080
204 #define	IFIF_NOACCEPT		0x00000100
205 #define	IFIF_IPV4		0x00000200
206 #define	IFIF_IPV6		0x00000400
207 
208 /* ipadm_addr_info_t state */
209 typedef enum {
210 	IFA_DISABLED,		/* Address not in active configuration. */
211 	IFA_DUPLICATE,		/* DAD failed. */
212 	IFA_DOWN,		/* Address is not IFF_UP */
213 	IFA_TENTATIVE,		/* DAD verification initiated */
214 	IFA_OK,			/* Address is usable */
215 	IFA_INACCESSIBLE	/* Interface has failed */
216 } ipadm_addr_state_t;
217 
218 /* possible address types */
219 typedef enum  {
220 	IPADM_ADDR_NONE,
221 	IPADM_ADDR_STATIC,
222 	IPADM_ADDR_IPV6_ADDRCONF,
223 	IPADM_ADDR_DHCP
224 } ipadm_addr_type_t;
225 
226 typedef struct ipadm_addr_info_s {
227 	struct ifaddrs		ia_ifa;		/* list of addresses */
228 	char			ia_sname[NI_MAXHOST];	/* local hostname */
229 	char			ia_dname[NI_MAXHOST];	/* remote hostname */
230 	char			ia_aobjname[IPADM_AOBJSIZ];
231 	uint_t			ia_cflags;	/* active flags */
232 	uint_t			ia_pflags;	/* persistent flags */
233 	ipadm_addr_type_t	ia_atype;	/* see above */
234 	ipadm_addr_state_t	ia_state;	/* see above */
235 } ipadm_addr_info_t;
236 #define	IA_NEXT(ia)		((ipadm_addr_info_t *)(ia->ia_ifa.ifa_next))
237 
238 /* ipadm_addr_info_t flags */
239 #define	IA_UP			0x00000001
240 #define	IA_UNNUMBERED		0x00000002
241 #define	IA_PRIVATE		0x00000004
242 #define	IA_TEMPORARY		0x00000008
243 #define	IA_DEPRECATED		0x00000010
244 
245 /* open/close libipadm handle */
246 extern ipadm_status_t	ipadm_open(ipadm_handle_t *, uint32_t);
247 extern void		ipadm_close(ipadm_handle_t);
248 
249 /* Check authorization for network configuration */
250 extern boolean_t	ipadm_check_auth(void);
251 /*
252  * Interface mangement functions
253  */
254 extern ipadm_status_t	ipadm_create_if(ipadm_handle_t, char *, sa_family_t,
255 			    uint32_t);
256 extern ipadm_status_t	ipadm_disable_if(ipadm_handle_t, const char *,
257 			    uint32_t);
258 extern ipadm_status_t	ipadm_enable_if(ipadm_handle_t, const char *, uint32_t);
259 extern ipadm_status_t	ipadm_if_info(ipadm_handle_t, const char *,
260 			    ipadm_if_info_t **, uint32_t, int64_t);
261 extern void		ipadm_free_if_info(ipadm_if_info_t *);
262 extern ipadm_status_t	ipadm_delete_if(ipadm_handle_t, const char *,
263 			    sa_family_t, uint32_t);
264 extern void		ipadm_if_move(ipadm_handle_t, const char *);
265 
266 /*
267  * Address management functions
268  */
269 extern ipadm_status_t	ipadm_create_addr(ipadm_handle_t, ipadm_addrobj_t,
270 			    uint32_t);
271 extern ipadm_status_t	ipadm_disable_addr(ipadm_handle_t, const char *,
272 			    uint32_t);
273 extern ipadm_status_t	ipadm_enable_addr(ipadm_handle_t, const char *,
274 			    uint32_t);
275 extern ipadm_status_t	ipadm_addr_info(ipadm_handle_t, const char *,
276 			    ipadm_addr_info_t **, uint32_t, int64_t);
277 extern void		ipadm_free_addr_info(ipadm_addr_info_t *);
278 extern ipadm_status_t	ipadm_up_addr(ipadm_handle_t, const char *,
279 			    uint32_t);
280 extern ipadm_status_t	ipadm_down_addr(ipadm_handle_t, const char *,
281 			    uint32_t);
282 extern ipadm_status_t	ipadm_refresh_addr(ipadm_handle_t, const char *,
283 			    uint32_t);
284 extern ipadm_status_t	ipadm_delete_addr(ipadm_handle_t, const char *,
285 			    uint32_t);
286 
287 /* Functions related to creating/deleting/modifying opaque address object */
288 extern ipadm_status_t	ipadm_create_addrobj(ipadm_addr_type_t, const char *,
289 			    ipadm_addrobj_t *);
290 extern void		ipadm_destroy_addrobj(ipadm_addrobj_t);
291 extern ipadm_status_t   ipadm_get_aobjname(const ipadm_addrobj_t, char *,
292 			    size_t);
293 
294 /* Functions to set fields in addrobj for static addresses */
295 extern ipadm_status_t	ipadm_set_addr(ipadm_addrobj_t, const char *,
296 			    sa_family_t);
297 extern ipadm_status_t	ipadm_set_dst_addr(ipadm_addrobj_t, const char *,
298 			    sa_family_t);
299 extern ipadm_status_t   ipadm_get_addr(const ipadm_addrobj_t,
300 			    struct sockaddr_storage *);
301 
302 /* Functions to set fields in addrobj for IPv6 addrconf */
303 extern ipadm_status_t	ipadm_set_interface_id(ipadm_addrobj_t, const char *);
304 extern ipadm_status_t	ipadm_set_stateless(ipadm_addrobj_t, boolean_t);
305 extern ipadm_status_t	ipadm_set_stateful(ipadm_addrobj_t, boolean_t);
306 
307 /* Functions to set fields in addrobj for DHCP */
308 extern ipadm_status_t	ipadm_set_primary(ipadm_addrobj_t, boolean_t);
309 extern ipadm_status_t	ipadm_set_wait_time(ipadm_addrobj_t, int32_t);
310 
311 /*
312  * Property management functions
313  */
314 /* call back function for the property walker */
315 typedef boolean_t	ipadm_prop_wfunc_t(void *, const char *, uint_t);
316 extern ipadm_status_t	ipadm_walk_proptbl(uint_t, uint_t, ipadm_prop_wfunc_t *,
317 			    void *);
318 extern ipadm_status_t	ipadm_walk_prop(const char *, uint_t, uint_t,
319 			    ipadm_prop_wfunc_t *, void *);
320 
321 /* Interface property management - set, reset and get */
322 extern ipadm_status_t	ipadm_set_ifprop(ipadm_handle_t, const char *,
323 			    const char *, const char *, uint_t, uint_t);
324 extern ipadm_status_t	ipadm_get_ifprop(ipadm_handle_t, const char *,
325 			    const char *, char *, uint_t *, uint_t, uint_t);
326 
327 /* Address property management - set, reset and get */
328 extern ipadm_status_t	ipadm_set_addrprop(ipadm_handle_t, const char *,
329 			    const char *, const char *, uint_t);
330 extern ipadm_status_t	ipadm_get_addrprop(ipadm_handle_t, const char *, char *,
331 			    uint_t *, const char *, uint_t);
332 
333 /* Protoocl property management - set, reset and get */
334 extern ipadm_status_t	ipadm_set_prop(ipadm_handle_t, const char *,
335 			    const char *, uint_t, uint_t);
336 extern ipadm_status_t	ipadm_get_prop(ipadm_handle_t, const char *, char *,
337 			    uint_t *, uint_t, uint_t);
338 extern ipadm_status_t	ipadm_init_prop(void);
339 
340 /*
341  * miscellaneous helper functions.
342  */
343 extern const char 	*ipadm_status2str(ipadm_status_t);
344 extern int		ipadm_str2nvlist(const char *, nvlist_t **, uint_t);
345 extern size_t		ipadm_nvlist2str(nvlist_t *, char *, size_t);
346 extern char		*ipadm_proto2str(uint_t);
347 extern uint_t		ipadm_str2proto(const char *);
348 extern ipadm_status_t	ipadm_open_arp_on_udp(const char *, int *);
349 
350 #ifdef	__cplusplus
351 }
352 #endif
353 
354 #endif	/* _LIBIPADM_H */
355