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