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