3 Subroutines that support dhcp tracing... */
6 * Copyright (c) 2001-2002 Internet Software Consortium.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of The Internet Software Consortium nor the names
19 * of its contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE INTERNET SOFTWARE CONSORTIUM AND
23 * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
24 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
25 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26 * DISCLAIMED. IN NO EVENT SHALL THE INTERNET SOFTWARE CONSORTIUM OR
27 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
30 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
31 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
32 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
33 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * This software has been written for the Internet Software Consortium
37 * by Ted Lemon, as part of a project for Nominum, Inc. To learn more
38 * about the Internet Software Consortium, see http://www.isc.org/. To
39 * learn more about Nominum, Inc., see ``http://www.nominum.com''.
45 void trace_interface_register (trace_type_t *ttype, struct interface_info *ip)
47 trace_interface_packet_t tipkt;
49 if (trace_record ()) {
50 memset (&tipkt, 0, sizeof tipkt);
51 memcpy (&tipkt.hw_address,
52 &ip -> hw_address, sizeof ip -> hw_address);
53 memcpy (&tipkt.primary_address,
54 &ip -> primary_address, sizeof ip -> primary_address);
55 memcpy (tipkt.name, ip -> name, sizeof ip -> name);
56 tipkt.index = htonl (ip -> index);
58 trace_write_packet (ttype, sizeof tipkt, (char *)&tipkt, MDL);
62 void trace_interface_input (trace_type_t *ttype, unsigned len, char *buf)
64 trace_interface_packet_t *tipkt;
65 struct interface_info *ip;
66 struct sockaddr_in *sin;
70 if (len != sizeof *tipkt) {
71 log_error ("trace interface packet size mismatch: %ld != %d",
72 (long)(sizeof *tipkt), len);
75 tipkt = (trace_interface_packet_t *)buf;
77 ip = (struct interface_info *)0;
78 status = interface_allocate (&ip, MDL);
79 if (status != ISC_R_SUCCESS) {
81 log_error ("trace_interface_input: %s.",
82 isc_result_totext (status));
85 ip -> ifp = dmalloc (sizeof *(ip -> ifp), MDL);
87 interface_dereference (&ip, MDL);
88 status = ISC_R_NOMEMORY;
92 memcpy (&ip -> hw_address, &tipkt -> hw_address,
93 sizeof ip -> hw_address);
94 memcpy (&ip -> primary_address, &tipkt -> primary_address,
95 sizeof ip -> primary_address);
96 memcpy (ip -> name, tipkt -> name, sizeof ip -> name);
97 ip -> index = ntohl (tipkt -> index);
99 interface_snorf (ip, 0);
100 if (dhcp_interface_discovery_hook)
101 (*dhcp_interface_discovery_hook) (ip);
103 /* Fake up an ifp. */
104 memcpy (ip -> ifp -> ifr_name, ip -> name, sizeof ip -> name);
106 ip -> ifp -> ifr_addr.sa_len = sizeof (struct sockaddr_in);
108 sin = (struct sockaddr_in *)&ip -> ifp -> ifr_addr;
109 sin -> sin_addr = ip -> primary_address;
112 memcpy (addr.iabuf, &sin -> sin_addr.s_addr, addr.len);
113 if (dhcp_interface_setup_hook)
114 (*dhcp_interface_setup_hook) (ip, &addr);
115 interface_stash (ip);
117 if (!quiet_interface_discovery) {
118 log_info ("Listening on Trace/%s/%s%s%s",
120 print_hw_addr (ip -> hw_address.hbuf [0],
121 ip -> hw_address.hlen - 1,
122 &ip -> hw_address.hbuf [1]),
123 (ip -> shared_network ? "/" : ""),
124 (ip -> shared_network ?
125 ip -> shared_network -> name : ""));
126 if (strcmp (ip -> name, "fallback")) {
127 log_info ("Sending on Trace/%s/%s%s%s",
129 print_hw_addr (ip -> hw_address.hbuf [0],
130 ip -> hw_address.hlen - 1,
131 &ip -> hw_address.hbuf [1]),
132 (ip -> shared_network ? "/" : ""),
133 (ip -> shared_network ?
134 ip -> shared_network -> name : ""));
137 interface_dereference (&ip, MDL);
140 void trace_interface_stop (trace_type_t *ttype) {
144 void trace_inpacket_stash (struct interface_info *interface,
145 struct dhcp_packet *packet,
147 unsigned int from_port,
149 struct hardware *hfrom)
151 trace_inpacket_t tip;
154 if (!trace_record ())
156 tip.from_port = from_port;
162 memset (&tip.hfrom, 0, sizeof tip.hfrom);
165 tip.index = htonl (interface -> index);
167 iov [0].buf = (char *)&tip;
168 iov [0].len = sizeof tip;
169 iov [1].buf = (char *)packet;
171 trace_write_packet_iov (inpacket_trace, 2, iov, MDL);
174 void trace_inpacket_input (trace_type_t *ttype, unsigned len, char *buf)
176 trace_inpacket_t *tip;
179 if (len < sizeof *tip) {
180 log_error ("trace_input_packet: too short - %d", len);
183 tip = (trace_inpacket_t *)buf;
184 index = ntohl (tip -> index);
186 if (index > interface_count ||
188 !interface_vector [index]) {
189 log_error ("trace_input_packet: unknown interface index %d",
194 if (!bootp_packet_handler) {
195 log_error ("trace_input_packet: no bootp packet handler.");
199 (*bootp_packet_handler) (interface_vector [index],
200 (struct dhcp_packet *)(tip + 1),
206 : (struct hardware *)0));
209 void trace_inpacket_stop (trace_type_t *ttype) { }
211 ssize_t trace_packet_send (struct interface_info *interface,
212 struct packet *packet,
213 struct dhcp_packet *raw,
216 struct sockaddr_in *to,
217 struct hardware *hto)
219 trace_outpacket_t tip;
222 if (trace_record ()) {
227 memset (&tip.hto, 0, sizeof tip.hto);
231 memcpy (tip.from.iabuf, &from, 4);
233 memcpy (tip.to.iabuf, &to -> sin_addr, 4);
234 tip.to_port = to -> sin_port;
235 tip.index = htonl (interface -> index);
237 iov [0].buf = (char *)&tip;
238 iov [0].len = sizeof tip;
239 iov [1].buf = (char *)raw;
241 trace_write_packet_iov (outpacket_trace, 2, iov, MDL);
243 if (!trace_playback ()) {
244 return send_packet (interface, packet, raw, len,
250 void trace_outpacket_input (trace_type_t *ttype, unsigned len, char *buf)
252 trace_outpacket_t *tip;
255 if (len < sizeof *tip) {
256 log_error ("trace_input_packet: too short - %d", len);
259 tip = (trace_outpacket_t *)buf;
260 index = ntohl (tip -> index);
262 if (index > interface_count ||
264 !interface_vector [index]) {
265 log_error ("trace_input_packet: unknown interface index %d",
270 /* XXX would be nice to somehow take notice of these. */
273 void trace_outpacket_stop (trace_type_t *ttype) { }
275 void trace_seed_stash (trace_type_t *ttype, unsigned seed)
278 if (!trace_record ())
280 outseed = htonl (seed);
281 trace_write_packet (ttype, sizeof outseed, (char *)&outseed, MDL);
285 void trace_seed_input (trace_type_t *ttype, unsigned length, char *buf)
289 if (length != sizeof seed) {
290 log_error ("trace_seed_input: wrong size (%d)", length);
292 seed = (u_int32_t *)buf;
293 srandom (ntohl (*seed));
296 void trace_seed_stop (trace_type_t *ttype) { }