xfrm_state.c 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363
  1. /*
  2. * xfrm_state.c
  3. *
  4. * Changes:
  5. * Mitsuru KANDA @USAGI
  6. * Kazunori MIYAZAWA @USAGI
  7. * Kunihiro Ishiguro <[email protected]>
  8. * IPv6 support
  9. * YOSHIFUJI Hideaki @USAGI
  10. * Split up af-specific functions
  11. * Derek Atkins <[email protected]>
  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 + 1;
  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. u32 if_id = fl->flowi_xfrm.if_id;
  683. unsigned short encap_family = tmpl->encap_family;
  684. unsigned int sequence;
  685. struct km_event c;
  686. to_put = NULL;
  687. sequence = read_seqcount_begin(&xfrm_state_hash_generation);
  688. rcu_read_lock();
  689. h = xfrm_dst_hash(net, daddr, saddr, tmpl->reqid, encap_family);
  690. hlist_for_each_entry_rcu(x, net->xfrm.state_bydst + h, bydst) {
  691. if (x->props.family == encap_family &&
  692. x->props.reqid == tmpl->reqid &&
  693. (mark & x->mark.m) == x->mark.v &&
  694. x->if_id == if_id &&
  695. !(x->props.flags & XFRM_STATE_WILDRECV) &&
  696. xfrm_state_addr_check(x, daddr, saddr, encap_family) &&
  697. tmpl->mode == x->props.mode &&
  698. tmpl->id.proto == x->id.proto &&
  699. (tmpl->id.spi == x->id.spi || !tmpl->id.spi))
  700. xfrm_state_look_at(pol, x, fl, encap_family,
  701. &best, &acquire_in_progress, &error);
  702. }
  703. if (best || acquire_in_progress)
  704. goto found;
  705. h_wildcard = xfrm_dst_hash(net, daddr, &saddr_wildcard, tmpl->reqid, encap_family);
  706. hlist_for_each_entry_rcu(x, net->xfrm.state_bydst + h_wildcard, bydst) {
  707. if (x->props.family == encap_family &&
  708. x->props.reqid == tmpl->reqid &&
  709. (mark & x->mark.m) == x->mark.v &&
  710. x->if_id == if_id &&
  711. !(x->props.flags & XFRM_STATE_WILDRECV) &&
  712. xfrm_addr_equal(&x->id.daddr, daddr, encap_family) &&
  713. tmpl->mode == x->props.mode &&
  714. tmpl->id.proto == x->id.proto &&
  715. (tmpl->id.spi == x->id.spi || !tmpl->id.spi))
  716. xfrm_state_look_at(pol, x, fl, encap_family,
  717. &best, &acquire_in_progress, &error);
  718. }
  719. found:
  720. x = best;
  721. if (!x && !error && !acquire_in_progress) {
  722. if (tmpl->id.spi &&
  723. (x0 = __xfrm_state_lookup(net, mark, daddr, tmpl->id.spi,
  724. tmpl->id.proto, encap_family)) != NULL) {
  725. to_put = x0;
  726. error = -EEXIST;
  727. goto out;
  728. }
  729. c.net = net;
  730. /* If the KMs have no listeners (yet...), avoid allocating an SA
  731. * for each and every packet - garbage collection might not
  732. * handle the flood.
  733. */
  734. if (!km_is_alive(&c)) {
  735. error = -ESRCH;
  736. goto out;
  737. }
  738. x = xfrm_state_alloc(net);
  739. if (x == NULL) {
  740. error = -ENOMEM;
  741. goto out;
  742. }
  743. /* Initialize temporary state matching only
  744. * to current session. */
  745. xfrm_init_tempstate(x, fl, tmpl, daddr, saddr, family);
  746. memcpy(&x->mark, &pol->mark, sizeof(x->mark));
  747. x->if_id = if_id;
  748. error = security_xfrm_state_alloc_acquire(x, pol->security, fl->flowi_secid);
  749. if (error) {
  750. x->km.state = XFRM_STATE_DEAD;
  751. to_put = x;
  752. x = NULL;
  753. goto out;
  754. }
  755. if (km_query(x, tmpl, pol) == 0) {
  756. spin_lock_bh(&net->xfrm.xfrm_state_lock);
  757. x->km.state = XFRM_STATE_ACQ;
  758. list_add(&x->km.all, &net->xfrm.state_all);
  759. hlist_add_head_rcu(&x->bydst, net->xfrm.state_bydst + h);
  760. h = xfrm_src_hash(net, daddr, saddr, encap_family);
  761. hlist_add_head_rcu(&x->bysrc, net->xfrm.state_bysrc + h);
  762. if (x->id.spi) {
  763. h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto, encap_family);
  764. hlist_add_head_rcu(&x->byspi, net->xfrm.state_byspi + h);
  765. }
  766. x->lft.hard_add_expires_seconds = net->xfrm.sysctl_acq_expires;
  767. tasklet_hrtimer_start(&x->mtimer, ktime_set(net->xfrm.sysctl_acq_expires, 0), HRTIMER_MODE_REL);
  768. net->xfrm.state_num++;
  769. xfrm_hash_grow_check(net, x->bydst.next != NULL);
  770. spin_unlock_bh(&net->xfrm.xfrm_state_lock);
  771. } else {
  772. x->km.state = XFRM_STATE_DEAD;
  773. to_put = x;
  774. x = NULL;
  775. error = -ESRCH;
  776. }
  777. }
  778. out:
  779. if (x) {
  780. if (!xfrm_state_hold_rcu(x)) {
  781. *err = -EAGAIN;
  782. x = NULL;
  783. }
  784. } else {
  785. *err = acquire_in_progress ? -EAGAIN : error;
  786. }
  787. rcu_read_unlock();
  788. if (to_put)
  789. xfrm_state_put(to_put);
  790. if (read_seqcount_retry(&xfrm_state_hash_generation, sequence)) {
  791. *err = -EAGAIN;
  792. if (x) {
  793. xfrm_state_put(x);
  794. x = NULL;
  795. }
  796. }
  797. return x;
  798. }
  799. struct xfrm_state *
  800. xfrm_stateonly_find(struct net *net, u32 mark, u32 if_id,
  801. xfrm_address_t *daddr, xfrm_address_t *saddr,
  802. unsigned short family, u8 mode, u8 proto, u32 reqid)
  803. {
  804. unsigned int h;
  805. struct xfrm_state *rx = NULL, *x = NULL;
  806. spin_lock_bh(&net->xfrm.xfrm_state_lock);
  807. h = xfrm_dst_hash(net, daddr, saddr, reqid, family);
  808. hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
  809. if (x->props.family == family &&
  810. x->props.reqid == reqid &&
  811. (mark & x->mark.m) == x->mark.v &&
  812. x->if_id == if_id &&
  813. !(x->props.flags & XFRM_STATE_WILDRECV) &&
  814. xfrm_state_addr_check(x, daddr, saddr, family) &&
  815. mode == x->props.mode &&
  816. proto == x->id.proto &&
  817. x->km.state == XFRM_STATE_VALID) {
  818. rx = x;
  819. break;
  820. }
  821. }
  822. if (rx)
  823. xfrm_state_hold(rx);
  824. spin_unlock_bh(&net->xfrm.xfrm_state_lock);
  825. return rx;
  826. }
  827. EXPORT_SYMBOL(xfrm_stateonly_find);
  828. struct xfrm_state *xfrm_state_lookup_byspi(struct net *net, __be32 spi,
  829. unsigned short family)
  830. {
  831. struct xfrm_state *x;
  832. struct xfrm_state_walk *w;
  833. spin_lock_bh(&net->xfrm.xfrm_state_lock);
  834. list_for_each_entry(w, &net->xfrm.state_all, all) {
  835. x = container_of(w, struct xfrm_state, km);
  836. if (x->props.family != family ||
  837. x->id.spi != spi)
  838. continue;
  839. xfrm_state_hold(x);
  840. spin_unlock_bh(&net->xfrm.xfrm_state_lock);
  841. return x;
  842. }
  843. spin_unlock_bh(&net->xfrm.xfrm_state_lock);
  844. return NULL;
  845. }
  846. EXPORT_SYMBOL(xfrm_state_lookup_byspi);
  847. static void __xfrm_state_insert(struct xfrm_state *x)
  848. {
  849. struct net *net = xs_net(x);
  850. unsigned int h;
  851. list_add(&x->km.all, &net->xfrm.state_all);
  852. h = xfrm_dst_hash(net, &x->id.daddr, &x->props.saddr,
  853. x->props.reqid, x->props.family);
  854. hlist_add_head_rcu(&x->bydst, net->xfrm.state_bydst + h);
  855. h = xfrm_src_hash(net, &x->id.daddr, &x->props.saddr, x->props.family);
  856. hlist_add_head_rcu(&x->bysrc, net->xfrm.state_bysrc + h);
  857. if (x->id.spi) {
  858. h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto,
  859. x->props.family);
  860. hlist_add_head_rcu(&x->byspi, net->xfrm.state_byspi + h);
  861. }
  862. tasklet_hrtimer_start(&x->mtimer, ktime_set(1, 0), HRTIMER_MODE_REL);
  863. if (x->replay_maxage)
  864. mod_timer(&x->rtimer, jiffies + x->replay_maxage);
  865. net->xfrm.state_num++;
  866. xfrm_hash_grow_check(net, x->bydst.next != NULL);
  867. }
  868. /* net->xfrm.xfrm_state_lock is held */
  869. static void __xfrm_state_bump_genids(struct xfrm_state *xnew)
  870. {
  871. struct net *net = xs_net(xnew);
  872. unsigned short family = xnew->props.family;
  873. u32 reqid = xnew->props.reqid;
  874. struct xfrm_state *x;
  875. unsigned int h;
  876. u32 mark = xnew->mark.v & xnew->mark.m;
  877. u32 if_id = xnew->if_id;
  878. h = xfrm_dst_hash(net, &xnew->id.daddr, &xnew->props.saddr, reqid, family);
  879. hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
  880. if (x->props.family == family &&
  881. x->props.reqid == reqid &&
  882. x->if_id == if_id &&
  883. (mark & x->mark.m) == x->mark.v &&
  884. xfrm_addr_equal(&x->id.daddr, &xnew->id.daddr, family) &&
  885. xfrm_addr_equal(&x->props.saddr, &xnew->props.saddr, family))
  886. x->genid++;
  887. }
  888. }
  889. void xfrm_state_insert(struct xfrm_state *x)
  890. {
  891. struct net *net = xs_net(x);
  892. spin_lock_bh(&net->xfrm.xfrm_state_lock);
  893. __xfrm_state_bump_genids(x);
  894. __xfrm_state_insert(x);
  895. spin_unlock_bh(&net->xfrm.xfrm_state_lock);
  896. }
  897. EXPORT_SYMBOL(xfrm_state_insert);
  898. /* net->xfrm.xfrm_state_lock is held */
  899. static struct xfrm_state *__find_acq_core(struct net *net,
  900. const struct xfrm_mark *m,
  901. unsigned short family, u8 mode,
  902. u32 reqid, u32 if_id, u8 proto,
  903. const xfrm_address_t *daddr,
  904. const xfrm_address_t *saddr,
  905. int create)
  906. {
  907. unsigned int h = xfrm_dst_hash(net, daddr, saddr, reqid, family);
  908. struct xfrm_state *x;
  909. u32 mark = m->v & m->m;
  910. hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
  911. if (x->props.reqid != reqid ||
  912. x->props.mode != mode ||
  913. x->props.family != family ||
  914. x->km.state != XFRM_STATE_ACQ ||
  915. x->id.spi != 0 ||
  916. x->id.proto != proto ||
  917. (mark & x->mark.m) != x->mark.v ||
  918. !xfrm_addr_equal(&x->id.daddr, daddr, family) ||
  919. !xfrm_addr_equal(&x->props.saddr, saddr, family))
  920. continue;
  921. xfrm_state_hold(x);
  922. return x;
  923. }
  924. if (!create)
  925. return NULL;
  926. x = xfrm_state_alloc(net);
  927. if (likely(x)) {
  928. switch (family) {
  929. case AF_INET:
  930. x->sel.daddr.a4 = daddr->a4;
  931. x->sel.saddr.a4 = saddr->a4;
  932. x->sel.prefixlen_d = 32;
  933. x->sel.prefixlen_s = 32;
  934. x->props.saddr.a4 = saddr->a4;
  935. x->id.daddr.a4 = daddr->a4;
  936. break;
  937. case AF_INET6:
  938. x->sel.daddr.in6 = daddr->in6;
  939. x->sel.saddr.in6 = saddr->in6;
  940. x->sel.prefixlen_d = 128;
  941. x->sel.prefixlen_s = 128;
  942. x->props.saddr.in6 = saddr->in6;
  943. x->id.daddr.in6 = daddr->in6;
  944. break;
  945. }
  946. x->km.state = XFRM_STATE_ACQ;
  947. x->id.proto = proto;
  948. x->props.family = family;
  949. x->props.mode = mode;
  950. x->props.reqid = reqid;
  951. x->if_id = if_id;
  952. x->mark.v = m->v;
  953. x->mark.m = m->m;
  954. x->lft.hard_add_expires_seconds = net->xfrm.sysctl_acq_expires;
  955. xfrm_state_hold(x);
  956. tasklet_hrtimer_start(&x->mtimer, ktime_set(net->xfrm.sysctl_acq_expires, 0), HRTIMER_MODE_REL);
  957. list_add(&x->km.all, &net->xfrm.state_all);
  958. hlist_add_head_rcu(&x->bydst, net->xfrm.state_bydst + h);
  959. h = xfrm_src_hash(net, daddr, saddr, family);
  960. hlist_add_head_rcu(&x->bysrc, net->xfrm.state_bysrc + h);
  961. net->xfrm.state_num++;
  962. xfrm_hash_grow_check(net, x->bydst.next != NULL);
  963. }
  964. return x;
  965. }
  966. static struct xfrm_state *__xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq);
  967. int xfrm_state_add(struct xfrm_state *x)
  968. {
  969. struct net *net = xs_net(x);
  970. struct xfrm_state *x1, *to_put;
  971. int family;
  972. int err;
  973. u32 mark = x->mark.v & x->mark.m;
  974. int use_spi = xfrm_id_proto_match(x->id.proto, IPSEC_PROTO_ANY);
  975. family = x->props.family;
  976. to_put = NULL;
  977. spin_lock_bh(&net->xfrm.xfrm_state_lock);
  978. x1 = __xfrm_state_locate(x, use_spi, family);
  979. if (x1) {
  980. to_put = x1;
  981. x1 = NULL;
  982. err = -EEXIST;
  983. goto out;
  984. }
  985. if (use_spi && x->km.seq) {
  986. x1 = __xfrm_find_acq_byseq(net, mark, x->km.seq);
  987. if (x1 && ((x1->id.proto != x->id.proto) ||
  988. !xfrm_addr_equal(&x1->id.daddr, &x->id.daddr, family))) {
  989. to_put = x1;
  990. x1 = NULL;
  991. }
  992. }
  993. if (use_spi && !x1)
  994. x1 = __find_acq_core(net, &x->mark, family, x->props.mode,
  995. x->props.reqid, x->if_id, x->id.proto,
  996. &x->id.daddr, &x->props.saddr, 0);
  997. __xfrm_state_bump_genids(x);
  998. __xfrm_state_insert(x);
  999. err = 0;
  1000. out:
  1001. spin_unlock_bh(&net->xfrm.xfrm_state_lock);
  1002. if (x1) {
  1003. xfrm_state_delete(x1);
  1004. xfrm_state_put(x1);
  1005. }
  1006. if (to_put)
  1007. xfrm_state_put(to_put);
  1008. return err;
  1009. }
  1010. EXPORT_SYMBOL(xfrm_state_add);
  1011. #ifdef CONFIG_XFRM_MIGRATE
  1012. static struct xfrm_state *xfrm_state_clone(struct xfrm_state *orig)
  1013. {
  1014. struct net *net = xs_net(orig);
  1015. struct xfrm_state *x = xfrm_state_alloc(net);
  1016. if (!x)
  1017. goto out;
  1018. memcpy(&x->id, &orig->id, sizeof(x->id));
  1019. memcpy(&x->sel, &orig->sel, sizeof(x->sel));
  1020. memcpy(&x->lft, &orig->lft, sizeof(x->lft));
  1021. x->props.mode = orig->props.mode;
  1022. x->props.replay_window = orig->props.replay_window;
  1023. x->props.reqid = orig->props.reqid;
  1024. x->props.family = orig->props.family;
  1025. x->props.saddr = orig->props.saddr;
  1026. if (orig->aalg) {
  1027. x->aalg = xfrm_algo_auth_clone(orig->aalg);
  1028. if (!x->aalg)
  1029. goto error;
  1030. }
  1031. x->props.aalgo = orig->props.aalgo;
  1032. if (orig->aead) {
  1033. x->aead = xfrm_algo_aead_clone(orig->aead);
  1034. x->geniv = orig->geniv;
  1035. if (!x->aead)
  1036. goto error;
  1037. }
  1038. if (orig->ealg) {
  1039. x->ealg = xfrm_algo_clone(orig->ealg);
  1040. if (!x->ealg)
  1041. goto error;
  1042. }
  1043. x->props.ealgo = orig->props.ealgo;
  1044. if (orig->calg) {
  1045. x->calg = xfrm_algo_clone(orig->calg);
  1046. if (!x->calg)
  1047. goto error;
  1048. }
  1049. x->props.calgo = orig->props.calgo;
  1050. if (orig->encap) {
  1051. x->encap = kmemdup(orig->encap, sizeof(*x->encap), GFP_KERNEL);
  1052. if (!x->encap)
  1053. goto error;
  1054. }
  1055. if (orig->coaddr) {
  1056. x->coaddr = kmemdup(orig->coaddr, sizeof(*x->coaddr),
  1057. GFP_KERNEL);
  1058. if (!x->coaddr)
  1059. goto error;
  1060. }
  1061. if (orig->replay_esn) {
  1062. if (xfrm_replay_clone(x, orig))
  1063. goto error;
  1064. }
  1065. memcpy(&x->mark, &orig->mark, sizeof(x->mark));
  1066. if (xfrm_init_state(x) < 0)
  1067. goto error;
  1068. x->props.flags = orig->props.flags;
  1069. x->props.extra_flags = orig->props.extra_flags;
  1070. x->if_id = orig->if_id;
  1071. x->tfcpad = orig->tfcpad;
  1072. x->replay_maxdiff = orig->replay_maxdiff;
  1073. x->replay_maxage = orig->replay_maxage;
  1074. x->curlft.add_time = orig->curlft.add_time;
  1075. x->km.state = orig->km.state;
  1076. x->km.seq = orig->km.seq;
  1077. x->replay = orig->replay;
  1078. x->preplay = orig->preplay;
  1079. return x;
  1080. error:
  1081. xfrm_state_put(x);
  1082. out:
  1083. return NULL;
  1084. }
  1085. struct xfrm_state *xfrm_migrate_state_find(struct xfrm_migrate *m, struct net *net)
  1086. {
  1087. unsigned int h;
  1088. struct xfrm_state *x = NULL;
  1089. spin_lock_bh(&net->xfrm.xfrm_state_lock);
  1090. if (m->reqid) {
  1091. h = xfrm_dst_hash(net, &m->old_daddr, &m->old_saddr,
  1092. m->reqid, m->old_family);
  1093. hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
  1094. if (x->props.mode != m->mode ||
  1095. x->id.proto != m->proto)
  1096. continue;
  1097. if (m->reqid && x->props.reqid != m->reqid)
  1098. continue;
  1099. if (!xfrm_addr_equal(&x->id.daddr, &m->old_daddr,
  1100. m->old_family) ||
  1101. !xfrm_addr_equal(&x->props.saddr, &m->old_saddr,
  1102. m->old_family))
  1103. continue;
  1104. xfrm_state_hold(x);
  1105. break;
  1106. }
  1107. } else {
  1108. h = xfrm_src_hash(net, &m->old_daddr, &m->old_saddr,
  1109. m->old_family);
  1110. hlist_for_each_entry(x, net->xfrm.state_bysrc+h, bysrc) {
  1111. if (x->props.mode != m->mode ||
  1112. x->id.proto != m->proto)
  1113. continue;
  1114. if (!xfrm_addr_equal(&x->id.daddr, &m->old_daddr,
  1115. m->old_family) ||
  1116. !xfrm_addr_equal(&x->props.saddr, &m->old_saddr,
  1117. m->old_family))
  1118. continue;
  1119. xfrm_state_hold(x);
  1120. break;
  1121. }
  1122. }
  1123. spin_unlock_bh(&net->xfrm.xfrm_state_lock);
  1124. return x;
  1125. }
  1126. EXPORT_SYMBOL(xfrm_migrate_state_find);
  1127. struct xfrm_state *xfrm_state_migrate(struct xfrm_state *x,
  1128. struct xfrm_migrate *m)
  1129. {
  1130. struct xfrm_state *xc;
  1131. xc = xfrm_state_clone(x);
  1132. if (!xc)
  1133. return NULL;
  1134. memcpy(&xc->id.daddr, &m->new_daddr, sizeof(xc->id.daddr));
  1135. memcpy(&xc->props.saddr, &m->new_saddr, sizeof(xc->props.saddr));
  1136. /* add state */
  1137. if (xfrm_addr_equal(&x->id.daddr, &m->new_daddr, m->new_family)) {
  1138. /* a care is needed when the destination address of the
  1139. state is to be updated as it is a part of triplet */
  1140. xfrm_state_insert(xc);
  1141. } else {
  1142. if (xfrm_state_add(xc) < 0)
  1143. goto error;
  1144. }
  1145. return xc;
  1146. error:
  1147. xfrm_state_put(xc);
  1148. return NULL;
  1149. }
  1150. EXPORT_SYMBOL(xfrm_state_migrate);
  1151. #endif
  1152. int xfrm_state_update(struct xfrm_state *x)
  1153. {
  1154. struct xfrm_state *x1, *to_put;
  1155. int err;
  1156. int use_spi = xfrm_id_proto_match(x->id.proto, IPSEC_PROTO_ANY);
  1157. struct net *net = xs_net(x);
  1158. to_put = NULL;
  1159. spin_lock_bh(&net->xfrm.xfrm_state_lock);
  1160. x1 = __xfrm_state_locate(x, use_spi, x->props.family);
  1161. err = -ESRCH;
  1162. if (!x1)
  1163. goto out;
  1164. if (xfrm_state_kern(x1)) {
  1165. to_put = x1;
  1166. err = -EEXIST;
  1167. goto out;
  1168. }
  1169. if (x1->km.state == XFRM_STATE_ACQ) {
  1170. __xfrm_state_insert(x);
  1171. x = NULL;
  1172. }
  1173. err = 0;
  1174. out:
  1175. spin_unlock_bh(&net->xfrm.xfrm_state_lock);
  1176. if (to_put)
  1177. xfrm_state_put(to_put);
  1178. if (err)
  1179. return err;
  1180. if (!x) {
  1181. xfrm_state_delete(x1);
  1182. xfrm_state_put(x1);
  1183. return 0;
  1184. }
  1185. err = -EINVAL;
  1186. spin_lock_bh(&x1->lock);
  1187. if (likely(x1->km.state == XFRM_STATE_VALID)) {
  1188. if (x->encap && x1->encap)
  1189. memcpy(x1->encap, x->encap, sizeof(*x1->encap));
  1190. if (x->coaddr && x1->coaddr) {
  1191. memcpy(x1->coaddr, x->coaddr, sizeof(*x1->coaddr));
  1192. }
  1193. if (!use_spi && memcmp(&x1->sel, &x->sel, sizeof(x1->sel)))
  1194. memcpy(&x1->sel, &x->sel, sizeof(x1->sel));
  1195. memcpy(&x1->lft, &x->lft, sizeof(x1->lft));
  1196. x1->km.dying = 0;
  1197. tasklet_hrtimer_start(&x1->mtimer, ktime_set(1, 0), HRTIMER_MODE_REL);
  1198. if (x1->curlft.use_time)
  1199. xfrm_state_check_expire(x1);
  1200. if (x->props.smark.m || x->props.smark.v || x->if_id) {
  1201. spin_lock_bh(&net->xfrm.xfrm_state_lock);
  1202. if (x->props.smark.m || x->props.smark.v)
  1203. x1->props.smark = x->props.smark;
  1204. if (x->if_id)
  1205. x1->if_id = x->if_id;
  1206. __xfrm_state_bump_genids(x1);
  1207. spin_unlock_bh(&net->xfrm.xfrm_state_lock);
  1208. }
  1209. err = 0;
  1210. x->km.state = XFRM_STATE_DEAD;
  1211. __xfrm_state_put(x);
  1212. }
  1213. spin_unlock_bh(&x1->lock);
  1214. xfrm_state_put(x1);
  1215. return err;
  1216. }
  1217. EXPORT_SYMBOL(xfrm_state_update);
  1218. int xfrm_state_check_expire(struct xfrm_state *x)
  1219. {
  1220. if (!x->curlft.use_time)
  1221. x->curlft.use_time = get_seconds();
  1222. if (x->curlft.bytes >= x->lft.hard_byte_limit ||
  1223. x->curlft.packets >= x->lft.hard_packet_limit) {
  1224. x->km.state = XFRM_STATE_EXPIRED;
  1225. tasklet_hrtimer_start(&x->mtimer, ktime_set(0, 0), HRTIMER_MODE_REL);
  1226. return -EINVAL;
  1227. }
  1228. if (!x->km.dying &&
  1229. (x->curlft.bytes >= x->lft.soft_byte_limit ||
  1230. x->curlft.packets >= x->lft.soft_packet_limit)) {
  1231. x->km.dying = 1;
  1232. km_state_expired(x, 0, 0);
  1233. }
  1234. return 0;
  1235. }
  1236. EXPORT_SYMBOL(xfrm_state_check_expire);
  1237. struct xfrm_state *
  1238. xfrm_state_lookup(struct net *net, u32 mark, const xfrm_address_t *daddr, __be32 spi,
  1239. u8 proto, unsigned short family)
  1240. {
  1241. struct xfrm_state *x;
  1242. rcu_read_lock();
  1243. x = __xfrm_state_lookup(net, mark, daddr, spi, proto, family);
  1244. rcu_read_unlock();
  1245. return x;
  1246. }
  1247. EXPORT_SYMBOL(xfrm_state_lookup);
  1248. struct xfrm_state *
  1249. xfrm_state_lookup_byaddr(struct net *net, u32 mark,
  1250. const xfrm_address_t *daddr, const xfrm_address_t *saddr,
  1251. u8 proto, unsigned short family)
  1252. {
  1253. struct xfrm_state *x;
  1254. spin_lock_bh(&net->xfrm.xfrm_state_lock);
  1255. x = __xfrm_state_lookup_byaddr(net, mark, daddr, saddr, proto, family);
  1256. spin_unlock_bh(&net->xfrm.xfrm_state_lock);
  1257. return x;
  1258. }
  1259. EXPORT_SYMBOL(xfrm_state_lookup_byaddr);
  1260. struct xfrm_state *
  1261. xfrm_find_acq(struct net *net, const struct xfrm_mark *mark, u8 mode, u32 reqid,
  1262. u32 if_id, u8 proto, const xfrm_address_t *daddr,
  1263. const xfrm_address_t *saddr, int create, unsigned short family)
  1264. {
  1265. struct xfrm_state *x;
  1266. spin_lock_bh(&net->xfrm.xfrm_state_lock);
  1267. x = __find_acq_core(net, mark, family, mode, reqid, if_id, proto, daddr, saddr, create);
  1268. spin_unlock_bh(&net->xfrm.xfrm_state_lock);
  1269. return x;
  1270. }
  1271. EXPORT_SYMBOL(xfrm_find_acq);
  1272. #ifdef CONFIG_XFRM_SUB_POLICY
  1273. int
  1274. xfrm_tmpl_sort(struct xfrm_tmpl **dst, struct xfrm_tmpl **src, int n,
  1275. unsigned short family, struct net *net)
  1276. {
  1277. int err = 0;
  1278. struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
  1279. if (!afinfo)
  1280. return -EAFNOSUPPORT;
  1281. spin_lock_bh(&net->xfrm.xfrm_state_lock); /*FIXME*/
  1282. if (afinfo->tmpl_sort)
  1283. err = afinfo->tmpl_sort(dst, src, n);
  1284. spin_unlock_bh(&net->xfrm.xfrm_state_lock);
  1285. xfrm_state_put_afinfo(afinfo);
  1286. return err;
  1287. }
  1288. EXPORT_SYMBOL(xfrm_tmpl_sort);
  1289. int
  1290. xfrm_state_sort(struct xfrm_state **dst, struct xfrm_state **src, int n,
  1291. unsigned short family)
  1292. {
  1293. int err = 0;
  1294. struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
  1295. struct net *net = xs_net(*src);
  1296. if (!afinfo)
  1297. return -EAFNOSUPPORT;
  1298. spin_lock_bh(&net->xfrm.xfrm_state_lock);
  1299. if (afinfo->state_sort)
  1300. err = afinfo->state_sort(dst, src, n);
  1301. spin_unlock_bh(&net->xfrm.xfrm_state_lock);
  1302. xfrm_state_put_afinfo(afinfo);
  1303. return err;
  1304. }
  1305. EXPORT_SYMBOL(xfrm_state_sort);
  1306. #endif
  1307. /* Silly enough, but I'm lazy to build resolution list */
  1308. static struct xfrm_state *__xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq)
  1309. {
  1310. int i;
  1311. for (i = 0; i <= net->xfrm.state_hmask; i++) {
  1312. struct xfrm_state *x;
  1313. hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) {
  1314. if (x->km.seq == seq &&
  1315. (mark & x->mark.m) == x->mark.v &&
  1316. x->km.state == XFRM_STATE_ACQ) {
  1317. xfrm_state_hold(x);
  1318. return x;
  1319. }
  1320. }
  1321. }
  1322. return NULL;
  1323. }
  1324. struct xfrm_state *xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq)
  1325. {
  1326. struct xfrm_state *x;
  1327. spin_lock_bh(&net->xfrm.xfrm_state_lock);
  1328. x = __xfrm_find_acq_byseq(net, mark, seq);
  1329. spin_unlock_bh(&net->xfrm.xfrm_state_lock);
  1330. return x;
  1331. }
  1332. EXPORT_SYMBOL(xfrm_find_acq_byseq);
  1333. u32 xfrm_get_acqseq(void)
  1334. {
  1335. u32 res;
  1336. static atomic_t acqseq;
  1337. do {
  1338. res = atomic_inc_return(&acqseq);
  1339. } while (!res);
  1340. return res;
  1341. }
  1342. EXPORT_SYMBOL(xfrm_get_acqseq);
  1343. int verify_spi_info(u8 proto, u32 min, u32 max)
  1344. {
  1345. switch (proto) {
  1346. case IPPROTO_AH:
  1347. case IPPROTO_ESP:
  1348. break;
  1349. case IPPROTO_COMP:
  1350. /* IPCOMP spi is 16-bits. */
  1351. if (max >= 0x10000)
  1352. return -EINVAL;
  1353. break;
  1354. default:
  1355. return -EINVAL;
  1356. }
  1357. if (min > max)
  1358. return -EINVAL;
  1359. return 0;
  1360. }
  1361. EXPORT_SYMBOL(verify_spi_info);
  1362. int xfrm_alloc_spi(struct xfrm_state *x, u32 low, u32 high)
  1363. {
  1364. struct net *net = xs_net(x);
  1365. unsigned int h;
  1366. struct xfrm_state *x0;
  1367. int err = -ENOENT;
  1368. __be32 minspi = htonl(low);
  1369. __be32 maxspi = htonl(high);
  1370. u32 mark = x->mark.v & x->mark.m;
  1371. spin_lock_bh(&x->lock);
  1372. if (x->km.state == XFRM_STATE_DEAD)
  1373. goto unlock;
  1374. err = 0;
  1375. if (x->id.spi)
  1376. goto unlock;
  1377. err = -ENOENT;
  1378. if (minspi == maxspi) {
  1379. x0 = xfrm_state_lookup(net, mark, &x->id.daddr, minspi, x->id.proto, x->props.family);
  1380. if (x0) {
  1381. xfrm_state_put(x0);
  1382. goto unlock;
  1383. }
  1384. x->id.spi = minspi;
  1385. } else {
  1386. u32 spi = 0;
  1387. for (h = 0; h < high-low+1; h++) {
  1388. spi = low + prandom_u32()%(high-low+1);
  1389. x0 = xfrm_state_lookup(net, mark, &x->id.daddr, htonl(spi), x->id.proto, x->props.family);
  1390. if (x0 == NULL) {
  1391. x->id.spi = htonl(spi);
  1392. break;
  1393. }
  1394. xfrm_state_put(x0);
  1395. }
  1396. }
  1397. if (x->id.spi) {
  1398. spin_lock_bh(&net->xfrm.xfrm_state_lock);
  1399. h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto, x->props.family);
  1400. hlist_add_head_rcu(&x->byspi, net->xfrm.state_byspi + h);
  1401. spin_unlock_bh(&net->xfrm.xfrm_state_lock);
  1402. err = 0;
  1403. }
  1404. unlock:
  1405. spin_unlock_bh(&x->lock);
  1406. return err;
  1407. }
  1408. EXPORT_SYMBOL(xfrm_alloc_spi);
  1409. static bool __xfrm_state_filter_match(struct xfrm_state *x,
  1410. struct xfrm_address_filter *filter)
  1411. {
  1412. if (filter) {
  1413. if ((filter->family == AF_INET ||
  1414. filter->family == AF_INET6) &&
  1415. x->props.family != filter->family)
  1416. return false;
  1417. return addr_match(&x->props.saddr, &filter->saddr,
  1418. filter->splen) &&
  1419. addr_match(&x->id.daddr, &filter->daddr,
  1420. filter->dplen);
  1421. }
  1422. return true;
  1423. }
  1424. int xfrm_state_walk(struct net *net, struct xfrm_state_walk *walk,
  1425. int (*func)(struct xfrm_state *, int, void*),
  1426. void *data)
  1427. {
  1428. struct xfrm_state *state;
  1429. struct xfrm_state_walk *x;
  1430. int err = 0;
  1431. if (walk->seq != 0 && list_empty(&walk->all))
  1432. return 0;
  1433. spin_lock_bh(&net->xfrm.xfrm_state_lock);
  1434. if (list_empty(&walk->all))
  1435. x = list_first_entry(&net->xfrm.state_all, struct xfrm_state_walk, all);
  1436. else
  1437. x = list_first_entry(&walk->all, struct xfrm_state_walk, all);
  1438. list_for_each_entry_from(x, &net->xfrm.state_all, all) {
  1439. if (x->state == XFRM_STATE_DEAD)
  1440. continue;
  1441. state = container_of(x, struct xfrm_state, km);
  1442. if (!xfrm_id_proto_match(state->id.proto, walk->proto))
  1443. continue;
  1444. if (!__xfrm_state_filter_match(state, walk->filter))
  1445. continue;
  1446. err = func(state, walk->seq, data);
  1447. if (err) {
  1448. list_move_tail(&walk->all, &x->all);
  1449. goto out;
  1450. }
  1451. walk->seq++;
  1452. }
  1453. if (walk->seq == 0) {
  1454. err = -ENOENT;
  1455. goto out;
  1456. }
  1457. list_del_init(&walk->all);
  1458. out:
  1459. spin_unlock_bh(&net->xfrm.xfrm_state_lock);
  1460. return err;
  1461. }
  1462. EXPORT_SYMBOL(xfrm_state_walk);
  1463. void xfrm_state_walk_init(struct xfrm_state_walk *walk, u8 proto,
  1464. struct xfrm_address_filter *filter)
  1465. {
  1466. INIT_LIST_HEAD(&walk->all);
  1467. walk->proto = proto;
  1468. walk->state = XFRM_STATE_DEAD;
  1469. walk->seq = 0;
  1470. walk->filter = filter;
  1471. }
  1472. EXPORT_SYMBOL(xfrm_state_walk_init);
  1473. void xfrm_state_walk_done(struct xfrm_state_walk *walk, struct net *net)
  1474. {
  1475. kfree(walk->filter);
  1476. if (list_empty(&walk->all))
  1477. return;
  1478. spin_lock_bh(&net->xfrm.xfrm_state_lock);
  1479. list_del(&walk->all);
  1480. spin_unlock_bh(&net->xfrm.xfrm_state_lock);
  1481. }
  1482. EXPORT_SYMBOL(xfrm_state_walk_done);
  1483. static void xfrm_replay_timer_handler(unsigned long data)
  1484. {
  1485. struct xfrm_state *x = (struct xfrm_state *)data;
  1486. spin_lock(&x->lock);
  1487. if (x->km.state == XFRM_STATE_VALID) {
  1488. if (xfrm_aevent_is_on(xs_net(x)))
  1489. x->repl->notify(x, XFRM_REPLAY_TIMEOUT);
  1490. else
  1491. x->xflags |= XFRM_TIME_DEFER;
  1492. }
  1493. spin_unlock(&x->lock);
  1494. }
  1495. static LIST_HEAD(xfrm_km_list);
  1496. void km_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
  1497. {
  1498. struct xfrm_mgr *km;
  1499. rcu_read_lock();
  1500. list_for_each_entry_rcu(km, &xfrm_km_list, list)
  1501. if (km->notify_policy)
  1502. km->notify_policy(xp, dir, c);
  1503. rcu_read_unlock();
  1504. }
  1505. void km_state_notify(struct xfrm_state *x, const struct km_event *c)
  1506. {
  1507. struct xfrm_mgr *km;
  1508. rcu_read_lock();
  1509. list_for_each_entry_rcu(km, &xfrm_km_list, list)
  1510. if (km->notify)
  1511. km->notify(x, c);
  1512. rcu_read_unlock();
  1513. }
  1514. EXPORT_SYMBOL(km_policy_notify);
  1515. EXPORT_SYMBOL(km_state_notify);
  1516. void km_state_expired(struct xfrm_state *x, int hard, u32 portid)
  1517. {
  1518. struct km_event c;
  1519. c.data.hard = hard;
  1520. c.portid = portid;
  1521. c.event = XFRM_MSG_EXPIRE;
  1522. km_state_notify(x, &c);
  1523. }
  1524. EXPORT_SYMBOL(km_state_expired);
  1525. /*
  1526. * We send to all registered managers regardless of failure
  1527. * We are happy with one success
  1528. */
  1529. int km_query(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *pol)
  1530. {
  1531. int err = -EINVAL, acqret;
  1532. struct xfrm_mgr *km;
  1533. rcu_read_lock();
  1534. list_for_each_entry_rcu(km, &xfrm_km_list, list) {
  1535. acqret = km->acquire(x, t, pol);
  1536. if (!acqret)
  1537. err = acqret;
  1538. }
  1539. rcu_read_unlock();
  1540. return err;
  1541. }
  1542. EXPORT_SYMBOL(km_query);
  1543. int km_new_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr, __be16 sport)
  1544. {
  1545. int err = -EINVAL;
  1546. struct xfrm_mgr *km;
  1547. rcu_read_lock();
  1548. list_for_each_entry_rcu(km, &xfrm_km_list, list) {
  1549. if (km->new_mapping)
  1550. err = km->new_mapping(x, ipaddr, sport);
  1551. if (!err)
  1552. break;
  1553. }
  1554. rcu_read_unlock();
  1555. return err;
  1556. }
  1557. EXPORT_SYMBOL(km_new_mapping);
  1558. void km_policy_expired(struct xfrm_policy *pol, int dir, int hard, u32 portid)
  1559. {
  1560. struct km_event c;
  1561. c.data.hard = hard;
  1562. c.portid = portid;
  1563. c.event = XFRM_MSG_POLEXPIRE;
  1564. km_policy_notify(pol, dir, &c);
  1565. }
  1566. EXPORT_SYMBOL(km_policy_expired);
  1567. #ifdef CONFIG_XFRM_MIGRATE
  1568. int km_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
  1569. const struct xfrm_migrate *m, int num_migrate,
  1570. const struct xfrm_kmaddress *k)
  1571. {
  1572. int err = -EINVAL;
  1573. int ret;
  1574. struct xfrm_mgr *km;
  1575. rcu_read_lock();
  1576. list_for_each_entry_rcu(km, &xfrm_km_list, list) {
  1577. if (km->migrate) {
  1578. ret = km->migrate(sel, dir, type, m, num_migrate, k);
  1579. if (!ret)
  1580. err = ret;
  1581. }
  1582. }
  1583. rcu_read_unlock();
  1584. return err;
  1585. }
  1586. EXPORT_SYMBOL(km_migrate);
  1587. #endif
  1588. int km_report(struct net *net, u8 proto, struct xfrm_selector *sel, xfrm_address_t *addr)
  1589. {
  1590. int err = -EINVAL;
  1591. int ret;
  1592. struct xfrm_mgr *km;
  1593. rcu_read_lock();
  1594. list_for_each_entry_rcu(km, &xfrm_km_list, list) {
  1595. if (km->report) {
  1596. ret = km->report(net, proto, sel, addr);
  1597. if (!ret)
  1598. err = ret;
  1599. }
  1600. }
  1601. rcu_read_unlock();
  1602. return err;
  1603. }
  1604. EXPORT_SYMBOL(km_report);
  1605. bool km_is_alive(const struct km_event *c)
  1606. {
  1607. struct xfrm_mgr *km;
  1608. bool is_alive = false;
  1609. rcu_read_lock();
  1610. list_for_each_entry_rcu(km, &xfrm_km_list, list) {
  1611. if (km->is_alive && km->is_alive(c)) {
  1612. is_alive = true;
  1613. break;
  1614. }
  1615. }
  1616. rcu_read_unlock();
  1617. return is_alive;
  1618. }
  1619. EXPORT_SYMBOL(km_is_alive);
  1620. int xfrm_user_policy(struct sock *sk, int optname, u8 __user *optval, int optlen)
  1621. {
  1622. int err;
  1623. u8 *data;
  1624. struct xfrm_mgr *km;
  1625. struct xfrm_policy *pol = NULL;
  1626. if (!optval && !optlen) {
  1627. xfrm_sk_policy_insert(sk, XFRM_POLICY_IN, NULL);
  1628. xfrm_sk_policy_insert(sk, XFRM_POLICY_OUT, NULL);
  1629. __sk_dst_reset(sk);
  1630. return 0;
  1631. }
  1632. if (optlen <= 0 || optlen > PAGE_SIZE)
  1633. return -EMSGSIZE;
  1634. data = kmalloc(optlen, GFP_KERNEL);
  1635. if (!data)
  1636. return -ENOMEM;
  1637. err = -EFAULT;
  1638. if (copy_from_user(data, optval, optlen))
  1639. goto out;
  1640. err = -EINVAL;
  1641. rcu_read_lock();
  1642. list_for_each_entry_rcu(km, &xfrm_km_list, list) {
  1643. pol = km->compile_policy(sk, optname, data,
  1644. optlen, &err);
  1645. if (err >= 0)
  1646. break;
  1647. }
  1648. rcu_read_unlock();
  1649. if (err >= 0) {
  1650. xfrm_sk_policy_insert(sk, err, pol);
  1651. xfrm_pol_put(pol);
  1652. __sk_dst_reset(sk);
  1653. err = 0;
  1654. }
  1655. out:
  1656. kfree(data);
  1657. return err;
  1658. }
  1659. EXPORT_SYMBOL(xfrm_user_policy);
  1660. static DEFINE_SPINLOCK(xfrm_km_lock);
  1661. int xfrm_register_km(struct xfrm_mgr *km)
  1662. {
  1663. spin_lock_bh(&xfrm_km_lock);
  1664. list_add_tail_rcu(&km->list, &xfrm_km_list);
  1665. spin_unlock_bh(&xfrm_km_lock);
  1666. return 0;
  1667. }
  1668. EXPORT_SYMBOL(xfrm_register_km);
  1669. int xfrm_unregister_km(struct xfrm_mgr *km)
  1670. {
  1671. spin_lock_bh(&xfrm_km_lock);
  1672. list_del_rcu(&km->list);
  1673. spin_unlock_bh(&xfrm_km_lock);
  1674. synchronize_rcu();
  1675. return 0;
  1676. }
  1677. EXPORT_SYMBOL(xfrm_unregister_km);
  1678. int xfrm_state_register_afinfo(struct xfrm_state_afinfo *afinfo)
  1679. {
  1680. int err = 0;
  1681. if (unlikely(afinfo == NULL))
  1682. return -EINVAL;
  1683. if (unlikely(afinfo->family >= NPROTO))
  1684. return -EAFNOSUPPORT;
  1685. spin_lock_bh(&xfrm_state_afinfo_lock);
  1686. if (unlikely(xfrm_state_afinfo[afinfo->family] != NULL))
  1687. err = -EEXIST;
  1688. else
  1689. rcu_assign_pointer(xfrm_state_afinfo[afinfo->family], afinfo);
  1690. spin_unlock_bh(&xfrm_state_afinfo_lock);
  1691. return err;
  1692. }
  1693. EXPORT_SYMBOL(xfrm_state_register_afinfo);
  1694. int xfrm_state_unregister_afinfo(struct xfrm_state_afinfo *afinfo)
  1695. {
  1696. int err = 0;
  1697. if (unlikely(afinfo == NULL))
  1698. return -EINVAL;
  1699. if (unlikely(afinfo->family >= NPROTO))
  1700. return -EAFNOSUPPORT;
  1701. spin_lock_bh(&xfrm_state_afinfo_lock);
  1702. if (likely(xfrm_state_afinfo[afinfo->family] != NULL)) {
  1703. if (unlikely(xfrm_state_afinfo[afinfo->family] != afinfo))
  1704. err = -EINVAL;
  1705. else
  1706. RCU_INIT_POINTER(xfrm_state_afinfo[afinfo->family], NULL);
  1707. }
  1708. spin_unlock_bh(&xfrm_state_afinfo_lock);
  1709. synchronize_rcu();
  1710. return err;
  1711. }
  1712. EXPORT_SYMBOL(xfrm_state_unregister_afinfo);
  1713. struct xfrm_state_afinfo *xfrm_state_get_afinfo(unsigned int family)
  1714. {
  1715. struct xfrm_state_afinfo *afinfo;
  1716. if (unlikely(family >= NPROTO))
  1717. return NULL;
  1718. rcu_read_lock();
  1719. afinfo = rcu_dereference(xfrm_state_afinfo[family]);
  1720. if (unlikely(!afinfo))
  1721. rcu_read_unlock();
  1722. return afinfo;
  1723. }
  1724. void xfrm_state_put_afinfo(struct xfrm_state_afinfo *afinfo)
  1725. {
  1726. rcu_read_unlock();
  1727. }
  1728. /* Temporarily located here until net/xfrm/xfrm_tunnel.c is created */
  1729. void xfrm_state_delete_tunnel(struct xfrm_state *x)
  1730. {
  1731. if (x->tunnel) {
  1732. struct xfrm_state *t = x->tunnel;
  1733. if (atomic_read(&t->tunnel_users) == 2)
  1734. xfrm_state_delete(t);
  1735. atomic_dec(&t->tunnel_users);
  1736. xfrm_state_put(t);
  1737. x->tunnel = NULL;
  1738. }
  1739. }
  1740. EXPORT_SYMBOL(xfrm_state_delete_tunnel);
  1741. int xfrm_state_mtu(struct xfrm_state *x, int mtu)
  1742. {
  1743. int res;
  1744. spin_lock_bh(&x->lock);
  1745. if (x->km.state == XFRM_STATE_VALID &&
  1746. x->type && x->type->get_mtu)
  1747. res = x->type->get_mtu(x, mtu);
  1748. else
  1749. res = mtu - x->props.header_len;
  1750. spin_unlock_bh(&x->lock);
  1751. return res;
  1752. }
  1753. int __xfrm_init_state(struct xfrm_state *x, bool init_replay)
  1754. {
  1755. struct xfrm_state_afinfo *afinfo;
  1756. struct xfrm_mode *inner_mode;
  1757. int family = x->props.family;
  1758. int err;
  1759. err = -EAFNOSUPPORT;
  1760. afinfo = xfrm_state_get_afinfo(family);
  1761. if (!afinfo)
  1762. goto error;
  1763. err = 0;
  1764. if (afinfo->init_flags)
  1765. err = afinfo->init_flags(x);
  1766. xfrm_state_put_afinfo(afinfo);
  1767. if (err)
  1768. goto error;
  1769. err = -EPROTONOSUPPORT;
  1770. if (x->sel.family != AF_UNSPEC) {
  1771. inner_mode = xfrm_get_mode(x->props.mode, x->sel.family);
  1772. if (inner_mode == NULL)
  1773. goto error;
  1774. if (!(inner_mode->flags & XFRM_MODE_FLAG_TUNNEL) &&
  1775. family != x->sel.family) {
  1776. xfrm_put_mode(inner_mode);
  1777. goto error;
  1778. }
  1779. x->inner_mode = inner_mode;
  1780. } else {
  1781. struct xfrm_mode *inner_mode_iaf;
  1782. int iafamily = AF_INET;
  1783. inner_mode = xfrm_get_mode(x->props.mode, x->props.family);
  1784. if (inner_mode == NULL)
  1785. goto error;
  1786. if (!(inner_mode->flags & XFRM_MODE_FLAG_TUNNEL)) {
  1787. xfrm_put_mode(inner_mode);
  1788. goto error;
  1789. }
  1790. x->inner_mode = inner_mode;
  1791. if (x->props.family == AF_INET)
  1792. iafamily = AF_INET6;
  1793. inner_mode_iaf = xfrm_get_mode(x->props.mode, iafamily);
  1794. if (inner_mode_iaf) {
  1795. if (inner_mode_iaf->flags & XFRM_MODE_FLAG_TUNNEL)
  1796. x->inner_mode_iaf = inner_mode_iaf;
  1797. else
  1798. xfrm_put_mode(inner_mode_iaf);
  1799. }
  1800. }
  1801. x->type = xfrm_get_type(x->id.proto, family);
  1802. if (x->type == NULL)
  1803. goto error;
  1804. err = x->type->init_state(x);
  1805. if (err)
  1806. goto error;
  1807. x->outer_mode = xfrm_get_mode(x->props.mode, family);
  1808. if (x->outer_mode == NULL) {
  1809. err = -EPROTONOSUPPORT;
  1810. goto error;
  1811. }
  1812. if (init_replay) {
  1813. err = xfrm_init_replay(x);
  1814. if (err)
  1815. goto error;
  1816. }
  1817. x->km.state = XFRM_STATE_VALID;
  1818. error:
  1819. return err;
  1820. }
  1821. EXPORT_SYMBOL(__xfrm_init_state);
  1822. int xfrm_init_state(struct xfrm_state *x)
  1823. {
  1824. return __xfrm_init_state(x, true);
  1825. }
  1826. EXPORT_SYMBOL(xfrm_init_state);
  1827. int __net_init xfrm_state_init(struct net *net)
  1828. {
  1829. unsigned int sz;
  1830. INIT_LIST_HEAD(&net->xfrm.state_all);
  1831. sz = sizeof(struct hlist_head) * 8;
  1832. net->xfrm.state_bydst = xfrm_hash_alloc(sz);
  1833. if (!net->xfrm.state_bydst)
  1834. goto out_bydst;
  1835. net->xfrm.state_bysrc = xfrm_hash_alloc(sz);
  1836. if (!net->xfrm.state_bysrc)
  1837. goto out_bysrc;
  1838. net->xfrm.state_byspi = xfrm_hash_alloc(sz);
  1839. if (!net->xfrm.state_byspi)
  1840. goto out_byspi;
  1841. net->xfrm.state_hmask = ((sz / sizeof(struct hlist_head)) - 1);
  1842. net->xfrm.state_num = 0;
  1843. INIT_WORK(&net->xfrm.state_hash_work, xfrm_hash_resize);
  1844. spin_lock_init(&net->xfrm.xfrm_state_lock);
  1845. return 0;
  1846. out_byspi:
  1847. xfrm_hash_free(net->xfrm.state_bysrc, sz);
  1848. out_bysrc:
  1849. xfrm_hash_free(net->xfrm.state_bydst, sz);
  1850. out_bydst:
  1851. return -ENOMEM;
  1852. }
  1853. void xfrm_state_fini(struct net *net)
  1854. {
  1855. unsigned int sz;
  1856. flush_work(&net->xfrm.state_hash_work);
  1857. xfrm_state_flush(net, 0, false);
  1858. flush_work(&xfrm_state_gc_work);
  1859. WARN_ON(!list_empty(&net->xfrm.state_all));
  1860. sz = (net->xfrm.state_hmask + 1) * sizeof(struct hlist_head);
  1861. WARN_ON(!hlist_empty(net->xfrm.state_byspi));
  1862. xfrm_hash_free(net->xfrm.state_byspi, sz);
  1863. WARN_ON(!hlist_empty(net->xfrm.state_bysrc));
  1864. xfrm_hash_free(net->xfrm.state_bysrc, sz);
  1865. WARN_ON(!hlist_empty(net->xfrm.state_bydst));
  1866. xfrm_hash_free(net->xfrm.state_bydst, sz);
  1867. }
  1868. #ifdef CONFIG_AUDITSYSCALL
  1869. static void xfrm_audit_helper_sainfo(struct xfrm_state *x,
  1870. struct audit_buffer *audit_buf)
  1871. {
  1872. struct xfrm_sec_ctx *ctx = x->security;
  1873. u32 spi = ntohl(x->id.spi);
  1874. if (ctx)
  1875. audit_log_format(audit_buf, " sec_alg=%u sec_doi=%u sec_obj=%s",
  1876. ctx->ctx_alg, ctx->ctx_doi, ctx->ctx_str);
  1877. switch (x->props.family) {
  1878. case AF_INET:
  1879. audit_log_format(audit_buf, " src=%pI4 dst=%pI4",
  1880. &x->props.saddr.a4, &x->id.daddr.a4);
  1881. break;
  1882. case AF_INET6:
  1883. audit_log_format(audit_buf, " src=%pI6 dst=%pI6",
  1884. x->props.saddr.a6, x->id.daddr.a6);
  1885. break;
  1886. }
  1887. audit_log_format(audit_buf, " spi=%u(0x%x)", spi, spi);
  1888. }
  1889. static void xfrm_audit_helper_pktinfo(struct sk_buff *skb, u16 family,
  1890. struct audit_buffer *audit_buf)
  1891. {
  1892. const struct iphdr *iph4;
  1893. const struct ipv6hdr *iph6;
  1894. switch (family) {
  1895. case AF_INET:
  1896. iph4 = ip_hdr(skb);
  1897. audit_log_format(audit_buf, " src=%pI4 dst=%pI4",
  1898. &iph4->saddr, &iph4->daddr);
  1899. break;
  1900. case AF_INET6:
  1901. iph6 = ipv6_hdr(skb);
  1902. audit_log_format(audit_buf,
  1903. " src=%pI6 dst=%pI6 flowlbl=0x%x%02x%02x",
  1904. &iph6->saddr, &iph6->daddr,
  1905. iph6->flow_lbl[0] & 0x0f,
  1906. iph6->flow_lbl[1],
  1907. iph6->flow_lbl[2]);
  1908. break;
  1909. }
  1910. }
  1911. void xfrm_audit_state_add(struct xfrm_state *x, int result, bool task_valid)
  1912. {
  1913. struct audit_buffer *audit_buf;
  1914. audit_buf = xfrm_audit_start("SAD-add");
  1915. if (audit_buf == NULL)
  1916. return;
  1917. xfrm_audit_helper_usrinfo(task_valid, audit_buf);
  1918. xfrm_audit_helper_sainfo(x, audit_buf);
  1919. audit_log_format(audit_buf, " res=%u", result);
  1920. audit_log_end(audit_buf);
  1921. }
  1922. EXPORT_SYMBOL_GPL(xfrm_audit_state_add);
  1923. void xfrm_audit_state_delete(struct xfrm_state *x, int result, bool task_valid)
  1924. {
  1925. struct audit_buffer *audit_buf;
  1926. audit_buf = xfrm_audit_start("SAD-delete");
  1927. if (audit_buf == NULL)
  1928. return;
  1929. xfrm_audit_helper_usrinfo(task_valid, audit_buf);
  1930. xfrm_audit_helper_sainfo(x, audit_buf);
  1931. audit_log_format(audit_buf, " res=%u", result);
  1932. audit_log_end(audit_buf);
  1933. }
  1934. EXPORT_SYMBOL_GPL(xfrm_audit_state_delete);
  1935. void xfrm_audit_state_replay_overflow(struct xfrm_state *x,
  1936. struct sk_buff *skb)
  1937. {
  1938. struct audit_buffer *audit_buf;
  1939. u32 spi;
  1940. audit_buf = xfrm_audit_start("SA-replay-overflow");
  1941. if (audit_buf == NULL)
  1942. return;
  1943. xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
  1944. /* don't record the sequence number because it's inherent in this kind
  1945. * of audit message */
  1946. spi = ntohl(x->id.spi);
  1947. audit_log_format(audit_buf, " spi=%u(0x%x)", spi, spi);
  1948. audit_log_end(audit_buf);
  1949. }
  1950. EXPORT_SYMBOL_GPL(xfrm_audit_state_replay_overflow);
  1951. void xfrm_audit_state_replay(struct xfrm_state *x,
  1952. struct sk_buff *skb, __be32 net_seq)
  1953. {
  1954. struct audit_buffer *audit_buf;
  1955. u32 spi;
  1956. audit_buf = xfrm_audit_start("SA-replayed-pkt");
  1957. if (audit_buf == NULL)
  1958. return;
  1959. xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
  1960. spi = ntohl(x->id.spi);
  1961. audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
  1962. spi, spi, ntohl(net_seq));
  1963. audit_log_end(audit_buf);
  1964. }
  1965. EXPORT_SYMBOL_GPL(xfrm_audit_state_replay);
  1966. void xfrm_audit_state_notfound_simple(struct sk_buff *skb, u16 family)
  1967. {
  1968. struct audit_buffer *audit_buf;
  1969. audit_buf = xfrm_audit_start("SA-notfound");
  1970. if (audit_buf == NULL)
  1971. return;
  1972. xfrm_audit_helper_pktinfo(skb, family, audit_buf);
  1973. audit_log_end(audit_buf);
  1974. }
  1975. EXPORT_SYMBOL_GPL(xfrm_audit_state_notfound_simple);
  1976. void xfrm_audit_state_notfound(struct sk_buff *skb, u16 family,
  1977. __be32 net_spi, __be32 net_seq)
  1978. {
  1979. struct audit_buffer *audit_buf;
  1980. u32 spi;
  1981. audit_buf = xfrm_audit_start("SA-notfound");
  1982. if (audit_buf == NULL)
  1983. return;
  1984. xfrm_audit_helper_pktinfo(skb, family, audit_buf);
  1985. spi = ntohl(net_spi);
  1986. audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
  1987. spi, spi, ntohl(net_seq));
  1988. audit_log_end(audit_buf);
  1989. }
  1990. EXPORT_SYMBOL_GPL(xfrm_audit_state_notfound);
  1991. void xfrm_audit_state_icvfail(struct xfrm_state *x,
  1992. struct sk_buff *skb, u8 proto)
  1993. {
  1994. struct audit_buffer *audit_buf;
  1995. __be32 net_spi;
  1996. __be32 net_seq;
  1997. audit_buf = xfrm_audit_start("SA-icv-failure");
  1998. if (audit_buf == NULL)
  1999. return;
  2000. xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
  2001. if (xfrm_parse_spi(skb, proto, &net_spi, &net_seq) == 0) {
  2002. u32 spi = ntohl(net_spi);
  2003. audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
  2004. spi, spi, ntohl(net_seq));
  2005. }
  2006. audit_log_end(audit_buf);
  2007. }
  2008. EXPORT_SYMBOL_GPL(xfrm_audit_state_icvfail);
  2009. #endif /* CONFIG_AUDITSYSCALL */