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