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