linux.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /* NHRP daemon Linux specific glue
  2. * Copyright (c) 2014-2015 Timo Teräs
  3. *
  4. * This file is free software: you may copy, redistribute and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 2 of the License, or
  7. * (at your option) any later version.
  8. */
  9. #include <fcntl.h>
  10. #include <stdio.h>
  11. #include <unistd.h>
  12. #include <string.h>
  13. #include <sys/ioctl.h>
  14. #include <sys/socket.h>
  15. #include <sys/types.h>
  16. #include <asm/types.h>
  17. #include <arpa/inet.h>
  18. #include <linux/netlink.h>
  19. #include <linux/rtnetlink.h>
  20. #include <linux/ip.h>
  21. #include <linux/if_arp.h>
  22. #include <linux/if_tunnel.h>
  23. #include "nhrp_protocol.h"
  24. #include "os.h"
  25. #include "netlink.h"
  26. static int nhrp_socket_fd = -1;
  27. int os_socket(void)
  28. {
  29. if (nhrp_socket_fd < 0)
  30. nhrp_socket_fd = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_NHRP));
  31. return nhrp_socket_fd;
  32. }
  33. int os_sendmsg(const uint8_t *buf, size_t len, int ifindex, const uint8_t *addr, size_t addrlen)
  34. {
  35. struct sockaddr_ll lladdr;
  36. struct iovec iov = {
  37. .iov_base = (void*) buf,
  38. .iov_len = len,
  39. };
  40. struct msghdr msg = {
  41. .msg_name = &lladdr,
  42. .msg_namelen = sizeof(lladdr),
  43. .msg_iov = &iov,
  44. .msg_iovlen = 1,
  45. };
  46. int status;
  47. if (addrlen > sizeof(lladdr.sll_addr))
  48. return -1;
  49. memset(&lladdr, 0, sizeof(lladdr));
  50. lladdr.sll_family = AF_PACKET;
  51. lladdr.sll_protocol = htons(ETH_P_NHRP);
  52. lladdr.sll_ifindex = ifindex;
  53. lladdr.sll_halen = addrlen;
  54. memcpy(lladdr.sll_addr, addr, addrlen);
  55. status = sendmsg(nhrp_socket_fd, &msg, 0);
  56. if (status < 0)
  57. return -1;
  58. return 0;
  59. }
  60. int os_recvmsg(uint8_t *buf, size_t *len, int *ifindex, uint8_t *addr, size_t *addrlen)
  61. {
  62. struct sockaddr_ll lladdr;
  63. struct iovec iov = {
  64. .iov_base = buf,
  65. .iov_len = *len,
  66. };
  67. struct msghdr msg = {
  68. .msg_name = &lladdr,
  69. .msg_namelen = sizeof(lladdr),
  70. .msg_iov = &iov,
  71. .msg_iovlen = 1,
  72. };
  73. int r;
  74. r = recvmsg(nhrp_socket_fd, &msg, MSG_DONTWAIT);
  75. if (r < 0)
  76. return r;
  77. *len = r;
  78. *ifindex = lladdr.sll_ifindex;
  79. if (*addrlen <= (size_t) lladdr.sll_addr) {
  80. if (memcmp(lladdr.sll_addr, "\x00\x00\x00\x00", 4) != 0) {
  81. memcpy(addr, lladdr.sll_addr, lladdr.sll_halen);
  82. *addrlen = lladdr.sll_halen;
  83. } else {
  84. *addrlen = 0;
  85. }
  86. }
  87. return 0;
  88. }
  89. static int linux_configure_arp(const char *iface, int on)
  90. {
  91. struct ifreq ifr;
  92. strncpy(ifr.ifr_name, iface, IFNAMSIZ);
  93. if (ioctl(nhrp_socket_fd, SIOCGIFFLAGS, &ifr))
  94. return -1;
  95. if (on)
  96. ifr.ifr_flags &= ~IFF_NOARP;
  97. else
  98. ifr.ifr_flags |= IFF_NOARP;
  99. if (ioctl(nhrp_socket_fd, SIOCSIFFLAGS, &ifr))
  100. return -1;
  101. return 0;
  102. }
  103. static int linux_icmp_redirect_off(const char *iface)
  104. {
  105. char fname[256];
  106. int fd, ret = -1;
  107. sprintf(fname, "/proc/sys/net/ipv4/conf/%s/send_redirects", iface);
  108. fd = open(fname, O_WRONLY);
  109. if (fd < 0)
  110. return -1;
  111. if (write(fd, "0\n", 2) == 2)
  112. ret = 0;
  113. close(fd);
  114. return ret;
  115. }
  116. int os_configure_dmvpn(unsigned int ifindex, const char *ifname, int af)
  117. {
  118. int ret = -1;
  119. switch (af) {
  120. case AF_INET:
  121. ret = linux_icmp_redirect_off("all");
  122. ret |= linux_icmp_redirect_off(ifname);
  123. ret |= netlink_configure_arp(ifindex, AF_INET);
  124. ret |= linux_configure_arp(ifname, 1);
  125. break;
  126. }
  127. return ret;
  128. }