]>
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" |
2a333e0f | 33 | |
34 | #include "pim_msdp.h" | |
35 | #include "pim_msdp_socket.h" | |
36 | ||
2a333e0f | 37 | /* increase socket send buffer size */ |
d62a17ae | 38 | static void pim_msdp_update_sock_send_buffer_size(int fd) |
2a333e0f | 39 | { |
d62a17ae | 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) { | |
3613d898 DS |
45 | zlog_ferr(LIB_ERR_SOCKET, "getsockopt of SO_SNDBUF failed %s\n", |
46 | safe_strerror(errno)); | |
d62a17ae | 47 | return; |
48 | } | |
49 | ||
50 | if (optval < size) { | |
51 | if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &size, sizeof(size)) | |
52 | < 0) { | |
3613d898 DS |
53 | zlog_ferr(LIB_ERR_SOCKET, |
54 | "Couldn't increase send buffer: %s\n", | |
55 | safe_strerror(errno)); | |
d62a17ae | 56 | } |
57 | } | |
2a333e0f | 58 | } |
59 | ||
60 | /* passive peer socket accept */ | |
d62a17ae | 61 | static int pim_msdp_sock_accept(struct thread *thread) |
2a333e0f | 62 | { |
d62a17ae | 63 | union sockunion su; |
472ad383 | 64 | struct pim_instance *pim = THREAD_ARG(thread); |
d62a17ae | 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) { | |
3613d898 DS |
75 | zlog_ferr(LIB_ERR_DEVELOPMENT, |
76 | "accept_sock is negative value %d", accept_sock); | |
d62a17ae | 77 | return -1; |
78 | } | |
472ad383 DS |
79 | pim->msdp.listener.thread = NULL; |
80 | thread_add_read(master, pim_msdp_sock_accept, pim, accept_sock, | |
81 | &pim->msdp.listener.thread); | |
d62a17ae | 82 | |
83 | /* accept client connection. */ | |
84 | msdp_sock = sockunion_accept(accept_sock, &su); | |
85 | if (msdp_sock < 0) { | |
3613d898 DS |
86 | zlog_ferr(LIB_ERR_SOCKET, "pim_msdp_sock_accept failed (%s)", |
87 | safe_strerror(errno)); | |
d62a17ae | 88 | return -1; |
89 | } | |
90 | ||
91 | /* see if have peer config for this */ | |
472ad383 | 92 | mp = pim_msdp_peer_find(pim, su.sin.sin_addr); |
d62a17ae | 93 | if (!mp || !PIM_MSDP_PEER_IS_LISTENER(mp)) { |
472ad383 | 94 | ++pim->msdp.rejected_accepts; |
d62a17ae | 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) { | |
3613d898 | 112 | zlog_notice( |
d62a17ae | 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; | |
2a333e0f | 123 | } |
124 | ||
125 | /* global listener for the MSDP well know TCP port */ | |
472ad383 | 126 | int pim_msdp_sock_listen(struct pim_instance *pim) |
2a333e0f | 127 | { |
d62a17ae | 128 | int sock; |
129 | int socklen; | |
130 | struct sockaddr_in sin; | |
131 | int rc; | |
472ad383 | 132 | struct pim_msdp_listener *listener = &pim->msdp.listener; |
d62a17ae | 133 | |
472ad383 | 134 | if (pim->msdp.flags & PIM_MSDPF_LISTENER) { |
d62a17ae | 135 | /* listener already setup */ |
136 | return 0; | |
137 | } | |
138 | ||
139 | sock = socket(AF_INET, SOCK_STREAM, 0); | |
140 | if (sock < 0) { | |
3613d898 | 141 | zlog_ferr(LIB_ERR_SOCKET, "socket: %s", safe_strerror(errno)); |
d62a17ae | 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); | |
2a333e0f | 149 | #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN |
d62a17ae | 150 | sin.sin_len = socklen; |
2a333e0f | 151 | #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */ |
152 | ||
d62a17ae | 153 | sockopt_reuseaddr(sock); |
154 | sockopt_reuseport(sock); | |
155 | ||
2267994c DS |
156 | if (pim->vrf_id != VRF_DEFAULT) { |
157 | struct interface *ifp = | |
158 | if_lookup_by_name(pim->vrf->name, pim->vrf_id); | |
449a3f5b | 159 | if (!ifp) { |
3613d898 DS |
160 | zlog_ferr(LIB_ERR_INTERFACE, |
161 | "%s: Unable to lookup vrf interface: %s", | |
162 | __PRETTY_FUNCTION__, pim->vrf->name); | |
e691f179 DS |
163 | close(sock); |
164 | return -1; | |
165 | } | |
166 | if (pim_socket_bind(sock, ifp)) { | |
3613d898 DS |
167 | zlog_ferr(LIB_ERR_SOCKET, |
168 | "%s: Unable to bind to socket: %s", | |
169 | __PRETTY_FUNCTION__, safe_strerror(errno)); | |
e691f179 | 170 | close(sock); |
449a3f5b DS |
171 | return -1; |
172 | } | |
2267994c DS |
173 | } |
174 | ||
d62a17ae | 175 | if (pimd_privs.change(ZPRIVS_RAISE)) { |
3613d898 DS |
176 | zlog_ferr(LIB_ERR_PRIVILEGES, |
177 | "pim_msdp_socket: could not raise privs, %s", | |
178 | safe_strerror(errno)); | |
d62a17ae | 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)) { | |
3613d898 DS |
185 | zlog_ferr(LIB_ERR_PRIVILEGES, |
186 | "pim_msdp_socket: could not lower privs, %s", | |
187 | safe_strerror(errno)); | |
d62a17ae | 188 | } |
189 | ||
190 | if (rc < 0) { | |
3613d898 DS |
191 | zlog_ferr(LIB_ERR_SOCKET, "pim_msdp_socket bind to port %d: %s", |
192 | ntohs(sin.sin_port), safe_strerror(errno)); | |
d62a17ae | 193 | close(sock); |
194 | return rc; | |
195 | } | |
196 | ||
197 | rc = listen(sock, 3 /* backlog */); | |
198 | if (rc < 0) { | |
3613d898 DS |
199 | zlog_ferr(LIB_ERR_SOCKET, "pim_msdp_socket listen: %s", |
200 | safe_strerror(errno)); | |
d62a17ae | 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; | |
472ad383 | 209 | thread_add_read(pim->msdp.master, pim_msdp_sock_accept, pim, sock, |
d62a17ae | 210 | &listener->thread); |
211 | ||
472ad383 | 212 | pim->msdp.flags |= PIM_MSDPF_LISTENER; |
d62a17ae | 213 | return 0; |
2a333e0f | 214 | } |
215 | ||
216 | /* active peer socket setup */ | |
d62a17ae | 217 | int pim_msdp_sock_connect(struct pim_msdp_peer *mp) |
2a333e0f | 218 | { |
d62a17ae | 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) { | |
3613d898 | 230 | zlog_notice( |
d62a17ae | 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) { | |
3613d898 DS |
240 | zlog_ferr(LIB_ERR_SOCKET, "pim_msdp_socket socket failure: %s", |
241 | safe_strerror(errno)); | |
d62a17ae | 242 | return -1; |
243 | } | |
244 | ||
62fde409 DS |
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); | |
449a3f5b | 248 | if (!ifp) { |
3613d898 DS |
249 | zlog_ferr(LIB_ERR_INTERFACE, |
250 | "%s: Unable to lookup vrf interface: %s", | |
251 | __PRETTY_FUNCTION__, mp->pim->vrf->name); | |
449a3f5b DS |
252 | return -1; |
253 | } | |
e691f179 | 254 | if (pim_socket_bind(mp->fd, ifp)) { |
3613d898 DS |
255 | zlog_ferr(LIB_ERR_SOCKET, |
256 | "%s: Unable to bind to socket: %s", | |
257 | __PRETTY_FUNCTION__, safe_strerror(errno)); | |
e691f179 DS |
258 | close(mp->fd); |
259 | mp->fd = -1; | |
260 | return -1; | |
261 | } | |
62fde409 DS |
262 | } |
263 | ||
d62a17ae | 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) { | |
3613d898 DS |
274 | zlog_ferr(LIB_ERR_SOCKET, |
275 | "pim_msdp_socket connect bind failure: %s", | |
276 | safe_strerror(errno)); | |
d62a17ae | 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)); | |
2a333e0f | 285 | } |