ipsec_input.c revision 1.69 1 /* $NetBSD: ipsec_input.c,v 1.69 2018/04/29 14:54:09 maxv Exp $ */
2 /* $FreeBSD: ipsec_input.c,v 1.2.4.2 2003/03/28 20:32:53 sam Exp $ */
3 /* $OpenBSD: ipsec_input.c,v 1.63 2003/02/20 18:35:43 deraadt Exp $ */
4
5 /*
6 * The authors of this code are John Ioannidis (ji (at) tla.org),
7 * Angelos D. Keromytis (kermit (at) csd.uch.gr) and
8 * Niels Provos (provos (at) physnet.uni-hamburg.de).
9 *
10 * This code was written by John Ioannidis for BSD/OS in Athens, Greece,
11 * in November 1995.
12 *
13 * Ported to OpenBSD and NetBSD, with additional transforms, in December 1996,
14 * by Angelos D. Keromytis.
15 *
16 * Additional transforms and features in 1997 and 1998 by Angelos D. Keromytis
17 * and Niels Provos.
18 *
19 * Additional features in 1999 by Angelos D. Keromytis.
20 *
21 * Copyright (C) 1995, 1996, 1997, 1998, 1999 by John Ioannidis,
22 * Angelos D. Keromytis and Niels Provos.
23 * Copyright (c) 2001, Angelos D. Keromytis.
24 *
25 * Permission to use, copy, and modify this software with or without fee
26 * is hereby granted, provided that this entire notice is included in
27 * all copies of any software which is or includes a copy or
28 * modification of this software.
29 * You may use this code under the GNU public license if you so wish. Please
30 * contribute changes back to the authors under this freer than GPL license
31 * so that we may further the use of strong encryption without limitations to
32 * all.
33 *
34 * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR
35 * IMPLIED WARRANTY. IN PARTICULAR, NONE OF THE AUTHORS MAKES ANY
36 * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE
37 * MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR
38 * PURPOSE.
39 */
40
41 #include <sys/cdefs.h>
42 __KERNEL_RCSID(0, "$NetBSD: ipsec_input.c,v 1.69 2018/04/29 14:54:09 maxv Exp $");
43
44 /*
45 * IPsec input processing.
46 */
47
48 #if defined(_KERNEL_OPT)
49 #include "opt_inet.h"
50 #endif
51
52 #include <sys/param.h>
53 #include <sys/systm.h>
54 #include <sys/mbuf.h>
55 #include <sys/domain.h>
56 #include <sys/protosw.h>
57 #include <sys/socket.h>
58 #include <sys/errno.h>
59 #include <sys/syslog.h>
60
61 #include <net/if.h>
62 #include <net/route.h>
63
64 #include <netinet/in.h>
65 #include <netinet/in_systm.h>
66 #include <netinet/ip.h>
67 #include <netinet/ip_var.h>
68 #include <netinet/in_var.h>
69 #include <netinet/in_proto.h>
70 #include <netinet/udp.h>
71 #include <netinet/tcp.h>
72
73 #include <netinet/ip6.h>
74 #ifdef INET6
75 #include <netinet6/in6.h>
76 #include <netinet6/ip6_var.h>
77 #include <netinet6/ip6_private.h>
78 #include <netinet6/scope6_var.h>
79 #endif
80 #include <netinet/in_pcb.h>
81
82 #include <netipsec/ipsec.h>
83 #include <netipsec/ipsec_private.h>
84 #ifdef INET6
85 #include <netipsec/ipsec6.h>
86 #endif
87 #include <netipsec/ah_var.h>
88 #include <netipsec/esp.h>
89 #include <netipsec/esp_var.h>
90 #include <netipsec/ipcomp_var.h>
91
92 #include <netipsec/key.h>
93 #include <netipsec/keydb.h>
94
95 #include <netipsec/xform.h>
96 #include <netinet6/ip6protosw.h>
97
98 #define IPSEC_ISTAT(p, x, y, z) \
99 do { \
100 switch (p) { \
101 case IPPROTO_ESP: \
102 ESP_STATINC(x); \
103 break; \
104 case IPPROTO_AH: \
105 AH_STATINC(y); \
106 break; \
107 default: \
108 IPCOMP_STATINC(z); \
109 break; \
110 } \
111 } while (/*CONSTCOND*/0)
112
113 /*
114 * fixup TCP/UDP checksum
115 *
116 * XXX: if we have NAT-OA payload from IKE server,
117 * we must do the differential update of checksum.
118 *
119 * XXX: NAT-OAi/NAT-OAr drived from IKE initiator/responder.
120 * how to know the IKE side from kernel?
121 */
122 static struct mbuf *
123 ipsec4_fixup_checksum(struct mbuf *m)
124 {
125 struct ip *ip;
126 struct tcphdr *th;
127 struct udphdr *uh;
128 int poff, off;
129 int plen;
130
131 if (m->m_len < sizeof(*ip)) {
132 m = m_pullup(m, sizeof(*ip));
133 if (m == NULL)
134 return NULL;
135 }
136 ip = mtod(m, struct ip *);
137 poff = ip->ip_hl << 2;
138 plen = ntohs(ip->ip_len) - poff;
139
140 switch (ip->ip_p) {
141 case IPPROTO_TCP:
142 IP6_EXTHDR_GET(th, struct tcphdr *, m, poff, sizeof(*th));
143 if (th == NULL)
144 return NULL;
145 off = th->th_off << 2;
146 if (off < sizeof(*th) || off > plen) {
147 m_freem(m);
148 return NULL;
149 }
150 th->th_sum = 0;
151 th->th_sum = in4_cksum(m, IPPROTO_TCP, poff, plen);
152 break;
153 case IPPROTO_UDP:
154 IP6_EXTHDR_GET(uh, struct udphdr *, m, poff, sizeof(*uh));
155 if (uh == NULL)
156 return NULL;
157 off = sizeof(*uh);
158 if (off > plen) {
159 m_freem(m);
160 return NULL;
161 }
162 uh->uh_sum = 0;
163 uh->uh_sum = in4_cksum(m, IPPROTO_UDP, poff, plen);
164 break;
165 default:
166 /* no checksum */
167 return m;
168 }
169
170 return m;
171 }
172
173 /*
174 * ipsec_common_input gets called when an IPsec-protected packet
175 * is received by IPv4 or IPv6. Its job is to find the right SA
176 * and call the appropriate transform. The transform callback
177 * takes care of further processing (like ingress filtering).
178 */
179 static int
180 ipsec_common_input(struct mbuf *m, int skip, int protoff, int af, int sproto)
181 {
182 char buf[IPSEC_ADDRSTRLEN];
183 union sockaddr_union dst_address;
184 struct secasvar *sav;
185 u_int32_t spi;
186 u_int16_t sport;
187 u_int16_t dport;
188 int s, error;
189
190 IPSEC_ISTAT(sproto, ESP_STAT_INPUT, AH_STAT_INPUT,
191 IPCOMP_STAT_INPUT);
192
193 KASSERT(m != NULL);
194
195 if ((sproto == IPPROTO_ESP && !esp_enable) ||
196 (sproto == IPPROTO_AH && !ah_enable) ||
197 (sproto == IPPROTO_IPCOMP && !ipcomp_enable)) {
198 m_freem(m);
199 IPSEC_ISTAT(sproto, ESP_STAT_PDROPS, AH_STAT_PDROPS,
200 IPCOMP_STAT_PDROPS);
201 return EOPNOTSUPP;
202 }
203
204 if (m->m_pkthdr.len - skip < 2 * sizeof(u_int32_t)) {
205 m_freem(m);
206 IPSEC_ISTAT(sproto, ESP_STAT_HDROPS, AH_STAT_HDROPS,
207 IPCOMP_STAT_HDROPS);
208 IPSECLOG(LOG_DEBUG, "packet too small\n");
209 return EINVAL;
210 }
211
212 /* Retrieve the SPI from the relevant IPsec header */
213 if (sproto == IPPROTO_ESP) {
214 m_copydata(m, skip, sizeof(u_int32_t), &spi);
215 } else if (sproto == IPPROTO_AH) {
216 m_copydata(m, skip + sizeof(u_int32_t), sizeof(u_int32_t), &spi);
217 } else if (sproto == IPPROTO_IPCOMP) {
218 u_int16_t cpi;
219 m_copydata(m, skip + sizeof(u_int16_t), sizeof(u_int16_t), &cpi);
220 spi = ntohl(htons(cpi));
221 } else {
222 panic("%s called with bad protocol number: %d\n", __func__,
223 sproto);
224 }
225
226 /* find the source port for NAT-T */
227 nat_t_ports_get(m, &dport, &sport);
228
229 /*
230 * Find the SA and (indirectly) call the appropriate
231 * kernel crypto routine. The resulting mbuf chain is a valid
232 * IP packet ready to go through input processing.
233 */
234 memset(&dst_address, 0, sizeof(dst_address));
235 dst_address.sa.sa_family = af;
236 switch (af) {
237 #ifdef INET
238 case AF_INET:
239 dst_address.sin.sin_len = sizeof(struct sockaddr_in);
240 m_copydata(m, offsetof(struct ip, ip_dst),
241 sizeof(struct in_addr),
242 &dst_address.sin.sin_addr);
243 break;
244 #endif
245 #ifdef INET6
246 case AF_INET6:
247 dst_address.sin6.sin6_len = sizeof(struct sockaddr_in6);
248 m_copydata(m, offsetof(struct ip6_hdr, ip6_dst),
249 sizeof(struct in6_addr),
250 &dst_address.sin6.sin6_addr);
251 if (sa6_recoverscope(&dst_address.sin6)) {
252 m_freem(m);
253 return EINVAL;
254 }
255 break;
256 #endif
257 default:
258 IPSECLOG(LOG_DEBUG, "unsupported protocol family %u\n", af);
259 m_freem(m);
260 IPSEC_ISTAT(sproto, ESP_STAT_NOPF, AH_STAT_NOPF,
261 IPCOMP_STAT_NOPF);
262 return EPFNOSUPPORT;
263 }
264
265 s = splsoftnet();
266
267 /* NB: only pass dst since key_lookup_sa follows RFC2401 */
268 sav = KEY_LOOKUP_SA(&dst_address, sproto, spi, sport, dport);
269 if (sav == NULL) {
270 IPSECLOG(LOG_DEBUG,
271 "no key association found for SA %s/%08lx/%u/%u\n",
272 ipsec_address(&dst_address, buf, sizeof(buf)),
273 (u_long) ntohl(spi), sproto, ntohs(dport));
274 IPSEC_ISTAT(sproto, ESP_STAT_NOTDB, AH_STAT_NOTDB,
275 IPCOMP_STAT_NOTDB);
276 splx(s);
277 m_freem(m);
278 return ENOENT;
279 }
280
281 KASSERT(sav->tdb_xform != NULL);
282
283 /*
284 * Call appropriate transform and return -- callback takes care of
285 * everything else.
286 */
287 error = (*sav->tdb_xform->xf_input)(m, sav, skip, protoff);
288 KEY_SA_UNREF(&sav);
289 splx(s);
290 return error;
291 }
292
293 #ifdef INET
294 /*
295 * Common input handler for IPv4 AH, ESP, and IPCOMP.
296 */
297 void
298 ipsec4_common_input(struct mbuf *m, ...)
299 {
300 va_list ap;
301 int off, nxt;
302
303 va_start(ap, m);
304 off = va_arg(ap, int);
305 nxt = va_arg(ap, int);
306 va_end(ap);
307
308 (void)ipsec_common_input(m, off, offsetof(struct ip, ip_p),
309 AF_INET, nxt);
310 }
311
312 /*
313 * IPsec input callback for INET protocols.
314 * This routine is called as the transform callback.
315 * Takes care of filtering and other sanity checks on
316 * the processed packet.
317 */
318 int
319 ipsec4_common_input_cb(struct mbuf *m, struct secasvar *sav,
320 int skip, int protoff)
321 {
322 int prot, af __diagused, sproto;
323 struct ip *ip;
324 struct secasindex *saidx;
325 int error;
326
327 if (__predict_false(m == NULL)) {
328 panic("%s: NULL mbuf", __func__);
329 }
330 if (__predict_false(skip < sizeof(struct ip))) {
331 panic("%s: short skip", __func__);
332 }
333
334 KASSERT(sav != NULL);
335 saidx = &sav->sah->saidx;
336 af = saidx->dst.sa.sa_family;
337 KASSERTMSG(af == AF_INET, "unexpected af %u", af);
338 sproto = saidx->proto;
339 KASSERTMSG(sproto == IPPROTO_ESP || sproto == IPPROTO_AH ||
340 sproto == IPPROTO_IPCOMP,
341 "unexpected security protocol %u", sproto);
342
343 /*
344 * Update the IPv4 header. The length of the packet may have changed,
345 * so fix it, and recompute the checksum.
346 */
347 if (m->m_len < skip && (m = m_pullup(m, skip)) == NULL) {
348 char buf[IPSEC_ADDRSTRLEN];
349 cantpull:
350 IPSECLOG(LOG_DEBUG,
351 "processing failed for SA %s/%08lx\n",
352 ipsec_address(&sav->sah->saidx.dst, buf,
353 sizeof(buf)), (u_long) ntohl(sav->spi));
354 IPSEC_ISTAT(sproto, ESP_STAT_HDROPS, AH_STAT_HDROPS,
355 IPCOMP_STAT_HDROPS);
356 error = ENOBUFS;
357 goto bad;
358 }
359 ip = mtod(m, struct ip *);
360 ip->ip_len = htons(m->m_pkthdr.len);
361 ip->ip_sum = 0;
362 ip->ip_sum = in_cksum(m, ip->ip_hl << 2);
363
364 /*
365 * Update TCP/UDP checksum
366 * XXX: should only do it in NAT-T case
367 * XXX: should do it incrementally, see FreeBSD code.
368 */
369 m = ipsec4_fixup_checksum(m);
370 if (m == NULL)
371 goto cantpull;
372 ip = mtod(m, struct ip *);
373
374 prot = ip->ip_p;
375
376 M_VERIFY_PACKET(m);
377
378 key_sa_recordxfer(sav, m);
379
380 if ((inetsw[ip_protox[prot]].pr_flags & PR_LASTHDR) != 0 &&
381 ipsec_in_reject(m, NULL)) {
382 error = EINVAL;
383 goto bad;
384 }
385 (*inetsw[ip_protox[prot]].pr_input)(m, skip, prot);
386 return 0;
387
388 bad:
389 m_freem(m);
390 return error;
391 }
392 #endif /* INET */
393
394 #ifdef INET6
395 int
396 ipsec6_common_input(struct mbuf **mp, int *offp, int proto)
397 {
398 int l = 0;
399 int protoff, nxt;
400 struct ip6_ext ip6e;
401
402 if (*offp < sizeof(struct ip6_hdr)) {
403 IPSECLOG(LOG_DEBUG, "bad offset %u\n", *offp);
404 IPSEC_ISTAT(proto, ESP_STAT_HDROPS, AH_STAT_HDROPS,
405 IPCOMP_STAT_HDROPS);
406 m_freem(*mp);
407 return IPPROTO_DONE;
408 } else if (*offp == sizeof(struct ip6_hdr)) {
409 protoff = offsetof(struct ip6_hdr, ip6_nxt);
410 } else {
411 /* Chase down the header chain... */
412 protoff = sizeof(struct ip6_hdr);
413 nxt = (mtod(*mp, struct ip6_hdr *))->ip6_nxt;
414
415 do {
416 protoff += l;
417 m_copydata(*mp, protoff, sizeof(ip6e), &ip6e);
418
419 if (nxt == IPPROTO_AH)
420 l = (ip6e.ip6e_len + 2) << 2;
421 else if (nxt == IPPROTO_FRAGMENT)
422 l = sizeof(struct ip6_frag);
423 else
424 l = (ip6e.ip6e_len + 1) << 3;
425 KASSERT(l > 0);
426
427 nxt = ip6e.ip6e_nxt;
428 } while (protoff + l < *offp);
429
430 /* Malformed packet check */
431 if (protoff + l != *offp) {
432 IPSECLOG(LOG_DEBUG, "bad packet header chain, "
433 "protoff %u, l %u, off %u\n", protoff, l, *offp);
434 IPSEC_ISTAT(proto, ESP_STAT_HDROPS,
435 AH_STAT_HDROPS,
436 IPCOMP_STAT_HDROPS);
437 m_freem(*mp);
438 *mp = NULL;
439 return IPPROTO_DONE;
440 }
441 protoff += offsetof(struct ip6_ext, ip6e_nxt);
442 }
443 (void) ipsec_common_input(*mp, *offp, protoff, AF_INET6, proto);
444 return IPPROTO_DONE;
445 }
446
447 /*
448 * IPsec input callback, called by the transform callback. Takes care of
449 * filtering and other sanity checks on the processed packet.
450 */
451 int
452 ipsec6_common_input_cb(struct mbuf *m, struct secasvar *sav, int skip,
453 int protoff)
454 {
455 int af __diagused, sproto;
456 struct ip6_hdr *ip6;
457 struct secasindex *saidx;
458 int nxt;
459 u_int8_t prot;
460 int error, nest;
461
462 if (__predict_false(m == NULL)) {
463 panic("%s: NULL mbuf", __func__);
464 }
465
466 KASSERT(sav != NULL);
467 saidx = &sav->sah->saidx;
468 af = saidx->dst.sa.sa_family;
469 KASSERTMSG(af == AF_INET6, "unexpected af %u", af);
470 sproto = saidx->proto;
471 KASSERTMSG(sproto == IPPROTO_ESP || sproto == IPPROTO_AH ||
472 sproto == IPPROTO_IPCOMP,
473 "unexpected security protocol %u", sproto);
474
475 /* Fix IPv6 header */
476 if (m->m_len < sizeof(struct ip6_hdr) &&
477 (m = m_pullup(m, sizeof(struct ip6_hdr))) == NULL) {
478 char buf[IPSEC_ADDRSTRLEN];
479 IPSECLOG(LOG_DEBUG, "processing failed for SA %s/%08lx\n",
480 ipsec_address(&sav->sah->saidx.dst,
481 buf, sizeof(buf)), (u_long) ntohl(sav->spi));
482 IPSEC_ISTAT(sproto, ESP_STAT_HDROPS, AH_STAT_HDROPS,
483 IPCOMP_STAT_HDROPS);
484 error = EACCES;
485 goto bad;
486 }
487
488 ip6 = mtod(m, struct ip6_hdr *);
489 ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(struct ip6_hdr));
490
491 m_copydata(m, protoff, sizeof(prot), &prot);
492
493 key_sa_recordxfer(sav, m);
494
495 /*
496 * See the end of ip6_input for this logic.
497 * IPPROTO_IPV[46] case will be processed just like other ones
498 */
499 nest = 0;
500 nxt = prot;
501 while (nxt != IPPROTO_DONE) {
502 if (ip6_hdrnestlimit && (++nest > ip6_hdrnestlimit)) {
503 IP6_STATINC(IP6_STAT_TOOMANYHDR);
504 error = EINVAL;
505 goto bad;
506 }
507
508 M_VERIFY_PACKET(m);
509
510 /*
511 * Protection against faulty packet - there should be
512 * more sanity checks in header chain processing.
513 */
514 if (m->m_pkthdr.len < skip) {
515 IP6_STATINC(IP6_STAT_TOOSHORT);
516 in6_ifstat_inc(m_get_rcvif_NOMPSAFE(m),
517 ifs6_in_truncated);
518 error = EINVAL;
519 goto bad;
520 }
521
522 /*
523 * Enforce IPsec policy checking if we are seeing last header.
524 * Note that we do not visit this with protocols with pcb layer
525 * code - like udp/tcp/raw ip.
526 */
527 if ((inet6sw[ip6_protox[nxt]].pr_flags & PR_LASTHDR) != 0 &&
528 ipsec_in_reject(m, NULL)) {
529 error = EINVAL;
530 goto bad;
531 }
532 nxt = (*inet6sw[ip6_protox[nxt]].pr_input)(&m, &skip, nxt);
533 }
534 return 0;
535
536 bad:
537 if (m)
538 m_freem(m);
539 return error;
540 }
541 #endif /* INET6 */
542