Part of fix for bug 8276 - FD_SET out of bounds access crash.
[Samba/gebeck_regimport.git] / source3 / nmbd / nmbd_packets.c
blob0324c9dd4274402d31d2088785f79bd94f3a1762
1 /*
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/>.
22 #include "includes.h"
23 #include "nmbd/nmbd.h"
24 #include "../lib/util/select.h"
25 #include "system/select.h"
26 #include "libsmb/libsmb.h"
28 extern int ClientNMB;
29 extern int ClientDGRAM;
30 extern int global_nmb_port;
32 extern int num_response_packets;
34 bool rescan_listen_set = False;
36 static struct nb_packet_server *packet_server;
38 bool nmbd_init_packet_server(void)
40 NTSTATUS status;
42 status = nb_packet_server_create(
43 NULL, nmbd_event_context(),
44 lp_parm_int(-1, "nmbd", "unexpected_clients", 200),
45 &packet_server);
46 if (!NT_STATUS_IS_OK(status)) {
47 DEBUG(0, ("ERROR: nb_packet_server_create failed: %s\n",
48 nt_errstr(status)));
49 return false;
51 return true;
55 /*******************************************************************
56 The global packet linked-list. Incoming entries are
57 added to the end of this list. It is supposed to remain fairly
58 short so we won't bother with an end pointer.
59 ******************************************************************/
61 static struct packet_struct *packet_queue = NULL;
63 /***************************************************************************
64 Utility function to find the specific fd to send a packet out on.
65 **************************************************************************/
67 static int find_subnet_fd_for_address( struct in_addr local_ip )
69 struct subnet_record *subrec;
71 for( subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec))
72 if(ip_equal_v4(local_ip, subrec->myip))
73 return subrec->nmb_sock;
75 return ClientNMB;
78 /***************************************************************************
79 Utility function to find the specific fd to send a mailslot packet out on.
80 **************************************************************************/
82 static int find_subnet_mailslot_fd_for_address( struct in_addr local_ip )
84 struct subnet_record *subrec;
86 for( subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec))
87 if(ip_equal_v4(local_ip, subrec->myip))
88 return subrec->dgram_sock;
90 return ClientDGRAM;
93 /***************************************************************************
94 Get/Set problematic nb_flags as network byte order 16 bit int.
95 **************************************************************************/
97 uint16 get_nb_flags(char *buf)
99 return ((((uint16)*buf)&0xFFFF) & NB_FLGMSK);
102 void set_nb_flags(char *buf, uint16 nb_flags)
104 *buf++ = ((nb_flags & NB_FLGMSK) & 0xFF);
105 *buf = '\0';
108 /***************************************************************************
109 Dumps out the browse packet data.
110 **************************************************************************/
112 static void debug_browse_data(const char *outbuf, int len)
114 int i,j;
116 DEBUG( 4, ( "debug_browse_data():\n" ) );
117 for (i = 0; i < len; i+= 16) {
118 DEBUGADD( 4, ( "%3x char ", i ) );
120 for (j = 0; j < 16; j++) {
121 unsigned char x;
122 if (i+j >= len)
123 break;
125 x = outbuf[i+j];
126 if (x < 32 || x > 127)
127 x = '.';
129 DEBUGADD( 4, ( "%c", x ) );
132 DEBUGADD( 4, ( "%*s hex", 16-j, "" ) );
134 for (j = 0; j < 16; j++) {
135 if (i+j >= len)
136 break;
137 DEBUGADD( 4, ( " %02x", (unsigned char)outbuf[i+j] ) );
140 DEBUGADD( 4, ("\n") );
144 /***************************************************************************
145 Generates the unique transaction identifier
146 **************************************************************************/
148 static uint16 name_trn_id=0;
150 static uint16 generate_name_trn_id(void)
152 if (!name_trn_id) {
153 name_trn_id = ((unsigned)time(NULL)%(unsigned)0x7FFF) + ((unsigned)sys_getpid()%(unsigned)100);
155 name_trn_id = (name_trn_id+1) % (unsigned)0x7FFF;
156 return name_trn_id;
159 /***************************************************************************
160 Either loops back or sends out a completed NetBIOS packet.
161 **************************************************************************/
163 static bool send_netbios_packet(struct packet_struct *p)
165 bool loopback_this_packet = False;
167 /* Check if we are sending to or from ourselves as a WINS server. */
168 if(ismyip_v4(p->ip) && (p->port == global_nmb_port))
169 loopback_this_packet = True;
171 if(loopback_this_packet) {
172 struct packet_struct *lo_packet = NULL;
173 DEBUG(5,("send_netbios_packet: sending packet to ourselves.\n"));
174 if((lo_packet = copy_packet(p)) == NULL)
175 return False;
176 queue_packet(lo_packet);
177 } else if (!send_packet(p)) {
178 DEBUG(0,("send_netbios_packet: send_packet() to IP %s port %d failed\n",
179 inet_ntoa(p->ip),p->port));
180 return False;
183 return True;
186 /***************************************************************************
187 Sets up the common elements of an outgoing NetBIOS packet.
189 Note: do not attempt to rationalise whether rec_des should be set or not
190 in a particular situation. Just follow rfc_1002 or look at examples from WinXX.
191 It does NOT follow the rule that requests to the wins server always have
192 rec_des true. See for example name releases and refreshes
193 **************************************************************************/
195 static struct packet_struct *create_and_init_netbios_packet(struct nmb_name *nmbname,
196 bool bcast, bool rec_des,
197 struct in_addr to_ip)
199 struct packet_struct *packet = NULL;
200 struct nmb_packet *nmb = NULL;
202 /* Allocate the packet_struct we will return. */
203 if((packet = SMB_MALLOC_P(struct packet_struct)) == NULL) {
204 DEBUG(0,("create_and_init_netbios_packet: malloc fail (1) for packet struct.\n"));
205 return NULL;
208 memset((char *)packet,'\0',sizeof(*packet));
210 nmb = &packet->packet.nmb;
212 nmb->header.name_trn_id = generate_name_trn_id();
213 nmb->header.response = False;
214 nmb->header.nm_flags.recursion_desired = rec_des;
215 nmb->header.nm_flags.recursion_available = False;
216 nmb->header.nm_flags.trunc = False;
217 nmb->header.nm_flags.authoritative = False;
218 nmb->header.nm_flags.bcast = bcast;
220 nmb->header.rcode = 0;
221 nmb->header.qdcount = 1;
222 nmb->header.ancount = 0;
223 nmb->header.nscount = 0;
225 nmb->question.question_name = *nmbname;
226 nmb->question.question_type = QUESTION_TYPE_NB_QUERY;
227 nmb->question.question_class = QUESTION_CLASS_IN;
229 packet->ip = to_ip;
230 packet->port = NMB_PORT;
231 packet->recv_fd = -1;
232 packet->send_fd = ClientNMB;
233 packet->timestamp = time(NULL);
234 packet->packet_type = NMB_PACKET;
235 packet->locked = False;
237 return packet; /* Caller must free. */
240 /***************************************************************************
241 Sets up the common elements of register, refresh or release packet.
242 **************************************************************************/
244 static bool create_and_init_additional_record(struct packet_struct *packet,
245 uint16 nb_flags,
246 const struct in_addr *register_ip)
248 struct nmb_packet *nmb = &packet->packet.nmb;
250 if((nmb->additional = SMB_MALLOC_P(struct res_rec)) == NULL) {
251 DEBUG(0,("create_and_init_additional_record: malloc fail for additional record.\n"));
252 return False;
255 memset((char *)nmb->additional,'\0',sizeof(struct res_rec));
257 nmb->additional->rr_name = nmb->question.question_name;
258 nmb->additional->rr_type = RR_TYPE_NB;
259 nmb->additional->rr_class = RR_CLASS_IN;
261 /* See RFC 1002, sections 5.1.1.1, 5.1.1.2 and 5.1.1.3 */
262 if (nmb->header.nm_flags.bcast)
263 nmb->additional->ttl = PERMANENT_TTL;
264 else
265 nmb->additional->ttl = lp_max_ttl();
267 nmb->additional->rdlength = 6;
269 set_nb_flags(nmb->additional->rdata,nb_flags);
271 /* Set the address for the name we are registering. */
272 putip(&nmb->additional->rdata[2], register_ip);
275 it turns out that Jeremys code was correct, we are supposed
276 to send registrations from the IP we are registering. The
277 trick is what to do on timeouts! When we send on a
278 non-routable IP then the reply will timeout, and we should
279 treat this as success, not failure. That means we go into
280 our standard refresh cycle for that name which copes nicely
281 with disconnected networks.
283 packet->recv_fd = -1;
284 packet->send_fd = find_subnet_fd_for_address(*register_ip);
286 return True;
289 /***************************************************************************
290 Sends out a name query.
291 **************************************************************************/
293 static bool initiate_name_query_packet( struct packet_struct *packet)
295 struct nmb_packet *nmb = NULL;
297 nmb = &packet->packet.nmb;
299 nmb->header.opcode = NMB_NAME_QUERY_OPCODE;
300 nmb->header.arcount = 0;
302 nmb->header.nm_flags.recursion_desired = True;
304 DEBUG(4,("initiate_name_query_packet: sending query for name %s (bcast=%s) to IP %s\n",
305 nmb_namestr(&nmb->question.question_name),
306 BOOLSTR(nmb->header.nm_flags.bcast), inet_ntoa(packet->ip)));
308 return send_netbios_packet( packet );
311 /***************************************************************************
312 Sends out a name query - from a WINS server.
313 **************************************************************************/
315 static bool initiate_name_query_packet_from_wins_server( struct packet_struct *packet)
317 struct nmb_packet *nmb = NULL;
319 nmb = &packet->packet.nmb;
321 nmb->header.opcode = NMB_NAME_QUERY_OPCODE;
322 nmb->header.arcount = 0;
324 nmb->header.nm_flags.recursion_desired = False;
326 DEBUG(4,("initiate_name_query_packet_from_wins_server: sending query for name %s (bcast=%s) to IP %s\n",
327 nmb_namestr(&nmb->question.question_name),
328 BOOLSTR(nmb->header.nm_flags.bcast), inet_ntoa(packet->ip)));
330 return send_netbios_packet( packet );
333 /***************************************************************************
334 Sends out a name register.
335 **************************************************************************/
337 static bool initiate_name_register_packet( struct packet_struct *packet,
338 uint16 nb_flags, const struct in_addr *register_ip)
340 struct nmb_packet *nmb = &packet->packet.nmb;
342 nmb->header.opcode = NMB_NAME_REG_OPCODE;
343 nmb->header.arcount = 1;
345 nmb->header.nm_flags.recursion_desired = True;
347 if(create_and_init_additional_record(packet, nb_flags, register_ip) == False)
348 return False;
350 DEBUG(4,("initiate_name_register_packet: sending registration for name %s (bcast=%s) to IP %s\n",
351 nmb_namestr(&nmb->additional->rr_name),
352 BOOLSTR(nmb->header.nm_flags.bcast), inet_ntoa(packet->ip)));
354 return send_netbios_packet( packet );
357 /***************************************************************************
358 Sends out a multihomed name register.
359 **************************************************************************/
361 static bool initiate_multihomed_name_register_packet(struct packet_struct *packet,
362 uint16 nb_flags, struct in_addr *register_ip)
364 struct nmb_packet *nmb = &packet->packet.nmb;
365 fstring second_ip_buf;
367 fstrcpy(second_ip_buf, inet_ntoa(packet->ip));
369 nmb->header.opcode = NMB_NAME_MULTIHOMED_REG_OPCODE;
370 nmb->header.arcount = 1;
372 nmb->header.nm_flags.recursion_desired = True;
374 if(create_and_init_additional_record(packet, nb_flags, register_ip) == False)
375 return False;
377 DEBUG(4,("initiate_multihomed_name_register_packet: sending registration \
378 for name %s IP %s (bcast=%s) to IP %s\n",
379 nmb_namestr(&nmb->additional->rr_name), inet_ntoa(*register_ip),
380 BOOLSTR(nmb->header.nm_flags.bcast), second_ip_buf ));
382 return send_netbios_packet( packet );
385 /***************************************************************************
386 Sends out a name refresh.
387 **************************************************************************/
389 static bool initiate_name_refresh_packet( struct packet_struct *packet,
390 uint16 nb_flags, struct in_addr *refresh_ip)
392 struct nmb_packet *nmb = &packet->packet.nmb;
394 nmb->header.opcode = NMB_NAME_REFRESH_OPCODE_8;
395 nmb->header.arcount = 1;
397 nmb->header.nm_flags.recursion_desired = False;
399 if(create_and_init_additional_record(packet, nb_flags, refresh_ip) == False)
400 return False;
402 DEBUG(4,("initiate_name_refresh_packet: sending refresh for name %s (bcast=%s) to IP %s\n",
403 nmb_namestr(&nmb->additional->rr_name),
404 BOOLSTR(nmb->header.nm_flags.bcast), inet_ntoa(packet->ip)));
406 return send_netbios_packet( packet );
409 /***************************************************************************
410 Sends out a name release.
411 **************************************************************************/
413 static bool initiate_name_release_packet( struct packet_struct *packet,
414 uint16 nb_flags, struct in_addr *release_ip)
416 struct nmb_packet *nmb = &packet->packet.nmb;
418 nmb->header.opcode = NMB_NAME_RELEASE_OPCODE;
419 nmb->header.arcount = 1;
421 nmb->header.nm_flags.recursion_desired = False;
423 if(create_and_init_additional_record(packet, nb_flags, release_ip) == False)
424 return False;
426 DEBUG(4,("initiate_name_release_packet: sending release for name %s (bcast=%s) to IP %s\n",
427 nmb_namestr(&nmb->additional->rr_name),
428 BOOLSTR(nmb->header.nm_flags.bcast), inet_ntoa(packet->ip)));
430 return send_netbios_packet( packet );
433 /***************************************************************************
434 Sends out a node status.
435 **************************************************************************/
437 static bool initiate_node_status_packet( struct packet_struct *packet )
439 struct nmb_packet *nmb = &packet->packet.nmb;
441 nmb->header.opcode = NMB_NAME_QUERY_OPCODE;
442 nmb->header.arcount = 0;
444 nmb->header.nm_flags.recursion_desired = False;
446 nmb->question.question_type = QUESTION_TYPE_NB_STATUS;
448 DEBUG(4,("initiate_node_status_packet: sending node status request for name %s to IP %s\n",
449 nmb_namestr(&nmb->question.question_name),
450 inet_ntoa(packet->ip)));
452 return send_netbios_packet( packet );
455 /****************************************************************************
456 Simplification functions for queuing standard packets.
457 These should be the only publicly callable functions for sending
458 out packets.
459 ****************************************************************************/
461 /****************************************************************************
462 Assertion - we should never be sending nmbd packets on the remote
463 broadcast subnet.
464 ****************************************************************************/
466 static bool assert_check_subnet(struct subnet_record *subrec)
468 if( subrec == remote_broadcast_subnet) {
469 DEBUG(0,("assert_check_subnet: Attempt to send packet on remote broadcast subnet. \
470 This is a bug.\n"));
471 return True;
473 return False;
476 /****************************************************************************
477 Queue a register name packet to the broadcast address of a subnet.
478 ****************************************************************************/
480 struct response_record *queue_register_name( struct subnet_record *subrec,
481 response_function resp_fn,
482 timeout_response_function timeout_fn,
483 register_name_success_function success_fn,
484 register_name_fail_function fail_fn,
485 struct userdata_struct *userdata,
486 struct nmb_name *nmbname,
487 uint16 nb_flags)
489 struct packet_struct *p;
490 struct response_record *rrec;
491 struct sockaddr_storage ss;
492 const struct sockaddr_storage *pss = NULL;
493 if(assert_check_subnet(subrec))
494 return NULL;
496 /* note that all name registration requests have RD set (rfc1002 - section 4.2.2 */
497 if ((p = create_and_init_netbios_packet(nmbname, (subrec != unicast_subnet), True,
498 subrec->bcast_ip)) == NULL)
499 return NULL;
501 in_addr_to_sockaddr_storage(&ss, subrec->bcast_ip);
502 pss = iface_ip((struct sockaddr *)(void *)&ss);
503 if (!pss || pss->ss_family != AF_INET) {
504 p->locked = False;
505 free_packet(p);
506 return NULL;
509 if(initiate_name_register_packet(p, nb_flags,
510 &((const struct sockaddr_in *)pss)->sin_addr) == False) {
511 p->locked = False;
512 free_packet(p);
513 return NULL;
516 if((rrec = make_response_record(subrec, /* subnet record. */
517 p, /* packet we sent. */
518 resp_fn, /* function to call on response. */
519 timeout_fn, /* function to call on timeout. */
520 (success_function)success_fn, /* function to call on operation success. */
521 (fail_function)fail_fn, /* function to call on operation fail. */
522 userdata)) == NULL) {
523 p->locked = False;
524 free_packet(p);
525 return NULL;
528 return rrec;
531 /****************************************************************************
532 Queue a refresh name packet to the broadcast address of a subnet.
533 ****************************************************************************/
535 void queue_wins_refresh(struct nmb_name *nmbname,
536 response_function resp_fn,
537 timeout_response_function timeout_fn,
538 uint16 nb_flags,
539 struct in_addr refresh_ip,
540 const char *tag)
542 struct packet_struct *p;
543 struct response_record *rrec;
544 struct in_addr wins_ip;
545 struct userdata_struct *userdata;
546 fstring ip_str;
548 wins_ip = wins_srv_ip_tag(tag, refresh_ip);
550 if ((p = create_and_init_netbios_packet(nmbname, False, False, wins_ip)) == NULL) {
551 return;
554 if (!initiate_name_refresh_packet(p, nb_flags, &refresh_ip)) {
555 p->locked = False;
556 free_packet(p);
557 return;
560 fstrcpy(ip_str, inet_ntoa(refresh_ip));
562 DEBUG(6,("Refreshing name %s IP %s with WINS server %s using tag '%s'\n",
563 nmb_namestr(nmbname), ip_str, inet_ntoa(wins_ip), tag));
565 userdata = (struct userdata_struct *)SMB_MALLOC(sizeof(*userdata) + strlen(tag) + 1);
566 if (!userdata) {
567 p->locked = False;
568 free_packet(p);
569 DEBUG(0,("Failed to allocate userdata structure!\n"));
570 return;
572 ZERO_STRUCTP(userdata);
573 userdata->userdata_len = strlen(tag) + 1;
574 strlcpy(userdata->data, tag, userdata->userdata_len);
576 if ((rrec = make_response_record(unicast_subnet,
578 resp_fn, timeout_fn,
579 NULL,
580 NULL,
581 userdata)) == NULL) {
582 p->locked = False;
583 free_packet(p);
584 return;
587 free(userdata);
589 /* we don't want to repeat refresh packets */
590 rrec->repeat_count = 0;
594 /****************************************************************************
595 Queue a multihomed register name packet to a given WINS server IP
596 ****************************************************************************/
598 struct response_record *queue_register_multihomed_name( struct subnet_record *subrec,
599 response_function resp_fn,
600 timeout_response_function timeout_fn,
601 register_name_success_function success_fn,
602 register_name_fail_function fail_fn,
603 struct userdata_struct *userdata,
604 struct nmb_name *nmbname,
605 uint16 nb_flags,
606 struct in_addr register_ip,
607 struct in_addr wins_ip)
609 struct packet_struct *p;
610 struct response_record *rrec;
611 bool ret;
613 /* Sanity check. */
614 if(subrec != unicast_subnet) {
615 DEBUG(0,("queue_register_multihomed_name: should only be done on \
616 unicast subnet. subnet is %s\n.", subrec->subnet_name ));
617 return NULL;
620 if(assert_check_subnet(subrec))
621 return NULL;
623 if ((p = create_and_init_netbios_packet(nmbname, False, True, wins_ip)) == NULL)
624 return NULL;
626 if (nb_flags & NB_GROUP)
627 ret = initiate_name_register_packet( p, nb_flags, &register_ip);
628 else
629 ret = initiate_multihomed_name_register_packet(p, nb_flags, &register_ip);
631 if (ret == False) {
632 p->locked = False;
633 free_packet(p);
634 return NULL;
637 if ((rrec = make_response_record(subrec, /* subnet record. */
638 p, /* packet we sent. */
639 resp_fn, /* function to call on response. */
640 timeout_fn, /* function to call on timeout. */
641 (success_function)success_fn, /* function to call on operation success. */
642 (fail_function)fail_fn, /* function to call on operation fail. */
643 userdata)) == NULL) {
644 p->locked = False;
645 free_packet(p);
646 return NULL;
649 return rrec;
652 /****************************************************************************
653 Queue a release name packet to the broadcast address of a subnet.
654 ****************************************************************************/
656 struct response_record *queue_release_name( struct subnet_record *subrec,
657 response_function resp_fn,
658 timeout_response_function timeout_fn,
659 release_name_success_function success_fn,
660 release_name_fail_function fail_fn,
661 struct userdata_struct *userdata,
662 struct nmb_name *nmbname,
663 uint16 nb_flags,
664 struct in_addr release_ip,
665 struct in_addr dest_ip)
667 struct packet_struct *p;
668 struct response_record *rrec;
670 if(assert_check_subnet(subrec))
671 return NULL;
673 if ((p = create_and_init_netbios_packet(nmbname, (subrec != unicast_subnet), False, dest_ip)) == NULL)
674 return NULL;
676 if(initiate_name_release_packet( p, nb_flags, &release_ip) == False) {
677 p->locked = False;
678 free_packet(p);
679 return NULL;
682 if((rrec = make_response_record(subrec, /* subnet record. */
683 p, /* packet we sent. */
684 resp_fn, /* function to call on response. */
685 timeout_fn, /* function to call on timeout. */
686 (success_function)success_fn, /* function to call on operation success. */
687 (fail_function)fail_fn, /* function to call on operation fail. */
688 userdata)) == NULL) {
689 p->locked = False;
690 free_packet(p);
691 return NULL;
695 * For a broadcast release packet, only send once.
696 * This will cause us to remove the name asap. JRA.
699 if (subrec != unicast_subnet) {
700 rrec->repeat_count = 0;
701 rrec->repeat_time = 0;
704 return rrec;
707 /****************************************************************************
708 Queue a query name packet to the broadcast address of a subnet.
709 ****************************************************************************/
711 struct response_record *queue_query_name( struct subnet_record *subrec,
712 response_function resp_fn,
713 timeout_response_function timeout_fn,
714 query_name_success_function success_fn,
715 query_name_fail_function fail_fn,
716 struct userdata_struct *userdata,
717 struct nmb_name *nmbname)
719 struct packet_struct *p;
720 struct response_record *rrec;
721 struct in_addr to_ip;
723 if(assert_check_subnet(subrec))
724 return NULL;
726 to_ip = subrec->bcast_ip;
728 /* queries to the WINS server turn up here as queries to IP 0.0.0.0
729 These need to be handled a bit differently */
730 if (subrec->type == UNICAST_SUBNET && is_zero_ip_v4(to_ip)) {
731 /* What we really need to do is loop over each of our wins
732 * servers and wins server tags here, but that just doesn't
733 * fit our architecture at the moment (userdata may already
734 * be used when we get here). For now we just query the first
735 * active wins server on the first tag.
737 char **tags = wins_srv_tags();
738 if (!tags) {
739 return NULL;
741 to_ip = wins_srv_ip_tag(tags[0], to_ip);
742 wins_srv_tags_free(tags);
745 if(( p = create_and_init_netbios_packet(nmbname,
746 (subrec != unicast_subnet),
747 (subrec == unicast_subnet),
748 to_ip)) == NULL)
749 return NULL;
751 if(lp_bind_interfaces_only()) {
752 int i;
754 DEBUG(10,("queue_query_name: bind_interfaces_only is set, looking for suitable source IP\n"));
755 for(i = 0; i < iface_count(); i++) {
756 const struct in_addr *ifip = iface_n_ip_v4(i);
758 if (ifip == NULL) {
759 DEBUG(0,("queue_query_name: interface %d has NULL IP address !\n", i));
760 continue;
763 if (is_loopback_ip_v4(*ifip)) {
764 DEBUG(5,("queue_query_name: ignoring loopback interface (%d)\n", i));
765 continue;
768 DEBUG(10,("queue_query_name: using source IP %s\n",inet_ntoa(*ifip)));
769 p->send_fd = find_subnet_fd_for_address( *ifip );
770 break;
774 if(initiate_name_query_packet( p ) == False) {
775 p->locked = False;
776 free_packet(p);
777 return NULL;
780 if((rrec = make_response_record(subrec, /* subnet record. */
781 p, /* packet we sent. */
782 resp_fn, /* function to call on response. */
783 timeout_fn, /* function to call on timeout. */
784 (success_function)success_fn, /* function to call on operation success. */
785 (fail_function)fail_fn, /* function to call on operation fail. */
786 userdata)) == NULL) {
787 p->locked = False;
788 free_packet(p);
789 return NULL;
792 return rrec;
795 /****************************************************************************
796 Queue a query name packet to a given address from the WINS subnet.
797 ****************************************************************************/
799 struct response_record *queue_query_name_from_wins_server( struct in_addr to_ip,
800 response_function resp_fn,
801 timeout_response_function timeout_fn,
802 query_name_success_function success_fn,
803 query_name_fail_function fail_fn,
804 struct userdata_struct *userdata,
805 struct nmb_name *nmbname)
807 struct packet_struct *p;
808 struct response_record *rrec;
810 if ((p = create_and_init_netbios_packet(nmbname, False, False, to_ip)) == NULL)
811 return NULL;
813 if(initiate_name_query_packet_from_wins_server( p ) == False) {
814 p->locked = False;
815 free_packet(p);
816 return NULL;
819 if((rrec = make_response_record(wins_server_subnet, /* subnet record. */
820 p, /* packet we sent. */
821 resp_fn, /* function to call on response. */
822 timeout_fn, /* function to call on timeout. */
823 (success_function)success_fn, /* function to call on operation success. */
824 (fail_function)fail_fn, /* function to call on operation fail. */
825 userdata)) == NULL) {
826 p->locked = False;
827 free_packet(p);
828 return NULL;
831 return rrec;
834 /****************************************************************************
835 Queue a node status packet to a given name and address.
836 ****************************************************************************/
838 struct response_record *queue_node_status( struct subnet_record *subrec,
839 response_function resp_fn,
840 timeout_response_function timeout_fn,
841 node_status_success_function success_fn,
842 node_status_fail_function fail_fn,
843 struct userdata_struct *userdata,
844 struct nmb_name *nmbname,
845 struct in_addr send_ip)
847 struct packet_struct *p;
848 struct response_record *rrec;
850 /* Sanity check. */
851 if(subrec != unicast_subnet) {
852 DEBUG(0,("queue_register_multihomed_name: should only be done on \
853 unicast subnet. subnet is %s\n.", subrec->subnet_name ));
854 return NULL;
857 if(assert_check_subnet(subrec))
858 return NULL;
860 if(( p = create_and_init_netbios_packet(nmbname, False, False, send_ip)) == NULL)
861 return NULL;
863 if(initiate_node_status_packet(p) == False) {
864 p->locked = False;
865 free_packet(p);
866 return NULL;
869 if((rrec = make_response_record(subrec, /* subnet record. */
870 p, /* packet we sent. */
871 resp_fn, /* function to call on response. */
872 timeout_fn, /* function to call on timeout. */
873 (success_function)success_fn, /* function to call on operation success. */
874 (fail_function)fail_fn, /* function to call on operation fail. */
875 userdata)) == NULL) {
876 p->locked = False;
877 free_packet(p);
878 return NULL;
881 return rrec;
884 /****************************************************************************
885 Reply to a netbios name packet. see rfc1002.txt
886 ****************************************************************************/
888 void reply_netbios_packet(struct packet_struct *orig_packet,
889 int rcode, enum netbios_reply_type_code rcv_code, int opcode,
890 int ttl, char *data,int len)
892 struct packet_struct packet;
893 struct nmb_packet *nmb = NULL;
894 struct res_rec answers;
895 struct nmb_packet *orig_nmb = &orig_packet->packet.nmb;
896 bool loopback_this_packet = False;
897 int rr_type = RR_TYPE_NB;
898 const char *packet_type = "unknown";
900 /* Check if we are sending to or from ourselves. */
901 if(ismyip_v4(orig_packet->ip) && (orig_packet->port == global_nmb_port))
902 loopback_this_packet = True;
904 nmb = &packet.packet.nmb;
906 /* Do a partial copy of the packet. We clear the locked flag and
907 the resource record pointers. */
908 packet = *orig_packet; /* Full structure copy. */
909 packet.locked = False;
910 nmb->answers = NULL;
911 nmb->nsrecs = NULL;
912 nmb->additional = NULL;
914 switch (rcv_code) {
915 case NMB_STATUS:
916 packet_type = "nmb_status";
917 nmb->header.nm_flags.recursion_desired = False;
918 nmb->header.nm_flags.recursion_available = False;
919 rr_type = RR_TYPE_NBSTAT;
920 break;
921 case NMB_QUERY:
922 packet_type = "nmb_query";
923 nmb->header.nm_flags.recursion_desired = True;
924 nmb->header.nm_flags.recursion_available = True;
925 if (rcode) {
926 rr_type = RR_TYPE_NULL;
928 break;
929 case NMB_REG:
930 case NMB_REG_REFRESH:
931 packet_type = "nmb_reg";
932 nmb->header.nm_flags.recursion_desired = True;
933 nmb->header.nm_flags.recursion_available = True;
934 break;
935 case NMB_REL:
936 packet_type = "nmb_rel";
937 nmb->header.nm_flags.recursion_desired = False;
938 nmb->header.nm_flags.recursion_available = False;
939 break;
940 case NMB_WAIT_ACK:
941 packet_type = "nmb_wack";
942 nmb->header.nm_flags.recursion_desired = False;
943 nmb->header.nm_flags.recursion_available = False;
944 rr_type = RR_TYPE_NULL;
945 break;
946 case WINS_REG:
947 packet_type = "wins_reg";
948 nmb->header.nm_flags.recursion_desired = True;
949 nmb->header.nm_flags.recursion_available = True;
950 break;
951 case WINS_QUERY:
952 packet_type = "wins_query";
953 nmb->header.nm_flags.recursion_desired = True;
954 nmb->header.nm_flags.recursion_available = True;
955 if (rcode) {
956 rr_type = RR_TYPE_NULL;
958 break;
959 default:
960 DEBUG(0,("reply_netbios_packet: Unknown packet type: %s %s to ip %s\n",
961 packet_type, nmb_namestr(&orig_nmb->question.question_name),
962 inet_ntoa(packet.ip)));
963 return;
966 DEBUG(4,("reply_netbios_packet: sending a reply of packet type: %s %s to ip %s \
967 for id %hu\n", packet_type, nmb_namestr(&orig_nmb->question.question_name),
968 inet_ntoa(packet.ip), orig_nmb->header.name_trn_id));
970 nmb->header.name_trn_id = orig_nmb->header.name_trn_id;
971 nmb->header.opcode = opcode;
972 nmb->header.response = True;
973 nmb->header.nm_flags.bcast = False;
974 nmb->header.nm_flags.trunc = False;
975 nmb->header.nm_flags.authoritative = True;
977 nmb->header.rcode = rcode;
978 nmb->header.qdcount = 0;
979 nmb->header.ancount = 1;
980 nmb->header.nscount = 0;
981 nmb->header.arcount = 0;
983 memset((char*)&nmb->question,'\0',sizeof(nmb->question));
985 nmb->answers = &answers;
986 memset((char*)nmb->answers,'\0',sizeof(*nmb->answers));
988 nmb->answers->rr_name = orig_nmb->question.question_name;
989 nmb->answers->rr_type = rr_type;
990 nmb->answers->rr_class = RR_CLASS_IN;
991 nmb->answers->ttl = ttl;
993 if (data && len) {
994 if (len < 0 || len > sizeof(nmb->answers->rdata)) {
995 DEBUG(5,("reply_netbios_packet: "
996 "invalid packet len (%d)\n",
997 len ));
998 return;
1000 nmb->answers->rdlength = len;
1001 memcpy(nmb->answers->rdata, data, len);
1004 packet.packet_type = NMB_PACKET;
1005 packet.recv_fd = -1;
1006 /* Ensure we send out on the same fd that the original
1007 packet came in on to give the correct source IP address. */
1008 if (orig_packet->send_fd != -1) {
1009 packet.send_fd = orig_packet->send_fd;
1010 } else {
1011 packet.send_fd = orig_packet->recv_fd;
1013 packet.timestamp = time(NULL);
1015 debug_nmb_packet(&packet);
1017 if(loopback_this_packet) {
1018 struct packet_struct *lo_packet;
1019 DEBUG(5,("reply_netbios_packet: sending packet to ourselves.\n"));
1020 if((lo_packet = copy_packet(&packet)) == NULL)
1021 return;
1022 queue_packet(lo_packet);
1023 } else if (!send_packet(&packet)) {
1024 DEBUG(0,("reply_netbios_packet: send_packet to IP %s port %d failed\n",
1025 inet_ntoa(packet.ip),packet.port));
1029 /*******************************************************************
1030 Queue a packet into a packet queue
1031 ******************************************************************/
1033 void queue_packet(struct packet_struct *packet)
1035 DLIST_ADD_END(packet_queue, packet, struct packet_struct *);
1038 /****************************************************************************
1039 Try and find a matching subnet record for a datagram port 138 packet.
1040 ****************************************************************************/
1042 static struct subnet_record *find_subnet_for_dgram_browse_packet(struct packet_struct *p)
1044 struct subnet_record *subrec;
1046 /* Go through all the broadcast subnets and see if the mask matches. */
1047 for (subrec = FIRST_SUBNET; subrec ; subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec)) {
1048 if(same_net_v4(p->ip, subrec->bcast_ip, subrec->mask_ip))
1049 return subrec;
1052 /* If the subnet record is the remote announce broadcast subnet,
1053 hack it here to be the first subnet. This is really gross and
1054 is needed due to people turning on port 137/138 broadcast
1055 forwarding on their routers. May fire and brimstone rain
1056 down upon them...
1059 return FIRST_SUBNET;
1062 /****************************************************************************
1063 Dispatch a browse frame from port 138 to the correct processing function.
1064 ****************************************************************************/
1066 static void process_browse_packet(struct packet_struct *p, const char *buf,int len)
1068 struct dgram_packet *dgram = &p->packet.dgram;
1069 int command = CVAL(buf,0);
1070 struct subnet_record *subrec = find_subnet_for_dgram_browse_packet(p);
1071 char scope[64];
1072 unstring src_name;
1074 /* Drop the packet if it's a different NetBIOS scope, or the source is from one of our names. */
1075 pull_ascii(scope, dgram->dest_name.scope, 64, 64, STR_TERMINATE);
1076 if (!strequal(scope, lp_netbios_scope())) {
1077 DEBUG(7,("process_browse_packet: Discarding datagram from IP %s. Scope (%s) \
1078 mismatch with our scope (%s).\n", inet_ntoa(p->ip), scope, lp_netbios_scope()));
1079 return;
1082 pull_ascii_nstring(src_name, sizeof(src_name), dgram->source_name.name);
1083 if (is_myname(src_name)) {
1084 DEBUG(7,("process_browse_packet: Discarding datagram from IP %s. Source name \
1085 %s is one of our names !\n", inet_ntoa(p->ip), nmb_namestr(&dgram->source_name)));
1086 return;
1089 switch (command) {
1090 case ANN_HostAnnouncement:
1091 debug_browse_data(buf, len);
1092 process_host_announce(subrec, p, buf+1);
1093 break;
1094 case ANN_DomainAnnouncement:
1095 debug_browse_data(buf, len);
1096 process_workgroup_announce(subrec, p, buf+1);
1097 break;
1098 case ANN_LocalMasterAnnouncement:
1099 debug_browse_data(buf, len);
1100 process_local_master_announce(subrec, p, buf+1);
1101 break;
1102 case ANN_AnnouncementRequest:
1103 debug_browse_data(buf, len);
1104 process_announce_request(subrec, p, buf+1);
1105 break;
1106 case ANN_Election:
1107 debug_browse_data(buf, len);
1108 process_election(subrec, p, buf+1);
1109 break;
1110 case ANN_GetBackupListReq:
1111 debug_browse_data(buf, len);
1112 process_get_backup_list_request(subrec, p, buf+1);
1113 break;
1114 case ANN_GetBackupListResp:
1115 debug_browse_data(buf, len);
1116 /* We never send ANN_GetBackupListReq so we should never get these. */
1117 DEBUG(0,("process_browse_packet: Discarding GetBackupListResponse \
1118 packet from %s IP %s\n", nmb_namestr(&dgram->source_name), inet_ntoa(p->ip)));
1119 break;
1120 case ANN_ResetBrowserState:
1121 debug_browse_data(buf, len);
1122 process_reset_browser(subrec, p, buf+1);
1123 break;
1124 case ANN_MasterAnnouncement:
1125 /* Master browser datagrams must be processed on the unicast subnet. */
1126 subrec = unicast_subnet;
1128 debug_browse_data(buf, len);
1129 process_master_browser_announce(subrec, p, buf+1);
1130 break;
1131 case ANN_BecomeBackup:
1133 * We don't currently implement this. Log it just in case.
1135 debug_browse_data(buf, len);
1136 DEBUG(10,("process_browse_packet: On subnet %s ignoring browse packet \
1137 command ANN_BecomeBackup from %s IP %s to %s\n", subrec->subnet_name, nmb_namestr(&dgram->source_name),
1138 inet_ntoa(p->ip), nmb_namestr(&dgram->dest_name)));
1139 break;
1140 default:
1141 debug_browse_data(buf, len);
1142 DEBUG(0,("process_browse_packet: On subnet %s ignoring browse packet \
1143 command code %d from %s IP %s to %s\n", subrec->subnet_name, command, nmb_namestr(&dgram->source_name),
1144 inet_ntoa(p->ip), nmb_namestr(&dgram->dest_name)));
1145 break;
1149 /****************************************************************************
1150 Dispatch a LanMan browse frame from port 138 to the correct processing function.
1151 ****************************************************************************/
1153 static void process_lanman_packet(struct packet_struct *p, const char *buf,int len)
1155 struct dgram_packet *dgram = &p->packet.dgram;
1156 int command = SVAL(buf,0);
1157 struct subnet_record *subrec = find_subnet_for_dgram_browse_packet(p);
1158 char scope[64];
1159 unstring src_name;
1161 /* Drop the packet if it's a different NetBIOS scope, or the source is from one of our names. */
1163 pull_ascii(scope, dgram->dest_name.scope, 64, 64, STR_TERMINATE);
1164 if (!strequal(scope, lp_netbios_scope())) {
1165 DEBUG(7,("process_lanman_packet: Discarding datagram from IP %s. Scope (%s) \
1166 mismatch with our scope (%s).\n", inet_ntoa(p->ip), scope, lp_netbios_scope()));
1167 return;
1170 pull_ascii_nstring(src_name, sizeof(src_name), dgram->source_name.name);
1171 if (is_myname(src_name)) {
1172 DEBUG(0,("process_lanman_packet: Discarding datagram from IP %s. Source name \
1173 %s is one of our names !\n", inet_ntoa(p->ip), nmb_namestr(&dgram->source_name)));
1174 return;
1177 switch (command) {
1178 case ANN_HostAnnouncement:
1179 debug_browse_data(buf, len);
1180 process_lm_host_announce(subrec, p, buf+1, len > 1 ? len-1 : 0);
1181 break;
1182 case ANN_AnnouncementRequest:
1183 process_lm_announce_request(subrec, p, buf+1, len > 1 ? len-1 : 0);
1184 break;
1185 default:
1186 DEBUG(0,("process_lanman_packet: On subnet %s ignoring browse packet \
1187 command code %d from %s IP %s to %s\n", subrec->subnet_name, command, nmb_namestr(&dgram->source_name),
1188 inet_ntoa(p->ip), nmb_namestr(&dgram->dest_name)));
1189 break;
1193 /****************************************************************************
1194 Determine if a packet is for us on port 138. Note that to have any chance of
1195 being efficient we need to drop as many packets as possible at this
1196 stage as subsequent processing is expensive.
1197 ****************************************************************************/
1199 static bool listening(struct packet_struct *p,struct nmb_name *nbname)
1201 struct subnet_record *subrec = NULL;
1203 for (subrec = FIRST_SUBNET; subrec ; subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec)) {
1204 if(same_net_v4(p->ip, subrec->bcast_ip, subrec->mask_ip))
1205 break;
1208 if(subrec == NULL)
1209 subrec = unicast_subnet;
1211 return (find_name_on_subnet(subrec, nbname, FIND_SELF_NAME) != NULL);
1214 /****************************************************************************
1215 Process udp 138 datagrams
1216 ****************************************************************************/
1218 static void process_dgram(struct packet_struct *p)
1220 const char *buf;
1221 const char *buf2;
1222 int len;
1223 struct dgram_packet *dgram = &p->packet.dgram;
1225 /* If we aren't listening to the destination name then ignore the packet */
1226 if (!listening(p,&dgram->dest_name)) {
1227 nb_packet_dispatch(packet_server, p);
1228 DEBUG(5,("process_dgram: ignoring dgram packet sent to name %s from %s\n",
1229 nmb_namestr(&dgram->dest_name), inet_ntoa(p->ip)));
1230 return;
1233 if (dgram->header.msg_type != 0x10 && dgram->header.msg_type != 0x11 && dgram->header.msg_type != 0x12) {
1234 nb_packet_dispatch(packet_server, p);
1235 /* Don't process error packets etc yet */
1236 DEBUG(5,("process_dgram: ignoring dgram packet sent to name %s from IP %s as it is \
1237 an error packet of type %x\n", nmb_namestr(&dgram->dest_name), inet_ntoa(p->ip), dgram->header.msg_type));
1238 return;
1241 /* Ensure we have a large enough packet before looking inside. */
1242 if (dgram->datasize < (smb_vwv12 - 2)) {
1243 /* That's the offset minus the 4 byte length + 2 bytes of offset. */
1244 DEBUG(0,("process_dgram: ignoring too short dgram packet (%u) sent to name %s from IP %s\n",
1245 (unsigned int)dgram->datasize,
1246 nmb_namestr(&dgram->dest_name),
1247 inet_ntoa(p->ip) ));
1248 return;
1251 buf = &dgram->data[0];
1252 buf -= 4; /* XXXX for the pseudo tcp length - someday I need to get rid of this */
1254 if (CVAL(buf,smb_com) != SMBtrans)
1255 return;
1257 len = SVAL(buf,smb_vwv11);
1258 buf2 = smb_base(buf) + SVAL(buf,smb_vwv12);
1260 if (len <= 0 || len > dgram->datasize) {
1261 DEBUG(0,("process_dgram: ignoring malformed1 (datasize = %d, len = %d) datagram \
1262 packet sent to name %s from IP %s\n",
1263 dgram->datasize,
1264 len,
1265 nmb_namestr(&dgram->dest_name),
1266 inet_ntoa(p->ip) ));
1267 return;
1270 if (buf2 < dgram->data || (buf2 >= dgram->data + dgram->datasize)) {
1271 DEBUG(0,("process_dgram: ignoring malformed2 (datasize = %d, len=%d, off=%d) datagram \
1272 packet sent to name %s from IP %s\n",
1273 dgram->datasize,
1274 len,
1275 (int)PTR_DIFF(buf2, dgram->data),
1276 nmb_namestr(&dgram->dest_name),
1277 inet_ntoa(p->ip) ));
1278 return;
1281 if ((buf2 + len < dgram->data) || (buf2 + len > dgram->data + dgram->datasize)) {
1282 DEBUG(0,("process_dgram: ignoring malformed3 (datasize = %d, len=%d, off=%d) datagram \
1283 packet sent to name %s from IP %s\n",
1284 dgram->datasize,
1285 len,
1286 (int)PTR_DIFF(buf2, dgram->data),
1287 nmb_namestr(&dgram->dest_name),
1288 inet_ntoa(p->ip) ));
1289 return;
1292 DEBUG(4,("process_dgram: datagram from %s to %s IP %s for %s of type %d len=%d\n",
1293 nmb_namestr(&dgram->source_name),nmb_namestr(&dgram->dest_name),
1294 inet_ntoa(p->ip), smb_buf_const(buf),CVAL(buf2,0),len));
1296 /* Datagram packet received for the browser mailslot */
1297 if (strequal(smb_buf_const(buf),BROWSE_MAILSLOT)) {
1298 process_browse_packet(p,buf2,len);
1299 return;
1302 /* Datagram packet received for the LAN Manager mailslot */
1303 if (strequal(smb_buf_const(buf),LANMAN_MAILSLOT)) {
1304 process_lanman_packet(p,buf2,len);
1305 return;
1308 /* Datagram packet received for the domain logon mailslot */
1309 if (strequal(smb_buf_const(buf),NET_LOGON_MAILSLOT)) {
1310 process_logon_packet(p,buf2,len,NET_LOGON_MAILSLOT);
1311 return;
1314 /* Datagram packet received for the NT domain logon mailslot */
1315 if (strequal(smb_buf_const(buf),NT_LOGON_MAILSLOT)) {
1316 process_logon_packet(p,buf2,len,NT_LOGON_MAILSLOT);
1317 return;
1320 nb_packet_dispatch(packet_server, p);
1323 /****************************************************************************
1324 Validate a response nmb packet.
1325 ****************************************************************************/
1327 static bool validate_nmb_response_packet( struct nmb_packet *nmb )
1329 bool ignore = False;
1331 switch (nmb->header.opcode) {
1332 case NMB_NAME_REG_OPCODE:
1333 case NMB_NAME_REFRESH_OPCODE_8: /* ambiguity in rfc1002 about which is correct. */
1334 case NMB_NAME_REFRESH_OPCODE_9: /* WinNT uses 8 by default. */
1335 if (nmb->header.ancount == 0) {
1336 DEBUG(0,("validate_nmb_response_packet: Bad REG/REFRESH Packet. "));
1337 ignore = True;
1339 break;
1341 case NMB_NAME_QUERY_OPCODE:
1342 if ((nmb->header.ancount != 0) && (nmb->header.ancount != 1)) {
1343 DEBUG(0,("validate_nmb_response_packet: Bad QUERY Packet. "));
1344 ignore = True;
1346 break;
1348 case NMB_NAME_RELEASE_OPCODE:
1349 if (nmb->header.ancount == 0) {
1350 DEBUG(0,("validate_nmb_response_packet: Bad RELEASE Packet. "));
1351 ignore = True;
1353 break;
1355 case NMB_WACK_OPCODE:
1356 /* Check WACK response here. */
1357 if (nmb->header.ancount != 1) {
1358 DEBUG(0,("validate_nmb_response_packet: Bad WACK Packet. "));
1359 ignore = True;
1361 break;
1362 default:
1363 DEBUG(0,("validate_nmb_response_packet: Ignoring packet with unknown opcode %d.\n",
1364 nmb->header.opcode));
1365 return True;
1368 if(ignore)
1369 DEBUG(0,("Ignoring response packet with opcode %d.\n", nmb->header.opcode));
1371 return ignore;
1374 /****************************************************************************
1375 Validate a request nmb packet.
1376 ****************************************************************************/
1378 static bool validate_nmb_packet( struct nmb_packet *nmb )
1380 bool ignore = False;
1382 switch (nmb->header.opcode) {
1383 case NMB_NAME_REG_OPCODE:
1384 case NMB_NAME_REFRESH_OPCODE_8: /* ambiguity in rfc1002 about which is correct. */
1385 case NMB_NAME_REFRESH_OPCODE_9: /* WinNT uses 8 by default. */
1386 case NMB_NAME_MULTIHOMED_REG_OPCODE:
1387 if (nmb->header.qdcount==0 || nmb->header.arcount==0) {
1388 DEBUG(0,("validate_nmb_packet: Bad REG/REFRESH Packet. "));
1389 ignore = True;
1391 break;
1393 case NMB_NAME_QUERY_OPCODE:
1394 if ((nmb->header.qdcount == 0) || ((nmb->question.question_type != QUESTION_TYPE_NB_QUERY) &&
1395 (nmb->question.question_type != QUESTION_TYPE_NB_STATUS))) {
1396 DEBUG(0,("validate_nmb_packet: Bad QUERY Packet. "));
1397 ignore = True;
1399 break;
1401 case NMB_NAME_RELEASE_OPCODE:
1402 if (nmb->header.qdcount==0 || nmb->header.arcount==0) {
1403 DEBUG(0,("validate_nmb_packet: Bad RELEASE Packet. "));
1404 ignore = True;
1406 break;
1407 default:
1408 DEBUG(0,("validate_nmb_packet: Ignoring packet with unknown opcode %d.\n",
1409 nmb->header.opcode));
1410 return True;
1413 if(ignore)
1414 DEBUG(0,("validate_nmb_packet: Ignoring request packet with opcode %d.\n", nmb->header.opcode));
1416 return ignore;
1419 /****************************************************************************
1420 Find a subnet (and potentially a response record) for a packet.
1421 ****************************************************************************/
1423 static struct subnet_record *find_subnet_for_nmb_packet( struct packet_struct *p,
1424 struct response_record **pprrec)
1426 struct nmb_packet *nmb = &p->packet.nmb;
1427 struct response_record *rrec = NULL;
1428 struct subnet_record *subrec = NULL;
1430 if(pprrec != NULL)
1431 *pprrec = NULL;
1433 if(nmb->header.response) {
1434 /* It's a response packet. Find a record for it or it's an error. */
1436 rrec = find_response_record( &subrec, nmb->header.name_trn_id);
1437 if(rrec == NULL) {
1438 DEBUG(3,("find_subnet_for_nmb_packet: response record not found for response id %hu\n",
1439 nmb->header.name_trn_id));
1440 nb_packet_dispatch(packet_server, p);
1441 return NULL;
1444 if(subrec == NULL) {
1445 DEBUG(0,("find_subnet_for_nmb_packet: subnet record not found for response id %hu\n",
1446 nmb->header.name_trn_id));
1447 return NULL;
1450 if(pprrec != NULL)
1451 *pprrec = rrec;
1452 return subrec;
1455 /* Try and see what subnet this packet belongs to. */
1457 /* WINS server ? */
1458 if(packet_is_for_wins_server(p))
1459 return wins_server_subnet;
1461 /* If it wasn't a broadcast packet then send to the UNICAST subnet. */
1462 if(nmb->header.nm_flags.bcast == False)
1463 return unicast_subnet;
1465 /* Go through all the broadcast subnets and see if the mask matches. */
1466 for (subrec = FIRST_SUBNET; subrec ; subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec)) {
1467 if(same_net_v4(p->ip, subrec->bcast_ip, subrec->mask_ip))
1468 return subrec;
1471 /* If none match it must have been a directed broadcast - assign the remote_broadcast_subnet. */
1472 return remote_broadcast_subnet;
1475 /****************************************************************************
1476 Process a nmb request packet - validate the packet and route it.
1477 ****************************************************************************/
1479 static void process_nmb_request(struct packet_struct *p)
1481 struct nmb_packet *nmb = &p->packet.nmb;
1482 struct subnet_record *subrec = NULL;
1484 debug_nmb_packet(p);
1486 /* Ensure we have a good packet. */
1487 if(validate_nmb_packet(nmb))
1488 return;
1490 /* Allocate a subnet to this packet - if we cannot - fail. */
1491 if((subrec = find_subnet_for_nmb_packet(p, NULL))==NULL)
1492 return;
1494 switch (nmb->header.opcode) {
1495 case NMB_NAME_REG_OPCODE:
1496 if(subrec == wins_server_subnet)
1497 wins_process_name_registration_request(subrec, p);
1498 else
1499 process_name_registration_request(subrec, p);
1500 break;
1502 case NMB_NAME_REFRESH_OPCODE_8: /* ambiguity in rfc1002 about which is correct. */
1503 case NMB_NAME_REFRESH_OPCODE_9:
1504 if(subrec == wins_server_subnet)
1505 wins_process_name_refresh_request(subrec, p);
1506 else
1507 process_name_refresh_request(subrec, p);
1508 break;
1510 case NMB_NAME_MULTIHOMED_REG_OPCODE:
1511 if(subrec == wins_server_subnet) {
1512 wins_process_multihomed_name_registration_request(subrec, p);
1513 } else {
1514 DEBUG(0,("process_nmb_request: Multihomed registration request must be \
1515 directed at a WINS server.\n"));
1517 break;
1519 case NMB_NAME_QUERY_OPCODE:
1520 switch (nmb->question.question_type) {
1521 case QUESTION_TYPE_NB_QUERY:
1522 if(subrec == wins_server_subnet)
1523 wins_process_name_query_request(subrec, p);
1524 else
1525 process_name_query_request(subrec, p);
1526 break;
1527 case QUESTION_TYPE_NB_STATUS:
1528 if(subrec == wins_server_subnet) {
1529 DEBUG(0,("process_nmb_request: NB_STATUS request directed at WINS server is \
1530 not allowed.\n"));
1531 break;
1532 } else {
1533 process_node_status_request(subrec, p);
1535 break;
1537 break;
1539 case NMB_NAME_RELEASE_OPCODE:
1540 if(subrec == wins_server_subnet)
1541 wins_process_name_release_request(subrec, p);
1542 else
1543 process_name_release_request(subrec, p);
1544 break;
1548 /****************************************************************************
1549 Process a nmb response packet - validate the packet and route it.
1550 to either the WINS server or a normal response.
1551 ****************************************************************************/
1553 static void process_nmb_response(struct packet_struct *p)
1555 struct nmb_packet *nmb = &p->packet.nmb;
1556 struct subnet_record *subrec = NULL;
1557 struct response_record *rrec = NULL;
1559 debug_nmb_packet(p);
1561 if(validate_nmb_response_packet(nmb))
1562 return;
1564 if((subrec = find_subnet_for_nmb_packet(p, &rrec))==NULL)
1565 return;
1567 if(rrec == NULL) {
1568 DEBUG(0,("process_nmb_response: response packet received but no response record \
1569 found for id = %hu. Ignoring packet.\n", nmb->header.name_trn_id));
1570 return;
1573 /* Increment the number of responses received for this record. */
1574 rrec->num_msgs++;
1575 /* Ensure we don't re-send the request. */
1576 rrec->repeat_count = 0;
1578 /* Call the response received function for this packet. */
1579 (*rrec->resp_fn)(subrec, rrec, p);
1582 /*******************************************************************
1583 Run elements off the packet queue till its empty
1584 ******************************************************************/
1586 void run_packet_queue(void)
1588 struct packet_struct *p;
1590 while ((p = packet_queue)) {
1591 DLIST_REMOVE(packet_queue, p);
1593 switch (p->packet_type) {
1594 case NMB_PACKET:
1595 if(p->packet.nmb.header.response)
1596 process_nmb_response(p);
1597 else
1598 process_nmb_request(p);
1599 break;
1601 case DGRAM_PACKET:
1602 process_dgram(p);
1603 break;
1605 free_packet(p);
1609 /*******************************************************************
1610 Retransmit or timeout elements from all the outgoing subnet response
1611 record queues. NOTE that this code must also check the WINS server
1612 subnet for response records to timeout as the WINS server code
1613 can send requests to check if a client still owns a name.
1614 (Patch from Andrey Alekseyev <fetch@muffin.arcadia.spb.ru>).
1615 ******************************************************************/
1617 void retransmit_or_expire_response_records(time_t t)
1619 struct subnet_record *subrec;
1621 for (subrec = FIRST_SUBNET; subrec; subrec = get_next_subnet_maybe_unicast_or_wins_server(subrec)) {
1622 struct response_record *rrec, *nextrrec;
1624 restart:
1626 for (rrec = subrec->responselist; rrec; rrec = nextrrec) {
1627 nextrrec = rrec->next;
1629 if (rrec->repeat_time <= t) {
1630 if (rrec->repeat_count > 0) {
1631 /* Resend while we have a non-zero repeat_count. */
1632 if(!send_packet(rrec->packet)) {
1633 DEBUG(0,("retransmit_or_expire_response_records: Failed to resend packet id %hu \
1634 to IP %s on subnet %s\n", rrec->response_id, inet_ntoa(rrec->packet->ip), subrec->subnet_name));
1636 rrec->repeat_time = t + rrec->repeat_interval;
1637 rrec->repeat_count--;
1638 } else {
1639 DEBUG(4,("retransmit_or_expire_response_records: timeout for packet id %hu to IP %s \
1640 on subnet %s\n", rrec->response_id, inet_ntoa(rrec->packet->ip), subrec->subnet_name));
1643 * Check the flag in this record to prevent recursion if we end
1644 * up in this function again via the timeout function call.
1647 if(!rrec->in_expiration_processing) {
1650 * Set the recursion protection flag in this record.
1653 rrec->in_expiration_processing = True;
1655 /* Call the timeout function. This will deal with removing the
1656 timed out packet. */
1657 if(rrec->timeout_fn) {
1658 (*rrec->timeout_fn)(subrec, rrec);
1659 } else {
1660 /* We must remove the record ourself if there is
1661 no timeout function. */
1662 remove_response_record(subrec, rrec);
1664 /* We have changed subrec->responselist,
1665 * restart from the beginning of this list. */
1666 goto restart;
1667 } /* !rrec->in_expitation_processing */
1668 } /* rrec->repeat_count > 0 */
1669 } /* rrec->repeat_time <= t */
1670 } /* end for rrec */
1671 } /* end for subnet */
1674 /****************************************************************************
1675 Create an fd_set containing all the sockets in the subnet structures,
1676 plus the broadcast sockets.
1677 ***************************************************************************/
1679 struct socket_attributes {
1680 enum packet_type type;
1681 bool broadcast;
1684 static bool create_listen_pollfds(struct pollfd **pfds,
1685 struct socket_attributes **pattrs,
1686 int *pnum_sockets)
1688 struct subnet_record *subrec = NULL;
1689 int count = 0;
1690 int num = 0;
1691 struct pollfd *fds;
1692 struct socket_attributes *attrs;
1694 /* The ClientNMB and ClientDGRAM sockets */
1695 count = 2;
1697 /* Check that we can add all the fd's we need. */
1698 for (subrec = FIRST_SUBNET;
1699 subrec != NULL;
1700 subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec)) {
1701 if (subrec->nmb_sock != -1) {
1702 count += 1;
1704 if (subrec->dgram_sock != -1) {
1705 count += 1;
1707 if (subrec->nmb_bcast != -1) {
1708 count += 1;
1710 if (subrec->dgram_bcast != -1) {
1711 count += 1;
1715 fds = talloc_zero_array(NULL, struct pollfd, count);
1716 if (fds == NULL) {
1717 DEBUG(1, ("create_listen_pollfds: malloc fail for fds. "
1718 "size %d\n", count));
1719 return true;
1722 attrs = talloc_array(NULL, struct socket_attributes, count);
1723 if (fds == NULL) {
1724 DEBUG(1, ("create_listen_pollfds: malloc fail for attrs. "
1725 "size %d\n", count));
1726 SAFE_FREE(fds);
1727 return true;
1730 num = 0;
1732 fds[num].fd = ClientNMB;
1733 attrs[num].type = NMB_PACKET;
1734 attrs[num].broadcast = false;
1735 num += 1;
1737 fds[num].fd = ClientDGRAM;
1738 attrs[num].type = DGRAM_PACKET;
1739 attrs[num].broadcast = false;
1740 num += 1;
1742 for (subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec)) {
1744 if (subrec->nmb_sock != -1) {
1745 fds[num].fd = subrec->nmb_sock;
1746 attrs[num].type = NMB_PACKET;
1747 attrs[num].broadcast = false;
1748 num += 1;
1751 if (subrec->nmb_bcast != -1) {
1752 fds[num].fd = subrec->nmb_bcast;
1753 attrs[num].type = NMB_PACKET;
1754 attrs[num].broadcast = true;
1755 num += 1;
1758 if (subrec->dgram_sock != -1) {
1759 fds[num].fd = subrec->dgram_sock;
1760 attrs[num].type = DGRAM_PACKET;
1761 attrs[num].broadcast = false;
1762 num += 1;
1765 if (subrec->dgram_bcast != -1) {
1766 fds[num].fd = subrec->dgram_bcast;
1767 attrs[num].type = DGRAM_PACKET;
1768 attrs[num].broadcast = true;
1769 num += 1;
1773 TALLOC_FREE(*pfds);
1774 *pfds = fds;
1776 TALLOC_FREE(*pattrs);
1777 *pattrs = attrs;
1779 *pnum_sockets = count;
1781 return False;
1784 /****************************************************************************
1785 List of packets we're processing this select.
1786 ***************************************************************************/
1788 struct processed_packet {
1789 struct processed_packet *next;
1790 struct processed_packet *prev;
1791 enum packet_type packet_type;
1792 struct in_addr ip;
1793 int packet_id;
1796 /****************************************************************************
1797 Have we seen this before ?
1798 ***************************************************************************/
1800 static bool is_processed_packet(struct processed_packet *processed_packet_list,
1801 struct packet_struct *packet)
1803 struct processed_packet *p = NULL;
1805 for (p = processed_packet_list; p; p = p->next) {
1806 if (ip_equal_v4(p->ip, packet->ip) && p->packet_type == packet->packet_type) {
1807 if ((p->packet_type == NMB_PACKET) &&
1808 (p->packet_id ==
1809 packet->packet.nmb.header.name_trn_id)) {
1810 return true;
1811 } else if ((p->packet_type == DGRAM_PACKET) &&
1812 (p->packet_id ==
1813 packet->packet.dgram.header.dgm_id)) {
1814 return true;
1818 return false;
1821 /****************************************************************************
1822 Keep a list of what we've seen before.
1823 ***************************************************************************/
1825 static bool store_processed_packet(struct processed_packet **pp_processed_packet_list,
1826 struct packet_struct *packet)
1828 struct processed_packet *p = SMB_MALLOC_P(struct processed_packet);
1829 if (!p) {
1830 return false;
1832 p->packet_type = packet->packet_type;
1833 p->ip = packet->ip;
1834 if (packet->packet_type == NMB_PACKET) {
1835 p->packet_id = packet->packet.nmb.header.name_trn_id;
1836 } else if (packet->packet_type == DGRAM_PACKET) {
1837 p->packet_id = packet->packet.dgram.header.dgm_id;
1838 } else {
1839 SAFE_FREE(p);
1840 return false;
1843 DLIST_ADD(*pp_processed_packet_list, p);
1844 return true;
1847 /****************************************************************************
1848 Throw away what we've seen before.
1849 ***************************************************************************/
1851 static void free_processed_packet_list(struct processed_packet **pp_processed_packet_list)
1853 struct processed_packet *p = NULL, *next = NULL;
1855 for (p = *pp_processed_packet_list; p; p = next) {
1856 next = p->next;
1857 DLIST_REMOVE(*pp_processed_packet_list, p);
1858 SAFE_FREE(p);
1862 /****************************************************************************
1863 Listens for NMB or DGRAM packets, and queues them.
1864 return True if the socket is dead
1865 ***************************************************************************/
1867 bool listen_for_packets(bool run_election)
1869 static struct pollfd *fds = NULL;
1870 static struct socket_attributes *attrs = NULL;
1871 static int listen_number = 0;
1872 int num_sockets;
1873 int i;
1875 int pollrtn;
1876 int timeout;
1877 #ifndef SYNC_DNS
1878 int dns_fd;
1879 int dns_pollidx = -1;
1880 #endif
1881 struct processed_packet *processed_packet_list = NULL;
1883 if ((fds == NULL) || rescan_listen_set) {
1884 if (create_listen_pollfds(&fds, &attrs, &listen_number)) {
1885 DEBUG(0,("listen_for_packets: Fatal error. unable to create listen set. Exiting.\n"));
1886 return True;
1888 rescan_listen_set = False;
1892 * "fds" can be enlarged by event_add_to_poll_args
1893 * below. Shrink it again to what was given to us by
1894 * create_listen_pollfds.
1897 fds = talloc_realloc(NULL, fds, struct pollfd, listen_number);
1898 if (fds == NULL) {
1899 return true;
1901 num_sockets = listen_number;
1903 #ifndef SYNC_DNS
1904 dns_fd = asyncdns_fd();
1905 if (dns_fd != -1) {
1906 fds = talloc_realloc(NULL, fds, struct pollfd, num_sockets+1);
1907 if (fds == NULL) {
1908 return true;
1910 dns_pollidx = num_sockets;
1911 fds[num_sockets].fd = dns_fd;
1912 num_sockets += 1;
1914 #endif
1916 for (i=0; i<num_sockets; i++) {
1917 fds[i].events = POLLIN|POLLHUP;
1920 /* Process a signal and timer events now... */
1921 if (run_events_poll(nmbd_event_context(), 0, NULL, 0)) {
1922 return False;
1926 * During elections and when expecting a netbios response packet we
1927 * need to send election packets at tighter intervals.
1928 * Ideally it needs to be the interval (in ms) between time now and
1929 * the time we are expecting the next netbios packet.
1932 timeout = ((run_election||num_response_packets)
1933 ? 1 : NMBD_SELECT_LOOP) * 1000;
1935 event_add_to_poll_args(nmbd_event_context(), NULL,
1936 &fds, &num_sockets, &timeout);
1938 pollrtn = sys_poll(fds, num_sockets, timeout);
1940 if (run_events_poll(nmbd_event_context(), pollrtn, fds, num_sockets)) {
1941 return False;
1944 if (pollrtn == -1) {
1945 return False;
1948 #ifndef SYNC_DNS
1949 if ((dns_fd != -1) && (dns_pollidx != -1) &&
1950 (fds[dns_pollidx].revents & (POLLIN|POLLHUP|POLLERR))) {
1951 run_dns_queue();
1953 #endif
1955 for(i = 0; i < listen_number; i++) {
1956 enum packet_type packet_type;
1957 struct packet_struct *packet;
1958 const char *packet_name;
1959 int client_fd;
1960 int client_port;
1962 if ((fds[i].revents & (POLLIN|POLLHUP|POLLERR)) == 0) {
1963 continue;
1966 if (attrs[i].type == NMB_PACKET) {
1967 /* Port 137 */
1968 packet_type = NMB_PACKET;
1969 packet_name = "nmb";
1970 client_fd = ClientNMB;
1971 client_port = global_nmb_port;
1972 } else {
1973 /* Port 138 */
1974 packet_type = DGRAM_PACKET;
1975 packet_name = "dgram";
1976 client_fd = ClientDGRAM;
1977 client_port = DGRAM_PORT;
1980 packet = read_packet(fds[i].fd, packet_type);
1981 if (!packet) {
1982 continue;
1986 * If we got a packet on the broadcast socket and interfaces
1987 * only is set then check it came from one of our local nets.
1989 if (lp_bind_interfaces_only() &&
1990 (fds[i].fd == client_fd) &&
1991 (!is_local_net_v4(packet->ip))) {
1992 DEBUG(7,("discarding %s packet sent to broadcast socket from %s:%d\n",
1993 packet_name, inet_ntoa(packet->ip), packet->port));
1994 free_packet(packet);
1995 continue;
1998 if ((is_loopback_ip_v4(packet->ip) || ismyip_v4(packet->ip)) &&
1999 packet->port == client_port)
2001 if (client_port == DGRAM_PORT) {
2002 DEBUG(7,("discarding own dgram packet from %s:%d\n",
2003 inet_ntoa(packet->ip),packet->port));
2004 free_packet(packet);
2005 continue;
2008 if (packet->packet.nmb.header.nm_flags.bcast) {
2009 DEBUG(7,("discarding own nmb bcast packet from %s:%d\n",
2010 inet_ntoa(packet->ip),packet->port));
2011 free_packet(packet);
2012 continue;
2016 if (is_processed_packet(processed_packet_list, packet)) {
2017 DEBUG(7,("discarding duplicate packet from %s:%d\n",
2018 inet_ntoa(packet->ip),packet->port));
2019 free_packet(packet);
2020 continue;
2023 store_processed_packet(&processed_packet_list, packet);
2025 if (attrs[i].broadcast) {
2026 /* this is a broadcast socket */
2027 packet->send_fd = fds[i-1].fd;
2028 } else {
2029 /* this is already a unicast socket */
2030 packet->send_fd = fds[i].fd;
2033 queue_packet(packet);
2036 free_processed_packet_list(&processed_packet_list);
2037 return False;
2040 /****************************************************************************
2041 Construct and send a netbios DGRAM.
2042 **************************************************************************/
2044 bool send_mailslot(bool unique, const char *mailslot,char *buf, size_t len,
2045 const char *srcname, int src_type,
2046 const char *dstname, int dest_type,
2047 struct in_addr dest_ip,struct in_addr src_ip,
2048 int dest_port)
2050 bool loopback_this_packet = False;
2051 struct packet_struct p;
2052 struct dgram_packet *dgram = &p.packet.dgram;
2053 char *ptr,*p2;
2054 char tmp[4];
2056 memset((char *)&p,'\0',sizeof(p));
2058 if(ismyip_v4(dest_ip) && (dest_port == DGRAM_PORT)) /* Only if to DGRAM_PORT */
2059 loopback_this_packet = True;
2061 /* generate_name_trn_id(); */ /* Not used, so gone, RJS */
2063 /* DIRECT GROUP or UNIQUE datagram. */
2064 dgram->header.msg_type = unique ? 0x10 : 0x11;
2065 dgram->header.flags.node_type = M_NODE;
2066 dgram->header.flags.first = True;
2067 dgram->header.flags.more = False;
2068 dgram->header.dgm_id = generate_name_trn_id();
2069 dgram->header.source_ip = src_ip;
2070 dgram->header.source_port = DGRAM_PORT;
2071 dgram->header.dgm_length = 0; /* Let build_dgram() handle this. */
2072 dgram->header.packet_offset = 0;
2074 make_nmb_name(&dgram->source_name,srcname,src_type);
2075 make_nmb_name(&dgram->dest_name,dstname,dest_type);
2077 ptr = &dgram->data[0];
2079 /* Setup the smb part. */
2080 ptr -= 4; /* XXX Ugliness because of handling of tcp SMB length. */
2081 memcpy(tmp,ptr,4);
2083 if (smb_size + 17*2 + strlen(mailslot) + 1 + len > MAX_DGRAM_SIZE) {
2084 DEBUG(0, ("send_mailslot: Cannot write beyond end of packet\n"));
2085 return false;
2088 cli_set_message(ptr,17,strlen(mailslot) + 1 + len,True);
2089 memcpy(ptr,tmp,4);
2091 SCVAL(ptr,smb_com,SMBtrans);
2092 SSVAL(ptr,smb_vwv1,len);
2093 SSVAL(ptr,smb_vwv11,len);
2094 SSVAL(ptr,smb_vwv12,70 + strlen(mailslot));
2095 SSVAL(ptr,smb_vwv13,3);
2096 SSVAL(ptr,smb_vwv14,1);
2097 SSVAL(ptr,smb_vwv15,1);
2098 SSVAL(ptr,smb_vwv16,2);
2099 p2 = smb_buf(ptr);
2100 strlcpy_base(p2, mailslot, dgram->data, sizeof(dgram->data));
2101 p2 = skip_string(ptr,MAX_DGRAM_SIZE,p2);
2103 if (((p2+len) > dgram->data+sizeof(dgram->data)) || ((p2+len) < p2)) {
2104 DEBUG(0, ("send_mailslot: Cannot write beyond end of packet\n"));
2105 return False;
2106 } else {
2107 if (len) {
2108 memcpy(p2,buf,len);
2110 p2 += len;
2113 dgram->datasize = PTR_DIFF(p2,ptr+4); /* +4 for tcp length. */
2115 p.ip = dest_ip;
2116 p.port = dest_port;
2117 p.recv_fd = -1;
2118 p.send_fd = find_subnet_mailslot_fd_for_address( src_ip );
2119 p.timestamp = time(NULL);
2120 p.packet_type = DGRAM_PACKET;
2122 DEBUG(4,("send_mailslot: Sending to mailslot %s from %s IP %s ", mailslot,
2123 nmb_namestr(&dgram->source_name), inet_ntoa(src_ip)));
2124 DEBUG(4,("to %s IP %s\n", nmb_namestr(&dgram->dest_name), inet_ntoa(dest_ip)));
2126 debug_browse_data(buf, len);
2128 if(loopback_this_packet) {
2129 struct packet_struct *lo_packet = NULL;
2130 DEBUG(5,("send_mailslot: sending packet to ourselves.\n"));
2131 if((lo_packet = copy_packet(&p)) == NULL)
2132 return False;
2133 queue_packet(lo_packet);
2134 return True;
2135 } else {
2136 return(send_packet(&p));