iface.c revision 55531
1/*-
2 * Copyright (c) 1998 Brian Somers <brian@Awfulhak.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
26 * $FreeBSD: head/usr.sbin/ppp/iface.c 55531 2000-01-07 03:47:12Z brian $
27 */
28
29#include <sys/param.h>
30#include <sys/socket.h>
31#include <netinet/in.h>
32#include <net/if.h>
33#include <net/if_dl.h>
34#include <net/route.h>
35#include <arpa/inet.h>
36#include <netinet/in_systm.h>
37#include <netinet/ip.h>
38#include <sys/un.h>
39
40#include <errno.h>
41#include <string.h>
42#include <stdio.h>
43#include <stdlib.h>
44#include <sys/ioctl.h>
45#include <sys/sysctl.h>
46#include <termios.h>
47#include <unistd.h>
48
49#include "layer.h"
50#include "defs.h"
51#include "command.h"
52#include "mbuf.h"
53#include "log.h"
54#include "id.h"
55#include "timer.h"
56#include "fsm.h"
57#include "iplist.h"
58#include "lqr.h"
59#include "hdlc.h"
60#include "throughput.h"
61#include "slcompress.h"
62#include "descriptor.h"
63#include "ipcp.h"
64#include "filter.h"
65#include "lcp.h"
66#include "ccp.h"
67#include "link.h"
68#include "mp.h"
69#ifndef NORADIUS
70#include "radius.h"
71#endif
72#include "bundle.h"
73#include "prompt.h"
74#include "iface.h"
75
76
77static int
78bitsinmask(struct in_addr mask)
79{
80  u_int32_t bitmask, maskaddr;
81  int bits;
82
83  bitmask = 0xffffffff;
84  maskaddr = ntohl(mask.s_addr);
85  for (bits = 32; bits >= 0; bits--) {
86    if (maskaddr == bitmask)
87      break;
88    bitmask &= ~(1 << (32 - bits));
89  }
90
91  return bits;
92}
93
94struct iface *
95iface_Create(const char *name)
96{
97  int mib[6], i, s;
98  size_t needed;
99  char *buf, *ptr, *end, *cp, *lim;
100  struct if_msghdr *ifm;
101  struct ifa_msghdr *ifam;
102  struct sockaddr_dl *dl;
103  struct rt_addrinfo rti;
104  struct iface *iface;
105  struct iface_addr *addr;
106
107  s = socket(AF_INET, SOCK_DGRAM, 0);
108  if (s < 0) {
109    fprintf(stderr, "iface_Create: socket(): %s\n", strerror(errno));
110    return NULL;
111  }
112
113  mib[0] = CTL_NET;
114  mib[1] = PF_ROUTE;
115  mib[2] = 0;
116  mib[3] = 0;
117  mib[4] = NET_RT_IFLIST;
118  mib[5] = 0;
119
120  if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0) {
121    fprintf(stderr, "iface_Create: sysctl: estimate: %s\n",
122              strerror(errno));
123    close(s);
124    return NULL;
125  }
126
127  if ((buf = (char *)malloc(needed)) == NULL) {
128    fprintf(stderr, "iface_Create: malloc failed: %s\n", strerror(errno));
129    close(s);
130    return NULL;
131  }
132
133  if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) {
134    fprintf(stderr, "iface_Create: sysctl: %s\n", strerror(errno));
135    free(buf);
136    close(s);
137    return NULL;
138  }
139
140  ptr = buf;
141  end = buf + needed;
142  iface = NULL;
143
144  while (ptr < end && iface == NULL) {
145    ifm = (struct if_msghdr *)ptr;			/* On if_msghdr */
146    if (ifm->ifm_type != RTM_IFINFO)
147      break;
148    dl = (struct sockaddr_dl *)(ifm + 1);		/* Single _dl at end */
149    if (!strncmp(name, dl->sdl_data, dl->sdl_nlen)) {
150      iface = (struct iface *)malloc(sizeof *iface);
151      if (iface == NULL) {
152        fprintf(stderr, "iface_Create: malloc: %s\n", strerror(errno));
153        return NULL;
154      }
155      iface->name = strdup(name);
156      iface->flags = ifm->ifm_flags;
157      iface->index = ifm->ifm_index;
158      iface->in_addrs = 0;
159      iface->in_addr = NULL;
160    }
161    ptr += ifm->ifm_msglen;				/* First ifa_msghdr */
162    for (; ptr < end; ptr += ifam->ifam_msglen) {
163      ifam = (struct ifa_msghdr *)ptr;			/* Next if address */
164
165      if (ifam->ifam_type != RTM_NEWADDR)		/* finished this if */
166        break;
167
168      if (iface == NULL)				/* Keep wading */
169        continue;
170
171      /* Found an address ! */
172
173      if (ifam->ifam_addrs & (1 << RTAX_IFA)) {
174        /* *And* it's configured ! */
175        rti.rti_addrs = ifam->ifam_addrs;
176        lim = (char *)ifam + ifam->ifam_msglen;
177        cp = (char *)(ifam + 1);
178        memset(rti.rti_info, '\0', sizeof(rti.rti_info));
179        for (i = 0; i < RTAX_MAX && cp < lim; i++) {
180          if ((rti.rti_addrs & (1 << i)) == 0)
181            continue;
182          rti.rti_info[i] = (struct sockaddr *)cp;
183#define ROUNDUP(x) \
184          ((x) > 0 ? (1 + (((x) - 1) | (sizeof(long) - 1))) : sizeof(long))
185          cp += ROUNDUP(rti.rti_info[i]->sa_len);
186        }
187
188        if (rti.rti_info[RTAX_IFA] &&
189            rti.rti_info[RTAX_IFA]->sa_family == AF_INET) {
190          /* Record the iface address rti */
191
192          addr = (struct iface_addr *)realloc
193            (iface->in_addr, (iface->in_addrs + 1) * sizeof iface->in_addr[0]);
194          if (addr == NULL)
195            break;
196          iface->in_addr = addr;
197
198          addr += iface->in_addrs;
199          iface->in_addrs++;
200
201          addr->ifa.s_addr = ((struct sockaddr_in *)rti.rti_info[RTAX_IFA])->
202            sin_addr.s_addr;
203          addr->brd.s_addr = rti.rti_info[RTAX_BRD] ?
204            ((struct sockaddr_in *)rti.rti_info[RTAX_BRD])->sin_addr.s_addr :
205            INADDR_ANY;
206          addr->mask.s_addr = rti.rti_info[RTAX_NETMASK] ?
207            ((struct sockaddr_in *)rti.rti_info[RTAX_NETMASK])->sin_addr.s_addr:
208            INADDR_ANY;
209
210          addr->bits = bitsinmask(addr->mask);
211        }
212      }
213    }
214  }
215
216  free(buf);
217  close(s);
218
219  return iface;
220}
221
222static void
223iface_addr_Zap(const char *name, struct iface_addr *addr)
224{
225  struct ifaliasreq ifra;
226  struct sockaddr_in *me, *peer;
227  int s;
228
229  s = ID0socket(AF_INET, SOCK_DGRAM, 0);
230  if (s < 0)
231    log_Printf(LogERROR, "iface_addr_Zap: socket(): %s\n", strerror(errno));
232  else {
233    memset(&ifra, '\0', sizeof ifra);
234    strncpy(ifra.ifra_name, name, sizeof ifra.ifra_name - 1);
235    me = (struct sockaddr_in *)&ifra.ifra_addr;
236    peer = (struct sockaddr_in *)&ifra.ifra_broadaddr;
237    me->sin_family = peer->sin_family = AF_INET;
238    me->sin_len = peer->sin_len = sizeof(struct sockaddr_in);
239    me->sin_addr = addr->ifa;
240    peer->sin_addr = addr->brd;
241    log_Printf(LogDEBUG, "Delete %s\n", inet_ntoa(addr->ifa));
242    if (ID0ioctl(s, SIOCDIFADDR, &ifra) < 0)
243      log_Printf(LogWARN, "iface_addr_Zap: ioctl(SIOCDIFADDR, %s): %s\n",
244                 inet_ntoa(addr->ifa), strerror(errno));
245    close(s);
246  }
247}
248
249void
250iface_inClear(struct iface *iface, int how)
251{
252  int n, addrs;
253
254  if (iface->in_addrs) {
255    addrs = n = how == IFACE_CLEAR_ALL ? 0 : 1;
256    for (; n < iface->in_addrs; n++)
257      iface_addr_Zap(iface->name, iface->in_addr + n);
258
259    iface->in_addrs = addrs;
260    /* Don't bother realloc()ing - we have little to gain */
261  }
262}
263
264int
265iface_inAdd(struct iface *iface, struct in_addr ifa, struct in_addr mask,
266            struct in_addr brd, int how)
267{
268  int slot, s, chg;
269  struct ifaliasreq ifra;
270  struct sockaddr_in *me, *peer, *msk;
271  struct iface_addr *addr;
272
273  for (slot = 0; slot < iface->in_addrs; slot++)
274    if (iface->in_addr[slot].ifa.s_addr == ifa.s_addr) {
275      if (how & IFACE_FORCE_ADD)
276        break;
277      else
278        /* errno = EEXIST; */
279        return 0;
280    }
281
282  addr = (struct iface_addr *)realloc
283    (iface->in_addr, (iface->in_addrs + 1) * sizeof iface->in_addr[0]);
284  if (addr == NULL) {
285    log_Printf(LogERROR, "iface_inAdd: realloc: %s\n", strerror(errno));
286    return 0;
287  }
288  iface->in_addr = addr;
289
290  s = ID0socket(AF_INET, SOCK_DGRAM, 0);
291  if (s < 0) {
292    log_Printf(LogERROR, "iface_inAdd: socket(): %s\n", strerror(errno));
293    return 0;
294  }
295
296  /*
297   * We've gotta be careful here.  If we try to add an address with the
298   * same destination as an existing interface, nothing will work.
299   * Instead, we tweak all previous address entries that match the
300   * to-be-added destination to 255.255.255.255 (w/ a similar netmask).
301   * There *may* be more than one - if the user has ``iface add''ed
302   * stuff previously.
303   */
304  for (chg = 0; chg < iface->in_addrs; chg++) {
305    if ((iface->in_addr[chg].brd.s_addr == brd.s_addr &&
306         brd.s_addr != INADDR_BROADCAST) || chg == slot) {
307      memset(&ifra, '\0', sizeof ifra);
308      strncpy(ifra.ifra_name, iface->name, sizeof ifra.ifra_name - 1);
309      me = (struct sockaddr_in *)&ifra.ifra_addr;
310      msk = (struct sockaddr_in *)&ifra.ifra_mask;
311      peer = (struct sockaddr_in *)&ifra.ifra_broadaddr;
312      me->sin_family = msk->sin_family = peer->sin_family = AF_INET;
313      me->sin_len = msk->sin_len = peer->sin_len = sizeof(struct sockaddr_in);
314      me->sin_addr = iface->in_addr[chg].ifa;
315      msk->sin_addr = iface->in_addr[chg].mask;
316      peer->sin_addr = iface->in_addr[chg].brd;
317      log_Printf(LogDEBUG, "Delete %s\n", inet_ntoa(me->sin_addr));
318      ID0ioctl(s, SIOCDIFADDR, &ifra);	/* Don't care if it fails... */
319      if (chg != slot) {
320        peer->sin_addr.s_addr = iface->in_addr[chg].brd.s_addr =
321          msk->sin_addr.s_addr = iface->in_addr[chg].mask.s_addr =
322            INADDR_BROADCAST;
323        iface->in_addr[chg].bits = 32;
324        log_Printf(LogDEBUG, "Add %s -> 255.255.255.255\n",
325                   inet_ntoa(me->sin_addr));
326        if (ID0ioctl(s, SIOCAIFADDR, &ifra) < 0 && errno != EEXIST) {
327          /* Oops - that's bad(ish) news !  We've lost an alias ! */
328          log_Printf(LogERROR, "iface_inAdd: ioctl(SIOCAIFADDR): %s: %s\n",
329               inet_ntoa(me->sin_addr), strerror(errno));
330          iface->in_addrs--;
331          bcopy(iface->in_addr + chg + 1, iface->in_addr + chg,
332                (iface->in_addrs - chg) * sizeof iface->in_addr[0]);
333          if (slot > chg)
334            slot--;
335          chg--;
336        }
337      }
338    }
339  }
340
341  memset(&ifra, '\0', sizeof ifra);
342  strncpy(ifra.ifra_name, iface->name, sizeof ifra.ifra_name - 1);
343  me = (struct sockaddr_in *)&ifra.ifra_addr;
344  msk = (struct sockaddr_in *)&ifra.ifra_mask;
345  peer = (struct sockaddr_in *)&ifra.ifra_broadaddr;
346  me->sin_family = msk->sin_family = peer->sin_family = AF_INET;
347  me->sin_len = msk->sin_len = peer->sin_len = sizeof(struct sockaddr_in);
348  me->sin_addr = ifa;
349  msk->sin_addr = mask;
350  peer->sin_addr = brd;
351
352  if (log_IsKept(LogDEBUG)) {
353    char buf[16];
354
355    strncpy(buf, inet_ntoa(brd), sizeof buf-1);
356    buf[sizeof buf - 1] = '\0';
357    log_Printf(LogDEBUG, "Add %s -> %s\n", inet_ntoa(ifa), buf);
358  }
359
360  /* An EEXIST failure w/ brd == INADDR_BROADCAST is ok (and works!) */
361  if (ID0ioctl(s, SIOCAIFADDR, &ifra) < 0 &&
362      (brd.s_addr != INADDR_BROADCAST || errno != EEXIST)) {
363    log_Printf(LogERROR, "iface_inAdd: ioctl(SIOCAIFADDR): %s: %s\n",
364               inet_ntoa(ifa), strerror(errno));
365    ID0ioctl(s, SIOCDIFADDR, &ifra);	/* EEXIST ? */
366    close(s);
367    return 0;
368  }
369  close(s);
370
371  if (slot == iface->in_addrs) {
372    /* We're adding a new interface address */
373
374    if (how & IFACE_ADD_FIRST) {
375      /* Stuff it at the start of our list */
376      slot = 0;
377      bcopy(iface->in_addr, iface->in_addr + 1,
378            iface->in_addrs * sizeof iface->in_addr[0]);
379    }
380
381    iface->in_addrs++;
382  } else if (how & IFACE_ADD_FIRST) {
383    /* Shift it up to the first slot */
384    bcopy(iface->in_addr, iface->in_addr + 1, slot * sizeof iface->in_addr[0]);
385    slot = 0;
386  }
387
388  iface->in_addr[slot].ifa = ifa;
389  iface->in_addr[slot].mask = mask;
390  iface->in_addr[slot].brd = brd;
391  iface->in_addr[slot].bits = bitsinmask(iface->in_addr[slot].mask);
392
393  return 1;
394}
395
396int
397iface_inDelete(struct iface *iface, struct in_addr ip)
398{
399  int n;
400
401  for (n = 0; n < iface->in_addrs; n++)
402    if (iface->in_addr[n].ifa.s_addr == ip.s_addr) {
403      iface_addr_Zap(iface->name, iface->in_addr + n);
404      bcopy(iface->in_addr + n + 1, iface->in_addr + n,
405            (iface->in_addrs - n - 1) * sizeof iface->in_addr[0]);
406      iface->in_addrs--;
407      return 1;
408    }
409
410  return 0;
411}
412
413#define IFACE_ADDFLAGS 1
414#define IFACE_DELFLAGS 2
415
416static int
417iface_ChangeFlags(struct iface *iface, int flags, int how)
418{
419  struct ifreq ifrq;
420  int s;
421
422  s = ID0socket(AF_INET, SOCK_DGRAM, 0);
423  if (s < 0) {
424    log_Printf(LogERROR, "iface_ChangeFlags: socket: %s\n", strerror(errno));
425    return 0;
426  }
427
428  memset(&ifrq, '\0', sizeof ifrq);
429  strncpy(ifrq.ifr_name, iface->name, sizeof ifrq.ifr_name - 1);
430  ifrq.ifr_name[sizeof ifrq.ifr_name - 1] = '\0';
431  if (ID0ioctl(s, SIOCGIFFLAGS, &ifrq) < 0) {
432    log_Printf(LogERROR, "iface_ChangeFlags: ioctl(SIOCGIFFLAGS): %s\n",
433       strerror(errno));
434    close(s);
435    return 0;
436  }
437
438  if (how == IFACE_ADDFLAGS)
439    ifrq.ifr_flags |= flags;
440  else
441    ifrq.ifr_flags &= ~flags;
442
443  if (ID0ioctl(s, SIOCSIFFLAGS, &ifrq) < 0) {
444    log_Printf(LogERROR, "iface_ChangeFlags: ioctl(SIOCSIFFLAGS): %s\n",
445       strerror(errno));
446    close(s);
447    return 0;
448  }
449  close(s);
450
451  return 1;	/* Success */
452}
453
454int
455iface_SetFlags(struct iface *iface, int flags)
456{
457  return iface_ChangeFlags(iface, flags, IFACE_ADDFLAGS);
458}
459
460int
461iface_ClearFlags(struct iface *iface, int flags)
462{
463  return iface_ChangeFlags(iface, flags, IFACE_DELFLAGS);
464}
465
466void
467iface_Destroy(struct iface *iface)
468{
469  /*
470   * iface_Clear(iface, IFACE_CLEAR_ALL) must be called manually
471   * if that's what the user wants.  It's better to leave the interface
472   * allocated so that existing connections can continue to work.
473   */
474
475  if (iface != NULL) {
476    free(iface->name);
477    free(iface->in_addr);
478    free(iface);
479  }
480}
481
482#define if_entry(x) { IFF_##x, #x }
483
484struct {
485  int flag;
486  const char *value;
487} if_flags[] = {
488  if_entry(UP),
489  if_entry(BROADCAST),
490  if_entry(DEBUG),
491  if_entry(LOOPBACK),
492  if_entry(POINTOPOINT),
493  if_entry(RUNNING),
494  if_entry(NOARP),
495  if_entry(PROMISC),
496  if_entry(ALLMULTI),
497  if_entry(OACTIVE),
498  if_entry(SIMPLEX),
499  if_entry(LINK0),
500  if_entry(LINK1),
501  if_entry(LINK2),
502  if_entry(MULTICAST),
503  { 0, "???" }
504};
505
506int
507iface_Show(struct cmdargs const *arg)
508{
509  struct iface *iface = arg->bundle->iface, *current;
510  int f, flags;
511
512  current = iface_Create(iface->name);
513  flags = iface->flags = current->flags;
514  iface_Destroy(current);
515
516  prompt_Printf(arg->prompt, "%s (idx %d) <", iface->name, iface->index);
517  for (f = 0; f < sizeof if_flags / sizeof if_flags[0]; f++)
518    if ((if_flags[f].flag & flags) || (!if_flags[f].flag && flags)) {
519      prompt_Printf(arg->prompt, "%s%s", flags == iface->flags ? "" : ",",
520                    if_flags[f].value);
521      flags &= ~if_flags[f].flag;
522    }
523  prompt_Printf(arg->prompt, "> has %d address%s:\n", iface->in_addrs,
524                iface->in_addrs == 1 ? "" : "es");
525
526  for (f = 0; f < iface->in_addrs; f++) {
527    prompt_Printf(arg->prompt, "  %s", inet_ntoa(iface->in_addr[f].ifa));
528    if (iface->in_addr[f].bits >= 0)
529      prompt_Printf(arg->prompt, "/%d", iface->in_addr[f].bits);
530    if (iface->flags & IFF_POINTOPOINT)
531      prompt_Printf(arg->prompt, " -> %s", inet_ntoa(iface->in_addr[f].brd));
532    else if (iface->flags & IFF_BROADCAST)
533      prompt_Printf(arg->prompt, " broadcast %s",
534                    inet_ntoa(iface->in_addr[f].brd));
535    if (iface->in_addr[f].bits < 0)
536      prompt_Printf(arg->prompt, " (mask %s)",
537                    inet_ntoa(iface->in_addr[f].mask));
538    prompt_Printf(arg->prompt, "\n");
539  }
540
541  return 0;
542}
543