Commit 9c12a6f24d8bfd0e0d81a4a77f515e32d15547c1
Committed by
Anthony Liguori
1 parent
6dd5ffb6
slirp: Do not allow to remove non-hostfwd sockets
Prevent that the users accidentally shoots down dynamic sockets. This allows to remove looping for removals as there can now only be one match. Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com> Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
Showing
2 changed files
with
9 additions
and
13 deletions
net.c
| @@ -880,7 +880,7 @@ void net_slirp_hostfwd_remove(Monitor *mon, const char *src_str) | @@ -880,7 +880,7 @@ void net_slirp_hostfwd_remove(Monitor *mon, const char *src_str) | ||
| 880 | char buf[256] = ""; | 880 | char buf[256] = ""; |
| 881 | const char *p = src_str; | 881 | const char *p = src_str; |
| 882 | int is_udp = 0; | 882 | int is_udp = 0; |
| 883 | - int n; | 883 | + int err; |
| 884 | 884 | ||
| 885 | if (!slirp_inited) { | 885 | if (!slirp_inited) { |
| 886 | monitor_printf(mon, "user mode network stack not in use\n"); | 886 | monitor_printf(mon, "user mode network stack not in use\n"); |
| @@ -909,10 +909,10 @@ void net_slirp_hostfwd_remove(Monitor *mon, const char *src_str) | @@ -909,10 +909,10 @@ void net_slirp_hostfwd_remove(Monitor *mon, const char *src_str) | ||
| 909 | 909 | ||
| 910 | host_port = atoi(p); | 910 | host_port = atoi(p); |
| 911 | 911 | ||
| 912 | - n = slirp_remove_hostfwd(is_udp, host_addr, host_port); | 912 | + err = slirp_remove_hostfwd(is_udp, host_addr, host_port); |
| 913 | 913 | ||
| 914 | - monitor_printf(mon, "removed %d host forwarding rules for %s\n", n, | ||
| 915 | - src_str); | 914 | + monitor_printf(mon, "host forwarding rule for %s %s\n", src_str, |
| 915 | + err ? "removed" : "not found"); | ||
| 916 | return; | 916 | return; |
| 917 | 917 | ||
| 918 | fail_syntax: | 918 | fail_syntax: |
slirp/slirp.c
| @@ -757,9 +757,7 @@ void if_encap(const uint8_t *ip_data, int ip_data_len) | @@ -757,9 +757,7 @@ void if_encap(const uint8_t *ip_data, int ip_data_len) | ||
| 757 | } | 757 | } |
| 758 | } | 758 | } |
| 759 | 759 | ||
| 760 | -/* Unlistens a redirection | ||
| 761 | - * | ||
| 762 | - * Return value: number of redirs removed */ | 760 | +/* Drop host forwarding rule, return 0 if found. */ |
| 763 | int slirp_remove_hostfwd(int is_udp, struct in_addr host_addr, int host_port) | 761 | int slirp_remove_hostfwd(int is_udp, struct in_addr host_addr, int host_port) |
| 764 | { | 762 | { |
| 765 | struct socket *so; | 763 | struct socket *so; |
| @@ -767,22 +765,20 @@ int slirp_remove_hostfwd(int is_udp, struct in_addr host_addr, int host_port) | @@ -767,22 +765,20 @@ int slirp_remove_hostfwd(int is_udp, struct in_addr host_addr, int host_port) | ||
| 767 | struct sockaddr_in addr; | 765 | struct sockaddr_in addr; |
| 768 | int port = htons(host_port); | 766 | int port = htons(host_port); |
| 769 | socklen_t addr_len; | 767 | socklen_t addr_len; |
| 770 | - int n = 0; | ||
| 771 | 768 | ||
| 772 | - loop_again: | ||
| 773 | for (so = head->so_next; so != head; so = so->so_next) { | 769 | for (so = head->so_next; so != head; so = so->so_next) { |
| 774 | addr_len = sizeof(addr); | 770 | addr_len = sizeof(addr); |
| 775 | - if (getsockname(so->s, (struct sockaddr *)&addr, &addr_len) == 0 && | 771 | + if ((so->so_state & SS_HOSTFWD) && |
| 772 | + getsockname(so->s, (struct sockaddr *)&addr, &addr_len) == 0 && | ||
| 776 | addr.sin_addr.s_addr == host_addr.s_addr && | 773 | addr.sin_addr.s_addr == host_addr.s_addr && |
| 777 | addr.sin_port == port) { | 774 | addr.sin_port == port) { |
| 778 | close(so->s); | 775 | close(so->s); |
| 779 | sofree(so); | 776 | sofree(so); |
| 780 | - n++; | ||
| 781 | - goto loop_again; | 777 | + return 0; |
| 782 | } | 778 | } |
| 783 | } | 779 | } |
| 784 | 780 | ||
| 785 | - return n; | 781 | + return -1; |
| 786 | } | 782 | } |
| 787 | 783 | ||
| 788 | int slirp_add_hostfwd(int is_udp, struct in_addr host_addr, int host_port, | 784 | int slirp_add_hostfwd(int is_udp, struct in_addr host_addr, int host_port, |