1 | /* |
---|
2 | * Copyright (c) 1982, 1986, 1988, 1993 |
---|
3 | * The Regents of the University of California. All rights reserved. |
---|
4 | * |
---|
5 | * Redistribution and use in source and binary forms, with or without |
---|
6 | * modification, are permitted provided that the following conditions |
---|
7 | * are met: |
---|
8 | * 1. Redistributions of source code must retain the above copyright |
---|
9 | * notice, this list of conditions and the following disclaimer. |
---|
10 | * 2. Redistributions in binary form must reproduce the above copyright |
---|
11 | * notice, this list of conditions and the following disclaimer in the |
---|
12 | * documentation and/or other materials provided with the distribution. |
---|
13 | * 4. Neither the name of the University nor the names of its contributors |
---|
14 | * may be used to endorse or promote products derived from this software |
---|
15 | * without specific prior written permission. |
---|
16 | * |
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
---|
18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
---|
21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
---|
22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
---|
23 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
---|
24 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
---|
25 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
---|
26 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
---|
27 | * SUCH DAMAGE. |
---|
28 | * |
---|
29 | * $FreeBSD: src/sys/netinet/ip_divert.c,v 1.113 2005/05/13 11:44:37 glebius Exp $ |
---|
30 | */ |
---|
31 | |
---|
32 | /* |
---|
33 | * $Id$ |
---|
34 | */ |
---|
35 | |
---|
36 | #include <sys/param.h> |
---|
37 | #include <rtems/bsd/sys/queue.h> |
---|
38 | #include <sys/malloc.h> |
---|
39 | #include <sys/mbuf.h> |
---|
40 | #include <sys/socket.h> |
---|
41 | #include <sys/protosw.h> |
---|
42 | #include <sys/socketvar.h> |
---|
43 | #include <sys/errno.h> |
---|
44 | #include <sys/systm.h> |
---|
45 | |
---|
46 | #include <net/if.h> |
---|
47 | #include <net/route.h> |
---|
48 | |
---|
49 | #include <netinet/in.h> |
---|
50 | #include <netinet/in_systm.h> |
---|
51 | #include <netinet/ip.h> |
---|
52 | #include <netinet/in_pcb.h> |
---|
53 | #include <netinet/in_var.h> |
---|
54 | #include <netinet/ip_var.h> |
---|
55 | |
---|
56 | /* |
---|
57 | * Divert sockets |
---|
58 | */ |
---|
59 | |
---|
60 | /* |
---|
61 | * Allocate enough space to hold a full IP packet |
---|
62 | */ |
---|
63 | #define DIVSNDQ (65536 + 100) |
---|
64 | #define DIVRCVQ (65536 + 100) |
---|
65 | |
---|
66 | /* Global variables */ |
---|
67 | |
---|
68 | /* |
---|
69 | * ip_input() and ip_output() set this secret value before calling us to |
---|
70 | * let us know which divert port to divert a packet to; this is done so |
---|
71 | * we can use the existing prototype for struct protosw's pr_input(). |
---|
72 | * This is stored in host order. |
---|
73 | */ |
---|
74 | u_short ip_divert_port; |
---|
75 | |
---|
76 | /* |
---|
77 | * We set this value to a non-zero port number when we want the call to |
---|
78 | * ip_fw_chk() in ip_input() or ip_output() to ignore ``divert <port>'' |
---|
79 | * chain entries. This is stored in host order. |
---|
80 | */ |
---|
81 | u_short ip_divert_ignore; |
---|
82 | |
---|
83 | /* Internal variables. */ |
---|
84 | static struct inpcbhead divcb; |
---|
85 | static struct inpcbinfo divcbinfo; |
---|
86 | |
---|
87 | static u_long div_sendspace = DIVSNDQ; /* XXX sysctl ? */ |
---|
88 | static u_long div_recvspace = DIVRCVQ; /* XXX sysctl ? */ |
---|
89 | |
---|
90 | /* Optimization: have this preinitialized */ |
---|
91 | static struct sockaddr_in divsrc = { sizeof(divsrc), AF_INET }; |
---|
92 | |
---|
93 | /* Internal functions */ |
---|
94 | |
---|
95 | static int div_output(struct socket *so, |
---|
96 | struct mbuf *m, struct mbuf *addr, struct mbuf *control); |
---|
97 | |
---|
98 | /* |
---|
99 | * Initialize divert connection block queue. |
---|
100 | */ |
---|
101 | void |
---|
102 | div_init(void) |
---|
103 | { |
---|
104 | LIST_INIT(&divcb); |
---|
105 | divcbinfo.listhead = &divcb; |
---|
106 | /* |
---|
107 | * XXX We don't use the hash list for divert IP, but it's easier |
---|
108 | * to allocate a one entry hash list than it is to check all |
---|
109 | * over the place for hashbase == NULL. |
---|
110 | */ |
---|
111 | divcbinfo.hashbase = hashinit(1, M_PCB, &divcbinfo.hashmask); |
---|
112 | } |
---|
113 | |
---|
114 | /* |
---|
115 | * Setup generic address and protocol structures |
---|
116 | * for div_input routine, then pass them along with |
---|
117 | * mbuf chain. ip->ip_len is assumed to have had |
---|
118 | * the header length (hlen) subtracted out already. |
---|
119 | * We tell whether the packet was incoming or outgoing |
---|
120 | * by seeing if hlen == 0, which is a hack. |
---|
121 | */ |
---|
122 | void |
---|
123 | div_input(struct mbuf *m, int hlen) |
---|
124 | { |
---|
125 | struct ip *ip; |
---|
126 | struct inpcb *inp; |
---|
127 | struct socket *sa; |
---|
128 | |
---|
129 | /* Sanity check */ |
---|
130 | if (ip_divert_port == 0) |
---|
131 | panic("div_input: port is 0"); |
---|
132 | |
---|
133 | /* Assure header */ |
---|
134 | if (m->m_len < sizeof(struct ip) && |
---|
135 | (m = m_pullup(m, sizeof(struct ip))) == 0) { |
---|
136 | return; |
---|
137 | } |
---|
138 | ip = mtod(m, struct ip *); |
---|
139 | |
---|
140 | /* Record divert port */ |
---|
141 | divsrc.sin_port = htons(ip_divert_port); |
---|
142 | |
---|
143 | /* Restore packet header fields */ |
---|
144 | ip->ip_len += hlen; |
---|
145 | HTONS(ip->ip_len); |
---|
146 | HTONS(ip->ip_off); |
---|
147 | |
---|
148 | /* Record receive interface address, if any */ |
---|
149 | divsrc.sin_addr.s_addr = 0; |
---|
150 | if (hlen) { |
---|
151 | struct ifaddr *ifa; |
---|
152 | |
---|
153 | #ifdef DIAGNOSTIC |
---|
154 | /* Sanity check */ |
---|
155 | if (!(m->m_flags & M_PKTHDR)) |
---|
156 | panic("div_input: no pkt hdr"); |
---|
157 | #endif |
---|
158 | |
---|
159 | /* More fields affected by ip_input() */ |
---|
160 | HTONS(ip->ip_id); |
---|
161 | |
---|
162 | /* Find IP address for recieve interface */ |
---|
163 | for (ifa = m->m_pkthdr.rcvif->if_addrlist; |
---|
164 | ifa != NULL; ifa = ifa->ifa_next) { |
---|
165 | if (ifa->ifa_addr == NULL) |
---|
166 | continue; |
---|
167 | if (ifa->ifa_addr->sa_family != AF_INET) |
---|
168 | continue; |
---|
169 | divsrc.sin_addr = |
---|
170 | ((struct sockaddr_in *) ifa->ifa_addr)->sin_addr; |
---|
171 | break; |
---|
172 | } |
---|
173 | } |
---|
174 | |
---|
175 | /* Put packet on socket queue, if any */ |
---|
176 | sa = NULL; |
---|
177 | for (inp = divcb.lh_first; inp != NULL; inp = inp->inp_list.le_next) { |
---|
178 | if (inp->inp_lport == htons(ip_divert_port)) |
---|
179 | sa = inp->inp_socket; |
---|
180 | } |
---|
181 | if (sa) { |
---|
182 | if (sbappendaddr(&sa->so_rcv, (struct sockaddr *)&divsrc, |
---|
183 | m, (struct mbuf *)0) == 0) |
---|
184 | m_freem(m); |
---|
185 | else |
---|
186 | sorwakeup(sa); |
---|
187 | } else { |
---|
188 | m_freem(m); |
---|
189 | ipstat.ips_noproto++; |
---|
190 | ipstat.ips_delivered--; |
---|
191 | } |
---|
192 | } |
---|
193 | |
---|
194 | /* |
---|
195 | * Deliver packet back into the IP processing machinery. |
---|
196 | * |
---|
197 | * If no address specified, or address is 0.0.0.0, send to ip_output(); |
---|
198 | * otherwise, send to ip_input() and mark as having been received on |
---|
199 | * the interface with that address. |
---|
200 | * |
---|
201 | * If no address specified, or dest port is 0, allow packet to divert |
---|
202 | * back to this socket; otherwise, don't. |
---|
203 | */ |
---|
204 | static int |
---|
205 | div_output(struct socket *so, struct mbuf *m, |
---|
206 | struct mbuf *addr, struct mbuf *control) |
---|
207 | { |
---|
208 | register struct inpcb *const inp = sotoinpcb(so); |
---|
209 | register struct ip *const ip = mtod(m, struct ip *); |
---|
210 | struct sockaddr_in *sin = NULL; |
---|
211 | int error = 0; |
---|
212 | |
---|
213 | if (control) |
---|
214 | m_freem(control); /* XXX */ |
---|
215 | if (addr) |
---|
216 | sin = mtod(addr, struct sockaddr_in *); |
---|
217 | |
---|
218 | /* Loopback avoidance option */ |
---|
219 | ip_divert_ignore = ntohs(inp->inp_lport); |
---|
220 | |
---|
221 | /* Reinject packet into the system as incoming or outgoing */ |
---|
222 | if (!sin || sin->sin_addr.s_addr == 0) { |
---|
223 | /* Don't allow both user specified and setsockopt options, |
---|
224 | and don't allow packet length sizes that will crash */ |
---|
225 | if (((ip->ip_hl != (sizeof (*ip) >> 2)) && inp->inp_options) || |
---|
226 | ((u_short)ntohs(ip->ip_len) > m->m_pkthdr.len)) { |
---|
227 | error = EINVAL; |
---|
228 | goto cantsend; |
---|
229 | } |
---|
230 | |
---|
231 | /* Convert fields to host order for ip_output() */ |
---|
232 | NTOHS(ip->ip_len); |
---|
233 | NTOHS(ip->ip_off); |
---|
234 | |
---|
235 | /* Send packet to output processing */ |
---|
236 | ipstat.ips_rawout++; /* XXX */ |
---|
237 | error = ip_output(m, inp->inp_options, &inp->inp_route, |
---|
238 | (so->so_options & SO_DONTROUTE) | |
---|
239 | IP_ALLOWBROADCAST | IP_RAWOUTPUT, inp->inp_moptions); |
---|
240 | } else { |
---|
241 | struct ifaddr *ifa; |
---|
242 | |
---|
243 | /* Find receive interface with the given IP address */ |
---|
244 | sin->sin_port = 0; |
---|
245 | if ((ifa = ifa_ifwithaddr((struct sockaddr *) sin)) == 0) { |
---|
246 | error = EADDRNOTAVAIL; |
---|
247 | goto cantsend; |
---|
248 | } |
---|
249 | m->m_pkthdr.rcvif = ifa->ifa_ifp; |
---|
250 | |
---|
251 | /* Send packet to input processing */ |
---|
252 | ip_input(m); |
---|
253 | } |
---|
254 | |
---|
255 | /* Reset for next time (and other packets) */ |
---|
256 | ip_divert_ignore = 0; |
---|
257 | return error; |
---|
258 | |
---|
259 | cantsend: |
---|
260 | ip_divert_ignore = 0; |
---|
261 | m_freem(m); |
---|
262 | return error; |
---|
263 | } |
---|
264 | |
---|
265 | /*ARGSUSED*/ |
---|
266 | int |
---|
267 | div_usrreq( |
---|
268 | struct socket *so, |
---|
269 | int req, |
---|
270 | struct mbuf *m, |
---|
271 | struct mbuf *nam, |
---|
272 | struct mbuf *control ) |
---|
273 | { |
---|
274 | register int error = 0; |
---|
275 | register struct inpcb *inp = sotoinpcb(so); |
---|
276 | int s; |
---|
277 | |
---|
278 | if (inp == NULL && req != PRU_ATTACH) { |
---|
279 | error = EINVAL; |
---|
280 | goto release; |
---|
281 | } |
---|
282 | switch (req) { |
---|
283 | |
---|
284 | case PRU_ATTACH: |
---|
285 | if (inp) |
---|
286 | panic("div_attach"); |
---|
287 | if ((so->so_state & SS_PRIV) == 0) { |
---|
288 | error = EACCES; |
---|
289 | break; |
---|
290 | } |
---|
291 | s = splnet(); |
---|
292 | error = in_pcballoc(so, &divcbinfo); |
---|
293 | splx(s); |
---|
294 | if (error) |
---|
295 | break; |
---|
296 | error = soreserve(so, div_sendspace, div_recvspace); |
---|
297 | if (error) |
---|
298 | break; |
---|
299 | inp = (struct inpcb *)so->so_pcb; |
---|
300 | inp->inp_ip_p = (intptr_t)nam; /* XXX */ |
---|
301 | inp->inp_flags |= INP_HDRINCL; |
---|
302 | /* The socket is always "connected" because |
---|
303 | we always know "where" to send the packet */ |
---|
304 | so->so_state |= SS_ISCONNECTED; |
---|
305 | break; |
---|
306 | |
---|
307 | case PRU_DISCONNECT: |
---|
308 | if ((so->so_state & SS_ISCONNECTED) == 0) { |
---|
309 | error = ENOTCONN; |
---|
310 | break; |
---|
311 | } |
---|
312 | /* FALLTHROUGH */ |
---|
313 | case PRU_ABORT: |
---|
314 | soisdisconnected(so); |
---|
315 | /* FALLTHROUGH */ |
---|
316 | case PRU_DETACH: |
---|
317 | if (inp == 0) |
---|
318 | panic("div_detach"); |
---|
319 | in_pcbdetach(inp); |
---|
320 | break; |
---|
321 | |
---|
322 | case PRU_BIND: |
---|
323 | s = splnet(); |
---|
324 | error = in_pcbbind(inp, nam); |
---|
325 | splx(s); |
---|
326 | break; |
---|
327 | |
---|
328 | /* |
---|
329 | * Mark the connection as being incapable of further input. |
---|
330 | */ |
---|
331 | case PRU_SHUTDOWN: |
---|
332 | socantsendmore(so); |
---|
333 | break; |
---|
334 | |
---|
335 | case PRU_SEND: |
---|
336 | /* Packet must have a header (but that's about it) */ |
---|
337 | if (m->m_len < sizeof (struct ip) || |
---|
338 | (m = m_pullup(m, sizeof (struct ip))) == 0) { |
---|
339 | ipstat.ips_toosmall++; |
---|
340 | error = EINVAL; |
---|
341 | break; |
---|
342 | } |
---|
343 | |
---|
344 | /* Send packet */ |
---|
345 | error = div_output(so, m, nam, control); |
---|
346 | m = NULL; |
---|
347 | break; |
---|
348 | |
---|
349 | case PRU_SOCKADDR: |
---|
350 | in_setsockaddr(inp, nam); |
---|
351 | break; |
---|
352 | |
---|
353 | case PRU_SENSE: |
---|
354 | /* |
---|
355 | * stat: don't bother with a blocksize. |
---|
356 | */ |
---|
357 | return (0); |
---|
358 | |
---|
359 | /* |
---|
360 | * Not supported. |
---|
361 | */ |
---|
362 | case PRU_CONNECT: |
---|
363 | case PRU_CONNECT2: |
---|
364 | case PRU_CONTROL: |
---|
365 | case PRU_RCVOOB: |
---|
366 | case PRU_RCVD: |
---|
367 | case PRU_LISTEN: |
---|
368 | case PRU_ACCEPT: |
---|
369 | case PRU_SENDOOB: |
---|
370 | case PRU_PEERADDR: |
---|
371 | error = EOPNOTSUPP; |
---|
372 | break; |
---|
373 | |
---|
374 | default: |
---|
375 | panic("div_usrreq"); |
---|
376 | } |
---|
377 | release: |
---|
378 | if (m) |
---|
379 | m_freem(m); |
---|
380 | return (error); |
---|
381 | } |
---|