]> CyberLeo.Net >> Repos - FreeBSD/releng/10.0.git/blob - sys/netipsec/ipsec.c
- Copy stable/10 (r259064) to releng/10.0 as part of the
[FreeBSD/releng/10.0.git] / sys / netipsec / ipsec.c
1 /*      $FreeBSD$       */
2 /*      $KAME: ipsec.c,v 1.103 2001/05/24 07:14:18 sakane Exp $ */
3
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  * IPsec controller part.
35  */
36
37 #include "opt_inet.h"
38 #include "opt_inet6.h"
39 #include "opt_ipsec.h"
40
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/malloc.h>
44 #include <sys/mbuf.h>
45 #include <sys/domain.h>
46 #include <sys/priv.h>
47 #include <sys/protosw.h>
48 #include <sys/socket.h>
49 #include <sys/socketvar.h>
50 #include <sys/errno.h>
51 #include <sys/time.h>
52 #include <sys/kernel.h>
53 #include <sys/syslog.h>
54 #include <sys/sysctl.h>
55 #include <sys/proc.h>
56
57 #include <net/if.h>
58 #include <net/route.h>
59 #include <net/vnet.h>
60
61 #include <netinet/in.h>
62 #include <netinet/in_systm.h>
63 #include <netinet/ip.h>
64 #include <netinet/ip_var.h>
65 #include <netinet/in_var.h>
66 #include <netinet/udp.h>
67 #include <netinet/udp_var.h>
68 #include <netinet/tcp.h>
69 #include <netinet/udp.h>
70
71 #include <netinet/ip6.h>
72 #ifdef INET6
73 #include <netinet6/ip6_var.h>
74 #endif
75 #include <netinet/in_pcb.h>
76 #ifdef INET6
77 #include <netinet/icmp6.h>
78 #endif
79
80 #include <sys/types.h>
81 #include <netipsec/ipsec.h>
82 #ifdef INET6
83 #include <netipsec/ipsec6.h>
84 #endif
85 #include <netipsec/ah_var.h>
86 #include <netipsec/esp_var.h>
87 #include <netipsec/ipcomp.h>            /*XXX*/
88 #include <netipsec/ipcomp_var.h>
89
90 #include <netipsec/key.h>
91 #include <netipsec/keydb.h>
92 #include <netipsec/key_debug.h>
93
94 #include <netipsec/xform.h>
95
96 #include <machine/in_cksum.h>
97
98 #include <opencrypto/cryptodev.h>
99
100 #ifdef IPSEC_DEBUG
101 VNET_DEFINE(int, ipsec_debug) = 1;
102 #else
103 VNET_DEFINE(int, ipsec_debug) = 0;
104 #endif
105
106 /* NB: name changed so netstat doesn't use it. */
107 VNET_PCPUSTAT_DEFINE(struct ipsecstat, ipsec4stat);
108 VNET_PCPUSTAT_SYSINIT(ipsec4stat);
109
110 #ifdef VIMAGE
111 VNET_PCPUSTAT_SYSUNINIT(ipsec4stat);
112 #endif /* VIMAGE */
113
114 VNET_DEFINE(int, ip4_ah_offsetmask) = 0;        /* maybe IP_DF? */
115 /* DF bit on encap. 0: clear 1: set 2: copy */
116 VNET_DEFINE(int, ip4_ipsec_dfbit) = 0;
117 VNET_DEFINE(int, ip4_esp_trans_deflev) = IPSEC_LEVEL_USE;
118 VNET_DEFINE(int, ip4_esp_net_deflev) = IPSEC_LEVEL_USE;
119 VNET_DEFINE(int, ip4_ah_trans_deflev) = IPSEC_LEVEL_USE;
120 VNET_DEFINE(int, ip4_ah_net_deflev) = IPSEC_LEVEL_USE;
121 VNET_DEFINE(struct secpolicy, ip4_def_policy);
122 /* ECN ignore(-1)/forbidden(0)/allowed(1) */
123 VNET_DEFINE(int, ip4_ipsec_ecn) = 0;
124 VNET_DEFINE(int, ip4_esp_randpad) = -1;
125
126 /*
127  * Crypto support requirements:
128  *
129  *  1   require hardware support
130  * -1   require software support
131  *  0   take anything
132  */
133 VNET_DEFINE(int, crypto_support) = CRYPTOCAP_F_HARDWARE | CRYPTOCAP_F_SOFTWARE;
134
135 FEATURE(ipsec, "Internet Protocol Security (IPsec)");
136 #ifdef IPSEC_NAT_T
137 FEATURE(ipsec_natt, "UDP Encapsulation of IPsec ESP Packets ('NAT-T')");
138 #endif
139
140 SYSCTL_DECL(_net_inet_ipsec);
141
142 /* net.inet.ipsec */
143 SYSCTL_VNET_INT(_net_inet_ipsec, IPSECCTL_DEF_POLICY, def_policy,
144         CTLFLAG_RW, &VNET_NAME(ip4_def_policy).policy, 0,
145         "IPsec default policy.");
146 SYSCTL_VNET_INT(_net_inet_ipsec, IPSECCTL_DEF_ESP_TRANSLEV, esp_trans_deflev,
147         CTLFLAG_RW, &VNET_NAME(ip4_esp_trans_deflev), 0,
148         "Default ESP transport mode level");
149 SYSCTL_VNET_INT(_net_inet_ipsec, IPSECCTL_DEF_ESP_NETLEV, esp_net_deflev,
150         CTLFLAG_RW, &VNET_NAME(ip4_esp_net_deflev), 0,
151         "Default ESP tunnel mode level.");
152 SYSCTL_VNET_INT(_net_inet_ipsec, IPSECCTL_DEF_AH_TRANSLEV, ah_trans_deflev,
153         CTLFLAG_RW, &VNET_NAME(ip4_ah_trans_deflev), 0,
154         "AH transfer mode default level.");
155 SYSCTL_VNET_INT(_net_inet_ipsec, IPSECCTL_DEF_AH_NETLEV, ah_net_deflev,
156         CTLFLAG_RW, &VNET_NAME(ip4_ah_net_deflev), 0,
157         "AH tunnel mode default level.");
158 SYSCTL_VNET_INT(_net_inet_ipsec, IPSECCTL_AH_CLEARTOS, ah_cleartos,
159         CTLFLAG_RW, &VNET_NAME(ah_cleartos), 0,
160         "If set clear type-of-service field when doing AH computation.");
161 SYSCTL_VNET_INT(_net_inet_ipsec, IPSECCTL_AH_OFFSETMASK, ah_offsetmask,
162         CTLFLAG_RW, &VNET_NAME(ip4_ah_offsetmask), 0,
163         "If not set clear offset field mask when doing AH computation.");
164 SYSCTL_VNET_INT(_net_inet_ipsec, IPSECCTL_DFBIT, dfbit,
165         CTLFLAG_RW, &VNET_NAME(ip4_ipsec_dfbit), 0,
166         "Do not fragment bit on encap.");
167 SYSCTL_VNET_INT(_net_inet_ipsec, IPSECCTL_ECN, ecn,
168         CTLFLAG_RW, &VNET_NAME(ip4_ipsec_ecn), 0,
169         "Explicit Congestion Notification handling.");
170 SYSCTL_VNET_INT(_net_inet_ipsec, IPSECCTL_DEBUG, debug,
171         CTLFLAG_RW, &VNET_NAME(ipsec_debug), 0,
172         "Enable IPsec debugging output when set.");
173 SYSCTL_VNET_INT(_net_inet_ipsec, OID_AUTO, crypto_support,
174         CTLFLAG_RW, &VNET_NAME(crypto_support), 0,
175         "Crypto driver selection.");
176 SYSCTL_VNET_PCPUSTAT(_net_inet_ipsec, OID_AUTO, ipsecstats, struct ipsecstat,
177     ipsec4stat, "IPsec IPv4 statistics.");
178
179 #ifdef REGRESSION
180 /*
181  * When set to 1, IPsec will send packets with the same sequence number.
182  * This allows to verify if the other side has proper replay attacks detection.
183  */
184 VNET_DEFINE(int, ipsec_replay) = 0;
185 SYSCTL_VNET_INT(_net_inet_ipsec, OID_AUTO, test_replay,
186         CTLFLAG_RW, &VNET_NAME(ipsec_replay), 0,
187         "Emulate replay attack");
188 /*
189  * When set 1, IPsec will send packets with corrupted HMAC.
190  * This allows to verify if the other side properly detects modified packets.
191  */
192 VNET_DEFINE(int, ipsec_integrity) = 0;
193 SYSCTL_VNET_INT(_net_inet_ipsec, OID_AUTO, test_integrity,
194         CTLFLAG_RW, &VNET_NAME(ipsec_integrity), 0,
195         "Emulate man-in-the-middle attack");
196 #endif
197
198 #ifdef INET6 
199 VNET_PCPUSTAT_DEFINE(struct ipsecstat, ipsec6stat);
200 VNET_PCPUSTAT_SYSINIT(ipsec6stat);
201
202 #ifdef VIMAGE
203 VNET_PCPUSTAT_SYSUNINIT(ipsec6stat);
204 #endif /* VIMAGE */
205
206 VNET_DEFINE(int, ip6_esp_trans_deflev) = IPSEC_LEVEL_USE;
207 VNET_DEFINE(int, ip6_esp_net_deflev) = IPSEC_LEVEL_USE;
208 VNET_DEFINE(int, ip6_ah_trans_deflev) = IPSEC_LEVEL_USE;
209 VNET_DEFINE(int, ip6_ah_net_deflev) = IPSEC_LEVEL_USE;
210 VNET_DEFINE(int, ip6_ipsec_ecn) = 0;    /* ECN ignore(-1)/forbidden(0)/allowed(1) */
211
212 SYSCTL_DECL(_net_inet6_ipsec6);
213
214 /* net.inet6.ipsec6 */
215 SYSCTL_VNET_INT(_net_inet6_ipsec6, IPSECCTL_DEF_POLICY, def_policy, CTLFLAG_RW,
216         &VNET_NAME(ip4_def_policy).policy, 0,
217         "IPsec default policy.");
218 SYSCTL_VNET_INT(_net_inet6_ipsec6, IPSECCTL_DEF_ESP_TRANSLEV, 
219         esp_trans_deflev, CTLFLAG_RW, &VNET_NAME(ip6_esp_trans_deflev), 0,
220         "Default ESP transport mode level.");
221 SYSCTL_VNET_INT(_net_inet6_ipsec6, IPSECCTL_DEF_ESP_NETLEV, 
222         esp_net_deflev, CTLFLAG_RW, &VNET_NAME(ip6_esp_net_deflev),     0,
223         "Default ESP tunnel mode level.");
224 SYSCTL_VNET_INT(_net_inet6_ipsec6, IPSECCTL_DEF_AH_TRANSLEV, 
225         ah_trans_deflev, CTLFLAG_RW, &VNET_NAME(ip6_ah_trans_deflev),   0,
226         "AH transfer mode default level.");
227 SYSCTL_VNET_INT(_net_inet6_ipsec6, IPSECCTL_DEF_AH_NETLEV, 
228         ah_net_deflev, CTLFLAG_RW, &VNET_NAME(ip6_ah_net_deflev),       0,
229         "AH tunnel mode default level.");
230 SYSCTL_VNET_INT(_net_inet6_ipsec6, IPSECCTL_ECN,
231         ecn, CTLFLAG_RW, &VNET_NAME(ip6_ipsec_ecn),     0,
232         "Explicit Congestion Notification handling.");
233 SYSCTL_VNET_INT(_net_inet6_ipsec6, IPSECCTL_DEBUG, debug, CTLFLAG_RW,
234         &VNET_NAME(ipsec_debug), 0,
235         "Enable IPsec debugging output when set.");
236 SYSCTL_VNET_PCPUSTAT(_net_inet6_ipsec6, IPSECCTL_STATS, ipsecstats,
237     struct ipsecstat, ipsec6stat, "IPsec IPv6 statistics.");
238 #endif /* INET6 */
239
240 static int ipsec_setspidx_inpcb __P((struct mbuf *, struct inpcb *));
241 static int ipsec_setspidx __P((struct mbuf *, struct secpolicyindex *, int));
242 static void ipsec4_get_ulp __P((struct mbuf *m, struct secpolicyindex *, int));
243 static int ipsec4_setspidx_ipaddr __P((struct mbuf *, struct secpolicyindex *));
244 #ifdef INET6
245 static void ipsec6_get_ulp __P((struct mbuf *m, struct secpolicyindex *, int));
246 static int ipsec6_setspidx_ipaddr __P((struct mbuf *, struct secpolicyindex *));
247 #endif
248 static void ipsec_delpcbpolicy __P((struct inpcbpolicy *));
249 static struct secpolicy *ipsec_deepcopy_policy __P((struct secpolicy *src));
250 static void vshiftl __P((unsigned char *, int, int));
251
252 MALLOC_DEFINE(M_IPSEC_INPCB, "inpcbpolicy", "inpcb-resident ipsec policy");
253
254 /*
255  * Return a held reference to the default SP.
256  */
257 static struct secpolicy *
258 key_allocsp_default(const char* where, int tag)
259 {
260         struct secpolicy *sp;
261
262         KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
263                 printf("DP key_allocsp_default from %s:%u\n", where, tag));
264
265         sp = &V_ip4_def_policy;
266         if (sp->policy != IPSEC_POLICY_DISCARD &&
267             sp->policy != IPSEC_POLICY_NONE) {
268                 ipseclog((LOG_INFO, "fixed system default policy: %d->%d\n",
269                     sp->policy, IPSEC_POLICY_NONE));
270                 sp->policy = IPSEC_POLICY_NONE;
271         }
272         key_addref(sp);
273
274         KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
275                 printf("DP key_allocsp_default returns SP:%p (%u)\n",
276                         sp, sp->refcnt));
277         return (sp);
278 }
279 #define KEY_ALLOCSP_DEFAULT() \
280         key_allocsp_default(__FILE__, __LINE__)
281
282 /*
283  * For OUTBOUND packet having a socket. Searching SPD for packet,
284  * and return a pointer to SP.
285  * OUT: NULL:   no apropreate SP found, the following value is set to error.
286  *              0       : bypass
287  *              EACCES  : discard packet.
288  *              ENOENT  : ipsec_acquire() in progress, maybe.
289  *              others  : error occured.
290  *      others: a pointer to SP
291  *
292  * NOTE: IPv6 mapped adddress concern is implemented here.
293  */
294 struct secpolicy *
295 ipsec_getpolicy(struct tdb_ident *tdbi, u_int dir)
296 {
297         struct secpolicy *sp;
298
299         IPSEC_ASSERT(tdbi != NULL, ("null tdbi"));
300         IPSEC_ASSERT(dir == IPSEC_DIR_INBOUND || dir == IPSEC_DIR_OUTBOUND,
301                 ("invalid direction %u", dir));
302
303         sp = KEY_ALLOCSP2(tdbi->spi, &tdbi->dst, tdbi->proto, dir);
304         if (sp == NULL)                 /*XXX????*/
305                 sp = KEY_ALLOCSP_DEFAULT();
306         IPSEC_ASSERT(sp != NULL, ("null SP"));
307         return (sp);
308 }
309
310 /*
311  * For OUTBOUND packet having a socket. Searching SPD for packet,
312  * and return a pointer to SP.
313  * OUT: NULL:   no apropreate SP found, the following value is set to error.
314  *              0       : bypass
315  *              EACCES  : discard packet.
316  *              ENOENT  : ipsec_acquire() in progress, maybe.
317  *              others  : error occured.
318  *      others: a pointer to SP
319  *
320  * NOTE: IPv6 mapped adddress concern is implemented here.
321  */
322 static struct secpolicy *
323 ipsec_getpolicybysock(struct mbuf *m, u_int dir, struct inpcb *inp, int *error)
324 {
325         struct inpcbpolicy *pcbsp;
326         struct secpolicy *currsp = NULL;        /* Policy on socket. */
327         struct secpolicy *sp;
328
329         IPSEC_ASSERT(m != NULL, ("null mbuf"));
330         IPSEC_ASSERT(inp != NULL, ("null inpcb"));
331         IPSEC_ASSERT(error != NULL, ("null error"));
332         IPSEC_ASSERT(dir == IPSEC_DIR_INBOUND || dir == IPSEC_DIR_OUTBOUND,
333                 ("invalid direction %u", dir));
334
335         /* Set spidx in pcb. */
336         *error = ipsec_setspidx_inpcb(m, inp);
337         if (*error)
338                 return (NULL);
339
340         pcbsp = inp->inp_sp;
341         IPSEC_ASSERT(pcbsp != NULL, ("null pcbsp"));
342         switch (dir) {
343         case IPSEC_DIR_INBOUND:
344                 currsp = pcbsp->sp_in;
345                 break;
346         case IPSEC_DIR_OUTBOUND:
347                 currsp = pcbsp->sp_out;
348                 break;
349         }
350         IPSEC_ASSERT(currsp != NULL, ("null currsp"));
351
352         if (pcbsp->priv) {                      /* When privilieged socket. */
353                 switch (currsp->policy) {
354                 case IPSEC_POLICY_BYPASS:
355                 case IPSEC_POLICY_IPSEC:
356                         key_addref(currsp);
357                         sp = currsp;
358                         break;
359
360                 case IPSEC_POLICY_ENTRUST:
361                         /* Look for a policy in SPD. */
362                         sp = KEY_ALLOCSP(&currsp->spidx, dir);
363                         if (sp == NULL)         /* No SP found. */
364                                 sp = KEY_ALLOCSP_DEFAULT();
365                         break;
366
367                 default:
368                         ipseclog((LOG_ERR, "%s: Invalid policy for PCB %d\n",
369                                 __func__, currsp->policy));
370                         *error = EINVAL;
371                         return (NULL);
372                 }
373         } else {                                /* Unpriv, SPD has policy. */
374                 sp = KEY_ALLOCSP(&currsp->spidx, dir);
375                 if (sp == NULL) {               /* No SP found. */
376                         switch (currsp->policy) {
377                         case IPSEC_POLICY_BYPASS:
378                                 ipseclog((LOG_ERR, "%s: Illegal policy for "
379                                         "non-priviliged defined %d\n",
380                                         __func__, currsp->policy));
381                                 *error = EINVAL;
382                                 return (NULL);
383
384                         case IPSEC_POLICY_ENTRUST:
385                                 sp = KEY_ALLOCSP_DEFAULT();
386                                 break;
387
388                         case IPSEC_POLICY_IPSEC:
389                                 key_addref(currsp);
390                                 sp = currsp;
391                                 break;
392
393                         default:
394                                 ipseclog((LOG_ERR, "%s: Invalid policy for "
395                                         "PCB %d\n", __func__, currsp->policy));
396                                 *error = EINVAL;
397                                 return (NULL);
398                         }
399                 }
400         }
401         IPSEC_ASSERT(sp != NULL,
402                 ("null SP (priv %u policy %u", pcbsp->priv, currsp->policy));
403         KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
404                 printf("DP %s (priv %u policy %u) allocate SP:%p (refcnt %u)\n",
405                         __func__, pcbsp->priv, currsp->policy, sp, sp->refcnt));
406         return (sp);
407 }
408
409 /*
410  * For FORWADING packet or OUTBOUND without a socket. Searching SPD for packet,
411  * and return a pointer to SP.
412  * OUT: positive: a pointer to the entry for security policy leaf matched.
413  *      NULL:   no apropreate SP found, the following value is set to error.
414  *              0       : bypass
415  *              EACCES  : discard packet.
416  *              ENOENT  : ipsec_acquire() in progress, maybe.
417  *              others  : error occured.
418  */
419 struct secpolicy *
420 ipsec_getpolicybyaddr(struct mbuf *m, u_int dir, int flag, int *error)
421 {
422         struct secpolicyindex spidx;
423         struct secpolicy *sp;
424
425         IPSEC_ASSERT(m != NULL, ("null mbuf"));
426         IPSEC_ASSERT(error != NULL, ("null error"));
427         IPSEC_ASSERT(dir == IPSEC_DIR_INBOUND || dir == IPSEC_DIR_OUTBOUND,
428                 ("invalid direction %u", dir));
429
430         sp = NULL;
431         if (key_havesp(dir)) {
432                 /* Make an index to look for a policy. */
433                 *error = ipsec_setspidx(m, &spidx,
434                                         (flag & IP_FORWARDING) ? 0 : 1);
435                 if (*error != 0) {
436                         DPRINTF(("%s: setpidx failed, dir %u flag %u\n",
437                                 __func__, dir, flag));
438                         return (NULL);
439                 }
440                 spidx.dir = dir;
441
442                 sp = KEY_ALLOCSP(&spidx, dir);
443         }
444         if (sp == NULL)                 /* No SP found, use system default. */
445                 sp = KEY_ALLOCSP_DEFAULT();
446         IPSEC_ASSERT(sp != NULL, ("null SP"));
447         return (sp);
448 }
449
450 struct secpolicy *
451 ipsec4_checkpolicy(struct mbuf *m, u_int dir, u_int flag, int *error,
452     struct inpcb *inp)
453 {
454         struct secpolicy *sp;
455
456         *error = 0;
457         if (inp == NULL)
458                 sp = ipsec_getpolicybyaddr(m, dir, flag, error);
459         else
460                 sp = ipsec_getpolicybysock(m, dir, inp, error);
461         if (sp == NULL) {
462                 IPSEC_ASSERT(*error != 0, ("getpolicy failed w/o error"));
463                 IPSECSTAT_INC(ips_out_inval);
464                 return (NULL);
465         }
466         IPSEC_ASSERT(*error == 0, ("sp w/ error set to %u", *error));
467         switch (sp->policy) {
468         case IPSEC_POLICY_ENTRUST:
469         default:
470                 printf("%s: invalid policy %u\n", __func__, sp->policy);
471                 /* FALLTHROUGH */
472         case IPSEC_POLICY_DISCARD:
473                 IPSECSTAT_INC(ips_out_polvio);
474                 *error = -EINVAL;       /* Packet is discarded by caller. */
475                 break;
476         case IPSEC_POLICY_BYPASS:
477         case IPSEC_POLICY_NONE:
478                 KEY_FREESP(&sp);
479                 sp = NULL;              /* NB: force NULL result. */
480                 break;
481         case IPSEC_POLICY_IPSEC:
482                 if (sp->req == NULL)    /* Acquire a SA. */
483                         *error = key_spdacquire(sp);
484                 break;
485         }
486         if (*error != 0) {
487                 KEY_FREESP(&sp);
488                 sp = NULL;
489         }
490         return (sp);
491 }
492
493 static int
494 ipsec_setspidx_inpcb(struct mbuf *m, struct inpcb *inp)
495 {
496         int error;
497
498         IPSEC_ASSERT(inp != NULL, ("null inp"));
499         IPSEC_ASSERT(inp->inp_sp != NULL, ("null inp_sp"));
500         IPSEC_ASSERT(inp->inp_sp->sp_out != NULL && inp->inp_sp->sp_in != NULL,
501                 ("null sp_in || sp_out"));
502
503         error = ipsec_setspidx(m, &inp->inp_sp->sp_in->spidx, 1);
504         if (error == 0) {
505                 inp->inp_sp->sp_in->spidx.dir = IPSEC_DIR_INBOUND;
506                 inp->inp_sp->sp_out->spidx = inp->inp_sp->sp_in->spidx;
507                 inp->inp_sp->sp_out->spidx.dir = IPSEC_DIR_OUTBOUND;
508         } else {
509                 bzero(&inp->inp_sp->sp_in->spidx,
510                         sizeof (inp->inp_sp->sp_in->spidx));
511                 bzero(&inp->inp_sp->sp_out->spidx,
512                         sizeof (inp->inp_sp->sp_in->spidx));
513         }
514         return (error);
515 }
516
517 /*
518  * Configure security policy index (src/dst/proto/sport/dport)
519  * by looking at the content of mbuf.
520  * The caller is responsible for error recovery (like clearing up spidx).
521  */
522 static int
523 ipsec_setspidx(struct mbuf *m, struct secpolicyindex *spidx, int needport)
524 {
525         struct ip *ip = NULL;
526         struct ip ipbuf;
527         u_int v;
528         struct mbuf *n;
529         int len;
530         int error;
531
532         IPSEC_ASSERT(m != NULL, ("null mbuf"));
533
534         /*
535          * Validate m->m_pkthdr.len.  We see incorrect length if we
536          * mistakenly call this function with inconsistent mbuf chain
537          * (like 4.4BSD tcp/udp processing).  XXX Should we panic here?
538          */
539         len = 0;
540         for (n = m; n; n = n->m_next)
541                 len += n->m_len;
542         if (m->m_pkthdr.len != len) {
543                 KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
544                         printf("%s: pkthdr len(%d) mismatch (%d), ignored.\n",
545                                 __func__, len, m->m_pkthdr.len));
546                 return (EINVAL);
547         }
548
549         if (m->m_pkthdr.len < sizeof(struct ip)) {
550                 KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
551                         printf("%s: pkthdr len(%d) too small (v4), ignored.\n",
552                             __func__, m->m_pkthdr.len));
553                 return (EINVAL);
554         }
555
556         if (m->m_len >= sizeof(*ip))
557                 ip = mtod(m, struct ip *);
558         else {
559                 m_copydata(m, 0, sizeof(ipbuf), (caddr_t)&ipbuf);
560                 ip = &ipbuf;
561         }
562 #ifdef _IP_VHL
563         v = _IP_VHL_V(ip->ip_vhl);
564 #else
565         v = ip->ip_v;
566 #endif
567         switch (v) {
568         case 4:
569                 error = ipsec4_setspidx_ipaddr(m, spidx);
570                 if (error)
571                         return (error);
572                 ipsec4_get_ulp(m, spidx, needport);
573                 return (0);
574 #ifdef INET6
575         case 6:
576                 if (m->m_pkthdr.len < sizeof(struct ip6_hdr)) {
577                         KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
578                                 printf("%s: pkthdr len(%d) too small (v6), "
579                                 "ignored\n", __func__, m->m_pkthdr.len));
580                         return (EINVAL);
581                 }
582                 error = ipsec6_setspidx_ipaddr(m, spidx);
583                 if (error)
584                         return (error);
585                 ipsec6_get_ulp(m, spidx, needport);
586                 return (0);
587 #endif
588         default:
589                 KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
590                         printf("%s: " "unknown IP version %u, ignored.\n",
591                                 __func__, v));
592                 return (EINVAL);
593         }
594 }
595
596 static void
597 ipsec4_get_ulp(struct mbuf *m, struct secpolicyindex *spidx, int needport)
598 {
599         u_int8_t nxt;
600         int off;
601
602         /* Sanity check. */
603         IPSEC_ASSERT(m != NULL, ("null mbuf"));
604         IPSEC_ASSERT(m->m_pkthdr.len >= sizeof(struct ip),("packet too short"));
605
606         if (m->m_len >= sizeof (struct ip)) {
607                 struct ip *ip = mtod(m, struct ip *);
608                 if (ip->ip_off & htons(IP_MF | IP_OFFMASK))
609                         goto done;
610 #ifdef _IP_VHL
611                 off = _IP_VHL_HL(ip->ip_vhl) << 2;
612 #else
613                 off = ip->ip_hl << 2;
614 #endif
615                 nxt = ip->ip_p;
616         } else {
617                 struct ip ih;
618
619                 m_copydata(m, 0, sizeof (struct ip), (caddr_t) &ih);
620                 if (ih.ip_off & htons(IP_MF | IP_OFFMASK))
621                         goto done;
622 #ifdef _IP_VHL
623                 off = _IP_VHL_HL(ih.ip_vhl) << 2;
624 #else
625                 off = ih.ip_hl << 2;
626 #endif
627                 nxt = ih.ip_p;
628         }
629
630         while (off < m->m_pkthdr.len) {
631                 struct ip6_ext ip6e;
632                 struct tcphdr th;
633                 struct udphdr uh;
634
635                 switch (nxt) {
636                 case IPPROTO_TCP:
637                         spidx->ul_proto = nxt;
638                         if (!needport)
639                                 goto done_proto;
640                         if (off + sizeof(struct tcphdr) > m->m_pkthdr.len)
641                                 goto done;
642                         m_copydata(m, off, sizeof (th), (caddr_t) &th);
643                         spidx->src.sin.sin_port = th.th_sport;
644                         spidx->dst.sin.sin_port = th.th_dport;
645                         return;
646                 case IPPROTO_UDP:
647                         spidx->ul_proto = nxt;
648                         if (!needport)
649                                 goto done_proto;
650                         if (off + sizeof(struct udphdr) > m->m_pkthdr.len)
651                                 goto done;
652                         m_copydata(m, off, sizeof (uh), (caddr_t) &uh);
653                         spidx->src.sin.sin_port = uh.uh_sport;
654                         spidx->dst.sin.sin_port = uh.uh_dport;
655                         return;
656                 case IPPROTO_AH:
657                         if (off + sizeof(ip6e) > m->m_pkthdr.len)
658                                 goto done;
659                         /* XXX Sigh, this works but is totally bogus. */
660                         m_copydata(m, off, sizeof(ip6e), (caddr_t) &ip6e);
661                         off += (ip6e.ip6e_len + 2) << 2;
662                         nxt = ip6e.ip6e_nxt;
663                         break;
664                 case IPPROTO_ICMP:
665                 default:
666                         /* XXX Intermediate headers??? */
667                         spidx->ul_proto = nxt;
668                         goto done_proto;
669                 }
670         }
671 done:
672         spidx->ul_proto = IPSEC_ULPROTO_ANY;
673 done_proto:
674         spidx->src.sin.sin_port = IPSEC_PORT_ANY;
675         spidx->dst.sin.sin_port = IPSEC_PORT_ANY;
676 }
677
678 /* Assumes that m is sane. */
679 static int
680 ipsec4_setspidx_ipaddr(struct mbuf *m, struct secpolicyindex *spidx)
681 {
682         static const struct sockaddr_in template = {
683                 sizeof (struct sockaddr_in),
684                 AF_INET,
685                 0, { 0 }, { 0, 0, 0, 0, 0, 0, 0, 0 }
686         };
687
688         spidx->src.sin = template;
689         spidx->dst.sin = template;
690
691         if (m->m_len < sizeof (struct ip)) {
692                 m_copydata(m, offsetof(struct ip, ip_src),
693                            sizeof (struct  in_addr),
694                            (caddr_t) &spidx->src.sin.sin_addr);
695                 m_copydata(m, offsetof(struct ip, ip_dst),
696                            sizeof (struct  in_addr),
697                            (caddr_t) &spidx->dst.sin.sin_addr);
698         } else {
699                 struct ip *ip = mtod(m, struct ip *);
700                 spidx->src.sin.sin_addr = ip->ip_src;
701                 spidx->dst.sin.sin_addr = ip->ip_dst;
702         }
703
704         spidx->prefs = sizeof(struct in_addr) << 3;
705         spidx->prefd = sizeof(struct in_addr) << 3;
706
707         return (0);
708 }
709
710 #ifdef INET6
711 static void
712 ipsec6_get_ulp(struct mbuf *m, struct secpolicyindex *spidx, int needport)
713 {
714         int off, nxt;
715         struct tcphdr th;
716         struct udphdr uh;
717         struct icmp6_hdr ih;
718
719         /* Sanity check. */
720         if (m == NULL)
721                 panic("%s: NULL pointer was passed.\n", __func__);
722
723         KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
724                 printf("%s:\n", __func__); kdebug_mbuf(m));
725
726         /* Set default. */
727         spidx->ul_proto = IPSEC_ULPROTO_ANY;
728         ((struct sockaddr_in6 *)&spidx->src)->sin6_port = IPSEC_PORT_ANY;
729         ((struct sockaddr_in6 *)&spidx->dst)->sin6_port = IPSEC_PORT_ANY;
730
731         nxt = -1;
732         off = ip6_lasthdr(m, 0, IPPROTO_IPV6, &nxt);
733         if (off < 0 || m->m_pkthdr.len < off)
734                 return;
735
736         switch (nxt) {
737         case IPPROTO_TCP:
738                 spidx->ul_proto = nxt;
739                 if (!needport)
740                         break;
741                 if (off + sizeof(struct tcphdr) > m->m_pkthdr.len)
742                         break;
743                 m_copydata(m, off, sizeof(th), (caddr_t)&th);
744                 ((struct sockaddr_in6 *)&spidx->src)->sin6_port = th.th_sport;
745                 ((struct sockaddr_in6 *)&spidx->dst)->sin6_port = th.th_dport;
746                 break;
747         case IPPROTO_UDP:
748                 spidx->ul_proto = nxt;
749                 if (!needport)
750                         break;
751                 if (off + sizeof(struct udphdr) > m->m_pkthdr.len)
752                         break;
753                 m_copydata(m, off, sizeof(uh), (caddr_t)&uh);
754                 ((struct sockaddr_in6 *)&spidx->src)->sin6_port = uh.uh_sport;
755                 ((struct sockaddr_in6 *)&spidx->dst)->sin6_port = uh.uh_dport;
756                 break;
757         case IPPROTO_ICMPV6:
758                 spidx->ul_proto = nxt;
759                 if (off + sizeof(struct icmp6_hdr) > m->m_pkthdr.len)
760                         break;
761                 m_copydata(m, off, sizeof(ih), (caddr_t)&ih);
762                 ((struct sockaddr_in6 *)&spidx->src)->sin6_port =
763                     htons((uint16_t)ih.icmp6_type);
764                 ((struct sockaddr_in6 *)&spidx->dst)->sin6_port =
765                     htons((uint16_t)ih.icmp6_code);
766                 break;
767         default:
768                 /* XXX Intermediate headers??? */
769                 spidx->ul_proto = nxt;
770                 break;
771         }
772 }
773
774 /* Assumes that m is sane. */
775 static int
776 ipsec6_setspidx_ipaddr(struct mbuf *m, struct secpolicyindex *spidx)
777 {
778         struct ip6_hdr *ip6 = NULL;
779         struct ip6_hdr ip6buf;
780         struct sockaddr_in6 *sin6;
781
782         if (m->m_len >= sizeof(*ip6))
783                 ip6 = mtod(m, struct ip6_hdr *);
784         else {
785                 m_copydata(m, 0, sizeof(ip6buf), (caddr_t)&ip6buf);
786                 ip6 = &ip6buf;
787         }
788
789         sin6 = (struct sockaddr_in6 *)&spidx->src;
790         bzero(sin6, sizeof(*sin6));
791         sin6->sin6_family = AF_INET6;
792         sin6->sin6_len = sizeof(struct sockaddr_in6);
793         bcopy(&ip6->ip6_src, &sin6->sin6_addr, sizeof(ip6->ip6_src));
794         if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_src)) {
795                 sin6->sin6_addr.s6_addr16[1] = 0;
796                 sin6->sin6_scope_id = ntohs(ip6->ip6_src.s6_addr16[1]);
797         }
798         spidx->prefs = sizeof(struct in6_addr) << 3;
799
800         sin6 = (struct sockaddr_in6 *)&spidx->dst;
801         bzero(sin6, sizeof(*sin6));
802         sin6->sin6_family = AF_INET6;
803         sin6->sin6_len = sizeof(struct sockaddr_in6);
804         bcopy(&ip6->ip6_dst, &sin6->sin6_addr, sizeof(ip6->ip6_dst));
805         if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst)) {
806                 sin6->sin6_addr.s6_addr16[1] = 0;
807                 sin6->sin6_scope_id = ntohs(ip6->ip6_dst.s6_addr16[1]);
808         }
809         spidx->prefd = sizeof(struct in6_addr) << 3;
810
811         return (0);
812 }
813 #endif
814
815 static void
816 ipsec_delpcbpolicy(struct inpcbpolicy *p)
817 {
818
819         free(p, M_IPSEC_INPCB);
820 }
821
822 /* Initialize policy in PCB. */
823 int
824 ipsec_init_policy(struct socket *so, struct inpcbpolicy **pcb_sp)
825 {
826         struct inpcbpolicy *new;
827
828         /* Sanity check. */
829         if (so == NULL || pcb_sp == NULL)
830                 panic("%s: NULL pointer was passed.\n", __func__);
831
832         new = (struct inpcbpolicy *) malloc(sizeof(struct inpcbpolicy),
833                                             M_IPSEC_INPCB, M_NOWAIT|M_ZERO);
834         if (new == NULL) {
835                 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
836                 return (ENOBUFS);
837         }
838
839         new->priv = IPSEC_IS_PRIVILEGED_SO(so);
840
841         if ((new->sp_in = KEY_NEWSP()) == NULL) {
842                 ipsec_delpcbpolicy(new);
843                 return (ENOBUFS);
844         }
845         new->sp_in->state = IPSEC_SPSTATE_ALIVE;
846         new->sp_in->policy = IPSEC_POLICY_ENTRUST;
847
848         if ((new->sp_out = KEY_NEWSP()) == NULL) {
849                 KEY_FREESP(&new->sp_in);
850                 ipsec_delpcbpolicy(new);
851                 return (ENOBUFS);
852         }
853         new->sp_out->state = IPSEC_SPSTATE_ALIVE;
854         new->sp_out->policy = IPSEC_POLICY_ENTRUST;
855
856         *pcb_sp = new;
857
858         return (0);
859 }
860
861 /* Copy old IPsec policy into new. */
862 int
863 ipsec_copy_policy(struct inpcbpolicy *old, struct inpcbpolicy *new)
864 {
865         struct secpolicy *sp;
866
867         sp = ipsec_deepcopy_policy(old->sp_in);
868         if (sp) {
869                 KEY_FREESP(&new->sp_in);
870                 new->sp_in = sp;
871         } else
872                 return (ENOBUFS);
873
874         sp = ipsec_deepcopy_policy(old->sp_out);
875         if (sp) {
876                 KEY_FREESP(&new->sp_out);
877                 new->sp_out = sp;
878         } else
879                 return (ENOBUFS);
880
881         new->priv = old->priv;
882
883         return (0);
884 }
885
886 struct ipsecrequest *
887 ipsec_newisr(void)
888 {
889         struct ipsecrequest *p;
890
891         p = malloc(sizeof(struct ipsecrequest), M_IPSEC_SR, M_NOWAIT|M_ZERO);
892         if (p != NULL)
893                 IPSECREQUEST_LOCK_INIT(p);
894         return (p);
895 }
896
897 void
898 ipsec_delisr(struct ipsecrequest *p)
899 {
900
901         IPSECREQUEST_LOCK_DESTROY(p);
902         free(p, M_IPSEC_SR);
903 }
904
905 /* Deep-copy a policy in PCB. */
906 static struct secpolicy *
907 ipsec_deepcopy_policy(struct secpolicy *src)
908 {
909         struct ipsecrequest *newchain = NULL;
910         struct ipsecrequest *p;
911         struct ipsecrequest **q;
912         struct ipsecrequest *r;
913         struct secpolicy *dst;
914
915         if (src == NULL)
916                 return (NULL);
917         dst = KEY_NEWSP();
918         if (dst == NULL)
919                 return (NULL);
920
921         /*
922          * Deep-copy IPsec request chain.  This is required since struct
923          * ipsecrequest is not reference counted.
924          */
925         q = &newchain;
926         for (p = src->req; p; p = p->next) {
927                 *q = ipsec_newisr();
928                 if (*q == NULL)
929                         goto fail;
930                 (*q)->saidx.proto = p->saidx.proto;
931                 (*q)->saidx.mode = p->saidx.mode;
932                 (*q)->level = p->level;
933                 (*q)->saidx.reqid = p->saidx.reqid;
934
935                 bcopy(&p->saidx.src, &(*q)->saidx.src, sizeof((*q)->saidx.src));
936                 bcopy(&p->saidx.dst, &(*q)->saidx.dst, sizeof((*q)->saidx.dst));
937
938                 (*q)->sp = dst;
939
940                 q = &((*q)->next);
941         }
942
943         dst->req = newchain;
944         dst->state = src->state;
945         dst->policy = src->policy;
946         /* Do not touch the refcnt fields. */
947
948         return (dst);
949
950 fail:
951         for (p = newchain; p; p = r) {
952                 r = p->next;
953                 ipsec_delisr(p);
954                 p = NULL;
955         }
956         return (NULL);
957 }
958
959 /* Set policy and IPsec request if present. */
960 static int
961 ipsec_set_policy_internal(struct secpolicy **pcb_sp, int optname,
962     caddr_t request, size_t len, struct ucred *cred)
963 {
964         struct sadb_x_policy *xpl;
965         struct secpolicy *newsp = NULL;
966         int error;
967
968         /* Sanity check. */
969         if (pcb_sp == NULL || *pcb_sp == NULL || request == NULL)
970                 return (EINVAL);
971         if (len < sizeof(*xpl))
972                 return (EINVAL);
973         xpl = (struct sadb_x_policy *)request;
974
975         KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
976                 printf("%s: passed policy\n", __func__);
977                 kdebug_sadb_x_policy((struct sadb_ext *)xpl));
978
979         /* Check policy type. */
980         /* ipsec_set_policy_internal() accepts IPSEC, ENTRUST and BYPASS. */
981         if (xpl->sadb_x_policy_type == IPSEC_POLICY_DISCARD
982          || xpl->sadb_x_policy_type == IPSEC_POLICY_NONE)
983                 return (EINVAL);
984
985         /* Check privileged socket. */
986         if (cred != NULL && xpl->sadb_x_policy_type == IPSEC_POLICY_BYPASS) {
987                 error = priv_check_cred(cred, PRIV_NETINET_IPSEC, 0);
988                 if (error)
989                         return (EACCES);
990         }
991
992         /* Allocating new SP entry. */
993         if ((newsp = key_msg2sp(xpl, len, &error)) == NULL)
994                 return (error);
995
996         newsp->state = IPSEC_SPSTATE_ALIVE;
997
998         /* Clear old SP and set new SP. */
999         KEY_FREESP(pcb_sp);
1000         *pcb_sp = newsp;
1001         KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
1002                 printf("%s: new policy\n", __func__);
1003                 kdebug_secpolicy(newsp));
1004
1005         return (0);
1006 }
1007
1008 int
1009 ipsec_set_policy(struct inpcb *inp, int optname, caddr_t request,
1010     size_t len, struct ucred *cred)
1011 {
1012         struct sadb_x_policy *xpl;
1013         struct secpolicy **pcb_sp;
1014
1015         /* Sanity check. */
1016         if (inp == NULL || request == NULL)
1017                 return (EINVAL);
1018         if (len < sizeof(*xpl))
1019                 return (EINVAL);
1020         xpl = (struct sadb_x_policy *)request;
1021
1022         /* Select direction. */
1023         switch (xpl->sadb_x_policy_dir) {
1024         case IPSEC_DIR_INBOUND:
1025                 pcb_sp = &inp->inp_sp->sp_in;
1026                 break;
1027         case IPSEC_DIR_OUTBOUND:
1028                 pcb_sp = &inp->inp_sp->sp_out;
1029                 break;
1030         default:
1031                 ipseclog((LOG_ERR, "%s: invalid direction=%u\n", __func__,
1032                         xpl->sadb_x_policy_dir));
1033                 return (EINVAL);
1034         }
1035
1036         return (ipsec_set_policy_internal(pcb_sp, optname, request, len, cred));
1037 }
1038
1039 int
1040 ipsec_get_policy(struct inpcb *inp, caddr_t request, size_t len,
1041     struct mbuf **mp)
1042 {
1043         struct sadb_x_policy *xpl;
1044         struct secpolicy *pcb_sp;
1045
1046         /* Sanity check. */
1047         if (inp == NULL || request == NULL || mp == NULL)
1048                 return (EINVAL);
1049         IPSEC_ASSERT(inp->inp_sp != NULL, ("null inp_sp"));
1050         if (len < sizeof(*xpl))
1051                 return (EINVAL);
1052         xpl = (struct sadb_x_policy *)request;
1053
1054         /* Select direction. */
1055         switch (xpl->sadb_x_policy_dir) {
1056         case IPSEC_DIR_INBOUND:
1057                 pcb_sp = inp->inp_sp->sp_in;
1058                 break;
1059         case IPSEC_DIR_OUTBOUND:
1060                 pcb_sp = inp->inp_sp->sp_out;
1061                 break;
1062         default:
1063                 ipseclog((LOG_ERR, "%s: invalid direction=%u\n", __func__,
1064                         xpl->sadb_x_policy_dir));
1065                 return (EINVAL);
1066         }
1067
1068         /* Sanity check. Should be an IPSEC_ASSERT. */
1069         if (pcb_sp == NULL)
1070                 return (EINVAL);
1071
1072         *mp = key_sp2msg(pcb_sp);
1073         if (!*mp) {
1074                 ipseclog((LOG_DEBUG, "%s: No more memory.\n", __func__));
1075                 return (ENOBUFS);
1076         }
1077
1078         (*mp)->m_type = MT_DATA;
1079         KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
1080                 printf("%s:\n", __func__); kdebug_mbuf(*mp));
1081
1082         return (0);
1083 }
1084
1085 /* Delete policy in PCB. */
1086 int
1087 ipsec_delete_pcbpolicy(struct inpcb *inp)
1088 {
1089         IPSEC_ASSERT(inp != NULL, ("null inp"));
1090
1091         if (inp->inp_sp == NULL)
1092                 return (0);
1093
1094         if (inp->inp_sp->sp_in != NULL)
1095                 KEY_FREESP(&inp->inp_sp->sp_in);
1096
1097         if (inp->inp_sp->sp_out != NULL)
1098                 KEY_FREESP(&inp->inp_sp->sp_out);
1099
1100         ipsec_delpcbpolicy(inp->inp_sp);
1101         inp->inp_sp = NULL;
1102
1103         return (0);
1104 }
1105
1106 /*
1107  * Return current level.
1108  * Either IPSEC_LEVEL_USE or IPSEC_LEVEL_REQUIRE are always returned.
1109  */
1110 u_int
1111 ipsec_get_reqlevel(struct ipsecrequest *isr)
1112 {
1113         u_int level = 0;
1114         u_int esp_trans_deflev, esp_net_deflev;
1115         u_int ah_trans_deflev, ah_net_deflev;
1116
1117         IPSEC_ASSERT(isr != NULL && isr->sp != NULL, ("null argument"));
1118         IPSEC_ASSERT(isr->sp->spidx.src.sa.sa_family == isr->sp->spidx.dst.sa.sa_family,
1119                 ("af family mismatch, src %u, dst %u",
1120                  isr->sp->spidx.src.sa.sa_family,
1121                  isr->sp->spidx.dst.sa.sa_family));
1122
1123 /* XXX Note that we have ipseclog() expanded here - code sync issue. */
1124 #define IPSEC_CHECK_DEFAULT(lev) \
1125         (((lev) != IPSEC_LEVEL_USE && (lev) != IPSEC_LEVEL_REQUIRE            \
1126                         && (lev) != IPSEC_LEVEL_UNIQUE)                       \
1127                 ? (V_ipsec_debug                                                      \
1128                         ? log(LOG_INFO, "fixed system default level " #lev ":%d->%d\n",\
1129                                 (lev), IPSEC_LEVEL_REQUIRE)                   \
1130                         : 0),                                                 \
1131                         (lev) = IPSEC_LEVEL_REQUIRE,                          \
1132                         (lev)                                                 \
1133                 : (lev))
1134
1135         /* Set default level. */
1136         switch (((struct sockaddr *)&isr->sp->spidx.src)->sa_family) {
1137 #ifdef INET
1138         case AF_INET:
1139                 esp_trans_deflev = IPSEC_CHECK_DEFAULT(V_ip4_esp_trans_deflev);
1140                 esp_net_deflev = IPSEC_CHECK_DEFAULT(V_ip4_esp_net_deflev);
1141                 ah_trans_deflev = IPSEC_CHECK_DEFAULT(V_ip4_ah_trans_deflev);
1142                 ah_net_deflev = IPSEC_CHECK_DEFAULT(V_ip4_ah_net_deflev);
1143                 break;
1144 #endif
1145 #ifdef INET6
1146         case AF_INET6:
1147                 esp_trans_deflev = IPSEC_CHECK_DEFAULT(V_ip6_esp_trans_deflev);
1148                 esp_net_deflev = IPSEC_CHECK_DEFAULT(V_ip6_esp_net_deflev);
1149                 ah_trans_deflev = IPSEC_CHECK_DEFAULT(V_ip6_ah_trans_deflev);
1150                 ah_net_deflev = IPSEC_CHECK_DEFAULT(V_ip6_ah_net_deflev);
1151                 break;
1152 #endif /* INET6 */
1153         default:
1154                 panic("%s: unknown af %u",
1155                         __func__, isr->sp->spidx.src.sa.sa_family);
1156         }
1157
1158 #undef IPSEC_CHECK_DEFAULT
1159
1160         /* Set level. */
1161         switch (isr->level) {
1162         case IPSEC_LEVEL_DEFAULT:
1163                 switch (isr->saidx.proto) {
1164                 case IPPROTO_ESP:
1165                         if (isr->saidx.mode == IPSEC_MODE_TUNNEL)
1166                                 level = esp_net_deflev;
1167                         else
1168                                 level = esp_trans_deflev;
1169                         break;
1170                 case IPPROTO_AH:
1171                         if (isr->saidx.mode == IPSEC_MODE_TUNNEL)
1172                                 level = ah_net_deflev;
1173                         else
1174                                 level = ah_trans_deflev;
1175                         break;
1176                 case IPPROTO_IPCOMP:
1177                         /*
1178                          * We don't really care, as IPcomp document says that
1179                          * we shouldn't compress small packets.
1180                          */
1181                         level = IPSEC_LEVEL_USE;
1182                         break;
1183                 default:
1184                         panic("%s: Illegal protocol defined %u\n", __func__,
1185                                 isr->saidx.proto);
1186                 }
1187                 break;
1188
1189         case IPSEC_LEVEL_USE:
1190         case IPSEC_LEVEL_REQUIRE:
1191                 level = isr->level;
1192                 break;
1193         case IPSEC_LEVEL_UNIQUE:
1194                 level = IPSEC_LEVEL_REQUIRE;
1195                 break;
1196
1197         default:
1198                 panic("%s: Illegal IPsec level %u\n", __func__, isr->level);
1199         }
1200
1201         return (level);
1202 }
1203
1204 /*
1205  * Check security policy requirements against the actual
1206  * packet contents.  Return one if the packet should be
1207  * reject as "invalid"; otherwiser return zero to have the
1208  * packet treated as "valid".
1209  *
1210  * OUT:
1211  *      0: valid
1212  *      1: invalid
1213  */
1214 int
1215 ipsec_in_reject(struct secpolicy *sp, struct mbuf *m)
1216 {
1217         struct ipsecrequest *isr;
1218         int need_auth;
1219
1220         KEYDEBUG(KEYDEBUG_IPSEC_DATA,
1221                 printf("%s: using SP\n", __func__); kdebug_secpolicy(sp));
1222
1223         /* Check policy. */
1224         switch (sp->policy) {
1225         case IPSEC_POLICY_DISCARD:
1226                 return (1);
1227         case IPSEC_POLICY_BYPASS:
1228         case IPSEC_POLICY_NONE:
1229                 return (0);
1230         }
1231
1232         IPSEC_ASSERT(sp->policy == IPSEC_POLICY_IPSEC,
1233                 ("invalid policy %u", sp->policy));
1234
1235         /* XXX Should compare policy against IPsec header history. */
1236
1237         need_auth = 0;
1238         for (isr = sp->req; isr != NULL; isr = isr->next) {
1239                 if (ipsec_get_reqlevel(isr) != IPSEC_LEVEL_REQUIRE)
1240                         continue;
1241                 switch (isr->saidx.proto) {
1242                 case IPPROTO_ESP:
1243                         if ((m->m_flags & M_DECRYPTED) == 0) {
1244                                 KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
1245                                     printf("%s: ESP m_flags:%x\n", __func__,
1246                                             m->m_flags));
1247                                 return (1);
1248                         }
1249
1250                         if (!need_auth &&
1251                             isr->sav != NULL &&
1252                             isr->sav->tdb_authalgxform != NULL &&
1253                             (m->m_flags & M_AUTHIPDGM) == 0) {
1254                                 KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
1255                                     printf("%s: ESP/AH m_flags:%x\n", __func__,
1256                                             m->m_flags));
1257                                 return (1);
1258                         }
1259                         break;
1260                 case IPPROTO_AH:
1261                         need_auth = 1;
1262                         if ((m->m_flags & M_AUTHIPHDR) == 0) {
1263                                 KEYDEBUG(KEYDEBUG_IPSEC_DUMP,
1264                                     printf("%s: AH m_flags:%x\n", __func__,
1265                                             m->m_flags));
1266                                 return (1);
1267                         }
1268                         break;
1269                 case IPPROTO_IPCOMP:
1270                         /*
1271                          * We don't really care, as IPcomp document
1272                          * says that we shouldn't compress small
1273                          * packets.  IPComp policy should always be
1274                          * treated as being in "use" level.
1275                          */
1276                         break;
1277                 }
1278         }
1279         return (0);             /* Valid. */
1280 }
1281
1282 static int
1283 ipsec46_in_reject(struct mbuf *m, struct inpcb *inp)
1284 {
1285         struct secpolicy *sp;
1286         int error;
1287         int result;
1288
1289         IPSEC_ASSERT(m != NULL, ("null mbuf"));
1290
1291         /*
1292          * Get SP for this packet.
1293          * When we are called from ip_forward(), we call
1294          * ipsec_getpolicybyaddr() with IP_FORWARDING flag.
1295          */
1296         if (inp == NULL)
1297                 sp = ipsec_getpolicybyaddr(m, IPSEC_DIR_INBOUND, IP_FORWARDING, &error);
1298         else
1299                 sp = ipsec_getpolicybysock(m, IPSEC_DIR_INBOUND, inp, &error);
1300
1301         if (sp != NULL) {
1302                 result = ipsec_in_reject(sp, m);
1303                 KEY_FREESP(&sp);
1304         } else {
1305                 result = 0;     /* XXX Should be panic?
1306                                  * -> No, there may be error. */
1307         }
1308         return (result);
1309 }
1310
1311 /*
1312  * Check AH/ESP integrity.
1313  * This function is called from tcp_input(), udp_input(),
1314  * and {ah,esp}4_input for tunnel mode.
1315  */
1316 int
1317 ipsec4_in_reject(struct mbuf *m, struct inpcb *inp)
1318 {
1319         int result;
1320
1321         result = ipsec46_in_reject(m, inp);
1322         if (result)
1323                 IPSECSTAT_INC(ips_in_polvio);
1324
1325         return (result);
1326 }
1327
1328 #ifdef INET6
1329 /*
1330  * Check AH/ESP integrity.
1331  * This function is called from tcp6_input(), udp6_input(),
1332  * and {ah,esp}6_input for tunnel mode.
1333  */
1334 int
1335 ipsec6_in_reject(struct mbuf *m, struct inpcb *inp)
1336 {
1337         int result;
1338
1339         result = ipsec46_in_reject(m, inp);
1340         if (result)
1341                 IPSEC6STAT_INC(ips_in_polvio);
1342
1343         return (result);
1344 }
1345 #endif
1346
1347 /*
1348  * Compute the byte size to be occupied by IPsec header.
1349  * In case it is tunnelled, it includes the size of outer IP header.
1350  * NOTE: SP passed is freed in this function.
1351  */
1352 static size_t
1353 ipsec_hdrsiz_internal(struct secpolicy *sp)
1354 {
1355         struct ipsecrequest *isr;
1356         size_t size;
1357
1358         KEYDEBUG(KEYDEBUG_IPSEC_DATA,
1359                 printf("%s: using SP\n", __func__); kdebug_secpolicy(sp));
1360
1361         switch (sp->policy) {
1362         case IPSEC_POLICY_DISCARD:
1363         case IPSEC_POLICY_BYPASS:
1364         case IPSEC_POLICY_NONE:
1365                 return (0);
1366         }
1367
1368         IPSEC_ASSERT(sp->policy == IPSEC_POLICY_IPSEC,
1369                 ("invalid policy %u", sp->policy));
1370
1371         size = 0;
1372         for (isr = sp->req; isr != NULL; isr = isr->next) {
1373                 size_t clen = 0;
1374
1375                 switch (isr->saidx.proto) {
1376                 case IPPROTO_ESP:
1377                         clen = esp_hdrsiz(isr->sav);
1378                         break;
1379                 case IPPROTO_AH:
1380                         clen = ah_hdrsiz(isr->sav);
1381                         break;
1382                 case IPPROTO_IPCOMP:
1383                         clen = sizeof(struct ipcomp);
1384                         break;
1385                 }
1386
1387                 if (isr->saidx.mode == IPSEC_MODE_TUNNEL) {
1388                         switch (isr->saidx.dst.sa.sa_family) {
1389                         case AF_INET:
1390                                 clen += sizeof(struct ip);
1391                                 break;
1392 #ifdef INET6
1393                         case AF_INET6:
1394                                 clen += sizeof(struct ip6_hdr);
1395                                 break;
1396 #endif
1397                         default:
1398                                 ipseclog((LOG_ERR, "%s: unknown AF %d in "
1399                                     "IPsec tunnel SA\n", __func__,
1400                                     ((struct sockaddr *)&isr->saidx.dst)->sa_family));
1401                                 break;
1402                         }
1403                 }
1404                 size += clen;
1405         }
1406
1407         return (size);
1408 }
1409
1410 /* 
1411  * This function is called from ipsec_hdrsiz_tcp(), ip_ipsec_mtu(),
1412  * disabled ip6_ipsec_mtu() and ip6_forward().
1413  */
1414 size_t
1415 ipsec_hdrsiz(struct mbuf *m, u_int dir, struct inpcb *inp)
1416 {
1417         struct secpolicy *sp;
1418         int error;
1419         size_t size;
1420
1421         IPSEC_ASSERT(m != NULL, ("null mbuf"));
1422
1423         /* Get SP for this packet.
1424          * When we are called from ip_forward(), we call
1425          * ipsec_getpolicybyaddr() with IP_FORWARDING flag.
1426          */
1427         if (inp == NULL)
1428                 sp = ipsec_getpolicybyaddr(m, dir, IP_FORWARDING, &error);
1429         else
1430                 sp = ipsec_getpolicybysock(m, dir, inp, &error);
1431
1432         if (sp != NULL) {
1433                 size = ipsec_hdrsiz_internal(sp);
1434                 KEYDEBUG(KEYDEBUG_IPSEC_DATA,
1435                         printf("%s: size:%lu.\n", __func__,
1436                                 (unsigned long)size));
1437
1438                 KEY_FREESP(&sp);
1439         } else {
1440                 size = 0;       /* XXX Should be panic?
1441                                  * -> No, we are called w/o knowing if
1442                                  *    IPsec processing is needed. */
1443         }
1444         return (size);
1445 }
1446
1447 /*
1448  * Check the variable replay window.
1449  * ipsec_chkreplay() performs replay check before ICV verification.
1450  * ipsec_updatereplay() updates replay bitmap.  This must be called after
1451  * ICV verification (it also performs replay check, which is usually done
1452  * beforehand).
1453  * 0 (zero) is returned if packet disallowed, 1 if packet permitted.
1454  *
1455  * Based on RFC 2401.
1456  */
1457 int
1458 ipsec_chkreplay(u_int32_t seq, struct secasvar *sav)
1459 {
1460         const struct secreplay *replay;
1461         u_int32_t diff;
1462         int fr;
1463         u_int32_t wsizeb;       /* Constant: bits of window size. */
1464         int frlast;             /* Constant: last frame. */
1465
1466         IPSEC_ASSERT(sav != NULL, ("Null SA"));
1467         IPSEC_ASSERT(sav->replay != NULL, ("Null replay state"));
1468
1469         replay = sav->replay;
1470
1471         if (replay->wsize == 0)
1472                 return (1);     /* No need to check replay. */
1473
1474         /* Constant. */
1475         frlast = replay->wsize - 1;
1476         wsizeb = replay->wsize << 3;
1477
1478         /* Sequence number of 0 is invalid. */
1479         if (seq == 0)
1480                 return (0);
1481
1482         /* First time is always okay. */
1483         if (replay->count == 0)
1484                 return (1);
1485
1486         if (seq > replay->lastseq) {
1487                 /* Larger sequences are okay. */
1488                 return (1);
1489         } else {
1490                 /* seq is equal or less than lastseq. */
1491                 diff = replay->lastseq - seq;
1492
1493                 /* Over range to check, i.e. too old or wrapped. */
1494                 if (diff >= wsizeb)
1495                         return (0);
1496
1497                 fr = frlast - diff / 8;
1498
1499                 /* This packet already seen? */
1500                 if ((replay->bitmap)[fr] & (1 << (diff % 8)))
1501                         return (0);
1502
1503                 /* Out of order but good. */
1504                 return (1);
1505         }
1506 }
1507
1508 /*
1509  * Check replay counter whether to update or not.
1510  * OUT: 0:      OK
1511  *      1:      NG
1512  */
1513 int
1514 ipsec_updatereplay(u_int32_t seq, struct secasvar *sav)
1515 {
1516         struct secreplay *replay;
1517         u_int32_t diff;
1518         int fr;
1519         u_int32_t wsizeb;       /* Constant: bits of window size. */
1520         int frlast;             /* Constant: last frame. */
1521
1522         IPSEC_ASSERT(sav != NULL, ("Null SA"));
1523         IPSEC_ASSERT(sav->replay != NULL, ("Null replay state"));
1524
1525         replay = sav->replay;
1526
1527         if (replay->wsize == 0)
1528                 goto ok;        /* No need to check replay. */
1529
1530         /* Constant. */
1531         frlast = replay->wsize - 1;
1532         wsizeb = replay->wsize << 3;
1533
1534         /* Sequence number of 0 is invalid. */
1535         if (seq == 0)
1536                 return (1);
1537
1538         /* First time. */
1539         if (replay->count == 0) {
1540                 replay->lastseq = seq;
1541                 bzero(replay->bitmap, replay->wsize);
1542                 (replay->bitmap)[frlast] = 1;
1543                 goto ok;
1544         }
1545
1546         if (seq > replay->lastseq) {
1547                 /* seq is larger than lastseq. */
1548                 diff = seq - replay->lastseq;
1549
1550                 /* New larger sequence number. */
1551                 if (diff < wsizeb) {
1552                         /* In window. */
1553                         /* Set bit for this packet. */
1554                         vshiftl(replay->bitmap, diff, replay->wsize);
1555                         (replay->bitmap)[frlast] |= 1;
1556                 } else {
1557                         /* This packet has a "way larger". */
1558                         bzero(replay->bitmap, replay->wsize);
1559                         (replay->bitmap)[frlast] = 1;
1560                 }
1561                 replay->lastseq = seq;
1562
1563                 /* Larger is good. */
1564         } else {
1565                 /* seq is equal or less than lastseq. */
1566                 diff = replay->lastseq - seq;
1567
1568                 /* Over range to check, i.e. too old or wrapped. */
1569                 if (diff >= wsizeb)
1570                         return (1);
1571
1572                 fr = frlast - diff / 8;
1573
1574                 /* This packet already seen? */
1575                 if ((replay->bitmap)[fr] & (1 << (diff % 8)))
1576                         return (1);
1577
1578                 /* Mark as seen. */
1579                 (replay->bitmap)[fr] |= (1 << (diff % 8));
1580
1581                 /* Out of order but good. */
1582         }
1583
1584 ok:
1585         if (replay->count == ~0) {
1586
1587                 /* Set overflow flag. */
1588                 replay->overflow++;
1589
1590                 /* Don't increment, no more packets accepted. */
1591                 if ((sav->flags & SADB_X_EXT_CYCSEQ) == 0)
1592                         return (1);
1593
1594                 ipseclog((LOG_WARNING, "%s: replay counter made %d cycle. %s\n",
1595                     __func__, replay->overflow, ipsec_logsastr(sav)));
1596         }
1597
1598         replay->count++;
1599
1600         return (0);
1601 }
1602
1603 /*
1604  * Shift variable length buffer to left.
1605  * IN:  bitmap: pointer to the buffer
1606  *      nbit:   the number of to shift.
1607  *      wsize:  buffer size (bytes).
1608  */
1609 static void
1610 vshiftl(unsigned char *bitmap, int nbit, int wsize)
1611 {
1612         int s, j, i;
1613         unsigned char over;
1614
1615         for (j = 0; j < nbit; j += 8) {
1616                 s = (nbit - j < 8) ? (nbit - j): 8;
1617                 bitmap[0] <<= s;
1618                 for (i = 1; i < wsize; i++) {
1619                         over = (bitmap[i] >> (8 - s));
1620                         bitmap[i] <<= s;
1621                         bitmap[i-1] |= over;
1622                 }
1623         }
1624 }
1625
1626 #ifdef INET
1627 /* Return a printable string for the IPv4 address. */
1628 static char *
1629 inet_ntoa4(struct in_addr ina)
1630 {
1631         static char buf[4][4 * sizeof "123" + 4];
1632         unsigned char *ucp = (unsigned char *) &ina;
1633         static int i = 3;
1634
1635         /* XXX-BZ Returns static buffer. */
1636         i = (i + 1) % 4;
1637         sprintf(buf[i], "%d.%d.%d.%d", ucp[0] & 0xff, ucp[1] & 0xff,
1638             ucp[2] & 0xff, ucp[3] & 0xff);
1639         return (buf[i]);
1640 }
1641 #endif
1642
1643 /* Return a printable string for the address. */
1644 char *
1645 ipsec_address(union sockaddr_union* sa)
1646 {
1647 #ifdef INET6
1648         char ip6buf[INET6_ADDRSTRLEN];
1649 #endif
1650
1651         switch (sa->sa.sa_family) {
1652 #ifdef INET
1653         case AF_INET:
1654                 return (inet_ntoa4(sa->sin.sin_addr));
1655 #endif /* INET */
1656 #ifdef INET6
1657         case AF_INET6:
1658                 return (ip6_sprintf(ip6buf, &sa->sin6.sin6_addr));
1659 #endif /* INET6 */
1660         default:
1661                 return ("(unknown address family)");
1662         }
1663 }
1664
1665 const char *
1666 ipsec_logsastr(struct secasvar *sav)
1667 {
1668         static char buf[256];
1669         char *p;
1670         struct secasindex *saidx = &sav->sah->saidx;
1671
1672         IPSEC_ASSERT(saidx->src.sa.sa_family == saidx->dst.sa.sa_family,
1673                 ("address family mismatch"));
1674
1675         p = buf;
1676         snprintf(buf, sizeof(buf), "SA(SPI=%u ", (u_int32_t)ntohl(sav->spi));
1677         while (p && *p)
1678                 p++;
1679         /* NB: only use ipsec_address on one address at a time. */
1680         snprintf(p, sizeof (buf) - (p - buf), "src=%s ",
1681                 ipsec_address(&saidx->src));
1682         while (p && *p)
1683                 p++;
1684         snprintf(p, sizeof (buf) - (p - buf), "dst=%s)",
1685                 ipsec_address(&saidx->dst));
1686
1687         return (buf);
1688 }
1689
1690 void
1691 ipsec_dumpmbuf(struct mbuf *m)
1692 {
1693         int totlen;
1694         int i;
1695         u_char *p;
1696
1697         totlen = 0;
1698         printf("---\n");
1699         while (m) {
1700                 p = mtod(m, u_char *);
1701                 for (i = 0; i < m->m_len; i++) {
1702                         printf("%02x ", p[i]);
1703                         totlen++;
1704                         if (totlen % 16 == 0)
1705                                 printf("\n");
1706                 }
1707                 m = m->m_next;
1708         }
1709         if (totlen % 16 != 0)
1710                 printf("\n");
1711         printf("---\n");
1712 }
1713
1714 static void
1715 ipsec_init(const void *unused __unused)
1716 {
1717
1718         SECPOLICY_LOCK_INIT(&V_ip4_def_policy);
1719         V_ip4_def_policy.refcnt = 1;                    /* NB: disallow free. */
1720 }
1721 VNET_SYSINIT(ipsec_init, SI_SUB_PROTO_DOMAININIT, SI_ORDER_ANY, ipsec_init,
1722     NULL);
1723
1724
1725 /* XXX This stuff doesn't belong here... */
1726
1727 static  struct xformsw* xforms = NULL;
1728
1729 /*
1730  * Register a transform; typically at system startup.
1731  */
1732 void
1733 xform_register(struct xformsw* xsp)
1734 {
1735
1736         xsp->xf_next = xforms;
1737         xforms = xsp;
1738 }
1739
1740 /*
1741  * Initialize transform support in an sav.
1742  */
1743 int
1744 xform_init(struct secasvar *sav, int xftype)
1745 {
1746         struct xformsw *xsp;
1747
1748         if (sav->tdb_xform != NULL)     /* Previously initialized. */
1749                 return (0);
1750         for (xsp = xforms; xsp; xsp = xsp->xf_next)
1751                 if (xsp->xf_type == xftype)
1752                         return ((*xsp->xf_init)(sav, xsp));
1753         return (EINVAL);
1754 }