]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/netinet6/raw_ip6.c
add -n option to suppress clearing the build tree and add -DNO_CLEAN
[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/lock.h>
71 #include <sys/malloc.h>
72 #include <sys/mbuf.h>
73 #include <sys/priv.h>
74 #include <sys/proc.h>
75 #include <sys/protosw.h>
76 #include <sys/signalvar.h>
77 #include <sys/socket.h>
78 #include <sys/socketvar.h>
79 #include <sys/sx.h>
80 #include <sys/syslog.h>
81 #include <sys/vimage.h>
82
83 #include <net/if.h>
84 #include <net/if_types.h>
85 #include <net/route.h>
86
87 #include <netinet/in.h>
88 #include <netinet/in_var.h>
89 #include <netinet/in_systm.h>
90 #include <netinet/icmp6.h>
91 #include <netinet/in_pcb.h>
92 #include <netinet/ip6.h>
93 #include <netinet6/ip6protosw.h>
94 #include <netinet6/ip6_mroute.h>
95 #include <netinet6/in6_pcb.h>
96 #include <netinet6/ip6_var.h>
97 #include <netinet6/nd6.h>
98 #include <netinet6/raw_ip6.h>
99 #include <netinet6/scope6_var.h>
100
101 #ifdef IPSEC
102 #include <netipsec/ipsec.h>
103 #include <netipsec/ipsec6.h>
104 #endif /* IPSEC */
105
106 #include <machine/stdarg.h>
107
108 #define satosin6(sa)    ((struct sockaddr_in6 *)(sa))
109 #define ifatoia6(ifa)   ((struct in6_ifaddr *)(ifa))
110
111 /*
112  * Raw interface to IP6 protocol.
113  */
114
115 extern struct   inpcbhead ripcb;
116 extern struct   inpcbinfo ripcbinfo;
117 extern u_long   rip_sendspace;
118 extern u_long   rip_recvspace;
119
120 struct rip6stat rip6stat;
121
122 /*
123  * Hooks for multicast forwarding.
124  */
125 struct socket *ip6_mrouter = NULL;
126 int (*ip6_mrouter_set)(struct socket *, struct sockopt *);
127 int (*ip6_mrouter_get)(struct socket *, struct sockopt *);
128 int (*ip6_mrouter_done)(void);
129 int (*ip6_mforward)(struct ip6_hdr *, struct ifnet *, struct mbuf *);
130 int (*mrt6_ioctl)(int, caddr_t);
131
132 /*
133  * Setup generic address and protocol structures for raw_input routine, then
134  * pass them along with mbuf chain.
135  */
136 int
137 rip6_input(struct mbuf **mp, int *offp, int proto)
138 {
139         INIT_VNET_INET(curvnet);
140         INIT_VNET_INET6(curvnet);
141 #ifdef IPSEC
142         INIT_VNET_IPSEC(curvnet);
143 #endif
144         struct mbuf *m = *mp;
145         register struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
146         register struct inpcb *in6p;
147         struct inpcb *last = 0;
148         struct mbuf *opts = NULL;
149         struct sockaddr_in6 fromsa;
150
151         V_rip6stat.rip6s_ipackets++;
152
153         if (faithprefix_p != NULL && (*faithprefix_p)(&ip6->ip6_dst)) {
154                 /* XXX Send icmp6 host/port unreach? */
155                 m_freem(m);
156                 return (IPPROTO_DONE);
157         }
158
159         init_sin6(&fromsa, m); /* general init */
160
161         INP_INFO_RLOCK(&V_ripcbinfo);
162         LIST_FOREACH(in6p, &V_ripcb, inp_list) {
163                 if ((in6p->in6p_vflag & INP_IPV6) == 0)
164                         continue;
165                 if (in6p->in6p_ip6_nxt &&
166                     in6p->in6p_ip6_nxt != proto)
167                         continue;
168                 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) &&
169                     !IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &ip6->ip6_dst))
170                         continue;
171                 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr) &&
172                     !IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, &ip6->ip6_src))
173                         continue;
174                 INP_RLOCK(in6p);
175                 if (in6p->in6p_cksum != -1) {
176                         V_rip6stat.rip6s_isum++;
177                         if (in6_cksum(m, proto, *offp,
178                             m->m_pkthdr.len - *offp)) {
179                                 INP_RUNLOCK(in6p);
180                                 V_rip6stat.rip6s_badsum++;
181                                 continue;
182                         }
183                 }
184                 if (last) {
185                         struct mbuf *n = m_copy(m, 0, (int)M_COPYALL);
186
187 #ifdef IPSEC
188                         /*
189                          * Check AH/ESP integrity.
190                          */
191                         if (n && ipsec6_in_reject(n, last)) {
192                                 m_freem(n);
193                                 V_ipsec6stat.in_polvio++;
194                                 /* Do not inject data into pcb. */
195                         } else
196 #endif /* IPSEC */
197                         if (n) {
198                                 if (last->in6p_flags & IN6P_CONTROLOPTS ||
199                                     last->in6p_socket->so_options & SO_TIMESTAMP)
200                                         ip6_savecontrol(last, n, &opts);
201                                 /* strip intermediate headers */
202                                 m_adj(n, *offp);
203                                 if (sbappendaddr(&last->in6p_socket->so_rcv,
204                                                 (struct sockaddr *)&fromsa,
205                                                  n, opts) == 0) {
206                                         m_freem(n);
207                                         if (opts)
208                                                 m_freem(opts);
209                                         V_rip6stat.rip6s_fullsock++;
210                                 } else
211                                         sorwakeup(last->in6p_socket);
212                                 opts = NULL;
213                         }
214                         INP_RUNLOCK(last);
215                 }
216                 last = in6p;
217         }
218         INP_INFO_RUNLOCK(&V_ripcbinfo);
219 #ifdef IPSEC
220         /*
221          * Check AH/ESP integrity.
222          */
223         if (last && ipsec6_in_reject(m, last)) {
224                 m_freem(m);
225                 V_ipsec6stat.in_polvio++;
226                 V_ip6stat.ip6s_delivered--;
227                 /* Do not inject data into pcb. */
228                 INP_RUNLOCK(last);
229         } else
230 #endif /* IPSEC */
231         if (last) {
232                 if (last->in6p_flags & IN6P_CONTROLOPTS ||
233                     last->in6p_socket->so_options & SO_TIMESTAMP)
234                         ip6_savecontrol(last, m, &opts);
235                 /* Strip intermediate headers. */
236                 m_adj(m, *offp);
237                 if (sbappendaddr(&last->in6p_socket->so_rcv,
238                     (struct sockaddr *)&fromsa, m, opts) == 0) {
239                         m_freem(m);
240                         if (opts)
241                                 m_freem(opts);
242                         V_rip6stat.rip6s_fullsock++;
243                 } else
244                         sorwakeup(last->in6p_socket);
245                 INP_RUNLOCK(last);
246         } else {
247                 V_rip6stat.rip6s_nosock++;
248                 if (m->m_flags & M_MCAST)
249                         V_rip6stat.rip6s_nosockmcast++;
250                 if (proto == IPPROTO_NONE)
251                         m_freem(m);
252                 else {
253                         char *prvnxtp = ip6_get_prevhdr(m, *offp); /* XXX */
254                         icmp6_error(m, ICMP6_PARAM_PROB,
255                             ICMP6_PARAMPROB_NEXTHEADER,
256                             prvnxtp - mtod(m, char *));
257                 }
258                 V_ip6stat.ip6s_delivered--;
259         }
260         return (IPPROTO_DONE);
261 }
262
263 void
264 rip6_ctlinput(int cmd, struct sockaddr *sa, void *d)
265 {
266         INIT_VNET_INET(curvnet);
267         struct ip6_hdr *ip6;
268         struct mbuf *m;
269         int off = 0;
270         struct ip6ctlparam *ip6cp = NULL;
271         const struct sockaddr_in6 *sa6_src = NULL;
272         void *cmdarg;
273         struct inpcb *(*notify)(struct inpcb *, int) = in6_rtchange;
274
275         if (sa->sa_family != AF_INET6 ||
276             sa->sa_len != sizeof(struct sockaddr_in6))
277                 return;
278
279         if ((unsigned)cmd >= PRC_NCMDS)
280                 return;
281         if (PRC_IS_REDIRECT(cmd))
282                 notify = in6_rtchange, d = NULL;
283         else if (cmd == PRC_HOSTDEAD)
284                 d = NULL;
285         else if (inet6ctlerrmap[cmd] == 0)
286                 return;
287
288         /*
289          * If the parameter is from icmp6, decode it.
290          */
291         if (d != NULL) {
292                 ip6cp = (struct ip6ctlparam *)d;
293                 m = ip6cp->ip6c_m;
294                 ip6 = ip6cp->ip6c_ip6;
295                 off = ip6cp->ip6c_off;
296                 cmdarg = ip6cp->ip6c_cmdarg;
297                 sa6_src = ip6cp->ip6c_src;
298         } else {
299                 m = NULL;
300                 ip6 = NULL;
301                 cmdarg = NULL;
302                 sa6_src = &sa6_any;
303         }
304
305         (void) in6_pcbnotify(&V_ripcbinfo, sa, 0,
306             (const struct sockaddr *)sa6_src, 0, cmd, cmdarg, notify);
307 }
308
309 /*
310  * Generate IPv6 header and pass packet to ip6_output.  Tack on options user
311  * may have setup with control call.
312  */
313 int
314 #if __STDC__
315 rip6_output(struct mbuf *m, ...)
316 #else
317 rip6_output(m, va_alist)
318         struct mbuf *m;
319         va_dcl
320 #endif
321 {
322         INIT_VNET_INET6(curvnet);
323         struct mbuf *control;
324         struct socket *so;
325         struct sockaddr_in6 *dstsock;
326         struct in6_addr *dst;
327         struct ip6_hdr *ip6;
328         struct inpcb *in6p;
329         u_int   plen = m->m_pkthdr.len;
330         int error = 0;
331         struct ip6_pktopts opt, *optp;
332         struct ifnet *oifp = NULL;
333         int type = 0, code = 0;         /* for ICMPv6 output statistics only */
334         int scope_ambiguous = 0;
335         struct in6_addr *in6a;
336         va_list ap;
337
338         va_start(ap, m);
339         so = va_arg(ap, struct socket *);
340         dstsock = va_arg(ap, struct sockaddr_in6 *);
341         control = va_arg(ap, struct mbuf *);
342         va_end(ap);
343
344         in6p = sotoin6pcb(so);
345         INP_WLOCK(in6p);
346
347         dst = &dstsock->sin6_addr;
348         if (control) {
349                 if ((error = ip6_setpktopts(control, &opt,
350                     in6p->in6p_outputopts, so->so_cred,
351                     so->so_proto->pr_protocol)) != 0) {
352                         goto bad;
353                 }
354                 optp = &opt;
355         } else
356                 optp = in6p->in6p_outputopts;
357
358         /*
359          * Check and convert scope zone ID into internal form.
360          *
361          * XXX: we may still need to determine the zone later.
362          */
363         if (!(so->so_state & SS_ISCONNECTED)) {
364                 if (dstsock->sin6_scope_id == 0 && !V_ip6_use_defzone)
365                         scope_ambiguous = 1;
366                 if ((error = sa6_embedscope(dstsock, V_ip6_use_defzone)) != 0)
367                         goto bad;
368         }
369
370         /*
371          * For an ICMPv6 packet, we should know its type and code to update
372          * statistics.
373          */
374         if (so->so_proto->pr_protocol == IPPROTO_ICMPV6) {
375                 struct icmp6_hdr *icmp6;
376                 if (m->m_len < sizeof(struct icmp6_hdr) &&
377                     (m = m_pullup(m, sizeof(struct icmp6_hdr))) == NULL) {
378                         error = ENOBUFS;
379                         goto bad;
380                 }
381                 icmp6 = mtod(m, struct icmp6_hdr *);
382                 type = icmp6->icmp6_type;
383                 code = icmp6->icmp6_code;
384         }
385
386         M_PREPEND(m, sizeof(*ip6), M_DONTWAIT);
387         if (m == NULL) {
388                 error = ENOBUFS;
389                 goto bad;
390         }
391         ip6 = mtod(m, struct ip6_hdr *);
392
393         /*
394          * Source address selection.
395          */
396         if ((in6a = in6_selectsrc(dstsock, optp, in6p, NULL, so->so_cred,
397             &oifp, &error)) == NULL) {
398                 if (error == 0)
399                         error = EADDRNOTAVAIL;
400                 goto bad;
401         }
402         ip6->ip6_src = *in6a;
403
404         if (oifp && scope_ambiguous) {
405                 /*
406                  * Application should provide a proper zone ID or the use of
407                  * default zone IDs should be enabled.  Unfortunately, some
408                  * applications do not behave as it should, so we need a
409                  * workaround.  Even if an appropriate ID is not determined
410                  * (when it's required), if we can determine the outgoing
411                  * interface. determine the zone ID based on the interface.
412                  */
413                 error = in6_setscope(&dstsock->sin6_addr, oifp, NULL);
414                 if (error != 0)
415                         goto bad;
416         }
417         ip6->ip6_dst = dstsock->sin6_addr;
418
419         /*
420          * Fill in the rest of the IPv6 header fields.
421          */
422         ip6->ip6_flow = (ip6->ip6_flow & ~IPV6_FLOWINFO_MASK) |
423             (in6p->in6p_flowinfo & IPV6_FLOWINFO_MASK);
424         ip6->ip6_vfc = (ip6->ip6_vfc & ~IPV6_VERSION_MASK) |
425             (IPV6_VERSION & IPV6_VERSION_MASK);
426
427         /*
428          * ip6_plen will be filled in ip6_output, so not fill it here.
429          */
430         ip6->ip6_nxt = in6p->in6p_ip6_nxt;
431         ip6->ip6_hlim = in6_selecthlim(in6p, oifp);
432
433         if (so->so_proto->pr_protocol == IPPROTO_ICMPV6 ||
434             in6p->in6p_cksum != -1) {
435                 struct mbuf *n;
436                 int off;
437                 u_int16_t *p;
438
439                 /* Compute checksum. */
440                 if (so->so_proto->pr_protocol == IPPROTO_ICMPV6)
441                         off = offsetof(struct icmp6_hdr, icmp6_cksum);
442                 else
443                         off = in6p->in6p_cksum;
444                 if (plen < off + 1) {
445                         error = EINVAL;
446                         goto bad;
447                 }
448                 off += sizeof(struct ip6_hdr);
449
450                 n = m;
451                 while (n && n->m_len <= off) {
452                         off -= n->m_len;
453                         n = n->m_next;
454                 }
455                 if (!n)
456                         goto bad;
457                 p = (u_int16_t *)(mtod(n, caddr_t) + off);
458                 *p = 0;
459                 *p = in6_cksum(m, ip6->ip6_nxt, sizeof(*ip6), plen);
460         }
461
462         error = ip6_output(m, optp, NULL, 0, in6p->in6p_moptions, &oifp, in6p);
463         if (so->so_proto->pr_protocol == IPPROTO_ICMPV6) {
464                 if (oifp)
465                         icmp6_ifoutstat_inc(oifp, type, code);
466                 V_icmp6stat.icp6s_outhist[type]++;
467         } else
468                 V_rip6stat.rip6s_opackets++;
469
470         goto freectl;
471
472  bad:
473         if (m)
474                 m_freem(m);
475
476  freectl:
477         if (control) {
478                 ip6_clearpktopts(&opt, -1);
479                 m_freem(control);
480         }
481         INP_WUNLOCK(in6p);
482         return (error);
483 }
484
485 /*
486  * Raw IPv6 socket option processing.
487  */
488 int
489 rip6_ctloutput(struct socket *so, struct sockopt *sopt)
490 {
491         int error;
492
493         if (sopt->sopt_level == IPPROTO_ICMPV6)
494                 /*
495                  * XXX: is it better to call icmp6_ctloutput() directly
496                  * from protosw?
497                  */
498                 return (icmp6_ctloutput(so, sopt));
499         else if (sopt->sopt_level != IPPROTO_IPV6)
500                 return (EINVAL);
501
502         error = 0;
503
504         switch (sopt->sopt_dir) {
505         case SOPT_GET:
506                 switch (sopt->sopt_name) {
507                 case MRT6_INIT:
508                 case MRT6_DONE:
509                 case MRT6_ADD_MIF:
510                 case MRT6_DEL_MIF:
511                 case MRT6_ADD_MFC:
512                 case MRT6_DEL_MFC:
513                 case MRT6_PIM:
514                         error = ip6_mrouter_get ?  ip6_mrouter_get(so, sopt) :
515                             EOPNOTSUPP;
516                         break;
517                 case IPV6_CHECKSUM:
518                         error = ip6_raw_ctloutput(so, sopt);
519                         break;
520                 default:
521                         error = ip6_ctloutput(so, sopt);
522                         break;
523                 }
524                 break;
525
526         case SOPT_SET:
527                 switch (sopt->sopt_name) {
528                 case MRT6_INIT:
529                 case MRT6_DONE:
530                 case MRT6_ADD_MIF:
531                 case MRT6_DEL_MIF:
532                 case MRT6_ADD_MFC:
533                 case MRT6_DEL_MFC:
534                 case MRT6_PIM:
535                         error = ip6_mrouter_set ?  ip6_mrouter_set(so, sopt) :
536                             EOPNOTSUPP;
537                         break;
538                 case IPV6_CHECKSUM:
539                         error = ip6_raw_ctloutput(so, sopt);
540                         break;
541                 default:
542                         error = ip6_ctloutput(so, sopt);
543                         break;
544                 }
545                 break;
546         }
547
548         return (error);
549 }
550
551 static int
552 rip6_attach(struct socket *so, int proto, struct thread *td)
553 {
554         INIT_VNET_INET(so->so_vnet);
555         struct inpcb *inp;
556         struct icmp6_filter *filter;
557         int error;
558
559         inp = sotoinpcb(so);
560         KASSERT(inp == NULL, ("rip6_attach: inp != NULL"));
561
562         error = priv_check(td, PRIV_NETINET_RAW);
563         if (error)
564                 return (error);
565         error = soreserve(so, rip_sendspace, rip_recvspace);
566         if (error)
567                 return (error);
568         MALLOC(filter, struct icmp6_filter *,
569                sizeof(struct icmp6_filter), M_PCB, M_NOWAIT);
570         if (filter == NULL)
571                 return (ENOMEM);
572         INP_INFO_WLOCK(&V_ripcbinfo);
573         error = in_pcballoc(so, &V_ripcbinfo);
574         if (error) {
575                 INP_INFO_WUNLOCK(&V_ripcbinfo);
576                 FREE(filter, M_PCB);
577                 return (error);
578         }
579         inp = (struct inpcb *)so->so_pcb;
580         INP_INFO_WUNLOCK(&V_ripcbinfo);
581         inp->inp_vflag |= INP_IPV6;
582         inp->in6p_ip6_nxt = (long)proto;
583         inp->in6p_hops = -1;    /* use kernel default */
584         inp->in6p_cksum = -1;
585         inp->in6p_icmp6filt = filter;
586         ICMP6_FILTER_SETPASSALL(inp->in6p_icmp6filt);
587         INP_WUNLOCK(inp);
588         return (0);
589 }
590
591 static void
592 rip6_detach(struct socket *so)
593 {
594         INIT_VNET_INET(so->so_vnet);
595         struct inpcb *inp;
596
597         inp = sotoinpcb(so);
598         KASSERT(inp != NULL, ("rip6_detach: inp == NULL"));
599
600         if (so == ip6_mrouter && ip6_mrouter_done)
601                 ip6_mrouter_done();
602         /* xxx: RSVP */
603         INP_INFO_WLOCK(&V_ripcbinfo);
604         INP_WLOCK(inp);
605         FREE(inp->in6p_icmp6filt, M_PCB);
606         in6_pcbdetach(inp);
607         in6_pcbfree(inp);
608         INP_INFO_WUNLOCK(&V_ripcbinfo);
609 }
610
611 /* XXXRW: This can't ever be called. */
612 static void
613 rip6_abort(struct socket *so)
614 {
615         struct inpcb *inp;
616
617         inp = sotoinpcb(so);
618         KASSERT(inp != NULL, ("rip6_abort: inp == NULL"));
619
620         soisdisconnected(so);
621 }
622
623 static void
624 rip6_close(struct socket *so)
625 {
626         struct inpcb *inp;
627
628         inp = sotoinpcb(so);
629         KASSERT(inp != NULL, ("rip6_close: inp == NULL"));
630
631         soisdisconnected(so);
632 }
633
634 static int
635 rip6_disconnect(struct socket *so)
636 {
637         struct inpcb *inp;
638
639         inp = sotoinpcb(so);
640         KASSERT(inp != NULL, ("rip6_disconnect: inp == NULL"));
641
642         if ((so->so_state & SS_ISCONNECTED) == 0)
643                 return (ENOTCONN);
644         inp->in6p_faddr = in6addr_any;
645         rip6_abort(so);
646         return (0);
647 }
648
649 static int
650 rip6_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
651 {
652         INIT_VNET_NET(so->so_vnet);
653         INIT_VNET_INET(so->so_vnet);
654         INIT_VNET_INET6(so->so_vnet);
655         struct inpcb *inp;
656         struct sockaddr_in6 *addr = (struct sockaddr_in6 *)nam;
657         struct ifaddr *ia = NULL;
658         int error = 0;
659
660         inp = sotoinpcb(so);
661         KASSERT(inp != NULL, ("rip6_bind: inp == NULL"));
662
663         if (nam->sa_len != sizeof(*addr))
664                 return (EINVAL);
665         if (TAILQ_EMPTY(&V_ifnet) || addr->sin6_family != AF_INET6)
666                 return (EADDRNOTAVAIL);
667         if ((error = sa6_embedscope(addr, V_ip6_use_defzone)) != 0)
668                 return (error);
669
670         if (!IN6_IS_ADDR_UNSPECIFIED(&addr->sin6_addr) &&
671             (ia = ifa_ifwithaddr((struct sockaddr *)addr)) == 0)
672                 return (EADDRNOTAVAIL);
673         if (ia &&
674             ((struct in6_ifaddr *)ia)->ia6_flags &
675             (IN6_IFF_ANYCAST|IN6_IFF_NOTREADY|
676              IN6_IFF_DETACHED|IN6_IFF_DEPRECATED)) {
677                 return (EADDRNOTAVAIL);
678         }
679         INP_INFO_WLOCK(&V_ripcbinfo);
680         INP_WLOCK(inp);
681         inp->in6p_laddr = addr->sin6_addr;
682         INP_WUNLOCK(inp);
683         INP_INFO_WUNLOCK(&V_ripcbinfo);
684         return (0);
685 }
686
687 static int
688 rip6_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
689 {
690         INIT_VNET_NET(so->so_vnet);
691         INIT_VNET_INET(so->so_vnet);
692         INIT_VNET_INET6(so->so_vnet);
693         struct inpcb *inp;
694         struct sockaddr_in6 *addr = (struct sockaddr_in6 *)nam;
695         struct in6_addr *in6a = NULL;
696         struct ifnet *ifp = NULL;
697         int error = 0, scope_ambiguous = 0;
698
699         inp = sotoinpcb(so);
700         KASSERT(inp != NULL, ("rip6_connect: inp == NULL"));
701
702         if (nam->sa_len != sizeof(*addr))
703                 return (EINVAL);
704         if (TAILQ_EMPTY(&V_ifnet))
705                 return (EADDRNOTAVAIL);
706         if (addr->sin6_family != AF_INET6)
707                 return (EAFNOSUPPORT);
708
709         /*
710          * Application should provide a proper zone ID or the use of default
711          * zone IDs should be enabled.  Unfortunately, some applications do
712          * not behave as it should, so we need a workaround.  Even if an
713          * appropriate ID is not determined, we'll see if we can determine
714          * the outgoing interface.  If we can, determine the zone ID based on
715          * the interface below.
716          */
717         if (addr->sin6_scope_id == 0 && !V_ip6_use_defzone)
718                 scope_ambiguous = 1;
719         if ((error = sa6_embedscope(addr, V_ip6_use_defzone)) != 0)
720                 return (error);
721
722         INP_INFO_WLOCK(&V_ripcbinfo);
723         INP_WLOCK(inp);
724         /* Source address selection. XXX: need pcblookup? */
725         in6a = in6_selectsrc(addr, inp->in6p_outputopts,
726                              inp, NULL, so->so_cred,
727                              &ifp, &error);
728         if (in6a == NULL) {
729                 INP_WUNLOCK(inp);
730                 INP_INFO_WUNLOCK(&V_ripcbinfo);
731                 return (error ? error : EADDRNOTAVAIL);
732         }
733
734         /* XXX: see above */
735         if (ifp && scope_ambiguous &&
736             (error = in6_setscope(&addr->sin6_addr, ifp, NULL)) != 0) {
737                 INP_WUNLOCK(inp);
738                 INP_INFO_WUNLOCK(&V_ripcbinfo);
739                 return (error);
740         }
741         inp->in6p_faddr = addr->sin6_addr;
742         inp->in6p_laddr = *in6a;
743         soisconnected(so);
744         INP_WUNLOCK(inp);
745         INP_INFO_WUNLOCK(&V_ripcbinfo);
746         return (0);
747 }
748
749 static int
750 rip6_shutdown(struct socket *so)
751 {
752         struct inpcb *inp;
753
754         inp = sotoinpcb(so);
755         KASSERT(inp != NULL, ("rip6_shutdown: inp == NULL"));
756
757         INP_WLOCK(inp);
758         socantsendmore(so);
759         INP_WUNLOCK(inp);
760         return (0);
761 }
762
763 static int
764 rip6_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
765     struct mbuf *control, struct thread *td)
766 {
767         INIT_VNET_INET(so->so_vnet);
768         struct inpcb *inp;
769         struct sockaddr_in6 tmp;
770         struct sockaddr_in6 *dst;
771         int ret;
772
773         inp = sotoinpcb(so);
774         KASSERT(inp != NULL, ("rip6_send: inp == NULL"));
775
776         /* Always copy sockaddr to avoid overwrites. */
777         /* Unlocked read. */
778         if (so->so_state & SS_ISCONNECTED) {
779                 if (nam) {
780                         m_freem(m);
781                         return (EISCONN);
782                 }
783                 /* XXX */
784                 bzero(&tmp, sizeof(tmp));
785                 tmp.sin6_family = AF_INET6;
786                 tmp.sin6_len = sizeof(struct sockaddr_in6);
787                 INP_RLOCK(inp);
788                 bcopy(&inp->in6p_faddr, &tmp.sin6_addr,
789                     sizeof(struct in6_addr));
790                 INP_RUNLOCK(inp);
791                 dst = &tmp;
792         } else {
793                 if (nam == NULL) {
794                         m_freem(m);
795                         return (ENOTCONN);
796                 }
797                 if (nam->sa_len != sizeof(struct sockaddr_in6)) {
798                         m_freem(m);
799                         return (EINVAL);
800                 }
801                 tmp = *(struct sockaddr_in6 *)nam;
802                 dst = &tmp;
803
804                 if (dst->sin6_family == AF_UNSPEC) {
805                         /*
806                          * XXX: we allow this case for backward
807                          * compatibility to buggy applications that
808                          * rely on old (and wrong) kernel behavior.
809                          */
810                         log(LOG_INFO, "rip6 SEND: address family is "
811                             "unspec. Assume AF_INET6\n");
812                         dst->sin6_family = AF_INET6;
813                 } else if (dst->sin6_family != AF_INET6) {
814                         m_freem(m);
815                         return(EAFNOSUPPORT);
816                 }
817         }
818         ret = rip6_output(m, so, dst, control);
819         return (ret);
820 }
821
822 struct pr_usrreqs rip6_usrreqs = {
823         .pru_abort =            rip6_abort,
824         .pru_attach =           rip6_attach,
825         .pru_bind =             rip6_bind,
826         .pru_connect =          rip6_connect,
827         .pru_control =          in6_control,
828         .pru_detach =           rip6_detach,
829         .pru_disconnect =       rip6_disconnect,
830         .pru_peeraddr =         in6_getpeeraddr,
831         .pru_send =             rip6_send,
832         .pru_shutdown =         rip6_shutdown,
833         .pru_sockaddr =         in6_getsockaddr,
834         .pru_close =            rip6_close,
835 };