我正在尝试使用 libnl-3-routing 添加路由(内核 2.6.32-57)。我使用文档:
但是路由章节是空的... ;)
问题是“输入数据或参数无效”的内核响应。我已经使用 netlink 进行了一些测试,如果我设置以下属性,我可以添加路由:
- 接口索引
- 目的地址
- 网关地址
面具
int ret = 0; // Create the route. struct rtnl_route* rulesRoute = rtnl_route_alloc(); rtnl_route_set_iif(rulesRoute, AF_INET); // IPV4 // Set parameters. rtnl_route_set_scope(rulesRoute, RT_SCOPE_UNIVERSE); rtnl_route_set_table(rulesRoute, RT_TABLE_MAIN); rtnl_route_set_protocol(rulesRoute, RTPROT_STATIC); uint8_t maskTest = 16; rtnl_route_set_scope(rulesRoute, maskTest); // Set the destination. char destinationAddr[] = "0.0.0.0"; nl_addr* dstAddr = nl_addr_build(AF_INET, destinationAddr, 8); ret = rtnl_route_set_dst(rulesRoute, dstAddr); if (ret != 0) std::cout << "Error in setting destination route: " << nl_geterror(ret) << std::endl; // Set the next hop. struct rtnl_nexthop* route_nexthop = rtnl_route_nh_alloc(); char gatewayAddr[] = "10.110.0.240"; nl_addr* gatewAddr = nl_addr_build(AF_INET, gatewayAddr, 12); rtnl_route_nh_set_gateway(route_nexthop, gatewAddr); rtnl_route_nh_set_ifindex(route_nexthop, 2); rtnl_route_add_nexthop(rulesRoute, route_nexthop); ret = rtnl_route_add(m_nlSocket, rulesRoute, 0); if (ret != 0) std::cout << "Kernel response:" << nl_geterror(ret) << std::endl;
我添加信息我的路由表:
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
10.110.0.0 0.0.0.0 255.255.255.0 U 1 0 0 eth3
169.254.0.0 0.0.0.0 255.255.0.0 U 1000 0 0 eth3
0.0.0.0 10.110.0.1 0.0.0.0 UG 0 0 0 eth3
感谢您帮助解决此问题