DHCPIpUdpDecoder.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. goto fail;
  42. }
  43. if (ntoh8(iph->protocol) != IPV4_PROTOCOL_UDP) {
  44. goto fail;
  45. }
  46. if (pl_len < sizeof(struct udp_header)) {
  47. goto fail;
  48. }
  49. struct udp_header *udph = (void *)pl;
  50. if (ntoh16(udph->source_port) != DHCP_SERVER_PORT) {
  51. goto fail;
  52. }
  53. if (ntoh16(udph->dest_port) != DHCP_CLIENT_PORT) {
  54. goto fail;
  55. }
  56. int udph_length = ntoh16(udph->length);
  57. if (udph_length < sizeof(*udph)) {
  58. goto fail;
  59. }
  60. if (udph_length > data_len - (pl - data)) {
  61. goto fail;
  62. }
  63. if (ntoh16(udph->checksum) != 0) {
  64. uint16_t checksum_in_packet = udph->checksum;
  65. udph->checksum = 0;
  66. uint16_t checksum_computed = udp_checksum((uint8_t *)udph, udph_length, iph->source_address, iph->destination_address);
  67. udph->checksum = checksum_in_packet;
  68. if (checksum_in_packet != checksum_computed) {
  69. goto fail;
  70. }
  71. }
  72. // pass payload to output
  73. PacketPassInterface_Sender_Send(o->output, (uint8_t *)(udph + 1), udph_length - sizeof(*udph));
  74. return;
  75. fail:
  76. PacketPassInterface_Done(&o->input);
  77. }
  78. static void output_handler_done (DHCPIpUdpDecoder *o)
  79. {
  80. DebugObject_Access(&o->d_obj);
  81. PacketPassInterface_Done(&o->input);
  82. }
  83. void DHCPIpUdpDecoder_Init (DHCPIpUdpDecoder *o, PacketPassInterface *output, BPendingGroup *pg)
  84. {
  85. ASSERT(PacketPassInterface_GetMTU(output) <= INT_MAX - sizeof(struct combined_header))
  86. // init arguments
  87. o->output = output;
  88. // init output
  89. PacketPassInterface_Sender_Init(o->output, (PacketPassInterface_handler_done)output_handler_done, o);
  90. // init input
  91. PacketPassInterface_Init(&o->input, sizeof(struct combined_header) + PacketPassInterface_GetMTU(o->output), (PacketPassInterface_handler_send)input_handler_send, o, pg);
  92. DebugObject_Init(&o->d_obj);
  93. }
  94. void DHCPIpUdpDecoder_Free (DHCPIpUdpDecoder *o)
  95. {
  96. DebugObject_Free(&o->d_obj);
  97. // free input
  98. PacketPassInterface_Free(&o->input);
  99. }
  100. PacketPassInterface * DHCPIpUdpDecoder_GetInput (DHCPIpUdpDecoder *o)
  101. {
  102. DebugObject_Access(&o->d_obj);
  103. return &o->input;
  104. }