Home | History | Annotate | Line # | Download | only in netinet
raw_ip.c revision 1.21
      1 /*	$NetBSD: raw_ip.c,v 1.21 1995/06/18 20:01:15 cgd 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 	switch (req) {
    259 
    260 	case PRU_ATTACH:
    261 		if (inp)
    262 			panic("rip_attach");
    263 		if ((so->so_state & SS_PRIV) == 0) {
    264 			error = EACCES;
    265 			break;
    266 		}
    267 		if ((error = soreserve(so, rip_sendspace, rip_recvspace)) ||
    268 		    (error = in_pcballoc(so, &rawcbtable)))
    269 			break;
    270 		inp = (struct inpcb *)so->so_pcb;
    271 		inp->inp_ip.ip_p = (long)nam;
    272 		break;
    273 
    274 	case PRU_DISCONNECT:
    275 		if ((so->so_state & SS_ISCONNECTED) == 0) {
    276 			error = ENOTCONN;
    277 			break;
    278 		}
    279 		/* FALLTHROUGH */
    280 	case PRU_ABORT:
    281 		soisdisconnected(so);
    282 		/* FALLTHROUGH */
    283 	case PRU_DETACH:
    284 		if (inp == 0)
    285 			panic("rip_detach");
    286 #ifdef MROUTING
    287 		if (so == ip_mrouter)
    288 			ip_mrouter_done();
    289 #endif
    290 		in_pcbdetach(inp);
    291 		break;
    292 
    293 	case PRU_BIND:
    294 	    {
    295 		struct sockaddr_in *addr = mtod(nam, struct sockaddr_in *);
    296 
    297 		if (nam->m_len != sizeof(*addr)) {
    298 			error = EINVAL;
    299 			break;
    300 		}
    301 		if ((ifnet.tqh_first == 0) ||
    302 		    ((addr->sin_family != AF_INET) &&
    303 		     (addr->sin_family != AF_IMPLINK)) ||
    304 		    (addr->sin_addr.s_addr &&
    305 		     ifa_ifwithaddr(sintosa(addr)) == 0)) {
    306 			error = EADDRNOTAVAIL;
    307 			break;
    308 		}
    309 		inp->inp_laddr = addr->sin_addr;
    310 		break;
    311 	    }
    312 	case PRU_CONNECT:
    313 	    {
    314 		struct sockaddr_in *addr = mtod(nam, struct sockaddr_in *);
    315 
    316 		if (nam->m_len != sizeof(*addr)) {
    317 			error = EINVAL;
    318 			break;
    319 		}
    320 		if (ifnet.tqh_first == 0) {
    321 			error = EADDRNOTAVAIL;
    322 			break;
    323 		}
    324 		if ((addr->sin_family != AF_INET) &&
    325 		     (addr->sin_family != AF_IMPLINK)) {
    326 			error = EAFNOSUPPORT;
    327 			break;
    328 		}
    329 		inp->inp_faddr = addr->sin_addr;
    330 		soisconnected(so);
    331 		break;
    332 	    }
    333 
    334 	case PRU_CONNECT2:
    335 		error = EOPNOTSUPP;
    336 		break;
    337 
    338 	/*
    339 	 * Mark the connection as being incapable of further input.
    340 	 */
    341 	case PRU_SHUTDOWN:
    342 		socantsendmore(so);
    343 		break;
    344 
    345 	/*
    346 	 * Ship a packet out.  The appropriate raw output
    347 	 * routine handles any massaging necessary.
    348 	 */
    349 	case PRU_SEND:
    350 	    {
    351 		register u_int32_t dst;
    352 
    353 		if (so->so_state & SS_ISCONNECTED) {
    354 			if (nam) {
    355 				error = EISCONN;
    356 				break;
    357 			}
    358 			dst = inp->inp_faddr.s_addr;
    359 		} else {
    360 			if (nam == NULL) {
    361 				error = ENOTCONN;
    362 				break;
    363 			}
    364 			dst = mtod(nam, struct sockaddr_in *)->sin_addr.s_addr;
    365 		}
    366 		error = rip_output(m, so, dst);
    367 		m = NULL;
    368 		break;
    369 	    }
    370 
    371 	case PRU_SENSE:
    372 		/*
    373 		 * stat: don't bother with a blocksize.
    374 		 */
    375 		return (0);
    376 
    377 	/*
    378 	 * Not supported.
    379 	 */
    380 	case PRU_RCVOOB:
    381 	case PRU_RCVD:
    382 	case PRU_LISTEN:
    383 	case PRU_ACCEPT:
    384 	case PRU_SENDOOB:
    385 		error = EOPNOTSUPP;
    386 		break;
    387 
    388 	case PRU_SOCKADDR:
    389 		in_setsockaddr(inp, nam);
    390 		break;
    391 
    392 	case PRU_PEERADDR:
    393 		in_setpeeraddr(inp, nam);
    394 		break;
    395 
    396 	default:
    397 		panic("rip_usrreq");
    398 	}
    399 	if (m != NULL)
    400 		m_freem(m);
    401 	return (error);
    402 }
    403