Home | History | Annotate | Line # | Download | only in netinet
raw_ip.c revision 1.6
      1 /*
      2  * Copyright (c) 1982, 1986, 1988 Regents of the University of California.
      3  * 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  *	from: @(#)raw_ip.c	7.8 (Berkeley) 7/25/90
     34  *	$Id: raw_ip.c,v 1.6 1993/12/06 04:59:44 hpeyerl Exp $
     35  */
     36 
     37 #include "param.h"
     38 #include "malloc.h"
     39 #include "mbuf.h"
     40 #include "socket.h"
     41 #include "protosw.h"
     42 #include "socketvar.h"
     43 #include "errno.h"
     44 
     45 #include "../net/if.h"
     46 #include "../net/route.h"
     47 #include "../net/raw_cb.h"
     48 
     49 #include "in.h"
     50 #include "in_systm.h"
     51 #include "ip.h"
     52 #include "ip_var.h"
     53 #include "in_pcb.h"
     54 
     55 /*
     56  * Raw interface to IP protocol.
     57  */
     58 
     59 struct	sockaddr_in ripdst = { sizeof(ripdst), AF_INET };
     60 struct	sockaddr_in ripsrc = { sizeof(ripsrc), AF_INET };
     61 struct	sockproto ripproto = { PF_INET };
     62 /*
     63  * Setup generic address and protocol structures
     64  * for raw_input routine, then pass them along with
     65  * mbuf chain.
     66  */
     67 rip_input(m)
     68 	struct mbuf *m;
     69 {
     70 	register struct ip *ip = mtod(m, struct ip *);
     71 
     72 	ripproto.sp_protocol = ip->ip_p;
     73 	ripdst.sin_addr = ip->ip_dst;
     74 	ripsrc.sin_addr = ip->ip_src;
     75 	if (raw_input(m, &ripproto, (struct sockaddr *)&ripsrc,
     76 	  (struct sockaddr *)&ripdst) == 0) {
     77 		ipstat.ips_noproto++;
     78 		ipstat.ips_delivered--;
     79 	}
     80 }
     81 
     82 /*
     83  * Generate IP header and pass packet to ip_output.
     84  * Tack on options user may have setup with control call.
     85  */
     86 #define	satosin(sa)	((struct sockaddr_in *)(sa))
     87 rip_output(m, so)
     88 	register struct mbuf *m;
     89 	struct socket *so;
     90 {
     91 	register struct ip *ip;
     92 	register struct raw_inpcb *rp = sotorawinpcb(so);
     93 	register struct sockaddr_in *sin;
     94 
     95 	/*
     96 	 * If the user handed us a complete IP packet, use it.
     97 	 * Otherwise, allocate an mbuf for a header and fill it in.
     98 	 */
     99 	if (rp->rinp_flags & RINPF_HDRINCL) {
    100 		ip = mtod(m, struct ip *);
    101 		if (ip->ip_len > m->m_pkthdr.len)
    102 			return EMSGSIZE;
    103 		ip->ip_len = m->m_pkthdr.len;
    104 	} else {
    105 		M_PREPEND(m, sizeof(struct ip), M_WAIT);
    106 		ip = mtod(m, struct ip *);
    107 		ip->ip_tos = 0;
    108 		ip->ip_off = 0;
    109 		ip->ip_p = rp->rinp_rcb.rcb_proto.sp_protocol;
    110 		ip->ip_len = m->m_pkthdr.len;
    111 		if (sin = satosin(rp->rinp_rcb.rcb_laddr)) {
    112 			ip->ip_src = sin->sin_addr;
    113 		} else
    114 			ip->ip_src.s_addr = 0;
    115 		if (sin = satosin(rp->rinp_rcb.rcb_faddr))
    116 		    ip->ip_dst = sin->sin_addr;
    117 		ip->ip_ttl = MAXTTL;
    118 	}
    119 	return (ip_output(m,
    120 	   (rp->rinp_flags & RINPF_HDRINCL)? (struct mbuf *)0: rp->rinp_options,
    121 	    &rp->rinp_route,
    122 	   (so->so_options & SO_DONTROUTE) | IP_ALLOWBROADCAST
    123 #ifdef MULTICAST
    124 		| IP_MULTICASTOPTS, rp->rinp_rcb.rcb_moptions
    125 #endif
    126 	));
    127 }
    128 
    129 /*
    130  * Raw IP socket option processing.
    131  */
    132 rip_ctloutput(op, so, level, optname, m)
    133 	int op;
    134 	struct socket *so;
    135 	int level, optname;
    136 	struct mbuf **m;
    137 {
    138 	int error = 0;
    139 	register struct raw_inpcb *rp = sotorawinpcb(so);
    140 
    141 	if (level != IPPROTO_IP)
    142 		error = EINVAL;
    143 	else switch (op) {
    144 
    145 	case PRCO_SETOPT:
    146 		switch (optname) {
    147 
    148 		case IP_OPTIONS:
    149 			return (ip_pcbopts(&rp->rinp_options, *m));
    150 
    151 		case IP_HDRINCL:
    152 			if (m == 0 || *m == 0 || (*m)->m_len < sizeof (int)) {
    153 				error = EINVAL;
    154 				break;
    155 			}
    156 			if (*mtod(*m, int *))
    157 				rp->rinp_flags |= RINPF_HDRINCL;
    158 			else
    159 				rp->rinp_flags &= ~RINPF_HDRINCL;
    160 			break;
    161 #ifdef MULTICAST
    162 		case IP_MULTICAST_IF:
    163 		case IP_MULTICAST_TTL:
    164 		case IP_MULTICAST_LOOP:
    165 		case IP_ADD_MEMBERSHIP:
    166 		case IP_DROP_MEMBERSHIP:
    167 			error = ip_setmoptions(optname,
    168 					       &rp->rinp_rcb.rcb_moptions, *m);
    169 			break;
    170 		default:
    171 #ifdef MROUTING
    172 			error = ip_mrouter_cmd(optname, so, *m);
    173 #else
    174 			error = EINVAL;
    175 #endif
    176 			break;
    177 #else
    178 		default:
    179 			error = EINVAL;
    180 			break;
    181 #endif
    182 		}
    183 		break;
    184 
    185 	case PRCO_GETOPT:
    186 		*m = m_get(M_WAIT, MT_SOOPTS);
    187 		switch (optname) {
    188 
    189 		case IP_OPTIONS:
    190 			if (rp->rinp_options) {
    191 				(*m)->m_len = rp->rinp_options->m_len;
    192 				bcopy(mtod(rp->rinp_options, caddr_t),
    193 				    mtod(*m, caddr_t), (unsigned)(*m)->m_len);
    194 			} else
    195 				(*m)->m_len = 0;
    196 			break;
    197 
    198 		case IP_HDRINCL:
    199 			(*m)->m_len = sizeof (int);
    200 			*mtod(*m, int *) = rp->rinp_flags & RINPF_HDRINCL;
    201 			break;
    202 #ifdef MULTICAST
    203 		case IP_MULTICAST_IF:
    204 		case IP_MULTICAST_TTL:
    205 		case IP_MULTICAST_LOOP:
    206 		case IP_ADD_MEMBERSHIP:
    207 		case IP_DROP_MEMBERSHIP:
    208 			error = ip_getmoptions(optname,
    209 						rp->rinp_rcb.rcb_moptions, m);
    210                       break;
    211 #endif
    212 		default:
    213 			error = EINVAL;
    214 			m_freem(*m);
    215 			*m = 0;
    216 			break;
    217 		}
    218 		break;
    219 	}
    220 	if (op == PRCO_SETOPT && *m)
    221 		(void)m_free(*m);
    222 	return (error);
    223 }
    224 
    225 /*ARGSUSED*/
    226 rip_usrreq(so, req, m, nam, control)
    227 	register struct socket *so;
    228 	int req;
    229 	struct mbuf *m, *nam, *control;
    230 {
    231 	register int error = 0;
    232 	register struct raw_inpcb *rp = sotorawinpcb(so);
    233 #if defined(MULTICAST) && defined(MROUTING)
    234 	extern struct socket *ip_mrouter;
    235 #endif
    236 	switch (req) {
    237 
    238 	case PRU_ATTACH:
    239 		if (rp)
    240 			panic("rip_attach");
    241 		MALLOC(rp, struct raw_inpcb *, sizeof *rp, M_PCB, M_WAITOK);
    242 		if (rp == 0)
    243 			return (ENOBUFS);
    244 		bzero((caddr_t)rp, sizeof *rp);
    245 		so->so_pcb = (caddr_t)rp;
    246 		break;
    247 
    248 	case PRU_DETACH:
    249 		if (rp == 0)
    250 			panic("rip_detach");
    251 #if defined(MULTICAST) && defined(MROUTING)
    252 		if (so == ip_mrouter)
    253 			ip_mrouter_done();
    254 #endif
    255 		if (rp->rinp_options)
    256 			m_freem(rp->rinp_options);
    257 #ifdef MULTICAST
    258 		if (rp->rinp_rcb.rcb_moptions)
    259 			ip_freemoptions(rp->rinp_rcb.rcb_moptions);
    260 #endif
    261 		if (rp->rinp_route.ro_rt)
    262 			RTFREE(rp->rinp_route.ro_rt);
    263 		if (rp->rinp_rcb.rcb_laddr)
    264 			rp->rinp_rcb.rcb_laddr = 0;
    265 		break;
    266 
    267 	case PRU_BIND:
    268 	    {
    269 		struct sockaddr_in *addr = mtod(nam, struct sockaddr_in *);
    270 
    271 		if (nam->m_len != sizeof(*addr))
    272 			return (EINVAL);
    273 		if ((ifnet == 0) ||
    274 		    ((addr->sin_family != AF_INET) &&
    275 		     (addr->sin_family != AF_IMPLINK)) ||
    276 		    (addr->sin_addr.s_addr &&
    277 		     ifa_ifwithaddr((struct sockaddr *)addr) == 0))
    278 			return (EADDRNOTAVAIL);
    279 		rp->rinp_rcb.rcb_laddr = (struct sockaddr *)&rp->rinp_laddr;
    280 		rp->rinp_laddr = *addr;
    281 		return (0);
    282 	    }
    283 	case PRU_CONNECT:
    284 	    {
    285 		struct sockaddr_in *addr = mtod(nam, struct sockaddr_in *);
    286 
    287 		if (nam->m_len != sizeof(*addr))
    288 			return (EINVAL);
    289 		if (ifnet == 0)
    290 			return (EADDRNOTAVAIL);
    291 		if ((addr->sin_family != AF_INET) &&
    292 		     (addr->sin_family != AF_IMPLINK))
    293 			return (EAFNOSUPPORT);
    294 		rp->rinp_rcb.rcb_faddr = (struct sockaddr *)&rp->rinp_faddr;
    295 		rp->rinp_faddr = *addr;
    296 		soisconnected(so);
    297 		return (0);
    298 	    }
    299 	}
    300 	error =  raw_usrreq(so, req, m, nam, control);
    301 
    302 	if (error && (req == PRU_ATTACH) && so->so_pcb)
    303 		free(so->so_pcb, M_PCB);
    304 	return (error);
    305 }
    306