TAPWrapper.c 3.8 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 <https://www.gnu.org/licenses/>.
  14. */
  15. #include "interface/tuntap/TAPWrapper.h"
  16. #include "interface/tuntap/TUNMessageType.h"
  17. #include "util/Bits.h"
  18. #include "util/Identity.h"
  19. #include "util/AddrTools.h"
  20. #include "util/Defined.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 Iface external;
  28. struct Log* log;
  29. Identity
  30. };
  31. static Iface_DEFUN receiveMessage(struct Message* msg, struct Iface* external)
  32. {
  33. struct TAPWrapper_pvt* tw = Identity_containerOf(external, struct TAPWrapper_pvt, external);
  34. if (Message_getLength(msg) < Ethernet_SIZE-2) {
  35. Log_debug(tw->log, "runt");
  36. return Error(msg, "RUNT");
  37. }
  38. // wacky 14 byte headers, back off into outer-space to create the padding...
  39. Er_assert(Message_eshift(msg, 2));
  40. struct Ethernet eth;
  41. Er_assert(Message_epop(msg, &eth, sizeof(struct Ethernet)));
  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. if (Defined(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. }
  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 Error(msg, "INVALID");
  63. }
  64. }
  65. Er_assert(TUNMessageType_push(msg, eth.ethertype));
  66. return Iface_next(&tw->pub.internal, msg);
  67. }
  68. static Iface_DEFUN sendMessage(struct Message* msg, struct Iface* internal)
  69. {
  70. struct TAPWrapper_pvt* tw = Identity_containerOf(internal, struct TAPWrapper_pvt, pub.internal);
  71. uint16_t etherType = Er_assert(TUNMessageType_pop(msg));
  72. struct Ethernet eth = { .ethertype = etherType };
  73. Bits_memcpy(eth.srcAddr, TAPWrapper_LOCAL_MAC, Ethernet_ADDRLEN);
  74. Bits_memcpy(eth.destAddr, tw->pub.peerAddress, Ethernet_ADDRLEN);
  75. if (Bits_isZero(tw->pub.peerAddress, Ethernet_ADDRLEN)) {
  76. Log_debug(tw->log, "DROP Packet because peers MAC is not yet known");
  77. return Error(msg, "INVALID");
  78. }
  79. Er_assert(Message_epush(msg, &eth, sizeof(struct Ethernet)));
  80. // struct Ethernet contains 2 bytes of padding at the beginning.
  81. Er_assert(Message_eshift(msg, -2));
  82. return Iface_next(&tw->external, msg);
  83. }
  84. struct TAPWrapper* TAPWrapper_new(struct Iface* external,
  85. struct Log* log,
  86. struct Allocator* alloc)
  87. {
  88. struct TAPWrapper_pvt* out = Allocator_calloc(alloc, sizeof(struct TAPWrapper_pvt), 1);
  89. Identity_set(out);
  90. out->log = log;
  91. out->external.send = receiveMessage;
  92. out->pub.internal.send = sendMessage;
  93. Iface_plumb(external, &out->external);
  94. return &out->pub;
  95. }