raw_ip.c revision 1.58 1 /* $NetBSD: raw_ip.c,v 1.58 2001/11/04 20:55:28 matt Exp $ */
2
3 /*
4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5 * 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. Neither the name of the project nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32 /*
33 * Copyright (c) 1982, 1986, 1988, 1993
34 * The Regents of the University of California. All rights reserved.
35 *
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
38 * are met:
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 * 3. All advertising materials mentioning features or use of this software
45 * must display the following acknowledgement:
46 * This product includes software developed by the University of
47 * California, Berkeley and its contributors.
48 * 4. Neither the name of the University nor the names of its contributors
49 * may be used to endorse or promote products derived from this software
50 * without specific prior written permission.
51 *
52 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
53 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
54 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
55 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
56 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
57 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
58 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
59 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
60 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
61 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62 * SUCH DAMAGE.
63 *
64 * @(#)raw_ip.c 8.7 (Berkeley) 5/15/95
65 */
66
67 #include "opt_ipsec.h"
68 #include "opt_mrouting.h"
69
70 #include <sys/param.h>
71 #include <sys/malloc.h>
72 #include <sys/mbuf.h>
73 #include <sys/socket.h>
74 #include <sys/protosw.h>
75 #include <sys/socketvar.h>
76 #include <sys/errno.h>
77 #include <sys/systm.h>
78 #include <sys/proc.h>
79
80 #include <net/if.h>
81 #include <net/route.h>
82
83 #include <netinet/in.h>
84 #include <netinet/in_systm.h>
85 #include <netinet/ip.h>
86 #include <netinet/ip_var.h>
87 #include <netinet/ip_mroute.h>
88 #include <netinet/ip_icmp.h>
89 #include <netinet/in_pcb.h>
90 #include <netinet/in_var.h>
91
92 #include <machine/stdarg.h>
93
94 #ifdef IPSEC
95 #include <netinet6/ipsec.h>
96 #endif /*IPSEC*/
97
98 struct inpcbtable rawcbtable;
99
100 int rip_bind __P((struct inpcb *, struct mbuf *));
101 int rip_connect __P((struct inpcb *, struct mbuf *));
102 void rip_disconnect __P((struct inpcb *));
103
104 /*
105 * Nominal space allocated to a raw ip socket.
106 */
107 #define RIPSNDQ 8192
108 #define RIPRCVQ 8192
109
110 /*
111 * Raw interface to IP protocol.
112 */
113
114 /*
115 * Initialize raw connection block q.
116 */
117 void
118 rip_init()
119 {
120
121 in_pcbinit(&rawcbtable, 1, 1);
122 }
123
124 static struct sockaddr_in ripsrc = { sizeof(ripsrc), AF_INET };
125
126 /*
127 * Setup generic address and protocol structures
128 * for raw_input routine, then pass them along with
129 * mbuf chain.
130 */
131 void
132 #if __STDC__
133 rip_input(struct mbuf *m, ...)
134 #else
135 rip_input(m, va_alist)
136 struct mbuf *m;
137 va_dcl
138 #endif
139 {
140 int off, proto;
141 struct ip *ip = mtod(m, struct ip *);
142 struct inpcb *inp;
143 struct inpcb *last = 0;
144 struct mbuf *opts = 0;
145 struct sockaddr_in ripsrc;
146 va_list ap;
147
148 va_start(ap, m);
149 off = va_arg(ap, int);
150 proto = va_arg(ap, int);
151 va_end(ap);
152
153 ripsrc.sin_family = AF_INET;
154 ripsrc.sin_len = sizeof(struct sockaddr_in);
155 ripsrc.sin_addr = ip->ip_src;
156 ripsrc.sin_port = 0;
157 bzero((caddr_t)ripsrc.sin_zero, sizeof(ripsrc.sin_zero));
158
159 /*
160 * XXX Compatibility: programs using raw IP expect ip_len
161 * XXX to have the header length subtracted.
162 */
163 ip->ip_len -= ip->ip_hl << 2;
164
165 CIRCLEQ_FOREACH(inp, &rawcbtable.inpt_queue, inp_queue) {
166 if (inp->inp_ip.ip_p && inp->inp_ip.ip_p != proto)
167 continue;
168 if (!in_nullhost(inp->inp_laddr) &&
169 !in_hosteq(inp->inp_laddr, ip->ip_dst))
170 continue;
171 if (!in_nullhost(inp->inp_faddr) &&
172 !in_hosteq(inp->inp_faddr, ip->ip_src))
173 continue;
174 if (last) {
175 struct mbuf *n;
176
177 #ifdef IPSEC
178 /* check AH/ESP integrity. */
179 if (ipsec4_in_reject_so(m, last->inp_socket)) {
180 ipsecstat.in_polvio++;
181 /* do not inject data to pcb */
182 } else
183 #endif /*IPSEC*/
184 if ((n = m_copy(m, 0, (int)M_COPYALL)) != NULL) {
185 if (last->inp_flags & INP_CONTROLOPTS ||
186 last->inp_socket->so_options & SO_TIMESTAMP)
187 ip_savecontrol(last, &opts, ip, n);
188 if (sbappendaddr(&last->inp_socket->so_rcv,
189 sintosa(&ripsrc), n, opts) == 0) {
190 /* should notify about lost packet */
191 m_freem(n);
192 if (opts)
193 m_freem(opts);
194 } else
195 sorwakeup(last->inp_socket);
196 opts = NULL;
197 }
198 }
199 last = inp;
200 }
201 #ifdef IPSEC
202 /* check AH/ESP integrity. */
203 if (last && ipsec4_in_reject_so(m, last->inp_socket)) {
204 m_freem(m);
205 ipsecstat.in_polvio++;
206 ipstat.ips_delivered--;
207 /* do not inject data to pcb */
208 } else
209 #endif /*IPSEC*/
210 if (last) {
211 if (last->inp_flags & INP_CONTROLOPTS ||
212 last->inp_socket->so_options & SO_TIMESTAMP)
213 ip_savecontrol(last, &opts, ip, m);
214 if (sbappendaddr(&last->inp_socket->so_rcv,
215 sintosa(&ripsrc), m, opts) == 0) {
216 m_freem(m);
217 if (opts)
218 m_freem(opts);
219 } else
220 sorwakeup(last->inp_socket);
221 } else {
222 if (inetsw[ip_protox[ip->ip_p]].pr_input == rip_input) {
223 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PROTOCOL,
224 0, 0);
225 ipstat.ips_noproto++;
226 ipstat.ips_delivered--;
227 } else
228 m_freem(m);
229 }
230 return;
231 }
232
233 /*
234 * Generate IP header and pass packet to ip_output.
235 * Tack on options user may have setup with control call.
236 */
237 int
238 #if __STDC__
239 rip_output(struct mbuf *m, ...)
240 #else
241 rip_output(m, va_alist)
242 struct mbuf *m;
243 va_dcl
244 #endif
245 {
246 struct inpcb *inp;
247 struct ip *ip;
248 struct mbuf *opts;
249 int flags;
250 va_list ap;
251
252 va_start(ap, m);
253 inp = va_arg(ap, struct inpcb *);
254 va_end(ap);
255
256 flags =
257 (inp->inp_socket->so_options & SO_DONTROUTE) | IP_ALLOWBROADCAST
258 | IP_RETURNMTU;
259
260 /*
261 * If the user handed us a complete IP packet, use it.
262 * Otherwise, allocate an mbuf for a header and fill it in.
263 */
264 if ((inp->inp_flags & INP_HDRINCL) == 0) {
265 if ((m->m_pkthdr.len + sizeof(struct ip)) > IP_MAXPACKET) {
266 m_freem(m);
267 return (EMSGSIZE);
268 }
269 M_PREPEND(m, sizeof(struct ip), M_WAIT);
270 ip = mtod(m, struct ip *);
271 ip->ip_tos = 0;
272 ip->ip_off = 0;
273 ip->ip_p = inp->inp_ip.ip_p;
274 ip->ip_len = m->m_pkthdr.len;
275 ip->ip_src = inp->inp_laddr;
276 ip->ip_dst = inp->inp_faddr;
277 ip->ip_ttl = MAXTTL;
278 opts = inp->inp_options;
279 } else {
280 if (m->m_pkthdr.len > IP_MAXPACKET) {
281 m_freem(m);
282 return (EMSGSIZE);
283 }
284 ip = mtod(m, struct ip *);
285 if (m->m_pkthdr.len != ip->ip_len) {
286 m_freem(m);
287 return (EINVAL);
288 }
289 if (ip->ip_id == 0)
290 ip->ip_id = htons(ip_id++);
291 opts = NULL;
292 /* XXX prevent ip_output from overwriting header fields */
293 flags |= IP_RAWOUTPUT;
294 ipstat.ips_rawout++;
295 }
296 #ifdef IPSEC
297 if (ipsec_setsocket(m, inp->inp_socket) != 0) {
298 m_freem(m);
299 return ENOBUFS;
300 }
301 #endif /*IPSEC*/
302 return (ip_output(m, opts, &inp->inp_route, flags, inp->inp_moptions, &inp->inp_errormtu));
303 }
304
305 /*
306 * Raw IP socket option processing.
307 */
308 int
309 rip_ctloutput(op, so, level, optname, m)
310 int op;
311 struct socket *so;
312 int level, optname;
313 struct mbuf **m;
314 {
315 struct inpcb *inp = sotoinpcb(so);
316 int error = 0;
317
318 if (level != IPPROTO_IP) {
319 error = ENOPROTOOPT;
320 if (op == PRCO_SETOPT && *m != 0)
321 (void) m_free(*m);
322 } else switch (op) {
323
324 case PRCO_SETOPT:
325 switch (optname) {
326 case IP_HDRINCL:
327 if (*m == 0 || (*m)->m_len < sizeof (int))
328 error = EINVAL;
329 else {
330 if (*mtod(*m, int *))
331 inp->inp_flags |= INP_HDRINCL;
332 else
333 inp->inp_flags &= ~INP_HDRINCL;
334 }
335 if (*m != 0)
336 (void) m_free(*m);
337 break;
338
339 #ifdef MROUTING
340 case MRT_INIT:
341 case MRT_DONE:
342 case MRT_ADD_VIF:
343 case MRT_DEL_VIF:
344 case MRT_ADD_MFC:
345 case MRT_DEL_MFC:
346 case MRT_ASSERT:
347 error = ip_mrouter_set(so, optname, m);
348 break;
349 #endif
350
351 default:
352 error = ip_ctloutput(op, so, level, optname, m);
353 break;
354 }
355 break;
356
357 case PRCO_GETOPT:
358 switch (optname) {
359 case IP_HDRINCL:
360 *m = m_get(M_WAIT, M_SOOPTS);
361 (*m)->m_len = sizeof (int);
362 *mtod(*m, int *) = inp->inp_flags & INP_HDRINCL ? 1 : 0;
363 break;
364
365 #ifdef MROUTING
366 case MRT_VERSION:
367 case MRT_ASSERT:
368 error = ip_mrouter_get(so, optname, m);
369 break;
370 #endif
371
372 default:
373 error = ip_ctloutput(op, so, level, optname, m);
374 break;
375 }
376 break;
377 }
378 return (error);
379 }
380
381 int
382 rip_bind(inp, nam)
383 struct inpcb *inp;
384 struct mbuf *nam;
385 {
386 struct sockaddr_in *addr = mtod(nam, struct sockaddr_in *);
387
388 if (nam->m_len != sizeof(*addr))
389 return (EINVAL);
390 if (TAILQ_FIRST(&ifnet) == 0)
391 return (EADDRNOTAVAIL);
392 if (addr->sin_family != AF_INET &&
393 addr->sin_family != AF_IMPLINK)
394 return (EAFNOSUPPORT);
395 if (!in_nullhost(addr->sin_addr) &&
396 ifa_ifwithaddr(sintosa(addr)) == 0)
397 return (EADDRNOTAVAIL);
398 inp->inp_laddr = addr->sin_addr;
399 return (0);
400 }
401
402 int
403 rip_connect(inp, nam)
404 struct inpcb *inp;
405 struct mbuf *nam;
406 {
407 struct sockaddr_in *addr = mtod(nam, struct sockaddr_in *);
408
409 if (nam->m_len != sizeof(*addr))
410 return (EINVAL);
411 if (TAILQ_FIRST(&ifnet) == 0)
412 return (EADDRNOTAVAIL);
413 if (addr->sin_family != AF_INET &&
414 addr->sin_family != AF_IMPLINK)
415 return (EAFNOSUPPORT);
416 inp->inp_faddr = addr->sin_addr;
417 return (0);
418 }
419
420 void
421 rip_disconnect(inp)
422 struct inpcb *inp;
423 {
424
425 inp->inp_faddr = zeroin_addr;
426 }
427
428 u_long rip_sendspace = RIPSNDQ;
429 u_long rip_recvspace = RIPRCVQ;
430
431 /*ARGSUSED*/
432 int
433 rip_usrreq(so, req, m, nam, control, p)
434 struct socket *so;
435 int req;
436 struct mbuf *m, *nam, *control;
437 struct proc *p;
438 {
439 struct inpcb *inp;
440 int s;
441 int error = 0;
442 #ifdef MROUTING
443 extern struct socket *ip_mrouter;
444 #endif
445
446 if (req == PRU_CONTROL)
447 return (in_control(so, (long)m, (caddr_t)nam,
448 (struct ifnet *)control, p));
449
450 if (req == PRU_PURGEIF) {
451 in_pcbpurgeif0(&rawcbtable, (struct ifnet *)control);
452 in_purgeif((struct ifnet *)control);
453 in_pcbpurgeif(&rawcbtable, (struct ifnet *)control);
454 return (0);
455 }
456
457 s = splsoftnet();
458 inp = sotoinpcb(so);
459 #ifdef DIAGNOSTIC
460 if (req != PRU_SEND && req != PRU_SENDOOB && control)
461 panic("rip_usrreq: unexpected control mbuf");
462 #endif
463 if (inp == 0 && req != PRU_ATTACH) {
464 error = EINVAL;
465 goto release;
466 }
467
468 switch (req) {
469
470 case PRU_ATTACH:
471 if (inp != 0) {
472 error = EISCONN;
473 break;
474 }
475 if (p == 0 || (error = suser(p->p_ucred, &p->p_acflag))) {
476 error = EACCES;
477 break;
478 }
479 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
480 error = soreserve(so, rip_sendspace, rip_recvspace);
481 if (error)
482 break;
483 }
484 error = in_pcballoc(so, &rawcbtable);
485 if (error)
486 break;
487 inp = sotoinpcb(so);
488 inp->inp_ip.ip_p = (long)nam;
489 break;
490
491 case PRU_DETACH:
492 #ifdef MROUTING
493 if (so == ip_mrouter)
494 ip_mrouter_done();
495 #endif
496 in_pcbdetach(inp);
497 break;
498
499 case PRU_BIND:
500 error = rip_bind(inp, nam);
501 break;
502
503 case PRU_LISTEN:
504 error = EOPNOTSUPP;
505 break;
506
507 case PRU_CONNECT:
508 error = rip_connect(inp, nam);
509 if (error)
510 break;
511 soisconnected(so);
512 break;
513
514 case PRU_CONNECT2:
515 error = EOPNOTSUPP;
516 break;
517
518 case PRU_DISCONNECT:
519 soisdisconnected(so);
520 rip_disconnect(inp);
521 break;
522
523 /*
524 * Mark the connection as being incapable of further input.
525 */
526 case PRU_SHUTDOWN:
527 socantsendmore(so);
528 break;
529
530 case PRU_RCVD:
531 error = EOPNOTSUPP;
532 break;
533
534 /*
535 * Ship a packet out. The appropriate raw output
536 * routine handles any massaging necessary.
537 */
538 case PRU_SEND:
539 if (control && control->m_len) {
540 m_freem(control);
541 m_freem(m);
542 error = EINVAL;
543 break;
544 }
545 {
546 if (nam) {
547 if ((so->so_state & SS_ISCONNECTED) != 0) {
548 error = EISCONN;
549 goto die;
550 }
551 error = rip_connect(inp, nam);
552 if (error) {
553 die:
554 m_freem(m);
555 break;
556 }
557 } else {
558 if ((so->so_state & SS_ISCONNECTED) == 0) {
559 error = ENOTCONN;
560 goto die;
561 }
562 }
563 error = rip_output(m, inp);
564 if (nam)
565 rip_disconnect(inp);
566 }
567 break;
568
569 case PRU_SENSE:
570 /*
571 * stat: don't bother with a blocksize.
572 */
573 splx(s);
574 return (0);
575
576 case PRU_RCVOOB:
577 error = EOPNOTSUPP;
578 break;
579
580 case PRU_SENDOOB:
581 m_freem(control);
582 m_freem(m);
583 error = EOPNOTSUPP;
584 break;
585
586 case PRU_SOCKADDR:
587 in_setsockaddr(inp, nam);
588 break;
589
590 case PRU_PEERADDR:
591 in_setpeeraddr(inp, nam);
592 break;
593
594 default:
595 panic("rip_usrreq");
596 }
597
598 release:
599 splx(s);
600 return (error);
601 }
602