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
|
#include "config.h"
#include "defines.h"
#include "common.h"
#include "iputil.h"
#include <dnet.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
static ssize_t
inet_add_option_6(void *buf, size_t len, int proto, const void *optbuf, size_t optlen);
ssize_t
inet_add_option(uint16_t eth_type, void *buf, size_t len,
int proto, const void *optbuf, size_t optlen)
{
if (eth_type == ETH_TYPE_IP) {
return ip_add_option(buf, len, proto, optbuf, optlen);
} else if (eth_type == ETH_TYPE_IPV6) {
return inet_add_option_6(buf, len, proto, optbuf, optlen);
} else {
errno = EINVAL;
return -1;
}
}
/*
* IPv6 version of libdnet's ip_add_option():
*/
ssize_t
inet_add_option_6(void *buf, size_t len, int proto, const void *optbuf, size_t optlen)
{
struct ip6_hdr *ip6;
struct tcp_hdr *tcp = NULL;
u_char *p;
int hl, datalen, padlen;
if (proto != IP_PROTO_TCP) {
errno = EINVAL;
return (-1);
}
ip6 = (struct ip6_hdr *)buf;
p = (u_char *)buf + IP6_HDR_LEN;
tcp = (struct tcp_hdr *)p;
hl = tcp->th_off << 2;
p = (u_char *)tcp + hl;
datalen = ntohs(ip6->ip6_plen) + IP6_HDR_LEN - (p - (u_char *)buf);
/* Compute padding to next word boundary. */
if ((padlen = 4 - (optlen % 4)) == 4)
padlen = 0;
/* XXX - IP_HDR_LEN_MAX == TCP_HDR_LEN_MAX */
if (hl + optlen + padlen > IP_HDR_LEN_MAX ||
ntohs(ip6->ip6_plen) + IP6_HDR_LEN + optlen + padlen > len) {
errno = EINVAL;
return (-1);
}
/* Shift any existing data. */
if (datalen) {
memmove(p + optlen + padlen, p, datalen);
}
/* XXX - IP_OPT_NOP == TCP_OPT_NOP */
if (padlen) {
memset(p, IP_OPT_NOP, padlen);
p += padlen;
}
memmove(p, optbuf, optlen);
p += optlen;
optlen += padlen;
tcp->th_off = (p - (u_char *)tcp) >> 2;
ip6->ip6_plen = htons(ntohs(ip6->ip6_plen) + optlen);
return (optlen);
}
void
inet_checksum(uint16_t eth_type, void *buf, size_t len)
{
if (eth_type == ETH_TYPE_IP) {
return ip_checksum(buf, len);
} else if (eth_type == ETH_TYPE_IPV6) {
return ip6_checksum(buf, len);
}
}
int
raw_ip_opt_parse(int argc, char *argv[], uint8_t *opt_type, uint8_t *opt_len,
uint8_t *buff, int buff_len)
{
int i, j;
if (sscanf(argv[0], "%hhx", opt_type) != 1) {
warn("invalid opt_type");
return -1;
}
if (sscanf(argv[1], "%hhx", opt_len) != 1) {
warn("invalid opt_len");
return -1;
}
j = 0;
for (i = 2; i < argc && j < buff_len; ++i, ++j) {
if (sscanf(argv[i], "%hhx", &buff[j]) != 1) {
warn("invalid opt_data");
return -1;
}
}
if (*opt_len != j + 2) {
warnx("invalid opt->len (%d) doesn't match data length (%d)",
*opt_len, j);
return -1;
}
return 0;
}
int
raw_ip6_opt_parse(int argc, char *argv[], uint8_t *proto, int *len,
uint8_t *buff, int buff_len)
{
int i, j;
if (sscanf(argv[0], "%hhx", proto) != 1) {
warn("invalid protocol");
return -1;
}
j = 0;
for (i = 1; i < argc && j < buff_len; ++i, ++j) {
if (sscanf(argv[i], "%hhx", &buff[j]) != 1) {
warn("invalid opt_data");
return -1;
}
}
*len = j;
if ((j + 2) % 8 != 0) {
warnx("(opt_len (%d) + 2) %% 8 != 0", j);
return -1;
}
return 0;
}
|