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