xref: /xnu-12377.61.12/bsd/security/audit/audit_bsm_socket_type.c (revision 4d495c6e23c53686cf65f45067f79024cf5dcee8)
1 /*-
2  * Copyright (c) 2008-2009 Apple Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1.  Redistributions of source code must retain the above copyright
9  *     notice, this list of conditions and the following disclaimer.
10  * 2.  Redistributions in binary form must reproduce the above copyright
11  *     notice, this list of conditions and the following disclaimer in the
12  *     documentation and/or other materials provided with the distribution.
13  * 3.  Neither the name of Apple Inc. ("Apple") nor the names of
14  *     its contributors may be used to endorse or promote products derived
15  *     from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR
21  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
25  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
26  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  *
29  */
30 
31 #include <sys/param.h>
32 #include <sys/socket.h>
33 
34 #include <security/audit/audit.h>
35 
36 #include <bsm/audit_record.h>
37 #include <bsm/audit_socket_type.h>
38 
39 #if CONFIG_AUDIT
40 struct bsm_socket_type {
41 	u_short bst_bsm_socket_type;
42 	int     bst_local_socket_type;
43 };
44 
45 #define ST_NO_LOCAL_MAPPING     -600
46 
47 static const struct bsm_socket_type bsm_socket_types[] = {
48 	{ BSM_SOCK_DGRAM, SOCK_DGRAM },
49 	{ BSM_SOCK_STREAM, SOCK_STREAM },
50 	{ BSM_SOCK_RAW, SOCK_RAW },
51 	{ BSM_SOCK_RDM, SOCK_RDM },
52 	{ BSM_SOCK_SEQPACKET, SOCK_SEQPACKET },
53 };
54 static const int bsm_socket_types_count = sizeof(bsm_socket_types) /
55     sizeof(bsm_socket_types[0]);
56 
57 static const struct bsm_socket_type *
bsm_lookup_local_socket_type(int local_socket_type)58 bsm_lookup_local_socket_type(int local_socket_type)
59 {
60 	int i;
61 
62 	for (i = 0; i < bsm_socket_types_count; i++) {
63 		if (bsm_socket_types[i].bst_local_socket_type ==
64 		    local_socket_type) {
65 			return &bsm_socket_types[i];
66 		}
67 	}
68 	return NULL;
69 }
70 
71 u_short
au_socket_type_to_bsm(int local_socket_type)72 au_socket_type_to_bsm(int local_socket_type)
73 {
74 	const struct bsm_socket_type *bstp;
75 
76 	bstp = bsm_lookup_local_socket_type(local_socket_type);
77 	if (bstp == NULL) {
78 		return BSM_SOCK_UNKNOWN;
79 	}
80 	return bstp->bst_bsm_socket_type;
81 }
82 
83 static const struct bsm_socket_type *
bsm_lookup_bsm_socket_type(u_short bsm_socket_type)84 bsm_lookup_bsm_socket_type(u_short bsm_socket_type)
85 {
86 	int i;
87 
88 	for (i = 0; i < bsm_socket_types_count; i++) {
89 		if (bsm_socket_types[i].bst_bsm_socket_type ==
90 		    bsm_socket_type) {
91 			return &bsm_socket_types[i];
92 		}
93 	}
94 	return NULL;
95 }
96 
97 int
au_bsm_to_socket_type(u_short bsm_socket_type,int * local_socket_typep)98 au_bsm_to_socket_type(u_short bsm_socket_type, int *local_socket_typep)
99 {
100 	const struct bsm_socket_type *bstp;
101 
102 	bstp = bsm_lookup_bsm_socket_type(bsm_socket_type);
103 	if (bstp == NULL || bstp->bst_local_socket_type) {
104 		return -1;
105 	}
106 	*local_socket_typep = bstp->bst_local_socket_type;
107 	return 0;
108 }
109 #endif /* CONFIG_AUDIT */
110