Statistics
| Branch: | Revision:

iof-bird-daemon / sysdep / unix / krt-iface.c @ b1a1faba

History | View | Annotate | Download (5.13 KB)

1
/*
2
 *        BIRD -- Unix Interface Scanning and Syncing
3
 *
4
 *        (c) 1998--2000 Martin Mares <mj@ucw.cz>
5
 *      (c) 2004       Ondrej Filip <feela@network.cz>
6
 *
7
 *        Can be freely distributed and used under the terms of the GNU GPL.
8
 */
9

    
10
#include <sys/types.h>
11
#include <sys/socket.h>
12
#include <netinet/in.h>
13
#include <net/if.h>
14
#include <sys/ioctl.h>
15
#include <errno.h>
16

    
17
#undef LOCAL_DEBUG
18

    
19
#include "nest/bird.h"
20
#include "nest/iface.h"
21
#include "nest/route.h"
22
#include "nest/protocol.h"
23
#include "lib/timer.h"
24
#include "lib/krt.h"
25
#include "lib/string.h"
26

    
27
#include "unix.h"
28

    
29
int if_scan_sock = -1;
30

    
31
static void
32
scan_ifs(struct ifreq *r, int cnt)
33
{
34
  struct iface i, *pi;
35
  struct ifa a;
36
  char *err, *colon;
37
  unsigned fl;
38
  ip_addr netmask;
39
  int l, scope;
40
  sockaddr *sa;
41

    
42
  if_start_update();
43
  for (cnt /= sizeof(struct ifreq); cnt; cnt--, r++)
44
    {
45
      int sec = 0;
46
      bzero(&i, sizeof(i));
47
      bzero(&a, sizeof(a));
48
      if (colon = strchr(r->ifr_name, ':'))
49
        {
50
          /* It's an alias -- let's interpret it as a secondary interface address */
51
          sec = 1;
52
          *colon = 0;
53
        }
54
      strncpy(i.name, r->ifr_name, sizeof(i.name) - 1);
55

    
56
      if(ioctl(if_scan_sock, SIOCGIFADDR,r)<0) continue;
57

    
58
      get_sockaddr((struct sockaddr_in *) &r->ifr_addr, &a.ip, NULL, 1);
59
      if (ipa_nonzero(a.ip))
60
        {
61
          l = ipa_classify(a.ip);
62
          if (l < 0 || !(l & IADDR_HOST))
63
            {
64
              log(L_ERR "%s: Invalid interface address", i.name);
65
              a.ip = IPA_NONE;
66
            }
67
          else
68
            {
69
              a.scope = l & IADDR_SCOPE_MASK;
70
              if (a.scope == SCOPE_HOST)
71
                i.flags |= IF_LOOPBACK | IF_IGNORE;
72
            }
73
        }
74

    
75
      if (ioctl(if_scan_sock, SIOCGIFFLAGS, r) < 0)
76
        {
77
          err = "SIOCGIFFLAGS";
78
        faulty:
79
          log(L_ERR "%s(%s): %m", err, i.name);
80
        bad:
81
          i.flags = (i.flags & ~IF_LINK_UP) | IF_ADMIN_DOWN;
82
          continue;
83
        }
84
      fl = r->ifr_flags;
85
      if (fl & IFF_UP)
86
        i.flags |= IF_LINK_UP;
87

    
88
      if (ioctl(if_scan_sock, SIOCGIFNETMASK, r) < 0)
89
        { err = "SIOCGIFNETMASK"; goto faulty; }
90
      get_sockaddr((struct sockaddr_in *) &r->ifr_addr, &netmask, NULL, 0);
91
      l = ipa_mklen(netmask);
92
      if (l < 0 || l == 31)
93
        {
94
          log(L_ERR "%s: Invalid netmask (%x)", i.name, netmask);
95
          goto bad;
96
        }
97
      a.pxlen = l;
98

    
99
      if (fl & IFF_POINTOPOINT)
100
        {
101
          a.flags |= IA_UNNUMBERED;
102
          if (ioctl(if_scan_sock, SIOCGIFDSTADDR, r) < 0)
103
            { err = "SIOCGIFDSTADDR"; goto faulty; }
104
          get_sockaddr((struct sockaddr_in *) &r->ifr_addr, &a.opposite, NULL, 1);
105
          a.prefix = a.opposite;
106
          a.pxlen = BITS_PER_IP_ADDRESS;
107
        }
108
      else
109
        a.prefix = ipa_and(a.ip, ipa_mkmask(a.pxlen));
110
      if (fl & IFF_LOOPBACK)
111
        i.flags |= IF_LOOPBACK | IF_IGNORE;
112
      if (1
113
#ifndef CONFIG_ALL_MULTICAST
114
          && (fl & IFF_MULTICAST)
115
#endif
116
#ifndef CONFIG_UNNUM_MULTICAST
117
          && !(a.flags & IA_UNNUMBERED)
118
#endif
119
         )
120
        i.flags |= IF_MULTICAST;
121

    
122
      scope = ipa_classify(a.ip);
123
      if (scope < 0)
124
        {
125
          log(L_ERR "%s: Invalid address", i.name);
126
          goto bad;
127
        }
128
      a.scope = scope & IADDR_SCOPE_MASK;
129

    
130
      if (a.pxlen < 32)
131
        {
132
          a.brd = ipa_or(a.prefix, ipa_not(ipa_mkmask(a.pxlen)));
133
          if (ipa_equal(a.ip, a.prefix) || ipa_equal(a.ip, a.brd))
134
            {
135
              log(L_ERR "%s: Using network or broadcast address for interface", i.name);
136
              goto bad;
137
            }
138
          if (fl & IFF_BROADCAST)
139
            i.flags |= IF_BROADCAST;
140
          if (a.pxlen < 30)
141
            i.flags |= IF_MULTIACCESS;
142
          else
143
            a.opposite = ipa_opposite(a.ip);
144
        }
145
      else
146
        a.brd = a.opposite;
147
      a.scope = SCOPE_UNIVERSE;
148

    
149
      if (ioctl(if_scan_sock, SIOCGIFMTU, r) < 0)
150
        { err = "SIOCGIFMTU"; goto faulty; }
151
      i.mtu = r->ifr_mtu;
152

    
153
#ifdef SIOCGIFINDEX
154
      if (ioctl(if_scan_sock, SIOCGIFINDEX, r) >= 0)
155
        i.index = r->ifr_ifindex;
156
      else if (errno != -EINVAL)
157
        DBG("SIOCGIFINDEX failed: %m\n");
158
      else        /* defined, but not supported by the kernel */
159
#endif
160
      /*
161
       *  The kernel doesn't give us real ifindices, but we still need them
162
       *  at least for OSPF unnumbered links. So let's make them up ourselves.
163
       */
164
      if (pi = if_find_by_name(i.name))
165
        i.index = pi->index;
166
      else
167
        {
168
          static int if_index_counter = 1;
169
          i.index = if_index_counter++;
170
        }
171

    
172
      pi = NULL;
173
      if (sec)
174
        {
175
          a.flags |= IA_SECONDARY;
176
          pi = if_find_by_index(i.index);
177
        }
178
      if (!pi)
179
        pi = if_update(&i);
180
      a.iface = pi;
181
      ifa_update(&a);
182
    }
183
  if_end_update();
184
}
185

    
186
void
187
krt_if_scan(struct kif_proto *p)
188
{
189
  struct ifconf ic;
190
  static int last_ifbuf_size = 4*sizeof(struct ifreq);
191
  int res;
192

    
193
  for(;;)
194
    {
195
      ic.ifc_buf = alloca(last_ifbuf_size);
196
      ic.ifc_len = last_ifbuf_size;
197
      res = ioctl(if_scan_sock, SIOCGIFCONF, &ic);
198
      if (res < 0 && errno != EFAULT)
199
        die("SIOCCGIFCONF: %m");
200
      if (res >= 0 && ic.ifc_len <= last_ifbuf_size)
201
        break;
202
      last_ifbuf_size *= 2;
203
      DBG("Increased ifconf buffer size to %d\n", last_ifbuf_size);
204
    }
205
  scan_ifs(ic.ifc_req, ic.ifc_len);
206
}
207

    
208
void
209
krt_if_construct(struct kif_config *c)
210
{
211
}
212

    
213
void
214
krt_if_start(struct kif_proto *p)
215
{
216
}
217

    
218
void
219
krt_if_shutdown(struct kif_proto *p)
220
{
221
}
222

    
223
void
224
krt_if_io_init(void)
225
{
226
  if_scan_sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
227
  DBG("Using socket %d for interface and route scanning\n", if_scan_sock);
228
  if (if_scan_sock < 0)
229
    die("Cannot create scanning socket: %m");
230
}
231