From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1LL0U9-0000RI-6k for qemu-devel@nongnu.org; Thu, 08 Jan 2009 14:24:05 -0500 Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1LL0U8-0000Qn-IH for qemu-devel@nongnu.org; Thu, 08 Jan 2009 14:24:04 -0500 Received: from [199.232.76.173] (port=58825 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1LL0U8-0000Qc-AS for qemu-devel@nongnu.org; Thu, 08 Jan 2009 14:24:04 -0500 Received: from savannah.gnu.org ([199.232.41.3]:60882 helo=sv.gnu.org) by monty-python.gnu.org with esmtps (TLS-1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.60) (envelope-from ) id 1LL0U7-0003eE-Q6 for qemu-devel@nongnu.org; Thu, 08 Jan 2009 14:24:04 -0500 Received: from cvs.savannah.gnu.org ([199.232.41.69]) by sv.gnu.org with esmtp (Exim 4.63) (envelope-from ) id 1LL0U5-0006FB-Jm for qemu-devel@nongnu.org; Thu, 08 Jan 2009 19:24:01 +0000 Received: from aliguori by cvs.savannah.gnu.org with local (Exim 4.63) (envelope-from ) id 1LL0U4-0006F6-Us for qemu-devel@nongnu.org; Thu, 08 Jan 2009 19:24:01 +0000 MIME-Version: 1.0 Errors-To: aliguori Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit From: Anthony Liguori Message-Id: Date: Thu, 08 Jan 2009 19:24:01 +0000 Subject: [Qemu-devel] [6241] Add slirp_restrict option (Gleb Natapov) Reply-To: qemu-devel@nongnu.org List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Revision: 6241 http://svn.sv.gnu.org/viewvc/?view=rev&root=qemu&revision=6241 Author: aliguori Date: 2009-01-08 19:24:00 +0000 (Thu, 08 Jan 2009) Log Message: ----------- Add slirp_restrict option (Gleb Natapov) Add "slirp firewall" to permit connection only to vmchannel addresses. Signed-off-by: Gleb Natapov Signed-off-by: Anthony Liguori Modified Paths: -------------- trunk/net.c trunk/slirp/bootp.c trunk/slirp/ip_input.c trunk/slirp/libslirp.h trunk/slirp/main.h trunk/slirp/slirp.c trunk/slirp/tcp_input.c trunk/slirp/udp.c Modified: trunk/net.c =================================================================== --- trunk/net.c 2009-01-08 19:18:21 UTC (rev 6240) +++ trunk/net.c 2009-01-08 19:24:00 UTC (rev 6241) @@ -483,7 +483,7 @@ { if (!slirp_inited) { slirp_inited = 1; - slirp_init(); + slirp_init(0, NULL); } slirp_vc = qemu_new_vlan_client(vlan, model, name, slirp_receive, NULL, NULL); @@ -501,7 +501,7 @@ if (!slirp_inited) { slirp_inited = 1; - slirp_init(); + slirp_init(0, NULL); } p = redir_str; @@ -587,7 +587,7 @@ if (!slirp_inited) { slirp_inited = 1; - slirp_init(); + slirp_init(0, NULL); } /* XXX: better tmp dir construction */ Modified: trunk/slirp/bootp.c =================================================================== --- trunk/slirp/bootp.c 2009-01-08 19:18:21 UTC (rev 6240) +++ trunk/slirp/bootp.c 2009-01-08 19:24:00 UTC (rev 6241) @@ -219,16 +219,18 @@ *q++ = 0xff; *q++ = 0x00; - *q++ = RFC1533_GATEWAY; - *q++ = 4; - memcpy(q, &saddr.sin_addr, 4); - q += 4; + if (!slirp_restrict) { + *q++ = RFC1533_GATEWAY; + *q++ = 4; + memcpy(q, &saddr.sin_addr, 4); + q += 4; - *q++ = RFC1533_DNS; - *q++ = 4; - dns_addr.s_addr = htonl(ntohl(special_addr.s_addr) | CTL_DNS); - memcpy(q, &dns_addr, 4); - q += 4; + *q++ = RFC1533_DNS; + *q++ = 4; + dns_addr.s_addr = htonl(ntohl(special_addr.s_addr) | CTL_DNS); + memcpy(q, &dns_addr, 4); + q += 4; + } *q++ = RFC2132_LEASE_TIME; *q++ = 4; Modified: trunk/slirp/ip_input.c =================================================================== --- trunk/slirp/ip_input.c 2009-01-08 19:18:21 UTC (rev 6240) +++ trunk/slirp/ip_input.c 2009-01-08 19:24:00 UTC (rev 6241) @@ -136,6 +136,27 @@ STAT(ipstat.ips_tooshort++); goto bad; } + + if (slirp_restrict) { + if (memcmp(&ip->ip_dst.s_addr, &special_addr, 3)) { + if (ip->ip_dst.s_addr == 0xffffffff && ip->ip_p != IPPROTO_UDP) + goto bad; + } else { + int host = ntohl(ip->ip_dst.s_addr) & 0xff; + struct ex_list *ex_ptr; + + if (host == 0xff) + goto bad; + + for (ex_ptr = exec_list; ex_ptr; ex_ptr = ex_ptr->ex_next) + if (ex_ptr->ex_addr == host) + break; + + if (!ex_ptr) + goto bad; + } + } + /* Should drop packet if mbuf too long? hmmm... */ if (m->m_len > ip->ip_len) m_adj(m, ip->ip_len - m->m_len); Modified: trunk/slirp/libslirp.h =================================================================== --- trunk/slirp/libslirp.h 2009-01-08 19:18:21 UTC (rev 6240) +++ trunk/slirp/libslirp.h 2009-01-08 19:24:00 UTC (rev 6241) @@ -5,7 +5,7 @@ extern "C" { #endif -void slirp_init(void); +void slirp_init(int restrict, char *special_ip); void slirp_select_fill(int *pnfds, fd_set *readfds, fd_set *writefds, fd_set *xfds); Modified: trunk/slirp/main.h =================================================================== --- trunk/slirp/main.h 2009-01-08 19:18:21 UTC (rev 6240) +++ trunk/slirp/main.h 2009-01-08 19:24:00 UTC (rev 6241) @@ -44,6 +44,8 @@ extern int ppp_exit; extern int tcp_keepintvl; extern uint8_t client_ethaddr[6]; +extern char *slirp_special_ip; +extern int slirp_restrict; #define PROTO_SLIP 0x1 #ifdef USE_PPP Modified: trunk/slirp/slirp.c =================================================================== --- trunk/slirp/slirp.c 2009-01-08 19:18:21 UTC (rev 6240) +++ trunk/slirp/slirp.c 2009-01-08 19:24:00 UTC (rev 6241) @@ -46,6 +46,8 @@ static const uint8_t zero_ethaddr[6] = { 0, 0, 0, 0, 0, 0 }; +char *slirp_special_ip = CTL_SPECIAL; +int slirp_restrict; int do_slowtimo; int link_up; struct timeval tt; @@ -164,7 +166,7 @@ } #endif -void slirp_init(void) +void slirp_init(int restrict, char *special_ip) { // debug_init("/tmp/slirp.log", DEBUG_DEFAULT); @@ -177,6 +179,7 @@ #endif link_up = 1; + slirp_restrict = restrict; if_init(); ip_init(); @@ -192,7 +195,10 @@ fprintf (stderr, "Warning: No DNS servers found\n"); } - inet_aton(CTL_SPECIAL, &special_addr); + if (special_ip) + slirp_special_ip = special_ip; + + inet_aton(slirp_special_ip, &special_addr); alias_addr.s_addr = special_addr.s_addr | htonl(CTL_ALIAS); getouraddr(); } Modified: trunk/slirp/tcp_input.c =================================================================== --- trunk/slirp/tcp_input.c 2009-01-08 19:18:21 UTC (rev 6240) +++ trunk/slirp/tcp_input.c 2009-01-08 19:24:00 UTC (rev 6241) @@ -253,6 +253,7 @@ u_long tiwin; int ret; /* int ts_present = 0; */ + struct ex_list *ex_ptr; DEBUG_CALL("tcp_input"); DEBUG_ARGS((dfd," m = %8lx iphlen = %2d inso = %lx\n", @@ -363,6 +364,15 @@ m->m_data += sizeof(struct tcpiphdr)+off-sizeof(struct tcphdr); m->m_len -= sizeof(struct tcpiphdr)+off-sizeof(struct tcphdr); + if (slirp_restrict) { + for (ex_ptr = exec_list; ex_ptr; ex_ptr = ex_ptr->ex_next) + if (ex_ptr->ex_fport == ti->ti_dport && + (ntohl(ti->ti_dst.s_addr) & 0xff) == ex_ptr->ex_addr) + break; + + if (!ex_ptr) + goto drop; + } /* * Locate pcb for segment. */ @@ -646,7 +656,6 @@ #endif { /* May be an add exec */ - struct ex_list *ex_ptr; for(ex_ptr = exec_list; ex_ptr; ex_ptr = ex_ptr->ex_next) { if(ex_ptr->ex_fport == so->so_fport && lastbyte == ex_ptr->ex_addr) { Modified: trunk/slirp/udp.c =================================================================== --- trunk/slirp/udp.c 2009-01-08 19:18:21 UTC (rev 6240) +++ trunk/slirp/udp.c 2009-01-08 19:24:00 UTC (rev 6241) @@ -158,6 +158,9 @@ goto bad; } + if (slirp_restrict) + goto bad; + /* * handle TFTP */