]> git.proxmox.com Git - mirror_frr.git/blob - nhrpd/linux.c
Merge pull request #8153 from reubendowle/nhrp-multicast
[mirror_frr.git] / nhrpd / linux.c
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
10 #ifdef HAVE_CONFIG_H
11 #include "config.h"
12 #endif
13
14 #include <fcntl.h>
15 #include <stdio.h>
16 #include <unistd.h>
17 #include <string.h>
18 #include <errno.h>
19 #include <sys/ioctl.h>
20 #include <sys/socket.h>
21 #include <sys/types.h>
22 #include <asm/types.h>
23 #include <arpa/inet.h>
24 #include <linux/netlink.h>
25 #include <linux/rtnetlink.h>
26 #include <linux/ip.h>
27 #include <linux/if_arp.h>
28 #include <linux/if_tunnel.h>
29 #include <linux/limits.h>
30
31 #include "nhrp_protocol.h"
32 #include "os.h"
33 #include "netlink.h"
34
35 #ifndef HAVE_STRLCPY
36 size_t strlcpy(char *__restrict dest,
37 const char *__restrict src, size_t destsize);
38 #endif
39
40 static int nhrp_socket_fd = -1;
41
42 int os_socket(void)
43 {
44 if (nhrp_socket_fd < 0)
45 nhrp_socket_fd =
46 socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_NHRP));
47 return nhrp_socket_fd;
48 }
49
50 int os_sendmsg(const uint8_t *buf, size_t len, int ifindex, const uint8_t *addr,
51 size_t addrlen, uint16_t protocol)
52 {
53 struct sockaddr_ll lladdr;
54 struct iovec iov = {
55 .iov_base = (void *)buf, .iov_len = len,
56 };
57 struct msghdr msg = {
58 .msg_name = &lladdr,
59 .msg_namelen = sizeof(lladdr),
60 .msg_iov = &iov,
61 .msg_iovlen = 1,
62 };
63 int status;
64
65 if (addrlen > sizeof(lladdr.sll_addr))
66 return -1;
67
68 memset(&lladdr, 0, sizeof(lladdr));
69 lladdr.sll_family = AF_PACKET;
70 lladdr.sll_protocol = htons(protocol);
71 lladdr.sll_ifindex = ifindex;
72 lladdr.sll_halen = addrlen;
73 memcpy(lladdr.sll_addr, addr, addrlen);
74
75 status = sendmsg(os_socket(), &msg, 0);
76 if (status < 0)
77 return -errno;
78
79 return status;
80 }
81
82 int os_recvmsg(uint8_t *buf, size_t *len, int *ifindex, uint8_t *addr,
83 size_t *addrlen)
84 {
85 struct sockaddr_ll lladdr;
86 struct iovec iov = {
87 .iov_base = buf, .iov_len = *len,
88 };
89 struct msghdr msg = {
90 .msg_name = &lladdr,
91 .msg_namelen = sizeof(lladdr),
92 .msg_iov = &iov,
93 .msg_iovlen = 1,
94 };
95 int r;
96
97 r = recvmsg(nhrp_socket_fd, &msg, MSG_DONTWAIT);
98 if (r < 0)
99 return r;
100
101 *len = r;
102 *ifindex = lladdr.sll_ifindex;
103
104 if (*addrlen <= (size_t)lladdr.sll_addr) {
105 if (memcmp(lladdr.sll_addr, "\x00\x00\x00\x00", 4) != 0) {
106 memcpy(addr, lladdr.sll_addr, lladdr.sll_halen);
107 *addrlen = lladdr.sll_halen;
108 } else {
109 *addrlen = 0;
110 }
111 }
112
113 return 0;
114 }
115
116 static int linux_configure_arp(const char *iface, int on)
117 {
118 struct ifreq ifr;
119
120 strlcpy(ifr.ifr_name, iface, IFNAMSIZ);
121 if (ioctl(nhrp_socket_fd, SIOCGIFFLAGS, &ifr))
122 return -1;
123
124 if (on)
125 ifr.ifr_flags &= ~IFF_NOARP;
126 else
127 ifr.ifr_flags |= IFF_NOARP;
128
129 if (ioctl(nhrp_socket_fd, SIOCSIFFLAGS, &ifr))
130 return -1;
131
132 return 0;
133 }
134
135 static int linux_icmp_redirect_off(const char *iface)
136 {
137 char fname[PATH_MAX];
138 int fd, ret = -1;
139
140 snprintf(fname, sizeof(fname),
141 "/proc/sys/net/ipv4/conf/%s/send_redirects", iface);
142 fd = open(fname, O_WRONLY);
143 if (fd < 0)
144 return -1;
145 if (write(fd, "0\n", 2) == 2)
146 ret = 0;
147 close(fd);
148
149 return ret;
150 }
151
152 int os_configure_dmvpn(unsigned int ifindex, const char *ifname, int af)
153 {
154 int ret = 0;
155
156 switch (af) {
157 case AF_INET:
158 ret |= linux_icmp_redirect_off("all");
159 ret |= linux_icmp_redirect_off(ifname);
160 break;
161 }
162 ret |= linux_configure_arp(ifname, 1);
163
164 return ret;
165 }