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