ipsec_input.c revision 1.71 1 /* $NetBSD: ipsec_input.c,v 1.71 2018/09/14 05:09:51 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.71 2018/09/14 05:09:51 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 M_REGION_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 M_REGION_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, int off, int proto)
299 {
300 (void)ipsec_common_input(m, off, offsetof(struct ip, ip_p),
301 AF_INET, proto);
302 }
303
304 /*
305 * IPsec input callback for INET protocols.
306 * This routine is called as the transform callback.
307 * Takes care of filtering and other sanity checks on
308 * the processed packet.
309 */
310 int
311 ipsec4_common_input_cb(struct mbuf *m, struct secasvar *sav,
312 int skip, int protoff)
313 {
314 int prot, af __diagused, sproto;
315 struct ip *ip;
316 struct secasindex *saidx;
317 int error;
318
319 if (__predict_false(m == NULL)) {
320 panic("%s: NULL mbuf", __func__);
321 }
322 if (__predict_false(skip < sizeof(struct ip))) {
323 panic("%s: short skip", __func__);
324 }
325
326 KASSERT(sav != NULL);
327 saidx = &sav->sah->saidx;
328 af = saidx->dst.sa.sa_family;
329 KASSERTMSG(af == AF_INET, "unexpected af %u", af);
330 sproto = saidx->proto;
331 KASSERTMSG(sproto == IPPROTO_ESP || sproto == IPPROTO_AH ||
332 sproto == IPPROTO_IPCOMP,
333 "unexpected security protocol %u", sproto);
334
335 /*
336 * Update the IPv4 header. The length of the packet may have changed,
337 * so fix it, and recompute the checksum.
338 */
339 if (m->m_len < skip && (m = m_pullup(m, skip)) == NULL) {
340 char buf[IPSEC_ADDRSTRLEN];
341 cantpull:
342 IPSECLOG(LOG_DEBUG,
343 "processing failed for SA %s/%08lx\n",
344 ipsec_address(&sav->sah->saidx.dst, buf,
345 sizeof(buf)), (u_long) ntohl(sav->spi));
346 IPSEC_ISTAT(sproto, ESP_STAT_HDROPS, AH_STAT_HDROPS,
347 IPCOMP_STAT_HDROPS);
348 error = ENOBUFS;
349 goto bad;
350 }
351 ip = mtod(m, struct ip *);
352 ip->ip_len = htons(m->m_pkthdr.len);
353 ip->ip_sum = 0;
354 ip->ip_sum = in_cksum(m, ip->ip_hl << 2);
355
356 /*
357 * Update TCP/UDP checksum
358 * XXX: should only do it in NAT-T case
359 * XXX: should do it incrementally, see FreeBSD code.
360 */
361 m = ipsec4_fixup_checksum(m);
362 if (m == NULL)
363 goto cantpull;
364 ip = mtod(m, struct ip *);
365
366 prot = ip->ip_p;
367
368 M_VERIFY_PACKET(m);
369
370 key_sa_recordxfer(sav, m);
371
372 if ((inetsw[ip_protox[prot]].pr_flags & PR_LASTHDR) != 0 &&
373 ipsec_in_reject(m, NULL)) {
374 error = EINVAL;
375 goto bad;
376 }
377 (*inetsw[ip_protox[prot]].pr_input)(m, skip, prot);
378 return 0;
379
380 bad:
381 m_freem(m);
382 return error;
383 }
384 #endif /* INET */
385
386 #ifdef INET6
387 int
388 ipsec6_common_input(struct mbuf **mp, int *offp, int proto)
389 {
390 int l = 0;
391 int protoff, nxt;
392 struct ip6_ext ip6e;
393
394 if (*offp < sizeof(struct ip6_hdr)) {
395 IPSECLOG(LOG_DEBUG, "bad offset %u\n", *offp);
396 IPSEC_ISTAT(proto, ESP_STAT_HDROPS, AH_STAT_HDROPS,
397 IPCOMP_STAT_HDROPS);
398 m_freem(*mp);
399 return IPPROTO_DONE;
400 } else if (*offp == sizeof(struct ip6_hdr)) {
401 protoff = offsetof(struct ip6_hdr, ip6_nxt);
402 } else {
403 /* Chase down the header chain... */
404 protoff = sizeof(struct ip6_hdr);
405 nxt = (mtod(*mp, struct ip6_hdr *))->ip6_nxt;
406
407 do {
408 protoff += l;
409 m_copydata(*mp, protoff, sizeof(ip6e), &ip6e);
410
411 if (nxt == IPPROTO_AH)
412 l = (ip6e.ip6e_len + 2) << 2;
413 else if (nxt == IPPROTO_FRAGMENT)
414 l = sizeof(struct ip6_frag);
415 else
416 l = (ip6e.ip6e_len + 1) << 3;
417 KASSERT(l > 0);
418
419 nxt = ip6e.ip6e_nxt;
420 } while (protoff + l < *offp);
421
422 /* Malformed packet check */
423 if (protoff + l != *offp) {
424 IPSECLOG(LOG_DEBUG, "bad packet header chain, "
425 "protoff %u, l %u, off %u\n", protoff, l, *offp);
426 IPSEC_ISTAT(proto, ESP_STAT_HDROPS,
427 AH_STAT_HDROPS,
428 IPCOMP_STAT_HDROPS);
429 m_freem(*mp);
430 *mp = NULL;
431 return IPPROTO_DONE;
432 }
433 protoff += offsetof(struct ip6_ext, ip6e_nxt);
434 }
435 (void) ipsec_common_input(*mp, *offp, protoff, AF_INET6, proto);
436 return IPPROTO_DONE;
437 }
438
439 /*
440 * IPsec input callback, called by the transform callback. Takes care of
441 * filtering and other sanity checks on the processed packet.
442 */
443 int
444 ipsec6_common_input_cb(struct mbuf *m, struct secasvar *sav, int skip,
445 int protoff)
446 {
447 int af __diagused, sproto;
448 struct ip6_hdr *ip6;
449 struct secasindex *saidx;
450 int nxt;
451 u_int8_t prot;
452 int error, nest;
453
454 if (__predict_false(m == NULL)) {
455 panic("%s: NULL mbuf", __func__);
456 }
457
458 KASSERT(sav != NULL);
459 saidx = &sav->sah->saidx;
460 af = saidx->dst.sa.sa_family;
461 KASSERTMSG(af == AF_INET6, "unexpected af %u", af);
462 sproto = saidx->proto;
463 KASSERTMSG(sproto == IPPROTO_ESP || sproto == IPPROTO_AH ||
464 sproto == IPPROTO_IPCOMP,
465 "unexpected security protocol %u", sproto);
466
467 /* Fix IPv6 header */
468 if (m->m_len < sizeof(struct ip6_hdr) &&
469 (m = m_pullup(m, sizeof(struct ip6_hdr))) == NULL) {
470 char buf[IPSEC_ADDRSTRLEN];
471 IPSECLOG(LOG_DEBUG, "processing failed for SA %s/%08lx\n",
472 ipsec_address(&sav->sah->saidx.dst,
473 buf, sizeof(buf)), (u_long) ntohl(sav->spi));
474 IPSEC_ISTAT(sproto, ESP_STAT_HDROPS, AH_STAT_HDROPS,
475 IPCOMP_STAT_HDROPS);
476 error = EACCES;
477 goto bad;
478 }
479
480 ip6 = mtod(m, struct ip6_hdr *);
481 ip6->ip6_plen = htons(m->m_pkthdr.len - sizeof(struct ip6_hdr));
482
483 m_copydata(m, protoff, sizeof(prot), &prot);
484
485 key_sa_recordxfer(sav, m);
486
487 /*
488 * See the end of ip6_input for this logic.
489 * IPPROTO_IPV[46] case will be processed just like other ones
490 */
491 nest = 0;
492 nxt = prot;
493 while (nxt != IPPROTO_DONE) {
494 if (ip6_hdrnestlimit && (++nest > ip6_hdrnestlimit)) {
495 IP6_STATINC(IP6_STAT_TOOMANYHDR);
496 error = EINVAL;
497 goto bad;
498 }
499
500 M_VERIFY_PACKET(m);
501
502 /*
503 * Protection against faulty packet - there should be
504 * more sanity checks in header chain processing.
505 */
506 if (m->m_pkthdr.len < skip) {
507 IP6_STATINC(IP6_STAT_TOOSHORT);
508 in6_ifstat_inc(m_get_rcvif_NOMPSAFE(m),
509 ifs6_in_truncated);
510 error = EINVAL;
511 goto bad;
512 }
513
514 /*
515 * Enforce IPsec policy checking if we are seeing last header.
516 * Note that we do not visit this with protocols with pcb layer
517 * code - like udp/tcp/raw ip.
518 */
519 if ((inet6sw[ip6_protox[nxt]].pr_flags & PR_LASTHDR) != 0 &&
520 ipsec_in_reject(m, NULL)) {
521 error = EINVAL;
522 goto bad;
523 }
524 nxt = (*inet6sw[ip6_protox[nxt]].pr_input)(&m, &skip, nxt);
525 }
526 return 0;
527
528 bad:
529 if (m)
530 m_freem(m);
531 return error;
532 }
533 #endif /* INET6 */
534