1da6c28aaSamw /*
2da6c28aaSamw  * CDDL HEADER START
3da6c28aaSamw  *
4da6c28aaSamw  * The contents of this file are subject to the terms of the
5da6c28aaSamw  * Common Development and Distribution License (the "License").
6da6c28aaSamw  * You may not use this file except in compliance with the License.
7da6c28aaSamw  *
8da6c28aaSamw  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9da6c28aaSamw  * or http://www.opensolaris.org/os/licensing.
10da6c28aaSamw  * See the License for the specific language governing permissions
11da6c28aaSamw  * and limitations under the License.
12da6c28aaSamw  *
13da6c28aaSamw  * When distributing Covered Code, include this CDDL HEADER in each
14da6c28aaSamw  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15da6c28aaSamw  * If applicable, add the following below this CDDL HEADER, with the
16da6c28aaSamw  * fields enclosed by brackets "[]" replaced with your own identifying
17da6c28aaSamw  * information: Portions Copyright [yyyy] [name of copyright owner]
18da6c28aaSamw  *
19da6c28aaSamw  * CDDL HEADER END
20da6c28aaSamw  */
21da6c28aaSamw /*
22*fd9ee8b5Sjoyce mcintosh  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
23da6c28aaSamw  */
25a0aa776eSAlan Wright #include <sys/tzfile.h>
268d7e4166Sjose borrego #include <errno.h>
27da6c28aaSamw #include <stdlib.h>
28da6c28aaSamw #include <stdio.h>
29da6c28aaSamw #include <unistd.h>
30da6c28aaSamw #include <syslog.h>
31da6c28aaSamw #include <string.h>
32da6c28aaSamw #include <strings.h>
33da6c28aaSamw #include <time.h>
34da6c28aaSamw #include <synch.h>
35da6c28aaSamw #include <netdb.h>
36da6c28aaSamw #include <sys/socket.h>
37da6c28aaSamw #include <arpa/inet.h>
39dc20a302Sas #include <smbsrv/libsmb.h>
40da6c28aaSamw #include <smbsrv/libsmbns.h>
41bbf6f00cSJordan Brown #include <smbsrv/smb.h>
42da6c28aaSamw #include <smbsrv/mailslot.h>
43da6c28aaSamw #include <smbns_browser.h>
44da6c28aaSamw #include <smbns_netbios.h>
468d7e4166Sjose borrego /*
478d7e4166Sjose borrego  * ntdomain_info
488d7e4166Sjose borrego  * Temporary. It should be removed once NBTD is integrated.
498d7e4166Sjose borrego  */
508d7e4166Sjose borrego smb_ntdomain_t ntdomain_info;
518d7e4166Sjose borrego mutex_t ntdomain_mtx;
528d7e4166Sjose borrego cond_t ntdomain_cv;
538d7e4166Sjose borrego 
54da6c28aaSamw #define	SMB_SERVER_SIGNATURE		0xaa550415
567b59d02dSjb typedef struct smb_hostinfo {
577b59d02dSjb 	list_node_t	hi_lnd;
587b59d02dSjb 	smb_nic_t	hi_nic;
597b59d02dSjb 	char		hi_nbname[NETBIOS_NAME_SZ];
607b59d02dSjb 	name_entry_t	hi_netname;
617b59d02dSjb 	uint32_t	hi_nextannouce;
627b59d02dSjb 	int		hi_reps;
637b59d02dSjb 	int		hi_interval;
647b59d02dSjb 	uint8_t		hi_updatecnt;
657b59d02dSjb 	uint32_t	hi_type;
669fb67ea3Safshin salek ardakani - Sun Microsystems - Irvine United States 	smb_version_t	hi_version;
677b59d02dSjb } smb_hostinfo_t;
697b59d02dSjb typedef struct smb_browserinfo {
707b59d02dSjb 	list_t		bi_hlist;
717b59d02dSjb 	int		bi_hcnt;
727b59d02dSjb 	rwlock_t	bi_hlist_rwl;
737b59d02dSjb 	boolean_t	bi_changed;
747b59d02dSjb 	mutex_t		bi_mtx;
757b59d02dSjb } smb_browserinfo_t;
777b59d02dSjb static smb_browserinfo_t smb_binfo;
797b59d02dSjb static int smb_browser_init(void);
807b59d02dSjb static void smb_browser_infoinit(void);
817b59d02dSjb static void smb_browser_infoterm(void);
827b59d02dSjb static void smb_browser_infofree(void);
848d7e4166Sjose borrego 
857b59d02dSjb void
smb_browser_reconfig(void)867b59d02dSjb smb_browser_reconfig(void)
87da6c28aaSamw {
887b59d02dSjb 	(void) mutex_lock(&smb_binfo.bi_mtx);
897b59d02dSjb 	smb_binfo.bi_changed = B_TRUE;
907b59d02dSjb 	(void) mutex_unlock(&smb_binfo.bi_mtx);
91da6c28aaSamw }
93da6c28aaSamw /*
94da6c28aaSamw  * 3. Browser Overview
95da6c28aaSamw  *
96da6c28aaSamw  * Hosts involved in the browsing process can be separated into two
97da6c28aaSamw  * distinct groups, browser clients and browser servers (often referred to
98da6c28aaSamw  * simply as "browsers").
99da6c28aaSamw  *
100da6c28aaSamw  * A browser is a server which maintains information about servers -
101da6c28aaSamw  * primarily the domain they are in and the services that they are running
102da6c28aaSamw  * -- and about domains. Browsers may assume several different roles in
103da6c28aaSamw  * their lifetimes, and dynamically switch between them.
104da6c28aaSamw  *
105da6c28aaSamw  *  Browser clients are of two types: workstations and (non-browser)
106da6c28aaSamw  * servers. In the context of browsing, workstations query browsers for the
107da6c28aaSamw  * information they contain; servers supply browsers the information by
108da6c28aaSamw  * registering with them. Note that, at times, browsers may themselves
109da6c28aaSamw  * behave as browser clients and query other browsers.
110da6c28aaSamw  *
111da6c28aaSamw  * For the purposes of this specification, a domain is simply a name with
112da6c28aaSamw  * which to associate a group of resources such as computers, servers and
113da6c28aaSamw  * users. Domains allow a convenient means for browser clients to restrict
114da6c28aaSamw  * the scope of a search when they query browser servers. Every domain has
115da6c28aaSamw  * a "master" server called the Primary Domain Controller (PDC) that
116da6c28aaSamw  * manages various  activities within the domain.
117da6c28aaSamw  *
118da6c28aaSamw  * One browser for each domain on a subnet is designated the Local Master
119da6c28aaSamw  * Browser for that domain. Servers in its domain on the subnet register
120da6c28aaSamw  * with it, as do the Local Master Browsers for other domains on the
121da6c28aaSamw  * subnet. It uses these registrations to maintain authoritative
122da6c28aaSamw  * information about its domain on its subnet. If there are other subnets
123da6c28aaSamw  * in the network, it also knows the name of the server running the
124da6c28aaSamw  * domain's Domain Master Browser; it registers with it, and uses it to
125da6c28aaSamw  * obtain information about the rest of the network (see below).
126da6c28aaSamw  *
127da6c28aaSamw  * Clients on a subnet query browsers designated as the Backup Browsers for
128da6c28aaSamw  * the subnet (not the Master Browser). Backup Browsers maintain a copy of
129da6c28aaSamw  * the information on the Local Master Browser; they get it by periodically
130da6c28aaSamw  * querying the Local Master Browser for all of its information. Clients
131da6c28aaSamw  * find the Backup Browsers by asking the Local Master Browser. Clients are
132da6c28aaSamw  * expected to spread their queries evenly across Backup Browsers to
133da6c28aaSamw  * balance the load.
134da6c28aaSamw  *
135da6c28aaSamw  * The Local Master Browser is dynamically elected automatically. Multiple
136da6c28aaSamw  * Backup Browser Servers may exist per subnet; they are selected from
137da6c28aaSamw  * among the potential browser servers by the Local Master Browser, which
138da6c28aaSamw  * is configured to select enough to handle the expected query load.
139da6c28aaSamw  *
140da6c28aaSamw  * When there are multiple subnets, a Domain Master Browser is assigned
141da6c28aaSamw  * the task of keeping the multiple subnets in synchronization. The Primary
142da6c28aaSamw  * Domain Controller (PDC) always acts as the Domain Master Browser. The
143da6c28aaSamw  * Domain Master Browser periodically acts as a client and queries all the
144da6c28aaSamw  * Local Master Browsers for its domain, asking them for a list containing
145da6c28aaSamw  * all the domains and all the servers in their domain known within their
146da6c28aaSamw  * subnets; it merges all the replies into a single master list. This
147da6c28aaSamw  * allows a Domain Master Browser server to act as a collection point for
148da6c28aaSamw  * inter-subnet browsing information. Local Master Browsers periodically
149da6c28aaSamw  * query the Domain Master Browser to retrieve the network-wide information
150da6c28aaSamw  * it maintains.
151da6c28aaSamw  *
152da6c28aaSamw  * When a domain spans only a single subnet, there will not be any distinct
153da6c28aaSamw  * Local Master Browser; this role will be handled by the Domain Master
154da6c28aaSamw  * Browser. Similarly, the Domain Master Browser is always the Local Master
155da6c28aaSamw  * Browser for the subnet it is on.
156da6c28aaSamw  *
157da6c28aaSamw  * When a browser client suspects that the Local Master Browser has failed,
158da6c28aaSamw  * the client will instigate an election in which the browser servers
159da6c28aaSamw  * participate, and some browser servers may change roles.
160da6c28aaSamw  *
161da6c28aaSamw  * Some characteristics of a good browsing mechanism include:
162da6c28aaSamw  * . minimal network traffic
163da6c28aaSamw  * . minimum server discovery time
164da6c28aaSamw  * . minimum change discovery latency
165da6c28aaSamw  * . immunity to machine failures
166da6c28aaSamw  *
167da6c28aaSamw  * Historically, Browser implementations had been very closely tied to
168da6c28aaSamw  * NETBIOS and datagrams. The early implementations caused a lot of
169da6c28aaSamw  * broadcast traffic. See Appendix D for an overview that presents how the
170da6c28aaSamw  * Browser specification evolved.
171da6c28aaSamw  *
172da6c28aaSamw  * 4. Browsing Protocol Architecture
173da6c28aaSamw  *
174da6c28aaSamw  * This section first describes the how the browsing protocol is layered,
175da6c28aaSamw  * then describes the roles of clients, servers, and browsers in the
176da6c28aaSamw  * browsing subsystem.
177da6c28aaSamw  *
178da6c28aaSamw  * 4.1 Layering of Browsing Protocol Requests
179da6c28aaSamw  *
180da6c28aaSamw  * Most of the browser functionality is implemented using mailslots.
181da6c28aaSamw  * Mailslots provide a mechanism for fast, unreliable unidirectional data