xref: /illumos-gate/usr/src/lib/libipadm/common/libipadm_impl.h (revision c8152f8f417d34bc129af68ca0b76cfa9ca764db)
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 /*
23  * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
24  * Copyright (c) 2013 by Delphix. All rights reserved.
25  * Copyright (c) 2016, Chris Fraire <cfraire@me.com>.
26  * Copyright 2021 Tintri by DDN, Inc. All rights reserved.
27  * Copyright 2023 Oxide Computer Company
28  */
29 
30 #ifndef _LIBIPADM_IMPL_H
31 #define	_LIBIPADM_IMPL_H
32 
33 #ifdef	__cplusplus
34 extern "C" {
35 #endif
36 
37 #include <sys/socket.h>
38 #include <sys/list.h>
39 #include <net/if.h>
40 #include <libipadm.h>
41 #include <libdladm.h>
42 #include <ipadm_ipmgmt.h>
43 #include <inet/tunables.h>
44 #include <netinet/in.h>
45 #include <pthread.h>
46 #include <libinetutil.h>
47 #include <libsocket_priv.h>
48 
49 #define	IPADM_STRSIZE		256
50 #define	IPADM_ONSTR		"on"
51 #define	IPADM_OFFSTR		"off"
52 #define	ARP_MOD_NAME		"arp"
53 #define	IPADM_LOGICAL_SEP	':'
54 #define	IPV6_MIN_MTU		1280	/* rfc2460 */
55 
56 /* mask for flags accepted by libipadm functions */
57 #define	IPADM_COMMON_OPT_MASK	(IPADM_OPT_ACTIVE | IPADM_OPT_PERSIST)
58 
59 typedef enum {
60     IPADM_ADD_IPMP,
61     IPADM_REMOVE_IPMP
62 } ipadm_ipmp_op_t;
63 
64 /* Opaque library handle */
65 struct ipadm_handle {
66 	int		iph_sock;	/* socket to interface */
67 	int		iph_sock6;	/* socket to interface */
68 	int		iph_door_fd;	/* door descriptor to ipmgmtd */
69 	int		iph_rtsock;	/* routing socket */
70 	dladm_handle_t	iph_dlh;	/* handle to libdladm library */
71 	uint32_t	iph_flags;	/* internal flags */
72 	pthread_mutex_t	iph_lock;	/* lock to set door_fd */
73 	zoneid_t	iph_zoneid;	/* zoneid where handle was opened */
74 };
75 
76 struct ipadm_addrobj_s {
77 	char			ipadm_ifname[LIFNAMSIZ];
78 	int32_t			ipadm_lifnum;
79 	char			ipadm_aobjname[IPADM_AOBJSIZ];
80 	ipadm_addr_type_t	ipadm_atype;
81 	uint32_t		ipadm_flags;
82 	sa_family_t		ipadm_af;
83 	union {
84 		struct {
85 			char			ipadm_ahname[MAXNAMELEN];
86 			struct sockaddr_storage	ipadm_addr;
87 			uint32_t		ipadm_prefixlen;
88 			char			ipadm_dhname[MAXNAMELEN];
89 			struct sockaddr_storage ipadm_dstaddr;
90 		} ipadm_static_addr_s;
91 		struct {
92 			struct sockaddr_in6	ipadm_intfid;
93 			uint32_t		ipadm_intfidlen;
94 			boolean_t		ipadm_stateless;
95 			boolean_t		ipadm_stateful;
96 		} ipadm_ipv6_intfid_s;
97 		struct {
98 			boolean_t		ipadm_primary;
99 			int32_t			ipadm_wait;
100 			char			ipadm_reqhost[MAXNAMELEN];
101 		} ipadm_dhcp_s;
102 	} ipadm_addr_u;
103 };
104 
105 #define	ipadm_static_addr	ipadm_addr_u.ipadm_static_addr_s.ipadm_addr
106 #define	ipadm_static_aname	ipadm_addr_u.ipadm_static_addr_s.ipadm_ahname
107 #define	ipadm_static_prefixlen	ipadm_addr_u.ipadm_static_addr_s.ipadm_prefixlen
108 #define	ipadm_static_dst_addr	ipadm_addr_u.ipadm_static_addr_s.ipadm_dstaddr
109 #define	ipadm_static_dname	ipadm_addr_u.ipadm_static_addr_s.ipadm_dhname
110 #define	ipadm_intfid		ipadm_addr_u.ipadm_ipv6_intfid_s.ipadm_intfid
111 #define	ipadm_intfidlen		ipadm_addr_u.ipadm_ipv6_intfid_s.ipadm_intfidlen
112 #define	ipadm_stateless		ipadm_addr_u.ipadm_ipv6_intfid_s.ipadm_stateless
113 #define	ipadm_stateful		ipadm_addr_u.ipadm_ipv6_intfid_s.ipadm_stateful
114 #define	ipadm_primary		ipadm_addr_u.ipadm_dhcp_s.ipadm_primary
115 #define	ipadm_wait		ipadm_addr_u.ipadm_dhcp_s.ipadm_wait
116 #define	ipadm_reqhost	ipadm_addr_u.ipadm_dhcp_s.ipadm_reqhost
117 
118 /*
119  * Data structures and callback functions related to property management
120  */
121 struct ipadm_prop_desc;
122 typedef struct ipadm_prop_desc ipadm_prop_desc_t;
123 
124 /* property set() callback */
125 typedef ipadm_status_t	ipadm_pd_setf_t(ipadm_handle_t, const void *,
126     ipadm_prop_desc_t *, const void *, uint_t, uint_t);
127 
128 /* property get() callback */
129 typedef ipadm_status_t	ipadm_pd_getf_t(ipadm_handle_t, const void *,
130     ipadm_prop_desc_t *, char *, uint_t *, uint_t, uint_t);
131 
132 struct ipadm_prop_desc {
133 	char		*ipd_name;	/* property name */
134 	char		*ipd_old_name;	/* for backward compatibility */
135 	uint_t		ipd_class;	/* prop. class - global/perif/both */
136 	uint_t		ipd_proto;	/* protocol to which property belongs */
137 	uint_t		ipd_flags;	/* see below */
138 	ipadm_pd_setf_t	*ipd_set;	/* set callback function */
139 	ipadm_pd_getf_t	*ipd_get_range;	/* get range callback function */
140 	ipadm_pd_getf_t	*ipd_get;	/* get value callback function */
141 };
142 
143 /* ipd_flags values */
144 #define	IPADMPROP_MULVAL	0x00000001	/* property multi-valued */
145 
146 extern ipadm_prop_desc_t	ipadm_addrprop_table[];
147 extern ipadm_pd_getf_t		i_ipadm_get_onoff;
148 
149 /* libipadm.c */
150 extern ipadm_status_t	i_ipadm_get_flags(ipadm_handle_t, const char *,
151 			    sa_family_t, uint64_t *);
152 extern ipadm_status_t	i_ipadm_set_flags(ipadm_handle_t, const char *,
153 			    sa_family_t, uint64_t, uint64_t);
154 extern ipadm_status_t	i_ipadm_init_ifs(ipadm_handle_t, const char *,
155 			    nvlist_t **);
156 extern ipadm_status_t	i_ipadm_init_ifobj(ipadm_handle_t, const char *,
157 			    nvlist_t *);
158 extern ipadm_status_t	i_ipadm_init_addrobj(ipadm_handle_t, nvlist_t *);
159 extern ipadm_status_t	i_ipadm_addr_persist(ipadm_handle_t,
160 			    const ipadm_addrobj_t, boolean_t, uint32_t,
161 			    const char *);
162 extern ipadm_status_t	i_ipadm_delete_addr(ipadm_handle_t, ipadm_addrobj_t);
163 extern int		i_ipadm_strioctl(int, int, char *, int);
164 extern boolean_t	i_ipadm_is_loopback(const char *);
165 extern boolean_t	i_ipadm_is_vni(const char *);
166 extern boolean_t	i_ipadm_is_6to4(ipadm_handle_t, char *);
167 extern boolean_t	i_ipadm_validate_ifname(ipadm_handle_t, const char *);
168 extern ipadm_status_t	ipadm_errno2status(int);
169 extern int		ipadm_door_call(ipadm_handle_t, void *, size_t, void **,
170 			    size_t, boolean_t);
171 extern boolean_t	ipadm_if_enabled(ipadm_handle_t, const char *,
172 			    sa_family_t);
173 extern ipadm_status_t i_ipadm_call_ipmgmtd(ipadm_handle_t, void *,
174 	    size_t, nvlist_t **);
175 
176 /* ipadm_ndpd.c */
177 extern	ipadm_status_t	i_ipadm_create_ipv6addrs(ipadm_handle_t,
178 			    ipadm_addrobj_t, uint32_t);
179 extern ipadm_status_t	i_ipadm_delete_ipv6addrs(ipadm_handle_t,
180 			    ipadm_addrobj_t);
181 extern ipadm_status_t	i_ipadm_disable_autoconf(const char *);
182 extern ipadm_status_t	i_ipadm_enable_autoconf(const char *);
183 
184 /* ipadm_persist.c */
185 extern ipadm_status_t	i_ipadm_add_ipaddr2nvl(nvlist_t *, ipadm_addrobj_t);
186 extern ipadm_status_t	i_ipadm_add_ip6addr2nvl(nvlist_t *, ipadm_addrobj_t);
187 extern ipadm_status_t	i_ipadm_add_intfid2nvl(nvlist_t *, ipadm_addrobj_t);
188 extern ipadm_status_t	i_ipadm_add_dhcp2nvl(nvlist_t *, boolean_t, int32_t);
189 
190 /* ipadm_prop.c */
191 extern ipadm_status_t	i_ipadm_persist_propval(ipadm_handle_t,
192 			    ipadm_prop_desc_t *, const char *, const void *,
193 			    uint_t);
194 extern ipadm_status_t	i_ipadm_get_persist_propval(ipadm_handle_t,
195 			    ipadm_prop_desc_t *, char *, uint_t *,
196 			    const void *);
197 
198 /* ipadm_addr.c */
199 extern ipadm_status_t	i_ipadm_active_addr_info(ipadm_handle_t, const char *,
200 			    ipadm_addr_info_t **, uint32_t, int64_t);
201 extern void		i_ipadm_init_addr(ipadm_addrobj_t, const char *,
202 			    const char *, ipadm_addr_type_t);
203 extern ipadm_status_t	i_ipadm_merge_addrprops_from_nvl(nvlist_t *, nvlist_t *,
204 			    const char *);
205 extern ipadm_status_t	i_ipadm_get_addrobj(ipadm_handle_t, ipadm_addrobj_t);
206 extern ipadm_status_t	i_ipadm_enable_static(ipadm_handle_t, const char *,
207 			    nvlist_t *, sa_family_t);
208 extern ipadm_status_t	i_ipadm_enable_dhcp(ipadm_handle_t, const char *,
209 			    nvlist_t *);
210 extern ipadm_status_t	i_ipadm_enable_addrconf(ipadm_handle_t, const char *,
211 			    nvlist_t *);
212 extern void		i_ipadm_addrobj2lifname(ipadm_addrobj_t, char *, int);
213 extern ipadm_status_t	i_ipadm_nvl2in6_addr(nvlist_t *, char *,
214 			    in6_addr_t *);
215 extern ipadm_status_t	i_ipadm_get_lif2addrobj(ipadm_handle_t,
216 			    ipadm_addrobj_t);
217 extern ipadm_status_t	i_ipadm_lookupadd_addrobj(ipadm_handle_t,
218 			    ipadm_addrobj_t);
219 extern ipadm_status_t	i_ipadm_setlifnum_addrobj(ipadm_handle_t,
220 			    ipadm_addrobj_t);
221 extern ipadm_status_t	i_ipadm_do_addif(ipadm_handle_t, ipadm_addrobj_t,
222 			    boolean_t *);
223 extern ipadm_status_t	i_ipadm_delete_addrobj(ipadm_handle_t,
224 			    const ipadm_addrobj_t, uint32_t);
225 extern boolean_t	i_ipadm_name2atype(const char *, sa_family_t *,
226 			    ipadm_addr_type_t *);
227 extern ipadm_status_t	i_ipadm_resolve_addr(const char *, sa_family_t,
228 			    struct sockaddr_storage *);
229 
230 /* ipadm_if.c */
231 extern ipadm_status_t	i_ipadm_create_if(ipadm_handle_t, char *,  sa_family_t,
232 			    uint32_t);
233 extern ipadm_status_t	i_ipadm_delete_if(ipadm_handle_t, const char *,
234 			    sa_family_t, uint32_t);
235 extern ipadm_status_t	i_ipadm_plumb_if(ipadm_handle_t, char *, sa_family_t,
236 			    uint32_t);
237 extern ipadm_status_t	i_ipadm_unplumb_if(ipadm_handle_t, const char *,
238 			    sa_family_t);
239 extern ipadm_status_t	i_ipadm_if_pexists(ipadm_handle_t, const char *,
240 			    sa_family_t, boolean_t *);
241 extern ipadm_status_t	i_ipadm_delete_ifobj(ipadm_handle_t, const char *,
242 			    sa_family_t, boolean_t);
243 extern int		i_ipadm_get_lnum(const char *);
244 
245 extern ipadm_status_t i_ipadm_set_groupname_active(ipadm_handle_t,
246 	    const char *, const char *);
247 extern ipadm_status_t i_ipadm_get_groupname_active(ipadm_handle_t,
248 	    const char *, char *, size_t);
249 extern boolean_t i_ipadm_is_under_ipmp(ipadm_handle_t, const char *);
250 extern boolean_t i_ipadm_is_ipmp(ipadm_handle_t, const char *);
251 #ifdef	__cplusplus
252 }
253 #endif
254 
255 #endif	/* _LIBIPADM_IMPL_H */
256