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