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 <machine/sysarch.h>
50 #include <netinet/in.h>
51 #include <netinet/sctp.h>
52 #include <netinet/tcp.h>
53 #include <netinet/udp.h>
54 #include <netinet/udplite.h>
55 #include <nfsserver/nfs.h>
56 #include <ufs/ufs/quota.h>
58 #include <vm/vm_param.h>
66 #include <sysdecode.h>
68 #include <sys/bitstring.h>
69 #include <netgraph/bluetooth/include/ng_hci.h>
70 #include <netgraph/bluetooth/include/ng_l2cap.h>
71 #include <netgraph/bluetooth/include/ng_btsocket.h>
74 * This is taken from the xlat tables originally in truss which were
75 * in turn taken from strace.
82 #define X(a) { a, #a },
83 #define XEND { 0, NULL }
85 #define TABLE_START(n) static struct name_table n[] = {
87 #define TABLE_END XEND };
96 * These are simple support macros. print_or utilizes a variable
97 * defined in the calling function to track whether or not it should
98 * print a logical-OR character ('|') before a string. if_print_or
99 * simply handles the necessary "if" statement used in many lines
102 #define print_or(fp,str,orflag) do { \
103 if (orflag) fputc(fp, '|'); else orflag = true; \
104 fprintf(fp, str); } \
106 #define if_print_or(fp,i,flag,orflag) do { \
107 if ((i & flag) == flag) \
108 print_or(fp,#flag,orflag); } \
112 lookup_value(struct name_table *table, uintmax_t val)
115 for (; table->str != NULL; table++)
116 if (table->val == val)
122 * Used when the value maps to a bitmask of #definition values in the
123 * table. This is a helper routine which outputs a symbolic mask of
124 * matched masks. Multiple masks are separated by a pipe ('|').
125 * The value is modified on return to only hold unmatched bits.
128 print_mask_part(FILE *fp, struct name_table *table, uintmax_t *valp,
134 for (; table->str != NULL; table++) {
135 if ((table->val & rem) == table->val) {
137 * Only print a zero mask if the raw value is
140 if (table->val == 0 && *valp != 0)
142 fprintf(fp, "%s%s", *printed ? "|" : "", table->str);
152 * Used when the value maps to a bitmask of #definition values in the
153 * table. The return value is true if something was printed. If
154 * rem is not NULL, *rem holds any bits not decoded if something was
155 * printed. If nothing was printed and rem is not NULL, *rem holds
156 * the original value.
159 print_mask_int(FILE *fp, struct name_table *table, int ival, int *rem)
165 val = (unsigned)ival;
166 print_mask_part(fp, table, &val, &printed);
173 * Used for a mask of optional flags where a value of 0 is valid.
176 print_mask_0(FILE *fp, struct name_table *table, int val, int *rem)
185 return (print_mask_int(fp, table, val, rem));
189 * Like print_mask_0 but for a unsigned long instead of an int.
192 print_mask_0ul(FILE *fp, struct name_table *table, u_long lval, u_long *rem)
206 print_mask_part(fp, table, &val, &printed);
213 print_integer(FILE *fp, int val, int base)
218 fprintf(fp, "0%o", val);
221 fprintf(fp, "%d", val);
224 fprintf(fp, "0x%x", val);
227 abort2("bad base", 0, NULL);
233 print_value(FILE *fp, struct name_table *table, uintmax_t val)
237 str = lookup_value(table, val);
246 sysdecode_atfd(int fd)
255 sysdecode_atflags(FILE *fp, int flag, int *rem)
258 return (print_mask_int(fp, atflags, flag, rem));
261 static struct name_table semctlops[] = {
262 X(GETNCNT) X(GETPID) X(GETVAL) X(GETALL) X(GETZCNT) X(SETVAL) X(SETALL)
263 X(IPC_RMID) X(IPC_SET) X(IPC_STAT) XEND
267 sysdecode_semctl_cmd(int cmd)
270 return (lookup_value(semctlops, cmd));
273 static struct name_table shmctlops[] = {
274 X(IPC_RMID) X(IPC_SET) X(IPC_STAT) XEND
278 sysdecode_shmctl_cmd(int cmd)
281 return (lookup_value(shmctlops, cmd));
285 sysdecode_msgctl_cmd(int cmd)
288 return (sysdecode_shmctl_cmd(cmd));
291 static struct name_table semgetflags[] = {
292 X(IPC_CREAT) X(IPC_EXCL) X(SEM_R) X(SEM_A) X((SEM_R>>3)) X((SEM_A>>3))
293 X((SEM_R>>6)) X((SEM_A>>6)) XEND
297 sysdecode_semget_flags(FILE *fp, int flag, int *rem)
300 return (print_mask_int(fp, semgetflags, flag, rem));
303 static struct name_table idtypes[] = {
304 X(P_PID) X(P_PPID) X(P_PGID) X(P_SID) X(P_CID) X(P_UID) X(P_GID)
305 X(P_ALL) X(P_LWPID) X(P_TASKID) X(P_PROJID) X(P_POOLID) X(P_JAILID)
306 X(P_CTID) X(P_CPUID) X(P_PSETID) XEND
309 /* XXX: idtype is really an idtype_t */
311 sysdecode_idtype(int idtype)
314 return (lookup_value(idtypes, idtype));
318 * [g|s]etsockopt's level argument can either be SOL_SOCKET or a
319 * protocol-specific value.
322 sysdecode_sockopt_level(int level)
326 if (level == SOL_SOCKET)
327 return ("SOL_SOCKET");
329 /* SOL_* constants for Bluetooth sockets. */
330 str = lookup_value(ngbtsolevel, level);
335 * IP and Infiniband sockets use IP protocols as levels. Not all
336 * protocols are valid but it is simpler to just allow all of them.
338 * XXX: IPPROTO_IP == 0, but UNIX domain sockets use a level of 0
339 * for private options.
341 str = sysdecode_ipproto(level);
349 sysdecode_vmprot(FILE *fp, int type, int *rem)
352 return (print_mask_int(fp, vmprot, type, rem));
355 static struct name_table sockflags[] = {
356 X(SOCK_CLOEXEC) X(SOCK_NONBLOCK) XEND
360 sysdecode_socket_type(FILE *fp, int type, int *rem)
366 str = lookup_value(socktype, type & ~(SOCK_CLOEXEC | SOCK_NONBLOCK));
372 *rem = type & ~(SOCK_CLOEXEC | SOCK_NONBLOCK);
375 val = type & (SOCK_CLOEXEC | SOCK_NONBLOCK);
376 print_mask_part(fp, sockflags, &val, &printed);
381 sysdecode_access_mode(FILE *fp, int mode, int *rem)
384 return (print_mask_int(fp, accessmode, mode, rem));
387 /* XXX: 'type' is really an acl_type_t. */
389 sysdecode_acltype(int type)
392 return (lookup_value(acltype, type));
396 sysdecode_cap_fcntlrights(FILE *fp, uint32_t rights, uint32_t *rem)
399 return (print_mask_int(fp, capfcntl, rights, rem));
403 sysdecode_extattrnamespace(int namespace)
406 return (lookup_value(extattrns, namespace));
410 sysdecode_fadvice(int advice)
413 return (lookup_value(fadvisebehav, advice));
417 sysdecode_open_flags(FILE *fp, int flags, int *rem)
423 mode = flags & O_ACCMODE;
427 if (flags & O_EXEC) {
431 fputs("O_RDONLY", fp);
436 fputs("O_WRONLY", fp);
448 val = (unsigned)flags;
449 print_mask_part(fp, openflags, &val, &printed);
456 sysdecode_fcntl_fileflags(FILE *fp, int flags, int *rem)
462 * The file flags used with F_GETFL/F_SETFL mostly match the
463 * flags passed to open(2). However, a few open-only flag
464 * bits have been repurposed for fcntl-only flags.
466 oflags = flags & ~(O_NOFOLLOW | FRDAHEAD);
467 printed = sysdecode_open_flags(fp, oflags, rem);
468 if (flags & O_NOFOLLOW) {
469 fprintf(fp, "%sFPOIXSHM", printed ? "|" : "");
472 if (flags & FRDAHEAD) {
473 fprintf(fp, "%sFRDAHEAD", printed ? "|" : "");
480 sysdecode_flock_operation(FILE *fp, int operation, int *rem)
483 return (print_mask_int(fp, flockops, operation, rem));
486 static struct name_table getfsstatmode[] = {
487 X(MNT_WAIT) X(MNT_NOWAIT) XEND
491 sysdecode_getfsstat_mode(int mode)
494 return (lookup_value(getfsstatmode, mode));
498 sysdecode_getrusage_who(int who)
501 return (lookup_value(rusage, who));
505 sysdecode_kldsym_cmd(int cmd)
508 return (lookup_value(kldsymcmd, cmd));
512 sysdecode_kldunload_flags(int flags)
515 return (lookup_value(kldunloadfflags, flags));
519 sysdecode_lio_listio_mode(int mode)
522 return (lookup_value(lio_listiomodes, mode));
526 sysdecode_madvice(int advice)
529 return (lookup_value(madvisebehav, advice));
533 sysdecode_minherit_inherit(int inherit)
536 return (lookup_value(minheritflags, inherit));
540 sysdecode_mlockall_flags(FILE *fp, int flags, int *rem)
543 return (print_mask_int(fp, mlockallflags, flags, rem));
547 sysdecode_mmap_prot(FILE *fp, int prot, int *rem)
550 return (print_mask_int(fp, mmapprot, prot, rem));
554 sysdecode_fileflags(FILE *fp, fflags_t flags, fflags_t *rem)
557 return (print_mask_0(fp, fileflags, flags, rem));
561 sysdecode_filemode(FILE *fp, int mode, int *rem)
564 return (print_mask_0(fp, filemode, mode, rem));
568 sysdecode_mount_flags(FILE *fp, int flags, int *rem)
571 return (print_mask_int(fp, mountflags, flags, rem));
575 sysdecode_msync_flags(FILE *fp, int flags, int *rem)
578 return (print_mask_int(fp, msyncflags, flags, rem));
582 sysdecode_nfssvc_flags(int flags)
585 return (lookup_value(nfssvcflags, flags));
588 static struct name_table pipe2flags[] = {
589 X(O_CLOEXEC) X(O_NONBLOCK) XEND
593 sysdecode_pipe2_flags(FILE *fp, int flags, int *rem)
596 return (print_mask_0(fp, pipe2flags, flags, rem));
600 sysdecode_prio_which(int which)
603 return (lookup_value(prio, which));
607 sysdecode_procctl_cmd(int cmd)
610 return (lookup_value(procctlcmd, cmd));
614 sysdecode_ptrace_request(int request)
617 return (lookup_value(ptraceop, request));
620 static struct name_table quotatypes[] = {
621 X(GRPQUOTA) X(USRQUOTA) XEND
625 sysdecode_quotactl_cmd(FILE *fp, int cmd)
627 const char *primary, *type;
629 primary = lookup_value(quotactlcmds, cmd >> SUBCMDSHIFT);
632 fprintf(fp, "QCMD(%s,", primary);
633 type = lookup_value(quotatypes, cmd & SUBCMDMASK);
635 fprintf(fp, "%s", type);
637 fprintf(fp, "%#x", cmd & SUBCMDMASK);
643 sysdecode_reboot_howto(FILE *fp, int howto, int *rem)
648 * RB_AUTOBOOT is special in that its value is zero, but it is
649 * also an implied argument if a different operation is not
650 * requested via RB_HALT, RB_POWEROFF, or RB_REROOT.
652 if (howto != 0 && (howto & (RB_HALT | RB_POWEROFF | RB_REROOT)) == 0) {
653 fputs("RB_AUTOBOOT|", fp);
657 return (print_mask_int(fp, rebootopt, howto, rem) || printed);
661 sysdecode_rfork_flags(FILE *fp, int flags, int *rem)
664 return (print_mask_int(fp, rforkflags, flags, rem));
668 sysdecode_rlimit(int resource)
671 return (lookup_value(rlimit, resource));
675 sysdecode_scheduler_policy(int policy)
678 return (lookup_value(schedpolicy, policy));
682 sysdecode_sendfile_flags(FILE *fp, int flags, int *rem)
685 return (print_mask_int(fp, sendfileflags, flags, rem));
689 sysdecode_shmat_flags(FILE *fp, int flags, int *rem)
692 return (print_mask_int(fp, shmatflags, flags, rem));
696 sysdecode_shutdown_how(int how)
699 return (lookup_value(shutdownhow, how));
703 sysdecode_sigbus_code(int si_code)
706 return (lookup_value(sigbuscode, si_code));
710 sysdecode_sigchld_code(int si_code)
713 return (lookup_value(sigchldcode, si_code));
717 sysdecode_sigfpe_code(int si_code)
720 return (lookup_value(sigfpecode, si_code));
724 sysdecode_sigill_code(int si_code)
727 return (lookup_value(sigillcode, si_code));
731 sysdecode_sigsegv_code(int si_code)
734 return (lookup_value(sigsegvcode, si_code));
738 sysdecode_sigtrap_code(int si_code)
741 return (lookup_value(sigtrapcode, si_code));
745 sysdecode_sigprocmask_how(int how)
748 return (lookup_value(sigprocmaskhow, how));
752 sysdecode_socketdomain(int domain)
755 return (lookup_value(sockdomain, domain));
759 sysdecode_socket_protocol(int domain, int protocol)
765 return (lookup_value(sockipproto, protocol));
772 sysdecode_sockaddr_family(int sa_family)
775 return (lookup_value(sockfamily, sa_family));
779 sysdecode_ipproto(int protocol)
782 return (lookup_value(sockipproto, protocol));
786 sysdecode_sockopt_name(int level, int optname)
789 if (level == SOL_SOCKET)
790 return (lookup_value(sockopt, optname));
791 if (level == IPPROTO_IP)
792 /* XXX: UNIX domain socket options use a level of 0 also. */
793 return (lookup_value(sockoptip, optname));
794 if (level == IPPROTO_IPV6)
795 return (lookup_value(sockoptipv6, optname));
796 if (level == IPPROTO_SCTP)
797 return (lookup_value(sockoptsctp, optname));
798 if (level == IPPROTO_TCP)
799 return (lookup_value(sockopttcp, optname));
800 if (level == IPPROTO_UDP)
801 return (lookup_value(sockoptudp, optname));
802 if (level == IPPROTO_UDPLITE)
803 return (lookup_value(sockoptudplite, optname));
808 sysdecode_thr_create_flags(FILE *fp, int flags, int *rem)
811 return (print_mask_int(fp, thrcreateflags, flags, rem));
815 sysdecode_umtx_op(int op)
818 return (lookup_value(umtxop, op));
822 sysdecode_vmresult(int result)
825 return (lookup_value(vmresult, result));
829 sysdecode_wait4_options(FILE *fp, int options, int *rem)
834 /* A flags value of 0 is normal. */
843 * These flags are implicit and aren't valid flags for wait4()
844 * directly (though they don't fail with EINVAL).
846 opt6 = options & (WEXITED | WTRAPPED);
848 printed = print_mask_int(fp, wait6opt, options, rem);
855 sysdecode_wait6_options(FILE *fp, int options, int *rem)
858 return (print_mask_int(fp, wait6opt, options, rem));
862 sysdecode_whence(int whence)
865 return (lookup_value(seekwhence, whence));
869 sysdecode_fcntl_cmd(int cmd)
872 return (lookup_value(fcntlcmd, cmd));
875 static struct name_table fcntl_fd_arg[] = {
876 X(FD_CLOEXEC) X(0) XEND
880 sysdecode_fcntl_arg_p(int cmd)
894 sysdecode_fcntl_arg(FILE *fp, int cmd, uintptr_t arg, int base)
900 if (!print_value(fp, fcntl_fd_arg, arg))
901 print_integer(fp, arg, base);
904 if (!sysdecode_fcntl_fileflags(fp, arg, &rem))
905 fprintf(fp, "%#x", rem);
907 fprintf(fp, "|%#x", rem);
912 fprintf(fp, "%p", (void *)arg);
915 print_integer(fp, arg, base);
921 sysdecode_mmap_flags(FILE *fp, int flags, int *rem)
928 * MAP_ALIGNED can't be handled directly by print_mask_int().
929 * MAP_32BIT is also problematic since it isn't defined for
933 align = flags & MAP_ALIGNMENT_MASK;
934 val = (unsigned)flags & ~MAP_ALIGNMENT_MASK;
935 print_mask_part(fp, mmapflags, &val, &printed);
937 if (val & MAP_32BIT) {
938 fprintf(fp, "%sMAP_32BIT", printed ? "|" : "");
946 if (align == MAP_ALIGNED_SUPER)
947 fputs("MAP_ALIGNED_SUPER", fp);
949 fprintf(fp, "MAP_ALIGNED(%d)",
950 align >> MAP_ALIGNMENT_SHIFT);
959 sysdecode_pathconf_name(int name)
962 return (lookup_value(pathconfname, name));
966 sysdecode_rtprio_function(int function)
969 return (lookup_value(rtpriofuncs, function));
973 sysdecode_msg_flags(FILE *fp, int flags, int *rem)
976 return (print_mask_0(fp, msgflags, flags, rem));
980 sysdecode_sigcode(int sig, int si_code)
984 str = lookup_value(sigcode, si_code);
990 return (sysdecode_sigill_code(si_code));
992 return (sysdecode_sigbus_code(si_code));
994 return (sysdecode_sigsegv_code(si_code));
996 return (sysdecode_sigfpe_code(si_code));
998 return (sysdecode_sigtrap_code(si_code));
1000 return (sysdecode_sigchld_code(si_code));
1007 sysdecode_sysarch_number(int number)
1010 return (lookup_value(sysarchnum, number));
1014 sysdecode_umtx_cvwait_flags(FILE *fp, u_long flags, u_long *rem)
1017 return (print_mask_0ul(fp, umtxcvwaitflags, flags, rem));
1021 sysdecode_umtx_rwlock_flags(FILE *fp, u_long flags, u_long *rem)
1024 return (print_mask_0ul(fp, umtxrwlockflags, flags, rem));
1028 sysdecode_cap_rights(FILE *fp, cap_rights_t *rightsp)
1030 struct name_table *t;
1034 for (i = 0; i < CAPARSIZE(rightsp); i++) {
1035 if (CAPIDXBIT(rightsp->cr_rights[i]) != 1 << i) {
1036 fprintf(fp, "invalid cap_rights_t");
1041 for (t = caprights; t->str != NULL; t++) {
1042 if (cap_rights_is_set(rightsp, t->val)) {
1043 fprintf(fp, "%s%s", comma ? "," : "", t->str);