af_key.c 101 KB

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