/* * Create new entry (from uspace) */ static int ip_masq_user_new(struct ip_masq_user *ums) { struct ip_masq *ms = NULL; unsigned mflags = 0; int ret; if (masq_proto_num (ums->protocol) == -1) { return EPROTONOSUPPORT; } if (ums->dport == 0) { ums->flags |= IP_MASQ_USER_F_LISTEN; } if (ums->flags | IP_MASQ_USER_F_LISTEN) { if ((ums->saddr == 0) || (ums->sport == 0)) { return EINVAL; } mflags |= (IP_MASQ_F_NO_DPORT|IP_MASQ_F_NO_DADDR); } if ((ret = ip_masq_user_maddr(ums)) < 0) { return -ret; } mflags |= IP_MASQ_F_USER; ms = ip_masq_new(ums->protocol, ums->maddr, ums->mport, ums->saddr, ums->sport, ums->daddr, ums->dport, mflags); if (ms == NULL) { /* * FIXME: ip_masq_new() should return errno */ return EBUSY; } /* * Setup timeouts for this new entry */ if (ums->timeout) { ms->timeout = ums->timeout; } else if (ums->flags | IP_MASQ_USER_F_LISTEN) { ip_masq_listen(ms); } masq_user_k2u(ms, ums); ip_masq_put(ms); return 0; }
int masq_ftp_out (struct ip_masq_app *mapp, struct ip_masq *ms, struct sk_buff **skb_p, __u32 maddr) { struct sk_buff *skb; struct iphdr *iph; struct tcphdr *th; char *p, *data, *data_limit; unsigned char p1,p2,p3,p4,p5,p6; __u32 from; __u16 port; struct ip_masq *n_ms; char buf[24]; /* xxx.xxx.xxx.xxx,ppp,ppp\000 */ unsigned buf_len; int diff; skb = *skb_p; iph = skb->nh.iph; th = (struct tcphdr *)&(((char *)iph)[iph->ihl*4]); data = (char *)&th[1]; data_limit = skb->h.raw + skb->len - 18; if (skb->len >= 6 && (memcmp(data, "PASV\r\n", 6) == 0 || memcmp(data, "pasv\r\n", 6) == 0)) ms->app_data = &masq_ftp_pasv; while (data < data_limit) { if (memcmp(data,"PORT ",5) && memcmp(data,"port ",5)) { data ++; continue; } p = data+5; p1 = simple_strtoul(data+5,&data,10); if (*data!=',') continue; p2 = simple_strtoul(data+1,&data,10); if (*data!=',') continue; p3 = simple_strtoul(data+1,&data,10); if (*data!=',') continue; p4 = simple_strtoul(data+1,&data,10); if (*data!=',') continue; p5 = simple_strtoul(data+1,&data,10); if (*data!=',') continue; p6 = simple_strtoul(data+1,&data,10); if (*data!='\r' && *data!='\n') continue; from = (p1<<24) | (p2<<16) | (p3<<8) | p4; port = (p5<<8) | p6; IP_MASQ_DEBUG(1-debug, "PORT %X:%X detected\n",from,port); /* * Now update or create an masquerade entry for it */ IP_MASQ_DEBUG(1-debug, "protocol %d %lX:%X %X:%X\n", iph->protocol, htonl(from), htons(port), iph->daddr, 0); n_ms = ip_masq_out_get(iph->protocol, htonl(from), htons(port), iph->daddr, 0); if (!n_ms) { n_ms = ip_masq_new(IPPROTO_TCP, maddr, 0, htonl(from), htons(port), iph->daddr, 0, IP_MASQ_F_NO_DPORT); if (n_ms==NULL) return 0; ip_masq_control_add(n_ms, ms); } /* * Replace the old PORT with the new one */ from = ntohl(n_ms->maddr); port = ntohs(n_ms->mport); sprintf(buf,"%d,%d,%d,%d,%d,%d", from>>24&255,from>>16&255,from>>8&255,from&255, port>>8&255,port&255); buf_len = strlen(buf); IP_MASQ_DEBUG(1-debug, "new PORT %X:%X\n",from,port); /* * Calculate required delta-offset to keep TCP happy */ diff = buf_len - (data-p); /* * No shift. */ if (diff==0) { /* * simple case, just replace the old PORT cmd */ memcpy(p,buf,buf_len); } else { *skb_p = ip_masq_skb_replace(skb, GFP_ATOMIC, p, data-p, buf, buf_len); } /* * Move tunnel to listen state */ ip_masq_listen(n_ms); ip_masq_put(n_ms); return diff; } return 0; }
int masq_irc_out (struct ip_masq_app *mapp, struct ip_masq *ms, struct sk_buff **skb_p, __u32 maddr) { struct sk_buff *skb; struct iphdr *iph; struct tcphdr *th; char *data, *data_limit; __u32 s_addr; __u16 s_port; struct ip_masq *n_ms; char buf[20]; /* "m_addr m_port" (dec base)*/ unsigned buf_len; int diff; char *dcc_p, *addr_beg_p, *addr_end_p; skb = *skb_p; iph = skb->nh.iph; th = (struct tcphdr *)&(((char *)iph)[iph->ihl*4]); data = (char *)&th[1]; /* * Hunt irc DCC string, the _shortest_: * * strlen("\1DCC CHAT chat AAAAAAAA P\1\n")=27 * strlen("\1DCC SCHAT chat AAAAAAAA P\1\n")=28 * strlen("\1DCC SEND F AAAAAAAA P S\1\n")=26 * strlen("\1DCC MOVE F AAAAAAAA P S\1\n")=26 * strlen("\1DCC TSEND F AAAAAAAA P S\1\n")=27 * AAAAAAAAA: bound addr (1.0.0.0==16777216, min 8 digits) * P: bound port (min 1 d ) * F: filename (min 1 d ) * S: size (min 1 d ) * 0x01, \n: terminators */ data_limit = skb->h.raw + skb->len; while (data < (data_limit - ( 22 + MAXMATCHLEN ) ) ) { int i; if (memcmp(data,"\1DCC ",5)) { data ++; continue; } dcc_p = data; data += 5; /* point to DCC cmd */ for(i=0; i<NUM_DCCPROTO; i++) { /* * go through the table and hunt a match string */ if( memcmp(data, dccprotos[i].match, dccprotos[i].matchlen ) == 0 ) { data += dccprotos[i].matchlen; /* * skip next string. */ while( *data++ != ' ') /* * must still parse, at least, "AAAAAAAA P\1\n", * 12 bytes left. */ if (data > (data_limit-12)) return 0; addr_beg_p = data; /* * client bound address in dec base */ s_addr = simple_strtoul(data,&data,10); if (*data++ !=' ') continue; /* * client bound port in dec base */ s_port = simple_strtoul(data,&data,10); addr_end_p = data; /* * Now create an masquerade entry for it * must set NO_DPORT and NO_DADDR because * connection is requested by another client. */ n_ms = ip_masq_new(IPPROTO_TCP, maddr, 0, htonl(s_addr),htons(s_port), 0, 0, IP_MASQ_F_NO_DPORT|IP_MASQ_F_NO_DADDR); if (n_ms==NULL) return 0; /* * Replace the old "address port" with the new one */ buf_len = sprintf(buf,"%lu %u", ntohl(n_ms->maddr),ntohs(n_ms->mport)); /* * Calculate required delta-offset to keep TCP happy */ diff = buf_len - (addr_end_p-addr_beg_p); *addr_beg_p = '\0'; IP_MASQ_DEBUG(1-debug, "masq_irc_out(): '%s' %X:%X detected (diff=%d)\n", dcc_p, s_addr,s_port, diff); /* * No shift. */ if (diff==0) { /* * simple case, just copy. */ memcpy(addr_beg_p,buf,buf_len); } else { *skb_p = ip_masq_skb_replace(skb, GFP_ATOMIC, addr_beg_p, addr_end_p-addr_beg_p, buf, buf_len); } ip_masq_listen(n_ms); ip_masq_put(n_ms); return diff; } } } return 0; }