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