xref: /netbsd/external/mpl/dhcp/dist/common/ctrace.c (revision 13df4856)
1 /*	$NetBSD: ctrace.c,v 1.3 2022/04/03 01:10:58 christos Exp $	*/
2 
3 /* trace.c
4 
5    Subroutines that support dhcp tracing... */
6 
7 /*
8  * Copyright (C) 2004-2022 Internet Systems Consortium, Inc. ("ISC")
9  * Copyright (c) 2001-2003 by Internet Software Consortium
10  *
11  * This Source Code Form is subject to the terms of the Mozilla Public
12  * License, v. 2.0. If a copy of the MPL was not distributed with this
13  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
16  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
17  * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
18  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
21  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22  *
23  *   Internet Systems Consortium, Inc.
24  *   PO Box 360
25  *   Newmarket, NH 03857 USA
26  *   <info@isc.org>
27  *   https://www.isc.org/
28  *
29  */
30 
31 #include <sys/cdefs.h>
32 __RCSID("$NetBSD: ctrace.c,v 1.3 2022/04/03 01:10:58 christos Exp $");
33 
34 #include "dhcpd.h"
35 
36 #if defined (TRACING)
trace_interface_register(trace_type_t * ttype,struct interface_info * ip)37 void trace_interface_register (trace_type_t *ttype, struct interface_info *ip)
38 {
39 	trace_interface_packet_t tipkt;
40 
41 	if (trace_record ()) {
42 		memset (&tipkt, 0, sizeof tipkt);
43 		memcpy (&tipkt.hw_address,
44 			&ip -> hw_address, sizeof ip -> hw_address);
45 		if (ip->address_count)
46 			memcpy(&tipkt.primary_address,
47 			       ip->addresses, sizeof(*ip->addresses));
48 		memcpy (tipkt.name, ip -> name, sizeof ip -> name);
49 		tipkt.index = htonl (ip -> index);
50 
51 		trace_write_packet (ttype, sizeof tipkt, (char *)&tipkt, MDL);
52 	}
53 }
54 
trace_interface_input(trace_type_t * ttype,unsigned len,char * buf)55 void trace_interface_input (trace_type_t *ttype, unsigned len, char *buf)
56 {
57 	trace_interface_packet_t *tipkt;
58 	struct interface_info *ip;
59 	struct sockaddr_in *sin;
60 	struct iaddr addr;
61 	isc_result_t status;
62 
63 	if (len != sizeof *tipkt) {
64 		log_error ("trace interface packet size mismatch: %ld != %d",
65 			   (long)(sizeof *tipkt), len);
66 		return;
67 	}
68 	tipkt = (trace_interface_packet_t *)buf;
69 
70 	ip = (struct interface_info *)0;
71 	status = interface_allocate (&ip, MDL);
72 	if (status != ISC_R_SUCCESS) {
73 	      foo:
74 		log_error ("trace_interface_input: %s.",
75 			   isc_result_totext (status));
76 		return;
77 	}
78 	ip -> ifp = dmalloc (sizeof *(ip -> ifp), MDL);
79 	if (!ip -> ifp) {
80 		interface_dereference (&ip, MDL);
81 		status = ISC_R_NOMEMORY;
82 		goto foo;
83 	}
84 
85 	memcpy (&ip -> hw_address, &tipkt -> hw_address,
86 		sizeof ip -> hw_address);
87 	/* XXX: Without the full addresses state it's not quite a full
88 	 * trace.
89 	 */
90 	ip->address_count = ip->address_max = 1;
91 	ip->addresses = dmalloc(sizeof(*ip->addresses), MDL);
92 	if (!ip->addresses) {
93 		dfree(ip->ifp, MDL);
94 		ip->ifp = NULL;
95 		interface_dereference (&ip, MDL);
96 		status = ISC_R_NOMEMORY;
97 		goto foo;
98 	}
99 	memcpy(ip->addresses, &tipkt->primary_address, sizeof(*ip->addresses));
100 	memcpy (ip -> name, tipkt -> name, sizeof ip -> name);
101 	ip -> index = ntohl (tipkt -> index);
102 
103 	interface_snorf (ip, 0);
104 	if (dhcp_interface_discovery_hook)
105 		(*dhcp_interface_discovery_hook) (ip);
106 
107 	/* Fake up an ifp. */
108 	memcpy (ip -> ifp -> ifr_name, ip -> name, sizeof ip -> name);
109 #ifdef HAVE_SA_LEN
110 	ip -> ifp -> ifr_addr.sa_len = sizeof (struct sockaddr_in);
111 #endif
112 	sin = (struct sockaddr_in *)&ip -> ifp -> ifr_addr;
113 	sin->sin_addr = ip->addresses[0];
114 
115 	addr.len = 4;
116 	memcpy (addr.iabuf, &sin -> sin_addr.s_addr, addr.len);
117 	if (dhcp_interface_setup_hook)
118 		(*dhcp_interface_setup_hook) (ip, &addr);
119 	interface_stash (ip);
120 
121 	if (!quiet_interface_discovery) {
122 		log_info ("Listening on Trace/%s/%s%s%s",
123 			  ip -> name,
124 			  print_hw_addr (ip -> hw_address.hbuf [0],
125 					 ip -> hw_address.hlen - 1,
126 					 &ip -> hw_address.hbuf [1]),
127 			  (ip -> shared_network ? "/" : ""),
128 			  (ip -> shared_network ?
129 			   ip -> shared_network -> name : ""));
130 		if (strcmp (ip -> name, "fallback")) {
131 			log_info ("Sending   on Trace/%s/%s%s%s",
132 				  ip -> name,
133 				  print_hw_addr (ip -> hw_address.hbuf [0],
134 						 ip -> hw_address.hlen - 1,
135 						 &ip -> hw_address.hbuf [1]),
136 				  (ip -> shared_network ? "/" : ""),
137 				  (ip -> shared_network ?
138 				   ip -> shared_network -> name : ""));
139 		}
140 	}
141 	interface_dereference (&ip, MDL);
142 }
143 
trace_interface_stop(trace_type_t * ttype)144 void trace_interface_stop (trace_type_t *ttype) {
145 	/* XXX */
146 }
147 
trace_inpacket_stash(struct interface_info * interface,struct dhcp_packet * packet,unsigned len,unsigned int from_port,struct iaddr from,struct hardware * hfrom)148 void trace_inpacket_stash (struct interface_info *interface,
149 			   struct dhcp_packet *packet,
150 			   unsigned len,
151 			   unsigned int from_port,
152 			   struct iaddr from,
153 			   struct hardware *hfrom)
154 {
155 	trace_inpacket_t tip;
156 	trace_iov_t iov [2];
157 
158 	if (!trace_record ())
159 		return;
160 	tip.from_port = from_port;
161 	tip.from = from;
162 	tip.from.len = htonl (tip.from.len);
163 	if (hfrom) {
164 		tip.hfrom = *hfrom;
165 		tip.havehfrom = 1;
166 	} else {
167 		memset (&tip.hfrom, 0, sizeof tip.hfrom);
168 		tip.havehfrom = 0;
169 	}
170 	tip.index = htonl (interface -> index);
171 
172 	iov [0].buf = (char *)&tip;
173 	iov [0].len = sizeof tip;
174 	iov [1].buf = (char *)packet;
175 	iov [1].len = len;
176 	trace_write_packet_iov (inpacket_trace, 2, iov, MDL);
177 }
178 
trace_inpacket_input(trace_type_t * ttype,unsigned len,char * buf)179 void trace_inpacket_input (trace_type_t *ttype, unsigned len, char *buf)
180 {
181 	trace_inpacket_t *tip;
182 	int index;
183 
184 	if (len < sizeof *tip) {
185 		log_error ("trace_input_packet: too short - %d", len);
186 		return;
187 	}
188 	tip = (trace_inpacket_t *)buf;
189 	index = ntohl (tip -> index);
190 	tip -> from.len = ntohl (tip -> from.len);
191 
192 	if (index > interface_count ||
193 	    index < 0 ||
194 	    !interface_vector [index]) {
195 		log_error ("trace_input_packet: unknown interface index %d",
196 			   index);
197 		return;
198 	}
199 
200 	if (!bootp_packet_handler) {
201 		log_error ("trace_input_packet: no bootp packet handler.");
202 		return;
203 	}
204 
205 	(*bootp_packet_handler) (interface_vector [index],
206 				 (struct dhcp_packet *)(tip + 1),
207 				 len - sizeof *tip,
208 				 tip -> from_port,
209 				 tip -> from,
210 				 (tip -> havehfrom ?
211 				  &tip -> hfrom
212 				  : (struct hardware *)0));
213 }
214 
trace_inpacket_stop(trace_type_t * ttype)215 void trace_inpacket_stop (trace_type_t *ttype) { }
216 
trace_packet_send(struct interface_info * interface,struct packet * packet,struct dhcp_packet * raw,size_t len,struct in_addr from,struct sockaddr_in * to,struct hardware * hto)217 ssize_t trace_packet_send (struct interface_info *interface,
218 			   struct packet *packet,
219 			   struct dhcp_packet *raw,
220 			   size_t len,
221 			   struct in_addr from,
222 			   struct sockaddr_in *to,
223 			   struct hardware *hto)
224 {
225 	trace_outpacket_t tip;
226 	trace_iov_t iov [2];
227 
228 	if (trace_record ()) {
229 		if (hto) {
230 			tip.hto = *hto;
231 			tip.havehto = 1;
232 		} else {
233 			memset (&tip.hto, 0, sizeof tip.hto);
234 			tip.havehto = 0;
235 		}
236 		tip.from.len = 4;
237 		memcpy (tip.from.iabuf, &from, 4);
238 		tip.to.len = 4;
239 		memcpy (tip.to.iabuf, &to -> sin_addr, 4);
240 		tip.to_port = to -> sin_port;
241 		tip.index = htonl (interface -> index);
242 
243 		iov [0].buf = (char *)&tip;
244 		iov [0].len = sizeof tip;
245 		iov [1].buf = (char *)raw;
246 		iov [1].len = len;
247 		trace_write_packet_iov (outpacket_trace, 2, iov, MDL);
248 	}
249 	if (!trace_playback ()) {
250 		return send_packet (interface, packet, raw, len,
251 				    from, to, hto);
252 	}
253 	return len;
254 }
255 
trace_outpacket_input(trace_type_t * ttype,unsigned len,char * buf)256 void trace_outpacket_input (trace_type_t *ttype, unsigned len, char *buf)
257 {
258 	trace_outpacket_t *tip;
259 	int index;
260 
261 	if (len < sizeof *tip) {
262 		log_error ("trace_input_packet: too short - %d", len);
263 		return;
264 	}
265 	tip = (trace_outpacket_t *)buf;
266 	index = ntohl (tip -> index);
267 
268 	if (index > interface_count ||
269 	    index < 0 ||
270 	    !interface_vector [index]) {
271 		log_error ("trace_input_packet: unknown interface index %d",
272 			   index);
273 		return;
274 	}
275 
276 	/* XXX would be nice to somehow take notice of these. */
277 }
278 
trace_outpacket_stop(trace_type_t * ttype)279 void trace_outpacket_stop (trace_type_t *ttype) { }
280 
trace_seed_stash(trace_type_t * ttype,unsigned seed)281 void trace_seed_stash (trace_type_t *ttype, unsigned seed)
282 {
283 	u_int32_t outseed;
284 	if (!trace_record ())
285 		return;
286 	outseed = htonl (seed);
287 	trace_write_packet (ttype, sizeof outseed, (char *)&outseed, MDL);
288 	return;
289 }
290 
trace_seed_input(trace_type_t * ttype,unsigned length,char * buf)291 void trace_seed_input (trace_type_t *ttype, unsigned length, char *buf)
292 {
293 	u_int32_t *seed;
294 
295 	if (length != sizeof seed) {
296 		log_error ("trace_seed_input: wrong size (%d)", length);
297 	}
298 	seed = (u_int32_t *)buf;
299 	srandom (ntohl (*seed));
300 }
301 
trace_seed_stop(trace_type_t * ttype)302 void trace_seed_stop (trace_type_t *ttype) { }
303 #endif /* TRACING */
304