1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause 2 #ifndef __YNL_C_H 3 #define __YNL_C_H 1 4 5 #include <stddef.h> 6 #include <linux/genetlink.h> 7 #include <linux/types.h> 8 9 #include "ynl-priv.h" 10 11 enum ynl_error_code { 12 YNL_ERROR_NONE = 0, 13 __YNL_ERRNO_END = 4096, 14 YNL_ERROR_INTERNAL, 15 YNL_ERROR_EXPECT_ACK, 16 YNL_ERROR_EXPECT_MSG, 17 YNL_ERROR_UNEXPECT_MSG, 18 YNL_ERROR_ATTR_MISSING, 19 YNL_ERROR_ATTR_INVALID, 20 YNL_ERROR_UNKNOWN_NTF, 21 YNL_ERROR_INV_RESP, 22 }; 23 24 /** 25 * struct ynl_error - error encountered by YNL 26 * @code: errno (low values) or YNL error code (enum ynl_error_code) 27 * @attr_offs: offset of bad attribute (for very advanced users) 28 * @msg: error message 29 * 30 * Error information for when YNL operations fail. 31 * Users should interact with the err member of struct ynl_sock directly. 32 * The main exception to that rule is ynl_sock_create(). 33 */ 34 struct ynl_error { 35 enum ynl_error_code code; 36 unsigned int attr_offs; 37 char msg[512]; 38 }; 39 40 /** 41 * struct ynl_family - YNL family info 42 * Family description generated by codegen. Pass to ynl_sock_create(). 43 */ 44 struct ynl_family { 45 /* private: */ 46 const char *name; 47 const struct ynl_ntf_info *ntf_info; 48 unsigned int ntf_info_size; 49 }; 50 51 /** 52 * struct ynl_sock - YNL wrapped netlink socket 53 * @err: YNL error descriptor, cleared on every request. 54 */ 55 struct ynl_sock { 56 struct ynl_error err; 57 58 /* private: */ 59 const struct ynl_family *family; 60 struct mnl_socket *sock; 61 __u32 seq; 62 __u32 portid; 63 __u16 family_id; 64 65 unsigned int n_mcast_groups; 66 struct { 67 unsigned int id; 68 char name[GENL_NAMSIZ]; 69 } *mcast_groups; 70 71 struct ynl_ntf_base_type *ntf_first; 72 struct ynl_ntf_base_type **ntf_last_next; 73 74 struct nlmsghdr *nlh; 75 struct ynl_policy_nest *req_policy; 76 unsigned char *tx_buf; 77 unsigned char *rx_buf; 78 unsigned char raw_buf[]; 79 }; 80 81 struct ynl_sock * 82 ynl_sock_create(const struct ynl_family *yf, struct ynl_error *e); 83 void ynl_sock_destroy(struct ynl_sock *ys); 84 85 #define ynl_dump_foreach(dump, iter) \ 86 for (typeof(dump->obj) *iter = &dump->obj; \ 87 !ynl_dump_obj_is_last(iter); \ 88 iter = ynl_dump_obj_next(iter)) 89 90 int ynl_subscribe(struct ynl_sock *ys, const char *grp_name); 91 int ynl_socket_get_fd(struct ynl_sock *ys); 92 int ynl_ntf_check(struct ynl_sock *ys); 93 94 /** 95 * ynl_has_ntf() - check if socket has *parsed* notifications 96 * @ys: active YNL socket 97 * 98 * Note that this does not take into account notifications sitting 99 * in netlink socket, just the notifications which have already been 100 * read and parsed (e.g. during a ynl_ntf_check() call). 101 */ 102 static inline bool ynl_has_ntf(struct ynl_sock *ys) 103 { 104 return ys->ntf_last_next != &ys->ntf_first; 105 } 106 struct ynl_ntf_base_type *ynl_ntf_dequeue(struct ynl_sock *ys); 107 108 void ynl_ntf_free(struct ynl_ntf_base_type *ntf); 109 #endif 110