]>
git.saurik.com Git - apple/xnu.git/blob - bsd/net/kext_net.c
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_OSREFERENCE_HEADER_START@
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
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
30 /* Copyright (C) 1999 Apple Computer, Inc. */
33 * Support for Network Kernel Extensions: Socket Filters
35 * Justin C. Walker, 990319
38 #include <sys/types.h>
39 #include <sys/queue.h>
40 #include <sys/malloc.h>
41 #include <sys/param.h>
43 #include <sys/domain.h>
44 #include <sys/protosw.h>
45 #include <sys/socket.h>
46 #include <machine/spl.h>
49 /* List of kernel extensions (networking) known to kernel */
50 struct nf_list nf_list
;
52 static int sockfilter_fix_symantec_bug(struct NFDescriptor
* theirDesc
);
55 * Register a global filter for the specified protocol
56 * Make a few checks and then insert the new descriptor in the
57 * filter list and, if global, in its protosw's chain.
60 register_sockfilter(struct NFDescriptor
*nfp
, struct NFDescriptor
*nfp1
,
61 struct protosw
*pr
, int flags
)
63 static int NF_initted
= 0;
68 /* Fix Symantec's broken NPC kext */
69 if (nfp
->nf_handle
== 0xf1ab02de) {
70 int err
= sockfilter_fix_symantec_bug(nfp
);
82 * Install the extension:
83 * First, put it in the global list of all filters
84 * Then, if global, install in the protosw's list
86 TAILQ_INSERT_TAIL(&nf_list
, nfp
, nf_list
);
87 if (nfp
->nf_flags
& NFD_GLOBAL
)
88 { if (flags
& NFF_BEFORE
)
90 { TAILQ_INSERT_HEAD(&pr
->pr_sfilter
,
93 TAILQ_INSERT_BEFORE(nfp1
, nfp
, nf_next
);
94 } else /* Default: AFTER */
96 { TAILQ_INSERT_TAIL(&pr
->pr_sfilter
,
99 TAILQ_INSERT_AFTER(&pr
->pr_sfilter
, nfp1
,
108 unregister_sockfilter(struct NFDescriptor
*nfp
, struct protosw
*pr
, __unused
int flags
)
112 TAILQ_REMOVE(&nf_list
, nfp
, nf_list
);
113 /* Only globals are attached to the protosw entry */
114 if (nfp
->nf_flags
& NFD_GLOBAL
)
115 TAILQ_REMOVE(&pr
->pr_sfilter
, nfp
, nf_next
);
120 struct NFDescriptor
*
121 find_nke(unsigned int handle
)
122 { struct NFDescriptor
*nfp
;
124 nfp
= nf_list
.tqh_first
;
126 { if (nfp
->nf_handle
== handle
)
128 nfp
= nfp
->nf_list
.tqe_next
;
134 * Insert a previously registered, non-global, NKE into the list of
135 * active NKEs for this socket. Then invoke its "attach/create" entry.
136 * Assumed called with protection in place (spl/mutex/whatever)
137 * XXX: How to which extension is not found, on error.
140 nke_insert(struct socket
*so
, struct so_nke
*np
)
142 struct kextcb
*kp
, *kp1
;
143 struct NFDescriptor
*nf1
, *nf2
= NULL
;
145 if (np
->nke_where
!= NULL
)
146 { if ((nf2
= find_nke(np
->nke_where
)) == NULL
)
148 return(ENXIO
);/* XXX */
152 if ((nf1
= find_nke(np
->nke_handle
)) == NULL
)
154 return(ENXIO
);/* XXX */
159 if (np
->nke_flags
& NFF_BEFORE
)
162 { if (kp
->e_nfd
== nf2
)
168 return(ENXIO
);/* XXX */
173 { if (kp
->e_nfd
== nf2
)
179 return(ENXIO
);/* XXX */
184 * Here with kp1 pointing to the insertion point.
185 * If null, this is first entry.
186 * Now, create and insert the descriptor.
189 MALLOC(kp
, struct kextcb
*, sizeof(*kp
), M_TEMP
, M_WAITOK
);
191 return(ENOBUFS
); /* so_free will clean up */
192 bzero(kp
, sizeof (*kp
));
194 { kp
->e_next
= so
->so_ext
;
197 { kp
->e_next
= kp1
->e_next
;
202 kp
->e_soif
= nf1
->nf_soif
;
203 kp
->e_sout
= nf1
->nf_soutil
;
205 * Ignore return value for create
206 * Everyone gets a chance at startup
208 if (kp
->e_soif
&& kp
->e_soif
->sf_socreate
)
209 (*kp
->e_soif
->sf_socreate
)(so
, so
->so_proto
, kp
);
214 * The following gunk is a fix for Symantec's broken NPC kext
215 * Symantec's NPC kext does not check that the kextcb->e_fcb
216 * is not NULL before derefing it. The result is a panic in
217 * the very few cases where the e_fcb is actually NULL.
219 * This gross chunk of code copies the old function ptrs
220 * supplied by the kext and wraps a few select ones in
221 * our own functions that just check for NULL before
222 * calling in to the kext.
225 static struct sockif
* g_symantec_if_funcs
= NULL
;
226 static struct sockutil
* g_symantec_util_funcs
= NULL
;
227 static int sym_fix_sbflush(struct sockbuf
*, struct kextcb
*);
228 static int sym_fix_sbappend(struct sockbuf
*, struct mbuf
*, struct kextcb
*);
229 static int sym_fix_soclose(struct socket
*, struct kextcb
*);
230 static int sym_fix_sofree(struct socket
*, struct kextcb
*);
231 static int sym_fix_soconnect(struct socket
*, struct sockaddr
*, struct kextcb
*);
232 static int sym_fix_soisconnected(struct socket
*, struct kextcb
*);
233 static int sym_fix_sosend(struct socket
*, struct sockaddr
**, struct uio
**, struct mbuf
**,
234 struct mbuf
**, int *, struct kextcb
*);
235 static int sym_fix_socantrcvmore(struct socket
*, struct kextcb
*);
236 static int sym_fix_socontrol(struct socket
*, struct sockopt
*, struct kextcb
*);
238 static int sockfilter_fix_symantec_bug(struct NFDescriptor
* theirDesc
)
240 if (!g_symantec_if_funcs
) {
241 MALLOC(g_symantec_if_funcs
, struct sockif
*, sizeof(*g_symantec_if_funcs
), M_TEMP
, M_WAITOK
);
243 if (!g_symantec_if_funcs
)
246 *g_symantec_if_funcs
= *theirDesc
->nf_soif
;
249 if (!g_symantec_util_funcs
) {
250 MALLOC(g_symantec_util_funcs
, struct sockutil
*, sizeof(*g_symantec_util_funcs
), M_TEMP
, M_WAITOK
);
252 if (!g_symantec_util_funcs
)
255 *g_symantec_util_funcs
= *theirDesc
->nf_soutil
;
258 if (theirDesc
->nf_soutil
->su_sbflush
)
259 theirDesc
->nf_soutil
->su_sbflush
= sym_fix_sbflush
;
260 if (theirDesc
->nf_soutil
->su_sbappend
)
261 theirDesc
->nf_soutil
->su_sbappend
= sym_fix_sbappend
;
262 if (theirDesc
->nf_soif
->sf_soclose
)
263 theirDesc
->nf_soif
->sf_soclose
= sym_fix_soclose
;
264 if (theirDesc
->nf_soif
->sf_sofree
)
265 theirDesc
->nf_soif
->sf_sofree
= sym_fix_sofree
;
266 if (theirDesc
->nf_soif
->sf_soconnect
)
267 theirDesc
->nf_soif
->sf_soconnect
= sym_fix_soconnect
;
268 if (theirDesc
->nf_soif
->sf_soisconnected
)
269 theirDesc
->nf_soif
->sf_soisconnected
= sym_fix_soisconnected
;
270 if (theirDesc
->nf_soif
->sf_sosend
)
271 theirDesc
->nf_soif
->sf_sosend
= sym_fix_sosend
;
272 if (theirDesc
->nf_soif
->sf_socantrcvmore
)
273 theirDesc
->nf_soif
->sf_socantrcvmore
= sym_fix_socantrcvmore
;
274 if (theirDesc
->nf_soif
->sf_socontrol
)
275 theirDesc
->nf_soif
->sf_socontrol
= sym_fix_socontrol
;
280 static int sym_fix_sbflush(struct sockbuf
*p1
, struct kextcb
*p2
)
282 if (p2
->e_fcb
!= NULL
&& g_symantec_util_funcs
)
283 return g_symantec_util_funcs
->su_sbflush(p1
, p2
);
288 static int sym_fix_sbappend(struct sockbuf
*p1
, struct mbuf
*p2
, struct kextcb
*p3
)
290 if (p3
->e_fcb
!= NULL
&& g_symantec_util_funcs
)
291 return g_symantec_util_funcs
->su_sbappend(p1
, p2
, p3
);
296 static int sym_fix_soclose(struct socket
*p1
, struct kextcb
*p2
)
298 if (p2
->e_fcb
!= NULL
&& g_symantec_if_funcs
)
299 return g_symantec_if_funcs
->sf_soclose(p1
, p2
);
304 static int sym_fix_sofree(struct socket
*p1
, struct kextcb
*p2
)
306 if (p2
->e_fcb
!= NULL
&& g_symantec_if_funcs
)
307 return g_symantec_if_funcs
->sf_sofree(p1
, p2
);
312 static int sym_fix_soconnect(struct socket
*p1
, struct sockaddr
*p2
, struct kextcb
*p3
)
314 if (p3
->e_fcb
!= NULL
&& g_symantec_if_funcs
)
315 return g_symantec_if_funcs
->sf_soconnect(p1
, p2
, p3
);
320 static int sym_fix_soisconnected(struct socket
*p1
, struct kextcb
*p2
)
322 if (p2
->e_fcb
!= NULL
&& g_symantec_if_funcs
)
323 return g_symantec_if_funcs
->sf_soisconnected(p1
, p2
);
328 static int sym_fix_sosend(struct socket
*p1
, struct sockaddr
**p2
, struct uio
**p3
, struct mbuf
**p4
,
329 struct mbuf
**p5
, int *p6
, struct kextcb
*p7
)
331 if (p7
->e_fcb
!= NULL
&& g_symantec_if_funcs
)
332 return g_symantec_if_funcs
->sf_sosend(p1
, p2
, p3
, p4
, p5
, p6
, p7
);
337 static int sym_fix_socantrcvmore(struct socket
*p1
, struct kextcb
*p2
)
339 if (p2
->e_fcb
!= NULL
&& g_symantec_if_funcs
)
340 return g_symantec_if_funcs
->sf_socantrcvmore(p1
, p2
);
345 static int sym_fix_socontrol(struct socket
*p1
, struct sockopt
*p2
, struct kextcb
*p3
)
347 if (p3
->e_fcb
!= NULL
&& g_symantec_if_funcs
)
348 return g_symantec_if_funcs
->sf_socontrol(p1
, p2
, p3
);