1 /* Copyright (C) 2017 Fastly, Inc.
2
3 This program is free software: you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation, either version 3 of the License, or
6 (at your option) any later version.
7
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12
13 You should have received a copy of the GNU General Public License
14 along with this program. If not, see <https://www.gnu.org/licenses/>.
15 */
16
17 #include <netinet/in.h>
18
19 #include "knot/include/module.h"
20
whoami_query(knotd_in_state_t state,knot_pkt_t * pkt,knotd_qdata_t * qdata,knotd_mod_t * mod)21 static knotd_in_state_t whoami_query(knotd_in_state_t state, knot_pkt_t *pkt,
22 knotd_qdata_t *qdata, knotd_mod_t *mod)
23 {
24 assert(pkt && qdata);
25
26 const knot_dname_t *zone_name = knotd_qdata_zone_name(qdata);
27 if (zone_name == NULL) {
28 return KNOTD_IN_STATE_ERROR;
29 }
30
31 /* Retrieve the query tuple. */
32 const knot_dname_t *qname = knot_pkt_qname(qdata->query);
33 const uint16_t qtype = knot_pkt_qtype(qdata->query);
34 const uint16_t qclass = knot_pkt_qclass(qdata->query);
35
36 /* We only generate A and AAAA records, which are Internet class. */
37 if (qclass != KNOT_CLASS_IN) {
38 return state;
39 }
40
41 /* Only handle queries with qname set to the zone name. */
42 if (!knot_dname_is_equal(qname, zone_name)) {
43 return state;
44 }
45
46 /* Only handle A and AAAA queries. */
47 if (qtype != KNOT_RRTYPE_A && qtype != KNOT_RRTYPE_AAAA) {
48 return state;
49 }
50
51 /* Retrieve the IP address that sent the query. */
52 const struct sockaddr_storage *query_source = knotd_qdata_remote_addr(qdata);
53 if (query_source == NULL) {
54 return KNOTD_IN_STATE_ERROR;
55 }
56
57 /* If the socket address family corresponds to the query type (i.e.,
58 * AF_INET <-> A and AF_INET6 <-> AAAA), put the socket address and
59 * length into 'rdata' and 'len_rdata'.
60 */
61 const void *rdata = NULL;
62 uint16_t len_rdata = 0;
63 if (query_source->ss_family == AF_INET && qtype == KNOT_RRTYPE_A) {
64 const struct sockaddr_in *sai = (struct sockaddr_in *)query_source;
65 rdata = &sai->sin_addr.s_addr;
66 len_rdata = sizeof(sai->sin_addr.s_addr);
67 } else if (query_source->ss_family == AF_INET6 && qtype == KNOT_RRTYPE_AAAA) {
68 const struct sockaddr_in6 *sai6 = (struct sockaddr_in6 *)query_source;
69 rdata = &sai6->sin6_addr;
70 len_rdata = sizeof(sai6->sin6_addr);
71 } else {
72 /* Query type didn't match address family. */
73 return state;
74 }
75
76 /* Synthesize the response RRset. */
77
78 /* TTL is taken from the TTL of the SOA record. */
79 knot_rrset_t soa = knotd_qdata_zone_apex_rrset(qdata, KNOT_RRTYPE_SOA);
80
81 /* Owner name, type, and class are taken from the question. */
82 knot_rrset_t *rrset = knot_rrset_new(qname, qtype, qclass, soa.ttl, &pkt->mm);
83 if (rrset == NULL) {
84 return KNOTD_IN_STATE_ERROR;
85 }
86
87 /* Record data is the query source address. */
88 int ret = knot_rrset_add_rdata(rrset, rdata, len_rdata, &pkt->mm);
89 if (ret != KNOT_EOK) {
90 knot_rrset_free(rrset, &pkt->mm);
91 return KNOTD_IN_STATE_ERROR;
92 }
93
94 /* Add the new RRset to the response packet. */
95 ret = knot_pkt_put(pkt, KNOT_COMPR_HINT_QNAME, rrset, KNOT_PF_FREE);
96 if (ret != KNOT_EOK) {
97 knot_rrset_free(rrset, &pkt->mm);
98 return KNOTD_IN_STATE_ERROR;
99 }
100
101 /* Success. */
102 return KNOTD_IN_STATE_HIT;
103 }
104
whoami_load(knotd_mod_t * mod)105 int whoami_load(knotd_mod_t *mod)
106 {
107 /* Hook to the query plan. */
108 knotd_mod_in_hook(mod, KNOTD_STAGE_ANSWER, whoami_query);
109
110 return KNOT_EOK;
111 }
112
113 KNOTD_MOD_API(whoami, KNOTD_MOD_FLAG_SCOPE_ZONE | KNOTD_MOD_FLAG_OPT_CONF,
114 whoami_load, NULL, NULL, NULL);
115