2 * BIRD Internet Routing Daemon -- Linux Multicasting and Network Includes
4 * (c) 1998--2000 Martin Mares <mj@ucw.cz>
6 * Can be freely distributed and used under the terms of the GNU GPL.
9 #ifndef IPV6_MINHOPCOUNT
10 #define IPV6_MINHOPCOUNT 73
13 #ifndef TCP_MD5SIG_EXT
14 #define TCP_MD5SIG_EXT 32
17 #ifndef TCP_MD5SIG_FLAG_PREFIX
18 #define TCP_MD5SIG_FLAG_PREFIX 1
21 /* We redefine the tcp_md5sig structure with different name to avoid collision with older headers */
22 struct tcp_md5sig_ext
{
23 struct sockaddr_storage tcpm_addr
; /* Address associated */
24 u8 tcpm_flags
; /* Extension flags */
25 u8 tcpm_prefixlen
; /* Address prefix */
26 u16 tcpm_keylen
; /* Key length */
27 u32 __tcpm_pad2
; /* Zero */
28 u8 tcpm_key
[TCP_MD5SIG_MAXKEYLEN
]; /* Key (binary) */
32 /* Linux does not care if sa_len is larger than needed */
33 #define SA_LEN(x) sizeof(sockaddr)
37 * Linux IPv4 multicast syscalls
40 #define INIT_MREQ4(maddr,ifa) \
41 { .imr_multiaddr = ipa_to_in4(maddr), .imr_ifindex = ifa->index }
44 sk_setup_multicast4(sock
*s
)
46 struct ip_mreqn mr
= { .imr_ifindex
= s
->iface
->index
};
50 /* This defines where should we send _outgoing_ multicasts */
51 if (setsockopt(s
->fd
, SOL_IP
, IP_MULTICAST_IF
, &mr
, sizeof(mr
)) < 0)
52 ERR("IP_MULTICAST_IF");
54 if (setsockopt(s
->fd
, SOL_IP
, IP_MULTICAST_TTL
, &ttl
, sizeof(ttl
)) < 0)
55 ERR("IP_MULTICAST_TTL");
57 if (setsockopt(s
->fd
, SOL_IP
, IP_MULTICAST_LOOP
, &n
, sizeof(n
)) < 0)
58 ERR("IP_MULTICAST_LOOP");
64 sk_join_group4(sock
*s
, ip_addr maddr
)
66 struct ip_mreqn mr
= INIT_MREQ4(maddr
, s
->iface
);
68 if (setsockopt(s
->fd
, SOL_IP
, IP_ADD_MEMBERSHIP
, &mr
, sizeof(mr
)) < 0)
69 ERR("IP_ADD_MEMBERSHIP");
75 sk_leave_group4(sock
*s
, ip_addr maddr
)
77 struct ip_mreqn mr
= INIT_MREQ4(maddr
, s
->iface
);
79 if (setsockopt(s
->fd
, SOL_IP
, IP_DROP_MEMBERSHIP
, &mr
, sizeof(mr
)) < 0)
80 ERR("IP_DROP_MEMBERSHIP");
87 * Linux IPv4 packet control messages
90 /* Mostly similar to standardized IPv6 code */
92 #define CMSG4_SPACE_PKTINFO CMSG_SPACE(sizeof(struct in_pktinfo))
93 #define CMSG4_SPACE_TTL CMSG_SPACE(sizeof(int))
96 sk_request_cmsg4_pktinfo(sock
*s
)
100 if (setsockopt(s
->fd
, SOL_IP
, IP_PKTINFO
, &y
, sizeof(y
)) < 0)
107 sk_request_cmsg4_ttl(sock
*s
)
111 if (setsockopt(s
->fd
, SOL_IP
, IP_RECVTTL
, &y
, sizeof(y
)) < 0)
118 sk_process_cmsg4_pktinfo(sock
*s
, struct cmsghdr
*cm
)
120 if (cm
->cmsg_type
== IP_PKTINFO
)
122 struct in_pktinfo
*pi
= (struct in_pktinfo
*) CMSG_DATA(cm
);
123 s
->laddr
= ipa_from_in4(pi
->ipi_addr
);
124 s
->lifindex
= pi
->ipi_ifindex
;
129 sk_process_cmsg4_ttl(sock
*s
, struct cmsghdr
*cm
)
131 if (cm
->cmsg_type
== IP_TTL
)
132 s
->rcv_ttl
= * (int *) CMSG_DATA(cm
);
136 sk_prepare_cmsgs4(sock
*s
, struct msghdr
*msg
, void *cbuf
, size_t cbuflen
)
139 struct in_pktinfo
*pi
;
142 msg
->msg_control
= cbuf
;
143 msg
->msg_controllen
= cbuflen
;
145 cm
= CMSG_FIRSTHDR(msg
);
146 cm
->cmsg_level
= SOL_IP
;
147 cm
->cmsg_type
= IP_PKTINFO
;
148 cm
->cmsg_len
= CMSG_LEN(sizeof(*pi
));
149 controllen
+= CMSG_SPACE(sizeof(*pi
));
151 pi
= (struct in_pktinfo
*) CMSG_DATA(cm
);
152 pi
->ipi_ifindex
= s
->iface
? s
->iface
->index
: 0;
153 pi
->ipi_spec_dst
= ipa_to_in4(s
->saddr
);
154 pi
->ipi_addr
= ipa_to_in4(IPA_NONE
);
156 msg
->msg_controllen
= controllen
;
161 * Miscellaneous Linux socket syscalls
165 sk_set_md5_auth(sock
*s
, ip_addr local UNUSED
, ip_addr remote
, int pxlen
, struct iface
*ifa
, char *passwd
, int setkey UNUSED
)
167 struct tcp_md5sig_ext md5
;
169 memset(&md5
, 0, sizeof(md5
));
170 sockaddr_fill((sockaddr
*) &md5
.tcpm_addr
, s
->af
, remote
, ifa
, 0);
174 int len
= strlen(passwd
);
176 if (len
> TCP_MD5SIG_MAXKEYLEN
)
177 ERR_MSG("The password for TCP MD5 Signature is too long");
179 md5
.tcpm_keylen
= len
;
180 memcpy(&md5
.tcpm_key
, passwd
, len
);
185 if (setsockopt(s
->fd
, SOL_TCP
, TCP_MD5SIG
, &md5
, sizeof(md5
)) < 0)
186 if (errno
== ENOPROTOOPT
)
187 ERR_MSG("Kernel does not support TCP MD5 signatures");
193 md5
.tcpm_flags
= TCP_MD5SIG_FLAG_PREFIX
;
194 md5
.tcpm_prefixlen
= pxlen
;
196 if (setsockopt(s
->fd
, SOL_TCP
, TCP_MD5SIG_EXT
, &md5
, sizeof(md5
)) < 0)
198 if (errno
== ENOPROTOOPT
)
199 ERR_MSG("Kernel does not support extended TCP MD5 signatures");
201 ERR("TCP_MD5SIG_EXT");
209 sk_set_min_ttl4(sock
*s
, int ttl
)
211 if (setsockopt(s
->fd
, SOL_IP
, IP_MINTTL
, &ttl
, sizeof(ttl
)) < 0)
213 if (errno
== ENOPROTOOPT
)
214 ERR_MSG("Kernel does not support IPv4 TTL security");
223 sk_set_min_ttl6(sock
*s
, int ttl
)
225 if (setsockopt(s
->fd
, SOL_IPV6
, IPV6_MINHOPCOUNT
, &ttl
, sizeof(ttl
)) < 0)
227 if (errno
== ENOPROTOOPT
)
228 ERR_MSG("Kernel does not support IPv6 TTL security");
230 ERR("IPV6_MINHOPCOUNT");
237 sk_disable_mtu_disc4(sock
*s
)
239 int dont
= IP_PMTUDISC_DONT
;
241 if (setsockopt(s
->fd
, SOL_IP
, IP_MTU_DISCOVER
, &dont
, sizeof(dont
)) < 0)
242 ERR("IP_MTU_DISCOVER");
248 sk_disable_mtu_disc6(sock
*s
)
250 int dont
= IPV6_PMTUDISC_DONT
;
252 if (setsockopt(s
->fd
, SOL_IPV6
, IPV6_MTU_DISCOVER
, &dont
, sizeof(dont
)) < 0)
253 ERR("IPV6_MTU_DISCOVER");
258 int sk_priority_control
= 7;
261 sk_set_priority(sock
*s
, int prio
)
263 if (setsockopt(s
->fd
, SOL_SOCKET
, SO_PRIORITY
, &prio
, sizeof(prio
)) < 0)