insert SBCHECK calls looking for panic:receive problem
[unix-history] / usr / src / sys / netinet / ip_icmp.c
CommitLineData
fcfe450e 1/* ip_icmp.c 4.19 82/10/09 */
81a36a8d
BJ
2
3#include "../h/param.h"
2b4b57cd 4#include "../h/systm.h"
81a36a8d 5#include "../h/mbuf.h"
cdad2eb1 6#include "../h/protosw.h"
72e4f44e 7#include "../h/socket.h"
fcfe450e
BJ
8#include "../netinet/in.h"
9#include "../netinet/in_systm.h"
10#include "../netinet/ip.h"
11#include "../netinet/ip_icmp.h"
b2ac7f3b
BJ
12#include <time.h>
13#include "../h/kernel.h"
81a36a8d
BJ
14
15/*
16 * ICMP routines: error generation, receive packet processing, and
17 * routines to turnaround packets back to the originator, and
18 * host table maintenance routines.
19 */
67387c9c 20int icmpprintfs = 0;
81a36a8d
BJ
21
22/*
72e4f44e
SL
23 * Generate an error packet of type error
24 * in response to bad packet ip.
81a36a8d
BJ
25 */
26icmp_error(oip, type, code)
27 struct ip *oip;
72e4f44e 28 int type, code;
81a36a8d 29{
72e4f44e
SL
30 register unsigned oiplen = oip->ip_hl << 2;
31 register struct icmp *icp;
81a36a8d
BJ
32 struct mbuf *m;
33 struct ip *nip;
34
39674d5f
SL
35 if (icmpprintfs)
36 printf("icmp_error(%x, %d, %d)\n", oip, type, code);
81a36a8d
BJ
37 /*
38 * Make sure that the old IP packet had 8 bytes of data to return;
39 * if not, don't bother. Also don't EVER error if the old
40 * packet protocol was ICMP.
41 */
39674d5f 42 if (oip->ip_len < 8 || oip->ip_p == IPPROTO_ICMP)
81a36a8d
BJ
43 goto free;
44
45 /*
72e4f44e 46 * First, formulate icmp message
81a36a8d 47 */
ef9b4258 48 m = m_get(M_DONTWAIT);
81a36a8d
BJ
49 if (m == 0)
50 goto free;
72e4f44e
SL
51 m->m_len = oiplen + 8 + ICMP_MINLEN;
52 m->m_off = MMAXOFF - m->m_len;
53 icp = mtod(m, struct icmp *);
81a36a8d 54 icp->icmp_type = type;
72e4f44e 55 icp->icmp_void = 0;
81a36a8d 56 if (type == ICMP_PARAMPROB) {
81a36a8d 57 icp->icmp_pptr = code;
72e4f44e
SL
58 code = 0;
59 }
60 icp->icmp_code = code;
d52566dd 61 bcopy((caddr_t)oip, (caddr_t)&icp->icmp_ip, oiplen + 8);
39674d5f
SL
62 nip = &icp->icmp_ip;
63 nip->ip_len += oiplen;
64#if vax || pdp11
65 nip->ip_len = htons((u_short)nip->ip_len);
66#endif
81a36a8d
BJ
67
68 /*
72e4f44e
SL
69 * Now, copy old ip header in front of icmp
70 * message. This allows us to reuse any source
71 * routing info present.
81a36a8d 72 */
72e4f44e
SL
73 m->m_off -= oiplen;
74 nip = mtod(m, struct ip *);
75 bcopy((caddr_t)oip, (caddr_t)nip, oiplen);
76 nip->ip_len = m->m_len + oiplen;
77 nip->ip_p = IPPROTO_ICMP;
78 /* icmp_send adds ip header to m_off and m_len, so we deduct here */
79 m->m_off += oiplen;
d52566dd 80 icmp_reflect(nip);
81a36a8d 81
def04636 82free:
81a36a8d
BJ
83 m_freem(dtom(oip));
84}
85
72e4f44e
SL
86static char icmpmap[] = {
87 -1, -1, -1,
88 PRC_UNREACH_NET, PRC_QUENCH, PRC_REDIRECT_NET,
89 -1, -1, -1,
90 -1, -1, PRC_TIMXCEED_INTRANS,
91 PRC_PARAMPROB, -1, -1,
92 -1, -1
93};
94
95static struct sockproto icmproto = { AF_INET, IPPROTO_ICMP };
96static struct sockaddr_in icmpsrc = { AF_INET };
97static struct sockaddr_in icmpdst = { AF_INET };
98
81a36a8d 99/*
d52566dd 100 * Process a received ICMP message.
81a36a8d
BJ
101 */
102icmp_input(m)
103 struct mbuf *m;
104{
81a36a8d 105 register struct icmp *icp;
d52566dd 106 register struct ip *ip = mtod(m, struct ip *);
39674d5f 107 int icmplen = ip->ip_len, hlen = ip->ip_hl << 2, i, (*ctlfunc)();
b454c3ea 108 extern u_char ip_protox[];
81a36a8d
BJ
109
110 /*
111 * Locate icmp structure in mbuf, and check
112 * that not corrupted and of at least minimum length.
113 */
39674d5f
SL
114 if (icmpprintfs)
115 printf("icmp_input from %x, len %d\n", ip->ip_src, icmplen);
72e4f44e
SL
116 if (icmplen < ICMP_MINLEN)
117 goto free;
81a36a8d
BJ
118 m->m_len -= hlen;
119 m->m_off += hlen;
120 /* need routine to make sure header is in this mbuf here */
72e4f44e 121 icp = mtod(m, struct icmp *);
81a36a8d
BJ
122 i = icp->icmp_cksum;
123 icp->icmp_cksum = 0;
72e4f44e
SL
124 if (i != in_cksum(m, icmplen)) {
125 printf("icmp: cksum %x\n", i);
d52566dd 126 goto free;
72e4f44e 127 }
81a36a8d
BJ
128
129 /*
130 * Message type specific processing.
131 */
39674d5f
SL
132 if (icmpprintfs)
133 printf("icmp_input, type %d code %d\n", icp->icmp_type,
134 icp->icmp_code);
72e4f44e 135 switch (i = icp->icmp_type) {
81a36a8d
BJ
136
137 case ICMP_UNREACH:
138 case ICMP_TIMXCEED:
139 case ICMP_PARAMPROB:
81a36a8d 140 case ICMP_REDIRECT:
72e4f44e 141 case ICMP_SOURCEQUENCH:
81a36a8d
BJ
142 /*
143 * Problem with previous datagram; advise
144 * higher level routines.
145 */
39674d5f
SL
146#if vax || pdp11
147 icp->icmp_ip.ip_len = ntohs(icp->icmp_ip.ip_len);
148#endif
d52566dd
BJ
149 if (icmplen < ICMP_ADVLENMIN || icmplen < ICMP_ADVLEN(icp))
150 goto free;
39674d5f
SL
151 if (icmpprintfs)
152 printf("deliver to protocol %d\n", icp->icmp_ip.ip_p);
153 if (ctlfunc = protosw[ip_protox[icp->icmp_ip.ip_p]].pr_ctlinput)
154 (*ctlfunc)(icmpmap[i] + icp->icmp_code, (caddr_t)icp);
81a36a8d
BJ
155 goto free;
156
157 case ICMP_ECHO:
158 icp->icmp_type = ICMP_ECHOREPLY;
159 goto reflect;
160
161 case ICMP_TSTAMP:
d52566dd
BJ
162 if (icmplen < ICMP_TSLEN)
163 goto free;
81a36a8d 164 icp->icmp_type = ICMP_TSTAMPREPLY;
2b4b57cd 165 icp->icmp_rtime = iptime();
81a36a8d
BJ
166 icp->icmp_ttime = icp->icmp_rtime; /* bogus, do later! */
167 goto reflect;
168
169 case ICMP_IREQ:
170 /* fill in source address zero fields! */
171 goto reflect;
172
173 case ICMP_ECHOREPLY:
174 case ICMP_TSTAMPREPLY:
175 case ICMP_IREQREPLY:
d52566dd
BJ
176 if (icmplen < ICMP_ADVLENMIN || icmplen < ICMP_ADVLEN(icp))
177 goto free;
72e4f44e
SL
178 icmpsrc.sin_addr = ip->ip_src;
179 icmpdst.sin_addr = ip->ip_dst;
180 raw_input(dtom(icp), &icmproto, (struct sockaddr *)&icmpsrc,
181 (struct sockaddr *)&icmpdst);
81a36a8d
BJ
182 goto free;
183
184 default:
185 goto free;
186 }
187reflect:
188 icmp_reflect(ip);
189free:
190 m_freem(dtom(ip));
191}
192
193/*
194 * Reflect the ip packet back to the source
72e4f44e 195 * TODO: rearrange ip source routing options.
81a36a8d
BJ
196 */
197icmp_reflect(ip)
198 struct ip *ip;
199{
2b4b57cd 200 struct in_addr t;
81a36a8d 201
72e4f44e
SL
202 t = ip->ip_dst;
203 ip->ip_dst = ip->ip_src;
204 ip->ip_src = t;
81a36a8d
BJ
205 icmp_send(ip);
206}
207
67387c9c 208int generateicmpmsgs = 1;
72e4f44e 209
81a36a8d 210/*
72e4f44e
SL
211 * Send an icmp packet back to the ip level,
212 * after supplying a checksum.
81a36a8d 213 */
cdad2eb1 214icmp_send(ip)
81a36a8d 215 struct ip *ip;
81a36a8d 216{
72e4f44e
SL
217 register int hlen = ip->ip_hl << 2;
218 register struct icmp *icp;
219 register struct mbuf *m = dtom(ip);
d52566dd 220
72e4f44e
SL
221 if (!generateicmpmsgs)
222 return;
223 icp = mtod(m, struct icmp *);
224 icp->icmp_cksum = 0;
225 icp->icmp_cksum = in_cksum(m, ip->ip_len - hlen);
226 m->m_off -= hlen;
227 m->m_len += hlen;
39674d5f
SL
228 if (icmpprintfs)
229 printf("icmp_send dst %x src %x\n", ip->ip_dst, ip->ip_src);
72e4f44e 230 (void) ip_output(m, 0, 0, 0);
d52566dd
BJ
231}
232
cdad2eb1 233n_time
2b4b57cd 234iptime()
d52566dd 235{
cdad2eb1 236 int s = spl6();
2752c877 237 u_long t;
d52566dd 238
b2ac7f3b 239 t = (time.tv_sec % (24*60*60)) * 1000 + time.tv_usec / 1000;
cdad2eb1
BJ
240 splx(s);
241 return (htonl(t));
d52566dd 242}