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