]> git.saurik.com Git - apple/xnu.git/blob - bsd/netinet6/raw_ip6.c
762258e4554d67e4d1ec39f5a64660f39820b12a
[apple/xnu.git] / bsd / netinet6 / raw_ip6.c
1 /*
2 * Copyright (c) 2008 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28 /*
29 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
30 * All rights reserved.
31 *
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
34 * are met:
35 * 1. Redistributions of source code must retain the above copyright
36 * notice, this list of conditions and the following disclaimer.
37 * 2. Redistributions in binary form must reproduce the above copyright
38 * notice, this list of conditions and the following disclaimer in the
39 * documentation and/or other materials provided with the distribution.
40 * 3. Neither the name of the project nor the names of its contributors
41 * may be used to endorse or promote products derived from this software
42 * without specific prior written permission.
43 *
44 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
45 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
46 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
47 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
48 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
49 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
50 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
51 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
52 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
53 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
54 * SUCH DAMAGE.
55 *
56 * $FreeBSD: src/sys/netinet6/raw_ip6.c,v 1.7.2.4 2001/07/29 19:32:40 ume Exp $
57 */
58
59 /*
60 * Copyright (c) 1982, 1986, 1988, 1993
61 * The Regents of the University of California. All rights reserved.
62 *
63 * Redistribution and use in source and binary forms, with or without
64 * modification, are permitted provided that the following conditions
65 * are met:
66 * 1. Redistributions of source code must retain the above copyright
67 * notice, this list of conditions and the following disclaimer.
68 * 2. Redistributions in binary form must reproduce the above copyright
69 * notice, this list of conditions and the following disclaimer in the
70 * documentation and/or other materials provided with the distribution.
71 * 3. All advertising materials mentioning features or use of this software
72 * must display the following acknowledgement:
73 * This product includes software developed by the University of
74 * California, Berkeley and its contributors.
75 * 4. Neither the name of the University nor the names of its contributors
76 * may be used to endorse or promote products derived from this software
77 * without specific prior written permission.
78 *
79 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
80 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
81 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
82 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
83 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
84 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
85 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
86 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
87 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
88 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
89 * SUCH DAMAGE.
90 *
91 * @(#)raw_ip.c 8.2 (Berkeley) 1/4/94
92 */
93 #include <sys/param.h>
94 #include <sys/malloc.h>
95 #include <sys/proc.h>
96 #include <sys/mbuf.h>
97 #include <sys/socket.h>
98 #include <sys/protosw.h>
99 #include <sys/socketvar.h>
100 #include <sys/errno.h>
101 #include <sys/systm.h>
102
103 #include <net/if.h>
104 #include <net/route.h>
105 #include <net/if_types.h>
106
107 #include <netinet/in.h>
108 #include <netinet/in_var.h>
109 #include <netinet/in_systm.h>
110 #include <netinet/ip6.h>
111 #include <netinet6/ip6_var.h>
112 #include <netinet6/ip6_mroute.h>
113 #include <netinet/icmp6.h>
114 #include <netinet/in_pcb.h>
115 #include <netinet6/in6_pcb.h>
116 #include <netinet6/nd6.h>
117 #include <netinet6/ip6protosw.h>
118 #if ENABLE_DEFAULT_SCOPE
119 #include <netinet6/scope6_var.h>
120 #endif
121 #include <netinet6/raw_ip6.h>
122 #include <netinet6/ip6_fw.h>
123
124 #if IPSEC
125 #include <netinet6/ipsec.h>
126 #include <netinet6/ipsec6.h>
127 extern int ipsec_bypass;
128 #endif /*IPSEC*/
129
130 #define satosin6(sa) ((struct sockaddr_in6 *)(sa))
131 #define ifatoia6(ifa) ((struct in6_ifaddr *)(ifa))
132
133 /*
134 * Raw interface to IP6 protocol.
135 */
136
137 extern struct inpcbhead ripcb;
138 extern struct inpcbinfo ripcbinfo;
139 extern u_int32_t rip_sendspace;
140 extern u_int32_t rip_recvspace;
141
142 struct rip6stat rip6stat;
143
144 /*
145 * Setup generic address and protocol structures
146 * for raw_input routine, then pass them along with
147 * mbuf chain.
148 */
149 int
150 rip6_input(
151 struct mbuf **mp,
152 int *offp)
153 {
154 struct mbuf *m = *mp;
155 register struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
156 register struct inpcb *in6p;
157 struct inpcb *last = 0;
158 struct mbuf *opts = NULL;
159 struct sockaddr_in6 rip6src;
160 int proto = ip6->ip6_nxt;
161
162 rip6stat.rip6s_ipackets++;
163
164 #if defined(NFAITH) && 0 < NFAITH
165 if (faithprefix(&ip6->ip6_dst)) {
166 /* XXX send icmp6 host/port unreach? */
167 m_freem(m);
168 return IPPROTO_DONE;
169 }
170 #endif
171
172 init_sin6(&rip6src, m); /* general init */
173
174 lck_rw_lock_shared(ripcbinfo.mtx);
175 LIST_FOREACH(in6p, &ripcb, inp_list) {
176 if ((in6p->in6p_vflag & INP_IPV6) == 0)
177 continue;
178 if (in6p->in6p_ip6_nxt &&
179 in6p->in6p_ip6_nxt != proto)
180 continue;
181 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) &&
182 !IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &ip6->ip6_dst))
183 continue;
184 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr) &&
185 !IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, &ip6->ip6_src))
186 continue;
187 if (in6p->in6p_cksum != -1) {
188 rip6stat.rip6s_isum++;
189 if (in6_cksum(m, ip6->ip6_nxt, *offp,
190 m->m_pkthdr.len - *offp)) {
191 rip6stat.rip6s_badsum++;
192 continue;
193 }
194 }
195 if (last) {
196 struct mbuf *n = m_copy(m, 0, (int)M_COPYALL);
197
198 #if IPSEC
199 /*
200 * Check AH/ESP integrity.
201 */
202 if (ipsec_bypass == 0 && n && ipsec6_in_reject_so(n, last->inp_socket)) {
203 m_freem(n);
204 IPSEC_STAT_INCREMENT(ipsec6stat.in_polvio);
205 /* do not inject data into pcb */
206 } else
207 #endif /*IPSEC*/
208 if (n) {
209 if (last->in6p_flags & IN6P_CONTROLOPTS ||
210 last->in6p_socket->so_options & SO_TIMESTAMP)
211 ip6_savecontrol(last, &opts, ip6, n);
212 /* strip intermediate headers */
213 m_adj(n, *offp);
214 if (sbappendaddr(&last->in6p_socket->so_rcv,
215 (struct sockaddr *)&rip6src,
216 n, opts, NULL) == 0) {
217 rip6stat.rip6s_fullsock++;
218 } else
219 sorwakeup(last->in6p_socket);
220 opts = NULL;
221 }
222 }
223 last = in6p;
224 }
225 lck_rw_done(ripcbinfo.mtx);
226 #if IPSEC
227 /*
228 * Check AH/ESP integrity.
229 */
230 if (ipsec_bypass == 0 && last && ipsec6_in_reject_so(m, last->inp_socket)) {
231 m_freem(m);
232 IPSEC_STAT_INCREMENT(ipsec6stat.in_polvio);
233 ip6stat.ip6s_delivered--;
234 /* do not inject data into pcb */
235 } else
236 #endif /*IPSEC*/
237 if (last) {
238 if (last->in6p_flags & IN6P_CONTROLOPTS ||
239 last->in6p_socket->so_options & SO_TIMESTAMP)
240 ip6_savecontrol(last, &opts, ip6, m);
241 /* strip intermediate headers */
242 m_adj(m, *offp);
243 if (sbappendaddr(&last->in6p_socket->so_rcv,
244 (struct sockaddr *)&rip6src, m, opts, NULL) == 0) {
245 rip6stat.rip6s_fullsock++;
246 } else
247 sorwakeup(last->in6p_socket);
248 } else {
249 rip6stat.rip6s_nosock++;
250 if (m->m_flags & M_MCAST)
251 rip6stat.rip6s_nosockmcast++;
252 if (proto == IPPROTO_NONE)
253 m_freem(m);
254 else {
255 char *prvnxtp = ip6_get_prevhdr(m, *offp); /* XXX */
256 icmp6_error(m, ICMP6_PARAM_PROB,
257 ICMP6_PARAMPROB_NEXTHEADER,
258 prvnxtp - mtod(m, char *));
259 }
260 ip6stat.ip6s_delivered--;
261 }
262 return IPPROTO_DONE;
263 }
264
265 void
266 rip6_ctlinput(
267 int cmd,
268 struct sockaddr *sa,
269 void *d)
270 {
271 struct ip6_hdr *ip6;
272 struct mbuf *m;
273 int off = 0;
274 struct ip6ctlparam *ip6cp = NULL;
275 const struct sockaddr_in6 *sa6_src = NULL;
276 void (*notify)(struct inpcb *, int) = in6_rtchange;
277
278 if (sa->sa_family != AF_INET6 ||
279 sa->sa_len != sizeof(struct sockaddr_in6))
280 return;
281
282 if ((unsigned)cmd >= PRC_NCMDS)
283 return;
284 if (PRC_IS_REDIRECT(cmd))
285 notify = in6_rtchange, d = NULL;
286 else if (cmd == PRC_HOSTDEAD)
287 d = NULL;
288 else if (inet6ctlerrmap[cmd] == 0)
289 return;
290
291 /* if the parameter is from icmp6, decode it. */
292 if (d != NULL) {
293 ip6cp = (struct ip6ctlparam *)d;
294 m = ip6cp->ip6c_m;
295 ip6 = ip6cp->ip6c_ip6;
296 off = ip6cp->ip6c_off;
297 sa6_src = ip6cp->ip6c_src;
298 } else {
299 m = NULL;
300 ip6 = NULL;
301 sa6_src = &sa6_any;
302 }
303
304 (void) in6_pcbnotify(&ripcbinfo, sa, 0, (const struct sockaddr *)sa6_src,
305 0, cmd, notify);
306 }
307
308 /*
309 * Generate IPv6 header and pass packet to ip6_output.
310 * Tack on options user may have setup with control call.
311 */
312 int
313 rip6_output(
314 register struct mbuf *m,
315 struct socket *so,
316 struct sockaddr_in6 *dstsock,
317 struct mbuf *control)
318 {
319 struct in6_addr *dst;
320 struct ip6_hdr *ip6;
321 struct inpcb *in6p;
322 u_int plen = m->m_pkthdr.len;
323 int error = 0;
324 struct ip6_pktopts opt, *optp = 0;
325 struct ifnet *oifp = NULL;
326 int type = 0, code = 0; /* for ICMPv6 output statistics only */
327 int priv = 0;
328
329 in6p = sotoin6pcb(so);
330
331 priv = 0;
332 if (so->so_uid == 0)
333 priv = 1;
334 dst = &dstsock->sin6_addr;
335 if (control) {
336 if ((error = ip6_setpktoptions(control, &opt, priv, 0)) != 0)
337 goto bad;
338 optp = &opt;
339 } else
340 optp = in6p->in6p_outputopts;
341
342 /*
343 * For an ICMPv6 packet, we should know its type and code
344 * to update statistics.
345 */
346 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6) {
347 struct icmp6_hdr *icmp6;
348 if (m->m_len < sizeof(struct icmp6_hdr) &&
349 (m = m_pullup(m, sizeof(struct icmp6_hdr))) == NULL) {
350 error = ENOBUFS;
351 goto bad;
352 }
353 icmp6 = mtod(m, struct icmp6_hdr *);
354 type = icmp6->icmp6_type;
355 code = icmp6->icmp6_code;
356 }
357
358 M_PREPEND(m, sizeof(*ip6), M_WAIT);
359 if (m == NULL) {
360 error = ENOBUFS;
361 goto bad;
362 }
363 ip6 = mtod(m, struct ip6_hdr *);
364
365 /*
366 * Next header might not be ICMP6 but use its pseudo header anyway.
367 */
368 ip6->ip6_dst = *dst;
369
370 /*
371 * If the scope of the destination is link-local, embed the interface
372 * index in the address.
373 *
374 * XXX advanced-api value overrides sin6_scope_id
375 */
376 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst)) {
377 struct in6_pktinfo *pi;
378
379 /*
380 * XXX Boundary check is assumed to be already done in
381 * ip6_setpktoptions().
382 */
383 ifnet_head_lock_shared();
384 if (optp && (pi = optp->ip6po_pktinfo) && pi->ipi6_ifindex) {
385 ip6->ip6_dst.s6_addr16[1] = htons(pi->ipi6_ifindex);
386 oifp = ifindex2ifnet[pi->ipi6_ifindex];
387 } else if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) &&
388 in6p->in6p_moptions &&
389 in6p->in6p_moptions->im6o_multicast_ifp) {
390 oifp = in6p->in6p_moptions->im6o_multicast_ifp;
391 ip6->ip6_dst.s6_addr16[1] = htons(oifp->if_index);
392 } else if (dstsock->sin6_scope_id) {
393 /*
394 * boundary check
395 *
396 * Sinced stsock->sin6_scope_id is unsigned, we don't
397 * need to check if it's < 0
398 */
399 if (if_index < dstsock->sin6_scope_id) {
400 error = ENXIO; /* XXX EINVAL? */
401 ifnet_head_done();
402 goto bad;
403 }
404 ip6->ip6_dst.s6_addr16[1]
405 = htons(dstsock->sin6_scope_id & 0xffff);/*XXX*/
406 }
407 ifnet_head_done();
408 }
409
410 /*
411 * Source address selection.
412 */
413 {
414 struct in6_addr *in6a;
415 struct in6_addr storage;
416 u_short index = 0;
417 if ((in6a = in6_selectsrc(dstsock, optp,
418 in6p->in6p_moptions,
419 &in6p->in6p_route,
420 &in6p->in6p_laddr,
421 &storage, &error)) == 0) {
422 if (error == 0)
423 error = EADDRNOTAVAIL;
424 goto bad;
425 }
426 ip6->ip6_src = *in6a;
427 if (in6p->in6p_route.ro_rt != NULL) {
428 RT_LOCK(in6p->in6p_route.ro_rt);
429 if (in6p->in6p_route.ro_rt->rt_ifp != NULL)
430 index = in6p->in6p_route.ro_rt->rt_ifp->if_index;
431 RT_UNLOCK(in6p->in6p_route.ro_rt);
432 ifnet_head_lock_shared();
433 if (index == 0 || if_index < index) {
434 panic("bad if_index on interface from route");
435 }
436 oifp = ifindex2ifnet[index];
437 ifnet_head_done();
438 }
439 }
440 ip6->ip6_flow = (ip6->ip6_flow & ~IPV6_FLOWINFO_MASK) |
441 (in6p->in6p_flowinfo & IPV6_FLOWINFO_MASK);
442 ip6->ip6_vfc = (ip6->ip6_vfc & ~IPV6_VERSION_MASK) |
443 (IPV6_VERSION & IPV6_VERSION_MASK);
444 /* ip6_plen will be filled in ip6_output, so not fill it here. */
445 ip6->ip6_nxt = in6p->in6p_ip6_nxt;
446 ip6->ip6_hlim = in6_selecthlim(in6p, oifp);
447
448 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6 ||
449 in6p->in6p_cksum != -1) {
450 struct mbuf *n;
451 int off;
452 u_int16_t *p;
453
454 /* compute checksum */
455 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6)
456 off = offsetof(struct icmp6_hdr, icmp6_cksum);
457 else
458 off = in6p->in6p_cksum;
459 if (plen < off + 1) {
460 error = EINVAL;
461 goto bad;
462 }
463 off += sizeof(struct ip6_hdr);
464
465 n = m;
466 while (n && n->m_len <= off) {
467 off -= n->m_len;
468 n = n->m_next;
469 }
470 if (!n)
471 goto bad;
472 p = (u_int16_t *)(mtod(n, caddr_t) + off);
473 *p = 0;
474 *p = in6_cksum(m, ip6->ip6_nxt, sizeof(*ip6), plen);
475 }
476
477 #if IPSEC
478 if (ipsec_bypass == 0 && ipsec_setsocket(m, so) != 0) {
479 error = ENOBUFS;
480 goto bad;
481 }
482 #endif /*IPSEC*/
483
484 if (in6p->in6p_route.ro_rt != NULL &&
485 in6p->in6p_route.ro_rt->generation_id != route_generation) {
486 rtfree(in6p->in6p_route.ro_rt);
487 in6p->in6p_route.ro_rt = NULL;
488 }
489
490 error = ip6_output(m, optp, &in6p->in6p_route, 0,
491 in6p->in6p_moptions, &oifp, 0);
492 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6) {
493 if (oifp)
494 icmp6_ifoutstat_inc(oifp, type, code);
495 icmp6stat.icp6s_outhist[type]++;
496 } else
497 rip6stat.rip6s_opackets++;
498
499 goto freectl;
500
501 bad:
502 if (m)
503 m_freem(m);
504
505 freectl:
506 if (optp == &opt && optp->ip6po_rthdr && optp->ip6po_route.ro_rt) {
507 rtfree(optp->ip6po_route.ro_rt);
508 optp->ip6po_route.ro_rt = NULL;
509 }
510 if (control) {
511 if (optp == &opt)
512 ip6_clearpktopts(optp, 0, -1);
513 m_freem(control);
514 }
515 return(error);
516 }
517
518 #if IPFW2
519 static void
520 load_ip6fw(void)
521 {
522 ip6_fw_init();
523 }
524 #endif
525
526 /*
527 * Raw IPv6 socket option processing.
528 */
529 int
530 rip6_ctloutput(
531 struct socket *so,
532 struct sockopt *sopt)
533 {
534 int error;
535
536 if (sopt->sopt_level == IPPROTO_ICMPV6)
537 /*
538 * XXX: is it better to call icmp6_ctloutput() directly
539 * from protosw?
540 */
541 return(icmp6_ctloutput(so, sopt));
542 else if (sopt->sopt_level != IPPROTO_IPV6)
543 return (EINVAL);
544
545 error = 0;
546
547 switch (sopt->sopt_dir) {
548 case SOPT_GET:
549 switch (sopt->sopt_name) {
550 #if IPFW2
551 case IPV6_FW_ADD:
552 case IPV6_FW_GET:
553 if (ip6_fw_ctl_ptr == 0)
554 load_ip6fw();
555 if (ip6_fw_ctl_ptr)
556 error = ip6_fw_ctl_ptr(sopt);
557 else
558 error = ENOPROTOOPT;
559 break;
560 #endif
561
562 case MRT6_INIT:
563 case MRT6_DONE:
564 case MRT6_ADD_MIF:
565 case MRT6_DEL_MIF:
566 case MRT6_ADD_MFC:
567 case MRT6_DEL_MFC:
568 case MRT6_PIM:
569 error = ip6_mrouter_get(so, sopt);
570 break;
571 default:
572 error = ip6_ctloutput(so, sopt);
573 break;
574 }
575 break;
576
577 case SOPT_SET:
578 switch (sopt->sopt_name) {
579 #if IPFW2
580 case IPV6_FW_ADD:
581 case IPV6_FW_DEL:
582 case IPV6_FW_FLUSH:
583 case IPV6_FW_ZERO:
584 if (ip6_fw_ctl_ptr == 0)
585 load_ip6fw();
586 if (ip6_fw_ctl_ptr)
587 error = ip6_fw_ctl_ptr(sopt);
588 else
589 error = ENOPROTOOPT;
590 break;
591 #endif
592
593 case MRT6_INIT:
594 case MRT6_DONE:
595 case MRT6_ADD_MIF:
596 case MRT6_DEL_MIF:
597 case MRT6_ADD_MFC:
598 case MRT6_DEL_MFC:
599 case MRT6_PIM:
600 error = ip6_mrouter_set(so, sopt);
601 break;
602 default:
603 error = ip6_ctloutput(so, sopt);
604 break;
605 }
606 break;
607 }
608
609 return (error);
610 }
611
612 static int
613 rip6_attach(struct socket *so, int proto, struct proc *p)
614 {
615 struct inpcb *inp;
616 int error;
617
618 inp = sotoinpcb(so);
619 if (inp)
620 panic("rip6_attach");
621 if ((error = proc_suser(p)) != 0)
622 return error;
623
624 error = soreserve(so, rip_sendspace, rip_recvspace);
625 if (error)
626 return error;
627 error = in_pcballoc(so, &ripcbinfo, p);
628 if (error)
629 return error;
630 inp = (struct inpcb *)so->so_pcb;
631 inp->inp_vflag |= INP_IPV6;
632 inp->in6p_ip6_nxt = (char)proto;
633 inp->in6p_hops = -1; /* use kernel default */
634 inp->in6p_cksum = -1;
635 MALLOC(inp->in6p_icmp6filt, struct icmp6_filter *,
636 sizeof(struct icmp6_filter), M_PCB, M_WAITOK);
637 if (inp->in6p_icmp6filt == NULL)
638 return (ENOMEM);
639 ICMP6_FILTER_SETPASSALL(inp->in6p_icmp6filt);
640 return 0;
641 }
642
643 static int
644 rip6_detach(struct socket *so)
645 {
646 struct inpcb *inp;
647
648 inp = sotoinpcb(so);
649 if (inp == 0)
650 panic("rip6_detach");
651 /* xxx: RSVP */
652 if (so == ip6_mrouter)
653 ip6_mrouter_done();
654 if (inp->in6p_icmp6filt) {
655 FREE(inp->in6p_icmp6filt, M_PCB);
656 inp->in6p_icmp6filt = NULL;
657 }
658 in6_pcbdetach(inp);
659 return 0;
660 }
661
662 static int
663 rip6_abort(struct socket *so)
664 {
665 soisdisconnected(so);
666 return rip6_detach(so);
667 }
668
669 static int
670 rip6_disconnect(struct socket *so)
671 {
672 struct inpcb *inp = sotoinpcb(so);
673
674 if ((so->so_state & SS_ISCONNECTED) == 0)
675 return ENOTCONN;
676 inp->in6p_faddr = in6addr_any;
677 return rip6_abort(so);
678 }
679
680 static int
681 rip6_bind(struct socket *so, struct sockaddr *nam, __unused struct proc *p)
682 {
683 struct inpcb *inp = sotoinpcb(so);
684 struct sockaddr_in6 *addr = (struct sockaddr_in6 *)nam;
685 struct ifaddr *ia = NULL;
686
687 if (nam->sa_len != sizeof(*addr))
688 return EINVAL;
689
690 if (TAILQ_EMPTY(&ifnet_head) || addr->sin6_family != AF_INET6)
691 return EADDRNOTAVAIL;
692 #if ENABLE_DEFAULT_SCOPE
693 if (addr->sin6_scope_id == 0) { /* not change if specified */
694 addr->sin6_scope_id = scope6_addr2default(&addr->sin6_addr);
695 }
696 #endif
697 if (!IN6_IS_ADDR_UNSPECIFIED(&addr->sin6_addr) &&
698 (ia = ifa_ifwithaddr((struct sockaddr *)addr)) == 0)
699 return EADDRNOTAVAIL;
700 if (ia &&
701 ((struct in6_ifaddr *)ia)->ia6_flags &
702 (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY|
703 IN6_IFF_DETACHED|IN6_IFF_DEPRECATED)) {
704 if (ia) ifafree(ia);
705 return(EADDRNOTAVAIL);
706 }
707 if (ia != NULL)
708 ifafree(ia);
709 inp->in6p_laddr = addr->sin6_addr;
710 return 0;
711 }
712
713 static int
714 rip6_connect(struct socket *so, struct sockaddr *nam, __unused struct proc *p)
715 {
716 struct inpcb *inp = sotoinpcb(so);
717 struct sockaddr_in6 *addr = (struct sockaddr_in6 *)nam;
718 struct in6_addr *in6a = NULL;
719 struct in6_addr storage;
720 int error = 0;
721 #if ENABLE_DEFAULT_SCOPE
722 struct sockaddr_in6 tmp;
723 #endif
724
725 if (nam->sa_len != sizeof(*addr))
726 return EINVAL;
727 if (TAILQ_EMPTY(&ifnet_head))
728 return EADDRNOTAVAIL;
729 if (addr->sin6_family != AF_INET6)
730 return EAFNOSUPPORT;
731 #if ENABLE_DEFAULT_SCOPE
732 if (addr->sin6_scope_id == 0) { /* not change if specified */
733 /* avoid overwrites */
734 tmp = *addr;
735 addr = &tmp;
736 addr->sin6_scope_id = scope6_addr2default(&addr->sin6_addr);
737 }
738 #endif
739 /* Source address selection. XXX: need pcblookup? */
740 in6a = in6_selectsrc(addr, inp->in6p_outputopts,
741 inp->in6p_moptions, &inp->in6p_route,
742 &inp->in6p_laddr, &storage, &error);
743 if (in6a == NULL)
744 return (error ? error : EADDRNOTAVAIL);
745 inp->in6p_laddr = *in6a;
746 inp->in6p_faddr = addr->sin6_addr;
747 soisconnected(so);
748 return 0;
749 }
750
751 static int
752 rip6_shutdown(struct socket *so)
753 {
754 socantsendmore(so);
755 return 0;
756 }
757
758 static int
759 rip6_send(struct socket *so, __unused int flags, struct mbuf *m, struct sockaddr *nam,
760 struct mbuf *control, __unused struct proc *p)
761 {
762 struct inpcb *inp = sotoinpcb(so);
763 struct sockaddr_in6 tmp;
764 struct sockaddr_in6 *dst;
765
766 /* always copy sockaddr to avoid overwrites */
767 if (so->so_state & SS_ISCONNECTED) {
768 if (nam) {
769 m_freem(m);
770 return EISCONN;
771 }
772 /* XXX */
773 bzero(&tmp, sizeof(tmp));
774 tmp.sin6_family = AF_INET6;
775 tmp.sin6_len = sizeof(struct sockaddr_in6);
776 bcopy(&inp->in6p_faddr, &tmp.sin6_addr,
777 sizeof(struct in6_addr));
778 dst = &tmp;
779 } else {
780 if (nam == NULL) {
781 m_freem(m);
782 return ENOTCONN;
783 }
784 tmp = *(struct sockaddr_in6 *)nam;
785 dst = &tmp;
786 }
787 #if ENABLE_DEFAULT_SCOPE
788 if (dst->sin6_scope_id == 0) { /* not change if specified */
789 dst->sin6_scope_id = scope6_addr2default(&dst->sin6_addr);
790 }
791 #endif
792 return rip6_output(m, so, dst, control);
793 }
794
795 struct pr_usrreqs rip6_usrreqs = {
796 rip6_abort, pru_accept_notsupp, rip6_attach, rip6_bind, rip6_connect,
797 pru_connect2_notsupp, in6_control, rip6_detach, rip6_disconnect,
798 pru_listen_notsupp, in6_setpeeraddr, pru_rcvd_notsupp,
799 pru_rcvoob_notsupp, rip6_send, pru_sense_null, rip6_shutdown,
800 in6_setsockaddr, sosend, soreceive, pru_sopoll_notsupp
801 };
802
803 __private_extern__ struct pr_usrreqs icmp6_dgram_usrreqs = {
804 rip6_abort, pru_accept_notsupp, icmp6_dgram_attach, rip6_bind, rip6_connect,
805 pru_connect2_notsupp, in6_control, rip6_detach, rip6_disconnect,
806 pru_listen_notsupp, in6_setpeeraddr, pru_rcvd_notsupp,
807 pru_rcvoob_notsupp, icmp6_dgram_send, pru_sense_null, rip6_shutdown,
808 in6_setsockaddr, sosend, soreceive, pru_sopoll_notsupp
809 };
810
811
812