1da6c28aamw/*
2da6c28aamw * CDDL HEADER START
3da6c28aamw *
4da6c28aamw * The contents of this file are subject to the terms of the
5da6c28aamw * Common Development and Distribution License (the "License").
6da6c28aamw * You may not use this file except in compliance with the License.
7da6c28aamw *
8da6c28aamw * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9da6c28aamw * or http://www.opensolaris.org/os/licensing.
10da6c28aamw * See the License for the specific language governing permissions
11da6c28aamw * and limitations under the License.
12da6c28aamw *
13da6c28aamw * When distributing Covered Code, include this CDDL HEADER in each
14da6c28aamw * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15da6c28aamw * If applicable, add the following below this CDDL HEADER, with the
16da6c28aamw * fields enclosed by brackets "[]" replaced with your own identifying
17da6c28aamw * information: Portions Copyright [yyyy] [name of copyright owner]
18da6c28aamw *
19da6c28aamw * CDDL HEADER END
20da6c28aamw */
21da6c28aamw/*
227f667e7jose borrego * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23da6c28aamw * Use is subject to license terms.
24da6c28aamw */
25da6c28aamw
26da6c28aamw/*
27da6c28aamw * Description:
28da6c28aamw *
29da6c28aamw *	Contains base code for netbios datagram service.
30da6c28aamw *
31da6c28aamw * Relavent sections from RFC1002:
32da6c28aamw *
33da6c28aamw *  5.3.  NetBIOS DATAGRAM SERVICE PROTOCOLS
34da6c28aamw *
35da6c28aamw *   The following are GLOBAL variables and should be NetBIOS user
36da6c28aamw *   configurable:
37da6c28aamw *
38da6c28aamw *   - SCOPE_ID: the non-leaf section of the domain name preceded by a
39da6c28aamw *     '.'  which represents the domain of the NetBIOS scope for the
40da6c28aamw *     NetBIOS name.  The following protocol description only supports
41da6c28aamw *     single scope operation.
42da6c28aamw *
43da6c28aamw *   - MAX_DATAGRAM_LENGTH: the maximum length of an IP datagram.  The
44da6c28aamw *     minimal maximum length defined in for IP is 576 bytes.  This
45da6c28aamw *     value is used when determining whether to fragment a NetBIOS
46da6c28aamw *     datagram.  Implementations are expected to be capable of
47da6c28aamw *     receiving unfragmented NetBIOS datagrams up to their maximum
48da6c28aamw *     size.
49da6c28aamw *
50da6c28aamw *   - BROADCAST_ADDRESS: the IP address B-nodes use to send datagrams
51da6c28aamw *     with group name destinations and broadcast datagrams.  The
52da6c28aamw *     default is the IP broadcast address for a single IP network.
53da6c28aamw *
54da6c28aamw *
55da6c28aamw *   The following are Defined Constants for the NetBIOS Datagram
56da6c28aamw *   Service:
57da6c28aamw *
58a0aa776Alan Wright *   - IPPORT_NETBIOS_DGM: the globally well-known UDP port allocated
59da6c28aamw *     where the NetBIOS Datagram Service receives UDP packets.  See
60da6c28aamw *     section 6, "Defined Constants", for its value.
61da6c28aamw */
62da6c28aamw
63da6c28aamw/*
64da6c28aamw *
65da6c28aamw *  6.  DEFINED CONSTANTS AND VARIABLES
66da6c28aamw *
67da6c28aamw *   GENERAL:
68da6c28aamw *
69da6c28aamw *      SCOPE_ID                   The name of the NetBIOS scope.
70da6c28aamw *
71da6c28aamw *                                 This is expressed as a character
72da6c28aamw *                                 string meeting the requirements of
73da6c28aamw *                                 the domain name system and without
74da6c28aamw *                                 a leading or trailing "dot".
75da6c28aamw *
76da6c28aamw *                                 An implementation may elect to make
77da6c28aamw *                                 this a single global value for the
78da6c28aamw *                                 node or allow it to be specified
79da6c28aamw *                                 with each separate NetBIOS name
80da6c28aamw *                                 (thus permitting cross-scope
81da6c28aamw *                                 references.)
82da6c28aamw *
83da6c28aamw *      BROADCAST_ADDRESS          An IP address composed of the
84da6c28aamw *                                 node network and subnetwork
85da6c28aamw *                                 numbers with all remaining bits set
86da6c28aamw *                                 to one.
87da6c28aamw *
88da6c28aamw *                                 I.e. "Specific subnet" broadcast
89da6c28aamw *                                 addressing according to section 2.3
90da6c28aamw *                                 of RFC 950.
91da6c28aamw *
92da6c28aamw *      BCAST_REQ_RETRY_TIMEOUT    250 milliseconds.
93da6c28aamw *                                 An adaptive timer may be used.
94da6c28aamw *
95da6c28aamw *      BCAST_REQ_RETRY_COUNT      3
96da6c28aamw *
97da6c28aamw *      UCAST_REQ_RETRY_TIMEOUT    5 seconds
98da6c28aamw *                                 An adaptive timer may be used.
99da6c28aamw *
100da6c28aamw *      UCAST_REQ_RETRY_COUNT      3
101da6c28aamw *
102da6c28aamw *      MAX_DATAGRAM_LENGTH        576 bytes (default)
103da6c28aamw *
104da6c28aamw *   DATAGRAM SERVICE:
105da6c28aamw *
106a0aa776Alan Wright *      IPPORT_NETBIOS_DGM          138 (decimal)
107da6c28aamw *
108da6c28aamw *      FRAGMENT_TO                2 seconds (default)
109da6c28aamw */
110da6c28aamw
111a0aa776Alan Wright#include <errno.h>
112da6c28aamw#include <stdlib.h>
113da6c28aamw#include <unistd.h>
114da6c28aamw#include <string.h>
115da6c28aamw#include <strings.h>
116da6c28aamw#include <syslog.h>
117da6c28aamw#include <synch.h>
118da6c28aamw#include <sys/socket.h>
119da6c28aamw#include <arpa/inet.h>
120da6c28aamw
121da6c28aamw#include <smbns_netbios.h>
122da6c28aamw
123da6c28aamw#include <smbsrv/libsmbns.h>
124da6c28aamw
125da6c28aamwstatic int datagram_sock = -1;
126da6c28aamwstatic short datagram_id = 1;
127da6c28aamwstatic struct datagram_queue smb_datagram_queue;
128da6c28aamwstatic mutex_t smb_dgq_mtx;
129da6c28aamw
1308b2cc8aafshin salek ardakani - Sun Microsystems - Irvine United Statesstatic void smb_netbios_datagram_error(unsigned char *buf);
1318b2cc8aafshin salek ardakani - Sun Microsystems - Irvine United States
132da6c28aamw/*
133da6c28aamw * Function:  smb_netbios_datagram_tick(void)
134da6c28aamw *
135da6c28aamw * Description:
136da6c28aamw *
137da6c28aamw *	Called once a second to handle time to live timeouts in
138da6c28aamw *	datagram assembly queue.
139da6c28aamw *
140da6c28aamw * Inputs:
141da6c28aamw *
142da6c28aamw * Returns:
143da6c28aamw *	void	-> Nothing at all...
144da6c28aamw */
145da6c28aamw
146da6c28aamwvoid
147da6c28aamwsmb_netbios_datagram_tick(void)
148da6c28aamw{
149da6c28aamw	struct datagram *entry;
150da6c28aamw	struct datagram *next;
151da6c28aamw
152da6c28aamw	(void) mutex_lock(&smb_dgq_mtx);
153da6c28aamw
154da6c28aamw	for (entry = smb_datagram_queue.forw;
155da6c28aamw	    entry != (struct datagram *)((uintptr_t)&smb_datagram_queue);
156da6c28aamw	    entry = next) {
157da6c28aamw		next = entry->forw;
158da6c28aamw		if (--entry->discard_timer == 0) {
159da6c28aamw			/* Toss it */
160da6c28aamw			QUEUE_CLIP(entry);
161da6c28aamw			free(entry);
162da6c28aamw		}
163da6c28aamw	}
164da6c28aamw	(void) mutex_unlock(&smb_dgq_mtx);
165