DHCPIpUdpDecoder.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. // pass payload to output
  64. PacketPassInterface_Sender_Send(o->output, (uint8_t *)(udph + 1), udph_length - sizeof(*udph));
  65. return;
  66. fail:
  67. PacketPassInterface_Done(&o->input);
  68. }
  69. static void output_handler_done (DHCPIpUdpDecoder *o)
  70. {
  71. DebugObject_Access(&o->d_obj);
  72. PacketPassInterface_Done(&o->input);
  73. }
  74. void DHCPIpUdpDecoder_Init (DHCPIpUdpDecoder *o, PacketPassInterface *output, BPendingGroup *pg)
  75. {
  76. ASSERT(PacketPassInterface_GetMTU(output) <= INT_MAX - sizeof(struct combined_header))
  77. // init arguments
  78. o->output = output;
  79. // init output
  80. PacketPassInterface_Sender_Init(o->output, (PacketPassInterface_handler_done)output_handler_done, o);
  81. // init input
  82. PacketPassInterface_Init(&o->input, sizeof(struct combined_header) + PacketPassInterface_GetMTU(o->output), (PacketPassInterface_handler_send)input_handler_send, o, pg);
  83. DebugObject_Init(&o->d_obj);
  84. }
  85. void DHCPIpUdpDecoder_Free (DHCPIpUdpDecoder *o)
  86. {
  87. DebugObject_Free(&o->d_obj);
  88. // free input
  89. PacketPassInterface_Free(&o->input);
  90. }
  91. PacketPassInterface * DHCPIpUdpDecoder_GetInput (DHCPIpUdpDecoder *o)
  92. {
  93. DebugObject_Access(&o->d_obj);
  94. return &o->input;
  95. }