/** * pass_to_client() - forwards dhcp packets from server to client * p - packet to send */ static void pass_to_client(struct dhcp_packet *p, int packet_len, int *fds) { int type; struct xid_item *item; /* check xid */ item = xid_find(p->xid); if (!item) { return; } /* check packet type */ type = get_dhcp_packet_type(p); if (type != DHCPOFFER && type != DHCPACK && type != DHCPNAK) { return; } //TODO: also do it if (p->flags & htons(BROADCAST_FLAG)) is set! if (item->ip.sin_addr.s_addr == htonl(INADDR_ANY)) item->ip.sin_addr.s_addr = htonl(INADDR_BROADCAST); if (sendto_ip4(fds[item->client], p, packet_len, &item->ip) != 0) { return; /* send error occurred */ } /* remove xid entry */ xid_del(p->xid); }
/** * pass_back() - forwards dhcp packets from server to client * p - packet to send */ static void pass_back(struct dhcpMessage *p, int packet_len, int *fds) { int res, type; struct xid_item *item; /* check xid */ item = xid_find(p->xid); if (!item) { return; } /* check packet type */ type = get_dhcp_packet_type(p); if (type != DHCPOFFER && type != DHCPACK && type != DHCPNAK) { return; } if (item->ip.sin_addr.s_addr == htonl(INADDR_ANY)) item->ip.sin_addr.s_addr = htonl(INADDR_BROADCAST); if (item->client > MAX_INTERFACES) return; res = sendto(fds[item->client], p, packet_len, 0, (struct sockaddr*)(&item->ip), sizeof(item->ip)); if (res != packet_len) { bb_perror_msg("pass_back"); return; } /* remove xid entry */ xid_del(p->xid); }
/** * pass_on() - forwards dhcp packets from client to server * p - packet to send * client - number of the client */ static void pass_on(struct dhcpMessage *p, int packet_len, int client, int *fds, struct sockaddr_in *client_addr, struct sockaddr_in *server_addr) { int res, type; struct xid_item *item; /* check packet_type */ type = get_dhcp_packet_type(p); if (type != DHCPDISCOVER && type != DHCPREQUEST && type != DHCPDECLINE && type != DHCPRELEASE && type != DHCPINFORM ) { return; } /* create new xid entry */ item = xid_add(p->xid, client_addr, client); /* forward request to LAN (server) */ res = sendto(fds[0], p, packet_len, 0, (struct sockaddr*)server_addr, sizeof(struct sockaddr_in)); if (res != packet_len) { bb_perror_msg("pass_on"); return; } }
/** * pass_to_server() - forwards dhcp packets from client to server * p - packet to send * client - number of the client */ static void pass_to_server(struct dhcp_packet *p, int packet_len, int client, int *fds, struct sockaddr_in *client_addr, struct sockaddr_in *server_addr) { int type; /* check packet_type */ type = get_dhcp_packet_type(p); if (type != DHCPDISCOVER && type != DHCPREQUEST && type != DHCPDECLINE && type != DHCPRELEASE && type != DHCPINFORM ) { return; } /* create new xid entry */ xid_add(p->xid, client_addr, client); /* forward request to server */ /* note that we send from fds[0] which is bound to SERVER_PORT (67). * IOW: we send _from_ SERVER_PORT! Although this may look strange, * RFC 1542 not only allows, but prescribes this for BOOTP relays. */ sendto_ip4(fds[0], p, packet_len, server_addr); }