Annotation of src/sys/netinet/raw_ip.c, Revision 1.45
1.45 ! thorpej 1: /* $NetBSD: raw_ip.c,v 1.44 1999/07/05 07:24:38 darrenr 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) {
210: icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PROTOCOL,0,0);
211: ipstat.ips_noproto++;
212: ipstat.ips_delivered--;
213: } else
214: m_freem(m);
1.1 cgd 215: }
1.43 itojun 216: return;
1.1 cgd 217: }
218:
219: /*
220: * Generate IP header and pass packet to ip_output.
221: * Tack on options user may have setup with control call.
222: */
1.9 mycroft 223: int
1.24 christos 224: #if __STDC__
225: rip_output(struct mbuf *m, ...)
226: #else
227: rip_output(m, va_alist)
228: struct mbuf *m;
229: va_dcl
230: #endif
231: {
1.27 mycroft 232: register struct inpcb *inp;
1.1 cgd 233: register struct ip *ip;
1.10 mycroft 234: struct mbuf *opts;
1.24 christos 235: int flags;
236: va_list ap;
237:
238: va_start(ap, m);
1.27 mycroft 239: inp = va_arg(ap, struct inpcb *);
1.24 christos 240: va_end(ap);
241:
1.27 mycroft 242: flags =
1.37 matt 243: (inp->inp_socket->so_options & SO_DONTROUTE) | IP_ALLOWBROADCAST
244: | IP_RETURNMTU;
1.1 cgd 245:
246: /*
247: * If the user handed us a complete IP packet, use it.
248: * Otherwise, allocate an mbuf for a header and fill it in.
249: */
1.13 mycroft 250: if ((inp->inp_flags & INP_HDRINCL) == 0) {
1.35 thorpej 251: if ((m->m_pkthdr.len + sizeof(struct ip)) > IP_MAXPACKET) {
252: m_freem(m);
253: return (EMSGSIZE);
254: }
1.1 cgd 255: M_PREPEND(m, sizeof(struct ip), M_WAIT);
256: ip = mtod(m, struct ip *);
257: ip->ip_tos = 0;
258: ip->ip_off = 0;
1.13 mycroft 259: ip->ip_p = inp->inp_ip.ip_p;
1.1 cgd 260: ip->ip_len = m->m_pkthdr.len;
1.13 mycroft 261: ip->ip_src = inp->inp_laddr;
1.27 mycroft 262: ip->ip_dst = inp->inp_faddr;
1.1 cgd 263: ip->ip_ttl = MAXTTL;
1.13 mycroft 264: opts = inp->inp_options;
265: } else {
1.35 thorpej 266: if (m->m_pkthdr.len > IP_MAXPACKET) {
267: m_freem(m);
268: return (EMSGSIZE);
269: }
1.13 mycroft 270: ip = mtod(m, struct ip *);
1.38 mycroft 271: if (m->m_pkthdr.len != ip->ip_len) {
272: m_freem(m);
273: return (EINVAL);
274: }
1.13 mycroft 275: if (ip->ip_id == 0)
276: ip->ip_id = htons(ip_id++);
277: opts = NULL;
278: /* XXX prevent ip_output from overwriting header fields */
279: flags |= IP_RAWOUTPUT;
280: ipstat.ips_rawout++;
1.1 cgd 281: }
1.43 itojun 282: #ifdef IPSEC
283: m->m_pkthdr.rcvif = (struct ifnet *)inp->inp_socket; /*XXX*/
284: #endif /*IPSEC*/
1.37 matt 285: return (ip_output(m, opts, &inp->inp_route, flags, inp->inp_moptions, &inp->inp_errormtu));
1.1 cgd 286: }
287:
288: /*
289: * Raw IP socket option processing.
290: */
1.9 mycroft 291: int
1.1 cgd 292: rip_ctloutput(op, so, level, optname, m)
293: int op;
294: struct socket *so;
295: int level, optname;
296: struct mbuf **m;
297: {
1.13 mycroft 298: register struct inpcb *inp = sotoinpcb(so);
1.31 mycroft 299: int error = 0;
1.1 cgd 300:
1.15 mycroft 301: if (level != IPPROTO_IP) {
1.31 mycroft 302: error = ENOPROTOOPT;
303: if (op == PRCO_SETOPT && *m != 0)
304: (void) m_free(*m);
305: } else switch (op) {
306:
307: case PRCO_SETOPT:
308: switch (optname) {
309: case IP_HDRINCL:
310: if (*m == 0 || (*m)->m_len < sizeof (int))
311: error = EINVAL;
312: else {
1.13 mycroft 313: if (*mtod(*m, int *))
314: inp->inp_flags |= INP_HDRINCL;
315: else
316: inp->inp_flags &= ~INP_HDRINCL;
317: }
1.31 mycroft 318: if (*m != 0)
319: (void) m_free(*m);
320: break;
321:
322: #ifdef MROUTING
323: case MRT_INIT:
324: case MRT_DONE:
325: case MRT_ADD_VIF:
326: case MRT_DEL_VIF:
327: case MRT_ADD_MFC:
328: case MRT_DEL_MFC:
329: case MRT_ASSERT:
330: error = ip_mrouter_set(so, optname, m);
331: break;
332: #endif
333:
334: default:
335: error = ip_ctloutput(op, so, level, optname, m);
336: break;
1.13 mycroft 337: }
338: break;
1.1 cgd 339:
1.31 mycroft 340: case PRCO_GETOPT:
341: switch (optname) {
342: case IP_HDRINCL:
343: *m = m_get(M_WAIT, M_SOOPTS);
344: (*m)->m_len = sizeof (int);
345: *mtod(*m, int *) = inp->inp_flags & INP_HDRINCL ? 1 : 0;
346: break;
347:
1.6 hpeyerl 348: #ifdef MROUTING
1.31 mycroft 349: case MRT_VERSION:
350: case MRT_ASSERT:
351: error = ip_mrouter_get(so, optname, m);
1.18 mycroft 352: break;
1.31 mycroft 353: #endif
354:
1.18 mycroft 355: default:
1.31 mycroft 356: error = ip_ctloutput(op, so, level, optname, m);
1.18 mycroft 357: break;
358: }
1.31 mycroft 359: break;
1.1 cgd 360: }
1.31 mycroft 361: return (error);
1.1 cgd 362: }
363:
1.27 mycroft 364: int
1.29 mycroft 365: rip_bind(inp, nam)
366: struct inpcb *inp;
367: struct mbuf *nam;
368: {
369: struct sockaddr_in *addr = mtod(nam, struct sockaddr_in *);
370:
371: if (nam->m_len != sizeof(*addr))
372: return (EINVAL);
373: if (ifnet.tqh_first == 0)
374: return (EADDRNOTAVAIL);
375: if (addr->sin_family != AF_INET &&
376: addr->sin_family != AF_IMPLINK)
377: return (EAFNOSUPPORT);
1.32 mycroft 378: if (!in_nullhost(addr->sin_addr) &&
1.29 mycroft 379: ifa_ifwithaddr(sintosa(addr)) == 0)
380: return (EADDRNOTAVAIL);
381: inp->inp_laddr = addr->sin_addr;
382: return (0);
383: }
384:
385: int
1.27 mycroft 386: rip_connect(inp, nam)
387: struct inpcb *inp;
388: struct mbuf *nam;
389: {
390: struct sockaddr_in *addr = mtod(nam, struct sockaddr_in *);
391:
392: if (nam->m_len != sizeof(*addr))
393: return (EINVAL);
394: if (ifnet.tqh_first == 0)
395: return (EADDRNOTAVAIL);
396: if (addr->sin_family != AF_INET &&
397: addr->sin_family != AF_IMPLINK)
398: return (EAFNOSUPPORT);
399: inp->inp_faddr = addr->sin_addr;
400: return (0);
401: }
402:
403: void
404: rip_disconnect(inp)
405: struct inpcb *inp;
406: {
407:
1.32 mycroft 408: inp->inp_faddr = zeroin_addr;
1.27 mycroft 409: }
410:
1.13 mycroft 411: u_long rip_sendspace = RIPSNDQ;
412: u_long rip_recvspace = RIPRCVQ;
413:
1.1 cgd 414: /*ARGSUSED*/
1.9 mycroft 415: int
1.26 mycroft 416: rip_usrreq(so, req, m, nam, control, p)
1.1 cgd 417: register struct socket *so;
418: int req;
1.2 cgd 419: struct mbuf *m, *nam, *control;
1.26 mycroft 420: struct proc *p;
1.1 cgd 421: {
1.27 mycroft 422: register struct inpcb *inp;
423: int s;
1.1 cgd 424: register int error = 0;
1.13 mycroft 425: #ifdef MROUTING
1.6 hpeyerl 426: extern struct socket *ip_mrouter;
427: #endif
1.27 mycroft 428:
1.22 pk 429: if (req == PRU_CONTROL)
430: return (in_control(so, (long)m, (caddr_t)nam,
1.26 mycroft 431: (struct ifnet *)control, p));
1.22 pk 432:
1.27 mycroft 433: s = splsoftnet();
434: inp = sotoinpcb(so);
1.28 mycroft 435: #ifdef DIAGNOSTIC
436: if (req != PRU_SEND && req != PRU_SENDOOB && control)
437: panic("rip_usrreq: unexpected control mbuf");
438: #endif
1.27 mycroft 439: if (inp == 0 && req != PRU_ATTACH) {
1.22 pk 440: error = EINVAL;
441: goto release;
442: }
443:
1.1 cgd 444: switch (req) {
445:
446: case PRU_ATTACH:
1.27 mycroft 447: if (inp != 0) {
448: error = EISCONN;
449: break;
450: }
1.26 mycroft 451: if (p == 0 || (error = suser(p->p_ucred, &p->p_acflag))) {
1.13 mycroft 452: error = EACCES;
453: break;
454: }
1.27 mycroft 455: if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
456: error = soreserve(so, rip_sendspace, rip_recvspace);
457: if (error)
458: break;
459: }
460: error = in_pcballoc(so, &rawcbtable);
461: if (error)
1.13 mycroft 462: break;
1.27 mycroft 463: inp = sotoinpcb(so);
1.17 cgd 464: inp->inp_ip.ip_p = (long)nam;
1.43 itojun 465: #ifdef IPSEC
466: error = ipsec_init_policy(&inp->inp_sp);
467: #endif /*IPSEC*/
1.1 cgd 468: break;
469:
470: case PRU_DETACH:
1.13 mycroft 471: #ifdef MROUTING
1.6 hpeyerl 472: if (so == ip_mrouter)
473: ip_mrouter_done();
474: #endif
1.13 mycroft 475: in_pcbdetach(inp);
1.1 cgd 476: break;
477:
478: case PRU_BIND:
1.29 mycroft 479: error = rip_bind(inp, nam);
1.27 mycroft 480: break;
481:
482: case PRU_LISTEN:
483: error = EOPNOTSUPP;
484: break;
485:
486: case PRU_CONNECT:
487: error = rip_connect(inp, nam);
488: if (error)
1.13 mycroft 489: break;
1.1 cgd 490: soisconnected(so);
1.13 mycroft 491: break;
492:
493: case PRU_CONNECT2:
494: error = EOPNOTSUPP;
495: break;
496:
1.27 mycroft 497: case PRU_DISCONNECT:
498: soisdisconnected(so);
499: rip_disconnect(inp);
500: break;
501:
1.13 mycroft 502: /*
503: * Mark the connection as being incapable of further input.
504: */
505: case PRU_SHUTDOWN:
506: socantsendmore(so);
507: break;
508:
1.27 mycroft 509: case PRU_RCVD:
510: error = EOPNOTSUPP;
511: break;
512:
1.13 mycroft 513: /*
514: * Ship a packet out. The appropriate raw output
515: * routine handles any massaging necessary.
516: */
517: case PRU_SEND:
1.28 mycroft 518: if (control && control->m_len) {
519: m_freem(control);
520: m_freem(m);
521: error = EINVAL;
522: break;
523: }
524: {
1.27 mycroft 525: if (nam) {
526: if ((so->so_state & SS_ISCONNECTED) != 0) {
1.13 mycroft 527: error = EISCONN;
1.28 mycroft 528: goto die;
1.13 mycroft 529: }
1.27 mycroft 530: error = rip_connect(inp, nam);
531: if (error) {
1.28 mycroft 532: die:
1.27 mycroft 533: m_freem(m);
534: break;
535: }
1.13 mycroft 536: } else {
1.27 mycroft 537: if ((so->so_state & SS_ISCONNECTED) == 0) {
1.13 mycroft 538: error = ENOTCONN;
1.28 mycroft 539: goto die;
1.13 mycroft 540: }
541: }
1.27 mycroft 542: error = rip_output(m, inp);
543: if (nam)
544: rip_disconnect(inp);
1.28 mycroft 545: }
1.13 mycroft 546: break;
547:
548: case PRU_SENSE:
549: /*
550: * stat: don't bother with a blocksize.
551: */
1.27 mycroft 552: splx(s);
1.1 cgd 553: return (0);
1.13 mycroft 554:
555: case PRU_RCVOOB:
1.27 mycroft 556: error = EOPNOTSUPP;
557: break;
558:
1.13 mycroft 559: case PRU_SENDOOB:
1.28 mycroft 560: m_freem(control);
1.27 mycroft 561: m_freem(m);
1.13 mycroft 562: error = EOPNOTSUPP;
563: break;
564:
565: case PRU_SOCKADDR:
566: in_setsockaddr(inp, nam);
567: break;
568:
569: case PRU_PEERADDR:
570: in_setpeeraddr(inp, nam);
571: break;
572:
573: default:
574: panic("rip_usrreq");
1.1 cgd 575: }
1.27 mycroft 576:
1.22 pk 577: release:
1.27 mycroft 578: splx(s);
1.1 cgd 579: return (error);
580: }
CVSweb <webmaster@jp.NetBSD.org>