]> git.saurik.com Git - apple/xnu.git/blame - bsd/netinet6/raw_ip6.c
xnu-344.tar.gz
[apple/xnu.git] / bsd / netinet6 / raw_ip6.c
CommitLineData
1c79356b
A
1/*
2 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
3 * All rights reserved.
9bccf70c 4 *
1c79356b
A
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.
9bccf70c 16 *
1c79356b
A
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.
9bccf70c
A
28 *
29 * $FreeBSD: src/sys/netinet6/raw_ip6.c,v 1.7.2.4 2001/07/29 19:32:40 ume Exp $
1c79356b
A
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.2 (Berkeley) 1/4/94
65 */
1c79356b
A
66#include <sys/param.h>
67#include <sys/malloc.h>
68#include <sys/proc.h>
69#include <sys/mbuf.h>
70#include <sys/socket.h>
71#include <sys/protosw.h>
72#include <sys/socketvar.h>
73#include <sys/errno.h>
74#include <sys/systm.h>
75
76#include <net/if.h>
77#include <net/route.h>
78#include <net/if_types.h>
79
80#include <netinet/in.h>
81#include <netinet/in_var.h>
82#include <netinet/in_systm.h>
83#include <netinet/ip6.h>
84#include <netinet6/ip6_var.h>
85#include <netinet6/ip6_mroute.h>
86#include <netinet/icmp6.h>
1c79356b 87#include <netinet/in_pcb.h>
1c79356b
A
88#include <netinet6/in6_pcb.h>
89#include <netinet6/nd6.h>
90#include <netinet6/ip6protosw.h>
9bccf70c
A
91#if ENABLE_DEFAULT_SCOPE
92#include <netinet6/scope6_var.h>
93#endif
94#include <netinet6/raw_ip6.h>
1c79356b
A
95
96#if IPSEC
97#include <netinet6/ipsec.h>
9bccf70c
A
98#include <netinet6/ipsec6.h>
99extern int ipsec_bypass;
1c79356b
A
100#endif /*IPSEC*/
101
102
103#include "faith.h"
9bccf70c
A
104#if defined(NFAITH) && 0 < NFAITH
105#include <net/if_faith.h>
106#endif
1c79356b 107
9bccf70c
A
108#define satosin6(sa) ((struct sockaddr_in6 *)(sa))
109#define ifatoia6(ifa) ((struct in6_ifaddr *)(ifa))
1c79356b
A
110
111/*
112 * Raw interface to IP6 protocol.
113 */
114
9bccf70c
A
115extern struct inpcbhead ripcb;
116extern struct inpcbinfo ripcbinfo;
1c79356b
A
117extern u_long rip_sendspace;
118extern u_long rip_recvspace;
119
9bccf70c
A
120struct rip6stat rip6stat;
121
1c79356b
A
122/*
123 * Setup generic address and protocol structures
124 * for raw_input routine, then pass them along with
125 * mbuf chain.
126 */
127int
9bccf70c 128rip6_input(mp, offp)
1c79356b 129 struct mbuf **mp;
9bccf70c 130 int *offp;
1c79356b
A
131{
132 struct mbuf *m = *mp;
133 register struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
134 register struct inpcb *in6p;
135 struct inpcb *last = 0;
9bccf70c 136 struct mbuf *opts = NULL;
1c79356b 137 struct sockaddr_in6 rip6src;
9bccf70c
A
138 int proto = ip6->ip6_nxt;
139
140 rip6stat.rip6s_ipackets++;
1c79356b
A
141
142#if defined(NFAITH) && 0 < NFAITH
9bccf70c
A
143 if (faithprefix(&ip6->ip6_dst)) {
144 /* XXX send icmp6 host/port unreach? */
145 m_freem(m);
146 return IPPROTO_DONE;
1c79356b
A
147 }
148#endif
9bccf70c 149
1c79356b 150 init_sin6(&rip6src, m); /* general init */
1c79356b
A
151
152 LIST_FOREACH(in6p, &ripcb, inp_list) {
9bccf70c 153 if ((in6p->in6p_vflag & INP_IPV6) == 0)
1c79356b
A
154 continue;
155 if (in6p->in6p_ip6_nxt &&
156 in6p->in6p_ip6_nxt != proto)
157 continue;
158 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) &&
159 !IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &ip6->ip6_dst))
160 continue;
161 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr) &&
162 !IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, &ip6->ip6_src))
163 continue;
9bccf70c
A
164 if (in6p->in6p_cksum != -1) {
165 rip6stat.rip6s_isum++;
166 if (in6_cksum(m, ip6->ip6_nxt, *offp,
167 m->m_pkthdr.len - *offp)) {
168 rip6stat.rip6s_badsum++;
169 continue;
170 }
1c79356b
A
171 }
172 if (last) {
173 struct mbuf *n = m_copy(m, 0, (int)M_COPYALL);
9bccf70c
A
174
175#if IPSEC
176 /*
177 * Check AH/ESP integrity.
178 */
179 if (ipsec_bypass == 0 && n && ipsec6_in_reject_so(n, last->inp_socket)) {
180 m_freem(n);
181 ipsec6stat.in_polvio++;
182 /* do not inject data into pcb */
183 } else
184#endif /*IPSEC*/
1c79356b
A
185 if (n) {
186 if (last->in6p_flags & IN6P_CONTROLOPTS ||
187 last->in6p_socket->so_options & SO_TIMESTAMP)
9bccf70c 188 ip6_savecontrol(last, &opts, ip6, n);
1c79356b
A
189 /* strip intermediate headers */
190 m_adj(n, *offp);
191 if (sbappendaddr(&last->in6p_socket->so_rcv,
192 (struct sockaddr *)&rip6src,
9bccf70c 193 n, opts) == 0) {
1c79356b 194 m_freem(n);
9bccf70c
A
195 if (opts)
196 m_freem(opts);
197 rip6stat.rip6s_fullsock++;
1c79356b
A
198 } else
199 sorwakeup(last->in6p_socket);
9bccf70c 200 opts = NULL;
1c79356b
A
201 }
202 }
203 last = in6p;
204 }
9bccf70c
A
205#if IPSEC
206 /*
207 * Check AH/ESP integrity.
208 */
209 if (ipsec_bypass == 0 && last && ipsec6_in_reject_so(m, last->inp_socket)) {
210 m_freem(m);
211 ipsec6stat.in_polvio++;
212 ip6stat.ip6s_delivered--;
213 /* do not inject data into pcb */
214 } else
215#endif /*IPSEC*/
1c79356b
A
216 if (last) {
217 if (last->in6p_flags & IN6P_CONTROLOPTS ||
218 last->in6p_socket->so_options & SO_TIMESTAMP)
9bccf70c 219 ip6_savecontrol(last, &opts, ip6, m);
1c79356b
A
220 /* strip intermediate headers */
221 m_adj(m, *offp);
222 if (sbappendaddr(&last->in6p_socket->so_rcv,
9bccf70c 223 (struct sockaddr *)&rip6src, m, opts) == 0) {
1c79356b 224 m_freem(m);
9bccf70c
A
225 if (opts)
226 m_freem(opts);
227 rip6stat.rip6s_fullsock++;
1c79356b
A
228 } else
229 sorwakeup(last->in6p_socket);
230 } else {
9bccf70c
A
231 rip6stat.rip6s_nosock++;
232 if (m->m_flags & M_MCAST)
233 rip6stat.rip6s_nosockmcast++;
1c79356b
A
234 if (proto == IPPROTO_NONE)
235 m_freem(m);
236 else {
237 char *prvnxtp = ip6_get_prevhdr(m, *offp); /* XXX */
238 icmp6_error(m, ICMP6_PARAM_PROB,
239 ICMP6_PARAMPROB_NEXTHEADER,
240 prvnxtp - mtod(m, char *));
241 }
242 ip6stat.ip6s_delivered--;
243 }
244 return IPPROTO_DONE;
245}
246
247void
248rip6_ctlinput(cmd, sa, d)
249 int cmd;
250 struct sockaddr *sa;
251 void *d;
252{
1c79356b
A
253 struct ip6_hdr *ip6;
254 struct mbuf *m;
255 int off = 0;
9bccf70c
A
256 struct ip6ctlparam *ip6cp = NULL;
257 const struct sockaddr_in6 *sa6_src = NULL;
1c79356b
A
258 void (*notify) __P((struct inpcb *, int)) = in6_rtchange;
259
260 if (sa->sa_family != AF_INET6 ||
261 sa->sa_len != sizeof(struct sockaddr_in6))
262 return;
263
264 if ((unsigned)cmd >= PRC_NCMDS)
265 return;
266 if (PRC_IS_REDIRECT(cmd))
267 notify = in6_rtchange, d = NULL;
268 else if (cmd == PRC_HOSTDEAD)
269 d = NULL;
270 else if (inet6ctlerrmap[cmd] == 0)
271 return;
272
273 /* if the parameter is from icmp6, decode it. */
274 if (d != NULL) {
9bccf70c 275 ip6cp = (struct ip6ctlparam *)d;
1c79356b
A
276 m = ip6cp->ip6c_m;
277 ip6 = ip6cp->ip6c_ip6;
278 off = ip6cp->ip6c_off;
9bccf70c 279 sa6_src = ip6cp->ip6c_src;
1c79356b
A
280 } else {
281 m = NULL;
282 ip6 = NULL;
9bccf70c 283 sa6_src = &sa6_any;
1c79356b
A
284 }
285
9bccf70c
A
286 (void) in6_pcbnotify(&ripcb, sa, 0, (struct sockaddr *)sa6_src,
287 0, cmd, notify);
1c79356b
A
288}
289
290/*
291 * Generate IPv6 header and pass packet to ip6_output.
292 * Tack on options user may have setup with control call.
293 */
294int
295rip6_output(m, so, dstsock, control)
296 register struct mbuf *m;
297 struct socket *so;
298 struct sockaddr_in6 *dstsock;
299 struct mbuf *control;
300{
301 struct in6_addr *dst;
302 struct ip6_hdr *ip6;
303 struct inpcb *in6p;
304 u_int plen = m->m_pkthdr.len;
305 int error = 0;
306 struct ip6_pktopts opt, *optp = 0;
307 struct ifnet *oifp = NULL;
308 int type = 0, code = 0; /* for ICMPv6 output statistics only */
309 int priv = 0;
310
311 in6p = sotoin6pcb(so);
312
313 priv = 0;
9bccf70c 314 if (so->so_uid == 0)
1c79356b 315 priv = 1;
1c79356b
A
316 dst = &dstsock->sin6_addr;
317 if (control) {
318 if ((error = ip6_setpktoptions(control, &opt, priv, 0)) != 0)
319 goto bad;
320 optp = &opt;
321 } else
322 optp = in6p->in6p_outputopts;
323
324 /*
325 * For an ICMPv6 packet, we should know its type and code
326 * to update statistics.
327 */
328 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6) {
329 struct icmp6_hdr *icmp6;
330 if (m->m_len < sizeof(struct icmp6_hdr) &&
331 (m = m_pullup(m, sizeof(struct icmp6_hdr))) == NULL) {
332 error = ENOBUFS;
333 goto bad;
334 }
335 icmp6 = mtod(m, struct icmp6_hdr *);
336 type = icmp6->icmp6_type;
337 code = icmp6->icmp6_code;
338 }
339
340 M_PREPEND(m, sizeof(*ip6), M_WAIT);
341 ip6 = mtod(m, struct ip6_hdr *);
342
343 /*
344 * Next header might not be ICMP6 but use its pseudo header anyway.
345 */
346 ip6->ip6_dst = *dst;
347
348 /*
349 * If the scope of the destination is link-local, embed the interface
350 * index in the address.
351 *
9bccf70c 352 * XXX advanced-api value overrides sin6_scope_id
1c79356b
A
353 */
354 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst)) {
355 struct in6_pktinfo *pi;
356
357 /*
358 * XXX Boundary check is assumed to be already done in
359 * ip6_setpktoptions().
360 */
361 if (optp && (pi = optp->ip6po_pktinfo) && pi->ipi6_ifindex) {
362 ip6->ip6_dst.s6_addr16[1] = htons(pi->ipi6_ifindex);
363 oifp = ifindex2ifnet[pi->ipi6_ifindex];
9bccf70c 364 } else if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) &&
1c79356b
A
365 in6p->in6p_moptions &&
366 in6p->in6p_moptions->im6o_multicast_ifp) {
367 oifp = in6p->in6p_moptions->im6o_multicast_ifp;
368 ip6->ip6_dst.s6_addr16[1] = htons(oifp->if_index);
369 } else if (dstsock->sin6_scope_id) {
370 /* boundary check */
9bccf70c 371 if (dstsock->sin6_scope_id < 0
1c79356b
A
372 || if_index < dstsock->sin6_scope_id) {
373 error = ENXIO; /* XXX EINVAL? */
374 goto bad;
375 }
376 ip6->ip6_dst.s6_addr16[1]
377 = htons(dstsock->sin6_scope_id & 0xffff);/*XXX*/
378 }
379 }
380
381 /*
382 * Source address selection.
383 */
384 {
385 struct in6_addr *in6a;
386
387 if ((in6a = in6_selectsrc(dstsock, optp,
388 in6p->in6p_moptions,
389 &in6p->in6p_route,
390 &in6p->in6p_laddr,
391 &error)) == 0) {
392 if (error == 0)
393 error = EADDRNOTAVAIL;
394 goto bad;
395 }
396 ip6->ip6_src = *in6a;
397 if (in6p->in6p_route.ro_rt)
398 oifp = ifindex2ifnet[in6p->in6p_route.ro_rt->rt_ifp->if_index];
399 }
9bccf70c
A
400 ip6->ip6_flow = (ip6->ip6_flow & ~IPV6_FLOWINFO_MASK) |
401 (in6p->in6p_flowinfo & IPV6_FLOWINFO_MASK);
402 ip6->ip6_vfc = (ip6->ip6_vfc & ~IPV6_VERSION_MASK) |
403 (IPV6_VERSION & IPV6_VERSION_MASK);
404 /* ip6_plen will be filled in ip6_output, so not fill it here. */
1c79356b
A
405 ip6->ip6_nxt = in6p->in6p_ip6_nxt;
406 ip6->ip6_hlim = in6_selecthlim(in6p, oifp);
407
408 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6 ||
409 in6p->in6p_cksum != -1) {
410 struct mbuf *n;
411 int off;
412 u_int16_t *p;
413
1c79356b
A
414 /* compute checksum */
415 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6)
416 off = offsetof(struct icmp6_hdr, icmp6_cksum);
417 else
418 off = in6p->in6p_cksum;
419 if (plen < off + 1) {
420 error = EINVAL;
421 goto bad;
422 }
423 off += sizeof(struct ip6_hdr);
424
425 n = m;
426 while (n && n->m_len <= off) {
427 off -= n->m_len;
428 n = n->m_next;
429 }
430 if (!n)
431 goto bad;
432 p = (u_int16_t *)(mtod(n, caddr_t) + off);
433 *p = 0;
434 *p = in6_cksum(m, ip6->ip6_nxt, sizeof(*ip6), plen);
435 }
436
437#if IPSEC
9bccf70c
A
438 if (ipsec_bypass == 0 && ipsec_setsocket(m, so) != 0) {
439 error = ENOBUFS;
440 goto bad;
441 }
1c79356b
A
442#endif /*IPSEC*/
443
9bccf70c
A
444 error = ip6_output(m, optp, &in6p->in6p_route, 0,
445 in6p->in6p_moptions, &oifp);
1c79356b
A
446 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6) {
447 if (oifp)
448 icmp6_ifoutstat_inc(oifp, type, code);
449 icmp6stat.icp6s_outhist[type]++;
9bccf70c
A
450 } else
451 rip6stat.rip6s_opackets++;
1c79356b
A
452
453 goto freectl;
454
455 bad:
456 if (m)
457 m_freem(m);
458
459 freectl:
460 if (optp == &opt && optp->ip6po_rthdr && optp->ip6po_route.ro_rt)
9bccf70c 461 rtfree(optp->ip6po_route.ro_rt);
1c79356b
A
462 if (control) {
463 if (optp == &opt)
464 ip6_clearpktopts(optp, 0, -1);
465 m_freem(control);
466 }
467 return(error);
468}
469
470/*
471 * Raw IPv6 socket option processing.
472 */
473int
474rip6_ctloutput(so, sopt)
475 struct socket *so;
476 struct sockopt *sopt;
477{
478 int error;
479
480 if (sopt->sopt_level == IPPROTO_ICMPV6)
481 /*
482 * XXX: is it better to call icmp6_ctloutput() directly
483 * from protosw?
484 */
485 return(icmp6_ctloutput(so, sopt));
486 else if (sopt->sopt_level != IPPROTO_IPV6)
487 return (EINVAL);
488
489 error = 0;
490
491 switch (sopt->sopt_dir) {
492 case SOPT_GET:
493 switch (sopt->sopt_name) {
494 case MRT6_INIT:
495 case MRT6_DONE:
496 case MRT6_ADD_MIF:
497 case MRT6_DEL_MIF:
498 case MRT6_ADD_MFC:
499 case MRT6_DEL_MFC:
500 case MRT6_PIM:
501 error = ip6_mrouter_get(so, sopt);
502 break;
503 default:
504 error = ip6_ctloutput(so, sopt);
505 break;
506 }
507 break;
508
509 case SOPT_SET:
510 switch (sopt->sopt_name) {
511 case MRT6_INIT:
512 case MRT6_DONE:
513 case MRT6_ADD_MIF:
514 case MRT6_DEL_MIF:
515 case MRT6_ADD_MFC:
516 case MRT6_DEL_MFC:
517 case MRT6_PIM:
518 error = ip6_mrouter_set(so, sopt);
519 break;
520 default:
521 error = ip6_ctloutput(so, sopt);
522 break;
523 }
524 break;
525 }
526
527 return (error);
528}
529
530static int
531rip6_attach(struct socket *so, int proto, struct proc *p)
532{
533 struct inpcb *inp;
534 int error, s;
535
536 inp = sotoinpcb(so);
537 if (inp)
538 panic("rip6_attach");
539 if (p && (error = suser(p->p_ucred, &p->p_acflag)) != 0)
540 return error;
541
9bccf70c
A
542 error = soreserve(so, rip_sendspace, rip_recvspace);
543 if (error)
544 return error;
1c79356b
A
545 s = splnet();
546 error = in_pcballoc(so, &ripcbinfo, p);
547 splx(s);
548 if (error)
549 return error;
550 inp = (struct inpcb *)so->so_pcb;
551 inp->inp_vflag |= INP_IPV6;
552 inp->in6p_ip6_nxt = (long)proto;
553 inp->in6p_hops = -1; /* use kernel default */
554 inp->in6p_cksum = -1;
1c79356b
A
555 MALLOC(inp->in6p_icmp6filt, struct icmp6_filter *,
556 sizeof(struct icmp6_filter), M_PCB, M_NOWAIT);
557 ICMP6_FILTER_SETPASSALL(inp->in6p_icmp6filt);
558 return 0;
559}
560
561static int
562rip6_detach(struct socket *so)
563{
564 struct inpcb *inp;
565
566 inp = sotoinpcb(so);
567 if (inp == 0)
568 panic("rip6_detach");
9bccf70c 569 /* xxx: RSVP */
1c79356b
A
570 if (so == ip6_mrouter)
571 ip6_mrouter_done();
1c79356b
A
572 if (inp->in6p_icmp6filt) {
573 FREE(inp->in6p_icmp6filt, M_PCB);
574 inp->in6p_icmp6filt = NULL;
575 }
576 in6_pcbdetach(inp);
577 return 0;
578}
579
580static int
581rip6_abort(struct socket *so)
582{
583 soisdisconnected(so);
584 return rip6_detach(so);
585}
586
587static int
588rip6_disconnect(struct socket *so)
589{
590 struct inpcb *inp = sotoinpcb(so);
591
592 if ((so->so_state & SS_ISCONNECTED) == 0)
593 return ENOTCONN;
594 inp->in6p_faddr = in6addr_any;
595 return rip6_abort(so);
596}
597
598static int
599rip6_bind(struct socket *so, struct sockaddr *nam, struct proc *p)
600{
601 struct inpcb *inp = sotoinpcb(so);
602 struct sockaddr_in6 *addr = (struct sockaddr_in6 *)nam;
603 struct ifaddr *ia = NULL;
604
605 if (nam->sa_len != sizeof(*addr))
606 return EINVAL;
607
608 if (TAILQ_EMPTY(&ifnet) || addr->sin6_family != AF_INET6)
609 return EADDRNOTAVAIL;
9bccf70c
A
610#if ENABLE_DEFAULT_SCOPE
611 if (addr->sin6_scope_id == 0) { /* not change if specified */
612 addr->sin6_scope_id = scope6_addr2default(&addr->sin6_addr);
613 }
614#endif
1c79356b
A
615 if (!IN6_IS_ADDR_UNSPECIFIED(&addr->sin6_addr) &&
616 (ia = ifa_ifwithaddr((struct sockaddr *)addr)) == 0)
617 return EADDRNOTAVAIL;
618 if (ia &&
619 ((struct in6_ifaddr *)ia)->ia6_flags &
620 (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY|
621 IN6_IFF_DETACHED|IN6_IFF_DEPRECATED)) {
622 return(EADDRNOTAVAIL);
623 }
624 inp->in6p_laddr = addr->sin6_addr;
625 return 0;
626}
627
628static int
629rip6_connect(struct socket *so, struct sockaddr *nam, struct proc *p)
630{
631 struct inpcb *inp = sotoinpcb(so);
632 struct sockaddr_in6 *addr = (struct sockaddr_in6 *)nam;
633 struct in6_addr *in6a = NULL;
634 int error = 0;
9bccf70c
A
635#if ENABLE_DEFAULT_SCOPE
636 struct sockaddr_in6 tmp;
637#endif
1c79356b
A
638
639 if (nam->sa_len != sizeof(*addr))
640 return EINVAL;
641 if (TAILQ_EMPTY(&ifnet))
642 return EADDRNOTAVAIL;
643 if (addr->sin6_family != AF_INET6)
644 return EAFNOSUPPORT;
9bccf70c
A
645#if ENABLE_DEFAULT_SCOPE
646 if (addr->sin6_scope_id == 0) { /* not change if specified */
647 /* avoid overwrites */
648 tmp = *addr;
649 addr = &tmp;
650 addr->sin6_scope_id = scope6_addr2default(&addr->sin6_addr);
651 }
652#endif
1c79356b
A
653 /* Source address selection. XXX: need pcblookup? */
654 in6a = in6_selectsrc(addr, inp->in6p_outputopts,
655 inp->in6p_moptions, &inp->in6p_route,
656 &inp->in6p_laddr, &error);
657 if (in6a == NULL)
658 return (error ? error : EADDRNOTAVAIL);
659 inp->in6p_laddr = *in6a;
660 inp->in6p_faddr = addr->sin6_addr;
661 soisconnected(so);
662 return 0;
663}
664
665static int
666rip6_shutdown(struct socket *so)
667{
668 socantsendmore(so);
669 return 0;
670}
671
672static int
673rip6_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
674 struct mbuf *control, struct proc *p)
675{
676 struct inpcb *inp = sotoinpcb(so);
677 struct sockaddr_in6 tmp;
678 struct sockaddr_in6 *dst;
679
9bccf70c 680 /* always copy sockaddr to avoid overwrites */
1c79356b
A
681 if (so->so_state & SS_ISCONNECTED) {
682 if (nam) {
683 m_freem(m);
684 return EISCONN;
685 }
686 /* XXX */
687 bzero(&tmp, sizeof(tmp));
688 tmp.sin6_family = AF_INET6;
689 tmp.sin6_len = sizeof(struct sockaddr_in6);
690 bcopy(&inp->in6p_faddr, &tmp.sin6_addr,
691 sizeof(struct in6_addr));
692 dst = &tmp;
693 } else {
694 if (nam == NULL) {
695 m_freem(m);
696 return ENOTCONN;
697 }
9bccf70c
A
698 tmp = *(struct sockaddr_in6 *)nam;
699 dst = &tmp;
1c79356b 700 }
9bccf70c
A
701#if ENABLE_DEFAULT_SCOPE
702 if (dst->sin6_scope_id == 0) { /* not change if specified */
703 dst->sin6_scope_id = scope6_addr2default(&dst->sin6_addr);
704 }
705#endif
1c79356b
A
706 return rip6_output(m, so, dst, control);
707}
708
709struct pr_usrreqs rip6_usrreqs = {
710 rip6_abort, pru_accept_notsupp, rip6_attach, rip6_bind, rip6_connect,
711 pru_connect2_notsupp, in6_control, rip6_detach, rip6_disconnect,
712 pru_listen_notsupp, in6_setpeeraddr, pru_rcvd_notsupp,
9bccf70c 713 pru_rcvoob_notsupp, rip6_send, pru_sense_null, rip6_shutdown,
1c79356b
A
714 in6_setsockaddr, sosend, soreceive, sopoll
715};