root/net/inet/devinet.c

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

DEFINITIONS

This source file includes following definitions.
  1. ip_get_mask
  2. ip_addr_match
  3. ip_chk_addr
  4. ip_my_addr
  5. ip_dev_check

   1 /*
   2  *      NET3    IP device support routines.
   3  *
   4  *              This program is free software; you can redistribute it and/or
   5  *              modify it under the terms of the GNU General Public License
   6  *              as published by the Free Software Foundation; either version
   7  *              2 of the License, or (at your option) any later version.
   8  *
   9  *      Derived from the IP parts of dev.c 1.0.19
  10  *              Authors:        Ross Biro, <bir7@leland.Stanford.Edu>
  11  *                              Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
  12  *                              Mark Evans, <evansmp@uhura.aston.ac.uk>
  13  *
  14  *      Additional Authors:
  15  *              Alan Cox, <gw4pts@gw4pts.ampr.org>
  16  */
  17  
  18 #include <asm/segment.h>
  19 #include <asm/system.h>
  20 #include <asm/bitops.h>
  21 #include <linux/config.h>
  22 #include <linux/types.h>
  23 #include <linux/kernel.h>
  24 #include <linux/sched.h>
  25 #include <linux/string.h>
  26 #include <linux/mm.h>
  27 #include <linux/socket.h>
  28 #include <linux/sockios.h>
  29 #include <linux/in.h>
  30 #include <linux/errno.h>
  31 #include <linux/interrupt.h>
  32 #include <linux/if_ether.h>
  33 #include <linux/inet.h>
  34 #include <linux/netdevice.h>
  35 #include <linux/etherdevice.h>
  36 #include "ip.h"
  37 #include "route.h"
  38 #include "protocol.h"
  39 #include "tcp.h"
  40 #include <linux/skbuff.h>
  41 #include "sock.h"
  42 #include "arp.h"
  43 
  44 /* 
  45  *      Determine a default network mask, based on the IP address. 
  46  */
  47  
  48 unsigned long ip_get_mask(unsigned long addr)
     /* [previous][next][first][last][top][bottom][index][help] */
  49 {
  50         unsigned long dst;
  51 
  52         if (addr == 0L) 
  53                 return(0L);     /* special case */
  54 
  55         dst = ntohl(addr);
  56         if (IN_CLASSA(dst)) 
  57                 return(htonl(IN_CLASSA_NET));
  58         if (IN_CLASSB(dst)) 
  59                 return(htonl(IN_CLASSB_NET));
  60         if (IN_CLASSC(dst)) 
  61                 return(htonl(IN_CLASSC_NET));
  62   
  63         /*
  64          *      Something else, probably a multicast. 
  65          */
  66          
  67         return(0);
  68 }
  69 
  70 /*
  71  *      Perform an IP address matching operation
  72  */
  73 
  74 int ip_addr_match(unsigned long me, unsigned long him)
     /* [previous][next][first][last][top][bottom][index][help] */
  75 {
  76         int i;
  77         unsigned long mask=0xFFFFFFFF;
  78 
  79         /*
  80          *      Simple case
  81          */
  82         if (me == him) 
  83                 return(1);
  84                 
  85         /*
  86          *      Look for a match ending in all 1's 
  87          */
  88          
  89         for (i = 0; i < 4; i++, me >>= 8, him >>= 8, mask >>= 8) 
  90         {
  91                 if ((me & 0xFF) != (him & 0xFF)) 
  92                 {
  93                 /*
  94                  * The only way this could be a match is for
  95                  * the rest of addr1 to be 0 or 255.
  96                  */
  97                         if (me != 0 && me != mask) 
  98                                 return(0);
  99                         return(1);
 100                 }
 101         }
 102         return(1);
 103 }
 104 
 105 
 106 /* 
 107  *      Check the address for our address, broadcasts, etc. 
 108  *
 109  *      I intend to fix this to at the very least cache the last
 110  *      resolved entry.
 111  */
 112  
 113 int ip_chk_addr(unsigned long addr)
     /* [previous][next][first][last][top][bottom][index][help] */
 114 {
 115         struct device *dev;
 116         unsigned long mask;
 117 
 118         /* 
 119          *      Accept both `all ones' and `all zeros' as BROADCAST. 
 120          *      (Support old BSD in other words). This old BSD 
 121          *      support will go very soon as it messes other things
 122          *      up.
 123          */
 124 
 125         if (addr == INADDR_ANY || addr == INADDR_BROADCAST)
 126                 return IS_BROADCAST;
 127 
 128         mask = ip_get_mask(addr);
 129 
 130         /*
 131          *      Accept all of the `loopback' class A net. 
 132          */
 133          
 134         if ((addr & mask) == htonl(0x7F000000L))
 135                 return IS_MYADDR;
 136 
 137         /*
 138          *      OK, now check the interface addresses. 
 139          */
 140          
 141         for (dev = dev_base; dev != NULL; dev = dev->next) 
 142         {
 143                 if (!(dev->flags & IFF_UP))
 144                         continue;
 145                 /*
 146                  *      If the protocol address of the device is 0 this is special
 147                  *      and means we are address hunting (eg bootp).
 148                  */
 149                  
 150                 if ((dev->pa_addr == 0)/* || (dev->flags&IFF_PROMISC)*/)
 151                         return IS_MYADDR;
 152                 /*
 153                  *      Is it the exact IP address? 
 154                  */
 155                  
 156                 if (addr == dev->pa_addr)
 157                         return IS_MYADDR;
 158                 /*
 159                  *      Is it our broadcast address? 
 160                  */
 161                  
 162                 if ((dev->flags & IFF_BROADCAST) && addr == dev->pa_brdaddr)
 163                         return IS_BROADCAST;
 164                 /*
 165                  *      Nope. Check for a subnetwork broadcast. 
 166                  */
 167                  
 168                 if (((addr ^ dev->pa_addr) & dev->pa_mask) == 0) 
 169                 {
 170                         if ((addr & ~dev->pa_mask) == 0)
 171                                 return IS_BROADCAST;
 172                         if ((addr & ~dev->pa_mask) == ~dev->pa_mask)
 173                                 return IS_BROADCAST;
 174                 }
 175                 
 176                 /*
 177                  *      Nope. Check for Network broadcast. 
 178                  */
 179                  
 180                 if (((addr ^ dev->pa_addr) & mask) == 0) 
 181                 {
 182                         if ((addr & ~mask) == 0)
 183                                 return IS_BROADCAST;
 184                         if ((addr & ~mask) == ~mask)
 185                                 return IS_BROADCAST;
 186                 }
 187         }
 188         return 0;               /* no match at all */
 189 }
 190 
 191 
 192 /*
 193  *      Retrieve our own address.
 194  *
 195  *      Because the loopback address (127.0.0.1) is already recognized
 196  *      automatically, we can use the loopback interface's address as
 197  *      our "primary" interface.  This is the address used by IP et
 198  *      al when it doesn't know which address to use (i.e. it does not
 199  *      yet know from or to which interface to go...).
 200  */
 201  
 202 unsigned long ip_my_addr(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 203 {
 204         struct device *dev;
 205 
 206         for (dev = dev_base; dev != NULL; dev = dev->next) 
 207         {
 208                 if (dev->flags & IFF_LOOPBACK) 
 209                         return(dev->pa_addr);
 210         }
 211         return(0);
 212 }
 213 
 214 /*
 215  *      Find an interface that can handle addresses for a certain address. 
 216  *
 217  *      This needs optimising, since its relatively trivial to collapse
 218  *      the two loops into one.
 219  */
 220  
 221 struct device * ip_dev_check(unsigned long addr)
     /* [previous][next][first][last][top][bottom][index][help] */
 222 {
 223         struct device *dev;
 224 
 225         for (dev = dev_base; dev; dev = dev->next) 
 226         {
 227                 if (!(dev->flags & IFF_UP))
 228                         continue;
 229                 if (!(dev->flags & IFF_POINTOPOINT))
 230                         continue;
 231                 if (addr != dev->pa_dstaddr)
 232                         continue;
 233                 return dev;
 234         }
 235         for (dev = dev_base; dev; dev = dev->next) 
 236         {
 237                 if (!(dev->flags & IFF_UP))
 238                         continue;
 239                 if (dev->flags & IFF_POINTOPOINT)
 240                         continue;
 241                 if (dev->pa_mask & (addr ^ dev->pa_addr))
 242                         continue;
 243                 return dev;
 244         }
 245         return NULL;
 246 }

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