xref: /freebsd/sys/netinet/raw_ip.c (revision f711d546d27edc6115841ef780df0252d44a996d)
1df8bae1dSRodney W. Grimes /*
2df8bae1dSRodney W. Grimes  * Copyright (c) 1982, 1986, 1988, 1993
3df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
4df8bae1dSRodney W. Grimes  *
5df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
6df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
7df8bae1dSRodney W. Grimes  * are met:
8df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
9df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
10df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
11df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
12df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
13df8bae1dSRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
14df8bae1dSRodney W. Grimes  *    must display the following acknowledgement:
15df8bae1dSRodney W. Grimes  *	This product includes software developed by the University of
16df8bae1dSRodney W. Grimes  *	California, Berkeley and its contributors.
17df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
18df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
19df8bae1dSRodney W. Grimes  *    without specific prior written permission.
20df8bae1dSRodney W. Grimes  *
21df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
32df8bae1dSRodney W. Grimes  *
3325f26ad8SGarrett Wollman  *	@(#)raw_ip.c	8.7 (Berkeley) 5/15/95
34f711d546SPoul-Henning Kamp  *	$Id: raw_ip.c,v 1.57 1999/04/20 13:32:06 peter Exp $
35df8bae1dSRodney W. Grimes  */
36df8bae1dSRodney W. Grimes 
37df8bae1dSRodney W. Grimes #include <sys/param.h>
38117bcae7SGarrett Wollman #include <sys/systm.h>
39117bcae7SGarrett Wollman #include <sys/kernel.h>
40df8bae1dSRodney W. Grimes #include <sys/malloc.h>
41df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
42a29f300eSGarrett Wollman #include <sys/proc.h>
43df8bae1dSRodney W. Grimes #include <sys/protosw.h>
44117bcae7SGarrett Wollman #include <sys/socket.h>
45df8bae1dSRodney W. Grimes #include <sys/socketvar.h>
46117bcae7SGarrett Wollman #include <sys/sysctl.h>
478781d8e9SBruce Evans 
483d4d47f3SGarrett Wollman #include <vm/vm_zone.h>
49df8bae1dSRodney W. Grimes 
50df8bae1dSRodney W. Grimes #include <net/if.h>
51df8bae1dSRodney W. Grimes #include <net/route.h>
52df8bae1dSRodney W. Grimes 
535e2d0696SGarrett Wollman #define _IP_VHL
54df8bae1dSRodney W. Grimes #include <netinet/in.h>
55df8bae1dSRodney W. Grimes #include <netinet/in_systm.h>
56df8bae1dSRodney W. Grimes #include <netinet/ip.h>
57c1f8a6ceSDavid Greenman #include <netinet/in_pcb.h>
58c1f8a6ceSDavid Greenman #include <netinet/in_var.h>
59df8bae1dSRodney W. Grimes #include <netinet/ip_var.h>
60df8bae1dSRodney W. Grimes #include <netinet/ip_mroute.h>
61df8bae1dSRodney W. Grimes 
62100ba1a6SJordan K. Hubbard #include <netinet/ip_fw.h>
63100ba1a6SJordan K. Hubbard 
64b715f178SLuigi Rizzo #include "opt_ipdn.h"
65b715f178SLuigi Rizzo #ifdef DUMMYNET
66b715f178SLuigi Rizzo #include <netinet/ip_dummynet.h>
67b715f178SLuigi Rizzo #endif
68f9493383SGarrett Wollman 
69f6d24a78SPoul-Henning Kamp static struct inpcbhead ripcb;
70f6d24a78SPoul-Henning Kamp static struct inpcbinfo ripcbinfo;
71df8bae1dSRodney W. Grimes 
72df8bae1dSRodney W. Grimes /*
73df8bae1dSRodney W. Grimes  * Nominal space allocated to a raw ip socket.
74df8bae1dSRodney W. Grimes  */
75df8bae1dSRodney W. Grimes #define	RIPSNDQ		8192
76df8bae1dSRodney W. Grimes #define	RIPRCVQ		8192
77df8bae1dSRodney W. Grimes 
78df8bae1dSRodney W. Grimes /*
79df8bae1dSRodney W. Grimes  * Raw interface to IP protocol.
80df8bae1dSRodney W. Grimes  */
81df8bae1dSRodney W. Grimes 
82df8bae1dSRodney W. Grimes /*
83df8bae1dSRodney W. Grimes  * Initialize raw connection block q.
84df8bae1dSRodney W. Grimes  */
85df8bae1dSRodney W. Grimes void
86df8bae1dSRodney W. Grimes rip_init()
87df8bae1dSRodney W. Grimes {
8815bd2b43SDavid Greenman 	LIST_INIT(&ripcb);
8915bd2b43SDavid Greenman 	ripcbinfo.listhead = &ripcb;
9015bd2b43SDavid Greenman 	/*
9115bd2b43SDavid Greenman 	 * XXX We don't use the hash list for raw IP, but it's easier
9215bd2b43SDavid Greenman 	 * to allocate a one entry hash list than it is to check all
9315bd2b43SDavid Greenman 	 * over the place for hashbase == NULL.
9415bd2b43SDavid Greenman 	 */
95ddd79a97SDavid Greenman 	ripcbinfo.hashbase = hashinit(1, M_PCB, &ripcbinfo.hashmask);
96c3229e05SDavid Greenman 	ripcbinfo.porthashbase = hashinit(1, M_PCB, &ripcbinfo.porthashmask);
973d4d47f3SGarrett Wollman 	ripcbinfo.ipi_zone = zinit("ripcb", sizeof(struct inpcb),
9898271db4SGarrett Wollman 				   maxsockets, ZONE_INTERRUPT, 0);
99df8bae1dSRodney W. Grimes }
100df8bae1dSRodney W. Grimes 
101f6d24a78SPoul-Henning Kamp static struct	sockaddr_in ripsrc = { sizeof(ripsrc), AF_INET };
102df8bae1dSRodney W. Grimes /*
103df8bae1dSRodney W. Grimes  * Setup generic address and protocol structures
104df8bae1dSRodney W. Grimes  * for raw_input routine, then pass them along with
105df8bae1dSRodney W. Grimes  * mbuf chain.
106df8bae1dSRodney W. Grimes  */
107df8bae1dSRodney W. Grimes void
108e62b8c49SBill Fenner rip_input(m, iphlen)
109df8bae1dSRodney W. Grimes 	struct mbuf *m;
110e62b8c49SBill Fenner 	int iphlen;
111df8bae1dSRodney W. Grimes {
112df8bae1dSRodney W. Grimes 	register struct ip *ip = mtod(m, struct ip *);
113df8bae1dSRodney W. Grimes 	register struct inpcb *inp;
11482c23ebaSBill Fenner 	struct inpcb *last = 0;
11582c23ebaSBill Fenner 	struct mbuf *opts = 0;
116df8bae1dSRodney W. Grimes 
117df8bae1dSRodney W. Grimes 	ripsrc.sin_addr = ip->ip_src;
11815bd2b43SDavid Greenman 	for (inp = ripcb.lh_first; inp != NULL; inp = inp->inp_list.le_next) {
119ca98b82cSDavid Greenman 		if (inp->inp_ip_p && inp->inp_ip_p != ip->ip_p)
120df8bae1dSRodney W. Grimes 			continue;
121df8bae1dSRodney W. Grimes 		if (inp->inp_laddr.s_addr &&
122d99c7a23SGarrett Wollman                   inp->inp_laddr.s_addr != ip->ip_dst.s_addr)
123df8bae1dSRodney W. Grimes 			continue;
124df8bae1dSRodney W. Grimes 		if (inp->inp_faddr.s_addr &&
125d99c7a23SGarrett Wollman                   inp->inp_faddr.s_addr != ip->ip_src.s_addr)
126df8bae1dSRodney W. Grimes 			continue;
127df8bae1dSRodney W. Grimes 		if (last) {
128623ae52eSPoul-Henning Kamp 			struct mbuf *n = m_copy(m, 0, (int)M_COPYALL);
129623ae52eSPoul-Henning Kamp 			if (n) {
13082c23ebaSBill Fenner 				if (last->inp_flags & INP_CONTROLOPTS ||
13182c23ebaSBill Fenner 				    last->inp_socket->so_options & SO_TIMESTAMP)
13282c23ebaSBill Fenner 				    ip_savecontrol(last, &opts, ip, n);
13382c23ebaSBill Fenner 				if (sbappendaddr(&last->inp_socket->so_rcv,
134623ae52eSPoul-Henning Kamp 				    (struct sockaddr *)&ripsrc, n,
13582c23ebaSBill Fenner 				    opts) == 0) {
136df8bae1dSRodney W. Grimes 					/* should notify about lost packet */
137df8bae1dSRodney W. Grimes 					m_freem(n);
13882c23ebaSBill Fenner 					if (opts)
13982c23ebaSBill Fenner 					    m_freem(opts);
14082c23ebaSBill Fenner 				} else
14182c23ebaSBill Fenner 					sorwakeup(last->inp_socket);
14282c23ebaSBill Fenner 				opts = 0;
143df8bae1dSRodney W. Grimes 			}
144df8bae1dSRodney W. Grimes 		}
14582c23ebaSBill Fenner 		last = inp;
146df8bae1dSRodney W. Grimes 	}
147df8bae1dSRodney W. Grimes 	if (last) {
14882c23ebaSBill Fenner 		if (last->inp_flags & INP_CONTROLOPTS ||
14982c23ebaSBill Fenner 		    last->inp_socket->so_options & SO_TIMESTAMP)
15082c23ebaSBill Fenner 			ip_savecontrol(last, &opts, ip, m);
15182c23ebaSBill Fenner 		if (sbappendaddr(&last->inp_socket->so_rcv,
15282c23ebaSBill Fenner 		    (struct sockaddr *)&ripsrc, m, opts) == 0) {
153df8bae1dSRodney W. Grimes 			m_freem(m);
15482c23ebaSBill Fenner 			if (opts)
15582c23ebaSBill Fenner 			    m_freem(opts);
15682c23ebaSBill Fenner 		} else
15782c23ebaSBill Fenner 			sorwakeup(last->inp_socket);
158df8bae1dSRodney W. Grimes 	} else {
159df8bae1dSRodney W. Grimes 		m_freem(m);
160df8bae1dSRodney W. Grimes               ipstat.ips_noproto++;
161df8bae1dSRodney W. Grimes               ipstat.ips_delivered--;
162df8bae1dSRodney W. Grimes       }
163df8bae1dSRodney W. Grimes }
164df8bae1dSRodney W. Grimes 
165df8bae1dSRodney W. Grimes /*
166df8bae1dSRodney W. Grimes  * Generate IP header and pass packet to ip_output.
167df8bae1dSRodney W. Grimes  * Tack on options user may have setup with control call.
168df8bae1dSRodney W. Grimes  */
169df8bae1dSRodney W. Grimes int
170df8bae1dSRodney W. Grimes rip_output(m, so, dst)
171df8bae1dSRodney W. Grimes 	register struct mbuf *m;
172df8bae1dSRodney W. Grimes 	struct socket *so;
173df8bae1dSRodney W. Grimes 	u_long dst;
174df8bae1dSRodney W. Grimes {
175df8bae1dSRodney W. Grimes 	register struct ip *ip;
176df8bae1dSRodney W. Grimes 	register struct inpcb *inp = sotoinpcb(so);
177df8bae1dSRodney W. Grimes 	int flags = (so->so_options & SO_DONTROUTE) | IP_ALLOWBROADCAST;
178df8bae1dSRodney W. Grimes 
179df8bae1dSRodney W. Grimes 	/*
180df8bae1dSRodney W. Grimes 	 * If the user handed us a complete IP packet, use it.
181df8bae1dSRodney W. Grimes 	 * Otherwise, allocate an mbuf for a header and fill it in.
182df8bae1dSRodney W. Grimes 	 */
183df8bae1dSRodney W. Grimes 	if ((inp->inp_flags & INP_HDRINCL) == 0) {
184430d30d8SBill Fenner 		if (m->m_pkthdr.len + sizeof(struct ip) > IP_MAXPACKET) {
185430d30d8SBill Fenner 			m_freem(m);
186430d30d8SBill Fenner 			return(EMSGSIZE);
187430d30d8SBill Fenner 		}
188df8bae1dSRodney W. Grimes 		M_PREPEND(m, sizeof(struct ip), M_WAIT);
189df8bae1dSRodney W. Grimes 		ip = mtod(m, struct ip *);
190df8bae1dSRodney W. Grimes 		ip->ip_tos = 0;
191df8bae1dSRodney W. Grimes 		ip->ip_off = 0;
192ca98b82cSDavid Greenman 		ip->ip_p = inp->inp_ip_p;
193df8bae1dSRodney W. Grimes 		ip->ip_len = m->m_pkthdr.len;
194df8bae1dSRodney W. Grimes 		ip->ip_src = inp->inp_laddr;
195df8bae1dSRodney W. Grimes 		ip->ip_dst.s_addr = dst;
196df8bae1dSRodney W. Grimes 		ip->ip_ttl = MAXTTL;
197df8bae1dSRodney W. Grimes 	} else {
198430d30d8SBill Fenner 		if (m->m_pkthdr.len > IP_MAXPACKET) {
199430d30d8SBill Fenner 			m_freem(m);
200430d30d8SBill Fenner 			return(EMSGSIZE);
201430d30d8SBill Fenner 		}
202df8bae1dSRodney W. Grimes 		ip = mtod(m, struct ip *);
203072b9b24SPaul Traina 		/* don't allow both user specified and setsockopt options,
204072b9b24SPaul Traina 		   and don't allow packet length sizes that will crash */
2055e2d0696SGarrett Wollman 		if (((IP_VHL_HL(ip->ip_vhl) != (sizeof (*ip) >> 2))
2065e2d0696SGarrett Wollman 		     && inp->inp_options)
20791108995SBill Fenner 		    || (ip->ip_len > m->m_pkthdr.len)
20891108995SBill Fenner 		    || (ip->ip_len < (IP_VHL_HL(ip->ip_vhl) << 2))) {
209072b9b24SPaul Traina 			m_freem(m);
210072b9b24SPaul Traina 			return EINVAL;
211072b9b24SPaul Traina 		}
212df8bae1dSRodney W. Grimes 		if (ip->ip_id == 0)
213df8bae1dSRodney W. Grimes 			ip->ip_id = htons(ip_id++);
214df8bae1dSRodney W. Grimes 		/* XXX prevent ip_output from overwriting header fields */
215df8bae1dSRodney W. Grimes 		flags |= IP_RAWOUTPUT;
216df8bae1dSRodney W. Grimes 		ipstat.ips_rawout++;
217df8bae1dSRodney W. Grimes 	}
218072b9b24SPaul Traina 	return (ip_output(m, inp->inp_options, &inp->inp_route, flags,
219072b9b24SPaul Traina 			  inp->inp_moptions));
220df8bae1dSRodney W. Grimes }
221df8bae1dSRodney W. Grimes 
222df8bae1dSRodney W. Grimes /*
223df8bae1dSRodney W. Grimes  * Raw IP socket option processing.
224df8bae1dSRodney W. Grimes  */
225df8bae1dSRodney W. Grimes int
226cfe8b629SGarrett Wollman rip_ctloutput(so, sopt)
227df8bae1dSRodney W. Grimes 	struct socket *so;
228cfe8b629SGarrett Wollman 	struct sockopt *sopt;
229df8bae1dSRodney W. Grimes {
230cfe8b629SGarrett Wollman 	struct	inpcb *inp = sotoinpcb(so);
231cfe8b629SGarrett Wollman 	int	error, optval;
232df8bae1dSRodney W. Grimes 
233cfe8b629SGarrett Wollman 	if (sopt->sopt_level != IPPROTO_IP)
234df8bae1dSRodney W. Grimes 		return (EINVAL);
235df8bae1dSRodney W. Grimes 
23625f26ad8SGarrett Wollman 	error = 0;
237cfe8b629SGarrett Wollman 
238cfe8b629SGarrett Wollman 	switch (sopt->sopt_dir) {
239cfe8b629SGarrett Wollman 	case SOPT_GET:
240cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
241cfe8b629SGarrett Wollman 		case IP_HDRINCL:
242cfe8b629SGarrett Wollman 			optval = inp->inp_flags & INP_HDRINCL;
243cfe8b629SGarrett Wollman 			error = sooptcopyout(sopt, &optval, sizeof optval);
244cfe8b629SGarrett Wollman 			break;
245df8bae1dSRodney W. Grimes 
24609bb5f75SPoul-Henning Kamp 		case IP_FW_GET:
247cfe8b629SGarrett Wollman 			if (ip_fw_ctl_ptr == 0)
248cfe8b629SGarrett Wollman 				error = ENOPROTOOPT;
249cfe8b629SGarrett Wollman 			else
250cfe8b629SGarrett Wollman 				error = ip_fw_ctl_ptr(sopt);
251cfe8b629SGarrett Wollman 			break;
2524dd1662bSUgen J.S. Antsilevich 
253b715f178SLuigi Rizzo #ifdef DUMMYNET
254b715f178SLuigi Rizzo 		case IP_DUMMYNET_GET:
255b715f178SLuigi Rizzo 			if (ip_dn_ctl_ptr == NULL)
256b715f178SLuigi Rizzo 				error = ENOPROTOOPT ;
257b715f178SLuigi Rizzo 			else
258b715f178SLuigi Rizzo 				error = ip_dn_ctl_ptr(sopt);
259b715f178SLuigi Rizzo 			break ;
260b715f178SLuigi Rizzo #endif /* DUMMYNET */
2611c5de19aSGarrett Wollman 
2621c5de19aSGarrett Wollman 		case MRT_INIT:
2631c5de19aSGarrett Wollman 		case MRT_DONE:
2641c5de19aSGarrett Wollman 		case MRT_ADD_VIF:
2651c5de19aSGarrett Wollman 		case MRT_DEL_VIF:
2661c5de19aSGarrett Wollman 		case MRT_ADD_MFC:
2671c5de19aSGarrett Wollman 		case MRT_DEL_MFC:
2681c5de19aSGarrett Wollman 		case MRT_VERSION:
2691c5de19aSGarrett Wollman 		case MRT_ASSERT:
270cfe8b629SGarrett Wollman 			error = ip_mrouter_get(so, sopt);
271cfe8b629SGarrett Wollman 			break;
272cfe8b629SGarrett Wollman 
273cfe8b629SGarrett Wollman 		default:
274cfe8b629SGarrett Wollman 			error = ip_ctloutput(so, sopt);
275cfe8b629SGarrett Wollman 			break;
276df8bae1dSRodney W. Grimes 		}
277cfe8b629SGarrett Wollman 		break;
278cfe8b629SGarrett Wollman 
279cfe8b629SGarrett Wollman 	case SOPT_SET:
280cfe8b629SGarrett Wollman 		switch (sopt->sopt_name) {
281cfe8b629SGarrett Wollman 		case IP_HDRINCL:
282cfe8b629SGarrett Wollman 			error = sooptcopyin(sopt, &optval, sizeof optval,
283cfe8b629SGarrett Wollman 					    sizeof optval);
284cfe8b629SGarrett Wollman 			if (error)
285cfe8b629SGarrett Wollman 				break;
286cfe8b629SGarrett Wollman 			if (optval)
287cfe8b629SGarrett Wollman 				inp->inp_flags |= INP_HDRINCL;
288cfe8b629SGarrett Wollman 			else
289cfe8b629SGarrett Wollman 				inp->inp_flags &= ~INP_HDRINCL;
290cfe8b629SGarrett Wollman 			break;
291cfe8b629SGarrett Wollman 
292cfe8b629SGarrett Wollman 		case IP_FW_ADD:
293cfe8b629SGarrett Wollman 		case IP_FW_DEL:
294cfe8b629SGarrett Wollman 		case IP_FW_FLUSH:
295cfe8b629SGarrett Wollman 		case IP_FW_ZERO:
296cfe8b629SGarrett Wollman 			if (ip_fw_ctl_ptr == 0)
297cfe8b629SGarrett Wollman 				error = ENOPROTOOPT;
298cfe8b629SGarrett Wollman 			else
299cfe8b629SGarrett Wollman 				error = ip_fw_ctl_ptr(sopt);
300cfe8b629SGarrett Wollman 			break;
301cfe8b629SGarrett Wollman 
302b715f178SLuigi Rizzo #ifdef DUMMYNET
303b715f178SLuigi Rizzo 		case IP_DUMMYNET_CONFIGURE:
304b715f178SLuigi Rizzo 		case IP_DUMMYNET_DEL:
305b715f178SLuigi Rizzo 		case IP_DUMMYNET_FLUSH:
306b715f178SLuigi Rizzo 			if (ip_dn_ctl_ptr == NULL)
307b715f178SLuigi Rizzo 				error = ENOPROTOOPT ;
308b715f178SLuigi Rizzo 			else
309b715f178SLuigi Rizzo 				error = ip_dn_ctl_ptr(sopt);
310b715f178SLuigi Rizzo 			break ;
311b715f178SLuigi Rizzo #endif
312cfe8b629SGarrett Wollman 
313cfe8b629SGarrett Wollman 		case IP_RSVP_ON:
314cfe8b629SGarrett Wollman 			error = ip_rsvp_init(so);
315cfe8b629SGarrett Wollman 			break;
316cfe8b629SGarrett Wollman 
317cfe8b629SGarrett Wollman 		case IP_RSVP_OFF:
318cfe8b629SGarrett Wollman 			error = ip_rsvp_done();
319cfe8b629SGarrett Wollman 			break;
320cfe8b629SGarrett Wollman 
321cfe8b629SGarrett Wollman 			/* XXX - should be combined */
322cfe8b629SGarrett Wollman 		case IP_RSVP_VIF_ON:
323cfe8b629SGarrett Wollman 			error = ip_rsvp_vif_init(so, sopt);
324cfe8b629SGarrett Wollman 			break;
325cfe8b629SGarrett Wollman 
326cfe8b629SGarrett Wollman 		case IP_RSVP_VIF_OFF:
327cfe8b629SGarrett Wollman 			error = ip_rsvp_vif_done(so, sopt);
328cfe8b629SGarrett Wollman 			break;
329cfe8b629SGarrett Wollman 
330cfe8b629SGarrett Wollman 		case MRT_INIT:
331cfe8b629SGarrett Wollman 		case MRT_DONE:
332cfe8b629SGarrett Wollman 		case MRT_ADD_VIF:
333cfe8b629SGarrett Wollman 		case MRT_DEL_VIF:
334cfe8b629SGarrett Wollman 		case MRT_ADD_MFC:
335cfe8b629SGarrett Wollman 		case MRT_DEL_MFC:
336cfe8b629SGarrett Wollman 		case MRT_VERSION:
337cfe8b629SGarrett Wollman 		case MRT_ASSERT:
338cfe8b629SGarrett Wollman 			error = ip_mrouter_set(so, sopt);
339cfe8b629SGarrett Wollman 			break;
340cfe8b629SGarrett Wollman 
341cfe8b629SGarrett Wollman 		default:
342cfe8b629SGarrett Wollman 			error = ip_ctloutput(so, sopt);
343cfe8b629SGarrett Wollman 			break;
344cfe8b629SGarrett Wollman 		}
345cfe8b629SGarrett Wollman 		break;
346cfe8b629SGarrett Wollman 	}
347cfe8b629SGarrett Wollman 
348cfe8b629SGarrett Wollman 	return (error);
349df8bae1dSRodney W. Grimes }
350df8bae1dSRodney W. Grimes 
35139191c8eSGarrett Wollman /*
35239191c8eSGarrett Wollman  * This function exists solely to receive the PRC_IFDOWN messages which
35339191c8eSGarrett Wollman  * are sent by if_down().  It looks for an ifaddr whose ifa_addr is sa,
35439191c8eSGarrett Wollman  * and calls in_ifadown() to remove all routes corresponding to that address.
35539191c8eSGarrett Wollman  * It also receives the PRC_IFUP messages from if_up() and reinstalls the
35639191c8eSGarrett Wollman  * interface routes.
35739191c8eSGarrett Wollman  */
35839191c8eSGarrett Wollman void
35939191c8eSGarrett Wollman rip_ctlinput(cmd, sa, vip)
36039191c8eSGarrett Wollman 	int cmd;
36139191c8eSGarrett Wollman 	struct sockaddr *sa;
36239191c8eSGarrett Wollman 	void *vip;
36339191c8eSGarrett Wollman {
36439191c8eSGarrett Wollman 	struct in_ifaddr *ia;
36539191c8eSGarrett Wollman 	struct ifnet *ifp;
36639191c8eSGarrett Wollman 	int err;
36739191c8eSGarrett Wollman 	int flags;
36839191c8eSGarrett Wollman 
36939191c8eSGarrett Wollman 	switch (cmd) {
37039191c8eSGarrett Wollman 	case PRC_IFDOWN:
37139191c8eSGarrett Wollman 		for (ia = in_ifaddrhead.tqh_first; ia;
37239191c8eSGarrett Wollman 		     ia = ia->ia_link.tqe_next) {
37339191c8eSGarrett Wollman 			if (ia->ia_ifa.ifa_addr == sa
37439191c8eSGarrett Wollman 			    && (ia->ia_flags & IFA_ROUTE)) {
37539191c8eSGarrett Wollman 				/*
37639191c8eSGarrett Wollman 				 * in_ifscrub kills the interface route.
37739191c8eSGarrett Wollman 				 */
37839191c8eSGarrett Wollman 				in_ifscrub(ia->ia_ifp, ia);
37939191c8eSGarrett Wollman 				/*
38039191c8eSGarrett Wollman 				 * in_ifadown gets rid of all the rest of
38139191c8eSGarrett Wollman 				 * the routes.  This is not quite the right
38239191c8eSGarrett Wollman 				 * thing to do, but at least if we are running
38339191c8eSGarrett Wollman 				 * a routing process they will come back.
38439191c8eSGarrett Wollman 				 */
38539191c8eSGarrett Wollman 				in_ifadown(&ia->ia_ifa);
38639191c8eSGarrett Wollman 				break;
38739191c8eSGarrett Wollman 			}
38839191c8eSGarrett Wollman 		}
38939191c8eSGarrett Wollman 		break;
39039191c8eSGarrett Wollman 
39139191c8eSGarrett Wollman 	case PRC_IFUP:
39239191c8eSGarrett Wollman 		for (ia = in_ifaddrhead.tqh_first; ia;
39339191c8eSGarrett Wollman 		     ia = ia->ia_link.tqe_next) {
39439191c8eSGarrett Wollman 			if (ia->ia_ifa.ifa_addr == sa)
39539191c8eSGarrett Wollman 				break;
39639191c8eSGarrett Wollman 		}
39739191c8eSGarrett Wollman 		if (ia == 0 || (ia->ia_flags & IFA_ROUTE))
39839191c8eSGarrett Wollman 			return;
39939191c8eSGarrett Wollman 		flags = RTF_UP;
40039191c8eSGarrett Wollman 		ifp = ia->ia_ifa.ifa_ifp;
40139191c8eSGarrett Wollman 
40239191c8eSGarrett Wollman 		if ((ifp->if_flags & IFF_LOOPBACK)
40339191c8eSGarrett Wollman 		    || (ifp->if_flags & IFF_POINTOPOINT))
40439191c8eSGarrett Wollman 			flags |= RTF_HOST;
40539191c8eSGarrett Wollman 
40639191c8eSGarrett Wollman 		err = rtinit(&ia->ia_ifa, RTM_ADD, flags);
40739191c8eSGarrett Wollman 		if (err == 0)
40839191c8eSGarrett Wollman 			ia->ia_flags |= IFA_ROUTE;
40939191c8eSGarrett Wollman 		break;
41039191c8eSGarrett Wollman 	}
41139191c8eSGarrett Wollman }
41239191c8eSGarrett Wollman 
413117bcae7SGarrett Wollman static u_long	rip_sendspace = RIPSNDQ;
414117bcae7SGarrett Wollman static u_long	rip_recvspace = RIPRCVQ;
415df8bae1dSRodney W. Grimes 
416117bcae7SGarrett Wollman SYSCTL_INT(_net_inet_raw, OID_AUTO, maxdgram, CTLFLAG_RW, &rip_sendspace,
417117bcae7SGarrett Wollman 	   0, "");
418117bcae7SGarrett Wollman SYSCTL_INT(_net_inet_raw, OID_AUTO, recvspace, CTLFLAG_RW, &rip_recvspace,
419117bcae7SGarrett Wollman 	   0, "");
420117bcae7SGarrett Wollman 
421117bcae7SGarrett Wollman static int
422a29f300eSGarrett Wollman rip_attach(struct socket *so, int proto, struct proc *p)
423df8bae1dSRodney W. Grimes {
424117bcae7SGarrett Wollman 	struct inpcb *inp;
42586b3ebceSDavid Greenman 	int error, s;
426c1f8a6ceSDavid Greenman 
427117bcae7SGarrett Wollman 	inp = sotoinpcb(so);
428df8bae1dSRodney W. Grimes 	if (inp)
429df8bae1dSRodney W. Grimes 		panic("rip_attach");
430f711d546SPoul-Henning Kamp 	if (p && (error = suser(p)) != 0)
431a29f300eSGarrett Wollman 		return error;
432117bcae7SGarrett Wollman 
43386b3ebceSDavid Greenman 	s = splnet();
43486b3ebceSDavid Greenman 	error = in_pcballoc(so, &ripcbinfo, p);
43586b3ebceSDavid Greenman 	splx(s);
43686b3ebceSDavid Greenman 	if (error)
43786b3ebceSDavid Greenman 		return error;
43886b3ebceSDavid Greenman 	error = soreserve(so, rip_sendspace, rip_recvspace);
43986b3ebceSDavid Greenman 	if (error)
440117bcae7SGarrett Wollman 		return error;
441df8bae1dSRodney W. Grimes 	inp = (struct inpcb *)so->so_pcb;
442ca98b82cSDavid Greenman 	inp->inp_ip_p = proto;
443117bcae7SGarrett Wollman 	return 0;
444df8bae1dSRodney W. Grimes }
445117bcae7SGarrett Wollman 
446117bcae7SGarrett Wollman static int
447117bcae7SGarrett Wollman rip_detach(struct socket *so)
448117bcae7SGarrett Wollman {
449117bcae7SGarrett Wollman 	struct inpcb *inp;
450117bcae7SGarrett Wollman 
451117bcae7SGarrett Wollman 	inp = sotoinpcb(so);
452df8bae1dSRodney W. Grimes 	if (inp == 0)
453df8bae1dSRodney W. Grimes 		panic("rip_detach");
454df8bae1dSRodney W. Grimes 	if (so == ip_mrouter)
455df8bae1dSRodney W. Grimes 		ip_mrouter_done();
456b4489dc3SGarrett Wollman 	ip_rsvp_force_done(so);
457838ecf42SGarrett Wollman 	if (so == ip_rsvpd)
458838ecf42SGarrett Wollman 		ip_rsvp_done();
459df8bae1dSRodney W. Grimes 	in_pcbdetach(inp);
460117bcae7SGarrett Wollman 	return 0;
461117bcae7SGarrett Wollman }
462df8bae1dSRodney W. Grimes 
463117bcae7SGarrett Wollman static int
464117bcae7SGarrett Wollman rip_abort(struct socket *so)
465df8bae1dSRodney W. Grimes {
466117bcae7SGarrett Wollman 	soisdisconnected(so);
467117bcae7SGarrett Wollman 	return rip_detach(so);
468117bcae7SGarrett Wollman }
469117bcae7SGarrett Wollman 
470117bcae7SGarrett Wollman static int
471117bcae7SGarrett Wollman rip_disconnect(struct socket *so)
472117bcae7SGarrett Wollman {
473117bcae7SGarrett Wollman 	if ((so->so_state & SS_ISCONNECTED) == 0)
474117bcae7SGarrett Wollman 		return ENOTCONN;
475117bcae7SGarrett Wollman 	return rip_abort(so);
476117bcae7SGarrett Wollman }
477117bcae7SGarrett Wollman 
478117bcae7SGarrett Wollman static int
47957bf258eSGarrett Wollman rip_bind(struct socket *so, struct sockaddr *nam, struct proc *p)
480117bcae7SGarrett Wollman {
481117bcae7SGarrett Wollman 	struct inpcb *inp = sotoinpcb(so);
48257bf258eSGarrett Wollman 	struct sockaddr_in *addr = (struct sockaddr_in *)nam;
483df8bae1dSRodney W. Grimes 
48457bf258eSGarrett Wollman 	if (nam->sa_len != sizeof(*addr))
485117bcae7SGarrett Wollman 		return EINVAL;
486117bcae7SGarrett Wollman 
487117bcae7SGarrett Wollman 	if (TAILQ_EMPTY(&ifnet) || ((addr->sin_family != AF_INET) &&
488df8bae1dSRodney W. Grimes 				    (addr->sin_family != AF_IMPLINK)) ||
489df8bae1dSRodney W. Grimes 	    (addr->sin_addr.s_addr &&
490117bcae7SGarrett Wollman 	     ifa_ifwithaddr((struct sockaddr *)addr) == 0))
491117bcae7SGarrett Wollman 		return EADDRNOTAVAIL;
492df8bae1dSRodney W. Grimes 	inp->inp_laddr = addr->sin_addr;
493117bcae7SGarrett Wollman 	return 0;
494df8bae1dSRodney W. Grimes }
495117bcae7SGarrett Wollman 
496117bcae7SGarrett Wollman static int
49757bf258eSGarrett Wollman rip_connect(struct socket *so, struct sockaddr *nam, struct proc *p)
498df8bae1dSRodney W. Grimes {
499117bcae7SGarrett Wollman 	struct inpcb *inp = sotoinpcb(so);
50057bf258eSGarrett Wollman 	struct sockaddr_in *addr = (struct sockaddr_in *)nam;
501df8bae1dSRodney W. Grimes 
50257bf258eSGarrett Wollman 	if (nam->sa_len != sizeof(*addr))
503117bcae7SGarrett Wollman 		return EINVAL;
504117bcae7SGarrett Wollman 	if (TAILQ_EMPTY(&ifnet))
505117bcae7SGarrett Wollman 		return EADDRNOTAVAIL;
506df8bae1dSRodney W. Grimes 	if ((addr->sin_family != AF_INET) &&
507117bcae7SGarrett Wollman 	    (addr->sin_family != AF_IMPLINK))
508117bcae7SGarrett Wollman 		return EAFNOSUPPORT;
509df8bae1dSRodney W. Grimes 	inp->inp_faddr = addr->sin_addr;
510df8bae1dSRodney W. Grimes 	soisconnected(so);
511117bcae7SGarrett Wollman 	return 0;
512df8bae1dSRodney W. Grimes }
513df8bae1dSRodney W. Grimes 
514117bcae7SGarrett Wollman static int
515117bcae7SGarrett Wollman rip_shutdown(struct socket *so)
516df8bae1dSRodney W. Grimes {
517117bcae7SGarrett Wollman 	socantsendmore(so);
518117bcae7SGarrett Wollman 	return 0;
519117bcae7SGarrett Wollman }
520117bcae7SGarrett Wollman 
521117bcae7SGarrett Wollman static int
52257bf258eSGarrett Wollman rip_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
523a29f300eSGarrett Wollman 	 struct mbuf *control, struct proc *p)
524117bcae7SGarrett Wollman {
525117bcae7SGarrett Wollman 	struct inpcb *inp = sotoinpcb(so);
526df8bae1dSRodney W. Grimes 	register u_long dst;
527df8bae1dSRodney W. Grimes 
528df8bae1dSRodney W. Grimes 	if (so->so_state & SS_ISCONNECTED) {
529df8bae1dSRodney W. Grimes 		if (nam) {
530117bcae7SGarrett Wollman 			m_freem(m);
531117bcae7SGarrett Wollman 			return EISCONN;
532df8bae1dSRodney W. Grimes 		}
533df8bae1dSRodney W. Grimes 		dst = inp->inp_faddr.s_addr;
534df8bae1dSRodney W. Grimes 	} else {
535df8bae1dSRodney W. Grimes 		if (nam == NULL) {
536117bcae7SGarrett Wollman 			m_freem(m);
537117bcae7SGarrett Wollman 			return ENOTCONN;
538df8bae1dSRodney W. Grimes 		}
53957bf258eSGarrett Wollman 		dst = ((struct sockaddr_in *)nam)->sin_addr.s_addr;
540df8bae1dSRodney W. Grimes 	}
541117bcae7SGarrett Wollman 	return rip_output(m, so, dst);
542df8bae1dSRodney W. Grimes }
543df8bae1dSRodney W. Grimes 
54498271db4SGarrett Wollman static int
54598271db4SGarrett Wollman rip_pcblist SYSCTL_HANDLER_ARGS
54698271db4SGarrett Wollman {
54798271db4SGarrett Wollman 	int error, i, n, s;
54898271db4SGarrett Wollman 	struct inpcb *inp, **inp_list;
54998271db4SGarrett Wollman 	inp_gen_t gencnt;
55098271db4SGarrett Wollman 	struct xinpgen xig;
55198271db4SGarrett Wollman 
55298271db4SGarrett Wollman 	/*
55398271db4SGarrett Wollman 	 * The process of preparing the TCB list is too time-consuming and
55498271db4SGarrett Wollman 	 * resource-intensive to repeat twice on every request.
55598271db4SGarrett Wollman 	 */
55698271db4SGarrett Wollman 	if (req->oldptr == 0) {
55798271db4SGarrett Wollman 		n = ripcbinfo.ipi_count;
55898271db4SGarrett Wollman 		req->oldidx = 2 * (sizeof xig)
55998271db4SGarrett Wollman 			+ (n + n/8) * sizeof(struct xinpcb);
56098271db4SGarrett Wollman 		return 0;
56198271db4SGarrett Wollman 	}
56298271db4SGarrett Wollman 
56398271db4SGarrett Wollman 	if (req->newptr != 0)
56498271db4SGarrett Wollman 		return EPERM;
56598271db4SGarrett Wollman 
56698271db4SGarrett Wollman 	/*
56798271db4SGarrett Wollman 	 * OK, now we're committed to doing something.
56898271db4SGarrett Wollman 	 */
56998271db4SGarrett Wollman 	s = splnet();
57098271db4SGarrett Wollman 	gencnt = ripcbinfo.ipi_gencnt;
57198271db4SGarrett Wollman 	n = ripcbinfo.ipi_count;
57298271db4SGarrett Wollman 	splx(s);
57398271db4SGarrett Wollman 
57498271db4SGarrett Wollman 	xig.xig_len = sizeof xig;
57598271db4SGarrett Wollman 	xig.xig_count = n;
57698271db4SGarrett Wollman 	xig.xig_gen = gencnt;
57798271db4SGarrett Wollman 	xig.xig_sogen = so_gencnt;
57898271db4SGarrett Wollman 	error = SYSCTL_OUT(req, &xig, sizeof xig);
57998271db4SGarrett Wollman 	if (error)
58098271db4SGarrett Wollman 		return error;
58198271db4SGarrett Wollman 
58298271db4SGarrett Wollman 	inp_list = malloc(n * sizeof *inp_list, M_TEMP, M_WAITOK);
58398271db4SGarrett Wollman 	if (inp_list == 0)
58498271db4SGarrett Wollman 		return ENOMEM;
58598271db4SGarrett Wollman 
58698271db4SGarrett Wollman 	s = splnet();
58798271db4SGarrett Wollman 	for (inp = ripcbinfo.listhead->lh_first, i = 0; inp && i < n;
58898271db4SGarrett Wollman 	     inp = inp->inp_list.le_next) {
58998271db4SGarrett Wollman 		if (inp->inp_gencnt <= gencnt)
59098271db4SGarrett Wollman 			inp_list[i++] = inp;
59198271db4SGarrett Wollman 	}
59298271db4SGarrett Wollman 	splx(s);
59398271db4SGarrett Wollman 	n = i;
59498271db4SGarrett Wollman 
59598271db4SGarrett Wollman 	error = 0;
59698271db4SGarrett Wollman 	for (i = 0; i < n; i++) {
59798271db4SGarrett Wollman 		inp = inp_list[i];
59898271db4SGarrett Wollman 		if (inp->inp_gencnt <= gencnt) {
59998271db4SGarrett Wollman 			struct xinpcb xi;
60098271db4SGarrett Wollman 			xi.xi_len = sizeof xi;
60198271db4SGarrett Wollman 			/* XXX should avoid extra copy */
60298271db4SGarrett Wollman 			bcopy(inp, &xi.xi_inp, sizeof *inp);
60398271db4SGarrett Wollman 			if (inp->inp_socket)
60498271db4SGarrett Wollman 				sotoxsocket(inp->inp_socket, &xi.xi_socket);
60598271db4SGarrett Wollman 			error = SYSCTL_OUT(req, &xi, sizeof xi);
60698271db4SGarrett Wollman 		}
60798271db4SGarrett Wollman 	}
60898271db4SGarrett Wollman 	if (!error) {
60998271db4SGarrett Wollman 		/*
61098271db4SGarrett Wollman 		 * Give the user an updated idea of our state.
61198271db4SGarrett Wollman 		 * If the generation differs from what we told
61298271db4SGarrett Wollman 		 * her before, she knows that something happened
61398271db4SGarrett Wollman 		 * while we were processing this request, and it
61498271db4SGarrett Wollman 		 * might be necessary to retry.
61598271db4SGarrett Wollman 		 */
61698271db4SGarrett Wollman 		s = splnet();
61798271db4SGarrett Wollman 		xig.xig_gen = ripcbinfo.ipi_gencnt;
61898271db4SGarrett Wollman 		xig.xig_sogen = so_gencnt;
61998271db4SGarrett Wollman 		xig.xig_count = ripcbinfo.ipi_count;
62098271db4SGarrett Wollman 		splx(s);
62198271db4SGarrett Wollman 		error = SYSCTL_OUT(req, &xig, sizeof xig);
62298271db4SGarrett Wollman 	}
62398271db4SGarrett Wollman 	free(inp_list, M_TEMP);
62498271db4SGarrett Wollman 	return error;
62598271db4SGarrett Wollman }
62698271db4SGarrett Wollman 
62798271db4SGarrett Wollman SYSCTL_PROC(_net_inet_raw, OID_AUTO/*XXX*/, pcblist, CTLFLAG_RD, 0, 0,
62898271db4SGarrett Wollman 	    rip_pcblist, "S,xinpcb", "List of active raw IP sockets");
62998271db4SGarrett Wollman 
630117bcae7SGarrett Wollman struct pr_usrreqs rip_usrreqs = {
631117bcae7SGarrett Wollman 	rip_abort, pru_accept_notsupp, rip_attach, rip_bind, rip_connect,
632117bcae7SGarrett Wollman 	pru_connect2_notsupp, in_control, rip_detach, rip_disconnect,
633117bcae7SGarrett Wollman 	pru_listen_notsupp, in_setpeeraddr, pru_rcvd_notsupp,
634117bcae7SGarrett Wollman 	pru_rcvoob_notsupp, rip_send, pru_sense_null, rip_shutdown,
635f8f6cbbaSPeter Wemm 	in_setsockaddr, sosend, soreceive, sopoll
636117bcae7SGarrett Wollman };
637