af_key.c 101 KB

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