xref: /netbsd-src/libexec/identd/npf.c (revision a363700a4c10735228ba5974bfc504d93585dc31)
1*a363700aSchristos /*	$NetBSD: npf.c,v 1.2 2016/12/10 22:09:18 christos Exp $	*/
2b525cd90Schristos 
3b525cd90Schristos /*-
4b525cd90Schristos  * Copyright (c) 2016 The NetBSD Foundation, Inc.
5b525cd90Schristos  * All rights reserved.
6b525cd90Schristos  *
7b525cd90Schristos  * This code is derived from software contributed to The NetBSD Foundation
8b525cd90Schristos  * by Christos Zoulas.
9b525cd90Schristos  *
10b525cd90Schristos  * Redistribution and use in source and binary forms, with or without
11b525cd90Schristos  * modification, are permitted provided that the following conditions
12b525cd90Schristos  * are met:
13b525cd90Schristos  * 1. Redistributions of source code must retain the above copyright
14b525cd90Schristos  *    notice, this list of conditions and the following disclaimer.
15b525cd90Schristos  * 2. Redistributions in binary form must reproduce the above copyright
16b525cd90Schristos  *    notice, this list of conditions and the following disclaimer in the
17b525cd90Schristos  *    documentation and/or other materials provided with the distribution.
18b525cd90Schristos  *
19b525cd90Schristos  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20b525cd90Schristos  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21b525cd90Schristos  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22b525cd90Schristos  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23b525cd90Schristos  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24b525cd90Schristos  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25b525cd90Schristos  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26b525cd90Schristos  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27b525cd90Schristos  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28b525cd90Schristos  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29b525cd90Schristos  * POSSIBILITY OF SUCH DAMAGE.
30b525cd90Schristos  */
31b525cd90Schristos 
32b525cd90Schristos #include <sys/cdefs.h>
33*a363700aSchristos __RCSID("$NetBSD: npf.c,v 1.2 2016/12/10 22:09:18 christos Exp $");
34b525cd90Schristos 
35b525cd90Schristos #include <sys/types.h>
36b525cd90Schristos #include <sys/socket.h>
37b525cd90Schristos #include <sys/ioctl.h>
38b525cd90Schristos #include <sys/fcntl.h>
39b525cd90Schristos 
40b525cd90Schristos #include <net/if.h>
41b525cd90Schristos #include <netinet/in.h>
42b525cd90Schristos 
43b525cd90Schristos #include <npf.h>
44b525cd90Schristos #include <stdlib.h>
45b525cd90Schristos #include <string.h>
46b525cd90Schristos #include <errno.h>
47b525cd90Schristos #include <syslog.h>
48b525cd90Schristos #include <unistd.h>
49b525cd90Schristos 
50b525cd90Schristos #include "identd.h"
51b525cd90Schristos 
52b525cd90Schristos int
npf_natlookup(const struct sockaddr_storage * ss,struct sockaddr_storage * nat_addr,in_port_t * nat_lport)53b525cd90Schristos npf_natlookup(const struct sockaddr_storage *ss,
54b525cd90Schristos     struct sockaddr_storage *nat_addr, in_port_t *nat_lport)
55b525cd90Schristos {
56b525cd90Schristos 	npf_addr_t *addr[2];
57b525cd90Schristos 	in_port_t port[2];
58b525cd90Schristos 	int dev, af;
59b525cd90Schristos 
60b525cd90Schristos 	/* copy the source into nat_addr so it is writable */
61b525cd90Schristos 	memcpy(nat_addr, &ss[0], sizeof(ss[0]));
62b525cd90Schristos 
63b525cd90Schristos 	switch (af = ss[0].ss_family) {
64b525cd90Schristos 	case AF_INET:
65b525cd90Schristos 		addr[0] = (void *)&satosin(nat_addr)->sin_addr;
66b525cd90Schristos 		addr[1] = __UNCONST(&csatosin(&ss[1])->sin_addr);
67b525cd90Schristos 		port[0] = csatosin(&ss[0])->sin_port;
68b525cd90Schristos 		port[1] = csatosin(&ss[1])->sin_port;
69b525cd90Schristos 		break;
70b525cd90Schristos 	case AF_INET6:
71b525cd90Schristos 		addr[0] = (void *)&satosin6(&nat_addr)->sin6_addr;
72b525cd90Schristos 		addr[1] = __UNCONST(&csatosin6(&ss[0])->sin6_addr);
73b525cd90Schristos 		port[0] = csatosin6(&ss[0])->sin6_port;
74b525cd90Schristos 		port[1] = csatosin6(&ss[1])->sin6_port;
75b525cd90Schristos 		break;
76b525cd90Schristos 	default:
77b525cd90Schristos 		errno = EAFNOSUPPORT;
78b525cd90Schristos 		maybe_syslog(LOG_ERR, "NAT lookup for %d: %m" , af);
79b525cd90Schristos 		return 0;
80b525cd90Schristos 	}
81b525cd90Schristos 
82b525cd90Schristos 	/* Open the /dev/pf device and do the lookup. */
83b525cd90Schristos 	if ((dev = open("/dev/npf", O_RDWR)) == -1) {
84b525cd90Schristos 		maybe_syslog(LOG_ERR, "Cannot open /dev/npf: %m");
85b525cd90Schristos 		return 0;
86b525cd90Schristos 	}
87*a363700aSchristos 	if (npf_nat_lookup(dev, af, addr, port, IPPROTO_TCP, PFIL_OUT) == -1) {
88b525cd90Schristos 		maybe_syslog(LOG_ERR, "NAT lookup failure: %m");
89b525cd90Schristos 		(void)close(dev);
90b525cd90Schristos 		return 0;
91b525cd90Schristos 	}
92b525cd90Schristos 	(void)close(dev);
93b525cd90Schristos 
94b525cd90Schristos 	/*
95b525cd90Schristos 	 * The originating address is already set into nat_addr so fill
96b525cd90Schristos 	 * in the rest, family, port (ident), len....
97b525cd90Schristos 	 */
98b525cd90Schristos 	switch (af) {
99b525cd90Schristos 	case AF_INET:
100b525cd90Schristos 		satosin(nat_addr)->sin_port = htons(113);
101b525cd90Schristos 		satosin(nat_addr)->sin_len = sizeof(struct sockaddr_in);
102b525cd90Schristos 		satosin(nat_addr)->sin_family = AF_INET;
103b525cd90Schristos 		break;
104b525cd90Schristos 	case AF_INET6:
105b525cd90Schristos 		satosin6(nat_addr)->sin6_port = htons(113);
106b525cd90Schristos 		satosin6(nat_addr)->sin6_len = sizeof(struct sockaddr_in6);
107b525cd90Schristos 		satosin6(nat_addr)->sin6_family = AF_INET6;
108b525cd90Schristos 		break;
109b525cd90Schristos 	}
110b525cd90Schristos 	/* Put the originating port into nat_lport. */
111b525cd90Schristos 	*nat_lport = ntohs(port[0]);
112b525cd90Schristos 
113b525cd90Schristos 	return 1;
114b525cd90Schristos }
115