bsm_socket_type.c revision 331722
1228753Smm/*-
2228753Smm * Copyright (c) 2008 Apple Inc.
3228753Smm * All rights reserved.
4228753Smm *
5228753Smm * Redistribution and use in source and binary forms, with or without
6228753Smm * modification, are permitted provided that the following conditions
7228753Smm * are met:
8228753Smm * 1.  Redistributions of source code must retain the above copyright
9228753Smm *     notice, this list of conditions and the following disclaimer.
10228753Smm * 2.  Redistributions in binary form must reproduce the above copyright
11228753Smm *     notice, this list of conditions and the following disclaimer in the
12228753Smm *     documentation and/or other materials provided with the distribution.
13228753Smm * 3.  Neither the name of Apple Inc. ("Apple") nor the names of
14228753Smm *     its contributors may be used to endorse or promote products derived
15228753Smm *     from this software without specific prior written permission.
16228753Smm *
17228753Smm * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND
18228753Smm * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19228753Smm * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20228753Smm * ARE DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR
21228753Smm * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22228753Smm * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23228753Smm * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24228753Smm * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
25228753Smm * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
26228753Smm * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27228753Smm * POSSIBILITY OF SUCH DAMAGE.
28228753Smm */
29228753Smm
30228753Smm#include <sys/cdefs.h>
31228753Smm__FBSDID("$FreeBSD: stable/11/sys/security/audit/bsm_socket_type.c 331722 2018-03-29 02:50:57Z eadler $");
32228753Smm
33228753Smm#include <sys/param.h>
34228753Smm#include <sys/socket.h>
35228753Smm
36228753Smm#include <security/audit/audit.h>
37228753Smm
38228753Smm#include <bsm/audit_record.h>
39228753Smm#include <bsm/audit_socket_type.h>
40228753Smm
41228753Smmstruct bsm_socket_type {
42228753Smm	u_short	bst_bsm_socket_type;
43228753Smm	int	bst_local_socket_type;
44228753Smm};
45228753Smm
46228753Smm#define	ST_NO_LOCAL_MAPPING	-600
47228753Smm
48228753Smmstatic const struct bsm_socket_type bsm_socket_types[] = {
49228753Smm	{ BSM_SOCK_DGRAM, SOCK_DGRAM },
50228753Smm	{ BSM_SOCK_STREAM, SOCK_STREAM },
51228753Smm	{ BSM_SOCK_RAW, SOCK_RAW },
52228753Smm	{ BSM_SOCK_RDM, SOCK_RDM },
53228753Smm	{ BSM_SOCK_SEQPACKET, SOCK_SEQPACKET },
54228753Smm};
55228753Smmstatic const int bsm_socket_types_count = nitems(bsm_socket_types);
56228753Smm
57228753Smmstatic const struct bsm_socket_type *
58228753Smmbsm_lookup_local_socket_type(int local_socket_type)
59228753Smm{
60228753Smm	int i;
61228753Smm
62228753Smm	for (i = 0; i < bsm_socket_types_count; i++) {
63228753Smm		if (bsm_socket_types[i].bst_local_socket_type ==
64228753Smm		    local_socket_type)
65228753Smm			return (&bsm_socket_types[i]);
66228753Smm	}
67228753Smm	return (NULL);
68228753Smm}
69228753Smm
70228753Smmu_short
71228753Smmau_socket_type_to_bsm(int local_socket_type)
72228753Smm{
73228753Smm	const struct bsm_socket_type *bstp;
74228753Smm
75228753Smm	bstp = bsm_lookup_local_socket_type(local_socket_type);
76228753Smm	if (bstp == NULL)
77228753Smm		return (BSM_SOCK_UNKNOWN);
78228753Smm	return (bstp->bst_bsm_socket_type);
79228753Smm}
80228753Smm
81228753Smmstatic const struct bsm_socket_type *
82228753Smmbsm_lookup_bsm_socket_type(u_short bsm_socket_type)
83228753Smm{
84228753Smm	int i;
85228753Smm
86228753Smm	for (i = 0; i < bsm_socket_types_count; i++) {
87228753Smm		if (bsm_socket_types[i].bst_bsm_socket_type ==
88228753Smm		    bsm_socket_type)
89228753Smm			return (&bsm_socket_types[i]);
90228753Smm	}
91228753Smm	return (NULL);
92228753Smm}
93228753Smm
94228753Smmint
95228753Smmau_bsm_to_socket_type(u_short bsm_socket_type, int *local_socket_typep)
96228753Smm{
97228753Smm	const struct bsm_socket_type *bstp;
98228753Smm
99228753Smm	bstp = bsm_lookup_bsm_socket_type(bsm_socket_type);
100228753Smm	if (bstp == NULL || bstp->bst_local_socket_type)
101228753Smm		return (-1);
102228753Smm	*local_socket_typep = bstp->bst_local_socket_type;
103228753Smm	return (0);
104228753Smm}
105228753Smm