xfrm_state.c 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330
  1. /*
  2. * xfrm_state.c
  3. *
  4. * Changes:
  5. * Mitsuru KANDA @USAGI
  6. * Kazunori MIYAZAWA @USAGI
  7. * Kunihiro Ishiguro <kunihiro@ipinfusion.com>
  8. * IPv6 support
  9. * YOSHIFUJI Hideaki @USAGI
  10. * Split up af-specific functions
  11. * Derek Atkins <derek@ihtfp.com>
  12. * Add UDP Encapsulation
  13. *
  14. */
  15. #include <linux/workqueue.h>
  16. #include <net/xfrm.h>
  17. #include <linux/pfkeyv2.h>
  18. #include <linux/ipsec.h>
  19. #include <linux/module.h>
  20. #include <linux/cache.h>
  21. #include <linux/audit.h>
  22. #include <asm/uaccess.h>
  23. #include <linux/ktime.h>
  24. #include <linux/slab.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/kernel.h>
  27. #include "xfrm_hash.h"
  28. #define xfrm_state_deref_prot(table, net) \
  29. rcu_dereference_protected((table), lockdep_is_held(&(net)->xfrm.xfrm_state_lock))
  30. static void xfrm_state_gc_task(struct work_struct *work);
  31. /* Each xfrm_state may be linked to two tables:
  32. 1. Hash table by (spi,daddr,ah/esp) to find SA by SPI. (input,ctl)
  33. 2. Hash table by (daddr,family,reqid) to find what SAs exist for given
  34. destination/tunnel endpoint. (output)
  35. */
  36. static unsigned int xfrm_state_hashmax __read_mostly = 1 * 1024 * 1024;
  37. static __read_mostly seqcount_t xfrm_state_hash_generation = SEQCNT_ZERO(xfrm_state_hash_generation);
  38. static DECLARE_WORK(xfrm_state_gc_work, xfrm_state_gc_task);
  39. static HLIST_HEAD(xfrm_state_gc_list);
  40. static inline bool xfrm_state_hold_rcu(struct xfrm_state __rcu *x)
  41. {
  42. return atomic_inc_not_zero(&x->refcnt);
  43. }
  44. static inline unsigned int xfrm_dst_hash(struct net *net,
  45. const xfrm_address_t *daddr,
  46. const xfrm_address_t *saddr,
  47. u32 reqid,
  48. unsigned short family)
  49. {
  50. return __xfrm_dst_hash(daddr, saddr, reqid, family, net->xfrm.state_hmask);
  51. }
  52. static inline unsigned int xfrm_src_hash(struct net *net,
  53. const xfrm_address_t *daddr,
  54. const xfrm_address_t *saddr,
  55. unsigned short family)
  56. {
  57. return __xfrm_src_hash(daddr, saddr, family, net->xfrm.state_hmask);
  58. }
  59. static inline unsigned int
  60. xfrm_spi_hash(struct net *net, const xfrm_address_t *daddr,
  61. __be32 spi, u8 proto, unsigned short family)
  62. {
  63. return __xfrm_spi_hash(daddr, spi, proto, family, net->xfrm.state_hmask);
  64. }
  65. static void xfrm_hash_transfer(struct hlist_head *list,
  66. struct hlist_head *ndsttable,
  67. struct hlist_head *nsrctable,
  68. struct hlist_head *nspitable,
  69. unsigned int nhashmask)
  70. {
  71. struct hlist_node *tmp;
  72. struct xfrm_state *x;
  73. hlist_for_each_entry_safe(x, tmp, list, bydst) {
  74. unsigned int h;
  75. h = __xfrm_dst_hash(&x->id.daddr, &x->props.saddr,
  76. x->props.reqid, x->props.family,
  77. nhashmask);
  78. hlist_add_head_rcu(&x->bydst, ndsttable + h);
  79. h = __xfrm_src_hash(&x->id.daddr, &x->props.saddr,
  80. x->props.family,
  81. nhashmask);
  82. hlist_add_head_rcu(&x->bysrc, nsrctable + h);
  83. if (x->id.spi) {
  84. h = __xfrm_spi_hash(&x->id.daddr, x->id.spi,
  85. x->id.proto, x->props.family,
  86. nhashmask);
  87. hlist_add_head_rcu(&x->byspi, nspitable + h);
  88. }
  89. }
  90. }
  91. static unsigned long xfrm_hash_new_size(unsigned int state_hmask)
  92. {
  93. return ((state_hmask + 1) << 1) * sizeof(struct hlist_head);
  94. }
  95. static void xfrm_hash_resize(struct work_struct *work)
  96. {
  97. struct net *net = container_of(work, struct net, xfrm.state_hash_work);
  98. struct hlist_head *ndst, *nsrc, *nspi, *odst, *osrc, *ospi;
  99. unsigned long nsize, osize;
  100. unsigned int nhashmask, ohashmask;
  101. int i;
  102. nsize = xfrm_hash_new_size(net->xfrm.state_hmask);
  103. ndst = xfrm_hash_alloc(nsize);
  104. if (!ndst)
  105. return;
  106. nsrc = xfrm_hash_alloc(nsize);
  107. if (!nsrc) {
  108. xfrm_hash_free(ndst, nsize);
  109. return;
  110. }
  111. nspi = xfrm_hash_alloc(nsize);
  112. if (!nspi) {
  113. xfrm_hash_free(ndst, nsize);
  114. xfrm_hash_free(nsrc, nsize);
  115. return;
  116. }
  117. spin_lock_bh(&net->xfrm.xfrm_state_lock);
  118. write_seqcount_begin(&xfrm_state_hash_generation);
  119. nhashmask = (nsize / sizeof(struct hlist_head)) - 1U;
  120. odst = xfrm_state_deref_prot(net->xfrm.state_bydst, net);
  121. for (i = net->xfrm.state_hmask; i >= 0; i--)
  122. xfrm_hash_transfer(odst + i, ndst, nsrc, nspi, nhashmask);
  123. osrc = xfrm_state_deref_prot(net->xfrm.state_bysrc, net);
  124. ospi = xfrm_state_deref_prot(net->xfrm.state_byspi, net);
  125. ohashmask = net->xfrm.state_hmask;
  126. rcu_assign_pointer(net->xfrm.state_bydst, ndst);
  127. rcu_assign_pointer(net->xfrm.state_bysrc, nsrc);
  128. rcu_assign_pointer(net->xfrm.state_byspi, nspi);
  129. net->xfrm.state_hmask = nhashmask;
  130. write_seqcount_end(&xfrm_state_hash_generation);
  131. spin_unlock_bh(&net->xfrm.xfrm_state_lock);
  132. osize = (ohashmask + 1) * sizeof(struct hlist_head);
  133. synchronize_rcu();
  134. xfrm_hash_free(odst, osize);
  135. xfrm_hash_free(osrc, osize);
  136. xfrm_hash_free(ospi, osize);
  137. }
  138. static DEFINE_SPINLOCK(xfrm_state_afinfo_lock);
  139. static struct xfrm_state_afinfo __rcu *xfrm_state_afinfo[NPROTO];
  140. static DEFINE_SPINLOCK(xfrm_state_gc_lock);
  141. int __xfrm_state_delete(struct xfrm_state *x);
  142. int km_query(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *pol);
  143. bool km_is_alive(const struct km_event *c);
  144. void km_state_expired(struct xfrm_state *x, int hard, u32 portid);
  145. static DEFINE_SPINLOCK(xfrm_type_lock);
  146. int xfrm_register_type(const struct xfrm_type *type, unsigned short family)
  147. {
  148. struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
  149. const struct xfrm_type **typemap;
  150. int err = 0;
  151. if (unlikely(afinfo == NULL))
  152. return -EAFNOSUPPORT;
  153. typemap = afinfo->type_map;
  154. spin_lock_bh(&xfrm_type_lock);
  155. if (likely(typemap[type->proto] == NULL))
  156. typemap[type->proto] = type;
  157. else
  158. err = -EEXIST;
  159. spin_unlock_bh(&xfrm_type_lock);
  160. xfrm_state_put_afinfo(afinfo);
  161. return err;
  162. }
  163. EXPORT_SYMBOL(xfrm_register_type);
  164. int xfrm_unregister_type(const struct xfrm_type *type, unsigned short family)
  165. {
  166. struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
  167. const struct xfrm_type **typemap;
  168. int err = 0;
  169. if (unlikely(afinfo == NULL))
  170. return -EAFNOSUPPORT;
  171. typemap = afinfo->type_map;
  172. spin_lock_bh(&xfrm_type_lock);
  173. if (unlikely(typemap[type->proto] != type))
  174. err = -ENOENT;
  175. else
  176. typemap[type->proto] = NULL;
  177. spin_unlock_bh(&xfrm_type_lock);
  178. xfrm_state_put_afinfo(afinfo);
  179. return err;
  180. }
  181. EXPORT_SYMBOL(xfrm_unregister_type);
  182. static const struct xfrm_type *xfrm_get_type(u8 proto, unsigned short family)
  183. {
  184. struct xfrm_state_afinfo *afinfo;
  185. const struct xfrm_type **typemap;
  186. const struct xfrm_type *type;
  187. int modload_attempted = 0;
  188. retry:
  189. afinfo = xfrm_state_get_afinfo(family);
  190. if (unlikely(afinfo == NULL))
  191. return NULL;
  192. typemap = afinfo->type_map;
  193. type = typemap[proto];
  194. if (unlikely(type && !try_module_get(type->owner)))
  195. type = NULL;
  196. if (!type && !modload_attempted) {
  197. xfrm_state_put_afinfo(afinfo);
  198. request_module("xfrm-type-%d-%d", family, proto);
  199. modload_attempted = 1;
  200. goto retry;
  201. }
  202. xfrm_state_put_afinfo(afinfo);
  203. return type;
  204. }
  205. static void xfrm_put_type(const struct xfrm_type *type)
  206. {
  207. module_put(type->owner);
  208. }
  209. static DEFINE_SPINLOCK(xfrm_mode_lock);
  210. int xfrm_register_mode(struct xfrm_mode *mode, int family)
  211. {
  212. struct xfrm_state_afinfo *afinfo;
  213. struct xfrm_mode **modemap;
  214. int err;
  215. if (unlikely(mode->encap >= XFRM_MODE_MAX))
  216. return -EINVAL;
  217. afinfo = xfrm_state_get_afinfo(family);
  218. if (unlikely(afinfo == NULL))
  219. return -EAFNOSUPPORT;
  220. err = -EEXIST;
  221. modemap = afinfo->mode_map;
  222. spin_lock_bh(&xfrm_mode_lock);
  223. if (modemap[mode->encap])
  224. goto out;
  225. err = -ENOENT;
  226. if (!try_module_get(afinfo->owner))
  227. goto out;
  228. mode->afinfo = afinfo;
  229. modemap[mode->encap] = mode;
  230. err = 0;
  231. out:
  232. spin_unlock_bh(&xfrm_mode_lock);
  233. xfrm_state_put_afinfo(afinfo);
  234. return err;
  235. }
  236. EXPORT_SYMBOL(xfrm_register_mode);
  237. int xfrm_unregister_mode(struct xfrm_mode *mode, int family)
  238. {
  239. struct xfrm_state_afinfo *afinfo;
  240. struct xfrm_mode **modemap;
  241. int err;
  242. if (unlikely(mode->encap >= XFRM_MODE_MAX))
  243. return -EINVAL;
  244. afinfo = xfrm_state_get_afinfo(family);
  245. if (unlikely(afinfo == NULL))
  246. return -EAFNOSUPPORT;
  247. err = -ENOENT;
  248. modemap = afinfo->mode_map;
  249. spin_lock_bh(&xfrm_mode_lock);
  250. if (likely(modemap[mode->encap] == mode)) {
  251. modemap[mode->encap] = NULL;
  252. module_put(mode->afinfo->owner);
  253. err = 0;
  254. }
  255. spin_unlock_bh(&xfrm_mode_lock);
  256. xfrm_state_put_afinfo(afinfo);
  257. return err;
  258. }
  259. EXPORT_SYMBOL(xfrm_unregister_mode);
  260. static struct xfrm_mode *xfrm_get_mode(unsigned int encap, int family)
  261. {
  262. struct xfrm_state_afinfo *afinfo;
  263. struct xfrm_mode *mode;
  264. int modload_attempted = 0;
  265. if (unlikely(encap >= XFRM_MODE_MAX))
  266. return NULL;
  267. retry:
  268. afinfo = xfrm_state_get_afinfo(family);
  269. if (unlikely(afinfo == NULL))
  270. return NULL;
  271. mode = afinfo->mode_map[encap];
  272. if (unlikely(mode && !try_module_get(mode->owner)))
  273. mode = NULL;
  274. if (!mode && !modload_attempted) {
  275. xfrm_state_put_afinfo(afinfo);
  276. request_module("xfrm-mode-%d-%d", family, encap);
  277. modload_attempted = 1;
  278. goto retry;
  279. }
  280. xfrm_state_put_afinfo(afinfo);
  281. return mode;
  282. }
  283. static void xfrm_put_mode(struct xfrm_mode *mode)
  284. {
  285. module_put(mode->owner);
  286. }
  287. static void xfrm_state_gc_destroy(struct xfrm_state *x)
  288. {
  289. tasklet_hrtimer_cancel(&x->mtimer);
  290. del_timer_sync(&x->rtimer);
  291. kfree(x->aead);
  292. kfree(x->aalg);
  293. kfree(x->ealg);
  294. kfree(x->calg);
  295. kfree(x->encap);
  296. kfree(x->coaddr);
  297. kfree(x->replay_esn);
  298. kfree(x->preplay_esn);
  299. if (x->inner_mode)
  300. xfrm_put_mode(x->inner_mode);
  301. if (x->inner_mode_iaf)
  302. xfrm_put_mode(x->inner_mode_iaf);
  303. if (x->outer_mode)
  304. xfrm_put_mode(x->outer_mode);
  305. if (x->type) {
  306. x->type->destructor(x);
  307. xfrm_put_type(x->type);
  308. }
  309. security_xfrm_state_free(x);
  310. kfree(x);
  311. }
  312. static void xfrm_state_gc_task(struct work_struct *work)
  313. {
  314. struct xfrm_state *x;
  315. struct hlist_node *tmp;
  316. struct hlist_head gc_list;
  317. spin_lock_bh(&xfrm_state_gc_lock);
  318. hlist_move_list(&xfrm_state_gc_list, &gc_list);
  319. spin_unlock_bh(&xfrm_state_gc_lock);
  320. synchronize_rcu();
  321. hlist_for_each_entry_safe(x, tmp, &gc_list, gclist)
  322. xfrm_state_gc_destroy(x);
  323. }
  324. static inline unsigned long make_jiffies(long secs)
  325. {
  326. if (secs >= (MAX_SCHEDULE_TIMEOUT-1)/HZ)
  327. return MAX_SCHEDULE_TIMEOUT-1;
  328. else
  329. return secs*HZ;
  330. }
  331. static enum hrtimer_restart xfrm_timer_handler(struct hrtimer *me)
  332. {
  333. struct tasklet_hrtimer *thr = container_of(me, struct tasklet_hrtimer, timer);
  334. struct xfrm_state *x = container_of(thr, struct xfrm_state, mtimer);
  335. unsigned long now = get_seconds();
  336. long next = LONG_MAX;
  337. int warn = 0;
  338. int err = 0;
  339. spin_lock(&x->lock);
  340. if (x->km.state == XFRM_STATE_DEAD)
  341. goto out;
  342. if (x->km.state == XFRM_STATE_EXPIRED)
  343. goto expired;
  344. if (x->lft.hard_add_expires_seconds) {
  345. long tmo = x->lft.hard_add_expires_seconds +
  346. x->curlft.add_time - now;
  347. if (tmo <= 0) {
  348. if (x->xflags & XFRM_SOFT_EXPIRE) {
  349. /* enter hard expire without soft expire first?!
  350. * setting a new date could trigger this.
  351. * workarbound: fix x->curflt.add_time by below:
  352. */
  353. x->curlft.add_time = now - x->saved_tmo - 1;
  354. tmo = x->lft.hard_add_expires_seconds - x->saved_tmo;
  355. } else
  356. goto expired;
  357. }
  358. if (tmo < next)
  359. next = tmo;
  360. }
  361. if (x->lft.hard_use_expires_seconds) {
  362. long tmo = x->lft.hard_use_expires_seconds +
  363. (x->curlft.use_time ? : now) - now;
  364. if (tmo <= 0)
  365. goto expired;
  366. if (tmo < next)
  367. next = tmo;
  368. }
  369. if (x->km.dying)
  370. goto resched;
  371. if (x->lft.soft_add_expires_seconds) {
  372. long tmo = x->lft.soft_add_expires_seconds +
  373. x->curlft.add_time - now;
  374. if (tmo <= 0) {
  375. warn = 1;
  376. x->xflags &= ~XFRM_SOFT_EXPIRE;
  377. } else if (tmo < next) {
  378. next = tmo;
  379. x->xflags |= XFRM_SOFT_EXPIRE;
  380. x->saved_tmo = tmo;
  381. }
  382. }
  383. if (x->lft.soft_use_expires_seconds) {
  384. long tmo = x->lft.soft_use_expires_seconds +
  385. (x->curlft.use_time ? : now) - now;
  386. if (tmo <= 0)
  387. warn = 1;
  388. else if (tmo < next)
  389. next = tmo;
  390. }
  391. x->km.dying = warn;
  392. if (warn)
  393. km_state_expired(x, 0, 0);
  394. resched:
  395. if (next != LONG_MAX) {
  396. tasklet_hrtimer_start(&x->mtimer, ktime_set(next, 0), HRTIMER_MODE_REL);
  397. }
  398. goto out;
  399. expired:
  400. if (x->km.state == XFRM_STATE_ACQ && x->id.spi == 0)
  401. x->km.state = XFRM_STATE_EXPIRED;
  402. err = __xfrm_state_delete(x);
  403. if (!err)
  404. km_state_expired(x, 1, 0);
  405. xfrm_audit_state_delete(x, err ? 0 : 1, true);
  406. out:
  407. spin_unlock(&x->lock);
  408. return HRTIMER_NORESTART;
  409. }
  410. static void xfrm_replay_timer_handler(unsigned long data);
  411. struct xfrm_state *xfrm_state_alloc(struct net *net)
  412. {
  413. struct xfrm_state *x;
  414. x = kzalloc(sizeof(struct xfrm_state), GFP_ATOMIC);
  415. if (x) {
  416. write_pnet(&x->xs_net, net);
  417. atomic_set(&x->refcnt, 1);
  418. atomic_set(&x->tunnel_users, 0);
  419. INIT_LIST_HEAD(&x->km.all);
  420. INIT_HLIST_NODE(&x->bydst);
  421. INIT_HLIST_NODE(&x->bysrc);
  422. INIT_HLIST_NODE(&x->byspi);
  423. tasklet_hrtimer_init(&x->mtimer, xfrm_timer_handler,
  424. CLOCK_BOOTTIME, HRTIMER_MODE_ABS);
  425. setup_timer(&x->rtimer, xfrm_replay_timer_handler,
  426. (unsigned long)x);
  427. x->curlft.add_time = get_seconds();
  428. x->lft.soft_byte_limit = XFRM_INF;
  429. x->lft.soft_packet_limit = XFRM_INF;
  430. x->lft.hard_byte_limit = XFRM_INF;
  431. x->lft.hard_packet_limit = XFRM_INF;
  432. x->replay_maxage = 0;
  433. x->replay_maxdiff = 0;
  434. x->inner_mode = NULL;
  435. x->inner_mode_iaf = NULL;
  436. spin_lock_init(&x->lock);
  437. }
  438. return x;
  439. }
  440. EXPORT_SYMBOL(xfrm_state_alloc);
  441. void __xfrm_state_destroy(struct xfrm_state *x)
  442. {
  443. WARN_ON(x->km.state != XFRM_STATE_DEAD);
  444. spin_lock_bh(&xfrm_state_gc_lock);
  445. hlist_add_head(&x->gclist, &xfrm_state_gc_list);
  446. spin_unlock_bh(&xfrm_state_gc_lock);
  447. schedule_work(&xfrm_state_gc_work);
  448. }
  449. EXPORT_SYMBOL(__xfrm_state_destroy);
  450. int __xfrm_state_delete(struct xfrm_state *x)
  451. {
  452. struct net *net = xs_net(x);
  453. int err = -ESRCH;
  454. if (x->km.state != XFRM_STATE_DEAD) {
  455. x->km.state = XFRM_STATE_DEAD;
  456. spin_lock(&net->xfrm.xfrm_state_lock);
  457. list_del(&x->km.all);
  458. hlist_del_rcu(&x->bydst);
  459. hlist_del_rcu(&x->bysrc);
  460. if (x->id.spi)
  461. hlist_del_rcu(&x->byspi);
  462. net->xfrm.state_num--;
  463. spin_unlock(&net->xfrm.xfrm_state_lock);
  464. /* All xfrm_state objects are created by xfrm_state_alloc.
  465. * The xfrm_state_alloc call gives a reference, and that
  466. * is what we are dropping here.
  467. */
  468. xfrm_state_put(x);
  469. err = 0;
  470. }
  471. return err;
  472. }
  473. EXPORT_SYMBOL(__xfrm_state_delete);
  474. int xfrm_state_delete(struct xfrm_state *x)
  475. {
  476. int err;
  477. spin_lock_bh(&x->lock);
  478. err = __xfrm_state_delete(x);
  479. spin_unlock_bh(&x->lock);
  480. return err;
  481. }
  482. EXPORT_SYMBOL(xfrm_state_delete);
  483. #ifdef CONFIG_SECURITY_NETWORK_XFRM
  484. static inline int
  485. xfrm_state_flush_secctx_check(struct net *net, u8 proto, bool task_valid)
  486. {
  487. int i, err = 0;
  488. for (i = 0; i <= net->xfrm.state_hmask; i++) {
  489. struct xfrm_state *x;
  490. hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) {
  491. if (xfrm_id_proto_match(x->id.proto, proto) &&
  492. (err = security_xfrm_state_delete(x)) != 0) {
  493. xfrm_audit_state_delete(x, 0, task_valid);
  494. return err;
  495. }
  496. }
  497. }
  498. return err;
  499. }
  500. #else
  501. static inline int
  502. xfrm_state_flush_secctx_check(struct net *net, u8 proto, bool task_valid)
  503. {
  504. return 0;
  505. }
  506. #endif
  507. int xfrm_state_flush(struct net *net, u8 proto, bool task_valid)
  508. {
  509. int i, err = 0, cnt = 0;
  510. spin_lock_bh(&net->xfrm.xfrm_state_lock);
  511. err = xfrm_state_flush_secctx_check(net, proto, task_valid);
  512. if (err)
  513. goto out;
  514. err = -ESRCH;
  515. for (i = 0; i <= net->xfrm.state_hmask; i++) {
  516. struct xfrm_state *x;
  517. restart:
  518. hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) {
  519. if (!xfrm_state_kern(x) &&
  520. xfrm_id_proto_match(x->id.proto, proto)) {
  521. xfrm_state_hold(x);
  522. spin_unlock_bh(&net->xfrm.xfrm_state_lock);
  523. err = xfrm_state_delete(x);
  524. xfrm_audit_state_delete(x, err ? 0 : 1,
  525. task_valid);
  526. xfrm_state_put(x);
  527. if (!err)
  528. cnt++;
  529. spin_lock_bh(&net->xfrm.xfrm_state_lock);
  530. goto restart;
  531. }
  532. }
  533. }
  534. if (cnt)
  535. err = 0;
  536. out:
  537. spin_unlock_bh(&net->xfrm.xfrm_state_lock);
  538. return err;
  539. }
  540. EXPORT_SYMBOL(xfrm_state_flush);
  541. void xfrm_sad_getinfo(struct net *net, struct xfrmk_sadinfo *si)
  542. {
  543. spin_lock_bh(&net->xfrm.xfrm_state_lock);
  544. si->sadcnt = net->xfrm.state_num;
  545. si->sadhcnt = net->xfrm.state_hmask;
  546. si->sadhmcnt = xfrm_state_hashmax;
  547. spin_unlock_bh(&net->xfrm.xfrm_state_lock);
  548. }
  549. EXPORT_SYMBOL(xfrm_sad_getinfo);
  550. static int
  551. xfrm_init_tempstate(struct xfrm_state *x, const struct flowi *fl,
  552. const struct xfrm_tmpl *tmpl,
  553. const xfrm_address_t *daddr, const xfrm_address_t *saddr,
  554. unsigned short family)
  555. {
  556. struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
  557. if (!afinfo)
  558. return -1;
  559. afinfo->init_tempsel(&x->sel, fl);
  560. if (family != tmpl->encap_family) {
  561. xfrm_state_put_afinfo(afinfo);
  562. afinfo = xfrm_state_get_afinfo(tmpl->encap_family);
  563. if (!afinfo)
  564. return -1;
  565. }
  566. afinfo->init_temprop(x, tmpl, daddr, saddr);
  567. xfrm_state_put_afinfo(afinfo);
  568. return 0;
  569. }
  570. static struct xfrm_state *__xfrm_state_lookup(struct net *net, u32 mark,
  571. const xfrm_address_t *daddr,
  572. __be32 spi, u8 proto,
  573. unsigned short family)
  574. {
  575. unsigned int h = xfrm_spi_hash(net, daddr, spi, proto, family);
  576. struct xfrm_state *x;
  577. hlist_for_each_entry_rcu(x, net->xfrm.state_byspi + h, byspi) {
  578. if (x->props.family != family ||
  579. x->id.spi != spi ||
  580. x->id.proto != proto ||
  581. !xfrm_addr_equal(&x->id.daddr, daddr, family))
  582. continue;
  583. if ((mark & x->mark.m) != x->mark.v)
  584. continue;
  585. if (!xfrm_state_hold_rcu(x))
  586. continue;
  587. return x;
  588. }
  589. return NULL;
  590. }
  591. static struct xfrm_state *__xfrm_state_lookup_byaddr(struct net *net, u32 mark,
  592. const xfrm_address_t *daddr,
  593. const xfrm_address_t *saddr,
  594. u8 proto, unsigned short family)
  595. {
  596. unsigned int h = xfrm_src_hash(net, daddr, saddr, family);
  597. struct xfrm_state *x;
  598. hlist_for_each_entry_rcu(x, net->xfrm.state_bysrc + h, bysrc) {
  599. if (x->props.family != family ||
  600. x->id.proto != proto ||
  601. !xfrm_addr_equal(&x->id.daddr, daddr, family) ||
  602. !xfrm_addr_equal(&x->props.saddr, saddr, family))
  603. continue;
  604. if ((mark & x->mark.m) != x->mark.v)
  605. continue;
  606. if (!xfrm_state_hold_rcu(x))
  607. continue;
  608. return x;
  609. }
  610. return NULL;
  611. }
  612. static inline struct xfrm_state *
  613. __xfrm_state_locate(struct xfrm_state *x, int use_spi, int family)
  614. {
  615. struct net *net = xs_net(x);
  616. u32 mark = x->mark.v & x->mark.m;
  617. if (use_spi)
  618. return __xfrm_state_lookup(net, mark, &x->id.daddr,
  619. x->id.spi, x->id.proto, family);
  620. else
  621. return __xfrm_state_lookup_byaddr(net, mark,
  622. &x->id.daddr,
  623. &x->props.saddr,
  624. x->id.proto, family);
  625. }
  626. static void xfrm_hash_grow_check(struct net *net, int have_hash_collision)
  627. {
  628. if (have_hash_collision &&
  629. (net->xfrm.state_hmask + 1) < xfrm_state_hashmax &&
  630. net->xfrm.state_num > net->xfrm.state_hmask)
  631. schedule_work(&net->xfrm.state_hash_work);
  632. }
  633. static void xfrm_state_look_at(struct xfrm_policy *pol, struct xfrm_state *x,
  634. const struct flowi *fl, unsigned short family,
  635. struct xfrm_state **best, int *acq_in_progress,
  636. int *error)
  637. {
  638. /* Resolution logic:
  639. * 1. There is a valid state with matching selector. Done.
  640. * 2. Valid state with inappropriate selector. Skip.
  641. *
  642. * Entering area of "sysdeps".
  643. *
  644. * 3. If state is not valid, selector is temporary, it selects
  645. * only session which triggered previous resolution. Key
  646. * manager will do something to install a state with proper
  647. * selector.
  648. */
  649. if (x->km.state == XFRM_STATE_VALID) {
  650. if ((x->sel.family &&
  651. !xfrm_selector_match(&x->sel, fl, x->sel.family)) ||
  652. !security_xfrm_state_pol_flow_match(x, pol, fl))
  653. return;
  654. if (!*best ||
  655. (*best)->km.dying > x->km.dying ||
  656. ((*best)->km.dying == x->km.dying &&
  657. (*best)->curlft.add_time < x->curlft.add_time))
  658. *best = x;
  659. } else if (x->km.state == XFRM_STATE_ACQ) {
  660. *acq_in_progress = 1;
  661. } else if (x->km.state == XFRM_STATE_ERROR ||
  662. x->km.state == XFRM_STATE_EXPIRED) {
  663. if (xfrm_selector_match(&x->sel, fl, x->sel.family) &&
  664. security_xfrm_state_pol_flow_match(x, pol, fl))
  665. *error = -ESRCH;
  666. }
  667. }
  668. struct xfrm_state *
  669. xfrm_state_find(const xfrm_address_t *daddr, const xfrm_address_t *saddr,
  670. const struct flowi *fl, struct xfrm_tmpl *tmpl,
  671. struct xfrm_policy *pol, int *err,
  672. unsigned short family)
  673. {
  674. static xfrm_address_t saddr_wildcard = { };
  675. struct net *net = xp_net(pol);
  676. unsigned int h, h_wildcard;
  677. struct xfrm_state *x, *x0, *to_put;
  678. int acquire_in_progress = 0;
  679. int error = 0;
  680. struct xfrm_state *best = NULL;
  681. u32 mark = pol->mark.v & pol->mark.m;
  682. unsigned short encap_family = tmpl->encap_family;
  683. unsigned int sequence;
  684. struct km_event c;
  685. to_put = NULL;
  686. sequence = read_seqcount_begin(&xfrm_state_hash_generation);
  687. rcu_read_lock();
  688. h = xfrm_dst_hash(net, daddr, saddr, tmpl->reqid, encap_family);
  689. hlist_for_each_entry_rcu(x, net->xfrm.state_bydst + h, bydst) {
  690. if (x->props.family == encap_family &&
  691. x->props.reqid == tmpl->reqid &&
  692. (mark & x->mark.m) == x->mark.v &&
  693. !(x->props.flags & XFRM_STATE_WILDRECV) &&
  694. xfrm_state_addr_check(x, daddr, saddr, encap_family) &&
  695. tmpl->mode == x->props.mode &&
  696. tmpl->id.proto == x->id.proto &&
  697. (tmpl->id.spi == x->id.spi || !tmpl->id.spi))
  698. xfrm_state_look_at(pol, x, fl, encap_family,
  699. &best, &acquire_in_progress, &error);
  700. }
  701. if (best || acquire_in_progress)
  702. goto found;
  703. h_wildcard = xfrm_dst_hash(net, daddr, &saddr_wildcard, tmpl->reqid, encap_family);
  704. hlist_for_each_entry_rcu(x, net->xfrm.state_bydst + h_wildcard, bydst) {
  705. if (x->props.family == encap_family &&
  706. x->props.reqid == tmpl->reqid &&
  707. (mark & x->mark.m) == x->mark.v &&
  708. !(x->props.flags & XFRM_STATE_WILDRECV) &&
  709. xfrm_addr_equal(&x->id.daddr, daddr, encap_family) &&
  710. tmpl->mode == x->props.mode &&
  711. tmpl->id.proto == x->id.proto &&
  712. (tmpl->id.spi == x->id.spi || !tmpl->id.spi))
  713. xfrm_state_look_at(pol, x, fl, encap_family,
  714. &best, &acquire_in_progress, &error);
  715. }
  716. found:
  717. x = best;
  718. if (!x && !error && !acquire_in_progress) {
  719. if (tmpl->id.spi &&
  720. (x0 = __xfrm_state_lookup(net, mark, daddr, tmpl->id.spi,
  721. tmpl->id.proto, encap_family)) != NULL) {
  722. to_put = x0;
  723. error = -EEXIST;
  724. goto out;
  725. }
  726. c.net = net;
  727. /* If the KMs have no listeners (yet...), avoid allocating an SA
  728. * for each and every packet - garbage collection might not
  729. * handle the flood.
  730. */
  731. if (!km_is_alive(&c)) {
  732. error = -ESRCH;
  733. goto out;
  734. }
  735. x = xfrm_state_alloc(net);
  736. if (x == NULL) {
  737. error = -ENOMEM;
  738. goto out;
  739. }
  740. /* Initialize temporary state matching only
  741. * to current session. */
  742. xfrm_init_tempstate(x, fl, tmpl, daddr, saddr, family);
  743. memcpy(&x->mark, &pol->mark, sizeof(x->mark));
  744. error = security_xfrm_state_alloc_acquire(x, pol->security, fl->flowi_secid);
  745. if (error) {
  746. x->km.state = XFRM_STATE_DEAD;
  747. to_put = x;
  748. x = NULL;
  749. goto out;
  750. }
  751. if (km_query(x, tmpl, pol) == 0) {
  752. spin_lock_bh(&net->xfrm.xfrm_state_lock);
  753. x->km.state = XFRM_STATE_ACQ;
  754. list_add(&x->km.all, &net->xfrm.state_all);
  755. hlist_add_head_rcu(&x->bydst, net->xfrm.state_bydst + h);
  756. h = xfrm_src_hash(net, daddr, saddr, encap_family);
  757. hlist_add_head_rcu(&x->bysrc, net->xfrm.state_bysrc + h);
  758. if (x->id.spi) {
  759. h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto, encap_family);
  760. hlist_add_head_rcu(&x->byspi, net->xfrm.state_byspi + h);
  761. }
  762. x->lft.hard_add_expires_seconds = net->xfrm.sysctl_acq_expires;
  763. tasklet_hrtimer_start(&x->mtimer, ktime_set(net->xfrm.sysctl_acq_expires, 0), HRTIMER_MODE_REL);
  764. net->xfrm.state_num++;
  765. xfrm_hash_grow_check(net, x->bydst.next != NULL);
  766. spin_unlock_bh(&net->xfrm.xfrm_state_lock);
  767. } else {
  768. x->km.state = XFRM_STATE_DEAD;
  769. to_put = x;
  770. x = NULL;
  771. error = -ESRCH;
  772. }
  773. }
  774. out:
  775. if (x) {
  776. if (!xfrm_state_hold_rcu(x)) {
  777. *err = -EAGAIN;
  778. x = NULL;
  779. }
  780. } else {
  781. *err = acquire_in_progress ? -EAGAIN : error;
  782. }
  783. rcu_read_unlock();
  784. if (to_put)
  785. xfrm_state_put(to_put);
  786. if (read_seqcount_retry(&xfrm_state_hash_generation, sequence)) {
  787. *err = -EAGAIN;
  788. if (x) {
  789. xfrm_state_put(x);
  790. x = NULL;
  791. }
  792. }
  793. return x;
  794. }
  795. struct xfrm_state *
  796. xfrm_stateonly_find(struct net *net, u32 mark,
  797. xfrm_address_t *daddr, xfrm_address_t *saddr,
  798. unsigned short family, u8 mode, u8 proto, u32 reqid)
  799. {
  800. unsigned int h;
  801. struct xfrm_state *rx = NULL, *x = NULL;
  802. spin_lock_bh(&net->xfrm.xfrm_state_lock);
  803. h = xfrm_dst_hash(net, daddr, saddr, reqid, family);
  804. hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
  805. if (x->props.family == family &&
  806. x->props.reqid == reqid &&
  807. (mark & x->mark.m) == x->mark.v &&
  808. !(x->props.flags & XFRM_STATE_WILDRECV) &&
  809. xfrm_state_addr_check(x, daddr, saddr, family) &&
  810. mode == x->props.mode &&
  811. proto == x->id.proto &&
  812. x->km.state == XFRM_STATE_VALID) {
  813. rx = x;
  814. break;
  815. }
  816. }
  817. if (rx)
  818. xfrm_state_hold(rx);
  819. spin_unlock_bh(&net->xfrm.xfrm_state_lock);
  820. return rx;
  821. }
  822. EXPORT_SYMBOL(xfrm_stateonly_find);
  823. struct xfrm_state *xfrm_state_lookup_byspi(struct net *net, __be32 spi,
  824. unsigned short family)
  825. {
  826. struct xfrm_state *x;
  827. struct xfrm_state_walk *w;
  828. spin_lock_bh(&net->xfrm.xfrm_state_lock);
  829. list_for_each_entry(w, &net->xfrm.state_all, all) {
  830. x = container_of(w, struct xfrm_state, km);
  831. if (x->props.family != family ||
  832. x->id.spi != spi)
  833. continue;
  834. xfrm_state_hold(x);
  835. spin_unlock_bh(&net->xfrm.xfrm_state_lock);
  836. return x;
  837. }
  838. spin_unlock_bh(&net->xfrm.xfrm_state_lock);
  839. return NULL;
  840. }
  841. EXPORT_SYMBOL(xfrm_state_lookup_byspi);
  842. static void __xfrm_state_insert(struct xfrm_state *x)
  843. {
  844. struct net *net = xs_net(x);
  845. unsigned int h;
  846. list_add(&x->km.all, &net->xfrm.state_all);
  847. h = xfrm_dst_hash(net, &x->id.daddr, &x->props.saddr,
  848. x->props.reqid, x->props.family);
  849. hlist_add_head_rcu(&x->bydst, net->xfrm.state_bydst + h);
  850. h = xfrm_src_hash(net, &x->id.daddr, &x->props.saddr, x->props.family);
  851. hlist_add_head_rcu(&x->bysrc, net->xfrm.state_bysrc + h);
  852. if (x->id.spi) {
  853. h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto,
  854. x->props.family);
  855. hlist_add_head_rcu(&x->byspi, net->xfrm.state_byspi + h);
  856. }
  857. tasklet_hrtimer_start(&x->mtimer, ktime_set(1, 0), HRTIMER_MODE_REL);
  858. if (x->replay_maxage)
  859. mod_timer(&x->rtimer, jiffies + x->replay_maxage);
  860. net->xfrm.state_num++;
  861. xfrm_hash_grow_check(net, x->bydst.next != NULL);
  862. }
  863. /* net->xfrm.xfrm_state_lock is held */
  864. static void __xfrm_state_bump_genids(struct xfrm_state *xnew)
  865. {
  866. struct net *net = xs_net(xnew);
  867. unsigned short family = xnew->props.family;
  868. u32 reqid = xnew->props.reqid;
  869. struct xfrm_state *x;
  870. unsigned int h;
  871. u32 mark = xnew->mark.v & xnew->mark.m;
  872. h = xfrm_dst_hash(net, &xnew->id.daddr, &xnew->props.saddr, reqid, family);
  873. hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
  874. if (x->props.family == family &&
  875. x->props.reqid == reqid &&
  876. (mark & x->mark.m) == x->mark.v &&
  877. xfrm_addr_equal(&x->id.daddr, &xnew->id.daddr, family) &&
  878. xfrm_addr_equal(&x->props.saddr, &xnew->props.saddr, family))
  879. x->genid++;
  880. }
  881. }
  882. void xfrm_state_insert(struct xfrm_state *x)
  883. {
  884. struct net *net = xs_net(x);
  885. spin_lock_bh(&net->xfrm.xfrm_state_lock);
  886. __xfrm_state_bump_genids(x);
  887. __xfrm_state_insert(x);
  888. spin_unlock_bh(&net->xfrm.xfrm_state_lock);
  889. }
  890. EXPORT_SYMBOL(xfrm_state_insert);
  891. /* net->xfrm.xfrm_state_lock is held */
  892. static struct xfrm_state *__find_acq_core(struct net *net,
  893. const struct xfrm_mark *m,
  894. unsigned short family, u8 mode,
  895. u32 reqid, u8 proto,
  896. const xfrm_address_t *daddr,
  897. const xfrm_address_t *saddr,
  898. int create)
  899. {
  900. unsigned int h = xfrm_dst_hash(net, daddr, saddr, reqid, family);
  901. struct xfrm_state *x;
  902. u32 mark = m->v & m->m;
  903. hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
  904. if (x->props.reqid != reqid ||
  905. x->props.mode != mode ||
  906. x->props.family != family ||
  907. x->km.state != XFRM_STATE_ACQ ||
  908. x->id.spi != 0 ||
  909. x->id.proto != proto ||
  910. (mark & x->mark.m) != x->mark.v ||
  911. !xfrm_addr_equal(&x->id.daddr, daddr, family) ||
  912. !xfrm_addr_equal(&x->props.saddr, saddr, family))
  913. continue;
  914. xfrm_state_hold(x);
  915. return x;
  916. }
  917. if (!create)
  918. return NULL;
  919. x = xfrm_state_alloc(net);
  920. if (likely(x)) {
  921. switch (family) {
  922. case AF_INET:
  923. x->sel.daddr.a4 = daddr->a4;
  924. x->sel.saddr.a4 = saddr->a4;
  925. x->sel.prefixlen_d = 32;
  926. x->sel.prefixlen_s = 32;
  927. x->props.saddr.a4 = saddr->a4;
  928. x->id.daddr.a4 = daddr->a4;
  929. break;
  930. case AF_INET6:
  931. x->sel.daddr.in6 = daddr->in6;
  932. x->sel.saddr.in6 = saddr->in6;
  933. x->sel.prefixlen_d = 128;
  934. x->sel.prefixlen_s = 128;
  935. x->props.saddr.in6 = saddr->in6;
  936. x->id.daddr.in6 = daddr->in6;
  937. break;
  938. }
  939. x->km.state = XFRM_STATE_ACQ;
  940. x->id.proto = proto;
  941. x->props.family = family;
  942. x->props.mode = mode;
  943. x->props.reqid = reqid;
  944. x->mark.v = m->v;
  945. x->mark.m = m->m;
  946. x->lft.hard_add_expires_seconds = net->xfrm.sysctl_acq_expires;
  947. xfrm_state_hold(x);
  948. tasklet_hrtimer_start(&x->mtimer, ktime_set(net->xfrm.sysctl_acq_expires, 0), HRTIMER_MODE_REL);
  949. list_add(&x->km.all, &net->xfrm.state_all);
  950. hlist_add_head_rcu(&x->bydst, net->xfrm.state_bydst + h);
  951. h = xfrm_src_hash(net, daddr, saddr, family);
  952. hlist_add_head_rcu(&x->bysrc, net->xfrm.state_bysrc + h);
  953. net->xfrm.state_num++;
  954. xfrm_hash_grow_check(net, x->bydst.next != NULL);
  955. }
  956. return x;
  957. }
  958. static struct xfrm_state *__xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq);
  959. int xfrm_state_add(struct xfrm_state *x)
  960. {
  961. struct net *net = xs_net(x);
  962. struct xfrm_state *x1, *to_put;
  963. int family;
  964. int err;
  965. u32 mark = x->mark.v & x->mark.m;
  966. int use_spi = xfrm_id_proto_match(x->id.proto, IPSEC_PROTO_ANY);
  967. family = x->props.family;
  968. to_put = NULL;
  969. spin_lock_bh(&net->xfrm.xfrm_state_lock);
  970. x1 = __xfrm_state_locate(x, use_spi, family);
  971. if (x1) {
  972. to_put = x1;
  973. x1 = NULL;
  974. err = -EEXIST;
  975. goto out;
  976. }
  977. if (use_spi && x->km.seq) {
  978. x1 = __xfrm_find_acq_byseq(net, mark, x->km.seq);
  979. if (x1 && ((x1->id.proto != x->id.proto) ||
  980. !xfrm_addr_equal(&x1->id.daddr, &x->id.daddr, family))) {
  981. to_put = x1;
  982. x1 = NULL;
  983. }
  984. }
  985. if (use_spi && !x1)
  986. x1 = __find_acq_core(net, &x->mark, family, x->props.mode,
  987. x->props.reqid, x->id.proto,
  988. &x->id.daddr, &x->props.saddr, 0);
  989. __xfrm_state_bump_genids(x);
  990. __xfrm_state_insert(x);
  991. err = 0;
  992. out:
  993. spin_unlock_bh(&net->xfrm.xfrm_state_lock);
  994. if (x1) {
  995. xfrm_state_delete(x1);
  996. xfrm_state_put(x1);
  997. }
  998. if (to_put)
  999. xfrm_state_put(to_put);
  1000. return err;
  1001. }
  1002. EXPORT_SYMBOL(xfrm_state_add);
  1003. #ifdef CONFIG_XFRM_MIGRATE
  1004. static struct xfrm_state *xfrm_state_clone(struct xfrm_state *orig)
  1005. {
  1006. struct net *net = xs_net(orig);
  1007. struct xfrm_state *x = xfrm_state_alloc(net);
  1008. if (!x)
  1009. goto out;
  1010. memcpy(&x->id, &orig->id, sizeof(x->id));
  1011. memcpy(&x->sel, &orig->sel, sizeof(x->sel));
  1012. memcpy(&x->lft, &orig->lft, sizeof(x->lft));
  1013. x->props.mode = orig->props.mode;
  1014. x->props.replay_window = orig->props.replay_window;
  1015. x->props.reqid = orig->props.reqid;
  1016. x->props.family = orig->props.family;
  1017. x->props.saddr = orig->props.saddr;
  1018. if (orig->aalg) {
  1019. x->aalg = xfrm_algo_auth_clone(orig->aalg);
  1020. if (!x->aalg)
  1021. goto error;
  1022. }
  1023. x->props.aalgo = orig->props.aalgo;
  1024. if (orig->aead) {
  1025. x->aead = xfrm_algo_aead_clone(orig->aead);
  1026. if (!x->aead)
  1027. goto error;
  1028. }
  1029. if (orig->ealg) {
  1030. x->ealg = xfrm_algo_clone(orig->ealg);
  1031. if (!x->ealg)
  1032. goto error;
  1033. }
  1034. x->props.ealgo = orig->props.ealgo;
  1035. if (orig->calg) {
  1036. x->calg = xfrm_algo_clone(orig->calg);
  1037. if (!x->calg)
  1038. goto error;
  1039. }
  1040. x->props.calgo = orig->props.calgo;
  1041. if (orig->encap) {
  1042. x->encap = kmemdup(orig->encap, sizeof(*x->encap), GFP_KERNEL);
  1043. if (!x->encap)
  1044. goto error;
  1045. }
  1046. if (orig->coaddr) {
  1047. x->coaddr = kmemdup(orig->coaddr, sizeof(*x->coaddr),
  1048. GFP_KERNEL);
  1049. if (!x->coaddr)
  1050. goto error;
  1051. }
  1052. if (orig->replay_esn) {
  1053. if (xfrm_replay_clone(x, orig))
  1054. goto error;
  1055. }
  1056. memcpy(&x->mark, &orig->mark, sizeof(x->mark));
  1057. if (xfrm_init_state(x) < 0)
  1058. goto error;
  1059. x->props.flags = orig->props.flags;
  1060. x->props.extra_flags = orig->props.extra_flags;
  1061. x->tfcpad = orig->tfcpad;
  1062. x->replay_maxdiff = orig->replay_maxdiff;
  1063. x->replay_maxage = orig->replay_maxage;
  1064. x->curlft.add_time = orig->curlft.add_time;
  1065. x->km.state = orig->km.state;
  1066. x->km.seq = orig->km.seq;
  1067. return x;
  1068. error:
  1069. xfrm_state_put(x);
  1070. out:
  1071. return NULL;
  1072. }
  1073. struct xfrm_state *xfrm_migrate_state_find(struct xfrm_migrate *m, struct net *net)
  1074. {
  1075. unsigned int h;
  1076. struct xfrm_state *x = NULL;
  1077. spin_lock_bh(&net->xfrm.xfrm_state_lock);
  1078. if (m->reqid) {
  1079. h = xfrm_dst_hash(net, &m->old_daddr, &m->old_saddr,
  1080. m->reqid, m->old_family);
  1081. hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
  1082. if (x->props.mode != m->mode ||
  1083. x->id.proto != m->proto)
  1084. continue;
  1085. if (m->reqid && x->props.reqid != m->reqid)
  1086. continue;
  1087. if (!xfrm_addr_equal(&x->id.daddr, &m->old_daddr,
  1088. m->old_family) ||
  1089. !xfrm_addr_equal(&x->props.saddr, &m->old_saddr,
  1090. m->old_family))
  1091. continue;
  1092. xfrm_state_hold(x);
  1093. break;
  1094. }
  1095. } else {
  1096. h = xfrm_src_hash(net, &m->old_daddr, &m->old_saddr,
  1097. m->old_family);
  1098. hlist_for_each_entry(x, net->xfrm.state_bysrc+h, bysrc) {
  1099. if (x->props.mode != m->mode ||
  1100. x->id.proto != m->proto)
  1101. continue;
  1102. if (!xfrm_addr_equal(&x->id.daddr, &m->old_daddr,
  1103. m->old_family) ||
  1104. !xfrm_addr_equal(&x->props.saddr, &m->old_saddr,
  1105. m->old_family))
  1106. continue;
  1107. xfrm_state_hold(x);
  1108. break;
  1109. }
  1110. }
  1111. spin_unlock_bh(&net->xfrm.xfrm_state_lock);
  1112. return x;
  1113. }
  1114. EXPORT_SYMBOL(xfrm_migrate_state_find);
  1115. struct xfrm_state *xfrm_state_migrate(struct xfrm_state *x,
  1116. struct xfrm_migrate *m)
  1117. {
  1118. struct xfrm_state *xc;
  1119. xc = xfrm_state_clone(x);
  1120. if (!xc)
  1121. return NULL;
  1122. memcpy(&xc->id.daddr, &m->new_daddr, sizeof(xc->id.daddr));
  1123. memcpy(&xc->props.saddr, &m->new_saddr, sizeof(xc->props.saddr));
  1124. /* add state */
  1125. if (xfrm_addr_equal(&x->id.daddr, &m->new_daddr, m->new_family)) {
  1126. /* a care is needed when the destination address of the
  1127. state is to be updated as it is a part of triplet */
  1128. xfrm_state_insert(xc);
  1129. } else {
  1130. if (xfrm_state_add(xc) < 0)
  1131. goto error;
  1132. }
  1133. return xc;
  1134. error:
  1135. xfrm_state_put(xc);
  1136. return NULL;
  1137. }
  1138. EXPORT_SYMBOL(xfrm_state_migrate);
  1139. #endif
  1140. int xfrm_state_update(struct xfrm_state *x)
  1141. {
  1142. struct xfrm_state *x1, *to_put;
  1143. int err;
  1144. int use_spi = xfrm_id_proto_match(x->id.proto, IPSEC_PROTO_ANY);
  1145. struct net *net = xs_net(x);
  1146. to_put = NULL;
  1147. spin_lock_bh(&net->xfrm.xfrm_state_lock);
  1148. x1 = __xfrm_state_locate(x, use_spi, x->props.family);
  1149. err = -ESRCH;
  1150. if (!x1)
  1151. goto out;
  1152. if (xfrm_state_kern(x1)) {
  1153. to_put = x1;
  1154. err = -EEXIST;
  1155. goto out;
  1156. }
  1157. if (x1->km.state == XFRM_STATE_ACQ) {
  1158. __xfrm_state_insert(x);
  1159. x = NULL;
  1160. }
  1161. err = 0;
  1162. out:
  1163. spin_unlock_bh(&net->xfrm.xfrm_state_lock);
  1164. if (to_put)
  1165. xfrm_state_put(to_put);
  1166. if (err)
  1167. return err;
  1168. if (!x) {
  1169. xfrm_state_delete(x1);
  1170. xfrm_state_put(x1);
  1171. return 0;
  1172. }
  1173. err = -EINVAL;
  1174. spin_lock_bh(&x1->lock);
  1175. if (likely(x1->km.state == XFRM_STATE_VALID)) {
  1176. if (x->encap && x1->encap)
  1177. memcpy(x1->encap, x->encap, sizeof(*x1->encap));
  1178. if (x->coaddr && x1->coaddr) {
  1179. memcpy(x1->coaddr, x->coaddr, sizeof(*x1->coaddr));
  1180. }
  1181. if (!use_spi && memcmp(&x1->sel, &x->sel, sizeof(x1->sel)))
  1182. memcpy(&x1->sel, &x->sel, sizeof(x1->sel));
  1183. memcpy(&x1->lft, &x->lft, sizeof(x1->lft));
  1184. x1->km.dying = 0;
  1185. tasklet_hrtimer_start(&x1->mtimer, ktime_set(1, 0), HRTIMER_MODE_REL);
  1186. if (x1->curlft.use_time)
  1187. xfrm_state_check_expire(x1);
  1188. err = 0;
  1189. x->km.state = XFRM_STATE_DEAD;
  1190. __xfrm_state_put(x);
  1191. }
  1192. spin_unlock_bh(&x1->lock);
  1193. xfrm_state_put(x1);
  1194. return err;
  1195. }
  1196. EXPORT_SYMBOL(xfrm_state_update);
  1197. int xfrm_state_check_expire(struct xfrm_state *x)
  1198. {
  1199. if (!x->curlft.use_time)
  1200. x->curlft.use_time = get_seconds();
  1201. if (x->curlft.bytes >= x->lft.hard_byte_limit ||
  1202. x->curlft.packets >= x->lft.hard_packet_limit) {
  1203. x->km.state = XFRM_STATE_EXPIRED;
  1204. tasklet_hrtimer_start(&x->mtimer, ktime_set(0, 0), HRTIMER_MODE_REL);
  1205. return -EINVAL;
  1206. }
  1207. if (!x->km.dying &&
  1208. (x->curlft.bytes >= x->lft.soft_byte_limit ||
  1209. x->curlft.packets >= x->lft.soft_packet_limit)) {
  1210. x->km.dying = 1;
  1211. km_state_expired(x, 0, 0);
  1212. }
  1213. return 0;
  1214. }
  1215. EXPORT_SYMBOL(xfrm_state_check_expire);
  1216. struct xfrm_state *
  1217. xfrm_state_lookup(struct net *net, u32 mark, const xfrm_address_t *daddr, __be32 spi,
  1218. u8 proto, unsigned short family)
  1219. {
  1220. struct xfrm_state *x;
  1221. rcu_read_lock();
  1222. x = __xfrm_state_lookup(net, mark, daddr, spi, proto, family);
  1223. rcu_read_unlock();
  1224. return x;
  1225. }
  1226. EXPORT_SYMBOL(xfrm_state_lookup);
  1227. struct xfrm_state *
  1228. xfrm_state_lookup_byaddr(struct net *net, u32 mark,
  1229. const xfrm_address_t *daddr, const xfrm_address_t *saddr,
  1230. u8 proto, unsigned short family)
  1231. {
  1232. struct xfrm_state *x;
  1233. spin_lock_bh(&net->xfrm.xfrm_state_lock);
  1234. x = __xfrm_state_lookup_byaddr(net, mark, daddr, saddr, proto, family);
  1235. spin_unlock_bh(&net->xfrm.xfrm_state_lock);
  1236. return x;
  1237. }
  1238. EXPORT_SYMBOL(xfrm_state_lookup_byaddr);
  1239. struct xfrm_state *
  1240. xfrm_find_acq(struct net *net, const struct xfrm_mark *mark, u8 mode, u32 reqid,
  1241. u8 proto, const xfrm_address_t *daddr,
  1242. const xfrm_address_t *saddr, int create, unsigned short family)
  1243. {
  1244. struct xfrm_state *x;
  1245. spin_lock_bh(&net->xfrm.xfrm_state_lock);
  1246. x = __find_acq_core(net, mark, family, mode, reqid, proto, daddr, saddr, create);
  1247. spin_unlock_bh(&net->xfrm.xfrm_state_lock);
  1248. return x;
  1249. }
  1250. EXPORT_SYMBOL(xfrm_find_acq);
  1251. #ifdef CONFIG_XFRM_SUB_POLICY
  1252. int
  1253. xfrm_tmpl_sort(struct xfrm_tmpl **dst, struct xfrm_tmpl **src, int n,
  1254. unsigned short family, struct net *net)
  1255. {
  1256. int err = 0;
  1257. struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
  1258. if (!afinfo)
  1259. return -EAFNOSUPPORT;
  1260. spin_lock_bh(&net->xfrm.xfrm_state_lock); /*FIXME*/
  1261. if (afinfo->tmpl_sort)
  1262. err = afinfo->tmpl_sort(dst, src, n);
  1263. spin_unlock_bh(&net->xfrm.xfrm_state_lock);
  1264. xfrm_state_put_afinfo(afinfo);
  1265. return err;
  1266. }
  1267. EXPORT_SYMBOL(xfrm_tmpl_sort);
  1268. int
  1269. xfrm_state_sort(struct xfrm_state **dst, struct xfrm_state **src, int n,
  1270. unsigned short family)
  1271. {
  1272. int err = 0;
  1273. struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
  1274. struct net *net = xs_net(*src);
  1275. if (!afinfo)
  1276. return -EAFNOSUPPORT;
  1277. spin_lock_bh(&net->xfrm.xfrm_state_lock);
  1278. if (afinfo->state_sort)
  1279. err = afinfo->state_sort(dst, src, n);
  1280. spin_unlock_bh(&net->xfrm.xfrm_state_lock);
  1281. xfrm_state_put_afinfo(afinfo);
  1282. return err;
  1283. }
  1284. EXPORT_SYMBOL(xfrm_state_sort);
  1285. #endif
  1286. /* Silly enough, but I'm lazy to build resolution list */
  1287. static struct xfrm_state *__xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq)
  1288. {
  1289. int i;
  1290. for (i = 0; i <= net->xfrm.state_hmask; i++) {
  1291. struct xfrm_state *x;
  1292. hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) {
  1293. if (x->km.seq == seq &&
  1294. (mark & x->mark.m) == x->mark.v &&
  1295. x->km.state == XFRM_STATE_ACQ) {
  1296. xfrm_state_hold(x);
  1297. return x;
  1298. }
  1299. }
  1300. }
  1301. return NULL;
  1302. }
  1303. struct xfrm_state *xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq)
  1304. {
  1305. struct xfrm_state *x;
  1306. spin_lock_bh(&net->xfrm.xfrm_state_lock);
  1307. x = __xfrm_find_acq_byseq(net, mark, seq);
  1308. spin_unlock_bh(&net->xfrm.xfrm_state_lock);
  1309. return x;
  1310. }
  1311. EXPORT_SYMBOL(xfrm_find_acq_byseq);
  1312. u32 xfrm_get_acqseq(void)
  1313. {
  1314. u32 res;
  1315. static atomic_t acqseq;
  1316. do {
  1317. res = atomic_inc_return(&acqseq);
  1318. } while (!res);
  1319. return res;
  1320. }
  1321. EXPORT_SYMBOL(xfrm_get_acqseq);
  1322. int verify_spi_info(u8 proto, u32 min, u32 max)
  1323. {
  1324. switch (proto) {
  1325. case IPPROTO_AH:
  1326. case IPPROTO_ESP:
  1327. break;
  1328. case IPPROTO_COMP:
  1329. /* IPCOMP spi is 16-bits. */
  1330. if (max >= 0x10000)
  1331. return -EINVAL;
  1332. break;
  1333. default:
  1334. return -EINVAL;
  1335. }
  1336. if (min > max)
  1337. return -EINVAL;
  1338. return 0;
  1339. }
  1340. EXPORT_SYMBOL(verify_spi_info);
  1341. int xfrm_alloc_spi(struct xfrm_state *x, u32 low, u32 high)
  1342. {
  1343. struct net *net = xs_net(x);
  1344. unsigned int h;
  1345. struct xfrm_state *x0;
  1346. int err = -ENOENT;
  1347. __be32 minspi = htonl(low);
  1348. __be32 maxspi = htonl(high);
  1349. u32 mark = x->mark.v & x->mark.m;
  1350. spin_lock_bh(&x->lock);
  1351. if (x->km.state == XFRM_STATE_DEAD)
  1352. goto unlock;
  1353. err = 0;
  1354. if (x->id.spi)
  1355. goto unlock;
  1356. err = -ENOENT;
  1357. if (minspi == maxspi) {
  1358. x0 = xfrm_state_lookup(net, mark, &x->id.daddr, minspi, x->id.proto, x->props.family);
  1359. if (x0) {
  1360. xfrm_state_put(x0);
  1361. goto unlock;
  1362. }
  1363. x->id.spi = minspi;
  1364. } else {
  1365. u32 spi = 0;
  1366. for (h = 0; h < high-low+1; h++) {
  1367. spi = low + prandom_u32()%(high-low+1);
  1368. x0 = xfrm_state_lookup(net, mark, &x->id.daddr, htonl(spi), x->id.proto, x->props.family);
  1369. if (x0 == NULL) {
  1370. x->id.spi = htonl(spi);
  1371. break;
  1372. }
  1373. xfrm_state_put(x0);
  1374. }
  1375. }
  1376. if (x->id.spi) {
  1377. spin_lock_bh(&net->xfrm.xfrm_state_lock);
  1378. h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto, x->props.family);
  1379. hlist_add_head_rcu(&x->byspi, net->xfrm.state_byspi + h);
  1380. spin_unlock_bh(&net->xfrm.xfrm_state_lock);
  1381. err = 0;
  1382. }
  1383. unlock:
  1384. spin_unlock_bh(&x->lock);
  1385. return err;
  1386. }
  1387. EXPORT_SYMBOL(xfrm_alloc_spi);
  1388. static bool __xfrm_state_filter_match(struct xfrm_state *x,
  1389. struct xfrm_address_filter *filter)
  1390. {
  1391. if (filter) {
  1392. if ((filter->family == AF_INET ||
  1393. filter->family == AF_INET6) &&
  1394. x->props.family != filter->family)
  1395. return false;
  1396. return addr_match(&x->props.saddr, &filter->saddr,
  1397. filter->splen) &&
  1398. addr_match(&x->id.daddr, &filter->daddr,
  1399. filter->dplen);
  1400. }
  1401. return true;
  1402. }
  1403. int xfrm_state_walk(struct net *net, struct xfrm_state_walk *walk,
  1404. int (*func)(struct xfrm_state *, int, void*),
  1405. void *data)
  1406. {
  1407. struct xfrm_state *state;
  1408. struct xfrm_state_walk *x;
  1409. int err = 0;
  1410. if (walk->seq != 0 && list_empty(&walk->all))
  1411. return 0;
  1412. spin_lock_bh(&net->xfrm.xfrm_state_lock);
  1413. if (list_empty(&walk->all))
  1414. x = list_first_entry(&net->xfrm.state_all, struct xfrm_state_walk, all);
  1415. else
  1416. x = list_first_entry(&walk->all, struct xfrm_state_walk, all);
  1417. list_for_each_entry_from(x, &net->xfrm.state_all, all) {
  1418. if (x->state == XFRM_STATE_DEAD)
  1419. continue;
  1420. state = container_of(x, struct xfrm_state, km);
  1421. if (!xfrm_id_proto_match(state->id.proto, walk->proto))
  1422. continue;
  1423. if (!__xfrm_state_filter_match(state, walk->filter))
  1424. continue;
  1425. err = func(state, walk->seq, data);
  1426. if (err) {
  1427. list_move_tail(&walk->all, &x->all);
  1428. goto out;
  1429. }
  1430. walk->seq++;
  1431. }
  1432. if (walk->seq == 0) {
  1433. err = -ENOENT;
  1434. goto out;
  1435. }
  1436. list_del_init(&walk->all);
  1437. out:
  1438. spin_unlock_bh(&net->xfrm.xfrm_state_lock);
  1439. return err;
  1440. }
  1441. EXPORT_SYMBOL(xfrm_state_walk);
  1442. void xfrm_state_walk_init(struct xfrm_state_walk *walk, u8 proto,
  1443. struct xfrm_address_filter *filter)
  1444. {
  1445. INIT_LIST_HEAD(&walk->all);
  1446. walk->proto = proto;
  1447. walk->state = XFRM_STATE_DEAD;
  1448. walk->seq = 0;
  1449. walk->filter = filter;
  1450. }
  1451. EXPORT_SYMBOL(xfrm_state_walk_init);
  1452. void xfrm_state_walk_done(struct xfrm_state_walk *walk, struct net *net)
  1453. {
  1454. kfree(walk->filter);
  1455. if (list_empty(&walk->all))
  1456. return;
  1457. spin_lock_bh(&net->xfrm.xfrm_state_lock);
  1458. list_del(&walk->all);
  1459. spin_unlock_bh(&net->xfrm.xfrm_state_lock);
  1460. }
  1461. EXPORT_SYMBOL(xfrm_state_walk_done);
  1462. static void xfrm_replay_timer_handler(unsigned long data)
  1463. {
  1464. struct xfrm_state *x = (struct xfrm_state *)data;
  1465. spin_lock(&x->lock);
  1466. if (x->km.state == XFRM_STATE_VALID) {
  1467. if (xfrm_aevent_is_on(xs_net(x)))
  1468. x->repl->notify(x, XFRM_REPLAY_TIMEOUT);
  1469. else
  1470. x->xflags |= XFRM_TIME_DEFER;
  1471. }
  1472. spin_unlock(&x->lock);
  1473. }
  1474. static LIST_HEAD(xfrm_km_list);
  1475. void km_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
  1476. {
  1477. struct xfrm_mgr *km;
  1478. rcu_read_lock();
  1479. list_for_each_entry_rcu(km, &xfrm_km_list, list)
  1480. if (km->notify_policy)
  1481. km->notify_policy(xp, dir, c);
  1482. rcu_read_unlock();
  1483. }
  1484. void km_state_notify(struct xfrm_state *x, const struct km_event *c)
  1485. {
  1486. struct xfrm_mgr *km;
  1487. rcu_read_lock();
  1488. list_for_each_entry_rcu(km, &xfrm_km_list, list)
  1489. if (km->notify)
  1490. km->notify(x, c);
  1491. rcu_read_unlock();
  1492. }
  1493. EXPORT_SYMBOL(km_policy_notify);
  1494. EXPORT_SYMBOL(km_state_notify);
  1495. void km_state_expired(struct xfrm_state *x, int hard, u32 portid)
  1496. {
  1497. struct km_event c;
  1498. c.data.hard = hard;
  1499. c.portid = portid;
  1500. c.event = XFRM_MSG_EXPIRE;
  1501. km_state_notify(x, &c);
  1502. }
  1503. EXPORT_SYMBOL(km_state_expired);
  1504. /*
  1505. * We send to all registered managers regardless of failure
  1506. * We are happy with one success
  1507. */
  1508. int km_query(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *pol)
  1509. {
  1510. int err = -EINVAL, acqret;
  1511. struct xfrm_mgr *km;
  1512. rcu_read_lock();
  1513. list_for_each_entry_rcu(km, &xfrm_km_list, list) {
  1514. acqret = km->acquire(x, t, pol);
  1515. if (!acqret)
  1516. err = acqret;
  1517. }
  1518. rcu_read_unlock();
  1519. return err;
  1520. }
  1521. EXPORT_SYMBOL(km_query);
  1522. int km_new_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr, __be16 sport)
  1523. {
  1524. int err = -EINVAL;
  1525. struct xfrm_mgr *km;
  1526. rcu_read_lock();
  1527. list_for_each_entry_rcu(km, &xfrm_km_list, list) {
  1528. if (km->new_mapping)
  1529. err = km->new_mapping(x, ipaddr, sport);
  1530. if (!err)
  1531. break;
  1532. }
  1533. rcu_read_unlock();
  1534. return err;
  1535. }
  1536. EXPORT_SYMBOL(km_new_mapping);
  1537. void km_policy_expired(struct xfrm_policy *pol, int dir, int hard, u32 portid)
  1538. {
  1539. struct km_event c;
  1540. c.data.hard = hard;
  1541. c.portid = portid;
  1542. c.event = XFRM_MSG_POLEXPIRE;
  1543. km_policy_notify(pol, dir, &c);
  1544. }
  1545. EXPORT_SYMBOL(km_policy_expired);
  1546. #ifdef CONFIG_XFRM_MIGRATE
  1547. int km_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
  1548. const struct xfrm_migrate *m, int num_migrate,
  1549. const struct xfrm_kmaddress *k)
  1550. {
  1551. int err = -EINVAL;
  1552. int ret;
  1553. struct xfrm_mgr *km;
  1554. rcu_read_lock();
  1555. list_for_each_entry_rcu(km, &xfrm_km_list, list) {
  1556. if (km->migrate) {
  1557. ret = km->migrate(sel, dir, type, m, num_migrate, k);
  1558. if (!ret)
  1559. err = ret;
  1560. }
  1561. }
  1562. rcu_read_unlock();
  1563. return err;
  1564. }
  1565. EXPORT_SYMBOL(km_migrate);
  1566. #endif
  1567. int km_report(struct net *net, u8 proto, struct xfrm_selector *sel, xfrm_address_t *addr)
  1568. {
  1569. int err = -EINVAL;
  1570. int ret;
  1571. struct xfrm_mgr *km;
  1572. rcu_read_lock();
  1573. list_for_each_entry_rcu(km, &xfrm_km_list, list) {
  1574. if (km->report) {
  1575. ret = km->report(net, proto, sel, addr);
  1576. if (!ret)
  1577. err = ret;
  1578. }
  1579. }
  1580. rcu_read_unlock();
  1581. return err;
  1582. }
  1583. EXPORT_SYMBOL(km_report);
  1584. bool km_is_alive(const struct km_event *c)
  1585. {
  1586. struct xfrm_mgr *km;
  1587. bool is_alive = false;
  1588. rcu_read_lock();
  1589. list_for_each_entry_rcu(km, &xfrm_km_list, list) {
  1590. if (km->is_alive && km->is_alive(c)) {
  1591. is_alive = true;
  1592. break;
  1593. }
  1594. }
  1595. rcu_read_unlock();
  1596. return is_alive;
  1597. }
  1598. EXPORT_SYMBOL(km_is_alive);
  1599. int xfrm_user_policy(struct sock *sk, int optname, u8 __user *optval, int optlen)
  1600. {
  1601. int err;
  1602. u8 *data;
  1603. struct xfrm_mgr *km;
  1604. struct xfrm_policy *pol = NULL;
  1605. if (optlen <= 0 || optlen > PAGE_SIZE)
  1606. return -EMSGSIZE;
  1607. data = kmalloc(optlen, GFP_KERNEL);
  1608. if (!data)
  1609. return -ENOMEM;
  1610. err = -EFAULT;
  1611. if (copy_from_user(data, optval, optlen))
  1612. goto out;
  1613. err = -EINVAL;
  1614. rcu_read_lock();
  1615. list_for_each_entry_rcu(km, &xfrm_km_list, list) {
  1616. pol = km->compile_policy(sk, optname, data,
  1617. optlen, &err);
  1618. if (err >= 0)
  1619. break;
  1620. }
  1621. rcu_read_unlock();
  1622. if (err >= 0) {
  1623. xfrm_sk_policy_insert(sk, err, pol);
  1624. xfrm_pol_put(pol);
  1625. err = 0;
  1626. }
  1627. out:
  1628. kfree(data);
  1629. return err;
  1630. }
  1631. EXPORT_SYMBOL(xfrm_user_policy);
  1632. static DEFINE_SPINLOCK(xfrm_km_lock);
  1633. int xfrm_register_km(struct xfrm_mgr *km)
  1634. {
  1635. spin_lock_bh(&xfrm_km_lock);
  1636. list_add_tail_rcu(&km->list, &xfrm_km_list);
  1637. spin_unlock_bh(&xfrm_km_lock);
  1638. return 0;
  1639. }
  1640. EXPORT_SYMBOL(xfrm_register_km);
  1641. int xfrm_unregister_km(struct xfrm_mgr *km)
  1642. {
  1643. spin_lock_bh(&xfrm_km_lock);
  1644. list_del_rcu(&km->list);
  1645. spin_unlock_bh(&xfrm_km_lock);
  1646. synchronize_rcu();
  1647. return 0;
  1648. }
  1649. EXPORT_SYMBOL(xfrm_unregister_km);
  1650. int xfrm_state_register_afinfo(struct xfrm_state_afinfo *afinfo)
  1651. {
  1652. int err = 0;
  1653. if (unlikely(afinfo == NULL))
  1654. return -EINVAL;
  1655. if (unlikely(afinfo->family >= NPROTO))
  1656. return -EAFNOSUPPORT;
  1657. spin_lock_bh(&xfrm_state_afinfo_lock);
  1658. if (unlikely(xfrm_state_afinfo[afinfo->family] != NULL))
  1659. err = -EEXIST;
  1660. else
  1661. rcu_assign_pointer(xfrm_state_afinfo[afinfo->family], afinfo);
  1662. spin_unlock_bh(&xfrm_state_afinfo_lock);
  1663. return err;
  1664. }
  1665. EXPORT_SYMBOL(xfrm_state_register_afinfo);
  1666. int xfrm_state_unregister_afinfo(struct xfrm_state_afinfo *afinfo)
  1667. {
  1668. int err = 0;
  1669. if (unlikely(afinfo == NULL))
  1670. return -EINVAL;
  1671. if (unlikely(afinfo->family >= NPROTO))
  1672. return -EAFNOSUPPORT;
  1673. spin_lock_bh(&xfrm_state_afinfo_lock);
  1674. if (likely(xfrm_state_afinfo[afinfo->family] != NULL)) {
  1675. if (unlikely(xfrm_state_afinfo[afinfo->family] != afinfo))
  1676. err = -EINVAL;
  1677. else
  1678. RCU_INIT_POINTER(xfrm_state_afinfo[afinfo->family], NULL);
  1679. }
  1680. spin_unlock_bh(&xfrm_state_afinfo_lock);
  1681. synchronize_rcu();
  1682. return err;
  1683. }
  1684. EXPORT_SYMBOL(xfrm_state_unregister_afinfo);
  1685. struct xfrm_state_afinfo *xfrm_state_get_afinfo(unsigned int family)
  1686. {
  1687. struct xfrm_state_afinfo *afinfo;
  1688. if (unlikely(family >= NPROTO))
  1689. return NULL;
  1690. rcu_read_lock();
  1691. afinfo = rcu_dereference(xfrm_state_afinfo[family]);
  1692. if (unlikely(!afinfo))
  1693. rcu_read_unlock();
  1694. return afinfo;
  1695. }
  1696. void xfrm_state_put_afinfo(struct xfrm_state_afinfo *afinfo)
  1697. {
  1698. rcu_read_unlock();
  1699. }
  1700. /* Temporarily located here until net/xfrm/xfrm_tunnel.c is created */
  1701. void xfrm_state_delete_tunnel(struct xfrm_state *x)
  1702. {
  1703. if (x->tunnel) {
  1704. struct xfrm_state *t = x->tunnel;
  1705. if (atomic_read(&t->tunnel_users) == 2)
  1706. xfrm_state_delete(t);
  1707. atomic_dec(&t->tunnel_users);
  1708. xfrm_state_put(t);
  1709. x->tunnel = NULL;
  1710. }
  1711. }
  1712. EXPORT_SYMBOL(xfrm_state_delete_tunnel);
  1713. int xfrm_state_mtu(struct xfrm_state *x, int mtu)
  1714. {
  1715. int res;
  1716. spin_lock_bh(&x->lock);
  1717. if (x->km.state == XFRM_STATE_VALID &&
  1718. x->type && x->type->get_mtu)
  1719. res = x->type->get_mtu(x, mtu);
  1720. else
  1721. res = mtu - x->props.header_len;
  1722. spin_unlock_bh(&x->lock);
  1723. return res;
  1724. }
  1725. int __xfrm_init_state(struct xfrm_state *x, bool init_replay)
  1726. {
  1727. struct xfrm_state_afinfo *afinfo;
  1728. struct xfrm_mode *inner_mode;
  1729. int family = x->props.family;
  1730. int err;
  1731. err = -EAFNOSUPPORT;
  1732. afinfo = xfrm_state_get_afinfo(family);
  1733. if (!afinfo)
  1734. goto error;
  1735. err = 0;
  1736. if (afinfo->init_flags)
  1737. err = afinfo->init_flags(x);
  1738. xfrm_state_put_afinfo(afinfo);
  1739. if (err)
  1740. goto error;
  1741. err = -EPROTONOSUPPORT;
  1742. if (x->sel.family != AF_UNSPEC) {
  1743. inner_mode = xfrm_get_mode(x->props.mode, x->sel.family);
  1744. if (inner_mode == NULL)
  1745. goto error;
  1746. if (!(inner_mode->flags & XFRM_MODE_FLAG_TUNNEL) &&
  1747. family != x->sel.family) {
  1748. xfrm_put_mode(inner_mode);
  1749. goto error;
  1750. }
  1751. x->inner_mode = inner_mode;
  1752. } else {
  1753. struct xfrm_mode *inner_mode_iaf;
  1754. int iafamily = AF_INET;
  1755. inner_mode = xfrm_get_mode(x->props.mode, x->props.family);
  1756. if (inner_mode == NULL)
  1757. goto error;
  1758. if (!(inner_mode->flags & XFRM_MODE_FLAG_TUNNEL)) {
  1759. xfrm_put_mode(inner_mode);
  1760. goto error;
  1761. }
  1762. x->inner_mode = inner_mode;
  1763. if (x->props.family == AF_INET)
  1764. iafamily = AF_INET6;
  1765. inner_mode_iaf = xfrm_get_mode(x->props.mode, iafamily);
  1766. if (inner_mode_iaf) {
  1767. if (inner_mode_iaf->flags & XFRM_MODE_FLAG_TUNNEL)
  1768. x->inner_mode_iaf = inner_mode_iaf;
  1769. else
  1770. xfrm_put_mode(inner_mode_iaf);
  1771. }
  1772. }
  1773. x->type = xfrm_get_type(x->id.proto, family);
  1774. if (x->type == NULL)
  1775. goto error;
  1776. err = x->type->init_state(x);
  1777. if (err)
  1778. goto error;
  1779. x->outer_mode = xfrm_get_mode(x->props.mode, family);
  1780. if (x->outer_mode == NULL) {
  1781. err = -EPROTONOSUPPORT;
  1782. goto error;
  1783. }
  1784. if (init_replay) {
  1785. err = xfrm_init_replay(x);
  1786. if (err)
  1787. goto error;
  1788. }
  1789. x->km.state = XFRM_STATE_VALID;
  1790. error:
  1791. return err;
  1792. }
  1793. EXPORT_SYMBOL(__xfrm_init_state);
  1794. int xfrm_init_state(struct xfrm_state *x)
  1795. {
  1796. return __xfrm_init_state(x, true);
  1797. }
  1798. EXPORT_SYMBOL(xfrm_init_state);
  1799. int __net_init xfrm_state_init(struct net *net)
  1800. {
  1801. unsigned int sz;
  1802. INIT_LIST_HEAD(&net->xfrm.state_all);
  1803. sz = sizeof(struct hlist_head) * 8;
  1804. net->xfrm.state_bydst = xfrm_hash_alloc(sz);
  1805. if (!net->xfrm.state_bydst)
  1806. goto out_bydst;
  1807. net->xfrm.state_bysrc = xfrm_hash_alloc(sz);
  1808. if (!net->xfrm.state_bysrc)
  1809. goto out_bysrc;
  1810. net->xfrm.state_byspi = xfrm_hash_alloc(sz);
  1811. if (!net->xfrm.state_byspi)
  1812. goto out_byspi;
  1813. net->xfrm.state_hmask = ((sz / sizeof(struct hlist_head)) - 1);
  1814. net->xfrm.state_num = 0;
  1815. INIT_WORK(&net->xfrm.state_hash_work, xfrm_hash_resize);
  1816. spin_lock_init(&net->xfrm.xfrm_state_lock);
  1817. return 0;
  1818. out_byspi:
  1819. xfrm_hash_free(net->xfrm.state_bysrc, sz);
  1820. out_bysrc:
  1821. xfrm_hash_free(net->xfrm.state_bydst, sz);
  1822. out_bydst:
  1823. return -ENOMEM;
  1824. }
  1825. void xfrm_state_fini(struct net *net)
  1826. {
  1827. unsigned int sz;
  1828. flush_work(&net->xfrm.state_hash_work);
  1829. xfrm_state_flush(net, IPSEC_PROTO_ANY, false);
  1830. flush_work(&xfrm_state_gc_work);
  1831. WARN_ON(!list_empty(&net->xfrm.state_all));
  1832. sz = (net->xfrm.state_hmask + 1) * sizeof(struct hlist_head);
  1833. WARN_ON(!hlist_empty(net->xfrm.state_byspi));
  1834. xfrm_hash_free(net->xfrm.state_byspi, sz);
  1835. WARN_ON(!hlist_empty(net->xfrm.state_bysrc));
  1836. xfrm_hash_free(net->xfrm.state_bysrc, sz);
  1837. WARN_ON(!hlist_empty(net->xfrm.state_bydst));
  1838. xfrm_hash_free(net->xfrm.state_bydst, sz);
  1839. }
  1840. #ifdef CONFIG_AUDITSYSCALL
  1841. static void xfrm_audit_helper_sainfo(struct xfrm_state *x,
  1842. struct audit_buffer *audit_buf)
  1843. {
  1844. struct xfrm_sec_ctx *ctx = x->security;
  1845. u32 spi = ntohl(x->id.spi);
  1846. if (ctx)
  1847. audit_log_format(audit_buf, " sec_alg=%u sec_doi=%u sec_obj=%s",
  1848. ctx->ctx_alg, ctx->ctx_doi, ctx->ctx_str);
  1849. switch (x->props.family) {
  1850. case AF_INET:
  1851. audit_log_format(audit_buf, " src=%pI4 dst=%pI4",
  1852. &x->props.saddr.a4, &x->id.daddr.a4);
  1853. break;
  1854. case AF_INET6:
  1855. audit_log_format(audit_buf, " src=%pI6 dst=%pI6",
  1856. x->props.saddr.a6, x->id.daddr.a6);
  1857. break;
  1858. }
  1859. audit_log_format(audit_buf, " spi=%u(0x%x)", spi, spi);
  1860. }
  1861. static void xfrm_audit_helper_pktinfo(struct sk_buff *skb, u16 family,
  1862. struct audit_buffer *audit_buf)
  1863. {
  1864. const struct iphdr *iph4;
  1865. const struct ipv6hdr *iph6;
  1866. switch (family) {
  1867. case AF_INET:
  1868. iph4 = ip_hdr(skb);
  1869. audit_log_format(audit_buf, " src=%pI4 dst=%pI4",
  1870. &iph4->saddr, &iph4->daddr);
  1871. break;
  1872. case AF_INET6:
  1873. iph6 = ipv6_hdr(skb);
  1874. audit_log_format(audit_buf,
  1875. " src=%pI6 dst=%pI6 flowlbl=0x%x%02x%02x",
  1876. &iph6->saddr, &iph6->daddr,
  1877. iph6->flow_lbl[0] & 0x0f,
  1878. iph6->flow_lbl[1],
  1879. iph6->flow_lbl[2]);
  1880. break;
  1881. }
  1882. }
  1883. void xfrm_audit_state_add(struct xfrm_state *x, int result, bool task_valid)
  1884. {
  1885. struct audit_buffer *audit_buf;
  1886. audit_buf = xfrm_audit_start("SAD-add");
  1887. if (audit_buf == NULL)
  1888. return;
  1889. xfrm_audit_helper_usrinfo(task_valid, audit_buf);
  1890. xfrm_audit_helper_sainfo(x, audit_buf);
  1891. audit_log_format(audit_buf, " res=%u", result);
  1892. audit_log_end(audit_buf);
  1893. }
  1894. EXPORT_SYMBOL_GPL(xfrm_audit_state_add);
  1895. void xfrm_audit_state_delete(struct xfrm_state *x, int result, bool task_valid)
  1896. {
  1897. struct audit_buffer *audit_buf;
  1898. audit_buf = xfrm_audit_start("SAD-delete");
  1899. if (audit_buf == NULL)
  1900. return;
  1901. xfrm_audit_helper_usrinfo(task_valid, audit_buf);
  1902. xfrm_audit_helper_sainfo(x, audit_buf);
  1903. audit_log_format(audit_buf, " res=%u", result);
  1904. audit_log_end(audit_buf);
  1905. }
  1906. EXPORT_SYMBOL_GPL(xfrm_audit_state_delete);
  1907. void xfrm_audit_state_replay_overflow(struct xfrm_state *x,
  1908. struct sk_buff *skb)
  1909. {
  1910. struct audit_buffer *audit_buf;
  1911. u32 spi;
  1912. audit_buf = xfrm_audit_start("SA-replay-overflow");
  1913. if (audit_buf == NULL)
  1914. return;
  1915. xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
  1916. /* don't record the sequence number because it's inherent in this kind
  1917. * of audit message */
  1918. spi = ntohl(x->id.spi);
  1919. audit_log_format(audit_buf, " spi=%u(0x%x)", spi, spi);
  1920. audit_log_end(audit_buf);
  1921. }
  1922. EXPORT_SYMBOL_GPL(xfrm_audit_state_replay_overflow);
  1923. void xfrm_audit_state_replay(struct xfrm_state *x,
  1924. struct sk_buff *skb, __be32 net_seq)
  1925. {
  1926. struct audit_buffer *audit_buf;
  1927. u32 spi;
  1928. audit_buf = xfrm_audit_start("SA-replayed-pkt");
  1929. if (audit_buf == NULL)
  1930. return;
  1931. xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
  1932. spi = ntohl(x->id.spi);
  1933. audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
  1934. spi, spi, ntohl(net_seq));
  1935. audit_log_end(audit_buf);
  1936. }
  1937. EXPORT_SYMBOL_GPL(xfrm_audit_state_replay);
  1938. void xfrm_audit_state_notfound_simple(struct sk_buff *skb, u16 family)
  1939. {
  1940. struct audit_buffer *audit_buf;
  1941. audit_buf = xfrm_audit_start("SA-notfound");
  1942. if (audit_buf == NULL)
  1943. return;
  1944. xfrm_audit_helper_pktinfo(skb, family, audit_buf);
  1945. audit_log_end(audit_buf);
  1946. }
  1947. EXPORT_SYMBOL_GPL(xfrm_audit_state_notfound_simple);
  1948. void xfrm_audit_state_notfound(struct sk_buff *skb, u16 family,
  1949. __be32 net_spi, __be32 net_seq)
  1950. {
  1951. struct audit_buffer *audit_buf;
  1952. u32 spi;
  1953. audit_buf = xfrm_audit_start("SA-notfound");
  1954. if (audit_buf == NULL)
  1955. return;
  1956. xfrm_audit_helper_pktinfo(skb, family, audit_buf);
  1957. spi = ntohl(net_spi);
  1958. audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
  1959. spi, spi, ntohl(net_seq));
  1960. audit_log_end(audit_buf);
  1961. }
  1962. EXPORT_SYMBOL_GPL(xfrm_audit_state_notfound);
  1963. void xfrm_audit_state_icvfail(struct xfrm_state *x,
  1964. struct sk_buff *skb, u8 proto)
  1965. {
  1966. struct audit_buffer *audit_buf;
  1967. __be32 net_spi;
  1968. __be32 net_seq;
  1969. audit_buf = xfrm_audit_start("SA-icv-failure");
  1970. if (audit_buf == NULL)
  1971. return;
  1972. xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
  1973. if (xfrm_parse_spi(skb, proto, &net_spi, &net_seq) == 0) {
  1974. u32 spi = ntohl(net_spi);
  1975. audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
  1976. spi, spi, ntohl(net_seq));
  1977. }
  1978. audit_log_end(audit_buf);
  1979. }
  1980. EXPORT_SYMBOL_GPL(xfrm_audit_state_icvfail);
  1981. #endif /* CONFIG_AUDITSYSCALL */