Initial import from FreeBSD RELENG_4:
[dragonfly.git] / sys / contrib / ipfilter / netinet / ip_ipsec_pxy.c
1 /*
2  * Simple ISAKMP transparent proxy for in-kernel use.  For use with the NAT
3  * code.
4  *
5  * $Id: ip_ipsec_pxy.c,v 1.1.2.10 2002/01/13 04:58:29 darrenr Exp $
6  * $FreeBSD: src/sys/contrib/ipfilter/netinet/ip_ipsec_pxy.c,v 1.1.1.1.2.1 2002/04/27 17:37:12 darrenr Exp $
7  *
8  */
9 #define IPF_IPSEC_PROXY
10
11
12 int ippr_ipsec_init __P((void));
13 int ippr_ipsec_new __P((fr_info_t *, ip_t *, ap_session_t *, nat_t *));
14 void ippr_ipsec_del __P((ap_session_t *));
15 int ippr_ipsec_out __P((fr_info_t *, ip_t *, ap_session_t *, nat_t *));
16 int ippr_ipsec_match __P((fr_info_t *, ap_session_t *, nat_t *));
17
18 static  frentry_t       ipsecfr;
19
20
21 static  char    ipsec_buffer[1500];
22
23 /*
24  * RCMD application proxy initialization.
25  */
26 int ippr_ipsec_init()
27 {
28         bzero((char *)&ipsecfr, sizeof(ipsecfr));
29         ipsecfr.fr_ref = 1;
30         ipsecfr.fr_flags = FR_OUTQUE|FR_PASS|FR_QUICK|FR_KEEPSTATE;
31         return 0;
32 }
33
34
35 /*
36  * Setup for a new IPSEC proxy.
37  */
38 int ippr_ipsec_new(fin, ip, aps, nat)
39 fr_info_t *fin;
40 ip_t *ip;
41 ap_session_t *aps;
42 nat_t *nat;
43 {
44         ipsec_pxy_t *ipsec;
45         fr_info_t fi;
46         ipnat_t *ipn;
47         char *ptr;
48         int p, off, dlen;
49         mb_t *m;
50
51         bzero(ipsec_buffer, sizeof(ipsec_buffer));
52         off = fin->fin_hlen + sizeof(udphdr_t);
53 #ifdef  _KERNEL
54 # if     SOLARIS
55         m = fin->fin_qfm;
56
57         dlen = msgdsize(m) - off;
58         if (dlen < 16)
59                 return -1;
60         copyout_mblk(m, off, MIN(sizeof(ipsec_buffer), dlen), ipsec_buffer);
61 # else
62         m = *(mb_t **)fin->fin_mp;
63         dlen = mbufchainlen(m) - off;
64         if (dlen < 16)
65                 return -1;
66         m_copydata(m, off, MIN(sizeof(ipsec_buffer), dlen), ipsec_buffer);
67 # endif
68 #else
69         m = *(mb_t **)fin->fin_mp;
70         dlen = ip->ip_len - off;
71         ptr = (char *)m;
72         ptr += off;
73         bcopy(ptr, ipsec_buffer, MIN(sizeof(ipsec_buffer), dlen));
74 #endif
75
76         /*
77          * Because _new() gets called from nat_new(), ipf_nat is held with a
78          * write lock so pass rw=1 to nat_outlookup().
79          */
80         if (nat_outlookup(fin, 0, IPPROTO_ESP, nat->nat_inip,
81                           ip->ip_dst, 1) != NULL)
82                 return -1;
83
84         aps->aps_psiz = sizeof(*ipsec);
85         KMALLOCS(aps->aps_data, ipsec_pxy_t *, sizeof(*ipsec));
86         if (aps->aps_data == NULL)
87                 return -1;
88
89         ipsec = aps->aps_data;
90         bzero((char *)ipsec, sizeof(*ipsec));
91
92         /*
93          * Create NAT rule against which the tunnel/transport mapping is
94          * created.  This is required because the current NAT rule does not
95          * describe ESP but UDP instead.
96          */
97         ipn = &ipsec->ipsc_rule;
98         ipn->in_ifp = fin->fin_ifp;
99         ipn->in_apr = NULL;
100         ipn->in_use = 1;
101         ipn->in_hits = 1;
102         ipn->in_nip = ntohl(nat->nat_outip.s_addr);
103         ipn->in_ippip = 1;
104         ipn->in_inip = nat->nat_inip.s_addr;
105         ipn->in_inmsk = 0xffffffff;
106         ipn->in_outip = nat->nat_outip.s_addr;
107         ipn->in_outmsk = 0xffffffff;
108         ipn->in_srcip = fin->fin_saddr;
109         ipn->in_srcmsk = 0xffffffff;
110         ipn->in_redir = NAT_MAP;
111         bcopy(nat->nat_ptr->in_ifname, ipn->in_ifname, sizeof(ipn->in_ifname));
112         ipn->in_p = IPPROTO_ESP;
113
114         bcopy((char *)fin, (char *)&fi, sizeof(fi));
115         fi.fin_fi.fi_p = IPPROTO_ESP;
116         fi.fin_fr = &ipsecfr;
117         fi.fin_data[0] = 0;
118         fi.fin_data[1] = 0;
119         p = ip->ip_p;
120         ip->ip_p = IPPROTO_ESP;
121         fi.fin_fl &= ~FI_TCPUDP;
122
123         ptr = ipsec_buffer;
124         bcopy(ptr, ipsec->ipsc_icookie, sizeof(ipsec_cookie_t));
125         ptr += sizeof(ipsec_cookie_t);
126         bcopy(ptr, ipsec->ipsc_rcookie, sizeof(ipsec_cookie_t));
127         /*
128          * The responder cookie should only be non-zero if the initiator
129          * cookie is non-zero.  Therefore, it is safe to assume(!) that the
130          * cookies are both set after copying if the responder is non-zero.
131          */
132         if ((ipsec->ipsc_rcookie[0]|ipsec->ipsc_rcookie[1]) != 0)
133                 ipsec->ipsc_rckset = 1;
134         else
135                 nat->nat_age = 60;      /* 30 seconds */
136
137         ipsec->ipsc_nat = nat_new(&fi, ip, ipn, &ipsec->ipsc_nat, FI_IGNOREPKT,
138                                    NAT_OUTBOUND);
139         if (ipsec->ipsc_nat != NULL) {
140                 fi.fin_data[0] = 0;
141                 fi.fin_data[1] = 0;
142                 ipsec->ipsc_state = fr_addstate(ip, &fi, &ipsec->ipsc_state,
143                                                 FI_IGNOREPKT|FI_NORULE);
144         }
145         ip->ip_p = p;
146         return 0;
147 }
148
149
150 /*
151  * For outgoing IKE packets.  refresh timeouts for NAT & stat entries, if
152  * we can.  If they have disappeared, recreate them.
153  */
154 int ippr_ipsec_out(fin, ip, aps, nat)
155 fr_info_t *fin;
156 ip_t *ip;
157 ap_session_t *aps;
158 nat_t *nat;
159 {
160         ipsec_pxy_t *ipsec;
161         fr_info_t fi;
162         int p;
163
164         bcopy((char *)fin, (char *)&fi, sizeof(fi));
165         fi.fin_fi.fi_p = IPPROTO_ESP;
166         fi.fin_fr = &ipsecfr;
167         fi.fin_data[0] = 0;
168         fi.fin_data[1] = 0;
169         p = ip->ip_p;
170         ip->ip_p = IPPROTO_ESP;
171         fi.fin_fl &= ~FI_TCPUDP;
172
173         ipsec = aps->aps_data;
174         if (ipsec != NULL) {
175                 /*
176                  * Update NAT timeout/create NAT if missing.
177                  */
178                 if (ipsec->ipsc_rckset == 0)
179                         nat->nat_age = 60;      /* 30 seconds */
180                 if (ipsec->ipsc_nat != NULL)
181                         ipsec->ipsc_nat->nat_age = nat->nat_age;
182                 else
183                         ipsec->ipsc_nat = nat_new(&fi, ip, &ipsec->ipsc_rule,
184                                                   &ipsec->ipsc_nat,
185                                                   FI_IGNOREPKT, NAT_OUTBOUND);
186
187                 /*
188                  * Update state timeout/create state if missing.
189                  */
190                 READ_ENTER(&ipf_state);
191                 if (ipsec->ipsc_state != NULL) {
192                         ipsec->ipsc_state->is_age = nat->nat_age;
193                         RWLOCK_EXIT(&ipf_state);
194                 } else {
195                         RWLOCK_EXIT(&ipf_state);
196                         fi.fin_data[0] = 0;
197                         fi.fin_data[1] = 0;
198                         ipsec->ipsc_state = fr_addstate(ip, &fi,
199                                                         &ipsec->ipsc_state,
200                                                         FI_IGNOREPKT|FI_NORULE);
201                 }
202         }
203         ip->ip_p = p;
204         return 0;
205 }
206
207
208 /*
209  * This extends the NAT matching to be based on the cookies associated with
210  * a session and found at the front of IKE packets.  The cookies are always
211  * in the same order (not reversed depending on packet flow direction as with
212  * UDP/TCP port numbers).
213  */
214 int ippr_ipsec_match(fin, aps, nat)
215 fr_info_t *fin;
216 ap_session_t *aps;
217 nat_t *nat;
218 {
219         ipsec_pxy_t *ipsec;
220         u_32_t cookies[4];
221         mb_t *m;
222         int off;
223
224         if ((fin->fin_dlen < sizeof(cookies)) || (fin->fin_fl & FI_FRAG))
225                 return -1;
226
227         ipsec = aps->aps_data;
228         off = fin->fin_hlen + sizeof(udphdr_t);
229 #ifdef  _KERNEL
230 # if     SOLARIS
231         m = fin->fin_qfm;
232
233         copyout_mblk(m, off, sizeof(cookies), (char *)cookies);
234 # else
235         m = *(mb_t **)fin->fin_mp;
236         m_copydata(m, off, sizeof(cookies), (char *)cookies);
237 # endif
238 #else
239         m = *(mb_t **)fin->fin_mp;
240         bcopy((char *)m + off, cookies, sizeof(cookies));
241 #endif
242
243         if ((cookies[0] != ipsec->ipsc_icookie[0]) ||
244             (cookies[1] != ipsec->ipsc_icookie[1]))
245                 return -1;
246
247         if (ipsec->ipsc_rckset == 0) {
248                 if ((cookies[2]|cookies[3]) == 0) {
249                         nat->nat_age = 60;      /* 30 seconds */
250                         return 0;
251                 }
252                 ipsec->ipsc_rckset = 1;
253                 ipsec->ipsc_rcookie[0] = cookies[2];
254                 ipsec->ipsc_rcookie[1] = cookies[3];
255                 return 0;
256         }
257
258         if ((cookies[2] != ipsec->ipsc_rcookie[0]) ||
259             (cookies[3] != ipsec->ipsc_rcookie[1]))
260                 return -1;
261         return 0;
262 }
263
264
265 /*
266  * clean up after ourselves.
267  */
268 void ippr_ipsec_del(aps)
269 ap_session_t *aps;
270 {
271         ipsec_pxy_t *ipsec;
272
273         ipsec = aps->aps_data;
274
275         if (ipsec != NULL) {
276                 /*
277                  * Don't delete it from here, just schedule it to be
278                  * deleted ASAP.
279                  */
280                 if (ipsec->ipsc_nat != NULL) {
281                         ipsec->ipsc_nat->nat_age = 1;
282                         ipsec->ipsc_nat->nat_ptr = NULL;
283                 }
284
285                 READ_ENTER(&ipf_state);
286                 if (ipsec->ipsc_state != NULL)
287                         ipsec->ipsc_state->is_age = 1;
288                 RWLOCK_EXIT(&ipf_state);
289
290                 ipsec->ipsc_state = NULL;
291                 ipsec->ipsc_nat = NULL;
292         }
293 }