Changeset View
Changeset View
Standalone View
Standalone View
sys/netpfil/ipfw/nat64/nat64lsn.h
| /*- | /*- | ||||
| * SPDX-License-Identifier: BSD-2-Clause | * SPDX-License-Identifier: BSD-2-Clause-FreeBSD | ||||
| * | * | ||||
| * Copyright (c) 2015-2019 Yandex LLC | * Copyright (c) 2015-2020 Yandex LLC | ||||
| * Copyright (c) 2015 Alexander V. Chernikov <melifaro@FreeBSD.org> | * Copyright (c) 2015 Alexander V. Chernikov <melifaro@FreeBSD.org> | ||||
| * Copyright (c) 2015-2019 Andrey V. Elsukov <ae@FreeBSD.org> | * Copyright (c) 2015-2020 Andrey V. Elsukov <ae@FreeBSD.org> | ||||
| * | * | ||||
| * Redistribution and use in source and binary forms, with or without | * Redistribution and use in source and binary forms, with or without | ||||
| * modification, are permitted provided that the following conditions | * modification, are permitted provided that the following conditions | ||||
| * are met: | * are met: | ||||
| * | * | ||||
| * 1. Redistributions of source code must retain the above copyright | * 1. Redistributions of source code must retain the above copyright | ||||
| * notice, this list of conditions and the following disclaimer. | * notice, this list of conditions and the following disclaimer. | ||||
| * 2. Redistributions in binary form must reproduce the above copyright | * 2. Redistributions in binary form must reproduce the above copyright | ||||
| ▲ Show 20 Lines • Show All 44 Lines • ▼ Show 20 Lines | |||||
| struct nat64lsn_states_chunk { | struct nat64lsn_states_chunk { | ||||
| struct nat64lsn_state state[64]; | struct nat64lsn_state state[64]; | ||||
| }; | }; | ||||
| #define ISSET64(mask, bit) ((mask) & ((uint64_t)1 << (bit))) | #define ISSET64(mask, bit) ((mask) & ((uint64_t)1 << (bit))) | ||||
| #define ISSET32(mask, bit) ((mask) & ((uint32_t)1 << (bit))) | #define ISSET32(mask, bit) ((mask) & ((uint32_t)1 << (bit))) | ||||
| struct nat64lsn_pg { | struct nat64lsn_pg { | ||||
| CK_SLIST_ENTRY(nat64lsn_pg) entries; | |||||
| uint16_t base_port; | uint16_t base_port; | ||||
| uint16_t timestamp; | uint16_t timestamp; | ||||
| uint8_t proto; | uint8_t proto; | ||||
| uint8_t chunks_count; | uint8_t chunks_count; | ||||
| uint8_t spare[2]; | uint16_t flags; | ||||
| #define NAT64LSN_DEADPG 1 | |||||
| union { | union { | ||||
| uint64_t freemask64; | uint64_t freemask64; | ||||
| uint32_t freemask32[2]; | uint32_t freemask32[2]; | ||||
| uint64_t *freemask64_chunk; | uint64_t *freemask64_chunk; | ||||
| uint32_t *freemask32_chunk; | uint32_t *freemask32_chunk; | ||||
| void *freemask_chunk; | void *freemask_chunk; | ||||
| }; | }; | ||||
| union { | union { | ||||
| struct nat64lsn_states_chunk *states; | struct nat64lsn_states_chunk *states; | ||||
| struct nat64lsn_states_chunk **states_chunk; | struct nat64lsn_states_chunk **states_chunk; | ||||
| }; | }; | ||||
| /* | |||||
| * An alias object holds chain of all allocated PGs. | |||||
| * The chain is used mostly by expiration code. | |||||
| */ | |||||
| CK_SLIST_ENTRY(nat64lsn_pg) entries; | |||||
| }; | }; | ||||
| #define CHUNK_BY_FADDR(p, a) ((a) & ((p)->chunks_count - 1)) | #define CHUNK_BY_FADDR(p, a) ((a) & ((p)->chunks_count - 1)) | ||||
| #ifdef __LP64__ | #ifdef __LP64__ | ||||
| #define FREEMASK_CHUNK(p, v) \ | #define FREEMASK_CHUNK(p, v) \ | ||||
| ((p)->chunks_count == 1 ? &(p)->freemask64 : \ | ((p)->chunks_count == 1 ? &(p)->freemask64 : \ | ||||
| &(p)->freemask64_chunk[CHUNK_BY_FADDR(p, v)]) | &(p)->freemask64_chunk[CHUNK_BY_FADDR(p, v)]) | ||||
| Show All 24 Lines | |||||
| struct nat64lsn_alias { | struct nat64lsn_alias { | ||||
| struct nat64lsn_aliaslink_slist hosts; | struct nat64lsn_aliaslink_slist hosts; | ||||
| struct nat64lsn_pg_slist portgroups; | struct nat64lsn_pg_slist portgroups; | ||||
| struct mtx lock; | struct mtx lock; | ||||
| in_addr_t addr; /* host byte order */ | in_addr_t addr; /* host byte order */ | ||||
| uint32_t hosts_count; | uint32_t hosts_count; | ||||
| uint32_t portgroups_count; | |||||
| uint32_t tcp_chunkmask; | |||||
| uint32_t udp_chunkmask; | |||||
| uint32_t icmp_chunkmask; | |||||
| uint16_t timestamp; | |||||
| uint16_t tcp_pgcount; | |||||
| uint16_t udp_pgcount; | |||||
| uint16_t icmp_pgcount; | |||||
| /* | |||||
| * We keep PGs in chunks by 32 PGs in each. | |||||
| * Each chunk allocated by demand, and then corresponding bit | |||||
| * is set in chunkmask. | |||||
| * | |||||
| * Also we keep last used PG's index for each protocol. | |||||
| * pgidx / 32 = index of pgchunk; | |||||
| * pgidx % 32 = index of pgptr in pgchunk. | |||||
| */ | |||||
| uint32_t tcp_chunkmask; | |||||
| uint32_t tcp_pgidx; | uint32_t tcp_pgidx; | ||||
| uint32_t udp_chunkmask; | |||||
| uint32_t udp_pgidx; | uint32_t udp_pgidx; | ||||
| uint32_t icmp_pgidx; | |||||
| uint16_t timestamp; | |||||
| uint16_t spare; | |||||
| uint32_t icmp_chunkmask; | |||||
| uint32_t icmp_pgidx; | |||||
| /* | |||||
| * Each pgchunk keeps 32 pointers to PGs. If pgptr pointer is | |||||
| * valid, we have corresponding bit set in the pgmask. | |||||
| */ | |||||
| uint32_t tcp_pgmask[32]; | uint32_t tcp_pgmask[32]; | ||||
| uint32_t udp_pgmask[32]; | uint32_t udp_pgmask[32]; | ||||
| uint32_t icmp_pgmask[32]; | uint32_t icmp_pgmask[32]; | ||||
| struct nat64lsn_pgchunk *tcp[32]; | struct nat64lsn_pgchunk *tcp[32]; | ||||
| struct nat64lsn_pgchunk *udp[32]; | struct nat64lsn_pgchunk *udp[32]; | ||||
| struct nat64lsn_pgchunk *icmp[32]; | struct nat64lsn_pgchunk *icmp[32]; | ||||
| /* pointer to PG that can be used for faster state allocation */ | |||||
| struct nat64lsn_pg *tcp_pg; | |||||
| struct nat64lsn_pg *udp_pg; | |||||
| struct nat64lsn_pg *icmp_pg; | |||||
| }; | }; | ||||
| #define ALIAS_LOCK_INIT(p) \ | #define ALIAS_LOCK_INIT(p) \ | ||||
| mtx_init(&(p)->lock, "alias_lock", NULL, MTX_DEF) | mtx_init(&(p)->lock, "alias_lock", NULL, MTX_DEF) | ||||
| #define ALIAS_LOCK_DESTROY(p) mtx_destroy(&(p)->lock) | #define ALIAS_LOCK_DESTROY(p) mtx_destroy(&(p)->lock) | ||||
| #define ALIAS_LOCK(p) mtx_lock(&(p)->lock) | #define ALIAS_LOCK(p) mtx_lock(&(p)->lock) | ||||
| #define ALIAS_UNLOCK(p) mtx_unlock(&(p)->lock) | #define ALIAS_UNLOCK(p) mtx_unlock(&(p)->lock) | ||||
| #define NAT64LSN_HSIZE 256 | #define NAT64LSN_HSIZE 256 | ||||
| Show All 16 Lines | |||||
| }; | }; | ||||
| #define HOST_LOCK_INIT(p) \ | #define HOST_LOCK_INIT(p) \ | ||||
| mtx_init(&(p)->lock, "host_lock", NULL, MTX_DEF|MTX_NEW) | mtx_init(&(p)->lock, "host_lock", NULL, MTX_DEF|MTX_NEW) | ||||
| #define HOST_LOCK_DESTROY(p) mtx_destroy(&(p)->lock) | #define HOST_LOCK_DESTROY(p) mtx_destroy(&(p)->lock) | ||||
| #define HOST_LOCK(p) mtx_lock(&(p)->lock) | #define HOST_LOCK(p) mtx_lock(&(p)->lock) | ||||
| #define HOST_UNLOCK(p) mtx_unlock(&(p)->lock) | #define HOST_UNLOCK(p) mtx_unlock(&(p)->lock) | ||||
| VNET_DECLARE(uint16_t, nat64lsn_eid); | VNET_DECLARE(uint32_t, nat64lsn_eid); | ||||
| #define V_nat64lsn_eid VNET(nat64lsn_eid) | #define V_nat64lsn_eid VNET(nat64lsn_eid) | ||||
| #define IPFW_TLV_NAT64LSN_NAME IPFW_TLV_EACTION_NAME(V_nat64lsn_eid) | #define IPFW_TLV_NAT64LSN_NAME IPFW_TLV_EACTION_NAME(V_nat64lsn_eid) | ||||
| /* Timestamp macro */ | /* Timestamp macro */ | ||||
| #define _CT ((int)time_uptime % 65536) | #define _CT ((int)time_uptime % 65536) | ||||
| #define SET_AGE(x) (x) = _CT | #define SET_AGE(x) (x) = _CT | ||||
| #define GET_AGE(x) ((_CT >= (x)) ? _CT - (x): (int)65536 + _CT - (x)) | #define GET_AGE(x) ((_CT >= (x)) ? _CT - (x): (int)65536 + _CT - (x)) | ||||
| STAILQ_HEAD(nat64lsn_job_head, nat64lsn_job_item); | STAILQ_HEAD(nat64lsn_job_head, nat64lsn_job_item); | ||||
| struct nat64lsn_cfg { | struct nat64lsn_cfg { | ||||
| struct named_object no; | |||||
| struct nat64lsn_hosts_slist *hosts_hash; | struct nat64lsn_hosts_slist *hosts_hash; | ||||
| struct nat64lsn_alias *aliases; /* array of aliases */ | struct nat64lsn_alias *aliases; /* array of aliases */ | ||||
| struct mtx lock; | struct mtx lock; | ||||
| uint32_t hosts_hashsize; | uint32_t hosts_hashsize; | ||||
| uint32_t hash_seed; | uint32_t hash_seed; | ||||
| uint32_t prefix4; /* IPv4 prefix */ | uint32_t prefix4; /* IPv4 prefix */ | ||||
| Show All 9 Lines | struct nat64lsn_cfg { | ||||
| uint16_t pg_delete_delay; /* Stale portgroup del delay */ | uint16_t pg_delete_delay; /* Stale portgroup del delay */ | ||||
| uint16_t st_syn_ttl; /* TCP syn expire */ | uint16_t st_syn_ttl; /* TCP syn expire */ | ||||
| uint16_t st_close_ttl; /* TCP fin expire */ | uint16_t st_close_ttl; /* TCP fin expire */ | ||||
| uint16_t st_estab_ttl; /* TCP established expire */ | uint16_t st_estab_ttl; /* TCP established expire */ | ||||
| uint16_t st_udp_ttl; /* UDP expire */ | uint16_t st_udp_ttl; /* UDP expire */ | ||||
| uint16_t st_icmp_ttl; /* ICMP expire */ | uint16_t st_icmp_ttl; /* ICMP expire */ | ||||
| struct nat64_config base; | struct nat64_config base; | ||||
| #define NAT64LSN_FLAGSMASK (NAT64_LOG | NAT64_ALLOW_PRIVATE) | #define NAT64LSN_FLAGSMASK (NAT64_LOG | NAT64_ALLOW_PRIVATE | \ | ||||
| NAT64LSN_ALLOW_SWAPCONF) | |||||
| #define NAT64LSN_ANYPREFIX 0x00000100 | #define NAT64LSN_ANYPREFIX 0x00000100 | ||||
| struct mtx periodic_lock; | struct mtx periodic_lock; | ||||
| struct callout periodic; | struct callout periodic; | ||||
| struct callout jcallout; | struct callout jcallout; | ||||
| struct vnet *vp; | struct vnet *vp; | ||||
| struct nat64lsn_job_head jhead; | struct nat64lsn_job_head jhead; | ||||
| int jlen; | int jlen; | ||||
| char name[64]; /* Nat instance name */ | char name[64]; /* Nat instance name */ | ||||
| }; | }; | ||||
| struct nat64lsn_instance { | |||||
| struct named_object no; | |||||
| struct nat64lsn_cfg *cfg; | |||||
| char name[64]; /* Nat instance name */ | |||||
| }; | |||||
| /* CFG_LOCK protects cfg->hosts_hash from modification */ | /* CFG_LOCK protects cfg->hosts_hash from modification */ | ||||
| #define CFG_LOCK_INIT(p) \ | #define CFG_LOCK_INIT(p) \ | ||||
| mtx_init(&(p)->lock, "cfg_lock", NULL, MTX_DEF) | mtx_init(&(p)->lock, "cfg_lock", NULL, MTX_DEF) | ||||
| #define CFG_LOCK_DESTROY(p) mtx_destroy(&(p)->lock) | #define CFG_LOCK_DESTROY(p) mtx_destroy(&(p)->lock) | ||||
| #define CFG_LOCK(p) mtx_lock(&(p)->lock) | #define CFG_LOCK(p) mtx_lock(&(p)->lock) | ||||
| #define CFG_UNLOCK(p) mtx_unlock(&(p)->lock) | #define CFG_UNLOCK(p) mtx_unlock(&(p)->lock) | ||||
| #define CALLOUT_LOCK_INIT(p) \ | #define CALLOUT_LOCK_INIT(p) \ | ||||
| mtx_init(&(p)->periodic_lock, "periodic_lock", NULL, MTX_DEF) | mtx_init(&(p)->periodic_lock, "periodic_lock", NULL, MTX_DEF) | ||||
| #define CALLOUT_LOCK_DESTROY(p) mtx_destroy(&(p)->periodic_lock) | #define CALLOUT_LOCK_DESTROY(p) mtx_destroy(&(p)->periodic_lock) | ||||
| #define CALLOUT_LOCK(p) mtx_lock(&(p)->periodic_lock) | #define CALLOUT_LOCK(p) mtx_lock(&(p)->periodic_lock) | ||||
| #define CALLOUT_UNLOCK(p) mtx_unlock(&(p)->periodic_lock) | #define CALLOUT_UNLOCK(p) mtx_unlock(&(p)->periodic_lock) | ||||
| struct nat64lsn_cfg *nat64lsn_init_instance(struct ip_fw_chain *ch, | MALLOC_DECLARE(M_NAT64LSN); | ||||
| struct nat64lsn_cfg *nat64lsn_init_config(struct ip_fw_chain *ch, | |||||
| in_addr_t prefix, int plen); | in_addr_t prefix, int plen); | ||||
| void nat64lsn_destroy_instance(struct nat64lsn_cfg *cfg); | void nat64lsn_destroy_config(struct nat64lsn_cfg *cfg); | ||||
| void nat64lsn_start_instance(struct nat64lsn_cfg *cfg); | void nat64lsn_start_instance(struct nat64lsn_cfg *cfg); | ||||
| void nat64lsn_init_internal(void); | void nat64lsn_init_internal(void); | ||||
| void nat64lsn_uninit_internal(void); | void nat64lsn_uninit_internal(void); | ||||
| int ipfw_nat64lsn(struct ip_fw_chain *ch, struct ip_fw_args *args, | int ipfw_nat64lsn(struct ip_fw_chain *ch, struct ip_fw_args *args, | ||||
| ipfw_insn *cmd, int *done); | ipfw_insn *cmd, int *done); | ||||
| #endif /* _IP_FW_NAT64LSN_H_ */ | #endif /* _IP_FW_NAT64LSN_H_ */ | ||||