~ubuntu-branches/ubuntu/hardy/openswan/hardy-updates

« back to all changes in this revision

Viewing changes to linux/net/ipsec/goodmask.c

  • Committer: Bazaar Package Importer
  • Author(s): Rene Mayrhofer
  • Date: 2005-01-27 16:10:11 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20050127161011-idgybmyz3vwhpfiq
Tags: 2.3.0-2
Urgency HIGH due to security issue and problems with build-deps in sarge.
* Fix the security issue. Please see
  http://www.idefense.com/application/poi/display?id=190&
      type=vulnerabilities&flashstatus=false
  for more details. Thanks to Martin Schulze for informing me about
  this issue.
  Closes: #292458: Openswan XAUTH/PAM Buffer Overflow Vulnerability
* Added a Build-Dependency to lynx.
  Closes: #291143: openswan: FTBFS: Missing build dependency.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * minor utilities for subnet-mask manipulation
 
3
 * Copyright (C) 1998, 1999  Henry Spencer.
 
4
 * 
 
5
 * This library is free software; you can redistribute it and/or modify it
 
6
 * under the terms of the GNU Library General Public License as published by
 
7
 * the Free Software Foundation; either version 2 of the License, or (at your
 
8
 * option) any later version.  See <http://www.fsf.org/copyleft/lgpl.txt>.
 
9
 * 
 
10
 * This library is distributed in the hope that it will be useful, but
 
11
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 
12
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
 
13
 * License for more details.
 
14
 *
 
15
 * RCSID $Id: goodmask.c,v 1.12 2004/07/10 07:43:47 mcr Exp $
 
16
 */
 
17
#include "openswan.h"
 
18
 
 
19
#ifndef ABITS
 
20
#define ABITS   32      /* bits in an IPv4 address */
 
21
#endif
 
22
 
 
23
/*
 
24
 - goodmask - is this a good (^1*0*$) subnet mask?
 
25
 * You are not expected to understand this.  See Henry S. Warren Jr, 
 
26
 * "Functions realizable with word-parallel logical and two's-complement
 
27
 * addition instructions", CACM 20.6 (June 1977), p.439.
 
28
 */
 
29
int                             /* predicate */
 
30
goodmask(mask)
 
31
struct in_addr mask;
 
32
{
 
33
        unsigned long x = ntohl(mask.s_addr);
 
34
        /* clear rightmost contiguous string of 1-bits */
 
35
#       define  CRCS1B(x)       (((x|(x-1))+1)&x)
 
36
#       define  TOPBIT          (1UL << 31)
 
37
 
 
38
        /* either zero, or has one string of 1-bits which is left-justified */
 
39
        if (x == 0 || (CRCS1B(x) == 0 && (x&TOPBIT)))
 
40
                return 1;
 
41
        return 0;
 
42
}
 
43
 
 
44
/*
 
45
 - masktobits - how many bits in this mask?
 
46
 * The algorithm is essentially a binary search, but highly optimized
 
47
 * for this particular task.
 
48
 */
 
49
int                             /* -1 means !goodmask() */
 
50
masktobits(mask)
 
51
struct in_addr mask;
 
52
{
 
53
        unsigned long m = ntohl(mask.s_addr);
 
54
        int masklen;
 
55
 
 
56
        if (!goodmask(mask))
 
57
                return -1;
 
58
 
 
59
        if (m&0x00000001UL)
 
60
                return 32;
 
61
        masklen = 0;
 
62
        if (m&(0x0000ffffUL<<1)) {      /* <<1 for 1-origin numbering */
 
63
                masklen |= 0x10;
 
64
                m <<= 16;
 
65
        }
 
66
        if (m&(0x00ff0000UL<<1)) {
 
67
                masklen |= 0x08;
 
68
                m <<= 8;
 
69
        }
 
70
        if (m&(0x0f000000UL<<1)) {
 
71
                masklen |= 0x04;
 
72
                m <<= 4;
 
73
        }
 
74
        if (m&(0x30000000UL<<1)) {
 
75
                masklen |= 0x02;
 
76
                m <<= 2;
 
77
        }
 
78
        if (m&(0x40000000UL<<1))
 
79
                masklen |= 0x01;
 
80
 
 
81
        return masklen;
 
82
}
 
83
 
 
84
/*
 
85
 - bitstomask - return a mask with this many high bits on
 
86
 */
 
87
struct in_addr
 
88
bitstomask(n)
 
89
int n;
 
90
{
 
91
        struct in_addr result;
 
92
 
 
93
        if (n > 0 && n <= ABITS)
 
94
                result.s_addr = htonl(~((1UL << (ABITS - n)) - 1));
 
95
        else if (n == 0)
 
96
                result.s_addr = 0;
 
97
        else
 
98
                result.s_addr = 0;      /* best error report we can do */
 
99
        return result;
 
100
}