2 Unix SMB/CIFS implementation.
3 NBT netbios routines and daemon - version 2
4 Copyright (C) Andrew Tridgell 1994-1998
5 Copyright (C) Luke Kenneth Casson Leighton 1994-1998
6 Copyright (C) Jeremy Allison 1994-2003
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "nmbd/nmbd.h"
24 #include "../lib/util/select.h"
25 #include "system/select.h"
26 #include "libsmb/libsmb.h"
29 extern int ClientDGRAM;
30 extern int global_nmb_port;
32 bool rescan_listen_set = False;
34 static struct nb_packet_server *packet_server;
36 bool nmbd_init_packet_server(void)
40 status = nb_packet_server_create(
41 NULL, nmbd_event_context(),
42 lp_parm_int(-1, "nmbd", "unexpected_clients", 200),
44 if (!NT_STATUS_IS_OK(status)) {
45 DEBUG(0, ("ERROR: nb_packet_server_create failed: %s\n",
53 /*******************************************************************
54 The global packet linked-list. Incoming entries are
55 added to the end of this list. It is supposed to remain fairly
56 short so we won't bother with an end pointer.
57 ******************************************************************/
59 static struct packet_struct *packet_queue = NULL;
61 /***************************************************************************
62 Utility function to find the specific fd to send a packet out on.
63 **************************************************************************/
65 static int find_subnet_fd_for_address( struct in_addr local_ip )
67 struct subnet_record *subrec;
69 for( subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec))
70 if(ip_equal_v4(local_ip, subrec->myip))
71 return subrec->nmb_sock;
76 /***************************************************************************
77 Utility function to find the specific fd to send a mailslot packet out on.
78 **************************************************************************/
80 static int find_subnet_mailslot_fd_for_address( struct in_addr local_ip )
82 struct subnet_record *subrec;
84 for( subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec))
85 if(ip_equal_v4(local_ip, subrec->myip))
86 return subrec->dgram_sock;
91 /***************************************************************************
92 Get/Set problematic nb_flags as network byte order 16 bit int.
93 **************************************************************************/
95 uint16_t get_nb_flags(char *buf)
97 return ((((uint16_t)*buf)&0xFFFF) & NB_FLGMSK);
100 void set_nb_flags(char *buf, uint16_t nb_flags)
102 *buf++ = ((nb_flags & NB_FLGMSK) & 0xFF);
106 /***************************************************************************
107 Dumps out the browse packet data.
108 **************************************************************************/
110 static void debug_browse_data(const char *outbuf, int len)
114 DEBUG( 4, ( "debug_browse_data():\n" ) );
115 for (i = 0; i < len; i+= 16) {
116 DEBUGADD( 4, ( "%3x char ", i ) );
118 for (j = 0; j < 16; j++) {
124 if (x < 32 || x > 127)
127 DEBUGADD( 4, ( "%c", x ) );
130 DEBUGADD( 4, ( "%*s hex", 16-j, "" ) );
132 for (j = 0; j < 16; j++) {
135 DEBUGADD( 4, ( " %02x", (unsigned char)outbuf[i+j] ) );
138 DEBUGADD( 4, ("\n") );
142 /***************************************************************************
143 Generates the unique transaction identifier
144 **************************************************************************/
146 static uint16_t name_trn_id=0;
148 static uint16_t generate_name_trn_id(void)
151 name_trn_id = ((unsigned)time(NULL)%(unsigned)0x7FFF) + ((unsigned)getpid()%(unsigned)100);
153 name_trn_id = (name_trn_id+1) % (unsigned)0x7FFF;
157 /***************************************************************************
158 Either loops back or sends out a completed NetBIOS packet.
159 **************************************************************************/
161 static bool send_netbios_packet(struct packet_struct *p)
163 bool loopback_this_packet = False;
165 /* Check if we are sending to or from ourselves as a WINS server. */
166 if(ismyip_v4(p->ip) && (p->port == global_nmb_port))
167 loopback_this_packet = True;
169 if(loopback_this_packet) {
170 struct packet_struct *lo_packet = NULL;
171 DEBUG(5,("send_netbios_packet: sending packet to ourselves.\n"));
172 if((lo_packet = copy_packet(p)) == NULL)
174 queue_packet(lo_packet);
175 } else if (!send_packet(p)) {
176 DEBUG(0,("send_netbios_packet: send_packet() to IP %s port %d failed\n",
177 inet_ntoa(p->ip),p->port));
184 /***************************************************************************
185 Sets up the common elements of an outgoing NetBIOS packet.
187 Note: do not attempt to rationalise whether rec_des should be set or not
188 in a particular situation. Just follow rfc_1002 or look at examples from WinXX.
189 It does NOT follow the rule that requests to the wins server always have
190 rec_des true. See for example name releases and refreshes
191 **************************************************************************/
193 static struct packet_struct *create_and_init_netbios_packet(struct nmb_name *nmbname,
194 bool bcast, bool rec_des,
195 struct in_addr to_ip)
197 struct packet_struct *packet = NULL;
198 struct nmb_packet *nmb = NULL;
200 /* Allocate the packet_struct we will return. */
201 if((packet = SMB_MALLOC_P(struct packet_struct)) == NULL) {
202 DEBUG(0,("create_and_init_netbios_packet: malloc fail (1) for packet struct.\n"));
206 memset((char *)packet,'\0',sizeof(*packet));
208 nmb = &packet->packet.nmb;
210 nmb->header.name_trn_id = generate_name_trn_id();
211 nmb->header.response = False;
212 nmb->header.nm_flags.recursion_desired = rec_des;
213 nmb->header.nm_flags.recursion_available = False;
214 nmb->header.nm_flags.trunc = False;
215 nmb->header.nm_flags.authoritative = False;
216 nmb->header.nm_flags.bcast = bcast;
218 nmb->header.rcode = 0;
219 nmb->header.qdcount = 1;
220 nmb->header.ancount = 0;
221 nmb->header.nscount = 0;
223 nmb->question.question_name = *nmbname;
224 nmb->question.question_type = QUESTION_TYPE_NB_QUERY;
225 nmb->question.question_class = QUESTION_CLASS_IN;
228 packet->port = NMB_PORT;
229 packet->recv_fd = -1;
230 packet->send_fd = ClientNMB;
231 packet->timestamp = time(NULL);
232 packet->packet_type = NMB_PACKET;
233 packet->locked = False;
235 return packet; /* Caller must free. */
238 /***************************************************************************
239 Sets up the common elements of register, refresh or release packet.
240 **************************************************************************/
242 static bool create_and_init_additional_record(struct packet_struct *packet,
244 const struct in_addr *register_ip)
246 struct nmb_packet *nmb = &packet->packet.nmb;
248 if((nmb->additional = SMB_MALLOC_P(struct res_rec)) == NULL) {
249 DEBUG(0,("create_and_init_additional_record: malloc fail for additional record.\n"));
253 memset((char *)nmb->additional,'\0',sizeof(struct res_rec));
255 nmb->additional->rr_name = nmb->question.question_name;
256 nmb->additional->rr_type = RR_TYPE_NB;
257 nmb->additional->rr_class = RR_CLASS_IN;
259 /* See RFC 1002, sections 5.1.1.1, 5.1.1.2 and 5.1.1.3 */
260 if (nmb->header.nm_flags.bcast)
261 nmb->additional->ttl = PERMANENT_TTL;
263 nmb->additional->ttl = lp_max_ttl();
265 nmb->additional->rdlength = 6;
267 set_nb_flags(nmb->additional->rdata,nb_flags);
269 /* Set the address for the name we are registering. */
270 putip(&nmb->additional->rdata[2], register_ip);
273 it turns out that Jeremys code was correct, we are supposed
274 to send registrations from the IP we are registering. The
275 trick is what to do on timeouts! When we send on a
276 non-routable IP then the reply will timeout, and we should
277 treat this as success, not failure. That means we go into
278 our standard refresh cycle for that name which copes nicely
279 with disconnected networks.
281 packet->recv_fd = -1;
282 packet->send_fd = find_subnet_fd_for_address(*register_ip);
287 /***************************************************************************
288 Sends out a name query.
289 **************************************************************************/
291 static bool initiate_name_query_packet( struct packet_struct *packet)
293 struct nmb_packet *nmb = NULL;
295 nmb = &packet->packet.nmb;
297 nmb->header.opcode = NMB_NAME_QUERY_OPCODE;
298 nmb->header.arcount = 0;
300 nmb->header.nm_flags.recursion_desired = True;
302 DEBUG(4,("initiate_name_query_packet: sending query for name %s (bcast=%s) to IP %s\n",
303 nmb_namestr(&nmb->question.question_name),
304 BOOLSTR(nmb->header.nm_flags.bcast), inet_ntoa(packet->ip)));
306 return send_netbios_packet( packet );
309 /***************************************************************************
310 Sends out a name query - from a WINS server.
311 **************************************************************************/
313 static bool initiate_name_query_packet_from_wins_server( struct packet_struct *packet)
315 struct nmb_packet *nmb = NULL;
317 nmb = &packet->packet.nmb;
319 nmb->header.opcode = NMB_NAME_QUERY_OPCODE;
320 nmb->header.arcount = 0;
322 nmb->header.nm_flags.recursion_desired = False;
324 DEBUG(4,("initiate_name_query_packet_from_wins_server: sending query for name %s (bcast=%s) to IP %s\n",
325 nmb_namestr(&nmb->question.question_name),
326 BOOLSTR(nmb->header.nm_flags.bcast), inet_ntoa(packet->ip)));
328 return send_netbios_packet( packet );
331 /***************************************************************************
332 Sends out a name register.
333 **************************************************************************/
335 static bool initiate_name_register_packet( struct packet_struct *packet,
336 uint16_t nb_flags, const struct in_addr *register_ip)
338 struct nmb_packet *nmb = &packet->packet.nmb;
340 nmb->header.opcode = NMB_NAME_REG_OPCODE;
341 nmb->header.arcount = 1;
343 nmb->header.nm_flags.recursion_desired = True;
345 if(create_and_init_additional_record(packet, nb_flags, register_ip) == False)
348 DEBUG(4,("initiate_name_register_packet: sending registration for name %s (bcast=%s) to IP %s\n",
349 nmb_namestr(&nmb->additional->rr_name),
350 BOOLSTR(nmb->header.nm_flags.bcast), inet_ntoa(packet->ip)));
352 return send_netbios_packet( packet );
355 /***************************************************************************
356 Sends out a multihomed name register.
357 **************************************************************************/
359 static bool initiate_multihomed_name_register_packet(struct packet_struct *packet,
360 uint16_t nb_flags, struct in_addr *register_ip)
362 struct nmb_packet *nmb = &packet->packet.nmb;
363 fstring second_ip_buf;
365 fstrcpy(second_ip_buf, inet_ntoa(packet->ip));
367 nmb->header.opcode = NMB_NAME_MULTIHOMED_REG_OPCODE;
368 nmb->header.arcount = 1;
370 nmb->header.nm_flags.recursion_desired = True;
372 if(create_and_init_additional_record(packet, nb_flags, register_ip) == False)
375 DEBUG(4,("initiate_multihomed_name_register_packet: sending registration \
376 for name %s IP %s (bcast=%s) to IP %s\n",
377 nmb_namestr(&nmb->additional->rr_name), inet_ntoa(*register_ip),
378 BOOLSTR(nmb->header.nm_flags.bcast), second_ip_buf ));
380 return send_netbios_packet( packet );
383 /***************************************************************************
384 Sends out a name refresh.
385 **************************************************************************/
387 static bool initiate_name_refresh_packet( struct packet_struct *packet,
388 uint16_t nb_flags, struct in_addr *refresh_ip)
390 struct nmb_packet *nmb = &packet->packet.nmb;
392 nmb->header.opcode = NMB_NAME_REFRESH_OPCODE_8;
393 nmb->header.arcount = 1;
395 nmb->header.nm_flags.recursion_desired = False;
397 if(create_and_init_additional_record(packet, nb_flags, refresh_ip) == False)
400 DEBUG(4,("initiate_name_refresh_packet: sending refresh for name %s (bcast=%s) to IP %s\n",
401 nmb_namestr(&nmb->additional->rr_name),
402 BOOLSTR(nmb->header.nm_flags.bcast), inet_ntoa(packet->ip)));
404 return send_netbios_packet( packet );
407 /***************************************************************************
408 Sends out a name release.
409 **************************************************************************/
411 static bool initiate_name_release_packet( struct packet_struct *packet,
412 uint16_t nb_flags, struct in_addr *release_ip)
414 struct nmb_packet *nmb = &packet->packet.nmb;
416 nmb->header.opcode = NMB_NAME_RELEASE_OPCODE;
417 nmb->header.arcount = 1;
419 nmb->header.nm_flags.recursion_desired = False;
421 if(create_and_init_additional_record(packet, nb_flags, release_ip) == False)
424 DEBUG(4,("initiate_name_release_packet: sending release for name %s (bcast=%s) to IP %s\n",
425 nmb_namestr(&nmb->additional->rr_name),
426 BOOLSTR(nmb->header.nm_flags.bcast), inet_ntoa(packet->ip)));
428 return send_netbios_packet( packet );
431 /***************************************************************************
432 Sends out a node status.
433 **************************************************************************/
435 static bool initiate_node_status_packet( struct packet_struct *packet )
437 struct nmb_packet *nmb = &packet->packet.nmb;
439 nmb->header.opcode = NMB_NAME_QUERY_OPCODE;
440 nmb->header.arcount = 0;
442 nmb->header.nm_flags.recursion_desired = False;
444 nmb->question.question_type = QUESTION_TYPE_NB_STATUS;
446 DEBUG(4,("initiate_node_status_packet: sending node status request for name %s to IP %s\n",
447 nmb_namestr(&nmb->question.question_name),
448 inet_ntoa(packet->ip)));
450 return send_netbios_packet( packet );
453 /****************************************************************************
454 Simplification functions for queuing standard packets.
455 These should be the only publicly callable functions for sending
457 ****************************************************************************/
459 /****************************************************************************
460 Assertion - we should never be sending nmbd packets on the remote
462 ****************************************************************************/
464 static bool assert_check_subnet(struct subnet_record *subrec)
466 if( subrec == remote_broadcast_subnet) {
467 DEBUG(0,("assert_check_subnet: Attempt to send packet on remote broadcast subnet. \
474 /****************************************************************************
475 Queue a register name packet to the broadcast address of a subnet.
476 ****************************************************************************/
478 struct response_record *queue_register_name( struct subnet_record *subrec,
479 response_function resp_fn,
480 timeout_response_function timeout_fn,
481 register_name_success_function success_fn,
482 register_name_fail_function fail_fn,
483 struct userdata_struct *userdata,
484 struct nmb_name *nmbname,
487 struct packet_struct *p;
488 struct response_record *rrec;
489 struct sockaddr_storage ss;
490 const struct sockaddr_storage *pss = NULL;
491 if(assert_check_subnet(subrec))
494 /* note that all name registration requests have RD set (rfc1002 - section 4.2.2 */
495 if ((p = create_and_init_netbios_packet(nmbname, (subrec != unicast_subnet), True,
496 subrec->bcast_ip)) == NULL)
499 in_addr_to_sockaddr_storage(&ss, subrec->bcast_ip);
500 pss = iface_ip((struct sockaddr *)(void *)&ss);
501 if (!pss || pss->ss_family != AF_INET) {
507 if(initiate_name_register_packet(p, nb_flags,
508 &((const struct sockaddr_in *)pss)->sin_addr) == False) {
514 if((rrec = make_response_record(subrec, /* subnet record. */
515 p, /* packet we sent. */
516 resp_fn, /* function to call on response. */
517 timeout_fn, /* function to call on timeout. */
518 (success_function)success_fn, /* function to call on operation success. */
519 (fail_function)fail_fn, /* function to call on operation fail. */
520 userdata)) == NULL) {
529 /****************************************************************************
530 Queue a refresh name packet to the broadcast address of a subnet.
531 ****************************************************************************/
533 void queue_wins_refresh(struct nmb_name *nmbname,
534 response_function resp_fn,
535 timeout_response_function timeout_fn,
537 struct in_addr refresh_ip,
540 struct packet_struct *p;
541 struct response_record *rrec;
542 struct in_addr wins_ip;
543 struct userdata_struct *userdata;
546 wins_ip = wins_srv_ip_tag(tag, refresh_ip);
548 if ((p = create_and_init_netbios_packet(nmbname, False, False, wins_ip)) == NULL) {
552 if (!initiate_name_refresh_packet(p, nb_flags, &refresh_ip)) {
558 fstrcpy(ip_str, inet_ntoa(refresh_ip));
560 DEBUG(6,("Refreshing name %s IP %s with WINS server %s using tag '%s'\n",
561 nmb_namestr(nmbname), ip_str, inet_ntoa(wins_ip), tag));
563 userdata = (struct userdata_struct *)SMB_MALLOC(sizeof(*userdata) + strlen(tag) + 1);
567 DEBUG(0,("Failed to allocate userdata structure!\n"));
570 ZERO_STRUCTP(userdata);
571 userdata->userdata_len = strlen(tag) + 1;
572 strlcpy(userdata->data, tag, userdata->userdata_len);
574 if ((rrec = make_response_record(unicast_subnet,
579 userdata)) == NULL) {
587 /* we don't want to repeat refresh packets */
588 rrec->repeat_count = 0;
592 /****************************************************************************
593 Queue a multihomed register name packet to a given WINS server IP
594 ****************************************************************************/
596 struct response_record *queue_register_multihomed_name( struct subnet_record *subrec,
597 response_function resp_fn,
598 timeout_response_function timeout_fn,
599 register_name_success_function success_fn,
600 register_name_fail_function fail_fn,
601 struct userdata_struct *userdata,
602 struct nmb_name *nmbname,
604 struct in_addr register_ip,
605 struct in_addr wins_ip)
607 struct packet_struct *p;
608 struct response_record *rrec;
612 if(subrec != unicast_subnet) {
613 DEBUG(0,("queue_register_multihomed_name: should only be done on \
614 unicast subnet. subnet is %s\n.", subrec->subnet_name ));
618 if(assert_check_subnet(subrec))
621 if ((p = create_and_init_netbios_packet(nmbname, False, True, wins_ip)) == NULL)
624 if (nb_flags & NB_GROUP)
625 ret = initiate_name_register_packet( p, nb_flags, ®ister_ip);
627 ret = initiate_multihomed_name_register_packet(p, nb_flags, ®ister_ip);
635 if ((rrec = make_response_record(subrec, /* subnet record. */
636 p, /* packet we sent. */
637 resp_fn, /* function to call on response. */
638 timeout_fn, /* function to call on timeout. */
639 (success_function)success_fn, /* function to call on operation success. */
640 (fail_function)fail_fn, /* function to call on operation fail. */
641 userdata)) == NULL) {
650 /****************************************************************************
651 Queue a release name packet to the broadcast address of a subnet.
652 ****************************************************************************/
654 struct response_record *queue_release_name( struct subnet_record *subrec,
655 response_function resp_fn,
656 timeout_response_function timeout_fn,
657 release_name_success_function success_fn,
658 release_name_fail_function fail_fn,
659 struct userdata_struct *userdata,
660 struct nmb_name *nmbname,
662 struct in_addr release_ip,
663 struct in_addr dest_ip)
665 struct packet_struct *p;
666 struct response_record *rrec;
668 if(assert_check_subnet(subrec))
671 if ((p = create_and_init_netbios_packet(nmbname, (subrec != unicast_subnet), False, dest_ip)) == NULL)
674 if(initiate_name_release_packet( p, nb_flags, &release_ip) == False) {
680 if((rrec = make_response_record(subrec, /* subnet record. */
681 p, /* packet we sent. */
682 resp_fn, /* function to call on response. */
683 timeout_fn, /* function to call on timeout. */
684 (success_function)success_fn, /* function to call on operation success. */
685 (fail_function)fail_fn, /* function to call on operation fail. */
686 userdata)) == NULL) {
693 * For a broadcast release packet, only send once.
694 * This will cause us to remove the name asap. JRA.
697 if (subrec != unicast_subnet) {
698 rrec->repeat_count = 0;
699 rrec->repeat_time = 0;
705 /****************************************************************************
706 Queue a query name packet to the broadcast address of a subnet.
707 ****************************************************************************/
709 struct response_record *queue_query_name( struct subnet_record *subrec,
710 response_function resp_fn,
711 timeout_response_function timeout_fn,
712 query_name_success_function success_fn,
713 query_name_fail_function fail_fn,
714 struct userdata_struct *userdata,
715 struct nmb_name *nmbname)
717 struct packet_struct *p;
718 struct response_record *rrec;
719 struct in_addr to_ip;
721 if(assert_check_subnet(subrec))
724 to_ip = subrec->bcast_ip;
726 /* queries to the WINS server turn up here as queries to IP 0.0.0.0
727 These need to be handled a bit differently */
728 if (subrec->type == UNICAST_SUBNET && is_zero_ip_v4(to_ip)) {
729 /* What we really need to do is loop over each of our wins
730 * servers and wins server tags here, but that just doesn't
731 * fit our architecture at the moment (userdata may already
732 * be used when we get here). For now we just query the first
733 * active wins server on the first tag.
735 char **tags = wins_srv_tags();
739 to_ip = wins_srv_ip_tag(tags[0], to_ip);
740 wins_srv_tags_free(tags);
743 if(( p = create_and_init_netbios_packet(nmbname,
744 (subrec != unicast_subnet),
745 (subrec == unicast_subnet),
749 if(lp_bind_interfaces_only()) {
752 DEBUG(10,("queue_query_name: bind_interfaces_only is set, looking for suitable source IP\n"));
753 for(i = 0; i < iface_count(); i++) {
754 const struct in_addr *ifip = iface_n_ip_v4(i);
757 DEBUG(0,("queue_query_name: interface %d has NULL IP address !\n", i));
761 if (is_loopback_ip_v4(*ifip)) {
762 DEBUG(5,("queue_query_name: ignoring loopback interface (%d)\n", i));
766 DEBUG(10,("queue_query_name: using source IP %s\n",inet_ntoa(*ifip)));
767 p->send_fd = find_subnet_fd_for_address( *ifip );
772 if(initiate_name_query_packet( p ) == False) {
778 if((rrec = make_response_record(subrec, /* subnet record. */
779 p, /* packet we sent. */
780 resp_fn, /* function to call on response. */
781 timeout_fn, /* function to call on timeout. */
782 (success_function)success_fn, /* function to call on operation success. */
783 (fail_function)fail_fn, /* function to call on operation fail. */
784 userdata)) == NULL) {
793 /****************************************************************************
794 Queue a query name packet to a given address from the WINS subnet.
795 ****************************************************************************/
797 struct response_record *queue_query_name_from_wins_server( struct in_addr to_ip,
798 response_function resp_fn,
799 timeout_response_function timeout_fn,
800 query_name_success_function success_fn,
801 query_name_fail_function fail_fn,
802 struct userdata_struct *userdata,
803 struct nmb_name *nmbname)
805 struct packet_struct *p;
806 struct response_record *rrec;
808 if ((p = create_and_init_netbios_packet(nmbname, False, False, to_ip)) == NULL)
811 if(initiate_name_query_packet_from_wins_server( p ) == False) {
817 if((rrec = make_response_record(wins_server_subnet, /* subnet record. */
818 p, /* packet we sent. */
819 resp_fn, /* function to call on response. */
820 timeout_fn, /* function to call on timeout. */
821 (success_function)success_fn, /* function to call on operation success. */
822 (fail_function)fail_fn, /* function to call on operation fail. */
823 userdata)) == NULL) {
832 /****************************************************************************
833 Queue a node status packet to a given name and address.
834 ****************************************************************************/
836 struct response_record *queue_node_status( struct subnet_record *subrec,
837 response_function resp_fn,
838 timeout_response_function timeout_fn,
839 node_status_success_function success_fn,
840 node_status_fail_function fail_fn,
841 struct userdata_struct *userdata,
842 struct nmb_name *nmbname,
843 struct in_addr send_ip)
845 struct packet_struct *p;
846 struct response_record *rrec;
849 if(subrec != unicast_subnet) {
850 DEBUG(0,("queue_register_multihomed_name: should only be done on \
851 unicast subnet. subnet is %s\n.", subrec->subnet_name ));
855 if(assert_check_subnet(subrec))
858 if(( p = create_and_init_netbios_packet(nmbname, False, False, send_ip)) == NULL)
861 if(initiate_node_status_packet(p) == False) {
867 if((rrec = make_response_record(subrec, /* subnet record. */
868 p, /* packet we sent. */
869 resp_fn, /* function to call on response. */
870 timeout_fn, /* function to call on timeout. */
871 (success_function)success_fn, /* function to call on operation success. */
872 (fail_function)fail_fn, /* function to call on operation fail. */
873 userdata)) == NULL) {
882 /****************************************************************************
883 Reply to a netbios name packet. see rfc1002.txt
884 ****************************************************************************/
886 void reply_netbios_packet(struct packet_struct *orig_packet,
887 int rcode, enum netbios_reply_type_code rcv_code, int opcode,
888 int ttl, char *data,int len)
890 struct packet_struct packet;
891 struct nmb_packet *nmb = NULL;
892 struct res_rec answers;
893 struct nmb_packet *orig_nmb = &orig_packet->packet.nmb;
894 bool loopback_this_packet = False;
895 int rr_type = RR_TYPE_NB;
896 const char *packet_type = "unknown";
898 /* Check if we are sending to or from ourselves. */
899 if(ismyip_v4(orig_packet->ip) && (orig_packet->port == global_nmb_port))
900 loopback_this_packet = True;
902 nmb = &packet.packet.nmb;
904 /* Do a partial copy of the packet. We clear the locked flag and
905 the resource record pointers. */
906 packet = *orig_packet; /* Full structure copy. */
907 packet.locked = False;
910 nmb->additional = NULL;
914 packet_type = "nmb_status";
915 nmb->header.nm_flags.recursion_desired = False;
916 nmb->header.nm_flags.recursion_available = False;
917 rr_type = RR_TYPE_NBSTAT;
920 packet_type = "nmb_query";
921 nmb->header.nm_flags.recursion_desired = True;
922 nmb->header.nm_flags.recursion_available = True;
924 rr_type = RR_TYPE_NULL;
928 case NMB_REG_REFRESH:
929 packet_type = "nmb_reg";
930 nmb->header.nm_flags.recursion_desired = True;
931 nmb->header.nm_flags.recursion_available = True;
934 packet_type = "nmb_rel";
935 nmb->header.nm_flags.recursion_desired = False;
936 nmb->header.nm_flags.recursion_available = False;
939 packet_type = "nmb_wack";
940 nmb->header.nm_flags.recursion_desired = False;
941 nmb->header.nm_flags.recursion_available = False;
942 rr_type = RR_TYPE_NULL;
945 packet_type = "wins_reg";
946 nmb->header.nm_flags.recursion_desired = True;
947 nmb->header.nm_flags.recursion_available = True;
950 packet_type = "wins_query";
951 nmb->header.nm_flags.recursion_desired = True;
952 nmb->header.nm_flags.recursion_available = True;
954 rr_type = RR_TYPE_NULL;
958 DEBUG(0,("reply_netbios_packet: Unknown packet type: %s %s to ip %s\n",
959 packet_type, nmb_namestr(&orig_nmb->question.question_name),
960 inet_ntoa(packet.ip)));
964 DEBUG(4, ("reply_netbios_packet: sending a reply of packet type: %s "
965 "%s to ip %s for id %d\n", packet_type,
966 nmb_namestr(&orig_nmb->question.question_name),
967 inet_ntoa(packet.ip), orig_nmb->header.name_trn_id));
969 nmb->header.name_trn_id = orig_nmb->header.name_trn_id;
970 nmb->header.opcode = opcode;
971 nmb->header.response = True;
972 nmb->header.nm_flags.bcast = False;
973 nmb->header.nm_flags.trunc = False;
974 nmb->header.nm_flags.authoritative = True;
976 nmb->header.rcode = rcode;
977 nmb->header.qdcount = 0;
978 nmb->header.ancount = 1;
979 nmb->header.nscount = 0;
980 nmb->header.arcount = 0;
982 memset((char*)&nmb->question,'\0',sizeof(nmb->question));
984 nmb->answers = &answers;
985 memset((char*)nmb->answers,'\0',sizeof(*nmb->answers));
987 nmb->answers->rr_name = orig_nmb->question.question_name;
988 nmb->answers->rr_type = rr_type;
989 nmb->answers->rr_class = RR_CLASS_IN;
990 nmb->answers->ttl = ttl;
993 if (len < 0 || len > sizeof(nmb->answers->rdata)) {
994 DEBUG(5,("reply_netbios_packet: "
995 "invalid packet len (%d)\n",
999 nmb->answers->rdlength = len;
1000 memcpy(nmb->answers->rdata, data, len);
1003 packet.packet_type = NMB_PACKET;
1004 packet.recv_fd = -1;
1005 /* Ensure we send out on the same fd that the original
1006 packet came in on to give the correct source IP address. */
1007 if (orig_packet->send_fd != -1) {
1008 packet.send_fd = orig_packet->send_fd;
1010 packet.send_fd = orig_packet->recv_fd;
1012 packet.timestamp = time(NULL);
1014 debug_nmb_packet(&packet);
1016 if(loopback_this_packet) {
1017 struct packet_struct *lo_packet;
1018 DEBUG(5,("reply_netbios_packet: sending packet to ourselves.\n"));
1019 if((lo_packet = copy_packet(&packet)) == NULL)
1021 queue_packet(lo_packet);
1022 } else if (!send_packet(&packet)) {
1023 DEBUG(0,("reply_netbios_packet: send_packet to IP %s port %d failed\n",
1024 inet_ntoa(packet.ip),packet.port));
1028 /*******************************************************************
1029 Queue a packet into a packet queue
1030 ******************************************************************/
1032 void queue_packet(struct packet_struct *packet)
1034 DLIST_ADD_END(packet_queue, packet);
1037 /****************************************************************************
1038 Try and find a matching subnet record for a datagram port 138 packet.
1039 ****************************************************************************/
1041 static struct subnet_record *find_subnet_for_dgram_browse_packet(struct packet_struct *p)
1043 struct subnet_record *subrec;
1045 /* Go through all the broadcast subnets and see if the mask matches. */
1046 for (subrec = FIRST_SUBNET; subrec ; subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec)) {
1047 if(same_net_v4(p->ip, subrec->bcast_ip, subrec->mask_ip))
1051 /* If the subnet record is the remote announce broadcast subnet,
1052 hack it here to be the first subnet. This is really gross and
1053 is needed due to people turning on port 137/138 broadcast
1054 forwarding on their routers. May fire and brimstone rain
1058 return FIRST_SUBNET;
1061 /****************************************************************************
1062 Dispatch a browse frame from port 138 to the correct processing function.
1063 ****************************************************************************/
1065 static void process_browse_packet(struct packet_struct *p, const char *buf,int len)
1067 struct dgram_packet *dgram = &p->packet.dgram;
1068 int command = CVAL(buf,0);
1069 struct subnet_record *subrec = find_subnet_for_dgram_browse_packet(p);
1073 /* Drop the packet if it's a different NetBIOS scope, or the source is from one of our names. */
1074 pull_ascii(scope, dgram->dest_name.scope, 64, 64, STR_TERMINATE);
1075 if (!strequal(scope, lp_netbios_scope())) {
1076 DEBUG(7,("process_browse_packet: Discarding datagram from IP %s. Scope (%s) \
1077 mismatch with our scope (%s).\n", inet_ntoa(p->ip), scope, lp_netbios_scope()));
1081 pull_ascii_nstring(src_name, sizeof(src_name), dgram->source_name.name);
1082 if (is_myname(src_name)) {
1083 DEBUG(7,("process_browse_packet: Discarding datagram from IP %s. Source name \
1084 %s is one of our names !\n", inet_ntoa(p->ip), nmb_namestr(&dgram->source_name)));
1089 case ANN_HostAnnouncement:
1090 debug_browse_data(buf, len);
1091 process_host_announce(subrec, p, buf+1);
1093 case ANN_DomainAnnouncement:
1094 debug_browse_data(buf, len);
1095 process_workgroup_announce(subrec, p, buf+1);
1097 case ANN_LocalMasterAnnouncement:
1098 debug_browse_data(buf, len);
1099 process_local_master_announce(subrec, p, buf+1);
1101 case ANN_AnnouncementRequest:
1102 debug_browse_data(buf, len);
1103 process_announce_request(subrec, p, buf+1);
1106 debug_browse_data(buf, len);
1107 process_election(subrec, p, buf+1);
1109 case ANN_GetBackupListReq:
1110 debug_browse_data(buf, len);
1111 process_get_backup_list_request(subrec, p, buf+1);
1113 case ANN_GetBackupListResp:
1114 debug_browse_data(buf, len);
1115 /* We never send ANN_GetBackupListReq so we should never get these. */
1116 DEBUG(0,("process_browse_packet: Discarding GetBackupListResponse \
1117 packet from %s IP %s\n", nmb_namestr(&dgram->source_name), inet_ntoa(p->ip)));
1119 case ANN_ResetBrowserState:
1120 debug_browse_data(buf, len);
1121 process_reset_browser(subrec, p, buf+1);
1123 case ANN_MasterAnnouncement:
1124 /* Master browser datagrams must be processed on the unicast subnet. */
1125 subrec = unicast_subnet;
1127 debug_browse_data(buf, len);
1128 process_master_browser_announce(subrec, p, buf+1);
1130 case ANN_BecomeBackup:
1132 * We don't currently implement this. Log it just in case.
1134 debug_browse_data(buf, len);
1135 DEBUG(10,("process_browse_packet: On subnet %s ignoring browse packet \
1136 command ANN_BecomeBackup from %s IP %s to %s\n", subrec->subnet_name, nmb_namestr(&dgram->source_name),
1137 inet_ntoa(p->ip), nmb_namestr(&dgram->dest_name)));
1140 debug_browse_data(buf, len);
1141 DEBUG(0,("process_browse_packet: On subnet %s ignoring browse packet \
1142 command code %d from %s IP %s to %s\n", subrec->subnet_name, command, nmb_namestr(&dgram->source_name),
1143 inet_ntoa(p->ip), nmb_namestr(&dgram->dest_name)));
1148 /****************************************************************************
1149 Dispatch a LanMan browse frame from port 138 to the correct processing function.
1150 ****************************************************************************/
1152 static void process_lanman_packet(struct packet_struct *p, const char *buf,int len)
1154 struct dgram_packet *dgram = &p->packet.dgram;
1155 int command = SVAL(buf,0);
1156 struct subnet_record *subrec = find_subnet_for_dgram_browse_packet(p);
1160 /* Drop the packet if it's a different NetBIOS scope, or the source is from one of our names. */
1162 pull_ascii(scope, dgram->dest_name.scope, 64, 64, STR_TERMINATE);
1163 if (!strequal(scope, lp_netbios_scope())) {
1164 DEBUG(7,("process_lanman_packet: Discarding datagram from IP %s. Scope (%s) \
1165 mismatch with our scope (%s).\n", inet_ntoa(p->ip), scope, lp_netbios_scope()));
1169 pull_ascii_nstring(src_name, sizeof(src_name), dgram->source_name.name);
1170 if (is_myname(src_name)) {
1171 DEBUG(0,("process_lanman_packet: Discarding datagram from IP %s. Source name \
1172 %s is one of our names !\n", inet_ntoa(p->ip), nmb_namestr(&dgram->source_name)));
1177 case ANN_HostAnnouncement:
1178 debug_browse_data(buf, len);
1179 process_lm_host_announce(subrec, p, buf+1, len > 1 ? len-1 : 0);
1181 case ANN_AnnouncementRequest:
1182 process_lm_announce_request(subrec, p, buf+1, len > 1 ? len-1 : 0);
1185 DEBUG(0,("process_lanman_packet: On subnet %s ignoring browse packet \
1186 command code %d from %s IP %s to %s\n", subrec->subnet_name, command, nmb_namestr(&dgram->source_name),
1187 inet_ntoa(p->ip), nmb_namestr(&dgram->dest_name)));
1192 /****************************************************************************
1193 Determine if a packet is for us on port 138. Note that to have any chance of
1194 being efficient we need to drop as many packets as possible at this
1195 stage as subsequent processing is expensive.
1196 ****************************************************************************/
1198 static bool listening(struct packet_struct *p,struct nmb_name *nbname)
1200 struct subnet_record *subrec = NULL;
1202 for (subrec = FIRST_SUBNET; subrec ; subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec)) {
1203 if(same_net_v4(p->ip, subrec->bcast_ip, subrec->mask_ip))
1208 subrec = unicast_subnet;
1210 return (find_name_on_subnet(subrec, nbname, FIND_SELF_NAME) != NULL);
1213 /****************************************************************************
1214 Process udp 138 datagrams
1215 ****************************************************************************/
1217 static void process_dgram(struct packet_struct *p)
1222 struct dgram_packet *dgram = &p->packet.dgram;
1224 /* If we aren't listening to the destination name then ignore the packet */
1225 if (!listening(p,&dgram->dest_name)) {
1226 nb_packet_dispatch(packet_server, p);
1227 DEBUG(5,("process_dgram: ignoring dgram packet sent to name %s from %s\n",
1228 nmb_namestr(&dgram->dest_name), inet_ntoa(p->ip)));
1232 if (dgram->header.msg_type != 0x10 && dgram->header.msg_type != 0x11 && dgram->header.msg_type != 0x12) {
1233 nb_packet_dispatch(packet_server, p);
1234 /* Don't process error packets etc yet */
1235 DEBUG(5,("process_dgram: ignoring dgram packet sent to name %s from IP %s as it is \
1236 an error packet of type %x\n", nmb_namestr(&dgram->dest_name), inet_ntoa(p->ip), dgram->header.msg_type));
1240 /* Ensure we have a large enough packet before looking inside. */
1241 if (dgram->datasize < (smb_vwv12 - 2)) {
1242 /* That's the offset minus the 4 byte length + 2 bytes of offset. */
1243 DEBUG(0,("process_dgram: ignoring too short dgram packet (%u) sent to name %s from IP %s\n",
1244 (unsigned int)dgram->datasize,
1245 nmb_namestr(&dgram->dest_name),
1246 inet_ntoa(p->ip) ));
1250 buf = &dgram->data[0];
1251 buf -= 4; /* XXXX for the pseudo tcp length - someday I need to get rid of this */
1253 if (CVAL(buf,smb_com) != SMBtrans)
1256 len = SVAL(buf,smb_vwv11);
1257 buf2 = smb_base(buf) + SVAL(buf,smb_vwv12);
1259 if (len <= 0 || len > dgram->datasize) {
1260 DEBUG(0,("process_dgram: ignoring malformed1 (datasize = %d, len = %d) datagram \
1261 packet sent to name %s from IP %s\n",
1264 nmb_namestr(&dgram->dest_name),
1265 inet_ntoa(p->ip) ));
1269 if (buf2 < dgram->data || (buf2 >= dgram->data + dgram->datasize)) {
1270 DEBUG(0,("process_dgram: ignoring malformed2 (datasize = %d, len=%d, off=%d) datagram \
1271 packet sent to name %s from IP %s\n",
1274 (int)PTR_DIFF(buf2, dgram->data),
1275 nmb_namestr(&dgram->dest_name),
1276 inet_ntoa(p->ip) ));
1280 if ((buf2 + len < dgram->data) || (buf2 + len > dgram->data + dgram->datasize)) {
1281 DEBUG(0,("process_dgram: ignoring malformed3 (datasize = %d, len=%d, off=%d) datagram \
1282 packet sent to name %s from IP %s\n",
1285 (int)PTR_DIFF(buf2, dgram->data),
1286 nmb_namestr(&dgram->dest_name),
1287 inet_ntoa(p->ip) ));
1291 DEBUG(4,("process_dgram: datagram from %s to %s IP %s for %s of type %d len=%d\n",
1292 nmb_namestr(&dgram->source_name),nmb_namestr(&dgram->dest_name),
1293 inet_ntoa(p->ip), smb_buf_const(buf),CVAL(buf2,0),len));
1295 /* Datagram packet received for the browser mailslot */
1296 if (strequal(smb_buf_const(buf),BROWSE_MAILSLOT)) {
1297 process_browse_packet(p,buf2,len);
1301 /* Datagram packet received for the LAN Manager mailslot */
1302 if (strequal(smb_buf_const(buf),LANMAN_MAILSLOT)) {
1303 process_lanman_packet(p,buf2,len);
1307 /* Datagram packet received for the domain logon mailslot */
1308 if (strequal(smb_buf_const(buf),NET_LOGON_MAILSLOT)) {
1309 process_logon_packet(p,buf2,len,NET_LOGON_MAILSLOT);
1313 /* Datagram packet received for the NT domain logon mailslot */
1314 if (strequal(smb_buf_const(buf),NT_LOGON_MAILSLOT)) {
1315 process_logon_packet(p,buf2,len,NT_LOGON_MAILSLOT);
1319 nb_packet_dispatch(packet_server, p);
1322 /****************************************************************************
1323 Validate a response nmb packet.
1324 ****************************************************************************/
1326 static bool validate_nmb_response_packet( struct nmb_packet *nmb )
1328 bool ignore = False;
1330 switch (nmb->header.opcode) {
1331 case NMB_NAME_REG_OPCODE:
1332 case NMB_NAME_REFRESH_OPCODE_8: /* ambiguity in rfc1002 about which is correct. */
1333 case NMB_NAME_REFRESH_OPCODE_9: /* WinNT uses 8 by default. */
1334 if (nmb->header.ancount == 0) {
1335 DEBUG(0,("validate_nmb_response_packet: Bad REG/REFRESH Packet. "));
1340 case NMB_NAME_QUERY_OPCODE:
1341 if ((nmb->header.ancount != 0) && (nmb->header.ancount != 1)) {
1342 DEBUG(0,("validate_nmb_response_packet: Bad QUERY Packet. "));
1347 case NMB_NAME_RELEASE_OPCODE:
1348 if (nmb->header.ancount == 0) {
1349 DEBUG(0,("validate_nmb_response_packet: Bad RELEASE Packet. "));
1354 case NMB_WACK_OPCODE:
1355 /* Check WACK response here. */
1356 if (nmb->header.ancount != 1) {
1357 DEBUG(0,("validate_nmb_response_packet: Bad WACK Packet. "));
1362 DEBUG(0,("validate_nmb_response_packet: Ignoring packet with unknown opcode %d.\n",
1363 nmb->header.opcode));
1368 DEBUG(0,("Ignoring response packet with opcode %d.\n", nmb->header.opcode));
1373 /****************************************************************************
1374 Validate a request nmb packet.
1375 ****************************************************************************/
1377 static bool validate_nmb_packet( struct nmb_packet *nmb )
1379 bool ignore = False;
1381 switch (nmb->header.opcode) {
1382 case NMB_NAME_REG_OPCODE:
1383 case NMB_NAME_REFRESH_OPCODE_8: /* ambiguity in rfc1002 about which is correct. */
1384 case NMB_NAME_REFRESH_OPCODE_9: /* WinNT uses 8 by default. */
1385 case NMB_NAME_MULTIHOMED_REG_OPCODE:
1386 if (nmb->header.qdcount==0 || nmb->header.arcount==0) {
1387 DEBUG(0,("validate_nmb_packet: Bad REG/REFRESH Packet. "));
1392 case NMB_NAME_QUERY_OPCODE:
1393 if ((nmb->header.qdcount == 0) || ((nmb->question.question_type != QUESTION_TYPE_NB_QUERY) &&
1394 (nmb->question.question_type != QUESTION_TYPE_NB_STATUS))) {
1395 DEBUG(0,("validate_nmb_packet: Bad QUERY Packet. "));
1400 case NMB_NAME_RELEASE_OPCODE:
1401 if (nmb->header.qdcount==0 || nmb->header.arcount==0) {
1402 DEBUG(0,("validate_nmb_packet: Bad RELEASE Packet. "));
1407 DEBUG(0,("validate_nmb_packet: Ignoring packet with unknown opcode %d.\n",
1408 nmb->header.opcode));
1413 DEBUG(0,("validate_nmb_packet: Ignoring request packet with opcode %d.\n", nmb->header.opcode));
1418 /****************************************************************************
1419 Find a subnet (and potentially a response record) for a packet.
1420 ****************************************************************************/
1422 static struct subnet_record *find_subnet_for_nmb_packet( struct packet_struct *p,
1423 struct response_record **pprrec)
1425 struct nmb_packet *nmb = &p->packet.nmb;
1426 struct response_record *rrec = NULL;
1427 struct subnet_record *subrec = NULL;
1432 if(nmb->header.response) {
1433 /* It's a response packet. Find a record for it or it's an error. */
1435 rrec = find_response_record( &subrec, nmb->header.name_trn_id);
1437 DEBUG(3, ("find_subnet_for_nmb_packet: response "
1438 "record not found for response id %d\n",
1439 nmb->header.name_trn_id));
1440 nb_packet_dispatch(packet_server, p);
1444 if(subrec == NULL) {
1445 DEBUG(0, ("find_subnet_for_nmb_packet: subnet record "
1446 "not found for response id %d\n",
1447 nmb->header.name_trn_id));
1456 /* Try and see what subnet this packet belongs to. */
1459 if(packet_is_for_wins_server(p))
1460 return wins_server_subnet;
1462 /* If it wasn't a broadcast packet then send to the UNICAST subnet. */
1463 if(nmb->header.nm_flags.bcast == False)
1464 return unicast_subnet;
1466 /* Go through all the broadcast subnets and see if the mask matches. */
1467 for (subrec = FIRST_SUBNET; subrec ; subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec)) {
1468 if(same_net_v4(p->ip, subrec->bcast_ip, subrec->mask_ip))
1472 /* If none match it must have been a directed broadcast - assign the remote_broadcast_subnet. */
1473 return remote_broadcast_subnet;
1476 /****************************************************************************
1477 Process a nmb request packet - validate the packet and route it.
1478 ****************************************************************************/
1480 static void process_nmb_request(struct packet_struct *p)
1482 struct nmb_packet *nmb = &p->packet.nmb;
1483 struct subnet_record *subrec = NULL;
1485 debug_nmb_packet(p);
1487 /* Ensure we have a good packet. */
1488 if(validate_nmb_packet(nmb))
1491 /* Allocate a subnet to this packet - if we cannot - fail. */
1492 if((subrec = find_subnet_for_nmb_packet(p, NULL))==NULL)
1495 switch (nmb->header.opcode) {
1496 case NMB_NAME_REG_OPCODE:
1497 if(subrec == wins_server_subnet)
1498 wins_process_name_registration_request(subrec, p);
1500 process_name_registration_request(subrec, p);
1503 case NMB_NAME_REFRESH_OPCODE_8: /* ambiguity in rfc1002 about which is correct. */
1504 case NMB_NAME_REFRESH_OPCODE_9:
1505 if(subrec == wins_server_subnet)
1506 wins_process_name_refresh_request(subrec, p);
1508 process_name_refresh_request(subrec, p);
1511 case NMB_NAME_MULTIHOMED_REG_OPCODE:
1512 if(subrec == wins_server_subnet) {
1513 wins_process_multihomed_name_registration_request(subrec, p);
1515 DEBUG(0,("process_nmb_request: Multihomed registration request must be \
1516 directed at a WINS server.\n"));
1520 case NMB_NAME_QUERY_OPCODE:
1521 switch (nmb->question.question_type) {
1522 case QUESTION_TYPE_NB_QUERY:
1523 if(subrec == wins_server_subnet)
1524 wins_process_name_query_request(subrec, p);
1526 process_name_query_request(subrec, p);
1528 case QUESTION_TYPE_NB_STATUS:
1529 if(subrec == wins_server_subnet) {
1530 DEBUG(0,("process_nmb_request: NB_STATUS request directed at WINS server is \
1534 process_node_status_request(subrec, p);
1540 case NMB_NAME_RELEASE_OPCODE:
1541 if(subrec == wins_server_subnet)
1542 wins_process_name_release_request(subrec, p);
1544 process_name_release_request(subrec, p);
1549 /****************************************************************************
1550 Process a nmb response packet - validate the packet and route it.
1551 to either the WINS server or a normal response.
1552 ****************************************************************************/
1554 static void process_nmb_response(struct packet_struct *p)
1556 struct nmb_packet *nmb = &p->packet.nmb;
1557 struct subnet_record *subrec = NULL;
1558 struct response_record *rrec = NULL;
1560 debug_nmb_packet(p);
1562 if(validate_nmb_response_packet(nmb))
1565 if((subrec = find_subnet_for_nmb_packet(p, &rrec))==NULL)
1569 DEBUG(0, ("process_nmb_response: response packet received but "
1570 "no response record found for id = %d. Ignoring "
1571 "packet.\n", nmb->header.name_trn_id));
1575 /* Increment the number of responses received for this record. */
1577 /* Ensure we don't re-send the request. */
1578 rrec->repeat_count = 0;
1580 /* Call the response received function for this packet. */
1581 (*rrec->resp_fn)(subrec, rrec, p);
1584 /*******************************************************************
1585 Run elements off the packet queue till its empty
1586 ******************************************************************/
1588 void run_packet_queue(void)
1590 struct packet_struct *p;
1592 while ((p = packet_queue)) {
1593 DLIST_REMOVE(packet_queue, p);
1595 switch (p->packet_type) {
1597 if(p->packet.nmb.header.response)
1598 process_nmb_response(p);
1600 process_nmb_request(p);
1611 /*******************************************************************
1612 Retransmit or timeout elements from all the outgoing subnet response
1613 record queues. NOTE that this code must also check the WINS server
1614 subnet for response records to timeout as the WINS server code
1615 can send requests to check if a client still owns a name.
1616 (Patch from Andrey Alekseyev <fetch@muffin.arcadia.spb.ru>).
1617 ******************************************************************/
1619 void retransmit_or_expire_response_records(time_t t)
1621 struct subnet_record *subrec;
1623 for (subrec = FIRST_SUBNET; subrec; subrec = get_next_subnet_maybe_unicast_or_wins_server(subrec)) {
1624 struct response_record *rrec, *nextrrec;
1628 for (rrec = subrec->responselist; rrec; rrec = nextrrec) {
1629 nextrrec = rrec->next;
1631 if (rrec->repeat_time <= t) {
1632 if (rrec->repeat_count > 0) {
1633 /* Resend while we have a non-zero repeat_count. */
1634 if(!send_packet(rrec->packet)) {
1635 DEBUG(0,("retransmit_or_expire_response_records: Failed to resend packet id %hu \
1636 to IP %s on subnet %s\n", rrec->response_id, inet_ntoa(rrec->packet->ip), subrec->subnet_name));
1638 rrec->repeat_time = t + rrec->repeat_interval;
1639 rrec->repeat_count--;
1641 DEBUG(4,("retransmit_or_expire_response_records: timeout for packet id %hu to IP %s \
1642 on subnet %s\n", rrec->response_id, inet_ntoa(rrec->packet->ip), subrec->subnet_name));
1645 * Check the flag in this record to prevent recursion if we end
1646 * up in this function again via the timeout function call.
1649 if(!rrec->in_expiration_processing) {
1652 * Set the recursion protection flag in this record.
1655 rrec->in_expiration_processing = True;
1657 /* Call the timeout function. This will deal with removing the
1658 timed out packet. */
1659 if(rrec->timeout_fn) {
1660 (*rrec->timeout_fn)(subrec, rrec);
1662 /* We must remove the record ourself if there is
1663 no timeout function. */
1664 remove_response_record(subrec, rrec);
1666 /* We have changed subrec->responselist,
1667 * restart from the beginning of this list. */
1669 } /* !rrec->in_expitation_processing */
1670 } /* rrec->repeat_count > 0 */
1671 } /* rrec->repeat_time <= t */
1672 } /* end for rrec */
1673 } /* end for subnet */
1676 /****************************************************************************
1677 Create an fd_set containing all the sockets in the subnet structures,
1678 plus the broadcast sockets.
1679 ***************************************************************************/
1681 struct socket_attributes {
1682 enum packet_type type;
1688 static bool create_listen_array(struct socket_attributes **pattrs,
1691 struct subnet_record *subrec = NULL;
1694 struct socket_attributes *attrs;
1696 /* The ClientNMB and ClientDGRAM sockets */
1699 /* Check that we can add all the fd's we need. */
1700 for (subrec = FIRST_SUBNET;
1702 subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec)) {
1703 if (subrec->nmb_sock != -1) {
1706 if (subrec->dgram_sock != -1) {
1709 if (subrec->nmb_bcast != -1) {
1712 if (subrec->dgram_bcast != -1) {
1717 attrs = talloc_zero_array(NULL, struct socket_attributes, count);
1718 if (attrs == NULL) {
1719 DEBUG(1, ("talloc fail for attrs. "
1720 "size %d\n", count));
1726 attrs[num].fd = ClientNMB;
1727 attrs[num].type = NMB_PACKET;
1728 attrs[num].broadcast = false;
1731 attrs[num].fd = ClientDGRAM;
1732 attrs[num].type = DGRAM_PACKET;
1733 attrs[num].broadcast = false;
1736 for (subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec)) {
1738 if (subrec->nmb_sock != -1) {
1739 attrs[num].fd = subrec->nmb_sock;
1740 attrs[num].type = NMB_PACKET;
1741 attrs[num].broadcast = false;
1745 if (subrec->nmb_bcast != -1) {
1746 attrs[num].fd = subrec->nmb_bcast;
1747 attrs[num].type = NMB_PACKET;
1748 attrs[num].broadcast = true;
1752 if (subrec->dgram_sock != -1) {
1753 attrs[num].fd = subrec->dgram_sock;
1754 attrs[num].type = DGRAM_PACKET;
1755 attrs[num].broadcast = false;
1759 if (subrec->dgram_bcast != -1) {
1760 attrs[num].fd = subrec->dgram_bcast;
1761 attrs[num].type = DGRAM_PACKET;
1762 attrs[num].broadcast = true;
1767 TALLOC_FREE(*pattrs);
1770 *pnum_sockets = count;
1775 /****************************************************************************
1776 List of packets we're processing this select.
1777 ***************************************************************************/
1779 struct processed_packet {
1780 struct processed_packet *next;
1781 struct processed_packet *prev;
1782 enum packet_type packet_type;
1787 /****************************************************************************
1788 Have we seen this before ?
1789 ***************************************************************************/
1791 static bool is_processed_packet(struct processed_packet *processed_packet_list,
1792 struct packet_struct *packet)
1794 struct processed_packet *p = NULL;
1796 for (p = processed_packet_list; p; p = p->next) {
1797 if (ip_equal_v4(p->ip, packet->ip) && p->packet_type == packet->packet_type) {
1798 if ((p->packet_type == NMB_PACKET) &&
1800 packet->packet.nmb.header.name_trn_id)) {
1802 } else if ((p->packet_type == DGRAM_PACKET) &&
1804 packet->packet.dgram.header.dgm_id)) {
1812 /****************************************************************************
1813 Keep a list of what we've seen before.
1814 ***************************************************************************/
1816 static bool store_processed_packet(struct processed_packet **pp_processed_packet_list,
1817 struct packet_struct *packet)
1819 struct processed_packet *p = SMB_MALLOC_P(struct processed_packet);
1823 p->packet_type = packet->packet_type;
1825 if (packet->packet_type == NMB_PACKET) {
1826 p->packet_id = packet->packet.nmb.header.name_trn_id;
1827 } else if (packet->packet_type == DGRAM_PACKET) {
1828 p->packet_id = packet->packet.dgram.header.dgm_id;
1834 DLIST_ADD(*pp_processed_packet_list, p);
1838 /****************************************************************************
1839 Throw away what we've seen before.
1840 ***************************************************************************/
1842 static void free_processed_packet_list(struct processed_packet **pp_processed_packet_list)
1844 struct processed_packet *p = NULL, *next = NULL;
1846 for (p = *pp_processed_packet_list; p; p = next) {
1848 DLIST_REMOVE(*pp_processed_packet_list, p);
1853 /****************************************************************************
1854 Timeout callback - just notice we timed out.
1855 ***************************************************************************/
1857 static void nmbd_timeout_handler(struct tevent_context *ev,
1858 struct tevent_timer *te,
1859 struct timeval current_time,
1862 bool *got_timeout = private_data;
1863 *got_timeout = true;
1866 /****************************************************************************
1867 fd callback - remember the fd that triggered.
1868 ***************************************************************************/
1870 static void nmbd_fd_handler(struct tevent_context *ev,
1871 struct tevent_fd *fde,
1875 struct socket_attributes *attr = private_data;
1876 attr->triggered = true;
1879 /****************************************************************************
1880 Listens for NMB or DGRAM packets, and queues them.
1881 return True if the socket is dead
1882 ***************************************************************************/
1884 bool listen_for_packets(struct messaging_context *msg, bool run_election)
1886 static struct socket_attributes *attrs = NULL;
1887 static int listen_number = 0;
1895 int dns_pollidx = -1;
1897 struct processed_packet *processed_packet_list = NULL;
1898 struct tevent_timer *te = NULL;
1899 bool got_timeout = false;
1900 TALLOC_CTX *frame = talloc_stackframe();
1902 if ((attrs == NULL) || rescan_listen_set) {
1903 if (create_listen_array(&attrs, &listen_number)) {
1904 DEBUG(0,("listen_for_packets: Fatal error. unable to create listen set. Exiting.\n"));
1908 rescan_listen_set = False;
1911 num_sockets = listen_number;
1914 dns_fd = asyncdns_fd();
1916 attrs = talloc_realloc(NULL,
1918 struct socket_attributes,
1920 if (attrs == NULL) {
1924 dns_pollidx = num_sockets;
1925 attrs[dns_pollidx].fd = dns_fd;
1927 * dummy values, we only need
1930 attrs[dns_pollidx].type = NMB_PACKET;
1931 attrs[dns_pollidx].broadcast = false;
1936 for (i=0; i<num_sockets; i++) {
1937 struct tevent_fd *tfd = tevent_add_fd(nmbd_event_context(),
1947 attrs[i].triggered = false;
1951 * During elections and when expecting a netbios response packet we
1952 * need to send election packets at tighter intervals.
1953 * Ideally it needs to be the interval (in ms) between time now and
1954 * the time we are expecting the next netbios packet.
1957 if (run_election||num_response_packets) {
1960 timeout_secs = NMBD_SELECT_LOOP;
1963 te = tevent_add_timer(nmbd_event_context(),
1965 tevent_timeval_current_ofs(timeout_secs, 0),
1966 nmbd_timeout_handler,
1973 loop_rtn = tevent_loop_once(nmbd_event_context());
1975 if (loop_rtn == -1) {
1986 if ((dns_fd != -1) && (dns_pollidx != -1) &&
1987 attrs[dns_pollidx].triggered){
1994 for(i = 0; i < listen_number; i++) {
1995 enum packet_type packet_type;
1996 struct packet_struct *packet;
1997 const char *packet_name;
2001 if (!attrs[i].triggered) {
2005 if (attrs[i].type == NMB_PACKET) {
2007 packet_type = NMB_PACKET;
2008 packet_name = "nmb";
2009 client_fd = ClientNMB;
2010 client_port = global_nmb_port;
2013 packet_type = DGRAM_PACKET;
2014 packet_name = "dgram";
2015 client_fd = ClientDGRAM;
2016 client_port = DGRAM_PORT;
2019 packet = read_packet(attrs[i].fd, packet_type);
2025 * If we got a packet on the broadcast socket and interfaces
2026 * only is set then check it came from one of our local nets.
2028 if (lp_bind_interfaces_only() &&
2029 (attrs[i].fd == client_fd) &&
2030 (!is_local_net_v4(packet->ip))) {
2031 DEBUG(7,("discarding %s packet sent to broadcast socket from %s:%d\n",
2032 packet_name, inet_ntoa(packet->ip), packet->port));
2033 free_packet(packet);
2038 if ((is_loopback_ip_v4(packet->ip) || ismyip_v4(packet->ip)) &&
2039 packet->port == client_port)
2041 if (client_port == DGRAM_PORT) {
2042 DEBUG(7,("discarding own dgram packet from %s:%d\n",
2043 inet_ntoa(packet->ip),packet->port));
2044 free_packet(packet);
2048 if (packet->packet.nmb.header.nm_flags.bcast) {
2049 DEBUG(7,("discarding own nmb bcast packet from %s:%d\n",
2050 inet_ntoa(packet->ip),packet->port));
2051 free_packet(packet);
2057 if (is_processed_packet(processed_packet_list, packet)) {
2058 DEBUG(7,("discarding duplicate packet from %s:%d\n",
2059 inet_ntoa(packet->ip),packet->port));
2060 free_packet(packet);
2064 store_processed_packet(&processed_packet_list, packet);
2066 if (attrs[i].broadcast) {
2067 /* this is a broadcast socket */
2068 packet->send_fd = attrs[i-1].fd;
2070 /* this is already a unicast socket */
2071 packet->send_fd = attrs[i].fd;
2074 queue_packet(packet);
2077 free_processed_packet_list(&processed_packet_list);
2082 /****************************************************************************
2083 Construct and send a netbios DGRAM.
2084 **************************************************************************/
2086 bool send_mailslot(bool unique, const char *mailslot,char *buf, size_t len,
2087 const char *srcname, int src_type,
2088 const char *dstname, int dest_type,
2089 struct in_addr dest_ip,struct in_addr src_ip,
2092 bool loopback_this_packet = False;
2093 struct packet_struct p;
2094 struct dgram_packet *dgram = &p.packet.dgram;
2098 memset((char *)&p,'\0',sizeof(p));
2100 if(ismyip_v4(dest_ip) && (dest_port == DGRAM_PORT)) /* Only if to DGRAM_PORT */
2101 loopback_this_packet = True;
2103 /* generate_name_trn_id(); */ /* Not used, so gone, RJS */
2105 /* DIRECT GROUP or UNIQUE datagram. */
2106 dgram->header.msg_type = unique ? 0x10 : 0x11;
2107 dgram->header.flags.node_type = M_NODE;
2108 dgram->header.flags.first = True;
2109 dgram->header.flags.more = False;
2110 dgram->header.dgm_id = generate_name_trn_id();
2111 dgram->header.source_ip = src_ip;
2112 dgram->header.source_port = DGRAM_PORT;
2113 dgram->header.dgm_length = 0; /* Let build_dgram() handle this. */
2114 dgram->header.packet_offset = 0;
2116 make_nmb_name(&dgram->source_name,srcname,src_type);
2117 make_nmb_name(&dgram->dest_name,dstname,dest_type);
2119 ptr = &dgram->data[0];
2121 /* Setup the smb part. */
2122 ptr -= 4; /* XXX Ugliness because of handling of tcp SMB length. */
2125 if (smb_size + 17*2 + strlen(mailslot) + 1 + len > MAX_DGRAM_SIZE) {
2126 DEBUG(0, ("send_mailslot: Cannot write beyond end of packet\n"));
2130 cli_set_message(ptr,17,strlen(mailslot) + 1 + len,True);
2133 SCVAL(ptr,smb_com,SMBtrans);
2134 SSVAL(ptr,smb_vwv1,len);
2135 SSVAL(ptr,smb_vwv11,len);
2136 SSVAL(ptr,smb_vwv12,70 + strlen(mailslot));
2137 SSVAL(ptr,smb_vwv13,3);
2138 SSVAL(ptr,smb_vwv14,1);
2139 SSVAL(ptr,smb_vwv15,1);
2140 SSVAL(ptr,smb_vwv16,2);
2142 strlcpy_base(p2, mailslot, dgram->data, sizeof(dgram->data));
2143 p2 = skip_string(ptr,MAX_DGRAM_SIZE,p2);
2145 if (((p2+len) > dgram->data+sizeof(dgram->data)) || ((p2+len) < p2)) {
2146 DEBUG(0, ("send_mailslot: Cannot write beyond end of packet\n"));
2155 dgram->datasize = PTR_DIFF(p2,ptr+4); /* +4 for tcp length. */
2160 p.send_fd = find_subnet_mailslot_fd_for_address( src_ip );
2161 p.timestamp = time(NULL);
2162 p.packet_type = DGRAM_PACKET;
2164 DEBUG(4,("send_mailslot: Sending to mailslot %s from %s IP %s ", mailslot,
2165 nmb_namestr(&dgram->source_name), inet_ntoa(src_ip)));
2166 DEBUG(4,("to %s IP %s\n", nmb_namestr(&dgram->dest_name), inet_ntoa(dest_ip)));
2168 debug_browse_data(buf, len);
2170 if(loopback_this_packet) {
2171 struct packet_struct *lo_packet = NULL;
2172 DEBUG(5,("send_mailslot: sending packet to ourselves.\n"));
2173 if((lo_packet = copy_packet(&p)) == NULL)
2175 queue_packet(lo_packet);
2178 return(send_packet(&p));