DHCPIpUdpDecoder.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /**
  2. * @file DHCPIpUdpDecoder.c
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the author nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. #include <limits.h>
  30. #include <string.h>
  31. #include <misc/ipv4_proto.h>
  32. #include <misc/udp_proto.h>
  33. #include <misc/byteorder.h>
  34. #include <dhcpclient/DHCPIpUdpDecoder.h>
  35. #define DHCP_SERVER_PORT 67
  36. #define DHCP_CLIENT_PORT 68
  37. struct combined_header {
  38. struct ipv4_header ip;
  39. struct udp_header udp;
  40. } __attribute__((packed));
  41. static void input_handler_send (DHCPIpUdpDecoder *o, uint8_t *data, int data_len)
  42. {
  43. ASSERT(data_len >= 0)
  44. DebugObject_Access(&o->d_obj);
  45. struct ipv4_header iph;
  46. uint8_t *pl;
  47. int pl_len;
  48. if (!ipv4_check(data, data_len, &iph, &pl, &pl_len)) {
  49. goto fail;
  50. }
  51. if (ntoh8(iph.protocol) != IPV4_PROTOCOL_UDP) {
  52. goto fail;
  53. }
  54. if (pl_len < sizeof(struct udp_header)) {
  55. goto fail;
  56. }
  57. struct udp_header udph;
  58. memcpy(&udph, pl, sizeof(udph));
  59. if (ntoh16(udph.source_port) != DHCP_SERVER_PORT) {
  60. goto fail;
  61. }
  62. if (ntoh16(udph.dest_port) != DHCP_CLIENT_PORT) {
  63. goto fail;
  64. }
  65. int udph_length = ntoh16(udph.length);
  66. if (udph_length < sizeof(udph)) {
  67. goto fail;
  68. }
  69. if (udph_length > data_len - (pl - data)) {
  70. goto fail;
  71. }
  72. if (ntoh16(udph.checksum) != 0) {
  73. uint16_t checksum_in_packet = udph.checksum;
  74. udph.checksum = 0;
  75. uint16_t checksum_computed = udp_checksum(&udph, pl + sizeof(udph), udph_length - sizeof(udph), iph.source_address, iph.destination_address);
  76. if (checksum_in_packet != checksum_computed) {
  77. goto fail;
  78. }
  79. }
  80. // pass payload to output
  81. PacketPassInterface_Sender_Send(o->output, pl + sizeof(udph), udph_length - sizeof(udph));
  82. return;
  83. fail:
  84. PacketPassInterface_Done(&o->input);
  85. }
  86. static void output_handler_done (DHCPIpUdpDecoder *o)
  87. {
  88. DebugObject_Access(&o->d_obj);
  89. PacketPassInterface_Done(&o->input);
  90. }
  91. void DHCPIpUdpDecoder_Init (DHCPIpUdpDecoder *o, PacketPassInterface *output, BPendingGroup *pg)
  92. {
  93. ASSERT(PacketPassInterface_GetMTU(output) <= INT_MAX - sizeof(struct combined_header))
  94. // init arguments
  95. o->output = output;
  96. // init output
  97. PacketPassInterface_Sender_Init(o->output, (PacketPassInterface_handler_done)output_handler_done, o);
  98. // init input
  99. PacketPassInterface_Init(&o->input, sizeof(struct combined_header) + PacketPassInterface_GetMTU(o->output), (PacketPassInterface_handler_send)input_handler_send, o, pg);
  100. DebugObject_Init(&o->d_obj);
  101. }
  102. void DHCPIpUdpDecoder_Free (DHCPIpUdpDecoder *o)
  103. {
  104. DebugObject_Free(&o->d_obj);
  105. // free input
  106. PacketPassInterface_Free(&o->input);
  107. }
  108. PacketPassInterface * DHCPIpUdpDecoder_GetInput (DHCPIpUdpDecoder *o)
  109. {
  110. DebugObject_Access(&o->d_obj);
  111. return &o->input;
  112. }