1
0

TAPWrapper.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /* vim: set expandtab ts=4 sw=4: */
  2. /*
  3. * You may redistribute this program and/or modify it under the terms of
  4. * the GNU General Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. #include "interface/tuntap/TAPWrapper.h"
  16. #include "interface/tuntap/TUNMessageType.h"
  17. #include "interface/InterfaceWrapper.h"
  18. #include "util/Bits.h"
  19. #include "util/Identity.h"
  20. #include "util/AddrTools.h"
  21. #include "wire/Message.h"
  22. #include "wire/Ethernet.h"
  23. #include "wire/Error.h"
  24. struct TAPWrapper_pvt
  25. {
  26. struct TAPWrapper pub;
  27. struct Interface* wrapped;
  28. struct Log* log;
  29. Identity
  30. };
  31. static uint8_t receiveMessage(struct Message* msg, struct Interface* iface)
  32. {
  33. struct TAPWrapper_pvt* tw = Identity_check((struct TAPWrapper_pvt*)iface->receiverContext);
  34. if (msg->length < Ethernet_SIZE-2) {
  35. Log_debug(tw->log, "runt");
  36. return 0;
  37. }
  38. // wacky 14 byte headers, back off into outer-space to create the padding...
  39. Message_shift(msg, 2, NULL);
  40. struct Ethernet eth;
  41. Message_pop(msg, &eth, sizeof(struct Ethernet), NULL);
  42. // Not for us and not multicast...
  43. if (Bits_memcmp(eth.destAddr, TAPWrapper_LOCAL_MAC, Ethernet_ADDRLEN)
  44. && !(eth.destAddr[0] & 0x01))
  45. {
  46. #ifdef Log_DEBUG
  47. uint8_t printedMac[18];
  48. AddrTools_printMac(printedMac, eth.destAddr);
  49. Log_debug(tw->log, "Packet destine for unknown ethernet MAC [%s]", printedMac);
  50. #endif
  51. //return 0;
  52. }
  53. if (Bits_memcmp(eth.srcAddr, tw->pub.peerAddress, Ethernet_ADDRLEN)) {
  54. if (Bits_isZero(tw->pub.peerAddress, Ethernet_ADDRLEN)) {
  55. Bits_memcpy(tw->pub.peerAddress, eth.srcAddr, Ethernet_ADDRLEN);
  56. } else {
  57. #ifdef Log_DEBUG
  58. uint8_t printedMac[18];
  59. AddrTools_printMac(printedMac, eth.srcAddr);
  60. Log_debug(tw->log, "DROP Packet with unexpected source MAC [%s]", printedMac);
  61. #endif
  62. return 0;
  63. }
  64. }
  65. TUNMessageType_push(msg, eth.ethertype, NULL);
  66. Interface_receiveMessage(&tw->pub.generic, msg);
  67. return 0;
  68. }
  69. static uint8_t sendMessage(struct Message* msg, struct Interface* iface)
  70. {
  71. struct TAPWrapper_pvt* tw = Identity_check((struct TAPWrapper_pvt*)iface);
  72. uint16_t etherType = TUNMessageType_pop(msg, NULL);
  73. struct Ethernet eth = { .ethertype = etherType };
  74. Bits_memcpyConst(eth.srcAddr, TAPWrapper_LOCAL_MAC, Ethernet_ADDRLEN);
  75. Bits_memcpyConst(eth.destAddr, tw->pub.peerAddress, Ethernet_ADDRLEN);
  76. if (Bits_isZero(tw->pub.peerAddress, Ethernet_ADDRLEN)) {
  77. Log_debug(tw->log, "DROP Packet because peers MAC is not yet known");
  78. return Error_UNDELIVERABLE;
  79. }
  80. Message_push(msg, &eth, sizeof(struct Ethernet), NULL);
  81. Message_shift(msg, -2, NULL);
  82. return Interface_sendMessage(tw->wrapped, msg);
  83. }
  84. struct TAPWrapper* TAPWrapper_new(struct Interface* external,
  85. struct Log* log,
  86. struct Allocator* alloc)
  87. {
  88. struct TAPWrapper_pvt* out = Allocator_clone(alloc, (&(struct TAPWrapper_pvt) {
  89. .wrapped = external,
  90. .log = log
  91. }));
  92. Identity_set(out);
  93. InterfaceWrapper_wrap(external, sendMessage, receiveMessage, &out->pub.generic);
  94. return &out->pub;
  95. }