]>
Commit | Line | Data |
---|---|---|
55e303ae A |
1 | /* $FreeBSD: src/sys/netinet6/ah_input.c,v 1.1.2.6 2002/04/28 05:40:26 suz Exp $ */ |
2 | /* $KAME: ah_input.c,v 1.67 2002/01/07 11:39:56 kjc Exp $ */ | |
9bccf70c | 3 | |
1c79356b A |
4 | /* |
5 | * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. | |
6 | * All rights reserved. | |
7 | * | |
8 | * Redistribution and use in source and binary forms, with or without | |
9 | * modification, are permitted provided that the following conditions | |
10 | * are met: | |
11 | * 1. Redistributions of source code must retain the above copyright | |
12 | * notice, this list of conditions and the following disclaimer. | |
13 | * 2. Redistributions in binary form must reproduce the above copyright | |
14 | * notice, this list of conditions and the following disclaimer in the | |
15 | * documentation and/or other materials provided with the distribution. | |
16 | * 3. Neither the name of the project nor the names of its contributors | |
17 | * may be used to endorse or promote products derived from this software | |
18 | * without specific prior written permission. | |
19 | * | |
20 | * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND | |
21 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
23 | * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE | |
24 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
25 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
26 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
27 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
28 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
29 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
30 | * SUCH DAMAGE. | |
31 | */ | |
32 | ||
33 | /* | |
34 | * RFC1826/2402 authentication header. | |
35 | */ | |
36 | ||
1c79356b A |
37 | #include <sys/param.h> |
38 | #include <sys/systm.h> | |
39 | #include <sys/malloc.h> | |
40 | #include <sys/mbuf.h> | |
41 | #include <sys/domain.h> | |
42 | #include <sys/protosw.h> | |
43 | #include <sys/socket.h> | |
44 | #include <sys/errno.h> | |
45 | #include <sys/time.h> | |
46 | #include <sys/kernel.h> | |
47 | #include <sys/syslog.h> | |
48 | ||
49 | #include <net/if.h> | |
50 | #include <net/route.h> | |
1c79356b | 51 | #include <kern/cpu_number.h> |
91447636 | 52 | #include <kern/locks.h> |
1c79356b A |
53 | |
54 | #include <netinet/in.h> | |
55 | #include <netinet/in_systm.h> | |
56 | #include <netinet/in_var.h> | |
57 | #include <netinet/ip.h> | |
58 | #include <netinet/ip_var.h> | |
59 | #include <netinet/ip_ecn.h> | |
9bccf70c A |
60 | #include <netinet/in_pcb.h> |
61 | #if INET6 | |
62 | #include <netinet6/ip6_ecn.h> | |
63 | #endif | |
1c79356b A |
64 | |
65 | #if INET6 | |
66 | #include <netinet/ip6.h> | |
67 | #include <netinet6/ip6_var.h> | |
9bccf70c | 68 | #include <netinet6/in6_pcb.h> |
1c79356b | 69 | #include <netinet/icmp6.h> |
9bccf70c | 70 | #include <netinet6/ip6protosw.h> |
1c79356b A |
71 | #endif |
72 | ||
73 | #include <netinet6/ipsec.h> | |
9bccf70c A |
74 | #if INET6 |
75 | #include <netinet6/ipsec6.h> | |
76 | #endif | |
1c79356b | 77 | #include <netinet6/ah.h> |
9bccf70c A |
78 | #if INET6 |
79 | #include <netinet6/ah6.h> | |
80 | #endif | |
1c79356b A |
81 | #include <netkey/key.h> |
82 | #include <netkey/keydb.h> | |
9bccf70c | 83 | #if IPSEC_DEBUG |
1c79356b | 84 | #include <netkey/key_debug.h> |
9bccf70c A |
85 | #else |
86 | #define KEYDEBUG(lev,arg) | |
87 | #endif | |
1c79356b A |
88 | |
89 | ||
90 | #include <net/net_osdep.h> | |
91 | ||
92 | #define IPLEN_FLIPPED | |
93 | ||
91447636 | 94 | extern lck_mtx_t *sadb_mutex; |
1c79356b A |
95 | #if INET |
96 | extern struct protosw inetsw[]; | |
1c79356b A |
97 | |
98 | void | |
99 | ah4_input(struct mbuf *m, int off) | |
100 | { | |
101 | struct ip *ip; | |
102 | struct ah *ah; | |
103 | u_int32_t spi; | |
9bccf70c | 104 | const struct ah_algorithm *algo; |
1c79356b A |
105 | size_t siz; |
106 | size_t siz1; | |
107 | u_char *cksum; | |
108 | struct secasvar *sav = NULL; | |
109 | u_int16_t nxt; | |
110 | size_t hlen; | |
111 | int s; | |
9bccf70c A |
112 | size_t stripsiz = 0; |
113 | ||
91447636 | 114 | lck_mtx_lock(sadb_mutex); |
1c79356b A |
115 | |
116 | #ifndef PULLDOWN_TEST | |
117 | if (m->m_len < off + sizeof(struct newah)) { | |
118 | m = m_pullup(m, off + sizeof(struct newah)); | |
119 | if (!m) { | |
120 | ipseclog((LOG_DEBUG, "IPv4 AH input: can't pullup;" | |
121 | "dropping the packet for simplicity\n")); | |
122 | ipsecstat.in_inval++; | |
123 | goto fail; | |
124 | } | |
125 | } | |
126 | ||
127 | ip = mtod(m, struct ip *); | |
128 | ah = (struct ah *)(((caddr_t)ip) + off); | |
129 | #else | |
130 | ip = mtod(m, struct ip *); | |
131 | IP6_EXTHDR_GET(ah, struct ah *, m, off, sizeof(struct newah)); | |
132 | if (ah == NULL) { | |
133 | ipseclog((LOG_DEBUG, "IPv4 AH input: can't pullup;" | |
134 | "dropping the packet for simplicity\n")); | |
135 | ipsecstat.in_inval++; | |
136 | goto fail; | |
137 | } | |
138 | #endif | |
139 | nxt = ah->ah_nxt; | |
140 | #ifdef _IP_VHL | |
141 | hlen = IP_VHL_HL(ip->ip_vhl) << 2; | |
142 | #else | |
143 | hlen = ip->ip_hl << 2; | |
144 | #endif | |
145 | ||
146 | /* find the sassoc. */ | |
147 | spi = ah->ah_spi; | |
148 | ||
149 | if ((sav = key_allocsa(AF_INET, | |
150 | (caddr_t)&ip->ip_src, (caddr_t)&ip->ip_dst, | |
151 | IPPROTO_AH, spi)) == 0) { | |
152 | ipseclog((LOG_WARNING, | |
153 | "IPv4 AH input: no key association found for spi %u\n", | |
154 | (u_int32_t)ntohl(spi))); | |
155 | ipsecstat.in_nosa++; | |
156 | goto fail; | |
157 | } | |
158 | KEYDEBUG(KEYDEBUG_IPSEC_STAMP, | |
159 | printf("DP ah4_input called to allocate SA:%p\n", sav)); | |
160 | if (sav->state != SADB_SASTATE_MATURE | |
161 | && sav->state != SADB_SASTATE_DYING) { | |
162 | ipseclog((LOG_DEBUG, | |
163 | "IPv4 AH input: non-mature/dying SA found for spi %u\n", | |
164 | (u_int32_t)ntohl(spi))); | |
165 | ipsecstat.in_badspi++; | |
166 | goto fail; | |
167 | } | |
9bccf70c A |
168 | |
169 | algo = ah_algorithm_lookup(sav->alg_auth); | |
170 | if (!algo) { | |
1c79356b | 171 | ipseclog((LOG_DEBUG, "IPv4 AH input: " |
9bccf70c | 172 | "unsupported authentication algorithm for spi %u\n", |
1c79356b A |
173 | (u_int32_t)ntohl(spi))); |
174 | ipsecstat.in_badspi++; | |
175 | goto fail; | |
176 | } | |
177 | ||
1c79356b A |
178 | siz = (*algo->sumsiz)(sav); |
179 | siz1 = ((siz + 3) & ~(4 - 1)); | |
180 | ||
181 | /* | |
182 | * sanity checks for header, 1. | |
183 | */ | |
184 | { | |
185 | int sizoff; | |
186 | ||
187 | sizoff = (sav->flags & SADB_X_EXT_OLD) ? 0 : 4; | |
188 | ||
9bccf70c A |
189 | /* |
190 | * Here, we do not do "siz1 == siz". This is because the way | |
191 | * RFC240[34] section 2 is written. They do not require truncation | |
192 | * to 96 bits. | |
193 | * For example, Microsoft IPsec stack attaches 160 bits of | |
194 | * authentication data for both hmac-md5 and hmac-sha1. For hmac-sha1, | |
195 | * 32 bits of padding is attached. | |
196 | * | |
197 | * There are two downsides to this specification. | |
198 | * They have no real harm, however, they leave us fuzzy feeling. | |
199 | * - if we attach more than 96 bits of authentication data onto AH, | |
200 | * we will never notice about possible modification by rogue | |
201 | * intermediate nodes. | |
202 | * Since extra bits in AH checksum is never used, this constitutes | |
203 | * no real issue, however, it is wacky. | |
204 | * - even if the peer attaches big authentication data, we will never | |
205 | * notice the difference, since longer authentication data will just | |
206 | * work. | |
207 | * | |
208 | * We may need some clarification in the spec. | |
209 | */ | |
210 | if (siz1 < siz) { | |
211 | ipseclog((LOG_NOTICE, "sum length too short in IPv4 AH input " | |
212 | "(%lu, should be at least %lu): %s\n", | |
213 | (u_long)siz1, (u_long)siz, | |
214 | ipsec4_logpacketstr(ip, spi))); | |
215 | ipsecstat.in_inval++; | |
216 | goto fail; | |
217 | } | |
1c79356b A |
218 | if ((ah->ah_len << 2) - sizoff != siz1) { |
219 | ipseclog((LOG_NOTICE, "sum length mismatch in IPv4 AH input " | |
9bccf70c A |
220 | "(%d should be %lu): %s\n", |
221 | (ah->ah_len << 2) - sizoff, (u_long)siz1, | |
222 | ipsec4_logpacketstr(ip, spi))); | |
1c79356b A |
223 | ipsecstat.in_inval++; |
224 | goto fail; | |
225 | } | |
226 | ||
227 | #ifndef PULLDOWN_TEST | |
228 | if (m->m_len < off + sizeof(struct ah) + sizoff + siz1) { | |
229 | m = m_pullup(m, off + sizeof(struct ah) + sizoff + siz1); | |
230 | if (!m) { | |
231 | ipseclog((LOG_DEBUG, "IPv4 AH input: can't pullup\n")); | |
232 | ipsecstat.in_inval++; | |
233 | goto fail; | |
234 | } | |
235 | ||
236 | ip = mtod(m, struct ip *); | |
237 | ah = (struct ah *)(((caddr_t)ip) + off); | |
238 | } | |
239 | #else | |
240 | IP6_EXTHDR_GET(ah, struct ah *, m, off, | |
241 | sizeof(struct ah) + sizoff + siz1); | |
242 | if (ah == NULL) { | |
243 | ipseclog((LOG_DEBUG, "IPv4 AH input: can't pullup\n")); | |
244 | ipsecstat.in_inval++; | |
245 | goto fail; | |
246 | } | |
247 | #endif | |
248 | } | |
249 | ||
250 | /* | |
251 | * check for sequence number. | |
252 | */ | |
253 | if ((sav->flags & SADB_X_EXT_OLD) == 0 && sav->replay) { | |
254 | if (ipsec_chkreplay(ntohl(((struct newah *)ah)->ah_seq), sav)) | |
255 | ; /*okey*/ | |
256 | else { | |
257 | ipsecstat.in_ahreplay++; | |
258 | ipseclog((LOG_WARNING, | |
259 | "replay packet in IPv4 AH input: %s %s\n", | |
260 | ipsec4_logpacketstr(ip, spi), ipsec_logsastr(sav))); | |
261 | goto fail; | |
262 | } | |
263 | } | |
264 | ||
265 | /* | |
266 | * alright, it seems sane. now we are going to check the | |
267 | * cryptographic checksum. | |
268 | */ | |
269 | cksum = _MALLOC(siz1, M_TEMP, M_NOWAIT); | |
270 | if (!cksum) { | |
271 | ipseclog((LOG_DEBUG, "IPv4 AH input: " | |
272 | "couldn't alloc temporary region for cksum\n")); | |
273 | ipsecstat.in_inval++; | |
274 | goto fail; | |
275 | } | |
276 | ||
1c79356b A |
277 | /* |
278 | * some of IP header fields are flipped to the host endian. | |
279 | * convert them back to network endian. VERY stupid. | |
280 | */ | |
1c79356b | 281 | ip->ip_len = htons(ip->ip_len + hlen); |
1c79356b | 282 | ip->ip_off = htons(ip->ip_off); |
9bccf70c A |
283 | if (ah4_calccksum(m, (caddr_t)cksum, siz1, algo, sav)) { |
284 | FREE(cksum, M_TEMP); | |
1c79356b A |
285 | ipsecstat.in_inval++; |
286 | goto fail; | |
287 | } | |
288 | ipsecstat.in_ahhist[sav->alg_auth]++; | |
1c79356b A |
289 | /* |
290 | * flip them back. | |
291 | */ | |
1c79356b | 292 | ip->ip_len = ntohs(ip->ip_len) - hlen; |
1c79356b | 293 | ip->ip_off = ntohs(ip->ip_off); |
1c79356b A |
294 | |
295 | { | |
296 | caddr_t sumpos = NULL; | |
297 | ||
298 | if (sav->flags & SADB_X_EXT_OLD) { | |
299 | /* RFC 1826 */ | |
300 | sumpos = (caddr_t)(ah + 1); | |
301 | } else { | |
302 | /* RFC 2402 */ | |
303 | sumpos = (caddr_t)(((struct newah *)ah) + 1); | |
304 | } | |
305 | ||
306 | if (bcmp(sumpos, cksum, siz) != 0) { | |
307 | ipseclog((LOG_WARNING, | |
308 | "checksum mismatch in IPv4 AH input: %s %s\n", | |
309 | ipsec4_logpacketstr(ip, spi), ipsec_logsastr(sav))); | |
9bccf70c | 310 | FREE(cksum, M_TEMP); |
1c79356b A |
311 | ipsecstat.in_ahauthfail++; |
312 | goto fail; | |
313 | } | |
314 | } | |
315 | ||
9bccf70c | 316 | FREE(cksum, M_TEMP); |
1c79356b A |
317 | |
318 | m->m_flags |= M_AUTHIPHDR; | |
319 | m->m_flags |= M_AUTHIPDGM; | |
320 | ||
321 | #if 0 | |
322 | /* | |
323 | * looks okey, but we need more sanity check. | |
324 | * XXX should elaborate. | |
325 | */ | |
326 | if (ah->ah_nxt == IPPROTO_IPIP || ah->ah_nxt == IPPROTO_IP) { | |
327 | struct ip *nip; | |
328 | size_t sizoff; | |
329 | ||
330 | sizoff = (sav->flags & SADB_X_EXT_OLD) ? 0 : 4; | |
331 | ||
332 | if (m->m_len < off + sizeof(struct ah) + sizoff + siz1 + hlen) { | |
333 | m = m_pullup(m, off + sizeof(struct ah) | |
334 | + sizoff + siz1 + hlen); | |
335 | if (!m) { | |
336 | ipseclog((LOG_DEBUG, | |
337 | "IPv4 AH input: can't pullup\n")); | |
338 | ipsecstat.in_inval++; | |
339 | goto fail; | |
340 | } | |
341 | } | |
342 | ||
343 | nip = (struct ip *)((u_char *)(ah + 1) + sizoff + siz1); | |
344 | if (nip->ip_src.s_addr != ip->ip_src.s_addr | |
345 | || nip->ip_dst.s_addr != ip->ip_dst.s_addr) { | |
346 | m->m_flags &= ~M_AUTHIPHDR; | |
347 | m->m_flags &= ~M_AUTHIPDGM; | |
348 | } | |
349 | } | |
350 | #if INET6 | |
351 | else if (ah->ah_nxt == IPPROTO_IPV6) { | |
352 | m->m_flags &= ~M_AUTHIPHDR; | |
353 | m->m_flags &= ~M_AUTHIPDGM; | |
354 | } | |
355 | #endif /*INET6*/ | |
356 | #endif /*0*/ | |
357 | ||
358 | if (m->m_flags & M_AUTHIPHDR | |
359 | && m->m_flags & M_AUTHIPDGM) { | |
360 | #if 0 | |
361 | ipseclog((LOG_DEBUG, | |
362 | "IPv4 AH input: authentication succeess\n")); | |
363 | #endif | |
364 | ipsecstat.in_ahauthsucc++; | |
365 | } else { | |
366 | ipseclog((LOG_WARNING, | |
367 | "authentication failed in IPv4 AH input: %s %s\n", | |
368 | ipsec4_logpacketstr(ip, spi), ipsec_logsastr(sav))); | |
369 | ipsecstat.in_ahauthfail++; | |
370 | goto fail; | |
371 | } | |
372 | ||
373 | /* | |
374 | * update sequence number. | |
375 | */ | |
376 | if ((sav->flags & SADB_X_EXT_OLD) == 0 && sav->replay) { | |
377 | if (ipsec_updatereplay(ntohl(((struct newah *)ah)->ah_seq), sav)) { | |
378 | ipsecstat.in_ahreplay++; | |
379 | goto fail; | |
380 | } | |
381 | } | |
382 | ||
383 | /* was it transmitted over the IPsec tunnel SA? */ | |
9bccf70c A |
384 | if (sav->flags & SADB_X_EXT_OLD) { |
385 | /* RFC 1826 */ | |
386 | stripsiz = sizeof(struct ah) + siz1; | |
387 | } else { | |
388 | /* RFC 2402 */ | |
389 | stripsiz = sizeof(struct newah) + siz1; | |
390 | } | |
391 | if (ipsec4_tunnel_validate(m, off + stripsiz, nxt, sav)) { | |
1c79356b A |
392 | /* |
393 | * strip off all the headers that precedes AH. | |
394 | * IP xx AH IP' payload -> IP' payload | |
395 | * | |
396 | * XXX more sanity checks | |
397 | * XXX relationship with gif? | |
398 | */ | |
1c79356b A |
399 | u_int8_t tos; |
400 | ||
401 | tos = ip->ip_tos; | |
1c79356b A |
402 | m_adj(m, off + stripsiz); |
403 | if (m->m_len < sizeof(*ip)) { | |
404 | m = m_pullup(m, sizeof(*ip)); | |
405 | if (!m) { | |
406 | ipsecstat.in_inval++; | |
407 | goto fail; | |
408 | } | |
409 | } | |
410 | ip = mtod(m, struct ip *); | |
411 | /* ECN consideration. */ | |
412 | ip_ecn_egress(ip4_ipsec_ecn, &tos, &ip->ip_tos); | |
413 | if (!key_checktunnelsanity(sav, AF_INET, | |
414 | (caddr_t)&ip->ip_src, (caddr_t)&ip->ip_dst)) { | |
415 | ipseclog((LOG_NOTICE, "ipsec tunnel address mismatch " | |
416 | "in IPv4 AH input: %s %s\n", | |
417 | ipsec4_logpacketstr(ip, spi), ipsec_logsastr(sav))); | |
418 | ipsecstat.in_inval++; | |
419 | goto fail; | |
420 | } | |
421 | ||
1c79356b A |
422 | #if 1 |
423 | /* | |
424 | * Should the inner packet be considered authentic? | |
425 | * My current answer is: NO. | |
426 | * | |
427 | * host1 -- gw1 === gw2 -- host2 | |
428 | * In this case, gw2 can trust the authenticity of the | |
429 | * outer packet, but NOT inner. Packet may be altered | |
430 | * between host1 and gw1. | |
431 | * | |
432 | * host1 -- gw1 === host2 | |
433 | * This case falls into the same scenario as above. | |
434 | * | |
435 | * host1 === host2 | |
436 | * This case is the only case when we may be able to leave | |
437 | * M_AUTHIPHDR and M_AUTHIPDGM set. | |
438 | * However, if host1 is wrongly configured, and allows | |
439 | * attacker to inject some packet with src=host1 and | |
440 | * dst=host2, you are in risk. | |
441 | */ | |
442 | m->m_flags &= ~M_AUTHIPHDR; | |
443 | m->m_flags &= ~M_AUTHIPDGM; | |
444 | #endif | |
445 | ||
446 | key_sa_recordxfer(sav, m); | |
9bccf70c A |
447 | if (ipsec_addhist(m, IPPROTO_AH, spi) != 0 || |
448 | ipsec_addhist(m, IPPROTO_IPV4, 0) != 0) { | |
449 | ipsecstat.in_nomem++; | |
450 | goto fail; | |
451 | } | |
91447636 A |
452 | lck_mtx_unlock(sadb_mutex); |
453 | proto_input(PF_INET, m); | |
454 | lck_mtx_lock(sadb_mutex); | |
1c79356b A |
455 | nxt = IPPROTO_DONE; |
456 | } else { | |
457 | /* | |
458 | * strip off AH. | |
1c79356b | 459 | */ |
1c79356b A |
460 | |
461 | ip = mtod(m, struct ip *); | |
462 | #ifndef PULLDOWN_TEST | |
9bccf70c A |
463 | /* |
464 | * We do deep-copy since KAME requires that | |
465 | * the packet is placed in a single external mbuf. | |
466 | */ | |
1c79356b A |
467 | ovbcopy((caddr_t)ip, (caddr_t)(((u_char *)ip) + stripsiz), off); |
468 | m->m_data += stripsiz; | |
469 | m->m_len -= stripsiz; | |
470 | m->m_pkthdr.len -= stripsiz; | |
471 | #else | |
472 | /* | |
473 | * even in m_pulldown case, we need to strip off AH so that | |
474 | * we can compute checksum for multiple AH correctly. | |
475 | */ | |
476 | if (m->m_len >= stripsiz + off) { | |
477 | ovbcopy((caddr_t)ip, ((caddr_t)ip) + stripsiz, off); | |
478 | m->m_data += stripsiz; | |
479 | m->m_len -= stripsiz; | |
480 | m->m_pkthdr.len -= stripsiz; | |
481 | } else { | |
482 | /* | |
483 | * this comes with no copy if the boundary is on | |
484 | * cluster | |
485 | */ | |
486 | struct mbuf *n; | |
487 | ||
488 | n = m_split(m, off, M_DONTWAIT); | |
489 | if (n == NULL) { | |
490 | /* m is retained by m_split */ | |
491 | goto fail; | |
492 | } | |
493 | m_adj(n, stripsiz); | |
1c79356b A |
494 | /* m_cat does not update m_pkthdr.len */ |
495 | m->m_pkthdr.len += n->m_pkthdr.len; | |
55e303ae | 496 | m_cat(m, n); |
1c79356b A |
497 | } |
498 | #endif | |
499 | ||
500 | if (m->m_len < sizeof(*ip)) { | |
501 | m = m_pullup(m, sizeof(*ip)); | |
502 | if (m == NULL) { | |
503 | ipsecstat.in_inval++; | |
504 | goto fail; | |
505 | } | |
506 | } | |
507 | ip = mtod(m, struct ip *); | |
508 | #ifdef IPLEN_FLIPPED | |
509 | ip->ip_len = ip->ip_len - stripsiz; | |
510 | #else | |
511 | ip->ip_len = htons(ntohs(ip->ip_len) - stripsiz); | |
512 | #endif | |
513 | ip->ip_p = nxt; | |
514 | /* forget about IP hdr checksum, the check has already been passed */ | |
515 | ||
516 | key_sa_recordxfer(sav, m); | |
9bccf70c A |
517 | if (ipsec_addhist(m, IPPROTO_AH, spi) != 0) { |
518 | ipsecstat.in_nomem++; | |
519 | goto fail; | |
520 | } | |
1c79356b | 521 | |
9bccf70c A |
522 | if (nxt != IPPROTO_DONE) { |
523 | if ((ip_protox[nxt]->pr_flags & PR_LASTHDR) != 0 && | |
524 | ipsec4_in_reject(m, NULL)) { | |
525 | ipsecstat.in_polvio++; | |
526 | goto fail; | |
527 | } | |
91447636 A |
528 | lck_mtx_unlock(sadb_mutex); |
529 | ip_proto_dispatch_in(m, off, nxt, 0); | |
530 | lck_mtx_lock(sadb_mutex); | |
9bccf70c | 531 | } else |
1c79356b A |
532 | m_freem(m); |
533 | m = NULL; | |
534 | } | |
535 | ||
536 | if (sav) { | |
537 | KEYDEBUG(KEYDEBUG_IPSEC_STAMP, | |
538 | printf("DP ah4_input call free SA:%p\n", sav)); | |
539 | key_freesav(sav); | |
540 | } | |
541 | ipsecstat.in_success++; | |
91447636 | 542 | lck_mtx_unlock(sadb_mutex); |
1c79356b A |
543 | return; |
544 | ||
545 | fail: | |
546 | if (sav) { | |
547 | KEYDEBUG(KEYDEBUG_IPSEC_STAMP, | |
548 | printf("DP ah4_input call free SA:%p\n", sav)); | |
549 | key_freesav(sav); | |
550 | } | |
91447636 | 551 | lck_mtx_unlock(sadb_mutex); |
1c79356b A |
552 | if (m) |
553 | m_freem(m); | |
554 | return; | |
555 | } | |
556 | #endif /* INET */ | |
557 | ||
558 | #if INET6 | |
559 | int | |
55e303ae | 560 | ah6_input(mp, offp) |
1c79356b | 561 | struct mbuf **mp; |
55e303ae | 562 | int *offp; |
1c79356b A |
563 | { |
564 | struct mbuf *m = *mp; | |
565 | int off = *offp; | |
566 | struct ip6_hdr *ip6; | |
567 | struct ah *ah; | |
568 | u_int32_t spi; | |
9bccf70c | 569 | const struct ah_algorithm *algo; |
1c79356b A |
570 | size_t siz; |
571 | size_t siz1; | |
572 | u_char *cksum; | |
573 | struct secasvar *sav = NULL; | |
574 | u_int16_t nxt; | |
575 | int s; | |
9bccf70c | 576 | size_t stripsiz = 0; |
1c79356b | 577 | |
91447636 A |
578 | lck_mtx_lock(sadb_mutex); |
579 | ||
1c79356b | 580 | #ifndef PULLDOWN_TEST |
91447636 | 581 | IP6_EXTHDR_CHECK(m, off, sizeof(struct ah), {lck_mtx_unlock(sadb_mutex);return IPPROTO_DONE;}); |
1c79356b A |
582 | ah = (struct ah *)(mtod(m, caddr_t) + off); |
583 | #else | |
584 | IP6_EXTHDR_GET(ah, struct ah *, m, off, sizeof(struct newah)); | |
585 | if (ah == NULL) { | |
586 | ipseclog((LOG_DEBUG, "IPv6 AH input: can't pullup\n")); | |
9bccf70c | 587 | ipsec6stat.in_inval++; |
91447636 | 588 | lck_mtx_unlock(sadb_mutex); |
1c79356b A |
589 | return IPPROTO_DONE; |
590 | } | |
591 | #endif | |
592 | ip6 = mtod(m, struct ip6_hdr *); | |
593 | nxt = ah->ah_nxt; | |
594 | ||
595 | /* find the sassoc. */ | |
596 | spi = ah->ah_spi; | |
597 | ||
598 | if (ntohs(ip6->ip6_plen) == 0) { | |
599 | ipseclog((LOG_ERR, "IPv6 AH input: " | |
600 | "AH with IPv6 jumbogram is not supported.\n")); | |
601 | ipsec6stat.in_inval++; | |
602 | goto fail; | |
603 | } | |
604 | ||
605 | if ((sav = key_allocsa(AF_INET6, | |
606 | (caddr_t)&ip6->ip6_src, (caddr_t)&ip6->ip6_dst, | |
607 | IPPROTO_AH, spi)) == 0) { | |
608 | ipseclog((LOG_WARNING, | |
609 | "IPv6 AH input: no key association found for spi %u\n", | |
610 | (u_int32_t)ntohl(spi))); | |
611 | ipsec6stat.in_nosa++; | |
612 | goto fail; | |
613 | } | |
614 | KEYDEBUG(KEYDEBUG_IPSEC_STAMP, | |
615 | printf("DP ah6_input called to allocate SA:%p\n", sav)); | |
616 | if (sav->state != SADB_SASTATE_MATURE | |
617 | && sav->state != SADB_SASTATE_DYING) { | |
618 | ipseclog((LOG_DEBUG, | |
619 | "IPv6 AH input: non-mature/dying SA found for spi %u; ", | |
620 | (u_int32_t)ntohl(spi))); | |
621 | ipsec6stat.in_badspi++; | |
622 | goto fail; | |
623 | } | |
9bccf70c A |
624 | |
625 | algo = ah_algorithm_lookup(sav->alg_auth); | |
626 | if (!algo) { | |
1c79356b | 627 | ipseclog((LOG_DEBUG, "IPv6 AH input: " |
9bccf70c | 628 | "unsupported authentication algorithm for spi %u\n", |
1c79356b A |
629 | (u_int32_t)ntohl(spi))); |
630 | ipsec6stat.in_badspi++; | |
631 | goto fail; | |
632 | } | |
633 | ||
1c79356b A |
634 | siz = (*algo->sumsiz)(sav); |
635 | siz1 = ((siz + 3) & ~(4 - 1)); | |
636 | ||
637 | /* | |
638 | * sanity checks for header, 1. | |
639 | */ | |
640 | { | |
641 | int sizoff; | |
642 | ||
643 | sizoff = (sav->flags & SADB_X_EXT_OLD) ? 0 : 4; | |
644 | ||
9bccf70c A |
645 | /* |
646 | * Here, we do not do "siz1 == siz". See ah4_input() for complete | |
647 | * description. | |
648 | */ | |
649 | if (siz1 < siz) { | |
650 | ipseclog((LOG_NOTICE, "sum length too short in IPv6 AH input " | |
651 | "(%lu, should be at least %lu): %s\n", | |
652 | (u_long)siz1, (u_long)siz, | |
653 | ipsec6_logpacketstr(ip6, spi))); | |
654 | ipsec6stat.in_inval++; | |
655 | goto fail; | |
656 | } | |
1c79356b A |
657 | if ((ah->ah_len << 2) - sizoff != siz1) { |
658 | ipseclog((LOG_NOTICE, "sum length mismatch in IPv6 AH input " | |
9bccf70c A |
659 | "(%d should be %lu): %s\n", |
660 | (ah->ah_len << 2) - sizoff, (u_long)siz1, | |
661 | ipsec6_logpacketstr(ip6, spi))); | |
1c79356b A |
662 | ipsec6stat.in_inval++; |
663 | goto fail; | |
664 | } | |
665 | #ifndef PULLDOWN_TEST | |
91447636 A |
666 | IP6_EXTHDR_CHECK(m, off, sizeof(struct ah) + sizoff + siz1, |
667 | {lck_mtx_unlock(sadb_mutex);return IPPROTO_DONE;}); | |
1c79356b A |
668 | #else |
669 | IP6_EXTHDR_GET(ah, struct ah *, m, off, | |
670 | sizeof(struct ah) + sizoff + siz1); | |
671 | if (ah == NULL) { | |
672 | ipseclog((LOG_NOTICE, "couldn't pullup gather IPv6 AH checksum part")); | |
9bccf70c | 673 | ipsec6stat.in_inval++; |
1c79356b A |
674 | m = NULL; |
675 | goto fail; | |
676 | } | |
677 | #endif | |
678 | } | |
679 | ||
680 | /* | |
681 | * check for sequence number. | |
682 | */ | |
683 | if ((sav->flags & SADB_X_EXT_OLD) == 0 && sav->replay) { | |
684 | if (ipsec_chkreplay(ntohl(((struct newah *)ah)->ah_seq), sav)) | |
685 | ; /*okey*/ | |
686 | else { | |
687 | ipsec6stat.in_ahreplay++; | |
688 | ipseclog((LOG_WARNING, | |
689 | "replay packet in IPv6 AH input: %s %s\n", | |
690 | ipsec6_logpacketstr(ip6, spi), | |
691 | ipsec_logsastr(sav))); | |
692 | goto fail; | |
693 | } | |
694 | } | |
695 | ||
696 | /* | |
697 | * alright, it seems sane. now we are going to check the | |
698 | * cryptographic checksum. | |
699 | */ | |
700 | cksum = _MALLOC(siz1, M_TEMP, M_NOWAIT); | |
701 | if (!cksum) { | |
702 | ipseclog((LOG_DEBUG, "IPv6 AH input: " | |
703 | "couldn't alloc temporary region for cksum\n")); | |
704 | ipsec6stat.in_inval++; | |
705 | goto fail; | |
706 | } | |
707 | ||
9bccf70c A |
708 | if (ah6_calccksum(m, (caddr_t)cksum, siz1, algo, sav)) { |
709 | FREE(cksum, M_TEMP); | |
1c79356b A |
710 | ipsec6stat.in_inval++; |
711 | goto fail; | |
712 | } | |
713 | ipsec6stat.in_ahhist[sav->alg_auth]++; | |
714 | ||
715 | { | |
716 | caddr_t sumpos = NULL; | |
717 | ||
718 | if (sav->flags & SADB_X_EXT_OLD) { | |
719 | /* RFC 1826 */ | |
720 | sumpos = (caddr_t)(ah + 1); | |
721 | } else { | |
722 | /* RFC 2402 */ | |
723 | sumpos = (caddr_t)(((struct newah *)ah) + 1); | |
724 | } | |
725 | ||
726 | if (bcmp(sumpos, cksum, siz) != 0) { | |
727 | ipseclog((LOG_WARNING, | |
728 | "checksum mismatch in IPv6 AH input: %s %s\n", | |
729 | ipsec6_logpacketstr(ip6, spi), ipsec_logsastr(sav))); | |
9bccf70c | 730 | FREE(cksum, M_TEMP); |
1c79356b A |
731 | ipsec6stat.in_ahauthfail++; |
732 | goto fail; | |
733 | } | |
734 | } | |
735 | ||
9bccf70c | 736 | FREE(cksum, M_TEMP); |
1c79356b A |
737 | |
738 | m->m_flags |= M_AUTHIPHDR; | |
739 | m->m_flags |= M_AUTHIPDGM; | |
740 | ||
741 | #if 0 | |
742 | /* | |
743 | * looks okey, but we need more sanity check. | |
744 | * XXX should elaborate. | |
745 | */ | |
746 | if (ah->ah_nxt == IPPROTO_IPV6) { | |
747 | struct ip6_hdr *nip6; | |
748 | size_t sizoff; | |
749 | ||
750 | sizoff = (sav->flags & SADB_X_EXT_OLD) ? 0 : 4; | |
751 | ||
752 | IP6_EXTHDR_CHECK(m, off, sizeof(struct ah) + sizoff + siz1 | |
91447636 A |
753 | + sizeof(struct ip6_hdr), |
754 | {lck_mtx_unlock(sadb_mutex);return IPPROTO_DONE;}); | |
1c79356b A |
755 | |
756 | nip6 = (struct ip6_hdr *)((u_char *)(ah + 1) + sizoff + siz1); | |
757 | if (!IN6_ARE_ADDR_EQUAL(&nip6->ip6_src, &ip6->ip6_src) | |
758 | || !IN6_ARE_ADDR_EQUAL(&nip6->ip6_dst, &ip6->ip6_dst)) { | |
759 | m->m_flags &= ~M_AUTHIPHDR; | |
760 | m->m_flags &= ~M_AUTHIPDGM; | |
761 | } | |
762 | } else if (ah->ah_nxt == IPPROTO_IPIP) { | |
763 | m->m_flags &= ~M_AUTHIPHDR; | |
764 | m->m_flags &= ~M_AUTHIPDGM; | |
765 | } else if (ah->ah_nxt == IPPROTO_IP) { | |
766 | m->m_flags &= ~M_AUTHIPHDR; | |
767 | m->m_flags &= ~M_AUTHIPDGM; | |
768 | } | |
769 | #endif | |
770 | ||
771 | if (m->m_flags & M_AUTHIPHDR | |
772 | && m->m_flags & M_AUTHIPDGM) { | |
773 | #if 0 | |
774 | ipseclog((LOG_DEBUG, | |
775 | "IPv6 AH input: authentication succeess\n")); | |
776 | #endif | |
777 | ipsec6stat.in_ahauthsucc++; | |
778 | } else { | |
779 | ipseclog((LOG_WARNING, | |
780 | "authentication failed in IPv6 AH input: %s %s\n", | |
781 | ipsec6_logpacketstr(ip6, spi), ipsec_logsastr(sav))); | |
782 | ipsec6stat.in_ahauthfail++; | |
783 | goto fail; | |
784 | } | |
785 | ||
786 | /* | |
787 | * update sequence number. | |
788 | */ | |
789 | if ((sav->flags & SADB_X_EXT_OLD) == 0 && sav->replay) { | |
790 | if (ipsec_updatereplay(ntohl(((struct newah *)ah)->ah_seq), sav)) { | |
791 | ipsec6stat.in_ahreplay++; | |
792 | goto fail; | |
793 | } | |
794 | } | |
795 | ||
796 | /* was it transmitted over the IPsec tunnel SA? */ | |
9bccf70c A |
797 | if (sav->flags & SADB_X_EXT_OLD) { |
798 | /* RFC 1826 */ | |
799 | stripsiz = sizeof(struct ah) + siz1; | |
800 | } else { | |
801 | /* RFC 2402 */ | |
802 | stripsiz = sizeof(struct newah) + siz1; | |
803 | } | |
804 | if (ipsec6_tunnel_validate(m, off + stripsiz, nxt, sav)) { | |
1c79356b A |
805 | /* |
806 | * strip off all the headers that precedes AH. | |
807 | * IP6 xx AH IP6' payload -> IP6' payload | |
808 | * | |
809 | * XXX more sanity checks | |
810 | * XXX relationship with gif? | |
811 | */ | |
1c79356b A |
812 | u_int32_t flowinfo; /*net endian*/ |
813 | ||
814 | flowinfo = ip6->ip6_flow; | |
1c79356b A |
815 | m_adj(m, off + stripsiz); |
816 | if (m->m_len < sizeof(*ip6)) { | |
817 | /* | |
818 | * m_pullup is prohibited in KAME IPv6 input processing | |
819 | * but there's no other way! | |
820 | */ | |
821 | m = m_pullup(m, sizeof(*ip6)); | |
822 | if (!m) { | |
823 | ipsec6stat.in_inval++; | |
824 | goto fail; | |
825 | } | |
826 | } | |
827 | ip6 = mtod(m, struct ip6_hdr *); | |
828 | /* ECN consideration. */ | |
829 | ip6_ecn_egress(ip6_ipsec_ecn, &flowinfo, &ip6->ip6_flow); | |
830 | if (!key_checktunnelsanity(sav, AF_INET6, | |
831 | (caddr_t)&ip6->ip6_src, (caddr_t)&ip6->ip6_dst)) { | |
832 | ipseclog((LOG_NOTICE, "ipsec tunnel address mismatch " | |
833 | "in IPv6 AH input: %s %s\n", | |
834 | ipsec6_logpacketstr(ip6, spi), | |
835 | ipsec_logsastr(sav))); | |
836 | ipsec6stat.in_inval++; | |
837 | goto fail; | |
838 | } | |
839 | ||
1c79356b A |
840 | #if 1 |
841 | /* | |
842 | * should the inner packet be considered authentic? | |
843 | * see comment in ah4_input(). | |
844 | */ | |
845 | m->m_flags &= ~M_AUTHIPHDR; | |
846 | m->m_flags &= ~M_AUTHIPDGM; | |
847 | #endif | |
848 | ||
849 | key_sa_recordxfer(sav, m); | |
9bccf70c A |
850 | if (ipsec_addhist(m, IPPROTO_AH, spi) != 0 || |
851 | ipsec_addhist(m, IPPROTO_IPV6, 0) != 0) { | |
852 | ipsec6stat.in_nomem++; | |
853 | goto fail; | |
854 | } | |
91447636 A |
855 | lck_mtx_unlock(sadb_mutex); |
856 | proto_input(PF_INET6, m); | |
857 | lck_mtx_lock(sadb_mutex); | |
1c79356b A |
858 | nxt = IPPROTO_DONE; |
859 | } else { | |
860 | /* | |
861 | * strip off AH. | |
1c79356b | 862 | */ |
1c79356b A |
863 | char *prvnxtp; |
864 | ||
865 | /* | |
866 | * Copy the value of the next header field of AH to the | |
867 | * next header field of the previous header. | |
868 | * This is necessary because AH will be stripped off below. | |
869 | */ | |
870 | prvnxtp = ip6_get_prevhdr(m, off); /* XXX */ | |
871 | *prvnxtp = nxt; | |
872 | ||
1c79356b A |
873 | ip6 = mtod(m, struct ip6_hdr *); |
874 | #ifndef PULLDOWN_TEST | |
9bccf70c A |
875 | /* |
876 | * We do deep-copy since KAME requires that | |
877 | * the packet is placed in a single mbuf. | |
878 | */ | |
1c79356b A |
879 | ovbcopy((caddr_t)ip6, ((caddr_t)ip6) + stripsiz, off); |
880 | m->m_data += stripsiz; | |
881 | m->m_len -= stripsiz; | |
882 | m->m_pkthdr.len -= stripsiz; | |
883 | #else | |
884 | /* | |
885 | * even in m_pulldown case, we need to strip off AH so that | |
886 | * we can compute checksum for multiple AH correctly. | |
887 | */ | |
888 | if (m->m_len >= stripsiz + off) { | |
889 | ovbcopy((caddr_t)ip6, ((caddr_t)ip6) + stripsiz, off); | |
890 | m->m_data += stripsiz; | |
891 | m->m_len -= stripsiz; | |
892 | m->m_pkthdr.len -= stripsiz; | |
893 | } else { | |
894 | /* | |
895 | * this comes with no copy if the boundary is on | |
896 | * cluster | |
897 | */ | |
898 | struct mbuf *n; | |
899 | ||
900 | n = m_split(m, off, M_DONTWAIT); | |
901 | if (n == NULL) { | |
902 | /* m is retained by m_split */ | |
903 | goto fail; | |
904 | } | |
905 | m_adj(n, stripsiz); | |
1c79356b A |
906 | /* m_cat does not update m_pkthdr.len */ |
907 | m->m_pkthdr.len += n->m_pkthdr.len; | |
55e303ae | 908 | m_cat(m, n); |
1c79356b A |
909 | } |
910 | #endif | |
911 | ip6 = mtod(m, struct ip6_hdr *); | |
912 | /* XXX jumbogram */ | |
913 | ip6->ip6_plen = htons(ntohs(ip6->ip6_plen) - stripsiz); | |
914 | ||
915 | key_sa_recordxfer(sav, m); | |
9bccf70c A |
916 | if (ipsec_addhist(m, IPPROTO_AH, spi) != 0) { |
917 | ipsec6stat.in_nomem++; | |
918 | goto fail; | |
919 | } | |
1c79356b A |
920 | } |
921 | ||
922 | *offp = off; | |
923 | *mp = m; | |
924 | ||
925 | if (sav) { | |
926 | KEYDEBUG(KEYDEBUG_IPSEC_STAMP, | |
927 | printf("DP ah6_input call free SA:%p\n", sav)); | |
928 | key_freesav(sav); | |
929 | } | |
930 | ipsec6stat.in_success++; | |
91447636 | 931 | lck_mtx_unlock(sadb_mutex); |
1c79356b A |
932 | return nxt; |
933 | ||
934 | fail: | |
935 | if (sav) { | |
936 | KEYDEBUG(KEYDEBUG_IPSEC_STAMP, | |
937 | printf("DP ah6_input call free SA:%p\n", sav)); | |
938 | key_freesav(sav); | |
939 | } | |
91447636 | 940 | lck_mtx_unlock(sadb_mutex); |
1c79356b A |
941 | if (m) |
942 | m_freem(m); | |
943 | return IPPROTO_DONE; | |
944 | } | |
9bccf70c A |
945 | |
946 | void | |
947 | ah6_ctlinput(cmd, sa, d) | |
948 | int cmd; | |
949 | struct sockaddr *sa; | |
950 | void *d; | |
951 | { | |
952 | const struct newah *ahp; | |
953 | struct newah ah; | |
954 | struct secasvar *sav; | |
955 | struct ip6_hdr *ip6; | |
956 | struct mbuf *m; | |
957 | struct ip6ctlparam *ip6cp = NULL; | |
958 | int off; | |
55e303ae | 959 | struct sockaddr_in6 *sa6_src, *sa6_dst; |
9bccf70c A |
960 | |
961 | if (sa->sa_family != AF_INET6 || | |
962 | sa->sa_len != sizeof(struct sockaddr_in6)) | |
963 | return; | |
964 | if ((unsigned)cmd >= PRC_NCMDS) | |
965 | return; | |
966 | ||
967 | /* if the parameter is from icmp6, decode it. */ | |
968 | if (d != NULL) { | |
969 | ip6cp = (struct ip6ctlparam *)d; | |
970 | m = ip6cp->ip6c_m; | |
971 | ip6 = ip6cp->ip6c_ip6; | |
972 | off = ip6cp->ip6c_off; | |
973 | } else { | |
974 | m = NULL; | |
975 | ip6 = NULL; | |
976 | } | |
977 | ||
978 | if (ip6) { | |
979 | /* | |
980 | * XXX: We assume that when ip6 is non NULL, | |
981 | * M and OFF are valid. | |
982 | */ | |
983 | ||
984 | /* check if we can safely examine src and dst ports */ | |
985 | if (m->m_pkthdr.len < off + sizeof(ah)) | |
986 | return; | |
987 | ||
988 | if (m->m_len < off + sizeof(ah)) { | |
989 | /* | |
990 | * this should be rare case, | |
991 | * so we compromise on this copy... | |
992 | */ | |
993 | m_copydata(m, off, sizeof(ah), (caddr_t)&ah); | |
994 | ahp = &ah; | |
995 | } else | |
996 | ahp = (struct newah *)(mtod(m, caddr_t) + off); | |
997 | ||
998 | if (cmd == PRC_MSGSIZE) { | |
999 | int valid = 0; | |
1000 | ||
1001 | /* | |
1002 | * Check to see if we have a valid SA corresponding to | |
1003 | * the address in the ICMP message payload. | |
1004 | */ | |
55e303ae A |
1005 | sa6_src = ip6cp->ip6c_src; |
1006 | sa6_dst = (struct sockaddr_in6 *)sa; | |
91447636 | 1007 | lck_mtx_lock(sadb_mutex); |
9bccf70c | 1008 | sav = key_allocsa(AF_INET6, |
55e303ae A |
1009 | (caddr_t)&sa6_src->sin6_addr, |
1010 | (caddr_t)&sa6_dst->sin6_addr, | |
9bccf70c A |
1011 | IPPROTO_AH, ahp->ah_spi); |
1012 | if (sav) { | |
1013 | if (sav->state == SADB_SASTATE_MATURE || | |
1014 | sav->state == SADB_SASTATE_DYING) | |
1015 | valid++; | |
1016 | key_freesav(sav); | |
1017 | } | |
91447636 | 1018 | lck_mtx_unlock(sadb_mutex); |
9bccf70c A |
1019 | |
1020 | /* XXX Further validation? */ | |
1021 | ||
1022 | /* | |
1023 | * Depending on the value of "valid" and routing table | |
1024 | * size (mtudisc_{hi,lo}wat), we will: | |
1025 | * - recalcurate the new MTU and create the | |
1026 | * corresponding routing entry, or | |
1027 | * - ignore the MTU change notification. | |
1028 | */ | |
1029 | icmp6_mtudisc_update((struct ip6ctlparam *)d, valid); | |
1030 | } | |
1031 | ||
1032 | /* we normally notify single pcb here */ | |
1033 | } else { | |
1034 | /* we normally notify any pcb here */ | |
1035 | } | |
1036 | } | |
1c79356b | 1037 | #endif /* INET6 */ |