]>
Commit | Line | Data |
---|---|---|
1c79356b | 1 | /* |
c910b4d9 | 2 | * Copyright (c) 2000-2008 Apple Inc. All rights reserved. |
5d5c5d0d | 3 | * |
2d21ac55 | 4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ |
1c79356b | 5 | * |
2d21ac55 A |
6 | * This file contains Original Code and/or Modifications of Original Code |
7 | * as defined in and that are subject to the Apple Public Source License | |
8 | * Version 2.0 (the 'License'). You may not use this file except in | |
9 | * compliance with the License. The rights granted to you under the License | |
10 | * may not be used to create, or enable the creation or redistribution of, | |
11 | * unlawful or unlicensed copies of an Apple operating system, or to | |
12 | * circumvent, violate, or enable the circumvention or violation of, any | |
13 | * terms of an Apple operating system software license agreement. | |
8f6c56a5 | 14 | * |
2d21ac55 A |
15 | * Please obtain a copy of the License at |
16 | * http://www.opensource.apple.com/apsl/ and read it before using this file. | |
17 | * | |
18 | * The Original Code and all software distributed under the License are | |
19 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER | |
8f6c56a5 A |
20 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
21 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
2d21ac55 A |
22 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. |
23 | * Please see the License for the specific language governing rights and | |
24 | * limitations under the License. | |
8f6c56a5 | 25 | * |
2d21ac55 | 26 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ |
1c79356b A |
27 | */ |
28 | /* | |
29 | * Copyright (c) 1982, 1986, 1988, 1993 | |
30 | * The Regents of the University of California. All rights reserved. | |
31 | * | |
32 | * Redistribution and use in source and binary forms, with or without | |
33 | * modification, are permitted provided that the following conditions | |
34 | * are met: | |
35 | * 1. Redistributions of source code must retain the above copyright | |
36 | * notice, this list of conditions and the following disclaimer. | |
37 | * 2. Redistributions in binary form must reproduce the above copyright | |
38 | * notice, this list of conditions and the following disclaimer in the | |
39 | * documentation and/or other materials provided with the distribution. | |
40 | * 3. All advertising materials mentioning features or use of this software | |
41 | * must display the following acknowledgement: | |
42 | * This product includes software developed by the University of | |
43 | * California, Berkeley and its contributors. | |
44 | * 4. Neither the name of the University nor the names of its contributors | |
45 | * may be used to endorse or promote products derived from this software | |
46 | * without specific prior written permission. | |
47 | * | |
48 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
49 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
50 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
51 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
52 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
53 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
54 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
55 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
56 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
57 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
58 | * SUCH DAMAGE. | |
59 | * | |
60 | * @(#)raw_ip.c 8.7 (Berkeley) 5/15/95 | |
61 | */ | |
2d21ac55 A |
62 | /* |
63 | * NOTICE: This file was modified by SPARTA, Inc. in 2005 to introduce | |
64 | * support for mandatory and extensible security protections. This notice | |
65 | * is included in support of clause 2.2 (b) of the Apple Public License, | |
66 | * Version 2.0. | |
67 | */ | |
1c79356b A |
68 | |
69 | #include <sys/param.h> | |
70 | #include <sys/systm.h> | |
71 | #include <sys/kernel.h> | |
72 | #include <sys/malloc.h> | |
73 | #include <sys/mbuf.h> | |
74 | #include <sys/proc.h> | |
91447636 | 75 | #include <sys/domain.h> |
1c79356b A |
76 | #include <sys/protosw.h> |
77 | #include <sys/socket.h> | |
78 | #include <sys/socketvar.h> | |
79 | #include <sys/sysctl.h> | |
2d21ac55 A |
80 | #include <libkern/OSAtomic.h> |
81 | #include <kern/zalloc.h> | |
1c79356b | 82 | |
2d21ac55 | 83 | #include <pexpert/pexpert.h> |
1c79356b A |
84 | |
85 | #include <net/if.h> | |
86 | #include <net/route.h> | |
87 | ||
88 | #define _IP_VHL | |
89 | #include <netinet/in.h> | |
90 | #include <netinet/in_systm.h> | |
91 | #include <netinet/ip.h> | |
1c79356b A |
92 | #include <netinet/in_pcb.h> |
93 | #include <netinet/in_var.h> | |
94 | #include <netinet/ip_var.h> | |
95 | #include <netinet/ip_mroute.h> | |
96 | ||
97 | #include <netinet/ip_fw.h> | |
98 | ||
99 | #if IPSEC | |
100 | #include <netinet6/ipsec.h> | |
101 | #endif /*IPSEC*/ | |
102 | ||
1c79356b A |
103 | #if DUMMYNET |
104 | #include <netinet/ip_dummynet.h> | |
105 | #endif | |
9bccf70c | 106 | |
2d21ac55 A |
107 | #if CONFIG_MACF_NET |
108 | #include <security/mac_framework.h> | |
109 | #endif /* MAC_NET */ | |
110 | ||
111 | int load_ipfw(void); | |
112 | int rip_detach(struct socket *); | |
113 | int rip_abort(struct socket *); | |
114 | int rip_disconnect(struct socket *); | |
115 | int rip_bind(struct socket *, struct sockaddr *, struct proc *); | |
116 | int rip_connect(struct socket *, struct sockaddr *, struct proc *); | |
117 | int rip_shutdown(struct socket *); | |
118 | ||
9bccf70c A |
119 | #if IPSEC |
120 | extern int ipsec_bypass; | |
1c79356b A |
121 | #endif |
122 | ||
91447636 | 123 | extern u_long route_generation; |
9bccf70c A |
124 | struct inpcbhead ripcb; |
125 | struct inpcbinfo ripcbinfo; | |
1c79356b | 126 | |
91447636 | 127 | /* control hooks for ipfw and dummynet */ |
4a3eedf9 | 128 | #if IPFIREWALL |
91447636 A |
129 | ip_fw_ctl_t *ip_fw_ctl_ptr; |
130 | #if DUMMYNET | |
131 | ip_dn_ctl_t *ip_dn_ctl_ptr; | |
132 | #endif /* DUMMYNET */ | |
4a3eedf9 | 133 | #endif /* IPFIREWALL */ |
91447636 | 134 | |
1c79356b A |
135 | /* |
136 | * Nominal space allocated to a raw ip socket. | |
137 | */ | |
138 | #define RIPSNDQ 8192 | |
139 | #define RIPRCVQ 8192 | |
140 | ||
141 | /* | |
142 | * Raw interface to IP protocol. | |
143 | */ | |
144 | ||
145 | /* | |
146 | * Initialize raw connection block q. | |
147 | */ | |
148 | void | |
149 | rip_init() | |
150 | { | |
91447636 A |
151 | struct inpcbinfo *pcbinfo; |
152 | ||
1c79356b A |
153 | LIST_INIT(&ripcb); |
154 | ripcbinfo.listhead = &ripcb; | |
155 | /* | |
156 | * XXX We don't use the hash list for raw IP, but it's easier | |
157 | * to allocate a one entry hash list than it is to check all | |
158 | * over the place for hashbase == NULL. | |
159 | */ | |
160 | ripcbinfo.hashbase = hashinit(1, M_PCB, &ripcbinfo.hashmask); | |
161 | ripcbinfo.porthashbase = hashinit(1, M_PCB, &ripcbinfo.porthashmask); | |
162 | ||
163 | ripcbinfo.ipi_zone = (void *) zinit(sizeof(struct inpcb), | |
164 | (4096 * sizeof(struct inpcb)), | |
165 | 4096, "ripzone"); | |
166 | ||
91447636 A |
167 | pcbinfo = &ripcbinfo; |
168 | /* | |
169 | * allocate lock group attribute and group for udp pcb mutexes | |
170 | */ | |
171 | pcbinfo->mtx_grp_attr = lck_grp_attr_alloc_init(); | |
91447636 A |
172 | |
173 | pcbinfo->mtx_grp = lck_grp_alloc_init("ripcb", pcbinfo->mtx_grp_attr); | |
174 | ||
175 | /* | |
176 | * allocate the lock attribute for udp pcb mutexes | |
177 | */ | |
178 | pcbinfo->mtx_attr = lck_attr_alloc_init(); | |
91447636 A |
179 | |
180 | if ((pcbinfo->mtx = lck_rw_alloc_init(pcbinfo->mtx_grp, pcbinfo->mtx_attr)) == NULL) | |
181 | return; /* pretty much dead if this fails... */ | |
182 | ||
1c79356b A |
183 | } |
184 | ||
2d21ac55 | 185 | static struct sockaddr_in ripsrc = { sizeof(ripsrc), AF_INET , 0, {0}, {0,0,0,0,0,0,0,0,} }; |
1c79356b A |
186 | /* |
187 | * Setup generic address and protocol structures | |
188 | * for raw_input routine, then pass them along with | |
189 | * mbuf chain. | |
190 | */ | |
191 | void | |
192 | rip_input(m, iphlen) | |
193 | struct mbuf *m; | |
194 | int iphlen; | |
195 | { | |
196 | register struct ip *ip = mtod(m, struct ip *); | |
197 | register struct inpcb *inp; | |
198 | struct inpcb *last = 0; | |
199 | struct mbuf *opts = 0; | |
91447636 | 200 | int skipit; |
1c79356b A |
201 | |
202 | ripsrc.sin_addr = ip->ip_src; | |
91447636 | 203 | lck_rw_lock_shared(ripcbinfo.mtx); |
1c79356b | 204 | LIST_FOREACH(inp, &ripcb, inp_list) { |
9bccf70c A |
205 | #if INET6 |
206 | if ((inp->inp_vflag & INP_IPV4) == 0) | |
1c79356b | 207 | continue; |
9bccf70c A |
208 | #endif |
209 | if (inp->inp_ip_p && (inp->inp_ip_p != ip->ip_p)) | |
1c79356b A |
210 | continue; |
211 | if (inp->inp_laddr.s_addr && | |
212 | inp->inp_laddr.s_addr != ip->ip_dst.s_addr) | |
213 | continue; | |
214 | if (inp->inp_faddr.s_addr && | |
215 | inp->inp_faddr.s_addr != ip->ip_src.s_addr) | |
216 | continue; | |
217 | if (last) { | |
218 | struct mbuf *n = m_copy(m, 0, (int)M_COPYALL); | |
91447636 | 219 | |
9bccf70c A |
220 | #if IPSEC |
221 | /* check AH/ESP integrity. */ | |
91447636 A |
222 | skipit = 0; |
223 | if (ipsec_bypass == 0 && n) { | |
91447636 A |
224 | if (ipsec4_in_reject_so(n, last->inp_socket)) { |
225 | m_freem(n); | |
2d21ac55 | 226 | IPSEC_STAT_INCREMENT(ipsecstat.in_polvio); |
91447636 A |
227 | /* do not inject data to pcb */ |
228 | skipit = 1; | |
229 | } | |
91447636 | 230 | } |
9bccf70c | 231 | #endif /*IPSEC*/ |
2d21ac55 A |
232 | #if CONFIG_MACF_NET |
233 | if (n && skipit == 0) { | |
234 | if (mac_inpcb_check_deliver(last, n, AF_INET, | |
235 | SOCK_RAW) != 0) | |
236 | skipit = 1; | |
237 | } | |
238 | #endif | |
91447636 A |
239 | if (n && skipit == 0) { |
240 | int error = 0; | |
1c79356b A |
241 | if (last->inp_flags & INP_CONTROLOPTS || |
242 | last->inp_socket->so_options & SO_TIMESTAMP) | |
243 | ip_savecontrol(last, &opts, ip, n); | |
9bccf70c A |
244 | if (last->inp_flags & INP_STRIPHDR) { |
245 | n->m_len -= iphlen; | |
246 | n->m_pkthdr.len -= iphlen; | |
247 | n->m_data += iphlen; | |
248 | } | |
91447636 | 249 | // ###LOCK need to lock that socket? |
1c79356b A |
250 | if (sbappendaddr(&last->inp_socket->so_rcv, |
251 | (struct sockaddr *)&ripsrc, n, | |
91447636 | 252 | opts, &error) != 0) { |
9bccf70c | 253 | sorwakeup(last->inp_socket); |
91447636 A |
254 | } |
255 | else { | |
256 | if (error) { | |
257 | /* should notify about lost packet */ | |
258 | kprintf("rip_input can't append to socket\n"); | |
259 | } | |
260 | } | |
1c79356b A |
261 | opts = 0; |
262 | } | |
263 | } | |
264 | last = inp; | |
265 | } | |
91447636 | 266 | lck_rw_done(ripcbinfo.mtx); |
9bccf70c A |
267 | #if IPSEC |
268 | /* check AH/ESP integrity. */ | |
91447636 A |
269 | skipit = 0; |
270 | if (ipsec_bypass == 0 && last) { | |
91447636 A |
271 | if (ipsec4_in_reject_so(m, last->inp_socket)) { |
272 | m_freem(m); | |
2d21ac55 A |
273 | IPSEC_STAT_INCREMENT(ipsecstat.in_polvio); |
274 | OSAddAtomic(1, (SInt32*)&ipstat.ips_delivered); | |
91447636 A |
275 | /* do not inject data to pcb */ |
276 | skipit = 1; | |
277 | } | |
91447636 | 278 | } |
9bccf70c | 279 | #endif /*IPSEC*/ |
2d21ac55 A |
280 | #if CONFIG_MACF_NET |
281 | if (last && skipit == 0) { | |
282 | if (mac_inpcb_check_deliver(last, m, AF_INET, SOCK_RAW) != 0) | |
283 | skipit = 1; | |
284 | } | |
285 | #endif | |
91447636 A |
286 | if (skipit == 0) { |
287 | if (last) { | |
288 | if (last->inp_flags & INP_CONTROLOPTS || | |
289 | last->inp_socket->so_options & SO_TIMESTAMP) | |
290 | ip_savecontrol(last, &opts, ip, m); | |
291 | if (last->inp_flags & INP_STRIPHDR) { | |
292 | m->m_len -= iphlen; | |
293 | m->m_pkthdr.len -= iphlen; | |
294 | m->m_data += iphlen; | |
295 | } | |
296 | if (sbappendaddr(&last->inp_socket->so_rcv, | |
297 | (struct sockaddr *)&ripsrc, m, opts, NULL) != 0) { | |
298 | sorwakeup(last->inp_socket); | |
299 | } else { | |
300 | kprintf("rip_input(2) can't append to socket\n"); | |
301 | } | |
302 | } else { | |
1c79356b | 303 | m_freem(m); |
2d21ac55 A |
304 | OSAddAtomic(1, (SInt32*)&ipstat.ips_noproto); |
305 | OSAddAtomic(-1, (SInt32*)&ipstat.ips_delivered); | |
91447636 | 306 | } |
9bccf70c | 307 | } |
1c79356b A |
308 | } |
309 | ||
310 | /* | |
311 | * Generate IP header and pass packet to ip_output. | |
312 | * Tack on options user may have setup with control call. | |
313 | */ | |
314 | int | |
315 | rip_output(m, so, dst) | |
316 | register struct mbuf *m; | |
317 | struct socket *so; | |
318 | u_long dst; | |
319 | { | |
320 | register struct ip *ip; | |
321 | register struct inpcb *inp = sotoinpcb(so); | |
322 | int flags = (so->so_options & SO_DONTROUTE) | IP_ALLOWBROADCAST; | |
c910b4d9 A |
323 | struct ip_out_args ipoa; |
324 | ||
325 | /* If socket was bound to an ifindex, tell ip_output about it */ | |
326 | ipoa.ipoa_ifscope = (inp->inp_flags & INP_BOUND_IF) ? | |
327 | inp->inp_boundif : IFSCOPE_NONE; | |
328 | flags |= IP_OUTARGS; | |
1c79356b A |
329 | |
330 | /* | |
331 | * If the user handed us a complete IP packet, use it. | |
332 | * Otherwise, allocate an mbuf for a header and fill it in. | |
333 | */ | |
334 | if ((inp->inp_flags & INP_HDRINCL) == 0) { | |
335 | if (m->m_pkthdr.len + sizeof(struct ip) > IP_MAXPACKET) { | |
336 | m_freem(m); | |
337 | return(EMSGSIZE); | |
338 | } | |
339 | M_PREPEND(m, sizeof(struct ip), M_WAIT); | |
340 | ip = mtod(m, struct ip *); | |
9bccf70c | 341 | ip->ip_tos = inp->inp_ip_tos; |
1c79356b A |
342 | ip->ip_off = 0; |
343 | ip->ip_p = inp->inp_ip_p; | |
344 | ip->ip_len = m->m_pkthdr.len; | |
345 | ip->ip_src = inp->inp_laddr; | |
346 | ip->ip_dst.s_addr = dst; | |
9bccf70c | 347 | ip->ip_ttl = inp->inp_ip_ttl; |
1c79356b A |
348 | } else { |
349 | if (m->m_pkthdr.len > IP_MAXPACKET) { | |
350 | m_freem(m); | |
351 | return(EMSGSIZE); | |
352 | } | |
353 | ip = mtod(m, struct ip *); | |
354 | /* don't allow both user specified and setsockopt options, | |
355 | and don't allow packet length sizes that will crash */ | |
9bccf70c | 356 | if (((IP_VHL_HL(ip->ip_vhl) != (sizeof (*ip) >> 2)) |
1c79356b A |
357 | && inp->inp_options) |
358 | || (ip->ip_len > m->m_pkthdr.len) | |
359 | || (ip->ip_len < (IP_VHL_HL(ip->ip_vhl) << 2))) { | |
360 | m_freem(m); | |
361 | return EINVAL; | |
362 | } | |
363 | if (ip->ip_id == 0) | |
9bccf70c A |
364 | #if RANDOM_IP_ID |
365 | ip->ip_id = ip_randomid(); | |
366 | #else | |
1c79356b | 367 | ip->ip_id = htons(ip_id++); |
9bccf70c | 368 | #endif |
1c79356b A |
369 | /* XXX prevent ip_output from overwriting header fields */ |
370 | flags |= IP_RAWOUTPUT; | |
2d21ac55 | 371 | OSAddAtomic(1, (SInt32*)&ipstat.ips_rawout); |
1c79356b A |
372 | } |
373 | ||
374 | #if IPSEC | |
9bccf70c A |
375 | if (ipsec_bypass == 0 && ipsec_setsocket(m, so) != 0) { |
376 | m_freem(m); | |
377 | return ENOBUFS; | |
378 | } | |
1c79356b A |
379 | #endif /*IPSEC*/ |
380 | ||
91447636 A |
381 | if (inp->inp_route.ro_rt && inp->inp_route.ro_rt->generation_id != route_generation) { |
382 | rtfree(inp->inp_route.ro_rt); | |
383 | inp->inp_route.ro_rt = (struct rtentry *)0; | |
384 | } | |
385 | ||
2d21ac55 A |
386 | #if CONFIG_MACF_NET |
387 | mac_mbuf_label_associate_inpcb(inp, m); | |
388 | #endif | |
389 | ||
593a1d5f A |
390 | #if CONFIG_IP_EDGEHOLE |
391 | ip_edgehole_mbuf_tag(inp, m); | |
392 | #endif | |
c910b4d9 A |
393 | return (ip_output(m, inp->inp_options, &inp->inp_route, flags, |
394 | inp->inp_moptions, &ipoa)); | |
1c79356b A |
395 | } |
396 | ||
2d21ac55 A |
397 | #if IPFIREWALL |
398 | int | |
399 | load_ipfw(void) | |
55e303ae A |
400 | { |
401 | kern_return_t err; | |
402 | ||
91447636 | 403 | ipfw_init(); |
55e303ae | 404 | |
91447636 A |
405 | #if DUMMYNET |
406 | if (!DUMMYNET_LOADED) | |
407 | ip_dn_init(); | |
408 | #endif /* DUMMYNET */ | |
409 | err = 0; | |
55e303ae A |
410 | |
411 | return err == 0 && ip_fw_ctl_ptr == NULL ? -1 : err; | |
412 | } | |
2d21ac55 | 413 | #endif /* IPFIREWALL */ |
55e303ae | 414 | |
1c79356b A |
415 | /* |
416 | * Raw IP socket option processing. | |
417 | */ | |
418 | int | |
419 | rip_ctloutput(so, sopt) | |
420 | struct socket *so; | |
421 | struct sockopt *sopt; | |
422 | { | |
423 | struct inpcb *inp = sotoinpcb(so); | |
424 | int error, optval; | |
425 | ||
426 | if (sopt->sopt_level != IPPROTO_IP) | |
427 | return (EINVAL); | |
428 | ||
429 | error = 0; | |
430 | ||
431 | switch (sopt->sopt_dir) { | |
432 | case SOPT_GET: | |
433 | switch (sopt->sopt_name) { | |
434 | case IP_HDRINCL: | |
435 | optval = inp->inp_flags & INP_HDRINCL; | |
436 | error = sooptcopyout(sopt, &optval, sizeof optval); | |
437 | break; | |
438 | ||
439 | case IP_STRIPHDR: | |
440 | optval = inp->inp_flags & INP_STRIPHDR; | |
441 | error = sooptcopyout(sopt, &optval, sizeof optval); | |
442 | break; | |
443 | ||
2d21ac55 | 444 | #if IPFIREWALL |
9bccf70c | 445 | case IP_FW_ADD: |
1c79356b | 446 | case IP_FW_GET: |
9bccf70c A |
447 | case IP_OLD_FW_ADD: |
448 | case IP_OLD_FW_GET: | |
1c79356b | 449 | if (ip_fw_ctl_ptr == 0) |
55e303ae A |
450 | error = load_ipfw(); |
451 | if (ip_fw_ctl_ptr && error == 0) | |
1c79356b | 452 | error = ip_fw_ctl_ptr(sopt); |
55e303ae A |
453 | else |
454 | error = ENOPROTOOPT; | |
1c79356b | 455 | break; |
2d21ac55 | 456 | #endif IPFIREWALL |
1c79356b | 457 | |
1c79356b A |
458 | #if DUMMYNET |
459 | case IP_DUMMYNET_GET: | |
91447636 | 460 | if (DUMMYNET_LOADED) |
1c79356b | 461 | error = ip_dn_ctl_ptr(sopt); |
91447636 A |
462 | else |
463 | error = ENOPROTOOPT; | |
1c79356b A |
464 | break ; |
465 | #endif /* DUMMYNET */ | |
1c79356b | 466 | |
2d21ac55 | 467 | #if MROUTING |
1c79356b A |
468 | case MRT_INIT: |
469 | case MRT_DONE: | |
470 | case MRT_ADD_VIF: | |
471 | case MRT_DEL_VIF: | |
472 | case MRT_ADD_MFC: | |
473 | case MRT_DEL_MFC: | |
474 | case MRT_VERSION: | |
475 | case MRT_ASSERT: | |
476 | error = ip_mrouter_get(so, sopt); | |
477 | break; | |
2d21ac55 | 478 | #endif /* MROUTING */ |
1c79356b A |
479 | |
480 | default: | |
481 | error = ip_ctloutput(so, sopt); | |
482 | break; | |
483 | } | |
484 | break; | |
485 | ||
486 | case SOPT_SET: | |
487 | switch (sopt->sopt_name) { | |
488 | case IP_HDRINCL: | |
489 | error = sooptcopyin(sopt, &optval, sizeof optval, | |
490 | sizeof optval); | |
491 | if (error) | |
492 | break; | |
493 | if (optval) | |
494 | inp->inp_flags |= INP_HDRINCL; | |
495 | else | |
496 | inp->inp_flags &= ~INP_HDRINCL; | |
497 | break; | |
498 | ||
499 | case IP_STRIPHDR: | |
500 | error = sooptcopyin(sopt, &optval, sizeof optval, | |
501 | sizeof optval); | |
502 | if (error) | |
503 | break; | |
504 | if (optval) | |
505 | inp->inp_flags |= INP_STRIPHDR; | |
506 | else | |
507 | inp->inp_flags &= ~INP_STRIPHDR; | |
508 | break; | |
509 | ||
510 | ||
2d21ac55 | 511 | #if IPFIREWALL |
1c79356b A |
512 | case IP_FW_ADD: |
513 | case IP_FW_DEL: | |
514 | case IP_FW_FLUSH: | |
515 | case IP_FW_ZERO: | |
9bccf70c A |
516 | case IP_FW_RESETLOG: |
517 | case IP_OLD_FW_ADD: | |
518 | case IP_OLD_FW_DEL: | |
519 | case IP_OLD_FW_FLUSH: | |
520 | case IP_OLD_FW_ZERO: | |
521 | case IP_OLD_FW_RESETLOG: | |
1c79356b | 522 | if (ip_fw_ctl_ptr == 0) |
55e303ae A |
523 | error = load_ipfw(); |
524 | if (ip_fw_ctl_ptr && error == 0) | |
1c79356b | 525 | error = ip_fw_ctl_ptr(sopt); |
55e303ae A |
526 | else |
527 | error = ENOPROTOOPT; | |
1c79356b | 528 | break; |
2d21ac55 | 529 | #endif /* IPFIREWALL */ |
1c79356b | 530 | |
1c79356b A |
531 | #if DUMMYNET |
532 | case IP_DUMMYNET_CONFIGURE: | |
533 | case IP_DUMMYNET_DEL: | |
534 | case IP_DUMMYNET_FLUSH: | |
91447636 | 535 | if (DUMMYNET_LOADED) |
1c79356b | 536 | error = ip_dn_ctl_ptr(sopt); |
91447636 A |
537 | else |
538 | error = ENOPROTOOPT ; | |
1c79356b A |
539 | break ; |
540 | #endif | |
1c79356b | 541 | |
2d21ac55 | 542 | #if MROUTING |
1c79356b A |
543 | case IP_RSVP_ON: |
544 | error = ip_rsvp_init(so); | |
545 | break; | |
546 | ||
547 | case IP_RSVP_OFF: | |
548 | error = ip_rsvp_done(); | |
549 | break; | |
550 | ||
551 | /* XXX - should be combined */ | |
552 | case IP_RSVP_VIF_ON: | |
553 | error = ip_rsvp_vif_init(so, sopt); | |
554 | break; | |
555 | ||
556 | case IP_RSVP_VIF_OFF: | |
557 | error = ip_rsvp_vif_done(so, sopt); | |
558 | break; | |
2d21ac55 | 559 | |
1c79356b A |
560 | case MRT_INIT: |
561 | case MRT_DONE: | |
562 | case MRT_ADD_VIF: | |
563 | case MRT_DEL_VIF: | |
564 | case MRT_ADD_MFC: | |
565 | case MRT_DEL_MFC: | |
566 | case MRT_VERSION: | |
567 | case MRT_ASSERT: | |
568 | error = ip_mrouter_set(so, sopt); | |
569 | break; | |
2d21ac55 | 570 | #endif /* MROUTING */ |
1c79356b A |
571 | |
572 | default: | |
573 | error = ip_ctloutput(so, sopt); | |
574 | break; | |
575 | } | |
576 | break; | |
577 | } | |
578 | ||
579 | return (error); | |
580 | } | |
581 | ||
582 | /* | |
583 | * This function exists solely to receive the PRC_IFDOWN messages which | |
584 | * are sent by if_down(). It looks for an ifaddr whose ifa_addr is sa, | |
585 | * and calls in_ifadown() to remove all routes corresponding to that address. | |
586 | * It also receives the PRC_IFUP messages from if_up() and reinstalls the | |
587 | * interface routes. | |
588 | */ | |
589 | void | |
2d21ac55 A |
590 | rip_ctlinput( |
591 | int cmd, | |
592 | struct sockaddr *sa, | |
593 | __unused void *vip) | |
1c79356b A |
594 | { |
595 | struct in_ifaddr *ia; | |
596 | struct ifnet *ifp; | |
597 | int err; | |
598 | int flags; | |
599 | ||
600 | switch (cmd) { | |
601 | case PRC_IFDOWN: | |
91447636 | 602 | lck_mtx_lock(rt_mtx); |
1c79356b A |
603 | for (ia = in_ifaddrhead.tqh_first; ia; |
604 | ia = ia->ia_link.tqe_next) { | |
605 | if (ia->ia_ifa.ifa_addr == sa | |
606 | && (ia->ia_flags & IFA_ROUTE)) { | |
607 | /* | |
608 | * in_ifscrub kills the interface route. | |
609 | */ | |
91447636 | 610 | in_ifscrub(ia->ia_ifp, ia, 1); |
1c79356b A |
611 | /* |
612 | * in_ifadown gets rid of all the rest of | |
613 | * the routes. This is not quite the right | |
614 | * thing to do, but at least if we are running | |
615 | * a routing process they will come back. | |
616 | */ | |
9bccf70c | 617 | in_ifadown(&ia->ia_ifa, 1); |
1c79356b A |
618 | break; |
619 | } | |
620 | } | |
91447636 | 621 | lck_mtx_unlock(rt_mtx); |
1c79356b A |
622 | break; |
623 | ||
624 | case PRC_IFUP: | |
91447636 | 625 | lck_mtx_lock(rt_mtx); |
1c79356b A |
626 | for (ia = in_ifaddrhead.tqh_first; ia; |
627 | ia = ia->ia_link.tqe_next) { | |
628 | if (ia->ia_ifa.ifa_addr == sa) | |
629 | break; | |
630 | } | |
91447636 A |
631 | if (ia == 0 || (ia->ia_flags & IFA_ROUTE)) { |
632 | lck_mtx_unlock(rt_mtx); | |
1c79356b | 633 | return; |
91447636 | 634 | } |
1c79356b A |
635 | flags = RTF_UP; |
636 | ifp = ia->ia_ifa.ifa_ifp; | |
637 | ||
638 | if ((ifp->if_flags & IFF_LOOPBACK) | |
639 | || (ifp->if_flags & IFF_POINTOPOINT)) | |
640 | flags |= RTF_HOST; | |
641 | ||
91447636 A |
642 | err = rtinit_locked(&ia->ia_ifa, RTM_ADD, flags); |
643 | lck_mtx_unlock(rt_mtx); | |
1c79356b A |
644 | if (err == 0) |
645 | ia->ia_flags |= IFA_ROUTE; | |
646 | break; | |
647 | } | |
648 | } | |
649 | ||
650 | u_long rip_sendspace = RIPSNDQ; | |
651 | u_long rip_recvspace = RIPRCVQ; | |
652 | ||
9bccf70c A |
653 | SYSCTL_INT(_net_inet_raw, OID_AUTO, maxdgram, CTLFLAG_RW, |
654 | &rip_sendspace, 0, "Maximum outgoing raw IP datagram size"); | |
655 | SYSCTL_INT(_net_inet_raw, OID_AUTO, recvspace, CTLFLAG_RW, | |
656 | &rip_recvspace, 0, "Maximum incoming raw IP datagram size"); | |
1c79356b A |
657 | |
658 | static int | |
659 | rip_attach(struct socket *so, int proto, struct proc *p) | |
660 | { | |
661 | struct inpcb *inp; | |
2d21ac55 | 662 | int error; |
1c79356b A |
663 | |
664 | inp = sotoinpcb(so); | |
665 | if (inp) | |
666 | panic("rip_attach"); | |
9bccf70c A |
667 | if ((so->so_state & SS_PRIV) == 0) |
668 | return (EPERM); | |
1c79356b | 669 | |
9bccf70c A |
670 | error = soreserve(so, rip_sendspace, rip_recvspace); |
671 | if (error) | |
672 | return error; | |
1c79356b | 673 | error = in_pcballoc(so, &ripcbinfo, p); |
1c79356b A |
674 | if (error) |
675 | return error; | |
676 | inp = (struct inpcb *)so->so_pcb; | |
677 | inp->inp_vflag |= INP_IPV4; | |
678 | inp->inp_ip_p = proto; | |
9bccf70c | 679 | inp->inp_ip_ttl = ip_defttl; |
1c79356b A |
680 | return 0; |
681 | } | |
682 | ||
9bccf70c | 683 | __private_extern__ int |
1c79356b A |
684 | rip_detach(struct socket *so) |
685 | { | |
686 | struct inpcb *inp; | |
687 | ||
688 | inp = sotoinpcb(so); | |
689 | if (inp == 0) | |
690 | panic("rip_detach"); | |
2d21ac55 | 691 | #if MROUTING |
1c79356b A |
692 | if (so == ip_mrouter) |
693 | ip_mrouter_done(); | |
694 | ip_rsvp_force_done(so); | |
695 | if (so == ip_rsvpd) | |
696 | ip_rsvp_done(); | |
2d21ac55 | 697 | #endif /* MROUTING */ |
1c79356b A |
698 | in_pcbdetach(inp); |
699 | return 0; | |
700 | } | |
701 | ||
9bccf70c | 702 | __private_extern__ int |
1c79356b A |
703 | rip_abort(struct socket *so) |
704 | { | |
705 | soisdisconnected(so); | |
706 | return rip_detach(so); | |
707 | } | |
708 | ||
9bccf70c | 709 | __private_extern__ int |
1c79356b A |
710 | rip_disconnect(struct socket *so) |
711 | { | |
712 | if ((so->so_state & SS_ISCONNECTED) == 0) | |
713 | return ENOTCONN; | |
714 | return rip_abort(so); | |
715 | } | |
716 | ||
9bccf70c | 717 | __private_extern__ int |
2d21ac55 | 718 | rip_bind(struct socket *so, struct sockaddr *nam, __unused struct proc *p) |
1c79356b A |
719 | { |
720 | struct inpcb *inp = sotoinpcb(so); | |
721 | struct sockaddr_in *addr = (struct sockaddr_in *)nam; | |
91447636 | 722 | struct ifaddr *ifa = NULL; |
1c79356b A |
723 | |
724 | if (nam->sa_len != sizeof(*addr)) | |
725 | return EINVAL; | |
726 | ||
91447636 | 727 | if (TAILQ_EMPTY(&ifnet_head) || ((addr->sin_family != AF_INET) && |
1c79356b A |
728 | (addr->sin_family != AF_IMPLINK)) || |
729 | (addr->sin_addr.s_addr && | |
91447636 | 730 | (ifa = ifa_ifwithaddr((struct sockaddr *)addr)) == 0)) { |
1c79356b | 731 | return EADDRNOTAVAIL; |
91447636 A |
732 | } |
733 | else if (ifa) { | |
734 | ifafree(ifa); | |
735 | ifa = NULL; | |
736 | } | |
1c79356b A |
737 | inp->inp_laddr = addr->sin_addr; |
738 | return 0; | |
739 | } | |
740 | ||
9bccf70c | 741 | __private_extern__ int |
2d21ac55 | 742 | rip_connect(struct socket *so, struct sockaddr *nam, __unused struct proc *p) |
1c79356b A |
743 | { |
744 | struct inpcb *inp = sotoinpcb(so); | |
745 | struct sockaddr_in *addr = (struct sockaddr_in *)nam; | |
746 | ||
747 | if (nam->sa_len != sizeof(*addr)) | |
748 | return EINVAL; | |
91447636 | 749 | if (TAILQ_EMPTY(&ifnet_head)) |
1c79356b A |
750 | return EADDRNOTAVAIL; |
751 | if ((addr->sin_family != AF_INET) && | |
752 | (addr->sin_family != AF_IMPLINK)) | |
753 | return EAFNOSUPPORT; | |
754 | inp->inp_faddr = addr->sin_addr; | |
755 | soisconnected(so); | |
756 | return 0; | |
757 | } | |
758 | ||
9bccf70c | 759 | __private_extern__ int |
1c79356b A |
760 | rip_shutdown(struct socket *so) |
761 | { | |
762 | socantsendmore(so); | |
763 | return 0; | |
764 | } | |
765 | ||
9bccf70c | 766 | __private_extern__ int |
2d21ac55 A |
767 | rip_send(struct socket *so, __unused int flags, struct mbuf *m, struct sockaddr *nam, |
768 | __unused struct mbuf *control, __unused struct proc *p) | |
1c79356b A |
769 | { |
770 | struct inpcb *inp = sotoinpcb(so); | |
771 | register u_long dst; | |
772 | ||
773 | if (so->so_state & SS_ISCONNECTED) { | |
774 | if (nam) { | |
775 | m_freem(m); | |
776 | return EISCONN; | |
777 | } | |
778 | dst = inp->inp_faddr.s_addr; | |
779 | } else { | |
780 | if (nam == NULL) { | |
781 | m_freem(m); | |
782 | return ENOTCONN; | |
783 | } | |
784 | dst = ((struct sockaddr_in *)nam)->sin_addr.s_addr; | |
785 | } | |
786 | return rip_output(m, so, dst); | |
787 | } | |
788 | ||
0c530ab8 A |
789 | /* note: rip_unlock is called from different protos instead of the generic socket_unlock, |
790 | * it will handle the socket dealloc on last reference | |
791 | * */ | |
91447636 A |
792 | int |
793 | rip_unlock(struct socket *so, int refcount, int debug) | |
794 | { | |
795 | int lr_saved; | |
796 | struct inpcb *inp = sotoinpcb(so); | |
0c530ab8 A |
797 | |
798 | if (debug == 0) | |
799 | lr_saved = (unsigned int) __builtin_return_address(0); | |
91447636 | 800 | else lr_saved = debug; |
0c530ab8 | 801 | |
91447636 A |
802 | if (refcount) { |
803 | if (so->so_usecount <= 0) | |
2d21ac55 | 804 | panic("rip_unlock: bad refoucnt so=%p val=%x\n", so, so->so_usecount); |
91447636 A |
805 | so->so_usecount--; |
806 | if (so->so_usecount == 0 && (inp->inp_wantcnt == WNT_STOPUSING)) { | |
0c530ab8 | 807 | /* cleanup after last reference */ |
91447636 A |
808 | lck_mtx_unlock(so->so_proto->pr_domain->dom_mtx); |
809 | lck_rw_lock_exclusive(ripcbinfo.mtx); | |
810 | in_pcbdispose(inp); | |
811 | lck_rw_done(ripcbinfo.mtx); | |
812 | return(0); | |
813 | } | |
814 | } | |
2d21ac55 | 815 | so->unlock_lr[so->next_unlock_lr] = (u_int32_t)lr_saved; |
0c530ab8 | 816 | so->next_unlock_lr = (so->next_unlock_lr+1) % SO_LCKDBG_MAX; |
91447636 A |
817 | lck_mtx_unlock(so->so_proto->pr_domain->dom_mtx); |
818 | return(0); | |
819 | } | |
820 | ||
1c79356b A |
821 | static int |
822 | rip_pcblist SYSCTL_HANDLER_ARGS | |
823 | { | |
2d21ac55 A |
824 | #pragma unused(oidp, arg1, arg2) |
825 | int error, i, n; | |
1c79356b A |
826 | struct inpcb *inp, **inp_list; |
827 | inp_gen_t gencnt; | |
828 | struct xinpgen xig; | |
829 | ||
830 | /* | |
831 | * The process of preparing the TCB list is too time-consuming and | |
832 | * resource-intensive to repeat twice on every request. | |
833 | */ | |
91447636 A |
834 | lck_rw_lock_exclusive(ripcbinfo.mtx); |
835 | if (req->oldptr == USER_ADDR_NULL) { | |
1c79356b A |
836 | n = ripcbinfo.ipi_count; |
837 | req->oldidx = 2 * (sizeof xig) | |
838 | + (n + n/8) * sizeof(struct xinpcb); | |
91447636 | 839 | lck_rw_done(ripcbinfo.mtx); |
1c79356b A |
840 | return 0; |
841 | } | |
842 | ||
91447636 A |
843 | if (req->newptr != USER_ADDR_NULL) { |
844 | lck_rw_done(ripcbinfo.mtx); | |
1c79356b | 845 | return EPERM; |
91447636 | 846 | } |
1c79356b A |
847 | |
848 | /* | |
849 | * OK, now we're committed to doing something. | |
850 | */ | |
1c79356b A |
851 | gencnt = ripcbinfo.ipi_gencnt; |
852 | n = ripcbinfo.ipi_count; | |
3a60a9f5 A |
853 | |
854 | bzero(&xig, sizeof(xig)); | |
1c79356b A |
855 | xig.xig_len = sizeof xig; |
856 | xig.xig_count = n; | |
857 | xig.xig_gen = gencnt; | |
858 | xig.xig_sogen = so_gencnt; | |
859 | error = SYSCTL_OUT(req, &xig, sizeof xig); | |
91447636 A |
860 | if (error) { |
861 | lck_rw_done(ripcbinfo.mtx); | |
1c79356b | 862 | return error; |
91447636 | 863 | } |
9bccf70c A |
864 | /* |
865 | * We are done if there is no pcb | |
866 | */ | |
91447636 A |
867 | if (n == 0) { |
868 | lck_rw_done(ripcbinfo.mtx); | |
9bccf70c | 869 | return 0; |
91447636 | 870 | } |
1c79356b A |
871 | |
872 | inp_list = _MALLOC(n * sizeof *inp_list, M_TEMP, M_WAITOK); | |
91447636 A |
873 | if (inp_list == 0) { |
874 | lck_rw_done(ripcbinfo.mtx); | |
1c79356b | 875 | return ENOMEM; |
91447636 | 876 | } |
1c79356b | 877 | |
1c79356b A |
878 | for (inp = ripcbinfo.listhead->lh_first, i = 0; inp && i < n; |
879 | inp = inp->inp_list.le_next) { | |
91447636 | 880 | if (inp->inp_gencnt <= gencnt && inp->inp_state != INPCB_STATE_DEAD) |
1c79356b A |
881 | inp_list[i++] = inp; |
882 | } | |
1c79356b A |
883 | n = i; |
884 | ||
885 | error = 0; | |
886 | for (i = 0; i < n; i++) { | |
887 | inp = inp_list[i]; | |
91447636 | 888 | if (inp->inp_gencnt <= gencnt && inp->inp_state != INPCB_STATE_DEAD) { |
1c79356b | 889 | struct xinpcb xi; |
3a60a9f5 A |
890 | |
891 | bzero(&xi, sizeof(xi)); | |
1c79356b A |
892 | xi.xi_len = sizeof xi; |
893 | /* XXX should avoid extra copy */ | |
91447636 | 894 | inpcb_to_compat(inp, &xi.xi_inp); |
1c79356b A |
895 | if (inp->inp_socket) |
896 | sotoxsocket(inp->inp_socket, &xi.xi_socket); | |
897 | error = SYSCTL_OUT(req, &xi, sizeof xi); | |
898 | } | |
899 | } | |
900 | if (!error) { | |
901 | /* | |
902 | * Give the user an updated idea of our state. | |
903 | * If the generation differs from what we told | |
904 | * her before, she knows that something happened | |
905 | * while we were processing this request, and it | |
906 | * might be necessary to retry. | |
907 | */ | |
3a60a9f5 A |
908 | bzero(&xig, sizeof(xig)); |
909 | xig.xig_len = sizeof xig; | |
1c79356b A |
910 | xig.xig_gen = ripcbinfo.ipi_gencnt; |
911 | xig.xig_sogen = so_gencnt; | |
912 | xig.xig_count = ripcbinfo.ipi_count; | |
1c79356b A |
913 | error = SYSCTL_OUT(req, &xig, sizeof xig); |
914 | } | |
915 | FREE(inp_list, M_TEMP); | |
91447636 | 916 | lck_rw_done(ripcbinfo.mtx); |
1c79356b A |
917 | return error; |
918 | } | |
919 | ||
1c79356b A |
920 | SYSCTL_PROC(_net_inet_raw, OID_AUTO/*XXX*/, pcblist, CTLFLAG_RD, 0, 0, |
921 | rip_pcblist, "S,xinpcb", "List of active raw IP sockets"); | |
922 | ||
923 | struct pr_usrreqs rip_usrreqs = { | |
924 | rip_abort, pru_accept_notsupp, rip_attach, rip_bind, rip_connect, | |
925 | pru_connect2_notsupp, in_control, rip_detach, rip_disconnect, | |
926 | pru_listen_notsupp, in_setpeeraddr, pru_rcvd_notsupp, | |
9bccf70c | 927 | pru_rcvoob_notsupp, rip_send, pru_sense_null, rip_shutdown, |
91447636 | 928 | in_setsockaddr, sosend, soreceive, pru_sopoll_notsupp |
1c79356b | 929 | }; |
2d21ac55 | 930 | /* DSEP Review Done pl-20051213-v02 @3253 */ |