Annotation of src/sys/netinet/raw_ip.c, Revision 1.11
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
1.11 ! hpeyerl 34: * $Id: raw_ip.c,v 1.10 1994/01/10 20:14:28 mycroft Exp $
1.1 cgd 35: */
36:
1.7 mycroft 37: #include <sys/param.h>
38: #include <sys/malloc.h>
39: #include <sys/mbuf.h>
40: #include <sys/socket.h>
41: #include <sys/protosw.h>
42: #include <sys/socketvar.h>
43: #include <sys/errno.h>
1.1 cgd 44:
1.7 mycroft 45: #include <net/if.h>
46: #include <net/route.h>
47: #include <net/raw_cb.h>
1.1 cgd 48:
1.7 mycroft 49: #include <netinet/in.h>
50: #include <netinet/in_systm.h>
51: #include <netinet/ip.h>
52: #include <netinet/ip_var.h>
53: #include <netinet/in_pcb.h>
1.9 mycroft 54: #include <netinet/ip_mroute.h>
1.1 cgd 55:
56: /*
57: * Raw interface to IP protocol.
58: */
59: struct sockaddr_in ripdst = { sizeof(ripdst), AF_INET };
60: struct sockaddr_in ripsrc = { sizeof(ripsrc), AF_INET };
61: struct sockproto ripproto = { PF_INET };
1.9 mycroft 62:
1.1 cgd 63: /*
64: * Setup generic address and protocol structures
65: * for raw_input routine, then pass them along with
66: * mbuf chain.
67: */
1.9 mycroft 68: void
1.1 cgd 69: rip_input(m)
70: struct mbuf *m;
71: {
72: register struct ip *ip = mtod(m, struct ip *);
73:
74: ripproto.sp_protocol = ip->ip_p;
75: ripdst.sin_addr = ip->ip_dst;
76: ripsrc.sin_addr = ip->ip_src;
77: if (raw_input(m, &ripproto, (struct sockaddr *)&ripsrc,
78: (struct sockaddr *)&ripdst) == 0) {
79: ipstat.ips_noproto++;
80: ipstat.ips_delivered--;
81: }
82: }
83:
84: /*
85: * Generate IP header and pass packet to ip_output.
86: * Tack on options user may have setup with control call.
87: */
88: #define satosin(sa) ((struct sockaddr_in *)(sa))
1.9 mycroft 89: int
1.1 cgd 90: rip_output(m, so)
91: register struct mbuf *m;
92: struct socket *so;
93: {
94: register struct ip *ip;
95: register struct raw_inpcb *rp = sotorawinpcb(so);
96: register struct sockaddr_in *sin;
1.10 mycroft 97: struct mbuf *opts;
1.1 cgd 98:
99: /*
100: * If the user handed us a complete IP packet, use it.
101: * Otherwise, allocate an mbuf for a header and fill it in.
102: */
1.4 cgd 103: if (rp->rinp_flags & RINPF_HDRINCL) {
1.1 cgd 104: ip = mtod(m, struct ip *);
1.4 cgd 105: if (ip->ip_len > m->m_pkthdr.len)
106: return EMSGSIZE;
107: ip->ip_len = m->m_pkthdr.len;
1.10 mycroft 108: opts = NULL;
1.4 cgd 109: } else {
1.1 cgd 110: M_PREPEND(m, sizeof(struct ip), M_WAIT);
111: ip = mtod(m, struct ip *);
112: ip->ip_tos = 0;
113: ip->ip_off = 0;
114: ip->ip_p = rp->rinp_rcb.rcb_proto.sp_protocol;
115: ip->ip_len = m->m_pkthdr.len;
116: if (sin = satosin(rp->rinp_rcb.rcb_laddr)) {
117: ip->ip_src = sin->sin_addr;
118: } else
119: ip->ip_src.s_addr = 0;
120: if (sin = satosin(rp->rinp_rcb.rcb_faddr))
121: ip->ip_dst = sin->sin_addr;
122: ip->ip_ttl = MAXTTL;
1.10 mycroft 123: opts = rp->rinp_options;
1.1 cgd 124: }
1.10 mycroft 125: return (ip_output(m, opts, &rp->rinp_route,
126: (so->so_options & SO_DONTROUTE) | IP_ALLOWBROADCAST
1.11 ! hpeyerl 127: , rp->rinp_rcb.rcb_moptions ));
1.1 cgd 128: }
129:
130: /*
131: * Raw IP socket option processing.
132: */
1.9 mycroft 133: int
1.1 cgd 134: rip_ctloutput(op, so, level, optname, m)
135: int op;
136: struct socket *so;
137: int level, optname;
138: struct mbuf **m;
139: {
140: int error = 0;
141: register struct raw_inpcb *rp = sotorawinpcb(so);
142:
143: if (level != IPPROTO_IP)
144: error = EINVAL;
145: else switch (op) {
146:
147: case PRCO_SETOPT:
148: switch (optname) {
149:
150: case IP_OPTIONS:
151: return (ip_pcbopts(&rp->rinp_options, *m));
152:
153: case IP_HDRINCL:
154: if (m == 0 || *m == 0 || (*m)->m_len < sizeof (int)) {
155: error = EINVAL;
156: break;
157: }
158: if (*mtod(*m, int *))
159: rp->rinp_flags |= RINPF_HDRINCL;
160: else
161: rp->rinp_flags &= ~RINPF_HDRINCL;
162: break;
1.6 hpeyerl 163: case IP_MULTICAST_IF:
164: case IP_MULTICAST_TTL:
165: case IP_MULTICAST_LOOP:
166: case IP_ADD_MEMBERSHIP:
167: case IP_DROP_MEMBERSHIP:
168: error = ip_setmoptions(optname,
169: &rp->rinp_rcb.rcb_moptions, *m);
170: break;
171: default:
172: #ifdef MROUTING
173: error = ip_mrouter_cmd(optname, so, *m);
174: #else
175: error = EINVAL;
176: #endif
177: break;
1.1 cgd 178: }
179: break;
180:
181: case PRCO_GETOPT:
182: *m = m_get(M_WAIT, MT_SOOPTS);
183: switch (optname) {
184:
185: case IP_OPTIONS:
186: if (rp->rinp_options) {
187: (*m)->m_len = rp->rinp_options->m_len;
188: bcopy(mtod(rp->rinp_options, caddr_t),
189: mtod(*m, caddr_t), (unsigned)(*m)->m_len);
190: } else
191: (*m)->m_len = 0;
192: break;
193:
194: case IP_HDRINCL:
195: (*m)->m_len = sizeof (int);
196: *mtod(*m, int *) = rp->rinp_flags & RINPF_HDRINCL;
197: break;
1.6 hpeyerl 198: case IP_MULTICAST_IF:
199: case IP_MULTICAST_TTL:
200: case IP_MULTICAST_LOOP:
201: case IP_ADD_MEMBERSHIP:
202: case IP_DROP_MEMBERSHIP:
1.8 mycroft 203: error = ip_getmoptions(optname,
204: rp->rinp_rcb.rcb_moptions, m);
205: break;
1.1 cgd 206: default:
207: error = EINVAL;
208: m_freem(*m);
209: *m = 0;
210: break;
211: }
212: break;
213: }
214: if (op == PRCO_SETOPT && *m)
215: (void)m_free(*m);
216: return (error);
217: }
218:
219: /*ARGSUSED*/
1.9 mycroft 220: int
1.2 cgd 221: rip_usrreq(so, req, m, nam, control)
1.1 cgd 222: register struct socket *so;
223: int req;
1.2 cgd 224: struct mbuf *m, *nam, *control;
1.1 cgd 225: {
226: register int error = 0;
227: register struct raw_inpcb *rp = sotorawinpcb(so);
1.11 ! hpeyerl 228: #if defined(MROUTING)
1.6 hpeyerl 229: extern struct socket *ip_mrouter;
230: #endif
1.1 cgd 231: switch (req) {
232:
233: case PRU_ATTACH:
234: if (rp)
235: panic("rip_attach");
236: MALLOC(rp, struct raw_inpcb *, sizeof *rp, M_PCB, M_WAITOK);
237: if (rp == 0)
238: return (ENOBUFS);
239: bzero((caddr_t)rp, sizeof *rp);
240: so->so_pcb = (caddr_t)rp;
241: break;
242:
243: case PRU_DETACH:
244: if (rp == 0)
245: panic("rip_detach");
1.11 ! hpeyerl 246: #if defined(MROUTING)
1.6 hpeyerl 247: if (so == ip_mrouter)
248: ip_mrouter_done();
249: #endif
1.1 cgd 250: if (rp->rinp_options)
251: m_freem(rp->rinp_options);
1.6 hpeyerl 252: if (rp->rinp_rcb.rcb_moptions)
253: ip_freemoptions(rp->rinp_rcb.rcb_moptions);
1.1 cgd 254: if (rp->rinp_route.ro_rt)
255: RTFREE(rp->rinp_route.ro_rt);
256: if (rp->rinp_rcb.rcb_laddr)
257: rp->rinp_rcb.rcb_laddr = 0;
258: break;
259:
260: case PRU_BIND:
261: {
262: struct sockaddr_in *addr = mtod(nam, struct sockaddr_in *);
263:
264: if (nam->m_len != sizeof(*addr))
265: return (EINVAL);
266: if ((ifnet == 0) ||
267: ((addr->sin_family != AF_INET) &&
268: (addr->sin_family != AF_IMPLINK)) ||
269: (addr->sin_addr.s_addr &&
270: ifa_ifwithaddr((struct sockaddr *)addr) == 0))
271: return (EADDRNOTAVAIL);
272: rp->rinp_rcb.rcb_laddr = (struct sockaddr *)&rp->rinp_laddr;
273: rp->rinp_laddr = *addr;
274: return (0);
275: }
276: case PRU_CONNECT:
277: {
278: struct sockaddr_in *addr = mtod(nam, struct sockaddr_in *);
279:
280: if (nam->m_len != sizeof(*addr))
281: return (EINVAL);
282: if (ifnet == 0)
283: return (EADDRNOTAVAIL);
284: if ((addr->sin_family != AF_INET) &&
285: (addr->sin_family != AF_IMPLINK))
286: return (EAFNOSUPPORT);
287: rp->rinp_rcb.rcb_faddr = (struct sockaddr *)&rp->rinp_faddr;
288: rp->rinp_faddr = *addr;
289: soisconnected(so);
290: return (0);
291: }
292: }
293: error = raw_usrreq(so, req, m, nam, control);
294:
295: if (error && (req == PRU_ATTACH) && so->so_pcb)
296: free(so->so_pcb, M_PCB);
297: return (error);
298: }
CVSweb <webmaster@jp.NetBSD.org>