xref: /freebsd/sys/netinet/raw_ip.c (revision f8f6cbba927c69012c21cc4922b799a377f52f4f)
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
34f8f6cbbaSPeter Wemm  *	$Id: raw_ip.c,v 1.48 1997/08/16 19:15:37 wollman 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>
47df8bae1dSRodney W. Grimes 
48df8bae1dSRodney W. Grimes #include <net/if.h>
49df8bae1dSRodney W. Grimes #include <net/route.h>
50df8bae1dSRodney W. Grimes 
515e2d0696SGarrett Wollman #define _IP_VHL
52df8bae1dSRodney W. Grimes #include <netinet/in.h>
53df8bae1dSRodney W. Grimes #include <netinet/in_systm.h>
54df8bae1dSRodney W. Grimes #include <netinet/ip.h>
55c1f8a6ceSDavid Greenman #include <netinet/in_pcb.h>
56c1f8a6ceSDavid Greenman #include <netinet/in_var.h>
57df8bae1dSRodney W. Grimes #include <netinet/ip_var.h>
58df8bae1dSRodney W. Grimes #include <netinet/ip_mroute.h>
59df8bae1dSRodney W. Grimes 
60100ba1a6SJordan K. Hubbard #include <netinet/ip_fw.h>
61100ba1a6SJordan K. Hubbard 
62f9493383SGarrett Wollman #if !defined(COMPAT_IPFW) || COMPAT_IPFW == 1
63f9493383SGarrett Wollman #undef COMPAT_IPFW
64f9493383SGarrett Wollman #define COMPAT_IPFW 1
65f9493383SGarrett Wollman #else
66f9493383SGarrett Wollman #undef COMPAT_IPFW
67f9493383SGarrett Wollman #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);
96df8bae1dSRodney W. Grimes }
97df8bae1dSRodney W. Grimes 
98f6d24a78SPoul-Henning Kamp static struct	sockaddr_in ripsrc = { sizeof(ripsrc), AF_INET };
99df8bae1dSRodney W. Grimes /*
100df8bae1dSRodney W. Grimes  * Setup generic address and protocol structures
101df8bae1dSRodney W. Grimes  * for raw_input routine, then pass them along with
102df8bae1dSRodney W. Grimes  * mbuf chain.
103df8bae1dSRodney W. Grimes  */
104df8bae1dSRodney W. Grimes void
105e62b8c49SBill Fenner rip_input(m, iphlen)
106df8bae1dSRodney W. Grimes 	struct mbuf *m;
107e62b8c49SBill Fenner 	int iphlen;
108df8bae1dSRodney W. Grimes {
109df8bae1dSRodney W. Grimes 	register struct ip *ip = mtod(m, struct ip *);
110df8bae1dSRodney W. Grimes 	register struct inpcb *inp;
11182c23ebaSBill Fenner 	struct inpcb *last = 0;
11282c23ebaSBill Fenner 	struct mbuf *opts = 0;
113df8bae1dSRodney W. Grimes 
114df8bae1dSRodney W. Grimes 	ripsrc.sin_addr = ip->ip_src;
11515bd2b43SDavid Greenman 	for (inp = ripcb.lh_first; inp != NULL; inp = inp->inp_list.le_next) {
116ca98b82cSDavid Greenman 		if (inp->inp_ip_p && inp->inp_ip_p != ip->ip_p)
117df8bae1dSRodney W. Grimes 			continue;
118df8bae1dSRodney W. Grimes 		if (inp->inp_laddr.s_addr &&
119d99c7a23SGarrett Wollman                   inp->inp_laddr.s_addr != ip->ip_dst.s_addr)
120df8bae1dSRodney W. Grimes 			continue;
121df8bae1dSRodney W. Grimes 		if (inp->inp_faddr.s_addr &&
122d99c7a23SGarrett Wollman                   inp->inp_faddr.s_addr != ip->ip_src.s_addr)
123df8bae1dSRodney W. Grimes 			continue;
124df8bae1dSRodney W. Grimes 		if (last) {
125623ae52eSPoul-Henning Kamp 			struct mbuf *n = m_copy(m, 0, (int)M_COPYALL);
126623ae52eSPoul-Henning Kamp 			if (n) {
12782c23ebaSBill Fenner 				if (last->inp_flags & INP_CONTROLOPTS ||
12882c23ebaSBill Fenner 				    last->inp_socket->so_options & SO_TIMESTAMP)
12982c23ebaSBill Fenner 				    ip_savecontrol(last, &opts, ip, n);
13082c23ebaSBill Fenner 				if (sbappendaddr(&last->inp_socket->so_rcv,
131623ae52eSPoul-Henning Kamp 				    (struct sockaddr *)&ripsrc, n,
13282c23ebaSBill Fenner 				    opts) == 0) {
133df8bae1dSRodney W. Grimes 					/* should notify about lost packet */
134df8bae1dSRodney W. Grimes 					m_freem(n);
13582c23ebaSBill Fenner 					if (opts)
13682c23ebaSBill Fenner 					    m_freem(opts);
13782c23ebaSBill Fenner 				} else
13882c23ebaSBill Fenner 					sorwakeup(last->inp_socket);
13982c23ebaSBill Fenner 				opts = 0;
140df8bae1dSRodney W. Grimes 			}
141df8bae1dSRodney W. Grimes 		}
14282c23ebaSBill Fenner 		last = inp;
143df8bae1dSRodney W. Grimes 	}
144df8bae1dSRodney W. Grimes 	if (last) {
14582c23ebaSBill Fenner 		if (last->inp_flags & INP_CONTROLOPTS ||
14682c23ebaSBill Fenner 		    last->inp_socket->so_options & SO_TIMESTAMP)
14782c23ebaSBill Fenner 			ip_savecontrol(last, &opts, ip, m);
14882c23ebaSBill Fenner 		if (sbappendaddr(&last->inp_socket->so_rcv,
14982c23ebaSBill Fenner 		    (struct sockaddr *)&ripsrc, m, opts) == 0) {
150df8bae1dSRodney W. Grimes 			m_freem(m);
15182c23ebaSBill Fenner 			if (opts)
15282c23ebaSBill Fenner 			    m_freem(opts);
15382c23ebaSBill Fenner 		} else
15482c23ebaSBill Fenner 			sorwakeup(last->inp_socket);
155df8bae1dSRodney W. Grimes 	} else {
156df8bae1dSRodney W. Grimes 		m_freem(m);
157df8bae1dSRodney W. Grimes               ipstat.ips_noproto++;
158df8bae1dSRodney W. Grimes               ipstat.ips_delivered--;
159df8bae1dSRodney W. Grimes       }
160df8bae1dSRodney W. Grimes }
161df8bae1dSRodney W. Grimes 
162df8bae1dSRodney W. Grimes /*
163df8bae1dSRodney W. Grimes  * Generate IP header and pass packet to ip_output.
164df8bae1dSRodney W. Grimes  * Tack on options user may have setup with control call.
165df8bae1dSRodney W. Grimes  */
166df8bae1dSRodney W. Grimes int
167df8bae1dSRodney W. Grimes rip_output(m, so, dst)
168df8bae1dSRodney W. Grimes 	register struct mbuf *m;
169df8bae1dSRodney W. Grimes 	struct socket *so;
170df8bae1dSRodney W. Grimes 	u_long dst;
171df8bae1dSRodney W. Grimes {
172df8bae1dSRodney W. Grimes 	register struct ip *ip;
173df8bae1dSRodney W. Grimes 	register struct inpcb *inp = sotoinpcb(so);
174df8bae1dSRodney W. Grimes 	int flags = (so->so_options & SO_DONTROUTE) | IP_ALLOWBROADCAST;
175df8bae1dSRodney W. Grimes 
176df8bae1dSRodney W. Grimes 	/*
177df8bae1dSRodney W. Grimes 	 * If the user handed us a complete IP packet, use it.
178df8bae1dSRodney W. Grimes 	 * Otherwise, allocate an mbuf for a header and fill it in.
179df8bae1dSRodney W. Grimes 	 */
180df8bae1dSRodney W. Grimes 	if ((inp->inp_flags & INP_HDRINCL) == 0) {
181430d30d8SBill Fenner 		if (m->m_pkthdr.len + sizeof(struct ip) > IP_MAXPACKET) {
182430d30d8SBill Fenner 			m_freem(m);
183430d30d8SBill Fenner 			return(EMSGSIZE);
184430d30d8SBill Fenner 		}
185df8bae1dSRodney W. Grimes 		M_PREPEND(m, sizeof(struct ip), M_WAIT);
186df8bae1dSRodney W. Grimes 		ip = mtod(m, struct ip *);
187df8bae1dSRodney W. Grimes 		ip->ip_tos = 0;
188df8bae1dSRodney W. Grimes 		ip->ip_off = 0;
189ca98b82cSDavid Greenman 		ip->ip_p = inp->inp_ip_p;
190df8bae1dSRodney W. Grimes 		ip->ip_len = m->m_pkthdr.len;
191df8bae1dSRodney W. Grimes 		ip->ip_src = inp->inp_laddr;
192df8bae1dSRodney W. Grimes 		ip->ip_dst.s_addr = dst;
193df8bae1dSRodney W. Grimes 		ip->ip_ttl = MAXTTL;
194df8bae1dSRodney W. Grimes 	} else {
195430d30d8SBill Fenner 		if (m->m_pkthdr.len > IP_MAXPACKET) {
196430d30d8SBill Fenner 			m_freem(m);
197430d30d8SBill Fenner 			return(EMSGSIZE);
198430d30d8SBill Fenner 		}
199df8bae1dSRodney W. Grimes 		ip = mtod(m, struct ip *);
200072b9b24SPaul Traina 		/* don't allow both user specified and setsockopt options,
201072b9b24SPaul Traina 		   and don't allow packet length sizes that will crash */
2025e2d0696SGarrett Wollman 		if (((IP_VHL_HL(ip->ip_vhl) != (sizeof (*ip) >> 2))
2035e2d0696SGarrett Wollman 		     && inp->inp_options)
20491108995SBill Fenner 		    || (ip->ip_len > m->m_pkthdr.len)
20591108995SBill Fenner 		    || (ip->ip_len < (IP_VHL_HL(ip->ip_vhl) << 2))) {
206072b9b24SPaul Traina 			m_freem(m);
207072b9b24SPaul Traina 			return EINVAL;
208072b9b24SPaul Traina 		}
209df8bae1dSRodney W. Grimes 		if (ip->ip_id == 0)
210df8bae1dSRodney W. Grimes 			ip->ip_id = htons(ip_id++);
211df8bae1dSRodney W. Grimes 		/* XXX prevent ip_output from overwriting header fields */
212df8bae1dSRodney W. Grimes 		flags |= IP_RAWOUTPUT;
213df8bae1dSRodney W. Grimes 		ipstat.ips_rawout++;
214df8bae1dSRodney W. Grimes 	}
215072b9b24SPaul Traina 	return (ip_output(m, inp->inp_options, &inp->inp_route, flags,
216072b9b24SPaul Traina 			  inp->inp_moptions));
217df8bae1dSRodney W. Grimes }
218df8bae1dSRodney W. Grimes 
219df8bae1dSRodney W. Grimes /*
220df8bae1dSRodney W. Grimes  * Raw IP socket option processing.
221df8bae1dSRodney W. Grimes  */
222df8bae1dSRodney W. Grimes int
223a29f300eSGarrett Wollman rip_ctloutput(op, so, level, optname, m, p)
224df8bae1dSRodney W. Grimes 	int op;
225df8bae1dSRodney W. Grimes 	struct socket *so;
226df8bae1dSRodney W. Grimes 	int level, optname;
227df8bae1dSRodney W. Grimes 	struct mbuf **m;
228a29f300eSGarrett Wollman 	struct proc *p;
229df8bae1dSRodney W. Grimes {
230df8bae1dSRodney W. Grimes 	register struct inpcb *inp = sotoinpcb(so);
231df8bae1dSRodney W. Grimes 	register int error;
232df8bae1dSRodney W. Grimes 
233aedcdea1SDavid Greenman 	if (level != IPPROTO_IP) {
234aedcdea1SDavid Greenman 		if (op == PRCO_SETOPT && *m)
235aedcdea1SDavid Greenman 			(void)m_free(*m);
236df8bae1dSRodney W. Grimes 		return (EINVAL);
237aedcdea1SDavid Greenman 	}
238df8bae1dSRodney W. Grimes 
239df8bae1dSRodney W. Grimes 	switch (optname) {
240df8bae1dSRodney W. Grimes 
241df8bae1dSRodney W. Grimes 	case IP_HDRINCL:
24225f26ad8SGarrett Wollman 		error = 0;
243df8bae1dSRodney W. Grimes 		if (op == PRCO_SETOPT) {
24425f26ad8SGarrett Wollman 			if (m == 0 || *m == 0 || (*m)->m_len < sizeof (int))
24525f26ad8SGarrett Wollman 				error = EINVAL;
24625f26ad8SGarrett Wollman 			else if (*mtod(*m, int *))
247df8bae1dSRodney W. Grimes 				inp->inp_flags |= INP_HDRINCL;
248df8bae1dSRodney W. Grimes 			else
249df8bae1dSRodney W. Grimes 				inp->inp_flags &= ~INP_HDRINCL;
25025f26ad8SGarrett Wollman 			if (*m)
251df8bae1dSRodney W. Grimes 				(void)m_free(*m);
252df8bae1dSRodney W. Grimes 		} else {
25325f26ad8SGarrett Wollman 			*m = m_get(M_WAIT, MT_SOOPTS);
254df8bae1dSRodney W. Grimes 			(*m)->m_len = sizeof (int);
255df8bae1dSRodney W. Grimes 			*mtod(*m, int *) = inp->inp_flags & INP_HDRINCL;
256df8bae1dSRodney W. Grimes 		}
25725f26ad8SGarrett Wollman 		return (error);
258df8bae1dSRodney W. Grimes 
259f9493383SGarrett Wollman #ifdef COMPAT_IPFW
26009bb5f75SPoul-Henning Kamp 	case IP_FW_GET:
26109bb5f75SPoul-Henning Kamp 		if (ip_fw_ctl_ptr == NULL || op == PRCO_SETOPT) {
26209bb5f75SPoul-Henning Kamp 			if (*m) (void)m_free(*m);
26309bb5f75SPoul-Henning Kamp 			return(EINVAL);
26409bb5f75SPoul-Henning Kamp 		}
26509bb5f75SPoul-Henning Kamp 		return (*ip_fw_ctl_ptr)(optname, m);
266fed1c7e9SSøren Schmidt 
2674dd1662bSUgen J.S. Antsilevich 	case IP_FW_ADD:
2684dd1662bSUgen J.S. Antsilevich 	case IP_FW_DEL:
269100ba1a6SJordan K. Hubbard 	case IP_FW_FLUSH:
270e7319babSPoul-Henning Kamp 	case IP_FW_ZERO:
27109bb5f75SPoul-Henning Kamp 		if (ip_fw_ctl_ptr == NULL || op != PRCO_SETOPT) {
27209bb5f75SPoul-Henning Kamp 			if (*m) (void)m_free(*m);
2734dd1662bSUgen J.S. Antsilevich 			return(EINVAL);
2744dd1662bSUgen J.S. Antsilevich 		}
27509bb5f75SPoul-Henning Kamp 		return (*ip_fw_ctl_ptr)(optname, m);
2764dd1662bSUgen J.S. Antsilevich 
277fed1c7e9SSøren Schmidt 	case IP_NAT:
278fed1c7e9SSøren Schmidt 		if (ip_nat_ctl_ptr == NULL) {
279fed1c7e9SSøren Schmidt 			if (*m) (void)m_free(*m);
280fed1c7e9SSøren Schmidt 			return(EINVAL);
281fed1c7e9SSøren Schmidt 		}
2828f52c187SSøren Schmidt 		return (*ip_nat_ctl_ptr)(op, m);
283fed1c7e9SSøren Schmidt 
28458938916SGarrett Wollman #endif
285f0068c4aSGarrett Wollman 	case IP_RSVP_ON:
286479bb8daSGarrett Wollman 		return ip_rsvp_init(so);
287f0068c4aSGarrett Wollman 		break;
288f0068c4aSGarrett Wollman 
289f0068c4aSGarrett Wollman 	case IP_RSVP_OFF:
290479bb8daSGarrett Wollman 		return ip_rsvp_done();
291f0068c4aSGarrett Wollman 		break;
292f0068c4aSGarrett Wollman 
2931c5de19aSGarrett Wollman 	case IP_RSVP_VIF_ON:
2941c5de19aSGarrett Wollman 		return ip_rsvp_vif_init(so, *m);
2951c5de19aSGarrett Wollman 
2961c5de19aSGarrett Wollman 	case IP_RSVP_VIF_OFF:
2971c5de19aSGarrett Wollman 		return ip_rsvp_vif_done(so, *m);
2981c5de19aSGarrett Wollman 
2991c5de19aSGarrett Wollman 	case MRT_INIT:
3001c5de19aSGarrett Wollman 	case MRT_DONE:
3011c5de19aSGarrett Wollman 	case MRT_ADD_VIF:
3021c5de19aSGarrett Wollman 	case MRT_DEL_VIF:
3031c5de19aSGarrett Wollman 	case MRT_ADD_MFC:
3041c5de19aSGarrett Wollman 	case MRT_DEL_MFC:
3051c5de19aSGarrett Wollman 	case MRT_VERSION:
3061c5de19aSGarrett Wollman 	case MRT_ASSERT:
307df8bae1dSRodney W. Grimes 		if (op == PRCO_SETOPT) {
3081c5de19aSGarrett Wollman 			error = ip_mrouter_set(optname, so, *m);
309df8bae1dSRodney W. Grimes 			if (*m)
310df8bae1dSRodney W. Grimes 				(void)m_free(*m);
3111c5de19aSGarrett Wollman 		} else if (op == PRCO_GETOPT) {
3121c5de19aSGarrett Wollman 			error = ip_mrouter_get(optname, so, m);
313df8bae1dSRodney W. Grimes 		} else
314df8bae1dSRodney W. Grimes 			error = EINVAL;
315df8bae1dSRodney W. Grimes 		return (error);
316df8bae1dSRodney W. Grimes 	}
317a29f300eSGarrett Wollman 	return (ip_ctloutput(op, so, level, optname, m, p));
318df8bae1dSRodney W. Grimes }
319df8bae1dSRodney W. Grimes 
32039191c8eSGarrett Wollman /*
32139191c8eSGarrett Wollman  * This function exists solely to receive the PRC_IFDOWN messages which
32239191c8eSGarrett Wollman  * are sent by if_down().  It looks for an ifaddr whose ifa_addr is sa,
32339191c8eSGarrett Wollman  * and calls in_ifadown() to remove all routes corresponding to that address.
32439191c8eSGarrett Wollman  * It also receives the PRC_IFUP messages from if_up() and reinstalls the
32539191c8eSGarrett Wollman  * interface routes.
32639191c8eSGarrett Wollman  */
32739191c8eSGarrett Wollman void
32839191c8eSGarrett Wollman rip_ctlinput(cmd, sa, vip)
32939191c8eSGarrett Wollman 	int cmd;
33039191c8eSGarrett Wollman 	struct sockaddr *sa;
33139191c8eSGarrett Wollman 	void *vip;
33239191c8eSGarrett Wollman {
33339191c8eSGarrett Wollman 	struct in_ifaddr *ia;
33439191c8eSGarrett Wollman 	struct ifnet *ifp;
33539191c8eSGarrett Wollman 	int err;
33639191c8eSGarrett Wollman 	int flags;
33739191c8eSGarrett Wollman 
33839191c8eSGarrett Wollman 	switch(cmd) {
33939191c8eSGarrett Wollman 	case PRC_IFDOWN:
34039191c8eSGarrett Wollman 		for (ia = in_ifaddrhead.tqh_first; ia;
34139191c8eSGarrett Wollman 		     ia = ia->ia_link.tqe_next) {
34239191c8eSGarrett Wollman 			if (ia->ia_ifa.ifa_addr == sa
34339191c8eSGarrett Wollman 			    && (ia->ia_flags & IFA_ROUTE)) {
34439191c8eSGarrett Wollman 				/*
34539191c8eSGarrett Wollman 				 * in_ifscrub kills the interface route.
34639191c8eSGarrett Wollman 				 */
34739191c8eSGarrett Wollman 				in_ifscrub(ia->ia_ifp, ia);
34839191c8eSGarrett Wollman 				/*
34939191c8eSGarrett Wollman 				 * in_ifadown gets rid of all the rest of
35039191c8eSGarrett Wollman 				 * the routes.  This is not quite the right
35139191c8eSGarrett Wollman 				 * thing to do, but at least if we are running
35239191c8eSGarrett Wollman 				 * a routing process they will come back.
35339191c8eSGarrett Wollman 				 */
35439191c8eSGarrett Wollman 				in_ifadown(&ia->ia_ifa);
35539191c8eSGarrett Wollman 				break;
35639191c8eSGarrett Wollman 			}
35739191c8eSGarrett Wollman 		}
35839191c8eSGarrett Wollman 		break;
35939191c8eSGarrett Wollman 
36039191c8eSGarrett Wollman 	case PRC_IFUP:
36139191c8eSGarrett Wollman 		for (ia = in_ifaddrhead.tqh_first; ia;
36239191c8eSGarrett Wollman 		     ia = ia->ia_link.tqe_next) {
36339191c8eSGarrett Wollman 			if (ia->ia_ifa.ifa_addr == sa)
36439191c8eSGarrett Wollman 				break;
36539191c8eSGarrett Wollman 		}
36639191c8eSGarrett Wollman 		if (ia == 0 || (ia->ia_flags & IFA_ROUTE))
36739191c8eSGarrett Wollman 			return;
36839191c8eSGarrett Wollman 		flags = RTF_UP;
36939191c8eSGarrett Wollman 		ifp = ia->ia_ifa.ifa_ifp;
37039191c8eSGarrett Wollman 
37139191c8eSGarrett Wollman 		if ((ifp->if_flags & IFF_LOOPBACK)
37239191c8eSGarrett Wollman 		    || (ifp->if_flags & IFF_POINTOPOINT))
37339191c8eSGarrett Wollman 			flags |= RTF_HOST;
37439191c8eSGarrett Wollman 
37539191c8eSGarrett Wollman 		err = rtinit(&ia->ia_ifa, RTM_ADD, flags);
37639191c8eSGarrett Wollman 		if (err == 0)
37739191c8eSGarrett Wollman 			ia->ia_flags |= IFA_ROUTE;
37839191c8eSGarrett Wollman 		break;
37939191c8eSGarrett Wollman 	}
38039191c8eSGarrett Wollman }
38139191c8eSGarrett Wollman 
382117bcae7SGarrett Wollman static u_long	rip_sendspace = RIPSNDQ;
383117bcae7SGarrett Wollman static u_long	rip_recvspace = RIPRCVQ;
384df8bae1dSRodney W. Grimes 
385117bcae7SGarrett Wollman SYSCTL_INT(_net_inet_raw, OID_AUTO, maxdgram, CTLFLAG_RW, &rip_sendspace,
386117bcae7SGarrett Wollman 	   0, "");
387117bcae7SGarrett Wollman SYSCTL_INT(_net_inet_raw, OID_AUTO, recvspace, CTLFLAG_RW, &rip_recvspace,
388117bcae7SGarrett Wollman 	   0, "");
389117bcae7SGarrett Wollman 
390117bcae7SGarrett Wollman static int
391a29f300eSGarrett Wollman rip_attach(struct socket *so, int proto, struct proc *p)
392df8bae1dSRodney W. Grimes {
393117bcae7SGarrett Wollman 	struct inpcb *inp;
394117bcae7SGarrett Wollman 	int error;
395c1f8a6ceSDavid Greenman 
396117bcae7SGarrett Wollman 	inp = sotoinpcb(so);
397df8bae1dSRodney W. Grimes 	if (inp)
398df8bae1dSRodney W. Grimes 		panic("rip_attach");
399a29f300eSGarrett Wollman 	if (p && (error = suser(p->p_ucred, &p->p_acflag)) != 0)
400a29f300eSGarrett Wollman 		return error;
401117bcae7SGarrett Wollman 
402df8bae1dSRodney W. Grimes 	if ((error = soreserve(so, rip_sendspace, rip_recvspace)) ||
403a29f300eSGarrett Wollman 	    (error = in_pcballoc(so, &ripcbinfo, p)))
404117bcae7SGarrett Wollman 		return error;
405df8bae1dSRodney W. Grimes 	inp = (struct inpcb *)so->so_pcb;
406ca98b82cSDavid Greenman 	inp->inp_ip_p = proto;
407117bcae7SGarrett Wollman 	return 0;
408df8bae1dSRodney W. Grimes }
409117bcae7SGarrett Wollman 
410117bcae7SGarrett Wollman static int
411117bcae7SGarrett Wollman rip_detach(struct socket *so)
412117bcae7SGarrett Wollman {
413117bcae7SGarrett Wollman 	struct inpcb *inp;
414117bcae7SGarrett Wollman 
415117bcae7SGarrett Wollman 	inp = sotoinpcb(so);
416df8bae1dSRodney W. Grimes 	if (inp == 0)
417df8bae1dSRodney W. Grimes 		panic("rip_detach");
418df8bae1dSRodney W. Grimes 	if (so == ip_mrouter)
419df8bae1dSRodney W. Grimes 		ip_mrouter_done();
420b4489dc3SGarrett Wollman 	ip_rsvp_force_done(so);
421838ecf42SGarrett Wollman 	if (so == ip_rsvpd)
422838ecf42SGarrett Wollman 		ip_rsvp_done();
423df8bae1dSRodney W. Grimes 	in_pcbdetach(inp);
424117bcae7SGarrett Wollman 	return 0;
425117bcae7SGarrett Wollman }
426df8bae1dSRodney W. Grimes 
427117bcae7SGarrett Wollman static int
428117bcae7SGarrett Wollman rip_abort(struct socket *so)
429df8bae1dSRodney W. Grimes {
430117bcae7SGarrett Wollman 	soisdisconnected(so);
431117bcae7SGarrett Wollman 	return rip_detach(so);
432117bcae7SGarrett Wollman }
433117bcae7SGarrett Wollman 
434117bcae7SGarrett Wollman static int
435117bcae7SGarrett Wollman rip_disconnect(struct socket *so)
436117bcae7SGarrett Wollman {
437117bcae7SGarrett Wollman 	if ((so->so_state & SS_ISCONNECTED) == 0)
438117bcae7SGarrett Wollman 		return ENOTCONN;
439117bcae7SGarrett Wollman 	return rip_abort(so);
440117bcae7SGarrett Wollman }
441117bcae7SGarrett Wollman 
442117bcae7SGarrett Wollman static int
44357bf258eSGarrett Wollman rip_bind(struct socket *so, struct sockaddr *nam, struct proc *p)
444117bcae7SGarrett Wollman {
445117bcae7SGarrett Wollman 	struct inpcb *inp = sotoinpcb(so);
44657bf258eSGarrett Wollman 	struct sockaddr_in *addr = (struct sockaddr_in *)nam;
447df8bae1dSRodney W. Grimes 
44857bf258eSGarrett Wollman 	if (nam->sa_len != sizeof(*addr))
449117bcae7SGarrett Wollman 		return EINVAL;
450117bcae7SGarrett Wollman 
451117bcae7SGarrett Wollman 	if (TAILQ_EMPTY(&ifnet) || ((addr->sin_family != AF_INET) &&
452df8bae1dSRodney W. Grimes 				    (addr->sin_family != AF_IMPLINK)) ||
453df8bae1dSRodney W. Grimes 	    (addr->sin_addr.s_addr &&
454117bcae7SGarrett Wollman 	     ifa_ifwithaddr((struct sockaddr *)addr) == 0))
455117bcae7SGarrett Wollman 		return EADDRNOTAVAIL;
456df8bae1dSRodney W. Grimes 	inp->inp_laddr = addr->sin_addr;
457117bcae7SGarrett Wollman 	return 0;
458df8bae1dSRodney W. Grimes }
459117bcae7SGarrett Wollman 
460117bcae7SGarrett Wollman static int
46157bf258eSGarrett Wollman rip_connect(struct socket *so, struct sockaddr *nam, struct proc *p)
462df8bae1dSRodney W. Grimes {
463117bcae7SGarrett Wollman 	struct inpcb *inp = sotoinpcb(so);
46457bf258eSGarrett Wollman 	struct sockaddr_in *addr = (struct sockaddr_in *)nam;
465df8bae1dSRodney W. Grimes 
46657bf258eSGarrett Wollman 	if (nam->sa_len != sizeof(*addr))
467117bcae7SGarrett Wollman 		return EINVAL;
468117bcae7SGarrett Wollman 	if (TAILQ_EMPTY(&ifnet))
469117bcae7SGarrett Wollman 		return EADDRNOTAVAIL;
470df8bae1dSRodney W. Grimes 	if ((addr->sin_family != AF_INET) &&
471117bcae7SGarrett Wollman 	    (addr->sin_family != AF_IMPLINK))
472117bcae7SGarrett Wollman 		return EAFNOSUPPORT;
473df8bae1dSRodney W. Grimes 	inp->inp_faddr = addr->sin_addr;
474df8bae1dSRodney W. Grimes 	soisconnected(so);
475117bcae7SGarrett Wollman 	return 0;
476df8bae1dSRodney W. Grimes }
477df8bae1dSRodney W. Grimes 
478117bcae7SGarrett Wollman static int
479117bcae7SGarrett Wollman rip_shutdown(struct socket *so)
480df8bae1dSRodney W. Grimes {
481117bcae7SGarrett Wollman 	socantsendmore(so);
482117bcae7SGarrett Wollman 	return 0;
483117bcae7SGarrett Wollman }
484117bcae7SGarrett Wollman 
485117bcae7SGarrett Wollman static int
48657bf258eSGarrett Wollman rip_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
487a29f300eSGarrett Wollman 	 struct mbuf *control, struct proc *p)
488117bcae7SGarrett Wollman {
489117bcae7SGarrett Wollman 	struct inpcb *inp = sotoinpcb(so);
490df8bae1dSRodney W. Grimes 	register u_long dst;
491df8bae1dSRodney W. Grimes 
492df8bae1dSRodney W. Grimes 	if (so->so_state & SS_ISCONNECTED) {
493df8bae1dSRodney W. Grimes 		if (nam) {
494117bcae7SGarrett Wollman 			m_freem(m);
495117bcae7SGarrett Wollman 			return EISCONN;
496df8bae1dSRodney W. Grimes 		}
497df8bae1dSRodney W. Grimes 		dst = inp->inp_faddr.s_addr;
498df8bae1dSRodney W. Grimes 	} else {
499df8bae1dSRodney W. Grimes 		if (nam == NULL) {
500117bcae7SGarrett Wollman 			m_freem(m);
501117bcae7SGarrett Wollman 			return ENOTCONN;
502df8bae1dSRodney W. Grimes 		}
50357bf258eSGarrett Wollman 		dst = ((struct sockaddr_in *)nam)->sin_addr.s_addr;
504df8bae1dSRodney W. Grimes 	}
505117bcae7SGarrett Wollman 	return rip_output(m, so, dst);
506df8bae1dSRodney W. Grimes }
507df8bae1dSRodney W. Grimes 
508117bcae7SGarrett Wollman struct pr_usrreqs rip_usrreqs = {
509117bcae7SGarrett Wollman 	rip_abort, pru_accept_notsupp, rip_attach, rip_bind, rip_connect,
510117bcae7SGarrett Wollman 	pru_connect2_notsupp, in_control, rip_detach, rip_disconnect,
511117bcae7SGarrett Wollman 	pru_listen_notsupp, in_setpeeraddr, pru_rcvd_notsupp,
512117bcae7SGarrett Wollman 	pru_rcvoob_notsupp, rip_send, pru_sense_null, rip_shutdown,
513f8f6cbbaSPeter Wemm 	in_setsockaddr, sosend, soreceive, sopoll
514117bcae7SGarrett Wollman };
515