root/net/inet/route.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. rt_print
  2. rt_del
  3. rt_flush
  4. guess_mask
  5. get_gw_dev
  6. rt_add
  7. rt_new
  8. rt_kill
  9. rt_get_info
  10. rt_route
  11. rt_ioctl

   1 /*
   2  * INET         An implementation of the TCP/IP protocol suite for the LINUX
   3  *              operating system.  INET is implemented using the  BSD Socket
   4  *              interface as the means of communication with the user level.
   5  *
   6  *              ROUTE - implementation of the IP router.
   7  *
   8  * Version:     @(#)route.c     1.0.14  05/31/93
   9  *
  10  * Authors:     Ross Biro, <bir7@leland.Stanford.Edu>
  11  *              Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
  12  *
  13  * Fixes:
  14  *              Alan Cox        :       Verify area fixes.
  15  *              Alan Cox        :       cli() protects routing changes
  16  *              Rui Oliveira    :       ICMP routing table updates
  17  *              (rco@di.uminho.pt)      Routing table insertion and update
  18  *
  19  *              This program is free software; you can redistribute it and/or
  20  *              modify it under the terms of the GNU General Public License
  21  *              as published by the Free Software Foundation; either version
  22  *              2 of the License, or (at your option) any later version.
  23  */
  24 #include <asm/segment.h>
  25 #include <asm/system.h>
  26 #include <linux/types.h>
  27 #include <linux/kernel.h>
  28 #include <linux/sched.h>
  29 #include <linux/string.h>
  30 #include <linux/socket.h>
  31 #include <linux/sockios.h>
  32 #include <linux/errno.h>
  33 #include <linux/in.h>
  34 #include "inet.h"
  35 #include "dev.h"
  36 #include "ip.h"
  37 #include "protocol.h"
  38 #include "route.h"
  39 #include "tcp.h"
  40 #include "skbuff.h"
  41 #include "sock.h"
  42 #include "arp.h"
  43 #include "icmp.h"
  44 
  45 
  46 static struct rtable *rt_base = NULL;
  47 static struct rtable *rt_loopback = NULL;
  48 
  49 /* Dump the contents of a routing table entry. */
  50 static void
  51 rt_print(struct rtable *rt)
     /* [previous][next][first][last][top][bottom][index][help] */
  52 {
  53   if (rt == NULL || inet_debug != DBG_RT) return;
  54 
  55   printk("RT: %06lx NXT=%06lx FLAGS=0x%02x\n",
  56                 (long) rt, (long) rt->rt_next, rt->rt_flags);
  57   printk("    TARGET=%s ", in_ntoa(rt->rt_dst));
  58   printk("GW=%s ", in_ntoa(rt->rt_gateway));
  59   printk("    DEV=%s USE=%ld REF=%d\n",
  60         (rt->rt_dev == NULL) ? "NONE" : rt->rt_dev->name,
  61         rt->rt_use, rt->rt_refcnt);
  62 }
  63 
  64 
  65 /*
  66  * Remove a routing table entry.
  67  */
  68 static void rt_del(unsigned long dst)
     /* [previous][next][first][last][top][bottom][index][help] */
  69 {
  70         struct rtable *r, **rp;
  71         unsigned long flags;
  72 
  73         DPRINTF((DBG_RT, "RT: flushing for dst %s\n", in_ntoa(dst)));
  74         rp = &rt_base;
  75         save_flags(flags);
  76         cli();
  77         while((r = *rp) != NULL) {
  78                 if (r->rt_dst != dst) {
  79                         rp = &r->rt_next;
  80                         continue;
  81                 }
  82                 *rp = r->rt_next;
  83                 if (rt_loopback == r)
  84                         rt_loopback = NULL;
  85                 kfree_s(r, sizeof(struct rtable));
  86         } 
  87         restore_flags(flags);
  88 }
  89 
  90 
  91 /*
  92  * Remove all routing table entries for a device.
  93  */
  94 void rt_flush(struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
  95 {
  96         struct rtable *r;
  97         struct rtable **rp;
  98         unsigned long flags;
  99 
 100         DPRINTF((DBG_RT, "RT: flushing for dev 0x%08lx (%s)\n", (long)dev, dev->name));
 101         rp = &rt_base;
 102         cli();
 103         save_flags(flags);
 104         while ((r = *rp) != NULL) {
 105                 if (r->rt_dev != dev) {
 106                         rp = &r->rt_next;
 107                         continue;
 108                 }
 109                 *rp = r->rt_next;
 110                 if (rt_loopback == r)
 111                         rt_loopback = NULL;
 112                 kfree_s(r, sizeof(struct rtable));
 113         } 
 114         restore_flags(flags);
 115 }
 116 
 117 /*
 118  * Used by 'rt_add()' when we can't get the netmask from the device..
 119  *
 120  * If the lower byte or two are zero, we guess the mask based on the
 121  * number of zero 8-bit net numbers, otherwise we use the "default"
 122  * masks judging by the destination address.
 123  *
 124  * We should really use masks everywhere, but the current system
 125  * interface for adding routes doesn't even contain a netmask field.
 126  * Similarly, ICMP redirect messages contain only the address to
 127  * redirect.. Anyway, this function should give reasonable values
 128  * for almost anything.
 129  */
 130 static unsigned long guess_mask(unsigned long dst)
     /* [previous][next][first][last][top][bottom][index][help] */
 131 {
 132         unsigned long mask = 0xffffffff;
 133 
 134         while (mask & dst)
 135                 mask <<= 8;
 136         if (mask)
 137                 return ~mask;
 138         dst = ntohl(dst);
 139         if (IN_CLASSA(dst))
 140                 return htonl(IN_CLASSA_NET);
 141         if (IN_CLASSB(dst))
 142                 return htonl(IN_CLASSB_NET);
 143         return htonl(IN_CLASSC_NET);
 144 }
 145 
 146 static inline struct device * get_gw_dev(unsigned long gw)
     /* [previous][next][first][last][top][bottom][index][help] */
 147 {
 148         struct rtable * rt;
 149 
 150         for (rt = rt_base ; rt ; rt = rt->rt_next) {
 151                 if ((gw ^ rt->rt_dst) & rt->rt_mask)
 152                         continue;
 153                 /* gateways behind gateways are a no-no */
 154                 if (rt->rt_flags & RTF_GATEWAY)
 155                         return NULL;
 156                 return rt->rt_dev;
 157         }
 158         return NULL;
 159 }
 160 
 161 /*
 162  * rewrote rt_add(), as the old one was weird. Linus
 163  */
 164 void
 165 rt_add(short flags, unsigned long dst, unsigned long gw, struct device *dev)
     /* [previous][next][first][last][top][bottom][index][help] */
 166 {
 167         struct rtable *r, *rt;
 168         struct rtable **rp;
 169         unsigned long mask;
 170         unsigned long cpuflags;
 171 
 172         if (flags & RTF_HOST) {
 173                 mask = 0xffffffff;
 174         } else {
 175                 if (!((dst ^ dev->pa_addr) & dev->pa_mask)) {
 176                         mask = dev->pa_mask;
 177                         flags &= ~RTF_GATEWAY;
 178                         if (flags & RTF_DYNAMIC) {
 179                                 /*printk("Dynamic route to my own net rejected\n");*/
 180                                 return;
 181                         }
 182                 } else
 183                         mask = guess_mask(dst);
 184                 dst &= mask;
 185         }
 186         if (gw == dev->pa_addr)
 187                 flags &= ~RTF_GATEWAY;
 188         if (flags & RTF_GATEWAY) {
 189                 /* don't try to add a gateway we can't reach.. */
 190                 if (dev != get_gw_dev(gw))
 191                         return;
 192                 flags |= RTF_GATEWAY;
 193         } else
 194                 gw = 0;
 195         /* Allocate an entry. */
 196         rt = (struct rtable *) kmalloc(sizeof(struct rtable), GFP_ATOMIC);
 197         if (rt == NULL) {
 198                 DPRINTF((DBG_RT, "RT: no memory for new route!\n"));
 199                 return;
 200         }
 201         memset(rt, 0, sizeof(struct rtable));
 202         rt->rt_flags = flags | RTF_UP;
 203         rt->rt_dst = dst;
 204         rt->rt_dev = dev;
 205         rt->rt_gateway = gw;
 206         rt->rt_mask = mask;
 207         rt_print(rt);
 208         /*
 209          * What we have to do is loop though this until we have
 210          * found the first address which has a higher generality than
 211          * the one in rt.  Then we can put rt in right before it.
 212          */
 213         save_flags(cpuflags);
 214         cli();
 215         /* remove old route if we are getting a duplicate. */
 216         rp = &rt_base;
 217         while ((r = *rp) != NULL) {
 218                 if (r->rt_dst != dst) {
 219                         rp = &r->rt_next;
 220                         continue;
 221                 }
 222                 *rp = r->rt_next;
 223                 if (rt_loopback == r)
 224                         rt_loopback = NULL;
 225                 kfree_s(r, sizeof(struct rtable));
 226         }
 227         /* add the new route */
 228         rp = &rt_base;
 229         while ((r = *rp) != NULL) {
 230                 if ((r->rt_mask & mask) != mask)
 231                         break;
 232                 rp = &r->rt_next;
 233         }
 234         rt->rt_next = r;
 235         *rp = rt;
 236         if (rt->rt_dev->flags & IFF_LOOPBACK)
 237                 rt_loopback = rt;
 238         restore_flags(cpuflags);
 239         return;
 240 }
 241 
 242 
 243 static int
 244 rt_new(struct rtentry *r)
     /* [previous][next][first][last][top][bottom][index][help] */
 245 {
 246   struct device *dev;
 247 
 248   if ((r->rt_dst.sa_family != AF_INET) ||
 249       (r->rt_gateway.sa_family != AF_INET)) {
 250         DPRINTF((DBG_RT, "RT: We only know about AF_INET !\n"));
 251         return(-EAFNOSUPPORT);
 252   }
 253 
 254   /*
 255    * I admit that the following bits of code were "inspired" by
 256    * the Berkeley UNIX system source code.  I could think of no
 257    * other way to find out how to make it compatible with it (I
 258    * want this to be compatible to get "routed" up and running).
 259    * -FvK
 260    */
 261 
 262   /* If we have a 'gateway' route here, check the correct address. */
 263   if (!(r->rt_flags & RTF_GATEWAY))
 264         dev = dev_check(((struct sockaddr_in *) &r->rt_dst)->sin_addr.s_addr);
 265   else
 266         dev = get_gw_dev(((struct sockaddr_in *) &r->rt_gateway)->sin_addr.s_addr);
 267 
 268   DPRINTF((DBG_RT, "RT: dev for %s gw ",
 269         in_ntoa((*(struct sockaddr_in *)&r->rt_dst).sin_addr.s_addr)));
 270   DPRINTF((DBG_RT, "%s (0x%04X) is 0x%X (%s)\n",
 271         in_ntoa((*(struct sockaddr_in *)&r->rt_gateway).sin_addr.s_addr),
 272         r->rt_flags, dev, (dev == NULL) ? "NONE" : dev->name));
 273 
 274   if (dev == NULL) return(-ENETUNREACH);
 275 
 276   rt_add(r->rt_flags, (*(struct sockaddr_in *) &r->rt_dst).sin_addr.s_addr,
 277          (*(struct sockaddr_in *) &r->rt_gateway).sin_addr.s_addr, dev);
 278 
 279   return(0);
 280 }
 281 
 282 
 283 static int
 284 rt_kill(struct rtentry *r)
     /* [previous][next][first][last][top][bottom][index][help] */
 285 {
 286   struct sockaddr_in *trg;
 287 
 288   trg = (struct sockaddr_in *) &r->rt_dst;
 289   rt_del(trg->sin_addr.s_addr);
 290 
 291   return(0);
 292 }
 293 
 294 
 295 /* Called from the PROCfs module. */
 296 int
 297 rt_get_info(char *buffer)
     /* [previous][next][first][last][top][bottom][index][help] */
 298 {
 299   struct rtable *r;
 300   char *pos;
 301 
 302   pos = buffer;
 303 
 304   pos += sprintf(pos,
 305                  "Iface\tDestination\tGateway \tFlags\tRefCnt\tUse\tMetric\tMask\n");
 306   
 307   /* This isn't quite right -- r->rt_dst is a struct! */
 308   for (r = rt_base; r != NULL; r = r->rt_next) {
 309         pos += sprintf(pos, "%s\t%08lX\t%08lX\t%02X\t%d\t%lu\t%d\t%08lX\n",
 310                 r->rt_dev->name, r->rt_dst, r->rt_gateway,
 311                 r->rt_flags, r->rt_refcnt, r->rt_use, r->rt_metric,
 312                 r->rt_mask);
 313   }
 314   return(pos - buffer);
 315 }
 316 
 317 /*
 318  * This is hackish, but results in better code. Use "-S" to see why.
 319  */
 320 #define early_out ({ goto no_route; 1; })
 321 
 322 struct rtable * rt_route(unsigned long daddr, struct options *opt)
     /* [previous][next][first][last][top][bottom][index][help] */
 323 {
 324         struct rtable *rt;
 325 
 326         for (rt = rt_base; rt != NULL || early_out ; rt = rt->rt_next) {
 327                 if (!((rt->rt_dst ^ daddr) & rt->rt_mask))
 328                         break;
 329                 /* broadcast addresses can be special cases.. */
 330                 if ((rt->rt_dev->flags & IFF_BROADCAST) &&
 331                      rt->rt_dev->pa_brdaddr == daddr)
 332                         break;
 333         }
 334         if (daddr == rt->rt_dev->pa_addr)
 335                 rt = rt_loopback;
 336         rt->rt_use++;
 337         return rt;
 338 no_route:
 339         return NULL;
 340 }
 341 
 342 
 343 int
 344 rt_ioctl(unsigned int cmd, void *arg)
     /* [previous][next][first][last][top][bottom][index][help] */
 345 {
 346   struct device *dev;
 347   struct rtentry rt;
 348   char namebuf[32];
 349   int ret;
 350   int err;
 351 
 352   switch(cmd) {
 353         case DDIOCSDBG:
 354                 ret = dbg_ioctl(arg, DBG_RT);
 355                 break;
 356         case SIOCADDRT:
 357         case SIOCDELRT:
 358                 if (!suser()) return(-EPERM);
 359                 err=verify_area(VERIFY_READ, arg, sizeof(struct rtentry));
 360                 if(err)
 361                         return err;
 362                 memcpy_fromfs(&rt, arg, sizeof(struct rtentry));
 363                 if (rt.rt_dev) {
 364                     err=verify_area(VERIFY_READ, rt.rt_dev, sizeof namebuf);
 365                     if(err)
 366                         return err;
 367                     memcpy_fromfs(&namebuf, rt.rt_dev, sizeof namebuf);
 368                     dev = dev_get(namebuf);
 369                     rt.rt_dev = dev;
 370                 }
 371                 ret = (cmd == SIOCDELRT) ? rt_kill(&rt) : rt_new(&rt);
 372                 break;
 373         default:
 374                 ret = -EINVAL;
 375   }
 376 
 377   return(ret);
 378 }

/* [previous][next][first][last][top][bottom][index][help] */