]>
Commit | Line | Data |
---|---|---|
67ece6df MM |
1 | /* |
2 | * BIRD Internet Routing Daemon -- Linux Multicasting and Network Includes | |
3 | * | |
f380aa60 | 4 | * (c) 1998--2000 Martin Mares <mj@ucw.cz> |
67ece6df MM |
5 | * |
6 | * Can be freely distributed and used under the terms of the GNU GPL. | |
7 | */ | |
8 | ||
e7b09e4a OZ |
9 | #include <net/if.h> |
10 | ||
67ece6df | 11 | #ifdef IPV6 |
a2867cd9 | 12 | |
84f07002 MM |
13 | #ifndef IPV6_UNICAST_HOPS |
14 | /* Needed on glibc 2.0 systems */ | |
f380aa60 | 15 | #include <linux/in6.h> |
84f07002 | 16 | #define CONFIG_IPV6_GLIBC_20 |
f380aa60 | 17 | #endif |
67ece6df | 18 | |
a2867cd9 MM |
19 | static inline void |
20 | set_inaddr(struct in6_addr *ia, ip_addr a) | |
21 | { | |
22 | ipa_hton(a); | |
23 | memcpy(ia, &a, sizeof(a)); | |
24 | } | |
25 | ||
353729f5 OZ |
26 | static inline void |
27 | get_inaddr(ip_addr *a, struct in6_addr *ia) | |
28 | { | |
29 | memcpy(a, ia, sizeof(*a)); | |
30 | ipa_ntoh(*a); | |
31 | } | |
32 | ||
e7b09e4a OZ |
33 | static inline char * |
34 | sysio_bind_to_iface(sock *s) | |
35 | { | |
36 | struct ifreq ifr; | |
37 | strcpy(ifr.ifr_name, s->iface->name); | |
38 | if (setsockopt(s->fd, SOL_SOCKET, SO_BINDTODEVICE, &ifr, sizeof(ifr)) < 0) | |
39 | return "SO_BINDTODEVICE"; | |
67ece6df | 40 | |
e7b09e4a OZ |
41 | return NULL; |
42 | } | |
43 | ||
44 | #else | |
0e5aa966 | 45 | |
a2867cd9 MM |
46 | static inline void |
47 | set_inaddr(struct in_addr *ia, ip_addr a) | |
48 | { | |
49 | ipa_hton(a); | |
50 | memcpy(&ia->s_addr, &a, sizeof(a)); | |
51 | } | |
52 | ||
353729f5 OZ |
53 | static inline void |
54 | get_inaddr(ip_addr *a, struct in_addr *ia) | |
55 | { | |
56 | memcpy(a, &ia->s_addr, sizeof(*a)); | |
57 | ipa_ntoh(*a); | |
58 | } | |
59 | ||
67ece6df MM |
60 | /* |
61 | * Multicasting in Linux systems is a real mess. Not only different kernels | |
62 | * have different interfaces, but also different libc's export it in different | |
63 | * ways. Horrible. | |
64 | */ | |
65 | ||
f9c799a0 OZ |
66 | |
67 | #if defined(CONFIG_LINUX_MC_MREQ) || defined(CONFIG_LINUX_MC_MREQ_BIND) | |
68 | /* | |
69 | * Older kernels support only struct mreq which matches interfaces by their | |
70 | * addresses and thus fails on unnumbered devices. On newer 2.0 kernels | |
71 | * we can use SO_BINDTODEVICE to circumvent this problem. | |
72 | */ | |
73 | ||
74 | #define MREQ_IFA struct in_addr | |
75 | #define MREQ_GRP struct ip_mreq | |
0aad2b92 | 76 | static inline void fill_mreq_ifa(struct in_addr *m, struct iface *ifa UNUSED, ip_addr saddr, ip_addr maddr UNUSED) |
67ece6df | 77 | { |
0aad2b92 | 78 | set_inaddr(m, saddr); |
f9c799a0 | 79 | } |
67ece6df | 80 | |
0aad2b92 | 81 | static inline void fill_mreq_grp(struct ip_mreq *m, struct iface *ifa, ip_addr saddr, ip_addr maddr) |
f9c799a0 OZ |
82 | { |
83 | bzero(m, sizeof(*m)); | |
84 | #ifdef CONFIG_LINUX_MC_MREQ_BIND | |
85 | m->imr_interface.s_addr = INADDR_ANY; | |
86 | #else | |
0aad2b92 | 87 | set_inaddr(&m->imr_interface, saddr); |
67ece6df | 88 | #endif |
f9c799a0 | 89 | set_inaddr(&m->imr_multiaddr, maddr); |
67ece6df | 90 | } |
f9c799a0 OZ |
91 | #endif |
92 | ||
67ece6df MM |
93 | |
94 | #ifdef CONFIG_LINUX_MC_MREQN | |
95 | /* | |
96 | * 2.1 and newer kernels use struct mreqn which passes ifindex, so no | |
97 | * problems with unnumbered devices. | |
98 | */ | |
99 | ||
100 | #ifndef HAVE_STRUCT_IP_MREQN | |
101 | /* Several versions of glibc don't define this structure, so we have to do it ourselves */ | |
102 | struct ip_mreqn | |
103 | { | |
104 | struct in_addr imr_multiaddr; /* IP multicast address of group */ | |
105 | struct in_addr imr_address; /* local IP address of interface */ | |
106 | int imr_ifindex; /* Interface index */ | |
107 | }; | |
108 | #endif | |
109 | ||
f9c799a0 OZ |
110 | #define MREQ_IFA struct ip_mreqn |
111 | #define MREQ_GRP struct ip_mreqn | |
112 | #define fill_mreq_ifa fill_mreq | |
113 | #define fill_mreq_grp fill_mreq | |
114 | ||
0aad2b92 | 115 | static inline void fill_mreq(struct ip_mreqn *m, struct iface *ifa, ip_addr saddr, ip_addr maddr) |
67ece6df | 116 | { |
f9c799a0 OZ |
117 | bzero(m, sizeof(*m)); |
118 | m->imr_ifindex = ifa->index; | |
0aad2b92 | 119 | set_inaddr(&m->imr_address, saddr); |
f9c799a0 | 120 | set_inaddr(&m->imr_multiaddr, maddr); |
67ece6df MM |
121 | } |
122 | #endif | |
123 | ||
f9c799a0 OZ |
124 | static inline char * |
125 | sysio_setup_multicast(sock *s) | |
67ece6df | 126 | { |
f9c799a0 OZ |
127 | MREQ_IFA m; |
128 | int zero = 0; | |
67ece6df | 129 | |
f9c799a0 OZ |
130 | if (setsockopt(s->fd, SOL_IP, IP_MULTICAST_LOOP, &zero, sizeof(zero)) < 0) |
131 | return "IP_MULTICAST_LOOP"; | |
132 | ||
133 | if (setsockopt(s->fd, SOL_IP, IP_MULTICAST_TTL, &s->ttl, sizeof(s->ttl)) < 0) | |
134 | return "IP_MULTICAST_TTL"; | |
135 | ||
136 | /* This defines where should we send _outgoing_ multicasts */ | |
0aad2b92 | 137 | fill_mreq_ifa(&m, s->iface, s->saddr, IPA_NONE); |
f9c799a0 OZ |
138 | if (setsockopt(s->fd, SOL_IP, IP_MULTICAST_IF, &m, sizeof(m)) < 0) |
139 | return "IP_MULTICAST_IF"; | |
140 | ||
141 | #if defined(CONFIG_LINUX_MC_MREQ_BIND) || defined(CONFIG_LINUX_MC_MREQN) | |
67ece6df MM |
142 | { |
143 | struct ifreq ifr; | |
144 | strcpy(ifr.ifr_name, s->iface->name); | |
145 | if (setsockopt(s->fd, SOL_SOCKET, SO_BINDTODEVICE, &ifr, sizeof(ifr)) < 0) | |
146 | return "SO_BINDTODEVICE"; | |
67ece6df | 147 | } |
67ece6df | 148 | #endif |
f9c799a0 OZ |
149 | |
150 | return NULL; | |
151 | } | |
152 | ||
153 | static inline char * | |
154 | sysio_join_group(sock *s, ip_addr maddr) | |
155 | { | |
156 | MREQ_GRP m; | |
157 | ||
67ece6df | 158 | /* And this one sets interface for _receiving_ multicasts from */ |
0aad2b92 | 159 | fill_mreq_grp(&m, s->iface, s->saddr, maddr); |
f9c799a0 | 160 | if (setsockopt(s->fd, SOL_IP, IP_ADD_MEMBERSHIP, &m, sizeof(m)) < 0) |
67ece6df | 161 | return "IP_ADD_MEMBERSHIP"; |
f9c799a0 OZ |
162 | |
163 | return NULL; | |
164 | } | |
165 | ||
166 | static inline char * | |
167 | sysio_leave_group(sock *s, ip_addr maddr) | |
168 | { | |
169 | MREQ_GRP m; | |
170 | ||
171 | /* And this one sets interface for _receiving_ multicasts from */ | |
0aad2b92 | 172 | fill_mreq_grp(&m, s->iface, s->saddr, maddr); |
f9c799a0 OZ |
173 | if (setsockopt(s->fd, SOL_IP, IP_DROP_MEMBERSHIP, &m, sizeof(m)) < 0) |
174 | return "IP_DROP_MEMBERSHIP"; | |
175 | ||
67ece6df MM |
176 | return NULL; |
177 | } | |
67ece6df MM |
178 | |
179 | #endif | |
d51aa281 | 180 | |
f9c799a0 | 181 | |
d51aa281 OZ |
182 | #include <linux/socket.h> |
183 | #include <linux/tcp.h> | |
184 | ||
185 | /* For the case that we have older kernel headers */ | |
186 | /* Copied from Linux kernel file include/linux/tcp.h */ | |
187 | ||
188 | #ifndef TCP_MD5SIG | |
189 | ||
190 | #define TCP_MD5SIG 14 | |
191 | #define TCP_MD5SIG_MAXKEYLEN 80 | |
192 | ||
193 | struct tcp_md5sig { | |
11ec0f1a | 194 | struct sockaddr_storage tcpm_addr; /* address associated */ |
d51aa281 OZ |
195 | __u16 __tcpm_pad1; /* zero */ |
196 | __u16 tcpm_keylen; /* key length */ | |
197 | __u32 __tcpm_pad2; /* zero */ | |
198 | __u8 tcpm_key[TCP_MD5SIG_MAXKEYLEN]; /* key (binary) */ | |
199 | }; | |
200 | ||
201 | #endif | |
2b70f074 OF |
202 | |
203 | static int | |
204 | sk_set_md5_auth_int(sock *s, sockaddr *sa, char *passwd) | |
205 | { | |
206 | struct tcp_md5sig md5; | |
207 | ||
208 | memset(&md5, 0, sizeof(md5)); | |
209 | memcpy(&md5.tcpm_addr, (struct sockaddr *) sa, sizeof(*sa)); | |
210 | ||
211 | if (passwd) | |
212 | { | |
213 | int len = strlen(passwd); | |
214 | ||
215 | if (len > TCP_MD5SIG_MAXKEYLEN) | |
216 | { | |
217 | log(L_ERR "MD5 password too long"); | |
218 | return -1; | |
219 | } | |
220 | ||
221 | md5.tcpm_keylen = len; | |
222 | memcpy(&md5.tcpm_key, passwd, len); | |
223 | } | |
224 | ||
225 | int rv = setsockopt(s->fd, IPPROTO_TCP, TCP_MD5SIG, &md5, sizeof(md5)); | |
226 | ||
227 | if (rv < 0) | |
228 | { | |
229 | if (errno == ENOPROTOOPT) | |
230 | log(L_ERR "Kernel does not support TCP MD5 signatures"); | |
231 | else | |
232 | log(L_ERR "sk_set_md5_auth_int: setsockopt: %m"); | |
233 | } | |
234 | ||
235 | return rv; | |
236 | } | |
353729f5 OZ |
237 | |
238 | ||
239 | #ifndef IPV6 | |
240 | ||
241 | /* RX/TX packet info handling for IPv4 */ | |
242 | /* Mostly similar to standardized IPv6 code */ | |
243 | ||
244 | #define CMSG_RX_SPACE CMSG_SPACE(sizeof(struct in_pktinfo)) | |
245 | #define CMSG_TX_SPACE CMSG_SPACE(sizeof(struct in_pktinfo)) | |
246 | ||
247 | static char * | |
248 | sysio_register_cmsgs(sock *s) | |
249 | { | |
250 | int ok = 1; | |
251 | if ((s->flags & SKF_LADDR_RX) && | |
252 | setsockopt(s->fd, IPPROTO_IP, IP_PKTINFO, &ok, sizeof(ok)) < 0) | |
253 | return "IP_PKTINFO"; | |
254 | ||
255 | return NULL; | |
256 | } | |
257 | ||
258 | static void | |
259 | sysio_process_rx_cmsgs(sock *s, struct msghdr *msg) | |
260 | { | |
261 | struct cmsghdr *cm; | |
262 | struct in_pktinfo *pi = NULL; | |
263 | ||
264 | if (!(s->flags & SKF_LADDR_RX)) | |
265 | return; | |
266 | ||
267 | for (cm = CMSG_FIRSTHDR(msg); cm != NULL; cm = CMSG_NXTHDR(msg, cm)) | |
268 | { | |
269 | if (cm->cmsg_level == IPPROTO_IP && cm->cmsg_type == IP_PKTINFO) | |
270 | pi = (struct in_pktinfo *) CMSG_DATA(cm); | |
271 | } | |
272 | ||
273 | if (!pi) | |
274 | { | |
275 | s->laddr = IPA_NONE; | |
276 | s->lifindex = 0; | |
277 | return; | |
278 | } | |
279 | ||
280 | get_inaddr(&s->laddr, &pi->ipi_addr); | |
281 | s->lifindex = pi->ipi_ifindex; | |
282 | return; | |
283 | } | |
284 | ||
646b24d9 | 285 | /* |
bed41728 OZ |
286 | static void |
287 | sysio_prepare_tx_cmsgs(sock *s, struct msghdr *msg, void *cbuf, size_t cbuflen) | |
353729f5 OZ |
288 | { |
289 | struct cmsghdr *cm; | |
290 | struct in_pktinfo *pi; | |
291 | ||
292 | if (!(s->flags & SKF_LADDR_TX)) | |
bed41728 OZ |
293 | return; |
294 | ||
295 | msg->msg_control = cbuf; | |
296 | msg->msg_controllen = cbuflen; | |
353729f5 OZ |
297 | |
298 | cm = CMSG_FIRSTHDR(msg); | |
299 | cm->cmsg_level = IPPROTO_IP; | |
300 | cm->cmsg_type = IP_PKTINFO; | |
301 | cm->cmsg_len = CMSG_LEN(sizeof(*pi)); | |
302 | ||
303 | pi = (struct in_pktinfo *) CMSG_DATA(cm); | |
304 | set_inaddr(&pi->ipi_spec_dst, s->saddr); | |
305 | pi->ipi_ifindex = s->iface ? s->iface->index : 0; | |
306 | ||
307 | msg->msg_controllen = cm->cmsg_len; | |
308 | } | |
646b24d9 OZ |
309 | */ |
310 | ||
353729f5 | 311 | #endif |
b1b19433 OZ |
312 | |
313 | ||
314 | #ifndef IP_MINTTL | |
315 | #define IP_MINTTL 21 | |
316 | #endif | |
317 | ||
318 | #ifndef IPV6_MINHOPCOUNT | |
319 | #define IPV6_MINHOPCOUNT 73 | |
320 | #endif | |
321 | ||
322 | ||
323 | #ifndef IPV6 | |
324 | ||
325 | static int | |
326 | sk_set_min_ttl4(sock *s, int ttl) | |
327 | { | |
328 | if (setsockopt(s->fd, IPPROTO_IP, IP_MINTTL, &ttl, sizeof(ttl)) < 0) | |
329 | { | |
330 | if (errno == ENOPROTOOPT) | |
331 | log(L_ERR "Kernel does not support IPv4 TTL security"); | |
332 | else | |
333 | log(L_ERR "sk_set_min_ttl4: setsockopt: %m"); | |
334 | ||
335 | return -1; | |
336 | } | |
337 | ||
338 | return 0; | |
339 | } | |
340 | ||
341 | #else | |
342 | ||
343 | static int | |
344 | sk_set_min_ttl6(sock *s, int ttl) | |
345 | { | |
346 | if (setsockopt(s->fd, IPPROTO_IPV6, IPV6_MINHOPCOUNT, &ttl, sizeof(ttl)) < 0) | |
347 | { | |
348 | if (errno == ENOPROTOOPT) | |
349 | log(L_ERR "Kernel does not support IPv6 TTL security"); | |
350 | else | |
351 | log(L_ERR "sk_set_min_ttl4: setsockopt: %m"); | |
352 | ||
353 | return -1; | |
354 | } | |
355 | ||
356 | return 0; | |
357 | } | |
358 | ||
359 | #endif |