]>
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" | |
993e3d8e | 32 | #include "pim_instance.h" |
62fde409 | 33 | #include "pim_sock.h" |
d9ff4302 | 34 | #include "pim_errors.h" |
2a333e0f | 35 | |
36 | #include "pim_msdp.h" | |
37 | #include "pim_msdp_socket.h" | |
38 | ||
b069b568 AMR |
39 | #include "sockopt.h" |
40 | ||
2a333e0f | 41 | /* increase socket send buffer size */ |
d62a17ae | 42 | static void pim_msdp_update_sock_send_buffer_size(int fd) |
2a333e0f | 43 | { |
d62a17ae | 44 | int size = PIM_MSDP_SOCKET_SNDBUF_SIZE; |
45 | int optval; | |
46 | socklen_t optlen = sizeof(optval); | |
47 | ||
48 | if (getsockopt(fd, SOL_SOCKET, SO_SNDBUF, &optval, &optlen) < 0) { | |
450971aa | 49 | flog_err_sys(EC_LIB_SOCKET, |
1d5453d6 | 50 | "getsockopt of SO_SNDBUF failed %s", |
09c866e3 | 51 | safe_strerror(errno)); |
d62a17ae | 52 | return; |
53 | } | |
54 | ||
55 | if (optval < size) { | |
56 | if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &size, sizeof(size)) | |
57 | < 0) { | |
450971aa | 58 | flog_err_sys(EC_LIB_SOCKET, |
1d5453d6 | 59 | "Couldn't increase send buffer: %s", |
09c866e3 | 60 | safe_strerror(errno)); |
d62a17ae | 61 | } |
62 | } | |
2a333e0f | 63 | } |
64 | ||
65 | /* passive peer socket accept */ | |
cc9f21da | 66 | static void pim_msdp_sock_accept(struct thread *thread) |
2a333e0f | 67 | { |
d62a17ae | 68 | union sockunion su; |
472ad383 | 69 | struct pim_instance *pim = THREAD_ARG(thread); |
d62a17ae | 70 | int accept_sock; |
71 | int msdp_sock; | |
72 | struct pim_msdp_peer *mp; | |
d62a17ae | 73 | |
74 | sockunion_init(&su); | |
75 | ||
76 | /* re-register accept thread */ | |
77 | accept_sock = THREAD_FD(thread); | |
78 | if (accept_sock < 0) { | |
1c50c1c0 QY |
79 | flog_err(EC_LIB_DEVELOPMENT, "accept_sock is negative value %d", |
80 | accept_sock); | |
cc9f21da | 81 | return; |
d62a17ae | 82 | } |
472ad383 | 83 | pim->msdp.listener.thread = NULL; |
36417fcc | 84 | thread_add_read(router->master, pim_msdp_sock_accept, pim, accept_sock, |
472ad383 | 85 | &pim->msdp.listener.thread); |
d62a17ae | 86 | |
87 | /* accept client connection. */ | |
88 | msdp_sock = sockunion_accept(accept_sock, &su); | |
89 | if (msdp_sock < 0) { | |
450971aa | 90 | flog_err_sys(EC_LIB_SOCKET, "pim_msdp_sock_accept failed (%s)", |
09c866e3 | 91 | safe_strerror(errno)); |
cc9f21da | 92 | return; |
d62a17ae | 93 | } |
94 | ||
95 | /* see if have peer config for this */ | |
472ad383 | 96 | mp = pim_msdp_peer_find(pim, su.sin.sin_addr); |
d62a17ae | 97 | if (!mp || !PIM_MSDP_PEER_IS_LISTENER(mp)) { |
472ad383 | 98 | ++pim->msdp.rejected_accepts; |
d62a17ae | 99 | if (PIM_DEBUG_MSDP_EVENTS) { |
298004a1 | 100 | flog_err(EC_PIM_MSDP_PACKET, |
6b73800b | 101 | "msdp peer connection refused from %pSU", &su); |
d62a17ae | 102 | } |
103 | close(msdp_sock); | |
cc9f21da | 104 | return; |
d62a17ae | 105 | } |
106 | ||
107 | if (PIM_DEBUG_MSDP_INTERNAL) { | |
108 | zlog_debug("MSDP peer %s accept success%s", mp->key_str, | |
109 | mp->fd >= 0 ? "(dup)" : ""); | |
110 | } | |
111 | ||
112 | /* if we have an existing connection we need to kill that one | |
113 | * with this one */ | |
114 | if (mp->fd >= 0) { | |
115 | if (PIM_DEBUG_MSDP_EVENTS) { | |
3613d898 | 116 | zlog_notice( |
6b73800b DS |
117 | "msdp peer new connection from %pSU stop old connection", |
118 | &su); | |
d62a17ae | 119 | } |
120 | pim_msdp_peer_stop_tcp_conn(mp, true /* chg_state */); | |
121 | } | |
122 | mp->fd = msdp_sock; | |
123 | set_nonblocking(mp->fd); | |
124 | pim_msdp_update_sock_send_buffer_size(mp->fd); | |
125 | pim_msdp_peer_established(mp); | |
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) { | |
1c50c1c0 | 144 | flog_err_sys(EC_LIB_SOCKET, "socket: %s", safe_strerror(errno)); |
d62a17ae | 145 | return sock; |
146 | } | |
147 | ||
148 | memset(&sin, 0, sizeof(struct sockaddr_in)); | |
149 | sin.sin_family = AF_INET; | |
150 | sin.sin_port = htons(PIM_MSDP_TCP_PORT); | |
151 | socklen = sizeof(struct sockaddr_in); | |
2a333e0f | 152 | #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN |
d62a17ae | 153 | sin.sin_len = socklen; |
2a333e0f | 154 | #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */ |
155 | ||
d62a17ae | 156 | sockopt_reuseaddr(sock); |
157 | sockopt_reuseport(sock); | |
158 | ||
d3cc1e45 | 159 | if (pim->vrf->vrf_id != VRF_DEFAULT) { |
2267994c | 160 | struct interface *ifp = |
d3cc1e45 | 161 | if_lookup_by_name(pim->vrf->name, pim->vrf->vrf_id); |
449a3f5b | 162 | if (!ifp) { |
450971aa | 163 | flog_err(EC_LIB_INTERFACE, |
1c50c1c0 | 164 | "%s: Unable to lookup vrf interface: %s", |
5e81f5dd | 165 | __func__, pim->vrf->name); |
e691f179 DS |
166 | close(sock); |
167 | return -1; | |
168 | } | |
169 | if (pim_socket_bind(sock, ifp)) { | |
450971aa | 170 | flog_err_sys(EC_LIB_SOCKET, |
09c866e3 | 171 | "%s: Unable to bind to socket: %s", |
5e81f5dd | 172 | __func__, safe_strerror(errno)); |
e691f179 | 173 | close(sock); |
449a3f5b DS |
174 | return -1; |
175 | } | |
2267994c DS |
176 | } |
177 | ||
0cf6db21 | 178 | frr_with_privs(&pimd_privs) { |
6bb30c2c DL |
179 | /* bind to well known TCP port */ |
180 | rc = bind(sock, (struct sockaddr *)&sin, socklen); | |
d62a17ae | 181 | } |
182 | ||
183 | if (rc < 0) { | |
450971aa | 184 | flog_err_sys(EC_LIB_SOCKET, |
09c866e3 QY |
185 | "pim_msdp_socket bind to port %d: %s", |
186 | ntohs(sin.sin_port), safe_strerror(errno)); | |
d62a17ae | 187 | close(sock); |
188 | return rc; | |
189 | } | |
190 | ||
191 | rc = listen(sock, 3 /* backlog */); | |
192 | if (rc < 0) { | |
450971aa | 193 | flog_err_sys(EC_LIB_SOCKET, "pim_msdp_socket listen: %s", |
09c866e3 | 194 | safe_strerror(errno)); |
d62a17ae | 195 | close(sock); |
196 | return rc; | |
197 | } | |
198 | ||
b069b568 AMR |
199 | /* Set socket DSCP byte */ |
200 | if (setsockopt_ipv4_tos(sock, IPTOS_PREC_INTERNETCONTROL)) { | |
201 | zlog_warn("can't set sockopt IP_TOS to MSDP socket %d: %s", | |
202 | sock, safe_strerror(errno)); | |
203 | } | |
204 | ||
d62a17ae | 205 | /* add accept thread */ |
206 | listener->fd = sock; | |
207 | memcpy(&listener->su, &sin, socklen); | |
472ad383 | 208 | thread_add_read(pim->msdp.master, pim_msdp_sock_accept, pim, sock, |
d62a17ae | 209 | &listener->thread); |
210 | ||
472ad383 | 211 | pim->msdp.flags |= PIM_MSDPF_LISTENER; |
d62a17ae | 212 | return 0; |
2a333e0f | 213 | } |
214 | ||
215 | /* active peer socket setup */ | |
d62a17ae | 216 | int pim_msdp_sock_connect(struct pim_msdp_peer *mp) |
2a333e0f | 217 | { |
d62a17ae | 218 | int rc; |
219 | ||
220 | if (PIM_DEBUG_MSDP_INTERNAL) { | |
221 | zlog_debug("MSDP peer %s attempt connect%s", mp->key_str, | |
222 | mp->fd < 0 ? "" : "(dup)"); | |
223 | } | |
224 | ||
225 | /* if we have an existing connection we need to kill that one | |
226 | * with this one */ | |
227 | if (mp->fd >= 0) { | |
228 | if (PIM_DEBUG_MSDP_EVENTS) { | |
3613d898 | 229 | zlog_notice( |
d62a17ae | 230 | "msdp duplicate connect to %s nuke old connection", |
231 | mp->key_str); | |
232 | } | |
233 | pim_msdp_peer_stop_tcp_conn(mp, false /* chg_state */); | |
234 | } | |
235 | ||
236 | /* Make socket for the peer. */ | |
237 | mp->fd = sockunion_socket(&mp->su_peer); | |
238 | if (mp->fd < 0) { | |
450971aa | 239 | flog_err_sys(EC_LIB_SOCKET, |
09c866e3 QY |
240 | "pim_msdp_socket socket failure: %s", |
241 | safe_strerror(errno)); | |
d62a17ae | 242 | return -1; |
243 | } | |
244 | ||
d3cc1e45 DS |
245 | if (mp->pim->vrf->vrf_id != VRF_DEFAULT) { |
246 | struct interface *ifp = if_lookup_by_name(mp->pim->vrf->name, | |
247 | mp->pim->vrf->vrf_id); | |
449a3f5b | 248 | if (!ifp) { |
450971aa | 249 | flog_err(EC_LIB_INTERFACE, |
1c50c1c0 | 250 | "%s: Unable to lookup vrf interface: %s", |
15569c58 | 251 | __func__, mp->pim->vrf->name); |
449a3f5b DS |
252 | return -1; |
253 | } | |
e691f179 | 254 | if (pim_socket_bind(mp->fd, ifp)) { |
450971aa | 255 | flog_err_sys(EC_LIB_SOCKET, |
09c866e3 | 256 | "%s: Unable to bind to socket: %s", |
15569c58 | 257 | __func__, 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) { | |
450971aa | 274 | flog_err_sys(EC_LIB_SOCKET, |
09c866e3 QY |
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 | ||
b069b568 AMR |
282 | /* Set socket DSCP byte */ |
283 | if (setsockopt_ipv4_tos(mp->fd, IPTOS_PREC_INTERNETCONTROL)) { | |
284 | zlog_warn("can't set sockopt IP_TOS to MSDP socket %d: %s", | |
285 | mp->fd, safe_strerror(errno)); | |
286 | } | |
287 | ||
d62a17ae | 288 | /* Connect to the remote mp. */ |
289 | return (sockunion_connect(mp->fd, &mp->su_peer, | |
290 | htons(PIM_MSDP_TCP_PORT), 0)); | |
2a333e0f | 291 | } |