This commit was generated by cvs2svn to track changes on a CVS vendor
[unix-history] / sys / netinet / raw_ip.c
CommitLineData
15637ed4
RG
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 *
38e82238 33 * from: @(#)raw_ip.c 7.8 (Berkeley) 7/25/90
fde1aeb2 34 * $Id: raw_ip.c,v 1.3 1993/11/25 01:35:11 wollman Exp $
15637ed4
RG
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 */
fde1aeb2
GW
58static struct sockaddr_in ripdst = { sizeof(ripdst), AF_INET };
59static struct sockaddr_in ripsrc = { sizeof(ripsrc), AF_INET };
60static struct sockproto ripproto = { PF_INET };
15637ed4 61
15637ed4
RG
62/*
63 * Setup generic address and protocol structures
64 * for raw_input routine, then pass them along with
65 * mbuf chain.
66 */
4c45483e 67void
15637ed4
RG
68rip_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))
4c45483e 88int
15637ed4
RG
89rip_output(m, so)
90 register struct mbuf *m;
91 struct socket *so;
92{
93 register struct ip *ip;
94 register struct raw_inpcb *rp = sotorawinpcb(so);
95 register struct sockaddr_in *sin;
96
97 /*
98 * If the user handed us a complete IP packet, use it.
99 * Otherwise, allocate an mbuf for a header and fill it in.
100 */
101 if (rp->rinp_flags & RINPF_HDRINCL)
102 ip = mtod(m, struct ip *);
103 else {
104 M_PREPEND(m, sizeof(struct ip), M_WAIT);
105 ip = mtod(m, struct ip *);
106 ip->ip_tos = 0;
107 ip->ip_off = 0;
108 ip->ip_p = rp->rinp_rcb.rcb_proto.sp_protocol;
109 ip->ip_len = m->m_pkthdr.len;
110 if (sin = satosin(rp->rinp_rcb.rcb_laddr)) {
111 ip->ip_src = sin->sin_addr;
112 } else
113 ip->ip_src.s_addr = 0;
114 if (sin = satosin(rp->rinp_rcb.rcb_faddr))
115 ip->ip_dst = sin->sin_addr;
116 ip->ip_ttl = MAXTTL;
117 }
118 return (ip_output(m,
119 (rp->rinp_flags & RINPF_HDRINCL)? (struct mbuf *)0: rp->rinp_options,
120 &rp->rinp_route,
121 (so->so_options & SO_DONTROUTE) | IP_ALLOWBROADCAST));
122}
123
124/*
125 * Raw IP socket option processing.
126 */
4c45483e 127int
15637ed4
RG
128rip_ctloutput(op, so, level, optname, m)
129 int op;
130 struct socket *so;
131 int level, optname;
132 struct mbuf **m;
133{
134 int error = 0;
135 register struct raw_inpcb *rp = sotorawinpcb(so);
136
137 if (level != IPPROTO_IP)
138 error = EINVAL;
139 else switch (op) {
140
141 case PRCO_SETOPT:
142 switch (optname) {
143
144 case IP_OPTIONS:
145 return (ip_pcbopts(&rp->rinp_options, *m));
146
147 case IP_HDRINCL:
148 if (m == 0 || *m == 0 || (*m)->m_len < sizeof (int)) {
149 error = EINVAL;
150 break;
151 }
152 if (*mtod(*m, int *))
153 rp->rinp_flags |= RINPF_HDRINCL;
154 else
155 rp->rinp_flags &= ~RINPF_HDRINCL;
156 break;
157
158 default:
159 error = EINVAL;
160 break;
161 }
162 break;
163
164 case PRCO_GETOPT:
165 *m = m_get(M_WAIT, MT_SOOPTS);
166 switch (optname) {
167
168 case IP_OPTIONS:
169 if (rp->rinp_options) {
170 (*m)->m_len = rp->rinp_options->m_len;
171 bcopy(mtod(rp->rinp_options, caddr_t),
172 mtod(*m, caddr_t), (unsigned)(*m)->m_len);
173 } else
174 (*m)->m_len = 0;
175 break;
176
177 case IP_HDRINCL:
178 (*m)->m_len = sizeof (int);
179 *mtod(*m, int *) = rp->rinp_flags & RINPF_HDRINCL;
180 break;
181
182 default:
183 error = EINVAL;
184 m_freem(*m);
185 *m = 0;
186 break;
187 }
188 break;
189 }
190 if (op == PRCO_SETOPT && *m)
191 (void)m_free(*m);
192 return (error);
193}
194
195/*ARGSUSED*/
4c45483e 196int
15637ed4
RG
197rip_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 }
fde1aeb2 261 error = raw_usrreq(so, req, m, nam, control, 0);
15637ed4
RG
262
263 if (error && (req == PRU_ATTACH) && so->so_pcb)
264 free(so->so_pcb, M_PCB);
265 return (error);
266}