xref: /titanic_50/usr/src/cmd/smbsrv/smbd/smbd_nicmon.c (revision 1573d361e97690e67db291a1e0dc9a9b58f73fd8)
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 2008 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 /*
29  * This is the SMB NIC monitoring module.
30  */
31 #include <sys/types.h>
32 #include <stdlib.h>
33 #include <errno.h>
34 #include <string.h>
35 #include <unistd.h>
36 #include <signal.h>
37 #include <stdio.h>
38 #include <net/if.h>
39 #include <net/route.h>
40 #include <sys/sockio.h>
41 #include <sys/socket.h>
42 #include <netinet/in.h>
43 #include <arpa/inet.h>
44 #include <fcntl.h>
45 #include <pthread.h>
46 #include <syslog.h>
47 #include <smbsrv/libsmb.h>
48 #include <smbsrv/libsmbns.h>
49 
50 static pthread_t smb_nicmon_thread;
51 
52 static void smb_setup_rtsock(int, int *);
53 static int smb_nics_changed(int);
54 static void *smb_nicmonitor(void *);
55 static int smb_setup_eventpipe(int *, int *);
56 static void smb_process_nic_change();
57 
58 /* Use this to stop monitoring */
59 static int eventpipe_write = -1;
60 
61 /*
62  * Start the nic monitor thread.
63  */
64 int
65 smb_nicmon_start(void)
66 {
67 	int rc = 0;
68 
69 	smb_nic_build_info();
70 	rc = pthread_create(&smb_nicmon_thread, NULL, smb_nicmonitor, 0);
71 	if (rc != 0) {
72 		syslog(LOG_ERR, "smb_nicmonitor: "
73 		    "NIC monitoring failed to start: %s", strerror(errno));
74 		return (rc);
75 	}
76 	return (rc);
77 }
78 
79 /*
80  * Stop the nic monitor.
81  */
82 void
83 smb_nicmon_stop(void)
84 {
85 	uchar_t buf = 1;
86 
87 	if (eventpipe_write < 0)
88 		return;
89 
90 	(void) write(eventpipe_write, &buf, sizeof (buf));
91 }
92 
93 /*
94  * Setup routing socket for getting RTM messages.
95  */
96 static void
97 smb_setup_rtsock(int af, int *s)
98 {
99 	int flags;
100 
101 	*s = socket(PF_ROUTE, SOCK_RAW, af);
102 	if (*s == -1) {
103 		syslog(LOG_ERR, "smb_setup_rtsock: failed to "
104 		    "create routing socket");
105 		return;
106 	}
107 	if ((flags = fcntl(*s, F_GETFL, 0)) < 0) {
108 		syslog(LOG_ERR, "smb_setup_rtsock: failed to fcntl F_GETFL");
109 		(void) close(*s);
110 		*s = -1;
111 		return;
112 	}
113 	if ((fcntl(*s, F_SETFL, flags | O_NONBLOCK)) < 0) {
114 		syslog(LOG_ERR, "smb_setup_rtsock: failed to fcntl F_SETFL");
115 		(void) close(*s);
116 		*s = -1;
117 		return;
118 	}
119 }
120 
121 static int
122 smb_nics_changed(int sock)
123 {
124 	int	nbytes;
125 	int64_t msg[2048 / 8];
126 	struct rt_msghdr *rtm;
127 	int need_if_scan = 0;
128 
129 	/* Read as many messages as possible and try to empty the sockets */
130 	for (;;) {
131 		nbytes = read(sock, msg, sizeof (msg));
132 		if (nbytes <= 0) {
133 			break;
134 		}
135 		rtm = (struct rt_msghdr *)msg;
136 		if (rtm->rtm_version != RTM_VERSION) {
137 			continue;
138 		}
139 		if (nbytes < rtm->rtm_msglen) {
140 			syslog(LOG_DEBUG, "smb_nicmonitor: short read: %d "
141 			    "of %d", nbytes, rtm->rtm_msglen);
142 			continue;
143 		}
144 
145 		switch (rtm->rtm_type) {
146 		case RTM_NEWADDR:
147 		case RTM_DELADDR:
148 		case RTM_IFINFO:
149 			need_if_scan = 1;
150 			break;
151 		default:
152 			break;
153 		}
154 	}
155 
156 	return (need_if_scan);
157 }
158 
159 /*
160  * Create pipe for signal delivery and set up signal handlers.
161  */
162 static int
163 smb_setup_eventpipe(int *read_pipe, int *write_pipe)
164 {
165 	int fds[2];
166 
167 	if ((pipe(fds)) < 0) {
168 		syslog(LOG_ERR, "smb_nicmonitor: failed to open pipe");
169 		return (1);
170 	}
171 	*read_pipe = fds[0];
172 	*write_pipe = fds[1];
173 	return (0);
174 }
175 
176 /*
177  * Call this to do stuff after ifs changed.
178  */
179 static void
180 smb_process_nic_change()
181 {
182 	boolean_t ddns_enabled;
183 
184 	ddns_enabled = smb_config_getbool(SMB_CI_DYNDNS_ENABLE);
185 
186 	/* Clear rev zone before creating if list */
187 	if (ddns_enabled) {
188 		if (dyndns_clear_rev_zone() != 0) {
189 			syslog(LOG_ERR, "smb_nicmonitor: "
190 			    "failed to clear DNS reverse lookup zone");
191 		}
192 	}
193 
194 	/* re-initialize NIC table */
195 	smb_nic_build_info();
196 
197 	smb_netbios_name_reconfig();
198 	smb_browser_config();
199 
200 	if (ddns_enabled) {
201 		if (dyndns_update() != 0) {
202 			syslog(LOG_ERR, "smb_nicmonitor: "
203 			    "failed to update dynamic DNS");
204 		}
205 	}
206 }
207 
208 /*ARGSUSED*/
209 static void *
210 smb_nicmonitor(void *args)
211 {
212 	struct pollfd pollfds[2];
213 	int pollfd_num = 2;
214 	int i, nic_changed;
215 	/* AF_INET routing socket add AF_INET6 when we support IPv6 */
216 	static int rtsock_v4;
217 	static int eventpipe_read = -1;
218 
219 	/*
220 	 * Create the global routing socket.  We use this to
221 	 * monitor changes in NIC interfaces. We are only interested
222 	 * in new inerface addition/deletion and change in UP/DOWN status.
223 	 */
224 	smb_setup_rtsock(AF_INET, &rtsock_v4);
225 	if (rtsock_v4 == -1) {
226 		syslog(LOG_ERR, "smb_nicmonitor: cannot open routing socket");
227 		return (NULL);
228 	}
229 	if (smb_setup_eventpipe(&eventpipe_read, &eventpipe_write) != 0) {
230 		syslog(LOG_ERR, "smb_nicmonitor: cannot open event pipes");
231 		return (NULL);
232 	}
233 
234 	/*
235 	 * Keep listening for activity on any of the sockets.
236 	 */
237 	for (;;) {
238 		nic_changed = 0;
239 		pollfds[0].fd = rtsock_v4;
240 		pollfds[0].events = POLLIN;
241 		pollfds[1].fd = eventpipe_read;
242 		pollfds[1].events = POLLIN;
243 		if (poll(pollfds, pollfd_num, -1) < 0) {
244 			if (errno == EINTR)
245 				continue;
246 			syslog(LOG_ERR, "smb_nicmonitor: "
247 			    "poll failed with errno %d", errno);
248 			break;
249 		}
250 		for (i = 0; i < pollfd_num; i++) {
251 			if ((pollfds[i].fd < 0) ||
252 			    !(pollfds[i].revents & POLLIN))
253 				continue;
254 			if (pollfds[i].fd == rtsock_v4)
255 				nic_changed = smb_nics_changed(rtsock_v4);
256 			if (pollfds[i].fd == eventpipe_read)
257 				goto done;
258 		}
259 
260 		/*
261 		 * If anything changed do refresh our
262 		 * nic list and other configs.
263 		 */
264 		if (nic_changed)
265 			smb_process_nic_change();
266 	}
267 done:
268 	/* Close sockets */
269 	(void) close(rtsock_v4);
270 	(void) close(eventpipe_read);
271 	(void) close(eventpipe_write);
272 	eventpipe_write = -1;
273 	return (NULL);
274 }
275