]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.sbin/ppp/route.c
This commit was generated by cvs2svn to compensate for changes in r101386,
[FreeBSD/FreeBSD.git] / usr.sbin / ppp / route.c
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
43 #include <errno.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <sys/sysctl.h>
48 #include <termios.h>
49 #include <unistd.h>
50
51 #include "layer.h"
52 #include "defs.h"
53 #include "command.h"
54 #include "mbuf.h"
55 #include "log.h"
56 #include "iplist.h"
57 #include "timer.h"
58 #include "throughput.h"
59 #include "lqr.h"
60 #include "hdlc.h"
61 #include "fsm.h"
62 #include "lcp.h"
63 #include "ccp.h"
64 #include "link.h"
65 #include "slcompress.h"
66 #include "ncpaddr.h"
67 #include "ipcp.h"
68 #include "filter.h"
69 #include "descriptor.h"
70 #include "mp.h"
71 #ifndef NORADIUS
72 #include "radius.h"
73 #endif
74 #include "ipv6cp.h"
75 #include "ncp.h"
76 #include "bundle.h"
77 #include "route.h"
78 #include "prompt.h"
79 #include "iface.h"
80 #include "id.h"
81
82
83 static void
84 p_sockaddr(struct prompt *prompt, struct sockaddr *phost,
85            struct sockaddr *pmask, int width)
86 {
87   struct ncprange range;
88   char buf[29];
89   struct sockaddr_dl *dl = (struct sockaddr_dl *)phost;
90
91   if (log_IsKept(LogDEBUG)) {
92     char tmp[50];
93
94     log_Printf(LogDEBUG, "Found the following sockaddr:\n");
95     log_Printf(LogDEBUG, "  Family %d, len %d\n",
96                (int)phost->sa_family, (int)phost->sa_len);
97     inet_ntop(phost->sa_family, phost->sa_data, tmp, sizeof tmp);
98     log_Printf(LogDEBUG, "  Addr %s\n", tmp);
99     if (pmask) {
100       inet_ntop(pmask->sa_family, pmask->sa_data, tmp, sizeof tmp);
101       log_Printf(LogDEBUG, "  Mask %s\n", tmp);
102     }
103   }
104
105   switch (phost->sa_family) {
106   case AF_INET:
107 #ifndef NOINET6
108   case AF_INET6:
109 #endif
110     ncprange_setsa(&range, phost, pmask);
111     if (ncprange_isdefault(&range))
112       prompt_Printf(prompt, "%-*s ", width - 1, "default");
113     else
114       prompt_Printf(prompt, "%-*s ", width - 1, ncprange_ntoa(&range));
115     return;
116
117   case AF_LINK:
118     if (dl->sdl_nlen)
119       snprintf(buf, sizeof buf, "%.*s", dl->sdl_nlen, dl->sdl_data);
120     else if (dl->sdl_alen) {
121       if (dl->sdl_type == IFT_ETHER) {
122         if (dl->sdl_alen < sizeof buf / 3) {
123           int f;
124           u_char *MAC;
125
126           MAC = (u_char *)dl->sdl_data + dl->sdl_nlen;
127           for (f = 0; f < dl->sdl_alen; f++)
128             sprintf(buf+f*3, "%02x:", MAC[f]);
129           buf[f*3-1] = '\0';
130         } else
131           strcpy(buf, "??:??:??:??:??:??");
132       } else
133         sprintf(buf, "<IFT type %d>", dl->sdl_type);
134     }  else if (dl->sdl_slen)
135       sprintf(buf, "<slen %d?>", dl->sdl_slen);
136     else
137       sprintf(buf, "link#%d", dl->sdl_index);
138     break;
139
140   default:
141     sprintf(buf, "<AF type %d>", phost->sa_family);
142     break;
143   }
144
145   prompt_Printf(prompt, "%-*s ", width-1, buf);
146 }
147
148 static struct bits {
149   u_int32_t b_mask;
150   char b_val;
151 } bits[] = {
152   { RTF_UP, 'U' },
153   { RTF_GATEWAY, 'G' },
154   { RTF_HOST, 'H' },
155   { RTF_REJECT, 'R' },
156   { RTF_DYNAMIC, 'D' },
157   { RTF_MODIFIED, 'M' },
158   { RTF_DONE, 'd' },
159   { RTF_CLONING, 'C' },
160   { RTF_XRESOLVE, 'X' },
161   { RTF_LLINFO, 'L' },
162   { RTF_STATIC, 'S' },
163   { RTF_PROTO1, '1' },
164   { RTF_PROTO2, '2' },
165   { RTF_BLACKHOLE, 'B' },
166 #ifdef RTF_WASCLONED
167   { RTF_WASCLONED, 'W' },
168 #endif
169 #ifdef RTF_PRCLONING
170   { RTF_PRCLONING, 'c' },
171 #endif
172 #ifdef RTF_PROTO3
173   { RTF_PROTO3, '3' },
174 #endif
175 #ifdef RTF_BROADCAST
176   { RTF_BROADCAST, 'b' },
177 #endif
178   { 0, '\0' }
179 };
180
181 #ifndef RTF_WASCLONED
182 #define RTF_WASCLONED (0)
183 #endif
184
185 static void
186 p_flags(struct prompt *prompt, u_int32_t f, int max)
187 {
188   char name[33], *flags;
189   register struct bits *p = bits;
190
191   if (max > sizeof name - 1)
192     max = sizeof name - 1;
193
194   for (flags = name; p->b_mask && flags - name < max; p++)
195     if (p->b_mask & f)
196       *flags++ = p->b_val;
197   *flags = '\0';
198   prompt_Printf(prompt, "%-*.*s", max, max, name);
199 }
200
201 static int route_nifs = -1;
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 debug_done;        /* Debug once */
214
215   if (idx > route_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       route_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             route_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 (route_nifs < ifm->ifm_index)
283             route_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 < route_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 > route_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
533   idx = 1;
534   while (route_nifs == -1 || idx < route_nifs)
535     if (strcmp(Index2Nam(idx), name) == 0)
536       return idx;
537     else
538       idx++;
539   return -1;
540 }
541
542 void
543 route_Change(struct bundle *bundle, struct sticky_route *r,
544              const struct ncpaddr *me, const struct ncpaddr *peer)
545 {
546   struct ncpaddr dst;
547
548   for (; r; r = r->next) {
549     ncprange_getaddr(&r->dst, &dst);
550     if (ncpaddr_family(me) == AF_INET) {
551       if ((r->type & ROUTE_DSTMYADDR) && !ncpaddr_equal(&dst, me)) {
552         rt_Set(bundle, RTM_DELETE, &r->dst, NULL, 1, 0);
553         ncprange_sethost(&r->dst, me);
554         if (r->type & ROUTE_GWHISADDR)
555           ncpaddr_copy(&r->gw, peer);
556       } else if ((r->type & ROUTE_DSTHISADDR) && !ncpaddr_equal(&dst, peer)) {
557         rt_Set(bundle, RTM_DELETE, &r->dst, NULL, 1, 0);
558         ncprange_sethost(&r->dst, peer);
559         if (r->type & ROUTE_GWHISADDR)
560           ncpaddr_copy(&r->gw, peer);
561       } else if ((r->type & ROUTE_DSTDNS0) && !ncpaddr_equal(&dst, peer)) {
562         if (bundle->ncp.ipcp.ns.dns[0].s_addr == INADDR_NONE)
563           continue;
564         rt_Set(bundle, RTM_DELETE, &r->dst, NULL, 1, 0);
565         if (r->type & ROUTE_GWHISADDR)
566           ncpaddr_copy(&r->gw, peer);
567       } else if ((r->type & ROUTE_DSTDNS1) && !ncpaddr_equal(&dst, peer)) {
568         if (bundle->ncp.ipcp.ns.dns[1].s_addr == INADDR_NONE)
569           continue;
570         rt_Set(bundle, RTM_DELETE, &r->dst, NULL, 1, 0);
571         if (r->type & ROUTE_GWHISADDR)
572           ncpaddr_copy(&r->gw, peer);
573       } else if ((r->type & ROUTE_GWHISADDR) && !ncpaddr_equal(&r->gw, peer))
574         ncpaddr_copy(&r->gw, peer);
575 #ifndef NOINET6
576     } else if (ncpaddr_family(me) == AF_INET6) {
577       if ((r->type & ROUTE_DSTMYADDR6) && !ncpaddr_equal(&dst, me)) {
578         rt_Set(bundle, RTM_DELETE, &r->dst, NULL, 1, 0);
579         ncprange_sethost(&r->dst, me);
580         if (r->type & ROUTE_GWHISADDR)
581           ncpaddr_copy(&r->gw, peer);
582       } else if ((r->type & ROUTE_DSTHISADDR6) && !ncpaddr_equal(&dst, peer)) {
583         rt_Set(bundle, RTM_DELETE, &r->dst, NULL, 1, 0);
584         ncprange_sethost(&r->dst, peer);
585         if (r->type & ROUTE_GWHISADDR)
586           ncpaddr_copy(&r->gw, peer);
587       } else if ((r->type & ROUTE_GWHISADDR6) && !ncpaddr_equal(&r->gw, peer))
588         ncpaddr_copy(&r->gw, peer);
589 #endif
590     }
591     rt_Set(bundle, RTM_ADD, &r->dst, &r->gw, 1, 0);
592   }
593 }
594
595 void
596 route_Add(struct sticky_route **rp, int type, const struct ncprange *dst,
597           const struct ncpaddr *gw)
598 {
599   struct sticky_route *r;
600   int dsttype = type & ROUTE_DSTANY;
601
602   r = NULL;
603   while (*rp) {
604     if ((dsttype && dsttype == ((*rp)->type & ROUTE_DSTANY)) ||
605         (!dsttype && ncprange_equal(&(*rp)->dst, dst))) {
606       /* Oops, we already have this route - unlink it */
607       free(r);                  /* impossible really  */
608       r = *rp;
609       *rp = r->next;
610     } else
611       rp = &(*rp)->next;
612   }
613
614   if (!r)
615     r = (struct sticky_route *)malloc(sizeof(struct sticky_route));
616   r->type = type;
617   r->next = NULL;
618   ncprange_copy(&r->dst, dst);
619   ncpaddr_copy(&r->gw, gw);
620   *rp = r;
621 }
622
623 void
624 route_Delete(struct sticky_route **rp, int type, const struct ncprange *dst)
625 {
626   struct sticky_route *r;
627   int dsttype = type & ROUTE_DSTANY;
628
629   for (; *rp; rp = &(*rp)->next) {
630     if ((dsttype && dsttype == ((*rp)->type & ROUTE_DSTANY)) ||
631         (!dsttype && ncprange_equal(dst, &(*rp)->dst))) {
632       r = *rp;
633       *rp = r->next;
634       free(r);
635       break;
636     }
637   }
638 }
639
640 void
641 route_DeleteAll(struct sticky_route **rp)
642 {
643   struct sticky_route *r, *rn;
644
645   for (r = *rp; r; r = rn) {
646     rn = r->next;
647     free(r);
648   }
649   *rp = NULL;
650 }
651
652 void
653 route_ShowSticky(struct prompt *p, struct sticky_route *r, const char *tag,
654                  int indent)
655 {
656   int tlen = strlen(tag);
657
658   if (tlen + 2 > indent)
659     prompt_Printf(p, "%s:\n%*s", tag, indent, "");
660   else
661     prompt_Printf(p, "%s:%*s", tag, indent - tlen - 1, "");
662
663   for (; r; r = r->next) {
664     prompt_Printf(p, "%*sadd ", tlen ? 0 : indent, "");
665     tlen = 0;
666     if (r->type & ROUTE_DSTMYADDR)
667       prompt_Printf(p, "MYADDR");
668     else if (r->type & ROUTE_DSTMYADDR6)
669       prompt_Printf(p, "MYADDR6");
670     else if (r->type & ROUTE_DSTHISADDR)
671       prompt_Printf(p, "HISADDR");
672     else if (r->type & ROUTE_DSTHISADDR6)
673       prompt_Printf(p, "HISADDR6");
674     else if (r->type & ROUTE_DSTDNS0)
675       prompt_Printf(p, "DNS0");
676     else if (r->type & ROUTE_DSTDNS1)
677       prompt_Printf(p, "DNS1");
678     else if (ncprange_isdefault(&r->dst))
679       prompt_Printf(p, "default");
680     else
681       prompt_Printf(p, "%s", ncprange_ntoa(&r->dst));
682
683     if (r->type & ROUTE_GWHISADDR)
684       prompt_Printf(p, " HISADDR\n");
685     else if (r->type & ROUTE_GWHISADDR6)
686       prompt_Printf(p, " HISADDR6\n");
687     else
688       prompt_Printf(p, " %s\n", ncpaddr_ntoa(&r->gw));
689   }
690 }
691
692 struct rtmsg {
693   struct rt_msghdr m_rtm;
694   char m_space[256];
695 };
696
697 static size_t
698 memcpy_roundup(char *cp, const void *data, size_t len)
699 {
700   size_t padlen;
701
702   padlen = ROUNDUP(len);
703   memcpy(cp, data, len);
704   if (padlen > len)
705     memset(cp + len, '\0', padlen - len);
706
707   return padlen;
708 }
709
710 int
711 rt_Set(struct bundle *bundle, int cmd, const struct ncprange *dst,
712        const struct ncpaddr *gw, int bang, int quiet)
713 {
714   struct rtmsg rtmes;
715   int s, nb, wb;
716   char *cp;
717   const char *cmdstr;
718   struct sockaddr_storage sadst, samask, sagw;
719   int result = 1;
720
721   if (bang)
722     cmdstr = (cmd == RTM_ADD ? "Add!" : "Delete!");
723   else
724     cmdstr = (cmd == RTM_ADD ? "Add" : "Delete");
725   s = ID0socket(PF_ROUTE, SOCK_RAW, 0);
726   if (s < 0) {
727     log_Printf(LogERROR, "rt_Set: socket(): %s\n", strerror(errno));
728     return result;
729   }
730   memset(&rtmes, '\0', sizeof rtmes);
731   rtmes.m_rtm.rtm_version = RTM_VERSION;
732   rtmes.m_rtm.rtm_type = cmd;
733   rtmes.m_rtm.rtm_addrs = RTA_DST;
734   rtmes.m_rtm.rtm_seq = ++bundle->routing_seq;
735   rtmes.m_rtm.rtm_pid = getpid();
736   rtmes.m_rtm.rtm_flags = RTF_UP | RTF_GATEWAY | RTF_STATIC;
737
738   if (cmd == RTM_ADD) {
739     if (bundle->ncp.cfg.sendpipe > 0) {
740       rtmes.m_rtm.rtm_rmx.rmx_sendpipe = bundle->ncp.cfg.sendpipe;
741       rtmes.m_rtm.rtm_inits |= RTV_SPIPE;
742     }
743     if (bundle->ncp.cfg.recvpipe > 0) {
744       rtmes.m_rtm.rtm_rmx.rmx_recvpipe = bundle->ncp.cfg.recvpipe;
745       rtmes.m_rtm.rtm_inits |= RTV_RPIPE;
746     }
747   }
748
749   ncprange_getsa(dst, &sadst, &samask);
750
751   cp = rtmes.m_space;
752   cp += memcpy_roundup(cp, &sadst, sadst.ss_len);
753   if (cmd == RTM_ADD) {
754     if (gw == NULL) {
755       log_Printf(LogERROR, "rt_Set: Program error\n");
756       close(s);
757       return result;
758     }
759     ncpaddr_getsa(gw, &sagw);
760     if (ncpaddr_isdefault(gw)) {
761       if (!quiet)
762         log_Printf(LogERROR, "rt_Set: Cannot add a route with"
763                    " gateway 0.0.0.0\n");
764       close(s);
765       return result;
766     } else {
767       cp += memcpy_roundup(cp, &sagw, sagw.ss_len);
768       rtmes.m_rtm.rtm_addrs |= RTA_GATEWAY;
769     }
770   }
771
772   if (!ncprange_ishost(dst)) {
773     cp += memcpy_roundup(cp, &samask, samask.ss_len);
774     rtmes.m_rtm.rtm_addrs |= RTA_NETMASK;
775   }
776
777   nb = cp - (char *)&rtmes;
778   rtmes.m_rtm.rtm_msglen = nb;
779   wb = ID0write(s, &rtmes, nb);
780   if (wb < 0) {
781     log_Printf(LogTCPIP, "rt_Set failure:\n");
782     log_Printf(LogTCPIP, "rt_Set:  Cmd = %s\n", cmdstr);
783     log_Printf(LogTCPIP, "rt_Set:  Dst = %s\n", ncprange_ntoa(dst));
784     if (gw != NULL)
785       log_Printf(LogTCPIP, "rt_Set:  Gateway = %s\n", ncpaddr_ntoa(gw));
786 failed:
787     if (cmd == RTM_ADD && (rtmes.m_rtm.rtm_errno == EEXIST ||
788                            (rtmes.m_rtm.rtm_errno == 0 && errno == EEXIST))) {
789       if (!bang) {
790         log_Printf(LogWARN, "Add route failed: %s already exists\n",
791                    ncprange_ntoa(dst));
792         result = 0;     /* Don't add to our dynamic list */
793       } else {
794         rtmes.m_rtm.rtm_type = cmd = RTM_CHANGE;
795         if ((wb = ID0write(s, &rtmes, nb)) < 0)
796           goto failed;
797       }
798     } else if (cmd == RTM_DELETE &&
799              (rtmes.m_rtm.rtm_errno == ESRCH ||
800               (rtmes.m_rtm.rtm_errno == 0 && errno == ESRCH))) {
801       if (!bang)
802         log_Printf(LogWARN, "Del route failed: %s: Non-existent\n",
803                   ncprange_ntoa(dst));
804     } else if (rtmes.m_rtm.rtm_errno == 0) {
805       if (!quiet || errno != ENETUNREACH)
806         log_Printf(LogWARN, "%s route failed: %s: errno: %s\n", cmdstr,
807                    ncprange_ntoa(dst), strerror(errno));
808     } else
809       log_Printf(LogWARN, "%s route failed: %s: %s\n",
810                  cmdstr, ncprange_ntoa(dst), strerror(rtmes.m_rtm.rtm_errno));
811   }
812
813   if (log_IsKept(LogDEBUG)) {
814     char gwstr[40];
815
816     if (gw)
817       snprintf(gwstr, sizeof gwstr, "%s", ncpaddr_ntoa(gw));
818     else
819       snprintf(gwstr, sizeof gwstr, "<none>");
820     log_Printf(LogDEBUG, "wrote %d: cmd = %s, dst = %s, gateway = %s\n",
821                wb, cmdstr, ncprange_ntoa(dst), gwstr);
822   }
823   close(s);
824
825   return result;
826 }
827
828 void
829 rt_Update(struct bundle *bundle, const struct sockaddr *dst,
830           const struct sockaddr *gw, const struct sockaddr *mask)
831 {
832   struct ncprange ncpdst;
833   struct rtmsg rtmes;
834   char *p;
835   int s, wb;
836
837   s = ID0socket(PF_ROUTE, SOCK_RAW, 0);
838   if (s < 0) {
839     log_Printf(LogERROR, "rt_Update: socket(): %s\n", strerror(errno));
840     return;
841   }
842
843   memset(&rtmes, '\0', sizeof rtmes);
844   rtmes.m_rtm.rtm_version = RTM_VERSION;
845   rtmes.m_rtm.rtm_type = RTM_CHANGE;
846   rtmes.m_rtm.rtm_addrs = 0;
847   rtmes.m_rtm.rtm_seq = ++bundle->routing_seq;
848   rtmes.m_rtm.rtm_pid = getpid();
849   rtmes.m_rtm.rtm_flags = RTF_UP | RTF_STATIC;
850
851   if (bundle->ncp.cfg.sendpipe > 0) {
852     rtmes.m_rtm.rtm_rmx.rmx_sendpipe = bundle->ncp.cfg.sendpipe;
853     rtmes.m_rtm.rtm_inits |= RTV_SPIPE;
854   }
855
856   if (bundle->ncp.cfg.recvpipe > 0) {
857     rtmes.m_rtm.rtm_rmx.rmx_recvpipe = bundle->ncp.cfg.recvpipe;
858     rtmes.m_rtm.rtm_inits |= RTV_RPIPE;
859   }
860
861   rtmes.m_rtm.rtm_rmx.rmx_mtu = bundle->iface->mtu;
862   rtmes.m_rtm.rtm_inits |= RTV_MTU;
863   p = rtmes.m_space;
864
865   if (dst) {
866     rtmes.m_rtm.rtm_addrs |= RTA_DST;
867     p += memcpy_roundup(p, dst, dst->sa_len);
868   }
869
870   rtmes.m_rtm.rtm_addrs |= RTA_GATEWAY;
871   p += memcpy_roundup(p, gw, gw->sa_len);
872   if (mask) {
873     rtmes.m_rtm.rtm_addrs |= RTA_NETMASK;
874     p += memcpy_roundup(p, mask, mask->sa_len);
875   }
876
877   rtmes.m_rtm.rtm_msglen = p - (char *)&rtmes;
878
879   wb = ID0write(s, &rtmes, rtmes.m_rtm.rtm_msglen);
880   if (wb < 0) {
881     ncprange_setsa(&ncpdst, dst, mask);
882
883     log_Printf(LogTCPIP, "rt_Update failure:\n");
884     log_Printf(LogTCPIP, "rt_Update:  Dst = %s\n", ncprange_ntoa(&ncpdst));
885
886     if (rtmes.m_rtm.rtm_errno == 0)
887       log_Printf(LogWARN, "%s: Change route failed: errno: %s\n",
888                  ncprange_ntoa(&ncpdst), strerror(errno));
889     else
890       log_Printf(LogWARN, "%s: Change route failed: %s\n",
891                  ncprange_ntoa(&ncpdst), strerror(rtmes.m_rtm.rtm_errno));
892   }
893   close(s);
894 }