-
Notifications
You must be signed in to change notification settings - Fork 0
/
netif.c
149 lines (128 loc) · 3.58 KB
/
netif.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
142
143
144
145
146
147
148
149
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <poll.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <net/ethernet.h>
#include <net/if.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/ip6.h>
#include <netinet/if_ether.h>
#include <linux/if_packet.h>
#include "akarivpn.h"
#include "netif.h"
static void
hwaddrfmt(uint8_t *hw, char *str) {
snprintf(str, 32, "%02x:%02x:%02x:%02x:%02x:%02x",
hw[0], hw[1], hw[2], hw[3], hw[4], hw[5]);
}
static void
ipv4addrfmt(uint32_t ipaddr, char *str) {
uint8_t *ip = (uint8_t *)&ipaddr;
snprintf(str, 32, "%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]);
}
static void
ipv6addrfmt(struct in6_addr *ipaddr, char *str) {
uint16_t *addr = (uint16_t *)ipaddr->s6_addr;
snprintf(str, 64, "%x:%x:%x:%x:%x:%x:%x:%x",
ntohs(addr[0]), ntohs(addr[1]), ntohs(addr[2]), ntohs(addr[3]),
ntohs(addr[4]), ntohs(addr[5]), ntohs(addr[6]), ntohs(addr[7]));
}
static const char *
ip_protocol_fmt(uint8_t proto) {
switch(proto) {
case IPPROTO_TCP:
return "tcp";
case IPPROTO_UDP:
return "udp";
default:
return "???";
}
}
static void
ipv4_packet_dump(unsigned char *buf) {
struct iphdr *iphdr = (struct iphdr *)buf;
char dst[32] = {0};
char src[32] = {0};
ipv4addrfmt(iphdr->daddr, dst);
ipv4addrfmt(iphdr->saddr, src);
printf("ipv4 %s ---> %s %s\n", src, dst, ip_protocol_fmt(iphdr->protocol));
}
static void
ipv6_packet_dump(unsigned char *buf) {
struct ip6_hdr *ip6 = (struct ip6_hdr *)buf;
struct in6_addr *dst = &ip6->ip6_dst;
struct in6_addr *src = &ip6->ip6_src;
char d_str[64];
char s_str[64];
ipv6addrfmt(dst, d_str);
ipv6addrfmt(src, s_str);
printf("ipv6 %s ---> %s %s\n", s_str, d_str, ip_protocol_fmt(ip6->ip6_nxt));
}
static void
arp_packet_dump(unsigned char *packet) {
struct arphdr *arp = (struct arphdr *)packet;
unsigned char hln = arp->ar_hln;
unsigned char pln = arp->ar_pln;
int op = ntohs(arp->ar_op);
uint8_t *body = packet + sizeof(struct arphdr);
uint8_t *sha = body;
uint8_t *sip = sha + hln;
uint8_t *tha = sip + pln;
uint8_t *tip = tha + hln;
char ssha[32], stha[32], ssip[32], stip[32];
hwaddrfmt(sha, ssha);
hwaddrfmt(tha, stha);
snprintf(ssip, 32, "%d.%d.%d.%d", sip[0], sip[1], sip[2], sip[3]);
snprintf(stip, 32, "%d.%d.%d.%d", tip[0], tip[1], tip[2], tip[3]);
switch(op) {
case ARPOP_REPLY:
printf("arp reply %s %s says: %s is %s\n", ssha, ssip, stip, stha);
break;
case ARPOP_REQUEST:
printf("arp request %s %s says: who has %s?\n", ssha, ssip, stip);
break;
default:
break;
}
}
void
l3packet_dump(unsigned char *packet, size_t size, unsigned short type) {
switch(type) {
case 0x0800: // ipv4
ipv4_packet_dump(packet);
break;
case 0x86dd: // ipv6
ipv6_packet_dump(packet);
break;
case 0x0806:
arp_packet_dump(packet);
break;
default:
printf("?\n");
break;
};
}
void
l2packet_dump(unsigned char *packet, size_t size) {
struct ether_header *eth = (struct ether_header *)packet;
unsigned short type = ntohs(eth->ether_type);
packet += sizeof(struct ether_header);
size -= sizeof(struct ether_header);
printf("ether type %#x size %ld ", type, size);
l3packet_dump(packet, size, type);
}
void
packetdump(struct netif *netif, unsigned char *buf, size_t size) {
printf("-------- packet to %s\n", netif->ifname);
for(int i = 0; i < size; i++) {
printf("%02x ", buf[i]);
if((i + 1) % 16 == 0)
printf("\n");
}
}