]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * IP MSDP socket management | |
3 | * Copyright (C) 2016 Cumulus Networks, Inc. | |
4 | * | |
5 | * This program is free software; you can redistribute it and/or modify | |
6 | * it under the terms of the GNU General Public License as published by | |
7 | * the Free Software Foundation; either version 2 of the License, or | |
8 | * (at your option) any later version. | |
9 | * | |
10 | * This program is distributed in the hope that it will be useful, but | |
11 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 | * General Public License for more details. | |
14 | * | |
15 | * You should have received a copy of the GNU General Public License along | |
16 | * with this program; see the file COPYING; if not, write to the Free Software | |
17 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
18 | */ | |
19 | ||
20 | #include <zebra.h> | |
21 | ||
22 | #include <lib/log.h> | |
23 | #include <lib/network.h> | |
24 | #include <lib/sockunion.h> | |
25 | #include <lib/thread.h> | |
26 | #include <lib/vty.h> | |
27 | #include <lib/if.h> | |
28 | #include <lib/vrf.h> | |
29 | #include <lib/lib_errors.h> | |
30 | ||
31 | #include "pimd.h" | |
32 | #include "pim_sock.h" | |
33 | ||
34 | #include "pim_msdp.h" | |
35 | #include "pim_msdp_socket.h" | |
36 | ||
37 | /* increase socket send buffer size */ | |
38 | static void pim_msdp_update_sock_send_buffer_size(int fd) | |
39 | { | |
40 | int size = PIM_MSDP_SOCKET_SNDBUF_SIZE; | |
41 | int optval; | |
42 | socklen_t optlen = sizeof(optval); | |
43 | ||
44 | if (getsockopt(fd, SOL_SOCKET, SO_SNDBUF, &optval, &optlen) < 0) { | |
45 | zlog_ferr(LIB_ERR_SOCKET, "getsockopt of SO_SNDBUF failed %s\n", | |
46 | safe_strerror(errno)); | |
47 | return; | |
48 | } | |
49 | ||
50 | if (optval < size) { | |
51 | if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &size, sizeof(size)) | |
52 | < 0) { | |
53 | zlog_ferr(LIB_ERR_SOCKET, | |
54 | "Couldn't increase send buffer: %s\n", | |
55 | safe_strerror(errno)); | |
56 | } | |
57 | } | |
58 | } | |
59 | ||
60 | /* passive peer socket accept */ | |
61 | static int pim_msdp_sock_accept(struct thread *thread) | |
62 | { | |
63 | union sockunion su; | |
64 | struct pim_instance *pim = THREAD_ARG(thread); | |
65 | int accept_sock; | |
66 | int msdp_sock; | |
67 | struct pim_msdp_peer *mp; | |
68 | char buf[SU_ADDRSTRLEN]; | |
69 | ||
70 | sockunion_init(&su); | |
71 | ||
72 | /* re-register accept thread */ | |
73 | accept_sock = THREAD_FD(thread); | |
74 | if (accept_sock < 0) { | |
75 | zlog_ferr(LIB_ERR_DEVELOPMENT, | |
76 | "accept_sock is negative value %d", accept_sock); | |
77 | return -1; | |
78 | } | |
79 | pim->msdp.listener.thread = NULL; | |
80 | thread_add_read(master, pim_msdp_sock_accept, pim, accept_sock, | |
81 | &pim->msdp.listener.thread); | |
82 | ||
83 | /* accept client connection. */ | |
84 | msdp_sock = sockunion_accept(accept_sock, &su); | |
85 | if (msdp_sock < 0) { | |
86 | zlog_ferr(LIB_ERR_SOCKET, "pim_msdp_sock_accept failed (%s)", | |
87 | safe_strerror(errno)); | |
88 | return -1; | |
89 | } | |
90 | ||
91 | /* see if have peer config for this */ | |
92 | mp = pim_msdp_peer_find(pim, su.sin.sin_addr); | |
93 | if (!mp || !PIM_MSDP_PEER_IS_LISTENER(mp)) { | |
94 | ++pim->msdp.rejected_accepts; | |
95 | if (PIM_DEBUG_MSDP_EVENTS) { | |
96 | zlog_err("msdp peer connection refused from %s", | |
97 | sockunion2str(&su, buf, SU_ADDRSTRLEN)); | |
98 | } | |
99 | close(msdp_sock); | |
100 | return -1; | |
101 | } | |
102 | ||
103 | if (PIM_DEBUG_MSDP_INTERNAL) { | |
104 | zlog_debug("MSDP peer %s accept success%s", mp->key_str, | |
105 | mp->fd >= 0 ? "(dup)" : ""); | |
106 | } | |
107 | ||
108 | /* if we have an existing connection we need to kill that one | |
109 | * with this one */ | |
110 | if (mp->fd >= 0) { | |
111 | if (PIM_DEBUG_MSDP_EVENTS) { | |
112 | zlog_notice( | |
113 | "msdp peer new connection from %s stop old connection", | |
114 | sockunion2str(&su, buf, SU_ADDRSTRLEN)); | |
115 | } | |
116 | pim_msdp_peer_stop_tcp_conn(mp, true /* chg_state */); | |
117 | } | |
118 | mp->fd = msdp_sock; | |
119 | set_nonblocking(mp->fd); | |
120 | pim_msdp_update_sock_send_buffer_size(mp->fd); | |
121 | pim_msdp_peer_established(mp); | |
122 | return 0; | |
123 | } | |
124 | ||
125 | /* global listener for the MSDP well know TCP port */ | |
126 | int pim_msdp_sock_listen(struct pim_instance *pim) | |
127 | { | |
128 | int sock; | |
129 | int socklen; | |
130 | struct sockaddr_in sin; | |
131 | int rc; | |
132 | struct pim_msdp_listener *listener = &pim->msdp.listener; | |
133 | ||
134 | if (pim->msdp.flags & PIM_MSDPF_LISTENER) { | |
135 | /* listener already setup */ | |
136 | return 0; | |
137 | } | |
138 | ||
139 | sock = socket(AF_INET, SOCK_STREAM, 0); | |
140 | if (sock < 0) { | |
141 | zlog_ferr(LIB_ERR_SOCKET, "socket: %s", safe_strerror(errno)); | |
142 | return sock; | |
143 | } | |
144 | ||
145 | memset(&sin, 0, sizeof(struct sockaddr_in)); | |
146 | sin.sin_family = AF_INET; | |
147 | sin.sin_port = htons(PIM_MSDP_TCP_PORT); | |
148 | socklen = sizeof(struct sockaddr_in); | |
149 | #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN | |
150 | sin.sin_len = socklen; | |
151 | #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */ | |
152 | ||
153 | sockopt_reuseaddr(sock); | |
154 | sockopt_reuseport(sock); | |
155 | ||
156 | if (pim->vrf_id != VRF_DEFAULT) { | |
157 | struct interface *ifp = | |
158 | if_lookup_by_name(pim->vrf->name, pim->vrf_id); | |
159 | if (!ifp) { | |
160 | zlog_ferr(LIB_ERR_INTERFACE, | |
161 | "%s: Unable to lookup vrf interface: %s", | |
162 | __PRETTY_FUNCTION__, pim->vrf->name); | |
163 | close(sock); | |
164 | return -1; | |
165 | } | |
166 | if (pim_socket_bind(sock, ifp)) { | |
167 | zlog_ferr(LIB_ERR_SOCKET, | |
168 | "%s: Unable to bind to socket: %s", | |
169 | __PRETTY_FUNCTION__, safe_strerror(errno)); | |
170 | close(sock); | |
171 | return -1; | |
172 | } | |
173 | } | |
174 | ||
175 | if (pimd_privs.change(ZPRIVS_RAISE)) { | |
176 | zlog_ferr(LIB_ERR_PRIVILEGES, | |
177 | "pim_msdp_socket: could not raise privs, %s", | |
178 | safe_strerror(errno)); | |
179 | } | |
180 | ||
181 | /* bind to well known TCP port */ | |
182 | rc = bind(sock, (struct sockaddr *)&sin, socklen); | |
183 | ||
184 | if (pimd_privs.change(ZPRIVS_LOWER)) { | |
185 | zlog_ferr(LIB_ERR_PRIVILEGES, | |
186 | "pim_msdp_socket: could not lower privs, %s", | |
187 | safe_strerror(errno)); | |
188 | } | |
189 | ||
190 | if (rc < 0) { | |
191 | zlog_ferr(LIB_ERR_SOCKET, "pim_msdp_socket bind to port %d: %s", | |
192 | ntohs(sin.sin_port), safe_strerror(errno)); | |
193 | close(sock); | |
194 | return rc; | |
195 | } | |
196 | ||
197 | rc = listen(sock, 3 /* backlog */); | |
198 | if (rc < 0) { | |
199 | zlog_ferr(LIB_ERR_SOCKET, "pim_msdp_socket listen: %s", | |
200 | safe_strerror(errno)); | |
201 | close(sock); | |
202 | return rc; | |
203 | } | |
204 | ||
205 | /* add accept thread */ | |
206 | listener->fd = sock; | |
207 | memcpy(&listener->su, &sin, socklen); | |
208 | listener->thread = NULL; | |
209 | thread_add_read(pim->msdp.master, pim_msdp_sock_accept, pim, sock, | |
210 | &listener->thread); | |
211 | ||
212 | pim->msdp.flags |= PIM_MSDPF_LISTENER; | |
213 | return 0; | |
214 | } | |
215 | ||
216 | /* active peer socket setup */ | |
217 | int pim_msdp_sock_connect(struct pim_msdp_peer *mp) | |
218 | { | |
219 | int rc; | |
220 | ||
221 | if (PIM_DEBUG_MSDP_INTERNAL) { | |
222 | zlog_debug("MSDP peer %s attempt connect%s", mp->key_str, | |
223 | mp->fd < 0 ? "" : "(dup)"); | |
224 | } | |
225 | ||
226 | /* if we have an existing connection we need to kill that one | |
227 | * with this one */ | |
228 | if (mp->fd >= 0) { | |
229 | if (PIM_DEBUG_MSDP_EVENTS) { | |
230 | zlog_notice( | |
231 | "msdp duplicate connect to %s nuke old connection", | |
232 | mp->key_str); | |
233 | } | |
234 | pim_msdp_peer_stop_tcp_conn(mp, false /* chg_state */); | |
235 | } | |
236 | ||
237 | /* Make socket for the peer. */ | |
238 | mp->fd = sockunion_socket(&mp->su_peer); | |
239 | if (mp->fd < 0) { | |
240 | zlog_ferr(LIB_ERR_SOCKET, "pim_msdp_socket socket failure: %s", | |
241 | safe_strerror(errno)); | |
242 | return -1; | |
243 | } | |
244 | ||
245 | if (mp->pim->vrf_id != VRF_DEFAULT) { | |
246 | struct interface *ifp = | |
247 | if_lookup_by_name(mp->pim->vrf->name, mp->pim->vrf_id); | |
248 | if (!ifp) { | |
249 | zlog_ferr(LIB_ERR_INTERFACE, | |
250 | "%s: Unable to lookup vrf interface: %s", | |
251 | __PRETTY_FUNCTION__, mp->pim->vrf->name); | |
252 | return -1; | |
253 | } | |
254 | if (pim_socket_bind(mp->fd, ifp)) { | |
255 | zlog_ferr(LIB_ERR_SOCKET, | |
256 | "%s: Unable to bind to socket: %s", | |
257 | __PRETTY_FUNCTION__, safe_strerror(errno)); | |
258 | close(mp->fd); | |
259 | mp->fd = -1; | |
260 | return -1; | |
261 | } | |
262 | } | |
263 | ||
264 | set_nonblocking(mp->fd); | |
265 | ||
266 | /* Set socket send buffer size */ | |
267 | pim_msdp_update_sock_send_buffer_size(mp->fd); | |
268 | sockopt_reuseaddr(mp->fd); | |
269 | sockopt_reuseport(mp->fd); | |
270 | ||
271 | /* source bind */ | |
272 | rc = sockunion_bind(mp->fd, &mp->su_local, 0, &mp->su_local); | |
273 | if (rc < 0) { | |
274 | zlog_ferr(LIB_ERR_SOCKET, | |
275 | "pim_msdp_socket connect bind failure: %s", | |
276 | safe_strerror(errno)); | |
277 | close(mp->fd); | |
278 | mp->fd = -1; | |
279 | return rc; | |
280 | } | |
281 | ||
282 | /* Connect to the remote mp. */ | |
283 | return (sockunion_connect(mp->fd, &mp->su_peer, | |
284 | htons(PIM_MSDP_TCP_PORT), 0)); | |
285 | } |