summaryrefslogtreecommitdiffstats
path: root/src/pcaps.c
blob: f89b9bb493bed6745e82522542794ae67ba52776 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/*
 * Copyright (c) 2016-2023, OARC, Inc.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * 3. Neither the name of the copyright holder nor the names of its
 *    contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include "config.h"

#include "pcaps.h"
#include "log.h"
#include "network.h"

#include "pcap-thread/pcap_thread_ext_frag.h"

static void
drop_pkt(u_char* user, const struct pcap_pkthdr* hdr, const u_char* pkt, const char* name, const int dlt)
{
    mypcap_ptr mypcap = (mypcap_ptr)user;

    pcap_drops++;
    if (mypcap) {
        mypcap->drops++;
    }
}

void print_pcap_thread_error(const char* func, int err)
{
    if (err == PCAP_THREAD_EPCAP) {
        fprintf(stderr, "%s: pcap_thread libpcap error [%d] %s: %s (%s)\n",
            ProgramName,
            pcap_thread_status(&pcap_thread),
            func,
            pcap_statustostr(pcap_thread_status(&pcap_thread)),
            pcap_thread_errbuf(&pcap_thread));
    } else if (err != PCAP_THREAD_OK) {
        fprintf(stderr, "%s: pcap_thread error [%d] %s: %s\n",
            ProgramName,
            err,
            func,
            pcap_thread_strerr(err));
    }
}

static pcap_thread_ext_frag_conf_t frag_conf_v4 = PCAP_THREAD_EXT_FRAG_CONF_T_INIT;
static pcap_thread_ext_frag_conf_t frag_conf_v6 = PCAP_THREAD_EXT_FRAG_CONF_T_INIT;

void open_pcaps(void)
{
    mypcap_ptr mypcap;
    int        err;

    if ((err = pcap_thread_set_snaplen(&pcap_thread, SNAPLEN)) != PCAP_THREAD_OK) {
        print_pcap_thread_error("pcap_thread_set_snaplen()", err);
        exit(1);
    }
    if ((err = pcap_thread_set_promiscuous(&pcap_thread, promisc)) != PCAP_THREAD_OK) {
        print_pcap_thread_error("pcap_thread_set_promiscuous()", err);
        exit(1);
    }
    if ((err = pcap_thread_set_monitor(&pcap_thread, monitor_mode)) != PCAP_THREAD_OK) {
        print_pcap_thread_error("pcap_thread_set_monitor()", err);
        exit(1);
    }
    if ((err = pcap_thread_set_immediate_mode(&pcap_thread, immediate_mode)) != PCAP_THREAD_OK) {
        print_pcap_thread_error("pcap_thread_set_immediate_mode()", err);
        exit(1);
    }
    if (options.use_layers) {
        if ((err = pcap_thread_set_callback_icmp(&pcap_thread, layer_pkt)) != PCAP_THREAD_OK) {
            print_pcap_thread_error("pcap_thread_set_callback_icmp()", err);
            exit(1);
        }
        if ((err = pcap_thread_set_callback_icmpv6(&pcap_thread, layer_pkt)) != PCAP_THREAD_OK) {
            print_pcap_thread_error("pcap_thread_set_callback_icmpv6()", err);
            exit(1);
        }
        if ((err = pcap_thread_set_callback_udp(&pcap_thread, layer_pkt)) != PCAP_THREAD_OK) {
            print_pcap_thread_error("pcap_thread_set_callback_udp()", err);
            exit(1);
        }
        if ((err = pcap_thread_set_callback_tcp(&pcap_thread, layer_pkt)) != PCAP_THREAD_OK) {
            print_pcap_thread_error("pcap_thread_set_callback_tcp()", err);
            exit(1);
        }

        if ((err = pcap_thread_set_use_layers(&pcap_thread, 1)) != PCAP_THREAD_OK) {
            print_pcap_thread_error("pcap_thread_set_use_layers()", err);
            exit(1);
        }

        if (options.defrag_ipv4) {
            if (options.max_ipv4_fragments > 0 && (err = pcap_thread_ext_frag_conf_set_fragments(&frag_conf_v4, options.max_ipv4_fragments)) != PCAP_THREAD_OK) {
                print_pcap_thread_error("pcap_thread_ext_frag_conf_set_fragments()", err);
                exit(1);
            }
            if (options.max_ipv4_fragments_per_packet > 0 && (err = pcap_thread_ext_frag_conf_set_per_packet(&frag_conf_v4, options.max_ipv4_fragments_per_packet)) != PCAP_THREAD_OK) {
                print_pcap_thread_error("pcap_thread_ext_frag_conf_set_per_packet()", err);
                exit(1);
            }
            if ((err = pcap_thread_set_callback_ipv4_frag(&pcap_thread, pcap_thread_ext_frag_layer_callback(&frag_conf_v4))) != PCAP_THREAD_OK) {
                print_pcap_thread_error("pcap_thread_set_callback_ipv4_frag()", err);
                exit(1);
            }
        }
        if (options.defrag_ipv6) {
            if (options.max_ipv6_fragments > 0 && (err = pcap_thread_ext_frag_conf_set_fragments(&frag_conf_v6, options.max_ipv6_fragments)) != PCAP_THREAD_OK) {
                print_pcap_thread_error("pcap_thread_ext_frag_conf_set_fragments()", err);
                exit(1);
            }
            if (options.max_ipv6_fragments_per_packet > 0 && (err = pcap_thread_ext_frag_conf_set_per_packet(&frag_conf_v6, options.max_ipv6_fragments_per_packet)) != PCAP_THREAD_OK) {
                print_pcap_thread_error("pcap_thread_ext_frag_conf_set_per_packet()", err);
                exit(1);
            }
            if ((err = pcap_thread_set_callback_ipv6_frag(&pcap_thread, pcap_thread_ext_frag_layer_callback(&frag_conf_v6))) != PCAP_THREAD_OK) {
                print_pcap_thread_error("pcap_thread_set_callback_ipv6_frag()", err);
                exit(1);
            }
        }
    } else {
        if ((err = pcap_thread_set_callback(&pcap_thread, dl_pkt)) != PCAP_THREAD_OK) {
            print_pcap_thread_error("pcap_thread_set_callback()", err);
            exit(1);
        }
    }
    if ((err = pcap_thread_set_dropback(&pcap_thread, drop_pkt)) != PCAP_THREAD_OK) {
        print_pcap_thread_error("pcap_thread_set_dropback()", err);
        exit(1);
    }
    if ((err = pcap_thread_set_filter(&pcap_thread, bpft, strlen(bpft))) != PCAP_THREAD_OK) {
        print_pcap_thread_error("pcap_thread_set_filter()", err);
        exit(1);
    }
    if (options.pcap_buffer_size && (err = pcap_thread_set_buffer_size(&pcap_thread, options.pcap_buffer_size)) != PCAP_THREAD_OK) {
        print_pcap_thread_error("pcap_thread_set_buffer_size()", err);
        exit(1);
    }

    assert(!EMPTY(mypcaps));
    for (mypcap = HEAD(mypcaps);
         mypcap != NULL;
         mypcap = NEXT(mypcap, link)) {
        if (pcap_offline)
            err = pcap_thread_open_offline(&pcap_thread, mypcap->name, (u_char*)mypcap);
        else
            err = pcap_thread_open(&pcap_thread, mypcap->name, (u_char*)mypcap);

        if (err == PCAP_THREAD_EPCAP) {
            fprintf(stderr, "%s: pcap_thread libpcap error [%d]: %s (%s)\n",
                ProgramName,
                pcap_thread_status(&pcap_thread),
                pcap_statustostr(pcap_thread_status(&pcap_thread)),
                pcap_thread_errbuf(&pcap_thread));
            exit(1);
        }
        if (err) {
            fprintf(stderr, "%s: pcap_thread error [%d]: %s\n",
                ProgramName,
                err,
                pcap_thread_strerr(err));
            exit(1);
        }
    }
    pcap_dead = pcap_open_dead(DLT_RAW, SNAPLEN);
}

void poll_pcaps(void)
{
    pcap_thread_run(&pcap_thread);
    main_exit = TRUE;
}

void breakloop_pcaps(void)
{
    pcap_thread_stop(&pcap_thread);
}

void close_pcaps(void)
{
    pcap_thread_close(&pcap_thread);
}

static void stat_callback(u_char* user, const struct pcap_stat* stats, const char* name, int dlt)
{
    mypcap_ptr mypcap;
    for (mypcap = HEAD(mypcaps);
         mypcap != NULL;
         mypcap = NEXT(mypcap, link)) {
        if (!strcmp(name, mypcap->name))
            break;
    }

    if (mypcap) {
        mypcap->ps0 = mypcap->ps1;
        mypcap->ps1 = *stats;
        logerr("%s: %u recv %u drop %u total ptdrop %lu",
            mypcap->name,
            mypcap->ps1.ps_recv - mypcap->ps0.ps_recv,
            mypcap->ps1.ps_drop - mypcap->ps0.ps_drop,
            mypcap->ps1.ps_recv + mypcap->ps1.ps_drop - mypcap->ps0.ps_recv - mypcap->ps0.ps_drop,
            mypcap->drops);
    }
}

void do_pcap_stats()
{
    logerr("total drops: %lu", pcap_drops);
    pcap_thread_stats(&pcap_thread, stat_callback, 0);
}