xref: /titanic_41/usr/src/cmd/cmd-inet/lib/nwamd/ncu.h (revision 3e14f97f673e8a630f076077de35afdd43dc1587)
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 2010 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #ifndef _NCU_H
28 #define	_NCU_H
29 
30 #include <dhcpagent_ipc.h>
31 #include <dhcpagent_util.h>
32 #include <inetcfg.h>
33 #include <libdladm.h>
34 #include <libdlpi.h>
35 #include <libdlwlan.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 	size_t prefix;
115 	struct sockaddr address;
116 	char pad[sizeof (struct sockaddr_storage)];
117 	boolean_t configured;
118 	boolean_t dhcp_if;
119 	boolean_t stateless_if;
120 	char ifname[LIFNAMSIZ];
121 	struct nwamd_if_address *next;
122 };
123 
124 typedef struct nwamd_if {
125 	boolean_t nwamd_if_dhcp_requested;
126 	boolean_t nwamd_if_dhcp_configured;
127 	boolean_t nwamd_if_stateful_requested;
128 	boolean_t nwamd_if_stateful_configured;
129 	boolean_t nwamd_if_stateless_requested;
130 	boolean_t nwamd_if_stateless_configured;
131 	struct nwamd_if_address *nwamd_if_list;
132 	struct sockaddr_in nwamd_if_ipv4_default_route;
133 	boolean_t nwamd_if_ipv4_default_route_set;
134 	struct sockaddr_in6 nwamd_if_ipv6_default_route;
135 	boolean_t nwamd_if_ipv6_default_route_set;
136 	boolean_t nwamd_if_ipv4;
137 	boolean_t nwamd_if_ipv6;
138 } nwamd_if_t;
139 
140 typedef struct nwamd_ncu {
141 	nwam_ncu_type_t ncu_type;
142 	char *ncu_name;
143 	char ncu_parent[NWAM_MAX_NAME_LEN];
144 	boolean_t ncu_enabled; /* whether NCU has been enabled or not */
145 	union {
146 		nwamd_link_t u_link;
147 		nwamd_if_t u_if;
148 	} ncu_node;
149 } nwamd_ncu_t;
150 
151 #define	ncu_link	ncu_node.u_link
152 #define	ncu_if		ncu_node.u_if
153 
154 #define	LOOPBACK_IF				"lo0"
155 
156 struct nwamd_dhcp_thread_arg {
157 	char *name;
158 	dhcp_ipc_type_t type;
159 	int timeout;
160 	volatile uint32_t *guard;
161 };
162 
163 #define	WIRELESS_SCAN_INTERVAL_DEFAULT		120
164 #define	WIRELESS_SCAN_INTERVAL_MIN		30
165 #define	WIRELESS_SCAN_REQUESTED_INTERVAL_MIN	10
166 #define	WIRELESS_MONITOR_SIGNAL_INTERVAL	10
167 #define	WIRELESS_RETRY_INTERVAL			30
168 #define	WIRELESS_SCAN_LEVEL_DEFAULT		DLADM_WLAN_STRENGTH_WEAK
169 #define	NWAMD_DHCP_RETRIES			5
170 #define	NWAMD_DHCP_RETRY_WAIT_TIME		10
171 #define	NWAMD_READONLY_RETRY_INTERVAL		5
172 
173 /*
174  * This dladm handle is opened before interfaces are initialized and
175  * closed only when nwamd shuts down.
176  */
177 extern dladm_handle_t dld_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 char *nwamd_sockaddr_to_str(const struct sockaddr *, char *, size_t);
200 extern void nwamd_propogate_link_up_down_to_ip(const char *, boolean_t);
201 extern void nwamd_set_unset_link_properties(nwamd_ncu_t *, boolean_t);
202 /* DLPI event hooking */
203 extern void nwamd_dlpi_add_link(nwamd_object_t);
204 extern void nwamd_dlpi_delete_link(nwamd_object_t);
205 
206 /* IP functions */
207 extern void nwamd_update_addresses_unconfigured(nwamd_ncu_t *, sa_family_t);
208 extern boolean_t nwamd_static_addresses_configured(nwamd_ncu_t *, sa_family_t);
209 extern void nwamd_plumb_interface(nwamd_ncu_t *, uint_t, int);
210 extern void nwamd_unplumb_interface(nwamd_ncu_t *, uint_t, int);
211 extern void nwamd_start_dhcp(nwamd_ncu_t *);
212 extern void nwamd_dhcp_inform(nwamd_ncu_t *);
213 extern boolean_t nwamd_dhcp_managing(int, nwamd_ncu_t *);
214 extern void nwamd_configure_interface_addresses(nwamd_ncu_t *);
215 extern char *nwamd_get_dhcpinfo_data(const char *, char *);
216 extern void nwamd_dhcp_release(const char *);
217 extern void nwamd_add_default_routes(nwamd_ncu_t *);
218 extern void nwamd_add_route(struct sockaddr *, struct sockaddr *,
219     struct sockaddr *, const char *);
220 
221 /* NCU value set/get functions */
222 extern nwam_error_t nwamd_set_ncu_uint(nwam_ncu_handle_t, uint64_t *, uint_t,
223     const char *);
224 extern nwam_error_t nwamd_set_ncu_string(nwam_ncu_handle_t, char **, uint_t,
225     const char *);
226 extern nwam_error_t nwamd_get_ncu_uint(nwam_ncu_handle_t, nwam_value_t *,
227     uint64_t **, uint_t *, const char *);
228 extern nwam_error_t nwamd_get_ncu_string(nwam_ncu_handle_t, nwam_value_t *,
229     char ***, uint_t *, const char *);
230 
231 extern void nwamd_walk_physical_configuration(void);
232 
233 char *nwamd_link_to_ifname(const char *, int, char *, int);
234 
235 #endif /* _NCU_H */
236