ipsec_mod.c revision 315514
1/*- 2 * Copyright (c) 2016 Andrey V. Elsukov <ae@FreeBSD.org> 3 * 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 * 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27#include "opt_inet.h" 28#include "opt_inet6.h" 29#include "opt_ipsec.h" 30 31#include <sys/cdefs.h> 32__FBSDID("$FreeBSD: stable/11/sys/netipsec/ipsec_mod.c 315514 2017-03-18 22:04:20Z ae $"); 33 34#include <sys/param.h> 35#include <sys/systm.h> 36#include <sys/kernel.h> 37#include <sys/lock.h> 38#include <sys/malloc.h> 39#include <sys/mbuf.h> 40#include <sys/module.h> 41#include <sys/priv.h> 42#include <sys/rmlock.h> 43#include <sys/socket.h> 44#include <sys/sockopt.h> 45#include <sys/syslog.h> 46#include <sys/proc.h> 47 48#include <netinet/in.h> 49#include <netinet/in_pcb.h> 50 51#include <netipsec/ipsec.h> 52#include <netipsec/ipsec6.h> 53#include <netipsec/key.h> 54#include <netipsec/key_debug.h> 55 56#include <netipsec/ipsec_support.h> 57 58#ifdef INET 59static const struct ipsec_methods ipv4_methods = { 60 .input = ipsec4_input, 61 .forward = ipsec4_forward, 62 .output = ipsec4_output, 63 .pcbctl = ipsec4_pcbctl, 64 .capability = ipsec4_capability, 65 .check_policy = ipsec4_in_reject, 66 .hdrsize = ipsec_hdrsiz_inpcb, 67 .udp_input = udp_ipsec_input, 68 .udp_pcbctl = udp_ipsec_pcbctl, 69}; 70#ifndef KLD_MODULE 71static const struct ipsec_support ipv4_ipsec = { 72 .enabled = IPSEC_MODULE_ENABLED, 73 .methods = &ipv4_methods 74}; 75const struct ipsec_support * const ipv4_ipsec_support = &ipv4_ipsec; 76#endif /* !KLD_MODULE */ 77#endif /* INET */ 78 79#ifdef INET6 80static const struct ipsec_methods ipv6_methods = { 81 .input = ipsec6_input, 82 .forward = ipsec6_forward, 83 .output = ipsec6_output, 84 .pcbctl = ipsec6_pcbctl, 85 .capability = ipsec6_capability, 86 .check_policy = ipsec6_in_reject, 87 .hdrsize = ipsec_hdrsiz_inpcb, 88}; 89#ifndef KLD_MODULE 90static const struct ipsec_support ipv6_ipsec = { 91 .enabled = IPSEC_MODULE_ENABLED, 92 .methods = &ipv6_methods 93}; 94const struct ipsec_support * const ipv6_ipsec_support = &ipv6_ipsec; 95#endif /* !KLD_MODULE */ 96#endif /* INET6 */ 97 98/* 99 * Always register ipsec module. 100 * Even when IPsec is build in the kernel, we need to have 101 * module registered. This will prevent to load ipsec.ko. 102 */ 103static int 104ipsec_modevent(module_t mod, int type, void *data) 105{ 106 107 switch (type) { 108 case MOD_LOAD: 109 /* All xforms are registered via SYSINIT */ 110 if (!ipsec_initialized()) 111 return (ENOMEM); 112#ifdef KLD_MODULE 113#ifdef INET 114 ipsec_support_enable(ipv4_ipsec_support, &ipv4_methods); 115#endif 116#ifdef INET6 117 ipsec_support_enable(ipv6_ipsec_support, &ipv6_methods); 118#endif 119#endif /* KLD_MODULE */ 120 break; 121 case MOD_UNLOAD: 122 /* All xforms are unregistered via SYSUNINIT */ 123#ifdef KLD_MODULE 124#ifdef INET 125 ipsec_support_disable(ipv4_ipsec_support); 126#endif 127#ifdef INET6 128 ipsec_support_disable(ipv6_ipsec_support); 129#endif 130#endif /* KLD_MODULE */ 131 break; 132 default: 133 return (EOPNOTSUPP); 134 } 135 return (0); 136} 137 138static moduledata_t ipsec_mod = { 139 "ipsec", 140 ipsec_modevent, 141 0 142}; 143 144DECLARE_MODULE(ipsec, ipsec_mod, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY); 145MODULE_VERSION(ipsec, 1); 146#ifdef KLD_MODULE 147MODULE_DEPEND(ipsec, ipsec_support, 1, 1, 1); 148#endif 149