]> git.proxmox.com Git - mirror_frr.git/blob - nhrpd/linux.c
Merge pull request #8409 from rgirada/ospf-memleak
[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, fd;
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 fd = os_socket();
76 if (fd < 0)
77 return -1;
78
79 status = sendmsg(fd, &msg, 0);
80 if (status < 0)
81 return -errno;
82
83 return status;
84 }
85
86 int os_recvmsg(uint8_t *buf, size_t *len, int *ifindex, uint8_t *addr,
87 size_t *addrlen)
88 {
89 struct sockaddr_ll lladdr;
90 struct iovec iov = {
91 .iov_base = buf, .iov_len = *len,
92 };
93 struct msghdr msg = {
94 .msg_name = &lladdr,
95 .msg_namelen = sizeof(lladdr),
96 .msg_iov = &iov,
97 .msg_iovlen = 1,
98 };
99 int r;
100
101 r = recvmsg(nhrp_socket_fd, &msg, MSG_DONTWAIT);
102 if (r < 0)
103 return r;
104
105 *len = r;
106 *ifindex = lladdr.sll_ifindex;
107
108 if (*addrlen <= (size_t)lladdr.sll_addr) {
109 if (memcmp(lladdr.sll_addr, "\x00\x00\x00\x00", 4) != 0) {
110 memcpy(addr, lladdr.sll_addr, lladdr.sll_halen);
111 *addrlen = lladdr.sll_halen;
112 } else {
113 *addrlen = 0;
114 }
115 }
116
117 return 0;
118 }
119
120 static int linux_configure_arp(const char *iface, int on)
121 {
122 struct ifreq ifr;
123
124 strlcpy(ifr.ifr_name, iface, IFNAMSIZ);
125 if (ioctl(nhrp_socket_fd, SIOCGIFFLAGS, &ifr))
126 return -1;
127
128 if (on)
129 ifr.ifr_flags &= ~IFF_NOARP;
130 else
131 ifr.ifr_flags |= IFF_NOARP;
132
133 if (ioctl(nhrp_socket_fd, SIOCSIFFLAGS, &ifr))
134 return -1;
135
136 return 0;
137 }
138
139 static int linux_icmp_redirect_off(const char *iface)
140 {
141 char fname[PATH_MAX];
142 int fd, ret = -1;
143
144 snprintf(fname, sizeof(fname),
145 "/proc/sys/net/ipv4/conf/%s/send_redirects", iface);
146 fd = open(fname, O_WRONLY);
147 if (fd < 0)
148 return -1;
149 if (write(fd, "0\n", 2) == 2)
150 ret = 0;
151 close(fd);
152
153 return ret;
154 }
155
156 int os_configure_dmvpn(unsigned int ifindex, const char *ifname, int af)
157 {
158 int ret = 0;
159
160 switch (af) {
161 case AF_INET:
162 ret |= linux_icmp_redirect_off("all");
163 ret |= linux_icmp_redirect_off(ifname);
164 break;
165 }
166 ret |= linux_configure_arp(ifname, 1);
167
168 return ret;
169 }