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_domain.h>
39187214Srwatson#include <bsm/audit_record.h>
40187214Srwatson
41187214Srwatsonstruct bsm_domain {
42187214Srwatson	u_short	bd_bsm_domain;
43187214Srwatson	int	bd_local_domain;
44187214Srwatson};
45187214Srwatson
46187214Srwatson#define	PF_NO_LOCAL_MAPPING	-600
47187214Srwatson
48187214Srwatsonstatic const struct bsm_domain bsm_domains[] = {
49187214Srwatson	{ BSM_PF_UNSPEC, PF_UNSPEC },
50187214Srwatson	{ BSM_PF_LOCAL, PF_LOCAL },
51187214Srwatson	{ BSM_PF_INET, PF_INET },
52187214Srwatson	{ BSM_PF_IMPLINK,
53187214Srwatson#ifdef PF_IMPLINK
54187214Srwatson	PF_IMPLINK
55187214Srwatson#else
56187214Srwatson	PF_NO_LOCAL_MAPPING
57187214Srwatson#endif
58187214Srwatson	},
59187214Srwatson	{ BSM_PF_PUP,
60187214Srwatson#ifdef PF_PUP
61187214Srwatson	PF_PUP
62187214Srwatson#else
63187214Srwatson	PF_NO_LOCAL_MAPPING
64187214Srwatson#endif
65187214Srwatson	},
66187214Srwatson	{ BSM_PF_CHAOS,
67187214Srwatson#ifdef PF_CHAOS
68187214Srwatson	PF_CHAOS
69187214Srwatson#else
70187214Srwatson	PF_NO_LOCAL_MAPPING
71187214Srwatson#endif
72187214Srwatson	},
73187214Srwatson	{ BSM_PF_NS,
74187214Srwatson#ifdef PF_NS
75187214Srwatson	PF_NS
76187214Srwatson#else
77187214Srwatson	PF_NO_LOCAL_MAPPING
78187214Srwatson#endif
79187214Srwatson	},
80187214Srwatson	{ BSM_PF_NBS,
81187214Srwatson#ifdef PF_NBS
82187214Srwatson	PF_NBS
83187214Srwatson#else
84187214Srwatson	PF_NO_LOCAL_MAPPING
85187214Srwatson#endif
86187214Srwatson	},
87187214Srwatson	{ BSM_PF_ECMA,
88187214Srwatson#ifdef PF_ECMA
89187214Srwatson	PF_ECMA
90187214Srwatson#else
91187214Srwatson	PF_NO_LOCAL_MAPPING
92187214Srwatson#endif
93187214Srwatson	},
94187214Srwatson	{ BSM_PF_DATAKIT,
95187214Srwatson#ifdef PF_DATAKIT
96187214Srwatson	PF_DATAKIT
97187214Srwatson#else
98187214Srwatson	PF_NO_LOCAL_MAPPING
99187214Srwatson#endif
100187214Srwatson	},
101187214Srwatson	{ BSM_PF_CCITT,
102187214Srwatson#ifdef PF_CCITT
103187214Srwatson	PF_CCITT
104187214Srwatson#else
105187214Srwatson	PF_NO_LOCAL_MAPPING
106187214Srwatson#endif
107187214Srwatson	},
108187214Srwatson	{ BSM_PF_SNA, PF_SNA },
109187214Srwatson	{ BSM_PF_DECnet, PF_DECnet },
110187214Srwatson	{ BSM_PF_DLI,
111187214Srwatson#ifdef PF_DLI
112187214Srwatson	PF_DLI
113187214Srwatson#else
114187214Srwatson	PF_NO_LOCAL_MAPPING
115187214Srwatson#endif
116187214Srwatson	},
117187214Srwatson	{ BSM_PF_LAT,
118187214Srwatson#ifdef PF_LAT
119187214Srwatson	PF_LAT
120187214Srwatson#else
121187214Srwatson	PF_NO_LOCAL_MAPPING
122187214Srwatson#endif
123187214Srwatson	},
124187214Srwatson	{ BSM_PF_HYLINK,
125187214Srwatson#ifdef PF_HYLINK
126187214Srwatson	PF_HYLINK
127187214Srwatson#else
128187214Srwatson	PF_NO_LOCAL_MAPPING
129187214Srwatson#endif
130187214Srwatson	},
131187214Srwatson	{ BSM_PF_APPLETALK, PF_APPLETALK },
132187214Srwatson	{ BSM_PF_NIT,
133187214Srwatson#ifdef PF_NIT
134187214Srwatson	PF_NIT
135187214Srwatson#else
136187214Srwatson	PF_NO_LOCAL_MAPPING
137187214Srwatson#endif
138187214Srwatson	},
139187214Srwatson	{ BSM_PF_802,
140187214Srwatson#ifdef PF_802
141187214Srwatson	PF_802
142187214Srwatson#else
143187214Srwatson	PF_NO_LOCAL_MAPPING
144187214Srwatson#endif
145187214Srwatson	},
146187214Srwatson	{ BSM_PF_OSI,
147187214Srwatson#ifdef PF_OSI
148187214Srwatson	PF_OSI
149187214Srwatson#else
150187214Srwatson	PF_NO_LOCAL_MAPPING
151187214Srwatson#endif
152187214Srwatson	},
153187214Srwatson	{ BSM_PF_X25,
154187214Srwatson#ifdef PF_X25
155187214Srwatson	PF_X25
156187214Srwatson#else
157187214Srwatson	PF_NO_LOCAL_MAPPING
158187214Srwatson#endif
159187214Srwatson	},
160187214Srwatson	{ BSM_PF_OSINET,
161187214Srwatson#ifdef PF_OSINET
162187214Srwatson	PF_OSINET
163187214Srwatson#else
164187214Srwatson	PF_NO_LOCAL_MAPPING
165187214Srwatson#endif
166187214Srwatson	},
167187214Srwatson	{ BSM_PF_GOSIP,
168187214Srwatson#ifdef PF_GOSIP
169187214Srwatson	PF_GOSIP
170187214Srwatson#else
171187214Srwatson	PF_NO_LOCAL_MAPPING
172187214Srwatson#endif
173187214Srwatson	},
174187214Srwatson	{ BSM_PF_IPX, PF_IPX },
175187214Srwatson	{ BSM_PF_ROUTE, PF_ROUTE },
176187214Srwatson	{ BSM_PF_LINK,
177187214Srwatson#ifdef PF_LINK
178187214Srwatson	PF_LINK
179187214Srwatson#else
180187214Srwatson	PF_NO_LOCAL_MAPPING
181187214Srwatson#endif
182187214Srwatson	},
183187214Srwatson	{ BSM_PF_INET6, PF_INET6 },
184187214Srwatson	{ BSM_PF_KEY, PF_KEY },
185187214Srwatson	{ BSM_PF_NCA,
186187214Srwatson#ifdef PF_NCA
187187214Srwatson	PF_NCA
188187214Srwatson#else
189187214Srwatson	PF_NO_LOCAL_MAPPING
190187214Srwatson#endif
191187214Srwatson	},
192187214Srwatson	{ BSM_PF_POLICY,
193187214Srwatson#ifdef PF_POLICY
194187214Srwatson	PF_POLICY
195187214Srwatson#else
196187214Srwatson	PF_NO_LOCAL_MAPPING
197187214Srwatson#endif
198187214Srwatson	},
199187214Srwatson	{ BSM_PF_INET_OFFLOAD,
200187214Srwatson#ifdef PF_INET_OFFLOAD
201187214Srwatson	PF_INET_OFFLOAD
202187214Srwatson#else
203187214Srwatson	PF_NO_LOCAL_MAPPING
204187214Srwatson#endif
205187214Srwatson	},
206187214Srwatson	{ BSM_PF_NETBIOS,
207187214Srwatson#ifdef PF_NETBIOS
208187214Srwatson	PF_NETBIOS
209187214Srwatson#else
210187214Srwatson	PF_NO_LOCAL_MAPPING
211187214Srwatson#endif
212187214Srwatson	},
213187214Srwatson	{ BSM_PF_ISO,
214187214Srwatson#ifdef PF_ISO
215187214Srwatson	PF_ISO
216187214Srwatson#else
217187214Srwatson	PF_NO_LOCAL_MAPPING
218187214Srwatson#endif
219187214Srwatson	},
220187214Srwatson	{ BSM_PF_XTP,
221187214Srwatson#ifdef PF_XTP
222187214Srwatson	PF_XTP
223187214Srwatson#else
224187214Srwatson	PF_NO_LOCAL_MAPPING
225187214Srwatson#endif
226187214Srwatson	},
227187214Srwatson	{ BSM_PF_COIP,
228187214Srwatson#ifdef PF_COIP
229187214Srwatson	PF_COIP
230187214Srwatson#else
231187214Srwatson	PF_NO_LOCAL_MAPPING
232187214Srwatson#endif
233187214Srwatson	},
234187214Srwatson	{ BSM_PF_CNT,
235187214Srwatson#ifdef PF_CNT
236187214Srwatson	PF_CNT
237187214Srwatson#else
238187214Srwatson	PF_NO_LOCAL_MAPPING
239187214Srwatson#endif
240187214Srwatson	},
241187214Srwatson	{ BSM_PF_RTIP,
242187214Srwatson#ifdef PF_RTIP
243187214Srwatson	PF_RTIP
244187214Srwatson#else
245187214Srwatson	PF_NO_LOCAL_MAPPING
246187214Srwatson#endif
247187214Srwatson	},
248187214Srwatson	{ BSM_PF_SIP,
249187214Srwatson#ifdef PF_SIP
250187214Srwatson	PF_SIP
251187214Srwatson#else
252187214Srwatson	PF_NO_LOCAL_MAPPING
253187214Srwatson#endif
254187214Srwatson	},
255187214Srwatson	{ BSM_PF_PIP,
256187214Srwatson#ifdef PF_PIP
257187214Srwatson	PF_PIP
258187214Srwatson#else
259187214Srwatson	PF_NO_LOCAL_MAPPING
260187214Srwatson#endif
261187214Srwatson	},
262187214Srwatson	{ BSM_PF_ISDN,
263187214Srwatson#ifdef PF_ISDN
264187214Srwatson	PF_ISDN
265187214Srwatson#else
266187214Srwatson	PF_NO_LOCAL_MAPPING
267187214Srwatson#endif
268187214Srwatson	},
269187214Srwatson	{ BSM_PF_E164,
270187214Srwatson#ifdef PF_E164
271187214Srwatson	PF_E164
272187214Srwatson#else
273187214Srwatson	PF_NO_LOCAL_MAPPING
274187214Srwatson#endif
275187214Srwatson	},
276187214Srwatson	{ BSM_PF_NATM,
277187214Srwatson#ifdef PF_NATM
278187214Srwatson	PF_NATM
279187214Srwatson#else
280187214Srwatson	PF_NO_LOCAL_MAPPING
281187214Srwatson#endif
282187214Srwatson	},
283187214Srwatson	{ BSM_PF_ATM,
284187214Srwatson#ifdef PF_ATM
285187214Srwatson	PF_ATM
286187214Srwatson#else
287187214Srwatson	PF_NO_LOCAL_MAPPING
288187214Srwatson#endif
289187214Srwatson	},
290187214Srwatson	{ BSM_PF_NETGRAPH,
291187214Srwatson#ifdef PF_NETGRAPH
292187214Srwatson	PF_NETGRAPH
293187214Srwatson#else
294187214Srwatson	PF_NO_LOCAL_MAPPING
295187214Srwatson#endif
296187214Srwatson	},
297187214Srwatson	{ BSM_PF_SLOW,
298187214Srwatson#ifdef PF_SLOW
299187214Srwatson	PF_SLOW
300187214Srwatson#else
301187214Srwatson	PF_NO_LOCAL_MAPPING
302187214Srwatson#endif
303187214Srwatson	},
304187214Srwatson	{ BSM_PF_SCLUSTER,
305187214Srwatson#ifdef PF_SCLUSTER
306187214Srwatson	PF_SCLUSTER
307187214Srwatson#else
308187214Srwatson	PF_NO_LOCAL_MAPPING
309187214Srwatson#endif
310187214Srwatson	},
311187214Srwatson	{ BSM_PF_ARP,
312187214Srwatson#ifdef PF_ARP
313187214Srwatson	PF_ARP
314187214Srwatson#else
315187214Srwatson	PF_NO_LOCAL_MAPPING
316187214Srwatson#endif
317187214Srwatson	},
318187214Srwatson	{ BSM_PF_BLUETOOTH,
319187214Srwatson#ifdef PF_BLUETOOTH
320187214Srwatson	PF_BLUETOOTH
321187214Srwatson#else
322187214Srwatson	PF_NO_LOCAL_MAPPING
323187214Srwatson#endif
324187214Srwatson	},
325187214Srwatson	{ BSM_PF_AX25,
326187214Srwatson#ifdef PF_AX25
327187214Srwatson	PF_AX25
328187214Srwatson#else
329187214Srwatson	PF_NO_LOCAL_MAPPING
330187214Srwatson#endif
331187214Srwatson	},
332187214Srwatson	{ BSM_PF_ROSE,
333187214Srwatson#ifdef PF_ROSE
334187214Srwatson	PF_ROSE
335187214Srwatson#else
336187214Srwatson	PF_NO_LOCAL_MAPPING
337187214Srwatson#endif
338187214Srwatson	},
339187214Srwatson	{ BSM_PF_NETBEUI,
340187214Srwatson#ifdef PF_NETBEUI
341187214Srwatson	PF_NETBEUI
342187214Srwatson#else
343187214Srwatson	PF_NO_LOCAL_MAPPING
344187214Srwatson#endif
345187214Srwatson	},
346187214Srwatson	{ BSM_PF_SECURITY,
347187214Srwatson#ifdef PF_SECURITY
348187214Srwatson	PF_SECURITY
349187214Srwatson#else
350187214Srwatson	PF_NO_LOCAL_MAPPING
351187214Srwatson#endif
352187214Srwatson	},
353187214Srwatson	{ BSM_PF_PACKET,
354187214Srwatson#ifdef PF_PACKET
355187214Srwatson	PF_PACKET
356187214Srwatson#else
357187214Srwatson	PF_NO_LOCAL_MAPPING
358187214Srwatson#endif
359187214Srwatson	},
360187214Srwatson	{ BSM_PF_ASH,
361187214Srwatson#ifdef PF_ASH
362187214Srwatson	PF_ASH
363187214Srwatson#else
364187214Srwatson	PF_NO_LOCAL_MAPPING
365187214Srwatson#endif
366187214Srwatson	},
367187214Srwatson	{ BSM_PF_ECONET,
368187214Srwatson#ifdef PF_ECONET
369187214Srwatson	PF_ECONET
370187214Srwatson#else
371187214Srwatson	PF_NO_LOCAL_MAPPING
372187214Srwatson#endif
373187214Srwatson	},
374187214Srwatson	{ BSM_PF_ATMSVC,
375187214Srwatson#ifdef PF_ATMSVC
376187214Srwatson	PF_ATMSVC
377187214Srwatson#else
378187214Srwatson	PF_NO_LOCAL_MAPPING
379187214Srwatson#endif
380187214Srwatson	},
381187214Srwatson	{ BSM_PF_IRDA,
382187214Srwatson#ifdef PF_IRDA
383187214Srwatson	PF_IRDA
384187214Srwatson#else
385187214Srwatson	PF_NO_LOCAL_MAPPING
386187214Srwatson#endif
387187214Srwatson	},
388187214Srwatson	{ BSM_PF_PPPOX,
389187214Srwatson#ifdef PF_PPPOX
390187214Srwatson	PF_PPPOX
391187214Srwatson#else
392187214Srwatson	PF_NO_LOCAL_MAPPING
393187214Srwatson#endif
394187214Srwatson	},
395187214Srwatson	{ BSM_PF_WANPIPE,
396187214Srwatson#ifdef PF_WANPIPE
397187214Srwatson	PF_WANPIPE
398187214Srwatson#else
399187214Srwatson	PF_NO_LOCAL_MAPPING
400187214Srwatson#endif
401187214Srwatson	},
402187214Srwatson	{ BSM_PF_LLC,
403187214Srwatson#ifdef PF_LLC
404187214Srwatson	PF_LLC
405187214Srwatson#else
406187214Srwatson	PF_NO_LOCAL_MAPPING
407187214Srwatson#endif
408187214Srwatson	},
409187214Srwatson	{ BSM_PF_CAN,
410187214Srwatson#ifdef PF_CAN
411187214Srwatson	PF_CAN
412187214Srwatson#else
413187214Srwatson	PF_NO_LOCAL_MAPPING
414187214Srwatson#endif
415187214Srwatson	},
416187214Srwatson	{ BSM_PF_TIPC,
417187214Srwatson#ifdef PF_TIPC
418187214Srwatson	PF_TIPC
419187214Srwatson#else
420187214Srwatson	PF_NO_LOCAL_MAPPING
421187214Srwatson#endif
422187214Srwatson	},
423187214Srwatson	{ BSM_PF_IUCV,
424187214Srwatson#ifdef PF_IUCV
425187214Srwatson	PF_IUCV
426187214Srwatson#else
427187214Srwatson	PF_NO_LOCAL_MAPPING
428187214Srwatson#endif
429187214Srwatson	},
430187214Srwatson	{ BSM_PF_RXRPC,
431187214Srwatson#ifdef PF_RXRPC
432187214Srwatson	PF_RXRPC
433187214Srwatson#else
434187214Srwatson	PF_NO_LOCAL_MAPPING
435187214Srwatson#endif
436187214Srwatson	},
437187214Srwatson	{ BSM_PF_PHONET,
438187214Srwatson#ifdef PF_PHONET
439187214Srwatson	PF_PHONET
440187214Srwatson#else
441187214Srwatson	PF_NO_LOCAL_MAPPING
442187214Srwatson#endif
443187214Srwatson	},
444187214Srwatson};
445187214Srwatsonstatic const int bsm_domains_count = sizeof(bsm_domains) /
446187214Srwatson	    sizeof(bsm_domains[0]);
447187214Srwatson
448187214Srwatsonstatic const struct bsm_domain *
449187214Srwatsonbsm_lookup_local_domain(int local_domain)
450187214Srwatson{
451187214Srwatson	int i;
452187214Srwatson
453187214Srwatson	for (i = 0; i < bsm_domains_count; i++) {
454187214Srwatson		if (bsm_domains[i].bd_local_domain == local_domain)
455187214Srwatson			return (&bsm_domains[i]);
456187214Srwatson	}
457187214Srwatson	return (NULL);
458187214Srwatson}
459187214Srwatson
460187214Srwatsonu_short
461187214Srwatsonau_domain_to_bsm(int local_domain)
462187214Srwatson{
463187214Srwatson	const struct bsm_domain *bstp;
464187214Srwatson
465187214Srwatson	bstp = bsm_lookup_local_domain(local_domain);
466187214Srwatson	if (bstp == NULL)
467187214Srwatson		return (BSM_PF_UNKNOWN);
468187214Srwatson	return (bstp->bd_bsm_domain);
469187214Srwatson}
470187214Srwatson
471187214Srwatsonstatic const struct bsm_domain *
472187214Srwatsonbsm_lookup_bsm_domain(u_short bsm_domain)
473187214Srwatson{
474187214Srwatson	int i;
475187214Srwatson
476187214Srwatson	for (i = 0; i < bsm_domains_count; i++) {
477187214Srwatson		if (bsm_domains[i].bd_bsm_domain == bsm_domain)
478187214Srwatson			return (&bsm_domains[i]);
479187214Srwatson	}
480187214Srwatson	return (NULL);
481187214Srwatson}
482187214Srwatson
483187214Srwatsonint
484187214Srwatsonau_bsm_to_domain(u_short bsm_domain, int *local_domainp)
485187214Srwatson{
486187214Srwatson	const struct bsm_domain *bstp;
487187214Srwatson
488187214Srwatson	bstp = bsm_lookup_bsm_domain(bsm_domain);
489187214Srwatson	if (bstp == NULL || bstp->bd_local_domain)
490187214Srwatson		return (-1);
491187214Srwatson	*local_domainp = bstp->bd_local_domain;
492187214Srwatson	return (0);
493187214Srwatson}
494