Files
Thomas Turner 4b6d521246 Merge tag 'v4.19.325-cip128' of https://git.kernel.org/pub/scm/linux/kernel/git/cip/linux-cip into android13-4.19-kona
version 4.19.325-cip128

* tag 'v4.19.325-cip128' of https://git.kernel.org/pub/scm/linux/kernel/git/cip/linux-cip:
  CIP: Bump version suffix to -cip128 after merge from cip/linux-4.19.y-st tree
  Update localversion-st, tree is up-to-date with 5.10.248.
  NFSv4: ensure the open stateid seqid doesn't go backwards
  libceph: make calc_target() set t->paused, not just clear it
  crypto: af_alg - zero initialize memory allocated via sock_kmalloc
  jbd2: fix the inconsistency between checksum and data in memory for journal sb
  f2fs: fix to detect recoverable inode during dryrun of find_fsync_dnodes()
  NFS: add barriers when testing for NFS_FSDATA_BLOCKED
  NFS: unlink/rmdir shouldn't call d_delete() twice on ENOENT
  efi/cper: Fix cper_bits_to_str buffer handling and return value
  iommu/omap: fix device leaks on probe_device()
  crypto: seqiv - Do not use req->iv after crypto_aead_encrypt
  f2fs: fix return value of f2fs_recover_fsync_data()
  ext4: improve integrity checking in __mb_check_buddy by enhancing order-0 validation
  ext4: remove unused return value of __mb_check_buddy
  clk: renesas: r9a06g032: Fix memory leak in error path
  nbd: defer config put in recv_work
  jbd2: avoid bug_on in jbd2_journal_get_create_access() when file system corrupted
  scsi: sg: Fix occasional bogus elapsed time that exceeds timeout
  powercap: fix sscanf() error return value handling
  powercap: fix race condition in register_control_type()
  arp: do not assume dev_hard_header() does not change skb->head
  net: usb: pegasus: fix memory leak in update_eth_regs_async()
  HID: quirks: work around VID/PID conflict for appledisplay
  netdev: preserve NETIF_F_ALL_FOR_ALL across TSO updates
  net: sock: fix hardened usercopy panic in sock_recv_errqueue
  inet: ping: Fix icmp out counting
  netfilter: nf_conncount: update last_gc only when GC has been performed
  ARM: dts: imx6q-ba16: fix RTC interrupt level
  scsi: ipr: Enable/disable IRQD_NO_BALANCING during reset
  alpha: don't reference obsolete termio struct for TC* constants
  ARM: 9461/1: Disable HIGHPTE on PREEMPT_RT kernels
  ext4: fix out-of-bound read in ext4_xattr_inode_dec_ref_all()
  ext4: introduce ITAIL helper
  libceph: make free_choose_arg_map() resilient to partial allocation
  libceph: replace overzealous BUG_ON in osdmap_apply_incremental()
  wifi: avoid kernel-infoleak from struct iw_point
  drm/pl111: Fix error handling in pl111_amba_probe
  net: 3com: 3c59x: fix possible null dereference in vortex_probe1()
  atm: Fix dma_free_coherent() size
  Revert "iommu/amd: Skip enabling command/event buffers for kdump"
  pwm: stm32: Always program polarity
  bus: fsl-mc-bus: fix KASAN use-after-free in fsl_mc_bus_remove()
  scsi: iscsi_tcp: Fix UAF during logout when accessing the shost ipaddress
  scsi: iscsi: Move pool freeing
  wifi: mac80211: Discard Beacon frames to non-broadcast address
  media: samsung: exynos4-is: fix potential ABBA deadlock on init
  media: vpif_capture: fix section mismatch
  media: renesas: rcar_drif: fix device node reference leak in rcar_drif_bond_enabled
  SUNRPC: svcauth_gss: avoid NULL deref on zero length gss_token in gss_read_proxy_verf
  NFSD: Clear SECLABEL in the suppattr_exclcreat bitmap
  usb: ohci-nxp: fix device leak on probe failure
  usb: ohci-nxp: Use helper function devm_clk_get_enabled()
  f2fs: fix to avoid updating zero-sized extent in extent cache
  ext4: fix string copying in parse_apply_sb_mount_options()
  ALSA: wavefront: Fix integer overflow in sample size validation
  ALSA: wavefront: Clear substream pointers on close
  usb: gadget: udc: fix use-after-free in usb_gadget_state_work
  HID: core: Harden s32ton() against conversion to 0 bits
  ipv4: Fix uninit-value access in __ip_make_skb()
  ipv6: Fix potential uninit-value access in __ip6_make_skb()
  virtio_console: fix order of fields cols and rows
  drm/nouveau/dispnv50: Don't call drm_atomic_get_crtc_state() in prepare_fb
  net: nfc: fix deadlock between nfc_unregister_device and rfkill_fop_write
  net: usb: sr9700: fix incorrect command used to write single register
  fjes: Add missing iounmap in fjes_hw_init()
  e1000: fix OOB in e1000_tbi_should_accept()
  RDMA/core: Check for the presence of LS_NLA_TYPE_DGID correctly
  media: i2c: adv7842: Remove redundant cancel_delayed_work in probe
  media: i2c: ADV7604: Remove redundant cancel_delayed_work in probe
  media: TDA1997x: Remove redundant cancel_delayed_work in probe
  media: msp3400: Avoid possible out-of-bounds array accesses in msp3400c_thread()
  media: cec: Fix debugfs leak on bus_register() failure
  fbdev: tcx.c fix mem_map to correct smem_start offset
  fbdev: pxafb: Fix multiple clamped values in pxafb_adjust_timing
  fbdev: gbefb: fix to use physical address instead of dma address
  media: adv7842: Avoid possible out-of-bounds array accesses in adv7842_cp_log_status()
  parisc: entry: set W bit for !compat tasks in syscall_restore_rfi()
  parisc: entry.S: fix space adjustment on interruption for 64-bit userspace
  media: rc: st_rc: Fix reset control resource leak
  PCI/PM: Reinstate clearing state_saved in legacy and !PM codepaths
  iommu/exynos: fix device leak on of_xlate()
  ASoC: qcom: qdsp6: q6asm-dai: set 10 ms period and buffer alignment.
  ASoC: qcom: q6adm: the the copp device only during last instance
  ASoC: qcom: q6asm-dai: perform correct state check before closing
  selftests/ftrace: traceonoff_triggers: strip off names
  RDMA/bnxt_re: fix dma_free_coherent() pointer
  net: rose: fix invalid array index in rose_kill_by_device()
  ipv4: Fix reference count leak when using error routes with nexthop objects
  ipv6: BUG() in pskb_expand_head() as part of calipso_skbuff_setattr()
  net: bridge: Describe @tunnel_hash member in net_bridge_vlan_group struct
  net: dsa: b53: skip multicast entries for fdb_dump()
  firewire: nosy: Fix dma_free_coherent() size
  firewire: nosy: switch from 'pci_' to 'dma_' API
  genalloc.h: fix htmldocs warning
  net: usb: rtl8150: fix memory leak on usb_submit_urb() failure
  team: fix check for port enabled in team_queue_override_port_prio_changed()
  platform/x86: ibm_rtl: fix EBDA signature search pointer arithmetic
  platform/x86: msi-laptop: add missing sysfs_remove_group()
  ip6_gre: make ip6gre_header() robust
  i40e: fix scheduling in set_rx_mode
  hwmon: (w83l786ng) Convert macros to functions to avoid TOCTOU
  hwmon: (w83791d) Convert macros to functions to avoid TOCTOU
  rpmsg: glink: fix rpmsg device leak
  amba: tegra-ahb: Fix device leak on SMMU enable
  nfsd: Mark variable __maybe_unused to avoid W=1 build break
  PM: runtime: Do not clear needs_force_resume with enabled runtime PM
  tracing: Do not register unsupported perf events
  KVM: x86: Fix VM hard lockup after prolonged inactivity with periodic HV timer
  KVM: x86: Explicitly set new periodic hrtimer expiration in apic_timer_fn()
  KVM: x86: WARN if hrtimer callback for periodic APIC timer fires with period=0
  libceph: make decode_pool() more resilient against corrupted osdmaps
  parisc: Do not reprogram affinitiy on ASP chip
  ocfs2: fix kernel BUG in ocfs2_find_victim_chain
  tools/testing/nvdimm: Use per-DIMM device handle
  f2fs: invalidate dentry cache on failed whiteout creation
  scsi: target: Reset t_task_cdb pointer in error case
  NFSD: use correct reservation type in nfsd4_scsi_fence_client
  scsi: aic94xx: fix use-after-free in device removal path
  cpufreq: nforce2: fix reference count leak in nforce2
  char: applicom: fix NULL pointer dereference in ac_ioctl
  usb: renesas_usbhs: Fix a resource leak in usbhs_pipe_malloc()
  media: pvrusb2: Fix incorrect variable used in trace message
  media: dvb-usb: dtv5100: fix out-of-bounds in dtv5100_i2c_msg()
  usb: usb-storage: Maintain minimal modifications to the bcdDevice range.
  media: v4l2-mem2mem: Fix outdated documentation
  jbd2: use a weaker annotation in journal handling
  ext4: fix incorrect group number assertion in mb_check_buddy
  ext4: xattr: fix null pointer deref in ext4_raw_inode()
  ktest.pl: Fix uninitialized var in config-bisect.pl
  floppy: fix for PAGE_SIZE != 4KB
  usb: usb-storage: No additional quirks need to be added to the EL-R12 optical drive.
  usb: xhci: limit run_graceperiod for only usb 3.0 devices
  usb: typec: ucsi: Handle incorrect num_connectors capability
  via_wdt: fix critical boot hang due to unnamed resource allocation
  scsi: qla2xxx: Use reinit_completion on mbx_intr_comp
  powerpc/addnote: Fix overflow on 32-bit builds
  clk: mvebu: cp110 add CLK_IGNORE_UNUSED to pcie_x10, pcie_x11 & pcie_x4
  ipmi: Fix __scan_channels() failing to rescan channels
  ipmi: Fix the race between __scan_channels() and deliver_response()
  ALSA: usb-mixer: us16x08: validate meter packet indices
  ALSA: pcmcia: Fix resource leak in snd_pdacf_probe error path
  ALSA: vxpocket: Fix resource leak in vxpocket_probe error path
  spi: fsl-cpm: Check length parity before switching to 16 bit mode
  Input: ti_am335x_tsc - fix off-by-one error in wire_order validation
  MIPS: Fix a reference leak bug in ip22_check_gio()
  hwmon: (ibmpex) fix use-after-free in high/low store
  net/mlx5: fw_tracer, Handle escaped percent properly
  net/mlx5: fw_tracer, Validate format string parameters
  net/mlx5: fw_tracer, Add support for unrecognized string
  nfc: pn533: Fix error code in pn533_acr122_poweron_rdr()
  caif: fix integer underflow in cffrml_receive()
  ipvs: fix ipv4 null-ptr-deref in route error path
  netfilter: nf_conncount: fix leaked ct in error paths
  broadcom: b44: prevent uninitialized value usage
  net: openvswitch: fix middle attribute validation in push_nsh() action
  mlxsw: spectrum_router: Fix neighbour use-after-free
  ipvlan: Ignore PACKET_LOOPBACK in handle_mode_l2()
  netrom: Fix memory leak in nr_sendmsg()
  btrfs: scrub: always update btrfs_scrub_progress::last_physical
  hfsplus: fix volume corruption issue for generic/073
  hfsplus: Verify inode mode when loading from disk
  hfsplus: fix missing hfs_bnode_get() in __hfs_bnode_create
  hfsplus: fix volume corruption issue for generic/070
  cpufreq: s5pv210: fix refcount leak
  ACPI: property: Use ACPI functions in acpi_graph_get_next_endpoint() only
  ACPICA: Avoid walking the Namespace if start_node is NULL
  netfilter: nft_connlimit: memleak if nf_ct_netns_get() fails
  netfilter: nf_conncount: garbage collection is not skipped when jiffies wrap around
  NFS: Fix missing unlock in nfs_unlink()
  ALSA: dice: fix buffer overflow in detect_stream_formats()
  usb: phy: Initialize struct usb_phy list_head
  ocfs2: fix memory leak in ocfs2_merge_rec_left()
  efi/cper: Adjust infopfx size to accept an extra space
  efi/cper: Add a new helper function to print bitmasks
  dm log-writes: Add missing set_freezable() for freezable kthread
  dm-raid: fix possible NULL dereference with undefined raid type
  ARM: 9464/1: fix input-only operand modification in load_unaligned_zeropad()
  ALSA: uapi: Fix typo in asound.h comment
  fs/nls: Fix inconsistency between utf8_to_utf32() and utf32_to_utf8()
  NFSv4/pNFS: Clear NFS_INO_LAYOUTCOMMIT in pnfs_mark_layout_stateid_invalid
  fs/nls: Fix utf16 to utf8 conversion
  NFS: don't unhash dentry during unlink/rename
  NFS: Label the dentry with a verifier in nfs_rmdir() and nfs_unlink()
  fbdev: ssd1307fb: fix potential page leak in ssd1307fb_probe()
  pinctrl: single: Fix incorrect type for error return variable
  pinctrl: single: Fix PIN_CONFIG_BIAS_DISABLE handling
  perf tools: Fix split kallsyms DSO counting
  net/sched: sch_cake: Fix incorrect qlen reduction in cake_drop
  mtd: lpddr_cmds: fix signed shifts in lpddr_cmds
  netfilter: nft_connlimit: update the count if add was skipped
  netfilter: nf_conncount: rework API to use sk_buff directly
  netfilter: nf_conncount: reduce unnecessary GC
  netfilter: nft_connlimit: move stateful fields out of expression data
  regulator: core: Protect regulator_supply_alias_list with regulator_list_mutex
  virtio: fix virtqueue_set_affinity() docs
  ACPI: processor_core: fix map_x2apic_id for amd-pstate on am4
  backlight: lp855x: Fix lp855x.h kernel-doc warnings
  staging: fbtft: core: fix potential memory leak in fbtft_probe_common()
  usb: dwc2: fix hang during shutdown if set as peripheral
  usb: dwc2: disable platform lowlevel hw resources during shutdown
  usb: chaoskey: fix locking for O_NONBLOCK
  wifi: rtl818x: rtl8187: Fix potential buffer underflow in rtl8187_rx_cb()
  powerpc/64s/ptdump: Fix kernel_hash_pagetable dump for ISA v3.00 HPTE format
  NFSD/blocklayout: Fix minlength check in proc_layoutget
  scsi: sim710: Fix resource leak by adding missing ioport_unmap() calls
  ACPI: property: Fix fwnode refcount leak in acpi_fwnode_graph_parse_endpoint()
  ocfs2: relax BUG() to ocfs2_error() in __ocfs2_move_extent()
  nbd: defer config unlock in nbd_genl_connect
  wifi: cw1200: Fix potential memory leak in cw1200_bh_rx_helper()
  macintosh/mac_hid: fix race condition in mac_hid_toggle_emumouse
  scsi: stex: Fix reboot_notifier leak in probe error path
  perf/x86/intel: Correct large PEBS flag check
  ext4: correct the checking of quota files before moving extents
  ext4: minor defrag code improvements
  mfd: da9055: Fix missing regmap_del_irq_chip() in error path
  scsi: target: Do not write NUL characters into ASCII configfs output
  power: supply: apm_power: only unset own apm_get_power_status
  power: supply: wm831x: Check wm831x_set_bits() return value
  kmsan: introduce __no_sanitize_memory and __no_kmsan_checks
  compiler-gcc.h: Define __SANITIZE_ADDRESS__ under hwaddress sanitizer
  s390/smp: Fix fallback CPU detection
  crypto: asymmetric_keys - prevent overflow in asymmetric_key_generate_id
  inet: Avoid ehash lookup race in inet_ehash_insert()
  rculist: Add hlist_nulls_replace_rcu() and hlist_nulls_replace_init_rcu()
  irqchip/qcom-irq-combiner: Fix section mismatch
  USB: Fix descriptor count when handling invalid MBIM extended descriptor
  drm/vgem-fence: Fix potential deadlock on release
  smack: fix bug: unprivileged task can create labels
  staging: rtl8723bs: fix stack buffer overflow in OnAssocReq IE parsing
  comedi: multiq3: sanitize config options in multiq3_attach()
  comedi: c6xdigio: Fix invalid PNP driver unregistration
  platform/x86: acer-wmi: Ignore backlight event
  bfs: Reconstruct file type when loading from disk
  spi: imx: keep dma request disabled before dma transfer setup
  spi: xilinx: increase number of retries before declaring stall
  USB: serial: kobil_sct: fix TIOCMBIS and TIOCMBIC
  USB: serial: belkin_sa: fix TIOCMBIS and TIOCMBIC
  serial: add support of CPCI cards
  USB: serial: ftdi_sio: match on interface number for jtag
  USB: serial: option: move Telit 0x10c7 composition in the right place
  USB: serial: option: add Telit Cinterion FE910C04 new compositions
  USB: serial: option: add Foxconn T99W760
  ext4: add i_data_sem protection in ext4_destroy_inline_data_nolock()
  locking/spinlock/debug: Fix data-race in do_raw_write_lock
  ext4: refresh inline data size before write operations
  xfrm: flush all states in xfrm_state_fini
  xfrm: also call xfrm_state_delete_tunnel at destroy time for states that were never added
  Revert "xfrm: destroy xfrm_state synchronously on net exit path"
  xfrm: delete x->tunnel as we delete x

 Conflicts:
	drivers/rpmsg/qcom_glink_native.c
	drivers/usb/host/xhci-hub.c
	fs/f2fs/file.c
	fs/f2fs/super.c

Change-Id: I9e00c074af0214588acc89a73fcfc928d1724b7a
2026-02-06 18:23:19 +02:00

2231 lines
58 KiB
C

// SPDX-License-Identifier: GPL-2.0-only
/*
* Copyright (c) 2015 Nicira, Inc.
*/
#include <linux/module.h>
#include <linux/openvswitch.h>
#include <linux/tcp.h>
#include <linux/udp.h>
#include <linux/sctp.h>
#include <linux/static_key.h>
#include <net/ip.h>
#include <net/genetlink.h>
#include <net/netfilter/nf_conntrack_core.h>
#include <net/netfilter/nf_conntrack_count.h>
#include <net/netfilter/nf_conntrack_helper.h>
#include <net/netfilter/nf_conntrack_labels.h>
#include <net/netfilter/nf_conntrack_seqadj.h>
#include <net/netfilter/nf_conntrack_zones.h>
#include <net/netfilter/ipv6/nf_defrag_ipv6.h>
#include <net/ipv6_frag.h>
#ifdef CONFIG_NF_NAT_NEEDED
#include <linux/netfilter/nf_nat.h>
#include <net/netfilter/nf_nat_core.h>
#include <net/netfilter/nf_nat_l3proto.h>
#endif
#include "datapath.h"
#include "conntrack.h"
#include "flow.h"
#include "flow_netlink.h"
struct ovs_ct_len_tbl {
int maxlen;
int minlen;
};
/* Metadata mark for masked write to conntrack mark */
struct md_mark {
u32 value;
u32 mask;
};
/* Metadata label for masked write to conntrack label. */
struct md_labels {
struct ovs_key_ct_labels value;
struct ovs_key_ct_labels mask;
};
enum ovs_ct_nat {
OVS_CT_NAT = 1 << 0, /* NAT for committed connections only. */
OVS_CT_SRC_NAT = 1 << 1, /* Source NAT for NEW connections. */
OVS_CT_DST_NAT = 1 << 2, /* Destination NAT for NEW connections. */
};
/* Conntrack action context for execution. */
struct ovs_conntrack_info {
struct nf_conntrack_helper *helper;
struct nf_conntrack_zone zone;
struct nf_conn *ct;
u8 commit : 1;
u8 nat : 3; /* enum ovs_ct_nat */
u8 force : 1;
u8 have_eventmask : 1;
u16 family;
u32 eventmask; /* Mask of 1 << IPCT_*. */
struct md_mark mark;
struct md_labels labels;
#ifdef CONFIG_NF_NAT_NEEDED
struct nf_nat_range2 range; /* Only present for SRC NAT and DST NAT. */
#endif
};
#if IS_ENABLED(CONFIG_NETFILTER_CONNCOUNT)
#define OVS_CT_LIMIT_UNLIMITED 0
#define OVS_CT_LIMIT_DEFAULT OVS_CT_LIMIT_UNLIMITED
#define CT_LIMIT_HASH_BUCKETS 512
static DEFINE_STATIC_KEY_FALSE(ovs_ct_limit_enabled);
struct ovs_ct_limit {
/* Elements in ovs_ct_limit_info->limits hash table */
struct hlist_node hlist_node;
struct rcu_head rcu;
u16 zone;
u32 limit;
};
struct ovs_ct_limit_info {
u32 default_limit;
struct hlist_head *limits;
struct nf_conncount_data *data;
};
static const struct nla_policy ct_limit_policy[OVS_CT_LIMIT_ATTR_MAX + 1] = {
[OVS_CT_LIMIT_ATTR_ZONE_LIMIT] = { .type = NLA_NESTED, },
};
#endif
static bool labels_nonzero(const struct ovs_key_ct_labels *labels);
static void __ovs_ct_free_action(struct ovs_conntrack_info *ct_info);
static u16 key_to_nfproto(const struct sw_flow_key *key)
{
switch (ntohs(key->eth.type)) {
case ETH_P_IP:
return NFPROTO_IPV4;
case ETH_P_IPV6:
return NFPROTO_IPV6;
default:
return NFPROTO_UNSPEC;
}
}
/* Map SKB connection state into the values used by flow definition. */
static u8 ovs_ct_get_state(enum ip_conntrack_info ctinfo)
{
u8 ct_state = OVS_CS_F_TRACKED;
switch (ctinfo) {
case IP_CT_ESTABLISHED_REPLY:
case IP_CT_RELATED_REPLY:
ct_state |= OVS_CS_F_REPLY_DIR;
break;
default:
break;
}
switch (ctinfo) {
case IP_CT_ESTABLISHED:
case IP_CT_ESTABLISHED_REPLY:
ct_state |= OVS_CS_F_ESTABLISHED;
break;
case IP_CT_RELATED:
case IP_CT_RELATED_REPLY:
ct_state |= OVS_CS_F_RELATED;
break;
case IP_CT_NEW:
ct_state |= OVS_CS_F_NEW;
break;
default:
break;
}
return ct_state;
}
static u32 ovs_ct_get_mark(const struct nf_conn *ct)
{
#if IS_ENABLED(CONFIG_NF_CONNTRACK_MARK)
return ct ? ct->mark : 0;
#else
return 0;
#endif
}
/* Guard against conntrack labels max size shrinking below 128 bits. */
#if NF_CT_LABELS_MAX_SIZE < 16
#error NF_CT_LABELS_MAX_SIZE must be at least 16 bytes
#endif
static void ovs_ct_get_labels(const struct nf_conn *ct,
struct ovs_key_ct_labels *labels)
{
struct nf_conn_labels *cl = ct ? nf_ct_labels_find(ct) : NULL;
if (cl)
memcpy(labels, cl->bits, OVS_CT_LABELS_LEN);
else
memset(labels, 0, OVS_CT_LABELS_LEN);
}
static void __ovs_ct_update_key_orig_tp(struct sw_flow_key *key,
const struct nf_conntrack_tuple *orig,
u8 icmp_proto)
{
key->ct_orig_proto = orig->dst.protonum;
if (orig->dst.protonum == icmp_proto) {
key->ct.orig_tp.src = htons(orig->dst.u.icmp.type);
key->ct.orig_tp.dst = htons(orig->dst.u.icmp.code);
} else {
key->ct.orig_tp.src = orig->src.u.all;
key->ct.orig_tp.dst = orig->dst.u.all;
}
}
static void __ovs_ct_update_key(struct sw_flow_key *key, u8 state,
const struct nf_conntrack_zone *zone,
const struct nf_conn *ct)
{
key->ct_state = state;
key->ct_zone = zone->id;
key->ct.mark = ovs_ct_get_mark(ct);
ovs_ct_get_labels(ct, &key->ct.labels);
if (ct) {
const struct nf_conntrack_tuple *orig;
/* Use the master if we have one. */
if (ct->master)
ct = ct->master;
orig = &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple;
/* IP version must match with the master connection. */
if (key->eth.type == htons(ETH_P_IP) &&
nf_ct_l3num(ct) == NFPROTO_IPV4) {
key->ipv4.ct_orig.src = orig->src.u3.ip;
key->ipv4.ct_orig.dst = orig->dst.u3.ip;
__ovs_ct_update_key_orig_tp(key, orig, IPPROTO_ICMP);
return;
} else if (key->eth.type == htons(ETH_P_IPV6) &&
!sw_flow_key_is_nd(key) &&
nf_ct_l3num(ct) == NFPROTO_IPV6) {
key->ipv6.ct_orig.src = orig->src.u3.in6;
key->ipv6.ct_orig.dst = orig->dst.u3.in6;
__ovs_ct_update_key_orig_tp(key, orig, NEXTHDR_ICMP);
return;
}
}
/* Clear 'ct_orig_proto' to mark the non-existence of conntrack
* original direction key fields.
*/
key->ct_orig_proto = 0;
}
/* Update 'key' based on skb->_nfct. If 'post_ct' is true, then OVS has
* previously sent the packet to conntrack via the ct action. If
* 'keep_nat_flags' is true, the existing NAT flags retained, else they are
* initialized from the connection status.
*/
static void ovs_ct_update_key(const struct sk_buff *skb,
const struct ovs_conntrack_info *info,
struct sw_flow_key *key, bool post_ct,
bool keep_nat_flags)
{
const struct nf_conntrack_zone *zone = &nf_ct_zone_dflt;
enum ip_conntrack_info ctinfo;
struct nf_conn *ct;
u8 state = 0;
ct = nf_ct_get(skb, &ctinfo);
if (ct) {
state = ovs_ct_get_state(ctinfo);
/* All unconfirmed entries are NEW connections. */
if (!nf_ct_is_confirmed(ct))
state |= OVS_CS_F_NEW;
/* OVS persists the related flag for the duration of the
* connection.
*/
if (ct->master)
state |= OVS_CS_F_RELATED;
if (keep_nat_flags) {
state |= key->ct_state & OVS_CS_F_NAT_MASK;
} else {
if (ct->status & IPS_SRC_NAT)
state |= OVS_CS_F_SRC_NAT;
if (ct->status & IPS_DST_NAT)
state |= OVS_CS_F_DST_NAT;
}
zone = nf_ct_zone(ct);
} else if (post_ct) {
state = OVS_CS_F_TRACKED | OVS_CS_F_INVALID;
if (info)
zone = &info->zone;
}
__ovs_ct_update_key(key, state, zone, ct);
}
/* This is called to initialize CT key fields possibly coming in from the local
* stack.
*/
void ovs_ct_fill_key(const struct sk_buff *skb, struct sw_flow_key *key)
{
ovs_ct_update_key(skb, NULL, key, false, false);
}
int ovs_ct_put_key(const struct sw_flow_key *swkey,
const struct sw_flow_key *output, struct sk_buff *skb)
{
if (nla_put_u32(skb, OVS_KEY_ATTR_CT_STATE, output->ct_state))
return -EMSGSIZE;
if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES) &&
nla_put_u16(skb, OVS_KEY_ATTR_CT_ZONE, output->ct_zone))
return -EMSGSIZE;
if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) &&
nla_put_u32(skb, OVS_KEY_ATTR_CT_MARK, output->ct.mark))
return -EMSGSIZE;
if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS) &&
nla_put(skb, OVS_KEY_ATTR_CT_LABELS, sizeof(output->ct.labels),
&output->ct.labels))
return -EMSGSIZE;
if (swkey->ct_orig_proto) {
if (swkey->eth.type == htons(ETH_P_IP)) {
struct ovs_key_ct_tuple_ipv4 orig;
memset(&orig, 0, sizeof(orig));
orig.ipv4_src = output->ipv4.ct_orig.src;
orig.ipv4_dst = output->ipv4.ct_orig.dst;
orig.src_port = output->ct.orig_tp.src;
orig.dst_port = output->ct.orig_tp.dst;
orig.ipv4_proto = output->ct_orig_proto;
if (nla_put(skb, OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV4,
sizeof(orig), &orig))
return -EMSGSIZE;
} else if (swkey->eth.type == htons(ETH_P_IPV6)) {
struct ovs_key_ct_tuple_ipv6 orig;
memset(&orig, 0, sizeof(orig));
memcpy(orig.ipv6_src, output->ipv6.ct_orig.src.s6_addr32,
sizeof(orig.ipv6_src));
memcpy(orig.ipv6_dst, output->ipv6.ct_orig.dst.s6_addr32,
sizeof(orig.ipv6_dst));
orig.src_port = output->ct.orig_tp.src;
orig.dst_port = output->ct.orig_tp.dst;
orig.ipv6_proto = output->ct_orig_proto;
if (nla_put(skb, OVS_KEY_ATTR_CT_ORIG_TUPLE_IPV6,
sizeof(orig), &orig))
return -EMSGSIZE;
}
}
return 0;
}
static int ovs_ct_set_mark(struct nf_conn *ct, struct sw_flow_key *key,
u32 ct_mark, u32 mask)
{
#if IS_ENABLED(CONFIG_NF_CONNTRACK_MARK)
u32 new_mark;
new_mark = ct_mark | (ct->mark & ~(mask));
if (ct->mark != new_mark) {
ct->mark = new_mark;
if (nf_ct_is_confirmed(ct))
nf_conntrack_event_cache(IPCT_MARK, ct);
key->ct.mark = new_mark;
}
return 0;
#else
return -ENOTSUPP;
#endif
}
static struct nf_conn_labels *ovs_ct_get_conn_labels(struct nf_conn *ct)
{
struct nf_conn_labels *cl;
cl = nf_ct_labels_find(ct);
if (!cl) {
nf_ct_labels_ext_add(ct);
cl = nf_ct_labels_find(ct);
}
return cl;
}
/* Initialize labels for a new, yet to be committed conntrack entry. Note that
* since the new connection is not yet confirmed, and thus no-one else has
* access to it's labels, we simply write them over.
*/
static int ovs_ct_init_labels(struct nf_conn *ct, struct sw_flow_key *key,
const struct ovs_key_ct_labels *labels,
const struct ovs_key_ct_labels *mask)
{
struct nf_conn_labels *cl, *master_cl;
bool have_mask = labels_nonzero(mask);
/* Inherit master's labels to the related connection? */
master_cl = ct->master ? nf_ct_labels_find(ct->master) : NULL;
if (!master_cl && !have_mask)
return 0; /* Nothing to do. */
cl = ovs_ct_get_conn_labels(ct);
if (!cl)
return -ENOSPC;
/* Inherit the master's labels, if any. */
if (master_cl)
*cl = *master_cl;
if (have_mask) {
u32 *dst = (u32 *)cl->bits;
int i;
for (i = 0; i < OVS_CT_LABELS_LEN_32; i++)
dst[i] = (dst[i] & ~mask->ct_labels_32[i]) |
(labels->ct_labels_32[i]
& mask->ct_labels_32[i]);
}
/* Labels are included in the IPCTNL_MSG_CT_NEW event only if the
* IPCT_LABEL bit is set in the event cache.
*/
nf_conntrack_event_cache(IPCT_LABEL, ct);
memcpy(&key->ct.labels, cl->bits, OVS_CT_LABELS_LEN);
return 0;
}
static int ovs_ct_set_labels(struct nf_conn *ct, struct sw_flow_key *key,
const struct ovs_key_ct_labels *labels,
const struct ovs_key_ct_labels *mask)
{
struct nf_conn_labels *cl;
int err;
cl = ovs_ct_get_conn_labels(ct);
if (!cl)
return -ENOSPC;
err = nf_connlabels_replace(ct, labels->ct_labels_32,
mask->ct_labels_32,
OVS_CT_LABELS_LEN_32);
if (err)
return err;
memcpy(&key->ct.labels, cl->bits, OVS_CT_LABELS_LEN);
return 0;
}
/* 'skb' should already be pulled to nh_ofs. */
static int ovs_ct_helper(struct sk_buff *skb, u16 proto)
{
const struct nf_conntrack_helper *helper;
const struct nf_conn_help *help;
enum ip_conntrack_info ctinfo;
unsigned int protoff;
struct nf_conn *ct;
int err;
ct = nf_ct_get(skb, &ctinfo);
if (!ct || ctinfo == IP_CT_RELATED_REPLY)
return NF_ACCEPT;
help = nfct_help(ct);
if (!help)
return NF_ACCEPT;
helper = rcu_dereference(help->helper);
if (!helper)
return NF_ACCEPT;
switch (proto) {
case NFPROTO_IPV4:
protoff = ip_hdrlen(skb);
break;
case NFPROTO_IPV6: {
u8 nexthdr = ipv6_hdr(skb)->nexthdr;
__be16 frag_off;
int ofs;
ofs = ipv6_skip_exthdr(skb, sizeof(struct ipv6hdr), &nexthdr,
&frag_off);
if (ofs < 0 || (frag_off & htons(~0x7)) != 0) {
pr_debug("proto header not found\n");
return NF_ACCEPT;
}
protoff = ofs;
break;
}
default:
WARN_ONCE(1, "helper invoked on non-IP family!");
return NF_DROP;
}
err = helper->help(skb, protoff, ct, ctinfo);
if (err != NF_ACCEPT)
return err;
/* Adjust seqs after helper. This is needed due to some helpers (e.g.,
* FTP with NAT) adusting the TCP payload size when mangling IP
* addresses and/or port numbers in the text-based control connection.
*/
if (test_bit(IPS_SEQ_ADJUST_BIT, &ct->status) &&
!nf_ct_seq_adjust(skb, ct, ctinfo, protoff))
return NF_DROP;
return NF_ACCEPT;
}
/* Returns 0 on success, -EINPROGRESS if 'skb' is stolen, or other nonzero
* value if 'skb' is freed.
*/
static int handle_fragments(struct net *net, struct sw_flow_key *key,
u16 zone, struct sk_buff *skb)
{
struct ovs_skb_cb ovs_cb = *OVS_CB(skb);
int err;
if (key->eth.type == htons(ETH_P_IP)) {
enum ip_defrag_users user = IP_DEFRAG_CONNTRACK_IN + zone;
memset(IPCB(skb), 0, sizeof(struct inet_skb_parm));
err = ip_defrag(net, skb, user);
if (err)
return err;
ovs_cb.mru = IPCB(skb)->frag_max_size;
#if IS_ENABLED(CONFIG_NF_DEFRAG_IPV6)
} else if (key->eth.type == htons(ETH_P_IPV6)) {
enum ip6_defrag_users user = IP6_DEFRAG_CONNTRACK_IN + zone;
memset(IP6CB(skb), 0, sizeof(struct inet6_skb_parm));
err = nf_ct_frag6_gather(net, skb, user);
if (err) {
if (err != -EINPROGRESS)
kfree_skb(skb);
return err;
}
key->ip.proto = ipv6_hdr(skb)->nexthdr;
ovs_cb.mru = IP6CB(skb)->frag_max_size;
#endif
} else {
kfree_skb(skb);
return -EPFNOSUPPORT;
}
key->ip.frag = OVS_FRAG_TYPE_NONE;
skb_clear_hash(skb);
skb->ignore_df = 1;
*OVS_CB(skb) = ovs_cb;
return 0;
}
static struct nf_conntrack_expect *
ovs_ct_expect_find(struct net *net, const struct nf_conntrack_zone *zone,
u16 proto, const struct sk_buff *skb)
{
struct nf_conntrack_tuple tuple;
struct nf_conntrack_expect *exp;
if (!nf_ct_get_tuplepr(skb, skb_network_offset(skb), proto, net, &tuple))
return NULL;
exp = __nf_ct_expect_find(net, zone, &tuple);
if (exp) {
struct nf_conntrack_tuple_hash *h;
/* Delete existing conntrack entry, if it clashes with the
* expectation. This can happen since conntrack ALGs do not
* check for clashes between (new) expectations and existing
* conntrack entries. nf_conntrack_in() will check the
* expectations only if a conntrack entry can not be found,
* which can lead to OVS finding the expectation (here) in the
* init direction, but which will not be removed by the
* nf_conntrack_in() call, if a matching conntrack entry is
* found instead. In this case all init direction packets
* would be reported as new related packets, while reply
* direction packets would be reported as un-related
* established packets.
*/
h = nf_conntrack_find_get(net, zone, &tuple);
if (h) {
struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(h);
nf_ct_delete(ct, 0, 0);
nf_conntrack_put(&ct->ct_general);
}
}
return exp;
}
/* This replicates logic from nf_conntrack_core.c that is not exported. */
static enum ip_conntrack_info
ovs_ct_get_info(const struct nf_conntrack_tuple_hash *h)
{
const struct nf_conn *ct = nf_ct_tuplehash_to_ctrack(h);
if (NF_CT_DIRECTION(h) == IP_CT_DIR_REPLY)
return IP_CT_ESTABLISHED_REPLY;
/* Once we've had two way comms, always ESTABLISHED. */
if (test_bit(IPS_SEEN_REPLY_BIT, &ct->status))
return IP_CT_ESTABLISHED;
if (test_bit(IPS_EXPECTED_BIT, &ct->status))
return IP_CT_RELATED;
return IP_CT_NEW;
}
/* Find an existing connection which this packet belongs to without
* re-attributing statistics or modifying the connection state. This allows an
* skb->_nfct lost due to an upcall to be recovered during actions execution.
*
* Must be called with rcu_read_lock.
*
* On success, populates skb->_nfct and returns the connection. Returns NULL
* if there is no existing entry.
*/
static struct nf_conn *
ovs_ct_find_existing(struct net *net, const struct nf_conntrack_zone *zone,
u8 l3num, struct sk_buff *skb, bool natted)
{
struct nf_conntrack_tuple tuple;
struct nf_conntrack_tuple_hash *h;
struct nf_conn *ct;
if (!nf_ct_get_tuplepr(skb, skb_network_offset(skb), l3num,
net, &tuple)) {
pr_debug("ovs_ct_find_existing: Can't get tuple\n");
return NULL;
}
/* Must invert the tuple if skb has been transformed by NAT. */
if (natted) {
struct nf_conntrack_tuple inverse;
if (!nf_ct_invert_tuplepr(&inverse, &tuple)) {
pr_debug("ovs_ct_find_existing: Inversion failed!\n");
return NULL;
}
tuple = inverse;
}
/* look for tuple match */
h = nf_conntrack_find_get(net, zone, &tuple);
if (!h)
return NULL; /* Not found. */
ct = nf_ct_tuplehash_to_ctrack(h);
/* Inverted packet tuple matches the reverse direction conntrack tuple,
* select the other tuplehash to get the right 'ctinfo' bits for this
* packet.
*/
if (natted)
h = &ct->tuplehash[!h->tuple.dst.dir];
nf_ct_set(skb, ct, ovs_ct_get_info(h));
return ct;
}
static
struct nf_conn *ovs_ct_executed(struct net *net,
const struct sw_flow_key *key,
const struct ovs_conntrack_info *info,
struct sk_buff *skb,
bool *ct_executed)
{
struct nf_conn *ct = NULL;
/* If no ct, check if we have evidence that an existing conntrack entry
* might be found for this skb. This happens when we lose a skb->_nfct
* due to an upcall, or if the direction is being forced. If the
* connection was not confirmed, it is not cached and needs to be run
* through conntrack again.
*/
*ct_executed = (key->ct_state & OVS_CS_F_TRACKED) &&
!(key->ct_state & OVS_CS_F_INVALID) &&
(key->ct_zone == info->zone.id);
if (*ct_executed || (!key->ct_state && info->force)) {
ct = ovs_ct_find_existing(net, &info->zone, info->family, skb,
!!(key->ct_state &
OVS_CS_F_NAT_MASK));
}
return ct;
}
/* Determine whether skb->_nfct is equal to the result of conntrack lookup. */
static bool skb_nfct_cached(struct net *net,
const struct sw_flow_key *key,
const struct ovs_conntrack_info *info,
struct sk_buff *skb)
{
enum ip_conntrack_info ctinfo;
struct nf_conn *ct;
bool ct_executed = true;
ct = nf_ct_get(skb, &ctinfo);
if (!ct)
ct = ovs_ct_executed(net, key, info, skb, &ct_executed);
if (ct)
nf_ct_get(skb, &ctinfo);
else
return false;
if (!net_eq(net, read_pnet(&ct->ct_net)))
return false;
if (!nf_ct_zone_equal_any(info->ct, nf_ct_zone(ct)))
return false;
if (info->helper) {
struct nf_conn_help *help;
help = nf_ct_ext_find(ct, NF_CT_EXT_HELPER);
if (help && rcu_access_pointer(help->helper) != info->helper)
return false;
}
/* Force conntrack entry direction to the current packet? */
if (info->force && CTINFO2DIR(ctinfo) != IP_CT_DIR_ORIGINAL) {
/* Delete the conntrack entry if confirmed, else just release
* the reference.
*/
if (nf_ct_is_confirmed(ct))
nf_ct_delete(ct, 0, 0);
nf_conntrack_put(&ct->ct_general);
nf_ct_set(skb, NULL, 0);
return false;
}
return ct_executed;
}
#ifdef CONFIG_NF_NAT_NEEDED
/* Modelled after nf_nat_ipv[46]_fn().
* range is only used for new, uninitialized NAT state.
* Returns either NF_ACCEPT or NF_DROP.
*/
static int ovs_ct_nat_execute(struct sk_buff *skb, struct nf_conn *ct,
enum ip_conntrack_info ctinfo,
const struct nf_nat_range2 *range,
enum nf_nat_manip_type maniptype)
{
int hooknum, nh_off, err = NF_ACCEPT;
nh_off = skb_network_offset(skb);
skb_pull_rcsum(skb, nh_off);
/* See HOOK2MANIP(). */
if (maniptype == NF_NAT_MANIP_SRC)
hooknum = NF_INET_LOCAL_IN; /* Source NAT */
else
hooknum = NF_INET_LOCAL_OUT; /* Destination NAT */
switch (ctinfo) {
case IP_CT_RELATED:
case IP_CT_RELATED_REPLY:
if (IS_ENABLED(CONFIG_NF_NAT_IPV4) &&
skb->protocol == htons(ETH_P_IP) &&
ip_hdr(skb)->protocol == IPPROTO_ICMP) {
if (!nf_nat_icmp_reply_translation(skb, ct, ctinfo,
hooknum))
err = NF_DROP;
goto push;
} else if (IS_ENABLED(CONFIG_NF_NAT_IPV6) &&
skb->protocol == htons(ETH_P_IPV6)) {
__be16 frag_off;
u8 nexthdr = ipv6_hdr(skb)->nexthdr;
int hdrlen = ipv6_skip_exthdr(skb,
sizeof(struct ipv6hdr),
&nexthdr, &frag_off);
if (hdrlen >= 0 && nexthdr == IPPROTO_ICMPV6) {
if (!nf_nat_icmpv6_reply_translation(skb, ct,
ctinfo,
hooknum,
hdrlen))
err = NF_DROP;
goto push;
}
}
/* Non-ICMP, fall thru to initialize if needed. */
/* fall through */
case IP_CT_NEW:
/* Seen it before? This can happen for loopback, retrans,
* or local packets.
*/
if (!nf_nat_initialized(ct, maniptype)) {
/* Initialize according to the NAT action. */
err = (range && range->flags & NF_NAT_RANGE_MAP_IPS)
/* Action is set up to establish a new
* mapping.
*/
? nf_nat_setup_info(ct, range, maniptype)
: nf_nat_alloc_null_binding(ct, hooknum);
if (err != NF_ACCEPT)
goto push;
}
break;
case IP_CT_ESTABLISHED:
case IP_CT_ESTABLISHED_REPLY:
break;
default:
err = NF_DROP;
goto push;
}
err = nf_nat_packet(ct, ctinfo, hooknum, skb);
push:
skb_push(skb, nh_off);
skb_postpush_rcsum(skb, skb->data, nh_off);
return err;
}
static void ovs_nat_update_key(struct sw_flow_key *key,
const struct sk_buff *skb,
enum nf_nat_manip_type maniptype)
{
if (maniptype == NF_NAT_MANIP_SRC) {
__be16 src;
key->ct_state |= OVS_CS_F_SRC_NAT;
if (key->eth.type == htons(ETH_P_IP))
key->ipv4.addr.src = ip_hdr(skb)->saddr;
else if (key->eth.type == htons(ETH_P_IPV6))
memcpy(&key->ipv6.addr.src, &ipv6_hdr(skb)->saddr,
sizeof(key->ipv6.addr.src));
else
return;
if (key->ip.proto == IPPROTO_UDP)
src = udp_hdr(skb)->source;
else if (key->ip.proto == IPPROTO_TCP)
src = tcp_hdr(skb)->source;
else if (key->ip.proto == IPPROTO_SCTP)
src = sctp_hdr(skb)->source;
else
return;
key->tp.src = src;
} else {
__be16 dst;
key->ct_state |= OVS_CS_F_DST_NAT;
if (key->eth.type == htons(ETH_P_IP))
key->ipv4.addr.dst = ip_hdr(skb)->daddr;
else if (key->eth.type == htons(ETH_P_IPV6))
memcpy(&key->ipv6.addr.dst, &ipv6_hdr(skb)->daddr,
sizeof(key->ipv6.addr.dst));
else
return;
if (key->ip.proto == IPPROTO_UDP)
dst = udp_hdr(skb)->dest;
else if (key->ip.proto == IPPROTO_TCP)
dst = tcp_hdr(skb)->dest;
else if (key->ip.proto == IPPROTO_SCTP)
dst = sctp_hdr(skb)->dest;
else
return;
key->tp.dst = dst;
}
}
/* Returns NF_DROP if the packet should be dropped, NF_ACCEPT otherwise. */
static int ovs_ct_nat(struct net *net, struct sw_flow_key *key,
const struct ovs_conntrack_info *info,
struct sk_buff *skb, struct nf_conn *ct,
enum ip_conntrack_info ctinfo)
{
enum nf_nat_manip_type maniptype;
int err;
/* Add NAT extension if not confirmed yet. */
if (!nf_ct_is_confirmed(ct) && !nf_ct_nat_ext_add(ct))
return NF_ACCEPT; /* Can't NAT. */
/* Determine NAT type.
* Check if the NAT type can be deduced from the tracked connection.
* Make sure new expected connections (IP_CT_RELATED) are NATted only
* when committing.
*/
if (info->nat & OVS_CT_NAT && ctinfo != IP_CT_NEW &&
ct->status & IPS_NAT_MASK &&
(ctinfo != IP_CT_RELATED || info->commit)) {
/* NAT an established or related connection like before. */
if (CTINFO2DIR(ctinfo) == IP_CT_DIR_REPLY)
/* This is the REPLY direction for a connection
* for which NAT was applied in the forward
* direction. Do the reverse NAT.
*/
maniptype = ct->status & IPS_SRC_NAT
? NF_NAT_MANIP_DST : NF_NAT_MANIP_SRC;
else
maniptype = ct->status & IPS_SRC_NAT
? NF_NAT_MANIP_SRC : NF_NAT_MANIP_DST;
} else if (info->nat & OVS_CT_SRC_NAT) {
maniptype = NF_NAT_MANIP_SRC;
} else if (info->nat & OVS_CT_DST_NAT) {
maniptype = NF_NAT_MANIP_DST;
} else {
return NF_ACCEPT; /* Connection is not NATed. */
}
err = ovs_ct_nat_execute(skb, ct, ctinfo, &info->range, maniptype);
if (err == NF_ACCEPT && ct->status & IPS_DST_NAT) {
if (ct->status & IPS_SRC_NAT) {
if (maniptype == NF_NAT_MANIP_SRC)
maniptype = NF_NAT_MANIP_DST;
else
maniptype = NF_NAT_MANIP_SRC;
err = ovs_ct_nat_execute(skb, ct, ctinfo, &info->range,
maniptype);
} else if (CTINFO2DIR(ctinfo) == IP_CT_DIR_ORIGINAL) {
err = ovs_ct_nat_execute(skb, ct, ctinfo, NULL,
NF_NAT_MANIP_SRC);
}
}
/* Mark NAT done if successful and update the flow key. */
if (err == NF_ACCEPT)
ovs_nat_update_key(key, skb, maniptype);
return err;
}
#else /* !CONFIG_NF_NAT_NEEDED */
static int ovs_ct_nat(struct net *net, struct sw_flow_key *key,
const struct ovs_conntrack_info *info,
struct sk_buff *skb, struct nf_conn *ct,
enum ip_conntrack_info ctinfo)
{
return NF_ACCEPT;
}
#endif
/* Pass 'skb' through conntrack in 'net', using zone configured in 'info', if
* not done already. Update key with new CT state after passing the packet
* through conntrack.
* Note that if the packet is deemed invalid by conntrack, skb->_nfct will be
* set to NULL and 0 will be returned.
*/
static int __ovs_ct_lookup(struct net *net, struct sw_flow_key *key,
const struct ovs_conntrack_info *info,
struct sk_buff *skb)
{
/* If we are recirculating packets to match on conntrack fields and
* committing with a separate conntrack action, then we don't need to
* actually run the packet through conntrack twice unless it's for a
* different zone.
*/
bool cached = skb_nfct_cached(net, key, info, skb);
enum ip_conntrack_info ctinfo;
struct nf_conn *ct;
if (!cached) {
struct nf_conn *tmpl = info->ct;
int err;
/* Associate skb with specified zone. */
if (tmpl) {
if (skb_nfct(skb))
nf_conntrack_put(skb_nfct(skb));
nf_conntrack_get(&tmpl->ct_general);
nf_ct_set(skb, tmpl, IP_CT_NEW);
}
err = nf_conntrack_in(net, info->family,
NF_INET_PRE_ROUTING, skb);
if (err != NF_ACCEPT)
return -ENOENT;
/* Clear CT state NAT flags to mark that we have not yet done
* NAT after the nf_conntrack_in() call. We can actually clear
* the whole state, as it will be re-initialized below.
*/
key->ct_state = 0;
/* Update the key, but keep the NAT flags. */
ovs_ct_update_key(skb, info, key, true, true);
}
ct = nf_ct_get(skb, &ctinfo);
if (ct) {
/* Packets starting a new connection must be NATted before the
* helper, so that the helper knows about the NAT. We enforce
* this by delaying both NAT and helper calls for unconfirmed
* connections until the committing CT action. For later
* packets NAT and Helper may be called in either order.
*
* NAT will be done only if the CT action has NAT, and only
* once per packet (per zone), as guarded by the NAT bits in
* the key->ct_state.
*/
if (info->nat && !(key->ct_state & OVS_CS_F_NAT_MASK) &&
(nf_ct_is_confirmed(ct) || info->commit) &&
ovs_ct_nat(net, key, info, skb, ct, ctinfo) != NF_ACCEPT) {
return -EINVAL;
}
/* Userspace may decide to perform a ct lookup without a helper
* specified followed by a (recirculate and) commit with one.
* Therefore, for unconfirmed connections which we will commit,
* we need to attach the helper here.
*/
if (!nf_ct_is_confirmed(ct) && info->commit &&
info->helper && !nfct_help(ct)) {
int err = __nf_ct_try_assign_helper(ct, info->ct,
GFP_ATOMIC);
if (err)
return err;
}
/* Call the helper only if:
* - nf_conntrack_in() was executed above ("!cached") for a
* confirmed connection, or
* - When committing an unconfirmed connection.
*/
if ((nf_ct_is_confirmed(ct) ? !cached : info->commit) &&
ovs_ct_helper(skb, info->family) != NF_ACCEPT) {
return -EINVAL;
}
}
return 0;
}
/* Lookup connection and read fields into key. */
static int ovs_ct_lookup(struct net *net, struct sw_flow_key *key,
const struct ovs_conntrack_info *info,
struct sk_buff *skb)
{
struct nf_conntrack_expect *exp;
/* If we pass an expected packet through nf_conntrack_in() the
* expectation is typically removed, but the packet could still be
* lost in upcall processing. To prevent this from happening we
* perform an explicit expectation lookup. Expected connections are
* always new, and will be passed through conntrack only when they are
* committed, as it is OK to remove the expectation at that time.
*/
exp = ovs_ct_expect_find(net, &info->zone, info->family, skb);
if (exp) {
u8 state;
/* NOTE: New connections are NATted and Helped only when
* committed, so we are not calling into NAT here.
*/
state = OVS_CS_F_TRACKED | OVS_CS_F_NEW | OVS_CS_F_RELATED;
__ovs_ct_update_key(key, state, &info->zone, exp->master);
} else {
struct nf_conn *ct;
int err;
err = __ovs_ct_lookup(net, key, info, skb);
if (err)
return err;
ct = (struct nf_conn *)skb_nfct(skb);
if (ct)
nf_ct_deliver_cached_events(ct);
}
return 0;
}
static bool labels_nonzero(const struct ovs_key_ct_labels *labels)
{
size_t i;
for (i = 0; i < OVS_CT_LABELS_LEN_32; i++)
if (labels->ct_labels_32[i])
return true;
return false;
}
#if IS_ENABLED(CONFIG_NETFILTER_CONNCOUNT)
static struct hlist_head *ct_limit_hash_bucket(
const struct ovs_ct_limit_info *info, u16 zone)
{
return &info->limits[zone & (CT_LIMIT_HASH_BUCKETS - 1)];
}
/* Call with ovs_mutex */
static void ct_limit_set(const struct ovs_ct_limit_info *info,
struct ovs_ct_limit *new_ct_limit)
{
struct ovs_ct_limit *ct_limit;
struct hlist_head *head;
head = ct_limit_hash_bucket(info, new_ct_limit->zone);
hlist_for_each_entry_rcu(ct_limit, head, hlist_node) {
if (ct_limit->zone == new_ct_limit->zone) {
hlist_replace_rcu(&ct_limit->hlist_node,
&new_ct_limit->hlist_node);
kfree_rcu(ct_limit, rcu);
return;
}
}
hlist_add_head_rcu(&new_ct_limit->hlist_node, head);
}
/* Call with ovs_mutex */
static void ct_limit_del(const struct ovs_ct_limit_info *info, u16 zone)
{
struct ovs_ct_limit *ct_limit;
struct hlist_head *head;
struct hlist_node *n;
head = ct_limit_hash_bucket(info, zone);
hlist_for_each_entry_safe(ct_limit, n, head, hlist_node) {
if (ct_limit->zone == zone) {
hlist_del_rcu(&ct_limit->hlist_node);
kfree_rcu(ct_limit, rcu);
return;
}
}
}
/* Call with RCU read lock */
static u32 ct_limit_get(const struct ovs_ct_limit_info *info, u16 zone)
{
struct ovs_ct_limit *ct_limit;
struct hlist_head *head;
head = ct_limit_hash_bucket(info, zone);
hlist_for_each_entry_rcu(ct_limit, head, hlist_node) {
if (ct_limit->zone == zone)
return ct_limit->limit;
}
return info->default_limit;
}
static int ovs_ct_check_limit(struct net *net,
const struct sk_buff *skb,
const struct ovs_conntrack_info *info)
{
struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
const struct ovs_ct_limit_info *ct_limit_info = ovs_net->ct_limit_info;
u32 per_zone_limit, connections;
u32 conncount_key;
conncount_key = info->zone.id;
per_zone_limit = ct_limit_get(ct_limit_info, info->zone.id);
if (per_zone_limit == OVS_CT_LIMIT_UNLIMITED)
return 0;
connections = nf_conncount_count_skb(net, skb, info->family,
ct_limit_info->data,
&conncount_key);
if (connections > per_zone_limit)
return -ENOMEM;
return 0;
}
#endif
/* Lookup connection and confirm if unconfirmed. */
static int ovs_ct_commit(struct net *net, struct sw_flow_key *key,
const struct ovs_conntrack_info *info,
struct sk_buff *skb)
{
enum ip_conntrack_info ctinfo;
struct nf_conn *ct;
int err;
err = __ovs_ct_lookup(net, key, info, skb);
if (err)
return err;
/* The connection could be invalid, in which case this is a no-op.*/
ct = nf_ct_get(skb, &ctinfo);
if (!ct)
return 0;
#if IS_ENABLED(CONFIG_NETFILTER_CONNCOUNT)
if (static_branch_unlikely(&ovs_ct_limit_enabled)) {
if (!nf_ct_is_confirmed(ct)) {
err = ovs_ct_check_limit(net, skb, info);
if (err) {
net_warn_ratelimited("openvswitch: zone: %u "
"execeeds conntrack limit\n",
info->zone.id);
return err;
}
}
}
#endif
/* Set the conntrack event mask if given. NEW and DELETE events have
* their own groups, but the NFNLGRP_CONNTRACK_UPDATE group listener
* typically would receive many kinds of updates. Setting the event
* mask allows those events to be filtered. The set event mask will
* remain in effect for the lifetime of the connection unless changed
* by a further CT action with both the commit flag and the eventmask
* option. */
if (info->have_eventmask) {
struct nf_conntrack_ecache *cache = nf_ct_ecache_find(ct);
if (cache)
cache->ctmask = info->eventmask;
}
/* Apply changes before confirming the connection so that the initial
* conntrack NEW netlink event carries the values given in the CT
* action.
*/
if (info->mark.mask) {
err = ovs_ct_set_mark(ct, key, info->mark.value,
info->mark.mask);
if (err)
return err;
}
if (!nf_ct_is_confirmed(ct)) {
err = ovs_ct_init_labels(ct, key, &info->labels.value,
&info->labels.mask);
if (err)
return err;
} else if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS) &&
labels_nonzero(&info->labels.mask)) {
err = ovs_ct_set_labels(ct, key, &info->labels.value,
&info->labels.mask);
if (err)
return err;
}
/* This will take care of sending queued events even if the connection
* is already confirmed.
*/
if (nf_conntrack_confirm(skb) != NF_ACCEPT)
return -EINVAL;
return 0;
}
/* Trim the skb to the length specified by the IP/IPv6 header,
* removing any trailing lower-layer padding. This prepares the skb
* for higher-layer processing that assumes skb->len excludes padding
* (such as nf_ip_checksum). The caller needs to pull the skb to the
* network header, and ensure ip_hdr/ipv6_hdr points to valid data.
*/
static int ovs_skb_network_trim(struct sk_buff *skb)
{
unsigned int len;
int err;
switch (skb->protocol) {
case htons(ETH_P_IP):
len = ntohs(ip_hdr(skb)->tot_len);
break;
case htons(ETH_P_IPV6):
len = sizeof(struct ipv6hdr)
+ ntohs(ipv6_hdr(skb)->payload_len);
break;
default:
len = skb->len;
}
err = pskb_trim_rcsum(skb, len);
if (err)
kfree_skb(skb);
return err;
}
/* Returns 0 on success, -EINPROGRESS if 'skb' is stolen, or other nonzero
* value if 'skb' is freed.
*/
int ovs_ct_execute(struct net *net, struct sk_buff *skb,
struct sw_flow_key *key,
const struct ovs_conntrack_info *info)
{
int nh_ofs;
int err;
/* The conntrack module expects to be working at L3. */
nh_ofs = skb_network_offset(skb);
skb_pull_rcsum(skb, nh_ofs);
err = ovs_skb_network_trim(skb);
if (err)
return err;
if (key->ip.frag != OVS_FRAG_TYPE_NONE) {
err = handle_fragments(net, key, info->zone.id, skb);
if (err)
return err;
}
if (info->commit)
err = ovs_ct_commit(net, key, info, skb);
else
err = ovs_ct_lookup(net, key, info, skb);
skb_push(skb, nh_ofs);
skb_postpush_rcsum(skb, skb->data, nh_ofs);
if (err)
kfree_skb(skb);
return err;
}
int ovs_ct_clear(struct sk_buff *skb, struct sw_flow_key *key)
{
if (skb_nfct(skb)) {
nf_conntrack_put(skb_nfct(skb));
nf_ct_set(skb, NULL, IP_CT_UNTRACKED);
if (key)
ovs_ct_fill_key(skb, key);
}
return 0;
}
static int ovs_ct_add_helper(struct ovs_conntrack_info *info, const char *name,
const struct sw_flow_key *key, bool log)
{
struct nf_conntrack_helper *helper;
struct nf_conn_help *help;
helper = nf_conntrack_helper_try_module_get(name, info->family,
key->ip.proto);
if (!helper) {
OVS_NLERR(log, "Unknown helper \"%s\"", name);
return -EINVAL;
}
help = nf_ct_helper_ext_add(info->ct, GFP_KERNEL);
if (!help) {
nf_conntrack_helper_put(helper);
return -ENOMEM;
}
rcu_assign_pointer(help->helper, helper);
info->helper = helper;
if (info->nat)
request_module("ip_nat_%s", name);
return 0;
}
#ifdef CONFIG_NF_NAT_NEEDED
static int parse_nat(const struct nlattr *attr,
struct ovs_conntrack_info *info, bool log)
{
struct nlattr *a;
int rem;
bool have_ip_max = false;
bool have_proto_max = false;
bool ip_vers = (info->family == NFPROTO_IPV6);
nla_for_each_nested(a, attr, rem) {
static const int ovs_nat_attr_lens[OVS_NAT_ATTR_MAX + 1][2] = {
[OVS_NAT_ATTR_SRC] = {0, 0},
[OVS_NAT_ATTR_DST] = {0, 0},
[OVS_NAT_ATTR_IP_MIN] = {sizeof(struct in_addr),
sizeof(struct in6_addr)},
[OVS_NAT_ATTR_IP_MAX] = {sizeof(struct in_addr),
sizeof(struct in6_addr)},
[OVS_NAT_ATTR_PROTO_MIN] = {sizeof(u16), sizeof(u16)},
[OVS_NAT_ATTR_PROTO_MAX] = {sizeof(u16), sizeof(u16)},
[OVS_NAT_ATTR_PERSISTENT] = {0, 0},
[OVS_NAT_ATTR_PROTO_HASH] = {0, 0},
[OVS_NAT_ATTR_PROTO_RANDOM] = {0, 0},
};
int type = nla_type(a);
if (type > OVS_NAT_ATTR_MAX) {
OVS_NLERR(log, "Unknown NAT attribute (type=%d, max=%d)",
type, OVS_NAT_ATTR_MAX);
return -EINVAL;
}
if (nla_len(a) != ovs_nat_attr_lens[type][ip_vers]) {
OVS_NLERR(log, "NAT attribute type %d has unexpected length (%d != %d)",
type, nla_len(a),
ovs_nat_attr_lens[type][ip_vers]);
return -EINVAL;
}
switch (type) {
case OVS_NAT_ATTR_SRC:
case OVS_NAT_ATTR_DST:
if (info->nat) {
OVS_NLERR(log, "Only one type of NAT may be specified");
return -ERANGE;
}
info->nat |= OVS_CT_NAT;
info->nat |= ((type == OVS_NAT_ATTR_SRC)
? OVS_CT_SRC_NAT : OVS_CT_DST_NAT);
break;
case OVS_NAT_ATTR_IP_MIN:
nla_memcpy(&info->range.min_addr, a,
sizeof(info->range.min_addr));
info->range.flags |= NF_NAT_RANGE_MAP_IPS;
break;
case OVS_NAT_ATTR_IP_MAX:
have_ip_max = true;
nla_memcpy(&info->range.max_addr, a,
sizeof(info->range.max_addr));
info->range.flags |= NF_NAT_RANGE_MAP_IPS;
break;
case OVS_NAT_ATTR_PROTO_MIN:
info->range.min_proto.all = htons(nla_get_u16(a));
info->range.flags |= NF_NAT_RANGE_PROTO_SPECIFIED;
break;
case OVS_NAT_ATTR_PROTO_MAX:
have_proto_max = true;
info->range.max_proto.all = htons(nla_get_u16(a));
info->range.flags |= NF_NAT_RANGE_PROTO_SPECIFIED;
break;
case OVS_NAT_ATTR_PERSISTENT:
info->range.flags |= NF_NAT_RANGE_PERSISTENT;
break;
case OVS_NAT_ATTR_PROTO_HASH:
info->range.flags |= NF_NAT_RANGE_PROTO_RANDOM;
break;
case OVS_NAT_ATTR_PROTO_RANDOM:
info->range.flags |= NF_NAT_RANGE_PROTO_RANDOM_FULLY;
break;
default:
OVS_NLERR(log, "Unknown nat attribute (%d)", type);
return -EINVAL;
}
}
if (rem > 0) {
OVS_NLERR(log, "NAT attribute has %d unknown bytes", rem);
return -EINVAL;
}
if (!info->nat) {
/* Do not allow flags if no type is given. */
if (info->range.flags) {
OVS_NLERR(log,
"NAT flags may be given only when NAT range (SRC or DST) is also specified."
);
return -EINVAL;
}
info->nat = OVS_CT_NAT; /* NAT existing connections. */
} else if (!info->commit) {
OVS_NLERR(log,
"NAT attributes may be specified only when CT COMMIT flag is also specified."
);
return -EINVAL;
}
/* Allow missing IP_MAX. */
if (info->range.flags & NF_NAT_RANGE_MAP_IPS && !have_ip_max) {
memcpy(&info->range.max_addr, &info->range.min_addr,
sizeof(info->range.max_addr));
}
/* Allow missing PROTO_MAX. */
if (info->range.flags & NF_NAT_RANGE_PROTO_SPECIFIED &&
!have_proto_max) {
info->range.max_proto.all = info->range.min_proto.all;
}
return 0;
}
#endif
static const struct ovs_ct_len_tbl ovs_ct_attr_lens[OVS_CT_ATTR_MAX + 1] = {
[OVS_CT_ATTR_COMMIT] = { .minlen = 0, .maxlen = 0 },
[OVS_CT_ATTR_FORCE_COMMIT] = { .minlen = 0, .maxlen = 0 },
[OVS_CT_ATTR_ZONE] = { .minlen = sizeof(u16),
.maxlen = sizeof(u16) },
[OVS_CT_ATTR_MARK] = { .minlen = sizeof(struct md_mark),
.maxlen = sizeof(struct md_mark) },
[OVS_CT_ATTR_LABELS] = { .minlen = sizeof(struct md_labels),
.maxlen = sizeof(struct md_labels) },
[OVS_CT_ATTR_HELPER] = { .minlen = 1,
.maxlen = NF_CT_HELPER_NAME_LEN },
#ifdef CONFIG_NF_NAT_NEEDED
/* NAT length is checked when parsing the nested attributes. */
[OVS_CT_ATTR_NAT] = { .minlen = 0, .maxlen = INT_MAX },
#endif
[OVS_CT_ATTR_EVENTMASK] = { .minlen = sizeof(u32),
.maxlen = sizeof(u32) },
};
static int parse_ct(const struct nlattr *attr, struct ovs_conntrack_info *info,
const char **helper, bool log)
{
struct nlattr *a;
int rem;
nla_for_each_nested(a, attr, rem) {
int type = nla_type(a);
int maxlen;
int minlen;
if (type > OVS_CT_ATTR_MAX) {
OVS_NLERR(log,
"Unknown conntrack attr (type=%d, max=%d)",
type, OVS_CT_ATTR_MAX);
return -EINVAL;
}
maxlen = ovs_ct_attr_lens[type].maxlen;
minlen = ovs_ct_attr_lens[type].minlen;
if (nla_len(a) < minlen || nla_len(a) > maxlen) {
OVS_NLERR(log,
"Conntrack attr type has unexpected length (type=%d, length=%d, expected=%d)",
type, nla_len(a), maxlen);
return -EINVAL;
}
switch (type) {
case OVS_CT_ATTR_FORCE_COMMIT:
info->force = true;
/* fall through. */
case OVS_CT_ATTR_COMMIT:
info->commit = true;
break;
#ifdef CONFIG_NF_CONNTRACK_ZONES
case OVS_CT_ATTR_ZONE:
info->zone.id = nla_get_u16(a);
break;
#endif
#ifdef CONFIG_NF_CONNTRACK_MARK
case OVS_CT_ATTR_MARK: {
struct md_mark *mark = nla_data(a);
if (!mark->mask) {
OVS_NLERR(log, "ct_mark mask cannot be 0");
return -EINVAL;
}
info->mark = *mark;
break;
}
#endif
#ifdef CONFIG_NF_CONNTRACK_LABELS
case OVS_CT_ATTR_LABELS: {
struct md_labels *labels = nla_data(a);
if (!labels_nonzero(&labels->mask)) {
OVS_NLERR(log, "ct_labels mask cannot be 0");
return -EINVAL;
}
info->labels = *labels;
break;
}
#endif
case OVS_CT_ATTR_HELPER:
*helper = nla_data(a);
if (!memchr(*helper, '\0', nla_len(a))) {
OVS_NLERR(log, "Invalid conntrack helper");
return -EINVAL;
}
break;
#ifdef CONFIG_NF_NAT_NEEDED
case OVS_CT_ATTR_NAT: {
int err = parse_nat(a, info, log);
if (err)
return err;
break;
}
#endif
case OVS_CT_ATTR_EVENTMASK:
info->have_eventmask = true;
info->eventmask = nla_get_u32(a);
break;
default:
OVS_NLERR(log, "Unknown conntrack attr (%d)",
type);
return -EINVAL;
}
}
#ifdef CONFIG_NF_CONNTRACK_MARK
if (!info->commit && info->mark.mask) {
OVS_NLERR(log,
"Setting conntrack mark requires 'commit' flag.");
return -EINVAL;
}
#endif
#ifdef CONFIG_NF_CONNTRACK_LABELS
if (!info->commit && labels_nonzero(&info->labels.mask)) {
OVS_NLERR(log,
"Setting conntrack labels requires 'commit' flag.");
return -EINVAL;
}
#endif
if (rem > 0) {
OVS_NLERR(log, "Conntrack attr has %d unknown bytes", rem);
return -EINVAL;
}
return 0;
}
bool ovs_ct_verify(struct net *net, enum ovs_key_attr attr)
{
if (attr == OVS_KEY_ATTR_CT_STATE)
return true;
if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES) &&
attr == OVS_KEY_ATTR_CT_ZONE)
return true;
if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) &&
attr == OVS_KEY_ATTR_CT_MARK)
return true;
if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS) &&
attr == OVS_KEY_ATTR_CT_LABELS) {
struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
return ovs_net->xt_label;
}
return false;
}
int ovs_ct_copy_action(struct net *net, const struct nlattr *attr,
const struct sw_flow_key *key,
struct sw_flow_actions **sfa, bool log)
{
struct ovs_conntrack_info ct_info;
const char *helper = NULL;
u16 family;
int err;
family = key_to_nfproto(key);
if (family == NFPROTO_UNSPEC) {
OVS_NLERR(log, "ct family unspecified");
return -EINVAL;
}
memset(&ct_info, 0, sizeof(ct_info));
ct_info.family = family;
nf_ct_zone_init(&ct_info.zone, NF_CT_DEFAULT_ZONE_ID,
NF_CT_DEFAULT_ZONE_DIR, 0);
err = parse_ct(attr, &ct_info, &helper, log);
if (err)
return err;
/* Set up template for tracking connections in specific zones. */
ct_info.ct = nf_ct_tmpl_alloc(net, &ct_info.zone, GFP_KERNEL);
if (!ct_info.ct) {
OVS_NLERR(log, "Failed to allocate conntrack template");
return -ENOMEM;
}
if (helper) {
err = ovs_ct_add_helper(&ct_info, helper, key, log);
if (err)
goto err_free_ct;
}
err = ovs_nla_add_action(sfa, OVS_ACTION_ATTR_CT, &ct_info,
sizeof(ct_info), log);
if (err)
goto err_free_ct;
__set_bit(IPS_CONFIRMED_BIT, &ct_info.ct->status);
nf_conntrack_get(&ct_info.ct->ct_general);
return 0;
err_free_ct:
__ovs_ct_free_action(&ct_info);
return err;
}
#ifdef CONFIG_NF_NAT_NEEDED
static bool ovs_ct_nat_to_attr(const struct ovs_conntrack_info *info,
struct sk_buff *skb)
{
struct nlattr *start;
start = nla_nest_start(skb, OVS_CT_ATTR_NAT);
if (!start)
return false;
if (info->nat & OVS_CT_SRC_NAT) {
if (nla_put_flag(skb, OVS_NAT_ATTR_SRC))
return false;
} else if (info->nat & OVS_CT_DST_NAT) {
if (nla_put_flag(skb, OVS_NAT_ATTR_DST))
return false;
} else {
goto out;
}
if (info->range.flags & NF_NAT_RANGE_MAP_IPS) {
if (IS_ENABLED(CONFIG_NF_NAT_IPV4) &&
info->family == NFPROTO_IPV4) {
if (nla_put_in_addr(skb, OVS_NAT_ATTR_IP_MIN,
info->range.min_addr.ip) ||
(info->range.max_addr.ip
!= info->range.min_addr.ip &&
(nla_put_in_addr(skb, OVS_NAT_ATTR_IP_MAX,
info->range.max_addr.ip))))
return false;
} else if (IS_ENABLED(CONFIG_NF_NAT_IPV6) &&
info->family == NFPROTO_IPV6) {
if (nla_put_in6_addr(skb, OVS_NAT_ATTR_IP_MIN,
&info->range.min_addr.in6) ||
(memcmp(&info->range.max_addr.in6,
&info->range.min_addr.in6,
sizeof(info->range.max_addr.in6)) &&
(nla_put_in6_addr(skb, OVS_NAT_ATTR_IP_MAX,
&info->range.max_addr.in6))))
return false;
} else {
return false;
}
}
if (info->range.flags & NF_NAT_RANGE_PROTO_SPECIFIED &&
(nla_put_u16(skb, OVS_NAT_ATTR_PROTO_MIN,
ntohs(info->range.min_proto.all)) ||
(info->range.max_proto.all != info->range.min_proto.all &&
nla_put_u16(skb, OVS_NAT_ATTR_PROTO_MAX,
ntohs(info->range.max_proto.all)))))
return false;
if (info->range.flags & NF_NAT_RANGE_PERSISTENT &&
nla_put_flag(skb, OVS_NAT_ATTR_PERSISTENT))
return false;
if (info->range.flags & NF_NAT_RANGE_PROTO_RANDOM &&
nla_put_flag(skb, OVS_NAT_ATTR_PROTO_HASH))
return false;
if (info->range.flags & NF_NAT_RANGE_PROTO_RANDOM_FULLY &&
nla_put_flag(skb, OVS_NAT_ATTR_PROTO_RANDOM))
return false;
out:
nla_nest_end(skb, start);
return true;
}
#endif
int ovs_ct_action_to_attr(const struct ovs_conntrack_info *ct_info,
struct sk_buff *skb)
{
struct nlattr *start;
start = nla_nest_start(skb, OVS_ACTION_ATTR_CT);
if (!start)
return -EMSGSIZE;
if (ct_info->commit && nla_put_flag(skb, ct_info->force
? OVS_CT_ATTR_FORCE_COMMIT
: OVS_CT_ATTR_COMMIT))
return -EMSGSIZE;
if (IS_ENABLED(CONFIG_NF_CONNTRACK_ZONES) &&
nla_put_u16(skb, OVS_CT_ATTR_ZONE, ct_info->zone.id))
return -EMSGSIZE;
if (IS_ENABLED(CONFIG_NF_CONNTRACK_MARK) && ct_info->mark.mask &&
nla_put(skb, OVS_CT_ATTR_MARK, sizeof(ct_info->mark),
&ct_info->mark))
return -EMSGSIZE;
if (IS_ENABLED(CONFIG_NF_CONNTRACK_LABELS) &&
labels_nonzero(&ct_info->labels.mask) &&
nla_put(skb, OVS_CT_ATTR_LABELS, sizeof(ct_info->labels),
&ct_info->labels))
return -EMSGSIZE;
if (ct_info->helper) {
if (nla_put_string(skb, OVS_CT_ATTR_HELPER,
ct_info->helper->name))
return -EMSGSIZE;
}
if (ct_info->have_eventmask &&
nla_put_u32(skb, OVS_CT_ATTR_EVENTMASK, ct_info->eventmask))
return -EMSGSIZE;
#ifdef CONFIG_NF_NAT_NEEDED
if (ct_info->nat && !ovs_ct_nat_to_attr(ct_info, skb))
return -EMSGSIZE;
#endif
nla_nest_end(skb, start);
return 0;
}
void ovs_ct_free_action(const struct nlattr *a)
{
struct ovs_conntrack_info *ct_info = nla_data(a);
__ovs_ct_free_action(ct_info);
}
static void __ovs_ct_free_action(struct ovs_conntrack_info *ct_info)
{
if (ct_info->helper)
nf_conntrack_helper_put(ct_info->helper);
if (ct_info->ct)
nf_ct_tmpl_free(ct_info->ct);
}
#if IS_ENABLED(CONFIG_NETFILTER_CONNCOUNT)
static int ovs_ct_limit_init(struct net *net, struct ovs_net *ovs_net)
{
int i, err;
ovs_net->ct_limit_info = kmalloc(sizeof(*ovs_net->ct_limit_info),
GFP_KERNEL);
if (!ovs_net->ct_limit_info)
return -ENOMEM;
ovs_net->ct_limit_info->default_limit = OVS_CT_LIMIT_DEFAULT;
ovs_net->ct_limit_info->limits =
kmalloc_array(CT_LIMIT_HASH_BUCKETS, sizeof(struct hlist_head),
GFP_KERNEL);
if (!ovs_net->ct_limit_info->limits) {
kfree(ovs_net->ct_limit_info);
return -ENOMEM;
}
for (i = 0; i < CT_LIMIT_HASH_BUCKETS; i++)
INIT_HLIST_HEAD(&ovs_net->ct_limit_info->limits[i]);
ovs_net->ct_limit_info->data =
nf_conncount_init(net, NFPROTO_INET, sizeof(u32));
if (IS_ERR(ovs_net->ct_limit_info->data)) {
err = PTR_ERR(ovs_net->ct_limit_info->data);
kfree(ovs_net->ct_limit_info->limits);
kfree(ovs_net->ct_limit_info);
pr_err("openvswitch: failed to init nf_conncount %d\n", err);
return err;
}
return 0;
}
static void ovs_ct_limit_exit(struct net *net, struct ovs_net *ovs_net)
{
const struct ovs_ct_limit_info *info = ovs_net->ct_limit_info;
int i;
nf_conncount_destroy(net, NFPROTO_INET, info->data);
for (i = 0; i < CT_LIMIT_HASH_BUCKETS; ++i) {
struct hlist_head *head = &info->limits[i];
struct ovs_ct_limit *ct_limit;
struct hlist_node *next;
hlist_for_each_entry_safe(ct_limit, next, head, hlist_node)
kfree_rcu(ct_limit, rcu);
}
kfree(ovs_net->ct_limit_info->limits);
kfree(ovs_net->ct_limit_info);
}
static struct sk_buff *
ovs_ct_limit_cmd_reply_start(struct genl_info *info, u8 cmd,
struct ovs_header **ovs_reply_header)
{
struct ovs_header *ovs_header = info->userhdr;
struct sk_buff *skb;
skb = genlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
if (!skb)
return ERR_PTR(-ENOMEM);
*ovs_reply_header = genlmsg_put(skb, info->snd_portid,
info->snd_seq,
&dp_ct_limit_genl_family, 0, cmd);
if (!*ovs_reply_header) {
nlmsg_free(skb);
return ERR_PTR(-EMSGSIZE);
}
(*ovs_reply_header)->dp_ifindex = ovs_header->dp_ifindex;
return skb;
}
static bool check_zone_id(int zone_id, u16 *pzone)
{
if (zone_id >= 0 && zone_id <= 65535) {
*pzone = (u16)zone_id;
return true;
}
return false;
}
static int ovs_ct_limit_set_zone_limit(struct nlattr *nla_zone_limit,
struct ovs_ct_limit_info *info)
{
struct ovs_zone_limit *zone_limit;
int rem;
u16 zone;
rem = NLA_ALIGN(nla_len(nla_zone_limit));
zone_limit = (struct ovs_zone_limit *)nla_data(nla_zone_limit);
while (rem >= sizeof(*zone_limit)) {
if (unlikely(zone_limit->zone_id ==
OVS_ZONE_LIMIT_DEFAULT_ZONE)) {
ovs_lock();
info->default_limit = zone_limit->limit;
ovs_unlock();
} else if (unlikely(!check_zone_id(
zone_limit->zone_id, &zone))) {
OVS_NLERR(true, "zone id is out of range");
} else {
struct ovs_ct_limit *ct_limit;
ct_limit = kmalloc(sizeof(*ct_limit), GFP_KERNEL);
if (!ct_limit)
return -ENOMEM;
ct_limit->zone = zone;
ct_limit->limit = zone_limit->limit;
ovs_lock();
ct_limit_set(info, ct_limit);
ovs_unlock();
}
rem -= NLA_ALIGN(sizeof(*zone_limit));
zone_limit = (struct ovs_zone_limit *)((u8 *)zone_limit +
NLA_ALIGN(sizeof(*zone_limit)));
}
if (rem)
OVS_NLERR(true, "set zone limit has %d unknown bytes", rem);
return 0;
}
static int ovs_ct_limit_del_zone_limit(struct nlattr *nla_zone_limit,
struct ovs_ct_limit_info *info)
{
struct ovs_zone_limit *zone_limit;
int rem;
u16 zone;
rem = NLA_ALIGN(nla_len(nla_zone_limit));
zone_limit = (struct ovs_zone_limit *)nla_data(nla_zone_limit);
while (rem >= sizeof(*zone_limit)) {
if (unlikely(zone_limit->zone_id ==
OVS_ZONE_LIMIT_DEFAULT_ZONE)) {
ovs_lock();
info->default_limit = OVS_CT_LIMIT_DEFAULT;
ovs_unlock();
} else if (unlikely(!check_zone_id(
zone_limit->zone_id, &zone))) {
OVS_NLERR(true, "zone id is out of range");
} else {
ovs_lock();
ct_limit_del(info, zone);
ovs_unlock();
}
rem -= NLA_ALIGN(sizeof(*zone_limit));
zone_limit = (struct ovs_zone_limit *)((u8 *)zone_limit +
NLA_ALIGN(sizeof(*zone_limit)));
}
if (rem)
OVS_NLERR(true, "del zone limit has %d unknown bytes", rem);
return 0;
}
static int ovs_ct_limit_get_default_limit(struct ovs_ct_limit_info *info,
struct sk_buff *reply)
{
struct ovs_zone_limit zone_limit;
int err;
zone_limit.zone_id = OVS_ZONE_LIMIT_DEFAULT_ZONE;
zone_limit.limit = info->default_limit;
err = nla_put_nohdr(reply, sizeof(zone_limit), &zone_limit);
if (err)
return err;
return 0;
}
static int __ovs_ct_limit_get_zone_limit(struct net *net,
struct nf_conncount_data *data,
u16 zone_id, u32 limit,
struct sk_buff *reply)
{
struct nf_conntrack_zone ct_zone;
struct ovs_zone_limit zone_limit;
u32 conncount_key = zone_id;
zone_limit.zone_id = zone_id;
zone_limit.limit = limit;
nf_ct_zone_init(&ct_zone, zone_id, NF_CT_DEFAULT_ZONE_DIR, 0);
zone_limit.count = nf_conncount_count_skb(net, NULL, 0, data,
&conncount_key);
return nla_put_nohdr(reply, sizeof(zone_limit), &zone_limit);
}
static int ovs_ct_limit_get_zone_limit(struct net *net,
struct nlattr *nla_zone_limit,
struct ovs_ct_limit_info *info,
struct sk_buff *reply)
{
struct ovs_zone_limit *zone_limit;
int rem, err;
u32 limit;
u16 zone;
rem = NLA_ALIGN(nla_len(nla_zone_limit));
zone_limit = (struct ovs_zone_limit *)nla_data(nla_zone_limit);
while (rem >= sizeof(*zone_limit)) {
if (unlikely(zone_limit->zone_id ==
OVS_ZONE_LIMIT_DEFAULT_ZONE)) {
err = ovs_ct_limit_get_default_limit(info, reply);
if (err)
return err;
} else if (unlikely(!check_zone_id(zone_limit->zone_id,
&zone))) {
OVS_NLERR(true, "zone id is out of range");
} else {
rcu_read_lock();
limit = ct_limit_get(info, zone);
rcu_read_unlock();
err = __ovs_ct_limit_get_zone_limit(
net, info->data, zone, limit, reply);
if (err)
return err;
}
rem -= NLA_ALIGN(sizeof(*zone_limit));
zone_limit = (struct ovs_zone_limit *)((u8 *)zone_limit +
NLA_ALIGN(sizeof(*zone_limit)));
}
if (rem)
OVS_NLERR(true, "get zone limit has %d unknown bytes", rem);
return 0;
}
static int ovs_ct_limit_get_all_zone_limit(struct net *net,
struct ovs_ct_limit_info *info,
struct sk_buff *reply)
{
struct ovs_ct_limit *ct_limit;
struct hlist_head *head;
int i, err = 0;
err = ovs_ct_limit_get_default_limit(info, reply);
if (err)
return err;
rcu_read_lock();
for (i = 0; i < CT_LIMIT_HASH_BUCKETS; ++i) {
head = &info->limits[i];
hlist_for_each_entry_rcu(ct_limit, head, hlist_node) {
err = __ovs_ct_limit_get_zone_limit(net, info->data,
ct_limit->zone, ct_limit->limit, reply);
if (err)
goto exit_err;
}
}
exit_err:
rcu_read_unlock();
return err;
}
static int ovs_ct_limit_cmd_set(struct sk_buff *skb, struct genl_info *info)
{
struct nlattr **a = info->attrs;
struct sk_buff *reply;
struct ovs_header *ovs_reply_header;
struct ovs_net *ovs_net = net_generic(sock_net(skb->sk), ovs_net_id);
struct ovs_ct_limit_info *ct_limit_info = ovs_net->ct_limit_info;
int err;
reply = ovs_ct_limit_cmd_reply_start(info, OVS_CT_LIMIT_CMD_SET,
&ovs_reply_header);
if (IS_ERR(reply))
return PTR_ERR(reply);
if (!a[OVS_CT_LIMIT_ATTR_ZONE_LIMIT]) {
err = -EINVAL;
goto exit_err;
}
err = ovs_ct_limit_set_zone_limit(a[OVS_CT_LIMIT_ATTR_ZONE_LIMIT],
ct_limit_info);
if (err)
goto exit_err;
static_branch_enable(&ovs_ct_limit_enabled);
genlmsg_end(reply, ovs_reply_header);
return genlmsg_reply(reply, info);
exit_err:
nlmsg_free(reply);
return err;
}
static int ovs_ct_limit_cmd_del(struct sk_buff *skb, struct genl_info *info)
{
struct nlattr **a = info->attrs;
struct sk_buff *reply;
struct ovs_header *ovs_reply_header;
struct ovs_net *ovs_net = net_generic(sock_net(skb->sk), ovs_net_id);
struct ovs_ct_limit_info *ct_limit_info = ovs_net->ct_limit_info;
int err;
reply = ovs_ct_limit_cmd_reply_start(info, OVS_CT_LIMIT_CMD_DEL,
&ovs_reply_header);
if (IS_ERR(reply))
return PTR_ERR(reply);
if (!a[OVS_CT_LIMIT_ATTR_ZONE_LIMIT]) {
err = -EINVAL;
goto exit_err;
}
err = ovs_ct_limit_del_zone_limit(a[OVS_CT_LIMIT_ATTR_ZONE_LIMIT],
ct_limit_info);
if (err)
goto exit_err;
genlmsg_end(reply, ovs_reply_header);
return genlmsg_reply(reply, info);
exit_err:
nlmsg_free(reply);
return err;
}
static int ovs_ct_limit_cmd_get(struct sk_buff *skb, struct genl_info *info)
{
struct nlattr **a = info->attrs;
struct nlattr *nla_reply;
struct sk_buff *reply;
struct ovs_header *ovs_reply_header;
struct net *net = sock_net(skb->sk);
struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
struct ovs_ct_limit_info *ct_limit_info = ovs_net->ct_limit_info;
int err;
reply = ovs_ct_limit_cmd_reply_start(info, OVS_CT_LIMIT_CMD_GET,
&ovs_reply_header);
if (IS_ERR(reply))
return PTR_ERR(reply);
nla_reply = nla_nest_start(reply, OVS_CT_LIMIT_ATTR_ZONE_LIMIT);
if (a[OVS_CT_LIMIT_ATTR_ZONE_LIMIT]) {
err = ovs_ct_limit_get_zone_limit(
net, a[OVS_CT_LIMIT_ATTR_ZONE_LIMIT], ct_limit_info,
reply);
if (err)
goto exit_err;
} else {
err = ovs_ct_limit_get_all_zone_limit(net, ct_limit_info,
reply);
if (err)
goto exit_err;
}
nla_nest_end(reply, nla_reply);
genlmsg_end(reply, ovs_reply_header);
return genlmsg_reply(reply, info);
exit_err:
nlmsg_free(reply);
return err;
}
static struct genl_ops ct_limit_genl_ops[] = {
{ .cmd = OVS_CT_LIMIT_CMD_SET,
.flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN
* privilege. */
.policy = ct_limit_policy,
.doit = ovs_ct_limit_cmd_set,
},
{ .cmd = OVS_CT_LIMIT_CMD_DEL,
.flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN
* privilege. */
.policy = ct_limit_policy,
.doit = ovs_ct_limit_cmd_del,
},
{ .cmd = OVS_CT_LIMIT_CMD_GET,
.flags = 0, /* OK for unprivileged users. */
.policy = ct_limit_policy,
.doit = ovs_ct_limit_cmd_get,
},
};
static const struct genl_multicast_group ovs_ct_limit_multicast_group = {
.name = OVS_CT_LIMIT_MCGROUP,
};
struct genl_family dp_ct_limit_genl_family __ro_after_init = {
.hdrsize = sizeof(struct ovs_header),
.name = OVS_CT_LIMIT_FAMILY,
.version = OVS_CT_LIMIT_VERSION,
.maxattr = OVS_CT_LIMIT_ATTR_MAX,
.netnsok = true,
.parallel_ops = true,
.ops = ct_limit_genl_ops,
.n_ops = ARRAY_SIZE(ct_limit_genl_ops),
.mcgrps = &ovs_ct_limit_multicast_group,
.n_mcgrps = 1,
.module = THIS_MODULE,
};
#endif
int ovs_ct_init(struct net *net)
{
unsigned int n_bits = sizeof(struct ovs_key_ct_labels) * BITS_PER_BYTE;
struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
if (nf_connlabels_get(net, n_bits - 1)) {
ovs_net->xt_label = false;
OVS_NLERR(true, "Failed to set connlabel length");
} else {
ovs_net->xt_label = true;
}
#if IS_ENABLED(CONFIG_NETFILTER_CONNCOUNT)
return ovs_ct_limit_init(net, ovs_net);
#else
return 0;
#endif
}
void ovs_ct_exit(struct net *net)
{
struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
#if IS_ENABLED(CONFIG_NETFILTER_CONNCOUNT)
ovs_ct_limit_exit(net, ovs_net);
#endif
if (ovs_net->xt_label)
nf_connlabels_put(net);
}