route.c revision 37210
1/*
2 *	      PPP Routing related Module
3 *
4 *	    Written by Toshiharu OHNO (tony-o@iij.ad.jp)
5 *
6 *   Copyright (C) 1994, Internet Initiative Japan, Inc. All rights reserverd.
7 *
8 * Redistribution and use in source and binary forms are permitted
9 * provided that the above copyright notice and this paragraph are
10 * duplicated in all such forms and that any documentation,
11 * advertising materials, and other materials related to such
12 * distribution and use acknowledge that the software was developed
13 * by the Internet Initiative Japan, Inc.  The name of the
14 * IIJ may not be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
18 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
19 *
20 * $Id: route.c,v 1.50 1998/06/27 12:03:49 brian Exp $
21 *
22 */
23
24#include <sys/types.h>
25#include <sys/socket.h>
26#include <net/if_types.h>
27#include <net/route.h>
28#include <net/if.h>
29#include <netinet/in.h>
30#include <arpa/inet.h>
31#include <net/if_dl.h>
32#include <netinet/in_systm.h>
33#include <netinet/ip.h>
34#include <sys/un.h>
35
36#include <errno.h>
37#include <stdio.h>
38#include <stdlib.h>
39#include <string.h>
40#include <sys/sysctl.h>
41#include <termios.h>
42
43#include "defs.h"
44#include "command.h"
45#include "mbuf.h"
46#include "log.h"
47#include "iplist.h"
48#include "timer.h"
49#include "throughput.h"
50#include "lqr.h"
51#include "hdlc.h"
52#include "fsm.h"
53#include "lcp.h"
54#include "ccp.h"
55#include "link.h"
56#include "slcompress.h"
57#include "ipcp.h"
58#include "filter.h"
59#include "descriptor.h"
60#include "mp.h"
61#include "bundle.h"
62#include "route.h"
63#include "prompt.h"
64
65static void
66p_sockaddr(struct prompt *prompt, struct sockaddr *phost,
67           struct sockaddr *pmask, int width)
68{
69  char buf[29];
70  struct sockaddr_in *ihost = (struct sockaddr_in *)phost;
71  struct sockaddr_in *mask = (struct sockaddr_in *)pmask;
72  struct sockaddr_dl *dl = (struct sockaddr_dl *)phost;
73
74  switch (phost->sa_family) {
75  case AF_INET:
76    if (!phost)
77      buf[0] = '\0';
78    else if (ihost->sin_addr.s_addr == INADDR_ANY)
79      strcpy(buf, "default");
80    else if (!mask)
81      strcpy(buf, inet_ntoa(ihost->sin_addr));
82    else {
83      u_int32_t msk = ntohl(mask->sin_addr.s_addr);
84      u_int32_t tst;
85      int bits;
86      int len;
87      struct sockaddr_in net;
88
89      for (tst = 1, bits=32; tst; tst <<= 1, bits--)
90        if (msk & tst)
91          break;
92
93      for (tst <<= 1; tst; tst <<= 1)
94        if (!(msk & tst))
95          break;
96
97      net.sin_addr.s_addr = ihost->sin_addr.s_addr & mask->sin_addr.s_addr;
98      strcpy(buf, inet_ntoa(net.sin_addr));
99      for (len = strlen(buf); len > 3; buf[len -= 2] = '\0')
100        if (strcmp(buf + len - 2, ".0"))
101          break;
102
103      if (tst)    /* non-contiguous :-( */
104        sprintf(buf + strlen(buf),"&0x%08lx", (u_long)msk);
105      else
106        sprintf(buf + strlen(buf), "/%d", bits);
107    }
108    break;
109
110  case AF_LINK:
111    if (dl->sdl_nlen)
112      snprintf(buf, sizeof buf, "%.*s", dl->sdl_nlen, dl->sdl_data);
113    else if (dl->sdl_alen) {
114      if (dl->sdl_type == IFT_ETHER) {
115        if (dl->sdl_alen < sizeof buf / 3) {
116          int f;
117          u_char *MAC;
118
119          MAC = (u_char *)dl->sdl_data + dl->sdl_nlen;
120          for (f = 0; f < dl->sdl_alen; f++)
121            sprintf(buf+f*3, "%02x:", MAC[f]);
122          buf[f*3-1] = '\0';
123        } else
124	  strcpy(buf, "??:??:??:??:??:??");
125      } else
126        sprintf(buf, "<IFT type %d>", dl->sdl_type);
127    }  else if (dl->sdl_slen)
128      sprintf(buf, "<slen %d?>", dl->sdl_slen);
129    else
130      sprintf(buf, "link#%d", dl->sdl_index);
131    break;
132
133  default:
134    sprintf(buf, "<AF type %d>", phost->sa_family);
135    break;
136  }
137
138  prompt_Printf(prompt, "%-*s ", width-1, buf);
139}
140
141static struct bits {
142  u_int32_t b_mask;
143  char b_val;
144} bits[] = {
145  { RTF_UP, 'U' },
146  { RTF_GATEWAY, 'G' },
147  { RTF_HOST, 'H' },
148  { RTF_REJECT, 'R' },
149  { RTF_DYNAMIC, 'D' },
150  { RTF_MODIFIED, 'M' },
151  { RTF_DONE, 'd' },
152  { RTF_CLONING, 'C' },
153  { RTF_XRESOLVE, 'X' },
154  { RTF_LLINFO, 'L' },
155  { RTF_STATIC, 'S' },
156  { RTF_PROTO1, '1' },
157  { RTF_PROTO2, '2' },
158  { RTF_BLACKHOLE, 'B' },
159#ifdef RTF_WASCLONED
160  { RTF_WASCLONED, 'W' },
161#endif
162#ifdef RTF_PRCLONING
163  { RTF_PRCLONING, 'c' },
164#endif
165#ifdef RTF_PROTO3
166  { RTF_PROTO3, '3' },
167#endif
168#ifdef RTF_BROADCAST
169  { RTF_BROADCAST, 'b' },
170#endif
171  { 0, '\0' }
172};
173
174#ifndef RTF_WASCLONED
175#define RTF_WASCLONED (0)
176#endif
177
178static void
179p_flags(struct prompt *prompt, u_int32_t f, int max)
180{
181  char name[33], *flags;
182  register struct bits *p = bits;
183
184  if (max > sizeof name - 1)
185    max = sizeof name - 1;
186
187  for (flags = name; p->b_mask && flags - name < max; p++)
188    if (p->b_mask & f)
189      *flags++ = p->b_val;
190  *flags = '\0';
191  prompt_Printf(prompt, "%-*.*s", max, max, name);
192}
193
194const char *
195Index2Nam(int idx)
196{
197  /*
198   * XXX: Maybe we should select() on the routing socket so that we can
199   *      notice interfaces that come & go (PCCARD support).
200   *      Or we could even support a signal that resets these so that
201   *      the PCCARD insert/remove events can signal ppp.
202   */
203  static char **ifs;		/* Figure these out once */
204  static int nifs, debug_done;	/* Figure out how many once, and debug once */
205
206  if (!nifs) {
207    int mib[6], have, had;
208    size_t needed;
209    char *buf, *ptr, *end;
210    struct sockaddr_dl *dl;
211    struct if_msghdr *ifm;
212
213    mib[0] = CTL_NET;
214    mib[1] = PF_ROUTE;
215    mib[2] = 0;
216    mib[3] = 0;
217    mib[4] = NET_RT_IFLIST;
218    mib[5] = 0;
219
220    if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0) {
221      log_Printf(LogERROR, "Index2Nam: sysctl: estimate: %s\n",
222                 strerror(errno));
223      return "???";
224    }
225    if ((buf = malloc(needed)) == NULL)
226      return "???";
227    if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) {
228      free(buf);
229      return "???";
230    }
231    end = buf + needed;
232
233    have = 0;
234    for (ptr = buf; ptr < end; ptr += ifm->ifm_msglen) {
235      ifm = (struct if_msghdr *)ptr;
236      dl = (struct sockaddr_dl *)(ifm + 1);
237      if (ifm->ifm_index > 0) {
238        if (ifm->ifm_index > have) {
239          had = have;
240          have = ifm->ifm_index + 5;
241          if (had)
242            ifs = (char **)realloc(ifs, sizeof(char *) * have);
243          else
244            ifs = (char **)malloc(sizeof(char *) * have);
245          if (!ifs) {
246            log_Printf(LogDEBUG, "Index2Nam: %s\n", strerror(errno));
247            nifs = 0;
248            return "???";
249          }
250          memset(ifs + had, '\0', sizeof(char *) * (have - had));
251        }
252        if (ifs[ifm->ifm_index-1] == NULL) {
253          ifs[ifm->ifm_index-1] = (char *)malloc(dl->sdl_nlen+1);
254          memcpy(ifs[ifm->ifm_index-1], dl->sdl_data, dl->sdl_nlen);
255          ifs[ifm->ifm_index-1][dl->sdl_nlen] = '\0';
256          if (nifs < ifm->ifm_index)
257            nifs = ifm->ifm_index;
258        }
259      } else if (log_IsKept(LogDEBUG))
260        log_Printf(LogDEBUG, "Skipping out-of-range interface %d!\n",
261                  ifm->ifm_index);
262    }
263    free(buf);
264  }
265
266  if (log_IsKept(LogDEBUG) && !debug_done) {
267    int f;
268
269    log_Printf(LogDEBUG, "Found the following interfaces:\n");
270    for (f = 0; f < nifs; f++)
271      if (ifs[f] != NULL)
272        log_Printf(LogDEBUG, " Index %d, name \"%s\"\n", f+1, ifs[f]);
273    debug_done = 1;
274  }
275
276  if (idx < 1 || idx > nifs || ifs[idx-1] == NULL)
277    return "???";
278
279  return ifs[idx-1];
280}
281
282int
283route_Show(struct cmdargs const *arg)
284{
285  struct rt_msghdr *rtm;
286  struct sockaddr *sa_dst, *sa_gw, *sa_mask;
287  char *sp, *ep, *cp, *wp;
288  size_t needed;
289  int mib[6];
290
291  mib[0] = CTL_NET;
292  mib[1] = PF_ROUTE;
293  mib[2] = 0;
294  mib[3] = 0;
295  mib[4] = NET_RT_DUMP;
296  mib[5] = 0;
297  if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0) {
298    log_Printf(LogERROR, "route_Show: sysctl: estimate: %s\n", strerror(errno));
299    return (1);
300  }
301  sp = malloc(needed);
302  if (sp == NULL)
303    return (1);
304  if (sysctl(mib, 6, sp, &needed, NULL, 0) < 0) {
305    log_Printf(LogERROR, "route_Show: sysctl: getroute: %s\n", strerror(errno));
306    free(sp);
307    return (1);
308  }
309  ep = sp + needed;
310
311  prompt_Printf(arg->prompt, "%-20s%-20sFlags  Netif\n",
312                "Destination", "Gateway");
313  for (cp = sp; cp < ep; cp += rtm->rtm_msglen) {
314    rtm = (struct rt_msghdr *) cp;
315    wp = (char *)(rtm+1);
316
317    if (rtm->rtm_addrs & RTA_DST) {
318      sa_dst = (struct sockaddr *)wp;
319      wp += sa_dst->sa_len;
320    } else
321      sa_dst = NULL;
322
323    if (rtm->rtm_addrs & RTA_GATEWAY) {
324      sa_gw = (struct sockaddr *)wp;
325      wp += sa_gw->sa_len;
326    } else
327      sa_gw = NULL;
328
329    if (rtm->rtm_addrs & RTA_NETMASK) {
330      sa_mask = (struct sockaddr *)wp;
331      wp += sa_mask->sa_len;
332    } else
333      sa_mask = NULL;
334
335    p_sockaddr(arg->prompt, sa_dst, sa_mask, 20);
336    p_sockaddr(arg->prompt, sa_gw, NULL, 20);
337
338    p_flags(arg->prompt, rtm->rtm_flags, 6);
339    prompt_Printf(arg->prompt, " %s\n", Index2Nam(rtm->rtm_index));
340  }
341  free(sp);
342  return 0;
343}
344
345/*
346 *  Delete routes associated with our interface
347 */
348void
349route_IfDelete(struct bundle *bundle, int all)
350{
351  struct rt_msghdr *rtm;
352  struct sockaddr *sa;
353  struct in_addr sa_dst, sa_none;
354  int pass;
355  size_t needed;
356  char *sp, *cp, *ep;
357  int mib[6];
358
359  log_Printf(LogDEBUG, "route_IfDelete (%d)\n", bundle->ifp.Index);
360  sa_none.s_addr = INADDR_ANY;
361
362  mib[0] = CTL_NET;
363  mib[1] = PF_ROUTE;
364  mib[2] = 0;
365  mib[3] = 0;
366  mib[4] = NET_RT_DUMP;
367  mib[5] = 0;
368  if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0) {
369    log_Printf(LogERROR, "route_IfDelete: sysctl: estimate: %s\n",
370	      strerror(errno));
371    return;
372  }
373
374  sp = malloc(needed);
375  if (sp == NULL)
376    return;
377
378  if (sysctl(mib, 6, sp, &needed, NULL, 0) < 0) {
379    log_Printf(LogERROR, "route_IfDelete: sysctl: getroute: %s\n",
380	      strerror(errno));
381    free(sp);
382    return;
383  }
384  ep = sp + needed;
385
386  for (pass = 0; pass < 2; pass++) {
387    /*
388     * We do 2 passes.  The first deletes all cloned routes.  The second
389     * deletes all non-cloned routes.  This is necessary to avoid
390     * potential errors from trying to delete route X after route Y where
391     * route X was cloned from route Y (and is no longer there 'cos it
392     * may have gone with route Y).
393     */
394    if (RTF_WASCLONED == 0 && pass == 0)
395      /* So we can't tell ! */
396      continue;
397    for (cp = sp; cp < ep; cp += rtm->rtm_msglen) {
398      rtm = (struct rt_msghdr *) cp;
399      sa = (struct sockaddr *) (rtm + 1);
400      log_Printf(LogDEBUG, "route_IfDelete: addrs: %x, Netif: %d (%s),"
401                " flags: %x, dst: %s ?\n", rtm->rtm_addrs, rtm->rtm_index,
402                Index2Nam(rtm->rtm_index), rtm->rtm_flags,
403	        inet_ntoa(((struct sockaddr_in *) sa)->sin_addr));
404      if (rtm->rtm_addrs & RTA_DST && rtm->rtm_addrs & RTA_GATEWAY &&
405	  rtm->rtm_index == bundle->ifp.Index &&
406	  (all || (rtm->rtm_flags & RTF_GATEWAY))) {
407        sa_dst.s_addr = ((struct sockaddr_in *)sa)->sin_addr.s_addr;
408        sa = (struct sockaddr *)((char *)sa + sa->sa_len);
409        if (sa->sa_family == AF_INET || sa->sa_family == AF_LINK) {
410          if ((pass == 0 && (rtm->rtm_flags & RTF_WASCLONED)) ||
411              (pass == 1 && !(rtm->rtm_flags & RTF_WASCLONED))) {
412            log_Printf(LogDEBUG, "route_IfDelete: Remove it (pass %d)\n", pass);
413            bundle_SetRoute(bundle, RTM_DELETE, sa_dst, sa_none, sa_none, 0);
414          } else
415            log_Printf(LogDEBUG, "route_IfDelete: Skip it (pass %d)\n", pass);
416        } else
417          log_Printf(LogDEBUG,
418                    "route_IfDelete: Can't remove routes of %d family !\n",
419                    sa->sa_family);
420      }
421    }
422  }
423  free(sp);
424}
425
426int
427GetIfIndex(char *name)
428{
429  int idx;
430  const char *got;
431
432  idx = 1;
433  while (strcmp(got = Index2Nam(idx), "???"))
434    if (!strcmp(got, name))
435      return idx;
436    else
437      idx++;
438  return -1;
439}
440
441void
442route_Change(struct bundle *bundle, struct sticky_route *r,
443             struct in_addr me, struct in_addr peer)
444{
445  struct in_addr none, del;
446
447  none.s_addr = INADDR_ANY;
448  for (; r; r = r->next) {
449    if ((r->type & ROUTE_DSTMYADDR) && r->dst.s_addr != me.s_addr) {
450      del.s_addr = r->dst.s_addr & r->mask.s_addr;
451      bundle_SetRoute(bundle, RTM_DELETE, del, none, none, 1);
452      r->dst = me;
453      if (r->type & ROUTE_GWHISADDR)
454        r->gw = peer;
455    } else if ((r->type & ROUTE_DSTHISADDR) && r->dst.s_addr != peer.s_addr) {
456      del.s_addr = r->dst.s_addr & r->mask.s_addr;
457      bundle_SetRoute(bundle, RTM_DELETE, del, none, none, 1);
458      r->dst = peer;
459      if (r->type & ROUTE_GWHISADDR)
460        r->gw = peer;
461    } else if ((r->type & ROUTE_GWHISADDR) && r->gw.s_addr != peer.s_addr)
462      r->gw = peer;
463    bundle_SetRoute(bundle, RTM_ADD, r->dst, r->gw, r->mask, 1);
464  }
465}
466
467void
468route_Clean(struct bundle *bundle, struct sticky_route *r)
469{
470  struct in_addr none, del;
471
472  none.s_addr = INADDR_ANY;
473  for (; r; r = r->next) {
474    del.s_addr = r->dst.s_addr & r->mask.s_addr;
475    bundle_SetRoute(bundle, RTM_DELETE, del, none, none, 1);
476  }
477}
478
479void
480route_Add(struct sticky_route **rp, int type, struct in_addr dst,
481          struct in_addr mask, struct in_addr gw)
482{
483  if (type != ROUTE_STATIC) {
484    struct sticky_route *r;
485    int dsttype = type & ROUTE_DSTANY;
486
487    r = NULL;
488    while (*rp) {
489      if ((dsttype && dsttype == ((*rp)->type & ROUTE_DSTANY)) ||
490          (!dsttype && (*rp)->dst.s_addr == dst.s_addr)) {
491        r = *rp;
492        *rp = r->next;
493      } else
494        rp = &(*rp)->next;
495    }
496
497    if (!r)
498      r = (struct sticky_route *)malloc(sizeof(struct sticky_route));
499    r->type = type;
500    r->next = NULL;
501    r->dst = dst;
502    r->mask = mask;
503    r->gw = gw;
504    *rp = r;
505  }
506}
507
508void
509route_Delete(struct sticky_route **rp, int type, struct in_addr dst)
510{
511  struct sticky_route *r;
512  int dsttype = type & ROUTE_DSTANY;
513
514  for (; *rp; rp = &(*rp)->next) {
515    if ((dsttype && dsttype == ((*rp)->type & ROUTE_DSTANY)) ||
516        (!dsttype && dst.s_addr == ((*rp)->dst.s_addr & (*rp)->mask.s_addr))) {
517      r = *rp;
518      *rp = r->next;
519      free(r);
520      break;
521    }
522  }
523}
524
525void
526route_DeleteAll(struct sticky_route **rp)
527{
528  struct sticky_route *r, *rn;
529
530  for (r = *rp; r; r = rn) {
531    rn = r->next;
532    free(r);
533  }
534  *rp = NULL;
535}
536
537void
538route_ShowSticky(struct prompt *p, struct sticky_route *r)
539{
540  int def;
541
542  prompt_Printf(p, "Sticky routes:\n");
543  for (; r; r = r->next) {
544    def = r->dst.s_addr == INADDR_ANY && r->mask.s_addr == INADDR_ANY;
545
546    prompt_Printf(p, " add ");
547    if (r->type & ROUTE_DSTMYADDR)
548      prompt_Printf(p, "MYADDR");
549    else if (r->type & ROUTE_DSTHISADDR)
550      prompt_Printf(p, "HISADDR");
551    else if (!def)
552      prompt_Printf(p, "%s", inet_ntoa(r->dst));
553
554    if (def)
555      prompt_Printf(p, "default ");
556    else
557      prompt_Printf(p, " %s ", inet_ntoa(r->mask));
558
559    if (r->type & ROUTE_GWHISADDR)
560      prompt_Printf(p, "HISADDR\n");
561    else
562      prompt_Printf(p, "%s\n", inet_ntoa(r->gw));
563  }
564}
565