From 60daf104a1bd54b97c7bf3d71d74b637a0507ee1 Mon Sep 17 00:00:00 2001 From: Ashish Sharma Date: Thu, 7 Jul 2011 18:16:01 -0700 Subject: [PATCH 1/4] Add communication support for xt_qtaguid(network traffic accounting) kernel module. Change-Id: Ie0fb5b593987c53ee6f906fe6e0caab5a581d5a1 --- include/cutils/qtaguid.h | 36 ++++++++++++++++++++++++++++++++ libcutils/Android.mk | 3 ++- libcutils/qtaguid.c | 44 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 82 insertions(+), 1 deletion(-) create mode 100644 include/cutils/qtaguid.h create mode 100644 libcutils/qtaguid.c diff --git a/include/cutils/qtaguid.h b/include/cutils/qtaguid.h new file mode 100644 index 000000000..dd2db5224 --- /dev/null +++ b/include/cutils/qtaguid.h @@ -0,0 +1,36 @@ +/* + * Copyright (C) 2011 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef __CUTILS_QTAGUID_H +#define __CUTILS_QTAGUID_H + +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +/* + * Set tags (and owning UIDs) for network sockets. +*/ +extern int set_qtaguid(int sockfd, int tag, uid_t uid); + +#ifdef __cplusplus +} +#endif + +#endif /* __CUTILS_QTAG_UID_H */ diff --git a/libcutils/Android.mk b/libcutils/Android.mk index 84cccd989..64b369edf 100644 --- a/libcutils/Android.mk +++ b/libcutils/Android.mk @@ -47,7 +47,8 @@ commonSources := \ threads.c \ sched_policy.c \ iosched_policy.c \ - str_parms.c + str_parms.c \ + qtaguid.c commonHostSources := \ ashmem-host.c diff --git a/libcutils/qtaguid.c b/libcutils/qtaguid.c new file mode 100644 index 000000000..517e78422 --- /dev/null +++ b/libcutils/qtaguid.c @@ -0,0 +1,44 @@ +/* libcutils/qtaguid.c +** +** Copyright 2011, The Android Open Source Project +** +** Licensed under the Apache License, Version 2.0 (the "License"); +** you may not use this file except in compliance with the License. +** You may obtain a copy of the License at +** +** http://www.apache.org/licenses/LICENSE-2.0 +** +** Unless required by applicable law or agreed to in writing, software +** distributed under the License is distributed on an "AS IS" BASIS, +** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +** See the License for the specific language governing permissions and +** limitations under the License. +*/ + +#define LOG_TAG "qtaguid" + +#include +#include +#include +#include +#include +#include + +extern int set_qtaguid(int sockfd, int tag, uid_t uid) { + char lineBuf[128]; + int fd, cnt = 0; + uint64_t kTag = (uint64_t)tag << 32; + snprintf(lineBuf, sizeof(lineBuf), "t %d %llu %d", sockfd, kTag, uid); + + LOGV("Tagging Socket with command %s\n", lineBuf); + /* TODO: Enable after the kernel module is fixed. + fd = open("/proc/net/xt_qtaguid/ctrl", O_WRONLY); + if (fd < 0) { + return -1; + } + + cnt = write(fd, lineBuf, strlen(lineBuf)); + close(fd); + */ + return (cnt>0?0:-1); +} From 13825eb0a3f4a4914e20a5096e8ff7e73334752e Mon Sep 17 00:00:00 2001 From: Ashish Sharma Date: Sun, 10 Jul 2011 13:57:05 -0700 Subject: [PATCH 2/4] Fix header file inclusion for type uid_t. Change-Id: I401fc0b41b4b77114fce7240662c9dfe4509e841 --- include/cutils/qtaguid.h | 1 + 1 file changed, 1 insertion(+) diff --git a/include/cutils/qtaguid.h b/include/cutils/qtaguid.h index dd2db5224..8aa34ea35 100644 --- a/include/cutils/qtaguid.h +++ b/include/cutils/qtaguid.h @@ -19,6 +19,7 @@ #include #include +#include #ifdef __cplusplus extern "C" { From 86993946391ba6603974b61f35fbaf4463893f00 Mon Sep 17 00:00:00 2001 From: JP Abgrall Date: Wed, 10 Aug 2011 12:24:54 -0700 Subject: [PATCH 3/4] Move qtaguid.c from common sources in Android.mk to fix windows build. Change-Id: I0a2d1615108b6c10064b3635d05699748a1341a4 --- libcutils/Android.mk | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/libcutils/Android.mk b/libcutils/Android.mk index 64b369edf..03e6e9a61 100644 --- a/libcutils/Android.mk +++ b/libcutils/Android.mk @@ -47,8 +47,7 @@ commonSources := \ threads.c \ sched_policy.c \ iosched_policy.c \ - str_parms.c \ - qtaguid.c + str_parms.c commonHostSources := \ ashmem-host.c @@ -111,7 +110,7 @@ else #!sim # ======================================================== include $(CLEAR_VARS) LOCAL_MODULE := libcutils -LOCAL_SRC_FILES := $(commonSources) ashmem-dev.c mq.c uevent.c +LOCAL_SRC_FILES := $(commonSources) ashmem-dev.c mq.c uevent.c qtaguid.c ifeq ($(TARGET_ARCH),arm) LOCAL_SRC_FILES += arch-arm/memset32.S From fa2f985b295fbf98eb45a9b5eb100f946055c5b4 Mon Sep 17 00:00:00 2001 From: Ashish Sharma Date: Wed, 3 Aug 2011 00:31:19 -0700 Subject: [PATCH 4/4] libcutils: qtaguid: support socket untagging, return errors. - Enable and rename qtaguid_tagSocket() - Add qtaguid_untagSocket() - Return kernel errors to caller Change-Id: I8e33c8832b7f6b24ed9081f36ce1ea9ae6b099c0 Signed-off-by: Ashish Sharma --- include/cutils/qtaguid.h | 7 +++++- libcutils/qtaguid.c | 47 ++++++++++++++++++++++++++++++---------- 2 files changed, 41 insertions(+), 13 deletions(-) diff --git a/include/cutils/qtaguid.h b/include/cutils/qtaguid.h index 8aa34ea35..e6d61e639 100644 --- a/include/cutils/qtaguid.h +++ b/include/cutils/qtaguid.h @@ -28,7 +28,12 @@ extern "C" { /* * Set tags (and owning UIDs) for network sockets. */ -extern int set_qtaguid(int sockfd, int tag, uid_t uid); +extern int qtaguid_tagSocket(int sockfd, int tag, uid_t uid); + +/* + * Untag a network socket before closing. +*/ +extern int qtaguid_untagSocket(int sockfd); #ifdef __cplusplus } diff --git a/libcutils/qtaguid.c b/libcutils/qtaguid.c index 517e78422..218a21f13 100644 --- a/libcutils/qtaguid.c +++ b/libcutils/qtaguid.c @@ -19,26 +19,49 @@ #include #include +#include #include #include #include #include -extern int set_qtaguid(int sockfd, int tag, uid_t uid) { +extern int qtaguid_tagSocket(int sockfd, int tag, uid_t uid) { char lineBuf[128]; - int fd, cnt = 0; + int fd, cnt = 0, res = 0; uint64_t kTag = (uint64_t)tag << 32; snprintf(lineBuf, sizeof(lineBuf), "t %d %llu %d", sockfd, kTag, uid); - LOGV("Tagging Socket with command %s\n", lineBuf); - /* TODO: Enable after the kernel module is fixed. - fd = open("/proc/net/xt_qtaguid/ctrl", O_WRONLY); - if (fd < 0) { - return -1; - } + LOGI("Tagging socket %d with tag %llx(%d) for uid %d", sockfd, kTag, tag, uid); + fd = open("/proc/net/xt_qtaguid/ctrl", O_WRONLY); + if (fd < 0) { + return -errno; + } - cnt = write(fd, lineBuf, strlen(lineBuf)); - close(fd); - */ - return (cnt>0?0:-1); + cnt = write(fd, lineBuf, strlen(lineBuf)); + if (cnt < 0) { + res = -errno; + } + + close(fd); + return res; +} + +extern int qtaguid_untagSocket(int sockfd) { + char lineBuf[128]; + int fd, cnt = 0, res = 0; + snprintf(lineBuf, sizeof(lineBuf), "u %d", sockfd); + + LOGI("Untagging socket %d", sockfd); + fd = open("/proc/net/xt_qtaguid/ctrl", O_WRONLY); + if (fd < 0) { + return -errno; + } + + cnt = write(fd, lineBuf, strlen(lineBuf)); + if (cnt < 0) { + res = -errno; + } + + close(fd); + return res; }