1 /* $NetBSD: in_proto.c,v 1.79 2006/11/23 04:07:07 rpaulo Exp $ */ 2 3 /* 4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. Neither the name of the project nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32 /* 33 * Copyright (c) 1982, 1986, 1993 34 * The Regents of the University of California. All rights reserved. 35 * 36 * Redistribution and use in source and binary forms, with or without 37 * modification, are permitted provided that the following conditions 38 * are met: 39 * 1. Redistributions of source code must retain the above copyright 40 * notice, this list of conditions and the following disclaimer. 41 * 2. Redistributions in binary form must reproduce the above copyright 42 * notice, this list of conditions and the following disclaimer in the 43 * documentation and/or other materials provided with the distribution. 44 * 3. Neither the name of the University nor the names of its contributors 45 * may be used to endorse or promote products derived from this software 46 * without specific prior written permission. 47 * 48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 58 * SUCH DAMAGE. 59 * 60 * @(#)in_proto.c 8.2 (Berkeley) 2/9/95 61 */ 62 63 #include <sys/cdefs.h> 64 __KERNEL_RCSID(0, "$NetBSD: in_proto.c,v 1.79 2006/11/23 04:07:07 rpaulo Exp $"); 65 66 #include "opt_mrouting.h" 67 #include "opt_eon.h" /* ISO CLNL over IP */ 68 #include "opt_iso.h" /* ISO TP tunneled over IP */ 69 #include "opt_inet.h" 70 #include "opt_ipsec.h" 71 #include "opt_pim.h" 72 73 #include <sys/param.h> 74 #include <sys/socket.h> 75 #include <sys/protosw.h> 76 #include <sys/domain.h> 77 #include <sys/mbuf.h> 78 79 #include <net/if.h> 80 #include <net/radix.h> 81 #include <net/route.h> 82 83 #include <netinet/in.h> 84 #include <netinet/in_systm.h> 85 #include <netinet/ip.h> 86 #include <netinet/ip_var.h> 87 #include <netinet/ip_icmp.h> 88 #include <netinet/in_ifattach.h> 89 #include <netinet/in_pcb.h> 90 #include <netinet/in_proto.h> 91 92 #ifdef INET6 93 #ifndef INET 94 #include <netinet/in.h> 95 #endif 96 #include <netinet/ip6.h> 97 #endif 98 99 #include <netinet/igmp_var.h> 100 #ifdef PIM 101 #include <netinet/pim_var.h> 102 #endif 103 #include <netinet/tcp.h> 104 #include <netinet/tcp_fsm.h> 105 #include <netinet/tcp_seq.h> 106 #include <netinet/tcp_timer.h> 107 #include <netinet/tcp_var.h> 108 #include <netinet/tcpip.h> 109 #include <netinet/tcp_debug.h> 110 #include <netinet/udp.h> 111 #include <netinet/udp_var.h> 112 #include <netinet/ip_encap.h> 113 114 /* 115 * TCP/IP protocol family: IP, ICMP, UDP, TCP. 116 */ 117 118 #ifdef IPSEC 119 #include <netinet6/ipsec.h> 120 #include <netinet6/ah.h> 121 #ifdef IPSEC_ESP 122 #include <netinet6/esp.h> 123 #endif 124 #include <netinet6/ipcomp.h> 125 #endif /* IPSEC */ 126 127 #ifdef FAST_IPSEC 128 #include <netipsec/ipsec.h> 129 #include <netipsec/key.h> 130 #endif /* FAST_IPSEC */ 131 132 #ifdef TPIP 133 #include <netiso/tp_param.h> 134 #include <netiso/tp_var.h> 135 #endif /* TPIP */ 136 137 #ifdef EON 138 #include <netiso/eonvar.h> 139 #endif /* EON */ 140 141 #include "gre.h" 142 #if NGRE > 0 143 #include <netinet/ip_gre.h> 144 #endif 145 146 #include "carp.h" 147 #if NCARP > 0 148 #include <netinet/ip_carp.h> 149 #endif 150 151 #include "etherip.h" 152 #if NETHERIP > 0 153 #include <netinet/ip_etherip.h> 154 #endif 155 156 DOMAIN_DEFINE(inetdomain); /* forward declare and add to link set */ 157 158 const struct protosw inetsw[] = { 159 { 0, &inetdomain, 0, 0, 160 0, ip_output, 0, 0, 161 0, 162 ip_init, 0, ip_slowtimo, ip_drain, 163 }, 164 { SOCK_DGRAM, &inetdomain, IPPROTO_UDP, PR_ATOMIC|PR_ADDR|PR_PURGEIF, 165 udp_input, 0, udp_ctlinput, udp_ctloutput, 166 udp_usrreq, 167 udp_init, 0, 0, 0, 168 }, 169 { SOCK_STREAM, &inetdomain, IPPROTO_TCP, PR_CONNREQUIRED|PR_WANTRCVD|PR_LISTEN|PR_ABRTACPTDIS|PR_PURGEIF, 170 tcp_input, 0, tcp_ctlinput, tcp_ctloutput, 171 tcp_usrreq, 172 tcp_init, 0, tcp_slowtimo, tcp_drain, 173 }, 174 { SOCK_RAW, &inetdomain, IPPROTO_RAW, PR_ATOMIC|PR_ADDR|PR_PURGEIF, 175 rip_input, rip_output, rip_ctlinput, rip_ctloutput, 176 rip_usrreq, 177 0, 0, 0, 0, 178 }, 179 { SOCK_RAW, &inetdomain, IPPROTO_ICMP, PR_ATOMIC|PR_ADDR|PR_LASTHDR, 180 icmp_input, rip_output, rip_ctlinput, rip_ctloutput, 181 rip_usrreq, 182 icmp_init, 0, 0, 0, 183 }, 184 #ifdef IPSEC 185 { SOCK_RAW, &inetdomain, IPPROTO_AH, PR_ATOMIC|PR_ADDR, 186 ah4_input, 0, ah4_ctlinput, 0, 187 0, 188 0, 0, 0, 0, 189 }, 190 #ifdef IPSEC_ESP 191 { SOCK_RAW, &inetdomain, IPPROTO_ESP, PR_ATOMIC|PR_ADDR, 192 esp4_input, 193 0, esp4_ctlinput, 0, 194 0, 195 0, 0, 0, 0, 196 }, 197 #endif 198 { SOCK_RAW, &inetdomain, IPPROTO_IPCOMP, PR_ATOMIC|PR_ADDR, 199 ipcomp4_input, 200 0, 0, 0, 201 0, 202 0, 0, 0, 0, 203 }, 204 #endif /* IPSEC */ 205 #ifdef FAST_IPSEC 206 { SOCK_RAW, &inetdomain, IPPROTO_AH, PR_ATOMIC|PR_ADDR, 207 ipsec4_common_input, 0, ah4_ctlinput, 0, 208 0, 0, 0, 0, 0, 209 }, 210 { SOCK_RAW, &inetdomain, IPPROTO_ESP, PR_ATOMIC|PR_ADDR, 211 ipsec4_common_input, 0, esp4_ctlinput, 0, 212 0, 213 0, 0, 0, 0, 214 }, 215 { SOCK_RAW, &inetdomain, IPPROTO_IPCOMP, PR_ATOMIC|PR_ADDR, 216 ipsec4_common_input, 0, 0, 0, 217 0, 218 0, 0, 0, 0, 219 }, 220 #endif /* FAST_IPSEC */ 221 { SOCK_RAW, &inetdomain, IPPROTO_IPV4, PR_ATOMIC|PR_ADDR|PR_LASTHDR, 222 encap4_input, rip_output, rip_ctlinput, rip_ctloutput, 223 rip_usrreq, /*XXX*/ 224 encap_init, 0, 0, 0, 225 }, 226 #ifdef INET6 227 { SOCK_RAW, &inetdomain, IPPROTO_IPV6, PR_ATOMIC|PR_ADDR|PR_LASTHDR, 228 encap4_input, rip_output, rip_ctlinput, rip_ctloutput, 229 rip_usrreq, /*XXX*/ 230 encap_init, 0, 0, 0, 231 }, 232 #endif /* INET6 */ 233 #if NETHERIP > 0 234 { SOCK_RAW, &inetdomain, IPPROTO_ETHERIP, PR_ATOMIC|PR_ADDR|PR_LASTHDR, 235 ip_etherip_input, rip_output, rip_ctlinput, rip_ctloutput, 236 rip_usrreq, 237 0, 0, 0, 0, 238 }, 239 #endif 240 #if NCARP > 0 241 { SOCK_RAW, &inetdomain, IPPROTO_CARP, PR_ATOMIC|PR_ADDR, 242 carp_proto_input, rip_output, 0, rip_ctloutput, 243 rip_usrreq, 244 0, 0, 0, 0, 245 }, 246 #endif 247 #if NGRE > 0 248 { SOCK_RAW, &inetdomain, IPPROTO_GRE, PR_ATOMIC|PR_ADDR|PR_LASTHDR, 249 gre_input, rip_output, rip_ctlinput, rip_ctloutput, 250 rip_usrreq, 251 0, 0, 0, 0, 252 }, 253 { SOCK_RAW, &inetdomain, IPPROTO_MOBILE, PR_ATOMIC|PR_ADDR|PR_LASTHDR, 254 gre_mobile_input, rip_output, rip_ctlinput, rip_ctloutput, 255 rip_usrreq, 256 0, 0, 0, 0, 257 }, 258 #endif /* NGRE > 0 */ 259 { SOCK_RAW, &inetdomain, IPPROTO_IGMP, PR_ATOMIC|PR_ADDR|PR_LASTHDR, 260 igmp_input, rip_output, rip_ctlinput, rip_ctloutput, 261 rip_usrreq, 262 NULL, igmp_fasttimo, igmp_slowtimo, 0, 263 }, 264 #ifdef PIM 265 { SOCK_RAW, &inetdomain, IPPROTO_PIM, PR_ATOMIC|PR_ADDR|PR_LASTHDR, 266 pim_input, rip_output, rip_ctlinput, rip_ctloutput, 267 rip_usrreq, 268 NULL, 0, 0, 0, 269 }, 270 #endif /* PIM */ 271 #ifdef TPIP 272 { SOCK_SEQPACKET,&inetdomain, IPPROTO_TP, PR_CONNREQUIRED|PR_WANTRCVD|PR_LISTEN|PR_LASTHDR|PR_ABRTACPTDIS, 273 tpip_input, 0, tpip_ctlinput, tp_ctloutput, 274 tp_usrreq, 275 tp_init, 0, tp_slowtimo, tp_drain, 276 }, 277 #endif /* TPIP */ 278 #ifdef ISO 279 /* EON (ISO CLNL over IP) */ 280 #ifdef EON 281 { SOCK_RAW, &inetdomain, IPPROTO_EON, PR_LASTHDR, 282 eoninput, 0, eonctlinput, 0, 283 0, 284 eonprotoinit, 0, 0, 0, 285 }, 286 #else 287 { SOCK_RAW, &inetdomain, IPPROTO_EON, PR_ATOMIC|PR_ADDR|PR_LASTHDR, 288 encap4_input, rip_output, rip_ctlinput, rip_ctloutput, 289 rip_usrreq, /*XXX*/ 290 encap_init, 0, 0, 0, 291 }, 292 #endif /* EON */ 293 #endif /* ISO */ 294 /* raw wildcard */ 295 { SOCK_RAW, &inetdomain, 0, PR_ATOMIC|PR_ADDR|PR_LASTHDR, 296 rip_input, rip_output, rip_ctlinput, rip_ctloutput, 297 rip_usrreq, 298 rip_init, 0, 0, 0, 299 }, 300 }; 301 302 extern struct ifqueue ipintrq; 303 304 struct domain inetdomain = { 305 PF_INET, "internet", 0, 0, 0, 306 inetsw, &inetsw[sizeof(inetsw)/sizeof(inetsw[0])], 307 rn_inithead, 32, sizeof(struct sockaddr_in), 308 #ifdef IPSELSRC 309 in_domifattach, 310 in_domifdetach, 311 #else 312 NULL, 313 NULL, 314 #endif 315 { &ipintrq, NULL }, 316 { NULL }, 317 MOWNER_INIT("",""), 318 }; 319 320 u_char ip_protox[IPPROTO_MAX]; 321 322 int icmperrppslim = 100; /* 100pps */ 323