Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 */
0002 #ifndef __LINUX_NETLINK_H
0003 #define __LINUX_NETLINK_H
0004 
0005 
0006 #include <linux/capability.h>
0007 #include <linux/skbuff.h>
0008 #include <linux/export.h>
0009 #include <net/scm.h>
0010 #include <uapi/linux/netlink.h>
0011 
0012 struct net;
0013 
0014 void do_trace_netlink_extack(const char *msg);
0015 
0016 static inline struct nlmsghdr *nlmsg_hdr(const struct sk_buff *skb)
0017 {
0018     return (struct nlmsghdr *)skb->data;
0019 }
0020 
0021 enum netlink_skb_flags {
0022     NETLINK_SKB_DST     = 0x8,  /* Dst set in sendto or sendmsg */
0023 };
0024 
0025 struct netlink_skb_parms {
0026     struct scm_creds    creds;      /* Skb credentials  */
0027     __u32           portid;
0028     __u32           dst_group;
0029     __u32           flags;
0030     struct sock     *sk;
0031     bool            nsid_is_set;
0032     int         nsid;
0033 };
0034 
0035 #define NETLINK_CB(skb)     (*(struct netlink_skb_parms*)&((skb)->cb))
0036 #define NETLINK_CREDS(skb)  (&NETLINK_CB((skb)).creds)
0037 
0038 
0039 void netlink_table_grab(void);
0040 void netlink_table_ungrab(void);
0041 
0042 #define NL_CFG_F_NONROOT_RECV   (1 << 0)
0043 #define NL_CFG_F_NONROOT_SEND   (1 << 1)
0044 
0045 /* optional Netlink kernel configuration parameters */
0046 struct netlink_kernel_cfg {
0047     unsigned int    groups;
0048     unsigned int    flags;
0049     void        (*input)(struct sk_buff *skb);
0050     struct mutex    *cb_mutex;
0051     int     (*bind)(struct net *net, int group);
0052     void        (*unbind)(struct net *net, int group);
0053     bool        (*compare)(struct net *net, struct sock *sk);
0054 };
0055 
0056 struct sock *__netlink_kernel_create(struct net *net, int unit,
0057                         struct module *module,
0058                         struct netlink_kernel_cfg *cfg);
0059 static inline struct sock *
0060 netlink_kernel_create(struct net *net, int unit, struct netlink_kernel_cfg *cfg)
0061 {
0062     return __netlink_kernel_create(net, unit, THIS_MODULE, cfg);
0063 }
0064 
0065 /* this can be increased when necessary - don't expose to userland */
0066 #define NETLINK_MAX_COOKIE_LEN  20
0067 
0068 /**
0069  * struct netlink_ext_ack - netlink extended ACK report struct
0070  * @_msg: message string to report - don't access directly, use
0071  *  %NL_SET_ERR_MSG
0072  * @bad_attr: attribute with error
0073  * @policy: policy for a bad attribute
0074  * @cookie: cookie data to return to userspace (for success)
0075  * @cookie_len: actual cookie data length
0076  */
0077 struct netlink_ext_ack {
0078     const char *_msg;
0079     const struct nlattr *bad_attr;
0080     const struct nla_policy *policy;
0081     u8 cookie[NETLINK_MAX_COOKIE_LEN];
0082     u8 cookie_len;
0083 };
0084 
0085 /* Always use this macro, this allows later putting the
0086  * message into a separate section or such for things
0087  * like translation or listing all possible messages.
0088  * Currently string formatting is not supported (due
0089  * to the lack of an output buffer.)
0090  */
0091 #define NL_SET_ERR_MSG(extack, msg) do {        \
0092     static const char __msg[] = msg;        \
0093     struct netlink_ext_ack *__extack = (extack);    \
0094                             \
0095     do_trace_netlink_extack(__msg);         \
0096                             \
0097     if (__extack)                   \
0098         __extack->_msg = __msg;         \
0099 } while (0)
0100 
0101 #define NL_SET_ERR_MSG_MOD(extack, msg)         \
0102     NL_SET_ERR_MSG((extack), KBUILD_MODNAME ": " msg)
0103 
0104 #define NL_SET_BAD_ATTR_POLICY(extack, attr, pol) do {  \
0105     if ((extack)) {                 \
0106         (extack)->bad_attr = (attr);        \
0107         (extack)->policy = (pol);       \
0108     }                       \
0109 } while (0)
0110 
0111 #define NL_SET_BAD_ATTR(extack, attr) NL_SET_BAD_ATTR_POLICY(extack, attr, NULL)
0112 
0113 #define NL_SET_ERR_MSG_ATTR_POL(extack, attr, pol, msg) do {    \
0114     static const char __msg[] = msg;            \
0115     struct netlink_ext_ack *__extack = (extack);        \
0116                                 \
0117     do_trace_netlink_extack(__msg);             \
0118                                 \
0119     if (__extack) {                     \
0120         __extack->_msg = __msg;             \
0121         __extack->bad_attr = (attr);            \
0122         __extack->policy = (pol);           \
0123     }                           \
0124 } while (0)
0125 
0126 #define NL_SET_ERR_MSG_ATTR(extack, attr, msg)      \
0127     NL_SET_ERR_MSG_ATTR_POL(extack, attr, NULL, msg)
0128 
0129 static inline void nl_set_extack_cookie_u64(struct netlink_ext_ack *extack,
0130                         u64 cookie)
0131 {
0132     if (!extack)
0133         return;
0134     memcpy(extack->cookie, &cookie, sizeof(cookie));
0135     extack->cookie_len = sizeof(cookie);
0136 }
0137 
0138 void netlink_kernel_release(struct sock *sk);
0139 int __netlink_change_ngroups(struct sock *sk, unsigned int groups);
0140 int netlink_change_ngroups(struct sock *sk, unsigned int groups);
0141 void __netlink_clear_multicast_users(struct sock *sk, unsigned int group);
0142 void netlink_ack(struct sk_buff *in_skb, struct nlmsghdr *nlh, int err,
0143          const struct netlink_ext_ack *extack);
0144 int netlink_has_listeners(struct sock *sk, unsigned int group);
0145 bool netlink_strict_get_check(struct sk_buff *skb);
0146 
0147 int netlink_unicast(struct sock *ssk, struct sk_buff *skb, __u32 portid, int nonblock);
0148 int netlink_broadcast(struct sock *ssk, struct sk_buff *skb, __u32 portid,
0149               __u32 group, gfp_t allocation);
0150 int netlink_set_err(struct sock *ssk, __u32 portid, __u32 group, int code);
0151 int netlink_register_notifier(struct notifier_block *nb);
0152 int netlink_unregister_notifier(struct notifier_block *nb);
0153 
0154 /* finegrained unicast helpers: */
0155 struct sock *netlink_getsockbyfilp(struct file *filp);
0156 int netlink_attachskb(struct sock *sk, struct sk_buff *skb,
0157               long *timeo, struct sock *ssk);
0158 void netlink_detachskb(struct sock *sk, struct sk_buff *skb);
0159 int netlink_sendskb(struct sock *sk, struct sk_buff *skb);
0160 
0161 static inline struct sk_buff *
0162 netlink_skb_clone(struct sk_buff *skb, gfp_t gfp_mask)
0163 {
0164     struct sk_buff *nskb;
0165 
0166     nskb = skb_clone(skb, gfp_mask);
0167     if (!nskb)
0168         return NULL;
0169 
0170     /* This is a large skb, set destructor callback to release head */
0171     if (is_vmalloc_addr(skb->head))
0172         nskb->destructor = skb->destructor;
0173 
0174     return nskb;
0175 }
0176 
0177 /*
0178  *  skb should fit one page. This choice is good for headerless malloc.
0179  *  But we should limit to 8K so that userspace does not have to
0180  *  use enormous buffer sizes on recvmsg() calls just to avoid
0181  *  MSG_TRUNC when PAGE_SIZE is very large.
0182  */
0183 #if PAGE_SIZE < 8192UL
0184 #define NLMSG_GOODSIZE  SKB_WITH_OVERHEAD(PAGE_SIZE)
0185 #else
0186 #define NLMSG_GOODSIZE  SKB_WITH_OVERHEAD(8192UL)
0187 #endif
0188 
0189 #define NLMSG_DEFAULT_SIZE (NLMSG_GOODSIZE - NLMSG_HDRLEN)
0190 
0191 
0192 struct netlink_callback {
0193     struct sk_buff      *skb;
0194     const struct nlmsghdr   *nlh;
0195     int         (*dump)(struct sk_buff * skb,
0196                     struct netlink_callback *cb);
0197     int         (*done)(struct netlink_callback *cb);
0198     void            *data;
0199     /* the module that dump function belong to */
0200     struct module       *module;
0201     struct netlink_ext_ack  *extack;
0202     u16         family;
0203     u16         answer_flags;
0204     u32         min_dump_alloc;
0205     unsigned int        prev_seq, seq;
0206     bool            strict_check;
0207     union {
0208         u8      ctx[48];
0209 
0210         /* args is deprecated. Cast a struct over ctx instead
0211          * for proper type safety.
0212          */
0213         long        args[6];
0214     };
0215 };
0216 
0217 struct netlink_notify {
0218     struct net *net;
0219     u32 portid;
0220     int protocol;
0221 };
0222 
0223 struct nlmsghdr *
0224 __nlmsg_put(struct sk_buff *skb, u32 portid, u32 seq, int type, int len, int flags);
0225 
0226 struct netlink_dump_control {
0227     int (*start)(struct netlink_callback *);
0228     int (*dump)(struct sk_buff *skb, struct netlink_callback *);
0229     int (*done)(struct netlink_callback *);
0230     void *data;
0231     struct module *module;
0232     u32 min_dump_alloc;
0233 };
0234 
0235 int __netlink_dump_start(struct sock *ssk, struct sk_buff *skb,
0236                 const struct nlmsghdr *nlh,
0237                 struct netlink_dump_control *control);
0238 static inline int netlink_dump_start(struct sock *ssk, struct sk_buff *skb,
0239                      const struct nlmsghdr *nlh,
0240                      struct netlink_dump_control *control)
0241 {
0242     if (!control->module)
0243         control->module = THIS_MODULE;
0244 
0245     return __netlink_dump_start(ssk, skb, nlh, control);
0246 }
0247 
0248 struct netlink_tap {
0249     struct net_device *dev;
0250     struct module *module;
0251     struct list_head list;
0252 };
0253 
0254 int netlink_add_tap(struct netlink_tap *nt);
0255 int netlink_remove_tap(struct netlink_tap *nt);
0256 
0257 bool __netlink_ns_capable(const struct netlink_skb_parms *nsp,
0258               struct user_namespace *ns, int cap);
0259 bool netlink_ns_capable(const struct sk_buff *skb,
0260             struct user_namespace *ns, int cap);
0261 bool netlink_capable(const struct sk_buff *skb, int cap);
0262 bool netlink_net_capable(const struct sk_buff *skb, int cap);
0263 
0264 #endif  /* __LINUX_NETLINK_H */