~ubuntu-branches/ubuntu/oneiric/isc-dhcp/oneiric-security

« back to all changes in this revision

Viewing changes to common/icmp.c

  • Committer: Bazaar Package Importer
  • Author(s): Andrew Pollock
  • Date: 2009-09-02 22:34:25 UTC
  • Revision ID: james.westby@ubuntu.com-20090902223425-nypo7bkftxffq41m
Tags: upstream-4.1.0
ImportĀ upstreamĀ versionĀ 4.1.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* dhcp.c
 
2
 
 
3
   ICMP Protocol engine - for sending out pings and receiving
 
4
   responses. */
 
5
 
 
6
/*
 
7
 * Copyright (c) 2004,2007 by Internet Systems Consortium, Inc. ("ISC")
 
8
 * Copyright (c) 1996-2003 by Internet Software Consortium
 
9
 *
 
10
 * Permission to use, copy, modify, and distribute this software for any
 
11
 * purpose with or without fee is hereby granted, provided that the above
 
12
 * copyright notice and this permission notice appear in all copies.
 
13
 *
 
14
 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
 
15
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 
16
 * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
 
17
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 
18
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 
19
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
 
20
 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 
21
 *
 
22
 *   Internet Systems Consortium, Inc.
 
23
 *   950 Charter Street
 
24
 *   Redwood City, CA 94063
 
25
 *   <info@isc.org>
 
26
 *   http://www.isc.org/
 
27
 *
 
28
 * This software has been written for Internet Systems Consortium
 
29
 * by Ted Lemon in cooperation with Vixie Enterprises and Nominum, Inc.
 
30
 * To learn more about Internet Systems Consortium, see
 
31
 * ``http://www.isc.org/''.  To learn more about Vixie Enterprises,
 
32
 * see ``http://www.vix.com''.   To learn more about Nominum, Inc., see
 
33
 * ``http://www.nominum.com''.
 
34
 */
 
35
 
 
36
#include "dhcpd.h"
 
37
#include "netinet/ip.h"
 
38
#include "netinet/ip_icmp.h"
 
39
 
 
40
struct icmp_state *icmp_state;
 
41
static omapi_object_type_t *dhcp_type_icmp;
 
42
static int no_icmp;
 
43
 
 
44
OMAPI_OBJECT_ALLOC (icmp_state, struct icmp_state, dhcp_type_icmp)
 
45
 
 
46
#if defined (TRACING)
 
47
trace_type_t *trace_icmp_input;
 
48
trace_type_t *trace_icmp_output;
 
49
#endif
 
50
 
 
51
/* Initialize the ICMP protocol. */
 
52
 
 
53
void icmp_startup (routep, handler)
 
54
        int routep;
 
55
        void (*handler) PROTO ((struct iaddr, u_int8_t *, int));
 
56
{
 
57
        struct protoent *proto;
 
58
        int protocol = 1;
 
59
        int state;
 
60
        isc_result_t result;
 
61
 
 
62
        /* Only initialize icmp once. */
 
63
        if (dhcp_type_icmp)
 
64
                log_fatal ("attempted to reinitialize icmp protocol");
 
65
 
 
66
        result = omapi_object_type_register (&dhcp_type_icmp, "icmp",
 
67
                                             0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
 
68
                                             sizeof (struct icmp_state),
 
69
                                             0, RC_MISC);
 
70
 
 
71
        if (result != ISC_R_SUCCESS)
 
72
                log_fatal ("Can't register icmp object type: %s",
 
73
                           isc_result_totext (result));
 
74
 
 
75
        icmp_state_allocate (&icmp_state, MDL);
 
76
        icmp_state -> icmp_handler = handler;
 
77
 
 
78
#if defined (TRACING)
 
79
        trace_icmp_input = trace_type_register ("icmp-input", (void *)0,
 
80
                                                trace_icmp_input_input,
 
81
                                                trace_icmp_input_stop, MDL);
 
82
        trace_icmp_output = trace_type_register ("icmp-output", (void *)0,
 
83
                                                 trace_icmp_output_input,
 
84
                                                 trace_icmp_output_stop, MDL);
 
85
 
 
86
        /* If we're playing back a trace file, don't create the socket
 
87
           or set up the callback. */
 
88
        if (!trace_playback ()) {
 
89
#endif
 
90
                /* Get the protocol number (should be 1). */
 
91
                proto = getprotobyname ("icmp");
 
92
                if (proto)
 
93
                        protocol = proto -> p_proto;
 
94
                
 
95
                /* Get a raw socket for the ICMP protocol. */
 
96
                icmp_state -> socket = socket (AF_INET, SOCK_RAW, protocol);
 
97
                if (icmp_state -> socket < 0) {
 
98
                        no_icmp = 1;
 
99
                        log_error ("unable to create icmp socket: %m");
 
100
                        return;
 
101
                }
 
102
 
 
103
#if defined (HAVE_SETFD)
 
104
                if (fcntl (icmp_state -> socket, F_SETFD, 1) < 0)
 
105
                        log_error ("Can't set close-on-exec on icmp: %m");
 
106
#endif
 
107
 
 
108
                /* Make sure it does routing... */
 
109
                state = 0;
 
110
                if (setsockopt (icmp_state -> socket, SOL_SOCKET, SO_DONTROUTE,
 
111
                                (char *)&state, sizeof state) < 0)
 
112
                        log_fatal ("Can't disable SO_DONTROUTE on ICMP: %m");
 
113
 
 
114
                result = (omapi_register_io_object
 
115
                          ((omapi_object_t *)icmp_state,
 
116
                           icmp_readsocket, 0, icmp_echoreply, 0, 0));
 
117
                if (result != ISC_R_SUCCESS)
 
118
                        log_fatal ("Can't register icmp handle: %s",
 
119
                                   isc_result_totext (result));
 
120
#if defined (TRACING)
 
121
        }
 
122
#endif
 
123
}
 
124
 
 
125
int icmp_readsocket (h)
 
126
        omapi_object_t *h;
 
127
{
 
128
        struct icmp_state *state;
 
129
 
 
130
        state = (struct icmp_state *)h;
 
131
        return state -> socket;
 
132
}
 
133
 
 
134
int icmp_echorequest (addr)
 
135
        struct iaddr *addr;
 
136
{
 
137
        struct sockaddr_in to;
 
138
        struct icmp icmp;
 
139
        int status;
 
140
#if defined (TRACING)
 
141
        trace_iov_t iov [2];
 
142
#endif
 
143
 
 
144
        if (no_icmp)
 
145
                return 1;
 
146
        if (!icmp_state)
 
147
                log_fatal ("ICMP protocol used before initialization.");
 
148
 
 
149
        memset (&to, 0, sizeof(to));
 
150
#ifdef HAVE_SA_LEN
 
151
        to.sin_len = sizeof to;
 
152
#endif
 
153
        to.sin_family = AF_INET;
 
154
        to.sin_port = 0; /* unused. */
 
155
        memcpy (&to.sin_addr, addr -> iabuf, sizeof to.sin_addr); /* XXX */
 
156
 
 
157
        icmp.icmp_type = ICMP_ECHO;
 
158
        icmp.icmp_code = 0;
 
159
        icmp.icmp_cksum = 0;
 
160
        icmp.icmp_seq = 0;
 
161
#if SIZEOF_STRUCT_IADDR_P == 8
 
162
        icmp.icmp_id = (((u_int32_t)(u_int64_t)addr) ^
 
163
                        (u_int32_t)(((u_int64_t)addr) >> 32));
 
164
#else
 
165
        icmp.icmp_id = (u_int32_t)addr;
 
166
#endif
 
167
        memset (&icmp.icmp_dun, 0, sizeof icmp.icmp_dun);
 
168
 
 
169
        icmp.icmp_cksum = wrapsum (checksum ((unsigned char *)&icmp,
 
170
                                             sizeof icmp, 0));
 
171
 
 
172
#if defined (TRACING)
 
173
        if (trace_playback ()) {
 
174
                char *buf = (char *)0;
 
175
                unsigned buflen = 0;
 
176
 
 
177
                /* Consume the ICMP event. */
 
178
                status = trace_get_packet (&trace_icmp_output, &buflen, &buf);
 
179
                if (status != ISC_R_SUCCESS)
 
180
                        log_error ("icmp_echorequest: %s",
 
181
                                   isc_result_totext (status));
 
182
                if (buf)
 
183
                        dfree (buf, MDL);
 
184
        } else {
 
185
                if (trace_record ()) {
 
186
                        iov [0].buf = (char *)addr;
 
187
                        iov [0].len = sizeof *addr;
 
188
                        iov [1].buf = (char *)&icmp;
 
189
                        iov [1].len = sizeof icmp;
 
190
                        trace_write_packet_iov (trace_icmp_output,
 
191
                                                2, iov, MDL);
 
192
                }
 
193
#endif
 
194
                /* Send the ICMP packet... */
 
195
                status = sendto (icmp_state -> socket,
 
196
                                 (char *)&icmp, sizeof icmp, 0,
 
197
                                 (struct sockaddr *)&to, sizeof to);
 
198
                if (status < 0)
 
199
                        log_error ("icmp_echorequest %s: %m",
 
200
                                   inet_ntoa(to.sin_addr));
 
201
 
 
202
                if (status != sizeof icmp)
 
203
                        return 0;
 
204
#if defined (TRACING)
 
205
        }
 
206
#endif
 
207
        return 1;
 
208
}
 
209
 
 
210
isc_result_t icmp_echoreply (h)
 
211
        omapi_object_t *h;
 
212
{
 
213
        struct icmp *icfrom;
 
214
        struct ip *ip;
 
215
        struct sockaddr_in from;
 
216
        u_int8_t icbuf [1500];
 
217
        int status;
 
218
        SOCKLEN_T sl;
 
219
        int hlen, len;
 
220
        struct iaddr ia;
 
221
        struct icmp_state *state;
 
222
#if defined (TRACING)
 
223
        trace_iov_t iov [2];
 
224
#endif
 
225
 
 
226
        state = (struct icmp_state *)h;
 
227
 
 
228
        sl = sizeof from;
 
229
        status = recvfrom (state -> socket, (char *)icbuf, sizeof icbuf, 0,
 
230
                          (struct sockaddr *)&from, &sl);
 
231
        if (status < 0) {
 
232
                log_error ("icmp_echoreply: %m");
 
233
                return ISC_R_UNEXPECTED;
 
234
        }
 
235
 
 
236
        /* Find the IP header length... */
 
237
        ip = (struct ip *)icbuf;
 
238
        hlen = IP_HL (ip);
 
239
 
 
240
        /* Short packet? */
 
241
        if (status < hlen + (sizeof *icfrom)) {
 
242
                return ISC_R_SUCCESS;
 
243
        }
 
244
 
 
245
        len = status - hlen;
 
246
        icfrom = (struct icmp *)(icbuf + hlen);
 
247
 
 
248
        /* Silently discard ICMP packets that aren't echoreplies. */
 
249
        if (icfrom -> icmp_type != ICMP_ECHOREPLY) {
 
250
                return ISC_R_SUCCESS;
 
251
        }
 
252
 
 
253
        /* If we were given a second-stage handler, call it. */
 
254
        if (state -> icmp_handler) {
 
255
                memcpy (ia.iabuf, &from.sin_addr, sizeof from.sin_addr);
 
256
                ia.len = sizeof from.sin_addr;
 
257
 
 
258
#if defined (TRACING)
 
259
                if (trace_record ()) {
 
260
                        ia.len = htonl(ia.len);
 
261
                        iov [0].buf = (char *)&ia;
 
262
                        iov [0].len = sizeof ia;
 
263
                        iov [1].buf = (char *)icbuf;
 
264
                        iov [1].len = len;
 
265
                        trace_write_packet_iov (trace_icmp_input, 2, iov, MDL);
 
266
                        ia.len = ntohl(ia.len);
 
267
                }
 
268
#endif
 
269
                (*state -> icmp_handler) (ia, icbuf, len);
 
270
        }
 
271
        return ISC_R_SUCCESS;
 
272
}
 
273
 
 
274
#if defined (TRACING)
 
275
void trace_icmp_input_input (trace_type_t *ttype, unsigned length, char *buf)
 
276
{
 
277
        struct iaddr *ia;
 
278
        u_int8_t *icbuf;
 
279
        ia = (struct iaddr *)buf;
 
280
        ia->len = ntohl(ia->len);
 
281
        icbuf = (u_int8_t *)(ia + 1);
 
282
        if (icmp_state -> icmp_handler)
 
283
                (*icmp_state -> icmp_handler) (*ia, icbuf,
 
284
                                               (int)(length - sizeof ia));
 
285
}
 
286
 
 
287
void trace_icmp_input_stop (trace_type_t *ttype) { }
 
288
 
 
289
void trace_icmp_output_input (trace_type_t *ttype, unsigned length, char *buf)
 
290
{
 
291
        struct icmp *icmp;
 
292
        struct iaddr ia;
 
293
 
 
294
        if (length != (sizeof (*icmp) + (sizeof ia))) {
 
295
                log_error ("trace_icmp_output_input: data size mismatch %d:%d",
 
296
                           length, (int)((sizeof (*icmp)) + (sizeof ia)));
 
297
                return;
 
298
        }
 
299
        ia.len = 4;
 
300
        memcpy (ia.iabuf, buf, 4);
 
301
        icmp = (struct icmp *)(buf + 1);
 
302
 
 
303
        log_error ("trace_icmp_output_input: unsent ping to %s", piaddr (ia));
 
304
}
 
305
 
 
306
void trace_icmp_output_stop (trace_type_t *ttype) { }
 
307
#endif /* TRACING */