af_key.c 103 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953
  1. /*
  2. * net/key/af_key.c An implementation of PF_KEYv2 sockets.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version
  7. * 2 of the License, or (at your option) any later version.
  8. *
  9. * Authors: Maxim Giryaev <gem@asplinux.ru>
  10. * David S. Miller <davem@redhat.com>
  11. * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
  12. * Kunihiro Ishiguro <kunihiro@ipinfusion.com>
  13. * Kazunori MIYAZAWA / USAGI Project <miyazawa@linux-ipv6.org>
  14. * Derek Atkins <derek@ihtfp.com>
  15. */
  16. #include <linux/capability.h>
  17. #include <linux/module.h>
  18. #include <linux/kernel.h>
  19. #include <linux/socket.h>
  20. #include <linux/pfkeyv2.h>
  21. #include <linux/ipsec.h>
  22. #include <linux/skbuff.h>
  23. #include <linux/rtnetlink.h>
  24. #include <linux/in.h>
  25. #include <linux/in6.h>
  26. #include <linux/proc_fs.h>
  27. #include <linux/init.h>
  28. #include <linux/slab.h>
  29. #include <net/net_namespace.h>
  30. #include <net/netns/generic.h>
  31. #include <net/xfrm.h>
  32. #include <net/sock.h>
  33. #define _X2KEY(x) ((x) == XFRM_INF ? 0 : (x))
  34. #define _KEY2X(x) ((x) == 0 ? XFRM_INF : (x))
  35. static int pfkey_net_id __read_mostly;
  36. struct netns_pfkey {
  37. /* List of all pfkey sockets. */
  38. struct hlist_head table;
  39. atomic_t socks_nr;
  40. };
  41. static DEFINE_MUTEX(pfkey_mutex);
  42. #define DUMMY_MARK 0
  43. static const struct xfrm_mark dummy_mark = {0, 0};
  44. struct pfkey_sock {
  45. /* struct sock must be the first member of struct pfkey_sock */
  46. struct sock sk;
  47. int registered;
  48. int promisc;
  49. struct {
  50. uint8_t msg_version;
  51. uint32_t msg_portid;
  52. int (*dump)(struct pfkey_sock *sk);
  53. void (*done)(struct pfkey_sock *sk);
  54. union {
  55. struct xfrm_policy_walk policy;
  56. struct xfrm_state_walk state;
  57. } u;
  58. struct sk_buff *skb;
  59. } dump;
  60. struct mutex dump_lock;
  61. };
  62. static int parse_sockaddr_pair(struct sockaddr *sa, int ext_len,
  63. xfrm_address_t *saddr, xfrm_address_t *daddr,
  64. u16 *family);
  65. static inline struct pfkey_sock *pfkey_sk(struct sock *sk)
  66. {
  67. return (struct pfkey_sock *)sk;
  68. }
  69. static int pfkey_can_dump(const struct sock *sk)
  70. {
  71. if (3 * atomic_read(&sk->sk_rmem_alloc) <= 2 * sk->sk_rcvbuf)
  72. return 1;
  73. return 0;
  74. }
  75. static void pfkey_terminate_dump(struct pfkey_sock *pfk)
  76. {
  77. if (pfk->dump.dump) {
  78. if (pfk->dump.skb) {
  79. kfree_skb(pfk->dump.skb);
  80. pfk->dump.skb = NULL;
  81. }
  82. pfk->dump.done(pfk);
  83. pfk->dump.dump = NULL;
  84. pfk->dump.done = NULL;
  85. }
  86. }
  87. static void pfkey_sock_destruct(struct sock *sk)
  88. {
  89. struct net *net = sock_net(sk);
  90. struct netns_pfkey *net_pfkey = net_generic(net, pfkey_net_id);
  91. pfkey_terminate_dump(pfkey_sk(sk));
  92. skb_queue_purge(&sk->sk_receive_queue);
  93. if (!sock_flag(sk, SOCK_DEAD)) {
  94. pr_err("Attempt to release alive pfkey socket: %p\n", sk);
  95. return;
  96. }
  97. WARN_ON(atomic_read(&sk->sk_rmem_alloc));
  98. WARN_ON(atomic_read(&sk->sk_wmem_alloc));
  99. atomic_dec(&net_pfkey->socks_nr);
  100. }
  101. static const struct proto_ops pfkey_ops;
  102. static void pfkey_insert(struct sock *sk)
  103. {
  104. struct net *net = sock_net(sk);
  105. struct netns_pfkey *net_pfkey = net_generic(net, pfkey_net_id);
  106. mutex_lock(&pfkey_mutex);
  107. sk_add_node_rcu(sk, &net_pfkey->table);
  108. mutex_unlock(&pfkey_mutex);
  109. }
  110. static void pfkey_remove(struct sock *sk)
  111. {
  112. mutex_lock(&pfkey_mutex);
  113. sk_del_node_init_rcu(sk);
  114. mutex_unlock(&pfkey_mutex);
  115. }
  116. static struct proto key_proto = {
  117. .name = "KEY",
  118. .owner = THIS_MODULE,
  119. .obj_size = sizeof(struct pfkey_sock),
  120. };
  121. static int pfkey_create(struct net *net, struct socket *sock, int protocol,
  122. int kern)
  123. {
  124. struct netns_pfkey *net_pfkey = net_generic(net, pfkey_net_id);
  125. struct sock *sk;
  126. struct pfkey_sock *pfk;
  127. int err;
  128. if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
  129. return -EPERM;
  130. if (sock->type != SOCK_RAW)
  131. return -ESOCKTNOSUPPORT;
  132. if (protocol != PF_KEY_V2)
  133. return -EPROTONOSUPPORT;
  134. err = -ENOMEM;
  135. sk = sk_alloc(net, PF_KEY, GFP_KERNEL, &key_proto, kern);
  136. if (sk == NULL)
  137. goto out;
  138. pfk = pfkey_sk(sk);
  139. mutex_init(&pfk->dump_lock);
  140. sock->ops = &pfkey_ops;
  141. sock_init_data(sock, sk);
  142. sk->sk_family = PF_KEY;
  143. sk->sk_destruct = pfkey_sock_destruct;
  144. atomic_inc(&net_pfkey->socks_nr);
  145. pfkey_insert(sk);
  146. return 0;
  147. out:
  148. return err;
  149. }
  150. static int pfkey_release(struct socket *sock)
  151. {
  152. struct sock *sk = sock->sk;
  153. if (!sk)
  154. return 0;
  155. pfkey_remove(sk);
  156. sock_orphan(sk);
  157. sock->sk = NULL;
  158. skb_queue_purge(&sk->sk_write_queue);
  159. synchronize_rcu();
  160. sock_put(sk);
  161. return 0;
  162. }
  163. static int pfkey_broadcast_one(struct sk_buff *skb, struct sk_buff **skb2,
  164. gfp_t allocation, struct sock *sk)
  165. {
  166. int err = -ENOBUFS;
  167. sock_hold(sk);
  168. if (*skb2 == NULL) {
  169. if (atomic_read(&skb->users) != 1) {
  170. *skb2 = skb_clone(skb, allocation);
  171. } else {
  172. *skb2 = skb;
  173. atomic_inc(&skb->users);
  174. }
  175. }
  176. if (*skb2 != NULL) {
  177. if (atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf) {
  178. skb_set_owner_r(*skb2, sk);
  179. skb_queue_tail(&sk->sk_receive_queue, *skb2);
  180. sk->sk_data_ready(sk);
  181. *skb2 = NULL;
  182. err = 0;
  183. }
  184. }
  185. sock_put(sk);
  186. return err;
  187. }
  188. /* Send SKB to all pfkey sockets matching selected criteria. */
  189. #define BROADCAST_ALL 0
  190. #define BROADCAST_ONE 1
  191. #define BROADCAST_REGISTERED 2
  192. #define BROADCAST_PROMISC_ONLY 4
  193. static int pfkey_broadcast(struct sk_buff *skb, gfp_t allocation,
  194. int broadcast_flags, struct sock *one_sk,
  195. struct net *net)
  196. {
  197. struct netns_pfkey *net_pfkey = net_generic(net, pfkey_net_id);
  198. struct sock *sk;
  199. struct sk_buff *skb2 = NULL;
  200. int err = -ESRCH;
  201. /* XXX Do we need something like netlink_overrun? I think
  202. * XXX PF_KEY socket apps will not mind current behavior.
  203. */
  204. if (!skb)
  205. return -ENOMEM;
  206. rcu_read_lock();
  207. sk_for_each_rcu(sk, &net_pfkey->table) {
  208. struct pfkey_sock *pfk = pfkey_sk(sk);
  209. int err2;
  210. /* Yes, it means that if you are meant to receive this
  211. * pfkey message you receive it twice as promiscuous
  212. * socket.
  213. */
  214. if (pfk->promisc)
  215. pfkey_broadcast_one(skb, &skb2, GFP_ATOMIC, sk);
  216. /* the exact target will be processed later */
  217. if (sk == one_sk)
  218. continue;
  219. if (broadcast_flags != BROADCAST_ALL) {
  220. if (broadcast_flags & BROADCAST_PROMISC_ONLY)
  221. continue;
  222. if ((broadcast_flags & BROADCAST_REGISTERED) &&
  223. !pfk->registered)
  224. continue;
  225. if (broadcast_flags & BROADCAST_ONE)
  226. continue;
  227. }
  228. err2 = pfkey_broadcast_one(skb, &skb2, GFP_ATOMIC, sk);
  229. /* Error is cleared after successful sending to at least one
  230. * registered KM */
  231. if ((broadcast_flags & BROADCAST_REGISTERED) && err)
  232. err = err2;
  233. }
  234. rcu_read_unlock();
  235. if (one_sk != NULL)
  236. err = pfkey_broadcast_one(skb, &skb2, allocation, one_sk);
  237. kfree_skb(skb2);
  238. kfree_skb(skb);
  239. return err;
  240. }
  241. static int pfkey_do_dump(struct pfkey_sock *pfk)
  242. {
  243. struct sadb_msg *hdr;
  244. int rc;
  245. mutex_lock(&pfk->dump_lock);
  246. if (!pfk->dump.dump) {
  247. rc = 0;
  248. goto out;
  249. }
  250. rc = pfk->dump.dump(pfk);
  251. if (rc == -ENOBUFS) {
  252. rc = 0;
  253. goto out;
  254. }
  255. if (pfk->dump.skb) {
  256. if (!pfkey_can_dump(&pfk->sk)) {
  257. rc = 0;
  258. goto out;
  259. }
  260. hdr = (struct sadb_msg *) pfk->dump.skb->data;
  261. hdr->sadb_msg_seq = 0;
  262. hdr->sadb_msg_errno = rc;
  263. pfkey_broadcast(pfk->dump.skb, GFP_ATOMIC, BROADCAST_ONE,
  264. &pfk->sk, sock_net(&pfk->sk));
  265. pfk->dump.skb = NULL;
  266. }
  267. pfkey_terminate_dump(pfk);
  268. out:
  269. mutex_unlock(&pfk->dump_lock);
  270. return rc;
  271. }
  272. static inline void pfkey_hdr_dup(struct sadb_msg *new,
  273. const struct sadb_msg *orig)
  274. {
  275. *new = *orig;
  276. }
  277. static int pfkey_error(const struct sadb_msg *orig, int err, struct sock *sk)
  278. {
  279. struct sk_buff *skb = alloc_skb(sizeof(struct sadb_msg) + 16, GFP_KERNEL);
  280. struct sadb_msg *hdr;
  281. if (!skb)
  282. return -ENOBUFS;
  283. /* Woe be to the platform trying to support PFKEY yet
  284. * having normal errnos outside the 1-255 range, inclusive.
  285. */
  286. err = -err;
  287. if (err == ERESTARTSYS ||
  288. err == ERESTARTNOHAND ||
  289. err == ERESTARTNOINTR)
  290. err = EINTR;
  291. if (err >= 512)
  292. err = EINVAL;
  293. BUG_ON(err <= 0 || err >= 256);
  294. hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
  295. pfkey_hdr_dup(hdr, orig);
  296. hdr->sadb_msg_errno = (uint8_t) err;
  297. hdr->sadb_msg_len = (sizeof(struct sadb_msg) /
  298. sizeof(uint64_t));
  299. pfkey_broadcast(skb, GFP_KERNEL, BROADCAST_ONE, sk, sock_net(sk));
  300. return 0;
  301. }
  302. static const u8 sadb_ext_min_len[] = {
  303. [SADB_EXT_RESERVED] = (u8) 0,
  304. [SADB_EXT_SA] = (u8) sizeof(struct sadb_sa),
  305. [SADB_EXT_LIFETIME_CURRENT] = (u8) sizeof(struct sadb_lifetime),
  306. [SADB_EXT_LIFETIME_HARD] = (u8) sizeof(struct sadb_lifetime),
  307. [SADB_EXT_LIFETIME_SOFT] = (u8) sizeof(struct sadb_lifetime),
  308. [SADB_EXT_ADDRESS_SRC] = (u8) sizeof(struct sadb_address),
  309. [SADB_EXT_ADDRESS_DST] = (u8) sizeof(struct sadb_address),
  310. [SADB_EXT_ADDRESS_PROXY] = (u8) sizeof(struct sadb_address),
  311. [SADB_EXT_KEY_AUTH] = (u8) sizeof(struct sadb_key),
  312. [SADB_EXT_KEY_ENCRYPT] = (u8) sizeof(struct sadb_key),
  313. [SADB_EXT_IDENTITY_SRC] = (u8) sizeof(struct sadb_ident),
  314. [SADB_EXT_IDENTITY_DST] = (u8) sizeof(struct sadb_ident),
  315. [SADB_EXT_SENSITIVITY] = (u8) sizeof(struct sadb_sens),
  316. [SADB_EXT_PROPOSAL] = (u8) sizeof(struct sadb_prop),
  317. [SADB_EXT_SUPPORTED_AUTH] = (u8) sizeof(struct sadb_supported),
  318. [SADB_EXT_SUPPORTED_ENCRYPT] = (u8) sizeof(struct sadb_supported),
  319. [SADB_EXT_SPIRANGE] = (u8) sizeof(struct sadb_spirange),
  320. [SADB_X_EXT_KMPRIVATE] = (u8) sizeof(struct sadb_x_kmprivate),
  321. [SADB_X_EXT_POLICY] = (u8) sizeof(struct sadb_x_policy),
  322. [SADB_X_EXT_SA2] = (u8) sizeof(struct sadb_x_sa2),
  323. [SADB_X_EXT_NAT_T_TYPE] = (u8) sizeof(struct sadb_x_nat_t_type),
  324. [SADB_X_EXT_NAT_T_SPORT] = (u8) sizeof(struct sadb_x_nat_t_port),
  325. [SADB_X_EXT_NAT_T_DPORT] = (u8) sizeof(struct sadb_x_nat_t_port),
  326. [SADB_X_EXT_NAT_T_OA] = (u8) sizeof(struct sadb_address),
  327. [SADB_X_EXT_SEC_CTX] = (u8) sizeof(struct sadb_x_sec_ctx),
  328. [SADB_X_EXT_KMADDRESS] = (u8) sizeof(struct sadb_x_kmaddress),
  329. [SADB_X_EXT_FILTER] = (u8) sizeof(struct sadb_x_filter),
  330. };
  331. /* Verify sadb_address_{len,prefixlen} against sa_family. */
  332. static int verify_address_len(const void *p)
  333. {
  334. const struct sadb_address *sp = p;
  335. const struct sockaddr *addr = (const struct sockaddr *)(sp + 1);
  336. const struct sockaddr_in *sin;
  337. #if IS_ENABLED(CONFIG_IPV6)
  338. const struct sockaddr_in6 *sin6;
  339. #endif
  340. int len;
  341. if (sp->sadb_address_len <
  342. DIV_ROUND_UP(sizeof(*sp) + offsetofend(typeof(*addr), sa_family),
  343. sizeof(uint64_t)))
  344. return -EINVAL;
  345. switch (addr->sa_family) {
  346. case AF_INET:
  347. len = DIV_ROUND_UP(sizeof(*sp) + sizeof(*sin), sizeof(uint64_t));
  348. if (sp->sadb_address_len != len ||
  349. sp->sadb_address_prefixlen > 32)
  350. return -EINVAL;
  351. break;
  352. #if IS_ENABLED(CONFIG_IPV6)
  353. case AF_INET6:
  354. len = DIV_ROUND_UP(sizeof(*sp) + sizeof(*sin6), sizeof(uint64_t));
  355. if (sp->sadb_address_len != len ||
  356. sp->sadb_address_prefixlen > 128)
  357. return -EINVAL;
  358. break;
  359. #endif
  360. default:
  361. /* It is user using kernel to keep track of security
  362. * associations for another protocol, such as
  363. * OSPF/RSVP/RIPV2/MIP. It is user's job to verify
  364. * lengths.
  365. *
  366. * XXX Actually, association/policy database is not yet
  367. * XXX able to cope with arbitrary sockaddr families.
  368. * XXX When it can, remove this -EINVAL. -DaveM
  369. */
  370. return -EINVAL;
  371. }
  372. return 0;
  373. }
  374. static inline int sadb_key_len(const struct sadb_key *key)
  375. {
  376. int key_bytes = DIV_ROUND_UP(key->sadb_key_bits, 8);
  377. return DIV_ROUND_UP(sizeof(struct sadb_key) + key_bytes,
  378. sizeof(uint64_t));
  379. }
  380. static int verify_key_len(const void *p)
  381. {
  382. const struct sadb_key *key = p;
  383. if (sadb_key_len(key) > key->sadb_key_len)
  384. return -EINVAL;
  385. return 0;
  386. }
  387. static inline int pfkey_sec_ctx_len(const struct sadb_x_sec_ctx *sec_ctx)
  388. {
  389. return DIV_ROUND_UP(sizeof(struct sadb_x_sec_ctx) +
  390. sec_ctx->sadb_x_ctx_len,
  391. sizeof(uint64_t));
  392. }
  393. static inline int verify_sec_ctx_len(const void *p)
  394. {
  395. const struct sadb_x_sec_ctx *sec_ctx = p;
  396. int len = sec_ctx->sadb_x_ctx_len;
  397. if (len > PAGE_SIZE)
  398. return -EINVAL;
  399. len = pfkey_sec_ctx_len(sec_ctx);
  400. if (sec_ctx->sadb_x_sec_len != len)
  401. return -EINVAL;
  402. return 0;
  403. }
  404. static inline struct xfrm_user_sec_ctx *pfkey_sadb2xfrm_user_sec_ctx(const struct sadb_x_sec_ctx *sec_ctx,
  405. gfp_t gfp)
  406. {
  407. struct xfrm_user_sec_ctx *uctx = NULL;
  408. int ctx_size = sec_ctx->sadb_x_ctx_len;
  409. uctx = kmalloc((sizeof(*uctx)+ctx_size), gfp);
  410. if (!uctx)
  411. return NULL;
  412. uctx->len = pfkey_sec_ctx_len(sec_ctx);
  413. uctx->exttype = sec_ctx->sadb_x_sec_exttype;
  414. uctx->ctx_doi = sec_ctx->sadb_x_ctx_doi;
  415. uctx->ctx_alg = sec_ctx->sadb_x_ctx_alg;
  416. uctx->ctx_len = sec_ctx->sadb_x_ctx_len;
  417. memcpy(uctx + 1, sec_ctx + 1,
  418. uctx->ctx_len);
  419. return uctx;
  420. }
  421. static int present_and_same_family(const struct sadb_address *src,
  422. const struct sadb_address *dst)
  423. {
  424. const struct sockaddr *s_addr, *d_addr;
  425. if (!src || !dst)
  426. return 0;
  427. s_addr = (const struct sockaddr *)(src + 1);
  428. d_addr = (const struct sockaddr *)(dst + 1);
  429. if (s_addr->sa_family != d_addr->sa_family)
  430. return 0;
  431. if (s_addr->sa_family != AF_INET
  432. #if IS_ENABLED(CONFIG_IPV6)
  433. && s_addr->sa_family != AF_INET6
  434. #endif
  435. )
  436. return 0;
  437. return 1;
  438. }
  439. static int parse_exthdrs(struct sk_buff *skb, const struct sadb_msg *hdr, void **ext_hdrs)
  440. {
  441. const char *p = (char *) hdr;
  442. int len = skb->len;
  443. len -= sizeof(*hdr);
  444. p += sizeof(*hdr);
  445. while (len > 0) {
  446. const struct sadb_ext *ehdr = (const struct sadb_ext *) p;
  447. uint16_t ext_type;
  448. int ext_len;
  449. if (len < sizeof(*ehdr))
  450. return -EINVAL;
  451. ext_len = ehdr->sadb_ext_len;
  452. ext_len *= sizeof(uint64_t);
  453. ext_type = ehdr->sadb_ext_type;
  454. if (ext_len < sizeof(uint64_t) ||
  455. ext_len > len ||
  456. ext_type == SADB_EXT_RESERVED)
  457. return -EINVAL;
  458. if (ext_type <= SADB_EXT_MAX) {
  459. int min = (int) sadb_ext_min_len[ext_type];
  460. if (ext_len < min)
  461. return -EINVAL;
  462. if (ext_hdrs[ext_type-1] != NULL)
  463. return -EINVAL;
  464. switch (ext_type) {
  465. case SADB_EXT_ADDRESS_SRC:
  466. case SADB_EXT_ADDRESS_DST:
  467. case SADB_EXT_ADDRESS_PROXY:
  468. case SADB_X_EXT_NAT_T_OA:
  469. if (verify_address_len(p))
  470. return -EINVAL;
  471. break;
  472. case SADB_X_EXT_SEC_CTX:
  473. if (verify_sec_ctx_len(p))
  474. return -EINVAL;
  475. break;
  476. case SADB_EXT_KEY_AUTH:
  477. case SADB_EXT_KEY_ENCRYPT:
  478. if (verify_key_len(p))
  479. return -EINVAL;
  480. break;
  481. default:
  482. break;
  483. }
  484. ext_hdrs[ext_type-1] = (void *) p;
  485. }
  486. p += ext_len;
  487. len -= ext_len;
  488. }
  489. return 0;
  490. }
  491. static uint16_t
  492. pfkey_satype2proto(uint8_t satype)
  493. {
  494. switch (satype) {
  495. case SADB_SATYPE_UNSPEC:
  496. return IPSEC_PROTO_ANY;
  497. case SADB_SATYPE_AH:
  498. return IPPROTO_AH;
  499. case SADB_SATYPE_ESP:
  500. return IPPROTO_ESP;
  501. case SADB_X_SATYPE_IPCOMP:
  502. return IPPROTO_COMP;
  503. default:
  504. return 0;
  505. }
  506. /* NOTREACHED */
  507. }
  508. static uint8_t
  509. pfkey_proto2satype(uint16_t proto)
  510. {
  511. switch (proto) {
  512. case IPPROTO_AH:
  513. return SADB_SATYPE_AH;
  514. case IPPROTO_ESP:
  515. return SADB_SATYPE_ESP;
  516. case IPPROTO_COMP:
  517. return SADB_X_SATYPE_IPCOMP;
  518. default:
  519. return 0;
  520. }
  521. /* NOTREACHED */
  522. }
  523. /* BTW, this scheme means that there is no way with PFKEY2 sockets to
  524. * say specifically 'just raw sockets' as we encode them as 255.
  525. */
  526. static uint8_t pfkey_proto_to_xfrm(uint8_t proto)
  527. {
  528. return proto == IPSEC_PROTO_ANY ? 0 : proto;
  529. }
  530. static uint8_t pfkey_proto_from_xfrm(uint8_t proto)
  531. {
  532. return proto ? proto : IPSEC_PROTO_ANY;
  533. }
  534. static inline int pfkey_sockaddr_len(sa_family_t family)
  535. {
  536. switch (family) {
  537. case AF_INET:
  538. return sizeof(struct sockaddr_in);
  539. #if IS_ENABLED(CONFIG_IPV6)
  540. case AF_INET6:
  541. return sizeof(struct sockaddr_in6);
  542. #endif
  543. }
  544. return 0;
  545. }
  546. static
  547. int pfkey_sockaddr_extract(const struct sockaddr *sa, xfrm_address_t *xaddr)
  548. {
  549. switch (sa->sa_family) {
  550. case AF_INET:
  551. xaddr->a4 =
  552. ((struct sockaddr_in *)sa)->sin_addr.s_addr;
  553. return AF_INET;
  554. #if IS_ENABLED(CONFIG_IPV6)
  555. case AF_INET6:
  556. memcpy(xaddr->a6,
  557. &((struct sockaddr_in6 *)sa)->sin6_addr,
  558. sizeof(struct in6_addr));
  559. return AF_INET6;
  560. #endif
  561. }
  562. return 0;
  563. }
  564. static
  565. int pfkey_sadb_addr2xfrm_addr(const struct sadb_address *addr, xfrm_address_t *xaddr)
  566. {
  567. return pfkey_sockaddr_extract((struct sockaddr *)(addr + 1),
  568. xaddr);
  569. }
  570. static struct xfrm_state *pfkey_xfrm_state_lookup(struct net *net, const struct sadb_msg *hdr, void * const *ext_hdrs)
  571. {
  572. const struct sadb_sa *sa;
  573. const struct sadb_address *addr;
  574. uint16_t proto;
  575. unsigned short family;
  576. xfrm_address_t *xaddr;
  577. sa = ext_hdrs[SADB_EXT_SA - 1];
  578. if (sa == NULL)
  579. return NULL;
  580. proto = pfkey_satype2proto(hdr->sadb_msg_satype);
  581. if (proto == 0)
  582. return NULL;
  583. /* sadb_address_len should be checked by caller */
  584. addr = ext_hdrs[SADB_EXT_ADDRESS_DST - 1];
  585. if (addr == NULL)
  586. return NULL;
  587. family = ((const struct sockaddr *)(addr + 1))->sa_family;
  588. switch (family) {
  589. case AF_INET:
  590. xaddr = (xfrm_address_t *)&((const struct sockaddr_in *)(addr + 1))->sin_addr;
  591. break;
  592. #if IS_ENABLED(CONFIG_IPV6)
  593. case AF_INET6:
  594. xaddr = (xfrm_address_t *)&((const struct sockaddr_in6 *)(addr + 1))->sin6_addr;
  595. break;
  596. #endif
  597. default:
  598. xaddr = NULL;
  599. }
  600. if (!xaddr)
  601. return NULL;
  602. return xfrm_state_lookup(net, DUMMY_MARK, xaddr, sa->sadb_sa_spi, proto, family);
  603. }
  604. #define PFKEY_ALIGN8(a) (1 + (((a) - 1) | (8 - 1)))
  605. static int
  606. pfkey_sockaddr_size(sa_family_t family)
  607. {
  608. return PFKEY_ALIGN8(pfkey_sockaddr_len(family));
  609. }
  610. static inline int pfkey_mode_from_xfrm(int mode)
  611. {
  612. switch(mode) {
  613. case XFRM_MODE_TRANSPORT:
  614. return IPSEC_MODE_TRANSPORT;
  615. case XFRM_MODE_TUNNEL:
  616. return IPSEC_MODE_TUNNEL;
  617. case XFRM_MODE_BEET:
  618. return IPSEC_MODE_BEET;
  619. default:
  620. return -1;
  621. }
  622. }
  623. static inline int pfkey_mode_to_xfrm(int mode)
  624. {
  625. switch(mode) {
  626. case IPSEC_MODE_ANY: /*XXX*/
  627. case IPSEC_MODE_TRANSPORT:
  628. return XFRM_MODE_TRANSPORT;
  629. case IPSEC_MODE_TUNNEL:
  630. return XFRM_MODE_TUNNEL;
  631. case IPSEC_MODE_BEET:
  632. return XFRM_MODE_BEET;
  633. default:
  634. return -1;
  635. }
  636. }
  637. static unsigned int pfkey_sockaddr_fill(const xfrm_address_t *xaddr, __be16 port,
  638. struct sockaddr *sa,
  639. unsigned short family)
  640. {
  641. switch (family) {
  642. case AF_INET:
  643. {
  644. struct sockaddr_in *sin = (struct sockaddr_in *)sa;
  645. sin->sin_family = AF_INET;
  646. sin->sin_port = port;
  647. sin->sin_addr.s_addr = xaddr->a4;
  648. memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
  649. return 32;
  650. }
  651. #if IS_ENABLED(CONFIG_IPV6)
  652. case AF_INET6:
  653. {
  654. struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sa;
  655. sin6->sin6_family = AF_INET6;
  656. sin6->sin6_port = port;
  657. sin6->sin6_flowinfo = 0;
  658. sin6->sin6_addr = xaddr->in6;
  659. sin6->sin6_scope_id = 0;
  660. return 128;
  661. }
  662. #endif
  663. }
  664. return 0;
  665. }
  666. static struct sk_buff *__pfkey_xfrm_state2msg(const struct xfrm_state *x,
  667. int add_keys, int hsc)
  668. {
  669. struct sk_buff *skb;
  670. struct sadb_msg *hdr;
  671. struct sadb_sa *sa;
  672. struct sadb_lifetime *lifetime;
  673. struct sadb_address *addr;
  674. struct sadb_key *key;
  675. struct sadb_x_sa2 *sa2;
  676. struct sadb_x_sec_ctx *sec_ctx;
  677. struct xfrm_sec_ctx *xfrm_ctx;
  678. int ctx_size = 0;
  679. int size;
  680. int auth_key_size = 0;
  681. int encrypt_key_size = 0;
  682. int sockaddr_size;
  683. struct xfrm_encap_tmpl *natt = NULL;
  684. int mode;
  685. /* address family check */
  686. sockaddr_size = pfkey_sockaddr_size(x->props.family);
  687. if (!sockaddr_size)
  688. return ERR_PTR(-EINVAL);
  689. /* base, SA, (lifetime (HSC),) address(SD), (address(P),)
  690. key(AE), (identity(SD),) (sensitivity)> */
  691. size = sizeof(struct sadb_msg) +sizeof(struct sadb_sa) +
  692. sizeof(struct sadb_lifetime) +
  693. ((hsc & 1) ? sizeof(struct sadb_lifetime) : 0) +
  694. ((hsc & 2) ? sizeof(struct sadb_lifetime) : 0) +
  695. sizeof(struct sadb_address)*2 +
  696. sockaddr_size*2 +
  697. sizeof(struct sadb_x_sa2);
  698. if ((xfrm_ctx = x->security)) {
  699. ctx_size = PFKEY_ALIGN8(xfrm_ctx->ctx_len);
  700. size += sizeof(struct sadb_x_sec_ctx) + ctx_size;
  701. }
  702. /* identity & sensitivity */
  703. if (!xfrm_addr_equal(&x->sel.saddr, &x->props.saddr, x->props.family))
  704. size += sizeof(struct sadb_address) + sockaddr_size;
  705. if (add_keys) {
  706. if (x->aalg && x->aalg->alg_key_len) {
  707. auth_key_size =
  708. PFKEY_ALIGN8((x->aalg->alg_key_len + 7) / 8);
  709. size += sizeof(struct sadb_key) + auth_key_size;
  710. }
  711. if (x->ealg && x->ealg->alg_key_len) {
  712. encrypt_key_size =
  713. PFKEY_ALIGN8((x->ealg->alg_key_len+7) / 8);
  714. size += sizeof(struct sadb_key) + encrypt_key_size;
  715. }
  716. }
  717. if (x->encap)
  718. natt = x->encap;
  719. if (natt && natt->encap_type) {
  720. size += sizeof(struct sadb_x_nat_t_type);
  721. size += sizeof(struct sadb_x_nat_t_port);
  722. size += sizeof(struct sadb_x_nat_t_port);
  723. }
  724. skb = alloc_skb(size + 16, GFP_ATOMIC);
  725. if (skb == NULL)
  726. return ERR_PTR(-ENOBUFS);
  727. /* call should fill header later */
  728. hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
  729. memset(hdr, 0, size); /* XXX do we need this ? */
  730. hdr->sadb_msg_len = size / sizeof(uint64_t);
  731. /* sa */
  732. sa = (struct sadb_sa *) skb_put(skb, sizeof(struct sadb_sa));
  733. sa->sadb_sa_len = sizeof(struct sadb_sa)/sizeof(uint64_t);
  734. sa->sadb_sa_exttype = SADB_EXT_SA;
  735. sa->sadb_sa_spi = x->id.spi;
  736. sa->sadb_sa_replay = x->props.replay_window;
  737. switch (x->km.state) {
  738. case XFRM_STATE_VALID:
  739. sa->sadb_sa_state = x->km.dying ?
  740. SADB_SASTATE_DYING : SADB_SASTATE_MATURE;
  741. break;
  742. case XFRM_STATE_ACQ:
  743. sa->sadb_sa_state = SADB_SASTATE_LARVAL;
  744. break;
  745. default:
  746. sa->sadb_sa_state = SADB_SASTATE_DEAD;
  747. break;
  748. }
  749. sa->sadb_sa_auth = 0;
  750. if (x->aalg) {
  751. struct xfrm_algo_desc *a = xfrm_aalg_get_byname(x->aalg->alg_name, 0);
  752. sa->sadb_sa_auth = (a && a->pfkey_supported) ?
  753. a->desc.sadb_alg_id : 0;
  754. }
  755. sa->sadb_sa_encrypt = 0;
  756. BUG_ON(x->ealg && x->calg);
  757. if (x->ealg) {
  758. struct xfrm_algo_desc *a = xfrm_ealg_get_byname(x->ealg->alg_name, 0);
  759. sa->sadb_sa_encrypt = (a && a->pfkey_supported) ?
  760. a->desc.sadb_alg_id : 0;
  761. }
  762. /* KAME compatible: sadb_sa_encrypt is overloaded with calg id */
  763. if (x->calg) {
  764. struct xfrm_algo_desc *a = xfrm_calg_get_byname(x->calg->alg_name, 0);
  765. sa->sadb_sa_encrypt = (a && a->pfkey_supported) ?
  766. a->desc.sadb_alg_id : 0;
  767. }
  768. sa->sadb_sa_flags = 0;
  769. if (x->props.flags & XFRM_STATE_NOECN)
  770. sa->sadb_sa_flags |= SADB_SAFLAGS_NOECN;
  771. if (x->props.flags & XFRM_STATE_DECAP_DSCP)
  772. sa->sadb_sa_flags |= SADB_SAFLAGS_DECAP_DSCP;
  773. if (x->props.flags & XFRM_STATE_NOPMTUDISC)
  774. sa->sadb_sa_flags |= SADB_SAFLAGS_NOPMTUDISC;
  775. /* hard time */
  776. if (hsc & 2) {
  777. lifetime = (struct sadb_lifetime *) skb_put(skb,
  778. sizeof(struct sadb_lifetime));
  779. lifetime->sadb_lifetime_len =
  780. sizeof(struct sadb_lifetime)/sizeof(uint64_t);
  781. lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_HARD;
  782. lifetime->sadb_lifetime_allocations = _X2KEY(x->lft.hard_packet_limit);
  783. lifetime->sadb_lifetime_bytes = _X2KEY(x->lft.hard_byte_limit);
  784. lifetime->sadb_lifetime_addtime = x->lft.hard_add_expires_seconds;
  785. lifetime->sadb_lifetime_usetime = x->lft.hard_use_expires_seconds;
  786. }
  787. /* soft time */
  788. if (hsc & 1) {
  789. lifetime = (struct sadb_lifetime *) skb_put(skb,
  790. sizeof(struct sadb_lifetime));
  791. lifetime->sadb_lifetime_len =
  792. sizeof(struct sadb_lifetime)/sizeof(uint64_t);
  793. lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_SOFT;
  794. lifetime->sadb_lifetime_allocations = _X2KEY(x->lft.soft_packet_limit);
  795. lifetime->sadb_lifetime_bytes = _X2KEY(x->lft.soft_byte_limit);
  796. lifetime->sadb_lifetime_addtime = x->lft.soft_add_expires_seconds;
  797. lifetime->sadb_lifetime_usetime = x->lft.soft_use_expires_seconds;
  798. }
  799. /* current time */
  800. lifetime = (struct sadb_lifetime *) skb_put(skb,
  801. sizeof(struct sadb_lifetime));
  802. lifetime->sadb_lifetime_len =
  803. sizeof(struct sadb_lifetime)/sizeof(uint64_t);
  804. lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_CURRENT;
  805. lifetime->sadb_lifetime_allocations = x->curlft.packets;
  806. lifetime->sadb_lifetime_bytes = x->curlft.bytes;
  807. lifetime->sadb_lifetime_addtime = x->curlft.add_time;
  808. lifetime->sadb_lifetime_usetime = x->curlft.use_time;
  809. /* src address */
  810. addr = (struct sadb_address*) skb_put(skb,
  811. sizeof(struct sadb_address)+sockaddr_size);
  812. addr->sadb_address_len =
  813. (sizeof(struct sadb_address)+sockaddr_size)/
  814. sizeof(uint64_t);
  815. addr->sadb_address_exttype = SADB_EXT_ADDRESS_SRC;
  816. /* "if the ports are non-zero, then the sadb_address_proto field,
  817. normally zero, MUST be filled in with the transport
  818. protocol's number." - RFC2367 */
  819. addr->sadb_address_proto = 0;
  820. addr->sadb_address_reserved = 0;
  821. addr->sadb_address_prefixlen =
  822. pfkey_sockaddr_fill(&x->props.saddr, 0,
  823. (struct sockaddr *) (addr + 1),
  824. x->props.family);
  825. if (!addr->sadb_address_prefixlen)
  826. BUG();
  827. /* dst address */
  828. addr = (struct sadb_address*) skb_put(skb,
  829. sizeof(struct sadb_address)+sockaddr_size);
  830. addr->sadb_address_len =
  831. (sizeof(struct sadb_address)+sockaddr_size)/
  832. sizeof(uint64_t);
  833. addr->sadb_address_exttype = SADB_EXT_ADDRESS_DST;
  834. addr->sadb_address_proto = 0;
  835. addr->sadb_address_reserved = 0;
  836. addr->sadb_address_prefixlen =
  837. pfkey_sockaddr_fill(&x->id.daddr, 0,
  838. (struct sockaddr *) (addr + 1),
  839. x->props.family);
  840. if (!addr->sadb_address_prefixlen)
  841. BUG();
  842. if (!xfrm_addr_equal(&x->sel.saddr, &x->props.saddr,
  843. x->props.family)) {
  844. addr = (struct sadb_address*) skb_put(skb,
  845. sizeof(struct sadb_address)+sockaddr_size);
  846. addr->sadb_address_len =
  847. (sizeof(struct sadb_address)+sockaddr_size)/
  848. sizeof(uint64_t);
  849. addr->sadb_address_exttype = SADB_EXT_ADDRESS_PROXY;
  850. addr->sadb_address_proto =
  851. pfkey_proto_from_xfrm(x->sel.proto);
  852. addr->sadb_address_prefixlen = x->sel.prefixlen_s;
  853. addr->sadb_address_reserved = 0;
  854. pfkey_sockaddr_fill(&x->sel.saddr, x->sel.sport,
  855. (struct sockaddr *) (addr + 1),
  856. x->props.family);
  857. }
  858. /* auth key */
  859. if (add_keys && auth_key_size) {
  860. key = (struct sadb_key *) skb_put(skb,
  861. sizeof(struct sadb_key)+auth_key_size);
  862. key->sadb_key_len = (sizeof(struct sadb_key) + auth_key_size) /
  863. sizeof(uint64_t);
  864. key->sadb_key_exttype = SADB_EXT_KEY_AUTH;
  865. key->sadb_key_bits = x->aalg->alg_key_len;
  866. key->sadb_key_reserved = 0;
  867. memcpy(key + 1, x->aalg->alg_key, (x->aalg->alg_key_len+7)/8);
  868. }
  869. /* encrypt key */
  870. if (add_keys && encrypt_key_size) {
  871. key = (struct sadb_key *) skb_put(skb,
  872. sizeof(struct sadb_key)+encrypt_key_size);
  873. key->sadb_key_len = (sizeof(struct sadb_key) +
  874. encrypt_key_size) / sizeof(uint64_t);
  875. key->sadb_key_exttype = SADB_EXT_KEY_ENCRYPT;
  876. key->sadb_key_bits = x->ealg->alg_key_len;
  877. key->sadb_key_reserved = 0;
  878. memcpy(key + 1, x->ealg->alg_key,
  879. (x->ealg->alg_key_len+7)/8);
  880. }
  881. /* sa */
  882. sa2 = (struct sadb_x_sa2 *) skb_put(skb, sizeof(struct sadb_x_sa2));
  883. sa2->sadb_x_sa2_len = sizeof(struct sadb_x_sa2)/sizeof(uint64_t);
  884. sa2->sadb_x_sa2_exttype = SADB_X_EXT_SA2;
  885. if ((mode = pfkey_mode_from_xfrm(x->props.mode)) < 0) {
  886. kfree_skb(skb);
  887. return ERR_PTR(-EINVAL);
  888. }
  889. sa2->sadb_x_sa2_mode = mode;
  890. sa2->sadb_x_sa2_reserved1 = 0;
  891. sa2->sadb_x_sa2_reserved2 = 0;
  892. sa2->sadb_x_sa2_sequence = 0;
  893. sa2->sadb_x_sa2_reqid = x->props.reqid;
  894. if (natt && natt->encap_type) {
  895. struct sadb_x_nat_t_type *n_type;
  896. struct sadb_x_nat_t_port *n_port;
  897. /* type */
  898. n_type = (struct sadb_x_nat_t_type*) skb_put(skb, sizeof(*n_type));
  899. n_type->sadb_x_nat_t_type_len = sizeof(*n_type)/sizeof(uint64_t);
  900. n_type->sadb_x_nat_t_type_exttype = SADB_X_EXT_NAT_T_TYPE;
  901. n_type->sadb_x_nat_t_type_type = natt->encap_type;
  902. n_type->sadb_x_nat_t_type_reserved[0] = 0;
  903. n_type->sadb_x_nat_t_type_reserved[1] = 0;
  904. n_type->sadb_x_nat_t_type_reserved[2] = 0;
  905. /* source port */
  906. n_port = (struct sadb_x_nat_t_port*) skb_put(skb, sizeof (*n_port));
  907. n_port->sadb_x_nat_t_port_len = sizeof(*n_port)/sizeof(uint64_t);
  908. n_port->sadb_x_nat_t_port_exttype = SADB_X_EXT_NAT_T_SPORT;
  909. n_port->sadb_x_nat_t_port_port = natt->encap_sport;
  910. n_port->sadb_x_nat_t_port_reserved = 0;
  911. /* dest port */
  912. n_port = (struct sadb_x_nat_t_port*) skb_put(skb, sizeof (*n_port));
  913. n_port->sadb_x_nat_t_port_len = sizeof(*n_port)/sizeof(uint64_t);
  914. n_port->sadb_x_nat_t_port_exttype = SADB_X_EXT_NAT_T_DPORT;
  915. n_port->sadb_x_nat_t_port_port = natt->encap_dport;
  916. n_port->sadb_x_nat_t_port_reserved = 0;
  917. }
  918. /* security context */
  919. if (xfrm_ctx) {
  920. sec_ctx = (struct sadb_x_sec_ctx *) skb_put(skb,
  921. sizeof(struct sadb_x_sec_ctx) + ctx_size);
  922. sec_ctx->sadb_x_sec_len =
  923. (sizeof(struct sadb_x_sec_ctx) + ctx_size) / sizeof(uint64_t);
  924. sec_ctx->sadb_x_sec_exttype = SADB_X_EXT_SEC_CTX;
  925. sec_ctx->sadb_x_ctx_doi = xfrm_ctx->ctx_doi;
  926. sec_ctx->sadb_x_ctx_alg = xfrm_ctx->ctx_alg;
  927. sec_ctx->sadb_x_ctx_len = xfrm_ctx->ctx_len;
  928. memcpy(sec_ctx + 1, xfrm_ctx->ctx_str,
  929. xfrm_ctx->ctx_len);
  930. }
  931. return skb;
  932. }
  933. static inline struct sk_buff *pfkey_xfrm_state2msg(const struct xfrm_state *x)
  934. {
  935. struct sk_buff *skb;
  936. skb = __pfkey_xfrm_state2msg(x, 1, 3);
  937. return skb;
  938. }
  939. static inline struct sk_buff *pfkey_xfrm_state2msg_expire(const struct xfrm_state *x,
  940. int hsc)
  941. {
  942. return __pfkey_xfrm_state2msg(x, 0, hsc);
  943. }
  944. static struct xfrm_state * pfkey_msg2xfrm_state(struct net *net,
  945. const struct sadb_msg *hdr,
  946. void * const *ext_hdrs)
  947. {
  948. struct xfrm_state *x;
  949. const struct sadb_lifetime *lifetime;
  950. const struct sadb_sa *sa;
  951. const struct sadb_key *key;
  952. const struct sadb_x_sec_ctx *sec_ctx;
  953. uint16_t proto;
  954. int err;
  955. sa = ext_hdrs[SADB_EXT_SA - 1];
  956. if (!sa ||
  957. !present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
  958. ext_hdrs[SADB_EXT_ADDRESS_DST-1]))
  959. return ERR_PTR(-EINVAL);
  960. if (hdr->sadb_msg_satype == SADB_SATYPE_ESP &&
  961. !ext_hdrs[SADB_EXT_KEY_ENCRYPT-1])
  962. return ERR_PTR(-EINVAL);
  963. if (hdr->sadb_msg_satype == SADB_SATYPE_AH &&
  964. !ext_hdrs[SADB_EXT_KEY_AUTH-1])
  965. return ERR_PTR(-EINVAL);
  966. if (!!ext_hdrs[SADB_EXT_LIFETIME_HARD-1] !=
  967. !!ext_hdrs[SADB_EXT_LIFETIME_SOFT-1])
  968. return ERR_PTR(-EINVAL);
  969. proto = pfkey_satype2proto(hdr->sadb_msg_satype);
  970. if (proto == 0)
  971. return ERR_PTR(-EINVAL);
  972. /* default error is no buffer space */
  973. err = -ENOBUFS;
  974. /* RFC2367:
  975. Only SADB_SASTATE_MATURE SAs may be submitted in an SADB_ADD message.
  976. SADB_SASTATE_LARVAL SAs are created by SADB_GETSPI and it is not
  977. sensible to add a new SA in the DYING or SADB_SASTATE_DEAD state.
  978. Therefore, the sadb_sa_state field of all submitted SAs MUST be
  979. SADB_SASTATE_MATURE and the kernel MUST return an error if this is
  980. not true.
  981. However, KAME setkey always uses SADB_SASTATE_LARVAL.
  982. Hence, we have to _ignore_ sadb_sa_state, which is also reasonable.
  983. */
  984. if (sa->sadb_sa_auth > SADB_AALG_MAX ||
  985. (hdr->sadb_msg_satype == SADB_X_SATYPE_IPCOMP &&
  986. sa->sadb_sa_encrypt > SADB_X_CALG_MAX) ||
  987. sa->sadb_sa_encrypt > SADB_EALG_MAX)
  988. return ERR_PTR(-EINVAL);
  989. key = ext_hdrs[SADB_EXT_KEY_AUTH - 1];
  990. if (key != NULL &&
  991. sa->sadb_sa_auth != SADB_X_AALG_NULL &&
  992. key->sadb_key_bits == 0)
  993. return ERR_PTR(-EINVAL);
  994. key = ext_hdrs[SADB_EXT_KEY_ENCRYPT-1];
  995. if (key != NULL &&
  996. sa->sadb_sa_encrypt != SADB_EALG_NULL &&
  997. key->sadb_key_bits == 0)
  998. return ERR_PTR(-EINVAL);
  999. x = xfrm_state_alloc(net);
  1000. if (x == NULL)
  1001. return ERR_PTR(-ENOBUFS);
  1002. x->id.proto = proto;
  1003. x->id.spi = sa->sadb_sa_spi;
  1004. x->props.replay_window = min_t(unsigned int, sa->sadb_sa_replay,
  1005. (sizeof(x->replay.bitmap) * 8));
  1006. if (sa->sadb_sa_flags & SADB_SAFLAGS_NOECN)
  1007. x->props.flags |= XFRM_STATE_NOECN;
  1008. if (sa->sadb_sa_flags & SADB_SAFLAGS_DECAP_DSCP)
  1009. x->props.flags |= XFRM_STATE_DECAP_DSCP;
  1010. if (sa->sadb_sa_flags & SADB_SAFLAGS_NOPMTUDISC)
  1011. x->props.flags |= XFRM_STATE_NOPMTUDISC;
  1012. lifetime = ext_hdrs[SADB_EXT_LIFETIME_HARD - 1];
  1013. if (lifetime != NULL) {
  1014. x->lft.hard_packet_limit = _KEY2X(lifetime->sadb_lifetime_allocations);
  1015. x->lft.hard_byte_limit = _KEY2X(lifetime->sadb_lifetime_bytes);
  1016. x->lft.hard_add_expires_seconds = lifetime->sadb_lifetime_addtime;
  1017. x->lft.hard_use_expires_seconds = lifetime->sadb_lifetime_usetime;
  1018. }
  1019. lifetime = ext_hdrs[SADB_EXT_LIFETIME_SOFT - 1];
  1020. if (lifetime != NULL) {
  1021. x->lft.soft_packet_limit = _KEY2X(lifetime->sadb_lifetime_allocations);
  1022. x->lft.soft_byte_limit = _KEY2X(lifetime->sadb_lifetime_bytes);
  1023. x->lft.soft_add_expires_seconds = lifetime->sadb_lifetime_addtime;
  1024. x->lft.soft_use_expires_seconds = lifetime->sadb_lifetime_usetime;
  1025. }
  1026. sec_ctx = ext_hdrs[SADB_X_EXT_SEC_CTX - 1];
  1027. if (sec_ctx != NULL) {
  1028. struct xfrm_user_sec_ctx *uctx = pfkey_sadb2xfrm_user_sec_ctx(sec_ctx, GFP_KERNEL);
  1029. if (!uctx)
  1030. goto out;
  1031. err = security_xfrm_state_alloc(x, uctx);
  1032. kfree(uctx);
  1033. if (err)
  1034. goto out;
  1035. }
  1036. err = -ENOBUFS;
  1037. key = ext_hdrs[SADB_EXT_KEY_AUTH - 1];
  1038. if (sa->sadb_sa_auth) {
  1039. int keysize = 0;
  1040. struct xfrm_algo_desc *a = xfrm_aalg_get_byid(sa->sadb_sa_auth);
  1041. if (!a || !a->pfkey_supported) {
  1042. err = -ENOSYS;
  1043. goto out;
  1044. }
  1045. if (key)
  1046. keysize = (key->sadb_key_bits + 7) / 8;
  1047. x->aalg = kmalloc(sizeof(*x->aalg) + keysize, GFP_KERNEL);
  1048. if (!x->aalg) {
  1049. err = -ENOMEM;
  1050. goto out;
  1051. }
  1052. strcpy(x->aalg->alg_name, a->name);
  1053. x->aalg->alg_key_len = 0;
  1054. if (key) {
  1055. x->aalg->alg_key_len = key->sadb_key_bits;
  1056. memcpy(x->aalg->alg_key, key+1, keysize);
  1057. }
  1058. x->aalg->alg_trunc_len = a->uinfo.auth.icv_truncbits;
  1059. x->props.aalgo = sa->sadb_sa_auth;
  1060. /* x->algo.flags = sa->sadb_sa_flags; */
  1061. }
  1062. if (sa->sadb_sa_encrypt) {
  1063. if (hdr->sadb_msg_satype == SADB_X_SATYPE_IPCOMP) {
  1064. struct xfrm_algo_desc *a = xfrm_calg_get_byid(sa->sadb_sa_encrypt);
  1065. if (!a || !a->pfkey_supported) {
  1066. err = -ENOSYS;
  1067. goto out;
  1068. }
  1069. x->calg = kmalloc(sizeof(*x->calg), GFP_KERNEL);
  1070. if (!x->calg) {
  1071. err = -ENOMEM;
  1072. goto out;
  1073. }
  1074. strcpy(x->calg->alg_name, a->name);
  1075. x->props.calgo = sa->sadb_sa_encrypt;
  1076. } else {
  1077. int keysize = 0;
  1078. struct xfrm_algo_desc *a = xfrm_ealg_get_byid(sa->sadb_sa_encrypt);
  1079. if (!a || !a->pfkey_supported) {
  1080. err = -ENOSYS;
  1081. goto out;
  1082. }
  1083. key = (struct sadb_key*) ext_hdrs[SADB_EXT_KEY_ENCRYPT-1];
  1084. if (key)
  1085. keysize = (key->sadb_key_bits + 7) / 8;
  1086. x->ealg = kmalloc(sizeof(*x->ealg) + keysize, GFP_KERNEL);
  1087. if (!x->ealg) {
  1088. err = -ENOMEM;
  1089. goto out;
  1090. }
  1091. strcpy(x->ealg->alg_name, a->name);
  1092. x->ealg->alg_key_len = 0;
  1093. if (key) {
  1094. x->ealg->alg_key_len = key->sadb_key_bits;
  1095. memcpy(x->ealg->alg_key, key+1, keysize);
  1096. }
  1097. x->props.ealgo = sa->sadb_sa_encrypt;
  1098. x->geniv = a->uinfo.encr.geniv;
  1099. }
  1100. }
  1101. /* x->algo.flags = sa->sadb_sa_flags; */
  1102. x->props.family = pfkey_sadb_addr2xfrm_addr((struct sadb_address *) ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
  1103. &x->props.saddr);
  1104. pfkey_sadb_addr2xfrm_addr((struct sadb_address *) ext_hdrs[SADB_EXT_ADDRESS_DST-1],
  1105. &x->id.daddr);
  1106. if (ext_hdrs[SADB_X_EXT_SA2-1]) {
  1107. const struct sadb_x_sa2 *sa2 = ext_hdrs[SADB_X_EXT_SA2-1];
  1108. int mode = pfkey_mode_to_xfrm(sa2->sadb_x_sa2_mode);
  1109. if (mode < 0) {
  1110. err = -EINVAL;
  1111. goto out;
  1112. }
  1113. x->props.mode = mode;
  1114. x->props.reqid = sa2->sadb_x_sa2_reqid;
  1115. }
  1116. if (ext_hdrs[SADB_EXT_ADDRESS_PROXY-1]) {
  1117. const struct sadb_address *addr = ext_hdrs[SADB_EXT_ADDRESS_PROXY-1];
  1118. /* Nobody uses this, but we try. */
  1119. x->sel.family = pfkey_sadb_addr2xfrm_addr(addr, &x->sel.saddr);
  1120. x->sel.prefixlen_s = addr->sadb_address_prefixlen;
  1121. }
  1122. if (!x->sel.family)
  1123. x->sel.family = x->props.family;
  1124. if (ext_hdrs[SADB_X_EXT_NAT_T_TYPE-1]) {
  1125. const struct sadb_x_nat_t_type* n_type;
  1126. struct xfrm_encap_tmpl *natt;
  1127. x->encap = kmalloc(sizeof(*x->encap), GFP_KERNEL);
  1128. if (!x->encap) {
  1129. err = -ENOMEM;
  1130. goto out;
  1131. }
  1132. natt = x->encap;
  1133. n_type = ext_hdrs[SADB_X_EXT_NAT_T_TYPE-1];
  1134. natt->encap_type = n_type->sadb_x_nat_t_type_type;
  1135. if (ext_hdrs[SADB_X_EXT_NAT_T_SPORT-1]) {
  1136. const struct sadb_x_nat_t_port *n_port =
  1137. ext_hdrs[SADB_X_EXT_NAT_T_SPORT-1];
  1138. natt->encap_sport = n_port->sadb_x_nat_t_port_port;
  1139. }
  1140. if (ext_hdrs[SADB_X_EXT_NAT_T_DPORT-1]) {
  1141. const struct sadb_x_nat_t_port *n_port =
  1142. ext_hdrs[SADB_X_EXT_NAT_T_DPORT-1];
  1143. natt->encap_dport = n_port->sadb_x_nat_t_port_port;
  1144. }
  1145. memset(&natt->encap_oa, 0, sizeof(natt->encap_oa));
  1146. }
  1147. err = xfrm_init_state(x);
  1148. if (err)
  1149. goto out;
  1150. x->km.seq = hdr->sadb_msg_seq;
  1151. return x;
  1152. out:
  1153. x->km.state = XFRM_STATE_DEAD;
  1154. xfrm_state_put(x);
  1155. return ERR_PTR(err);
  1156. }
  1157. static int pfkey_reserved(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr, void * const *ext_hdrs)
  1158. {
  1159. return -EOPNOTSUPP;
  1160. }
  1161. static int pfkey_getspi(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr, void * const *ext_hdrs)
  1162. {
  1163. struct net *net = sock_net(sk);
  1164. struct sk_buff *resp_skb;
  1165. struct sadb_x_sa2 *sa2;
  1166. struct sadb_address *saddr, *daddr;
  1167. struct sadb_msg *out_hdr;
  1168. struct sadb_spirange *range;
  1169. struct xfrm_state *x = NULL;
  1170. int mode;
  1171. int err;
  1172. u32 min_spi, max_spi;
  1173. u32 reqid;
  1174. u8 proto;
  1175. unsigned short family;
  1176. xfrm_address_t *xsaddr = NULL, *xdaddr = NULL;
  1177. if (!present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
  1178. ext_hdrs[SADB_EXT_ADDRESS_DST-1]))
  1179. return -EINVAL;
  1180. proto = pfkey_satype2proto(hdr->sadb_msg_satype);
  1181. if (proto == 0)
  1182. return -EINVAL;
  1183. if ((sa2 = ext_hdrs[SADB_X_EXT_SA2-1]) != NULL) {
  1184. mode = pfkey_mode_to_xfrm(sa2->sadb_x_sa2_mode);
  1185. if (mode < 0)
  1186. return -EINVAL;
  1187. reqid = sa2->sadb_x_sa2_reqid;
  1188. } else {
  1189. mode = 0;
  1190. reqid = 0;
  1191. }
  1192. saddr = ext_hdrs[SADB_EXT_ADDRESS_SRC-1];
  1193. daddr = ext_hdrs[SADB_EXT_ADDRESS_DST-1];
  1194. family = ((struct sockaddr *)(saddr + 1))->sa_family;
  1195. switch (family) {
  1196. case AF_INET:
  1197. xdaddr = (xfrm_address_t *)&((struct sockaddr_in *)(daddr + 1))->sin_addr.s_addr;
  1198. xsaddr = (xfrm_address_t *)&((struct sockaddr_in *)(saddr + 1))->sin_addr.s_addr;
  1199. break;
  1200. #if IS_ENABLED(CONFIG_IPV6)
  1201. case AF_INET6:
  1202. xdaddr = (xfrm_address_t *)&((struct sockaddr_in6 *)(daddr + 1))->sin6_addr;
  1203. xsaddr = (xfrm_address_t *)&((struct sockaddr_in6 *)(saddr + 1))->sin6_addr;
  1204. break;
  1205. #endif
  1206. }
  1207. if (hdr->sadb_msg_seq) {
  1208. x = xfrm_find_acq_byseq(net, DUMMY_MARK, hdr->sadb_msg_seq);
  1209. if (x && !xfrm_addr_equal(&x->id.daddr, xdaddr, family)) {
  1210. xfrm_state_put(x);
  1211. x = NULL;
  1212. }
  1213. }
  1214. if (!x)
  1215. x = xfrm_find_acq(net, &dummy_mark, mode, reqid, proto, xdaddr, xsaddr, 1, family);
  1216. if (x == NULL)
  1217. return -ENOENT;
  1218. min_spi = 0x100;
  1219. max_spi = 0x0fffffff;
  1220. range = ext_hdrs[SADB_EXT_SPIRANGE-1];
  1221. if (range) {
  1222. min_spi = range->sadb_spirange_min;
  1223. max_spi = range->sadb_spirange_max;
  1224. }
  1225. err = verify_spi_info(x->id.proto, min_spi, max_spi);
  1226. if (err) {
  1227. xfrm_state_put(x);
  1228. return err;
  1229. }
  1230. err = xfrm_alloc_spi(x, min_spi, max_spi);
  1231. resp_skb = err ? ERR_PTR(err) : pfkey_xfrm_state2msg(x);
  1232. if (IS_ERR(resp_skb)) {
  1233. xfrm_state_put(x);
  1234. return PTR_ERR(resp_skb);
  1235. }
  1236. out_hdr = (struct sadb_msg *) resp_skb->data;
  1237. out_hdr->sadb_msg_version = hdr->sadb_msg_version;
  1238. out_hdr->sadb_msg_type = SADB_GETSPI;
  1239. out_hdr->sadb_msg_satype = pfkey_proto2satype(proto);
  1240. out_hdr->sadb_msg_errno = 0;
  1241. out_hdr->sadb_msg_reserved = 0;
  1242. out_hdr->sadb_msg_seq = hdr->sadb_msg_seq;
  1243. out_hdr->sadb_msg_pid = hdr->sadb_msg_pid;
  1244. xfrm_state_put(x);
  1245. pfkey_broadcast(resp_skb, GFP_KERNEL, BROADCAST_ONE, sk, net);
  1246. return 0;
  1247. }
  1248. static int pfkey_acquire(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr, void * const *ext_hdrs)
  1249. {
  1250. struct net *net = sock_net(sk);
  1251. struct xfrm_state *x;
  1252. if (hdr->sadb_msg_len != sizeof(struct sadb_msg)/8)
  1253. return -EOPNOTSUPP;
  1254. if (hdr->sadb_msg_seq == 0 || hdr->sadb_msg_errno == 0)
  1255. return 0;
  1256. x = xfrm_find_acq_byseq(net, DUMMY_MARK, hdr->sadb_msg_seq);
  1257. if (x == NULL)
  1258. return 0;
  1259. spin_lock_bh(&x->lock);
  1260. if (x->km.state == XFRM_STATE_ACQ)
  1261. x->km.state = XFRM_STATE_ERROR;
  1262. spin_unlock_bh(&x->lock);
  1263. xfrm_state_put(x);
  1264. return 0;
  1265. }
  1266. static inline int event2poltype(int event)
  1267. {
  1268. switch (event) {
  1269. case XFRM_MSG_DELPOLICY:
  1270. return SADB_X_SPDDELETE;
  1271. case XFRM_MSG_NEWPOLICY:
  1272. return SADB_X_SPDADD;
  1273. case XFRM_MSG_UPDPOLICY:
  1274. return SADB_X_SPDUPDATE;
  1275. case XFRM_MSG_POLEXPIRE:
  1276. // return SADB_X_SPDEXPIRE;
  1277. default:
  1278. pr_err("pfkey: Unknown policy event %d\n", event);
  1279. break;
  1280. }
  1281. return 0;
  1282. }
  1283. static inline int event2keytype(int event)
  1284. {
  1285. switch (event) {
  1286. case XFRM_MSG_DELSA:
  1287. return SADB_DELETE;
  1288. case XFRM_MSG_NEWSA:
  1289. return SADB_ADD;
  1290. case XFRM_MSG_UPDSA:
  1291. return SADB_UPDATE;
  1292. case XFRM_MSG_EXPIRE:
  1293. return SADB_EXPIRE;
  1294. default:
  1295. pr_err("pfkey: Unknown SA event %d\n", event);
  1296. break;
  1297. }
  1298. return 0;
  1299. }
  1300. /* ADD/UPD/DEL */
  1301. static int key_notify_sa(struct xfrm_state *x, const struct km_event *c)
  1302. {
  1303. struct sk_buff *skb;
  1304. struct sadb_msg *hdr;
  1305. skb = pfkey_xfrm_state2msg(x);
  1306. if (IS_ERR(skb))
  1307. return PTR_ERR(skb);
  1308. hdr = (struct sadb_msg *) skb->data;
  1309. hdr->sadb_msg_version = PF_KEY_V2;
  1310. hdr->sadb_msg_type = event2keytype(c->event);
  1311. hdr->sadb_msg_satype = pfkey_proto2satype(x->id.proto);
  1312. hdr->sadb_msg_errno = 0;
  1313. hdr->sadb_msg_reserved = 0;
  1314. hdr->sadb_msg_seq = c->seq;
  1315. hdr->sadb_msg_pid = c->portid;
  1316. pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_ALL, NULL, xs_net(x));
  1317. return 0;
  1318. }
  1319. static int pfkey_add(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr, void * const *ext_hdrs)
  1320. {
  1321. struct net *net = sock_net(sk);
  1322. struct xfrm_state *x;
  1323. int err;
  1324. struct km_event c;
  1325. x = pfkey_msg2xfrm_state(net, hdr, ext_hdrs);
  1326. if (IS_ERR(x))
  1327. return PTR_ERR(x);
  1328. xfrm_state_hold(x);
  1329. if (hdr->sadb_msg_type == SADB_ADD)
  1330. err = xfrm_state_add(x);
  1331. else
  1332. err = xfrm_state_update(x);
  1333. xfrm_audit_state_add(x, err ? 0 : 1, true);
  1334. if (err < 0) {
  1335. x->km.state = XFRM_STATE_DEAD;
  1336. __xfrm_state_put(x);
  1337. goto out;
  1338. }
  1339. if (hdr->sadb_msg_type == SADB_ADD)
  1340. c.event = XFRM_MSG_NEWSA;
  1341. else
  1342. c.event = XFRM_MSG_UPDSA;
  1343. c.seq = hdr->sadb_msg_seq;
  1344. c.portid = hdr->sadb_msg_pid;
  1345. km_state_notify(x, &c);
  1346. out:
  1347. xfrm_state_put(x);
  1348. return err;
  1349. }
  1350. static int pfkey_delete(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr, void * const *ext_hdrs)
  1351. {
  1352. struct net *net = sock_net(sk);
  1353. struct xfrm_state *x;
  1354. struct km_event c;
  1355. int err;
  1356. if (!ext_hdrs[SADB_EXT_SA-1] ||
  1357. !present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
  1358. ext_hdrs[SADB_EXT_ADDRESS_DST-1]))
  1359. return -EINVAL;
  1360. x = pfkey_xfrm_state_lookup(net, hdr, ext_hdrs);
  1361. if (x == NULL)
  1362. return -ESRCH;
  1363. if ((err = security_xfrm_state_delete(x)))
  1364. goto out;
  1365. if (xfrm_state_kern(x)) {
  1366. err = -EPERM;
  1367. goto out;
  1368. }
  1369. err = xfrm_state_delete(x);
  1370. if (err < 0)
  1371. goto out;
  1372. c.seq = hdr->sadb_msg_seq;
  1373. c.portid = hdr->sadb_msg_pid;
  1374. c.event = XFRM_MSG_DELSA;
  1375. km_state_notify(x, &c);
  1376. out:
  1377. xfrm_audit_state_delete(x, err ? 0 : 1, true);
  1378. xfrm_state_put(x);
  1379. return err;
  1380. }
  1381. static int pfkey_get(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr, void * const *ext_hdrs)
  1382. {
  1383. struct net *net = sock_net(sk);
  1384. __u8 proto;
  1385. struct sk_buff *out_skb;
  1386. struct sadb_msg *out_hdr;
  1387. struct xfrm_state *x;
  1388. if (!ext_hdrs[SADB_EXT_SA-1] ||
  1389. !present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
  1390. ext_hdrs[SADB_EXT_ADDRESS_DST-1]))
  1391. return -EINVAL;
  1392. x = pfkey_xfrm_state_lookup(net, hdr, ext_hdrs);
  1393. if (x == NULL)
  1394. return -ESRCH;
  1395. out_skb = pfkey_xfrm_state2msg(x);
  1396. proto = x->id.proto;
  1397. xfrm_state_put(x);
  1398. if (IS_ERR(out_skb))
  1399. return PTR_ERR(out_skb);
  1400. out_hdr = (struct sadb_msg *) out_skb->data;
  1401. out_hdr->sadb_msg_version = hdr->sadb_msg_version;
  1402. out_hdr->sadb_msg_type = SADB_GET;
  1403. out_hdr->sadb_msg_satype = pfkey_proto2satype(proto);
  1404. out_hdr->sadb_msg_errno = 0;
  1405. out_hdr->sadb_msg_reserved = 0;
  1406. out_hdr->sadb_msg_seq = hdr->sadb_msg_seq;
  1407. out_hdr->sadb_msg_pid = hdr->sadb_msg_pid;
  1408. pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_ONE, sk, sock_net(sk));
  1409. return 0;
  1410. }
  1411. static struct sk_buff *compose_sadb_supported(const struct sadb_msg *orig,
  1412. gfp_t allocation)
  1413. {
  1414. struct sk_buff *skb;
  1415. struct sadb_msg *hdr;
  1416. int len, auth_len, enc_len, i;
  1417. auth_len = xfrm_count_pfkey_auth_supported();
  1418. if (auth_len) {
  1419. auth_len *= sizeof(struct sadb_alg);
  1420. auth_len += sizeof(struct sadb_supported);
  1421. }
  1422. enc_len = xfrm_count_pfkey_enc_supported();
  1423. if (enc_len) {
  1424. enc_len *= sizeof(struct sadb_alg);
  1425. enc_len += sizeof(struct sadb_supported);
  1426. }
  1427. len = enc_len + auth_len + sizeof(struct sadb_msg);
  1428. skb = alloc_skb(len + 16, allocation);
  1429. if (!skb)
  1430. goto out_put_algs;
  1431. hdr = (struct sadb_msg *) skb_put(skb, sizeof(*hdr));
  1432. pfkey_hdr_dup(hdr, orig);
  1433. hdr->sadb_msg_errno = 0;
  1434. hdr->sadb_msg_len = len / sizeof(uint64_t);
  1435. if (auth_len) {
  1436. struct sadb_supported *sp;
  1437. struct sadb_alg *ap;
  1438. sp = (struct sadb_supported *) skb_put(skb, auth_len);
  1439. ap = (struct sadb_alg *) (sp + 1);
  1440. sp->sadb_supported_len = auth_len / sizeof(uint64_t);
  1441. sp->sadb_supported_exttype = SADB_EXT_SUPPORTED_AUTH;
  1442. for (i = 0; ; i++) {
  1443. struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(i);
  1444. if (!aalg)
  1445. break;
  1446. if (!aalg->pfkey_supported)
  1447. continue;
  1448. if (aalg->available)
  1449. *ap++ = aalg->desc;
  1450. }
  1451. }
  1452. if (enc_len) {
  1453. struct sadb_supported *sp;
  1454. struct sadb_alg *ap;
  1455. sp = (struct sadb_supported *) skb_put(skb, enc_len);
  1456. ap = (struct sadb_alg *) (sp + 1);
  1457. sp->sadb_supported_len = enc_len / sizeof(uint64_t);
  1458. sp->sadb_supported_exttype = SADB_EXT_SUPPORTED_ENCRYPT;
  1459. for (i = 0; ; i++) {
  1460. struct xfrm_algo_desc *ealg = xfrm_ealg_get_byidx(i);
  1461. if (!ealg)
  1462. break;
  1463. if (!ealg->pfkey_supported)
  1464. continue;
  1465. if (ealg->available)
  1466. *ap++ = ealg->desc;
  1467. }
  1468. }
  1469. out_put_algs:
  1470. return skb;
  1471. }
  1472. static int pfkey_register(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr, void * const *ext_hdrs)
  1473. {
  1474. struct pfkey_sock *pfk = pfkey_sk(sk);
  1475. struct sk_buff *supp_skb;
  1476. if (hdr->sadb_msg_satype > SADB_SATYPE_MAX)
  1477. return -EINVAL;
  1478. if (hdr->sadb_msg_satype != SADB_SATYPE_UNSPEC) {
  1479. if (pfk->registered&(1<<hdr->sadb_msg_satype))
  1480. return -EEXIST;
  1481. pfk->registered |= (1<<hdr->sadb_msg_satype);
  1482. }
  1483. xfrm_probe_algs();
  1484. supp_skb = compose_sadb_supported(hdr, GFP_KERNEL);
  1485. if (!supp_skb) {
  1486. if (hdr->sadb_msg_satype != SADB_SATYPE_UNSPEC)
  1487. pfk->registered &= ~(1<<hdr->sadb_msg_satype);
  1488. return -ENOBUFS;
  1489. }
  1490. pfkey_broadcast(supp_skb, GFP_KERNEL, BROADCAST_REGISTERED, sk,
  1491. sock_net(sk));
  1492. return 0;
  1493. }
  1494. static int unicast_flush_resp(struct sock *sk, const struct sadb_msg *ihdr)
  1495. {
  1496. struct sk_buff *skb;
  1497. struct sadb_msg *hdr;
  1498. skb = alloc_skb(sizeof(struct sadb_msg) + 16, GFP_ATOMIC);
  1499. if (!skb)
  1500. return -ENOBUFS;
  1501. hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
  1502. memcpy(hdr, ihdr, sizeof(struct sadb_msg));
  1503. hdr->sadb_msg_errno = (uint8_t) 0;
  1504. hdr->sadb_msg_len = (sizeof(struct sadb_msg) / sizeof(uint64_t));
  1505. return pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_ONE, sk,
  1506. sock_net(sk));
  1507. }
  1508. static int key_notify_sa_flush(const struct km_event *c)
  1509. {
  1510. struct sk_buff *skb;
  1511. struct sadb_msg *hdr;
  1512. skb = alloc_skb(sizeof(struct sadb_msg) + 16, GFP_ATOMIC);
  1513. if (!skb)
  1514. return -ENOBUFS;
  1515. hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
  1516. hdr->sadb_msg_satype = pfkey_proto2satype(c->data.proto);
  1517. hdr->sadb_msg_type = SADB_FLUSH;
  1518. hdr->sadb_msg_seq = c->seq;
  1519. hdr->sadb_msg_pid = c->portid;
  1520. hdr->sadb_msg_version = PF_KEY_V2;
  1521. hdr->sadb_msg_errno = (uint8_t) 0;
  1522. hdr->sadb_msg_len = (sizeof(struct sadb_msg) / sizeof(uint64_t));
  1523. hdr->sadb_msg_reserved = 0;
  1524. pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_ALL, NULL, c->net);
  1525. return 0;
  1526. }
  1527. static int pfkey_flush(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr, void * const *ext_hdrs)
  1528. {
  1529. struct net *net = sock_net(sk);
  1530. unsigned int proto;
  1531. struct km_event c;
  1532. int err, err2;
  1533. proto = pfkey_satype2proto(hdr->sadb_msg_satype);
  1534. if (proto == 0)
  1535. return -EINVAL;
  1536. err = xfrm_state_flush(net, proto, true);
  1537. err2 = unicast_flush_resp(sk, hdr);
  1538. if (err || err2) {
  1539. if (err == -ESRCH) /* empty table - go quietly */
  1540. err = 0;
  1541. return err ? err : err2;
  1542. }
  1543. c.data.proto = proto;
  1544. c.seq = hdr->sadb_msg_seq;
  1545. c.portid = hdr->sadb_msg_pid;
  1546. c.event = XFRM_MSG_FLUSHSA;
  1547. c.net = net;
  1548. km_state_notify(NULL, &c);
  1549. return 0;
  1550. }
  1551. static int dump_sa(struct xfrm_state *x, int count, void *ptr)
  1552. {
  1553. struct pfkey_sock *pfk = ptr;
  1554. struct sk_buff *out_skb;
  1555. struct sadb_msg *out_hdr;
  1556. if (!pfkey_can_dump(&pfk->sk))
  1557. return -ENOBUFS;
  1558. out_skb = pfkey_xfrm_state2msg(x);
  1559. if (IS_ERR(out_skb))
  1560. return PTR_ERR(out_skb);
  1561. out_hdr = (struct sadb_msg *) out_skb->data;
  1562. out_hdr->sadb_msg_version = pfk->dump.msg_version;
  1563. out_hdr->sadb_msg_type = SADB_DUMP;
  1564. out_hdr->sadb_msg_satype = pfkey_proto2satype(x->id.proto);
  1565. out_hdr->sadb_msg_errno = 0;
  1566. out_hdr->sadb_msg_reserved = 0;
  1567. out_hdr->sadb_msg_seq = count + 1;
  1568. out_hdr->sadb_msg_pid = pfk->dump.msg_portid;
  1569. if (pfk->dump.skb)
  1570. pfkey_broadcast(pfk->dump.skb, GFP_ATOMIC, BROADCAST_ONE,
  1571. &pfk->sk, sock_net(&pfk->sk));
  1572. pfk->dump.skb = out_skb;
  1573. return 0;
  1574. }
  1575. static int pfkey_dump_sa(struct pfkey_sock *pfk)
  1576. {
  1577. struct net *net = sock_net(&pfk->sk);
  1578. return xfrm_state_walk(net, &pfk->dump.u.state, dump_sa, (void *) pfk);
  1579. }
  1580. static void pfkey_dump_sa_done(struct pfkey_sock *pfk)
  1581. {
  1582. struct net *net = sock_net(&pfk->sk);
  1583. xfrm_state_walk_done(&pfk->dump.u.state, net);
  1584. }
  1585. static int pfkey_dump(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr, void * const *ext_hdrs)
  1586. {
  1587. u8 proto;
  1588. struct xfrm_address_filter *filter = NULL;
  1589. struct pfkey_sock *pfk = pfkey_sk(sk);
  1590. mutex_lock(&pfk->dump_lock);
  1591. if (pfk->dump.dump != NULL) {
  1592. mutex_unlock(&pfk->dump_lock);
  1593. return -EBUSY;
  1594. }
  1595. proto = pfkey_satype2proto(hdr->sadb_msg_satype);
  1596. if (proto == 0) {
  1597. mutex_unlock(&pfk->dump_lock);
  1598. return -EINVAL;
  1599. }
  1600. if (ext_hdrs[SADB_X_EXT_FILTER - 1]) {
  1601. struct sadb_x_filter *xfilter = ext_hdrs[SADB_X_EXT_FILTER - 1];
  1602. filter = kmalloc(sizeof(*filter), GFP_KERNEL);
  1603. if (filter == NULL) {
  1604. mutex_unlock(&pfk->dump_lock);
  1605. return -ENOMEM;
  1606. }
  1607. memcpy(&filter->saddr, &xfilter->sadb_x_filter_saddr,
  1608. sizeof(xfrm_address_t));
  1609. memcpy(&filter->daddr, &xfilter->sadb_x_filter_daddr,
  1610. sizeof(xfrm_address_t));
  1611. filter->family = xfilter->sadb_x_filter_family;
  1612. filter->splen = xfilter->sadb_x_filter_splen;
  1613. filter->dplen = xfilter->sadb_x_filter_dplen;
  1614. }
  1615. pfk->dump.msg_version = hdr->sadb_msg_version;
  1616. pfk->dump.msg_portid = hdr->sadb_msg_pid;
  1617. pfk->dump.dump = pfkey_dump_sa;
  1618. pfk->dump.done = pfkey_dump_sa_done;
  1619. xfrm_state_walk_init(&pfk->dump.u.state, proto, filter);
  1620. mutex_unlock(&pfk->dump_lock);
  1621. return pfkey_do_dump(pfk);
  1622. }
  1623. static int pfkey_promisc(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr, void * const *ext_hdrs)
  1624. {
  1625. struct pfkey_sock *pfk = pfkey_sk(sk);
  1626. int satype = hdr->sadb_msg_satype;
  1627. bool reset_errno = false;
  1628. if (hdr->sadb_msg_len == (sizeof(*hdr) / sizeof(uint64_t))) {
  1629. reset_errno = true;
  1630. if (satype != 0 && satype != 1)
  1631. return -EINVAL;
  1632. pfk->promisc = satype;
  1633. }
  1634. if (reset_errno && skb_cloned(skb))
  1635. skb = skb_copy(skb, GFP_KERNEL);
  1636. else
  1637. skb = skb_clone(skb, GFP_KERNEL);
  1638. if (reset_errno && skb) {
  1639. struct sadb_msg *new_hdr = (struct sadb_msg *) skb->data;
  1640. new_hdr->sadb_msg_errno = 0;
  1641. }
  1642. pfkey_broadcast(skb, GFP_KERNEL, BROADCAST_ALL, NULL, sock_net(sk));
  1643. return 0;
  1644. }
  1645. static int check_reqid(struct xfrm_policy *xp, int dir, int count, void *ptr)
  1646. {
  1647. int i;
  1648. u32 reqid = *(u32*)ptr;
  1649. for (i=0; i<xp->xfrm_nr; i++) {
  1650. if (xp->xfrm_vec[i].reqid == reqid)
  1651. return -EEXIST;
  1652. }
  1653. return 0;
  1654. }
  1655. static u32 gen_reqid(struct net *net)
  1656. {
  1657. struct xfrm_policy_walk walk;
  1658. u32 start;
  1659. int rc;
  1660. static u32 reqid = IPSEC_MANUAL_REQID_MAX;
  1661. start = reqid;
  1662. do {
  1663. ++reqid;
  1664. if (reqid == 0)
  1665. reqid = IPSEC_MANUAL_REQID_MAX+1;
  1666. xfrm_policy_walk_init(&walk, XFRM_POLICY_TYPE_MAIN);
  1667. rc = xfrm_policy_walk(net, &walk, check_reqid, (void*)&reqid);
  1668. xfrm_policy_walk_done(&walk, net);
  1669. if (rc != -EEXIST)
  1670. return reqid;
  1671. } while (reqid != start);
  1672. return 0;
  1673. }
  1674. static int
  1675. parse_ipsecrequest(struct xfrm_policy *xp, struct sadb_x_ipsecrequest *rq)
  1676. {
  1677. struct net *net = xp_net(xp);
  1678. struct xfrm_tmpl *t = xp->xfrm_vec + xp->xfrm_nr;
  1679. int mode;
  1680. if (xp->xfrm_nr >= XFRM_MAX_DEPTH)
  1681. return -ELOOP;
  1682. if (rq->sadb_x_ipsecrequest_mode == 0)
  1683. return -EINVAL;
  1684. t->id.proto = rq->sadb_x_ipsecrequest_proto; /* XXX check proto */
  1685. if ((mode = pfkey_mode_to_xfrm(rq->sadb_x_ipsecrequest_mode)) < 0)
  1686. return -EINVAL;
  1687. t->mode = mode;
  1688. if (rq->sadb_x_ipsecrequest_level == IPSEC_LEVEL_USE)
  1689. t->optional = 1;
  1690. else if (rq->sadb_x_ipsecrequest_level == IPSEC_LEVEL_UNIQUE) {
  1691. t->reqid = rq->sadb_x_ipsecrequest_reqid;
  1692. if (t->reqid > IPSEC_MANUAL_REQID_MAX)
  1693. t->reqid = 0;
  1694. if (!t->reqid && !(t->reqid = gen_reqid(net)))
  1695. return -ENOBUFS;
  1696. }
  1697. /* addresses present only in tunnel mode */
  1698. if (t->mode == XFRM_MODE_TUNNEL) {
  1699. int err;
  1700. err = parse_sockaddr_pair(
  1701. (struct sockaddr *)(rq + 1),
  1702. rq->sadb_x_ipsecrequest_len - sizeof(*rq),
  1703. &t->saddr, &t->id.daddr, &t->encap_family);
  1704. if (err)
  1705. return err;
  1706. } else
  1707. t->encap_family = xp->family;
  1708. /* No way to set this via kame pfkey */
  1709. t->allalgs = 1;
  1710. xp->xfrm_nr++;
  1711. return 0;
  1712. }
  1713. static int
  1714. parse_ipsecrequests(struct xfrm_policy *xp, struct sadb_x_policy *pol)
  1715. {
  1716. int err;
  1717. int len = pol->sadb_x_policy_len*8 - sizeof(struct sadb_x_policy);
  1718. struct sadb_x_ipsecrequest *rq = (void*)(pol+1);
  1719. if (pol->sadb_x_policy_len * 8 < sizeof(struct sadb_x_policy))
  1720. return -EINVAL;
  1721. while (len >= sizeof(*rq)) {
  1722. if (len < rq->sadb_x_ipsecrequest_len ||
  1723. rq->sadb_x_ipsecrequest_len < sizeof(*rq))
  1724. return -EINVAL;
  1725. if ((err = parse_ipsecrequest(xp, rq)) < 0)
  1726. return err;
  1727. len -= rq->sadb_x_ipsecrequest_len;
  1728. rq = (void*)((u8*)rq + rq->sadb_x_ipsecrequest_len);
  1729. }
  1730. return 0;
  1731. }
  1732. static inline int pfkey_xfrm_policy2sec_ctx_size(const struct xfrm_policy *xp)
  1733. {
  1734. struct xfrm_sec_ctx *xfrm_ctx = xp->security;
  1735. if (xfrm_ctx) {
  1736. int len = sizeof(struct sadb_x_sec_ctx);
  1737. len += xfrm_ctx->ctx_len;
  1738. return PFKEY_ALIGN8(len);
  1739. }
  1740. return 0;
  1741. }
  1742. static int pfkey_xfrm_policy2msg_size(const struct xfrm_policy *xp)
  1743. {
  1744. const struct xfrm_tmpl *t;
  1745. int sockaddr_size = pfkey_sockaddr_size(xp->family);
  1746. int socklen = 0;
  1747. int i;
  1748. for (i=0; i<xp->xfrm_nr; i++) {
  1749. t = xp->xfrm_vec + i;
  1750. socklen += pfkey_sockaddr_len(t->encap_family);
  1751. }
  1752. return sizeof(struct sadb_msg) +
  1753. (sizeof(struct sadb_lifetime) * 3) +
  1754. (sizeof(struct sadb_address) * 2) +
  1755. (sockaddr_size * 2) +
  1756. sizeof(struct sadb_x_policy) +
  1757. (xp->xfrm_nr * sizeof(struct sadb_x_ipsecrequest)) +
  1758. (socklen * 2) +
  1759. pfkey_xfrm_policy2sec_ctx_size(xp);
  1760. }
  1761. static struct sk_buff * pfkey_xfrm_policy2msg_prep(const struct xfrm_policy *xp)
  1762. {
  1763. struct sk_buff *skb;
  1764. int size;
  1765. size = pfkey_xfrm_policy2msg_size(xp);
  1766. skb = alloc_skb(size + 16, GFP_ATOMIC);
  1767. if (skb == NULL)
  1768. return ERR_PTR(-ENOBUFS);
  1769. return skb;
  1770. }
  1771. static int pfkey_xfrm_policy2msg(struct sk_buff *skb, const struct xfrm_policy *xp, int dir)
  1772. {
  1773. struct sadb_msg *hdr;
  1774. struct sadb_address *addr;
  1775. struct sadb_lifetime *lifetime;
  1776. struct sadb_x_policy *pol;
  1777. struct sadb_x_sec_ctx *sec_ctx;
  1778. struct xfrm_sec_ctx *xfrm_ctx;
  1779. int i;
  1780. int size;
  1781. int sockaddr_size = pfkey_sockaddr_size(xp->family);
  1782. int socklen = pfkey_sockaddr_len(xp->family);
  1783. size = pfkey_xfrm_policy2msg_size(xp);
  1784. /* call should fill header later */
  1785. hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
  1786. memset(hdr, 0, size); /* XXX do we need this ? */
  1787. /* src address */
  1788. addr = (struct sadb_address*) skb_put(skb,
  1789. sizeof(struct sadb_address)+sockaddr_size);
  1790. addr->sadb_address_len =
  1791. (sizeof(struct sadb_address)+sockaddr_size)/
  1792. sizeof(uint64_t);
  1793. addr->sadb_address_exttype = SADB_EXT_ADDRESS_SRC;
  1794. addr->sadb_address_proto = pfkey_proto_from_xfrm(xp->selector.proto);
  1795. addr->sadb_address_prefixlen = xp->selector.prefixlen_s;
  1796. addr->sadb_address_reserved = 0;
  1797. if (!pfkey_sockaddr_fill(&xp->selector.saddr,
  1798. xp->selector.sport,
  1799. (struct sockaddr *) (addr + 1),
  1800. xp->family))
  1801. BUG();
  1802. /* dst address */
  1803. addr = (struct sadb_address*) skb_put(skb,
  1804. sizeof(struct sadb_address)+sockaddr_size);
  1805. addr->sadb_address_len =
  1806. (sizeof(struct sadb_address)+sockaddr_size)/
  1807. sizeof(uint64_t);
  1808. addr->sadb_address_exttype = SADB_EXT_ADDRESS_DST;
  1809. addr->sadb_address_proto = pfkey_proto_from_xfrm(xp->selector.proto);
  1810. addr->sadb_address_prefixlen = xp->selector.prefixlen_d;
  1811. addr->sadb_address_reserved = 0;
  1812. pfkey_sockaddr_fill(&xp->selector.daddr, xp->selector.dport,
  1813. (struct sockaddr *) (addr + 1),
  1814. xp->family);
  1815. /* hard time */
  1816. lifetime = (struct sadb_lifetime *) skb_put(skb,
  1817. sizeof(struct sadb_lifetime));
  1818. lifetime->sadb_lifetime_len =
  1819. sizeof(struct sadb_lifetime)/sizeof(uint64_t);
  1820. lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_HARD;
  1821. lifetime->sadb_lifetime_allocations = _X2KEY(xp->lft.hard_packet_limit);
  1822. lifetime->sadb_lifetime_bytes = _X2KEY(xp->lft.hard_byte_limit);
  1823. lifetime->sadb_lifetime_addtime = xp->lft.hard_add_expires_seconds;
  1824. lifetime->sadb_lifetime_usetime = xp->lft.hard_use_expires_seconds;
  1825. /* soft time */
  1826. lifetime = (struct sadb_lifetime *) skb_put(skb,
  1827. sizeof(struct sadb_lifetime));
  1828. lifetime->sadb_lifetime_len =
  1829. sizeof(struct sadb_lifetime)/sizeof(uint64_t);
  1830. lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_SOFT;
  1831. lifetime->sadb_lifetime_allocations = _X2KEY(xp->lft.soft_packet_limit);
  1832. lifetime->sadb_lifetime_bytes = _X2KEY(xp->lft.soft_byte_limit);
  1833. lifetime->sadb_lifetime_addtime = xp->lft.soft_add_expires_seconds;
  1834. lifetime->sadb_lifetime_usetime = xp->lft.soft_use_expires_seconds;
  1835. /* current time */
  1836. lifetime = (struct sadb_lifetime *) skb_put(skb,
  1837. sizeof(struct sadb_lifetime));
  1838. lifetime->sadb_lifetime_len =
  1839. sizeof(struct sadb_lifetime)/sizeof(uint64_t);
  1840. lifetime->sadb_lifetime_exttype = SADB_EXT_LIFETIME_CURRENT;
  1841. lifetime->sadb_lifetime_allocations = xp->curlft.packets;
  1842. lifetime->sadb_lifetime_bytes = xp->curlft.bytes;
  1843. lifetime->sadb_lifetime_addtime = xp->curlft.add_time;
  1844. lifetime->sadb_lifetime_usetime = xp->curlft.use_time;
  1845. pol = (struct sadb_x_policy *) skb_put(skb, sizeof(struct sadb_x_policy));
  1846. pol->sadb_x_policy_len = sizeof(struct sadb_x_policy)/sizeof(uint64_t);
  1847. pol->sadb_x_policy_exttype = SADB_X_EXT_POLICY;
  1848. pol->sadb_x_policy_type = IPSEC_POLICY_DISCARD;
  1849. if (xp->action == XFRM_POLICY_ALLOW) {
  1850. if (xp->xfrm_nr)
  1851. pol->sadb_x_policy_type = IPSEC_POLICY_IPSEC;
  1852. else
  1853. pol->sadb_x_policy_type = IPSEC_POLICY_NONE;
  1854. }
  1855. pol->sadb_x_policy_dir = dir+1;
  1856. pol->sadb_x_policy_reserved = 0;
  1857. pol->sadb_x_policy_id = xp->index;
  1858. pol->sadb_x_policy_priority = xp->priority;
  1859. for (i=0; i<xp->xfrm_nr; i++) {
  1860. const struct xfrm_tmpl *t = xp->xfrm_vec + i;
  1861. struct sadb_x_ipsecrequest *rq;
  1862. int req_size;
  1863. int mode;
  1864. req_size = sizeof(struct sadb_x_ipsecrequest);
  1865. if (t->mode == XFRM_MODE_TUNNEL) {
  1866. socklen = pfkey_sockaddr_len(t->encap_family);
  1867. req_size += socklen * 2;
  1868. } else {
  1869. size -= 2*socklen;
  1870. }
  1871. rq = (void*)skb_put(skb, req_size);
  1872. pol->sadb_x_policy_len += req_size/8;
  1873. memset(rq, 0, sizeof(*rq));
  1874. rq->sadb_x_ipsecrequest_len = req_size;
  1875. rq->sadb_x_ipsecrequest_proto = t->id.proto;
  1876. if ((mode = pfkey_mode_from_xfrm(t->mode)) < 0)
  1877. return -EINVAL;
  1878. rq->sadb_x_ipsecrequest_mode = mode;
  1879. rq->sadb_x_ipsecrequest_level = IPSEC_LEVEL_REQUIRE;
  1880. if (t->reqid)
  1881. rq->sadb_x_ipsecrequest_level = IPSEC_LEVEL_UNIQUE;
  1882. if (t->optional)
  1883. rq->sadb_x_ipsecrequest_level = IPSEC_LEVEL_USE;
  1884. rq->sadb_x_ipsecrequest_reqid = t->reqid;
  1885. if (t->mode == XFRM_MODE_TUNNEL) {
  1886. u8 *sa = (void *)(rq + 1);
  1887. pfkey_sockaddr_fill(&t->saddr, 0,
  1888. (struct sockaddr *)sa,
  1889. t->encap_family);
  1890. pfkey_sockaddr_fill(&t->id.daddr, 0,
  1891. (struct sockaddr *) (sa + socklen),
  1892. t->encap_family);
  1893. }
  1894. }
  1895. /* security context */
  1896. if ((xfrm_ctx = xp->security)) {
  1897. int ctx_size = pfkey_xfrm_policy2sec_ctx_size(xp);
  1898. sec_ctx = (struct sadb_x_sec_ctx *) skb_put(skb, ctx_size);
  1899. sec_ctx->sadb_x_sec_len = ctx_size / sizeof(uint64_t);
  1900. sec_ctx->sadb_x_sec_exttype = SADB_X_EXT_SEC_CTX;
  1901. sec_ctx->sadb_x_ctx_doi = xfrm_ctx->ctx_doi;
  1902. sec_ctx->sadb_x_ctx_alg = xfrm_ctx->ctx_alg;
  1903. sec_ctx->sadb_x_ctx_len = xfrm_ctx->ctx_len;
  1904. memcpy(sec_ctx + 1, xfrm_ctx->ctx_str,
  1905. xfrm_ctx->ctx_len);
  1906. }
  1907. hdr->sadb_msg_len = size / sizeof(uint64_t);
  1908. hdr->sadb_msg_reserved = atomic_read(&xp->refcnt);
  1909. return 0;
  1910. }
  1911. static int key_notify_policy(struct xfrm_policy *xp, int dir, const struct km_event *c)
  1912. {
  1913. struct sk_buff *out_skb;
  1914. struct sadb_msg *out_hdr;
  1915. int err;
  1916. out_skb = pfkey_xfrm_policy2msg_prep(xp);
  1917. if (IS_ERR(out_skb))
  1918. return PTR_ERR(out_skb);
  1919. err = pfkey_xfrm_policy2msg(out_skb, xp, dir);
  1920. if (err < 0)
  1921. return err;
  1922. out_hdr = (struct sadb_msg *) out_skb->data;
  1923. out_hdr->sadb_msg_version = PF_KEY_V2;
  1924. if (c->data.byid && c->event == XFRM_MSG_DELPOLICY)
  1925. out_hdr->sadb_msg_type = SADB_X_SPDDELETE2;
  1926. else
  1927. out_hdr->sadb_msg_type = event2poltype(c->event);
  1928. out_hdr->sadb_msg_errno = 0;
  1929. out_hdr->sadb_msg_seq = c->seq;
  1930. out_hdr->sadb_msg_pid = c->portid;
  1931. pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_ALL, NULL, xp_net(xp));
  1932. return 0;
  1933. }
  1934. static int pfkey_spdadd(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr, void * const *ext_hdrs)
  1935. {
  1936. struct net *net = sock_net(sk);
  1937. int err = 0;
  1938. struct sadb_lifetime *lifetime;
  1939. struct sadb_address *sa;
  1940. struct sadb_x_policy *pol;
  1941. struct xfrm_policy *xp;
  1942. struct km_event c;
  1943. struct sadb_x_sec_ctx *sec_ctx;
  1944. if (!present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
  1945. ext_hdrs[SADB_EXT_ADDRESS_DST-1]) ||
  1946. !ext_hdrs[SADB_X_EXT_POLICY-1])
  1947. return -EINVAL;
  1948. pol = ext_hdrs[SADB_X_EXT_POLICY-1];
  1949. if (pol->sadb_x_policy_type > IPSEC_POLICY_IPSEC)
  1950. return -EINVAL;
  1951. if (!pol->sadb_x_policy_dir || pol->sadb_x_policy_dir >= IPSEC_DIR_MAX)
  1952. return -EINVAL;
  1953. xp = xfrm_policy_alloc(net, GFP_KERNEL);
  1954. if (xp == NULL)
  1955. return -ENOBUFS;
  1956. xp->action = (pol->sadb_x_policy_type == IPSEC_POLICY_DISCARD ?
  1957. XFRM_POLICY_BLOCK : XFRM_POLICY_ALLOW);
  1958. xp->priority = pol->sadb_x_policy_priority;
  1959. sa = ext_hdrs[SADB_EXT_ADDRESS_SRC-1];
  1960. xp->family = pfkey_sadb_addr2xfrm_addr(sa, &xp->selector.saddr);
  1961. xp->selector.family = xp->family;
  1962. xp->selector.prefixlen_s = sa->sadb_address_prefixlen;
  1963. xp->selector.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
  1964. xp->selector.sport = ((struct sockaddr_in *)(sa+1))->sin_port;
  1965. if (xp->selector.sport)
  1966. xp->selector.sport_mask = htons(0xffff);
  1967. sa = ext_hdrs[SADB_EXT_ADDRESS_DST-1];
  1968. pfkey_sadb_addr2xfrm_addr(sa, &xp->selector.daddr);
  1969. xp->selector.prefixlen_d = sa->sadb_address_prefixlen;
  1970. /* Amusing, we set this twice. KAME apps appear to set same value
  1971. * in both addresses.
  1972. */
  1973. xp->selector.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
  1974. xp->selector.dport = ((struct sockaddr_in *)(sa+1))->sin_port;
  1975. if (xp->selector.dport)
  1976. xp->selector.dport_mask = htons(0xffff);
  1977. sec_ctx = ext_hdrs[SADB_X_EXT_SEC_CTX - 1];
  1978. if (sec_ctx != NULL) {
  1979. struct xfrm_user_sec_ctx *uctx = pfkey_sadb2xfrm_user_sec_ctx(sec_ctx, GFP_KERNEL);
  1980. if (!uctx) {
  1981. err = -ENOBUFS;
  1982. goto out;
  1983. }
  1984. err = security_xfrm_policy_alloc(&xp->security, uctx, GFP_KERNEL);
  1985. kfree(uctx);
  1986. if (err)
  1987. goto out;
  1988. }
  1989. xp->lft.soft_byte_limit = XFRM_INF;
  1990. xp->lft.hard_byte_limit = XFRM_INF;
  1991. xp->lft.soft_packet_limit = XFRM_INF;
  1992. xp->lft.hard_packet_limit = XFRM_INF;
  1993. if ((lifetime = ext_hdrs[SADB_EXT_LIFETIME_HARD-1]) != NULL) {
  1994. xp->lft.hard_packet_limit = _KEY2X(lifetime->sadb_lifetime_allocations);
  1995. xp->lft.hard_byte_limit = _KEY2X(lifetime->sadb_lifetime_bytes);
  1996. xp->lft.hard_add_expires_seconds = lifetime->sadb_lifetime_addtime;
  1997. xp->lft.hard_use_expires_seconds = lifetime->sadb_lifetime_usetime;
  1998. }
  1999. if ((lifetime = ext_hdrs[SADB_EXT_LIFETIME_SOFT-1]) != NULL) {
  2000. xp->lft.soft_packet_limit = _KEY2X(lifetime->sadb_lifetime_allocations);
  2001. xp->lft.soft_byte_limit = _KEY2X(lifetime->sadb_lifetime_bytes);
  2002. xp->lft.soft_add_expires_seconds = lifetime->sadb_lifetime_addtime;
  2003. xp->lft.soft_use_expires_seconds = lifetime->sadb_lifetime_usetime;
  2004. }
  2005. xp->xfrm_nr = 0;
  2006. if (pol->sadb_x_policy_type == IPSEC_POLICY_IPSEC &&
  2007. (err = parse_ipsecrequests(xp, pol)) < 0)
  2008. goto out;
  2009. err = xfrm_policy_insert(pol->sadb_x_policy_dir-1, xp,
  2010. hdr->sadb_msg_type != SADB_X_SPDUPDATE);
  2011. xfrm_audit_policy_add(xp, err ? 0 : 1, true);
  2012. if (err)
  2013. goto out;
  2014. if (hdr->sadb_msg_type == SADB_X_SPDUPDATE)
  2015. c.event = XFRM_MSG_UPDPOLICY;
  2016. else
  2017. c.event = XFRM_MSG_NEWPOLICY;
  2018. c.seq = hdr->sadb_msg_seq;
  2019. c.portid = hdr->sadb_msg_pid;
  2020. km_policy_notify(xp, pol->sadb_x_policy_dir-1, &c);
  2021. xfrm_pol_put(xp);
  2022. return 0;
  2023. out:
  2024. xp->walk.dead = 1;
  2025. xfrm_policy_destroy(xp);
  2026. return err;
  2027. }
  2028. static int pfkey_spddelete(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr, void * const *ext_hdrs)
  2029. {
  2030. struct net *net = sock_net(sk);
  2031. int err;
  2032. struct sadb_address *sa;
  2033. struct sadb_x_policy *pol;
  2034. struct xfrm_policy *xp;
  2035. struct xfrm_selector sel;
  2036. struct km_event c;
  2037. struct sadb_x_sec_ctx *sec_ctx;
  2038. struct xfrm_sec_ctx *pol_ctx = NULL;
  2039. if (!present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC-1],
  2040. ext_hdrs[SADB_EXT_ADDRESS_DST-1]) ||
  2041. !ext_hdrs[SADB_X_EXT_POLICY-1])
  2042. return -EINVAL;
  2043. pol = ext_hdrs[SADB_X_EXT_POLICY-1];
  2044. if (!pol->sadb_x_policy_dir || pol->sadb_x_policy_dir >= IPSEC_DIR_MAX)
  2045. return -EINVAL;
  2046. memset(&sel, 0, sizeof(sel));
  2047. sa = ext_hdrs[SADB_EXT_ADDRESS_SRC-1];
  2048. sel.family = pfkey_sadb_addr2xfrm_addr(sa, &sel.saddr);
  2049. sel.prefixlen_s = sa->sadb_address_prefixlen;
  2050. sel.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
  2051. sel.sport = ((struct sockaddr_in *)(sa+1))->sin_port;
  2052. if (sel.sport)
  2053. sel.sport_mask = htons(0xffff);
  2054. sa = ext_hdrs[SADB_EXT_ADDRESS_DST-1];
  2055. pfkey_sadb_addr2xfrm_addr(sa, &sel.daddr);
  2056. sel.prefixlen_d = sa->sadb_address_prefixlen;
  2057. sel.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
  2058. sel.dport = ((struct sockaddr_in *)(sa+1))->sin_port;
  2059. if (sel.dport)
  2060. sel.dport_mask = htons(0xffff);
  2061. sec_ctx = ext_hdrs[SADB_X_EXT_SEC_CTX - 1];
  2062. if (sec_ctx != NULL) {
  2063. struct xfrm_user_sec_ctx *uctx = pfkey_sadb2xfrm_user_sec_ctx(sec_ctx, GFP_KERNEL);
  2064. if (!uctx)
  2065. return -ENOMEM;
  2066. err = security_xfrm_policy_alloc(&pol_ctx, uctx, GFP_KERNEL);
  2067. kfree(uctx);
  2068. if (err)
  2069. return err;
  2070. }
  2071. xp = xfrm_policy_bysel_ctx(net, DUMMY_MARK, XFRM_POLICY_TYPE_MAIN,
  2072. pol->sadb_x_policy_dir - 1, &sel, pol_ctx,
  2073. 1, &err);
  2074. security_xfrm_policy_free(pol_ctx);
  2075. if (xp == NULL)
  2076. return -ENOENT;
  2077. xfrm_audit_policy_delete(xp, err ? 0 : 1, true);
  2078. if (err)
  2079. goto out;
  2080. c.seq = hdr->sadb_msg_seq;
  2081. c.portid = hdr->sadb_msg_pid;
  2082. c.data.byid = 0;
  2083. c.event = XFRM_MSG_DELPOLICY;
  2084. km_policy_notify(xp, pol->sadb_x_policy_dir-1, &c);
  2085. out:
  2086. xfrm_pol_put(xp);
  2087. if (err == 0)
  2088. xfrm_garbage_collect(net);
  2089. return err;
  2090. }
  2091. static int key_pol_get_resp(struct sock *sk, struct xfrm_policy *xp, const struct sadb_msg *hdr, int dir)
  2092. {
  2093. int err;
  2094. struct sk_buff *out_skb;
  2095. struct sadb_msg *out_hdr;
  2096. err = 0;
  2097. out_skb = pfkey_xfrm_policy2msg_prep(xp);
  2098. if (IS_ERR(out_skb)) {
  2099. err = PTR_ERR(out_skb);
  2100. goto out;
  2101. }
  2102. err = pfkey_xfrm_policy2msg(out_skb, xp, dir);
  2103. if (err < 0)
  2104. goto out;
  2105. out_hdr = (struct sadb_msg *) out_skb->data;
  2106. out_hdr->sadb_msg_version = hdr->sadb_msg_version;
  2107. out_hdr->sadb_msg_type = hdr->sadb_msg_type;
  2108. out_hdr->sadb_msg_satype = 0;
  2109. out_hdr->sadb_msg_errno = 0;
  2110. out_hdr->sadb_msg_seq = hdr->sadb_msg_seq;
  2111. out_hdr->sadb_msg_pid = hdr->sadb_msg_pid;
  2112. pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_ONE, sk, xp_net(xp));
  2113. err = 0;
  2114. out:
  2115. return err;
  2116. }
  2117. static int pfkey_sockaddr_pair_size(sa_family_t family)
  2118. {
  2119. return PFKEY_ALIGN8(pfkey_sockaddr_len(family) * 2);
  2120. }
  2121. static int parse_sockaddr_pair(struct sockaddr *sa, int ext_len,
  2122. xfrm_address_t *saddr, xfrm_address_t *daddr,
  2123. u16 *family)
  2124. {
  2125. int af, socklen;
  2126. if (ext_len < 2 || ext_len < pfkey_sockaddr_pair_size(sa->sa_family))
  2127. return -EINVAL;
  2128. af = pfkey_sockaddr_extract(sa, saddr);
  2129. if (!af)
  2130. return -EINVAL;
  2131. socklen = pfkey_sockaddr_len(af);
  2132. if (pfkey_sockaddr_extract((struct sockaddr *) (((u8 *)sa) + socklen),
  2133. daddr) != af)
  2134. return -EINVAL;
  2135. *family = af;
  2136. return 0;
  2137. }
  2138. #ifdef CONFIG_NET_KEY_MIGRATE
  2139. static int ipsecrequests_to_migrate(struct sadb_x_ipsecrequest *rq1, int len,
  2140. struct xfrm_migrate *m)
  2141. {
  2142. int err;
  2143. struct sadb_x_ipsecrequest *rq2;
  2144. int mode;
  2145. if (len < sizeof(*rq1) ||
  2146. len < rq1->sadb_x_ipsecrequest_len ||
  2147. rq1->sadb_x_ipsecrequest_len < sizeof(*rq1))
  2148. return -EINVAL;
  2149. /* old endoints */
  2150. err = parse_sockaddr_pair((struct sockaddr *)(rq1 + 1),
  2151. rq1->sadb_x_ipsecrequest_len - sizeof(*rq1),
  2152. &m->old_saddr, &m->old_daddr,
  2153. &m->old_family);
  2154. if (err)
  2155. return err;
  2156. rq2 = (struct sadb_x_ipsecrequest *)((u8 *)rq1 + rq1->sadb_x_ipsecrequest_len);
  2157. len -= rq1->sadb_x_ipsecrequest_len;
  2158. if (len <= sizeof(*rq2) ||
  2159. len < rq2->sadb_x_ipsecrequest_len ||
  2160. rq2->sadb_x_ipsecrequest_len < sizeof(*rq2))
  2161. return -EINVAL;
  2162. /* new endpoints */
  2163. err = parse_sockaddr_pair((struct sockaddr *)(rq2 + 1),
  2164. rq2->sadb_x_ipsecrequest_len - sizeof(*rq2),
  2165. &m->new_saddr, &m->new_daddr,
  2166. &m->new_family);
  2167. if (err)
  2168. return err;
  2169. if (rq1->sadb_x_ipsecrequest_proto != rq2->sadb_x_ipsecrequest_proto ||
  2170. rq1->sadb_x_ipsecrequest_mode != rq2->sadb_x_ipsecrequest_mode ||
  2171. rq1->sadb_x_ipsecrequest_reqid != rq2->sadb_x_ipsecrequest_reqid)
  2172. return -EINVAL;
  2173. m->proto = rq1->sadb_x_ipsecrequest_proto;
  2174. if ((mode = pfkey_mode_to_xfrm(rq1->sadb_x_ipsecrequest_mode)) < 0)
  2175. return -EINVAL;
  2176. m->mode = mode;
  2177. m->reqid = rq1->sadb_x_ipsecrequest_reqid;
  2178. return ((int)(rq1->sadb_x_ipsecrequest_len +
  2179. rq2->sadb_x_ipsecrequest_len));
  2180. }
  2181. static int pfkey_migrate(struct sock *sk, struct sk_buff *skb,
  2182. const struct sadb_msg *hdr, void * const *ext_hdrs)
  2183. {
  2184. int i, len, ret, err = -EINVAL;
  2185. u8 dir;
  2186. struct sadb_address *sa;
  2187. struct sadb_x_kmaddress *kma;
  2188. struct sadb_x_policy *pol;
  2189. struct sadb_x_ipsecrequest *rq;
  2190. struct xfrm_selector sel;
  2191. struct xfrm_migrate m[XFRM_MAX_DEPTH];
  2192. struct xfrm_kmaddress k;
  2193. struct net *net = sock_net(sk);
  2194. if (!present_and_same_family(ext_hdrs[SADB_EXT_ADDRESS_SRC - 1],
  2195. ext_hdrs[SADB_EXT_ADDRESS_DST - 1]) ||
  2196. !ext_hdrs[SADB_X_EXT_POLICY - 1]) {
  2197. err = -EINVAL;
  2198. goto out;
  2199. }
  2200. kma = ext_hdrs[SADB_X_EXT_KMADDRESS - 1];
  2201. pol = ext_hdrs[SADB_X_EXT_POLICY - 1];
  2202. if (pol->sadb_x_policy_dir >= IPSEC_DIR_MAX) {
  2203. err = -EINVAL;
  2204. goto out;
  2205. }
  2206. if (kma) {
  2207. /* convert sadb_x_kmaddress to xfrm_kmaddress */
  2208. k.reserved = kma->sadb_x_kmaddress_reserved;
  2209. ret = parse_sockaddr_pair((struct sockaddr *)(kma + 1),
  2210. 8*(kma->sadb_x_kmaddress_len) - sizeof(*kma),
  2211. &k.local, &k.remote, &k.family);
  2212. if (ret < 0) {
  2213. err = ret;
  2214. goto out;
  2215. }
  2216. }
  2217. dir = pol->sadb_x_policy_dir - 1;
  2218. memset(&sel, 0, sizeof(sel));
  2219. /* set source address info of selector */
  2220. sa = ext_hdrs[SADB_EXT_ADDRESS_SRC - 1];
  2221. sel.family = pfkey_sadb_addr2xfrm_addr(sa, &sel.saddr);
  2222. sel.prefixlen_s = sa->sadb_address_prefixlen;
  2223. sel.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
  2224. sel.sport = ((struct sockaddr_in *)(sa + 1))->sin_port;
  2225. if (sel.sport)
  2226. sel.sport_mask = htons(0xffff);
  2227. /* set destination address info of selector */
  2228. sa = ext_hdrs[SADB_EXT_ADDRESS_DST - 1];
  2229. pfkey_sadb_addr2xfrm_addr(sa, &sel.daddr);
  2230. sel.prefixlen_d = sa->sadb_address_prefixlen;
  2231. sel.proto = pfkey_proto_to_xfrm(sa->sadb_address_proto);
  2232. sel.dport = ((struct sockaddr_in *)(sa + 1))->sin_port;
  2233. if (sel.dport)
  2234. sel.dport_mask = htons(0xffff);
  2235. rq = (struct sadb_x_ipsecrequest *)(pol + 1);
  2236. /* extract ipsecrequests */
  2237. i = 0;
  2238. len = pol->sadb_x_policy_len * 8 - sizeof(struct sadb_x_policy);
  2239. while (len > 0 && i < XFRM_MAX_DEPTH) {
  2240. ret = ipsecrequests_to_migrate(rq, len, &m[i]);
  2241. if (ret < 0) {
  2242. err = ret;
  2243. goto out;
  2244. } else {
  2245. rq = (struct sadb_x_ipsecrequest *)((u8 *)rq + ret);
  2246. len -= ret;
  2247. i++;
  2248. }
  2249. }
  2250. if (!i || len > 0) {
  2251. err = -EINVAL;
  2252. goto out;
  2253. }
  2254. return xfrm_migrate(&sel, dir, XFRM_POLICY_TYPE_MAIN, m, i,
  2255. kma ? &k : NULL, net);
  2256. out:
  2257. return err;
  2258. }
  2259. #else
  2260. static int pfkey_migrate(struct sock *sk, struct sk_buff *skb,
  2261. const struct sadb_msg *hdr, void * const *ext_hdrs)
  2262. {
  2263. return -ENOPROTOOPT;
  2264. }
  2265. #endif
  2266. static int pfkey_spdget(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr, void * const *ext_hdrs)
  2267. {
  2268. struct net *net = sock_net(sk);
  2269. unsigned int dir;
  2270. int err = 0, delete;
  2271. struct sadb_x_policy *pol;
  2272. struct xfrm_policy *xp;
  2273. struct km_event c;
  2274. if ((pol = ext_hdrs[SADB_X_EXT_POLICY-1]) == NULL)
  2275. return -EINVAL;
  2276. dir = xfrm_policy_id2dir(pol->sadb_x_policy_id);
  2277. if (dir >= XFRM_POLICY_MAX)
  2278. return -EINVAL;
  2279. delete = (hdr->sadb_msg_type == SADB_X_SPDDELETE2);
  2280. xp = xfrm_policy_byid(net, DUMMY_MARK, XFRM_POLICY_TYPE_MAIN,
  2281. dir, pol->sadb_x_policy_id, delete, &err);
  2282. if (xp == NULL)
  2283. return -ENOENT;
  2284. if (delete) {
  2285. xfrm_audit_policy_delete(xp, err ? 0 : 1, true);
  2286. if (err)
  2287. goto out;
  2288. c.seq = hdr->sadb_msg_seq;
  2289. c.portid = hdr->sadb_msg_pid;
  2290. c.data.byid = 1;
  2291. c.event = XFRM_MSG_DELPOLICY;
  2292. km_policy_notify(xp, dir, &c);
  2293. } else {
  2294. err = key_pol_get_resp(sk, xp, hdr, dir);
  2295. }
  2296. out:
  2297. xfrm_pol_put(xp);
  2298. if (delete && err == 0)
  2299. xfrm_garbage_collect(net);
  2300. return err;
  2301. }
  2302. static int dump_sp(struct xfrm_policy *xp, int dir, int count, void *ptr)
  2303. {
  2304. struct pfkey_sock *pfk = ptr;
  2305. struct sk_buff *out_skb;
  2306. struct sadb_msg *out_hdr;
  2307. int err;
  2308. if (!pfkey_can_dump(&pfk->sk))
  2309. return -ENOBUFS;
  2310. out_skb = pfkey_xfrm_policy2msg_prep(xp);
  2311. if (IS_ERR(out_skb))
  2312. return PTR_ERR(out_skb);
  2313. err = pfkey_xfrm_policy2msg(out_skb, xp, dir);
  2314. if (err < 0)
  2315. return err;
  2316. out_hdr = (struct sadb_msg *) out_skb->data;
  2317. out_hdr->sadb_msg_version = pfk->dump.msg_version;
  2318. out_hdr->sadb_msg_type = SADB_X_SPDDUMP;
  2319. out_hdr->sadb_msg_satype = SADB_SATYPE_UNSPEC;
  2320. out_hdr->sadb_msg_errno = 0;
  2321. out_hdr->sadb_msg_seq = count + 1;
  2322. out_hdr->sadb_msg_pid = pfk->dump.msg_portid;
  2323. if (pfk->dump.skb)
  2324. pfkey_broadcast(pfk->dump.skb, GFP_ATOMIC, BROADCAST_ONE,
  2325. &pfk->sk, sock_net(&pfk->sk));
  2326. pfk->dump.skb = out_skb;
  2327. return 0;
  2328. }
  2329. static int pfkey_dump_sp(struct pfkey_sock *pfk)
  2330. {
  2331. struct net *net = sock_net(&pfk->sk);
  2332. return xfrm_policy_walk(net, &pfk->dump.u.policy, dump_sp, (void *) pfk);
  2333. }
  2334. static void pfkey_dump_sp_done(struct pfkey_sock *pfk)
  2335. {
  2336. struct net *net = sock_net((struct sock *)pfk);
  2337. xfrm_policy_walk_done(&pfk->dump.u.policy, net);
  2338. }
  2339. static int pfkey_spddump(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr, void * const *ext_hdrs)
  2340. {
  2341. struct pfkey_sock *pfk = pfkey_sk(sk);
  2342. mutex_lock(&pfk->dump_lock);
  2343. if (pfk->dump.dump != NULL) {
  2344. mutex_unlock(&pfk->dump_lock);
  2345. return -EBUSY;
  2346. }
  2347. pfk->dump.msg_version = hdr->sadb_msg_version;
  2348. pfk->dump.msg_portid = hdr->sadb_msg_pid;
  2349. pfk->dump.dump = pfkey_dump_sp;
  2350. pfk->dump.done = pfkey_dump_sp_done;
  2351. xfrm_policy_walk_init(&pfk->dump.u.policy, XFRM_POLICY_TYPE_MAIN);
  2352. mutex_unlock(&pfk->dump_lock);
  2353. return pfkey_do_dump(pfk);
  2354. }
  2355. static int key_notify_policy_flush(const struct km_event *c)
  2356. {
  2357. struct sk_buff *skb_out;
  2358. struct sadb_msg *hdr;
  2359. skb_out = alloc_skb(sizeof(struct sadb_msg) + 16, GFP_ATOMIC);
  2360. if (!skb_out)
  2361. return -ENOBUFS;
  2362. hdr = (struct sadb_msg *) skb_put(skb_out, sizeof(struct sadb_msg));
  2363. hdr->sadb_msg_type = SADB_X_SPDFLUSH;
  2364. hdr->sadb_msg_seq = c->seq;
  2365. hdr->sadb_msg_pid = c->portid;
  2366. hdr->sadb_msg_version = PF_KEY_V2;
  2367. hdr->sadb_msg_errno = (uint8_t) 0;
  2368. hdr->sadb_msg_satype = SADB_SATYPE_UNSPEC;
  2369. hdr->sadb_msg_len = (sizeof(struct sadb_msg) / sizeof(uint64_t));
  2370. hdr->sadb_msg_reserved = 0;
  2371. pfkey_broadcast(skb_out, GFP_ATOMIC, BROADCAST_ALL, NULL, c->net);
  2372. return 0;
  2373. }
  2374. static int pfkey_spdflush(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr, void * const *ext_hdrs)
  2375. {
  2376. struct net *net = sock_net(sk);
  2377. struct km_event c;
  2378. int err, err2;
  2379. err = xfrm_policy_flush(net, XFRM_POLICY_TYPE_MAIN, true);
  2380. err2 = unicast_flush_resp(sk, hdr);
  2381. if (err || err2) {
  2382. if (err == -ESRCH) /* empty table - old silent behavior */
  2383. return 0;
  2384. return err;
  2385. }
  2386. c.data.type = XFRM_POLICY_TYPE_MAIN;
  2387. c.event = XFRM_MSG_FLUSHPOLICY;
  2388. c.portid = hdr->sadb_msg_pid;
  2389. c.seq = hdr->sadb_msg_seq;
  2390. c.net = net;
  2391. km_policy_notify(NULL, 0, &c);
  2392. return 0;
  2393. }
  2394. typedef int (*pfkey_handler)(struct sock *sk, struct sk_buff *skb,
  2395. const struct sadb_msg *hdr, void * const *ext_hdrs);
  2396. static const pfkey_handler pfkey_funcs[SADB_MAX + 1] = {
  2397. [SADB_RESERVED] = pfkey_reserved,
  2398. [SADB_GETSPI] = pfkey_getspi,
  2399. [SADB_UPDATE] = pfkey_add,
  2400. [SADB_ADD] = pfkey_add,
  2401. [SADB_DELETE] = pfkey_delete,
  2402. [SADB_GET] = pfkey_get,
  2403. [SADB_ACQUIRE] = pfkey_acquire,
  2404. [SADB_REGISTER] = pfkey_register,
  2405. [SADB_EXPIRE] = NULL,
  2406. [SADB_FLUSH] = pfkey_flush,
  2407. [SADB_DUMP] = pfkey_dump,
  2408. [SADB_X_PROMISC] = pfkey_promisc,
  2409. [SADB_X_PCHANGE] = NULL,
  2410. [SADB_X_SPDUPDATE] = pfkey_spdadd,
  2411. [SADB_X_SPDADD] = pfkey_spdadd,
  2412. [SADB_X_SPDDELETE] = pfkey_spddelete,
  2413. [SADB_X_SPDGET] = pfkey_spdget,
  2414. [SADB_X_SPDACQUIRE] = NULL,
  2415. [SADB_X_SPDDUMP] = pfkey_spddump,
  2416. [SADB_X_SPDFLUSH] = pfkey_spdflush,
  2417. [SADB_X_SPDSETIDX] = pfkey_spdadd,
  2418. [SADB_X_SPDDELETE2] = pfkey_spdget,
  2419. [SADB_X_MIGRATE] = pfkey_migrate,
  2420. };
  2421. static int pfkey_process(struct sock *sk, struct sk_buff *skb, const struct sadb_msg *hdr)
  2422. {
  2423. void *ext_hdrs[SADB_EXT_MAX];
  2424. int err;
  2425. pfkey_broadcast(skb_clone(skb, GFP_KERNEL), GFP_KERNEL,
  2426. BROADCAST_PROMISC_ONLY, NULL, sock_net(sk));
  2427. memset(ext_hdrs, 0, sizeof(ext_hdrs));
  2428. err = parse_exthdrs(skb, hdr, ext_hdrs);
  2429. if (!err) {
  2430. err = -EOPNOTSUPP;
  2431. if (pfkey_funcs[hdr->sadb_msg_type])
  2432. err = pfkey_funcs[hdr->sadb_msg_type](sk, skb, hdr, ext_hdrs);
  2433. }
  2434. return err;
  2435. }
  2436. static struct sadb_msg *pfkey_get_base_msg(struct sk_buff *skb, int *errp)
  2437. {
  2438. struct sadb_msg *hdr = NULL;
  2439. if (skb->len < sizeof(*hdr)) {
  2440. *errp = -EMSGSIZE;
  2441. } else {
  2442. hdr = (struct sadb_msg *) skb->data;
  2443. if (hdr->sadb_msg_version != PF_KEY_V2 ||
  2444. hdr->sadb_msg_reserved != 0 ||
  2445. (hdr->sadb_msg_type <= SADB_RESERVED ||
  2446. hdr->sadb_msg_type > SADB_MAX)) {
  2447. hdr = NULL;
  2448. *errp = -EINVAL;
  2449. } else if (hdr->sadb_msg_len != (skb->len /
  2450. sizeof(uint64_t)) ||
  2451. hdr->sadb_msg_len < (sizeof(struct sadb_msg) /
  2452. sizeof(uint64_t))) {
  2453. hdr = NULL;
  2454. *errp = -EMSGSIZE;
  2455. } else {
  2456. *errp = 0;
  2457. }
  2458. }
  2459. return hdr;
  2460. }
  2461. static inline int aalg_tmpl_set(const struct xfrm_tmpl *t,
  2462. const struct xfrm_algo_desc *d)
  2463. {
  2464. unsigned int id = d->desc.sadb_alg_id;
  2465. if (id >= sizeof(t->aalgos) * 8)
  2466. return 0;
  2467. return (t->aalgos >> id) & 1;
  2468. }
  2469. static inline int ealg_tmpl_set(const struct xfrm_tmpl *t,
  2470. const struct xfrm_algo_desc *d)
  2471. {
  2472. unsigned int id = d->desc.sadb_alg_id;
  2473. if (id >= sizeof(t->ealgos) * 8)
  2474. return 0;
  2475. return (t->ealgos >> id) & 1;
  2476. }
  2477. static int count_ah_combs(const struct xfrm_tmpl *t)
  2478. {
  2479. int i, sz = 0;
  2480. for (i = 0; ; i++) {
  2481. const struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(i);
  2482. if (!aalg)
  2483. break;
  2484. if (!aalg->pfkey_supported)
  2485. continue;
  2486. if (aalg_tmpl_set(t, aalg) && aalg->available)
  2487. sz += sizeof(struct sadb_comb);
  2488. }
  2489. return sz + sizeof(struct sadb_prop);
  2490. }
  2491. static int count_esp_combs(const struct xfrm_tmpl *t)
  2492. {
  2493. int i, k, sz = 0;
  2494. for (i = 0; ; i++) {
  2495. const struct xfrm_algo_desc *ealg = xfrm_ealg_get_byidx(i);
  2496. if (!ealg)
  2497. break;
  2498. if (!ealg->pfkey_supported)
  2499. continue;
  2500. if (!(ealg_tmpl_set(t, ealg) && ealg->available))
  2501. continue;
  2502. for (k = 1; ; k++) {
  2503. const struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(k);
  2504. if (!aalg)
  2505. break;
  2506. if (!aalg->pfkey_supported)
  2507. continue;
  2508. if (aalg_tmpl_set(t, aalg) && aalg->available)
  2509. sz += sizeof(struct sadb_comb);
  2510. }
  2511. }
  2512. return sz + sizeof(struct sadb_prop);
  2513. }
  2514. static void dump_ah_combs(struct sk_buff *skb, const struct xfrm_tmpl *t)
  2515. {
  2516. struct sadb_prop *p;
  2517. int i;
  2518. p = (struct sadb_prop*)skb_put(skb, sizeof(struct sadb_prop));
  2519. p->sadb_prop_len = sizeof(struct sadb_prop)/8;
  2520. p->sadb_prop_exttype = SADB_EXT_PROPOSAL;
  2521. p->sadb_prop_replay = 32;
  2522. memset(p->sadb_prop_reserved, 0, sizeof(p->sadb_prop_reserved));
  2523. for (i = 0; ; i++) {
  2524. const struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(i);
  2525. if (!aalg)
  2526. break;
  2527. if (!aalg->pfkey_supported)
  2528. continue;
  2529. if (aalg_tmpl_set(t, aalg) && aalg->available) {
  2530. struct sadb_comb *c;
  2531. c = (struct sadb_comb*)skb_put(skb, sizeof(struct sadb_comb));
  2532. memset(c, 0, sizeof(*c));
  2533. p->sadb_prop_len += sizeof(struct sadb_comb)/8;
  2534. c->sadb_comb_auth = aalg->desc.sadb_alg_id;
  2535. c->sadb_comb_auth_minbits = aalg->desc.sadb_alg_minbits;
  2536. c->sadb_comb_auth_maxbits = aalg->desc.sadb_alg_maxbits;
  2537. c->sadb_comb_hard_addtime = 24*60*60;
  2538. c->sadb_comb_soft_addtime = 20*60*60;
  2539. c->sadb_comb_hard_usetime = 8*60*60;
  2540. c->sadb_comb_soft_usetime = 7*60*60;
  2541. }
  2542. }
  2543. }
  2544. static void dump_esp_combs(struct sk_buff *skb, const struct xfrm_tmpl *t)
  2545. {
  2546. struct sadb_prop *p;
  2547. int i, k;
  2548. p = (struct sadb_prop*)skb_put(skb, sizeof(struct sadb_prop));
  2549. p->sadb_prop_len = sizeof(struct sadb_prop)/8;
  2550. p->sadb_prop_exttype = SADB_EXT_PROPOSAL;
  2551. p->sadb_prop_replay = 32;
  2552. memset(p->sadb_prop_reserved, 0, sizeof(p->sadb_prop_reserved));
  2553. for (i=0; ; i++) {
  2554. const struct xfrm_algo_desc *ealg = xfrm_ealg_get_byidx(i);
  2555. if (!ealg)
  2556. break;
  2557. if (!ealg->pfkey_supported)
  2558. continue;
  2559. if (!(ealg_tmpl_set(t, ealg) && ealg->available))
  2560. continue;
  2561. for (k = 1; ; k++) {
  2562. struct sadb_comb *c;
  2563. const struct xfrm_algo_desc *aalg = xfrm_aalg_get_byidx(k);
  2564. if (!aalg)
  2565. break;
  2566. if (!aalg->pfkey_supported)
  2567. continue;
  2568. if (!(aalg_tmpl_set(t, aalg) && aalg->available))
  2569. continue;
  2570. c = (struct sadb_comb*)skb_put(skb, sizeof(struct sadb_comb));
  2571. memset(c, 0, sizeof(*c));
  2572. p->sadb_prop_len += sizeof(struct sadb_comb)/8;
  2573. c->sadb_comb_auth = aalg->desc.sadb_alg_id;
  2574. c->sadb_comb_auth_minbits = aalg->desc.sadb_alg_minbits;
  2575. c->sadb_comb_auth_maxbits = aalg->desc.sadb_alg_maxbits;
  2576. c->sadb_comb_encrypt = ealg->desc.sadb_alg_id;
  2577. c->sadb_comb_encrypt_minbits = ealg->desc.sadb_alg_minbits;
  2578. c->sadb_comb_encrypt_maxbits = ealg->desc.sadb_alg_maxbits;
  2579. c->sadb_comb_hard_addtime = 24*60*60;
  2580. c->sadb_comb_soft_addtime = 20*60*60;
  2581. c->sadb_comb_hard_usetime = 8*60*60;
  2582. c->sadb_comb_soft_usetime = 7*60*60;
  2583. }
  2584. }
  2585. }
  2586. static int key_notify_policy_expire(struct xfrm_policy *xp, const struct km_event *c)
  2587. {
  2588. return 0;
  2589. }
  2590. static int key_notify_sa_expire(struct xfrm_state *x, const struct km_event *c)
  2591. {
  2592. struct sk_buff *out_skb;
  2593. struct sadb_msg *out_hdr;
  2594. int hard;
  2595. int hsc;
  2596. hard = c->data.hard;
  2597. if (hard)
  2598. hsc = 2;
  2599. else
  2600. hsc = 1;
  2601. out_skb = pfkey_xfrm_state2msg_expire(x, hsc);
  2602. if (IS_ERR(out_skb))
  2603. return PTR_ERR(out_skb);
  2604. out_hdr = (struct sadb_msg *) out_skb->data;
  2605. out_hdr->sadb_msg_version = PF_KEY_V2;
  2606. out_hdr->sadb_msg_type = SADB_EXPIRE;
  2607. out_hdr->sadb_msg_satype = pfkey_proto2satype(x->id.proto);
  2608. out_hdr->sadb_msg_errno = 0;
  2609. out_hdr->sadb_msg_reserved = 0;
  2610. out_hdr->sadb_msg_seq = 0;
  2611. out_hdr->sadb_msg_pid = 0;
  2612. pfkey_broadcast(out_skb, GFP_ATOMIC, BROADCAST_REGISTERED, NULL,
  2613. xs_net(x));
  2614. return 0;
  2615. }
  2616. static int pfkey_send_notify(struct xfrm_state *x, const struct km_event *c)
  2617. {
  2618. struct net *net = x ? xs_net(x) : c->net;
  2619. struct netns_pfkey *net_pfkey = net_generic(net, pfkey_net_id);
  2620. if (atomic_read(&net_pfkey->socks_nr) == 0)
  2621. return 0;
  2622. switch (c->event) {
  2623. case XFRM_MSG_EXPIRE:
  2624. return key_notify_sa_expire(x, c);
  2625. case XFRM_MSG_DELSA:
  2626. case XFRM_MSG_NEWSA:
  2627. case XFRM_MSG_UPDSA:
  2628. return key_notify_sa(x, c);
  2629. case XFRM_MSG_FLUSHSA:
  2630. return key_notify_sa_flush(c);
  2631. case XFRM_MSG_NEWAE: /* not yet supported */
  2632. break;
  2633. default:
  2634. pr_err("pfkey: Unknown SA event %d\n", c->event);
  2635. break;
  2636. }
  2637. return 0;
  2638. }
  2639. static int pfkey_send_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
  2640. {
  2641. if (xp && xp->type != XFRM_POLICY_TYPE_MAIN)
  2642. return 0;
  2643. switch (c->event) {
  2644. case XFRM_MSG_POLEXPIRE:
  2645. return key_notify_policy_expire(xp, c);
  2646. case XFRM_MSG_DELPOLICY:
  2647. case XFRM_MSG_NEWPOLICY:
  2648. case XFRM_MSG_UPDPOLICY:
  2649. return key_notify_policy(xp, dir, c);
  2650. case XFRM_MSG_FLUSHPOLICY:
  2651. if (c->data.type != XFRM_POLICY_TYPE_MAIN)
  2652. break;
  2653. return key_notify_policy_flush(c);
  2654. default:
  2655. pr_err("pfkey: Unknown policy event %d\n", c->event);
  2656. break;
  2657. }
  2658. return 0;
  2659. }
  2660. static u32 get_acqseq(void)
  2661. {
  2662. u32 res;
  2663. static atomic_t acqseq;
  2664. do {
  2665. res = atomic_inc_return(&acqseq);
  2666. } while (!res);
  2667. return res;
  2668. }
  2669. static bool pfkey_is_alive(const struct km_event *c)
  2670. {
  2671. struct netns_pfkey *net_pfkey = net_generic(c->net, pfkey_net_id);
  2672. struct sock *sk;
  2673. bool is_alive = false;
  2674. rcu_read_lock();
  2675. sk_for_each_rcu(sk, &net_pfkey->table) {
  2676. if (pfkey_sk(sk)->registered) {
  2677. is_alive = true;
  2678. break;
  2679. }
  2680. }
  2681. rcu_read_unlock();
  2682. return is_alive;
  2683. }
  2684. static int pfkey_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *t, struct xfrm_policy *xp)
  2685. {
  2686. struct sk_buff *skb;
  2687. struct sadb_msg *hdr;
  2688. struct sadb_address *addr;
  2689. struct sadb_x_policy *pol;
  2690. int sockaddr_size;
  2691. int size;
  2692. struct sadb_x_sec_ctx *sec_ctx;
  2693. struct xfrm_sec_ctx *xfrm_ctx;
  2694. int ctx_size = 0;
  2695. sockaddr_size = pfkey_sockaddr_size(x->props.family);
  2696. if (!sockaddr_size)
  2697. return -EINVAL;
  2698. size = sizeof(struct sadb_msg) +
  2699. (sizeof(struct sadb_address) * 2) +
  2700. (sockaddr_size * 2) +
  2701. sizeof(struct sadb_x_policy);
  2702. if (x->id.proto == IPPROTO_AH)
  2703. size += count_ah_combs(t);
  2704. else if (x->id.proto == IPPROTO_ESP)
  2705. size += count_esp_combs(t);
  2706. if ((xfrm_ctx = x->security)) {
  2707. ctx_size = PFKEY_ALIGN8(xfrm_ctx->ctx_len);
  2708. size += sizeof(struct sadb_x_sec_ctx) + ctx_size;
  2709. }
  2710. skb = alloc_skb(size + 16, GFP_ATOMIC);
  2711. if (skb == NULL)
  2712. return -ENOMEM;
  2713. hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
  2714. hdr->sadb_msg_version = PF_KEY_V2;
  2715. hdr->sadb_msg_type = SADB_ACQUIRE;
  2716. hdr->sadb_msg_satype = pfkey_proto2satype(x->id.proto);
  2717. hdr->sadb_msg_len = size / sizeof(uint64_t);
  2718. hdr->sadb_msg_errno = 0;
  2719. hdr->sadb_msg_reserved = 0;
  2720. hdr->sadb_msg_seq = x->km.seq = get_acqseq();
  2721. hdr->sadb_msg_pid = 0;
  2722. /* src address */
  2723. addr = (struct sadb_address*) skb_put(skb,
  2724. sizeof(struct sadb_address)+sockaddr_size);
  2725. addr->sadb_address_len =
  2726. (sizeof(struct sadb_address)+sockaddr_size)/
  2727. sizeof(uint64_t);
  2728. addr->sadb_address_exttype = SADB_EXT_ADDRESS_SRC;
  2729. addr->sadb_address_proto = 0;
  2730. addr->sadb_address_reserved = 0;
  2731. addr->sadb_address_prefixlen =
  2732. pfkey_sockaddr_fill(&x->props.saddr, 0,
  2733. (struct sockaddr *) (addr + 1),
  2734. x->props.family);
  2735. if (!addr->sadb_address_prefixlen)
  2736. BUG();
  2737. /* dst address */
  2738. addr = (struct sadb_address*) skb_put(skb,
  2739. sizeof(struct sadb_address)+sockaddr_size);
  2740. addr->sadb_address_len =
  2741. (sizeof(struct sadb_address)+sockaddr_size)/
  2742. sizeof(uint64_t);
  2743. addr->sadb_address_exttype = SADB_EXT_ADDRESS_DST;
  2744. addr->sadb_address_proto = 0;
  2745. addr->sadb_address_reserved = 0;
  2746. addr->sadb_address_prefixlen =
  2747. pfkey_sockaddr_fill(&x->id.daddr, 0,
  2748. (struct sockaddr *) (addr + 1),
  2749. x->props.family);
  2750. if (!addr->sadb_address_prefixlen)
  2751. BUG();
  2752. pol = (struct sadb_x_policy *) skb_put(skb, sizeof(struct sadb_x_policy));
  2753. pol->sadb_x_policy_len = sizeof(struct sadb_x_policy)/sizeof(uint64_t);
  2754. pol->sadb_x_policy_exttype = SADB_X_EXT_POLICY;
  2755. pol->sadb_x_policy_type = IPSEC_POLICY_IPSEC;
  2756. pol->sadb_x_policy_dir = XFRM_POLICY_OUT + 1;
  2757. pol->sadb_x_policy_reserved = 0;
  2758. pol->sadb_x_policy_id = xp->index;
  2759. pol->sadb_x_policy_priority = xp->priority;
  2760. /* Set sadb_comb's. */
  2761. if (x->id.proto == IPPROTO_AH)
  2762. dump_ah_combs(skb, t);
  2763. else if (x->id.proto == IPPROTO_ESP)
  2764. dump_esp_combs(skb, t);
  2765. /* security context */
  2766. if (xfrm_ctx) {
  2767. sec_ctx = (struct sadb_x_sec_ctx *) skb_put(skb,
  2768. sizeof(struct sadb_x_sec_ctx) + ctx_size);
  2769. sec_ctx->sadb_x_sec_len =
  2770. (sizeof(struct sadb_x_sec_ctx) + ctx_size) / sizeof(uint64_t);
  2771. sec_ctx->sadb_x_sec_exttype = SADB_X_EXT_SEC_CTX;
  2772. sec_ctx->sadb_x_ctx_doi = xfrm_ctx->ctx_doi;
  2773. sec_ctx->sadb_x_ctx_alg = xfrm_ctx->ctx_alg;
  2774. sec_ctx->sadb_x_ctx_len = xfrm_ctx->ctx_len;
  2775. memcpy(sec_ctx + 1, xfrm_ctx->ctx_str,
  2776. xfrm_ctx->ctx_len);
  2777. }
  2778. return pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_REGISTERED, NULL,
  2779. xs_net(x));
  2780. }
  2781. static struct xfrm_policy *pfkey_compile_policy(struct sock *sk, int opt,
  2782. u8 *data, int len, int *dir)
  2783. {
  2784. struct net *net = sock_net(sk);
  2785. struct xfrm_policy *xp;
  2786. struct sadb_x_policy *pol = (struct sadb_x_policy*)data;
  2787. struct sadb_x_sec_ctx *sec_ctx;
  2788. switch (sk->sk_family) {
  2789. case AF_INET:
  2790. if (opt != IP_IPSEC_POLICY) {
  2791. *dir = -EOPNOTSUPP;
  2792. return NULL;
  2793. }
  2794. break;
  2795. #if IS_ENABLED(CONFIG_IPV6)
  2796. case AF_INET6:
  2797. if (opt != IPV6_IPSEC_POLICY) {
  2798. *dir = -EOPNOTSUPP;
  2799. return NULL;
  2800. }
  2801. break;
  2802. #endif
  2803. default:
  2804. *dir = -EINVAL;
  2805. return NULL;
  2806. }
  2807. *dir = -EINVAL;
  2808. if (len < sizeof(struct sadb_x_policy) ||
  2809. pol->sadb_x_policy_len*8 > len ||
  2810. pol->sadb_x_policy_type > IPSEC_POLICY_BYPASS ||
  2811. (!pol->sadb_x_policy_dir || pol->sadb_x_policy_dir > IPSEC_DIR_OUTBOUND))
  2812. return NULL;
  2813. xp = xfrm_policy_alloc(net, GFP_ATOMIC);
  2814. if (xp == NULL) {
  2815. *dir = -ENOBUFS;
  2816. return NULL;
  2817. }
  2818. xp->action = (pol->sadb_x_policy_type == IPSEC_POLICY_DISCARD ?
  2819. XFRM_POLICY_BLOCK : XFRM_POLICY_ALLOW);
  2820. xp->lft.soft_byte_limit = XFRM_INF;
  2821. xp->lft.hard_byte_limit = XFRM_INF;
  2822. xp->lft.soft_packet_limit = XFRM_INF;
  2823. xp->lft.hard_packet_limit = XFRM_INF;
  2824. xp->family = sk->sk_family;
  2825. xp->xfrm_nr = 0;
  2826. if (pol->sadb_x_policy_type == IPSEC_POLICY_IPSEC &&
  2827. (*dir = parse_ipsecrequests(xp, pol)) < 0)
  2828. goto out;
  2829. /* security context too */
  2830. if (len >= (pol->sadb_x_policy_len*8 +
  2831. sizeof(struct sadb_x_sec_ctx))) {
  2832. char *p = (char *)pol;
  2833. struct xfrm_user_sec_ctx *uctx;
  2834. p += pol->sadb_x_policy_len*8;
  2835. sec_ctx = (struct sadb_x_sec_ctx *)p;
  2836. if (len < pol->sadb_x_policy_len*8 +
  2837. sec_ctx->sadb_x_sec_len*8) {
  2838. *dir = -EINVAL;
  2839. goto out;
  2840. }
  2841. if ((*dir = verify_sec_ctx_len(p)))
  2842. goto out;
  2843. uctx = pfkey_sadb2xfrm_user_sec_ctx(sec_ctx, GFP_ATOMIC);
  2844. *dir = security_xfrm_policy_alloc(&xp->security, uctx, GFP_ATOMIC);
  2845. kfree(uctx);
  2846. if (*dir)
  2847. goto out;
  2848. }
  2849. *dir = pol->sadb_x_policy_dir-1;
  2850. return xp;
  2851. out:
  2852. xp->walk.dead = 1;
  2853. xfrm_policy_destroy(xp);
  2854. return NULL;
  2855. }
  2856. static int pfkey_send_new_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr, __be16 sport)
  2857. {
  2858. struct sk_buff *skb;
  2859. struct sadb_msg *hdr;
  2860. struct sadb_sa *sa;
  2861. struct sadb_address *addr;
  2862. struct sadb_x_nat_t_port *n_port;
  2863. int sockaddr_size;
  2864. int size;
  2865. __u8 satype = (x->id.proto == IPPROTO_ESP ? SADB_SATYPE_ESP : 0);
  2866. struct xfrm_encap_tmpl *natt = NULL;
  2867. sockaddr_size = pfkey_sockaddr_size(x->props.family);
  2868. if (!sockaddr_size)
  2869. return -EINVAL;
  2870. if (!satype)
  2871. return -EINVAL;
  2872. if (!x->encap)
  2873. return -EINVAL;
  2874. natt = x->encap;
  2875. /* Build an SADB_X_NAT_T_NEW_MAPPING message:
  2876. *
  2877. * HDR | SA | ADDRESS_SRC (old addr) | NAT_T_SPORT (old port) |
  2878. * ADDRESS_DST (new addr) | NAT_T_DPORT (new port)
  2879. */
  2880. size = sizeof(struct sadb_msg) +
  2881. sizeof(struct sadb_sa) +
  2882. (sizeof(struct sadb_address) * 2) +
  2883. (sockaddr_size * 2) +
  2884. (sizeof(struct sadb_x_nat_t_port) * 2);
  2885. skb = alloc_skb(size + 16, GFP_ATOMIC);
  2886. if (skb == NULL)
  2887. return -ENOMEM;
  2888. hdr = (struct sadb_msg *) skb_put(skb, sizeof(struct sadb_msg));
  2889. hdr->sadb_msg_version = PF_KEY_V2;
  2890. hdr->sadb_msg_type = SADB_X_NAT_T_NEW_MAPPING;
  2891. hdr->sadb_msg_satype = satype;
  2892. hdr->sadb_msg_len = size / sizeof(uint64_t);
  2893. hdr->sadb_msg_errno = 0;
  2894. hdr->sadb_msg_reserved = 0;
  2895. hdr->sadb_msg_seq = x->km.seq = get_acqseq();
  2896. hdr->sadb_msg_pid = 0;
  2897. /* SA */
  2898. sa = (struct sadb_sa *) skb_put(skb, sizeof(struct sadb_sa));
  2899. sa->sadb_sa_len = sizeof(struct sadb_sa)/sizeof(uint64_t);
  2900. sa->sadb_sa_exttype = SADB_EXT_SA;
  2901. sa->sadb_sa_spi = x->id.spi;
  2902. sa->sadb_sa_replay = 0;
  2903. sa->sadb_sa_state = 0;
  2904. sa->sadb_sa_auth = 0;
  2905. sa->sadb_sa_encrypt = 0;
  2906. sa->sadb_sa_flags = 0;
  2907. /* ADDRESS_SRC (old addr) */
  2908. addr = (struct sadb_address*)
  2909. skb_put(skb, sizeof(struct sadb_address)+sockaddr_size);
  2910. addr->sadb_address_len =
  2911. (sizeof(struct sadb_address)+sockaddr_size)/
  2912. sizeof(uint64_t);
  2913. addr->sadb_address_exttype = SADB_EXT_ADDRESS_SRC;
  2914. addr->sadb_address_proto = 0;
  2915. addr->sadb_address_reserved = 0;
  2916. addr->sadb_address_prefixlen =
  2917. pfkey_sockaddr_fill(&x->props.saddr, 0,
  2918. (struct sockaddr *) (addr + 1),
  2919. x->props.family);
  2920. if (!addr->sadb_address_prefixlen)
  2921. BUG();
  2922. /* NAT_T_SPORT (old port) */
  2923. n_port = (struct sadb_x_nat_t_port*) skb_put(skb, sizeof (*n_port));
  2924. n_port->sadb_x_nat_t_port_len = sizeof(*n_port)/sizeof(uint64_t);
  2925. n_port->sadb_x_nat_t_port_exttype = SADB_X_EXT_NAT_T_SPORT;
  2926. n_port->sadb_x_nat_t_port_port = natt->encap_sport;
  2927. n_port->sadb_x_nat_t_port_reserved = 0;
  2928. /* ADDRESS_DST (new addr) */
  2929. addr = (struct sadb_address*)
  2930. skb_put(skb, sizeof(struct sadb_address)+sockaddr_size);
  2931. addr->sadb_address_len =
  2932. (sizeof(struct sadb_address)+sockaddr_size)/
  2933. sizeof(uint64_t);
  2934. addr->sadb_address_exttype = SADB_EXT_ADDRESS_DST;
  2935. addr->sadb_address_proto = 0;
  2936. addr->sadb_address_reserved = 0;
  2937. addr->sadb_address_prefixlen =
  2938. pfkey_sockaddr_fill(ipaddr, 0,
  2939. (struct sockaddr *) (addr + 1),
  2940. x->props.family);
  2941. if (!addr->sadb_address_prefixlen)
  2942. BUG();
  2943. /* NAT_T_DPORT (new port) */
  2944. n_port = (struct sadb_x_nat_t_port*) skb_put(skb, sizeof (*n_port));
  2945. n_port->sadb_x_nat_t_port_len = sizeof(*n_port)/sizeof(uint64_t);
  2946. n_port->sadb_x_nat_t_port_exttype = SADB_X_EXT_NAT_T_DPORT;
  2947. n_port->sadb_x_nat_t_port_port = sport;
  2948. n_port->sadb_x_nat_t_port_reserved = 0;
  2949. return pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_REGISTERED, NULL,
  2950. xs_net(x));
  2951. }
  2952. #ifdef CONFIG_NET_KEY_MIGRATE
  2953. static int set_sadb_address(struct sk_buff *skb, int sasize, int type,
  2954. const struct xfrm_selector *sel)
  2955. {
  2956. struct sadb_address *addr;
  2957. addr = (struct sadb_address *)skb_put(skb, sizeof(struct sadb_address) + sasize);
  2958. addr->sadb_address_len = (sizeof(struct sadb_address) + sasize)/8;
  2959. addr->sadb_address_exttype = type;
  2960. addr->sadb_address_proto = sel->proto;
  2961. addr->sadb_address_reserved = 0;
  2962. switch (type) {
  2963. case SADB_EXT_ADDRESS_SRC:
  2964. addr->sadb_address_prefixlen = sel->prefixlen_s;
  2965. pfkey_sockaddr_fill(&sel->saddr, 0,
  2966. (struct sockaddr *)(addr + 1),
  2967. sel->family);
  2968. break;
  2969. case SADB_EXT_ADDRESS_DST:
  2970. addr->sadb_address_prefixlen = sel->prefixlen_d;
  2971. pfkey_sockaddr_fill(&sel->daddr, 0,
  2972. (struct sockaddr *)(addr + 1),
  2973. sel->family);
  2974. break;
  2975. default:
  2976. return -EINVAL;
  2977. }
  2978. return 0;
  2979. }
  2980. static int set_sadb_kmaddress(struct sk_buff *skb, const struct xfrm_kmaddress *k)
  2981. {
  2982. struct sadb_x_kmaddress *kma;
  2983. u8 *sa;
  2984. int family = k->family;
  2985. int socklen = pfkey_sockaddr_len(family);
  2986. int size_req;
  2987. size_req = (sizeof(struct sadb_x_kmaddress) +
  2988. pfkey_sockaddr_pair_size(family));
  2989. kma = (struct sadb_x_kmaddress *)skb_put(skb, size_req);
  2990. memset(kma, 0, size_req);
  2991. kma->sadb_x_kmaddress_len = size_req / 8;
  2992. kma->sadb_x_kmaddress_exttype = SADB_X_EXT_KMADDRESS;
  2993. kma->sadb_x_kmaddress_reserved = k->reserved;
  2994. sa = (u8 *)(kma + 1);
  2995. if (!pfkey_sockaddr_fill(&k->local, 0, (struct sockaddr *)sa, family) ||
  2996. !pfkey_sockaddr_fill(&k->remote, 0, (struct sockaddr *)(sa+socklen), family))
  2997. return -EINVAL;
  2998. return 0;
  2999. }
  3000. static int set_ipsecrequest(struct sk_buff *skb,
  3001. uint8_t proto, uint8_t mode, int level,
  3002. uint32_t reqid, uint8_t family,
  3003. const xfrm_address_t *src, const xfrm_address_t *dst)
  3004. {
  3005. struct sadb_x_ipsecrequest *rq;
  3006. u8 *sa;
  3007. int socklen = pfkey_sockaddr_len(family);
  3008. int size_req;
  3009. size_req = sizeof(struct sadb_x_ipsecrequest) +
  3010. pfkey_sockaddr_pair_size(family);
  3011. rq = (struct sadb_x_ipsecrequest *)skb_put(skb, size_req);
  3012. memset(rq, 0, size_req);
  3013. rq->sadb_x_ipsecrequest_len = size_req;
  3014. rq->sadb_x_ipsecrequest_proto = proto;
  3015. rq->sadb_x_ipsecrequest_mode = mode;
  3016. rq->sadb_x_ipsecrequest_level = level;
  3017. rq->sadb_x_ipsecrequest_reqid = reqid;
  3018. sa = (u8 *) (rq + 1);
  3019. if (!pfkey_sockaddr_fill(src, 0, (struct sockaddr *)sa, family) ||
  3020. !pfkey_sockaddr_fill(dst, 0, (struct sockaddr *)(sa + socklen), family))
  3021. return -EINVAL;
  3022. return 0;
  3023. }
  3024. #endif
  3025. #ifdef CONFIG_NET_KEY_MIGRATE
  3026. static int pfkey_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
  3027. const struct xfrm_migrate *m, int num_bundles,
  3028. const struct xfrm_kmaddress *k)
  3029. {
  3030. int i;
  3031. int sasize_sel;
  3032. int size = 0;
  3033. int size_pol = 0;
  3034. struct sk_buff *skb;
  3035. struct sadb_msg *hdr;
  3036. struct sadb_x_policy *pol;
  3037. const struct xfrm_migrate *mp;
  3038. if (type != XFRM_POLICY_TYPE_MAIN)
  3039. return 0;
  3040. if (num_bundles <= 0 || num_bundles > XFRM_MAX_DEPTH)
  3041. return -EINVAL;
  3042. if (k != NULL) {
  3043. /* addresses for KM */
  3044. size += PFKEY_ALIGN8(sizeof(struct sadb_x_kmaddress) +
  3045. pfkey_sockaddr_pair_size(k->family));
  3046. }
  3047. /* selector */
  3048. sasize_sel = pfkey_sockaddr_size(sel->family);
  3049. if (!sasize_sel)
  3050. return -EINVAL;
  3051. size += (sizeof(struct sadb_address) + sasize_sel) * 2;
  3052. /* policy info */
  3053. size_pol += sizeof(struct sadb_x_policy);
  3054. /* ipsecrequests */
  3055. for (i = 0, mp = m; i < num_bundles; i++, mp++) {
  3056. /* old locator pair */
  3057. size_pol += sizeof(struct sadb_x_ipsecrequest) +
  3058. pfkey_sockaddr_pair_size(mp->old_family);
  3059. /* new locator pair */
  3060. size_pol += sizeof(struct sadb_x_ipsecrequest) +
  3061. pfkey_sockaddr_pair_size(mp->new_family);
  3062. }
  3063. size += sizeof(struct sadb_msg) + size_pol;
  3064. /* alloc buffer */
  3065. skb = alloc_skb(size, GFP_ATOMIC);
  3066. if (skb == NULL)
  3067. return -ENOMEM;
  3068. hdr = (struct sadb_msg *)skb_put(skb, sizeof(struct sadb_msg));
  3069. hdr->sadb_msg_version = PF_KEY_V2;
  3070. hdr->sadb_msg_type = SADB_X_MIGRATE;
  3071. hdr->sadb_msg_satype = pfkey_proto2satype(m->proto);
  3072. hdr->sadb_msg_len = size / 8;
  3073. hdr->sadb_msg_errno = 0;
  3074. hdr->sadb_msg_reserved = 0;
  3075. hdr->sadb_msg_seq = 0;
  3076. hdr->sadb_msg_pid = 0;
  3077. /* Addresses to be used by KM for negotiation, if ext is available */
  3078. if (k != NULL && (set_sadb_kmaddress(skb, k) < 0))
  3079. goto err;
  3080. /* selector src */
  3081. set_sadb_address(skb, sasize_sel, SADB_EXT_ADDRESS_SRC, sel);
  3082. /* selector dst */
  3083. set_sadb_address(skb, sasize_sel, SADB_EXT_ADDRESS_DST, sel);
  3084. /* policy information */
  3085. pol = (struct sadb_x_policy *)skb_put(skb, sizeof(struct sadb_x_policy));
  3086. pol->sadb_x_policy_len = size_pol / 8;
  3087. pol->sadb_x_policy_exttype = SADB_X_EXT_POLICY;
  3088. pol->sadb_x_policy_type = IPSEC_POLICY_IPSEC;
  3089. pol->sadb_x_policy_dir = dir + 1;
  3090. pol->sadb_x_policy_reserved = 0;
  3091. pol->sadb_x_policy_id = 0;
  3092. pol->sadb_x_policy_priority = 0;
  3093. for (i = 0, mp = m; i < num_bundles; i++, mp++) {
  3094. /* old ipsecrequest */
  3095. int mode = pfkey_mode_from_xfrm(mp->mode);
  3096. if (mode < 0)
  3097. goto err;
  3098. if (set_ipsecrequest(skb, mp->proto, mode,
  3099. (mp->reqid ? IPSEC_LEVEL_UNIQUE : IPSEC_LEVEL_REQUIRE),
  3100. mp->reqid, mp->old_family,
  3101. &mp->old_saddr, &mp->old_daddr) < 0)
  3102. goto err;
  3103. /* new ipsecrequest */
  3104. if (set_ipsecrequest(skb, mp->proto, mode,
  3105. (mp->reqid ? IPSEC_LEVEL_UNIQUE : IPSEC_LEVEL_REQUIRE),
  3106. mp->reqid, mp->new_family,
  3107. &mp->new_saddr, &mp->new_daddr) < 0)
  3108. goto err;
  3109. }
  3110. /* broadcast migrate message to sockets */
  3111. pfkey_broadcast(skb, GFP_ATOMIC, BROADCAST_ALL, NULL, &init_net);
  3112. return 0;
  3113. err:
  3114. kfree_skb(skb);
  3115. return -EINVAL;
  3116. }
  3117. #else
  3118. static int pfkey_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
  3119. const struct xfrm_migrate *m, int num_bundles,
  3120. const struct xfrm_kmaddress *k)
  3121. {
  3122. return -ENOPROTOOPT;
  3123. }
  3124. #endif
  3125. static int pfkey_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
  3126. {
  3127. struct sock *sk = sock->sk;
  3128. struct sk_buff *skb = NULL;
  3129. struct sadb_msg *hdr = NULL;
  3130. int err;
  3131. struct net *net = sock_net(sk);
  3132. err = -EOPNOTSUPP;
  3133. if (msg->msg_flags & MSG_OOB)
  3134. goto out;
  3135. err = -EMSGSIZE;
  3136. if ((unsigned int)len > sk->sk_sndbuf - 32)
  3137. goto out;
  3138. err = -ENOBUFS;
  3139. skb = alloc_skb(len, GFP_KERNEL);
  3140. if (skb == NULL)
  3141. goto out;
  3142. err = -EFAULT;
  3143. if (memcpy_from_msg(skb_put(skb,len), msg, len))
  3144. goto out;
  3145. hdr = pfkey_get_base_msg(skb, &err);
  3146. if (!hdr)
  3147. goto out;
  3148. mutex_lock(&net->xfrm.xfrm_cfg_mutex);
  3149. err = pfkey_process(sk, skb, hdr);
  3150. mutex_unlock(&net->xfrm.xfrm_cfg_mutex);
  3151. out:
  3152. if (err && hdr && pfkey_error(hdr, err, sk) == 0)
  3153. err = 0;
  3154. kfree_skb(skb);
  3155. return err ? : len;
  3156. }
  3157. static int pfkey_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
  3158. int flags)
  3159. {
  3160. struct sock *sk = sock->sk;
  3161. struct pfkey_sock *pfk = pfkey_sk(sk);
  3162. struct sk_buff *skb;
  3163. int copied, err;
  3164. err = -EINVAL;
  3165. if (flags & ~(MSG_PEEK|MSG_DONTWAIT|MSG_TRUNC|MSG_CMSG_COMPAT))
  3166. goto out;
  3167. skb = skb_recv_datagram(sk, flags, flags & MSG_DONTWAIT, &err);
  3168. if (skb == NULL)
  3169. goto out;
  3170. copied = skb->len;
  3171. if (copied > len) {
  3172. msg->msg_flags |= MSG_TRUNC;
  3173. copied = len;
  3174. }
  3175. skb_reset_transport_header(skb);
  3176. err = skb_copy_datagram_msg(skb, 0, msg, copied);
  3177. if (err)
  3178. goto out_free;
  3179. sock_recv_ts_and_drops(msg, sk, skb);
  3180. err = (flags & MSG_TRUNC) ? skb->len : copied;
  3181. if (pfk->dump.dump != NULL &&
  3182. 3 * atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf)
  3183. pfkey_do_dump(pfk);
  3184. out_free:
  3185. skb_free_datagram(sk, skb);
  3186. out:
  3187. return err;
  3188. }
  3189. static const struct proto_ops pfkey_ops = {
  3190. .family = PF_KEY,
  3191. .owner = THIS_MODULE,
  3192. /* Operations that make no sense on pfkey sockets. */
  3193. .bind = sock_no_bind,
  3194. .connect = sock_no_connect,
  3195. .socketpair = sock_no_socketpair,
  3196. .accept = sock_no_accept,
  3197. .getname = sock_no_getname,
  3198. .ioctl = sock_no_ioctl,
  3199. .listen = sock_no_listen,
  3200. .shutdown = sock_no_shutdown,
  3201. .setsockopt = sock_no_setsockopt,
  3202. .getsockopt = sock_no_getsockopt,
  3203. .mmap = sock_no_mmap,
  3204. .sendpage = sock_no_sendpage,
  3205. /* Now the operations that really occur. */
  3206. .release = pfkey_release,
  3207. .poll = datagram_poll,
  3208. .sendmsg = pfkey_sendmsg,
  3209. .recvmsg = pfkey_recvmsg,
  3210. };
  3211. static const struct net_proto_family pfkey_family_ops = {
  3212. .family = PF_KEY,
  3213. .create = pfkey_create,
  3214. .owner = THIS_MODULE,
  3215. };
  3216. #ifdef CONFIG_PROC_FS
  3217. static int pfkey_seq_show(struct seq_file *f, void *v)
  3218. {
  3219. struct sock *s = sk_entry(v);
  3220. if (v == SEQ_START_TOKEN)
  3221. seq_printf(f ,"sk RefCnt Rmem Wmem User Inode\n");
  3222. else
  3223. seq_printf(f, "%pK %-6d %-6u %-6u %-6u %-6lu\n",
  3224. s,
  3225. atomic_read(&s->sk_refcnt),
  3226. sk_rmem_alloc_get(s),
  3227. sk_wmem_alloc_get(s),
  3228. from_kuid_munged(seq_user_ns(f), sock_i_uid(s)),
  3229. sock_i_ino(s)
  3230. );
  3231. return 0;
  3232. }
  3233. static void *pfkey_seq_start(struct seq_file *f, loff_t *ppos)
  3234. __acquires(rcu)
  3235. {
  3236. struct net *net = seq_file_net(f);
  3237. struct netns_pfkey *net_pfkey = net_generic(net, pfkey_net_id);
  3238. rcu_read_lock();
  3239. return seq_hlist_start_head_rcu(&net_pfkey->table, *ppos);
  3240. }
  3241. static void *pfkey_seq_next(struct seq_file *f, void *v, loff_t *ppos)
  3242. {
  3243. struct net *net = seq_file_net(f);
  3244. struct netns_pfkey *net_pfkey = net_generic(net, pfkey_net_id);
  3245. return seq_hlist_next_rcu(v, &net_pfkey->table, ppos);
  3246. }
  3247. static void pfkey_seq_stop(struct seq_file *f, void *v)
  3248. __releases(rcu)
  3249. {
  3250. rcu_read_unlock();
  3251. }
  3252. static const struct seq_operations pfkey_seq_ops = {
  3253. .start = pfkey_seq_start,
  3254. .next = pfkey_seq_next,
  3255. .stop = pfkey_seq_stop,
  3256. .show = pfkey_seq_show,
  3257. };
  3258. static int pfkey_seq_open(struct inode *inode, struct file *file)
  3259. {
  3260. return seq_open_net(inode, file, &pfkey_seq_ops,
  3261. sizeof(struct seq_net_private));
  3262. }
  3263. static const struct file_operations pfkey_proc_ops = {
  3264. .open = pfkey_seq_open,
  3265. .read = seq_read,
  3266. .llseek = seq_lseek,
  3267. .release = seq_release_net,
  3268. };
  3269. static int __net_init pfkey_init_proc(struct net *net)
  3270. {
  3271. struct proc_dir_entry *e;
  3272. e = proc_create("pfkey", 0, net->proc_net, &pfkey_proc_ops);
  3273. if (e == NULL)
  3274. return -ENOMEM;
  3275. return 0;
  3276. }
  3277. static void __net_exit pfkey_exit_proc(struct net *net)
  3278. {
  3279. remove_proc_entry("pfkey", net->proc_net);
  3280. }
  3281. #else
  3282. static inline int pfkey_init_proc(struct net *net)
  3283. {
  3284. return 0;
  3285. }
  3286. static inline void pfkey_exit_proc(struct net *net)
  3287. {
  3288. }
  3289. #endif
  3290. static struct xfrm_mgr pfkeyv2_mgr =
  3291. {
  3292. .id = "pfkeyv2",
  3293. .notify = pfkey_send_notify,
  3294. .acquire = pfkey_send_acquire,
  3295. .compile_policy = pfkey_compile_policy,
  3296. .new_mapping = pfkey_send_new_mapping,
  3297. .notify_policy = pfkey_send_policy_notify,
  3298. .migrate = pfkey_send_migrate,
  3299. .is_alive = pfkey_is_alive,
  3300. };
  3301. static int __net_init pfkey_net_init(struct net *net)
  3302. {
  3303. struct netns_pfkey *net_pfkey = net_generic(net, pfkey_net_id);
  3304. int rv;
  3305. INIT_HLIST_HEAD(&net_pfkey->table);
  3306. atomic_set(&net_pfkey->socks_nr, 0);
  3307. rv = pfkey_init_proc(net);
  3308. return rv;
  3309. }
  3310. static void __net_exit pfkey_net_exit(struct net *net)
  3311. {
  3312. struct netns_pfkey *net_pfkey = net_generic(net, pfkey_net_id);
  3313. pfkey_exit_proc(net);
  3314. BUG_ON(!hlist_empty(&net_pfkey->table));
  3315. }
  3316. static struct pernet_operations pfkey_net_ops = {
  3317. .init = pfkey_net_init,
  3318. .exit = pfkey_net_exit,
  3319. .id = &pfkey_net_id,
  3320. .size = sizeof(struct netns_pfkey),
  3321. };
  3322. static void __exit ipsec_pfkey_exit(void)
  3323. {
  3324. xfrm_unregister_km(&pfkeyv2_mgr);
  3325. sock_unregister(PF_KEY);
  3326. unregister_pernet_subsys(&pfkey_net_ops);
  3327. proto_unregister(&key_proto);
  3328. }
  3329. static int __init ipsec_pfkey_init(void)
  3330. {
  3331. int err = proto_register(&key_proto, 0);
  3332. if (err != 0)
  3333. goto out;
  3334. err = register_pernet_subsys(&pfkey_net_ops);
  3335. if (err != 0)
  3336. goto out_unregister_key_proto;
  3337. err = sock_register(&pfkey_family_ops);
  3338. if (err != 0)
  3339. goto out_unregister_pernet;
  3340. err = xfrm_register_km(&pfkeyv2_mgr);
  3341. if (err != 0)
  3342. goto out_sock_unregister;
  3343. out:
  3344. return err;
  3345. out_sock_unregister:
  3346. sock_unregister(PF_KEY);
  3347. out_unregister_pernet:
  3348. unregister_pernet_subsys(&pfkey_net_ops);
  3349. out_unregister_key_proto:
  3350. proto_unregister(&key_proto);
  3351. goto out;
  3352. }
  3353. module_init(ipsec_pfkey_init);
  3354. module_exit(ipsec_pfkey_exit);
  3355. MODULE_LICENSE("GPL");
  3356. MODULE_ALIAS_NETPROTO(PF_KEY);