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