]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/netinet6/raw_ip6.c
libpcap: Update to 1.10.4
[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_private.h>
90 #include <net/if_types.h>
91 #include <net/route.h>
92 #include <net/vnet.h>
93
94 #include <netinet/in.h>
95 #include <netinet/in_var.h>
96 #include <netinet/in_systm.h>
97 #include <netinet/in_pcb.h>
98
99 #include <netinet/icmp6.h>
100 #include <netinet/ip6.h>
101 #include <netinet/ip_var.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 inpcbinfo, ripcbinfo);
123 #define V_ripcbinfo                     VNET(ripcbinfo)
124
125 extern u_long   rip_sendspace;
126 extern u_long   rip_recvspace;
127
128 VNET_PCPUSTAT_DEFINE(struct rip6stat, rip6stat);
129 VNET_PCPUSTAT_SYSINIT(rip6stat);
130
131 #ifdef VIMAGE
132 VNET_PCPUSTAT_SYSUNINIT(rip6stat);
133 #endif /* VIMAGE */
134
135 /*
136  * Hooks for multicast routing. They all default to NULL, so leave them not
137  * initialized and rely on BSS being set to 0.
138  */
139
140 /*
141  * The socket used to communicate with the multicast routing daemon.
142  */
143 VNET_DEFINE(struct socket *, ip6_mrouter);
144
145 /*
146  * The various mrouter functions.
147  */
148 int (*ip6_mrouter_set)(struct socket *, struct sockopt *);
149 int (*ip6_mrouter_get)(struct socket *, struct sockopt *);
150 int (*ip6_mrouter_done)(void);
151 int (*ip6_mforward)(struct ip6_hdr *, struct ifnet *, struct mbuf *);
152 int (*mrt6_ioctl)(u_long, caddr_t);
153
154 struct rip6_inp_match_ctx {
155         struct ip6_hdr *ip6;
156         int proto;
157 };
158
159 static bool
160 rip6_inp_match(const struct inpcb *inp, void *v)
161 {
162         struct rip6_inp_match_ctx *c = v;
163         struct ip6_hdr *ip6 = c->ip6;
164         int proto = c->proto;
165
166         /* XXX inp locking */
167         if ((inp->inp_vflag & INP_IPV6) == 0)
168                 return (false);
169         if (inp->inp_ip_p && inp->inp_ip_p != proto)
170                 return (false);
171         if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr) &&
172             !IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, &ip6->ip6_dst))
173                 return (false);
174         if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) &&
175             !IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, &ip6->ip6_src))
176                 return (false);
177
178         return (true);
179 }
180
181 /*
182  * Setup generic address and protocol structures for raw_input routine, then
183  * pass them along with mbuf chain.
184  */
185 int
186 rip6_input(struct mbuf **mp, int *offp, int proto)
187 {
188         struct ifnet *ifp;
189         struct mbuf *n, *m = *mp;
190         struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
191         struct inpcb *inp;
192         struct mbuf *opts = NULL;
193         struct sockaddr_in6 fromsa;
194         struct rip6_inp_match_ctx ctx = { .ip6 = ip6, .proto = proto };
195         struct inpcb_iterator inpi = INP_ITERATOR(&V_ripcbinfo,
196             INPLOOKUP_RLOCKPCB, rip6_inp_match, &ctx);
197         int delivered = 0;
198
199         NET_EPOCH_ASSERT();
200
201         RIP6STAT_INC(rip6s_ipackets);
202
203         init_sin6(&fromsa, m, 0); /* general init */
204
205         ifp = m->m_pkthdr.rcvif;
206
207         while ((inp = inp_next(&inpi)) != NULL) {
208                 INP_RLOCK_ASSERT(inp);
209 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
210                 /*
211                  * Check AH/ESP integrity.
212                  */
213                 if (IPSEC_ENABLED(ipv6) &&
214                     IPSEC_CHECK_POLICY(ipv6, m, inp) != 0) {
215                         /* Do not inject data into pcb. */
216                         continue;
217                 }
218 #endif /* IPSEC */
219                 if (jailed_without_vnet(inp->inp_cred) &&
220                     !IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) &&
221                     prison_check_ip6(inp->inp_cred, &ip6->ip6_dst) != 0)
222                         /*
223                          * Allow raw socket in jail to receive multicast;
224                          * assume process had PRIV_NETINET_RAW at attach,
225                          * and fall through into normal filter path if so.
226                          */
227                         continue;
228                 if (inp->in6p_cksum != -1) {
229                         RIP6STAT_INC(rip6s_isum);
230                         if (m->m_pkthdr.len - (*offp + inp->in6p_cksum) < 2 ||
231                             in6_cksum(m, proto, *offp,
232                             m->m_pkthdr.len - *offp)) {
233                                 RIP6STAT_INC(rip6s_badsum);
234                                 /*
235                                  * Drop the received message, don't send an
236                                  * ICMP6 message. Set proto to IPPROTO_NONE
237                                  * to achieve that.
238                                  */
239                                 INP_RUNLOCK(inp);
240                                 proto = IPPROTO_NONE;
241                                 break;
242                         }
243                 }
244                 /*
245                  * If this raw socket has multicast state, and we
246                  * have received a multicast, check if this socket
247                  * should receive it, as multicast filtering is now
248                  * the responsibility of the transport layer.
249                  */
250                 if (inp->in6p_moptions &&
251                     IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
252                         /*
253                          * If the incoming datagram is for MLD, allow it
254                          * through unconditionally to the raw socket.
255                          *
256                          * Use the M_RTALERT_MLD flag to check for MLD
257                          * traffic without having to inspect the mbuf chain
258                          * more deeply, as all MLDv1/v2 host messages MUST
259                          * contain the Router Alert option.
260                          *
261                          * In the case of MLDv1, we may not have explicitly
262                          * joined the group, and may have set IFF_ALLMULTI
263                          * on the interface. im6o_mc_filter() may discard
264                          * control traffic we actually need to see.
265                          *
266                          * Userland multicast routing daemons should continue
267                          * filter the control traffic appropriately.
268                          */
269                         int blocked;
270
271                         blocked = MCAST_PASS;
272                         if ((m->m_flags & M_RTALERT_MLD) == 0) {
273                                 struct sockaddr_in6 mcaddr;
274
275                                 bzero(&mcaddr, sizeof(struct sockaddr_in6));
276                                 mcaddr.sin6_len = sizeof(struct sockaddr_in6);
277                                 mcaddr.sin6_family = AF_INET6;
278                                 mcaddr.sin6_addr = ip6->ip6_dst;
279
280                                 blocked = im6o_mc_filter(inp->in6p_moptions,
281                                     ifp,
282                                     (struct sockaddr *)&mcaddr,
283                                     (struct sockaddr *)&fromsa);
284                         }
285                         if (blocked != MCAST_PASS) {
286                                 IP6STAT_INC(ip6s_notmember);
287                                 continue;
288                         }
289                 }
290                 if ((n = m_copym(m, 0, M_COPYALL, M_NOWAIT)) == NULL)
291                         continue;
292                 if (inp->inp_flags & INP_CONTROLOPTS ||
293                     inp->inp_socket->so_options & SO_TIMESTAMP)
294                         ip6_savecontrol(inp, n, &opts);
295                 /* strip intermediate headers */
296                 m_adj(n, *offp);
297                 if (sbappendaddr(&inp->inp_socket->so_rcv,
298                     (struct sockaddr *)&fromsa, n, opts) == 0) {
299                         soroverflow(inp->inp_socket);
300                         m_freem(n);
301                         if (opts)
302                                 m_freem(opts);
303                         RIP6STAT_INC(rip6s_fullsock);
304                 } else {
305                         sorwakeup(inp->inp_socket);
306                         delivered++;
307                 }
308                 opts = NULL;
309         }
310         if (delivered == 0) {
311                 RIP6STAT_INC(rip6s_nosock);
312                 if (m->m_flags & M_MCAST)
313                         RIP6STAT_INC(rip6s_nosockmcast);
314                 if (proto == IPPROTO_NONE)
315                         m_freem(m);
316                 else
317                         icmp6_error(m, ICMP6_PARAM_PROB,
318                             ICMP6_PARAMPROB_NEXTHEADER,
319                             ip6_get_prevhdr(m, *offp));
320                 IP6STAT_DEC(ip6s_delivered);
321         } else
322                 m_freem(m);
323         return (IPPROTO_DONE);
324 }
325
326 void
327 rip6_ctlinput(struct ip6ctlparam *ip6cp)
328 {
329         int errno;
330
331         if ((errno = icmp6_errmap(ip6cp->ip6c_icmp6)) != 0)
332                 in6_pcbnotify(&V_ripcbinfo, ip6cp->ip6c_finaldst, 0,
333                     ip6cp->ip6c_src, 0, errno, ip6cp->ip6c_cmdarg,
334                     in6_rtchange);
335 }
336
337 /*
338  * Generate IPv6 header and pass packet to ip6_output.  Tack on options user
339  * may have setup with control call.
340  */
341 static int
342 rip6_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
343     struct mbuf *control, struct thread *td)
344 {
345         struct epoch_tracker et;
346         struct inpcb *inp;
347         struct sockaddr_in6 tmp, *dstsock;
348         struct m_tag *mtag;
349         struct ip6_hdr *ip6;
350         u_int   plen = m->m_pkthdr.len;
351         struct ip6_pktopts opt, *optp;
352         struct ifnet *oifp = NULL;
353         int error;
354         int type = 0, code = 0;         /* for ICMPv6 output statistics only */
355         int scope_ambiguous = 0;
356         int use_defzone = 0;
357         int hlim = 0;
358         struct in6_addr in6a;
359
360         inp = sotoinpcb(so);
361         KASSERT(inp != NULL, ("rip6_send: inp == NULL"));
362
363         /* Always copy sockaddr to avoid overwrites. */
364         /* Unlocked read. */
365         if (so->so_state & SS_ISCONNECTED) {
366                 if (nam) {
367                         error = EISCONN;
368                         goto release;
369                 }
370                 tmp = (struct sockaddr_in6 ){
371                         .sin6_family = AF_INET6,
372                         .sin6_len = sizeof(struct sockaddr_in6),
373                 };
374                 INP_RLOCK(inp);
375                 bcopy(&inp->in6p_faddr, &tmp.sin6_addr,
376                     sizeof(struct in6_addr));
377                 INP_RUNLOCK(inp);
378                 dstsock = &tmp;
379         } else {
380                 if (nam == NULL)
381                         error = ENOTCONN;
382                 else if (nam->sa_family != AF_INET6)
383                         error = EAFNOSUPPORT;
384                 else if (nam->sa_len != sizeof(struct sockaddr_in6))
385                         error = EINVAL;
386                 else
387                         error = 0;
388                 if (error != 0)
389                         goto release;
390                 dstsock = (struct sockaddr_in6 *)nam;
391                 if (dstsock->sin6_family != AF_INET6) {
392                         error = EAFNOSUPPORT;
393                         goto release;
394                 }
395         }
396
397         INP_WLOCK(inp);
398
399         if (control != NULL) {
400                 NET_EPOCH_ENTER(et);
401                 error = ip6_setpktopts(control, &opt, inp->in6p_outputopts,
402                     so->so_cred, inp->inp_ip_p);
403                 NET_EPOCH_EXIT(et);
404
405                 if (error != 0) {
406                         goto bad;
407                 }
408                 optp = &opt;
409         } else
410                 optp = inp->in6p_outputopts;
411
412         /*
413          * Check and convert scope zone ID into internal form.
414          *
415          * XXX: we may still need to determine the zone later.
416          */
417         if (!(so->so_state & SS_ISCONNECTED)) {
418                 if (!optp || !optp->ip6po_pktinfo ||
419                     !optp->ip6po_pktinfo->ipi6_ifindex)
420                         use_defzone = V_ip6_use_defzone;
421                 if (dstsock->sin6_scope_id == 0 && !use_defzone)
422                         scope_ambiguous = 1;
423                 if ((error = sa6_embedscope(dstsock, use_defzone)) != 0)
424                         goto bad;
425         }
426
427         /*
428          * For an ICMPv6 packet, we should know its type and code to update
429          * statistics.
430          */
431         if (inp->inp_ip_p == IPPROTO_ICMPV6) {
432                 struct icmp6_hdr *icmp6;
433                 if (m->m_len < sizeof(struct icmp6_hdr) &&
434                     (m = m_pullup(m, sizeof(struct icmp6_hdr))) == NULL) {
435                         error = ENOBUFS;
436                         goto bad;
437                 }
438                 icmp6 = mtod(m, struct icmp6_hdr *);
439                 type = icmp6->icmp6_type;
440                 code = icmp6->icmp6_code;
441         }
442
443         M_PREPEND(m, sizeof(*ip6), M_NOWAIT);
444         if (m == NULL) {
445                 error = ENOBUFS;
446                 goto bad;
447         }
448         ip6 = mtod(m, struct ip6_hdr *);
449
450 #ifdef ROUTE_MPATH
451         if (CALC_FLOWID_OUTBOUND) {
452                 uint32_t hash_type, hash_val;
453
454                 hash_val = fib6_calc_software_hash(&inp->in6p_laddr,
455                     &dstsock->sin6_addr, 0, 0, inp->inp_ip_p, &hash_type);
456                 inp->inp_flowid = hash_val;
457                 inp->inp_flowtype = hash_type;
458         }
459 #endif
460         /*
461          * Source address selection.
462          */
463         NET_EPOCH_ENTER(et);
464         error = in6_selectsrc_socket(dstsock, optp, inp, so->so_cred,
465             scope_ambiguous, &in6a, &hlim);
466         NET_EPOCH_EXIT(et);
467
468         if (error)
469                 goto bad;
470         error = prison_check_ip6(inp->inp_cred, &in6a);
471         if (error != 0)
472                 goto bad;
473         ip6->ip6_src = in6a;
474
475         ip6->ip6_dst = dstsock->sin6_addr;
476
477         /*
478          * Fill in the rest of the IPv6 header fields.
479          */
480         ip6->ip6_flow = (ip6->ip6_flow & ~IPV6_FLOWINFO_MASK) |
481             (inp->inp_flow & IPV6_FLOWINFO_MASK);
482         ip6->ip6_vfc = (ip6->ip6_vfc & ~IPV6_VERSION_MASK) |
483             (IPV6_VERSION & IPV6_VERSION_MASK);
484
485         /*
486          * ip6_plen will be filled in ip6_output, so not fill it here.
487          */
488         ip6->ip6_nxt = inp->inp_ip_p;
489         ip6->ip6_hlim = hlim;
490
491         if (inp->inp_ip_p == IPPROTO_ICMPV6 || inp->in6p_cksum != -1) {
492                 struct mbuf *n;
493                 int off;
494                 u_int16_t *p;
495
496                 /* Compute checksum. */
497                 if (inp->inp_ip_p == IPPROTO_ICMPV6)
498                         off = offsetof(struct icmp6_hdr, icmp6_cksum);
499                 else
500                         off = inp->in6p_cksum;
501                 if (plen < off + 2) {
502                         error = EINVAL;
503                         goto bad;
504                 }
505                 off += sizeof(struct ip6_hdr);
506
507                 n = m;
508                 while (n && n->m_len <= off) {
509                         off -= n->m_len;
510                         n = n->m_next;
511                 }
512                 if (!n)
513                         goto bad;
514                 p = (u_int16_t *)(mtod(n, caddr_t) + off);
515                 *p = 0;
516                 *p = in6_cksum(m, ip6->ip6_nxt, sizeof(*ip6), plen);
517         }
518
519         /*
520          * Send RA/RS messages to user land for protection, before sending
521          * them to rtadvd/rtsol.
522          */
523         if ((send_sendso_input_hook != NULL) &&
524             inp->inp_ip_p == IPPROTO_ICMPV6) {
525                 switch (type) {
526                 case ND_ROUTER_ADVERT:
527                 case ND_ROUTER_SOLICIT:
528                         mtag = m_tag_get(PACKET_TAG_ND_OUTGOING,
529                                 sizeof(unsigned short), M_NOWAIT);
530                         if (mtag == NULL)
531                                 goto bad;
532                         m_tag_prepend(m, mtag);
533                 }
534         }
535
536         NET_EPOCH_ENTER(et);
537         error = ip6_output(m, optp, NULL, 0, inp->in6p_moptions, &oifp, inp);
538         NET_EPOCH_EXIT(et);
539         if (inp->inp_ip_p == IPPROTO_ICMPV6) {
540                 if (oifp)
541                         icmp6_ifoutstat_inc(oifp, type, code);
542                 ICMP6STAT_INC(icp6s_outhist[type]);
543         } else
544                 RIP6STAT_INC(rip6s_opackets);
545
546         goto freectl;
547
548  bad:
549         if (m)
550                 m_freem(m);
551
552  freectl:
553         if (control != NULL) {
554                 ip6_clearpktopts(&opt, -1);
555                 m_freem(control);
556         }
557         INP_WUNLOCK(inp);
558         return (error);
559
560 release:
561         if (control != NULL)
562                 m_freem(control);
563         m_freem(m);
564         return (error);
565 }
566
567 /*
568  * Raw IPv6 socket option processing.
569  */
570 int
571 rip6_ctloutput(struct socket *so, struct sockopt *sopt)
572 {
573         struct inpcb *inp = sotoinpcb(so);
574         int error;
575
576         if (sopt->sopt_level == IPPROTO_ICMPV6)
577                 /*
578                  * XXX: is it better to call icmp6_ctloutput() directly
579                  * from protosw?
580                  */
581                 return (icmp6_ctloutput(so, sopt));
582         else if (sopt->sopt_level != IPPROTO_IPV6) {
583                 if (sopt->sopt_level == SOL_SOCKET &&
584                     sopt->sopt_name == SO_SETFIB) {
585                         INP_WLOCK(inp);
586                         inp->inp_inc.inc_fibnum = so->so_fibnum;
587                         INP_WUNLOCK(inp);
588                         return (0);
589                 }
590                 return (EINVAL);
591         }
592
593         error = 0;
594
595         switch (sopt->sopt_dir) {
596         case SOPT_GET:
597                 switch (sopt->sopt_name) {
598                 case MRT6_INIT:
599                 case MRT6_DONE:
600                 case MRT6_ADD_MIF:
601                 case MRT6_DEL_MIF:
602                 case MRT6_ADD_MFC:
603                 case MRT6_DEL_MFC:
604                 case MRT6_PIM:
605                         if (inp->inp_ip_p != IPPROTO_ICMPV6)
606                                 return (EOPNOTSUPP);
607                         error = ip6_mrouter_get ?  ip6_mrouter_get(so, sopt) :
608                             EOPNOTSUPP;
609                         break;
610                 case IPV6_CHECKSUM:
611                         error = ip6_raw_ctloutput(so, sopt);
612                         break;
613                 default:
614                         error = ip6_ctloutput(so, sopt);
615                         break;
616                 }
617                 break;
618
619         case SOPT_SET:
620                 switch (sopt->sopt_name) {
621                 case MRT6_INIT:
622                 case MRT6_DONE:
623                 case MRT6_ADD_MIF:
624                 case MRT6_DEL_MIF:
625                 case MRT6_ADD_MFC:
626                 case MRT6_DEL_MFC:
627                 case MRT6_PIM:
628                         if (inp->inp_ip_p != IPPROTO_ICMPV6)
629                                 return (EOPNOTSUPP);
630                         error = ip6_mrouter_set ?  ip6_mrouter_set(so, sopt) :
631                             EOPNOTSUPP;
632                         break;
633                 case IPV6_CHECKSUM:
634                         error = ip6_raw_ctloutput(so, sopt);
635                         break;
636                 default:
637                         error = ip6_ctloutput(so, sopt);
638                         break;
639                 }
640                 break;
641         }
642
643         return (error);
644 }
645
646 static int
647 rip6_attach(struct socket *so, int proto, struct thread *td)
648 {
649         struct inpcb *inp;
650         struct icmp6_filter *filter;
651         int error;
652
653         inp = sotoinpcb(so);
654         KASSERT(inp == NULL, ("rip6_attach: inp != NULL"));
655
656         error = priv_check(td, PRIV_NETINET_RAW);
657         if (error)
658                 return (error);
659         if (proto >= IPPROTO_MAX || proto < 0)
660                 return (EPROTONOSUPPORT);
661         error = soreserve(so, rip_sendspace, rip_recvspace);
662         if (error)
663                 return (error);
664         filter = malloc(sizeof(struct icmp6_filter), M_PCB, M_NOWAIT);
665         if (filter == NULL)
666                 return (ENOMEM);
667         error = in_pcballoc(so, &V_ripcbinfo);
668         if (error) {
669                 free(filter, M_PCB);
670                 return (error);
671         }
672         inp = (struct inpcb *)so->so_pcb;
673         inp->inp_ip_p = proto;
674         inp->in6p_cksum = -1;
675         inp->in6p_icmp6filt = filter;
676         ICMP6_FILTER_SETPASSALL(inp->in6p_icmp6filt);
677         INP_WUNLOCK(inp);
678         return (0);
679 }
680
681 static void
682 rip6_detach(struct socket *so)
683 {
684         struct inpcb *inp;
685
686         inp = sotoinpcb(so);
687         KASSERT(inp != NULL, ("rip6_detach: inp == NULL"));
688
689         if (so == V_ip6_mrouter && ip6_mrouter_done)
690                 ip6_mrouter_done();
691         /* xxx: RSVP */
692         INP_WLOCK(inp);
693         free(inp->in6p_icmp6filt, M_PCB);
694         in_pcbdetach(inp);
695         in_pcbfree(inp);
696 }
697
698 /* XXXRW: This can't ever be called. */
699 static void
700 rip6_abort(struct socket *so)
701 {
702         struct inpcb *inp __diagused;
703
704         inp = sotoinpcb(so);
705         KASSERT(inp != NULL, ("rip6_abort: inp == NULL"));
706
707         soisdisconnected(so);
708 }
709
710 static void
711 rip6_close(struct socket *so)
712 {
713         struct inpcb *inp __diagused;
714
715         inp = sotoinpcb(so);
716         KASSERT(inp != NULL, ("rip6_close: inp == NULL"));
717
718         soisdisconnected(so);
719 }
720
721 static int
722 rip6_disconnect(struct socket *so)
723 {
724         struct inpcb *inp;
725
726         inp = sotoinpcb(so);
727         KASSERT(inp != NULL, ("rip6_disconnect: inp == NULL"));
728
729         if ((so->so_state & SS_ISCONNECTED) == 0)
730                 return (ENOTCONN);
731         inp->in6p_faddr = in6addr_any;
732         rip6_abort(so);
733         return (0);
734 }
735
736 static int
737 rip6_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
738 {
739         struct epoch_tracker et;
740         struct inpcb *inp;
741         struct sockaddr_in6 *addr = (struct sockaddr_in6 *)nam;
742         struct ifaddr *ifa = NULL;
743         int error = 0;
744
745         inp = sotoinpcb(so);
746         KASSERT(inp != NULL, ("rip6_bind: inp == NULL"));
747
748         if (nam->sa_family != AF_INET6)
749                 return (EAFNOSUPPORT);
750         if (nam->sa_len != sizeof(*addr))
751                 return (EINVAL);
752         if ((error = prison_check_ip6(td->td_ucred, &addr->sin6_addr)) != 0)
753                 return (error);
754         if (CK_STAILQ_EMPTY(&V_ifnet) || addr->sin6_family != AF_INET6)
755                 return (EADDRNOTAVAIL);
756         if ((error = sa6_embedscope(addr, V_ip6_use_defzone)) != 0)
757                 return (error);
758
759         NET_EPOCH_ENTER(et);
760         if (!IN6_IS_ADDR_UNSPECIFIED(&addr->sin6_addr) &&
761             (ifa = ifa_ifwithaddr((struct sockaddr *)addr)) == NULL) {
762                 NET_EPOCH_EXIT(et);
763                 return (EADDRNOTAVAIL);
764         }
765         if (ifa != NULL &&
766             ((struct in6_ifaddr *)ifa)->ia6_flags &
767             (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY|
768              IN6_IFF_DETACHED|IN6_IFF_DEPRECATED)) {
769                 NET_EPOCH_EXIT(et);
770                 return (EADDRNOTAVAIL);
771         }
772         NET_EPOCH_EXIT(et);
773         INP_WLOCK(inp);
774         INP_INFO_WLOCK(&V_ripcbinfo);
775         inp->in6p_laddr = addr->sin6_addr;
776         INP_INFO_WUNLOCK(&V_ripcbinfo);
777         INP_WUNLOCK(inp);
778         return (0);
779 }
780
781 static int
782 rip6_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
783 {
784         struct inpcb *inp;
785         struct sockaddr_in6 *addr = (struct sockaddr_in6 *)nam;
786         struct in6_addr in6a;
787         struct epoch_tracker et;
788         int error = 0, scope_ambiguous = 0;
789
790         inp = sotoinpcb(so);
791         KASSERT(inp != NULL, ("rip6_connect: inp == NULL"));
792
793         if (nam->sa_len != sizeof(*addr))
794                 return (EINVAL);
795         if (CK_STAILQ_EMPTY(&V_ifnet))
796                 return (EADDRNOTAVAIL);
797         if (addr->sin6_family != AF_INET6)
798                 return (EAFNOSUPPORT);
799
800         /*
801          * Application should provide a proper zone ID or the use of default
802          * zone IDs should be enabled.  Unfortunately, some applications do
803          * not behave as it should, so we need a workaround.  Even if an
804          * appropriate ID is not determined, we'll see if we can determine
805          * the outgoing interface.  If we can, determine the zone ID based on
806          * the interface below.
807          */
808         if (addr->sin6_scope_id == 0 && !V_ip6_use_defzone)
809                 scope_ambiguous = 1;
810         if ((error = sa6_embedscope(addr, V_ip6_use_defzone)) != 0)
811                 return (error);
812
813         INP_WLOCK(inp);
814         INP_INFO_WLOCK(&V_ripcbinfo);
815         /* Source address selection. XXX: need pcblookup? */
816         NET_EPOCH_ENTER(et);
817         error = in6_selectsrc_socket(addr, inp->in6p_outputopts,
818             inp, so->so_cred, scope_ambiguous, &in6a, NULL);
819         NET_EPOCH_EXIT(et);
820         if (error) {
821                 INP_INFO_WUNLOCK(&V_ripcbinfo);
822                 INP_WUNLOCK(inp);
823                 return (error);
824         }
825
826         inp->in6p_faddr = addr->sin6_addr;
827         inp->in6p_laddr = in6a;
828         soisconnected(so);
829         INP_INFO_WUNLOCK(&V_ripcbinfo);
830         INP_WUNLOCK(inp);
831         return (0);
832 }
833
834 static int
835 rip6_shutdown(struct socket *so)
836 {
837         struct inpcb *inp;
838
839         inp = sotoinpcb(so);
840         KASSERT(inp != NULL, ("rip6_shutdown: inp == NULL"));
841
842         INP_WLOCK(inp);
843         socantsendmore(so);
844         INP_WUNLOCK(inp);
845         return (0);
846 }
847
848 struct protosw rip6_protosw = {
849         .pr_type =              SOCK_RAW,
850         .pr_flags =             PR_ATOMIC|PR_ADDR,
851         .pr_ctloutput =         rip6_ctloutput,
852         .pr_abort =             rip6_abort,
853         .pr_attach =            rip6_attach,
854         .pr_bind =              rip6_bind,
855         .pr_connect =           rip6_connect,
856         .pr_control =           in6_control,
857         .pr_detach =            rip6_detach,
858         .pr_disconnect =        rip6_disconnect,
859         .pr_peeraddr =          in6_getpeeraddr,
860         .pr_send =              rip6_send,
861         .pr_shutdown =          rip6_shutdown,
862         .pr_sockaddr =          in6_getsockaddr,
863         .pr_close =             rip6_close
864 };