xfrm_state.c 82 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * xfrm_state.c
  4. *
  5. * Changes:
  6. * Mitsuru KANDA @USAGI
  7. * Kazunori MIYAZAWA @USAGI
  8. * Kunihiro Ishiguro <kunihiro@ipinfusion.com>
  9. * IPv6 support
  10. * YOSHIFUJI Hideaki @USAGI
  11. * Split up af-specific functions
  12. * Derek Atkins <derek@ihtfp.com>
  13. * Add UDP Encapsulation
  14. *
  15. */
  16. #include <linux/compat.h>
  17. #include <linux/workqueue.h>
  18. #include <net/xfrm.h>
  19. #include <linux/pfkeyv2.h>
  20. #include <linux/ipsec.h>
  21. #include <linux/module.h>
  22. #include <linux/cache.h>
  23. #include <linux/audit.h>
  24. #include <linux/uaccess.h>
  25. #include <linux/ktime.h>
  26. #include <linux/slab.h>
  27. #include <linux/interrupt.h>
  28. #include <linux/kernel.h>
  29. #include <crypto/aead.h>
  30. #include "xfrm_hash.h"
  31. #define xfrm_state_deref_prot(table, net) \
  32. rcu_dereference_protected((table), lockdep_is_held(&(net)->xfrm.xfrm_state_lock))
  33. #define xfrm_state_deref_check(table, net) \
  34. rcu_dereference_check((table), lockdep_is_held(&(net)->xfrm.xfrm_state_lock))
  35. static void xfrm_state_gc_task(struct work_struct *work);
  36. /* Each xfrm_state may be linked to two tables:
  37. 1. Hash table by (spi,daddr,ah/esp) to find SA by SPI. (input,ctl)
  38. 2. Hash table by (daddr,family,reqid) to find what SAs exist for given
  39. destination/tunnel endpoint. (output)
  40. */
  41. static unsigned int xfrm_state_hashmax __read_mostly = 1 * 1024 * 1024;
  42. static struct kmem_cache *xfrm_state_cache __ro_after_init;
  43. static DECLARE_WORK(xfrm_state_gc_work, xfrm_state_gc_task);
  44. static HLIST_HEAD(xfrm_state_gc_list);
  45. static HLIST_HEAD(xfrm_state_dev_gc_list);
  46. static inline bool xfrm_state_hold_rcu(struct xfrm_state __rcu *x)
  47. {
  48. return refcount_inc_not_zero(&x->refcnt);
  49. }
  50. static inline unsigned int xfrm_dst_hash(struct net *net,
  51. const xfrm_address_t *daddr,
  52. const xfrm_address_t *saddr,
  53. u32 reqid,
  54. unsigned short family)
  55. {
  56. lockdep_assert_held(&net->xfrm.xfrm_state_lock);
  57. return __xfrm_dst_hash(daddr, saddr, reqid, family, net->xfrm.state_hmask);
  58. }
  59. static inline unsigned int xfrm_src_hash(struct net *net,
  60. const xfrm_address_t *daddr,
  61. const xfrm_address_t *saddr,
  62. unsigned short family)
  63. {
  64. lockdep_assert_held(&net->xfrm.xfrm_state_lock);
  65. return __xfrm_src_hash(daddr, saddr, family, net->xfrm.state_hmask);
  66. }
  67. static inline unsigned int
  68. xfrm_spi_hash(struct net *net, const xfrm_address_t *daddr,
  69. __be32 spi, u8 proto, unsigned short family)
  70. {
  71. lockdep_assert_held(&net->xfrm.xfrm_state_lock);
  72. return __xfrm_spi_hash(daddr, spi, proto, family, net->xfrm.state_hmask);
  73. }
  74. static unsigned int xfrm_seq_hash(struct net *net, u32 seq)
  75. {
  76. lockdep_assert_held(&net->xfrm.xfrm_state_lock);
  77. return __xfrm_seq_hash(seq, net->xfrm.state_hmask);
  78. }
  79. #define XFRM_STATE_INSERT(by, _n, _h, _type) \
  80. { \
  81. struct xfrm_state *_x = NULL; \
  82. \
  83. if (_type != XFRM_DEV_OFFLOAD_PACKET) { \
  84. hlist_for_each_entry_rcu(_x, _h, by) { \
  85. if (_x->xso.type == XFRM_DEV_OFFLOAD_PACKET) \
  86. continue; \
  87. break; \
  88. } \
  89. } \
  90. \
  91. if (!_x || _x->xso.type == XFRM_DEV_OFFLOAD_PACKET) \
  92. /* SAD is empty or consist from HW SAs only */ \
  93. hlist_add_head_rcu(_n, _h); \
  94. else \
  95. hlist_add_before_rcu(_n, &_x->by); \
  96. }
  97. static void xfrm_hash_transfer(struct hlist_head *list,
  98. struct hlist_head *ndsttable,
  99. struct hlist_head *nsrctable,
  100. struct hlist_head *nspitable,
  101. struct hlist_head *nseqtable,
  102. unsigned int nhashmask)
  103. {
  104. struct hlist_node *tmp;
  105. struct xfrm_state *x;
  106. hlist_for_each_entry_safe(x, tmp, list, bydst) {
  107. unsigned int h;
  108. h = __xfrm_dst_hash(&x->id.daddr, &x->props.saddr,
  109. x->props.reqid, x->props.family,
  110. nhashmask);
  111. XFRM_STATE_INSERT(bydst, &x->bydst, ndsttable + h, x->xso.type);
  112. h = __xfrm_src_hash(&x->id.daddr, &x->props.saddr,
  113. x->props.family,
  114. nhashmask);
  115. XFRM_STATE_INSERT(bysrc, &x->bysrc, nsrctable + h, x->xso.type);
  116. if (x->id.spi) {
  117. h = __xfrm_spi_hash(&x->id.daddr, x->id.spi,
  118. x->id.proto, x->props.family,
  119. nhashmask);
  120. XFRM_STATE_INSERT(byspi, &x->byspi, nspitable + h,
  121. x->xso.type);
  122. }
  123. if (x->km.seq) {
  124. h = __xfrm_seq_hash(x->km.seq, nhashmask);
  125. XFRM_STATE_INSERT(byseq, &x->byseq, nseqtable + h,
  126. x->xso.type);
  127. }
  128. }
  129. }
  130. static unsigned long xfrm_hash_new_size(unsigned int state_hmask)
  131. {
  132. return ((state_hmask + 1) << 1) * sizeof(struct hlist_head);
  133. }
  134. static void xfrm_hash_resize(struct work_struct *work)
  135. {
  136. struct net *net = container_of(work, struct net, xfrm.state_hash_work);
  137. struct hlist_head *ndst, *nsrc, *nspi, *nseq, *odst, *osrc, *ospi, *oseq;
  138. unsigned long nsize, osize;
  139. unsigned int nhashmask, ohashmask;
  140. int i;
  141. nsize = xfrm_hash_new_size(net->xfrm.state_hmask);
  142. ndst = xfrm_hash_alloc(nsize);
  143. if (!ndst)
  144. return;
  145. nsrc = xfrm_hash_alloc(nsize);
  146. if (!nsrc) {
  147. xfrm_hash_free(ndst, nsize);
  148. return;
  149. }
  150. nspi = xfrm_hash_alloc(nsize);
  151. if (!nspi) {
  152. xfrm_hash_free(ndst, nsize);
  153. xfrm_hash_free(nsrc, nsize);
  154. return;
  155. }
  156. nseq = xfrm_hash_alloc(nsize);
  157. if (!nseq) {
  158. xfrm_hash_free(ndst, nsize);
  159. xfrm_hash_free(nsrc, nsize);
  160. xfrm_hash_free(nspi, nsize);
  161. return;
  162. }
  163. spin_lock_bh(&net->xfrm.xfrm_state_lock);
  164. write_seqcount_begin(&net->xfrm.xfrm_state_hash_generation);
  165. nhashmask = (nsize / sizeof(struct hlist_head)) - 1U;
  166. odst = xfrm_state_deref_prot(net->xfrm.state_bydst, net);
  167. for (i = net->xfrm.state_hmask; i >= 0; i--)
  168. xfrm_hash_transfer(odst + i, ndst, nsrc, nspi, nseq, nhashmask);
  169. osrc = xfrm_state_deref_prot(net->xfrm.state_bysrc, net);
  170. ospi = xfrm_state_deref_prot(net->xfrm.state_byspi, net);
  171. oseq = xfrm_state_deref_prot(net->xfrm.state_byseq, net);
  172. ohashmask = net->xfrm.state_hmask;
  173. rcu_assign_pointer(net->xfrm.state_bydst, ndst);
  174. rcu_assign_pointer(net->xfrm.state_bysrc, nsrc);
  175. rcu_assign_pointer(net->xfrm.state_byspi, nspi);
  176. rcu_assign_pointer(net->xfrm.state_byseq, nseq);
  177. net->xfrm.state_hmask = nhashmask;
  178. write_seqcount_end(&net->xfrm.xfrm_state_hash_generation);
  179. spin_unlock_bh(&net->xfrm.xfrm_state_lock);
  180. osize = (ohashmask + 1) * sizeof(struct hlist_head);
  181. synchronize_rcu();
  182. xfrm_hash_free(odst, osize);
  183. xfrm_hash_free(osrc, osize);
  184. xfrm_hash_free(ospi, osize);
  185. xfrm_hash_free(oseq, osize);
  186. }
  187. static DEFINE_SPINLOCK(xfrm_state_afinfo_lock);
  188. static struct xfrm_state_afinfo __rcu *xfrm_state_afinfo[NPROTO];
  189. static DEFINE_SPINLOCK(xfrm_state_gc_lock);
  190. static DEFINE_SPINLOCK(xfrm_state_dev_gc_lock);
  191. int __xfrm_state_delete(struct xfrm_state *x);
  192. int km_query(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *pol);
  193. static bool km_is_alive(const struct km_event *c);
  194. void km_state_expired(struct xfrm_state *x, int hard, u32 portid);
  195. int xfrm_register_type(const struct xfrm_type *type, unsigned short family)
  196. {
  197. struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
  198. int err = 0;
  199. if (!afinfo)
  200. return -EAFNOSUPPORT;
  201. #define X(afi, T, name) do { \
  202. WARN_ON((afi)->type_ ## name); \
  203. (afi)->type_ ## name = (T); \
  204. } while (0)
  205. switch (type->proto) {
  206. case IPPROTO_COMP:
  207. X(afinfo, type, comp);
  208. break;
  209. case IPPROTO_AH:
  210. X(afinfo, type, ah);
  211. break;
  212. case IPPROTO_ESP:
  213. X(afinfo, type, esp);
  214. break;
  215. case IPPROTO_IPIP:
  216. X(afinfo, type, ipip);
  217. break;
  218. case IPPROTO_DSTOPTS:
  219. X(afinfo, type, dstopts);
  220. break;
  221. case IPPROTO_ROUTING:
  222. X(afinfo, type, routing);
  223. break;
  224. case IPPROTO_IPV6:
  225. X(afinfo, type, ipip6);
  226. break;
  227. default:
  228. WARN_ON(1);
  229. err = -EPROTONOSUPPORT;
  230. break;
  231. }
  232. #undef X
  233. rcu_read_unlock();
  234. return err;
  235. }
  236. EXPORT_SYMBOL(xfrm_register_type);
  237. void xfrm_unregister_type(const struct xfrm_type *type, unsigned short family)
  238. {
  239. struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
  240. if (unlikely(afinfo == NULL))
  241. return;
  242. #define X(afi, T, name) do { \
  243. WARN_ON((afi)->type_ ## name != (T)); \
  244. (afi)->type_ ## name = NULL; \
  245. } while (0)
  246. switch (type->proto) {
  247. case IPPROTO_COMP:
  248. X(afinfo, type, comp);
  249. break;
  250. case IPPROTO_AH:
  251. X(afinfo, type, ah);
  252. break;
  253. case IPPROTO_ESP:
  254. X(afinfo, type, esp);
  255. break;
  256. case IPPROTO_IPIP:
  257. X(afinfo, type, ipip);
  258. break;
  259. case IPPROTO_DSTOPTS:
  260. X(afinfo, type, dstopts);
  261. break;
  262. case IPPROTO_ROUTING:
  263. X(afinfo, type, routing);
  264. break;
  265. case IPPROTO_IPV6:
  266. X(afinfo, type, ipip6);
  267. break;
  268. default:
  269. WARN_ON(1);
  270. break;
  271. }
  272. #undef X
  273. rcu_read_unlock();
  274. }
  275. EXPORT_SYMBOL(xfrm_unregister_type);
  276. static const struct xfrm_type *xfrm_get_type(u8 proto, unsigned short family)
  277. {
  278. const struct xfrm_type *type = NULL;
  279. struct xfrm_state_afinfo *afinfo;
  280. int modload_attempted = 0;
  281. retry:
  282. afinfo = xfrm_state_get_afinfo(family);
  283. if (unlikely(afinfo == NULL))
  284. return NULL;
  285. switch (proto) {
  286. case IPPROTO_COMP:
  287. type = afinfo->type_comp;
  288. break;
  289. case IPPROTO_AH:
  290. type = afinfo->type_ah;
  291. break;
  292. case IPPROTO_ESP:
  293. type = afinfo->type_esp;
  294. break;
  295. case IPPROTO_IPIP:
  296. type = afinfo->type_ipip;
  297. break;
  298. case IPPROTO_DSTOPTS:
  299. type = afinfo->type_dstopts;
  300. break;
  301. case IPPROTO_ROUTING:
  302. type = afinfo->type_routing;
  303. break;
  304. case IPPROTO_IPV6:
  305. type = afinfo->type_ipip6;
  306. break;
  307. default:
  308. break;
  309. }
  310. if (unlikely(type && !try_module_get(type->owner)))
  311. type = NULL;
  312. rcu_read_unlock();
  313. if (!type && !modload_attempted) {
  314. request_module("xfrm-type-%d-%d", family, proto);
  315. modload_attempted = 1;
  316. goto retry;
  317. }
  318. return type;
  319. }
  320. static void xfrm_put_type(const struct xfrm_type *type)
  321. {
  322. module_put(type->owner);
  323. }
  324. int xfrm_register_type_offload(const struct xfrm_type_offload *type,
  325. unsigned short family)
  326. {
  327. struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
  328. int err = 0;
  329. if (unlikely(afinfo == NULL))
  330. return -EAFNOSUPPORT;
  331. switch (type->proto) {
  332. case IPPROTO_ESP:
  333. WARN_ON(afinfo->type_offload_esp);
  334. afinfo->type_offload_esp = type;
  335. break;
  336. default:
  337. WARN_ON(1);
  338. err = -EPROTONOSUPPORT;
  339. break;
  340. }
  341. rcu_read_unlock();
  342. return err;
  343. }
  344. EXPORT_SYMBOL(xfrm_register_type_offload);
  345. void xfrm_unregister_type_offload(const struct xfrm_type_offload *type,
  346. unsigned short family)
  347. {
  348. struct xfrm_state_afinfo *afinfo = xfrm_state_get_afinfo(family);
  349. if (unlikely(afinfo == NULL))
  350. return;
  351. switch (type->proto) {
  352. case IPPROTO_ESP:
  353. WARN_ON(afinfo->type_offload_esp != type);
  354. afinfo->type_offload_esp = NULL;
  355. break;
  356. default:
  357. WARN_ON(1);
  358. break;
  359. }
  360. rcu_read_unlock();
  361. }
  362. EXPORT_SYMBOL(xfrm_unregister_type_offload);
  363. static const struct xfrm_type_offload *
  364. xfrm_get_type_offload(u8 proto, unsigned short family, bool try_load)
  365. {
  366. const struct xfrm_type_offload *type = NULL;
  367. struct xfrm_state_afinfo *afinfo;
  368. retry:
  369. afinfo = xfrm_state_get_afinfo(family);
  370. if (unlikely(afinfo == NULL))
  371. return NULL;
  372. switch (proto) {
  373. case IPPROTO_ESP:
  374. type = afinfo->type_offload_esp;
  375. break;
  376. default:
  377. break;
  378. }
  379. if ((type && !try_module_get(type->owner)))
  380. type = NULL;
  381. rcu_read_unlock();
  382. if (!type && try_load) {
  383. request_module("xfrm-offload-%d-%d", family, proto);
  384. try_load = false;
  385. goto retry;
  386. }
  387. return type;
  388. }
  389. static void xfrm_put_type_offload(const struct xfrm_type_offload *type)
  390. {
  391. module_put(type->owner);
  392. }
  393. static const struct xfrm_mode xfrm4_mode_map[XFRM_MODE_MAX] = {
  394. [XFRM_MODE_BEET] = {
  395. .encap = XFRM_MODE_BEET,
  396. .flags = XFRM_MODE_FLAG_TUNNEL,
  397. .family = AF_INET,
  398. },
  399. [XFRM_MODE_TRANSPORT] = {
  400. .encap = XFRM_MODE_TRANSPORT,
  401. .family = AF_INET,
  402. },
  403. [XFRM_MODE_TUNNEL] = {
  404. .encap = XFRM_MODE_TUNNEL,
  405. .flags = XFRM_MODE_FLAG_TUNNEL,
  406. .family = AF_INET,
  407. },
  408. };
  409. static const struct xfrm_mode xfrm6_mode_map[XFRM_MODE_MAX] = {
  410. [XFRM_MODE_BEET] = {
  411. .encap = XFRM_MODE_BEET,
  412. .flags = XFRM_MODE_FLAG_TUNNEL,
  413. .family = AF_INET6,
  414. },
  415. [XFRM_MODE_ROUTEOPTIMIZATION] = {
  416. .encap = XFRM_MODE_ROUTEOPTIMIZATION,
  417. .family = AF_INET6,
  418. },
  419. [XFRM_MODE_TRANSPORT] = {
  420. .encap = XFRM_MODE_TRANSPORT,
  421. .family = AF_INET6,
  422. },
  423. [XFRM_MODE_TUNNEL] = {
  424. .encap = XFRM_MODE_TUNNEL,
  425. .flags = XFRM_MODE_FLAG_TUNNEL,
  426. .family = AF_INET6,
  427. },
  428. };
  429. static const struct xfrm_mode *xfrm_get_mode(unsigned int encap, int family)
  430. {
  431. const struct xfrm_mode *mode;
  432. if (unlikely(encap >= XFRM_MODE_MAX))
  433. return NULL;
  434. switch (family) {
  435. case AF_INET:
  436. mode = &xfrm4_mode_map[encap];
  437. if (mode->family == family)
  438. return mode;
  439. break;
  440. case AF_INET6:
  441. mode = &xfrm6_mode_map[encap];
  442. if (mode->family == family)
  443. return mode;
  444. break;
  445. default:
  446. break;
  447. }
  448. return NULL;
  449. }
  450. void xfrm_state_free(struct xfrm_state *x)
  451. {
  452. kmem_cache_free(xfrm_state_cache, x);
  453. }
  454. EXPORT_SYMBOL(xfrm_state_free);
  455. static void ___xfrm_state_destroy(struct xfrm_state *x)
  456. {
  457. hrtimer_cancel(&x->mtimer);
  458. del_timer_sync(&x->rtimer);
  459. kfree(x->aead);
  460. kfree(x->aalg);
  461. kfree(x->ealg);
  462. kfree(x->calg);
  463. kfree(x->encap);
  464. kfree(x->coaddr);
  465. kfree(x->replay_esn);
  466. kfree(x->preplay_esn);
  467. if (x->type_offload)
  468. xfrm_put_type_offload(x->type_offload);
  469. if (x->type) {
  470. x->type->destructor(x);
  471. xfrm_put_type(x->type);
  472. }
  473. if (x->xfrag.page)
  474. put_page(x->xfrag.page);
  475. xfrm_dev_state_free(x);
  476. security_xfrm_state_free(x);
  477. xfrm_state_free(x);
  478. }
  479. static void xfrm_state_gc_task(struct work_struct *work)
  480. {
  481. struct xfrm_state *x;
  482. struct hlist_node *tmp;
  483. struct hlist_head gc_list;
  484. spin_lock_bh(&xfrm_state_gc_lock);
  485. hlist_move_list(&xfrm_state_gc_list, &gc_list);
  486. spin_unlock_bh(&xfrm_state_gc_lock);
  487. synchronize_rcu();
  488. hlist_for_each_entry_safe(x, tmp, &gc_list, gclist)
  489. ___xfrm_state_destroy(x);
  490. }
  491. static enum hrtimer_restart xfrm_timer_handler(struct hrtimer *me)
  492. {
  493. struct xfrm_state *x = container_of(me, struct xfrm_state, mtimer);
  494. enum hrtimer_restart ret = HRTIMER_NORESTART;
  495. time64_t now = ktime_get_real_seconds();
  496. time64_t next = TIME64_MAX;
  497. int warn = 0;
  498. int err = 0;
  499. spin_lock(&x->lock);
  500. xfrm_dev_state_update_stats(x);
  501. if (x->km.state == XFRM_STATE_DEAD)
  502. goto out;
  503. if (x->km.state == XFRM_STATE_EXPIRED)
  504. goto expired;
  505. if (x->lft.hard_add_expires_seconds) {
  506. time64_t tmo = x->lft.hard_add_expires_seconds +
  507. x->curlft.add_time - now;
  508. if (tmo <= 0) {
  509. if (x->xflags & XFRM_SOFT_EXPIRE) {
  510. /* enter hard expire without soft expire first?!
  511. * setting a new date could trigger this.
  512. * workaround: fix x->curflt.add_time by below:
  513. */
  514. x->curlft.add_time = now - x->saved_tmo - 1;
  515. tmo = x->lft.hard_add_expires_seconds - x->saved_tmo;
  516. } else
  517. goto expired;
  518. }
  519. if (tmo < next)
  520. next = tmo;
  521. }
  522. if (x->lft.hard_use_expires_seconds) {
  523. time64_t tmo = x->lft.hard_use_expires_seconds +
  524. (READ_ONCE(x->curlft.use_time) ? : now) - now;
  525. if (tmo <= 0)
  526. goto expired;
  527. if (tmo < next)
  528. next = tmo;
  529. }
  530. if (x->km.dying)
  531. goto resched;
  532. if (x->lft.soft_add_expires_seconds) {
  533. time64_t tmo = x->lft.soft_add_expires_seconds +
  534. x->curlft.add_time - now;
  535. if (tmo <= 0) {
  536. warn = 1;
  537. x->xflags &= ~XFRM_SOFT_EXPIRE;
  538. } else if (tmo < next) {
  539. next = tmo;
  540. x->xflags |= XFRM_SOFT_EXPIRE;
  541. x->saved_tmo = tmo;
  542. }
  543. }
  544. if (x->lft.soft_use_expires_seconds) {
  545. time64_t tmo = x->lft.soft_use_expires_seconds +
  546. (READ_ONCE(x->curlft.use_time) ? : now) - now;
  547. if (tmo <= 0)
  548. warn = 1;
  549. else if (tmo < next)
  550. next = tmo;
  551. }
  552. x->km.dying = warn;
  553. if (warn)
  554. km_state_expired(x, 0, 0);
  555. resched:
  556. if (next != TIME64_MAX) {
  557. hrtimer_forward_now(&x->mtimer, ktime_set(next, 0));
  558. ret = HRTIMER_RESTART;
  559. }
  560. goto out;
  561. expired:
  562. if (x->km.state == XFRM_STATE_ACQ && x->id.spi == 0)
  563. x->km.state = XFRM_STATE_EXPIRED;
  564. err = __xfrm_state_delete(x);
  565. if (!err)
  566. km_state_expired(x, 1, 0);
  567. xfrm_audit_state_delete(x, err ? 0 : 1, true);
  568. out:
  569. spin_unlock(&x->lock);
  570. return ret;
  571. }
  572. static void xfrm_replay_timer_handler(struct timer_list *t);
  573. struct xfrm_state *xfrm_state_alloc(struct net *net)
  574. {
  575. struct xfrm_state *x;
  576. x = kmem_cache_zalloc(xfrm_state_cache, GFP_ATOMIC);
  577. if (x) {
  578. write_pnet(&x->xs_net, net);
  579. refcount_set(&x->refcnt, 1);
  580. atomic_set(&x->tunnel_users, 0);
  581. INIT_LIST_HEAD(&x->km.all);
  582. INIT_HLIST_NODE(&x->state_cache);
  583. INIT_HLIST_NODE(&x->bydst);
  584. INIT_HLIST_NODE(&x->bysrc);
  585. INIT_HLIST_NODE(&x->byspi);
  586. INIT_HLIST_NODE(&x->byseq);
  587. hrtimer_init(&x->mtimer, CLOCK_BOOTTIME, HRTIMER_MODE_ABS_SOFT);
  588. x->mtimer.function = xfrm_timer_handler;
  589. timer_setup(&x->rtimer, xfrm_replay_timer_handler, 0);
  590. x->curlft.add_time = ktime_get_real_seconds();
  591. x->lft.soft_byte_limit = XFRM_INF;
  592. x->lft.soft_packet_limit = XFRM_INF;
  593. x->lft.hard_byte_limit = XFRM_INF;
  594. x->lft.hard_packet_limit = XFRM_INF;
  595. x->replay_maxage = 0;
  596. x->replay_maxdiff = 0;
  597. x->pcpu_num = UINT_MAX;
  598. spin_lock_init(&x->lock);
  599. }
  600. return x;
  601. }
  602. EXPORT_SYMBOL(xfrm_state_alloc);
  603. #ifdef CONFIG_XFRM_OFFLOAD
  604. void xfrm_dev_state_delete(struct xfrm_state *x)
  605. {
  606. struct xfrm_dev_offload *xso = &x->xso;
  607. struct net_device *dev = READ_ONCE(xso->dev);
  608. if (dev) {
  609. dev->xfrmdev_ops->xdo_dev_state_delete(x);
  610. spin_lock_bh(&xfrm_state_dev_gc_lock);
  611. hlist_add_head(&x->dev_gclist, &xfrm_state_dev_gc_list);
  612. spin_unlock_bh(&xfrm_state_dev_gc_lock);
  613. }
  614. }
  615. EXPORT_SYMBOL_GPL(xfrm_dev_state_delete);
  616. void xfrm_dev_state_free(struct xfrm_state *x)
  617. {
  618. struct xfrm_dev_offload *xso = &x->xso;
  619. struct net_device *dev = READ_ONCE(xso->dev);
  620. if (dev && dev->xfrmdev_ops) {
  621. spin_lock_bh(&xfrm_state_dev_gc_lock);
  622. if (!hlist_unhashed(&x->dev_gclist))
  623. hlist_del(&x->dev_gclist);
  624. spin_unlock_bh(&xfrm_state_dev_gc_lock);
  625. if (dev->xfrmdev_ops->xdo_dev_state_free)
  626. dev->xfrmdev_ops->xdo_dev_state_free(x);
  627. WRITE_ONCE(xso->dev, NULL);
  628. xso->type = XFRM_DEV_OFFLOAD_UNSPECIFIED;
  629. netdev_put(dev, &xso->dev_tracker);
  630. }
  631. }
  632. #endif
  633. void __xfrm_state_destroy(struct xfrm_state *x, bool sync)
  634. {
  635. WARN_ON(x->km.state != XFRM_STATE_DEAD);
  636. if (sync) {
  637. synchronize_rcu();
  638. ___xfrm_state_destroy(x);
  639. } else {
  640. spin_lock_bh(&xfrm_state_gc_lock);
  641. hlist_add_head(&x->gclist, &xfrm_state_gc_list);
  642. spin_unlock_bh(&xfrm_state_gc_lock);
  643. schedule_work(&xfrm_state_gc_work);
  644. }
  645. }
  646. EXPORT_SYMBOL(__xfrm_state_destroy);
  647. int __xfrm_state_delete(struct xfrm_state *x)
  648. {
  649. struct net *net = xs_net(x);
  650. int err = -ESRCH;
  651. if (x->km.state != XFRM_STATE_DEAD) {
  652. x->km.state = XFRM_STATE_DEAD;
  653. spin_lock(&net->xfrm.xfrm_state_lock);
  654. list_del(&x->km.all);
  655. hlist_del_rcu(&x->bydst);
  656. hlist_del_rcu(&x->bysrc);
  657. if (x->km.seq)
  658. hlist_del_rcu(&x->byseq);
  659. if (!hlist_unhashed(&x->state_cache))
  660. hlist_del_rcu(&x->state_cache);
  661. if (!hlist_unhashed(&x->state_cache_input))
  662. hlist_del_rcu(&x->state_cache_input);
  663. if (x->id.spi)
  664. hlist_del_rcu(&x->byspi);
  665. net->xfrm.state_num--;
  666. xfrm_nat_keepalive_state_updated(x);
  667. spin_unlock(&net->xfrm.xfrm_state_lock);
  668. if (x->encap_sk)
  669. sock_put(rcu_dereference_raw(x->encap_sk));
  670. xfrm_dev_state_delete(x);
  671. /* All xfrm_state objects are created by xfrm_state_alloc.
  672. * The xfrm_state_alloc call gives a reference, and that
  673. * is what we are dropping here.
  674. */
  675. xfrm_state_put(x);
  676. err = 0;
  677. }
  678. return err;
  679. }
  680. EXPORT_SYMBOL(__xfrm_state_delete);
  681. int xfrm_state_delete(struct xfrm_state *x)
  682. {
  683. int err;
  684. spin_lock_bh(&x->lock);
  685. err = __xfrm_state_delete(x);
  686. spin_unlock_bh(&x->lock);
  687. return err;
  688. }
  689. EXPORT_SYMBOL(xfrm_state_delete);
  690. #ifdef CONFIG_SECURITY_NETWORK_XFRM
  691. static inline int
  692. xfrm_state_flush_secctx_check(struct net *net, u8 proto, bool task_valid)
  693. {
  694. int i, err = 0;
  695. for (i = 0; i <= net->xfrm.state_hmask; i++) {
  696. struct xfrm_state *x;
  697. hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) {
  698. if (xfrm_id_proto_match(x->id.proto, proto) &&
  699. (err = security_xfrm_state_delete(x)) != 0) {
  700. xfrm_audit_state_delete(x, 0, task_valid);
  701. return err;
  702. }
  703. }
  704. }
  705. return err;
  706. }
  707. static inline int
  708. xfrm_dev_state_flush_secctx_check(struct net *net, struct net_device *dev, bool task_valid)
  709. {
  710. int i, err = 0;
  711. for (i = 0; i <= net->xfrm.state_hmask; i++) {
  712. struct xfrm_state *x;
  713. struct xfrm_dev_offload *xso;
  714. hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) {
  715. xso = &x->xso;
  716. if (xso->dev == dev &&
  717. (err = security_xfrm_state_delete(x)) != 0) {
  718. xfrm_audit_state_delete(x, 0, task_valid);
  719. return err;
  720. }
  721. }
  722. }
  723. return err;
  724. }
  725. #else
  726. static inline int
  727. xfrm_state_flush_secctx_check(struct net *net, u8 proto, bool task_valid)
  728. {
  729. return 0;
  730. }
  731. static inline int
  732. xfrm_dev_state_flush_secctx_check(struct net *net, struct net_device *dev, bool task_valid)
  733. {
  734. return 0;
  735. }
  736. #endif
  737. int xfrm_state_flush(struct net *net, u8 proto, bool task_valid, bool sync)
  738. {
  739. int i, err = 0, cnt = 0;
  740. spin_lock_bh(&net->xfrm.xfrm_state_lock);
  741. err = xfrm_state_flush_secctx_check(net, proto, task_valid);
  742. if (err)
  743. goto out;
  744. err = -ESRCH;
  745. for (i = 0; i <= net->xfrm.state_hmask; i++) {
  746. struct xfrm_state *x;
  747. restart:
  748. hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) {
  749. if (!xfrm_state_kern(x) &&
  750. xfrm_id_proto_match(x->id.proto, proto)) {
  751. xfrm_state_hold(x);
  752. spin_unlock_bh(&net->xfrm.xfrm_state_lock);
  753. err = xfrm_state_delete(x);
  754. xfrm_audit_state_delete(x, err ? 0 : 1,
  755. task_valid);
  756. if (sync)
  757. xfrm_state_put_sync(x);
  758. else
  759. xfrm_state_put(x);
  760. if (!err)
  761. cnt++;
  762. spin_lock_bh(&net->xfrm.xfrm_state_lock);
  763. goto restart;
  764. }
  765. }
  766. }
  767. out:
  768. spin_unlock_bh(&net->xfrm.xfrm_state_lock);
  769. if (cnt)
  770. err = 0;
  771. return err;
  772. }
  773. EXPORT_SYMBOL(xfrm_state_flush);
  774. int xfrm_dev_state_flush(struct net *net, struct net_device *dev, bool task_valid)
  775. {
  776. struct xfrm_state *x;
  777. struct hlist_node *tmp;
  778. struct xfrm_dev_offload *xso;
  779. int i, err = 0, cnt = 0;
  780. spin_lock_bh(&net->xfrm.xfrm_state_lock);
  781. err = xfrm_dev_state_flush_secctx_check(net, dev, task_valid);
  782. if (err)
  783. goto out;
  784. err = -ESRCH;
  785. for (i = 0; i <= net->xfrm.state_hmask; i++) {
  786. restart:
  787. hlist_for_each_entry(x, net->xfrm.state_bydst+i, bydst) {
  788. xso = &x->xso;
  789. if (!xfrm_state_kern(x) && xso->dev == dev) {
  790. xfrm_state_hold(x);
  791. spin_unlock_bh(&net->xfrm.xfrm_state_lock);
  792. err = xfrm_state_delete(x);
  793. xfrm_dev_state_free(x);
  794. xfrm_audit_state_delete(x, err ? 0 : 1,
  795. task_valid);
  796. xfrm_state_put(x);
  797. if (!err)
  798. cnt++;
  799. spin_lock_bh(&net->xfrm.xfrm_state_lock);
  800. goto restart;
  801. }
  802. }
  803. }
  804. if (cnt)
  805. err = 0;
  806. out:
  807. spin_unlock_bh(&net->xfrm.xfrm_state_lock);
  808. spin_lock_bh(&xfrm_state_dev_gc_lock);
  809. restart_gc:
  810. hlist_for_each_entry_safe(x, tmp, &xfrm_state_dev_gc_list, dev_gclist) {
  811. xso = &x->xso;
  812. if (xso->dev == dev) {
  813. spin_unlock_bh(&xfrm_state_dev_gc_lock);
  814. xfrm_dev_state_free(x);
  815. spin_lock_bh(&xfrm_state_dev_gc_lock);
  816. goto restart_gc;
  817. }
  818. }
  819. spin_unlock_bh(&xfrm_state_dev_gc_lock);
  820. xfrm_flush_gc();
  821. return err;
  822. }
  823. EXPORT_SYMBOL(xfrm_dev_state_flush);
  824. void xfrm_sad_getinfo(struct net *net, struct xfrmk_sadinfo *si)
  825. {
  826. spin_lock_bh(&net->xfrm.xfrm_state_lock);
  827. si->sadcnt = net->xfrm.state_num;
  828. si->sadhcnt = net->xfrm.state_hmask + 1;
  829. si->sadhmcnt = xfrm_state_hashmax;
  830. spin_unlock_bh(&net->xfrm.xfrm_state_lock);
  831. }
  832. EXPORT_SYMBOL(xfrm_sad_getinfo);
  833. static void
  834. __xfrm4_init_tempsel(struct xfrm_selector *sel, const struct flowi *fl)
  835. {
  836. const struct flowi4 *fl4 = &fl->u.ip4;
  837. sel->daddr.a4 = fl4->daddr;
  838. sel->saddr.a4 = fl4->saddr;
  839. sel->dport = xfrm_flowi_dport(fl, &fl4->uli);
  840. sel->dport_mask = htons(0xffff);
  841. sel->sport = xfrm_flowi_sport(fl, &fl4->uli);
  842. sel->sport_mask = htons(0xffff);
  843. sel->family = AF_INET;
  844. sel->prefixlen_d = 32;
  845. sel->prefixlen_s = 32;
  846. sel->proto = fl4->flowi4_proto;
  847. sel->ifindex = fl4->flowi4_oif;
  848. }
  849. static void
  850. __xfrm6_init_tempsel(struct xfrm_selector *sel, const struct flowi *fl)
  851. {
  852. const struct flowi6 *fl6 = &fl->u.ip6;
  853. /* Initialize temporary selector matching only to current session. */
  854. *(struct in6_addr *)&sel->daddr = fl6->daddr;
  855. *(struct in6_addr *)&sel->saddr = fl6->saddr;
  856. sel->dport = xfrm_flowi_dport(fl, &fl6->uli);
  857. sel->dport_mask = htons(0xffff);
  858. sel->sport = xfrm_flowi_sport(fl, &fl6->uli);
  859. sel->sport_mask = htons(0xffff);
  860. sel->family = AF_INET6;
  861. sel->prefixlen_d = 128;
  862. sel->prefixlen_s = 128;
  863. sel->proto = fl6->flowi6_proto;
  864. sel->ifindex = fl6->flowi6_oif;
  865. }
  866. static void
  867. xfrm_init_tempstate(struct xfrm_state *x, const struct flowi *fl,
  868. const struct xfrm_tmpl *tmpl,
  869. const xfrm_address_t *daddr, const xfrm_address_t *saddr,
  870. unsigned short family)
  871. {
  872. switch (family) {
  873. case AF_INET:
  874. __xfrm4_init_tempsel(&x->sel, fl);
  875. break;
  876. case AF_INET6:
  877. __xfrm6_init_tempsel(&x->sel, fl);
  878. break;
  879. }
  880. x->id = tmpl->id;
  881. switch (tmpl->encap_family) {
  882. case AF_INET:
  883. if (x->id.daddr.a4 == 0)
  884. x->id.daddr.a4 = daddr->a4;
  885. x->props.saddr = tmpl->saddr;
  886. if (x->props.saddr.a4 == 0)
  887. x->props.saddr.a4 = saddr->a4;
  888. break;
  889. case AF_INET6:
  890. if (ipv6_addr_any((struct in6_addr *)&x->id.daddr))
  891. memcpy(&x->id.daddr, daddr, sizeof(x->sel.daddr));
  892. memcpy(&x->props.saddr, &tmpl->saddr, sizeof(x->props.saddr));
  893. if (ipv6_addr_any((struct in6_addr *)&x->props.saddr))
  894. memcpy(&x->props.saddr, saddr, sizeof(x->props.saddr));
  895. break;
  896. }
  897. x->props.mode = tmpl->mode;
  898. x->props.reqid = tmpl->reqid;
  899. x->props.family = tmpl->encap_family;
  900. }
  901. struct xfrm_hash_state_ptrs {
  902. const struct hlist_head *bydst;
  903. const struct hlist_head *bysrc;
  904. const struct hlist_head *byspi;
  905. unsigned int hmask;
  906. };
  907. static void xfrm_hash_ptrs_get(const struct net *net, struct xfrm_hash_state_ptrs *ptrs)
  908. {
  909. unsigned int sequence;
  910. do {
  911. sequence = read_seqcount_begin(&net->xfrm.xfrm_state_hash_generation);
  912. ptrs->bydst = xfrm_state_deref_check(net->xfrm.state_bydst, net);
  913. ptrs->bysrc = xfrm_state_deref_check(net->xfrm.state_bysrc, net);
  914. ptrs->byspi = xfrm_state_deref_check(net->xfrm.state_byspi, net);
  915. ptrs->hmask = net->xfrm.state_hmask;
  916. } while (read_seqcount_retry(&net->xfrm.xfrm_state_hash_generation, sequence));
  917. }
  918. static struct xfrm_state *__xfrm_state_lookup_all(const struct xfrm_hash_state_ptrs *state_ptrs,
  919. u32 mark,
  920. const xfrm_address_t *daddr,
  921. __be32 spi, u8 proto,
  922. unsigned short family,
  923. struct xfrm_dev_offload *xdo)
  924. {
  925. unsigned int h = __xfrm_spi_hash(daddr, spi, proto, family, state_ptrs->hmask);
  926. struct xfrm_state *x;
  927. hlist_for_each_entry_rcu(x, state_ptrs->byspi + h, byspi) {
  928. #ifdef CONFIG_XFRM_OFFLOAD
  929. if (xdo->type == XFRM_DEV_OFFLOAD_PACKET) {
  930. if (x->xso.type != XFRM_DEV_OFFLOAD_PACKET)
  931. /* HW states are in the head of list, there is
  932. * no need to iterate further.
  933. */
  934. break;
  935. /* Packet offload: both policy and SA should
  936. * have same device.
  937. */
  938. if (xdo->dev != x->xso.dev)
  939. continue;
  940. } else if (x->xso.type == XFRM_DEV_OFFLOAD_PACKET)
  941. /* Skip HW policy for SW lookups */
  942. continue;
  943. #endif
  944. if (x->props.family != family ||
  945. x->id.spi != spi ||
  946. x->id.proto != proto ||
  947. !xfrm_addr_equal(&x->id.daddr, daddr, family))
  948. continue;
  949. if ((mark & x->mark.m) != x->mark.v)
  950. continue;
  951. if (!xfrm_state_hold_rcu(x))
  952. continue;
  953. return x;
  954. }
  955. return NULL;
  956. }
  957. static struct xfrm_state *__xfrm_state_lookup(const struct xfrm_hash_state_ptrs *state_ptrs,
  958. u32 mark,
  959. const xfrm_address_t *daddr,
  960. __be32 spi, u8 proto,
  961. unsigned short family)
  962. {
  963. unsigned int h = __xfrm_spi_hash(daddr, spi, proto, family, state_ptrs->hmask);
  964. struct xfrm_state *x;
  965. hlist_for_each_entry_rcu(x, state_ptrs->byspi + h, byspi) {
  966. if (x->props.family != family ||
  967. x->id.spi != spi ||
  968. x->id.proto != proto ||
  969. !xfrm_addr_equal(&x->id.daddr, daddr, family))
  970. continue;
  971. if ((mark & x->mark.m) != x->mark.v)
  972. continue;
  973. if (!xfrm_state_hold_rcu(x))
  974. continue;
  975. return x;
  976. }
  977. return NULL;
  978. }
  979. struct xfrm_state *xfrm_input_state_lookup(struct net *net, u32 mark,
  980. const xfrm_address_t *daddr,
  981. __be32 spi, u8 proto,
  982. unsigned short family)
  983. {
  984. struct xfrm_hash_state_ptrs state_ptrs;
  985. struct hlist_head *state_cache_input;
  986. struct xfrm_state *x = NULL;
  987. state_cache_input = raw_cpu_ptr(net->xfrm.state_cache_input);
  988. rcu_read_lock();
  989. hlist_for_each_entry_rcu(x, state_cache_input, state_cache_input) {
  990. if (x->props.family != family ||
  991. x->id.spi != spi ||
  992. x->id.proto != proto ||
  993. !xfrm_addr_equal(&x->id.daddr, daddr, family))
  994. continue;
  995. if ((mark & x->mark.m) != x->mark.v)
  996. continue;
  997. if (!xfrm_state_hold_rcu(x))
  998. continue;
  999. goto out;
  1000. }
  1001. xfrm_hash_ptrs_get(net, &state_ptrs);
  1002. x = __xfrm_state_lookup(&state_ptrs, mark, daddr, spi, proto, family);
  1003. if (x && x->km.state == XFRM_STATE_VALID) {
  1004. spin_lock_bh(&net->xfrm.xfrm_state_lock);
  1005. if (hlist_unhashed(&x->state_cache_input)) {
  1006. hlist_add_head_rcu(&x->state_cache_input, state_cache_input);
  1007. } else {
  1008. hlist_del_rcu(&x->state_cache_input);
  1009. hlist_add_head_rcu(&x->state_cache_input, state_cache_input);
  1010. }
  1011. spin_unlock_bh(&net->xfrm.xfrm_state_lock);
  1012. }
  1013. out:
  1014. rcu_read_unlock();
  1015. return x;
  1016. }
  1017. EXPORT_SYMBOL(xfrm_input_state_lookup);
  1018. static struct xfrm_state *__xfrm_state_lookup_byaddr(const struct xfrm_hash_state_ptrs *state_ptrs,
  1019. u32 mark,
  1020. const xfrm_address_t *daddr,
  1021. const xfrm_address_t *saddr,
  1022. u8 proto, unsigned short family)
  1023. {
  1024. unsigned int h = __xfrm_src_hash(daddr, saddr, family, state_ptrs->hmask);
  1025. struct xfrm_state *x;
  1026. hlist_for_each_entry_rcu(x, state_ptrs->bysrc + h, bysrc) {
  1027. if (x->props.family != family ||
  1028. x->id.proto != proto ||
  1029. !xfrm_addr_equal(&x->id.daddr, daddr, family) ||
  1030. !xfrm_addr_equal(&x->props.saddr, saddr, family))
  1031. continue;
  1032. if ((mark & x->mark.m) != x->mark.v)
  1033. continue;
  1034. if (!xfrm_state_hold_rcu(x))
  1035. continue;
  1036. return x;
  1037. }
  1038. return NULL;
  1039. }
  1040. static inline struct xfrm_state *
  1041. __xfrm_state_locate(struct xfrm_state *x, int use_spi, int family)
  1042. {
  1043. struct xfrm_hash_state_ptrs state_ptrs;
  1044. struct net *net = xs_net(x);
  1045. u32 mark = x->mark.v & x->mark.m;
  1046. xfrm_hash_ptrs_get(net, &state_ptrs);
  1047. if (use_spi)
  1048. return __xfrm_state_lookup(&state_ptrs, mark, &x->id.daddr,
  1049. x->id.spi, x->id.proto, family);
  1050. else
  1051. return __xfrm_state_lookup_byaddr(&state_ptrs, mark,
  1052. &x->id.daddr,
  1053. &x->props.saddr,
  1054. x->id.proto, family);
  1055. }
  1056. static void xfrm_hash_grow_check(struct net *net, int have_hash_collision)
  1057. {
  1058. if (have_hash_collision &&
  1059. (net->xfrm.state_hmask + 1) < xfrm_state_hashmax &&
  1060. net->xfrm.state_num > net->xfrm.state_hmask)
  1061. schedule_work(&net->xfrm.state_hash_work);
  1062. }
  1063. static void xfrm_state_look_at(struct xfrm_policy *pol, struct xfrm_state *x,
  1064. const struct flowi *fl, unsigned short family,
  1065. struct xfrm_state **best, int *acq_in_progress,
  1066. int *error)
  1067. {
  1068. /* We need the cpu id just as a lookup key,
  1069. * we don't require it to be stable.
  1070. */
  1071. unsigned int pcpu_id = get_cpu();
  1072. put_cpu();
  1073. /* Resolution logic:
  1074. * 1. There is a valid state with matching selector. Done.
  1075. * 2. Valid state with inappropriate selector. Skip.
  1076. *
  1077. * Entering area of "sysdeps".
  1078. *
  1079. * 3. If state is not valid, selector is temporary, it selects
  1080. * only session which triggered previous resolution. Key
  1081. * manager will do something to install a state with proper
  1082. * selector.
  1083. */
  1084. if (x->km.state == XFRM_STATE_VALID) {
  1085. if ((x->sel.family &&
  1086. (x->sel.family != family ||
  1087. !xfrm_selector_match(&x->sel, fl, family))) ||
  1088. !security_xfrm_state_pol_flow_match(x, pol,
  1089. &fl->u.__fl_common))
  1090. return;
  1091. if (x->pcpu_num != UINT_MAX && x->pcpu_num != pcpu_id)
  1092. return;
  1093. if (!*best ||
  1094. ((*best)->pcpu_num == UINT_MAX && x->pcpu_num == pcpu_id) ||
  1095. (*best)->km.dying > x->km.dying ||
  1096. ((*best)->km.dying == x->km.dying &&
  1097. (*best)->curlft.add_time < x->curlft.add_time))
  1098. *best = x;
  1099. } else if (x->km.state == XFRM_STATE_ACQ) {
  1100. if (!*best || x->pcpu_num == pcpu_id)
  1101. *acq_in_progress = 1;
  1102. } else if (x->km.state == XFRM_STATE_ERROR ||
  1103. x->km.state == XFRM_STATE_EXPIRED) {
  1104. if ((!x->sel.family ||
  1105. (x->sel.family == family &&
  1106. xfrm_selector_match(&x->sel, fl, family))) &&
  1107. security_xfrm_state_pol_flow_match(x, pol,
  1108. &fl->u.__fl_common))
  1109. *error = -ESRCH;
  1110. }
  1111. }
  1112. struct xfrm_state *
  1113. xfrm_state_find(const xfrm_address_t *daddr, const xfrm_address_t *saddr,
  1114. const struct flowi *fl, struct xfrm_tmpl *tmpl,
  1115. struct xfrm_policy *pol, int *err,
  1116. unsigned short family, u32 if_id)
  1117. {
  1118. static xfrm_address_t saddr_wildcard = { };
  1119. struct xfrm_hash_state_ptrs state_ptrs;
  1120. struct net *net = xp_net(pol);
  1121. unsigned int h, h_wildcard;
  1122. struct xfrm_state *x, *x0, *to_put;
  1123. int acquire_in_progress = 0;
  1124. int error = 0;
  1125. struct xfrm_state *best = NULL;
  1126. u32 mark = pol->mark.v & pol->mark.m;
  1127. unsigned short encap_family = tmpl->encap_family;
  1128. unsigned int sequence;
  1129. struct km_event c;
  1130. unsigned int pcpu_id;
  1131. bool cached = false;
  1132. /* We need the cpu id just as a lookup key,
  1133. * we don't require it to be stable.
  1134. */
  1135. pcpu_id = get_cpu();
  1136. put_cpu();
  1137. to_put = NULL;
  1138. sequence = read_seqcount_begin(&net->xfrm.xfrm_state_hash_generation);
  1139. rcu_read_lock();
  1140. hlist_for_each_entry_rcu(x, &pol->state_cache_list, state_cache) {
  1141. if (x->props.family == encap_family &&
  1142. x->props.reqid == tmpl->reqid &&
  1143. (mark & x->mark.m) == x->mark.v &&
  1144. x->if_id == if_id &&
  1145. !(x->props.flags & XFRM_STATE_WILDRECV) &&
  1146. xfrm_state_addr_check(x, daddr, saddr, encap_family) &&
  1147. tmpl->mode == x->props.mode &&
  1148. tmpl->id.proto == x->id.proto &&
  1149. (tmpl->id.spi == x->id.spi || !tmpl->id.spi))
  1150. xfrm_state_look_at(pol, x, fl, encap_family,
  1151. &best, &acquire_in_progress, &error);
  1152. }
  1153. if (best)
  1154. goto cached;
  1155. hlist_for_each_entry_rcu(x, &pol->state_cache_list, state_cache) {
  1156. if (x->props.family == encap_family &&
  1157. x->props.reqid == tmpl->reqid &&
  1158. (mark & x->mark.m) == x->mark.v &&
  1159. x->if_id == if_id &&
  1160. !(x->props.flags & XFRM_STATE_WILDRECV) &&
  1161. xfrm_addr_equal(&x->id.daddr, daddr, encap_family) &&
  1162. tmpl->mode == x->props.mode &&
  1163. tmpl->id.proto == x->id.proto &&
  1164. (tmpl->id.spi == x->id.spi || !tmpl->id.spi))
  1165. xfrm_state_look_at(pol, x, fl, family,
  1166. &best, &acquire_in_progress, &error);
  1167. }
  1168. cached:
  1169. cached = true;
  1170. if (best)
  1171. goto found;
  1172. else if (error)
  1173. best = NULL;
  1174. else if (acquire_in_progress) /* XXX: acquire_in_progress should not happen */
  1175. WARN_ON(1);
  1176. xfrm_hash_ptrs_get(net, &state_ptrs);
  1177. h = __xfrm_dst_hash(daddr, saddr, tmpl->reqid, encap_family, state_ptrs.hmask);
  1178. hlist_for_each_entry_rcu(x, state_ptrs.bydst + h, bydst) {
  1179. #ifdef CONFIG_XFRM_OFFLOAD
  1180. if (pol->xdo.type == XFRM_DEV_OFFLOAD_PACKET) {
  1181. if (x->xso.type != XFRM_DEV_OFFLOAD_PACKET)
  1182. /* HW states are in the head of list, there is
  1183. * no need to iterate further.
  1184. */
  1185. break;
  1186. /* Packet offload: both policy and SA should
  1187. * have same device.
  1188. */
  1189. if (pol->xdo.dev != x->xso.dev)
  1190. continue;
  1191. } else if (x->xso.type == XFRM_DEV_OFFLOAD_PACKET)
  1192. /* Skip HW policy for SW lookups */
  1193. continue;
  1194. #endif
  1195. if (x->props.family == encap_family &&
  1196. x->props.reqid == tmpl->reqid &&
  1197. (mark & x->mark.m) == x->mark.v &&
  1198. x->if_id == if_id &&
  1199. !(x->props.flags & XFRM_STATE_WILDRECV) &&
  1200. xfrm_state_addr_check(x, daddr, saddr, encap_family) &&
  1201. tmpl->mode == x->props.mode &&
  1202. tmpl->id.proto == x->id.proto &&
  1203. (tmpl->id.spi == x->id.spi || !tmpl->id.spi))
  1204. xfrm_state_look_at(pol, x, fl, family,
  1205. &best, &acquire_in_progress, &error);
  1206. }
  1207. if (best || acquire_in_progress)
  1208. goto found;
  1209. h_wildcard = __xfrm_dst_hash(daddr, &saddr_wildcard, tmpl->reqid,
  1210. encap_family, state_ptrs.hmask);
  1211. hlist_for_each_entry_rcu(x, state_ptrs.bydst + h_wildcard, bydst) {
  1212. #ifdef CONFIG_XFRM_OFFLOAD
  1213. if (pol->xdo.type == XFRM_DEV_OFFLOAD_PACKET) {
  1214. if (x->xso.type != XFRM_DEV_OFFLOAD_PACKET)
  1215. /* HW states are in the head of list, there is
  1216. * no need to iterate further.
  1217. */
  1218. break;
  1219. /* Packet offload: both policy and SA should
  1220. * have same device.
  1221. */
  1222. if (pol->xdo.dev != x->xso.dev)
  1223. continue;
  1224. } else if (x->xso.type == XFRM_DEV_OFFLOAD_PACKET)
  1225. /* Skip HW policy for SW lookups */
  1226. continue;
  1227. #endif
  1228. if (x->props.family == encap_family &&
  1229. x->props.reqid == tmpl->reqid &&
  1230. (mark & x->mark.m) == x->mark.v &&
  1231. x->if_id == if_id &&
  1232. !(x->props.flags & XFRM_STATE_WILDRECV) &&
  1233. xfrm_addr_equal(&x->id.daddr, daddr, encap_family) &&
  1234. tmpl->mode == x->props.mode &&
  1235. tmpl->id.proto == x->id.proto &&
  1236. (tmpl->id.spi == x->id.spi || !tmpl->id.spi))
  1237. xfrm_state_look_at(pol, x, fl, family,
  1238. &best, &acquire_in_progress, &error);
  1239. }
  1240. found:
  1241. if (!(pol->flags & XFRM_POLICY_CPU_ACQUIRE) ||
  1242. (best && (best->pcpu_num == pcpu_id)))
  1243. x = best;
  1244. if (!x && !error && !acquire_in_progress) {
  1245. if (tmpl->id.spi &&
  1246. (x0 = __xfrm_state_lookup_all(&state_ptrs, mark, daddr,
  1247. tmpl->id.spi, tmpl->id.proto,
  1248. encap_family,
  1249. &pol->xdo)) != NULL) {
  1250. to_put = x0;
  1251. error = -EEXIST;
  1252. goto out;
  1253. }
  1254. c.net = net;
  1255. /* If the KMs have no listeners (yet...), avoid allocating an SA
  1256. * for each and every packet - garbage collection might not
  1257. * handle the flood.
  1258. */
  1259. if (!km_is_alive(&c)) {
  1260. error = -ESRCH;
  1261. goto out;
  1262. }
  1263. x = xfrm_state_alloc(net);
  1264. if (x == NULL) {
  1265. error = -ENOMEM;
  1266. goto out;
  1267. }
  1268. /* Initialize temporary state matching only
  1269. * to current session. */
  1270. xfrm_init_tempstate(x, fl, tmpl, daddr, saddr, family);
  1271. memcpy(&x->mark, &pol->mark, sizeof(x->mark));
  1272. x->if_id = if_id;
  1273. if ((pol->flags & XFRM_POLICY_CPU_ACQUIRE) && best)
  1274. x->pcpu_num = pcpu_id;
  1275. error = security_xfrm_state_alloc_acquire(x, pol->security, fl->flowi_secid);
  1276. if (error) {
  1277. x->km.state = XFRM_STATE_DEAD;
  1278. to_put = x;
  1279. x = NULL;
  1280. goto out;
  1281. }
  1282. #ifdef CONFIG_XFRM_OFFLOAD
  1283. if (pol->xdo.type == XFRM_DEV_OFFLOAD_PACKET) {
  1284. struct xfrm_dev_offload *xdo = &pol->xdo;
  1285. struct xfrm_dev_offload *xso = &x->xso;
  1286. xso->type = XFRM_DEV_OFFLOAD_PACKET;
  1287. xso->dir = xdo->dir;
  1288. xso->dev = xdo->dev;
  1289. xso->real_dev = xdo->real_dev;
  1290. xso->flags = XFRM_DEV_OFFLOAD_FLAG_ACQ;
  1291. netdev_hold(xso->dev, &xso->dev_tracker, GFP_ATOMIC);
  1292. error = xso->dev->xfrmdev_ops->xdo_dev_state_add(x, NULL);
  1293. if (error) {
  1294. xso->dir = 0;
  1295. netdev_put(xso->dev, &xso->dev_tracker);
  1296. xso->dev = NULL;
  1297. xso->real_dev = NULL;
  1298. xso->type = XFRM_DEV_OFFLOAD_UNSPECIFIED;
  1299. x->km.state = XFRM_STATE_DEAD;
  1300. to_put = x;
  1301. x = NULL;
  1302. goto out;
  1303. }
  1304. }
  1305. #endif
  1306. if (km_query(x, tmpl, pol) == 0) {
  1307. spin_lock_bh(&net->xfrm.xfrm_state_lock);
  1308. x->km.state = XFRM_STATE_ACQ;
  1309. x->dir = XFRM_SA_DIR_OUT;
  1310. list_add(&x->km.all, &net->xfrm.state_all);
  1311. h = xfrm_dst_hash(net, daddr, saddr, tmpl->reqid, encap_family);
  1312. XFRM_STATE_INSERT(bydst, &x->bydst,
  1313. net->xfrm.state_bydst + h,
  1314. x->xso.type);
  1315. h = xfrm_src_hash(net, daddr, saddr, encap_family);
  1316. XFRM_STATE_INSERT(bysrc, &x->bysrc,
  1317. net->xfrm.state_bysrc + h,
  1318. x->xso.type);
  1319. INIT_HLIST_NODE(&x->state_cache);
  1320. if (x->id.spi) {
  1321. h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto, encap_family);
  1322. XFRM_STATE_INSERT(byspi, &x->byspi,
  1323. net->xfrm.state_byspi + h,
  1324. x->xso.type);
  1325. }
  1326. if (x->km.seq) {
  1327. h = xfrm_seq_hash(net, x->km.seq);
  1328. XFRM_STATE_INSERT(byseq, &x->byseq,
  1329. net->xfrm.state_byseq + h,
  1330. x->xso.type);
  1331. }
  1332. x->lft.hard_add_expires_seconds = net->xfrm.sysctl_acq_expires;
  1333. hrtimer_start(&x->mtimer,
  1334. ktime_set(net->xfrm.sysctl_acq_expires, 0),
  1335. HRTIMER_MODE_REL_SOFT);
  1336. net->xfrm.state_num++;
  1337. xfrm_hash_grow_check(net, x->bydst.next != NULL);
  1338. spin_unlock_bh(&net->xfrm.xfrm_state_lock);
  1339. } else {
  1340. #ifdef CONFIG_XFRM_OFFLOAD
  1341. struct xfrm_dev_offload *xso = &x->xso;
  1342. if (xso->type == XFRM_DEV_OFFLOAD_PACKET) {
  1343. xfrm_dev_state_delete(x);
  1344. xfrm_dev_state_free(x);
  1345. }
  1346. #endif
  1347. x->km.state = XFRM_STATE_DEAD;
  1348. to_put = x;
  1349. x = NULL;
  1350. error = -ESRCH;
  1351. }
  1352. /* Use the already installed 'fallback' while the CPU-specific
  1353. * SA acquire is handled*/
  1354. if (best)
  1355. x = best;
  1356. }
  1357. out:
  1358. if (x) {
  1359. if (!xfrm_state_hold_rcu(x)) {
  1360. *err = -EAGAIN;
  1361. x = NULL;
  1362. }
  1363. } else {
  1364. *err = acquire_in_progress ? -EAGAIN : error;
  1365. }
  1366. if (x && x->km.state == XFRM_STATE_VALID && !cached &&
  1367. (!(pol->flags & XFRM_POLICY_CPU_ACQUIRE) || x->pcpu_num == pcpu_id)) {
  1368. spin_lock_bh(&net->xfrm.xfrm_state_lock);
  1369. if (hlist_unhashed(&x->state_cache))
  1370. hlist_add_head_rcu(&x->state_cache, &pol->state_cache_list);
  1371. spin_unlock_bh(&net->xfrm.xfrm_state_lock);
  1372. }
  1373. rcu_read_unlock();
  1374. if (to_put)
  1375. xfrm_state_put(to_put);
  1376. if (read_seqcount_retry(&net->xfrm.xfrm_state_hash_generation, sequence)) {
  1377. *err = -EAGAIN;
  1378. if (x) {
  1379. xfrm_state_put(x);
  1380. x = NULL;
  1381. }
  1382. }
  1383. return x;
  1384. }
  1385. struct xfrm_state *
  1386. xfrm_stateonly_find(struct net *net, u32 mark, u32 if_id,
  1387. xfrm_address_t *daddr, xfrm_address_t *saddr,
  1388. unsigned short family, u8 mode, u8 proto, u32 reqid)
  1389. {
  1390. unsigned int h;
  1391. struct xfrm_state *rx = NULL, *x = NULL;
  1392. spin_lock_bh(&net->xfrm.xfrm_state_lock);
  1393. h = xfrm_dst_hash(net, daddr, saddr, reqid, family);
  1394. hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
  1395. if (x->props.family == family &&
  1396. x->props.reqid == reqid &&
  1397. (mark & x->mark.m) == x->mark.v &&
  1398. x->if_id == if_id &&
  1399. !(x->props.flags & XFRM_STATE_WILDRECV) &&
  1400. xfrm_state_addr_check(x, daddr, saddr, family) &&
  1401. mode == x->props.mode &&
  1402. proto == x->id.proto &&
  1403. x->km.state == XFRM_STATE_VALID) {
  1404. rx = x;
  1405. break;
  1406. }
  1407. }
  1408. if (rx)
  1409. xfrm_state_hold(rx);
  1410. spin_unlock_bh(&net->xfrm.xfrm_state_lock);
  1411. return rx;
  1412. }
  1413. EXPORT_SYMBOL(xfrm_stateonly_find);
  1414. struct xfrm_state *xfrm_state_lookup_byspi(struct net *net, __be32 spi,
  1415. unsigned short family)
  1416. {
  1417. struct xfrm_state *x;
  1418. struct xfrm_state_walk *w;
  1419. spin_lock_bh(&net->xfrm.xfrm_state_lock);
  1420. list_for_each_entry(w, &net->xfrm.state_all, all) {
  1421. x = container_of(w, struct xfrm_state, km);
  1422. if (x->props.family != family ||
  1423. x->id.spi != spi)
  1424. continue;
  1425. xfrm_state_hold(x);
  1426. spin_unlock_bh(&net->xfrm.xfrm_state_lock);
  1427. return x;
  1428. }
  1429. spin_unlock_bh(&net->xfrm.xfrm_state_lock);
  1430. return NULL;
  1431. }
  1432. EXPORT_SYMBOL(xfrm_state_lookup_byspi);
  1433. static void __xfrm_state_insert(struct xfrm_state *x)
  1434. {
  1435. struct net *net = xs_net(x);
  1436. unsigned int h;
  1437. list_add(&x->km.all, &net->xfrm.state_all);
  1438. h = xfrm_dst_hash(net, &x->id.daddr, &x->props.saddr,
  1439. x->props.reqid, x->props.family);
  1440. XFRM_STATE_INSERT(bydst, &x->bydst, net->xfrm.state_bydst + h,
  1441. x->xso.type);
  1442. h = xfrm_src_hash(net, &x->id.daddr, &x->props.saddr, x->props.family);
  1443. XFRM_STATE_INSERT(bysrc, &x->bysrc, net->xfrm.state_bysrc + h,
  1444. x->xso.type);
  1445. if (x->id.spi) {
  1446. h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto,
  1447. x->props.family);
  1448. XFRM_STATE_INSERT(byspi, &x->byspi, net->xfrm.state_byspi + h,
  1449. x->xso.type);
  1450. }
  1451. if (x->km.seq) {
  1452. h = xfrm_seq_hash(net, x->km.seq);
  1453. XFRM_STATE_INSERT(byseq, &x->byseq, net->xfrm.state_byseq + h,
  1454. x->xso.type);
  1455. }
  1456. hrtimer_start(&x->mtimer, ktime_set(1, 0), HRTIMER_MODE_REL_SOFT);
  1457. if (x->replay_maxage)
  1458. mod_timer(&x->rtimer, jiffies + x->replay_maxage);
  1459. net->xfrm.state_num++;
  1460. xfrm_hash_grow_check(net, x->bydst.next != NULL);
  1461. xfrm_nat_keepalive_state_updated(x);
  1462. }
  1463. /* net->xfrm.xfrm_state_lock is held */
  1464. static void __xfrm_state_bump_genids(struct xfrm_state *xnew)
  1465. {
  1466. struct net *net = xs_net(xnew);
  1467. unsigned short family = xnew->props.family;
  1468. u32 reqid = xnew->props.reqid;
  1469. struct xfrm_state *x;
  1470. unsigned int h;
  1471. u32 mark = xnew->mark.v & xnew->mark.m;
  1472. u32 if_id = xnew->if_id;
  1473. u32 cpu_id = xnew->pcpu_num;
  1474. h = xfrm_dst_hash(net, &xnew->id.daddr, &xnew->props.saddr, reqid, family);
  1475. hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
  1476. if (x->props.family == family &&
  1477. x->props.reqid == reqid &&
  1478. x->if_id == if_id &&
  1479. x->pcpu_num == cpu_id &&
  1480. (mark & x->mark.m) == x->mark.v &&
  1481. xfrm_addr_equal(&x->id.daddr, &xnew->id.daddr, family) &&
  1482. xfrm_addr_equal(&x->props.saddr, &xnew->props.saddr, family))
  1483. x->genid++;
  1484. }
  1485. }
  1486. void xfrm_state_insert(struct xfrm_state *x)
  1487. {
  1488. struct net *net = xs_net(x);
  1489. spin_lock_bh(&net->xfrm.xfrm_state_lock);
  1490. __xfrm_state_bump_genids(x);
  1491. __xfrm_state_insert(x);
  1492. spin_unlock_bh(&net->xfrm.xfrm_state_lock);
  1493. }
  1494. EXPORT_SYMBOL(xfrm_state_insert);
  1495. /* net->xfrm.xfrm_state_lock is held */
  1496. static struct xfrm_state *__find_acq_core(struct net *net,
  1497. const struct xfrm_mark *m,
  1498. unsigned short family, u8 mode,
  1499. u32 reqid, u32 if_id, u32 pcpu_num, u8 proto,
  1500. const xfrm_address_t *daddr,
  1501. const xfrm_address_t *saddr,
  1502. int create)
  1503. {
  1504. unsigned int h = xfrm_dst_hash(net, daddr, saddr, reqid, family);
  1505. struct xfrm_state *x;
  1506. u32 mark = m->v & m->m;
  1507. hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
  1508. if (x->props.reqid != reqid ||
  1509. x->props.mode != mode ||
  1510. x->props.family != family ||
  1511. x->km.state != XFRM_STATE_ACQ ||
  1512. x->id.spi != 0 ||
  1513. x->id.proto != proto ||
  1514. (mark & x->mark.m) != x->mark.v ||
  1515. x->pcpu_num != pcpu_num ||
  1516. !xfrm_addr_equal(&x->id.daddr, daddr, family) ||
  1517. !xfrm_addr_equal(&x->props.saddr, saddr, family))
  1518. continue;
  1519. xfrm_state_hold(x);
  1520. return x;
  1521. }
  1522. if (!create)
  1523. return NULL;
  1524. x = xfrm_state_alloc(net);
  1525. if (likely(x)) {
  1526. switch (family) {
  1527. case AF_INET:
  1528. x->sel.daddr.a4 = daddr->a4;
  1529. x->sel.saddr.a4 = saddr->a4;
  1530. x->sel.prefixlen_d = 32;
  1531. x->sel.prefixlen_s = 32;
  1532. x->props.saddr.a4 = saddr->a4;
  1533. x->id.daddr.a4 = daddr->a4;
  1534. break;
  1535. case AF_INET6:
  1536. x->sel.daddr.in6 = daddr->in6;
  1537. x->sel.saddr.in6 = saddr->in6;
  1538. x->sel.prefixlen_d = 128;
  1539. x->sel.prefixlen_s = 128;
  1540. x->props.saddr.in6 = saddr->in6;
  1541. x->id.daddr.in6 = daddr->in6;
  1542. break;
  1543. }
  1544. x->pcpu_num = pcpu_num;
  1545. x->km.state = XFRM_STATE_ACQ;
  1546. x->id.proto = proto;
  1547. x->props.family = family;
  1548. x->props.mode = mode;
  1549. x->props.reqid = reqid;
  1550. x->if_id = if_id;
  1551. x->mark.v = m->v;
  1552. x->mark.m = m->m;
  1553. x->lft.hard_add_expires_seconds = net->xfrm.sysctl_acq_expires;
  1554. xfrm_state_hold(x);
  1555. hrtimer_start(&x->mtimer,
  1556. ktime_set(net->xfrm.sysctl_acq_expires, 0),
  1557. HRTIMER_MODE_REL_SOFT);
  1558. list_add(&x->km.all, &net->xfrm.state_all);
  1559. XFRM_STATE_INSERT(bydst, &x->bydst, net->xfrm.state_bydst + h,
  1560. x->xso.type);
  1561. h = xfrm_src_hash(net, daddr, saddr, family);
  1562. XFRM_STATE_INSERT(bysrc, &x->bysrc, net->xfrm.state_bysrc + h,
  1563. x->xso.type);
  1564. net->xfrm.state_num++;
  1565. xfrm_hash_grow_check(net, x->bydst.next != NULL);
  1566. }
  1567. return x;
  1568. }
  1569. static struct xfrm_state *__xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq, u32 pcpu_num);
  1570. int xfrm_state_add(struct xfrm_state *x)
  1571. {
  1572. struct net *net = xs_net(x);
  1573. struct xfrm_state *x1, *to_put;
  1574. int family;
  1575. int err;
  1576. u32 mark = x->mark.v & x->mark.m;
  1577. int use_spi = xfrm_id_proto_match(x->id.proto, IPSEC_PROTO_ANY);
  1578. family = x->props.family;
  1579. to_put = NULL;
  1580. spin_lock_bh(&net->xfrm.xfrm_state_lock);
  1581. x1 = __xfrm_state_locate(x, use_spi, family);
  1582. if (x1) {
  1583. to_put = x1;
  1584. x1 = NULL;
  1585. err = -EEXIST;
  1586. goto out;
  1587. }
  1588. if (use_spi && x->km.seq) {
  1589. x1 = __xfrm_find_acq_byseq(net, mark, x->km.seq, x->pcpu_num);
  1590. if (x1 && ((x1->id.proto != x->id.proto) ||
  1591. !xfrm_addr_equal(&x1->id.daddr, &x->id.daddr, family))) {
  1592. to_put = x1;
  1593. x1 = NULL;
  1594. }
  1595. }
  1596. if (use_spi && !x1)
  1597. x1 = __find_acq_core(net, &x->mark, family, x->props.mode,
  1598. x->props.reqid, x->if_id, x->pcpu_num, x->id.proto,
  1599. &x->id.daddr, &x->props.saddr, 0);
  1600. __xfrm_state_bump_genids(x);
  1601. __xfrm_state_insert(x);
  1602. err = 0;
  1603. out:
  1604. spin_unlock_bh(&net->xfrm.xfrm_state_lock);
  1605. if (x1) {
  1606. xfrm_state_delete(x1);
  1607. xfrm_state_put(x1);
  1608. }
  1609. if (to_put)
  1610. xfrm_state_put(to_put);
  1611. return err;
  1612. }
  1613. EXPORT_SYMBOL(xfrm_state_add);
  1614. #ifdef CONFIG_XFRM_MIGRATE
  1615. static inline int clone_security(struct xfrm_state *x, struct xfrm_sec_ctx *security)
  1616. {
  1617. struct xfrm_user_sec_ctx *uctx;
  1618. int size = sizeof(*uctx) + security->ctx_len;
  1619. int err;
  1620. uctx = kmalloc(size, GFP_KERNEL);
  1621. if (!uctx)
  1622. return -ENOMEM;
  1623. uctx->exttype = XFRMA_SEC_CTX;
  1624. uctx->len = size;
  1625. uctx->ctx_doi = security->ctx_doi;
  1626. uctx->ctx_alg = security->ctx_alg;
  1627. uctx->ctx_len = security->ctx_len;
  1628. memcpy(uctx + 1, security->ctx_str, security->ctx_len);
  1629. err = security_xfrm_state_alloc(x, uctx);
  1630. kfree(uctx);
  1631. if (err)
  1632. return err;
  1633. return 0;
  1634. }
  1635. static struct xfrm_state *xfrm_state_clone(struct xfrm_state *orig,
  1636. struct xfrm_encap_tmpl *encap)
  1637. {
  1638. struct net *net = xs_net(orig);
  1639. struct xfrm_state *x = xfrm_state_alloc(net);
  1640. if (!x)
  1641. goto out;
  1642. memcpy(&x->id, &orig->id, sizeof(x->id));
  1643. memcpy(&x->sel, &orig->sel, sizeof(x->sel));
  1644. memcpy(&x->lft, &orig->lft, sizeof(x->lft));
  1645. x->props.mode = orig->props.mode;
  1646. x->props.replay_window = orig->props.replay_window;
  1647. x->props.reqid = orig->props.reqid;
  1648. x->props.family = orig->props.family;
  1649. x->props.saddr = orig->props.saddr;
  1650. if (orig->aalg) {
  1651. x->aalg = xfrm_algo_auth_clone(orig->aalg);
  1652. if (!x->aalg)
  1653. goto error;
  1654. }
  1655. x->props.aalgo = orig->props.aalgo;
  1656. if (orig->aead) {
  1657. x->aead = xfrm_algo_aead_clone(orig->aead);
  1658. x->geniv = orig->geniv;
  1659. if (!x->aead)
  1660. goto error;
  1661. }
  1662. if (orig->ealg) {
  1663. x->ealg = xfrm_algo_clone(orig->ealg);
  1664. if (!x->ealg)
  1665. goto error;
  1666. }
  1667. x->props.ealgo = orig->props.ealgo;
  1668. if (orig->calg) {
  1669. x->calg = xfrm_algo_clone(orig->calg);
  1670. if (!x->calg)
  1671. goto error;
  1672. }
  1673. x->props.calgo = orig->props.calgo;
  1674. if (encap || orig->encap) {
  1675. if (encap)
  1676. x->encap = kmemdup(encap, sizeof(*x->encap),
  1677. GFP_KERNEL);
  1678. else
  1679. x->encap = kmemdup(orig->encap, sizeof(*x->encap),
  1680. GFP_KERNEL);
  1681. if (!x->encap)
  1682. goto error;
  1683. }
  1684. if (orig->security)
  1685. if (clone_security(x, orig->security))
  1686. goto error;
  1687. if (orig->coaddr) {
  1688. x->coaddr = kmemdup(orig->coaddr, sizeof(*x->coaddr),
  1689. GFP_KERNEL);
  1690. if (!x->coaddr)
  1691. goto error;
  1692. }
  1693. if (orig->replay_esn) {
  1694. if (xfrm_replay_clone(x, orig))
  1695. goto error;
  1696. }
  1697. memcpy(&x->mark, &orig->mark, sizeof(x->mark));
  1698. memcpy(&x->props.smark, &orig->props.smark, sizeof(x->props.smark));
  1699. x->props.flags = orig->props.flags;
  1700. x->props.extra_flags = orig->props.extra_flags;
  1701. x->pcpu_num = orig->pcpu_num;
  1702. x->if_id = orig->if_id;
  1703. x->tfcpad = orig->tfcpad;
  1704. x->replay_maxdiff = orig->replay_maxdiff;
  1705. x->replay_maxage = orig->replay_maxage;
  1706. memcpy(&x->curlft, &orig->curlft, sizeof(x->curlft));
  1707. x->km.state = orig->km.state;
  1708. x->km.seq = orig->km.seq;
  1709. x->replay = orig->replay;
  1710. x->preplay = orig->preplay;
  1711. x->mapping_maxage = orig->mapping_maxage;
  1712. x->lastused = orig->lastused;
  1713. x->new_mapping = 0;
  1714. x->new_mapping_sport = 0;
  1715. x->dir = orig->dir;
  1716. return x;
  1717. error:
  1718. xfrm_state_put(x);
  1719. out:
  1720. return NULL;
  1721. }
  1722. struct xfrm_state *xfrm_migrate_state_find(struct xfrm_migrate *m, struct net *net,
  1723. u32 if_id)
  1724. {
  1725. unsigned int h;
  1726. struct xfrm_state *x = NULL;
  1727. spin_lock_bh(&net->xfrm.xfrm_state_lock);
  1728. if (m->reqid) {
  1729. h = xfrm_dst_hash(net, &m->old_daddr, &m->old_saddr,
  1730. m->reqid, m->old_family);
  1731. hlist_for_each_entry(x, net->xfrm.state_bydst+h, bydst) {
  1732. if (x->props.mode != m->mode ||
  1733. x->id.proto != m->proto)
  1734. continue;
  1735. if (m->reqid && x->props.reqid != m->reqid)
  1736. continue;
  1737. if (if_id != 0 && x->if_id != if_id)
  1738. continue;
  1739. if (!xfrm_addr_equal(&x->id.daddr, &m->old_daddr,
  1740. m->old_family) ||
  1741. !xfrm_addr_equal(&x->props.saddr, &m->old_saddr,
  1742. m->old_family))
  1743. continue;
  1744. xfrm_state_hold(x);
  1745. break;
  1746. }
  1747. } else {
  1748. h = xfrm_src_hash(net, &m->old_daddr, &m->old_saddr,
  1749. m->old_family);
  1750. hlist_for_each_entry(x, net->xfrm.state_bysrc+h, bysrc) {
  1751. if (x->props.mode != m->mode ||
  1752. x->id.proto != m->proto)
  1753. continue;
  1754. if (if_id != 0 && x->if_id != if_id)
  1755. continue;
  1756. if (!xfrm_addr_equal(&x->id.daddr, &m->old_daddr,
  1757. m->old_family) ||
  1758. !xfrm_addr_equal(&x->props.saddr, &m->old_saddr,
  1759. m->old_family))
  1760. continue;
  1761. xfrm_state_hold(x);
  1762. break;
  1763. }
  1764. }
  1765. spin_unlock_bh(&net->xfrm.xfrm_state_lock);
  1766. return x;
  1767. }
  1768. EXPORT_SYMBOL(xfrm_migrate_state_find);
  1769. struct xfrm_state *xfrm_state_migrate(struct xfrm_state *x,
  1770. struct xfrm_migrate *m,
  1771. struct xfrm_encap_tmpl *encap)
  1772. {
  1773. struct xfrm_state *xc;
  1774. xc = xfrm_state_clone(x, encap);
  1775. if (!xc)
  1776. return NULL;
  1777. xc->props.family = m->new_family;
  1778. if (xfrm_init_state(xc) < 0)
  1779. goto error;
  1780. memcpy(&xc->id.daddr, &m->new_daddr, sizeof(xc->id.daddr));
  1781. memcpy(&xc->props.saddr, &m->new_saddr, sizeof(xc->props.saddr));
  1782. /* add state */
  1783. if (xfrm_addr_equal(&x->id.daddr, &m->new_daddr, m->new_family)) {
  1784. /* a care is needed when the destination address of the
  1785. state is to be updated as it is a part of triplet */
  1786. xfrm_state_insert(xc);
  1787. } else {
  1788. if (xfrm_state_add(xc) < 0)
  1789. goto error;
  1790. }
  1791. return xc;
  1792. error:
  1793. xfrm_state_put(xc);
  1794. return NULL;
  1795. }
  1796. EXPORT_SYMBOL(xfrm_state_migrate);
  1797. #endif
  1798. int xfrm_state_update(struct xfrm_state *x)
  1799. {
  1800. struct xfrm_state *x1, *to_put;
  1801. int err;
  1802. int use_spi = xfrm_id_proto_match(x->id.proto, IPSEC_PROTO_ANY);
  1803. struct net *net = xs_net(x);
  1804. to_put = NULL;
  1805. spin_lock_bh(&net->xfrm.xfrm_state_lock);
  1806. x1 = __xfrm_state_locate(x, use_spi, x->props.family);
  1807. err = -ESRCH;
  1808. if (!x1)
  1809. goto out;
  1810. if (xfrm_state_kern(x1)) {
  1811. to_put = x1;
  1812. err = -EEXIST;
  1813. goto out;
  1814. }
  1815. if (x1->km.state == XFRM_STATE_ACQ) {
  1816. if (x->dir && x1->dir != x->dir)
  1817. goto out;
  1818. __xfrm_state_insert(x);
  1819. x = NULL;
  1820. } else {
  1821. if (x1->dir != x->dir)
  1822. goto out;
  1823. }
  1824. err = 0;
  1825. out:
  1826. spin_unlock_bh(&net->xfrm.xfrm_state_lock);
  1827. if (to_put)
  1828. xfrm_state_put(to_put);
  1829. if (err)
  1830. return err;
  1831. if (!x) {
  1832. xfrm_state_delete(x1);
  1833. xfrm_state_put(x1);
  1834. return 0;
  1835. }
  1836. err = -EINVAL;
  1837. spin_lock_bh(&x1->lock);
  1838. if (likely(x1->km.state == XFRM_STATE_VALID)) {
  1839. if (x->encap && x1->encap &&
  1840. x->encap->encap_type == x1->encap->encap_type)
  1841. memcpy(x1->encap, x->encap, sizeof(*x1->encap));
  1842. else if (x->encap || x1->encap)
  1843. goto fail;
  1844. if (x->coaddr && x1->coaddr) {
  1845. memcpy(x1->coaddr, x->coaddr, sizeof(*x1->coaddr));
  1846. }
  1847. if (!use_spi && memcmp(&x1->sel, &x->sel, sizeof(x1->sel)))
  1848. memcpy(&x1->sel, &x->sel, sizeof(x1->sel));
  1849. memcpy(&x1->lft, &x->lft, sizeof(x1->lft));
  1850. x1->km.dying = 0;
  1851. hrtimer_start(&x1->mtimer, ktime_set(1, 0),
  1852. HRTIMER_MODE_REL_SOFT);
  1853. if (READ_ONCE(x1->curlft.use_time))
  1854. xfrm_state_check_expire(x1);
  1855. if (x->props.smark.m || x->props.smark.v || x->if_id) {
  1856. spin_lock_bh(&net->xfrm.xfrm_state_lock);
  1857. if (x->props.smark.m || x->props.smark.v)
  1858. x1->props.smark = x->props.smark;
  1859. if (x->if_id)
  1860. x1->if_id = x->if_id;
  1861. __xfrm_state_bump_genids(x1);
  1862. spin_unlock_bh(&net->xfrm.xfrm_state_lock);
  1863. }
  1864. err = 0;
  1865. x->km.state = XFRM_STATE_DEAD;
  1866. __xfrm_state_put(x);
  1867. }
  1868. fail:
  1869. spin_unlock_bh(&x1->lock);
  1870. xfrm_state_put(x1);
  1871. return err;
  1872. }
  1873. EXPORT_SYMBOL(xfrm_state_update);
  1874. int xfrm_state_check_expire(struct xfrm_state *x)
  1875. {
  1876. xfrm_dev_state_update_stats(x);
  1877. if (!READ_ONCE(x->curlft.use_time))
  1878. WRITE_ONCE(x->curlft.use_time, ktime_get_real_seconds());
  1879. if (x->curlft.bytes >= x->lft.hard_byte_limit ||
  1880. x->curlft.packets >= x->lft.hard_packet_limit) {
  1881. x->km.state = XFRM_STATE_EXPIRED;
  1882. hrtimer_start(&x->mtimer, 0, HRTIMER_MODE_REL_SOFT);
  1883. return -EINVAL;
  1884. }
  1885. if (!x->km.dying &&
  1886. (x->curlft.bytes >= x->lft.soft_byte_limit ||
  1887. x->curlft.packets >= x->lft.soft_packet_limit)) {
  1888. x->km.dying = 1;
  1889. km_state_expired(x, 0, 0);
  1890. }
  1891. return 0;
  1892. }
  1893. EXPORT_SYMBOL(xfrm_state_check_expire);
  1894. void xfrm_state_update_stats(struct net *net)
  1895. {
  1896. struct xfrm_state *x;
  1897. int i;
  1898. spin_lock_bh(&net->xfrm.xfrm_state_lock);
  1899. for (i = 0; i <= net->xfrm.state_hmask; i++) {
  1900. hlist_for_each_entry(x, net->xfrm.state_bydst + i, bydst)
  1901. xfrm_dev_state_update_stats(x);
  1902. }
  1903. spin_unlock_bh(&net->xfrm.xfrm_state_lock);
  1904. }
  1905. struct xfrm_state *
  1906. xfrm_state_lookup(struct net *net, u32 mark, const xfrm_address_t *daddr, __be32 spi,
  1907. u8 proto, unsigned short family)
  1908. {
  1909. struct xfrm_hash_state_ptrs state_ptrs;
  1910. struct xfrm_state *x;
  1911. rcu_read_lock();
  1912. xfrm_hash_ptrs_get(net, &state_ptrs);
  1913. x = __xfrm_state_lookup(&state_ptrs, mark, daddr, spi, proto, family);
  1914. rcu_read_unlock();
  1915. return x;
  1916. }
  1917. EXPORT_SYMBOL(xfrm_state_lookup);
  1918. struct xfrm_state *
  1919. xfrm_state_lookup_byaddr(struct net *net, u32 mark,
  1920. const xfrm_address_t *daddr, const xfrm_address_t *saddr,
  1921. u8 proto, unsigned short family)
  1922. {
  1923. struct xfrm_hash_state_ptrs state_ptrs;
  1924. struct xfrm_state *x;
  1925. spin_lock_bh(&net->xfrm.xfrm_state_lock);
  1926. xfrm_hash_ptrs_get(net, &state_ptrs);
  1927. x = __xfrm_state_lookup_byaddr(&state_ptrs, mark, daddr, saddr, proto, family);
  1928. spin_unlock_bh(&net->xfrm.xfrm_state_lock);
  1929. return x;
  1930. }
  1931. EXPORT_SYMBOL(xfrm_state_lookup_byaddr);
  1932. struct xfrm_state *
  1933. xfrm_find_acq(struct net *net, const struct xfrm_mark *mark, u8 mode, u32 reqid,
  1934. u32 if_id, u32 pcpu_num, u8 proto, const xfrm_address_t *daddr,
  1935. const xfrm_address_t *saddr, int create, unsigned short family)
  1936. {
  1937. struct xfrm_state *x;
  1938. spin_lock_bh(&net->xfrm.xfrm_state_lock);
  1939. x = __find_acq_core(net, mark, family, mode, reqid, if_id, pcpu_num,
  1940. proto, daddr, saddr, create);
  1941. spin_unlock_bh(&net->xfrm.xfrm_state_lock);
  1942. return x;
  1943. }
  1944. EXPORT_SYMBOL(xfrm_find_acq);
  1945. #ifdef CONFIG_XFRM_SUB_POLICY
  1946. #if IS_ENABLED(CONFIG_IPV6)
  1947. /* distribution counting sort function for xfrm_state and xfrm_tmpl */
  1948. static void
  1949. __xfrm6_sort(void **dst, void **src, int n,
  1950. int (*cmp)(const void *p), int maxclass)
  1951. {
  1952. int count[XFRM_MAX_DEPTH] = { };
  1953. int class[XFRM_MAX_DEPTH];
  1954. int i;
  1955. for (i = 0; i < n; i++) {
  1956. int c = cmp(src[i]);
  1957. class[i] = c;
  1958. count[c]++;
  1959. }
  1960. for (i = 2; i < maxclass; i++)
  1961. count[i] += count[i - 1];
  1962. for (i = 0; i < n; i++) {
  1963. dst[count[class[i] - 1]++] = src[i];
  1964. src[i] = NULL;
  1965. }
  1966. }
  1967. /* Rule for xfrm_state:
  1968. *
  1969. * rule 1: select IPsec transport except AH
  1970. * rule 2: select MIPv6 RO or inbound trigger
  1971. * rule 3: select IPsec transport AH
  1972. * rule 4: select IPsec tunnel
  1973. * rule 5: others
  1974. */
  1975. static int __xfrm6_state_sort_cmp(const void *p)
  1976. {
  1977. const struct xfrm_state *v = p;
  1978. switch (v->props.mode) {
  1979. case XFRM_MODE_TRANSPORT:
  1980. if (v->id.proto != IPPROTO_AH)
  1981. return 1;
  1982. else
  1983. return 3;
  1984. #if IS_ENABLED(CONFIG_IPV6_MIP6)
  1985. case XFRM_MODE_ROUTEOPTIMIZATION:
  1986. case XFRM_MODE_IN_TRIGGER:
  1987. return 2;
  1988. #endif
  1989. case XFRM_MODE_TUNNEL:
  1990. case XFRM_MODE_BEET:
  1991. return 4;
  1992. }
  1993. return 5;
  1994. }
  1995. /* Rule for xfrm_tmpl:
  1996. *
  1997. * rule 1: select IPsec transport
  1998. * rule 2: select MIPv6 RO or inbound trigger
  1999. * rule 3: select IPsec tunnel
  2000. * rule 4: others
  2001. */
  2002. static int __xfrm6_tmpl_sort_cmp(const void *p)
  2003. {
  2004. const struct xfrm_tmpl *v = p;
  2005. switch (v->mode) {
  2006. case XFRM_MODE_TRANSPORT:
  2007. return 1;
  2008. #if IS_ENABLED(CONFIG_IPV6_MIP6)
  2009. case XFRM_MODE_ROUTEOPTIMIZATION:
  2010. case XFRM_MODE_IN_TRIGGER:
  2011. return 2;
  2012. #endif
  2013. case XFRM_MODE_TUNNEL:
  2014. case XFRM_MODE_BEET:
  2015. return 3;
  2016. }
  2017. return 4;
  2018. }
  2019. #else
  2020. static inline int __xfrm6_state_sort_cmp(const void *p) { return 5; }
  2021. static inline int __xfrm6_tmpl_sort_cmp(const void *p) { return 4; }
  2022. static inline void
  2023. __xfrm6_sort(void **dst, void **src, int n,
  2024. int (*cmp)(const void *p), int maxclass)
  2025. {
  2026. int i;
  2027. for (i = 0; i < n; i++)
  2028. dst[i] = src[i];
  2029. }
  2030. #endif /* CONFIG_IPV6 */
  2031. void
  2032. xfrm_tmpl_sort(struct xfrm_tmpl **dst, struct xfrm_tmpl **src, int n,
  2033. unsigned short family)
  2034. {
  2035. int i;
  2036. if (family == AF_INET6)
  2037. __xfrm6_sort((void **)dst, (void **)src, n,
  2038. __xfrm6_tmpl_sort_cmp, 5);
  2039. else
  2040. for (i = 0; i < n; i++)
  2041. dst[i] = src[i];
  2042. }
  2043. void
  2044. xfrm_state_sort(struct xfrm_state **dst, struct xfrm_state **src, int n,
  2045. unsigned short family)
  2046. {
  2047. int i;
  2048. if (family == AF_INET6)
  2049. __xfrm6_sort((void **)dst, (void **)src, n,
  2050. __xfrm6_state_sort_cmp, 6);
  2051. else
  2052. for (i = 0; i < n; i++)
  2053. dst[i] = src[i];
  2054. }
  2055. #endif
  2056. /* Silly enough, but I'm lazy to build resolution list */
  2057. static struct xfrm_state *__xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq, u32 pcpu_num)
  2058. {
  2059. unsigned int h = xfrm_seq_hash(net, seq);
  2060. struct xfrm_state *x;
  2061. hlist_for_each_entry_rcu(x, net->xfrm.state_byseq + h, byseq) {
  2062. if (x->km.seq == seq &&
  2063. (mark & x->mark.m) == x->mark.v &&
  2064. x->pcpu_num == pcpu_num &&
  2065. x->km.state == XFRM_STATE_ACQ) {
  2066. xfrm_state_hold(x);
  2067. return x;
  2068. }
  2069. }
  2070. return NULL;
  2071. }
  2072. struct xfrm_state *xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq, u32 pcpu_num)
  2073. {
  2074. struct xfrm_state *x;
  2075. spin_lock_bh(&net->xfrm.xfrm_state_lock);
  2076. x = __xfrm_find_acq_byseq(net, mark, seq, pcpu_num);
  2077. spin_unlock_bh(&net->xfrm.xfrm_state_lock);
  2078. return x;
  2079. }
  2080. EXPORT_SYMBOL(xfrm_find_acq_byseq);
  2081. u32 xfrm_get_acqseq(void)
  2082. {
  2083. u32 res;
  2084. static atomic_t acqseq;
  2085. do {
  2086. res = atomic_inc_return(&acqseq);
  2087. } while (!res);
  2088. return res;
  2089. }
  2090. EXPORT_SYMBOL(xfrm_get_acqseq);
  2091. int verify_spi_info(u8 proto, u32 min, u32 max, struct netlink_ext_ack *extack)
  2092. {
  2093. switch (proto) {
  2094. case IPPROTO_AH:
  2095. case IPPROTO_ESP:
  2096. break;
  2097. case IPPROTO_COMP:
  2098. /* IPCOMP spi is 16-bits. */
  2099. if (max >= 0x10000) {
  2100. NL_SET_ERR_MSG(extack, "IPCOMP SPI must be <= 65535");
  2101. return -EINVAL;
  2102. }
  2103. break;
  2104. default:
  2105. NL_SET_ERR_MSG(extack, "Invalid protocol, must be one of AH, ESP, IPCOMP");
  2106. return -EINVAL;
  2107. }
  2108. if (min > max) {
  2109. NL_SET_ERR_MSG(extack, "Invalid SPI range: min > max");
  2110. return -EINVAL;
  2111. }
  2112. return 0;
  2113. }
  2114. EXPORT_SYMBOL(verify_spi_info);
  2115. int xfrm_alloc_spi(struct xfrm_state *x, u32 low, u32 high,
  2116. struct netlink_ext_ack *extack)
  2117. {
  2118. struct net *net = xs_net(x);
  2119. unsigned int h;
  2120. struct xfrm_state *x0;
  2121. int err = -ENOENT;
  2122. __be32 minspi = htonl(low);
  2123. __be32 maxspi = htonl(high);
  2124. __be32 newspi = 0;
  2125. u32 mark = x->mark.v & x->mark.m;
  2126. spin_lock_bh(&x->lock);
  2127. if (x->km.state == XFRM_STATE_DEAD) {
  2128. NL_SET_ERR_MSG(extack, "Target ACQUIRE is in DEAD state");
  2129. goto unlock;
  2130. }
  2131. err = 0;
  2132. if (x->id.spi)
  2133. goto unlock;
  2134. err = -ENOENT;
  2135. if (minspi == maxspi) {
  2136. x0 = xfrm_state_lookup(net, mark, &x->id.daddr, minspi, x->id.proto, x->props.family);
  2137. if (x0) {
  2138. NL_SET_ERR_MSG(extack, "Requested SPI is already in use");
  2139. xfrm_state_put(x0);
  2140. goto unlock;
  2141. }
  2142. newspi = minspi;
  2143. } else {
  2144. u32 spi = 0;
  2145. for (h = 0; h < high-low+1; h++) {
  2146. spi = get_random_u32_inclusive(low, high);
  2147. x0 = xfrm_state_lookup(net, mark, &x->id.daddr, htonl(spi), x->id.proto, x->props.family);
  2148. if (x0 == NULL) {
  2149. newspi = htonl(spi);
  2150. break;
  2151. }
  2152. xfrm_state_put(x0);
  2153. }
  2154. }
  2155. if (newspi) {
  2156. spin_lock_bh(&net->xfrm.xfrm_state_lock);
  2157. x->id.spi = newspi;
  2158. h = xfrm_spi_hash(net, &x->id.daddr, x->id.spi, x->id.proto, x->props.family);
  2159. XFRM_STATE_INSERT(byspi, &x->byspi, net->xfrm.state_byspi + h,
  2160. x->xso.type);
  2161. spin_unlock_bh(&net->xfrm.xfrm_state_lock);
  2162. err = 0;
  2163. } else {
  2164. NL_SET_ERR_MSG(extack, "No SPI available in the requested range");
  2165. }
  2166. unlock:
  2167. spin_unlock_bh(&x->lock);
  2168. return err;
  2169. }
  2170. EXPORT_SYMBOL(xfrm_alloc_spi);
  2171. static bool __xfrm_state_filter_match(struct xfrm_state *x,
  2172. struct xfrm_address_filter *filter)
  2173. {
  2174. if (filter) {
  2175. if ((filter->family == AF_INET ||
  2176. filter->family == AF_INET6) &&
  2177. x->props.family != filter->family)
  2178. return false;
  2179. return addr_match(&x->props.saddr, &filter->saddr,
  2180. filter->splen) &&
  2181. addr_match(&x->id.daddr, &filter->daddr,
  2182. filter->dplen);
  2183. }
  2184. return true;
  2185. }
  2186. int xfrm_state_walk(struct net *net, struct xfrm_state_walk *walk,
  2187. int (*func)(struct xfrm_state *, int, void*),
  2188. void *data)
  2189. {
  2190. struct xfrm_state *state;
  2191. struct xfrm_state_walk *x;
  2192. int err = 0;
  2193. if (walk->seq != 0 && list_empty(&walk->all))
  2194. return 0;
  2195. spin_lock_bh(&net->xfrm.xfrm_state_lock);
  2196. if (list_empty(&walk->all))
  2197. x = list_first_entry(&net->xfrm.state_all, struct xfrm_state_walk, all);
  2198. else
  2199. x = list_first_entry(&walk->all, struct xfrm_state_walk, all);
  2200. list_for_each_entry_from(x, &net->xfrm.state_all, all) {
  2201. if (x->state == XFRM_STATE_DEAD)
  2202. continue;
  2203. state = container_of(x, struct xfrm_state, km);
  2204. if (!xfrm_id_proto_match(state->id.proto, walk->proto))
  2205. continue;
  2206. if (!__xfrm_state_filter_match(state, walk->filter))
  2207. continue;
  2208. err = func(state, walk->seq, data);
  2209. if (err) {
  2210. list_move_tail(&walk->all, &x->all);
  2211. goto out;
  2212. }
  2213. walk->seq++;
  2214. }
  2215. if (walk->seq == 0) {
  2216. err = -ENOENT;
  2217. goto out;
  2218. }
  2219. list_del_init(&walk->all);
  2220. out:
  2221. spin_unlock_bh(&net->xfrm.xfrm_state_lock);
  2222. return err;
  2223. }
  2224. EXPORT_SYMBOL(xfrm_state_walk);
  2225. void xfrm_state_walk_init(struct xfrm_state_walk *walk, u8 proto,
  2226. struct xfrm_address_filter *filter)
  2227. {
  2228. INIT_LIST_HEAD(&walk->all);
  2229. walk->proto = proto;
  2230. walk->state = XFRM_STATE_DEAD;
  2231. walk->seq = 0;
  2232. walk->filter = filter;
  2233. }
  2234. EXPORT_SYMBOL(xfrm_state_walk_init);
  2235. void xfrm_state_walk_done(struct xfrm_state_walk *walk, struct net *net)
  2236. {
  2237. kfree(walk->filter);
  2238. if (list_empty(&walk->all))
  2239. return;
  2240. spin_lock_bh(&net->xfrm.xfrm_state_lock);
  2241. list_del(&walk->all);
  2242. spin_unlock_bh(&net->xfrm.xfrm_state_lock);
  2243. }
  2244. EXPORT_SYMBOL(xfrm_state_walk_done);
  2245. static void xfrm_replay_timer_handler(struct timer_list *t)
  2246. {
  2247. struct xfrm_state *x = from_timer(x, t, rtimer);
  2248. spin_lock(&x->lock);
  2249. if (x->km.state == XFRM_STATE_VALID) {
  2250. if (xfrm_aevent_is_on(xs_net(x)))
  2251. xfrm_replay_notify(x, XFRM_REPLAY_TIMEOUT);
  2252. else
  2253. x->xflags |= XFRM_TIME_DEFER;
  2254. }
  2255. spin_unlock(&x->lock);
  2256. }
  2257. static LIST_HEAD(xfrm_km_list);
  2258. void km_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
  2259. {
  2260. struct xfrm_mgr *km;
  2261. rcu_read_lock();
  2262. list_for_each_entry_rcu(km, &xfrm_km_list, list)
  2263. if (km->notify_policy)
  2264. km->notify_policy(xp, dir, c);
  2265. rcu_read_unlock();
  2266. }
  2267. void km_state_notify(struct xfrm_state *x, const struct km_event *c)
  2268. {
  2269. struct xfrm_mgr *km;
  2270. rcu_read_lock();
  2271. list_for_each_entry_rcu(km, &xfrm_km_list, list)
  2272. if (km->notify)
  2273. km->notify(x, c);
  2274. rcu_read_unlock();
  2275. }
  2276. EXPORT_SYMBOL(km_policy_notify);
  2277. EXPORT_SYMBOL(km_state_notify);
  2278. void km_state_expired(struct xfrm_state *x, int hard, u32 portid)
  2279. {
  2280. struct km_event c;
  2281. c.data.hard = hard;
  2282. c.portid = portid;
  2283. c.event = XFRM_MSG_EXPIRE;
  2284. km_state_notify(x, &c);
  2285. }
  2286. EXPORT_SYMBOL(km_state_expired);
  2287. /*
  2288. * We send to all registered managers regardless of failure
  2289. * We are happy with one success
  2290. */
  2291. int km_query(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *pol)
  2292. {
  2293. int err = -EINVAL, acqret;
  2294. struct xfrm_mgr *km;
  2295. rcu_read_lock();
  2296. list_for_each_entry_rcu(km, &xfrm_km_list, list) {
  2297. acqret = km->acquire(x, t, pol);
  2298. if (!acqret)
  2299. err = acqret;
  2300. }
  2301. rcu_read_unlock();
  2302. return err;
  2303. }
  2304. EXPORT_SYMBOL(km_query);
  2305. static int __km_new_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr, __be16 sport)
  2306. {
  2307. int err = -EINVAL;
  2308. struct xfrm_mgr *km;
  2309. rcu_read_lock();
  2310. list_for_each_entry_rcu(km, &xfrm_km_list, list) {
  2311. if (km->new_mapping)
  2312. err = km->new_mapping(x, ipaddr, sport);
  2313. if (!err)
  2314. break;
  2315. }
  2316. rcu_read_unlock();
  2317. return err;
  2318. }
  2319. int km_new_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr, __be16 sport)
  2320. {
  2321. int ret = 0;
  2322. if (x->mapping_maxage) {
  2323. if ((jiffies / HZ - x->new_mapping) > x->mapping_maxage ||
  2324. x->new_mapping_sport != sport) {
  2325. x->new_mapping_sport = sport;
  2326. x->new_mapping = jiffies / HZ;
  2327. ret = __km_new_mapping(x, ipaddr, sport);
  2328. }
  2329. } else {
  2330. ret = __km_new_mapping(x, ipaddr, sport);
  2331. }
  2332. return ret;
  2333. }
  2334. EXPORT_SYMBOL(km_new_mapping);
  2335. void km_policy_expired(struct xfrm_policy *pol, int dir, int hard, u32 portid)
  2336. {
  2337. struct km_event c;
  2338. c.data.hard = hard;
  2339. c.portid = portid;
  2340. c.event = XFRM_MSG_POLEXPIRE;
  2341. km_policy_notify(pol, dir, &c);
  2342. }
  2343. EXPORT_SYMBOL(km_policy_expired);
  2344. #ifdef CONFIG_XFRM_MIGRATE
  2345. int km_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
  2346. const struct xfrm_migrate *m, int num_migrate,
  2347. const struct xfrm_kmaddress *k,
  2348. const struct xfrm_encap_tmpl *encap)
  2349. {
  2350. int err = -EINVAL;
  2351. int ret;
  2352. struct xfrm_mgr *km;
  2353. rcu_read_lock();
  2354. list_for_each_entry_rcu(km, &xfrm_km_list, list) {
  2355. if (km->migrate) {
  2356. ret = km->migrate(sel, dir, type, m, num_migrate, k,
  2357. encap);
  2358. if (!ret)
  2359. err = ret;
  2360. }
  2361. }
  2362. rcu_read_unlock();
  2363. return err;
  2364. }
  2365. EXPORT_SYMBOL(km_migrate);
  2366. #endif
  2367. int km_report(struct net *net, u8 proto, struct xfrm_selector *sel, xfrm_address_t *addr)
  2368. {
  2369. int err = -EINVAL;
  2370. int ret;
  2371. struct xfrm_mgr *km;
  2372. rcu_read_lock();
  2373. list_for_each_entry_rcu(km, &xfrm_km_list, list) {
  2374. if (km->report) {
  2375. ret = km->report(net, proto, sel, addr);
  2376. if (!ret)
  2377. err = ret;
  2378. }
  2379. }
  2380. rcu_read_unlock();
  2381. return err;
  2382. }
  2383. EXPORT_SYMBOL(km_report);
  2384. static bool km_is_alive(const struct km_event *c)
  2385. {
  2386. struct xfrm_mgr *km;
  2387. bool is_alive = false;
  2388. rcu_read_lock();
  2389. list_for_each_entry_rcu(km, &xfrm_km_list, list) {
  2390. if (km->is_alive && km->is_alive(c)) {
  2391. is_alive = true;
  2392. break;
  2393. }
  2394. }
  2395. rcu_read_unlock();
  2396. return is_alive;
  2397. }
  2398. #if IS_ENABLED(CONFIG_XFRM_USER_COMPAT)
  2399. static DEFINE_SPINLOCK(xfrm_translator_lock);
  2400. static struct xfrm_translator __rcu *xfrm_translator;
  2401. struct xfrm_translator *xfrm_get_translator(void)
  2402. {
  2403. struct xfrm_translator *xtr;
  2404. rcu_read_lock();
  2405. xtr = rcu_dereference(xfrm_translator);
  2406. if (unlikely(!xtr))
  2407. goto out;
  2408. if (!try_module_get(xtr->owner))
  2409. xtr = NULL;
  2410. out:
  2411. rcu_read_unlock();
  2412. return xtr;
  2413. }
  2414. EXPORT_SYMBOL_GPL(xfrm_get_translator);
  2415. void xfrm_put_translator(struct xfrm_translator *xtr)
  2416. {
  2417. module_put(xtr->owner);
  2418. }
  2419. EXPORT_SYMBOL_GPL(xfrm_put_translator);
  2420. int xfrm_register_translator(struct xfrm_translator *xtr)
  2421. {
  2422. int err = 0;
  2423. spin_lock_bh(&xfrm_translator_lock);
  2424. if (unlikely(xfrm_translator != NULL))
  2425. err = -EEXIST;
  2426. else
  2427. rcu_assign_pointer(xfrm_translator, xtr);
  2428. spin_unlock_bh(&xfrm_translator_lock);
  2429. return err;
  2430. }
  2431. EXPORT_SYMBOL_GPL(xfrm_register_translator);
  2432. int xfrm_unregister_translator(struct xfrm_translator *xtr)
  2433. {
  2434. int err = 0;
  2435. spin_lock_bh(&xfrm_translator_lock);
  2436. if (likely(xfrm_translator != NULL)) {
  2437. if (rcu_access_pointer(xfrm_translator) != xtr)
  2438. err = -EINVAL;
  2439. else
  2440. RCU_INIT_POINTER(xfrm_translator, NULL);
  2441. }
  2442. spin_unlock_bh(&xfrm_translator_lock);
  2443. synchronize_rcu();
  2444. return err;
  2445. }
  2446. EXPORT_SYMBOL_GPL(xfrm_unregister_translator);
  2447. #endif
  2448. int xfrm_user_policy(struct sock *sk, int optname, sockptr_t optval, int optlen)
  2449. {
  2450. int err;
  2451. u8 *data;
  2452. struct xfrm_mgr *km;
  2453. struct xfrm_policy *pol = NULL;
  2454. if (sockptr_is_null(optval) && !optlen) {
  2455. xfrm_sk_policy_insert(sk, XFRM_POLICY_IN, NULL);
  2456. xfrm_sk_policy_insert(sk, XFRM_POLICY_OUT, NULL);
  2457. __sk_dst_reset(sk);
  2458. return 0;
  2459. }
  2460. if (optlen <= 0 || optlen > PAGE_SIZE)
  2461. return -EMSGSIZE;
  2462. data = memdup_sockptr(optval, optlen);
  2463. if (IS_ERR(data))
  2464. return PTR_ERR(data);
  2465. if (in_compat_syscall()) {
  2466. struct xfrm_translator *xtr = xfrm_get_translator();
  2467. if (!xtr) {
  2468. kfree(data);
  2469. return -EOPNOTSUPP;
  2470. }
  2471. err = xtr->xlate_user_policy_sockptr(&data, optlen);
  2472. xfrm_put_translator(xtr);
  2473. if (err) {
  2474. kfree(data);
  2475. return err;
  2476. }
  2477. }
  2478. err = -EINVAL;
  2479. rcu_read_lock();
  2480. list_for_each_entry_rcu(km, &xfrm_km_list, list) {
  2481. pol = km->compile_policy(sk, optname, data,
  2482. optlen, &err);
  2483. if (err >= 0)
  2484. break;
  2485. }
  2486. rcu_read_unlock();
  2487. if (err >= 0) {
  2488. xfrm_sk_policy_insert(sk, err, pol);
  2489. xfrm_pol_put(pol);
  2490. __sk_dst_reset(sk);
  2491. err = 0;
  2492. }
  2493. kfree(data);
  2494. return err;
  2495. }
  2496. EXPORT_SYMBOL(xfrm_user_policy);
  2497. static DEFINE_SPINLOCK(xfrm_km_lock);
  2498. void xfrm_register_km(struct xfrm_mgr *km)
  2499. {
  2500. spin_lock_bh(&xfrm_km_lock);
  2501. list_add_tail_rcu(&km->list, &xfrm_km_list);
  2502. spin_unlock_bh(&xfrm_km_lock);
  2503. }
  2504. EXPORT_SYMBOL(xfrm_register_km);
  2505. void xfrm_unregister_km(struct xfrm_mgr *km)
  2506. {
  2507. spin_lock_bh(&xfrm_km_lock);
  2508. list_del_rcu(&km->list);
  2509. spin_unlock_bh(&xfrm_km_lock);
  2510. synchronize_rcu();
  2511. }
  2512. EXPORT_SYMBOL(xfrm_unregister_km);
  2513. int xfrm_state_register_afinfo(struct xfrm_state_afinfo *afinfo)
  2514. {
  2515. int err = 0;
  2516. if (WARN_ON(afinfo->family >= NPROTO))
  2517. return -EAFNOSUPPORT;
  2518. spin_lock_bh(&xfrm_state_afinfo_lock);
  2519. if (unlikely(xfrm_state_afinfo[afinfo->family] != NULL))
  2520. err = -EEXIST;
  2521. else
  2522. rcu_assign_pointer(xfrm_state_afinfo[afinfo->family], afinfo);
  2523. spin_unlock_bh(&xfrm_state_afinfo_lock);
  2524. return err;
  2525. }
  2526. EXPORT_SYMBOL(xfrm_state_register_afinfo);
  2527. int xfrm_state_unregister_afinfo(struct xfrm_state_afinfo *afinfo)
  2528. {
  2529. int err = 0, family = afinfo->family;
  2530. if (WARN_ON(family >= NPROTO))
  2531. return -EAFNOSUPPORT;
  2532. spin_lock_bh(&xfrm_state_afinfo_lock);
  2533. if (likely(xfrm_state_afinfo[afinfo->family] != NULL)) {
  2534. if (rcu_access_pointer(xfrm_state_afinfo[family]) != afinfo)
  2535. err = -EINVAL;
  2536. else
  2537. RCU_INIT_POINTER(xfrm_state_afinfo[afinfo->family], NULL);
  2538. }
  2539. spin_unlock_bh(&xfrm_state_afinfo_lock);
  2540. synchronize_rcu();
  2541. return err;
  2542. }
  2543. EXPORT_SYMBOL(xfrm_state_unregister_afinfo);
  2544. struct xfrm_state_afinfo *xfrm_state_afinfo_get_rcu(unsigned int family)
  2545. {
  2546. if (unlikely(family >= NPROTO))
  2547. return NULL;
  2548. return rcu_dereference(xfrm_state_afinfo[family]);
  2549. }
  2550. EXPORT_SYMBOL_GPL(xfrm_state_afinfo_get_rcu);
  2551. struct xfrm_state_afinfo *xfrm_state_get_afinfo(unsigned int family)
  2552. {
  2553. struct xfrm_state_afinfo *afinfo;
  2554. if (unlikely(family >= NPROTO))
  2555. return NULL;
  2556. rcu_read_lock();
  2557. afinfo = rcu_dereference(xfrm_state_afinfo[family]);
  2558. if (unlikely(!afinfo))
  2559. rcu_read_unlock();
  2560. return afinfo;
  2561. }
  2562. void xfrm_flush_gc(void)
  2563. {
  2564. flush_work(&xfrm_state_gc_work);
  2565. }
  2566. EXPORT_SYMBOL(xfrm_flush_gc);
  2567. /* Temporarily located here until net/xfrm/xfrm_tunnel.c is created */
  2568. void xfrm_state_delete_tunnel(struct xfrm_state *x)
  2569. {
  2570. if (x->tunnel) {
  2571. struct xfrm_state *t = x->tunnel;
  2572. if (atomic_read(&t->tunnel_users) == 2)
  2573. xfrm_state_delete(t);
  2574. atomic_dec(&t->tunnel_users);
  2575. xfrm_state_put_sync(t);
  2576. x->tunnel = NULL;
  2577. }
  2578. }
  2579. EXPORT_SYMBOL(xfrm_state_delete_tunnel);
  2580. u32 xfrm_state_mtu(struct xfrm_state *x, int mtu)
  2581. {
  2582. const struct xfrm_type *type = READ_ONCE(x->type);
  2583. struct crypto_aead *aead;
  2584. u32 blksize, net_adj = 0;
  2585. if (x->km.state != XFRM_STATE_VALID ||
  2586. !type || type->proto != IPPROTO_ESP)
  2587. return mtu - x->props.header_len;
  2588. aead = x->data;
  2589. blksize = ALIGN(crypto_aead_blocksize(aead), 4);
  2590. switch (x->props.mode) {
  2591. case XFRM_MODE_TRANSPORT:
  2592. case XFRM_MODE_BEET:
  2593. if (x->props.family == AF_INET)
  2594. net_adj = sizeof(struct iphdr);
  2595. else if (x->props.family == AF_INET6)
  2596. net_adj = sizeof(struct ipv6hdr);
  2597. break;
  2598. case XFRM_MODE_TUNNEL:
  2599. break;
  2600. default:
  2601. WARN_ON_ONCE(1);
  2602. break;
  2603. }
  2604. return ((mtu - x->props.header_len - crypto_aead_authsize(aead) -
  2605. net_adj) & ~(blksize - 1)) + net_adj - 2;
  2606. }
  2607. EXPORT_SYMBOL_GPL(xfrm_state_mtu);
  2608. int __xfrm_init_state(struct xfrm_state *x, bool init_replay, bool offload,
  2609. struct netlink_ext_ack *extack)
  2610. {
  2611. const struct xfrm_mode *inner_mode;
  2612. const struct xfrm_mode *outer_mode;
  2613. int family = x->props.family;
  2614. int err;
  2615. if (family == AF_INET &&
  2616. READ_ONCE(xs_net(x)->ipv4.sysctl_ip_no_pmtu_disc))
  2617. x->props.flags |= XFRM_STATE_NOPMTUDISC;
  2618. err = -EPROTONOSUPPORT;
  2619. if (x->sel.family != AF_UNSPEC) {
  2620. inner_mode = xfrm_get_mode(x->props.mode, x->sel.family);
  2621. if (inner_mode == NULL) {
  2622. NL_SET_ERR_MSG(extack, "Requested mode not found");
  2623. goto error;
  2624. }
  2625. if (!(inner_mode->flags & XFRM_MODE_FLAG_TUNNEL) &&
  2626. family != x->sel.family) {
  2627. NL_SET_ERR_MSG(extack, "Only tunnel modes can accommodate a change of family");
  2628. goto error;
  2629. }
  2630. x->inner_mode = *inner_mode;
  2631. } else {
  2632. const struct xfrm_mode *inner_mode_iaf;
  2633. int iafamily = AF_INET;
  2634. inner_mode = xfrm_get_mode(x->props.mode, x->props.family);
  2635. if (inner_mode == NULL) {
  2636. NL_SET_ERR_MSG(extack, "Requested mode not found");
  2637. goto error;
  2638. }
  2639. x->inner_mode = *inner_mode;
  2640. if (x->props.family == AF_INET)
  2641. iafamily = AF_INET6;
  2642. inner_mode_iaf = xfrm_get_mode(x->props.mode, iafamily);
  2643. if (inner_mode_iaf) {
  2644. if (inner_mode_iaf->flags & XFRM_MODE_FLAG_TUNNEL)
  2645. x->inner_mode_iaf = *inner_mode_iaf;
  2646. }
  2647. }
  2648. x->type = xfrm_get_type(x->id.proto, family);
  2649. if (x->type == NULL) {
  2650. NL_SET_ERR_MSG(extack, "Requested type not found");
  2651. goto error;
  2652. }
  2653. x->type_offload = xfrm_get_type_offload(x->id.proto, family, offload);
  2654. err = x->type->init_state(x, extack);
  2655. if (err)
  2656. goto error;
  2657. outer_mode = xfrm_get_mode(x->props.mode, family);
  2658. if (!outer_mode) {
  2659. NL_SET_ERR_MSG(extack, "Requested mode not found");
  2660. err = -EPROTONOSUPPORT;
  2661. goto error;
  2662. }
  2663. x->outer_mode = *outer_mode;
  2664. if (init_replay) {
  2665. err = xfrm_init_replay(x, extack);
  2666. if (err)
  2667. goto error;
  2668. }
  2669. if (x->nat_keepalive_interval) {
  2670. if (x->dir != XFRM_SA_DIR_OUT) {
  2671. NL_SET_ERR_MSG(extack, "NAT keepalive is only supported for outbound SAs");
  2672. err = -EINVAL;
  2673. goto error;
  2674. }
  2675. if (!x->encap || x->encap->encap_type != UDP_ENCAP_ESPINUDP) {
  2676. NL_SET_ERR_MSG(extack,
  2677. "NAT keepalive is only supported for UDP encapsulation");
  2678. err = -EINVAL;
  2679. goto error;
  2680. }
  2681. }
  2682. error:
  2683. return err;
  2684. }
  2685. EXPORT_SYMBOL(__xfrm_init_state);
  2686. int xfrm_init_state(struct xfrm_state *x)
  2687. {
  2688. int err;
  2689. err = __xfrm_init_state(x, true, false, NULL);
  2690. if (!err)
  2691. x->km.state = XFRM_STATE_VALID;
  2692. return err;
  2693. }
  2694. EXPORT_SYMBOL(xfrm_init_state);
  2695. int __net_init xfrm_state_init(struct net *net)
  2696. {
  2697. unsigned int sz;
  2698. if (net_eq(net, &init_net))
  2699. xfrm_state_cache = KMEM_CACHE(xfrm_state,
  2700. SLAB_HWCACHE_ALIGN | SLAB_PANIC);
  2701. INIT_LIST_HEAD(&net->xfrm.state_all);
  2702. sz = sizeof(struct hlist_head) * 8;
  2703. net->xfrm.state_bydst = xfrm_hash_alloc(sz);
  2704. if (!net->xfrm.state_bydst)
  2705. goto out_bydst;
  2706. net->xfrm.state_bysrc = xfrm_hash_alloc(sz);
  2707. if (!net->xfrm.state_bysrc)
  2708. goto out_bysrc;
  2709. net->xfrm.state_byspi = xfrm_hash_alloc(sz);
  2710. if (!net->xfrm.state_byspi)
  2711. goto out_byspi;
  2712. net->xfrm.state_byseq = xfrm_hash_alloc(sz);
  2713. if (!net->xfrm.state_byseq)
  2714. goto out_byseq;
  2715. net->xfrm.state_cache_input = alloc_percpu(struct hlist_head);
  2716. if (!net->xfrm.state_cache_input)
  2717. goto out_state_cache_input;
  2718. net->xfrm.state_hmask = ((sz / sizeof(struct hlist_head)) - 1);
  2719. net->xfrm.state_num = 0;
  2720. INIT_WORK(&net->xfrm.state_hash_work, xfrm_hash_resize);
  2721. spin_lock_init(&net->xfrm.xfrm_state_lock);
  2722. seqcount_spinlock_init(&net->xfrm.xfrm_state_hash_generation,
  2723. &net->xfrm.xfrm_state_lock);
  2724. return 0;
  2725. out_state_cache_input:
  2726. xfrm_hash_free(net->xfrm.state_byseq, sz);
  2727. out_byseq:
  2728. xfrm_hash_free(net->xfrm.state_byspi, sz);
  2729. out_byspi:
  2730. xfrm_hash_free(net->xfrm.state_bysrc, sz);
  2731. out_bysrc:
  2732. xfrm_hash_free(net->xfrm.state_bydst, sz);
  2733. out_bydst:
  2734. return -ENOMEM;
  2735. }
  2736. void xfrm_state_fini(struct net *net)
  2737. {
  2738. unsigned int sz;
  2739. flush_work(&net->xfrm.state_hash_work);
  2740. flush_work(&xfrm_state_gc_work);
  2741. xfrm_state_flush(net, 0, false, true);
  2742. WARN_ON(!list_empty(&net->xfrm.state_all));
  2743. sz = (net->xfrm.state_hmask + 1) * sizeof(struct hlist_head);
  2744. WARN_ON(!hlist_empty(net->xfrm.state_byseq));
  2745. xfrm_hash_free(net->xfrm.state_byseq, sz);
  2746. WARN_ON(!hlist_empty(net->xfrm.state_byspi));
  2747. xfrm_hash_free(net->xfrm.state_byspi, sz);
  2748. WARN_ON(!hlist_empty(net->xfrm.state_bysrc));
  2749. xfrm_hash_free(net->xfrm.state_bysrc, sz);
  2750. WARN_ON(!hlist_empty(net->xfrm.state_bydst));
  2751. xfrm_hash_free(net->xfrm.state_bydst, sz);
  2752. free_percpu(net->xfrm.state_cache_input);
  2753. }
  2754. #ifdef CONFIG_AUDITSYSCALL
  2755. static void xfrm_audit_helper_sainfo(struct xfrm_state *x,
  2756. struct audit_buffer *audit_buf)
  2757. {
  2758. struct xfrm_sec_ctx *ctx = x->security;
  2759. u32 spi = ntohl(x->id.spi);
  2760. if (ctx)
  2761. audit_log_format(audit_buf, " sec_alg=%u sec_doi=%u sec_obj=%s",
  2762. ctx->ctx_alg, ctx->ctx_doi, ctx->ctx_str);
  2763. switch (x->props.family) {
  2764. case AF_INET:
  2765. audit_log_format(audit_buf, " src=%pI4 dst=%pI4",
  2766. &x->props.saddr.a4, &x->id.daddr.a4);
  2767. break;
  2768. case AF_INET6:
  2769. audit_log_format(audit_buf, " src=%pI6 dst=%pI6",
  2770. x->props.saddr.a6, x->id.daddr.a6);
  2771. break;
  2772. }
  2773. audit_log_format(audit_buf, " spi=%u(0x%x)", spi, spi);
  2774. }
  2775. static void xfrm_audit_helper_pktinfo(struct sk_buff *skb, u16 family,
  2776. struct audit_buffer *audit_buf)
  2777. {
  2778. const struct iphdr *iph4;
  2779. const struct ipv6hdr *iph6;
  2780. switch (family) {
  2781. case AF_INET:
  2782. iph4 = ip_hdr(skb);
  2783. audit_log_format(audit_buf, " src=%pI4 dst=%pI4",
  2784. &iph4->saddr, &iph4->daddr);
  2785. break;
  2786. case AF_INET6:
  2787. iph6 = ipv6_hdr(skb);
  2788. audit_log_format(audit_buf,
  2789. " src=%pI6 dst=%pI6 flowlbl=0x%x%02x%02x",
  2790. &iph6->saddr, &iph6->daddr,
  2791. iph6->flow_lbl[0] & 0x0f,
  2792. iph6->flow_lbl[1],
  2793. iph6->flow_lbl[2]);
  2794. break;
  2795. }
  2796. }
  2797. void xfrm_audit_state_add(struct xfrm_state *x, int result, bool task_valid)
  2798. {
  2799. struct audit_buffer *audit_buf;
  2800. audit_buf = xfrm_audit_start("SAD-add");
  2801. if (audit_buf == NULL)
  2802. return;
  2803. xfrm_audit_helper_usrinfo(task_valid, audit_buf);
  2804. xfrm_audit_helper_sainfo(x, audit_buf);
  2805. audit_log_format(audit_buf, " res=%u", result);
  2806. audit_log_end(audit_buf);
  2807. }
  2808. EXPORT_SYMBOL_GPL(xfrm_audit_state_add);
  2809. void xfrm_audit_state_delete(struct xfrm_state *x, int result, bool task_valid)
  2810. {
  2811. struct audit_buffer *audit_buf;
  2812. audit_buf = xfrm_audit_start("SAD-delete");
  2813. if (audit_buf == NULL)
  2814. return;
  2815. xfrm_audit_helper_usrinfo(task_valid, audit_buf);
  2816. xfrm_audit_helper_sainfo(x, audit_buf);
  2817. audit_log_format(audit_buf, " res=%u", result);
  2818. audit_log_end(audit_buf);
  2819. }
  2820. EXPORT_SYMBOL_GPL(xfrm_audit_state_delete);
  2821. void xfrm_audit_state_replay_overflow(struct xfrm_state *x,
  2822. struct sk_buff *skb)
  2823. {
  2824. struct audit_buffer *audit_buf;
  2825. u32 spi;
  2826. audit_buf = xfrm_audit_start("SA-replay-overflow");
  2827. if (audit_buf == NULL)
  2828. return;
  2829. xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
  2830. /* don't record the sequence number because it's inherent in this kind
  2831. * of audit message */
  2832. spi = ntohl(x->id.spi);
  2833. audit_log_format(audit_buf, " spi=%u(0x%x)", spi, spi);
  2834. audit_log_end(audit_buf);
  2835. }
  2836. EXPORT_SYMBOL_GPL(xfrm_audit_state_replay_overflow);
  2837. void xfrm_audit_state_replay(struct xfrm_state *x,
  2838. struct sk_buff *skb, __be32 net_seq)
  2839. {
  2840. struct audit_buffer *audit_buf;
  2841. u32 spi;
  2842. audit_buf = xfrm_audit_start("SA-replayed-pkt");
  2843. if (audit_buf == NULL)
  2844. return;
  2845. xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
  2846. spi = ntohl(x->id.spi);
  2847. audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
  2848. spi, spi, ntohl(net_seq));
  2849. audit_log_end(audit_buf);
  2850. }
  2851. EXPORT_SYMBOL_GPL(xfrm_audit_state_replay);
  2852. void xfrm_audit_state_notfound_simple(struct sk_buff *skb, u16 family)
  2853. {
  2854. struct audit_buffer *audit_buf;
  2855. audit_buf = xfrm_audit_start("SA-notfound");
  2856. if (audit_buf == NULL)
  2857. return;
  2858. xfrm_audit_helper_pktinfo(skb, family, audit_buf);
  2859. audit_log_end(audit_buf);
  2860. }
  2861. EXPORT_SYMBOL_GPL(xfrm_audit_state_notfound_simple);
  2862. void xfrm_audit_state_notfound(struct sk_buff *skb, u16 family,
  2863. __be32 net_spi, __be32 net_seq)
  2864. {
  2865. struct audit_buffer *audit_buf;
  2866. u32 spi;
  2867. audit_buf = xfrm_audit_start("SA-notfound");
  2868. if (audit_buf == NULL)
  2869. return;
  2870. xfrm_audit_helper_pktinfo(skb, family, audit_buf);
  2871. spi = ntohl(net_spi);
  2872. audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
  2873. spi, spi, ntohl(net_seq));
  2874. audit_log_end(audit_buf);
  2875. }
  2876. EXPORT_SYMBOL_GPL(xfrm_audit_state_notfound);
  2877. void xfrm_audit_state_icvfail(struct xfrm_state *x,
  2878. struct sk_buff *skb, u8 proto)
  2879. {
  2880. struct audit_buffer *audit_buf;
  2881. __be32 net_spi;
  2882. __be32 net_seq;
  2883. audit_buf = xfrm_audit_start("SA-icv-failure");
  2884. if (audit_buf == NULL)
  2885. return;
  2886. xfrm_audit_helper_pktinfo(skb, x->props.family, audit_buf);
  2887. if (xfrm_parse_spi(skb, proto, &net_spi, &net_seq) == 0) {
  2888. u32 spi = ntohl(net_spi);
  2889. audit_log_format(audit_buf, " spi=%u(0x%x) seqno=%u",
  2890. spi, spi, ntohl(net_seq));
  2891. }
  2892. audit_log_end(audit_buf);
  2893. }
  2894. EXPORT_SYMBOL_GPL(xfrm_audit_state_icvfail);
  2895. #endif /* CONFIG_AUDITSYSCALL */