forked from reese-grimsley/tsn_sandbox
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathudp_jammer.c
141 lines (106 loc) · 4.21 KB
/
udp_jammer.c
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
/**
MIT License
Copyright (c) 2021 Reese Grimsley
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
* The Jammer will inject traffic into a raw socket as quickly as it can.
* However, we're using a 2.5Gbps capable NIC, so it's unlikely it is anywhere near saturation..
* We will send through UDP or ethernet
* However, strange cases have been observed where the switch routes traffic through a totally different
* interface like wireless. This will not suitably slow traffic intended for the ethernet NIC of the sink
*
* Assumed platform: Ubuntu 20.04 LTS, Intel Nuc (series 11), NIC i225
* Must be run as SUDO!
* MAC address of each device (sink, jammer, and source) are assumed within the constants.h file
*
* Author: Reese Grimsley
* Created: 11/15/21
*
* Raw sockets references:
* https://www.binarytides.com/raw-sockets-c-code-linux/
*
* No guarantees for this software. Use as is at your own risk. This is created as a learning exercise.
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <arpa/inet.h>
#include <net/ethernet.h>
#include <net/if.h>
#include <linux/if_packet.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <errno.h>
#include "helpers.h"
#include "constants.h"
#include "types.h"
int go_slower = 0; //boolean flag to wait between outgoing messages
struct timespec wait_duration = {.tv_sec=0, .tv_nsec= 1000};
// char ADDRESS_TO_JAM[ETHER_ADDR_LEN+1] = SINK_MAC_ADDR;
char ADDRESS_TO_JAM[ETHER_ADDR_LEN+1] = SOURCE_MAC_ADDR;
int setup_sock_udp(struct sockaddr_in* addr_sink, struct sockaddr_in* addr_jammer)
{
int jammer_sock, rt;
char junk_data[MAX_UDP_PACKET_SIZE];
printf("Configure jammer socket for UDP\n");
jammer_sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if( jammer_sock == -1)
{
printf("Send socket returned err: [%d]\n", errno);
exit(errno);
}
memset(addr_sink, 0, sizeof(*addr_sink));
memset(addr_jammer, 0, sizeof(*addr_jammer));
addr_sink->sin_family = AF_INET;
addr_sink->sin_port = htons(SINK_PORT+1);
addr_sink->sin_addr.s_addr = inet_addr(SINK_IP_ADDR_VLAN);
addr_jammer->sin_family = AF_INET;
addr_jammer->sin_port = htons(JAMMER_PORT);
addr_jammer->sin_addr.s_addr = inet_addr(JAMMER_IP_ADDR_VLAN);
rt = bind(jammer_sock, (struct sockaddr*) addr_jammer, sizeof(*addr_jammer));
if (rt != 0)
{
perror("bind socket");
shutdown(jammer_sock,2);
exit(errno);
}
return jammer_sock;
}
int main(int argc, char* argv[])
{
int jammer_sock;
struct sockaddr_in addr_sink, addr_jammer;
char junk_data[MAX_UDP_PACKET_SIZE];
printf("Start jammer\n");
jammer_sock = setup_sock_udp(&addr_sink, &addr_jammer);
memset(junk_data, '^', MAX_UDP_PACKET_SIZE);
printf("Send data as fast as possible\n");
while(1)
{
int rc = sendto(jammer_sock, junk_data, MAX_UDP_PACKET_SIZE, 0, (struct sockaddr*) &addr_sink, sizeof(addr_sink));
if (rc < 0)
{
printf("Socket did not send correctly... returned [%d] (error number: [%d])", rc, errno);
// perror("socket fail");
continue;
}
if (go_slower)
{
wait(wait_duration, 1);
}
}
printf("Done\n");
}