1 /* $NetBSD: udp6_usrreq.c,v 1.57 2003/08/22 22:05:11 itojun Exp $ */ 2 /* $KAME: udp6_usrreq.c,v 1.86 2001/05/27 17:33:00 itojun 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 * Copyright (c) 1982, 1986, 1989, 1993 35 * The Regents of the University of California. All rights reserved. 36 * 37 * Redistribution and use in source and binary forms, with or without 38 * modification, are permitted provided that the following conditions 39 * are met: 40 * 1. Redistributions of source code must retain the above copyright 41 * notice, this list of conditions and the following disclaimer. 42 * 2. Redistributions in binary form must reproduce the above copyright 43 * notice, this list of conditions and the following disclaimer in the 44 * documentation and/or other materials provided with the distribution. 45 * 3. Neither the name of the University nor the names of its contributors 46 * may be used to endorse or promote products derived from this software 47 * without specific prior written permission. 48 * 49 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 52 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 59 * SUCH DAMAGE. 60 * 61 * @(#)udp_var.h 8.1 (Berkeley) 6/10/93 62 */ 63 64 #include <sys/cdefs.h> 65 __KERNEL_RCSID(0, "$NetBSD: udp6_usrreq.c,v 1.57 2003/08/22 22:05:11 itojun Exp $"); 66 67 #include <sys/param.h> 68 #include <sys/malloc.h> 69 #include <sys/mbuf.h> 70 #include <sys/protosw.h> 71 #include <sys/socket.h> 72 #include <sys/socketvar.h> 73 #include <sys/errno.h> 74 #include <sys/stat.h> 75 #include <sys/systm.h> 76 #include <sys/proc.h> 77 #include <sys/syslog.h> 78 #include <sys/sysctl.h> 79 80 #include <net/if.h> 81 #include <net/route.h> 82 #include <net/if_types.h> 83 84 #include <netinet/in.h> 85 #include <netinet/in_var.h> 86 #include <netinet/in_systm.h> 87 #include <netinet/ip.h> 88 #include <netinet/ip_var.h> 89 #include <netinet/in_pcb.h> 90 #include <netinet/udp.h> 91 #include <netinet/udp_var.h> 92 #include <netinet/ip6.h> 93 #include <netinet6/ip6_var.h> 94 #include <netinet6/in6_pcb.h> 95 #include <netinet/icmp6.h> 96 #include <netinet6/udp6_var.h> 97 #include <netinet6/ip6protosw.h> 98 99 #include "faith.h" 100 #if defined(NFAITH) && NFAITH > 0 101 #include <net/if_faith.h> 102 #endif 103 104 /* 105 * UDP protocol inplementation. 106 * Per RFC 768, August, 1980. 107 */ 108 109 struct in6pcb udb6; 110 struct in6pcb *udp6_last_in6pcb = &udb6; 111 struct udp6stat udp6stat; 112 113 static void udp6_detach __P((struct in6pcb *)); 114 static void udp6_notify __P((struct in6pcb *, int)); 115 116 void 117 udp6_init() 118 { 119 udb6.in6p_next = udb6.in6p_prev = &udb6; 120 } 121 122 /* 123 * Notify a udp user of an asynchronous error; 124 * just wake up so that he can collect error status. 125 */ 126 static void 127 udp6_notify(in6p, errno) 128 struct in6pcb *in6p; 129 int errno; 130 { 131 in6p->in6p_socket->so_error = errno; 132 sorwakeup(in6p->in6p_socket); 133 sowwakeup(in6p->in6p_socket); 134 } 135 136 void 137 udp6_ctlinput(cmd, sa, d) 138 int cmd; 139 struct sockaddr *sa; 140 void *d; 141 { 142 struct udphdr uh; 143 struct ip6_hdr *ip6; 144 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)sa; 145 struct mbuf *m; 146 int off; 147 void *cmdarg; 148 struct ip6ctlparam *ip6cp = NULL; 149 const struct sockaddr_in6 *sa6_src = NULL; 150 void (*notify) __P((struct in6pcb *, int)) = udp6_notify; 151 struct udp_portonly { 152 u_int16_t uh_sport; 153 u_int16_t uh_dport; 154 } *uhp; 155 156 if (sa->sa_family != AF_INET6 || 157 sa->sa_len != sizeof(struct sockaddr_in6)) 158 return; 159 160 if ((unsigned)cmd >= PRC_NCMDS) 161 return; 162 if (PRC_IS_REDIRECT(cmd)) 163 notify = in6_rtchange, d = NULL; 164 else if (cmd == PRC_HOSTDEAD) 165 d = NULL; 166 else if (cmd == PRC_MSGSIZE) { 167 /* special code is present, see below */ 168 notify = in6_rtchange; 169 } 170 else if (inet6ctlerrmap[cmd] == 0) 171 return; 172 173 /* if the parameter is from icmp6, decode it. */ 174 if (d != NULL) { 175 ip6cp = (struct ip6ctlparam *)d; 176 m = ip6cp->ip6c_m; 177 ip6 = ip6cp->ip6c_ip6; 178 off = ip6cp->ip6c_off; 179 cmdarg = ip6cp->ip6c_cmdarg; 180 sa6_src = ip6cp->ip6c_src; 181 } else { 182 m = NULL; 183 ip6 = NULL; 184 cmdarg = NULL; 185 sa6_src = &sa6_any; 186 } 187 188 if (ip6) { 189 /* 190 * XXX: We assume that when IPV6 is non NULL, 191 * M and OFF are valid. 192 */ 193 194 /* check if we can safely examine src and dst ports */ 195 if (m->m_pkthdr.len < off + sizeof(*uhp)) { 196 if (cmd == PRC_MSGSIZE) 197 icmp6_mtudisc_update((struct ip6ctlparam *)d, 0); 198 return; 199 } 200 201 bzero(&uh, sizeof(uh)); 202 m_copydata(m, off, sizeof(*uhp), (caddr_t)&uh); 203 204 if (cmd == PRC_MSGSIZE) { 205 int valid = 0; 206 207 /* 208 * Check to see if we have a valid UDP socket 209 * corresponding to the address in the ICMPv6 message 210 * payload. 211 */ 212 if (in6_pcblookup_connect(&udb6, &sa6->sin6_addr, 213 uh.uh_dport, (struct in6_addr *)&sa6_src->sin6_addr, 214 uh.uh_sport, 0)) 215 valid++; 216 #if 0 217 /* 218 * As the use of sendto(2) is fairly popular, 219 * we may want to allow non-connected pcb too. 220 * But it could be too weak against attacks... 221 * We should at least check if the local address (= s) 222 * is really ours. 223 */ 224 else if (in6_pcblookup_bind(&udb6, &sa6->sin6_addr, 225 uh.uh_dport, 0)) 226 valid++; 227 #endif 228 229 /* 230 * Depending on the value of "valid" and routing table 231 * size (mtudisc_{hi,lo}wat), we will: 232 * - recalculate the new MTU and create the 233 * corresponding routing entry, or 234 * - ignore the MTU change notification. 235 */ 236 icmp6_mtudisc_update((struct ip6ctlparam *)d, valid); 237 238 /* 239 * regardless of if we called icmp6_mtudisc_update(), 240 * we need to call in6_pcbnotify(), to notify path 241 * MTU change to the userland (2292bis-02), because 242 * some unconnected sockets may share the same 243 * destination and want to know the path MTU. 244 */ 245 } 246 247 (void) in6_pcbnotify(&udb6, sa, uh.uh_dport, 248 (struct sockaddr *)sa6_src, uh.uh_sport, cmd, cmdarg, 249 notify); 250 } else { 251 (void) in6_pcbnotify(&udb6, sa, 0, (struct sockaddr *)sa6_src, 252 0, cmd, cmdarg, notify); 253 } 254 } 255 256 extern int udp6_sendspace; 257 extern int udp6_recvspace; 258 259 int 260 udp6_usrreq(so, req, m, addr6, control, p) 261 struct socket *so; 262 int req; 263 struct mbuf *m, *addr6, *control; 264 struct proc *p; 265 { 266 struct in6pcb *in6p = sotoin6pcb(so); 267 int error = 0; 268 int s; 269 270 /* 271 * MAPPED_ADDR implementation info: 272 * Mapped addr support for PRU_CONTROL is not necessary. 273 * Because typical user of PRU_CONTROL is such as ifconfig, 274 * and they don't associate any addr to their socket. Then 275 * socket family is only hint about the PRU_CONTROL'ed address 276 * family, especially when getting addrs from kernel. 277 * So AF_INET socket need to be used to control AF_INET addrs, 278 * and AF_INET6 socket for AF_INET6 addrs. 279 */ 280 if (req == PRU_CONTROL) 281 return (in6_control(so, (u_long)m, (caddr_t)addr6, 282 (struct ifnet *)control, p)); 283 284 if (req == PRU_PURGEIF) { 285 in6_pcbpurgeif0(&udb6, (struct ifnet *)control); 286 in6_purgeif((struct ifnet *)control); 287 in6_pcbpurgeif(&udb6, (struct ifnet *)control); 288 return (0); 289 } 290 291 if (in6p == NULL && req != PRU_ATTACH) { 292 error = EINVAL; 293 goto release; 294 } 295 296 switch (req) { 297 case PRU_ATTACH: 298 /* 299 * MAPPED_ADDR implementation spec: 300 * Always attach for IPv6, 301 * and only when necessary for IPv4. 302 */ 303 if (in6p != NULL) { 304 error = EINVAL; 305 break; 306 } 307 s = splsoftnet(); 308 error = in6_pcballoc(so, &udb6); 309 splx(s); 310 if (error) 311 break; 312 error = soreserve(so, udp6_sendspace, udp6_recvspace); 313 if (error) 314 break; 315 in6p = sotoin6pcb(so); 316 in6p->in6p_cksum = -1; /* just to be sure */ 317 break; 318 319 case PRU_DETACH: 320 udp6_detach(in6p); 321 break; 322 323 case PRU_BIND: 324 s = splsoftnet(); 325 error = in6_pcbbind(in6p, addr6, p); 326 splx(s); 327 break; 328 329 case PRU_LISTEN: 330 error = EOPNOTSUPP; 331 break; 332 333 case PRU_CONNECT: 334 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) { 335 error = EISCONN; 336 break; 337 } 338 s = splsoftnet(); 339 error = in6_pcbconnect(in6p, addr6); 340 if (ip6_auto_flowlabel) { 341 in6p->in6p_flowinfo &= ~IPV6_FLOWLABEL_MASK; 342 in6p->in6p_flowinfo |= 343 (htonl(ip6_flow_seq++) & IPV6_FLOWLABEL_MASK); 344 } 345 splx(s); 346 if (error == 0) 347 soisconnected(so); 348 break; 349 350 case PRU_CONNECT2: 351 error = EOPNOTSUPP; 352 break; 353 354 case PRU_ACCEPT: 355 error = EOPNOTSUPP; 356 break; 357 358 case PRU_DISCONNECT: 359 if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) { 360 error = ENOTCONN; 361 break; 362 } 363 s = splsoftnet(); 364 in6_pcbdisconnect(in6p); 365 bzero((caddr_t)&in6p->in6p_laddr, sizeof(in6p->in6p_laddr)); 366 splx(s); 367 so->so_state &= ~SS_ISCONNECTED; /* XXX */ 368 break; 369 370 case PRU_SHUTDOWN: 371 socantsendmore(so); 372 break; 373 374 case PRU_SEND: 375 return (udp6_output(in6p, m, addr6, control, p)); 376 377 case PRU_ABORT: 378 soisdisconnected(so); 379 udp6_detach(in6p); 380 break; 381 382 case PRU_SOCKADDR: 383 in6_setsockaddr(in6p, addr6); 384 break; 385 386 case PRU_PEERADDR: 387 in6_setpeeraddr(in6p, addr6); 388 break; 389 390 case PRU_SENSE: 391 /* 392 * stat: don't bother with a blocksize 393 */ 394 return (0); 395 396 case PRU_SENDOOB: 397 case PRU_FASTTIMO: 398 case PRU_SLOWTIMO: 399 case PRU_PROTORCV: 400 case PRU_PROTOSEND: 401 error = EOPNOTSUPP; 402 break; 403 404 case PRU_RCVD: 405 case PRU_RCVOOB: 406 return (EOPNOTSUPP); /* do not free mbuf's */ 407 408 default: 409 panic("udp6_usrreq"); 410 } 411 412 release: 413 if (control) 414 m_freem(control); 415 if (m) 416 m_freem(m); 417 return (error); 418 } 419 420 static void 421 udp6_detach(in6p) 422 struct in6pcb *in6p; 423 { 424 int s = splsoftnet(); 425 426 if (in6p == udp6_last_in6pcb) 427 udp6_last_in6pcb = &udb6; 428 in6_pcbdetach(in6p); 429 splx(s); 430 } 431 432 int 433 udp6_sysctl(name, namelen, oldp, oldlenp, newp, newlen) 434 int *name; 435 u_int namelen; 436 void *oldp; 437 size_t *oldlenp; 438 void *newp; 439 size_t newlen; 440 { 441 /* All sysctl names at this level are terminal. */ 442 if (namelen != 1) 443 return ENOTDIR; 444 445 switch (name[0]) { 446 447 case UDP6CTL_SENDSPACE: 448 return sysctl_int(oldp, oldlenp, newp, newlen, 449 &udp6_sendspace); 450 case UDP6CTL_RECVSPACE: 451 return sysctl_int(oldp, oldlenp, newp, newlen, 452 &udp6_recvspace); 453 default: 454 return ENOPROTOOPT; 455 } 456 /* NOTREACHED */ 457 } 458