suricata
respond-reject.c
Go to the documentation of this file.
1 /* Copyright (C) 2007-2020 Open Information Security Foundation
2  *
3  * You can copy, redistribute or modify this Program under the terms of
4  * the GNU General Public License version 2 as published by the Free
5  * Software Foundation.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * version 2 along with this program; if not, write to the Free Software
14  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
15  * 02110-1301, USA.
16  */
17 
18 /**
19  * \file
20  *
21  * \author William Metcalf <william.metcalf@gmail.com>
22  *
23  * RespondReject is a threaded wrapper for sending Rejects
24  *
25  */
26 
27 #include "suricata-common.h"
28 #include "decode.h"
29 #include "packet-queue.h"
30 #include "threads.h"
31 #include "threadvars.h"
32 #include "tm-queuehandlers.h"
33 #include "tm-threads.h"
34 #include "action-globals.h"
35 
36 #include "respond-reject.h"
38 
39 #include "util-debug.h"
40 #include "util-privs.h"
41 
42 int RejectSendIPv4TCP(ThreadVars *, Packet *, void *);
43 int RejectSendIPv4ICMP(ThreadVars *, Packet *, void *);
44 int RejectSendIPv6TCP(ThreadVars *, Packet *, void *);
45 int RejectSendIPv6ICMP(ThreadVars *, Packet *, void *);
46 static TmEcode RespondRejectFunc(ThreadVars *tv, Packet *p, void *data);
47 static TmEcode RespondRejectThreadDeinit(ThreadVars *tv, void *data);
48 
50 {
51  tmm_modules[TMM_RESPONDREJECT].name = "RespondReject";
53  tmm_modules[TMM_RESPONDREJECT].Func = RespondRejectFunc;
54  tmm_modules[TMM_RESPONDREJECT].ThreadDeinit = RespondRejectThreadDeinit;
55  tmm_modules[TMM_RESPONDREJECT].cap_flags = 0; /* libnet is not compat with caps */
56 }
57 
58 static TmEcode RespondRejectThreadDeinit(ThreadVars *tv, void *data)
59 {
60  FreeCachedCtx();
61  return TM_ECODE_OK;
62 }
63 
64 static TmEcode RespondRejectFunc(ThreadVars *tv, Packet *p, void *data)
65 {
66  /* ACTION_REJECT defaults to rejecting the SRC */
68  return TM_ECODE_OK;
69  }
70 
71  if (IS_TUNNEL_PKT(p)) {
72  return TM_ECODE_OK;
73  }
74 
75  if (PKT_IS_IPV4(p)) {
76  if (PKT_IS_TCP(p)) {
77  (void)RejectSendIPv4TCP(tv, p, data);
78  } else {
79  (void)RejectSendIPv4ICMP(tv, p, data);
80  }
81  } else if (PKT_IS_IPV6(p)) {
82  if (PKT_IS_TCP(p)) {
83  (void)RejectSendIPv6TCP(tv, p, data);
84  } else {
85  (void)RejectSendIPv6ICMP(tv, p, data);
86  }
87  }
88 
89  return TM_ECODE_OK;
90 }
91 
92 int RejectSendIPv4TCP(ThreadVars *tv, Packet *p, void *data)
93 {
94  SCEnter();
96  int r = RejectSendLibnet11IPv4TCP(tv, p, data, REJECT_DIR_SRC);
97  SCReturnInt(r);
98  } else if (PACKET_TEST_ACTION(p, ACTION_REJECT_DST)) {
99  int r = RejectSendLibnet11IPv4TCP(tv, p, data, REJECT_DIR_DST);
100  SCReturnInt(r);
101  } else if(PACKET_TEST_ACTION(p, ACTION_REJECT_BOTH)) {
102  int r = RejectSendLibnet11IPv4TCP(tv, p, data, REJECT_DIR_SRC);
104  SCReturnInt(r);
105  }
106  SCReturnInt(0);
107 }
108 
109 int RejectSendIPv4ICMP(ThreadVars *tv, Packet *p, void *data)
110 {
111  SCEnter();
113  int r = RejectSendLibnet11IPv4ICMP(tv, p, data, REJECT_DIR_SRC);
114  SCReturnInt(r);
115  } else if (PACKET_TEST_ACTION(p, ACTION_REJECT_DST)) {
116  int r = RejectSendLibnet11IPv4ICMP(tv, p, data, REJECT_DIR_DST);
117  SCReturnInt(r);
118  } else if(PACKET_TEST_ACTION(p, ACTION_REJECT_BOTH)) {
119  int r = RejectSendLibnet11IPv4ICMP(tv, p, data, REJECT_DIR_SRC);
121  SCReturnInt(r);
122  }
123  SCReturnInt(0);
124 }
125 
126 int RejectSendIPv6TCP(ThreadVars *tv, Packet *p, void *data)
127 {
128  SCEnter();
130  int r = RejectSendLibnet11IPv6TCP(tv, p, data, REJECT_DIR_SRC);
131  SCReturnInt(r);
132  } else if (PACKET_TEST_ACTION(p, ACTION_REJECT_DST)) {
133  int r = RejectSendLibnet11IPv6TCP(tv, p, data, REJECT_DIR_DST);
134  SCReturnInt(r);
135  } else if(PACKET_TEST_ACTION(p, ACTION_REJECT_BOTH)) {
136  int r = RejectSendLibnet11IPv6TCP(tv, p, data, REJECT_DIR_SRC);
138  SCReturnInt(r);
139  }
140  SCReturnInt(0);
141 }
142 
143 int RejectSendIPv6ICMP(ThreadVars *tv, Packet *p, void *data)
144 {
145  SCEnter();
147  int r = RejectSendLibnet11IPv6ICMP(tv, p, data, REJECT_DIR_SRC);
148  SCReturnInt(r);
149  } else if (PACKET_TEST_ACTION(p, ACTION_REJECT_DST)) {
150  int r = RejectSendLibnet11IPv6ICMP(tv, p, data, REJECT_DIR_DST);
151  SCReturnInt(r);
152  } else if(PACKET_TEST_ACTION(p, ACTION_REJECT_BOTH)) {
153  int r = RejectSendLibnet11IPv6ICMP(tv, p, data, REJECT_DIR_SRC);
155  SCReturnInt(r);
156  }
157  SCReturnInt(0);
158 }
TmModule_::cap_flags
uint8_t cap_flags
Definition: tm-modules.h:67
tm-threads.h
RejectSendIPv6TCP
int RejectSendIPv6TCP(ThreadVars *, Packet *, void *)
Definition: respond-reject.c:126
RejectSendIPv4TCP
int RejectSendIPv4TCP(ThreadVars *, Packet *, void *)
Definition: respond-reject.c:92
PKT_IS_IPV6
#define PKT_IS_IPV6(p)
Definition: decode.h:258
ACTION_REJECT
#define ACTION_REJECT
Definition: action-globals.h:31
RejectSendIPv4ICMP
int RejectSendIPv4ICMP(ThreadVars *, Packet *, void *)
Definition: respond-reject.c:109
RejectSendLibnet11IPv6TCP
int RejectSendLibnet11IPv6TCP(ThreadVars *tv, Packet *p, void *data, enum RejectDirection dir)
Definition: respond-reject-libnet11.c:592
action-globals.h
threads.h
packet-queue.h
RejectSendIPv6ICMP
int RejectSendIPv6ICMP(ThreadVars *, Packet *, void *)
Definition: respond-reject.c:143
REJECT_DIR_DST
@ REJECT_DIR_DST
Definition: respond-reject.h:31
ACTION_REJECT_ANY
#define ACTION_REJECT_ANY
Definition: action-globals.h:37
util-privs.h
TM_ECODE_OK
@ TM_ECODE_OK
Definition: tm-threads-common.h:80
REJECT_DIR_SRC
@ REJECT_DIR_SRC
Definition: respond-reject.h:30
TmModule_::ThreadDeinit
TmEcode(* ThreadDeinit)(ThreadVars *, void *)
Definition: tm-modules.h:49
ACTION_REJECT_DST
#define ACTION_REJECT_DST
Definition: action-globals.h:32
PKT_IS_TCP
#define PKT_IS_TCP(p)
Definition: decode.h:259
decode.h
util-debug.h
SCEnter
#define SCEnter(...)
Definition: util-debug.h:300
ThreadVars_
Per thread variable structure.
Definition: threadvars.h:58
TmModule_::Func
TmEcode(* Func)(ThreadVars *, Packet *, void *)
Definition: tm-modules.h:52
RejectSendLibnet11IPv4TCP
int RejectSendLibnet11IPv4TCP(ThreadVars *tv, Packet *p, void *data, enum RejectDirection dir)
Definition: respond-reject-libnet11.c:576
Packet_
Definition: decode.h:414
tmm_modules
TmModule tmm_modules[TMM_SIZE]
Definition: tm-modules.c:33
FreeCachedCtx
void FreeCachedCtx(void)
Definition: respond-reject-libnet11.c:608
TmEcode
TmEcode
Definition: tm-threads-common.h:79
IS_TUNNEL_PKT
#define IS_TUNNEL_PKT(p)
Definition: decode.h:894
TmModule_::name
const char * name
Definition: tm-modules.h:44
tm-queuehandlers.h
ACTION_REJECT_BOTH
#define ACTION_REJECT_BOTH
Definition: action-globals.h:33
suricata-common.h
respond-reject-libnet11.h
TMM_RESPONDREJECT
@ TMM_RESPONDREJECT
Definition: tm-threads-common.h:46
TmModule_::ThreadInit
TmEcode(* ThreadInit)(ThreadVars *, const void *, void **)
Definition: tm-modules.h:47
tv
ThreadVars * tv
Definition: fuzz_decodepcapfile.c:29
RejectSendLibnet11IPv6ICMP
int RejectSendLibnet11IPv6ICMP(ThreadVars *tv, Packet *p, void *data, enum RejectDirection dir)
Definition: respond-reject-libnet11.c:600
threadvars.h
RejectSendLibnet11IPv4ICMP
int RejectSendLibnet11IPv4ICMP(ThreadVars *tv, Packet *p, void *data, enum RejectDirection dir)
Definition: respond-reject-libnet11.c:584
TmModuleRespondRejectRegister
void TmModuleRespondRejectRegister(void)
Definition: respond-reject.c:49
likely
#define likely(expr)
Definition: util-optimize.h:32
respond-reject.h
SCReturnInt
#define SCReturnInt(x)
Definition: util-debug.h:304
PKT_IS_IPV4
#define PKT_IS_IPV4(p)
Definition: decode.h:257
PACKET_TEST_ACTION
#define PACKET_TEST_ACTION(p, a)
Definition: decode.h:870