]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/netinet6/raw_ip6.c
This commit was generated by cvs2svn to compensate for changes in r150974,
[FreeBSD/FreeBSD.git] / sys / netinet6 / raw_ip6.c
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$
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 #include <sys/syslog.h>
79
80 #include <net/if.h>
81 #include <net/if_types.h>
82 #include <net/route.h>
83
84 #include <netinet/in.h>
85 #include <netinet/in_var.h>
86 #include <netinet/in_systm.h>
87 #include <netinet/icmp6.h>
88 #include <netinet/in_pcb.h>
89 #include <netinet/ip6.h>
90 #include <netinet6/ip6protosw.h>
91 #include <netinet6/ip6_mroute.h>
92 #include <netinet6/in6_pcb.h>
93 #include <netinet6/ip6_var.h>
94 #include <netinet6/nd6.h>
95 #include <netinet6/raw_ip6.h>
96 #include <netinet6/scope6_var.h>
97
98 #ifdef IPSEC
99 #include <netinet6/ipsec.h>
100 #include <netinet6/ipsec6.h>
101 #endif /*IPSEC*/
102
103 #ifdef FAST_IPSEC
104 #include <netipsec/ipsec.h>
105 #include <netipsec/ipsec6.h>
106 #endif /* FAST_IPSEC */
107
108 #include <machine/stdarg.h>
109
110 #define satosin6(sa)    ((struct sockaddr_in6 *)(sa))
111 #define ifatoia6(ifa)   ((struct in6_ifaddr *)(ifa))
112
113 /*
114  * Raw interface to IP6 protocol.
115  */
116
117 extern struct   inpcbhead ripcb;
118 extern struct   inpcbinfo ripcbinfo;
119 extern u_long   rip_sendspace;
120 extern u_long   rip_recvspace;
121
122 struct rip6stat rip6stat;
123
124 /*
125  * Setup generic address and protocol structures
126  * for raw_input routine, then pass them along with
127  * mbuf chain.
128  */
129 int
130 rip6_input(mp, offp, proto)
131         struct  mbuf **mp;
132         int     *offp, proto;
133 {
134         struct mbuf *m = *mp;
135         register struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
136         register struct inpcb *in6p;
137         struct inpcb *last = 0;
138         struct mbuf *opts = NULL;
139         struct sockaddr_in6 fromsa;
140
141         rip6stat.rip6s_ipackets++;
142
143         if (faithprefix_p != NULL && (*faithprefix_p)(&ip6->ip6_dst)) {
144                 /* XXX send icmp6 host/port unreach? */
145                 m_freem(m);
146                 return IPPROTO_DONE;
147         }
148
149         init_sin6(&fromsa, m); /* general init */
150
151         INP_INFO_RLOCK(&ripcbinfo);
152         LIST_FOREACH(in6p, &ripcb, inp_list) {
153                 INP_LOCK(in6p);
154                 if ((in6p->in6p_vflag & INP_IPV6) == 0) {
155 docontinue:
156                         INP_UNLOCK(in6p);
157                         continue;
158                 }
159                 if (in6p->in6p_ip6_nxt &&
160                     in6p->in6p_ip6_nxt != proto)
161                         goto docontinue;
162                 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) &&
163                     !IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &ip6->ip6_dst))
164                         goto docontinue;
165                 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr) &&
166                     !IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, &ip6->ip6_src))
167                         goto docontinue;
168                 if (in6p->in6p_cksum != -1) {
169                         rip6stat.rip6s_isum++;
170                         if (in6_cksum(m, ip6->ip6_nxt, *offp,
171                             m->m_pkthdr.len - *offp)) {
172                                 rip6stat.rip6s_badsum++;
173                                 goto docontinue;
174                         }
175                 }
176                 if (last) {
177                         struct mbuf *n = m_copy(m, 0, (int)M_COPYALL);
178
179 #if defined(IPSEC) || defined(FAST_IPSEC)
180                         /*
181                          * Check AH/ESP integrity.
182                          */
183                         if (n && ipsec6_in_reject(n, last)) {
184                                 m_freem(n);
185 #ifdef IPSEC
186                                 ipsec6stat.in_polvio++;
187 #endif /*IPSEC*/
188                                 /* do not inject data into pcb */
189                         } else
190 #endif /*IPSEC || FAST_IPSEC*/
191                         if (n) {
192                                 if (last->in6p_flags & IN6P_CONTROLOPTS ||
193                                     last->in6p_socket->so_options & SO_TIMESTAMP)
194                                         ip6_savecontrol(last, n, &opts);
195                                 /* strip intermediate headers */
196                                 m_adj(n, *offp);
197                                 if (sbappendaddr(&last->in6p_socket->so_rcv,
198                                                 (struct sockaddr *)&fromsa,
199                                                  n, opts) == 0) {
200                                         m_freem(n);
201                                         if (opts)
202                                                 m_freem(opts);
203                                         rip6stat.rip6s_fullsock++;
204                                 } else
205                                         sorwakeup(last->in6p_socket);
206                                 opts = NULL;
207                         }
208                         INP_UNLOCK(last);
209                 }
210                 last = in6p;
211         }
212 #if defined(IPSEC) || defined(FAST_IPSEC)
213         /*
214          * Check AH/ESP integrity.
215          */
216         if (last && ipsec6_in_reject(m, last)) {
217                 m_freem(m);
218 #ifdef IPSEC
219                 ipsec6stat.in_polvio++;
220 #endif /*IPSEC*/
221                 ip6stat.ip6s_delivered--;
222                 /* do not inject data into pcb */
223                 INP_UNLOCK(last);
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, *optp;
328         struct ifnet *oifp = NULL;
329         int type = 0, code = 0;         /* for ICMPv6 output statistics only */
330         int priv = 0;
331         int scope_ambiguous = 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
344         priv = 0;
345         if (so->so_cred->cr_uid == 0)
346                 priv = 1;
347         dst = &dstsock->sin6_addr;
348         if (control) {
349                 if ((error = ip6_setpktopts(control, &opt,
350                     in6p->in6p_outputopts, priv, so->so_proto->pr_protocol))
351                     != 0) {
352                         goto bad;
353                 }
354                 optp = &opt;
355         } else
356                 optp = in6p->in6p_outputopts;
357
358         /*
359          * Check and convert scope zone ID into internal form.
360          * XXX: we may still need to determine the zone later.
361          */
362         if (!(so->so_state & SS_ISCONNECTED)) {
363                 if (dstsock->sin6_scope_id == 0 && !ip6_use_defzone)
364                         scope_ambiguous = 1;
365                 if ((error = sa6_embedscope(dstsock, ip6_use_defzone)) != 0)
366                         goto bad;
367         }
368
369         /*
370          * For an ICMPv6 packet, we should know its type and code
371          * to update statistics.
372          */
373         if (so->so_proto->pr_protocol == IPPROTO_ICMPV6) {
374                 struct icmp6_hdr *icmp6;
375                 if (m->m_len < sizeof(struct icmp6_hdr) &&
376                     (m = m_pullup(m, sizeof(struct icmp6_hdr))) == NULL) {
377                         error = ENOBUFS;
378                         goto bad;
379                 }
380                 icmp6 = mtod(m, struct icmp6_hdr *);
381                 type = icmp6->icmp6_type;
382                 code = icmp6->icmp6_code;
383         }
384
385         M_PREPEND(m, sizeof(*ip6), M_DONTWAIT);
386         if (m == NULL) {
387                 error = ENOBUFS;
388                 goto bad;
389         }
390         ip6 = mtod(m, struct ip6_hdr *);
391
392         /*
393          * Source address selection.
394          */
395         if ((in6a = in6_selectsrc(dstsock, optp, in6p->in6p_moptions, NULL,
396             &in6p->in6p_laddr, &oifp, &error)) == NULL) {
397                 if (error == 0)
398                         error = EADDRNOTAVAIL;
399                 goto bad;
400         }
401         ip6->ip6_src = *in6a;
402
403         if (oifp && scope_ambiguous) {
404                 /*
405                  * Application should provide a proper zone ID or the use of
406                  * default zone IDs should be enabled.  Unfortunately, some
407                  * applications do not behave as it should, so we need a
408                  * workaround.  Even if an appropriate ID is not determined
409                  * (when it's required), if we can determine the outgoing
410                  * interface. determine the zone ID based on the interface.
411                  */
412                 error = in6_setscope(&dstsock->sin6_addr, oifp, NULL);
413                 if (error != 0)
414                         goto bad;
415         }
416         ip6->ip6_dst = dstsock->sin6_addr;
417
418         /* fill in the rest of the IPv6 header fields */
419         ip6->ip6_flow = (ip6->ip6_flow & ~IPV6_FLOWINFO_MASK) |
420                 (in6p->in6p_flowinfo & IPV6_FLOWINFO_MASK);
421         ip6->ip6_vfc = (ip6->ip6_vfc & ~IPV6_VERSION_MASK) |
422                 (IPV6_VERSION & IPV6_VERSION_MASK);
423         /* ip6_plen will be filled in ip6_output, so not fill it here. */
424         ip6->ip6_nxt = in6p->in6p_ip6_nxt;
425         ip6->ip6_hlim = in6_selecthlim(in6p, oifp);
426
427         if (so->so_proto->pr_protocol == IPPROTO_ICMPV6 ||
428             in6p->in6p_cksum != -1) {
429                 struct mbuf *n;
430                 int off;
431                 u_int16_t *p;
432
433                 /* compute checksum */
434                 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6)
435                         off = offsetof(struct icmp6_hdr, icmp6_cksum);
436                 else
437                         off = in6p->in6p_cksum;
438                 if (plen < off + 1) {
439                         error = EINVAL;
440                         goto bad;
441                 }
442                 off += sizeof(struct ip6_hdr);
443
444                 n = m;
445                 while (n && n->m_len <= off) {
446                         off -= n->m_len;
447                         n = n->m_next;
448                 }
449                 if (!n)
450                         goto bad;
451                 p = (u_int16_t *)(mtod(n, caddr_t) + off);
452                 *p = 0;
453                 *p = in6_cksum(m, ip6->ip6_nxt, sizeof(*ip6), plen);
454         }
455
456         error = ip6_output(m, optp, NULL, 0, in6p->in6p_moptions, &oifp, in6p);
457         if (so->so_proto->pr_protocol == IPPROTO_ICMPV6) {
458                 if (oifp)
459                         icmp6_ifoutstat_inc(oifp, type, code);
460                 icmp6stat.icp6s_outhist[type]++;
461         } else
462                 rip6stat.rip6s_opackets++;
463
464         goto freectl;
465
466  bad:
467         if (m)
468                 m_freem(m);
469
470  freectl:
471         if (control) {
472                 ip6_clearpktopts(&opt, -1);
473                 m_freem(control);
474         }
475         INP_UNLOCK(in6p);
476         return (error);
477 }
478
479 /*
480  * Raw IPv6 socket option processing.
481  */
482 int
483 rip6_ctloutput(so, sopt)
484         struct socket *so;
485         struct sockopt *sopt;
486 {
487         int error;
488
489         if (sopt->sopt_level == IPPROTO_ICMPV6)
490                 /*
491                  * XXX: is it better to call icmp6_ctloutput() directly
492                  * from protosw?
493                  */
494                 return (icmp6_ctloutput(so, sopt));
495         else if (sopt->sopt_level != IPPROTO_IPV6)
496                 return (EINVAL);
497
498         error = 0;
499
500         switch (sopt->sopt_dir) {
501         case SOPT_GET:
502                 switch (sopt->sopt_name) {
503                 case MRT6_INIT:
504                 case MRT6_DONE:
505                 case MRT6_ADD_MIF:
506                 case MRT6_DEL_MIF:
507                 case MRT6_ADD_MFC:
508                 case MRT6_DEL_MFC:
509                 case MRT6_PIM:
510                         error = ip6_mrouter_get(so, sopt);
511                         break;
512                 case IPV6_CHECKSUM:
513                         error = ip6_raw_ctloutput(so, sopt);
514                         break;
515                 default:
516                         error = ip6_ctloutput(so, sopt);
517                         break;
518                 }
519                 break;
520
521         case SOPT_SET:
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_set(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
542         return (error);
543 }
544
545 static int
546 rip6_attach(struct socket *so, int proto, struct thread *td)
547 {
548         struct inpcb *inp;
549         struct icmp6_filter *filter;
550         int error, s;
551
552         INP_INFO_WLOCK(&ripcbinfo);
553         inp = sotoinpcb(so);
554         if (inp) {
555                 INP_INFO_WUNLOCK(&ripcbinfo);
556                 panic("rip6_attach");
557         }
558         if (td && (error = suser(td)) != 0) {
559                 INP_INFO_WUNLOCK(&ripcbinfo);
560                 return error;
561         }
562         error = soreserve(so, rip_sendspace, rip_recvspace);
563         if (error) {
564                 INP_INFO_WUNLOCK(&ripcbinfo);
565                 return error;
566         }
567         MALLOC(filter, struct icmp6_filter *,
568                sizeof(struct icmp6_filter), M_PCB, M_NOWAIT);
569         if (filter == NULL)
570                 return ENOMEM;
571         s = splnet();
572         error = in_pcballoc(so, &ripcbinfo, "raw6inp");
573         splx(s);
574         if (error) {
575                 INP_INFO_WUNLOCK(&ripcbinfo);
576                 FREE(filter, M_PCB);
577                 return error;
578         }
579         inp = (struct inpcb *)so->so_pcb;
580         INP_LOCK(inp);
581         INP_INFO_WUNLOCK(&ripcbinfo);
582         inp->inp_vflag |= INP_IPV6;
583         inp->in6p_ip6_nxt = (long)proto;
584         inp->in6p_hops = -1;    /* use kernel default */
585         inp->in6p_cksum = -1;
586         inp->in6p_icmp6filt = filter;
587         ICMP6_FILTER_SETPASSALL(inp->in6p_icmp6filt);
588         INP_UNLOCK(inp);
589         return 0;
590 }
591
592 static int
593 rip6_detach(struct socket *so)
594 {
595         struct inpcb *inp;
596
597         INP_INFO_WLOCK(&ripcbinfo);
598         inp = sotoinpcb(so);
599         if (inp == 0) {
600                 INP_INFO_WUNLOCK(&ripcbinfo);
601                 panic("rip6_detach");
602         }
603         /* xxx: RSVP */
604         if (so == ip6_mrouter)
605                 ip6_mrouter_done();
606         if (inp->in6p_icmp6filt) {
607                 FREE(inp->in6p_icmp6filt, M_PCB);
608                 inp->in6p_icmp6filt = NULL;
609         }
610         INP_LOCK(inp);
611         in6_pcbdetach(inp);
612         INP_INFO_WUNLOCK(&ripcbinfo);
613         return 0;
614 }
615
616 static int
617 rip6_abort(struct socket *so)
618 {
619         soisdisconnected(so);
620         return rip6_detach(so);
621 }
622
623 static int
624 rip6_disconnect(struct socket *so)
625 {
626         struct inpcb *inp = sotoinpcb(so);
627
628         if ((so->so_state & SS_ISCONNECTED) == 0)
629                 return ENOTCONN;
630         inp->in6p_faddr = in6addr_any;
631         return rip6_abort(so);
632 }
633
634 static int
635 rip6_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
636 {
637         struct inpcb *inp = sotoinpcb(so);
638         struct sockaddr_in6 *addr = (struct sockaddr_in6 *)nam;
639         struct ifaddr *ia = NULL;
640         int error = 0;
641
642         if (nam->sa_len != sizeof(*addr))
643                 return EINVAL;
644         if (TAILQ_EMPTY(&ifnet) || addr->sin6_family != AF_INET6)
645                 return EADDRNOTAVAIL;
646         if ((error = sa6_embedscope(addr, ip6_use_defzone)) != 0)
647                 return(error);
648
649         if (!IN6_IS_ADDR_UNSPECIFIED(&addr->sin6_addr) &&
650             (ia = ifa_ifwithaddr((struct sockaddr *)addr)) == 0)
651                 return EADDRNOTAVAIL;
652         if (ia &&
653             ((struct in6_ifaddr *)ia)->ia6_flags &
654             (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY|
655              IN6_IFF_DETACHED|IN6_IFF_DEPRECATED)) {
656                 return (EADDRNOTAVAIL);
657         }
658         INP_INFO_WLOCK(&ripcbinfo);
659         INP_LOCK(inp);
660         inp->in6p_laddr = addr->sin6_addr;
661         INP_UNLOCK(inp);
662         INP_INFO_WUNLOCK(&ripcbinfo);
663         return 0;
664 }
665
666 static int
667 rip6_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
668 {
669         struct inpcb *inp = sotoinpcb(so);
670         struct sockaddr_in6 *addr = (struct sockaddr_in6 *)nam;
671         struct in6_addr *in6a = NULL;
672         struct ifnet *ifp = NULL;
673         int error = 0, scope_ambiguous = 0;
674
675         if (nam->sa_len != sizeof(*addr))
676                 return EINVAL;
677         if (TAILQ_EMPTY(&ifnet))
678                 return EADDRNOTAVAIL;
679         if (addr->sin6_family != AF_INET6)
680                 return EAFNOSUPPORT;
681
682         /*
683          * Application should provide a proper zone ID or the use of
684          * default zone IDs should be enabled.  Unfortunately, some
685          * applications do not behave as it should, so we need a
686          * workaround.  Even if an appropriate ID is not determined,
687          * we'll see if we can determine the outgoing interface.  If we
688          * can, determine the zone ID based on the interface below.
689          */
690         if (addr->sin6_scope_id == 0 && !ip6_use_defzone)
691                 scope_ambiguous = 1;
692         if ((error = sa6_embedscope(addr, ip6_use_defzone)) != 0)
693                 return(error);
694
695         INP_INFO_WLOCK(&ripcbinfo);
696         INP_LOCK(inp);
697         /* Source address selection. XXX: need pcblookup? */
698         in6a = in6_selectsrc(addr, inp->in6p_outputopts,
699                              inp->in6p_moptions, NULL,
700                              &inp->in6p_laddr, &ifp, &error);
701         if (in6a == NULL) {
702                 INP_UNLOCK(inp);
703                 INP_INFO_WUNLOCK(&ripcbinfo);
704                 return (error ? error : EADDRNOTAVAIL);
705         }
706
707         /* XXX: see above */
708         if (ifp && scope_ambiguous &&
709             (error = in6_setscope(&addr->sin6_addr, ifp, NULL)) != 0) {
710                 INP_UNLOCK(inp);
711                 INP_INFO_WUNLOCK(&ripcbinfo);
712                 return(error);
713         }
714         inp->in6p_faddr = addr->sin6_addr;
715         inp->in6p_laddr = *in6a;
716         soisconnected(so);
717         INP_UNLOCK(inp);
718         INP_INFO_WUNLOCK(&ripcbinfo);
719         return 0;
720 }
721
722 static int
723 rip6_shutdown(struct socket *so)
724 {
725         struct inpcb *inp;
726
727         INP_INFO_RLOCK(&ripcbinfo);
728         inp = sotoinpcb(so);
729         INP_LOCK(inp);
730         INP_INFO_RUNLOCK(&ripcbinfo);
731         socantsendmore(so);
732         INP_UNLOCK(inp);
733         return 0;
734 }
735
736 static int
737 rip6_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
738          struct mbuf *control, struct thread *td)
739 {
740         struct inpcb *inp = sotoinpcb(so);
741         struct sockaddr_in6 tmp;
742         struct sockaddr_in6 *dst;
743         int ret;
744
745         INP_INFO_WLOCK(&ripcbinfo);
746         /* always copy sockaddr to avoid overwrites */
747         /* Unlocked read. */
748         if (so->so_state & SS_ISCONNECTED) {
749                 if (nam) {
750                         INP_INFO_WUNLOCK(&ripcbinfo);
751                         m_freem(m);
752                         return EISCONN;
753                 }
754                 /* XXX */
755                 bzero(&tmp, sizeof(tmp));
756                 tmp.sin6_family = AF_INET6;
757                 tmp.sin6_len = sizeof(struct sockaddr_in6);
758                 bcopy(&inp->in6p_faddr, &tmp.sin6_addr,
759                       sizeof(struct in6_addr));
760                 dst = &tmp;
761         } else {
762                 if (nam == NULL) {
763                         INP_INFO_WUNLOCK(&ripcbinfo);
764                         m_freem(m);
765                         return ENOTCONN;
766                 }
767                 if (nam->sa_len != sizeof(struct sockaddr_in6)) {
768                         INP_INFO_WUNLOCK(&ripcbinfo);
769                         m_freem(m);
770                         return(EINVAL);
771                 }
772                 tmp = *(struct sockaddr_in6 *)nam;
773                 dst = &tmp;
774
775                 if (dst->sin6_family == AF_UNSPEC) {
776                         /*
777                          * XXX: we allow this case for backward
778                          * compatibility to buggy applications that
779                          * rely on old (and wrong) kernel behavior.
780                          */
781                         log(LOG_INFO, "rip6 SEND: address family is "
782                             "unspec. Assume AF_INET6\n");
783                         dst->sin6_family = AF_INET6;
784                 } else if (dst->sin6_family != AF_INET6) {
785                         INP_INFO_WUNLOCK(&ripcbinfo);
786                         m_freem(m);
787                         return(EAFNOSUPPORT);
788                 }
789         }
790         ret = rip6_output(m, so, dst, control);
791         INP_INFO_WUNLOCK(&ripcbinfo);
792         return (ret);
793 }
794
795 struct pr_usrreqs rip6_usrreqs = {
796         .pru_abort =            rip6_abort,
797         .pru_attach =           rip6_attach,
798         .pru_bind =             rip6_bind,
799         .pru_connect =          rip6_connect,
800         .pru_control =          in6_control,
801         .pru_detach =           rip6_detach,
802         .pru_disconnect =       rip6_disconnect,
803         .pru_peeraddr =         in6_setpeeraddr,
804         .pru_send =             rip6_send,
805         .pru_shutdown =         rip6_shutdown,
806         .pru_sockaddr =         in6_setsockaddr,
807 };