diff options
author | Dmitry Kozlov <xeb@mail.ru> | 2014-10-27 10:48:25 +0300 |
---|---|---|
committer | Dmitry Kozlov <xeb@mail.ru> | 2014-10-27 10:48:30 +0300 |
commit | 421dac7884ab3b7253ba942aa05983e47289a1a5 (patch) | |
tree | d8229bbd3211e0e0d1644e026dff8ad0db288367 | |
parent | 1819d99a0469b1c3fe7d1e9b6fbd593d86f5052f (diff) | |
download | accel-ppp-421dac7884ab3b7253ba942aa05983e47289a1a5.tar.gz accel-ppp-421dac7884ab3b7253ba942aa05983e47289a1a5.zip |
ipoe: implemented handling relayed DHCP clients
-rw-r--r-- | accel-pppd/ctrl/ipoe/dhcpv4.c | 38 | ||||
-rw-r--r-- | accel-pppd/ctrl/ipoe/dhcpv4.h | 1 | ||||
-rw-r--r-- | accel-pppd/ctrl/ipoe/ipoe.c | 67 | ||||
-rw-r--r-- | accel-pppd/libnetlink/iputils.c | 17 | ||||
-rw-r--r-- | accel-pppd/libnetlink/iputils.h | 4 |
5 files changed, 84 insertions, 43 deletions
diff --git a/accel-pppd/ctrl/ipoe/dhcpv4.c b/accel-pppd/ctrl/ipoe/dhcpv4.c index b34a2399..936b50e4 100644 --- a/accel-pppd/ctrl/ipoe/dhcpv4.c +++ b/accel-pppd/ctrl/ipoe/dhcpv4.c @@ -506,6 +506,8 @@ static int dhcpv4_read(struct triton_md_handler_t *h) continue; } + pack->src_addr = addr.sin_addr.s_addr; + if (serv->recv) serv->recv(serv, pack); @@ -640,7 +642,7 @@ static int dhcpv4_send_raw(struct dhcpv4_serv *serv, struct dhcpv4_packet *pack, return 0; } -static int dhcpv4_send_udp(struct dhcpv4_serv *serv, struct dhcpv4_packet *pack) +static int dhcpv4_send_udp(struct dhcpv4_serv *serv, struct dhcpv4_packet *pack, in_addr_t ip, int port) { struct sockaddr_in addr; int n; @@ -648,8 +650,8 @@ static int dhcpv4_send_udp(struct dhcpv4_serv *serv, struct dhcpv4_packet *pack) memset(&addr, 0, sizeof(addr)); addr.sin_family = AF_INET; - addr.sin_port = htons(DHCP_CLIENT_PORT); - addr.sin_addr.s_addr = pack->hdr->giaddr; + addr.sin_port = htons(port); + addr.sin_addr.s_addr = ip; n = sendto(serv->hnd.fd, pack->data, len, 0, (struct sockaddr *)&addr, sizeof(addr)); if (n != len) @@ -658,14 +660,6 @@ static int dhcpv4_send_udp(struct dhcpv4_serv *serv, struct dhcpv4_packet *pack) return 0; } -static int dhcpv4_send(struct dhcpv4_serv *serv, struct dhcpv4_packet *pack, in_addr_t saddr, in_addr_t daddr) -{ - if (pack->hdr->giaddr) - return dhcpv4_send_udp(serv, pack); - - return dhcpv4_send_raw(serv, pack, saddr, daddr); -} - int dhcpv4_packet_add_opt(struct dhcpv4_packet *pack, int type, const void *data, int len) { struct dhcpv4_option *opt = mempool_alloc(opt_pool); @@ -713,12 +707,13 @@ int dhcpv4_send_reply(int msg_type, struct dhcpv4_serv *serv, struct dhcpv4_pack memcpy(pack->hdr, req->hdr, sizeof(*req->hdr)); pack->hdr->op = DHCP_OP_REPLY; - //pack->hdr->ciaddr = 0; pack->hdr->yiaddr = yiaddr; - if (msg_type == DHCPOFFER) - pack->hdr->siaddr = siaddr; + if (msg_type == DHCPACK) + pack->hdr->ciaddr = req->hdr->ciaddr; else - pack->hdr->siaddr = 0; + pack->hdr->ciaddr = 0; + pack->hdr->siaddr = 0; + pack->hdr->giaddr = req->hdr->giaddr; if (dhcpv4_packet_add_opt(pack, 53, &msg_type, 1)) goto out_err; @@ -768,7 +763,12 @@ int dhcpv4_send_reply(int msg_type, struct dhcpv4_serv *serv, struct dhcpv4_pack dhcpv4_print_packet(pack, 0, log_ppp_info2); } - r = dhcpv4_send(serv, pack, siaddr, yiaddr); + if (req->hdr->giaddr) + r = dhcpv4_send_udp(serv, pack, req->hdr->giaddr, DHCP_SERV_PORT); + else if (pack->hdr->ciaddr) + r = dhcpv4_send_udp(serv, pack, req->hdr->ciaddr, DHCP_CLIENT_PORT); + else + r = dhcpv4_send_raw(serv, pack, siaddr, yiaddr); dhcpv4_packet_free(pack); @@ -796,6 +796,7 @@ int dhcpv4_send_nak(struct dhcpv4_serv *serv, struct dhcpv4_packet *req) pack->hdr->ciaddr = 0; pack->hdr->yiaddr = 0; pack->hdr->siaddr = 0; + pack->hdr->giaddr = req->hdr->giaddr; val = DHCPNAK; if (dhcpv4_packet_add_opt(pack, 53, &val, 1)) @@ -809,7 +810,10 @@ int dhcpv4_send_nak(struct dhcpv4_serv *serv, struct dhcpv4_packet *req) dhcpv4_print_packet(pack, 0, log_info2); } - r = dhcpv4_send(serv, pack, 0, 0xffffffff); + if (req->hdr->giaddr) + r = dhcpv4_send_udp(serv, pack, req->hdr->giaddr, DHCP_SERV_PORT); + else + r = dhcpv4_send_raw(serv, pack, 0, 0xffffffff); dhcpv4_packet_free(pack); diff --git a/accel-pppd/ctrl/ipoe/dhcpv4.h b/accel-pppd/ctrl/ipoe/dhcpv4.h index 7494b731..15db8ed5 100644 --- a/accel-pppd/ctrl/ipoe/dhcpv4.h +++ b/accel-pppd/ctrl/ipoe/dhcpv4.h @@ -65,6 +65,7 @@ struct dhcpv4_packet { uint32_t request_ip; uint32_t server_id; int msg_type; + in_addr_t src_addr; int volatile refs; uint8_t *ptr; uint8_t data[0]; diff --git a/accel-pppd/ctrl/ipoe/ipoe.c b/accel-pppd/ctrl/ipoe/ipoe.c index ac26b413..87aa897f 100644 --- a/accel-pppd/ctrl/ipoe/ipoe.c +++ b/accel-pppd/ctrl/ipoe/ipoe.c @@ -615,7 +615,7 @@ static void find_gw_addr(struct ipoe_session *ses) list_for_each_entry(a, &conf_gw_addr, entry) { if ((ntohl(ses->yiaddr) & (a->mask1)) == (ntohl(a->addr) & (a->mask1))) { - ses->siaddr = a->addr; + ses->router = a->addr; ses->mask = a->mask; return; } @@ -633,9 +633,6 @@ static void __ipoe_session_start(struct ipoe_session *ses) if (!ses->yiaddr && !ses->serv->opt_nat) ses->ses.ipv4 = ipdb_get_ipv4(&ses->ses); - if (!ses->mask) - ses->mask = conf_netmask; - if (ses->ses.ipv4) { if (!ses->mask) ses->mask = ses->ses.ipv4->mask; @@ -652,21 +649,50 @@ static void __ipoe_session_start(struct ipoe_session *ses) ses->ipv4.mask = ses->mask; ses->ipv4.owner = NULL; }*/ - + if (ses->dhcpv4_request) { if (!ses->yiaddr) { log_ppp_error("no free IPv4 address\n"); ap_session_terminate(&ses->ses, TERM_NAS_REQUEST, 0); return; } + + if (!ses->router) + find_gw_addr(ses); + + if (!ses->mask) + ses->mask = conf_netmask; + + if (!ses->mask) + ses->mask = 32; + + if (ses->dhcpv4_request->hdr->giaddr) { + /*uint32_t mask = ses->mask == 32 ? 0xffffffff : (((1 << ses->mask) - 1) << (32 - ses->mask)); + + ses->siaddr = iproute_get(ses->dhcpv4_request->hdr->giaddr); + if ((ntohl(ses->router) & mask) == (ntohl(ses->siaddr) & mask)) + ses->siaddr = ses->router; + else if (!ses->router) + ses->router = ses->dhcpv4_request->hdr->giaddr;*/ + if (ses->serv->opt_mode == MODE_L2) + ses->siaddr = ses->router; + else { + ses->siaddr = iproute_get(ses->dhcpv4_request->hdr->giaddr, NULL); + if (!ses->router) + ses->router = ses->dhcpv4_request->hdr->giaddr; + } + } + + if (!ses->router) { + log_ppp_error("can't determine router address\n"); + ap_session_terminate(&ses->ses, TERM_NAS_REQUEST, 0); + return; + } if (!ses->siaddr && ses->router != ses->yiaddr) ses->siaddr = ses->router; if (!ses->siaddr) - find_gw_addr(ses); - - if (!ses->siaddr) ses->siaddr = ses->serv->opt_src; if (!ses->siaddr && ses->serv->dhcpv4_relay) @@ -681,12 +707,6 @@ static void __ipoe_session_start(struct ipoe_session *ses) if (ses->ses.ipv4 && !ses->ses.ipv4->addr) ses->ses.ipv4->addr = ses->siaddr; - if (!ses->router) - ses->router = ses->siaddr; - - if (!ses->mask) - ses->mask = 32; - dhcpv4_send_reply(DHCPOFFER, ses->serv->dhcpv4, ses->dhcpv4_request, ses->yiaddr, ses->siaddr, ses->router, ses->mask, ses->lease_time, ses->dhcpv4_relay_reply); dhcpv4_packet_free(ses->dhcpv4_request); @@ -704,7 +724,7 @@ static void __ipoe_session_start(struct ipoe_session *ses) ses->siaddr = ses->serv->opt_src; if (!ses->siaddr) - ses->siaddr = iproute_get(ses->yiaddr); + ses->siaddr = iproute_get(ses->yiaddr, NULL); if (!ses->siaddr) { log_ppp_error("can't determine local address\n"); @@ -779,7 +799,7 @@ static void ipoe_ifcfg_add(struct ipoe_session *ses) ipoe_serv_add_addr(ses->serv, ses->siaddr, conf_ip_unnumbered ? 32 : ses->mask); if (conf_ip_unnumbered) { - if (iproute_add(serv->ifindex, ses->serv->opt_src ? ses->serv->opt_src : ses->router, ses->yiaddr, conf_proto)) + if (iproute_add(serv->ifindex, ses->serv->opt_src ? ses->serv->opt_src : ses->router, ses->yiaddr, 0, conf_proto)) log_ppp_warn("ipoe: failed to add route to interface '%s'\n", serv->ifname); } @@ -817,7 +837,16 @@ static void __ipoe_session_activate(struct ipoe_session *ses) addr = ses->ses.ipv4->peer_addr; else if (!conf_ip_unnumbered) ses->ctrl.dont_ifcfg = 1; - + + if (ses->dhcpv4_request && ses->serv->opt_mode == MODE_L3) { + in_addr_t gw; + iproute_get(ses->router, &gw); + if (gw) + iproute_add(0, ses->siaddr, ses->yiaddr, gw, conf_proto); + else + iproute_add(0, ses->siaddr, ses->router, gw, conf_proto); + } + if (ipoe_nl_modify(ses->ifindex, ses->yiaddr, addr, NULL, NULL)) { ap_session_terminate(&ses->ses, TERM_NAS_ERROR, 0); return; @@ -837,10 +866,10 @@ static void __ipoe_session_activate(struct ipoe_session *ses) ipoe_nl_add_exclude(ses->yiaddr, 32); - iproute_add(ses->serv->ifindex, ses->siaddr, ses->yiaddr, conf_proto); + iproute_add(ses->serv->ifindex, ses->siaddr, ses->yiaddr, 0, conf_proto); ses->ctrl.dont_ifcfg = 1; - } else if (ses->ctrl.dont_ifcfg) + } else if (ses->ctrl.dont_ifcfg && ses->serv->opt_mode == MODE_L2) ipaddr_add(ses->ifindex, ses->siaddr, ses->mask); if (ses->l4_redirect) diff --git a/accel-pppd/libnetlink/iputils.c b/accel-pppd/libnetlink/iputils.c index 7077098d..9ce4643f 100644 --- a/accel-pppd/libnetlink/iputils.c +++ b/accel-pppd/libnetlink/iputils.c @@ -305,7 +305,7 @@ int __export ipaddr_del(int ifindex, in_addr_t addr, int mask) return 0; } -int __export iproute_add(int ifindex, in_addr_t src, in_addr_t dst, int proto) +int __export iproute_add(int ifindex, in_addr_t src, in_addr_t dst, in_addr_t gw, int proto) { struct ipaddr_req { struct nlmsghdr n; @@ -326,15 +326,18 @@ int __export iproute_add(int ifindex, in_addr_t src, in_addr_t dst, int proto) 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_scope = ifindex ? RT_SCOPE_LINK : RT_SCOPE_UNIVERSE; req.i.rtm_protocol = proto; req.i.rtm_type = RTN_UNICAST; req.i.rtm_dst_len = 32; + if (ifindex) + addattr32(&req.n, sizeof(req), RTA_OIF, ifindex); if (src) addattr32(&req.n, sizeof(req), RTA_PREFSRC, src); + if (gw) + addattr32(&req.n, sizeof(req), RTA_GATEWAY, gw); 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; @@ -413,7 +416,7 @@ int __export ip6route_add(int ifindex, struct in6_addr *dst, int pref_len, int p } -in_addr_t __export iproute_get(in_addr_t dst) +in_addr_t __export iproute_get(in_addr_t dst, in_addr_t *gw) { struct ipaddr_req { struct nlmsghdr n; @@ -424,6 +427,7 @@ in_addr_t __export iproute_get(in_addr_t dst) struct rtattr *tb[RTA_MAX+1]; int len; in_addr_t res = 0; + *gw = 0; if (!rth) open_rth(); @@ -470,7 +474,10 @@ in_addr_t __export iproute_get(in_addr_t dst) if (tb[RTA_PREFSRC]) res = *(uint32_t *)RTA_DATA(tb[RTA_PREFSRC]); - + + if (gw && tb[RTA_GATEWAY]) + *gw = *(uint32_t *)RTA_DATA(tb[RTA_GATEWAY]); + out: return res; } diff --git a/accel-pppd/libnetlink/iputils.h b/accel-pppd/libnetlink/iputils.h index 0c88793d..b9a6488c 100644 --- a/accel-pppd/libnetlink/iputils.h +++ b/accel-pppd/libnetlink/iputils.h @@ -14,9 +14,9 @@ int iplink_vlan_del(int ifindex); int ipaddr_add(int ifindex, in_addr_t addr, int mask); int ipaddr_del(int ifindex, in_addr_t addr, int mask); -int iproute_add(int ifindex, in_addr_t src, in_addr_t dst, int proto); +int iproute_add(int ifindex, in_addr_t src, in_addr_t dst, in_addr_t gw, int proto); int iproute_del(int ifindex, in_addr_t dst, int proto); -in_addr_t iproute_get(in_addr_t dst); +in_addr_t iproute_get(in_addr_t dst, in_addr_t *gw); int ip6route_add(int ifindex, struct in6_addr *dst, int prefix_len, int proto); |