1 /* $OpenBSD: raw_ip.c,v 1.104 2017/10/06 21:14:55 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 int 119 rip_input(struct mbuf **mp, int *offp, int proto, int af) 120 { 121 struct mbuf *m = *mp; 122 struct ip *ip = mtod(m, struct ip *); 123 struct inpcb *inp, *last = NULL; 124 struct mbuf *opts = NULL; 125 struct counters_ref ref; 126 uint64_t *counters; 127 128 KASSERT(af == AF_INET); 129 130 ripsrc.sin_addr = ip->ip_src; 131 TAILQ_FOREACH(inp, &rawcbtable.inpt_queue, inp_queue) { 132 if (inp->inp_socket->so_state & SS_CANTRCVMORE) 133 continue; 134 #ifdef INET6 135 if (inp->inp_flags & INP_IPV6) 136 continue; 137 #endif 138 if (rtable_l2(inp->inp_rtableid) != 139 rtable_l2(m->m_pkthdr.ph_rtableid)) 140 continue; 141 142 if (inp->inp_ip.ip_p && inp->inp_ip.ip_p != ip->ip_p) 143 continue; 144 #if NPF > 0 145 if (m->m_pkthdr.pf.flags & PF_TAG_DIVERTED) { 146 struct pf_divert *divert; 147 148 /* XXX rdomain support */ 149 if ((divert = pf_find_divert(m)) == NULL) 150 continue; 151 if (!divert->addr.v4.s_addr) 152 goto divert_reply; 153 if (inp->inp_laddr.s_addr != divert->addr.v4.s_addr) 154 continue; 155 } else 156 divert_reply: 157 #endif 158 if (inp->inp_laddr.s_addr && 159 inp->inp_laddr.s_addr != ip->ip_dst.s_addr) 160 continue; 161 if (inp->inp_faddr.s_addr && 162 inp->inp_faddr.s_addr != ip->ip_src.s_addr) 163 continue; 164 if (last) { 165 struct mbuf *n; 166 167 if ((n = m_copym(m, 0, M_COPYALL, M_NOWAIT)) != NULL) { 168 if (last->inp_flags & INP_CONTROLOPTS || 169 last->inp_socket->so_options & SO_TIMESTAMP) 170 ip_savecontrol(last, &opts, ip, n); 171 if (sbappendaddr(last->inp_socket, 172 &last->inp_socket->so_rcv, 173 sintosa(&ripsrc), n, opts) == 0) { 174 /* should notify about lost packet */ 175 m_freem(n); 176 m_freem(opts); 177 } else 178 sorwakeup(last->inp_socket); 179 opts = NULL; 180 } 181 } 182 last = inp; 183 } 184 if (last) { 185 if (last->inp_flags & INP_CONTROLOPTS || 186 last->inp_socket->so_options & SO_TIMESTAMP) 187 ip_savecontrol(last, &opts, ip, m); 188 if (sbappendaddr(last->inp_socket, &last->inp_socket->so_rcv, 189 sintosa(&ripsrc), m, opts) == 0) { 190 m_freem(m); 191 m_freem(opts); 192 } else 193 sorwakeup(last->inp_socket); 194 } else { 195 if (ip->ip_p != IPPROTO_ICMP) 196 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PROTOCOL, 0, 0); 197 else 198 m_freem(m); 199 200 counters = counters_enter(&ref, ipcounters); 201 counters[ips_noproto]++; 202 counters[ips_delivered]--; 203 counters_leave(&ref, ipcounters); 204 } 205 return IPPROTO_DONE; 206 } 207 208 /* 209 * Generate IP header and pass packet to ip_output. 210 * Tack on options user may have setup with control call. 211 */ 212 int 213 rip_output(struct mbuf *m, struct socket *so, struct sockaddr *dstaddr, 214 struct mbuf *control) 215 { 216 struct ip *ip; 217 struct inpcb *inp; 218 int flags, error; 219 220 inp = sotoinpcb(so); 221 flags = IP_ALLOWBROADCAST; 222 223 /* 224 * If the user handed us a complete IP packet, use it. 225 * Otherwise, allocate an mbuf for a header and fill it in. 226 */ 227 if ((inp->inp_flags & INP_HDRINCL) == 0) { 228 if ((m->m_pkthdr.len + sizeof(struct ip)) > IP_MAXPACKET) { 229 m_freem(m); 230 return (EMSGSIZE); 231 } 232 M_PREPEND(m, sizeof(struct ip), M_DONTWAIT); 233 if (!m) 234 return (ENOBUFS); 235 ip = mtod(m, struct ip *); 236 ip->ip_tos = inp->inp_ip.ip_tos; 237 ip->ip_off = htons(0); 238 ip->ip_p = inp->inp_ip.ip_p; 239 ip->ip_len = htons(m->m_pkthdr.len); 240 ip->ip_src = inp->inp_laddr; 241 ip->ip_dst = satosin(dstaddr)->sin_addr; 242 ip->ip_ttl = inp->inp_ip.ip_ttl ? inp->inp_ip.ip_ttl : MAXTTL; 243 } else { 244 if (m->m_pkthdr.len > IP_MAXPACKET) { 245 m_freem(m); 246 return (EMSGSIZE); 247 } 248 if (m->m_pkthdr.len < sizeof(struct ip)) { 249 m_freem(m); 250 return (EINVAL); 251 } 252 ip = mtod(m, struct ip *); 253 /* 254 * don't allow both user specified and setsockopt options, 255 * and don't allow packet length sizes that will crash 256 */ 257 if ((ip->ip_hl != (sizeof (*ip) >> 2) && inp->inp_options) || 258 ntohs(ip->ip_len) > m->m_pkthdr.len || 259 ntohs(ip->ip_len) < ip->ip_hl << 2) { 260 m_freem(m); 261 return (EINVAL); 262 } 263 if (ip->ip_id == 0) { 264 ip->ip_id = htons(ip_randomid()); 265 } 266 /* XXX prevent ip_output from overwriting header fields */ 267 flags |= IP_RAWOUTPUT; 268 ipstat_inc(ips_rawout); 269 } 270 #ifdef INET6 271 /* 272 * A thought: Even though raw IP shouldn't be able to set IPv6 273 * multicast options, if it does, the last parameter to 274 * ip_output should be guarded against v6/v4 problems. 275 */ 276 #endif 277 /* force routing table */ 278 m->m_pkthdr.ph_rtableid = inp->inp_rtableid; 279 280 #if NPF > 0 281 if (inp->inp_socket->so_state & SS_ISCONNECTED && 282 ip->ip_p != IPPROTO_ICMP) 283 m->m_pkthdr.pf.inp = inp; 284 #endif 285 286 error = ip_output(m, inp->inp_options, &inp->inp_route, flags, 287 inp->inp_moptions, inp, 0); 288 if (error == EACCES) /* translate pf(4) error for userland */ 289 error = EHOSTUNREACH; 290 return (error); 291 } 292 293 /* 294 * Raw IP socket option processing. 295 */ 296 int 297 rip_ctloutput(int op, struct socket *so, int level, int optname, 298 struct mbuf *m) 299 { 300 struct inpcb *inp = sotoinpcb(so); 301 int error; 302 303 if (level != IPPROTO_IP) 304 return (EINVAL); 305 306 switch (optname) { 307 308 case IP_HDRINCL: 309 error = 0; 310 if (op == PRCO_SETOPT) { 311 if (m == NULL || m->m_len < sizeof (int)) 312 error = EINVAL; 313 else if (*mtod(m, int *)) 314 inp->inp_flags |= INP_HDRINCL; 315 else 316 inp->inp_flags &= ~INP_HDRINCL; 317 } else { 318 m->m_len = sizeof(int); 319 *mtod(m, int *) = inp->inp_flags & INP_HDRINCL; 320 } 321 return (error); 322 323 case MRT_INIT: 324 case MRT_DONE: 325 case MRT_ADD_VIF: 326 case MRT_DEL_VIF: 327 case MRT_ADD_MFC: 328 case MRT_DEL_MFC: 329 case MRT_VERSION: 330 case MRT_ASSERT: 331 case MRT_API_SUPPORT: 332 case MRT_API_CONFIG: 333 #ifdef MROUTING 334 switch (op) { 335 case PRCO_SETOPT: 336 error = ip_mrouter_set(so, optname, m); 337 break; 338 case PRCO_GETOPT: 339 error = ip_mrouter_get(so, optname, m); 340 break; 341 default: 342 error = EINVAL; 343 break; 344 } 345 return (error); 346 #else 347 return (EOPNOTSUPP); 348 #endif 349 } 350 return (ip_ctloutput(op, so, level, optname, m)); 351 } 352 353 u_long rip_sendspace = RIPSNDQ; 354 u_long rip_recvspace = RIPRCVQ; 355 356 /*ARGSUSED*/ 357 int 358 rip_usrreq(struct socket *so, int req, struct mbuf *m, struct mbuf *nam, 359 struct mbuf *control, struct proc *p) 360 { 361 struct inpcb *inp = sotoinpcb(so); 362 int error = 0; 363 364 soassertlocked(so); 365 366 if (req == PRU_CONTROL) 367 return (in_control(so, (u_long)m, (caddr_t)nam, 368 (struct ifnet *)control)); 369 370 if (inp == NULL) { 371 error = EINVAL; 372 goto release; 373 } 374 375 switch (req) { 376 377 case PRU_DISCONNECT: 378 if ((so->so_state & SS_ISCONNECTED) == 0) { 379 error = ENOTCONN; 380 break; 381 } 382 /* FALLTHROUGH */ 383 case PRU_ABORT: 384 soisdisconnected(so); 385 /* FALLTHROUGH */ 386 case PRU_DETACH: 387 if (inp == NULL) 388 panic("rip_detach"); 389 #ifdef MROUTING 390 if (so == ip_mrouter[inp->inp_rtableid]) 391 ip_mrouter_done(so); 392 #endif 393 in_pcbdetach(inp); 394 break; 395 396 case PRU_BIND: 397 { 398 struct sockaddr_in *addr; 399 400 if ((error = in_nam2sin(nam, &addr))) 401 break; 402 if (!((so->so_options & SO_BINDANY) || 403 addr->sin_addr.s_addr == INADDR_ANY || 404 addr->sin_addr.s_addr == INADDR_BROADCAST || 405 in_broadcast(addr->sin_addr, inp->inp_rtableid) || 406 ifa_ifwithaddr(sintosa(addr), inp->inp_rtableid))) { 407 error = EADDRNOTAVAIL; 408 break; 409 } 410 inp->inp_laddr = addr->sin_addr; 411 break; 412 } 413 case PRU_CONNECT: 414 { 415 struct sockaddr_in *addr; 416 417 if ((error = in_nam2sin(nam, &addr))) 418 break; 419 inp->inp_faddr = addr->sin_addr; 420 soisconnected(so); 421 break; 422 } 423 424 case PRU_CONNECT2: 425 error = EOPNOTSUPP; 426 break; 427 428 /* 429 * Mark the connection as being incapable of further input. 430 */ 431 case PRU_SHUTDOWN: 432 socantsendmore(so); 433 break; 434 435 /* 436 * Ship a packet out. The appropriate raw output 437 * routine handles any massaging necessary. 438 */ 439 case PRU_SEND: 440 { 441 struct sockaddr_in dst; 442 443 memset(&dst, 0, sizeof(dst)); 444 dst.sin_family = AF_INET; 445 dst.sin_len = sizeof(dst); 446 if (so->so_state & SS_ISCONNECTED) { 447 if (nam) { 448 error = EISCONN; 449 break; 450 } 451 dst.sin_addr = inp->inp_faddr; 452 } else { 453 struct sockaddr_in *addr; 454 455 if (nam == NULL) { 456 error = ENOTCONN; 457 break; 458 } 459 if ((error = in_nam2sin(nam, &addr))) 460 break; 461 dst.sin_addr = addr->sin_addr; 462 } 463 #ifdef IPSEC 464 /* XXX Find an IPsec TDB */ 465 #endif 466 error = rip_output(m, so, sintosa(&dst), NULL); 467 m = NULL; 468 break; 469 } 470 471 case PRU_SENSE: 472 /* 473 * stat: don't bother with a blocksize. 474 */ 475 return (0); 476 477 /* 478 * Not supported. 479 */ 480 case PRU_RCVOOB: 481 case PRU_RCVD: 482 case PRU_LISTEN: 483 case PRU_ACCEPT: 484 case PRU_SENDOOB: 485 error = EOPNOTSUPP; 486 break; 487 488 case PRU_SOCKADDR: 489 in_setsockaddr(inp, nam); 490 break; 491 492 case PRU_PEERADDR: 493 in_setpeeraddr(inp, nam); 494 break; 495 496 default: 497 panic("rip_usrreq"); 498 } 499 release: 500 m_freem(m); 501 return (error); 502 } 503 504 int 505 rip_attach(struct socket *so, int proto) 506 { 507 struct inpcb *inp; 508 int error; 509 510 if (so->so_pcb) 511 panic("rip_attach"); 512 if ((so->so_state & SS_PRIV) == 0) 513 return EACCES; 514 if (proto < 0 || proto >= IPPROTO_MAX) 515 return EPROTONOSUPPORT; 516 517 if ((error = soreserve(so, rip_sendspace, rip_recvspace)) || 518 (error = in_pcballoc(so, &rawcbtable))) { 519 return error; 520 } 521 inp = sotoinpcb(so); 522 inp->inp_ip.ip_p = proto; 523 return 0; 524 } 525