]>
git.saurik.com Git - apple/xnu.git/blob - bsd/net/kext_net.c
6fa429d0e46477603daaae317ef9286f63efbbfe
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_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. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
23 /* Copyright (C) 1999 Apple Computer, Inc. */
26 * Support for Network Kernel Extensions: Socket Filters
28 * Justin C. Walker, 990319
31 #include <sys/types.h>
32 #include <sys/queue.h>
33 #include <sys/malloc.h>
34 #include <sys/param.h>
36 #include <sys/domain.h>
37 #include <sys/protosw.h>
38 #include <sys/socket.h>
39 #include <machine/spl.h>
42 /* List of kernel extensions (networking) known to kernel */
43 struct nf_list nf_list
;
45 static int sockfilter_fix_symantec_bug(struct NFDescriptor
* theirDesc
);
48 * Register a global filter for the specified protocol
49 * Make a few checks and then insert the new descriptor in the
50 * filter list and, if global, in its protosw's chain.
53 register_sockfilter(struct NFDescriptor
*nfp
, struct NFDescriptor
*nfp1
,
54 struct protosw
*pr
, int flags
)
56 static int NF_initted
= 0;
61 /* Fix Symantec's broken NPC kext */
62 if (nfp
->nf_handle
== 0xf1ab02de) {
63 int err
= sockfilter_fix_symantec_bug(nfp
);
75 * Install the extension:
76 * First, put it in the global list of all filters
77 * Then, if global, install in the protosw's list
79 TAILQ_INSERT_TAIL(&nf_list
, nfp
, nf_list
);
80 if (nfp
->nf_flags
& NFD_GLOBAL
)
81 { if (flags
& NFF_BEFORE
)
83 { TAILQ_INSERT_HEAD(&pr
->pr_sfilter
,
86 TAILQ_INSERT_BEFORE(nfp1
, nfp
, nf_next
);
87 } else /* Default: AFTER */
89 { TAILQ_INSERT_TAIL(&pr
->pr_sfilter
,
92 TAILQ_INSERT_AFTER(&pr
->pr_sfilter
, nfp1
,
101 unregister_sockfilter(struct NFDescriptor
*nfp
, struct protosw
*pr
, __unused
int flags
)
105 TAILQ_REMOVE(&nf_list
, nfp
, nf_list
);
106 /* Only globals are attached to the protosw entry */
107 if (nfp
->nf_flags
& NFD_GLOBAL
)
108 TAILQ_REMOVE(&pr
->pr_sfilter
, nfp
, nf_next
);
113 struct NFDescriptor
*
114 find_nke(unsigned int handle
)
115 { struct NFDescriptor
*nfp
;
117 nfp
= nf_list
.tqh_first
;
119 { if (nfp
->nf_handle
== handle
)
121 nfp
= nfp
->nf_list
.tqe_next
;
127 * Insert a previously registered, non-global, NKE into the list of
128 * active NKEs for this socket. Then invoke its "attach/create" entry.
129 * Assumed called with protection in place (spl/mutex/whatever)
130 * XXX: How to which extension is not found, on error.
133 nke_insert(struct socket
*so
, struct so_nke
*np
)
135 struct kextcb
*kp
, *kp1
;
136 struct NFDescriptor
*nf1
, *nf2
= NULL
;
138 if (np
->nke_where
!= NULL
)
139 { if ((nf2
= find_nke(np
->nke_where
)) == NULL
)
141 return(ENXIO
);/* XXX */
145 if ((nf1
= find_nke(np
->nke_handle
)) == NULL
)
147 return(ENXIO
);/* XXX */
152 if (np
->nke_flags
& NFF_BEFORE
)
155 { if (kp
->e_nfd
== nf2
)
161 return(ENXIO
);/* XXX */
166 { if (kp
->e_nfd
== nf2
)
172 return(ENXIO
);/* XXX */
177 * Here with kp1 pointing to the insertion point.
178 * If null, this is first entry.
179 * Now, create and insert the descriptor.
182 MALLOC(kp
, struct kextcb
*, sizeof(*kp
), M_TEMP
, M_WAITOK
);
184 return(ENOBUFS
); /* so_free will clean up */
185 bzero(kp
, sizeof (*kp
));
187 { kp
->e_next
= so
->so_ext
;
190 { kp
->e_next
= kp1
->e_next
;
195 kp
->e_soif
= nf1
->nf_soif
;
196 kp
->e_sout
= nf1
->nf_soutil
;
198 * Ignore return value for create
199 * Everyone gets a chance at startup
201 if (kp
->e_soif
&& kp
->e_soif
->sf_socreate
)
202 (*kp
->e_soif
->sf_socreate
)(so
, so
->so_proto
, kp
);
207 * The following gunk is a fix for Symantec's broken NPC kext
208 * Symantec's NPC kext does not check that the kextcb->e_fcb
209 * is not NULL before derefing it. The result is a panic in
210 * the very few cases where the e_fcb is actually NULL.
212 * This gross chunk of code copies the old function ptrs
213 * supplied by the kext and wraps a few select ones in
214 * our own functions that just check for NULL before
215 * calling in to the kext.
218 static struct sockif
* g_symantec_if_funcs
= NULL
;
219 static struct sockutil
* g_symantec_util_funcs
= NULL
;
220 static int sym_fix_sbflush(struct sockbuf
*, struct kextcb
*);
221 static int sym_fix_sbappend(struct sockbuf
*, struct mbuf
*, struct kextcb
*);
222 static int sym_fix_soclose(struct socket
*, struct kextcb
*);
223 static int sym_fix_sofree(struct socket
*, struct kextcb
*);
224 static int sym_fix_soconnect(struct socket
*, struct sockaddr
*, struct kextcb
*);
225 static int sym_fix_soisconnected(struct socket
*, struct kextcb
*);
226 static int sym_fix_sosend(struct socket
*, struct sockaddr
**, struct uio
**, struct mbuf
**,
227 struct mbuf
**, int *, struct kextcb
*);
228 static int sym_fix_socantrcvmore(struct socket
*, struct kextcb
*);
229 static int sym_fix_socontrol(struct socket
*, struct sockopt
*, struct kextcb
*);
231 static int sockfilter_fix_symantec_bug(struct NFDescriptor
* theirDesc
)
233 if (!g_symantec_if_funcs
) {
234 MALLOC(g_symantec_if_funcs
, struct sockif
*, sizeof(*g_symantec_if_funcs
), M_TEMP
, M_WAITOK
);
236 if (!g_symantec_if_funcs
)
239 *g_symantec_if_funcs
= *theirDesc
->nf_soif
;
242 if (!g_symantec_util_funcs
) {
243 MALLOC(g_symantec_util_funcs
, struct sockutil
*, sizeof(*g_symantec_util_funcs
), M_TEMP
, M_WAITOK
);
245 if (!g_symantec_util_funcs
)
248 *g_symantec_util_funcs
= *theirDesc
->nf_soutil
;
251 if (theirDesc
->nf_soutil
->su_sbflush
)
252 theirDesc
->nf_soutil
->su_sbflush
= sym_fix_sbflush
;
253 if (theirDesc
->nf_soutil
->su_sbappend
)
254 theirDesc
->nf_soutil
->su_sbappend
= sym_fix_sbappend
;
255 if (theirDesc
->nf_soif
->sf_soclose
)
256 theirDesc
->nf_soif
->sf_soclose
= sym_fix_soclose
;
257 if (theirDesc
->nf_soif
->sf_sofree
)
258 theirDesc
->nf_soif
->sf_sofree
= sym_fix_sofree
;
259 if (theirDesc
->nf_soif
->sf_soconnect
)
260 theirDesc
->nf_soif
->sf_soconnect
= sym_fix_soconnect
;
261 if (theirDesc
->nf_soif
->sf_soisconnected
)
262 theirDesc
->nf_soif
->sf_soisconnected
= sym_fix_soisconnected
;
263 if (theirDesc
->nf_soif
->sf_sosend
)
264 theirDesc
->nf_soif
->sf_sosend
= sym_fix_sosend
;
265 if (theirDesc
->nf_soif
->sf_socantrcvmore
)
266 theirDesc
->nf_soif
->sf_socantrcvmore
= sym_fix_socantrcvmore
;
267 if (theirDesc
->nf_soif
->sf_socontrol
)
268 theirDesc
->nf_soif
->sf_socontrol
= sym_fix_socontrol
;
273 static int sym_fix_sbflush(struct sockbuf
*p1
, struct kextcb
*p2
)
275 if (p2
->e_fcb
!= NULL
&& g_symantec_util_funcs
)
276 return g_symantec_util_funcs
->su_sbflush(p1
, p2
);
281 static int sym_fix_sbappend(struct sockbuf
*p1
, struct mbuf
*p2
, struct kextcb
*p3
)
283 if (p3
->e_fcb
!= NULL
&& g_symantec_util_funcs
)
284 return g_symantec_util_funcs
->su_sbappend(p1
, p2
, p3
);
289 static int sym_fix_soclose(struct socket
*p1
, struct kextcb
*p2
)
291 if (p2
->e_fcb
!= NULL
&& g_symantec_if_funcs
)
292 return g_symantec_if_funcs
->sf_soclose(p1
, p2
);
297 static int sym_fix_sofree(struct socket
*p1
, struct kextcb
*p2
)
299 if (p2
->e_fcb
!= NULL
&& g_symantec_if_funcs
)
300 return g_symantec_if_funcs
->sf_sofree(p1
, p2
);
305 static int sym_fix_soconnect(struct socket
*p1
, struct sockaddr
*p2
, struct kextcb
*p3
)
307 if (p3
->e_fcb
!= NULL
&& g_symantec_if_funcs
)
308 return g_symantec_if_funcs
->sf_soconnect(p1
, p2
, p3
);
313 static int sym_fix_soisconnected(struct socket
*p1
, struct kextcb
*p2
)
315 if (p2
->e_fcb
!= NULL
&& g_symantec_if_funcs
)
316 return g_symantec_if_funcs
->sf_soisconnected(p1
, p2
);
321 static int sym_fix_sosend(struct socket
*p1
, struct sockaddr
**p2
, struct uio
**p3
, struct mbuf
**p4
,
322 struct mbuf
**p5
, int *p6
, struct kextcb
*p7
)
324 if (p7
->e_fcb
!= NULL
&& g_symantec_if_funcs
)
325 return g_symantec_if_funcs
->sf_sosend(p1
, p2
, p3
, p4
, p5
, p6
, p7
);
330 static int sym_fix_socantrcvmore(struct socket
*p1
, struct kextcb
*p2
)
332 if (p2
->e_fcb
!= NULL
&& g_symantec_if_funcs
)
333 return g_symantec_if_funcs
->sf_socantrcvmore(p1
, p2
);
338 static int sym_fix_socontrol(struct socket
*p1
, struct sockopt
*p2
, struct kextcb
*p3
)
340 if (p3
->e_fcb
!= NULL
&& g_symantec_if_funcs
)
341 return g_symantec_if_funcs
->sf_socontrol(p1
, p2
, p3
);