]> git.proxmox.com Git - mirror_frr.git/blobdiff - zebra/zebra_netns_id.c
Merge pull request #13458 from sri-mohan1/srib-ldpd
[mirror_frr.git] / zebra / zebra_netns_id.c
index 739ba330364e161a2713c7073041478e049a2aad..0531ab959100b11c02a23045866514c0e1944067 100644 (file)
@@ -1,20 +1,7 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
 /* zebra NETNS ID handling routines
  * those routines are implemented locally to avoid having external dependencies.
  * Copyright (C) 2018 6WIND
- *
- * This program is free software; you can redistribute it and/or modify it
- * under the terms of the GNU General Public License as published by the Free
- * Software Foundation; either version 2 of the License, or (at your option)
- * any later version.
- *
- * This program is distributed in the hope that it will be useful, but WITHOUT
- * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
- * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
- * more details.
- *
- * You should have received a copy of the GNU General Public License along
- * with this program; see the file COPYING; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
 #include <zebra.h>
@@ -23,6 +10,7 @@
 #include "vrf.h"
 #include "log.h"
 #include "lib_errors.h"
+#include "network.h"
 
 #include "zebra/rib.h"
 #include "zebra/zebra_dplane.h"
@@ -73,7 +61,7 @@ static struct nlmsghdr *initiate_nlh(char *buf, unsigned int *seq, int type)
        nlh->nlmsg_flags = NLM_F_REQUEST;
        if (type == RTM_NEWNSID)
                nlh->nlmsg_flags |= NLM_F_ACK;
-       nlh->nlmsg_seq = *seq = time(NULL);
+       nlh->nlmsg_seq = *seq = frr_sequence32_next();
        return nlh;
 }
 
@@ -255,66 +243,59 @@ ns_id_t zebra_ns_id_get(const char *netnspath, int fd_param)
                }
        }
 
-       if (ret <= 0) {
-               if (errno != EEXIST && ret != 0) {
-                       flog_err(
-                               EC_LIB_SOCKET,
-                               "netlink( %u) recvfrom() error 2 when reading: %s",
-                               fd, safe_strerror(errno));
-                       close(sock);
-                       if (netnspath)
-                               close(fd);
-                       if (errno == ENOTSUP) {
-                               zlog_debug("NEWNSID locally generated");
-                               return zebra_ns_id_get_fallback(netnspath);
-                       }
-                       return NS_UNKNOWN;
-               }
-               /* message to send to netlink : GETNSID */
-               memset(buf, 0, NETLINK_SOCKET_BUFFER_SIZE);
-               nlh = initiate_nlh(buf, &seq, RTM_GETNSID);
-               rt = (struct rtgenmsg *)(buf + nlh->nlmsg_len);
-               nlh->nlmsg_len += NETLINK_ALIGN(sizeof(struct rtgenmsg));
-               rt->rtgen_family = AF_UNSPEC;
-
-               nl_attr_put32(nlh, NETLINK_SOCKET_BUFFER_SIZE, NETNSA_FD, fd);
-               nl_attr_put32(nlh, NETLINK_SOCKET_BUFFER_SIZE, NETNSA_NSID,
-                             ns_id);
-
-               ret = send_receive(sock, nlh, seq, buf);
-               if (ret < 0) {
-                       close(sock);
-                       if (netnspath)
-                               close(fd);
-                       return NS_UNKNOWN;
+       if (errno != EEXIST && ret != 0) {
+               flog_err(EC_LIB_SOCKET,
+                        "netlink( %u) recvfrom() error 2 when reading: %s", fd,
+                        safe_strerror(errno));
+               close(sock);
+               if (netnspath)
+                       close(fd);
+               if (errno == ENOTSUP) {
+                       zlog_debug("NEWNSID locally generated");
+                       return zebra_ns_id_get_fallback(netnspath);
                }
-               nlh = (struct nlmsghdr *)buf;
-               len = ret;
-               ret = 0;
-               do {
-                       if (nlh->nlmsg_type >= NLMSG_MIN_TYPE) {
-                               return_nsid = extract_nsid(nlh, buf);
-                               if (return_nsid != NS_UNKNOWN)
-                                       break;
-                       } else if (nlh->nlmsg_type == NLMSG_ERROR) {
-                               struct nlmsgerr *err =
-                                       (struct nlmsgerr
-                                                *)((char *)nlh
-                                                   + NETLINK_ALIGN(sizeof(
-                                                             struct
-                                                             nlmsghdr)));
-                               if (err->error < 0)
-                                       errno = -err->error;
-                               else
-                                       errno = err->error;
-                               break;
-                       }
-                       len = len - NETLINK_ALIGN(nlh->nlmsg_len);
-                       nlh = (struct nlmsghdr *)((char *)nlh
-                                                 + NETLINK_ALIGN(
-                                                           nlh->nlmsg_len));
-               } while (len != 0 && ret == 0);
+               return NS_UNKNOWN;
        }
+       /* message to send to netlink : GETNSID */
+       memset(buf, 0, NETLINK_SOCKET_BUFFER_SIZE);
+       nlh = initiate_nlh(buf, &seq, RTM_GETNSID);
+       rt = (struct rtgenmsg *)(buf + nlh->nlmsg_len);
+       nlh->nlmsg_len += NETLINK_ALIGN(sizeof(struct rtgenmsg));
+       rt->rtgen_family = AF_UNSPEC;
+
+       nl_attr_put32(nlh, NETLINK_SOCKET_BUFFER_SIZE, NETNSA_FD, fd);
+       nl_attr_put32(nlh, NETLINK_SOCKET_BUFFER_SIZE, NETNSA_NSID, ns_id);
+
+       ret = send_receive(sock, nlh, seq, buf);
+       if (ret < 0) {
+               close(sock);
+               if (netnspath)
+                       close(fd);
+               return NS_UNKNOWN;
+       }
+       nlh = (struct nlmsghdr *)buf;
+       len = ret;
+       ret = 0;
+       do {
+               if (nlh->nlmsg_type >= NLMSG_MIN_TYPE) {
+                       return_nsid = extract_nsid(nlh, buf);
+                       if (return_nsid != NS_UNKNOWN)
+                               break;
+               } else if (nlh->nlmsg_type == NLMSG_ERROR) {
+                       struct nlmsgerr *err =
+                               (struct nlmsgerr *)((char *)nlh +
+                                                   NETLINK_ALIGN(sizeof(
+                                                           struct nlmsghdr)));
+                       if (err->error < 0)
+                               errno = -err->error;
+                       else
+                               errno = err->error;
+                       break;
+               }
+               len = len - NETLINK_ALIGN(nlh->nlmsg_len);
+               nlh = (struct nlmsghdr *)((char *)nlh +
+                                         NETLINK_ALIGN(nlh->nlmsg_len));
+       } while (len != 0 && ret == 0);
 
        if (netnspath)
                close(fd);