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