From 31fd6f10555f8e0905764870f7f9bfc26a84df6e Mon Sep 17 00:00:00 2001 From: Benjamin Cama Date: Fri, 19 Aug 2011 19:23:27 +0200 Subject: [PATCH 1/4] Remove the netlink code to get the tun if index. It only worked for kernel >= 2.6.33. Use if_nametoindex() instead, which made me include net/if.h in place of linux/if.h. Also, fix the RTM_SETLINK use which should really be RTM_NEWLINK with correct parameters. Signed-off-by: Benjamin Cama --- l2tpns.c | 52 ++++++++++++---------------------------------------- 1 file changed, 12 insertions(+), 40 deletions(-) diff --git a/l2tpns.c b/l2tpns.c index c34dc63..5545aed 100644 --- a/l2tpns.c +++ b/l2tpns.c @@ -30,7 +30,7 @@ char const *cvs_id_l2tpns = "$Id: l2tpns.c,v 1.176 2011/01/20 12:48:40 bodea Exp #include #include #include -#include +#include #include #include #include @@ -641,7 +641,6 @@ static char *tun_nl_phase_msg[] = { // Set up TUN interface static void inittun(void) { - struct ifinfomsg ifinfo; struct ifreq ifr; memset(&ifr, 0, sizeof(ifr)); @@ -665,41 +664,13 @@ static void inittun(void) assert(strlen(ifr.ifr_name) < sizeof(config->tundevice) - 1); strncpy(config->tundevice, ifr.ifr_name, sizeof(config->tundevice)); + tunidx = if_nametoindex(config->tundevice); + if (tunidx == 0) { - // get the interface index - struct { - struct nlmsghdr nh; - struct ifinfomsg ifinfo; - } req; - char buf[4096]; - ssize_t len; - struct nlmsghdr *resp_nh; - - req.nh.nlmsg_type = RTM_GETLINK; - req.nh.nlmsg_flags = NLM_F_REQUEST; - req.nh.nlmsg_len = NLMSG_LENGTH(sizeof(req.ifinfo)); - - req.ifinfo.ifi_family = AF_UNSPEC; // as the man says - - netlink_addattr(&req.nh, IFLA_IFNAME, config->tundevice, strlen(config->tundevice)+1); - - if(netlink_send(&req.nh) < 0 || (len = netlink_recv(buf, sizeof(buf))) < 0) - { - LOG(0, 0, 0, "Error getting tun ifindex: %s\n", strerror(errno)); - exit(1); - } - - resp_nh = (struct nlmsghdr *)buf; - if (!NLMSG_OK (resp_nh, len)) - { - LOG(0, 0, 0, "Malformed answer getting tun ifindex %ld\n", len); - exit(1); - } - - memcpy(&ifinfo, NLMSG_DATA(resp_nh), sizeof(ifinfo)); - // got index - tunidx = ifinfo.ifi_index; + LOG(0, 0, 0, "Can't get tun interface index\n"); + exit(1); } + { struct { // interface setting @@ -715,11 +686,12 @@ static void inittun(void) memset(&req, 0, sizeof(req)); - req.nh.nlmsg_type = RTM_SETLINK; + req.nh.nlmsg_type = RTM_NEWLINK; req.nh.nlmsg_flags = NLM_F_REQUEST | NLM_F_MULTI; req.nh.nlmsg_len = NLMSG_LENGTH(sizeof(req.ifmsg.ifinfo)); - req.ifmsg.ifinfo = ifinfo; + req.ifmsg.ifinfo.ifi_family = AF_UNSPEC; + req.ifmsg.ifinfo.ifi_index = tunidx; req.ifmsg.ifinfo.ifi_flags |= IFF_UP; // set interface up req.ifmsg.ifinfo.ifi_change = IFF_UP; // only change this flag @@ -742,7 +714,7 @@ static void inittun(void) req.ifmsg.ifaddr.ifa_family = AF_INET; req.ifmsg.ifaddr.ifa_prefixlen = 32; req.ifmsg.ifaddr.ifa_scope = RT_SCOPE_UNIVERSE; - req.ifmsg.ifaddr.ifa_index = ifinfo.ifi_index; + req.ifmsg.ifaddr.ifa_index = tunidx; if (config->bind_address) ip = config->bind_address; @@ -766,7 +738,7 @@ static void inittun(void) req.ifmsg.ifaddr.ifa_family = AF_INET6; req.ifmsg.ifaddr.ifa_prefixlen = 64; req.ifmsg.ifaddr.ifa_scope = RT_SCOPE_LINK; - req.ifmsg.ifaddr.ifa_index = ifinfo.ifi_index; + req.ifmsg.ifaddr.ifa_index = tunidx; // Link local address is FE80::1 memset(&ip6, 0, sizeof(ip6)); @@ -787,7 +759,7 @@ static void inittun(void) req.ifmsg.ifaddr.ifa_family = AF_INET6; req.ifmsg.ifaddr.ifa_prefixlen = 64; req.ifmsg.ifaddr.ifa_scope = RT_SCOPE_UNIVERSE; - req.ifmsg.ifaddr.ifa_index = ifinfo.ifi_index; + req.ifmsg.ifaddr.ifa_index = tunidx; // Global address is prefix::1 ip6 = config->ipv6_prefix; From c23b9c14abdbb0fa3c5d94fa137ac38e3b13a71a Mon Sep 17 00:00:00 2001 From: Benjamin Cama Date: Sat, 20 Aug 2011 18:23:58 +0200 Subject: [PATCH 2/4] Correctly form the delete route/route6 request. Signed-off-by: Benjamin Cama --- l2tpns.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/l2tpns.c b/l2tpns.c index 5545aed..bab453f 100644 --- a/l2tpns.c +++ b/l2tpns.c @@ -444,7 +444,11 @@ static void routeset(sessionidt s, in_addr_t ip, int prefixlen, in_addr_t gw, in req.nh.nlmsg_flags = NLM_F_REQUEST | NLM_F_CREATE | NLM_F_REPLACE; } else + { req.nh.nlmsg_type = RTM_DELROUTE; + req.nh.nlmsg_flags = NLM_F_REQUEST; + } + req.nh.nlmsg_len = NLMSG_LENGTH(sizeof(req.rt)); req.rt.rtm_family = AF_INET; @@ -519,7 +523,11 @@ void route6set(sessionidt s, struct in6_addr ip, int prefixlen, int add) req.nh.nlmsg_flags = NLM_F_REQUEST | NLM_F_CREATE | NLM_F_REPLACE; } else + { req.nh.nlmsg_type = RTM_DELROUTE; + req.nh.nlmsg_flags = NLM_F_REQUEST; + } + req.nh.nlmsg_len = NLMSG_LENGTH(sizeof(req.rt)); req.rt.rtm_family = AF_INET6; From 5382473b0e690398304eab989a3e2a5f1158bb68 Mon Sep 17 00:00:00 2001 From: Benjamin Cama Date: Sun, 21 Aug 2011 13:06:57 +0200 Subject: [PATCH 3/4] Better error handling on CLI initialization. Signed-off-by: Benjamin Cama --- cli.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/cli.c b/cli.c index 9433106..3a037fa 100644 --- a/cli.c +++ b/cli.c @@ -287,10 +287,18 @@ void init_cli(char *hostname) addr.sin_port = htons(23); if (bind(clifd, (void *) &addr, sizeof(addr)) < 0) { - LOG(0, 0, 0, "Error listening on cli port 23: %s\n", strerror(errno)); + LOG(0, 0, 0, "Error binding cli on port 23: %s\n", strerror(errno)); + close(clifd); + clifd = -1; + return; + } + if (listen(clifd, 10) < 0) + { + LOG(0, 0, 0, "Error listening on cli port 23: %s\n", strerror(errno)); + close(clifd); + clifd = -1; return; } - listen(clifd, 10); } void cli_do(int sockfd) From 6b54e4985e16ba807172532e1341ccd922f81260 Mon Sep 17 00:00:00 2001 From: Benjamin Cama Date: Thu, 15 Sep 2011 16:48:32 +0200 Subject: [PATCH 4/4] Correctly set mp_epdis in host order. Signed-off-by: Benjamin Cama --- l2tpns.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/l2tpns.c b/l2tpns.c index ae7f3f4..90ac930 100644 --- a/l2tpns.c +++ b/l2tpns.c @@ -2959,7 +2959,7 @@ void processudp(uint8_t *buf, int len, struct sockaddr_in *addr) // Set multilink options before sending initial LCP packet sess_local[s].mp_mrru = 1614; - sess_local[s].mp_epdis = config->bind_address ? config->bind_address : my_address; + sess_local[s].mp_epdis = ntohl(config->bind_address ? config->bind_address : my_address); sendlcp(s, t); change_state(s, lcp, RequestSent);