1 /* $NetBSD: raw_ip.c,v 1.22 1995/11/30 16:42:18 pk Exp $ */ 2 3 /* 4 * Copyright (c) 1982, 1986, 1988, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by the University of 18 * California, Berkeley and its contributors. 19 * 4. Neither the name of the University nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 * 35 * @(#)raw_ip.c 8.2 (Berkeley) 1/4/94 36 */ 37 38 #include <sys/param.h> 39 #include <sys/malloc.h> 40 #include <sys/mbuf.h> 41 #include <sys/socket.h> 42 #include <sys/protosw.h> 43 #include <sys/socketvar.h> 44 #include <sys/errno.h> 45 #include <sys/systm.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 struct inpcbtable rawcbtable; 58 59 /* 60 * Nominal space allocated to a raw ip socket. 61 */ 62 #define RIPSNDQ 8192 63 #define RIPRCVQ 8192 64 65 /* 66 * Raw interface to IP protocol. 67 */ 68 69 /* 70 * Initialize raw connection block q. 71 */ 72 void 73 rip_init() 74 { 75 76 in_pcbinit(&rawcbtable); 77 } 78 79 struct sockaddr_in ripsrc = { sizeof(ripsrc), AF_INET }; 80 /* 81 * Setup generic address and protocol structures 82 * for raw_input routine, then pass them along with 83 * mbuf chain. 84 */ 85 void 86 rip_input(m) 87 struct mbuf *m; 88 { 89 register struct ip *ip = mtod(m, struct ip *); 90 register struct inpcb *inp; 91 struct socket *last = 0; 92 93 ripsrc.sin_addr = ip->ip_src; 94 for (inp = rawcbtable.inpt_queue.cqh_first; 95 inp != (struct inpcb *)&rawcbtable.inpt_queue; 96 inp = inp->inp_queue.cqe_next) { 97 if (inp->inp_ip.ip_p && inp->inp_ip.ip_p != ip->ip_p) 98 continue; 99 if (inp->inp_laddr.s_addr && 100 inp->inp_laddr.s_addr != ip->ip_dst.s_addr) 101 continue; 102 if (inp->inp_faddr.s_addr && 103 inp->inp_faddr.s_addr != ip->ip_src.s_addr) 104 continue; 105 if (last) { 106 struct mbuf *n; 107 if (n = m_copy(m, 0, (int)M_COPYALL)) { 108 if (sbappendaddr(&last->so_rcv, 109 sintosa(&ripsrc), n, 110 (struct mbuf *)0) == 0) 111 /* should notify about lost packet */ 112 m_freem(n); 113 else 114 sorwakeup(last); 115 } 116 } 117 last = inp->inp_socket; 118 } 119 if (last) { 120 if (sbappendaddr(&last->so_rcv, sintosa(&ripsrc), m, 121 (struct mbuf *)0) == 0) 122 m_freem(m); 123 else 124 sorwakeup(last); 125 } else { 126 m_freem(m); 127 ipstat.ips_noproto++; 128 ipstat.ips_delivered--; 129 } 130 } 131 132 /* 133 * Generate IP header and pass packet to ip_output. 134 * Tack on options user may have setup with control call. 135 */ 136 int 137 rip_output(m, so, dst) 138 register struct mbuf *m; 139 struct socket *so; 140 u_long dst; 141 { 142 register struct ip *ip; 143 register struct inpcb *inp = sotoinpcb(so); 144 struct mbuf *opts; 145 int flags = (so->so_options & SO_DONTROUTE) | IP_ALLOWBROADCAST; 146 147 /* 148 * If the user handed us a complete IP packet, use it. 149 * Otherwise, allocate an mbuf for a header and fill it in. 150 */ 151 if ((inp->inp_flags & INP_HDRINCL) == 0) { 152 M_PREPEND(m, sizeof(struct ip), M_WAIT); 153 ip = mtod(m, struct ip *); 154 ip->ip_tos = 0; 155 ip->ip_off = 0; 156 ip->ip_p = inp->inp_ip.ip_p; 157 ip->ip_len = m->m_pkthdr.len; 158 ip->ip_src = inp->inp_laddr; 159 ip->ip_dst.s_addr = dst; 160 ip->ip_ttl = MAXTTL; 161 opts = inp->inp_options; 162 } else { 163 ip = mtod(m, struct ip *); 164 if (ip->ip_id == 0) 165 ip->ip_id = htons(ip_id++); 166 opts = NULL; 167 /* XXX prevent ip_output from overwriting header fields */ 168 flags |= IP_RAWOUTPUT; 169 ipstat.ips_rawout++; 170 } 171 return (ip_output(m, opts, &inp->inp_route, flags, inp->inp_moptions)); 172 } 173 174 /* 175 * Raw IP socket option processing. 176 */ 177 int 178 rip_ctloutput(op, so, level, optname, m) 179 int op; 180 struct socket *so; 181 int level, optname; 182 struct mbuf **m; 183 { 184 register struct inpcb *inp = sotoinpcb(so); 185 register int error; 186 187 if (level != IPPROTO_IP) { 188 if (m != 0 && *m != 0) 189 (void)m_free(*m); 190 return (EINVAL); 191 } 192 193 switch (optname) { 194 195 case IP_HDRINCL: 196 if (op == PRCO_SETOPT || op == PRCO_GETOPT) { 197 if (m == 0 || *m == 0 || (*m)->m_len < sizeof (int)) 198 return (EINVAL); 199 if (op == PRCO_SETOPT) { 200 if (*mtod(*m, int *)) 201 inp->inp_flags |= INP_HDRINCL; 202 else 203 inp->inp_flags &= ~INP_HDRINCL; 204 (void)m_free(*m); 205 } else { 206 (*m)->m_len = sizeof (int); 207 *mtod(*m, int *) = inp->inp_flags & INP_HDRINCL; 208 } 209 return (0); 210 } 211 break; 212 213 case MRT_INIT: 214 case MRT_DONE: 215 case MRT_ADD_VIF: 216 case MRT_DEL_VIF: 217 case MRT_ADD_MFC: 218 case MRT_DEL_MFC: 219 case MRT_VERSION: 220 case MRT_ASSERT: 221 #ifdef MROUTING 222 switch (op) { 223 case PRCO_SETOPT: 224 error = ip_mrouter_set(optname, so, m); 225 break; 226 case PRCO_GETOPT: 227 error = ip_mrouter_get(optname, so, m); 228 break; 229 default: 230 error = EINVAL; 231 break; 232 } 233 return (error); 234 #else 235 if (op == PRCO_SETOPT && *m) 236 m_free(*m); 237 return (EOPNOTSUPP); 238 #endif 239 } 240 return (ip_ctloutput(op, so, level, optname, m)); 241 } 242 243 u_long rip_sendspace = RIPSNDQ; 244 u_long rip_recvspace = RIPRCVQ; 245 246 /*ARGSUSED*/ 247 int 248 rip_usrreq(so, req, m, nam, control) 249 register struct socket *so; 250 int req; 251 struct mbuf *m, *nam, *control; 252 { 253 register int error = 0; 254 register struct inpcb *inp = sotoinpcb(so); 255 #ifdef MROUTING 256 extern struct socket *ip_mrouter; 257 #endif 258 if (req == PRU_CONTROL) 259 return (in_control(so, (long)m, (caddr_t)nam, 260 (struct ifnet *)control)); 261 262 if (inp == NULL && req != PRU_ATTACH) { 263 error = EINVAL; 264 goto release; 265 } 266 267 switch (req) { 268 269 case PRU_ATTACH: 270 if (inp) 271 panic("rip_attach"); 272 if ((so->so_state & SS_PRIV) == 0) { 273 error = EACCES; 274 break; 275 } 276 if ((error = soreserve(so, rip_sendspace, rip_recvspace)) || 277 (error = in_pcballoc(so, &rawcbtable))) 278 break; 279 inp = (struct inpcb *)so->so_pcb; 280 inp->inp_ip.ip_p = (long)nam; 281 break; 282 283 case PRU_DISCONNECT: 284 if ((so->so_state & SS_ISCONNECTED) == 0) { 285 error = ENOTCONN; 286 break; 287 } 288 /* FALLTHROUGH */ 289 case PRU_ABORT: 290 soisdisconnected(so); 291 /* FALLTHROUGH */ 292 case PRU_DETACH: 293 if (inp == 0) 294 panic("rip_detach"); 295 #ifdef MROUTING 296 if (so == ip_mrouter) 297 ip_mrouter_done(); 298 #endif 299 in_pcbdetach(inp); 300 break; 301 302 case PRU_BIND: 303 { 304 struct sockaddr_in *addr = mtod(nam, struct sockaddr_in *); 305 306 if (nam->m_len != sizeof(*addr)) { 307 error = EINVAL; 308 break; 309 } 310 if ((ifnet.tqh_first == 0) || 311 ((addr->sin_family != AF_INET) && 312 (addr->sin_family != AF_IMPLINK)) || 313 (addr->sin_addr.s_addr && 314 ifa_ifwithaddr(sintosa(addr)) == 0)) { 315 error = EADDRNOTAVAIL; 316 break; 317 } 318 inp->inp_laddr = addr->sin_addr; 319 break; 320 } 321 case PRU_CONNECT: 322 { 323 struct sockaddr_in *addr = mtod(nam, struct sockaddr_in *); 324 325 if (nam->m_len != sizeof(*addr)) { 326 error = EINVAL; 327 break; 328 } 329 if (ifnet.tqh_first == 0) { 330 error = EADDRNOTAVAIL; 331 break; 332 } 333 if ((addr->sin_family != AF_INET) && 334 (addr->sin_family != AF_IMPLINK)) { 335 error = EAFNOSUPPORT; 336 break; 337 } 338 inp->inp_faddr = addr->sin_addr; 339 soisconnected(so); 340 break; 341 } 342 343 case PRU_CONNECT2: 344 error = EOPNOTSUPP; 345 break; 346 347 /* 348 * Mark the connection as being incapable of further input. 349 */ 350 case PRU_SHUTDOWN: 351 socantsendmore(so); 352 break; 353 354 /* 355 * Ship a packet out. The appropriate raw output 356 * routine handles any massaging necessary. 357 */ 358 case PRU_SEND: 359 { 360 register u_int32_t dst; 361 362 if (so->so_state & SS_ISCONNECTED) { 363 if (nam) { 364 error = EISCONN; 365 break; 366 } 367 dst = inp->inp_faddr.s_addr; 368 } else { 369 if (nam == NULL) { 370 error = ENOTCONN; 371 break; 372 } 373 dst = mtod(nam, struct sockaddr_in *)->sin_addr.s_addr; 374 } 375 error = rip_output(m, so, dst); 376 m = NULL; 377 break; 378 } 379 380 case PRU_SENSE: 381 /* 382 * stat: don't bother with a blocksize. 383 */ 384 return (0); 385 386 /* 387 * Not supported. 388 */ 389 case PRU_RCVOOB: 390 case PRU_RCVD: 391 case PRU_LISTEN: 392 case PRU_ACCEPT: 393 case PRU_SENDOOB: 394 error = EOPNOTSUPP; 395 break; 396 397 case PRU_SOCKADDR: 398 in_setsockaddr(inp, nam); 399 break; 400 401 case PRU_PEERADDR: 402 in_setpeeraddr(inp, nam); 403 break; 404 405 default: 406 panic("rip_usrreq"); 407 } 408 release: 409 if (m != NULL) 410 m_freem(m); 411 return (error); 412 } 413