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.
33 * All rights reserved.
34 *
35 * Redistribution and use in source and binary forms, with or without
36 * modification, are permitted provided that the following conditions
37 * are met:
38 * 1. Redistributions of source code must retain the above copyright
39 * notice, this list of conditions and the following disclaimer.
40 * 2. Redistributions in binary form must reproduce the above copyright
41 * notice, this list of conditions and the following disclaimer in the
42 * documentation and/or other materials provided with the distribution.
43 * 4. Neither the name of the University nor the names of its contributors
44 * may be used to endorse or promote products derived from this software
45 * without specific prior written permission.
46 *
47 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
48 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
49 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
50 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
51 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
52 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
53 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
54 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
55 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
56 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
57 * SUCH DAMAGE.
58 *
59 * @(#)raw_ip.c 8.2 (Berkeley) 1/4/94
60 */
61
62 #include <sys/cdefs.h>
63 __FBSDID("$FreeBSD: releng/7.3/sys/netinet6/raw_ip6.c 202914 2010-01-24 10:08:54Z bz $");
64
65 #include "opt_ipsec.h"
66 #include "opt_inet6.h"
67
68 #include <sys/param.h>
69 #include <sys/errno.h>
70 #include <sys/jail.h>
71 #include <sys/lock.h>
72 #include <sys/malloc.h>
73 #include <sys/mbuf.h>
74 #include <sys/priv.h>
75 #include <sys/proc.h>
76 #include <sys/protosw.h>
77 #include <sys/signalvar.h>
78 #include <sys/socket.h>
79 #include <sys/socketvar.h>
80 #include <sys/sx.h>
81 #include <sys/syslog.h>
82
83 #include <net/if.h>
84 #include <net/if_types.h>
85 #include <net/route.h>
86
87 #include <netinet/in.h>
88 #include <netinet/in_var.h>
89 #include <netinet/in_systm.h>
90 #include <netinet/icmp6.h>
91 #include <netinet/in_pcb.h>
92 #include <netinet/ip6.h>
93 #include <netinet6/ip6protosw.h>
94 #include <netinet6/ip6_mroute.h>
95 #include <netinet6/in6_pcb.h>
96 #include <netinet6/ip6_var.h>
97 #include <netinet6/nd6.h>
98 #include <netinet6/raw_ip6.h>
99 #include <netinet6/scope6_var.h>
100
101 #ifdef IPSEC
102 #include <netipsec/ipsec.h>
103 #include <netipsec/ipsec6.h>
104 #endif /* IPSEC */
105
106 #include <machine/stdarg.h>
107
108 #define satosin6(sa) ((struct sockaddr_in6 *)(sa))
109 #define ifatoia6(ifa) ((struct in6_ifaddr *)(ifa))
110
111 /*
112 * Raw interface to IP6 protocol.
113 */
114
115 extern struct inpcbhead ripcb;
116 extern struct inpcbinfo ripcbinfo;
117 extern u_long rip_sendspace;
118 extern u_long rip_recvspace;
119
120 struct rip6stat rip6stat;
121
122 /*
123 * Hooks for multicast forwarding.
124 */
125 struct socket *ip6_mrouter = NULL;
126 int (*ip6_mrouter_set)(struct socket *, struct sockopt *);
127 int (*ip6_mrouter_get)(struct socket *, struct sockopt *);
128 int (*ip6_mrouter_done)(void);
129 int (*ip6_mforward)(struct ip6_hdr *, struct ifnet *, struct mbuf *);
130 int (*mrt6_ioctl)(int, caddr_t);
131
132 /*
133 * Setup generic address and protocol structures for raw_input routine, then
134 * pass them along with mbuf chain.
135 */
136 int
137 rip6_input(struct mbuf **mp, int *offp, int proto)
138 {
139 struct mbuf *m = *mp;
140 register struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
141 register struct inpcb *in6p;
142 struct inpcb *last = 0;
143 struct mbuf *opts = NULL;
144 struct sockaddr_in6 fromsa;
145
146 rip6stat.rip6s_ipackets++;
147
148 if (faithprefix_p != NULL && (*faithprefix_p)(&ip6->ip6_dst)) {
149 /* XXX Send icmp6 host/port unreach? */
150 m_freem(m);
151 return (IPPROTO_DONE);
152 }
153
154 init_sin6(&fromsa, m); /* general init */
155
156 INP_INFO_RLOCK(&ripcbinfo);
157 LIST_FOREACH(in6p, &ripcb, inp_list) {
158 /* XXX inp locking */
159 if ((in6p->inp_vflag & INP_IPV6) == 0)
160 continue;
161 if (in6p->inp_ip_p &&
162 in6p->inp_ip_p != proto)
163 continue;
164 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) &&
165 !IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &ip6->ip6_dst))
166 continue;
167 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr) &&
168 !IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, &ip6->ip6_src))
169 continue;
170 if (prison_check_ip6(in6p->inp_cred, &ip6->ip6_dst) != 0)
171 continue;
172 INP_RLOCK(in6p);
173 if (in6p->in6p_cksum != -1) {
174 rip6stat.rip6s_isum++;
175 if (in6_cksum(m, proto, *offp,
176 m->m_pkthdr.len - *offp)) {
177 INP_RUNLOCK(in6p);
178 rip6stat.rip6s_badsum++;
179 continue;
180 }
181 }
182 if (last) {
183 struct mbuf *n = m_copy(m, 0, (int)M_COPYALL);
184
185 #ifdef IPSEC
186 /*
187 * Check AH/ESP integrity.
188 */
189 if (n && ipsec6_in_reject(n, last)) {
190 m_freem(n);
191 ipsec6stat.in_polvio++;
192 /* Do not inject data into pcb. */
193 } else
194 #endif /* IPSEC */
195 if (n) {
196 if (last->inp_flags & INP_CONTROLOPTS ||
197 last->inp_socket->so_options & SO_TIMESTAMP)
198 ip6_savecontrol(last, n, &opts);
199 /* strip intermediate headers */
200 m_adj(n, *offp);
201 if (sbappendaddr(&last->inp_socket->so_rcv,
202 (struct sockaddr *)&fromsa,
203 n, opts) == 0) {
204 m_freem(n);
205 if (opts)
206 m_freem(opts);
207 rip6stat.rip6s_fullsock++;
208 } else
209 sorwakeup(last->inp_socket);
210 opts = NULL;
211 }
212 INP_RUNLOCK(last);
213 }
214 last = in6p;
215 }
216 INP_INFO_RUNLOCK(&ripcbinfo);
217 #ifdef IPSEC
218 /*
219 * Check AH/ESP integrity.
220 */
221 if (last && ipsec6_in_reject(m, last)) {
222 m_freem(m);
223 ipsec6stat.in_polvio++;
224 ip6stat.ip6s_delivered--;
225 /* Do not inject data into pcb. */
226 INP_RUNLOCK(last);
227 } else
228 #endif /* IPSEC */
229 if (last) {
230 if (last->inp_flags & INP_CONTROLOPTS ||
231 last->inp_socket->so_options & SO_TIMESTAMP)
232 ip6_savecontrol(last, m, &opts);
233 /* Strip intermediate headers. */
234 m_adj(m, *offp);
235 if (sbappendaddr(&last->inp_socket->so_rcv,
236 (struct sockaddr *)&fromsa, m, opts) == 0) {
237 m_freem(m);
238 if (opts)
239 m_freem(opts);
240 rip6stat.rip6s_fullsock++;
241 } else
242 sorwakeup(last->inp_socket);
243 INP_RUNLOCK(last);
244 } else {
245 rip6stat.rip6s_nosock++;
246 if (m->m_flags & M_MCAST)
247 rip6stat.rip6s_nosockmcast++;
248 if (proto == IPPROTO_NONE)
249 m_freem(m);
250 else {
251 char *prvnxtp = ip6_get_prevhdr(m, *offp); /* XXX */
252 icmp6_error(m, ICMP6_PARAM_PROB,
253 ICMP6_PARAMPROB_NEXTHEADER,
254 prvnxtp - mtod(m, char *));
255 }
256 ip6stat.ip6s_delivered--;
257 }
258 return (IPPROTO_DONE);
259 }
260
261 void
262 rip6_ctlinput(int cmd, struct sockaddr *sa, void *d)
263 {
264 struct ip6_hdr *ip6;
265 struct mbuf *m;
266 int off = 0;
267 struct ip6ctlparam *ip6cp = NULL;
268 const struct sockaddr_in6 *sa6_src = NULL;
269 void *cmdarg;
270 struct inpcb *(*notify)(struct inpcb *, int) = in6_rtchange;
271
272 if (sa->sa_family != AF_INET6 ||
273 sa->sa_len != sizeof(struct sockaddr_in6))
274 return;
275
276 if ((unsigned)cmd >= PRC_NCMDS)
277 return;
278 if (PRC_IS_REDIRECT(cmd))
279 notify = in6_rtchange, d = NULL;
280 else if (cmd == PRC_HOSTDEAD)
281 d = NULL;
282 else if (inet6ctlerrmap[cmd] == 0)
283 return;
284
285 /*
286 * If the parameter is from icmp6, decode it.
287 */
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, 0, cmd, cmdarg, notify);
304 }
305
306 /*
307 * Generate IPv6 header and pass packet to ip6_output. Tack on options user
308 * 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, *optp;
328 struct ifnet *oifp = NULL;
329 int type = 0, code = 0; /* for ICMPv6 output statistics only */
330 int scope_ambiguous = 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 = sotoinpcb(so);
341 INP_WLOCK(in6p);
342
343 dst = &dstsock->sin6_addr;
344 if (control) {
345 if ((error = ip6_setpktopts(control, &opt,
346 in6p->in6p_outputopts, so->so_cred,
347 so->so_proto->pr_protocol)) != 0) {
348 goto bad;
349 }
350 optp = &opt;
351 } else
352 optp = in6p->in6p_outputopts;
353
354 /*
355 * Check and convert scope zone ID into internal form.
356 *
357 * XXX: we may still need to determine the zone later.
358 */
359 if (!(so->so_state & SS_ISCONNECTED)) {
360 if (dstsock->sin6_scope_id == 0 && !ip6_use_defzone)
361 scope_ambiguous = 1;
362 if ((error = sa6_embedscope(dstsock, ip6_use_defzone)) != 0)
363 goto bad;
364 }
365
366 /*
367 * For an ICMPv6 packet, we should know its type and code to update
368 * statistics.
369 */
370 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6) {
371 struct icmp6_hdr *icmp6;
372 if (m->m_len < sizeof(struct icmp6_hdr) &&
373 (m = m_pullup(m, sizeof(struct icmp6_hdr))) == NULL) {
374 error = ENOBUFS;
375 goto bad;
376 }
377 icmp6 = mtod(m, struct icmp6_hdr *);
378 type = icmp6->icmp6_type;
379 code = icmp6->icmp6_code;
380 }
381
382 M_PREPEND(m, sizeof(*ip6), M_DONTWAIT);
383 if (m == NULL) {
384 error = ENOBUFS;
385 goto bad;
386 }
387 ip6 = mtod(m, struct ip6_hdr *);
388
389 /*
390 * Source address selection.
391 */
392 error = in6_selectsrc(dstsock, optp, in6p, NULL, so->so_cred,
393 &oifp, &in6a);
394 if (error)
395 goto bad;
396 error = prison_get_ip6(in6p->inp_cred, &in6a);
397 if (error != 0)
398 goto bad;
399 ip6->ip6_src = in6a;
400
401 if (oifp && scope_ambiguous) {
402 /*
403 * Application should provide a proper zone ID or the use of
404 * default zone IDs should be enabled. Unfortunately, some
405 * applications do not behave as it should, so we need a
406 * workaround. Even if an appropriate ID is not determined
407 * (when it's required), if we can determine the outgoing
408 * interface. determine the zone ID based on the interface.
409 */
410 error = in6_setscope(&dstsock->sin6_addr, oifp, NULL);
411 if (error != 0)
412 goto bad;
413 }
414 ip6->ip6_dst = dstsock->sin6_addr;
415
416 /*
417 * Fill in the rest of the IPv6 header fields.
418 */
419 ip6->ip6_flow = (ip6->ip6_flow & ~IPV6_FLOWINFO_MASK) |
420 (in6p->inp_flow & IPV6_FLOWINFO_MASK);
421 ip6->ip6_vfc = (ip6->ip6_vfc & ~IPV6_VERSION_MASK) |
422 (IPV6_VERSION & IPV6_VERSION_MASK);
423
424 /*
425 * ip6_plen will be filled in ip6_output, so not fill it here.
426 */
427 ip6->ip6_nxt = in6p->inp_ip_p;
428 ip6->ip6_hlim = in6_selecthlim(in6p, oifp);
429
430 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6 ||
431 in6p->in6p_cksum != -1) {
432 struct mbuf *n;
433 int off;
434 u_int16_t *p;
435
436 /* Compute checksum. */
437 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6)
438 off = offsetof(struct icmp6_hdr, icmp6_cksum);
439 else
440 off = in6p->in6p_cksum;
441 if (plen < off + 1) {
442 error = EINVAL;
443 goto bad;
444 }
445 off += sizeof(struct ip6_hdr);
446
447 n = m;
448 while (n && n->m_len <= off) {
449 off -= n->m_len;
450 n = n->m_next;
451 }
452 if (!n)
453 goto bad;
454 p = (u_int16_t *)(mtod(n, caddr_t) + off);
455 *p = 0;
456 *p = in6_cksum(m, ip6->ip6_nxt, sizeof(*ip6), plen);
457 }
458
459 error = ip6_output(m, optp, NULL, 0, in6p->in6p_moptions, &oifp, in6p);
460 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6) {
461 if (oifp)
462 icmp6_ifoutstat_inc(oifp, type, code);
463 icmp6stat.icp6s_outhist[type]++;
464 } else
465 rip6stat.rip6s_opackets++;
466
467 goto freectl;
468
469 bad:
470 if (m)
471 m_freem(m);
472
473 freectl:
474 if (control) {
475 ip6_clearpktopts(&opt, -1);
476 m_freem(control);
477 }
478 INP_WUNLOCK(in6p);
479 return (error);
480 }
481
482 /*
483 * Raw IPv6 socket option processing.
484 */
485 int
486 rip6_ctloutput(struct socket *so, 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 ? ip6_mrouter_get(so, sopt) :
512 EOPNOTSUPP;
513 break;
514 case IPV6_CHECKSUM:
515 error = ip6_raw_ctloutput(so, sopt);
516 break;
517 default:
518 error = ip6_ctloutput(so, sopt);
519 break;
520 }
521 break;
522
523 case SOPT_SET:
524 switch (sopt->sopt_name) {
525 case MRT6_INIT:
526 case MRT6_DONE:
527 case MRT6_ADD_MIF:
528 case MRT6_DEL_MIF:
529 case MRT6_ADD_MFC:
530 case MRT6_DEL_MFC:
531 case MRT6_PIM:
532 error = ip6_mrouter_set ? ip6_mrouter_set(so, sopt) :
533 EOPNOTSUPP;
534 break;
535 case IPV6_CHECKSUM:
536 error = ip6_raw_ctloutput(so, sopt);
537 break;
538 default:
539 error = ip6_ctloutput(so, sopt);
540 break;
541 }
542 break;
543 }
544
545 return (error);
546 }
547
548 static int
549 rip6_attach(struct socket *so, int proto, struct thread *td)
550 {
551 struct inpcb *inp;
552 struct icmp6_filter *filter;
553 int error;
554
555 inp = sotoinpcb(so);
556 KASSERT(inp == NULL, ("rip6_attach: inp != NULL"));
557
558 error = priv_check(td, PRIV_NETINET_RAW);
559 if (error)
560 return (error);
561 error = soreserve(so, rip_sendspace, rip_recvspace);
562 if (error)
563 return (error);
564 MALLOC(filter, struct icmp6_filter *,
565 sizeof(struct icmp6_filter), M_PCB, M_NOWAIT);
566 if (filter == NULL)
567 return (ENOMEM);
568 INP_INFO_WLOCK(&ripcbinfo);
569 error = in_pcballoc(so, &ripcbinfo);
570 if (error) {
571 INP_INFO_WUNLOCK(&ripcbinfo);
572 FREE(filter, M_PCB);
573 return (error);
574 }
575 inp = (struct inpcb *)so->so_pcb;
576 INP_INFO_WUNLOCK(&ripcbinfo);
577 inp->inp_vflag |= INP_IPV6;
578 inp->inp_ip_p = (long)proto;
579 inp->in6p_hops = -1; /* use kernel default */
580 inp->in6p_cksum = -1;
581 inp->in6p_icmp6filt = filter;
582 ICMP6_FILTER_SETPASSALL(inp->in6p_icmp6filt);
583 INP_WUNLOCK(inp);
584 return (0);
585 }
586
587 static void
588 rip6_detach(struct socket *so)
589 {
590 struct inpcb *inp;
591
592 inp = sotoinpcb(so);
593 KASSERT(inp != NULL, ("rip6_detach: inp == NULL"));
594
595 if (so == ip6_mrouter && ip6_mrouter_done)
596 ip6_mrouter_done();
597 /* xxx: RSVP */
598 INP_INFO_WLOCK(&ripcbinfo);
599 INP_WLOCK(inp);
600 FREE(inp->in6p_icmp6filt, M_PCB);
601 in_pcbdetach(inp);
602 in_pcbfree(inp);
603 INP_INFO_WUNLOCK(&ripcbinfo);
604 }
605
606 /* XXXRW: This can't ever be called. */
607 static void
608 rip6_abort(struct socket *so)
609 {
610 struct inpcb *inp;
611
612 inp = sotoinpcb(so);
613 KASSERT(inp != NULL, ("rip6_abort: inp == NULL"));
614
615 soisdisconnected(so);
616 }
617
618 static void
619 rip6_close(struct socket *so)
620 {
621 struct inpcb *inp;
622
623 inp = sotoinpcb(so);
624 KASSERT(inp != NULL, ("rip6_close: inp == NULL"));
625
626 soisdisconnected(so);
627 }
628
629 static int
630 rip6_disconnect(struct socket *so)
631 {
632 struct inpcb *inp;
633
634 inp = sotoinpcb(so);
635 KASSERT(inp != NULL, ("rip6_disconnect: inp == NULL"));
636
637 if ((so->so_state & SS_ISCONNECTED) == 0)
638 return (ENOTCONN);
639 inp->in6p_faddr = in6addr_any;
640 rip6_abort(so);
641 return (0);
642 }
643
644 static int
645 rip6_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
646 {
647 struct inpcb *inp;
648 struct sockaddr_in6 *addr = (struct sockaddr_in6 *)nam;
649 struct ifaddr *ia = NULL;
650 int error = 0;
651
652 inp = sotoinpcb(so);
653 KASSERT(inp != NULL, ("rip6_bind: inp == NULL"));
654
655 if (nam->sa_len != sizeof(*addr))
656 return (EINVAL);
657 if ((error = prison_check_ip6(td->td_ucred, &addr->sin6_addr)) != 0)
658 return (error);
659 if (TAILQ_EMPTY(&ifnet) || addr->sin6_family != AF_INET6)
660 return (EADDRNOTAVAIL);
661 if ((error = sa6_embedscope(addr, ip6_use_defzone)) != 0)
662 return (error);
663
664 if (!IN6_IS_ADDR_UNSPECIFIED(&addr->sin6_addr) &&
665 (ia = ifa_ifwithaddr((struct sockaddr *)addr)) == 0)
666 return (EADDRNOTAVAIL);
667 if (ia &&
668 ((struct in6_ifaddr *)ia)->ia6_flags &
669 (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY|
670 IN6_IFF_DETACHED|IN6_IFF_DEPRECATED)) {
671 return (EADDRNOTAVAIL);
672 }
673 INP_INFO_WLOCK(&ripcbinfo);
674 INP_WLOCK(inp);
675 inp->in6p_laddr = addr->sin6_addr;
676 INP_WUNLOCK(inp);
677 INP_INFO_WUNLOCK(&ripcbinfo);
678 return (0);
679 }
680
681 static int
682 rip6_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
683 {
684 struct inpcb *inp;
685 struct sockaddr_in6 *addr = (struct sockaddr_in6 *)nam;
686 struct in6_addr in6a;
687 struct ifnet *ifp = NULL;
688 int error = 0, scope_ambiguous = 0;
689
690 inp = sotoinpcb(so);
691 KASSERT(inp != NULL, ("rip6_connect: inp == NULL"));
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
700 /*
701 * Application should provide a proper zone ID or the use of default
702 * zone IDs should be enabled. Unfortunately, some applications do
703 * not behave as it should, so we need a workaround. Even if an
704 * appropriate ID is not determined, we'll see if we can determine
705 * the outgoing interface. If we can, determine the zone ID based on
706 * the interface below.
707 */
708 if (addr->sin6_scope_id == 0 && !ip6_use_defzone)
709 scope_ambiguous = 1;
710 if ((error = sa6_embedscope(addr, ip6_use_defzone)) != 0)
711 return (error);
712
713 INP_INFO_WLOCK(&ripcbinfo);
714 INP_WLOCK(inp);
715 /* Source address selection. XXX: need pcblookup? */
716 error = in6_selectsrc(addr, inp->in6p_outputopts,
717 inp, NULL, so->so_cred, &ifp, &in6a);
718 if (error) {
719 INP_WUNLOCK(inp);
720 INP_INFO_WUNLOCK(&ripcbinfo);
721 return (error);
722 }
723
724 /* XXX: see above */
725 if (ifp && scope_ambiguous &&
726 (error = in6_setscope(&addr->sin6_addr, ifp, NULL)) != 0) {
727 INP_WUNLOCK(inp);
728 INP_INFO_WUNLOCK(&ripcbinfo);
729 return (error);
730 }
731 inp->in6p_faddr = addr->sin6_addr;
732 inp->in6p_laddr = in6a;
733 soisconnected(so);
734 INP_WUNLOCK(inp);
735 INP_INFO_WUNLOCK(&ripcbinfo);
736 return (0);
737 }
738
739 static int
740 rip6_shutdown(struct socket *so)
741 {
742 struct inpcb *inp;
743
744 inp = sotoinpcb(so);
745 KASSERT(inp != NULL, ("rip6_shutdown: inp == NULL"));
746
747 INP_WLOCK(inp);
748 socantsendmore(so);
749 INP_WUNLOCK(inp);
750 return (0);
751 }
752
753 static int
754 rip6_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
755 struct mbuf *control, struct thread *td)
756 {
757 struct inpcb *inp;
758 struct sockaddr_in6 tmp;
759 struct sockaddr_in6 *dst;
760 int ret;
761
762 inp = sotoinpcb(so);
763 KASSERT(inp != NULL, ("rip6_send: inp == NULL"));
764
765 /* Always copy sockaddr to avoid overwrites. */
766 /* Unlocked read. */
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 INP_RLOCK(inp);
777 bcopy(&inp->in6p_faddr, &tmp.sin6_addr,
778 sizeof(struct in6_addr));
779 INP_RUNLOCK(inp);
780 dst = &tmp;
781 } else {
782 if (nam == NULL) {
783 m_freem(m);
784 return (ENOTCONN);
785 }
786 if (nam->sa_len != sizeof(struct sockaddr_in6)) {
787 m_freem(m);
788 return (EINVAL);
789 }
790 tmp = *(struct sockaddr_in6 *)nam;
791 dst = &tmp;
792
793 if (dst->sin6_family == AF_UNSPEC) {
794 /*
795 * XXX: we allow this case for backward
796 * compatibility to buggy applications that
797 * rely on old (and wrong) kernel behavior.
798 */
799 log(LOG_INFO, "rip6 SEND: address family is "
800 "unspec. Assume AF_INET6\n");
801 dst->sin6_family = AF_INET6;
802 } else if (dst->sin6_family != AF_INET6) {
803 m_freem(m);
804 return(EAFNOSUPPORT);
805 }
806 }
807 ret = rip6_output(m, so, dst, control);
808 return (ret);
809 }
810
811 struct pr_usrreqs rip6_usrreqs = {
812 .pru_abort = rip6_abort,
813 .pru_attach = rip6_attach,
814 .pru_bind = rip6_bind,
815 .pru_connect = rip6_connect,
816 .pru_control = in6_control,
817 .pru_detach = rip6_detach,
818 .pru_disconnect = rip6_disconnect,
819 .pru_peeraddr = in6_getpeeraddr,
820 .pru_send = rip6_send,
821 .pru_shutdown = rip6_shutdown,
822 .pru_sockaddr = in6_getsockaddr,
823 .pru_close = rip6_close,
824 };
Cache object: ed9f93e05a2ff7976e1922b388d5a4cf
|