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