tools/adflib: build only host variant which is used by Sam440 target
[AROS.git] / workbench / network / stacks / AROSTCP / bsdsocket / netinet / ip_icmp.c
blobc31e0f8e81b3dca28c1a40cd787e7e73f1e44875
1 /*
2 * Copyright (C) 1993 AmiTCP/IP Group, <amitcp-group@hut.fi>
3 * Helsinki University of Technology, Finland.
4 * All rights reserved.
5 * Copyright (C) 2005 Neil Cafferkey
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
19 * MA 02111-1307, USA.
24 * Copyright (c) 1982, 1986, 1989 Regents of the University of California.
25 * All rights reserved.
27 * Redistribution and use in source and binary forms, with or without
28 * modification, are permitted provided that the following conditions
29 * are met:
30 * 1. Redistributions of source code must retain the above copyright
31 * notice, this list of conditions and the following disclaimer.
32 * 2. Redistributions in binary form must reproduce the above copyright
33 * notice, this list of conditions and the following disclaimer in the
34 * documentation and/or other materials provided with the distribution.
35 * 3. All advertising materials mentioning features or use of this software
36 * must display the following acknowledgement:
37 * This product includes software developed by the University of
38 * California, Berkeley and its contributors.
39 * 4. Neither the name of the University nor the names of its contributors
40 * may be used to endorse or promote products derived from this software
41 * without specific prior written permission.
43 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
44 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
45 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
46 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
47 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
48 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
49 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
50 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
51 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
52 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
53 * SUCH DAMAGE.
55 * @(#)ip_icmp.c 7.15 (Berkeley) 4/20/91
58 #include <conf.h>
60 #include <sys/param.h>
61 #include <sys/systm.h>
62 #include <sys/malloc.h>
63 #include <sys/mbuf.h>
64 #include <sys/protosw.h>
65 #include <sys/socket.h>
66 /*#include <sys/time.h>*/
67 #include <sys/kernel.h>
69 #include <net/route.h>
70 #include <net/if.h>
72 #include <netinet/in.h>
73 #include <netinet/in_systm.h>
74 #include <netinet/in_var.h>
75 #include <netinet/ip.h>
76 #include <netinet/ip_icmp.h>
77 #include <netinet/icmp_var.h>
79 #include <netinet/ip_icmp_protos.h>
80 #include <netinet/ip_input_protos.h>
81 #include <netinet/ip_output_protos.h>
82 #include <netinet/in_cksum_protos.h>
83 #include <netinet/in_protos.h>
84 #include <kern/uipc_domain_protos.h>
85 #include <net/raw_usrreq_protos.h>
87 struct icmpstat icmpstat = { 0 };
90 * ICMP routines: error generation, receive packet processing, and
91 * routines to turnaround packets back to the originator, and
92 * host table maintenance routines.
94 int icmpprintfs = 0; /* has effect only if ICMPPRINTFS is defined */
96 extern struct protosw inetsw[];
99 * Generate an error packet of type error
100 * in response to bad packet ip.
103 void
104 icmp_error(n, type, code, dest)
105 struct mbuf *n;
106 int type, code;
107 struct in_addr dest;
109 register struct ip *oip = mtod(n, struct ip *), *nip;
110 register unsigned oiplen = oip->ip_hl << 2;
111 register struct icmp *icp;
112 register struct mbuf *m;
113 unsigned icmplen;
115 #ifdef ICMPPRINTFS
116 if (icmpprintfs)
117 printf("icmp_error(%lx, %ld, %ld)\n", (u_long)oip, type, code);
118 #endif
119 if (type != ICMP_REDIRECT)
120 icmpstat.icps_error++;
122 * Don't send error if not the first fragment of message.
123 * Don't error if the old packet protocol was ICMP
124 * error message, only known informational types.
126 if (oip->ip_off &~ (IP_MF|IP_DF))
127 goto freeit;
128 if (oip->ip_p == IPPROTO_ICMP && type != ICMP_REDIRECT &&
129 n->m_len >= oiplen + ICMP_MINLEN &&
130 !ICMP_INFOTYPE(((struct icmp *)((caddr_t)oip + oiplen))->icmp_type)) {
131 icmpstat.icps_oldicmp++;
132 goto freeit;
136 * First, formulate icmp message
138 m = m_gethdr(M_DONTWAIT, MT_HEADER);
139 if (m == NULL)
140 goto freeit;
141 icmplen = oiplen + MIN(8, oip->ip_len);
142 m->m_len = icmplen + ICMP_MINLEN;
143 MH_ALIGN(m, m->m_len);
144 icp = mtod(m, struct icmp *);
145 if ((u_int)type > ICMP_MAXTYPE)
146 panic("icmp_error");
147 icmpstat.icps_outhist[type]++;
148 icp->icmp_type = type;
149 if (type == ICMP_REDIRECT)
150 icp->icmp_gwaddr = dest;
151 else
152 icp->icmp_void = 0;
153 if (type == ICMP_PARAMPROB) {
154 icp->icmp_pptr = code;
155 code = 0;
157 icp->icmp_code = code;
158 aligned_bcopy((caddr_t)oip, (caddr_t)&icp->icmp_ip, icmplen);
159 nip = &icp->icmp_ip;
160 nip->ip_len = htons((u_short)(nip->ip_len + oiplen));
163 * Now, copy old ip header (without options)
164 * in front of icmp message.
166 if (m->m_data - sizeof(struct ip) < m->m_pktdat)
167 panic("icmp len");
168 m->m_data -= sizeof(struct ip);
169 m->m_len += sizeof(struct ip);
170 m->m_pkthdr.len = m->m_len;
171 m->m_pkthdr.rcvif = n->m_pkthdr.rcvif;
172 nip = mtod(m, struct ip *);
173 aligned_bcopy((caddr_t)oip, (caddr_t)nip, oiplen);
174 nip->ip_len = m->m_len;
175 nip->ip_hl = sizeof(struct ip) >> 2;
176 nip->ip_p = IPPROTO_ICMP;
177 icmp_reflect(m);
179 freeit:
180 m_freem(n);
183 static struct sockproto icmproto = { AF_INET, IPPROTO_ICMP };
184 static struct sockaddr_in icmpsrc = { sizeof (struct sockaddr_in), AF_INET };
185 static struct sockaddr_in icmpdst = { sizeof (struct sockaddr_in), AF_INET };
186 static struct sockaddr_in icmpgw = { sizeof (struct sockaddr_in), AF_INET };
187 struct sockaddr_in icmpmask = { 8, 0 };
188 struct in_ifaddr *ifptoia();
191 * Process a received ICMP message.
193 void icmp_input(void *arg, ...)
195 register struct mbuf *m = arg;
196 register struct icmp *icp;
197 register struct ip *ip = mtod(m, struct ip *);
198 int icmplen = ip->ip_len;
199 register int i;
200 struct in_ifaddr *ia;
201 int code;
202 extern u_char ip_protox[];
203 extern struct in_addr in_makeaddr();
204 int hlen;
205 va_list va;
208 va_start(va, arg);
209 hlen = va_arg(va, int);
210 va_end(va);
212 * Locate icmp structure in mbuf, and check
213 * that not corrupted and of at least minimum length.
215 #ifdef ICMPPRINTFS
216 if (icmpprintfs)
217 printf("icmp_input from %lx, len %ld\n", ip->ip_src.s_addr, icmplen);
218 #endif
219 if (icmplen < ICMP_MINLEN) {
220 icmpstat.icps_tooshort++;
221 goto freeit;
223 i = hlen + MIN(icmplen, ICMP_ADVLENMIN);
224 if (m->m_len < i && (m = m_pullup(m, i)) == 0) {
225 icmpstat.icps_tooshort++;
226 return;
228 ip = mtod(m, struct ip *);
229 m->m_len -= hlen;
230 m->m_data += hlen;
231 icp = mtod(m, struct icmp *);
232 if (in_cksum(m, icmplen)) {
233 icmpstat.icps_checksum++;
234 goto freeit;
236 m->m_len += hlen;
237 m->m_data -= hlen;
239 #ifdef ICMPPRINTFS
241 * Message type specific processing.
243 if (icmpprintfs)
244 printf("icmp_input, type %ld code %ld\n", icp->icmp_type,
245 icp->icmp_code);
246 #endif
247 if (icp->icmp_type > ICMP_MAXTYPE)
248 goto raw;
249 icmpstat.icps_inhist[icp->icmp_type]++;
250 code = icp->icmp_code;
251 switch (icp->icmp_type) {
253 case ICMP_UNREACH:
254 if (code > 5)
255 goto badcode;
256 code += PRC_UNREACH_NET;
257 goto deliver;
259 case ICMP_TIMXCEED:
260 if (code > 1)
261 goto badcode;
262 code += PRC_TIMXCEED_INTRANS;
263 goto deliver;
265 case ICMP_PARAMPROB:
266 if (code)
267 goto badcode;
268 code = PRC_PARAMPROB;
269 goto deliver;
271 case ICMP_SOURCEQUENCH:
272 if (code)
273 goto badcode;
274 code = PRC_QUENCH;
275 deliver:
277 * Problem with datagram; advise higher level routines.
279 if (icmplen < ICMP_ADVLENMIN || icmplen < ICMP_ADVLEN(icp) ||
280 icp->icmp_ip.ip_hl < (sizeof(struct ip) >> 2)) {
281 icmpstat.icps_badlen++;
282 goto freeit;
284 NTOHS(icp->icmp_ip.ip_len);
285 #ifdef ICMPPRINTFS
286 if (icmpprintfs)
287 printf("deliver to protocol %ld\n", icp->icmp_ip.ip_p);
288 #endif
289 icmpsrc.sin_addr = icp->icmp_ip.ip_dst;
290 if (inetsw[ip_protox[icp->icmp_ip.ip_p]].pr_ctlinput)
291 (*inetsw[ip_protox[icp->icmp_ip.ip_p]].pr_ctlinput)
292 (code, (struct sockaddr *)&icmpsrc,
293 (caddr_t) &icp->icmp_ip);
294 break;
296 badcode:
297 icmpstat.icps_badcode++;
298 break;
300 case ICMP_ECHO:
301 icp->icmp_type = ICMP_ECHOREPLY;
302 goto reflect;
304 case ICMP_TSTAMP:
305 if (icmplen < ICMP_TSLEN) {
306 icmpstat.icps_badlen++;
307 break;
309 icp->icmp_type = ICMP_TSTAMPREPLY;
310 icp->icmp_rtime = iptime();
311 icp->icmp_ttime = icp->icmp_rtime; /* bogus, do later! */
312 goto reflect;
314 case ICMP_IREQ:
315 #define satosin(sa) ((struct sockaddr_in *)(sa))
316 if (in_netof(ip->ip_src) == 0 &&
317 (ia = ifptoia(m->m_pkthdr.rcvif)))
318 ip->ip_src = in_makeaddr(in_netof(IA_SIN(ia)->sin_addr),
319 in_lnaof(ip->ip_src));
320 icp->icmp_type = ICMP_IREQREPLY;
321 goto reflect;
323 case ICMP_MASKREQ:
324 if (icmplen < ICMP_MASKLEN ||
325 (ia = ifptoia(m->m_pkthdr.rcvif)) == 0)
326 break;
327 icp->icmp_type = ICMP_MASKREPLY;
328 icp->icmp_mask = ia->ia_sockmask.sin_addr.s_addr;
329 if (ip->ip_src.s_addr == 0) {
330 if (ia->ia_ifp->if_flags & IFF_BROADCAST)
331 ip->ip_src = satosin(&ia->ia_broadaddr)->sin_addr;
332 else if (ia->ia_ifp->if_flags & IFF_POINTOPOINT)
333 ip->ip_src = satosin(&ia->ia_dstaddr)->sin_addr;
335 reflect:
336 ip->ip_len += hlen; /* since ip_input deducts this */
337 icmpstat.icps_reflect++;
338 icmpstat.icps_outhist[icp->icmp_type]++;
339 icmp_reflect(m);
340 return;
342 case ICMP_REDIRECT:
343 if (icmplen < ICMP_ADVLENMIN || icmplen < ICMP_ADVLEN(icp)) {
344 icmpstat.icps_badlen++;
345 break;
348 * Short circuit routing redirects to force
349 * immediate change in the kernel's routing
350 * tables. The message is also handed to anyone
351 * listening on a raw socket (e.g. the routing
352 * daemon for use in updating its tables).
354 icmpgw.sin_addr = ip->ip_src;
355 icmpdst.sin_addr = icp->icmp_gwaddr;
356 #ifdef ICMPPRINTFS
357 if (icmpprintfs)
358 printf("redirect dst %lx to %lx\n", icp->icmp_ip.ip_dst.s_addr,
359 icp->icmp_gwaddr.s_addr);
360 #endif
361 if (code == ICMP_REDIRECT_NET || code == ICMP_REDIRECT_TOSNET) {
362 u_long in_netof();
363 icmpsrc.sin_addr =
364 in_makeaddr(in_netof(icp->icmp_ip.ip_dst), INADDR_ANY);
365 in_sockmaskof(icp->icmp_ip.ip_dst, &icmpmask);
366 rtredirect((struct sockaddr *)&icmpsrc,
367 (struct sockaddr *)&icmpdst,
368 (struct sockaddr *)&icmpmask, RTF_GATEWAY,
369 (struct sockaddr *)&icmpgw, (struct rtentry **)0);
370 icmpsrc.sin_addr = icp->icmp_ip.ip_dst;
371 pfctlinput(PRC_REDIRECT_NET,
372 (struct sockaddr *)&icmpsrc);
373 } else {
374 icmpsrc.sin_addr = icp->icmp_ip.ip_dst;
375 rtredirect((struct sockaddr *)&icmpsrc,
376 (struct sockaddr *)&icmpdst,
377 (struct sockaddr *)0, RTF_GATEWAY | RTF_HOST,
378 (struct sockaddr *)&icmpgw, (struct rtentry **)0);
379 pfctlinput(PRC_REDIRECT_HOST,
380 (struct sockaddr *)&icmpsrc);
382 break;
385 * No kernel processing for the following;
386 * just fall through to send to raw listener.
388 case ICMP_ECHOREPLY:
389 case ICMP_TSTAMPREPLY:
390 case ICMP_IREQREPLY:
391 case ICMP_MASKREPLY:
392 default:
393 break;
396 raw:
397 icmpsrc.sin_addr = ip->ip_src;
398 icmpdst.sin_addr = ip->ip_dst;
399 (void) raw_input(m, &icmproto, (struct sockaddr *)&icmpsrc,
400 (struct sockaddr *)&icmpdst);
401 return;
403 freeit:
404 m_freem(m);
408 * Reflect the ip packet back to the source
410 void
411 icmp_reflect(m)
412 struct mbuf *m;
414 register struct ip *ip = mtod(m, struct ip *);
415 register struct in_ifaddr *ia;
416 struct in_addr t;
417 struct mbuf *opts = 0;
418 int optlen = (ip->ip_hl << 2) - sizeof(struct ip);
420 t = ip->ip_dst;
421 ip->ip_dst = ip->ip_src;
423 * If the incoming packet was addressed directly to us,
424 * use dst as the src for the reply. Otherwise (broadcast
425 * or anonymous), use the address which corresponds
426 * to the incoming interface.
428 for (ia = in_ifaddr; ia; ia = ia->ia_next) {
429 if (t.s_addr == IA_SIN(ia)->sin_addr.s_addr)
430 break;
431 if ((ia->ia_ifp->if_flags & IFF_BROADCAST) &&
432 t.s_addr == satosin(&ia->ia_broadaddr)->sin_addr.s_addr)
433 break;
435 if (ia == (struct in_ifaddr *)0)
436 ia = ifptoia(m->m_pkthdr.rcvif);
437 if (ia == (struct in_ifaddr *)0)
438 ia = in_ifaddr;
439 t = IA_SIN(ia)->sin_addr;
440 ip->ip_src = t;
441 ip->ip_ttl = MAXTTL;
443 if (optlen > 0) {
444 register u_char *cp;
445 int opt, cnt;
446 u_int len;
449 * Retrieve any source routing from the incoming packet;
450 * add on any record-route or timestamp options.
452 cp = (u_char *) (ip + 1);
453 if ((opts = ip_srcroute()) == 0 &&
454 (opts = m_gethdr(M_DONTWAIT, MT_HEADER))) {
455 opts->m_len = sizeof(struct in_addr);
456 mtod(opts, struct in_addr *)->s_addr = 0;
458 if (opts) {
459 #ifdef ICMPPRINTFS
460 if (icmpprintfs)
461 printf("icmp_reflect optlen %ld rt %ld => ",
462 optlen, opts->m_len);
463 #endif
464 for (cnt = optlen; cnt > 0; cnt -= len, cp += len) {
465 opt = cp[IPOPT_OPTVAL];
466 if (opt == IPOPT_EOL)
467 break;
468 if (opt == IPOPT_NOP)
469 len = 1;
470 else {
471 len = cp[IPOPT_OLEN];
472 if (len <= 0 || len > cnt)
473 break;
476 * should check for overflow, but it "can't happen"
478 if (opt == IPOPT_RR || opt == IPOPT_TS) {
479 bcopy((caddr_t)cp,
480 mtod(opts, caddr_t) + opts->m_len, len);
481 opts->m_len += len;
484 if (opts->m_len % 4 != 0) {
485 *(mtod(opts, caddr_t) + opts->m_len) = IPOPT_EOL;
486 opts->m_len++;
488 #ifdef ICMPPRINTFS
489 if (icmpprintfs)
490 printf("%ld\n", opts->m_len);
491 #endif
494 * Now strip out original options by copying rest of first
495 * mbuf's data back, and adjust the IP length.
497 ip->ip_len -= optlen;
498 ip->ip_hl = sizeof(struct ip) >> 2;
499 m->m_len -= optlen;
500 if (m->m_flags & M_PKTHDR)
501 m->m_pkthdr.len -= optlen;
502 optlen += sizeof(struct ip);
503 bcopy((caddr_t)ip + optlen, (caddr_t)(ip + 1),
504 (unsigned)(m->m_len - sizeof(struct ip)));
506 icmp_send(m, opts);
507 if (opts)
508 (void)m_free(opts);
511 struct in_ifaddr *
512 ifptoia(ifp)
513 struct ifnet *ifp;
515 register struct in_ifaddr *ia;
517 for (ia = in_ifaddr; ia; ia = ia->ia_next)
518 if (ia->ia_ifp == ifp)
519 return (ia);
520 return ((struct in_ifaddr *)0);
524 * Send an icmp packet back to the ip level,
525 * after supplying a checksum.
527 void
528 icmp_send(m, opts)
529 register struct mbuf *m;
530 struct mbuf *opts;
532 register struct ip *ip = mtod(m, struct ip *);
533 register int hlen;
534 register struct icmp *icp;
536 hlen = ip->ip_hl << 2;
537 m->m_data += hlen;
538 m->m_len -= hlen;
539 icp = mtod(m, struct icmp *);
540 icp->icmp_cksum = 0;
541 icp->icmp_cksum = in_cksum(m, ip->ip_len - hlen);
542 m->m_data -= hlen;
543 m->m_len += hlen;
544 #ifdef ICMPPRINTFS
545 if (icmpprintfs)
546 printf("icmp_send dst %lx src %lx\n", ip->ip_dst.s_addr, ip->ip_src.s_addr);
547 #endif
548 (void) ip_output(m, opts, (struct route *)0, 0);
551 n_time
552 iptime()
554 struct timeval atv;
555 u_long t;
557 GetSysTime(&atv);
558 t = (atv.tv_sec % (24*60*60)) * 1000 + atv.tv_usec / 1000;
559 return (htonl(t));