1 /* $NetBSD: ipf.c,v 1.1 2005/04/03 22:15:32 peter Exp $ */
4 * ipf.c - NAT lookup code for IP Filter.
6 * This software is in the public domain.
7 * Written by Peter Postma <peter@NetBSD.org>
10 #include <sys/cdefs.h>
13 #include <sys/types.h>
14 #include <sys/socket.h>
15 #include <sys/ioctl.h>
16 #include <sys/fcntl.h>
19 #include <netinet/in.h>
20 #include <netinet/in_systm.h>
21 #include <netinet/ipl.h>
22 #include <netinet/ip_compat.h>
23 #include <netinet/ip_fil.h>
24 #include <netinet/ip_nat.h>
34 ipf_natlookup(struct sockaddr_storage
*ss
, struct sockaddr
*nat_addr
,
41 (void)memset(&obj
, 0, sizeof(obj
));
42 (void)memset(&nl
, 0, sizeof(nl
));
44 /* Build the ipf object description structure. */
45 obj
.ipfo_rev
= IPFILTER_VERSION
;
46 obj
.ipfo_size
= sizeof(nl
);
48 obj
.ipfo_type
= IPFOBJ_NATLOOKUP
;
50 /* Build the ipf natlook structure. */
51 switch (ss
[0].ss_family
) {
53 (void)memcpy(&nl
.nl_realip
, &satosin(&ss
[0])->sin_addr
,
54 sizeof(struct in_addr
));
55 (void)memcpy(&nl
.nl_outip
, &satosin(&ss
[1])->sin_addr
,
56 sizeof(struct in_addr
));
57 nl
.nl_realport
= ntohs(satosin(&ss
[0])->sin_port
);
58 nl
.nl_outport
= ntohs(satosin(&ss
[1])->sin_port
);
59 nl
.nl_flags
= IPN_TCP
| IPN_IN
;
62 /* XXX IP Filter doesn't support IPv6 NAT yet. */
64 maybe_syslog(LOG_ERR
, "Unsupported protocol for NAT lookup "
65 "(no. %d)", ss
[0].ss_family
);
69 /* Open the NAT device and do the lookup. */
70 if ((dev
= open(IPNAT_NAME
, O_RDONLY
)) == -1) {
71 maybe_syslog(LOG_ERR
, "Cannot open %s: %m", IPNAT_NAME
);
74 if (ioctl(dev
, SIOCGNATL
, &obj
) == -1) {
75 maybe_syslog(LOG_ERR
, "NAT lookup failure: %m");
82 * Put the originating address into nat_addr and fill
83 * the port with the ident port, 113.
85 switch (ss
[0].ss_family
) {
87 (void)memcpy(&satosin(nat_addr
)->sin_addr
, &nl
.nl_inip
,
88 sizeof(struct in_addr
));
89 satosin(nat_addr
)->sin_port
= htons(113);
90 satosin(nat_addr
)->sin_len
= sizeof(struct sockaddr_in
);
91 satosin(nat_addr
)->sin_family
= AF_INET
;
96 /* Put the originating port into nat_lport. */
97 *nat_lport
= nl
.nl_inport
;