2 Unix SMB/CIFS implementation.
3 NBT netbios routines and daemon - version 2
4 Copyright (C) Andrew Tridgell 1994-1998
5 Copyright (C) Luke Kenneth Casson Leighton 1994-1998
6 Copyright (C) Jeremy Allison 1994-2003
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "nmbd/nmbd.h"
24 #include "../lib/util/select.h"
25 #include "system/select.h"
26 #include "libsmb/libsmb.h"
27 #include "libsmb/unexpected.h"
28 #include "lib/util/string_wrappers.h"
31 extern int ClientDGRAM
;
32 extern int global_nmb_port
;
34 extern int num_response_packets
;
36 bool rescan_listen_set
= False
;
38 static struct nb_packet_server
*packet_server
;
40 bool nmbd_init_packet_server(void)
44 status
= nb_packet_server_create(
45 NULL
, nmbd_event_context(),
46 lp_parm_int(-1, "nmbd", "unexpected_clients", 200),
48 if (!NT_STATUS_IS_OK(status
)) {
49 DEBUG(0, ("ERROR: nb_packet_server_create failed: %s\n",
57 /*******************************************************************
58 The global packet linked-list. Incoming entries are
59 added to the end of this list. It is supposed to remain fairly
60 short so we won't bother with an end pointer.
61 ******************************************************************/
63 static struct packet_struct
*packet_queue
= NULL
;
65 /***************************************************************************
66 Utility function to find the specific fd to send a packet out on.
67 **************************************************************************/
69 static int find_subnet_fd_for_address( struct in_addr local_ip
)
71 struct subnet_record
*subrec
;
73 for( subrec
= FIRST_SUBNET
; subrec
; subrec
= NEXT_SUBNET_EXCLUDING_UNICAST(subrec
))
74 if(ip_equal_v4(local_ip
, subrec
->myip
))
75 return subrec
->nmb_sock
;
80 /***************************************************************************
81 Utility function to find the specific fd to send a mailslot packet out on.
82 **************************************************************************/
84 static int find_subnet_mailslot_fd_for_address( struct in_addr local_ip
)
86 struct subnet_record
*subrec
;
88 for( subrec
= FIRST_SUBNET
; subrec
; subrec
= NEXT_SUBNET_EXCLUDING_UNICAST(subrec
))
89 if(ip_equal_v4(local_ip
, subrec
->myip
))
90 return subrec
->dgram_sock
;
95 /***************************************************************************
96 Get/Set problematic nb_flags as network byte order 16 bit int.
97 **************************************************************************/
99 uint16_t get_nb_flags(char *buf
)
101 return ((((uint16_t)*buf
)&0xFFFF) & NB_FLGMSK
);
104 void set_nb_flags(char *buf
, uint16_t nb_flags
)
106 *buf
++ = ((nb_flags
& NB_FLGMSK
) & 0xFF);
110 /***************************************************************************
111 Dumps out the browse packet data.
112 **************************************************************************/
114 static void debug_browse_data(const char *outbuf
, int len
)
118 DEBUG( 4, ( "debug_browse_data():\n" ) );
119 for (i
= 0; i
< len
; i
+= 16) {
120 DEBUGADD( 4, ( "%3x char ", i
) );
122 for (j
= 0; j
< 16; j
++) {
128 if (x
< 32 || x
> 127)
131 DEBUGADD( 4, ( "%c", x
) );
134 DEBUGADD( 4, ( "%*s hex", 16-j
, "" ) );
136 for (j
= 0; j
< 16; j
++) {
139 DEBUGADD( 4, ( " %02x", (unsigned char)outbuf
[i
+j
] ) );
142 DEBUGADD( 4, ("\n") );
146 /***************************************************************************
147 Generates the unique transaction identifier
148 **************************************************************************/
150 static uint16_t name_trn_id
=0;
152 static uint16_t generate_name_trn_id(void)
155 name_trn_id
= ((unsigned)time(NULL
)%(unsigned)0x7FFF) + ((unsigned)getpid()%(unsigned)100);
157 name_trn_id
= (name_trn_id
+1) % (unsigned)0x7FFF;
161 /***************************************************************************
162 Either loops back or sends out a completed NetBIOS packet.
163 **************************************************************************/
165 static bool send_netbios_packet(struct packet_struct
*p
)
167 bool loopback_this_packet
= False
;
169 /* Check if we are sending to or from ourselves as a WINS server. */
170 if(ismyip_v4(p
->ip
) && (p
->port
== global_nmb_port
))
171 loopback_this_packet
= True
;
173 if(loopback_this_packet
) {
174 struct packet_struct
*lo_packet
= NULL
;
175 DEBUG(5,("send_netbios_packet: sending packet to ourselves.\n"));
176 if((lo_packet
= copy_packet(p
)) == NULL
)
178 queue_packet(lo_packet
);
179 } else if (!send_packet(p
)) {
180 DEBUG(0,("send_netbios_packet: send_packet() to IP %s port %d failed\n",
181 inet_ntoa(p
->ip
),p
->port
));
188 /***************************************************************************
189 Sets up the common elements of an outgoing NetBIOS packet.
191 Note: do not attempt to rationalise whether rec_des should be set or not
192 in a particular situation. Just follow rfc_1002 or look at examples from WinXX.
193 It does NOT follow the rule that requests to the wins server always have
194 rec_des true. See for example name releases and refreshes
195 **************************************************************************/
197 static struct packet_struct
*create_and_init_netbios_packet(struct nmb_name
*nmbname
,
198 bool bcast
, bool rec_des
,
199 struct in_addr to_ip
)
201 struct packet_struct
*packet
= NULL
;
202 struct nmb_packet
*nmb
= NULL
;
204 /* Allocate the packet_struct we will return. */
205 if((packet
= SMB_MALLOC_P(struct packet_struct
)) == NULL
) {
206 DEBUG(0,("create_and_init_netbios_packet: malloc fail (1) for packet struct.\n"));
210 memset((char *)packet
,'\0',sizeof(*packet
));
212 nmb
= &packet
->packet
.nmb
;
214 nmb
->header
.name_trn_id
= generate_name_trn_id();
215 nmb
->header
.response
= False
;
216 nmb
->header
.nm_flags
.recursion_desired
= rec_des
;
217 nmb
->header
.nm_flags
.recursion_available
= False
;
218 nmb
->header
.nm_flags
.trunc
= False
;
219 nmb
->header
.nm_flags
.authoritative
= False
;
220 nmb
->header
.nm_flags
.bcast
= bcast
;
222 nmb
->header
.rcode
= 0;
223 nmb
->header
.qdcount
= 1;
224 nmb
->header
.ancount
= 0;
225 nmb
->header
.nscount
= 0;
227 nmb
->question
.question_name
= *nmbname
;
228 nmb
->question
.question_type
= QUESTION_TYPE_NB_QUERY
;
229 nmb
->question
.question_class
= QUESTION_CLASS_IN
;
232 packet
->port
= NMB_PORT
;
233 packet
->recv_fd
= -1;
234 packet
->send_fd
= ClientNMB
;
235 packet
->timestamp
= time(NULL
);
236 packet
->packet_type
= NMB_PACKET
;
237 packet
->locked
= False
;
239 return packet
; /* Caller must free. */
242 /***************************************************************************
243 Sets up the common elements of register, refresh or release packet.
244 **************************************************************************/
246 static bool create_and_init_additional_record(struct packet_struct
*packet
,
248 const struct in_addr
*register_ip
)
250 struct nmb_packet
*nmb
= &packet
->packet
.nmb
;
252 if((nmb
->additional
= SMB_MALLOC_P(struct res_rec
)) == NULL
) {
253 DEBUG(0,("create_and_init_additional_record: malloc fail for additional record.\n"));
257 memset((char *)nmb
->additional
,'\0',sizeof(struct res_rec
));
259 nmb
->additional
->rr_name
= nmb
->question
.question_name
;
260 nmb
->additional
->rr_type
= RR_TYPE_NB
;
261 nmb
->additional
->rr_class
= RR_CLASS_IN
;
263 /* See RFC 1002, sections 5.1.1.1, 5.1.1.2 and 5.1.1.3 */
264 if (nmb
->header
.nm_flags
.bcast
)
265 nmb
->additional
->ttl
= PERMANENT_TTL
;
267 nmb
->additional
->ttl
= lp_max_ttl();
269 nmb
->additional
->rdlength
= 6;
271 set_nb_flags(nmb
->additional
->rdata
,nb_flags
);
273 /* Set the address for the name we are registering. */
274 putip(&nmb
->additional
->rdata
[2], register_ip
);
277 it turns out that Jeremys code was correct, we are supposed
278 to send registrations from the IP we are registering. The
279 trick is what to do on timeouts! When we send on a
280 non-routable IP then the reply will timeout, and we should
281 treat this as success, not failure. That means we go into
282 our standard refresh cycle for that name which copes nicely
283 with disconnected networks.
285 packet
->recv_fd
= -1;
286 packet
->send_fd
= find_subnet_fd_for_address(*register_ip
);
291 /***************************************************************************
292 Sends out a name query.
293 **************************************************************************/
295 static bool initiate_name_query_packet( struct packet_struct
*packet
)
297 struct nmb_packet
*nmb
= NULL
;
299 nmb
= &packet
->packet
.nmb
;
301 nmb
->header
.opcode
= NMB_NAME_QUERY_OPCODE
;
302 nmb
->header
.arcount
= 0;
304 nmb
->header
.nm_flags
.recursion_desired
= True
;
306 DEBUG(4,("initiate_name_query_packet: sending query for name %s (bcast=%s) to IP %s\n",
307 nmb_namestr(&nmb
->question
.question_name
),
308 BOOLSTR(nmb
->header
.nm_flags
.bcast
), inet_ntoa(packet
->ip
)));
310 return send_netbios_packet( packet
);
313 /***************************************************************************
314 Sends out a name query - from a WINS server.
315 **************************************************************************/
317 static bool initiate_name_query_packet_from_wins_server( struct packet_struct
*packet
)
319 struct nmb_packet
*nmb
= NULL
;
321 nmb
= &packet
->packet
.nmb
;
323 nmb
->header
.opcode
= NMB_NAME_QUERY_OPCODE
;
324 nmb
->header
.arcount
= 0;
326 nmb
->header
.nm_flags
.recursion_desired
= False
;
328 DEBUG(4,("initiate_name_query_packet_from_wins_server: sending query for name %s (bcast=%s) to IP %s\n",
329 nmb_namestr(&nmb
->question
.question_name
),
330 BOOLSTR(nmb
->header
.nm_flags
.bcast
), inet_ntoa(packet
->ip
)));
332 return send_netbios_packet( packet
);
335 /***************************************************************************
336 Sends out a name register.
337 **************************************************************************/
339 static bool initiate_name_register_packet( struct packet_struct
*packet
,
340 uint16_t nb_flags
, const struct in_addr
*register_ip
)
342 struct nmb_packet
*nmb
= &packet
->packet
.nmb
;
344 nmb
->header
.opcode
= NMB_NAME_REG_OPCODE
;
345 nmb
->header
.arcount
= 1;
347 nmb
->header
.nm_flags
.recursion_desired
= True
;
349 if(create_and_init_additional_record(packet
, nb_flags
, register_ip
) == False
)
352 DEBUG(4,("initiate_name_register_packet: sending registration for name %s (bcast=%s) to IP %s\n",
353 nmb_namestr(&nmb
->additional
->rr_name
),
354 BOOLSTR(nmb
->header
.nm_flags
.bcast
), inet_ntoa(packet
->ip
)));
356 return send_netbios_packet( packet
);
359 /***************************************************************************
360 Sends out a multihomed name register.
361 **************************************************************************/
363 static bool initiate_multihomed_name_register_packet(struct packet_struct
*packet
,
364 uint16_t nb_flags
, struct in_addr
*register_ip
)
366 struct nmb_packet
*nmb
= &packet
->packet
.nmb
;
367 fstring second_ip_buf
;
369 fstrcpy(second_ip_buf
, inet_ntoa(packet
->ip
));
371 nmb
->header
.opcode
= NMB_NAME_MULTIHOMED_REG_OPCODE
;
372 nmb
->header
.arcount
= 1;
374 nmb
->header
.nm_flags
.recursion_desired
= True
;
376 if(create_and_init_additional_record(packet
, nb_flags
, register_ip
) == False
)
379 DEBUG(4,("initiate_multihomed_name_register_packet: sending registration \
380 for name %s IP %s (bcast=%s) to IP %s\n",
381 nmb_namestr(&nmb
->additional
->rr_name
), inet_ntoa(*register_ip
),
382 BOOLSTR(nmb
->header
.nm_flags
.bcast
), second_ip_buf
));
384 return send_netbios_packet( packet
);
387 /***************************************************************************
388 Sends out a name refresh.
389 **************************************************************************/
391 static bool initiate_name_refresh_packet( struct packet_struct
*packet
,
392 uint16_t nb_flags
, struct in_addr
*refresh_ip
)
394 struct nmb_packet
*nmb
= &packet
->packet
.nmb
;
396 nmb
->header
.opcode
= NMB_NAME_REFRESH_OPCODE_8
;
397 nmb
->header
.arcount
= 1;
399 nmb
->header
.nm_flags
.recursion_desired
= False
;
401 if(create_and_init_additional_record(packet
, nb_flags
, refresh_ip
) == False
)
404 DEBUG(4,("initiate_name_refresh_packet: sending refresh for name %s (bcast=%s) to IP %s\n",
405 nmb_namestr(&nmb
->additional
->rr_name
),
406 BOOLSTR(nmb
->header
.nm_flags
.bcast
), inet_ntoa(packet
->ip
)));
408 return send_netbios_packet( packet
);
411 /***************************************************************************
412 Sends out a name release.
413 **************************************************************************/
415 static bool initiate_name_release_packet( struct packet_struct
*packet
,
416 uint16_t nb_flags
, struct in_addr
*release_ip
)
418 struct nmb_packet
*nmb
= &packet
->packet
.nmb
;
420 nmb
->header
.opcode
= NMB_NAME_RELEASE_OPCODE
;
421 nmb
->header
.arcount
= 1;
423 nmb
->header
.nm_flags
.recursion_desired
= False
;
425 if(create_and_init_additional_record(packet
, nb_flags
, release_ip
) == False
)
428 DEBUG(4,("initiate_name_release_packet: sending release for name %s (bcast=%s) to IP %s\n",
429 nmb_namestr(&nmb
->additional
->rr_name
),
430 BOOLSTR(nmb
->header
.nm_flags
.bcast
), inet_ntoa(packet
->ip
)));
432 return send_netbios_packet( packet
);
435 /***************************************************************************
436 Sends out a node status.
437 **************************************************************************/
439 static bool initiate_node_status_packet( struct packet_struct
*packet
)
441 struct nmb_packet
*nmb
= &packet
->packet
.nmb
;
443 nmb
->header
.opcode
= NMB_NAME_QUERY_OPCODE
;
444 nmb
->header
.arcount
= 0;
446 nmb
->header
.nm_flags
.recursion_desired
= False
;
448 nmb
->question
.question_type
= QUESTION_TYPE_NB_STATUS
;
450 DEBUG(4,("initiate_node_status_packet: sending node status request for name %s to IP %s\n",
451 nmb_namestr(&nmb
->question
.question_name
),
452 inet_ntoa(packet
->ip
)));
454 return send_netbios_packet( packet
);
457 /****************************************************************************
458 Simplification functions for queuing standard packets.
459 These should be the only publicly callable functions for sending
461 ****************************************************************************/
463 /****************************************************************************
464 Assertion - we should never be sending nmbd packets on the remote
466 ****************************************************************************/
468 static bool assert_check_subnet(struct subnet_record
*subrec
)
470 if( subrec
== remote_broadcast_subnet
) {
471 DEBUG(0,("assert_check_subnet: Attempt to send packet on remote broadcast subnet. \
478 /****************************************************************************
479 Queue a register name packet to the broadcast address of a subnet.
480 ****************************************************************************/
482 struct response_record
*queue_register_name( struct subnet_record
*subrec
,
483 response_function resp_fn
,
484 timeout_response_function timeout_fn
,
485 register_name_success_function success_fn
,
486 register_name_fail_function fail_fn
,
487 struct userdata_struct
*userdata
,
488 struct nmb_name
*nmbname
,
491 struct packet_struct
*p
;
492 struct response_record
*rrec
;
493 struct sockaddr_storage ss
;
494 const struct sockaddr_storage
*pss
= NULL
;
495 if(assert_check_subnet(subrec
))
498 /* note that all name registration requests have RD set (rfc1002 - section 4.2.2 */
499 if ((p
= create_and_init_netbios_packet(nmbname
, (subrec
!= unicast_subnet
), True
,
500 subrec
->bcast_ip
)) == NULL
)
503 in_addr_to_sockaddr_storage(&ss
, subrec
->bcast_ip
);
504 pss
= iface_ip((struct sockaddr
*)(void *)&ss
);
505 if (!pss
|| pss
->ss_family
!= AF_INET
) {
511 if(initiate_name_register_packet(p
, nb_flags
,
512 &((const struct sockaddr_in
*)pss
)->sin_addr
) == False
) {
518 if((rrec
= make_response_record(subrec
, /* subnet record. */
519 p
, /* packet we sent. */
520 resp_fn
, /* function to call on response. */
521 timeout_fn
, /* function to call on timeout. */
522 (success_function
)success_fn
, /* function to call on operation success. */
523 (fail_function
)fail_fn
, /* function to call on operation fail. */
524 userdata
)) == NULL
) {
533 /****************************************************************************
534 Queue a refresh name packet to the broadcast address of a subnet.
535 ****************************************************************************/
537 void queue_wins_refresh(struct nmb_name
*nmbname
,
538 response_function resp_fn
,
539 timeout_response_function timeout_fn
,
541 struct in_addr refresh_ip
,
544 struct packet_struct
*p
;
545 struct response_record
*rrec
;
546 struct in_addr wins_ip
;
547 struct userdata_struct
*userdata
;
550 wins_ip
= wins_srv_ip_tag(tag
, refresh_ip
);
552 if ((p
= create_and_init_netbios_packet(nmbname
, False
, False
, wins_ip
)) == NULL
) {
556 if (!initiate_name_refresh_packet(p
, nb_flags
, &refresh_ip
)) {
562 fstrcpy(ip_str
, inet_ntoa(refresh_ip
));
564 DEBUG(6,("Refreshing name %s IP %s with WINS server %s using tag '%s'\n",
565 nmb_namestr(nmbname
), ip_str
, inet_ntoa(wins_ip
), tag
));
567 userdata
= (struct userdata_struct
*)SMB_MALLOC(sizeof(*userdata
) + strlen(tag
) + 1);
571 DEBUG(0,("Failed to allocate userdata structure!\n"));
574 ZERO_STRUCTP(userdata
);
575 userdata
->userdata_len
= strlen(tag
) + 1;
576 strlcpy(userdata
->data
, tag
, userdata
->userdata_len
);
578 if ((rrec
= make_response_record(unicast_subnet
,
583 userdata
)) == NULL
) {
591 /* we don't want to repeat refresh packets */
592 rrec
->repeat_count
= 0;
596 /****************************************************************************
597 Queue a multihomed register name packet to a given WINS server IP
598 ****************************************************************************/
600 struct response_record
*queue_register_multihomed_name( struct subnet_record
*subrec
,
601 response_function resp_fn
,
602 timeout_response_function timeout_fn
,
603 register_name_success_function success_fn
,
604 register_name_fail_function fail_fn
,
605 struct userdata_struct
*userdata
,
606 struct nmb_name
*nmbname
,
608 struct in_addr register_ip
,
609 struct in_addr wins_ip
)
611 struct packet_struct
*p
;
612 struct response_record
*rrec
;
616 if(subrec
!= unicast_subnet
) {
617 DEBUG(0,("queue_register_multihomed_name: should only be done on \
618 unicast subnet. subnet is %s\n.", subrec
->subnet_name
));
622 if(assert_check_subnet(subrec
))
625 if ((p
= create_and_init_netbios_packet(nmbname
, False
, True
, wins_ip
)) == NULL
)
628 if (nb_flags
& NB_GROUP
)
629 ret
= initiate_name_register_packet( p
, nb_flags
, ®ister_ip
);
631 ret
= initiate_multihomed_name_register_packet(p
, nb_flags
, ®ister_ip
);
639 if ((rrec
= make_response_record(subrec
, /* subnet record. */
640 p
, /* packet we sent. */
641 resp_fn
, /* function to call on response. */
642 timeout_fn
, /* function to call on timeout. */
643 (success_function
)success_fn
, /* function to call on operation success. */
644 (fail_function
)fail_fn
, /* function to call on operation fail. */
645 userdata
)) == NULL
) {
654 /****************************************************************************
655 Queue a release name packet to the broadcast address of a subnet.
656 ****************************************************************************/
658 struct response_record
*queue_release_name( struct subnet_record
*subrec
,
659 response_function resp_fn
,
660 timeout_response_function timeout_fn
,
661 release_name_success_function success_fn
,
662 release_name_fail_function fail_fn
,
663 struct userdata_struct
*userdata
,
664 struct nmb_name
*nmbname
,
666 struct in_addr release_ip
,
667 struct in_addr dest_ip
)
669 struct packet_struct
*p
;
670 struct response_record
*rrec
;
672 if(assert_check_subnet(subrec
))
675 if ((p
= create_and_init_netbios_packet(nmbname
, (subrec
!= unicast_subnet
), False
, dest_ip
)) == NULL
)
678 if(initiate_name_release_packet( p
, nb_flags
, &release_ip
) == False
) {
684 if((rrec
= make_response_record(subrec
, /* subnet record. */
685 p
, /* packet we sent. */
686 resp_fn
, /* function to call on response. */
687 timeout_fn
, /* function to call on timeout. */
688 (success_function
)success_fn
, /* function to call on operation success. */
689 (fail_function
)fail_fn
, /* function to call on operation fail. */
690 userdata
)) == NULL
) {
697 * For a broadcast release packet, only send once.
698 * This will cause us to remove the name asap. JRA.
701 if (subrec
!= unicast_subnet
) {
702 rrec
->repeat_count
= 0;
703 rrec
->repeat_time
= 0;
709 /****************************************************************************
710 Queue a query name packet to the broadcast address of a subnet.
711 ****************************************************************************/
713 struct response_record
*queue_query_name( struct subnet_record
*subrec
,
714 response_function resp_fn
,
715 timeout_response_function timeout_fn
,
716 query_name_success_function success_fn
,
717 query_name_fail_function fail_fn
,
718 struct userdata_struct
*userdata
,
719 struct nmb_name
*nmbname
)
721 struct packet_struct
*p
;
722 struct response_record
*rrec
;
723 struct in_addr to_ip
;
725 if(assert_check_subnet(subrec
))
728 to_ip
= subrec
->bcast_ip
;
730 /* queries to the WINS server turn up here as queries to IP 0.0.0.0
731 These need to be handled a bit differently */
732 if (subrec
->type
== UNICAST_SUBNET
&& is_zero_ip_v4(to_ip
)) {
733 /* What we really need to do is loop over each of our wins
734 * servers and wins server tags here, but that just doesn't
735 * fit our architecture at the moment (userdata may already
736 * be used when we get here). For now we just query the first
737 * active wins server on the first tag.
739 char **tags
= wins_srv_tags();
743 to_ip
= wins_srv_ip_tag(tags
[0], to_ip
);
744 wins_srv_tags_free(tags
);
747 if(( p
= create_and_init_netbios_packet(nmbname
,
748 (subrec
!= unicast_subnet
),
749 (subrec
== unicast_subnet
),
753 if(lp_bind_interfaces_only()) {
756 DEBUG(10,("queue_query_name: bind_interfaces_only is set, looking for suitable source IP\n"));
757 for(i
= 0; i
< iface_count(); i
++) {
758 const struct in_addr
*ifip
= iface_n_ip_v4(i
);
761 DEBUG(0,("queue_query_name: interface %d has NULL IP address !\n", i
));
765 if (is_loopback_ip_v4(*ifip
)) {
766 DEBUG(5,("queue_query_name: ignoring loopback interface (%d)\n", i
));
770 DEBUG(10,("queue_query_name: using source IP %s\n",inet_ntoa(*ifip
)));
771 p
->send_fd
= find_subnet_fd_for_address( *ifip
);
776 if(initiate_name_query_packet( p
) == False
) {
782 if((rrec
= make_response_record(subrec
, /* subnet record. */
783 p
, /* packet we sent. */
784 resp_fn
, /* function to call on response. */
785 timeout_fn
, /* function to call on timeout. */
786 (success_function
)success_fn
, /* function to call on operation success. */
787 (fail_function
)fail_fn
, /* function to call on operation fail. */
788 userdata
)) == NULL
) {
797 /****************************************************************************
798 Queue a query name packet to a given address from the WINS subnet.
799 ****************************************************************************/
801 struct response_record
*queue_query_name_from_wins_server( struct in_addr to_ip
,
802 response_function resp_fn
,
803 timeout_response_function timeout_fn
,
804 query_name_success_function success_fn
,
805 query_name_fail_function fail_fn
,
806 struct userdata_struct
*userdata
,
807 struct nmb_name
*nmbname
)
809 struct packet_struct
*p
;
810 struct response_record
*rrec
;
812 if ((p
= create_and_init_netbios_packet(nmbname
, False
, False
, to_ip
)) == NULL
)
815 if(initiate_name_query_packet_from_wins_server( p
) == False
) {
821 if((rrec
= make_response_record(wins_server_subnet
, /* subnet record. */
822 p
, /* packet we sent. */
823 resp_fn
, /* function to call on response. */
824 timeout_fn
, /* function to call on timeout. */
825 (success_function
)success_fn
, /* function to call on operation success. */
826 (fail_function
)fail_fn
, /* function to call on operation fail. */
827 userdata
)) == NULL
) {
836 /****************************************************************************
837 Queue a node status packet to a given name and address.
838 ****************************************************************************/
840 struct response_record
*queue_node_status( struct subnet_record
*subrec
,
841 response_function resp_fn
,
842 timeout_response_function timeout_fn
,
843 node_status_success_function success_fn
,
844 node_status_fail_function fail_fn
,
845 struct userdata_struct
*userdata
,
846 struct nmb_name
*nmbname
,
847 struct in_addr send_ip
)
849 struct packet_struct
*p
;
850 struct response_record
*rrec
;
853 if(subrec
!= unicast_subnet
) {
854 DEBUG(0,("queue_register_multihomed_name: should only be done on \
855 unicast subnet. subnet is %s\n.", subrec
->subnet_name
));
859 if(assert_check_subnet(subrec
))
862 if(( p
= create_and_init_netbios_packet(nmbname
, False
, False
, send_ip
)) == NULL
)
865 if(initiate_node_status_packet(p
) == False
) {
871 if((rrec
= make_response_record(subrec
, /* subnet record. */
872 p
, /* packet we sent. */
873 resp_fn
, /* function to call on response. */
874 timeout_fn
, /* function to call on timeout. */
875 (success_function
)success_fn
, /* function to call on operation success. */
876 (fail_function
)fail_fn
, /* function to call on operation fail. */
877 userdata
)) == NULL
) {
886 /****************************************************************************
887 Reply to a netbios name packet. see rfc1002.txt
888 ****************************************************************************/
890 void reply_netbios_packet(struct packet_struct
*orig_packet
,
891 int rcode
, enum netbios_reply_type_code rcv_code
, int opcode
,
892 int ttl
, char *data
,int len
)
894 struct packet_struct packet
;
895 struct nmb_packet
*nmb
= NULL
;
896 struct res_rec answers
;
897 struct nmb_packet
*orig_nmb
= &orig_packet
->packet
.nmb
;
898 bool loopback_this_packet
= False
;
899 int rr_type
= RR_TYPE_NB
;
900 const char *packet_type
= "unknown";
902 /* Check if we are sending to or from ourselves. */
903 if(ismyip_v4(orig_packet
->ip
) && (orig_packet
->port
== global_nmb_port
))
904 loopback_this_packet
= True
;
906 nmb
= &packet
.packet
.nmb
;
908 /* Do a partial copy of the packet. We clear the locked flag and
909 the resource record pointers. */
910 packet
= *orig_packet
; /* Full structure copy. */
911 packet
.locked
= False
;
914 nmb
->additional
= NULL
;
918 packet_type
= "nmb_status";
919 nmb
->header
.nm_flags
.recursion_desired
= False
;
920 nmb
->header
.nm_flags
.recursion_available
= False
;
921 rr_type
= RR_TYPE_NBSTAT
;
924 packet_type
= "nmb_query";
925 nmb
->header
.nm_flags
.recursion_desired
= True
;
926 nmb
->header
.nm_flags
.recursion_available
= True
;
928 rr_type
= RR_TYPE_NULL
;
932 case NMB_REG_REFRESH
:
933 packet_type
= "nmb_reg";
934 nmb
->header
.nm_flags
.recursion_desired
= True
;
935 nmb
->header
.nm_flags
.recursion_available
= True
;
938 packet_type
= "nmb_rel";
939 nmb
->header
.nm_flags
.recursion_desired
= False
;
940 nmb
->header
.nm_flags
.recursion_available
= False
;
943 packet_type
= "nmb_wack";
944 nmb
->header
.nm_flags
.recursion_desired
= False
;
945 nmb
->header
.nm_flags
.recursion_available
= False
;
946 rr_type
= RR_TYPE_NULL
;
949 packet_type
= "wins_reg";
950 nmb
->header
.nm_flags
.recursion_desired
= True
;
951 nmb
->header
.nm_flags
.recursion_available
= True
;
954 packet_type
= "wins_query";
955 nmb
->header
.nm_flags
.recursion_desired
= True
;
956 nmb
->header
.nm_flags
.recursion_available
= True
;
958 rr_type
= RR_TYPE_NULL
;
962 DEBUG(0,("reply_netbios_packet: Unknown packet type: %s %s to ip %s\n",
963 packet_type
, nmb_namestr(&orig_nmb
->question
.question_name
),
964 inet_ntoa(packet
.ip
)));
968 DEBUG(4, ("reply_netbios_packet: sending a reply of packet type: %s "
969 "%s to ip %s for id %d\n", packet_type
,
970 nmb_namestr(&orig_nmb
->question
.question_name
),
971 inet_ntoa(packet
.ip
), orig_nmb
->header
.name_trn_id
));
973 nmb
->header
.name_trn_id
= orig_nmb
->header
.name_trn_id
;
974 nmb
->header
.opcode
= opcode
;
975 nmb
->header
.response
= True
;
976 nmb
->header
.nm_flags
.bcast
= False
;
977 nmb
->header
.nm_flags
.trunc
= False
;
978 nmb
->header
.nm_flags
.authoritative
= True
;
980 nmb
->header
.rcode
= rcode
;
981 nmb
->header
.qdcount
= 0;
982 nmb
->header
.ancount
= 1;
983 nmb
->header
.nscount
= 0;
984 nmb
->header
.arcount
= 0;
986 memset((char*)&nmb
->question
,'\0',sizeof(nmb
->question
));
988 nmb
->answers
= &answers
;
989 memset((char*)nmb
->answers
,'\0',sizeof(*nmb
->answers
));
991 nmb
->answers
->rr_name
= orig_nmb
->question
.question_name
;
992 nmb
->answers
->rr_type
= rr_type
;
993 nmb
->answers
->rr_class
= RR_CLASS_IN
;
994 nmb
->answers
->ttl
= ttl
;
997 if (len
< 0 || len
> sizeof(nmb
->answers
->rdata
)) {
998 DEBUG(5,("reply_netbios_packet: "
999 "invalid packet len (%d)\n",
1003 nmb
->answers
->rdlength
= len
;
1004 memcpy(nmb
->answers
->rdata
, data
, len
);
1007 packet
.packet_type
= NMB_PACKET
;
1008 packet
.recv_fd
= -1;
1009 /* Ensure we send out on the same fd that the original
1010 packet came in on to give the correct source IP address. */
1011 if (orig_packet
->send_fd
!= -1) {
1012 packet
.send_fd
= orig_packet
->send_fd
;
1014 packet
.send_fd
= orig_packet
->recv_fd
;
1016 packet
.timestamp
= time(NULL
);
1018 debug_nmb_packet(&packet
);
1020 if(loopback_this_packet
) {
1021 struct packet_struct
*lo_packet
;
1022 DEBUG(5,("reply_netbios_packet: sending packet to ourselves.\n"));
1023 if((lo_packet
= copy_packet(&packet
)) == NULL
)
1025 queue_packet(lo_packet
);
1026 } else if (!send_packet(&packet
)) {
1027 DEBUG(0,("reply_netbios_packet: send_packet to IP %s port %d failed\n",
1028 inet_ntoa(packet
.ip
),packet
.port
));
1032 /*******************************************************************
1033 Queue a packet into a packet queue
1034 ******************************************************************/
1036 void queue_packet(struct packet_struct
*packet
)
1038 DLIST_ADD_END(packet_queue
, packet
);
1041 /****************************************************************************
1042 Try and find a matching subnet record for a datagram port 138 packet.
1043 ****************************************************************************/
1045 static struct subnet_record
*find_subnet_for_dgram_browse_packet(struct packet_struct
*p
)
1047 struct subnet_record
*subrec
;
1049 /* Go through all the broadcast subnets and see if the mask matches. */
1050 for (subrec
= FIRST_SUBNET
; subrec
; subrec
= NEXT_SUBNET_EXCLUDING_UNICAST(subrec
)) {
1051 if(same_net_v4(p
->ip
, subrec
->bcast_ip
, subrec
->mask_ip
))
1055 /* If the subnet record is the remote announce broadcast subnet,
1056 hack it here to be the first subnet. This is really gross and
1057 is needed due to people turning on port 137/138 broadcast
1058 forwarding on their routers. May fire and brimstone rain
1062 return FIRST_SUBNET
;
1065 /****************************************************************************
1066 Dispatch a browse frame from port 138 to the correct processing function.
1067 ****************************************************************************/
1069 static void process_browse_packet(struct packet_struct
*p
, const char *buf
,int len
)
1071 struct dgram_packet
*dgram
= &p
->packet
.dgram
;
1072 int command
= CVAL(buf
,0);
1073 struct subnet_record
*subrec
= find_subnet_for_dgram_browse_packet(p
);
1077 /* Drop the packet if it's a different NetBIOS scope, or the source is from one of our names. */
1078 pull_ascii(scope
, dgram
->dest_name
.scope
, 64, 64, STR_TERMINATE
);
1079 if (!strequal(scope
, lp_netbios_scope())) {
1080 DEBUG(7,("process_browse_packet: Discarding datagram from IP %s. Scope (%s) \
1081 mismatch with our scope (%s).\n", inet_ntoa(p
->ip
), scope
, lp_netbios_scope()));
1085 pull_ascii_nstring(src_name
, sizeof(src_name
), dgram
->source_name
.name
);
1086 if (is_myname(src_name
)) {
1087 DEBUG(7,("process_browse_packet: Discarding datagram from IP %s. Source name \
1088 %s is one of our names !\n", inet_ntoa(p
->ip
), nmb_namestr(&dgram
->source_name
)));
1093 case ANN_HostAnnouncement
:
1094 debug_browse_data(buf
, len
);
1095 process_host_announce(subrec
, p
, buf
+1);
1097 case ANN_DomainAnnouncement
:
1098 debug_browse_data(buf
, len
);
1099 process_workgroup_announce(subrec
, p
, buf
+1);
1101 case ANN_LocalMasterAnnouncement
:
1102 debug_browse_data(buf
, len
);
1103 process_local_master_announce(subrec
, p
, buf
+1);
1105 case ANN_AnnouncementRequest
:
1106 debug_browse_data(buf
, len
);
1107 process_announce_request(subrec
, p
, buf
+1);
1110 debug_browse_data(buf
, len
);
1111 process_election(subrec
, p
, buf
+1);
1113 case ANN_GetBackupListReq
:
1114 debug_browse_data(buf
, len
);
1115 process_get_backup_list_request(subrec
, p
, buf
+1);
1117 case ANN_GetBackupListResp
:
1118 debug_browse_data(buf
, len
);
1119 /* We never send ANN_GetBackupListReq so we should never get these. */
1120 DEBUG(0,("process_browse_packet: Discarding GetBackupListResponse \
1121 packet from %s IP %s\n", nmb_namestr(&dgram
->source_name
), inet_ntoa(p
->ip
)));
1123 case ANN_ResetBrowserState
:
1124 debug_browse_data(buf
, len
);
1125 process_reset_browser(subrec
, p
, buf
+1);
1127 case ANN_MasterAnnouncement
:
1128 /* Master browser datagrams must be processed on the unicast subnet. */
1129 subrec
= unicast_subnet
;
1131 debug_browse_data(buf
, len
);
1132 process_master_browser_announce(subrec
, p
, buf
+1);
1134 case ANN_BecomeBackup
:
1136 * We don't currently implement this. Log it just in case.
1138 debug_browse_data(buf
, len
);
1139 DEBUG(10,("process_browse_packet: On subnet %s ignoring browse packet \
1140 command ANN_BecomeBackup from %s IP %s to %s\n", subrec
->subnet_name
, nmb_namestr(&dgram
->source_name
),
1141 inet_ntoa(p
->ip
), nmb_namestr(&dgram
->dest_name
)));
1144 debug_browse_data(buf
, len
);
1145 DEBUG(0,("process_browse_packet: On subnet %s ignoring browse packet \
1146 command code %d from %s IP %s to %s\n", subrec
->subnet_name
, command
, nmb_namestr(&dgram
->source_name
),
1147 inet_ntoa(p
->ip
), nmb_namestr(&dgram
->dest_name
)));
1152 /****************************************************************************
1153 Dispatch a LanMan browse frame from port 138 to the correct processing function.
1154 ****************************************************************************/
1156 static void process_lanman_packet(struct packet_struct
*p
, const char *buf
,int len
)
1158 struct dgram_packet
*dgram
= &p
->packet
.dgram
;
1159 int command
= SVAL(buf
,0);
1160 struct subnet_record
*subrec
= find_subnet_for_dgram_browse_packet(p
);
1164 /* Drop the packet if it's a different NetBIOS scope, or the source is from one of our names. */
1166 pull_ascii(scope
, dgram
->dest_name
.scope
, 64, 64, STR_TERMINATE
);
1167 if (!strequal(scope
, lp_netbios_scope())) {
1168 DEBUG(7,("process_lanman_packet: Discarding datagram from IP %s. Scope (%s) \
1169 mismatch with our scope (%s).\n", inet_ntoa(p
->ip
), scope
, lp_netbios_scope()));
1173 pull_ascii_nstring(src_name
, sizeof(src_name
), dgram
->source_name
.name
);
1174 if (is_myname(src_name
)) {
1175 DEBUG(0,("process_lanman_packet: Discarding datagram from IP %s. Source name \
1176 %s is one of our names !\n", inet_ntoa(p
->ip
), nmb_namestr(&dgram
->source_name
)));
1181 case ANN_HostAnnouncement
:
1182 debug_browse_data(buf
, len
);
1183 process_lm_host_announce(subrec
, p
, buf
+1, len
> 1 ? len
-1 : 0);
1185 case ANN_AnnouncementRequest
:
1186 process_lm_announce_request(subrec
, p
, buf
+1, len
> 1 ? len
-1 : 0);
1189 DEBUG(0,("process_lanman_packet: On subnet %s ignoring browse packet \
1190 command code %d from %s IP %s to %s\n", subrec
->subnet_name
, command
, nmb_namestr(&dgram
->source_name
),
1191 inet_ntoa(p
->ip
), nmb_namestr(&dgram
->dest_name
)));
1196 /****************************************************************************
1197 Determine if a packet is for us on port 138. Note that to have any chance of
1198 being efficient we need to drop as many packets as possible at this
1199 stage as subsequent processing is expensive.
1200 ****************************************************************************/
1202 static bool listening(struct packet_struct
*p
,struct nmb_name
*nbname
)
1204 struct subnet_record
*subrec
= NULL
;
1206 for (subrec
= FIRST_SUBNET
; subrec
; subrec
= NEXT_SUBNET_EXCLUDING_UNICAST(subrec
)) {
1207 if(same_net_v4(p
->ip
, subrec
->bcast_ip
, subrec
->mask_ip
))
1212 subrec
= unicast_subnet
;
1214 return (find_name_on_subnet(subrec
, nbname
, FIND_SELF_NAME
) != NULL
);
1217 /****************************************************************************
1218 Process udp 138 datagrams
1219 ****************************************************************************/
1221 static void process_dgram(struct packet_struct
*p
)
1226 struct dgram_packet
*dgram
= &p
->packet
.dgram
;
1228 /* If we aren't listening to the destination name then ignore the packet */
1229 if (!listening(p
,&dgram
->dest_name
)) {
1230 nb_packet_dispatch(packet_server
, p
);
1231 DEBUG(5,("process_dgram: ignoring dgram packet sent to name %s from %s\n",
1232 nmb_namestr(&dgram
->dest_name
), inet_ntoa(p
->ip
)));
1236 if (dgram
->header
.msg_type
!= 0x10 && dgram
->header
.msg_type
!= 0x11 && dgram
->header
.msg_type
!= 0x12) {
1237 nb_packet_dispatch(packet_server
, p
);
1238 /* Don't process error packets etc yet */
1239 DEBUG(5,("process_dgram: ignoring dgram packet sent to name %s from IP %s as it is \
1240 an error packet of type %x\n", nmb_namestr(&dgram
->dest_name
), inet_ntoa(p
->ip
), dgram
->header
.msg_type
));
1244 /* Ensure we have a large enough packet before looking inside. */
1245 if (dgram
->datasize
< (smb_vwv12
- 2)) {
1246 /* That's the offset minus the 4 byte length + 2 bytes of offset. */
1247 DEBUG(0,("process_dgram: ignoring too short dgram packet (%u) sent to name %s from IP %s\n",
1248 (unsigned int)dgram
->datasize
,
1249 nmb_namestr(&dgram
->dest_name
),
1250 inet_ntoa(p
->ip
) ));
1254 buf
= &dgram
->data
[0];
1255 buf
-= 4; /* XXXX for the pseudo tcp length - someday I need to get rid of this */
1257 if (CVAL(buf
,smb_com
) != SMBtrans
)
1260 len
= SVAL(buf
,smb_vwv11
);
1261 buf2
= smb_base(buf
) + SVAL(buf
,smb_vwv12
);
1263 if (len
<= 0 || len
> dgram
->datasize
) {
1264 DEBUG(0,("process_dgram: ignoring malformed1 (datasize = %d, len = %d) datagram \
1265 packet sent to name %s from IP %s\n",
1268 nmb_namestr(&dgram
->dest_name
),
1269 inet_ntoa(p
->ip
) ));
1273 if (buf2
< dgram
->data
|| (buf2
>= dgram
->data
+ dgram
->datasize
)) {
1274 DEBUG(0,("process_dgram: ignoring malformed2 (datasize = %d, len=%d, off=%d) datagram \
1275 packet sent to name %s from IP %s\n",
1278 (int)PTR_DIFF(buf2
, dgram
->data
),
1279 nmb_namestr(&dgram
->dest_name
),
1280 inet_ntoa(p
->ip
) ));
1284 if ((buf2
+ len
< dgram
->data
) || (buf2
+ len
> dgram
->data
+ dgram
->datasize
)) {
1285 DEBUG(0,("process_dgram: ignoring malformed3 (datasize = %d, len=%d, off=%d) datagram \
1286 packet sent to name %s from IP %s\n",
1289 (int)PTR_DIFF(buf2
, dgram
->data
),
1290 nmb_namestr(&dgram
->dest_name
),
1291 inet_ntoa(p
->ip
) ));
1295 DEBUG(4,("process_dgram: datagram from %s to %s IP %s for %s of type %d len=%d\n",
1296 nmb_namestr(&dgram
->source_name
),nmb_namestr(&dgram
->dest_name
),
1297 inet_ntoa(p
->ip
), smb_buf_const(buf
),CVAL(buf2
,0),len
));
1299 /* Datagram packet received for the browser mailslot */
1300 if (strequal(smb_buf_const(buf
),BROWSE_MAILSLOT
)) {
1301 process_browse_packet(p
,buf2
,len
);
1305 /* Datagram packet received for the LAN Manager mailslot */
1306 if (strequal(smb_buf_const(buf
),LANMAN_MAILSLOT
)) {
1307 process_lanman_packet(p
,buf2
,len
);
1311 /* Datagram packet received for the domain logon mailslot */
1312 if (strequal(smb_buf_const(buf
),NET_LOGON_MAILSLOT
)) {
1313 process_logon_packet(p
,buf2
,len
,NET_LOGON_MAILSLOT
);
1317 /* Datagram packet received for the NT domain logon mailslot */
1318 if (strequal(smb_buf_const(buf
),NT_LOGON_MAILSLOT
)) {
1319 process_logon_packet(p
,buf2
,len
,NT_LOGON_MAILSLOT
);
1323 nb_packet_dispatch(packet_server
, p
);
1326 /****************************************************************************
1327 Validate a response nmb packet.
1328 ****************************************************************************/
1330 static bool validate_nmb_response_packet( struct nmb_packet
*nmb
)
1332 bool ignore
= False
;
1334 switch (nmb
->header
.opcode
) {
1335 case NMB_NAME_REG_OPCODE
:
1336 case NMB_NAME_REFRESH_OPCODE_8
: /* ambiguity in rfc1002 about which is correct. */
1337 case NMB_NAME_REFRESH_OPCODE_9
: /* WinNT uses 8 by default. */
1338 if (nmb
->header
.ancount
== 0) {
1339 DEBUG(0,("validate_nmb_response_packet: Bad REG/REFRESH Packet. "));
1344 case NMB_NAME_QUERY_OPCODE
:
1345 if ((nmb
->header
.ancount
!= 0) && (nmb
->header
.ancount
!= 1)) {
1346 DEBUG(0,("validate_nmb_response_packet: Bad QUERY Packet. "));
1351 case NMB_NAME_RELEASE_OPCODE
:
1352 if (nmb
->header
.ancount
== 0) {
1353 DEBUG(0,("validate_nmb_response_packet: Bad RELEASE Packet. "));
1358 case NMB_WACK_OPCODE
:
1359 /* Check WACK response here. */
1360 if (nmb
->header
.ancount
!= 1) {
1361 DEBUG(0,("validate_nmb_response_packet: Bad WACK Packet. "));
1366 DEBUG(0,("validate_nmb_response_packet: Ignoring packet with unknown opcode %d.\n",
1367 nmb
->header
.opcode
));
1372 DEBUG(0,("Ignoring response packet with opcode %d.\n", nmb
->header
.opcode
));
1377 /****************************************************************************
1378 Validate a request nmb packet.
1379 ****************************************************************************/
1381 static bool validate_nmb_packet( struct nmb_packet
*nmb
)
1383 bool ignore
= False
;
1385 switch (nmb
->header
.opcode
) {
1386 case NMB_NAME_REG_OPCODE
:
1387 case NMB_NAME_REFRESH_OPCODE_8
: /* ambiguity in rfc1002 about which is correct. */
1388 case NMB_NAME_REFRESH_OPCODE_9
: /* WinNT uses 8 by default. */
1389 case NMB_NAME_MULTIHOMED_REG_OPCODE
:
1390 if (nmb
->header
.qdcount
==0 || nmb
->header
.arcount
==0) {
1391 DEBUG(0,("validate_nmb_packet: Bad REG/REFRESH Packet. "));
1396 case NMB_NAME_QUERY_OPCODE
:
1397 if ((nmb
->header
.qdcount
== 0) || ((nmb
->question
.question_type
!= QUESTION_TYPE_NB_QUERY
) &&
1398 (nmb
->question
.question_type
!= QUESTION_TYPE_NB_STATUS
))) {
1399 DEBUG(0,("validate_nmb_packet: Bad QUERY Packet. "));
1404 case NMB_NAME_RELEASE_OPCODE
:
1405 if (nmb
->header
.qdcount
==0 || nmb
->header
.arcount
==0) {
1406 DEBUG(0,("validate_nmb_packet: Bad RELEASE Packet. "));
1411 DEBUG(0,("validate_nmb_packet: Ignoring packet with unknown opcode %d.\n",
1412 nmb
->header
.opcode
));
1417 DEBUG(0,("validate_nmb_packet: Ignoring request packet with opcode %d.\n", nmb
->header
.opcode
));
1422 /****************************************************************************
1423 Find a subnet (and potentially a response record) for a packet.
1424 ****************************************************************************/
1426 static struct subnet_record
*find_subnet_for_nmb_packet( struct packet_struct
*p
,
1427 struct response_record
**pprrec
)
1429 struct nmb_packet
*nmb
= &p
->packet
.nmb
;
1430 struct response_record
*rrec
= NULL
;
1431 struct subnet_record
*subrec
= NULL
;
1436 if(nmb
->header
.response
) {
1437 /* It's a response packet. Find a record for it or it's an error. */
1439 rrec
= find_response_record( &subrec
, nmb
->header
.name_trn_id
);
1441 DEBUG(3, ("find_subnet_for_nmb_packet: response "
1442 "record not found for response id %d\n",
1443 nmb
->header
.name_trn_id
));
1444 nb_packet_dispatch(packet_server
, p
);
1448 if(subrec
== NULL
) {
1449 DEBUG(0, ("find_subnet_for_nmb_packet: subnet record "
1450 "not found for response id %d\n",
1451 nmb
->header
.name_trn_id
));
1460 /* Try and see what subnet this packet belongs to. */
1463 if(packet_is_for_wins_server(p
))
1464 return wins_server_subnet
;
1466 /* If it wasn't a broadcast packet then send to the UNICAST subnet. */
1467 if(nmb
->header
.nm_flags
.bcast
== False
)
1468 return unicast_subnet
;
1470 /* Go through all the broadcast subnets and see if the mask matches. */
1471 for (subrec
= FIRST_SUBNET
; subrec
; subrec
= NEXT_SUBNET_EXCLUDING_UNICAST(subrec
)) {
1472 if(same_net_v4(p
->ip
, subrec
->bcast_ip
, subrec
->mask_ip
))
1476 /* If none match it must have been a directed broadcast - assign the remote_broadcast_subnet. */
1477 return remote_broadcast_subnet
;
1480 /****************************************************************************
1481 Process a nmb request packet - validate the packet and route it.
1482 ****************************************************************************/
1484 static void process_nmb_request(struct packet_struct
*p
)
1486 struct nmb_packet
*nmb
= &p
->packet
.nmb
;
1487 struct subnet_record
*subrec
= NULL
;
1489 debug_nmb_packet(p
);
1491 /* Ensure we have a good packet. */
1492 if(validate_nmb_packet(nmb
))
1495 /* Allocate a subnet to this packet - if we cannot - fail. */
1496 if((subrec
= find_subnet_for_nmb_packet(p
, NULL
))==NULL
)
1499 switch (nmb
->header
.opcode
) {
1500 case NMB_NAME_REG_OPCODE
:
1501 if(subrec
== wins_server_subnet
)
1502 wins_process_name_registration_request(subrec
, p
);
1504 process_name_registration_request(subrec
, p
);
1507 case NMB_NAME_REFRESH_OPCODE_8
: /* ambiguity in rfc1002 about which is correct. */
1508 case NMB_NAME_REFRESH_OPCODE_9
:
1509 if(subrec
== wins_server_subnet
)
1510 wins_process_name_refresh_request(subrec
, p
);
1512 process_name_refresh_request(subrec
, p
);
1515 case NMB_NAME_MULTIHOMED_REG_OPCODE
:
1516 if(subrec
== wins_server_subnet
) {
1517 wins_process_multihomed_name_registration_request(subrec
, p
);
1519 DEBUG(0,("process_nmb_request: Multihomed registration request must be \
1520 directed at a WINS server.\n"));
1524 case NMB_NAME_QUERY_OPCODE
:
1525 switch (nmb
->question
.question_type
) {
1526 case QUESTION_TYPE_NB_QUERY
:
1527 if(subrec
== wins_server_subnet
)
1528 wins_process_name_query_request(subrec
, p
);
1530 process_name_query_request(subrec
, p
);
1532 case QUESTION_TYPE_NB_STATUS
:
1533 if(subrec
== wins_server_subnet
) {
1534 DEBUG(0,("process_nmb_request: NB_STATUS request directed at WINS server is \
1538 process_node_status_request(subrec
, p
);
1544 case NMB_NAME_RELEASE_OPCODE
:
1545 if(subrec
== wins_server_subnet
)
1546 wins_process_name_release_request(subrec
, p
);
1548 process_name_release_request(subrec
, p
);
1553 /****************************************************************************
1554 Process a nmb response packet - validate the packet and route it.
1555 to either the WINS server or a normal response.
1556 ****************************************************************************/
1558 static void process_nmb_response(struct packet_struct
*p
)
1560 struct nmb_packet
*nmb
= &p
->packet
.nmb
;
1561 struct subnet_record
*subrec
= NULL
;
1562 struct response_record
*rrec
= NULL
;
1564 debug_nmb_packet(p
);
1566 if(validate_nmb_response_packet(nmb
))
1569 if((subrec
= find_subnet_for_nmb_packet(p
, &rrec
))==NULL
)
1573 DEBUG(0, ("process_nmb_response: response packet received but "
1574 "no response record found for id = %d. Ignoring "
1575 "packet.\n", nmb
->header
.name_trn_id
));
1579 /* Increment the number of responses received for this record. */
1581 /* Ensure we don't re-send the request. */
1582 rrec
->repeat_count
= 0;
1584 /* Call the response received function for this packet. */
1585 (*rrec
->resp_fn
)(subrec
, rrec
, p
);
1588 /*******************************************************************
1589 Run elements off the packet queue till its empty
1590 ******************************************************************/
1592 void run_packet_queue(void)
1594 struct packet_struct
*p
;
1596 while ((p
= packet_queue
)) {
1597 DLIST_REMOVE(packet_queue
, p
);
1599 switch (p
->packet_type
) {
1601 if(p
->packet
.nmb
.header
.response
)
1602 process_nmb_response(p
);
1604 process_nmb_request(p
);
1615 /*******************************************************************
1616 Retransmit or timeout elements from all the outgoing subnet response
1617 record queues. NOTE that this code must also check the WINS server
1618 subnet for response records to timeout as the WINS server code
1619 can send requests to check if a client still owns a name.
1620 (Patch from Andrey Alekseyev <fetch@muffin.arcadia.spb.ru>).
1621 ******************************************************************/
1623 void retransmit_or_expire_response_records(time_t t
)
1625 struct subnet_record
*subrec
;
1627 for (subrec
= FIRST_SUBNET
; subrec
; subrec
= get_next_subnet_maybe_unicast_or_wins_server(subrec
)) {
1628 struct response_record
*rrec
, *nextrrec
;
1632 for (rrec
= subrec
->responselist
; rrec
; rrec
= nextrrec
) {
1633 nextrrec
= rrec
->next
;
1635 if (rrec
->repeat_time
<= t
) {
1636 if (rrec
->repeat_count
> 0) {
1637 /* Resend while we have a non-zero repeat_count. */
1638 if(!send_packet(rrec
->packet
)) {
1639 DEBUG(0,("retransmit_or_expire_response_records: Failed to resend packet id %hu \
1640 to IP %s on subnet %s\n", rrec
->response_id
, inet_ntoa(rrec
->packet
->ip
), subrec
->subnet_name
));
1642 rrec
->repeat_time
= t
+ rrec
->repeat_interval
;
1643 rrec
->repeat_count
--;
1645 DEBUG(4,("retransmit_or_expire_response_records: timeout for packet id %hu to IP %s \
1646 on subnet %s\n", rrec
->response_id
, inet_ntoa(rrec
->packet
->ip
), subrec
->subnet_name
));
1649 * Check the flag in this record to prevent recursion if we end
1650 * up in this function again via the timeout function call.
1653 if(!rrec
->in_expiration_processing
) {
1656 * Set the recursion protection flag in this record.
1659 rrec
->in_expiration_processing
= True
;
1661 /* Call the timeout function. This will deal with removing the
1662 timed out packet. */
1663 if(rrec
->timeout_fn
) {
1664 (*rrec
->timeout_fn
)(subrec
, rrec
);
1666 /* We must remove the record ourself if there is
1667 no timeout function. */
1668 remove_response_record(subrec
, rrec
);
1670 /* We have changed subrec->responselist,
1671 * restart from the beginning of this list. */
1673 } /* !rrec->in_expitation_processing */
1674 } /* rrec->repeat_count > 0 */
1675 } /* rrec->repeat_time <= t */
1676 } /* end for rrec */
1677 } /* end for subnet */
1680 /****************************************************************************
1681 Create an fd_set containing all the sockets in the subnet structures,
1682 plus the broadcast sockets.
1683 ***************************************************************************/
1685 struct socket_attributes
{
1686 enum packet_type type
;
1692 static bool create_listen_array(struct socket_attributes
**pattrs
,
1695 struct subnet_record
*subrec
= NULL
;
1698 struct socket_attributes
*attrs
;
1700 /* The ClientNMB and ClientDGRAM sockets */
1703 /* Check that we can add all the fd's we need. */
1704 for (subrec
= FIRST_SUBNET
;
1706 subrec
= NEXT_SUBNET_EXCLUDING_UNICAST(subrec
)) {
1707 if (subrec
->nmb_sock
!= -1) {
1710 if (subrec
->dgram_sock
!= -1) {
1713 if (subrec
->nmb_bcast
!= -1) {
1716 if (subrec
->dgram_bcast
!= -1) {
1721 attrs
= talloc_zero_array(NULL
, struct socket_attributes
, count
);
1722 if (attrs
== NULL
) {
1723 DEBUG(1, ("talloc fail for attrs. "
1724 "size %d\n", count
));
1730 attrs
[num
].fd
= ClientNMB
;
1731 attrs
[num
].type
= NMB_PACKET
;
1732 attrs
[num
].broadcast
= false;
1735 attrs
[num
].fd
= ClientDGRAM
;
1736 attrs
[num
].type
= DGRAM_PACKET
;
1737 attrs
[num
].broadcast
= false;
1740 for (subrec
= FIRST_SUBNET
; subrec
; subrec
= NEXT_SUBNET_EXCLUDING_UNICAST(subrec
)) {
1742 if (subrec
->nmb_sock
!= -1) {
1743 attrs
[num
].fd
= subrec
->nmb_sock
;
1744 attrs
[num
].type
= NMB_PACKET
;
1745 attrs
[num
].broadcast
= false;
1749 if (subrec
->nmb_bcast
!= -1) {
1750 attrs
[num
].fd
= subrec
->nmb_bcast
;
1751 attrs
[num
].type
= NMB_PACKET
;
1752 attrs
[num
].broadcast
= true;
1756 if (subrec
->dgram_sock
!= -1) {
1757 attrs
[num
].fd
= subrec
->dgram_sock
;
1758 attrs
[num
].type
= DGRAM_PACKET
;
1759 attrs
[num
].broadcast
= false;
1763 if (subrec
->dgram_bcast
!= -1) {
1764 attrs
[num
].fd
= subrec
->dgram_bcast
;
1765 attrs
[num
].type
= DGRAM_PACKET
;
1766 attrs
[num
].broadcast
= true;
1771 TALLOC_FREE(*pattrs
);
1774 *pnum_sockets
= count
;
1779 /****************************************************************************
1780 List of packets we're processing this select.
1781 ***************************************************************************/
1783 struct processed_packet
{
1784 struct processed_packet
*next
;
1785 struct processed_packet
*prev
;
1786 enum packet_type packet_type
;
1791 /****************************************************************************
1792 Have we seen this before ?
1793 ***************************************************************************/
1795 static bool is_processed_packet(struct processed_packet
*processed_packet_list
,
1796 struct packet_struct
*packet
)
1798 struct processed_packet
*p
= NULL
;
1800 for (p
= processed_packet_list
; p
; p
= p
->next
) {
1801 if (ip_equal_v4(p
->ip
, packet
->ip
) && p
->packet_type
== packet
->packet_type
) {
1802 if ((p
->packet_type
== NMB_PACKET
) &&
1804 packet
->packet
.nmb
.header
.name_trn_id
)) {
1806 } else if ((p
->packet_type
== DGRAM_PACKET
) &&
1808 packet
->packet
.dgram
.header
.dgm_id
)) {
1816 /****************************************************************************
1817 Keep a list of what we've seen before.
1818 ***************************************************************************/
1820 static bool store_processed_packet(struct processed_packet
**pp_processed_packet_list
,
1821 struct packet_struct
*packet
)
1823 struct processed_packet
*p
= SMB_MALLOC_P(struct processed_packet
);
1827 p
->packet_type
= packet
->packet_type
;
1829 if (packet
->packet_type
== NMB_PACKET
) {
1830 p
->packet_id
= packet
->packet
.nmb
.header
.name_trn_id
;
1831 } else if (packet
->packet_type
== DGRAM_PACKET
) {
1832 p
->packet_id
= packet
->packet
.dgram
.header
.dgm_id
;
1838 DLIST_ADD(*pp_processed_packet_list
, p
);
1842 /****************************************************************************
1843 Throw away what we've seen before.
1844 ***************************************************************************/
1846 static void free_processed_packet_list(struct processed_packet
**pp_processed_packet_list
)
1848 struct processed_packet
*p
= NULL
, *next
= NULL
;
1850 for (p
= *pp_processed_packet_list
; p
; p
= next
) {
1852 DLIST_REMOVE(*pp_processed_packet_list
, p
);
1857 /****************************************************************************
1858 Timeout callback - just notice we timed out.
1859 ***************************************************************************/
1861 static void nmbd_timeout_handler(struct tevent_context
*ev
,
1862 struct tevent_timer
*te
,
1863 struct timeval current_time
,
1866 bool *got_timeout
= private_data
;
1867 *got_timeout
= true;
1870 /****************************************************************************
1871 fd callback - remember the fd that triggered.
1872 ***************************************************************************/
1874 static void nmbd_fd_handler(struct tevent_context
*ev
,
1875 struct tevent_fd
*fde
,
1879 struct socket_attributes
*attr
= private_data
;
1880 attr
->triggered
= true;
1883 /****************************************************************************
1885 ****************************************************************************/
1887 static ssize_t
read_udp_v4_socket(
1891 struct sockaddr_storage
*psa
)
1894 socklen_t socklen
= sizeof(*psa
);
1895 struct sockaddr_in
*si
= (struct sockaddr_in
*)psa
;
1897 memset((char *)psa
,'\0',socklen
);
1899 ret
= (ssize_t
)sys_recvfrom(fd
,buf
,len
,0,
1900 (struct sockaddr
*)psa
,&socklen
);
1902 /* Don't print a low debug error for a non-blocking socket. */
1903 if (errno
== EAGAIN
) {
1904 DEBUG(10,("read_udp_v4_socket: returned EAGAIN\n"));
1906 DEBUG(2,("read_udp_v4_socket: failed. errno=%s\n",
1912 if (psa
->ss_family
!= AF_INET
) {
1913 DEBUG(2,("read_udp_v4_socket: invalid address family %d "
1914 "(not IPv4)\n", (int)psa
->ss_family
));
1918 DEBUG(10,("read_udp_v4_socket: ip %s port %d read: %lu\n",
1919 inet_ntoa(si
->sin_addr
),
1921 (unsigned long)ret
));
1926 /*******************************************************************
1927 Read a packet from a socket and parse it, returning a packet ready
1928 to be used or put on the queue. This assumes a UDP socket.
1929 ******************************************************************/
1931 static struct packet_struct
*read_packet(int fd
,enum packet_type packet_type
)
1933 struct packet_struct
*packet
;
1934 struct sockaddr_storage sa
;
1935 struct sockaddr_in
*si
= (struct sockaddr_in
*)&sa
;
1936 char buf
[MAX_DGRAM_SIZE
];
1939 length
= read_udp_v4_socket(fd
,buf
,sizeof(buf
),&sa
);
1940 if (length
< MIN_DGRAM_SIZE
|| sa
.ss_family
!= AF_INET
) {
1944 packet
= parse_packet(buf
,
1948 ntohs(si
->sin_port
));
1952 packet
->recv_fd
= fd
;
1953 packet
->send_fd
= -1;
1955 DEBUG(5,("Received a packet of len %d from (%s) port %d\n",
1956 length
, inet_ntoa(packet
->ip
), packet
->port
) );
1961 /****************************************************************************
1962 Listens for NMB or DGRAM packets, and queues them.
1963 return True if the socket is dead
1964 ***************************************************************************/
1966 bool listen_for_packets(struct messaging_context
*msg
, bool run_election
)
1968 static struct socket_attributes
*attrs
= NULL
;
1969 static int listen_number
= 0;
1977 int dns_pollidx
= -1;
1979 struct processed_packet
*processed_packet_list
= NULL
;
1980 struct tevent_timer
*te
= NULL
;
1981 bool got_timeout
= false;
1982 TALLOC_CTX
*frame
= talloc_stackframe();
1984 if ((attrs
== NULL
) || rescan_listen_set
) {
1985 if (create_listen_array(&attrs
, &listen_number
)) {
1986 DEBUG(0,("listen_for_packets: Fatal error. unable to create listen set. Exiting.\n"));
1990 rescan_listen_set
= False
;
1993 num_sockets
= listen_number
;
1996 dns_fd
= asyncdns_fd();
1998 attrs
= talloc_realloc(NULL
,
2000 struct socket_attributes
,
2002 if (attrs
== NULL
) {
2006 dns_pollidx
= num_sockets
;
2007 attrs
[dns_pollidx
].fd
= dns_fd
;
2009 * dummy values, we only need
2012 attrs
[dns_pollidx
].type
= NMB_PACKET
;
2013 attrs
[dns_pollidx
].broadcast
= false;
2018 for (i
=0; i
<num_sockets
; i
++) {
2019 struct tevent_fd
*tfd
= tevent_add_fd(nmbd_event_context(),
2029 attrs
[i
].triggered
= false;
2033 * During elections and when expecting a netbios response packet we
2034 * need to send election packets at tighter intervals.
2035 * Ideally it needs to be the interval (in ms) between time now and
2036 * the time we are expecting the next netbios packet.
2039 if (run_election
||num_response_packets
) {
2042 timeout_secs
= NMBD_SELECT_LOOP
;
2045 te
= tevent_add_timer(nmbd_event_context(),
2047 tevent_timeval_current_ofs(timeout_secs
, 0),
2048 nmbd_timeout_handler
,
2055 loop_rtn
= tevent_loop_once(nmbd_event_context());
2057 if (loop_rtn
== -1) {
2068 if ((dns_fd
!= -1) && (dns_pollidx
!= -1) &&
2069 attrs
[dns_pollidx
].triggered
){
2076 for(i
= 0; i
< listen_number
; i
++) {
2077 enum packet_type packet_type
;
2078 struct packet_struct
*packet
;
2079 const char *packet_name
;
2083 if (!attrs
[i
].triggered
) {
2087 if (attrs
[i
].type
== NMB_PACKET
) {
2089 packet_type
= NMB_PACKET
;
2090 packet_name
= "nmb";
2091 client_fd
= ClientNMB
;
2092 client_port
= global_nmb_port
;
2095 packet_type
= DGRAM_PACKET
;
2096 packet_name
= "dgram";
2097 client_fd
= ClientDGRAM
;
2098 client_port
= DGRAM_PORT
;
2101 packet
= read_packet(attrs
[i
].fd
, packet_type
);
2107 * If we got a packet on the broadcast socket and interfaces
2108 * only is set then check it came from one of our local nets.
2110 if (lp_bind_interfaces_only() &&
2111 (attrs
[i
].fd
== client_fd
) &&
2112 (!is_local_net_v4(packet
->ip
))) {
2113 DEBUG(7,("discarding %s packet sent to broadcast socket from %s:%d\n",
2114 packet_name
, inet_ntoa(packet
->ip
), packet
->port
));
2115 free_packet(packet
);
2120 if ((is_loopback_ip_v4(packet
->ip
) || ismyip_v4(packet
->ip
)) &&
2121 packet
->port
== client_port
)
2123 if (client_port
== DGRAM_PORT
) {
2124 DEBUG(7,("discarding own dgram packet from %s:%d\n",
2125 inet_ntoa(packet
->ip
),packet
->port
));
2126 free_packet(packet
);
2130 if (packet
->packet
.nmb
.header
.nm_flags
.bcast
) {
2131 DEBUG(7,("discarding own nmb bcast packet from %s:%d\n",
2132 inet_ntoa(packet
->ip
),packet
->port
));
2133 free_packet(packet
);
2139 if (is_processed_packet(processed_packet_list
, packet
)) {
2140 DEBUG(7,("discarding duplicate packet from %s:%d\n",
2141 inet_ntoa(packet
->ip
),packet
->port
));
2142 free_packet(packet
);
2146 store_processed_packet(&processed_packet_list
, packet
);
2148 if (attrs
[i
].broadcast
) {
2149 /* this is a broadcast socket */
2150 packet
->send_fd
= attrs
[i
-1].fd
;
2152 /* this is already a unicast socket */
2153 packet
->send_fd
= attrs
[i
].fd
;
2156 queue_packet(packet
);
2159 free_processed_packet_list(&processed_packet_list
);
2164 /****************************************************************************
2165 Construct and send a netbios DGRAM.
2166 **************************************************************************/
2168 bool send_mailslot(bool unique
, const char *mailslot
,char *buf
, size_t len
,
2169 const char *srcname
, int src_type
,
2170 const char *dstname
, int dest_type
,
2171 struct in_addr dest_ip
,struct in_addr src_ip
,
2174 bool loopback_this_packet
= False
;
2175 struct packet_struct p
;
2176 struct dgram_packet
*dgram
= &p
.packet
.dgram
;
2180 memset((char *)&p
,'\0',sizeof(p
));
2182 if(ismyip_v4(dest_ip
) && (dest_port
== DGRAM_PORT
)) /* Only if to DGRAM_PORT */
2183 loopback_this_packet
= True
;
2185 /* generate_name_trn_id(); */ /* Not used, so gone, RJS */
2187 /* DIRECT GROUP or UNIQUE datagram. */
2188 dgram
->header
.msg_type
= unique
? 0x10 : 0x11;
2189 dgram
->header
.flags
.node_type
= M_NODE
;
2190 dgram
->header
.flags
.first
= True
;
2191 dgram
->header
.flags
.more
= False
;
2192 dgram
->header
.dgm_id
= generate_name_trn_id();
2193 dgram
->header
.source_ip
= src_ip
;
2194 dgram
->header
.source_port
= DGRAM_PORT
;
2195 dgram
->header
.dgm_length
= 0; /* Let build_dgram() handle this. */
2196 dgram
->header
.packet_offset
= 0;
2198 make_nmb_name(&dgram
->source_name
,srcname
,src_type
);
2199 make_nmb_name(&dgram
->dest_name
,dstname
,dest_type
);
2201 ptr
= &dgram
->data
[0];
2203 /* Setup the smb part. */
2204 ptr
-= 4; /* XXX Ugliness because of handling of tcp SMB length. */
2207 if (smb_size
+ 17*2 + strlen(mailslot
) + 1 + len
> MAX_DGRAM_SIZE
) {
2208 DEBUG(0, ("send_mailslot: Cannot write beyond end of packet\n"));
2212 cli_set_message(ptr
,17,strlen(mailslot
) + 1 + len
,True
);
2215 SCVAL(ptr
,smb_com
,SMBtrans
);
2216 SSVAL(ptr
,smb_vwv1
,len
);
2217 SSVAL(ptr
,smb_vwv11
,len
);
2218 SSVAL(ptr
,smb_vwv12
,70 + strlen(mailslot
));
2219 SSVAL(ptr
,smb_vwv13
,3);
2220 SSVAL(ptr
,smb_vwv14
,1);
2221 SSVAL(ptr
,smb_vwv15
,1);
2222 SSVAL(ptr
,smb_vwv16
,2);
2224 strlcpy_base(p2
, mailslot
, dgram
->data
, sizeof(dgram
->data
));
2225 p2
= skip_string(ptr
,MAX_DGRAM_SIZE
,p2
);
2227 if (((p2
+len
) > dgram
->data
+sizeof(dgram
->data
)) || ((p2
+len
) < p2
)) {
2228 DEBUG(0, ("send_mailslot: Cannot write beyond end of packet\n"));
2237 dgram
->datasize
= PTR_DIFF(p2
,ptr
+4); /* +4 for tcp length. */
2242 p
.send_fd
= find_subnet_mailslot_fd_for_address( src_ip
);
2243 p
.timestamp
= time(NULL
);
2244 p
.packet_type
= DGRAM_PACKET
;
2246 DEBUG(4,("send_mailslot: Sending to mailslot %s from %s IP %s ", mailslot
,
2247 nmb_namestr(&dgram
->source_name
), inet_ntoa(src_ip
)));
2248 DEBUG(4,("to %s IP %s\n", nmb_namestr(&dgram
->dest_name
), inet_ntoa(dest_ip
)));
2250 debug_browse_data(buf
, len
);
2252 if(loopback_this_packet
) {
2253 struct packet_struct
*lo_packet
= NULL
;
2254 DEBUG(5,("send_mailslot: sending packet to ourselves.\n"));
2255 if((lo_packet
= copy_packet(&p
)) == NULL
)
2257 queue_packet(lo_packet
);
2260 return(send_packet(&p
));