1 /* $OpenBSD: raw_ip.c,v 1.92 2017/01/23 16:31:24 bluhm Exp $ */ 2 /* $NetBSD: raw_ip.c,v 1.25 1996/02/18 18:58:33 christos Exp $ */ 3 4 /* 5 * Copyright (c) 1982, 1986, 1988, 1993 6 * The Regents of the University of California. 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 University 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 REGENTS 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 REGENTS 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 * @(#)COPYRIGHT 1.1 (NRL) 17 January 1995 33 * 34 * NRL grants permission for redistribution and use in source and binary 35 * forms, with or without modification, of the software and documentation 36 * created at NRL provided that the following conditions are met: 37 * 38 * 1. Redistributions of source code must retain the above copyright 39 * notice, this list of conditions and the following disclaimer. 40 * 2. Redistributions in binary form must reproduce the above copyright 41 * notice, this list of conditions and the following disclaimer in the 42 * documentation and/or other materials provided with the distribution. 43 * 3. All advertising materials mentioning features or use of this software 44 * must display the following acknowledgements: 45 * This product includes software developed by the University of 46 * California, Berkeley and its contributors. 47 * This product includes software developed at the Information 48 * Technology Division, US Naval Research Laboratory. 49 * 4. Neither the name of the NRL nor the names of its contributors 50 * may be used to endorse or promote products derived from this software 51 * without specific prior written permission. 52 * 53 * THE SOFTWARE PROVIDED BY NRL IS PROVIDED BY NRL AND CONTRIBUTORS ``AS 54 * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 55 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 56 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL NRL OR 57 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 58 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 59 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 60 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 61 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 62 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 63 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 64 * 65 * The views and conclusions contained in the software and documentation 66 * are those of the authors and should not be interpreted as representing 67 * official policies, either expressed or implied, of the US Naval 68 * Research Laboratory (NRL). 69 */ 70 71 #include <sys/param.h> 72 #include <sys/systm.h> 73 #include <sys/mbuf.h> 74 #include <sys/socket.h> 75 #include <sys/protosw.h> 76 #include <sys/socketvar.h> 77 78 #include <net/if.h> 79 #include <net/if_var.h> 80 #include <net/route.h> 81 82 #include <netinet/in.h> 83 #include <netinet/ip.h> 84 #include <netinet/ip_mroute.h> 85 #include <netinet/ip_var.h> 86 #include <netinet/in_pcb.h> 87 #include <netinet/in_var.h> 88 #include <netinet/ip_icmp.h> 89 90 #include <net/pfvar.h> 91 92 #include "pf.h" 93 94 struct inpcbtable rawcbtable; 95 96 /* 97 * Nominal space allocated to a raw ip socket. 98 */ 99 #define RIPSNDQ 8192 100 #define RIPRCVQ 8192 101 102 /* 103 * Raw interface to IP protocol. 104 */ 105 106 /* 107 * Initialize raw connection block q. 108 */ 109 void 110 rip_init(void) 111 { 112 113 in_pcbinit(&rawcbtable, 1); 114 } 115 116 struct sockaddr_in ripsrc = { sizeof(ripsrc), AF_INET }; 117 118 void 119 rip_input(struct mbuf *m, ...) 120 { 121 struct ip *ip = mtod(m, struct ip *); 122 struct inpcb *inp, *last = NULL; 123 struct mbuf *opts = NULL; 124 struct counters_ref ref; 125 uint64_t *counters; 126 127 ripsrc.sin_addr = ip->ip_src; 128 TAILQ_FOREACH(inp, &rawcbtable.inpt_queue, inp_queue) { 129 if (inp->inp_socket->so_state & SS_CANTRCVMORE) 130 continue; 131 #ifdef INET6 132 if (inp->inp_flags & INP_IPV6) 133 continue; 134 #endif 135 if (rtable_l2(inp->inp_rtableid) != 136 rtable_l2(m->m_pkthdr.ph_rtableid)) 137 continue; 138 139 if (inp->inp_ip.ip_p && inp->inp_ip.ip_p != ip->ip_p) 140 continue; 141 #if NPF > 0 142 if (m->m_pkthdr.pf.flags & PF_TAG_DIVERTED) { 143 struct pf_divert *divert; 144 145 /* XXX rdomain support */ 146 if ((divert = pf_find_divert(m)) == NULL) 147 continue; 148 if (!divert->addr.v4.s_addr) 149 goto divert_reply; 150 if (inp->inp_laddr.s_addr != divert->addr.v4.s_addr) 151 continue; 152 } else 153 divert_reply: 154 #endif 155 if (inp->inp_laddr.s_addr && 156 inp->inp_laddr.s_addr != ip->ip_dst.s_addr) 157 continue; 158 if (inp->inp_faddr.s_addr && 159 inp->inp_faddr.s_addr != ip->ip_src.s_addr) 160 continue; 161 if (last) { 162 struct mbuf *n; 163 164 if ((n = m_copym(m, 0, M_COPYALL, M_NOWAIT)) != NULL) { 165 if (last->inp_flags & INP_CONTROLOPTS || 166 last->inp_socket->so_options & SO_TIMESTAMP) 167 ip_savecontrol(last, &opts, ip, n); 168 if (sbappendaddr(&last->inp_socket->so_rcv, 169 sintosa(&ripsrc), n, opts) == 0) { 170 /* should notify about lost packet */ 171 m_freem(n); 172 m_freem(opts); 173 } else 174 sorwakeup(last->inp_socket); 175 opts = NULL; 176 } 177 } 178 last = inp; 179 } 180 if (last) { 181 if (last->inp_flags & INP_CONTROLOPTS || 182 last->inp_socket->so_options & SO_TIMESTAMP) 183 ip_savecontrol(last, &opts, ip, m); 184 if (sbappendaddr(&last->inp_socket->so_rcv, sintosa(&ripsrc), m, 185 opts) == 0) { 186 m_freem(m); 187 m_freem(opts); 188 } else 189 sorwakeup(last->inp_socket); 190 } else { 191 if (ip->ip_p != IPPROTO_ICMP) 192 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PROTOCOL, 0, 0); 193 else 194 m_freem(m); 195 196 counters = counters_enter(&ref, ipcounters); 197 counters[ips_noproto]++; 198 counters[ips_delivered]--; 199 counters_leave(&ref, ipcounters); 200 } 201 } 202 203 /* 204 * Generate IP header and pass packet to ip_output. 205 * Tack on options user may have setup with control call. 206 */ 207 int 208 rip_output(struct mbuf *m, ...) 209 { 210 struct socket *so; 211 u_long dst; 212 struct ip *ip; 213 struct inpcb *inp; 214 int flags, error; 215 va_list ap; 216 217 va_start(ap, m); 218 so = va_arg(ap, struct socket *); 219 dst = va_arg(ap, u_long); 220 va_end(ap); 221 222 inp = sotoinpcb(so); 223 flags = IP_ALLOWBROADCAST; 224 225 /* 226 * If the user handed us a complete IP packet, use it. 227 * Otherwise, allocate an mbuf for a header and fill it in. 228 */ 229 if ((inp->inp_flags & INP_HDRINCL) == 0) { 230 if ((m->m_pkthdr.len + sizeof(struct ip)) > IP_MAXPACKET) { 231 m_freem(m); 232 return (EMSGSIZE); 233 } 234 M_PREPEND(m, sizeof(struct ip), M_DONTWAIT); 235 if (!m) 236 return (ENOBUFS); 237 ip = mtod(m, struct ip *); 238 ip->ip_tos = inp->inp_ip.ip_tos; 239 ip->ip_off = htons(0); 240 ip->ip_p = inp->inp_ip.ip_p; 241 ip->ip_len = htons(m->m_pkthdr.len); 242 ip->ip_src = inp->inp_laddr; 243 ip->ip_dst.s_addr = dst; 244 ip->ip_ttl = inp->inp_ip.ip_ttl ? inp->inp_ip.ip_ttl : MAXTTL; 245 } else { 246 if (m->m_pkthdr.len > IP_MAXPACKET) { 247 m_freem(m); 248 return (EMSGSIZE); 249 } 250 if (m->m_pkthdr.len < sizeof(struct ip)) { 251 m_freem(m); 252 return (EINVAL); 253 } 254 ip = mtod(m, struct ip *); 255 /* 256 * don't allow both user specified and setsockopt options, 257 * and don't allow packet length sizes that will crash 258 */ 259 if ((ip->ip_hl != (sizeof (*ip) >> 2) && inp->inp_options) || 260 ntohs(ip->ip_len) > m->m_pkthdr.len || 261 ntohs(ip->ip_len) < ip->ip_hl << 2) { 262 m_freem(m); 263 return (EINVAL); 264 } 265 if (ip->ip_id == 0) { 266 ip->ip_id = htons(ip_randomid()); 267 } 268 /* XXX prevent ip_output from overwriting header fields */ 269 flags |= IP_RAWOUTPUT; 270 ipstat_inc(ips_rawout); 271 } 272 #ifdef INET6 273 /* 274 * A thought: Even though raw IP shouldn't be able to set IPv6 275 * multicast options, if it does, the last parameter to 276 * ip_output should be guarded against v6/v4 problems. 277 */ 278 #endif 279 /* force routing table */ 280 m->m_pkthdr.ph_rtableid = inp->inp_rtableid; 281 282 #if NPF > 0 283 if (inp->inp_socket->so_state & SS_ISCONNECTED && 284 ip->ip_p != IPPROTO_ICMP) 285 m->m_pkthdr.pf.inp = inp; 286 #endif 287 288 error = ip_output(m, inp->inp_options, &inp->inp_route, flags, 289 inp->inp_moptions, inp, 0); 290 if (error == EACCES) /* translate pf(4) error for userland */ 291 error = EHOSTUNREACH; 292 return (error); 293 } 294 295 /* 296 * Raw IP socket option processing. 297 */ 298 int 299 rip_ctloutput(int op, struct socket *so, int level, int optname, 300 struct mbuf **mp) 301 { 302 struct inpcb *inp = sotoinpcb(so); 303 int error = 0; 304 int dir; 305 306 if (level != IPPROTO_IP) { 307 if (op == PRCO_SETOPT) 308 (void) m_free(*mp); 309 return (EINVAL); 310 } 311 312 switch (optname) { 313 314 case IP_HDRINCL: 315 error = 0; 316 if (op == PRCO_SETOPT) { 317 if (*mp == NULL || (*mp)->m_len < sizeof (int)) 318 error = EINVAL; 319 else if (*mtod(*mp, int *)) 320 inp->inp_flags |= INP_HDRINCL; 321 else 322 inp->inp_flags &= ~INP_HDRINCL; 323 m_free(*mp); 324 } else { 325 *mp = m_get(M_WAIT, M_SOOPTS); 326 (*mp)->m_len = sizeof(int); 327 *mtod(*mp, int *) = inp->inp_flags & INP_HDRINCL; 328 } 329 return (error); 330 331 case IP_DIVERTFL: 332 switch (op) { 333 case PRCO_SETOPT: 334 if (*mp == NULL || (*mp)->m_len < sizeof (int)) { 335 error = EINVAL; 336 break; 337 } 338 dir = *mtod(*mp, int *); 339 if (inp->inp_divertfl > 0) 340 error = ENOTSUP; 341 else if ((dir & IPPROTO_DIVERT_RESP) || 342 (dir & IPPROTO_DIVERT_INIT)) 343 inp->inp_divertfl = dir; 344 else 345 error = EINVAL; 346 347 break; 348 349 case PRCO_GETOPT: 350 *mp = m_get(M_WAIT, M_SOOPTS); 351 (*mp)->m_len = sizeof(int); 352 *mtod(*mp, int *) = inp->inp_divertfl; 353 break; 354 355 default: 356 error = EINVAL; 357 break; 358 } 359 360 if (op == PRCO_SETOPT) 361 (void)m_free(*mp); 362 return (error); 363 364 case MRT_INIT: 365 case MRT_DONE: 366 case MRT_ADD_VIF: 367 case MRT_DEL_VIF: 368 case MRT_ADD_MFC: 369 case MRT_DEL_MFC: 370 case MRT_VERSION: 371 case MRT_ASSERT: 372 case MRT_API_SUPPORT: 373 case MRT_API_CONFIG: 374 #ifdef MROUTING 375 switch (op) { 376 case PRCO_SETOPT: 377 error = ip_mrouter_set(so, optname, mp); 378 break; 379 case PRCO_GETOPT: 380 error = ip_mrouter_get(so, optname, mp); 381 break; 382 default: 383 error = EINVAL; 384 break; 385 } 386 return (error); 387 #else 388 if (op == PRCO_SETOPT) 389 m_free(*mp); 390 return (EOPNOTSUPP); 391 #endif 392 } 393 return (ip_ctloutput(op, so, level, optname, mp)); 394 } 395 396 u_long rip_sendspace = RIPSNDQ; 397 u_long rip_recvspace = RIPRCVQ; 398 399 /*ARGSUSED*/ 400 int 401 rip_usrreq(struct socket *so, int req, struct mbuf *m, struct mbuf *nam, 402 struct mbuf *control, struct proc *p) 403 { 404 struct inpcb *inp = sotoinpcb(so); 405 int error = 0; 406 407 NET_ASSERT_LOCKED(); 408 409 if (req == PRU_CONTROL) 410 return (in_control(so, (u_long)m, (caddr_t)nam, 411 (struct ifnet *)control)); 412 413 if (inp == NULL && req != PRU_ATTACH) { 414 error = EINVAL; 415 goto release; 416 } 417 418 switch (req) { 419 420 case PRU_ATTACH: 421 if (inp) 422 panic("rip_attach"); 423 if ((so->so_state & SS_PRIV) == 0) { 424 error = EACCES; 425 break; 426 } 427 if ((long)nam < 0 || (long)nam >= IPPROTO_MAX) { 428 error = EPROTONOSUPPORT; 429 break; 430 } 431 if ((error = soreserve(so, rip_sendspace, rip_recvspace)) || 432 (error = in_pcballoc(so, &rawcbtable))) { 433 break; 434 } 435 inp = sotoinpcb(so); 436 inp->inp_ip.ip_p = (long)nam; 437 break; 438 439 case PRU_DISCONNECT: 440 if ((so->so_state & SS_ISCONNECTED) == 0) { 441 error = ENOTCONN; 442 break; 443 } 444 /* FALLTHROUGH */ 445 case PRU_ABORT: 446 soisdisconnected(so); 447 /* FALLTHROUGH */ 448 case PRU_DETACH: 449 if (inp == NULL) 450 panic("rip_detach"); 451 #ifdef MROUTING 452 if (so == ip_mrouter[inp->inp_rtableid]) 453 ip_mrouter_done(so); 454 #endif 455 in_pcbdetach(inp); 456 break; 457 458 case PRU_BIND: 459 { 460 struct sockaddr_in *addr = mtod(nam, struct sockaddr_in *); 461 462 if (nam->m_len != sizeof(*addr)) { 463 error = EINVAL; 464 break; 465 } 466 if (addr->sin_family != AF_INET) { 467 error = EADDRNOTAVAIL; 468 break; 469 } 470 if (!((so->so_options & SO_BINDANY) || 471 addr->sin_addr.s_addr == INADDR_ANY || 472 addr->sin_addr.s_addr == INADDR_BROADCAST || 473 in_broadcast(addr->sin_addr, inp->inp_rtableid) || 474 ifa_ifwithaddr(sintosa(addr), inp->inp_rtableid))) { 475 error = EADDRNOTAVAIL; 476 break; 477 } 478 inp->inp_laddr = addr->sin_addr; 479 break; 480 } 481 case PRU_CONNECT: 482 { 483 struct sockaddr_in *addr = mtod(nam, struct sockaddr_in *); 484 485 if (nam->m_len != sizeof(*addr)) { 486 error = EINVAL; 487 break; 488 } 489 if (addr->sin_family != AF_INET) { 490 error = EAFNOSUPPORT; 491 break; 492 } 493 inp->inp_faddr = addr->sin_addr; 494 soisconnected(so); 495 break; 496 } 497 498 case PRU_CONNECT2: 499 error = EOPNOTSUPP; 500 break; 501 502 /* 503 * Mark the connection as being incapable of further input. 504 */ 505 case PRU_SHUTDOWN: 506 socantsendmore(so); 507 break; 508 509 /* 510 * Ship a packet out. The appropriate raw output 511 * routine handles any massaging necessary. 512 */ 513 case PRU_SEND: 514 { 515 u_int32_t dst; 516 517 if (so->so_state & SS_ISCONNECTED) { 518 if (nam) { 519 error = EISCONN; 520 break; 521 } 522 dst = inp->inp_faddr.s_addr; 523 } else { 524 if (nam == NULL) { 525 error = ENOTCONN; 526 break; 527 } 528 dst = mtod(nam, struct sockaddr_in *)->sin_addr.s_addr; 529 } 530 #ifdef IPSEC 531 /* XXX Find an IPsec TDB */ 532 #endif 533 error = rip_output(m, so, dst); 534 m = NULL; 535 break; 536 } 537 538 case PRU_SENSE: 539 /* 540 * stat: don't bother with a blocksize. 541 */ 542 return (0); 543 544 /* 545 * Not supported. 546 */ 547 case PRU_RCVOOB: 548 case PRU_RCVD: 549 case PRU_LISTEN: 550 case PRU_ACCEPT: 551 case PRU_SENDOOB: 552 error = EOPNOTSUPP; 553 break; 554 555 case PRU_SOCKADDR: 556 in_setsockaddr(inp, nam); 557 break; 558 559 case PRU_PEERADDR: 560 in_setpeeraddr(inp, nam); 561 break; 562 563 default: 564 panic("rip_usrreq"); 565 } 566 release: 567 m_freem(m); 568 return (error); 569 } 570