1 6b5c4000 2026-03-02 rsadowski /* $OpenBSD: check_icmp.c,v 1.49 2026/03/02 19:28:01 rsadowski Exp $ */
4 8c736326 2007-09-28 pyr * Copyright (c) 2006 Pierre-Yves Ritschard <pyr@openbsd.org>
6 41042ecc 2006-12-16 reyk * Permission to use, copy, modify, and distribute this software for any
7 41042ecc 2006-12-16 reyk * purpose with or without fee is hereby granted, provided that the above
8 41042ecc 2006-12-16 reyk * copyright notice and this permission notice appear in all copies.
10 41042ecc 2006-12-16 reyk * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 41042ecc 2006-12-16 reyk * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 41042ecc 2006-12-16 reyk * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 41042ecc 2006-12-16 reyk * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 41042ecc 2006-12-16 reyk * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 41042ecc 2006-12-16 reyk * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 41042ecc 2006-12-16 reyk * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 41042ecc 2006-12-16 reyk #include <sys/types.h>
20 41042ecc 2006-12-16 reyk #include <sys/queue.h>
21 41042ecc 2006-12-16 reyk #include <sys/socket.h>
22 446e8f12 2009-08-07 reyk #include <sys/sysctl.h>
23 d93c36af 2015-01-22 reyk #include <sys/time.h>
25 41042ecc 2006-12-16 reyk #include <netinet/in.h>
26 41042ecc 2006-12-16 reyk #include <netinet/ip.h>
27 41042ecc 2006-12-16 reyk #include <netinet/ip_icmp.h>
28 41042ecc 2006-12-16 reyk #include <netinet/icmp6.h>
29 d93c36af 2015-01-22 reyk #include <arpa/inet.h>
31 41042ecc 2006-12-16 reyk #include <event.h>
32 41042ecc 2006-12-16 reyk #include <errno.h>
33 41042ecc 2006-12-16 reyk #include <unistd.h>
34 41042ecc 2006-12-16 reyk #include <string.h>
35 ae98db81 2006-12-25 reyk #include <stdlib.h>
37 6d0767ae 2007-12-07 reyk #include "relayd.h"
38 6b5c4000 2026-03-02 rsadowski #include "log.h"
40 6d0767ae 2007-12-07 reyk void icmp_setup(struct relayd *, struct ctl_icmp_event *, int);
41 816ddb53 2007-01-11 reyk void check_icmp_add(struct ctl_icmp_event *, int, struct timeval *,
42 816ddb53 2007-01-11 reyk void (*)(int, short, void *));
43 816ddb53 2007-01-11 reyk int icmp_checks_done(struct ctl_icmp_event *);
44 b2665266 2008-12-05 reyk void icmp_checks_timeout(struct ctl_icmp_event *, enum host_error);
45 816ddb53 2007-01-11 reyk void send_icmp(int, short, void *);
46 816ddb53 2007-01-11 reyk void recv_icmp(int, short, void *);
47 ae98db81 2006-12-25 reyk int in_cksum(u_short *, int);
50 6d0767ae 2007-12-07 reyk icmp_setup(struct relayd *env, struct ctl_icmp_event *cie, int af)
52 779fb716 2015-08-21 yasuoka int proto = IPPROTO_ICMP, val;
54 816ddb53 2007-01-11 reyk if (af == AF_INET6)
55 816ddb53 2007-01-11 reyk proto = IPPROTO_ICMPV6;
56 e475725e 2019-06-28 deraadt if ((cie->s = socket(af, SOCK_RAW | SOCK_NONBLOCK, proto)) == -1)
57 09e53fee 2017-05-28 benno fatal("%s: socket", __func__);
58 779fb716 2015-08-21 yasuoka val = ICMP_RCVBUF_SIZE;
59 779fb716 2015-08-21 yasuoka if (setsockopt(cie->s, SOL_SOCKET, SO_RCVBUF, &val, sizeof(val)) == -1)
60 09e53fee 2017-05-28 benno fatal("%s: setsockopt", __func__);
61 816ddb53 2007-01-11 reyk cie->env = env;
62 816ddb53 2007-01-11 reyk cie->af = af;
66 6d0767ae 2007-12-07 reyk icmp_init(struct relayd *env)
68 23fba4ad 2008-01-31 reyk icmp_setup(env, &env->sc_icmp_send, AF_INET);
69 23fba4ad 2008-01-31 reyk icmp_setup(env, &env->sc_icmp_recv, AF_INET);
70 23fba4ad 2008-01-31 reyk icmp_setup(env, &env->sc_icmp6_send, AF_INET6);
71 23fba4ad 2008-01-31 reyk icmp_setup(env, &env->sc_icmp6_recv, AF_INET6);
72 23fba4ad 2008-01-31 reyk env->sc_id = getpid() & 0xffff;
76 6d0767ae 2007-12-07 reyk schedule_icmp(struct relayd *env, struct host *host)
78 816ddb53 2007-01-11 reyk host->last_up = host->up;
79 816ddb53 2007-01-11 reyk host->flags &= ~(F_CHECK_SENT|F_CHECK_DONE);
81 87178699 2007-05-27 pyr if (((struct sockaddr *)&host->conf.ss)->sa_family == AF_INET)
82 23fba4ad 2008-01-31 reyk env->sc_has_icmp = 1;
84 23fba4ad 2008-01-31 reyk env->sc_has_icmp6 = 1;
88 816ddb53 2007-01-11 reyk check_icmp_add(struct ctl_icmp_event *cie, int flags, struct timeval *start,
89 816ddb53 2007-01-11 reyk void (*fn)(int, short, void *))
91 816ddb53 2007-01-11 reyk struct timeval tv;
93 816ddb53 2007-01-11 reyk if (start != NULL)
94 816ddb53 2007-01-11 reyk bcopy(start, &cie->tv_start, sizeof(cie->tv_start));
95 58fc320c 2016-09-02 reyk bcopy(&cie->env->sc_conf.timeout, &tv, sizeof(tv));
96 b7f7a948 2013-03-10 reyk getmonotime(&cie->tv_start);
97 816ddb53 2007-01-11 reyk event_del(&cie->ev);
98 816ddb53 2007-01-11 reyk event_set(&cie->ev, cie->s, EV_TIMEOUT|flags, fn, cie);
99 816ddb53 2007-01-11 reyk event_add(&cie->ev, &tv);
103 6d0767ae 2007-12-07 reyk check_icmp(struct relayd *env, struct timeval *tv)
105 23fba4ad 2008-01-31 reyk if (env->sc_has_icmp) {
106 23fba4ad 2008-01-31 reyk check_icmp_add(&env->sc_icmp_recv, EV_READ, tv, recv_icmp);
107 23fba4ad 2008-01-31 reyk check_icmp_add(&env->sc_icmp_send, EV_WRITE, tv, send_icmp);
109 23fba4ad 2008-01-31 reyk if (env->sc_has_icmp6) {
110 23fba4ad 2008-01-31 reyk check_icmp_add(&env->sc_icmp6_recv, EV_READ, tv, recv_icmp);
111 23fba4ad 2008-01-31 reyk check_icmp_add(&env->sc_icmp6_send, EV_WRITE, tv, send_icmp);
116 816ddb53 2007-01-11 reyk icmp_checks_done(struct ctl_icmp_event *cie)
118 ae98db81 2006-12-25 reyk struct table *table;
119 ae98db81 2006-12-25 reyk struct host *host;
121 23fba4ad 2008-01-31 reyk TAILQ_FOREACH(table, cie->env->sc_tables, entry) {
122 87178699 2007-05-27 pyr if (table->conf.flags & F_DISABLE ||
123 87178699 2007-05-27 pyr table->conf.check != CHECK_ICMP)
125 ae98db81 2006-12-25 reyk TAILQ_FOREACH(host, &table->hosts, entry) {
126 87178699 2007-05-27 pyr if (((struct sockaddr *)&host->conf.ss)->sa_family !=
129 ae98db81 2006-12-25 reyk if (!(host->flags & F_CHECK_DONE))
130 ae98db81 2006-12-25 reyk return (0);
133 ae98db81 2006-12-25 reyk return (1);
137 b2665266 2008-12-05 reyk icmp_checks_timeout(struct ctl_icmp_event *cie, enum host_error he)
139 ae98db81 2006-12-25 reyk struct table *table;
140 ae98db81 2006-12-25 reyk struct host *host;
142 23fba4ad 2008-01-31 reyk TAILQ_FOREACH(table, cie->env->sc_tables, entry) {
143 87178699 2007-05-27 pyr if (table->conf.flags & F_DISABLE ||
144 87178699 2007-05-27 pyr table->conf.check != CHECK_ICMP)
146 ae98db81 2006-12-25 reyk TAILQ_FOREACH(host, &table->hosts, entry) {
147 87178699 2007-05-27 pyr if (((struct sockaddr *)&host->conf.ss)->sa_family !=
150 4bd84ef0 2009-08-14 reyk if (!(host->flags & (F_CHECK_DONE|F_DISABLE))) {
151 816ddb53 2007-01-11 reyk host->up = HOST_DOWN;
152 b2665266 2008-12-05 reyk hce_notify_done(host, he);
159 816ddb53 2007-01-11 reyk send_icmp(int s, short event, void *arg)
161 6e65a975 2013-01-17 bluhm struct ctl_icmp_event *cie = arg;
162 816ddb53 2007-01-11 reyk struct table *table;
163 816ddb53 2007-01-11 reyk struct host *host;
164 ea9b7d20 2006-12-16 reyk struct sockaddr *to;
165 816ddb53 2007-01-11 reyk struct icmp *icp;
166 816ddb53 2007-01-11 reyk struct icmp6_hdr *icp6;
168 9920aa41 2007-01-09 reyk u_char packet[ICMP_BUF_SIZE];
169 073d0cf1 2015-10-23 benno socklen_t slen, len;
170 073d0cf1 2015-10-23 benno int i = 0, ttl;
171 5c4f74ea 2012-09-19 reyk u_int32_t id;
173 816ddb53 2007-01-11 reyk if (event == EV_TIMEOUT) {
174 b2665266 2008-12-05 reyk icmp_checks_timeout(cie, HCE_ICMP_WRITE_TIMEOUT);
178 41042ecc 2006-12-16 reyk bzero(&packet, sizeof(packet));
179 41042ecc 2006-12-16 reyk icp = (struct icmp *)packet;
180 816ddb53 2007-01-11 reyk icp6 = (struct icmp6_hdr *)packet;
181 816ddb53 2007-01-11 reyk if (cie->af == AF_INET) {
182 816ddb53 2007-01-11 reyk icp->icmp_type = ICMP_ECHO;
183 816ddb53 2007-01-11 reyk icp->icmp_code = 0;
184 23fba4ad 2008-01-31 reyk icp->icmp_id = htons(cie->env->sc_id);
185 816ddb53 2007-01-11 reyk icp->icmp_cksum = 0;
186 816ddb53 2007-01-11 reyk slen = sizeof(struct sockaddr_in);
188 816ddb53 2007-01-11 reyk icp6->icmp6_type = ICMP6_ECHO_REQUEST;
189 816ddb53 2007-01-11 reyk icp6->icmp6_code = 0;
190 816ddb53 2007-01-11 reyk icp6->icmp6_cksum = 0;
191 23fba4ad 2008-01-31 reyk icp6->icmp6_id = htons(cie->env->sc_id);
192 816ddb53 2007-01-11 reyk slen = sizeof(struct sockaddr_in6);
195 23fba4ad 2008-01-31 reyk TAILQ_FOREACH(table, cie->env->sc_tables, entry) {
196 87178699 2007-05-27 pyr if (table->conf.check != CHECK_ICMP ||
197 87178699 2007-05-27 pyr table->conf.flags & F_DISABLE)
199 816ddb53 2007-01-11 reyk TAILQ_FOREACH(host, &table->hosts, entry) {
200 17acabee 2008-07-19 reyk if (host->flags & (F_DISABLE | F_CHECK_SENT) ||
201 17acabee 2008-07-19 reyk host->conf.parentid)
203 87178699 2007-05-27 pyr if (((struct sockaddr *)&host->conf.ss)->sa_family !=
207 87178699 2007-05-27 pyr to = (struct sockaddr *)&host->conf.ss;
208 5c4f74ea 2012-09-19 reyk id = htonl(host->conf.id);
210 816ddb53 2007-01-11 reyk if (cie->af == AF_INET) {
211 816ddb53 2007-01-11 reyk icp->icmp_seq = htons(i);
212 816ddb53 2007-01-11 reyk icp->icmp_cksum = 0;
213 5c4f74ea 2012-09-19 reyk icp->icmp_mask = id;
214 816ddb53 2007-01-11 reyk icp->icmp_cksum = in_cksum((u_short *)icp,
215 816ddb53 2007-01-11 reyk sizeof(packet));
217 816ddb53 2007-01-11 reyk icp6->icmp6_seq = htons(i);
218 816ddb53 2007-01-11 reyk icp6->icmp6_cksum = 0;
219 5c4f74ea 2012-09-19 reyk memcpy(packet + sizeof(*icp6), &id, sizeof(id));
220 816ddb53 2007-01-11 reyk icp6->icmp6_cksum = in_cksum((u_short *)icp6,
221 816ddb53 2007-01-11 reyk sizeof(packet));
224 3d2b3138 2017-07-12 jca ttl = host->conf.ttl;
225 85a9c342 2017-07-11 florian switch(cie->af) {
226 85a9c342 2017-07-11 florian case AF_INET:
227 3d2b3138 2017-07-12 jca if (ttl > 0) {
228 85a9c342 2017-07-11 florian if (setsockopt(s, IPPROTO_IP, IP_TTL,
229 3d2b3138 2017-07-12 jca &ttl, sizeof(ttl)) == -1)
230 85a9c342 2017-07-11 florian log_warn("%s: setsockopt",
231 85a9c342 2017-07-11 florian __func__);
232 85a9c342 2017-07-11 florian } else {
233 85a9c342 2017-07-11 florian /* Revert to default TTL */
234 85a9c342 2017-07-11 florian len = sizeof(ttl);
235 85a9c342 2017-07-11 florian if (getsockopt(s, IPPROTO_IP,
236 85a9c342 2017-07-11 florian IP_IPDEFTTL, &ttl, &len) == 0) {
237 85a9c342 2017-07-11 florian if (setsockopt(s, IPPROTO_IP,
238 85a9c342 2017-07-11 florian IP_TTL, &ttl, len) == -1)
239 85a9c342 2017-07-11 florian log_warn(
240 85a9c342 2017-07-11 florian "%s: setsockopt",
241 85a9c342 2017-07-11 florian __func__);
243 85a9c342 2017-07-11 florian log_warn("%s: getsockopt",
244 85a9c342 2017-07-11 florian __func__);
247 85a9c342 2017-07-11 florian case AF_INET6:
248 3d2b3138 2017-07-12 jca if (ttl > 0) {
249 85a9c342 2017-07-11 florian if (setsockopt(s, IPPROTO_IPV6,
250 3d2b3138 2017-07-12 jca IPV6_UNICAST_HOPS, &ttl,
251 3d2b3138 2017-07-12 jca sizeof(ttl)) == -1)
252 85a9c342 2017-07-11 florian log_warn("%s: setsockopt",
253 85a9c342 2017-07-11 florian __func__);
254 85a9c342 2017-07-11 florian } else {
255 85a9c342 2017-07-11 florian /* Revert to default hop limit */
256 85a9c342 2017-07-11 florian ttl = -1;
257 85a9c342 2017-07-11 florian if (setsockopt(s, IPPROTO_IPV6,
258 85a9c342 2017-07-11 florian IPV6_UNICAST_HOPS, &ttl,
259 3d2b3138 2017-07-12 jca sizeof(ttl)) == -1)
260 85a9c342 2017-07-11 florian log_warn("%s: setsockopt",
261 85a9c342 2017-07-11 florian __func__);
266 816ddb53 2007-01-11 reyk r = sendto(s, packet, sizeof(packet), 0, to, slen);
267 816ddb53 2007-01-11 reyk if (r == -1) {
268 d641fae2 2007-07-16 pyr if (errno == EAGAIN || errno == EINTR)
269 816ddb53 2007-01-11 reyk goto retry;
270 816ddb53 2007-01-11 reyk host->flags |= F_CHECK_SENT|F_CHECK_DONE;
271 816ddb53 2007-01-11 reyk host->up = HOST_DOWN;
272 816ddb53 2007-01-11 reyk } else if (r != sizeof(packet))
273 816ddb53 2007-01-11 reyk goto retry;
274 816ddb53 2007-01-11 reyk host->flags |= F_CHECK_SENT;
281 816ddb53 2007-01-11 reyk event_again(&cie->ev, s, EV_TIMEOUT|EV_WRITE, send_icmp,
282 58fc320c 2016-09-02 reyk &cie->tv_start, &cie->env->sc_conf.timeout, cie);
286 816ddb53 2007-01-11 reyk recv_icmp(int s, short event, void *arg)
288 6e65a975 2013-01-17 bluhm struct ctl_icmp_event *cie = arg;
289 816ddb53 2007-01-11 reyk u_char packet[ICMP_BUF_SIZE];
290 816ddb53 2007-01-11 reyk socklen_t slen;
291 816ddb53 2007-01-11 reyk struct sockaddr_storage ss;
292 ae98db81 2006-12-25 reyk struct icmp *icp;
293 816ddb53 2007-01-11 reyk struct icmp6_hdr *icp6;
294 816ddb53 2007-01-11 reyk u_int16_t icpid;
295 ae98db81 2006-12-25 reyk struct host *host;
297 5c4f74ea 2012-09-19 reyk u_int32_t id;
299 ae98db81 2006-12-25 reyk if (event == EV_TIMEOUT) {
300 b2665266 2008-12-05 reyk icmp_checks_timeout(cie, HCE_ICMP_READ_TIMEOUT);
304 ae98db81 2006-12-25 reyk bzero(&packet, sizeof(packet));
305 ae98db81 2006-12-25 reyk bzero(&ss, sizeof(ss));
306 0c7b5676 2012-09-19 reyk slen = sizeof(ss);
308 c8b775d3 2007-01-12 pyr r = recvfrom(s, packet, sizeof(packet), 0,
309 c8b775d3 2007-01-12 pyr (struct sockaddr *)&ss, &slen);
310 816ddb53 2007-01-11 reyk if (r == -1 || r != ICMP_BUF_SIZE) {
311 816ddb53 2007-01-11 reyk if (r == -1 && errno != EAGAIN && errno != EINTR)
312 666bd316 2011-05-05 reyk log_debug("%s: receive error", __func__);
313 816ddb53 2007-01-11 reyk goto retry;
316 816ddb53 2007-01-11 reyk if (cie->af == AF_INET) {
317 816ddb53 2007-01-11 reyk icp = (struct icmp *)(packet + sizeof(struct ip));
318 816ddb53 2007-01-11 reyk icpid = ntohs(icp->icmp_id);
319 5c4f74ea 2012-09-19 reyk id = icp->icmp_mask;
321 816ddb53 2007-01-11 reyk icp6 = (struct icmp6_hdr *)packet;
322 816ddb53 2007-01-11 reyk icpid = ntohs(icp6->icmp6_id);
323 816ddb53 2007-01-11 reyk memcpy(&id, packet + sizeof(*icp6), sizeof(id));
325 23fba4ad 2008-01-31 reyk if (icpid != cie->env->sc_id)
326 816ddb53 2007-01-11 reyk goto retry;
327 5c4f74ea 2012-09-19 reyk id = ntohl(id);
328 ae98db81 2006-12-25 reyk host = host_find(cie->env, id);
329 ae98db81 2006-12-25 reyk if (host == NULL) {
330 666bd316 2011-05-05 reyk log_warn("%s: ping for unknown host received", __func__);
331 816ddb53 2007-01-11 reyk goto retry;
333 87178699 2007-05-27 pyr if (bcmp(&ss, &host->conf.ss, slen)) {
334 666bd316 2011-05-05 reyk log_warnx("%s: forged icmp packet?", __func__);
335 816ddb53 2007-01-11 reyk goto retry;
338 816ddb53 2007-01-11 reyk host->up = HOST_UP;
339 ae98db81 2006-12-25 reyk host->flags |= F_CHECK_DONE;
340 b2665266 2008-12-05 reyk hce_notify_done(host, HCE_ICMP_OK);
342 816ddb53 2007-01-11 reyk if (icmp_checks_done(cie))
346 816ddb53 2007-01-11 reyk event_again(&cie->ev, s, EV_TIMEOUT|EV_READ, recv_icmp,
347 58fc320c 2016-09-02 reyk &cie->tv_start, &cie->env->sc_conf.timeout, cie);
350 ea9b7d20 2006-12-16 reyk /* in_cksum from ping.c --
351 ea9b7d20 2006-12-16 reyk * Checksum routine for Internet Protocol family headers (C Version)
353 ea9b7d20 2006-12-16 reyk * Copyright (c) 1989, 1993
354 ea9b7d20 2006-12-16 reyk * The Regents of the University of California. All rights reserved.
356 ea9b7d20 2006-12-16 reyk * This code is derived from software contributed to Berkeley by
357 ea9b7d20 2006-12-16 reyk * Mike Muuss.
359 ea9b7d20 2006-12-16 reyk * Redistribution and use in source and binary forms, with or without
360 ea9b7d20 2006-12-16 reyk * modification, are permitted provided that the following conditions
362 ea9b7d20 2006-12-16 reyk * 1. Redistributions of source code must retain the above copyright
363 ea9b7d20 2006-12-16 reyk * notice, this list of conditions and the following disclaimer.
364 ea9b7d20 2006-12-16 reyk * 2. Redistributions in binary form must reproduce the above copyright
365 ea9b7d20 2006-12-16 reyk * notice, this list of conditions and the following disclaimer in the
366 ea9b7d20 2006-12-16 reyk * documentation and/or other materials provided with the distribution.
367 ea9b7d20 2006-12-16 reyk * 3. Neither the name of the University nor the names of its contributors
368 ea9b7d20 2006-12-16 reyk * may be used to endorse or promote products derived from this software
369 ea9b7d20 2006-12-16 reyk * without specific prior written permission.
371 ea9b7d20 2006-12-16 reyk * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
372 ea9b7d20 2006-12-16 reyk * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
373 ea9b7d20 2006-12-16 reyk * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
374 ea9b7d20 2006-12-16 reyk * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
375 ea9b7d20 2006-12-16 reyk * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
376 ea9b7d20 2006-12-16 reyk * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
377 ea9b7d20 2006-12-16 reyk * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
378 ea9b7d20 2006-12-16 reyk * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
379 ea9b7d20 2006-12-16 reyk * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
380 ea9b7d20 2006-12-16 reyk * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
381 ea9b7d20 2006-12-16 reyk * SUCH DAMAGE.
384 41042ecc 2006-12-16 reyk in_cksum(u_short *addr, int len)
386 ea9b7d20 2006-12-16 reyk int nleft = len;
387 ea9b7d20 2006-12-16 reyk u_short *w = addr;
388 ea9b7d20 2006-12-16 reyk int sum = 0;
389 ea9b7d20 2006-12-16 reyk u_short answer = 0;
392 ea9b7d20 2006-12-16 reyk * Our algorithm is simple, using a 32 bit accumulator (sum), we add
393 ea9b7d20 2006-12-16 reyk * sequential 16 bit words to it, and at the end, fold back all the
394 ea9b7d20 2006-12-16 reyk * carry bits from the top 16 bits into the lower 16 bits.
396 ea9b7d20 2006-12-16 reyk while (nleft > 1) {
397 ea9b7d20 2006-12-16 reyk sum += *w++;
398 ea9b7d20 2006-12-16 reyk nleft -= 2;
401 ea9b7d20 2006-12-16 reyk /* mop up an odd byte, if necessary */
402 ea9b7d20 2006-12-16 reyk if (nleft == 1) {
403 ea9b7d20 2006-12-16 reyk *(u_char *)(&answer) = *(u_char *)w ;
404 ea9b7d20 2006-12-16 reyk sum += answer;
407 ea9b7d20 2006-12-16 reyk /* add back carry outs from top 16 bits to low 16 bits */
408 4b4b1588 2007-02-23 deraadt sum = (sum >> 16) + (sum & 0xffff); /* add hi 16 to low 16 */
409 ea9b7d20 2006-12-16 reyk sum += (sum >> 16); /* add carry */
410 ea9b7d20 2006-12-16 reyk answer = ~sum; /* truncate to 16 bits */
412 ea9b7d20 2006-12-16 reyk return (answer);