]> git.proxmox.com Git - mirror_frr.git/blobdiff - lib/sockopt.c
lib: enforce vrf_name_to_id by returning default_vrf when name is null
[mirror_frr.git] / lib / sockopt.c
index d100991a3319fc6919c0fdcc65aeca821da5d852..ea04f2a43ee93ac2c180021f63cfeb9eeae3ad8a 100644 (file)
@@ -27,6 +27,7 @@
 #include "log.h"
 #include "sockopt.h"
 #include "sockunion.h"
+#include "lib_errors.h"
 
 void setsockopt_so_recvbuf(int sock, int size)
 {
@@ -37,8 +38,9 @@ void setsockopt_so_recvbuf(int sock, int size)
                size /= 2;
 
        if (size != orig_req)
-               zlog_warn("%s: fd %d: SO_RCVBUF set to %d (requested %d)",
-                         __func__, sock, size, orig_req);
+               flog_err(EC_LIB_SOCKET,
+                        "%s: fd %d: SO_RCVBUF set to %d (requested %d)",
+                        __func__, sock, size, orig_req);
 }
 
 void setsockopt_so_sendbuf(const int sock, int size)
@@ -50,19 +52,21 @@ void setsockopt_so_sendbuf(const int sock, int size)
                size /= 2;
 
        if (size != orig_req)
-               zlog_warn("%s: fd %d: SO_SNDBUF set to %d (requested %d)",
-                         __func__, sock, size, orig_req);
+               flog_err(EC_LIB_SOCKET,
+                        "%s: fd %d: SO_SNDBUF set to %d (requested %d)",
+                        __func__, sock, size, orig_req);
 }
 
 int getsockopt_so_sendbuf(const int sock)
 {
-       u_int32_t optval;
+       uint32_t optval;
        socklen_t optlen = sizeof(optval);
        int ret = getsockopt(sock, SOL_SOCKET, SO_SNDBUF, (char *)&optval,
                             &optlen);
        if (ret < 0) {
-               zlog_err("fd %d: can't getsockopt SO_SNDBUF: %d (%s)", sock,
-                        errno, safe_strerror(errno));
+               flog_err_sys(EC_LIB_SYSTEM_CALL,
+                            "fd %d: can't getsockopt SO_SNDBUF: %d (%s)", sock,
+                            errno, safe_strerror(errno));
                return ret;
        }
        return optval;
@@ -73,9 +77,9 @@ static void *getsockopt_cmsg_data(struct msghdr *msgh, int level, int type)
        struct cmsghdr *cmsg;
        void *ptr = NULL;
 
-       for (cmsg = ZCMSG_FIRSTHDR(msgh); cmsg != NULL;
+       for (cmsg = CMSG_FIRSTHDR(msgh); cmsg != NULL;
             cmsg = CMSG_NXTHDR(msgh, cmsg))
-               if (cmsg->cmsg_level == level && cmsg->cmsg_type)
+               if (cmsg->cmsg_level == level && cmsg->cmsg_type == type)
                        return (ptr = CMSG_DATA(cmsg));
 
        return NULL;
@@ -90,13 +94,14 @@ int setsockopt_ipv6_pktinfo(int sock, int val)
        ret = setsockopt(sock, IPPROTO_IPV6, IPV6_RECVPKTINFO, &val,
                         sizeof(val));
        if (ret < 0)
-               zlog_warn("can't setsockopt IPV6_RECVPKTINFO : %s",
-                         safe_strerror(errno));
+               flog_err(EC_LIB_SOCKET,
+                        "can't setsockopt IPV6_RECVPKTINFO : %s",
+                        safe_strerror(errno));
 #else  /*RFC2292*/
        ret = setsockopt(sock, IPPROTO_IPV6, IPV6_PKTINFO, &val, sizeof(val));
        if (ret < 0)
-               zlog_warn("can't setsockopt IPV6_PKTINFO : %s",
-                         safe_strerror(errno));
+               flog_err(EC_LIB_SOCKET, "can't setsockopt IPV6_PKTINFO : %s",
+                        safe_strerror(errno));
 #endif /* INIA_IPV6 */
        return ret;
 }
@@ -112,7 +117,7 @@ int setsockopt_ipv6_checksum(int sock, int val)
        ret = setsockopt(sock, IPPROTO_IPV6, IPV6_CHECKSUM, &val, sizeof(val));
 #endif /* GNU_LINUX */
        if (ret < 0)
-               zlog_warn("can't setsockopt IPV6_CHECKSUM");
+               flog_err(EC_LIB_SOCKET, "can't setsockopt IPV6_CHECKSUM");
        return ret;
 }
 
@@ -124,7 +129,7 @@ int setsockopt_ipv6_multicast_hops(int sock, int val)
        ret = setsockopt(sock, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &val,
                         sizeof(val));
        if (ret < 0)
-               zlog_warn("can't setsockopt IPV6_MULTICAST_HOPS");
+               flog_err(EC_LIB_SOCKET, "can't setsockopt IPV6_MULTICAST_HOPS");
        return ret;
 }
 
@@ -136,7 +141,7 @@ int setsockopt_ipv6_unicast_hops(int sock, int val)
        ret = setsockopt(sock, IPPROTO_IPV6, IPV6_UNICAST_HOPS, &val,
                         sizeof(val));
        if (ret < 0)
-               zlog_warn("can't setsockopt IPV6_UNICAST_HOPS");
+               flog_err(EC_LIB_SOCKET, "can't setsockopt IPV6_UNICAST_HOPS");
        return ret;
 }
 
@@ -148,11 +153,11 @@ int setsockopt_ipv6_hoplimit(int sock, int val)
        ret = setsockopt(sock, IPPROTO_IPV6, IPV6_RECVHOPLIMIT, &val,
                         sizeof(val));
        if (ret < 0)
-               zlog_warn("can't setsockopt IPV6_RECVHOPLIMIT");
+               flog_err(EC_LIB_SOCKET, "can't setsockopt IPV6_RECVHOPLIMIT");
 #else /*RFC2292*/
        ret = setsockopt(sock, IPPROTO_IPV6, IPV6_HOPLIMIT, &val, sizeof(val));
        if (ret < 0)
-               zlog_warn("can't setsockopt IPV6_HOPLIMIT");
+               flog_err(EC_LIB_SOCKET, "can't setsockopt IPV6_HOPLIMIT");
 #endif
        return ret;
 }
@@ -165,7 +170,7 @@ int setsockopt_ipv6_multicast_loop(int sock, int val)
        ret = setsockopt(sock, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, &val,
                         sizeof(val));
        if (ret < 0)
-               zlog_warn("can't setsockopt IPV6_MULTICAST_LOOP");
+               flog_err(EC_LIB_SOCKET, "can't setsockopt IPV6_MULTICAST_LOOP");
        return ret;
 }
 
@@ -186,8 +191,9 @@ int setsockopt_ipv6_tclass(int sock, int tclass)
        ret = setsockopt(sock, IPPROTO_IPV6, IPV6_TCLASS, &tclass,
                         sizeof(tclass));
        if (ret < 0)
-               zlog_warn("Can't set IPV6_TCLASS option for fd %d to %#x: %s",
-                         sock, tclass, safe_strerror(errno));
+               flog_err(EC_LIB_SOCKET,
+                        "Can't set IPV6_TCLASS option for fd %d to %#x: %s",
+                        sock, tclass, safe_strerror(errno));
 #endif
        return ret;
 }
@@ -382,14 +388,14 @@ int setsockopt_ipv4_multicast_if(int sock, struct in_addr if_addr,
 #endif
 }
 
-int setsockopt_ipv4_multicast_loop(int sock, u_char val)
+int setsockopt_ipv4_multicast_loop(int sock, uint8_t val)
 {
        int ret;
 
        ret = setsockopt(sock, IPPROTO_IP, IP_MULTICAST_LOOP, (void *)&val,
                         sizeof(val));
        if (ret < 0)
-               zlog_warn("can't setsockopt IP_MULTICAST_LOOP");
+               flog_err(EC_LIB_SOCKET, "can't setsockopt IP_MULTICAST_LOOP");
 
        return ret;
 }
@@ -401,13 +407,15 @@ static int setsockopt_ipv4_ifindex(int sock, ifindex_t val)
 #if defined(IP_PKTINFO)
        if ((ret = setsockopt(sock, IPPROTO_IP, IP_PKTINFO, &val, sizeof(val)))
            < 0)
-               zlog_warn("Can't set IP_PKTINFO option for fd %d to %d: %s",
-                         sock, val, safe_strerror(errno));
+               flog_err(EC_LIB_SOCKET,
+                        "Can't set IP_PKTINFO option for fd %d to %d: %s",
+                        sock, val, safe_strerror(errno));
 #elif defined(IP_RECVIF)
        if ((ret = setsockopt(sock, IPPROTO_IP, IP_RECVIF, &val, sizeof(val)))
            < 0)
-               zlog_warn("Can't set IP_RECVIF option for fd %d to %d: %s",
-                         sock, val, safe_strerror(errno));
+               flog_err(EC_LIB_SOCKET,
+                        "Can't set IP_RECVIF option for fd %d to %d: %s", sock,
+                        val, safe_strerror(errno));
 #else
 #warning "Neither IP_PKTINFO nor IP_RECVIF is available."
 #warning "Will not be able to receive link info."
@@ -425,8 +433,9 @@ int setsockopt_ipv4_tos(int sock, int tos)
 
        ret = setsockopt(sock, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
        if (ret < 0)
-               zlog_warn("Can't set IP_TOS option for fd %d to %#x: %s", sock,
-                         tos, safe_strerror(errno));
+               flog_err(EC_LIB_SOCKET,
+                        "Can't set IP_TOS option for fd %d to %#x: %s", sock,
+                        tos, safe_strerror(errno));
        return ret;
 }
 
@@ -443,7 +452,8 @@ int setsockopt_ifindex(int af, int sock, ifindex_t val)
                ret = setsockopt_ipv6_pktinfo(sock, val);
                break;
        default:
-               zlog_warn("setsockopt_ifindex: unknown address family %d", af);
+               flog_err(EC_LIB_DEVELOPMENT,
+                        "setsockopt_ifindex: unknown address family %d", af);
        }
        return ret;
 }
@@ -457,8 +467,7 @@ int setsockopt_ifindex(int af, int sock, ifindex_t val)
  */
 static ifindex_t getsockopt_ipv4_ifindex(struct msghdr *msgh)
 {
-       /* XXX: initialize to zero?  (Always overwritten, so just cosmetic.) */
-       ifindex_t ifindex = -1;
+       ifindex_t ifindex;
 
 #if defined(IP_PKTINFO)
        /* Linux pktinfo based ifindex retrieval */
@@ -466,7 +475,11 @@ static ifindex_t getsockopt_ipv4_ifindex(struct msghdr *msgh)
 
        pktinfo = (struct in_pktinfo *)getsockopt_cmsg_data(msgh, IPPROTO_IP,
                                                            IP_PKTINFO);
-       /* XXX Can pktinfo be NULL?  Clean up post 0.98. */
+
+       /* getsockopt_ifindex() will forward this, being 0 "not found" */
+       if (pktinfo == NULL)
+               return 0;
+
        ifindex = pktinfo->ipi_ifindex;
 
 #elif defined(IP_RECVIF)
@@ -529,7 +542,8 @@ ifindex_t getsockopt_ifindex(int af, struct msghdr *msgh)
                return (getsockopt_ipv6_ifindex(msgh));
                break;
        default:
-               zlog_warn("getsockopt_ifindex: unknown address family %d", af);
+               flog_err(EC_LIB_DEVELOPMENT,
+                        "getsockopt_ifindex: unknown address family %d", af);
                return 0;
        }
 }
@@ -575,31 +589,7 @@ int sockopt_tcp_rtt(int sock)
 
 int sockopt_tcp_signature(int sock, union sockunion *su, const char *password)
 {
-#if defined(HAVE_TCP_MD5_LINUX24) && defined(GNU_LINUX)
-/* Support for the old Linux 2.4 TCP-MD5 patch, taken from Hasso Tepper's
- * version of the Quagga patch (based on work by Rick Payne, and Bruce
- * Simpson)
- */
-#define TCP_MD5_AUTH 13
-#define TCP_MD5_AUTH_ADD 1
-#define TCP_MD5_AUTH_DEL 2
-       struct tcp_rfc2385_cmd {
-               u_int8_t command;  /* Command - Add/Delete */
-               u_int32_t address; /* IPV4 address associated */
-               u_int8_t keylen;   /* MD5 Key len (do NOT assume 0 terminated
-                                     ascii) */
-               void *key;       /* MD5 Key */
-       } cmd;
-       struct in_addr *addr = &su->sin.sin_addr;
-
-       cmd.command = (password != NULL ? TCP_MD5_AUTH_ADD : TCP_MD5_AUTH_DEL);
-       cmd.address = addr->s_addr;
-       cmd.keylen = (password != NULL ? strlen(password) : 0);
-       cmd.key = password;
-
-       return setsockopt(sock, IPPROTO_TCP, TCP_MD5_AUTH, &cmd, sizeof cmd);
-
-#elif HAVE_DECL_TCP_MD5SIG
+#if HAVE_DECL_TCP_MD5SIG
        int ret;
 #ifndef GNU_LINUX
        /*
@@ -667,8 +657,10 @@ int sockopt_tcp_signature(int sock, union sockunion *su, const char *password)
                if (ENOENT == errno)
                        ret = 0;
                else
-                       zlog_err("sockopt_tcp_signature: setsockopt(%d): %s",
-                                sock, safe_strerror(errno));
+                       flog_err_sys(
+                               EC_LIB_SYSTEM_CALL,
+                               "sockopt_tcp_signature: setsockopt(%d): %s",
+                               sock, safe_strerror(errno));
        }
        return ret;
 #else  /* HAVE_TCP_MD5SIG */