Initial netd binder interface.

In this change:

1. AIDL files for a new, android.net.INetd service, and
   corresponding implementation using generated code. For now the
   interface is just a prototype: it only has one trivial method.
2. Permission checking code to check for CONNECTIVITY_INTERNAL.
3. Add a Big Netd Lock and provide a wrapper that makes it easy
   to ensure that it is taken by every CommandListener command.

Bug: 27239233
Change-Id: I448d0ac233edd0e351a7fe7f13901fb6871683a2
diff --git a/server/NetdNativeService.cpp b/server/NetdNativeService.cpp
new file mode 100644
index 0000000..f4b5a10
--- /dev/null
+++ b/server/NetdNativeService.cpp
@@ -0,0 +1,73 @@
+/**
+ * Copyright (c) 2016, 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 "Netd"
+
+#include <android-base/stringprintf.h>
+#include <cutils/log.h>
+#include <utils/Errors.h>
+
+#include <binder/IPCThreadState.h>
+#include <binder/IServiceManager.h>
+#include "android/net/BnNetd.h"
+
+#include "NetdConstants.h"
+#include "NetdNativeService.h"
+
+using android::base::StringPrintf;
+
+namespace android {
+namespace net {
+
+namespace {
+
+const char CONNECTIVITY_INTERNAL[] = "android.permission.CONNECTIVITY_INTERNAL";
+
+binder::Status checkPermission(const char *permission) {
+    pid_t pid;
+    uid_t uid;
+
+    if (checkCallingPermission(String16(permission), (int32_t *) &pid, (int32_t *) &uid)) {
+        return binder::Status::ok();
+    } else {
+        auto err = StringPrintf("UID %d / PID %d lacks permission %s", uid, pid, permission);
+        return binder::Status::fromExceptionCode(binder::Status::EX_SECURITY, String8(err.c_str()));
+    }
+}
+
+#define ENFORCE_PERMISSION(permission) {                    \
+    binder::Status status = checkPermission((permission));  \
+    if (!status.isOk()) {                                   \
+        return status;                                      \
+    }                                                       \
+}
+
+#define NETD_LOCKING_RPC(permission)               \
+    ENFORCE_PERMISSION(permission);                \
+    android::RWLock::AutoWLock lock(gBigNetdLock);
+
+}  // namespace
+
+
+binder::Status NetdNativeService::isAlive(bool *alive) {
+    NETD_LOCKING_RPC(CONNECTIVITY_INTERNAL);
+
+    *alive = true;
+    return binder::Status::ok();
+}
+
+}  // namespace net
+}  // namespace android