ip_set_core.c 63 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* Copyright (C) 2000-2002 Joakim Axelsson <gozem@linux.nu>
  3. * Patrick Schaaf <bof@bof.de>
  4. * Copyright (C) 2003-2013 Jozsef Kadlecsik <kadlec@netfilter.org>
  5. */
  6. /* Kernel module for IP set management */
  7. #include <linux/init.h>
  8. #include <linux/module.h>
  9. #include <linux/moduleparam.h>
  10. #include <linux/ip.h>
  11. #include <linux/skbuff.h>
  12. #include <linux/spinlock.h>
  13. #include <linux/rculist.h>
  14. #include <net/netlink.h>
  15. #include <net/net_namespace.h>
  16. #include <net/netns/generic.h>
  17. #include <linux/netfilter.h>
  18. #include <linux/netfilter/x_tables.h>
  19. #include <linux/netfilter/nfnetlink.h>
  20. #include <linux/netfilter/ipset/ip_set.h>
  21. static LIST_HEAD(ip_set_type_list); /* all registered set types */
  22. static DEFINE_MUTEX(ip_set_type_mutex); /* protects ip_set_type_list */
  23. static DEFINE_RWLOCK(ip_set_ref_lock); /* protects the set refs */
  24. struct ip_set_net {
  25. struct ip_set * __rcu *ip_set_list; /* all individual sets */
  26. ip_set_id_t ip_set_max; /* max number of sets */
  27. bool is_deleted; /* deleted by ip_set_net_exit */
  28. bool is_destroyed; /* all sets are destroyed */
  29. };
  30. static unsigned int ip_set_net_id __read_mostly;
  31. static struct ip_set_net *ip_set_pernet(struct net *net)
  32. {
  33. return net_generic(net, ip_set_net_id);
  34. }
  35. #define IP_SET_INC 64
  36. #define STRNCMP(a, b) (strncmp(a, b, IPSET_MAXNAMELEN) == 0)
  37. static unsigned int max_sets;
  38. module_param(max_sets, int, 0600);
  39. MODULE_PARM_DESC(max_sets, "maximal number of sets");
  40. MODULE_LICENSE("GPL");
  41. MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@netfilter.org>");
  42. MODULE_DESCRIPTION("core IP set support");
  43. MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_IPSET);
  44. /* When the nfnl mutex or ip_set_ref_lock is held: */
  45. #define ip_set_dereference(p) \
  46. rcu_dereference_protected(p, \
  47. lockdep_nfnl_is_held(NFNL_SUBSYS_IPSET) || \
  48. lockdep_is_held(&ip_set_ref_lock))
  49. #define ip_set(inst, id) \
  50. ip_set_dereference((inst)->ip_set_list)[id]
  51. #define ip_set_ref_netlink(inst,id) \
  52. rcu_dereference_raw((inst)->ip_set_list)[id]
  53. /* The set types are implemented in modules and registered set types
  54. * can be found in ip_set_type_list. Adding/deleting types is
  55. * serialized by ip_set_type_mutex.
  56. */
  57. static void
  58. ip_set_type_lock(void)
  59. {
  60. mutex_lock(&ip_set_type_mutex);
  61. }
  62. static void
  63. ip_set_type_unlock(void)
  64. {
  65. mutex_unlock(&ip_set_type_mutex);
  66. }
  67. /* Register and deregister settype */
  68. static struct ip_set_type *
  69. find_set_type(const char *name, u8 family, u8 revision)
  70. {
  71. struct ip_set_type *type;
  72. list_for_each_entry_rcu(type, &ip_set_type_list, list,
  73. lockdep_is_held(&ip_set_type_mutex))
  74. if (STRNCMP(type->name, name) &&
  75. (type->family == family ||
  76. type->family == NFPROTO_UNSPEC) &&
  77. revision >= type->revision_min &&
  78. revision <= type->revision_max)
  79. return type;
  80. return NULL;
  81. }
  82. /* Unlock, try to load a set type module and lock again */
  83. static bool
  84. load_settype(const char *name)
  85. {
  86. nfnl_unlock(NFNL_SUBSYS_IPSET);
  87. pr_debug("try to load ip_set_%s\n", name);
  88. if (request_module("ip_set_%s", name) < 0) {
  89. pr_warn("Can't find ip_set type %s\n", name);
  90. nfnl_lock(NFNL_SUBSYS_IPSET);
  91. return false;
  92. }
  93. nfnl_lock(NFNL_SUBSYS_IPSET);
  94. return true;
  95. }
  96. /* Find a set type and reference it */
  97. #define find_set_type_get(name, family, revision, found) \
  98. __find_set_type_get(name, family, revision, found, false)
  99. static int
  100. __find_set_type_get(const char *name, u8 family, u8 revision,
  101. struct ip_set_type **found, bool retry)
  102. {
  103. struct ip_set_type *type;
  104. int err;
  105. if (retry && !load_settype(name))
  106. return -IPSET_ERR_FIND_TYPE;
  107. rcu_read_lock();
  108. *found = find_set_type(name, family, revision);
  109. if (*found) {
  110. err = !try_module_get((*found)->me) ? -EFAULT : 0;
  111. goto unlock;
  112. }
  113. /* Make sure the type is already loaded
  114. * but we don't support the revision
  115. */
  116. list_for_each_entry_rcu(type, &ip_set_type_list, list)
  117. if (STRNCMP(type->name, name)) {
  118. err = -IPSET_ERR_FIND_TYPE;
  119. goto unlock;
  120. }
  121. rcu_read_unlock();
  122. return retry ? -IPSET_ERR_FIND_TYPE :
  123. __find_set_type_get(name, family, revision, found, true);
  124. unlock:
  125. rcu_read_unlock();
  126. return err;
  127. }
  128. /* Find a given set type by name and family.
  129. * If we succeeded, the supported minimal and maximum revisions are
  130. * filled out.
  131. */
  132. #define find_set_type_minmax(name, family, min, max) \
  133. __find_set_type_minmax(name, family, min, max, false)
  134. static int
  135. __find_set_type_minmax(const char *name, u8 family, u8 *min, u8 *max,
  136. bool retry)
  137. {
  138. struct ip_set_type *type;
  139. bool found = false;
  140. if (retry && !load_settype(name))
  141. return -IPSET_ERR_FIND_TYPE;
  142. *min = 255; *max = 0;
  143. rcu_read_lock();
  144. list_for_each_entry_rcu(type, &ip_set_type_list, list)
  145. if (STRNCMP(type->name, name) &&
  146. (type->family == family ||
  147. type->family == NFPROTO_UNSPEC)) {
  148. found = true;
  149. if (type->revision_min < *min)
  150. *min = type->revision_min;
  151. if (type->revision_max > *max)
  152. *max = type->revision_max;
  153. }
  154. rcu_read_unlock();
  155. if (found)
  156. return 0;
  157. return retry ? -IPSET_ERR_FIND_TYPE :
  158. __find_set_type_minmax(name, family, min, max, true);
  159. }
  160. #define family_name(f) ((f) == NFPROTO_IPV4 ? "inet" : \
  161. (f) == NFPROTO_IPV6 ? "inet6" : "any")
  162. /* Register a set type structure. The type is identified by
  163. * the unique triple of name, family and revision.
  164. */
  165. int
  166. ip_set_type_register(struct ip_set_type *type)
  167. {
  168. int ret = 0;
  169. if (type->protocol != IPSET_PROTOCOL) {
  170. pr_warn("ip_set type %s, family %s, revision %u:%u uses wrong protocol version %u (want %u)\n",
  171. type->name, family_name(type->family),
  172. type->revision_min, type->revision_max,
  173. type->protocol, IPSET_PROTOCOL);
  174. return -EINVAL;
  175. }
  176. ip_set_type_lock();
  177. if (find_set_type(type->name, type->family, type->revision_min)) {
  178. /* Duplicate! */
  179. pr_warn("ip_set type %s, family %s with revision min %u already registered!\n",
  180. type->name, family_name(type->family),
  181. type->revision_min);
  182. ip_set_type_unlock();
  183. return -EINVAL;
  184. }
  185. list_add_rcu(&type->list, &ip_set_type_list);
  186. pr_debug("type %s, family %s, revision %u:%u registered.\n",
  187. type->name, family_name(type->family),
  188. type->revision_min, type->revision_max);
  189. ip_set_type_unlock();
  190. return ret;
  191. }
  192. EXPORT_SYMBOL_GPL(ip_set_type_register);
  193. /* Unregister a set type. There's a small race with ip_set_create */
  194. void
  195. ip_set_type_unregister(struct ip_set_type *type)
  196. {
  197. ip_set_type_lock();
  198. if (!find_set_type(type->name, type->family, type->revision_min)) {
  199. pr_warn("ip_set type %s, family %s with revision min %u not registered\n",
  200. type->name, family_name(type->family),
  201. type->revision_min);
  202. ip_set_type_unlock();
  203. return;
  204. }
  205. list_del_rcu(&type->list);
  206. pr_debug("type %s, family %s with revision min %u unregistered.\n",
  207. type->name, family_name(type->family), type->revision_min);
  208. ip_set_type_unlock();
  209. synchronize_rcu();
  210. }
  211. EXPORT_SYMBOL_GPL(ip_set_type_unregister);
  212. /* Utility functions */
  213. void *
  214. ip_set_alloc(size_t size)
  215. {
  216. return kvzalloc(size, GFP_KERNEL_ACCOUNT);
  217. }
  218. EXPORT_SYMBOL_GPL(ip_set_alloc);
  219. void
  220. ip_set_free(void *members)
  221. {
  222. pr_debug("%p: free with %s\n", members,
  223. is_vmalloc_addr(members) ? "vfree" : "kfree");
  224. kvfree(members);
  225. }
  226. EXPORT_SYMBOL_GPL(ip_set_free);
  227. static bool
  228. flag_nested(const struct nlattr *nla)
  229. {
  230. return nla->nla_type & NLA_F_NESTED;
  231. }
  232. static const struct nla_policy ipaddr_policy[IPSET_ATTR_IPADDR_MAX + 1] = {
  233. [IPSET_ATTR_IPADDR_IPV4] = { .type = NLA_U32 },
  234. [IPSET_ATTR_IPADDR_IPV6] = NLA_POLICY_EXACT_LEN(sizeof(struct in6_addr)),
  235. };
  236. int
  237. ip_set_get_ipaddr4(struct nlattr *nla, __be32 *ipaddr)
  238. {
  239. struct nlattr *tb[IPSET_ATTR_IPADDR_MAX + 1];
  240. if (unlikely(!flag_nested(nla)))
  241. return -IPSET_ERR_PROTOCOL;
  242. if (nla_parse_nested(tb, IPSET_ATTR_IPADDR_MAX, nla,
  243. ipaddr_policy, NULL))
  244. return -IPSET_ERR_PROTOCOL;
  245. if (unlikely(!ip_set_attr_netorder(tb, IPSET_ATTR_IPADDR_IPV4)))
  246. return -IPSET_ERR_PROTOCOL;
  247. *ipaddr = nla_get_be32(tb[IPSET_ATTR_IPADDR_IPV4]);
  248. return 0;
  249. }
  250. EXPORT_SYMBOL_GPL(ip_set_get_ipaddr4);
  251. int
  252. ip_set_get_ipaddr6(struct nlattr *nla, union nf_inet_addr *ipaddr)
  253. {
  254. struct nlattr *tb[IPSET_ATTR_IPADDR_MAX + 1];
  255. if (unlikely(!flag_nested(nla)))
  256. return -IPSET_ERR_PROTOCOL;
  257. if (nla_parse_nested(tb, IPSET_ATTR_IPADDR_MAX, nla,
  258. ipaddr_policy, NULL))
  259. return -IPSET_ERR_PROTOCOL;
  260. if (unlikely(!ip_set_attr_netorder(tb, IPSET_ATTR_IPADDR_IPV6)))
  261. return -IPSET_ERR_PROTOCOL;
  262. memcpy(ipaddr, nla_data(tb[IPSET_ATTR_IPADDR_IPV6]),
  263. sizeof(struct in6_addr));
  264. return 0;
  265. }
  266. EXPORT_SYMBOL_GPL(ip_set_get_ipaddr6);
  267. static u32
  268. ip_set_timeout_get(const unsigned long *timeout)
  269. {
  270. u32 t;
  271. if (*timeout == IPSET_ELEM_PERMANENT)
  272. return 0;
  273. t = jiffies_to_msecs(*timeout - jiffies) / MSEC_PER_SEC;
  274. /* Zero value in userspace means no timeout */
  275. return t == 0 ? 1 : t;
  276. }
  277. static char *
  278. ip_set_comment_uget(struct nlattr *tb)
  279. {
  280. return nla_data(tb);
  281. }
  282. /* Called from uadd only, protected by the set spinlock.
  283. * The kadt functions don't use the comment extensions in any way.
  284. */
  285. void
  286. ip_set_init_comment(struct ip_set *set, struct ip_set_comment *comment,
  287. const struct ip_set_ext *ext)
  288. {
  289. struct ip_set_comment_rcu *c = rcu_dereference_protected(comment->c, 1);
  290. size_t len = ext->comment ? strlen(ext->comment) : 0;
  291. if (unlikely(c)) {
  292. set->ext_size -= sizeof(*c) + strlen(c->str) + 1;
  293. kfree_rcu(c, rcu);
  294. rcu_assign_pointer(comment->c, NULL);
  295. }
  296. if (!len)
  297. return;
  298. if (unlikely(len > IPSET_MAX_COMMENT_SIZE))
  299. len = IPSET_MAX_COMMENT_SIZE;
  300. c = kmalloc(sizeof(*c) + len + 1, GFP_ATOMIC);
  301. if (unlikely(!c))
  302. return;
  303. strlcpy(c->str, ext->comment, len + 1);
  304. set->ext_size += sizeof(*c) + strlen(c->str) + 1;
  305. rcu_assign_pointer(comment->c, c);
  306. }
  307. EXPORT_SYMBOL_GPL(ip_set_init_comment);
  308. /* Used only when dumping a set, protected by rcu_read_lock() */
  309. static int
  310. ip_set_put_comment(struct sk_buff *skb, const struct ip_set_comment *comment)
  311. {
  312. struct ip_set_comment_rcu *c = rcu_dereference(comment->c);
  313. if (!c)
  314. return 0;
  315. return nla_put_string(skb, IPSET_ATTR_COMMENT, c->str);
  316. }
  317. /* Called from uadd/udel, flush or the garbage collectors protected
  318. * by the set spinlock.
  319. * Called when the set is destroyed and when there can't be any user
  320. * of the set data anymore.
  321. */
  322. static void
  323. ip_set_comment_free(struct ip_set *set, void *ptr)
  324. {
  325. struct ip_set_comment *comment = ptr;
  326. struct ip_set_comment_rcu *c;
  327. c = rcu_dereference_protected(comment->c, 1);
  328. if (unlikely(!c))
  329. return;
  330. set->ext_size -= sizeof(*c) + strlen(c->str) + 1;
  331. kfree_rcu(c, rcu);
  332. rcu_assign_pointer(comment->c, NULL);
  333. }
  334. typedef void (*destroyer)(struct ip_set *, void *);
  335. /* ipset data extension types, in size order */
  336. const struct ip_set_ext_type ip_set_extensions[] = {
  337. [IPSET_EXT_ID_COUNTER] = {
  338. .type = IPSET_EXT_COUNTER,
  339. .flag = IPSET_FLAG_WITH_COUNTERS,
  340. .len = sizeof(struct ip_set_counter),
  341. .align = __alignof__(struct ip_set_counter),
  342. },
  343. [IPSET_EXT_ID_TIMEOUT] = {
  344. .type = IPSET_EXT_TIMEOUT,
  345. .len = sizeof(unsigned long),
  346. .align = __alignof__(unsigned long),
  347. },
  348. [IPSET_EXT_ID_SKBINFO] = {
  349. .type = IPSET_EXT_SKBINFO,
  350. .flag = IPSET_FLAG_WITH_SKBINFO,
  351. .len = sizeof(struct ip_set_skbinfo),
  352. .align = __alignof__(struct ip_set_skbinfo),
  353. },
  354. [IPSET_EXT_ID_COMMENT] = {
  355. .type = IPSET_EXT_COMMENT | IPSET_EXT_DESTROY,
  356. .flag = IPSET_FLAG_WITH_COMMENT,
  357. .len = sizeof(struct ip_set_comment),
  358. .align = __alignof__(struct ip_set_comment),
  359. .destroy = ip_set_comment_free,
  360. },
  361. };
  362. EXPORT_SYMBOL_GPL(ip_set_extensions);
  363. static bool
  364. add_extension(enum ip_set_ext_id id, u32 flags, struct nlattr *tb[])
  365. {
  366. return ip_set_extensions[id].flag ?
  367. (flags & ip_set_extensions[id].flag) :
  368. !!tb[IPSET_ATTR_TIMEOUT];
  369. }
  370. size_t
  371. ip_set_elem_len(struct ip_set *set, struct nlattr *tb[], size_t len,
  372. size_t align)
  373. {
  374. enum ip_set_ext_id id;
  375. u32 cadt_flags = 0;
  376. if (tb[IPSET_ATTR_CADT_FLAGS])
  377. cadt_flags = ip_set_get_h32(tb[IPSET_ATTR_CADT_FLAGS]);
  378. if (cadt_flags & IPSET_FLAG_WITH_FORCEADD)
  379. set->flags |= IPSET_CREATE_FLAG_FORCEADD;
  380. if (!align)
  381. align = 1;
  382. for (id = 0; id < IPSET_EXT_ID_MAX; id++) {
  383. if (!add_extension(id, cadt_flags, tb))
  384. continue;
  385. if (align < ip_set_extensions[id].align)
  386. align = ip_set_extensions[id].align;
  387. len = ALIGN(len, ip_set_extensions[id].align);
  388. set->offset[id] = len;
  389. set->extensions |= ip_set_extensions[id].type;
  390. len += ip_set_extensions[id].len;
  391. }
  392. return ALIGN(len, align);
  393. }
  394. EXPORT_SYMBOL_GPL(ip_set_elem_len);
  395. int
  396. ip_set_get_extensions(struct ip_set *set, struct nlattr *tb[],
  397. struct ip_set_ext *ext)
  398. {
  399. u64 fullmark;
  400. if (unlikely(!ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT) ||
  401. !ip_set_optattr_netorder(tb, IPSET_ATTR_PACKETS) ||
  402. !ip_set_optattr_netorder(tb, IPSET_ATTR_BYTES) ||
  403. !ip_set_optattr_netorder(tb, IPSET_ATTR_SKBMARK) ||
  404. !ip_set_optattr_netorder(tb, IPSET_ATTR_SKBPRIO) ||
  405. !ip_set_optattr_netorder(tb, IPSET_ATTR_SKBQUEUE)))
  406. return -IPSET_ERR_PROTOCOL;
  407. if (tb[IPSET_ATTR_TIMEOUT]) {
  408. if (!SET_WITH_TIMEOUT(set))
  409. return -IPSET_ERR_TIMEOUT;
  410. ext->timeout = ip_set_timeout_uget(tb[IPSET_ATTR_TIMEOUT]);
  411. }
  412. if (tb[IPSET_ATTR_BYTES] || tb[IPSET_ATTR_PACKETS]) {
  413. if (!SET_WITH_COUNTER(set))
  414. return -IPSET_ERR_COUNTER;
  415. if (tb[IPSET_ATTR_BYTES])
  416. ext->bytes = be64_to_cpu(nla_get_be64(
  417. tb[IPSET_ATTR_BYTES]));
  418. if (tb[IPSET_ATTR_PACKETS])
  419. ext->packets = be64_to_cpu(nla_get_be64(
  420. tb[IPSET_ATTR_PACKETS]));
  421. }
  422. if (tb[IPSET_ATTR_COMMENT]) {
  423. if (!SET_WITH_COMMENT(set))
  424. return -IPSET_ERR_COMMENT;
  425. ext->comment = ip_set_comment_uget(tb[IPSET_ATTR_COMMENT]);
  426. }
  427. if (tb[IPSET_ATTR_SKBMARK]) {
  428. if (!SET_WITH_SKBINFO(set))
  429. return -IPSET_ERR_SKBINFO;
  430. fullmark = be64_to_cpu(nla_get_be64(tb[IPSET_ATTR_SKBMARK]));
  431. ext->skbinfo.skbmark = fullmark >> 32;
  432. ext->skbinfo.skbmarkmask = fullmark & 0xffffffff;
  433. }
  434. if (tb[IPSET_ATTR_SKBPRIO]) {
  435. if (!SET_WITH_SKBINFO(set))
  436. return -IPSET_ERR_SKBINFO;
  437. ext->skbinfo.skbprio =
  438. be32_to_cpu(nla_get_be32(tb[IPSET_ATTR_SKBPRIO]));
  439. }
  440. if (tb[IPSET_ATTR_SKBQUEUE]) {
  441. if (!SET_WITH_SKBINFO(set))
  442. return -IPSET_ERR_SKBINFO;
  443. ext->skbinfo.skbqueue =
  444. be16_to_cpu(nla_get_be16(tb[IPSET_ATTR_SKBQUEUE]));
  445. }
  446. return 0;
  447. }
  448. EXPORT_SYMBOL_GPL(ip_set_get_extensions);
  449. static u64
  450. ip_set_get_bytes(const struct ip_set_counter *counter)
  451. {
  452. return (u64)atomic64_read(&(counter)->bytes);
  453. }
  454. static u64
  455. ip_set_get_packets(const struct ip_set_counter *counter)
  456. {
  457. return (u64)atomic64_read(&(counter)->packets);
  458. }
  459. static bool
  460. ip_set_put_counter(struct sk_buff *skb, const struct ip_set_counter *counter)
  461. {
  462. return nla_put_net64(skb, IPSET_ATTR_BYTES,
  463. cpu_to_be64(ip_set_get_bytes(counter)),
  464. IPSET_ATTR_PAD) ||
  465. nla_put_net64(skb, IPSET_ATTR_PACKETS,
  466. cpu_to_be64(ip_set_get_packets(counter)),
  467. IPSET_ATTR_PAD);
  468. }
  469. static bool
  470. ip_set_put_skbinfo(struct sk_buff *skb, const struct ip_set_skbinfo *skbinfo)
  471. {
  472. /* Send nonzero parameters only */
  473. return ((skbinfo->skbmark || skbinfo->skbmarkmask) &&
  474. nla_put_net64(skb, IPSET_ATTR_SKBMARK,
  475. cpu_to_be64((u64)skbinfo->skbmark << 32 |
  476. skbinfo->skbmarkmask),
  477. IPSET_ATTR_PAD)) ||
  478. (skbinfo->skbprio &&
  479. nla_put_net32(skb, IPSET_ATTR_SKBPRIO,
  480. cpu_to_be32(skbinfo->skbprio))) ||
  481. (skbinfo->skbqueue &&
  482. nla_put_net16(skb, IPSET_ATTR_SKBQUEUE,
  483. cpu_to_be16(skbinfo->skbqueue)));
  484. }
  485. int
  486. ip_set_put_extensions(struct sk_buff *skb, const struct ip_set *set,
  487. const void *e, bool active)
  488. {
  489. if (SET_WITH_TIMEOUT(set)) {
  490. unsigned long *timeout = ext_timeout(e, set);
  491. if (nla_put_net32(skb, IPSET_ATTR_TIMEOUT,
  492. htonl(active ? ip_set_timeout_get(timeout)
  493. : *timeout)))
  494. return -EMSGSIZE;
  495. }
  496. if (SET_WITH_COUNTER(set) &&
  497. ip_set_put_counter(skb, ext_counter(e, set)))
  498. return -EMSGSIZE;
  499. if (SET_WITH_COMMENT(set) &&
  500. ip_set_put_comment(skb, ext_comment(e, set)))
  501. return -EMSGSIZE;
  502. if (SET_WITH_SKBINFO(set) &&
  503. ip_set_put_skbinfo(skb, ext_skbinfo(e, set)))
  504. return -EMSGSIZE;
  505. return 0;
  506. }
  507. EXPORT_SYMBOL_GPL(ip_set_put_extensions);
  508. static bool
  509. ip_set_match_counter(u64 counter, u64 match, u8 op)
  510. {
  511. switch (op) {
  512. case IPSET_COUNTER_NONE:
  513. return true;
  514. case IPSET_COUNTER_EQ:
  515. return counter == match;
  516. case IPSET_COUNTER_NE:
  517. return counter != match;
  518. case IPSET_COUNTER_LT:
  519. return counter < match;
  520. case IPSET_COUNTER_GT:
  521. return counter > match;
  522. }
  523. return false;
  524. }
  525. static void
  526. ip_set_add_bytes(u64 bytes, struct ip_set_counter *counter)
  527. {
  528. atomic64_add((long long)bytes, &(counter)->bytes);
  529. }
  530. static void
  531. ip_set_add_packets(u64 packets, struct ip_set_counter *counter)
  532. {
  533. atomic64_add((long long)packets, &(counter)->packets);
  534. }
  535. static void
  536. ip_set_update_counter(struct ip_set_counter *counter,
  537. const struct ip_set_ext *ext, u32 flags)
  538. {
  539. if (ext->packets != ULLONG_MAX &&
  540. !(flags & IPSET_FLAG_SKIP_COUNTER_UPDATE)) {
  541. ip_set_add_bytes(ext->bytes, counter);
  542. ip_set_add_packets(ext->packets, counter);
  543. }
  544. }
  545. static void
  546. ip_set_get_skbinfo(struct ip_set_skbinfo *skbinfo,
  547. const struct ip_set_ext *ext,
  548. struct ip_set_ext *mext, u32 flags)
  549. {
  550. mext->skbinfo = *skbinfo;
  551. }
  552. bool
  553. ip_set_match_extensions(struct ip_set *set, const struct ip_set_ext *ext,
  554. struct ip_set_ext *mext, u32 flags, void *data)
  555. {
  556. if (SET_WITH_TIMEOUT(set) &&
  557. ip_set_timeout_expired(ext_timeout(data, set)))
  558. return false;
  559. if (SET_WITH_COUNTER(set)) {
  560. struct ip_set_counter *counter = ext_counter(data, set);
  561. ip_set_update_counter(counter, ext, flags);
  562. if (flags & IPSET_FLAG_MATCH_COUNTERS &&
  563. !(ip_set_match_counter(ip_set_get_packets(counter),
  564. mext->packets, mext->packets_op) &&
  565. ip_set_match_counter(ip_set_get_bytes(counter),
  566. mext->bytes, mext->bytes_op)))
  567. return false;
  568. }
  569. if (SET_WITH_SKBINFO(set))
  570. ip_set_get_skbinfo(ext_skbinfo(data, set),
  571. ext, mext, flags);
  572. return true;
  573. }
  574. EXPORT_SYMBOL_GPL(ip_set_match_extensions);
  575. /* Creating/destroying/renaming/swapping affect the existence and
  576. * the properties of a set. All of these can be executed from userspace
  577. * only and serialized by the nfnl mutex indirectly from nfnetlink.
  578. *
  579. * Sets are identified by their index in ip_set_list and the index
  580. * is used by the external references (set/SET netfilter modules).
  581. *
  582. * The set behind an index may change by swapping only, from userspace.
  583. */
  584. static void
  585. __ip_set_get(struct ip_set *set)
  586. {
  587. write_lock_bh(&ip_set_ref_lock);
  588. set->ref++;
  589. write_unlock_bh(&ip_set_ref_lock);
  590. }
  591. static void
  592. __ip_set_put(struct ip_set *set)
  593. {
  594. write_lock_bh(&ip_set_ref_lock);
  595. BUG_ON(set->ref == 0);
  596. set->ref--;
  597. write_unlock_bh(&ip_set_ref_lock);
  598. }
  599. /* set->ref can be swapped out by ip_set_swap, netlink events (like dump) need
  600. * a separate reference counter
  601. */
  602. static void
  603. __ip_set_put_netlink(struct ip_set *set)
  604. {
  605. write_lock_bh(&ip_set_ref_lock);
  606. BUG_ON(set->ref_netlink == 0);
  607. set->ref_netlink--;
  608. write_unlock_bh(&ip_set_ref_lock);
  609. }
  610. /* Add, del and test set entries from kernel.
  611. *
  612. * The set behind the index must exist and must be referenced
  613. * so it can't be destroyed (or changed) under our foot.
  614. */
  615. static struct ip_set *
  616. ip_set_rcu_get(struct net *net, ip_set_id_t index)
  617. {
  618. struct ip_set *set;
  619. struct ip_set_net *inst = ip_set_pernet(net);
  620. rcu_read_lock();
  621. /* ip_set_list itself needs to be protected */
  622. set = rcu_dereference(inst->ip_set_list)[index];
  623. rcu_read_unlock();
  624. return set;
  625. }
  626. static inline void
  627. ip_set_lock(struct ip_set *set)
  628. {
  629. if (!set->variant->region_lock)
  630. spin_lock_bh(&set->lock);
  631. }
  632. static inline void
  633. ip_set_unlock(struct ip_set *set)
  634. {
  635. if (!set->variant->region_lock)
  636. spin_unlock_bh(&set->lock);
  637. }
  638. int
  639. ip_set_test(ip_set_id_t index, const struct sk_buff *skb,
  640. const struct xt_action_param *par, struct ip_set_adt_opt *opt)
  641. {
  642. struct ip_set *set = ip_set_rcu_get(xt_net(par), index);
  643. int ret = 0;
  644. BUG_ON(!set);
  645. pr_debug("set %s, index %u\n", set->name, index);
  646. if (opt->dim < set->type->dimension ||
  647. !(opt->family == set->family || set->family == NFPROTO_UNSPEC))
  648. return 0;
  649. rcu_read_lock_bh();
  650. ret = set->variant->kadt(set, skb, par, IPSET_TEST, opt);
  651. rcu_read_unlock_bh();
  652. if (ret == -EAGAIN) {
  653. /* Type requests element to be completed */
  654. pr_debug("element must be completed, ADD is triggered\n");
  655. ip_set_lock(set);
  656. set->variant->kadt(set, skb, par, IPSET_ADD, opt);
  657. ip_set_unlock(set);
  658. ret = 1;
  659. } else {
  660. /* --return-nomatch: invert matched element */
  661. if ((opt->cmdflags & IPSET_FLAG_RETURN_NOMATCH) &&
  662. (set->type->features & IPSET_TYPE_NOMATCH) &&
  663. (ret > 0 || ret == -ENOTEMPTY))
  664. ret = -ret;
  665. }
  666. /* Convert error codes to nomatch */
  667. return (ret < 0 ? 0 : ret);
  668. }
  669. EXPORT_SYMBOL_GPL(ip_set_test);
  670. int
  671. ip_set_add(ip_set_id_t index, const struct sk_buff *skb,
  672. const struct xt_action_param *par, struct ip_set_adt_opt *opt)
  673. {
  674. struct ip_set *set = ip_set_rcu_get(xt_net(par), index);
  675. int ret;
  676. BUG_ON(!set);
  677. pr_debug("set %s, index %u\n", set->name, index);
  678. if (opt->dim < set->type->dimension ||
  679. !(opt->family == set->family || set->family == NFPROTO_UNSPEC))
  680. return -IPSET_ERR_TYPE_MISMATCH;
  681. ip_set_lock(set);
  682. ret = set->variant->kadt(set, skb, par, IPSET_ADD, opt);
  683. ip_set_unlock(set);
  684. return ret;
  685. }
  686. EXPORT_SYMBOL_GPL(ip_set_add);
  687. int
  688. ip_set_del(ip_set_id_t index, const struct sk_buff *skb,
  689. const struct xt_action_param *par, struct ip_set_adt_opt *opt)
  690. {
  691. struct ip_set *set = ip_set_rcu_get(xt_net(par), index);
  692. int ret = 0;
  693. BUG_ON(!set);
  694. pr_debug("set %s, index %u\n", set->name, index);
  695. if (opt->dim < set->type->dimension ||
  696. !(opt->family == set->family || set->family == NFPROTO_UNSPEC))
  697. return -IPSET_ERR_TYPE_MISMATCH;
  698. ip_set_lock(set);
  699. ret = set->variant->kadt(set, skb, par, IPSET_DEL, opt);
  700. ip_set_unlock(set);
  701. return ret;
  702. }
  703. EXPORT_SYMBOL_GPL(ip_set_del);
  704. /* Find set by name, reference it once. The reference makes sure the
  705. * thing pointed to, does not go away under our feet.
  706. *
  707. */
  708. ip_set_id_t
  709. ip_set_get_byname(struct net *net, const char *name, struct ip_set **set)
  710. {
  711. ip_set_id_t i, index = IPSET_INVALID_ID;
  712. struct ip_set *s;
  713. struct ip_set_net *inst = ip_set_pernet(net);
  714. rcu_read_lock();
  715. for (i = 0; i < inst->ip_set_max; i++) {
  716. s = rcu_dereference(inst->ip_set_list)[i];
  717. if (s && STRNCMP(s->name, name)) {
  718. __ip_set_get(s);
  719. index = i;
  720. *set = s;
  721. break;
  722. }
  723. }
  724. rcu_read_unlock();
  725. return index;
  726. }
  727. EXPORT_SYMBOL_GPL(ip_set_get_byname);
  728. /* If the given set pointer points to a valid set, decrement
  729. * reference count by 1. The caller shall not assume the index
  730. * to be valid, after calling this function.
  731. *
  732. */
  733. static void
  734. __ip_set_put_byindex(struct ip_set_net *inst, ip_set_id_t index)
  735. {
  736. struct ip_set *set;
  737. rcu_read_lock();
  738. set = rcu_dereference(inst->ip_set_list)[index];
  739. if (set)
  740. __ip_set_put(set);
  741. rcu_read_unlock();
  742. }
  743. void
  744. ip_set_put_byindex(struct net *net, ip_set_id_t index)
  745. {
  746. struct ip_set_net *inst = ip_set_pernet(net);
  747. __ip_set_put_byindex(inst, index);
  748. }
  749. EXPORT_SYMBOL_GPL(ip_set_put_byindex);
  750. /* Get the name of a set behind a set index.
  751. * Set itself is protected by RCU, but its name isn't: to protect against
  752. * renaming, grab ip_set_ref_lock as reader (see ip_set_rename()) and copy the
  753. * name.
  754. */
  755. void
  756. ip_set_name_byindex(struct net *net, ip_set_id_t index, char *name)
  757. {
  758. struct ip_set *set = ip_set_rcu_get(net, index);
  759. BUG_ON(!set);
  760. read_lock_bh(&ip_set_ref_lock);
  761. strncpy(name, set->name, IPSET_MAXNAMELEN);
  762. read_unlock_bh(&ip_set_ref_lock);
  763. }
  764. EXPORT_SYMBOL_GPL(ip_set_name_byindex);
  765. /* Routines to call by external subsystems, which do not
  766. * call nfnl_lock for us.
  767. */
  768. /* Find set by index, reference it once. The reference makes sure the
  769. * thing pointed to, does not go away under our feet.
  770. *
  771. * The nfnl mutex is used in the function.
  772. */
  773. ip_set_id_t
  774. ip_set_nfnl_get_byindex(struct net *net, ip_set_id_t index)
  775. {
  776. struct ip_set *set;
  777. struct ip_set_net *inst = ip_set_pernet(net);
  778. if (index >= inst->ip_set_max)
  779. return IPSET_INVALID_ID;
  780. nfnl_lock(NFNL_SUBSYS_IPSET);
  781. set = ip_set(inst, index);
  782. if (set)
  783. __ip_set_get(set);
  784. else
  785. index = IPSET_INVALID_ID;
  786. nfnl_unlock(NFNL_SUBSYS_IPSET);
  787. return index;
  788. }
  789. EXPORT_SYMBOL_GPL(ip_set_nfnl_get_byindex);
  790. /* If the given set pointer points to a valid set, decrement
  791. * reference count by 1. The caller shall not assume the index
  792. * to be valid, after calling this function.
  793. *
  794. * The nfnl mutex is used in the function.
  795. */
  796. void
  797. ip_set_nfnl_put(struct net *net, ip_set_id_t index)
  798. {
  799. struct ip_set *set;
  800. struct ip_set_net *inst = ip_set_pernet(net);
  801. nfnl_lock(NFNL_SUBSYS_IPSET);
  802. if (!inst->is_deleted) { /* already deleted from ip_set_net_exit() */
  803. set = ip_set(inst, index);
  804. if (set)
  805. __ip_set_put(set);
  806. }
  807. nfnl_unlock(NFNL_SUBSYS_IPSET);
  808. }
  809. EXPORT_SYMBOL_GPL(ip_set_nfnl_put);
  810. /* Communication protocol with userspace over netlink.
  811. *
  812. * The commands are serialized by the nfnl mutex.
  813. */
  814. static inline u8 protocol(const struct nlattr * const tb[])
  815. {
  816. return nla_get_u8(tb[IPSET_ATTR_PROTOCOL]);
  817. }
  818. static inline bool
  819. protocol_failed(const struct nlattr * const tb[])
  820. {
  821. return !tb[IPSET_ATTR_PROTOCOL] || protocol(tb) != IPSET_PROTOCOL;
  822. }
  823. static inline bool
  824. protocol_min_failed(const struct nlattr * const tb[])
  825. {
  826. return !tb[IPSET_ATTR_PROTOCOL] || protocol(tb) < IPSET_PROTOCOL_MIN;
  827. }
  828. static inline u32
  829. flag_exist(const struct nlmsghdr *nlh)
  830. {
  831. return nlh->nlmsg_flags & NLM_F_EXCL ? 0 : IPSET_FLAG_EXIST;
  832. }
  833. static struct nlmsghdr *
  834. start_msg(struct sk_buff *skb, u32 portid, u32 seq, unsigned int flags,
  835. enum ipset_cmd cmd)
  836. {
  837. struct nlmsghdr *nlh;
  838. struct nfgenmsg *nfmsg;
  839. nlh = nlmsg_put(skb, portid, seq, nfnl_msg_type(NFNL_SUBSYS_IPSET, cmd),
  840. sizeof(*nfmsg), flags);
  841. if (!nlh)
  842. return NULL;
  843. nfmsg = nlmsg_data(nlh);
  844. nfmsg->nfgen_family = NFPROTO_IPV4;
  845. nfmsg->version = NFNETLINK_V0;
  846. nfmsg->res_id = 0;
  847. return nlh;
  848. }
  849. /* Create a set */
  850. static const struct nla_policy ip_set_create_policy[IPSET_ATTR_CMD_MAX + 1] = {
  851. [IPSET_ATTR_PROTOCOL] = { .type = NLA_U8 },
  852. [IPSET_ATTR_SETNAME] = { .type = NLA_NUL_STRING,
  853. .len = IPSET_MAXNAMELEN - 1 },
  854. [IPSET_ATTR_TYPENAME] = { .type = NLA_NUL_STRING,
  855. .len = IPSET_MAXNAMELEN - 1},
  856. [IPSET_ATTR_REVISION] = { .type = NLA_U8 },
  857. [IPSET_ATTR_FAMILY] = { .type = NLA_U8 },
  858. [IPSET_ATTR_DATA] = { .type = NLA_NESTED },
  859. };
  860. static struct ip_set *
  861. find_set_and_id(struct ip_set_net *inst, const char *name, ip_set_id_t *id)
  862. {
  863. struct ip_set *set = NULL;
  864. ip_set_id_t i;
  865. *id = IPSET_INVALID_ID;
  866. for (i = 0; i < inst->ip_set_max; i++) {
  867. set = ip_set(inst, i);
  868. if (set && STRNCMP(set->name, name)) {
  869. *id = i;
  870. break;
  871. }
  872. }
  873. return (*id == IPSET_INVALID_ID ? NULL : set);
  874. }
  875. static inline struct ip_set *
  876. find_set(struct ip_set_net *inst, const char *name)
  877. {
  878. ip_set_id_t id;
  879. return find_set_and_id(inst, name, &id);
  880. }
  881. static int
  882. find_free_id(struct ip_set_net *inst, const char *name, ip_set_id_t *index,
  883. struct ip_set **set)
  884. {
  885. struct ip_set *s;
  886. ip_set_id_t i;
  887. *index = IPSET_INVALID_ID;
  888. for (i = 0; i < inst->ip_set_max; i++) {
  889. s = ip_set(inst, i);
  890. if (!s) {
  891. if (*index == IPSET_INVALID_ID)
  892. *index = i;
  893. } else if (STRNCMP(name, s->name)) {
  894. /* Name clash */
  895. *set = s;
  896. return -EEXIST;
  897. }
  898. }
  899. if (*index == IPSET_INVALID_ID)
  900. /* No free slot remained */
  901. return -IPSET_ERR_MAX_SETS;
  902. return 0;
  903. }
  904. static int ip_set_none(struct net *net, struct sock *ctnl, struct sk_buff *skb,
  905. const struct nlmsghdr *nlh,
  906. const struct nlattr * const attr[],
  907. struct netlink_ext_ack *extack)
  908. {
  909. return -EOPNOTSUPP;
  910. }
  911. static int ip_set_create(struct net *net, struct sock *ctnl,
  912. struct sk_buff *skb, const struct nlmsghdr *nlh,
  913. const struct nlattr * const attr[],
  914. struct netlink_ext_ack *extack)
  915. {
  916. struct ip_set_net *inst = ip_set_pernet(net);
  917. struct ip_set *set, *clash = NULL;
  918. ip_set_id_t index = IPSET_INVALID_ID;
  919. struct nlattr *tb[IPSET_ATTR_CREATE_MAX + 1] = {};
  920. const char *name, *typename;
  921. u8 family, revision;
  922. u32 flags = flag_exist(nlh);
  923. int ret = 0;
  924. if (unlikely(protocol_min_failed(attr) ||
  925. !attr[IPSET_ATTR_SETNAME] ||
  926. !attr[IPSET_ATTR_TYPENAME] ||
  927. !attr[IPSET_ATTR_REVISION] ||
  928. !attr[IPSET_ATTR_FAMILY] ||
  929. (attr[IPSET_ATTR_DATA] &&
  930. !flag_nested(attr[IPSET_ATTR_DATA]))))
  931. return -IPSET_ERR_PROTOCOL;
  932. name = nla_data(attr[IPSET_ATTR_SETNAME]);
  933. typename = nla_data(attr[IPSET_ATTR_TYPENAME]);
  934. family = nla_get_u8(attr[IPSET_ATTR_FAMILY]);
  935. revision = nla_get_u8(attr[IPSET_ATTR_REVISION]);
  936. pr_debug("setname: %s, typename: %s, family: %s, revision: %u\n",
  937. name, typename, family_name(family), revision);
  938. /* First, and without any locks, allocate and initialize
  939. * a normal base set structure.
  940. */
  941. set = kzalloc(sizeof(*set), GFP_KERNEL);
  942. if (!set)
  943. return -ENOMEM;
  944. spin_lock_init(&set->lock);
  945. strlcpy(set->name, name, IPSET_MAXNAMELEN);
  946. set->family = family;
  947. set->revision = revision;
  948. /* Next, check that we know the type, and take
  949. * a reference on the type, to make sure it stays available
  950. * while constructing our new set.
  951. *
  952. * After referencing the type, we try to create the type
  953. * specific part of the set without holding any locks.
  954. */
  955. ret = find_set_type_get(typename, family, revision, &set->type);
  956. if (ret)
  957. goto out;
  958. /* Without holding any locks, create private part. */
  959. if (attr[IPSET_ATTR_DATA] &&
  960. nla_parse_nested(tb, IPSET_ATTR_CREATE_MAX, attr[IPSET_ATTR_DATA],
  961. set->type->create_policy, NULL)) {
  962. ret = -IPSET_ERR_PROTOCOL;
  963. goto put_out;
  964. }
  965. ret = set->type->create(net, set, tb, flags);
  966. if (ret != 0)
  967. goto put_out;
  968. /* BTW, ret==0 here. */
  969. /* Here, we have a valid, constructed set and we are protected
  970. * by the nfnl mutex. Find the first free index in ip_set_list
  971. * and check clashing.
  972. */
  973. ret = find_free_id(inst, set->name, &index, &clash);
  974. if (ret == -EEXIST) {
  975. /* If this is the same set and requested, ignore error */
  976. if ((flags & IPSET_FLAG_EXIST) &&
  977. STRNCMP(set->type->name, clash->type->name) &&
  978. set->type->family == clash->type->family &&
  979. set->type->revision_min == clash->type->revision_min &&
  980. set->type->revision_max == clash->type->revision_max &&
  981. set->variant->same_set(set, clash))
  982. ret = 0;
  983. goto cleanup;
  984. } else if (ret == -IPSET_ERR_MAX_SETS) {
  985. struct ip_set **list, **tmp;
  986. ip_set_id_t i = inst->ip_set_max + IP_SET_INC;
  987. if (i < inst->ip_set_max || i == IPSET_INVALID_ID)
  988. /* Wraparound */
  989. goto cleanup;
  990. list = kvcalloc(i, sizeof(struct ip_set *), GFP_KERNEL);
  991. if (!list)
  992. goto cleanup;
  993. /* nfnl mutex is held, both lists are valid */
  994. tmp = ip_set_dereference(inst->ip_set_list);
  995. memcpy(list, tmp, sizeof(struct ip_set *) * inst->ip_set_max);
  996. rcu_assign_pointer(inst->ip_set_list, list);
  997. /* Make sure all current packets have passed through */
  998. synchronize_net();
  999. /* Use new list */
  1000. index = inst->ip_set_max;
  1001. inst->ip_set_max = i;
  1002. kvfree(tmp);
  1003. ret = 0;
  1004. } else if (ret) {
  1005. goto cleanup;
  1006. }
  1007. /* Finally! Add our shiny new set to the list, and be done. */
  1008. pr_debug("create: '%s' created with index %u!\n", set->name, index);
  1009. ip_set(inst, index) = set;
  1010. return ret;
  1011. cleanup:
  1012. set->variant->destroy(set);
  1013. put_out:
  1014. module_put(set->type->me);
  1015. out:
  1016. kfree(set);
  1017. return ret;
  1018. }
  1019. /* Destroy sets */
  1020. static const struct nla_policy
  1021. ip_set_setname_policy[IPSET_ATTR_CMD_MAX + 1] = {
  1022. [IPSET_ATTR_PROTOCOL] = { .type = NLA_U8 },
  1023. [IPSET_ATTR_SETNAME] = { .type = NLA_NUL_STRING,
  1024. .len = IPSET_MAXNAMELEN - 1 },
  1025. };
  1026. static void
  1027. ip_set_destroy_set(struct ip_set *set)
  1028. {
  1029. pr_debug("set: %s\n", set->name);
  1030. /* Must call it without holding any lock */
  1031. set->variant->destroy(set);
  1032. module_put(set->type->me);
  1033. kfree(set);
  1034. }
  1035. static int ip_set_destroy(struct net *net, struct sock *ctnl,
  1036. struct sk_buff *skb, const struct nlmsghdr *nlh,
  1037. const struct nlattr * const attr[],
  1038. struct netlink_ext_ack *extack)
  1039. {
  1040. struct ip_set_net *inst = ip_set_pernet(net);
  1041. struct ip_set *s;
  1042. ip_set_id_t i;
  1043. int ret = 0;
  1044. if (unlikely(protocol_min_failed(attr)))
  1045. return -IPSET_ERR_PROTOCOL;
  1046. /* Must wait for flush to be really finished in list:set */
  1047. rcu_barrier();
  1048. /* Commands are serialized and references are
  1049. * protected by the ip_set_ref_lock.
  1050. * External systems (i.e. xt_set) must call
  1051. * ip_set_put|get_nfnl_* functions, that way we
  1052. * can safely check references here.
  1053. *
  1054. * list:set timer can only decrement the reference
  1055. * counter, so if it's already zero, we can proceed
  1056. * without holding the lock.
  1057. */
  1058. read_lock_bh(&ip_set_ref_lock);
  1059. if (!attr[IPSET_ATTR_SETNAME]) {
  1060. for (i = 0; i < inst->ip_set_max; i++) {
  1061. s = ip_set(inst, i);
  1062. if (s && (s->ref || s->ref_netlink)) {
  1063. ret = -IPSET_ERR_BUSY;
  1064. goto out;
  1065. }
  1066. }
  1067. inst->is_destroyed = true;
  1068. read_unlock_bh(&ip_set_ref_lock);
  1069. for (i = 0; i < inst->ip_set_max; i++) {
  1070. s = ip_set(inst, i);
  1071. if (s) {
  1072. ip_set(inst, i) = NULL;
  1073. ip_set_destroy_set(s);
  1074. }
  1075. }
  1076. /* Modified by ip_set_destroy() only, which is serialized */
  1077. inst->is_destroyed = false;
  1078. } else {
  1079. s = find_set_and_id(inst, nla_data(attr[IPSET_ATTR_SETNAME]),
  1080. &i);
  1081. if (!s) {
  1082. ret = -ENOENT;
  1083. goto out;
  1084. } else if (s->ref || s->ref_netlink) {
  1085. ret = -IPSET_ERR_BUSY;
  1086. goto out;
  1087. }
  1088. ip_set(inst, i) = NULL;
  1089. read_unlock_bh(&ip_set_ref_lock);
  1090. ip_set_destroy_set(s);
  1091. }
  1092. return 0;
  1093. out:
  1094. read_unlock_bh(&ip_set_ref_lock);
  1095. return ret;
  1096. }
  1097. /* Flush sets */
  1098. static void
  1099. ip_set_flush_set(struct ip_set *set)
  1100. {
  1101. pr_debug("set: %s\n", set->name);
  1102. ip_set_lock(set);
  1103. set->variant->flush(set);
  1104. ip_set_unlock(set);
  1105. }
  1106. static int ip_set_flush(struct net *net, struct sock *ctnl, struct sk_buff *skb,
  1107. const struct nlmsghdr *nlh,
  1108. const struct nlattr * const attr[],
  1109. struct netlink_ext_ack *extack)
  1110. {
  1111. struct ip_set_net *inst = ip_set_pernet(net);
  1112. struct ip_set *s;
  1113. ip_set_id_t i;
  1114. if (unlikely(protocol_min_failed(attr)))
  1115. return -IPSET_ERR_PROTOCOL;
  1116. if (!attr[IPSET_ATTR_SETNAME]) {
  1117. for (i = 0; i < inst->ip_set_max; i++) {
  1118. s = ip_set(inst, i);
  1119. if (s)
  1120. ip_set_flush_set(s);
  1121. }
  1122. } else {
  1123. s = find_set(inst, nla_data(attr[IPSET_ATTR_SETNAME]));
  1124. if (!s)
  1125. return -ENOENT;
  1126. ip_set_flush_set(s);
  1127. }
  1128. return 0;
  1129. }
  1130. /* Rename a set */
  1131. static const struct nla_policy
  1132. ip_set_setname2_policy[IPSET_ATTR_CMD_MAX + 1] = {
  1133. [IPSET_ATTR_PROTOCOL] = { .type = NLA_U8 },
  1134. [IPSET_ATTR_SETNAME] = { .type = NLA_NUL_STRING,
  1135. .len = IPSET_MAXNAMELEN - 1 },
  1136. [IPSET_ATTR_SETNAME2] = { .type = NLA_NUL_STRING,
  1137. .len = IPSET_MAXNAMELEN - 1 },
  1138. };
  1139. static int ip_set_rename(struct net *net, struct sock *ctnl,
  1140. struct sk_buff *skb, const struct nlmsghdr *nlh,
  1141. const struct nlattr * const attr[],
  1142. struct netlink_ext_ack *extack)
  1143. {
  1144. struct ip_set_net *inst = ip_set_pernet(net);
  1145. struct ip_set *set, *s;
  1146. const char *name2;
  1147. ip_set_id_t i;
  1148. int ret = 0;
  1149. if (unlikely(protocol_min_failed(attr) ||
  1150. !attr[IPSET_ATTR_SETNAME] ||
  1151. !attr[IPSET_ATTR_SETNAME2]))
  1152. return -IPSET_ERR_PROTOCOL;
  1153. set = find_set(inst, nla_data(attr[IPSET_ATTR_SETNAME]));
  1154. if (!set)
  1155. return -ENOENT;
  1156. write_lock_bh(&ip_set_ref_lock);
  1157. if (set->ref != 0 || set->ref_netlink != 0) {
  1158. ret = -IPSET_ERR_REFERENCED;
  1159. goto out;
  1160. }
  1161. name2 = nla_data(attr[IPSET_ATTR_SETNAME2]);
  1162. for (i = 0; i < inst->ip_set_max; i++) {
  1163. s = ip_set(inst, i);
  1164. if (s && STRNCMP(s->name, name2)) {
  1165. ret = -IPSET_ERR_EXIST_SETNAME2;
  1166. goto out;
  1167. }
  1168. }
  1169. strncpy(set->name, name2, IPSET_MAXNAMELEN);
  1170. out:
  1171. write_unlock_bh(&ip_set_ref_lock);
  1172. return ret;
  1173. }
  1174. /* Swap two sets so that name/index points to the other.
  1175. * References and set names are also swapped.
  1176. *
  1177. * The commands are serialized by the nfnl mutex and references are
  1178. * protected by the ip_set_ref_lock. The kernel interfaces
  1179. * do not hold the mutex but the pointer settings are atomic
  1180. * so the ip_set_list always contains valid pointers to the sets.
  1181. */
  1182. static int ip_set_swap(struct net *net, struct sock *ctnl, struct sk_buff *skb,
  1183. const struct nlmsghdr *nlh,
  1184. const struct nlattr * const attr[],
  1185. struct netlink_ext_ack *extack)
  1186. {
  1187. struct ip_set_net *inst = ip_set_pernet(net);
  1188. struct ip_set *from, *to;
  1189. ip_set_id_t from_id, to_id;
  1190. char from_name[IPSET_MAXNAMELEN];
  1191. if (unlikely(protocol_min_failed(attr) ||
  1192. !attr[IPSET_ATTR_SETNAME] ||
  1193. !attr[IPSET_ATTR_SETNAME2]))
  1194. return -IPSET_ERR_PROTOCOL;
  1195. from = find_set_and_id(inst, nla_data(attr[IPSET_ATTR_SETNAME]),
  1196. &from_id);
  1197. if (!from)
  1198. return -ENOENT;
  1199. to = find_set_and_id(inst, nla_data(attr[IPSET_ATTR_SETNAME2]),
  1200. &to_id);
  1201. if (!to)
  1202. return -IPSET_ERR_EXIST_SETNAME2;
  1203. /* Features must not change.
  1204. * Not an artifical restriction anymore, as we must prevent
  1205. * possible loops created by swapping in setlist type of sets.
  1206. */
  1207. if (!(from->type->features == to->type->features &&
  1208. from->family == to->family))
  1209. return -IPSET_ERR_TYPE_MISMATCH;
  1210. write_lock_bh(&ip_set_ref_lock);
  1211. if (from->ref_netlink || to->ref_netlink) {
  1212. write_unlock_bh(&ip_set_ref_lock);
  1213. return -EBUSY;
  1214. }
  1215. strncpy(from_name, from->name, IPSET_MAXNAMELEN);
  1216. strncpy(from->name, to->name, IPSET_MAXNAMELEN);
  1217. strncpy(to->name, from_name, IPSET_MAXNAMELEN);
  1218. swap(from->ref, to->ref);
  1219. ip_set(inst, from_id) = to;
  1220. ip_set(inst, to_id) = from;
  1221. write_unlock_bh(&ip_set_ref_lock);
  1222. return 0;
  1223. }
  1224. /* List/save set data */
  1225. #define DUMP_INIT 0
  1226. #define DUMP_ALL 1
  1227. #define DUMP_ONE 2
  1228. #define DUMP_LAST 3
  1229. #define DUMP_TYPE(arg) (((u32)(arg)) & 0x0000FFFF)
  1230. #define DUMP_FLAGS(arg) (((u32)(arg)) >> 16)
  1231. int
  1232. ip_set_put_flags(struct sk_buff *skb, struct ip_set *set)
  1233. {
  1234. u32 cadt_flags = 0;
  1235. if (SET_WITH_TIMEOUT(set))
  1236. if (unlikely(nla_put_net32(skb, IPSET_ATTR_TIMEOUT,
  1237. htonl(set->timeout))))
  1238. return -EMSGSIZE;
  1239. if (SET_WITH_COUNTER(set))
  1240. cadt_flags |= IPSET_FLAG_WITH_COUNTERS;
  1241. if (SET_WITH_COMMENT(set))
  1242. cadt_flags |= IPSET_FLAG_WITH_COMMENT;
  1243. if (SET_WITH_SKBINFO(set))
  1244. cadt_flags |= IPSET_FLAG_WITH_SKBINFO;
  1245. if (SET_WITH_FORCEADD(set))
  1246. cadt_flags |= IPSET_FLAG_WITH_FORCEADD;
  1247. if (!cadt_flags)
  1248. return 0;
  1249. return nla_put_net32(skb, IPSET_ATTR_CADT_FLAGS, htonl(cadt_flags));
  1250. }
  1251. EXPORT_SYMBOL_GPL(ip_set_put_flags);
  1252. static int
  1253. ip_set_dump_done(struct netlink_callback *cb)
  1254. {
  1255. if (cb->args[IPSET_CB_ARG0]) {
  1256. struct ip_set_net *inst =
  1257. (struct ip_set_net *)cb->args[IPSET_CB_NET];
  1258. ip_set_id_t index = (ip_set_id_t)cb->args[IPSET_CB_INDEX];
  1259. struct ip_set *set = ip_set_ref_netlink(inst, index);
  1260. if (set->variant->uref)
  1261. set->variant->uref(set, cb, false);
  1262. pr_debug("release set %s\n", set->name);
  1263. __ip_set_put_netlink(set);
  1264. }
  1265. return 0;
  1266. }
  1267. static inline void
  1268. dump_attrs(struct nlmsghdr *nlh)
  1269. {
  1270. const struct nlattr *attr;
  1271. int rem;
  1272. pr_debug("dump nlmsg\n");
  1273. nlmsg_for_each_attr(attr, nlh, sizeof(struct nfgenmsg), rem) {
  1274. pr_debug("type: %u, len %u\n", nla_type(attr), attr->nla_len);
  1275. }
  1276. }
  1277. static const struct nla_policy
  1278. ip_set_dump_policy[IPSET_ATTR_CMD_MAX + 1] = {
  1279. [IPSET_ATTR_PROTOCOL] = { .type = NLA_U8 },
  1280. [IPSET_ATTR_SETNAME] = { .type = NLA_NUL_STRING,
  1281. .len = IPSET_MAXNAMELEN - 1 },
  1282. [IPSET_ATTR_FLAGS] = { .type = NLA_U32 },
  1283. };
  1284. static int
  1285. ip_set_dump_start(struct netlink_callback *cb)
  1286. {
  1287. struct nlmsghdr *nlh = nlmsg_hdr(cb->skb);
  1288. int min_len = nlmsg_total_size(sizeof(struct nfgenmsg));
  1289. struct nlattr *cda[IPSET_ATTR_CMD_MAX + 1];
  1290. struct nlattr *attr = (void *)nlh + min_len;
  1291. struct sk_buff *skb = cb->skb;
  1292. struct ip_set_net *inst = ip_set_pernet(sock_net(skb->sk));
  1293. u32 dump_type;
  1294. int ret;
  1295. ret = nla_parse(cda, IPSET_ATTR_CMD_MAX, attr,
  1296. nlh->nlmsg_len - min_len,
  1297. ip_set_dump_policy, NULL);
  1298. if (ret)
  1299. goto error;
  1300. cb->args[IPSET_CB_PROTO] = nla_get_u8(cda[IPSET_ATTR_PROTOCOL]);
  1301. if (cda[IPSET_ATTR_SETNAME]) {
  1302. ip_set_id_t index;
  1303. struct ip_set *set;
  1304. set = find_set_and_id(inst, nla_data(cda[IPSET_ATTR_SETNAME]),
  1305. &index);
  1306. if (!set) {
  1307. ret = -ENOENT;
  1308. goto error;
  1309. }
  1310. dump_type = DUMP_ONE;
  1311. cb->args[IPSET_CB_INDEX] = index;
  1312. } else {
  1313. dump_type = DUMP_ALL;
  1314. }
  1315. if (cda[IPSET_ATTR_FLAGS]) {
  1316. u32 f = ip_set_get_h32(cda[IPSET_ATTR_FLAGS]);
  1317. dump_type |= (f << 16);
  1318. }
  1319. cb->args[IPSET_CB_NET] = (unsigned long)inst;
  1320. cb->args[IPSET_CB_DUMP] = dump_type;
  1321. return 0;
  1322. error:
  1323. /* We have to create and send the error message manually :-( */
  1324. if (nlh->nlmsg_flags & NLM_F_ACK) {
  1325. netlink_ack(cb->skb, nlh, ret, NULL);
  1326. }
  1327. return ret;
  1328. }
  1329. static int
  1330. ip_set_dump_do(struct sk_buff *skb, struct netlink_callback *cb)
  1331. {
  1332. ip_set_id_t index = IPSET_INVALID_ID, max;
  1333. struct ip_set *set = NULL;
  1334. struct nlmsghdr *nlh = NULL;
  1335. unsigned int flags = NETLINK_CB(cb->skb).portid ? NLM_F_MULTI : 0;
  1336. struct ip_set_net *inst = ip_set_pernet(sock_net(skb->sk));
  1337. u32 dump_type, dump_flags;
  1338. bool is_destroyed;
  1339. int ret = 0;
  1340. if (!cb->args[IPSET_CB_DUMP])
  1341. return -EINVAL;
  1342. if (cb->args[IPSET_CB_INDEX] >= inst->ip_set_max)
  1343. goto out;
  1344. dump_type = DUMP_TYPE(cb->args[IPSET_CB_DUMP]);
  1345. dump_flags = DUMP_FLAGS(cb->args[IPSET_CB_DUMP]);
  1346. max = dump_type == DUMP_ONE ? cb->args[IPSET_CB_INDEX] + 1
  1347. : inst->ip_set_max;
  1348. dump_last:
  1349. pr_debug("dump type, flag: %u %u index: %ld\n",
  1350. dump_type, dump_flags, cb->args[IPSET_CB_INDEX]);
  1351. for (; cb->args[IPSET_CB_INDEX] < max; cb->args[IPSET_CB_INDEX]++) {
  1352. index = (ip_set_id_t)cb->args[IPSET_CB_INDEX];
  1353. write_lock_bh(&ip_set_ref_lock);
  1354. set = ip_set(inst, index);
  1355. is_destroyed = inst->is_destroyed;
  1356. if (!set || is_destroyed) {
  1357. write_unlock_bh(&ip_set_ref_lock);
  1358. if (dump_type == DUMP_ONE) {
  1359. ret = -ENOENT;
  1360. goto out;
  1361. }
  1362. if (is_destroyed) {
  1363. /* All sets are just being destroyed */
  1364. ret = 0;
  1365. goto out;
  1366. }
  1367. continue;
  1368. }
  1369. /* When dumping all sets, we must dump "sorted"
  1370. * so that lists (unions of sets) are dumped last.
  1371. */
  1372. if (dump_type != DUMP_ONE &&
  1373. ((dump_type == DUMP_ALL) ==
  1374. !!(set->type->features & IPSET_DUMP_LAST))) {
  1375. write_unlock_bh(&ip_set_ref_lock);
  1376. continue;
  1377. }
  1378. pr_debug("List set: %s\n", set->name);
  1379. if (!cb->args[IPSET_CB_ARG0]) {
  1380. /* Start listing: make sure set won't be destroyed */
  1381. pr_debug("reference set\n");
  1382. set->ref_netlink++;
  1383. }
  1384. write_unlock_bh(&ip_set_ref_lock);
  1385. nlh = start_msg(skb, NETLINK_CB(cb->skb).portid,
  1386. cb->nlh->nlmsg_seq, flags,
  1387. IPSET_CMD_LIST);
  1388. if (!nlh) {
  1389. ret = -EMSGSIZE;
  1390. goto release_refcount;
  1391. }
  1392. if (nla_put_u8(skb, IPSET_ATTR_PROTOCOL,
  1393. cb->args[IPSET_CB_PROTO]) ||
  1394. nla_put_string(skb, IPSET_ATTR_SETNAME, set->name))
  1395. goto nla_put_failure;
  1396. if (dump_flags & IPSET_FLAG_LIST_SETNAME)
  1397. goto next_set;
  1398. switch (cb->args[IPSET_CB_ARG0]) {
  1399. case 0:
  1400. /* Core header data */
  1401. if (nla_put_string(skb, IPSET_ATTR_TYPENAME,
  1402. set->type->name) ||
  1403. nla_put_u8(skb, IPSET_ATTR_FAMILY,
  1404. set->family) ||
  1405. nla_put_u8(skb, IPSET_ATTR_REVISION,
  1406. set->revision))
  1407. goto nla_put_failure;
  1408. if (cb->args[IPSET_CB_PROTO] > IPSET_PROTOCOL_MIN &&
  1409. nla_put_net16(skb, IPSET_ATTR_INDEX, htons(index)))
  1410. goto nla_put_failure;
  1411. ret = set->variant->head(set, skb);
  1412. if (ret < 0)
  1413. goto release_refcount;
  1414. if (dump_flags & IPSET_FLAG_LIST_HEADER)
  1415. goto next_set;
  1416. if (set->variant->uref)
  1417. set->variant->uref(set, cb, true);
  1418. fallthrough;
  1419. default:
  1420. ret = set->variant->list(set, skb, cb);
  1421. if (!cb->args[IPSET_CB_ARG0])
  1422. /* Set is done, proceed with next one */
  1423. goto next_set;
  1424. goto release_refcount;
  1425. }
  1426. }
  1427. /* If we dump all sets, continue with dumping last ones */
  1428. if (dump_type == DUMP_ALL) {
  1429. dump_type = DUMP_LAST;
  1430. cb->args[IPSET_CB_DUMP] = dump_type | (dump_flags << 16);
  1431. cb->args[IPSET_CB_INDEX] = 0;
  1432. if (set && set->variant->uref)
  1433. set->variant->uref(set, cb, false);
  1434. goto dump_last;
  1435. }
  1436. goto out;
  1437. nla_put_failure:
  1438. ret = -EFAULT;
  1439. next_set:
  1440. if (dump_type == DUMP_ONE)
  1441. cb->args[IPSET_CB_INDEX] = IPSET_INVALID_ID;
  1442. else
  1443. cb->args[IPSET_CB_INDEX]++;
  1444. release_refcount:
  1445. /* If there was an error or set is done, release set */
  1446. if (ret || !cb->args[IPSET_CB_ARG0]) {
  1447. set = ip_set_ref_netlink(inst, index);
  1448. if (set->variant->uref)
  1449. set->variant->uref(set, cb, false);
  1450. pr_debug("release set %s\n", set->name);
  1451. __ip_set_put_netlink(set);
  1452. cb->args[IPSET_CB_ARG0] = 0;
  1453. }
  1454. out:
  1455. if (nlh) {
  1456. nlmsg_end(skb, nlh);
  1457. pr_debug("nlmsg_len: %u\n", nlh->nlmsg_len);
  1458. dump_attrs(nlh);
  1459. }
  1460. return ret < 0 ? ret : skb->len;
  1461. }
  1462. static int ip_set_dump(struct net *net, struct sock *ctnl, struct sk_buff *skb,
  1463. const struct nlmsghdr *nlh,
  1464. const struct nlattr * const attr[],
  1465. struct netlink_ext_ack *extack)
  1466. {
  1467. if (unlikely(protocol_min_failed(attr)))
  1468. return -IPSET_ERR_PROTOCOL;
  1469. {
  1470. struct netlink_dump_control c = {
  1471. .start = ip_set_dump_start,
  1472. .dump = ip_set_dump_do,
  1473. .done = ip_set_dump_done,
  1474. };
  1475. return netlink_dump_start(ctnl, skb, nlh, &c);
  1476. }
  1477. }
  1478. /* Add, del and test */
  1479. static const struct nla_policy ip_set_adt_policy[IPSET_ATTR_CMD_MAX + 1] = {
  1480. [IPSET_ATTR_PROTOCOL] = { .type = NLA_U8 },
  1481. [IPSET_ATTR_SETNAME] = { .type = NLA_NUL_STRING,
  1482. .len = IPSET_MAXNAMELEN - 1 },
  1483. [IPSET_ATTR_LINENO] = { .type = NLA_U32 },
  1484. [IPSET_ATTR_DATA] = { .type = NLA_NESTED },
  1485. [IPSET_ATTR_ADT] = { .type = NLA_NESTED },
  1486. };
  1487. static int
  1488. call_ad(struct sock *ctnl, struct sk_buff *skb, struct ip_set *set,
  1489. struct nlattr *tb[], enum ipset_adt adt,
  1490. u32 flags, bool use_lineno)
  1491. {
  1492. int ret;
  1493. u32 lineno = 0;
  1494. bool eexist = flags & IPSET_FLAG_EXIST, retried = false;
  1495. do {
  1496. ip_set_lock(set);
  1497. ret = set->variant->uadt(set, tb, adt, &lineno, flags, retried);
  1498. ip_set_unlock(set);
  1499. retried = true;
  1500. } while (ret == -EAGAIN &&
  1501. set->variant->resize &&
  1502. (ret = set->variant->resize(set, retried)) == 0);
  1503. if (!ret || (ret == -IPSET_ERR_EXIST && eexist))
  1504. return 0;
  1505. if (lineno && use_lineno) {
  1506. /* Error in restore/batch mode: send back lineno */
  1507. struct nlmsghdr *rep, *nlh = nlmsg_hdr(skb);
  1508. struct sk_buff *skb2;
  1509. struct nlmsgerr *errmsg;
  1510. size_t payload = min(SIZE_MAX,
  1511. sizeof(*errmsg) + nlmsg_len(nlh));
  1512. int min_len = nlmsg_total_size(sizeof(struct nfgenmsg));
  1513. struct nlattr *cda[IPSET_ATTR_CMD_MAX + 1];
  1514. struct nlattr *cmdattr;
  1515. u32 *errline;
  1516. skb2 = nlmsg_new(payload, GFP_KERNEL);
  1517. if (!skb2)
  1518. return -ENOMEM;
  1519. rep = __nlmsg_put(skb2, NETLINK_CB(skb).portid,
  1520. nlh->nlmsg_seq, NLMSG_ERROR, payload, 0);
  1521. errmsg = nlmsg_data(rep);
  1522. errmsg->error = ret;
  1523. memcpy(&errmsg->msg, nlh, nlh->nlmsg_len);
  1524. cmdattr = (void *)&errmsg->msg + min_len;
  1525. ret = nla_parse(cda, IPSET_ATTR_CMD_MAX, cmdattr,
  1526. nlh->nlmsg_len - min_len, ip_set_adt_policy,
  1527. NULL);
  1528. if (ret) {
  1529. nlmsg_free(skb2);
  1530. return ret;
  1531. }
  1532. errline = nla_data(cda[IPSET_ATTR_LINENO]);
  1533. *errline = lineno;
  1534. netlink_unicast(ctnl, skb2, NETLINK_CB(skb).portid,
  1535. MSG_DONTWAIT);
  1536. /* Signal netlink not to send its ACK/errmsg. */
  1537. return -EINTR;
  1538. }
  1539. return ret;
  1540. }
  1541. static int ip_set_ad(struct net *net, struct sock *ctnl,
  1542. struct sk_buff *skb,
  1543. enum ipset_adt adt,
  1544. const struct nlmsghdr *nlh,
  1545. const struct nlattr * const attr[],
  1546. struct netlink_ext_ack *extack)
  1547. {
  1548. struct ip_set_net *inst = ip_set_pernet(net);
  1549. struct ip_set *set;
  1550. struct nlattr *tb[IPSET_ATTR_ADT_MAX + 1] = {};
  1551. const struct nlattr *nla;
  1552. u32 flags = flag_exist(nlh);
  1553. bool use_lineno;
  1554. int ret = 0;
  1555. if (unlikely(protocol_min_failed(attr) ||
  1556. !attr[IPSET_ATTR_SETNAME] ||
  1557. !((attr[IPSET_ATTR_DATA] != NULL) ^
  1558. (attr[IPSET_ATTR_ADT] != NULL)) ||
  1559. (attr[IPSET_ATTR_DATA] &&
  1560. !flag_nested(attr[IPSET_ATTR_DATA])) ||
  1561. (attr[IPSET_ATTR_ADT] &&
  1562. (!flag_nested(attr[IPSET_ATTR_ADT]) ||
  1563. !attr[IPSET_ATTR_LINENO]))))
  1564. return -IPSET_ERR_PROTOCOL;
  1565. set = find_set(inst, nla_data(attr[IPSET_ATTR_SETNAME]));
  1566. if (!set)
  1567. return -ENOENT;
  1568. use_lineno = !!attr[IPSET_ATTR_LINENO];
  1569. if (attr[IPSET_ATTR_DATA]) {
  1570. if (nla_parse_nested(tb, IPSET_ATTR_ADT_MAX,
  1571. attr[IPSET_ATTR_DATA],
  1572. set->type->adt_policy, NULL))
  1573. return -IPSET_ERR_PROTOCOL;
  1574. ret = call_ad(ctnl, skb, set, tb, adt, flags,
  1575. use_lineno);
  1576. } else {
  1577. int nla_rem;
  1578. nla_for_each_nested(nla, attr[IPSET_ATTR_ADT], nla_rem) {
  1579. if (nla_type(nla) != IPSET_ATTR_DATA ||
  1580. !flag_nested(nla) ||
  1581. nla_parse_nested(tb, IPSET_ATTR_ADT_MAX, nla,
  1582. set->type->adt_policy, NULL))
  1583. return -IPSET_ERR_PROTOCOL;
  1584. ret = call_ad(ctnl, skb, set, tb, adt,
  1585. flags, use_lineno);
  1586. if (ret < 0)
  1587. return ret;
  1588. }
  1589. }
  1590. return ret;
  1591. }
  1592. static int ip_set_uadd(struct net *net, struct sock *ctnl,
  1593. struct sk_buff *skb, const struct nlmsghdr *nlh,
  1594. const struct nlattr * const attr[],
  1595. struct netlink_ext_ack *extack)
  1596. {
  1597. return ip_set_ad(net, ctnl, skb,
  1598. IPSET_ADD, nlh, attr, extack);
  1599. }
  1600. static int ip_set_udel(struct net *net, struct sock *ctnl,
  1601. struct sk_buff *skb, const struct nlmsghdr *nlh,
  1602. const struct nlattr * const attr[],
  1603. struct netlink_ext_ack *extack)
  1604. {
  1605. return ip_set_ad(net, ctnl, skb,
  1606. IPSET_DEL, nlh, attr, extack);
  1607. }
  1608. static int ip_set_utest(struct net *net, struct sock *ctnl, struct sk_buff *skb,
  1609. const struct nlmsghdr *nlh,
  1610. const struct nlattr * const attr[],
  1611. struct netlink_ext_ack *extack)
  1612. {
  1613. struct ip_set_net *inst = ip_set_pernet(net);
  1614. struct ip_set *set;
  1615. struct nlattr *tb[IPSET_ATTR_ADT_MAX + 1] = {};
  1616. int ret = 0;
  1617. u32 lineno;
  1618. if (unlikely(protocol_min_failed(attr) ||
  1619. !attr[IPSET_ATTR_SETNAME] ||
  1620. !attr[IPSET_ATTR_DATA] ||
  1621. !flag_nested(attr[IPSET_ATTR_DATA])))
  1622. return -IPSET_ERR_PROTOCOL;
  1623. set = find_set(inst, nla_data(attr[IPSET_ATTR_SETNAME]));
  1624. if (!set)
  1625. return -ENOENT;
  1626. if (nla_parse_nested(tb, IPSET_ATTR_ADT_MAX, attr[IPSET_ATTR_DATA],
  1627. set->type->adt_policy, NULL))
  1628. return -IPSET_ERR_PROTOCOL;
  1629. rcu_read_lock_bh();
  1630. ret = set->variant->uadt(set, tb, IPSET_TEST, &lineno, 0, 0);
  1631. rcu_read_unlock_bh();
  1632. /* Userspace can't trigger element to be re-added */
  1633. if (ret == -EAGAIN)
  1634. ret = 1;
  1635. return ret > 0 ? 0 : -IPSET_ERR_EXIST;
  1636. }
  1637. /* Get headed data of a set */
  1638. static int ip_set_header(struct net *net, struct sock *ctnl,
  1639. struct sk_buff *skb, const struct nlmsghdr *nlh,
  1640. const struct nlattr * const attr[],
  1641. struct netlink_ext_ack *extack)
  1642. {
  1643. struct ip_set_net *inst = ip_set_pernet(net);
  1644. const struct ip_set *set;
  1645. struct sk_buff *skb2;
  1646. struct nlmsghdr *nlh2;
  1647. int ret = 0;
  1648. if (unlikely(protocol_min_failed(attr) ||
  1649. !attr[IPSET_ATTR_SETNAME]))
  1650. return -IPSET_ERR_PROTOCOL;
  1651. set = find_set(inst, nla_data(attr[IPSET_ATTR_SETNAME]));
  1652. if (!set)
  1653. return -ENOENT;
  1654. skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  1655. if (!skb2)
  1656. return -ENOMEM;
  1657. nlh2 = start_msg(skb2, NETLINK_CB(skb).portid, nlh->nlmsg_seq, 0,
  1658. IPSET_CMD_HEADER);
  1659. if (!nlh2)
  1660. goto nlmsg_failure;
  1661. if (nla_put_u8(skb2, IPSET_ATTR_PROTOCOL, protocol(attr)) ||
  1662. nla_put_string(skb2, IPSET_ATTR_SETNAME, set->name) ||
  1663. nla_put_string(skb2, IPSET_ATTR_TYPENAME, set->type->name) ||
  1664. nla_put_u8(skb2, IPSET_ATTR_FAMILY, set->family) ||
  1665. nla_put_u8(skb2, IPSET_ATTR_REVISION, set->revision))
  1666. goto nla_put_failure;
  1667. nlmsg_end(skb2, nlh2);
  1668. ret = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).portid, MSG_DONTWAIT);
  1669. if (ret < 0)
  1670. return ret;
  1671. return 0;
  1672. nla_put_failure:
  1673. nlmsg_cancel(skb2, nlh2);
  1674. nlmsg_failure:
  1675. kfree_skb(skb2);
  1676. return -EMSGSIZE;
  1677. }
  1678. /* Get type data */
  1679. static const struct nla_policy ip_set_type_policy[IPSET_ATTR_CMD_MAX + 1] = {
  1680. [IPSET_ATTR_PROTOCOL] = { .type = NLA_U8 },
  1681. [IPSET_ATTR_TYPENAME] = { .type = NLA_NUL_STRING,
  1682. .len = IPSET_MAXNAMELEN - 1 },
  1683. [IPSET_ATTR_FAMILY] = { .type = NLA_U8 },
  1684. };
  1685. static int ip_set_type(struct net *net, struct sock *ctnl, struct sk_buff *skb,
  1686. const struct nlmsghdr *nlh,
  1687. const struct nlattr * const attr[],
  1688. struct netlink_ext_ack *extack)
  1689. {
  1690. struct sk_buff *skb2;
  1691. struct nlmsghdr *nlh2;
  1692. u8 family, min, max;
  1693. const char *typename;
  1694. int ret = 0;
  1695. if (unlikely(protocol_min_failed(attr) ||
  1696. !attr[IPSET_ATTR_TYPENAME] ||
  1697. !attr[IPSET_ATTR_FAMILY]))
  1698. return -IPSET_ERR_PROTOCOL;
  1699. family = nla_get_u8(attr[IPSET_ATTR_FAMILY]);
  1700. typename = nla_data(attr[IPSET_ATTR_TYPENAME]);
  1701. ret = find_set_type_minmax(typename, family, &min, &max);
  1702. if (ret)
  1703. return ret;
  1704. skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  1705. if (!skb2)
  1706. return -ENOMEM;
  1707. nlh2 = start_msg(skb2, NETLINK_CB(skb).portid, nlh->nlmsg_seq, 0,
  1708. IPSET_CMD_TYPE);
  1709. if (!nlh2)
  1710. goto nlmsg_failure;
  1711. if (nla_put_u8(skb2, IPSET_ATTR_PROTOCOL, protocol(attr)) ||
  1712. nla_put_string(skb2, IPSET_ATTR_TYPENAME, typename) ||
  1713. nla_put_u8(skb2, IPSET_ATTR_FAMILY, family) ||
  1714. nla_put_u8(skb2, IPSET_ATTR_REVISION, max) ||
  1715. nla_put_u8(skb2, IPSET_ATTR_REVISION_MIN, min))
  1716. goto nla_put_failure;
  1717. nlmsg_end(skb2, nlh2);
  1718. pr_debug("Send TYPE, nlmsg_len: %u\n", nlh2->nlmsg_len);
  1719. ret = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).portid, MSG_DONTWAIT);
  1720. if (ret < 0)
  1721. return ret;
  1722. return 0;
  1723. nla_put_failure:
  1724. nlmsg_cancel(skb2, nlh2);
  1725. nlmsg_failure:
  1726. kfree_skb(skb2);
  1727. return -EMSGSIZE;
  1728. }
  1729. /* Get protocol version */
  1730. static const struct nla_policy
  1731. ip_set_protocol_policy[IPSET_ATTR_CMD_MAX + 1] = {
  1732. [IPSET_ATTR_PROTOCOL] = { .type = NLA_U8 },
  1733. };
  1734. static int ip_set_protocol(struct net *net, struct sock *ctnl,
  1735. struct sk_buff *skb, const struct nlmsghdr *nlh,
  1736. const struct nlattr * const attr[],
  1737. struct netlink_ext_ack *extack)
  1738. {
  1739. struct sk_buff *skb2;
  1740. struct nlmsghdr *nlh2;
  1741. int ret = 0;
  1742. if (unlikely(!attr[IPSET_ATTR_PROTOCOL]))
  1743. return -IPSET_ERR_PROTOCOL;
  1744. skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  1745. if (!skb2)
  1746. return -ENOMEM;
  1747. nlh2 = start_msg(skb2, NETLINK_CB(skb).portid, nlh->nlmsg_seq, 0,
  1748. IPSET_CMD_PROTOCOL);
  1749. if (!nlh2)
  1750. goto nlmsg_failure;
  1751. if (nla_put_u8(skb2, IPSET_ATTR_PROTOCOL, IPSET_PROTOCOL))
  1752. goto nla_put_failure;
  1753. if (nla_put_u8(skb2, IPSET_ATTR_PROTOCOL_MIN, IPSET_PROTOCOL_MIN))
  1754. goto nla_put_failure;
  1755. nlmsg_end(skb2, nlh2);
  1756. ret = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).portid, MSG_DONTWAIT);
  1757. if (ret < 0)
  1758. return ret;
  1759. return 0;
  1760. nla_put_failure:
  1761. nlmsg_cancel(skb2, nlh2);
  1762. nlmsg_failure:
  1763. kfree_skb(skb2);
  1764. return -EMSGSIZE;
  1765. }
  1766. /* Get set by name or index, from userspace */
  1767. static int ip_set_byname(struct net *net, struct sock *ctnl,
  1768. struct sk_buff *skb, const struct nlmsghdr *nlh,
  1769. const struct nlattr * const attr[],
  1770. struct netlink_ext_ack *extack)
  1771. {
  1772. struct ip_set_net *inst = ip_set_pernet(net);
  1773. struct sk_buff *skb2;
  1774. struct nlmsghdr *nlh2;
  1775. ip_set_id_t id = IPSET_INVALID_ID;
  1776. const struct ip_set *set;
  1777. int ret = 0;
  1778. if (unlikely(protocol_failed(attr) ||
  1779. !attr[IPSET_ATTR_SETNAME]))
  1780. return -IPSET_ERR_PROTOCOL;
  1781. set = find_set_and_id(inst, nla_data(attr[IPSET_ATTR_SETNAME]), &id);
  1782. if (id == IPSET_INVALID_ID)
  1783. return -ENOENT;
  1784. skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  1785. if (!skb2)
  1786. return -ENOMEM;
  1787. nlh2 = start_msg(skb2, NETLINK_CB(skb).portid, nlh->nlmsg_seq, 0,
  1788. IPSET_CMD_GET_BYNAME);
  1789. if (!nlh2)
  1790. goto nlmsg_failure;
  1791. if (nla_put_u8(skb2, IPSET_ATTR_PROTOCOL, protocol(attr)) ||
  1792. nla_put_u8(skb2, IPSET_ATTR_FAMILY, set->family) ||
  1793. nla_put_net16(skb2, IPSET_ATTR_INDEX, htons(id)))
  1794. goto nla_put_failure;
  1795. nlmsg_end(skb2, nlh2);
  1796. ret = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).portid, MSG_DONTWAIT);
  1797. if (ret < 0)
  1798. return ret;
  1799. return 0;
  1800. nla_put_failure:
  1801. nlmsg_cancel(skb2, nlh2);
  1802. nlmsg_failure:
  1803. kfree_skb(skb2);
  1804. return -EMSGSIZE;
  1805. }
  1806. static const struct nla_policy ip_set_index_policy[IPSET_ATTR_CMD_MAX + 1] = {
  1807. [IPSET_ATTR_PROTOCOL] = { .type = NLA_U8 },
  1808. [IPSET_ATTR_INDEX] = { .type = NLA_U16 },
  1809. };
  1810. static int ip_set_byindex(struct net *net, struct sock *ctnl,
  1811. struct sk_buff *skb, const struct nlmsghdr *nlh,
  1812. const struct nlattr * const attr[],
  1813. struct netlink_ext_ack *extack)
  1814. {
  1815. struct ip_set_net *inst = ip_set_pernet(net);
  1816. struct sk_buff *skb2;
  1817. struct nlmsghdr *nlh2;
  1818. ip_set_id_t id = IPSET_INVALID_ID;
  1819. const struct ip_set *set;
  1820. int ret = 0;
  1821. if (unlikely(protocol_failed(attr) ||
  1822. !attr[IPSET_ATTR_INDEX]))
  1823. return -IPSET_ERR_PROTOCOL;
  1824. id = ip_set_get_h16(attr[IPSET_ATTR_INDEX]);
  1825. if (id >= inst->ip_set_max)
  1826. return -ENOENT;
  1827. set = ip_set(inst, id);
  1828. if (set == NULL)
  1829. return -ENOENT;
  1830. skb2 = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  1831. if (!skb2)
  1832. return -ENOMEM;
  1833. nlh2 = start_msg(skb2, NETLINK_CB(skb).portid, nlh->nlmsg_seq, 0,
  1834. IPSET_CMD_GET_BYINDEX);
  1835. if (!nlh2)
  1836. goto nlmsg_failure;
  1837. if (nla_put_u8(skb2, IPSET_ATTR_PROTOCOL, protocol(attr)) ||
  1838. nla_put_string(skb2, IPSET_ATTR_SETNAME, set->name))
  1839. goto nla_put_failure;
  1840. nlmsg_end(skb2, nlh2);
  1841. ret = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).portid, MSG_DONTWAIT);
  1842. if (ret < 0)
  1843. return ret;
  1844. return 0;
  1845. nla_put_failure:
  1846. nlmsg_cancel(skb2, nlh2);
  1847. nlmsg_failure:
  1848. kfree_skb(skb2);
  1849. return -EMSGSIZE;
  1850. }
  1851. static const struct nfnl_callback ip_set_netlink_subsys_cb[IPSET_MSG_MAX] = {
  1852. [IPSET_CMD_NONE] = {
  1853. .call = ip_set_none,
  1854. .attr_count = IPSET_ATTR_CMD_MAX,
  1855. },
  1856. [IPSET_CMD_CREATE] = {
  1857. .call = ip_set_create,
  1858. .attr_count = IPSET_ATTR_CMD_MAX,
  1859. .policy = ip_set_create_policy,
  1860. },
  1861. [IPSET_CMD_DESTROY] = {
  1862. .call = ip_set_destroy,
  1863. .attr_count = IPSET_ATTR_CMD_MAX,
  1864. .policy = ip_set_setname_policy,
  1865. },
  1866. [IPSET_CMD_FLUSH] = {
  1867. .call = ip_set_flush,
  1868. .attr_count = IPSET_ATTR_CMD_MAX,
  1869. .policy = ip_set_setname_policy,
  1870. },
  1871. [IPSET_CMD_RENAME] = {
  1872. .call = ip_set_rename,
  1873. .attr_count = IPSET_ATTR_CMD_MAX,
  1874. .policy = ip_set_setname2_policy,
  1875. },
  1876. [IPSET_CMD_SWAP] = {
  1877. .call = ip_set_swap,
  1878. .attr_count = IPSET_ATTR_CMD_MAX,
  1879. .policy = ip_set_setname2_policy,
  1880. },
  1881. [IPSET_CMD_LIST] = {
  1882. .call = ip_set_dump,
  1883. .attr_count = IPSET_ATTR_CMD_MAX,
  1884. .policy = ip_set_dump_policy,
  1885. },
  1886. [IPSET_CMD_SAVE] = {
  1887. .call = ip_set_dump,
  1888. .attr_count = IPSET_ATTR_CMD_MAX,
  1889. .policy = ip_set_setname_policy,
  1890. },
  1891. [IPSET_CMD_ADD] = {
  1892. .call = ip_set_uadd,
  1893. .attr_count = IPSET_ATTR_CMD_MAX,
  1894. .policy = ip_set_adt_policy,
  1895. },
  1896. [IPSET_CMD_DEL] = {
  1897. .call = ip_set_udel,
  1898. .attr_count = IPSET_ATTR_CMD_MAX,
  1899. .policy = ip_set_adt_policy,
  1900. },
  1901. [IPSET_CMD_TEST] = {
  1902. .call = ip_set_utest,
  1903. .attr_count = IPSET_ATTR_CMD_MAX,
  1904. .policy = ip_set_adt_policy,
  1905. },
  1906. [IPSET_CMD_HEADER] = {
  1907. .call = ip_set_header,
  1908. .attr_count = IPSET_ATTR_CMD_MAX,
  1909. .policy = ip_set_setname_policy,
  1910. },
  1911. [IPSET_CMD_TYPE] = {
  1912. .call = ip_set_type,
  1913. .attr_count = IPSET_ATTR_CMD_MAX,
  1914. .policy = ip_set_type_policy,
  1915. },
  1916. [IPSET_CMD_PROTOCOL] = {
  1917. .call = ip_set_protocol,
  1918. .attr_count = IPSET_ATTR_CMD_MAX,
  1919. .policy = ip_set_protocol_policy,
  1920. },
  1921. [IPSET_CMD_GET_BYNAME] = {
  1922. .call = ip_set_byname,
  1923. .attr_count = IPSET_ATTR_CMD_MAX,
  1924. .policy = ip_set_setname_policy,
  1925. },
  1926. [IPSET_CMD_GET_BYINDEX] = {
  1927. .call = ip_set_byindex,
  1928. .attr_count = IPSET_ATTR_CMD_MAX,
  1929. .policy = ip_set_index_policy,
  1930. },
  1931. };
  1932. static struct nfnetlink_subsystem ip_set_netlink_subsys __read_mostly = {
  1933. .name = "ip_set",
  1934. .subsys_id = NFNL_SUBSYS_IPSET,
  1935. .cb_count = IPSET_MSG_MAX,
  1936. .cb = ip_set_netlink_subsys_cb,
  1937. };
  1938. /* Interface to iptables/ip6tables */
  1939. static int
  1940. ip_set_sockfn_get(struct sock *sk, int optval, void __user *user, int *len)
  1941. {
  1942. unsigned int *op;
  1943. void *data;
  1944. int copylen = *len, ret = 0;
  1945. struct net *net = sock_net(sk);
  1946. struct ip_set_net *inst = ip_set_pernet(net);
  1947. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  1948. return -EPERM;
  1949. if (optval != SO_IP_SET)
  1950. return -EBADF;
  1951. if (*len < sizeof(unsigned int))
  1952. return -EINVAL;
  1953. data = vmalloc(*len);
  1954. if (!data)
  1955. return -ENOMEM;
  1956. if (copy_from_user(data, user, *len) != 0) {
  1957. ret = -EFAULT;
  1958. goto done;
  1959. }
  1960. op = data;
  1961. if (*op < IP_SET_OP_VERSION) {
  1962. /* Check the version at the beginning of operations */
  1963. struct ip_set_req_version *req_version = data;
  1964. if (*len < sizeof(struct ip_set_req_version)) {
  1965. ret = -EINVAL;
  1966. goto done;
  1967. }
  1968. if (req_version->version < IPSET_PROTOCOL_MIN) {
  1969. ret = -EPROTO;
  1970. goto done;
  1971. }
  1972. }
  1973. switch (*op) {
  1974. case IP_SET_OP_VERSION: {
  1975. struct ip_set_req_version *req_version = data;
  1976. if (*len != sizeof(struct ip_set_req_version)) {
  1977. ret = -EINVAL;
  1978. goto done;
  1979. }
  1980. req_version->version = IPSET_PROTOCOL;
  1981. if (copy_to_user(user, req_version,
  1982. sizeof(struct ip_set_req_version)))
  1983. ret = -EFAULT;
  1984. goto done;
  1985. }
  1986. case IP_SET_OP_GET_BYNAME: {
  1987. struct ip_set_req_get_set *req_get = data;
  1988. ip_set_id_t id;
  1989. if (*len != sizeof(struct ip_set_req_get_set)) {
  1990. ret = -EINVAL;
  1991. goto done;
  1992. }
  1993. req_get->set.name[IPSET_MAXNAMELEN - 1] = '\0';
  1994. nfnl_lock(NFNL_SUBSYS_IPSET);
  1995. find_set_and_id(inst, req_get->set.name, &id);
  1996. req_get->set.index = id;
  1997. nfnl_unlock(NFNL_SUBSYS_IPSET);
  1998. goto copy;
  1999. }
  2000. case IP_SET_OP_GET_FNAME: {
  2001. struct ip_set_req_get_set_family *req_get = data;
  2002. ip_set_id_t id;
  2003. if (*len != sizeof(struct ip_set_req_get_set_family)) {
  2004. ret = -EINVAL;
  2005. goto done;
  2006. }
  2007. req_get->set.name[IPSET_MAXNAMELEN - 1] = '\0';
  2008. nfnl_lock(NFNL_SUBSYS_IPSET);
  2009. find_set_and_id(inst, req_get->set.name, &id);
  2010. req_get->set.index = id;
  2011. if (id != IPSET_INVALID_ID)
  2012. req_get->family = ip_set(inst, id)->family;
  2013. nfnl_unlock(NFNL_SUBSYS_IPSET);
  2014. goto copy;
  2015. }
  2016. case IP_SET_OP_GET_BYINDEX: {
  2017. struct ip_set_req_get_set *req_get = data;
  2018. struct ip_set *set;
  2019. if (*len != sizeof(struct ip_set_req_get_set) ||
  2020. req_get->set.index >= inst->ip_set_max) {
  2021. ret = -EINVAL;
  2022. goto done;
  2023. }
  2024. nfnl_lock(NFNL_SUBSYS_IPSET);
  2025. set = ip_set(inst, req_get->set.index);
  2026. ret = strscpy(req_get->set.name, set ? set->name : "",
  2027. IPSET_MAXNAMELEN);
  2028. nfnl_unlock(NFNL_SUBSYS_IPSET);
  2029. if (ret < 0)
  2030. goto done;
  2031. goto copy;
  2032. }
  2033. default:
  2034. ret = -EBADMSG;
  2035. goto done;
  2036. } /* end of switch(op) */
  2037. copy:
  2038. if (copy_to_user(user, data, copylen))
  2039. ret = -EFAULT;
  2040. done:
  2041. vfree(data);
  2042. if (ret > 0)
  2043. ret = 0;
  2044. return ret;
  2045. }
  2046. static struct nf_sockopt_ops so_set __read_mostly = {
  2047. .pf = PF_INET,
  2048. .get_optmin = SO_IP_SET,
  2049. .get_optmax = SO_IP_SET + 1,
  2050. .get = ip_set_sockfn_get,
  2051. .owner = THIS_MODULE,
  2052. };
  2053. static int __net_init
  2054. ip_set_net_init(struct net *net)
  2055. {
  2056. struct ip_set_net *inst = ip_set_pernet(net);
  2057. struct ip_set **list;
  2058. inst->ip_set_max = max_sets ? max_sets : CONFIG_IP_SET_MAX;
  2059. if (inst->ip_set_max >= IPSET_INVALID_ID)
  2060. inst->ip_set_max = IPSET_INVALID_ID - 1;
  2061. list = kvcalloc(inst->ip_set_max, sizeof(struct ip_set *), GFP_KERNEL);
  2062. if (!list)
  2063. return -ENOMEM;
  2064. inst->is_deleted = false;
  2065. inst->is_destroyed = false;
  2066. rcu_assign_pointer(inst->ip_set_list, list);
  2067. return 0;
  2068. }
  2069. static void __net_exit
  2070. ip_set_net_exit(struct net *net)
  2071. {
  2072. struct ip_set_net *inst = ip_set_pernet(net);
  2073. struct ip_set *set = NULL;
  2074. ip_set_id_t i;
  2075. inst->is_deleted = true; /* flag for ip_set_nfnl_put */
  2076. nfnl_lock(NFNL_SUBSYS_IPSET);
  2077. for (i = 0; i < inst->ip_set_max; i++) {
  2078. set = ip_set(inst, i);
  2079. if (set) {
  2080. ip_set(inst, i) = NULL;
  2081. ip_set_destroy_set(set);
  2082. }
  2083. }
  2084. nfnl_unlock(NFNL_SUBSYS_IPSET);
  2085. kvfree(rcu_dereference_protected(inst->ip_set_list, 1));
  2086. }
  2087. static struct pernet_operations ip_set_net_ops = {
  2088. .init = ip_set_net_init,
  2089. .exit = ip_set_net_exit,
  2090. .id = &ip_set_net_id,
  2091. .size = sizeof(struct ip_set_net),
  2092. };
  2093. static int __init
  2094. ip_set_init(void)
  2095. {
  2096. int ret = register_pernet_subsys(&ip_set_net_ops);
  2097. if (ret) {
  2098. pr_err("ip_set: cannot register pernet_subsys.\n");
  2099. return ret;
  2100. }
  2101. ret = nfnetlink_subsys_register(&ip_set_netlink_subsys);
  2102. if (ret != 0) {
  2103. pr_err("ip_set: cannot register with nfnetlink.\n");
  2104. unregister_pernet_subsys(&ip_set_net_ops);
  2105. return ret;
  2106. }
  2107. ret = nf_register_sockopt(&so_set);
  2108. if (ret != 0) {
  2109. pr_err("SO_SET registry failed: %d\n", ret);
  2110. nfnetlink_subsys_unregister(&ip_set_netlink_subsys);
  2111. unregister_pernet_subsys(&ip_set_net_ops);
  2112. return ret;
  2113. }
  2114. return 0;
  2115. }
  2116. static void __exit
  2117. ip_set_fini(void)
  2118. {
  2119. nf_unregister_sockopt(&so_set);
  2120. nfnetlink_subsys_unregister(&ip_set_netlink_subsys);
  2121. unregister_pernet_subsys(&ip_set_net_ops);
  2122. pr_debug("these are the famous last words\n");
  2123. }
  2124. module_init(ip_set_init);
  2125. module_exit(ip_set_fini);
  2126. MODULE_DESCRIPTION("ip_set: protocol " __stringify(IPSET_PROTOCOL));