00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #include <linux/module.h>
00018 #include <linux/jiffies.h>
00019 #include <linux/kernel.h>
00020 #include <linux/inet.h>
00021 #include <linux/mm.h>
00022 #include <linux/net.h>
00023 #include <linux/string.h>
00024 #include <linux/types.h>
00025 #include <linux/random.h>
00026 #include <linux/percpu.h>
00027 #include <linux/init.h>
00028 #include <net/sock.h>
00029
00030 #include <asm/byteorder.h>
00031 #include <asm/system.h>
00032 #include <asm/uaccess.h>
00033
00034 #ifndef DDE_LINUX
00035 int net_msg_cost __read_mostly = 5*HZ;
00036 DEFINE_RATELIMIT_STATE(net_ratelimit_state, 5 * HZ, 10);
00037 #else
00038 int net_msg_cost = 500;
00039 #endif
00040 int net_msg_burst __read_mostly = 10;
00041 int net_msg_warn __read_mostly = 1;
00042 EXPORT_SYMBOL(net_msg_warn);
00043
00044
00045
00046
00047 int net_ratelimit(void)
00048 {
00049 #ifndef DDE_LINUX
00050 return __ratelimit(&net_ratelimit_state);
00051 #else
00052 return 0;
00053 #endif
00054 }
00055 EXPORT_SYMBOL(net_ratelimit);
00056
00057
00058
00059
00060
00061
00062
00063 __be32 in_aton(const char *str)
00064 {
00065 unsigned long l;
00066 unsigned int val;
00067 int i;
00068
00069 l = 0;
00070 for (i = 0; i < 4; i++)
00071 {
00072 l <<= 8;
00073 if (*str != '\0')
00074 {
00075 val = 0;
00076 while (*str != '\0' && *str != '.' && *str != '\n')
00077 {
00078 val *= 10;
00079 val += *str - '0';
00080 str++;
00081 }
00082 l |= val;
00083 if (*str != '\0')
00084 str++;
00085 }
00086 }
00087 return(htonl(l));
00088 }
00089
00090 EXPORT_SYMBOL(in_aton);
00091
00092 #define IN6PTON_XDIGIT 0x00010000
00093 #define IN6PTON_DIGIT 0x00020000
00094 #define IN6PTON_COLON_MASK 0x00700000
00095 #define IN6PTON_COLON_1 0x00100000
00096 #define IN6PTON_COLON_2 0x00200000
00097 #define IN6PTON_COLON_1_2 0x00400000
00098 #define IN6PTON_DOT 0x00800000
00099 #define IN6PTON_DELIM 0x10000000
00100 #define IN6PTON_NULL 0x20000000
00101 #define IN6PTON_UNKNOWN 0x40000000
00102
00103 static inline int xdigit2bin(char c, int delim)
00104 {
00105 if (c == delim || c == '\0')
00106 return IN6PTON_DELIM;
00107 if (c == ':')
00108 return IN6PTON_COLON_MASK;
00109 if (c == '.')
00110 return IN6PTON_DOT;
00111 if (c >= '0' && c <= '9')
00112 return (IN6PTON_XDIGIT | IN6PTON_DIGIT| (c - '0'));
00113 if (c >= 'a' && c <= 'f')
00114 return (IN6PTON_XDIGIT | (c - 'a' + 10));
00115 if (c >= 'A' && c <= 'F')
00116 return (IN6PTON_XDIGIT | (c - 'A' + 10));
00117 if (delim == -1)
00118 return IN6PTON_DELIM;
00119 return IN6PTON_UNKNOWN;
00120 }
00121
00122 int in4_pton(const char *src, int srclen,
00123 u8 *dst,
00124 int delim, const char **end)
00125 {
00126 const char *s;
00127 u8 *d;
00128 u8 dbuf[4];
00129 int ret = 0;
00130 int i;
00131 int w = 0;
00132
00133 if (srclen < 0)
00134 srclen = strlen(src);
00135 s = src;
00136 d = dbuf;
00137 i = 0;
00138 while(1) {
00139 int c;
00140 c = xdigit2bin(srclen > 0 ? *s : '\0', delim);
00141 if (!(c & (IN6PTON_DIGIT | IN6PTON_DOT | IN6PTON_DELIM | IN6PTON_COLON_MASK))) {
00142 goto out;
00143 }
00144 if (c & (IN6PTON_DOT | IN6PTON_DELIM | IN6PTON_COLON_MASK)) {
00145 if (w == 0)
00146 goto out;
00147 *d++ = w & 0xff;
00148 w = 0;
00149 i++;
00150 if (c & (IN6PTON_DELIM | IN6PTON_COLON_MASK)) {
00151 if (i != 4)
00152 goto out;
00153 break;
00154 }
00155 goto cont;
00156 }
00157 w = (w * 10) + c;
00158 if ((w & 0xffff) > 255) {
00159 goto out;
00160 }
00161 cont:
00162 if (i >= 4)
00163 goto out;
00164 s++;
00165 srclen--;
00166 }
00167 ret = 1;
00168 memcpy(dst, dbuf, sizeof(dbuf));
00169 out:
00170 if (end)
00171 *end = s;
00172 return ret;
00173 }
00174
00175 EXPORT_SYMBOL(in4_pton);
00176
00177 int in6_pton(const char *src, int srclen,
00178 u8 *dst,
00179 int delim, const char **end)
00180 {
00181 const char *s, *tok = NULL;
00182 u8 *d, *dc = NULL;
00183 u8 dbuf[16];
00184 int ret = 0;
00185 int i;
00186 int state = IN6PTON_COLON_1_2 | IN6PTON_XDIGIT | IN6PTON_NULL;
00187 int w = 0;
00188
00189 memset(dbuf, 0, sizeof(dbuf));
00190
00191 s = src;
00192 d = dbuf;
00193 if (srclen < 0)
00194 srclen = strlen(src);
00195
00196 while (1) {
00197 int c;
00198
00199 c = xdigit2bin(srclen > 0 ? *s : '\0', delim);
00200 if (!(c & state))
00201 goto out;
00202 if (c & (IN6PTON_DELIM | IN6PTON_COLON_MASK)) {
00203
00204 if (!(state & IN6PTON_NULL)) {
00205 *d++ = (w >> 8) & 0xff;
00206 *d++ = w & 0xff;
00207 }
00208 w = 0;
00209 if (c & IN6PTON_DELIM) {
00210
00211 break;
00212 }
00213
00214
00215
00216
00217
00218 switch (state & IN6PTON_COLON_MASK) {
00219 case IN6PTON_COLON_2:
00220 dc = d;
00221 state = IN6PTON_XDIGIT | IN6PTON_DELIM;
00222 if (dc - dbuf >= sizeof(dbuf))
00223 state |= IN6PTON_NULL;
00224 break;
00225 case IN6PTON_COLON_1|IN6PTON_COLON_1_2:
00226 state = IN6PTON_XDIGIT | IN6PTON_COLON_2;
00227 break;
00228 case IN6PTON_COLON_1:
00229 state = IN6PTON_XDIGIT;
00230 break;
00231 case IN6PTON_COLON_1_2:
00232 state = IN6PTON_COLON_2;
00233 break;
00234 default:
00235 state = 0;
00236 }
00237 tok = s + 1;
00238 goto cont;
00239 }
00240
00241 if (c & IN6PTON_DOT) {
00242 ret = in4_pton(tok ? tok : s, srclen + (int)(s - tok), d, delim, &s);
00243 if (ret > 0) {
00244 d += 4;
00245 break;
00246 }
00247 goto out;
00248 }
00249
00250 w = (w << 4) | (0xff & c);
00251 state = IN6PTON_COLON_1 | IN6PTON_DELIM;
00252 if (!(w & 0xf000)) {
00253 state |= IN6PTON_XDIGIT;
00254 }
00255 if (!dc && d + 2 < dbuf + sizeof(dbuf)) {
00256 state |= IN6PTON_COLON_1_2;
00257 state &= ~IN6PTON_DELIM;
00258 }
00259 if (d + 2 >= dbuf + sizeof(dbuf)) {
00260 state &= ~(IN6PTON_COLON_1|IN6PTON_COLON_1_2);
00261 }
00262 cont:
00263 if ((dc && d + 4 < dbuf + sizeof(dbuf)) ||
00264 d + 4 == dbuf + sizeof(dbuf)) {
00265 state |= IN6PTON_DOT;
00266 }
00267 if (d >= dbuf + sizeof(dbuf)) {
00268 state &= ~(IN6PTON_XDIGIT|IN6PTON_COLON_MASK);
00269 }
00270 s++;
00271 srclen--;
00272 }
00273
00274 i = 15; d--;
00275
00276 if (dc) {
00277 while(d >= dc)
00278 dst[i--] = *d--;
00279 while(i >= dc - dbuf)
00280 dst[i--] = 0;
00281 while(i >= 0)
00282 dst[i--] = *d--;
00283 } else
00284 memcpy(dst, dbuf, sizeof(dbuf));
00285
00286 ret = 1;
00287 out:
00288 if (end)
00289 *end = s;
00290 return ret;
00291 }
00292
00293 EXPORT_SYMBOL(in6_pton);
00294
00295 void inet_proto_csum_replace4(__sum16 *sum, struct sk_buff *skb,
00296 __be32 from, __be32 to, int pseudohdr)
00297 {
00298 __be32 diff[] = { ~from, to };
00299 if (skb->ip_summed != CHECKSUM_PARTIAL) {
00300 *sum = csum_fold(csum_partial(diff, sizeof(diff),
00301 ~csum_unfold(*sum)));
00302 if (skb->ip_summed == CHECKSUM_COMPLETE && pseudohdr)
00303 skb->csum = ~csum_partial(diff, sizeof(diff),
00304 ~skb->csum);
00305 } else if (pseudohdr)
00306 *sum = ~csum_fold(csum_partial(diff, sizeof(diff),
00307 csum_unfold(*sum)));
00308 }
00309 EXPORT_SYMBOL(inet_proto_csum_replace4);