xref: /freebsd/sys/netinet/raw_ip.c (revision cacdd70cc751fb68dec4b86c5e5b8c969b6e26ef)
1 /*-
2  * Copyright (c) 1982, 1986, 1988, 1993
3  *	The Regents of the University of California.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 4. Neither the name of the University nor the names of its contributors
15  *    may be used to endorse or promote products derived from this software
16  *    without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  *
30  *	@(#)raw_ip.c	8.7 (Berkeley) 5/15/95
31  */
32 
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
35 
36 #include "opt_inet6.h"
37 #include "opt_ipsec.h"
38 #include "opt_mac.h"
39 
40 #include <sys/param.h>
41 #include <sys/jail.h>
42 #include <sys/kernel.h>
43 #include <sys/lock.h>
44 #include <sys/malloc.h>
45 #include <sys/mbuf.h>
46 #include <sys/priv.h>
47 #include <sys/proc.h>
48 #include <sys/protosw.h>
49 #include <sys/signalvar.h>
50 #include <sys/socket.h>
51 #include <sys/socketvar.h>
52 #include <sys/sx.h>
53 #include <sys/sysctl.h>
54 #include <sys/systm.h>
55 
56 #include <vm/uma.h>
57 
58 #include <net/if.h>
59 #include <net/route.h>
60 
61 #include <netinet/in.h>
62 #include <netinet/in_systm.h>
63 #include <netinet/in_pcb.h>
64 #include <netinet/in_var.h>
65 #include <netinet/ip.h>
66 #include <netinet/ip_var.h>
67 #include <netinet/ip_mroute.h>
68 
69 #include <netinet/ip_fw.h>
70 #include <netinet/ip_dummynet.h>
71 
72 #ifdef IPSEC
73 #include <netipsec/ipsec.h>
74 #endif /*IPSEC*/
75 
76 #include <security/mac/mac_framework.h>
77 
78 struct	inpcbhead ripcb;
79 struct	inpcbinfo ripcbinfo;
80 
81 /* control hooks for ipfw and dummynet */
82 ip_fw_ctl_t *ip_fw_ctl_ptr = NULL;
83 ip_dn_ctl_t *ip_dn_ctl_ptr = NULL;
84 
85 /*
86  * Hooks for multicast routing. They all default to NULL, so leave them not
87  * initialized and rely on BSS being set to 0.
88  */
89 
90 /*
91  * The socket used to communicate with the multicast routing daemon.
92  */
93 struct socket  *ip_mrouter;
94 
95 /*
96  * The various mrouter and rsvp functions.
97  */
98 int (*ip_mrouter_set)(struct socket *, struct sockopt *);
99 int (*ip_mrouter_get)(struct socket *, struct sockopt *);
100 int (*ip_mrouter_done)(void);
101 int (*ip_mforward)(struct ip *, struct ifnet *, struct mbuf *,
102 		   struct ip_moptions *);
103 int (*mrt_ioctl)(int, caddr_t, int);
104 int (*legal_vif_num)(int);
105 u_long (*ip_mcast_src)(int);
106 
107 void (*rsvp_input_p)(struct mbuf *m, int off);
108 int (*ip_rsvp_vif)(struct socket *, struct sockopt *);
109 void (*ip_rsvp_force_done)(struct socket *);
110 
111 /*
112  * Hash functions
113  */
114 
115 #define INP_PCBHASH_RAW_SIZE	256
116 #define INP_PCBHASH_RAW(proto, laddr, faddr, mask) \
117         (((proto) + (laddr) + (faddr)) % (mask) + 1)
118 
119 static void
120 rip_inshash(struct inpcb *inp)
121 {
122 	struct inpcbinfo *pcbinfo = inp->inp_pcbinfo;
123 	struct inpcbhead *pcbhash;
124 	int hash;
125 
126 	INP_INFO_WLOCK_ASSERT(pcbinfo);
127 	INP_WLOCK_ASSERT(inp);
128 
129 	if (inp->inp_ip_p != 0 &&
130 	    inp->inp_laddr.s_addr != INADDR_ANY &&
131 	    inp->inp_faddr.s_addr != INADDR_ANY) {
132 		hash = INP_PCBHASH_RAW(inp->inp_ip_p, inp->inp_laddr.s_addr,
133 		    inp->inp_faddr.s_addr, pcbinfo->ipi_hashmask);
134 	} else
135 		hash = 0;
136 	pcbhash = &pcbinfo->ipi_hashbase[hash];
137 	LIST_INSERT_HEAD(pcbhash, inp, inp_hash);
138 }
139 
140 static void
141 rip_delhash(struct inpcb *inp)
142 {
143 
144 	INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
145 	INP_WLOCK_ASSERT(inp);
146 
147 	LIST_REMOVE(inp, inp_hash);
148 }
149 
150 /*
151  * Raw interface to IP protocol.
152  */
153 
154 /*
155  * Initialize raw connection block q.
156  */
157 static void
158 rip_zone_change(void *tag)
159 {
160 
161 	uma_zone_set_max(ripcbinfo.ipi_zone, maxsockets);
162 }
163 
164 static int
165 rip_inpcb_init(void *mem, int size, int flags)
166 {
167 	struct inpcb *inp = mem;
168 
169 	INP_LOCK_INIT(inp, "inp", "rawinp");
170 	return (0);
171 }
172 
173 void
174 rip_init(void)
175 {
176 
177 	INP_INFO_LOCK_INIT(&ripcbinfo, "rip");
178 	LIST_INIT(&ripcb);
179 	ripcbinfo.ipi_listhead = &ripcb;
180 	ripcbinfo.ipi_hashbase = hashinit(INP_PCBHASH_RAW_SIZE, M_PCB,
181 	    &ripcbinfo.ipi_hashmask);
182 	ripcbinfo.ipi_porthashbase = hashinit(1, M_PCB,
183 	    &ripcbinfo.ipi_porthashmask);
184 	ripcbinfo.ipi_zone = uma_zcreate("ripcb", sizeof(struct inpcb),
185 	    NULL, NULL, rip_inpcb_init, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
186 	uma_zone_set_max(ripcbinfo.ipi_zone, maxsockets);
187 	EVENTHANDLER_REGISTER(maxsockets_change, rip_zone_change, NULL,
188 	    EVENTHANDLER_PRI_ANY);
189 }
190 
191 static int
192 rip_append(struct inpcb *last, struct ip *ip, struct mbuf *n,
193     struct sockaddr_in *ripsrc)
194 {
195 	int policyfail = 0;
196 
197 	INP_RLOCK_ASSERT(last);
198 
199 #ifdef IPSEC
200 	/* check AH/ESP integrity. */
201 	if (ipsec4_in_reject(n, last)) {
202 		policyfail = 1;
203 	}
204 #endif /* IPSEC */
205 #ifdef MAC
206 	if (!policyfail && mac_inpcb_check_deliver(last, n) != 0)
207 		policyfail = 1;
208 #endif
209 	/* Check the minimum TTL for socket. */
210 	if (last->inp_ip_minttl && last->inp_ip_minttl > ip->ip_ttl)
211 		policyfail = 1;
212 	if (!policyfail) {
213 		struct mbuf *opts = NULL;
214 		struct socket *so;
215 
216 		so = last->inp_socket;
217 		if ((last->inp_flags & INP_CONTROLOPTS) ||
218 		    (so->so_options & (SO_TIMESTAMP | SO_BINTIME)))
219 			ip_savecontrol(last, &opts, ip, n);
220 		SOCKBUF_LOCK(&so->so_rcv);
221 		if (sbappendaddr_locked(&so->so_rcv,
222 		    (struct sockaddr *)ripsrc, n, opts) == 0) {
223 			/* should notify about lost packet */
224 			m_freem(n);
225 			if (opts)
226 				m_freem(opts);
227 			SOCKBUF_UNLOCK(&so->so_rcv);
228 		} else
229 			sorwakeup_locked(so);
230 	} else
231 		m_freem(n);
232 	return (policyfail);
233 }
234 
235 /*
236  * Setup generic address and protocol structures for raw_input routine, then
237  * pass them along with mbuf chain.
238  */
239 void
240 rip_input(struct mbuf *m, int off)
241 {
242 	struct ip *ip = mtod(m, struct ip *);
243 	int proto = ip->ip_p;
244 	struct inpcb *inp, *last;
245 	struct sockaddr_in ripsrc;
246 	int hash;
247 
248 	bzero(&ripsrc, sizeof(ripsrc));
249 	ripsrc.sin_len = sizeof(ripsrc);
250 	ripsrc.sin_family = AF_INET;
251 	ripsrc.sin_addr = ip->ip_src;
252 	last = NULL;
253 	hash = INP_PCBHASH_RAW(proto, ip->ip_src.s_addr,
254 	    ip->ip_dst.s_addr, ripcbinfo.ipi_hashmask);
255 	INP_INFO_RLOCK(&ripcbinfo);
256 	LIST_FOREACH(inp, &ripcbinfo.ipi_hashbase[hash], inp_hash) {
257 		if (inp->inp_ip_p != proto)
258 			continue;
259 #ifdef INET6
260 		if ((inp->inp_vflag & INP_IPV4) == 0)
261 			continue;
262 #endif
263 		if (inp->inp_laddr.s_addr != ip->ip_dst.s_addr)
264 			continue;
265 		if (inp->inp_faddr.s_addr != ip->ip_src.s_addr)
266 			continue;
267 		INP_RLOCK(inp);
268 		if (jailed(inp->inp_socket->so_cred) &&
269 		    (htonl(prison_getip(inp->inp_socket->so_cred)) !=
270 		    ip->ip_dst.s_addr)) {
271 			INP_RUNLOCK(inp);
272 			continue;
273 		}
274 		if (last) {
275 			struct mbuf *n;
276 
277 			n = m_copy(m, 0, (int)M_COPYALL);
278 			if (n != NULL)
279 		    	    (void) rip_append(last, ip, n, &ripsrc);
280 			/* XXX count dropped packet */
281 			INP_RUNLOCK(last);
282 		}
283 		last = inp;
284 	}
285 	LIST_FOREACH(inp, &ripcbinfo.ipi_hashbase[0], inp_hash) {
286 		if (inp->inp_ip_p && inp->inp_ip_p != proto)
287 			continue;
288 #ifdef INET6
289 		if ((inp->inp_vflag & INP_IPV4) == 0)
290 			continue;
291 #endif
292 		if (inp->inp_laddr.s_addr &&
293 		    inp->inp_laddr.s_addr != ip->ip_dst.s_addr)
294 			continue;
295 		if (inp->inp_faddr.s_addr &&
296 		    inp->inp_faddr.s_addr != ip->ip_src.s_addr)
297 			continue;
298 		INP_RLOCK(inp);
299 		if (jailed(inp->inp_socket->so_cred) &&
300 		    (htonl(prison_getip(inp->inp_socket->so_cred)) !=
301 		    ip->ip_dst.s_addr)) {
302 			INP_RUNLOCK(inp);
303 			continue;
304 		}
305 		if (last) {
306 			struct mbuf *n;
307 
308 			n = m_copy(m, 0, (int)M_COPYALL);
309 			if (n != NULL)
310 				(void) rip_append(last, ip, n, &ripsrc);
311 			/* XXX count dropped packet */
312 			INP_RUNLOCK(last);
313 		}
314 		last = inp;
315 	}
316 	INP_INFO_RUNLOCK(&ripcbinfo);
317 	if (last != NULL) {
318 		if (rip_append(last, ip, m, &ripsrc) != 0)
319 			ipstat.ips_delivered--;
320 		INP_RUNLOCK(last);
321 	} else {
322 		m_freem(m);
323 		ipstat.ips_noproto++;
324 		ipstat.ips_delivered--;
325 	}
326 }
327 
328 /*
329  * Generate IP header and pass packet to ip_output.  Tack on options user may
330  * have setup with control call.
331  */
332 int
333 rip_output(struct mbuf *m, struct socket *so, u_long dst)
334 {
335 	struct ip *ip;
336 	int error;
337 	struct inpcb *inp = sotoinpcb(so);
338 	int flags = ((so->so_options & SO_DONTROUTE) ? IP_ROUTETOIF : 0) |
339 	    IP_ALLOWBROADCAST;
340 
341 	/*
342 	 * If the user handed us a complete IP packet, use it.  Otherwise,
343 	 * allocate an mbuf for a header and fill it in.
344 	 */
345 	if ((inp->inp_flags & INP_HDRINCL) == 0) {
346 		if (m->m_pkthdr.len + sizeof(struct ip) > IP_MAXPACKET) {
347 			m_freem(m);
348 			return(EMSGSIZE);
349 		}
350 		M_PREPEND(m, sizeof(struct ip), M_DONTWAIT);
351 		if (m == NULL)
352 			return(ENOBUFS);
353 
354 		INP_RLOCK(inp);
355 		ip = mtod(m, struct ip *);
356 		ip->ip_tos = inp->inp_ip_tos;
357 		if (inp->inp_flags & INP_DONTFRAG)
358 			ip->ip_off = IP_DF;
359 		else
360 			ip->ip_off = 0;
361 		ip->ip_p = inp->inp_ip_p;
362 		ip->ip_len = m->m_pkthdr.len;
363 		if (jailed(inp->inp_socket->so_cred))
364 			ip->ip_src.s_addr =
365 			    htonl(prison_getip(inp->inp_socket->so_cred));
366 		else
367 			ip->ip_src = inp->inp_laddr;
368 		ip->ip_dst.s_addr = dst;
369 		ip->ip_ttl = inp->inp_ip_ttl;
370 	} else {
371 		if (m->m_pkthdr.len > IP_MAXPACKET) {
372 			m_freem(m);
373 			return(EMSGSIZE);
374 		}
375 		INP_RLOCK(inp);
376 		ip = mtod(m, struct ip *);
377 		if (jailed(inp->inp_socket->so_cred)) {
378 			if (ip->ip_src.s_addr !=
379 			    htonl(prison_getip(inp->inp_socket->so_cred))) {
380 				INP_RUNLOCK(inp);
381 				m_freem(m);
382 				return (EPERM);
383 			}
384 		}
385 
386 		/*
387 		 * Don't allow both user specified and setsockopt options,
388 		 * and don't allow packet length sizes that will crash.
389 		 */
390 		if (((ip->ip_hl != (sizeof (*ip) >> 2)) && inp->inp_options)
391 		    || (ip->ip_len > m->m_pkthdr.len)
392 		    || (ip->ip_len < (ip->ip_hl << 2))) {
393 			INP_RUNLOCK(inp);
394 			m_freem(m);
395 			return (EINVAL);
396 		}
397 		if (ip->ip_id == 0)
398 			ip->ip_id = ip_newid();
399 
400 		/*
401 		 * XXX prevent ip_output from overwriting header fields.
402 		 */
403 		flags |= IP_RAWOUTPUT;
404 		ipstat.ips_rawout++;
405 	}
406 
407 	if (inp->inp_flags & INP_ONESBCAST)
408 		flags |= IP_SENDONES;
409 
410 #ifdef MAC
411 	mac_inpcb_create_mbuf(inp, m);
412 #endif
413 
414 	error = ip_output(m, inp->inp_options, NULL, flags,
415 	    inp->inp_moptions, inp);
416 	INP_RUNLOCK(inp);
417 	return (error);
418 }
419 
420 /*
421  * Raw IP socket option processing.
422  *
423  * IMPORTANT NOTE regarding access control: Traditionally, raw sockets could
424  * only be created by a privileged process, and as such, socket option
425  * operations to manage system properties on any raw socket were allowed to
426  * take place without explicit additional access control checks.  However,
427  * raw sockets can now also be created in jail(), and therefore explicit
428  * checks are now required.  Likewise, raw sockets can be used by a process
429  * after it gives up privilege, so some caution is required.  For options
430  * passed down to the IP layer via ip_ctloutput(), checks are assumed to be
431  * performed in ip_ctloutput() and therefore no check occurs here.
432  * Unilaterally checking priv_check() here breaks normal IP socket option
433  * operations on raw sockets.
434  *
435  * When adding new socket options here, make sure to add access control
436  * checks here as necessary.
437  */
438 int
439 rip_ctloutput(struct socket *so, struct sockopt *sopt)
440 {
441 	struct	inpcb *inp = sotoinpcb(so);
442 	int	error, optval;
443 
444 	if (sopt->sopt_level != IPPROTO_IP)
445 		return (EINVAL);
446 
447 	error = 0;
448 	switch (sopt->sopt_dir) {
449 	case SOPT_GET:
450 		switch (sopt->sopt_name) {
451 		case IP_HDRINCL:
452 			optval = inp->inp_flags & INP_HDRINCL;
453 			error = sooptcopyout(sopt, &optval, sizeof optval);
454 			break;
455 
456 		case IP_FW_ADD:	/* ADD actually returns the body... */
457 		case IP_FW_GET:
458 		case IP_FW_TABLE_GETSIZE:
459 		case IP_FW_TABLE_LIST:
460 		case IP_FW_NAT_GET_CONFIG:
461 		case IP_FW_NAT_GET_LOG:
462 			if (ip_fw_ctl_ptr != NULL)
463 				error = ip_fw_ctl_ptr(sopt);
464 			else
465 				error = ENOPROTOOPT;
466 			break;
467 
468 		case IP_DUMMYNET_GET:
469 			if (ip_dn_ctl_ptr != NULL)
470 				error = ip_dn_ctl_ptr(sopt);
471 			else
472 				error = ENOPROTOOPT;
473 			break ;
474 
475 		case MRT_INIT:
476 		case MRT_DONE:
477 		case MRT_ADD_VIF:
478 		case MRT_DEL_VIF:
479 		case MRT_ADD_MFC:
480 		case MRT_DEL_MFC:
481 		case MRT_VERSION:
482 		case MRT_ASSERT:
483 		case MRT_API_SUPPORT:
484 		case MRT_API_CONFIG:
485 		case MRT_ADD_BW_UPCALL:
486 		case MRT_DEL_BW_UPCALL:
487 			error = priv_check(curthread, PRIV_NETINET_MROUTE);
488 			if (error != 0)
489 				return (error);
490 			error = ip_mrouter_get ? ip_mrouter_get(so, sopt) :
491 				EOPNOTSUPP;
492 			break;
493 
494 		default:
495 			error = ip_ctloutput(so, sopt);
496 			break;
497 		}
498 		break;
499 
500 	case SOPT_SET:
501 		switch (sopt->sopt_name) {
502 		case IP_HDRINCL:
503 			error = sooptcopyin(sopt, &optval, sizeof optval,
504 					    sizeof optval);
505 			if (error)
506 				break;
507 			if (optval)
508 				inp->inp_flags |= INP_HDRINCL;
509 			else
510 				inp->inp_flags &= ~INP_HDRINCL;
511 			break;
512 
513 		case IP_FW_ADD:
514 		case IP_FW_DEL:
515 		case IP_FW_FLUSH:
516 		case IP_FW_ZERO:
517 		case IP_FW_RESETLOG:
518 		case IP_FW_TABLE_ADD:
519 		case IP_FW_TABLE_DEL:
520 		case IP_FW_TABLE_FLUSH:
521 		case IP_FW_NAT_CFG:
522 		case IP_FW_NAT_DEL:
523 			if (ip_fw_ctl_ptr != NULL)
524 				error = ip_fw_ctl_ptr(sopt);
525 			else
526 				error = ENOPROTOOPT;
527 			break;
528 
529 		case IP_DUMMYNET_CONFIGURE:
530 		case IP_DUMMYNET_DEL:
531 		case IP_DUMMYNET_FLUSH:
532 			if (ip_dn_ctl_ptr != NULL)
533 				error = ip_dn_ctl_ptr(sopt);
534 			else
535 				error = ENOPROTOOPT ;
536 			break ;
537 
538 		case IP_RSVP_ON:
539 			error = priv_check(curthread, PRIV_NETINET_MROUTE);
540 			if (error != 0)
541 				return (error);
542 			error = ip_rsvp_init(so);
543 			break;
544 
545 		case IP_RSVP_OFF:
546 			error = priv_check(curthread, PRIV_NETINET_MROUTE);
547 			if (error != 0)
548 				return (error);
549 			error = ip_rsvp_done();
550 			break;
551 
552 		case IP_RSVP_VIF_ON:
553 		case IP_RSVP_VIF_OFF:
554 			error = priv_check(curthread, PRIV_NETINET_MROUTE);
555 			if (error != 0)
556 				return (error);
557 			error = ip_rsvp_vif ?
558 				ip_rsvp_vif(so, sopt) : EINVAL;
559 			break;
560 
561 		case MRT_INIT:
562 		case MRT_DONE:
563 		case MRT_ADD_VIF:
564 		case MRT_DEL_VIF:
565 		case MRT_ADD_MFC:
566 		case MRT_DEL_MFC:
567 		case MRT_VERSION:
568 		case MRT_ASSERT:
569 		case MRT_API_SUPPORT:
570 		case MRT_API_CONFIG:
571 		case MRT_ADD_BW_UPCALL:
572 		case MRT_DEL_BW_UPCALL:
573 			error = priv_check(curthread, PRIV_NETINET_MROUTE);
574 			if (error != 0)
575 				return (error);
576 			error = ip_mrouter_set ? ip_mrouter_set(so, sopt) :
577 					EOPNOTSUPP;
578 			break;
579 
580 		default:
581 			error = ip_ctloutput(so, sopt);
582 			break;
583 		}
584 		break;
585 	}
586 
587 	return (error);
588 }
589 
590 /*
591  * This function exists solely to receive the PRC_IFDOWN messages which are
592  * sent by if_down().  It looks for an ifaddr whose ifa_addr is sa, and calls
593  * in_ifadown() to remove all routes corresponding to that address.  It also
594  * receives the PRC_IFUP messages from if_up() and reinstalls the interface
595  * routes.
596  */
597 void
598 rip_ctlinput(int cmd, struct sockaddr *sa, void *vip)
599 {
600 	struct in_ifaddr *ia;
601 	struct ifnet *ifp;
602 	int err;
603 	int flags;
604 
605 	switch (cmd) {
606 	case PRC_IFDOWN:
607 		TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link) {
608 			if (ia->ia_ifa.ifa_addr == sa
609 			    && (ia->ia_flags & IFA_ROUTE)) {
610 				/*
611 				 * in_ifscrub kills the interface route.
612 				 */
613 				in_ifscrub(ia->ia_ifp, ia);
614 				/*
615 				 * in_ifadown gets rid of all the rest of the
616 				 * routes.  This is not quite the right thing
617 				 * to do, but at least if we are running a
618 				 * routing process they will come back.
619 				 */
620 				in_ifadown(&ia->ia_ifa, 0);
621 				break;
622 			}
623 		}
624 		break;
625 
626 	case PRC_IFUP:
627 		TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link) {
628 			if (ia->ia_ifa.ifa_addr == sa)
629 				break;
630 		}
631 		if (ia == 0 || (ia->ia_flags & IFA_ROUTE))
632 			return;
633 		flags = RTF_UP;
634 		ifp = ia->ia_ifa.ifa_ifp;
635 
636 		if ((ifp->if_flags & IFF_LOOPBACK)
637 		    || (ifp->if_flags & IFF_POINTOPOINT))
638 			flags |= RTF_HOST;
639 
640 		err = rtinit(&ia->ia_ifa, RTM_ADD, flags);
641 		if (err == 0)
642 			ia->ia_flags |= IFA_ROUTE;
643 		break;
644 	}
645 }
646 
647 u_long	rip_sendspace = 9216;
648 u_long	rip_recvspace = 9216;
649 
650 SYSCTL_ULONG(_net_inet_raw, OID_AUTO, maxdgram, CTLFLAG_RW,
651     &rip_sendspace, 0, "Maximum outgoing raw IP datagram size");
652 SYSCTL_ULONG(_net_inet_raw, OID_AUTO, recvspace, CTLFLAG_RW,
653     &rip_recvspace, 0, "Maximum space for incoming raw IP datagrams");
654 
655 static int
656 rip_attach(struct socket *so, int proto, struct thread *td)
657 {
658 	struct inpcb *inp;
659 	int error;
660 
661 	inp = sotoinpcb(so);
662 	KASSERT(inp == NULL, ("rip_attach: inp != NULL"));
663 
664 	error = priv_check(td, PRIV_NETINET_RAW);
665 	if (error)
666 		return (error);
667 	if (proto >= IPPROTO_MAX || proto < 0)
668 		return EPROTONOSUPPORT;
669 	error = soreserve(so, rip_sendspace, rip_recvspace);
670 	if (error)
671 		return (error);
672 	INP_INFO_WLOCK(&ripcbinfo);
673 	error = in_pcballoc(so, &ripcbinfo);
674 	if (error) {
675 		INP_INFO_WUNLOCK(&ripcbinfo);
676 		return (error);
677 	}
678 	inp = (struct inpcb *)so->so_pcb;
679 	inp->inp_vflag |= INP_IPV4;
680 	inp->inp_ip_p = proto;
681 	inp->inp_ip_ttl = ip_defttl;
682 	rip_inshash(inp);
683 	INP_INFO_WUNLOCK(&ripcbinfo);
684 	INP_WUNLOCK(inp);
685 	return (0);
686 }
687 
688 static void
689 rip_detach(struct socket *so)
690 {
691 	struct inpcb *inp;
692 
693 	inp = sotoinpcb(so);
694 	KASSERT(inp != NULL, ("rip_detach: inp == NULL"));
695 	KASSERT(inp->inp_faddr.s_addr == INADDR_ANY,
696 	    ("rip_detach: not closed"));
697 
698 	INP_INFO_WLOCK(&ripcbinfo);
699 	INP_WLOCK(inp);
700 	rip_delhash(inp);
701 	if (so == ip_mrouter && ip_mrouter_done)
702 		ip_mrouter_done();
703 	if (ip_rsvp_force_done)
704 		ip_rsvp_force_done(so);
705 	if (so == ip_rsvpd)
706 		ip_rsvp_done();
707 	in_pcbdetach(inp);
708 	in_pcbfree(inp);
709 	INP_INFO_WUNLOCK(&ripcbinfo);
710 }
711 
712 static void
713 rip_dodisconnect(struct socket *so, struct inpcb *inp)
714 {
715 
716 	INP_INFO_WLOCK_ASSERT(inp->inp_pcbinfo);
717 	INP_WLOCK_ASSERT(inp);
718 
719 	rip_delhash(inp);
720 	inp->inp_faddr.s_addr = INADDR_ANY;
721 	rip_inshash(inp);
722 	SOCK_LOCK(so);
723 	so->so_state &= ~SS_ISCONNECTED;
724 	SOCK_UNLOCK(so);
725 }
726 
727 static void
728 rip_abort(struct socket *so)
729 {
730 	struct inpcb *inp;
731 
732 	inp = sotoinpcb(so);
733 	KASSERT(inp != NULL, ("rip_abort: inp == NULL"));
734 
735 	INP_INFO_WLOCK(&ripcbinfo);
736 	INP_WLOCK(inp);
737 	rip_dodisconnect(so, inp);
738 	INP_WUNLOCK(inp);
739 	INP_INFO_WUNLOCK(&ripcbinfo);
740 }
741 
742 static void
743 rip_close(struct socket *so)
744 {
745 	struct inpcb *inp;
746 
747 	inp = sotoinpcb(so);
748 	KASSERT(inp != NULL, ("rip_close: inp == NULL"));
749 
750 	INP_INFO_WLOCK(&ripcbinfo);
751 	INP_WLOCK(inp);
752 	rip_dodisconnect(so, inp);
753 	INP_WUNLOCK(inp);
754 	INP_INFO_WUNLOCK(&ripcbinfo);
755 }
756 
757 static int
758 rip_disconnect(struct socket *so)
759 {
760 	struct inpcb *inp;
761 
762 	if ((so->so_state & SS_ISCONNECTED) == 0)
763 		return (ENOTCONN);
764 
765 	inp = sotoinpcb(so);
766 	KASSERT(inp != NULL, ("rip_disconnect: inp == NULL"));
767 
768 	INP_INFO_WLOCK(&ripcbinfo);
769 	INP_WLOCK(inp);
770 	rip_dodisconnect(so, inp);
771 	INP_WUNLOCK(inp);
772 	INP_INFO_WUNLOCK(&ripcbinfo);
773 	return (0);
774 }
775 
776 static int
777 rip_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
778 {
779 	struct sockaddr_in *addr = (struct sockaddr_in *)nam;
780 	struct inpcb *inp;
781 
782 	if (nam->sa_len != sizeof(*addr))
783 		return (EINVAL);
784 
785 	if (jailed(td->td_ucred)) {
786 		if (addr->sin_addr.s_addr == INADDR_ANY)
787 			addr->sin_addr.s_addr =
788 			    htonl(prison_getip(td->td_ucred));
789 		if (htonl(prison_getip(td->td_ucred)) != addr->sin_addr.s_addr)
790 			return (EADDRNOTAVAIL);
791 	}
792 
793 	if (TAILQ_EMPTY(&ifnet) ||
794 	    (addr->sin_family != AF_INET && addr->sin_family != AF_IMPLINK) ||
795 	    (addr->sin_addr.s_addr &&
796 	     ifa_ifwithaddr((struct sockaddr *)addr) == 0))
797 		return (EADDRNOTAVAIL);
798 
799 	inp = sotoinpcb(so);
800 	KASSERT(inp != NULL, ("rip_bind: inp == NULL"));
801 
802 	INP_INFO_WLOCK(&ripcbinfo);
803 	INP_WLOCK(inp);
804 	rip_delhash(inp);
805 	inp->inp_laddr = addr->sin_addr;
806 	rip_inshash(inp);
807 	INP_WUNLOCK(inp);
808 	INP_INFO_WUNLOCK(&ripcbinfo);
809 	return (0);
810 }
811 
812 static int
813 rip_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
814 {
815 	struct sockaddr_in *addr = (struct sockaddr_in *)nam;
816 	struct inpcb *inp;
817 
818 	if (nam->sa_len != sizeof(*addr))
819 		return (EINVAL);
820 	if (TAILQ_EMPTY(&ifnet))
821 		return (EADDRNOTAVAIL);
822 	if (addr->sin_family != AF_INET && addr->sin_family != AF_IMPLINK)
823 		return (EAFNOSUPPORT);
824 
825 	inp = sotoinpcb(so);
826 	KASSERT(inp != NULL, ("rip_connect: inp == NULL"));
827 
828 	INP_INFO_WLOCK(&ripcbinfo);
829 	INP_WLOCK(inp);
830 	rip_delhash(inp);
831 	inp->inp_faddr = addr->sin_addr;
832 	rip_inshash(inp);
833 	soisconnected(so);
834 	INP_WUNLOCK(inp);
835 	INP_INFO_WUNLOCK(&ripcbinfo);
836 	return (0);
837 }
838 
839 static int
840 rip_shutdown(struct socket *so)
841 {
842 	struct inpcb *inp;
843 
844 	inp = sotoinpcb(so);
845 	KASSERT(inp != NULL, ("rip_shutdown: inp == NULL"));
846 
847 	INP_WLOCK(inp);
848 	socantsendmore(so);
849 	INP_WUNLOCK(inp);
850 	return (0);
851 }
852 
853 static int
854 rip_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
855     struct mbuf *control, struct thread *td)
856 {
857 	struct inpcb *inp;
858 	u_long dst;
859 
860 	inp = sotoinpcb(so);
861 	KASSERT(inp != NULL, ("rip_send: inp == NULL"));
862 
863 	/*
864 	 * Note: 'dst' reads below are unlocked.
865 	 */
866 	if (so->so_state & SS_ISCONNECTED) {
867 		if (nam) {
868 			m_freem(m);
869 			return (EISCONN);
870 		}
871 		dst = inp->inp_faddr.s_addr;	/* Unlocked read. */
872 	} else {
873 		if (nam == NULL) {
874 			m_freem(m);
875 			return (ENOTCONN);
876 		}
877 		dst = ((struct sockaddr_in *)nam)->sin_addr.s_addr;
878 	}
879 	return (rip_output(m, so, dst));
880 }
881 
882 static int
883 rip_pcblist(SYSCTL_HANDLER_ARGS)
884 {
885 	int error, i, n;
886 	struct inpcb *inp, **inp_list;
887 	inp_gen_t gencnt;
888 	struct xinpgen xig;
889 
890 	/*
891 	 * The process of preparing the TCB list is too time-consuming and
892 	 * resource-intensive to repeat twice on every request.
893 	 */
894 	if (req->oldptr == 0) {
895 		n = ripcbinfo.ipi_count;
896 		req->oldidx = 2 * (sizeof xig)
897 		    + (n + n/8) * sizeof(struct xinpcb);
898 		return (0);
899 	}
900 
901 	if (req->newptr != 0)
902 		return (EPERM);
903 
904 	/*
905 	 * OK, now we're committed to doing something.
906 	 */
907 	INP_INFO_RLOCK(&ripcbinfo);
908 	gencnt = ripcbinfo.ipi_gencnt;
909 	n = ripcbinfo.ipi_count;
910 	INP_INFO_RUNLOCK(&ripcbinfo);
911 
912 	xig.xig_len = sizeof xig;
913 	xig.xig_count = n;
914 	xig.xig_gen = gencnt;
915 	xig.xig_sogen = so_gencnt;
916 	error = SYSCTL_OUT(req, &xig, sizeof xig);
917 	if (error)
918 		return (error);
919 
920 	inp_list = malloc(n * sizeof *inp_list, M_TEMP, M_WAITOK);
921 	if (inp_list == 0)
922 		return (ENOMEM);
923 
924 	INP_INFO_RLOCK(&ripcbinfo);
925 	for (inp = LIST_FIRST(ripcbinfo.ipi_listhead), i = 0; inp && i < n;
926 	     inp = LIST_NEXT(inp, inp_list)) {
927 		INP_RLOCK(inp);
928 		if (inp->inp_gencnt <= gencnt &&
929 		    cr_canseesocket(req->td->td_ucred, inp->inp_socket) == 0) {
930 			/* XXX held references? */
931 			inp_list[i++] = inp;
932 		}
933 		INP_RUNLOCK(inp);
934 	}
935 	INP_INFO_RUNLOCK(&ripcbinfo);
936 	n = i;
937 
938 	error = 0;
939 	for (i = 0; i < n; i++) {
940 		inp = inp_list[i];
941 		INP_RLOCK(inp);
942 		if (inp->inp_gencnt <= gencnt) {
943 			struct xinpcb xi;
944 			bzero(&xi, sizeof(xi));
945 			xi.xi_len = sizeof xi;
946 			/* XXX should avoid extra copy */
947 			bcopy(inp, &xi.xi_inp, sizeof *inp);
948 			if (inp->inp_socket)
949 				sotoxsocket(inp->inp_socket, &xi.xi_socket);
950 			INP_RUNLOCK(inp);
951 			error = SYSCTL_OUT(req, &xi, sizeof xi);
952 		} else
953 			INP_RUNLOCK(inp);
954 	}
955 	if (!error) {
956 		/*
957 		 * Give the user an updated idea of our state.  If the
958 		 * generation differs from what we told her before, she knows
959 		 * that something happened while we were processing this
960 		 * request, and it might be necessary to retry.
961 		 */
962 		INP_INFO_RLOCK(&ripcbinfo);
963 		xig.xig_gen = ripcbinfo.ipi_gencnt;
964 		xig.xig_sogen = so_gencnt;
965 		xig.xig_count = ripcbinfo.ipi_count;
966 		INP_INFO_RUNLOCK(&ripcbinfo);
967 		error = SYSCTL_OUT(req, &xig, sizeof xig);
968 	}
969 	free(inp_list, M_TEMP);
970 	return (error);
971 }
972 
973 SYSCTL_PROC(_net_inet_raw, OID_AUTO/*XXX*/, pcblist, CTLFLAG_RD, 0, 0,
974     rip_pcblist, "S,xinpcb", "List of active raw IP sockets");
975 
976 struct pr_usrreqs rip_usrreqs = {
977 	.pru_abort =		rip_abort,
978 	.pru_attach =		rip_attach,
979 	.pru_bind =		rip_bind,
980 	.pru_connect =		rip_connect,
981 	.pru_control =		in_control,
982 	.pru_detach =		rip_detach,
983 	.pru_disconnect =	rip_disconnect,
984 	.pru_peeraddr =		in_getpeeraddr,
985 	.pru_send =		rip_send,
986 	.pru_shutdown =		rip_shutdown,
987 	.pru_sockaddr =		in_getsockaddr,
988 	.pru_sosetlabel =	in_pcbsosetlabel,
989 	.pru_close =		rip_close,
990 };
991