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 * $FreeBSD: releng/5.3/sys/netinet6/raw_ip6.c 136588 2004-10-16 08:43:07Z cvs2svn $
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 * 4. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
47 *
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 * SUCH DAMAGE.
59 *
60 * @(#)raw_ip.c 8.2 (Berkeley) 1/4/94
61 */
62
63 #include "opt_ipsec.h"
64 #include "opt_inet6.h"
65
66 #include <sys/param.h>
67 #include <sys/errno.h>
68 #include <sys/lock.h>
69 #include <sys/malloc.h>
70 #include <sys/mbuf.h>
71 #include <sys/proc.h>
72 #include <sys/protosw.h>
73 #include <sys/signalvar.h>
74 #include <sys/socket.h>
75 #include <sys/socketvar.h>
76 #include <sys/sx.h>
77 #include <sys/systm.h>
78
79 #include <net/if.h>
80 #include <net/if_types.h>
81 #include <net/route.h>
82
83 #include <netinet/in.h>
84 #include <netinet/in_var.h>
85 #include <netinet/in_systm.h>
86 #include <netinet/icmp6.h>
87 #include <netinet/in_pcb.h>
88 #include <netinet/ip6.h>
89 #include <netinet6/ip6protosw.h>
90 #include <netinet6/ip6_mroute.h>
91 #include <netinet6/in6_pcb.h>
92 #include <netinet6/ip6_var.h>
93 #include <netinet6/nd6.h>
94 #include <netinet6/raw_ip6.h>
95 #ifdef ENABLE_DEFAULT_SCOPE
96 #include <netinet6/scope6_var.h>
97 #endif
98
99 #ifdef IPSEC
100 #include <netinet6/ipsec.h>
101 #include <netinet6/ipsec6.h>
102 #endif /*IPSEC*/
103
104 #ifdef FAST_IPSEC
105 #include <netipsec/ipsec.h>
106 #include <netipsec/ipsec6.h>
107 #endif /* FAST_IPSEC */
108
109 #include <machine/stdarg.h>
110
111 #define satosin6(sa) ((struct sockaddr_in6 *)(sa))
112 #define ifatoia6(ifa) ((struct in6_ifaddr *)(ifa))
113
114 /*
115 * Raw interface to IP6 protocol.
116 */
117
118 extern struct inpcbhead ripcb;
119 extern struct inpcbinfo ripcbinfo;
120 extern u_long rip_sendspace;
121 extern u_long rip_recvspace;
122
123 struct rip6stat rip6stat;
124
125 /*
126 * Setup generic address and protocol structures
127 * for raw_input routine, then pass them along with
128 * mbuf chain.
129 */
130 int
131 rip6_input(mp, offp, proto)
132 struct mbuf **mp;
133 int *offp, proto;
134 {
135 struct mbuf *m = *mp;
136 register struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
137 register struct inpcb *in6p;
138 struct inpcb *last = 0;
139 struct mbuf *opts = NULL;
140 struct sockaddr_in6 fromsa;
141
142 rip6stat.rip6s_ipackets++;
143
144 if (faithprefix_p != NULL && (*faithprefix_p)(&ip6->ip6_dst)) {
145 /* XXX send icmp6 host/port unreach? */
146 m_freem(m);
147 return IPPROTO_DONE;
148 }
149
150 init_sin6(&fromsa, m); /* general init */
151
152 INP_INFO_RLOCK(&ripcbinfo);
153 LIST_FOREACH(in6p, &ripcb, inp_list) {
154 INP_LOCK(in6p);
155 if ((in6p->in6p_vflag & INP_IPV6) == 0) {
156 docontinue:
157 INP_UNLOCK(in6p);
158 continue;
159 }
160 if (in6p->in6p_ip6_nxt &&
161 in6p->in6p_ip6_nxt != proto)
162 goto docontinue;
163 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) &&
164 !IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &ip6->ip6_dst))
165 goto docontinue;
166 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr) &&
167 !IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, &ip6->ip6_src))
168 goto docontinue;
169 if (in6p->in6p_cksum != -1) {
170 rip6stat.rip6s_isum++;
171 if (in6_cksum(m, ip6->ip6_nxt, *offp,
172 m->m_pkthdr.len - *offp)) {
173 rip6stat.rip6s_badsum++;
174 goto docontinue;
175 }
176 }
177 if (last) {
178 struct mbuf *n = m_copy(m, 0, (int)M_COPYALL);
179
180 #if defined(IPSEC) || defined(FAST_IPSEC)
181 /*
182 * Check AH/ESP integrity.
183 */
184 if (n && ipsec6_in_reject(n, last)) {
185 m_freem(n);
186 #ifdef IPSEC
187 ipsec6stat.in_polvio++;
188 #endif /*IPSEC*/
189 /* do not inject data into pcb */
190 } else
191 #endif /*IPSEC || FAST_IPSEC*/
192 if (n) {
193 if (last->in6p_flags & IN6P_CONTROLOPTS ||
194 last->in6p_socket->so_options & SO_TIMESTAMP)
195 ip6_savecontrol(last, n, &opts);
196 /* strip intermediate headers */
197 m_adj(n, *offp);
198 if (sbappendaddr(&last->in6p_socket->so_rcv,
199 (struct sockaddr *)&fromsa,
200 n, opts) == 0) {
201 m_freem(n);
202 if (opts)
203 m_freem(opts);
204 rip6stat.rip6s_fullsock++;
205 } else
206 sorwakeup(last->in6p_socket);
207 opts = NULL;
208 }
209 INP_UNLOCK(last);
210 }
211 last = in6p;
212 }
213 #if defined(IPSEC) || defined(FAST_IPSEC)
214 /*
215 * Check AH/ESP integrity.
216 */
217 if (last && ipsec6_in_reject(m, last)) {
218 m_freem(m);
219 #ifdef IPSEC
220 ipsec6stat.in_polvio++;
221 #endif /*IPSEC*/
222 ip6stat.ip6s_delivered--;
223 /* do not inject data into pcb */
224 } else
225 #endif /*IPSEC || FAST_IPSEC*/
226 if (last) {
227 if (last->in6p_flags & IN6P_CONTROLOPTS ||
228 last->in6p_socket->so_options & SO_TIMESTAMP)
229 ip6_savecontrol(last, m, &opts);
230 /* strip intermediate headers */
231 m_adj(m, *offp);
232 if (sbappendaddr(&last->in6p_socket->so_rcv,
233 (struct sockaddr *)&fromsa, m, opts) == 0) {
234 m_freem(m);
235 if (opts)
236 m_freem(opts);
237 rip6stat.rip6s_fullsock++;
238 } else
239 sorwakeup(last->in6p_socket);
240 INP_UNLOCK(last);
241 } else {
242 rip6stat.rip6s_nosock++;
243 if (m->m_flags & M_MCAST)
244 rip6stat.rip6s_nosockmcast++;
245 if (proto == IPPROTO_NONE)
246 m_freem(m);
247 else {
248 char *prvnxtp = ip6_get_prevhdr(m, *offp); /* XXX */
249 icmp6_error(m, ICMP6_PARAM_PROB,
250 ICMP6_PARAMPROB_NEXTHEADER,
251 prvnxtp - mtod(m, char *));
252 }
253 ip6stat.ip6s_delivered--;
254 }
255 INP_INFO_RUNLOCK(&ripcbinfo);
256 return IPPROTO_DONE;
257 }
258
259 void
260 rip6_ctlinput(cmd, sa, d)
261 int cmd;
262 struct sockaddr *sa;
263 void *d;
264 {
265 struct ip6_hdr *ip6;
266 struct mbuf *m;
267 int off = 0;
268 struct ip6ctlparam *ip6cp = NULL;
269 const struct sockaddr_in6 *sa6_src = NULL;
270 void *cmdarg;
271 struct inpcb *(*notify) __P((struct inpcb *, int)) = in6_rtchange;
272
273 if (sa->sa_family != AF_INET6 ||
274 sa->sa_len != sizeof(struct sockaddr_in6))
275 return;
276
277 if ((unsigned)cmd >= PRC_NCMDS)
278 return;
279 if (PRC_IS_REDIRECT(cmd))
280 notify = in6_rtchange, d = NULL;
281 else if (cmd == PRC_HOSTDEAD)
282 d = NULL;
283 else if (inet6ctlerrmap[cmd] == 0)
284 return;
285
286 /* if the parameter is from icmp6, decode it. */
287 if (d != NULL) {
288 ip6cp = (struct ip6ctlparam *)d;
289 m = ip6cp->ip6c_m;
290 ip6 = ip6cp->ip6c_ip6;
291 off = ip6cp->ip6c_off;
292 cmdarg = ip6cp->ip6c_cmdarg;
293 sa6_src = ip6cp->ip6c_src;
294 } else {
295 m = NULL;
296 ip6 = NULL;
297 cmdarg = NULL;
298 sa6_src = &sa6_any;
299 }
300
301 (void) in6_pcbnotify(&ripcbinfo, sa, 0,
302 (const struct sockaddr *)sa6_src,
303 0, cmd, cmdarg, notify);
304 }
305
306 /*
307 * Generate IPv6 header and pass packet to ip6_output.
308 * Tack on options user may have setup with control call.
309 */
310 int
311 #if __STDC__
312 rip6_output(struct mbuf *m, ...)
313 #else
314 rip6_output(m, va_alist)
315 struct mbuf *m;
316 va_dcl
317 #endif
318 {
319 struct mbuf *control;
320 struct socket *so;
321 struct sockaddr_in6 *dstsock;
322 struct in6_addr *dst;
323 struct ip6_hdr *ip6;
324 struct inpcb *in6p;
325 u_int plen = m->m_pkthdr.len;
326 int error = 0;
327 struct ip6_pktopts opt, *stickyopt = NULL;
328 struct ifnet *oifp = NULL;
329 int type = 0, code = 0; /* for ICMPv6 output statistics only */
330 int priv = 0;
331 struct in6_addr *in6a;
332 va_list ap;
333
334 va_start(ap, m);
335 so = va_arg(ap, struct socket *);
336 dstsock = va_arg(ap, struct sockaddr_in6 *);
337 control = va_arg(ap, struct mbuf *);
338 va_end(ap);
339
340 in6p = sotoin6pcb(so);
341 /*
342 * XXXRW: In IPv6, we don't start referencing the contents of the
343 * inpcb until after all M_TRYWAIT allocations have finished. We may
344 * want to reorder this function to provide similar guarantees here,
345 * so as to avoid holding a mutex over M_TRYWAIT.
346 */
347 INP_LOCK(in6p);
348 stickyopt = in6p->in6p_outputopts;
349
350 priv = 0;
351 if (so->so_cred->cr_uid == 0)
352 priv = 1;
353 dst = &dstsock->sin6_addr;
354 if (control) {
355 if ((error = ip6_setpktoptions(control, &opt,
356 stickyopt, priv, 0,
357 so->so_proto->pr_protocol))
358 != 0) {
359 goto bad;
360 }
361 in6p->in6p_outputopts = &opt;
362 }
363
364 /*
365 * For an ICMPv6 packet, we should know its type and code
366 * to update statistics.
367 */
368 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6) {
369 struct icmp6_hdr *icmp6;
370 if (m->m_len < sizeof(struct icmp6_hdr) &&
371 (m = m_pullup(m, sizeof(struct icmp6_hdr))) == NULL) {
372 error = ENOBUFS;
373 goto bad;
374 }
375 icmp6 = mtod(m, struct icmp6_hdr *);
376 type = icmp6->icmp6_type;
377 code = icmp6->icmp6_code;
378 }
379
380 M_PREPEND(m, sizeof(*ip6), M_DONTWAIT);
381 if (m == NULL) {
382 error = ENOBUFS;
383 goto bad;
384 }
385 ip6 = mtod(m, struct ip6_hdr *);
386
387 /*
388 * Next header might not be ICMP6 but use its pseudo header anyway.
389 */
390 ip6->ip6_dst = *dst;
391
392 /*
393 * If the scope of the destination is link-local, embed the interface
394 * index in the address.
395 *
396 * XXX advanced-api value overrides sin6_scope_id
397 */
398 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst)) {
399 struct in6_pktinfo *pi;
400
401 /*
402 * XXX Boundary check is assumed to be already done in
403 * ip6_setpktoptions().
404 */
405 if (in6p->in6p_outputopts &&
406 (pi = in6p->in6p_outputopts->ip6po_pktinfo) &&
407 pi->ipi6_ifindex) {
408 ip6->ip6_dst.s6_addr16[1] = htons(pi->ipi6_ifindex);
409 oifp = ifnet_byindex(pi->ipi6_ifindex);
410 } else if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) &&
411 in6p->in6p_moptions &&
412 in6p->in6p_moptions->im6o_multicast_ifp) {
413 oifp = in6p->in6p_moptions->im6o_multicast_ifp;
414 ip6->ip6_dst.s6_addr16[1] = htons(oifp->if_index);
415 } else if (dstsock->sin6_scope_id) {
416 /* boundary check */
417 if (dstsock->sin6_scope_id < 0 ||
418 if_index < dstsock->sin6_scope_id) {
419 error = ENXIO; /* XXX EINVAL? */
420 goto bad;
421 }
422 ip6->ip6_dst.s6_addr16[1] =
423 htons(dstsock->sin6_scope_id & 0xffff); /* XXX */
424 }
425 }
426
427 /*
428 * Source address selection.
429 */
430 if ((in6a = in6_selectsrc(dstsock, in6p->in6p_outputopts,
431 in6p->in6p_moptions, NULL, &in6p->in6p_laddr, &error)) == 0) {
432 if (error == 0)
433 error = EADDRNOTAVAIL;
434 goto bad;
435 }
436 ip6->ip6_src = *in6a;
437 ip6->ip6_flow = (ip6->ip6_flow & ~IPV6_FLOWINFO_MASK) |
438 (in6p->in6p_flowinfo & IPV6_FLOWINFO_MASK);
439 ip6->ip6_vfc = (ip6->ip6_vfc & ~IPV6_VERSION_MASK) |
440 (IPV6_VERSION & IPV6_VERSION_MASK);
441 /* ip6_plen will be filled in ip6_output, so not fill it here. */
442 ip6->ip6_nxt = in6p->in6p_ip6_nxt;
443 ip6->ip6_hlim = in6_selecthlim(in6p, oifp);
444
445 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6 ||
446 in6p->in6p_cksum != -1) {
447 struct mbuf *n;
448 int off;
449 u_int16_t *p;
450
451 /* compute checksum */
452 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6)
453 off = offsetof(struct icmp6_hdr, icmp6_cksum);
454 else
455 off = in6p->in6p_cksum;
456 if (plen < off + 1) {
457 error = EINVAL;
458 goto bad;
459 }
460 off += sizeof(struct ip6_hdr);
461
462 n = m;
463 while (n && n->m_len <= off) {
464 off -= n->m_len;
465 n = n->m_next;
466 }
467 if (!n)
468 goto bad;
469 p = (u_int16_t *)(mtod(n, caddr_t) + off);
470 *p = 0;
471 *p = in6_cksum(m, ip6->ip6_nxt, sizeof(*ip6), plen);
472 }
473
474 error = ip6_output(m, in6p->in6p_outputopts, NULL, 0,
475 in6p->in6p_moptions, &oifp, in6p);
476 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6) {
477 if (oifp)
478 icmp6_ifoutstat_inc(oifp, type, code);
479 icmp6stat.icp6s_outhist[type]++;
480 } else
481 rip6stat.rip6s_opackets++;
482
483 goto freectl;
484
485 bad:
486 if (m)
487 m_freem(m);
488
489 freectl:
490 if (control) {
491 ip6_clearpktopts(in6p->in6p_outputopts, -1);
492 in6p->in6p_outputopts = stickyopt;
493 m_freem(control);
494 }
495 INP_UNLOCK(in6p);
496 return (error);
497 }
498
499 /*
500 * Raw IPv6 socket option processing.
501 */
502 int
503 rip6_ctloutput(so, sopt)
504 struct socket *so;
505 struct sockopt *sopt;
506 {
507 int error;
508
509 if (sopt->sopt_level == IPPROTO_ICMPV6)
510 /*
511 * XXX: is it better to call icmp6_ctloutput() directly
512 * from protosw?
513 */
514 return (icmp6_ctloutput(so, sopt));
515 else if (sopt->sopt_level != IPPROTO_IPV6)
516 return (EINVAL);
517
518 error = 0;
519
520 switch (sopt->sopt_dir) {
521 case SOPT_GET:
522 switch (sopt->sopt_name) {
523 case MRT6_INIT:
524 case MRT6_DONE:
525 case MRT6_ADD_MIF:
526 case MRT6_DEL_MIF:
527 case MRT6_ADD_MFC:
528 case MRT6_DEL_MFC:
529 case MRT6_PIM:
530 error = ip6_mrouter_get(so, sopt);
531 break;
532 case IPV6_CHECKSUM:
533 error = ip6_raw_ctloutput(so, sopt);
534 break;
535 default:
536 error = ip6_ctloutput(so, sopt);
537 break;
538 }
539 break;
540
541 case SOPT_SET:
542 switch (sopt->sopt_name) {
543 case MRT6_INIT:
544 case MRT6_DONE:
545 case MRT6_ADD_MIF:
546 case MRT6_DEL_MIF:
547 case MRT6_ADD_MFC:
548 case MRT6_DEL_MFC:
549 case MRT6_PIM:
550 error = ip6_mrouter_set(so, sopt);
551 break;
552 case IPV6_CHECKSUM:
553 error = ip6_raw_ctloutput(so, sopt);
554 break;
555 default:
556 error = ip6_ctloutput(so, sopt);
557 break;
558 }
559 break;
560 }
561
562 return (error);
563 }
564
565 static int
566 rip6_attach(struct socket *so, int proto, struct thread *td)
567 {
568 struct inpcb *inp;
569 int error, s;
570
571 INP_INFO_WLOCK(&ripcbinfo);
572 inp = sotoinpcb(so);
573 if (inp) {
574 INP_INFO_WUNLOCK(&ripcbinfo);
575 panic("rip6_attach");
576 }
577 if (td && (error = suser(td)) != 0) {
578 INP_INFO_WUNLOCK(&ripcbinfo);
579 return error;
580 }
581 error = soreserve(so, rip_sendspace, rip_recvspace);
582 if (error) {
583 INP_INFO_WUNLOCK(&ripcbinfo);
584 return error;
585 }
586 s = splnet();
587 error = in_pcballoc(so, &ripcbinfo, "raw6inp");
588 splx(s);
589 if (error) {
590 INP_INFO_WUNLOCK(&ripcbinfo);
591 return error;
592 }
593 inp = (struct inpcb *)so->so_pcb;
594 INP_LOCK(inp);
595 INP_INFO_WUNLOCK(&ripcbinfo);
596 inp->inp_vflag |= INP_IPV6;
597 inp->in6p_ip6_nxt = (long)proto;
598 inp->in6p_hops = -1; /* use kernel default */
599 inp->in6p_cksum = -1;
600 MALLOC(inp->in6p_icmp6filt, struct icmp6_filter *,
601 sizeof(struct icmp6_filter), M_PCB, M_NOWAIT);
602 ICMP6_FILTER_SETPASSALL(inp->in6p_icmp6filt);
603 INP_UNLOCK(inp);
604 return 0;
605 }
606
607 static int
608 rip6_detach(struct socket *so)
609 {
610 struct inpcb *inp;
611
612 INP_INFO_WLOCK(&ripcbinfo);
613 inp = sotoinpcb(so);
614 if (inp == 0) {
615 INP_INFO_WUNLOCK(&ripcbinfo);
616 panic("rip6_detach");
617 }
618 /* xxx: RSVP */
619 if (so == ip6_mrouter)
620 ip6_mrouter_done();
621 if (inp->in6p_icmp6filt) {
622 FREE(inp->in6p_icmp6filt, M_PCB);
623 inp->in6p_icmp6filt = NULL;
624 }
625 INP_LOCK(inp);
626 in6_pcbdetach(inp);
627 INP_INFO_WUNLOCK(&ripcbinfo);
628 return 0;
629 }
630
631 static int
632 rip6_abort(struct socket *so)
633 {
634 soisdisconnected(so);
635 return rip6_detach(so);
636 }
637
638 static int
639 rip6_disconnect(struct socket *so)
640 {
641 struct inpcb *inp = sotoinpcb(so);
642
643 if ((so->so_state & SS_ISCONNECTED) == 0)
644 return ENOTCONN;
645 inp->in6p_faddr = in6addr_any;
646 return rip6_abort(so);
647 }
648
649 static int
650 rip6_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
651 {
652 struct inpcb *inp = sotoinpcb(so);
653 struct sockaddr_in6 *addr = (struct sockaddr_in6 *)nam;
654 struct ifaddr *ia = NULL;
655
656 if (nam->sa_len != sizeof(*addr))
657 return EINVAL;
658 if (TAILQ_EMPTY(&ifnet) || addr->sin6_family != AF_INET6)
659 return EADDRNOTAVAIL;
660 #ifdef ENABLE_DEFAULT_SCOPE
661 if (addr->sin6_scope_id == 0) { /* not change if specified */
662 addr->sin6_scope_id = scope6_addr2default(&addr->sin6_addr);
663 }
664 #endif
665 if (!IN6_IS_ADDR_UNSPECIFIED(&addr->sin6_addr) &&
666 (ia = ifa_ifwithaddr((struct sockaddr *)addr)) == 0)
667 return EADDRNOTAVAIL;
668 if (ia &&
669 ((struct in6_ifaddr *)ia)->ia6_flags &
670 (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY|
671 IN6_IFF_DETACHED|IN6_IFF_DEPRECATED)) {
672 return (EADDRNOTAVAIL);
673 }
674 INP_INFO_WLOCK(&ripcbinfo);
675 INP_LOCK(inp);
676 inp->in6p_laddr = addr->sin6_addr;
677 INP_UNLOCK(inp);
678 INP_INFO_WUNLOCK(&ripcbinfo);
679 return 0;
680 }
681
682 static int
683 rip6_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
684 {
685 struct inpcb *inp = sotoinpcb(so);
686 struct sockaddr_in6 *addr = (struct sockaddr_in6 *)nam;
687 struct in6_addr *in6a = NULL;
688 int error = 0;
689 #ifdef ENABLE_DEFAULT_SCOPE
690 struct sockaddr_in6 tmp;
691 #endif
692
693 if (nam->sa_len != sizeof(*addr))
694 return EINVAL;
695 if (TAILQ_EMPTY(&ifnet))
696 return EADDRNOTAVAIL;
697 if (addr->sin6_family != AF_INET6)
698 return EAFNOSUPPORT;
699 #ifdef ENABLE_DEFAULT_SCOPE
700 if (addr->sin6_scope_id == 0) { /* not change if specified */
701 /* avoid overwrites */
702 tmp = *addr;
703 addr = &tmp;
704 addr->sin6_scope_id = scope6_addr2default(&addr->sin6_addr);
705 }
706 #endif
707 INP_INFO_WLOCK(&ripcbinfo);
708 INP_LOCK(inp);
709 /* Source address selection. XXX: need pcblookup? */
710 in6a = in6_selectsrc(addr, inp->in6p_outputopts,
711 inp->in6p_moptions, NULL,
712 &inp->in6p_laddr, &error);
713 if (in6a == NULL) {
714 INP_UNLOCK(inp);
715 INP_INFO_WUNLOCK(&ripcbinfo);
716 return (error ? error : EADDRNOTAVAIL);
717 }
718 inp->in6p_laddr = *in6a;
719 inp->in6p_faddr = addr->sin6_addr;
720 soisconnected(so);
721 INP_UNLOCK(inp);
722 INP_INFO_WUNLOCK(&ripcbinfo);
723 return 0;
724 }
725
726 static int
727 rip6_shutdown(struct socket *so)
728 {
729 struct inpcb *inp;
730
731 INP_INFO_RLOCK(&ripcbinfo);
732 inp = sotoinpcb(so);
733 INP_LOCK(inp);
734 INP_INFO_RUNLOCK(&ripcbinfo);
735 socantsendmore(so);
736 INP_UNLOCK(inp);
737 return 0;
738 }
739
740 static int
741 rip6_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
742 struct mbuf *control, struct thread *td)
743 {
744 struct inpcb *inp = sotoinpcb(so);
745 struct sockaddr_in6 tmp;
746 struct sockaddr_in6 *dst;
747 int ret;
748
749 INP_INFO_WLOCK(&ripcbinfo);
750 /* always copy sockaddr to avoid overwrites */
751 /* Unlocked read. */
752 if (so->so_state & SS_ISCONNECTED) {
753 if (nam) {
754 INP_INFO_WUNLOCK(&ripcbinfo);
755 m_freem(m);
756 return EISCONN;
757 }
758 /* XXX */
759 bzero(&tmp, sizeof(tmp));
760 tmp.sin6_family = AF_INET6;
761 tmp.sin6_len = sizeof(struct sockaddr_in6);
762 bcopy(&inp->in6p_faddr, &tmp.sin6_addr,
763 sizeof(struct in6_addr));
764 dst = &tmp;
765 } else {
766 if (nam == NULL) {
767 INP_INFO_WUNLOCK(&ripcbinfo);
768 m_freem(m);
769 return ENOTCONN;
770 }
771 tmp = *(struct sockaddr_in6 *)nam;
772 dst = &tmp;
773 }
774 #ifdef ENABLE_DEFAULT_SCOPE
775 if (dst->sin6_scope_id == 0) { /* not change if specified */
776 dst->sin6_scope_id = scope6_addr2default(&dst->sin6_addr);
777 }
778 #endif
779 ret = rip6_output(m, so, dst, control);
780 INP_INFO_WUNLOCK(&ripcbinfo);
781 return (ret);
782 }
783
784 struct pr_usrreqs rip6_usrreqs = {
785 rip6_abort, pru_accept_notsupp, rip6_attach, rip6_bind, rip6_connect,
786 pru_connect2_notsupp, in6_control, rip6_detach, rip6_disconnect,
787 pru_listen_notsupp, in6_setpeeraddr, pru_rcvd_notsupp,
788 pru_rcvoob_notsupp, rip6_send, pru_sense_null, rip6_shutdown,
789 in6_setsockaddr, sosend, soreceive, sopoll, pru_sosetlabel_null
790 };
Cache object: 1f606c0e16bdac82e819b81e94f6ae1c
|