xfrm_state.c 54 KB

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