]>
Commit | Line | Data |
---|---|---|
2a333e0f | 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 | * | |
896014f4 DL |
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 | |
2a333e0f | 18 | */ |
19 | ||
20 | #include <zebra.h> | |
21 | ||
22 | #include <lib/log.h> | |
23 | #include <lib/network.h> | |
2a333e0f | 24 | #include <lib/sockunion.h> |
3c72d654 | 25 | #include <lib/thread.h> |
26 | #include <lib/vty.h> | |
62fde409 DS |
27 | #include <lib/if.h> |
28 | #include <lib/vrf.h> | |
3613d898 | 29 | #include <lib/lib_errors.h> |
2a333e0f | 30 | |
31 | #include "pimd.h" | |
62fde409 | 32 | #include "pim_sock.h" |
d9ff4302 | 33 | #include "pim_errors.h" |
2a333e0f | 34 | |
35 | #include "pim_msdp.h" | |
36 | #include "pim_msdp_socket.h" | |
37 | ||
2a333e0f | 38 | /* increase socket send buffer size */ |
d62a17ae | 39 | static void pim_msdp_update_sock_send_buffer_size(int fd) |
2a333e0f | 40 | { |
d62a17ae | 41 | int size = PIM_MSDP_SOCKET_SNDBUF_SIZE; |
42 | int optval; | |
43 | socklen_t optlen = sizeof(optval); | |
44 | ||
45 | if (getsockopt(fd, SOL_SOCKET, SO_SNDBUF, &optval, &optlen) < 0) { | |
09c866e3 QY |
46 | flog_err_sys(LIB_ERR_SOCKET, |
47 | "getsockopt of SO_SNDBUF failed %s\n", | |
48 | safe_strerror(errno)); | |
d62a17ae | 49 | return; |
50 | } | |
51 | ||
52 | if (optval < size) { | |
53 | if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &size, sizeof(size)) | |
54 | < 0) { | |
09c866e3 QY |
55 | flog_err_sys(LIB_ERR_SOCKET, |
56 | "Couldn't increase send buffer: %s\n", | |
57 | safe_strerror(errno)); | |
d62a17ae | 58 | } |
59 | } | |
2a333e0f | 60 | } |
61 | ||
62 | /* passive peer socket accept */ | |
d62a17ae | 63 | static int pim_msdp_sock_accept(struct thread *thread) |
2a333e0f | 64 | { |
d62a17ae | 65 | union sockunion su; |
472ad383 | 66 | struct pim_instance *pim = THREAD_ARG(thread); |
d62a17ae | 67 | int accept_sock; |
68 | int msdp_sock; | |
69 | struct pim_msdp_peer *mp; | |
70 | char buf[SU_ADDRSTRLEN]; | |
71 | ||
72 | sockunion_init(&su); | |
73 | ||
74 | /* re-register accept thread */ | |
75 | accept_sock = THREAD_FD(thread); | |
76 | if (accept_sock < 0) { | |
af4c2728 | 77 | flog_err(LIB_ERR_DEVELOPMENT, |
3613d898 | 78 | "accept_sock is negative value %d", accept_sock); |
d62a17ae | 79 | return -1; |
80 | } | |
472ad383 DS |
81 | pim->msdp.listener.thread = NULL; |
82 | thread_add_read(master, pim_msdp_sock_accept, pim, accept_sock, | |
83 | &pim->msdp.listener.thread); | |
d62a17ae | 84 | |
85 | /* accept client connection. */ | |
86 | msdp_sock = sockunion_accept(accept_sock, &su); | |
87 | if (msdp_sock < 0) { | |
09c866e3 QY |
88 | flog_err_sys(LIB_ERR_SOCKET, "pim_msdp_sock_accept failed (%s)", |
89 | safe_strerror(errno)); | |
d62a17ae | 90 | return -1; |
91 | } | |
92 | ||
93 | /* see if have peer config for this */ | |
472ad383 | 94 | mp = pim_msdp_peer_find(pim, su.sin.sin_addr); |
d62a17ae | 95 | if (!mp || !PIM_MSDP_PEER_IS_LISTENER(mp)) { |
472ad383 | 96 | ++pim->msdp.rejected_accepts; |
d62a17ae | 97 | if (PIM_DEBUG_MSDP_EVENTS) { |
af4c2728 | 98 | flog_err(PIM_ERR_MSDP_PACKET, |
d9ff4302 DS |
99 | "msdp peer connection refused from %s", |
100 | sockunion2str(&su, buf, SU_ADDRSTRLEN)); | |
d62a17ae | 101 | } |
102 | close(msdp_sock); | |
103 | return -1; | |
104 | } | |
105 | ||
106 | if (PIM_DEBUG_MSDP_INTERNAL) { | |
107 | zlog_debug("MSDP peer %s accept success%s", mp->key_str, | |
108 | mp->fd >= 0 ? "(dup)" : ""); | |
109 | } | |
110 | ||
111 | /* if we have an existing connection we need to kill that one | |
112 | * with this one */ | |
113 | if (mp->fd >= 0) { | |
114 | if (PIM_DEBUG_MSDP_EVENTS) { | |
3613d898 | 115 | zlog_notice( |
d62a17ae | 116 | "msdp peer new connection from %s stop old connection", |
117 | sockunion2str(&su, buf, SU_ADDRSTRLEN)); | |
118 | } | |
119 | pim_msdp_peer_stop_tcp_conn(mp, true /* chg_state */); | |
120 | } | |
121 | mp->fd = msdp_sock; | |
122 | set_nonblocking(mp->fd); | |
123 | pim_msdp_update_sock_send_buffer_size(mp->fd); | |
124 | pim_msdp_peer_established(mp); | |
125 | return 0; | |
2a333e0f | 126 | } |
127 | ||
128 | /* global listener for the MSDP well know TCP port */ | |
472ad383 | 129 | int pim_msdp_sock_listen(struct pim_instance *pim) |
2a333e0f | 130 | { |
d62a17ae | 131 | int sock; |
132 | int socklen; | |
133 | struct sockaddr_in sin; | |
134 | int rc; | |
472ad383 | 135 | struct pim_msdp_listener *listener = &pim->msdp.listener; |
d62a17ae | 136 | |
472ad383 | 137 | if (pim->msdp.flags & PIM_MSDPF_LISTENER) { |
d62a17ae | 138 | /* listener already setup */ |
139 | return 0; | |
140 | } | |
141 | ||
142 | sock = socket(AF_INET, SOCK_STREAM, 0); | |
143 | if (sock < 0) { | |
09c866e3 QY |
144 | flog_err_sys(LIB_ERR_SOCKET, "socket: %s", |
145 | safe_strerror(errno)); | |
d62a17ae | 146 | return sock; |
147 | } | |
148 | ||
149 | memset(&sin, 0, sizeof(struct sockaddr_in)); | |
150 | sin.sin_family = AF_INET; | |
151 | sin.sin_port = htons(PIM_MSDP_TCP_PORT); | |
152 | socklen = sizeof(struct sockaddr_in); | |
2a333e0f | 153 | #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN |
d62a17ae | 154 | sin.sin_len = socklen; |
2a333e0f | 155 | #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */ |
156 | ||
d62a17ae | 157 | sockopt_reuseaddr(sock); |
158 | sockopt_reuseport(sock); | |
159 | ||
2267994c DS |
160 | if (pim->vrf_id != VRF_DEFAULT) { |
161 | struct interface *ifp = | |
162 | if_lookup_by_name(pim->vrf->name, pim->vrf_id); | |
449a3f5b | 163 | if (!ifp) { |
af4c2728 | 164 | flog_err(LIB_ERR_INTERFACE, |
3613d898 DS |
165 | "%s: Unable to lookup vrf interface: %s", |
166 | __PRETTY_FUNCTION__, pim->vrf->name); | |
e691f179 DS |
167 | close(sock); |
168 | return -1; | |
169 | } | |
170 | if (pim_socket_bind(sock, ifp)) { | |
09c866e3 QY |
171 | flog_err_sys(LIB_ERR_SOCKET, |
172 | "%s: Unable to bind to socket: %s", | |
173 | __PRETTY_FUNCTION__, safe_strerror(errno)); | |
e691f179 | 174 | close(sock); |
449a3f5b DS |
175 | return -1; |
176 | } | |
2267994c DS |
177 | } |
178 | ||
d62a17ae | 179 | if (pimd_privs.change(ZPRIVS_RAISE)) { |
af4c2728 | 180 | flog_err(LIB_ERR_PRIVILEGES, |
3613d898 DS |
181 | "pim_msdp_socket: could not raise privs, %s", |
182 | safe_strerror(errno)); | |
d62a17ae | 183 | } |
184 | ||
185 | /* bind to well known TCP port */ | |
186 | rc = bind(sock, (struct sockaddr *)&sin, socklen); | |
187 | ||
188 | if (pimd_privs.change(ZPRIVS_LOWER)) { | |
af4c2728 | 189 | flog_err(LIB_ERR_PRIVILEGES, |
3613d898 DS |
190 | "pim_msdp_socket: could not lower privs, %s", |
191 | safe_strerror(errno)); | |
d62a17ae | 192 | } |
193 | ||
194 | if (rc < 0) { | |
09c866e3 QY |
195 | flog_err_sys(LIB_ERR_SOCKET, |
196 | "pim_msdp_socket bind to port %d: %s", | |
197 | ntohs(sin.sin_port), safe_strerror(errno)); | |
d62a17ae | 198 | close(sock); |
199 | return rc; | |
200 | } | |
201 | ||
202 | rc = listen(sock, 3 /* backlog */); | |
203 | if (rc < 0) { | |
09c866e3 QY |
204 | flog_err_sys(LIB_ERR_SOCKET, "pim_msdp_socket listen: %s", |
205 | safe_strerror(errno)); | |
d62a17ae | 206 | close(sock); |
207 | return rc; | |
208 | } | |
209 | ||
210 | /* add accept thread */ | |
211 | listener->fd = sock; | |
212 | memcpy(&listener->su, &sin, socklen); | |
213 | listener->thread = NULL; | |
472ad383 | 214 | thread_add_read(pim->msdp.master, pim_msdp_sock_accept, pim, sock, |
d62a17ae | 215 | &listener->thread); |
216 | ||
472ad383 | 217 | pim->msdp.flags |= PIM_MSDPF_LISTENER; |
d62a17ae | 218 | return 0; |
2a333e0f | 219 | } |
220 | ||
221 | /* active peer socket setup */ | |
d62a17ae | 222 | int pim_msdp_sock_connect(struct pim_msdp_peer *mp) |
2a333e0f | 223 | { |
d62a17ae | 224 | int rc; |
225 | ||
226 | if (PIM_DEBUG_MSDP_INTERNAL) { | |
227 | zlog_debug("MSDP peer %s attempt connect%s", mp->key_str, | |
228 | mp->fd < 0 ? "" : "(dup)"); | |
229 | } | |
230 | ||
231 | /* if we have an existing connection we need to kill that one | |
232 | * with this one */ | |
233 | if (mp->fd >= 0) { | |
234 | if (PIM_DEBUG_MSDP_EVENTS) { | |
3613d898 | 235 | zlog_notice( |
d62a17ae | 236 | "msdp duplicate connect to %s nuke old connection", |
237 | mp->key_str); | |
238 | } | |
239 | pim_msdp_peer_stop_tcp_conn(mp, false /* chg_state */); | |
240 | } | |
241 | ||
242 | /* Make socket for the peer. */ | |
243 | mp->fd = sockunion_socket(&mp->su_peer); | |
244 | if (mp->fd < 0) { | |
09c866e3 QY |
245 | flog_err_sys(LIB_ERR_SOCKET, |
246 | "pim_msdp_socket socket failure: %s", | |
247 | safe_strerror(errno)); | |
d62a17ae | 248 | return -1; |
249 | } | |
250 | ||
62fde409 DS |
251 | if (mp->pim->vrf_id != VRF_DEFAULT) { |
252 | struct interface *ifp = | |
253 | if_lookup_by_name(mp->pim->vrf->name, mp->pim->vrf_id); | |
449a3f5b | 254 | if (!ifp) { |
af4c2728 | 255 | flog_err(LIB_ERR_INTERFACE, |
3613d898 DS |
256 | "%s: Unable to lookup vrf interface: %s", |
257 | __PRETTY_FUNCTION__, mp->pim->vrf->name); | |
449a3f5b DS |
258 | return -1; |
259 | } | |
e691f179 | 260 | if (pim_socket_bind(mp->fd, ifp)) { |
09c866e3 QY |
261 | flog_err_sys(LIB_ERR_SOCKET, |
262 | "%s: Unable to bind to socket: %s", | |
263 | __PRETTY_FUNCTION__, safe_strerror(errno)); | |
e691f179 DS |
264 | close(mp->fd); |
265 | mp->fd = -1; | |
266 | return -1; | |
267 | } | |
62fde409 DS |
268 | } |
269 | ||
d62a17ae | 270 | set_nonblocking(mp->fd); |
271 | ||
272 | /* Set socket send buffer size */ | |
273 | pim_msdp_update_sock_send_buffer_size(mp->fd); | |
274 | sockopt_reuseaddr(mp->fd); | |
275 | sockopt_reuseport(mp->fd); | |
276 | ||
277 | /* source bind */ | |
278 | rc = sockunion_bind(mp->fd, &mp->su_local, 0, &mp->su_local); | |
279 | if (rc < 0) { | |
09c866e3 QY |
280 | flog_err_sys(LIB_ERR_SOCKET, |
281 | "pim_msdp_socket connect bind failure: %s", | |
282 | safe_strerror(errno)); | |
d62a17ae | 283 | close(mp->fd); |
284 | mp->fd = -1; | |
285 | return rc; | |
286 | } | |
287 | ||
288 | /* Connect to the remote mp. */ | |
289 | return (sockunion_connect(mp->fd, &mp->su_peer, | |
290 | htons(PIM_MSDP_TCP_PORT), 0)); | |
2a333e0f | 291 | } |