raw_ip.c revision 1.65 1 /* $NetBSD: raw_ip.c,v 1.65 2002/11/07 17:49:08 thorpej 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 <sys/cdefs.h>
68 __KERNEL_RCSID(0, "$NetBSD: raw_ip.c,v 1.65 2002/11/07 17:49:08 thorpej Exp $");
69
70 #include "opt_ipsec.h"
71 #include "opt_mrouting.h"
72
73 #include <sys/param.h>
74 #include <sys/malloc.h>
75 #include <sys/mbuf.h>
76 #include <sys/socket.h>
77 #include <sys/protosw.h>
78 #include <sys/socketvar.h>
79 #include <sys/errno.h>
80 #include <sys/systm.h>
81 #include <sys/proc.h>
82
83 #include <net/if.h>
84 #include <net/route.h>
85
86 #include <netinet/in.h>
87 #include <netinet/in_systm.h>
88 #include <netinet/ip.h>
89 #include <netinet/ip_var.h>
90 #include <netinet/ip_mroute.h>
91 #include <netinet/ip_icmp.h>
92 #include <netinet/in_pcb.h>
93 #include <netinet/in_var.h>
94
95 #include <machine/stdarg.h>
96
97 #ifdef IPSEC
98 #include <netinet6/ipsec.h>
99 #endif /*IPSEC*/
100
101 struct inpcbtable rawcbtable;
102
103 int rip_pcbnotify __P((struct inpcbtable *, struct in_addr,
104 struct in_addr, int, int, void (*) __P((struct inpcb *, int))));
105 int rip_bind __P((struct inpcb *, struct mbuf *));
106 int rip_connect __P((struct inpcb *, struct mbuf *));
107 void rip_disconnect __P((struct inpcb *));
108
109 /*
110 * Nominal space allocated to a raw ip socket.
111 */
112 #define RIPSNDQ 8192
113 #define RIPRCVQ 8192
114
115 /*
116 * Raw interface to IP protocol.
117 */
118
119 /*
120 * Initialize raw connection block q.
121 */
122 void
123 rip_init()
124 {
125
126 in_pcbinit(&rawcbtable, 1, 1);
127 }
128
129 /*
130 * Setup generic address and protocol structures
131 * for raw_input routine, then pass them along with
132 * mbuf chain.
133 */
134 void
135 #if __STDC__
136 rip_input(struct mbuf *m, ...)
137 #else
138 rip_input(m, va_alist)
139 struct mbuf *m;
140 va_dcl
141 #endif
142 {
143 int proto;
144 struct ip *ip = mtod(m, struct ip *);
145 struct inpcb *inp;
146 struct inpcb *last = 0;
147 struct mbuf *opts = 0;
148 struct sockaddr_in ripsrc;
149 va_list ap;
150
151 va_start(ap, m);
152 (void)va_arg(ap, int); /* ignore value, advance ap */
153 proto = va_arg(ap, int);
154 va_end(ap);
155
156 ripsrc.sin_family = AF_INET;
157 ripsrc.sin_len = sizeof(struct sockaddr_in);
158 ripsrc.sin_addr = ip->ip_src;
159 ripsrc.sin_port = 0;
160 bzero((caddr_t)ripsrc.sin_zero, sizeof(ripsrc.sin_zero));
161
162 /*
163 * XXX Compatibility: programs using raw IP expect ip_len
164 * XXX to have the header length subtracted, and in host order.
165 * XXX ip_off is also expected to be host order.
166 */
167 ip->ip_len = ntohs(ip->ip_len) - (ip->ip_hl << 2);
168 NTOHS(ip->ip_off);
169
170 CIRCLEQ_FOREACH(inp, &rawcbtable.inpt_queue, inp_queue) {
171 if (inp->inp_ip.ip_p && inp->inp_ip.ip_p != proto)
172 continue;
173 if (!in_nullhost(inp->inp_laddr) &&
174 !in_hosteq(inp->inp_laddr, ip->ip_dst))
175 continue;
176 if (!in_nullhost(inp->inp_faddr) &&
177 !in_hosteq(inp->inp_faddr, ip->ip_src))
178 continue;
179 if (last) {
180 struct mbuf *n;
181
182 #ifdef IPSEC
183 /* check AH/ESP integrity. */
184 if (ipsec4_in_reject_so(m, last->inp_socket)) {
185 ipsecstat.in_polvio++;
186 /* do not inject data to pcb */
187 } else
188 #endif /*IPSEC*/
189 if ((n = m_copy(m, 0, (int)M_COPYALL)) != NULL) {
190 if (last->inp_flags & INP_CONTROLOPTS ||
191 last->inp_socket->so_options & SO_TIMESTAMP)
192 ip_savecontrol(last, &opts, ip, n);
193 if (sbappendaddr(&last->inp_socket->so_rcv,
194 sintosa(&ripsrc), n, opts) == 0) {
195 /* should notify about lost packet */
196 m_freem(n);
197 if (opts)
198 m_freem(opts);
199 } else
200 sorwakeup(last->inp_socket);
201 opts = NULL;
202 }
203 }
204 last = inp;
205 }
206 #ifdef IPSEC
207 /* check AH/ESP integrity. */
208 if (last && ipsec4_in_reject_so(m, last->inp_socket)) {
209 m_freem(m);
210 ipsecstat.in_polvio++;
211 ipstat.ips_delivered--;
212 /* do not inject data to pcb */
213 } else
214 #endif /*IPSEC*/
215 if (last) {
216 if (last->inp_flags & INP_CONTROLOPTS ||
217 last->inp_socket->so_options & SO_TIMESTAMP)
218 ip_savecontrol(last, &opts, ip, m);
219 if (sbappendaddr(&last->inp_socket->so_rcv,
220 sintosa(&ripsrc), m, opts) == 0) {
221 m_freem(m);
222 if (opts)
223 m_freem(opts);
224 } else
225 sorwakeup(last->inp_socket);
226 } else {
227 if (inetsw[ip_protox[ip->ip_p]].pr_input == rip_input) {
228 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PROTOCOL,
229 0, 0);
230 ipstat.ips_noproto++;
231 ipstat.ips_delivered--;
232 } else
233 m_freem(m);
234 }
235 return;
236 }
237
238 int
239 rip_pcbnotify(table, faddr, laddr, proto, errno, notify)
240 struct inpcbtable *table;
241 struct in_addr faddr, laddr;
242 int proto;
243 int errno;
244 void (*notify) __P((struct inpcb *, int));
245 {
246 struct inpcb *inp, *ninp;
247 int nmatch;
248
249 nmatch = 0;
250 for (inp = CIRCLEQ_FIRST(&table->inpt_queue);
251 inp != (struct inpcb *)&table->inpt_queue;
252 inp = ninp) {
253 ninp = inp->inp_queue.cqe_next;
254 if (inp->inp_ip.ip_p && inp->inp_ip.ip_p != proto)
255 continue;
256 if (in_hosteq(inp->inp_faddr, faddr) &&
257 in_hosteq(inp->inp_laddr, laddr)) {
258 (*notify)(inp, errno);
259 nmatch++;
260 }
261 }
262
263 return nmatch;
264 }
265
266 void *
267 rip_ctlinput(cmd, sa, v)
268 int cmd;
269 struct sockaddr *sa;
270 void *v;
271 {
272 struct ip *ip = v;
273 void (*notify) __P((struct inpcb *, int)) = in_rtchange;
274 int errno;
275
276 if (sa->sa_family != AF_INET ||
277 sa->sa_len != sizeof(struct sockaddr_in))
278 return NULL;
279 if ((unsigned)cmd >= PRC_NCMDS)
280 return NULL;
281 errno = inetctlerrmap[cmd];
282 if (PRC_IS_REDIRECT(cmd))
283 notify = in_rtchange, ip = 0;
284 else if (cmd == PRC_HOSTDEAD)
285 ip = 0;
286 else if (errno == 0)
287 return NULL;
288 if (ip) {
289 rip_pcbnotify(&rawcbtable, satosin(sa)->sin_addr,
290 ip->ip_src, ip->ip_p, errno, notify);
291
292 /* XXX mapped address case */
293 } else
294 in_pcbnotifyall(&rawcbtable, satosin(sa)->sin_addr, errno,
295 notify);
296 return NULL;
297 }
298
299 /*
300 * Generate IP header and pass packet to ip_output.
301 * Tack on options user may have setup with control call.
302 */
303 int
304 #if __STDC__
305 rip_output(struct mbuf *m, ...)
306 #else
307 rip_output(m, va_alist)
308 struct mbuf *m;
309 va_dcl
310 #endif
311 {
312 struct inpcb *inp;
313 struct ip *ip;
314 struct mbuf *opts;
315 int flags;
316 va_list ap;
317
318 va_start(ap, m);
319 inp = va_arg(ap, struct inpcb *);
320 va_end(ap);
321
322 flags =
323 (inp->inp_socket->so_options & SO_DONTROUTE) | IP_ALLOWBROADCAST
324 | IP_RETURNMTU;
325
326 /*
327 * If the user handed us a complete IP packet, use it.
328 * Otherwise, allocate an mbuf for a header and fill it in.
329 */
330 if ((inp->inp_flags & INP_HDRINCL) == 0) {
331 if ((m->m_pkthdr.len + sizeof(struct ip)) > IP_MAXPACKET) {
332 m_freem(m);
333 return (EMSGSIZE);
334 }
335 M_PREPEND(m, sizeof(struct ip), M_WAIT);
336 ip = mtod(m, struct ip *);
337 ip->ip_tos = 0;
338 ip->ip_off = htons(0);
339 ip->ip_p = inp->inp_ip.ip_p;
340 ip->ip_len = htons(m->m_pkthdr.len);
341 ip->ip_src = inp->inp_laddr;
342 ip->ip_dst = inp->inp_faddr;
343 ip->ip_ttl = MAXTTL;
344 opts = inp->inp_options;
345 } else {
346 if (m->m_pkthdr.len > IP_MAXPACKET) {
347 m_freem(m);
348 return (EMSGSIZE);
349 }
350 ip = mtod(m, struct ip *);
351
352 /*
353 * If the mbuf is read-only, we need to allocate
354 * a new mbuf for the header, since we need to
355 * modify the header.
356 */
357 if (M_READONLY(m)) {
358 int hlen = ip->ip_hl << 2;
359
360 m = m_copyup(m, hlen, (max_linkhdr + 3) & ~3);
361 if (m == NULL)
362 return (ENOMEM); /* XXX */
363 ip = mtod(m, struct ip *);
364 }
365
366 /* XXX userland passes ip_len and ip_off in host order */
367 if (m->m_pkthdr.len != ip->ip_len) {
368 m_freem(m);
369 return (EINVAL);
370 }
371 HTONS(ip->ip_len);
372 HTONS(ip->ip_off);
373 if (ip->ip_id == 0)
374 ip->ip_id = htons(ip_id++);
375 opts = NULL;
376 /* XXX prevent ip_output from overwriting header fields */
377 flags |= IP_RAWOUTPUT;
378 ipstat.ips_rawout++;
379 }
380 #ifdef IPSEC
381 if (ipsec_setsocket(m, inp->inp_socket) != 0) {
382 m_freem(m);
383 return ENOBUFS;
384 }
385 #endif /*IPSEC*/
386 return (ip_output(m, opts, &inp->inp_route, flags, inp->inp_moptions,
387 &inp->inp_errormtu));
388 }
389
390 /*
391 * Raw IP socket option processing.
392 */
393 int
394 rip_ctloutput(op, so, level, optname, m)
395 int op;
396 struct socket *so;
397 int level, optname;
398 struct mbuf **m;
399 {
400 struct inpcb *inp = sotoinpcb(so);
401 int error = 0;
402
403 if (level != IPPROTO_IP) {
404 error = ENOPROTOOPT;
405 if (op == PRCO_SETOPT && *m != 0)
406 (void) m_free(*m);
407 } else switch (op) {
408
409 case PRCO_SETOPT:
410 switch (optname) {
411 case IP_HDRINCL:
412 if (*m == 0 || (*m)->m_len < sizeof (int))
413 error = EINVAL;
414 else {
415 if (*mtod(*m, int *))
416 inp->inp_flags |= INP_HDRINCL;
417 else
418 inp->inp_flags &= ~INP_HDRINCL;
419 }
420 if (*m != 0)
421 (void) m_free(*m);
422 break;
423
424 #ifdef MROUTING
425 case MRT_INIT:
426 case MRT_DONE:
427 case MRT_ADD_VIF:
428 case MRT_DEL_VIF:
429 case MRT_ADD_MFC:
430 case MRT_DEL_MFC:
431 case MRT_ASSERT:
432 error = ip_mrouter_set(so, optname, m);
433 break;
434 #endif
435
436 default:
437 error = ip_ctloutput(op, so, level, optname, m);
438 break;
439 }
440 break;
441
442 case PRCO_GETOPT:
443 switch (optname) {
444 case IP_HDRINCL:
445 *m = m_get(M_WAIT, M_SOOPTS);
446 (*m)->m_len = sizeof (int);
447 *mtod(*m, int *) = inp->inp_flags & INP_HDRINCL ? 1 : 0;
448 break;
449
450 #ifdef MROUTING
451 case MRT_VERSION:
452 case MRT_ASSERT:
453 error = ip_mrouter_get(so, optname, m);
454 break;
455 #endif
456
457 default:
458 error = ip_ctloutput(op, so, level, optname, m);
459 break;
460 }
461 break;
462 }
463 return (error);
464 }
465
466 int
467 rip_bind(inp, nam)
468 struct inpcb *inp;
469 struct mbuf *nam;
470 {
471 struct sockaddr_in *addr = mtod(nam, struct sockaddr_in *);
472
473 if (nam->m_len != sizeof(*addr))
474 return (EINVAL);
475 if (TAILQ_FIRST(&ifnet) == 0)
476 return (EADDRNOTAVAIL);
477 if (addr->sin_family != AF_INET &&
478 addr->sin_family != AF_IMPLINK)
479 return (EAFNOSUPPORT);
480 if (!in_nullhost(addr->sin_addr) &&
481 ifa_ifwithaddr(sintosa(addr)) == 0)
482 return (EADDRNOTAVAIL);
483 inp->inp_laddr = addr->sin_addr;
484 return (0);
485 }
486
487 int
488 rip_connect(inp, nam)
489 struct inpcb *inp;
490 struct mbuf *nam;
491 {
492 struct sockaddr_in *addr = mtod(nam, struct sockaddr_in *);
493
494 if (nam->m_len != sizeof(*addr))
495 return (EINVAL);
496 if (TAILQ_FIRST(&ifnet) == 0)
497 return (EADDRNOTAVAIL);
498 if (addr->sin_family != AF_INET &&
499 addr->sin_family != AF_IMPLINK)
500 return (EAFNOSUPPORT);
501 inp->inp_faddr = addr->sin_addr;
502 return (0);
503 }
504
505 void
506 rip_disconnect(inp)
507 struct inpcb *inp;
508 {
509
510 inp->inp_faddr = zeroin_addr;
511 }
512
513 u_long rip_sendspace = RIPSNDQ;
514 u_long rip_recvspace = RIPRCVQ;
515
516 /*ARGSUSED*/
517 int
518 rip_usrreq(so, req, m, nam, control, p)
519 struct socket *so;
520 int req;
521 struct mbuf *m, *nam, *control;
522 struct proc *p;
523 {
524 struct inpcb *inp;
525 int s;
526 int error = 0;
527 #ifdef MROUTING
528 extern struct socket *ip_mrouter;
529 #endif
530
531 if (req == PRU_CONTROL)
532 return (in_control(so, (long)m, (caddr_t)nam,
533 (struct ifnet *)control, p));
534
535 if (req == PRU_PURGEIF) {
536 in_pcbpurgeif0(&rawcbtable, (struct ifnet *)control);
537 in_purgeif((struct ifnet *)control);
538 in_pcbpurgeif(&rawcbtable, (struct ifnet *)control);
539 return (0);
540 }
541
542 s = splsoftnet();
543 inp = sotoinpcb(so);
544 #ifdef DIAGNOSTIC
545 if (req != PRU_SEND && req != PRU_SENDOOB && control)
546 panic("rip_usrreq: unexpected control mbuf");
547 #endif
548 if (inp == 0 && req != PRU_ATTACH) {
549 error = EINVAL;
550 goto release;
551 }
552
553 switch (req) {
554
555 case PRU_ATTACH:
556 if (inp != 0) {
557 error = EISCONN;
558 break;
559 }
560 if (p == 0 || (error = suser(p->p_ucred, &p->p_acflag))) {
561 error = EACCES;
562 break;
563 }
564 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
565 error = soreserve(so, rip_sendspace, rip_recvspace);
566 if (error)
567 break;
568 }
569 error = in_pcballoc(so, &rawcbtable);
570 if (error)
571 break;
572 inp = sotoinpcb(so);
573 inp->inp_ip.ip_p = (long)nam;
574 break;
575
576 case PRU_DETACH:
577 #ifdef MROUTING
578 if (so == ip_mrouter)
579 ip_mrouter_done();
580 #endif
581 in_pcbdetach(inp);
582 break;
583
584 case PRU_BIND:
585 error = rip_bind(inp, nam);
586 break;
587
588 case PRU_LISTEN:
589 error = EOPNOTSUPP;
590 break;
591
592 case PRU_CONNECT:
593 error = rip_connect(inp, nam);
594 if (error)
595 break;
596 soisconnected(so);
597 break;
598
599 case PRU_CONNECT2:
600 error = EOPNOTSUPP;
601 break;
602
603 case PRU_DISCONNECT:
604 soisdisconnected(so);
605 rip_disconnect(inp);
606 break;
607
608 /*
609 * Mark the connection as being incapable of further input.
610 */
611 case PRU_SHUTDOWN:
612 socantsendmore(so);
613 break;
614
615 case PRU_RCVD:
616 error = EOPNOTSUPP;
617 break;
618
619 /*
620 * Ship a packet out. The appropriate raw output
621 * routine handles any massaging necessary.
622 */
623 case PRU_SEND:
624 if (control && control->m_len) {
625 m_freem(control);
626 m_freem(m);
627 error = EINVAL;
628 break;
629 }
630 {
631 if (nam) {
632 if ((so->so_state & SS_ISCONNECTED) != 0) {
633 error = EISCONN;
634 goto die;
635 }
636 error = rip_connect(inp, nam);
637 if (error) {
638 die:
639 m_freem(m);
640 break;
641 }
642 } else {
643 if ((so->so_state & SS_ISCONNECTED) == 0) {
644 error = ENOTCONN;
645 goto die;
646 }
647 }
648 error = rip_output(m, inp);
649 if (nam)
650 rip_disconnect(inp);
651 }
652 break;
653
654 case PRU_SENSE:
655 /*
656 * stat: don't bother with a blocksize.
657 */
658 splx(s);
659 return (0);
660
661 case PRU_RCVOOB:
662 error = EOPNOTSUPP;
663 break;
664
665 case PRU_SENDOOB:
666 m_freem(control);
667 m_freem(m);
668 error = EOPNOTSUPP;
669 break;
670
671 case PRU_SOCKADDR:
672 in_setsockaddr(inp, nam);
673 break;
674
675 case PRU_PEERADDR:
676 in_setpeeraddr(inp, nam);
677 break;
678
679 default:
680 panic("rip_usrreq");
681 }
682
683 release:
684 splx(s);
685 return (error);
686 }
687