1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
|
--- sys/netinet/libalias/alias.c.orig
+++ sys/netinet/libalias/alias.c
@@ -724,21 +724,37 @@
return (PKT_ALIAS_IGNORED);
}
+#define MF_ISSET(_pip) (ntohs((_pip)->ip_off) & IP_MF)
+#define FRAG_NO_HDR(_pip) (ntohs((_pip)->ip_off) & IP_OFFMASK)
+
+static struct udphdr *
+ValidateUdpLength(struct ip *pip)
+{
+ struct udphdr *ud;
+ size_t dlen;
+
+#ifdef _KERNEL
+ KASSERT(!FRAG_NO_HDR(pip), ("header-less fragment isn't expected here"));
+#endif
+ dlen = ntohs(pip->ip_len) - (pip->ip_hl << 2);
+ if (dlen < sizeof(struct udphdr))
+ return (NULL);
+ ud = (struct udphdr *)ip_next(pip);
+ if (!MF_ISSET(pip) && dlen < ntohs(ud->uh_ulen))
+ return (NULL);
+ return (ud);
+}
+
static int
UdpAliasIn(struct libalias *la, struct ip *pip)
{
struct udphdr *ud;
struct alias_link *lnk;
- size_t dlen;
LIBALIAS_LOCK_ASSERT(la);
- dlen = ntohs(pip->ip_len) - (pip->ip_hl << 2);
- if (dlen < sizeof(struct udphdr))
- return (PKT_ALIAS_IGNORED);
-
- ud = (struct udphdr *)ip_next(pip);
- if (dlen < ntohs(ud->uh_ulen))
+ ud = ValidateUdpLength(pip);
+ if (ud == NULL)
return (PKT_ALIAS_IGNORED);
lnk = FindUdpTcpIn(la, pip->ip_src, pip->ip_dst,
@@ -753,8 +769,8 @@
int accumulate;
int error;
struct alias_data ad = {
- .lnk = lnk,
- .oaddr = &original_address,
+ .lnk = lnk,
+ .oaddr = &original_address,
.aaddr = &alias_address,
.aport = &alias_port,
.sport = &ud->uh_sport,
@@ -769,46 +785,48 @@
ud->uh_dport = GetOriginalPort(lnk);
proxy_port = GetProxyPort(lnk);
- /* Walk out chain. */
+ /* Walk out chain. */
error = find_handler(IN, UDP, la, pip, &ad);
/* If we cannot figure out the packet, ignore it. */
if (error < 0)
return (PKT_ALIAS_IGNORED);
-/* If UDP checksum is not zero, then adjust since destination port */
-/* is being unaliased and destination address is being altered. */
+ /* If UDP checksum is not zero, then adjust since
+ * destination port is being unaliased and
+ * destination address is being altered. */
if (ud->uh_sum != 0) {
accumulate = alias_port;
accumulate -= ud->uh_dport;
accumulate += twowords(&alias_address);
accumulate -= twowords(&original_address);
-/* If this is a proxy packet, modify checksum because of source change.*/
- if (proxy_port != 0) {
- accumulate += ud->uh_sport;
- accumulate -= proxy_port;
- }
+ /* If this is a proxy packet, modify checksum
+ * because of source change.*/
+ if (proxy_port != 0) {
+ accumulate += ud->uh_sport;
+ accumulate -= proxy_port;
+ }
- if (proxy_address.s_addr != 0) {
+ if (proxy_address.s_addr != 0) {
accumulate += twowords(&pip->ip_src);
accumulate -= twowords(&proxy_address);
- }
+ }
ADJUST_CHECKSUM(accumulate, ud->uh_sum);
}
-/* XXX: Could the two if's below be concatenated to one ? */
-/* Restore source port and/or address in case of proxying*/
- if (proxy_port != 0)
- ud->uh_sport = proxy_port;
+ /* XXX: Could the two if's below be concatenated to one ? */
+ /* Restore source port and/or address in case of proxying*/
+ if (proxy_port != 0)
+ ud->uh_sport = proxy_port;
- if (proxy_address.s_addr != 0) {
- DifferentialChecksum(&pip->ip_sum,
- &proxy_address, &pip->ip_src, 2);
- pip->ip_src = proxy_address;
- }
+ if (proxy_address.s_addr != 0) {
+ DifferentialChecksum(&pip->ip_sum,
+ &proxy_address, &pip->ip_src, 2);
+ pip->ip_src = proxy_address;
+ }
-/* Restore original IP address */
+ /* Restore original IP address */
DifferentialChecksum(&pip->ip_sum,
&original_address, &pip->ip_dst, 2);
pip->ip_dst = original_address;
@@ -829,47 +847,41 @@
u_short proxy_server_port;
int proxy_type;
int error;
- size_t dlen;
LIBALIAS_LOCK_ASSERT(la);
-/* Return if proxy-only mode is enabled and not proxyrule found.*/
- dlen = ntohs(pip->ip_len) - (pip->ip_hl << 2);
- if (dlen < sizeof(struct udphdr))
+ ud = ValidateUdpLength(pip);
+ if (ud == NULL)
return (PKT_ALIAS_IGNORED);
- ud = (struct udphdr *)ip_next(pip);
- if (dlen < ntohs(ud->uh_ulen))
- return (PKT_ALIAS_IGNORED);
-
- proxy_type = ProxyCheck(la, &proxy_server_address,
- &proxy_server_port, pip->ip_src, pip->ip_dst,
- ud->uh_dport, pip->ip_p);
+ /* Return if proxy-only mode is enabled and not proxyrule found.*/
+ proxy_type = ProxyCheck(la, &proxy_server_address, &proxy_server_port,
+ pip->ip_src, pip->ip_dst, ud->uh_dport, pip->ip_p);
if (proxy_type == 0 && (la->packetAliasMode & PKT_ALIAS_PROXY_ONLY))
return (PKT_ALIAS_OK);
-/* If this is a transparent proxy, save original destination,
- * then alter the destination and adjust checksums */
+ /* If this is a transparent proxy, save original destination,
+ * then alter the destination and adjust checksums */
dest_port = ud->uh_dport;
dest_address = pip->ip_dst;
if (proxy_type != 0) {
- int accumulate;
+ int accumulate;
accumulate = twowords(&pip->ip_dst);
accumulate -= twowords(&proxy_server_address);
- ADJUST_CHECKSUM(accumulate, pip->ip_sum);
+ ADJUST_CHECKSUM(accumulate, pip->ip_sum);
if (ud->uh_sum != 0) {
accumulate = twowords(&pip->ip_dst);
accumulate -= twowords(&proxy_server_address);
- accumulate += ud->uh_dport;
- accumulate -= proxy_server_port;
- ADJUST_CHECKSUM(accumulate, ud->uh_sum);
+ accumulate += ud->uh_dport;
+ accumulate -= proxy_server_port;
+ ADJUST_CHECKSUM(accumulate, ud->uh_sum);
}
- pip->ip_dst = proxy_server_address;
- ud->uh_dport = proxy_server_port;
+ pip->ip_dst = proxy_server_address;
+ ud->uh_dport = proxy_server_port;
}
lnk = FindUdpTcpOut(la, pip->ip_src, pip->ip_dst,
ud->uh_sport, ud->uh_dport,
@@ -878,7 +890,7 @@
u_short alias_port;
struct in_addr alias_address;
struct alias_data ad = {
- .lnk = lnk,
+ .lnk = lnk,
.oaddr = NULL,
.aaddr = &alias_address,
.aport = &alias_port,
@@ -887,24 +899,24 @@
.maxpktsize = 0
};
-/* Save original destination address, if this is a proxy packet.
- * Also modify packet to include destination encoding. This may
- * change the size of IP header. */
+ /* Save original destination address, if this is a proxy packet.
+ * Also modify packet to include destination encoding. This may
+ * change the size of IP header. */
if (proxy_type != 0) {
- SetProxyPort(lnk, dest_port);
- SetProxyAddress(lnk, dest_address);
- ProxyModify(la, lnk, pip, maxpacketsize, proxy_type);
- ud = (struct udphdr *)ip_next(pip);
- }
+ SetProxyPort(lnk, dest_port);
+ SetProxyAddress(lnk, dest_address);
+ ProxyModify(la, lnk, pip, maxpacketsize, proxy_type);
+ ud = (struct udphdr *)ip_next(pip);
+ }
alias_address = GetAliasAddress(lnk);
alias_port = GetAliasPort(lnk);
- /* Walk out chain. */
+ /* Walk out chain. */
error = find_handler(OUT, UDP, la, pip, &ad);
-/* If UDP checksum is not zero, adjust since source port is */
-/* being aliased and source address is being altered */
+ /* If UDP checksum is not zero, adjust since source port is */
+ /* being aliased and source address is being altered */
if (ud->uh_sum != 0) {
int accumulate;
@@ -914,10 +926,10 @@
accumulate -= twowords(&alias_address);
ADJUST_CHECKSUM(accumulate, ud->uh_sum);
}
-/* Put alias port in UDP header */
+ /* Put alias port in UDP header */
ud->uh_sport = alias_port;
-/* Change source address */
+ /* Change source address */
DifferentialChecksum(&pip->ip_sum,
&alias_address, &pip->ip_src, 2);
pip->ip_src = alias_address;
@@ -1340,68 +1352,69 @@
/* Defense against mangled packets */
if (ntohs(pip->ip_len) > maxpacketsize
|| (pip->ip_hl << 2) > maxpacketsize) {
- iresult = PKT_ALIAS_IGNORED;
+ iresult = PKT_ALIAS_IGNORED;
+ goto getout;
+ }
+
+ if (FRAG_NO_HDR(pip)) {
+ iresult = FragmentIn(la, pip->ip_src, pip, pip->ip_id,
+ &pip->ip_sum);
goto getout;
}
iresult = PKT_ALIAS_IGNORED;
- if ((ntohs(pip->ip_off) & IP_OFFMASK) == 0) {
- switch (pip->ip_p) {
- case IPPROTO_ICMP:
- iresult = IcmpAliasIn(la, pip);
- break;
- case IPPROTO_UDP:
- iresult = UdpAliasIn(la, pip);
- break;
- case IPPROTO_TCP:
- iresult = TcpAliasIn(la, pip);
- break;
+ switch (pip->ip_p) {
+ case IPPROTO_ICMP:
+ iresult = IcmpAliasIn(la, pip);
+ break;
+ case IPPROTO_UDP:
+ iresult = UdpAliasIn(la, pip);
+ break;
+ case IPPROTO_TCP:
+ iresult = TcpAliasIn(la, pip);
+ break;
#ifdef _KERNEL
- case IPPROTO_SCTP:
- iresult = SctpAlias(la, pip, SN_TO_LOCAL);
- break;
+ case IPPROTO_SCTP:
+ iresult = SctpAlias(la, pip, SN_TO_LOCAL);
+ break;
#endif
- case IPPROTO_GRE: {
- int error;
- struct alias_data ad = {
- .lnk = NULL,
- .oaddr = NULL,
- .aaddr = NULL,
- .aport = NULL,
- .sport = NULL,
- .dport = NULL,
- .maxpktsize = 0
- };
-
- /* Walk out chain. */
- error = find_handler(IN, IP, la, pip, &ad);
- if (error == 0)
- iresult = PKT_ALIAS_OK;
- else
- iresult = ProtoAliasIn(la, pip->ip_src,
- pip, pip->ip_p, &pip->ip_sum);
- }
- break;
- default:
- iresult = ProtoAliasIn(la, pip->ip_src, pip,
- pip->ip_p, &pip->ip_sum);
- break;
- }
+ case IPPROTO_GRE: {
+ int error;
+ struct alias_data ad = {
+ .lnk = NULL,
+ .oaddr = NULL,
+ .aaddr = NULL,
+ .aport = NULL,
+ .sport = NULL,
+ .dport = NULL,
+ .maxpktsize = 0
+ };
- if (ntohs(pip->ip_off) & IP_MF) {
- struct alias_link *lnk;
+ /* Walk out chain. */
+ error = find_handler(IN, IP, la, pip, &ad);
+ if (error == 0)
+ iresult = PKT_ALIAS_OK;
+ else
+ iresult = ProtoAliasIn(la, pip->ip_src,
+ pip, pip->ip_p, &pip->ip_sum);
+ break;
+ }
+ default:
+ iresult = ProtoAliasIn(la, pip->ip_src, pip,
+ pip->ip_p, &pip->ip_sum);
+ break;
+ }
- lnk = FindFragmentIn1(la, pip->ip_src, alias_addr, pip->ip_id);
- if (lnk != NULL) {
- iresult = PKT_ALIAS_FOUND_HEADER_FRAGMENT;
- SetFragmentAddr(lnk, pip->ip_dst);
- } else {
- iresult = PKT_ALIAS_ERROR;
- }
+ if (MF_ISSET(pip)) {
+ struct alias_link *lnk;
+
+ lnk = FindFragmentIn1(la, pip->ip_src, alias_addr, pip->ip_id);
+ if (lnk != NULL) {
+ iresult = PKT_ALIAS_FOUND_HEADER_FRAGMENT;
+ SetFragmentAddr(lnk, pip->ip_dst);
+ } else {
+ iresult = PKT_ALIAS_ERROR;
}
- } else {
- iresult = FragmentIn(la, pip->ip_src, pip, pip->ip_id,
- &pip->ip_sum);
}
getout:
@@ -1449,10 +1462,10 @@
}
static int
-LibAliasOutLocked(struct libalias *la, struct ip *pip, /* valid IP packet */
- int maxpacketsize, /* How much the packet data may grow (FTP
- * and IRC inline changes) */
- int create /* Create new entries ? */
+LibAliasOutLocked(struct libalias *la,
+ struct ip *pip, /* valid IP packet */
+ int maxpacketsize, /* How much the packet data may grow (FTP and IRC inline changes) */
+ int create /* Create new entries ? */
)
{
int iresult;
@@ -1498,52 +1511,55 @@
} else if (la->packetAliasMode & PKT_ALIAS_PROXY_ONLY) {
SetDefaultAliasAddress(la, pip->ip_src);
}
+
+ if (FRAG_NO_HDR(pip)) {
+ iresult = FragmentOut(la, pip, &pip->ip_sum);
+ goto getout_restore;
+ }
+
iresult = PKT_ALIAS_IGNORED;
- if ((ntohs(pip->ip_off) & IP_OFFMASK) == 0) {
- switch (pip->ip_p) {
- case IPPROTO_ICMP:
- iresult = IcmpAliasOut(la, pip, create);
- break;
- case IPPROTO_UDP:
- iresult = UdpAliasOut(la, pip, maxpacketsize, create);
- break;
- case IPPROTO_TCP:
- iresult = TcpAliasOut(la, pip, maxpacketsize, create);
- break;
+ switch (pip->ip_p) {
+ case IPPROTO_ICMP:
+ iresult = IcmpAliasOut(la, pip, create);
+ break;
+ case IPPROTO_UDP:
+ iresult = UdpAliasOut(la, pip, maxpacketsize, create);
+ break;
+ case IPPROTO_TCP:
+ iresult = TcpAliasOut(la, pip, maxpacketsize, create);
+ break;
#ifdef _KERNEL
- case IPPROTO_SCTP:
- iresult = SctpAlias(la, pip, SN_TO_GLOBAL);
- break;
+ case IPPROTO_SCTP:
+ iresult = SctpAlias(la, pip, SN_TO_GLOBAL);
+ break;
#endif
- case IPPROTO_GRE: {
- int error;
- struct alias_data ad = {
- .lnk = NULL,
- .oaddr = NULL,
- .aaddr = NULL,
- .aport = NULL,
- .sport = NULL,
- .dport = NULL,
- .maxpktsize = 0
- };
- /* Walk out chain. */
- error = find_handler(OUT, IP, la, pip, &ad);
- if (error == 0)
- iresult = PKT_ALIAS_OK;
- else
- iresult = ProtoAliasOut(la, pip,
- pip->ip_dst, pip->ip_p, &pip->ip_sum, create);
- }
- break;
- default:
+ case IPPROTO_GRE: {
+ int error;
+ struct alias_data ad = {
+ .lnk = NULL,
+ .oaddr = NULL,
+ .aaddr = NULL,
+ .aport = NULL,
+ .sport = NULL,
+ .dport = NULL,
+ .maxpktsize = 0
+ };
+ /* Walk out chain. */
+ error = find_handler(OUT, IP, la, pip, &ad);
+ if (error == 0)
+ iresult = PKT_ALIAS_OK;
+ else
iresult = ProtoAliasOut(la, pip,
pip->ip_dst, pip->ip_p, &pip->ip_sum, create);
- break;
+ break;
}
- } else {
- iresult = FragmentOut(la, pip, &pip->ip_sum);
+ default:
+ iresult = ProtoAliasOut(la, pip,
+ pip->ip_dst, pip->ip_p, &pip->ip_sum, create);
+ break;
}
+getout_restore:
SetDefaultAliasAddress(la, addr_save);
getout:
return (iresult);
|