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
30187214Srwatson#include <sys/cdefs.h>
31187214Srwatson__FBSDID("$FreeBSD$");
32187214Srwatson
33187214Srwatson#include <sys/param.h>
34187214Srwatson#include <sys/socket.h>
35187214Srwatson
36187214Srwatson#include <security/audit/audit.h>
37187214Srwatson
38187214Srwatson#include <bsm/audit_record.h>
39187214Srwatson#include <bsm/audit_socket_type.h>
40187214Srwatson
41187214Srwatsonstruct bsm_socket_type {
42187214Srwatson	u_short	bst_bsm_socket_type;
43187214Srwatson	int	bst_local_socket_type;
44187214Srwatson};
45187214Srwatson
46187214Srwatson#define	ST_NO_LOCAL_MAPPING	-600
47187214Srwatson
48187214Srwatsonstatic const struct bsm_socket_type bsm_socket_types[] = {
49187214Srwatson	{ BSM_SOCK_DGRAM, SOCK_DGRAM },
50187214Srwatson	{ BSM_SOCK_STREAM, SOCK_STREAM },
51187214Srwatson	{ BSM_SOCK_RAW, SOCK_RAW },
52187214Srwatson	{ BSM_SOCK_RDM, SOCK_RDM },
53187214Srwatson	{ BSM_SOCK_SEQPACKET, SOCK_SEQPACKET },
54187214Srwatson};
55187214Srwatsonstatic const int bsm_socket_types_count = sizeof(bsm_socket_types) /
56187214Srwatson	    sizeof(bsm_socket_types[0]);
57187214Srwatson
58187214Srwatsonstatic const struct bsm_socket_type *
59187214Srwatsonbsm_lookup_local_socket_type(int local_socket_type)
60187214Srwatson{
61187214Srwatson	int i;
62187214Srwatson
63187214Srwatson	for (i = 0; i < bsm_socket_types_count; i++) {
64187214Srwatson		if (bsm_socket_types[i].bst_local_socket_type ==
65187214Srwatson		    local_socket_type)
66187214Srwatson			return (&bsm_socket_types[i]);
67187214Srwatson	}
68187214Srwatson	return (NULL);
69187214Srwatson}
70187214Srwatson
71187214Srwatsonu_short
72187214Srwatsonau_socket_type_to_bsm(int local_socket_type)
73187214Srwatson{
74187214Srwatson	const struct bsm_socket_type *bstp;
75187214Srwatson
76187214Srwatson	bstp = bsm_lookup_local_socket_type(local_socket_type);
77187214Srwatson	if (bstp == NULL)
78187214Srwatson		return (BSM_SOCK_UNKNOWN);
79187214Srwatson	return (bstp->bst_bsm_socket_type);
80187214Srwatson}
81187214Srwatson
82187214Srwatsonstatic const struct bsm_socket_type *
83187214Srwatsonbsm_lookup_bsm_socket_type(u_short bsm_socket_type)
84187214Srwatson{
85187214Srwatson	int i;
86187214Srwatson
87187214Srwatson	for (i = 0; i < bsm_socket_types_count; i++) {
88187214Srwatson		if (bsm_socket_types[i].bst_bsm_socket_type ==
89187214Srwatson		    bsm_socket_type)
90187214Srwatson			return (&bsm_socket_types[i]);
91187214Srwatson	}
92187214Srwatson	return (NULL);
93187214Srwatson}
94187214Srwatson
95187214Srwatsonint
96187214Srwatsonau_bsm_to_socket_type(u_short bsm_socket_type, int *local_socket_typep)
97187214Srwatson{
98187214Srwatson	const struct bsm_socket_type *bstp;
99187214Srwatson
100187214Srwatson	bstp = bsm_lookup_bsm_socket_type(bsm_socket_type);
101187214Srwatson	if (bstp == NULL || bstp->bst_local_socket_type)
102187214Srwatson		return (-1);
103187214Srwatson	*local_socket_typep = bstp->bst_local_socket_type;
104187214Srwatson	return (0);
105187214Srwatson}
106