Skip to content

Commit

Permalink
slirp: Rework internal configuration
Browse files Browse the repository at this point in the history
The user mode IP stack is currently only minimally configurable /wrt to
its virtual IP addresses. This is unfortunate if some guest has a fixed
idea of which IP addresses to use.

Therefore this patch prepares the stack for fully configurable IP
addresses and masks. The user interface and default addresses remain
untouched in this step, they will be enhanced in the following patch.

Signed-off-by: Jan Kiszka <[email protected]>
Signed-off-by: Anthony Liguori <[email protected]>
  • Loading branch information
jan-kiszka authored and Anthony Liguori committed Jun 29, 2009
1 parent ad196a9 commit a13a412
Show file tree
Hide file tree
Showing 13 changed files with 149 additions and 142 deletions.
26 changes: 10 additions & 16 deletions slirp/bootp.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@

#define NB_ADDR 16

#define START_ADDR 15

#define LEASE_TIME (24 * 3600)

typedef struct {
Expand Down Expand Up @@ -64,20 +62,20 @@ static BOOTPClient *get_new_addr(struct in_addr *paddr,
found:
bc = &bootp_clients[i];
bc->allocated = 1;
paddr->s_addr = htonl(ntohl(special_addr.s_addr) | (i + START_ADDR));
paddr->s_addr = vdhcp_startaddr.s_addr + htonl(i);
return bc;
}

static BOOTPClient *request_addr(const struct in_addr *paddr,
const uint8_t *macaddr)
{
uint32_t req_addr = ntohl(paddr->s_addr);
uint32_t spec_addr = ntohl(special_addr.s_addr);
uint32_t dhcp_addr = ntohl(vdhcp_startaddr.s_addr);
BOOTPClient *bc;

if (req_addr >= (spec_addr | START_ADDR) &&
req_addr < (spec_addr | (NB_ADDR + START_ADDR))) {
bc = &bootp_clients[(req_addr & 0xff) - START_ADDR];
if (req_addr >= dhcp_addr &&
req_addr < (dhcp_addr + NB_ADDR)) {
bc = &bootp_clients[req_addr - dhcp_addr];
if (!bc->allocated || !memcmp(macaddr, bc->macaddr, 6)) {
bc->allocated = 1;
return bc;
Expand All @@ -99,7 +97,7 @@ static BOOTPClient *find_addr(struct in_addr *paddr, const uint8_t *macaddr)
found:
bc = &bootp_clients[i];
bc->allocated = 1;
paddr->s_addr = htonl(ntohl(special_addr.s_addr) | (i + START_ADDR));
paddr->s_addr = vdhcp_startaddr.s_addr + htonl(i);
return bc;
}

Expand Down Expand Up @@ -156,7 +154,6 @@ static void bootp_reply(const struct bootp_t *bp)
struct mbuf *m;
struct bootp_t *rbp;
struct sockaddr_in saddr, daddr;
struct in_addr dns_addr;
const struct in_addr *preq_addr;
int dhcp_msg_type, val;
uint8_t *q;
Expand Down Expand Up @@ -218,7 +215,7 @@ static void bootp_reply(const struct bootp_t *bp)
}
}

saddr.sin_addr.s_addr = htonl(ntohl(special_addr.s_addr) | CTL_ALIAS);
saddr.sin_addr = vhost_addr;
saddr.sin_port = htons(BOOTP_SERVER);

daddr.sin_port = htons(BOOTP_CLIENT);
Expand Down Expand Up @@ -262,10 +259,8 @@ static void bootp_reply(const struct bootp_t *bp)

*q++ = RFC1533_NETMASK;
*q++ = 4;
*q++ = 0xff;
*q++ = 0xff;
*q++ = 0xff;
*q++ = 0x00;
memcpy(q, &vnetwork_mask, 4);
q += 4;

if (!slirp_restrict) {
*q++ = RFC1533_GATEWAY;
Expand All @@ -275,8 +270,7 @@ static void bootp_reply(const struct bootp_t *bp)

*q++ = RFC1533_DNS;
*q++ = 4;
dns_addr.s_addr = htonl(ntohl(special_addr.s_addr) | CTL_DNS);
memcpy(q, &dns_addr, 4);
memcpy(q, &vnameserver_addr, 4);
q += 4;
}

Expand Down
7 changes: 0 additions & 7 deletions slirp/ctl.h

This file was deleted.

15 changes: 6 additions & 9 deletions slirp/ip_icmp.c
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ icmp_input(struct mbuf *m, int hlen)
case ICMP_ECHO:
icp->icmp_type = ICMP_ECHOREPLY;
ip->ip_len += hlen; /* since ip_input subtracts this */
if (ip->ip_dst.s_addr == alias_addr.s_addr) {
if (ip->ip_dst.s_addr == vhost_addr.s_addr) {
icmp_reflect(m);
} else {
struct socket *so;
Expand All @@ -134,16 +134,13 @@ icmp_input(struct mbuf *m, int hlen)

/* Send the packet */
addr.sin_family = AF_INET;
if ((so->so_faddr.s_addr & htonl(0xffffff00)) == special_addr.s_addr) {
if ((so->so_faddr.s_addr & vnetwork_mask.s_addr) ==
vnetwork_addr.s_addr) {
/* It's an alias */
switch(ntohl(so->so_faddr.s_addr) & 0xff) {
case CTL_DNS:
if (so->so_faddr.s_addr == vnameserver_addr.s_addr) {
addr.sin_addr = dns_addr;
break;
case CTL_ALIAS:
default:
} else {
addr.sin_addr = loopback_addr;
break;
}
} else {
addr.sin_addr = so->so_faddr;
Expand Down Expand Up @@ -302,7 +299,7 @@ icmp_error(struct mbuf *msrc, u_char type, u_char code, int minsize,
ip->ip_ttl = MAXTTL;
ip->ip_p = IPPROTO_ICMP;
ip->ip_dst = ip->ip_src; /* ip adresses */
ip->ip_src = alias_addr;
ip->ip_src = vhost_addr;

(void ) ip_output((struct socket *)NULL, m);

Expand Down
9 changes: 5 additions & 4 deletions slirp/ip_input.c
Original file line number Diff line number Diff line change
Expand Up @@ -134,18 +134,19 @@ ip_input(struct mbuf *m)
}

if (slirp_restrict) {
if (memcmp(&ip->ip_dst.s_addr, &special_addr, 3)) {
if ((ip->ip_dst.s_addr & vnetwork_mask.s_addr) ==
vnetwork_addr.s_addr) {
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)
if ((ip->ip_dst.s_addr & ~vnetwork_mask.s_addr) ==
~vnetwork_mask.s_addr)
goto bad;

for (ex_ptr = exec_list; ex_ptr; ex_ptr = ex_ptr->ex_next)
if (ex_ptr->ex_addr == host)
if (ex_ptr->ex_addr.s_addr == ip->ip_dst.s_addr)
break;

if (!ex_ptr)
Expand Down
9 changes: 5 additions & 4 deletions slirp/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,11 @@ extern char *slirp_tty;
extern char *exec_shell;
extern u_int curtime;
extern fd_set *global_readfds, *global_writefds, *global_xfds;
extern struct in_addr ctl_addr;
extern struct in_addr special_addr;
extern struct in_addr alias_addr;
extern struct in_addr vnetwork_addr;
extern struct in_addr vnetwork_mask;
extern struct in_addr vhost_addr;
extern struct in_addr vdhcp_startaddr;
extern struct in_addr vnameserver_addr;
extern struct in_addr our_addr;
extern struct in_addr loopback_addr;
extern struct in_addr dns_addr;
Expand All @@ -44,7 +46,6 @@ extern int towrite_max;
extern int ppp_exit;
extern int tcp_keepintvl;
extern uint8_t client_ethaddr[6];
extern const char *slirp_special_ip;
extern int slirp_restrict;
extern char *tftp_prefix;
extern char *bootp_filename;
Expand Down
9 changes: 5 additions & 4 deletions slirp/misc.c
Original file line number Diff line number Diff line change
Expand Up @@ -112,15 +112,16 @@ remque(void *a)
/* #endif */


int
add_exec(struct ex_list **ex_ptr, int do_pty, char *exec, int addr, int port)
int add_exec(struct ex_list **ex_ptr, int do_pty, char *exec,
struct in_addr addr, int port)
{
struct ex_list *tmp_ptr;

/* First, check if the port is "bound" */
for (tmp_ptr = *ex_ptr; tmp_ptr; tmp_ptr = tmp_ptr->ex_next) {
if (port == tmp_ptr->ex_fport && addr == tmp_ptr->ex_addr)
return -1;
if (port == tmp_ptr->ex_fport &&
addr.s_addr == tmp_ptr->ex_addr.s_addr)
return -1;
}

tmp_ptr = *ex_ptr;
Expand Down
4 changes: 2 additions & 2 deletions slirp/misc.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

struct ex_list {
int ex_pty; /* Do we want a pty? */
int ex_addr; /* The last byte of the address */
struct in_addr ex_addr; /* Server address */
int ex_fport; /* Port to telnet to */
const char *ex_exec; /* Command line of what to exec */
struct ex_list *ex_next;
Expand Down Expand Up @@ -74,7 +74,7 @@ void redir_x _P((u_int32_t, int, int, int));
void getouraddr _P((void));
void slirp_insque _P((void *, void *));
void slirp_remque _P((void *));
int add_exec _P((struct ex_list **, int, char *, int, int));
int add_exec _P((struct ex_list **, int, char *, struct in_addr, int));
int slirp_openpty _P((int *, int *));
int fork_exec(struct socket *so, const char *ex, int do_pty);
void snooze_hup _P((int));
Expand Down
Loading

0 comments on commit a13a412

Please sign in to comment.