This repository has been archived by the owner on Sep 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
linkloop_reply.c
92 lines (80 loc) · 2.51 KB
/
linkloop_reply.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
/*
* Written by Oron Peled <[email protected]>
*
* Some code snippets adapted from spak
* (http://www.xenos.net/software/spak/)
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*/
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <netinet/if_ether.h>
#include <linux/if_packet.h>
#include <net/if.h>
#include <sys/select.h>
#include "config.h"
#include "linkloop.h"
/*
* This program runs as a deamon, listening at the llc level on network interfaces
* passed on the command line, and responding by sending back a test packet.
*/
/* Linkloop reply configuration */
static struct linkloop_reply {
const char *program;
int ifindex_listen[MAX_IFACES];
u_int8_t mac_listen[MAX_IFACES][IFHWADDRLEN];
u_int8_t src_mac[IFHWADDRLEN];
u_int8_t dst_mac[IFHWADDRLEN];
} llr;
int main(int argc, char *argv[]) {
struct llc_packet spack;
struct llc_packet rpack;
struct sockaddr_ll sll;
int nif = argc - 1; /* number of interfaces to listen */
size_t len;
int sock;
int i;
llr.program = argv[0];
if (nif == 0 || nif > MAX_IFACES) {
/* one parameter expected at least: the lan interface name to listen */
fprintf(stderr, "Expecting a list of one to %d interfaces to listen; eg: %s eth0 eth3\n",
MAX_IFACES, llr.program);
return 1;
}
/* Open the socket */
if ((sock = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_802_2))) == -1) {
perror("socket");
return 1;
}
/* Getting mac addresses for all listened interfaces */
for(i = 0; i < nif; ++i) {
get_hwaddr(sock, argv[i+1], &llr.ifindex_listen[i], llr.mac_listen[i]);
}
/* listen and reply forever */
do {
len = recv_packet(sock, &rpack);
memcpy(llr.src_mac, rpack.eth_hdr.ether_shost, IFHWADDRLEN);
memcpy(llr.dst_mac, rpack.eth_hdr.ether_dhost, IFHWADDRLEN);
/* check against listened interfaces */
for(i = 0; i < nif; ++i)
{
/* check if this packet has been received on a listened interface */
if(memcmp(llr.mac_listen[i], llr.dst_mac, IFHWADDRLEN))
continue;
/* return a test packet to the sender */
printf("Received packet on %s\n", argv[i+1]);
mk_test_packet(&spack, &sll, llr.dst_mac, llr.ifindex_listen[i], llr.src_mac, len, 1);
send_packet(sock, &spack, &sll);
}
} while(1);
return 0;
}