2 * Written by Atsushi Murai <amurai@spec.co.jp>
3 * Copyright (c) 1998, System Planning and Engineering Co.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * oConsidering for word alignment for other platform.
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
35 alias_nbt.c performs special processing for NetBios over TCP/IP
38 Initial version: May, 1998 (Atsushi Murai <amurai@spec.co.jp>)
40 See HISTORY file for record of revisions.
45 #include <sys/param.h>
46 #include <sys/ctype.h>
47 #include <sys/libkern.h>
49 #include <sys/types.h>
53 #include <arpa/inet.h>
56 #include <netinet/in_systm.h>
57 #include <netinet/in.h>
58 #include <netinet/ip.h>
59 #include <netinet/udp.h>
60 #include <netinet/tcp.h>
63 #include <netinet/libalias/alias_local.h>
65 #include "alias_local.h"
69 struct in_addr oldaddr;
71 struct in_addr newaddr;
80 struct in_addr source_ip;
94 u_short dir: 1, opcode:4, nmflags:7, rcode:4;
111 PrintRcode(u_char rcode)
116 printf("\nFormat Error.");
118 printf("\nSever failure.");
120 printf("\nUnsupported request error.\n");
122 printf("\nRefused error.\n");
124 printf("\nActive error.\n");
126 printf("\nName in conflict error.\n");
128 printf("\n?%c?=%0x\n", '?', rcode);
136 /* Handling Name field */
138 AliasHandleName(u_char * p, char *pmax)
145 /* Following length field */
147 if (p == NULL || (char *)p >= pmax)
152 if ((char *)p > pmax)
154 return ((u_char *) p);
156 while ((*p & 0x3f) != 0x00) {
163 /* Get next length field */
164 p = (u_char *) (p + (*p & 0x3f) + 1);
165 if ((char *)p > pmax) {
174 c = (u_char) (((((*s & 0x0f) << 4) | (*(s + 1) & 0x0f)) - 0x11));
179 printf("<0x%02x>", c);
195 /* Set up to out of Name field */
196 if (p == NULL || (char *)p >= pmax)
200 return ((u_char *) p);
204 * NetBios Datagram Handler (IP/UDP)
206 #define DGM_DIRECT_UNIQ 0x10
207 #define DGM_DIRECT_GROUP 0x11
208 #define DGM_BROADCAST 0x12
209 #define DGM_ERROR 0x13
210 #define DGM_QUERY 0x14
211 #define DGM_POSITIVE_RES 0x15
212 #define DGM_NEGATIVE_RES 0x16
217 struct ip *pip, /* IP packet to examine/patch */
218 struct alias_link *lnk,
219 struct in_addr *alias_address,
231 /* Calculate data length of UDP packet */
232 uh = (struct udphdr *)ip_next(pip);
233 pmax = (char *)uh + ntohs(uh->uh_ulen);
235 ndh = (NbtDataHeader *)udp_next(uh);
236 if ((char *)(ndh + 1) > pmax)
239 printf("\nType=%02x,", ndh->type);
242 case DGM_DIRECT_UNIQ:
243 case DGM_DIRECT_GROUP:
245 p = (u_char *) ndh + 14;
246 p = AliasHandleName(p, pmax); /* Source Name */
247 p = AliasHandleName(p, pmax); /* Destination Name */
250 p = (u_char *) ndh + 11;
253 case DGM_POSITIVE_RES:
254 case DGM_NEGATIVE_RES:
255 p = (u_char *) ndh + 10;
256 p = AliasHandleName(p, pmax); /* Destination Name */
259 if (p == NULL || (char *)p > pmax)
262 printf("%s:%d-->", inet_ntoa(ndh->source_ip), ntohs(ndh->source_port));
264 /* Doing an IP address and Port number Translation */
265 if (uh->uh_sum != 0) {
269 acc = ndh->source_port;
271 sptr = (u_short *) & (ndh->source_ip);
274 sptr = (u_short *) alias_address;
277 ADJUST_CHECKSUM(acc, uh->uh_sum);
279 ndh->source_ip = *alias_address;
280 ndh->source_port = alias_port;
282 printf("%s:%d\n", inet_ntoa(ndh->source_ip), ntohs(ndh->source_port));
285 return ((p == NULL) ? -1 : 0);
288 /* Question Section */
289 #define QS_TYPE_NB 0x0020
290 #define QS_TYPE_NBSTAT 0x0021
291 #define QS_CLAS_IN 0x0001
293 u_short type; /* The type of Request */
294 u_short class; /* The class of Request */
302 NBTArguments * nbtarg)
309 q = (NBTNsQuestion *) AliasHandleName((u_char *) q, pmax);
311 if (q == NULL || (char *)(q + 1) > pmax) {
315 /* Type and Class filed */
316 switch (ntohs(q->type)) {
323 printf("\nUnknown Type on Question %0x\n", ntohs(q->type));
330 /* Set up to out of Question Section */
331 return ((u_char *) q);
334 /* Resource Record */
335 #define RR_TYPE_A 0x0001
336 #define RR_TYPE_NS 0x0002
337 #define RR_TYPE_NULL 0x000a
338 #define RR_TYPE_NB 0x0020
339 #define RR_TYPE_NBSTAT 0x0021
340 #define RR_CLAS_IN 0x0001
341 #define SizeOfNsResource 8
349 #define SizeOfNsRNB 6
351 u_short g: 1 , ont:2, resv:13;
356 AliasHandleResourceNB(
359 NBTArguments * nbtarg)
364 if (q == NULL || (char *)(q + 1) > pmax)
366 /* Check out a length */
367 bcount = ntohs(q->rdlen);
369 /* Forward to Resource NB position */
370 nb = (NBTNsRNB *) ((u_char *) q + SizeOfNsResource);
372 /* Processing all in_addr array */
374 printf("NB rec[%s", inet_ntoa(nbtarg->oldaddr));
375 printf("->%s, %dbytes] ", inet_ntoa(nbtarg->newaddr), bcount);
377 while (nb != NULL && bcount != 0) {
378 if ((char *)(nb + 1) > pmax) {
383 printf("<%s>", inet_ntoa(nb->addr));
385 if (!bcmp(&nbtarg->oldaddr, &nb->addr, sizeof(struct in_addr))) {
386 if (*nbtarg->uh_sum != 0) {
390 sptr = (u_short *) & (nb->addr);
393 sptr = (u_short *) & (nbtarg->newaddr);
396 ADJUST_CHECKSUM(acc, *nbtarg->uh_sum);
398 nb->addr = nbtarg->newaddr;
408 nb = (NBTNsRNB *) ((u_char *) nb + SizeOfNsRNB);
409 bcount -= SizeOfNsRNB;
411 if (nb == NULL || (char *)(nb + 1) > pmax) {
414 return ((u_char *) nb);
417 #define SizeOfResourceA 6
423 AliasHandleResourceA(
426 NBTArguments * nbtarg)
431 if (q == NULL || (char *)(q + 1) > pmax)
434 /* Forward to Resource A position */
435 a = (NBTNsResourceA *) ((u_char *) q + sizeof(NBTNsResource));
437 /* Check out of length */
438 bcount = ntohs(q->rdlen);
440 /* Processing all in_addr array */
442 printf("Arec [%s", inet_ntoa(nbtarg->oldaddr));
443 printf("->%s]", inet_ntoa(nbtarg->newaddr));
445 while (bcount != 0) {
446 if (a == NULL || (char *)(a + 1) > pmax)
449 printf("..%s", inet_ntoa(a->addr));
451 if (!bcmp(&nbtarg->oldaddr, &a->addr, sizeof(struct in_addr))) {
452 if (*nbtarg->uh_sum != 0) {
456 sptr = (u_short *) & (a->addr); /* Old */
459 sptr = (u_short *) & nbtarg->newaddr; /* New */
462 ADJUST_CHECKSUM(acc, *nbtarg->uh_sum);
464 a->addr = nbtarg->newaddr;
467 bcount -= SizeOfResourceA;
469 if (a == NULL || (char *)(a + 1) > pmax)
471 return ((u_char *) a);
475 u_short opcode:4, flags:8, resv:4;
479 AliasHandleResourceNULL(
482 NBTArguments * nbtarg)
484 NBTNsResourceNULL *n;
489 if (q == NULL || (char *)(q + 1) > pmax)
492 /* Forward to Resource NULL position */
493 n = (NBTNsResourceNULL *) ((u_char *) q + sizeof(NBTNsResource));
495 /* Check out of length */
496 bcount = ntohs(q->rdlen);
498 /* Processing all in_addr array */
499 while (bcount != 0) {
500 if ((char *)(n + 1) > pmax) {
505 bcount -= sizeof(NBTNsResourceNULL);
507 if ((char *)(n + 1) > pmax)
510 return ((u_char *) n);
514 AliasHandleResourceNS(
517 NBTArguments * nbtarg)
519 NBTNsResourceNULL *n;
524 if (q == NULL || (char *)(q + 1) > pmax)
527 /* Forward to Resource NULL position */
528 n = (NBTNsResourceNULL *) ((u_char *) q + sizeof(NBTNsResource));
530 /* Check out of length */
531 bcount = ntohs(q->rdlen);
533 /* Resource Record Name Filed */
534 q = (NBTNsResource *) AliasHandleName((u_char *) n, pmax); /* XXX */
536 if (q == NULL || (char *)((u_char *) n + bcount) > pmax)
539 return ((u_char *) n + bcount);
544 } NBTNsResourceNBSTAT;
547 AliasHandleResourceNBSTAT(
550 NBTArguments * nbtarg)
552 NBTNsResourceNBSTAT *n;
557 if (q == NULL || (char *)(q + 1) > pmax)
560 /* Forward to Resource NBSTAT position */
561 n = (NBTNsResourceNBSTAT *) ((u_char *) q + sizeof(NBTNsResource));
563 /* Check out of length */
564 bcount = ntohs(q->rdlen);
566 if (q == NULL || (char *)((u_char *) n + bcount) > pmax)
569 return ((u_char *) n + bcount);
581 /* Resource Record Name Filed */
582 q = (NBTNsResource *) AliasHandleName((u_char *) q, pmax);
584 if (q == NULL || (char *)(q + 1) > pmax)
587 printf("type=%02x, count=%d\n", ntohs(q->type), count);
590 /* Type and Class filed */
591 switch (ntohs(q->type)) {
593 q = (NBTNsResource *) AliasHandleResourceNB(
600 q = (NBTNsResource *) AliasHandleResourceA(
607 q = (NBTNsResource *) AliasHandleResourceNS(
614 q = (NBTNsResource *) AliasHandleResourceNULL(
621 q = (NBTNsResource *) AliasHandleResourceNBSTAT(
630 "\nUnknown Type of Resource %0x\n",
639 return ((u_char *) q);
645 struct ip *pip, /* IP packet to examine/patch */
646 struct alias_link *lnk,
647 struct in_addr *alias_address,
648 u_short * alias_port,
649 struct in_addr *original_address,
650 u_short * original_port)
661 /* Set up Common Parameter */
662 nbtarg.oldaddr = *alias_address;
663 nbtarg.oldport = *alias_port;
664 nbtarg.newaddr = *original_address;
665 nbtarg.newport = *original_port;
667 /* Calculate data length of UDP packet */
668 uh = (struct udphdr *)ip_next(pip);
669 nbtarg.uh_sum = &(uh->uh_sum);
670 nsh = (NbtNSHeader *)udp_next(uh);
671 p = (u_char *) (nsh + 1);
672 pmax = (char *)uh + ntohs(uh->uh_ulen);
674 if ((char *)(nsh + 1) > pmax)
678 printf(" [%s] ID=%02x, op=%01x, flag=%02x, rcode=%01x, qd=%04x"
679 ", an=%04x, ns=%04x, ar=%04x, [%d]-->",
680 nsh->dir ? "Response" : "Request",
689 (u_char *) p - (u_char *) nsh
693 /* Question Entries */
694 if (ntohs(nsh->qdcount) != 0) {
695 p = AliasHandleQuestion(
702 /* Answer Resource Records */
703 if (ntohs(nsh->ancount) != 0) {
704 p = AliasHandleResource(
711 /* Authority Resource Recodrs */
712 if (ntohs(nsh->nscount) != 0) {
713 p = AliasHandleResource(
720 /* Additional Resource Recodrs */
721 if (ntohs(nsh->arcount) != 0) {
722 p = AliasHandleResource(
730 PrintRcode(nsh->rcode);
732 return ((p == NULL) ? -1 : 0);