b655d243621bc8de25df273c4681c5b28c557621
[openwrt/staging/blogic.git] / backport / backport-include / net / genetlink.h
1 #ifndef __BACKPORT_NET_GENETLINK_H
2 #define __BACKPORT_NET_GENETLINK_H
3 #include_next <net/genetlink.h>
4 #include <linux/version.h>
5
6 #if LINUX_VERSION_IS_LESS(4,12,0)
7 #define GENL_SET_ERR_MSG(info, msg) do { } while (0)
8
9 static inline int genl_err_attr(struct genl_info *info, int err,
10 struct nlattr *attr)
11 {
12 #if LINUX_VERSION_IS_GEQ(4,12,0)
13 info->extack->bad_attr = attr;
14 #endif
15
16 return err;
17 }
18 #endif
19
20 /* this is for patches we apply */
21 static inline struct netlink_ext_ack *genl_info_extack(struct genl_info *info)
22 {
23 #if LINUX_VERSION_IS_GEQ(4,12,0)
24 return info->extack;
25 #else
26 return NULL;
27 #endif
28 }
29
30 /* this is for patches we apply */
31 #if LINUX_VERSION_IS_LESS(3,7,0)
32 #define genl_info_snd_portid(__genl_info) (__genl_info->snd_pid)
33 #else
34 #define genl_info_snd_portid(__genl_info) (__genl_info->snd_portid)
35 #endif
36
37 #ifndef GENLMSG_DEFAULT_SIZE
38 #define GENLMSG_DEFAULT_SIZE (NLMSG_DEFAULT_SIZE - GENL_HDRLEN)
39 #endif
40
41 #if LINUX_VERSION_IS_LESS(3,1,0)
42 #define genl_dump_check_consistent(cb, user_hdr, family)
43 #endif
44
45 #if LINUX_VERSION_IS_LESS(3,13,0) && RHEL_RELEASE_CODE < RHEL_RELEASE_VERSION(7,0)
46 static inline int __real_genl_register_family(struct genl_family *family)
47 {
48 return genl_register_family(family);
49 }
50
51 /* Needed for the mcgrps pointer */
52 struct backport_genl_family {
53 struct genl_family family;
54
55 unsigned int id, hdrsize, version, maxattr;
56 char name[GENL_NAMSIZ];
57 bool netnsok;
58 bool parallel_ops;
59
60 struct nlattr **attrbuf;
61
62 int (*pre_doit)(struct genl_ops *ops, struct sk_buff *skb,
63 struct genl_info *info);
64
65 void (*post_doit)(struct genl_ops *ops, struct sk_buff *skb,
66 struct genl_info *info);
67
68 struct genl_multicast_group *mcgrps;
69 struct genl_ops *ops;
70 unsigned int n_mcgrps, n_ops;
71
72 struct module *module;
73 };
74 #define genl_family LINUX_BACKPORT(genl_family)
75
76 int __backport_genl_register_family(struct genl_family *family);
77
78 #define genl_register_family LINUX_BACKPORT(genl_register_family)
79 static inline int
80 genl_register_family(struct genl_family *family)
81 {
82 family->module = THIS_MODULE;
83 return __backport_genl_register_family(family);
84 }
85
86 #define _genl_register_family_with_ops_grps \
87 _backport_genl_register_family_with_ops_grps
88 static inline int
89 _genl_register_family_with_ops_grps(struct genl_family *family,
90 struct genl_ops *ops, size_t n_ops,
91 struct genl_multicast_group *mcgrps,
92 size_t n_mcgrps)
93 {
94 family->ops = ops;
95 family->n_ops = n_ops;
96 family->mcgrps = mcgrps;
97 family->n_mcgrps = n_mcgrps;
98 return genl_register_family(family);
99 }
100
101 #define genl_register_family_with_ops(family, ops) \
102 _genl_register_family_with_ops_grps((family), \
103 (ops), ARRAY_SIZE(ops), \
104 NULL, 0)
105 #define genl_register_family_with_ops_groups(family, ops, grps) \
106 _genl_register_family_with_ops_grps((family), \
107 (ops), ARRAY_SIZE(ops), \
108 (grps), ARRAY_SIZE(grps))
109
110 #define genl_unregister_family backport_genl_unregister_family
111 int genl_unregister_family(struct genl_family *family);
112
113 #if LINUX_VERSION_IS_LESS(3,3,0)
114 extern void genl_notify(struct sk_buff *skb, struct net *net, u32 pid,
115 u32 group, struct nlmsghdr *nlh, gfp_t flags);
116 #endif
117 #define genl_notify(_fam, _skb, _info, _group, _flags) \
118 genl_notify(_skb, genl_info_net(_info), \
119 genl_info_snd_portid(_info), \
120 (_fam)->mcgrps[_group].id, _info->nlhdr, _flags)
121 #define genlmsg_put(_skb, _pid, _seq, _fam, _flags, _cmd) \
122 genlmsg_put(_skb, _pid, _seq, &(_fam)->family, _flags, _cmd)
123 #define genlmsg_nlhdr(_hdr, _fam) \
124 genlmsg_nlhdr(_hdr, &(_fam)->family)
125 #ifndef genl_dump_check_consistent
126 #define genl_dump_check_consistent(_cb, _hdr, _fam) \
127 genl_dump_check_consistent(_cb, _hdr, &(_fam)->family)
128 #endif
129 #ifndef genlmsg_put_reply /* might already be there from _info override above */
130 #define genlmsg_put_reply(_skb, _info, _fam, _flags, _cmd) \
131 genlmsg_put_reply(_skb, _info, &(_fam)->family, _flags, _cmd)
132 #endif
133 #define genlmsg_multicast_netns LINUX_BACKPORT(genlmsg_multicast_netns)
134 static inline int genlmsg_multicast_netns(struct genl_family *family,
135 struct net *net, struct sk_buff *skb,
136 u32 portid, unsigned int group,
137 gfp_t flags)
138 {
139 if (WARN_ON_ONCE(group >= family->n_mcgrps))
140 return -EINVAL;
141 group = family->mcgrps[group].id;
142 return nlmsg_multicast(
143 net->genl_sock,
144 skb, portid, group, flags);
145 }
146 #define genlmsg_multicast LINUX_BACKPORT(genlmsg_multicast)
147 static inline int genlmsg_multicast(struct genl_family *family,
148 struct sk_buff *skb, u32 portid,
149 unsigned int group, gfp_t flags)
150 {
151 if (WARN_ON_ONCE(group >= family->n_mcgrps))
152 return -EINVAL;
153 group = family->mcgrps[group].id;
154 return nlmsg_multicast(
155 init_net.genl_sock,
156 skb, portid, group, flags);
157 }
158 static inline int
159 backport_genlmsg_multicast_allns(struct genl_family *family,
160 struct sk_buff *skb, u32 portid,
161 unsigned int group, gfp_t flags)
162 {
163 if (WARN_ON_ONCE(group >= family->n_mcgrps))
164 return -EINVAL;
165 group = family->mcgrps[group].id;
166 return genlmsg_multicast_allns(skb, portid, group, flags);
167 }
168 #define genlmsg_multicast_allns LINUX_BACKPORT(genlmsg_multicast_allns)
169
170 #define __genl_const
171 #else /* < 3.13 */
172 #define __genl_const const
173 #if LINUX_VERSION_IS_LESS(4,4,0)
174 #define genl_notify(_fam, _skb, _info, _group, _flags) \
175 genl_notify(_fam, _skb, genl_info_net(_info), \
176 genl_info_snd_portid(_info), \
177 _group, _info->nlhdr, _flags)
178 #endif /* < 4.4 */
179 #endif /* < 3.13 */
180
181 #if LINUX_VERSION_IS_LESS(4,10,0)
182 /**
183 * genl_family_attrbuf - return family's attrbuf
184 * @family: the family
185 *
186 * Return the family's attrbuf, while validating that it's
187 * actually valid to access it.
188 *
189 * You cannot use this function with a family that has parallel_ops
190 * and you can only use it within (pre/post) doit/dumpit callbacks.
191 */
192 #define genl_family_attrbuf LINUX_BACKPORT(genl_family_attrbuf)
193 static inline struct nlattr **genl_family_attrbuf(struct genl_family *family)
194 {
195 WARN_ON(family->parallel_ops);
196
197 return family->attrbuf;
198 }
199
200 #define __genl_ro_after_init
201 #else
202 #define __genl_ro_after_init __ro_after_init
203 #endif
204
205 #endif /* __BACKPORT_NET_GENETLINK_H */