]> 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 4df1aafe5d839c04a70f64f2687d7cb9f72e380f..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)
 {
@@ -842,6 +843,8 @@ static void ripng_route_process(struct rte *rte, struct sockaddr_in6 *from,
                   unusable). */
                if (rte->metric != RIPNG_METRIC_INFINITY)
                        ripng_ecmp_add(&newinfo);
+               else
+                       route_unlock_node(rp);
        } else {
                /* If there is an existing route, compare the next hop address
                   to the address of the router from which the datagram came.
@@ -1377,7 +1380,7 @@ static void ripng_clear_changed_flag(void)
    enabled interface. */
 static int ripng_update(struct thread *t)
 {
-       struct listnode *node;
+       struct vrf *vrf = vrf_lookup_by_id(VRF_DEFAULT);
        struct interface *ifp;
        struct ripng_interface *ri;
 
@@ -1389,7 +1392,7 @@ static int ripng_update(struct thread *t)
                zlog_debug("RIPng update timer expired!");
 
        /* Supply routes to each interface. */
-       for (ALL_LIST_ELEMENTS_RO(vrf_iflist(VRF_DEFAULT), node, ifp)) {
+       FOR_ALL_INTERFACES (vrf, ifp) {
                ri = ifp->info;
 
                if (if_is_loopback(ifp) || !if_is_up(ifp))
@@ -1445,7 +1448,7 @@ static int ripng_triggered_interval(struct thread *t)
 /* Execute triggered update. */
 int ripng_triggered_update(struct thread *t)
 {
-       struct listnode *node;
+       struct vrf *vrf = vrf_lookup_by_id(VRF_DEFAULT);
        struct interface *ifp;
        struct ripng_interface *ri;
        int interval;
@@ -1465,7 +1468,7 @@ int ripng_triggered_update(struct thread *t)
 
        /* Split Horizon processing is done when generating triggered
           updates as well as normal updates (see section 2.6). */
-       for (ALL_LIST_ELEMENTS_RO(vrf_iflist(VRF_DEFAULT), node, ifp)) {
+       FOR_ALL_INTERFACES (vrf, ifp) {
                ri = ifp->info;
 
                if (if_is_loopback(ifp) || !if_is_up(ifp))
@@ -1502,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) {
@@ -1512,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);
@@ -2058,7 +2062,7 @@ DEFUN (show_ipv6_ripng_status,
        "Show RIPng routes\n"
        "IPv6 routing protocol process parameters and statistics\n")
 {
-       struct listnode *node;
+       struct vrf *vrf = vrf_lookup_by_id(VRF_DEFAULT);
        struct interface *ifp;
 
        if (!ripng)
@@ -2091,7 +2095,7 @@ DEFUN (show_ipv6_ripng_status,
 
        vty_out(vty, "    Interface        Send  Recv\n");
 
-       for (ALL_LIST_ELEMENTS_RO(vrf_iflist(VRF_DEFAULT), node, ifp)) {
+       FOR_ALL_INTERFACES (vrf, ifp) {
                struct ripng_interface *ri;
 
                ri = ifp->info;
@@ -2791,10 +2795,10 @@ void ripng_distribute_update_interface(struct interface *ifp)
 /* Update all interface's distribute list. */
 static void ripng_distribute_update_all(struct prefix_list *notused)
 {
+       struct vrf *vrf = vrf_lookup_by_id(VRF_DEFAULT);
        struct interface *ifp;
-       struct listnode *node;
 
-       for (ALL_LIST_ELEMENTS_RO(vrf_iflist(VRF_DEFAULT), node, ifp))
+       FOR_ALL_INTERFACES (vrf, ifp)
                ripng_distribute_update_interface(ifp);
 }
 
@@ -2968,10 +2972,10 @@ static void ripng_routemap_update_redistribute(void)
 
 static void ripng_routemap_update(const char *unused)
 {
+       struct vrf *vrf = vrf_lookup_by_id(VRF_DEFAULT);
        struct interface *ifp;
-       struct listnode *node;
 
-       for (ALL_LIST_ELEMENTS_RO(vrf_iflist(VRF_DEFAULT), node, ifp))
+       FOR_ALL_INTERFACES (vrf, ifp)
                ripng_if_rmap_update_interface(ifp);
 
        ripng_routemap_update_redistribute();