diff options
author | Dmitry Kozlov <xeb@mail.ru> | 2014-12-01 16:24:11 +0300 |
---|---|---|
committer | Dmitry Kozlov <xeb@mail.ru> | 2014-12-01 16:39:35 +0300 |
commit | 3cc7fd0165e096be25761710b66b44fda9d09190 (patch) | |
tree | b6eef91b863c78858be060902a515b26a7b2bf4b /accel-pppd/libnetlink/iputils.c | |
parent | ebc0ec740280efd2ea7f22abbb84eda53ab06632 (diff) | |
download | accel-ppp-3cc7fd0165e096be25761710b66b44fda9d09190.tar.gz accel-ppp-3cc7fd0165e096be25761710b66b44fda9d09190.zip |
ipv6: if assigned prefix is <= 64 add it as address else add it as route
Diffstat (limited to 'accel-pppd/libnetlink/iputils.c')
-rw-r--r-- | accel-pppd/libnetlink/iputils.c | 116 |
1 files changed, 74 insertions, 42 deletions
diff --git a/accel-pppd/libnetlink/iputils.c b/accel-pppd/libnetlink/iputils.c index fb85973..480f028 100644 --- a/accel-pppd/libnetlink/iputils.c +++ b/accel-pppd/libnetlink/iputils.c @@ -39,7 +39,7 @@ static void open_rth(void) if (!rth) return; - + memset(rth, 0, sizeof(*rth)); if (rtnl_open(rth, 0)) { @@ -78,7 +78,7 @@ static int store_nlmsg(const struct sockaddr_nl *who, struct nlmsghdr *n, void * if (tb[IFLA_IFNAME] == NULL) return 0; - + //printf("%i %s\n", ifi->ifi_index, RTA_DATA(tb[IFLA_IFNAME])); return a->func(ifi->ifi_index, ifi->ifi_flags, RTA_DATA(tb[IFLA_IFNAME]), a->arg); @@ -127,12 +127,12 @@ int __export iplink_get_stats(int ifindex, struct rtnl_link_stats *stats) if (!rth) open_rth(); - + if (!rth) return -1; memset(&req, 0, sizeof(req) - 4096); - + req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg)); req.n.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK; req.n.nlmsg_type = RTM_GETLINK; @@ -158,10 +158,10 @@ int __export iplink_get_stats(int ifindex, struct rtnl_link_stats *stats) memcpy(stats, RTA_DATA(tb[IFLA_STATS]), sizeof(*stats)); else return -1; - + return 0; } - + int __export iplink_vlan_add(const char *ifname, int ifindex, int vid) { struct iplink_req { @@ -173,20 +173,20 @@ int __export iplink_vlan_add(const char *ifname, int ifindex, int vid) if (!rth) open_rth(); - + if (!rth) return -1; memset(&req, 0, sizeof(req) - 4096); - + req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg)); req.n.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK | NLM_F_CREATE | NLM_F_EXCL; req.n.nlmsg_type = RTM_NEWLINK; req.i.ifi_family = AF_UNSPEC; - + addattr_l(&req.n, 4096, IFLA_LINK, &ifindex, 4); addattr_l(&req.n, 4096, IFLA_IFNAME, ifname, strlen(ifname)); - + linkinfo = NLMSG_TAIL(&req.n); addattr_l(&req.n, 4096, IFLA_LINKINFO, NULL, 0); addattr_l(&req.n, 4096, IFLA_INFO_KIND, "vlan", 4); @@ -195,12 +195,12 @@ int __export iplink_vlan_add(const char *ifname, int ifindex, int vid) addattr_l(&req.n, 4096, IFLA_INFO_DATA, NULL, 0); addattr_l(&req.n, 4096, IFLA_VLAN_ID, &vid, 2); data->rta_len = (void *)NLMSG_TAIL(&req.n) - (void *)data; - + 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; - + return 0; } @@ -215,18 +215,18 @@ int __export iplink_vlan_del(int ifindex) if (!rth) open_rth(); - + if (!rth) return -1; memset(&req, 0, sizeof(req) - 4096); - + req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg)); req.n.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK; req.n.nlmsg_type = RTM_DELLINK; req.i.ifi_family = AF_UNSPEC; req.i.ifi_index = ifindex; - + linkinfo = NLMSG_TAIL(&req.n); addattr_l(&req.n, 4096, IFLA_LINKINFO, NULL, 0); addattr_l(&req.n, 4096, IFLA_INFO_KIND, "vlan", 4); @@ -234,12 +234,12 @@ int __export iplink_vlan_del(int ifindex) /*data = NLMSG_TAIL(&req.n); addattr_l(&req.n, 4096, IFLA_VLAN_ID, &vid, 2); data->rta_len = (void *)NLMSG_TAIL(&req.n) - (void *)data;*/ - + 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; - + return 0; } @@ -253,12 +253,12 @@ int __export ipaddr_add(int ifindex, in_addr_t addr, int mask) if (!rth) open_rth(); - + if (!rth) return -1; memset(&req, 0, sizeof(req) - 4096); - + req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg)); req.n.nlmsg_flags = NLM_F_REQUEST | NLM_F_CREATE; req.n.nlmsg_type = RTM_NEWADDR; @@ -270,7 +270,7 @@ int __export ipaddr_add(int ifindex, in_addr_t addr, int mask) if (rtnl_talk(rth, &req.n, 0, 0, NULL, NULL, NULL, 0) < 0) return -1; - + return 0; } @@ -284,12 +284,12 @@ int __export ipaddr_del(int ifindex, in_addr_t addr, int mask) if (!rth) open_rth(); - + if (!rth) return -1; memset(&req, 0, sizeof(req) - 4096); - + req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg)); req.n.nlmsg_flags = NLM_F_REQUEST; req.n.nlmsg_type = RTM_DELADDR; @@ -301,7 +301,7 @@ int __export ipaddr_del(int ifindex, in_addr_t addr, int mask) if (rtnl_talk(rth, &req.n, 0, 0, NULL, NULL, NULL, 0) < 0) return -1; - + return 0; } @@ -315,12 +315,12 @@ int __export iproute_add(int ifindex, in_addr_t src, in_addr_t dst, in_addr_t gw if (!rth) open_rth(); - + if (!rth) return -1; memset(&req, 0, sizeof(req) - 4096); - + req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg)); req.n.nlmsg_flags = NLM_F_REQUEST | NLM_F_CREATE; req.n.nlmsg_type = RTM_NEWROUTE; @@ -341,7 +341,7 @@ int __export iproute_add(int ifindex, in_addr_t src, in_addr_t dst, in_addr_t gw if (rtnl_talk(rth, &req.n, 0, 0, NULL, NULL, NULL, 0) < 0) return -1; - + return 0; } @@ -355,12 +355,12 @@ int __export iproute_del(int ifindex, in_addr_t dst, int proto) if (!rth) open_rth(); - + if (!rth) return -1; memset(&req, 0, sizeof(req) - 4096); - + req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg)); req.n.nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK; req.n.nlmsg_type = RTM_DELROUTE; @@ -376,7 +376,7 @@ int __export iproute_del(int ifindex, in_addr_t dst, int proto) if (rtnl_talk(rth, &req.n, 0, 0, NULL, NULL, NULL, 0) < 0) return -1; - + return 0; } @@ -390,12 +390,12 @@ int __export ip6route_add(int ifindex, struct in6_addr *dst, int pref_len, int p if (!rth) open_rth(); - + if (!rth) return -1; memset(&req, 0, sizeof(req) - 4096); - + req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg)); req.n.nlmsg_flags = NLM_F_REQUEST | NLM_F_CREATE; req.n.nlmsg_type = RTM_NEWROUTE; @@ -411,11 +411,43 @@ int __export ip6route_add(int ifindex, struct in6_addr *dst, int pref_len, int p if (rtnl_talk(rth, &req.n, 0, 0, NULL, NULL, NULL, 0) < 0) return -1; - + return 0; } +int __export ip6addr_add(int ifindex, struct in6_addr *addr, int prefix_len) +{ + struct ipaddr_req { + struct nlmsghdr n; + struct ifaddrmsg i; + char buf[4096]; + } req; + + if (!rth) + open_rth(); + + if (!rth) + return -1; + + memset(&req, 0, sizeof(req) - 4096); + + req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg)); + req.n.nlmsg_flags = NLM_F_REQUEST | NLM_F_CREATE; + req.n.nlmsg_type = RTM_NEWADDR; + req.i.ifa_family = AF_INET6; + req.i.ifa_index = ifindex; + req.i.ifa_prefixlen = prefix_len; + req.i.ifa_flags = IFA_F_NODAD; + + 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; + + return 0; +} + in_addr_t __export iproute_get(in_addr_t dst, in_addr_t *gw) { struct ipaddr_req { @@ -433,12 +465,12 @@ in_addr_t __export iproute_get(in_addr_t dst, in_addr_t *gw) if (!rth) open_rth(); - + if (!rth) return -1; memset(&req, 0, sizeof(req) - 4096); - + req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg)); req.n.nlmsg_flags = NLM_F_REQUEST; req.n.nlmsg_type = RTM_GETROUTE; @@ -460,7 +492,7 @@ in_addr_t __export iproute_get(in_addr_t dst, in_addr_t *gw) r = NLMSG_DATA(&req.n); len = req.n.nlmsg_len; - + if (req.n.nlmsg_type != RTM_NEWROUTE) { log_error("failed to detect route to server (wrong netlink message type)"); goto out; @@ -473,7 +505,7 @@ in_addr_t __export iproute_get(in_addr_t dst, in_addr_t *gw) } parse_rtattr(tb, RTA_MAX, RTM_RTA(r), len); - + if (tb[RTA_PREFSRC]) res = *(uint32_t *)RTA_DATA(tb[RTA_PREFSRC]); @@ -494,12 +526,12 @@ int __export iprule_add(uint32_t addr, int table) if (!rth) open_rth(); - + if (!rth) return -1; memset(&req, 0, sizeof(req) - 4096); - + req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg)); req.n.nlmsg_flags = NLM_F_REQUEST; req.n.nlmsg_type = RTM_NEWRULE; @@ -516,7 +548,7 @@ int __export iprule_add(uint32_t addr, int table) if (rtnl_talk(rth, &req.n, 0, 0, NULL, NULL, NULL, 0) < 0) return -1; - + return 0; } @@ -530,12 +562,12 @@ int __export iprule_del(uint32_t addr, int table) if (!rth) open_rth(); - + if (!rth) return -1; memset(&req, 0, sizeof(req) - 4096); - + req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg)); req.n.nlmsg_flags = NLM_F_REQUEST; req.n.nlmsg_type = RTM_DELRULE; @@ -552,7 +584,7 @@ int __export iprule_del(uint32_t addr, int table) if (rtnl_talk(rth, &req.n, 0, 0, NULL, NULL, NULL, 0) < 0) return -1; - + return 0; } |