1187063Srwatson/*-
2187063Srwatson * Copyright (c) 2008 Apple Inc.
3187063Srwatson * All rights reserved.
4187063Srwatson *
5187063Srwatson * Redistribution and use in source and binary forms, with or without
6187063Srwatson * modification, are permitted provided that the following conditions
7187063Srwatson * are met:
8187063Srwatson * 1.  Redistributions of source code must retain the above copyright
9187063Srwatson *     notice, this list of conditions and the following disclaimer.
10187063Srwatson * 2.  Redistributions in binary form must reproduce the above copyright
11187063Srwatson *     notice, this list of conditions and the following disclaimer in the
12187063Srwatson *     documentation and/or other materials provided with the distribution.
13187063Srwatson * 3.  Neither the name of Apple Inc. ("Apple") nor the names of
14187063Srwatson *     its contributors may be used to endorse or promote products derived
15187063Srwatson *     from this software without specific prior written permission.
16187063Srwatson *
17187063Srwatson * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND
18187063Srwatson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19187063Srwatson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20187063Srwatson * ARE DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR
21187063Srwatson * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22187063Srwatson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23187063Srwatson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24187063Srwatson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
25187063Srwatson * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
26187063Srwatson * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27187063Srwatson * POSSIBILITY OF SUCH DAMAGE.
28187063Srwatson */
29187063Srwatson
30187063Srwatson#include <sys/types.h>
31187063Srwatson#include <sys/socket.h>
32187063Srwatson
33187063Srwatson#include <config/config.h>
34187063Srwatson
35187063Srwatson#include <bsm/audit_domain.h>
36187063Srwatson#include <bsm/libbsm.h>
37187063Srwatson
38187063Srwatsonstruct bsm_domain {
39187063Srwatson	u_short	bd_bsm_domain;
40187063Srwatson	int	bd_local_domain;
41187063Srwatson};
42187063Srwatson
43187063Srwatson#define	PF_NO_LOCAL_MAPPING	-600
44187063Srwatson
45187063Srwatsonstatic const struct bsm_domain bsm_domains[] = {
46187063Srwatson	{ BSM_PF_UNSPEC, PF_UNSPEC },
47187063Srwatson	{ BSM_PF_LOCAL, PF_LOCAL },
48187063Srwatson	{ BSM_PF_INET, PF_INET },
49187063Srwatson	{ BSM_PF_IMPLINK,
50187063Srwatson#ifdef PF_IMPLINK
51187063Srwatson	PF_IMPLINK
52187063Srwatson#else
53187063Srwatson	PF_NO_LOCAL_MAPPING
54187063Srwatson#endif
55187063Srwatson	},
56187063Srwatson	{ BSM_PF_PUP,
57187063Srwatson#ifdef PF_PUP
58187063Srwatson	PF_PUP
59187063Srwatson#else
60187063Srwatson	PF_NO_LOCAL_MAPPING
61187063Srwatson#endif
62187063Srwatson	},
63187063Srwatson	{ BSM_PF_CHAOS,
64187063Srwatson#ifdef PF_CHAOS
65187063Srwatson	PF_CHAOS
66187063Srwatson#else
67187063Srwatson	PF_NO_LOCAL_MAPPING
68187063Srwatson#endif
69187063Srwatson	},
70187063Srwatson	{ BSM_PF_NS,
71187063Srwatson#ifdef PF_NS
72187063Srwatson	PF_NS
73187063Srwatson#else
74187063Srwatson	PF_NO_LOCAL_MAPPING
75187063Srwatson#endif
76187063Srwatson	},
77187063Srwatson	{ BSM_PF_NBS,
78187063Srwatson#ifdef PF_NBS
79187063Srwatson	PF_NBS
80187063Srwatson#else
81187063Srwatson	PF_NO_LOCAL_MAPPING
82187063Srwatson#endif
83187063Srwatson	},
84187063Srwatson	{ BSM_PF_ECMA,
85187063Srwatson#ifdef PF_ECMA
86187063Srwatson	PF_ECMA
87187063Srwatson#else
88187063Srwatson	PF_NO_LOCAL_MAPPING
89187063Srwatson#endif
90187063Srwatson	},
91187063Srwatson	{ BSM_PF_DATAKIT,
92187063Srwatson#ifdef PF_DATAKIT
93187063Srwatson	PF_DATAKIT
94187063Srwatson#else
95187063Srwatson	PF_NO_LOCAL_MAPPING
96187063Srwatson#endif
97187063Srwatson	},
98187063Srwatson	{ BSM_PF_CCITT,
99187063Srwatson#ifdef PF_CCITT
100187063Srwatson	PF_CCITT
101187063Srwatson#else
102187063Srwatson	PF_NO_LOCAL_MAPPING
103187063Srwatson#endif
104187063Srwatson	},
105187063Srwatson	{ BSM_PF_SNA, PF_SNA },
106187063Srwatson	{ BSM_PF_DECnet, PF_DECnet },
107187063Srwatson	{ BSM_PF_DLI,
108187063Srwatson#ifdef PF_DLI
109187063Srwatson	PF_DLI
110187063Srwatson#else
111187063Srwatson	PF_NO_LOCAL_MAPPING
112187063Srwatson#endif
113187063Srwatson	},
114187063Srwatson	{ BSM_PF_LAT,
115187063Srwatson#ifdef PF_LAT
116187063Srwatson	PF_LAT
117187063Srwatson#else
118187063Srwatson	PF_NO_LOCAL_MAPPING
119187063Srwatson#endif
120187063Srwatson	},
121187063Srwatson	{ BSM_PF_HYLINK,
122187063Srwatson#ifdef PF_HYLINK
123187063Srwatson	PF_HYLINK
124187063Srwatson#else
125187063Srwatson	PF_NO_LOCAL_MAPPING
126187063Srwatson#endif
127187063Srwatson	},
128187063Srwatson	{ BSM_PF_APPLETALK, PF_APPLETALK },
129187063Srwatson	{ BSM_PF_NIT,
130187063Srwatson#ifdef PF_NIT
131187063Srwatson	PF_NIT
132187063Srwatson#else
133187063Srwatson	PF_NO_LOCAL_MAPPING
134187063Srwatson#endif
135187063Srwatson	},
136187063Srwatson	{ BSM_PF_802,
137187063Srwatson#ifdef PF_802
138187063Srwatson	PF_802
139187063Srwatson#else
140187063Srwatson	PF_NO_LOCAL_MAPPING
141187063Srwatson#endif
142187063Srwatson	},
143187063Srwatson	{ BSM_PF_OSI,
144187063Srwatson#ifdef PF_OSI
145187063Srwatson	PF_OSI
146187063Srwatson#else
147187063Srwatson	PF_NO_LOCAL_MAPPING
148187063Srwatson#endif
149187063Srwatson	},
150187063Srwatson	{ BSM_PF_X25,
151187063Srwatson#ifdef PF_X25
152187063Srwatson	PF_X25
153187063Srwatson#else
154187063Srwatson	PF_NO_LOCAL_MAPPING
155187063Srwatson#endif
156187063Srwatson	},
157187063Srwatson	{ BSM_PF_OSINET,
158187063Srwatson#ifdef PF_OSINET
159187063Srwatson	PF_OSINET
160187063Srwatson#else
161187063Srwatson	PF_NO_LOCAL_MAPPING
162187063Srwatson#endif
163187063Srwatson	},
164187063Srwatson	{ BSM_PF_GOSIP,
165187063Srwatson#ifdef PF_GOSIP
166187063Srwatson	PF_GOSIP
167187063Srwatson#else
168187063Srwatson	PF_NO_LOCAL_MAPPING
169187063Srwatson#endif
170187063Srwatson	},
171187063Srwatson	{ BSM_PF_IPX, PF_IPX },
172187063Srwatson	{ BSM_PF_ROUTE, PF_ROUTE },
173187063Srwatson	{ BSM_PF_LINK,
174187063Srwatson#ifdef PF_LINK
175187063Srwatson	PF_LINK
176187063Srwatson#else
177187063Srwatson	PF_NO_LOCAL_MAPPING
178187063Srwatson#endif
179187063Srwatson	},
180187063Srwatson	{ BSM_PF_INET6, PF_INET6 },
181187063Srwatson	{ BSM_PF_KEY, PF_KEY },
182187063Srwatson	{ BSM_PF_NCA,
183187063Srwatson#ifdef PF_NCA
184187063Srwatson	PF_NCA
185187063Srwatson#else
186187063Srwatson	PF_NO_LOCAL_MAPPING
187187063Srwatson#endif
188187063Srwatson	},
189187063Srwatson	{ BSM_PF_POLICY,
190187063Srwatson#ifdef PF_POLICY
191187063Srwatson	PF_POLICY
192187063Srwatson#else
193187063Srwatson	PF_NO_LOCAL_MAPPING
194187063Srwatson#endif
195187063Srwatson	},
196187063Srwatson	{ BSM_PF_INET_OFFLOAD,
197187063Srwatson#ifdef PF_INET_OFFLOAD
198187063Srwatson	PF_INET_OFFLOAD
199187063Srwatson#else
200187063Srwatson	PF_NO_LOCAL_MAPPING
201187063Srwatson#endif
202187063Srwatson	},
203187063Srwatson	{ BSM_PF_NETBIOS,
204187063Srwatson#ifdef PF_NETBIOS
205187063Srwatson	PF_NETBIOS
206187063Srwatson#else
207187063Srwatson	PF_NO_LOCAL_MAPPING
208187063Srwatson#endif
209187063Srwatson	},
210187063Srwatson	{ BSM_PF_ISO,
211187063Srwatson#ifdef PF_ISO
212187063Srwatson	PF_ISO
213187063Srwatson#else
214187063Srwatson	PF_NO_LOCAL_MAPPING
215187063Srwatson#endif
216187063Srwatson	},
217187063Srwatson	{ BSM_PF_XTP,
218187063Srwatson#ifdef PF_XTP
219187063Srwatson	PF_XTP
220187063Srwatson#else
221187063Srwatson	PF_NO_LOCAL_MAPPING
222187063Srwatson#endif
223187063Srwatson	},
224187063Srwatson	{ BSM_PF_COIP,
225187063Srwatson#ifdef PF_COIP
226187063Srwatson	PF_COIP
227187063Srwatson#else
228187063Srwatson	PF_NO_LOCAL_MAPPING
229187063Srwatson#endif
230187063Srwatson	},
231187063Srwatson	{ BSM_PF_CNT,
232187063Srwatson#ifdef PF_CNT
233187063Srwatson	PF_CNT
234187063Srwatson#else
235187063Srwatson	PF_NO_LOCAL_MAPPING
236187063Srwatson#endif
237187063Srwatson	},
238187063Srwatson	{ BSM_PF_RTIP,
239187063Srwatson#ifdef PF_RTIP
240187063Srwatson	PF_RTIP
241187063Srwatson#else
242187063Srwatson	PF_NO_LOCAL_MAPPING
243187063Srwatson#endif
244187063Srwatson	},
245187063Srwatson	{ BSM_PF_SIP,
246187063Srwatson#ifdef PF_SIP
247187063Srwatson	PF_SIP
248187063Srwatson#else
249187063Srwatson	PF_NO_LOCAL_MAPPING
250187063Srwatson#endif
251187063Srwatson	},
252187063Srwatson	{ BSM_PF_PIP,
253187063Srwatson#ifdef PF_PIP
254187063Srwatson	PF_PIP
255187063Srwatson#else
256187063Srwatson	PF_NO_LOCAL_MAPPING
257187063Srwatson#endif
258187063Srwatson	},
259187063Srwatson	{ BSM_PF_ISDN,
260187063Srwatson#ifdef PF_ISDN
261187063Srwatson	PF_ISDN
262187063Srwatson#else
263187063Srwatson	PF_NO_LOCAL_MAPPING
264187063Srwatson#endif
265187063Srwatson	},
266187063Srwatson	{ BSM_PF_E164,
267187063Srwatson#ifdef PF_E164
268187063Srwatson	PF_E164
269187063Srwatson#else
270187063Srwatson	PF_NO_LOCAL_MAPPING
271187063Srwatson#endif
272187063Srwatson	},
273187063Srwatson	{ BSM_PF_NATM,
274187063Srwatson#ifdef PF_NATM
275187063Srwatson	PF_NATM
276187063Srwatson#else
277187063Srwatson	PF_NO_LOCAL_MAPPING
278187063Srwatson#endif
279187063Srwatson	},
280187063Srwatson	{ BSM_PF_ATM,
281187063Srwatson#ifdef PF_ATM
282187063Srwatson	PF_ATM
283187063Srwatson#else
284187063Srwatson	PF_NO_LOCAL_MAPPING
285187063Srwatson#endif
286187063Srwatson	},
287187063Srwatson	{ BSM_PF_NETGRAPH,
288187063Srwatson#ifdef PF_NETGRAPH
289187063Srwatson	PF_NETGRAPH
290187063Srwatson#else
291187063Srwatson	PF_NO_LOCAL_MAPPING
292187063Srwatson#endif
293187063Srwatson	},
294187063Srwatson	{ BSM_PF_SLOW,
295187063Srwatson#ifdef PF_SLOW
296187063Srwatson	PF_SLOW
297187063Srwatson#else
298187063Srwatson	PF_NO_LOCAL_MAPPING
299187063Srwatson#endif
300187063Srwatson	},
301187063Srwatson	{ BSM_PF_SCLUSTER,
302187063Srwatson#ifdef PF_SCLUSTER
303187063Srwatson	PF_SCLUSTER
304187063Srwatson#else
305187063Srwatson	PF_NO_LOCAL_MAPPING
306187063Srwatson#endif
307187063Srwatson	},
308187063Srwatson	{ BSM_PF_ARP,
309187063Srwatson#ifdef PF_ARP
310187063Srwatson	PF_ARP
311187063Srwatson#else
312187063Srwatson	PF_NO_LOCAL_MAPPING
313187063Srwatson#endif
314187063Srwatson	},
315187063Srwatson	{ BSM_PF_BLUETOOTH,
316187063Srwatson#ifdef PF_BLUETOOTH
317187063Srwatson	PF_BLUETOOTH
318187063Srwatson#else
319187063Srwatson	PF_NO_LOCAL_MAPPING
320187063Srwatson#endif
321187063Srwatson	},
322187063Srwatson	{ BSM_PF_AX25,
323187063Srwatson#ifdef PF_AX25
324187063Srwatson	PF_AX25
325187063Srwatson#else
326187063Srwatson	PF_NO_LOCAL_MAPPING
327187063Srwatson#endif
328187063Srwatson	},
329187063Srwatson	{ BSM_PF_ROSE,
330187063Srwatson#ifdef PF_ROSE
331187063Srwatson	PF_ROSE
332187063Srwatson#else
333187063Srwatson	PF_NO_LOCAL_MAPPING
334187063Srwatson#endif
335187063Srwatson	},
336187063Srwatson	{ BSM_PF_NETBEUI,
337187063Srwatson#ifdef PF_NETBEUI
338187063Srwatson	PF_NETBEUI
339187063Srwatson#else
340187063Srwatson	PF_NO_LOCAL_MAPPING
341187063Srwatson#endif
342187063Srwatson	},
343187063Srwatson	{ BSM_PF_SECURITY,
344187063Srwatson#ifdef PF_SECURITY
345187063Srwatson	PF_SECURITY
346187063Srwatson#else
347187063Srwatson	PF_NO_LOCAL_MAPPING
348187063Srwatson#endif
349187063Srwatson	},
350187063Srwatson	{ BSM_PF_PACKET,
351187063Srwatson#ifdef PF_PACKET
352187063Srwatson	PF_PACKET
353187063Srwatson#else
354187063Srwatson	PF_NO_LOCAL_MAPPING
355187063Srwatson#endif
356187063Srwatson	},
357187063Srwatson	{ BSM_PF_ASH,
358187063Srwatson#ifdef PF_ASH
359187063Srwatson	PF_ASH
360187063Srwatson#else
361187063Srwatson	PF_NO_LOCAL_MAPPING
362187063Srwatson#endif
363187063Srwatson	},
364187063Srwatson	{ BSM_PF_ECONET,
365187063Srwatson#ifdef PF_ECONET
366187063Srwatson	PF_ECONET
367187063Srwatson#else
368187063Srwatson	PF_NO_LOCAL_MAPPING
369187063Srwatson#endif
370187063Srwatson	},
371187063Srwatson	{ BSM_PF_ATMSVC,
372187063Srwatson#ifdef PF_ATMSVC
373187063Srwatson	PF_ATMSVC
374187063Srwatson#else
375187063Srwatson	PF_NO_LOCAL_MAPPING
376187063Srwatson#endif
377187063Srwatson	},
378187063Srwatson	{ BSM_PF_IRDA,
379187063Srwatson#ifdef PF_IRDA
380187063Srwatson	PF_IRDA
381187063Srwatson#else
382187063Srwatson	PF_NO_LOCAL_MAPPING
383187063Srwatson#endif
384187063Srwatson	},
385187063Srwatson	{ BSM_PF_PPPOX,
386187063Srwatson#ifdef PF_PPPOX
387187063Srwatson	PF_PPPOX
388187063Srwatson#else
389187063Srwatson	PF_NO_LOCAL_MAPPING
390187063Srwatson#endif
391187063Srwatson	},
392187063Srwatson	{ BSM_PF_WANPIPE,
393187063Srwatson#ifdef PF_WANPIPE
394187063Srwatson	PF_WANPIPE
395187063Srwatson#else
396187063Srwatson	PF_NO_LOCAL_MAPPING
397187063Srwatson#endif
398187063Srwatson	},
399187063Srwatson	{ BSM_PF_LLC,
400187063Srwatson#ifdef PF_LLC
401187063Srwatson	PF_LLC
402187063Srwatson#else
403187063Srwatson	PF_NO_LOCAL_MAPPING
404187063Srwatson#endif
405187063Srwatson	},
406187063Srwatson	{ BSM_PF_CAN,
407187063Srwatson#ifdef PF_CAN
408187063Srwatson	PF_CAN
409187063Srwatson#else
410187063Srwatson	PF_NO_LOCAL_MAPPING
411187063Srwatson#endif
412187063Srwatson	},
413187063Srwatson	{ BSM_PF_TIPC,
414187063Srwatson#ifdef PF_TIPC
415187063Srwatson	PF_TIPC
416187063Srwatson#else
417187063Srwatson	PF_NO_LOCAL_MAPPING
418187063Srwatson#endif
419187063Srwatson	},
420187063Srwatson	{ BSM_PF_IUCV,
421187063Srwatson#ifdef PF_IUCV
422187063Srwatson	PF_IUCV
423187063Srwatson#else
424187063Srwatson	PF_NO_LOCAL_MAPPING
425187063Srwatson#endif
426187063Srwatson	},
427187063Srwatson	{ BSM_PF_RXRPC,
428187063Srwatson#ifdef PF_RXRPC
429187063Srwatson	PF_RXRPC
430187063Srwatson#else
431187063Srwatson	PF_NO_LOCAL_MAPPING
432187063Srwatson#endif
433187063Srwatson	},
434187063Srwatson	{ BSM_PF_PHONET,
435187063Srwatson#ifdef PF_PHONET
436187063Srwatson	PF_PHONET
437187063Srwatson#else
438187063Srwatson	PF_NO_LOCAL_MAPPING
439187063Srwatson#endif
440187063Srwatson	},
441187063Srwatson};
442187063Srwatsonstatic const int bsm_domains_count = sizeof(bsm_domains) /
443187063Srwatson	    sizeof(bsm_domains[0]);
444187063Srwatson
445187063Srwatsonstatic const struct bsm_domain *
446187063Srwatsonbsm_lookup_local_domain(int local_domain)
447187063Srwatson{
448187063Srwatson	int i;
449187063Srwatson
450187063Srwatson	for (i = 0; i < bsm_domains_count; i++) {
451187063Srwatson		if (bsm_domains[i].bd_local_domain == local_domain)
452187063Srwatson			return (&bsm_domains[i]);
453187063Srwatson	}
454187063Srwatson	return (NULL);
455187063Srwatson}
456187063Srwatson
457187063Srwatsonu_short
458187063Srwatsonau_domain_to_bsm(int local_domain)
459187063Srwatson{
460187063Srwatson	const struct bsm_domain *bstp;
461187063Srwatson
462187063Srwatson	bstp = bsm_lookup_local_domain(local_domain);
463187063Srwatson	if (bstp == NULL)
464187063Srwatson		return (BSM_PF_UNKNOWN);
465187063Srwatson	return (bstp->bd_bsm_domain);
466187063Srwatson}
467187063Srwatson
468187063Srwatsonstatic const struct bsm_domain *
469187063Srwatsonbsm_lookup_bsm_domain(u_short bsm_domain)
470187063Srwatson{
471187063Srwatson	int i;
472187063Srwatson
473187063Srwatson	for (i = 0; i < bsm_domains_count; i++) {
474187063Srwatson		if (bsm_domains[i].bd_bsm_domain == bsm_domain)
475187063Srwatson			return (&bsm_domains[i]);
476187063Srwatson	}
477187063Srwatson	return (NULL);
478187063Srwatson}
479187063Srwatson
480187063Srwatsonint
481187063Srwatsonau_bsm_to_domain(u_short bsm_domain, int *local_domainp)
482187063Srwatson{
483187063Srwatson	const struct bsm_domain *bstp;
484187063Srwatson
485187063Srwatson	bstp = bsm_lookup_bsm_domain(bsm_domain);
486187063Srwatson	if (bstp == NULL || bstp->bd_local_domain)
487187063Srwatson		return (-1);
488187063Srwatson	*local_domainp = bstp->bd_local_domain;
489187063Srwatson	return (0);
490187063Srwatson}
491