raw_ip.c revision 1.31 1 1.31 mycroft /* $NetBSD: raw_ip.c,v 1.31 1996/06/23 12:12:49 mycroft Exp $ */
2 1.14 cgd
3 1.1 cgd /*
4 1.13 mycroft * Copyright (c) 1982, 1986, 1988, 1993
5 1.13 mycroft * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * Redistribution and use in source and binary forms, with or without
8 1.1 cgd * modification, are permitted provided that the following conditions
9 1.1 cgd * are met:
10 1.1 cgd * 1. Redistributions of source code must retain the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer.
12 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 cgd * notice, this list of conditions and the following disclaimer in the
14 1.1 cgd * documentation and/or other materials provided with the distribution.
15 1.1 cgd * 3. All advertising materials mentioning features or use of this software
16 1.1 cgd * must display the following acknowledgement:
17 1.1 cgd * This product includes software developed by the University of
18 1.1 cgd * California, Berkeley and its contributors.
19 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
20 1.1 cgd * may be used to endorse or promote products derived from this software
21 1.1 cgd * without specific prior written permission.
22 1.1 cgd *
23 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 1.1 cgd * SUCH DAMAGE.
34 1.1 cgd *
35 1.14 cgd * @(#)raw_ip.c 8.2 (Berkeley) 1/4/94
36 1.1 cgd */
37 1.1 cgd
38 1.7 mycroft #include <sys/param.h>
39 1.7 mycroft #include <sys/malloc.h>
40 1.7 mycroft #include <sys/mbuf.h>
41 1.7 mycroft #include <sys/socket.h>
42 1.7 mycroft #include <sys/protosw.h>
43 1.7 mycroft #include <sys/socketvar.h>
44 1.7 mycroft #include <sys/errno.h>
45 1.13 mycroft #include <sys/systm.h>
46 1.26 mycroft #include <sys/proc.h>
47 1.1 cgd
48 1.7 mycroft #include <net/if.h>
49 1.7 mycroft #include <net/route.h>
50 1.1 cgd
51 1.7 mycroft #include <netinet/in.h>
52 1.7 mycroft #include <netinet/in_systm.h>
53 1.7 mycroft #include <netinet/ip.h>
54 1.7 mycroft #include <netinet/ip_var.h>
55 1.13 mycroft #include <netinet/ip_mroute.h>
56 1.7 mycroft #include <netinet/in_pcb.h>
57 1.24 christos #include <netinet/in_var.h>
58 1.24 christos
59 1.24 christos #include <machine/stdarg.h>
60 1.13 mycroft
61 1.20 mycroft struct inpcbtable rawcbtable;
62 1.30 pk
63 1.30 pk int rip_bind __P((struct inpcb *, struct mbuf *));
64 1.30 pk int rip_connect __P((struct inpcb *, struct mbuf *));
65 1.30 pk void rip_disconnect __P((struct inpcb *));
66 1.13 mycroft
67 1.13 mycroft /*
68 1.13 mycroft * Nominal space allocated to a raw ip socket.
69 1.13 mycroft */
70 1.13 mycroft #define RIPSNDQ 8192
71 1.13 mycroft #define RIPRCVQ 8192
72 1.1 cgd
73 1.1 cgd /*
74 1.1 cgd * Raw interface to IP protocol.
75 1.1 cgd */
76 1.13 mycroft
77 1.13 mycroft /*
78 1.13 mycroft * Initialize raw connection block q.
79 1.13 mycroft */
80 1.13 mycroft void
81 1.13 mycroft rip_init()
82 1.13 mycroft {
83 1.13 mycroft
84 1.23 mycroft in_pcbinit(&rawcbtable, 1);
85 1.13 mycroft }
86 1.13 mycroft
87 1.1 cgd struct sockaddr_in ripsrc = { sizeof(ripsrc), AF_INET };
88 1.1 cgd /*
89 1.1 cgd * Setup generic address and protocol structures
90 1.1 cgd * for raw_input routine, then pass them along with
91 1.1 cgd * mbuf chain.
92 1.1 cgd */
93 1.9 mycroft void
94 1.24 christos #if __STDC__
95 1.24 christos rip_input(struct mbuf *m, ...)
96 1.24 christos #else
97 1.24 christos rip_input(m, va_alist)
98 1.1 cgd struct mbuf *m;
99 1.24 christos va_dcl
100 1.24 christos #endif
101 1.1 cgd {
102 1.1 cgd register struct ip *ip = mtod(m, struct ip *);
103 1.13 mycroft register struct inpcb *inp;
104 1.13 mycroft struct socket *last = 0;
105 1.1 cgd
106 1.1 cgd ripsrc.sin_addr = ip->ip_src;
107 1.21 cgd for (inp = rawcbtable.inpt_queue.cqh_first;
108 1.21 cgd inp != (struct inpcb *)&rawcbtable.inpt_queue;
109 1.21 cgd inp = inp->inp_queue.cqe_next) {
110 1.13 mycroft if (inp->inp_ip.ip_p && inp->inp_ip.ip_p != ip->ip_p)
111 1.13 mycroft continue;
112 1.27 mycroft if (inp->inp_laddr.s_addr != INADDR_ANY &&
113 1.16 glass inp->inp_laddr.s_addr != ip->ip_dst.s_addr)
114 1.13 mycroft continue;
115 1.27 mycroft if (inp->inp_faddr.s_addr != INADDR_ANY &&
116 1.16 glass inp->inp_faddr.s_addr != ip->ip_src.s_addr)
117 1.13 mycroft continue;
118 1.13 mycroft if (last) {
119 1.13 mycroft struct mbuf *n;
120 1.24 christos if ((n = m_copy(m, 0, (int)M_COPYALL)) != NULL) {
121 1.13 mycroft if (sbappendaddr(&last->so_rcv,
122 1.19 mycroft sintosa(&ripsrc), n,
123 1.19 mycroft (struct mbuf *)0) == 0)
124 1.13 mycroft /* should notify about lost packet */
125 1.13 mycroft m_freem(n);
126 1.13 mycroft else
127 1.13 mycroft sorwakeup(last);
128 1.13 mycroft }
129 1.13 mycroft }
130 1.13 mycroft last = inp->inp_socket;
131 1.13 mycroft }
132 1.13 mycroft if (last) {
133 1.19 mycroft if (sbappendaddr(&last->so_rcv, sintosa(&ripsrc), m,
134 1.19 mycroft (struct mbuf *)0) == 0)
135 1.13 mycroft m_freem(m);
136 1.13 mycroft else
137 1.13 mycroft sorwakeup(last);
138 1.13 mycroft } else {
139 1.13 mycroft m_freem(m);
140 1.1 cgd ipstat.ips_noproto++;
141 1.1 cgd ipstat.ips_delivered--;
142 1.1 cgd }
143 1.1 cgd }
144 1.1 cgd
145 1.1 cgd /*
146 1.1 cgd * Generate IP header and pass packet to ip_output.
147 1.1 cgd * Tack on options user may have setup with control call.
148 1.1 cgd */
149 1.9 mycroft int
150 1.24 christos #if __STDC__
151 1.24 christos rip_output(struct mbuf *m, ...)
152 1.24 christos #else
153 1.24 christos rip_output(m, va_alist)
154 1.24 christos struct mbuf *m;
155 1.24 christos va_dcl
156 1.24 christos #endif
157 1.24 christos {
158 1.27 mycroft register struct inpcb *inp;
159 1.1 cgd register struct ip *ip;
160 1.10 mycroft struct mbuf *opts;
161 1.24 christos int flags;
162 1.24 christos va_list ap;
163 1.24 christos
164 1.24 christos va_start(ap, m);
165 1.27 mycroft inp = va_arg(ap, struct inpcb *);
166 1.24 christos va_end(ap);
167 1.24 christos
168 1.27 mycroft flags =
169 1.27 mycroft (inp->inp_socket->so_options & SO_DONTROUTE) | IP_ALLOWBROADCAST;
170 1.1 cgd
171 1.1 cgd /*
172 1.1 cgd * If the user handed us a complete IP packet, use it.
173 1.1 cgd * Otherwise, allocate an mbuf for a header and fill it in.
174 1.1 cgd */
175 1.13 mycroft if ((inp->inp_flags & INP_HDRINCL) == 0) {
176 1.1 cgd M_PREPEND(m, sizeof(struct ip), M_WAIT);
177 1.1 cgd ip = mtod(m, struct ip *);
178 1.1 cgd ip->ip_tos = 0;
179 1.1 cgd ip->ip_off = 0;
180 1.13 mycroft ip->ip_p = inp->inp_ip.ip_p;
181 1.1 cgd ip->ip_len = m->m_pkthdr.len;
182 1.13 mycroft ip->ip_src = inp->inp_laddr;
183 1.27 mycroft ip->ip_dst = inp->inp_faddr;
184 1.1 cgd ip->ip_ttl = MAXTTL;
185 1.13 mycroft opts = inp->inp_options;
186 1.13 mycroft } else {
187 1.13 mycroft ip = mtod(m, struct ip *);
188 1.13 mycroft if (ip->ip_id == 0)
189 1.13 mycroft ip->ip_id = htons(ip_id++);
190 1.13 mycroft opts = NULL;
191 1.13 mycroft /* XXX prevent ip_output from overwriting header fields */
192 1.13 mycroft flags |= IP_RAWOUTPUT;
193 1.13 mycroft ipstat.ips_rawout++;
194 1.1 cgd }
195 1.13 mycroft return (ip_output(m, opts, &inp->inp_route, flags, inp->inp_moptions));
196 1.1 cgd }
197 1.1 cgd
198 1.1 cgd /*
199 1.1 cgd * Raw IP socket option processing.
200 1.1 cgd */
201 1.9 mycroft int
202 1.1 cgd rip_ctloutput(op, so, level, optname, m)
203 1.1 cgd int op;
204 1.1 cgd struct socket *so;
205 1.1 cgd int level, optname;
206 1.1 cgd struct mbuf **m;
207 1.1 cgd {
208 1.13 mycroft register struct inpcb *inp = sotoinpcb(so);
209 1.31 mycroft int error = 0;
210 1.1 cgd
211 1.15 mycroft if (level != IPPROTO_IP) {
212 1.31 mycroft error = ENOPROTOOPT;
213 1.31 mycroft if (op == PRCO_SETOPT && *m != 0)
214 1.31 mycroft (void) m_free(*m);
215 1.31 mycroft } else switch (op) {
216 1.31 mycroft
217 1.31 mycroft case PRCO_SETOPT:
218 1.31 mycroft switch (optname) {
219 1.31 mycroft case IP_HDRINCL:
220 1.31 mycroft if (*m == 0 || (*m)->m_len < sizeof (int))
221 1.31 mycroft error = EINVAL;
222 1.31 mycroft else {
223 1.13 mycroft if (*mtod(*m, int *))
224 1.13 mycroft inp->inp_flags |= INP_HDRINCL;
225 1.13 mycroft else
226 1.13 mycroft inp->inp_flags &= ~INP_HDRINCL;
227 1.13 mycroft }
228 1.31 mycroft if (*m != 0)
229 1.31 mycroft (void) m_free(*m);
230 1.31 mycroft break;
231 1.31 mycroft
232 1.31 mycroft #ifdef MROUTING
233 1.31 mycroft case MRT_INIT:
234 1.31 mycroft case MRT_DONE:
235 1.31 mycroft case MRT_ADD_VIF:
236 1.31 mycroft case MRT_DEL_VIF:
237 1.31 mycroft case MRT_ADD_MFC:
238 1.31 mycroft case MRT_DEL_MFC:
239 1.31 mycroft case MRT_ASSERT:
240 1.31 mycroft error = ip_mrouter_set(so, optname, m);
241 1.31 mycroft break;
242 1.31 mycroft #endif
243 1.31 mycroft
244 1.31 mycroft default:
245 1.31 mycroft error = ip_ctloutput(op, so, level, optname, m);
246 1.31 mycroft break;
247 1.13 mycroft }
248 1.13 mycroft break;
249 1.1 cgd
250 1.31 mycroft case PRCO_GETOPT:
251 1.31 mycroft switch (optname) {
252 1.31 mycroft case IP_HDRINCL:
253 1.31 mycroft *m = m_get(M_WAIT, M_SOOPTS);
254 1.31 mycroft (*m)->m_len = sizeof (int);
255 1.31 mycroft *mtod(*m, int *) = inp->inp_flags & INP_HDRINCL ? 1 : 0;
256 1.31 mycroft break;
257 1.31 mycroft
258 1.6 hpeyerl #ifdef MROUTING
259 1.31 mycroft case MRT_VERSION:
260 1.31 mycroft case MRT_ASSERT:
261 1.31 mycroft error = ip_mrouter_get(so, optname, m);
262 1.18 mycroft break;
263 1.31 mycroft #endif
264 1.31 mycroft
265 1.18 mycroft default:
266 1.31 mycroft error = ip_ctloutput(op, so, level, optname, m);
267 1.18 mycroft break;
268 1.18 mycroft }
269 1.31 mycroft break;
270 1.1 cgd }
271 1.31 mycroft return (error);
272 1.1 cgd }
273 1.1 cgd
274 1.27 mycroft int
275 1.29 mycroft rip_bind(inp, nam)
276 1.29 mycroft struct inpcb *inp;
277 1.29 mycroft struct mbuf *nam;
278 1.29 mycroft {
279 1.29 mycroft struct sockaddr_in *addr = mtod(nam, struct sockaddr_in *);
280 1.29 mycroft
281 1.29 mycroft if (nam->m_len != sizeof(*addr))
282 1.29 mycroft return (EINVAL);
283 1.29 mycroft if (ifnet.tqh_first == 0)
284 1.29 mycroft return (EADDRNOTAVAIL);
285 1.29 mycroft if (addr->sin_family != AF_INET &&
286 1.29 mycroft addr->sin_family != AF_IMPLINK)
287 1.29 mycroft return (EAFNOSUPPORT);
288 1.29 mycroft if (addr->sin_addr.s_addr != INADDR_ANY &&
289 1.29 mycroft ifa_ifwithaddr(sintosa(addr)) == 0)
290 1.29 mycroft return (EADDRNOTAVAIL);
291 1.29 mycroft inp->inp_laddr = addr->sin_addr;
292 1.29 mycroft return (0);
293 1.29 mycroft }
294 1.29 mycroft
295 1.29 mycroft int
296 1.27 mycroft rip_connect(inp, nam)
297 1.27 mycroft struct inpcb *inp;
298 1.27 mycroft struct mbuf *nam;
299 1.27 mycroft {
300 1.27 mycroft struct sockaddr_in *addr = mtod(nam, struct sockaddr_in *);
301 1.27 mycroft
302 1.27 mycroft if (nam->m_len != sizeof(*addr))
303 1.27 mycroft return (EINVAL);
304 1.27 mycroft if (ifnet.tqh_first == 0)
305 1.27 mycroft return (EADDRNOTAVAIL);
306 1.27 mycroft if (addr->sin_family != AF_INET &&
307 1.27 mycroft addr->sin_family != AF_IMPLINK)
308 1.27 mycroft return (EAFNOSUPPORT);
309 1.27 mycroft inp->inp_faddr = addr->sin_addr;
310 1.27 mycroft return (0);
311 1.27 mycroft }
312 1.27 mycroft
313 1.27 mycroft void
314 1.27 mycroft rip_disconnect(inp)
315 1.27 mycroft struct inpcb *inp;
316 1.27 mycroft {
317 1.27 mycroft
318 1.27 mycroft inp->inp_faddr.s_addr = INADDR_ANY;
319 1.27 mycroft }
320 1.27 mycroft
321 1.13 mycroft u_long rip_sendspace = RIPSNDQ;
322 1.13 mycroft u_long rip_recvspace = RIPRCVQ;
323 1.13 mycroft
324 1.1 cgd /*ARGSUSED*/
325 1.9 mycroft int
326 1.26 mycroft rip_usrreq(so, req, m, nam, control, p)
327 1.1 cgd register struct socket *so;
328 1.1 cgd int req;
329 1.2 cgd struct mbuf *m, *nam, *control;
330 1.26 mycroft struct proc *p;
331 1.1 cgd {
332 1.27 mycroft register struct inpcb *inp;
333 1.27 mycroft int s;
334 1.1 cgd register int error = 0;
335 1.13 mycroft #ifdef MROUTING
336 1.6 hpeyerl extern struct socket *ip_mrouter;
337 1.6 hpeyerl #endif
338 1.27 mycroft
339 1.22 pk if (req == PRU_CONTROL)
340 1.22 pk return (in_control(so, (long)m, (caddr_t)nam,
341 1.26 mycroft (struct ifnet *)control, p));
342 1.22 pk
343 1.27 mycroft s = splsoftnet();
344 1.27 mycroft inp = sotoinpcb(so);
345 1.28 mycroft #ifdef DIAGNOSTIC
346 1.28 mycroft if (req != PRU_SEND && req != PRU_SENDOOB && control)
347 1.28 mycroft panic("rip_usrreq: unexpected control mbuf");
348 1.28 mycroft #endif
349 1.27 mycroft if (inp == 0 && req != PRU_ATTACH) {
350 1.22 pk error = EINVAL;
351 1.22 pk goto release;
352 1.22 pk }
353 1.22 pk
354 1.1 cgd switch (req) {
355 1.1 cgd
356 1.1 cgd case PRU_ATTACH:
357 1.27 mycroft if (inp != 0) {
358 1.27 mycroft error = EISCONN;
359 1.27 mycroft break;
360 1.27 mycroft }
361 1.26 mycroft if (p == 0 || (error = suser(p->p_ucred, &p->p_acflag))) {
362 1.13 mycroft error = EACCES;
363 1.13 mycroft break;
364 1.13 mycroft }
365 1.27 mycroft if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
366 1.27 mycroft error = soreserve(so, rip_sendspace, rip_recvspace);
367 1.27 mycroft if (error)
368 1.27 mycroft break;
369 1.27 mycroft }
370 1.27 mycroft error = in_pcballoc(so, &rawcbtable);
371 1.27 mycroft if (error)
372 1.13 mycroft break;
373 1.27 mycroft inp = sotoinpcb(so);
374 1.17 cgd inp->inp_ip.ip_p = (long)nam;
375 1.1 cgd break;
376 1.1 cgd
377 1.1 cgd case PRU_DETACH:
378 1.13 mycroft #ifdef MROUTING
379 1.6 hpeyerl if (so == ip_mrouter)
380 1.6 hpeyerl ip_mrouter_done();
381 1.6 hpeyerl #endif
382 1.13 mycroft in_pcbdetach(inp);
383 1.1 cgd break;
384 1.1 cgd
385 1.1 cgd case PRU_BIND:
386 1.29 mycroft error = rip_bind(inp, nam);
387 1.27 mycroft break;
388 1.27 mycroft
389 1.27 mycroft case PRU_LISTEN:
390 1.27 mycroft error = EOPNOTSUPP;
391 1.27 mycroft break;
392 1.27 mycroft
393 1.27 mycroft case PRU_CONNECT:
394 1.27 mycroft error = rip_connect(inp, nam);
395 1.27 mycroft if (error)
396 1.13 mycroft break;
397 1.1 cgd soisconnected(so);
398 1.13 mycroft break;
399 1.13 mycroft
400 1.13 mycroft case PRU_CONNECT2:
401 1.13 mycroft error = EOPNOTSUPP;
402 1.13 mycroft break;
403 1.13 mycroft
404 1.27 mycroft case PRU_DISCONNECT:
405 1.27 mycroft soisdisconnected(so);
406 1.27 mycroft rip_disconnect(inp);
407 1.27 mycroft break;
408 1.27 mycroft
409 1.13 mycroft /*
410 1.13 mycroft * Mark the connection as being incapable of further input.
411 1.13 mycroft */
412 1.13 mycroft case PRU_SHUTDOWN:
413 1.13 mycroft socantsendmore(so);
414 1.13 mycroft break;
415 1.13 mycroft
416 1.27 mycroft case PRU_RCVD:
417 1.27 mycroft error = EOPNOTSUPP;
418 1.27 mycroft break;
419 1.27 mycroft
420 1.13 mycroft /*
421 1.13 mycroft * Ship a packet out. The appropriate raw output
422 1.13 mycroft * routine handles any massaging necessary.
423 1.13 mycroft */
424 1.13 mycroft case PRU_SEND:
425 1.28 mycroft if (control && control->m_len) {
426 1.28 mycroft m_freem(control);
427 1.28 mycroft m_freem(m);
428 1.28 mycroft error = EINVAL;
429 1.28 mycroft break;
430 1.28 mycroft }
431 1.28 mycroft {
432 1.27 mycroft if (nam) {
433 1.27 mycroft if ((so->so_state & SS_ISCONNECTED) != 0) {
434 1.13 mycroft error = EISCONN;
435 1.28 mycroft goto die;
436 1.13 mycroft }
437 1.27 mycroft error = rip_connect(inp, nam);
438 1.27 mycroft if (error) {
439 1.28 mycroft die:
440 1.27 mycroft m_freem(m);
441 1.27 mycroft break;
442 1.27 mycroft }
443 1.13 mycroft } else {
444 1.27 mycroft if ((so->so_state & SS_ISCONNECTED) == 0) {
445 1.13 mycroft error = ENOTCONN;
446 1.28 mycroft goto die;
447 1.13 mycroft }
448 1.13 mycroft }
449 1.27 mycroft error = rip_output(m, inp);
450 1.27 mycroft if (nam)
451 1.27 mycroft rip_disconnect(inp);
452 1.28 mycroft }
453 1.13 mycroft break;
454 1.13 mycroft
455 1.13 mycroft case PRU_SENSE:
456 1.13 mycroft /*
457 1.13 mycroft * stat: don't bother with a blocksize.
458 1.13 mycroft */
459 1.27 mycroft splx(s);
460 1.1 cgd return (0);
461 1.13 mycroft
462 1.13 mycroft case PRU_RCVOOB:
463 1.27 mycroft error = EOPNOTSUPP;
464 1.27 mycroft break;
465 1.27 mycroft
466 1.13 mycroft case PRU_SENDOOB:
467 1.28 mycroft m_freem(control);
468 1.27 mycroft m_freem(m);
469 1.13 mycroft error = EOPNOTSUPP;
470 1.13 mycroft break;
471 1.13 mycroft
472 1.13 mycroft case PRU_SOCKADDR:
473 1.13 mycroft in_setsockaddr(inp, nam);
474 1.13 mycroft break;
475 1.13 mycroft
476 1.13 mycroft case PRU_PEERADDR:
477 1.13 mycroft in_setpeeraddr(inp, nam);
478 1.13 mycroft break;
479 1.13 mycroft
480 1.13 mycroft default:
481 1.13 mycroft panic("rip_usrreq");
482 1.1 cgd }
483 1.27 mycroft
484 1.22 pk release:
485 1.27 mycroft splx(s);
486 1.1 cgd return (error);
487 1.1 cgd }
488