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 2007 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #ifndef _SMB_NETBIOS_H_
27 #define	_SMB_NETBIOS_H_
28 
29 #pragma ident	"%Z%%M%	%I%	%E% SMI"
30 
31 /*
32  * 4.2.  NAME SERVICE PACKETS
33  *
34  * 4.2.1.  GENERAL FORMAT OF NAME SERVICE PACKETS
35  *
36  *   The NetBIOS Name Service packets follow the packet structure defined
37  *   in the Domain Name Service (DNS) RFC 883 [7 (pg 26-31)].  The
38  *   structures are compatible with the existing DNS packet formats,
39  *   however, additional types and codes have been added to work with
40  *   NetBIOS.
41  *
42  *   If Name Service packets are sent over a TCP connection they are
43  *   preceded by a 16 bit unsigned integer representing the length of the
44  *   Name Service packet.
45  *
46  *                        1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
47  *    0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
48  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
49  *   |                                                               |
50  *   + ------                                                ------- +
51  *   |                            HEADER                             |
52  *   + ------                                                ------- +
53  *   |                                                               |
54  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
55  *   |                                                               |
56  *   /                       QUESTION ENTRIES                        /
57  *   |                                                               |
58  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
59  *   |                                                               |
60  *   /                    ANSWER RESOURCE RECORDS                    /
61  *   |                                                               |
62  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
63  *   |                                                               |
64  *   /                  AUTHORITY RESOURCE RECORDS                   /
65  *   |                                                               |
66  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
67  *   |                                                               |
68  *   /                  ADDITIONAL RESOURCE RECORDS                  /
69  *   |                                                               |
70  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
71  */
72 
73 /*
74  * 4.2.1.1 HEADER
75  *
76  *                        1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
77  *    0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
78  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
79  *   |         NAME_TRN_ID           | OPCODE  |   NM_FLAGS  | RCODE |
80  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
81  *   |          QDCOUNT              |           ANCOUNT             |
82  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
83  *   |          NSCOUNT              |           ARCOUNT             |
84  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
85  *
86  *   Field     Description
87  *
88  *   NAME_TRN_ID      Transaction ID for Name Service Transaction.
89  *                    Requester places a unique value for each active
90  *                    transaction.  Responder puts NAME_TRN_ID value
91  *                    from request packet in response packet.
92  *
93  *   OPCODE           Packet type code, see table below.
94  *
95  *   NM_FLAGS         Flags for operation, see table below.
96  *
97  *   RCODE            Result codes of request.  Table of RCODE values
98  *                    for each response packet below.
99  *
100  *   QDCOUNT          Unsigned 16 bit integer specifying the number of
101  *                    entries in the question section of a Name
102  *                    Service packet.  Always zero (0) for responses.
103  *                    Must be non-zero for all NetBIOS Name requests.
104  *
105  *   ANCOUNT          Unsigned 16 bit integer specifying the number of
106  *                    resource records in the answer section of a Name
107  *                    Service packet.
108  *
109  *   NSCOUNT          Unsigned 16 bit integer specifying the number of
110  *                    resource records in the authority section of a
111  *                    Name Service packet.
112  *
113  *   ARCOUNT          Unsigned 16 bit integer specifying the number of
114  *                    resource records in the additional records
115  *                    section of a Name Service packet.
116  */
117 
118 
119 /*
120  *   The OPCODE field is defined as:
121  *
122  *     0   1   2   3   4
123  *   +---+---+---+---+---+
124  *   | R |    OPCODE     |
125  *   +---+---+---+---+---+
126  *
127  *   Symbol     Bit(s)   Description
128  *
129  *   OPCODE        1-4   Operation specifier:
130  *                         0 = query
131  *                         5 = registration
132  *                         6 = release
133  *                         7 = WACK
134  *                         8 = refresh
135  *
136  *   R               0   RESPONSE flag:
137  *                         if bit == 0 then request packet
138  *                         if bit == 1 then response packet.
139  */
140 
141 /*
142  *   The NM_FLAGS field is defined as:
143  *
144  *
145  *     0   1   2   3   4   5   6
146  *   +---+---+---+---+---+---+---+
147  *   |AA |TC |RD |RA | 0 | 0 | B |
148  *   +---+---+---+---+---+---+---+
149  *
150  *   Symbol     Bit(s)   Description
151  *
152  *   B               6   Broadcast Flag.
153  *                         = 1: packet was broadcast or multicast
154  *                         = 0: unicast
155  *
156  *   RA              3   Recursion Available Flag.
157  *
158  *                       Only valid in responses from a NetBIOS Name
159  *                       Server -- must be zero in all other
160  *                       responses.
161  *
162  *                       If one (1) then the NBNS supports recursive
163  *                       query, registration, and release.
164  *
165  *                       If zero (0) then the end-node must iterate
166  *                       for query and challenge for registration.
167  *
168  *   RD              2   Recursion Desired Flag.
169  *
170  *                       May only be set on a request to a NetBIOS
171  *                       Name Server.
172  *
173  *                       The NBNS will copy its state into the
174  *                       response packet.
175  *
176  *                       If one (1) the NBNS will iterate on the
177  *                       query, registration, or release.
178  *
179  *   TC              1   Truncation Flag.
180  *
181  *                       Set if this message was truncated because the
182  *                       datagram carrying it would be greater than
183  *                       576 bytes in length.  Use TCP to get the
184  *                       information from the NetBIOS Name Server.
185  *
186  *   AA              0   Authoritative Answer flag.
187  *
188  *                       Must be zero (0) if R flag of OPCODE is zero
189  *                       (0).
190  *
191  *                       If R flag is one (1) then if AA is one (1)
192  *                       then the node responding is an authority for
193  *                       the domain name.
194  *
195  *                       End nodes responding to queries always set
196  *                       this bit in responses.
197  */
198 
199 /*
200  * 4.2.1.2 QUESTION SECTION
201  *
202  *                        1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
203  *    0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
204  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
205  *   |                                                               |
206  *   /                         QUESTION_NAME                         /
207  *   /                                                               /
208  *   |                                                               |
209  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
210  *   |         QUESTION_TYPE         |        QUESTION_CLASS         |
211  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
212  *
213  *   Field            Description
214  *
215  *   QUESTION_NAME    The compressed name representation of the
216  *                    NetBIOS name for the request.
217  *
218  *   QUESTION_TYPE    The type of request.  The values for this field
219  *                    are specified for each request.
220  *
221  *   QUESTION_CLASS   The class of the request.  The values for this
222  *                    field are specified for each request.
223  *
224  *   QUESTION_TYPE is defined as:
225  *
226  *   Symbol      Value   Description:
227  *
228  *   NB         0x0020   NetBIOS general Name Service Resource Record
229  *   NBSTAT     0x0021   NetBIOS NODE STATUS Resource Record (See NODE
230  *                       STATUS REQUEST)
231  *
232  *   QUESTION_CLASS is defined as:
233  *
234  *   Symbol      Value   Description:
235  *
236  *   IN         0x0001   Internet class
237  */
238 
239 /*
240  * 4.2.1.3 RESOURCE RECORD
241  *
242  *                        1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
243  *    0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
244  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
245  *   |                                                               |
246  *   /                            RR_NAME                            /
247  *   /                                                               /
248  *   |                                                               |
249  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
250  *   |           RR_TYPE             |          RR_CLASS             |
251  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
252  *   |                              TTL                              |
253  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
254  *   |           RDLENGTH            |                               |
255  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+                               |
256  *   /                                                               /
257  *   /                             RDATA                             /
258  *   |                                                               |
259  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
260  *
261  *   Field            Description
262  *
263  *   RR_NAME          The compressed name representation of the
264  *                    NetBIOS name corresponding to this resource
265  *                    record.
266  *
267  *   RR_TYPE          Resource record type code
268  *
269  *   RR_CLASS         Resource record class code
270  *
271  *   TTL              The Time To Live of a the resource record's
272  *                    name.
273  *
274  *   RDLENGTH         Unsigned 16 bit integer that specifies the
275  *                    number of bytes in the RDATA field.
276  *
277  *   RDATA            RR_CLASS and RR_TYPE dependent field.  Contains
278  *                    the resource information for the NetBIOS name.
279  *
280  *   RESOURCE RECORD RR_TYPE field definitions:
281  *
282  *   Symbol      Value   Description:
283  *
284  *   A          0x0001   IP address Resource Record (See REDIRECT NAME
285  *                       QUERY RESPONSE)
286  *   NS         0x0002   Name Server Resource Record (See REDIRECT
287  *                       NAME QUERY RESPONSE)
288  *   NULL       0x000A   NULL Resource Record (See WAIT FOR
289  *                       ACKNOWLEDGEMENT RESPONSE)
290  *   NB         0x0020   NetBIOS general Name Service Resource Record
291  *                       (See NB_FLAGS and NB_ADDRESS, below)
292  *   NBSTAT     0x0021   NetBIOS NODE STATUS Resource Record (See NODE
293  *                       STATUS RESPONSE)
294  *
295  *   RESOURCE RECORD RR_CLASS field definitions:
296  *
297  *   Symbol      Value   Description:
298  *
299  *   IN         0x0001   Internet class
300  *
301  *   NB_FLAGS field of the RESOURCE RECORD RDATA field for RR_TYPE of
302  *   "NB":
303  *
304  *                                             1   1   1   1   1   1
305  *     0   1   2   3   4   5   6   7   8   9   0   1   2   3   4   5
306  *   +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
307  *   | G |  ONT  |                RESERVED                           |
308  *   +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
309  *
310  *   Symbol     Bit(s)   Description:
311  *
312  *   RESERVED     3-15   Reserved for future use.  Must be zero (0).
313  *   ONT           1,2   Owner Node Type:
314  *                          00 = B node
315  *                          01 = P node
316  *                          10 = M node
317  *                          11 = Reserved for future use
318  *                       For registration requests this is the
319  *                       claimant's type.
320  *                       For responses this is the actual owner's
321  *                       type.
322  *
323  *   G               0   Group Name Flag.
324  *                       If one (1) then the RR_NAME is a GROUP
325  *                       NetBIOS name.
326  *                       If zero (0) then the RR_NAME is a UNIQUE
327  *                       NetBIOS name.
328  *
329  *   The NB_ADDRESS field of the RESOURCE RECORD RDATA field for
330  *   RR_TYPE of "NB" is the IP address of the name's owner.
331  */
332 
333 /*
334  * 4.2.2. NAME REGISTRATION REQUEST
335  *
336  *                        1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
337  *    0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
338  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
339  *   |         NAME_TRN_ID           |0|  0x5  |0|0|1|0|0 0|B|  0x0  |
340  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
341  *   |          0x0001               |           0x0000              |
342  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
343  *   |          0x0000               |           0x0001              |
344  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
345  *   |                                                               |
346  *   /                         QUESTION_NAME                         /
347  *   /                                                               /
348  *   |                                                               |
349  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
350  *   |           NB (0x0020)         |        IN (0x0001)            |
351  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
352  *   |                                                               |
353  *   /                            RR_NAME                            /
354  *   /                                                               /
355  *   |                                                               |
356  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
357  *   |           NB (0x0020)         |         IN (0x0001)           |
358  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
359  *   |                              TTL                              |
360  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
361  *   |           0x0006              |          NB_FLAGS             |
362  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
363  *   |                          NB_ADDRESS                           |
364  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
365  *
366  *   Since the RR_NAME is the same name as the QUESTION_NAME, the
367  *   RR_NAME representation must use pointers to the QUESTION_NAME
368  *   name's labels to guarantee the length of the datagram is less
369  *   than the maximum 576 bytes.  See section above on name formats
370  *   and also page 31 and 32 of RFC 883, Domain Names - Implementation
371  *   and Specification, for a complete description of compressed name
372  *   label pointers.
373  */
374 
375 /*
376  * 4.2.3 NAME OVERWRITE REQUEST & DEMAND
377  *
378  *                        1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
379  *    0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
380  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
381  *   |         NAME_TRN_ID           |0|  0x5  |0|0|0|0|0 0|B|  0x0  |
382  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
383  *   |          0x0001               |           0x0000              |
384  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
385  *   |          0x0000               |           0x0001              |
386  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
387  *   |                                                               |
388  *   /                         QUESTION_NAME                         /
389  *   /                                                               /
390  *   |                                                               |
391  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
392  *   |           NB (0x0020)         |        IN (0x0001)            |
393  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
394  *   |                                                               |
395  *   /                            RR_NAME                            /
396  *   /                                                               /
397  *   |                                                               |
398  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
399  *   |           NB (0x0020)         |         IN (0x0001)           |
400  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
401  *   |                              TTL                              |
402  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
403  *   |           0x0006              |          NB_FLAGS             |
404  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
405  *   |                          NB_ADDRESS                           |
406  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
407  */
408 
409 /*
410  * 4.2.4 NAME REFRESH REQUEST
411  *
412  *                        1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
413  *    0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
414  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
415  *   |         NAME_TRN_ID           |0|  0x9  |0|0|0|0|0 0|B|  0x0  |
416  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
417  *   |          0x0001               |           0x0000              |
418  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
419  *   |          0x0000               |           0x0001              |
420  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
421  *   |                                                               |
422  *   /                         QUESTION_NAME                         /
423  *   /                                                               /
424  *   |                                                               |
425  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
426  *   |           NB (0x0020)         |        IN (0x0001)            |
427  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
428  *   |                                                               |
429  *   /                            RR_NAME                            /
430  *   /                                                               /
431  *   |                                                               |
432  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
433  *   |           NB (0x0020)         |         IN (0x0001)           |
434  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
435  *   |                              TTL                              |
436  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
437  *   |           0x0006              |          NB_FLAGS             |
438  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
439  *   |                          NB_ADDRESS                           |
440  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
441  */
442 
443 /*
444  * 4.2.5 POSITIVE NAME REGISTRATION RESPONSE
445  *
446  *                        1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
447  *    0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
448  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
449  *   |         NAME_TRN_ID           |1|  0x5  |1|0|1|1|0 0|0|  0x0  |
450  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
451  *   |          0x0000               |           0x0001              |
452  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
453  *   |          0x0000               |           0x0000              |
454  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
455  *   |                                                               |
456  *   /                            RR_NAME                            /
457  *   /                                                               /
458  *   |                                                               |
459  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
460  *   |           NB (0x0020)         |         IN (0x0001)           |
461  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
462  *   |                              TTL                              |
463  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
464  *   |           0x0006              |          NB_FLAGS             |
465  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
466  *   |                          NB_ADDRESS                           |
467  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
468  */
469 
470 /*
471  * 4.2.6 NEGATIVE NAME REGISTRATION RESPONSE
472  *
473  *                        1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
474  *    0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
475  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
476  *   |         NAME_TRN_ID           |1|  0x5  |1|0|1|1|0 0|0| RCODE |
477  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
478  *   |          0x0000               |           0x0001              |
479  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
480  *   |          0x0000               |           0x0000              |
481  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
482  *   |                                                               |
483  *   /                            RR_NAME                            /
484  *   /                                                               /
485  *   |                                                               |
486  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
487  *   |           NB (0x0020)         |         IN (0x0001)           |
488  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
489  *   |                              TTL                              |
490  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
491  *   |           0x0006              |          NB_FLAGS             |
492  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
493  *   |                          NB_ADDRESS                           |
494  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
495  *
496  *   RCODE field values:
497  *
498  *   Symbol      Value   Description:
499  *
500  *   FMT_ERR       0x1   Format Error.  Request was invalidly
501  *                       formatted.
502  *   SRV_ERR       0x2   Server failure.  Problem with NBNS, cannot
503  *                       process name.
504  *   IMP_ERR       0x4   Unsupported request error.  Allowable only
505  *                       for challenging NBNS when gets an Update type
506  *                       registration request.
507  *   RFS_ERR       0x5   Refused error.  For policy reasons server
508  *                       will not register this name from this host.
509  *   ACT_ERR       0x6   Active error.  Name is owned by another node.
510  *   CFT_ERR       0x7   Name in conflict error.  A UNIQUE name is
511  *                       owned by more than one node.
512  */
513 
514 /*
515  * 4.2.7 END-NODE CHALLENGE REGISTRATION RESPONSE
516  *
517  *                        1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
518  *    0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
519  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
520  *   |         NAME_TRN_ID           |1|  0x5  |1|0|1|0|0 0|0|  0x0  |
521  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
522  *   |          0x0000               |           0x0001              |
523  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
524  *   |          0x0000               |           0x0000              |
525  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
526  *   |                                                               |
527  *   /                            RR_NAME                            /
528  *   /                                                               /
529  *   |                                                               |
530  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
531  *   |           NB (0x0020)         |         IN (0x0001)           |
532  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
533  *   |                              TTL                              |
534  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
535  *   |           0x0006              |          NB_FLAGS             |
536  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
537  *   |                          NB_ADDRESS                           |
538  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
539  */
540 
541 /*
542  * 4.2.8 NAME CONFLICT DEMAND
543  *
544  *                        1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
545  *    0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
546  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
547  *   |         NAME_TRN_ID           |1|  0x5  |1|0|1|1|0 0|0|  0x7  |
548  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
549  *   |          0x0000               |           0x0001              |
550  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
551  *   |          0x0000               |           0x0000              |
552  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
553  *   |                                                               |
554  *   /                            RR_NAME                            /
555  *   /                                                               /
556  *   |                                                               |
557  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
558  *   |           NB (0x0020)         |         IN (0x0001)           |
559  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
560  *   |                          0x00000000                           |
561  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
562  *   |           0x0006              |0|ONT|0|     0x000             |
563  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
564  *   |                          0x00000000                           |
565  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
566  *
567  *   This packet is identical to a NEGATIVE NAME REGISTRATION RESPONSE
568  *   with RCODE = CFT_ERR.
569  */
570 
571 /*
572  * 4.2.9 NAME RELEASE REQUEST & DEMAND
573  *
574  *                        1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
575  *    0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
576  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
577  *   |         NAME_TRN_ID           |0|  0x6  |0|0|0|0|0 0|B|  0x0  |
578  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
579  *   |          0x0001               |           0x0000              |
580  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
581  *   |          0x0000               |           0x0001              |
582  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
583  *   |                                                               |
584  *   /                         QUESTION_NAME                         /
585  *   /                                                               /
586  *   |                                                               |
587  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
588  *   |           NB (0x0020)         |        IN (0x0001)            |
589  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
590  *   |                                                               |
591  *   /                            RR_NAME                            /
592  *   /                                                               /
593  *   |                                                               |
594  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
595  *   |           NB (0x0020)         |         IN (0x0001)           |
596  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
597  *   |                          0x00000000                           |
598  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
599  *   |           0x0006              |          NB_FLAGS             |
600  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
601  *   |                          NB_ADDRESS                           |
602  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
603  *
604  *   Since the RR_NAME is the same name as the QUESTION_NAME, the
605  *   RR_NAME representation must use label string pointers to the
606  *   QUESTION_NAME labels to guarantee the length of the datagram is
607  *   less than the maximum 576 bytes.  This is the same condition as
608  *   with the NAME REGISTRATION REQUEST.
609  */
610 
611 /*
612  * 4.2.10 POSITIVE NAME RELEASE RESPONSE
613  *
614  *                        1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
615  *    0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
616  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
617  *   |         NAME_TRN_ID           |1|  0x6  |1|0|0|0|0 0|0|  0x0  |
618  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
619  *   |          0x0000               |           0x0001              |
620  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
621  *   |          0x0000               |           0x0000              |
622  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
623  *   |                                                               |
624  *   /                            RR_NAME                            /
625  *   /                                                               /
626  *   |                                                               |
627  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
628  *   |           NB (0x0020)         |         IN (0x0001)           |
629  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
630  *   |                              TTL                              |
631  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
632  *   |           0x0006              |          NB_FLAGS             |
633  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
634  *   |                          NB_ADDRESS                           |
635  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
636  */
637 
638 /*
639  * 4.2.11 NEGATIVE NAME RELEASE RESPONSE
640  *
641  *                        1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
642  *    0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
643  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
644  *   |         NAME_TRN_ID           |1|  0x6  |1|0|0|0|0 0|0| RCODE |
645  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
646  *   |          0x0000               |           0x0001              |
647  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
648  *   |          0x0000               |           0x0000              |
649  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
650  *   |                                                               |
651  *   /                            RR_NAME                            /
652  *   /                                                               /
653  *   |                                                               |
654  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
655  *   |           NB (0x0020)         |         IN (0x0001)           |
656  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
657  *   |                              TTL                              |
658  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
659  *   |           0x0006              |          NB_FLAGS             |
660  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
661  *   |                          NB_ADDRESS                           |
662  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
663  *
664  *   RCODE field values:
665  *
666  *   Symbol      Value   Description:
667  *
668  *   FMT_ERR       0x1   Format Error.  Request was invalidly
669  *                       formatted.
670  *
671  *   SRV_ERR       0x2   Server failure.  Problem with NBNS, cannot
672  *                       process name.
673  *
674  *   RFS_ERR       0x5   Refused error.  For policy reasons server
675  *                       will not release this name from this host.
676  *
677  *   ACT_ERR       0x6   Active error.  Name is owned by another node.
678  *                       Only that node may release it.  A NetBIOS
679  *                       Name Server can optionally allow a node to
680  *                       release a name it does not own.  This would
681  *                       facilitate detection of inactive names for
682  *                       nodes that went down silently.
683  */
684 
685 /*
686  * 4.2.12 NAME QUERY REQUEST
687  *
688  *                        1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
689  *    0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
690  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
691  *   |         NAME_TRN_ID           |0|  0x0  |0|0|1|0|0 0|B|  0x0  |
692  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
693  *   |          0x0001               |           0x0000              |
694  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
695  *   |          0x0000               |           0x0000              |
696  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
697  *   |                                                               |
698  *   /                         QUESTION_NAME                         /
699  *   /                                                               /
700  *   |                                                               |
701  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
702  *   |           NB (0x0020)         |        IN (0x0001)            |
703  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
704  */
705 
706 /*
707  * 4.2.13 POSITIVE NAME QUERY RESPONSE
708  *
709  *                        1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
710  *    0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
711  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
712  *   |         NAME_TRN_ID           |1|  0x0  |1|T|1|?|0 0|0|  0x0  |
713  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
714  *   |          0x0000               |           0x0001              |
715  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
716  *   |          0x0000               |           0x0000              |
717  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
718  *   |                                                               |
719  *   /                            RR_NAME                            /
720  *   /                                                               /
721  *   |                                                               |
722  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
723  *   |           NB (0x0020)         |         IN (0x0001)           |
724  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
725  *   |                              TTL                              |
726  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
727  *   |           RDLENGTH            |                               |
728  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+                               |
729  *   |                                                               |
730  *   /                       ADDR_ENTRY ARRAY                        /
731  *   /                                                               /
732  *   |                                                               |
733  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
734  *
735  *   The ADDR_ENTRY ARRAY a sequence of zero or more ADDR_ENTRY
736  *   records.  Each ADDR_ENTRY record represents an owner of a name.
737  *   For group names there may be multiple entries.  However, the list
738  *   may be incomplete due to packet size limitations.  Bit 22, "T",
739  *   will be set to indicate truncated data.
740  *
741  *   Each ADDR_ENTRY has the following format:
742  *
743  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
744  *   |          NB_FLAGS             |          NB_ADDRESS           |
745  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
746  *   |   NB_ADDRESS (continued)      |
747  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
748  */
749 
750 /*
751  * 4.2.14 NEGATIVE NAME QUERY RESPONSE
752  *
753  *                        1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
754  *    0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
755  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
756  *   |         NAME_TRN_ID           |1|  0x0  |1|0|1|?|0 0|0| RCODE |
757  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
758  *   |          0x0000               |           0x0000              |
759  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
760  *   |          0x0000               |           0x0000              |
761  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
762  *   |                                                               |
763  *   /                            RR_NAME                            /
764  *   /                                                               /
765  *   |                                                               |
766  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
767  *   |           NULL (0x000A)       |         IN (0x0001)           |
768  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
769  *   |                          0x00000000                           |
770  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
771  *   |           0x0000              |
772  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
773  *
774  *   RCODE field values:
775  *
776  *   Symbol      Value   Description
777  *
778  *   FMT_ERR       0x1   Format Error.  Request was invalidly
779  *                       formatted.
780  *   SRV_ERR       0x2   Server failure.  Problem with NBNS, cannot
781  *                       process name.
782  *   NAM_ERR       0x3   Name Error.  The name requested does not
783  *                       exist.
784  *   IMP_ERR       0x4   Unsupported request error.  Allowable only
785  *                       for challenging NBNS when gets an Update type
786  *                       registration request.
787  *   RFS_ERR       0x5   Refused error.  For policy reasons server
788  *                       will not register this name from this host.
789  */
790 
791 /*
792  * 4.2.15 REDIRECT NAME QUERY RESPONSE
793  *
794  *                        1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
795  *    0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
796  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
797  *   |         NAME_TRN_ID           |1|  0x0  |0|0|1|0|0 0|0|  0x0  |
798  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
799  *   |          0x0000               |           0x0000              |
800  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
801  *   |          0x0001               |           0x0001              |
802  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
803  *   |                                                               |
804  *   /                            RR_NAME                            /
805  *   /                                                               /
806  *   |                                                               |
807  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
808  *   |           NS (0x0002)         |         IN (0x0001)           |
809  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
810  *   |                              TTL                              |
811  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
812  *   |           RDLENGTH            |                               |
813  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+                               +
814  *   |                                                               |
815  *   /                            NSD_NAME                           /
816  *   /                                                               /
817  *   |                                                               |
818  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
819  *   |                                                               |
820  *   /                            RR_NAME                            /
821  *   /                                                               /
822  *   |                                                               |
823  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
824  *   |           A (0x0001)          |         IN (0x0001)           |
825  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
826  *   |                              TTL                              |
827  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
828  *   |             0x0004            |           NSD_IP_ADDR         |
829  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
830  *   |     NSD_IP_ADDR, continued    |
831  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
832  *
833  *   An end node responding to a NAME QUERY REQUEST always responds
834  *   with the AA and RA bits set for both the NEGATIVE and POSITIVE
835  *   NAME QUERY RESPONSE packets.  An end node never sends a REDIRECT
836  *   NAME QUERY RESPONSE packet.
837  *
838  *   When the requestor receives the REDIRECT NAME QUERY RESPONSE it
839  *   must reiterate the NAME QUERY REQUEST to the NBNS specified by
840  *   the NSD_IP_ADDR field of the A type RESOURCE RECORD in the
841  *   ADDITIONAL section of the response packet.  This is an optional
842  *   packet for the NBNS.
843  *
844  *   The NSD_NAME and the RR_NAME in the ADDITIONAL section of the
845  *   response packet are the same name.  Space can be optimized if
846  *   label string pointers are used in the RR_NAME which point to the
847  *   labels in the NSD_NAME.
848  *
849  *   The RR_NAME in the AUTHORITY section is the name of the domain
850  *   the NBNS called by NSD_NAME has authority over.
851  */
852 
853 /*
854  * 4.2.16 WAIT FOR ACKNOWLEDGEMENT (WACK) RESPONSE
855  *
856  *                        1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
857  *    0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
858  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
859  *   |         NAME_TRN_ID           |1|  0x7  |1|0|0|0|0 0|0|  0x0  |
860  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
861  *   |          0x0000               |           0x0001              |
862  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
863  *   |          0x0000               |           0x0000              |
864  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
865  *   |                                                               |
866  *   /                            RR_NAME                            /
867  *   /                                                               /
868  *   |                                                               |
869  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
870  *   |          NULL (0x0020)        |         IN (0x0001)           |
871  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
872  *   |                              TTL                              |
873  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
874  *   |           0x0002              | OPCODE  |   NM_FLAGS  |  0x0  |
875  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
876  *
877  *   The NAME_TRN_ID of the WACK RESPONSE packet is the same
878  *   NAME_TRN_ID of the request that the NBNS is telling the requestor
879  *   to wait longer to complete.  The RR_NAME is the name from the
880  *   request, if any.  If no name is available from the request then
881  *   it is a null name, single byte of zero.
882  *
883  *   The TTL field of the ResourceRecord is the new time to wait, in
884  *   seconds, for the request to complete.  The RDATA field contains
885  *   the OPCODE and NM_FLAGS of the request.
886  *
887  *   A TTL value of 0 means that the NBNS can not estimate the time it
888  *   may take to complete a response.
889  */
890 
891 /*
892  * 4.2.17 NODE STATUS REQUEST
893  *
894  *                        1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
895  *    0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
896  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
897  *   |         NAME_TRN_ID           |0|  0x0  |0|0|0|0|0 0|B|  0x0  |
898  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
899  *   |          0x0001               |           0x0000              |
900  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
901  *   |          0x0000               |           0x0000              |
902  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
903  *   |                                                               |
904  *   /                         QUESTION_NAME                         /
905  *   |                                                               |
906  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
907  *   |         NBSTAT (0x0021)       |        IN (0x0001)            |
908  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
909  */
910 
911 /*
912  * 4.2.18 NODE STATUS RESPONSE
913  *
914  *                        1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
915  *    0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
916  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
917  *   |         NAME_TRN_ID           |1|  0x0  |1|0|0|0|0 0|0|  0x0  |
918  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
919  *   |          0x0000               |           0x0001              |
920  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
921  *   |          0x0000               |           0x0000              |
922  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
923  *   |                                                               |
924  *   /                            RR_NAME                            /
925  *   |                                                               |
926  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
927  *   |        NBSTAT (0x0021)        |         IN (0x0001)           |
928  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
929  *   |                          0x00000000                           |
930  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
931  *   |          RDLENGTH             |   NUM_NAMES   |               |
932  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+               +
933  *   |                                                               |
934  *   +                                                               +
935  *   /                         NODE_NAME ARRAY                       /
936  *   +                                                               +
937  *   |                                                               |
938  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
939  *   |                                                               |
940  *   +                                                               +
941  *   /                           STATISTICS                          /
942  *   +                                                               +
943  *   |                                                               |
944  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
945  *
946  *   The NODE_NAME ARRAY is an array of zero or more NUM_NAMES entries
947  *   of NODE_NAME records.  Each NODE_NAME entry represents an active
948  *   name in the same NetBIOS scope as the requesting name in the
949  *   local name table of the responder.  RR_NAME is the requesting
950  *   name.
951  */
952 
953 #include <stdio.h>
954 #include <synch.h>
955 #include <pthread.h>
956 #include <strings.h>
957 #include <netinet/in.h>
958 
959 #include <smbsrv/libsmbns.h>
960 
961 #include <smbsrv/smbinfo.h>
962 #include <smbsrv/netbios.h>
963 
964 #define	QUEUE_INSERT_TAIL(q, e) \
965 	((e)->back) = (void *)((q)->back);	\
966 	((e)->forw) = (void *)(q);		    \
967 	((q)->back->forw) = (void *)(e);	\
968 	((q)->back) = (void *)(e);
969 
970 #define	QUEUE_CLIP(e) \
971 	(e)->forw->back = (e)->back;	\
972 	(e)->back->forw = (e)->forw;	\
973 	(e)->forw = 0;					\
974 	(e)->back = 0;
975 
976 #define	NETBIOS_NAME_SVC_LAUNCHED	0x00001
977 #define	NETBIOS_NAME_SVC_RUNNING	0x00002
978 #define	NETBIOS_NAME_SVC_FAILED		0x00004
979 
980 #define	NETBIOS_DATAGRAM_SVC_LAUNCHED	0x00010
981 #define	NETBIOS_DATAGRAM_SVC_RUNNING	0x00020
982 #define	NETBIOS_DATAGRAM_SVC_FAILED	0x00040
983 
984 #define	NETBIOS_TIMER_LAUNCHED		0x00100
985 #define	NETBIOS_TIMER_RUNNING		0x00200
986 #define	NETBIOS_TIMER_FAILED		0x00400
987 
988 #define	NETBIOS_BROWSER_LAUNCHED	0x01000
989 #define	NETBIOS_BROWSER_RUNNING		0x02000
990 #define	NETBIOS_BROWSER_FAILED		0x04000
991 
992 #define	NETBIOS_SHUTTING_DOWN		0x10000
993 #define	NETBIOS_SHUT_DOWN		0x20000
994 
995 char smb_node_type;
996 
997 typedef struct {
998 	mutex_t mtx;
999 	cond_t cv;
1000 	uint32_t state;
1001 } netbios_status_t;
1002 extern netbios_status_t nb_status;
1003 
1004 /*
1005  * NAME service definitions
1006  */
1007 #define	ADDR_FLAG_INVALID		0x0000
1008 #define	ADDR_FLAG_VALID		0x0001
1009 
1010 typedef struct addr_entry {
1011 	struct addr_entry 	*forw;
1012 	struct addr_entry 	*back;
1013 	uint32_t		attributes;
1014 	uint32_t		conflict_timer;
1015 	uint32_t		refresh_ttl;
1016 	uint32_t		ttl;
1017 	struct sockaddr_in	sin;
1018 	int			sinlen;
1019 	uint32_t 		flags;
1020 } addr_entry_t;
1021 
1022 /*
1023  *   The NODE_NAME ARRAY is an array of zero or more NUM_NAMES entries
1024  *   of NODE_NAME records.  Each NODE_NAME entry represents an active
1025  *   name in the same NetBIOS scope as the requesting name in the
1026  *   local name table of the responder.  RR_NAME is the requesting
1027  *   name.
1028  *
1029  *   NODE_NAME Entry:
1030  *
1031  *                        1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
1032  *    0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
1033  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1034  *   |                                                               |
1035  *   +---                                                         ---+
1036  *   |                                                               |
1037  *   +---                    NETBIOS FORMAT NAME                  ---+
1038  *   |                                                               |
1039  *   +---                                                         ---+
1040  *   |                                                               |
1041  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1042  *   |         NAME_FLAGS            |
1043  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1044  *
1045  *   The NAME_FLAGS field:
1046  *
1047  *                                             1   1   1   1   1   1
1048  *     0   1   2   3   4   5   6   7   8   9   0   1   2   3   4   5
1049  *   +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
1050  *   | G |  ONT  |DRG|CNF|ACT|PRM|          RESERVED                 |
1051  *   +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
1052  *
1053  *   The NAME_FLAGS field is defined as:
1054  *
1055  *   Symbol     Bit(s)   Description:
1056  *
1057  *   RESERVED     7-15   Reserved for future use.  Must be zero (0).
1058  *   PRM             6   Permanent Name Flag.  If one (1) then entry
1059  *                       is for the permanent node name.  Flag is zero
1060  *                       (0) for all other names.
1061  *   ACT             5   Active Name Flag.  All entries have this flag
1062  *                       set to one (1).
1063  *   CNF             4   Conflict Flag.  If one (1) then name on this
1064  *                       node is in conflict.
1065  *   DRG             3   Deregister Flag.  If one (1) then this name
1066  *                       is in the process of being deleted.
1067  *   ONT           1,2   Owner Node Type:
1068  *                          00 = B node
1069  *                          01 = P node
1070  *                          10 = M node
1071  *                          11 = Reserved for future use
1072  *   G               0   Group Name Flag.
1073  *                       name.
1074  *                       If zero (0) then it is a UNIQUE NetBIOS name.
1075  */
1076 
1077 typedef struct name_entry {
1078 	struct name_entry 	*forw;
1079 	struct name_entry 	*back;
1080 	unsigned char		name[NETBIOS_NAME_SZ];
1081 	unsigned char		scope[NETBIOS_DOMAIN_NAME_MAX];
1082 	unsigned short		attributes;
1083 	struct addr_entry	addr_list;
1084 	mutex_t			mtx;
1085 } name_entry;
1086 
1087 struct name_question {
1088 	struct name_entry 	*name;
1089 	unsigned		question_type;
1090 	unsigned		question_class;
1091 };
1092 
1093 struct resource_record {
1094 	/*
1095 	 * These two flags and address are contained within RDATA
1096 	 * when rr_type==0x0020 (NB - NetBIOS general Name Service)
1097 	 * and rr_class==0x01 (IN - Internet Class).
1098 	 */
1099 
1100 	struct name_entry *name;
1101 	unsigned short rr_type;
1102 	unsigned short rr_class;
1103 	uint32_t ttl;
1104 	unsigned short rdlength;
1105 	unsigned char *rdata;
1106 };
1107 
1108 struct name_packet {
1109 	unsigned short		name_trn_id;
1110 	unsigned short		info;
1111 
1112 	unsigned		qdcount;	/* question entries */
1113 	unsigned		ancount;	/* answer recs */
1114 	unsigned		nscount;	/* authority recs */
1115 	unsigned		arcount;	/* additional recs */
1116 
1117 	struct name_question 	*question;
1118 	struct resource_record 	*answer;
1119 	struct resource_record 	*authority;
1120 	struct resource_record 	*additional;
1121 
1122 	unsigned char			block_data[4];	/* begining of space */
1123 };
1124 
1125 #define	NAME_OPCODE_R		0x8000	/* RESPONSE flag: 1 bit */
1126 #define	NAME_OPCODE_OPCODE_MASK	0x7800	/* OPCODE Field: 4 bits */
1127 #define	NAME_OPCODE_QUERY	0x0000
1128 #define	NAME_OPCODE_REGISTRATION	0x2800
1129 #define	NAME_OPCODE_RELEASE	0x3000
1130 #define	NAME_OPCODE_WACK	0x3800
1131 #define	NAME_OPCODE_REFRESH	0x4000
1132 #define	NAME_OPCODE_MULTIHOME	0x7800
1133 #define	NAME_NM_FLAGS_AA	0x0400	/* Authoritative Answer:1 bit */
1134 #define	NAME_NM_FLAGS_TC	0x0200	/* Truncation:		1 bit */
1135 #define	NAME_NM_FLAGS_RD	0x0100	/* Recursion desired:	1 bit */
1136 #define	NAME_NM_FLAGS_RA	0x0080	/* Recursion available:	1 bit */
1137 #define	NAME_NM_FLAGS_x2	0x0040	/* reserved, mbz:	1 bit */
1138 #define	NAME_NM_FLAGS_x1	0x0020	/* reserved, mbz:	1 bit */
1139 #define	NAME_NM_FLAGS_B		0x0010	/* Broadcast:		1 bit */
1140 #define	NAME_RCODE_MASK		0x000f	/* RCODE Field:		4 bits */
1141 #define	RCODE_FMT_ERR		0x0001
1142 #define	RCODE_SRV_ERR		0x0002
1143 #define	RCODE_NAM_ERR		0x0003
1144 #define	RCODE_IMP_ERR		0x0004
1145 #define	RCODE_RFS_ERR		0x0005
1146 #define	RCODE_ACT_ERR		0x0006
1147 #define	RCODE_CFT_ERR		0x0007
1148 
1149 #define	NM_FLAGS_UNICAST		0
1150 #define	NM_FLAGS_BROADCAST		NAME_NM_FLAGS_B
1151 
1152 #define	PACKET_TYPE(x)	((x) & (NAME_OPCODE_R | NAME_OPCODE_OPCODE_MASK | \
1153 			NAME_NM_FLAGS_AA | NAME_NM_FLAGS_RD))
1154 
1155 #define	RCODE(x)		((x) & NAME_RCODE_MASK)
1156 #define	POSITIVE_RESPONSE(x)	(RCODE(x) == 0)
1157 #define	NEGATIVE_RESPONSE(x)	(RCODE(x) != 0)
1158 
1159 #define	END_NODE_CHALLENGE_REGISTRATION_REQUEST				\
1160 	    (NAME_OPCODE_REGISTRATION | NAME_NM_FLAGS_AA | NAME_NM_FLAGS_RD)
1161 #define	END_NODE_CHALLENGE_NAME_REGISTRATION_RESPONSE			\
1162 	    (NAME_OPCODE_R | END_NODE_CHALLENGE_REGISTRATION_REQUEST)
1163 
1164 #define	NAME_QUERY_REQUEST						\
1165 	    (NAME_OPCODE_QUERY | NAME_NM_FLAGS_RD)
1166 #define	NAME_QUERY_RESPONSE						\
1167 	    (NAME_OPCODE_R | NAME_QUERY_REQUEST |			\
1168 	    NAME_NM_FLAGS_AA | NAME_NM_FLAGS_RD)
1169 
1170 #define	NODE_STATUS_REQUEST						\
1171 	    (NAME_OPCODE_QUERY)
1172 #define	NODE_STATUS_RESPONSE						\
1173 	    (NAME_OPCODE_R | NODE_STATUS_REQUEST | NAME_NM_FLAGS_AA)
1174 
1175 #define	REDIRECT_NAME_QUERY_RESPONSE					\
1176 	    (NAME_OPCODE_R | NAME_QUERY_REQUEST | NAME_NM_FLAGS_RD)
1177 
1178 #define	NAME_REFRESH_REQUEST						\
1179 	    (NAME_OPCODE_REFRESH)
1180 #define	NAME_REGISTRATION_REQUEST					\
1181 	    (NAME_OPCODE_REGISTRATION | NAME_NM_FLAGS_RD)
1182 #define	NAME_MULTIHOME_REGISTRATION_REQUEST				\
1183 	    (NAME_OPCODE_MULTIHOME | NAME_NM_FLAGS_RD)
1184 #define	NAME_REGISTRATION_RESPONSE					\
1185 	    (NAME_OPCODE_R | NAME_REGISTRATION_REQUEST | NAME_NM_FLAGS_AA)
1186 
1187 #define	NAME_RELEASE_REQUEST						\
1188 	    (NAME_OPCODE_RELEASE)
1189 #define	NAME_RELEASE_RESPONSE						\
1190 	    (NAME_OPCODE_R | NAME_RELEASE_REQUEST | NAME_NM_FLAGS_AA)
1191 
1192 #define	WACK_RESPONSE						\
1193 	    (NAME_OPCODE_R | NAME_OPCODE_WACK | NAME_NM_FLAGS_AA)
1194 
1195 #define	NAME_QUESTION_TYPE_NB		0x0020
1196 #define	NAME_QUESTION_TYPE_NBSTAT	0x0021
1197 #define	NAME_QUESTION_CLASS_IN		0x0001
1198 
1199 
1200 #define	NAME_RR_TYPE_A			0x0001	/* IP Address */
1201 #define	NAME_RR_TYPE_NS			0x0002	/* Name Server */
1202 #define	NAME_RR_TYPE_NULL		0x000A	/* NULL */
1203 #define	NAME_RR_TYPE_NB			0x0020	/* NetBIOS Name Service */
1204 #define	NAME_RR_TYPE_NBSTAT		0x0021	/* NetBIOS Node Status */
1205 
1206 #define	NAME_RR_CLASS_IN		0x0001	/* NetBIOS Node Status */
1207 
1208 #define	NAME_NB_FLAGS_ONT_MASK		(3<<13)
1209 #define	NAME_NB_FLAGS_ONT_B		(0<<13) /* B-node (broadcast) */
1210 #define	NAME_NB_FLAGS_ONT_P		(1<<13)	/* P-node (point-to-point) */
1211 #define	NAME_NB_FLAGS_ONT_M		(2<<13)	/* M-node (multicast) */
1212 #define	NAME_NB_FLAGS_ONT_resv		(3<<13)
1213 #define	NAME_NB_FLAGS_G			(1<<15)	/* Group Name */
1214 
1215 #define	UNICAST				0
1216 #define	BROADCAST			1
1217 #define	POINTCAST			2
1218 
1219 #define	NAME_ATTR_UNIQUE		0x0000
1220 #define	NAME_ATTR_GROUP			0x8000
1221 #define	NAME_ATTR_OWNER_NODE_TYPE	0x6000
1222 #define	  NAME_ATTR_OWNER_TYPE_BNODE	  0x0000
1223 #define	  NAME_ATTR_OWNER_TYPE_PNODE	  0x2000
1224 #define	  NAME_ATTR_OWNER_TYPE_MNODE	  0x4000
1225 #define	  NAME_ATTR_OWNER_TYPE_HNODE	  0x6000
1226 #define	NAME_ATTR_DEREGISTER		0x1000
1227 #define	NAME_ATTR_CONFLICT		0x0800
1228 #define	NAME_ATTR_ACTIVE_NAME		0x0400
1229 #define	NAME_ATTR_PERMANENT		0x0200
1230 #define	NAME_ATTR_RESERVED		0x01FF
1231 #define	NAME_ATTR_LOCAL			0x0001
1232 
1233 #define	NODE_TYPE(x)		((x) & NAME_ATTR_OWNER_NODE_TYPE))
1234 #define	IS_BNODE(x)		(NODE_TYPE(x) == NAME_ATTR_OWNER_TYPE_BNODE)
1235 #define	IS_PNODE(x)		(NODE_TYPE(x) == NAME_ATTR_OWNER_TYPE_PNODE)
1236 #define	IS_MNODE(x)		(NODE_TYPE(x) == NAME_ATTR_OWNER_TYPE_MNODE)
1237 #define	IS_HNODE(x)		(NODE_TYPE(x) == NAME_ATTR_OWNER_TYPE_HNODE)
1238 
1239 #define	IS_UNIQUE(x)		(((x) & NAME_ATTR_GROUP) == 0)
1240 #define	IS_GROUP(x)		(((x) & NAME_ATTR_GROUP) != 0)
1241 #define	IS_PERMANENT(x)		(((x) & NAME_ATTR_PERMANENT) != 0)
1242 #define	IS_CONFLICTING(x)	(((x) & NAME_ATTR_CONFLICT) != 0)
1243 #define	IS_ACTIVE(x)		(((x) & NAME_ATTR_ACTIVE) != 0)
1244 #define	IS_DEGREGISTERED(x)	(((x) & NAME_ATTR_ACTIVE) != 0)
1245 
1246 #define	IS_LOCAL(x)		(((x) & NAME_ATTR_LOCAL) != 0)
1247 #define	IS_PUBLIC(x)		(((x) & NAME_ATTR_LOCAL) == 0)
1248 #define	PUBLIC_BITS(x)		((x) & ~NAME_ATTR_RESERVED)
1249 
1250 #define	SAME_SCOPE(scope, e)	(strcmp((scope), ((e)->scope)) == 0)
1251 
1252 /*
1253  *   STATISTICS Field of the NODE STATUS RESPONSE:
1254  *
1255  *                        1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
1256  *    0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
1257  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1258  *   |               UNIT_ID (Unique unit ID)                        |
1259  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1260  *   |       UNIT_ID,continued       |    JUMPERS    |  TEST_RESULT  |
1261  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1262  *   |       VERSION_NUMBER          |      PERIOD_OF_STATISTICS     |
1263  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1264  *   |       NUMBER_OF_CRCs          |     NUMBER_ALIGNMENT_ERRORS   |
1265  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1266  *   |       NUMBER_OF_COLLISIONS    |        NUMBER_SEND_ABORTS     |
1267  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1268  *   |                       NUMBER_GOOD_SENDS                       |
1269  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1270  *   |                      NUMBER_GOOD_RECEIVES                     |
1271  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1272  *   |       NUMBER_RETRANSMITS      | NUMBER_NO_RESOURCE_CONDITIONS |
1273  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1274  *   |  NUMBER_FREE_COMMAND_BLOCKS   |  TOTAL_NUMBER_COMMAND_BLOCKS  |
1275  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1276  *   |MAX_TOTAL_NUMBER_COMMAND_BLOCKS|    NUMBER_PENDING_SESSIONS    |
1277  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1278  *   |  MAX_NUMBER_PENDING_SESSIONS  |  MAX_TOTAL_SESSIONS_POSSIBLE  |
1279  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1280  *   |   SESSION_DATA_PACKET_SIZE    |
1281  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1282  */
1283 
1284 typedef struct {
1285 	unsigned char	unit_id[6];
1286 	unsigned char	jumpers;
1287 	unsigned char	test_result;
1288 	unsigned short	version_number;
1289 	unsigned short	statistical_period;
1290 	unsigned short	crc_errors;
1291 	unsigned short	alignment_errors;
1292 	unsigned short	collisions;
1293 	unsigned short	send_aborts;
1294 	unsigned int	good_sends;
1295 	unsigned int	good_receives;
1296 	unsigned short	retransmits;
1297 	unsigned short	no_resource_conditions;
1298 	unsigned short	free_command_blocks;
1299 	unsigned short	total_command_blocks;
1300 	unsigned short	max_total_command_blocks;
1301 	unsigned short	pending_sessions;
1302 	unsigned short	max_pending_sessions;
1303 	unsigned short	total_possible_sessions;
1304 	unsigned short	session_data_packet_size;
1305 } node_status_response;
1306 
1307 /*
1308  * 4.4.1.  NetBIOS DATAGRAM HEADER
1309  *
1310  *                         1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
1311  *     0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
1312  *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1313  *    |   MSG_TYPE    |     FLAGS     |           DGM_ID              |
1314  *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1315  *    |                           SOURCE_IP                           |
1316  *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1317  *    |          SOURCE_PORT          |          DGM_LENGTH           |
1318  *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1319  *    |         PACKET_OFFSET         |
1320  *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1321  */
1322 typedef struct {
1323 	unsigned char	msg_type;
1324 	unsigned char	flags;
1325 	unsigned short	dgm_id;
1326 	uint32_t	source_ip;
1327 	unsigned short	source_port;
1328 	unsigned short	dgm_length;
1329 	unsigned short	packet_offset;
1330 } datagram_header;
1331 
1332 /*
1333  *    MSG_TYPE values (in hexidecimal):
1334  *
1335  *            10 -  DIRECT_UNIQUE DATAGRAM
1336  *            11 -  DIRECT_GROUP DATAGRAM
1337  *            12 -  BROADCAST DATAGRAM
1338  *            13 -  DATAGRAM ERROR
1339  *            14 -  DATAGRAM QUERY REQUEST
1340  *            15 -  DATAGRAM POSITIVE QUERY RESPONSE
1341  *            16 -  DATAGRAM NEGATIVE QUERY RESPONSE
1342  */
1343 #define	DATAGRAM_TYPE_DIRECT_UNIQUE	0x10
1344 #define	DATAGRAM_TYPE_DIRECT_GROUP	0x11
1345 #define	DATAGRAM_TYPE_BROADCAST		0x12
1346 #define	DATAGRAM_TYPE_ERROR_DATAGRAM	0x13
1347 #define	DATAGRAM_TYPE_QUERY_REQUEST	0x14
1348 #define	DATAGRAM_TYPE_POSITIVE_RESPONSE	0x15
1349 #define	DATAGRAM_TYPE_NEGATIVE_RESPONSE	0x16
1350 
1351 
1352 /*
1353  *    Bit definitions of the FLAGS field:
1354  *
1355  *      0   1   2   3   4   5   6   7
1356  *    +---+---+---+---+---+---+---+---+
1357  *    | 0 | 0 | 0 | 0 |  SNT  | F | M |
1358  *    +---+---+---+---+---+---+---+---+
1359  *
1360  *    Symbol     Bit(s)   Description
1361  *
1362  *    M               7   MORE flag, If set then more NetBIOS datagram
1363  *                        fragments follow.
1364  *
1365  *    F               6   FIRST packet flag,  If set then this is first
1366  *                        (and possibly only) fragment of NetBIOS
1367  *                        datagram
1368  *
1369  *    SNT           4,5   Source End-Node type:
1370  *                           00 = B node
1371  *                           01 = P node
1372  *                           10 = M node
1373  *                           11 = H node
1374  *    RESERVED      0-3   Reserved, must be zero (0)
1375  */
1376 #define	DATAGRAM_FLAGS_MORE	0x01
1377 #define	DATAGRAM_FLAGS_FIRST	0x02
1378 #define	DATAGRAM_FLAGS_SRC_TYPE	0x0c
1379 #define	DATAGRAM_FLAGS_B_NODE	  0x00
1380 #define	DATAGRAM_FLAGS_P_NODE	  0x04
1381 #define	DATAGRAM_FLAGS_M_NODE	  0x08
1382 #define	DATAGRAM_FLAGS_H_NODE	  0x0C
1383 #define	DATAGRAM_FLAGS_NBDD	  0x0c
1384 #define	DATAGRAM_FLAGS_RESERVED	0xf0
1385 
1386 /*
1387  * 4.4.2.  DIRECT_UNIQUE, DIRECT_GROUP, & BROADCAST DATAGRAM
1388  *
1389  *                         1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
1390  *     0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
1391  *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1392  *    |   MSG_TYPE    |     FLAGS     |           DGM_ID              |
1393  *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1394  *    |                           SOURCE_IP                           |
1395  *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1396  *    |          SOURCE_PORT          |          DGM_LENGTH           |
1397  *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1398  *    |         PACKET_OFFSET         |                               |
1399  *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+                               |
1400  *    |                                                               |
1401  *    /                          SOURCE_NAME                          /
1402  *    /                                                               /
1403  *    |                                                               |
1404  *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1405  *    |                                                               |
1406  *    /                       DESTINATION_NAME                        /
1407  *    /                                                               /
1408  *    |                                                               |
1409  *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1410  *    |                                                               |
1411  *    /                           USER_DATA                           /
1412  *    /                                                               /
1413  *    |                                                               |
1414  *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1415  */
1416 typedef struct {
1417 	datagram_header	header;
1418 	unsigned char	*source_name;
1419 	unsigned char	*destination_name;
1420 	unsigned char	*user_data;
1421 } datagram_packet;
1422 
1423 
1424 /*
1425  *    4.4.3.  DATAGRAM ERROR PACKET
1426  *
1427  *                         1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
1428  *     0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
1429  *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1430  *    |   MSG_TYPE    |     FLAGS     |           DGM_ID              |
1431  *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1432  *    |                           SOURCE_IP                           |
1433  *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1434  *    |          SOURCE_PORT          |  ERROR_CODE   |
1435  *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1436  *
1437  *    ERROR_CODE values (in hexidecimal):
1438  *
1439  *            82 -  DESTINATION NAME NOT PRESENT
1440  *            83 -  INVALID SOURCE NAME FORMAT
1441  *            84 -  INVALID DESTINATION NAME FORMAT
1442  */
1443 
1444 typedef struct {
1445 	unsigned char	msg_type;
1446 	unsigned char	flags;
1447 	unsigned short	dgm_id;
1448 	uint32_t	source_ip;
1449 	unsigned short	source_port;
1450 	unsigned char	error;
1451 } datagram_error_packet;
1452 
1453 /*
1454  * 4.4.4.  DATAGRAM QUERY REQUEST
1455  *
1456  *                         1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
1457  *     0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
1458  *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1459  *    |   MSG_TYPE    |     FLAGS     |           DGM_ID              |
1460  *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1461  *    |                           SOURCE_IP                           |
1462  *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1463  *    |          SOURCE_PORT          |                               |
1464  *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+                               +
1465  *    |                                                               |
1466  *    /                       DESTINATION_NAME                        /
1467  *    /                                                               /
1468  *    |                                                               |
1469  *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1470  *
1471  * 4.4.5.  DATAGRAM POSITIVE AND NEGATIVE QUERY RESPONSE
1472  *
1473  *                         1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
1474  *     0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
1475  *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1476  *    |   MSG_TYPE    |     FLAGS     |           DGM_ID              |
1477  *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1478  *    |                           SOURCE_IP                           |
1479  *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1480  *    |          SOURCE_PORT          |                               |
1481  *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+                               +
1482  *    |                                                               |
1483  *    /                       DESTINATION_NAME                        /
1484  *    /                                                               /
1485  *    |                                                               |
1486  *    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
1487  */
1488 
1489 typedef struct datagram_query_packet {
1490 	unsigned char	msg_type;
1491 	unsigned char	flags;
1492 	unsigned short	dgm_id;
1493 	uint32_t	source_ip;
1494 	unsigned short	source_port;
1495 	unsigned char	destination_name[MAX_NAME_LENGTH];
1496 } datagram_query_packet;
1497 
1498 
1499 typedef struct datagram {
1500 	struct datagram 	*forw;
1501 	struct datagram 	*back;
1502 	struct addr_entry	inaddr;
1503 	int			discard_timer;
1504 	unsigned char		packet_type;
1505 	unsigned char		flags;
1506 	unsigned short		datagram_id;
1507 	struct name_entry	src;
1508 	struct name_entry	dest;
1509 	unsigned short		offset;
1510 	unsigned short		data_length;
1511 	unsigned char 		*data;
1512 	unsigned int		rawbytes;
1513 	unsigned char		rawbuf[MAX_DATAGRAM_LENGTH];
1514 } datagram;
1515 
1516 typedef struct datagram_queue {
1517 	struct datagram 	*forw;
1518 	struct datagram 	*back;
1519 } datagram_queue;
1520 
1521 typedef struct name_queue {
1522 	struct name_entry head;
1523 	mutex_t mtx;
1524 } name_queue_t;
1525 
1526 #define	NETBIOS_EMPTY_NAME (unsigned char *)""
1527 
1528 #define	NETBIOS_NAME_IS_STAR(name) \
1529 	(bcmp(name, "*\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", NETBIOS_NAME_SZ) == 0)
1530 
1531 void smb_netbios_chg_status(uint32_t status, int set);
1532 
1533 /*
1534  * Name Cache Functions
1535  */
1536 int  smb_netbios_cache_init(void);
1537 void smb_netbios_cache_fini(void);
1538 void smb_netbios_cache_dump(void);
1539 void smb_netbios_cache_print(void);
1540 void smb_netbios_cache_diag(char ** pbuf);
1541 int smb_netbios_cache_count(void);
1542 void smb_netbios_cache_clean(void);
1543 void smb_netbios_cache_reset_ttl(void);
1544 void smb_netbios_cache_delete_locals(name_queue_t *delq);
1545 void smb_netbios_cache_refresh(name_queue_t *refq);
1546 
1547 int smb_netbios_cache_insert(struct name_entry *name);
1548 int smb_netbios_cache_insert_list(struct name_entry *name);
1549 void smb_netbios_cache_delete(struct name_entry *name);
1550 int smb_netbios_cache_delete_addr(struct name_entry *name);
1551 struct name_entry *smb_netbios_cache_lookup(struct name_entry *name);
1552 struct name_entry *smb_netbios_cache_lookup_addr(struct name_entry *name);
1553 void smb_netbios_cache_update_entry(struct name_entry *entry,
1554 					struct name_entry *name);
1555 void smb_netbios_cache_unlock_entry(struct name_entry *name);
1556 unsigned char *smb_netbios_cache_status(unsigned char *buf, int bufsize,
1557 					unsigned char *scope);
1558 
1559 void smb_netbios_name_dump(struct name_entry *entry);
1560 void smb_netbios_name_logf(struct name_entry *entry);
1561 void smb_netbios_name_freeaddrs(struct name_entry *entry);
1562 struct name_entry *smb_netbios_name_dup(struct name_entry *entry,
1563 						int alladdr);
1564 
1565 /* Name service functions */
1566 void *smb_netbios_name_service_daemon(void *);
1567 void smb_init_name_struct(unsigned char *, char,
1568 			    unsigned char *, uint32_t, unsigned short,
1569 			    uint32_t, uint32_t, struct name_entry *);
1570 
1571 struct name_entry *smb_name_find_name(struct name_entry *name);
1572 int smb_name_add_name(struct name_entry *name);
1573 int smb_name_delete_name(struct name_entry *name);
1574 void smb_name_unlock_name(struct name_entry *name);
1575 
1576 void smb_netbios_name_config(void);
1577 void smb_netbios_name_unconfig(void);
1578 void smb_netbios_name_tick(void);
1579 
1580 int smb_first_level_name_encode(struct name_entry *name,
1581 			    unsigned char *out, int max_out);
1582 int smb_first_level_name_decode(unsigned char *in,
1583 			    struct name_entry *name);
1584 void smb_encode_netbios_name(unsigned char *name,
1585 			    char suffix, unsigned char *scope,
1586 			    struct name_entry *dest);
1587 
1588 /* Datagram service functions */
1589 void *smb_netbios_datagram_service_daemon(void *);
1590 int smb_netbios_datagram_send(struct name_entry *,
1591 			    struct name_entry *, unsigned char *, int);
1592 void smb_netbios_datagram_tick(void);
1593 
1594 
1595 /* browser functions */
1596 void smb_browser_config(void);
1597 void *smb_browser_dispatch(void *arg);
1598 void *smb_browser_daemon(void *);
1599 int smb_net_id(uint32_t ipaddr);
1600 struct name_entry *smb_browser_get_srvname(unsigned short netid);
1601 int smb_browser_load_transact_header(unsigned char *buffer,
1602     int maxcnt, int data_count, int reply, char *mailbox);
1603 
1604 /* Netlogon function */
1605 /*
1606  * smb_netlogon_receive
1607  *
1608  * This is where we handle all incoming NetLogon messages. Currently, we
1609  * ignore requests from anyone else. We are only interested in responses
1610  * to our own requests. The NetLogonResponse provides the name of the PDC.
1611  * If we don't already have a controller name, we use the name provided
1612  * in the message. Otherwise we use the name already in the environment.
1613  */
1614 void smb_netlogon_receive(struct datagram *datagram, char *mailbox,
1615     unsigned char *data, int datalen);
1616 
1617 #endif /* _SMB_NETBIOS_H_ */
1618