root/net/ipv4/devinet.c

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

DEFINITIONS

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

   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/types.h>
  22 #include <linux/kernel.h>
  23 #include <linux/sched.h>
  24 #include <linux/string.h>
  25 #include <linux/mm.h>
  26 #include <linux/socket.h>
  27 #include <linux/sockios.h>
  28 #include <linux/in.h>
  29 #include <linux/errno.h>
  30 #include <linux/interrupt.h>
  31 #include <linux/if_ether.h>
  32 #include <linux/inet.h>
  33 #include <linux/netdevice.h>
  34 #include <linux/etherdevice.h>
  35 #include <net/ip.h>
  36 #include <net/route.h>
  37 #include <net/protocol.h>
  38 #include <net/tcp.h>
  39 #include <linux/skbuff.h>
  40 #include <net/sock.h>
  41 #include <net/arp.h>
  42 
  43 /* 
  44  *      Determine a default network mask, based on the IP address. 
  45  */
  46  
  47 unsigned long ip_get_mask(unsigned long addr)
     /* [previous][next][first][last][top][bottom][index][help] */
  48 {
  49         unsigned long dst;
  50 
  51         if (addr == 0L) 
  52                 return(0L);     /* special case */
  53 
  54         dst = ntohl(addr);
  55         if (IN_CLASSA(dst)) 
  56                 return(htonl(IN_CLASSA_NET));
  57         if (IN_CLASSB(dst)) 
  58                 return(htonl(IN_CLASSB_NET));
  59         if (IN_CLASSC(dst)) 
  60                 return(htonl(IN_CLASSC_NET));
  61   
  62         /*
  63          *      Something else, probably a multicast. 
  64          */
  65          
  66         return(0);
  67 }
  68 
  69 /* 
  70  *      Check the address for our address, broadcasts, etc. 
  71  *
  72  *      I intend to fix this to at the very least cache the last
  73  *      resolved entry.
  74  */
  75  
  76 int ip_chk_addr(unsigned long addr)
     /* [previous][next][first][last][top][bottom][index][help] */
  77 {
  78         struct device *dev;
  79         unsigned long mask;
  80 
  81         /* 
  82          *      Accept both `all ones' and `all zeros' as BROADCAST. 
  83          *      (Support old BSD in other words). This old BSD 
  84          *      support will go very soon as it messes other things
  85          *      up.
  86          *      Also accept `loopback broadcast' as BROADCAST.
  87          */
  88 
  89         if (addr == INADDR_ANY || addr == INADDR_BROADCAST ||
  90             addr == htonl(0x7FFFFFFFL))
  91                 return IS_BROADCAST;
  92 
  93         mask = ip_get_mask(addr);
  94 
  95         /*
  96          *      Accept all of the `loopback' class A net. 
  97          */
  98          
  99         if ((addr & mask) == htonl(0x7F000000L))
 100                 return IS_MYADDR;
 101 
 102         /*
 103          *      OK, now check the interface addresses. We could
 104          *      speed this by keeping a dev and a dev_up chain.
 105          */
 106          
 107         for (dev = dev_base; dev != NULL; dev = dev->next) 
 108         {
 109                 if ((!(dev->flags & IFF_UP)) || dev->family!=AF_INET)
 110                         continue;
 111                 /*
 112                  *      If the protocol address of the device is 0 this is special
 113                  *      and means we are address hunting (eg bootp).
 114                  */
 115                  
 116                 if (dev->pa_addr == 0)
 117                         return IS_MYADDR;
 118                 /*
 119                  *      Is it the exact IP address? 
 120                  */
 121                  
 122                 if (addr == dev->pa_addr)
 123                         return IS_MYADDR;
 124                 /*
 125                  *      Is it our broadcast address? 
 126                  */
 127                  
 128                 if ((dev->flags & IFF_BROADCAST) && addr == dev->pa_brdaddr)
 129                         return IS_BROADCAST;
 130                 /*
 131                  *      Nope. Check for a subnetwork broadcast. 
 132                  */
 133                  
 134                 if (((addr ^ dev->pa_addr) & dev->pa_mask) == 0) 
 135                 {
 136                         if ((addr & ~dev->pa_mask) == 0)
 137                                 return IS_BROADCAST;
 138                         if ((addr & ~dev->pa_mask) == ~dev->pa_mask)
 139                                 return IS_BROADCAST;
 140                 }
 141                 
 142                 /*
 143                  *      Nope. Check for Network broadcast. 
 144                  */
 145                  
 146                 if (((addr ^ dev->pa_addr) & mask) == 0) 
 147                 {
 148                         if ((addr & ~mask) == 0)
 149                                 return IS_BROADCAST;
 150                         if ((addr & ~mask) == ~mask)
 151                                 return IS_BROADCAST;
 152                 }
 153         }
 154         if(IN_MULTICAST(ntohl(addr)))
 155                 return IS_MULTICAST;
 156         return 0;               /* no match at all */
 157 }
 158 
 159 
 160 /*
 161  *      Retrieve our own address.
 162  *
 163  *      Because the loopback address (127.0.0.1) is already recognized
 164  *      automatically, we can use the loopback interface's address as
 165  *      our "primary" interface.  This is the address used by IP et
 166  *      al when it doesn't know which address to use (i.e. it does not
 167  *      yet know from or to which interface to go...).
 168  */
 169  
 170 unsigned long ip_my_addr(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 171 {
 172         struct device *dev;
 173 
 174         for (dev = dev_base; dev != NULL; dev = dev->next) 
 175         {
 176                 if (dev->flags & IFF_LOOPBACK) 
 177                         return(dev->pa_addr);
 178         }
 179         return(0);
 180 }
 181 
 182 /*
 183  *      Find an interface that can handle addresses for a certain address. 
 184  *
 185  *      This needs optimising, since it's relatively trivial to collapse
 186  *      the two loops into one.
 187  */
 188  
 189 struct device * ip_dev_check(unsigned long addr)
     /* [previous][next][first][last][top][bottom][index][help] */
 190 {
 191         struct device *dev;
 192 
 193         for (dev = dev_base; dev; dev = dev->next) 
 194         {
 195                 if (!(dev->flags & IFF_UP))
 196                         continue;
 197                 if (!(dev->flags & IFF_POINTOPOINT))
 198                         continue;
 199                 if (addr != dev->pa_dstaddr)
 200                         continue;
 201                 return dev;
 202         }
 203         for (dev = dev_base; dev; dev = dev->next) 
 204         {
 205                 if (!(dev->flags & IFF_UP))
 206                         continue;
 207                 if (dev->flags & IFF_POINTOPOINT)
 208                         continue;
 209                 if (dev->pa_mask & (addr ^ dev->pa_addr))
 210                         continue;
 211                 return dev;
 212         }
 213         return NULL;
 214 }
 215 
 216 /*
 217  *      Find the first device with a given source address.
 218  */
 219  
 220 struct device *ip_dev_find(unsigned long addr)
     /* [previous][next][first][last][top][bottom][index][help] */
 221 {
 222         struct device *dev;
 223         for(dev = dev_base; dev; dev=dev->next)
 224         {
 225                 if((dev->flags&IFF_UP) && dev->pa_addr==addr)
 226                         return dev;
 227         }
 228         return NULL;
 229 }
 230 
 231 struct device *dev_getbytype(unsigned short type)
     /* [previous][next][first][last][top][bottom][index][help] */
 232 {
 233         struct device *dev;
 234 
 235         for (dev = dev_base; dev != NULL; dev = dev->next) 
 236         {
 237                 if (dev->type == type && !(dev->flags&(IFF_LOOPBACK|IFF_NOARP)))
 238                         return(dev);
 239         }
 240         return(NULL);
 241 }
 242 

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