Large changes from jra@cygnus.com. Mainly browser updates.
[Samba/gbeck.git] / source / namepacket.c
blob09ee5dd79e2476eb9daf2ad7d044c32aa7673ffa
1 /*
2 Unix SMB/Netbios implementation.
3 Version 1.9.
4 NBT netbios routines and daemon - version 2
5 Copyright (C) Andrew Tridgell 1994-1995
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 Revision History:
23 14 jan 96: lkcl@pires.co.uk
24 added multiple workgroup domain master support
28 #include "includes.h"
30 extern int ClientNMB;
31 extern int ClientDGRAM;
33 extern int DEBUGLEVEL;
35 extern int num_response_packets;
37 BOOL CanRecurse = True;
38 extern pstring scope;
39 extern struct in_addr wins_ip;
41 static uint16 name_trn_id=0;
44 /***************************************************************************
45 updates the unique transaction identifier
46 **************************************************************************/
47 void debug_browse_data(char *outbuf, int len)
49 int i,j;
50 for (i = 0; i < len; i+= 16)
52 DEBUG(4, ("%3x char ", i));
54 for (j = 0; j < 16; j++)
56 unsigned char x = outbuf[i+j];
57 if (x < 32 || x > 127) x = '.';
59 if (i+j >= len) break;
60 DEBUG(4, ("%c", x));
63 DEBUG(4, (" hex ", i));
65 for (j = 0; j < 16; j++)
67 if (i+j >= len) break;
68 DEBUG(4, (" %02x", outbuf[i+j]));
71 DEBUG(4, ("\n"));
77 /***************************************************************************
78 updates the unique transaction identifier
79 **************************************************************************/
80 static void update_name_trn_id(void)
82 if (!name_trn_id)
84 name_trn_id = (time(NULL)%(unsigned)0x7FFF) + (getpid()%(unsigned)100);
86 name_trn_id = (name_trn_id+1) % (unsigned)0x7FFF;
90 /****************************************************************************
91 initiate a netbios packet
92 ****************************************************************************/
93 void initiate_netbios_packet(uint16 *id,
94 int fd,int quest_type,char *name,int name_type,
95 int nb_flags,BOOL bcast,BOOL recurse,
96 struct in_addr to_ip)
98 struct packet_struct p;
99 struct nmb_packet *nmb = &p.packet.nmb;
100 struct res_rec additional_rec;
101 char *packet_type = "unknown";
102 int opcode = -1;
104 if (!id) return;
106 if (quest_type == NMB_STATUS) { packet_type = "nmb_status"; opcode = 0; }
107 if (quest_type == NMB_QUERY ) { packet_type = "nmb_query"; opcode = 0; }
108 if (quest_type == NMB_REG ) { packet_type = "nmb_reg"; opcode = 5; }
109 if (quest_type == NMB_REG_REFRESH ) { packet_type = "nmb_reg_refresh"; opcode = 9; }
110 if (quest_type == NMB_REL ) { packet_type = "nmb_rel"; opcode = 6; }
112 DEBUG(4,("initiating netbios packet: %s %s(%x) (bcast=%s) %s\n",
113 packet_type, name, name_type, BOOLSTR(bcast), inet_ntoa(to_ip)));
115 if (opcode == -1) return;
117 bzero((char *)&p,sizeof(p));
119 if (*id == 0xffff) {
120 update_name_trn_id();
121 *id = name_trn_id; /* allow resending with same id */
124 nmb->header.name_trn_id = *id;
125 nmb->header.opcode = opcode;
126 nmb->header.response = False;
128 nmb->header.nm_flags.bcast = bcast;
129 nmb->header.nm_flags.recursion_available = False;
130 nmb->header.nm_flags.recursion_desired = recurse;
131 nmb->header.nm_flags.trunc = False;
132 nmb->header.nm_flags.authoritative = False;
134 nmb->header.rcode = 0;
135 nmb->header.qdcount = 1;
136 nmb->header.ancount = 0;
137 nmb->header.nscount = 0;
138 nmb->header.arcount = (quest_type==NMB_REG ||
139 quest_type==NMB_REL ||
140 quest_type==NMB_REG_REFRESH) ? 1 : 0;
142 make_nmb_name(&nmb->question.question_name,name,name_type,scope);
144 nmb->question.question_type = quest_type == NMB_STATUS ? 0x21 : 0x20;
145 nmb->question.question_class = 0x1;
147 if (quest_type == NMB_REG ||
148 quest_type == NMB_REG_REFRESH ||
149 quest_type == NMB_REL)
151 nmb->additional = &additional_rec;
152 bzero((char *)nmb->additional,sizeof(*nmb->additional));
154 nmb->additional->rr_name = nmb->question.question_name;
155 nmb->additional->rr_type = 0x20;
156 nmb->additional->rr_class = 0x1;
158 if (quest_type == NMB_REG || quest_type == NMB_REG_REFRESH)
159 nmb->additional->ttl = lp_max_ttl();
160 else
161 nmb->additional->ttl = 0;
163 nmb->additional->rdlength = 6;
164 nmb->additional->rdata[0] = nb_flags;
165 putip(&nmb->additional->rdata[2],(char *)iface_ip(to_ip));
168 p.ip = to_ip;
169 p.port = NMB_PORT;
170 p.fd = fd;
171 p.timestamp = time(NULL);
172 p.packet_type = NMB_PACKET;
174 debug_nmb_packet(&p);
176 if (!send_packet(&p)) {
177 DEBUG(3,("send_packet to %s %d failed\n",inet_ntoa(p.ip),p.port));
178 *id = 0xffff;
181 return;
185 /****************************************************************************
186 reply to a netbios name packet
187 ****************************************************************************/
188 void reply_netbios_packet(struct packet_struct *p1,int trn_id,
189 int rcode, int rcv_code, int opcode, BOOL recurse,
190 struct nmb_name *rr_name,int rr_type,int rr_class,int ttl,
191 char *data,int len)
193 struct packet_struct p;
194 struct nmb_packet *nmb = &p.packet.nmb;
195 struct res_rec answers;
196 char *packet_type = "unknown";
197 BOOL recursion_desired = False;
199 p = *p1;
201 switch (rcv_code)
203 case NMB_STATUS:
205 packet_type = "nmb_status";
206 recursion_desired = True;
207 break;
209 case NMB_QUERY:
211 packet_type = "nmb_query";
212 recursion_desired = True;
213 break;
215 case NMB_REG:
217 packet_type = "nmb_reg";
218 recursion_desired = True;
219 break;
221 case NMB_REL:
223 packet_type = "nmb_rel";
224 recursion_desired = False;
225 break;
227 case NMB_WAIT_ACK:
229 packet_type = "nmb_wack";
230 recursion_desired = False;
231 break;
233 default:
235 DEBUG(1,("replying netbios packet: %s %s\n",
236 packet_type, namestr(rr_name), inet_ntoa(p.ip)));
238 return;
242 DEBUG(4,("replying netbios packet: %s %s\n",
243 packet_type, namestr(rr_name), inet_ntoa(p.ip)));
245 nmb->header.name_trn_id = trn_id;
246 nmb->header.opcode = opcode;
247 nmb->header.response = True;
248 nmb->header.nm_flags.bcast = False;
249 nmb->header.nm_flags.recursion_available = recurse;
250 nmb->header.nm_flags.recursion_desired = recursion_desired;
251 nmb->header.nm_flags.trunc = False;
252 nmb->header.nm_flags.authoritative = True;
254 nmb->header.qdcount = 0;
255 nmb->header.ancount = 1;
256 nmb->header.nscount = 0;
257 nmb->header.arcount = 0;
258 nmb->header.rcode = rcode;
260 bzero((char*)&nmb->question,sizeof(nmb->question));
262 nmb->answers = &answers;
263 bzero((char*)nmb->answers,sizeof(*nmb->answers));
265 nmb->answers->rr_name = *rr_name;
266 nmb->answers->rr_type = rr_type;
267 nmb->answers->rr_class = rr_class;
268 nmb->answers->ttl = ttl;
270 if (data && len)
272 nmb->answers->rdlength = len;
273 memcpy(nmb->answers->rdata, data, len);
276 p.packet_type = NMB_PACKET;
278 debug_nmb_packet(&p);
280 send_packet(&p);
284 /*******************************************************************
285 the global packet linked-list. incoming entries are added to the
286 end of this list. it is supposed to remain fairly short so we
287 won't bother with an end pointer.
288 ******************************************************************/
289 static struct packet_struct *packet_queue = NULL;
291 /*******************************************************************
292 queue a packet into the packet queue
293 ******************************************************************/
294 void queue_packet(struct packet_struct *packet)
296 struct packet_struct *p;
298 if (!packet_queue) {
299 packet->prev = NULL;
300 packet->next = NULL;
301 packet_queue = packet;
302 return;
305 /* find the bottom */
306 for (p=packet_queue;p->next;p=p->next) ;
308 p->next = packet;
309 packet->next = NULL;
310 packet->prev = p;
313 /****************************************************************************
314 determine if a packet is for us. Note that to have any chance of
315 being efficient we need to drop as many packets as possible at this
316 stage as subsequent processing is expensive.
318 We also must make absolutely sure we don't tread on another machines
319 property by answering a packet that is not for us.
320 ****************************************************************************/
321 static BOOL listening(struct packet_struct *p,struct nmb_name *n)
323 struct subnet_record *d;
324 struct name_record *n1;
326 /* We explicitly don't search WINS here - this will be done
327 in find_name_search is it was a packet from a non-local subnet. */
328 d = find_subnet(p->ip);
330 n1 = find_name_search(&d,n,FIND_LOCAL|FIND_WINS|FIND_SELF,p->ip);
332 return (n1 != NULL);
336 /****************************************************************************
337 process udp 138 datagrams
338 ****************************************************************************/
339 static void process_dgram(struct packet_struct *p)
341 char *buf;
342 char *buf2;
343 int len;
344 struct dgram_packet *dgram = &p->packet.dgram;
346 /* if we aren't listening to the destination name then ignore the packet */
347 if (!listening(p,&dgram->dest_name))
348 return;
351 if (dgram->header.msg_type != 0x10 &&
352 dgram->header.msg_type != 0x11 &&
353 dgram->header.msg_type != 0x12) {
354 /* don't process error packets etc yet */
355 return;
358 buf = &dgram->data[0];
359 buf -= 4; /* XXXX for the pseudo tcp length -
360 someday I need to get rid of this */
362 if (CVAL(buf,smb_com) != SMBtrans) return;
364 len = SVAL(buf,smb_vwv11);
365 buf2 = smb_base(buf) + SVAL(buf,smb_vwv12);
367 DEBUG(4,("datagram from %s to %s for %s of type %d len=%d\n",
368 namestr(&dgram->source_name),namestr(&dgram->dest_name),
369 smb_buf(buf),CVAL(buf2,0),len));
372 if (len <= 0) return;
374 /* datagram packet received for the browser mailslot */
375 if (strequal(smb_buf(buf),BROWSE_MAILSLOT)) {
376 process_browse_packet(p,buf2,len);
377 return;
380 /* datagram packet received for the domain log on mailslot */
381 if (strequal(smb_buf(buf),NET_LOGON_MAILSLOT)) {
382 process_logon_packet(p,buf2,len);
383 return;
387 /****************************************************************************
388 process a nmb packet
389 ****************************************************************************/
390 static void process_nmb(struct packet_struct *p)
392 struct nmb_packet *nmb = &p->packet.nmb;
394 debug_nmb_packet(p);
396 switch (nmb->header.opcode)
398 case 8: /* what is this?? */
399 case NMB_REG:
400 case NMB_REG_REFRESH:
402 if (nmb->header.response)
404 if (nmb->header.ancount ==0) break;
405 response_netbios_packet(p); /* response to registration dealt
406 with here */
408 else
410 if (nmb->header.qdcount==0 || nmb->header.arcount==0) break;
411 reply_name_reg(p);
413 break;
416 case 0:
418 if (nmb->header.response)
420 switch (nmb->question.question_type)
422 case 0x0:
424 response_netbios_packet(p);
425 break;
428 return;
430 else if (nmb->header.qdcount>0)
432 switch (nmb->question.question_type)
434 case NMB_QUERY:
436 reply_name_query(p);
437 break;
439 case NMB_STATUS:
441 reply_name_status(p);
442 break;
445 return;
447 break;
450 case NMB_REL:
452 if (nmb->header.response)
454 if (nmb->header.ancount ==0) break;
455 response_netbios_packet(p); /* response to release dealt
456 with here */
458 else
460 if (nmb->header.qdcount==0 || nmb->header.arcount==0) break;
461 reply_name_release(p);
463 break;
469 /*******************************************************************
470 run elements off the packet queue till its empty
471 ******************************************************************/
472 void run_packet_queue()
474 struct packet_struct *p;
476 while ((p=packet_queue))
478 switch (p->packet_type)
480 case NMB_PACKET:
481 process_nmb(p);
482 break;
484 case DGRAM_PACKET:
485 process_dgram(p);
486 break;
489 packet_queue = packet_queue->next;
490 if (packet_queue) packet_queue->prev = NULL;
491 free_packet(p);
495 /****************************************************************************
496 listens for NMB or DGRAM packets, and queues them
497 ***************************************************************************/
498 void listen_for_packets(BOOL run_election)
500 fd_set fds;
501 int selrtn;
502 struct timeval timeout;
504 FD_ZERO(&fds);
505 FD_SET(ClientNMB,&fds);
506 FD_SET(ClientDGRAM,&fds);
508 /* during elections and when expecting a netbios response packet we
509 need to send election packets at tighter intervals
511 ideally it needs to be the interval (in ms) between time now and
512 the time we are expecting the next netbios packet */
514 timeout.tv_sec = (run_election||num_response_packets) ? 1:NMBD_SELECT_LOOP;
515 timeout.tv_usec = 0;
517 selrtn = sys_select(&fds,&timeout);
519 if (FD_ISSET(ClientNMB,&fds))
521 struct packet_struct *packet = read_packet(ClientNMB, NMB_PACKET);
522 if (packet)
524 if (ismyip(packet->ip) && packet->port == NMB_PORT)
526 DEBUG(7,("discarding own packet from %s:%d\n",
527 inet_ntoa(packet->ip),packet->port));
528 free_packet(packet);
530 else
532 queue_packet(packet);
537 if (FD_ISSET(ClientDGRAM,&fds))
539 struct packet_struct *packet = read_packet(ClientDGRAM, DGRAM_PACKET);
540 if (packet)
542 if (ismyip(packet->ip) && packet->port == DGRAM_PORT)
544 DEBUG(7,("discarding own packet from %s:%d\n",
545 inet_ntoa(packet->ip),packet->port));
546 free_packet(packet);
548 else
550 queue_packet(packet);
558 /****************************************************************************
559 construct and send a netbios DGRAM
561 Note that this currently sends all answers to port 138. thats the
562 wrong things to do! I should send to the requestors port. XXX
563 **************************************************************************/
564 BOOL send_mailslot_reply(BOOL unique, char *mailslot,int fd,char *buf,int len,char *srcname,
565 char *dstname,int src_type,int dest_type,
566 struct in_addr dest_ip,struct in_addr src_ip)
568 struct packet_struct p;
569 struct dgram_packet *dgram = &p.packet.dgram;
570 char *ptr,*p2;
571 char tmp[4];
573 /* ha ha. no. do NOT send packets to 255.255.255.255: it's a pseudo address */
574 if (ip_equal(wins_ip, dest_ip)) return False;
576 bzero((char *)&p,sizeof(p));
578 update_name_trn_id();
580 /* DIRECT GROUP or UNIQUE datagram */
581 dgram->header.msg_type = unique ? 0x10 : 0x11;
582 dgram->header.flags.node_type = M_NODE;
583 dgram->header.flags.first = True;
584 dgram->header.flags.more = False;
585 dgram->header.dgm_id = name_trn_id;
586 dgram->header.source_ip = src_ip;
587 dgram->header.source_port = DGRAM_PORT;
588 dgram->header.dgm_length = 0; /* let build_dgram() handle this */
589 dgram->header.packet_offset = 0;
591 make_nmb_name(&dgram->source_name,srcname,src_type,scope);
592 make_nmb_name(&dgram->dest_name,dstname,dest_type,scope);
594 ptr = &dgram->data[0];
596 /* now setup the smb part */
597 ptr -= 4; /* XXX ugliness because of handling of tcp SMB length */
598 memcpy(tmp,ptr,4);
599 set_message(ptr,17,17 + len,True);
600 memcpy(ptr,tmp,4);
602 CVAL(ptr,smb_com) = SMBtrans;
603 SSVAL(ptr,smb_vwv1,len);
604 SSVAL(ptr,smb_vwv11,len);
605 SSVAL(ptr,smb_vwv12,70 + strlen(mailslot));
606 SSVAL(ptr,smb_vwv13,3);
607 SSVAL(ptr,smb_vwv14,1);
608 SSVAL(ptr,smb_vwv15,1);
609 SSVAL(ptr,smb_vwv16,2);
610 p2 = smb_buf(ptr);
611 strcpy(p2,mailslot);
612 p2 = skip_string(p2,1);
614 memcpy(p2,buf,len);
615 p2 += len;
617 dgram->datasize = PTR_DIFF(p2,ptr+4); /* +4 for tcp length */
619 p.ip = dest_ip;
620 p.port = DGRAM_PORT;
621 p.fd = ClientDGRAM;
622 p.timestamp = time(NULL);
623 p.packet_type = DGRAM_PACKET;
625 DEBUG(4,("send mailslot %s from %s %s", mailslot,
626 inet_ntoa(src_ip),namestr(&dgram->source_name)));
627 DEBUG(4,("to %s %s\n", inet_ntoa(dest_ip),namestr(&dgram->dest_name)));
629 return(send_packet(&p));