raw_ip.c revision 7083
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.2 (Berkeley) 1/4/94 34 * $Id: raw_ip.c,v 1.15 1995/02/14 06:24:40 phk Exp $ 35 */ 36 37#include <sys/param.h> 38#include <sys/malloc.h> 39#include <sys/mbuf.h> 40#include <sys/socket.h> 41#include <sys/protosw.h> 42#include <sys/socketvar.h> 43#include <sys/errno.h> 44#include <sys/systm.h> 45 46#include <net/if.h> 47#include <net/route.h> 48 49#include <netinet/in.h> 50#include <netinet/in_systm.h> 51#include <netinet/ip.h> 52#include <netinet/ip_var.h> 53#include <netinet/ip_mroute.h> 54#include <netinet/in_pcb.h> 55 56#include <netinet/ip_fw.h> 57 58struct inpcb rawinpcb; 59 60/* 61 * Nominal space allocated to a raw ip socket. 62 */ 63#define RIPSNDQ 8192 64#define RIPRCVQ 8192 65 66/* 67 * Raw interface to IP protocol. 68 */ 69 70/* 71 * Initialize raw connection block q. 72 */ 73void 74rip_init() 75{ 76 77 rawinpcb.inp_next = rawinpcb.inp_prev = &rawinpcb; 78} 79 80struct sockaddr_in ripsrc = { sizeof(ripsrc), AF_INET }; 81/* 82 * Setup generic address and protocol structures 83 * for raw_input routine, then pass them along with 84 * mbuf chain. 85 */ 86void 87rip_input(m) 88 struct mbuf *m; 89{ 90 register struct ip *ip = mtod(m, struct ip *); 91 register struct inpcb *inp; 92 struct socket *last = 0; 93 94 ripsrc.sin_addr = ip->ip_src; 95 for (inp = rawinpcb.inp_next; inp != &rawinpcb; inp = inp->inp_next) { 96 if (inp->inp_ip.ip_p && inp->inp_ip.ip_p != ip->ip_p) 97 continue; 98 if (inp->inp_laddr.s_addr && 99 inp->inp_laddr.s_addr != ip->ip_dst.s_addr) 100 continue; 101 if (inp->inp_faddr.s_addr && 102 inp->inp_faddr.s_addr != ip->ip_src.s_addr) 103 continue; 104 if (last) { 105 struct mbuf *n = m_copy(m, 0, (int)M_COPYALL); 106 if (n) { 107 if (sbappendaddr(&last->so_rcv, 108 (struct sockaddr *)&ripsrc, n, 109 (struct mbuf *)0) == 0) 110 /* should notify about lost packet */ 111 m_freem(n); 112 else 113 sorwakeup(last); 114 } 115 } 116 last = inp->inp_socket; 117 } 118 if (last) { 119 if (sbappendaddr(&last->so_rcv, (struct sockaddr *)&ripsrc, 120 m, (struct mbuf *)0) == 0) 121 m_freem(m); 122 else 123 sorwakeup(last); 124 } else { 125 m_freem(m); 126 ipstat.ips_noproto++; 127 ipstat.ips_delivered--; 128 } 129} 130 131void rip_ip_input(mm, ip_mrouter, src) 132 struct mbuf *mm; 133 register struct socket *ip_mrouter; 134 struct sockaddr *src; 135{ 136 if (ip_mrouter) 137 { 138 if (sbappendaddr(&ip_mrouter->so_rcv, src, 139 mm, (struct mbuf *) 0) == 0) 140 m_freem(mm); 141 else 142 sorwakeup(ip_mrouter); 143 } 144 else 145 { 146 m_freem(mm); 147 ipstat.ips_noproto++; 148 ipstat.ips_delivered--; 149 } 150} 151 152/* 153 * Generate IP header and pass packet to ip_output. 154 * Tack on options user may have setup with control call. 155 */ 156int 157rip_output(m, so, dst) 158 register struct mbuf *m; 159 struct socket *so; 160 u_long dst; 161{ 162 register struct ip *ip; 163 register struct inpcb *inp = sotoinpcb(so); 164 struct mbuf *opts; 165 int flags = (so->so_options & SO_DONTROUTE) | IP_ALLOWBROADCAST; 166 167 /* 168 * If the user handed us a complete IP packet, use it. 169 * Otherwise, allocate an mbuf for a header and fill it in. 170 */ 171 if ((inp->inp_flags & INP_HDRINCL) == 0) { 172 M_PREPEND(m, sizeof(struct ip), M_WAIT); 173 ip = mtod(m, struct ip *); 174 ip->ip_tos = 0; 175 ip->ip_off = 0; 176 ip->ip_p = inp->inp_ip.ip_p; 177 ip->ip_len = m->m_pkthdr.len; 178 ip->ip_src = inp->inp_laddr; 179 ip->ip_dst.s_addr = dst; 180 ip->ip_ttl = MAXTTL; 181 opts = inp->inp_options; 182 } else { 183 ip = mtod(m, struct ip *); 184 if (ip->ip_id == 0) 185 ip->ip_id = htons(ip_id++); 186 opts = NULL; 187 /* XXX prevent ip_output from overwriting header fields */ 188 flags |= IP_RAWOUTPUT; 189 ipstat.ips_rawout++; 190 } 191 return (ip_output(m, opts, &inp->inp_route, flags, inp->inp_moptions)); 192} 193 194/* 195 * Raw IP socket option processing. 196 */ 197int 198rip_ctloutput(op, so, level, optname, m) 199 int op; 200 struct socket *so; 201 int level, optname; 202 struct mbuf **m; 203{ 204 register struct inpcb *inp = sotoinpcb(so); 205 register int error; 206 207 if (level != IPPROTO_IP) { 208 if (op == PRCO_SETOPT && *m) 209 (void)m_free(*m); 210 return (EINVAL); 211 } 212 213 switch (optname) { 214 215 case IP_HDRINCL: 216 if (op == PRCO_SETOPT || op == PRCO_GETOPT) { 217 if (m == 0 || *m == 0 || (*m)->m_len < sizeof (int)) 218 return (EINVAL); 219 if (op == PRCO_SETOPT) { 220 if (*mtod(*m, int *)) 221 inp->inp_flags |= INP_HDRINCL; 222 else 223 inp->inp_flags &= ~INP_HDRINCL; 224 (void)m_free(*m); 225 } else { 226 (*m)->m_len = sizeof (int); 227 *mtod(*m, int *) = inp->inp_flags & INP_HDRINCL; 228 } 229 return (0); 230 } 231 break; 232 233 case IP_FW_ADD: 234 case IP_FW_DEL: 235 case IP_FW_FLUSH: 236 case IP_FW_POLICY: 237 if (ip_fw_ctl_ptr==NULL) { 238 if (*m) 239 (void)m_free(*m); 240 return(EINVAL); 241 } 242 243 if (op == PRCO_SETOPT) { 244 error=(*ip_fw_ctl_ptr)(optname, *m); 245 if (*m) 246 (void)m_free(*m); 247 } 248 else 249 error=EINVAL; 250 return(error); 251 252 case IP_ACCT_DEL: 253 case IP_ACCT_ADD: 254 case IP_ACCT_CLR: 255 case IP_ACCT_FLUSH: 256 case IP_ACCT_ZERO: 257 if (ip_acct_ctl_ptr==NULL) { 258 if (*m) 259 (void)m_free(*m); 260 return(EINVAL); 261 } 262 263 op = PRCO_SETOPT; 264 if (op) { 265 error=(*ip_acct_ctl_ptr)(optname, *m); 266 if (*m) 267 (void)m_free(*m); 268 } 269 else 270 error=EINVAL; 271 return(error); 272 273 case IP_RSVP_ON: 274 return ip_rsvp_init(so); 275 break; 276 277 case IP_RSVP_OFF: 278 return ip_rsvp_done(); 279 break; 280 281 case DVMRP_INIT: 282 case DVMRP_DONE: 283 case DVMRP_ADD_VIF: 284 case DVMRP_DEL_VIF: 285 case DVMRP_ADD_MFC: 286 case DVMRP_DEL_MFC: 287 if (op == PRCO_SETOPT) { 288 error = ip_mrouter_cmd(optname, so, *m); 289 if (*m) 290 (void)m_free(*m); 291 } else 292 error = EINVAL; 293 return (error); 294 } 295 return (ip_ctloutput(op, so, level, optname, m)); 296} 297 298u_long rip_sendspace = RIPSNDQ; 299u_long rip_recvspace = RIPRCVQ; 300 301/*ARGSUSED*/ 302int 303rip_usrreq(so, req, m, nam, control) 304 register struct socket *so; 305 int req; 306 struct mbuf *m, *nam, *control; 307{ 308 register int error = 0; 309 register struct inpcb *inp = sotoinpcb(so); 310 switch (req) { 311 312 case PRU_ATTACH: 313 if (inp) 314 panic("rip_attach"); 315 if ((so->so_state & SS_PRIV) == 0) { 316 error = EACCES; 317 break; 318 } 319 if ((error = soreserve(so, rip_sendspace, rip_recvspace)) || 320 (error = in_pcballoc(so, &rawinpcb))) 321 break; 322 inp = (struct inpcb *)so->so_pcb; 323 inp->inp_ip.ip_p = (int)nam; 324 break; 325 326 case PRU_DISCONNECT: 327 if ((so->so_state & SS_ISCONNECTED) == 0) { 328 error = ENOTCONN; 329 break; 330 } 331 /* FALLTHROUGH */ 332 case PRU_ABORT: 333 soisdisconnected(so); 334 /* FALLTHROUGH */ 335 case PRU_DETACH: 336 if (inp == 0) 337 panic("rip_detach"); 338 if (so == ip_mrouter) 339 ip_mrouter_done(); 340 if (so == ip_rsvpd) 341 ip_rsvp_done(); 342 in_pcbdetach(inp); 343 break; 344 345 case PRU_BIND: 346 { 347 struct sockaddr_in *addr = mtod(nam, struct sockaddr_in *); 348 349 if (nam->m_len != sizeof(*addr)) { 350 error = EINVAL; 351 break; 352 } 353 if ((ifnet == 0) || 354 ((addr->sin_family != AF_INET) && 355 (addr->sin_family != AF_IMPLINK)) || 356 (addr->sin_addr.s_addr && 357 ifa_ifwithaddr((struct sockaddr *)addr) == 0)) { 358 error = EADDRNOTAVAIL; 359 break; 360 } 361 inp->inp_laddr = addr->sin_addr; 362 break; 363 } 364 case PRU_CONNECT: 365 { 366 struct sockaddr_in *addr = mtod(nam, struct sockaddr_in *); 367 368 if (nam->m_len != sizeof(*addr)) { 369 error = EINVAL; 370 break; 371 } 372 if (ifnet == 0) { 373 error = EADDRNOTAVAIL; 374 break; 375 } 376 if ((addr->sin_family != AF_INET) && 377 (addr->sin_family != AF_IMPLINK)) { 378 error = EAFNOSUPPORT; 379 break; 380 } 381 inp->inp_faddr = addr->sin_addr; 382 soisconnected(so); 383 break; 384 } 385 386 case PRU_CONNECT2: 387 error = EOPNOTSUPP; 388 break; 389 390 /* 391 * Mark the connection as being incapable of further input. 392 */ 393 case PRU_SHUTDOWN: 394 socantsendmore(so); 395 break; 396 397 /* 398 * Ship a packet out. The appropriate raw output 399 * routine handles any massaging necessary. 400 */ 401 case PRU_SEND: 402 { 403 register u_long dst; 404 405 if (so->so_state & SS_ISCONNECTED) { 406 if (nam) { 407 error = EISCONN; 408 break; 409 } 410 dst = inp->inp_faddr.s_addr; 411 } else { 412 if (nam == NULL) { 413 error = ENOTCONN; 414 break; 415 } 416 dst = mtod(nam, struct sockaddr_in *)->sin_addr.s_addr; 417 } 418 error = rip_output(m, so, dst); 419 m = NULL; 420 break; 421 } 422 423 case PRU_SENSE: 424 /* 425 * stat: don't bother with a blocksize. 426 */ 427 return (0); 428 429 /* 430 * Not supported. 431 */ 432 case PRU_RCVOOB: 433 case PRU_RCVD: 434 case PRU_LISTEN: 435 case PRU_ACCEPT: 436 case PRU_SENDOOB: 437 error = EOPNOTSUPP; 438 break; 439 440 case PRU_SOCKADDR: 441 in_setsockaddr(inp, nam); 442 break; 443 444 case PRU_PEERADDR: 445 in_setpeeraddr(inp, nam); 446 break; 447 448 default: 449 panic("rip_usrreq"); 450 } 451 if (m != NULL) 452 m_freem(m); 453 return (error); 454} 455