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