iface.c revision 52396
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 52396 1999-10-19 15:21:09Z 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  addrs = n = how == IFACE_CLEAR_ALL ? 0 : 1;
255  for (; n < iface->in_addrs; n++)
256    iface_addr_Zap(iface->name, iface->in_addr + n);
257
258  iface->in_addrs = addrs;
259  /* Don't bother realloc()ing - we have little to gain */
260}
261
262int
263iface_inAdd(struct iface *iface, struct in_addr ifa, struct in_addr mask,
264            struct in_addr brd, int how)
265{
266  int slot, s, chg;
267  struct ifaliasreq ifra;
268  struct sockaddr_in *me, *peer, *msk;
269  struct iface_addr *addr;
270
271  for (slot = 0; slot < iface->in_addrs; slot++)
272    if (iface->in_addr[slot].ifa.s_addr == ifa.s_addr) {
273      if (how & IFACE_FORCE_ADD)
274        break;
275      else
276        /* errno = EEXIST; */
277        return 0;
278    }
279
280  addr = (struct iface_addr *)realloc
281    (iface->in_addr, (iface->in_addrs + 1) * sizeof iface->in_addr[0]);
282  if (addr == NULL) {
283    log_Printf(LogERROR, "iface_inAdd: realloc: %s\n", strerror(errno));
284    return 0;
285  }
286  iface->in_addr = addr;
287
288  s = ID0socket(AF_INET, SOCK_DGRAM, 0);
289  if (s < 0) {
290    log_Printf(LogERROR, "iface_inAdd: socket(): %s\n", strerror(errno));
291    return 0;
292  }
293
294  /*
295   * We've gotta be careful here.  If we try to add an address with the
296   * same destination as an existing interface, nothing will work.
297   * Instead, we tweak all previous address entries that match the
298   * to-be-added destination to 255.255.255.255 (w/ a similar netmask).
299   * There *may* be more than one - if the user has ``iface add''ed
300   * stuff previously.
301   */
302  for (chg = 0; chg < iface->in_addrs; chg++) {
303    if ((iface->in_addr[chg].brd.s_addr == brd.s_addr &&
304         brd.s_addr != INADDR_BROADCAST) || chg == slot) {
305      memset(&ifra, '\0', sizeof ifra);
306      strncpy(ifra.ifra_name, iface->name, sizeof ifra.ifra_name - 1);
307      me = (struct sockaddr_in *)&ifra.ifra_addr;
308      msk = (struct sockaddr_in *)&ifra.ifra_mask;
309      peer = (struct sockaddr_in *)&ifra.ifra_broadaddr;
310      me->sin_family = msk->sin_family = peer->sin_family = AF_INET;
311      me->sin_len = msk->sin_len = peer->sin_len = sizeof(struct sockaddr_in);
312      me->sin_addr = iface->in_addr[chg].ifa;
313      msk->sin_addr = iface->in_addr[chg].mask;
314      peer->sin_addr = iface->in_addr[chg].brd;
315      log_Printf(LogDEBUG, "Delete %s\n", inet_ntoa(me->sin_addr));
316      ID0ioctl(s, SIOCDIFADDR, &ifra);	/* Don't care if it fails... */
317      if (chg != slot) {
318        peer->sin_addr.s_addr = iface->in_addr[chg].brd.s_addr =
319          msk->sin_addr.s_addr = iface->in_addr[chg].mask.s_addr =
320            INADDR_BROADCAST;
321        iface->in_addr[chg].bits = 32;
322        log_Printf(LogDEBUG, "Add %s -> 255.255.255.255\n",
323                   inet_ntoa(me->sin_addr));
324        if (ID0ioctl(s, SIOCAIFADDR, &ifra) < 0 && errno != EEXIST) {
325          /* Oops - that's bad(ish) news !  We've lost an alias ! */
326          log_Printf(LogERROR, "iface_inAdd: ioctl(SIOCAIFADDR): %s: %s\n",
327               inet_ntoa(me->sin_addr), strerror(errno));
328          iface->in_addrs--;
329          bcopy(iface->in_addr + chg + 1, iface->in_addr + chg,
330                (iface->in_addrs - chg) * sizeof iface->in_addr[0]);
331          if (slot > chg)
332            slot--;
333          chg--;
334        }
335      }
336    }
337  }
338
339  memset(&ifra, '\0', sizeof ifra);
340  strncpy(ifra.ifra_name, iface->name, sizeof ifra.ifra_name - 1);
341  me = (struct sockaddr_in *)&ifra.ifra_addr;
342  msk = (struct sockaddr_in *)&ifra.ifra_mask;
343  peer = (struct sockaddr_in *)&ifra.ifra_broadaddr;
344  me->sin_family = msk->sin_family = peer->sin_family = AF_INET;
345  me->sin_len = msk->sin_len = peer->sin_len = sizeof(struct sockaddr_in);
346  me->sin_addr = ifa;
347  msk->sin_addr = mask;
348  peer->sin_addr = brd;
349
350  if (log_IsKept(LogDEBUG)) {
351    char buf[16];
352
353    strncpy(buf, inet_ntoa(brd), sizeof buf-1);
354    buf[sizeof buf - 1] = '\0';
355    log_Printf(LogDEBUG, "Add %s -> %s\n", inet_ntoa(ifa), buf);
356  }
357
358  /* An EEXIST failure w/ brd == INADDR_BROADCAST is ok (and works!) */
359  if (ID0ioctl(s, SIOCAIFADDR, &ifra) < 0 &&
360      (brd.s_addr != INADDR_BROADCAST || errno != EEXIST)) {
361    log_Printf(LogERROR, "iface_inAdd: ioctl(SIOCAIFADDR): %s: %s\n",
362               inet_ntoa(ifa), strerror(errno));
363    ID0ioctl(s, SIOCDIFADDR, &ifra);	/* EEXIST ? */
364    close(s);
365    return 0;
366  }
367  close(s);
368
369  if (slot == iface->in_addrs) {
370    /* We're adding a new interface address */
371
372    if (how & IFACE_ADD_FIRST) {
373      /* Stuff it at the start of our list */
374      slot = 0;
375      bcopy(iface->in_addr, iface->in_addr + 1,
376            iface->in_addrs * sizeof iface->in_addr[0]);
377    }
378
379    iface->in_addrs++;
380  } else if (how & IFACE_ADD_FIRST) {
381    /* Shift it up to the first slot */
382    bcopy(iface->in_addr, iface->in_addr + 1, slot * sizeof iface->in_addr[0]);
383    slot = 0;
384  }
385
386  iface->in_addr[slot].ifa = ifa;
387  iface->in_addr[slot].mask = mask;
388  iface->in_addr[slot].brd = brd;
389  iface->in_addr[slot].bits = bitsinmask(iface->in_addr[slot].mask);
390
391  return 1;
392}
393
394int
395iface_inDelete(struct iface *iface, struct in_addr ip)
396{
397  int n;
398
399  for (n = 0; n < iface->in_addrs; n++)
400    if (iface->in_addr[n].ifa.s_addr == ip.s_addr) {
401      iface_addr_Zap(iface->name, iface->in_addr + n);
402      bcopy(iface->in_addr + n + 1, iface->in_addr + n,
403            (iface->in_addrs - n - 1) * sizeof iface->in_addr[0]);
404      iface->in_addrs--;
405      return 1;
406    }
407
408  return 0;
409}
410
411#define IFACE_ADDFLAGS 1
412#define IFACE_DELFLAGS 2
413
414static int
415iface_ChangeFlags(struct iface *iface, int flags, int how)
416{
417  struct ifreq ifrq;
418  int s;
419
420  s = ID0socket(AF_INET, SOCK_DGRAM, 0);
421  if (s < 0) {
422    log_Printf(LogERROR, "iface_ChangeFlags: socket: %s\n", strerror(errno));
423    return 0;
424  }
425
426  memset(&ifrq, '\0', sizeof ifrq);
427  strncpy(ifrq.ifr_name, iface->name, sizeof ifrq.ifr_name - 1);
428  ifrq.ifr_name[sizeof ifrq.ifr_name - 1] = '\0';
429  if (ID0ioctl(s, SIOCGIFFLAGS, &ifrq) < 0) {
430    log_Printf(LogERROR, "iface_ChangeFlags: ioctl(SIOCGIFFLAGS): %s\n",
431       strerror(errno));
432    close(s);
433    return 0;
434  }
435
436  if (how == IFACE_ADDFLAGS)
437    ifrq.ifr_flags |= flags;
438  else
439    ifrq.ifr_flags &= ~flags;
440
441  if (ID0ioctl(s, SIOCSIFFLAGS, &ifrq) < 0) {
442    log_Printf(LogERROR, "iface_ChangeFlags: ioctl(SIOCSIFFLAGS): %s\n",
443       strerror(errno));
444    close(s);
445    return 0;
446  }
447  close(s);
448
449  return 1;	/* Success */
450}
451
452int
453iface_SetFlags(struct iface *iface, int flags)
454{
455  return iface_ChangeFlags(iface, flags, IFACE_ADDFLAGS);
456}
457
458int
459iface_ClearFlags(struct iface *iface, int flags)
460{
461  return iface_ChangeFlags(iface, flags, IFACE_DELFLAGS);
462}
463
464void
465iface_Destroy(struct iface *iface)
466{
467  /*
468   * iface_Clear(iface, IFACE_CLEAR_ALL) must be called manually
469   * if that's what the user wants.  It's better to leave the interface
470   * allocated so that existing connections can continue to work.
471   */
472
473  if (iface != NULL) {
474    free(iface->name);
475    free(iface->in_addr);
476    free(iface);
477  }
478}
479
480#define if_entry(x) { IFF_##x, #x }
481
482struct {
483  int flag;
484  const char *value;
485} if_flags[] = {
486  if_entry(UP),
487  if_entry(BROADCAST),
488  if_entry(DEBUG),
489  if_entry(LOOPBACK),
490  if_entry(POINTOPOINT),
491  if_entry(RUNNING),
492  if_entry(NOARP),
493  if_entry(PROMISC),
494  if_entry(ALLMULTI),
495  if_entry(OACTIVE),
496  if_entry(SIMPLEX),
497  if_entry(LINK0),
498  if_entry(LINK1),
499  if_entry(LINK2),
500  if_entry(MULTICAST),
501  { 0, "???" }
502};
503
504int
505iface_Show(struct cmdargs const *arg)
506{
507  struct iface *iface = arg->bundle->iface, *current;
508  int f, flags;
509
510  current = iface_Create(iface->name);
511  flags = iface->flags = current->flags;
512  iface_Destroy(current);
513
514  prompt_Printf(arg->prompt, "%s (idx %d) <", iface->name, iface->index);
515  for (f = 0; f < sizeof if_flags / sizeof if_flags[0]; f++)
516    if ((if_flags[f].flag & flags) || (!if_flags[f].flag && flags)) {
517      prompt_Printf(arg->prompt, "%s%s", flags == iface->flags ? "" : ",",
518                    if_flags[f].value);
519      flags &= ~if_flags[f].flag;
520    }
521  prompt_Printf(arg->prompt, "> has %d address%s:\n", iface->in_addrs,
522                iface->in_addrs == 1 ? "" : "es");
523
524  for (f = 0; f < iface->in_addrs; f++) {
525    prompt_Printf(arg->prompt, "  %s", inet_ntoa(iface->in_addr[f].ifa));
526    if (iface->in_addr[f].bits >= 0)
527      prompt_Printf(arg->prompt, "/%d", iface->in_addr[f].bits);
528    if (iface->flags & IFF_POINTOPOINT)
529      prompt_Printf(arg->prompt, " -> %s", inet_ntoa(iface->in_addr[f].brd));
530    else if (iface->flags & IFF_BROADCAST)
531      prompt_Printf(arg->prompt, " broadcast %s",
532                    inet_ntoa(iface->in_addr[f].brd));
533    if (iface->in_addr[f].bits < 0)
534      prompt_Printf(arg->prompt, " (mask %s)",
535                    inet_ntoa(iface->in_addr[f].mask));
536    prompt_Printf(arg->prompt, "\n");
537  }
538
539  return 0;
540}
541