1187214Srwatson/*-
2187214Srwatson * Copyright (c) 2008 Apple Inc.
3187214Srwatson * All rights reserved.
4187214Srwatson *
5187214Srwatson * Redistribution and use in source and binary forms, with or without
6187214Srwatson * modification, are permitted provided that the following conditions
7187214Srwatson * are met:
8187214Srwatson * 1.  Redistributions of source code must retain the above copyright
9187214Srwatson *     notice, this list of conditions and the following disclaimer.
10187214Srwatson * 2.  Redistributions in binary form must reproduce the above copyright
11187214Srwatson *     notice, this list of conditions and the following disclaimer in the
12187214Srwatson *     documentation and/or other materials provided with the distribution.
13187214Srwatson * 3.  Neither the name of Apple Inc. ("Apple") nor the names of
14187214Srwatson *     its contributors may be used to endorse or promote products derived
15187214Srwatson *     from this software without specific prior written permission.
16187214Srwatson *
17187214Srwatson * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND
18187214Srwatson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19187214Srwatson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20187214Srwatson * ARE DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR
21187214Srwatson * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22187214Srwatson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23187214Srwatson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24187214Srwatson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
25187214Srwatson * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
26187214Srwatson * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27187214Srwatson * POSSIBILITY OF SUCH DAMAGE.
28187214Srwatson *
29187214Srwatson * P4: //depot/projects/trustedbsd/openbsm/libbsm/bsm_socket_type.c#1
30187214Srwatson */
31187214Srwatson
32187214Srwatson#include <sys/cdefs.h>
33187214Srwatson__FBSDID("$FreeBSD$");
34187214Srwatson
35187214Srwatson#include <sys/param.h>
36187214Srwatson#include <sys/socket.h>
37187214Srwatson
38187214Srwatson#include <security/audit/audit.h>
39187214Srwatson
40187214Srwatson#include <bsm/audit_record.h>
41187214Srwatson#include <bsm/audit_socket_type.h>
42187214Srwatson
43187214Srwatsonstruct bsm_socket_type {
44187214Srwatson	u_short	bst_bsm_socket_type;
45187214Srwatson	int	bst_local_socket_type;
46187214Srwatson};
47187214Srwatson
48187214Srwatson#define	ST_NO_LOCAL_MAPPING	-600
49187214Srwatson
50187214Srwatsonstatic const struct bsm_socket_type bsm_socket_types[] = {
51187214Srwatson	{ BSM_SOCK_DGRAM, SOCK_DGRAM },
52187214Srwatson	{ BSM_SOCK_STREAM, SOCK_STREAM },
53187214Srwatson	{ BSM_SOCK_RAW, SOCK_RAW },
54187214Srwatson	{ BSM_SOCK_RDM, SOCK_RDM },
55187214Srwatson	{ BSM_SOCK_SEQPACKET, SOCK_SEQPACKET },
56187214Srwatson};
57187214Srwatsonstatic const int bsm_socket_types_count = sizeof(bsm_socket_types) /
58187214Srwatson	    sizeof(bsm_socket_types[0]);
59187214Srwatson
60187214Srwatsonstatic const struct bsm_socket_type *
61187214Srwatsonbsm_lookup_local_socket_type(int local_socket_type)
62187214Srwatson{
63187214Srwatson	int i;
64187214Srwatson
65187214Srwatson	for (i = 0; i < bsm_socket_types_count; i++) {
66187214Srwatson		if (bsm_socket_types[i].bst_local_socket_type ==
67187214Srwatson		    local_socket_type)
68187214Srwatson			return (&bsm_socket_types[i]);
69187214Srwatson	}
70187214Srwatson	return (NULL);
71187214Srwatson}
72187214Srwatson
73187214Srwatsonu_short
74187214Srwatsonau_socket_type_to_bsm(int local_socket_type)
75187214Srwatson{
76187214Srwatson	const struct bsm_socket_type *bstp;
77187214Srwatson
78187214Srwatson	bstp = bsm_lookup_local_socket_type(local_socket_type);
79187214Srwatson	if (bstp == NULL)
80187214Srwatson		return (BSM_SOCK_UNKNOWN);
81187214Srwatson	return (bstp->bst_bsm_socket_type);
82187214Srwatson}
83187214Srwatson
84187214Srwatsonstatic const struct bsm_socket_type *
85187214Srwatsonbsm_lookup_bsm_socket_type(u_short bsm_socket_type)
86187214Srwatson{
87187214Srwatson	int i;
88187214Srwatson
89187214Srwatson	for (i = 0; i < bsm_socket_types_count; i++) {
90187214Srwatson		if (bsm_socket_types[i].bst_bsm_socket_type ==
91187214Srwatson		    bsm_socket_type)
92187214Srwatson			return (&bsm_socket_types[i]);
93187214Srwatson	}
94187214Srwatson	return (NULL);
95187214Srwatson}
96187214Srwatson
97187214Srwatsonint
98187214Srwatsonau_bsm_to_socket_type(u_short bsm_socket_type, int *local_socket_typep)
99187214Srwatson{
100187214Srwatson	const struct bsm_socket_type *bstp;
101187214Srwatson
102187214Srwatson	bstp = bsm_lookup_bsm_socket_type(bsm_socket_type);
103187214Srwatson	if (bstp == NULL || bstp->bst_local_socket_type)
104187214Srwatson		return (-1);
105187214Srwatson	*local_socket_typep = bstp->bst_local_socket_type;
106187214Srwatson	return (0);
107187214Srwatson}
108