176404edcSAsim Jamshed #include <assert.h> 276404edcSAsim Jamshed #include <ctype.h> 376404edcSAsim Jamshed #include <string.h> 476404edcSAsim Jamshed #ifdef ENABLE_DEBUG_EVENT 576404edcSAsim Jamshed #include <stdarg.h> 676404edcSAsim Jamshed #endif 776404edcSAsim Jamshed 876404edcSAsim Jamshed #include "mtcp.h" 976404edcSAsim Jamshed #include "mos_api.h" 1076404edcSAsim Jamshed #include "debug.h" 1176404edcSAsim Jamshed #include "config.h" 1276404edcSAsim Jamshed #include "ip_in.h" 1376404edcSAsim Jamshed #include "tcp_out.h" 1476404edcSAsim Jamshed /*----------------------------------------------------------------------------*/ 1576404edcSAsim Jamshed #define MAX(x, y) (((x) > (y)) ? (x) : (y)) 1676404edcSAsim Jamshed #define MIN(x, y) (((x) < (y)) ? (x) : (y)) 1776404edcSAsim Jamshed #define SKIP_SPACES(x) while (*x && isspace((int)*x)) x++; 1876404edcSAsim Jamshed #define SKIP_CHAR(x) while((*x) && !isspace(*x)) x++; 1976404edcSAsim Jamshed 2076404edcSAsim Jamshed #define KW_AND "and " 2176404edcSAsim Jamshed #define KW_OR "or " 2276404edcSAsim Jamshed #define KW_NOT "not " 2376404edcSAsim Jamshed #define KW_TCP "tcp" 2476404edcSAsim Jamshed #define KW_NOT_TCP "!tcp" 2576404edcSAsim Jamshed #define KW_NOT_TCP2 "not tcp" 2676404edcSAsim Jamshed #define KW_SRC "src " 2776404edcSAsim Jamshed #define KW_DST "dst " 2876404edcSAsim Jamshed #define KW_HOST "host " 2976404edcSAsim Jamshed #define KW_NET "net " 3076404edcSAsim Jamshed #define KW_MASK "mask " 3176404edcSAsim Jamshed #define KW_PORT "port " 3276404edcSAsim Jamshed #define KW_PORTRANGE "portrange " 3376404edcSAsim Jamshed /*----------------------------------------------------------------------------*/ 3476404edcSAsim Jamshed int 3576404edcSAsim Jamshed IsValidFlowRule(char *cf) 3676404edcSAsim Jamshed { 3776404edcSAsim Jamshed char *word; 3876404edcSAsim Jamshed int skip_word = 0; 3976404edcSAsim Jamshed 4076404edcSAsim Jamshed /* '!tcp' or 'not tcp' are also not supported in TCP flow filter */ 4176404edcSAsim Jamshed if (strstr(cf, KW_NOT_TCP) || strstr(cf, KW_NOT_TCP2)) { 4276404edcSAsim Jamshed TRACE_ERROR("'!tcp' or 'not tcp' is not a valid rule for TCP flow monitor.\n"); 4376404edcSAsim Jamshed return FALSE; 4476404edcSAsim Jamshed } 4576404edcSAsim Jamshed 4676404edcSAsim Jamshed /* verify that the rule contains flow-related keywords only */ 4776404edcSAsim Jamshed word = cf; 4876404edcSAsim Jamshed SKIP_SPACES(word); 4976404edcSAsim Jamshed 5076404edcSAsim Jamshed /* while (browse the rule by words) */ 5176404edcSAsim Jamshed while (*word) { 5276404edcSAsim Jamshed if (skip_word) { 5376404edcSAsim Jamshed skip_word = 0; 5476404edcSAsim Jamshed SKIP_CHAR(word); 5576404edcSAsim Jamshed SKIP_SPACES(word); 5676404edcSAsim Jamshed continue; 5776404edcSAsim Jamshed } 5876404edcSAsim Jamshed /* parse the keyword */ 5976404edcSAsim Jamshed /* case "tcp" "src" "dst" "not' "and" "or" -> move to the next word */ 6076404edcSAsim Jamshed if (!strncmp(word, KW_TCP, sizeof(KW_TCP) - 1) || 6176404edcSAsim Jamshed !strncmp(word, KW_SRC, sizeof(KW_SRC) - 1) || 6276404edcSAsim Jamshed !strncmp(word, KW_DST, sizeof(KW_DST) - 1) || 6376404edcSAsim Jamshed !strncmp(word, KW_NOT, sizeof(KW_NOT) - 1) || 6476404edcSAsim Jamshed !strncmp(word, KW_AND, sizeof(KW_AND) - 1) || 6576404edcSAsim Jamshed !strncmp(word, KW_OR, sizeof(KW_OR) - 1)) { 6676404edcSAsim Jamshed skip_word = 0; 6776404edcSAsim Jamshed } 6876404edcSAsim Jamshed /* case "net" "mask" "port" "portrange" -> skip a word (= param) */ 6976404edcSAsim Jamshed else if (!strncmp(word, KW_HOST, sizeof(KW_HOST) - 1) || 7076404edcSAsim Jamshed !strncmp(word, KW_NET, sizeof(KW_NET) - 1) || 7176404edcSAsim Jamshed !strncmp(word, KW_MASK, sizeof(KW_MASK) - 1) || 7276404edcSAsim Jamshed !strncmp(word, KW_PORT, sizeof(KW_PORT) - 1) || 7376404edcSAsim Jamshed !strncmp(word, KW_PORTRANGE, sizeof(KW_PORTRANGE) - 1)) { 7476404edcSAsim Jamshed skip_word = 1; 7576404edcSAsim Jamshed } 7676404edcSAsim Jamshed /* default (rule has any invalid keyword) -> return error */ 7776404edcSAsim Jamshed else { 7876404edcSAsim Jamshed TRACE_ERROR("Invalid keyword in filter (%s)\n", word); 7976404edcSAsim Jamshed return FALSE; 8076404edcSAsim Jamshed } 8176404edcSAsim Jamshed 8276404edcSAsim Jamshed SKIP_CHAR(word); 8376404edcSAsim Jamshed SKIP_SPACES(word); 8476404edcSAsim Jamshed } 8576404edcSAsim Jamshed 8676404edcSAsim Jamshed return TRUE; 8776404edcSAsim Jamshed } 8876404edcSAsim Jamshed /*----------------------------------------------------------------------------*/ 8976404edcSAsim Jamshed /* Assign an address range (specified by ft) to monitor via sock */ 9076404edcSAsim Jamshed int 9176404edcSAsim Jamshed mtcp_bind_monitor_filter(mctx_t mctx, int sockid, monitor_filter_t ft) 9276404edcSAsim Jamshed { 9376404edcSAsim Jamshed socket_map_t sock; 9476404edcSAsim Jamshed mtcp_manager_t mtcp; 9576404edcSAsim Jamshed 9676404edcSAsim Jamshed mtcp = GetMTCPManager(mctx); 9776404edcSAsim Jamshed if (!mtcp) { 9876404edcSAsim Jamshed errno = EACCES; 9976404edcSAsim Jamshed return -1; 10076404edcSAsim Jamshed } 10176404edcSAsim Jamshed 10276404edcSAsim Jamshed /* if filter is not set, do nothing and return */ 10376404edcSAsim Jamshed if (ft == NULL) { 10476404edcSAsim Jamshed TRACE_ERROR("filter not set!\n"); 10576404edcSAsim Jamshed return 0; 10676404edcSAsim Jamshed } 10776404edcSAsim Jamshed 10876404edcSAsim Jamshed /* retrieve the socket */ 10976404edcSAsim Jamshed if (sockid < 0 || sockid >= g_config.mos->max_concurrency) { 11076404edcSAsim Jamshed errno = EBADF; 11176404edcSAsim Jamshed TRACE_ERROR("sockid is invalid!\n"); 11276404edcSAsim Jamshed return -1; 11376404edcSAsim Jamshed } 11476404edcSAsim Jamshed sock = &mtcp->msmap[sockid]; 11576404edcSAsim Jamshed 11676404edcSAsim Jamshed /* check socket type */ 11776404edcSAsim Jamshed switch (sock->socktype) { 11876404edcSAsim Jamshed case MOS_SOCK_MONITOR_RAW: 11976404edcSAsim Jamshed /* For MONITOR_RAW type, allow any bpf rule */ 12076404edcSAsim Jamshed if (!ft->raw_pkt_filter) { 12176404edcSAsim Jamshed TRACE_ERROR("raw pkt filter is null"); 12276404edcSAsim Jamshed return 0; 12376404edcSAsim Jamshed } 12476404edcSAsim Jamshed if (SET_BPFFILTER(&sock->monitor_listener->raw_pkt_fcode, 12576404edcSAsim Jamshed ft->raw_pkt_filter) < 0) { 12676404edcSAsim Jamshed TRACE_ERROR("Invalid filter expression!\n"); 12776404edcSAsim Jamshed errno = EINVAL; 12876404edcSAsim Jamshed return -1; 12976404edcSAsim Jamshed } 13076404edcSAsim Jamshed break; 13176404edcSAsim Jamshed case MOS_SOCK_MONITOR_STREAM: 13276404edcSAsim Jamshed /* For MONITOR_STREAM_PASSIVE type, restrict to flow-level keywords */ 13376404edcSAsim Jamshed if (ft->stream_syn_filter) { 13476404edcSAsim Jamshed if (!IsValidFlowRule(ft->stream_syn_filter)) { 13576404edcSAsim Jamshed errno = EINVAL; 13676404edcSAsim Jamshed return -1; 13776404edcSAsim Jamshed } 13876404edcSAsim Jamshed if (SET_BPFFILTER(&sock->monitor_listener->stream_syn_fcode, 13976404edcSAsim Jamshed ft->stream_syn_filter) < 0) { 14076404edcSAsim Jamshed TRACE_ERROR("Invalid filter expression!\n"); 14176404edcSAsim Jamshed errno = EINVAL; 14276404edcSAsim Jamshed return -1; 14376404edcSAsim Jamshed } 14476404edcSAsim Jamshed } 14576404edcSAsim Jamshed if (ft->stream_orphan_filter) { 14676404edcSAsim Jamshed if (!IsValidFlowRule(ft->stream_orphan_filter)) { 14776404edcSAsim Jamshed errno = EINVAL; 14876404edcSAsim Jamshed return -1; 14976404edcSAsim Jamshed } 15076404edcSAsim Jamshed if (SET_BPFFILTER(&sock->monitor_listener->stream_orphan_fcode, 15176404edcSAsim Jamshed ft->stream_orphan_filter) < 0) { 15276404edcSAsim Jamshed TRACE_ERROR("Invalid filter expression!\n"); 15376404edcSAsim Jamshed errno = EINVAL; 15476404edcSAsim Jamshed return -1; 15576404edcSAsim Jamshed } 15676404edcSAsim Jamshed } 15776404edcSAsim Jamshed break; 15876404edcSAsim Jamshed default: 15976404edcSAsim Jamshed /* return error for other socket types */ 16076404edcSAsim Jamshed errno = ENOPROTOOPT; 16176404edcSAsim Jamshed TRACE_ERROR("Invalid sock type!\n"); 16276404edcSAsim Jamshed return -1; 16376404edcSAsim Jamshed } 16476404edcSAsim Jamshed 16576404edcSAsim Jamshed return 0; 16676404edcSAsim Jamshed } 16776404edcSAsim Jamshed /*----------------------------------------------------------------------------*/ 16876404edcSAsim Jamshed void 16976404edcSAsim Jamshed mtcp_app_join(mctx_t mctx) 17076404edcSAsim Jamshed { 17176404edcSAsim Jamshed mtcp_manager_t mtcp = GetMTCPManager(mctx); 17276404edcSAsim Jamshed if (!mtcp) return; 17376404edcSAsim Jamshed 17476404edcSAsim Jamshed RunPassiveLoop(mtcp); 17576404edcSAsim Jamshed return; 17676404edcSAsim Jamshed } 17776404edcSAsim Jamshed /*----------------------------------------------------------------------------*/ 17876404edcSAsim Jamshed /* Callback only functions */ 17976404edcSAsim Jamshed /*----------------------------------------------------------------------------*/ 18076404edcSAsim Jamshed void 18176404edcSAsim Jamshed mtcp_set_uctx(mctx_t mctx, int msock, void *uctx) 18276404edcSAsim Jamshed { 18376404edcSAsim Jamshed mtcp_manager_t mtcp; 18476404edcSAsim Jamshed 18576404edcSAsim Jamshed mtcp = GetMTCPManager(mctx); 18676404edcSAsim Jamshed if (!mtcp) { 18776404edcSAsim Jamshed return; 18876404edcSAsim Jamshed } 18976404edcSAsim Jamshed 19076404edcSAsim Jamshed /* check if the calling thread is in MOS context */ 19176404edcSAsim Jamshed if (mtcp->ctx->thread != pthread_self()) 19276404edcSAsim Jamshed return; 19376404edcSAsim Jamshed 19476404edcSAsim Jamshed if (msock < 0 || msock >= g_config.mos->max_concurrency) { 19576404edcSAsim Jamshed TRACE_API("Socket id %d out of range.\n", msock); 19676404edcSAsim Jamshed errno = EBADF; 19776404edcSAsim Jamshed return; 19876404edcSAsim Jamshed } 19976404edcSAsim Jamshed 20076404edcSAsim Jamshed socket_map_t socket = &mtcp->msmap[msock]; 20176404edcSAsim Jamshed if (socket->socktype == MOS_SOCK_MONITOR_STREAM_ACTIVE) 20276404edcSAsim Jamshed socket->monitor_stream->uctx = uctx; 20376404edcSAsim Jamshed else if (socket->socktype == MOS_SOCK_MONITOR_STREAM || 20476404edcSAsim Jamshed socket->socktype == MOS_SOCK_MONITOR_RAW) 20576404edcSAsim Jamshed socket->monitor_listener->uctx = uctx; 20676404edcSAsim Jamshed } 20776404edcSAsim Jamshed /*----------------------------------------------------------------------------*/ 20876404edcSAsim Jamshed void * 20976404edcSAsim Jamshed mtcp_get_uctx(mctx_t mctx, int msock) 21076404edcSAsim Jamshed { 21176404edcSAsim Jamshed mtcp_manager_t mtcp; 21276404edcSAsim Jamshed 21376404edcSAsim Jamshed mtcp = GetMTCPManager(mctx); 21476404edcSAsim Jamshed if (!mtcp) { 21576404edcSAsim Jamshed errno = EACCES; 21676404edcSAsim Jamshed return NULL; 21776404edcSAsim Jamshed } 21876404edcSAsim Jamshed 21976404edcSAsim Jamshed /* check if the calling thread is in MOS context */ 22076404edcSAsim Jamshed if (mtcp->ctx->thread != pthread_self()) { 22176404edcSAsim Jamshed errno = EPERM; 22276404edcSAsim Jamshed return NULL; 22376404edcSAsim Jamshed } 22476404edcSAsim Jamshed 22576404edcSAsim Jamshed if (msock < 0 || msock >= g_config.mos->max_concurrency) { 22676404edcSAsim Jamshed TRACE_API("Socket id %d out of range.\n", msock); 22776404edcSAsim Jamshed errno = EBADF; 22876404edcSAsim Jamshed return NULL; 22976404edcSAsim Jamshed } 23076404edcSAsim Jamshed 23176404edcSAsim Jamshed socket_map_t socket = &mtcp->msmap[msock]; 23276404edcSAsim Jamshed if (socket->socktype == MOS_SOCK_MONITOR_STREAM_ACTIVE) 23376404edcSAsim Jamshed return socket->monitor_stream->uctx; 23476404edcSAsim Jamshed else if (socket->socktype == MOS_SOCK_MONITOR_STREAM || 23576404edcSAsim Jamshed socket->socktype == MOS_SOCK_MONITOR_RAW) 23676404edcSAsim Jamshed return socket->monitor_listener->uctx; 23776404edcSAsim Jamshed else 23876404edcSAsim Jamshed return NULL; 23976404edcSAsim Jamshed } 24076404edcSAsim Jamshed /*----------------------------------------------------------------------------*/ 24176404edcSAsim Jamshed ssize_t 24276404edcSAsim Jamshed mtcp_peek(mctx_t mctx, int msock, int side, char *buf, size_t len) 24376404edcSAsim Jamshed { 24476404edcSAsim Jamshed int copylen, rc; 24576404edcSAsim Jamshed struct tcp_stream *cur_stream; 24676404edcSAsim Jamshed mtcp_manager_t mtcp; 24776404edcSAsim Jamshed socket_map_t sock; 24876404edcSAsim Jamshed 24976404edcSAsim Jamshed copylen = rc = 0; 25076404edcSAsim Jamshed mtcp = GetMTCPManager(mctx); 25176404edcSAsim Jamshed if (!mtcp) { 25276404edcSAsim Jamshed errno = EACCES; 25376404edcSAsim Jamshed return -1; 25476404edcSAsim Jamshed } 25576404edcSAsim Jamshed 25676404edcSAsim Jamshed /* check if the calling thread is in MOS context */ 25776404edcSAsim Jamshed if (mtcp->ctx->thread != pthread_self()) { 25876404edcSAsim Jamshed errno = EPERM; 25976404edcSAsim Jamshed return -1; 26076404edcSAsim Jamshed } 26176404edcSAsim Jamshed 26276404edcSAsim Jamshed /* check if the socket is monitor stream */ 26376404edcSAsim Jamshed sock = &mtcp->msmap[msock]; 26476404edcSAsim Jamshed if (sock->socktype != MOS_SOCK_MONITOR_STREAM_ACTIVE) { 26576404edcSAsim Jamshed TRACE_DBG("Invalid socket type!\n"); 26676404edcSAsim Jamshed errno = EBADF; 26776404edcSAsim Jamshed return -1; 26876404edcSAsim Jamshed } 26976404edcSAsim Jamshed 27076404edcSAsim Jamshed if (side != MOS_SIDE_CLI && side != MOS_SIDE_SVR) { 27176404edcSAsim Jamshed TRACE_ERROR("Invalid side requested!\n"); 27276404edcSAsim Jamshed exit(EXIT_FAILURE); 27376404edcSAsim Jamshed return -1; 27476404edcSAsim Jamshed } 27576404edcSAsim Jamshed 27676404edcSAsim Jamshed struct tcp_stream *mstrm = sock->monitor_stream->stream; 27776404edcSAsim Jamshed cur_stream = (side == mstrm->side) ? mstrm : mstrm->pair_stream; 27876404edcSAsim Jamshed 27976404edcSAsim Jamshed if (!cur_stream || !cur_stream->buffer_mgmt) { 28076404edcSAsim Jamshed TRACE_DBG("Stream is NULL!! or buffer management is disabled\n"); 28176404edcSAsim Jamshed errno = EINVAL; 28276404edcSAsim Jamshed return -1; 28376404edcSAsim Jamshed } 28476404edcSAsim Jamshed 28576404edcSAsim Jamshed /* Check if the read was not just due to syn-ack recv */ 28676404edcSAsim Jamshed if (cur_stream->rcvvar != NULL && 28776404edcSAsim Jamshed cur_stream->rcvvar->rcvbuf != NULL) { 28876404edcSAsim Jamshed tcprb_t *rcvbuf = cur_stream->rcvvar->rcvbuf; 28976404edcSAsim Jamshed loff_t *poff = &sock->monitor_stream->peek_offset[cur_stream->side]; 29076404edcSAsim Jamshed 29176404edcSAsim Jamshed rc = tcprb_ppeek(rcvbuf, (uint8_t *)buf, len, *poff); 29276404edcSAsim Jamshed if (rc < 0) { 29376404edcSAsim Jamshed errno = ENODATA; 29476404edcSAsim Jamshed return -1; 29576404edcSAsim Jamshed } 29676404edcSAsim Jamshed 29776404edcSAsim Jamshed *poff += rc; 29876404edcSAsim Jamshed UNUSED(copylen); 29976404edcSAsim Jamshed 30076404edcSAsim Jamshed return rc; 30176404edcSAsim Jamshed } else { 30276404edcSAsim Jamshed TRACE_DBG("Stream hasn't yet been initialized!\n"); 30376404edcSAsim Jamshed rc = 0; 30476404edcSAsim Jamshed } 30576404edcSAsim Jamshed 30676404edcSAsim Jamshed return rc; 30776404edcSAsim Jamshed } 30876404edcSAsim Jamshed /*----------------------------------------------------------------------------*/ 30976404edcSAsim Jamshed /** 31076404edcSAsim Jamshed * Copies from the frags.. returns no. of bytes copied to buf 31176404edcSAsim Jamshed */ 31276404edcSAsim Jamshed static inline int 31376404edcSAsim Jamshed ExtractPayloadFromFrags(struct tcp_ring_buffer *rcvbuf, char *buf, 31476404edcSAsim Jamshed size_t count, off_t seq_num) 31576404edcSAsim Jamshed { 31676404edcSAsim Jamshed int cpbytesleft; 31776404edcSAsim Jamshed struct fragment_ctx *it; 31876404edcSAsim Jamshed 31976404edcSAsim Jamshed it = rcvbuf->fctx; 32076404edcSAsim Jamshed cpbytesleft = count; 32176404edcSAsim Jamshed /* go through each frag */ 32276404edcSAsim Jamshed while (it) { 32376404edcSAsim Jamshed /* first check whether sequent number matches */ 32476404edcSAsim Jamshed if (TCP_SEQ_BETWEEN(seq_num, it->seq, it->seq + it->len)) { 32576404edcSAsim Jamshed /* copy buf starting from seq# seq_num */ 32676404edcSAsim Jamshed /* copy the MIN of seq-range and bytes to be copied */ 32776404edcSAsim Jamshed memcpy(buf + count - cpbytesleft, 32876404edcSAsim Jamshed rcvbuf->head + seq_num - rcvbuf->head_seq, 32976404edcSAsim Jamshed MIN(it->len - (seq_num - it->seq), cpbytesleft)); 33076404edcSAsim Jamshed /* update target seq num */ 33176404edcSAsim Jamshed seq_num += it->len - (seq_num - it->seq); 33276404edcSAsim Jamshed /* update cpbytes left */ 33376404edcSAsim Jamshed cpbytesleft -= it->len - (seq_num - it->seq); 33476404edcSAsim Jamshed if (cpbytesleft == 0) 33576404edcSAsim Jamshed break; 33676404edcSAsim Jamshed } 33776404edcSAsim Jamshed it = it->next; 33876404edcSAsim Jamshed } 33976404edcSAsim Jamshed 34076404edcSAsim Jamshed count -= cpbytesleft; 34176404edcSAsim Jamshed 34276404edcSAsim Jamshed /* return number of bytes copied */ 34376404edcSAsim Jamshed return count; 34476404edcSAsim Jamshed } 34576404edcSAsim Jamshed /*----------------------------------------------------------------------------*/ 34676404edcSAsim Jamshed /* Please see in-code comments for description */ 34776404edcSAsim Jamshed ssize_t 34876404edcSAsim Jamshed mtcp_ppeek(mctx_t mctx, int msock, int side, 34976404edcSAsim Jamshed char *buf, size_t count, uint64_t off) 35076404edcSAsim Jamshed { 35176404edcSAsim Jamshed mtcp_manager_t mtcp; 35276404edcSAsim Jamshed struct tcp_stream *cur_stream; 35376404edcSAsim Jamshed int rc; 35476404edcSAsim Jamshed socket_map_t sock; 35576404edcSAsim Jamshed 35676404edcSAsim Jamshed mtcp = GetMTCPManager(mctx); 35776404edcSAsim Jamshed if (!mtcp) { 35876404edcSAsim Jamshed errno = EACCES; 35976404edcSAsim Jamshed goto ppeek_error; 36076404edcSAsim Jamshed } 36176404edcSAsim Jamshed 36276404edcSAsim Jamshed /* check if the calling thread is in MOS context */ 36376404edcSAsim Jamshed if (mtcp->ctx->thread != pthread_self()) { 36476404edcSAsim Jamshed errno = EPERM; 36576404edcSAsim Jamshed goto ppeek_error; 36676404edcSAsim Jamshed } 36776404edcSAsim Jamshed 36876404edcSAsim Jamshed /* check if the socket is monitor stream */ 36976404edcSAsim Jamshed sock = &mtcp->msmap[msock]; 37076404edcSAsim Jamshed if (sock->socktype != MOS_SOCK_MONITOR_STREAM_ACTIVE) { 37176404edcSAsim Jamshed TRACE_DBG("Invalid socket type!\n"); 37276404edcSAsim Jamshed errno = ESOCKTNOSUPPORT; 37376404edcSAsim Jamshed goto ppeek_error; 37476404edcSAsim Jamshed } 37576404edcSAsim Jamshed 37676404edcSAsim Jamshed if (side != MOS_SIDE_CLI && side != MOS_SIDE_SVR) { 37776404edcSAsim Jamshed TRACE_ERROR("Invalid side requested!\n"); 37876404edcSAsim Jamshed exit(EXIT_FAILURE); 37976404edcSAsim Jamshed return -1; 38076404edcSAsim Jamshed } 38176404edcSAsim Jamshed 38276404edcSAsim Jamshed struct tcp_stream *mstrm = sock->monitor_stream->stream; 38376404edcSAsim Jamshed cur_stream = (side == mstrm->side) ? mstrm : mstrm->pair_stream; 38476404edcSAsim Jamshed 38576404edcSAsim Jamshed if (!cur_stream || !cur_stream->buffer_mgmt) { 38676404edcSAsim Jamshed TRACE_DBG("Stream is either NULL or ring buffer is not managed!!\n"); 38776404edcSAsim Jamshed errno = EACCES; 38876404edcSAsim Jamshed goto ppeek_error; 38976404edcSAsim Jamshed } 39076404edcSAsim Jamshed 39176404edcSAsim Jamshed rc = 0; 39276404edcSAsim Jamshed /* Check if the read was not just due to syn-ack recv */ 39376404edcSAsim Jamshed if (cur_stream->rcvvar != NULL && 39476404edcSAsim Jamshed cur_stream->rcvvar->rcvbuf != NULL) { 39576404edcSAsim Jamshed tcprb_t *rcvbuf = cur_stream->rcvvar->rcvbuf; 39676404edcSAsim Jamshed return tcprb_ppeek(rcvbuf, (uint8_t *)buf, count, off); 39776404edcSAsim Jamshed } else { 39876404edcSAsim Jamshed errno = EPERM; 39976404edcSAsim Jamshed goto ppeek_error; 40076404edcSAsim Jamshed } 40176404edcSAsim Jamshed 40276404edcSAsim Jamshed return rc; 40376404edcSAsim Jamshed 40476404edcSAsim Jamshed ppeek_error: 40576404edcSAsim Jamshed return -1; 40676404edcSAsim Jamshed } 40776404edcSAsim Jamshed /*----------------------------------------------------------------------------*/ 40876404edcSAsim Jamshed #ifdef MTCP_CB_GETCURPKT_CREATE_COPY 40976404edcSAsim Jamshed static __thread unsigned char local_frame[ETHERNET_FRAME_LEN]; 41076404edcSAsim Jamshed inline struct pkt_info * 411*a14d6bd4SAsim Jamshed ClonePacketCtx(struct pkt_info *to, unsigned char *frame, struct pkt_info *from) 41276404edcSAsim Jamshed { 41376404edcSAsim Jamshed /* memcpy the entire ethernet frame */ 41476404edcSAsim Jamshed assert(from); 415*a14d6bd4SAsim Jamshed assert(from->eth_len > 0); 416*a14d6bd4SAsim Jamshed assert(from->eth_len <= ETHERNET_FRAME_LEN); 417*a14d6bd4SAsim Jamshed memcpy(frame, from->ethh, from->eth_len); 418*a14d6bd4SAsim Jamshed 419*a14d6bd4SAsim Jamshed /* only memcpy till the last field before ethh */ 420*a14d6bd4SAsim Jamshed /* memcpy(to, from, PCTX_COPY_LEN); */ 421*a14d6bd4SAsim Jamshed memcpy(to, from, PKT_INFO_LEN); 42276404edcSAsim Jamshed /* set iph */ 42376404edcSAsim Jamshed to->ethh = (struct ethhdr *)frame; 42476404edcSAsim Jamshed /* set iph */ 425*a14d6bd4SAsim Jamshed to->iph = from->iph ? 42676404edcSAsim Jamshed (struct iphdr *)((uint8_t *)(frame + ETHERNET_HEADER_LEN)) : NULL; 42776404edcSAsim Jamshed /* set tcph */ 428*a14d6bd4SAsim Jamshed to->tcph = from->tcph ? 42976404edcSAsim Jamshed (struct tcphdr *)(((uint8_t *)(to->iph)) + (to->iph->ihl<<2)) : NULL; 43076404edcSAsim Jamshed /* set payload */ 431*a14d6bd4SAsim Jamshed to->payload = from->tcph ? 43276404edcSAsim Jamshed ((uint8_t *)(to->tcph) + (to->tcph->doff<<2)) : NULL; 43376404edcSAsim Jamshed return to; 43476404edcSAsim Jamshed } 43576404edcSAsim Jamshed /*----------------------------------------------------------------------------*/ 43676404edcSAsim Jamshed int 43776404edcSAsim Jamshed mtcp_getlastpkt(mctx_t mctx, int sock, int side, struct pkt_info *pkt) 43876404edcSAsim Jamshed { 43976404edcSAsim Jamshed mtcp_manager_t mtcp; 44076404edcSAsim Jamshed socket_map_t socket; 44176404edcSAsim Jamshed struct pkt_ctx *cur_pkt_ctx; 44276404edcSAsim Jamshed 44376404edcSAsim Jamshed mtcp = GetMTCPManager(mctx); 44476404edcSAsim Jamshed if (!mtcp) { 44576404edcSAsim Jamshed errno = EACCES; 44676404edcSAsim Jamshed return -1; 44776404edcSAsim Jamshed } 44876404edcSAsim Jamshed 44976404edcSAsim Jamshed /* check if the calling thread is in MOS context */ 45076404edcSAsim Jamshed if (mtcp->ctx->thread != pthread_self()) { 45176404edcSAsim Jamshed errno = EPERM; 45276404edcSAsim Jamshed return -1; 45376404edcSAsim Jamshed } 45476404edcSAsim Jamshed 45576404edcSAsim Jamshed /* check if the socket is monitor stream */ 45676404edcSAsim Jamshed socket = &mtcp->msmap[sock]; 457*a14d6bd4SAsim Jamshed #ifndef RECORDPKT_PER_STREAM 458*a14d6bd4SAsim Jamshed switch (socket->socktype) { 459*a14d6bd4SAsim Jamshed case MOS_SOCK_MONITOR_STREAM_ACTIVE: 460*a14d6bd4SAsim Jamshed case MOS_SOCK_MONITOR_RAW: 461*a14d6bd4SAsim Jamshed case MOS_SOCK_MONITOR_STREAM: 462*a14d6bd4SAsim Jamshed if (mtcp->pctx == NULL) { 463*a14d6bd4SAsim Jamshed errno = EACCES; 464*a14d6bd4SAsim Jamshed return -1; 465*a14d6bd4SAsim Jamshed } 466*a14d6bd4SAsim Jamshed cur_pkt_ctx = mtcp->pctx; 467*a14d6bd4SAsim Jamshed break; 468*a14d6bd4SAsim Jamshed default: 469*a14d6bd4SAsim Jamshed TRACE_DBG("Invalid socket type!\n"); 470*a14d6bd4SAsim Jamshed errno = EBADF; 471*a14d6bd4SAsim Jamshed return -1; 472*a14d6bd4SAsim Jamshed } 473*a14d6bd4SAsim Jamshed #else /* RECORDPKT_PER_STREAM */ 474*a14d6bd4SAsim Jamshed struct tcp_stream *cur_stream; 47576404edcSAsim Jamshed if (socket->socktype == MOS_SOCK_MONITOR_STREAM_ACTIVE) { 47676404edcSAsim Jamshed if (side != MOS_SIDE_CLI && side != MOS_SIDE_SVR) { 47776404edcSAsim Jamshed TRACE_ERROR("Invalid side requested!\n"); 47876404edcSAsim Jamshed exit(EXIT_FAILURE); 47976404edcSAsim Jamshed return -1; 48076404edcSAsim Jamshed } 48176404edcSAsim Jamshed 48276404edcSAsim Jamshed struct tcp_stream *mstrm = socket->monitor_stream->stream; 48376404edcSAsim Jamshed cur_stream = (side == mstrm->side) ? mstrm : mstrm->pair_stream; 48476404edcSAsim Jamshed 48576404edcSAsim Jamshed cur_pkt_ctx = &cur_stream->last_pctx; 48676404edcSAsim Jamshed if (!cur_pkt_ctx->p.ethh) { 48776404edcSAsim Jamshed errno = ENODATA; 48876404edcSAsim Jamshed return -1; 48976404edcSAsim Jamshed } 49076404edcSAsim Jamshed } else if (socket->socktype == MOS_SOCK_MONITOR_RAW) { 49176404edcSAsim Jamshed cur_pkt_ctx = mtcp->pctx; 49276404edcSAsim Jamshed } else if (socket->socktype == MOS_SOCK_MONITOR_STREAM) { 49376404edcSAsim Jamshed /* 49476404edcSAsim Jamshed * if it is a monitor socket, then this means that 49576404edcSAsim Jamshed * this is a request for an orphan tcp packet 49676404edcSAsim Jamshed */ 49776404edcSAsim Jamshed cur_pkt_ctx = mtcp->pctx; 49876404edcSAsim Jamshed } else { 49976404edcSAsim Jamshed TRACE_DBG("Invalid socket type!\n"); 50076404edcSAsim Jamshed errno = EBADF; 50176404edcSAsim Jamshed return -1; 50276404edcSAsim Jamshed } 503*a14d6bd4SAsim Jamshed #endif /* !RECORDPKT_PER_STREAM */ 504*a14d6bd4SAsim Jamshed ClonePacketCtx(pkt, local_frame, &(cur_pkt_ctx->p)); 50576404edcSAsim Jamshed return 0; 50676404edcSAsim Jamshed } 50776404edcSAsim Jamshed #else 50876404edcSAsim Jamshed /*----------------------------------------------------------------------------*/ 50976404edcSAsim Jamshed int 51076404edcSAsim Jamshed mtcp_getlastpkt(mctx_t mctx, int sock, int side, struct pkt_ctx **pctx) 51176404edcSAsim Jamshed { 51276404edcSAsim Jamshed mtcp_manager_t mtcp; 51376404edcSAsim Jamshed 51476404edcSAsim Jamshed mtcp = GetMTCPManager(mctx); 51576404edcSAsim Jamshed if (!mtcp) { 51676404edcSAsim Jamshed errno = EACCES; 51776404edcSAsim Jamshed return -1; 51876404edcSAsim Jamshed } 51976404edcSAsim Jamshed 52076404edcSAsim Jamshed /* check if the calling thread is in MOS context */ 52176404edcSAsim Jamshed if (mtcp->ctx->thread != pthread_self()) { 52276404edcSAsim Jamshed errno = EPERM; 52376404edcSAsim Jamshed return -1; 52476404edcSAsim Jamshed } 52576404edcSAsim Jamshed /* just pass direct pointer */ 52676404edcSAsim Jamshed *pctx = mtcp->pctx; 52776404edcSAsim Jamshed 52876404edcSAsim Jamshed return 0; 52976404edcSAsim Jamshed } 53076404edcSAsim Jamshed #endif 53176404edcSAsim Jamshed /*----------------------------------------------------------------------------*/ 53291df013fSAsim Jamshed int 53391df013fSAsim Jamshed mtcp_sendpkt(mctx_t mctx, int sock, const struct pkt_info *pkt) 53491df013fSAsim Jamshed { 53591df013fSAsim Jamshed mtcp_manager_t mtcp; 53691df013fSAsim Jamshed socket_map_t socket; 53791df013fSAsim Jamshed 53891df013fSAsim Jamshed mtcp = GetMTCPManager(mctx); 53991df013fSAsim Jamshed if (!mtcp || !pkt) { 54091df013fSAsim Jamshed errno = EACCES; 54191df013fSAsim Jamshed return -1; 54291df013fSAsim Jamshed } 54391df013fSAsim Jamshed 54491df013fSAsim Jamshed /* check if the calling thread is in MOS context */ 54591df013fSAsim Jamshed if (mtcp->ctx->thread != pthread_self()) { 54691df013fSAsim Jamshed errno = EPERM; 54791df013fSAsim Jamshed return -1; 54891df013fSAsim Jamshed } 54991df013fSAsim Jamshed 55091df013fSAsim Jamshed /* check if the socket is monitor stream */ 55191df013fSAsim Jamshed socket = &mtcp->msmap[sock]; 55291df013fSAsim Jamshed 55391df013fSAsim Jamshed if (!(pkt->iph) || !(pkt->tcph)) { 55491df013fSAsim Jamshed errno = ENODATA; 55591df013fSAsim Jamshed TRACE_INFO("mtcp_sendpkt() only supports TCP packet for now.\n"); 55691df013fSAsim Jamshed return -1; 55791df013fSAsim Jamshed } 55891df013fSAsim Jamshed 55991df013fSAsim Jamshed if (socket->socktype == MOS_SOCK_MONITOR_STREAM_ACTIVE) { 56091df013fSAsim Jamshed SendTCPPacketStandalone(mtcp, 56191df013fSAsim Jamshed pkt->iph->saddr, pkt->tcph->source, 56291df013fSAsim Jamshed pkt->iph->daddr, pkt->tcph->dest, 56391df013fSAsim Jamshed htonl(pkt->tcph->seq), htonl(pkt->tcph->ack_seq), 56491df013fSAsim Jamshed ntohs(pkt->tcph->window), TCP_FLAG_ACK, 56591df013fSAsim Jamshed pkt->payload, pkt->payloadlen, 56691df013fSAsim Jamshed socket->monitor_stream->stream->rcvvar->ts_recent, 56791df013fSAsim Jamshed socket->monitor_stream->stream->rcvvar->ts_lastack_rcvd, 568a834ea89SAsim Jamshed pkt->iph->id, pkt->in_ifidx); 56991df013fSAsim Jamshed 57091df013fSAsim Jamshed 57191df013fSAsim Jamshed } 57291df013fSAsim Jamshed 57391df013fSAsim Jamshed return 0; 57491df013fSAsim Jamshed } 57591df013fSAsim Jamshed /*----------------------------------------------------------------------------*/ 57676404edcSAsim Jamshed /** Disable events from the monitor stream socket 57776404edcSAsim Jamshed * @param [in] mtcp: mtcp_manager 57876404edcSAsim Jamshed * @param [in] sock: socket 57976404edcSAsim Jamshed * 58076404edcSAsim Jamshed * returns 0 on success, -1 on failure 58176404edcSAsim Jamshed * 58276404edcSAsim Jamshed * This is used for flow management based monitoring sockets 58376404edcSAsim Jamshed */ 58476404edcSAsim Jamshed int 58576404edcSAsim Jamshed RemoveMonitorEvents(mtcp_manager_t mtcp, socket_map_t socket, int side) 58676404edcSAsim Jamshed { 58776404edcSAsim Jamshed struct mon_stream *mstream; 58876404edcSAsim Jamshed struct mon_listener *mlistener; 58976404edcSAsim Jamshed 59076404edcSAsim Jamshed if (mtcp == NULL) { 59176404edcSAsim Jamshed TRACE_DBG("mtcp is not defined!!!\n"); 59276404edcSAsim Jamshed errno = EACCES; 59376404edcSAsim Jamshed return -1; 59476404edcSAsim Jamshed } 59576404edcSAsim Jamshed 59676404edcSAsim Jamshed switch (socket->socktype) { 59776404edcSAsim Jamshed case MOS_SOCK_MONITOR_STREAM_ACTIVE: 59876404edcSAsim Jamshed mstream = socket->monitor_stream; 59976404edcSAsim Jamshed if (mstream == NULL) { 60076404edcSAsim Jamshed TRACE_ERROR("Mon Stream does not exist!\n"); 60176404edcSAsim Jamshed /* exit(-1); */ 60276404edcSAsim Jamshed errno = ENODATA; 60376404edcSAsim Jamshed return -1; 60476404edcSAsim Jamshed } 60576404edcSAsim Jamshed 60676404edcSAsim Jamshed if (side == MOS_SIDE_SVR) mstream->server_mon = 0; 60776404edcSAsim Jamshed else if (side == MOS_SIDE_CLI) mstream->client_mon = 0; 60876404edcSAsim Jamshed 60976404edcSAsim Jamshed if (mstream->server_mon == 0 && mstream->client_mon == 0) { 61076404edcSAsim Jamshed #ifdef NEWEV 61176404edcSAsim Jamshed /* 61276404edcSAsim Jamshed * if stree_dontcare is NULL, then we know that all 61376404edcSAsim Jamshed * events have already been disabled 61476404edcSAsim Jamshed */ 61576404edcSAsim Jamshed if (mstream->stree_pre_rcv != NULL) { 61676404edcSAsim Jamshed stree_dec_ref(mtcp->ev_store, mstream->stree_dontcare); 61776404edcSAsim Jamshed stree_dec_ref(mtcp->ev_store, mstream->stree_pre_rcv); 61876404edcSAsim Jamshed stree_dec_ref(mtcp->ev_store, mstream->stree_post_snd); 61976404edcSAsim Jamshed 62076404edcSAsim Jamshed mstream->stree_dontcare = NULL; 62176404edcSAsim Jamshed mstream->stree_pre_rcv = NULL; 62276404edcSAsim Jamshed mstream->stree_post_snd = NULL; 62376404edcSAsim Jamshed } 62476404edcSAsim Jamshed #else 62576404edcSAsim Jamshed /* no error checking over here.. 62676404edcSAsim Jamshed * but its okay.. this code is 62776404edcSAsim Jamshed * deprecated 62876404edcSAsim Jamshed */ 62976404edcSAsim Jamshed CleanupEvP(&mstream->dontcare_evp); 63076404edcSAsim Jamshed CleanupEvP(&mstream->pre_tcp_evp); 63176404edcSAsim Jamshed CleanupEvP(&mstream->post_tcp_evp); 63276404edcSAsim Jamshed #endif 63376404edcSAsim Jamshed } 63476404edcSAsim Jamshed break; 63576404edcSAsim Jamshed case MOS_SOCK_MONITOR_STREAM: 63676404edcSAsim Jamshed mlistener = socket->monitor_listener; 63776404edcSAsim Jamshed if (mlistener == NULL) { 63876404edcSAsim Jamshed TRACE_ERROR("Mon listener does not exist!\n"); 63976404edcSAsim Jamshed errno = ENODATA; 64076404edcSAsim Jamshed return -1; 64176404edcSAsim Jamshed } 64276404edcSAsim Jamshed 64376404edcSAsim Jamshed if (side == MOS_SIDE_SVR) mlistener->server_mon = 0; 64476404edcSAsim Jamshed else if (side == MOS_SIDE_CLI) mlistener->client_mon = 0; 64576404edcSAsim Jamshed 64676404edcSAsim Jamshed if (mlistener->server_mon == 0 && mlistener->client_mon == 0) { 64776404edcSAsim Jamshed #ifdef NEWEV 64876404edcSAsim Jamshed /* 64976404edcSAsim Jamshed * if stree_dontcare is NULL, then we know that all 65076404edcSAsim Jamshed * events have already been disabled 65176404edcSAsim Jamshed */ 65276404edcSAsim Jamshed if (mlistener->stree_pre_rcv != NULL) { 65376404edcSAsim Jamshed stree_dec_ref(mtcp->ev_store, mlistener->stree_dontcare); 65476404edcSAsim Jamshed stree_dec_ref(mtcp->ev_store, mlistener->stree_pre_rcv); 65576404edcSAsim Jamshed stree_dec_ref(mtcp->ev_store, mlistener->stree_post_snd); 65676404edcSAsim Jamshed 65776404edcSAsim Jamshed mlistener->stree_dontcare = NULL; 65876404edcSAsim Jamshed mlistener->stree_pre_rcv = NULL; 65976404edcSAsim Jamshed mlistener->stree_post_snd = NULL; 66076404edcSAsim Jamshed } 66176404edcSAsim Jamshed #else 66276404edcSAsim Jamshed /* no error checking over here.. 66376404edcSAsim Jamshed * but its okay.. this code is 66476404edcSAsim Jamshed * deprecated 66576404edcSAsim Jamshed */ 66676404edcSAsim Jamshed CleanupEvB(mtcp, &mlistener->dontcare_evb); 66776404edcSAsim Jamshed CleanupEvB(mtcp, &mlistener->pre_tcp_evb); 66876404edcSAsim Jamshed CleanupEvB(mtcp, &mlistener->post_tcp_evb); 66976404edcSAsim Jamshed #endif 67076404edcSAsim Jamshed } 67176404edcSAsim Jamshed break; 67276404edcSAsim Jamshed default: 67376404edcSAsim Jamshed TRACE_ERROR("Invalid socket type!\n"); 67476404edcSAsim Jamshed } 67576404edcSAsim Jamshed 67676404edcSAsim Jamshed return 0; 67776404edcSAsim Jamshed } 67876404edcSAsim Jamshed /*----------------------------------------------------------------------------*/ 67976404edcSAsim Jamshed /** 68076404edcSAsim Jamshed * Disable monitoring based on side variable. 68176404edcSAsim Jamshed */ 68276404edcSAsim Jamshed int 68376404edcSAsim Jamshed mtcp_cb_stop(mctx_t mctx, int sock, int side) 68476404edcSAsim Jamshed { 68576404edcSAsim Jamshed mtcp_manager_t mtcp; 68676404edcSAsim Jamshed socket_map_t socket; 68776404edcSAsim Jamshed struct tcp_stream *stream; 68876404edcSAsim Jamshed struct socket_map *walk; 68976404edcSAsim Jamshed uint8_t mgmt; 69076404edcSAsim Jamshed 69176404edcSAsim Jamshed mtcp = GetMTCPManager(mctx); 69276404edcSAsim Jamshed if (!mtcp) { 69376404edcSAsim Jamshed errno = EACCES; 69476404edcSAsim Jamshed return -1; 69576404edcSAsim Jamshed } 69676404edcSAsim Jamshed 69776404edcSAsim Jamshed socket = &mtcp->msmap[sock]; 69876404edcSAsim Jamshed 69976404edcSAsim Jamshed /* works for both monitor listener and stream sockets */ 70076404edcSAsim Jamshed RemoveMonitorEvents(mtcp, socket, side); 70176404edcSAsim Jamshed 70276404edcSAsim Jamshed /* passive monitoring socket is not connected to any stream */ 70376404edcSAsim Jamshed if (socket->socktype == MOS_SOCK_MONITOR_STREAM) 70476404edcSAsim Jamshed return 0; 70576404edcSAsim Jamshed 70676404edcSAsim Jamshed if (side == MOS_SIDE_CLI) { 70776404edcSAsim Jamshed /* see if the associated stream requires monitoring any more */ 70876404edcSAsim Jamshed stream = (socket->monitor_stream->stream->side == MOS_SIDE_CLI) ? 70976404edcSAsim Jamshed socket->monitor_stream->stream : 71076404edcSAsim Jamshed socket->monitor_stream->stream->pair_stream; 71176404edcSAsim Jamshed 71276404edcSAsim Jamshed mgmt = 0; 71376404edcSAsim Jamshed SOCKQ_FOREACH_START(walk, &stream->msocks) { 71476404edcSAsim Jamshed if (walk->monitor_stream->client_mon == 1) { 71576404edcSAsim Jamshed mgmt = 1; 71676404edcSAsim Jamshed break; 71776404edcSAsim Jamshed } 71876404edcSAsim Jamshed } SOCKQ_FOREACH_END; 71976404edcSAsim Jamshed /* if all streams have mgmt off, then tag the stream for destruction */ 72076404edcSAsim Jamshed if (mgmt == 0) { 72176404edcSAsim Jamshed stream = (socket->monitor_stream->stream->side == MOS_SIDE_CLI) ? 72276404edcSAsim Jamshed socket->monitor_stream->stream : 72376404edcSAsim Jamshed socket->monitor_stream->stream->pair_stream; 72476404edcSAsim Jamshed stream->status_mgmt = 0; 72576404edcSAsim Jamshed } 72676404edcSAsim Jamshed } 72776404edcSAsim Jamshed 72876404edcSAsim Jamshed if (side == MOS_SIDE_SVR) { 72976404edcSAsim Jamshed /* see if the associated stream requires monitoring any more */ 73076404edcSAsim Jamshed stream = (socket->monitor_stream->stream->side == MOS_SIDE_SVR) ? 73176404edcSAsim Jamshed socket->monitor_stream->stream : 73276404edcSAsim Jamshed socket->monitor_stream->stream->pair_stream; 73376404edcSAsim Jamshed mgmt = 0; 73476404edcSAsim Jamshed SOCKQ_FOREACH_START(walk, &stream->msocks) { 73576404edcSAsim Jamshed if (walk->monitor_stream->server_mon == 1) { 73676404edcSAsim Jamshed mgmt = 1; 73776404edcSAsim Jamshed break; 73876404edcSAsim Jamshed } 73976404edcSAsim Jamshed } SOCKQ_FOREACH_END; 74076404edcSAsim Jamshed /* if all streams have mgmt off, then tag the stream for destruction */ 74176404edcSAsim Jamshed if (mgmt == 0) { 74276404edcSAsim Jamshed stream = (socket->monitor_stream->stream->side == MOS_SIDE_SVR) ? 74376404edcSAsim Jamshed socket->monitor_stream->stream : 74476404edcSAsim Jamshed socket->monitor_stream->stream->pair_stream; 74576404edcSAsim Jamshed stream->status_mgmt = 0; 74676404edcSAsim Jamshed } 74776404edcSAsim Jamshed } 74876404edcSAsim Jamshed 74976404edcSAsim Jamshed return 0; 75076404edcSAsim Jamshed } 75176404edcSAsim Jamshed /*----------------------------------------------------------------------------*/ 75276404edcSAsim Jamshed /** 75376404edcSAsim Jamshed * send a RST packet to the TCP stream (uni-directional) 75476404edcSAsim Jamshed */ 75576404edcSAsim Jamshed static inline void 75676404edcSAsim Jamshed SendRSTPacketStandalone(mtcp_manager_t mtcp, struct tcp_stream *stream) { 75776404edcSAsim Jamshed SendTCPPacketStandalone(mtcp, 75876404edcSAsim Jamshed stream->saddr, stream->sport, stream->daddr, stream->dport, 75976404edcSAsim Jamshed stream->snd_nxt, stream->rcv_nxt, 0, TCP_FLAG_RST | TCP_FLAG_ACK, 760a834ea89SAsim Jamshed NULL, 0, mtcp->cur_ts, 0, 0, -1); 76176404edcSAsim Jamshed } 76276404edcSAsim Jamshed /*----------------------------------------------------------------------------*/ 76376404edcSAsim Jamshed /** 76476404edcSAsim Jamshed * Reset the connection (send RST packets to both sides) 76576404edcSAsim Jamshed */ 76676404edcSAsim Jamshed int 76776404edcSAsim Jamshed mtcp_reset_conn(mctx_t mctx, int sock) 76876404edcSAsim Jamshed { 76976404edcSAsim Jamshed mtcp_manager_t mtcp; 77076404edcSAsim Jamshed socket_map_t socket; 77176404edcSAsim Jamshed 77276404edcSAsim Jamshed mtcp = GetMTCPManager(mctx); 77376404edcSAsim Jamshed if (!mtcp) { 77476404edcSAsim Jamshed errno = EACCES; 77576404edcSAsim Jamshed return -1; 77676404edcSAsim Jamshed } 77776404edcSAsim Jamshed 77876404edcSAsim Jamshed socket = &mtcp->msmap[sock]; 77976404edcSAsim Jamshed 78076404edcSAsim Jamshed /* passive monitoring socket is not connected to any stream */ 78176404edcSAsim Jamshed if (socket->socktype == MOS_SOCK_MONITOR_STREAM) { 78276404edcSAsim Jamshed errno = EINVAL; 78376404edcSAsim Jamshed return -1; 78476404edcSAsim Jamshed } 78576404edcSAsim Jamshed 78676404edcSAsim Jamshed /* send RST packets to the both sides */ 78776404edcSAsim Jamshed SendRSTPacketStandalone(mtcp, socket->monitor_stream->stream); 78876404edcSAsim Jamshed SendRSTPacketStandalone(mtcp, socket->monitor_stream->stream->pair_stream); 78976404edcSAsim Jamshed 79076404edcSAsim Jamshed return 0; 79176404edcSAsim Jamshed } 79276404edcSAsim Jamshed /*----------------------------------------------------------------------------*/ 79376404edcSAsim Jamshed uint32_t 79476404edcSAsim Jamshed mtcp_cb_get_ts(mctx_t mctx) 79576404edcSAsim Jamshed { 79676404edcSAsim Jamshed mtcp_manager_t mtcp; 79776404edcSAsim Jamshed 79876404edcSAsim Jamshed mtcp = GetMTCPManager(mctx); 79976404edcSAsim Jamshed if (!mtcp) { 80076404edcSAsim Jamshed TRACE_DBG("Can't access MTCP manager!\n"); 80176404edcSAsim Jamshed errno = EACCES; 80276404edcSAsim Jamshed return 0; 80376404edcSAsim Jamshed } 80476404edcSAsim Jamshed 80576404edcSAsim Jamshed /* check if the calling thread is in MOS context */ 80676404edcSAsim Jamshed if (mtcp->ctx->thread != pthread_self()) { 80776404edcSAsim Jamshed errno = EPERM; 80876404edcSAsim Jamshed return 0; 80976404edcSAsim Jamshed } 81076404edcSAsim Jamshed 81176404edcSAsim Jamshed return TS_TO_USEC(mtcp->cur_ts); 81276404edcSAsim Jamshed } 81376404edcSAsim Jamshed /*----------------------------------------------------------------------------*/ 81476404edcSAsim Jamshed /* Macros related to getpeername */ 81576404edcSAsim Jamshed #define TILL_SVRADDR offsetof(struct sockaddr_in, sin_zero) 81676404edcSAsim Jamshed #define TILL_SVRPORT offsetof(struct sockaddr_in, sin_addr) 81776404edcSAsim Jamshed #define TILL_SVRFAMILY offsetof(struct sockaddr_in, sin_port) 81876404edcSAsim Jamshed #define TILL_CLIADDR sizeof(struct sockaddr) + TILL_SVRADDR 81976404edcSAsim Jamshed #define TILL_CLIPORT sizeof(struct sockaddr) + TILL_SVRPORT 82076404edcSAsim Jamshed #define TILL_CLIFAMILY sizeof(struct sockaddr) + TILL_SVRFAMILY 82176404edcSAsim Jamshed 82276404edcSAsim Jamshed int 82376404edcSAsim Jamshed mtcp_getpeername(mctx_t mctx, int sockfd, struct sockaddr *saddr, 82476404edcSAsim Jamshed socklen_t *addrlen, int side) 82576404edcSAsim Jamshed { 82676404edcSAsim Jamshed mtcp_manager_t mtcp; 82776404edcSAsim Jamshed socket_map_t socket; 82876404edcSAsim Jamshed struct tcp_stream *stream; 82976404edcSAsim Jamshed struct sockaddr_in *sin; 83076404edcSAsim Jamshed int rc; 83176404edcSAsim Jamshed 83276404edcSAsim Jamshed mtcp = GetMTCPManager(mctx); 83376404edcSAsim Jamshed if (!mtcp) { 83476404edcSAsim Jamshed TRACE_DBG("Can't access MTCP manager!\n"); 83576404edcSAsim Jamshed errno = EACCES; 83676404edcSAsim Jamshed return -1; 83776404edcSAsim Jamshed } 83876404edcSAsim Jamshed 839a5e1a556SAsim Jamshed /* check if sockfd is within limits */ 840a5e1a556SAsim Jamshed if (sockfd < 0 || sockfd >= g_config.mos->max_concurrency) { 841a5e1a556SAsim Jamshed TRACE_API("Socket id %d out of range.\n", sockfd); 842a5e1a556SAsim Jamshed errno = EBADF; 843a5e1a556SAsim Jamshed return -1; 844a5e1a556SAsim Jamshed } 845a5e1a556SAsim Jamshed 84676404edcSAsim Jamshed /* check if the calling thread is in MOS context */ 84776404edcSAsim Jamshed if (mtcp->ctx->thread != pthread_self()) { 84876404edcSAsim Jamshed errno = EPERM; 84976404edcSAsim Jamshed return -1; 85076404edcSAsim Jamshed } 85176404edcSAsim Jamshed 85276404edcSAsim Jamshed socket = &mtcp->msmap[sockfd]; 85376404edcSAsim Jamshed sin = (struct sockaddr_in *)saddr; 85476404edcSAsim Jamshed rc = 0; 85576404edcSAsim Jamshed 85676404edcSAsim Jamshed /* retrieve both streams */ 85776404edcSAsim Jamshed stream = socket->monitor_stream->stream; 85876404edcSAsim Jamshed 85976404edcSAsim Jamshed if (side != stream->side) 86076404edcSAsim Jamshed stream = stream->pair_stream; 86176404edcSAsim Jamshed 862a5e1a556SAsim Jamshed if (stream == NULL) { 863a5e1a556SAsim Jamshed errno = ENOTCONN; 86476404edcSAsim Jamshed return -1; 865a5e1a556SAsim Jamshed } 86676404edcSAsim Jamshed 86776404edcSAsim Jamshed /* reset to 2 * sizeof(struct sockaddr) if addrlen is too big */ 86876404edcSAsim Jamshed if (*addrlen > 2 * sizeof(struct sockaddr)) 86976404edcSAsim Jamshed *addrlen = 2 * sizeof(struct sockaddr); 87076404edcSAsim Jamshed 87176404edcSAsim Jamshed /* according per manpage, address can be truncated */ 87276404edcSAsim Jamshed switch (*addrlen) { 87376404edcSAsim Jamshed case (2 * sizeof(struct sockaddr)): 87476404edcSAsim Jamshed case TILL_CLIADDR: 87576404edcSAsim Jamshed sin[1].sin_addr.s_addr = stream->side == MOS_SIDE_SVR ? 87676404edcSAsim Jamshed stream->daddr : stream->saddr; 87776404edcSAsim Jamshed case TILL_CLIPORT: 87876404edcSAsim Jamshed sin[1].sin_port = stream->side == MOS_SIDE_SVR ? 87976404edcSAsim Jamshed stream->dport : stream->sport; 88076404edcSAsim Jamshed case TILL_CLIFAMILY: 88176404edcSAsim Jamshed sin[1].sin_family = AF_INET; 88276404edcSAsim Jamshed case (sizeof(struct sockaddr)): 88376404edcSAsim Jamshed case TILL_SVRADDR: 88476404edcSAsim Jamshed sin->sin_addr.s_addr = stream->side == MOS_SIDE_SVR ? 88576404edcSAsim Jamshed stream->saddr : stream->daddr; 88676404edcSAsim Jamshed case TILL_SVRPORT: 88776404edcSAsim Jamshed sin->sin_port = stream->side == MOS_SIDE_SVR ? 88876404edcSAsim Jamshed stream->sport : stream->dport; 88976404edcSAsim Jamshed case TILL_SVRFAMILY: 89076404edcSAsim Jamshed sin->sin_family = AF_INET; 89176404edcSAsim Jamshed break; 89276404edcSAsim Jamshed default: 89376404edcSAsim Jamshed rc = -1; 89476404edcSAsim Jamshed *addrlen = 0xFFFF; 895a5e1a556SAsim Jamshed errno = EINVAL; 89676404edcSAsim Jamshed } 89776404edcSAsim Jamshed 89876404edcSAsim Jamshed return rc; 89976404edcSAsim Jamshed } 90076404edcSAsim Jamshed /*----------------------------------------------------------------------------*/ 90176404edcSAsim Jamshed int 90276404edcSAsim Jamshed mtcp_setlastpkt(mctx_t mctx, int sock, int side, off_t offset, 90376404edcSAsim Jamshed byte *data, uint16_t datalen, int option) 90476404edcSAsim Jamshed { 90576404edcSAsim Jamshed mtcp_manager_t mtcp; 90676404edcSAsim Jamshed struct pkt_ctx *cur_pkt_ctx; 90776404edcSAsim Jamshed struct ethhdr *ethh; 90876404edcSAsim Jamshed struct iphdr *iph; 90976404edcSAsim Jamshed struct tcphdr *tcph; 91076404edcSAsim Jamshed unsigned char *payload; 91176404edcSAsim Jamshed 91276404edcSAsim Jamshed #if 0 91376404edcSAsim Jamshed socket_map_t socket; 91476404edcSAsim Jamshed struct tcp_stream *cur_stream; 91576404edcSAsim Jamshed #endif 91676404edcSAsim Jamshed 91776404edcSAsim Jamshed /* checking if mtcp is valid */ 91876404edcSAsim Jamshed mtcp = GetMTCPManager(mctx); 91976404edcSAsim Jamshed if (!mtcp) { 92076404edcSAsim Jamshed errno = EACCES; 92176404edcSAsim Jamshed TRACE_ERROR("Invalid mtcp!\n"); 92276404edcSAsim Jamshed return -1; 92376404edcSAsim Jamshed } 92476404edcSAsim Jamshed 92576404edcSAsim Jamshed /* check if the calling thread is in MOS context */ 92676404edcSAsim Jamshed if (mtcp->ctx->thread != pthread_self()) { 92776404edcSAsim Jamshed errno = EPERM; 92876404edcSAsim Jamshed TRACE_ERROR("Invalid thread id!\n"); 92976404edcSAsim Jamshed return -1; 93076404edcSAsim Jamshed } 93176404edcSAsim Jamshed 93276404edcSAsim Jamshed #if 0 93376404edcSAsim Jamshed /* check if the socket is monitor stream */ 93476404edcSAsim Jamshed socket = &mtcp->msmap[sock]; 93576404edcSAsim Jamshed if (socket->socktype == MOS_SOCK_MONITOR_STREAM_ACTIVE) { 93676404edcSAsim Jamshed if (side != MOS_SIDE_CLI && side != MOS_SIDE_SVR) { 93776404edcSAsim Jamshed TRACE_ERROR("Invalid side requested!\n"); 93876404edcSAsim Jamshed exit(EXIT_FAILURE); 93976404edcSAsim Jamshed return -1; 94076404edcSAsim Jamshed } 94176404edcSAsim Jamshed 94276404edcSAsim Jamshed struct tcp_stream *mstrm = socket->monitor_stream->stream; 94376404edcSAsim Jamshed cur_stream = (side == mstrm->side) ? mstrm : mstrm->pair_stream; 94476404edcSAsim Jamshed 94576404edcSAsim Jamshed if (!cur_stream->allow_pkt_modification) 94676404edcSAsim Jamshed return -1; 94776404edcSAsim Jamshed } else if (socket->socktype != MOS_SOCK_MONITOR_RAW) { 94876404edcSAsim Jamshed TRACE_ERROR("Invalid socket type!\n"); 94976404edcSAsim Jamshed exit(EXIT_FAILURE); 95076404edcSAsim Jamshed return -1; 95176404edcSAsim Jamshed } 95276404edcSAsim Jamshed #endif 95376404edcSAsim Jamshed 95476404edcSAsim Jamshed /* see if cur_pkt_ctx is valid */ 95576404edcSAsim Jamshed cur_pkt_ctx = mtcp->pctx; 95676404edcSAsim Jamshed if (cur_pkt_ctx == NULL) { 95776404edcSAsim Jamshed TRACE_ERROR("pctx is NULL!\n"); 95876404edcSAsim Jamshed errno = ENODATA; 95976404edcSAsim Jamshed return -1; 96076404edcSAsim Jamshed } 96176404edcSAsim Jamshed 96276404edcSAsim Jamshed /* check if offset is valid */ 96376404edcSAsim Jamshed if (offset < 0) { 96476404edcSAsim Jamshed TRACE_ERROR("Invalid offset position!\n"); 96576404edcSAsim Jamshed errno = EINVAL; 96676404edcSAsim Jamshed return -1; 96776404edcSAsim Jamshed } 96876404edcSAsim Jamshed 96976404edcSAsim Jamshed if (__builtin_popcount(option & (MOS_DROP | MOS_CHOMP | 97076404edcSAsim Jamshed MOS_INSERT | MOS_OVERWRITE)) != 1) { 97176404edcSAsim Jamshed TRACE_ERROR("mtcp_setlastpkt() function only allows one of " 97276404edcSAsim Jamshed "(MOS_DROP | MOS_CHOMP | MOS_INSERT | MOS_OVERWRITE) " 97376404edcSAsim Jamshed "to be set at a time.\n"); 97476404edcSAsim Jamshed errno = EAGAIN; 97576404edcSAsim Jamshed return -1; 97676404edcSAsim Jamshed } 97776404edcSAsim Jamshed 97876404edcSAsim Jamshed /* drop pkt has the highest priority */ 97976404edcSAsim Jamshed if (option & MOS_DROP) { 98076404edcSAsim Jamshed mtcp->pctx->forward = 0; 98176404edcSAsim Jamshed return 0; 98276404edcSAsim Jamshed } else if (option & MOS_ETH_HDR) { 98376404edcSAsim Jamshed /* validity test */ 98476404edcSAsim Jamshed if ((ethh=cur_pkt_ctx->p.ethh) == NULL || 98576404edcSAsim Jamshed offset + datalen > sizeof(struct ethhdr)) { 98676404edcSAsim Jamshed TRACE_ERROR("Ethernet setting has gone out of bounds " 98776404edcSAsim Jamshed "(offset: %ld, datalen: %d)\n", 98876404edcSAsim Jamshed offset, datalen); 98976404edcSAsim Jamshed errno = EINVAL; 99076404edcSAsim Jamshed return -1; 99176404edcSAsim Jamshed } 99276404edcSAsim Jamshed if (option & MOS_CHOMP) { 99376404edcSAsim Jamshed TRACE_ERROR("Illegal call. " 99476404edcSAsim Jamshed "Ethernet header can't be chopped down!\n"); 99576404edcSAsim Jamshed errno = EACCES; 99676404edcSAsim Jamshed return -1; 99776404edcSAsim Jamshed } else if (option & MOS_INSERT) { 99876404edcSAsim Jamshed TRACE_ERROR("Illegal call. " 99976404edcSAsim Jamshed "Ethernet header can't be extended!\n"); 100076404edcSAsim Jamshed errno = EACCES; 100176404edcSAsim Jamshed return -1; 100276404edcSAsim Jamshed } else /* if (option & MOS_OVERWRITE) */ { 100376404edcSAsim Jamshed memcpy((uint8_t *)ethh + offset, data, datalen); 100476404edcSAsim Jamshed } 100576404edcSAsim Jamshed /* iph, tcph, and payload do not need to change */ 100676404edcSAsim Jamshed } else if (option & MOS_IP_HDR) { 100776404edcSAsim Jamshed /* validity test */ 100876404edcSAsim Jamshed if (cur_pkt_ctx->p.ethh == NULL || 100976404edcSAsim Jamshed cur_pkt_ctx->p.ethh->h_proto != ntohs(ETH_P_IP) || 101076404edcSAsim Jamshed (iph=(struct iphdr *)(cur_pkt_ctx->p.ethh + 1)) == NULL) { 101176404edcSAsim Jamshed TRACE_ERROR("ethh or iph are out of bounds\n"); 101276404edcSAsim Jamshed errno = EACCES; 101376404edcSAsim Jamshed return -1; 101476404edcSAsim Jamshed } 101576404edcSAsim Jamshed if (option & MOS_OVERWRITE) { 101676404edcSAsim Jamshed if (offset + datalen > (iph->ihl<<2)) { 101776404edcSAsim Jamshed TRACE_ERROR("IP setting has gone out of bounds " 101876404edcSAsim Jamshed "(offset: %ld, datalen: %d)\n", 101976404edcSAsim Jamshed offset, datalen); 102076404edcSAsim Jamshed errno = EINVAL; 102176404edcSAsim Jamshed return -1; 102276404edcSAsim Jamshed } 102376404edcSAsim Jamshed memcpy((uint8_t *)iph + offset, data, datalen); 102476404edcSAsim Jamshed } 102576404edcSAsim Jamshed if (option & MOS_CHOMP) { 102676404edcSAsim Jamshed memmove((uint8_t *)iph + offset, 102776404edcSAsim Jamshed (uint8_t *)iph + offset + datalen, 102876404edcSAsim Jamshed cur_pkt_ctx->p.ip_len - offset - datalen); 102976404edcSAsim Jamshed 103076404edcSAsim Jamshed /* iph does not need to change */ 103176404edcSAsim Jamshed if (iph->protocol == IPPROTO_TCP) { 103276404edcSAsim Jamshed cur_pkt_ctx->p.tcph = (struct tcphdr *)((uint8_t *)iph + (iph->ihl<<2)); 103376404edcSAsim Jamshed cur_pkt_ctx->p.payload = (uint8_t *)cur_pkt_ctx->p.tcph + 103476404edcSAsim Jamshed (cur_pkt_ctx->p.tcph->doff<<2); 103576404edcSAsim Jamshed } else { 103676404edcSAsim Jamshed /* reset tcph if iph does not have tcp proto */ 103776404edcSAsim Jamshed cur_pkt_ctx->p.tcph = NULL; 103876404edcSAsim Jamshed } 103976404edcSAsim Jamshed /* update iph total length */ 104076404edcSAsim Jamshed cur_pkt_ctx->p.ip_len = ntohs(iph->tot_len); 104176404edcSAsim Jamshed /* update eth frame length */ 104276404edcSAsim Jamshed cur_pkt_ctx->p.eth_len = cur_pkt_ctx->p.ip_len + sizeof(struct ethhdr); 104376404edcSAsim Jamshed } else if (option & MOS_INSERT) { 104476404edcSAsim Jamshed memmove((uint8_t *)iph + offset + datalen, 104576404edcSAsim Jamshed (uint8_t *)iph + offset + 1, 104676404edcSAsim Jamshed cur_pkt_ctx->p.ip_len - offset); 104776404edcSAsim Jamshed memcpy((uint8_t *)iph + offset, 104876404edcSAsim Jamshed data, datalen); 104976404edcSAsim Jamshed 105076404edcSAsim Jamshed /* iph does not need to change */ 105176404edcSAsim Jamshed if (iph->protocol == IPPROTO_TCP) { 105276404edcSAsim Jamshed cur_pkt_ctx->p.tcph = (struct tcphdr *)((uint8_t *)iph + (iph->ihl<<2)); 105376404edcSAsim Jamshed cur_pkt_ctx->p.payload = (uint8_t *)cur_pkt_ctx->p.tcph + 105476404edcSAsim Jamshed (cur_pkt_ctx->p.tcph->doff<<2); 105576404edcSAsim Jamshed } else { 105676404edcSAsim Jamshed /* reset tcph if iph does not have tcp proto */ 105776404edcSAsim Jamshed cur_pkt_ctx->p.tcph = NULL; 105876404edcSAsim Jamshed } 105976404edcSAsim Jamshed /* update iph total length */ 106076404edcSAsim Jamshed cur_pkt_ctx->p.ip_len = ntohs(iph->tot_len); 106176404edcSAsim Jamshed /* update eth frame length */ 106276404edcSAsim Jamshed cur_pkt_ctx->p.eth_len = cur_pkt_ctx->p.ip_len + sizeof(struct ethhdr); 106376404edcSAsim Jamshed } 106476404edcSAsim Jamshed /* can't update payloadlen because we don't know tcph->doff */ 106576404edcSAsim Jamshed } else if (option & MOS_TCP_HDR) { 106676404edcSAsim Jamshed /* validity test */ 106776404edcSAsim Jamshed iph = (struct iphdr *)(cur_pkt_ctx->p.ethh + 1); 106876404edcSAsim Jamshed if (iph == NULL || 106976404edcSAsim Jamshed iph->protocol != IPPROTO_TCP || 107076404edcSAsim Jamshed (tcph=(struct tcphdr *)((uint8_t *)iph + (iph->ihl<<2))) == NULL) { 107176404edcSAsim Jamshed TRACE_ERROR("TCP setting has gone out of bounds " 107276404edcSAsim Jamshed "(offset: %ld, datalen: %d)\n", 107376404edcSAsim Jamshed offset, datalen); 107476404edcSAsim Jamshed errno = EINVAL; 107576404edcSAsim Jamshed return -1; 107676404edcSAsim Jamshed } 107776404edcSAsim Jamshed if (option & MOS_OVERWRITE) { 107876404edcSAsim Jamshed if (offset + datalen > (tcph->doff<<2)) { 107976404edcSAsim Jamshed TRACE_ERROR("TCP setting has gone out of bounds " 108076404edcSAsim Jamshed "(offset: %ld, datalen: %d)\n", 108176404edcSAsim Jamshed offset, datalen); 108276404edcSAsim Jamshed errno = EINVAL; 108376404edcSAsim Jamshed return -1; 108476404edcSAsim Jamshed } 108576404edcSAsim Jamshed memcpy((uint8_t *)tcph + offset, data, datalen); 108676404edcSAsim Jamshed /* update tcp seq # */ 108776404edcSAsim Jamshed cur_pkt_ctx->p.seq = ntohl(tcph->seq); 108876404edcSAsim Jamshed /* update tcp ack_seq # */ 108976404edcSAsim Jamshed cur_pkt_ctx->p.ack_seq = ntohl(tcph->ack_seq); 109076404edcSAsim Jamshed /* update tcp window */ 109176404edcSAsim Jamshed cur_pkt_ctx->p.window = ntohs(tcph->window); 109276404edcSAsim Jamshed 109376404edcSAsim Jamshed /* 150422 dhkim TODO: seq and offset are two different form of same 109476404edcSAsim Jamshed * variable. We also need to update the offset. */ 109576404edcSAsim Jamshed } 109676404edcSAsim Jamshed if (option & MOS_CHOMP) { 109776404edcSAsim Jamshed memmove((uint8_t *)tcph + offset, 109876404edcSAsim Jamshed (uint8_t *)tcph + offset + datalen, 109976404edcSAsim Jamshed cur_pkt_ctx->p.payloadlen + (tcph->doff<<2) 110076404edcSAsim Jamshed - offset - datalen); 110176404edcSAsim Jamshed /* update payload ptr */ 110276404edcSAsim Jamshed cur_pkt_ctx->p.payload = (uint8_t *)tcph + (tcph->doff<<2); 110376404edcSAsim Jamshed } else if (option & MOS_INSERT) { 110476404edcSAsim Jamshed memmove((uint8_t *)tcph + offset + datalen, 110576404edcSAsim Jamshed (uint8_t *)tcph + offset + 1, 110676404edcSAsim Jamshed cur_pkt_ctx->p.payloadlen + (tcph->doff<<2) 110776404edcSAsim Jamshed - offset); 110876404edcSAsim Jamshed memcpy((uint8_t *)tcph + offset, data, datalen); 110976404edcSAsim Jamshed /* update payload ptr */ 111076404edcSAsim Jamshed cur_pkt_ctx->p.payload = (uint8_t *)tcph + (tcph->doff<<2); 111176404edcSAsim Jamshed } 111276404edcSAsim Jamshed } else if (option & MOS_TCP_PAYLOAD) { 111376404edcSAsim Jamshed iph = (struct iphdr *)(cur_pkt_ctx->p.ethh + 1); 111476404edcSAsim Jamshed tcph = (struct tcphdr *)((uint8_t *)iph + (iph->ihl<<2)); 111576404edcSAsim Jamshed payload = (uint8_t *)tcph + (tcph->doff<<2); 111676404edcSAsim Jamshed if (option & MOS_OVERWRITE) { 111776404edcSAsim Jamshed if (offset + datalen > ntohs(iph->tot_len) - 111876404edcSAsim Jamshed (iph->ihl<<2) - (tcph->doff<<2)) { 111976404edcSAsim Jamshed TRACE_ERROR("Payload setting has gone out of bounds " 112076404edcSAsim Jamshed "(offset: %ld, datalen: %d)\n", 112176404edcSAsim Jamshed offset, datalen); 112276404edcSAsim Jamshed errno = EINVAL; 112376404edcSAsim Jamshed return -1; 112476404edcSAsim Jamshed } 112576404edcSAsim Jamshed memcpy(payload + offset, data, datalen); 112676404edcSAsim Jamshed } 112776404edcSAsim Jamshed if (option & MOS_CHOMP) { 112876404edcSAsim Jamshed memmove(payload + offset, 112976404edcSAsim Jamshed payload + offset + datalen, 113076404edcSAsim Jamshed (cur_pkt_ctx->p.payloadlen - 113176404edcSAsim Jamshed offset - datalen)); 113276404edcSAsim Jamshed /* update payload length */ 113376404edcSAsim Jamshed cur_pkt_ctx->p.payloadlen = cur_pkt_ctx->p.ip_len - 113476404edcSAsim Jamshed (tcph->doff<<2) - (iph->ihl<<2); 113576404edcSAsim Jamshed } else if (option & MOS_INSERT) { 113676404edcSAsim Jamshed memmove(payload + offset + datalen, 113776404edcSAsim Jamshed payload + offset + 1, 113876404edcSAsim Jamshed cur_pkt_ctx->p.payloadlen - offset); 113976404edcSAsim Jamshed memcpy(payload + offset, data, datalen); 114076404edcSAsim Jamshed cur_pkt_ctx->p.payloadlen = cur_pkt_ctx->p.ip_len - 114176404edcSAsim Jamshed (tcph->doff<<2) - (iph->ihl<<2); 114276404edcSAsim Jamshed } 114376404edcSAsim Jamshed } else { 114476404edcSAsim Jamshed TRACE_ERROR("Invalid option!\n"); 114576404edcSAsim Jamshed errno = EINVAL; 114676404edcSAsim Jamshed return -1; 114776404edcSAsim Jamshed } 114876404edcSAsim Jamshed 114976404edcSAsim Jamshed /* update ip checksum */ 115076404edcSAsim Jamshed if (option & MOS_UPDATE_IP_CHKSUM) { 115176404edcSAsim Jamshed iph = (struct iphdr *)(cur_pkt_ctx->p.ethh + 1); 115276404edcSAsim Jamshed iph->check = 0; 115376404edcSAsim Jamshed iph->check = ip_fast_csum(iph, iph->ihl); 115476404edcSAsim Jamshed } 115576404edcSAsim Jamshed 115676404edcSAsim Jamshed /* update tcp checksum */ 115776404edcSAsim Jamshed if (option & MOS_UPDATE_TCP_CHKSUM) { 115876404edcSAsim Jamshed iph = (struct iphdr *)(cur_pkt_ctx->p.ethh + 1); 115976404edcSAsim Jamshed tcph = (struct tcphdr *)((uint8_t *)iph + (iph->ihl<<2)); 116076404edcSAsim Jamshed tcph->check = 0; 116176404edcSAsim Jamshed tcph->check = TCPCalcChecksum((uint16_t *)tcph, 116276404edcSAsim Jamshed ntohs(iph->tot_len) - (iph->ihl<<2), 116376404edcSAsim Jamshed iph->saddr, iph->daddr); 116476404edcSAsim Jamshed } 116576404edcSAsim Jamshed return 0; 116676404edcSAsim Jamshed } 116776404edcSAsim Jamshed /*----------------------------------------------------------------------------*/ 116876404edcSAsim Jamshed #if 0 116976404edcSAsim Jamshed inline int 117076404edcSAsim Jamshed mtcp_cb_updatecurpkt(mctx_t mctx, off_t offset, unsigned char *data, 117176404edcSAsim Jamshed uint16_t datalen, int option) 117276404edcSAsim Jamshed { 117376404edcSAsim Jamshed return mtcp_setlastpkt(mctx, sock, side, offset, data, datalen, option); 117476404edcSAsim Jamshed } 117576404edcSAsim Jamshed #endif 117676404edcSAsim Jamshed /*----------------------------------------------------------------------------*/ 117776404edcSAsim Jamshed /** 117876404edcSAsim Jamshed * THIS IS A DEPRECETED FUNCTION... 117976404edcSAsim Jamshed */ 118076404edcSAsim Jamshed int 118176404edcSAsim Jamshed mtcp_cb_dropcurpkt(mctx_t mctx) 118276404edcSAsim Jamshed { 118376404edcSAsim Jamshed mtcp_manager_t mtcp; 118476404edcSAsim Jamshed 118576404edcSAsim Jamshed /* checking if mtcp is valid */ 118676404edcSAsim Jamshed mtcp = GetMTCPManager(mctx); 118776404edcSAsim Jamshed if (!mtcp) { 118876404edcSAsim Jamshed TRACE_ERROR("Invalid mtcp!\n"); 118976404edcSAsim Jamshed errno = EACCES; 119076404edcSAsim Jamshed return -1; 119176404edcSAsim Jamshed } 119276404edcSAsim Jamshed 119376404edcSAsim Jamshed /* check if the calling thread is in MOS context */ 119476404edcSAsim Jamshed if (mtcp->ctx->thread != pthread_self()) { 119576404edcSAsim Jamshed TRACE_ERROR("Invalid thread id!\n"); 119676404edcSAsim Jamshed errno = EPERM; 119776404edcSAsim Jamshed return -1; 119876404edcSAsim Jamshed } 119976404edcSAsim Jamshed 120076404edcSAsim Jamshed /* see if cur_pkt_ctx is valid */ 120176404edcSAsim Jamshed if (mtcp->pctx == NULL) { 120276404edcSAsim Jamshed TRACE_ERROR("pctx is NULL!\n"); 120376404edcSAsim Jamshed errno = ENODATA; 120476404edcSAsim Jamshed return -1; 120576404edcSAsim Jamshed } 120676404edcSAsim Jamshed 120776404edcSAsim Jamshed mtcp->pctx->forward = 0; 120876404edcSAsim Jamshed 120976404edcSAsim Jamshed return 0; 121076404edcSAsim Jamshed } 121176404edcSAsim Jamshed /*----------------------------------------------------------------------------*/ 121276404edcSAsim Jamshed int 121376404edcSAsim Jamshed mtcp_set_debug_string(mtcp_manager_t mtcp, const char *fmt, ...) 121476404edcSAsim Jamshed { 121576404edcSAsim Jamshed #ifdef ENABLE_DEBUG_EVENT 121676404edcSAsim Jamshed va_list args; 121776404edcSAsim Jamshed int i; 121876404edcSAsim Jamshed 121976404edcSAsim Jamshed assert(mtcp); 122076404edcSAsim Jamshed 122176404edcSAsim Jamshed if (fmt == NULL) { 122276404edcSAsim Jamshed mtcp->dbg_buf[0] = '\0'; 122376404edcSAsim Jamshed return 0; 122476404edcSAsim Jamshed } 122576404edcSAsim Jamshed 122676404edcSAsim Jamshed va_start(args, fmt); 122776404edcSAsim Jamshed i = vsnprintf(mtcp->dbg_buf, DBG_BUF_LEN - 1, fmt, args); 122876404edcSAsim Jamshed va_end(args); 122976404edcSAsim Jamshed 123076404edcSAsim Jamshed return i; 123176404edcSAsim Jamshed #else 123276404edcSAsim Jamshed return -1; 123376404edcSAsim Jamshed #endif /* ENABLE_DEBUG_EVENT */ 123476404edcSAsim Jamshed } 123576404edcSAsim Jamshed /*----------------------------------------------------------------------------*/ 123676404edcSAsim Jamshed int 123776404edcSAsim Jamshed mtcp_get_debug_string(mctx_t mctx, char *buf, int len) 123876404edcSAsim Jamshed { 123976404edcSAsim Jamshed #ifdef ENABLE_DEBUG_EVENT 124076404edcSAsim Jamshed mtcp_manager_t mtcp; 124176404edcSAsim Jamshed int copylen; 124276404edcSAsim Jamshed 124376404edcSAsim Jamshed if (len < 0) 124476404edcSAsim Jamshed return -1; 124576404edcSAsim Jamshed else if (len == 0) 124676404edcSAsim Jamshed return 0; 124776404edcSAsim Jamshed 124876404edcSAsim Jamshed if (!(mtcp = GetMTCPManager(mctx))) 124976404edcSAsim Jamshed return -1; 125076404edcSAsim Jamshed 125176404edcSAsim Jamshed copylen = MIN(strlen(mtcp->dbg_buf), len); 125276404edcSAsim Jamshed strncpy(buf, mtcp->dbg_buf, copylen); 125376404edcSAsim Jamshed 125476404edcSAsim Jamshed return copylen; 125576404edcSAsim Jamshed #else 125676404edcSAsim Jamshed return -1; 125776404edcSAsim Jamshed #endif /* ENABLE_DEBUG_EVENT */ 125876404edcSAsim Jamshed } 125976404edcSAsim Jamshed /*----------------------------------------------------------------------------*/ 1260