]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/netinet6/raw_ip6.c
OpenSSL: Merge OpenSSL 1.1.1j
[FreeBSD/FreeBSD.git] / sys / netinet6 / raw_ip6.c
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the project nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31
32 /*-
33  * Copyright (c) 1982, 1986, 1988, 1993
34  *      The Regents of the University of California.
35  * All rights reserved.
36  *
37  * Redistribution and use in source and binary forms, with or without
38  * modification, are permitted provided that the following conditions
39  * are met:
40  * 1. Redistributions of source code must retain the above copyright
41  *    notice, this list of conditions and the following disclaimer.
42  * 2. Redistributions in binary form must reproduce the above copyright
43  *    notice, this list of conditions and the following disclaimer in the
44  *    documentation and/or other materials provided with the distribution.
45  * 3. Neither the name of the University nor the names of its contributors
46  *    may be used to endorse or promote products derived from this software
47  *    without specific prior written permission.
48  *
49  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59  * SUCH DAMAGE.
60  *
61  *      @(#)raw_ip.c    8.2 (Berkeley) 1/4/94
62  */
63
64 #include <sys/cdefs.h>
65 __FBSDID("$FreeBSD$");
66
67 #include "opt_ipsec.h"
68 #include "opt_inet6.h"
69 #include "opt_route.h"
70
71 #include <sys/param.h>
72 #include <sys/errno.h>
73 #include <sys/jail.h>
74 #include <sys/kernel.h>
75 #include <sys/lock.h>
76 #include <sys/malloc.h>
77 #include <sys/mbuf.h>
78 #include <sys/priv.h>
79 #include <sys/proc.h>
80 #include <sys/protosw.h>
81 #include <sys/signalvar.h>
82 #include <sys/socket.h>
83 #include <sys/socketvar.h>
84 #include <sys/sx.h>
85 #include <sys/syslog.h>
86
87 #include <net/if.h>
88 #include <net/if_var.h>
89 #include <net/if_types.h>
90 #include <net/route.h>
91 #include <net/vnet.h>
92
93 #include <netinet/in.h>
94 #include <netinet/in_var.h>
95 #include <netinet/in_systm.h>
96 #include <netinet/in_pcb.h>
97
98 #include <netinet/icmp6.h>
99 #include <netinet/ip6.h>
100 #include <netinet/ip_var.h>
101 #include <netinet6/ip6protosw.h>
102 #include <netinet6/ip6_mroute.h>
103 #include <netinet6/in6_pcb.h>
104 #include <netinet6/ip6_var.h>
105 #include <netinet6/nd6.h>
106 #include <netinet6/raw_ip6.h>
107 #include <netinet6/in6_fib.h>
108 #include <netinet6/scope6_var.h>
109 #include <netinet6/send.h>
110
111 #include <netipsec/ipsec_support.h>
112
113 #include <machine/stdarg.h>
114
115 #define satosin6(sa)    ((struct sockaddr_in6 *)(sa))
116 #define ifatoia6(ifa)   ((struct in6_ifaddr *)(ifa))
117
118 /*
119  * Raw interface to IP6 protocol.
120  */
121
122 VNET_DECLARE(struct inpcbhead, ripcb);
123 VNET_DECLARE(struct inpcbinfo, ripcbinfo);
124 #define V_ripcb                         VNET(ripcb)
125 #define V_ripcbinfo                     VNET(ripcbinfo)
126
127 extern u_long   rip_sendspace;
128 extern u_long   rip_recvspace;
129
130 VNET_PCPUSTAT_DEFINE(struct rip6stat, rip6stat);
131 VNET_PCPUSTAT_SYSINIT(rip6stat);
132
133 #ifdef VIMAGE
134 VNET_PCPUSTAT_SYSUNINIT(rip6stat);
135 #endif /* VIMAGE */
136
137 /*
138  * Hooks for multicast routing. They all default to NULL, so leave them not
139  * initialized and rely on BSS being set to 0.
140  */
141
142 /*
143  * The socket used to communicate with the multicast routing daemon.
144  */
145 VNET_DEFINE(struct socket *, ip6_mrouter);
146
147 /*
148  * The various mrouter functions.
149  */
150 int (*ip6_mrouter_set)(struct socket *, struct sockopt *);
151 int (*ip6_mrouter_get)(struct socket *, struct sockopt *);
152 int (*ip6_mrouter_done)(void);
153 int (*ip6_mforward)(struct ip6_hdr *, struct ifnet *, struct mbuf *);
154 int (*mrt6_ioctl)(u_long, caddr_t);
155
156 /*
157  * Setup generic address and protocol structures for raw_input routine, then
158  * pass them along with mbuf chain.
159  */
160 int
161 rip6_input(struct mbuf **mp, int *offp, int proto)
162 {
163         struct ifnet *ifp;
164         struct mbuf *m = *mp;
165         struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
166         struct inpcb *inp;
167         struct inpcb *last = NULL;
168         struct mbuf *opts = NULL;
169         struct sockaddr_in6 fromsa;
170
171         NET_EPOCH_ASSERT();
172
173         RIP6STAT_INC(rip6s_ipackets);
174
175         init_sin6(&fromsa, m, 0); /* general init */
176
177         ifp = m->m_pkthdr.rcvif;
178
179         CK_LIST_FOREACH(inp, &V_ripcb, inp_list) {
180                 /* XXX inp locking */
181                 if ((inp->inp_vflag & INP_IPV6) == 0)
182                         continue;
183                 if (inp->inp_ip_p &&
184                     inp->inp_ip_p != proto)
185                         continue;
186                 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr) &&
187                     !IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, &ip6->ip6_dst))
188                         continue;
189                 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) &&
190                     !IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, &ip6->ip6_src))
191                         continue;
192                 if (last != NULL) {
193                         struct mbuf *n = m_copym(m, 0, M_COPYALL, M_NOWAIT);
194
195 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
196                         /*
197                          * Check AH/ESP integrity.
198                          */
199                         if (IPSEC_ENABLED(ipv6)) {
200                                 if (n != NULL &&
201                                     IPSEC_CHECK_POLICY(ipv6, n, last) != 0) {
202                                         m_freem(n);
203                                         /* Do not inject data into pcb. */
204                                         n = NULL;
205                                 }
206                         }
207 #endif /* IPSEC */
208                         if (n) {
209                                 if (last->inp_flags & INP_CONTROLOPTS ||
210                                     last->inp_socket->so_options & SO_TIMESTAMP)
211                                         ip6_savecontrol(last, n, &opts);
212                                 /* strip intermediate headers */
213                                 m_adj(n, *offp);
214                                 if (sbappendaddr(&last->inp_socket->so_rcv,
215                                                 (struct sockaddr *)&fromsa,
216                                                  n, opts) == 0) {
217                                         m_freem(n);
218                                         if (opts)
219                                                 m_freem(opts);
220                                         RIP6STAT_INC(rip6s_fullsock);
221                                 } else
222                                         sorwakeup(last->inp_socket);
223                                 opts = NULL;
224                         }
225                         INP_RUNLOCK(last);
226                         last = NULL;
227                 }
228                 INP_RLOCK(inp);
229                 if (__predict_false(inp->inp_flags2 & INP_FREED))
230                         goto skip_2;
231                 if (jailed_without_vnet(inp->inp_cred)) {
232                         /*
233                          * Allow raw socket in jail to receive multicast;
234                          * assume process had PRIV_NETINET_RAW at attach,
235                          * and fall through into normal filter path if so.
236                          */
237                         if (!IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) &&
238                             prison_check_ip6(inp->inp_cred,
239                             &ip6->ip6_dst) != 0)
240                                 goto skip_2;
241                 }
242                 if (inp->in6p_cksum != -1) {
243                         RIP6STAT_INC(rip6s_isum);
244                         if (m->m_pkthdr.len - (*offp + inp->in6p_cksum) < 2 ||
245                             in6_cksum(m, proto, *offp,
246                             m->m_pkthdr.len - *offp)) {
247                                 RIP6STAT_INC(rip6s_badsum);
248                                 /*
249                                  * Drop the received message, don't send an
250                                  * ICMP6 message. Set proto to IPPROTO_NONE
251                                  * to achieve that.
252                                  */
253                                 proto = IPPROTO_NONE;
254                                 goto skip_2;
255                         }
256                 }
257                 /*
258                  * If this raw socket has multicast state, and we
259                  * have received a multicast, check if this socket
260                  * should receive it, as multicast filtering is now
261                  * the responsibility of the transport layer.
262                  */
263                 if (inp->in6p_moptions &&
264                     IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
265                         /*
266                          * If the incoming datagram is for MLD, allow it
267                          * through unconditionally to the raw socket.
268                          *
269                          * Use the M_RTALERT_MLD flag to check for MLD
270                          * traffic without having to inspect the mbuf chain
271                          * more deeply, as all MLDv1/v2 host messages MUST
272                          * contain the Router Alert option.
273                          *
274                          * In the case of MLDv1, we may not have explicitly
275                          * joined the group, and may have set IFF_ALLMULTI
276                          * on the interface. im6o_mc_filter() may discard
277                          * control traffic we actually need to see.
278                          *
279                          * Userland multicast routing daemons should continue
280                          * filter the control traffic appropriately.
281                          */
282                         int blocked;
283
284                         blocked = MCAST_PASS;
285                         if ((m->m_flags & M_RTALERT_MLD) == 0) {
286                                 struct sockaddr_in6 mcaddr;
287
288                                 bzero(&mcaddr, sizeof(struct sockaddr_in6));
289                                 mcaddr.sin6_len = sizeof(struct sockaddr_in6);
290                                 mcaddr.sin6_family = AF_INET6;
291                                 mcaddr.sin6_addr = ip6->ip6_dst;
292
293                                 blocked = im6o_mc_filter(inp->in6p_moptions,
294                                     ifp,
295                                     (struct sockaddr *)&mcaddr,
296                                     (struct sockaddr *)&fromsa);
297                         }
298                         if (blocked != MCAST_PASS) {
299                                 IP6STAT_INC(ip6s_notmember);
300                                 goto skip_2;
301                         }
302                 }
303                 last = inp;
304                 continue;
305 skip_2:
306                 INP_RUNLOCK(inp);
307         }
308 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
309         /*
310          * Check AH/ESP integrity.
311          */
312         if (IPSEC_ENABLED(ipv6) && last != NULL &&
313             IPSEC_CHECK_POLICY(ipv6, m, last) != 0) {
314                 m_freem(m);
315                 IP6STAT_DEC(ip6s_delivered);
316                 /* Do not inject data into pcb. */
317                 INP_RUNLOCK(last);
318         } else
319 #endif /* IPSEC */
320         if (last != NULL) {
321                 if (last->inp_flags & INP_CONTROLOPTS ||
322                     last->inp_socket->so_options & SO_TIMESTAMP)
323                         ip6_savecontrol(last, m, &opts);
324                 /* Strip intermediate headers. */
325                 m_adj(m, *offp);
326                 if (sbappendaddr(&last->inp_socket->so_rcv,
327                     (struct sockaddr *)&fromsa, m, opts) == 0) {
328                         m_freem(m);
329                         if (opts)
330                                 m_freem(opts);
331                         RIP6STAT_INC(rip6s_fullsock);
332                 } else
333                         sorwakeup(last->inp_socket);
334                 INP_RUNLOCK(last);
335         } else {
336                 RIP6STAT_INC(rip6s_nosock);
337                 if (m->m_flags & M_MCAST)
338                         RIP6STAT_INC(rip6s_nosockmcast);
339                 if (proto == IPPROTO_NONE)
340                         m_freem(m);
341                 else
342                         icmp6_error(m, ICMP6_PARAM_PROB,
343                             ICMP6_PARAMPROB_NEXTHEADER,
344                             ip6_get_prevhdr(m, *offp));
345                 IP6STAT_DEC(ip6s_delivered);
346         }
347         return (IPPROTO_DONE);
348 }
349
350 void
351 rip6_ctlinput(int cmd, struct sockaddr *sa, void *d)
352 {
353         struct ip6ctlparam *ip6cp = NULL;
354         const struct sockaddr_in6 *sa6_src = NULL;
355         void *cmdarg;
356         struct inpcb *(*notify)(struct inpcb *, int) = in6_rtchange;
357
358         if (sa->sa_family != AF_INET6 ||
359             sa->sa_len != sizeof(struct sockaddr_in6))
360                 return;
361
362         if ((unsigned)cmd >= PRC_NCMDS)
363                 return;
364         if (PRC_IS_REDIRECT(cmd))
365                 notify = in6_rtchange, d = NULL;
366         else if (cmd == PRC_HOSTDEAD)
367                 d = NULL;
368         else if (inet6ctlerrmap[cmd] == 0)
369                 return;
370
371         /*
372          * If the parameter is from icmp6, decode it.
373          */
374         if (d != NULL) {
375                 ip6cp = (struct ip6ctlparam *)d;
376                 cmdarg = ip6cp->ip6c_cmdarg;
377                 sa6_src = ip6cp->ip6c_src;
378         } else {
379                 cmdarg = NULL;
380                 sa6_src = &sa6_any;
381         }
382
383         (void) in6_pcbnotify(&V_ripcbinfo, sa, 0,
384             (const struct sockaddr *)sa6_src, 0, cmd, cmdarg, notify);
385 }
386
387 /*
388  * Generate IPv6 header and pass packet to ip6_output.  Tack on options user
389  * may have setup with control call.
390  */
391 int
392 rip6_output(struct mbuf *m, struct socket *so, ...)
393 {
394         struct epoch_tracker et;
395         struct mbuf *control;
396         struct m_tag *mtag;
397         struct sockaddr_in6 *dstsock;
398         struct ip6_hdr *ip6;
399         struct inpcb *inp;
400         u_int   plen = m->m_pkthdr.len;
401         int error = 0;
402         struct ip6_pktopts opt, *optp;
403         struct ifnet *oifp = NULL;
404         int type = 0, code = 0;         /* for ICMPv6 output statistics only */
405         int scope_ambiguous = 0;
406         int use_defzone = 0;
407         int hlim = 0;
408         struct in6_addr in6a;
409         va_list ap;
410
411         va_start(ap, so);
412         dstsock = va_arg(ap, struct sockaddr_in6 *);
413         control = va_arg(ap, struct mbuf *);
414         va_end(ap);
415
416         inp = sotoinpcb(so);
417         INP_WLOCK(inp);
418
419         if (control != NULL) {
420                 if ((error = ip6_setpktopts(control, &opt,
421                     inp->in6p_outputopts, so->so_cred,
422                     so->so_proto->pr_protocol)) != 0) {
423                         goto bad;
424                 }
425                 optp = &opt;
426         } else
427                 optp = inp->in6p_outputopts;
428
429         /*
430          * Check and convert scope zone ID into internal form.
431          *
432          * XXX: we may still need to determine the zone later.
433          */
434         if (!(so->so_state & SS_ISCONNECTED)) {
435                 if (!optp || !optp->ip6po_pktinfo ||
436                     !optp->ip6po_pktinfo->ipi6_ifindex)
437                         use_defzone = V_ip6_use_defzone;
438                 if (dstsock->sin6_scope_id == 0 && !use_defzone)
439                         scope_ambiguous = 1;
440                 if ((error = sa6_embedscope(dstsock, use_defzone)) != 0)
441                         goto bad;
442         }
443
444         /*
445          * For an ICMPv6 packet, we should know its type and code to update
446          * statistics.
447          */
448         if (so->so_proto->pr_protocol == IPPROTO_ICMPV6) {
449                 struct icmp6_hdr *icmp6;
450                 if (m->m_len < sizeof(struct icmp6_hdr) &&
451                     (m = m_pullup(m, sizeof(struct icmp6_hdr))) == NULL) {
452                         error = ENOBUFS;
453                         goto bad;
454                 }
455                 icmp6 = mtod(m, struct icmp6_hdr *);
456                 type = icmp6->icmp6_type;
457                 code = icmp6->icmp6_code;
458         }
459
460         M_PREPEND(m, sizeof(*ip6), M_NOWAIT);
461         if (m == NULL) {
462                 error = ENOBUFS;
463                 goto bad;
464         }
465         ip6 = mtod(m, struct ip6_hdr *);
466
467 #ifdef ROUTE_MPATH
468         if (CALC_FLOWID_OUTBOUND) {
469                 uint32_t hash_type, hash_val;
470
471                 hash_val = fib6_calc_software_hash(&inp->in6p_laddr,
472                     &dstsock->sin6_addr, 0, 0, so->so_proto->pr_protocol,
473                     &hash_type);
474                 inp->inp_flowid = hash_val;
475                 inp->inp_flowtype = hash_type;
476         }
477 #endif
478         /*
479          * Source address selection.
480          */
481         NET_EPOCH_ENTER(et);
482         error = in6_selectsrc_socket(dstsock, optp, inp, so->so_cred,
483             scope_ambiguous, &in6a, &hlim);
484         NET_EPOCH_EXIT(et);
485
486         if (error)
487                 goto bad;
488         error = prison_check_ip6(inp->inp_cred, &in6a);
489         if (error != 0)
490                 goto bad;
491         ip6->ip6_src = in6a;
492
493         ip6->ip6_dst = dstsock->sin6_addr;
494
495         /*
496          * Fill in the rest of the IPv6 header fields.
497          */
498         ip6->ip6_flow = (ip6->ip6_flow & ~IPV6_FLOWINFO_MASK) |
499             (inp->inp_flow & IPV6_FLOWINFO_MASK);
500         ip6->ip6_vfc = (ip6->ip6_vfc & ~IPV6_VERSION_MASK) |
501             (IPV6_VERSION & IPV6_VERSION_MASK);
502
503         /*
504          * ip6_plen will be filled in ip6_output, so not fill it here.
505          */
506         ip6->ip6_nxt = inp->inp_ip_p;
507         ip6->ip6_hlim = hlim;
508
509         if (so->so_proto->pr_protocol == IPPROTO_ICMPV6 ||
510             inp->in6p_cksum != -1) {
511                 struct mbuf *n;
512                 int off;
513                 u_int16_t *p;
514
515                 /* Compute checksum. */
516                 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6)
517                         off = offsetof(struct icmp6_hdr, icmp6_cksum);
518                 else
519                         off = inp->in6p_cksum;
520                 if (plen < off + 2) {
521                         error = EINVAL;
522                         goto bad;
523                 }
524                 off += sizeof(struct ip6_hdr);
525
526                 n = m;
527                 while (n && n->m_len <= off) {
528                         off -= n->m_len;
529                         n = n->m_next;
530                 }
531                 if (!n)
532                         goto bad;
533                 p = (u_int16_t *)(mtod(n, caddr_t) + off);
534                 *p = 0;
535                 *p = in6_cksum(m, ip6->ip6_nxt, sizeof(*ip6), plen);
536         }
537
538         /*
539          * Send RA/RS messages to user land for protection, before sending
540          * them to rtadvd/rtsol.
541          */
542         if ((send_sendso_input_hook != NULL) &&
543             so->so_proto->pr_protocol == IPPROTO_ICMPV6) {
544                 switch (type) {
545                 case ND_ROUTER_ADVERT:
546                 case ND_ROUTER_SOLICIT:
547                         mtag = m_tag_get(PACKET_TAG_ND_OUTGOING,
548                                 sizeof(unsigned short), M_NOWAIT);
549                         if (mtag == NULL)
550                                 goto bad;
551                         m_tag_prepend(m, mtag);
552                 }
553         }
554
555         NET_EPOCH_ENTER(et);
556         error = ip6_output(m, optp, NULL, 0, inp->in6p_moptions, &oifp, inp);
557         NET_EPOCH_EXIT(et);
558         if (so->so_proto->pr_protocol == IPPROTO_ICMPV6) {
559                 if (oifp)
560                         icmp6_ifoutstat_inc(oifp, type, code);
561                 ICMP6STAT_INC(icp6s_outhist[type]);
562         } else
563                 RIP6STAT_INC(rip6s_opackets);
564
565         goto freectl;
566
567  bad:
568         if (m)
569                 m_freem(m);
570
571  freectl:
572         if (control != NULL) {
573                 ip6_clearpktopts(&opt, -1);
574                 m_freem(control);
575         }
576         INP_WUNLOCK(inp);
577         return (error);
578 }
579
580 /*
581  * Raw IPv6 socket option processing.
582  */
583 int
584 rip6_ctloutput(struct socket *so, struct sockopt *sopt)
585 {
586         struct inpcb *inp;
587         int error;
588
589         if (sopt->sopt_level == IPPROTO_ICMPV6)
590                 /*
591                  * XXX: is it better to call icmp6_ctloutput() directly
592                  * from protosw?
593                  */
594                 return (icmp6_ctloutput(so, sopt));
595         else if (sopt->sopt_level != IPPROTO_IPV6) {
596                 if (sopt->sopt_level == SOL_SOCKET &&
597                     sopt->sopt_name == SO_SETFIB) {
598                         inp = sotoinpcb(so);
599                         INP_WLOCK(inp);
600                         inp->inp_inc.inc_fibnum = so->so_fibnum;
601                         INP_WUNLOCK(inp);
602                         return (0);
603                 }
604                 return (EINVAL);
605         }
606
607         error = 0;
608
609         switch (sopt->sopt_dir) {
610         case SOPT_GET:
611                 switch (sopt->sopt_name) {
612                 case MRT6_INIT:
613                 case MRT6_DONE:
614                 case MRT6_ADD_MIF:
615                 case MRT6_DEL_MIF:
616                 case MRT6_ADD_MFC:
617                 case MRT6_DEL_MFC:
618                 case MRT6_PIM:
619                         error = ip6_mrouter_get ?  ip6_mrouter_get(so, sopt) :
620                             EOPNOTSUPP;
621                         break;
622                 case IPV6_CHECKSUM:
623                         error = ip6_raw_ctloutput(so, sopt);
624                         break;
625                 default:
626                         error = ip6_ctloutput(so, sopt);
627                         break;
628                 }
629                 break;
630
631         case SOPT_SET:
632                 switch (sopt->sopt_name) {
633                 case MRT6_INIT:
634                 case MRT6_DONE:
635                 case MRT6_ADD_MIF:
636                 case MRT6_DEL_MIF:
637                 case MRT6_ADD_MFC:
638                 case MRT6_DEL_MFC:
639                 case MRT6_PIM:
640                         error = ip6_mrouter_set ?  ip6_mrouter_set(so, sopt) :
641                             EOPNOTSUPP;
642                         break;
643                 case IPV6_CHECKSUM:
644                         error = ip6_raw_ctloutput(so, sopt);
645                         break;
646                 default:
647                         error = ip6_ctloutput(so, sopt);
648                         break;
649                 }
650                 break;
651         }
652
653         return (error);
654 }
655
656 static int
657 rip6_attach(struct socket *so, int proto, struct thread *td)
658 {
659         struct inpcb *inp;
660         struct icmp6_filter *filter;
661         int error;
662
663         inp = sotoinpcb(so);
664         KASSERT(inp == NULL, ("rip6_attach: inp != NULL"));
665
666         error = priv_check(td, PRIV_NETINET_RAW);
667         if (error)
668                 return (error);
669         error = soreserve(so, rip_sendspace, rip_recvspace);
670         if (error)
671                 return (error);
672         filter = malloc(sizeof(struct icmp6_filter), M_PCB, M_NOWAIT);
673         if (filter == NULL)
674                 return (ENOMEM);
675         INP_INFO_WLOCK(&V_ripcbinfo);
676         error = in_pcballoc(so, &V_ripcbinfo);
677         if (error) {
678                 INP_INFO_WUNLOCK(&V_ripcbinfo);
679                 free(filter, M_PCB);
680                 return (error);
681         }
682         inp = (struct inpcb *)so->so_pcb;
683         INP_INFO_WUNLOCK(&V_ripcbinfo);
684         inp->inp_vflag |= INP_IPV6;
685         inp->inp_ip_p = (long)proto;
686         inp->in6p_hops = -1;    /* use kernel default */
687         inp->in6p_cksum = -1;
688         inp->in6p_icmp6filt = filter;
689         ICMP6_FILTER_SETPASSALL(inp->in6p_icmp6filt);
690         INP_WUNLOCK(inp);
691         return (0);
692 }
693
694 static void
695 rip6_detach(struct socket *so)
696 {
697         struct inpcb *inp;
698
699         inp = sotoinpcb(so);
700         KASSERT(inp != NULL, ("rip6_detach: inp == NULL"));
701
702         if (so == V_ip6_mrouter && ip6_mrouter_done)
703                 ip6_mrouter_done();
704         /* xxx: RSVP */
705         INP_INFO_WLOCK(&V_ripcbinfo);
706         INP_WLOCK(inp);
707         free(inp->in6p_icmp6filt, M_PCB);
708         in_pcbdetach(inp);
709         in_pcbfree(inp);
710         INP_INFO_WUNLOCK(&V_ripcbinfo);
711 }
712
713 /* XXXRW: This can't ever be called. */
714 static void
715 rip6_abort(struct socket *so)
716 {
717         struct inpcb *inp;
718
719         inp = sotoinpcb(so);
720         KASSERT(inp != NULL, ("rip6_abort: inp == NULL"));
721
722         soisdisconnected(so);
723 }
724
725 static void
726 rip6_close(struct socket *so)
727 {
728         struct inpcb *inp;
729
730         inp = sotoinpcb(so);
731         KASSERT(inp != NULL, ("rip6_close: inp == NULL"));
732
733         soisdisconnected(so);
734 }
735
736 static int
737 rip6_disconnect(struct socket *so)
738 {
739         struct inpcb *inp;
740
741         inp = sotoinpcb(so);
742         KASSERT(inp != NULL, ("rip6_disconnect: inp == NULL"));
743
744         if ((so->so_state & SS_ISCONNECTED) == 0)
745                 return (ENOTCONN);
746         inp->in6p_faddr = in6addr_any;
747         rip6_abort(so);
748         return (0);
749 }
750
751 static int
752 rip6_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
753 {
754         struct epoch_tracker et;
755         struct inpcb *inp;
756         struct sockaddr_in6 *addr = (struct sockaddr_in6 *)nam;
757         struct ifaddr *ifa = NULL;
758         int error = 0;
759
760         inp = sotoinpcb(so);
761         KASSERT(inp != NULL, ("rip6_bind: inp == NULL"));
762
763         if (nam->sa_len != sizeof(*addr))
764                 return (EINVAL);
765         if ((error = prison_check_ip6(td->td_ucred, &addr->sin6_addr)) != 0)
766                 return (error);
767         if (CK_STAILQ_EMPTY(&V_ifnet) || addr->sin6_family != AF_INET6)
768                 return (EADDRNOTAVAIL);
769         if ((error = sa6_embedscope(addr, V_ip6_use_defzone)) != 0)
770                 return (error);
771
772         NET_EPOCH_ENTER(et);
773         if (!IN6_IS_ADDR_UNSPECIFIED(&addr->sin6_addr) &&
774             (ifa = ifa_ifwithaddr((struct sockaddr *)addr)) == NULL) {
775                 NET_EPOCH_EXIT(et);
776                 return (EADDRNOTAVAIL);
777         }
778         if (ifa != NULL &&
779             ((struct in6_ifaddr *)ifa)->ia6_flags &
780             (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY|
781              IN6_IFF_DETACHED|IN6_IFF_DEPRECATED)) {
782                 NET_EPOCH_EXIT(et);
783                 return (EADDRNOTAVAIL);
784         }
785         NET_EPOCH_EXIT(et);
786         INP_INFO_WLOCK(&V_ripcbinfo);
787         INP_WLOCK(inp);
788         inp->in6p_laddr = addr->sin6_addr;
789         INP_WUNLOCK(inp);
790         INP_INFO_WUNLOCK(&V_ripcbinfo);
791         return (0);
792 }
793
794 static int
795 rip6_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
796 {
797         struct inpcb *inp;
798         struct sockaddr_in6 *addr = (struct sockaddr_in6 *)nam;
799         struct in6_addr in6a;
800         struct epoch_tracker et;
801         int error = 0, scope_ambiguous = 0;
802
803         inp = sotoinpcb(so);
804         KASSERT(inp != NULL, ("rip6_connect: inp == NULL"));
805
806         if (nam->sa_len != sizeof(*addr))
807                 return (EINVAL);
808         if (CK_STAILQ_EMPTY(&V_ifnet))
809                 return (EADDRNOTAVAIL);
810         if (addr->sin6_family != AF_INET6)
811                 return (EAFNOSUPPORT);
812
813         /*
814          * Application should provide a proper zone ID or the use of default
815          * zone IDs should be enabled.  Unfortunately, some applications do
816          * not behave as it should, so we need a workaround.  Even if an
817          * appropriate ID is not determined, we'll see if we can determine
818          * the outgoing interface.  If we can, determine the zone ID based on
819          * the interface below.
820          */
821         if (addr->sin6_scope_id == 0 && !V_ip6_use_defzone)
822                 scope_ambiguous = 1;
823         if ((error = sa6_embedscope(addr, V_ip6_use_defzone)) != 0)
824                 return (error);
825
826         INP_INFO_WLOCK(&V_ripcbinfo);
827         INP_WLOCK(inp);
828         /* Source address selection. XXX: need pcblookup? */
829         NET_EPOCH_ENTER(et);
830         error = in6_selectsrc_socket(addr, inp->in6p_outputopts,
831             inp, so->so_cred, scope_ambiguous, &in6a, NULL);
832         NET_EPOCH_EXIT(et);
833         if (error) {
834                 INP_WUNLOCK(inp);
835                 INP_INFO_WUNLOCK(&V_ripcbinfo);
836                 return (error);
837         }
838
839         inp->in6p_faddr = addr->sin6_addr;
840         inp->in6p_laddr = in6a;
841         soisconnected(so);
842         INP_WUNLOCK(inp);
843         INP_INFO_WUNLOCK(&V_ripcbinfo);
844         return (0);
845 }
846
847 static int
848 rip6_shutdown(struct socket *so)
849 {
850         struct inpcb *inp;
851
852         inp = sotoinpcb(so);
853         KASSERT(inp != NULL, ("rip6_shutdown: inp == NULL"));
854
855         INP_WLOCK(inp);
856         socantsendmore(so);
857         INP_WUNLOCK(inp);
858         return (0);
859 }
860
861 static int
862 rip6_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
863     struct mbuf *control, struct thread *td)
864 {
865         struct inpcb *inp;
866         struct sockaddr_in6 tmp;
867         struct sockaddr_in6 *dst;
868         int ret;
869
870         inp = sotoinpcb(so);
871         KASSERT(inp != NULL, ("rip6_send: inp == NULL"));
872
873         /* Always copy sockaddr to avoid overwrites. */
874         /* Unlocked read. */
875         if (so->so_state & SS_ISCONNECTED) {
876                 if (nam) {
877                         m_freem(m);
878                         return (EISCONN);
879                 }
880                 /* XXX */
881                 bzero(&tmp, sizeof(tmp));
882                 tmp.sin6_family = AF_INET6;
883                 tmp.sin6_len = sizeof(struct sockaddr_in6);
884                 INP_RLOCK(inp);
885                 bcopy(&inp->in6p_faddr, &tmp.sin6_addr,
886                     sizeof(struct in6_addr));
887                 INP_RUNLOCK(inp);
888                 dst = &tmp;
889         } else {
890                 if (nam == NULL) {
891                         m_freem(m);
892                         return (ENOTCONN);
893                 }
894                 if (nam->sa_len != sizeof(struct sockaddr_in6)) {
895                         m_freem(m);
896                         return (EINVAL);
897                 }
898                 tmp = *(struct sockaddr_in6 *)nam;
899                 dst = &tmp;
900
901                 if (dst->sin6_family == AF_UNSPEC) {
902                         /*
903                          * XXX: we allow this case for backward
904                          * compatibility to buggy applications that
905                          * rely on old (and wrong) kernel behavior.
906                          */
907                         log(LOG_INFO, "rip6 SEND: address family is "
908                             "unspec. Assume AF_INET6\n");
909                         dst->sin6_family = AF_INET6;
910                 } else if (dst->sin6_family != AF_INET6) {
911                         m_freem(m);
912                         return(EAFNOSUPPORT);
913                 }
914         }
915         ret = rip6_output(m, so, dst, control);
916         return (ret);
917 }
918
919 struct pr_usrreqs rip6_usrreqs = {
920         .pru_abort =            rip6_abort,
921         .pru_attach =           rip6_attach,
922         .pru_bind =             rip6_bind,
923         .pru_connect =          rip6_connect,
924         .pru_control =          in6_control,
925         .pru_detach =           rip6_detach,
926         .pru_disconnect =       rip6_disconnect,
927         .pru_peeraddr =         in6_getpeeraddr,
928         .pru_send =             rip6_send,
929         .pru_shutdown =         rip6_shutdown,
930         .pru_sockaddr =         in6_getsockaddr,
931         .pru_close =            rip6_close,
932 };