summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatthias Schiffer <mschiffer@universe-factory.net>2012-04-02 01:54:00 +0200
committerMatthias Schiffer <mschiffer@universe-factory.net>2012-04-02 01:54:00 +0200
commitb56a383d4ceaaf9981d2305834d7b2003b9da48f (patch)
tree25a9b5e7e2b8269173510f818a0f0f49574740d8
parent6df58e79890f05010ba41ce1adb1fc9f94e8a53b (diff)
downloadfastd-0.1-rc3.tar
fastd-0.1-rc3.zip
Warn on MTU mismatchv0.1-rc3
-rw-r--r--src/handshake.c25
-rw-r--r--src/handshake.h17
2 files changed, 40 insertions, 2 deletions
diff --git a/src/handshake.c b/src/handshake.c
index a17fd2b..2359af4 100644
--- a/src/handshake.c
+++ b/src/handshake.c
@@ -44,6 +44,7 @@ static const char const *RECORD_TYPES[RECORD_MAX] = {
"(protocol specific 3)",
"(protocol specific 4)",
"(protocol specific 5)",
+ "MTU",
};
static const char const *REPLY_TYPES[REPLY_MAX] = {
@@ -53,13 +54,15 @@ static const char const *REPLY_TYPES[REPLY_MAX] = {
};
#define AS_UINT8(ptr) (*(uint8_t*)(ptr).data)
+#define AS_UINT16(ptr) ((*(uint8_t*)(ptr).data) + (*((uint8_t*)(ptr).data+1) << 8))
fastd_buffer fastd_handshake_new_init(fastd_context *ctx, fastd_peer *peer, size_t tail_space) {
size_t protocol_len = strlen(ctx->conf->protocol->name);
fastd_buffer buffer = fastd_buffer_alloc(sizeof(fastd_packet), 0,
- 2*5 + /* handshake type, mode */
- 4+protocol_len+ /* protocol name */
+ 2*5 + /* handshake type, mode */
+ 6 + /* MTU */
+ 4+protocol_len + /* protocol name */
tail_space
);
fastd_packet *request = buffer.data;
@@ -69,6 +72,7 @@ fastd_buffer fastd_handshake_new_init(fastd_context *ctx, fastd_peer *peer, size
fastd_handshake_add_uint8(ctx, &buffer, RECORD_HANDSHAKE_TYPE, 1);
fastd_handshake_add_uint8(ctx, &buffer, RECORD_MODE, ctx->conf->mode);
+ fastd_handshake_add_uint16(ctx, &buffer, RECORD_MTU, ctx->conf->mtu);
fastd_handshake_add(ctx, &buffer, RECORD_PROTOCOL_NAME, protocol_len, ctx->conf->protocol->name);
@@ -76,8 +80,15 @@ fastd_buffer fastd_handshake_new_init(fastd_context *ctx, fastd_peer *peer, size
}
fastd_buffer fastd_handshake_new_reply(fastd_context *ctx, fastd_peer *peer, const fastd_handshake *handshake, size_t tail_space) {
+ bool first = (AS_UINT8(handshake->records[RECORD_HANDSHAKE_TYPE]) == 1);
+ size_t mtu_size = 0;
+
+ if (first)
+ mtu_size = 6;
+
fastd_buffer buffer = fastd_buffer_alloc(sizeof(fastd_packet), 0,
2*5 + /* handshake type, reply code */
+ mtu_size +
tail_space
);
fastd_packet *request = buffer.data;
@@ -88,6 +99,9 @@ fastd_buffer fastd_handshake_new_reply(fastd_context *ctx, fastd_peer *peer, con
fastd_handshake_add_uint8(ctx, &buffer, RECORD_HANDSHAKE_TYPE, AS_UINT8(handshake->records[RECORD_HANDSHAKE_TYPE])+1);
fastd_handshake_add_uint8(ctx, &buffer, RECORD_REPLY_CODE, 0);
+ if (first)
+ fastd_handshake_add_uint16(ctx, &buffer, RECORD_MTU, ctx->conf->mtu);
+
return buffer;
}
@@ -129,6 +143,13 @@ void fastd_handshake_handle(fastd_context *ctx, fastd_peer *peer, fastd_buffer b
handshake.type = AS_UINT8(handshake.records[RECORD_HANDSHAKE_TYPE]);
+ if (handshake.records[RECORD_MTU].length == 2) {
+ if (AS_UINT16(handshake.records[RECORD_MTU]) != ctx->conf->mtu) {
+ pr_warn(ctx, "MTU configuration differs with peer %P: local MTU is %u, remote MTU is %u",
+ peer, ctx->conf->mtu, AS_UINT16(handshake.records[RECORD_MTU]));
+ }
+ }
+
if (handshake.type == 1) {
uint8_t reply_code = REPLY_SUCCESS;
uint8_t error_detail = 0;
diff --git a/src/handshake.h b/src/handshake.h
index d540de0..a5b861e 100644
--- a/src/handshake.h
+++ b/src/handshake.h
@@ -42,6 +42,7 @@ typedef enum _fastd_handshake_record_type {
RECORD_PROTOCOL3,
RECORD_PROTOCOL4,
RECORD_PROTOCOL5,
+ RECORD_MTU,
RECORD_MAX,
} fastd_handshake_record_type;
@@ -101,5 +102,21 @@ static inline void fastd_handshake_add_uint8(fastd_context *ctx, fastd_buffer *b
buffer->len += 5;
}
+static inline void fastd_handshake_add_uint16(fastd_context *ctx, fastd_buffer *buffer, fastd_handshake_record_type type, uint16_t value) {
+ if ((uint8_t*)buffer->data + buffer->len + 6 > (uint8_t*)buffer->base + buffer->base_len)
+ exit_bug(ctx, "not enough buffer allocated for handshake");
+
+ uint8_t *dst = (uint8_t*)buffer->data + buffer->len;
+
+ dst[0] = type;
+ dst[1] = type >> 8;
+ dst[2] = 2;
+ dst[3] = 0;
+ dst[4] = value;
+ dst[5] = value >> 8;
+
+ buffer->len += 6;
+}
+
#endif /* _FASTD_HANDSHAKE_H_ */