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