4 * (C)opyright 1995-1998 Darren Reed.
6 * See the IPFILTER.LICENCE file for details on licencing.
15 #include <sys/types.h>
17 #include <sys/socket.h>
18 #include <netinet/in.h>
19 #include <netinet/in_systm.h>
20 #include <netinet/ip.h>
21 #include <netinet/tcp.h>
22 #include <netinet/udp.h>
23 #include <netinet/ip_icmp.h>
25 #include <netinet/ip_var.h>
26 #include <netinet/tcpip.h>
28 #include "ip_compat.h"
30 #include <linux/sockios.h>
36 static const char sccsid
[] = "@(#)ipsd.c 1.3 12/3/95 (C)1995 Darren Reed";
37 static const char rcsid
[] = "@(#)Id: ipsd.c,v 2.2 2001/06/09 17:09:25 darrenr Exp";
44 char default_device
[] = "eth0";
47 char default_device
[] = "le0";
50 char default_device
[] = "ln0";
52 char default_device
[] = "lan0";
59 u_short defports
[NPORTS
] = {
60 7, 9, 20, 21, 23, 25, 53, 69, 79, 111,
61 123, 161, 162, 512, 513, 514, 515, 520, 540, 6000, 0
64 ipsd_t
*iphits
[NPORTS
];
71 return sh1
->sh_ip
.s_addr
- sh2
->sh_ip
.s_addr
;
76 * Check to see if we've already received a packet from this host for this
79 int findhit(ihp
, src
, dport
)
89 if (ihp
->sd_sz
== 4) {
90 for (i
= 0, sh
= ihp
->sd_hit
; i
< ihp
->sd_cnt
; i
++, sh
++)
91 if (src
.s_addr
== sh
->sh_ip
.s_addr
)
94 for (i
= ihp
->sd_cnt
/ 2, j
= (i
/ 2) - 1; j
>= 0; j
--) {
95 k
= ihp
->sd_hit
[i
].sh_ip
.s_addr
- src
.s_addr
;
109 * Search for port number amongst the sorted array of targets we're
120 for (i
= 10, j
= 4; j
>= 0; j
--) {
121 k
= tcp
->th_dport
- defports
[i
];
124 if (findhit(ihp
, ip
->ip_src
, tcp
->th_dport
))
126 sh
= ihp
->sd_hit
+ ihp
->sd_cnt
;
127 sh
->sh_date
= time(NULL
);
128 sh
->sh_ip
.s_addr
= ip
->ip_src
.s_addr
;
129 if (++ihp
->sd_cnt
== ihp
->sd_sz
)
132 sh
= realloc(sh
, ihp
->sd_sz
* sizeof(*sh
));
135 qsort(sh
, ihp
->sd_cnt
, sizeof(*sh
), ipcmp
);
148 * Allocate initial storage for hosts
154 for (i
= 0; i
< NPORTS
; i
++) {
156 if (iphits
[i
]->sd_hit
)
157 free(iphits
[i
]->sd_hit
);
160 iphits
[i
] = (ipsd_t
*)malloc(sizeof(ipsd_t
));
161 iphits
[i
]->sd_port
= defports
[i
];
162 iphits
[i
]->sd_cnt
= 0;
163 iphits
[i
]->sd_sz
= 4;
164 iphits
[i
]->sd_hit
= (sdhit_t
*)malloc(sizeof(sdhit_t
) * 4);
179 * Write statistics out to a file
188 (void) sprintf(fname
, "/var/log/ipsd/ipsd-hits.%d", nwrites
);
189 fd
= open(fname
, O_RDWR
|O_CREAT
|O_TRUNC
|O_EXCL
, 0644);
190 for (i
= 0, ipsd
= iphits
; i
< NPORTS
; i
++, ipsd
++) {
193 write(fd
, ips
, sizeof(ipsd_t
));
194 write(fd
, ips
->sd_hit
, sizeof(sdhit_t
) * ips
->sd_sz
);
204 signal(SIGCHLD
, waiter
);
224 fprintf(stderr
, "Usage: %s [-d device]\n", prog
);
229 void detecthits(fd
, writecount
)
236 hits
+= readloop(fd
, ip
);
237 if (hits
> writecount
) {
249 char *name
= argv
[0], *dev
= NULL
;
250 int fd
, writeafter
= 10000, angelic
= 0, c
;
252 while ((c
= getopt(argc
, argv
, "ad:n:")) != -1)
262 writeafter
= atoi(optarg
);
265 fprintf(stderr
, "Unknown option \"%c\"\n", c
);
269 bzero(iphits
, sizeof(iphits
));
273 dev
= default_device
;
274 printf("Device: %s\n", dev
);
275 fd
= initdevice(dev
, 60);
284 (void) setpgrp(0, getpgrp());
294 signal(SIGUSR1
, writenow
);
295 detecthits(fd
, writeafter
);