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"
27 extern int ClientDGRAM;
28 extern int global_nmb_port;
30 extern int num_response_packets;
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 get_nb_flags(char *buf)
97 return ((((uint16)*buf)&0xFFFF) & NB_FLGMSK);
100 void set_nb_flags(char *buf, uint16 nb_flags)
102 *buf++ = ((nb_flags & NB_FLGMSK) & 0xFF);
106 /***************************************************************************
107 Dumps out the browse packet data.
108 **************************************************************************/
110 static void debug_browse_data(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 name_trn_id=0;
148 static uint16 generate_name_trn_id(void)
151 name_trn_id = ((unsigned)time(NULL)%(unsigned)0x7FFF) + ((unsigned)sys_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 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 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 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 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 %s to ip %s \
965 for id %hu\n", packet_type, nmb_namestr(&orig_nmb->question.question_name),
966 inet_ntoa(packet.ip), orig_nmb->header.name_trn_id));
968 nmb->header.name_trn_id = orig_nmb->header.name_trn_id;
969 nmb->header.opcode = opcode;
970 nmb->header.response = True;
971 nmb->header.nm_flags.bcast = False;
972 nmb->header.nm_flags.trunc = False;
973 nmb->header.nm_flags.authoritative = True;
975 nmb->header.rcode = rcode;
976 nmb->header.qdcount = 0;
977 nmb->header.ancount = 1;
978 nmb->header.nscount = 0;
979 nmb->header.arcount = 0;
981 memset((char*)&nmb->question,'\0',sizeof(nmb->question));
983 nmb->answers = &answers;
984 memset((char*)nmb->answers,'\0',sizeof(*nmb->answers));
986 nmb->answers->rr_name = orig_nmb->question.question_name;
987 nmb->answers->rr_type = rr_type;
988 nmb->answers->rr_class = RR_CLASS_IN;
989 nmb->answers->ttl = ttl;
992 if (len < 0 || len > sizeof(nmb->answers->rdata)) {
993 DEBUG(5,("reply_netbios_packet: "
994 "invalid packet len (%d)\n",
998 nmb->answers->rdlength = len;
999 memcpy(nmb->answers->rdata, data, len);
1002 packet.packet_type = NMB_PACKET;
1003 packet.recv_fd = -1;
1004 /* Ensure we send out on the same fd that the original
1005 packet came in on to give the correct source IP address. */
1006 if (orig_packet->send_fd != -1) {
1007 packet.send_fd = orig_packet->send_fd;
1009 packet.send_fd = orig_packet->recv_fd;
1011 packet.timestamp = time(NULL);
1013 debug_nmb_packet(&packet);
1015 if(loopback_this_packet) {
1016 struct packet_struct *lo_packet;
1017 DEBUG(5,("reply_netbios_packet: sending packet to ourselves.\n"));
1018 if((lo_packet = copy_packet(&packet)) == NULL)
1020 queue_packet(lo_packet);
1021 } else if (!send_packet(&packet)) {
1022 DEBUG(0,("reply_netbios_packet: send_packet to IP %s port %d failed\n",
1023 inet_ntoa(packet.ip),packet.port));
1027 /*******************************************************************
1028 Queue a packet into a packet queue
1029 ******************************************************************/
1031 void queue_packet(struct packet_struct *packet)
1033 DLIST_ADD_END(packet_queue, packet, struct packet_struct *);
1036 /****************************************************************************
1037 Try and find a matching subnet record for a datagram port 138 packet.
1038 ****************************************************************************/
1040 static struct subnet_record *find_subnet_for_dgram_browse_packet(struct packet_struct *p)
1042 struct subnet_record *subrec;
1044 /* Go through all the broadcast subnets and see if the mask matches. */
1045 for (subrec = FIRST_SUBNET; subrec ; subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec)) {
1046 if(same_net_v4(p->ip, subrec->bcast_ip, subrec->mask_ip))
1050 /* If the subnet record is the remote announce broadcast subnet,
1051 hack it here to be the first subnet. This is really gross and
1052 is needed due to people turning on port 137/138 broadcast
1053 forwarding on their routers. May fire and brimstone rain
1057 return FIRST_SUBNET;
1060 /****************************************************************************
1061 Dispatch a browse frame from port 138 to the correct processing function.
1062 ****************************************************************************/
1064 static void process_browse_packet(struct packet_struct *p, char *buf,int len)
1066 struct dgram_packet *dgram = &p->packet.dgram;
1067 int command = CVAL(buf,0);
1068 struct subnet_record *subrec = find_subnet_for_dgram_browse_packet(p);
1072 /* Drop the packet if it's a different NetBIOS scope, or the source is from one of our names. */
1073 pull_ascii(scope, dgram->dest_name.scope, 64, 64, STR_TERMINATE);
1074 if (!strequal(scope, global_scope())) {
1075 DEBUG(7,("process_browse_packet: Discarding datagram from IP %s. Scope (%s) \
1076 mismatch with our scope (%s).\n", inet_ntoa(p->ip), scope, global_scope()));
1080 pull_ascii_nstring(src_name, sizeof(src_name), dgram->source_name.name);
1081 if (is_myname(src_name)) {
1082 DEBUG(7,("process_browse_packet: Discarding datagram from IP %s. Source name \
1083 %s is one of our names !\n", inet_ntoa(p->ip), nmb_namestr(&dgram->source_name)));
1088 case ANN_HostAnnouncement:
1089 debug_browse_data(buf, len);
1090 process_host_announce(subrec, p, buf+1);
1092 case ANN_DomainAnnouncement:
1093 debug_browse_data(buf, len);
1094 process_workgroup_announce(subrec, p, buf+1);
1096 case ANN_LocalMasterAnnouncement:
1097 debug_browse_data(buf, len);
1098 process_local_master_announce(subrec, p, buf+1);
1100 case ANN_AnnouncementRequest:
1101 debug_browse_data(buf, len);
1102 process_announce_request(subrec, p, buf+1);
1105 debug_browse_data(buf, len);
1106 process_election(subrec, p, buf+1);
1108 case ANN_GetBackupListReq:
1109 debug_browse_data(buf, len);
1110 process_get_backup_list_request(subrec, p, buf+1);
1112 case ANN_GetBackupListResp:
1113 debug_browse_data(buf, len);
1114 /* We never send ANN_GetBackupListReq so we should never get these. */
1115 DEBUG(0,("process_browse_packet: Discarding GetBackupListResponse \
1116 packet from %s IP %s\n", nmb_namestr(&dgram->source_name), inet_ntoa(p->ip)));
1118 case ANN_ResetBrowserState:
1119 debug_browse_data(buf, len);
1120 process_reset_browser(subrec, p, buf+1);
1122 case ANN_MasterAnnouncement:
1123 /* Master browser datagrams must be processed on the unicast subnet. */
1124 subrec = unicast_subnet;
1126 debug_browse_data(buf, len);
1127 process_master_browser_announce(subrec, p, buf+1);
1129 case ANN_BecomeBackup:
1131 * We don't currently implement this. Log it just in case.
1133 debug_browse_data(buf, len);
1134 DEBUG(10,("process_browse_packet: On subnet %s ignoring browse packet \
1135 command ANN_BecomeBackup from %s IP %s to %s\n", subrec->subnet_name, nmb_namestr(&dgram->source_name),
1136 inet_ntoa(p->ip), nmb_namestr(&dgram->dest_name)));
1139 debug_browse_data(buf, len);
1140 DEBUG(0,("process_browse_packet: On subnet %s ignoring browse packet \
1141 command code %d from %s IP %s to %s\n", subrec->subnet_name, command, nmb_namestr(&dgram->source_name),
1142 inet_ntoa(p->ip), nmb_namestr(&dgram->dest_name)));
1147 /****************************************************************************
1148 Dispatch a LanMan browse frame from port 138 to the correct processing function.
1149 ****************************************************************************/
1151 static void process_lanman_packet(struct packet_struct *p, char *buf,int len)
1153 struct dgram_packet *dgram = &p->packet.dgram;
1154 int command = SVAL(buf,0);
1155 struct subnet_record *subrec = find_subnet_for_dgram_browse_packet(p);
1159 /* Drop the packet if it's a different NetBIOS scope, or the source is from one of our names. */
1161 pull_ascii(scope, dgram->dest_name.scope, 64, 64, STR_TERMINATE);
1162 if (!strequal(scope, global_scope())) {
1163 DEBUG(7,("process_lanman_packet: Discarding datagram from IP %s. Scope (%s) \
1164 mismatch with our scope (%s).\n", inet_ntoa(p->ip), scope, global_scope()));
1168 pull_ascii_nstring(src_name, sizeof(src_name), dgram->source_name.name);
1169 if (is_myname(src_name)) {
1170 DEBUG(0,("process_lanman_packet: Discarding datagram from IP %s. Source name \
1171 %s is one of our names !\n", inet_ntoa(p->ip), nmb_namestr(&dgram->source_name)));
1176 case ANN_HostAnnouncement:
1177 debug_browse_data(buf, len);
1178 process_lm_host_announce(subrec, p, buf+1, len > 1 ? len-1 : 0);
1180 case ANN_AnnouncementRequest:
1181 process_lm_announce_request(subrec, p, buf+1, len > 1 ? len-1 : 0);
1184 DEBUG(0,("process_lanman_packet: On subnet %s ignoring browse packet \
1185 command code %d from %s IP %s to %s\n", subrec->subnet_name, command, nmb_namestr(&dgram->source_name),
1186 inet_ntoa(p->ip), nmb_namestr(&dgram->dest_name)));
1191 /****************************************************************************
1192 Determine if a packet is for us on port 138. Note that to have any chance of
1193 being efficient we need to drop as many packets as possible at this
1194 stage as subsequent processing is expensive.
1195 ****************************************************************************/
1197 static bool listening(struct packet_struct *p,struct nmb_name *nbname)
1199 struct subnet_record *subrec = NULL;
1201 for (subrec = FIRST_SUBNET; subrec ; subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec)) {
1202 if(same_net_v4(p->ip, subrec->bcast_ip, subrec->mask_ip))
1207 subrec = unicast_subnet;
1209 return (find_name_on_subnet(subrec, nbname, FIND_SELF_NAME) != NULL);
1212 /****************************************************************************
1213 Process udp 138 datagrams
1214 ****************************************************************************/
1216 static void process_dgram(struct packet_struct *p)
1221 struct dgram_packet *dgram = &p->packet.dgram;
1223 /* If we aren't listening to the destination name then ignore the packet */
1224 if (!listening(p,&dgram->dest_name)) {
1225 nb_packet_dispatch(packet_server, p);
1226 DEBUG(5,("process_dgram: ignoring dgram packet sent to name %s from %s\n",
1227 nmb_namestr(&dgram->dest_name), inet_ntoa(p->ip)));
1231 if (dgram->header.msg_type != 0x10 && dgram->header.msg_type != 0x11 && dgram->header.msg_type != 0x12) {
1232 nb_packet_dispatch(packet_server, p);
1233 /* Don't process error packets etc yet */
1234 DEBUG(5,("process_dgram: ignoring dgram packet sent to name %s from IP %s as it is \
1235 an error packet of type %x\n", nmb_namestr(&dgram->dest_name), inet_ntoa(p->ip), dgram->header.msg_type));
1239 /* Ensure we have a large enough packet before looking inside. */
1240 if (dgram->datasize < (smb_vwv12 - 2)) {
1241 /* That's the offset minus the 4 byte length + 2 bytes of offset. */
1242 DEBUG(0,("process_dgram: ignoring too short dgram packet (%u) sent to name %s from IP %s\n",
1243 (unsigned int)dgram->datasize,
1244 nmb_namestr(&dgram->dest_name),
1245 inet_ntoa(p->ip) ));
1249 buf = &dgram->data[0];
1250 buf -= 4; /* XXXX for the pseudo tcp length - someday I need to get rid of this */
1252 if (CVAL(buf,smb_com) != SMBtrans)
1255 len = SVAL(buf,smb_vwv11);
1256 buf2 = smb_base(buf) + SVAL(buf,smb_vwv12);
1258 if (len <= 0 || len > dgram->datasize) {
1259 DEBUG(0,("process_dgram: ignoring malformed1 (datasize = %d, len = %d) datagram \
1260 packet sent to name %s from IP %s\n",
1263 nmb_namestr(&dgram->dest_name),
1264 inet_ntoa(p->ip) ));
1268 if (buf2 < dgram->data || (buf2 >= dgram->data + dgram->datasize)) {
1269 DEBUG(0,("process_dgram: ignoring malformed2 (datasize = %d, len=%d, off=%d) datagram \
1270 packet sent to name %s from IP %s\n",
1273 (int)PTR_DIFF(buf2, dgram->data),
1274 nmb_namestr(&dgram->dest_name),
1275 inet_ntoa(p->ip) ));
1279 if ((buf2 + len < dgram->data) || (buf2 + len > dgram->data + dgram->datasize)) {
1280 DEBUG(0,("process_dgram: ignoring malformed3 (datasize = %d, len=%d, off=%d) datagram \
1281 packet sent to name %s from IP %s\n",
1284 (int)PTR_DIFF(buf2, dgram->data),
1285 nmb_namestr(&dgram->dest_name),
1286 inet_ntoa(p->ip) ));
1290 DEBUG(4,("process_dgram: datagram from %s to %s IP %s for %s of type %d len=%d\n",
1291 nmb_namestr(&dgram->source_name),nmb_namestr(&dgram->dest_name),
1292 inet_ntoa(p->ip), smb_buf(buf),CVAL(buf2,0),len));
1294 /* Datagram packet received for the browser mailslot */
1295 if (strequal(smb_buf(buf),BROWSE_MAILSLOT)) {
1296 process_browse_packet(p,buf2,len);
1300 /* Datagram packet received for the LAN Manager mailslot */
1301 if (strequal(smb_buf(buf),LANMAN_MAILSLOT)) {
1302 process_lanman_packet(p,buf2,len);
1306 /* Datagram packet received for the domain logon mailslot */
1307 if (strequal(smb_buf(buf),NET_LOGON_MAILSLOT)) {
1308 process_logon_packet(p,buf2,len,NET_LOGON_MAILSLOT);
1312 /* Datagram packet received for the NT domain logon mailslot */
1313 if (strequal(smb_buf(buf),NT_LOGON_MAILSLOT)) {
1314 process_logon_packet(p,buf2,len,NT_LOGON_MAILSLOT);
1318 nb_packet_dispatch(packet_server, p);
1321 /****************************************************************************
1322 Validate a response nmb packet.
1323 ****************************************************************************/
1325 static bool validate_nmb_response_packet( struct nmb_packet *nmb )
1327 bool ignore = False;
1329 switch (nmb->header.opcode) {
1330 case NMB_NAME_REG_OPCODE:
1331 case NMB_NAME_REFRESH_OPCODE_8: /* ambiguity in rfc1002 about which is correct. */
1332 case NMB_NAME_REFRESH_OPCODE_9: /* WinNT uses 8 by default. */
1333 if (nmb->header.ancount == 0) {
1334 DEBUG(0,("validate_nmb_response_packet: Bad REG/REFRESH Packet. "));
1339 case NMB_NAME_QUERY_OPCODE:
1340 if ((nmb->header.ancount != 0) && (nmb->header.ancount != 1)) {
1341 DEBUG(0,("validate_nmb_response_packet: Bad QUERY Packet. "));
1346 case NMB_NAME_RELEASE_OPCODE:
1347 if (nmb->header.ancount == 0) {
1348 DEBUG(0,("validate_nmb_response_packet: Bad RELEASE Packet. "));
1353 case NMB_WACK_OPCODE:
1354 /* Check WACK response here. */
1355 if (nmb->header.ancount != 1) {
1356 DEBUG(0,("validate_nmb_response_packet: Bad WACK Packet. "));
1361 DEBUG(0,("validate_nmb_response_packet: Ignoring packet with unknown opcode %d.\n",
1362 nmb->header.opcode));
1367 DEBUG(0,("Ignoring response packet with opcode %d.\n", nmb->header.opcode));
1372 /****************************************************************************
1373 Validate a request nmb packet.
1374 ****************************************************************************/
1376 static bool validate_nmb_packet( struct nmb_packet *nmb )
1378 bool ignore = False;
1380 switch (nmb->header.opcode) {
1381 case NMB_NAME_REG_OPCODE:
1382 case NMB_NAME_REFRESH_OPCODE_8: /* ambiguity in rfc1002 about which is correct. */
1383 case NMB_NAME_REFRESH_OPCODE_9: /* WinNT uses 8 by default. */
1384 case NMB_NAME_MULTIHOMED_REG_OPCODE:
1385 if (nmb->header.qdcount==0 || nmb->header.arcount==0) {
1386 DEBUG(0,("validate_nmb_packet: Bad REG/REFRESH Packet. "));
1391 case NMB_NAME_QUERY_OPCODE:
1392 if ((nmb->header.qdcount == 0) || ((nmb->question.question_type != QUESTION_TYPE_NB_QUERY) &&
1393 (nmb->question.question_type != QUESTION_TYPE_NB_STATUS))) {
1394 DEBUG(0,("validate_nmb_packet: Bad QUERY Packet. "));
1399 case NMB_NAME_RELEASE_OPCODE:
1400 if (nmb->header.qdcount==0 || nmb->header.arcount==0) {
1401 DEBUG(0,("validate_nmb_packet: Bad RELEASE Packet. "));
1406 DEBUG(0,("validate_nmb_packet: Ignoring packet with unknown opcode %d.\n",
1407 nmb->header.opcode));
1412 DEBUG(0,("validate_nmb_packet: Ignoring request packet with opcode %d.\n", nmb->header.opcode));
1417 /****************************************************************************
1418 Find a subnet (and potentially a response record) for a packet.
1419 ****************************************************************************/
1421 static struct subnet_record *find_subnet_for_nmb_packet( struct packet_struct *p,
1422 struct response_record **pprrec)
1424 struct nmb_packet *nmb = &p->packet.nmb;
1425 struct response_record *rrec = NULL;
1426 struct subnet_record *subrec = NULL;
1431 if(nmb->header.response) {
1432 /* It's a response packet. Find a record for it or it's an error. */
1434 rrec = find_response_record( &subrec, nmb->header.name_trn_id);
1436 DEBUG(3,("find_subnet_for_nmb_packet: response record not found for response id %hu\n",
1437 nmb->header.name_trn_id));
1438 nb_packet_dispatch(packet_server, p);
1442 if(subrec == NULL) {
1443 DEBUG(0,("find_subnet_for_nmb_packet: subnet record not found for response id %hu\n",
1444 nmb->header.name_trn_id));
1453 /* Try and see what subnet this packet belongs to. */
1456 if(packet_is_for_wins_server(p))
1457 return wins_server_subnet;
1459 /* If it wasn't a broadcast packet then send to the UNICAST subnet. */
1460 if(nmb->header.nm_flags.bcast == False)
1461 return unicast_subnet;
1463 /* Go through all the broadcast subnets and see if the mask matches. */
1464 for (subrec = FIRST_SUBNET; subrec ; subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec)) {
1465 if(same_net_v4(p->ip, subrec->bcast_ip, subrec->mask_ip))
1469 /* If none match it must have been a directed broadcast - assign the remote_broadcast_subnet. */
1470 return remote_broadcast_subnet;
1473 /****************************************************************************
1474 Process a nmb request packet - validate the packet and route it.
1475 ****************************************************************************/
1477 static void process_nmb_request(struct packet_struct *p)
1479 struct nmb_packet *nmb = &p->packet.nmb;
1480 struct subnet_record *subrec = NULL;
1482 debug_nmb_packet(p);
1484 /* Ensure we have a good packet. */
1485 if(validate_nmb_packet(nmb))
1488 /* Allocate a subnet to this packet - if we cannot - fail. */
1489 if((subrec = find_subnet_for_nmb_packet(p, NULL))==NULL)
1492 switch (nmb->header.opcode) {
1493 case NMB_NAME_REG_OPCODE:
1494 if(subrec == wins_server_subnet)
1495 wins_process_name_registration_request(subrec, p);
1497 process_name_registration_request(subrec, p);
1500 case NMB_NAME_REFRESH_OPCODE_8: /* ambiguity in rfc1002 about which is correct. */
1501 case NMB_NAME_REFRESH_OPCODE_9:
1502 if(subrec == wins_server_subnet)
1503 wins_process_name_refresh_request(subrec, p);
1505 process_name_refresh_request(subrec, p);
1508 case NMB_NAME_MULTIHOMED_REG_OPCODE:
1509 if(subrec == wins_server_subnet) {
1510 wins_process_multihomed_name_registration_request(subrec, p);
1512 DEBUG(0,("process_nmb_request: Multihomed registration request must be \
1513 directed at a WINS server.\n"));
1517 case NMB_NAME_QUERY_OPCODE:
1518 switch (nmb->question.question_type) {
1519 case QUESTION_TYPE_NB_QUERY:
1520 if(subrec == wins_server_subnet)
1521 wins_process_name_query_request(subrec, p);
1523 process_name_query_request(subrec, p);
1525 case QUESTION_TYPE_NB_STATUS:
1526 if(subrec == wins_server_subnet) {
1527 DEBUG(0,("process_nmb_request: NB_STATUS request directed at WINS server is \
1531 process_node_status_request(subrec, p);
1537 case NMB_NAME_RELEASE_OPCODE:
1538 if(subrec == wins_server_subnet)
1539 wins_process_name_release_request(subrec, p);
1541 process_name_release_request(subrec, p);
1546 /****************************************************************************
1547 Process a nmb response packet - validate the packet and route it.
1548 to either the WINS server or a normal response.
1549 ****************************************************************************/
1551 static void process_nmb_response(struct packet_struct *p)
1553 struct nmb_packet *nmb = &p->packet.nmb;
1554 struct subnet_record *subrec = NULL;
1555 struct response_record *rrec = NULL;
1557 debug_nmb_packet(p);
1559 if(validate_nmb_response_packet(nmb))
1562 if((subrec = find_subnet_for_nmb_packet(p, &rrec))==NULL)
1566 DEBUG(0,("process_nmb_response: response packet received but no response record \
1567 found for id = %hu. Ignoring packet.\n", nmb->header.name_trn_id));
1571 /* Increment the number of responses received for this record. */
1573 /* Ensure we don't re-send the request. */
1574 rrec->repeat_count = 0;
1576 /* Call the response received function for this packet. */
1577 (*rrec->resp_fn)(subrec, rrec, p);
1580 /*******************************************************************
1581 Run elements off the packet queue till its empty
1582 ******************************************************************/
1584 void run_packet_queue(void)
1586 struct packet_struct *p;
1588 while ((p = packet_queue)) {
1589 DLIST_REMOVE(packet_queue, p);
1591 switch (p->packet_type) {
1593 if(p->packet.nmb.header.response)
1594 process_nmb_response(p);
1596 process_nmb_request(p);
1607 /*******************************************************************
1608 Retransmit or timeout elements from all the outgoing subnet response
1609 record queues. NOTE that this code must also check the WINS server
1610 subnet for response records to timeout as the WINS server code
1611 can send requests to check if a client still owns a name.
1612 (Patch from Andrey Alekseyev <fetch@muffin.arcadia.spb.ru>).
1613 ******************************************************************/
1615 void retransmit_or_expire_response_records(time_t t)
1617 struct subnet_record *subrec;
1619 for (subrec = FIRST_SUBNET; subrec; subrec = get_next_subnet_maybe_unicast_or_wins_server(subrec)) {
1620 struct response_record *rrec, *nextrrec;
1624 for (rrec = subrec->responselist; rrec; rrec = nextrrec) {
1625 nextrrec = rrec->next;
1627 if (rrec->repeat_time <= t) {
1628 if (rrec->repeat_count > 0) {
1629 /* Resend while we have a non-zero repeat_count. */
1630 if(!send_packet(rrec->packet)) {
1631 DEBUG(0,("retransmit_or_expire_response_records: Failed to resend packet id %hu \
1632 to IP %s on subnet %s\n", rrec->response_id, inet_ntoa(rrec->packet->ip), subrec->subnet_name));
1634 rrec->repeat_time = t + rrec->repeat_interval;
1635 rrec->repeat_count--;
1637 DEBUG(4,("retransmit_or_expire_response_records: timeout for packet id %hu to IP %s \
1638 on subnet %s\n", rrec->response_id, inet_ntoa(rrec->packet->ip), subrec->subnet_name));
1641 * Check the flag in this record to prevent recursion if we end
1642 * up in this function again via the timeout function call.
1645 if(!rrec->in_expiration_processing) {
1648 * Set the recursion protection flag in this record.
1651 rrec->in_expiration_processing = True;
1653 /* Call the timeout function. This will deal with removing the
1654 timed out packet. */
1655 if(rrec->timeout_fn) {
1656 (*rrec->timeout_fn)(subrec, rrec);
1658 /* We must remove the record ourself if there is
1659 no timeout function. */
1660 remove_response_record(subrec, rrec);
1662 /* We have changed subrec->responselist,
1663 * restart from the beginning of this list. */
1665 } /* !rrec->in_expitation_processing */
1666 } /* rrec->repeat_count > 0 */
1667 } /* rrec->repeat_time <= t */
1668 } /* end for rrec */
1669 } /* end for subnet */
1672 /****************************************************************************
1673 Create an fd_set containing all the sockets in the subnet structures,
1674 plus the broadcast sockets.
1675 ***************************************************************************/
1677 static bool create_listen_fdset(fd_set **ppset, int **psock_array, int *listen_number, int *maxfd)
1679 int *sock_array = NULL;
1680 struct subnet_record *subrec = NULL;
1683 fd_set *pset = SMB_MALLOC_P(fd_set);
1686 DEBUG(0,("create_listen_fdset: malloc fail !\n"));
1690 /* The Client* sockets */
1693 /* Check that we can add all the fd's we need. */
1694 for (subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec))
1697 /* each interface gets 4 sockets */
1700 if(count > FD_SETSIZE) {
1701 DEBUG(0,("create_listen_fdset: Too many file descriptors needed (%d). We can \
1702 only use %d.\n", count, FD_SETSIZE));
1707 if((sock_array = SMB_MALLOC_ARRAY(int, count)) == NULL) {
1708 DEBUG(0,("create_listen_fdset: malloc fail for socket array. size %d\n", count));
1715 /* Add in the lp_socket_address() interface on 137. */
1716 FD_SET(ClientNMB,pset);
1717 sock_array[num++] = ClientNMB;
1718 *maxfd = MAX( *maxfd, ClientNMB);
1720 /* the lp_socket_address() interface has only one socket */
1721 sock_array[num++] = -1;
1723 /* Add in the 137 sockets on all the interfaces. */
1724 for (subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec)) {
1725 FD_SET(subrec->nmb_sock,pset);
1726 sock_array[num++] = subrec->nmb_sock;
1727 *maxfd = MAX( *maxfd, subrec->nmb_sock);
1729 sock_array[num++] = subrec->nmb_bcast;
1730 if (subrec->nmb_bcast != -1) {
1731 FD_SET(subrec->nmb_bcast,pset);
1732 *maxfd = MAX( *maxfd, subrec->nmb_bcast);
1736 /* Add in the lp_socket_address() interface on 138. */
1737 FD_SET(ClientDGRAM,pset);
1738 sock_array[num++] = ClientDGRAM;
1739 *maxfd = MAX( *maxfd, ClientDGRAM);
1741 /* the lp_socket_address() interface has only one socket */
1742 sock_array[num++] = -1;
1744 /* Add in the 138 sockets on all the interfaces. */
1745 for (subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec)) {
1746 FD_SET(subrec->dgram_sock,pset);
1747 sock_array[num++] = subrec->dgram_sock;
1748 *maxfd = MAX( *maxfd, subrec->dgram_sock);
1750 sock_array[num++] = subrec->dgram_bcast;
1751 if (subrec->dgram_bcast != -1) {
1752 FD_SET(subrec->dgram_bcast,pset);
1753 *maxfd = MAX( *maxfd, subrec->dgram_bcast);
1757 *listen_number = count;
1760 SAFE_FREE(*psock_array);
1763 *psock_array = sock_array;
1768 /****************************************************************************
1769 List of packets we're processing this select.
1770 ***************************************************************************/
1772 struct processed_packet {
1773 struct processed_packet *next;
1774 struct processed_packet *prev;
1775 enum packet_type packet_type;
1780 /****************************************************************************
1781 Have we seen this before ?
1782 ***************************************************************************/
1784 static bool is_processed_packet(struct processed_packet *processed_packet_list,
1785 struct packet_struct *packet)
1787 struct processed_packet *p = NULL;
1789 for (p = processed_packet_list; p; p = p->next) {
1790 if (ip_equal_v4(p->ip, packet->ip) && p->packet_type == packet->packet_type) {
1791 if ((p->packet_type == NMB_PACKET) &&
1793 packet->packet.nmb.header.name_trn_id)) {
1795 } else if ((p->packet_type == DGRAM_PACKET) &&
1797 packet->packet.dgram.header.dgm_id)) {
1805 /****************************************************************************
1806 Keep a list of what we've seen before.
1807 ***************************************************************************/
1809 static bool store_processed_packet(struct processed_packet **pp_processed_packet_list,
1810 struct packet_struct *packet)
1812 struct processed_packet *p = SMB_MALLOC_P(struct processed_packet);
1816 p->packet_type = packet->packet_type;
1818 if (packet->packet_type == NMB_PACKET) {
1819 p->packet_id = packet->packet.nmb.header.name_trn_id;
1820 } else if (packet->packet_type == DGRAM_PACKET) {
1821 p->packet_id = packet->packet.dgram.header.dgm_id;
1826 DLIST_ADD(*pp_processed_packet_list, p);
1830 /****************************************************************************
1831 Throw away what we've seen before.
1832 ***************************************************************************/
1834 static void free_processed_packet_list(struct processed_packet **pp_processed_packet_list)
1836 struct processed_packet *p = NULL, *next = NULL;
1838 for (p = *pp_processed_packet_list; p; p = next) {
1840 DLIST_REMOVE(*pp_processed_packet_list, p);
1845 /****************************************************************************
1846 Listens for NMB or DGRAM packets, and queues them.
1847 return True if the socket is dead
1848 ***************************************************************************/
1850 bool listen_for_packets(bool run_election)
1852 static fd_set *listen_set = NULL;
1853 static int listen_number = 0;
1854 static int *sock_array = NULL;
1856 static int maxfd = 0;
1861 struct timeval timeout;
1865 struct processed_packet *processed_packet_list = NULL;
1867 if(listen_set == NULL || rescan_listen_set) {
1868 if(create_listen_fdset(&listen_set, &sock_array, &listen_number, &maxfd)) {
1869 DEBUG(0,("listen_for_packets: Fatal error. unable to create listen set. Exiting.\n"));
1872 rescan_listen_set = False;
1875 memcpy((char *)&r_fds, (char *)listen_set, sizeof(fd_set));
1879 dns_fd = asyncdns_fd();
1881 FD_SET(dns_fd, &r_fds);
1882 maxfd = MAX( maxfd, dns_fd);
1886 /* Process a signal and timer events now... */
1887 if (run_events(nmbd_event_context(), 0, NULL, NULL)) {
1892 * During elections and when expecting a netbios response packet we
1893 * need to send election packets at tighter intervals.
1894 * Ideally it needs to be the interval (in ms) between time now and
1895 * the time we are expecting the next netbios packet.
1898 timeout.tv_sec = (run_election||num_response_packets) ? 1 : NMBD_SELECT_LOOP;
1899 timeout.tv_usec = 0;
1901 event_add_to_select_args(nmbd_event_context(),
1902 &r_fds, &w_fds, &timeout, &maxfd);
1904 selrtn = sys_select(maxfd+1,&r_fds,&w_fds,NULL,&timeout);
1906 if (run_events(nmbd_event_context(), selrtn, &r_fds, &w_fds)) {
1915 if (dns_fd != -1 && FD_ISSET(dns_fd,&r_fds)) {
1920 for(i = 0; i < listen_number; i++) {
1921 enum packet_type packet_type;
1922 struct packet_struct *packet;
1923 const char *packet_name;
1927 if (sock_array[i] == -1) {
1931 if (!FD_ISSET(sock_array[i],&r_fds)) {
1935 if (i < (listen_number/2)) {
1937 packet_type = NMB_PACKET;
1938 packet_name = "nmb";
1939 client_fd = ClientNMB;
1940 client_port = global_nmb_port;
1943 packet_type = DGRAM_PACKET;
1944 packet_name = "dgram";
1945 client_fd = ClientDGRAM;
1946 client_port = DGRAM_PORT;
1949 packet = read_packet(sock_array[i], packet_type);
1955 * If we got a packet on the broadcast socket and interfaces
1956 * only is set then check it came from one of our local nets.
1958 if (lp_bind_interfaces_only() &&
1959 (sock_array[i] == client_fd) &&
1960 (!is_local_net_v4(packet->ip))) {
1961 DEBUG(7,("discarding %s packet sent to broadcast socket from %s:%d\n",
1962 packet_name, inet_ntoa(packet->ip), packet->port));
1963 free_packet(packet);
1967 if ((is_loopback_ip_v4(packet->ip) || ismyip_v4(packet->ip)) &&
1968 packet->port == client_port)
1970 if (client_port == DGRAM_PORT) {
1971 DEBUG(7,("discarding own dgram packet from %s:%d\n",
1972 inet_ntoa(packet->ip),packet->port));
1973 free_packet(packet);
1977 if (packet->packet.nmb.header.nm_flags.bcast) {
1978 DEBUG(7,("discarding own nmb bcast packet from %s:%d\n",
1979 inet_ntoa(packet->ip),packet->port));
1980 free_packet(packet);
1985 if (is_processed_packet(processed_packet_list, packet)) {
1986 DEBUG(7,("discarding duplicate packet from %s:%d\n",
1987 inet_ntoa(packet->ip),packet->port));
1988 free_packet(packet);
1992 store_processed_packet(&processed_packet_list, packet);
1995 * 0,2,4,... are unicast sockets
1996 * 1,3,5,... are broadcast sockets
1998 * on broadcast socket we only receive packets
1999 * and send replies via the unicast socket.
2001 * 0,1 and 2,3 and ... belong together.
2004 /* this is a broadcast socket */
2005 packet->send_fd = sock_array[i-1];
2007 /* this is already a unicast socket */
2008 packet->send_fd = sock_array[i];
2011 queue_packet(packet);
2014 free_processed_packet_list(&processed_packet_list);
2018 /****************************************************************************
2019 Construct and send a netbios DGRAM.
2020 **************************************************************************/
2022 bool send_mailslot(bool unique, const char *mailslot,char *buf, size_t len,
2023 const char *srcname, int src_type,
2024 const char *dstname, int dest_type,
2025 struct in_addr dest_ip,struct in_addr src_ip,
2028 bool loopback_this_packet = False;
2029 struct packet_struct p;
2030 struct dgram_packet *dgram = &p.packet.dgram;
2034 memset((char *)&p,'\0',sizeof(p));
2036 if(ismyip_v4(dest_ip) && (dest_port == DGRAM_PORT)) /* Only if to DGRAM_PORT */
2037 loopback_this_packet = True;
2039 /* generate_name_trn_id(); */ /* Not used, so gone, RJS */
2041 /* DIRECT GROUP or UNIQUE datagram. */
2042 dgram->header.msg_type = unique ? 0x10 : 0x11;
2043 dgram->header.flags.node_type = M_NODE;
2044 dgram->header.flags.first = True;
2045 dgram->header.flags.more = False;
2046 dgram->header.dgm_id = generate_name_trn_id();
2047 dgram->header.source_ip = src_ip;
2048 dgram->header.source_port = DGRAM_PORT;
2049 dgram->header.dgm_length = 0; /* Let build_dgram() handle this. */
2050 dgram->header.packet_offset = 0;
2052 make_nmb_name(&dgram->source_name,srcname,src_type);
2053 make_nmb_name(&dgram->dest_name,dstname,dest_type);
2055 ptr = &dgram->data[0];
2057 /* Setup the smb part. */
2058 ptr -= 4; /* XXX Ugliness because of handling of tcp SMB length. */
2061 if (smb_size + 17*2 + strlen(mailslot) + 1 + len > MAX_DGRAM_SIZE) {
2062 DEBUG(0, ("send_mailslot: Cannot write beyond end of packet\n"));
2066 cli_set_message(ptr,17,strlen(mailslot) + 1 + len,True);
2069 SCVAL(ptr,smb_com,SMBtrans);
2070 SSVAL(ptr,smb_vwv1,len);
2071 SSVAL(ptr,smb_vwv11,len);
2072 SSVAL(ptr,smb_vwv12,70 + strlen(mailslot));
2073 SSVAL(ptr,smb_vwv13,3);
2074 SSVAL(ptr,smb_vwv14,1);
2075 SSVAL(ptr,smb_vwv15,1);
2076 SSVAL(ptr,smb_vwv16,2);
2078 safe_strcpy_base(p2, mailslot, dgram->data, sizeof(dgram->data));
2079 p2 = skip_string(ptr,MAX_DGRAM_SIZE,p2);
2081 if (((p2+len) > dgram->data+sizeof(dgram->data)) || ((p2+len) < p2)) {
2082 DEBUG(0, ("send_mailslot: Cannot write beyond end of packet\n"));
2091 dgram->datasize = PTR_DIFF(p2,ptr+4); /* +4 for tcp length. */
2096 p.send_fd = find_subnet_mailslot_fd_for_address( src_ip );
2097 p.timestamp = time(NULL);
2098 p.packet_type = DGRAM_PACKET;
2100 DEBUG(4,("send_mailslot: Sending to mailslot %s from %s IP %s ", mailslot,
2101 nmb_namestr(&dgram->source_name), inet_ntoa(src_ip)));
2102 DEBUG(4,("to %s IP %s\n", nmb_namestr(&dgram->dest_name), inet_ntoa(dest_ip)));
2104 debug_browse_data(buf, len);
2106 if(loopback_this_packet) {
2107 struct packet_struct *lo_packet = NULL;
2108 DEBUG(5,("send_mailslot: sending packet to ourselves.\n"));
2109 if((lo_packet = copy_packet(&p)) == NULL)
2111 queue_packet(lo_packet);
2114 return(send_packet(&p));