2 * Copyright (c) 2006 "David Kirchner" <dpk@dpk.net>. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD$");
29 #define L2CAP_SOCKET_CHECKED
31 #include <sys/types.h>
33 #include <sys/capsicum.h>
34 #include <sys/extattr.h>
35 #include <sys/linker.h>
37 #include <sys/mount.h>
38 #include <sys/procctl.h>
39 #include <sys/ptrace.h>
40 #include <sys/reboot.h>
41 #include <sys/resource.h>
42 #include <sys/rtprio.h>
45 #include <sys/socket.h>
49 #include <netinet/in.h>
50 #include <netinet/tcp.h>
51 #include <netinet/udp.h>
52 #include <nfsserver/nfs.h>
53 #include <ufs/ufs/quota.h>
54 #include <vm/vm_param.h>
62 #include <sysdecode.h>
64 #include <sys/bitstring.h>
65 #include <netgraph/bluetooth/include/ng_hci.h>
66 #include <netgraph/bluetooth/include/ng_l2cap.h>
67 #include <netgraph/bluetooth/include/ng_btsocket.h>
70 * This is taken from the xlat tables originally in truss which were
71 * in turn taken from strace.
78 #define X(a) { a, #a },
79 #define XEND { 0, NULL }
81 #define TABLE_START(n) static struct name_table n[] = {
83 #define TABLE_END XEND };
92 * These are simple support macros. print_or utilizes a variable
93 * defined in the calling function to track whether or not it should
94 * print a logical-OR character ('|') before a string. if_print_or
95 * simply handles the necessary "if" statement used in many lines
98 #define print_or(fp,str,orflag) do { \
99 if (orflag) fputc(fp, '|'); else orflag = true; \
100 fprintf(fp, str); } \
102 #define if_print_or(fp,i,flag,orflag) do { \
103 if ((i & flag) == flag) \
104 print_or(fp,#flag,orflag); } \
108 lookup_value(struct name_table *table, uintmax_t val)
111 for (; table->str != NULL; table++)
112 if (table->val == val)
118 * Used when the value maps to a bitmask of #definition values in the
119 * table. This is a helper routine which outputs a symbolic mask of
120 * matched masks. Multiple masks are separated by a pipe ('|').
121 * The value is modified on return to only hold unmatched bits.
124 print_mask_part(FILE *fp, struct name_table *table, uintmax_t *valp,
130 for (; table->str != NULL; table++) {
131 if ((table->val & rem) == table->val) {
133 * Only print a zero mask if the raw value is
136 if (table->val == 0 && *valp != 0)
138 fprintf(fp, "%s%s", *printed ? "|" : "", table->str);
148 * Used when the value maps to a bitmask of #definition values in the
149 * table. The return value is true if something was printed. If
150 * rem is not NULL, *rem holds any bits not decoded if something was
151 * printed. If nothing was printed and rem is not NULL, *rem holds
152 * the original value.
155 print_mask_int(FILE *fp, struct name_table *table, int ival, int *rem)
161 val = (unsigned)ival;
162 print_mask_part(fp, table, &val, &printed);
169 * Used for a mask of optional flags where a value of 0 is valid.
172 print_mask_0(FILE *fp, struct name_table *table, int val, int *rem)
181 return (print_mask_int(fp, table, val, rem));
185 * Like print_mask_0 but for a unsigned long instead of an int.
188 print_mask_0ul(FILE *fp, struct name_table *table, u_long lval, u_long *rem)
202 print_mask_part(fp, table, &val, &printed);
209 print_integer(FILE *fp, int val, int base)
214 fprintf(fp, "0%o", val);
217 fprintf(fp, "%d", val);
220 fprintf(fp, "0x%x", val);
223 abort2("bad base", 0, NULL);
229 print_value(FILE *fp, struct name_table *table, uintmax_t val)
233 str = lookup_value(table, val);
242 sysdecode_atfd(int fd)
250 static struct name_table semctlops[] = {
251 X(GETNCNT) X(GETPID) X(GETVAL) X(GETALL) X(GETZCNT) X(SETVAL) X(SETALL)
252 X(IPC_RMID) X(IPC_SET) X(IPC_STAT) XEND
256 sysdecode_semctl_cmd(int cmd)
259 return (lookup_value(semctlops, cmd));
262 static struct name_table shmctlops[] = {
263 X(IPC_RMID) X(IPC_SET) X(IPC_STAT) XEND
267 sysdecode_shmctl_cmd(int cmd)
270 return (lookup_value(shmctlops, cmd));
274 sysdecode_msgctl_cmd(int cmd)
277 return (sysdecode_shmctl_cmd(cmd));
280 static struct name_table semgetflags[] = {
281 X(IPC_CREAT) X(IPC_EXCL) X(SEM_R) X(SEM_A) X((SEM_R>>3)) X((SEM_A>>3))
282 X((SEM_R>>6)) X((SEM_A>>6)) XEND
286 sysdecode_semget_flags(FILE *fp, int flag, int *rem)
289 return (print_mask_int(fp, semgetflags, flag, rem));
292 static struct name_table idtypes[] = {
293 X(P_PID) X(P_PPID) X(P_PGID) X(P_SID) X(P_CID) X(P_UID) X(P_GID)
294 X(P_ALL) X(P_LWPID) X(P_TASKID) X(P_PROJID) X(P_POOLID) X(P_JAILID)
295 X(P_CTID) X(P_CPUID) X(P_PSETID) XEND
298 /* XXX: idtype is really an idtype_t */
300 sysdecode_idtype(int idtype)
303 return (lookup_value(idtypes, idtype));
307 * [g|s]etsockopt's level argument can either be SOL_SOCKET or a
308 * protocol-specific value.
311 sysdecode_sockopt_level(int level)
315 if (level == SOL_SOCKET)
316 return ("SOL_SOCKET");
318 /* SOL_* constants for Bluetooth sockets. */
319 str = lookup_value(ngbtsolevel, level);
324 * IP and Infiniband sockets use IP protocols as levels. Not all
325 * protocols are valid but it is simpler to just allow all of them.
327 * XXX: IPPROTO_IP == 0, but UNIX domain sockets use a level of 0
328 * for private options.
330 str = sysdecode_ipproto(level);
338 sysdecode_vmprot(FILE *fp, int type, int *rem)
341 return (print_mask_int(fp, vmprot, type, rem));
344 static struct name_table sockflags[] = {
345 X(SOCK_CLOEXEC) X(SOCK_NONBLOCK) XEND
349 sysdecode_socket_type(FILE *fp, int type, int *rem)
355 str = lookup_value(socktype, type & ~(SOCK_CLOEXEC | SOCK_NONBLOCK));
361 *rem = type & ~(SOCK_CLOEXEC | SOCK_NONBLOCK);
364 val = type & (SOCK_CLOEXEC | SOCK_NONBLOCK);
365 print_mask_part(fp, sockflags, &val, &printed);
370 sysdecode_access_mode(FILE *fp, int mode, int *rem)
373 return (print_mask_int(fp, accessmode, mode, rem));
376 /* XXX: 'type' is really an acl_type_t. */
378 sysdecode_acltype(int type)
381 return (lookup_value(acltype, type));
385 sysdecode_cap_fcntlrights(FILE *fp, uint32_t rights, uint32_t *rem)
388 return (print_mask_int(fp, capfcntl, rights, rem));
392 sysdecode_extattrnamespace(int namespace)
395 return (lookup_value(extattrns, namespace));
399 sysdecode_fadvice(int advice)
402 return (lookup_value(fadvisebehav, advice));
406 sysdecode_open_flags(FILE *fp, int flags, int *rem)
412 mode = flags & O_ACCMODE;
416 if (flags & O_EXEC) {
420 fputs("O_RDONLY", fp);
425 fputs("O_WRONLY", fp);
437 val = (unsigned)flags;
438 print_mask_part(fp, openflags, &val, &printed);
445 sysdecode_fcntl_fileflags(FILE *fp, int flags, int *rem)
451 * The file flags used with F_GETFL/F_SETFL mostly match the
452 * flags passed to open(2). However, a few open-only flag
453 * bits have been repurposed for fcntl-only flags.
455 oflags = flags & ~(O_NOFOLLOW | FRDAHEAD);
456 printed = sysdecode_open_flags(fp, oflags, rem);
457 if (flags & O_NOFOLLOW) {
458 fprintf(fp, "%sFPOIXSHM", printed ? "|" : "");
461 if (flags & FRDAHEAD) {
462 fprintf(fp, "%sFRDAHEAD", printed ? "|" : "");
469 sysdecode_flock_operation(FILE *fp, int operation, int *rem)
472 return (print_mask_int(fp, flockops, operation, rem));
476 sysdecode_getfsstat_flags(FILE *fp, int flags, int *rem)
479 return (print_mask_int(fp, getfsstatflags, flags, rem));
483 sysdecode_kldsym_cmd(int cmd)
486 return (lookup_value(kldsymcmd, cmd));
490 sysdecode_kldunload_flags(int flags)
493 return (lookup_value(kldunloadfflags, flags));
497 sysdecode_lio_listio_mode(int mode)
500 return (lookup_value(lio_listiomodes, mode));
504 sysdecode_madvice(int advice)
507 return (lookup_value(madvisebehav, advice));
511 sysdecode_minherit_inherit(int inherit)
514 return (lookup_value(minheritflags, inherit));
518 sysdecode_mlockall_flags(FILE *fp, int flags, int *rem)
521 return (print_mask_int(fp, mlockallflags, flags, rem));
525 sysdecode_mmap_prot(FILE *fp, int prot, int *rem)
528 return (print_mask_int(fp, mmapprot, prot, rem));
532 sysdecode_fileflags(FILE *fp, fflags_t flags, fflags_t *rem)
535 return (print_mask_0(fp, fileflags, flags, rem));
539 sysdecode_filemode(FILE *fp, int mode, int *rem)
542 return (print_mask_0(fp, filemode, mode, rem));
546 sysdecode_mount_flags(FILE *fp, int flags, int *rem)
549 return (print_mask_int(fp, mountflags, flags, rem));
553 sysdecode_msync_flags(FILE *fp, int flags, int *rem)
556 return (print_mask_int(fp, msyncflags, flags, rem));
560 sysdecode_nfssvc_flags(int flags)
563 return (lookup_value(nfssvcflags, flags));
566 static struct name_table pipe2flags[] = {
567 X(O_CLOEXEC) X(O_NONBLOCK) XEND
571 sysdecode_pipe2_flags(FILE *fp, int flags, int *rem)
574 return (print_mask_0(fp, pipe2flags, flags, rem));
578 sysdecode_prio_which(int which)
581 return (lookup_value(prio, which));
585 sysdecode_procctl_cmd(int cmd)
588 return (lookup_value(procctlcmd, cmd));
592 sysdecode_ptrace_request(int request)
595 return (lookup_value(ptraceop, request));
598 static struct name_table quotatypes[] = {
599 X(GRPQUOTA) X(USRQUOTA) XEND
603 sysdecode_quotactl_cmd(FILE *fp, int cmd)
605 const char *primary, *type;
607 primary = lookup_value(quotactlcmds, cmd >> SUBCMDSHIFT);
610 fprintf(fp, "QCMD(%s,", primary);
611 type = lookup_value(quotatypes, cmd & SUBCMDMASK);
613 fprintf(fp, "%s", type);
615 fprintf(fp, "%#x", cmd & SUBCMDMASK);
621 sysdecode_reboot_howto(FILE *fp, int howto, int *rem)
624 return (print_mask_int(fp, rebootopt, howto, rem));
628 sysdecode_rfork_flags(FILE *fp, int flags, int *rem)
631 return (print_mask_int(fp, rforkflags, flags, rem));
635 sysdecode_rlimit(int resource)
638 return (lookup_value(rlimit, resource));
642 sysdecode_scheduler_policy(int policy)
645 return (lookup_value(schedpolicy, policy));
649 sysdecode_sendfile_flags(FILE *fp, int flags, int *rem)
652 return (print_mask_int(fp, sendfileflags, flags, rem));
656 sysdecode_shmat_flags(FILE *fp, int flags, int *rem)
659 return (print_mask_int(fp, shmatflags, flags, rem));
663 sysdecode_shutdown_how(int how)
666 return (lookup_value(shutdownhow, how));
670 sysdecode_sigbus_code(int si_code)
673 return (lookup_value(sigbuscode, si_code));
677 sysdecode_sigchld_code(int si_code)
680 return (lookup_value(sigchldcode, si_code));
684 sysdecode_sigfpe_code(int si_code)
687 return (lookup_value(sigfpecode, si_code));
691 sysdecode_sigill_code(int si_code)
694 return (lookup_value(sigillcode, si_code));
698 sysdecode_sigsegv_code(int si_code)
701 return (lookup_value(sigsegvcode, si_code));
705 sysdecode_sigtrap_code(int si_code)
708 return (lookup_value(sigtrapcode, si_code));
712 sysdecode_sigprocmask_how(int how)
715 return (lookup_value(sigprocmaskhow, how));
719 sysdecode_socketdomain(int domain)
722 return (lookup_value(sockdomain, domain));
726 sysdecode_sockaddr_family(int sa_family)
729 return (lookup_value(sockfamily, sa_family));
733 sysdecode_ipproto(int protocol)
736 return (lookup_value(sockipproto, protocol));
740 sysdecode_sockopt_name(int level, int optname)
743 if (level == SOL_SOCKET)
744 return (lookup_value(sockopt, optname));
745 if (level == IPPROTO_IP)
746 /* XXX: UNIX domain socket options use a level of 0 also. */
747 return (lookup_value(sockoptip, optname));
748 if (level == IPPROTO_TCP)
749 return (lookup_value(sockopttcp, optname));
750 if (level == IPPROTO_UDP)
751 return (lookup_value(sockoptudp, optname));
756 sysdecode_thr_create_flags(FILE *fp, int flags, int *rem)
759 return (print_mask_int(fp, thrcreateflags, flags, rem));
763 sysdecode_umtx_op(int op)
766 return (lookup_value(umtxop, op));
770 sysdecode_vmresult(int result)
773 return (lookup_value(vmresult, result));
777 sysdecode_wait4_options(FILE *fp, int options, int *rem)
782 /* A flags value of 0 is normal. */
791 * These flags are implicit and aren't valid flags for wait4()
792 * directly (though they don't fail with EINVAL).
794 opt6 = options & (WEXITED | WTRAPPED);
796 printed = print_mask_int(fp, wait6opt, options, rem);
803 sysdecode_wait6_options(FILE *fp, int options, int *rem)
806 return (print_mask_int(fp, wait6opt, options, rem));
810 sysdecode_whence(int whence)
813 return (lookup_value(seekwhence, whence));
817 sysdecode_fcntl_cmd(int cmd)
820 return (lookup_value(fcntlcmd, cmd));
823 static struct name_table fcntl_fd_arg[] = {
824 X(FD_CLOEXEC) X(0) XEND
828 sysdecode_fcntl_arg_p(int cmd)
842 sysdecode_fcntl_arg(FILE *fp, int cmd, uintptr_t arg, int base)
848 if (!print_value(fp, fcntl_fd_arg, arg))
849 print_integer(fp, arg, base);
852 if (!sysdecode_fcntl_fileflags(fp, arg, &rem))
853 fprintf(fp, "%#x", rem);
855 fprintf(fp, "|%#x", rem);
860 fprintf(fp, "%p", (void *)arg);
863 print_integer(fp, arg, base);
869 sysdecode_mmap_flags(FILE *fp, int flags, int *rem)
876 * MAP_ALIGNED can't be handled directly by print_mask_int().
877 * MAP_32BIT is also problematic since it isn't defined for
881 align = flags & MAP_ALIGNMENT_MASK;
882 val = (unsigned)flags & ~MAP_ALIGNMENT_MASK;
883 print_mask_part(fp, mmapflags, &val, &printed);
885 if (val & MAP_32BIT) {
886 fprintf(fp, "%sMAP_32BIT", printed ? "|" : "");
894 if (align == MAP_ALIGNED_SUPER)
895 fputs("MAP_ALIGNED_SUPER", fp);
897 fprintf(fp, "MAP_ALIGNED(%d)",
898 align >> MAP_ALIGNMENT_SHIFT);
907 sysdecode_rtprio_function(int function)
910 return (lookup_value(rtpriofuncs, function));
914 sysdecode_msg_flags(FILE *fp, int flags, int *rem)
917 return (print_mask_0(fp, msgflags, flags, rem));
921 sysdecode_sigcode(int sig, int si_code)
925 str = lookup_value(sigcode, si_code);
931 return (sysdecode_sigill_code(si_code));
933 return (sysdecode_sigbus_code(si_code));
935 return (sysdecode_sigsegv_code(si_code));
937 return (sysdecode_sigfpe_code(si_code));
939 return (sysdecode_sigtrap_code(si_code));
941 return (sysdecode_sigchld_code(si_code));
948 sysdecode_umtx_cvwait_flags(FILE *fp, u_long flags, u_long *rem)
951 return (print_mask_0ul(fp, umtxcvwaitflags, flags, rem));
955 sysdecode_umtx_rwlock_flags(FILE *fp, u_long flags, u_long *rem)
958 return (print_mask_0ul(fp, umtxrwlockflags, flags, rem));
961 /* XXX: This should be in <sys/capsicum.h> */
962 #define CAPMASK(right) ((right) && (((uint64_t)1 << 57) - 1))
965 sysdecode_cap_rights(FILE *fp, cap_rights_t *rightsp)
967 struct name_table *t;
972 for (t = caprights; t->str != NULL; t++) {
973 idx = ffs(CAPIDXBIT(t->val)) - 1;
974 if (CAPARSIZE(rightsp) < idx)
976 if ((rightsp->cr_rights[CAPIDXBIT(t->val)] & CAPMASK(t->val)) ==
978 fprintf(fp, "%s%s", comma ? "," : "", t->str);