+
+static bool checklength(node_t *source, vpn_packet_t *packet, length_t length) {
+ if(packet->len < length) {
+ ifdebug(TRAFFIC) logger(LOG_WARNING, "Got too short packet from %s (%s)", source->name, source->hostname);
+ return false;
+ } else
+ return true;
+}
+
+void clamp_mss(node_t *source, node_t *via, vpn_packet_t *packet) {
+ int i;
+ int len = ((packet->data[46] >> 4) - 5) * 4;
+
+ for(int i = 0; i < len;) {
+ if(packet->data[54 + i] == 0)
+ break;
+
+ if(packet->data[54 + i] == 1) {
+ i++;
+ continue;
+ }
+
+ if(i > len - 2 || i > len - packet->data[55 + i])
+ break;
+
+ if(packet->data[54 + i] != 2) {
+ if(packet->data[55 + i] < 2)
+ break;
+ i += packet->data[55 + i];
+ continue;
+ }
+
+ if(packet->data[55] != 4)
+ break;
+
+ uint16_t oldmss = packet->data[56 + i] << 8 | packet->data[57 + i];
+ uint16_t newmss = via->mtu - 54;
+ uint16_t csum = packet->data[50] << 8 | packet->data[51];
+
+ if(oldmss <= newmss)
+ break;
+
+ ifdebug(TRAFFIC) logger(LOG_INFO, "Clamping MSS of packet from %s to %s to %d", source->name, via->name, newmss);
+
+ packet->data[56 + i] = newmss >> 8;
+ packet->data[57 + i] = newmss & 0xff;
+ csum ^= 0xffff;
+ csum -= oldmss;
+ csum += newmss;
+ csum ^= 0xffff;
+ packet->data[50] = csum >> 8;
+ packet->data[51] = csum & 0xff;
+ break;
+ }
+}
+
+static void swap_mac_addresses(vpn_packet_t *packet) {
+ mac_t tmp;
+ memcpy(&tmp, &packet->data[0], sizeof tmp);
+ memcpy(&packet->data[0], &packet->data[6], sizeof tmp);
+ memcpy(&packet->data[6], &tmp, sizeof tmp);
+}