1/*- 2 * Copyright (c) 1982, 1986, 1988, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 4. Neither the name of the University nor the names of its contributors 14 * may be used to endorse or promote products derived from this software 15 * without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 *
|
77 */ 78 if (m_tag_find(m, PACKET_TAG_IPSEC_IN_DONE, NULL) != NULL) 79 return 1; 80#endif 81 return 0; 82} 83 84/* 85 * Check if this packet has an active SA and needs to be dropped instead 86 * of forwarded. 87 * Called from ip_input(). 88 * 1 = drop packet, 0 = forward packet. 89 */ 90int 91ip_ipsec_fwd(struct mbuf *m) 92{ 93#ifdef IPSEC 94 struct m_tag *mtag; 95 struct tdb_ident *tdbi; 96 struct secpolicy *sp; 97 int s, error; 98 99 mtag = m_tag_find(m, PACKET_TAG_IPSEC_IN_DONE, NULL); 100 s = splnet(); 101 if (mtag != NULL) { 102 tdbi = (struct tdb_ident *)(mtag + 1); 103 sp = ipsec_getpolicy(tdbi, IPSEC_DIR_INBOUND); 104 } else { 105 sp = ipsec_getpolicybyaddr(m, IPSEC_DIR_INBOUND, 106 IP_FORWARDING, &error); 107 } 108 if (sp == NULL) { /* NB: can happen if error */ 109 splx(s); 110 /*XXX error stat???*/ 111 DPRINTF(("ip_input: no SP for forwarding\n")); /*XXX*/ 112 return 1; 113 } 114 115 /* 116 * Check security policy against packet attributes. 117 */ 118 error = ipsec_in_reject(sp, m); 119 KEY_FREESP(&sp); 120 splx(s); 121 if (error) { 122 ipstat.ips_cantforward++; 123 return 1; 124 } 125#endif /* IPSEC */ 126 return 0; 127} 128 129/* 130 * Check if protocol type doesn't have a further header and do IPSEC 131 * decryption or reject right now. Protocols with further headers get 132 * their IPSEC treatment within the protocol specific processing. 133 * Called from ip_input(). 134 * 1 = drop packet, 0 = continue processing packet. 135 */ 136int 137ip_ipsec_input(struct mbuf *m) 138{ 139 struct ip *ip = mtod(m, struct ip *); 140#ifdef IPSEC 141 struct m_tag *mtag; 142 struct tdb_ident *tdbi; 143 struct secpolicy *sp; 144 int s, error; 145 /* 146 * enforce IPsec policy checking if we are seeing last header. 147 * note that we do not visit this with protocols with pcb layer 148 * code - like udp/tcp/raw ip. 149 */ 150 if ((inetsw[ip_protox[ip->ip_p]].pr_flags & PR_LASTHDR) != 0) { 151 /* 152 * Check if the packet has already had IPsec processing 153 * done. If so, then just pass it along. This tag gets 154 * set during AH, ESP, etc. input handling, before the 155 * packet is returned to the ip input queue for delivery. 156 */ 157 mtag = m_tag_find(m, PACKET_TAG_IPSEC_IN_DONE, NULL); 158 s = splnet(); 159 if (mtag != NULL) { 160 tdbi = (struct tdb_ident *)(mtag + 1); 161 sp = ipsec_getpolicy(tdbi, IPSEC_DIR_INBOUND); 162 } else { 163 sp = ipsec_getpolicybyaddr(m, IPSEC_DIR_INBOUND, 164 IP_FORWARDING, &error); 165 } 166 if (sp != NULL) { 167 /* 168 * Check security policy against packet attributes. 169 */ 170 error = ipsec_in_reject(sp, m); 171 KEY_FREESP(&sp); 172 } else { 173 /* XXX error stat??? */ 174 error = EINVAL; 175 DPRINTF(("ip_input: no SP, packet discarded\n"));/*XXX*/ 176 return 1; 177 } 178 splx(s); 179 if (error) 180 return 1; 181 } 182#endif /* IPSEC */ 183 return 0; 184} 185 186/* 187 * Compute the MTU for a forwarded packet that gets IPSEC encapsulated. 188 * Called from ip_forward(). 189 * Returns MTU suggestion for ICMP needfrag reply. 190 */ 191int 192ip_ipsec_mtu(struct mbuf *m) 193{ 194 int mtu = 0; 195 /* 196 * If the packet is routed over IPsec tunnel, tell the 197 * originator the tunnel MTU. 198 * tunnel MTU = if MTU - sizeof(IP) - ESP/AH hdrsiz 199 * XXX quickhack!!! 200 */ 201 struct secpolicy *sp = NULL; 202 int ipsecerror; 203 int ipsechdr; 204 struct route *ro; 205 sp = ipsec_getpolicybyaddr(m, 206 IPSEC_DIR_OUTBOUND, 207 IP_FORWARDING, 208 &ipsecerror); 209 if (sp != NULL) { 210 /* count IPsec header size */ 211 ipsechdr = ipsec4_hdrsiz(m, 212 IPSEC_DIR_OUTBOUND, 213 NULL); 214 215 /* 216 * find the correct route for outer IPv4 217 * header, compute tunnel MTU. 218 */ 219 if (sp->req != NULL && 220 sp->req->sav != NULL && 221 sp->req->sav->sah != NULL) { 222 ro = &sp->req->sav->sah->sa_route; 223 if (ro->ro_rt && ro->ro_rt->rt_ifp) { 224 mtu = 225 ro->ro_rt->rt_rmx.rmx_mtu ? 226 ro->ro_rt->rt_rmx.rmx_mtu : 227 ro->ro_rt->rt_ifp->if_mtu; 228 mtu -= ipsechdr; 229 } 230 } 231 KEY_FREESP(&sp); 232 } 233 return mtu; 234} 235 236/* 237 * 238 * Called from ip_output(). 239 * 1 = drop packet, 0 = continue processing packet, 240 * -1 = packet was reinjected and stop processing packet 241 */ 242int 243ip_ipsec_output(struct mbuf **m, struct inpcb *inp, int *flags, int *error, 244 struct route **ro, struct route *iproute, struct sockaddr_in **dst, 245 struct in_ifaddr **ia, struct ifnet **ifp) 246{ 247#ifdef IPSEC 248 struct secpolicy *sp = NULL; 249 struct ip *ip = mtod(*m, struct ip *); 250 struct tdb_ident *tdbi; 251 struct m_tag *mtag; 252 int s; 253 /* 254 * Check the security policy (SP) for the packet and, if 255 * required, do IPsec-related processing. There are two 256 * cases here; the first time a packet is sent through 257 * it will be untagged and handled by ipsec4_checkpolicy. 258 * If the packet is resubmitted to ip_output (e.g. after 259 * AH, ESP, etc. processing), there will be a tag to bypass 260 * the lookup and related policy checking. 261 */ 262 mtag = m_tag_find(*m, PACKET_TAG_IPSEC_PENDING_TDB, NULL); 263 s = splnet(); 264 if (mtag != NULL) { 265 tdbi = (struct tdb_ident *)(mtag + 1); 266 sp = ipsec_getpolicy(tdbi, IPSEC_DIR_OUTBOUND); 267 if (sp == NULL) 268 *error = -EINVAL; /* force silent drop */ 269 m_tag_delete(*m, mtag); 270 } else { 271 sp = ipsec4_checkpolicy(*m, IPSEC_DIR_OUTBOUND, *flags, 272 error, inp); 273 } 274 /* 275 * There are four return cases: 276 * sp != NULL apply IPsec policy 277 * sp == NULL, error == 0 no IPsec handling needed 278 * sp == NULL, error == -EINVAL discard packet w/o error 279 * sp == NULL, error != 0 discard packet, report error 280 */ 281 if (sp != NULL) { 282 /* Loop detection, check if ipsec processing already done */ 283 KASSERT(sp->req != NULL, ("ip_output: no ipsec request")); 284 for (mtag = m_tag_first(*m); mtag != NULL; 285 mtag = m_tag_next(*m, mtag)) { 286 if (mtag->m_tag_cookie != MTAG_ABI_COMPAT) 287 continue; 288 if (mtag->m_tag_id != PACKET_TAG_IPSEC_OUT_DONE && 289 mtag->m_tag_id != PACKET_TAG_IPSEC_OUT_CRYPTO_NEEDED) 290 continue; 291 /* 292 * Check if policy has an SA associated with it. 293 * This can happen when an SP has yet to acquire 294 * an SA; e.g. on first reference. If it occurs, 295 * then we let ipsec4_process_packet do its thing. 296 */ 297 if (sp->req->sav == NULL) 298 break; 299 tdbi = (struct tdb_ident *)(mtag + 1); 300 if (tdbi->spi == sp->req->sav->spi && 301 tdbi->proto == sp->req->sav->sah->saidx.proto && 302 bcmp(&tdbi->dst, &sp->req->sav->sah->saidx.dst, 303 sizeof (union sockaddr_union)) == 0) { 304 /* 305 * No IPsec processing is needed, free 306 * reference to SP. 307 * 308 * NB: null pointer to avoid free at 309 * done: below. 310 */ 311 KEY_FREESP(&sp), sp = NULL; 312 splx(s); 313 goto done; 314 } 315 } 316 317 /* 318 * Do delayed checksums now because we send before 319 * this is done in the normal processing path. 320 */ 321 if ((*m)->m_pkthdr.csum_flags & CSUM_DELAY_DATA) { 322 in_delayed_cksum(*m); 323 (*m)->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA; 324 } 325 326 ip->ip_len = htons(ip->ip_len); 327 ip->ip_off = htons(ip->ip_off); 328 329 /* NB: callee frees mbuf */ 330 *error = ipsec4_process_packet(*m, sp->req, *flags, 0); 331 /* 332 * Preserve KAME behaviour: ENOENT can be returned 333 * when an SA acquire is in progress. Don't propagate 334 * this to user-level; it confuses applications. 335 * 336 * XXX this will go away when the SADB is redone. 337 */ 338 if (*error == ENOENT) 339 *error = 0; 340 splx(s); 341 goto reinjected; 342 } else { /* sp == NULL */ 343 splx(s); 344 345 if (*error != 0) { 346 /* 347 * Hack: -EINVAL is used to signal that a packet 348 * should be silently discarded. This is typically 349 * because we asked key management for an SA and 350 * it was delayed (e.g. kicked up to IKE). 351 */ 352 if (*error == -EINVAL) 353 *error = 0; 354 goto bad; 355 } else { 356 /* No IPsec processing for this packet. */ 357 } 358#ifdef notyet 359 /* 360 * If deferred crypto processing is needed, check that 361 * the interface supports it. 362 */ 363 mtag = m_tag_find(*m, PACKET_TAG_IPSEC_OUT_CRYPTO_NEEDED, NULL); 364 if (mtag != NULL && ((*ifp)->if_capenable & IFCAP_IPSEC) == 0) { 365 /* notify IPsec to do its own crypto */ 366 ipsp_skipcrypto_unmark((struct tdb_ident *)(mtag + 1)); 367 *error = EHOSTUNREACH; 368 goto bad; 369 } 370#endif 371 } 372done: 373 if (sp != NULL) 374 KEY_FREESP(&sp); 375 return 0; 376reinjected: 377 if (sp != NULL) 378 KEY_FREESP(&sp); 379 return -1; 380bad: 381 if (sp != NULL) 382 KEY_FREESP(&sp); 383 return 1; 384#endif /* IPSEC */ 385 return 0; 386}
|