1 /* NHRP daemon Linux specific glue
2 * Copyright (c) 2014-2015 Timo Teräs
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.
14 #include <sys/ioctl.h>
15 #include <sys/socket.h>
16 #include <sys/types.h>
17 #include <asm/types.h>
18 #include <arpa/inet.h>
19 #include <linux/netlink.h>
20 #include <linux/rtnetlink.h>
22 #include <linux/if_arp.h>
23 #include <linux/if_tunnel.h>
25 #include "nhrp_protocol.h"
29 static int nhrp_socket_fd = -1;
33 if (nhrp_socket_fd < 0)
34 nhrp_socket_fd = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_NHRP));
35 return nhrp_socket_fd;
38 int os_sendmsg(const uint8_t *buf, size_t len, int ifindex, const uint8_t *addr, size_t addrlen)
40 struct sockaddr_ll lladdr;
42 .iov_base = (void*) buf,
47 .msg_namelen = sizeof(lladdr),
53 if (addrlen > sizeof(lladdr.sll_addr))
56 memset(&lladdr, 0, sizeof(lladdr));
57 lladdr.sll_family = AF_PACKET;
58 lladdr.sll_protocol = htons(ETH_P_NHRP);
59 lladdr.sll_ifindex = ifindex;
60 lladdr.sll_halen = addrlen;
61 memcpy(lladdr.sll_addr, addr, addrlen);
63 status = sendmsg(nhrp_socket_fd, &msg, 0);
70 int os_recvmsg(uint8_t *buf, size_t *len, int *ifindex, uint8_t *addr, size_t *addrlen)
72 struct sockaddr_ll lladdr;
79 .msg_namelen = sizeof(lladdr),
85 r = recvmsg(nhrp_socket_fd, &msg, MSG_DONTWAIT);
90 *ifindex = lladdr.sll_ifindex;
92 if (*addrlen <= (size_t) lladdr.sll_addr) {
93 if (memcmp(lladdr.sll_addr, "\x00\x00\x00\x00", 4) != 0) {
94 memcpy(addr, lladdr.sll_addr, lladdr.sll_halen);
95 *addrlen = lladdr.sll_halen;
104 static int linux_configure_arp(const char *iface, int on)
108 strncpy(ifr.ifr_name, iface, IFNAMSIZ);
109 if (ioctl(nhrp_socket_fd, SIOCGIFFLAGS, &ifr))
113 ifr.ifr_flags &= ~IFF_NOARP;
115 ifr.ifr_flags |= IFF_NOARP;
117 if (ioctl(nhrp_socket_fd, SIOCSIFFLAGS, &ifr))
123 static int linux_icmp_redirect_off(const char *iface)
128 sprintf(fname, "/proc/sys/net/ipv4/conf/%s/send_redirects", iface);
129 fd = open(fname, O_WRONLY);
132 if (write(fd, "0\n", 2) == 2)
139 int os_configure_dmvpn(unsigned int ifindex, const char *ifname, int af)
145 ret |= linux_icmp_redirect_off("all");
146 ret |= linux_icmp_redirect_off(ifname);
149 ret |= linux_configure_arp(ifname, 1);
150 ret |= netlink_configure_arp(ifindex, af);