netlink: Better error handling.
authorBenjamin Cama <benoar@dolka.fr>
Tue, 16 Aug 2011 16:45:53 +0000 (18:45 +0200)
committerBenjamin Cama <benoar@dolka.fr>
Tue, 16 Aug 2011 16:45:53 +0000 (18:45 +0200)
Signed-off-by: Benjamin Cama <benoar@dolka.fr>
l2tpns.c

index 8517ccf..96c6b00 100644 (file)
--- a/l2tpns.c
+++ b/l2tpns.c
@@ -576,6 +576,16 @@ static ssize_t netlink_recv(void *buf, ssize_t len)
        return recvmsg(nlfd, &msg, 0);
 }
 
        return recvmsg(nlfd, &msg, 0);
 }
 
+// messages corresponding to different phases seq number
+static char *tun_nl_phase_msg[] = {
+       "initialized",
+       "getting tun interface index",
+       "setting tun interface parameters",
+       "setting tun IPv4 address",
+       "setting tun LL IPv6 address",
+       "setting tun global IPv6 address",
+};
+
 //
 // Set up TUN interface
 static void inittun(void)
 //
 // Set up TUN interface
 static void inittun(void)
@@ -798,9 +808,9 @@ static void inittun(void)
                // if we get an error for seqnum < min_initok_nlseqnum,
                // we must exit as initialization went wrong
                if (config->ipv6_prefix.s6_addr[0])
                // if we get an error for seqnum < min_initok_nlseqnum,
                // we must exit as initialization went wrong
                if (config->ipv6_prefix.s6_addr[0])
-                       min_initok_nlseqnum = 3 + 1; // idx + if + addr
-               else
                        min_initok_nlseqnum = 5 + 1; // idx + if + addr + 2*addr6
                        min_initok_nlseqnum = 5 + 1; // idx + if + addr + 2*addr6
+               else
+                       min_initok_nlseqnum = 3 + 1; // idx + if + addr
        }
 }
 
        }
 }
 
@@ -3886,13 +3896,18 @@ static void mainloop(void)
                                        if (nh->nlmsg_type == NLMSG_ERROR)
                                        {
                                                struct nlmsgerr *errmsg = NLMSG_DATA(nh);
                                        if (nh->nlmsg_type == NLMSG_ERROR)
                                        {
                                                struct nlmsgerr *errmsg = NLMSG_DATA(nh);
-                                               if (errmsg->error && errmsg->msg.nlmsg_seq < min_initok_nlseqnum)
+                                               if (errmsg->error)
                                                {
                                                {
-                                                       LOG(0, 0, 0, "Got a fatal netlink error: seq %d flags %d code %d\n", nh->nlmsg_seq, nh->nlmsg_flags, errmsg->error);
-                                                       exit(1);
+                                                       if (errmsg->msg.nlmsg_seq < min_initok_nlseqnum)
+                                                       {
+                                                               LOG(0, 0, 0, "Got a fatal netlink error (while %s): %s\n", tun_nl_phase_msg[nh->nlmsg_seq], strerror(-errmsg->error));
+                                                               exit(1);
+                                                       }
+                                                       else
+
+                                                               LOG(0, 0, 0, "Got a netlink error: %s\n", strerror(-errmsg->error));
                                                }
                                                }
-                                               else
-                                                       LOG(1, 0, 0, "Got a netlink error: seq %d flags %d code %d\n", nh->nlmsg_seq, nh->nlmsg_flags, errmsg->error);
+                                               // else it's a ack
                                        }
                                        else
                                                LOG(1, 0, 0, "Got a unknown netlink message: type %d seq %d flags %d\n", nh->nlmsg_type, nh->nlmsg_seq, nh->nlmsg_flags);
                                        }
                                        else
                                                LOG(1, 0, 0, "Got a unknown netlink message: type %d seq %d flags %d\n", nh->nlmsg_type, nh->nlmsg_seq, nh->nlmsg_flags);