sysio.h 5.38 KB
Newer Older
1 2 3
/*
 *	BIRD Internet Routing Daemon -- Linux Multicasting and Network Includes
 *
4
 *	(c) 1998--2000 Martin Mares <mj@ucw.cz>
5 6 7 8 9
 *
 *	Can be freely distributed and used under the terms of the GNU GPL.
 */


10 11 12
#ifndef IP_MINTTL
#define IP_MINTTL 21
#endif
13

14 15 16
#ifndef IPV6_TCLASS
#define IPV6_TCLASS 67
#endif
17

18 19 20
#ifndef IPV6_MINHOPCOUNT
#define IPV6_MINHOPCOUNT 73
#endif
21

22

23
#ifndef TCP_MD5SIG
24

25 26
#define TCP_MD5SIG  14
#define TCP_MD5SIG_MAXKEYLEN 80
27

28 29 30 31 32 33 34
struct tcp_md5sig {
  struct  sockaddr_storage tcpm_addr;             /* address associated */
  u16   __tcpm_pad1;                              /* zero */
  u16   tcpm_keylen;                              /* key length */
  u32   __tcpm_pad2;                              /* zero */
  u8    tcpm_key[TCP_MD5SIG_MAXKEYLEN];           /* key (binary) */
};
35

36
#endif
37 38


39 40
/* Linux does not care if sa_len is larger than needed */
#define SA_LEN(x) sizeof(sockaddr)
41 42


43 44 45
/*
 *	Linux IPv4 multicast syscalls
 */
46

47 48
#define INIT_MREQ4(maddr,ifa) \
  { .imr_multiaddr = ipa_to_in4(maddr), .imr_ifindex = ifa->index }
49

50 51 52 53 54 55
static inline int
sk_setup_multicast4(sock *s)
{
  struct ip_mreqn mr = { .imr_ifindex = s->iface->index };
  int ttl = s->ttl;
  int n = 0;
56

57 58 59
  /* This defines where should we send _outgoing_ multicasts */
  if (setsockopt(s->fd, SOL_IP, IP_MULTICAST_IF, &mr, sizeof(mr)) < 0)
    ERR("IP_MULTICAST_IF");
60

61 62
  if (setsockopt(s->fd, SOL_IP, IP_MULTICAST_TTL, &ttl, sizeof(ttl)) < 0)
    ERR("IP_MULTICAST_TTL");
63

64 65
  if (setsockopt(s->fd, SOL_IP, IP_MULTICAST_LOOP, &n, sizeof(n)) < 0)
    ERR("IP_MULTICAST_LOOP");
66

67 68
  return 0;
}
69

70 71 72 73
static inline int
sk_join_group4(sock *s, ip_addr maddr)
{
  struct ip_mreqn mr = INIT_MREQ4(maddr, s->iface);
74

75 76
  if (setsockopt(s->fd, SOL_IP, IP_ADD_MEMBERSHIP, &mr, sizeof(mr)) < 0)
    ERR("IP_ADD_MEMBERSHIP");
77

78 79
  return 0;
}
80

81 82
static inline int
sk_leave_group4(sock *s, ip_addr maddr)
83
{
84
  struct ip_mreqn mr = INIT_MREQ4(maddr, s->iface);
85

86 87
  if (setsockopt(s->fd, SOL_IP, IP_DROP_MEMBERSHIP, &mr, sizeof(mr)) < 0)
    ERR("IP_DROP_MEMBERSHIP");
88

89
  return 0;
90
}
91 92


93 94 95
/*
 *	Linux IPv4 packet control messages
 */
96 97 98

/* Mostly similar to standardized IPv6 code */

99 100
#define CMSG4_SPACE_PKTINFO CMSG_SPACE(sizeof(struct in_pktinfo))
#define CMSG4_SPACE_TTL CMSG_SPACE(sizeof(int))
101

102 103
static inline int
sk_request_cmsg4_pktinfo(sock *s)
104
{
105
  int y = 1;
106

107 108
  if (setsockopt(s->fd, SOL_IP, IP_PKTINFO, &y, sizeof(y)) < 0)
    ERR("IP_PKTINFO");
109

110
  return 0;
111 112
}

113 114
static inline int
sk_request_cmsg4_ttl(sock *s)
115
{
116
  int y = 1;
117

118 119
  if (setsockopt(s->fd, SOL_IP, IP_RECVTTL, &y, sizeof(y)) < 0)
    ERR("IP_RECVTTL");
120

121 122
  return 0;
}
123

124 125 126 127
static inline void
sk_process_cmsg4_pktinfo(sock *s, struct cmsghdr *cm)
{
  if (cm->cmsg_type == IP_PKTINFO)
128
  {
129 130 131
    struct in_pktinfo *pi = (struct in_pktinfo *) CMSG_DATA(cm);
    s->laddr = ipa_from_in4(pi->ipi_addr);
    s->lifindex = pi->ipi_ifindex;
132
  }
133
}
134

135 136 137 138 139
static inline void
sk_process_cmsg4_ttl(sock *s, struct cmsghdr *cm)
{
  if (cm->cmsg_type == IP_TTL)
    s->rcv_ttl = * (int *) CMSG_DATA(cm);
140 141
}

142 143
static inline void
sk_prepare_cmsgs4(sock *s, struct msghdr *msg, void *cbuf, size_t cbuflen)
144 145 146
{
  struct cmsghdr *cm;
  struct in_pktinfo *pi;
147
  int controllen = 0;
148

149 150
  msg->msg_control = cbuf;
  msg->msg_controllen = cbuflen;
151 152

  cm = CMSG_FIRSTHDR(msg);
153
  cm->cmsg_level = SOL_IP;
154 155
  cm->cmsg_type = IP_PKTINFO;
  cm->cmsg_len = CMSG_LEN(sizeof(*pi));
156
  controllen += CMSG_SPACE(sizeof(*pi));
157 158 159

  pi = (struct in_pktinfo *) CMSG_DATA(cm);
  pi->ipi_ifindex = s->iface ? s->iface->index : 0;
160 161
  pi->ipi_spec_dst = ipa_to_in4(s->saddr);
  pi->ipi_addr = ipa_to_in4(IPA_NONE);
162

163
  msg->msg_controllen = controllen;
164
}
165

Ondřej Zajíček's avatar
Ondřej Zajíček committed
166

167 168 169
/*
 *	Miscellaneous Linux socket syscalls
 */
170

171
int
172
sk_set_md5_auth(sock *s, ip_addr local UNUSED, ip_addr remote, struct iface *ifa, char *passwd, int setkey UNUSED)
173 174
{
  struct tcp_md5sig md5;
175

176
  memset(&md5, 0, sizeof(md5));
177
  sockaddr_fill((sockaddr *) &md5.tcpm_addr, s->af, remote, ifa, 0);
178

179 180 181 182 183
  if (passwd)
  {
    int len = strlen(passwd);

    if (len > TCP_MD5SIG_MAXKEYLEN)
184
      ERR_MSG("The password for TCP MD5 Signature is too long");
185

186 187 188 189 190 191 192 193 194 195 196
    md5.tcpm_keylen = len;
    memcpy(&md5.tcpm_key, passwd, len);
  }

  if (setsockopt(s->fd, SOL_TCP, TCP_MD5SIG, &md5, sizeof(md5)) < 0)
  {
    if (errno == ENOPROTOOPT)
      ERR_MSG("Kernel does not support TCP MD5 signatures");
    else
      ERR("TCP_MD5SIG");
  }
197

198 199
  return 0;
}
200

201
static inline int
202 203
sk_set_min_ttl4(sock *s, int ttl)
{
204
  if (setsockopt(s->fd, SOL_IP, IP_MINTTL, &ttl, sizeof(ttl)) < 0)
205 206
  {
    if (errno == ENOPROTOOPT)
207
      ERR_MSG("Kernel does not support IPv4 TTL security");
208
    else
209
      ERR("IP_MINTTL");
210 211 212 213 214
  }

  return 0;
}

215
static inline int
216 217
sk_set_min_ttl6(sock *s, int ttl)
{
218
  if (setsockopt(s->fd, SOL_IPV6, IPV6_MINHOPCOUNT, &ttl, sizeof(ttl)) < 0)
219 220
  {
    if (errno == ENOPROTOOPT)
221
      ERR_MSG("Kernel does not support IPv6 TTL security");
222
    else
223
      ERR("IPV6_MINHOPCOUNT");
224 225 226 227 228
  }

  return 0;
}

229 230 231 232
static inline int
sk_disable_mtu_disc4(sock *s)
{
  int dont = IP_PMTUDISC_DONT;
233

234 235
  if (setsockopt(s->fd, SOL_IP, IP_MTU_DISCOVER, &dont, sizeof(dont)) < 0)
    ERR("IP_MTU_DISCOVER");
236

237 238 239 240 241 242 243 244 245 246 247 248 249
  return 0;
}

static inline int
sk_disable_mtu_disc6(sock *s)
{
  int dont = IPV6_PMTUDISC_DONT;

  if (setsockopt(s->fd, SOL_IPV6, IPV6_MTU_DISCOVER, &dont, sizeof(dont)) < 0)
    ERR("IPV6_MTU_DISCOVER");

  return 0;
}
250 251 252

int sk_priority_control = 7;

253
static inline int
254 255 256
sk_set_priority(sock *s, int prio)
{
  if (setsockopt(s->fd, SOL_SOCKET, SO_PRIORITY, &prio, sizeof(prio)) < 0)
257
    ERR("SO_PRIORITY");
258 259 260

  return 0;
}
261