summaryrefslogtreecommitdiff
path: root/usr/src/cmd/smbsrv/smbd/smbd_nicmon.c
blob: 90934997163ce236faffe16e343a2b20ddc7d9fa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
 */

/*
 * smbd NIC monitor.
 */

#include <sys/types.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include <stdio.h>
#include <net/if.h>
#include <net/route.h>
#include <sys/sockio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <fcntl.h>
#include <pthread.h>
#include <syslog.h>
#include <smbsrv/libsmb.h>
#include "smbd.h"

#define	SMBD_NICMON_ENABLE	"nicmon_enable"
#define	SMBD_NICMON_THROTTLE	100
#define	SMBD_NICMON_DEBOUNCE	2

extern smbd_t smbd;

static boolean_t smbd_nicmon_enabled = B_TRUE;

/* Use this to stop monitoring */
static int eventpipe_write = -1;

/* Use this to refresh service instance */
static char *smbd_nicmon_caller_fmri = NULL;

static void smbd_nicmon_run_check(void);
static int smbd_nicmon_setup_rtsock(int);
static int smbd_nicmon_needscan(int);
static int smbd_nicmon_setup_eventpipe(int *, int *);
static void *smbd_nicmon_daemon(void *);

/*
 * Start the nic monitor thread.
 */
int
smbd_nicmon_start(const char *svc_fmri)
{
	pthread_t	smbd_nicmon_tid;
	int		rc;

	if (smb_nic_init() != SMB_NIC_SUCCESS)
		return (-1);

	rc = pthread_create(&smbd_nicmon_tid, NULL, smbd_nicmon_daemon, NULL);
	if (rc != 0)
		return (-1);

	if (svc_fmri)
		smbd_nicmon_caller_fmri = (char *)svc_fmri;

	smbd_nicmon_run_check();
	return (0);
}

void
smbd_nicmon_stop(void)
{
	uchar_t buf = 1;

	if (eventpipe_write < 0)
		return;

	(void) write(eventpipe_write, &buf, sizeof (buf));
	smbd_nicmon_caller_fmri = NULL;
	smb_nic_fini();
}

int
smbd_nicmon_refresh(void)
{
	if (smb_nic_init() != SMB_NIC_SUCCESS)
		return (-1);

	smbd_nicmon_run_check();
	return (0);
}

/*
 * The monitor is enabled unless it is explicitly
 * disabled by setting smbd/nicmon_enable to false.
 * smbd/nicmon_enable is not defined by default.
 */
static void
smbd_nicmon_run_check(void)
{
	smb_scfhandle_t	*hd;
	uint8_t		status;
	int		rc;

	smbd_nicmon_enabled = B_TRUE;

	if ((hd = smb_smf_scf_init(SMBD_FMRI_PREFIX)) == NULL) {
		syslog(LOG_DEBUG,
		    "smbd_nicmon: smb_smf_scf_init failed");
		return;
	}

	rc = smb_smf_create_service_pgroup(hd, SMBD_PG_NAME);
	if (rc != SMBD_SMF_OK) {
		smb_smf_scf_fini(hd);
		syslog(LOG_DEBUG,
		    "smbd_nicmon: smb_smf_create_service_pgroup failed");
		return;
	}

	rc = smb_smf_get_boolean_property(hd, SMBD_NICMON_ENABLE, &status);
	if (rc == SMBD_SMF_OK && status == 0)
		smbd_nicmon_enabled = B_FALSE;

	smb_smf_scf_fini(hd);
}

/*
 * Setup routing socket for getting RTM messages.
 */
static int
smbd_nicmon_setup_rtsock(int af)
{
	int sd;
	int flags;

	if ((sd = socket(PF_ROUTE, SOCK_RAW, af)) == -1) {
		syslog(LOG_ERR,
		    "smbd_nicmon: routing socket failed: %d", errno);
		return (-1);
	}

	if ((flags = fcntl(sd, F_GETFL, 0)) < 0) {
		syslog(LOG_ERR,
		    "smbd_nicmon: fcntl F_GETFL failed: %d", errno);
		(void) close(sd);
		return (-1);
	}

	if ((fcntl(sd, F_SETFL, flags | O_NONBLOCK)) < 0) {
		syslog(LOG_ERR,
		    "smbd_nicmon: fcntl F_SETFL failed: %d", errno);
		(void) close(sd);
		return (-1);
	}

	return (sd);
}

static int
smbd_nicmon_needscan(int sock)
{
	static uint32_t		throttle;
	struct rt_msghdr	*rtm;
	int64_t			msg[2048 / 8];
	int			need_if_scan = 0;
	int			nbytes;

	/* Read as many messages as possible and try to empty the sockets */
	for (;;) {
		nbytes = read(sock, msg, sizeof (msg));
		if (nbytes <= 0)
			break;

		rtm = (struct rt_msghdr *)msg;
		if (rtm->rtm_version != RTM_VERSION)
			continue;

		if (nbytes < rtm->rtm_msglen) {
			if ((throttle % SMBD_NICMON_THROTTLE) == 0) {
				syslog(LOG_DEBUG,
				    "smbd_nicmon: short read: %d of %d",
				    nbytes, rtm->rtm_msglen);
			}
			++throttle;
			continue;
		}

		switch (rtm->rtm_type) {
		case RTM_NEWADDR:
		case RTM_DELADDR:
		case RTM_IFINFO:
			need_if_scan = 1;
			break;
		default:
			break;
		}
	}

	return (need_if_scan);
}

/*
 * Create pipe for signal delivery and set up signal handlers.
 */
static int
smbd_nicmon_setup_eventpipe(int *read_pipe, int *write_pipe)
{
	int fds[2];

	if ((pipe(fds)) < 0) {
		syslog(LOG_ERR,
		    "smbd_nicmon: event pipe failed: %d", errno);
		return (-1);
	}

	*read_pipe = fds[0];
	*write_pipe = fds[1];
	return (0);
}

/*
 * Create the global routing socket to monitor changes in NIC interfaces.
 * We are only interested in new inerface addition/deletion and changes
 * in UP/DOWN status.
 *
 * Note: only supports AF_INET routing socket.  Need to add AF_INET6 to
 * support IPv6.
 */
/*ARGSUSED*/
static void *
smbd_nicmon_daemon(void *arg)
{
	static uint32_t	throttle;
	static int	rtsock_v4;
	static int	eventpipe_read = -1;
	struct pollfd	pollfds[2];
	int		pollfd_num = 2;
	int		i, nic_changed;
	int		rc;

	if ((rtsock_v4 = smbd_nicmon_setup_rtsock(AF_INET)) == -1)
		return (NULL);

	rc = smbd_nicmon_setup_eventpipe(&eventpipe_read, &eventpipe_write);
	if (rc != 0)
		return (NULL);

	/*
	 * Listen for activity on any of the sockets.
	 * The delay before checking the rtsock will hopefully
	 * smooth things out when there is a lot of activity.
	 */
	for (;;) {
		errno = 0;
		nic_changed = 0;
		pollfds[0].fd = rtsock_v4;
		pollfds[0].events = POLLIN;
		pollfds[1].fd = eventpipe_read;
		pollfds[1].events = POLLIN;

		if (poll(pollfds, pollfd_num, -1) < 0) {
			if (errno == EINTR)
				continue;
			if ((throttle % SMBD_NICMON_THROTTLE) == 0)
				syslog(LOG_DEBUG,
				    "smbd_nicmon: poll failed: %d", errno);
			++throttle;
			break;
		}

		for (i = 0; i < pollfd_num; i++) {
			if ((pollfds[i].fd < 0) ||
			    !(pollfds[i].revents & POLLIN))
				continue;
			if (pollfds[i].fd == rtsock_v4) {
				(void) sleep(SMBD_NICMON_DEBOUNCE);
				nic_changed = smbd_nicmon_needscan(rtsock_v4);
			}
			if (pollfds[i].fd == eventpipe_read)
				goto done;
		}

		/*
		 * If the monitor is enabled and something has changed,
		 * refresh the registered SMF service.
		 */
		if (smbd_nicmon_enabled && nic_changed &&
		    smbd_nicmon_caller_fmri) {
			if (smf_refresh_instance(smbd_nicmon_caller_fmri) != 0)
				syslog(LOG_ERR,
				    "smbd_nicmon: %s refresh failed",
				    smbd_nicmon_caller_fmri);
		}
	}
done:
	(void) close(rtsock_v4);
	(void) close(eventpipe_read);
	(void) close(eventpipe_write);
	eventpipe_write = -1;
	return (NULL);
}