sysio.h 6.51 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
 *
 *	Can be freely distributed and used under the terms of the GNU GPL.
 */

9 10
#include <net/if.h>

11
#ifdef IPV6
12

13 14
#ifndef IPV6_UNICAST_HOPS
/* Needed on glibc 2.0 systems */
15
#include <linux/in6.h>
16
#define CONFIG_IPV6_GLIBC_20
17
#endif
18

19 20 21 22 23 24 25
static inline void
set_inaddr(struct in6_addr *ia, ip_addr a)
{
  ipa_hton(a);
  memcpy(ia, &a, sizeof(a));
}

26 27 28 29 30 31 32
static inline void
get_inaddr(ip_addr *a, struct in6_addr *ia)
{
  memcpy(a, ia, sizeof(*a));
  ipa_ntoh(*a);
}

33
#else
34

35 36 37 38 39 40 41
static inline void
set_inaddr(struct in_addr *ia, ip_addr a)
{
  ipa_hton(a);
  memcpy(&ia->s_addr, &a, sizeof(a));
}

42 43 44 45 46 47 48
static inline void
get_inaddr(ip_addr *a, struct in_addr *ia)
{
  memcpy(a, &ia->s_addr, sizeof(*a));
  ipa_ntoh(*a);
}

49 50 51 52 53 54 55 56 57 58 59

#ifndef HAVE_STRUCT_IP_MREQN
/* Several versions of glibc don't define this structure, so we have to do it ourselves */
struct ip_mreqn
{
	struct in_addr	imr_multiaddr;		/* IP multicast address of group */
	struct in_addr	imr_address;		/* local IP address of interface */
	int		imr_ifindex;		/* Interface index */
};
#endif

60

61
static inline void fill_mreqn(struct ip_mreqn *m, ip_addr maddr, struct iface *ifa)
62
{
63 64 65
  bzero(m, sizeof(*m));
  m->imr_ifindex = ifa->index;
  set_inaddr(&m->imr_multiaddr, maddr);
66 67
}

68 69
static inline char *
sysio_setup_multicast(sock *s)
70
{
71
  struct ip_mreqn m;
72
  int zero = 0;
73

74 75 76 77 78 79 80
  if (setsockopt(s->fd, SOL_IP, IP_MULTICAST_LOOP, &zero, sizeof(zero)) < 0)
    return "IP_MULTICAST_LOOP";

  if (setsockopt(s->fd, SOL_IP, IP_MULTICAST_TTL, &s->ttl, sizeof(s->ttl)) < 0)
    return "IP_MULTICAST_TTL";

  /* This defines where should we send _outgoing_ multicasts */
81
  fill_mreqn(&m, IPA_NONE, s->iface);
82 83 84 85 86 87 88 89 90
  if (setsockopt(s->fd, SOL_IP, IP_MULTICAST_IF, &m, sizeof(m)) < 0)
    return "IP_MULTICAST_IF";

  return NULL;
}

static inline char *
sysio_join_group(sock *s, ip_addr maddr)
{
91
  struct ip_mreqn m;
92

93
  /* And this one sets interface for _receiving_ multicasts from */
94
  fill_mreqn(&m, maddr, s->iface);
95
  if (setsockopt(s->fd, SOL_IP, IP_ADD_MEMBERSHIP, &m, sizeof(m)) < 0)
96
    return "IP_ADD_MEMBERSHIP";
97 98 99 100 101 102 103

  return NULL;
}

static inline char *
sysio_leave_group(sock *s, ip_addr maddr)
{
104
  struct ip_mreqn m;
105 106

  /* And this one sets interface for _receiving_ multicasts from */
107
  fill_mreqn(&m, maddr, s->iface);
108 109 110
  if (setsockopt(s->fd, SOL_IP, IP_DROP_MEMBERSHIP, &m, sizeof(m)) < 0)
    return "IP_DROP_MEMBERSHIP";

111 112 113 114
  return NULL;
}

#endif
115

116

117
/* For the case that we have older libc headers */
118 119 120 121 122 123 124 125
/* Copied from Linux kernel file include/linux/tcp.h */

#ifndef TCP_MD5SIG

#define TCP_MD5SIG  14
#define TCP_MD5SIG_MAXKEYLEN 80

struct tcp_md5sig {
126
  struct  sockaddr_storage tcpm_addr;             /* address associated */
127 128 129 130 131 132 133
  __u16   __tcpm_pad1;                            /* zero */
  __u16   tcpm_keylen;                            /* key length */
  __u32   __tcpm_pad2;                            /* zero */
  __u8    tcpm_key[TCP_MD5SIG_MAXKEYLEN];         /* key (binary) */
};

#endif
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156

static int
sk_set_md5_auth_int(sock *s, sockaddr *sa, char *passwd)
{
  struct tcp_md5sig md5;

  memset(&md5, 0, sizeof(md5));
  memcpy(&md5.tcpm_addr, (struct sockaddr *) sa, sizeof(*sa));

  if (passwd)
    {
      int len = strlen(passwd);

      if (len > TCP_MD5SIG_MAXKEYLEN)
	{
	  log(L_ERR "MD5 password too long");
	  return -1;
	}

      md5.tcpm_keylen = len;
      memcpy(&md5.tcpm_key, passwd, len);
    }

157
  int rv = setsockopt(s->fd, SOL_TCP, TCP_MD5SIG, &md5, sizeof(md5));
158 159 160 161 162 163 164 165 166 167 168

  if (rv < 0) 
    {
      if (errno == ENOPROTOOPT)
	log(L_ERR "Kernel does not support TCP MD5 signatures");
      else
	log(L_ERR "sk_set_md5_auth_int: setsockopt: %m");
    }

  return rv;
}
169 170 171 172 173 174 175


#ifndef IPV6

/* RX/TX packet info handling for IPv4 */
/* Mostly similar to standardized IPv6 code */

176
#define CMSG_RX_SPACE (CMSG_SPACE(sizeof(struct in_pktinfo)) + CMSG_SPACE(sizeof(int)))
177 178 179 180 181 182
#define CMSG_TX_SPACE CMSG_SPACE(sizeof(struct in_pktinfo))

static char *
sysio_register_cmsgs(sock *s)
{
  int ok = 1;
183

184
  if ((s->flags & SKF_LADDR_RX) &&
185
      (setsockopt(s->fd, SOL_IP, IP_PKTINFO, &ok, sizeof(ok)) < 0))
186 187
    return "IP_PKTINFO";

188
  if ((s->flags & SKF_TTL_RX) &&
189
      (setsockopt(s->fd, SOL_IP, IP_RECVTTL, &ok, sizeof(ok)) < 0))
190 191
    return "IP_RECVTTL";

192 193 194 195 196 197 198 199
  return NULL;
}

static void
sysio_process_rx_cmsgs(sock *s, struct msghdr *msg)
{
  struct cmsghdr *cm;
  struct in_pktinfo *pi = NULL;
200
  int *ttl = NULL;
201 202

  for (cm = CMSG_FIRSTHDR(msg); cm != NULL; cm = CMSG_NXTHDR(msg, cm))
203
  {
204
    if (cm->cmsg_level == SOL_IP && cm->cmsg_type == IP_PKTINFO)
205 206
      pi = (struct in_pktinfo *) CMSG_DATA(cm);

207
    if (cm->cmsg_level == SOL_IP && cm->cmsg_type == IP_TTL)
208 209 210 211 212 213
      ttl = (int *) CMSG_DATA(cm);
  }

  if (s->flags & SKF_LADDR_RX)
  {
    if (pi)
214
    {
215 216
      get_inaddr(&s->laddr, &pi->ipi_addr);
      s->lifindex = pi->ipi_ifindex;
217
    }
218
    else
219 220 221 222
    {
      s->laddr = IPA_NONE;
      s->lifindex = 0;
    }
223 224 225 226
  }

  if (s->flags & SKF_TTL_RX)
    s->ttl = ttl ? *ttl : -1;
227 228 229 230

  return;
}

231 232
static void
sysio_prepare_tx_cmsgs(sock *s, struct msghdr *msg, void *cbuf, size_t cbuflen)
233 234 235 236
{
  struct cmsghdr *cm;
  struct in_pktinfo *pi;

237 238
  msg->msg_control = cbuf;
  msg->msg_controllen = cbuflen;
239 240

  cm = CMSG_FIRSTHDR(msg);
241
  cm->cmsg_level = SOL_IP;
242 243 244 245 246
  cm->cmsg_type = IP_PKTINFO;
  cm->cmsg_len = CMSG_LEN(sizeof(*pi));

  pi = (struct in_pktinfo *) CMSG_DATA(cm);
  pi->ipi_ifindex = s->iface ? s->iface->index : 0;
247 248
  set_inaddr(&pi->ipi_spec_dst, s->saddr);
  set_inaddr(&pi->ipi_addr, IPA_NONE);
249 250 251

  msg->msg_controllen = cm->cmsg_len;
}
252

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

254
#endif
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270


#ifndef IP_MINTTL
#define IP_MINTTL 21
#endif

#ifndef IPV6_MINHOPCOUNT
#define IPV6_MINHOPCOUNT 73
#endif


#ifndef IPV6

static int
sk_set_min_ttl4(sock *s, int ttl)
{
271
  if (setsockopt(s->fd, SOL_IP, IP_MINTTL, &ttl, sizeof(ttl)) < 0)
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
  {
    if (errno == ENOPROTOOPT)
      log(L_ERR "Kernel does not support IPv4 TTL security");
    else
      log(L_ERR "sk_set_min_ttl4: setsockopt: %m");

    return -1;
  }

  return 0;
}

#else

static int
sk_set_min_ttl6(sock *s, int ttl)
{
289
  if (setsockopt(s->fd, SOL_IPV6, IPV6_MINHOPCOUNT, &ttl, sizeof(ttl)) < 0)
290 291 292 293
  {
    if (errno == ENOPROTOOPT)
      log(L_ERR "Kernel does not support IPv6 TTL security");
    else
Ondřej Zajíček's avatar
Ondřej Zajíček committed
294
      log(L_ERR "sk_set_min_ttl6: setsockopt: %m");
295 296 297 298 299 300 301 302

    return -1;
  }

  return 0;
}

#endif
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321


#ifndef IPV6_TCLASS
#define IPV6_TCLASS 67
#endif

int sk_priority_control = 7;

static int
sk_set_priority(sock *s, int prio)
{
  if (setsockopt(s->fd, SOL_SOCKET, SO_PRIORITY, &prio, sizeof(prio)) < 0)
  {
    log(L_WARN "sk_set_priority: setsockopt: %m");
    return -1;
  }

  return 0;
}