From e687bb04784040fb26c231540e3c7b3ad9fd73d9 Mon Sep 17 00:00:00 2001 From: Kozlov Dmitry Date: Sun, 15 Jul 2012 02:21:07 +0400 Subject: ipoe: better support for ip unnumbered --- accel-pppd/libnetlink/iplink.c | 138 +++++++++++++++++++++++++++++++++++++++++ accel-pppd/libnetlink/iplink.h | 5 ++ 2 files changed, 143 insertions(+) (limited to 'accel-pppd/libnetlink') diff --git a/accel-pppd/libnetlink/iplink.c b/accel-pppd/libnetlink/iplink.c index df00285..779a8ae 100644 --- a/accel-pppd/libnetlink/iplink.c +++ b/accel-pppd/libnetlink/iplink.c @@ -11,6 +11,9 @@ #include #include #include +//#include +//#include +//#include #include "triton.h" #include "log.h" @@ -158,6 +161,141 @@ int __export iplink_get_stats(int ifindex, struct rtnl_link_stats *stats) return 0; } +int __export ipaddr_add(int ifindex, in_addr_t addr, int mask) +{ + struct ipaddr_req { + struct nlmsghdr n; + struct ifaddrmsg i; + char buf[1024]; + } req; + + if (!rth) + open_rth(); + + if (!rth) + return -1; + + memset(&req, 0, sizeof(req) - 1024); + + 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_INET; + req.i.ifa_index = ifindex; + req.i.ifa_prefixlen = mask; + + addattr32(&req.n, sizeof(req), IFA_LOCAL, addr); + + if (rtnl_talk(rth, &req.n, 0, 0, NULL, NULL, NULL, 0) < 0) + return -1; + + return 0; +} + +int __export ipaddr_del(int ifindex, in_addr_t addr) +{ + struct ipaddr_req { + struct nlmsghdr n; + struct ifaddrmsg i; + char buf[1024]; + } req; + + if (!rth) + open_rth(); + + if (!rth) + return -1; + + memset(&req, 0, sizeof(req) - 1024); + + req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrmsg)); + req.n.nlmsg_flags = NLM_F_REQUEST; + req.n.nlmsg_type = RTM_DELADDR; + req.i.ifa_family = AF_INET; + req.i.ifa_index = ifindex; + req.i.ifa_prefixlen = 32; + + addattr32(&req.n, sizeof(req), IFA_LOCAL, addr); + + if (rtnl_talk(rth, &req.n, 0, 0, NULL, NULL, NULL, 0) < 0) + return -1; + + return 0; +} + +int __export iproute_add(int ifindex, in_addr_t src, in_addr_t dst) +{ + struct ipaddr_req { + struct nlmsghdr n; + struct rtmsg i; + char buf[1024]; + } req; + + if (!rth) + open_rth(); + + if (!rth) + return -1; + + memset(&req, 0, sizeof(req) - 1024); + + 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; + req.i.rtm_family = AF_INET; + req.i.rtm_table = RT_TABLE_MAIN; + req.i.rtm_scope = RT_SCOPE_LINK; + req.i.rtm_protocol = RTPROT_BOOT; + req.i.rtm_type = RTN_UNICAST; + req.i.rtm_dst_len = 32; + + addattr32(&req.n, sizeof(req), RTA_PREFSRC, src); + addattr32(&req.n, sizeof(req), RTA_DST, dst); + addattr32(&req.n, sizeof(req), RTA_OIF, ifindex); + + if (rtnl_talk(rth, &req.n, 0, 0, NULL, NULL, NULL, 0) < 0) + return -1; + + return 0; +} + +int __export iproute_del(int ifindex, in_addr_t dst) +{ + struct ipaddr_req { + struct nlmsghdr n; + struct rtmsg i; + char buf[1024]; + } req; + + if (!rth) + open_rth(); + + if (!rth) + return -1; + + memset(&req, 0, sizeof(req) - 1024); + + 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; + req.i.rtm_family = AF_INET; + req.i.rtm_table = RT_TABLE_MAIN; + req.i.rtm_scope = RT_SCOPE_LINK; + req.i.rtm_protocol = RTPROT_BOOT; + req.i.rtm_type = RTN_UNICAST; + req.i.rtm_dst_len = 32; + + addattr32(&req.n, sizeof(req), RTA_DST, dst); + addattr32(&req.n, sizeof(req), RTA_OIF, ifindex); + + if (rtnl_talk(rth, &req.n, 0, 0, NULL, NULL, NULL, 0) < 0) + return -1; + + return 0; +} + + + static void init(void) { pthread_key_create(&rth_key, free_rth); diff --git a/accel-pppd/libnetlink/iplink.h b/accel-pppd/libnetlink/iplink.h index e67e8ed..a6af662 100644 --- a/accel-pppd/libnetlink/iplink.h +++ b/accel-pppd/libnetlink/iplink.h @@ -8,4 +8,9 @@ typedef int (*iplink_list_func)(int index, int flags, const char *name, void *ar int iplink_list(iplink_list_func func, void *arg); int iplink_get_stats(int ifindex, struct rtnl_link_stats *stats); +int ipaddr_add(int ifindex, in_addr_t addr, int mask); +int ipaddr_del(int ifindex, in_addr_t addr); + +int iproute_add(int ifindex, in_addr_t src, in_addr_t dst); +int iproute_del(int ifindex, in_addr_t dst); #endif -- cgit v1.2.3