]> git.proxmox.com Git - mirror_frr.git/blobdiff - ripngd/ripngd.c
*: use frr_elevate_privs() (1/2: coccinelle)
[mirror_frr.git] / ripngd / ripngd.c
index df3af2a17f3649c88d0d1761e71147b755d9d1ce..925b3f13786712684c42be2ea92ea5d31e3eef9d 100644 (file)
@@ -35,6 +35,7 @@
 #include "routemap.h"
 #include "if_rmap.h"
 #include "privs.h"
+#include "lib_errors.h"
 
 #include "ripngd/ripngd.h"
 #include "ripngd/ripng_route.h"
@@ -50,8 +51,6 @@ enum { ripng_all_route,
        ripng_changed_route,
 };
 
-extern struct zebra_privs_t ripngd_privs;
-
 /* Prototypes. */
 void ripng_output_process(struct interface *, struct sockaddr_in6 *, int);
 
@@ -96,28 +95,28 @@ static int ripng_make_socket(void)
 
        sock = socket(AF_INET6, SOCK_DGRAM, 0);
        if (sock < 0) {
-               zlog_err("Can't make ripng socket");
+               flog_err_sys(LIB_ERR_SOCKET, "Can't make ripng socket");
                return sock;
        }
 
        setsockopt_so_recvbuf(sock, 8096);
        ret = setsockopt_ipv6_pktinfo(sock, 1);
        if (ret < 0)
-               return ret;
+               goto error;
 #ifdef IPTOS_PREC_INTERNETCONTROL
        ret = setsockopt_ipv6_tclass(sock, IPTOS_PREC_INTERNETCONTROL);
        if (ret < 0)
-               return ret;
+               goto error;
 #endif
        ret = setsockopt_ipv6_multicast_hops(sock, 255);
        if (ret < 0)
-               return ret;
+               goto error;
        ret = setsockopt_ipv6_multicast_loop(sock, 0);
        if (ret < 0)
-               return ret;
+               goto error;
        ret = setsockopt_ipv6_hoplimit(sock, 1);
        if (ret < 0)
-               return ret;
+               goto error;
 
        memset(&ripaddr, 0, sizeof(ripaddr));
        ripaddr.sin6_family = AF_INET6;
@@ -126,19 +125,19 @@ static int ripng_make_socket(void)
 #endif /* SIN6_LEN */
        ripaddr.sin6_port = htons(RIPNG_PORT_DEFAULT);
 
-       if (ripngd_privs.change(ZPRIVS_RAISE))
-               zlog_err("ripng_make_socket: could not raise privs");
+       frr_elevate_privs(&ripngd_privs) {
 
-       ret = bind(sock, (struct sockaddr *)&ripaddr, sizeof(ripaddr));
-       if (ret < 0) {
-               zlog_err("Can't bind ripng socket: %s.", safe_strerror(errno));
-               if (ripngd_privs.change(ZPRIVS_LOWER))
-                       zlog_err("ripng_make_socket: could not lower privs");
-               return ret;
+               ret = bind(sock, (struct sockaddr *)&ripaddr, sizeof(ripaddr));
+               if (ret < 0) {
+                       zlog_err("Can't bind ripng socket: %s.", safe_strerror(errno));
+                       goto error;
+               }
        }
-       if (ripngd_privs.change(ZPRIVS_LOWER))
-               zlog_err("ripng_make_socket: could not lower privs");
        return sock;
+
+error:
+       close(sock);
+       return ret;
 }
 
 /* Send RIPng packet. */
@@ -200,19 +199,21 @@ int ripng_send_packet(caddr_t buf, int bufsize, struct sockaddr_in6 *to,
 
        if (ret < 0) {
                if (to)
-                       zlog_err("RIPng send fail on %s to %s: %s", ifp->name,
-                                inet6_ntoa(to->sin6_addr),
-                                safe_strerror(errno));
+                       flog_err_sys(LIB_ERR_SOCKET,
+                                    "RIPng send fail on %s to %s: %s",
+                                    ifp->name, inet6_ntoa(to->sin6_addr),
+                                    safe_strerror(errno));
                else
-                       zlog_err("RIPng send fail on %s: %s", ifp->name,
-                                safe_strerror(errno));
+                       flog_err_sys(LIB_ERR_SOCKET,
+                                    "RIPng send fail on %s: %s", ifp->name,
+                                    safe_strerror(errno));
        }
 
        return ret;
 }
 
 /* Receive UDP RIPng packet from socket. */
-static int ripng_recv_packet(int sock, u_char *buf, int bufsize,
+static int ripng_recv_packet(int sock, uint8_t *buf, int bufsize,
                             struct sockaddr_in6 *from, ifindex_t *ifindex,
                             int *hoplimit)
 {
@@ -1504,7 +1505,7 @@ int ripng_triggered_update(struct thread *t)
 /* Write routing table entry to the stream and return next index of
    the routing table entry in the stream. */
 int ripng_write_rte(int num, struct stream *s, struct prefix_ipv6 *p,
-                   struct in6_addr *nexthop, u_int16_t tag, u_char metric)
+                   struct in6_addr *nexthop, uint16_t tag, uint8_t metric)
 {
        /* RIPng packet header. */
        if (num == 0) {
@@ -1514,10 +1515,11 @@ int ripng_write_rte(int num, struct stream *s, struct prefix_ipv6 *p,
        }
 
        /* Write routing table entry. */
-       if (!nexthop)
-               stream_write(s, (u_char *)&p->prefix, sizeof(struct in6_addr));
-       else
-               stream_write(s, (u_char *)nexthop, sizeof(struct in6_addr));
+       if (!nexthop) {
+               assert(p);
+               stream_write(s, (uint8_t *)&p->prefix, sizeof(struct in6_addr));
+       } else
+               stream_write(s, (uint8_t *)nexthop, sizeof(struct in6_addr));
        stream_putw(s, tag);
        if (p)
                stream_putc(s, p->prefixlen);