1 /* $NetBSD: npfctl.h,v 1.45 2017/12/10 22:04:41 rmind Exp $ */ 2 3 /*- 4 * Copyright (c) 2009-2017 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This material is based upon work partially supported by The 8 * NetBSD Foundation under a contract with Mindaugas Rasiukevicius. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 #ifndef _NPFCTL_H_ 33 #define _NPFCTL_H_ 34 35 #include <stdio.h> 36 #include <stdbool.h> 37 #include <inttypes.h> 38 #include <assert.h> 39 #include <util.h> 40 41 #define NPF_BPFCOP 42 #include <net/npf.h> 43 44 #define _NPF_PRIVATE 45 #include <npf.h> 46 47 #include "npf_var.h" 48 49 #define NPF_DEV_PATH "/dev/npf" 50 #define NPF_CONF_PATH "/etc/npf.conf" 51 #define NPF_DB_PATH "/var/db/npf.db" 52 53 typedef struct fam_addr_mask { 54 sa_family_t fam_family; 55 npf_addr_t fam_addr; 56 npf_netmask_t fam_mask; 57 unsigned long fam_ifindex; 58 } fam_addr_mask_t; 59 60 typedef struct ifnet_addr { 61 char * ifna_name; 62 unsigned long ifna_index; 63 sa_family_t ifna_family; 64 npfvar_t * ifna_filter; 65 npfvar_t * ifna_addrs; 66 } ifnet_addr_t; 67 68 typedef struct port_range { 69 in_port_t pr_start; 70 in_port_t pr_end; 71 } port_range_t; 72 73 typedef struct addr_port { 74 npfvar_t * ap_netaddr; 75 npfvar_t * ap_portrange; 76 } addr_port_t; 77 78 typedef struct filt_opts { 79 addr_port_t fo_from; 80 addr_port_t fo_to; 81 bool fo_finvert; 82 bool fo_tinvert; 83 } filt_opts_t; 84 85 typedef struct opt_proto { 86 int op_proto; 87 npfvar_t * op_opts; 88 } opt_proto_t; 89 90 typedef struct rule_group { 91 const char * rg_name; 92 uint32_t rg_attr; 93 const char * rg_ifname; 94 bool rg_default; 95 } rule_group_t; 96 97 typedef struct proc_call { 98 const char * pc_name; 99 npfvar_t * pc_opts; 100 } proc_call_t; 101 102 typedef struct proc_param { 103 const char * pp_param; 104 const char * pp_value; 105 } proc_param_t; 106 107 enum { NPFCTL_PARSE_FILE, NPFCTL_PARSE_STRING }; 108 109 bool join(char *, size_t, int, char **, const char *); 110 void yyerror(const char *, ...) __printflike(1, 2) __dead; 111 void npfctl_bpfjit(bool); 112 void npfctl_parse_file(const char *); 113 void npfctl_parse_string(const char *); 114 115 void npfctl_print_error(const npf_error_t *); 116 char * npfctl_print_addrmask(int, const char *, const npf_addr_t *, 117 npf_netmask_t); 118 void npfctl_note_interface(const char *); 119 unsigned npfctl_table_getid(const char *); 120 int npfctl_protono(const char *); 121 in_port_t npfctl_portno(const char *); 122 uint8_t npfctl_icmpcode(int, uint8_t, const char *); 123 uint8_t npfctl_icmptype(int, const char *); 124 npfvar_t * npfctl_ifnet_table(const char *); 125 npfvar_t * npfctl_parse_ifnet(const char *, const int); 126 npfvar_t * npfctl_parse_tcpflag(const char *); 127 npfvar_t * npfctl_parse_table_id(const char *); 128 npfvar_t * npfctl_parse_icmp(int, int, int); 129 npfvar_t * npfctl_parse_port_range(in_port_t, in_port_t); 130 npfvar_t * npfctl_parse_port_range_variable(const char *, npfvar_t *); 131 npfvar_t * npfctl_parse_fam_addr_mask(const char *, const char *, 132 unsigned long *); 133 bool npfctl_parse_cidr(char *, fam_addr_mask_t *, int *); 134 uint16_t npfctl_npt66_calcadj(npf_netmask_t, const npf_addr_t *, 135 const npf_addr_t *); 136 137 /* 138 * NPF extension loading. 139 */ 140 141 typedef struct npf_extmod npf_extmod_t; 142 143 npf_extmod_t * npf_extmod_get(const char *, nl_ext_t **); 144 int npf_extmod_param(npf_extmod_t *, nl_ext_t *, 145 const char *, const char *); 146 147 /* 148 * BFF byte-code generation interface. 149 */ 150 151 typedef struct npf_bpf npf_bpf_t; 152 153 #define MATCH_DST 0x01 154 #define MATCH_SRC 0x02 155 #define MATCH_INVERT 0x04 156 157 enum { 158 BM_IPVER, BM_PROTO, BM_SRC_CIDR, BM_SRC_TABLE, BM_DST_CIDR, 159 BM_DST_TABLE, BM_SRC_PORTS, BM_DST_PORTS, BM_TCPFL, BM_ICMP_TYPE, 160 BM_ICMP_CODE, 161 }; 162 163 npf_bpf_t * npfctl_bpf_create(void); 164 struct bpf_program *npfctl_bpf_complete(npf_bpf_t *); 165 const void * npfctl_bpf_bmarks(npf_bpf_t *, size_t *); 166 void npfctl_bpf_destroy(npf_bpf_t *); 167 168 void npfctl_bpf_group(npf_bpf_t *); 169 void npfctl_bpf_endgroup(npf_bpf_t *, bool); 170 171 void npfctl_bpf_proto(npf_bpf_t *, sa_family_t, int); 172 void npfctl_bpf_cidr(npf_bpf_t *, u_int, sa_family_t, 173 const npf_addr_t *, const npf_netmask_t); 174 void npfctl_bpf_ports(npf_bpf_t *, u_int, in_port_t, in_port_t); 175 void npfctl_bpf_tcpfl(npf_bpf_t *, uint8_t, uint8_t, bool); 176 void npfctl_bpf_icmp(npf_bpf_t *, int, int); 177 void npfctl_bpf_table(npf_bpf_t *, u_int, u_int); 178 179 /* 180 * Configuration building interface. 181 */ 182 183 #define NPFCTL_NAT_DYNAMIC 1 184 #define NPFCTL_NAT_STATIC 2 185 186 void npfctl_config_init(bool); 187 int npfctl_config_send(int, const char *); 188 nl_config_t * npfctl_config_ref(void); 189 int npfctl_config_show(int); 190 void npfctl_config_save(nl_config_t *, const char *); 191 192 void npfctl_show_init(void); 193 int npfctl_ruleset_show(int, const char *); 194 195 nl_rule_t * npfctl_rule_ref(void); 196 bool npfctl_debug_addif(const char *); 197 198 void npfctl_build_alg(const char *); 199 void npfctl_build_rproc(const char *, npfvar_t *); 200 void npfctl_build_group(const char *, int, const char *, bool); 201 void npfctl_build_group_end(void); 202 void npfctl_build_rule(uint32_t, const char *, sa_family_t, 203 const opt_proto_t *, const filt_opts_t *, 204 const char *, const char *); 205 void npfctl_build_natseg(int, int, unsigned, const char *, 206 const addr_port_t *, const addr_port_t *, 207 const opt_proto_t *, const filt_opts_t *, unsigned); 208 void npfctl_build_maprset(const char *, int, const char *); 209 void npfctl_build_table(const char *, u_int, const char *); 210 211 /* 212 * For the systems which do not define TH_ECE and TW_CRW. 213 */ 214 #ifndef TH_ECE 215 #define TH_ECE 0x40 216 #endif 217 #ifndef TH_CWR 218 #define TH_CWR 0x80 219 #endif 220 221 #endif 222