Annotation of src/sys/netinet/raw_ip.c, Revision 1.44
1.44 ! darrenr 1: /* $NetBSD: raw_ip.c,v 1.43 1999/07/01 08:12:51 itojun Exp $ */
1.43 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:
67: #include "opt_mrouting.h"
1.1 cgd 68:
1.7 mycroft 69: #include <sys/param.h>
70: #include <sys/malloc.h>
71: #include <sys/mbuf.h>
72: #include <sys/socket.h>
73: #include <sys/protosw.h>
74: #include <sys/socketvar.h>
75: #include <sys/errno.h>
1.13 mycroft 76: #include <sys/systm.h>
1.26 mycroft 77: #include <sys/proc.h>
1.1 cgd 78:
1.7 mycroft 79: #include <net/if.h>
80: #include <net/route.h>
1.1 cgd 81:
1.7 mycroft 82: #include <netinet/in.h>
83: #include <netinet/in_systm.h>
84: #include <netinet/ip.h>
85: #include <netinet/ip_var.h>
1.13 mycroft 86: #include <netinet/ip_mroute.h>
1.44 ! darrenr 87: #include <netinet/ip_icmp.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.43 itojun 93: #ifdef IPSEC
94: #include <netinet6/ipsec.h>
95: #endif /*IPSEC*/
96:
1.44 ! darrenr 97: extern u_char ip_protox[];
! 98: extern struct protosw inetsw[];
1.20 mycroft 99: struct inpcbtable rawcbtable;
1.30 pk 100:
101: int rip_bind __P((struct inpcb *, struct mbuf *));
102: int rip_connect __P((struct inpcb *, struct mbuf *));
103: void rip_disconnect __P((struct inpcb *));
1.13 mycroft 104:
105: /*
106: * Nominal space allocated to a raw ip socket.
107: */
108: #define RIPSNDQ 8192
109: #define RIPRCVQ 8192
1.1 cgd 110:
111: /*
112: * Raw interface to IP protocol.
113: */
1.13 mycroft 114:
115: /*
116: * Initialize raw connection block q.
117: */
118: void
119: rip_init()
120: {
121:
1.33 mycroft 122: in_pcbinit(&rawcbtable, 1, 1);
1.13 mycroft 123: }
124:
1.43 itojun 125: static struct sockaddr_in ripsrc = { sizeof(ripsrc), AF_INET };
126:
1.1 cgd 127: /*
128: * Setup generic address and protocol structures
129: * for raw_input routine, then pass them along with
130: * mbuf chain.
131: */
1.9 mycroft 132: void
1.24 christos 133: #if __STDC__
134: rip_input(struct mbuf *m, ...)
135: #else
136: rip_input(m, va_alist)
1.1 cgd 137: struct mbuf *m;
1.24 christos 138: va_dcl
139: #endif
1.1 cgd 140: {
1.43 itojun 141: int off, proto;
1.1 cgd 142: register struct ip *ip = mtod(m, struct ip *);
1.13 mycroft 143: register struct inpcb *inp;
1.36 thorpej 144: struct inpcb *last = 0;
145: struct mbuf *opts = 0;
1.32 mycroft 146: struct sockaddr_in ripsrc;
1.43 itojun 147: va_list ap;
148:
149: va_start(ap, m);
150: off = va_arg(ap, int);
151: proto = va_arg(ap, int);
152: va_end(ap);
1.1 cgd 153:
1.32 mycroft 154: ripsrc.sin_family = AF_INET;
155: ripsrc.sin_len = sizeof(struct sockaddr_in);
1.1 cgd 156: ripsrc.sin_addr = ip->ip_src;
1.34 mycroft 157: ripsrc.sin_port = 0;
158: bzero((caddr_t)ripsrc.sin_zero, sizeof(ripsrc.sin_zero));
1.42 thorpej 159:
160: /*
161: * XXX Compatibility: programs using raw IP expect ip_len
162: * XXX to have the header length subtracted.
163: */
164: ip->ip_len -= ip->ip_hl << 2;
1.32 mycroft 165:
1.21 cgd 166: for (inp = rawcbtable.inpt_queue.cqh_first;
167: inp != (struct inpcb *)&rawcbtable.inpt_queue;
168: inp = inp->inp_queue.cqe_next) {
1.43 itojun 169: if (inp->inp_ip.ip_p && inp->inp_ip.ip_p != proto)
1.13 mycroft 170: continue;
1.32 mycroft 171: if (!in_nullhost(inp->inp_laddr) &&
172: !in_hosteq(inp->inp_laddr, ip->ip_dst))
1.13 mycroft 173: continue;
1.32 mycroft 174: if (!in_nullhost(inp->inp_faddr) &&
175: !in_hosteq(inp->inp_faddr, ip->ip_src))
1.13 mycroft 176: continue;
177: if (last) {
178: struct mbuf *n;
1.24 christos 179: if ((n = m_copy(m, 0, (int)M_COPYALL)) != NULL) {
1.36 thorpej 180: if (last->inp_flags & INP_CONTROLOPTS ||
181: last->inp_socket->so_options & SO_TIMESTAMP)
182: ip_savecontrol(last, &opts, ip, n);
183: if (sbappendaddr(&last->inp_socket->so_rcv,
184: sintosa(&ripsrc), n, opts) == 0) {
1.13 mycroft 185: /* should notify about lost packet */
186: m_freem(n);
1.36 thorpej 187: if (opts)
188: m_freem(opts);
189: } else
190: sorwakeup(last->inp_socket);
1.41 thorpej 191: opts = NULL;
1.13 mycroft 192: }
193: }
1.36 thorpej 194: last = inp;
1.13 mycroft 195: }
196: if (last) {
1.36 thorpej 197: if (last->inp_flags & INP_CONTROLOPTS ||
198: last->inp_socket->so_options & SO_TIMESTAMP)
199: ip_savecontrol(last, &opts, ip, m);
200: if (sbappendaddr(&last->inp_socket->so_rcv,
201: sintosa(&ripsrc), m, opts) == 0) {
1.13 mycroft 202: m_freem(m);
1.36 thorpej 203: if (opts)
204: m_freem(opts);
205: } else
206: sorwakeup(last->inp_socket);
1.13 mycroft 207: } else {
1.44 ! darrenr 208: if (inetsw[ip_protox[ip->ip_p]].pr_input == rip_input) {
! 209: icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PROTOCOL,0,0);
! 210: ipstat.ips_noproto++;
! 211: ipstat.ips_delivered--;
! 212: } else
! 213: m_freem(m);
1.1 cgd 214: }
1.43 itojun 215: return;
1.1 cgd 216: }
217:
218: /*
219: * Generate IP header and pass packet to ip_output.
220: * Tack on options user may have setup with control call.
221: */
1.9 mycroft 222: int
1.24 christos 223: #if __STDC__
224: rip_output(struct mbuf *m, ...)
225: #else
226: rip_output(m, va_alist)
227: struct mbuf *m;
228: va_dcl
229: #endif
230: {
1.27 mycroft 231: register struct inpcb *inp;
1.1 cgd 232: register struct ip *ip;
1.10 mycroft 233: struct mbuf *opts;
1.24 christos 234: int flags;
235: va_list ap;
236:
237: va_start(ap, m);
1.27 mycroft 238: inp = va_arg(ap, struct inpcb *);
1.24 christos 239: va_end(ap);
240:
1.27 mycroft 241: flags =
1.37 matt 242: (inp->inp_socket->so_options & SO_DONTROUTE) | IP_ALLOWBROADCAST
243: | IP_RETURNMTU;
1.1 cgd 244:
245: /*
246: * If the user handed us a complete IP packet, use it.
247: * Otherwise, allocate an mbuf for a header and fill it in.
248: */
1.13 mycroft 249: if ((inp->inp_flags & INP_HDRINCL) == 0) {
1.35 thorpej 250: if ((m->m_pkthdr.len + sizeof(struct ip)) > IP_MAXPACKET) {
251: m_freem(m);
252: return (EMSGSIZE);
253: }
1.1 cgd 254: M_PREPEND(m, sizeof(struct ip), M_WAIT);
255: ip = mtod(m, struct ip *);
256: ip->ip_tos = 0;
257: ip->ip_off = 0;
1.13 mycroft 258: ip->ip_p = inp->inp_ip.ip_p;
1.1 cgd 259: ip->ip_len = m->m_pkthdr.len;
1.13 mycroft 260: ip->ip_src = inp->inp_laddr;
1.27 mycroft 261: ip->ip_dst = inp->inp_faddr;
1.1 cgd 262: ip->ip_ttl = MAXTTL;
1.13 mycroft 263: opts = inp->inp_options;
264: } else {
1.35 thorpej 265: if (m->m_pkthdr.len > IP_MAXPACKET) {
266: m_freem(m);
267: return (EMSGSIZE);
268: }
1.13 mycroft 269: ip = mtod(m, struct ip *);
1.38 mycroft 270: if (m->m_pkthdr.len != ip->ip_len) {
271: m_freem(m);
272: return (EINVAL);
273: }
1.13 mycroft 274: if (ip->ip_id == 0)
275: ip->ip_id = htons(ip_id++);
276: opts = NULL;
277: /* XXX prevent ip_output from overwriting header fields */
278: flags |= IP_RAWOUTPUT;
279: ipstat.ips_rawout++;
1.1 cgd 280: }
1.43 itojun 281: #ifdef IPSEC
282: m->m_pkthdr.rcvif = (struct ifnet *)inp->inp_socket; /*XXX*/
283: #endif /*IPSEC*/
1.37 matt 284: return (ip_output(m, opts, &inp->inp_route, flags, inp->inp_moptions, &inp->inp_errormtu));
1.1 cgd 285: }
286:
287: /*
288: * Raw IP socket option processing.
289: */
1.9 mycroft 290: int
1.1 cgd 291: rip_ctloutput(op, so, level, optname, m)
292: int op;
293: struct socket *so;
294: int level, optname;
295: struct mbuf **m;
296: {
1.13 mycroft 297: register struct inpcb *inp = sotoinpcb(so);
1.31 mycroft 298: int error = 0;
1.1 cgd 299:
1.15 mycroft 300: if (level != IPPROTO_IP) {
1.31 mycroft 301: error = ENOPROTOOPT;
302: if (op == PRCO_SETOPT && *m != 0)
303: (void) m_free(*m);
304: } else switch (op) {
305:
306: case PRCO_SETOPT:
307: switch (optname) {
308: case IP_HDRINCL:
309: if (*m == 0 || (*m)->m_len < sizeof (int))
310: error = EINVAL;
311: else {
1.13 mycroft 312: if (*mtod(*m, int *))
313: inp->inp_flags |= INP_HDRINCL;
314: else
315: inp->inp_flags &= ~INP_HDRINCL;
316: }
1.31 mycroft 317: if (*m != 0)
318: (void) m_free(*m);
319: break;
320:
321: #ifdef MROUTING
322: case MRT_INIT:
323: case MRT_DONE:
324: case MRT_ADD_VIF:
325: case MRT_DEL_VIF:
326: case MRT_ADD_MFC:
327: case MRT_DEL_MFC:
328: case MRT_ASSERT:
329: error = ip_mrouter_set(so, optname, m);
330: break;
331: #endif
332:
333: default:
334: error = ip_ctloutput(op, so, level, optname, m);
335: break;
1.13 mycroft 336: }
337: break;
1.1 cgd 338:
1.31 mycroft 339: case PRCO_GETOPT:
340: switch (optname) {
341: case IP_HDRINCL:
342: *m = m_get(M_WAIT, M_SOOPTS);
343: (*m)->m_len = sizeof (int);
344: *mtod(*m, int *) = inp->inp_flags & INP_HDRINCL ? 1 : 0;
345: break;
346:
1.6 hpeyerl 347: #ifdef MROUTING
1.31 mycroft 348: case MRT_VERSION:
349: case MRT_ASSERT:
350: error = ip_mrouter_get(so, optname, m);
1.18 mycroft 351: break;
1.31 mycroft 352: #endif
353:
1.18 mycroft 354: default:
1.31 mycroft 355: error = ip_ctloutput(op, so, level, optname, m);
1.18 mycroft 356: break;
357: }
1.31 mycroft 358: break;
1.1 cgd 359: }
1.31 mycroft 360: return (error);
1.1 cgd 361: }
362:
1.27 mycroft 363: int
1.29 mycroft 364: rip_bind(inp, nam)
365: struct inpcb *inp;
366: struct mbuf *nam;
367: {
368: struct sockaddr_in *addr = mtod(nam, struct sockaddr_in *);
369:
370: if (nam->m_len != sizeof(*addr))
371: return (EINVAL);
372: if (ifnet.tqh_first == 0)
373: return (EADDRNOTAVAIL);
374: if (addr->sin_family != AF_INET &&
375: addr->sin_family != AF_IMPLINK)
376: return (EAFNOSUPPORT);
1.32 mycroft 377: if (!in_nullhost(addr->sin_addr) &&
1.29 mycroft 378: ifa_ifwithaddr(sintosa(addr)) == 0)
379: return (EADDRNOTAVAIL);
380: inp->inp_laddr = addr->sin_addr;
381: return (0);
382: }
383:
384: int
1.27 mycroft 385: rip_connect(inp, nam)
386: struct inpcb *inp;
387: struct mbuf *nam;
388: {
389: struct sockaddr_in *addr = mtod(nam, struct sockaddr_in *);
390:
391: if (nam->m_len != sizeof(*addr))
392: return (EINVAL);
393: if (ifnet.tqh_first == 0)
394: return (EADDRNOTAVAIL);
395: if (addr->sin_family != AF_INET &&
396: addr->sin_family != AF_IMPLINK)
397: return (EAFNOSUPPORT);
398: inp->inp_faddr = addr->sin_addr;
399: return (0);
400: }
401:
402: void
403: rip_disconnect(inp)
404: struct inpcb *inp;
405: {
406:
1.32 mycroft 407: inp->inp_faddr = zeroin_addr;
1.27 mycroft 408: }
409:
1.13 mycroft 410: u_long rip_sendspace = RIPSNDQ;
411: u_long rip_recvspace = RIPRCVQ;
412:
1.1 cgd 413: /*ARGSUSED*/
1.9 mycroft 414: int
1.26 mycroft 415: rip_usrreq(so, req, m, nam, control, p)
1.1 cgd 416: register struct socket *so;
417: int req;
1.2 cgd 418: struct mbuf *m, *nam, *control;
1.26 mycroft 419: struct proc *p;
1.1 cgd 420: {
1.27 mycroft 421: register struct inpcb *inp;
422: int s;
1.1 cgd 423: register int error = 0;
1.13 mycroft 424: #ifdef MROUTING
1.6 hpeyerl 425: extern struct socket *ip_mrouter;
426: #endif
1.27 mycroft 427:
1.22 pk 428: if (req == PRU_CONTROL)
429: return (in_control(so, (long)m, (caddr_t)nam,
1.26 mycroft 430: (struct ifnet *)control, p));
1.22 pk 431:
1.27 mycroft 432: s = splsoftnet();
433: inp = sotoinpcb(so);
1.28 mycroft 434: #ifdef DIAGNOSTIC
435: if (req != PRU_SEND && req != PRU_SENDOOB && control)
436: panic("rip_usrreq: unexpected control mbuf");
437: #endif
1.27 mycroft 438: if (inp == 0 && req != PRU_ATTACH) {
1.22 pk 439: error = EINVAL;
440: goto release;
441: }
442:
1.1 cgd 443: switch (req) {
444:
445: case PRU_ATTACH:
1.27 mycroft 446: if (inp != 0) {
447: error = EISCONN;
448: break;
449: }
1.26 mycroft 450: if (p == 0 || (error = suser(p->p_ucred, &p->p_acflag))) {
1.13 mycroft 451: error = EACCES;
452: break;
453: }
1.27 mycroft 454: if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
455: error = soreserve(so, rip_sendspace, rip_recvspace);
456: if (error)
457: break;
458: }
459: error = in_pcballoc(so, &rawcbtable);
460: if (error)
1.13 mycroft 461: break;
1.27 mycroft 462: inp = sotoinpcb(so);
1.17 cgd 463: inp->inp_ip.ip_p = (long)nam;
1.43 itojun 464: #ifdef IPSEC
465: error = ipsec_init_policy(&inp->inp_sp);
466: #endif /*IPSEC*/
1.1 cgd 467: break;
468:
469: case PRU_DETACH:
1.13 mycroft 470: #ifdef MROUTING
1.6 hpeyerl 471: if (so == ip_mrouter)
472: ip_mrouter_done();
473: #endif
1.13 mycroft 474: in_pcbdetach(inp);
1.1 cgd 475: break;
476:
477: case PRU_BIND:
1.29 mycroft 478: error = rip_bind(inp, nam);
1.27 mycroft 479: break;
480:
481: case PRU_LISTEN:
482: error = EOPNOTSUPP;
483: break;
484:
485: case PRU_CONNECT:
486: error = rip_connect(inp, nam);
487: if (error)
1.13 mycroft 488: break;
1.1 cgd 489: soisconnected(so);
1.13 mycroft 490: break;
491:
492: case PRU_CONNECT2:
493: error = EOPNOTSUPP;
494: break;
495:
1.27 mycroft 496: case PRU_DISCONNECT:
497: soisdisconnected(so);
498: rip_disconnect(inp);
499: break;
500:
1.13 mycroft 501: /*
502: * Mark the connection as being incapable of further input.
503: */
504: case PRU_SHUTDOWN:
505: socantsendmore(so);
506: break;
507:
1.27 mycroft 508: case PRU_RCVD:
509: error = EOPNOTSUPP;
510: break;
511:
1.13 mycroft 512: /*
513: * Ship a packet out. The appropriate raw output
514: * routine handles any massaging necessary.
515: */
516: case PRU_SEND:
1.28 mycroft 517: if (control && control->m_len) {
518: m_freem(control);
519: m_freem(m);
520: error = EINVAL;
521: break;
522: }
523: {
1.27 mycroft 524: if (nam) {
525: if ((so->so_state & SS_ISCONNECTED) != 0) {
1.13 mycroft 526: error = EISCONN;
1.28 mycroft 527: goto die;
1.13 mycroft 528: }
1.27 mycroft 529: error = rip_connect(inp, nam);
530: if (error) {
1.28 mycroft 531: die:
1.27 mycroft 532: m_freem(m);
533: break;
534: }
1.13 mycroft 535: } else {
1.27 mycroft 536: if ((so->so_state & SS_ISCONNECTED) == 0) {
1.13 mycroft 537: error = ENOTCONN;
1.28 mycroft 538: goto die;
1.13 mycroft 539: }
540: }
1.27 mycroft 541: error = rip_output(m, inp);
542: if (nam)
543: rip_disconnect(inp);
1.28 mycroft 544: }
1.13 mycroft 545: break;
546:
547: case PRU_SENSE:
548: /*
549: * stat: don't bother with a blocksize.
550: */
1.27 mycroft 551: splx(s);
1.1 cgd 552: return (0);
1.13 mycroft 553:
554: case PRU_RCVOOB:
1.27 mycroft 555: error = EOPNOTSUPP;
556: break;
557:
1.13 mycroft 558: case PRU_SENDOOB:
1.28 mycroft 559: m_freem(control);
1.27 mycroft 560: m_freem(m);
1.13 mycroft 561: error = EOPNOTSUPP;
562: break;
563:
564: case PRU_SOCKADDR:
565: in_setsockaddr(inp, nam);
566: break;
567:
568: case PRU_PEERADDR:
569: in_setpeeraddr(inp, nam);
570: break;
571:
572: default:
573: panic("rip_usrreq");
1.1 cgd 574: }
1.27 mycroft 575:
1.22 pk 576: release:
1.27 mycroft 577: splx(s);
1.1 cgd 578: return (error);
579: }
CVSweb <webmaster@jp.NetBSD.org>