af_key.c 101 KB

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