2 Unix SMB/Netbios implementation.
4 NBT netbios routines and daemon - version 2
5 Copyright (C) Andrew Tridgell 1994-1997
6 Copyright (C) Luke Kenneth Casson Leighton 1994-1997
7 Copyright (C) Jeremy Allison 1994-1997
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 extern int ClientDGRAM;
29 extern int global_nmb_port;
31 extern int DEBUGLEVEL;
33 extern int num_response_packets;
36 extern struct in_addr loopback_ip;
38 /*******************************************************************
39 The global packet linked-list. Incoming entries are
40 added to the end of this list. It is supposed to remain fairly
41 short so we won't bother with an end pointer.
42 ******************************************************************/
44 static struct packet_struct *packet_queue = NULL;
46 /***************************************************************************
47 Utility function to find the specific fd to send a packet out on.
48 **************************************************************************/
50 static int find_subnet_fd_for_address( struct in_addr local_ip )
52 struct subnet_record *subrec;
54 for( subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec))
55 if(ip_equal(local_ip, subrec->myip))
56 return subrec->nmb_sock;
61 /***************************************************************************
62 Get/Set problematic nb_flags as network byte order 16 bit int.
63 **************************************************************************/
65 uint16 get_nb_flags(char *buf)
67 return ((((uint16)*buf)&0xFFFF) & NB_FLGMSK);
70 void set_nb_flags(char *buf, uint16 nb_flags)
72 *buf++ = ((nb_flags & NB_FLGMSK) & 0xFF);
76 /***************************************************************************
77 Dumps out the browse packet data.
78 **************************************************************************/
80 static void debug_browse_data(char *outbuf, int len)
83 for (i = 0; i < len; i+= 16)
85 DEBUG(4, ("%3x char ", i));
87 for (j = 0; j < 16; j++)
89 unsigned char x = outbuf[i+j];
90 if (x < 32 || x > 127)
98 DEBUG(4, (" hex ", i));
100 for (j = 0; j < 16; j++)
104 DEBUG(4, (" %02x", (unsigned char)outbuf[i+j]));
111 /***************************************************************************
112 Generates the unique transaction identifier
113 **************************************************************************/
115 static uint16 name_trn_id=0;
117 static uint16 generate_name_trn_id(void)
122 name_trn_id = (time(NULL)%(unsigned)0x7FFF) + (getpid()%(unsigned)100);
124 name_trn_id = (name_trn_id+1) % (unsigned)0x7FFF;
128 /***************************************************************************
129 Either loops back or sends out a completed NetBIOS packet.
130 **************************************************************************/
132 static BOOL send_netbios_packet(struct packet_struct *p)
134 BOOL loopback_this_packet = False;
136 /* Check if we are sending to or from ourselves as a WINS server. */
137 if(ismyip(p->ip) && (p->port == global_nmb_port))
138 loopback_this_packet = True;
140 if(loopback_this_packet)
142 struct packet_struct *lo_packet = NULL;
143 DEBUG(5,("send_netbios_packet: sending packet to ourselves.\n"));
144 if((lo_packet = copy_packet(p)) == NULL)
146 queue_packet(lo_packet);
148 else if (!send_packet(p))
150 DEBUG(0,("send_netbios_packet: send_packet() to IP %s port %d failed\n",
151 inet_ntoa(p->ip),p->port));
158 /***************************************************************************
159 Sets up the common elements of an outgoing NetBIOS packet.
160 **************************************************************************/
162 static struct packet_struct *create_and_init_netbios_packet(struct nmb_name *nmbname,
164 struct in_addr to_ip)
166 struct packet_struct *packet = NULL;
167 struct nmb_packet *nmb = NULL;
169 /* Allocate the packet_struct we will return. */
170 if((packet = (struct packet_struct *)malloc(sizeof(*packet))) == NULL)
172 DEBUG(0,("create_and_init_netbios_packet: malloc fail (1) for packet struct.\n"));
176 bzero((char *)packet,sizeof(*packet));
178 nmb = &packet->packet.nmb;
180 nmb->header.name_trn_id = generate_name_trn_id();
181 nmb->header.response = False;
182 nmb->header.nm_flags.recursion_desired = False;
183 nmb->header.nm_flags.recursion_available = False;
184 nmb->header.nm_flags.trunc = False;
185 nmb->header.nm_flags.authoritative = False;
186 nmb->header.nm_flags.bcast = bcast;
188 nmb->header.rcode = 0;
189 nmb->header.qdcount = 1;
190 nmb->header.ancount = 0;
191 nmb->header.nscount = 0;
193 nmb->question.question_name = *nmbname;
194 nmb->question.question_type = QUESTION_TYPE_NB_QUERY;
195 nmb->question.question_class = QUESTION_CLASS_IN;
198 packet->port = NMB_PORT;
199 packet->fd = ClientNMB;
200 packet->timestamp = time(NULL);
201 packet->packet_type = NMB_PACKET;
202 packet->locked = False;
204 return packet; /* Caller must free. */
207 /***************************************************************************
208 Sets up the common elements of register, refresh or release packet.
209 **************************************************************************/
211 static BOOL create_and_init_additional_record(struct packet_struct *packet,
213 struct in_addr *register_ip)
215 struct nmb_packet *nmb = &packet->packet.nmb;
217 if((nmb->additional = (struct res_rec *)malloc(sizeof(struct res_rec))) == NULL)
219 DEBUG(0,("initiate_name_register_packet: malloc fail for additional record.\n"));
223 bzero((char *)nmb->additional,sizeof(struct res_rec));
225 nmb->additional->rr_name = nmb->question.question_name;
226 nmb->additional->rr_type = RR_TYPE_NB;
227 nmb->additional->rr_class = RR_CLASS_IN;
229 nmb->additional->ttl = lp_max_ttl();
231 nmb->additional->rdlength = 6;
233 set_nb_flags(nmb->additional->rdata,nb_flags);
235 /* Set the address for the name we are registering. */
236 putip(&nmb->additional->rdata[2], register_ip);
238 /* Ensure that we send out the file descriptor to give us the
239 the specific source address we are registering as our
240 IP source address. */
242 packet->fd = find_subnet_fd_for_address( *register_ip );
247 /***************************************************************************
248 Sends out a name query.
249 **************************************************************************/
251 static BOOL initiate_name_query_packet( struct packet_struct *packet)
253 struct nmb_packet *nmb = NULL;
255 nmb = &packet->packet.nmb;
257 nmb->header.opcode = NMB_NAME_QUERY_OPCODE;
258 nmb->header.arcount = 0;
260 nmb->header.nm_flags.recursion_desired = True;
262 DEBUG(4,("initiate_name_query_packet: sending query for name %s (bcast=%s) to IP %s\n",
263 namestr(&nmb->question.question_name),
264 BOOLSTR(nmb->header.nm_flags.bcast), inet_ntoa(packet->ip)));
266 return send_netbios_packet( packet );
269 /***************************************************************************
270 Sends out a name register.
271 **************************************************************************/
273 static BOOL initiate_name_register_packet( struct packet_struct *packet,
274 uint16 nb_flags, struct in_addr *register_ip)
276 struct nmb_packet *nmb = &packet->packet.nmb;
278 nmb->header.opcode = NMB_NAME_REG_OPCODE;
279 nmb->header.arcount = 1;
281 nmb->header.nm_flags.recursion_desired = True;
283 if(create_and_init_additional_record(packet, nb_flags, register_ip) == False)
286 DEBUG(4,("initiate_name_register_packet: sending registration for name %s (bcast=%s) to IP %s\n",
287 namestr(&nmb->additional->rr_name),
288 BOOLSTR(nmb->header.nm_flags.bcast), inet_ntoa(packet->ip)));
290 return send_netbios_packet( packet );
293 /***************************************************************************
294 Sends out a multihomed name register.
295 **************************************************************************/
297 static BOOL initiate_multihomed_name_register_packet( struct packet_struct *packet,
298 uint16 nb_flags, struct in_addr *register_ip)
300 struct nmb_packet *nmb = &packet->packet.nmb;
301 char second_ip_buf[25];
303 strcpy(second_ip_buf, inet_ntoa(packet->ip));
305 nmb->header.opcode = NMB_NAME_MULTIHOMED_REG_OPCODE;
306 nmb->header.arcount = 1;
308 nmb->header.nm_flags.recursion_desired = True;
310 if(create_and_init_additional_record(packet, nb_flags, register_ip) == False)
313 DEBUG(4,("initiate_multihomed_name_register_packet: sending registration \
314 for name %s IP %s (bcast=%s) to IP %s\n",
315 namestr(&nmb->additional->rr_name), inet_ntoa(*register_ip),
316 BOOLSTR(nmb->header.nm_flags.bcast), second_ip_buf ));
318 return send_netbios_packet( packet );
321 /***************************************************************************
322 Sends out a name refresh.
323 **************************************************************************/
325 static BOOL initiate_name_refresh_packet( struct packet_struct *packet,
326 uint16 nb_flags, struct in_addr *refresh_ip)
328 struct nmb_packet *nmb = &packet->packet.nmb;
330 nmb->header.opcode = NMB_NAME_REFRESH_OPCODE_8;
331 nmb->header.arcount = 1;
333 nmb->header.nm_flags.recursion_desired = False;
335 if(create_and_init_additional_record(packet, nb_flags, refresh_ip) == False)
338 DEBUG(4,("initiate_name_refresh_packet: sending refresh for name %s (bcast=%s) to IP %s\n",
339 namestr(&nmb->additional->rr_name),
340 BOOLSTR(nmb->header.nm_flags.bcast), inet_ntoa(packet->ip)));
342 return send_netbios_packet( packet );
345 /***************************************************************************
346 Sends out a name release.
347 **************************************************************************/
349 static BOOL initiate_name_release_packet( struct packet_struct *packet,
350 uint16 nb_flags, struct in_addr *release_ip)
352 struct nmb_packet *nmb = &packet->packet.nmb;
354 nmb->header.opcode = NMB_NAME_RELEASE_OPCODE;
355 nmb->header.arcount = 1;
357 nmb->header.nm_flags.recursion_desired = False;
359 if(create_and_init_additional_record(packet, nb_flags, release_ip) == False)
362 DEBUG(4,("initiate_name_release_packet: sending release for name %s (bcast=%s) to IP %s\n",
363 namestr(&nmb->additional->rr_name),
364 BOOLSTR(nmb->header.nm_flags.bcast), inet_ntoa(packet->ip)));
366 return send_netbios_packet( packet );
369 /***************************************************************************
370 Sends out a node status.
371 **************************************************************************/
373 static BOOL initiate_node_status_packet( struct packet_struct *packet )
375 struct nmb_packet *nmb = &packet->packet.nmb;
377 nmb->header.opcode = NMB_NAME_QUERY_OPCODE;
378 nmb->header.arcount = 0;
380 nmb->header.nm_flags.recursion_desired = False;
382 nmb->question.question_type = QUESTION_TYPE_NB_STATUS;
384 DEBUG(4,("initiate_node_status_packet: sending node status request for name %s to IP %s\n",
385 namestr(&nmb->question.question_name),
386 inet_ntoa(packet->ip)));
388 return send_netbios_packet( packet );
391 /****************************************************************************
392 Simplification functions for queuing standard packets.
393 These should be the only publicly callable functions for sending
395 ****************************************************************************/
397 /****************************************************************************
398 Assertion - we should never be sending nmbd packets on the remote
400 ****************************************************************************/
402 static BOOL assert_check_subnet(struct subnet_record *subrec)
404 if( subrec == remote_broadcast_subnet)
406 DEBUG(0,("assert_check_subnet: Attempt to send packet on remote broadcast subnet. \
413 /****************************************************************************
414 Queue a register name packet to the broadcast address of a subnet.
415 ****************************************************************************/
417 struct response_record *queue_register_name( struct subnet_record *subrec,
418 response_function resp_fn,
419 timeout_response_function timeout_fn,
420 register_name_success_function success_fn,
421 register_name_fail_function fail_fn,
422 struct userdata_struct *userdata,
423 struct nmb_name *nmbname,
426 struct packet_struct *p;
427 struct response_record *rrec;
428 BOOL bcast = (subrec == unicast_subnet) ? False : True;
430 if(assert_check_subnet(subrec))
433 if(( p = create_and_init_netbios_packet(nmbname, bcast,
434 subrec->bcast_ip)) == NULL)
437 if(initiate_name_register_packet( p, nb_flags,
438 iface_ip(subrec->bcast_ip)) == False)
445 if((rrec = make_response_record(subrec, /* subnet record. */
446 p, /* packet we sent. */
447 resp_fn, /* function to call on response. */
448 timeout_fn, /* function to call on timeout. */
449 (success_function)success_fn, /* function to call on operation success. */
450 (fail_function)fail_fn, /* function to call on operation fail. */
461 /****************************************************************************
462 Queue a multihomed register name packet to the broadcast address of a subnet.
463 ****************************************************************************/
465 struct response_record *queue_register_multihomed_name( struct subnet_record *subrec,
466 response_function resp_fn,
467 timeout_response_function timeout_fn,
468 register_name_success_function success_fn,
469 register_name_fail_function fail_fn,
470 struct userdata_struct *userdata,
471 struct nmb_name *nmbname,
473 struct in_addr register_ip)
475 struct packet_struct *p;
476 struct response_record *rrec;
481 if(subrec != unicast_subnet)
483 DEBUG(0,("queue_register_multihomed_name: should only be done on \
484 unicast subnet. subnet is %s\n.", subrec->subnet_name ));
488 if(assert_check_subnet(subrec))
491 if(( p = create_and_init_netbios_packet(nmbname, bcast,
492 subrec->bcast_ip)) == NULL)
495 if (nb_flags & NB_GROUP)
496 ret = initiate_name_register_packet( p, nb_flags, ®ister_ip);
498 ret = initiate_multihomed_name_register_packet( p, nb_flags, ®ister_ip);
507 if((rrec = make_response_record(subrec, /* subnet record. */
508 p, /* packet we sent. */
509 resp_fn, /* function to call on response. */
510 timeout_fn, /* function to call on timeout. */
511 (success_function)success_fn, /* function to call on operation success. */
512 (fail_function)fail_fn, /* function to call on operation fail. */
523 /****************************************************************************
524 Queue a release name packet to the broadcast address of a subnet.
525 ****************************************************************************/
527 struct response_record *queue_release_name( struct subnet_record *subrec,
528 response_function resp_fn,
529 timeout_response_function timeout_fn,
530 release_name_success_function success_fn,
531 release_name_fail_function fail_fn,
532 struct userdata_struct *userdata,
533 struct nmb_name *nmbname,
535 struct in_addr release_ip)
537 BOOL bcast = (subrec == unicast_subnet) ? False : True;
538 struct packet_struct *p;
539 struct response_record *rrec;
541 if(assert_check_subnet(subrec))
544 if(( p = create_and_init_netbios_packet(nmbname, bcast,
545 subrec->bcast_ip)) == NULL)
548 if(initiate_name_release_packet( p, nb_flags, &release_ip) == False)
555 if((rrec = make_response_record(subrec, /* subnet record. */
556 p, /* packet we sent. */
557 resp_fn, /* function to call on response. */
558 timeout_fn, /* function to call on timeout. */
559 (success_function)success_fn, /* function to call on operation success. */
560 (fail_function)fail_fn, /* function to call on operation fail. */
571 /****************************************************************************
572 Queue a refresh name packet to the broadcast address of a subnet.
573 ****************************************************************************/
575 struct response_record *queue_refresh_name( struct subnet_record *subrec,
576 response_function resp_fn,
577 timeout_response_function timeout_fn,
578 refresh_name_success_function success_fn,
579 refresh_name_fail_function fail_fn,
580 struct userdata_struct *userdata,
581 struct name_record *namerec,
582 struct in_addr refresh_ip)
584 BOOL bcast = (subrec == unicast_subnet) ? False : True;
585 struct packet_struct *p;
586 struct response_record *rrec;
588 if(assert_check_subnet(subrec))
591 if(( p = create_and_init_netbios_packet(&namerec->name, bcast,
592 subrec->bcast_ip)) == NULL)
595 if(initiate_name_refresh_packet( p, namerec->nb_flags, &refresh_ip) == False)
602 if((rrec = make_response_record(subrec, /* subnet record. */
603 p, /* packet we sent. */
604 resp_fn, /* function to call on response. */
605 timeout_fn, /* function to call on timeout. */
606 (success_function)success_fn, /* function to call on operation success. */
607 (fail_function)fail_fn, /* function to call on operation fail. */
618 /****************************************************************************
619 Queue a query name packet to the broadcast address of a subnet.
620 ****************************************************************************/
622 struct response_record *queue_query_name( struct subnet_record *subrec,
623 response_function resp_fn,
624 timeout_response_function timeout_fn,
625 query_name_success_function success_fn,
626 query_name_fail_function fail_fn,
627 struct userdata_struct *userdata,
628 struct nmb_name *nmbname)
630 struct packet_struct *p;
631 struct response_record *rrec;
634 if ((subrec == unicast_subnet) || (subrec == wins_server_subnet))
637 if(assert_check_subnet(subrec))
640 if(( p = create_and_init_netbios_packet(nmbname, bcast,
641 subrec->bcast_ip)) == NULL)
644 if(initiate_name_query_packet( p ) == False)
651 if((rrec = make_response_record(subrec, /* subnet record. */
652 p, /* packet we sent. */
653 resp_fn, /* function to call on response. */
654 timeout_fn, /* function to call on timeout. */
655 (success_function)success_fn, /* function to call on operation success. */
656 (fail_function)fail_fn, /* function to call on operation fail. */
667 /****************************************************************************
668 Queue a node status packet to a given name and address.
669 ****************************************************************************/
671 struct response_record *queue_node_status( struct subnet_record *subrec,
672 response_function resp_fn,
673 timeout_response_function timeout_fn,
674 node_status_success_function success_fn,
675 node_status_fail_function fail_fn,
676 struct userdata_struct *userdata,
677 struct nmb_name *nmbname,
678 struct in_addr send_ip)
680 struct packet_struct *p;
681 struct response_record *rrec;
685 if(subrec != unicast_subnet)
687 DEBUG(0,("queue_register_multihomed_name: should only be done on \
688 unicast subnet. subnet is %s\n.", subrec->subnet_name ));
692 if(assert_check_subnet(subrec))
695 if(( p = create_and_init_netbios_packet(nmbname, bcast,
699 if(initiate_node_status_packet(p) == False)
706 if((rrec = make_response_record(subrec, /* subnet record. */
707 p, /* packet we sent. */
708 resp_fn, /* function to call on response. */
709 timeout_fn, /* function to call on timeout. */
710 (success_function)success_fn, /* function to call on operation success. */
711 (fail_function)fail_fn, /* function to call on operation fail. */
722 /****************************************************************************
723 Reply to a netbios name packet. see rfc1002.txt
724 ****************************************************************************/
726 void reply_netbios_packet(struct packet_struct *orig_packet,
727 int rcode, enum netbios_reply_type_code rcv_code, int opcode,
728 int ttl, char *data,int len)
730 struct packet_struct packet;
731 struct nmb_packet *nmb = NULL;
732 struct res_rec answers;
733 struct nmb_packet *orig_nmb = &orig_packet->packet.nmb;
734 BOOL loopback_this_packet = False;
735 char *packet_type = "unknown";
737 /* Check if we are sending to or from ourselves. */
738 if(ismyip(orig_packet->ip) && (orig_packet->port == global_nmb_port))
739 loopback_this_packet = True;
741 nmb = &packet.packet.nmb;
743 /* Do a partial copy of the packet. We clear the locked flag and
744 the resource record pointers. */
745 packet = *orig_packet; /* Full structure copy. */
746 packet.locked = False;
749 nmb->additional = NULL;
755 packet_type = "nmb_status";
756 nmb->header.nm_flags.recursion_desired = False;
757 nmb->header.nm_flags.recursion_available = False;
762 packet_type = "nmb_query";
763 nmb->header.nm_flags.recursion_desired = True;
764 nmb->header.nm_flags.recursion_available = True;
768 case NMB_REG_REFRESH:
770 packet_type = "nmb_reg";
771 nmb->header.nm_flags.recursion_desired = True;
772 nmb->header.nm_flags.recursion_available = True;
777 packet_type = "nmb_rel";
778 nmb->header.nm_flags.recursion_desired = False;
779 nmb->header.nm_flags.recursion_available = False;
784 packet_type = "nmb_wack";
785 nmb->header.nm_flags.recursion_desired = False;
786 nmb->header.nm_flags.recursion_available = False;
791 packet_type = "wins_reg";
792 nmb->header.nm_flags.recursion_desired = True;
793 nmb->header.nm_flags.recursion_available = True;
798 packet_type = "wins_query";
799 nmb->header.nm_flags.recursion_desired = True;
800 nmb->header.nm_flags.recursion_available = True;
806 DEBUG(0,("reply_netbios_packet: Unknown packet type: %s %s to ip %s\n",
807 packet_type, namestr(&orig_nmb->question.question_name),
808 inet_ntoa(packet.ip)));
814 DEBUG(4,("reply_netbios_packet: sending a reply of packet type: %s %s to ip %s \
816 packet_type, namestr(&orig_nmb->question.question_name),
817 inet_ntoa(packet.ip), orig_nmb->header.name_trn_id));
819 nmb->header.name_trn_id = orig_nmb->header.name_trn_id;
820 nmb->header.opcode = opcode;
821 nmb->header.response = True;
822 nmb->header.nm_flags.bcast = False;
823 nmb->header.nm_flags.trunc = False;
824 nmb->header.nm_flags.authoritative = True;
826 nmb->header.rcode = rcode;
827 nmb->header.qdcount = 0;
828 nmb->header.ancount = 1;
829 nmb->header.nscount = 0;
830 nmb->header.arcount = 0;
832 bzero((char*)&nmb->question,sizeof(nmb->question));
834 nmb->answers = &answers;
835 bzero((char*)nmb->answers,sizeof(*nmb->answers));
837 nmb->answers->rr_name = orig_nmb->question.question_name;
838 nmb->answers->rr_type = orig_nmb->question.question_type;
839 nmb->answers->rr_class = orig_nmb->question.question_class;
840 nmb->answers->ttl = ttl;
844 nmb->answers->rdlength = len;
845 memcpy(nmb->answers->rdata, data, len);
848 packet.packet_type = NMB_PACKET;
849 /* Ensure we send out on the same fd that the original
850 packet came in on to give the correct source IP address. */
851 packet.fd = orig_packet->fd;
852 packet.timestamp = time(NULL);
854 debug_nmb_packet(&packet);
856 if(loopback_this_packet)
858 struct packet_struct *lo_packet;
859 DEBUG(5,("reply_netbios_packet: sending packet to ourselves.\n"));
860 if((lo_packet = copy_packet(&packet)) == NULL)
862 queue_packet(lo_packet);
864 else if (!send_packet(&packet))
866 DEBUG(0,("reply_netbios_packet: send_packet to IP %s port %d failed\n",
867 inet_ntoa(packet.ip),packet.port));
871 /*******************************************************************
872 Queue a packet into a packet queue
873 ******************************************************************/
875 void queue_packet(struct packet_struct *packet)
877 struct packet_struct *p;
883 packet_queue = packet;
887 /* find the bottom */
888 for (p=packet_queue;p->next;p=p->next)
896 /****************************************************************************
897 Try and find a matching subnet record for a datagram port 138 packet.
898 ****************************************************************************/
900 static struct subnet_record *find_subnet_for_dgram_browse_packet(struct packet_struct *p)
902 struct subnet_record *subrec;
904 /* Go through all the broadcast subnets and see if the mask matches. */
905 for (subrec = FIRST_SUBNET; subrec ; subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec))
907 if(same_net(p->ip, subrec->bcast_ip, subrec->mask_ip))
911 /* If the subnet record is the remote announce broadcast subnet,
912 hack it here to be the first subnet. This is really gross and
913 is needed due to people turning on port 137/138 broadcast
914 forwarding on their routers. May fire and brimstone rain
921 /****************************************************************************
922 Dispatch a browse frame from port 138 to the correct processing function.
923 ****************************************************************************/
925 void process_browse_packet(struct packet_struct *p, char *buf,int len)
927 struct dgram_packet *dgram = &p->packet.dgram;
928 int command = CVAL(buf,0);
929 struct subnet_record *subrec = find_subnet_for_dgram_browse_packet(p);
931 /* Drop the packet if it's a different NetBIOS scope, or
932 the source is from one of our names. */
934 if (!strequal(dgram->dest_name.scope,scope ))
936 DEBUG(7,("process_browse_packet: Discarding datagram from IP %s. Scope (%s) \
937 mismatch with our scope (%s).\n", inet_ntoa(p->ip), dgram->dest_name.scope, scope));
941 if (is_myname(dgram->source_name.name))
943 DEBUG(0,("process_browse_packet: Discarding datagram from IP %s. Source name \
944 %s is one of our names !\n", inet_ntoa(p->ip), namestr(&dgram->source_name)));
950 case ANN_HostAnnouncement:
952 debug_browse_data(buf, len);
953 process_host_announce(subrec, p, buf+1);
956 case ANN_DomainAnnouncement:
958 debug_browse_data(buf, len);
959 process_workgroup_announce(subrec, p, buf+1);
962 case ANN_LocalMasterAnnouncement:
964 debug_browse_data(buf, len);
965 process_local_master_announce(subrec, p, buf+1);
968 case ANN_AnnouncementRequest:
970 process_announce_request(subrec, p, buf+1);
975 process_election(subrec, p, buf+1);
978 case ANN_GetBackupListReq:
980 debug_browse_data(buf, len);
982 /* This is one occasion where we change a subnet that is
983 given to us. If the packet was sent to WORKGROUP<1b> instead
984 of WORKGROUP<1d> then it was unicast to us a domain master
985 browser. Change subrec to unicast.
987 if(dgram->dest_name.name_type == 0x1b)
988 subrec = unicast_subnet;
990 process_get_backup_list_request(subrec, p, buf+1);
993 case ANN_GetBackupListResp:
995 debug_browse_data(buf, len);
996 /* We never send ANN_GetBackupListReq so we
997 should never get these. */
998 DEBUG(0,("process_browse_packet: Discarding GetBackupListResponse \
999 packet from %s IP %s\n", namestr(&dgram->source_name), inet_ntoa(p->ip)));
1002 case ANN_ResetBrowserState:
1004 process_reset_browser(subrec, p, buf+1);
1007 case ANN_MasterAnnouncement:
1009 /* Master browser datagrams must be processed
1010 on the unicast subnet. */
1011 subrec = unicast_subnet;
1013 process_master_browser_announce(subrec, p, buf+1);
1018 DEBUG(0,("process_browse_packet: On subnet %s ignoring browse packet \
1019 command code %d from %s IP %s to %s\n",
1020 subrec->subnet_name, command, namestr(&dgram->source_name),
1021 inet_ntoa(p->ip), namestr(&dgram->dest_name)));
1026 /****************************************************************************
1027 Determine if a packet is for us on port 138. Note that to have any chance of
1028 being efficient we need to drop as many packets as possible at this
1029 stage as subsequent processing is expensive.
1030 ****************************************************************************/
1032 static BOOL listening(struct packet_struct *p,struct nmb_name *nbname)
1034 struct subnet_record *subrec = NULL;
1036 for (subrec = FIRST_SUBNET; subrec ; subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec))
1038 if(same_net(p->ip, subrec->bcast_ip, subrec->mask_ip))
1043 subrec = unicast_subnet;
1045 return (find_name_on_subnet(subrec, nbname, FIND_SELF_NAME) != NULL);
1048 /****************************************************************************
1049 Process udp 138 datagrams
1050 ****************************************************************************/
1052 static void process_dgram(struct packet_struct *p)
1057 struct dgram_packet *dgram = &p->packet.dgram;
1059 /* If we aren't listening to the destination name then ignore the packet */
1060 if (!listening(p,&dgram->dest_name))
1062 DEBUG(5,("process_dgram: ignoring dgram packet sent to name %s from %s\n",
1063 namestr(&dgram->dest_name), inet_ntoa(p->ip)));
1067 if (dgram->header.msg_type != 0x10 &&
1068 dgram->header.msg_type != 0x11 &&
1069 dgram->header.msg_type != 0x12)
1071 /* Don't process error packets etc yet */
1072 DEBUG(5,("process_dgram: ignoring dgram packet sent to name %s from IP %s as it is \
1073 an error packet of type %x\n",
1074 namestr(&dgram->dest_name), inet_ntoa(p->ip), dgram->header.msg_type));
1078 buf = &dgram->data[0];
1079 buf -= 4; /* XXXX for the pseudo tcp length -
1080 someday I need to get rid of this */
1082 if (CVAL(buf,smb_com) != SMBtrans)
1085 len = SVAL(buf,smb_vwv11);
1086 buf2 = smb_base(buf) + SVAL(buf,smb_vwv12);
1088 DEBUG(4,("process_dgram: datagram from %s to %s IP %s for %s of type %d len=%d\n",
1089 namestr(&dgram->source_name),namestr(&dgram->dest_name),
1090 inet_ntoa(p->ip), smb_buf(buf),CVAL(buf2,0),len));
1096 /* Datagram packet received for the browser mailslot */
1097 if (strequal(smb_buf(buf),BROWSE_MAILSLOT))
1099 process_browse_packet(p,buf2,len);
1103 /* Datagram packet received for the domain logon mailslot */
1104 if (strequal(smb_buf(buf),NET_LOGON_MAILSLOT))
1106 process_logon_packet(p,buf2,len,NET_LOGON_MAILSLOT);
1110 /* Datagram packet received for the NT domain logon mailslot */
1111 if (strequal(smb_buf(buf),NT_LOGON_MAILSLOT))
1113 process_logon_packet(p,buf2,len,NT_LOGON_MAILSLOT);
1118 /****************************************************************************
1119 Validate a response nmb packet.
1120 ****************************************************************************/
1122 BOOL validate_nmb_response_packet( struct nmb_packet *nmb )
1124 BOOL ignore = False;
1126 switch (nmb->header.opcode)
1128 case NMB_NAME_REG_OPCODE:
1129 case NMB_NAME_REFRESH_OPCODE_8: /* ambiguity in rfc1002 about which is correct. */
1130 case NMB_NAME_REFRESH_OPCODE_9: /* WinNT uses 8 by default. */
1131 if (nmb->header.ancount == 0)
1133 DEBUG(0,("validate_nmb_response_packet: Bad REG/REFRESH Packet. "));
1138 case NMB_NAME_QUERY_OPCODE:
1139 if ((nmb->header.ancount != 0) && (nmb->header.ancount != 1))
1141 DEBUG(0,("validate_nmb_response_packet: Bad QUERY Packet. "));
1145 case NMB_NAME_RELEASE_OPCODE:
1146 if (nmb->header.ancount == 0)
1148 DEBUG(0,("validate_nmb_response_packet: Bad RELEASE Packet. "));
1152 case NMB_WACK_OPCODE:
1153 /* Check WACK response here. */
1154 if (nmb->header.ancount != 1)
1156 DEBUG(0,("validate_nmb_response_packet: Bad WACK Packet. "));
1161 DEBUG(0,("validate_nmb_response_packet: Ignoring packet with unknown opcode %d.\n",
1162 nmb->header.opcode));
1167 DEBUG(0,("Ignoring response packet with opcode %d.\n", nmb->header.opcode));
1172 /****************************************************************************
1173 Validate a request nmb packet.
1174 ****************************************************************************/
1176 BOOL validate_nmb_packet( struct nmb_packet *nmb )
1178 BOOL ignore = False;
1180 switch (nmb->header.opcode)
1182 case NMB_NAME_REG_OPCODE:
1183 case NMB_NAME_REFRESH_OPCODE_8: /* ambiguity in rfc1002 about which is correct. */
1184 case NMB_NAME_REFRESH_OPCODE_9: /* WinNT uses 8 by default. */
1185 case NMB_NAME_MULTIHOMED_REG_OPCODE:
1186 if (nmb->header.qdcount==0 || nmb->header.arcount==0)
1188 DEBUG(0,("validate_nmb_packet: Bad REG/REFRESH Packet. "));
1193 case NMB_NAME_QUERY_OPCODE:
1194 if ((nmb->header.qdcount == 0) ||
1195 ((nmb->question.question_type != QUESTION_TYPE_NB_QUERY) &&
1196 (nmb->question.question_type != QUESTION_TYPE_NB_STATUS)))
1198 DEBUG(0,("validate_nmb_packet: Bad QUERY Packet. "));
1203 case NMB_NAME_RELEASE_OPCODE:
1204 if (nmb->header.qdcount==0 || nmb->header.arcount==0)
1206 DEBUG(0,("validate_nmb_packet: Bad RELEASE Packet. "));
1211 DEBUG(0,("validate_nmb_packet: Ignoring packet with unknown opcode %d.\n",
1212 nmb->header.opcode));
1217 DEBUG(0,("validate_nmb_packet: Ignoring request packet with opcode %d.\n", nmb->header.opcode));
1222 /****************************************************************************
1223 Find a subnet (and potentially a response record) for a packet.
1224 ****************************************************************************/
1226 static struct subnet_record *find_subnet_for_nmb_packet( struct packet_struct *p,
1227 struct response_record **pprrec)
1229 struct nmb_packet *nmb = &p->packet.nmb;
1230 struct response_record *rrec = NULL;
1231 struct subnet_record *subrec = NULL;
1236 if(nmb->header.response)
1238 /* It's a response packet. Find a record for it or it's an error. */
1240 rrec = find_response_record( &subrec, nmb->header.name_trn_id);
1243 DEBUG(0,("find_subnet_for_nmb_packet: response record not found for response id %hu\n",
1244 nmb->header.name_trn_id));
1250 DEBUG(0,("find_subnet_for_nmb_packet: subnet record not found for response id %hu\n",
1251 nmb->header.name_trn_id));
1260 /* Try and see what subnet this packet belongs to. */
1263 if(packet_is_for_wins_server(p))
1264 return wins_server_subnet;
1266 /* If it wasn't a broadcast packet then send to the UNICAST subnet. */
1267 if(nmb->header.nm_flags.bcast == False)
1268 return unicast_subnet;
1270 /* Go through all the broadcast subnets and see if the mask matches. */
1271 for (subrec = FIRST_SUBNET; subrec ; subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec))
1273 if(same_net(p->ip, subrec->bcast_ip, subrec->mask_ip))
1277 /* If none match it must have been a directed broadcast - assign
1278 the remote_broadcast_subnet. */
1279 return remote_broadcast_subnet;
1282 /****************************************************************************
1283 Process a nmb request packet - validate the packet and route it.
1284 ****************************************************************************/
1286 static void process_nmb_request(struct packet_struct *p)
1288 struct nmb_packet *nmb = &p->packet.nmb;
1289 struct subnet_record *subrec = NULL;
1291 debug_nmb_packet(p);
1293 /* Ensure we have a good packet. */
1294 if(validate_nmb_packet(nmb))
1297 /* Allocate a subnet to this packet - if we cannot - fail. */
1298 if((subrec = find_subnet_for_nmb_packet(p, NULL))==NULL)
1301 switch (nmb->header.opcode)
1303 case NMB_NAME_REG_OPCODE:
1304 if(subrec == wins_server_subnet)
1305 wins_process_name_registration_request(subrec, p);
1307 process_name_registration_request(subrec, p);
1310 case NMB_NAME_REFRESH_OPCODE_8: /* ambiguity in rfc1002 about which is correct. */
1311 case NMB_NAME_REFRESH_OPCODE_9:
1312 if(subrec == wins_server_subnet)
1313 wins_process_name_refresh_request(subrec, p);
1315 process_name_refresh_request(subrec, p);
1318 case NMB_NAME_MULTIHOMED_REG_OPCODE:
1319 if(subrec == wins_server_subnet)
1320 wins_process_multihomed_name_registration_request(subrec, p);
1323 DEBUG(0,("process_nmb_request: Multihomed registration request must be \
1324 directed at a WINS server.\n"));
1328 case NMB_NAME_QUERY_OPCODE:
1329 switch (nmb->question.question_type)
1331 case QUESTION_TYPE_NB_QUERY:
1333 if(subrec == wins_server_subnet)
1334 wins_process_name_query_request(subrec, p);
1336 process_name_query_request(subrec, p);
1339 case QUESTION_TYPE_NB_STATUS:
1341 if(subrec == wins_server_subnet)
1343 DEBUG(0,("process_nmb_request: NB_STATUS request directed at WINS server is \
1348 process_node_status_request(subrec, p);
1354 case NMB_NAME_RELEASE_OPCODE:
1355 if(subrec == wins_server_subnet)
1356 wins_process_name_release_request(subrec, p);
1358 process_name_release_request(subrec, p);
1363 /****************************************************************************
1364 Process a nmb response packet - validate the packet and route it.
1365 to either the WINS server or a normal response.
1366 ****************************************************************************/
1368 static void process_nmb_response(struct packet_struct *p)
1370 struct nmb_packet *nmb = &p->packet.nmb;
1371 struct subnet_record *subrec = NULL;
1372 struct response_record *rrec = NULL;
1374 debug_nmb_packet(p);
1376 if(validate_nmb_response_packet(nmb))
1379 if((subrec = find_subnet_for_nmb_packet(p, &rrec))==NULL)
1384 DEBUG(0,("process_nmb_response: response packet received but no response record \
1385 found for id = %hu. Ignoring packet.\n", nmb->header.name_trn_id));
1389 /* Increment the number of responses received for this record. */
1391 /* Ensure we don't re-send the request. */
1392 rrec->repeat_count = 0;
1394 /* Call the response received function for this packet. */
1395 (*rrec->resp_fn)(subrec, rrec, p);
1399 /*******************************************************************
1400 Run elements off the packet queue till its empty
1401 ******************************************************************/
1403 void run_packet_queue()
1405 struct packet_struct *p;
1407 while ((p = packet_queue))
1409 packet_queue = p->next;
1411 packet_queue->prev = NULL;
1412 p->next = p->prev = NULL;
1414 switch (p->packet_type)
1417 if(p->packet.nmb.header.response)
1418 process_nmb_response(p);
1420 process_nmb_request(p);
1431 /*******************************************************************
1432 Retransmit or timeout elements from all the outgoing subnet response
1434 ******************************************************************/
1436 void retransmit_or_expire_response_records(time_t t)
1438 struct subnet_record *subrec;
1440 for (subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_INCLUDING_UNICAST(subrec))
1442 struct response_record *rrec, *nextrrec;
1444 for (rrec = subrec->responselist; rrec; rrec = nextrrec)
1446 nextrrec = rrec->next;
1448 if (rrec->repeat_time <= t)
1450 if (rrec->repeat_count > 0)
1452 /* Resend while we have a non-zero repeat_count. */
1453 if(!send_packet(rrec->packet))
1455 DEBUG(0,("retransmit_or_expire_response_records: Failed to resend packet id %hu \
1456 to IP %s on subnet %s\n", rrec->response_id, inet_ntoa(rrec->packet->ip),
1457 subrec->subnet_name));
1459 rrec->repeat_time += rrec->repeat_interval;
1460 rrec->repeat_count--;
1464 DEBUG(4,("retransmit_or_expire_response_records: timeout for packet id %hu to IP %s \
1465 on subnet %s\n", rrec->response_id, inet_ntoa(rrec->packet->ip),
1466 subrec->subnet_name));
1468 /* Call the timeout function. This will deal with removing the
1469 timed out packet. */
1470 if(rrec->timeout_fn)
1471 (*rrec->timeout_fn)(subrec, rrec);
1474 /* We must remove the record ourself if there is
1475 no timeout function. */
1476 remove_response_record(subrec, rrec);
1478 } /* rrec->repeat_count > 0 */
1479 } /* rrec->repeat_time <= t */
1480 } /* end for rrec */
1481 } /* end for subnet */
1484 /****************************************************************************
1485 Create an fd_set containing all the sockets in the subnet structures,
1486 plus the broadcast sockets.
1487 ***************************************************************************/
1489 static BOOL create_listen_fdset(fd_set **ppset, int **psock_array, int *listen_number)
1491 int *sock_array = NULL;
1492 struct subnet_record *subrec = NULL;
1495 fd_set *pset = (fd_set *)malloc(sizeof(fd_set));
1499 DEBUG(0,("create_listen_fdset: malloc fail !\n"));
1503 /* Check that we can add all the fd's we need. */
1504 for (subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec))
1507 if((count*2) + 2 > FD_SETSIZE)
1509 DEBUG(0,("create_listen_fdset: Too many file descriptors needed (%d). We can \
1510 only use %d.\n", (count*2) + 2, FD_SETSIZE));
1514 if((sock_array = (int *)malloc(((count*2) + 2)*sizeof(int))) == NULL)
1516 DEBUG(0,("create_listen_fdset: malloc fail for socket array.\n"));
1522 /* Add in the broadcast socket on 137. */
1523 FD_SET(ClientNMB,pset);
1524 sock_array[num++] = ClientNMB;
1526 /* Add in the 137 sockets on all the interfaces. */
1527 for (subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec))
1529 FD_SET(subrec->nmb_sock,pset);
1530 sock_array[num++] = subrec->nmb_sock;
1533 /* Add in the broadcast socket on 138. */
1534 FD_SET(ClientDGRAM,pset);
1535 sock_array[num++] = ClientDGRAM;
1537 /* Add in the 138 sockets on all the interfaces. */
1538 for (subrec = FIRST_SUBNET; subrec; subrec = NEXT_SUBNET_EXCLUDING_UNICAST(subrec))
1540 FD_SET(subrec->dgram_sock,pset);
1541 sock_array[num++] = subrec->dgram_sock;
1544 *listen_number = (count*2) + 2;
1546 *psock_array = sock_array;
1551 /****************************************************************************
1552 Listens for NMB or DGRAM packets, and queues them.
1553 ***************************************************************************/
1555 BOOL listen_for_packets(BOOL run_election)
1557 static fd_set *listen_set = NULL;
1558 static int listen_number = 0;
1559 static int *sock_array = NULL;
1563 struct timeval timeout;
1568 if(listen_set == NULL)
1570 if(create_listen_fdset(&listen_set, &sock_array, &listen_number))
1572 DEBUG(0,("listen_for_packets: Fatal error. unable to create listen set. Exiting.\n"));
1577 memcpy((char *)&fds, (char *)listen_set, sizeof(fd_set));
1580 dns_fd = asyncdns_fd();
1582 FD_SET(dns_fd, &fds);
1588 * During elections and when expecting a netbios response packet we
1589 * need to send election packets at tighter intervals.
1590 * Ideally it needs to be the interval (in ms) between time now and
1591 * the time we are expecting the next netbios packet.
1594 timeout.tv_sec = (run_election||num_response_packets) ? 1 : NMBD_SELECT_LOOP;
1595 timeout.tv_usec = 0;
1597 /* We can only take term signals when we are in the select. */
1598 BlockSignals(False, SIGTERM);
1599 selrtn = sys_select(&fds,&timeout);
1600 BlockSignals(True, SIGTERM);
1607 if (dns_fd != -1 && FD_ISSET(dns_fd,&fds)) {
1612 for(i = 0; i < listen_number; i++)
1614 if(i < (listen_number/2))
1616 /* Processing a 137 socket. */
1617 if (FD_ISSET(sock_array[i],&fds))
1619 struct packet_struct *packet = read_packet(sock_array[i], NMB_PACKET);
1623 * If we got a packet on the broadcast socket and interfaces
1624 * only is set then check it came from one of our local nets.
1626 if(lp_bind_interfaces_only() && (sock_array[i] == ClientNMB) &&
1627 (!is_local_net(packet->ip)))
1629 DEBUG(7,("discarding nmb packet sent to broadcast socket from %s:%d\n",
1630 inet_ntoa(packet->ip),packet->port));
1631 free_packet(packet);
1633 else if ((ip_equal(loopback_ip, packet->ip) ||
1634 ismyip(packet->ip)) && packet->port == global_nmb_port)
1636 DEBUG(7,("discarding own packet from %s:%d\n",
1637 inet_ntoa(packet->ip),packet->port));
1638 free_packet(packet);
1642 /* Save the file descriptor this packet came in on. */
1643 packet->fd = sock_array[i];
1644 queue_packet(packet);
1651 /* Processing a 138 socket. */
1653 if (FD_ISSET(sock_array[i],&fds))
1655 struct packet_struct *packet = read_packet(sock_array[i], DGRAM_PACKET);
1659 * If we got a packet on the broadcast socket and interfaces
1660 * only is set then check it came from one of our local nets.
1662 if(lp_bind_interfaces_only() && (sock_array[i] == ClientDGRAM) &&
1663 (!is_local_net(packet->ip)))
1665 DEBUG(7,("discarding dgram packet sent to broadcast socket from %s:%d\n",
1666 inet_ntoa(packet->ip),packet->port));
1667 free_packet(packet);
1669 else if ((ip_equal(loopback_ip, packet->ip) ||
1670 ismyip(packet->ip)) && packet->port == DGRAM_PORT)
1672 DEBUG(7,("discarding own packet from %s:%d\n",
1673 inet_ntoa(packet->ip),packet->port));
1674 free_packet(packet);
1678 /* Save the file descriptor this packet came in on. */
1679 packet->fd = sock_array[i];
1680 queue_packet(packet);
1684 } /* end processing 138 socket. */
1686 } /* end if selret > 0 */
1690 /****************************************************************************
1691 Construct and send a netbios DGRAM.
1692 Note that this currently sends all packets to port 138.
1693 **************************************************************************/
1695 BOOL send_mailslot(BOOL unique, char *mailslot,char *buf,int len,
1696 char *srcname, int src_type,
1697 char *dstname, int dest_type,
1698 struct in_addr dest_ip,struct in_addr src_ip)
1700 BOOL loopback_this_packet = False;
1701 struct packet_struct p;
1702 struct dgram_packet *dgram = &p.packet.dgram;
1706 bzero((char *)&p,sizeof(p));
1709 loopback_this_packet = True;
1711 generate_name_trn_id();
1713 /* DIRECT GROUP or UNIQUE datagram. */
1714 dgram->header.msg_type = unique ? 0x10 : 0x11;
1715 dgram->header.flags.node_type = M_NODE;
1716 dgram->header.flags.first = True;
1717 dgram->header.flags.more = False;
1718 dgram->header.dgm_id = name_trn_id;
1719 dgram->header.source_ip = src_ip;
1720 dgram->header.source_port = DGRAM_PORT;
1721 dgram->header.dgm_length = 0; /* Let build_dgram() handle this. */
1722 dgram->header.packet_offset = 0;
1724 make_nmb_name(&dgram->source_name,srcname,0,scope);
1725 make_nmb_name(&dgram->dest_name,dstname,dest_type,scope);
1727 ptr = &dgram->data[0];
1729 /* Setup the smb part. */
1730 ptr -= 4; /* XXX Ugliness because of handling of tcp SMB length. */
1732 set_message(ptr,17,17 + len,True);
1735 CVAL(ptr,smb_com) = SMBtrans;
1736 SSVAL(ptr,smb_vwv1,len);
1737 SSVAL(ptr,smb_vwv11,len);
1738 SSVAL(ptr,smb_vwv12,70 + strlen(mailslot));
1739 SSVAL(ptr,smb_vwv13,3);
1740 SSVAL(ptr,smb_vwv14,1);
1741 SSVAL(ptr,smb_vwv15,1);
1742 SSVAL(ptr,smb_vwv16,2);
1744 strcpy(p2,mailslot);
1745 p2 = skip_string(p2,1);
1750 dgram->datasize = PTR_DIFF(p2,ptr+4); /* +4 for tcp length. */
1753 p.port = DGRAM_PORT;
1755 p.timestamp = time(NULL);
1756 p.packet_type = DGRAM_PACKET;
1758 DEBUG(4,("send_mailslot: Sending to mailslot %s from %s IP %s ", mailslot,
1759 namestr(&dgram->source_name), inet_ntoa(src_ip)));
1760 DEBUG(4,("to %s IP %s\n", namestr(&dgram->dest_name), inet_ntoa(dest_ip)));
1762 debug_browse_data(buf, len);
1764 if(loopback_this_packet)
1766 struct packet_struct *lo_packet = NULL;
1767 DEBUG(5,("send_mailslot: sending packet to ourselves.\n"));
1768 if((lo_packet = copy_packet(&p)) == NULL)
1770 queue_packet(lo_packet);
1774 return(send_packet(&p));