1 /*-
2 * SPDX-License-Identifier: BSD-3-Clause
3 *
4 * Copyright (c) 1982, 1986, 1988, 1993
5 * The Regents of the University of California.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 *
32 * @(#)raw_ip.c 8.7 (Berkeley) 5/15/95
33 */
34
35 #include <sys/cdefs.h>
36 #include "opt_inet.h"
37 #include "opt_inet6.h"
38 #include "opt_ipsec.h"
39 #include "opt_route.h"
40
41 #include <sys/param.h>
42 #include <sys/jail.h>
43 #include <sys/kernel.h>
44 #include <sys/eventhandler.h>
45 #include <sys/lock.h>
46 #include <sys/malloc.h>
47 #include <sys/mbuf.h>
48 #include <sys/priv.h>
49 #include <sys/proc.h>
50 #include <sys/protosw.h>
51 #include <sys/rmlock.h>
52 #include <sys/rwlock.h>
53 #include <sys/signalvar.h>
54 #include <sys/socket.h>
55 #include <sys/socketvar.h>
56 #include <sys/sx.h>
57 #include <sys/sysctl.h>
58 #include <sys/systm.h>
59
60 #include <vm/uma.h>
61
62 #include <net/if.h>
63 #include <net/if_var.h>
64 #include <net/route.h>
65 #include <net/route/route_ctl.h>
66 #include <net/vnet.h>
67
68 #include <netinet/in.h>
69 #include <netinet/in_systm.h>
70 #include <netinet/in_fib.h>
71 #include <netinet/in_pcb.h>
72 #include <netinet/in_var.h>
73 #include <netinet/if_ether.h>
74 #include <netinet/ip.h>
75 #include <netinet/ip_var.h>
76 #include <netinet/ip_mroute.h>
77 #include <netinet/ip_icmp.h>
78
79 #include <netipsec/ipsec_support.h>
80
81 #include <machine/stdarg.h>
82 #include <security/mac/mac_framework.h>
83
84 VNET_DEFINE(int, ip_defttl) = IPDEFTTL;
85 SYSCTL_INT(_net_inet_ip, IPCTL_DEFTTL, ttl, CTLFLAG_VNET | CTLFLAG_RW,
86 &VNET_NAME(ip_defttl), 0,
87 "Maximum TTL on IP packets");
88
89 VNET_DEFINE(struct inpcbhead, ripcb);
90 VNET_DEFINE(struct inpcbinfo, ripcbinfo);
91
92 #define V_ripcb VNET(ripcb)
93 #define V_ripcbinfo VNET(ripcbinfo)
94
95 /*
96 * Control and data hooks for ipfw, dummynet, divert and so on.
97 * The data hooks are not used here but it is convenient
98 * to keep them all in one place.
99 */
100 VNET_DEFINE(ip_fw_chk_ptr_t, ip_fw_chk_ptr) = NULL;
101 VNET_DEFINE(ip_fw_ctl_ptr_t, ip_fw_ctl_ptr) = NULL;
102
103 int (*ip_dn_ctl_ptr)(struct sockopt *);
104 int (*ip_dn_io_ptr)(struct mbuf **, struct ip_fw_args *);
105 void (*ip_divert_ptr)(struct mbuf *, bool);
106 int (*ng_ipfw_input_p)(struct mbuf **, struct ip_fw_args *, bool);
107
108 #ifdef INET
109 /*
110 * Hooks for multicast routing. They all default to NULL, so leave them not
111 * initialized and rely on BSS being set to 0.
112 */
113
114 /*
115 * The socket used to communicate with the multicast routing daemon.
116 */
117 VNET_DEFINE(struct socket *, ip_mrouter);
118
119 /*
120 * The various mrouter and rsvp functions.
121 */
122 int (*ip_mrouter_set)(struct socket *, struct sockopt *);
123 int (*ip_mrouter_get)(struct socket *, struct sockopt *);
124 int (*ip_mrouter_done)(void);
125 int (*ip_mforward)(struct ip *, struct ifnet *, struct mbuf *,
126 struct ip_moptions *);
127 int (*mrt_ioctl)(u_long, caddr_t, int);
128 int (*legal_vif_num)(int);
129 u_long (*ip_mcast_src)(int);
130
131 int (*rsvp_input_p)(struct mbuf **, int *, int);
132 int (*ip_rsvp_vif)(struct socket *, struct sockopt *);
133 void (*ip_rsvp_force_done)(struct socket *);
134 #endif /* INET */
135
136 extern struct protosw inetsw[];
137
138 u_long rip_sendspace = 9216;
139 SYSCTL_ULONG(_net_inet_raw, OID_AUTO, maxdgram, CTLFLAG_RW,
140 &rip_sendspace, 0, "Maximum outgoing raw IP datagram size");
141
142 u_long rip_recvspace = 9216;
143 SYSCTL_ULONG(_net_inet_raw, OID_AUTO, recvspace, CTLFLAG_RW,
144 &rip_recvspace, 0, "Maximum space for incoming raw IP datagrams");
145
146 /*
147 * Hash functions
148 */
149
150 #define INP_PCBHASH_RAW_SIZE 256
151 #define INP_PCBHASH_RAW(proto, laddr, faddr, mask) \
152 (((proto) + (laddr) + (faddr)) % (mask) + 1)
153
154 #ifdef INET
155 static void
rip_inshash(struct inpcb * inp)156 rip_inshash(struct inpcb *inp)
157 {
158 struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
159 struct inpcbhead *pcbhash;
160 int hash;
161
162 INP_INFO_WLOCK_ASSERT(pcbinfo);
163 INP_WLOCK_ASSERT(inp);
164
165 if (inp->inp_ip_p != 0 &&
166 inp->inp_laddr.s_addr != INADDR_ANY &&
167 inp->inp_faddr.s_addr != INADDR_ANY) {
168 hash = INP_PCBHASH_RAW(inp->inp_ip_p, inp->inp_laddr.s_addr,
169 inp->inp_faddr.s_addr, pcbinfo->ipi_hashmask);
170 } else
171 hash = 0;
172 pcbhash = &pcbinfo->ipi_hashbase[hash];
173 CK_LIST_INSERT_HEAD(pcbhash, inp, inp_hash);
174 }
175
176 static void
rip_delhash(struct inpcb * inp)177 rip_delhash(struct inpcb *inp)
178 {
179
180 INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
181 INP_WLOCK_ASSERT(inp);
182
183 CK_LIST_REMOVE(inp, inp_hash);
184 }
185 #endif /* INET */
186
187 /*
188 * Raw interface to IP protocol.
189 */
190
191 /*
192 * Initialize raw connection block q.
193 */
194 static void
rip_zone_change(void * tag)195 rip_zone_change(void *tag)
196 {
197
198 uma_zone_set_max(V_ripcbinfo.ipi_zone, maxsockets);
199 }
200
201 static int
rip_inpcb_init(void * mem,int size,int flags)202 rip_inpcb_init(void *mem, int size, int flags)
203 {
204 struct inpcb *inp = mem;
205
206 INP_LOCK_INIT(inp, "inp", "rawinp");
207 return (0);
208 }
209
210 void
rip_init(void)211 rip_init(void)
212 {
213
214 in_pcbinfo_init(&V_ripcbinfo, "rip", &V_ripcb, INP_PCBHASH_RAW_SIZE,
215 1, "ripcb", rip_inpcb_init, IPI_HASHFIELDS_NONE);
216 EVENTHANDLER_REGISTER(maxsockets_change, rip_zone_change, NULL,
217 EVENTHANDLER_PRI_ANY);
218 }
219
220 #ifdef VIMAGE
221 static void
rip_destroy(void * unused __unused)222 rip_destroy(void *unused __unused)
223 {
224
225 in_pcbinfo_destroy(&V_ripcbinfo);
226 }
227 VNET_SYSUNINIT(raw_ip, SI_SUB_PROTO_DOMAIN, SI_ORDER_FOURTH, rip_destroy, NULL);
228 #endif
229
230 #ifdef INET
231 static int
rip_append(struct inpcb * last,struct ip * ip,struct mbuf * n,struct sockaddr_in * ripsrc)232 rip_append(struct inpcb *last, struct ip *ip, struct mbuf *n,
233 struct sockaddr_in *ripsrc)
234 {
235 int policyfail = 0;
236
237 INP_LOCK_ASSERT(last);
238
239 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
240 /* check AH/ESP integrity. */
241 if (IPSEC_ENABLED(ipv4)) {
242 if (IPSEC_CHECK_POLICY(ipv4, n, last) != 0)
243 policyfail = 1;
244 }
245 #endif /* IPSEC */
246 #ifdef MAC
247 if (!policyfail && mac_inpcb_check_deliver(last, n) != 0)
248 policyfail = 1;
249 #endif
250 /* Check the minimum TTL for socket. */
251 if (last->inp_ip_minttl && last->inp_ip_minttl > ip->ip_ttl)
252 policyfail = 1;
253 if (!policyfail) {
254 struct mbuf *opts = NULL;
255 struct socket *so;
256
257 so = last->inp_socket;
258 if ((last->inp_flags & INP_CONTROLOPTS) ||
259 (so->so_options & (SO_TIMESTAMP | SO_BINTIME)))
260 ip_savecontrol(last, &opts, ip, n);
261 SOCKBUF_LOCK(&so->so_rcv);
262 if (sbappendaddr_locked(&so->so_rcv,
263 (struct sockaddr *)ripsrc, n, opts) == 0) {
264 soroverflow_locked(so);
265 m_freem(n);
266 if (opts)
267 m_freem(opts);
268 } else
269 sorwakeup_locked(so);
270 } else
271 m_freem(n);
272 return (policyfail);
273 }
274
275 /*
276 * Setup generic address and protocol structures for raw_input routine, then
277 * pass them along with mbuf chain.
278 */
279 int
rip_input(struct mbuf ** mp,int * offp,int proto)280 rip_input(struct mbuf **mp, int *offp, int proto)
281 {
282 struct ifnet *ifp;
283 struct mbuf *m = *mp;
284 struct ip *ip = mtod(m, struct ip *);
285 struct inpcb *inp, *last;
286 struct sockaddr_in ripsrc;
287 int hash;
288
289 NET_EPOCH_ASSERT();
290
291 *mp = NULL;
292
293 bzero(&ripsrc, sizeof(ripsrc));
294 ripsrc.sin_len = sizeof(ripsrc);
295 ripsrc.sin_family = AF_INET;
296 ripsrc.sin_addr = ip->ip_src;
297 last = NULL;
298
299 ifp = m->m_pkthdr.rcvif;
300
301 hash = INP_PCBHASH_RAW(proto, ip->ip_src.s_addr,
302 ip->ip_dst.s_addr, V_ripcbinfo.ipi_hashmask);
303 CK_LIST_FOREACH(inp, &V_ripcbinfo.ipi_hashbase[hash], inp_hash) {
304 if (inp->inp_ip_p != proto)
305 continue;
306 #ifdef INET6
307 /* XXX inp locking */
308 if ((inp->inp_vflag & INP_IPV4) == 0)
309 continue;
310 #endif
311 if (inp->inp_laddr.s_addr != ip->ip_dst.s_addr)
312 continue;
313 if (inp->inp_faddr.s_addr != ip->ip_src.s_addr)
314 continue;
315 if (last != NULL) {
316 struct mbuf *n;
317
318 n = m_copym(m, 0, M_COPYALL, M_NOWAIT);
319 if (n != NULL)
320 (void) rip_append(last, ip, n, &ripsrc);
321 /* XXX count dropped packet */
322 INP_RUNLOCK(last);
323 last = NULL;
324 }
325 INP_RLOCK(inp);
326 if (__predict_false(inp->inp_flags2 & INP_FREED))
327 goto skip_1;
328 if (jailed_without_vnet(inp->inp_cred)) {
329 /*
330 * XXX: If faddr was bound to multicast group,
331 * jailed raw socket will drop datagram.
332 */
333 if (prison_check_ip4(inp->inp_cred, &ip->ip_dst) != 0)
334 goto skip_1;
335 }
336 last = inp;
337 continue;
338 skip_1:
339 INP_RUNLOCK(inp);
340 }
341 CK_LIST_FOREACH(inp, &V_ripcbinfo.ipi_hashbase[0], inp_hash) {
342 if (inp->inp_ip_p && inp->inp_ip_p != proto)
343 continue;
344 #ifdef INET6
345 /* XXX inp locking */
346 if ((inp->inp_vflag & INP_IPV4) == 0)
347 continue;
348 #endif
349 if (!in_nullhost(inp->inp_laddr) &&
350 !in_hosteq(inp->inp_laddr, ip->ip_dst))
351 continue;
352 if (!in_nullhost(inp->inp_faddr) &&
353 !in_hosteq(inp->inp_faddr, ip->ip_src))
354 continue;
355 if (last != NULL) {
356 struct mbuf *n;
357
358 n = m_copym(m, 0, M_COPYALL, M_NOWAIT);
359 if (n != NULL)
360 (void) rip_append(last, ip, n, &ripsrc);
361 /* XXX count dropped packet */
362 INP_RUNLOCK(last);
363 last = NULL;
364 }
365 INP_RLOCK(inp);
366 if (__predict_false(inp->inp_flags2 & INP_FREED))
367 goto skip_2;
368 if (jailed_without_vnet(inp->inp_cred)) {
369 /*
370 * Allow raw socket in jail to receive multicast;
371 * assume process had PRIV_NETINET_RAW at attach,
372 * and fall through into normal filter path if so.
373 */
374 if (!IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) &&
375 prison_check_ip4(inp->inp_cred, &ip->ip_dst) != 0)
376 goto skip_2;
377 }
378 /*
379 * If this raw socket has multicast state, and we
380 * have received a multicast, check if this socket
381 * should receive it, as multicast filtering is now
382 * the responsibility of the transport layer.
383 */
384 if (inp->inp_moptions != NULL &&
385 IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) {
386 /*
387 * If the incoming datagram is for IGMP, allow it
388 * through unconditionally to the raw socket.
389 *
390 * In the case of IGMPv2, we may not have explicitly
391 * joined the group, and may have set IFF_ALLMULTI
392 * on the interface. imo_multi_filter() may discard
393 * control traffic we actually need to see.
394 *
395 * Userland multicast routing daemons should continue
396 * filter the control traffic appropriately.
397 */
398 int blocked;
399
400 blocked = MCAST_PASS;
401 if (proto != IPPROTO_IGMP) {
402 struct sockaddr_in group;
403
404 bzero(&group, sizeof(struct sockaddr_in));
405 group.sin_len = sizeof(struct sockaddr_in);
406 group.sin_family = AF_INET;
407 group.sin_addr = ip->ip_dst;
408
409 blocked = imo_multi_filter(inp->inp_moptions,
410 ifp,
411 (struct sockaddr *)&group,
412 (struct sockaddr *)&ripsrc);
413 }
414
415 if (blocked != MCAST_PASS) {
416 IPSTAT_INC(ips_notmember);
417 goto skip_2;
418 }
419 }
420 last = inp;
421 continue;
422 skip_2:
423 INP_RUNLOCK(inp);
424 }
425 if (last != NULL) {
426 if (rip_append(last, ip, m, &ripsrc) != 0)
427 IPSTAT_INC(ips_delivered);
428 INP_RUNLOCK(last);
429 } else {
430 if (inetsw[ip_protox[ip->ip_p]].pr_input == rip_input) {
431 IPSTAT_INC(ips_noproto);
432 IPSTAT_DEC(ips_delivered);
433 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PROTOCOL, 0, 0);
434 } else {
435 m_freem(m);
436 }
437 }
438 return (IPPROTO_DONE);
439 }
440
441 /*
442 * Generate IP header and pass packet to ip_output. Tack on options user may
443 * have setup with control call.
444 */
445 int
rip_output(struct mbuf * m,struct socket * so,...)446 rip_output(struct mbuf *m, struct socket *so, ...)
447 {
448 struct epoch_tracker et;
449 struct ip *ip;
450 int error;
451 struct inpcb *inp = sotoinpcb(so);
452 va_list ap;
453 u_long dst;
454 int flags = ((so->so_options & SO_DONTROUTE) ? IP_ROUTETOIF : 0) |
455 IP_ALLOWBROADCAST;
456 int cnt, hlen;
457 u_char opttype, optlen, *cp;
458
459 va_start(ap, so);
460 dst = va_arg(ap, u_long);
461 va_end(ap);
462
463 /*
464 * If the user handed us a complete IP packet, use it. Otherwise,
465 * allocate an mbuf for a header and fill it in.
466 */
467 if ((inp->inp_flags & INP_HDRINCL) == 0) {
468 if (m->m_pkthdr.len + sizeof(struct ip) > IP_MAXPACKET) {
469 m_freem(m);
470 return(EMSGSIZE);
471 }
472 M_PREPEND(m, sizeof(struct ip), M_NOWAIT);
473 if (m == NULL)
474 return(ENOBUFS);
475
476 INP_RLOCK(inp);
477 ip = mtod(m, struct ip *);
478 ip->ip_tos = inp->inp_ip_tos;
479 if (inp->inp_flags & INP_DONTFRAG)
480 ip->ip_off = htons(IP_DF);
481 else
482 ip->ip_off = htons(0);
483 ip->ip_p = inp->inp_ip_p;
484 ip->ip_len = htons(m->m_pkthdr.len);
485 ip->ip_src = inp->inp_laddr;
486 ip->ip_dst.s_addr = dst;
487 #ifdef ROUTE_MPATH
488 if (CALC_FLOWID_OUTBOUND) {
489 uint32_t hash_type, hash_val;
490
491 hash_val = fib4_calc_software_hash(ip->ip_src,
492 ip->ip_dst, 0, 0, ip->ip_p, &hash_type);
493 m->m_pkthdr.flowid = hash_val;
494 M_HASHTYPE_SET(m, hash_type);
495 flags |= IP_NODEFAULTFLOWID;
496 }
497 #endif
498 if (jailed(inp->inp_cred)) {
499 /*
500 * prison_local_ip4() would be good enough but would
501 * let a source of INADDR_ANY pass, which we do not
502 * want to see from jails.
503 */
504 if (ip->ip_src.s_addr == INADDR_ANY) {
505 NET_EPOCH_ENTER(et);
506 error = in_pcbladdr(inp, &ip->ip_dst,
507 &ip->ip_src, inp->inp_cred);
508 NET_EPOCH_EXIT(et);
509 } else {
510 error = prison_local_ip4(inp->inp_cred,
511 &ip->ip_src);
512 }
513 if (error != 0) {
514 INP_RUNLOCK(inp);
515 m_freem(m);
516 return (error);
517 }
518 }
519 ip->ip_ttl = inp->inp_ip_ttl;
520 } else {
521 if (m->m_pkthdr.len > IP_MAXPACKET) {
522 m_freem(m);
523 return (EMSGSIZE);
524 }
525 if (m->m_pkthdr.len < sizeof(*ip)) {
526 m_freem(m);
527 return (EINVAL);
528 }
529 m = m_pullup(m, sizeof(*ip));
530 if (m == NULL)
531 return (ENOMEM);
532 ip = mtod(m, struct ip *);
533 hlen = ip->ip_hl << 2;
534 if (m->m_len < hlen) {
535 m = m_pullup(m, hlen);
536 if (m == NULL)
537 return (EINVAL);
538 ip = mtod(m, struct ip *);
539 }
540 #ifdef ROUTE_MPATH
541 if (CALC_FLOWID_OUTBOUND) {
542 uint32_t hash_type, hash_val;
543
544 hash_val = fib4_calc_software_hash(ip->ip_dst,
545 ip->ip_src, 0, 0, ip->ip_p, &hash_type);
546 m->m_pkthdr.flowid = hash_val;
547 M_HASHTYPE_SET(m, hash_type);
548 flags |= IP_NODEFAULTFLOWID;
549 }
550 #endif
551 INP_RLOCK(inp);
552 /*
553 * Don't allow both user specified and setsockopt options,
554 * and don't allow packet length sizes that will crash.
555 */
556 if ((hlen < sizeof (*ip))
557 || ((hlen > sizeof (*ip)) && inp->inp_options)
558 || (ntohs(ip->ip_len) != m->m_pkthdr.len)) {
559 INP_RUNLOCK(inp);
560 m_freem(m);
561 return (EINVAL);
562 }
563 error = prison_check_ip4(inp->inp_cred, &ip->ip_src);
564 if (error != 0) {
565 INP_RUNLOCK(inp);
566 m_freem(m);
567 return (error);
568 }
569 /*
570 * Don't allow IP options which do not have the required
571 * structure as specified in section 3.1 of RFC 791 on
572 * pages 15-23.
573 */
574 cp = (u_char *)(ip + 1);
575 cnt = hlen - sizeof (struct ip);
576 for (; cnt > 0; cnt -= optlen, cp += optlen) {
577 opttype = cp[IPOPT_OPTVAL];
578 if (opttype == IPOPT_EOL)
579 break;
580 if (opttype == IPOPT_NOP) {
581 optlen = 1;
582 continue;
583 }
584 if (cnt < IPOPT_OLEN + sizeof(u_char)) {
585 INP_RUNLOCK(inp);
586 m_freem(m);
587 return (EINVAL);
588 }
589 optlen = cp[IPOPT_OLEN];
590 if (optlen < IPOPT_OLEN + sizeof(u_char) ||
591 optlen > cnt) {
592 INP_RUNLOCK(inp);
593 m_freem(m);
594 return (EINVAL);
595 }
596 }
597 /*
598 * This doesn't allow application to specify ID of zero,
599 * but we got this limitation from the beginning of history.
600 */
601 if (ip->ip_id == 0)
602 ip_fillid(ip);
603
604 /*
605 * XXX prevent ip_output from overwriting header fields.
606 */
607 flags |= IP_RAWOUTPUT;
608 IPSTAT_INC(ips_rawout);
609 }
610
611 if (inp->inp_flags & INP_ONESBCAST)
612 flags |= IP_SENDONES;
613
614 #ifdef MAC
615 mac_inpcb_create_mbuf(inp, m);
616 #endif
617
618 NET_EPOCH_ENTER(et);
619 error = ip_output(m, inp->inp_options, NULL, flags,
620 inp->inp_moptions, inp);
621 NET_EPOCH_EXIT(et);
622 INP_RUNLOCK(inp);
623 return (error);
624 }
625
626 /*
627 * Raw IP socket option processing.
628 *
629 * IMPORTANT NOTE regarding access control: Traditionally, raw sockets could
630 * only be created by a privileged process, and as such, socket option
631 * operations to manage system properties on any raw socket were allowed to
632 * take place without explicit additional access control checks. However,
633 * raw sockets can now also be created in jail(), and therefore explicit
634 * checks are now required. Likewise, raw sockets can be used by a process
635 * after it gives up privilege, so some caution is required. For options
636 * passed down to the IP layer via ip_ctloutput(), checks are assumed to be
637 * performed in ip_ctloutput() and therefore no check occurs here.
638 * Unilaterally checking priv_check() here breaks normal IP socket option
639 * operations on raw sockets.
640 *
641 * When adding new socket options here, make sure to add access control
642 * checks here as necessary.
643 *
644 * XXX-BZ inp locking?
645 */
646 int
rip_ctloutput(struct socket * so,struct sockopt * sopt)647 rip_ctloutput(struct socket *so, struct sockopt *sopt)
648 {
649 struct inpcb *inp = sotoinpcb(so);
650 int error, optval;
651
652 if (sopt->sopt_level != IPPROTO_IP) {
653 if ((sopt->sopt_level == SOL_SOCKET) &&
654 (sopt->sopt_name == SO_SETFIB)) {
655 inp->inp_inc.inc_fibnum = so->so_fibnum;
656 return (0);
657 }
658 return (EINVAL);
659 }
660
661 error = 0;
662 switch (sopt->sopt_dir) {
663 case SOPT_GET:
664 switch (sopt->sopt_name) {
665 case IP_HDRINCL:
666 optval = inp->inp_flags & INP_HDRINCL;
667 error = sooptcopyout(sopt, &optval, sizeof optval);
668 break;
669
670 case IP_FW3: /* generic ipfw v.3 functions */
671 case IP_FW_ADD: /* ADD actually returns the body... */
672 case IP_FW_GET:
673 case IP_FW_TABLE_GETSIZE:
674 case IP_FW_TABLE_LIST:
675 case IP_FW_NAT_GET_CONFIG:
676 case IP_FW_NAT_GET_LOG:
677 if (V_ip_fw_ctl_ptr != NULL)
678 error = V_ip_fw_ctl_ptr(sopt);
679 else
680 error = ENOPROTOOPT;
681 break;
682
683 case IP_DUMMYNET3: /* generic dummynet v.3 functions */
684 case IP_DUMMYNET_GET:
685 if (ip_dn_ctl_ptr != NULL)
686 error = ip_dn_ctl_ptr(sopt);
687 else
688 error = ENOPROTOOPT;
689 break ;
690
691 case MRT_INIT:
692 case MRT_DONE:
693 case MRT_ADD_VIF:
694 case MRT_DEL_VIF:
695 case MRT_ADD_MFC:
696 case MRT_DEL_MFC:
697 case MRT_VERSION:
698 case MRT_ASSERT:
699 case MRT_API_SUPPORT:
700 case MRT_API_CONFIG:
701 case MRT_ADD_BW_UPCALL:
702 case MRT_DEL_BW_UPCALL:
703 error = priv_check(curthread, PRIV_NETINET_MROUTE);
704 if (error != 0)
705 return (error);
706 error = ip_mrouter_get ? ip_mrouter_get(so, sopt) :
707 EOPNOTSUPP;
708 break;
709
710 default:
711 error = ip_ctloutput(so, sopt);
712 break;
713 }
714 break;
715
716 case SOPT_SET:
717 switch (sopt->sopt_name) {
718 case IP_HDRINCL:
719 error = sooptcopyin(sopt, &optval, sizeof optval,
720 sizeof optval);
721 if (error)
722 break;
723 if (optval)
724 inp->inp_flags |= INP_HDRINCL;
725 else
726 inp->inp_flags &= ~INP_HDRINCL;
727 break;
728
729 case IP_FW3: /* generic ipfw v.3 functions */
730 case IP_FW_ADD:
731 case IP_FW_DEL:
732 case IP_FW_FLUSH:
733 case IP_FW_ZERO:
734 case IP_FW_RESETLOG:
735 case IP_FW_TABLE_ADD:
736 case IP_FW_TABLE_DEL:
737 case IP_FW_TABLE_FLUSH:
738 case IP_FW_NAT_CFG:
739 case IP_FW_NAT_DEL:
740 if (V_ip_fw_ctl_ptr != NULL)
741 error = V_ip_fw_ctl_ptr(sopt);
742 else
743 error = ENOPROTOOPT;
744 break;
745
746 case IP_DUMMYNET3: /* generic dummynet v.3 functions */
747 case IP_DUMMYNET_CONFIGURE:
748 case IP_DUMMYNET_DEL:
749 case IP_DUMMYNET_FLUSH:
750 if (ip_dn_ctl_ptr != NULL)
751 error = ip_dn_ctl_ptr(sopt);
752 else
753 error = ENOPROTOOPT ;
754 break ;
755
756 case IP_RSVP_ON:
757 error = priv_check(curthread, PRIV_NETINET_MROUTE);
758 if (error != 0)
759 return (error);
760 error = ip_rsvp_init(so);
761 break;
762
763 case IP_RSVP_OFF:
764 error = priv_check(curthread, PRIV_NETINET_MROUTE);
765 if (error != 0)
766 return (error);
767 error = ip_rsvp_done();
768 break;
769
770 case IP_RSVP_VIF_ON:
771 case IP_RSVP_VIF_OFF:
772 error = priv_check(curthread, PRIV_NETINET_MROUTE);
773 if (error != 0)
774 return (error);
775 error = ip_rsvp_vif ?
776 ip_rsvp_vif(so, sopt) : EINVAL;
777 break;
778
779 case MRT_INIT:
780 case MRT_DONE:
781 case MRT_ADD_VIF:
782 case MRT_DEL_VIF:
783 case MRT_ADD_MFC:
784 case MRT_DEL_MFC:
785 case MRT_VERSION:
786 case MRT_ASSERT:
787 case MRT_API_SUPPORT:
788 case MRT_API_CONFIG:
789 case MRT_ADD_BW_UPCALL:
790 case MRT_DEL_BW_UPCALL:
791 error = priv_check(curthread, PRIV_NETINET_MROUTE);
792 if (error != 0)
793 return (error);
794 error = ip_mrouter_set ? ip_mrouter_set(so, sopt) :
795 EOPNOTSUPP;
796 break;
797
798 default:
799 error = ip_ctloutput(so, sopt);
800 break;
801 }
802 break;
803 }
804
805 return (error);
806 }
807
808 /*
809 * This function exists solely to receive the PRC_IFDOWN messages which are
810 * sent by if_down(). It looks for an ifaddr whose ifa_addr is sa, and calls
811 * in_ifadown() to remove all routes corresponding to that address. It also
812 * receives the PRC_IFUP messages from if_up() and reinstalls the interface
813 * routes.
814 */
815 void
rip_ctlinput(int cmd,struct sockaddr * sa,void * vip)816 rip_ctlinput(int cmd, struct sockaddr *sa, void *vip)
817 {
818 struct rm_priotracker in_ifa_tracker;
819 struct in_ifaddr *ia;
820 struct ifnet *ifp;
821 int err;
822 int flags;
823
824 switch (cmd) {
825 case PRC_IFDOWN:
826 IN_IFADDR_RLOCK(&in_ifa_tracker);
827 CK_STAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
828 if (ia->ia_ifa.ifa_addr == sa
829 && (ia->ia_flags & IFA_ROUTE)) {
830 ifa_ref(&ia->ia_ifa);
831 IN_IFADDR_RUNLOCK(&in_ifa_tracker);
832 /*
833 * in_scrubprefix() kills the interface route.
834 */
835 in_scrubprefix(ia, 0);
836 /*
837 * in_ifadown gets rid of all the rest of the
838 * routes. This is not quite the right thing
839 * to do, but at least if we are running a
840 * routing process they will come back.
841 */
842 in_ifadown(&ia->ia_ifa, 0);
843 ifa_free(&ia->ia_ifa);
844 break;
845 }
846 }
847 if (ia == NULL) /* If ia matched, already unlocked. */
848 IN_IFADDR_RUNLOCK(&in_ifa_tracker);
849 break;
850
851 case PRC_IFUP:
852 IN_IFADDR_RLOCK(&in_ifa_tracker);
853 CK_STAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
854 if (ia->ia_ifa.ifa_addr == sa)
855 break;
856 }
857 if (ia == NULL || (ia->ia_flags & IFA_ROUTE)) {
858 IN_IFADDR_RUNLOCK(&in_ifa_tracker);
859 return;
860 }
861 ifa_ref(&ia->ia_ifa);
862 IN_IFADDR_RUNLOCK(&in_ifa_tracker);
863 flags = RTF_UP;
864 ifp = ia->ia_ifa.ifa_ifp;
865
866 if ((ifp->if_flags & IFF_LOOPBACK)
867 || (ifp->if_flags & IFF_POINTOPOINT))
868 flags |= RTF_HOST;
869
870 err = ifa_del_loopback_route((struct ifaddr *)ia, sa);
871
872 rt_addrmsg(RTM_ADD, &ia->ia_ifa, ia->ia_ifp->if_fib);
873 err = in_handle_ifaddr_route(RTM_ADD, ia);
874 if (err == 0)
875 ia->ia_flags |= IFA_ROUTE;
876
877 err = ifa_add_loopback_route((struct ifaddr *)ia, sa);
878
879 ifa_free(&ia->ia_ifa);
880 break;
881 }
882 }
883
884 static int
rip_attach(struct socket * so,int proto,struct thread * td)885 rip_attach(struct socket *so, int proto, struct thread *td)
886 {
887 struct inpcb *inp;
888 int error;
889
890 inp = sotoinpcb(so);
891 KASSERT(inp == NULL, ("rip_attach: inp != NULL"));
892
893 error = priv_check(td, PRIV_NETINET_RAW);
894 if (error)
895 return (error);
896 if (proto >= IPPROTO_MAX || proto < 0)
897 return EPROTONOSUPPORT;
898 error = soreserve(so, rip_sendspace, rip_recvspace);
899 if (error)
900 return (error);
901 INP_INFO_WLOCK(&V_ripcbinfo);
902 error = in_pcballoc(so, &V_ripcbinfo);
903 if (error) {
904 INP_INFO_WUNLOCK(&V_ripcbinfo);
905 return (error);
906 }
907 inp = (struct inpcb *)so->so_pcb;
908 inp->inp_vflag |= INP_IPV4;
909 inp->inp_ip_p = proto;
910 inp->inp_ip_ttl = V_ip_defttl;
911 rip_inshash(inp);
912 INP_INFO_WUNLOCK(&V_ripcbinfo);
913 INP_WUNLOCK(inp);
914 return (0);
915 }
916
917 static void
rip_detach(struct socket * so)918 rip_detach(struct socket *so)
919 {
920 struct inpcb *inp;
921
922 inp = sotoinpcb(so);
923 KASSERT(inp != NULL, ("rip_detach: inp == NULL"));
924 KASSERT(inp->inp_faddr.s_addr == INADDR_ANY,
925 ("rip_detach: not closed"));
926
927 INP_INFO_WLOCK(&V_ripcbinfo);
928 INP_WLOCK(inp);
929 rip_delhash(inp);
930 if (so == V_ip_mrouter && ip_mrouter_done)
931 ip_mrouter_done();
932 if (ip_rsvp_force_done)
933 ip_rsvp_force_done(so);
934 if (so == V_ip_rsvpd)
935 ip_rsvp_done();
936 in_pcbdetach(inp);
937 in_pcbfree(inp);
938 INP_INFO_WUNLOCK(&V_ripcbinfo);
939 }
940
941 static void
rip_dodisconnect(struct socket * so,struct inpcb * inp)942 rip_dodisconnect(struct socket *so, struct inpcb *inp)
943 {
944 struct inpcbinfo *pcbinfo;
945
946 pcbinfo = inp->inp_pcbinfo;
947 INP_INFO_WLOCK(pcbinfo);
948 INP_WLOCK(inp);
949 rip_delhash(inp);
950 inp->inp_faddr.s_addr = INADDR_ANY;
951 rip_inshash(inp);
952 SOCK_LOCK(so);
953 so->so_state &= ~SS_ISCONNECTED;
954 SOCK_UNLOCK(so);
955 INP_WUNLOCK(inp);
956 INP_INFO_WUNLOCK(pcbinfo);
957 }
958
959 static void
rip_abort(struct socket * so)960 rip_abort(struct socket *so)
961 {
962 struct inpcb *inp;
963
964 inp = sotoinpcb(so);
965 KASSERT(inp != NULL, ("rip_abort: inp == NULL"));
966
967 rip_dodisconnect(so, inp);
968 }
969
970 static void
rip_close(struct socket * so)971 rip_close(struct socket *so)
972 {
973 struct inpcb *inp;
974
975 inp = sotoinpcb(so);
976 KASSERT(inp != NULL, ("rip_close: inp == NULL"));
977
978 rip_dodisconnect(so, inp);
979 }
980
981 static int
rip_disconnect(struct socket * so)982 rip_disconnect(struct socket *so)
983 {
984 struct inpcb *inp;
985
986 if ((so->so_state & SS_ISCONNECTED) == 0)
987 return (ENOTCONN);
988
989 inp = sotoinpcb(so);
990 KASSERT(inp != NULL, ("rip_disconnect: inp == NULL"));
991
992 rip_dodisconnect(so, inp);
993 return (0);
994 }
995
996 static int
rip_bind(struct socket * so,struct sockaddr * nam,struct thread * td)997 rip_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
998 {
999 struct sockaddr_in *addr = (struct sockaddr_in *)nam;
1000 struct inpcb *inp;
1001 int error;
1002
1003 if (nam->sa_family != AF_INET)
1004 return (EAFNOSUPPORT);
1005 if (nam->sa_len != sizeof(*addr))
1006 return (EINVAL);
1007
1008 error = prison_check_ip4(td->td_ucred, &addr->sin_addr);
1009 if (error != 0)
1010 return (error);
1011
1012 inp = sotoinpcb(so);
1013 KASSERT(inp != NULL, ("rip_bind: inp == NULL"));
1014
1015 if (CK_STAILQ_EMPTY(&V_ifnet) ||
1016 (addr->sin_family != AF_INET && addr->sin_family != AF_IMPLINK) ||
1017 (addr->sin_addr.s_addr &&
1018 (inp->inp_flags & INP_BINDANY) == 0 &&
1019 ifa_ifwithaddr_check((struct sockaddr *)addr) == 0))
1020 return (EADDRNOTAVAIL);
1021
1022 INP_INFO_WLOCK(&V_ripcbinfo);
1023 INP_WLOCK(inp);
1024 rip_delhash(inp);
1025 inp->inp_laddr = addr->sin_addr;
1026 rip_inshash(inp);
1027 INP_WUNLOCK(inp);
1028 INP_INFO_WUNLOCK(&V_ripcbinfo);
1029 return (0);
1030 }
1031
1032 static int
rip_connect(struct socket * so,struct sockaddr * nam,struct thread * td)1033 rip_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
1034 {
1035 struct sockaddr_in *addr = (struct sockaddr_in *)nam;
1036 struct inpcb *inp;
1037
1038 if (nam->sa_len != sizeof(*addr))
1039 return (EINVAL);
1040 if (CK_STAILQ_EMPTY(&V_ifnet))
1041 return (EADDRNOTAVAIL);
1042 if (addr->sin_family != AF_INET && addr->sin_family != AF_IMPLINK)
1043 return (EAFNOSUPPORT);
1044
1045 inp = sotoinpcb(so);
1046 KASSERT(inp != NULL, ("rip_connect: inp == NULL"));
1047
1048 INP_INFO_WLOCK(&V_ripcbinfo);
1049 INP_WLOCK(inp);
1050 rip_delhash(inp);
1051 inp->inp_faddr = addr->sin_addr;
1052 rip_inshash(inp);
1053 soisconnected(so);
1054 INP_WUNLOCK(inp);
1055 INP_INFO_WUNLOCK(&V_ripcbinfo);
1056 return (0);
1057 }
1058
1059 static int
rip_shutdown(struct socket * so)1060 rip_shutdown(struct socket *so)
1061 {
1062 struct inpcb *inp;
1063
1064 inp = sotoinpcb(so);
1065 KASSERT(inp != NULL, ("rip_shutdown: inp == NULL"));
1066
1067 INP_WLOCK(inp);
1068 socantsendmore(so);
1069 INP_WUNLOCK(inp);
1070 return (0);
1071 }
1072
1073 static int
rip_send(struct socket * so,int flags,struct mbuf * m,struct sockaddr * nam,struct mbuf * control,struct thread * td)1074 rip_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
1075 struct mbuf *control, struct thread *td)
1076 {
1077 struct inpcb *inp;
1078 u_long dst;
1079 int error;
1080
1081 inp = sotoinpcb(so);
1082 KASSERT(inp != NULL, ("rip_send: inp == NULL"));
1083
1084 if (control != NULL) {
1085 m_freem(control);
1086 control = NULL;
1087 }
1088
1089 /*
1090 * Note: 'dst' reads below are unlocked.
1091 */
1092 if (so->so_state & SS_ISCONNECTED) {
1093 if (nam) {
1094 error = EISCONN;
1095 goto release;
1096 }
1097 dst = inp->inp_faddr.s_addr; /* Unlocked read. */
1098 } else {
1099 error = 0;
1100 if (nam == NULL)
1101 error = ENOTCONN;
1102 else if (nam->sa_family != AF_INET)
1103 error = EAFNOSUPPORT;
1104 else if (nam->sa_len != sizeof(struct sockaddr_in))
1105 error = EINVAL;
1106 if (error != 0)
1107 goto release;
1108 dst = ((struct sockaddr_in *)nam)->sin_addr.s_addr;
1109 }
1110 return (rip_output(m, so, dst));
1111
1112 release:
1113 m_freem(m);
1114 return (error);
1115 }
1116 #endif /* INET */
1117
1118 static int
rip_pcblist(SYSCTL_HANDLER_ARGS)1119 rip_pcblist(SYSCTL_HANDLER_ARGS)
1120 {
1121 struct xinpgen xig;
1122 struct epoch_tracker et;
1123 struct inpcb *inp;
1124 int error;
1125
1126 if (req->newptr != 0)
1127 return (EPERM);
1128
1129 if (req->oldptr == 0) {
1130 int n;
1131
1132 n = V_ripcbinfo.ipi_count;
1133 n += imax(n / 8, 10);
1134 req->oldidx = 2 * (sizeof xig) + n * sizeof(struct xinpcb);
1135 return (0);
1136 }
1137
1138 if ((error = sysctl_wire_old_buffer(req, 0)) != 0)
1139 return (error);
1140
1141 bzero(&xig, sizeof(xig));
1142 xig.xig_len = sizeof xig;
1143 xig.xig_count = V_ripcbinfo.ipi_count;
1144 xig.xig_gen = V_ripcbinfo.ipi_gencnt;
1145 xig.xig_sogen = so_gencnt;
1146 error = SYSCTL_OUT(req, &xig, sizeof xig);
1147 if (error)
1148 return (error);
1149
1150 NET_EPOCH_ENTER(et);
1151 for (inp = CK_LIST_FIRST(V_ripcbinfo.ipi_listhead);
1152 inp != NULL;
1153 inp = CK_LIST_NEXT(inp, inp_list)) {
1154 INP_RLOCK(inp);
1155 if (inp->inp_gencnt <= xig.xig_gen &&
1156 cr_canseeinpcb(req->td->td_ucred, inp) == 0) {
1157 struct xinpcb xi;
1158
1159 in_pcbtoxinpcb(inp, &xi);
1160 INP_RUNLOCK(inp);
1161 error = SYSCTL_OUT(req, &xi, sizeof xi);
1162 if (error)
1163 break;
1164 } else
1165 INP_RUNLOCK(inp);
1166 }
1167 NET_EPOCH_EXIT(et);
1168
1169 if (!error) {
1170 /*
1171 * Give the user an updated idea of our state. If the
1172 * generation differs from what we told her before, she knows
1173 * that something happened while we were processing this
1174 * request, and it might be necessary to retry.
1175 */
1176 xig.xig_gen = V_ripcbinfo.ipi_gencnt;
1177 xig.xig_sogen = so_gencnt;
1178 xig.xig_count = V_ripcbinfo.ipi_count;
1179 error = SYSCTL_OUT(req, &xig, sizeof xig);
1180 }
1181
1182 return (error);
1183 }
1184
1185 SYSCTL_PROC(_net_inet_raw, OID_AUTO/*XXX*/, pcblist,
1186 CTLTYPE_OPAQUE | CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, 0,
1187 rip_pcblist, "S,xinpcb",
1188 "List of active raw IP sockets");
1189
1190 #ifdef INET
1191 struct pr_usrreqs rip_usrreqs = {
1192 .pru_abort = rip_abort,
1193 .pru_attach = rip_attach,
1194 .pru_bind = rip_bind,
1195 .pru_connect = rip_connect,
1196 .pru_control = in_control,
1197 .pru_detach = rip_detach,
1198 .pru_disconnect = rip_disconnect,
1199 .pru_peeraddr = in_getpeeraddr,
1200 .pru_send = rip_send,
1201 .pru_shutdown = rip_shutdown,
1202 .pru_sockaddr = in_getsockaddr,
1203 .pru_sosetlabel = in_pcbsosetlabel,
1204 .pru_close = rip_close,
1205 };
1206 #endif /* INET */
1207