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