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