18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * NetLabel NETLINK Interface
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * This file defines the NETLINK interface for the NetLabel system.  The
68c2ecf20Sopenharmony_ci * NetLabel system manages static and dynamic label mappings for network
78c2ecf20Sopenharmony_ci * protocols such as CIPSO and RIPSO.
88c2ecf20Sopenharmony_ci *
98c2ecf20Sopenharmony_ci * Author: Paul Moore <paul@paul-moore.com>
108c2ecf20Sopenharmony_ci */
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_ci/*
138c2ecf20Sopenharmony_ci * (c) Copyright Hewlett-Packard Development Company, L.P., 2006
148c2ecf20Sopenharmony_ci */
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_ci#include <linux/init.h>
178c2ecf20Sopenharmony_ci#include <linux/types.h>
188c2ecf20Sopenharmony_ci#include <linux/list.h>
198c2ecf20Sopenharmony_ci#include <linux/socket.h>
208c2ecf20Sopenharmony_ci#include <linux/audit.h>
218c2ecf20Sopenharmony_ci#include <linux/tty.h>
228c2ecf20Sopenharmony_ci#include <linux/security.h>
238c2ecf20Sopenharmony_ci#include <linux/gfp.h>
248c2ecf20Sopenharmony_ci#include <net/sock.h>
258c2ecf20Sopenharmony_ci#include <net/netlink.h>
268c2ecf20Sopenharmony_ci#include <net/genetlink.h>
278c2ecf20Sopenharmony_ci#include <net/netlabel.h>
288c2ecf20Sopenharmony_ci#include <asm/bug.h>
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_ci#include "netlabel_mgmt.h"
318c2ecf20Sopenharmony_ci#include "netlabel_unlabeled.h"
328c2ecf20Sopenharmony_ci#include "netlabel_cipso_v4.h"
338c2ecf20Sopenharmony_ci#include "netlabel_calipso.h"
348c2ecf20Sopenharmony_ci#include "netlabel_user.h"
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_ci/*
378c2ecf20Sopenharmony_ci * NetLabel NETLINK Setup Functions
388c2ecf20Sopenharmony_ci */
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_ci/**
418c2ecf20Sopenharmony_ci * netlbl_netlink_init - Initialize the NETLINK communication channel
428c2ecf20Sopenharmony_ci *
438c2ecf20Sopenharmony_ci * Description:
448c2ecf20Sopenharmony_ci * Call out to the NetLabel components so they can register their families and
458c2ecf20Sopenharmony_ci * commands with the Generic NETLINK mechanism.  Returns zero on success and
468c2ecf20Sopenharmony_ci * non-zero on failure.
478c2ecf20Sopenharmony_ci *
488c2ecf20Sopenharmony_ci */
498c2ecf20Sopenharmony_ciint __init netlbl_netlink_init(void)
508c2ecf20Sopenharmony_ci{
518c2ecf20Sopenharmony_ci	int ret_val;
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_ci	ret_val = netlbl_mgmt_genl_init();
548c2ecf20Sopenharmony_ci	if (ret_val != 0)
558c2ecf20Sopenharmony_ci		return ret_val;
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_ci	ret_val = netlbl_cipsov4_genl_init();
588c2ecf20Sopenharmony_ci	if (ret_val != 0)
598c2ecf20Sopenharmony_ci		return ret_val;
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_ci	ret_val = netlbl_calipso_genl_init();
628c2ecf20Sopenharmony_ci	if (ret_val != 0)
638c2ecf20Sopenharmony_ci		return ret_val;
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_ci	return netlbl_unlabel_genl_init();
668c2ecf20Sopenharmony_ci}
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_ci/*
698c2ecf20Sopenharmony_ci * NetLabel Audit Functions
708c2ecf20Sopenharmony_ci */
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_ci/**
738c2ecf20Sopenharmony_ci * netlbl_audit_start_common - Start an audit message
748c2ecf20Sopenharmony_ci * @type: audit message type
758c2ecf20Sopenharmony_ci * @audit_info: NetLabel audit information
768c2ecf20Sopenharmony_ci *
778c2ecf20Sopenharmony_ci * Description:
788c2ecf20Sopenharmony_ci * Start an audit message using the type specified in @type and fill the audit
798c2ecf20Sopenharmony_ci * message with some fields common to all NetLabel audit messages.  Returns
808c2ecf20Sopenharmony_ci * a pointer to the audit buffer on success, NULL on failure.
818c2ecf20Sopenharmony_ci *
828c2ecf20Sopenharmony_ci */
838c2ecf20Sopenharmony_cistruct audit_buffer *netlbl_audit_start_common(int type,
848c2ecf20Sopenharmony_ci					       struct netlbl_audit *audit_info)
858c2ecf20Sopenharmony_ci{
868c2ecf20Sopenharmony_ci	struct audit_buffer *audit_buf;
878c2ecf20Sopenharmony_ci	char *secctx;
888c2ecf20Sopenharmony_ci	u32 secctx_len;
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_ci	if (audit_enabled == AUDIT_OFF)
918c2ecf20Sopenharmony_ci		return NULL;
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_ci	audit_buf = audit_log_start(audit_context(), GFP_ATOMIC, type);
948c2ecf20Sopenharmony_ci	if (audit_buf == NULL)
958c2ecf20Sopenharmony_ci		return NULL;
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_ci	audit_log_format(audit_buf, "netlabel: auid=%u ses=%u",
988c2ecf20Sopenharmony_ci			 from_kuid(&init_user_ns, audit_info->loginuid),
998c2ecf20Sopenharmony_ci			 audit_info->sessionid);
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_ci	if (audit_info->secid != 0 &&
1028c2ecf20Sopenharmony_ci	    security_secid_to_secctx(audit_info->secid,
1038c2ecf20Sopenharmony_ci				     &secctx,
1048c2ecf20Sopenharmony_ci				     &secctx_len) == 0) {
1058c2ecf20Sopenharmony_ci		audit_log_format(audit_buf, " subj=%s", secctx);
1068c2ecf20Sopenharmony_ci		security_release_secctx(secctx, secctx_len);
1078c2ecf20Sopenharmony_ci	}
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_ci	return audit_buf;
1108c2ecf20Sopenharmony_ci}
111