Annotation of src/sys/netinet/raw_ip.c, Revision 1.21.2.1
1.21.2.1! mycroft 1: /* $NetBSD: raw_ip.c,v 1.21 1995/06/18 20:01:15 cgd 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.21.2.1! 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.1 cgd 258: switch (req) {
259:
260: case PRU_ATTACH:
1.13 mycroft 261: if (inp)
1.1 cgd 262: panic("rip_attach");
1.13 mycroft 263: if ((so->so_state & SS_PRIV) == 0) {
264: error = EACCES;
265: break;
266: }
267: if ((error = soreserve(so, rip_sendspace, rip_recvspace)) ||
1.20 mycroft 268: (error = in_pcballoc(so, &rawcbtable)))
1.13 mycroft 269: break;
270: inp = (struct inpcb *)so->so_pcb;
1.17 cgd 271: inp->inp_ip.ip_p = (long)nam;
1.1 cgd 272: break;
273:
1.13 mycroft 274: case PRU_DISCONNECT:
275: if ((so->so_state & SS_ISCONNECTED) == 0) {
276: error = ENOTCONN;
277: break;
278: }
279: /* FALLTHROUGH */
280: case PRU_ABORT:
281: soisdisconnected(so);
282: /* FALLTHROUGH */
1.1 cgd 283: case PRU_DETACH:
1.13 mycroft 284: if (inp == 0)
1.1 cgd 285: panic("rip_detach");
1.13 mycroft 286: #ifdef MROUTING
1.6 hpeyerl 287: if (so == ip_mrouter)
288: ip_mrouter_done();
289: #endif
1.13 mycroft 290: in_pcbdetach(inp);
1.1 cgd 291: break;
292:
293: case PRU_BIND:
294: {
295: struct sockaddr_in *addr = mtod(nam, struct sockaddr_in *);
296:
1.13 mycroft 297: if (nam->m_len != sizeof(*addr)) {
298: error = EINVAL;
299: break;
300: }
1.20 mycroft 301: if ((ifnet.tqh_first == 0) ||
1.1 cgd 302: ((addr->sin_family != AF_INET) &&
303: (addr->sin_family != AF_IMPLINK)) ||
304: (addr->sin_addr.s_addr &&
1.19 mycroft 305: ifa_ifwithaddr(sintosa(addr)) == 0)) {
1.13 mycroft 306: error = EADDRNOTAVAIL;
307: break;
308: }
309: inp->inp_laddr = addr->sin_addr;
310: break;
1.1 cgd 311: }
312: case PRU_CONNECT:
313: {
314: struct sockaddr_in *addr = mtod(nam, struct sockaddr_in *);
315:
1.13 mycroft 316: if (nam->m_len != sizeof(*addr)) {
317: error = EINVAL;
318: break;
319: }
1.20 mycroft 320: if (ifnet.tqh_first == 0) {
1.13 mycroft 321: error = EADDRNOTAVAIL;
322: break;
323: }
1.1 cgd 324: if ((addr->sin_family != AF_INET) &&
1.13 mycroft 325: (addr->sin_family != AF_IMPLINK)) {
326: error = EAFNOSUPPORT;
327: break;
328: }
329: inp->inp_faddr = addr->sin_addr;
1.1 cgd 330: soisconnected(so);
1.13 mycroft 331: break;
332: }
333:
334: case PRU_CONNECT2:
335: error = EOPNOTSUPP;
336: break;
337:
338: /*
339: * Mark the connection as being incapable of further input.
340: */
341: case PRU_SHUTDOWN:
342: socantsendmore(so);
343: break;
344:
345: /*
346: * Ship a packet out. The appropriate raw output
347: * routine handles any massaging necessary.
348: */
349: case PRU_SEND:
350: {
1.17 cgd 351: register u_int32_t dst;
1.13 mycroft 352:
353: if (so->so_state & SS_ISCONNECTED) {
354: if (nam) {
355: error = EISCONN;
356: break;
357: }
358: dst = inp->inp_faddr.s_addr;
359: } else {
360: if (nam == NULL) {
361: error = ENOTCONN;
362: break;
363: }
364: dst = mtod(nam, struct sockaddr_in *)->sin_addr.s_addr;
365: }
366: error = rip_output(m, so, dst);
367: m = NULL;
368: break;
369: }
370:
371: case PRU_SENSE:
372: /*
373: * stat: don't bother with a blocksize.
374: */
1.1 cgd 375: return (0);
1.13 mycroft 376:
377: /*
378: * Not supported.
379: */
380: case PRU_RCVOOB:
381: case PRU_RCVD:
382: case PRU_LISTEN:
383: case PRU_ACCEPT:
384: case PRU_SENDOOB:
385: error = EOPNOTSUPP;
386: break;
387:
388: case PRU_SOCKADDR:
389: in_setsockaddr(inp, nam);
390: break;
391:
392: case PRU_PEERADDR:
393: in_setpeeraddr(inp, nam);
394: break;
395:
396: default:
397: panic("rip_usrreq");
1.1 cgd 398: }
1.13 mycroft 399: if (m != NULL)
400: m_freem(m);
1.1 cgd 401: return (error);
402: }
CVSweb <webmaster@jp.NetBSD.org>