Annotation of src/sys/netinet/raw_ip.c, Revision 1.3
1.1 cgd 1: /*
2: * Copyright (c) 1982, 1986, 1988 Regents of the University of California.
3: * All rights reserved.
4: *
5: * Redistribution and use in source and binary forms, with or without
6: * modification, are permitted provided that the following conditions
7: * are met:
8: * 1. Redistributions of source code must retain the above copyright
9: * notice, this list of conditions and the following disclaimer.
10: * 2. Redistributions in binary form must reproduce the above copyright
11: * notice, this list of conditions and the following disclaimer in the
12: * documentation and/or other materials provided with the distribution.
13: * 3. All advertising materials mentioning features or use of this software
14: * must display the following acknowledgement:
15: * This product includes software developed by the University of
16: * California, Berkeley and its contributors.
17: * 4. Neither the name of the University nor the names of its contributors
18: * may be used to endorse or promote products derived from this software
19: * without specific prior written permission.
20: *
21: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31: * SUCH DAMAGE.
32: *
1.3 ! cgd 33: * from: @(#)raw_ip.c 7.8 (Berkeley) 7/25/90
! 34: * $Id$
1.1 cgd 35: */
36:
37: #include "param.h"
38: #include "malloc.h"
1.3 ! cgd 39: #include "select.h"
1.1 cgd 40: #include "mbuf.h"
41: #include "socket.h"
42: #include "protosw.h"
43: #include "socketvar.h"
44: #include "errno.h"
45:
46: #include "../net/if.h"
47: #include "../net/route.h"
48: #include "../net/raw_cb.h"
49:
50: #include "in.h"
51: #include "in_systm.h"
52: #include "ip.h"
53: #include "ip_var.h"
54: #include "in_pcb.h"
55:
56: /*
57: * Raw interface to IP protocol.
58: */
59:
60: struct sockaddr_in ripdst = { sizeof(ripdst), AF_INET };
61: struct sockaddr_in ripsrc = { sizeof(ripsrc), AF_INET };
62: struct sockproto ripproto = { PF_INET };
63: /*
64: * Setup generic address and protocol structures
65: * for raw_input routine, then pass them along with
66: * mbuf chain.
67: */
68: rip_input(m)
69: struct mbuf *m;
70: {
71: register struct ip *ip = mtod(m, struct ip *);
72:
73: ripproto.sp_protocol = ip->ip_p;
74: ripdst.sin_addr = ip->ip_dst;
75: ripsrc.sin_addr = ip->ip_src;
76: if (raw_input(m, &ripproto, (struct sockaddr *)&ripsrc,
77: (struct sockaddr *)&ripdst) == 0) {
78: ipstat.ips_noproto++;
79: ipstat.ips_delivered--;
80: }
81: }
82:
83: /*
84: * Generate IP header and pass packet to ip_output.
85: * Tack on options user may have setup with control call.
86: */
87: #define satosin(sa) ((struct sockaddr_in *)(sa))
88: rip_output(m, so)
89: register struct mbuf *m;
90: struct socket *so;
91: {
92: register struct ip *ip;
93: register struct raw_inpcb *rp = sotorawinpcb(so);
94: register struct sockaddr_in *sin;
95:
96: /*
97: * If the user handed us a complete IP packet, use it.
98: * Otherwise, allocate an mbuf for a header and fill it in.
99: */
100: if (rp->rinp_flags & RINPF_HDRINCL)
101: ip = mtod(m, struct ip *);
102: else {
103: M_PREPEND(m, sizeof(struct ip), M_WAIT);
104: ip = mtod(m, struct ip *);
105: ip->ip_tos = 0;
106: ip->ip_off = 0;
107: ip->ip_p = rp->rinp_rcb.rcb_proto.sp_protocol;
108: ip->ip_len = m->m_pkthdr.len;
109: if (sin = satosin(rp->rinp_rcb.rcb_laddr)) {
110: ip->ip_src = sin->sin_addr;
111: } else
112: ip->ip_src.s_addr = 0;
113: if (sin = satosin(rp->rinp_rcb.rcb_faddr))
114: ip->ip_dst = sin->sin_addr;
115: ip->ip_ttl = MAXTTL;
116: }
117: return (ip_output(m,
118: (rp->rinp_flags & RINPF_HDRINCL)? (struct mbuf *)0: rp->rinp_options,
119: &rp->rinp_route,
120: (so->so_options & SO_DONTROUTE) | IP_ALLOWBROADCAST));
121: }
122:
123: /*
124: * Raw IP socket option processing.
125: */
126: rip_ctloutput(op, so, level, optname, m)
127: int op;
128: struct socket *so;
129: int level, optname;
130: struct mbuf **m;
131: {
132: int error = 0;
133: register struct raw_inpcb *rp = sotorawinpcb(so);
134:
135: if (level != IPPROTO_IP)
136: error = EINVAL;
137: else switch (op) {
138:
139: case PRCO_SETOPT:
140: switch (optname) {
141:
142: case IP_OPTIONS:
143: return (ip_pcbopts(&rp->rinp_options, *m));
144:
145: case IP_HDRINCL:
146: if (m == 0 || *m == 0 || (*m)->m_len < sizeof (int)) {
147: error = EINVAL;
148: break;
149: }
150: if (*mtod(*m, int *))
151: rp->rinp_flags |= RINPF_HDRINCL;
152: else
153: rp->rinp_flags &= ~RINPF_HDRINCL;
154: break;
155:
156: default:
157: error = EINVAL;
158: break;
159: }
160: break;
161:
162: case PRCO_GETOPT:
163: *m = m_get(M_WAIT, MT_SOOPTS);
164: switch (optname) {
165:
166: case IP_OPTIONS:
167: if (rp->rinp_options) {
168: (*m)->m_len = rp->rinp_options->m_len;
169: bcopy(mtod(rp->rinp_options, caddr_t),
170: mtod(*m, caddr_t), (unsigned)(*m)->m_len);
171: } else
172: (*m)->m_len = 0;
173: break;
174:
175: case IP_HDRINCL:
176: (*m)->m_len = sizeof (int);
177: *mtod(*m, int *) = rp->rinp_flags & RINPF_HDRINCL;
178: break;
179:
180: default:
181: error = EINVAL;
182: m_freem(*m);
183: *m = 0;
184: break;
185: }
186: break;
187: }
188: if (op == PRCO_SETOPT && *m)
189: (void)m_free(*m);
190: return (error);
191: }
192:
193: /*ARGSUSED*/
1.2 cgd 194: rip_usrreq(so, req, m, nam, control)
1.1 cgd 195: register struct socket *so;
196: int req;
1.2 cgd 197: struct mbuf *m, *nam, *control;
1.1 cgd 198: {
199: register int error = 0;
200: register struct raw_inpcb *rp = sotorawinpcb(so);
201:
202: switch (req) {
203:
204: case PRU_ATTACH:
205: if (rp)
206: panic("rip_attach");
207: MALLOC(rp, struct raw_inpcb *, sizeof *rp, M_PCB, M_WAITOK);
208: if (rp == 0)
209: return (ENOBUFS);
210: bzero((caddr_t)rp, sizeof *rp);
211: so->so_pcb = (caddr_t)rp;
212: break;
213:
214: case PRU_DETACH:
215: if (rp == 0)
216: panic("rip_detach");
217: if (rp->rinp_options)
218: m_freem(rp->rinp_options);
219: if (rp->rinp_route.ro_rt)
220: RTFREE(rp->rinp_route.ro_rt);
221: if (rp->rinp_rcb.rcb_laddr)
222: rp->rinp_rcb.rcb_laddr = 0;
223: break;
224:
225: case PRU_BIND:
226: {
227: struct sockaddr_in *addr = mtod(nam, struct sockaddr_in *);
228:
229: if (nam->m_len != sizeof(*addr))
230: return (EINVAL);
231: if ((ifnet == 0) ||
232: ((addr->sin_family != AF_INET) &&
233: (addr->sin_family != AF_IMPLINK)) ||
234: (addr->sin_addr.s_addr &&
235: ifa_ifwithaddr((struct sockaddr *)addr) == 0))
236: return (EADDRNOTAVAIL);
237: rp->rinp_rcb.rcb_laddr = (struct sockaddr *)&rp->rinp_laddr;
238: rp->rinp_laddr = *addr;
239: return (0);
240: }
241: case PRU_CONNECT:
242: {
243: struct sockaddr_in *addr = mtod(nam, struct sockaddr_in *);
244:
245: if (nam->m_len != sizeof(*addr))
246: return (EINVAL);
247: if (ifnet == 0)
248: return (EADDRNOTAVAIL);
249: if ((addr->sin_family != AF_INET) &&
250: (addr->sin_family != AF_IMPLINK))
251: return (EAFNOSUPPORT);
252: rp->rinp_rcb.rcb_faddr = (struct sockaddr *)&rp->rinp_faddr;
253: rp->rinp_faddr = *addr;
254: soisconnected(so);
255: return (0);
256: }
257: }
258: error = raw_usrreq(so, req, m, nam, control);
259:
260: if (error && (req == PRU_ATTACH) && so->so_pcb)
261: free(so->so_pcb, M_PCB);
262: return (error);
263: }
CVSweb <webmaster@jp.NetBSD.org>