2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Written by Atsushi Murai <amurai@spec.co.jp>
5 * Copyright (c) 1998, System Planning and Engineering Co.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * oConsidering for word alignment for other platform.
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
37 alias_nbt.c performs special processing for NetBios over TCP/IP
40 Initial version: May, 1998 (Atsushi Murai <amurai@spec.co.jp>)
42 See HISTORY file for record of revisions.
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/kernel.h>
50 #include <sys/module.h>
53 #include <sys/types.h>
58 #include <netinet/in_systm.h>
59 #include <netinet/in.h>
60 #include <netinet/ip.h>
61 #include <netinet/udp.h>
64 #include <netinet/libalias/alias_local.h>
65 #include <netinet/libalias/alias_mod.h>
67 #include "alias_local.h"
68 #include "alias_mod.h"
71 #define NETBIOS_NS_PORT_NUMBER 137
72 #define NETBIOS_DGM_PORT_NUMBER 138
75 AliasHandleUdpNbt(struct libalias *, struct ip *, struct alias_link *,
76 struct in_addr *, u_short);
78 AliasHandleUdpNbtNS(struct libalias *, struct ip *, struct alias_link *,
79 struct in_addr *, u_short *, struct in_addr *, u_short *);
82 fingerprint1(struct libalias *la, struct alias_data *ah)
84 if (ah->dport == NULL || ah->sport == NULL || ah->lnk == NULL ||
85 ah->aaddr == NULL || ah->aport == NULL)
87 if (ntohs(*ah->dport) == NETBIOS_DGM_PORT_NUMBER
88 || ntohs(*ah->sport) == NETBIOS_DGM_PORT_NUMBER)
94 protohandler1(struct libalias *la, struct ip *pip, struct alias_data *ah)
96 return (AliasHandleUdpNbt(la, pip, ah->lnk, ah->aaddr, *ah->aport));
100 fingerprint2(struct libalias *la, struct alias_data *ah)
102 if (ah->dport == NULL || ah->sport == NULL || ah->lnk == NULL ||
103 ah->aaddr == NULL || ah->aport == NULL)
105 if (ntohs(*ah->dport) == NETBIOS_NS_PORT_NUMBER
106 || ntohs(*ah->sport) == NETBIOS_NS_PORT_NUMBER)
112 protohandler2in(struct libalias *la, struct ip *pip, struct alias_data *ah)
114 AliasHandleUdpNbtNS(la, pip, ah->lnk, ah->aaddr, ah->aport,
115 ah->oaddr, ah->dport);
120 protohandler2out(struct libalias *la, struct ip *pip, struct alias_data *ah)
122 return (AliasHandleUdpNbtNS(la, pip, ah->lnk, &pip->ip_src, ah->sport,
123 ah->aaddr, ah->aport));
126 /* Kernel module definition. */
127 struct proto_handler handlers[] = {
132 .fingerprint = &fingerprint1,
133 .protohandler = &protohandler1
139 .fingerprint = &fingerprint2,
140 .protohandler = &protohandler2in
146 .fingerprint = &fingerprint2,
147 .protohandler = &protohandler2out
153 mod_handler(module_t mod, int type, void *data)
160 LibAliasAttachHandlers(handlers);
164 LibAliasDetachHandlers(handlers);
175 moduledata_t alias_mod = {
176 "alias_nbt", mod_handler, NULL
180 DECLARE_MODULE(alias_nbt, alias_mod, SI_SUB_DRIVERS, SI_ORDER_SECOND);
181 MODULE_VERSION(alias_nbt, 1);
182 MODULE_DEPEND(alias_nbt, libalias, 1, 1, 1);
186 struct in_addr oldaddr;
188 struct in_addr newaddr;
197 struct in_addr source_ip;
211 u_short dir:1, opcode:4, nmflags:7, rcode:4;
225 #ifdef LIBALIAS_DEBUG
227 PrintRcode(u_char rcode)
231 printf("\nFormat Error.");
233 printf("\nSever failure.");
235 printf("\nUnsupported request error.\n");
237 printf("\nRefused error.\n");
239 printf("\nActive error.\n");
241 printf("\nName in conflict error.\n");
243 printf("\n?%c?=%0x\n", '?', rcode);
249 /* Handling Name field */
251 AliasHandleName(u_char *p, char *pmax)
257 /* Following length field */
259 if (p == NULL || (char *)p >= pmax)
264 if ((char *)p > pmax)
266 return ((u_char *)p);
268 while ((*p & 0x3f) != 0x00) {
275 /* Get next length field */
276 p = (u_char *)(p + (*p & 0x3f) + 1);
277 if ((char *)p > pmax) {
281 #ifdef LIBALIAS_DEBUG
286 c = (u_char) (((((*s & 0x0f) << 4) | (*(s + 1) & 0x0f)) - 0x11));
287 #ifdef LIBALIAS_DEBUG
291 printf("<0x%02x>", c);
295 #ifdef LIBALIAS_DEBUG
301 #ifdef LIBALIAS_DEBUG
307 /* Set up to out of Name field */
308 if (p == NULL || (char *)p >= pmax)
312 return ((u_char *)p);
316 * NetBios Datagram Handler (IP/UDP)
318 #define DGM_DIRECT_UNIQ 0x10
319 #define DGM_DIRECT_GROUP 0x11
320 #define DGM_BROADCAST 0x12
321 #define DGM_ERROR 0x13
322 #define DGM_QUERY 0x14
323 #define DGM_POSITIVE_RES 0x15
324 #define DGM_NEGATIVE_RES 0x16
329 struct ip *pip, /* IP packet to examine/patch */
330 struct alias_link *lnk,
331 struct in_addr *alias_address,
338 #ifdef LIBALIAS_DEBUG
339 char addrbuf[INET_ADDRSTRLEN];
345 /* Calculate data length of UDP packet */
346 uh = (struct udphdr *)ip_next(pip);
347 pmax = (char *)uh + ntohs(uh->uh_ulen);
349 ndh = (NbtDataHeader *)udp_next(uh);
350 if ((char *)(ndh + 1) > pmax)
352 #ifdef LIBALIAS_DEBUG
353 printf("\nType=%02x,", ndh->type);
356 case DGM_DIRECT_UNIQ:
357 case DGM_DIRECT_GROUP:
359 p = (u_char *)ndh + 14;
360 p = AliasHandleName(p, pmax); /* Source Name */
361 p = AliasHandleName(p, pmax); /* Destination Name */
364 p = (u_char *)ndh + 11;
367 case DGM_POSITIVE_RES:
368 case DGM_NEGATIVE_RES:
369 p = (u_char *)ndh + 10;
370 p = AliasHandleName(p, pmax); /* Destination Name */
373 if (p == NULL || (char *)p > pmax)
375 #ifdef LIBALIAS_DEBUG
376 printf("%s:%d-->", inet_ntoa_r(ndh->source_ip, INET_NTOA_BUF(addrbuf)),
377 ntohs(ndh->source_port));
379 /* Doing an IP address and Port number Translation */
380 if (uh->uh_sum != 0) {
384 acc = ndh->source_port;
386 sptr = (u_short *)&(ndh->source_ip);
389 sptr = (u_short *)alias_address;
392 ADJUST_CHECKSUM(acc, uh->uh_sum);
394 ndh->source_ip = *alias_address;
395 ndh->source_port = alias_port;
396 #ifdef LIBALIAS_DEBUG
397 printf("%s:%d\n", inet_ntoa_r(ndh->source_ip, INET_NTOA_BUF(addrbuf)),
398 ntohs(ndh->source_port));
401 return ((p == NULL) ? -1 : 0);
404 /* Question Section */
405 #define QS_TYPE_NB 0x0020
406 #define QS_TYPE_NBSTAT 0x0021
407 #define QS_CLAS_IN 0x0001
409 u_short type; /* The type of Request */
410 u_short class; /* The class of Request */
418 NBTArguments * nbtarg)
424 q = (NBTNsQuestion *)AliasHandleName((u_char *)q, pmax);
426 if (q == NULL || (char *)(q + 1) > pmax) {
430 /* Type and Class filed */
431 switch (ntohs(q->type)) {
437 #ifdef LIBALIAS_DEBUG
438 printf("\nUnknown Type on Question %0x\n", ntohs(q->type));
445 /* Set up to out of Question Section */
446 return ((u_char *)q);
449 /* Resource Record */
450 #define RR_TYPE_A 0x0001
451 #define RR_TYPE_NS 0x0002
452 #define RR_TYPE_NULL 0x000a
453 #define RR_TYPE_NB 0x0020
454 #define RR_TYPE_NBSTAT 0x0021
455 #define RR_CLAS_IN 0x0001
456 #define SizeOfNsResource 8
464 #define SizeOfNsRNB 6
466 u_short g:1, ont:2, resv:13;
471 AliasHandleResourceNB(
474 NBTArguments *nbtarg)
478 #ifdef LIBALIAS_DEBUG
479 char oldbuf[INET_ADDRSTRLEN];
480 char newbuf[INET_ADDRSTRLEN];
483 if (q == NULL || (char *)(q + 1) > pmax)
485 /* Check out a length */
486 bcount = ntohs(q->rdlen);
488 /* Forward to Resource NB position */
489 nb = (NBTNsRNB *)((u_char *)q + SizeOfNsResource);
491 /* Processing all in_addr array */
492 #ifdef LIBALIAS_DEBUG
493 printf("NB rec[%s->%s, %dbytes] ",
494 inet_ntoa_r(nbtarg->oldaddr, INET_NTOA_BUF(oldbuf)),
495 inet_ntoa_r(nbtarg->newaddr, INET_NTOA_BUF(newbuf)),
498 while (nb != NULL && bcount != 0) {
499 if ((char *)(nb + 1) > pmax) {
503 #ifdef LIBALIAS_DEBUG
504 printf("<%s>", inet_ntoa_r(nb->addr, INET_NTOA_BUF(newbuf)));
506 if (!bcmp(&nbtarg->oldaddr, &nb->addr, sizeof(struct in_addr))) {
507 if (*nbtarg->uh_sum != 0) {
511 sptr = (u_short *)&(nb->addr);
514 sptr = (u_short *)&(nbtarg->newaddr);
517 ADJUST_CHECKSUM(acc, *nbtarg->uh_sum);
519 nb->addr = nbtarg->newaddr;
520 #ifdef LIBALIAS_DEBUG
524 #ifdef LIBALIAS_DEBUG
529 nb = (NBTNsRNB *)((u_char *)nb + SizeOfNsRNB);
530 bcount -= SizeOfNsRNB;
532 if (nb == NULL || (char *)(nb + 1) > pmax) {
535 return ((u_char *)nb);
538 #define SizeOfResourceA 6
544 AliasHandleResourceA(
547 NBTArguments *nbtarg)
551 #ifdef LIBALIAS_DEBUG
552 char oldbuf[INET_ADDRSTRLEN];
553 char newbuf[INET_ADDRSTRLEN];
556 if (q == NULL || (char *)(q + 1) > pmax)
559 /* Forward to Resource A position */
560 a = (NBTNsResourceA *)((u_char *)q + sizeof(NBTNsResource));
562 /* Check out of length */
563 bcount = ntohs(q->rdlen);
565 /* Processing all in_addr array */
566 #ifdef LIBALIAS_DEBUG
567 printf("Arec [%s->%s]",
568 inet_ntoa_r(nbtarg->oldaddr, INET_NTOA_BUF(oldbuf)),
569 inet_ntoa_r(nbtarg->newaddr, INET_NTOA_BUF(newbuf)));
571 while (bcount != 0) {
572 if (a == NULL || (char *)(a + 1) > pmax)
574 #ifdef LIBALIAS_DEBUG
575 printf("..%s", inet_ntoa_r(a->addr, INET_NTOA_BUF(newbuf)));
577 if (!bcmp(&nbtarg->oldaddr, &a->addr, sizeof(struct in_addr))) {
578 if (*nbtarg->uh_sum != 0) {
582 sptr = (u_short *)&(a->addr); /* Old */
585 sptr = (u_short *)&nbtarg->newaddr; /* New */
588 ADJUST_CHECKSUM(acc, *nbtarg->uh_sum);
590 a->addr = nbtarg->newaddr;
593 bcount -= SizeOfResourceA;
595 if (a == NULL || (char *)(a + 1) > pmax)
597 return ((u_char *)a);
601 u_short opcode:4, flags:8, resv:4;
605 AliasHandleResourceNULL(
608 NBTArguments *nbtarg)
610 NBTNsResourceNULL *n;
615 if (q == NULL || (char *)(q + 1) > pmax)
618 /* Forward to Resource NULL position */
619 n = (NBTNsResourceNULL *)((u_char *)q + sizeof(NBTNsResource));
621 /* Check out of length */
622 bcount = ntohs(q->rdlen);
624 /* Processing all in_addr array */
625 while (bcount != 0) {
626 if ((char *)(n + 1) > pmax) {
631 bcount -= sizeof(NBTNsResourceNULL);
633 if ((char *)(n + 1) > pmax)
636 return ((u_char *)n);
640 AliasHandleResourceNS(
643 NBTArguments *nbtarg)
645 NBTNsResourceNULL *n;
650 if (q == NULL || (char *)(q + 1) > pmax)
653 /* Forward to Resource NULL position */
654 n = (NBTNsResourceNULL *)((u_char *)q + sizeof(NBTNsResource));
656 /* Check out of length */
657 bcount = ntohs(q->rdlen);
659 /* Resource Record Name Filed */
660 q = (NBTNsResource *)AliasHandleName((u_char *)n, pmax); /* XXX */
662 if (q == NULL || (char *)((u_char *)n + bcount) > pmax)
665 return ((u_char *)n + bcount);
670 } NBTNsResourceNBSTAT;
673 AliasHandleResourceNBSTAT(
676 NBTArguments *nbtarg)
678 NBTNsResourceNBSTAT *n;
683 if (q == NULL || (char *)(q + 1) > pmax)
686 /* Forward to Resource NBSTAT position */
687 n = (NBTNsResourceNBSTAT *)((u_char *)q + sizeof(NBTNsResource));
689 /* Check out of length */
690 bcount = ntohs(q->rdlen);
692 if (q == NULL || (char *)((u_char *)n + bcount) > pmax)
695 return ((u_char *)n + bcount);
703 NBTArguments *nbtarg)
706 /* Resource Record Name Filed */
707 q = (NBTNsResource *)AliasHandleName((u_char *)q, pmax);
709 if (q == NULL || (char *)(q + 1) > pmax)
711 #ifdef LIBALIAS_DEBUG
712 printf("type=%02x, count=%d\n", ntohs(q->type), count);
715 /* Type and Class filed */
716 switch (ntohs(q->type)) {
718 q = (NBTNsResource *)AliasHandleResourceNB(
722 q = (NBTNsResource *)AliasHandleResourceA(
726 q = (NBTNsResource *)AliasHandleResourceNS(
730 q = (NBTNsResource *)AliasHandleResourceNULL(
734 q = (NBTNsResource *)AliasHandleResourceNBSTAT(
738 #ifdef LIBALIAS_DEBUG
740 "\nUnknown Type of Resource %0x\n",
749 return ((u_char *)q);
755 struct ip *pip, /* IP packet to examine/patch */
756 struct alias_link *lnk,
757 struct in_addr *alias_address,
759 struct in_addr *original_address,
760 u_short *original_port)
771 /* Set up Common Parameter */
772 nbtarg.oldaddr = *alias_address;
773 nbtarg.oldport = *alias_port;
774 nbtarg.newaddr = *original_address;
775 nbtarg.newport = *original_port;
777 /* Calculate data length of UDP packet */
778 uh = (struct udphdr *)ip_next(pip);
779 nbtarg.uh_sum = &(uh->uh_sum);
780 nsh = (NbtNSHeader *)udp_next(uh);
781 p = (u_char *)(nsh + 1);
782 pmax = (char *)uh + ntohs(uh->uh_ulen);
784 if ((char *)(nsh + 1) > pmax)
787 #ifdef LIBALIAS_DEBUG
788 printf(" [%s] ID=%02x, op=%01x, flag=%02x, rcode=%01x, qd=%04x"
789 ", an=%04x, ns=%04x, ar=%04x, [%d]-->",
790 nsh->dir ? "Response" : "Request",
799 (u_char *)p - (u_char *)nsh
803 /* Question Entries */
804 if (ntohs(nsh->qdcount) != 0) {
805 p = AliasHandleQuestion(
812 /* Answer Resource Records */
813 if (ntohs(nsh->ancount) != 0) {
814 p = AliasHandleResource(
821 /* Authority Resource Recodrs */
822 if (ntohs(nsh->nscount) != 0) {
823 p = AliasHandleResource(
830 /* Additional Resource Recodrs */
831 if (ntohs(nsh->arcount) != 0) {
832 p = AliasHandleResource(
839 #ifdef LIBALIAS_DEBUG
840 PrintRcode(nsh->rcode);
842 return ((p == NULL) ? -1 : 0);