raw_ip.c revision 1.25 1 /* $NetBSD: raw_ip.c,v 1.25 1996/02/18 18:58:33 christos 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 #include <netinet/in_var.h>
57
58 #include <machine/stdarg.h>
59
60 struct inpcbtable rawcbtable;
61
62 /*
63 * Nominal space allocated to a raw ip socket.
64 */
65 #define RIPSNDQ 8192
66 #define RIPRCVQ 8192
67
68 /*
69 * Raw interface to IP protocol.
70 */
71
72 /*
73 * Initialize raw connection block q.
74 */
75 void
76 rip_init()
77 {
78
79 in_pcbinit(&rawcbtable, 1);
80 }
81
82 struct sockaddr_in ripsrc = { sizeof(ripsrc), AF_INET };
83 /*
84 * Setup generic address and protocol structures
85 * for raw_input routine, then pass them along with
86 * mbuf chain.
87 */
88 void
89 #if __STDC__
90 rip_input(struct mbuf *m, ...)
91 #else
92 rip_input(m, va_alist)
93 struct mbuf *m;
94 va_dcl
95 #endif
96 {
97 register struct ip *ip = mtod(m, struct ip *);
98 register struct inpcb *inp;
99 struct socket *last = 0;
100
101 ripsrc.sin_addr = ip->ip_src;
102 for (inp = rawcbtable.inpt_queue.cqh_first;
103 inp != (struct inpcb *)&rawcbtable.inpt_queue;
104 inp = inp->inp_queue.cqe_next) {
105 if (inp->inp_ip.ip_p && inp->inp_ip.ip_p != ip->ip_p)
106 continue;
107 if (inp->inp_laddr.s_addr &&
108 inp->inp_laddr.s_addr != ip->ip_dst.s_addr)
109 continue;
110 if (inp->inp_faddr.s_addr &&
111 inp->inp_faddr.s_addr != ip->ip_src.s_addr)
112 continue;
113 if (last) {
114 struct mbuf *n;
115 if ((n = m_copy(m, 0, (int)M_COPYALL)) != NULL) {
116 if (sbappendaddr(&last->so_rcv,
117 sintosa(&ripsrc), n,
118 (struct mbuf *)0) == 0)
119 /* should notify about lost packet */
120 m_freem(n);
121 else
122 sorwakeup(last);
123 }
124 }
125 last = inp->inp_socket;
126 }
127 if (last) {
128 if (sbappendaddr(&last->so_rcv, sintosa(&ripsrc), m,
129 (struct mbuf *)0) == 0)
130 m_freem(m);
131 else
132 sorwakeup(last);
133 } else {
134 m_freem(m);
135 ipstat.ips_noproto++;
136 ipstat.ips_delivered--;
137 }
138 }
139
140 /*
141 * Generate IP header and pass packet to ip_output.
142 * Tack on options user may have setup with control call.
143 */
144 int
145 #if __STDC__
146 rip_output(struct mbuf *m, ...)
147 #else
148 rip_output(m, va_alist)
149 struct mbuf *m;
150 va_dcl
151 #endif
152 {
153 struct socket *so;
154 u_long dst;
155 register struct ip *ip;
156 register struct inpcb *inp;
157 struct mbuf *opts;
158 int flags;
159 va_list ap;
160
161 va_start(ap, m);
162 so = va_arg(ap, struct socket *);
163 dst = va_arg(ap, u_long);
164 va_end(ap);
165
166 inp = sotoinpcb(so);
167 flags = (so->so_options & SO_DONTROUTE) | IP_ALLOWBROADCAST;
168
169 /*
170 * If the user handed us a complete IP packet, use it.
171 * Otherwise, allocate an mbuf for a header and fill it in.
172 */
173 if ((inp->inp_flags & INP_HDRINCL) == 0) {
174 M_PREPEND(m, sizeof(struct ip), M_WAIT);
175 ip = mtod(m, struct ip *);
176 ip->ip_tos = 0;
177 ip->ip_off = 0;
178 ip->ip_p = inp->inp_ip.ip_p;
179 ip->ip_len = m->m_pkthdr.len;
180 ip->ip_src = inp->inp_laddr;
181 ip->ip_dst.s_addr = dst;
182 ip->ip_ttl = MAXTTL;
183 opts = inp->inp_options;
184 } else {
185 ip = mtod(m, struct ip *);
186 if (ip->ip_id == 0)
187 ip->ip_id = htons(ip_id++);
188 opts = NULL;
189 /* XXX prevent ip_output from overwriting header fields */
190 flags |= IP_RAWOUTPUT;
191 ipstat.ips_rawout++;
192 }
193 return (ip_output(m, opts, &inp->inp_route, flags, inp->inp_moptions));
194 }
195
196 /*
197 * Raw IP socket option processing.
198 */
199 int
200 rip_ctloutput(op, so, level, optname, m)
201 int op;
202 struct socket *so;
203 int level, optname;
204 struct mbuf **m;
205 {
206 register struct inpcb *inp = sotoinpcb(so);
207 #ifdef MROUTING
208 int error;
209 #endif
210
211 if (level != IPPROTO_IP) {
212 if (m != 0 && *m != 0)
213 (void)m_free(*m);
214 return (EINVAL);
215 }
216
217 switch (optname) {
218
219 case IP_HDRINCL:
220 if (op == PRCO_SETOPT || op == PRCO_GETOPT) {
221 if (m == 0 || *m == 0 || (*m)->m_len < sizeof (int))
222 return (EINVAL);
223 if (op == PRCO_SETOPT) {
224 if (*mtod(*m, int *))
225 inp->inp_flags |= INP_HDRINCL;
226 else
227 inp->inp_flags &= ~INP_HDRINCL;
228 (void)m_free(*m);
229 } else {
230 (*m)->m_len = sizeof (int);
231 *mtod(*m, int *) = inp->inp_flags & INP_HDRINCL;
232 }
233 return (0);
234 }
235 break;
236
237 case MRT_INIT:
238 case MRT_DONE:
239 case MRT_ADD_VIF:
240 case MRT_DEL_VIF:
241 case MRT_ADD_MFC:
242 case MRT_DEL_MFC:
243 case MRT_VERSION:
244 case MRT_ASSERT:
245 #ifdef MROUTING
246 switch (op) {
247 case PRCO_SETOPT:
248 error = ip_mrouter_set(optname, so, m);
249 break;
250 case PRCO_GETOPT:
251 error = ip_mrouter_get(optname, so, m);
252 break;
253 default:
254 error = EINVAL;
255 break;
256 }
257 return (error);
258 #else
259 if (op == PRCO_SETOPT && *m)
260 m_free(*m);
261 return (EOPNOTSUPP);
262 #endif
263 }
264 return (ip_ctloutput(op, so, level, optname, m));
265 }
266
267 u_long rip_sendspace = RIPSNDQ;
268 u_long rip_recvspace = RIPRCVQ;
269
270 /*ARGSUSED*/
271 int
272 rip_usrreq(so, req, m, nam, control)
273 register struct socket *so;
274 int req;
275 struct mbuf *m, *nam, *control;
276 {
277 register int error = 0;
278 register struct inpcb *inp = sotoinpcb(so);
279 #ifdef MROUTING
280 extern struct socket *ip_mrouter;
281 #endif
282 if (req == PRU_CONTROL)
283 return (in_control(so, (long)m, (caddr_t)nam,
284 (struct ifnet *)control));
285
286 if (inp == NULL && req != PRU_ATTACH) {
287 error = EINVAL;
288 goto release;
289 }
290
291 switch (req) {
292
293 case PRU_ATTACH:
294 if (inp)
295 panic("rip_attach");
296 if ((so->so_state & SS_PRIV) == 0) {
297 error = EACCES;
298 break;
299 }
300 if ((error = soreserve(so, rip_sendspace, rip_recvspace)) ||
301 (error = in_pcballoc(so, &rawcbtable)))
302 break;
303 inp = (struct inpcb *)so->so_pcb;
304 inp->inp_ip.ip_p = (long)nam;
305 break;
306
307 case PRU_DISCONNECT:
308 if ((so->so_state & SS_ISCONNECTED) == 0) {
309 error = ENOTCONN;
310 break;
311 }
312 /* FALLTHROUGH */
313 case PRU_ABORT:
314 soisdisconnected(so);
315 /* FALLTHROUGH */
316 case PRU_DETACH:
317 if (inp == 0)
318 panic("rip_detach");
319 #ifdef MROUTING
320 if (so == ip_mrouter)
321 ip_mrouter_done();
322 #endif
323 in_pcbdetach(inp);
324 break;
325
326 case PRU_BIND:
327 {
328 struct sockaddr_in *addr = mtod(nam, struct sockaddr_in *);
329
330 if (nam->m_len != sizeof(*addr)) {
331 error = EINVAL;
332 break;
333 }
334 if ((ifnet.tqh_first == 0) ||
335 ((addr->sin_family != AF_INET) &&
336 (addr->sin_family != AF_IMPLINK)) ||
337 (addr->sin_addr.s_addr &&
338 ifa_ifwithaddr(sintosa(addr)) == 0)) {
339 error = EADDRNOTAVAIL;
340 break;
341 }
342 inp->inp_laddr = addr->sin_addr;
343 break;
344 }
345 case PRU_CONNECT:
346 {
347 struct sockaddr_in *addr = mtod(nam, struct sockaddr_in *);
348
349 if (nam->m_len != sizeof(*addr)) {
350 error = EINVAL;
351 break;
352 }
353 if (ifnet.tqh_first == 0) {
354 error = EADDRNOTAVAIL;
355 break;
356 }
357 if ((addr->sin_family != AF_INET) &&
358 (addr->sin_family != AF_IMPLINK)) {
359 error = EAFNOSUPPORT;
360 break;
361 }
362 inp->inp_faddr = addr->sin_addr;
363 soisconnected(so);
364 break;
365 }
366
367 case PRU_CONNECT2:
368 error = EOPNOTSUPP;
369 break;
370
371 /*
372 * Mark the connection as being incapable of further input.
373 */
374 case PRU_SHUTDOWN:
375 socantsendmore(so);
376 break;
377
378 /*
379 * Ship a packet out. The appropriate raw output
380 * routine handles any massaging necessary.
381 */
382 case PRU_SEND:
383 {
384 register u_int32_t dst;
385
386 if (so->so_state & SS_ISCONNECTED) {
387 if (nam) {
388 error = EISCONN;
389 break;
390 }
391 dst = inp->inp_faddr.s_addr;
392 } else {
393 if (nam == NULL) {
394 error = ENOTCONN;
395 break;
396 }
397 dst = mtod(nam, struct sockaddr_in *)->sin_addr.s_addr;
398 }
399 error = rip_output(m, so, dst);
400 m = NULL;
401 break;
402 }
403
404 case PRU_SENSE:
405 /*
406 * stat: don't bother with a blocksize.
407 */
408 return (0);
409
410 /*
411 * Not supported.
412 */
413 case PRU_RCVOOB:
414 case PRU_RCVD:
415 case PRU_LISTEN:
416 case PRU_ACCEPT:
417 case PRU_SENDOOB:
418 error = EOPNOTSUPP;
419 break;
420
421 case PRU_SOCKADDR:
422 in_setsockaddr(inp, nam);
423 break;
424
425 case PRU_PEERADDR:
426 in_setpeeraddr(inp, nam);
427 break;
428
429 default:
430 panic("rip_usrreq");
431 }
432 release:
433 if (m != NULL)
434 m_freem(m);
435 return (error);
436 }
437