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