diff options
author | Dmitry Kozlov <xeb@mail.ru> | 2016-07-11 09:47:04 +0300 |
---|---|---|
committer | Dmitry Kozlov <xeb@mail.ru> | 2016-07-11 09:47:04 +0300 |
commit | e0cca4344f70b9c9e23e3c6a7e17c8e93c765184 (patch) | |
tree | 1068654b025b2e24c983d99776696ad889a0c138 /accel-pppd/libnetlink/iputils.c | |
parent | 6fb04941ca41312909208938bfac4f753d3c277c (diff) | |
download | accel-ppp-e0cca4344f70b9c9e23e3c6a7e17c8e93c765184.tar.gz accel-ppp-e0cca4344f70b9c9e23e3c6a7e17c8e93c765184.zip |
implemented support for network namespace
This is done using radius attribute NAS-Port-Id. The new format of this attribute is NAS-Port-Id=[ns/][name].
Namespaces must be created malually by "ip netns add ..." command
Diffstat (limited to 'accel-pppd/libnetlink/iputils.c')
-rw-r--r-- | accel-pppd/libnetlink/iputils.c | 220 |
1 files changed, 100 insertions, 120 deletions
diff --git a/accel-pppd/libnetlink/iputils.c b/accel-pppd/libnetlink/iputils.c index 30a06d1..7a29d08 100644 --- a/accel-pppd/libnetlink/iputils.c +++ b/accel-pppd/libnetlink/iputils.c @@ -20,6 +20,7 @@ #include "libnetlink.h" #include "iputils.h" +#include "ap_net.h" #ifdef ACCEL_DP #define _malloc(x) malloc(x) @@ -37,37 +38,6 @@ struct arg void *arg; }; -static pthread_key_t rth_key; -static __thread struct rtnl_handle *rth; - -static void open_rth(void) -{ - rth = _malloc(sizeof(*rth)); - - if (!rth) - return; - - memset(rth, 0, sizeof(*rth)); - - if (rtnl_open(rth, 0)) { - log_ppp_error("radius: cannot open rtnetlink\n"); - _free(rth); - rth = NULL; - return; - } - - pthread_setspecific(rth_key, rth); -} - -static void free_rth(void *arg) -{ - struct rtnl_handle *rth = arg; - - rtnl_close(rth); - - _free(rth); -} - static int store_nlmsg(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg) { struct ifinfomsg *ifi = NLMSG_DATA(n); @@ -143,9 +113,8 @@ int __export iplink_get_stats(int ifindex, struct rtnl_link_stats *stats) struct ifinfomsg *ifi; int len; struct rtattr *tb[IFLA_MAX + 1]; - - if (!rth) - open_rth(); + struct rtnl_handle *rth = net->rtnl_get(); + int r = -1; if (!rth) return -1; @@ -159,10 +128,10 @@ int __export iplink_get_stats(int ifindex, struct rtnl_link_stats *stats) req.i.ifi_index = ifindex; if (rtnl_talk(rth, &req.n, 0, 0, &req.n, NULL, NULL, 0) < 0) - return -1; + goto out; if (req.n.nlmsg_type != RTM_NEWLINK) - return -1; + goto out; ifi = NLMSG_DATA(&req.n); @@ -170,15 +139,18 @@ int __export iplink_get_stats(int ifindex, struct rtnl_link_stats *stats) len -= NLMSG_LENGTH(sizeof(*ifi)); if (len < 0) - return -1; + goto out; parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), len); - if (tb[IFLA_STATS]) + if (tb[IFLA_STATS]) { memcpy(stats, RTA_DATA(tb[IFLA_STATS]), sizeof(*stats)); - else - return -1; + r = 0; + } - return 0; +out: + net->rtnl_put(rth); + + return r; } int __export iplink_vlan_add(const char *ifname, int ifindex, int vid) @@ -189,9 +161,8 @@ int __export iplink_vlan_add(const char *ifname, int ifindex, int vid) char buf[4096]; } req; struct rtattr *linkinfo, *data; - - if (!rth) - open_rth(); + struct rtnl_handle *rth = net->rtnl_get(); + int r = 0; if (!rth) return -1; @@ -218,9 +189,11 @@ int __export iplink_vlan_add(const char *ifname, int ifindex, int vid) linkinfo->rta_len = (void *)NLMSG_TAIL(&req.n) - (void *)linkinfo; if (rtnl_talk(rth, &req.n, 0, 0, NULL, NULL, NULL, 0) < 0) - return -1; + r = -1; - return 0; + net->rtnl_put(rth); + + return r; } int __export iplink_vlan_del(int ifindex) @@ -231,9 +204,8 @@ int __export iplink_vlan_del(int ifindex) char buf[4096]; } req; struct rtattr *linkinfo; - - if (!rth) - open_rth(); + struct rtnl_handle *rth = net->rtnl_get(); + int r = 0; if (!rth) return -1; @@ -257,9 +229,11 @@ int __export iplink_vlan_del(int ifindex) linkinfo->rta_len = (void *)NLMSG_TAIL(&req.n) - (void *)linkinfo; if (rtnl_talk(rth, &req.n, 0, 0, NULL, NULL, NULL, 0) < 0) - return -1; + r = -1; - return 0; + net->rtnl_put(rth); + + return r; } int __export iplink_vlan_get_vid(int ifindex, int *iflink) @@ -272,9 +246,8 @@ int __export iplink_vlan_get_vid(int ifindex, int *iflink) struct ifinfomsg *ifi; int len; struct rtattr *tb[IFLA_MAX + 1]; - - if (!rth) - open_rth(); + struct rtnl_handle *rth = net->rtnl_get(); + int r = 0; if (!rth) return -1; @@ -288,10 +261,10 @@ int __export iplink_vlan_get_vid(int ifindex, int *iflink) req.i.ifi_index = ifindex; if (rtnl_talk(rth, &req.n, 0, 0, &req.n, NULL, NULL, 0) < 0) - return -1; + goto out; if (req.n.nlmsg_type != RTM_NEWLINK) - return -1; + goto out; ifi = NLMSG_DATA(&req.n); @@ -299,12 +272,12 @@ int __export iplink_vlan_get_vid(int ifindex, int *iflink) len -= NLMSG_LENGTH(sizeof(*ifi)); if (len < 0) - return -1; + goto out; parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), len); if (!tb[IFLA_LINKINFO]) - return 0; + goto out; if (iflink && tb[IFLA_LINK]) *iflink = *(int *)RTA_DATA(tb[IFLA_LINK]); @@ -312,10 +285,15 @@ int __export iplink_vlan_get_vid(int ifindex, int *iflink) parse_rtattr_nested(tb, IFLA_MAX, tb[IFLA_LINKINFO]); if (strcmp(RTA_DATA(tb[IFLA_INFO_KIND]), "vlan")) - return 0; + goto out; parse_rtattr_nested(tb, IFLA_MAX, tb[IFLA_INFO_DATA]); - return *(uint16_t *)RTA_DATA(tb[IFLA_VLAN_ID]); + r = *(uint16_t *)RTA_DATA(tb[IFLA_VLAN_ID]); + +out: + net->rtnl_put(rth); + + return r; } @@ -326,9 +304,8 @@ int __export ipaddr_add(int ifindex, in_addr_t addr, int mask) struct ifaddrmsg i; char buf[4096]; } req; - - if (!rth) - open_rth(); + struct rtnl_handle *rth = net->rtnl_get(); + int r = 0; if (!rth) return -1; @@ -345,9 +322,11 @@ int __export ipaddr_add(int ifindex, in_addr_t addr, int mask) addattr32(&req.n, sizeof(req), IFA_LOCAL, addr); if (rtnl_talk(rth, &req.n, 0, 0, NULL, NULL, NULL, 0) < 0) - return -1; + r = -1; - return 0; + net->rtnl_put(rth); + + return r; } int __export ipaddr_del(int ifindex, in_addr_t addr, int mask) @@ -357,9 +336,8 @@ int __export ipaddr_del(int ifindex, in_addr_t addr, int mask) struct ifaddrmsg i; char buf[4096]; } req; - - if (!rth) - open_rth(); + struct rtnl_handle *rth = net->rtnl_get(); + int r = 0; if (!rth) return -1; @@ -376,9 +354,11 @@ int __export ipaddr_del(int ifindex, in_addr_t addr, int mask) addattr32(&req.n, sizeof(req), IFA_LOCAL, addr); if (rtnl_talk(rth, &req.n, 0, 0, NULL, NULL, NULL, 0) < 0) - return -1; + r = -1; - return 0; + net->rtnl_put(rth); + + return r; } int __export iproute_add(int ifindex, in_addr_t src, in_addr_t dst, in_addr_t gw, int proto, int mask) @@ -388,9 +368,8 @@ int __export iproute_add(int ifindex, in_addr_t src, in_addr_t dst, in_addr_t gw struct rtmsg i; char buf[4096]; } req; - - if (!rth) - open_rth(); + struct rtnl_handle *rth = net->rtnl_get(); + int r = 0; if (!rth) return -1; @@ -416,9 +395,11 @@ int __export iproute_add(int ifindex, in_addr_t src, in_addr_t dst, in_addr_t gw addattr32(&req.n, sizeof(req), RTA_DST, dst); if (rtnl_talk(rth, &req.n, 0, 0, NULL, NULL, NULL, 0) < 0) - return -1; + r = -1; - return 0; + net->rtnl_put(rth); + + return r; } int __export iproute_del(int ifindex, in_addr_t dst, int proto, int mask) @@ -428,9 +409,8 @@ int __export iproute_del(int ifindex, in_addr_t dst, int proto, int mask) struct rtmsg i; char buf[4096]; } req; - - if (!rth) - open_rth(); + struct rtnl_handle *rth = net->rtnl_get(); + int r = 0; if (!rth) return -1; @@ -453,9 +433,11 @@ int __export iproute_del(int ifindex, in_addr_t dst, int proto, int mask) addattr32(&req.n, sizeof(req), RTA_OIF, ifindex); if (rtnl_talk(rth, &req.n, 0, 0, NULL, NULL, NULL, 0) < 0) - return -1; + r = -1; - return 0; + net->rtnl_put(rth); + + return r; } int __export ip6route_add(int ifindex, struct in6_addr *dst, int pref_len, int proto) @@ -465,9 +447,8 @@ int __export ip6route_add(int ifindex, struct in6_addr *dst, int pref_len, int p struct rtmsg i; char buf[4096]; } req; - - if (!rth) - open_rth(); + struct rtnl_handle *rth = net->rtnl_get(); + int r = 0; if (!rth) return -1; @@ -488,9 +469,11 @@ int __export ip6route_add(int ifindex, struct in6_addr *dst, int pref_len, int p addattr32(&req.n, sizeof(req), RTA_OIF, ifindex); if (rtnl_talk(rth, &req.n, 0, 0, NULL, NULL, NULL, 0) < 0) - return -1; + r = -1; - return 0; + net->rtnl_put(rth); + + return r; } int __export ip6route_del(int ifindex, struct in6_addr *dst, int pref_len) @@ -500,9 +483,8 @@ int __export ip6route_del(int ifindex, struct in6_addr *dst, int pref_len) struct rtmsg i; char buf[4096]; } req; - - if (!rth) - open_rth(); + struct rtnl_handle *rth = net->rtnl_get(); + int r = 0; if (!rth) return -1; @@ -521,9 +503,11 @@ int __export ip6route_del(int ifindex, struct in6_addr *dst, int pref_len) addattr_l(&req.n, sizeof(req), RTA_DST, dst, sizeof(*dst)); if (rtnl_talk(rth, &req.n, 0, 0, NULL, NULL, NULL, 0) < 0) - return -1; + r = -1; - return 0; + net->rtnl_put(rth); + + return r; } int __export ip6addr_add(int ifindex, struct in6_addr *addr, int prefix_len) @@ -533,9 +517,8 @@ int __export ip6addr_add(int ifindex, struct in6_addr *addr, int prefix_len) struct ifaddrmsg i; char buf[4096]; } req; - - if (!rth) - open_rth(); + struct rtnl_handle *rth = net->rtnl_get(); + int r = 0; if (!rth) return -1; @@ -553,9 +536,11 @@ int __export ip6addr_add(int ifindex, struct in6_addr *addr, int prefix_len) addattr_l(&req.n, sizeof(req), IFA_ADDRESS, addr, 16); if (rtnl_talk(rth, &req.n, 0, 0, NULL, NULL, NULL, 0) < 0) - return -1; + r = -1; - return 0; + net->rtnl_put(rth); + + return r; } int __export ip6addr_del(int ifindex, struct in6_addr *addr, int prefix_len) @@ -565,9 +550,8 @@ int __export ip6addr_del(int ifindex, struct in6_addr *addr, int prefix_len) struct ifaddrmsg i; char buf[4096]; } req; - - if (!rth) - open_rth(); + struct rtnl_handle *rth = net->rtnl_get(); + int r = 0; if (!rth) return -1; @@ -584,9 +568,11 @@ int __export ip6addr_del(int ifindex, struct in6_addr *addr, int prefix_len) addattr_l(&req.n, sizeof(req), IFA_ADDRESS, addr, 16); if (rtnl_talk(rth, &req.n, 0, 0, NULL, NULL, NULL, 0) < 0) - return -1; + r = -1; - return 0; + net->rtnl_put(rth); + + return r; } in_addr_t __export iproute_get(in_addr_t dst, in_addr_t *gw) @@ -600,14 +586,12 @@ in_addr_t __export iproute_get(in_addr_t dst, in_addr_t *gw) struct rtattr *tb[RTA_MAX+1]; int len; in_addr_t res = 0; + struct rtnl_handle *rth = net->rtnl_get(); if (gw) *gw = 0; if (!rth) - open_rth(); - - if (!rth) return -1; memset(&req, 0, sizeof(req) - 4096); @@ -654,6 +638,8 @@ in_addr_t __export iproute_get(in_addr_t dst, in_addr_t *gw) *gw = *(uint32_t *)RTA_DATA(tb[RTA_GATEWAY]); out: + net->rtnl_put(rth); + return res; } @@ -664,9 +650,8 @@ int __export iprule_add(uint32_t addr, int table) struct rtmsg i; char buf[4096]; } req; - - if (!rth) - open_rth(); + struct rtnl_handle *rth = net->rtnl_get(); + int r = 0; if (!rth) return -1; @@ -688,9 +673,11 @@ int __export iprule_add(uint32_t addr, int table) addattr32(&req.n, sizeof(req), FRA_TABLE, table); if (rtnl_talk(rth, &req.n, 0, 0, NULL, NULL, NULL, 0) < 0) - return -1; + r = -1; - return 0; + net->rtnl_put(rth); + + return r; } int __export iprule_del(uint32_t addr, int table) @@ -700,9 +687,8 @@ int __export iprule_del(uint32_t addr, int table) struct rtmsg i; char buf[4096]; } req; - - if (!rth) - open_rth(); + struct rtnl_handle *rth = net->rtnl_get(); + int r = 0; if (!rth) return -1; @@ -724,15 +710,9 @@ int __export iprule_del(uint32_t addr, int table) addattr32(&req.n, sizeof(req), FRA_TABLE, table); if (rtnl_talk(rth, &req.n, 0, 0, NULL, NULL, NULL, 0) < 0) - return -1; - - return 0; -} + r = -1; + net->rtnl_put(rth); -static void init(void) -{ - pthread_key_create(&rth_key, free_rth); + return r; } - -DEFINE_INIT(100, init); |