Tweaked the timing of flow expiration.
[distributedratelimiting.git] / extensions / printpkt.c
1 /* printpkt.c
2  *
3  * build something looking like a iptables LOG message
4  *
5  * (C) 2000-2003 by Harald Welte <laforge@gnumonks.org>
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License version 2 
9  *  as published by the Free Software Foundation
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  * $Id: printpkt.c 6432 2006-01-25 11:21:28Z /C=DE/ST=Berlin/L=Berlin/O=Netfilter Project/OU=Development/CN=laforge/emailAddress=laforge@netfilter.org $
21  *
22  */
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <unistd.h>
27 #include <string.h>
28 #include <time.h>
29 #include <sys/time.h>
30 #include <sys/types.h>
31 #include <sys/socket.h>
32 #include <arpa/inet.h>
33 #include <netinet/ip.h>
34 #include <netinet/ip_icmp.h>
35 #include <ulogd/ulogd.h>
36 #include <ulogd/conffile.h>
37
38 #ifndef HOST_NAME_MAX
39 #warning this libc does not define HOST_NAME_MAX
40 #define HOST_NAME_MAX   (255+1)
41 #endif
42
43 #define NIPQUAD(addr) \
44         ((unsigned char *)&addr)[0], \
45         ((unsigned char *)&addr)[1], \
46         ((unsigned char *)&addr)[2], \
47         ((unsigned char *)&addr)[3]
48
49 struct intr_id {
50         char* name;
51         unsigned int id;                
52 };
53
54 static char hostname[HOST_NAME_MAX+1];
55
56 #define INTR_IDS        35
57 static struct intr_id intr_ids[INTR_IDS] = {
58         { "oob.time.sec", 0 },
59         { "oob.prefix", 0 },
60         { "oob.in", 0 },
61         { "oob.out", 0 },
62         { "raw.mac", 0 },
63         { "ip.saddr", 0 },
64         { "ip.daddr", 0 },
65         { "ip.totlen", 0 },
66         { "ip.tos", 0 },
67         { "ip.ttl", 0 },
68         { "ip.id", 0 },
69         { "ip.fragoff", 0 },
70         { "ip.protocol", 0 },
71         { "tcp.sport", 0 },
72         { "tcp.dport", 0 },
73         { "tcp.seq", 0 },
74         { "tcp.ackseq", 0 },
75         { "tcp.window", 0 },
76         { "tcp.urg", 0 },
77         { "tcp.ack", 0 },
78         { "tcp.psh", 0 },
79         { "tcp.rst", 0 },
80         { "tcp.syn", 0 },
81         { "tcp.fin", 0 },
82         { "tcp.urgp", 0 },
83         { "udp.sport", 0 },
84         { "udp.dport", 0 },
85         { "udp.len", 0 },
86         { "icmp.type", 0 },
87         { "icmp.code", 0 },
88         { "icmp.echoid", 0 },
89         { "icmp.echoseq", 0 },
90         { "icmp.gateway", 0 },
91         { "icmp.fragmtu", 0 },
92         { "ahesp.spi", 0 },
93 };
94
95 #define GET_VALUE(x)    ulogd_keyh[intr_ids[x].id].interp->result[ulogd_keyh[intr_ids[x].id].offset].value
96 #define GET_FLAGS(x)    ulogd_keyh[intr_ids[x].id].interp->result[ulogd_keyh[intr_ids[x].id].offset].flags
97
98 int printpkt_print(ulog_iret_t *res, char *buf, int prefix)
99 {
100         char *timestr;
101         char *tmp;
102         time_t now;
103
104         char *buf_cur = buf;
105
106         if (prefix) {
107                 now = (time_t) GET_VALUE(0).ui32;
108                 timestr = ctime(&now) + 4;
109
110                 /* truncate time */
111                 if ((tmp = strchr(timestr, '\n')))
112                         *tmp = '\0';
113
114                 /* truncate hostname */
115                 if ((tmp = strchr(hostname, '.')))
116                         *tmp = '\0';
117
118                 /* print time and hostname */
119                 buf_cur += sprintf(buf_cur, "%.15s %s", timestr, hostname);
120         }
121
122         if (*(char *) GET_VALUE(1).ptr)
123                 buf_cur += sprintf(buf_cur, " %s", (char *) GET_VALUE(1).ptr);
124
125         buf_cur += sprintf(buf_cur," IN=%s OUT=%s ", 
126                            (char *) GET_VALUE(2).ptr, 
127                            (char *) GET_VALUE(3).ptr);
128
129         /* FIXME: configurable */
130         buf_cur += sprintf(buf_cur, "MAC=%s ", 
131                 (GET_FLAGS(4) & ULOGD_RETF_VALID) ? (char *) GET_VALUE(4).ptr : "");
132
133         buf_cur += sprintf(buf_cur, "SRC=%s ", 
134                        inet_ntoa((struct in_addr) {htonl(GET_VALUE(5).ui32)}));
135         buf_cur += sprintf(buf_cur, "DST=%s ", 
136                        inet_ntoa((struct in_addr) {htonl(GET_VALUE(6).ui32)}));
137
138         buf_cur += sprintf(buf_cur,"LEN=%u TOS=%02X PREC=0x%02X TTL=%u ID=%u ", 
139                         GET_VALUE(7).ui16, GET_VALUE(8).ui8 & IPTOS_TOS_MASK, 
140                         GET_VALUE(8).ui8 & IPTOS_PREC_MASK, GET_VALUE(9).ui8,
141                         GET_VALUE(10).ui16);
142
143         if (GET_VALUE(10).ui16 & IP_RF) 
144                 buf_cur += sprintf(buf_cur, "CE ");
145
146         if (GET_VALUE(11).ui16 & IP_DF)
147                 buf_cur += sprintf(buf_cur, "DF ");
148
149         if (GET_VALUE(11).ui16 & IP_MF)
150                 buf_cur += sprintf(buf_cur, "MF ");
151
152         if (GET_VALUE(11).ui16 & IP_OFFMASK)
153                 buf_cur += sprintf(buf_cur, "FRAG:%u ", 
154                                 GET_VALUE(11).ui16 & IP_OFFMASK);
155
156         switch (GET_VALUE(12).ui8) {
157
158         case IPPROTO_TCP:
159                 buf_cur += sprintf(buf_cur, "PROTO=TCP ");
160                 buf_cur += sprintf(buf_cur, "SPT=%u DPT=%u ",
161                                 GET_VALUE(13).ui16, GET_VALUE(14).ui16);
162                 /* FIXME: config */
163                 buf_cur += sprintf(buf_cur, "SEQ=%u ACK=%u ", 
164                                 GET_VALUE(15).ui32, GET_VALUE(16).ui32);
165
166                 buf_cur += sprintf(buf_cur, "WINDOW=%u ", GET_VALUE(17).ui16);
167
168 //              buf_cur += sprintf(buf_cur, "RES=0x%02x ", 
169                 
170                 if (GET_VALUE(18).b)
171                         buf_cur += sprintf(buf_cur, "URG ");
172
173                 if (GET_VALUE(19).b)
174                         buf_cur += sprintf(buf_cur, "ACK ");
175
176                 if (GET_VALUE(20).b)
177                         buf_cur += sprintf(buf_cur, "PSH ");
178
179                 if (GET_VALUE(21).b)
180                         buf_cur += sprintf(buf_cur, "RST ");
181
182                 if (GET_VALUE(22).b)
183                         buf_cur += sprintf(buf_cur, "SYN ");
184
185                 if (GET_VALUE(23).b)
186                         buf_cur += sprintf(buf_cur, "FIN ");
187
188                 buf_cur += sprintf(buf_cur, "URGP=%u ", GET_VALUE(24).ui16);
189
190                 break;
191         case IPPROTO_UDP:
192
193                 buf_cur += sprintf(buf_cur, "PROTO=UDP ");
194
195                 buf_cur += sprintf(buf_cur, "SPT=%u DPT=%u LEN=%u ", 
196                                 GET_VALUE(25).ui16, GET_VALUE(26).ui16, 
197                                 GET_VALUE(27).ui16);
198                         break;
199         case IPPROTO_ICMP:
200
201                 buf_cur += sprintf(buf_cur, "PROTO=ICMP ");
202
203                 buf_cur += sprintf(buf_cur, "TYPE=%u CODE=%u ",
204                                 GET_VALUE(28).ui8, GET_VALUE(29).ui8);
205
206                 switch (GET_VALUE(28).ui8) {
207                 case ICMP_ECHO:
208                 case ICMP_ECHOREPLY:
209                         buf_cur += sprintf(buf_cur, "ID=%u SEQ=%u ", 
210                                            GET_VALUE(30).ui16,
211                                            GET_VALUE(31).ui16);
212                         break;
213                 case ICMP_PARAMETERPROB:
214                         buf_cur += sprintf(buf_cur, "PARAMETER=%u ",
215                                            GET_VALUE(32).ui32 >> 24);
216                         break;
217                 case ICMP_REDIRECT:
218                         buf_cur += sprintf(buf_cur, "GATEWAY=%s ", inet_ntoa((struct in_addr) {htonl(GET_VALUE(32).ui32)}));
219                         break;
220                 case ICMP_DEST_UNREACH:
221                         if (GET_VALUE(29).ui8 == ICMP_FRAG_NEEDED)
222                                 buf_cur += sprintf(buf_cur, "MTU=%u ", 
223                                                    GET_VALUE(33).ui16);
224                         break;
225                 }
226                 break;
227         case IPPROTO_ESP:
228         case IPPROTO_AH:
229                 buf_cur += sprintf(buf_cur, "PROTO=%s ", GET_VALUE(12).ui8 == IPPROTO_ESP ? "ESP" : "AH");
230                 /* FIXME: "INCOMPLETE [%u bytes]" in case of short pkt */
231                 if (intr_ids[34].id > 0) {
232                         buf_cur += sprintf(buf_cur, "SPI=0x%x ", GET_VALUE(34).ui32);
233                 }
234                 break;
235         default:
236
237                 buf_cur += sprintf(buf_cur, "PROTO=%u ", GET_VALUE(12).ui8);
238         }
239         strcat(buf_cur, "\n");
240
241         return 0;
242 }
243
244 /* get all key id's for the keys we are intrested in */
245 static int get_ids(void)
246 {
247         int i;
248         struct intr_id *cur_id;
249
250         for (i = 0; i < INTR_IDS; i++) {
251                 cur_id = &intr_ids[i];
252                 cur_id->id = keyh_getid(cur_id->name);
253                 if (!cur_id->id) {
254                         ulogd_log(ULOGD_ERROR, 
255                                 "Cannot resolve keyhash id for %s\n", 
256                                 cur_id->name);
257                         return 1;
258                 }
259         }       
260         return 0;
261 }
262
263 int printpkt_init(void)
264 {
265         if (gethostname(hostname, sizeof(hostname)) < 0) {
266                 ulogd_log(ULOGD_FATAL, "can't gethostname(): %s\n",
267                           strerror(errno));
268                 exit(2);
269         }
270
271         if (get_ids())
272                 return 1;
273
274         return 0;
275 }