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