af_key.c 100 KB

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