1 #include <assert.h> 2 #include <ctype.h> 3 #include <string.h> 4 #ifdef ENABLE_DEBUG_EVENT 5 #include <stdarg.h> 6 #endif 7 8 #include "mtcp.h" 9 #include "mos_api.h" 10 #include "debug.h" 11 #include "config.h" 12 #include "ip_in.h" 13 #include "tcp_out.h" 14 /*----------------------------------------------------------------------------*/ 15 #define MAX(x, y) (((x) > (y)) ? (x) : (y)) 16 #define MIN(x, y) (((x) < (y)) ? (x) : (y)) 17 #define SKIP_SPACES(x) while (*x && isspace((int)*x)) x++; 18 #define SKIP_CHAR(x) while((*x) && !isspace(*x)) x++; 19 20 #define KW_AND "and " 21 #define KW_OR "or " 22 #define KW_NOT "not " 23 #define KW_TCP "tcp" 24 #define KW_NOT_TCP "!tcp" 25 #define KW_NOT_TCP2 "not tcp" 26 #define KW_SRC "src " 27 #define KW_DST "dst " 28 #define KW_HOST "host " 29 #define KW_NET "net " 30 #define KW_MASK "mask " 31 #define KW_PORT "port " 32 #define KW_PORTRANGE "portrange " 33 /*----------------------------------------------------------------------------*/ 34 int 35 IsValidFlowRule(char *cf) 36 { 37 char *word; 38 int skip_word = 0; 39 40 /* '!tcp' or 'not tcp' are also not supported in TCP flow filter */ 41 if (strstr(cf, KW_NOT_TCP) || strstr(cf, KW_NOT_TCP2)) { 42 TRACE_ERROR("'!tcp' or 'not tcp' is not a valid rule for TCP flow monitor.\n"); 43 return FALSE; 44 } 45 46 /* verify that the rule contains flow-related keywords only */ 47 word = cf; 48 SKIP_SPACES(word); 49 50 /* while (browse the rule by words) */ 51 while (*word) { 52 if (skip_word) { 53 skip_word = 0; 54 SKIP_CHAR(word); 55 SKIP_SPACES(word); 56 continue; 57 } 58 /* parse the keyword */ 59 /* case "tcp" "src" "dst" "not' "and" "or" -> move to the next word */ 60 if (!strncmp(word, KW_TCP, sizeof(KW_TCP) - 1) || 61 !strncmp(word, KW_SRC, sizeof(KW_SRC) - 1) || 62 !strncmp(word, KW_DST, sizeof(KW_DST) - 1) || 63 !strncmp(word, KW_NOT, sizeof(KW_NOT) - 1) || 64 !strncmp(word, KW_AND, sizeof(KW_AND) - 1) || 65 !strncmp(word, KW_OR, sizeof(KW_OR) - 1)) { 66 skip_word = 0; 67 } 68 /* case "net" "mask" "port" "portrange" -> skip a word (= param) */ 69 else if (!strncmp(word, KW_HOST, sizeof(KW_HOST) - 1) || 70 !strncmp(word, KW_NET, sizeof(KW_NET) - 1) || 71 !strncmp(word, KW_MASK, sizeof(KW_MASK) - 1) || 72 !strncmp(word, KW_PORT, sizeof(KW_PORT) - 1) || 73 !strncmp(word, KW_PORTRANGE, sizeof(KW_PORTRANGE) - 1)) { 74 skip_word = 1; 75 } 76 /* default (rule has any invalid keyword) -> return error */ 77 else { 78 TRACE_ERROR("Invalid keyword in filter (%s)\n", word); 79 return FALSE; 80 } 81 82 SKIP_CHAR(word); 83 SKIP_SPACES(word); 84 } 85 86 return TRUE; 87 } 88 /*----------------------------------------------------------------------------*/ 89 /* Assign an address range (specified by ft) to monitor via sock */ 90 int 91 mtcp_bind_monitor_filter(mctx_t mctx, int sockid, monitor_filter_t ft) 92 { 93 socket_map_t sock; 94 mtcp_manager_t mtcp; 95 96 mtcp = GetMTCPManager(mctx); 97 if (!mtcp) { 98 errno = EACCES; 99 return -1; 100 } 101 102 /* if filter is not set, do nothing and return */ 103 if (ft == NULL) { 104 TRACE_ERROR("filter not set!\n"); 105 return 0; 106 } 107 108 /* retrieve the socket */ 109 if (sockid < 0 || sockid >= g_config.mos->max_concurrency) { 110 errno = EBADF; 111 TRACE_ERROR("sockid is invalid!\n"); 112 return -1; 113 } 114 sock = &mtcp->msmap[sockid]; 115 116 /* check socket type */ 117 switch (sock->socktype) { 118 case MOS_SOCK_MONITOR_RAW: 119 /* For MONITOR_RAW type, allow any bpf rule */ 120 if (!ft->raw_pkt_filter) { 121 TRACE_ERROR("raw pkt filter is null"); 122 return 0; 123 } 124 if (SET_BPFFILTER(&sock->monitor_listener->raw_pkt_fcode, 125 ft->raw_pkt_filter) < 0) { 126 TRACE_ERROR("Invalid filter expression!\n"); 127 errno = EINVAL; 128 return -1; 129 } 130 break; 131 case MOS_SOCK_MONITOR_STREAM: 132 /* For MONITOR_STREAM_PASSIVE type, restrict to flow-level keywords */ 133 if (ft->stream_syn_filter) { 134 if (!IsValidFlowRule(ft->stream_syn_filter)) { 135 errno = EINVAL; 136 return -1; 137 } 138 if (SET_BPFFILTER(&sock->monitor_listener->stream_syn_fcode, 139 ft->stream_syn_filter) < 0) { 140 TRACE_ERROR("Invalid filter expression!\n"); 141 errno = EINVAL; 142 return -1; 143 } 144 } 145 if (ft->stream_orphan_filter) { 146 if (!IsValidFlowRule(ft->stream_orphan_filter)) { 147 errno = EINVAL; 148 return -1; 149 } 150 if (SET_BPFFILTER(&sock->monitor_listener->stream_orphan_fcode, 151 ft->stream_orphan_filter) < 0) { 152 TRACE_ERROR("Invalid filter expression!\n"); 153 errno = EINVAL; 154 return -1; 155 } 156 } 157 break; 158 default: 159 /* return error for other socket types */ 160 errno = ENOPROTOOPT; 161 TRACE_ERROR("Invalid sock type!\n"); 162 return -1; 163 } 164 165 return 0; 166 } 167 /*----------------------------------------------------------------------------*/ 168 void 169 mtcp_app_join(mctx_t mctx) 170 { 171 mtcp_manager_t mtcp = GetMTCPManager(mctx); 172 if (!mtcp) return; 173 174 RunPassiveLoop(mtcp); 175 return; 176 } 177 /*----------------------------------------------------------------------------*/ 178 /* Callback only functions */ 179 /*----------------------------------------------------------------------------*/ 180 void 181 mtcp_set_uctx(mctx_t mctx, int msock, void *uctx) 182 { 183 mtcp_manager_t mtcp; 184 185 mtcp = GetMTCPManager(mctx); 186 if (!mtcp) { 187 return; 188 } 189 190 /* check if the calling thread is in MOS context */ 191 if (mtcp->ctx->thread != pthread_self()) 192 return; 193 194 if (msock < 0 || msock >= g_config.mos->max_concurrency) { 195 TRACE_API("Socket id %d out of range.\n", msock); 196 errno = EBADF; 197 return; 198 } 199 200 socket_map_t socket = &mtcp->msmap[msock]; 201 if (socket->socktype == MOS_SOCK_MONITOR_STREAM_ACTIVE) 202 socket->monitor_stream->uctx = uctx; 203 else if (socket->socktype == MOS_SOCK_MONITOR_STREAM || 204 socket->socktype == MOS_SOCK_MONITOR_RAW) 205 socket->monitor_listener->uctx = uctx; 206 } 207 /*----------------------------------------------------------------------------*/ 208 void * 209 mtcp_get_uctx(mctx_t mctx, int msock) 210 { 211 mtcp_manager_t mtcp; 212 213 mtcp = GetMTCPManager(mctx); 214 if (!mtcp) { 215 errno = EACCES; 216 return NULL; 217 } 218 219 /* check if the calling thread is in MOS context */ 220 if (mtcp->ctx->thread != pthread_self()) { 221 errno = EPERM; 222 return NULL; 223 } 224 225 if (msock < 0 || msock >= g_config.mos->max_concurrency) { 226 TRACE_API("Socket id %d out of range.\n", msock); 227 errno = EBADF; 228 return NULL; 229 } 230 231 socket_map_t socket = &mtcp->msmap[msock]; 232 if (socket->socktype == MOS_SOCK_MONITOR_STREAM_ACTIVE) 233 return socket->monitor_stream->uctx; 234 else if (socket->socktype == MOS_SOCK_MONITOR_STREAM || 235 socket->socktype == MOS_SOCK_MONITOR_RAW) 236 return socket->monitor_listener->uctx; 237 else 238 return NULL; 239 } 240 /*----------------------------------------------------------------------------*/ 241 ssize_t 242 mtcp_peek(mctx_t mctx, int msock, int side, char *buf, size_t len) 243 { 244 int copylen, rc; 245 struct tcp_stream *cur_stream; 246 mtcp_manager_t mtcp; 247 socket_map_t sock; 248 249 copylen = rc = 0; 250 mtcp = GetMTCPManager(mctx); 251 if (!mtcp) { 252 errno = EACCES; 253 return -1; 254 } 255 256 /* check if the calling thread is in MOS context */ 257 if (mtcp->ctx->thread != pthread_self()) { 258 errno = EPERM; 259 return -1; 260 } 261 262 /* check if the socket is monitor stream */ 263 sock = &mtcp->msmap[msock]; 264 if (sock->socktype != MOS_SOCK_MONITOR_STREAM_ACTIVE) { 265 TRACE_DBG("Invalid socket type!\n"); 266 errno = EBADF; 267 return -1; 268 } 269 270 if (side != MOS_SIDE_CLI && side != MOS_SIDE_SVR) { 271 TRACE_ERROR("Invalid side requested!\n"); 272 exit(EXIT_FAILURE); 273 return -1; 274 } 275 276 struct tcp_stream *mstrm = sock->monitor_stream->stream; 277 cur_stream = (side == mstrm->side) ? mstrm : mstrm->pair_stream; 278 279 if (!cur_stream || !cur_stream->buffer_mgmt) { 280 TRACE_DBG("Stream is NULL!! or buffer management is disabled\n"); 281 errno = EINVAL; 282 return -1; 283 } 284 285 /* Check if the read was not just due to syn-ack recv */ 286 if (cur_stream->rcvvar != NULL && 287 cur_stream->rcvvar->rcvbuf != NULL) { 288 tcprb_t *rcvbuf = cur_stream->rcvvar->rcvbuf; 289 loff_t *poff = &sock->monitor_stream->peek_offset[cur_stream->side]; 290 291 rc = tcprb_ppeek(rcvbuf, (uint8_t *)buf, len, *poff); 292 if (rc < 0) { 293 errno = ENODATA; 294 return -1; 295 } 296 297 *poff += rc; 298 UNUSED(copylen); 299 300 return rc; 301 } else { 302 TRACE_DBG("Stream hasn't yet been initialized!\n"); 303 rc = 0; 304 } 305 306 return rc; 307 } 308 /*----------------------------------------------------------------------------*/ 309 /** 310 * Copies from the frags.. returns no. of bytes copied to buf 311 */ 312 static inline int 313 ExtractPayloadFromFrags(struct tcp_ring_buffer *rcvbuf, char *buf, 314 size_t count, off_t seq_num) 315 { 316 int cpbytesleft; 317 struct fragment_ctx *it; 318 319 it = rcvbuf->fctx; 320 cpbytesleft = count; 321 /* go through each frag */ 322 while (it) { 323 /* first check whether sequent number matches */ 324 if (TCP_SEQ_BETWEEN(seq_num, it->seq, it->seq + it->len)) { 325 /* copy buf starting from seq# seq_num */ 326 /* copy the MIN of seq-range and bytes to be copied */ 327 memcpy(buf + count - cpbytesleft, 328 rcvbuf->head + seq_num - rcvbuf->head_seq, 329 MIN(it->len - (seq_num - it->seq), cpbytesleft)); 330 /* update target seq num */ 331 seq_num += it->len - (seq_num - it->seq); 332 /* update cpbytes left */ 333 cpbytesleft -= it->len - (seq_num - it->seq); 334 if (cpbytesleft == 0) 335 break; 336 } 337 it = it->next; 338 } 339 340 count -= cpbytesleft; 341 342 /* return number of bytes copied */ 343 return count; 344 } 345 /*----------------------------------------------------------------------------*/ 346 /* Please see in-code comments for description */ 347 ssize_t 348 mtcp_ppeek(mctx_t mctx, int msock, int side, 349 char *buf, size_t count, uint64_t off) 350 { 351 mtcp_manager_t mtcp; 352 struct tcp_stream *cur_stream; 353 int rc; 354 socket_map_t sock; 355 356 mtcp = GetMTCPManager(mctx); 357 if (!mtcp) { 358 errno = EACCES; 359 goto ppeek_error; 360 } 361 362 /* check if the calling thread is in MOS context */ 363 if (mtcp->ctx->thread != pthread_self()) { 364 errno = EPERM; 365 goto ppeek_error; 366 } 367 368 /* check if the socket is monitor stream */ 369 sock = &mtcp->msmap[msock]; 370 if (sock->socktype != MOS_SOCK_MONITOR_STREAM_ACTIVE) { 371 TRACE_DBG("Invalid socket type!\n"); 372 errno = ESOCKTNOSUPPORT; 373 goto ppeek_error; 374 } 375 376 if (side != MOS_SIDE_CLI && side != MOS_SIDE_SVR) { 377 TRACE_ERROR("Invalid side requested!\n"); 378 exit(EXIT_FAILURE); 379 return -1; 380 } 381 382 struct tcp_stream *mstrm = sock->monitor_stream->stream; 383 cur_stream = (side == mstrm->side) ? mstrm : mstrm->pair_stream; 384 385 if (!cur_stream || !cur_stream->buffer_mgmt) { 386 TRACE_DBG("Stream is either NULL or ring buffer is not managed!!\n"); 387 errno = EACCES; 388 goto ppeek_error; 389 } 390 391 rc = 0; 392 /* Check if the read was not just due to syn-ack recv */ 393 if (cur_stream->rcvvar != NULL && 394 cur_stream->rcvvar->rcvbuf != NULL) { 395 tcprb_t *rcvbuf = cur_stream->rcvvar->rcvbuf; 396 return tcprb_ppeek(rcvbuf, (uint8_t *)buf, count, off); 397 } else { 398 errno = EPERM; 399 goto ppeek_error; 400 } 401 402 return rc; 403 404 ppeek_error: 405 return -1; 406 } 407 /*----------------------------------------------------------------------------*/ 408 #ifdef MTCP_CB_GETCURPKT_CREATE_COPY 409 static __thread unsigned char local_frame[ETHERNET_FRAME_LEN]; 410 inline struct pkt_info * 411 ClonePacketCtx(struct pkt_info *to, unsigned char *frame, struct pkt_info *from) 412 { 413 /* memcpy the entire ethernet frame */ 414 assert(from); 415 assert(from->eth_len > 0); 416 assert(from->eth_len <= ETHERNET_FRAME_LEN); 417 memcpy(frame, from->ethh, from->eth_len); 418 419 /* only memcpy till the last field before ethh */ 420 /* memcpy(to, from, PCTX_COPY_LEN); */ 421 memcpy(to, from, PKT_INFO_LEN); 422 /* set iph */ 423 to->ethh = (struct ethhdr *)frame; 424 /* set iph */ 425 to->iph = from->iph ? 426 (struct iphdr *)((uint8_t *)(frame + ETHERNET_HEADER_LEN)) : NULL; 427 if (to->iph) { 428 /* set tcph */ 429 to->tcph = from->tcph ? 430 (struct tcphdr *)(((uint8_t *)(to->iph)) + (to->iph->ihl<<2)) : NULL; 431 if (to->tcph) 432 /* set payload */ 433 to->payload = from->tcph ? 434 ((uint8_t *)(to->tcph) + (to->tcph->doff<<2)) : NULL; 435 } 436 return to; 437 } 438 /*----------------------------------------------------------------------------*/ 439 int 440 mtcp_getlastpkt(mctx_t mctx, int sock, int side, struct pkt_info *pkt) 441 { 442 mtcp_manager_t mtcp; 443 socket_map_t socket; 444 struct pkt_ctx *cur_pkt_ctx; 445 446 mtcp = GetMTCPManager(mctx); 447 if (!mtcp) { 448 errno = EACCES; 449 return -1; 450 } 451 452 /* check if the calling thread is in MOS context */ 453 if (mtcp->ctx->thread != pthread_self()) { 454 errno = EPERM; 455 return -1; 456 } 457 458 /* check if the socket is monitor stream */ 459 socket = &mtcp->msmap[sock]; 460 #ifndef RECORDPKT_PER_STREAM 461 switch (socket->socktype) { 462 case MOS_SOCK_MONITOR_STREAM_ACTIVE: 463 case MOS_SOCK_MONITOR_RAW: 464 case MOS_SOCK_MONITOR_STREAM: 465 if (mtcp->pctx == NULL) { 466 errno = EACCES; 467 return -1; 468 } 469 cur_pkt_ctx = mtcp->pctx; 470 break; 471 default: 472 TRACE_DBG("Invalid socket type!\n"); 473 errno = EBADF; 474 return -1; 475 } 476 #else /* RECORDPKT_PER_STREAM */ 477 struct tcp_stream *cur_stream; 478 if (socket->socktype == MOS_SOCK_MONITOR_STREAM_ACTIVE) { 479 if (side != MOS_SIDE_CLI && side != MOS_SIDE_SVR) { 480 TRACE_ERROR("Invalid side requested!\n"); 481 exit(EXIT_FAILURE); 482 return -1; 483 } 484 485 struct tcp_stream *mstrm = socket->monitor_stream->stream; 486 cur_stream = (side == mstrm->side) ? mstrm : mstrm->pair_stream; 487 488 cur_pkt_ctx = &cur_stream->last_pctx; 489 if (!cur_pkt_ctx->p.ethh) { 490 errno = ENODATA; 491 return -1; 492 } 493 } else if (socket->socktype == MOS_SOCK_MONITOR_RAW) { 494 cur_pkt_ctx = mtcp->pctx; 495 } else if (socket->socktype == MOS_SOCK_MONITOR_STREAM) { 496 /* 497 * if it is a monitor socket, then this means that 498 * this is a request for an orphan tcp packet 499 */ 500 cur_pkt_ctx = mtcp->pctx; 501 } else { 502 TRACE_DBG("Invalid socket type!\n"); 503 errno = EBADF; 504 return -1; 505 } 506 #endif /* !RECORDPKT_PER_STREAM */ 507 ClonePacketCtx(pkt, local_frame, &(cur_pkt_ctx->p)); 508 return 0; 509 } 510 #else 511 /*----------------------------------------------------------------------------*/ 512 int 513 mtcp_getlastpkt(mctx_t mctx, int sock, int side, struct pkt_ctx **pctx) 514 { 515 mtcp_manager_t mtcp; 516 517 mtcp = GetMTCPManager(mctx); 518 if (!mtcp) { 519 errno = EACCES; 520 return -1; 521 } 522 523 /* check if the calling thread is in MOS context */ 524 if (mtcp->ctx->thread != pthread_self()) { 525 errno = EPERM; 526 return -1; 527 } 528 /* just pass direct pointer */ 529 *pctx = mtcp->pctx; 530 531 return 0; 532 } 533 #endif 534 /*----------------------------------------------------------------------------*/ 535 int 536 mtcp_sendpkt(mctx_t mctx, int sock, const struct pkt_info *pkt) 537 { 538 mtcp_manager_t mtcp; 539 socket_map_t socket; 540 541 mtcp = GetMTCPManager(mctx); 542 if (!mtcp || !pkt) { 543 errno = EACCES; 544 return -1; 545 } 546 547 /* check if the calling thread is in MOS context */ 548 if (mtcp->ctx->thread != pthread_self()) { 549 errno = EPERM; 550 return -1; 551 } 552 553 /* check if the socket is monitor stream */ 554 socket = &mtcp->msmap[sock]; 555 556 if (!(pkt->iph) || !(pkt->tcph)) { 557 errno = ENODATA; 558 TRACE_INFO("mtcp_sendpkt() only supports TCP packet for now.\n"); 559 return -1; 560 } 561 562 if (socket->socktype == MOS_SOCK_MONITOR_STREAM_ACTIVE) { 563 SendTCPPacketStandalone(mtcp, 564 pkt->iph->saddr, pkt->tcph->source, 565 pkt->iph->daddr, pkt->tcph->dest, 566 htonl(pkt->tcph->seq), htonl(pkt->tcph->ack_seq), 567 ntohs(pkt->tcph->window), TCP_FLAG_ACK, 568 pkt->payload, pkt->payloadlen, 569 socket->monitor_stream->stream->rcvvar->ts_recent, 570 socket->monitor_stream->stream->rcvvar->ts_lastack_rcvd, 571 pkt->iph->id, pkt->in_ifidx); 572 573 574 } 575 576 return 0; 577 } 578 /*----------------------------------------------------------------------------*/ 579 /** Disable events from the monitor stream socket 580 * @param [in] mtcp: mtcp_manager 581 * @param [in] sock: socket 582 * 583 * returns 0 on success, -1 on failure 584 * 585 * This is used for flow management based monitoring sockets 586 */ 587 int 588 RemoveMonitorEvents(mtcp_manager_t mtcp, socket_map_t socket, int side) 589 { 590 struct mon_stream *mstream; 591 struct mon_listener *mlistener; 592 593 if (mtcp == NULL) { 594 TRACE_DBG("mtcp is not defined!!!\n"); 595 errno = EACCES; 596 return -1; 597 } 598 599 switch (socket->socktype) { 600 case MOS_SOCK_MONITOR_STREAM_ACTIVE: 601 mstream = socket->monitor_stream; 602 if (mstream == NULL) { 603 TRACE_ERROR("Mon Stream does not exist!\n"); 604 /* exit(-1); */ 605 errno = ENODATA; 606 return -1; 607 } 608 609 if (side == MOS_SIDE_SVR) mstream->server_mon = 0; 610 else if (side == MOS_SIDE_CLI) mstream->client_mon = 0; 611 612 if (mstream->server_mon == 0 && mstream->client_mon == 0) { 613 #ifdef NEWEV 614 /* 615 * if stree_dontcare is NULL, then we know that all 616 * events have already been disabled 617 */ 618 if (mstream->stree_pre_rcv != NULL) { 619 stree_dec_ref(mtcp->ev_store, mstream->stree_dontcare); 620 stree_dec_ref(mtcp->ev_store, mstream->stree_pre_rcv); 621 stree_dec_ref(mtcp->ev_store, mstream->stree_post_snd); 622 623 mstream->stree_dontcare = NULL; 624 mstream->stree_pre_rcv = NULL; 625 mstream->stree_post_snd = NULL; 626 } 627 #else 628 /* no error checking over here.. 629 * but its okay.. this code is 630 * deprecated 631 */ 632 CleanupEvP(&mstream->dontcare_evp); 633 CleanupEvP(&mstream->pre_tcp_evp); 634 CleanupEvP(&mstream->post_tcp_evp); 635 #endif 636 } 637 break; 638 case MOS_SOCK_MONITOR_STREAM: 639 mlistener = socket->monitor_listener; 640 if (mlistener == NULL) { 641 TRACE_ERROR("Mon listener does not exist!\n"); 642 errno = ENODATA; 643 return -1; 644 } 645 646 if (side == MOS_SIDE_SVR) mlistener->server_mon = 0; 647 else if (side == MOS_SIDE_CLI) mlistener->client_mon = 0; 648 649 if (mlistener->server_mon == 0 && mlistener->client_mon == 0) { 650 #ifdef NEWEV 651 /* 652 * if stree_dontcare is NULL, then we know that all 653 * events have already been disabled 654 */ 655 if (mlistener->stree_pre_rcv != NULL) { 656 stree_dec_ref(mtcp->ev_store, mlistener->stree_dontcare); 657 stree_dec_ref(mtcp->ev_store, mlistener->stree_pre_rcv); 658 stree_dec_ref(mtcp->ev_store, mlistener->stree_post_snd); 659 660 mlistener->stree_dontcare = NULL; 661 mlistener->stree_pre_rcv = NULL; 662 mlistener->stree_post_snd = NULL; 663 } 664 #else 665 /* no error checking over here.. 666 * but its okay.. this code is 667 * deprecated 668 */ 669 CleanupEvB(mtcp, &mlistener->dontcare_evb); 670 CleanupEvB(mtcp, &mlistener->pre_tcp_evb); 671 CleanupEvB(mtcp, &mlistener->post_tcp_evb); 672 #endif 673 } 674 break; 675 default: 676 TRACE_ERROR("Invalid socket type!\n"); 677 } 678 679 return 0; 680 } 681 /*----------------------------------------------------------------------------*/ 682 /** 683 * Disable monitoring based on side variable. 684 */ 685 int 686 mtcp_cb_stop(mctx_t mctx, int sock, int side) 687 { 688 mtcp_manager_t mtcp; 689 socket_map_t socket; 690 struct tcp_stream *stream; 691 struct socket_map *walk; 692 uint8_t mgmt; 693 694 mtcp = GetMTCPManager(mctx); 695 if (!mtcp) { 696 errno = EACCES; 697 return -1; 698 } 699 700 socket = &mtcp->msmap[sock]; 701 702 /* works for both monitor listener and stream sockets */ 703 RemoveMonitorEvents(mtcp, socket, side); 704 705 /* passive monitoring socket is not connected to any stream */ 706 if (socket->socktype == MOS_SOCK_MONITOR_STREAM) 707 return 0; 708 709 if (side == MOS_SIDE_CLI) { 710 /* see if the associated stream requires monitoring any more */ 711 stream = (socket->monitor_stream->stream->side == MOS_SIDE_CLI) ? 712 socket->monitor_stream->stream : 713 socket->monitor_stream->stream->pair_stream; 714 715 mgmt = 0; 716 SOCKQ_FOREACH_START(walk, &stream->msocks) { 717 if (walk->monitor_stream->client_mon == 1) { 718 mgmt = 1; 719 break; 720 } 721 } SOCKQ_FOREACH_END; 722 /* if all streams have mgmt off, then tag the stream for destruction */ 723 if (mgmt == 0) { 724 stream = (socket->monitor_stream->stream->side == MOS_SIDE_CLI) ? 725 socket->monitor_stream->stream : 726 socket->monitor_stream->stream->pair_stream; 727 stream->status_mgmt = 0; 728 } 729 } 730 731 if (side == MOS_SIDE_SVR) { 732 /* see if the associated stream requires monitoring any more */ 733 stream = (socket->monitor_stream->stream->side == MOS_SIDE_SVR) ? 734 socket->monitor_stream->stream : 735 socket->monitor_stream->stream->pair_stream; 736 mgmt = 0; 737 SOCKQ_FOREACH_START(walk, &stream->msocks) { 738 if (walk->monitor_stream->server_mon == 1) { 739 mgmt = 1; 740 break; 741 } 742 } SOCKQ_FOREACH_END; 743 /* if all streams have mgmt off, then tag the stream for destruction */ 744 if (mgmt == 0) { 745 stream = (socket->monitor_stream->stream->side == MOS_SIDE_SVR) ? 746 socket->monitor_stream->stream : 747 socket->monitor_stream->stream->pair_stream; 748 stream->status_mgmt = 0; 749 } 750 } 751 752 return 0; 753 } 754 /*----------------------------------------------------------------------------*/ 755 /** 756 * send a RST packet to the TCP stream (uni-directional) 757 */ 758 static inline void 759 SendRSTPacketStandalone(mtcp_manager_t mtcp, struct tcp_stream *stream) { 760 SendTCPPacketStandalone(mtcp, 761 stream->saddr, stream->sport, stream->daddr, stream->dport, 762 stream->snd_nxt, stream->rcv_nxt, 0, TCP_FLAG_RST | TCP_FLAG_ACK, 763 NULL, 0, mtcp->cur_ts, 0, 0, -1); 764 } 765 /*----------------------------------------------------------------------------*/ 766 /** 767 * Reset the connection (send RST packets to both sides) 768 */ 769 int 770 mtcp_reset_conn(mctx_t mctx, int sock) 771 { 772 mtcp_manager_t mtcp; 773 socket_map_t socket; 774 775 mtcp = GetMTCPManager(mctx); 776 if (!mtcp) { 777 errno = EACCES; 778 return -1; 779 } 780 781 socket = &mtcp->msmap[sock]; 782 783 /* passive monitoring socket is not connected to any stream */ 784 if (socket->socktype == MOS_SOCK_MONITOR_STREAM) { 785 errno = EINVAL; 786 return -1; 787 } 788 789 /* send RST packets to the both sides */ 790 SendRSTPacketStandalone(mtcp, socket->monitor_stream->stream); 791 SendRSTPacketStandalone(mtcp, socket->monitor_stream->stream->pair_stream); 792 793 return 0; 794 } 795 /*----------------------------------------------------------------------------*/ 796 uint32_t 797 mtcp_cb_get_ts(mctx_t mctx) 798 { 799 mtcp_manager_t mtcp; 800 801 mtcp = GetMTCPManager(mctx); 802 if (!mtcp) { 803 TRACE_DBG("Can't access MTCP manager!\n"); 804 errno = EACCES; 805 return 0; 806 } 807 808 /* check if the calling thread is in MOS context */ 809 if (mtcp->ctx->thread != pthread_self()) { 810 errno = EPERM; 811 return 0; 812 } 813 814 return TS_TO_USEC(mtcp->cur_ts); 815 } 816 /*----------------------------------------------------------------------------*/ 817 /* Macros related to getpeername */ 818 #define TILL_SVRADDR offsetof(struct sockaddr_in, sin_zero) 819 #define TILL_SVRPORT offsetof(struct sockaddr_in, sin_addr) 820 #define TILL_SVRFAMILY offsetof(struct sockaddr_in, sin_port) 821 #define TILL_CLIADDR sizeof(struct sockaddr) + TILL_SVRADDR 822 #define TILL_CLIPORT sizeof(struct sockaddr) + TILL_SVRPORT 823 #define TILL_CLIFAMILY sizeof(struct sockaddr) + TILL_SVRFAMILY 824 825 int 826 mtcp_getpeername(mctx_t mctx, int sockfd, struct sockaddr *saddr, 827 socklen_t *addrlen, int side) 828 { 829 mtcp_manager_t mtcp; 830 socket_map_t socket; 831 struct tcp_stream *stream; 832 struct sockaddr_in *sin; 833 int rc; 834 835 mtcp = GetMTCPManager(mctx); 836 if (!mtcp) { 837 TRACE_DBG("Can't access MTCP manager!\n"); 838 errno = EACCES; 839 return -1; 840 } 841 842 /* check if sockfd is within limits */ 843 if (sockfd < 0 || sockfd >= g_config.mos->max_concurrency) { 844 TRACE_API("Socket id %d out of range.\n", sockfd); 845 errno = EBADF; 846 return -1; 847 } 848 849 /* check if the calling thread is in MOS context */ 850 if (mtcp->ctx->thread != pthread_self()) { 851 errno = EPERM; 852 return -1; 853 } 854 855 socket = &mtcp->msmap[sockfd]; 856 sin = (struct sockaddr_in *)saddr; 857 rc = 0; 858 859 /* retrieve both streams */ 860 stream = socket->monitor_stream->stream; 861 862 if (side != stream->side) 863 stream = stream->pair_stream; 864 865 if (stream == NULL) { 866 errno = ENOTCONN; 867 return -1; 868 } 869 870 /* reset to 2 * sizeof(struct sockaddr) if addrlen is too big */ 871 if (*addrlen > 2 * sizeof(struct sockaddr)) 872 *addrlen = 2 * sizeof(struct sockaddr); 873 874 /* according per manpage, address can be truncated */ 875 switch (*addrlen) { 876 case (2 * sizeof(struct sockaddr)): 877 case TILL_CLIADDR: 878 sin[1].sin_addr.s_addr = stream->side == MOS_SIDE_SVR ? 879 stream->daddr : stream->saddr; 880 case TILL_CLIPORT: 881 sin[1].sin_port = stream->side == MOS_SIDE_SVR ? 882 stream->dport : stream->sport; 883 case TILL_CLIFAMILY: 884 sin[1].sin_family = AF_INET; 885 case (sizeof(struct sockaddr)): 886 case TILL_SVRADDR: 887 sin->sin_addr.s_addr = stream->side == MOS_SIDE_SVR ? 888 stream->saddr : stream->daddr; 889 case TILL_SVRPORT: 890 sin->sin_port = stream->side == MOS_SIDE_SVR ? 891 stream->sport : stream->dport; 892 case TILL_SVRFAMILY: 893 sin->sin_family = AF_INET; 894 break; 895 default: 896 rc = -1; 897 *addrlen = 0xFFFF; 898 errno = EINVAL; 899 } 900 901 return rc; 902 } 903 /*----------------------------------------------------------------------------*/ 904 int 905 mtcp_setlastpkt(mctx_t mctx, int sock, int side, off_t offset, 906 byte *data, uint16_t datalen, int option) 907 { 908 mtcp_manager_t mtcp; 909 struct pkt_ctx *cur_pkt_ctx; 910 struct ethhdr *ethh; 911 struct iphdr *iph; 912 struct tcphdr *tcph; 913 unsigned char *payload; 914 915 #if 0 916 socket_map_t socket; 917 struct tcp_stream *cur_stream; 918 #endif 919 920 /* checking if mtcp is valid */ 921 mtcp = GetMTCPManager(mctx); 922 if (!mtcp) { 923 errno = EACCES; 924 TRACE_ERROR("Invalid mtcp!\n"); 925 return -1; 926 } 927 928 /* check if the calling thread is in MOS context */ 929 if (mtcp->ctx->thread != pthread_self()) { 930 errno = EPERM; 931 TRACE_ERROR("Invalid thread id!\n"); 932 return -1; 933 } 934 935 #if 0 936 /* check if the socket is monitor stream */ 937 socket = &mtcp->msmap[sock]; 938 if (socket->socktype == MOS_SOCK_MONITOR_STREAM_ACTIVE) { 939 if (side != MOS_SIDE_CLI && side != MOS_SIDE_SVR) { 940 TRACE_ERROR("Invalid side requested!\n"); 941 exit(EXIT_FAILURE); 942 return -1; 943 } 944 945 struct tcp_stream *mstrm = socket->monitor_stream->stream; 946 cur_stream = (side == mstrm->side) ? mstrm : mstrm->pair_stream; 947 948 if (!cur_stream->allow_pkt_modification) 949 return -1; 950 } else if (socket->socktype != MOS_SOCK_MONITOR_RAW) { 951 TRACE_ERROR("Invalid socket type!\n"); 952 exit(EXIT_FAILURE); 953 return -1; 954 } 955 #endif 956 957 /* see if cur_pkt_ctx is valid */ 958 cur_pkt_ctx = mtcp->pctx; 959 if (cur_pkt_ctx == NULL) { 960 TRACE_ERROR("pctx is NULL!\n"); 961 errno = ENODATA; 962 return -1; 963 } 964 965 /* check if offset is valid */ 966 if (offset < 0) { 967 TRACE_ERROR("Invalid offset position!\n"); 968 errno = EINVAL; 969 return -1; 970 } 971 972 if (__builtin_popcount(option & (MOS_DROP | MOS_CHOMP | 973 MOS_INSERT | MOS_OVERWRITE)) != 1) { 974 TRACE_ERROR("mtcp_setlastpkt() function only allows one of " 975 "(MOS_DROP | MOS_CHOMP | MOS_INSERT | MOS_OVERWRITE) " 976 "to be set at a time.\n"); 977 errno = EAGAIN; 978 return -1; 979 } 980 981 /* drop pkt has the highest priority */ 982 if (option & MOS_DROP) { 983 mtcp->pctx->forward = 0; 984 return 0; 985 } else if (option & MOS_ETH_HDR) { 986 /* validity test */ 987 if ((ethh=cur_pkt_ctx->p.ethh) == NULL || 988 offset + datalen > sizeof(struct ethhdr)) { 989 TRACE_ERROR("Ethernet setting has gone out of bounds " 990 "(offset: %ld, datalen: %d)\n", 991 offset, datalen); 992 errno = EINVAL; 993 return -1; 994 } 995 if (option & MOS_CHOMP) { 996 TRACE_ERROR("Illegal call. " 997 "Ethernet header can't be chopped down!\n"); 998 errno = EACCES; 999 return -1; 1000 } else if (option & MOS_INSERT) { 1001 TRACE_ERROR("Illegal call. " 1002 "Ethernet header can't be extended!\n"); 1003 errno = EACCES; 1004 return -1; 1005 } else /* if (option & MOS_OVERWRITE) */ { 1006 memcpy((uint8_t *)ethh + offset, data, datalen); 1007 } 1008 /* iph, tcph, and payload do not need to change */ 1009 } else if (option & MOS_IP_HDR) { 1010 /* validity test */ 1011 if (cur_pkt_ctx->p.ethh == NULL || 1012 cur_pkt_ctx->p.ethh->h_proto != ntohs(ETH_P_IP) || 1013 (iph=(struct iphdr *)(cur_pkt_ctx->p.ethh + 1)) == NULL) { 1014 TRACE_ERROR("ethh or iph are out of bounds\n"); 1015 errno = EACCES; 1016 return -1; 1017 } 1018 if (option & MOS_OVERWRITE) { 1019 if (offset + datalen > (iph->ihl<<2)) { 1020 TRACE_ERROR("IP setting has gone out of bounds " 1021 "(offset: %ld, datalen: %d)\n", 1022 offset, datalen); 1023 errno = EINVAL; 1024 return -1; 1025 } 1026 memcpy((uint8_t *)iph + offset, data, datalen); 1027 } 1028 if (option & MOS_CHOMP) { 1029 memmove((uint8_t *)iph + offset, 1030 (uint8_t *)iph + offset + datalen, 1031 cur_pkt_ctx->p.ip_len - offset - datalen); 1032 1033 /* iph does not need to change */ 1034 if (iph->protocol == IPPROTO_TCP) { 1035 cur_pkt_ctx->p.tcph = (struct tcphdr *)((uint8_t *)iph + (iph->ihl<<2)); 1036 cur_pkt_ctx->p.payload = (uint8_t *)cur_pkt_ctx->p.tcph + 1037 (cur_pkt_ctx->p.tcph->doff<<2); 1038 } else { 1039 /* reset tcph if iph does not have tcp proto */ 1040 cur_pkt_ctx->p.tcph = NULL; 1041 } 1042 /* update iph total length */ 1043 cur_pkt_ctx->p.ip_len = ntohs(iph->tot_len); 1044 /* update eth frame length */ 1045 cur_pkt_ctx->p.eth_len = cur_pkt_ctx->p.ip_len + sizeof(struct ethhdr); 1046 } else if (option & MOS_INSERT) { 1047 memmove((uint8_t *)iph + offset + datalen, 1048 (uint8_t *)iph + offset + 1, 1049 cur_pkt_ctx->p.ip_len - offset); 1050 memcpy((uint8_t *)iph + offset, 1051 data, datalen); 1052 1053 /* iph does not need to change */ 1054 if (iph->protocol == IPPROTO_TCP) { 1055 cur_pkt_ctx->p.tcph = (struct tcphdr *)((uint8_t *)iph + (iph->ihl<<2)); 1056 cur_pkt_ctx->p.payload = (uint8_t *)cur_pkt_ctx->p.tcph + 1057 (cur_pkt_ctx->p.tcph->doff<<2); 1058 } else { 1059 /* reset tcph if iph does not have tcp proto */ 1060 cur_pkt_ctx->p.tcph = NULL; 1061 } 1062 /* update iph total length */ 1063 cur_pkt_ctx->p.ip_len = ntohs(iph->tot_len); 1064 /* update eth frame length */ 1065 cur_pkt_ctx->p.eth_len = cur_pkt_ctx->p.ip_len + sizeof(struct ethhdr); 1066 } 1067 /* can't update payloadlen because we don't know tcph->doff */ 1068 } else if (option & MOS_TCP_HDR) { 1069 /* validity test */ 1070 iph = (struct iphdr *)(cur_pkt_ctx->p.ethh + 1); 1071 if (iph == NULL || 1072 iph->protocol != IPPROTO_TCP || 1073 (tcph=(struct tcphdr *)((uint8_t *)iph + (iph->ihl<<2))) == NULL) { 1074 TRACE_ERROR("TCP setting has gone out of bounds " 1075 "(offset: %ld, datalen: %d)\n", 1076 offset, datalen); 1077 errno = EINVAL; 1078 return -1; 1079 } 1080 if (option & MOS_OVERWRITE) { 1081 if (offset + datalen > (tcph->doff<<2)) { 1082 TRACE_ERROR("TCP setting has gone out of bounds " 1083 "(offset: %ld, datalen: %d)\n", 1084 offset, datalen); 1085 errno = EINVAL; 1086 return -1; 1087 } 1088 memcpy((uint8_t *)tcph + offset, data, datalen); 1089 /* update tcp seq # */ 1090 cur_pkt_ctx->p.seq = ntohl(tcph->seq); 1091 /* update tcp ack_seq # */ 1092 cur_pkt_ctx->p.ack_seq = ntohl(tcph->ack_seq); 1093 /* update tcp window */ 1094 cur_pkt_ctx->p.window = ntohs(tcph->window); 1095 1096 /* 150422 dhkim TODO: seq and offset are two different form of same 1097 * variable. We also need to update the offset. */ 1098 } 1099 if (option & MOS_CHOMP) { 1100 memmove((uint8_t *)tcph + offset, 1101 (uint8_t *)tcph + offset + datalen, 1102 cur_pkt_ctx->p.payloadlen + (tcph->doff<<2) 1103 - offset - datalen); 1104 /* update payload ptr */ 1105 cur_pkt_ctx->p.payload = (uint8_t *)tcph + (tcph->doff<<2); 1106 } else if (option & MOS_INSERT) { 1107 memmove((uint8_t *)tcph + offset + datalen, 1108 (uint8_t *)tcph + offset + 1, 1109 cur_pkt_ctx->p.payloadlen + (tcph->doff<<2) 1110 - offset); 1111 memcpy((uint8_t *)tcph + offset, data, datalen); 1112 /* update payload ptr */ 1113 cur_pkt_ctx->p.payload = (uint8_t *)tcph + (tcph->doff<<2); 1114 } 1115 } else if (option & MOS_TCP_PAYLOAD) { 1116 iph = (struct iphdr *)(cur_pkt_ctx->p.ethh + 1); 1117 tcph = (struct tcphdr *)((uint8_t *)iph + (iph->ihl<<2)); 1118 payload = (uint8_t *)tcph + (tcph->doff<<2); 1119 if (option & MOS_OVERWRITE) { 1120 if (offset + datalen > ntohs(iph->tot_len) - 1121 (iph->ihl<<2) - (tcph->doff<<2)) { 1122 TRACE_ERROR("Payload setting has gone out of bounds " 1123 "(offset: %ld, datalen: %d)\n", 1124 offset, datalen); 1125 errno = EINVAL; 1126 return -1; 1127 } 1128 memcpy(payload + offset, data, datalen); 1129 } 1130 if (option & MOS_CHOMP) { 1131 memmove(payload + offset, 1132 payload + offset + datalen, 1133 (cur_pkt_ctx->p.payloadlen - 1134 offset - datalen)); 1135 /* update payload length */ 1136 cur_pkt_ctx->p.payloadlen = cur_pkt_ctx->p.ip_len - 1137 (tcph->doff<<2) - (iph->ihl<<2); 1138 } else if (option & MOS_INSERT) { 1139 memmove(payload + offset + datalen, 1140 payload + offset + 1, 1141 cur_pkt_ctx->p.payloadlen - offset); 1142 memcpy(payload + offset, data, datalen); 1143 cur_pkt_ctx->p.payloadlen = cur_pkt_ctx->p.ip_len - 1144 (tcph->doff<<2) - (iph->ihl<<2); 1145 } 1146 } else { 1147 TRACE_ERROR("Invalid option!\n"); 1148 errno = EINVAL; 1149 return -1; 1150 } 1151 1152 /* update ip checksum */ 1153 if (option & MOS_UPDATE_IP_CHKSUM) { 1154 iph = (struct iphdr *)(cur_pkt_ctx->p.ethh + 1); 1155 iph->check = 0; 1156 iph->check = ip_fast_csum(iph, iph->ihl); 1157 } 1158 1159 /* update tcp checksum */ 1160 if (option & MOS_UPDATE_TCP_CHKSUM) { 1161 iph = (struct iphdr *)(cur_pkt_ctx->p.ethh + 1); 1162 tcph = (struct tcphdr *)((uint8_t *)iph + (iph->ihl<<2)); 1163 tcph->check = 0; 1164 tcph->check = TCPCalcChecksum((uint16_t *)tcph, 1165 ntohs(iph->tot_len) - (iph->ihl<<2), 1166 iph->saddr, iph->daddr); 1167 } 1168 return 0; 1169 } 1170 /*----------------------------------------------------------------------------*/ 1171 #if 0 1172 inline int 1173 mtcp_cb_updatecurpkt(mctx_t mctx, off_t offset, unsigned char *data, 1174 uint16_t datalen, int option) 1175 { 1176 return mtcp_setlastpkt(mctx, sock, side, offset, data, datalen, option); 1177 } 1178 #endif 1179 /*----------------------------------------------------------------------------*/ 1180 /** 1181 * THIS IS A DEPRECETED FUNCTION... 1182 */ 1183 int 1184 mtcp_cb_dropcurpkt(mctx_t mctx) 1185 { 1186 mtcp_manager_t mtcp; 1187 1188 /* checking if mtcp is valid */ 1189 mtcp = GetMTCPManager(mctx); 1190 if (!mtcp) { 1191 TRACE_ERROR("Invalid mtcp!\n"); 1192 errno = EACCES; 1193 return -1; 1194 } 1195 1196 /* check if the calling thread is in MOS context */ 1197 if (mtcp->ctx->thread != pthread_self()) { 1198 TRACE_ERROR("Invalid thread id!\n"); 1199 errno = EPERM; 1200 return -1; 1201 } 1202 1203 /* see if cur_pkt_ctx is valid */ 1204 if (mtcp->pctx == NULL) { 1205 TRACE_ERROR("pctx is NULL!\n"); 1206 errno = ENODATA; 1207 return -1; 1208 } 1209 1210 mtcp->pctx->forward = 0; 1211 1212 return 0; 1213 } 1214 /*----------------------------------------------------------------------------*/ 1215 int 1216 mtcp_set_debug_string(mtcp_manager_t mtcp, const char *fmt, ...) 1217 { 1218 #ifdef ENABLE_DEBUG_EVENT 1219 va_list args; 1220 int i; 1221 1222 assert(mtcp); 1223 1224 if (fmt == NULL) { 1225 mtcp->dbg_buf[0] = '\0'; 1226 return 0; 1227 } 1228 1229 va_start(args, fmt); 1230 i = vsnprintf(mtcp->dbg_buf, DBG_BUF_LEN - 1, fmt, args); 1231 va_end(args); 1232 1233 return i; 1234 #else 1235 return -1; 1236 #endif /* ENABLE_DEBUG_EVENT */ 1237 } 1238 /*----------------------------------------------------------------------------*/ 1239 int 1240 mtcp_get_debug_string(mctx_t mctx, char *buf, int len) 1241 { 1242 #ifdef ENABLE_DEBUG_EVENT 1243 mtcp_manager_t mtcp; 1244 int copylen; 1245 1246 if (len < 0) 1247 return -1; 1248 else if (len == 0) 1249 return 0; 1250 1251 if (!(mtcp = GetMTCPManager(mctx))) 1252 return -1; 1253 1254 copylen = MIN(strlen(mtcp->dbg_buf), len); 1255 strncpy(buf, mtcp->dbg_buf, copylen); 1256 1257 return copylen; 1258 #else 1259 return -1; 1260 #endif /* ENABLE_DEBUG_EVENT */ 1261 } 1262 /*----------------------------------------------------------------------------*/ 1263