]> git.proxmox.com Git - mirror_frr.git/blob - nhrpd/linux.c
nhrpd: Add support for forwarding multicast packets
[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 static int nhrp_socket_fd = -1;
36
37 int os_socket(void)
38 {
39 if (nhrp_socket_fd < 0)
40 nhrp_socket_fd =
41 socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_NHRP));
42 return nhrp_socket_fd;
43 }
44
45 int os_sendmsg(const uint8_t *buf, size_t len, int ifindex, const uint8_t *addr,
46 size_t addrlen, uint16_t protocol)
47 {
48 struct sockaddr_ll lladdr;
49 struct iovec iov = {
50 .iov_base = (void *)buf, .iov_len = len,
51 };
52 struct msghdr msg = {
53 .msg_name = &lladdr,
54 .msg_namelen = sizeof(lladdr),
55 .msg_iov = &iov,
56 .msg_iovlen = 1,
57 };
58 int status;
59
60 if (addrlen > sizeof(lladdr.sll_addr))
61 return -1;
62
63 memset(&lladdr, 0, sizeof(lladdr));
64 lladdr.sll_family = AF_PACKET;
65 lladdr.sll_protocol = htons(protocol);
66 lladdr.sll_ifindex = ifindex;
67 lladdr.sll_halen = addrlen;
68 memcpy(lladdr.sll_addr, addr, addrlen);
69
70 status = sendmsg(os_socket(), &msg, 0);
71 if (status < 0)
72 return -errno;
73
74 return status;
75 }
76
77 int os_recvmsg(uint8_t *buf, size_t *len, int *ifindex, uint8_t *addr,
78 size_t *addrlen)
79 {
80 struct sockaddr_ll lladdr;
81 struct iovec iov = {
82 .iov_base = buf, .iov_len = *len,
83 };
84 struct msghdr msg = {
85 .msg_name = &lladdr,
86 .msg_namelen = sizeof(lladdr),
87 .msg_iov = &iov,
88 .msg_iovlen = 1,
89 };
90 int r;
91
92 r = recvmsg(nhrp_socket_fd, &msg, MSG_DONTWAIT);
93 if (r < 0)
94 return r;
95
96 *len = r;
97 *ifindex = lladdr.sll_ifindex;
98
99 if (*addrlen <= (size_t)lladdr.sll_addr) {
100 if (memcmp(lladdr.sll_addr, "\x00\x00\x00\x00", 4) != 0) {
101 memcpy(addr, lladdr.sll_addr, lladdr.sll_halen);
102 *addrlen = lladdr.sll_halen;
103 } else {
104 *addrlen = 0;
105 }
106 }
107
108 return 0;
109 }
110
111 static int linux_configure_arp(const char *iface, int on)
112 {
113 struct ifreq ifr;
114
115 strncpy(ifr.ifr_name, iface, IFNAMSIZ - 1);
116 if (ioctl(nhrp_socket_fd, SIOCGIFFLAGS, &ifr))
117 return -1;
118
119 if (on)
120 ifr.ifr_flags &= ~IFF_NOARP;
121 else
122 ifr.ifr_flags |= IFF_NOARP;
123
124 if (ioctl(nhrp_socket_fd, SIOCSIFFLAGS, &ifr))
125 return -1;
126
127 return 0;
128 }
129
130 static int linux_icmp_redirect_off(const char *iface)
131 {
132 char fname[PATH_MAX];
133 int fd, ret = -1;
134
135 snprintf(fname, sizeof(fname),
136 "/proc/sys/net/ipv4/conf/%s/send_redirects", iface);
137 fd = open(fname, O_WRONLY);
138 if (fd < 0)
139 return -1;
140 if (write(fd, "0\n", 2) == 2)
141 ret = 0;
142 close(fd);
143
144 return ret;
145 }
146
147 int os_configure_dmvpn(unsigned int ifindex, const char *ifname, int af)
148 {
149 int ret = 0;
150
151 switch (af) {
152 case AF_INET:
153 ret |= linux_icmp_redirect_off("all");
154 ret |= linux_icmp_redirect_off(ifname);
155 break;
156 }
157 ret |= linux_configure_arp(ifname, 1);
158 ret |= netlink_configure_arp(ifindex, af);
159
160 return ret;
161 }