|
| 1 | +/* |
| 2 | + * Copyright 2026 Aurora Operations, Inc. |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0 |
| 5 | + * |
| 6 | + * This work is dual licensed. |
| 7 | + * You may use it under Apache-2.0 or GPL-2.0 at your option. |
| 8 | + * |
| 9 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 10 | + * you may not use this file except in compliance with the License. |
| 11 | + * You may obtain a copy of the License at |
| 12 | + * |
| 13 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 14 | + * |
| 15 | + * Unless required by applicable law or agreed to in writing, software |
| 16 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 17 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 18 | + * See the License for the specific language governing permissions and |
| 19 | + * limitations under the License. |
| 20 | + * |
| 21 | + * OR |
| 22 | + * |
| 23 | + * This program is free software; you can redistribute it and/or |
| 24 | + * modify it under the terms of the GNU General Public License |
| 25 | + * as published by the Free Software Foundation; either version 2 |
| 26 | + * of the License, or (at your option) any later version. |
| 27 | + * |
| 28 | + * This program is distributed in the hope that it will be useful, |
| 29 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 30 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 31 | + * GNU General Public License for more details. |
| 32 | + * |
| 33 | + * You should have received a copy of the GNU General Public License |
| 34 | + * along with this program; if not, see |
| 35 | + * <https://www.gnu.org/licenses/>. |
| 36 | + */ |
| 37 | + |
| 38 | +#pragma once |
| 39 | + |
| 40 | +#include <linux/module.h> |
| 41 | +#include <linux/types.h> |
| 42 | + |
| 43 | +struct nat20device_driver {}; |
| 44 | + |
| 45 | +/** |
| 46 | + * struct nat20device_buffer - Buffer for dispatch function response |
| 47 | + * @data: Pointer to buffer data |
| 48 | + * @size: Size of the buffer in bytes |
| 49 | + */ |
| 50 | +struct nat20device_buffer { |
| 51 | + void* data; |
| 52 | + size_t size; |
| 53 | +}; |
| 54 | + |
| 55 | +/** |
| 56 | + * typedef nat20device_dispatch_fn - Dispatch function callback |
| 57 | + * @ctx: Driver-specific context |
| 58 | + * @request: Request buffer from userspace |
| 59 | + * @request_len: Length of request buffer |
| 60 | + * @response: Pointer to response buffer (allocated by driver) |
| 61 | + * |
| 62 | + * The dispatch function processes a request and returns a response buffer. |
| 63 | + * The driver must allocate the response buffer, which will be freed by |
| 64 | + * the framework using kfree after the read operation completes, |
| 65 | + * on the next write if the buffer has not been read yet, or when the file is closed. |
| 66 | + * |
| 67 | + * The framework serializes calls per open file descriptor. However, if the |
| 68 | + * device is opened multiple times, dispatch may be called concurrently with |
| 69 | + * the same @ctx from different file descriptors. The implementer must protect |
| 70 | + * shared state in @ctx against concurrent access. |
| 71 | + * |
| 72 | + * Return: 0 on success, negative error code on failure |
| 73 | + */ |
| 74 | +typedef int (*nat20device_dispatch_fn)(void* ctx, |
| 75 | + void const* request, |
| 76 | + size_t request_len, |
| 77 | + struct nat20device_buffer* response); |
| 78 | + |
| 79 | +/** |
| 80 | + * typedef nat20device_dice_chain_read - DICE chain read function callback |
| 81 | + * @ctx: Driver-specific context |
| 82 | + * @buf: User-space buffer to read DICE chain data into |
| 83 | + * @len: Length of the buffer |
| 84 | + * @f_pos: File position offset |
| 85 | + * |
| 86 | + * Reads the DICE certificate chain into the provided user-space buffer. |
| 87 | + * The data is encoded as a CBOR indefinite-length array. See |
| 88 | + * examples/linux/README.md for the encoding specification. |
| 89 | + * |
| 90 | + * This function may be called concurrently from multiple readers via the |
| 91 | + * securityfs interface. The implementer must ensure that concurrent access |
| 92 | + * to the underlying data is safe. |
| 93 | + * |
| 94 | + * Return: Number of bytes read on success, negative error code on failure |
| 95 | + */ |
| 96 | +typedef ssize_t (*nat20device_dice_chain_read)(void* ctx, |
| 97 | + char __user* buf, |
| 98 | + size_t len, |
| 99 | + loff_t* f_pos); |
| 100 | + |
| 101 | +/** |
| 102 | + * struct nat20device_driver_ops - Driver operations |
| 103 | + * @dispatch: Dispatch function for handling requests |
| 104 | + * @dice_chain_read: DICE chain read function for reading the boot certificate chain |
| 105 | + */ |
| 106 | +struct nat20device_driver_ops { |
| 107 | + nat20device_dispatch_fn dispatch; |
| 108 | + nat20device_dice_chain_read dice_chain_read; |
| 109 | +}; |
| 110 | + |
| 111 | +/** |
| 112 | + * nat20device_register_driver - Register a new NAT20 driver instance |
| 113 | + * @ops: Driver operations structure |
| 114 | + * @ctx: Driver-specific context |
| 115 | + * @owner: Module owner (usually THIS_MODULE). This is used to manage module |
| 116 | + * reference counting for the driver instance. Blocks the removal |
| 117 | + * of the module while a device node remains open. |
| 118 | + * |
| 119 | + * Registers a new driver instance and creates a character device node |
| 120 | + * with the name "nat20X" where X is an automatically assigned number. |
| 121 | + * |
| 122 | + * Return: Pointer to registered driver on success, ERR_PTR on failure |
| 123 | + */ |
| 124 | +struct nat20device_driver* nat20device_register_driver(const struct nat20device_driver_ops* ops, |
| 125 | + void* ctx, |
| 126 | + struct module* owner); |
| 127 | + |
| 128 | +/** |
| 129 | + * nat20device_unregister_driver - Unregister a NAT20 driver instance |
| 130 | + * @driver: Driver instance to unregister |
| 131 | + * |
| 132 | + * Unregisters a driver instance and removes its character device node. |
| 133 | + * |
| 134 | + * IMPORTANT: |
| 135 | + * This function must only be called from the registering module's exit |
| 136 | + * function. The file_operations.owner field is set to the registering |
| 137 | + * module, causing the kernel to hold a module reference for each open |
| 138 | + * file descriptor. This guarantees that module unload (and thus this |
| 139 | + * function) cannot execute while any file descriptor is still open. |
| 140 | + * Calling this function from any other context voids this guarantee |
| 141 | + * and results in undefined behavior. |
| 142 | + */ |
| 143 | +void nat20device_unregister_driver(struct nat20device_driver* driver); |
0 commit comments