raw_ip.c revision 1.24 1 /* $NetBSD: raw_ip.c,v 1.24 1996/02/13 23:43:29 christos Exp $ */
2
3 /*
4 * Copyright (c) 1982, 1986, 1988, 1993
5 * The Regents of the University of California. All rights reserved.
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 *
35 * @(#)raw_ip.c 8.2 (Berkeley) 1/4/94
36 */
37
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>
45 #include <sys/systm.h>
46
47 #include <net/if.h>
48 #include <net/route.h>
49
50 #include <netinet/in.h>
51 #include <netinet/in_systm.h>
52 #include <netinet/ip.h>
53 #include <netinet/ip_var.h>
54 #include <netinet/ip_mroute.h>
55 #include <netinet/in_pcb.h>
56 #include <netinet/in_var.h>
57
58 #include <machine/stdarg.h>
59
60 struct inpcbtable rawcbtable;
61
62 /*
63 * Nominal space allocated to a raw ip socket.
64 */
65 #define RIPSNDQ 8192
66 #define RIPRCVQ 8192
67
68 /*
69 * Raw interface to IP protocol.
70 */
71
72 /*
73 * Initialize raw connection block q.
74 */
75 void
76 rip_init()
77 {
78
79 in_pcbinit(&rawcbtable, 1);
80 }
81
82 struct sockaddr_in ripsrc = { sizeof(ripsrc), AF_INET };
83 /*
84 * Setup generic address and protocol structures
85 * for raw_input routine, then pass them along with
86 * mbuf chain.
87 */
88 void
89 #if __STDC__
90 rip_input(struct mbuf *m, ...)
91 #else
92 rip_input(m, va_alist)
93 struct mbuf *m;
94 va_dcl
95 #endif
96 {
97 register struct ip *ip = mtod(m, struct ip *);
98 register struct inpcb *inp;
99 struct socket *last = 0;
100
101 ripsrc.sin_addr = ip->ip_src;
102 for (inp = rawcbtable.inpt_queue.cqh_first;
103 inp != (struct inpcb *)&rawcbtable.inpt_queue;
104 inp = inp->inp_queue.cqe_next) {
105 if (inp->inp_ip.ip_p && inp->inp_ip.ip_p != ip->ip_p)
106 continue;
107 if (inp->inp_laddr.s_addr &&
108 inp->inp_laddr.s_addr != ip->ip_dst.s_addr)
109 continue;
110 if (inp->inp_faddr.s_addr &&
111 inp->inp_faddr.s_addr != ip->ip_src.s_addr)
112 continue;
113 if (last) {
114 struct mbuf *n;
115 if ((n = m_copy(m, 0, (int)M_COPYALL)) != NULL) {
116 if (sbappendaddr(&last->so_rcv,
117 sintosa(&ripsrc), n,
118 (struct mbuf *)0) == 0)
119 /* should notify about lost packet */
120 m_freem(n);
121 else
122 sorwakeup(last);
123 }
124 }
125 last = inp->inp_socket;
126 }
127 if (last) {
128 if (sbappendaddr(&last->so_rcv, sintosa(&ripsrc), m,
129 (struct mbuf *)0) == 0)
130 m_freem(m);
131 else
132 sorwakeup(last);
133 } else {
134 m_freem(m);
135 ipstat.ips_noproto++;
136 ipstat.ips_delivered--;
137 }
138 }
139
140 /*
141 * Generate IP header and pass packet to ip_output.
142 * Tack on options user may have setup with control call.
143 */
144 int
145 #if __STDC__
146 rip_output(struct mbuf *m, ...)
147 #else
148 rip_output(m, va_alist)
149 struct mbuf *m;
150 va_dcl
151 #endif
152 {
153 struct socket *so;
154 u_long dst;
155 register struct ip *ip;
156 register struct inpcb *inp;
157 struct mbuf *opts;
158 int flags;
159 va_list ap;
160
161 va_start(ap, m);
162 so = va_arg(ap, struct socket *);
163 dst = va_arg(ap, u_long);
164 va_end(ap);
165
166 inp = sotoinpcb(so);
167 flags = (so->so_options & SO_DONTROUTE) | IP_ALLOWBROADCAST;
168
169 /*
170 * If the user handed us a complete IP packet, use it.
171 * Otherwise, allocate an mbuf for a header and fill it in.
172 */
173 if ((inp->inp_flags & INP_HDRINCL) == 0) {
174 M_PREPEND(m, sizeof(struct ip), M_WAIT);
175 ip = mtod(m, struct ip *);
176 ip->ip_tos = 0;
177 ip->ip_off = 0;
178 ip->ip_p = inp->inp_ip.ip_p;
179 ip->ip_len = m->m_pkthdr.len;
180 ip->ip_src = inp->inp_laddr;
181 ip->ip_dst.s_addr = dst;
182 ip->ip_ttl = MAXTTL;
183 opts = inp->inp_options;
184 } else {
185 ip = mtod(m, struct ip *);
186 if (ip->ip_id == 0)
187 ip->ip_id = htons(ip_id++);
188 opts = NULL;
189 /* XXX prevent ip_output from overwriting header fields */
190 flags |= IP_RAWOUTPUT;
191 ipstat.ips_rawout++;
192 }
193 return (ip_output(m, opts, &inp->inp_route, flags, inp->inp_moptions));
194 }
195
196 /*
197 * Raw IP socket option processing.
198 */
199 int
200 rip_ctloutput(op, so, level, optname, m)
201 int op;
202 struct socket *so;
203 int level, optname;
204 struct mbuf **m;
205 {
206 register struct inpcb *inp = sotoinpcb(so);
207
208 if (level != IPPROTO_IP) {
209 if (m != 0 && *m != 0)
210 (void)m_free(*m);
211 return (EINVAL);
212 }
213
214 switch (optname) {
215
216 case IP_HDRINCL:
217 if (op == PRCO_SETOPT || op == PRCO_GETOPT) {
218 if (m == 0 || *m == 0 || (*m)->m_len < sizeof (int))
219 return (EINVAL);
220 if (op == PRCO_SETOPT) {
221 if (*mtod(*m, int *))
222 inp->inp_flags |= INP_HDRINCL;
223 else
224 inp->inp_flags &= ~INP_HDRINCL;
225 (void)m_free(*m);
226 } else {
227 (*m)->m_len = sizeof (int);
228 *mtod(*m, int *) = inp->inp_flags & INP_HDRINCL;
229 }
230 return (0);
231 }
232 break;
233
234 case MRT_INIT:
235 case MRT_DONE:
236 case MRT_ADD_VIF:
237 case MRT_DEL_VIF:
238 case MRT_ADD_MFC:
239 case MRT_DEL_MFC:
240 case MRT_VERSION:
241 case MRT_ASSERT:
242 #ifdef MROUTING
243 switch (op) {
244 case PRCO_SETOPT:
245 error = ip_mrouter_set(optname, so, m);
246 break;
247 case PRCO_GETOPT:
248 error = ip_mrouter_get(optname, so, m);
249 break;
250 default:
251 error = EINVAL;
252 break;
253 }
254 return (error);
255 #else
256 if (op == PRCO_SETOPT && *m)
257 m_free(*m);
258 return (EOPNOTSUPP);
259 #endif
260 }
261 return (ip_ctloutput(op, so, level, optname, m));
262 }
263
264 u_long rip_sendspace = RIPSNDQ;
265 u_long rip_recvspace = RIPRCVQ;
266
267 /*ARGSUSED*/
268 int
269 rip_usrreq(so, req, m, nam, control)
270 register struct socket *so;
271 int req;
272 struct mbuf *m, *nam, *control;
273 {
274 register int error = 0;
275 register struct inpcb *inp = sotoinpcb(so);
276 #ifdef MROUTING
277 extern struct socket *ip_mrouter;
278 #endif
279 if (req == PRU_CONTROL)
280 return (in_control(so, (long)m, (caddr_t)nam,
281 (struct ifnet *)control));
282
283 if (inp == NULL && req != PRU_ATTACH) {
284 error = EINVAL;
285 goto release;
286 }
287
288 switch (req) {
289
290 case PRU_ATTACH:
291 if (inp)
292 panic("rip_attach");
293 if ((so->so_state & SS_PRIV) == 0) {
294 error = EACCES;
295 break;
296 }
297 if ((error = soreserve(so, rip_sendspace, rip_recvspace)) ||
298 (error = in_pcballoc(so, &rawcbtable)))
299 break;
300 inp = (struct inpcb *)so->so_pcb;
301 inp->inp_ip.ip_p = (long)nam;
302 break;
303
304 case PRU_DISCONNECT:
305 if ((so->so_state & SS_ISCONNECTED) == 0) {
306 error = ENOTCONN;
307 break;
308 }
309 /* FALLTHROUGH */
310 case PRU_ABORT:
311 soisdisconnected(so);
312 /* FALLTHROUGH */
313 case PRU_DETACH:
314 if (inp == 0)
315 panic("rip_detach");
316 #ifdef MROUTING
317 if (so == ip_mrouter)
318 ip_mrouter_done();
319 #endif
320 in_pcbdetach(inp);
321 break;
322
323 case PRU_BIND:
324 {
325 struct sockaddr_in *addr = mtod(nam, struct sockaddr_in *);
326
327 if (nam->m_len != sizeof(*addr)) {
328 error = EINVAL;
329 break;
330 }
331 if ((ifnet.tqh_first == 0) ||
332 ((addr->sin_family != AF_INET) &&
333 (addr->sin_family != AF_IMPLINK)) ||
334 (addr->sin_addr.s_addr &&
335 ifa_ifwithaddr(sintosa(addr)) == 0)) {
336 error = EADDRNOTAVAIL;
337 break;
338 }
339 inp->inp_laddr = addr->sin_addr;
340 break;
341 }
342 case PRU_CONNECT:
343 {
344 struct sockaddr_in *addr = mtod(nam, struct sockaddr_in *);
345
346 if (nam->m_len != sizeof(*addr)) {
347 error = EINVAL;
348 break;
349 }
350 if (ifnet.tqh_first == 0) {
351 error = EADDRNOTAVAIL;
352 break;
353 }
354 if ((addr->sin_family != AF_INET) &&
355 (addr->sin_family != AF_IMPLINK)) {
356 error = EAFNOSUPPORT;
357 break;
358 }
359 inp->inp_faddr = addr->sin_addr;
360 soisconnected(so);
361 break;
362 }
363
364 case PRU_CONNECT2:
365 error = EOPNOTSUPP;
366 break;
367
368 /*
369 * Mark the connection as being incapable of further input.
370 */
371 case PRU_SHUTDOWN:
372 socantsendmore(so);
373 break;
374
375 /*
376 * Ship a packet out. The appropriate raw output
377 * routine handles any massaging necessary.
378 */
379 case PRU_SEND:
380 {
381 register u_int32_t dst;
382
383 if (so->so_state & SS_ISCONNECTED) {
384 if (nam) {
385 error = EISCONN;
386 break;
387 }
388 dst = inp->inp_faddr.s_addr;
389 } else {
390 if (nam == NULL) {
391 error = ENOTCONN;
392 break;
393 }
394 dst = mtod(nam, struct sockaddr_in *)->sin_addr.s_addr;
395 }
396 error = rip_output(m, so, dst);
397 m = NULL;
398 break;
399 }
400
401 case PRU_SENSE:
402 /*
403 * stat: don't bother with a blocksize.
404 */
405 return (0);
406
407 /*
408 * Not supported.
409 */
410 case PRU_RCVOOB:
411 case PRU_RCVD:
412 case PRU_LISTEN:
413 case PRU_ACCEPT:
414 case PRU_SENDOOB:
415 error = EOPNOTSUPP;
416 break;
417
418 case PRU_SOCKADDR:
419 in_setsockaddr(inp, nam);
420 break;
421
422 case PRU_PEERADDR:
423 in_setpeeraddr(inp, nam);
424 break;
425
426 default:
427 panic("rip_usrreq");
428 }
429 release:
430 if (m != NULL)
431 m_freem(m);
432 return (error);
433 }
434