1/*-
2 * Copyright (c) 2008 Apple Inc.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1.  Redistributions of source code must retain the above copyright
9 *     notice, this list of conditions and the following disclaimer.
10 * 2.  Redistributions in binary form must reproduce the above copyright
11 *     notice, this list of conditions and the following disclaimer in the
12 *     documentation and/or other materials provided with the distribution.
13 * 3.  Neither the name of Apple Inc. ("Apple") nor the names of
14 *     its contributors may be used to endorse or promote products derived
15 *     from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR
21 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
25 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
26 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 * POSSIBILITY OF SUCH DAMAGE.
28 */
29
30#include <sys/types.h>
31#include <sys/socket.h>
32
33#include <config/config.h>
34
35#include <bsm/audit_domain.h>
36#include <bsm/libbsm.h>
37
38struct bsm_domain {
39	u_short	bd_bsm_domain;
40	int	bd_local_domain;
41};
42
43#define	PF_NO_LOCAL_MAPPING	-600
44
45static const struct bsm_domain bsm_domains[] = {
46	{ BSM_PF_UNSPEC, PF_UNSPEC },
47	{ BSM_PF_LOCAL, PF_LOCAL },
48	{ BSM_PF_INET, PF_INET },
49	{ BSM_PF_IMPLINK,
50#ifdef PF_IMPLINK
51	PF_IMPLINK
52#else
53	PF_NO_LOCAL_MAPPING
54#endif
55	},
56	{ BSM_PF_PUP,
57#ifdef PF_PUP
58	PF_PUP
59#else
60	PF_NO_LOCAL_MAPPING
61#endif
62	},
63	{ BSM_PF_CHAOS,
64#ifdef PF_CHAOS
65	PF_CHAOS
66#else
67	PF_NO_LOCAL_MAPPING
68#endif
69	},
70	{ BSM_PF_NS,
71#ifdef PF_NS
72	PF_NS
73#else
74	PF_NO_LOCAL_MAPPING
75#endif
76	},
77	{ BSM_PF_NBS,
78#ifdef PF_NBS
79	PF_NBS
80#else
81	PF_NO_LOCAL_MAPPING
82#endif
83	},
84	{ BSM_PF_ECMA,
85#ifdef PF_ECMA
86	PF_ECMA
87#else
88	PF_NO_LOCAL_MAPPING
89#endif
90	},
91	{ BSM_PF_DATAKIT,
92#ifdef PF_DATAKIT
93	PF_DATAKIT
94#else
95	PF_NO_LOCAL_MAPPING
96#endif
97	},
98	{ BSM_PF_CCITT,
99#ifdef PF_CCITT
100	PF_CCITT
101#else
102	PF_NO_LOCAL_MAPPING
103#endif
104	},
105	{ BSM_PF_SNA, PF_SNA },
106	{ BSM_PF_DECnet, PF_DECnet },
107	{ BSM_PF_DLI,
108#ifdef PF_DLI
109	PF_DLI
110#else
111	PF_NO_LOCAL_MAPPING
112#endif
113	},
114	{ BSM_PF_LAT,
115#ifdef PF_LAT
116	PF_LAT
117#else
118	PF_NO_LOCAL_MAPPING
119#endif
120	},
121	{ BSM_PF_HYLINK,
122#ifdef PF_HYLINK
123	PF_HYLINK
124#else
125	PF_NO_LOCAL_MAPPING
126#endif
127	},
128	{ BSM_PF_APPLETALK, PF_APPLETALK },
129	{ BSM_PF_NIT,
130#ifdef PF_NIT
131	PF_NIT
132#else
133	PF_NO_LOCAL_MAPPING
134#endif
135	},
136	{ BSM_PF_802,
137#ifdef PF_802
138	PF_802
139#else
140	PF_NO_LOCAL_MAPPING
141#endif
142	},
143	{ BSM_PF_OSI,
144#ifdef PF_OSI
145	PF_OSI
146#else
147	PF_NO_LOCAL_MAPPING
148#endif
149	},
150	{ BSM_PF_X25,
151#ifdef PF_X25
152	PF_X25
153#else
154	PF_NO_LOCAL_MAPPING
155#endif
156	},
157	{ BSM_PF_OSINET,
158#ifdef PF_OSINET
159	PF_OSINET
160#else
161	PF_NO_LOCAL_MAPPING
162#endif
163	},
164	{ BSM_PF_GOSIP,
165#ifdef PF_GOSIP
166	PF_GOSIP
167#else
168	PF_NO_LOCAL_MAPPING
169#endif
170	},
171	{ BSM_PF_IPX, PF_IPX },
172	{ BSM_PF_ROUTE, PF_ROUTE },
173	{ BSM_PF_LINK,
174#ifdef PF_LINK
175	PF_LINK
176#else
177	PF_NO_LOCAL_MAPPING
178#endif
179	},
180	{ BSM_PF_INET6, PF_INET6 },
181	{ BSM_PF_KEY, PF_KEY },
182	{ BSM_PF_NCA,
183#ifdef PF_NCA
184	PF_NCA
185#else
186	PF_NO_LOCAL_MAPPING
187#endif
188	},
189	{ BSM_PF_POLICY,
190#ifdef PF_POLICY
191	PF_POLICY
192#else
193	PF_NO_LOCAL_MAPPING
194#endif
195	},
196	{ BSM_PF_INET_OFFLOAD,
197#ifdef PF_INET_OFFLOAD
198	PF_INET_OFFLOAD
199#else
200	PF_NO_LOCAL_MAPPING
201#endif
202	},
203	{ BSM_PF_NETBIOS,
204#ifdef PF_NETBIOS
205	PF_NETBIOS
206#else
207	PF_NO_LOCAL_MAPPING
208#endif
209	},
210	{ BSM_PF_ISO,
211#ifdef PF_ISO
212	PF_ISO
213#else
214	PF_NO_LOCAL_MAPPING
215#endif
216	},
217	{ BSM_PF_XTP,
218#ifdef PF_XTP
219	PF_XTP
220#else
221	PF_NO_LOCAL_MAPPING
222#endif
223	},
224	{ BSM_PF_COIP,
225#ifdef PF_COIP
226	PF_COIP
227#else
228	PF_NO_LOCAL_MAPPING
229#endif
230	},
231	{ BSM_PF_CNT,
232#ifdef PF_CNT
233	PF_CNT
234#else
235	PF_NO_LOCAL_MAPPING
236#endif
237	},
238	{ BSM_PF_RTIP,
239#ifdef PF_RTIP
240	PF_RTIP
241#else
242	PF_NO_LOCAL_MAPPING
243#endif
244	},
245	{ BSM_PF_SIP,
246#ifdef PF_SIP
247	PF_SIP
248#else
249	PF_NO_LOCAL_MAPPING
250#endif
251	},
252	{ BSM_PF_PIP,
253#ifdef PF_PIP
254	PF_PIP
255#else
256	PF_NO_LOCAL_MAPPING
257#endif
258	},
259	{ BSM_PF_ISDN,
260#ifdef PF_ISDN
261	PF_ISDN
262#else
263	PF_NO_LOCAL_MAPPING
264#endif
265	},
266	{ BSM_PF_E164,
267#ifdef PF_E164
268	PF_E164
269#else
270	PF_NO_LOCAL_MAPPING
271#endif
272	},
273	{ BSM_PF_NATM,
274#ifdef PF_NATM
275	PF_NATM
276#else
277	PF_NO_LOCAL_MAPPING
278#endif
279	},
280	{ BSM_PF_ATM,
281#ifdef PF_ATM
282	PF_ATM
283#else
284	PF_NO_LOCAL_MAPPING
285#endif
286	},
287	{ BSM_PF_NETGRAPH,
288#ifdef PF_NETGRAPH
289	PF_NETGRAPH
290#else
291	PF_NO_LOCAL_MAPPING
292#endif
293	},
294	{ BSM_PF_SLOW,
295#ifdef PF_SLOW
296	PF_SLOW
297#else
298	PF_NO_LOCAL_MAPPING
299#endif
300	},
301	{ BSM_PF_SCLUSTER,
302#ifdef PF_SCLUSTER
303	PF_SCLUSTER
304#else
305	PF_NO_LOCAL_MAPPING
306#endif
307	},
308	{ BSM_PF_ARP,
309#ifdef PF_ARP
310	PF_ARP
311#else
312	PF_NO_LOCAL_MAPPING
313#endif
314	},
315	{ BSM_PF_BLUETOOTH,
316#ifdef PF_BLUETOOTH
317	PF_BLUETOOTH
318#else
319	PF_NO_LOCAL_MAPPING
320#endif
321	},
322	{ BSM_PF_AX25,
323#ifdef PF_AX25
324	PF_AX25
325#else
326	PF_NO_LOCAL_MAPPING
327#endif
328	},
329	{ BSM_PF_ROSE,
330#ifdef PF_ROSE
331	PF_ROSE
332#else
333	PF_NO_LOCAL_MAPPING
334#endif
335	},
336	{ BSM_PF_NETBEUI,
337#ifdef PF_NETBEUI
338	PF_NETBEUI
339#else
340	PF_NO_LOCAL_MAPPING
341#endif
342	},
343	{ BSM_PF_SECURITY,
344#ifdef PF_SECURITY
345	PF_SECURITY
346#else
347	PF_NO_LOCAL_MAPPING
348#endif
349	},
350	{ BSM_PF_PACKET,
351#ifdef PF_PACKET
352	PF_PACKET
353#else
354	PF_NO_LOCAL_MAPPING
355#endif
356	},
357	{ BSM_PF_ASH,
358#ifdef PF_ASH
359	PF_ASH
360#else
361	PF_NO_LOCAL_MAPPING
362#endif
363	},
364	{ BSM_PF_ECONET,
365#ifdef PF_ECONET
366	PF_ECONET
367#else
368	PF_NO_LOCAL_MAPPING
369#endif
370	},
371	{ BSM_PF_ATMSVC,
372#ifdef PF_ATMSVC
373	PF_ATMSVC
374#else
375	PF_NO_LOCAL_MAPPING
376#endif
377	},
378	{ BSM_PF_IRDA,
379#ifdef PF_IRDA
380	PF_IRDA
381#else
382	PF_NO_LOCAL_MAPPING
383#endif
384	},
385	{ BSM_PF_PPPOX,
386#ifdef PF_PPPOX
387	PF_PPPOX
388#else
389	PF_NO_LOCAL_MAPPING
390#endif
391	},
392	{ BSM_PF_WANPIPE,
393#ifdef PF_WANPIPE
394	PF_WANPIPE
395#else
396	PF_NO_LOCAL_MAPPING
397#endif
398	},
399	{ BSM_PF_LLC,
400#ifdef PF_LLC
401	PF_LLC
402#else
403	PF_NO_LOCAL_MAPPING
404#endif
405	},
406	{ BSM_PF_CAN,
407#ifdef PF_CAN
408	PF_CAN
409#else
410	PF_NO_LOCAL_MAPPING
411#endif
412	},
413	{ BSM_PF_TIPC,
414#ifdef PF_TIPC
415	PF_TIPC
416#else
417	PF_NO_LOCAL_MAPPING
418#endif
419	},
420	{ BSM_PF_IUCV,
421#ifdef PF_IUCV
422	PF_IUCV
423#else
424	PF_NO_LOCAL_MAPPING
425#endif
426	},
427	{ BSM_PF_RXRPC,
428#ifdef PF_RXRPC
429	PF_RXRPC
430#else
431	PF_NO_LOCAL_MAPPING
432#endif
433	},
434	{ BSM_PF_PHONET,
435#ifdef PF_PHONET
436	PF_PHONET
437#else
438	PF_NO_LOCAL_MAPPING
439#endif
440	},
441};
442static const int bsm_domains_count = sizeof(bsm_domains) /
443	    sizeof(bsm_domains[0]);
444
445static const struct bsm_domain *
446bsm_lookup_local_domain(int local_domain)
447{
448	int i;
449
450	for (i = 0; i < bsm_domains_count; i++) {
451		if (bsm_domains[i].bd_local_domain == local_domain)
452			return (&bsm_domains[i]);
453	}
454	return (NULL);
455}
456
457u_short
458au_domain_to_bsm(int local_domain)
459{
460	const struct bsm_domain *bstp;
461
462	bstp = bsm_lookup_local_domain(local_domain);
463	if (bstp == NULL)
464		return (BSM_PF_UNKNOWN);
465	return (bstp->bd_bsm_domain);
466}
467
468static const struct bsm_domain *
469bsm_lookup_bsm_domain(u_short bsm_domain)
470{
471	int i;
472
473	for (i = 0; i < bsm_domains_count; i++) {
474		if (bsm_domains[i].bd_bsm_domain == bsm_domain)
475			return (&bsm_domains[i]);
476	}
477	return (NULL);
478}
479
480int
481au_bsm_to_domain(u_short bsm_domain, int *local_domainp)
482{
483	const struct bsm_domain *bstp;
484
485	bstp = bsm_lookup_bsm_domain(bsm_domain);
486	if (bstp == NULL || bstp->bd_local_domain)
487		return (-1);
488	*local_domainp = bstp->bd_local_domain;
489	return (0);
490}
491