11 vtl_model_t * new_vtl_model(model_type_t type) {
12 vtl_model_t * model = (vtl_model_t *)malloc(sizeof(vtl_model_t));
20 int initialize_ethernet_model(ethernet_model_t * model, RawEthernetPacket * pkt, int dir = OUTBOUND_PKT) {
21 ASSERT((model != NULL) && (pkt != NULL));
23 printf("initializing ethernet model\n");
24 if (dir == OUTBOUND_PKT) {
25 GET_ETH_DST(pkt->data, model->dst.addr);
26 GET_ETH_SRC(pkt->data, model->src.addr);
27 } else if (dir == INBOUND_PKT) {
28 GET_ETH_DST(pkt->data, model->src.addr);
29 GET_ETH_SRC(pkt->data, model->dst.addr);
34 model->type = GET_ETH_TYPE(pkt->data);
39 int initialize_ip_model(ip_model_t * model, RawEthernetPacket * pkt, int dir) {
40 ASSERT((model != NULL) && (pkt != NULL));
42 if (!is_ip_pkt(pkt)) {
46 if (dir == OUTBOUND_PKT) {
47 model->src.addr = GET_IP_SRC(pkt->data);
48 model->dst.addr = GET_IP_DST(pkt->data);
50 model->src.ip_id = GET_IP_ID(pkt->data);
51 model->src.ttl = GET_IP_TTL(pkt->data);
53 } else if (dir == INBOUND_PKT) {
54 model->src.addr = GET_IP_DST(pkt->data);
55 model->dst.addr = GET_IP_SRC(pkt->data);
57 model->dst.ip_id = GET_IP_ID(pkt->data);
59 model->dst.ttl = GET_IP_TTL(pkt->data);
64 model->version = GET_IP_VERSION(pkt->data);
65 model->proto = GET_IP_PROTO(pkt->data);
67 if (initialize_ethernet_model(&(model->ethernet), pkt, dir) == -1) {
74 int initialize_tcp_model(tcp_model_t * model, RawEthernetPacket * pkt, int dir) {
75 ASSERT((model != NULL) && (pkt != NULL));
79 if (!is_tcp_pkt(pkt)) {
83 parse_tcp_options(&options, pkt);
85 if (dir == OUTBOUND_PKT) {
86 model->src.port = GET_TCP_SRC_PORT(pkt->data);
87 model->dst.port = GET_TCP_DST_PORT(pkt->data);
88 model->src.seq_num = compute_next_tcp_seq_num(pkt);
89 model->src.last_ack = GET_TCP_ACK_NUM(pkt->data);
90 model->src.win = GET_TCP_WIN(pkt->data);
91 model->src.mss = options.mss;
92 model->src.ts = options.local_ts;
95 } else if (dir == INBOUND_PKT) {
96 model->src.port = GET_TCP_DST_PORT(pkt->data);
97 model->dst.port = GET_TCP_SRC_PORT(pkt->data);
98 model->dst.last_ack = GET_TCP_ACK_NUM(pkt->data);
99 model->src.seq_num = 1;
100 model->dst.seq_num = compute_next_tcp_seq_num(pkt);
101 model->dst.win = GET_TCP_WIN(pkt->data);
102 model->dst.mss = options.mss;
103 model->dst.ts = options.local_ts;
110 if (initialize_ip_model(&(model->ip), pkt, dir) == -1) {
117 int initialize_udp_model(udp_model_t * model, RawEthernetPacket * pkt, int dir) {
118 ASSERT((model != NULL) && (pkt != NULL));
120 if (!is_udp_pkt(pkt)) {
124 if (dir == OUTBOUND_PKT) {
125 model->src.port = GET_UDP_SRC_PORT(pkt->data);
126 model->dst.port = GET_UDP_DST_PORT(pkt->data);
127 } else if (dir == INBOUND_PKT) {
128 model->src.port = GET_UDP_DST_PORT(pkt->data);
129 model->dst.port = GET_UDP_SRC_PORT(pkt->data);
134 if (initialize_ip_model(&(model->ip), pkt, dir) == -1) {
143 int initialize_model(vtl_model_t * model, RawEthernetPacket * pkt, int dir) {
144 ASSERT((model != NULL) && (pkt != NULL));
146 if (model->type == TCP_MODEL) {
147 return initialize_tcp_model(&(model->model.tcp_model), pkt, dir);
148 } else if (model->type == UDP_MODEL) {
149 return initialize_udp_model(&(model->model.udp_model), pkt, dir);
150 } else if (model->type == IP_MODEL) {
151 } else if (model->type == ETHERNET_MODEL) {
159 int is_ethernet_model_pkt(ethernet_model_t * model, RawEthernetPacket * pkt) {
160 ASSERT((model != NULL) && (pkt != NULL));
162 if ((memcmp(model->src.addr, ETH_SRC(pkt->data), 6) == 0) &&
163 (memcmp(model->dst.addr, ETH_DST(pkt->data), 6) == 0)) {
165 } else if ((memcmp(model->src.addr, ETH_DST(pkt->data), 6) == 0) &&
166 (memcmp(model->dst.addr, ETH_SRC(pkt->data), 6) == 0)) {
173 int is_ip_model_pkt(ip_model_t * model, RawEthernetPacket * pkt) {
174 ASSERT((model != NULL) && (pkt != NULL));
176 if (!is_ip_pkt(pkt)) {
180 if ((model->src.addr == GET_IP_SRC(pkt->data)) &&
181 (model->dst.addr == GET_IP_DST(pkt->data))) {
183 } else if ((model->src.addr == GET_IP_DST(pkt->data)) &&
184 (model->dst.addr == GET_IP_SRC(pkt->data))) {
192 int is_tcp_model_pkt(tcp_model_t * model, RawEthernetPacket * pkt) {
193 ASSERT((model != NULL) && (pkt != NULL));
196 if (!is_tcp_pkt(pkt)) {
200 if ((ip_ret = is_ip_model_pkt(&(model->ip), pkt)) == INVALID_PKT) {
205 if (ip_ret == OUTBOUND_PKT) {
206 if ((model->src.port == GET_TCP_SRC_PORT(pkt->data)) &&
207 (model->dst.port == GET_TCP_DST_PORT(pkt->data))) {
210 } else if (ip_ret == INBOUND_PKT) {
211 if ((model->src.port == GET_TCP_DST_PORT(pkt->data)) &&
212 (model->dst.port == GET_TCP_SRC_PORT(pkt->data))) {
219 int is_udp_model_pkt(udp_model_t * model, RawEthernetPacket * pkt) {
220 ASSERT((model != NULL) && (pkt != NULL));
223 if (!is_udp_pkt(pkt)) {
227 if ((ip_ret = is_ip_model_pkt(&(model->ip), pkt)) == INVALID_PKT) {
231 if (ip_ret == OUTBOUND_PKT) {
232 if ((model->src.port == GET_UDP_SRC_PORT(pkt->data)) &&
233 (model->dst.port == GET_UDP_DST_PORT(pkt->data))) {
236 } else if (ip_ret == INBOUND_PKT) {
237 if ((model->src.port == GET_UDP_DST_PORT(pkt->data)) &&
238 (model->dst.port == GET_UDP_SRC_PORT(pkt->data))) {
246 int is_model_pkt(vtl_model_t * model, RawEthernetPacket * pkt) {
247 ASSERT((model != NULL) && (pkt != NULL));
249 if (model->type == TCP_MODEL) {
250 return is_tcp_model_pkt(&(model->model.tcp_model), pkt);
257 int sync_ip_model(ip_model_t * model, RawEthernetPacket * pkt) {
258 ASSERT((model != NULL) && (pkt != NULL));
262 ip_ret = is_ip_model_pkt(model, pkt);
264 if (ip_ret == OUTBOUND_PKT) {
265 model->src.ip_id = GET_IP_ID(pkt->data);
266 model->src.ttl = GET_IP_TTL(pkt->data);
267 } else if (ip_ret == INBOUND_PKT) {
268 model->dst.ip_id = GET_IP_ID(pkt->data);
269 model->dst.ttl = GET_IP_TTL(pkt->data);
278 int sync_tcp_model(tcp_model_t * model, RawEthernetPacket * pkt) {
279 ASSERT((model != NULL) && (pkt != NULL));
284 tcp_ret = is_tcp_model_pkt(model, pkt);
286 has_opts = parse_tcp_options(&options, pkt);
288 if (tcp_ret == OUTBOUND_PKT) {
289 model->src.seq_num = compute_next_tcp_seq_num(pkt);
290 model->src.last_ack = GET_TCP_ACK_NUM(pkt->data);
291 model->src.win = GET_TCP_WIN(pkt->data);
293 model->src.mss = options.mss;
294 model->src.ts = options.local_ts;
296 } else if (tcp_ret == INBOUND_PKT) {
297 model->dst.last_ack = GET_TCP_ACK_NUM(pkt->data);
298 model->dst.seq_num = compute_next_tcp_seq_num(pkt);
299 model->dst.win = GET_TCP_WIN(pkt->data);
301 model->dst.mss = options.mss;
302 model->dst.ts = options.local_ts;
308 if (sync_ip_model(&(model->ip), pkt) == -1) {
316 int sync_udp_model(udp_model_t * model, RawEthernetPacket * pkt) {
317 ASSERT((model != NULL) && (pkt != NULL));
320 udp_ret = is_udp_model_pkt(model, pkt);
322 if (udp_ret == INVALID_PKT) {
326 if (sync_ip_model(&(model->ip), pkt) == -1) {
333 int sync_model(vtl_model_t * model, RawEthernetPacket * pkt) {
335 if (model->type == TCP_MODEL) {
336 return sync_tcp_model(&(model->model.tcp_model), pkt);
337 } else if (model->type == IP_MODEL) {
338 return sync_ip_model(&(model->model.ip_model), pkt);
343 int create_empty_ethernet_pkt(ethernet_model_t * model, RawEthernetPacket * pkt, int dir) {
344 if (dir == OUTBOUND_PKT) {
345 SET_ETH_SRC(pkt->data, model->src.addr);
346 SET_ETH_DST(pkt->data, model->dst.addr);
347 } else if (dir == INBOUND_PKT) {
348 SET_ETH_SRC(pkt->data, model->dst.addr);
349 SET_ETH_DST(pkt->data, model->src.addr);
351 SET_ETH_TYPE(pkt->data, model->type);
352 pkt->set_size(ETH_HDR_LEN);
358 int create_empty_ip_pkt(ip_model_t * model, RawEthernetPacket * pkt, int dir) {
359 create_empty_ethernet_pkt(&(model->ethernet), pkt, dir);
361 SET_IP_VERSION(pkt->data, model->version);
362 SET_IP_HDR_LEN(pkt->data, 20);
363 SET_IP_SVC_TYPE(pkt->data, 0);
364 SET_IP_TOTAL_LEN(pkt->data, 20); // WE ARE JUST AN EMPTY PACKET HERE
365 SET_IP_FLAGS(pkt->data, 0);
366 SET_IP_FRAG(pkt->data, 0);
367 SET_IP_PROTO(pkt->data, model->proto);
370 if (dir == OUTBOUND_PKT) {
371 SET_IP_ID(pkt->data, model->src.ip_id + 1);
372 SET_IP_TTL(pkt->data, model->src.ttl);
373 SET_IP_SRC(pkt->data, model->src.addr);
374 SET_IP_DST(pkt->data, model->dst.addr);
375 } else if (dir == INBOUND_PKT) {
376 SET_IP_ID(pkt->data, model->dst.ip_id + 1);
377 SET_IP_TTL(pkt->data, model->dst.ttl);
378 // SET_IP_TTL(pkt->data, 5);
379 SET_IP_SRC(pkt->data, model->dst.addr);
380 SET_IP_DST(pkt->data, model->src.addr);
383 compute_ip_checksum(pkt);
384 pkt->set_size(compute_pkt_size(pkt));
389 int create_empty_tcp_pkt(tcp_model_t * model, RawEthernetPacket * pkt, int dir) {
390 create_empty_ip_pkt(&(model->ip), pkt, dir);
392 SET_TCP_HDR_LEN(pkt->data, 20);
393 SET_TCP_RSVD(pkt->data, 0);
394 SET_TCP_FLAGS(pkt->data, 0);
395 SET_TCP_URG_PTR(pkt->data, 0);
397 if (dir == OUTBOUND_PKT) {
398 SET_TCP_SRC_PORT(pkt->data, model->src.port);
399 SET_TCP_DST_PORT(pkt->data, model->dst.port);
400 SET_TCP_SEQ_NUM(pkt->data, model->src.seq_num);
402 // This is kind of weird
403 // We set the ack number to last ack that was sent on the actual channel
404 // We want to insert packets into the connection without messing things up
405 // So we don't want to ack data that hasn't necessarily been received
406 // Since we're blowing away the seq_num sequence anyway, this might not matter
407 // SET_TCP_ACK_NUM(pkt->data, model->src.last_ack);
408 SET_TCP_ACK_NUM(pkt->data, model->dst.seq_num);
409 SET_TCP_ACK_FLAG(pkt->data);
411 SET_TCP_WIN(pkt->data, model->src.win);
413 } else if (dir == INBOUND_PKT) {
414 SET_TCP_SRC_PORT(pkt->data, model->dst.port);
415 SET_TCP_DST_PORT(pkt->data, model->src.port);
417 SET_TCP_SEQ_NUM(pkt->data, model->dst.seq_num);
419 // This is kind of weird
420 // We set the ack number to last ack that was sent on the actual channel
421 // We want to insert packets into the connection without messing things up
422 // So we don't want to ack data that hasn't necessarily been received
423 // Since we're blowing away the seq_num sequence anyway, this might not matter
424 //SET_TCP_ACK_NUM(pkt->data, model->dst.last_ack);
425 SET_TCP_ACK_NUM(pkt->data, model->src.seq_num);
427 SET_TCP_ACK_FLAG(pkt->data);
429 SET_TCP_WIN(pkt->data, model->dst.win);
433 SET_IP_TOTAL_LEN(pkt->data, GET_IP_HDR_LEN(pkt->data) + GET_TCP_HDR_LEN(pkt->data));
434 pkt->set_size(compute_pkt_size(pkt));
436 compute_ip_checksum(pkt);
438 compute_tcp_checksum(pkt);
440 JRLDBG("tcp_len = %d\n", GET_TCP_HDR_LEN(pkt->data));
443 // Set the ip hdr len
449 int create_empty_udp_pkt(udp_model_t * model, RawEthernetPacket * pkt, int dir) {
450 create_empty_ip_pkt(&(model->ip), pkt, dir);
452 if (dir == OUTBOUND_PKT) {
453 SET_UDP_SRC_PORT(pkt->data, model->src.port);
454 SET_UDP_DST_PORT(pkt->data, model->dst.port);
455 } else if (dir == INBOUND_PKT) {
456 SET_UDP_SRC_PORT(pkt->data, model->dst.port);
457 SET_UDP_DST_PORT(pkt->data, model->src.port);
459 SET_UDP_LEN(pkt->data, 8);
460 SET_IP_TOTAL_LEN(pkt->data, GET_IP_HDR_LEN(pkt->data) + GET_UDP_LEN(pkt->data));
462 compute_ip_checksum(pkt);
463 compute_udp_checksum(pkt);
468 int create_empty_pkt(vtl_model_t * model, RawEthernetPacket * pkt, int dir) {
469 if (model->type == TCP_MODEL) {
470 return create_empty_tcp_pkt(&(model->model.tcp_model), pkt, dir);
471 } else if (model->type == UDP_MODEL) {
472 return create_empty_udp_pkt(&(model->model.udp_model), pkt, dir);
473 } else if (model->type == IP_MODEL) {
474 return create_empty_ip_pkt(&(model->model.ip_model), pkt, dir);
482 void dbg_dump_eth_model(ethernet_model_t * model) {
486 printf("ETHERNET MODEL {\n");
488 printf("\tType: %s\n", get_eth_protocol(model->type));
490 mac_to_string(model->src.addr, src_mac);
491 printf("\tSrc Host {\n");
492 printf("\t\taddr: %s\n", src_mac);
495 mac_to_string(model->dst.addr, dst_mac);
496 printf("\tDST Host {\n");
497 printf("\t\taddr: %s\n", dst_mac);
504 void dbg_dump_ip_model(ip_model_t * model) {
505 dbg_dump_eth_model(&(model->ethernet));
507 printf("IP MODEL {\n");
508 printf("\tVersion: %d\n", model->version);
509 printf("\tProtocol: %s\n", get_ip_protocol(model->proto));
511 printf("\tSrc Host {\n");
512 printf("\t\taddr: %s\n", ip_to_string(model->src.addr));
513 printf("\t\tIP ID: %lu\n", model->src.ip_id);
514 printf("\t\tttl: %d\n", model->src.ttl);
517 printf("\tDst Host {\n");
518 printf("\t\taddr: %s\n", ip_to_string(model->dst.addr));
519 printf("\t\tIP ID: %lu\n", model->dst.ip_id);
520 printf("\t\tttl: %d\n", model->dst.ttl);
526 void dbg_dump_tcp_model(tcp_model_t * model) {
527 dbg_dump_ip_model(&(model->ip));
529 printf("TCP MODEL {\n");
530 printf("\tSrc Host {\n");
531 printf("\t\tport: %hu\n", model->src.port);
532 printf("\t\tseq: %lu\n", (unsigned long)(model->src.seq_num));
533 printf("\t\tlast ack: %lu\n", (unsigned long)(model->src.last_ack));
534 printf("\t\tWin Size: %hu\n", model->src.win);
535 printf("\t\tTimestamp: %lu\n", (unsigned long)(model->src.ts));
536 printf("\t\tMSS: %hu\n", model->src.mss);
540 printf("\tDst Host {\n");
541 printf("\t\tport: %hu\n", model->dst.port);
542 printf("\t\tseq: %lu\n", (unsigned long)(model->dst.seq_num));
543 printf("\t\tlast ack: %lu\n", (unsigned long)(model->dst.last_ack));
544 printf("\t\tWin Size: %hu\n", model->dst.win);
545 printf("\t\tTimestamp: %lu\n", (unsigned long)(model->dst.ts));
546 printf("\t\tMSS: %hu\n", model->dst.mss);
556 void dbg_dump_model(vtl_model_t * model) {
557 if (model->type == TCP_MODEL) {
558 dbg_dump_tcp_model(&(model->model.tcp_model));
559 } else if (model->type == IP_MODEL) {
560 dbg_dump_ip_model(&(model->model.ip_model));