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