raw_ip6.c revision 1.2 1 /*
2 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the project nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30 /*
31 * Copyright (c) 1982, 1986, 1988, 1993
32 * The Regents of the University of California. All rights reserved.
33 *
34 * Redistribution and use in source and binary forms, with or without
35 * modification, are permitted provided that the following conditions
36 * are met:
37 * 1. Redistributions of source code must retain the above copyright
38 * notice, this list of conditions and the following disclaimer.
39 * 2. Redistributions in binary form must reproduce the above copyright
40 * notice, this list of conditions and the following disclaimer in the
41 * documentation and/or other materials provided with the distribution.
42 * 3. All advertising materials mentioning features or use of this software
43 * must display the following acknowledgement:
44 * This product includes software developed by the University of
45 * California, Berkeley and its contributors.
46 * 4. Neither the name of the University nor the names of its contributors
47 * may be used to endorse or promote products derived from this software
48 * without specific prior written permission.
49 *
50 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
51 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
52 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
53 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
54 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
55 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
56 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
57 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
58 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
59 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60 * SUCH DAMAGE.
61 *
62 * @(#)raw_ip.c 8.2 (Berkeley) 1/4/94
63 */
64
65 #include <sys/param.h>
66 #include <sys/malloc.h>
67 #include <sys/mbuf.h>
68 #include <sys/socket.h>
69 #include <sys/protosw.h>
70 #include <sys/socketvar.h>
71 #include <sys/errno.h>
72 #include <sys/systm.h>
73 #ifdef __NetBSD__
74 #include <sys/proc.h>
75 #endif
76
77 #include <net/if.h>
78 #include <net/route.h>
79 #include <net/if_types.h>
80
81 #include <netinet/in.h>
82 #include <netinet/in_var.h>
83 #include <netinet6/in6_systm.h>
84 #include <netinet6/ip6.h>
85 #include <netinet6/ip6_var.h>
86 #include <netinet6/ip6_mroute.h>
87 #include <netinet6/icmp6.h>
88 #include <netinet6/in6_pcb.h>
89 #include <netinet6/nd6.h>
90
91 #ifdef IPSEC
92 #include <netinet6/ipsec.h>
93 #endif /*IPSEC*/
94
95 #include <machine/stdarg.h>
96
97 #include "faith.h"
98
99 struct in6pcb rawin6pcb;
100 #define ifatoia6(ifa) ((struct in6_ifaddr *)(ifa))
101
102 /*
103 * Raw interface to IP6 protocol.
104 */
105
106 /*
107 * Initialize raw connection block queue.
108 */
109 void
110 rip6_init()
111 {
112 rawin6pcb.in6p_next = rawin6pcb.in6p_prev = &rawin6pcb;
113 }
114
115 /*
116 * Setup generic address and protocol structures
117 * for raw_input routine, then pass them along with
118 * mbuf chain.
119 */
120 int
121 rip6_input(mp, offp, proto)
122 struct mbuf **mp;
123 int *offp, proto;
124 {
125 struct mbuf *m = *mp;
126 register struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
127 register struct in6pcb *in6p;
128 struct in6pcb *last = NULL;
129 struct sockaddr_in6 rip6src;
130 struct mbuf *opts = NULL;
131
132 #if defined(NFAITH) && 0 < NFAITH
133 if (m->m_pkthdr.rcvif) {
134 if (m->m_pkthdr.rcvif->if_type == IFT_FAITH) {
135 /* send icmp6 host unreach? */
136 m_freem(m);
137 return IPPROTO_DONE;
138 }
139 }
140 #endif
141 bzero(&rip6src, sizeof(rip6src));
142 rip6src.sin6_len = sizeof(struct sockaddr_in6);
143 rip6src.sin6_family = AF_INET6;
144 rip6src.sin6_addr = ip6->ip6_src;
145 if (IN6_IS_SCOPE_LINKLOCAL(&rip6src.sin6_addr))
146 rip6src.sin6_addr.s6_addr16[1] = 0;
147 if (m->m_pkthdr.rcvif) {
148 if (IN6_IS_SCOPE_LINKLOCAL(&rip6src.sin6_addr))
149 rip6src.sin6_scope_id = m->m_pkthdr.rcvif->if_index;
150 else
151 rip6src.sin6_scope_id = 0;
152 } else
153 rip6src.sin6_scope_id = 0;
154
155 for (in6p = rawin6pcb.in6p_next;
156 in6p != &rawin6pcb; in6p = in6p->in6p_next) {
157 if (in6p->in6p_ip6.ip6_nxt &&
158 in6p->in6p_ip6.ip6_nxt != proto)
159 continue;
160 if (!IN6_IS_ADDR_ANY(&in6p->in6p_laddr) &&
161 !IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &ip6->ip6_dst))
162 continue;
163 if (!IN6_IS_ADDR_ANY(&in6p->in6p_faddr) &&
164 !IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, &ip6->ip6_src))
165 continue;
166 if (in6p->in6p_cksum != -1
167 && in6_cksum(m, ip6->ip6_nxt, *offp,
168 sizeof(struct ip6_hdr) + ip6->ip6_plen - *offp)) {
169 /* XXX bark something */
170 continue;
171 }
172 if (last) {
173 struct mbuf *n;
174 if ((n = m_copy(m, 0, (int)M_COPYALL)) != NULL) {
175 if (last->in6p_flags & IN6P_CONTROLOPTS)
176 ip6_savecontrol(last, &opts, ip6, n);
177 /* strip intermediate headers */
178 m_adj(n, *offp);
179 if (sbappendaddr(&last->in6p_socket->so_rcv,
180 (struct sockaddr *)&rip6src,
181 n, opts) == 0) {
182 /* should notify about lost packet */
183 m_freem(n);
184 if (opts)
185 m_freem(opts);
186 } else
187 sorwakeup(last->in6p_socket);
188 opts = NULL;
189 }
190 }
191 last = in6p;
192 }
193 if (last) {
194 if (last->in6p_flags & IN6P_CONTROLOPTS)
195 ip6_savecontrol(last, &opts, ip6, m);
196 /* strip intermediate headers */
197 m_adj(m, *offp);
198 if (sbappendaddr(&last->in6p_socket->so_rcv,
199 (struct sockaddr *)&rip6src, m, opts) == 0) {
200 m_freem(m);
201 if (opts)
202 m_freem(opts);
203 } else
204 sorwakeup(last->in6p_socket);
205 } else {
206 if (proto == IPPROTO_NONE)
207 m_freem(m);
208 else {
209 char *prvnxtp = ip6_get_prevhdr(m, *offp); /* XXX */
210 icmp6_error(m, ICMP6_PARAM_PROB,
211 ICMP6_PARAMPROB_NEXTHEADER,
212 prvnxtp - mtod(m, char *));
213 }
214 ip6stat.ip6s_delivered--;
215 }
216 return IPPROTO_DONE;
217 }
218
219 /*
220 * Generate IPv6 header and pass packet to ip6_output.
221 * Tack on options user may have setup with control call.
222 */
223 int
224 #if __STDC__
225 rip6_output(struct mbuf *m, ...)
226 #else
227 rip6_output(m, va_alist)
228 struct mbuf *m;
229 va_dcl
230 #endif
231 {
232 struct socket *so;
233 struct sockaddr_in6 *dstsock;
234 struct mbuf *control;
235 struct in6_addr *dst;
236 struct ip6_hdr *ip6;
237 struct in6pcb *in6p;
238 u_int plen = m->m_pkthdr.len;
239 int error = 0;
240 struct ip6_pktopts opt, *optp = NULL;
241 struct ifnet *oifp = NULL;
242 int priv = 0;
243 va_list ap;
244
245 va_start(ap, m);
246 so = va_arg(ap, struct socket *);
247 dstsock = va_arg(ap, struct sockaddr_in6 *);
248 control = va_arg(ap, struct mbuf *);
249 va_end(ap);
250
251 in6p = sotoin6pcb(so);
252
253 {
254 struct proc *p = curproc; /* XXX */
255
256 if (p && !suser(p->p_ucred, &p->p_acflag))
257 priv = 1;
258 }
259 dst = &dstsock->sin6_addr;
260 if (control) {
261 if ((error = ip6_setpktoptions(control, &opt, priv)) != 0)
262 goto bad;
263 optp = &opt;
264 } else
265 optp = in6p->in6p_outputopts;
266
267 M_PREPEND(m, sizeof(*ip6), M_WAIT);
268 ip6 = mtod(m, struct ip6_hdr *);
269
270 /*
271 * Next header might not be ICMP6 but use its pseudo header anyway.
272 */
273 ip6->ip6_dst = *dst;
274
275 /*
276 * If the scope of the destination is link-local, embed the interface
277 * index in the address.
278 *
279 * XXX advanced-api value overrides sin6_scope_id
280 */
281 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst)) {
282 struct in6_pktinfo *pi;
283
284 /*
285 * XXX Boundary check is assumed to be already done in
286 * in6_setpktoptions().
287 */
288 if (optp && (pi = optp->ip6po_pktinfo) && pi->ipi6_ifindex) {
289 ip6->ip6_dst.s6_addr16[1] = htons(pi->ipi6_ifindex);
290 oifp = ifindex2ifnet[pi->ipi6_ifindex];
291 }
292 else if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) &&
293 in6p->in6p_moptions &&
294 in6p->in6p_moptions->im6o_multicast_ifp) {
295 ip6->ip6_dst.s6_addr16[1] =
296 htons(in6p->in6p_moptions->im6o_multicast_ifp->if_index);
297 oifp = ifindex2ifnet[in6p->in6p_moptions->im6o_multicast_ifp->if_index];
298 } else if (dstsock->sin6_scope_id) {
299 /* boundary check */
300 if (dstsock->sin6_scope_id < 0
301 || if_index < dstsock->sin6_scope_id) {
302 error = ENXIO; /* XXX EINVAL? */
303 goto bad;
304 }
305 ip6->ip6_dst.s6_addr16[1]
306 = htons(dstsock->sin6_scope_id & 0xffff);/*XXX*/
307 }
308 }
309
310 if (IN6_IS_ADDR_ANY(&in6p->in6p_laddr)) {
311 struct in6_addr *in6a;
312
313 if ((in6a = in6_selectsrc(dstsock, optp,
314 in6p->in6p_moptions,
315 &in6p->in6p_route,
316 &error)) == 0) {
317 if (error == 0)
318 error = EADDRNOTAVAIL;
319 goto bad;
320 }
321 ip6->ip6_src = *in6a;
322 if (in6p->in6p_route.ro_rt)
323 oifp = ifindex2ifnet[in6p->in6p_route.ro_rt->rt_ifp->if_index];
324 } else
325 ip6->ip6_src = in6p->in6p_laddr;
326
327 ip6->ip6_flow = in6p->in6p_flowinfo & IPV6_FLOWINFO_MASK;
328 ip6->ip6_vfc = IPV6_VERSION;
329 #if 0 /* ip6_plen will be filled in ip6_output. */
330 ip6->ip6_plen = htons((u_short)plen);
331 #endif
332 ip6->ip6_nxt = in6p->in6p_ip6.ip6_nxt;
333 if (oifp)
334 ip6->ip6_hlim = nd_ifinfo[oifp->if_index].chlim;
335 else
336 ip6->ip6_hlim = in6p->in6p_ip6.ip6_hlim;
337
338 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6 ||
339 in6p->in6p_cksum != -1) {
340 struct mbuf *n;
341 int off;
342 u_int16_t *p;
343
344 #define offsetof(type, member) ((size_t)(&((type *)0)->member)) /* XXX */
345
346 /* compute checksum */
347 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6)
348 off = offsetof(struct icmp6_hdr, icmp6_cksum);
349 else
350 off = in6p->in6p_cksum;
351 if (plen < off + 1) {
352 error = EINVAL;
353 goto bad;
354 }
355 off += sizeof(struct ip6_hdr);
356
357 n = m;
358 while (n && n->m_len <= off) {
359 off -= n->m_len;
360 n = n->m_next;
361 }
362 if (!n)
363 goto bad;
364 p = (u_int16_t *)(mtod(n, caddr_t) + off);
365 *p = 0;
366 *p = in6_cksum(m, ip6->ip6_nxt, sizeof(*ip6), plen);
367 }
368
369 #ifdef IPSEC
370 m->m_pkthdr.rcvif = (struct ifnet *)so;
371 #endif /*IPSEC*/
372
373 error = ip6_output(m, optp, &in6p->in6p_route, 0, in6p->in6p_moptions);
374 goto freectl;
375
376 bad:
377 if (m)
378 m_freem(m);
379
380 freectl:
381 if (optp == &opt && optp->ip6po_rthdr && optp->ip6po_route.ro_rt)
382 RTFREE(optp->ip6po_route.ro_rt);
383 if (control)
384 m_freem(control);
385 return(error);
386 }
387
388 /*
389 * Raw IPv6 socket option processing.
390 */
391 int
392 rip6_ctloutput(op, so, level, optname, m)
393 int op;
394 struct socket *so;
395 int level, optname;
396 struct mbuf **m;
397 {
398 int error = 0;
399
400 switch(level) {
401 case IPPROTO_IPV6:
402 switch(optname) {
403 case MRT6_INIT:
404 case MRT6_DONE:
405 case MRT6_ADD_MIF:
406 case MRT6_DEL_MIF:
407 case MRT6_ADD_MFC:
408 case MRT6_DEL_MFC:
409 case MRT6_PIM:
410 if (op == PRCO_SETOPT) {
411 error = ip6_mrouter_set(optname, so, *m);
412 if (*m)
413 (void)m_free(*m);
414 } else if (op == PRCO_GETOPT) {
415 error = ip6_mrouter_get(optname, so, m);
416 } else
417 error = EINVAL;
418 return (error);
419 }
420 return (ip6_ctloutput(op, so, level, optname, m));
421 /* NOTREACHED */
422
423 case IPPROTO_ICMPV6:
424 /*
425 * XXX: is it better to call icmp6_ctloutput() directly
426 * from protosw?
427 */
428 return(icmp6_ctloutput(op, so, level, optname, m));
429
430 default:
431 if (op == PRCO_SETOPT && *m)
432 (void)m_free(*m);
433 return(EINVAL);
434 }
435 }
436
437 extern u_long rip6_sendspace;
438 extern u_long rip6_recvspace;
439
440 int
441 rip6_usrreq(so, req, m, nam, control, p)
442 register struct socket *so;
443 int req;
444 struct mbuf *m, *nam, *control;
445 struct proc *p;
446 {
447 register struct in6pcb *in6p = sotoin6pcb(so);
448 int s;
449 int error = 0;
450 /* extern struct socket *ip6_mrouter; */ /* xxx */
451
452 if (req == PRU_CONTROL)
453 return (in6_control(so, (u_long)m, (caddr_t)nam,
454 (struct ifnet *)control, p));
455
456 switch (req) {
457 case PRU_ATTACH:
458 if (in6p)
459 panic("rip6_attach");
460 if (p == 0 || suser(p->p_ucred, &p->p_acflag)) {
461 error = EACCES;
462 break;
463 }
464 s = splnet();
465 if ((error = soreserve(so, rip6_sendspace, rip6_recvspace)) ||
466 (error = in6_pcballoc(so, &rawin6pcb))) {
467 splx(s);
468 break;
469 }
470 splx(s);
471 in6p = sotoin6pcb(so);
472 in6p->in6p_ip6.ip6_nxt = (int)nam;
473 in6p->in6p_cksum = -1;
474 #ifdef IPSEC
475 if ((error = ipsec_init_policy(&in6p->in6p_sp)) != 0)
476 break;
477 #endif /*IPSEC*/
478
479 MALLOC(in6p->in6p_icmp6filt, struct icmp6_filter *,
480 sizeof(struct icmp6_filter), M_PCB, M_NOWAIT);
481 ICMP6_FILTER_SETPASSALL(in6p->in6p_icmp6filt);
482 break;
483
484 case PRU_DISCONNECT:
485 if ((so->so_state & SS_ISCONNECTED) == 0) {
486 error = ENOTCONN;
487 break;
488 }
489 in6p->in6p_faddr = in6addr_any;
490 so->so_state &= ~SS_ISCONNECTED; /* XXX */
491 break;
492
493 case PRU_ABORT:
494 soisdisconnected(so);
495 /* Fallthrough */
496 case PRU_DETACH:
497 if (in6p == 0)
498 panic("rip6_detach");
499 if (so == ip6_mrouter)
500 ip6_mrouter_done();
501 /* xxx: RSVP */
502 if (in6p->in6p_icmp6filt) {
503 FREE(in6p->in6p_icmp6filt, M_PCB);
504 in6p->in6p_icmp6filt = NULL;
505 }
506 in6_pcbdetach(in6p);
507 break;
508
509 case PRU_BIND:
510 {
511 struct sockaddr_in6 *addr = mtod(nam, struct sockaddr_in6 *);
512 struct ifaddr *ia = NULL;
513
514 if (nam->m_len != sizeof(*addr)) {
515 error = EINVAL;
516 break;
517 }
518 if ((ifnet.tqh_first == 0) ||
519 (addr->sin6_family != AF_INET6) ||
520 (!IN6_IS_ADDR_ANY(&addr->sin6_addr) &&
521 (ia = ifa_ifwithaddr((struct sockaddr *)addr)) == 0)) {
522 error = EADDRNOTAVAIL;
523 break;
524 }
525 if (ia &&
526 ((struct in6_ifaddr *)ia)->ia6_flags &
527 (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY|
528 IN6_IFF_DETACHED|IN6_IFF_DEPRECATED)) {
529 error = EADDRNOTAVAIL;
530 break;
531 }
532 in6p->in6p_laddr = addr->sin6_addr;
533 break;
534 }
535
536 case PRU_CONNECT:
537 {
538 struct sockaddr_in6 *addr = mtod(nam, struct sockaddr_in6 *);
539 struct in6_addr *in6a = NULL;
540
541 if (nam->m_len != sizeof(*addr)) {
542 error = EINVAL;
543 break;
544 }
545 if (ifnet.tqh_first == 0) {
546 error = EADDRNOTAVAIL;
547 break;
548 }
549 if (addr->sin6_family != AF_INET6) {
550 error = EAFNOSUPPORT;
551 break;
552 }
553
554 /* Source address selection. XXX: need pcblookup? */
555 in6a = &in6p->in6p_laddr;
556 if (IN6_IS_ADDR_ANY(in6a) &&
557 (in6a = in6_selectsrc(addr, in6p->in6p_outputopts,
558 in6p->in6p_moptions, &in6p->in6p_route,
559 &error)) == NULL) {
560 if (error == 0)
561 error = EADDRNOTAVAIL;
562 break;
563 }
564 in6p->in6p_laddr = *in6a;
565 in6p->in6p_faddr = addr->sin6_addr;
566 soisconnected(so);
567 break;
568 }
569
570 case PRU_CONNECT2:
571 error = EOPNOTSUPP;
572 break;
573
574 /*
575 * Mark the connection as being incapable of futther input.
576 */
577 case PRU_SHUTDOWN:
578 socantsendmore(so);
579 break;
580 /*
581 * Ship a packet out. The appropriate raw output
582 * routine handles any messaging necessary.
583 */
584 case PRU_SEND:
585 {
586 struct sockaddr_in6 tmp;
587 struct sockaddr_in6 *dst;
588
589 if (so->so_state & SS_ISCONNECTED) {
590 if (nam) {
591 error = EISCONN;
592 break;
593 }
594 /* XXX */
595 bzero(&tmp, sizeof(tmp));
596 tmp.sin6_family = AF_INET6;
597 tmp.sin6_len = sizeof(struct sockaddr_in6);
598 bcopy(&in6p->in6p_faddr, &tmp.sin6_addr,
599 sizeof(struct in6_addr));
600 dst = &tmp;
601 } else {
602 if (nam == NULL) {
603 error = ENOTCONN;
604 break;
605 }
606 dst = mtod(nam, struct sockaddr_in6 *);
607 }
608 error = rip6_output(m, so, dst, control);
609 m = NULL;
610 break;
611 }
612
613 case PRU_SENSE:
614 /*
615 * stat: don't bother with a blocksize
616 */
617 return(0);
618 /*
619 * Not supported.
620 */
621 case PRU_RCVOOB:
622 case PRU_RCVD:
623 case PRU_LISTEN:
624 case PRU_ACCEPT:
625 case PRU_SENDOOB:
626 error = EOPNOTSUPP;
627 break;
628
629 case PRU_SOCKADDR:
630 in6_setsockaddr(in6p, nam);
631 break;
632
633 case PRU_PEERADDR:
634 in6_setpeeraddr(in6p, nam);
635 break;
636
637 default:
638 panic("rip6_usrreq");
639 }
640 if (m != NULL)
641 m_freem(m);
642 return(error);
643 }
644