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