1 /* $OpenBSD: raw_ip6.c,v 1.28 2004/08/23 01:30:30 itojun Exp $ */
2 /* $KAME: raw_ip6.c,v 1.69 2001/03/04 15:55:44 itojun Exp $ */
3
4 /*
5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the project nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33 /*
34 * Copyright (c) 1982, 1986, 1988, 1993
35 * The Regents of the University of California. All rights reserved.
36 *
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
39 * are met:
40 * 1. Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer.
42 * 2. Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in the
44 * documentation and/or other materials provided with the distribution.
45 * 3. Neither the name of the University nor the names of its contributors
46 * may be used to endorse or promote products derived from this software
47 * without specific prior written permission.
48 *
49 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 * SUCH DAMAGE.
60 *
61 * @(#)raw_ip.c 8.2 (Berkeley) 1/4/94
62 */
63
64 #include <sys/param.h>
65 #include <sys/malloc.h>
66 #include <sys/mbuf.h>
67 #include <sys/socket.h>
68 #include <sys/protosw.h>
69 #include <sys/socketvar.h>
70 #include <sys/errno.h>
71 #include <sys/systm.h>
72
73 #include <net/if.h>
74 #include <net/route.h>
75 #include <net/if_types.h>
76
77 #include <netinet/in.h>
78 #include <netinet/in_var.h>
79 #include <netinet/ip6.h>
80 #include <netinet6/ip6_var.h>
81 #include <netinet6/ip6_mroute.h>
82 #include <netinet/icmp6.h>
83 #include <netinet/in_systm.h>
84 #include <netinet/ip.h>
85 #include <netinet/in_pcb.h>
86 #include <netinet6/nd6.h>
87 #include <netinet6/ip6protosw.h>
88 #ifdef ENABLE_DEFAULT_SCOPE
89 #include <netinet6/scope6_var.h>
90 #endif
91 #include <netinet6/raw_ip6.h>
92
93 #include <sys/stdarg.h>
94
95 #include "faith.h"
96
97 /*
98 * Raw interface to IP6 protocol.
99 */
100 /* inpcb members */
101 #define in6pcb inpcb
102 #define in6p_laddr inp_laddr6
103 #define in6p_faddr inp_faddr6
104 #define in6p_icmp6filt inp_icmp6filt
105 #define in6p_route inp_route6
106 #define in6p_socket inp_socket
107 #define in6p_flags inp_flags
108 #define in6p_moptions inp_moptions6
109 #define in6p_outputopts inp_outputopts6
110 #define in6p_ip6 inp_ipv6
111 #define in6p_flowinfo inp_flowinfo
112 #define in6p_sp inp_sp
113 #define in6p_next inp_next
114 #define in6p_prev inp_prev
115 /* macro names */
116 #define sotoin6pcb sotoinpcb
117 /* function names */
118 #define in6_pcbdetach in_pcbdetach
119 #define in6_rtchange in_rtchange
120
121 struct inpcbtable rawin6pcbtable;
122 #define ifatoia6(ifa) ((struct in6_ifaddr *)(ifa))
123
124 struct rip6stat rip6stat;
125
126 /*
127 * Initialize raw connection block queue.
128 */
129 void
rip6_init()130 rip6_init()
131 {
132
133 in_pcbinit(&rawin6pcbtable, 1);
134 }
135
136 /*
137 * Setup generic address and protocol structures
138 * for raw_input routine, then pass them along with
139 * mbuf chain.
140 */
141 int
rip6_input(mp,offp,proto)142 rip6_input(mp, offp, proto)
143 struct mbuf **mp;
144 int *offp, proto;
145 {
146 struct mbuf *m = *mp;
147 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
148 struct in6pcb *in6p;
149 struct in6pcb *last = NULL;
150 struct sockaddr_in6 rip6src;
151 struct mbuf *opts = NULL;
152
153 rip6stat.rip6s_ipackets++;
154
155 #if defined(NFAITH) && 0 < NFAITH
156 if (m->m_pkthdr.rcvif) {
157 if (m->m_pkthdr.rcvif->if_type == IFT_FAITH) {
158 /* send icmp6 host unreach? */
159 m_freem(m);
160 return IPPROTO_DONE;
161 }
162 }
163 #endif
164
165 /* Be proactive about malicious use of IPv4 mapped address */
166 if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) ||
167 IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) {
168 /* XXX stat */
169 m_freem(m);
170 return IPPROTO_DONE;
171 }
172
173 bzero(&rip6src, sizeof(rip6src));
174 rip6src.sin6_len = sizeof(struct sockaddr_in6);
175 rip6src.sin6_family = AF_INET6;
176 /* KAME hack: recover scopeid */
177 (void)in6_recoverscope(&rip6src, &ip6->ip6_src, m->m_pkthdr.rcvif);
178
179 CIRCLEQ_FOREACH(in6p, &rawin6pcbtable.inpt_queue, inp_queue) {
180 if (!(in6p->in6p_flags & INP_IPV6))
181 continue;
182 if (in6p->in6p_ip6.ip6_nxt &&
183 in6p->in6p_ip6.ip6_nxt != proto)
184 continue;
185 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) &&
186 !IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &ip6->ip6_dst))
187 continue;
188 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr) &&
189 !IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, &ip6->ip6_src))
190 continue;
191 if (in6p->in6p_cksum != -1) {
192 rip6stat.rip6s_isum++;
193 if (in6_cksum(m, proto, *offp,
194 m->m_pkthdr.len - *offp)) {
195 rip6stat.rip6s_badsum++;
196 continue;
197 }
198 }
199 if (last) {
200 struct mbuf *n;
201 if ((n = m_copy(m, 0, (int)M_COPYALL)) != NULL) {
202 if (last->in6p_flags & IN6P_CONTROLOPTS)
203 ip6_savecontrol(last, &opts, ip6, n);
204 /* strip intermediate headers */
205 m_adj(n, *offp);
206 if (sbappendaddr(&last->in6p_socket->so_rcv,
207 (struct sockaddr *)&rip6src, n, opts) == 0) {
208 /* should notify about lost packet */
209 m_freem(n);
210 if (opts)
211 m_freem(opts);
212 rip6stat.rip6s_fullsock++;
213 } else
214 sorwakeup(last->in6p_socket);
215 opts = NULL;
216 }
217 }
218 last = in6p;
219 }
220 if (last) {
221 if (last->in6p_flags & IN6P_CONTROLOPTS)
222 ip6_savecontrol(last, &opts, ip6, m);
223 /* strip intermediate headers */
224 m_adj(m, *offp);
225 if (sbappendaddr(&last->in6p_socket->so_rcv,
226 (struct sockaddr *)&rip6src, m, opts) == 0) {
227 m_freem(m);
228 if (opts)
229 m_freem(opts);
230 rip6stat.rip6s_fullsock++;
231 } else
232 sorwakeup(last->in6p_socket);
233 } else {
234 rip6stat.rip6s_nosock++;
235 if (m->m_flags & M_MCAST)
236 rip6stat.rip6s_nosockmcast++;
237 if (proto == IPPROTO_NONE)
238 m_freem(m);
239 else {
240 u_int8_t *prvnxtp = ip6_get_prevhdr(m, *offp); /* XXX */
241 in6_ifstat_inc(m->m_pkthdr.rcvif, ifs6_in_protounknown);
242 icmp6_error(m, ICMP6_PARAM_PROB,
243 ICMP6_PARAMPROB_NEXTHEADER,
244 prvnxtp - mtod(m, u_int8_t *));
245 }
246 ip6stat.ip6s_delivered--;
247 }
248 return IPPROTO_DONE;
249 }
250
251 void
rip6_ctlinput(cmd,sa,d)252 rip6_ctlinput(cmd, sa, d)
253 int cmd;
254 struct sockaddr *sa;
255 void *d;
256 {
257 struct ip6_hdr *ip6;
258 struct mbuf *m;
259 int off;
260 struct ip6ctlparam *ip6cp = NULL;
261 const struct sockaddr_in6 *sa6_src = NULL;
262 void *cmdarg;
263 void (*notify)(struct in6pcb *, int) = in6_rtchange;
264 int nxt;
265
266 if (sa->sa_family != AF_INET6 ||
267 sa->sa_len != sizeof(struct sockaddr_in6))
268 return;
269
270 if ((unsigned)cmd >= PRC_NCMDS)
271 return;
272 if (PRC_IS_REDIRECT(cmd))
273 notify = in6_rtchange, d = NULL;
274 else if (cmd == PRC_HOSTDEAD)
275 d = NULL;
276 else if (cmd == PRC_MSGSIZE)
277 ; /* special code is present, see below */
278 else if (inet6ctlerrmap[cmd] == 0)
279 return;
280
281 /* if the parameter is from icmp6, decode it. */
282 if (d != NULL) {
283 ip6cp = (struct ip6ctlparam *)d;
284 m = ip6cp->ip6c_m;
285 ip6 = ip6cp->ip6c_ip6;
286 off = ip6cp->ip6c_off;
287 cmdarg = ip6cp->ip6c_cmdarg;
288 sa6_src = ip6cp->ip6c_src;
289 nxt = ip6cp->ip6c_nxt;
290 } else {
291 m = NULL;
292 ip6 = NULL;
293 cmdarg = NULL;
294 sa6_src = &sa6_any;
295 nxt = -1;
296 }
297
298 if (ip6 && cmd == PRC_MSGSIZE) {
299 struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)sa;
300 int valid = 0;
301 struct in6pcb *in6p;
302
303 /*
304 * Check to see if we have a valid raw IPv6 socket
305 * corresponding to the address in the ICMPv6 message
306 * payload, and the protocol (ip6_nxt) meets the socket.
307 * XXX chase extension headers, or pass final nxt value
308 * from icmp6_notify_error()
309 */
310 in6p = NULL;
311 in6p = in6_pcbhashlookup(&rawin6pcbtable, &sa6->sin6_addr, 0,
312 (struct in6_addr *)&sa6_src->sin6_addr, 0);
313 #if 0
314 if (!in6p) {
315 /*
316 * As the use of sendto(2) is fairly popular,
317 * we may want to allow non-connected pcb too.
318 * But it could be too weak against attacks...
319 * We should at least check if the local
320 * address (= s) is really ours.
321 */
322 in6p = in_pcblookup(&rawin6pcbtable, &sa6->sin6_addr, 0,
323 (struct in6_addr *)&sa6_src->sin6_addr, 0,
324 INPLOOKUP_WILDCARD | INPLOOKUP_IPV6);
325 }
326 #endif
327
328 if (in6p && in6p->in6p_ip6.ip6_nxt &&
329 in6p->in6p_ip6.ip6_nxt == nxt)
330 valid++;
331
332 /*
333 * Depending on the value of "valid" and routing table
334 * size (mtudisc_{hi,lo}wat), we will:
335 * - recalculate the new MTU and create the
336 * corresponding routing entry, or
337 * - ignore the MTU change notification.
338 */
339 icmp6_mtudisc_update((struct ip6ctlparam *)d, valid);
340
341 /*
342 * regardless of if we called icmp6_mtudisc_update(),
343 * we need to call in6_pcbnotify(), to notify path
344 * MTU change to the userland (2292bis-02), because
345 * some unconnected sockets may share the same
346 * destination and want to know the path MTU.
347 */
348 }
349
350 (void) in6_pcbnotify(&rawin6pcbtable, sa, 0,
351 (struct sockaddr *)sa6_src, 0, cmd, cmdarg, notify);
352 }
353
354 /*
355 * Generate IPv6 header and pass packet to ip6_output.
356 * Tack on options user may have setup with control call.
357 */
358 int
rip6_output(struct mbuf * m,...)359 rip6_output(struct mbuf *m, ...)
360 {
361 struct socket *so;
362 struct sockaddr_in6 *dstsock;
363 struct mbuf *control;
364 struct in6_addr *dst;
365 struct ip6_hdr *ip6;
366 struct in6pcb *in6p;
367 u_int plen = m->m_pkthdr.len;
368 int error = 0;
369 struct ip6_pktopts opt, *optp = NULL, *origoptp;
370 struct ifnet *oifp = NULL;
371 /* for ICMPv6 output statistics only */
372 int type = 0, code = 0;
373 int priv = 0;
374 va_list ap;
375 int flags;
376
377 va_start(ap, m);
378 so = va_arg(ap, struct socket *);
379 dstsock = va_arg(ap, struct sockaddr_in6 *);
380 control = va_arg(ap, struct mbuf *);
381 va_end(ap);
382
383 in6p = sotoin6pcb(so);
384
385 priv = 0;
386 if ((so->so_state & SS_PRIV) != 0)
387 priv = 1;
388 dst = &dstsock->sin6_addr;
389 if (control) {
390 if ((error = ip6_setpktoptions(control, &opt, priv)) != 0)
391 goto bad;
392 optp = &opt;
393 } else
394 optp = in6p->in6p_outputopts;
395
396 /*
397 * For an ICMPv6 packet, we should know its type and code
398 * to update statistics.
399 */
400 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6) {
401 struct icmp6_hdr *icmp6;
402 if (m->m_len < sizeof(struct icmp6_hdr) &&
403 (m = m_pullup(m, sizeof(struct icmp6_hdr))) == NULL) {
404 error = ENOBUFS;
405 goto bad;
406 }
407 icmp6 = mtod(m, struct icmp6_hdr *);
408 type = icmp6->icmp6_type;
409 code = icmp6->icmp6_code;
410 }
411
412 M_PREPEND(m, sizeof(*ip6), M_DONTWAIT);
413 if (!m) {
414 error = ENOBUFS;
415 goto bad;
416 }
417 ip6 = mtod(m, struct ip6_hdr *);
418
419 /*
420 * Next header might not be ICMP6 but use its pseudo header anyway.
421 */
422 ip6->ip6_dst = *dst;
423
424 /* KAME hack: embed scopeid */
425 origoptp = in6p->in6p_outputopts;
426 in6p->in6p_outputopts = optp;
427 if (in6_embedscope(&ip6->ip6_dst, dstsock, in6p, &oifp) != 0) {
428 error = EINVAL;
429 goto bad;
430 }
431 in6p->in6p_outputopts = origoptp;
432
433 /*
434 * Source address selection.
435 */
436 {
437 struct in6_addr *in6a;
438
439 if ((in6a = in6_selectsrc(dstsock, optp, in6p->in6p_moptions,
440 &in6p->in6p_route, &in6p->in6p_laddr, &error)) == 0) {
441 if (error == 0)
442 error = EADDRNOTAVAIL;
443 goto bad;
444 }
445 ip6->ip6_src = *in6a;
446 if (in6p->in6p_route.ro_rt) {
447 /* what if oifp contradicts ? */
448 oifp = ifindex2ifnet[in6p->in6p_route.ro_rt->rt_ifp->if_index];
449 }
450 }
451
452 ip6->ip6_flow = in6p->in6p_flowinfo & IPV6_FLOWINFO_MASK;
453 ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
454 ip6->ip6_vfc |= IPV6_VERSION;
455 #if 0 /* ip6_plen will be filled in ip6_output. */
456 ip6->ip6_plen = htons((u_short)plen);
457 #endif
458 ip6->ip6_nxt = in6p->in6p_ip6.ip6_nxt;
459 ip6->ip6_hlim = in6_selecthlim(in6p, oifp);
460
461 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6 ||
462 in6p->in6p_cksum != -1) {
463 struct mbuf *n;
464 int off;
465 u_int16_t *sump;
466 int sumoff;
467
468 /* compute checksum */
469 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6)
470 off = offsetof(struct icmp6_hdr, icmp6_cksum);
471 else
472 off = in6p->in6p_cksum;
473 if (plen < off + 1) {
474 error = EINVAL;
475 goto bad;
476 }
477 off += sizeof(struct ip6_hdr);
478
479 n = m_pulldown(m, off, sizeof(*sump), &sumoff);
480 if (n == NULL) {
481 m = NULL;
482 error = ENOBUFS;
483 goto bad;
484 }
485 sump = (u_int16_t *)(mtod(n, caddr_t) + sumoff);
486 *sump = 0;
487 *sump = in6_cksum(m, ip6->ip6_nxt, sizeof(*ip6), plen);
488 }
489
490 flags = 0;
491 if (in6p->in6p_flags & IN6P_MINMTU)
492 flags |= IPV6_MINMTU;
493
494 error = ip6_output(m, optp, &in6p->in6p_route, flags,
495 in6p->in6p_moptions, &oifp);
496 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6) {
497 if (oifp)
498 icmp6_ifoutstat_inc(oifp, type, code);
499 icmp6stat.icp6s_outhist[type]++;
500 } else
501 rip6stat.rip6s_opackets++;
502
503 goto freectl;
504
505 bad:
506 if (m)
507 m_freem(m);
508
509 freectl:
510 if (optp == &opt && optp->ip6po_rthdr && optp->ip6po_route.ro_rt)
511 RTFREE(optp->ip6po_route.ro_rt);
512 if (control)
513 m_freem(control);
514 return (error);
515 }
516
517 /*
518 * Raw IPv6 socket option processing.
519 */
520 int
rip6_ctloutput(op,so,level,optname,mp)521 rip6_ctloutput(op, so, level, optname, mp)
522 int op;
523 struct socket *so;
524 int level, optname;
525 struct mbuf **mp;
526 {
527 int error = 0;
528
529 switch (level) {
530 case IPPROTO_IPV6:
531 switch (optname) {
532 case MRT6_INIT:
533 case MRT6_DONE:
534 case MRT6_ADD_MIF:
535 case MRT6_DEL_MIF:
536 case MRT6_ADD_MFC:
537 case MRT6_DEL_MFC:
538 case MRT6_PIM:
539 if (op == PRCO_SETOPT) {
540 error = ip6_mrouter_set(optname, so, *mp);
541 if (*mp)
542 (void)m_free(*mp);
543 } else if (op == PRCO_GETOPT)
544 error = ip6_mrouter_get(optname, so, mp);
545 else
546 error = EINVAL;
547 return (error);
548 case IPV6_CHECKSUM:
549 return (ip6_raw_ctloutput(op, so, level, optname, mp));
550 default:
551 return (ip6_ctloutput(op, so, level, optname, mp));
552 }
553
554 case IPPROTO_ICMPV6:
555 /*
556 * XXX: is it better to call icmp6_ctloutput() directly
557 * from protosw?
558 */
559 return (icmp6_ctloutput(op, so, level, optname, mp));
560
561 default:
562 if (op == PRCO_SETOPT && *mp)
563 m_free(*mp);
564 return EINVAL;
565 }
566 }
567
568 extern u_long rip6_sendspace;
569 extern u_long rip6_recvspace;
570
571 int
rip6_usrreq(so,req,m,nam,control,p)572 rip6_usrreq(so, req, m, nam, control, p)
573 struct socket *so;
574 int req;
575 struct mbuf *m, *nam, *control;
576 struct proc *p;
577 {
578 struct in6pcb *in6p = sotoin6pcb(so);
579 int s;
580 int error = 0;
581 int priv;
582
583 priv = 0;
584 if ((so->so_state & SS_PRIV) != 0)
585 priv++;
586
587 if (req == PRU_CONTROL)
588 return (in6_control(so, (u_long)m, (caddr_t)nam,
589 (struct ifnet *)control, p));
590
591 switch (req) {
592 case PRU_ATTACH:
593 if (in6p)
594 panic("rip6_attach");
595 if (!priv) {
596 error = EACCES;
597 break;
598 }
599 s = splsoftnet();
600 if ((error = soreserve(so, rip6_sendspace, rip6_recvspace)) != 0) {
601 splx(s);
602 break;
603 }
604 if ((error = in_pcballoc(so, &rawin6pcbtable)) != 0)
605 {
606 splx(s);
607 break;
608 }
609 splx(s);
610 in6p = sotoin6pcb(so);
611 in6p->in6p_ip6.ip6_nxt = (long)nam;
612 in6p->in6p_cksum = -1;
613
614 MALLOC(in6p->in6p_icmp6filt, struct icmp6_filter *,
615 sizeof(struct icmp6_filter), M_PCB, M_NOWAIT);
616 if (in6p->in6p_icmp6filt == NULL) {
617 in6_pcbdetach(in6p);
618 error = ENOMEM;
619 break;
620 }
621 ICMP6_FILTER_SETPASSALL(in6p->in6p_icmp6filt);
622 break;
623
624 case PRU_DISCONNECT:
625 if ((so->so_state & SS_ISCONNECTED) == 0) {
626 error = ENOTCONN;
627 break;
628 }
629 in6p->in6p_faddr = in6addr_any;
630 so->so_state &= ~SS_ISCONNECTED; /* XXX */
631 break;
632
633 case PRU_ABORT:
634 soisdisconnected(so);
635 /* Fallthrough */
636 case PRU_DETACH:
637 if (in6p == 0)
638 panic("rip6_detach");
639 if (so == ip6_mrouter)
640 ip6_mrouter_done();
641 /* xxx: RSVP */
642 if (in6p->in6p_icmp6filt) {
643 FREE(in6p->in6p_icmp6filt, M_PCB);
644 in6p->in6p_icmp6filt = NULL;
645 }
646 in6_pcbdetach(in6p);
647 break;
648
649 case PRU_BIND:
650 {
651 struct sockaddr_in6 *addr = mtod(nam, struct sockaddr_in6 *);
652 struct ifaddr *ia = NULL;
653
654 if (nam->m_len != sizeof(*addr)) {
655 error = EINVAL;
656 break;
657 }
658 if ((ifnet.tqh_first == 0) || (addr->sin6_family != AF_INET6)) {
659 error = EADDRNOTAVAIL;
660 break;
661 }
662 #ifdef ENABLE_DEFAULT_SCOPE
663 if (addr->sin6_scope_id == 0) /* not change if specified */
664 addr->sin6_scope_id =
665 scope6_addr2default(&addr->sin6_addr);
666 #endif
667 /*
668 * we don't support mapped address here, it would confuse
669 * users so reject it
670 */
671 if (IN6_IS_ADDR_V4MAPPED(&addr->sin6_addr)) {
672 error = EADDRNOTAVAIL;
673 break;
674 }
675 /*
676 * Currently, ifa_ifwithaddr tends to fail for a link-local
677 * address, since it implicitly expects that the link ID
678 * for the address is embedded in the sin6_addr part.
679 * For now, we'd rather keep this "as is". We'll eventually fix
680 * this in a more natural way.
681 */
682 if (!IN6_IS_ADDR_UNSPECIFIED(&addr->sin6_addr) &&
683 (ia = ifa_ifwithaddr((struct sockaddr *)addr)) == 0) {
684 error = EADDRNOTAVAIL;
685 break;
686 }
687 if (ia && ((struct in6_ifaddr *)ia)->ia6_flags &
688 (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY|
689 IN6_IFF_DETACHED|IN6_IFF_DEPRECATED)) {
690 error = EADDRNOTAVAIL;
691 break;
692 }
693 in6p->in6p_laddr = addr->sin6_addr;
694 break;
695 }
696
697 case PRU_CONNECT:
698 {
699 struct sockaddr_in6 *addr = mtod(nam, struct sockaddr_in6 *);
700 struct in6_addr *in6a = NULL;
701 #ifdef ENABLE_DEFAULT_SCOPE
702 struct sockaddr_in6 sin6;
703 #endif
704
705 if (nam->m_len != sizeof(*addr)) {
706 error = EINVAL;
707 break;
708 }
709 if (ifnet.tqh_first == 0)
710 {
711 error = EADDRNOTAVAIL;
712 break;
713 }
714 if (addr->sin6_family != AF_INET6) {
715 error = EAFNOSUPPORT;
716 break;
717 }
718
719 #ifdef ENABLE_DEFAULT_SCOPE
720 if (addr->sin6_scope_id == 0) {
721 /* protect *addr */
722 sin6 = *addr;
723 addr = &sin6;
724 addr->sin6_scope_id =
725 scope6_addr2default(&addr->sin6_addr);
726 }
727 #endif
728
729 /* Source address selection. XXX: need pcblookup? */
730 in6a = in6_selectsrc(addr, in6p->in6p_outputopts,
731 in6p->in6p_moptions, &in6p->in6p_route,
732 &in6p->in6p_laddr, &error);
733 if (in6a == NULL) {
734 if (error == 0)
735 error = EADDRNOTAVAIL;
736 break;
737 }
738 in6p->in6p_laddr = *in6a;
739 in6p->in6p_faddr = addr->sin6_addr;
740 soisconnected(so);
741 break;
742 }
743
744 case PRU_CONNECT2:
745 error = EOPNOTSUPP;
746 break;
747
748 /*
749 * Mark the connection as being incapable of futther input.
750 */
751 case PRU_SHUTDOWN:
752 socantsendmore(so);
753 break;
754 /*
755 * Ship a packet out. The appropriate raw output
756 * routine handles any messaging necessary.
757 */
758 case PRU_SEND:
759 {
760 struct sockaddr_in6 tmp;
761 struct sockaddr_in6 *dst;
762
763 /* always copy sockaddr to avoid overwrites */
764 if (so->so_state & SS_ISCONNECTED) {
765 if (nam) {
766 error = EISCONN;
767 break;
768 }
769 /* XXX */
770 bzero(&tmp, sizeof(tmp));
771 tmp.sin6_family = AF_INET6;
772 tmp.sin6_len = sizeof(struct sockaddr_in6);
773 bcopy(&in6p->in6p_faddr, &tmp.sin6_addr,
774 sizeof(struct in6_addr));
775 dst = &tmp;
776 } else {
777 if (nam == NULL) {
778 error = ENOTCONN;
779 break;
780 }
781 if (nam->m_len != sizeof(tmp)) {
782 error = EINVAL;
783 break;
784 }
785
786 tmp = *mtod(nam, struct sockaddr_in6 *);
787 dst = &tmp;
788
789 if (dst->sin6_family != AF_INET6) {
790 error = EAFNOSUPPORT;
791 break;
792 }
793 }
794 #ifdef ENABLE_DEFAULT_SCOPE
795 if (dst->sin6_scope_id == 0) {
796 dst->sin6_scope_id =
797 scope6_addr2default(&dst->sin6_addr);
798 }
799 #endif
800 error = rip6_output(m, so, dst, control);
801 m = NULL;
802 break;
803 }
804
805 case PRU_SENSE:
806 /*
807 * stat: don't bother with a blocksize
808 */
809 return (0);
810 /*
811 * Not supported.
812 */
813 case PRU_RCVOOB:
814 case PRU_RCVD:
815 case PRU_LISTEN:
816 case PRU_ACCEPT:
817 case PRU_SENDOOB:
818 error = EOPNOTSUPP;
819 break;
820
821 case PRU_SOCKADDR:
822 in6_setsockaddr(in6p, nam);
823 break;
824
825 case PRU_PEERADDR:
826 in6_setpeeraddr(in6p, nam);
827 break;
828
829 default:
830 panic("rip6_usrreq");
831 }
832 if (m != NULL)
833 m_freem(m);
834 return (error);
835 }
836