raw_ip.c revision 1.1.1.3 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.3 thorpej * @(#)raw_ip.c 8.7 (Berkeley) 5/15/95
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.3 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.3 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.3 thorpej if (sbappendaddr(&last->so_rcv,
105 1.1.1.3 thorpej (struct sockaddr *)&ripsrc, n,
106 1.1.1.3 thorpej (struct mbuf *)0) == 0)
107 1.1.1.2 thorpej /* should notify about lost packet */
108 1.1.1.2 thorpej m_freem(n);
109 1.1.1.2 thorpej else
110 1.1.1.2 thorpej sorwakeup(last);
111 1.1.1.2 thorpej }
112 1.1.1.2 thorpej }
113 1.1.1.2 thorpej last = inp->inp_socket;
114 1.1.1.2 thorpej }
115 1.1.1.2 thorpej if (last) {
116 1.1.1.3 thorpej if (sbappendaddr(&last->so_rcv, (struct sockaddr *)&ripsrc,
117 1.1.1.2 thorpej m, (struct mbuf *)0) == 0)
118 1.1.1.2 thorpej m_freem(m);
119 1.1.1.2 thorpej else
120 1.1.1.2 thorpej sorwakeup(last);
121 1.1.1.2 thorpej } else {
122 1.1.1.2 thorpej m_freem(m);
123 1.1 cgd ipstat.ips_noproto++;
124 1.1 cgd ipstat.ips_delivered--;
125 1.1 cgd }
126 1.1 cgd }
127 1.1 cgd
128 1.1 cgd /*
129 1.1 cgd * Generate IP header and pass packet to ip_output.
130 1.1 cgd * Tack on options user may have setup with control call.
131 1.1 cgd */
132 1.1.1.2 thorpej int
133 1.1.1.2 thorpej rip_output(m, so, dst)
134 1.1 cgd register struct mbuf *m;
135 1.1 cgd struct socket *so;
136 1.1.1.2 thorpej u_long dst;
137 1.1 cgd {
138 1.1 cgd register struct ip *ip;
139 1.1.1.2 thorpej register struct inpcb *inp = sotoinpcb(so);
140 1.1.1.2 thorpej struct mbuf *opts;
141 1.1.1.2 thorpej int flags = (so->so_options & SO_DONTROUTE) | IP_ALLOWBROADCAST;
142 1.1 cgd
143 1.1 cgd /*
144 1.1 cgd * If the user handed us a complete IP packet, use it.
145 1.1 cgd * Otherwise, allocate an mbuf for a header and fill it in.
146 1.1 cgd */
147 1.1.1.2 thorpej if ((inp->inp_flags & INP_HDRINCL) == 0) {
148 1.1 cgd M_PREPEND(m, sizeof(struct ip), M_WAIT);
149 1.1 cgd ip = mtod(m, struct ip *);
150 1.1 cgd ip->ip_tos = 0;
151 1.1 cgd ip->ip_off = 0;
152 1.1.1.2 thorpej ip->ip_p = inp->inp_ip.ip_p;
153 1.1 cgd ip->ip_len = m->m_pkthdr.len;
154 1.1.1.2 thorpej ip->ip_src = inp->inp_laddr;
155 1.1.1.2 thorpej ip->ip_dst.s_addr = dst;
156 1.1 cgd ip->ip_ttl = MAXTTL;
157 1.1.1.2 thorpej opts = inp->inp_options;
158 1.1.1.2 thorpej } else {
159 1.1.1.2 thorpej ip = mtod(m, struct ip *);
160 1.1.1.2 thorpej if (ip->ip_id == 0)
161 1.1.1.2 thorpej ip->ip_id = htons(ip_id++);
162 1.1.1.2 thorpej opts = NULL;
163 1.1.1.2 thorpej /* XXX prevent ip_output from overwriting header fields */
164 1.1.1.2 thorpej flags |= IP_RAWOUTPUT;
165 1.1.1.2 thorpej ipstat.ips_rawout++;
166 1.1 cgd }
167 1.1.1.2 thorpej return (ip_output(m, opts, &inp->inp_route, flags, inp->inp_moptions));
168 1.1 cgd }
169 1.1 cgd
170 1.1 cgd /*
171 1.1 cgd * Raw IP socket option processing.
172 1.1 cgd */
173 1.1.1.2 thorpej int
174 1.1 cgd rip_ctloutput(op, so, level, optname, m)
175 1.1 cgd int op;
176 1.1 cgd struct socket *so;
177 1.1 cgd int level, optname;
178 1.1 cgd struct mbuf **m;
179 1.1 cgd {
180 1.1.1.2 thorpej register struct inpcb *inp = sotoinpcb(so);
181 1.1.1.2 thorpej register int error;
182 1.1 cgd
183 1.1.1.3 thorpej if (level != IPPROTO_IP) {
184 1.1.1.3 thorpej if (op == PRCO_SETOPT && *m)
185 1.1.1.3 thorpej (void) m_free(*m);
186 1.1.1.2 thorpej return (EINVAL);
187 1.1.1.3 thorpej }
188 1.1 cgd
189 1.1.1.2 thorpej switch (optname) {
190 1.1 cgd
191 1.1.1.2 thorpej case IP_HDRINCL:
192 1.1.1.3 thorpej error = 0;
193 1.1.1.3 thorpej if (op == PRCO_SETOPT) {
194 1.1.1.3 thorpej if (*m == 0 || (*m)->m_len < sizeof (int))
195 1.1.1.3 thorpej error = EINVAL;
196 1.1.1.3 thorpej else if (*mtod(*m, int *))
197 1.1.1.3 thorpej inp->inp_flags |= INP_HDRINCL;
198 1.1.1.3 thorpej else
199 1.1.1.3 thorpej inp->inp_flags &= ~INP_HDRINCL;
200 1.1.1.3 thorpej if (*m)
201 1.1.1.2 thorpej (void)m_free(*m);
202 1.1.1.3 thorpej } else {
203 1.1.1.3 thorpej *m = m_get(M_WAIT, MT_SOOPTS);
204 1.1.1.3 thorpej (*m)->m_len = sizeof (int);
205 1.1.1.3 thorpej *mtod(*m, int *) = inp->inp_flags & INP_HDRINCL;
206 1.1 cgd }
207 1.1.1.3 thorpej return (error);
208 1.1 cgd
209 1.1.1.2 thorpej case DVMRP_INIT:
210 1.1.1.2 thorpej case DVMRP_DONE:
211 1.1.1.2 thorpej case DVMRP_ADD_VIF:
212 1.1.1.2 thorpej case DVMRP_DEL_VIF:
213 1.1.1.2 thorpej case DVMRP_ADD_LGRP:
214 1.1.1.2 thorpej case DVMRP_DEL_LGRP:
215 1.1.1.2 thorpej case DVMRP_ADD_MRT:
216 1.1.1.2 thorpej case DVMRP_DEL_MRT:
217 1.1.1.2 thorpej #ifdef MROUTING
218 1.1.1.2 thorpej if (op == PRCO_SETOPT) {
219 1.1.1.2 thorpej error = ip_mrouter_cmd(optname, so, *m);
220 1.1.1.2 thorpej if (*m)
221 1.1.1.2 thorpej (void)m_free(*m);
222 1.1.1.2 thorpej } else
223 1.1 cgd error = EINVAL;
224 1.1.1.2 thorpej return (error);
225 1.1.1.2 thorpej #else
226 1.1.1.2 thorpej if (op == PRCO_SETOPT && *m)
227 1.1.1.2 thorpej (void)m_free(*m);
228 1.1.1.2 thorpej return (EOPNOTSUPP);
229 1.1.1.2 thorpej #endif
230 1.1.1.3 thorpej
231 1.1.1.3 thorpej default:
232 1.1.1.3 thorpej if (optname >= DVMRP_INIT) {
233 1.1.1.3 thorpej #ifdef MROUTING
234 1.1.1.3 thorpej if (op == PRCO_SETOPT) {
235 1.1.1.3 thorpej error = ip_mrouter_cmd(optname, so, *m);
236 1.1.1.3 thorpej if (*m)
237 1.1.1.3 thorpej (void)m_free(*m);
238 1.1.1.3 thorpej } else
239 1.1.1.3 thorpej error = EINVAL;
240 1.1.1.3 thorpej return (error);
241 1.1.1.3 thorpej #else
242 1.1.1.3 thorpej if (op == PRCO_SETOPT && *m)
243 1.1.1.3 thorpej (void)m_free(*m);
244 1.1.1.3 thorpej return (EOPNOTSUPP);
245 1.1.1.3 thorpej #endif
246 1.1.1.3 thorpej }
247 1.1.1.3 thorpej
248 1.1 cgd }
249 1.1.1.2 thorpej return (ip_ctloutput(op, so, level, optname, m));
250 1.1 cgd }
251 1.1 cgd
252 1.1.1.2 thorpej u_long rip_sendspace = RIPSNDQ;
253 1.1.1.2 thorpej u_long rip_recvspace = RIPRCVQ;
254 1.1.1.2 thorpej
255 1.1 cgd /*ARGSUSED*/
256 1.1.1.2 thorpej int
257 1.1.1.2 thorpej rip_usrreq(so, req, m, nam, control)
258 1.1 cgd register struct socket *so;
259 1.1 cgd int req;
260 1.1.1.2 thorpej struct mbuf *m, *nam, *control;
261 1.1 cgd {
262 1.1 cgd register int error = 0;
263 1.1.1.2 thorpej register struct inpcb *inp = sotoinpcb(so);
264 1.1.1.2 thorpej #ifdef MROUTING
265 1.1.1.2 thorpej extern struct socket *ip_mrouter;
266 1.1.1.2 thorpej #endif
267 1.1 cgd switch (req) {
268 1.1 cgd
269 1.1 cgd case PRU_ATTACH:
270 1.1.1.2 thorpej if (inp)
271 1.1 cgd panic("rip_attach");
272 1.1.1.2 thorpej if ((so->so_state & SS_PRIV) == 0) {
273 1.1.1.2 thorpej error = EACCES;
274 1.1.1.2 thorpej break;
275 1.1.1.2 thorpej }
276 1.1.1.2 thorpej if ((error = soreserve(so, rip_sendspace, rip_recvspace)) ||
277 1.1.1.2 thorpej (error = in_pcballoc(so, &rawinpcb)))
278 1.1.1.2 thorpej break;
279 1.1.1.2 thorpej inp = (struct inpcb *)so->so_pcb;
280 1.1.1.2 thorpej inp->inp_ip.ip_p = (int)nam;
281 1.1 cgd break;
282 1.1 cgd
283 1.1.1.2 thorpej case PRU_DISCONNECT:
284 1.1.1.2 thorpej if ((so->so_state & SS_ISCONNECTED) == 0) {
285 1.1.1.2 thorpej error = ENOTCONN;
286 1.1.1.2 thorpej break;
287 1.1.1.2 thorpej }
288 1.1.1.2 thorpej /* FALLTHROUGH */
289 1.1.1.2 thorpej case PRU_ABORT:
290 1.1.1.2 thorpej soisdisconnected(so);
291 1.1.1.2 thorpej /* FALLTHROUGH */
292 1.1 cgd case PRU_DETACH:
293 1.1.1.2 thorpej if (inp == 0)
294 1.1 cgd panic("rip_detach");
295 1.1.1.2 thorpej #ifdef MROUTING
296 1.1.1.2 thorpej if (so == ip_mrouter)
297 1.1.1.2 thorpej ip_mrouter_done();
298 1.1.1.2 thorpej #endif
299 1.1.1.2 thorpej in_pcbdetach(inp);
300 1.1 cgd break;
301 1.1 cgd
302 1.1 cgd case PRU_BIND:
303 1.1 cgd {
304 1.1 cgd struct sockaddr_in *addr = mtod(nam, struct sockaddr_in *);
305 1.1 cgd
306 1.1.1.2 thorpej if (nam->m_len != sizeof(*addr)) {
307 1.1.1.2 thorpej error = EINVAL;
308 1.1.1.2 thorpej break;
309 1.1.1.2 thorpej }
310 1.1 cgd if ((ifnet == 0) ||
311 1.1 cgd ((addr->sin_family != AF_INET) &&
312 1.1 cgd (addr->sin_family != AF_IMPLINK)) ||
313 1.1 cgd (addr->sin_addr.s_addr &&
314 1.1.1.2 thorpej ifa_ifwithaddr((struct sockaddr *)addr) == 0)) {
315 1.1.1.2 thorpej error = EADDRNOTAVAIL;
316 1.1.1.2 thorpej break;
317 1.1.1.2 thorpej }
318 1.1.1.2 thorpej inp->inp_laddr = addr->sin_addr;
319 1.1.1.2 thorpej break;
320 1.1 cgd }
321 1.1 cgd case PRU_CONNECT:
322 1.1 cgd {
323 1.1 cgd struct sockaddr_in *addr = mtod(nam, struct sockaddr_in *);
324 1.1 cgd
325 1.1.1.2 thorpej if (nam->m_len != sizeof(*addr)) {
326 1.1.1.2 thorpej error = EINVAL;
327 1.1.1.2 thorpej break;
328 1.1.1.2 thorpej }
329 1.1.1.2 thorpej if (ifnet == 0) {
330 1.1.1.2 thorpej error = EADDRNOTAVAIL;
331 1.1.1.2 thorpej break;
332 1.1.1.2 thorpej }
333 1.1 cgd if ((addr->sin_family != AF_INET) &&
334 1.1.1.2 thorpej (addr->sin_family != AF_IMPLINK)) {
335 1.1.1.2 thorpej error = EAFNOSUPPORT;
336 1.1.1.2 thorpej break;
337 1.1.1.2 thorpej }
338 1.1.1.2 thorpej inp->inp_faddr = addr->sin_addr;
339 1.1 cgd soisconnected(so);
340 1.1.1.2 thorpej break;
341 1.1 cgd }
342 1.1 cgd
343 1.1.1.2 thorpej case PRU_CONNECT2:
344 1.1.1.2 thorpej error = EOPNOTSUPP;
345 1.1.1.2 thorpej break;
346 1.1.1.2 thorpej
347 1.1.1.2 thorpej /*
348 1.1.1.2 thorpej * Mark the connection as being incapable of further input.
349 1.1.1.2 thorpej */
350 1.1.1.2 thorpej case PRU_SHUTDOWN:
351 1.1.1.2 thorpej socantsendmore(so);
352 1.1.1.2 thorpej break;
353 1.1.1.2 thorpej
354 1.1.1.2 thorpej /*
355 1.1.1.2 thorpej * Ship a packet out. The appropriate raw output
356 1.1.1.2 thorpej * routine handles any massaging necessary.
357 1.1.1.2 thorpej */
358 1.1.1.2 thorpej case PRU_SEND:
359 1.1.1.2 thorpej {
360 1.1.1.2 thorpej register u_long dst;
361 1.1.1.2 thorpej
362 1.1.1.2 thorpej if (so->so_state & SS_ISCONNECTED) {
363 1.1.1.2 thorpej if (nam) {
364 1.1.1.2 thorpej error = EISCONN;
365 1.1.1.2 thorpej break;
366 1.1.1.2 thorpej }
367 1.1.1.2 thorpej dst = inp->inp_faddr.s_addr;
368 1.1.1.2 thorpej } else {
369 1.1.1.2 thorpej if (nam == NULL) {
370 1.1.1.2 thorpej error = ENOTCONN;
371 1.1.1.2 thorpej break;
372 1.1.1.2 thorpej }
373 1.1.1.2 thorpej dst = mtod(nam, struct sockaddr_in *)->sin_addr.s_addr;
374 1.1.1.2 thorpej }
375 1.1.1.2 thorpej error = rip_output(m, so, dst);
376 1.1.1.2 thorpej m = NULL;
377 1.1.1.2 thorpej break;
378 1.1.1.2 thorpej }
379 1.1.1.2 thorpej
380 1.1.1.2 thorpej case PRU_SENSE:
381 1.1.1.2 thorpej /*
382 1.1.1.2 thorpej * stat: don't bother with a blocksize.
383 1.1.1.2 thorpej */
384 1.1.1.2 thorpej return (0);
385 1.1.1.2 thorpej
386 1.1.1.2 thorpej /*
387 1.1.1.2 thorpej * Not supported.
388 1.1.1.2 thorpej */
389 1.1.1.2 thorpej case PRU_RCVOOB:
390 1.1.1.2 thorpej case PRU_RCVD:
391 1.1.1.2 thorpej case PRU_LISTEN:
392 1.1.1.2 thorpej case PRU_ACCEPT:
393 1.1.1.2 thorpej case PRU_SENDOOB:
394 1.1.1.2 thorpej error = EOPNOTSUPP;
395 1.1.1.2 thorpej break;
396 1.1.1.2 thorpej
397 1.1.1.2 thorpej case PRU_SOCKADDR:
398 1.1.1.2 thorpej in_setsockaddr(inp, nam);
399 1.1.1.2 thorpej break;
400 1.1.1.2 thorpej
401 1.1.1.2 thorpej case PRU_PEERADDR:
402 1.1.1.2 thorpej in_setpeeraddr(inp, nam);
403 1.1.1.2 thorpej break;
404 1.1.1.2 thorpej
405 1.1.1.2 thorpej default:
406 1.1.1.2 thorpej panic("rip_usrreq");
407 1.1.1.2 thorpej }
408 1.1.1.2 thorpej if (m != NULL)
409 1.1.1.2 thorpej m_freem(m);
410 1.1 cgd return (error);
411 1.1 cgd }
412