1 /* 2 * Copyright (c) 1982, 1986, 1988, 1993 3 * The Regents of the University of California. 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 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 * 33 * @(#)raw_ip.c 8.7 (Berkeley) 5/15/95 34 * $Id: raw_ip.c,v 1.45 1997/04/27 20:01:10 wollman Exp $ 35 */ 36 37 #include <sys/param.h> 38 #include <sys/systm.h> 39 #include <sys/kernel.h> 40 #include <sys/errno.h> 41 #include <sys/malloc.h> 42 #include <sys/mbuf.h> 43 #include <sys/proc.h> 44 #include <sys/protosw.h> 45 #include <sys/queue.h> 46 #include <sys/socket.h> 47 #include <sys/socketvar.h> 48 #include <sys/sysctl.h> 49 50 #include <net/if.h> 51 #include <net/route.h> 52 53 #define _IP_VHL 54 #include <netinet/in.h> 55 #include <netinet/in_systm.h> 56 #include <netinet/ip.h> 57 #include <netinet/in_pcb.h> 58 #include <netinet/in_var.h> 59 #include <netinet/ip_var.h> 60 #include <netinet/ip_mroute.h> 61 62 #include <netinet/ip_fw.h> 63 64 #if !defined(COMPAT_IPFW) || COMPAT_IPFW == 1 65 #undef COMPAT_IPFW 66 #define COMPAT_IPFW 1 67 #else 68 #undef COMPAT_IPFW 69 #endif 70 71 static struct inpcbhead ripcb; 72 static struct inpcbinfo ripcbinfo; 73 74 /* 75 * Nominal space allocated to a raw ip socket. 76 */ 77 #define RIPSNDQ 8192 78 #define RIPRCVQ 8192 79 80 /* 81 * Raw interface to IP protocol. 82 */ 83 84 /* 85 * Initialize raw connection block q. 86 */ 87 void 88 rip_init() 89 { 90 LIST_INIT(&ripcb); 91 ripcbinfo.listhead = &ripcb; 92 /* 93 * XXX We don't use the hash list for raw IP, but it's easier 94 * to allocate a one entry hash list than it is to check all 95 * over the place for hashbase == NULL. 96 */ 97 ripcbinfo.hashbase = hashinit(1, M_PCB, &ripcbinfo.hashmask); 98 } 99 100 static struct sockaddr_in ripsrc = { sizeof(ripsrc), AF_INET }; 101 /* 102 * Setup generic address and protocol structures 103 * for raw_input routine, then pass them along with 104 * mbuf chain. 105 */ 106 void 107 rip_input(m, iphlen) 108 struct mbuf *m; 109 int iphlen; 110 { 111 register struct ip *ip = mtod(m, struct ip *); 112 register struct inpcb *inp; 113 struct inpcb *last = 0; 114 struct mbuf *opts = 0; 115 116 ripsrc.sin_addr = ip->ip_src; 117 for (inp = ripcb.lh_first; inp != NULL; inp = inp->inp_list.le_next) { 118 if (inp->inp_ip_p && inp->inp_ip_p != ip->ip_p) 119 continue; 120 if (inp->inp_laddr.s_addr && 121 inp->inp_laddr.s_addr != ip->ip_dst.s_addr) 122 continue; 123 if (inp->inp_faddr.s_addr && 124 inp->inp_faddr.s_addr != ip->ip_src.s_addr) 125 continue; 126 if (last) { 127 struct mbuf *n = m_copy(m, 0, (int)M_COPYALL); 128 if (n) { 129 if (last->inp_flags & INP_CONTROLOPTS || 130 last->inp_socket->so_options & SO_TIMESTAMP) 131 ip_savecontrol(last, &opts, ip, n); 132 if (sbappendaddr(&last->inp_socket->so_rcv, 133 (struct sockaddr *)&ripsrc, n, 134 opts) == 0) { 135 /* should notify about lost packet */ 136 m_freem(n); 137 if (opts) 138 m_freem(opts); 139 } else 140 sorwakeup(last->inp_socket); 141 opts = 0; 142 } 143 } 144 last = inp; 145 } 146 if (last) { 147 if (last->inp_flags & INP_CONTROLOPTS || 148 last->inp_socket->so_options & SO_TIMESTAMP) 149 ip_savecontrol(last, &opts, ip, m); 150 if (sbappendaddr(&last->inp_socket->so_rcv, 151 (struct sockaddr *)&ripsrc, m, opts) == 0) { 152 m_freem(m); 153 if (opts) 154 m_freem(opts); 155 } else 156 sorwakeup(last->inp_socket); 157 } else { 158 m_freem(m); 159 ipstat.ips_noproto++; 160 ipstat.ips_delivered--; 161 } 162 } 163 164 /* 165 * Generate IP header and pass packet to ip_output. 166 * Tack on options user may have setup with control call. 167 */ 168 int 169 rip_output(m, so, dst) 170 register struct mbuf *m; 171 struct socket *so; 172 u_long dst; 173 { 174 register struct ip *ip; 175 register struct inpcb *inp = sotoinpcb(so); 176 int flags = (so->so_options & SO_DONTROUTE) | IP_ALLOWBROADCAST; 177 178 /* 179 * If the user handed us a complete IP packet, use it. 180 * Otherwise, allocate an mbuf for a header and fill it in. 181 */ 182 if ((inp->inp_flags & INP_HDRINCL) == 0) { 183 if (m->m_pkthdr.len + sizeof(struct ip) > IP_MAXPACKET) { 184 m_freem(m); 185 return(EMSGSIZE); 186 } 187 M_PREPEND(m, sizeof(struct ip), M_WAIT); 188 ip = mtod(m, struct ip *); 189 ip->ip_tos = 0; 190 ip->ip_off = 0; 191 ip->ip_p = inp->inp_ip_p; 192 ip->ip_len = m->m_pkthdr.len; 193 ip->ip_src = inp->inp_laddr; 194 ip->ip_dst.s_addr = dst; 195 ip->ip_ttl = MAXTTL; 196 } else { 197 if (m->m_pkthdr.len > IP_MAXPACKET) { 198 m_freem(m); 199 return(EMSGSIZE); 200 } 201 ip = mtod(m, struct ip *); 202 /* don't allow both user specified and setsockopt options, 203 and don't allow packet length sizes that will crash */ 204 if (((IP_VHL_HL(ip->ip_vhl) != (sizeof (*ip) >> 2)) 205 && inp->inp_options) 206 || (ip->ip_len > m->m_pkthdr.len) 207 || (ip->ip_len < (IP_VHL_HL(ip->ip_vhl) << 2))) { 208 m_freem(m); 209 return EINVAL; 210 } 211 if (ip->ip_id == 0) 212 ip->ip_id = htons(ip_id++); 213 /* XXX prevent ip_output from overwriting header fields */ 214 flags |= IP_RAWOUTPUT; 215 ipstat.ips_rawout++; 216 } 217 return (ip_output(m, inp->inp_options, &inp->inp_route, flags, 218 inp->inp_moptions)); 219 } 220 221 /* 222 * Raw IP socket option processing. 223 */ 224 int 225 rip_ctloutput(op, so, level, optname, m, p) 226 int op; 227 struct socket *so; 228 int level, optname; 229 struct mbuf **m; 230 struct proc *p; 231 { 232 register struct inpcb *inp = sotoinpcb(so); 233 register int error; 234 235 if (level != IPPROTO_IP) { 236 if (op == PRCO_SETOPT && *m) 237 (void)m_free(*m); 238 return (EINVAL); 239 } 240 241 switch (optname) { 242 243 case IP_HDRINCL: 244 error = 0; 245 if (op == PRCO_SETOPT) { 246 if (m == 0 || *m == 0 || (*m)->m_len < sizeof (int)) 247 error = EINVAL; 248 else if (*mtod(*m, int *)) 249 inp->inp_flags |= INP_HDRINCL; 250 else 251 inp->inp_flags &= ~INP_HDRINCL; 252 if (*m) 253 (void)m_free(*m); 254 } else { 255 *m = m_get(M_WAIT, MT_SOOPTS); 256 (*m)->m_len = sizeof (int); 257 *mtod(*m, int *) = inp->inp_flags & INP_HDRINCL; 258 } 259 return (error); 260 261 #ifdef COMPAT_IPFW 262 case IP_FW_GET: 263 if (ip_fw_ctl_ptr == NULL || op == PRCO_SETOPT) { 264 if (*m) (void)m_free(*m); 265 return(EINVAL); 266 } 267 return (*ip_fw_ctl_ptr)(optname, m); 268 269 case IP_FW_ADD: 270 case IP_FW_DEL: 271 case IP_FW_FLUSH: 272 case IP_FW_ZERO: 273 if (ip_fw_ctl_ptr == NULL || op != PRCO_SETOPT) { 274 if (*m) (void)m_free(*m); 275 return(EINVAL); 276 } 277 return (*ip_fw_ctl_ptr)(optname, m); 278 279 case IP_NAT: 280 if (ip_nat_ctl_ptr == NULL) { 281 if (*m) (void)m_free(*m); 282 return(EINVAL); 283 } 284 return (*ip_nat_ctl_ptr)(op, m); 285 286 #endif 287 case IP_RSVP_ON: 288 return ip_rsvp_init(so); 289 break; 290 291 case IP_RSVP_OFF: 292 return ip_rsvp_done(); 293 break; 294 295 case IP_RSVP_VIF_ON: 296 return ip_rsvp_vif_init(so, *m); 297 298 case IP_RSVP_VIF_OFF: 299 return ip_rsvp_vif_done(so, *m); 300 301 case MRT_INIT: 302 case MRT_DONE: 303 case MRT_ADD_VIF: 304 case MRT_DEL_VIF: 305 case MRT_ADD_MFC: 306 case MRT_DEL_MFC: 307 case MRT_VERSION: 308 case MRT_ASSERT: 309 if (op == PRCO_SETOPT) { 310 error = ip_mrouter_set(optname, so, *m); 311 if (*m) 312 (void)m_free(*m); 313 } else if (op == PRCO_GETOPT) { 314 error = ip_mrouter_get(optname, so, m); 315 } else 316 error = EINVAL; 317 return (error); 318 } 319 return (ip_ctloutput(op, so, level, optname, m, p)); 320 } 321 322 /* 323 * This function exists solely to receive the PRC_IFDOWN messages which 324 * are sent by if_down(). It looks for an ifaddr whose ifa_addr is sa, 325 * and calls in_ifadown() to remove all routes corresponding to that address. 326 * It also receives the PRC_IFUP messages from if_up() and reinstalls the 327 * interface routes. 328 */ 329 void 330 rip_ctlinput(cmd, sa, vip) 331 int cmd; 332 struct sockaddr *sa; 333 void *vip; 334 { 335 struct in_ifaddr *ia; 336 struct ifnet *ifp; 337 int err; 338 int flags; 339 340 switch(cmd) { 341 case PRC_IFDOWN: 342 for (ia = in_ifaddrhead.tqh_first; ia; 343 ia = ia->ia_link.tqe_next) { 344 if (ia->ia_ifa.ifa_addr == sa 345 && (ia->ia_flags & IFA_ROUTE)) { 346 /* 347 * in_ifscrub kills the interface route. 348 */ 349 in_ifscrub(ia->ia_ifp, ia); 350 /* 351 * in_ifadown gets rid of all the rest of 352 * the routes. This is not quite the right 353 * thing to do, but at least if we are running 354 * a routing process they will come back. 355 */ 356 in_ifadown(&ia->ia_ifa); 357 break; 358 } 359 } 360 break; 361 362 case PRC_IFUP: 363 for (ia = in_ifaddrhead.tqh_first; ia; 364 ia = ia->ia_link.tqe_next) { 365 if (ia->ia_ifa.ifa_addr == sa) 366 break; 367 } 368 if (ia == 0 || (ia->ia_flags & IFA_ROUTE)) 369 return; 370 flags = RTF_UP; 371 ifp = ia->ia_ifa.ifa_ifp; 372 373 if ((ifp->if_flags & IFF_LOOPBACK) 374 || (ifp->if_flags & IFF_POINTOPOINT)) 375 flags |= RTF_HOST; 376 377 err = rtinit(&ia->ia_ifa, RTM_ADD, flags); 378 if (err == 0) 379 ia->ia_flags |= IFA_ROUTE; 380 break; 381 } 382 } 383 384 static u_long rip_sendspace = RIPSNDQ; 385 static u_long rip_recvspace = RIPRCVQ; 386 387 SYSCTL_INT(_net_inet_raw, OID_AUTO, maxdgram, CTLFLAG_RW, &rip_sendspace, 388 0, ""); 389 SYSCTL_INT(_net_inet_raw, OID_AUTO, recvspace, CTLFLAG_RW, &rip_recvspace, 390 0, ""); 391 392 static int 393 rip_attach(struct socket *so, int proto, struct proc *p) 394 { 395 struct inpcb *inp; 396 int error; 397 398 inp = sotoinpcb(so); 399 if (inp) 400 panic("rip_attach"); 401 if (p && (error = suser(p->p_ucred, &p->p_acflag)) != 0) 402 return error; 403 404 if ((error = soreserve(so, rip_sendspace, rip_recvspace)) || 405 (error = in_pcballoc(so, &ripcbinfo, p))) 406 return error; 407 inp = (struct inpcb *)so->so_pcb; 408 inp->inp_ip_p = proto; 409 return 0; 410 } 411 412 static int 413 rip_detach(struct socket *so) 414 { 415 struct inpcb *inp; 416 417 inp = sotoinpcb(so); 418 if (inp == 0) 419 panic("rip_detach"); 420 if (so == ip_mrouter) 421 ip_mrouter_done(); 422 ip_rsvp_force_done(so); 423 if (so == ip_rsvpd) 424 ip_rsvp_done(); 425 in_pcbdetach(inp); 426 return 0; 427 } 428 429 static int 430 rip_abort(struct socket *so) 431 { 432 soisdisconnected(so); 433 return rip_detach(so); 434 } 435 436 static int 437 rip_disconnect(struct socket *so) 438 { 439 if ((so->so_state & SS_ISCONNECTED) == 0) 440 return ENOTCONN; 441 return rip_abort(so); 442 } 443 444 static int 445 rip_bind(struct socket *so, struct mbuf *nam, struct proc *p) 446 { 447 struct inpcb *inp = sotoinpcb(so); 448 struct sockaddr_in *addr = mtod(nam, struct sockaddr_in *); 449 450 if (nam->m_len != sizeof(*addr)) 451 return EINVAL; 452 453 if (TAILQ_EMPTY(&ifnet) || ((addr->sin_family != AF_INET) && 454 (addr->sin_family != AF_IMPLINK)) || 455 (addr->sin_addr.s_addr && 456 ifa_ifwithaddr((struct sockaddr *)addr) == 0)) 457 return EADDRNOTAVAIL; 458 inp->inp_laddr = addr->sin_addr; 459 return 0; 460 } 461 462 static int 463 rip_connect(struct socket *so, struct mbuf *nam, struct proc *p) 464 { 465 struct inpcb *inp = sotoinpcb(so); 466 struct sockaddr_in *addr = mtod(nam, struct sockaddr_in *); 467 468 if (nam->m_len != sizeof(*addr)) 469 return EINVAL; 470 if (TAILQ_EMPTY(&ifnet)) 471 return EADDRNOTAVAIL; 472 if ((addr->sin_family != AF_INET) && 473 (addr->sin_family != AF_IMPLINK)) 474 return EAFNOSUPPORT; 475 inp->inp_faddr = addr->sin_addr; 476 soisconnected(so); 477 return 0; 478 } 479 480 static int 481 rip_shutdown(struct socket *so) 482 { 483 socantsendmore(so); 484 return 0; 485 } 486 487 static int 488 rip_send(struct socket *so, int flags, struct mbuf *m, struct mbuf *nam, 489 struct mbuf *control, struct proc *p) 490 { 491 struct inpcb *inp = sotoinpcb(so); 492 register u_long dst; 493 494 if (so->so_state & SS_ISCONNECTED) { 495 if (nam) { 496 m_freem(m); 497 return EISCONN; 498 } 499 dst = inp->inp_faddr.s_addr; 500 } else { 501 if (nam == NULL) { 502 m_freem(m); 503 return ENOTCONN; 504 } 505 dst = mtod(nam, struct sockaddr_in *)->sin_addr.s_addr; 506 } 507 return rip_output(m, so, dst); 508 } 509 510 struct pr_usrreqs rip_usrreqs = { 511 rip_abort, pru_accept_notsupp, rip_attach, rip_bind, rip_connect, 512 pru_connect2_notsupp, in_control, rip_detach, rip_disconnect, 513 pru_listen_notsupp, in_setpeeraddr, pru_rcvd_notsupp, 514 pru_rcvoob_notsupp, rip_send, pru_sense_null, rip_shutdown, 515 in_setsockaddr, sosend, soreceive, soselect 516 }; 517