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