2N/A/*
2N/A * CDDL HEADER START
2N/A *
2N/A * The contents of this file are subject to the terms of the
2N/A * Common Development and Distribution License (the "License").
2N/A * You may not use this file except in compliance with the License.
2N/A *
2N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2N/A * or http://www.opensolaris.org/os/licensing.
2N/A * See the License for the specific language governing permissions
2N/A * and limitations under the License.
2N/A *
2N/A * When distributing Covered Code, include this CDDL HEADER in each
2N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
2N/A * If applicable, add the following below this CDDL HEADER, with the
2N/A * fields enclosed by brackets "[]" replaced with your own identifying
2N/A * information: Portions Copyright [yyyy] [name of copyright owner]
2N/A *
2N/A * CDDL HEADER END
2N/A */
2N/A/*
2N/A * Copyright (c) 2007, 2011, Oracle and/or its affiliates. All rights reserved.
2N/A */
2N/A
2N/A/*
2N/A * NetBIOS name resolution node types.
2N/A *
2N/A * A B-node (broadcast node) uses broadcasts for name registration
2N/A * and resolution. Routers typically do not forward broadcasts and
2N/A * only computers on the local subnet will respond.
2N/A *
2N/A * A P-node (peer-to-peer node) uses a NetBIOS name server (WINS)
2N/A * to resolve NetBIOS names, which allows it to work across routers.
2N/A * In order to function in a P-node environment, all computers must
2N/A * be configured to use the NetBIOS name server because P-nodes do
2N/A * not broadcast on the network.
2N/A *
2N/A * A mixed node (M-node) behaves as a B-node by default. If it cannot
2N/A * resolve the name via broadcast then it tries a NetBIOS name server
2N/A * lookup (P-node).
2N/A *
2N/A * A hybrid node (H-node) behaves as a P-node by default. If it cannot
2N/A * resolve the name using a NetBIOS name server then it resorts to
2N/A * broadcasts (B-node).
2N/A *
2N/A * NetBIOS Name Service Protocols
2N/A *
2N/A * A REQUEST packet is always sent to the well known UDP port 137.
2N/A * The destination address is normally either the IP broadcast address or
2N/A * the address of the NAME - the address of the NAME server it set up at
2N/A * initialization time. In rare cases, a request packet will be sent to
2N/A * an end node, e.g. a NAME QUERY REQUEST sent to "challenge" a node.
2N/A *
2N/A * A RESPONSE packet is always sent to the source UDP port and source IP
2N/A * address of the request packet.
2N/A *
2N/A * A DEMAND packet must always be sent to the well known UDP port 137.
2N/A * There is no restriction on the target IP address.
2N/A *
2N/A * A transaction ID is a value composed from the requestor's IP address and
2N/A * a unique 16 bit value generated by the originator of the transaction.
2N/A */
2N/A
2N/A#include <unistd.h>
2N/A#include <syslog.h>
2N/A#include <stdlib.h>
2N/A#include <synch.h>
2N/A#include <errno.h>
2N/A#include <netdb.h>
2N/A#include <sys/socket.h>
2N/A#include <sys/sockio.h>
2N/A#include <arpa/inet.h>
2N/A#include <net/if_arp.h>
2N/A
2N/A#include <smbsrv/libsmbns.h>
2N/A#include <smbns_netbios.h>
2N/A
2N/A/*
2N/A * RFC 1002 4.2.1.1. HEADER
2N/A */
2N/A#define QUESTION_TYPE_NETBIOS_GENERAL 0x20
2N/A#define QUESTION_TYPE_NETBIOS_STATUS 0x21
2N/A
2N/A#define QUESTION_CLASS_INTERNET 0x0001
2N/A
2N/A/*
2N/A * RFC 1002 4.2.1.3. RESOURCE RECORD
2N/A */
2N/A#define RR_TYPE_IP_ADDRESS_RESOURCE 0x0001
2N/A#define RR_TYPE_NAME_SERVER_RESOURCE 0x0002
2N/A#define RR_TYPE_NULL_RESOURCE 0x000A
2N/A#define RR_TYPE_NETBIOS_RESOURCE 0x0020
2N/A#define RR_TYPE_NETBIOS_STATUS 0x0021
2N/A
2N/A/*
2N/A *
2N/A * RESOURCE RECORD RR_CLASS field definitions
2N/A */
2N/A#define RR_CLASS_INTERNET_CLASS 0x0001
2N/A
2N/A/*
2N/A * NB_FLAGS field of the RESOURCE RECORD RDATA field for RR_TYPE of NB.
2N/A */
2N/A#define RR_FLAGS_NB_ONT_MASK 0x6000
2N/A#define RR_FLAGS_NB_ONT_B_NODE 0x0000
2N/A#define RR_FLAGS_NB_ONT_P_NODE 0x2000
2N/A#define RR_FLAGS_NB_ONT_M_NODE 0x4000
2N/A#define RR_FLAGS_NB_ONT_RESERVED 0x6000
2N/A#define RR_FLAGS_NB_GROUP_NAME 0x8000
2N/A
2N/A#define NAME_FLAGS_PERMANENT_NAME 0x0200
2N/A#define NAME_FLAGS_ACTIVE_NAME 0x0400
2N/A#define NAME_FLAGS_CONFLICT 0x0800
2N/A#define NAME_FLAGS_DEREGISTER 0x1000
2N/A#define NAME_FLAGS_ONT_MASK 0x6000
2N/A#define NAME_FLAGS_ONT_B_NODE 0x0000
2N/A#define NAME_FLAGS_ONT_P_NODE 0x2000
2N/A#define NAME_FLAGS_ONT_M_NODE 0x4000
2N/A#define NAME_FLAGS_ONT_RESERVED 0x6000
2N/A#define NAME_FLAGS_GROUP_NAME 0x8000
2N/A
2N/A#define MAX_NETBIOS_REPLY_DATA_SIZE 500
2N/A
2N/A#define NAME_HEADER_SIZE 12
2N/A
2N/Atypedef struct nbt_name_reply {
2N/A struct nbt_name_reply *forw;
2N/A struct nbt_name_reply *back;
2N/A struct name_packet *packet;
2N/A addr_entry_t *addr;
2N/A uint16_t name_trn_id;
2N/A boolean_t reply_ready;
2N/A} nbt_name_reply_t;
2N/A
2N/Astatic nbt_name_reply_t reply_queue;
2N/Astatic mutex_t rq_mtx;
2N/Astatic cond_t rq_cv;
2N/A
2N/Astatic mutex_t nbt_name_config_mtx;
2N/A
2N/Astatic name_queue_t delete_queue;
2N/Astatic name_queue_t refresh_queue;
2N/A
2N/Astatic int name_sock = 0;
2N/A
2N/A#define NBNS_NBCAST_DEFAULT 18
2N/A#define NBNS_NBCAST_MAX 1024
2N/A
2N/Astatic int bcast_num = 0;
2N/Astatic int nbns_num = 0;
2N/Astatic addr_entry_t smb_bcast_list[NBNS_NBCAST_MAX];
2N/Astatic addr_entry_t smb_nbns[SMB_PI_MAX_WINS];
2N/A
2N/Astatic int smb_netbios_process_response(uint16_t, addr_entry_t *,
2N/A struct name_packet *, uint32_t);
2N/A
2N/Astatic int smb_send_name_service_packet(addr_entry_t *addr,
2N/A struct name_packet *packet);
2N/A
2N/A/*
2N/A * Allocate a transaction id.
2N/A */
2N/Astatic uint16_t
2N/Asmb_netbios_name_trn_id(void)
2N/A{
2N/A static uint16_t trn_id;
2N/A static mutex_t trn_id_mtx;
2N/A
2N/A (void) mutex_lock(&trn_id_mtx);
2N/A
2N/A do {
2N/A ++trn_id;
2N/A } while (trn_id == 0 || trn_id == (uint16_t)-1);
2N/A
2N/A (void) mutex_unlock(&trn_id_mtx);
2N/A return (trn_id);
2N/A}
2N/A
2N/Astatic int
2N/Asmb_end_node_challenge(nbt_name_reply_t *reply_info)
2N/A{
2N/A int rc;
2N/A uint32_t retry;
2N/A uint16_t tid;
2N/A struct resource_record *answer;
2N/A struct name_question question;
2N/A addr_entry_t *addr;
2N/A struct name_entry *destination;
2N/A struct name_packet packet;
2N/A struct timespec st;
2N/A
2N/A /*
2N/A * The response packet has in it the address of the presumed owner
2N/A * of the name. Challenge that owner. If owner either does not
2N/A * respond or indicates that he no longer owns the name, claim the
2N/A * name. Otherwise, the name cannot be claimed.
2N/A */
2N/A
2N/A if ((answer = reply_info->packet->answer) == 0)
2N/A return (-1);
2N/A
2N/A destination = answer->name;
2N/A question.name = answer->name;
2N/A
2N/A packet.info = NAME_QUERY_REQUEST | NM_FLAGS_UNICAST;
2N/A packet.qdcount = 1; /* question entries */
2N/A packet.question = &question;
2N/A packet.ancount = 0; /* answer recs */
2N/A packet.answer = NULL;
2N/A packet.nscount = 0; /* authority recs */
2N/A packet.authority = NULL;
2N/A packet.arcount = 0; /* additional recs */
2N/A packet.additional = NULL;
2N/A
2N/A addr = &destination->addr_list;
2N/A for (retry = 0; retry < UCAST_REQ_RETRY_COUNT; retry++) {
2N/A tid = smb_netbios_name_trn_id();
2N/A packet.name_trn_id = tid;
2N/A if (smb_send_name_service_packet(addr, &packet) >= 0) {
2N/A if ((rc = smb_netbios_process_response(tid, addr,
2N/A &packet, UCAST_REQ_RETRY_TIMEOUT)) != 0)
2N/A return (rc);
2N/A }
2N/A st.tv_sec = 0;
2N/A st.tv_nsec = (UCAST_REQ_RETRY_TIMEOUT * 1000000);
2N/A (void) nanosleep(&st, 0);
2N/A }
2N/A /* No reply */
2N/A return (0);
2N/A}
2N/A
2N/Astatic nbt_name_reply_t *
2N/Asmb_name_get_reply(uint16_t tid, uint32_t timeout)
2N/A{
2N/A uint16_t info;
2N/A struct resource_record *answer;
2N/A nbt_name_reply_t *reply;
2N/A uint32_t wait_time, to_save; /* in millisecond */
2N/A struct timeval wt;
2N/A timestruc_t to;
2N/A
2N/A to_save = timeout;
2N/A reply = calloc(1, sizeof (nbt_name_reply_t));
2N/A if (reply == NULL)
2N/A return (NULL);
2N/A
2N/A reply->reply_ready = B_FALSE;
2N/A reply->name_trn_id = tid;
2N/A (void) mutex_lock(&rq_mtx);
2N/A QUEUE_INSERT_TAIL(&reply_queue, reply);
2N/A
2N/A for (;;) {
2N/A (void) gettimeofday(&wt, 0);
2N/A wait_time = wt.tv_usec / 1000;
2N/A
2N/A to.tv_sec = 0;
2N/A to.tv_nsec = timeout * 1000000;
2N/A (void) cond_reltimedwait(&rq_cv, &rq_mtx, &to);
2N/A
2N/A if (reply->reply_ready) {
2N/A info = reply->packet->info;
2N/A if (PACKET_TYPE(info) == WACK_RESPONSE) {
2N/A answer = reply->packet->answer;
2N/A wait_time = (answer) ?
2N/A TO_MILLISECONDS(answer->ttl) : DEFAULT_TTL;
2N/A free(reply->addr);
2N/A free(reply->packet);
2N/A timeout = to_save + wait_time;
2N/A reply->reply_ready = B_FALSE;
2N/A reply->name_trn_id = tid;
2N/A QUEUE_INSERT_TAIL(&reply_queue, reply);
2N/A continue;
2N/A }
2N/A (void) mutex_unlock(&rq_mtx);
2N/A return (reply);
2N/A }
2N/A (void) gettimeofday(&wt, 0);
2N/A wait_time = (wt.tv_usec / 1000) - wait_time;
2N/A if (wait_time >= timeout) {
2N/A QUEUE_CLIP(reply);
2N/A (void) mutex_unlock(&rq_mtx);
2N/A free(reply);
2N/A break;
2N/A }
2N/A timeout -= wait_time;
2N/A }
2N/A
2N/A return (NULL);
2N/A}
2N/A
2N/Astatic void
2N/Asmb_reply_ready(struct name_packet *packet, addr_entry_t *addr)
2N/A{
2N/A nbt_name_reply_t *reply;
2N/A struct resource_record *answer;
2N/A
2N/A (void) mutex_lock(&rq_mtx);
2N/A for (reply = reply_queue.forw; reply != &reply_queue;
2N/A reply = reply->forw) {
2N/A if (reply->name_trn_id == packet->name_trn_id) {
2N/A QUEUE_CLIP(reply);
2N/A
2N/A reply->addr = addr;
2N/A reply->packet = packet;
2N/A reply->reply_ready = B_TRUE;
2N/A (void) cond_signal(&rq_cv);
2N/A (void) mutex_unlock(&rq_mtx);
2N/A return;
2N/A }
2N/A }
2N/A (void) mutex_unlock(&rq_mtx);
2N/A
2N/A /* Presumably nobody is waiting any more... */
2N/A free(addr);
2N/A
2N/A answer = packet->answer;
2N/A if (answer)
2N/A smb_netbios_name_freeaddrs(answer->name);
2N/A free(packet);
2N/A}
2N/A
2N/Astatic int
2N/Asmb_netbios_process_response(uint16_t tid, addr_entry_t *addr,
2N/A struct name_packet *packet, uint32_t timeout)
2N/A{
2N/A int rc = 0;
2N/A uint16_t info;
2N/A nbt_name_reply_t *reply;
2N/A struct resource_record *answer;
2N/A struct name_entry *name;
2N/A struct name_entry *entry;
2N/A struct name_question *question;
2N/A uint32_t ttl;
2N/A
2N/A if ((reply = smb_name_get_reply(tid, timeout)) == 0) {
2N/A return (0); /* No reply: retry */
2N/A }
2N/A info = reply->packet->info;
2N/A answer = reply->packet->answer;
2N/A
2N/A /* response */
2N/A switch (PACKET_TYPE(info)) {
2N/A case NAME_QUERY_RESPONSE:
2N/A if (POSITIVE_RESPONSE(info)) {
2N/A addr = &answer->name->addr_list;
2N/A do {
2N/A /*
2N/A * Make sure that remote name is not
2N/A * flagged local
2N/A */
2N/A addr->attributes &= ~NAME_ATTR_LOCAL;
2N/A
2N/A if (answer->ttl)
2N/A addr->ttl = answer->ttl;
2N/A else
2N/A addr->ttl = DEFAULT_TTL;
2N/A addr->refresh_ttl = TO_SECONDS(addr->ttl);
2N/A addr->ttl = addr->refresh_ttl;
2N/A
2N/A addr = addr->forw;
2N/A } while (addr != &answer->name->addr_list);
2N/A smb_netbios_name_logf(answer->name);
2N/A (void) smb_netbios_cache_insert_list(answer->name);
2N/A rc = 1;
2N/A } else {
2N/A rc = -1;
2N/A }
2N/A break;
2N/A
2N/A case NAME_REGISTRATION_RESPONSE:
2N/A if (NEGATIVE_RESPONSE(info)) {
2N/A if (RCODE(info) == RCODE_CFT_ERR) {
2N/A if (answer == 0) {
2N/A rc = -RCODE(info);
2N/A break;
2N/A }
2N/A
2N/A name = answer->name;
2N/A entry = smb_netbios_cache_lookup(name);
2N/A if (entry) {
2N/A /*
2N/A * a name in the state "conflict
2N/A * detected" does not "logically" exist
2N/A * on that node. No further session
2N/A * will be accepted on that name.
2N/A * No datagrams can be sent against
2N/A * that name.
2N/A * Such an entry will not be used for
2N/A * purposes of processing incoming
2N/A * request packets.
2N/A * The only valid user NetBIOS operation
2N/A * against such a name is DELETE NAME.
2N/A */
2N/A entry->attributes |= NAME_ATTR_CONFLICT;
2N/A syslog(LOG_DEBUG,
2N/A "nbns: name conflict: %15.15s",
2N/A entry->name);
2N/A smb_netbios_cache_unlock_entry(entry);
2N/A }
2N/A }
2N/A rc = -RCODE(info);
2N/A break;
2N/A }
2N/A
2N/A /*
2N/A * name can be added:
2N/A * adjust refresh timeout value,
2N/A * TTL, for this name
2N/A */
2N/A question = packet->question;
2N/A ttl = (answer && answer->ttl) ? answer->ttl : DEFAULT_TTL;
2N/A ttl = TO_SECONDS(ttl);
2N/A if ((entry = smb_netbios_cache_lookup(question->name)) != 0) {
2N/A addr = &entry->addr_list;
2N/A do {
2N/A if ((addr->refresh_ttl == 0) ||
2N/A (ttl < addr->refresh_ttl))
2N/A addr->refresh_ttl = addr->ttl = ttl;
2N/A addr = addr->forw;
2N/A } while (addr != &entry->addr_list);
2N/A smb_netbios_cache_unlock_entry(entry);
2N/A }
2N/A
2N/A rc = 1;
2N/A break;
2N/A
2N/A case NAME_RELEASE_RESPONSE:
2N/A rc = 1;
2N/A break;
2N/A
2N/A case END_NODE_CHALLENGE_REGISTRATION_REQUEST:
2N/A /*
2N/A * The response packet has in it the
2N/A * address of the presumed owner of the
2N/A * name. Challenge that owner. If
2N/A * owner either does not respond or
2N/A * indicates that he no longer owns the
2N/A * name, claim the name. Otherwise,
2N/A * the name cannot be claimed.
2N/A */
2N/A rc = smb_end_node_challenge(reply);
2N/A break;
2N/A
2N/A default:
2N/A rc = 0;
2N/A break;
2N/A }
2N/A
2N/A if (answer)
2N/A smb_netbios_name_freeaddrs(answer->name);
2N/A free(reply->addr);
2N/A free(reply->packet);
2N/A free(reply);
2N/A return (rc); /* retry */
2N/A}
2N/A
2N/A/*
2N/A * smb_name_buf_from_packet
2N/A *
2N/A * Description:
2N/A * Convert a NetBIOS Name Server Packet Block (npb)
2N/A * into the bits and bytes destined for the wire.
2N/A * The "buf" is used as a heap.
2N/A *
2N/A * Inputs:
2N/A * char * buf -> Buffer, from the wire
2N/A * unsigned n_buf -> Length of 'buf'
2N/A * name_packet *npb -> Packet block, decode into
2N/A * unsigned n_npb -> Max bytes in 'npb'
2N/A *
2N/A * Returns:
2N/A * >0 -> Encode successful, value is length of packet in "buf"
2N/A * -1 -> Hard error, can not possibly encode
2N/A * -2 -> Need more memory in buf -- it's too small
2N/A */
2N/Astatic int
2N/Asmb_name_buf_from_packet(unsigned char *buf, int n_buf,
2N/A struct name_packet *npb)
2N/A{
2N/A addr_entry_t *raddr;
2N/A unsigned char *heap = buf;
2N/A unsigned char *end_heap = heap + n_buf;
2N/A unsigned char *dnptrs[32];
2N/A unsigned char comp_name_buf[MAX_NAME_LENGTH];
2N/A unsigned int tmp;
2N/A int i, step;
2N/A
2N/A if (n_buf < NAME_HEADER_SIZE)
2N/A return (-1); /* no header, impossible */
2N/A
2N/A dnptrs[0] = heap;
2N/A dnptrs[1] = 0;
2N/A
2N/A BE_OUT16(heap, npb->name_trn_id);
2N/A heap += 2;
2N/A
2N/A BE_OUT16(heap, npb->info);
2N/A heap += 2;
2N/A
2N/A BE_OUT16(heap, npb->qdcount);
2N/A heap += 2;
2N/A
2N/A BE_OUT16(heap, npb->ancount);
2N/A heap += 2;
2N/A
2N/A BE_OUT16(heap, npb->nscount);
2N/A heap += 2;
2N/A
2N/A BE_OUT16(heap, npb->arcount);
2N/A heap += 2;
2N/A
2N/A for (i = 0; i < npb->qdcount; i++) {
2N/A if ((heap + 34 + 4) > end_heap)
2N/A return (-2);
2N/A
2N/A (void) smb_first_level_name_encode(npb->question[i].name,
2N/A comp_name_buf, sizeof (comp_name_buf));
2N/A (void) strcpy((char *)heap, (char *)comp_name_buf);
2N/A heap += strlen((char *)comp_name_buf) + 1;
2N/A
2N/A BE_OUT16(heap, npb->question[i].question_type);
2N/A heap += 2;
2N/A
2N/A BE_OUT16(heap, npb->question[i].question_class);
2N/A heap += 2;
2N/A }
2N/A
2N/A for (step = 1; step <= 3; step++) {
2N/A struct resource_record *nrr;
2N/A int n;
2N/A
2N/A /* truly ugly, but saves code copying */
2N/A if (step == 1) {
2N/A n = npb->ancount;
2N/A nrr = npb->answer;
2N/A } else if (step == 2) {
2N/A n = npb->nscount;
2N/A nrr = npb->authority;
2N/A } else { /* step == 3 */
2N/A n = npb->arcount;
2N/A nrr = npb->additional;
2N/A }
2N/A
2N/A for (i = 0; i < n; i++) {
2N/A if ((heap + 34 + 10) > end_heap)
2N/A return (-2);
2N/A
2N/A (void) smb_first_level_name_encode(nrr->name,
2N/A comp_name_buf, sizeof (comp_name_buf));
2N/A (void) strcpy((char *)heap, (char *)comp_name_buf);
2N/A heap += strlen((char *)comp_name_buf) + 1;
2N/A
2N/A BE_OUT16(heap, nrr[i].rr_type);
2N/A heap += 2;
2N/A
2N/A BE_OUT16(heap, nrr[i].rr_class);
2N/A heap += 2;
2N/A
2N/A BE_OUT32(heap, nrr[i].ttl);
2N/A heap += 4;
2N/A
2N/A BE_OUT16(heap, nrr[i].rdlength);
2N/A heap += 2;
2N/A
2N/A if ((tmp = nrr[i].rdlength) > 0) {
2N/A if ((heap + tmp) > end_heap)
2N/A return (-2);
2N/A
2N/A if (nrr[i].rr_type == NAME_RR_TYPE_NB &&
2N/A nrr[i].rr_class == NAME_RR_CLASS_IN &&
2N/A tmp >= 6 && nrr[i].rdata == 0) {
2N/A tmp = nrr[i].name->attributes &
2N/A (NAME_ATTR_GROUP |
2N/A NAME_ATTR_OWNER_NODE_TYPE);
2N/A BE_OUT16(heap, tmp);
2N/A heap += 2;
2N/A
2N/A raddr = &nrr[i].name->addr_list;
2N/A (void) memcpy(heap,
2N/A &raddr->sin.sin_addr.s_addr,
2N/A sizeof (uint32_t));
2N/A heap += 4;
2N/A } else {
2N/A bcopy(nrr[i].rdata, heap, tmp);
2N/A heap += tmp;
2N/A }
2N/A }
2N/A }
2N/A }
2N/A return (heap - buf);
2N/A}
2N/A
2N/A/*
2N/A * strnchr
2N/A *
2N/A * Lookup for character 'c' in first 'n' chars of string 's'.
2N/A * Returns pointer to the found char, otherwise returns 0.
2N/A */
2N/Astatic char *
2N/Astrnchr(const char *s, char c, int n)
2N/A{
2N/A char *ps = (char *)s;
2N/A char *es = (char *)s + n;
2N/A
2N/A while (ps < es && *ps) {
2N/A if (*ps == c)
2N/A return (ps);
2N/A
2N/A ++ps;
2N/A }
2N/A
2N/A if (*ps == '\0' && c == '\0')
2N/A return (ps);
2N/A
2N/A return (0);
2N/A}
2N/A
2N/Astatic boolean_t
2N/Ais_multihome(char *name)
2N/A{
2N/A return (smb_nic_getnum(name) > 1);
2N/A}
2N/A
2N/A/*
2N/A * smb_netbios_getname
2N/A *
2N/A * Get the Netbios name part of the given record.
2N/A * Does some boundary checks.
2N/A *
2N/A * Returns the name length on success, otherwise
2N/A * returns 0.
2N/A */
2N/Astatic int
2N/Asmb_netbios_getname(char *name, char *buf, char *buf_end)
2N/A{
2N/A char *name_end;
2N/A int name_len;
2N/A
2N/A if (buf >= buf_end) {
2N/A /* no room for a NB name */
2N/A return (0);
2N/A }
2N/A
2N/A name_end = strnchr(buf, '\0', buf_end - buf + 1);
2N/A if (name_end == 0) {
2N/A /* not a valid NB name */
2N/A return (0);
2N/A }
2N/A
2N/A name_len = name_end - buf + 1;
2N/A
2N/A (void) strlcpy(name, buf, name_len);
2N/A return (name_len);
2N/A}
2N/A
2N/A/*
2N/A * smb_name_buf_to_packet
2N/A *
2N/A * Convert the bits and bytes that came from the wire into a NetBIOS
2N/A * Name Server Packet Block (npb). The "block" is used as a heap.
2N/A *
2N/A * Returns a pointer to a name packet on success. Otherwise, returns
2N/A * a NULL pointer.
2N/A */
2N/Astatic struct name_packet *
2N/Asmb_name_buf_to_packet(char *buf, int n_buf)
2N/A{
2N/A struct name_packet *npb;
2N/A unsigned char *heap;
2N/A unsigned char *scan = (unsigned char *)buf;
2N/A unsigned char *scan_end = scan + n_buf;
2N/A char name_buf[MAX_NAME_LENGTH];
2N/A struct resource_record *nrr = 0;
2N/A int rc, i, n, nn, ns;
2N/A uint16_t name_trn_id, info;
2N/A uint16_t qdcount, ancount, nscount, arcount;
2N/A addr_entry_t *next;
2N/A int name_len;
2N/A
2N/A if (n_buf < NAME_HEADER_SIZE) {
2N/A /* truncated header */
2N/A syslog(LOG_DEBUG, "nbns: short packet (%d bytes)", n_buf);
2N/A return (NULL);
2N/A }
2N/A
2N/A name_trn_id = BE_IN16(scan); scan += 2;
2N/A info = BE_IN16(scan); scan += 2;
2N/A qdcount = BE_IN16(scan); scan += 2;
2N/A ancount = BE_IN16(scan); scan += 2;
2N/A nscount = BE_IN16(scan); scan += 2;
2N/A arcount = BE_IN16(scan); scan += 2;
2N/A
2N/A ns = sizeof (struct name_entry);
2N/A n = n_buf + sizeof (struct name_packet) +
2N/A ((unsigned)qdcount * (sizeof (struct name_question) + ns)) +
2N/A ((unsigned)ancount * (sizeof (struct resource_record) + ns)) +
2N/A ((unsigned)nscount * (sizeof (struct resource_record) + ns)) +
2N/A ((unsigned)arcount * (sizeof (struct resource_record) + ns));
2N/A
2N/A if ((npb = calloc(1, n)) == NULL)
2N/A return (NULL);
2N/A
2N/A bzero(npb, n);
2N/A heap = npb->block_data;
2N/A npb->name_trn_id = name_trn_id;
2N/A npb->info = info;
2N/A npb->qdcount = qdcount;
2N/A npb->ancount = ancount;
2N/A npb->nscount = nscount;
2N/A npb->arcount = arcount;
2N/A
2N/A /* scan is in position for question entries */
2N/A
2N/A /*
2N/A * Measure the space needed for the tables
2N/A */
2N/A if (qdcount > 0) {
2N/A /* LINTED - E_BAD_PTR_CAST_ALIGN */
2N/A npb->question = (struct name_question *)heap;
2N/A heap += qdcount * sizeof (struct name_question);
2N/A for (i = 0; i < qdcount; i++) {
2N/A /* LINTED - E_BAD_PTR_CAST_ALIGN */
2N/A npb->question[i].name = (struct name_entry *)heap;
2N/A heap += sizeof (struct name_entry);
2N/A }
2N/A }
2N/A
2N/A /* LINTED - E_BAD_PTR_CAST_ALIGN */
2N/A nrr = (struct resource_record *)heap;
2N/A
2N/A if (ancount > 0) {
2N/A /* LINTED - E_BAD_PTR_CAST_ALIGN */
2N/A npb->answer = (struct resource_record *)heap;
2N/A heap += ancount * sizeof (struct resource_record);
2N/A }
2N/A
2N/A if (nscount > 0) {
2N/A /* LINTED - E_BAD_PTR_CAST_ALIGN */
2N/A npb->authority = (struct resource_record *)heap;
2N/A heap += nscount * sizeof (struct resource_record);
2N/A }
2N/A
2N/A if (arcount > 0) {
2N/A /* LINTED - E_BAD_PTR_CAST_ALIGN */
2N/A npb->additional = (struct resource_record *)heap;
2N/A heap += arcount * sizeof (struct resource_record);
2N/A }
2N/A
2N/A /*
2N/A * Populate each resource_record's .name field.
2N/A * Done as a second pass so that all resource records
2N/A * (answer, authority, additional) are consecutive via nrr[i].
2N/A */
2N/A for (i = 0; i < (ancount + nscount + arcount); i++) {
2N/A /* LINTED - E_BAD_PTR_CAST_ALIGN */
2N/A nrr[i].name = (struct name_entry *)heap;
2N/A heap += sizeof (struct name_entry);
2N/A }
2N/A
2N/A
2N/A for (i = 0; i < npb->qdcount; i++) {
2N/A name_len = smb_netbios_getname(name_buf, (char *)scan,
2N/A (char *)scan_end);
2N/A if (name_len <= 0) {
2N/A free(npb);
2N/A return (NULL);
2N/A }
2N/A
2N/A smb_init_name_struct(NETBIOS_EMPTY_NAME, 0, 0, 0, 0, 0, 0,
2N/A npb->question[i].name);
2N/A rc = smb_first_level_name_decode((unsigned char *)name_buf,
2N/A npb->question[i].name);
2N/A if (rc < 0) {
2N/A /* Couldn't decode the question name */
2N/A free(npb);
2N/A return (NULL);
2N/A }
2N/A
2N/A scan += name_len;
2N/A if (scan + 4 > scan_end) {
2N/A /* no room for Question Type(2) and Class(2) fields */
2N/A free(npb);
2N/A return (NULL);
2N/A }
2N/A
2N/A npb->question[i].question_type = BE_IN16(scan); scan += 2;
2N/A npb->question[i].question_class = BE_IN16(scan); scan += 2;
2N/A }
2N/A
2N/A /*
2N/A * Cheat. Remaining sections are of the same resource_record
2N/A * format. Table space is consecutive.
2N/A */
2N/A
2N/A for (i = 0; i < (ancount + nscount + arcount); i++) {
2N/A if (scan[0] == 0xc0) {
2N/A /* Namebuf is reused... */
2N/A rc = 2;
2N/A } else {
2N/A name_len = smb_netbios_getname(name_buf, (char *)scan,
2N/A (char *)scan_end);
2N/A if (name_len <= 0) {
2N/A free(npb);
2N/A return (NULL);
2N/A }
2N/A rc = name_len;
2N/A }
2N/A scan += rc;
2N/A
2N/A if (scan + 10 > scan_end) {
2N/A /*
2N/A * no room for RR_TYPE (2), RR_CLASS (2), TTL (4) and
2N/A * RDLENGTH (2) fields.
2N/A */
2N/A free(npb);
2N/A return (NULL);
2N/A }
2N/A
2N/A smb_init_name_struct(NETBIOS_EMPTY_NAME, 0, 0, 0, 0, 0, 0,
2N/A nrr[i].name);
2N/A if ((rc = smb_first_level_name_decode((unsigned char *)name_buf,
2N/A nrr[i].name)) < 0) {
2N/A free(npb);
2N/A return (NULL);
2N/A }
2N/A
2N/A nrr[i].rr_type = BE_IN16(scan); scan += 2;
2N/A nrr[i].rr_class = BE_IN16(scan); scan += 2;
2N/A nrr[i].ttl = BE_IN32(scan); scan += 4;
2N/A nrr[i].rdlength = BE_IN16(scan); scan += 2;
2N/A
2N/A if ((n = nrr[i].rdlength) > 0) {
2N/A if ((scan + n) > scan_end) {
2N/A /* no room for RDATA */
2N/A free(npb);
2N/A return (NULL);
2N/A }
2N/A bcopy(scan, heap, n);
2N/A
2N/A nn = n;
2N/A if (nrr[i].rr_type == 0x0020 &&
2N/A nrr[i].rr_class == 0x01 && n >= 6) {
2N/A while (nn) {
2N/A if (nn == 6)
2N/A next = &nrr[i].name->addr_list;
2N/A else {
2N/A next = calloc(1,
2N/A sizeof (addr_entry_t));
2N/A if (next == 0) {
2N/A /* not enough memory */
2N/A free(npb);
2N/A return (NULL);
2N/A }
2N/A QUEUE_INSERT_TAIL(
2N/A &nrr[i].name->addr_list,
2N/A next);
2N/A }
2N/A nrr[i].name->attributes =
2N/A BE_IN16(scan);
2N/A next->sin.sin_family = AF_INET;
2N/A next->sinlen = sizeof (next->sin);
2N/A (void) memcpy(
2N/A &next->sin.sin_addr.s_addr,
2N/A scan + 2, sizeof (uint32_t));
2N/A next->sin.sin_port =
2N/A htons(IPPORT_NETBIOS_DGM);
2N/A nn -= 6;
2N/A scan += 6;
2N/A }
2N/A } else {
2N/A nrr[i].rdata = heap;
2N/A scan += n;
2N/A }
2N/A heap += n;
2N/A }
2N/A }
2N/A return (npb);
2N/A}
2N/A
2N/A/*
2N/A * smb_send_name_service_packet
2N/A *
2N/A * Description:
2N/A *
2N/A * Send out a name service packet to proper destination.
2N/A *
2N/A * Inputs:
2N/A * struct netbios_name *dest -> NETBIOS name of destination
2N/A * struct name_packet *packet -> Packet to send
2N/A *
2N/A * Returns:
2N/A * success -> >0
2N/A * failure -> <=0
2N/A */
2N/Astatic int
2N/Asmb_send_name_service_packet(addr_entry_t *addr, struct name_packet *packet)
2N/A{
2N/A unsigned char buf[MAX_DATAGRAM_LENGTH];
2N/A int len;
2N/A
2N/A if ((len = smb_name_buf_from_packet(buf, sizeof (buf), packet)) < 0) {
2N/A errno = EINVAL;
2N/A return (-1);
2N/A }
2N/A
2N/A return (sendto(name_sock, buf, len, MSG_EOR,
2N/A (struct sockaddr *)&addr->sin, addr->sinlen));
2N/A}
2N/A
2N/A/*
2N/A * smb_netbios_send_rcv
2N/A *
2N/A * This function sends the given NetBIOS packet to the given
2N/A * address and get back the response. If send operation is not
2N/A * successful, it's repeated 'retries' times.
2N/A *
2N/A * Returns:
2N/A * 0 Unsuccessful send operation; no reply
2N/A * 1 Got reply
2N/A */
2N/Astatic int
2N/Asmb_netbios_send_rcv(int bcast, addr_entry_t *destination,
2N/A struct name_packet *packet, uint32_t retries, uint32_t timeout)
2N/A{
2N/A uint32_t retry;
2N/A uint16_t tid;
2N/A struct timespec st;
2N/A int rc;
2N/A
2N/A for (retry = 0; retry < retries; retry++) {
2N/A if ((destination->flags & ADDR_FLAG_VALID) == 0)
2N/A return (0);
2N/A
2N/A tid = smb_netbios_name_trn_id();
2N/A packet->name_trn_id = tid;
2N/A if (smb_send_name_service_packet(destination, packet) >= 0) {
2N/A rc = smb_netbios_process_response(tid, destination,
2N/A packet, timeout);
2N/A
2N/A if ((rc > 0) || (bcast == BROADCAST))
2N/A return (1);
2N/A
2N/A if (rc != 0)
2N/A return (0);
2N/A }
2N/A
2N/A st.tv_sec = 0;
2N/A st.tv_nsec = (timeout * 1000000);
2N/A (void) nanosleep(&st, 0);
2N/A }
2N/A
2N/A return (0);
2N/A}
2N/A
2N/A/*
2N/A * RFC 1002 4.2.2. NAME REGISTRATION REQUEST
2N/A */
2N/Astatic int
2N/Asmb_send_name_registration_request(int bcast, struct name_question *question,
2N/A struct resource_record *additional)
2N/A{
2N/A int gotreply = 0;
2N/A uint32_t retries;
2N/A uint32_t timeout;
2N/A addr_entry_t *destination;
2N/A struct name_packet packet;
2N/A unsigned char type;
2N/A int i, addr_num, rc;
2N/A
2N/A type = question->name->name[15];
2N/A if ((type != NBT_WKSTA) && (type != NBT_SERVER)) {
2N/A syslog(LOG_DEBUG, "nbns: name registration bad type (0x%02x)",
2N/A type);
2N/A smb_netbios_name_logf(question->name);
2N/A question->name->attributes &= ~NAME_ATTR_LOCAL;
2N/A return (-1);
2N/A }
2N/A
2N/A if (bcast == BROADCAST) {
2N/A if (bcast_num == 0)
2N/A return (0);
2N/A destination = smb_bcast_list;
2N/A addr_num = bcast_num;
2N/A retries = BCAST_REQ_RETRY_COUNT;
2N/A timeout = BCAST_REQ_RETRY_TIMEOUT;
2N/A packet.info = NAME_REGISTRATION_REQUEST | NM_FLAGS_BROADCAST;
2N/A } else {
2N/A if (nbns_num == 0)
2N/A return (0);
2N/A destination = smb_nbns;
2N/A addr_num = nbns_num;
2N/A retries = UCAST_REQ_RETRY_COUNT;
2N/A timeout = UCAST_REQ_RETRY_TIMEOUT;
2N/A packet.info = NAME_REGISTRATION_REQUEST | NM_FLAGS_UNICAST;
2N/A }
2N/A
2N/A packet.qdcount = 1; /* question entries */
2N/A packet.question = question;
2N/A packet.ancount = 0; /* answer recs */
2N/A packet.answer = NULL;
2N/A packet.nscount = 0; /* authority recs */
2N/A packet.authority = NULL;
2N/A packet.arcount = 1; /* additional recs */
2N/A packet.additional = additional;
2N/A
2N/A if (IS_UNIQUE(question->name->attributes) &&
2N/A (is_multihome((char *)(question->name->name))))
2N/A packet.info |= NAME_MULTIHOME_REGISTRATION_REQUEST;
2N/A
2N/A for (i = 0; i < addr_num; i++) {
2N/A /*
2N/A * Only register with the Primary WINS server,
2N/A * unless we got no reply.
2N/A */
2N/A if ((bcast == UNICAST) && gotreply)
2N/A break;
2N/A
2N/A rc = smb_netbios_send_rcv(bcast, &destination[i], &packet,
2N/A retries, timeout);
2N/A if (rc == 1)
2N/A gotreply = 1;
2N/A }
2N/A
2N/A return (gotreply);
2N/A}
2N/A
2N/A/*
2N/A * RFC 1002 4.2.4. NAME REFRESH REQUEST
2N/A */
2N/A/*ARGSUSED*/
2N/Astatic int
2N/Asmb_send_name_refresh_request(int bcast, struct name_question *question,
2N/A struct resource_record *additional, int force)
2N/A{
2N/A int rc = 0;
2N/A int gotreply = 0;
2N/A uint32_t retries;
2N/A uint32_t timeout;
2N/A addr_entry_t *addr;
2N/A addr_entry_t *destination;
2N/A struct name_packet packet;
2N/A unsigned char type;
2N/A int i, addr_num, q_addrs = 0;
2N/A
2N/A type = question->name->name[15];
2N/A if ((type != NBT_WKSTA) && (type != NBT_SERVER)) {
2N/A syslog(LOG_DEBUG, "nbns: name refresh bad type (0x%02x)", type);
2N/A smb_netbios_name_logf(question->name);
2N/A question->name->attributes &= ~NAME_ATTR_LOCAL;
2N/A return (-1);
2N/A }
2N/A switch (bcast) {
2N/A case BROADCAST :
2N/A if (bcast_num == 0)
2N/A return (-1);
2N/A destination = smb_bcast_list;
2N/A addr_num = bcast_num;
2N/A retries = BCAST_REQ_RETRY_COUNT;
2N/A timeout = BCAST_REQ_RETRY_TIMEOUT;
2N/A packet.info = NAME_REFRESH_REQUEST | NM_FLAGS_BROADCAST;
2N/A break;
2N/A
2N/A case UNICAST :
2N/A if (nbns_num == 0)
2N/A return (-1);
2N/A destination = smb_nbns;
2N/A addr_num = nbns_num;
2N/A retries = UCAST_REQ_RETRY_COUNT;
2N/A timeout = UCAST_REQ_RETRY_TIMEOUT;
2N/A packet.info = NAME_REFRESH_REQUEST | NM_FLAGS_UNICAST;
2N/A break;
2N/A
2N/A default:
2N/A destination = &question->name->addr_list;
2N/A /*
2N/A * the value of addr_num is irrelvant here, because
2N/A * the code is going to do special_process so it doesn't
2N/A * need the addr_num. We set a value here just to avoid
2N/A * compiler warning.
2N/A */
2N/A addr_num = 0;
2N/A retries = UCAST_REQ_RETRY_COUNT;
2N/A timeout = UCAST_REQ_RETRY_TIMEOUT;
2N/A packet.info = NAME_REFRESH_REQUEST | NM_FLAGS_UNICAST;
2N/A q_addrs = 1;
2N/A break;
2N/A }
2N/A
2N/A if (IS_UNIQUE(question->name->attributes) &&
2N/A (is_multihome((char *)(question->name->name))))
2N/A packet.info |= NAME_MULTIHOME_REGISTRATION_REQUEST;
2N/A
2N/A packet.qdcount = 1; /* question entries */
2N/A packet.question = question;
2N/A packet.ancount = 0; /* answer recs */
2N/A packet.answer = NULL;
2N/A packet.nscount = 0; /* authority recs */
2N/A packet.authority = NULL;
2N/A packet.arcount = 1; /* additional recs */
2N/A packet.additional = additional;
2N/A
2N/A if (q_addrs)
2N/A goto special_process;
2N/A
2N/A for (i = 0; i < addr_num; i++) {
2N/A rc = smb_netbios_send_rcv(bcast, &destination[i], &packet,
2N/A retries, timeout);
2N/A if (rc == 1)
2N/A gotreply = 1;
2N/A }
2N/A
2N/A return (gotreply);
2N/A
2N/Aspecial_process:
2N/A addr = destination;
2N/A do {
2N/A rc = smb_netbios_send_rcv(bcast, addr, &packet,
2N/A retries, timeout);
2N/A if (rc == 1)
2N/A gotreply = 1;
2N/A addr = addr->forw;
2N/A } while (addr != destination);
2N/A
2N/A return (gotreply);
2N/A}
2N/A
2N/A/*
2N/A * RFC 1002 4.2.5. POSITIVE NAME REGISTRATION RESPONSE
2N/A * RFC 1002 4.2.6. NEGATIVE NAME REGISTRATION RESPONSE
2N/A */
2N/Astatic int
2N/Asmb_send_name_registration_response(addr_entry_t *addr,
2N/A struct name_packet *original_packet, uint16_t rcode)
2N/A{
2N/A struct name_packet packet;
2N/A struct resource_record answer;
2N/A
2N/A bzero(&packet, sizeof (struct name_packet));
2N/A bzero(&answer, sizeof (struct resource_record));
2N/A
2N/A packet.name_trn_id = original_packet->name_trn_id;
2N/A packet.info = NAME_REGISTRATION_RESPONSE | NAME_NM_FLAGS_RA |
2N/A (rcode & NAME_RCODE_MASK);
2N/A packet.qdcount = 0; /* question entries */
2N/A packet.question = NULL;
2N/A packet.ancount = 1; /* answer recs */
2N/A packet.answer = &answer;
2N/A packet.nscount = 0; /* authority recs */
2N/A packet.authority = NULL;
2N/A packet.arcount = 0; /* additional recs */
2N/A packet.additional = NULL;
2N/A
2N/A answer.name = original_packet->question->name;
2N/A answer.rr_type = NAME_QUESTION_TYPE_NB;
2N/A answer.rr_class = NAME_QUESTION_CLASS_IN;
2N/A answer.ttl = original_packet->additional->ttl;
2N/A answer.rdlength = original_packet->additional->rdlength;
2N/A answer.rdata = original_packet->additional->rdata;
2N/A
2N/A return (smb_send_name_service_packet(addr, &packet));
2N/A}
2N/A
2N/A/*
2N/A * RFC 1002 4.2.9. NAME RELEASE REQUEST & DEMAND
2N/A */
2N/Astatic int
2N/Asmb_send_name_release_request_and_demand(int bcast,
2N/A struct name_question *question, struct resource_record *additional)
2N/A{
2N/A int gotreply = 0;
2N/A int i, rc;
2N/A int addr_num;
2N/A uint32_t retries;
2N/A uint32_t timeout;
2N/A addr_entry_t *destination;
2N/A struct name_packet packet;
2N/A
2N/A if (bcast == BROADCAST) {
2N/A if (bcast_num == 0)
2N/A return (-1);
2N/A destination = smb_bcast_list;
2N/A addr_num = bcast_num;
2N/A retries = 1; /* BCAST_REQ_RETRY_COUNT */
2N/A timeout = 100; /* BCAST_REQ_RETRY_TIMEOUT */
2N/A packet.info = NAME_RELEASE_REQUEST | NM_FLAGS_BROADCAST;
2N/A } else {
2N/A if (nbns_num == 0)
2N/A return (-1);
2N/A destination = smb_nbns;
2N/A addr_num = nbns_num;
2N/A retries = 1; /* UCAST_REQ_RETRY_COUNT */
2N/A timeout = 100; /* UCAST_REQ_RETRY_TIMEOUT */
2N/A packet.info = NAME_RELEASE_REQUEST | NM_FLAGS_UNICAST;
2N/A }
2N/A
2N/A packet.qdcount = 1; /* question entries */
2N/A packet.question = question;
2N/A packet.ancount = 0; /* answer recs */
2N/A packet.answer = NULL;
2N/A packet.nscount = 0; /* authority recs */
2N/A packet.authority = NULL;
2N/A packet.arcount = 1; /* additional recs */
2N/A packet.additional = additional;
2N/A
2N/A for (i = 0; i < addr_num; i++) {
2N/A rc = smb_netbios_send_rcv(bcast, &destination[i], &packet,
2N/A retries, timeout);
2N/A if (rc == 1)
2N/A gotreply = 1;
2N/A }
2N/A
2N/A return (gotreply);
2N/A}
2N/A
2N/A/*
2N/A * RFC 1002 4.2.10. POSITIVE NAME RELEASE RESPONSE
2N/A * RFC 1002 4.2.11. NEGATIVE NAME RELEASE RESPONSE
2N/A */
2N/Astatic int
2N/A/* LINTED - E_STATIC_UNUSED */
2N/Asmb_send_name_release_response(addr_entry_t *addr,
2N/A struct name_packet *original_packet, uint16_t rcode)
2N/A{
2N/A struct name_packet packet;
2N/A struct resource_record answer;
2N/A
2N/A bzero(&packet, sizeof (struct name_packet));
2N/A bzero(&answer, sizeof (struct resource_record));
2N/A
2N/A packet.name_trn_id = original_packet->name_trn_id;
2N/A packet.info = NAME_RELEASE_RESPONSE | (rcode & NAME_RCODE_MASK);
2N/A packet.qdcount = 0; /* question entries */
2N/A packet.question = NULL;
2N/A packet.ancount = 1; /* answer recs */
2N/A packet.answer = &answer;
2N/A packet.nscount = 0; /* authority recs */
2N/A packet.authority = NULL;
2N/A packet.arcount = 0; /* additional recs */
2N/A packet.additional = NULL;
2N/A
2N/A answer.name = original_packet->question->name;
2N/A answer.rr_type = NAME_QUESTION_TYPE_NB;
2N/A answer.rr_class = NAME_QUESTION_CLASS_IN;
2N/A answer.ttl = original_packet->additional->ttl;
2N/A answer.rdlength = original_packet->additional->rdlength;
2N/A answer.rdata = original_packet->additional->rdata;
2N/A
2N/A return (smb_send_name_service_packet(addr, &packet));
2N/A}
2N/A
2N/A/*
2N/A * RFC 1002 4.2.12. NAME QUERY REQUEST
2N/A */
2N/Astatic int
2N/Asmb_send_name_query_request(int bcast, struct name_question *question)
2N/A{
2N/A int rc = 0;
2N/A uint32_t retry, retries;
2N/A uint32_t timeout;
2N/A uint16_t tid;
2N/A addr_entry_t *destination;
2N/A struct name_packet packet;
2N/A int i, addr_num;
2N/A struct timespec st;
2N/A
2N/A if (bcast == BROADCAST) {
2N/A if (bcast_num == 0)
2N/A return (-1);
2N/A destination = smb_bcast_list;
2N/A addr_num = bcast_num;
2N/A retries = BCAST_REQ_RETRY_COUNT;
2N/A timeout = BCAST_REQ_RETRY_TIMEOUT;
2N/A packet.info = NAME_QUERY_REQUEST | NM_FLAGS_BROADCAST;
2N/A } else {
2N/A if (nbns_num == 0)
2N/A return (-1);
2N/A destination = smb_nbns;
2N/A addr_num = nbns_num;
2N/A retries = UCAST_REQ_RETRY_COUNT;
2N/A timeout = UCAST_REQ_RETRY_TIMEOUT;
2N/A packet.info = NAME_QUERY_REQUEST | NM_FLAGS_UNICAST;
2N/A }
2N/A packet.qdcount = 1; /* question entries */
2N/A packet.question = question;
2N/A packet.ancount = 0; /* answer recs */
2N/A packet.answer = NULL;
2N/A packet.nscount = 0; /* authority recs */
2N/A packet.authority = NULL;
2N/A packet.arcount = 0; /* additional recs */
2N/A packet.additional = NULL;
2N/A
2N/A for (i = 0; i < addr_num; i++) {
2N/A for (retry = 0; retry < retries; retry++) {
2N/A if ((destination[i].flags & ADDR_FLAG_VALID) == 0)
2N/A break;
2N/A tid = smb_netbios_name_trn_id();
2N/A packet.name_trn_id = tid;
2N/A
2N/A if (smb_send_name_service_packet(&destination[i],
2N/A &packet) >= 0) {
2N/A if ((rc = smb_netbios_process_response(tid,
2N/A &destination[i],
2N/A &packet, timeout)) != 0)
2N/A break;
2N/A }
2N/A st.tv_sec = 0;
2N/A st.tv_nsec = (timeout * 1000000);
2N/A (void) nanosleep(&st, 0);
2N/A }
2N/A }
2N/A
2N/A return (rc);
2N/A}
2N/A
2N/A/*
2N/A * RFC 1002 4.2.13. POSITIVE NAME QUERY RESPONSE
2N/A * RFC 1002 4.2.14. NEGATIVE NAME QUERY RESPONSE
2N/A */
2N/Astatic int
2N/Asmb_send_name_query_response(addr_entry_t *addr,
2N/A struct name_packet *original_packet, struct name_entry *entry,
2N/A uint16_t rcode)
2N/A{
2N/A addr_entry_t *raddr;
2N/A struct name_packet packet;
2N/A struct resource_record answer;
2N/A uint16_t attr;
2N/A unsigned char data[MAX_DATAGRAM_LENGTH];
2N/A unsigned char *scan = data;
2N/A uint32_t ret_addr;
2N/A
2N/A packet.name_trn_id = original_packet->name_trn_id;
2N/A packet.info = NAME_QUERY_RESPONSE | (rcode & NAME_RCODE_MASK);
2N/A packet.qdcount = 0; /* question entries */
2N/A packet.question = NULL;
2N/A packet.ancount = 1; /* answer recs */
2N/A packet.answer = &answer;
2N/A packet.nscount = 0; /* authority recs */
2N/A packet.authority = NULL;
2N/A packet.arcount = 0; /* additional recs */
2N/A packet.additional = NULL;
2N/A
2N/A answer.name = entry;
2N/A answer.rr_class = NAME_QUESTION_CLASS_IN;
2N/A answer.ttl = entry->addr_list.ttl;
2N/A answer.rdata = data;
2N/A if (rcode) {
2N/A answer.rr_type = NAME_RR_TYPE_NULL;
2N/A answer.rdlength = 0;
2N/A bzero(data, 6);
2N/A } else {
2N/A answer.rdlength = 0;
2N/A answer.rr_type = NAME_QUESTION_TYPE_NB;
2N/A raddr = &entry->addr_list;
2N/A scan = data;
2N/A do {
2N/A attr = entry->attributes & (NAME_ATTR_GROUP |
2N/A NAME_ATTR_OWNER_NODE_TYPE);
2N/A
2N/A BE_OUT16(scan, attr); scan += 2;
2N/A ret_addr = LE_32(raddr->sin.sin_addr.s_addr);
2N/A *scan++ = ret_addr;
2N/A *scan++ = ret_addr >> 8;
2N/A *scan++ = ret_addr >> 16;
2N/A *scan++ = ret_addr >> 24;
2N/A
2N/A answer.rdlength += 6;
2N/A raddr = raddr->forw;
2N/A } while (raddr != &entry->addr_list);
2N/A }
2N/A
2N/A return (smb_send_name_service_packet(addr, &packet));
2N/A}
2N/A
2N/A/*
2N/A * RFC 1002 4.2.18. NODE STATUS RESPONSE
2N/A */
2N/Astatic int
2N/Asmb_send_node_status_response(addr_entry_t *addr,
2N/A struct name_packet *original_packet)
2N/A{
2N/A uint32_t net_ipaddr;
2N/A int64_t max_connections;
2N/A struct arpreq arpreq;
2N/A struct name_packet packet;
2N/A struct resource_record answer;
2N/A unsigned char *scan;
2N/A unsigned char *scan_end;
2N/A unsigned char data[MAX_NETBIOS_REPLY_DATA_SIZE];
2N/A boolean_t scan_done = B_FALSE;
2N/A smb_inaddr_t ipaddr;
2N/A
2N/A bzero(&packet, sizeof (struct name_packet));
2N/A bzero(&answer, sizeof (struct resource_record));
2N/A
2N/A packet.name_trn_id = original_packet->name_trn_id;
2N/A packet.info = NODE_STATUS_RESPONSE;
2N/A packet.qdcount = 0; /* question entries */
2N/A packet.question = NULL;
2N/A packet.ancount = 1; /* answer recs */
2N/A packet.answer = &answer;
2N/A packet.nscount = 0; /* authority recs */
2N/A packet.authority = NULL;
2N/A packet.arcount = 0; /* additional recs */
2N/A packet.additional = NULL;
2N/A
2N/A answer.name = original_packet->question->name;
2N/A answer.rr_type = NAME_RR_TYPE_NBSTAT;
2N/A answer.rr_class = NAME_QUESTION_CLASS_IN;
2N/A answer.ttl = 0;
2N/A answer.rdata = data;
2N/A
2N/A scan = smb_netbios_cache_status(data, MAX_NETBIOS_REPLY_DATA_SIZE,
2N/A original_packet->question->name->scope);
2N/A
2N/A scan_end = data + MAX_NETBIOS_REPLY_DATA_SIZE;
2N/A
2N/A ipaddr.a_ipv4 = addr->sin.sin_addr.s_addr;
2N/A ipaddr.a_family = AF_INET;
2N/A if (smb_nic_is_same_subnet(&ipaddr))
2N/A net_ipaddr = addr->sin.sin_addr.s_addr;
2N/A else
2N/A net_ipaddr = 0;
2N/A
2N/A (void) smb_config_getnum(SMB_CI_MAX_CONNECTIONS, &max_connections);
2N/A
2N/A while (!scan_done) {
2N/A if ((scan + 6) >= scan_end) {
2N/A packet.info |= NAME_NM_FLAGS_TC;
2N/A break;
2N/A }
2N/A
2N/A if (net_ipaddr != 0) {
2N/A struct sockaddr_in *s_in;
2N/A int s;
2N/A
2N/A s = socket(AF_INET, SOCK_DGRAM, 0);
2N/A /* LINTED - E_BAD_PTR_CAST_ALIGN */
2N/A s_in = (struct sockaddr_in *)&arpreq.arp_pa;
2N/A s_in->sin_family = AF_INET;
2N/A s_in->sin_addr.s_addr = net_ipaddr;
2N/A if (ioctl(s, SIOCGARP, (caddr_t)&arpreq) < 0) {
2N/A bzero(scan, 6);
2N/A } else {
2N/A bcopy(&arpreq.arp_ha.sa_data, scan, 6);
2N/A }
2N/A (void) close(s);
2N/A } else {
2N/A bzero(scan, 6);
2N/A }
2N/A scan += 6;
2N/A
2N/A if ((scan + 26) >= scan_end) {
2N/A packet.info |= NAME_NM_FLAGS_TC;
2N/A break;
2N/A }
2N/A bzero(scan, 26);
2N/A scan += 26;
2N/A
2N/A if ((scan + 2) >= scan_end) {
2N/A packet.info |= NAME_NM_FLAGS_TC;
2N/A break;
2N/A }
2N/A BE_OUT16(scan, 0); scan += 2;
2N/A
2N/A if ((scan + 2) >= scan_end) {
2N/A packet.info |= NAME_NM_FLAGS_TC;
2N/A break;
2N/A }
2N/A BE_OUT16(scan, 0); scan += 2;
2N/A
2N/A if ((scan + 2) >= scan_end) {
2N/A packet.info |= NAME_NM_FLAGS_TC;
2N/A break;
2N/A }
2N/A BE_OUT16(scan, 0); scan += 2;
2N/A
2N/A if ((scan + 2) >= scan_end) {
2N/A packet.info |= NAME_NM_FLAGS_TC;
2N/A break;
2N/A }
2N/A BE_OUT16(scan, 0); scan += 2;
2N/A
2N/A if ((scan + 2) >= scan_end) {
2N/A packet.info |= NAME_NM_FLAGS_TC;
2N/A break;
2N/A }
2N/A BE_OUT16(scan, 0); scan += 2;
2N/A
2N/A if ((scan + 2) >= scan_end) {
2N/A packet.info |= NAME_NM_FLAGS_TC;
2N/A break;
2N/A }
2N/A BE_OUT16(scan, 0); scan += 2;
2N/A
2N/A if ((scan + 2) >= scan_end) {
2N/A packet.info |= NAME_NM_FLAGS_TC;
2N/A break;
2N/A }
2N/A BE_OUT16(scan, 0); scan += 2;
2N/A
2N/A if ((scan + 2) >= scan_end) {
2N/A packet.info |= NAME_NM_FLAGS_TC;
2N/A break;
2N/A }
2N/A BE_OUT16(scan, max_connections); scan += 2;
2N/A
2N/A if ((scan + 2) >= scan_end) {
2N/A packet.info |= NAME_NM_FLAGS_TC;
2N/A break;
2N/A }
2N/A
2N/A BE_OUT16(scan, 0); scan += 2;
2N/A
2N/A scan_done = B_TRUE;
2N/A }
2N/A answer.rdlength = scan - data;
2N/A return (smb_send_name_service_packet(addr, &packet));
2N/A}
2N/A
2N/Astatic int
2N/Asmb_name_Bnode_add_name(struct name_entry *name)
2N/A{
2N/A struct name_question question;
2N/A struct resource_record additional;
2N/A unsigned char data[8];
2N/A uint16_t attr;
2N/A addr_entry_t *addr;
2N/A int rc = 0;
2N/A
2N/A addr = &name->addr_list;
2N/A
2N/A do {
2N/A /* build name service packet */
2N/A question.name = name;
2N/A /*
2N/A * question.name->attributes |= NAME_NB_FLAGS_ONT_B;
2N/A * This is commented because NAME_NB_FLAGS_ONT_B is 0
2N/A */
2N/A question.question_type = NAME_QUESTION_TYPE_NB;
2N/A question.question_class = NAME_QUESTION_CLASS_IN;
2N/A
2N/A additional.name = name;
2N/A additional.rr_class = NAME_QUESTION_CLASS_IN;
2N/A additional.ttl = 0;
2N/A additional.rdata = data;
2N/A additional.rdlength = 6;
2N/A additional.rr_type = NAME_QUESTION_TYPE_NB;
2N/A attr = name->attributes & (NAME_ATTR_GROUP |
2N/A NAME_ATTR_OWNER_NODE_TYPE);
2N/A
2N/A BE_OUT16(&data[0], attr);
2N/A (void) memcpy(&data[2], &addr->sin.sin_addr.s_addr,
2N/A sizeof (uint32_t));
2N/A
2N/A rc |= smb_send_name_registration_request(BROADCAST, &question,
2N/A &additional);
2N/A addr = addr->forw;
2N/A
2N/A } while (addr != &name->addr_list);
2N/A
2N/A return (rc);
2N/A}
2N/A
2N/Astatic int
2N/Asmb_name_Bnode_find_name(struct name_entry *name)
2N/A{
2N/A struct name_question question;
2N/A
2N/A question.name = name;
2N/A question.question_type = NAME_QUESTION_TYPE_NB;
2N/A question.question_class = NAME_QUESTION_CLASS_IN;
2N/A
2N/A return (smb_send_name_query_request(BROADCAST, &question));
2N/A}
2N/A
2N/Astatic int
2N/Asmb_name_Bnode_delete_name(struct name_entry *name)
2N/A{
2N/A struct name_question question;
2N/A struct resource_record additional;
2N/A addr_entry_t *raddr;
2N/A unsigned char data[MAX_DATAGRAM_LENGTH];
2N/A unsigned char *scan = data;
2N/A uint32_t attr;
2N/A uint32_t ret_addr;
2N/A
2N/A /* build packet */
2N/A question.name = name;
2N/A question.question_type = NAME_QUESTION_TYPE_NB;
2N/A question.question_class = NAME_QUESTION_CLASS_IN;
2N/A
2N/A additional.name = name;
2N/A additional.rr_class = NAME_QUESTION_CLASS_IN;
2N/A additional.ttl = 0;
2N/A additional.rdata = data;
2N/A additional.rdlength = 0;
2N/A additional.rr_type = NAME_QUESTION_TYPE_NB;
2N/A raddr = &name->addr_list;
2N/A scan = data;
2N/A do {
2N/A attr = name->attributes & (NAME_ATTR_GROUP |
2N/A NAME_ATTR_OWNER_NODE_TYPE);
2N/A
2N/A BE_OUT16(scan, attr); scan += 2;
2N/A ret_addr = LE_32(raddr->sin.sin_addr.s_addr);
2N/A *scan++ = ret_addr;
2N/A *scan++ = ret_addr >> 8;
2N/A *scan++ = ret_addr >> 16;
2N/A *scan++ = ret_addr >> 24;
2N/A
2N/A additional.rdlength += 6;
2N/A } while (raddr != &name->addr_list);
2N/A
2N/A return (smb_send_name_release_request_and_demand(BROADCAST,
2N/A &question, &additional));
2N/A}
2N/A
2N/Astatic int
2N/Asmb_name_Pnode_add_name(struct name_entry *name)
2N/A{
2N/A struct name_question question;
2N/A struct resource_record additional;
2N/A unsigned char data[8];
2N/A uint16_t attr;
2N/A addr_entry_t *addr;
2N/A int rc = 0;
2N/A
2N/A /* build packet */
2N/A addr = &name->addr_list;
2N/A do {
2N/A question.name = name;
2N/A question.question_type = NAME_QUESTION_TYPE_NB;
2N/A question.question_class = NAME_QUESTION_CLASS_IN;
2N/A
2N/A additional.name = name;
2N/A additional.rr_class = NAME_QUESTION_CLASS_IN;
2N/A additional.ttl = 0;
2N/A additional.rdata = data;
2N/A additional.rdlength = 6;
2N/A additional.rr_type = NAME_QUESTION_TYPE_NB;
2N/A attr = name->attributes &
2N/A (NAME_ATTR_GROUP | NAME_ATTR_OWNER_NODE_TYPE);
2N/A
2N/A BE_OUT16(&data[0], attr);
2N/A (void) memcpy(&data[2], &addr->sin.sin_addr.s_addr,
2N/A sizeof (uint32_t));
2N/A
2N/A rc |= smb_send_name_registration_request(UNICAST, &question,
2N/A &additional);
2N/A
2N/A addr = addr->forw;
2N/A
2N/A } while (addr != &name->addr_list);
2N/A
2N/A return (rc);
2N/A}
2N/A
2N/Astatic int
2N/Asmb_name_Pnode_refresh_name(struct name_entry *name)
2N/A{
2N/A struct name_question question;
2N/A struct resource_record additional;
2N/A unsigned char data[8];
2N/A uint16_t attr;
2N/A addr_entry_t *addr;
2N/A int rc = 0;
2N/A
2N/A /* build packet */
2N/A addr = &name->addr_list;
2N/A do {
2N/A question.name = name;
2N/A question.question_type = NAME_QUESTION_TYPE_NB;
2N/A question.question_class = NAME_QUESTION_CLASS_IN;
2N/A
2N/A additional.name = name;
2N/A additional.rr_class = NAME_QUESTION_CLASS_IN;
2N/A additional.ttl = 0;
2N/A additional.rdata = data;
2N/A additional.rdlength = 6;
2N/A additional.rr_type = NAME_QUESTION_TYPE_NB;
2N/A attr = name->attributes &
2N/A (NAME_ATTR_GROUP | NAME_ATTR_OWNER_NODE_TYPE);
2N/A
2N/A BE_OUT16(&data[0], attr);
2N/A (void) memcpy(&data[2], &addr->sin.sin_addr.s_addr,
2N/A sizeof (uint32_t));
2N/A
2N/A rc |= smb_send_name_refresh_request(UNICAST, &question,
2N/A &additional, 1);
2N/A
2N/A addr = addr->forw;
2N/A } while (addr != &name->addr_list);
2N/A
2N/A return (rc);
2N/A}
2N/A
2N/Astatic int
2N/Asmb_name_Pnode_find_name(struct name_entry *name)
2N/A{
2N/A struct name_question question;
2N/A
2N/A /*
2N/A * Host initiated processing for a P node
2N/A */
2N/A question.name = name;
2N/A question.name->attributes |= NAME_NB_FLAGS_ONT_P;
2N/A question.question_type = NAME_QUESTION_TYPE_NB;
2N/A question.question_class = NAME_QUESTION_CLASS_IN;
2N/A
2N/A return (smb_send_name_query_request(UNICAST, &question));
2N/A}
2N/A
2N/Astatic int
2N/Asmb_name_Pnode_delete_name(struct name_entry *name)
2N/A{
2N/A struct name_question question;
2N/A struct resource_record additional;
2N/A addr_entry_t *raddr;
2N/A unsigned char data[MAX_DATAGRAM_LENGTH];
2N/A unsigned char *scan = data;
2N/A uint32_t attr;
2N/A uint32_t ret_addr;
2N/A
2N/A /* build packet */
2N/A question.name = name;
2N/A question.name->attributes |= NAME_NB_FLAGS_ONT_P;
2N/A question.question_type = NAME_QUESTION_TYPE_NB;
2N/A question.question_class = NAME_QUESTION_CLASS_IN;
2N/A
2N/A additional.name = name;
2N/A additional.rr_class = NAME_QUESTION_CLASS_IN;
2N/A additional.ttl = 0;
2N/A additional.rdata = data;
2N/A additional.rdlength = 0;
2N/A additional.rr_type = NAME_QUESTION_TYPE_NB;
2N/A raddr = &name->addr_list;
2N/A do {
2N/A scan = data;
2N/A attr = name->attributes & (NAME_ATTR_GROUP |
2N/A NAME_ATTR_OWNER_NODE_TYPE);
2N/A
2N/A BE_OUT16(scan, attr); scan += 2;
2N/A ret_addr = LE_32(raddr->sin.sin_addr.s_addr);
2N/A *scan++ = ret_addr;
2N/A *scan++ = ret_addr >> 8;
2N/A *scan++ = ret_addr >> 16;
2N/A *scan++ = ret_addr >> 24;
2N/A
2N/A additional.rdlength = 6;
2N/A raddr = raddr->forw;
2N/A (void) smb_send_name_release_request_and_demand(UNICAST,
2N/A &question, &additional);
2N/A } while (raddr != &name->addr_list);
2N/A
2N/A return (1);
2N/A}
2N/A
2N/Astatic int
2N/Asmb_name_Mnode_add_name(struct name_entry *name)
2N/A{
2N/A if (smb_name_Bnode_add_name(name) > 0) {
2N/A if (nbns_num == 0)
2N/A return (1); /* No name server configured */
2N/A
2N/A return (smb_name_Pnode_add_name(name));
2N/A }
2N/A return (-1);
2N/A}
2N/A
2N/Astatic int
2N/Asmb_name_Hnode_add_name(struct name_entry *name)
2N/A{
2N/A if (nbns_num > 0) {
2N/A if (smb_name_Pnode_add_name(name) == 1)
2N/A return (1);
2N/A }
2N/A
2N/A return (smb_name_Bnode_add_name(name));
2N/A}
2N/A
2N/Astatic int
2N/Asmb_name_Mnode_find_name(struct name_entry *name)
2N/A{
2N/A if (smb_name_Bnode_find_name(name) == 1)
2N/A return (1);
2N/A
2N/A if (nbns_num == 0)
2N/A return (1); /* No name server configured */
2N/A
2N/A return (smb_name_Pnode_find_name(name));
2N/A}
2N/A
2N/Astatic int
2N/Asmb_name_Hnode_find_name(struct name_entry *name)
2N/A{
2N/A if (nbns_num > 0)
2N/A if (smb_name_Pnode_find_name(name) == 1)
2N/A return (1);
2N/A
2N/A return (smb_name_Bnode_find_name(name));
2N/A}
2N/A
2N/Astatic int
2N/Asmb_name_Mnode_delete_name(struct name_entry *name)
2N/A{
2N/A (void) smb_name_Bnode_delete_name(name);
2N/A
2N/A if (nbns_num == 0)
2N/A return (-1); /* No name server configured */
2N/A
2N/A if (smb_name_Pnode_delete_name(name) > 0)
2N/A return (1);
2N/A
2N/A return (-1);
2N/A}
2N/A
2N/Astatic int
2N/Asmb_name_Hnode_delete_name(struct name_entry *name)
2N/A{
2N/A if (nbns_num > 0)
2N/A if (smb_name_Pnode_delete_name(name) > 0)
2N/A return (1);
2N/A
2N/A return (smb_name_Bnode_delete_name(name));
2N/A}
2N/A
2N/Astatic void
2N/Asmb_name_process_Bnode_packet(struct name_packet *packet, addr_entry_t *addr)
2N/A{
2N/A struct name_entry *name;
2N/A struct name_entry *entry;
2N/A struct name_question *question;
2N/A struct resource_record *additional;
2N/A
2N/A question = packet->question;
2N/A additional = packet->additional;
2N/A
2N/A switch (packet->info & NAME_OPCODE_OPCODE_MASK) {
2N/A case NAME_OPCODE_REFRESH:
2N/A /* Guard against malformed packets */
2N/A if ((question == 0) || (additional == 0))
2N/A break;
2N/A if (additional->name->addr_list.sin.sin_addr.s_addr == 0)
2N/A break;
2N/A
2N/A name = question->name;
2N/A name->addr_list.ttl = additional->ttl;
2N/A name->attributes = additional->name->attributes;
2N/A name->addr_list.sin = additional->name->addr_list.sin;
2N/A name->addr_list.forw = name->addr_list.back = &name->addr_list;
2N/A
2N/A if ((entry = smb_netbios_cache_lookup_addr(name)) != 0) {
2N/A smb_netbios_cache_update_entry(entry, question->name);
2N/A smb_netbios_cache_unlock_entry(entry);
2N/A }
2N/A else
2N/A (void) smb_netbios_cache_insert(question->name);
2N/A break;
2N/A
2N/A case NAME_OPCODE_QUERY:
2N/A /*
2N/A * This opcode covers both NAME_QUERY_REQUEST and
2N/A * NODE_STATUS_REQUEST. They can be distinguished
2N/A * based on the type of question entry.
2N/A */
2N/A
2N/A /* All query requests have to have question entry */
2N/A if (question == 0)
2N/A break;
2N/A
2N/A if (question->question_type == NAME_QUESTION_TYPE_NB) {
2N/A name = question->name;
2N/A if ((entry = smb_netbios_cache_lookup(name)) != 0) {
2N/A (void) smb_send_name_query_response(addr,
2N/A packet, entry, 0);
2N/A smb_netbios_cache_unlock_entry(entry);
2N/A }
2N/A }
2N/A else
2N/A if (question->question_type == NAME_QUESTION_TYPE_NBSTAT) {
2N/A /*
2N/A * Name of "*" may be used to force node to
2N/A * divulge status for administrative purposes
2N/A */
2N/A name = question->name;
2N/A entry = 0;
2N/A if (NETBIOS_NAME_IS_STAR(name->name) ||
2N/A ((entry = smb_netbios_cache_lookup(name)) != 0)) {
2N/A if (entry)
2N/A smb_netbios_cache_unlock_entry(entry);
2N/A /*
2N/A * send only those names that are
2N/A * in the same scope as the scope
2N/A * field in the request packet
2N/A */
2N/A (void) smb_send_node_status_response(addr,
2N/A packet);
2N/A }
2N/A }
2N/A break;
2N/A
2N/A default:
2N/A break;
2N/A }
2N/A}
2N/A
2N/Astatic void
2N/Asmb_name_process_Pnode_packet(struct name_packet *packet, addr_entry_t *addr)
2N/A{
2N/A struct name_entry *name;
2N/A struct name_entry *entry;
2N/A struct name_question *question;
2N/A struct resource_record *additional;
2N/A
2N/A question = packet->question;
2N/A additional = packet->additional;
2N/A
2N/A if (packet->info & NAME_NM_FLAGS_B) {
2N/A /*
2N/A * always ignore UDP broadcast packets
2N/A */
2N/A return;
2N/A }
2N/A
2N/A switch (packet->info & NAME_OPCODE_OPCODE_MASK) {
2N/A case NAME_OPCODE_REFRESH:
2N/A /* Guard against malformed packets */
2N/A if ((question == 0) || (additional == 0))
2N/A break;
2N/A if (additional->name->addr_list.sin.sin_addr.s_addr == 0)
2N/A break;
2N/A
2N/A name = question->name;
2N/A name->addr_list.ttl = additional->ttl;
2N/A name->attributes = additional->name->attributes;
2N/A name->addr_list.sin = additional->name->addr_list.sin;
2N/A name->addr_list.forw = name->addr_list.back = &name->addr_list;
2N/A
2N/A if ((entry = smb_netbios_cache_lookup(name)) != 0) {
2N/A smb_netbios_cache_update_entry(entry, name);
2N/A smb_netbios_cache_unlock_entry(entry);
2N/A }
2N/A else
2N/A (void) smb_netbios_cache_insert(name);
2N/A
2N/A (void) smb_send_name_registration_response(addr, packet, 0);
2N/A break;
2N/A
2N/A case NAME_OPCODE_QUERY:
2N/A /*
2N/A * This opcode covers both NAME_QUERY_REQUEST and
2N/A * NODE_STATUS_REQUEST. They can be distinguished
2N/A * based on the type of question entry.
2N/A */
2N/A
2N/A /* All query requests have to have question entry */
2N/A if (question == 0)
2N/A break;
2N/A
2N/A if (question->question_type == NAME_QUESTION_TYPE_NB) {
2N/A name = question->name;
2N/A if ((entry = smb_netbios_cache_lookup(name)) != 0) {
2N/A /*
2N/A * send response to the IP address and port
2N/A * number from which the request was received.
2N/A */
2N/A (void) smb_send_name_query_response(addr,
2N/A packet, entry, 0);
2N/A smb_netbios_cache_unlock_entry(entry);
2N/A } else {
2N/A /*
2N/A * send response to the requestor
2N/A */
2N/A (void) smb_send_name_query_response(addr,
2N/A packet, name, RCODE_NAM_ERR);
2N/A }
2N/A }
2N/A else
2N/A if (question->question_type == NAME_QUESTION_TYPE_NBSTAT) {
2N/A /*
2N/A * Name of "*" may be used to force node to
2N/A * divulge status for administrative purposes
2N/A */
2N/A name = question->name;
2N/A entry = 0;
2N/A if (NETBIOS_NAME_IS_STAR(name->name) ||
2N/A ((entry = smb_netbios_cache_lookup(name)) != 0)) {
2N/A /*
2N/A * send only those names that are
2N/A * in the same scope as the scope
2N/A * field in the request packet
2N/A */
2N/A if (entry)
2N/A smb_netbios_cache_unlock_entry(entry);
2N/A (void) smb_send_node_status_response(addr,
2N/A packet);
2N/A }
2N/A }
2N/A break;
2N/A
2N/A default:
2N/A break;
2N/A }
2N/A}
2N/A
2N/Astatic void
2N/Asmb_name_process_Mnode_packet(struct name_packet *packet, addr_entry_t *addr)
2N/A{
2N/A if (packet->info & NAME_NM_FLAGS_B)
2N/A smb_name_process_Bnode_packet(packet, addr);
2N/A else
2N/A smb_name_process_Pnode_packet(packet, addr);
2N/A}
2N/A
2N/Astatic void
2N/Asmb_name_process_Hnode_packet(struct name_packet *packet, addr_entry_t *addr)
2N/A{
2N/A if (packet->info & NAME_NM_FLAGS_B)
2N/A smb_name_process_Bnode_packet(packet, addr);
2N/A else
2N/A smb_name_process_Pnode_packet(packet, addr);
2N/A}
2N/A
2N/A
2N/A/*
2N/A * smb_netbios_name_tick
2N/A *
2N/A * Called once a second to handle name server timeouts.
2N/A */
2N/Avoid
2N/Asmb_netbios_name_tick(void)
2N/A{
2N/A struct name_entry *name;
2N/A struct name_entry *entry;
2N/A
2N/A (void) mutex_lock(&refresh_queue.mtx);
2N/A smb_netbios_cache_refresh(&refresh_queue);
2N/A
2N/A while ((name = refresh_queue.head.forw) != &refresh_queue.head) {
2N/A QUEUE_CLIP(name);
2N/A if (IS_LOCAL(name->attributes)) {
2N/A if (IS_UNIQUE(name->attributes)) {
2N/A (void) smb_name_Pnode_refresh_name(name);
2N/A }
2N/A } else {
2N/A entry = smb_name_find_name(name);
2N/A smb_name_unlock_name(entry);
2N/A }
2N/A free(name);
2N/A }
2N/A (void) mutex_unlock(&refresh_queue.mtx);
2N/A
2N/A smb_netbios_cache_reset_ttl();
2N/A}
2N/A
2N/A/*
2N/A * smb_name_find_name
2N/A *
2N/A * Lookup name cache for the given name.
2N/A * If it's not in the cache it'll send a
2N/A * name query request and then lookup the
2N/A * cache again. Note that if a name is
2N/A * returned it's locked and called MUST
2N/A * unlock it by calling smb_name_unlock_name()
2N/A */
2N/Astruct name_entry *
2N/Asmb_name_find_name(struct name_entry *name)
2N/A{
2N/A struct name_entry *result;
2N/A
2N/A if ((result = smb_netbios_cache_lookup(name)) == 0) {
2N/A switch (smb_node_type) {
2N/A case 'B':
2N/A (void) smb_name_Bnode_find_name(name);
2N/A break;
2N/A case 'P':
2N/A (void) smb_name_Pnode_find_name(name);
2N/A break;
2N/A case 'M':
2N/A (void) smb_name_Mnode_find_name(name);
2N/A break;
2N/A case 'H':
2N/A default:
2N/A (void) smb_name_Hnode_find_name(name);
2N/A break;
2N/A }
2N/A return (smb_netbios_cache_lookup(name));
2N/A }
2N/A
2N/A return (result);
2N/A}
2N/A
2N/Avoid
2N/Asmb_name_unlock_name(struct name_entry *name)
2N/A{
2N/A smb_netbios_cache_unlock_entry(name);
2N/A}
2N/A
2N/Aint
2N/Asmb_name_add_name(struct name_entry *name)
2N/A{
2N/A int rc = 1;
2N/A
2N/A smb_netbios_name_logf(name);
2N/A
2N/A switch (smb_node_type) {
2N/A case 'B':
2N/A rc = smb_name_Bnode_add_name(name);
2N/A break;
2N/A case 'P':
2N/A rc = smb_name_Pnode_add_name(name);
2N/A break;
2N/A case 'M':
2N/A rc = smb_name_Mnode_add_name(name);
2N/A break;
2N/A case 'H':
2N/A default:
2N/A rc = smb_name_Hnode_add_name(name);
2N/A break;
2N/A }
2N/A
2N/A if (rc >= 0)
2N/A (void) smb_netbios_cache_insert(name);
2N/A
2N/A return (rc);
2N/A}
2N/A
2N/Aint
2N/Asmb_name_delete_name(struct name_entry *name)
2N/A{
2N/A int rc;
2N/A unsigned char type;
2N/A
2N/A type = name->name[15];
2N/A if ((type != NBT_WKSTA) && (type != NBT_SERVER)) {
2N/A syslog(LOG_DEBUG, "nbns: name delete bad type (0x%02x)", type);
2N/A smb_netbios_name_logf(name);
2N/A name->attributes &= ~NAME_ATTR_LOCAL;
2N/A return (-1);
2N/A }
2N/A
2N/A smb_netbios_cache_delete(name);
2N/A
2N/A switch (smb_node_type) {
2N/A case 'B':
2N/A rc = smb_name_Bnode_delete_name(name);
2N/A break;
2N/A case 'P':
2N/A rc = smb_name_Pnode_delete_name(name);
2N/A break;
2N/A case 'M':
2N/A rc = smb_name_Mnode_delete_name(name);
2N/A break;
2N/A case 'H':
2N/A default:
2N/A rc = smb_name_Hnode_delete_name(name);
2N/A break;
2N/A }
2N/A
2N/A if (rc > 0)
2N/A return (0);
2N/A
2N/A return (-1);
2N/A}
2N/A
2N/Atypedef struct {
2N/A addr_entry_t *addr;
2N/A char *buf;
2N/A int length;
2N/A} worker_param_t;
2N/A
2N/A/*
2N/A * smb_netbios_worker
2N/A *
2N/A * Process incoming request/response packets for Netbios
2N/A * name service (on port 138).
2N/A */
2N/Avoid *
2N/Asmb_netbios_worker(void *arg)
2N/A{
2N/A worker_param_t *p = (worker_param_t *)arg;
2N/A addr_entry_t *addr = p->addr;
2N/A struct name_packet *packet;
2N/A
2N/A if ((packet = smb_name_buf_to_packet(p->buf, p->length)) != NULL) {
2N/A if (packet->info & NAME_OPCODE_R) {
2N/A /* Reply packet */
2N/A smb_reply_ready(packet, addr);
2N/A free(p->buf);
2N/A free(p);
2N/A return (NULL);
2N/A }
2N/A
2N/A /* Request packet */
2N/A switch (smb_node_type) {
2N/A case 'B':
2N/A smb_name_process_Bnode_packet(packet, addr);
2N/A break;
2N/A case 'P':
2N/A smb_name_process_Pnode_packet(packet, addr);
2N/A break;
2N/A case 'M':
2N/A smb_name_process_Mnode_packet(packet, addr);
2N/A break;
2N/A case 'H':
2N/A default:
2N/A smb_name_process_Hnode_packet(packet, addr);
2N/A break;
2N/A }
2N/A
2N/A if (packet->answer)
2N/A smb_netbios_name_freeaddrs(packet->answer->name);
2N/A free(packet);
2N/A } else {
2N/A syslog(LOG_ERR, "nbns: packet decode failed");
2N/A }
2N/A
2N/A free(addr);
2N/A free(p->buf);
2N/A free(p);
2N/A return (NULL);
2N/A}
2N/A
2N/A/*
2N/A * Configure the node type. If a WINS server has been specified,
2N/A * act like an H-node. Otherwise, behave like a B-node.
2N/A */
2N/Astatic void
2N/Asmb_netbios_node_config(void)
2N/A{
2N/A static smb_cfg_id_t wins[SMB_PI_MAX_WINS] = {
2N/A SMB_CI_WINS_SRV1,
2N/A SMB_CI_WINS_SRV2
2N/A };
2N/A char ipstr[16];
2N/A uint32_t ipaddr;
2N/A int i;
2N/A
2N/A smb_node_type = SMB_NODETYPE_B;
2N/A nbns_num = 0;
2N/A bzero(smb_nbns, sizeof (addr_entry_t) * SMB_PI_MAX_WINS);
2N/A
2N/A for (i = 0; i < SMB_PI_MAX_WINS; ++i) {
2N/A ipstr[0] = '\0';
2N/A (void) smb_config_getstr(wins[i], ipstr, sizeof (ipstr));
2N/A
2N/A if ((ipaddr = inet_addr(ipstr)) == INADDR_NONE)
2N/A continue;
2N/A
2N/A smb_node_type = SMB_NODETYPE_H;
2N/A smb_nbns[nbns_num].flags = ADDR_FLAG_VALID;
2N/A smb_nbns[nbns_num].sinlen = sizeof (struct sockaddr_in);
2N/A smb_nbns[nbns_num].sin.sin_family = AF_INET;
2N/A smb_nbns[nbns_num].sin.sin_addr.s_addr = ipaddr;
2N/A smb_nbns[nbns_num].sin.sin_port = htons(IPPORT_NETBIOS_NS);
2N/A nbns_num++;
2N/A }
2N/A}
2N/A
2N/Astatic void
2N/Asmb_netbios_name_registration(void)
2N/A{
2N/A nbcache_iter_t nbc_iter;
2N/A struct name_entry *name;
2N/A int rc;
2N/A
2N/A rc = smb_netbios_cache_getfirst(&nbc_iter);
2N/A while (rc == 0) {
2N/A name = nbc_iter.nbc_entry;
2N/A (void) smb_netbios_name_logf(name);
2N/A if (IS_UNIQUE(name->attributes) && IS_LOCAL(name->attributes)) {
2N/A switch (smb_node_type) {
2N/A case SMB_NODETYPE_B:
2N/A (void) smb_name_Bnode_add_name(name);
2N/A break;
2N/A case SMB_NODETYPE_P:
2N/A (void) smb_name_Pnode_add_name(name);
2N/A break;
2N/A case SMB_NODETYPE_M:
2N/A (void) smb_name_Mnode_add_name(name);
2N/A break;
2N/A case SMB_NODETYPE_H:
2N/A default:
2N/A (void) smb_name_Hnode_add_name(name);
2N/A break;
2N/A }
2N/A }
2N/A free(name);
2N/A rc = smb_netbios_cache_getnext(&nbc_iter);
2N/A }
2N/A}
2N/A
2N/A/*
2N/A * Sends name registration requests on all configured network links.
2N/A * Name registration requests are sent to claim ownership of a name.
2N/A * If WINS server(s) are configured, the request is directly sent
2N/A * to those servers, if not the request is broadcasted.
2N/A *
2N/A * Currently the code cannot scale well to handle hunderds of network
2N/A * links (which can be created using VLANs) so a maximum is enforced.
2N/A * If the number of configured links is greater than a threshold then
2N/A * name registration broadcast will not be done on any links.
2N/A *
2N/A * Note that the node configuration must be setup before calling
2N/A * smb_init_name_struct().
2N/A */
2N/Avoid
2N/Asmb_netbios_name_config(void)
2N/A{
2N/A addr_entry_t *bcast_entry;
2N/A struct name_entry name;
2N/A smb_niciter_t ni;
2N/A int rc;
2N/A int64_t citem;
2N/A int bcast_max;
2N/A boolean_t bcast_enable;
2N/A
2N/A (void) mutex_lock(&nbt_name_config_mtx);
2N/A rc = smb_config_getnum(SMB_CI_NBNS_BCAST_MAX, &citem);
2N/A if (rc != SMBD_SMF_OK)
2N/A bcast_max = NBNS_NBCAST_DEFAULT;
2N/A else if (citem > NBNS_NBCAST_MAX)
2N/A bcast_max = NBNS_NBCAST_MAX;
2N/A else
2N/A bcast_max = (int)citem;
2N/A
2N/A smb_netbios_node_config();
2N/A
2N/A bcast_num = 0;
2N/A bzero(smb_bcast_list, sizeof (addr_entry_t) * NBNS_NBCAST_MAX);
2N/A bcast_enable = (smb_nic_getnum(NULL) <= bcast_max);
2N/A
2N/A rc = smb_nic_getfirst(&ni);
2N/A while (rc == SMB_NIC_SUCCESS) {
2N/A if ((ni.ni_nic.nic_smbflags & SMB_NICF_NBEXCL) ||
2N/A (ni.ni_nic.nic_smbflags & SMB_NICF_ALIAS)) {
2N/A rc = smb_nic_getnext(&ni);
2N/A continue;
2N/A }
2N/A
2N/A smb_init_name_struct((unsigned char *)ni.ni_nic.nic_host,
2N/A NBT_WKSTA, 0, ni.ni_nic.nic_ip.a_ipv4,
2N/A htons(IPPORT_NETBIOS_DGM),
2N/A NAME_ATTR_UNIQUE, NAME_ATTR_LOCAL, &name);
2N/A (void) smb_netbios_cache_insert(&name);
2N/A
2N/A smb_init_name_struct((unsigned char *)ni.ni_nic.nic_host,
2N/A NBT_SERVER, 0, ni.ni_nic.nic_ip.a_ipv4,
2N/A htons(IPPORT_NETBIOS_DGM),
2N/A NAME_ATTR_UNIQUE, NAME_ATTR_LOCAL, &name);
2N/A (void) smb_netbios_cache_insert(&name);
2N/A
2N/A if (bcast_enable && (bcast_num < bcast_max)) {
2N/A bcast_entry = &smb_bcast_list[bcast_num];
2N/A bcast_entry->flags = ADDR_FLAG_VALID;
2N/A bcast_entry->attributes = NAME_ATTR_LOCAL;
2N/A bcast_entry->sinlen = sizeof (struct sockaddr_in);
2N/A bcast_entry->sin.sin_family = AF_INET;
2N/A bcast_entry->sin.sin_port = htons(IPPORT_NETBIOS_NS);
2N/A bcast_entry->sin.sin_addr.s_addr = ni.ni_nic.nic_bcast;
2N/A bcast_num++;
2N/A }
2N/A
2N/A rc = smb_nic_getnext(&ni);
2N/A }
2N/A
2N/A smb_netbios_name_registration();
2N/A (void) mutex_unlock(&nbt_name_config_mtx);
2N/A}
2N/A
2N/Astatic void
2N/Asmb_netbios_name_unconfig(void)
2N/A{
2N/A struct name_entry *name;
2N/A
2N/A (void) mutex_lock(&nbt_name_config_mtx);
2N/A (void) mutex_lock(&delete_queue.mtx);
2N/A smb_netbios_cache_delete_locals(&delete_queue);
2N/A
2N/A while ((name = delete_queue.head.forw) != &delete_queue.head) {
2N/A QUEUE_CLIP(name);
2N/A (void) smb_name_delete_name(name);
2N/A free(name);
2N/A }
2N/A (void) mutex_unlock(&delete_queue.mtx);
2N/A (void) mutex_unlock(&nbt_name_config_mtx);
2N/A}
2N/A
2N/Avoid
2N/Asmb_netbios_name_reconfig(void)
2N/A{
2N/A smb_netbios_name_unconfig();
2N/A smb_netbios_name_config();
2N/A}
2N/A
2N/A/*
2N/A * NetBIOS Name Service (port 137)
2N/A */
2N/A/*ARGSUSED*/
2N/Avoid *
2N/Asmb_netbios_name_service(void *arg)
2N/A{
2N/A struct sockaddr_in sin;
2N/A addr_entry_t *addr;
2N/A int len;
2N/A int flag = 1;
2N/A char *buf;
2N/A worker_param_t *worker_param;
2N/A smb_inaddr_t ipaddr;
2N/A int rc;
2N/A
2N/A /*
2N/A * Initialize reply_queue
2N/A */
2N/A bzero(&reply_queue, sizeof (reply_queue));
2N/A reply_queue.forw = reply_queue.back = &reply_queue;
2N/A
2N/A if ((name_sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
2N/A syslog(LOG_ERR, "nbns: socket failed: %m");
2N/A smb_netbios_event(NETBIOS_EVENT_ERROR);
2N/A return (NULL);
2N/A }
2N/A
2N/A flag = 1;
2N/A (void) setsockopt(name_sock, SOL_SOCKET, SO_REUSEADDR, &flag,
2N/A sizeof (flag));
2N/A flag = 1;
2N/A (void) setsockopt(name_sock, SOL_SOCKET, SO_BROADCAST, &flag,
2N/A sizeof (flag));
2N/A
2N/A bzero(&sin, sizeof (struct sockaddr_in));
2N/A sin.sin_family = AF_INET;
2N/A sin.sin_port = htons(IPPORT_NETBIOS_NS);
2N/A if (bind(name_sock, (struct sockaddr *)&sin, sizeof (sin)) != 0) {
2N/A syslog(LOG_ERR, "nbns: bind(%d) failed: %m",
2N/A IPPORT_NETBIOS_NS);
2N/A (void) close(name_sock);
2N/A smb_netbios_event(NETBIOS_EVENT_ERROR);
2N/A return (NULL);
2N/A }
2N/A
2N/A smb_netbios_event(NETBIOS_EVENT_NS_START);
2N/A
2N/A while (smb_netbios_running()) {
2N/A buf = calloc(1, MAX_DATAGRAM_LENGTH);
2N/A addr = calloc(1, sizeof (addr_entry_t));
2N/A if ((buf == NULL) || (addr == NULL)) {
2N/A /* Sleep for 10 seconds and try again */
2N/A free(addr);
2N/A free(buf);
2N/A smb_netbios_sleep(10);
2N/A continue;
2N/A }
2N/Aignore: bzero(addr, sizeof (addr_entry_t));
2N/A addr->sinlen = sizeof (addr->sin);
2N/A addr->forw = addr->back = addr;
2N/A
2N/A if ((len = recvfrom(name_sock, buf, MAX_DATAGRAM_LENGTH,
2N/A 0, (struct sockaddr *)&addr->sin, &addr->sinlen)) < 0) {
2N/A if (errno == ENOMEM || errno == ENFILE ||
2N/A errno == EMFILE) {
2N/A /* Sleep for 10 seconds and try again */
2N/A free(buf);
2N/A free(addr);
2N/A smb_netbios_sleep(10);
2N/A continue;
2N/A }
2N/A syslog(LOG_ERR, "nbns: recvfrom failed: %m");
2N/A free(buf);
2N/A free(addr);
2N/A smb_netbios_event(NETBIOS_EVENT_ERROR);
2N/A goto shutdown;
2N/A }
2N/A
2N/A /* Ignore any incoming packets from myself... */
2N/A
2N/A ipaddr.a_ipv4 = addr->sin.sin_addr.s_addr;
2N/A ipaddr.a_family = AF_INET;
2N/A if (smb_nic_is_local(&ipaddr))
2N/A goto ignore;
2N/A
2N/A /*
2N/A * Launch a netbios worker to process the received packet.
2N/A */
2N/A worker_param = calloc(1, sizeof (worker_param_t));
2N/A if (worker_param) {
2N/A pthread_t worker;
2N/A pthread_attr_t tattr;
2N/A
2N/A worker_param->addr = addr;
2N/A worker_param->buf = buf;
2N/A worker_param->length = len;
2N/A
2N/A (void) pthread_attr_init(&tattr);
2N/A (void) pthread_attr_setdetachstate(&tattr,
2N/A PTHREAD_CREATE_DETACHED);
2N/A rc = pthread_create(&worker, &tattr,
2N/A smb_netbios_worker, worker_param);
2N/A (void) pthread_attr_destroy(&tattr);
2N/A if (rc != 0) {
2N/A free(worker_param);
2N/A free(buf);
2N/A free(addr);
2N/A }
2N/A } else {
2N/A free(buf);
2N/A free(addr);
2N/A }
2N/A }
2N/A
2N/Ashutdown:
2N/A smb_netbios_event(NETBIOS_EVENT_NS_STOP);
2N/A smb_netbios_wait(NETBIOS_EVENT_BROWSER_STOP);
2N/A
2N/A if (!smb_netbios_error())
2N/A smb_netbios_name_unconfig();
2N/A
2N/A (void) close(name_sock);
2N/A return (NULL);
2N/A}