/linux-2.6.21.1-rsbac-1.3.4/rsbac/help/net_helpers.c

Go to the documentation of this file.
00001 /*
00002  * net_helpers.c: Helper functions for the Network.
00003  *
00004  * Author and Copyright (C) 1999-2005 Amon Ott (ao@rsbac.org)
00005  *
00006  *      This program is free software; you can redistribute it and/or
00007  *      modify it under the terms of the GNU General Public License as
00008  *      published by the Free Software Foundation, version 2.
00009  *
00010  * Last modified 24/04/2002.
00011  */
00012 
00013 #include <rsbac/types.h>
00014 #ifdef __KERNEL__
00015 #include <rsbac/network.h>
00016 #endif
00017 
00018 static __u32 ipv4_mask[32] = {
00019   0x00000000, 0x00000080, 0x000000C0, 0x000000E0,
00020   0x000000F0, 0x000000F8, 0x000000FC, 0x000000FE,
00021   0x000000FF, 0x000080FF, 0x0000C0FF, 0x0000E0FF,
00022   0x0000F0FF, 0x0000F8FF, 0x0000FCFF, 0x0000FEFF,
00023   0x0000FFFF, 0x0080FFFF, 0x00C0FFFF, 0x00E0FFFF,
00024   0x00F0FFFF, 0x00F8FFFF, 0x00FCFFFF, 0x00FEFFFF,
00025   0x00FFFFFF, 0x80FFFFFF, 0xC0FFFFFF, 0xE0FFFFFF,
00026   0xF0FFFFFF, 0xF8FFFFFF, 0xFCFFFFFF, 0xFEFFFFFF
00027 };
00028 
00029 static inline __u32 rsbac_net_make_mask_u32(__u8 bits)
00030 {                               
00031         if (bits >= 32)         
00032                 return (__u32)-1UL;
00033         return ipv4_mask[bits];
00034 }
00035 
00036 #ifdef __KERNEL__
00037 /* The lookup data param is always second, so we use it as description here! */
00038 int rsbac_net_compare_data(void *data1, void *data2)
00039 {
00040         struct rsbac_net_temp_data_t *temp = data1;
00041         struct rsbac_net_description_t *desc = data2;
00042 
00043         if (!temp || !desc)
00044                 return 1;
00045         if ((temp->address_family != RSBAC_NET_ANY)
00046             && (temp->address_family != desc->address_family)
00047             )
00048                 return 1;
00049         switch (desc->address_family) {
00050         case AF_INET:
00051                 {
00052                         __u32 mask;
00053                         int i;
00054 
00055                         if(temp->address.inet.nr_addr == 0)
00056                                 return 1;
00057                         if ((temp->type != RSBAC_NET_ANY)
00058                             && (desc->type != temp->type)
00059                             )
00060                                 return 1;
00061                         if ((temp->protocol != RSBAC_NET_ANY)
00062                             && (desc->protocol != temp->protocol)
00063                             )
00064                                 return 1;
00065                         if(temp->ports.nr_ports > 0) {
00066                                 i=0;
00067                                 while(i < temp->ports.nr_ports) {
00068                                         if ((desc->port >= temp->ports.ports[i].min)
00069                                         && (desc->port <= temp->ports.ports[i].max))
00070                                                 break;
00071                                         i++;
00072                                 }
00073                                 if(i == temp->ports.nr_ports)
00074                                         return 1;
00075                         }
00076                         if (temp->netdev[0]
00077                             && (!desc->netdev[0]
00078                                 || strncmp(desc->netdev, temp->netdev,
00079                                            RSBAC_IFNAMSIZ))
00080                             )
00081                                 return 1;
00082                         if (!desc->address)
00083                                 return 1;
00084                         i=0;
00085                         while(i < temp->address.inet.nr_addr) {
00086                                 mask = rsbac_net_make_mask_u32(temp->address.inet.valid_bits[i]);
00087                                 if ((((*(__u32 *) desc->address) & mask) ==
00088                                         (temp->address.inet.addr[i] & mask))
00089                                     )
00090                                     return 0;
00091                                 i++;
00092                         }
00093                         return 1;
00094                 }
00095 
00096                 /* Other address families: only socket type checks for now */
00097         default:
00098                 if ((temp->type != RSBAC_NET_ANY)
00099                     && (desc->type != temp->type)
00100                     )
00101                         return 1;
00102                 return 0;
00103         }
00104         return 1;
00105 }
00106 #endif

Generated on Wed May 16 11:53:48 2007 for RSBAC by  doxygen 1.5.1