For SNAT, don't store the pre-fragment L2 header before actions are applied.
[sliver-openvswitch.git] / lib / vconn-tcp.c
1 /* Copyright (c) 2008 The Board of Trustees of The Leland Stanford
2  * Junior University
3  * 
4  * We are making the OpenFlow specification and associated documentation
5  * (Software) available for public use and benefit with the expectation
6  * that others will use, modify and enhance the Software and contribute
7  * those enhancements back to the community. However, since we would
8  * like to make the Software available for broadest use, with as few
9  * restrictions as possible permission is hereby granted, free of
10  * charge, to any person obtaining a copy of this Software to deal in
11  * the Software under the copyrights without restriction, including
12  * without limitation the rights to use, copy, modify, merge, publish,
13  * distribute, sublicense, and/or sell copies of the Software, and to
14  * permit persons to whom the Software is furnished to do so, subject to
15  * the following conditions:
16  * 
17  * The above copyright notice and this permission notice shall be
18  * included in all copies or substantial portions of the Software.
19  * 
20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23  * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
24  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
25  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
26  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27  * SOFTWARE.
28  * 
29  * The name and trademarks of copyright holder(s) may NOT be used in
30  * advertising or publicity pertaining to the Software or any
31  * derivatives without specific, written prior permission.
32  */
33
34 #include <config.h>
35 #include "vconn.h"
36 #include <errno.h>
37 #include <inttypes.h>
38 #include <sys/types.h>
39 #include <netinet/in.h>
40 #include <netinet/tcp.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <unistd.h>
44 #include "packets.h"
45 #include "socket-util.h"
46 #include "util.h"
47 #include "openflow/openflow.h"
48 #include "vconn-provider.h"
49 #include "vconn-stream.h"
50
51 #include "vlog.h"
52 #define THIS_MODULE VLM_vconn_tcp
53
54 /* Active TCP. */
55
56 static int
57 new_tcp_vconn(const char *name, int fd, int connect_status,
58               const struct sockaddr_in *sin, struct vconn **vconnp)
59 {
60     int on = 1;
61     int retval;
62
63     retval = setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &on, sizeof on);
64     if (retval) {
65         VLOG_ERR("%s: setsockopt(TCP_NODELAY): %s", name, strerror(errno));
66         close(fd);
67         return errno;
68     }
69
70     return new_stream_vconn(name, fd, connect_status, sin->sin_addr.s_addr,
71                             vconnp);
72 }
73
74 static int
75 tcp_open(const char *name, char *suffix, struct vconn **vconnp)
76 {
77     char *save_ptr;
78     const char *host_name;
79     const char *port_string;
80     struct sockaddr_in sin;
81     int retval;
82     int fd;
83
84     /* Glibc 2.7 has a bug in strtok_r when compiling with optimization that
85      * can cause segfaults here:
86      * http://sources.redhat.com/bugzilla/show_bug.cgi?id=5614.
87      * Using "::" instead of the obvious ":" works around it. */
88     host_name = strtok_r(suffix, "::", &save_ptr);
89     port_string = strtok_r(NULL, "::", &save_ptr);
90     if (!host_name) {
91         ofp_error(0, "%s: bad peer name format", name);
92         return EAFNOSUPPORT;
93     }
94
95     memset(&sin, 0, sizeof sin);
96     sin.sin_family = AF_INET;
97     if (lookup_ip(host_name, &sin.sin_addr)) {
98         return ENOENT;
99     }
100     sin.sin_port = htons(port_string ? atoi(port_string) : OFP_TCP_PORT);
101
102     fd = socket(AF_INET, SOCK_STREAM, 0);
103     if (fd < 0) {
104         VLOG_ERR("%s: socket: %s", name, strerror(errno));
105         return errno;
106     }
107
108     retval = set_nonblocking(fd);
109     if (retval) {
110         close(fd);
111         return retval;
112     }
113
114     retval = connect(fd, (struct sockaddr *) &sin, sizeof sin);
115     if (retval < 0) {
116         if (errno == EINPROGRESS) {
117             return new_tcp_vconn(name, fd, EAGAIN, &sin, vconnp);
118         } else {
119             int error = errno;
120             VLOG_ERR("%s: connect: %s", name, strerror(error));
121             close(fd);
122             return error;
123         }
124     } else {
125         return new_tcp_vconn(name, fd, 0, &sin, vconnp);
126     }
127 }
128
129 struct vconn_class tcp_vconn_class = {
130     "tcp",                      /* name */
131     tcp_open,                   /* open */
132     NULL,                       /* close */
133     NULL,                       /* connect */
134     NULL,                       /* recv */
135     NULL,                       /* send */
136     NULL,                       /* wait */
137 };
138 \f
139 /* Passive TCP. */
140
141 static int ptcp_accept(int fd, const struct sockaddr *sa, size_t sa_len,
142                        struct vconn **vconnp);
143
144 static int
145 ptcp_open(const char *name, char *suffix, struct pvconn **pvconnp)
146 {
147     struct sockaddr_in sin;
148     int retval;
149     int fd;
150     unsigned int yes  = 1;
151
152     fd = socket(AF_INET, SOCK_STREAM, 0);
153     if (fd < 0) {
154         VLOG_ERR("%s: socket: %s", name, strerror(errno));
155         return errno;
156     }
157
158     if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof yes) < 0) {
159         VLOG_ERR("%s: setsockopt(SO_REUSEADDR): %s", name, strerror(errno));
160         return errno;
161     }
162
163     memset(&sin, 0, sizeof sin);
164     sin.sin_family = AF_INET;
165     sin.sin_addr.s_addr = htonl(INADDR_ANY);
166     sin.sin_port = htons(atoi(suffix) ? atoi(suffix) : OFP_TCP_PORT);
167     retval = bind(fd, (struct sockaddr *) &sin, sizeof sin);
168     if (retval < 0) {
169         int error = errno;
170         VLOG_ERR("%s: bind: %s", name, strerror(error));
171         close(fd);
172         return error;
173     }
174
175     return new_pstream_pvconn("ptcp", fd, ptcp_accept, pvconnp);
176 }
177
178 static int
179 ptcp_accept(int fd, const struct sockaddr *sa, size_t sa_len,
180             struct vconn **vconnp)
181 {
182     const struct sockaddr_in *sin = (const struct sockaddr_in *) sa;
183     char name[128];
184
185     if (sa_len == sizeof(struct sockaddr_in) && sin->sin_family == AF_INET) {
186         sprintf(name, "tcp:"IP_FMT, IP_ARGS(&sin->sin_addr));
187         if (sin->sin_port != htons(OFP_TCP_PORT)) {
188             sprintf(strchr(name, '\0'), ":%"PRIu16, ntohs(sin->sin_port));
189         }
190     } else {
191         strcpy(name, "tcp");
192     }
193     return new_tcp_vconn(name, fd, 0, sin, vconnp);
194 }
195
196 struct pvconn_class ptcp_pvconn_class = {
197     "ptcp",
198     ptcp_open,
199 };
200