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/6.0/sys/netinet6/raw_ip6.c 151533 2005-10-21 11:10:46Z suz $
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 INP_UNLOCK(last);
225 } else
226 #endif /*IPSEC || FAST_IPSEC*/
227 if (last) {
228 if (last->in6p_flags & IN6P_CONTROLOPTS ||
229 last->in6p_socket->so_options & SO_TIMESTAMP)
230 ip6_savecontrol(last, m, &opts);
231 /* strip intermediate headers */
232 m_adj(m, *offp);
233 if (sbappendaddr(&last->in6p_socket->so_rcv,
234 (struct sockaddr *)&fromsa, m, opts) == 0) {
235 m_freem(m);
236 if (opts)
237 m_freem(opts);
238 rip6stat.rip6s_fullsock++;
239 } else
240 sorwakeup(last->in6p_socket);
241 INP_UNLOCK(last);
242 } else {
243 rip6stat.rip6s_nosock++;
244 if (m->m_flags & M_MCAST)
245 rip6stat.rip6s_nosockmcast++;
246 if (proto == IPPROTO_NONE)
247 m_freem(m);
248 else {
249 char *prvnxtp = ip6_get_prevhdr(m, *offp); /* XXX */
250 icmp6_error(m, ICMP6_PARAM_PROB,
251 ICMP6_PARAMPROB_NEXTHEADER,
252 prvnxtp - mtod(m, char *));
253 }
254 ip6stat.ip6s_delivered--;
255 }
256 INP_INFO_RUNLOCK(&ripcbinfo);
257 return IPPROTO_DONE;
258 }
259
260 void
261 rip6_ctlinput(cmd, sa, d)
262 int cmd;
263 struct sockaddr *sa;
264 void *d;
265 {
266 struct ip6_hdr *ip6;
267 struct mbuf *m;
268 int off = 0;
269 struct ip6ctlparam *ip6cp = NULL;
270 const struct sockaddr_in6 *sa6_src = NULL;
271 void *cmdarg;
272 struct inpcb *(*notify) __P((struct inpcb *, int)) = in6_rtchange;
273
274 if (sa->sa_family != AF_INET6 ||
275 sa->sa_len != sizeof(struct sockaddr_in6))
276 return;
277
278 if ((unsigned)cmd >= PRC_NCMDS)
279 return;
280 if (PRC_IS_REDIRECT(cmd))
281 notify = in6_rtchange, d = NULL;
282 else if (cmd == PRC_HOSTDEAD)
283 d = NULL;
284 else if (inet6ctlerrmap[cmd] == 0)
285 return;
286
287 /* if the parameter is from icmp6, decode it. */
288 if (d != NULL) {
289 ip6cp = (struct ip6ctlparam *)d;
290 m = ip6cp->ip6c_m;
291 ip6 = ip6cp->ip6c_ip6;
292 off = ip6cp->ip6c_off;
293 cmdarg = ip6cp->ip6c_cmdarg;
294 sa6_src = ip6cp->ip6c_src;
295 } else {
296 m = NULL;
297 ip6 = NULL;
298 cmdarg = NULL;
299 sa6_src = &sa6_any;
300 }
301
302 (void) in6_pcbnotify(&ripcbinfo, sa, 0,
303 (const struct sockaddr *)sa6_src,
304 0, cmd, cmdarg, notify);
305 }
306
307 /*
308 * Generate IPv6 header and pass packet to ip6_output.
309 * Tack on options user may have setup with control call.
310 */
311 int
312 #if __STDC__
313 rip6_output(struct mbuf *m, ...)
314 #else
315 rip6_output(m, va_alist)
316 struct mbuf *m;
317 va_dcl
318 #endif
319 {
320 struct mbuf *control;
321 struct socket *so;
322 struct sockaddr_in6 *dstsock;
323 struct in6_addr *dst;
324 struct ip6_hdr *ip6;
325 struct inpcb *in6p;
326 u_int plen = m->m_pkthdr.len;
327 int error = 0;
328 struct ip6_pktopts opt, *stickyopt = NULL;
329 struct ifnet *oifp = NULL;
330 int type = 0, code = 0; /* for ICMPv6 output statistics only */
331 int priv = 0;
332 struct in6_addr *in6a;
333 va_list ap;
334
335 va_start(ap, m);
336 so = va_arg(ap, struct socket *);
337 dstsock = va_arg(ap, struct sockaddr_in6 *);
338 control = va_arg(ap, struct mbuf *);
339 va_end(ap);
340
341 in6p = sotoin6pcb(so);
342 INP_LOCK(in6p);
343 stickyopt = in6p->in6p_outputopts;
344
345 priv = 0;
346 if (so->so_cred->cr_uid == 0)
347 priv = 1;
348 dst = &dstsock->sin6_addr;
349 if (control) {
350 if ((error = ip6_setpktopts(control, &opt,
351 stickyopt, priv, 0, so->so_proto->pr_protocol))
352 != 0) {
353 goto bad;
354 }
355 in6p->in6p_outputopts = &opt;
356 }
357
358 /*
359 * For an ICMPv6 packet, we should know its type and code
360 * to update statistics.
361 */
362 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6) {
363 struct icmp6_hdr *icmp6;
364 if (m->m_len < sizeof(struct icmp6_hdr) &&
365 (m = m_pullup(m, sizeof(struct icmp6_hdr))) == NULL) {
366 error = ENOBUFS;
367 goto bad;
368 }
369 icmp6 = mtod(m, struct icmp6_hdr *);
370 type = icmp6->icmp6_type;
371 code = icmp6->icmp6_code;
372 }
373
374 M_PREPEND(m, sizeof(*ip6), M_DONTWAIT);
375 if (m == NULL) {
376 error = ENOBUFS;
377 goto bad;
378 }
379 ip6 = mtod(m, struct ip6_hdr *);
380
381 /*
382 * Next header might not be ICMP6 but use its pseudo header anyway.
383 */
384 ip6->ip6_dst = *dst;
385
386 /*
387 * If the scope of the destination is link-local, embed the interface
388 * index in the address.
389 *
390 * XXX advanced-api value overrides sin6_scope_id
391 */
392 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst)) {
393 struct in6_pktinfo *pi;
394
395 /*
396 * XXX Boundary check is assumed to be already done in
397 * ip6_setpktopts().
398 */
399 if (in6p->in6p_outputopts &&
400 (pi = in6p->in6p_outputopts->ip6po_pktinfo) &&
401 pi->ipi6_ifindex) {
402 ip6->ip6_dst.s6_addr16[1] = htons(pi->ipi6_ifindex);
403 oifp = ifnet_byindex(pi->ipi6_ifindex);
404 } else if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) &&
405 in6p->in6p_moptions &&
406 in6p->in6p_moptions->im6o_multicast_ifp) {
407 oifp = in6p->in6p_moptions->im6o_multicast_ifp;
408 ip6->ip6_dst.s6_addr16[1] = htons(oifp->if_index);
409 } else if (dstsock->sin6_scope_id) {
410 /* boundary check */
411 if (dstsock->sin6_scope_id < 0 ||
412 if_index < dstsock->sin6_scope_id) {
413 error = ENXIO; /* XXX EINVAL? */
414 goto bad;
415 }
416 ip6->ip6_dst.s6_addr16[1] =
417 htons(dstsock->sin6_scope_id & 0xffff); /* XXX */
418 }
419 }
420
421 /*
422 * Source address selection.
423 */
424 if ((in6a = in6_selectsrc(dstsock, in6p->in6p_outputopts,
425 in6p->in6p_moptions, NULL, &in6p->in6p_laddr, &error)) == 0) {
426 if (error == 0)
427 error = EADDRNOTAVAIL;
428 goto bad;
429 }
430 ip6->ip6_src = *in6a;
431 ip6->ip6_flow = (ip6->ip6_flow & ~IPV6_FLOWINFO_MASK) |
432 (in6p->in6p_flowinfo & IPV6_FLOWINFO_MASK);
433 ip6->ip6_vfc = (ip6->ip6_vfc & ~IPV6_VERSION_MASK) |
434 (IPV6_VERSION & IPV6_VERSION_MASK);
435 /* ip6_plen will be filled in ip6_output, so not fill it here. */
436 ip6->ip6_nxt = in6p->in6p_ip6_nxt;
437 ip6->ip6_hlim = in6_selecthlim(in6p, oifp);
438
439 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6 ||
440 in6p->in6p_cksum != -1) {
441 struct mbuf *n;
442 int off;
443 u_int16_t *p;
444
445 /* compute checksum */
446 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6)
447 off = offsetof(struct icmp6_hdr, icmp6_cksum);
448 else
449 off = in6p->in6p_cksum;
450 if (plen < off + 1) {
451 error = EINVAL;
452 goto bad;
453 }
454 off += sizeof(struct ip6_hdr);
455
456 n = m;
457 while (n && n->m_len <= off) {
458 off -= n->m_len;
459 n = n->m_next;
460 }
461 if (!n)
462 goto bad;
463 p = (u_int16_t *)(mtod(n, caddr_t) + off);
464 *p = 0;
465 *p = in6_cksum(m, ip6->ip6_nxt, sizeof(*ip6), plen);
466 }
467
468 error = ip6_output(m, in6p->in6p_outputopts, NULL, 0,
469 in6p->in6p_moptions, &oifp, in6p);
470 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6) {
471 if (oifp)
472 icmp6_ifoutstat_inc(oifp, type, code);
473 icmp6stat.icp6s_outhist[type]++;
474 } else
475 rip6stat.rip6s_opackets++;
476
477 goto freectl;
478
479 bad:
480 if (m)
481 m_freem(m);
482
483 freectl:
484 if (control) {
485 ip6_clearpktopts(in6p->in6p_outputopts, -1);
486 in6p->in6p_outputopts = stickyopt;
487 m_freem(control);
488 }
489 INP_UNLOCK(in6p);
490 return (error);
491 }
492
493 /*
494 * Raw IPv6 socket option processing.
495 */
496 int
497 rip6_ctloutput(so, sopt)
498 struct socket *so;
499 struct sockopt *sopt;
500 {
501 int error;
502
503 if (sopt->sopt_level == IPPROTO_ICMPV6)
504 /*
505 * XXX: is it better to call icmp6_ctloutput() directly
506 * from protosw?
507 */
508 return (icmp6_ctloutput(so, sopt));
509 else if (sopt->sopt_level != IPPROTO_IPV6)
510 return (EINVAL);
511
512 error = 0;
513
514 switch (sopt->sopt_dir) {
515 case SOPT_GET:
516 switch (sopt->sopt_name) {
517 case MRT6_INIT:
518 case MRT6_DONE:
519 case MRT6_ADD_MIF:
520 case MRT6_DEL_MIF:
521 case MRT6_ADD_MFC:
522 case MRT6_DEL_MFC:
523 case MRT6_PIM:
524 error = ip6_mrouter_get(so, sopt);
525 break;
526 case IPV6_CHECKSUM:
527 error = ip6_raw_ctloutput(so, sopt);
528 break;
529 default:
530 error = ip6_ctloutput(so, sopt);
531 break;
532 }
533 break;
534
535 case SOPT_SET:
536 switch (sopt->sopt_name) {
537 case MRT6_INIT:
538 case MRT6_DONE:
539 case MRT6_ADD_MIF:
540 case MRT6_DEL_MIF:
541 case MRT6_ADD_MFC:
542 case MRT6_DEL_MFC:
543 case MRT6_PIM:
544 error = ip6_mrouter_set(so, sopt);
545 break;
546 case IPV6_CHECKSUM:
547 error = ip6_raw_ctloutput(so, sopt);
548 break;
549 default:
550 error = ip6_ctloutput(so, sopt);
551 break;
552 }
553 break;
554 }
555
556 return (error);
557 }
558
559 static int
560 rip6_attach(struct socket *so, int proto, struct thread *td)
561 {
562 struct inpcb *inp;
563 struct icmp6_filter *filter;
564 int error, s;
565
566 INP_INFO_WLOCK(&ripcbinfo);
567 inp = sotoinpcb(so);
568 if (inp) {
569 INP_INFO_WUNLOCK(&ripcbinfo);
570 panic("rip6_attach");
571 }
572 if (td && (error = suser(td)) != 0) {
573 INP_INFO_WUNLOCK(&ripcbinfo);
574 return error;
575 }
576 error = soreserve(so, rip_sendspace, rip_recvspace);
577 if (error) {
578 INP_INFO_WUNLOCK(&ripcbinfo);
579 return error;
580 }
581 MALLOC(filter, struct icmp6_filter *,
582 sizeof(struct icmp6_filter), M_PCB, M_NOWAIT);
583 if (filter == NULL) {
584 INP_INFO_WUNLOCK(&ripcbinfo);
585 return ENOMEM;
586 }
587 s = splnet();
588 error = in_pcballoc(so, &ripcbinfo, "raw6inp");
589 splx(s);
590 if (error) {
591 INP_INFO_WUNLOCK(&ripcbinfo);
592 FREE(filter, M_PCB);
593 return error;
594 }
595 inp = (struct inpcb *)so->so_pcb;
596 INP_LOCK(inp);
597 INP_INFO_WUNLOCK(&ripcbinfo);
598 inp->inp_vflag |= INP_IPV6;
599 inp->in6p_ip6_nxt = (long)proto;
600 inp->in6p_hops = -1; /* use kernel default */
601 inp->in6p_cksum = -1;
602 inp->in6p_icmp6filt = filter;
603 ICMP6_FILTER_SETPASSALL(inp->in6p_icmp6filt);
604 INP_UNLOCK(inp);
605 return 0;
606 }
607
608 static int
609 rip6_detach(struct socket *so)
610 {
611 struct inpcb *inp;
612
613 INP_INFO_WLOCK(&ripcbinfo);
614 inp = sotoinpcb(so);
615 if (inp == 0) {
616 INP_INFO_WUNLOCK(&ripcbinfo);
617 panic("rip6_detach");
618 }
619 /* xxx: RSVP */
620 if (so == ip6_mrouter)
621 ip6_mrouter_done();
622 if (inp->in6p_icmp6filt) {
623 FREE(inp->in6p_icmp6filt, M_PCB);
624 inp->in6p_icmp6filt = NULL;
625 }
626 INP_LOCK(inp);
627 in6_pcbdetach(inp);
628 INP_INFO_WUNLOCK(&ripcbinfo);
629 return 0;
630 }
631
632 static int
633 rip6_abort(struct socket *so)
634 {
635 soisdisconnected(so);
636 return rip6_detach(so);
637 }
638
639 static int
640 rip6_disconnect(struct socket *so)
641 {
642 struct inpcb *inp = sotoinpcb(so);
643
644 if ((so->so_state & SS_ISCONNECTED) == 0)
645 return ENOTCONN;
646 inp->in6p_faddr = in6addr_any;
647 return rip6_abort(so);
648 }
649
650 static int
651 rip6_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
652 {
653 struct inpcb *inp = sotoinpcb(so);
654 struct sockaddr_in6 *addr = (struct sockaddr_in6 *)nam;
655 struct ifaddr *ia = NULL;
656
657 if (nam->sa_len != sizeof(*addr))
658 return EINVAL;
659 if (TAILQ_EMPTY(&ifnet) || addr->sin6_family != AF_INET6)
660 return EADDRNOTAVAIL;
661 #ifdef ENABLE_DEFAULT_SCOPE
662 if (addr->sin6_scope_id == 0) { /* not change if specified */
663 addr->sin6_scope_id = scope6_addr2default(&addr->sin6_addr);
664 }
665 #endif
666 if (!IN6_IS_ADDR_UNSPECIFIED(&addr->sin6_addr) &&
667 (ia = ifa_ifwithaddr((struct sockaddr *)addr)) == 0)
668 return EADDRNOTAVAIL;
669 if (ia &&
670 ((struct in6_ifaddr *)ia)->ia6_flags &
671 (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY|
672 IN6_IFF_DETACHED|IN6_IFF_DEPRECATED)) {
673 return (EADDRNOTAVAIL);
674 }
675 INP_INFO_WLOCK(&ripcbinfo);
676 INP_LOCK(inp);
677 inp->in6p_laddr = addr->sin6_addr;
678 INP_UNLOCK(inp);
679 INP_INFO_WUNLOCK(&ripcbinfo);
680 return 0;
681 }
682
683 static int
684 rip6_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
685 {
686 struct inpcb *inp = sotoinpcb(so);
687 struct sockaddr_in6 *addr = (struct sockaddr_in6 *)nam;
688 struct in6_addr *in6a = NULL;
689 int error = 0;
690 #ifdef ENABLE_DEFAULT_SCOPE
691 struct sockaddr_in6 tmp;
692 #endif
693
694 if (nam->sa_len != sizeof(*addr))
695 return EINVAL;
696 if (TAILQ_EMPTY(&ifnet))
697 return EADDRNOTAVAIL;
698 if (addr->sin6_family != AF_INET6)
699 return EAFNOSUPPORT;
700 #ifdef ENABLE_DEFAULT_SCOPE
701 if (addr->sin6_scope_id == 0) { /* not change if specified */
702 /* avoid overwrites */
703 tmp = *addr;
704 addr = &tmp;
705 addr->sin6_scope_id = scope6_addr2default(&addr->sin6_addr);
706 }
707 #endif
708 INP_INFO_WLOCK(&ripcbinfo);
709 INP_LOCK(inp);
710 /* Source address selection. XXX: need pcblookup? */
711 in6a = in6_selectsrc(addr, inp->in6p_outputopts,
712 inp->in6p_moptions, NULL,
713 &inp->in6p_laddr, &error);
714 if (in6a == NULL) {
715 INP_UNLOCK(inp);
716 INP_INFO_WUNLOCK(&ripcbinfo);
717 return (error ? error : EADDRNOTAVAIL);
718 }
719 inp->in6p_laddr = *in6a;
720 inp->in6p_faddr = addr->sin6_addr;
721 soisconnected(so);
722 INP_UNLOCK(inp);
723 INP_INFO_WUNLOCK(&ripcbinfo);
724 return 0;
725 }
726
727 static int
728 rip6_shutdown(struct socket *so)
729 {
730 struct inpcb *inp;
731
732 INP_INFO_RLOCK(&ripcbinfo);
733 inp = sotoinpcb(so);
734 INP_LOCK(inp);
735 INP_INFO_RUNLOCK(&ripcbinfo);
736 socantsendmore(so);
737 INP_UNLOCK(inp);
738 return 0;
739 }
740
741 static int
742 rip6_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
743 struct mbuf *control, struct thread *td)
744 {
745 struct inpcb *inp = sotoinpcb(so);
746 struct sockaddr_in6 tmp;
747 struct sockaddr_in6 *dst;
748 int ret;
749
750 INP_INFO_WLOCK(&ripcbinfo);
751 /* always copy sockaddr to avoid overwrites */
752 /* Unlocked read. */
753 if (so->so_state & SS_ISCONNECTED) {
754 if (nam) {
755 INP_INFO_WUNLOCK(&ripcbinfo);
756 m_freem(m);
757 return EISCONN;
758 }
759 /* XXX */
760 bzero(&tmp, sizeof(tmp));
761 tmp.sin6_family = AF_INET6;
762 tmp.sin6_len = sizeof(struct sockaddr_in6);
763 bcopy(&inp->in6p_faddr, &tmp.sin6_addr,
764 sizeof(struct in6_addr));
765 dst = &tmp;
766 } else {
767 if (nam == NULL) {
768 INP_INFO_WUNLOCK(&ripcbinfo);
769 m_freem(m);
770 return ENOTCONN;
771 }
772 tmp = *(struct sockaddr_in6 *)nam;
773 dst = &tmp;
774 }
775 #ifdef ENABLE_DEFAULT_SCOPE
776 if (dst->sin6_scope_id == 0) { /* not change if specified */
777 dst->sin6_scope_id = scope6_addr2default(&dst->sin6_addr);
778 }
779 #endif
780 ret = rip6_output(m, so, dst, control);
781 INP_INFO_WUNLOCK(&ripcbinfo);
782 return (ret);
783 }
784
785 struct pr_usrreqs rip6_usrreqs = {
786 .pru_abort = rip6_abort,
787 .pru_attach = rip6_attach,
788 .pru_bind = rip6_bind,
789 .pru_connect = rip6_connect,
790 .pru_control = in6_control,
791 .pru_detach = rip6_detach,
792 .pru_disconnect = rip6_disconnect,
793 .pru_peeraddr = in6_setpeeraddr,
794 .pru_send = rip6_send,
795 .pru_shutdown = rip6_shutdown,
796 .pru_sockaddr = in6_setsockaddr,
797 };
Cache object: e46c18d5e71dc4d5a4abb9884dde7f95
|