1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410
|
/*
* Copyright (c) 2014 Cisco Systems, Inc. All rights reserved.
* Copyright (c) 2017 Amazon.com, Inc. or its affiliates.
* All Rights reserved.
* Portions of this software copied from libfabric
* (https://github.com/ofiwg/libfabric)
*
* LICENSE_BEGIN
*
* BSD license:
*
* Redistribution and use in source and binary forms, with or
* without modification, are permitted provided that the following
* conditions are met:
*
* - Redistributions of source code must retain the above
* copyright notice, this list of conditions and the following
* disclaimer.
*
* - Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* LICENSE_END
*
*
*/
#include "opal_config.h"
#include <arpa/inet.h>
#include <errno.h>
#include <time.h>
#ifdef HAVE_NETINET_IN_H
# include <netinet/in.h>
#endif
#include "libnl_utils.h"
/* Adapt this copied code for Open MPI */
#include "opal/util/output.h"
static struct nla_policy route_policy[RTA_MAX + 1] = {
[RTA_IIF] =
{
.type = NLA_STRING,
.maxlen = IFNAMSIZ,
},
[RTA_OIF] = {.type = NLA_U32},
[RTA_PRIORITY] = {.type = NLA_U32},
[RTA_FLOW] = {.type = NLA_U32},
[RTA_MP_ALGO] = {.type = NLA_U32},
[RTA_CACHEINFO] = {.minlen = sizeof(struct rta_cacheinfo)},
[RTA_METRICS] = {.type = NLA_NESTED},
[RTA_MULTIPATH] = {.type = NLA_NESTED},
};
static int opal_reachable_netlink_is_nlreply_expected(struct opal_reachable_netlink_sk *unlsk,
struct nlmsghdr *nlm_hdr)
{
#if OPAL_ENABLE_DEBUG
if (nlm_hdr->nlmsg_pid != nl_socket_get_local_port(unlsk->nlh)
|| nlm_hdr->nlmsg_seq != unlsk->seq) {
opal_output(
0, "Not an expected reply msg pid: %u local pid: %u msg seq: %u expected seq: %u\n",
nlm_hdr->nlmsg_pid, nl_socket_get_local_port(unlsk->nlh), nlm_hdr->nlmsg_seq,
unlsk->seq);
return 0;
}
#endif
return 1;
}
static int opal_reachable_netlink_is_nlreply_err(struct nlmsghdr *nlm_hdr)
{
if (nlm_hdr->nlmsg_type == NLMSG_ERROR) {
struct nlmsgerr *e = (struct nlmsgerr *) nlmsg_data(nlm_hdr);
if (nlm_hdr->nlmsg_len >= (__u32) NLMSG_SIZE(sizeof(*e)))
opal_output_verbose(20, 0, "Received a netlink error message");
else
opal_output_verbose(20, 0, "Received a truncated netlink error message\n");
return 1;
}
return 0;
}
static int opal_reachable_netlink_send_query(struct opal_reachable_netlink_sk *unlsk,
struct nl_msg *msg, int protocol, int flag)
{
struct nlmsghdr *nlhdr;
nlhdr = nlmsg_hdr(msg);
nlhdr->nlmsg_pid = nl_socket_get_local_port(unlsk->nlh);
nlhdr->nlmsg_seq = ++unlsk->seq;
nlmsg_set_proto(msg, protocol);
nlhdr->nlmsg_flags = flag;
return nl_send(unlsk->nlh, msg);
}
static int opal_reachable_netlink_set_rcvsk_timer(NL_HANDLE *nlh)
{
int err = 0;
struct timeval timeout;
timeout.tv_sec = 1;
timeout.tv_usec = 0;
err = setsockopt(nl_socket_get_fd(nlh), SOL_SOCKET, SO_RCVTIMEO, (char *) &timeout,
sizeof(timeout));
#if OPAL_ENABLE_DEBUG
if (err < 0)
opal_output(0, "Failed to set SO_RCVTIMEO for nl socket");
#endif
return err;
}
static int opal_reachable_netlink_sk_alloc(struct opal_reachable_netlink_sk **p_sk, int protocol)
{
struct opal_reachable_netlink_sk *unlsk;
NL_HANDLE *nlh;
int err;
unlsk = calloc(1, sizeof(*unlsk));
if (!unlsk) {
opal_output(0, "Failed to allocate opal_reachable_netlink_sk struct\n");
return ENOMEM;
}
nlh = NL_HANDLE_ALLOC();
if (!nlh) {
opal_output(0, "Failed to allocate nl handle\n");
err = ENOMEM;
goto err_free_unlsk;
}
err = nl_connect(nlh, protocol);
if (err < 0) {
opal_output(0, "Failed to connect netlink route socket error: %s\n", NL_GETERROR(err));
err = EINVAL;
goto err_free_nlh;
}
NL_DISABLE_SEQ_CHECK(nlh);
err = opal_reachable_netlink_set_rcvsk_timer(nlh);
if (err < 0)
goto err_close_nlh;
unlsk->nlh = nlh;
unlsk->seq = time(NULL);
*p_sk = unlsk;
return 0;
err_close_nlh:
nl_close(nlh);
err_free_nlh:
NL_HANDLE_FREE(nlh);
err_free_unlsk:
free(unlsk);
return err;
}
static void opal_reachable_netlink_sk_free(struct opal_reachable_netlink_sk *unlsk)
{
nl_close(unlsk->nlh);
NL_HANDLE_FREE(unlsk->nlh);
free(unlsk);
}
static int opal_reachable_netlink_rt_raw_parse_cb(struct nl_msg *msg, void *arg)
{
struct opal_reachable_netlink_rt_cb_arg *lookup_arg = (struct opal_reachable_netlink_rt_cb_arg
*) arg;
struct opal_reachable_netlink_sk *unlsk = lookup_arg->unlsk;
struct nlmsghdr *nlm_hdr = nlmsg_hdr(msg);
struct rtmsg *rtm;
struct nlattr *tb[RTA_MAX + 1];
int found = 0;
int err;
INC_CB_MSGCNT(lookup_arg);
if (!opal_reachable_netlink_is_nlreply_expected(unlsk, nlm_hdr)) {
#if OPAL_ENABLE_DEBUG
nl_msg_dump(msg, stderr);
#endif
return NL_SKIP;
}
if (opal_reachable_netlink_is_nlreply_err(nlm_hdr)) {
#if OPAL_ENABLE_DEBUG
nl_msg_dump(msg, stderr);
#endif
return NL_SKIP;
}
if (nlm_hdr->nlmsg_type != RTM_NEWROUTE) {
#if OPAL_ENABLE_DEBUG
char buf[128];
nl_nlmsgtype2str(nlm_hdr->nlmsg_type, buf, sizeof(buf));
opal_output(0, "Received an invalid route request reply message type: %s\n", buf);
nl_msg_dump(msg, stderr);
#endif
return NL_SKIP;
}
rtm = nlmsg_data(nlm_hdr);
if (rtm->rtm_family != AF_INET
#if OPAL_ENABLE_IPV6
&& rtm->rtm_family != AF_INET6
#endif
) {
#if OPAL_ENABLE_DEBUG
opal_output(0, "RTM message contains invalid AF family: %u\n", rtm->rtm_family);
nl_msg_dump(msg, stderr);
#endif
return NL_SKIP;
}
err = nlmsg_parse(nlm_hdr, sizeof(struct rtmsg), tb, RTA_MAX, route_policy);
if (err < 0) {
#if OPAL_ENABLE_DEBUG
opal_output(0, "nlmsg parse error %s\n", NL_GETERROR(err));
nl_msg_dump(msg, stderr);
#endif
return NL_SKIP;
}
if (tb[RTA_OIF]) {
if (nla_get_u32(tb[RTA_OIF]) == (uint32_t) lookup_arg->oif)
found = 1;
else
/* usually, this means that there is a route to the remote
host, but that it's not through the given interface. For
our purposes, that means it's not reachable. */
opal_output_verbose(
20, 0, "Retrieved route has a different outgoing interface %d (expected %d)\n",
nla_get_u32(tb[RTA_OIF]), lookup_arg->oif);
}
if (found && tb[RTA_GATEWAY]) {
lookup_arg->has_gateway = 1;
}
lookup_arg->found = found;
return NL_STOP;
}
int opal_reachable_netlink_rt_lookup(uint32_t src_addr, uint32_t dst_addr, int outgoing_interface,
int *has_gateway)
{
struct opal_reachable_netlink_sk *unlsk; /* netlink socket */
struct nl_msg *nlm; /* netlink message */
struct rtmsg rmsg; /* route message */
struct opal_reachable_netlink_rt_cb_arg arg; /* callback argument */
int err;
/* allocate netlink socket */
unlsk = NULL;
err = opal_reachable_netlink_sk_alloc(&unlsk, NETLINK_ROUTE);
if (err)
return err;
/* allocate route message */
memset(&rmsg, 0, sizeof(rmsg));
rmsg.rtm_family = AF_INET;
rmsg.rtm_dst_len = sizeof(dst_addr) * CHAR_BIT;
rmsg.rtm_src_len = sizeof(src_addr) * CHAR_BIT;
/* allocate netlink message of type RTM_GETROUTE */
nlm = nlmsg_alloc_simple(RTM_GETROUTE, 0);
if (!nlm) {
opal_output(0, "Failed to alloc nl message, %s\n", NL_GETERROR(err));
err = ENOMEM;
goto out;
}
/* append route message and addresses to netlink message. */
nlmsg_append(nlm, &rmsg, sizeof(rmsg), NLMSG_ALIGNTO);
nla_put_u32(nlm, RTA_DST, dst_addr);
nla_put_u32(nlm, RTA_SRC, src_addr);
/* query kernel */
err = opal_reachable_netlink_send_query(unlsk, nlm, NETLINK_ROUTE, NLM_F_REQUEST);
nlmsg_free(nlm);
if (err < 0) {
opal_output(0, "Failed to send RTM_GETROUTE query message, error %s\n", NL_GETERROR(err));
err = EINVAL;
goto out;
}
/* Setup callback function */
memset(&arg, 0, sizeof(arg));
arg.oif = outgoing_interface;
arg.unlsk = unlsk;
err = nl_socket_modify_cb(unlsk->nlh, NL_CB_MSG_IN, NL_CB_CUSTOM,
opal_reachable_netlink_rt_raw_parse_cb, &arg);
if (err != 0) {
opal_output(0, "Failed to setup callback function, error %s\n", NL_GETERROR(err));
err = EINVAL;
goto out;
}
/* receive results */
NL_RECVMSGS(unlsk->nlh, arg, EHOSTUNREACH, err, out);
/* check whether a route was found */
if (arg.found) {
*has_gateway = arg.has_gateway;
err = 0;
} else {
*has_gateway = 0;
err = EHOSTUNREACH;
}
out:
opal_reachable_netlink_sk_free(unlsk);
return err;
}
#if OPAL_ENABLE_IPV6
int opal_reachable_netlink_rt_lookup6(struct in6_addr *src_addr, struct in6_addr *dst_addr,
int outgoing_interface, int *has_gateway)
{
struct opal_reachable_netlink_sk *unlsk; /* netlink socket */
struct nl_msg *nlm; /* netlink message */
struct rtmsg rmsg; /* route message */
struct opal_reachable_netlink_rt_cb_arg arg; /* callback argument */
int err;
/* allocate netlink socket */
unlsk = NULL;
err = opal_reachable_netlink_sk_alloc(&unlsk, NETLINK_ROUTE);
if (err)
return err;
/* allocate route message */
memset(&rmsg, 0, sizeof(rmsg));
rmsg.rtm_family = AF_INET6;
rmsg.rtm_dst_len = sizeof(*dst_addr) * CHAR_BIT;
rmsg.rtm_src_len = sizeof(*src_addr) * CHAR_BIT;
/* allocate netlink message of type RTM_GETROUTE */
nlm = nlmsg_alloc_simple(RTM_GETROUTE, 0);
if (!nlm) {
opal_output(0, "Failed to alloc nl message, %s\n", NL_GETERROR(err));
err = ENOMEM;
goto out;
}
/* append route message and addresses to netlink message. */
nlmsg_append(nlm, &rmsg, sizeof(rmsg), NLMSG_ALIGNTO);
nla_put(nlm, RTA_DST, sizeof(dst_addr->s6_addr), &(dst_addr->s6_addr));
nla_put(nlm, RTA_SRC, sizeof(src_addr->s6_addr), &(src_addr->s6_addr));
/* query kernel */
err = opal_reachable_netlink_send_query(unlsk, nlm, NETLINK_ROUTE, NLM_F_REQUEST);
nlmsg_free(nlm);
if (err < 0) {
opal_output(0, "Failed to send RTM_GETROUTE query message, error %s\n", NL_GETERROR(err));
err = EINVAL;
goto out;
}
/* Setup callback function */
memset(&arg, 0, sizeof(arg));
arg.oif = outgoing_interface;
arg.unlsk = unlsk;
err = nl_socket_modify_cb(unlsk->nlh, NL_CB_MSG_IN, NL_CB_CUSTOM,
opal_reachable_netlink_rt_raw_parse_cb, &arg);
if (err != 0) {
opal_output(0, "Failed to setup callback function, error %s\n", NL_GETERROR(err));
err = EINVAL;
goto out;
}
/* receive results */
NL_RECVMSGS(unlsk->nlh, arg, EHOSTUNREACH, err, out);
/* check whether a route was found */
if (arg.found) {
*has_gateway = arg.has_gateway;
err = 0;
} else {
*has_gateway = 0;
err = EHOSTUNREACH;
}
out:
opal_reachable_netlink_sk_free(unlsk);
return err;
}
#endif /* #if OPAL_ENABLE_IPV6 */
|