Home | History | Annotate | Line # | Download | only in netinet
raw_ip.c revision 1.46.2.2
      1 /*	$NetBSD: raw_ip.c,v 1.46.2.2 2001/02/11 19:17:17 bouyer Exp $	*/
      2 
      3 /*
      4  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
      5  * 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. Neither the name of the project nor the names of its contributors
     16  *    may be used to endorse or promote products derived from this software
     17  *    without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
     20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
     23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  */
     31 
     32 /*
     33  * Copyright (c) 1982, 1986, 1988, 1993
     34  *	The Regents of the University of California.  All rights reserved.
     35  *
     36  * Redistribution and use in source and binary forms, with or without
     37  * modification, are permitted provided that the following conditions
     38  * are met:
     39  * 1. Redistributions of source code must retain the above copyright
     40  *    notice, this list of conditions and the following disclaimer.
     41  * 2. Redistributions in binary form must reproduce the above copyright
     42  *    notice, this list of conditions and the following disclaimer in the
     43  *    documentation and/or other materials provided with the distribution.
     44  * 3. All advertising materials mentioning features or use of this software
     45  *    must display the following acknowledgement:
     46  *	This product includes software developed by the University of
     47  *	California, Berkeley and its contributors.
     48  * 4. Neither the name of the University nor the names of its contributors
     49  *    may be used to endorse or promote products derived from this software
     50  *    without specific prior written permission.
     51  *
     52  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     53  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     54  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     55  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     56  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     57  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     58  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     59  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     60  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     61  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     62  * SUCH DAMAGE.
     63  *
     64  *	@(#)raw_ip.c	8.7 (Berkeley) 5/15/95
     65  */
     66 
     67 #include "opt_ipsec.h"
     68 #include "opt_mrouting.h"
     69 
     70 #include <sys/param.h>
     71 #include <sys/malloc.h>
     72 #include <sys/mbuf.h>
     73 #include <sys/socket.h>
     74 #include <sys/protosw.h>
     75 #include <sys/socketvar.h>
     76 #include <sys/errno.h>
     77 #include <sys/systm.h>
     78 #include <sys/proc.h>
     79 
     80 #include <net/if.h>
     81 #include <net/route.h>
     82 
     83 #include <netinet/in.h>
     84 #include <netinet/in_systm.h>
     85 #include <netinet/ip.h>
     86 #include <netinet/ip_var.h>
     87 #include <netinet/ip_mroute.h>
     88 #include <netinet/ip_icmp.h>
     89 #include <netinet/in_pcb.h>
     90 #include <netinet/in_var.h>
     91 
     92 #include <machine/stdarg.h>
     93 
     94 #ifdef IPSEC
     95 #include <netinet6/ipsec.h>
     96 #endif /*IPSEC*/
     97 
     98 struct inpcbtable rawcbtable;
     99 
    100 int	 rip_bind __P((struct inpcb *, struct mbuf *));
    101 int	 rip_connect __P((struct inpcb *, struct mbuf *));
    102 void	 rip_disconnect __P((struct inpcb *));
    103 
    104 /*
    105  * Nominal space allocated to a raw ip socket.
    106  */
    107 #define	RIPSNDQ		8192
    108 #define	RIPRCVQ		8192
    109 
    110 /*
    111  * Raw interface to IP protocol.
    112  */
    113 
    114 /*
    115  * Initialize raw connection block q.
    116  */
    117 void
    118 rip_init()
    119 {
    120 
    121 	in_pcbinit(&rawcbtable, 1, 1);
    122 }
    123 
    124 static struct	sockaddr_in ripsrc = { sizeof(ripsrc), AF_INET };
    125 
    126 /*
    127  * Setup generic address and protocol structures
    128  * for raw_input routine, then pass them along with
    129  * mbuf chain.
    130  */
    131 void
    132 #if __STDC__
    133 rip_input(struct mbuf *m, ...)
    134 #else
    135 rip_input(m, va_alist)
    136 	struct mbuf *m;
    137 	va_dcl
    138 #endif
    139 {
    140 	int off, proto;
    141 	struct ip *ip = mtod(m, struct ip *);
    142 	struct inpcb *inp;
    143 	struct inpcb *last = 0;
    144 	struct mbuf *opts = 0;
    145 	struct sockaddr_in ripsrc;
    146 	va_list ap;
    147 
    148 	va_start(ap, m);
    149 	off = va_arg(ap, int);
    150 	proto = va_arg(ap, int);
    151 	va_end(ap);
    152 
    153 	ripsrc.sin_family = AF_INET;
    154 	ripsrc.sin_len = sizeof(struct sockaddr_in);
    155 	ripsrc.sin_addr = ip->ip_src;
    156 	ripsrc.sin_port = 0;
    157 	bzero((caddr_t)ripsrc.sin_zero, sizeof(ripsrc.sin_zero));
    158 
    159 	/*
    160 	 * XXX Compatibility: programs using raw IP expect ip_len
    161 	 * XXX to have the header length subtracted.
    162 	 */
    163 	ip->ip_len -= ip->ip_hl << 2;
    164 
    165 	for (inp = rawcbtable.inpt_queue.cqh_first;
    166 	    inp != (struct inpcb *)&rawcbtable.inpt_queue;
    167 	    inp = inp->inp_queue.cqe_next) {
    168 		if (inp->inp_ip.ip_p && inp->inp_ip.ip_p != proto)
    169 			continue;
    170 		if (!in_nullhost(inp->inp_laddr) &&
    171 		    !in_hosteq(inp->inp_laddr, ip->ip_dst))
    172 			continue;
    173 		if (!in_nullhost(inp->inp_faddr) &&
    174 		    !in_hosteq(inp->inp_faddr, ip->ip_src))
    175 			continue;
    176 		if (last) {
    177 			struct mbuf *n;
    178 			if ((n = m_copy(m, 0, (int)M_COPYALL)) != NULL) {
    179 				if (last->inp_flags & INP_CONTROLOPTS ||
    180 				    last->inp_socket->so_options & SO_TIMESTAMP)
    181 					ip_savecontrol(last, &opts, ip, n);
    182 				if (sbappendaddr(&last->inp_socket->so_rcv,
    183 				    sintosa(&ripsrc), n, opts) == 0) {
    184 					/* should notify about lost packet */
    185 					m_freem(n);
    186 					if (opts)
    187 						m_freem(opts);
    188 				} else
    189 					sorwakeup(last->inp_socket);
    190 				opts = NULL;
    191 			}
    192 		}
    193 		last = inp;
    194 	}
    195 	if (last) {
    196 		if (last->inp_flags & INP_CONTROLOPTS ||
    197 		    last->inp_socket->so_options & SO_TIMESTAMP)
    198 			ip_savecontrol(last, &opts, ip, m);
    199 		if (sbappendaddr(&last->inp_socket->so_rcv,
    200 		    sintosa(&ripsrc), m, opts) == 0) {
    201 			m_freem(m);
    202 			if (opts)
    203 				m_freem(opts);
    204 		} else
    205 			sorwakeup(last->inp_socket);
    206 	} else {
    207 		if (inetsw[ip_protox[ip->ip_p]].pr_input == rip_input) {
    208 			icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PROTOCOL,
    209 			    0, 0);
    210 			ipstat.ips_noproto++;
    211 			ipstat.ips_delivered--;
    212 		} else
    213 			m_freem(m);
    214 	}
    215 	return;
    216 }
    217 
    218 /*
    219  * Generate IP header and pass packet to ip_output.
    220  * Tack on options user may have setup with control call.
    221  */
    222 int
    223 #if __STDC__
    224 rip_output(struct mbuf *m, ...)
    225 #else
    226 rip_output(m, va_alist)
    227 	struct mbuf *m;
    228 	va_dcl
    229 #endif
    230 {
    231 	struct inpcb *inp;
    232 	struct ip *ip;
    233 	struct mbuf *opts;
    234 	int flags;
    235 	va_list ap;
    236 
    237 	va_start(ap, m);
    238 	inp = va_arg(ap, struct inpcb *);
    239 	va_end(ap);
    240 
    241 	flags =
    242 	    (inp->inp_socket->so_options & SO_DONTROUTE) | IP_ALLOWBROADCAST
    243 	    | IP_RETURNMTU;
    244 
    245 	/*
    246 	 * If the user handed us a complete IP packet, use it.
    247 	 * Otherwise, allocate an mbuf for a header and fill it in.
    248 	 */
    249 	if ((inp->inp_flags & INP_HDRINCL) == 0) {
    250 		if ((m->m_pkthdr.len + sizeof(struct ip)) > IP_MAXPACKET) {
    251 			m_freem(m);
    252 			return (EMSGSIZE);
    253 		}
    254 		M_PREPEND(m, sizeof(struct ip), M_WAIT);
    255 		ip = mtod(m, struct ip *);
    256 		ip->ip_tos = 0;
    257 		ip->ip_off = 0;
    258 		ip->ip_p = inp->inp_ip.ip_p;
    259 		ip->ip_len = m->m_pkthdr.len;
    260 		ip->ip_src = inp->inp_laddr;
    261 		ip->ip_dst = inp->inp_faddr;
    262 		ip->ip_ttl = MAXTTL;
    263 		opts = inp->inp_options;
    264 	} else {
    265 		if (m->m_pkthdr.len > IP_MAXPACKET) {
    266 			m_freem(m);
    267 			return (EMSGSIZE);
    268 		}
    269 		ip = mtod(m, struct ip *);
    270 		if (m->m_pkthdr.len != ip->ip_len) {
    271 			m_freem(m);
    272 			return (EINVAL);
    273 		}
    274 		if (ip->ip_id == 0)
    275 			ip->ip_id = htons(ip_id++);
    276 		opts = NULL;
    277 		/* XXX prevent ip_output from overwriting header fields */
    278 		flags |= IP_RAWOUTPUT;
    279 		ipstat.ips_rawout++;
    280 	}
    281 #ifdef IPSEC
    282 	if (ipsec_setsocket(m, inp->inp_socket) != 0) {
    283 		m_freem(m);
    284 		return ENOBUFS;
    285 	}
    286 #endif /*IPSEC*/
    287 	return (ip_output(m, opts, &inp->inp_route, flags, inp->inp_moptions, &inp->inp_errormtu));
    288 }
    289 
    290 /*
    291  * Raw IP socket option processing.
    292  */
    293 int
    294 rip_ctloutput(op, so, level, optname, m)
    295 	int op;
    296 	struct socket *so;
    297 	int level, optname;
    298 	struct mbuf **m;
    299 {
    300 	struct inpcb *inp = sotoinpcb(so);
    301 	int error = 0;
    302 
    303 	if (level != IPPROTO_IP) {
    304 		error = ENOPROTOOPT;
    305 		if (op == PRCO_SETOPT && *m != 0)
    306 			(void) m_free(*m);
    307 	} else switch (op) {
    308 
    309 	case PRCO_SETOPT:
    310 		switch (optname) {
    311 		case IP_HDRINCL:
    312 			if (*m == 0 || (*m)->m_len < sizeof (int))
    313 				error = EINVAL;
    314 			else {
    315 				if (*mtod(*m, int *))
    316 					inp->inp_flags |= INP_HDRINCL;
    317 				else
    318 					inp->inp_flags &= ~INP_HDRINCL;
    319 			}
    320 			if (*m != 0)
    321 				(void) m_free(*m);
    322 			break;
    323 
    324 #ifdef MROUTING
    325 		case MRT_INIT:
    326 		case MRT_DONE:
    327 		case MRT_ADD_VIF:
    328 		case MRT_DEL_VIF:
    329 		case MRT_ADD_MFC:
    330 		case MRT_DEL_MFC:
    331 		case MRT_ASSERT:
    332 			error = ip_mrouter_set(so, optname, m);
    333 			break;
    334 #endif
    335 
    336 		default:
    337 			error = ip_ctloutput(op, so, level, optname, m);
    338 			break;
    339 		}
    340 		break;
    341 
    342 	case PRCO_GETOPT:
    343 		switch (optname) {
    344 		case IP_HDRINCL:
    345 			*m = m_get(M_WAIT, M_SOOPTS);
    346 			(*m)->m_len = sizeof (int);
    347 			*mtod(*m, int *) = inp->inp_flags & INP_HDRINCL ? 1 : 0;
    348 			break;
    349 
    350 #ifdef MROUTING
    351 		case MRT_VERSION:
    352 		case MRT_ASSERT:
    353 			error = ip_mrouter_get(so, optname, m);
    354 			break;
    355 #endif
    356 
    357 		default:
    358 			error = ip_ctloutput(op, so, level, optname, m);
    359 			break;
    360 		}
    361 		break;
    362 	}
    363 	return (error);
    364 }
    365 
    366 int
    367 rip_bind(inp, nam)
    368 	struct inpcb *inp;
    369 	struct mbuf *nam;
    370 {
    371 	struct sockaddr_in *addr = mtod(nam, struct sockaddr_in *);
    372 
    373 	if (nam->m_len != sizeof(*addr))
    374 		return (EINVAL);
    375 	if (ifnet.tqh_first == 0)
    376 		return (EADDRNOTAVAIL);
    377 	if (addr->sin_family != AF_INET &&
    378 	    addr->sin_family != AF_IMPLINK)
    379 		return (EAFNOSUPPORT);
    380 	if (!in_nullhost(addr->sin_addr) &&
    381 	    ifa_ifwithaddr(sintosa(addr)) == 0)
    382 		return (EADDRNOTAVAIL);
    383 	inp->inp_laddr = addr->sin_addr;
    384 	return (0);
    385 }
    386 
    387 int
    388 rip_connect(inp, nam)
    389 	struct inpcb *inp;
    390 	struct mbuf *nam;
    391 {
    392 	struct sockaddr_in *addr = mtod(nam, struct sockaddr_in *);
    393 
    394 	if (nam->m_len != sizeof(*addr))
    395 		return (EINVAL);
    396 	if (ifnet.tqh_first == 0)
    397 		return (EADDRNOTAVAIL);
    398 	if (addr->sin_family != AF_INET &&
    399 	    addr->sin_family != AF_IMPLINK)
    400 		return (EAFNOSUPPORT);
    401 	inp->inp_faddr = addr->sin_addr;
    402 	return (0);
    403 }
    404 
    405 void
    406 rip_disconnect(inp)
    407 	struct inpcb *inp;
    408 {
    409 
    410 	inp->inp_faddr = zeroin_addr;
    411 }
    412 
    413 u_long	rip_sendspace = RIPSNDQ;
    414 u_long	rip_recvspace = RIPRCVQ;
    415 
    416 /*ARGSUSED*/
    417 int
    418 rip_usrreq(so, req, m, nam, control, p)
    419 	struct socket *so;
    420 	int req;
    421 	struct mbuf *m, *nam, *control;
    422 	struct proc *p;
    423 {
    424 	struct inpcb *inp;
    425 	int s;
    426 	int error = 0;
    427 #ifdef MROUTING
    428 	extern struct socket *ip_mrouter;
    429 #endif
    430 
    431 	if (req == PRU_CONTROL)
    432 		return (in_control(so, (long)m, (caddr_t)nam,
    433 		    (struct ifnet *)control, p));
    434 
    435 	if (req == PRU_PURGEIF) {
    436 		in_purgeif((struct ifnet *)control);
    437 		in_pcbpurgeif(&rawcbtable, (struct ifnet *)control);
    438 		return (0);
    439 	}
    440 
    441 	s = splsoftnet();
    442 	inp = sotoinpcb(so);
    443 #ifdef DIAGNOSTIC
    444 	if (req != PRU_SEND && req != PRU_SENDOOB && control)
    445 		panic("rip_usrreq: unexpected control mbuf");
    446 #endif
    447 	if (inp == 0 && req != PRU_ATTACH) {
    448 		error = EINVAL;
    449 		goto release;
    450 	}
    451 
    452 	switch (req) {
    453 
    454 	case PRU_ATTACH:
    455 		if (inp != 0) {
    456 			error = EISCONN;
    457 			break;
    458 		}
    459 		if (p == 0 || (error = suser(p->p_ucred, &p->p_acflag))) {
    460 			error = EACCES;
    461 			break;
    462 		}
    463 		if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
    464 			error = soreserve(so, rip_sendspace, rip_recvspace);
    465 			if (error)
    466 				break;
    467 		}
    468 		error = in_pcballoc(so, &rawcbtable);
    469 		if (error)
    470 			break;
    471 		inp = sotoinpcb(so);
    472 		inp->inp_ip.ip_p = (long)nam;
    473 #ifdef IPSEC
    474 		error = ipsec_init_policy(so, &inp->inp_sp);
    475 		if (error != 0) {
    476 			in_pcbdetach(inp);
    477 			break;
    478 		}
    479 #endif /*IPSEC*/
    480 		break;
    481 
    482 	case PRU_DETACH:
    483 #ifdef MROUTING
    484 		if (so == ip_mrouter)
    485 			ip_mrouter_done();
    486 #endif
    487 		in_pcbdetach(inp);
    488 		break;
    489 
    490 	case PRU_BIND:
    491 		error = rip_bind(inp, nam);
    492 		break;
    493 
    494 	case PRU_LISTEN:
    495 		error = EOPNOTSUPP;
    496 		break;
    497 
    498 	case PRU_CONNECT:
    499 		error = rip_connect(inp, nam);
    500 		if (error)
    501 			break;
    502 		soisconnected(so);
    503 		break;
    504 
    505 	case PRU_CONNECT2:
    506 		error = EOPNOTSUPP;
    507 		break;
    508 
    509 	case PRU_DISCONNECT:
    510 		soisdisconnected(so);
    511 		rip_disconnect(inp);
    512 		break;
    513 
    514 	/*
    515 	 * Mark the connection as being incapable of further input.
    516 	 */
    517 	case PRU_SHUTDOWN:
    518 		socantsendmore(so);
    519 		break;
    520 
    521 	case PRU_RCVD:
    522 		error = EOPNOTSUPP;
    523 		break;
    524 
    525 	/*
    526 	 * Ship a packet out.  The appropriate raw output
    527 	 * routine handles any massaging necessary.
    528 	 */
    529 	case PRU_SEND:
    530 		if (control && control->m_len) {
    531 			m_freem(control);
    532 			m_freem(m);
    533 			error = EINVAL;
    534 			break;
    535 		}
    536 	{
    537 		if (nam) {
    538 			if ((so->so_state & SS_ISCONNECTED) != 0) {
    539 				error = EISCONN;
    540 				goto die;
    541 			}
    542 			error = rip_connect(inp, nam);
    543 			if (error) {
    544 			die:
    545 				m_freem(m);
    546 				break;
    547 			}
    548 		} else {
    549 			if ((so->so_state & SS_ISCONNECTED) == 0) {
    550 				error = ENOTCONN;
    551 				goto die;
    552 			}
    553 		}
    554 		error = rip_output(m, inp);
    555 		if (nam)
    556 			rip_disconnect(inp);
    557 	}
    558 		break;
    559 
    560 	case PRU_SENSE:
    561 		/*
    562 		 * stat: don't bother with a blocksize.
    563 		 */
    564 		splx(s);
    565 		return (0);
    566 
    567 	case PRU_RCVOOB:
    568 		error = EOPNOTSUPP;
    569 		break;
    570 
    571 	case PRU_SENDOOB:
    572 		m_freem(control);
    573 		m_freem(m);
    574 		error = EOPNOTSUPP;
    575 		break;
    576 
    577 	case PRU_SOCKADDR:
    578 		in_setsockaddr(inp, nam);
    579 		break;
    580 
    581 	case PRU_PEERADDR:
    582 		in_setpeeraddr(inp, nam);
    583 		break;
    584 
    585 	default:
    586 		panic("rip_usrreq");
    587 	}
    588 
    589 release:
    590 	splx(s);
    591 	return (error);
    592 }
    593