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