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
30 /*-
31 * Copyright (c) 1982, 1986, 1988, 1993
32 * The Regents of the University of California. All rights reserved.
33 *
34 * Redistribution and use in source and binary forms, with or without
35 * modification, are permitted provided that the following conditions
36 * are met:
37 * 1. Redistributions of source code must retain the above copyright
38 * notice, this list of conditions and the following disclaimer.
39 * 2. Redistributions in binary form must reproduce the above copyright
40 * notice, this list of conditions and the following disclaimer in the
41 * documentation and/or other materials provided with the distribution.
42 * 4. Neither the name of the University nor the names of its contributors
43 * may be used to endorse or promote products derived from this software
44 * without specific prior written permission.
45 *
46 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
47 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
48 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
49 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
50 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
51 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
52 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
53 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
54 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
55 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
56 * SUCH DAMAGE.
57 *
58 * @(#)raw_ip.c 8.2 (Berkeley) 1/4/94
59 */
60
61 #include <sys/cdefs.h>
62 __FBSDID("$FreeBSD$");
63
64 #include "opt_ipsec.h"
65 #include "opt_inet6.h"
66
67 #include <sys/param.h>
68 #include <sys/errno.h>
69 #include <sys/lock.h>
70 #include <sys/malloc.h>
71 #include <sys/mbuf.h>
72 #include <sys/proc.h>
73 #include <sys/protosw.h>
74 #include <sys/signalvar.h>
75 #include <sys/socket.h>
76 #include <sys/socketvar.h>
77 #include <sys/sx.h>
78 #include <sys/systm.h>
79 #include <sys/syslog.h>
80
81 #include <net/if.h>
82 #include <net/if_types.h>
83 #include <net/route.h>
84
85 #include <netinet/in.h>
86 #include <netinet/in_var.h>
87 #include <netinet/in_systm.h>
88 #include <netinet/icmp6.h>
89 #include <netinet/in_pcb.h>
90 #include <netinet/ip6.h>
91 #include <netinet6/ip6protosw.h>
92 #include <netinet6/ip6_mroute.h>
93 #include <netinet6/in6_pcb.h>
94 #include <netinet6/ip6_var.h>
95 #include <netinet6/nd6.h>
96 #include <netinet6/raw_ip6.h>
97 #include <netinet6/scope6_var.h>
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, proto, *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)(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, *optp;
329 struct ifnet *oifp = NULL;
330 int type = 0, code = 0; /* for ICMPv6 output statistics only */
331 int priv = 0;
332 int scope_ambiguous = 0;
333 struct in6_addr *in6a;
334 va_list ap;
335
336 va_start(ap, m);
337 so = va_arg(ap, struct socket *);
338 dstsock = va_arg(ap, struct sockaddr_in6 *);
339 control = va_arg(ap, struct mbuf *);
340 va_end(ap);
341
342 in6p = sotoin6pcb(so);
343 INP_LOCK(in6p);
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 in6p->in6p_outputopts, priv, so->so_proto->pr_protocol))
352 != 0) {
353 goto bad;
354 }
355 optp = &opt;
356 } else
357 optp = in6p->in6p_outputopts;
358
359 /*
360 * Check and convert scope zone ID into internal form.
361 * XXX: we may still need to determine the zone later.
362 */
363 if (!(so->so_state & SS_ISCONNECTED)) {
364 if (dstsock->sin6_scope_id == 0 && !ip6_use_defzone)
365 scope_ambiguous = 1;
366 if ((error = sa6_embedscope(dstsock, ip6_use_defzone)) != 0)
367 goto bad;
368 }
369
370 /*
371 * For an ICMPv6 packet, we should know its type and code
372 * to update statistics.
373 */
374 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6) {
375 struct icmp6_hdr *icmp6;
376 if (m->m_len < sizeof(struct icmp6_hdr) &&
377 (m = m_pullup(m, sizeof(struct icmp6_hdr))) == NULL) {
378 error = ENOBUFS;
379 goto bad;
380 }
381 icmp6 = mtod(m, struct icmp6_hdr *);
382 type = icmp6->icmp6_type;
383 code = icmp6->icmp6_code;
384 }
385
386 M_PREPEND(m, sizeof(*ip6), M_DONTWAIT);
387 if (m == NULL) {
388 error = ENOBUFS;
389 goto bad;
390 }
391 ip6 = mtod(m, struct ip6_hdr *);
392
393 /*
394 * Source address selection.
395 */
396 if ((in6a = in6_selectsrc(dstsock, optp, in6p->in6p_moptions, NULL,
397 &in6p->in6p_laddr, &oifp, &error)) == NULL) {
398 if (error == 0)
399 error = EADDRNOTAVAIL;
400 goto bad;
401 }
402 ip6->ip6_src = *in6a;
403
404 if (oifp && scope_ambiguous) {
405 /*
406 * Application should provide a proper zone ID or the use of
407 * default zone IDs should be enabled. Unfortunately, some
408 * applications do not behave as it should, so we need a
409 * workaround. Even if an appropriate ID is not determined
410 * (when it's required), if we can determine the outgoing
411 * interface. determine the zone ID based on the interface.
412 */
413 error = in6_setscope(&dstsock->sin6_addr, oifp, NULL);
414 if (error != 0)
415 goto bad;
416 }
417 ip6->ip6_dst = dstsock->sin6_addr;
418
419 /* fill in the rest of the IPv6 header fields */
420 ip6->ip6_flow = (ip6->ip6_flow & ~IPV6_FLOWINFO_MASK) |
421 (in6p->in6p_flowinfo & IPV6_FLOWINFO_MASK);
422 ip6->ip6_vfc = (ip6->ip6_vfc & ~IPV6_VERSION_MASK) |
423 (IPV6_VERSION & IPV6_VERSION_MASK);
424 /* ip6_plen will be filled in ip6_output, so not fill it here. */
425 ip6->ip6_nxt = in6p->in6p_ip6_nxt;
426 ip6->ip6_hlim = in6_selecthlim(in6p, oifp);
427
428 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6 ||
429 in6p->in6p_cksum != -1) {
430 struct mbuf *n;
431 int off;
432 u_int16_t *p;
433
434 /* compute checksum */
435 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6)
436 off = offsetof(struct icmp6_hdr, icmp6_cksum);
437 else
438 off = in6p->in6p_cksum;
439 if (plen < off + 1) {
440 error = EINVAL;
441 goto bad;
442 }
443 off += sizeof(struct ip6_hdr);
444
445 n = m;
446 while (n && n->m_len <= off) {
447 off -= n->m_len;
448 n = n->m_next;
449 }
450 if (!n)
451 goto bad;
452 p = (u_int16_t *)(mtod(n, caddr_t) + off);
453 *p = 0;
454 *p = in6_cksum(m, ip6->ip6_nxt, sizeof(*ip6), plen);
455 }
456
457 error = ip6_output(m, optp, NULL, 0, in6p->in6p_moptions, &oifp, in6p);
458 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6) {
459 if (oifp)
460 icmp6_ifoutstat_inc(oifp, type, code);
461 icmp6stat.icp6s_outhist[type]++;
462 } else
463 rip6stat.rip6s_opackets++;
464
465 goto freectl;
466
467 bad:
468 if (m)
469 m_freem(m);
470
471 freectl:
472 if (control) {
473 ip6_clearpktopts(&opt, -1);
474 m_freem(control);
475 }
476 INP_UNLOCK(in6p);
477 return (error);
478 }
479
480 /*
481 * Raw IPv6 socket option processing.
482 */
483 int
484 rip6_ctloutput(so, sopt)
485 struct socket *so;
486 struct sockopt *sopt;
487 {
488 int error;
489
490 if (sopt->sopt_level == IPPROTO_ICMPV6)
491 /*
492 * XXX: is it better to call icmp6_ctloutput() directly
493 * from protosw?
494 */
495 return (icmp6_ctloutput(so, sopt));
496 else if (sopt->sopt_level != IPPROTO_IPV6)
497 return (EINVAL);
498
499 error = 0;
500
501 switch (sopt->sopt_dir) {
502 case SOPT_GET:
503 switch (sopt->sopt_name) {
504 case MRT6_INIT:
505 case MRT6_DONE:
506 case MRT6_ADD_MIF:
507 case MRT6_DEL_MIF:
508 case MRT6_ADD_MFC:
509 case MRT6_DEL_MFC:
510 case MRT6_PIM:
511 error = ip6_mrouter_get(so, sopt);
512 break;
513 case IPV6_CHECKSUM:
514 error = ip6_raw_ctloutput(so, sopt);
515 break;
516 default:
517 error = ip6_ctloutput(so, sopt);
518 break;
519 }
520 break;
521
522 case SOPT_SET:
523 switch (sopt->sopt_name) {
524 case MRT6_INIT:
525 case MRT6_DONE:
526 case MRT6_ADD_MIF:
527 case MRT6_DEL_MIF:
528 case MRT6_ADD_MFC:
529 case MRT6_DEL_MFC:
530 case MRT6_PIM:
531 error = ip6_mrouter_set(so, sopt);
532 break;
533 case IPV6_CHECKSUM:
534 error = ip6_raw_ctloutput(so, sopt);
535 break;
536 default:
537 error = ip6_ctloutput(so, sopt);
538 break;
539 }
540 break;
541 }
542
543 return (error);
544 }
545
546 static int
547 rip6_attach(struct socket *so, int proto, struct thread *td)
548 {
549 struct inpcb *inp;
550 struct icmp6_filter *filter;
551 int error, s;
552
553 INP_INFO_WLOCK(&ripcbinfo);
554 inp = sotoinpcb(so);
555 if (inp) {
556 INP_INFO_WUNLOCK(&ripcbinfo);
557 panic("rip6_attach");
558 }
559 if (td && (error = suser(td)) != 0) {
560 INP_INFO_WUNLOCK(&ripcbinfo);
561 return error;
562 }
563 error = soreserve(so, rip_sendspace, rip_recvspace);
564 if (error) {
565 INP_INFO_WUNLOCK(&ripcbinfo);
566 return error;
567 }
568 MALLOC(filter, struct icmp6_filter *,
569 sizeof(struct icmp6_filter), M_PCB, M_NOWAIT);
570 if (filter == NULL) {
571 INP_INFO_WUNLOCK(&ripcbinfo);
572 return ENOMEM;
573 }
574 s = splnet();
575 error = in_pcballoc(so, &ripcbinfo);
576 splx(s);
577 if (error) {
578 INP_INFO_WUNLOCK(&ripcbinfo);
579 FREE(filter, M_PCB);
580 return error;
581 }
582 inp = (struct inpcb *)so->so_pcb;
583 INP_INFO_WUNLOCK(&ripcbinfo);
584 inp->inp_vflag |= INP_IPV6;
585 inp->in6p_ip6_nxt = (long)proto;
586 inp->in6p_hops = -1; /* use kernel default */
587 inp->in6p_cksum = -1;
588 inp->in6p_icmp6filt = filter;
589 ICMP6_FILTER_SETPASSALL(inp->in6p_icmp6filt);
590 INP_UNLOCK(inp);
591 return 0;
592 }
593
594 static int
595 rip6_detach(struct socket *so)
596 {
597 struct inpcb *inp;
598
599 INP_INFO_WLOCK(&ripcbinfo);
600 inp = sotoinpcb(so);
601 if (inp == 0) {
602 INP_INFO_WUNLOCK(&ripcbinfo);
603 panic("rip6_detach");
604 }
605 /* xxx: RSVP */
606 if (so == ip6_mrouter)
607 ip6_mrouter_done();
608 if (inp->in6p_icmp6filt) {
609 FREE(inp->in6p_icmp6filt, M_PCB);
610 inp->in6p_icmp6filt = NULL;
611 }
612 INP_LOCK(inp);
613 in6_pcbdetach(inp);
614 INP_INFO_WUNLOCK(&ripcbinfo);
615 return 0;
616 }
617
618 static int
619 rip6_abort(struct socket *so)
620 {
621 soisdisconnected(so);
622 return rip6_detach(so);
623 }
624
625 static int
626 rip6_disconnect(struct socket *so)
627 {
628 struct inpcb *inp = sotoinpcb(so);
629
630 if ((so->so_state & SS_ISCONNECTED) == 0)
631 return ENOTCONN;
632 inp->in6p_faddr = in6addr_any;
633 return rip6_abort(so);
634 }
635
636 static int
637 rip6_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
638 {
639 struct inpcb *inp = sotoinpcb(so);
640 struct sockaddr_in6 *addr = (struct sockaddr_in6 *)nam;
641 struct ifaddr *ia = NULL;
642 int error = 0;
643
644 if (nam->sa_len != sizeof(*addr))
645 return EINVAL;
646 if (TAILQ_EMPTY(&ifnet) || addr->sin6_family != AF_INET6)
647 return EADDRNOTAVAIL;
648 if ((error = sa6_embedscope(addr, ip6_use_defzone)) != 0)
649 return(error);
650
651 if (!IN6_IS_ADDR_UNSPECIFIED(&addr->sin6_addr) &&
652 (ia = ifa_ifwithaddr((struct sockaddr *)addr)) == 0)
653 return EADDRNOTAVAIL;
654 if (ia &&
655 ((struct in6_ifaddr *)ia)->ia6_flags &
656 (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY|
657 IN6_IFF_DETACHED|IN6_IFF_DEPRECATED)) {
658 return (EADDRNOTAVAIL);
659 }
660 INP_INFO_WLOCK(&ripcbinfo);
661 INP_LOCK(inp);
662 inp->in6p_laddr = addr->sin6_addr;
663 INP_UNLOCK(inp);
664 INP_INFO_WUNLOCK(&ripcbinfo);
665 return 0;
666 }
667
668 static int
669 rip6_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
670 {
671 struct inpcb *inp = sotoinpcb(so);
672 struct sockaddr_in6 *addr = (struct sockaddr_in6 *)nam;
673 struct in6_addr *in6a = NULL;
674 struct ifnet *ifp = NULL;
675 int error = 0, scope_ambiguous = 0;
676
677 if (nam->sa_len != sizeof(*addr))
678 return EINVAL;
679 if (TAILQ_EMPTY(&ifnet))
680 return EADDRNOTAVAIL;
681 if (addr->sin6_family != AF_INET6)
682 return EAFNOSUPPORT;
683
684 /*
685 * Application should provide a proper zone ID or the use of
686 * default zone IDs should be enabled. Unfortunately, some
687 * applications do not behave as it should, so we need a
688 * workaround. Even if an appropriate ID is not determined,
689 * we'll see if we can determine the outgoing interface. If we
690 * can, determine the zone ID based on the interface below.
691 */
692 if (addr->sin6_scope_id == 0 && !ip6_use_defzone)
693 scope_ambiguous = 1;
694 if ((error = sa6_embedscope(addr, ip6_use_defzone)) != 0)
695 return(error);
696
697 INP_INFO_WLOCK(&ripcbinfo);
698 INP_LOCK(inp);
699 /* Source address selection. XXX: need pcblookup? */
700 in6a = in6_selectsrc(addr, inp->in6p_outputopts,
701 inp->in6p_moptions, NULL,
702 &inp->in6p_laddr, &ifp, &error);
703 if (in6a == NULL) {
704 INP_UNLOCK(inp);
705 INP_INFO_WUNLOCK(&ripcbinfo);
706 return (error ? error : EADDRNOTAVAIL);
707 }
708
709 /* XXX: see above */
710 if (ifp && scope_ambiguous &&
711 (error = in6_setscope(&addr->sin6_addr, ifp, NULL)) != 0) {
712 INP_UNLOCK(inp);
713 INP_INFO_WUNLOCK(&ripcbinfo);
714 return(error);
715 }
716 inp->in6p_faddr = addr->sin6_addr;
717 inp->in6p_laddr = *in6a;
718 soisconnected(so);
719 INP_UNLOCK(inp);
720 INP_INFO_WUNLOCK(&ripcbinfo);
721 return 0;
722 }
723
724 static int
725 rip6_shutdown(struct socket *so)
726 {
727 struct inpcb *inp;
728
729 INP_INFO_RLOCK(&ripcbinfo);
730 inp = sotoinpcb(so);
731 INP_LOCK(inp);
732 INP_INFO_RUNLOCK(&ripcbinfo);
733 socantsendmore(so);
734 INP_UNLOCK(inp);
735 return 0;
736 }
737
738 static int
739 rip6_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
740 struct mbuf *control, struct thread *td)
741 {
742 struct inpcb *inp = sotoinpcb(so);
743 struct sockaddr_in6 tmp;
744 struct sockaddr_in6 *dst;
745 int ret;
746
747 INP_INFO_WLOCK(&ripcbinfo);
748 /* always copy sockaddr to avoid overwrites */
749 /* Unlocked read. */
750 if (so->so_state & SS_ISCONNECTED) {
751 if (nam) {
752 INP_INFO_WUNLOCK(&ripcbinfo);
753 m_freem(m);
754 return EISCONN;
755 }
756 /* XXX */
757 bzero(&tmp, sizeof(tmp));
758 tmp.sin6_family = AF_INET6;
759 tmp.sin6_len = sizeof(struct sockaddr_in6);
760 bcopy(&inp->in6p_faddr, &tmp.sin6_addr,
761 sizeof(struct in6_addr));
762 dst = &tmp;
763 } else {
764 if (nam == NULL) {
765 INP_INFO_WUNLOCK(&ripcbinfo);
766 m_freem(m);
767 return ENOTCONN;
768 }
769 if (nam->sa_len != sizeof(struct sockaddr_in6)) {
770 INP_INFO_WUNLOCK(&ripcbinfo);
771 m_freem(m);
772 return(EINVAL);
773 }
774 tmp = *(struct sockaddr_in6 *)nam;
775 dst = &tmp;
776
777 if (dst->sin6_family == AF_UNSPEC) {
778 /*
779 * XXX: we allow this case for backward
780 * compatibility to buggy applications that
781 * rely on old (and wrong) kernel behavior.
782 */
783 log(LOG_INFO, "rip6 SEND: address family is "
784 "unspec. Assume AF_INET6\n");
785 dst->sin6_family = AF_INET6;
786 } else if (dst->sin6_family != AF_INET6) {
787 INP_INFO_WUNLOCK(&ripcbinfo);
788 m_freem(m);
789 return(EAFNOSUPPORT);
790 }
791 }
792 ret = rip6_output(m, so, dst, control);
793 INP_INFO_WUNLOCK(&ripcbinfo);
794 return (ret);
795 }
796
797 struct pr_usrreqs rip6_usrreqs = {
798 .pru_abort = rip6_abort,
799 .pru_attach = rip6_attach,
800 .pru_bind = rip6_bind,
801 .pru_connect = rip6_connect,
802 .pru_control = in6_control,
803 .pru_detach = rip6_detach,
804 .pru_disconnect = rip6_disconnect,
805 .pru_peeraddr = in6_setpeeraddr,
806 .pru_send = rip6_send,
807 .pru_shutdown = rip6_shutdown,
808 .pru_sockaddr = in6_setsockaddr,
809 };
Cache object: d9b701e3ecd4eb4fa2a6661b92091bd7
|