1 /*
2  * Copyright (c) 2017, Arnon Yaari
3  * All rights reserved.
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7 
8 /* The kernel is always 64 bit but Python is usually compiled as a 32 bit
9  * process. We're reading the kernel memory to get the network connections,
10  * so we need the structs we read to be defined with 64 bit "pointers".
11  * Here are the partial definitions of the structs we use, taken from the
12  * header files, with data type sizes converted to their 64 bit counterparts,
13  * and unused data truncated. */
14 
15 #ifdef __64BIT__
16 /* In case we're in a 64 bit process after all */
17 #include <sys/socketvar.h>
18 #include <sys/protosw.h>
19 #include <sys/unpcb.h>
20 #include <sys/mbuf_base.h>
21 #include <sys/mbuf_macro.h>
22 #include <netinet/ip_var.h>
23 #include <netinet/tcp.h>
24 #include <netinet/tcpip.h>
25 #include <netinet/tcp_timer.h>
26 #include <netinet/tcp_var.h>
27 #define file64 file
28 #define socket64 socket
29 #define protosw64 protosw
30 #define inpcb64 inpcb
31 #define tcpcb64 tcpcb
32 #define unpcb64 unpcb
33 #define mbuf64 mbuf
34 #else       /* __64BIT__ */
35   struct file64 {
36     int f_flag;
37     int f_count;
38     int f_options;
39     int f_type;
40     u_longlong_t f_data;
41  };
42 
43  struct socket64 {
44     short   so_type;                 /* generic type, see socket.h */
45     short   so_options;              /* from socket call, see socket.h */
46     ushort  so_linger;               /* time to linger while closing */
47     short   so_state;                /* internal state flags SS_*, below */
48     u_longlong_t so_pcb;             /* protocol control block */
49     u_longlong_t so_proto;           /* protocol handle */
50  };
51 
52  struct protosw64 {
53     short   pr_type;                 /* socket type used for */
54     u_longlong_t pr_domain;          /* domain protocol a member of */
55     short   pr_protocol;             /* protocol number */
56     short   pr_flags;                /* see below */
57  };
58 
59  struct inpcb64 {
60     u_longlong_t inp_next,inp_prev;
61                                      /* pointers to other pcb's */
62     u_longlong_t inp_head;           /* pointer back to chain of inpcb's
63                                        for this protocol */
64     u_int32_t inp_iflowinfo;         /* input flow label */
65     u_short inp_fport;               /* foreign port */
66     u_int16_t inp_fatype;            /* foreign address type */
67     union   in_addr_6 inp_faddr_6;   /* foreign host table entry */
68     u_int32_t inp_oflowinfo;         /* output flow label */
69     u_short inp_lport;               /* local port */
70     u_int16_t inp_latype;            /* local address type */
71     union   in_addr_6 inp_laddr_6;   /* local host table entry */
72     u_longlong_t inp_socket;         /* back pointer to socket */
73     u_longlong_t inp_ppcb;           /* pointer to per-protocol pcb */
74     u_longlong_t space_rt;
75     struct  sockaddr_in6 spare_dst;
76     u_longlong_t inp_ifa;            /* interface address to use */
77     int     inp_flags;               /* generic IP/datagram flags */
78 };
79 
80 struct tcpcb64 {
81     u_longlong_t seg__next;
82     u_longlong_t seg__prev;
83     short   t_state;                 /* state of this connection */
84 };
85 
86 struct unpcb64 {
87         u_longlong_t unp_socket;     /* pointer back to socket */
88         u_longlong_t unp_vnode;      /* if associated with file */
89         ino_t   unp_vno;             /* fake vnode number */
90         u_longlong_t unp_conn;       /* control block of connected socket */
91         u_longlong_t unp_refs;       /* referencing socket linked list */
92         u_longlong_t unp_nextref;    /* link in unp_refs list */
93         u_longlong_t unp_addr;       /* bound address of socket */
94 };
95 
96 struct m_hdr64
97 {
98     u_longlong_t mh_next;            /* next buffer in chain */
99     u_longlong_t mh_nextpkt;         /* next chain in queue/record */
100     long mh_len;                     /* amount of data in this mbuf */
101     u_longlong_t mh_data;            /* location of data */
102 };
103 
104 struct mbuf64
105 {
106     struct m_hdr64 m_hdr;
107 };
108 
109 #define m_len               m_hdr.mh_len
110 
111 #endif      /* __64BIT__ */
112