2 * copyright (c) 2001-2008 Emil Mikulic.
4 * acct.c: traffic accounting
6 * Permission to use, copy, modify, and distribute this file for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
29 #include <arpa/inet.h> /* for inet_aton() */
31 #include <netinet/tcp.h>
32 #include <sys/socket.h>
34 #include <ctype.h> /* for isdigit */
35 #include <netdb.h> /* for gai_strerror */
36 #include <stdlib.h> /* for free */
37 #include <string.h> /* for memcpy */
39 uint64_t acct_total_packets
= 0, acct_total_bytes
= 0;
41 static int using_localnet4
= 0, using_localnet6
= 0;
42 static struct addr localnet4
, localmask4
, localnet6
, localmask6
;
44 /* Parse the net/mask specification into two IPs or die trying. */
46 acct_init_localnet(const char *spec
)
49 unsigned int num_tokens
;
51 int pfxlen
, octets
, remainder
;
52 struct addr localnet
, localmask
;
54 tokens
= split('/', spec
, &num_tokens
);
56 errx(1, "expecting network/netmask, got \"%s\"", spec
);
58 if ((ret
= str_to_addr(tokens
[0], &localnet
)) != 0)
59 errx(1, "couldn't parse \"%s\": %s", tokens
[0], gai_strerror(ret
));
61 /* Detect a purely numeric argument. */
64 const char *p
= tokens
[1];
78 if ((ret
= str_to_addr(tokens
[1], &localmask
)) != 0)
79 errx(1, "couldn't parse \"%s\": %s", tokens
[1], gai_strerror(ret
));
80 if (localmask
.family
!= localnet
.family
)
81 errx(1, "family mismatch between net and mask");
85 localmask
.family
= localnet
.family
;
87 /* Compute the prefix length. */
88 pfxlen
= (int)strtonum(tokens
[1], 1,
89 (localnet
.family
== IPv6
) ? 128 : 32, NULL
);
92 errx(1, "invalid network prefix length \"%s\"", tokens
[1]);
94 /* Construct the network mask. */
96 remainder
= pfxlen
% 8;
97 p
= (localnet
.family
== IPv6
) ? (localmask
.ip
.v6
.s6_addr
)
98 : ((uint8_t *) &(localmask
.ip
.v4
));
100 if (localnet
.family
== IPv6
)
105 for (j
= 0; j
< octets
; ++j
)
108 frac
= (uint8_t)(0xff << (8 - remainder
));
110 p
[j
] = frac
; /* Have contribution for next position. */
117 /* Register the correct netmask and calculate the correct net. */
118 addr_mask(&localnet
, &localmask
);
119 if (localnet
.family
== IPv6
) {
121 localnet6
= localnet
;
122 localmask6
= localmask
;
125 localnet4
= localnet
;
126 localmask4
= localmask
;
129 verbosef("local network address: %s", addr_to_str(&localnet
));
130 verbosef(" local network mask: %s", addr_to_str(&localmask
));
134 addr_is_local(const struct addr
* const a
)
136 if (a
->family
== IPv4
) {
137 if (using_localnet4
) {
138 if (addr_inside(a
, &localnet4
, &localmask4
))
141 if (addr_equal(a
, &localip4
))
145 assert(a
->family
== IPv6
);
146 if (using_localnet6
) {
147 if (addr_inside(a
, &localnet6
, &localmask6
))
150 if (addr_equal(a
, &localip6
))
157 /* Account for the given packet summary. */
159 acct_for(const struct pktsummary
* const sm
)
161 struct bucket
*hs
= NULL
, *hd
= NULL
;
162 struct bucket
*ps
, *pd
;
165 #if 0 /* WANT_CHATTY? */
166 printf("%15s > ", addr_to_str(&sm
->src
));
167 printf("%15s ", addr_to_str(&sm
->dst
));
168 printf("len %4d proto %2d", sm
->len
, sm
->proto
);
170 if (sm
->proto
== IPPROTO_TCP
|| sm
->proto
== IPPROTO_UDP
)
171 printf(" port %5d : %5d", sm
->src_port
, sm
->dst_port
);
172 if (sm
->proto
== IPPROTO_TCP
)
173 printf(" %s%s%s%s%s%s",
174 (sm
->tcp_flags
& TH_FIN
)?"F":"",
175 (sm
->tcp_flags
& TH_SYN
)?"S":"",
176 (sm
->tcp_flags
& TH_RST
)?"R":"",
177 (sm
->tcp_flags
& TH_PUSH
)?"P":"",
178 (sm
->tcp_flags
& TH_ACK
)?"A":"",
179 (sm
->tcp_flags
& TH_URG
)?"U":""
185 acct_total_packets
++;
186 acct_total_bytes
+= sm
->len
;
189 dir_out
= addr_is_local(&(sm
->src
));
190 dir_in
= addr_is_local(&(sm
->dst
));
192 /* Traffic staying within the network isn't counted. */
193 if (dir_in
== 1 && dir_out
== 1)
194 dir_in
= dir_out
= 0;
197 daylog_acct((uint64_t)sm
->len
, GRAPH_OUT
);
198 graph_acct((uint64_t)sm
->len
, GRAPH_OUT
);
201 daylog_acct((uint64_t)sm
->len
, GRAPH_IN
);
202 graph_acct((uint64_t)sm
->len
, GRAPH_IN
);
205 if (opt_hosts_max
== 0) return; /* skip per-host accounting */
209 hs
= host_get(&(sm
->src
));
211 hs
->total
+= sm
->len
;
212 memcpy(hs
->u
.host
.mac_addr
, sm
->src_mac
, sizeof(sm
->src_mac
));
213 hs
->u
.host
.last_seen
= now
;
215 hd
= host_get(&(sm
->dst
));
217 hd
->total
+= sm
->len
;
218 memcpy(hd
->u
.host
.mac_addr
, sm
->dst_mac
, sizeof(sm
->dst_mac
));
219 hd
->u
.host
.last_seen
= now
;
222 if (sm
->proto
!= IPPROTO_INVALID
) {
223 ps
= host_get_ip_proto(hs
, sm
->proto
);
225 ps
->total
+= sm
->len
;
227 pd
= host_get_ip_proto(hd
, sm
->proto
);
229 pd
->total
+= sm
->len
;
232 if (opt_ports_max
== 0) return; /* skip ports accounting */
237 if (sm
->src_port
<= opt_highest_port
) {
238 ps
= host_get_port_tcp(hs
, sm
->src_port
);
240 ps
->total
+= sm
->len
;
243 if (sm
->dst_port
<= opt_highest_port
) {
244 pd
= host_get_port_tcp(hd
, sm
->dst_port
);
246 pd
->total
+= sm
->len
;
247 if (sm
->tcp_flags
== TH_SYN
)
248 pd
->u
.port_tcp
.syn
++;
253 if (sm
->src_port
<= opt_highest_port
) {
254 ps
= host_get_port_udp(hs
, sm
->src_port
);
256 ps
->total
+= sm
->len
;
259 if (sm
->dst_port
<= opt_highest_port
) {
260 pd
= host_get_port_udp(hd
, sm
->dst_port
);
262 pd
->total
+= sm
->len
;
271 /* known protocol, don't complain about it */
275 verbosef("unknown IP proto (%04x)", sm
->proto
);
279 /* vim:set ts=3 sw=3 tw=78 expandtab: */