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