xref: /freebsd/usr.sbin/ppp/route.c (revision 70fe064ad7cab6c0444b91622f60ec6a462f308a)
1 /*-
2  * Copyright (c) 1996 - 2001 Brian Somers <brian@Awfulhak.org>
3  *          based on work by Toshiharu OHNO <tony-o@iij.ad.jp>
4  *                           Internet Initiative Japan, Inc (IIJ)
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  *
28  * $FreeBSD$
29  */
30 
31 #include <sys/param.h>
32 #include <sys/socket.h>
33 #include <net/if_types.h>
34 #include <net/route.h>
35 #include <net/if.h>
36 #include <netinet/in.h>
37 #include <arpa/inet.h>
38 #include <net/if_dl.h>
39 #include <netinet/in_systm.h>
40 #include <netinet/ip.h>
41 #include <sys/un.h>
42 #include <netdb.h>
43 
44 #include <errno.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <string.h>
48 #include <sys/sysctl.h>
49 #include <termios.h>
50 #include <unistd.h>
51 
52 #include "layer.h"
53 #include "defs.h"
54 #include "command.h"
55 #include "mbuf.h"
56 #include "log.h"
57 #include "iplist.h"
58 #include "timer.h"
59 #include "throughput.h"
60 #include "lqr.h"
61 #include "hdlc.h"
62 #include "fsm.h"
63 #include "lcp.h"
64 #include "ccp.h"
65 #include "link.h"
66 #include "slcompress.h"
67 #include "ncpaddr.h"
68 #include "ip.h"
69 #include "ipcp.h"
70 #include "filter.h"
71 #include "descriptor.h"
72 #include "mp.h"
73 #ifndef NORADIUS
74 #include "radius.h"
75 #endif
76 #include "ipv6cp.h"
77 #include "ncp.h"
78 #include "bundle.h"
79 #include "route.h"
80 #include "prompt.h"
81 #include "iface.h"
82 #include "id.h"
83 
84 
85 static void
86 p_sockaddr(struct prompt *prompt, struct sockaddr *phost,
87            struct sockaddr *pmask, int width)
88 {
89   struct ncprange range;
90   char buf[29];
91   struct sockaddr_dl *dl = (struct sockaddr_dl *)phost;
92 
93   if (log_IsKept(LogDEBUG)) {
94     char tmp[50];
95 
96     log_Printf(LogDEBUG, "Found the following sockaddr:\n");
97     log_Printf(LogDEBUG, "  Family %d, len %d\n",
98                (int)phost->sa_family, (int)phost->sa_len);
99     inet_ntop(phost->sa_family, phost->sa_data, tmp, sizeof tmp);
100     log_Printf(LogDEBUG, "  Addr %s\n", tmp);
101     if (pmask) {
102       inet_ntop(pmask->sa_family, pmask->sa_data, tmp, sizeof tmp);
103       log_Printf(LogDEBUG, "  Mask %s\n", tmp);
104     }
105   }
106 
107   switch (phost->sa_family) {
108   case AF_INET:
109 #ifndef NOINET6
110   case AF_INET6:
111 #endif
112     ncprange_setsa(&range, phost, pmask);
113     if (ncprange_isdefault(&range))
114       prompt_Printf(prompt, "%-*s ", width - 1, "default");
115     else
116       prompt_Printf(prompt, "%-*s ", width - 1, ncprange_ntoa(&range));
117     return;
118 
119   case AF_LINK:
120     if (dl->sdl_nlen)
121       snprintf(buf, sizeof buf, "%.*s", dl->sdl_nlen, dl->sdl_data);
122     else if (dl->sdl_alen) {
123       if (dl->sdl_type == IFT_ETHER) {
124         if (dl->sdl_alen < sizeof buf / 3) {
125           int f;
126           u_char *MAC;
127 
128           MAC = (u_char *)dl->sdl_data + dl->sdl_nlen;
129           for (f = 0; f < dl->sdl_alen; f++)
130             sprintf(buf+f*3, "%02x:", MAC[f]);
131           buf[f*3-1] = '\0';
132         } else
133           strcpy(buf, "??:??:??:??:??:??");
134       } else
135         sprintf(buf, "<IFT type %d>", dl->sdl_type);
136     }  else if (dl->sdl_slen)
137       sprintf(buf, "<slen %d?>", dl->sdl_slen);
138     else
139       sprintf(buf, "link#%d", dl->sdl_index);
140     break;
141 
142   default:
143     sprintf(buf, "<AF type %d>", phost->sa_family);
144     break;
145   }
146 
147   prompt_Printf(prompt, "%-*s ", width-1, buf);
148 }
149 
150 static struct bits {
151   u_int32_t b_mask;
152   char b_val;
153 } bits[] = {
154   { RTF_UP, 'U' },
155   { RTF_GATEWAY, 'G' },
156   { RTF_HOST, 'H' },
157   { RTF_REJECT, 'R' },
158   { RTF_DYNAMIC, 'D' },
159   { RTF_MODIFIED, 'M' },
160   { RTF_DONE, 'd' },
161   { RTF_CLONING, 'C' },
162   { RTF_XRESOLVE, 'X' },
163   { RTF_LLINFO, 'L' },
164   { RTF_STATIC, 'S' },
165   { RTF_PROTO1, '1' },
166   { RTF_PROTO2, '2' },
167   { RTF_BLACKHOLE, 'B' },
168 #ifdef RTF_WASCLONED
169   { RTF_WASCLONED, 'W' },
170 #endif
171 #ifdef RTF_PRCLONING
172   { RTF_PRCLONING, 'c' },
173 #endif
174 #ifdef RTF_PROTO3
175   { RTF_PROTO3, '3' },
176 #endif
177 #ifdef RTF_BROADCAST
178   { RTF_BROADCAST, 'b' },
179 #endif
180   { 0, '\0' }
181 };
182 
183 #ifndef RTF_WASCLONED
184 #define RTF_WASCLONED (0)
185 #endif
186 
187 static void
188 p_flags(struct prompt *prompt, u_int32_t f, int max)
189 {
190   char name[33], *flags;
191   register struct bits *p = bits;
192 
193   if (max > sizeof name - 1)
194     max = sizeof name - 1;
195 
196   for (flags = name; p->b_mask && flags - name < max; p++)
197     if (p->b_mask & f)
198       *flags++ = p->b_val;
199   *flags = '\0';
200   prompt_Printf(prompt, "%-*.*s", max, max, name);
201 }
202 
203 const char *
204 Index2Nam(int idx)
205 {
206   /*
207    * XXX: Maybe we should select() on the routing socket so that we can
208    *      notice interfaces that come & go (PCCARD support).
209    *      Or we could even support a signal that resets these so that
210    *      the PCCARD insert/remove events can signal ppp.
211    */
212   static char **ifs;		/* Figure these out once */
213   static int nifs, debug_done;	/* Figure out how many once, and debug once */
214 
215   if (idx > nifs || (idx > 0 && ifs[idx-1] == NULL)) {
216     int mib[6], have, had;
217     size_t needed;
218     char *buf, *ptr, *end;
219     struct sockaddr_dl *dl;
220     struct if_msghdr *ifm;
221 
222     if (ifs) {
223       free(ifs);
224       ifs = NULL;
225       nifs = 0;
226     }
227     debug_done = 0;
228 
229     mib[0] = CTL_NET;
230     mib[1] = PF_ROUTE;
231     mib[2] = 0;
232     mib[3] = 0;
233     mib[4] = NET_RT_IFLIST;
234     mib[5] = 0;
235 
236     if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0) {
237       log_Printf(LogERROR, "Index2Nam: sysctl: estimate: %s\n",
238                  strerror(errno));
239       return NumStr(idx, NULL, 0);
240     }
241     if ((buf = malloc(needed)) == NULL)
242       return NumStr(idx, NULL, 0);
243     if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) {
244       free(buf);
245       return NumStr(idx, NULL, 0);
246     }
247     end = buf + needed;
248 
249     have = 0;
250     for (ptr = buf; ptr < end; ptr += ifm->ifm_msglen) {
251       ifm = (struct if_msghdr *)ptr;
252       if (ifm->ifm_type != RTM_IFINFO)
253         continue;
254       dl = (struct sockaddr_dl *)(ifm + 1);
255       if (ifm->ifm_index > 0) {
256         if (ifm->ifm_index > have) {
257           char **newifs;
258 
259           had = have;
260           have = ifm->ifm_index + 5;
261           if (had)
262             newifs = (char **)realloc(ifs, sizeof(char *) * have);
263           else
264             newifs = (char **)malloc(sizeof(char *) * have);
265           if (!newifs) {
266             log_Printf(LogDEBUG, "Index2Nam: %s\n", strerror(errno));
267             nifs = 0;
268             if (ifs) {
269               free(ifs);
270               ifs = NULL;
271             }
272             free(buf);
273             return NumStr(idx, NULL, 0);
274           }
275           ifs = newifs;
276           memset(ifs + had, '\0', sizeof(char *) * (have - had));
277         }
278         if (ifs[ifm->ifm_index-1] == NULL) {
279           ifs[ifm->ifm_index-1] = (char *)malloc(dl->sdl_nlen+1);
280           memcpy(ifs[ifm->ifm_index-1], dl->sdl_data, dl->sdl_nlen);
281           ifs[ifm->ifm_index-1][dl->sdl_nlen] = '\0';
282           if (nifs < ifm->ifm_index)
283             nifs = ifm->ifm_index;
284         }
285       } else if (log_IsKept(LogDEBUG))
286         log_Printf(LogDEBUG, "Skipping out-of-range interface %d!\n",
287                   ifm->ifm_index);
288     }
289     free(buf);
290   }
291 
292   if (log_IsKept(LogDEBUG) && !debug_done) {
293     int f;
294 
295     log_Printf(LogDEBUG, "Found the following interfaces:\n");
296     for (f = 0; f < nifs; f++)
297       if (ifs[f] != NULL)
298         log_Printf(LogDEBUG, " Index %d, name \"%s\"\n", f+1, ifs[f]);
299     debug_done = 1;
300   }
301 
302   if (idx < 1 || idx > nifs || ifs[idx-1] == NULL)
303     return NumStr(idx, NULL, 0);
304 
305   return ifs[idx-1];
306 }
307 
308 void
309 route_ParseHdr(struct rt_msghdr *rtm, struct sockaddr *sa[RTAX_MAX])
310 {
311   char *wp;
312   int rtax;
313 
314   wp = (char *)(rtm + 1);
315 
316   for (rtax = 0; rtax < RTAX_MAX; rtax++)
317     if (rtm->rtm_addrs & (1 << rtax)) {
318       sa[rtax] = (struct sockaddr *)wp;
319       wp += ROUNDUP(sa[rtax]->sa_len);
320       if (sa[rtax]->sa_family == 0)
321         sa[rtax] = NULL;	/* ??? */
322     } else
323       sa[rtax] = NULL;
324 }
325 
326 int
327 route_Show(struct cmdargs const *arg)
328 {
329   struct rt_msghdr *rtm;
330   struct sockaddr *sa[RTAX_MAX];
331   char *sp, *ep, *cp;
332   size_t needed;
333   int mib[6];
334 
335   mib[0] = CTL_NET;
336   mib[1] = PF_ROUTE;
337   mib[2] = 0;
338   mib[3] = 0;
339   mib[4] = NET_RT_DUMP;
340   mib[5] = 0;
341   if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0) {
342     log_Printf(LogERROR, "route_Show: sysctl: estimate: %s\n", strerror(errno));
343     return (1);
344   }
345   sp = malloc(needed);
346   if (sp == NULL)
347     return (1);
348   if (sysctl(mib, 6, sp, &needed, NULL, 0) < 0) {
349     log_Printf(LogERROR, "route_Show: sysctl: getroute: %s\n", strerror(errno));
350     free(sp);
351     return (1);
352   }
353   ep = sp + needed;
354 
355   prompt_Printf(arg->prompt, "%-20s%-20sFlags  Netif\n",
356                 "Destination", "Gateway");
357   for (cp = sp; cp < ep; cp += rtm->rtm_msglen) {
358     rtm = (struct rt_msghdr *)cp;
359 
360     route_ParseHdr(rtm, sa);
361 
362     if (sa[RTAX_DST] && sa[RTAX_GATEWAY]) {
363       p_sockaddr(arg->prompt, sa[RTAX_DST], sa[RTAX_NETMASK], 20);
364       p_sockaddr(arg->prompt, sa[RTAX_GATEWAY], NULL, 20);
365 
366       p_flags(arg->prompt, rtm->rtm_flags, 6);
367       prompt_Printf(arg->prompt, " %s\n", Index2Nam(rtm->rtm_index));
368     } else
369       prompt_Printf(arg->prompt, "<can't parse routing entry>\n");
370   }
371   free(sp);
372   return 0;
373 }
374 
375 /*
376  *  Delete routes associated with our interface
377  */
378 void
379 route_IfDelete(struct bundle *bundle, int all)
380 {
381   struct rt_msghdr *rtm;
382   struct sockaddr *sa[RTAX_MAX];
383   struct ncprange range;
384   int pass;
385   size_t needed;
386   char *sp, *cp, *ep;
387   int mib[6];
388 
389   log_Printf(LogDEBUG, "route_IfDelete (%d)\n", bundle->iface->index);
390 
391   mib[0] = CTL_NET;
392   mib[1] = PF_ROUTE;
393   mib[2] = 0;
394   mib[3] = 0;
395   mib[4] = NET_RT_DUMP;
396   mib[5] = 0;
397   if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0) {
398     log_Printf(LogERROR, "route_IfDelete: sysctl: estimate: %s\n",
399               strerror(errno));
400     return;
401   }
402 
403   sp = malloc(needed);
404   if (sp == NULL)
405     return;
406 
407   if (sysctl(mib, 6, sp, &needed, NULL, 0) < 0) {
408     log_Printf(LogERROR, "route_IfDelete: sysctl: getroute: %s\n",
409               strerror(errno));
410     free(sp);
411     return;
412   }
413   ep = sp + needed;
414 
415   for (pass = 0; pass < 2; pass++) {
416     /*
417      * We do 2 passes.  The first deletes all cloned routes.  The second
418      * deletes all non-cloned routes.  This is done to avoid
419      * potential errors from trying to delete route X after route Y where
420      * route X was cloned from route Y (and is no longer there 'cos it
421      * may have gone with route Y).
422      */
423     if (RTF_WASCLONED == 0 && pass == 0)
424       /* So we can't tell ! */
425       continue;
426     for (cp = sp; cp < ep; cp += rtm->rtm_msglen) {
427       rtm = (struct rt_msghdr *)cp;
428       route_ParseHdr(rtm, sa);
429       if (rtm->rtm_index == bundle->iface->index &&
430           sa[RTAX_DST] && sa[RTAX_GATEWAY] &&
431           (sa[RTAX_DST]->sa_family == AF_INET
432 #ifndef NOINET6
433            || sa[RTAX_DST]->sa_family == AF_INET6
434 #endif
435            ) &&
436           (all || (rtm->rtm_flags & RTF_GATEWAY))) {
437         if (log_IsKept(LogDEBUG)) {
438           char gwstr[41];
439           struct ncpaddr gw;
440           ncprange_setsa(&range, sa[RTAX_DST], sa[RTAX_NETMASK]);
441           ncpaddr_setsa(&gw, sa[RTAX_GATEWAY]);
442           snprintf(gwstr, sizeof gwstr, "%s", ncpaddr_ntoa(&gw));
443           log_Printf(LogDEBUG, "Found %s %s\n", ncprange_ntoa(&range), gwstr);
444         }
445         if (sa[RTAX_GATEWAY]->sa_family == AF_INET ||
446 #ifndef NOINET6
447             sa[RTAX_GATEWAY]->sa_family == AF_INET6 ||
448 #endif
449             sa[RTAX_GATEWAY]->sa_family == AF_LINK) {
450           if ((pass == 0 && (rtm->rtm_flags & RTF_WASCLONED)) ||
451               (pass == 1 && !(rtm->rtm_flags & RTF_WASCLONED))) {
452             ncprange_setsa(&range, sa[RTAX_DST], sa[RTAX_NETMASK]);
453             rt_Set(bundle, RTM_DELETE, &range, NULL, 0, 0);
454           } else
455             log_Printf(LogDEBUG, "route_IfDelete: Skip it (pass %d)\n", pass);
456         } else
457           log_Printf(LogDEBUG,
458                     "route_IfDelete: Can't remove routes for family %d\n",
459                     sa[RTAX_GATEWAY]->sa_family);
460       }
461     }
462   }
463   free(sp);
464 }
465 
466 
467 /*
468  *  Update the MTU on all routes for the given interface
469  */
470 void
471 route_UpdateMTU(struct bundle *bundle)
472 {
473   struct rt_msghdr *rtm;
474   struct sockaddr *sa[RTAX_MAX];
475   struct ncprange dst;
476   size_t needed;
477   char *sp, *cp, *ep;
478   int mib[6];
479 
480   log_Printf(LogDEBUG, "route_UpdateMTU (%d)\n", bundle->iface->index);
481 
482   mib[0] = CTL_NET;
483   mib[1] = PF_ROUTE;
484   mib[2] = 0;
485   mib[3] = 0;
486   mib[4] = NET_RT_DUMP;
487   mib[5] = 0;
488   if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0) {
489     log_Printf(LogERROR, "route_IfDelete: sysctl: estimate: %s\n",
490               strerror(errno));
491     return;
492   }
493 
494   sp = malloc(needed);
495   if (sp == NULL)
496     return;
497 
498   if (sysctl(mib, 6, sp, &needed, NULL, 0) < 0) {
499     log_Printf(LogERROR, "route_IfDelete: sysctl: getroute: %s\n",
500               strerror(errno));
501     free(sp);
502     return;
503   }
504   ep = sp + needed;
505 
506   for (cp = sp; cp < ep; cp += rtm->rtm_msglen) {
507     rtm = (struct rt_msghdr *)cp;
508     route_ParseHdr(rtm, sa);
509     if (sa[RTAX_DST] && (sa[RTAX_DST]->sa_family == AF_INET
510 #ifndef NOINET6
511                          || sa[RTAX_DST]->sa_family == AF_INET6
512 #endif
513                         ) &&
514         sa[RTAX_GATEWAY] && rtm->rtm_index == bundle->iface->index) {
515       if (log_IsKept(LogTCPIP)) {
516         ncprange_setsa(&dst, sa[RTAX_DST], sa[RTAX_NETMASK]);
517         log_Printf(LogTCPIP, "route_UpdateMTU: Netif: %d (%s), dst %s,"
518                    " mtu %d\n", rtm->rtm_index, Index2Nam(rtm->rtm_index),
519                    ncprange_ntoa(&dst), bundle->iface->mtu);
520       }
521       rt_Update(bundle, sa[RTAX_DST], sa[RTAX_GATEWAY], sa[RTAX_NETMASK]);
522     }
523   }
524 
525   free(sp);
526 }
527 
528 int
529 GetIfIndex(char *name)
530 {
531   int idx;
532   const char *got;
533 
534   idx = 1;
535   while (strcmp(got = Index2Nam(idx), "???"))
536     if (!strcmp(got, name))
537       return idx;
538     else
539       idx++;
540   return -1;
541 }
542 
543 void
544 route_Change(struct bundle *bundle, struct sticky_route *r,
545              const struct ncpaddr *me, const struct ncpaddr *peer)
546 {
547   struct ncpaddr dst;
548 
549   for (; r; r = r->next) {
550     ncprange_getaddr(&r->dst, &dst);
551     if (ncpaddr_family(me) == AF_INET) {
552       if ((r->type & ROUTE_DSTMYADDR) && !ncpaddr_equal(&dst, me)) {
553         rt_Set(bundle, RTM_DELETE, &r->dst, NULL, 1, 0);
554         ncprange_sethost(&r->dst, me);
555         if (r->type & ROUTE_GWHISADDR)
556           ncpaddr_copy(&r->gw, peer);
557       } else if ((r->type & ROUTE_DSTHISADDR) && !ncpaddr_equal(&dst, peer)) {
558         rt_Set(bundle, RTM_DELETE, &r->dst, NULL, 1, 0);
559         ncprange_sethost(&r->dst, peer);
560         if (r->type & ROUTE_GWHISADDR)
561           ncpaddr_copy(&r->gw, peer);
562       } else if ((r->type & ROUTE_DSTDNS0) && !ncpaddr_equal(&dst, peer)) {
563         if (bundle->ncp.ipcp.ns.dns[0].s_addr == INADDR_NONE)
564           continue;
565         rt_Set(bundle, RTM_DELETE, &r->dst, NULL, 1, 0);
566         if (r->type & ROUTE_GWHISADDR)
567           ncpaddr_copy(&r->gw, peer);
568       } else if ((r->type & ROUTE_DSTDNS1) && !ncpaddr_equal(&dst, peer)) {
569         if (bundle->ncp.ipcp.ns.dns[1].s_addr == INADDR_NONE)
570           continue;
571         rt_Set(bundle, RTM_DELETE, &r->dst, NULL, 1, 0);
572         if (r->type & ROUTE_GWHISADDR)
573           ncpaddr_copy(&r->gw, peer);
574       } else if ((r->type & ROUTE_GWHISADDR) && !ncpaddr_equal(&r->gw, peer))
575         ncpaddr_copy(&r->gw, peer);
576 #ifndef NOINET6
577     } else if (ncpaddr_family(me) == AF_INET6) {
578       if ((r->type & ROUTE_DSTMYADDR6) && !ncpaddr_equal(&dst, me)) {
579         rt_Set(bundle, RTM_DELETE, &r->dst, NULL, 1, 0);
580         ncprange_sethost(&r->dst, me);
581         if (r->type & ROUTE_GWHISADDR)
582           ncpaddr_copy(&r->gw, peer);
583       } else if ((r->type & ROUTE_DSTHISADDR6) && !ncpaddr_equal(&dst, peer)) {
584         rt_Set(bundle, RTM_DELETE, &r->dst, NULL, 1, 0);
585         ncprange_sethost(&r->dst, peer);
586         if (r->type & ROUTE_GWHISADDR)
587           ncpaddr_copy(&r->gw, peer);
588       } else if ((r->type & ROUTE_GWHISADDR6) && !ncpaddr_equal(&r->gw, peer))
589         ncpaddr_copy(&r->gw, peer);
590 #endif
591     }
592     rt_Set(bundle, RTM_ADD, &r->dst, &r->gw, 1, 0);
593   }
594 }
595 
596 void
597 route_Add(struct sticky_route **rp, int type, const struct ncprange *dst,
598           const struct ncpaddr *gw)
599 {
600   struct sticky_route *r;
601   int dsttype = type & ROUTE_DSTANY;
602 
603   r = NULL;
604   while (*rp) {
605     if ((dsttype && dsttype == ((*rp)->type & ROUTE_DSTANY)) ||
606         (!dsttype && ncprange_equal(&(*rp)->dst, dst))) {
607       /* Oops, we already have this route - unlink it */
608       free(r);			/* impossible really  */
609       r = *rp;
610       *rp = r->next;
611     } else
612       rp = &(*rp)->next;
613   }
614 
615   if (!r)
616     r = (struct sticky_route *)malloc(sizeof(struct sticky_route));
617   r->type = type;
618   r->next = NULL;
619   ncprange_copy(&r->dst, dst);
620   ncpaddr_copy(&r->gw, gw);
621   *rp = r;
622 }
623 
624 void
625 route_Delete(struct sticky_route **rp, int type, const struct ncprange *dst)
626 {
627   struct sticky_route *r;
628   int dsttype = type & ROUTE_DSTANY;
629 
630   for (; *rp; rp = &(*rp)->next) {
631     if ((dsttype && dsttype == ((*rp)->type & ROUTE_DSTANY)) ||
632         (!dsttype && ncprange_equal(dst, &(*rp)->dst))) {
633       r = *rp;
634       *rp = r->next;
635       free(r);
636       break;
637     }
638   }
639 }
640 
641 void
642 route_DeleteAll(struct sticky_route **rp)
643 {
644   struct sticky_route *r, *rn;
645 
646   for (r = *rp; r; r = rn) {
647     rn = r->next;
648     free(r);
649   }
650   *rp = NULL;
651 }
652 
653 void
654 route_ShowSticky(struct prompt *p, struct sticky_route *r, const char *tag,
655                  int indent)
656 {
657   int tlen = strlen(tag);
658 
659   if (tlen + 2 > indent)
660     prompt_Printf(p, "%s:\n%*s", tag, indent, "");
661   else
662     prompt_Printf(p, "%s:%*s", tag, indent - tlen - 1, "");
663 
664   for (; r; r = r->next) {
665     prompt_Printf(p, "%*sadd ", tlen ? 0 : indent, "");
666     tlen = 0;
667     if (r->type & ROUTE_DSTMYADDR)
668       prompt_Printf(p, "MYADDR");
669     else if (r->type & ROUTE_DSTMYADDR6)
670       prompt_Printf(p, "MYADDR6");
671     else if (r->type & ROUTE_DSTHISADDR)
672       prompt_Printf(p, "HISADDR");
673     else if (r->type & ROUTE_DSTHISADDR6)
674       prompt_Printf(p, "HISADDR6");
675     else if (r->type & ROUTE_DSTDNS0)
676       prompt_Printf(p, "DNS0");
677     else if (r->type & ROUTE_DSTDNS1)
678       prompt_Printf(p, "DNS1");
679     else if (ncprange_isdefault(&r->dst))
680       prompt_Printf(p, "default");
681     else
682       prompt_Printf(p, "%s", ncprange_ntoa(&r->dst));
683 
684     if (r->type & ROUTE_GWHISADDR)
685       prompt_Printf(p, " HISADDR\n");
686     else if (r->type & ROUTE_GWHISADDR6)
687       prompt_Printf(p, " HISADDR6\n");
688     else
689       prompt_Printf(p, " %s\n", ncpaddr_ntoa(&r->gw));
690   }
691 }
692 
693 struct rtmsg {
694   struct rt_msghdr m_rtm;
695   char m_space[256];
696 };
697 
698 static size_t
699 memcpy_roundup(char *cp, const void *data, size_t len)
700 {
701   size_t padlen;
702 
703   padlen = ROUNDUP(len);
704   memcpy(cp, data, len);
705   if (padlen > len)
706     memset(cp + len, '\0', padlen - len);
707 
708   return padlen;
709 }
710 
711 int
712 rt_Set(struct bundle *bundle, int cmd, const struct ncprange *dst,
713        const struct ncpaddr *gw, int bang, int quiet)
714 {
715   struct rtmsg rtmes;
716   int s, nb, wb;
717   char *cp;
718   const char *cmdstr;
719   struct sockaddr_storage sadst, samask, sagw;
720   int result = 1;
721 
722   if (bang)
723     cmdstr = (cmd == RTM_ADD ? "Add!" : "Delete!");
724   else
725     cmdstr = (cmd == RTM_ADD ? "Add" : "Delete");
726   s = ID0socket(PF_ROUTE, SOCK_RAW, 0);
727   if (s < 0) {
728     log_Printf(LogERROR, "rt_Set: socket(): %s\n", strerror(errno));
729     return result;
730   }
731   memset(&rtmes, '\0', sizeof rtmes);
732   rtmes.m_rtm.rtm_version = RTM_VERSION;
733   rtmes.m_rtm.rtm_type = cmd;
734   rtmes.m_rtm.rtm_addrs = RTA_DST;
735   rtmes.m_rtm.rtm_seq = ++bundle->routing_seq;
736   rtmes.m_rtm.rtm_pid = getpid();
737   rtmes.m_rtm.rtm_flags = RTF_UP | RTF_GATEWAY | RTF_STATIC;
738 
739   if (cmd == RTM_ADD) {
740     if (bundle->ncp.cfg.sendpipe > 0) {
741       rtmes.m_rtm.rtm_rmx.rmx_sendpipe = bundle->ncp.cfg.sendpipe;
742       rtmes.m_rtm.rtm_inits |= RTV_SPIPE;
743     }
744     if (bundle->ncp.cfg.recvpipe > 0) {
745       rtmes.m_rtm.rtm_rmx.rmx_recvpipe = bundle->ncp.cfg.recvpipe;
746       rtmes.m_rtm.rtm_inits |= RTV_RPIPE;
747     }
748   }
749 
750   ncprange_getsa(dst, &sadst, &samask);
751 
752   cp = rtmes.m_space;
753   cp += memcpy_roundup(cp, &sadst, sadst.ss_len);
754   if (cmd == RTM_ADD) {
755     if (gw == NULL) {
756       log_Printf(LogERROR, "rt_Set: Program error\n");
757       close(s);
758       return result;
759     }
760     ncpaddr_getsa(gw, &sagw);
761     if (ncpaddr_isdefault(gw)) {
762       if (!quiet)
763         log_Printf(LogERROR, "rt_Set: Cannot add a route with"
764                    " gateway 0.0.0.0\n");
765       close(s);
766       return result;
767     } else {
768       cp += memcpy_roundup(cp, &sagw, sagw.ss_len);
769       rtmes.m_rtm.rtm_addrs |= RTA_GATEWAY;
770     }
771   }
772 
773   if (!ncprange_ishost(dst)) {
774     cp += memcpy_roundup(cp, &samask, samask.ss_len);
775     rtmes.m_rtm.rtm_addrs |= RTA_NETMASK;
776   }
777 
778   nb = cp - (char *)&rtmes;
779   rtmes.m_rtm.rtm_msglen = nb;
780   wb = ID0write(s, &rtmes, nb);
781   if (wb < 0) {
782     log_Printf(LogTCPIP, "rt_Set failure:\n");
783     log_Printf(LogTCPIP, "rt_Set:  Cmd = %s\n", cmdstr);
784     log_Printf(LogTCPIP, "rt_Set:  Dst = %s\n", ncprange_ntoa(dst));
785     if (gw != NULL)
786       log_Printf(LogTCPIP, "rt_Set:  Gateway = %s\n", ncpaddr_ntoa(gw));
787 failed:
788     if (cmd == RTM_ADD && (rtmes.m_rtm.rtm_errno == EEXIST ||
789                            (rtmes.m_rtm.rtm_errno == 0 && errno == EEXIST))) {
790       if (!bang) {
791         log_Printf(LogWARN, "Add route failed: %s already exists\n",
792 		   ncprange_ntoa(dst));
793         result = 0;	/* Don't add to our dynamic list */
794       } else {
795         rtmes.m_rtm.rtm_type = cmd = RTM_CHANGE;
796         if ((wb = ID0write(s, &rtmes, nb)) < 0)
797           goto failed;
798       }
799     } else if (cmd == RTM_DELETE &&
800              (rtmes.m_rtm.rtm_errno == ESRCH ||
801               (rtmes.m_rtm.rtm_errno == 0 && errno == ESRCH))) {
802       if (!bang)
803         log_Printf(LogWARN, "Del route failed: %s: Non-existent\n",
804                   ncprange_ntoa(dst));
805     } else if (rtmes.m_rtm.rtm_errno == 0) {
806       if (!quiet || errno != ENETUNREACH)
807         log_Printf(LogWARN, "%s route failed: %s: errno: %s\n", cmdstr,
808                    ncprange_ntoa(dst), strerror(errno));
809     } else
810       log_Printf(LogWARN, "%s route failed: %s: %s\n",
811 		 cmdstr, ncprange_ntoa(dst), strerror(rtmes.m_rtm.rtm_errno));
812   }
813 
814   if (log_IsKept(LogDEBUG)) {
815     char gwstr[40];
816 
817     if (gw)
818       snprintf(gwstr, sizeof gwstr, "%s", ncpaddr_ntoa(gw));
819     else
820       snprintf(gwstr, sizeof gwstr, "<none>");
821     log_Printf(LogDEBUG, "wrote %d: cmd = %s, dst = %s, gateway = %s\n",
822                wb, cmdstr, ncprange_ntoa(dst), gwstr);
823   }
824   close(s);
825 
826   return result;
827 }
828 
829 void
830 rt_Update(struct bundle *bundle, const struct sockaddr *dst,
831           const struct sockaddr *gw, const struct sockaddr *mask)
832 {
833   struct ncprange ncpdst;
834   struct rtmsg rtmes;
835   char *p;
836   int s, wb;
837 
838   s = ID0socket(PF_ROUTE, SOCK_RAW, 0);
839   if (s < 0) {
840     log_Printf(LogERROR, "rt_Update: socket(): %s\n", strerror(errno));
841     return;
842   }
843 
844   memset(&rtmes, '\0', sizeof rtmes);
845   rtmes.m_rtm.rtm_version = RTM_VERSION;
846   rtmes.m_rtm.rtm_type = RTM_CHANGE;
847   rtmes.m_rtm.rtm_addrs = 0;
848   rtmes.m_rtm.rtm_seq = ++bundle->routing_seq;
849   rtmes.m_rtm.rtm_pid = getpid();
850   rtmes.m_rtm.rtm_flags = RTF_UP | RTF_GATEWAY | RTF_STATIC;
851 
852   if (bundle->ncp.cfg.sendpipe > 0) {
853     rtmes.m_rtm.rtm_rmx.rmx_sendpipe = bundle->ncp.cfg.sendpipe;
854     rtmes.m_rtm.rtm_inits |= RTV_SPIPE;
855   }
856 
857   if (bundle->ncp.cfg.recvpipe > 0) {
858     rtmes.m_rtm.rtm_rmx.rmx_recvpipe = bundle->ncp.cfg.recvpipe;
859     rtmes.m_rtm.rtm_inits |= RTV_RPIPE;
860   }
861 
862   rtmes.m_rtm.rtm_rmx.rmx_mtu = bundle->iface->mtu;
863   rtmes.m_rtm.rtm_inits |= RTV_MTU;
864   p = rtmes.m_space;
865 
866   if (dst) {
867     rtmes.m_rtm.rtm_addrs |= RTA_DST;
868     p += memcpy_roundup(p, dst, dst->sa_len);
869   }
870 
871   rtmes.m_rtm.rtm_msglen = p - (char *)&rtmes;
872 
873   wb = ID0write(s, &rtmes, rtmes.m_rtm.rtm_msglen);
874   if (wb < 0) {
875     ncprange_setsa(&ncpdst, dst, mask);
876 
877     log_Printf(LogTCPIP, "rt_Update failure:\n");
878     log_Printf(LogTCPIP, "rt_Update:  Dst = %s\n", ncprange_ntoa(&ncpdst));
879 
880     if (rtmes.m_rtm.rtm_errno == 0)
881       log_Printf(LogWARN, "%s: Change route failed: errno: %s\n",
882                  ncprange_ntoa(&ncpdst), strerror(errno));
883     else
884       log_Printf(LogWARN, "%s: Change route failed: %s\n",
885 		 ncprange_ntoa(&ncpdst), strerror(rtmes.m_rtm.rtm_errno));
886   }
887   close(s);
888 }
889