[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ]

FreeBSD/Linux Kernel Cross Reference
sys/netinet6/raw_ip6.c

Version: -  FREEBSD  -  FREEBSD7  -  FREEBSD70  -  FREEBSD6  -  FREEBSD64  -  FREEBSD63  -  FREEBSD62  -  FREEBSD61  -  FREEBSD60  -  FREEBSD5  -  FREEBSD55  -  FREEBSD54  -  FREEBSD53  -  FREEBSD52  -  FREEBSD51  -  FREEBSD50  -  FREEBSD4  -  FREEBSD3  -  FREEBSD22  -  linux-2.6  -  linux-2.4.22  -  MK83  -  MK84  -  PLAN9  -  DFBSD  -  NETBSD  -  NETBSD5  -  NETBSD4  -  NETBSD3  -  NETBSD20  -  OPENBSD  -  xnu-517  -  xnu-792  -  xnu-792.6.70  -  xnu-1228  -  OPENSOLARIS  -  minix-3-1-1  -  TRUSTEDBSD-SEBSD  -  FREEBSD-LIBC  -  FREEBSD7-LIBC  -  FREEBSD6-LIBC  -  GLIBC27 
SearchContext: -  none  -  excerpts  -  bigexcerpts 

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

[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.