DHCPIpUdpDecoder.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /**
  2. * @file DHCPIpUdpDecoder.c
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * This file is part of BadVPN.
  8. *
  9. * BadVPN is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2
  11. * as published by the Free Software Foundation.
  12. *
  13. * BadVPN is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. */
  22. #include <limits.h>
  23. #include <misc/ipv4_proto.h>
  24. #include <misc/udp_proto.h>
  25. #include <misc/byteorder.h>
  26. #include <dhcpclient/DHCPIpUdpDecoder.h>
  27. #define DHCP_SERVER_PORT 67
  28. #define DHCP_CLIENT_PORT 68
  29. struct combined_header {
  30. struct ipv4_header ip;
  31. struct udp_header udp;
  32. } __attribute__((packed));
  33. static void input_handler_send (DHCPIpUdpDecoder *o, uint8_t *data, int data_len)
  34. {
  35. ASSERT(data_len >= 0)
  36. DebugObject_Access(&o->d_obj);
  37. struct ipv4_header *iph;
  38. uint8_t *pl;
  39. int pl_len;
  40. if (!ipv4_check(data, data_len, &iph, &pl, &pl_len)) {
  41. DEBUG("ipv4_check failed");
  42. return;
  43. }
  44. if (ntoh8(iph->protocol) != IPV4_PROTOCOL_UDP) {
  45. DEBUG("not UDP");
  46. return;
  47. }
  48. if (pl_len < sizeof(struct udp_header)) {
  49. DEBUG("no UDP header");
  50. return;
  51. }
  52. struct udp_header *udph = (void *)pl;
  53. if (ntoh16(udph->source_port) != DHCP_SERVER_PORT) {
  54. DEBUG("wrong source port");
  55. return;
  56. }
  57. if (ntoh16(udph->dest_port) != DHCP_CLIENT_PORT) {
  58. DEBUG("wrong dest port");
  59. return;
  60. }
  61. int udph_length = ntoh16(udph->length);
  62. if (udph_length < sizeof(*udph)) {
  63. DEBUG("udp length too small");
  64. return;
  65. }
  66. if (udph_length > data_len - (pl - data)) {
  67. DEBUG("udp length too big");
  68. return;
  69. }
  70. // pass payload to output
  71. PacketPassInterface_Sender_Send(o->output, (uint8_t *)(udph + 1), udph_length - sizeof(*udph));
  72. }
  73. static void output_handler_done (DHCPIpUdpDecoder *o)
  74. {
  75. DebugObject_Access(&o->d_obj);
  76. PacketPassInterface_Done(&o->input);
  77. }
  78. void DHCPIpUdpDecoder_Init (DHCPIpUdpDecoder *o, PacketPassInterface *output, BPendingGroup *pg)
  79. {
  80. ASSERT(PacketPassInterface_GetMTU(output) <= INT_MAX - sizeof(struct combined_header))
  81. // init arguments
  82. o->output = output;
  83. // init output
  84. PacketPassInterface_Sender_Init(o->output, (PacketPassInterface_handler_done)output_handler_done, o);
  85. // init input
  86. PacketPassInterface_Init(&o->input, sizeof(struct combined_header) + PacketPassInterface_GetMTU(o->output), (PacketPassInterface_handler_send)input_handler_send, o, pg);
  87. DebugObject_Init(&o->d_obj);
  88. }
  89. void DHCPIpUdpDecoder_Free (DHCPIpUdpDecoder *o)
  90. {
  91. DebugObject_Free(&o->d_obj);
  92. // free input
  93. PacketPassInterface_Free(&o->input);
  94. }
  95. PacketPassInterface * DHCPIpUdpDecoder_GetInput (DHCPIpUdpDecoder *o)
  96. {
  97. DebugObject_Access(&o->d_obj);
  98. return &o->input;
  99. }