2 Unix SMB/CIFS implementation.
4 packet utility functions
6 Copyright (C) Andrew Tridgell 2005
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 "nbt_server/nbt_server.h"
24 #include "lib/socket/socket.h"
25 #include "librpc/gen_ndr/ndr_nbt.h"
28 we received a badly formed packet - log it
30 void nbtd_bad_packet(struct nbt_name_packet *packet,
31 const struct socket_address *src, const char *reason)
33 DEBUG(2,("nbtd: bad packet '%s' from %s:%d\n", reason, src->addr, src->port));
35 NDR_PRINT_DEBUG(nbt_name_packet, packet);
41 see if an incoming packet is a broadcast packet from one of our own
44 BOOL nbtd_self_packet_and_bcast(struct nbt_name_socket *nbtsock,
45 struct nbt_name_packet *packet,
46 const struct socket_address *src)
48 struct nbtd_interface *iface = talloc_get_type(nbtsock->incoming.private,
49 struct nbtd_interface);
51 /* if its not a broadcast then its not considered a self packet */
52 if (!(packet->operation & NBT_FLAG_BROADCAST)) {
57 * this uses the fact that iface->nbtsock is the unicast listen address
58 * if the interface isn't the global bcast interface
60 * so if the request was directed to the unicast address it isn't a broadcast
63 if (iface->nbtsock == nbtsock &&
64 iface != iface->nbtsrv->bcast_interface) {
68 return nbtd_self_packet(nbtsock, packet, src);
71 BOOL nbtd_self_packet(struct nbt_name_socket *nbtsock,
72 struct nbt_name_packet *packet,
73 const struct socket_address *src)
75 struct nbtd_interface *iface = talloc_get_type(nbtsock->incoming.private,
76 struct nbtd_interface);
77 struct nbtd_server *nbtsrv = iface->nbtsrv;
79 /* if its not from the nbt port, then it wasn't a broadcast from us */
80 if (src->port != lp_nbt_port()) {
84 /* we have to loop over our interface list, seeing if its from
85 one of our own interfaces */
86 for (iface=nbtsrv->interfaces;iface;iface=iface->next) {
87 if (strcmp(src->addr, iface->ip_address) == 0) {
97 send a name query reply
99 void nbtd_name_query_reply(struct nbt_name_socket *nbtsock,
100 struct nbt_name_packet *request_packet,
101 struct socket_address *src,
102 struct nbt_name *name, uint32_t ttl,
103 uint16_t nb_flags, const char **addresses)
105 struct nbt_name_packet *packet;
106 size_t num_addresses = str_list_length(addresses);
107 struct nbtd_interface *iface = talloc_get_type(nbtsock->incoming.private,
108 struct nbtd_interface);
109 struct nbtd_server *nbtsrv = iface->nbtsrv;
112 if (num_addresses == 0) {
113 DEBUG(3,("No addresses in name query reply - failing\n"));
117 packet = talloc_zero(nbtsock, struct nbt_name_packet);
118 if (packet == NULL) return;
120 packet->name_trn_id = request_packet->name_trn_id;
125 NBT_FLAG_AUTHORITIVE |
126 NBT_FLAG_RECURSION_DESIRED |
127 NBT_FLAG_RECURSION_AVAIL;
129 packet->answers = talloc_array(packet, struct nbt_res_rec, 1);
130 if (packet->answers == NULL) goto failed;
132 packet->answers[0].name = *name;
133 packet->answers[0].rr_type = NBT_QTYPE_NETBIOS;
134 packet->answers[0].rr_class = NBT_QCLASS_IP;
135 packet->answers[0].ttl = ttl;
136 packet->answers[0].rdata.netbios.length = num_addresses*6;
137 packet->answers[0].rdata.netbios.addresses =
138 talloc_array(packet->answers, struct nbt_rdata_address, num_addresses);
139 if (packet->answers[0].rdata.netbios.addresses == NULL) goto failed;
141 for (i=0;i<num_addresses;i++) {
142 struct nbt_rdata_address *addr =
143 &packet->answers[0].rdata.netbios.addresses[i];
144 addr->nb_flags = nb_flags;
145 addr->ipaddr = talloc_strdup(packet->answers, addresses[i]);
146 if (addr->ipaddr == NULL) goto failed;
149 DEBUG(7,("Sending name query reply for %s at %s to %s:%d\n",
150 nbt_name_string(packet, name), addresses[0], src->addr, src->port));
152 nbtsrv->stats.total_sent++;
153 nbt_name_reply_send(nbtsock, src, packet);
161 send a negative name query reply
163 void nbtd_negative_name_query_reply(struct nbt_name_socket *nbtsock,
164 struct nbt_name_packet *request_packet,
165 struct socket_address *src)
167 struct nbt_name_packet *packet;
168 struct nbt_name *name = &request_packet->questions[0].name;
169 struct nbtd_interface *iface = talloc_get_type(nbtsock->incoming.private,
170 struct nbtd_interface);
171 struct nbtd_server *nbtsrv = iface->nbtsrv;
173 packet = talloc_zero(nbtsock, struct nbt_name_packet);
174 if (packet == NULL) return;
176 packet->name_trn_id = request_packet->name_trn_id;
181 NBT_FLAG_AUTHORITIVE |
184 packet->answers = talloc_array(packet, struct nbt_res_rec, 1);
185 if (packet->answers == NULL) goto failed;
187 packet->answers[0].name = *name;
188 packet->answers[0].rr_type = NBT_QTYPE_NULL;
189 packet->answers[0].rr_class = NBT_QCLASS_IP;
190 packet->answers[0].ttl = 0;
191 ZERO_STRUCT(packet->answers[0].rdata);
193 DEBUG(7,("Sending negative name query reply for %s to %s:%d\n",
194 nbt_name_string(packet, name), src->addr, src->port));
196 nbtsrv->stats.total_sent++;
197 nbt_name_reply_send(nbtsock, src, packet);
204 send a name registration reply
206 void nbtd_name_registration_reply(struct nbt_name_socket *nbtsock,
207 struct nbt_name_packet *request_packet,
208 struct socket_address *src,
211 struct nbt_name_packet *packet;
212 struct nbt_name *name = &request_packet->questions[0].name;
213 struct nbtd_interface *iface = talloc_get_type(nbtsock->incoming.private,
214 struct nbtd_interface);
215 struct nbtd_server *nbtsrv = iface->nbtsrv;
217 packet = talloc_zero(nbtsock, struct nbt_name_packet);
218 if (packet == NULL) return;
220 packet->name_trn_id = request_packet->name_trn_id;
224 NBT_OPCODE_REGISTER |
225 NBT_FLAG_AUTHORITIVE |
226 NBT_FLAG_RECURSION_DESIRED |
227 NBT_FLAG_RECURSION_AVAIL |
230 packet->answers = talloc_array(packet, struct nbt_res_rec, 1);
231 if (packet->answers == NULL) goto failed;
233 packet->answers[0].name = *name;
234 packet->answers[0].rr_type = NBT_QTYPE_NETBIOS;
235 packet->answers[0].rr_class = NBT_QCLASS_IP;
236 packet->answers[0].ttl = request_packet->additional[0].ttl;
237 packet->answers[0].rdata = request_packet->additional[0].rdata;
239 DEBUG(7,("Sending %s name registration reply for %s to %s:%d\n",
240 rcode==0?"positive":"negative",
241 nbt_name_string(packet, name), src->addr, src->port));
243 nbtsrv->stats.total_sent++;
244 nbt_name_reply_send(nbtsock, src, packet);
252 send a name release reply
254 void nbtd_name_release_reply(struct nbt_name_socket *nbtsock,
255 struct nbt_name_packet *request_packet,
256 struct socket_address *src,
259 struct nbt_name_packet *packet;
260 struct nbt_name *name = &request_packet->questions[0].name;
261 struct nbtd_interface *iface = talloc_get_type(nbtsock->incoming.private,
262 struct nbtd_interface);
263 struct nbtd_server *nbtsrv = iface->nbtsrv;
265 packet = talloc_zero(nbtsock, struct nbt_name_packet);
266 if (packet == NULL) return;
268 packet->name_trn_id = request_packet->name_trn_id;
273 NBT_FLAG_AUTHORITIVE |
276 packet->answers = talloc_array(packet, struct nbt_res_rec, 1);
277 if (packet->answers == NULL) goto failed;
279 packet->answers[0].name = *name;
280 packet->answers[0].rr_type = NBT_QTYPE_NETBIOS;
281 packet->answers[0].rr_class = NBT_QCLASS_IP;
282 packet->answers[0].ttl = request_packet->additional[0].ttl;
283 packet->answers[0].rdata = request_packet->additional[0].rdata;
285 DEBUG(7,("Sending %s name release reply for %s to %s:%d\n",
286 rcode==0?"positive":"negative",
287 nbt_name_string(packet, name), src->addr, src->port));
289 nbtsrv->stats.total_sent++;
290 nbt_name_reply_send(nbtsock, src, packet);
300 void nbtd_wack_reply(struct nbt_name_socket *nbtsock,
301 struct nbt_name_packet *request_packet,
302 struct socket_address *src,
305 struct nbt_name_packet *packet;
306 struct nbt_name *name = &request_packet->questions[0].name;
307 struct nbtd_interface *iface = talloc_get_type(nbtsock->incoming.private,
308 struct nbtd_interface);
309 struct nbtd_server *nbtsrv = iface->nbtsrv;
311 packet = talloc_zero(nbtsock, struct nbt_name_packet);
312 if (packet == NULL) return;
314 packet->name_trn_id = request_packet->name_trn_id;
319 NBT_FLAG_AUTHORITIVE;
321 packet->answers = talloc_array(packet, struct nbt_res_rec, 1);
322 if (packet->answers == NULL) goto failed;
324 packet->answers[0].name = *name;
325 packet->answers[0].rr_type = NBT_QTYPE_NETBIOS;
326 packet->answers[0].rr_class = NBT_QCLASS_IP;
327 packet->answers[0].ttl = ttl;
328 packet->answers[0].rdata.data.length = 2;
329 packet->answers[0].rdata.data.data = talloc_size(packet, 2);
330 if (packet->answers[0].rdata.data.data == NULL) goto failed;
331 RSSVAL(packet->answers[0].rdata.data.data, 0, request_packet->operation);
333 DEBUG(7,("Sending WACK reply for %s to %s:%d\n",
334 nbt_name_string(packet, name), src->addr, src->port));
336 nbtsrv->stats.total_sent++;
337 nbt_name_reply_send(nbtsock, src, packet);