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