xref: /titanic_41/usr/src/cmd/cmd-inet/lib/nwamd/ncu.h (revision f6da83d4178694e7113b71d1e452f15b296f73d8)
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  */
25 
26 #ifndef _NCU_H
27 #define	_NCU_H
28 
29 #include <dhcpagent_ipc.h>
30 #include <dhcpagent_util.h>
31 #include <libdladm.h>
32 #include <libdlpi.h>
33 #include <libdlwlan.h>
34 #include <libinetutil.h>
35 #include <libipadm.h>
36 #include <libnwam.h>
37 #include <libnwam_priv.h>
38 #include <libuutil.h>
39 #include <pthread.h>
40 #include <sys/mac.h>
41 
42 #include "events.h"
43 
44 extern pthread_mutex_t active_ncp_mutex;
45 extern pthread_mutex_t active_loc_mutex;
46 extern char active_loc[];
47 extern uint64_t wireless_scan_interval;
48 extern dladm_wlan_strength_t wireless_scan_level;
49 extern boolean_t wireless_autoconf;
50 extern boolean_t wireless_strict_bssid;
51 
52 /*
53  * NCPs are collections of NCUs.  At the moment there is one NCP in the system
54  * and its expected there will never be many.  There is a lock on the NCP which
55  * must be obtained to add or remove anything from the NCP.
56  *
57  * NCUs are also kept in a uu list for easy walking.  Each NCU has a lock which
58  * is used to protect manipulation of its contents.  One of its members is a
59  * reference count which is initialized to 1 when its placed on the NCP.  As
60  * references are passed around that should be manipulated as necessary
61  * (helper functions YYY provided).  It is removed from the NCP by
62  * ncu_destroy() but the memory containing it is not returned to the free pool
63  * until the reference count falls to 0.
64  *
65  * As we add
66  * more complex system objects their relationship becomes more complex.  That
67  * is represented by the links within the NCUs.  Reference counts should be
68  * used to maintain the consistency of these links.  Care should be used when
69  * walking more complex structures that might contain cycles.
70  */
71 
72 /* Stores details of last/current WiFi scans */
73 typedef struct nwamd_wifi_scan {
74 	char nwamd_wifi_scan_link[NWAM_MAX_NAME_LEN];
75 	nwam_wlan_t nwamd_wifi_scan_last[NWAMD_MAX_NUM_WLANS];
76 	uint_t nwamd_wifi_scan_last_num;
77 	nwam_wlan_t nwamd_wifi_scan_curr[NWAMD_MAX_NUM_WLANS];
78 	uint_t nwamd_wifi_scan_curr_num;
79 	boolean_t nwamd_wifi_scan_changed;
80 	uint32_t nwamd_wifi_scan_last_time;
81 } nwamd_wifi_scan_t;
82 
83 typedef struct nwamd_link {
84 	pthread_mutex_t nwamd_link_wifi_mutex;
85 	pthread_t nwamd_link_wifi_scan_thread;
86 	pthread_t nwamd_link_wifi_monitor_thread;
87 	char nwamd_link_wifi_essid[DLADM_STRSIZE];
88 	char nwamd_link_wifi_bssid[DLADM_STRSIZE];
89 	char nwamd_link_wifi_keyname[DLADM_STRSIZE];
90 	char nwamd_link_wifi_signal_strength[DLADM_STRSIZE];
91 	boolean_t nwamd_link_wifi_add_to_known_wlans;
92 	boolean_t nwamd_link_wifi_connected;
93 	uint32_t nwamd_link_wifi_security_mode;
94 	dladm_wlan_key_t *nwamd_link_wifi_key;
95 	nwamd_wifi_scan_t nwamd_link_wifi_scan;
96 	uint64_t nwamd_link_wifi_priority;
97 	boolean_t nwamd_link_wifi_autoconf;
98 	uint32_t nwamd_link_id;
99 	uint32_t nwamd_link_media;
100 	uint64_t nwamd_link_flags;
101 	dlpi_handle_t nwamd_link_dhp;
102 	pthread_t nwamd_link_dlpi_thread;
103 	uint64_t nwamd_link_activation_mode;
104 	uint64_t nwamd_link_priority_mode;
105 	uint64_t nwamd_link_priority_group;
106 	char *nwamd_link_mac_addr;
107 	size_t nwamd_link_mac_addr_len;
108 	uint64_t nwamd_link_mtu;
109 	char **nwamd_link_autopush;
110 	uint_t nwamd_link_num_autopush;
111 } nwamd_link_t;
112 
113 struct nwamd_if_address {
114 	sa_family_t family;
115 	ipadm_addr_type_t ipaddr_atype;
116 	ipadm_addrobj_t ipaddr;
117 	boolean_t configured;
118 	struct sockaddr_storage conf_addr;	/* address configured for */
119 	struct sockaddr_storage conf_stateless_addr; /* this nwamd_if_address */
120 	struct nwamd_if_address *next;
121 };
122 
123 typedef struct nwamd_if {
124 	boolean_t nwamd_if_dhcp_requested;
125 	boolean_t nwamd_if_dhcp_configured;
126 	boolean_t nwamd_if_stateful_requested;
127 	boolean_t nwamd_if_stateful_configured;
128 	boolean_t nwamd_if_stateless_requested;
129 	boolean_t nwamd_if_stateless_configured;
130 	struct nwamd_if_address *nwamd_if_list;
131 	struct sockaddr_in nwamd_if_ipv4_default_route;
132 	boolean_t nwamd_if_ipv4_default_route_set;
133 	struct sockaddr_in6 nwamd_if_ipv6_default_route;
134 	boolean_t nwamd_if_ipv6_default_route_set;
135 	boolean_t nwamd_if_ipv4;
136 	boolean_t nwamd_if_ipv6;
137 } nwamd_if_t;
138 
139 typedef struct nwamd_ncu {
140 	nwam_ncu_type_t ncu_type;
141 	char *ncu_name;
142 	char ncu_parent[NWAM_MAX_NAME_LEN];
143 	boolean_t ncu_enabled; /* whether NCU has been enabled or not */
144 	union {
145 		nwamd_link_t u_link;
146 		nwamd_if_t u_if;
147 	} ncu_node;
148 } nwamd_ncu_t;
149 
150 #define	ncu_link	ncu_node.u_link
151 #define	ncu_if		ncu_node.u_if
152 
153 #define	LOOPBACK_IF				"lo0"
154 
155 struct nwamd_dhcp_thread_arg {
156 	char *name;
157 	dhcp_ipc_type_t type;
158 	ipadm_addrobj_t ipaddr;
159 	volatile uint32_t *guard;
160 };
161 
162 #define	WIRELESS_SCAN_INTERVAL_DEFAULT		120
163 #define	WIRELESS_SCAN_INTERVAL_MIN		30
164 #define	WIRELESS_SCAN_REQUESTED_INTERVAL_MIN	10
165 #define	WIRELESS_MONITOR_SIGNAL_INTERVAL	10
166 #define	WIRELESS_RETRY_INTERVAL			30
167 #define	WIRELESS_SCAN_LEVEL_DEFAULT		DLADM_WLAN_STRENGTH_WEAK
168 #define	NWAMD_DHCP_RETRIES			5
169 #define	NWAMD_DHCP_RETRY_WAIT_TIME		10
170 #define	NWAMD_READONLY_RETRY_INTERVAL		5
171 
172 /*
173  * This dladm and ipadm handles are opened before interfaces are initialized
174  * and closed only when nwamd shuts down.
175  */
176 extern dladm_handle_t dld_handle;
177 extern ipadm_handle_t ipadm_handle;
178 
179 extern nwamd_object_t nwamd_ncu_object_find(nwam_ncu_type_t, const char *);
180 extern void nwamd_log_ncus(void);
181 extern void nwamd_ncu_free(nwamd_ncu_t *);
182 
183 /* WLAN functions */
184 extern void nwamd_set_selected_connected(nwamd_ncu_t *, boolean_t, boolean_t);
185 extern nwam_error_t nwamd_wlan_select(const char *, const char *, const char *,
186     uint32_t, boolean_t);
187 extern nwam_error_t nwamd_wlan_set_key(const char *, const char *, const char *,
188     uint32_t, uint_t, char *);
189 extern nwam_error_t nwamd_wlan_scan(const char *);
190 extern void nwamd_wlan_connect(const char *);
191 extern boolean_t nwamd_wlan_connected(nwamd_object_t);
192 extern void nwamd_wlan_monitor_signal(const char *);
193 extern void nwamd_ncu_create_periodic_scan_event(nwamd_object_t);
194 extern dladm_wlan_key_t *nwamd_wlan_get_key_named(const char *, uint32_t);
195 extern void nwamd_set_key_name(const char *, const char *, char *, size_t);
196 
197 /* Link functions */
198 extern link_state_t nwamd_get_link_state(const char *);
199 extern const char *nwamd_sockaddr_to_str(const struct sockaddr *, char *,
200     size_t);
201 extern void nwamd_propogate_link_up_down_to_ip(const char *, boolean_t);
202 extern void nwamd_set_unset_link_properties(nwamd_ncu_t *, boolean_t);
203 /* DLPI event hooking */
204 extern void nwamd_dlpi_add_link(nwamd_object_t);
205 extern void nwamd_dlpi_delete_link(nwamd_object_t);
206 
207 /* IP functions */
208 extern boolean_t nwamd_static_addresses_configured(nwamd_ncu_t *, sa_family_t);
209 extern void nwamd_plumb_interface(nwamd_ncu_t *, sa_family_t);
210 extern void nwamd_unplumb_interface(nwamd_ncu_t *, sa_family_t);
211 extern boolean_t nwamd_dhcp_managing(int, nwamd_ncu_t *);
212 extern void nwamd_configure_interface_addresses(nwamd_ncu_t *);
213 extern char *nwamd_get_dhcpinfo_data(const char *, char *);
214 extern void nwamd_dhcp_release(const char *);
215 extern void nwamd_add_default_routes(nwamd_ncu_t *);
216 extern void nwamd_add_route(struct sockaddr *, struct sockaddr *,
217     struct sockaddr *, const char *);
218 
219 /* NCU value set/get functions */
220 extern nwam_error_t nwamd_set_ncu_uint(nwam_ncu_handle_t, uint64_t *, uint_t,
221     const char *);
222 extern nwam_error_t nwamd_set_ncu_string(nwam_ncu_handle_t, char **, uint_t,
223     const char *);
224 extern nwam_error_t nwamd_get_ncu_uint(nwam_ncu_handle_t, nwam_value_t *,
225     uint64_t **, uint_t *, const char *);
226 extern nwam_error_t nwamd_get_ncu_string(nwam_ncu_handle_t, nwam_value_t *,
227     char ***, uint_t *, const char *);
228 
229 extern void nwamd_walk_physical_configuration(void);
230 
231 #endif /* _NCU_H */
232