xfrm_user.c 75 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249
  1. /* xfrm_user.c: User interface to configure xfrm engine.
  2. *
  3. * Copyright (C) 2002 David S. Miller (davem@redhat.com)
  4. *
  5. * Changes:
  6. * Mitsuru KANDA @USAGI
  7. * Kazunori MIYAZAWA @USAGI
  8. * Kunihiro Ishiguro <kunihiro@ipinfusion.com>
  9. * IPv6 support
  10. *
  11. */
  12. #include <linux/crypto.h>
  13. #include <linux/module.h>
  14. #include <linux/kernel.h>
  15. #include <linux/types.h>
  16. #include <linux/slab.h>
  17. #include <linux/socket.h>
  18. #include <linux/string.h>
  19. #include <linux/net.h>
  20. #include <linux/skbuff.h>
  21. #include <linux/pfkeyv2.h>
  22. #include <linux/ipsec.h>
  23. #include <linux/init.h>
  24. #include <linux/security.h>
  25. #include <net/sock.h>
  26. #include <net/xfrm.h>
  27. #include <net/netlink.h>
  28. #include <net/ah.h>
  29. #include <linux/uaccess.h>
  30. #if IS_ENABLED(CONFIG_IPV6)
  31. #include <linux/in6.h>
  32. #endif
  33. #include <asm/unaligned.h>
  34. static int verify_one_alg(struct nlattr **attrs, enum xfrm_attr_type_t type)
  35. {
  36. struct nlattr *rt = attrs[type];
  37. struct xfrm_algo *algp;
  38. if (!rt)
  39. return 0;
  40. algp = nla_data(rt);
  41. if (nla_len(rt) < xfrm_alg_len(algp))
  42. return -EINVAL;
  43. switch (type) {
  44. case XFRMA_ALG_AUTH:
  45. case XFRMA_ALG_CRYPT:
  46. case XFRMA_ALG_COMP:
  47. break;
  48. default:
  49. return -EINVAL;
  50. }
  51. algp->alg_name[sizeof(algp->alg_name) - 1] = '\0';
  52. return 0;
  53. }
  54. static int verify_auth_trunc(struct nlattr **attrs)
  55. {
  56. struct nlattr *rt = attrs[XFRMA_ALG_AUTH_TRUNC];
  57. struct xfrm_algo_auth *algp;
  58. if (!rt)
  59. return 0;
  60. algp = nla_data(rt);
  61. if (nla_len(rt) < xfrm_alg_auth_len(algp))
  62. return -EINVAL;
  63. algp->alg_name[sizeof(algp->alg_name) - 1] = '\0';
  64. return 0;
  65. }
  66. static int verify_aead(struct nlattr **attrs)
  67. {
  68. struct nlattr *rt = attrs[XFRMA_ALG_AEAD];
  69. struct xfrm_algo_aead *algp;
  70. if (!rt)
  71. return 0;
  72. algp = nla_data(rt);
  73. if (nla_len(rt) < aead_len(algp))
  74. return -EINVAL;
  75. algp->alg_name[sizeof(algp->alg_name) - 1] = '\0';
  76. return 0;
  77. }
  78. static void verify_one_addr(struct nlattr **attrs, enum xfrm_attr_type_t type,
  79. xfrm_address_t **addrp)
  80. {
  81. struct nlattr *rt = attrs[type];
  82. if (rt && addrp)
  83. *addrp = nla_data(rt);
  84. }
  85. static inline int verify_sec_ctx_len(struct nlattr **attrs)
  86. {
  87. struct nlattr *rt = attrs[XFRMA_SEC_CTX];
  88. struct xfrm_user_sec_ctx *uctx;
  89. if (!rt)
  90. return 0;
  91. uctx = nla_data(rt);
  92. if (uctx->len != (sizeof(struct xfrm_user_sec_ctx) + uctx->ctx_len))
  93. return -EINVAL;
  94. return 0;
  95. }
  96. static inline int verify_replay(struct xfrm_usersa_info *p,
  97. struct nlattr **attrs)
  98. {
  99. struct nlattr *rt = attrs[XFRMA_REPLAY_ESN_VAL];
  100. struct xfrm_replay_state_esn *rs;
  101. if (p->flags & XFRM_STATE_ESN) {
  102. if (!rt)
  103. return -EINVAL;
  104. rs = nla_data(rt);
  105. if (rs->bmp_len > XFRMA_REPLAY_ESN_MAX / sizeof(rs->bmp[0]) / 8)
  106. return -EINVAL;
  107. if (nla_len(rt) < xfrm_replay_state_esn_len(rs) &&
  108. nla_len(rt) != sizeof(*rs))
  109. return -EINVAL;
  110. }
  111. if (!rt)
  112. return 0;
  113. /* As only ESP and AH support ESN feature. */
  114. if ((p->id.proto != IPPROTO_ESP) && (p->id.proto != IPPROTO_AH))
  115. return -EINVAL;
  116. if (p->replay_window != 0)
  117. return -EINVAL;
  118. return 0;
  119. }
  120. static int verify_newsa_info(struct xfrm_usersa_info *p,
  121. struct nlattr **attrs)
  122. {
  123. int err;
  124. err = -EINVAL;
  125. switch (p->family) {
  126. case AF_INET:
  127. break;
  128. case AF_INET6:
  129. #if IS_ENABLED(CONFIG_IPV6)
  130. break;
  131. #else
  132. err = -EAFNOSUPPORT;
  133. goto out;
  134. #endif
  135. default:
  136. goto out;
  137. }
  138. err = -EINVAL;
  139. switch (p->id.proto) {
  140. case IPPROTO_AH:
  141. if ((!attrs[XFRMA_ALG_AUTH] &&
  142. !attrs[XFRMA_ALG_AUTH_TRUNC]) ||
  143. attrs[XFRMA_ALG_AEAD] ||
  144. attrs[XFRMA_ALG_CRYPT] ||
  145. attrs[XFRMA_ALG_COMP] ||
  146. attrs[XFRMA_TFCPAD])
  147. goto out;
  148. break;
  149. case IPPROTO_ESP:
  150. if (attrs[XFRMA_ALG_COMP])
  151. goto out;
  152. if (!attrs[XFRMA_ALG_AUTH] &&
  153. !attrs[XFRMA_ALG_AUTH_TRUNC] &&
  154. !attrs[XFRMA_ALG_CRYPT] &&
  155. !attrs[XFRMA_ALG_AEAD])
  156. goto out;
  157. if ((attrs[XFRMA_ALG_AUTH] ||
  158. attrs[XFRMA_ALG_AUTH_TRUNC] ||
  159. attrs[XFRMA_ALG_CRYPT]) &&
  160. attrs[XFRMA_ALG_AEAD])
  161. goto out;
  162. if (attrs[XFRMA_TFCPAD] &&
  163. p->mode != XFRM_MODE_TUNNEL)
  164. goto out;
  165. break;
  166. case IPPROTO_COMP:
  167. if (!attrs[XFRMA_ALG_COMP] ||
  168. attrs[XFRMA_ALG_AEAD] ||
  169. attrs[XFRMA_ALG_AUTH] ||
  170. attrs[XFRMA_ALG_AUTH_TRUNC] ||
  171. attrs[XFRMA_ALG_CRYPT] ||
  172. attrs[XFRMA_TFCPAD] ||
  173. (ntohl(p->id.spi) >= 0x10000))
  174. goto out;
  175. break;
  176. #if IS_ENABLED(CONFIG_IPV6)
  177. case IPPROTO_DSTOPTS:
  178. case IPPROTO_ROUTING:
  179. if (attrs[XFRMA_ALG_COMP] ||
  180. attrs[XFRMA_ALG_AUTH] ||
  181. attrs[XFRMA_ALG_AUTH_TRUNC] ||
  182. attrs[XFRMA_ALG_AEAD] ||
  183. attrs[XFRMA_ALG_CRYPT] ||
  184. attrs[XFRMA_ENCAP] ||
  185. attrs[XFRMA_SEC_CTX] ||
  186. attrs[XFRMA_TFCPAD] ||
  187. !attrs[XFRMA_COADDR])
  188. goto out;
  189. break;
  190. #endif
  191. default:
  192. goto out;
  193. }
  194. if ((err = verify_aead(attrs)))
  195. goto out;
  196. if ((err = verify_auth_trunc(attrs)))
  197. goto out;
  198. if ((err = verify_one_alg(attrs, XFRMA_ALG_AUTH)))
  199. goto out;
  200. if ((err = verify_one_alg(attrs, XFRMA_ALG_CRYPT)))
  201. goto out;
  202. if ((err = verify_one_alg(attrs, XFRMA_ALG_COMP)))
  203. goto out;
  204. if ((err = verify_sec_ctx_len(attrs)))
  205. goto out;
  206. if ((err = verify_replay(p, attrs)))
  207. goto out;
  208. err = -EINVAL;
  209. switch (p->mode) {
  210. case XFRM_MODE_TRANSPORT:
  211. case XFRM_MODE_TUNNEL:
  212. case XFRM_MODE_ROUTEOPTIMIZATION:
  213. case XFRM_MODE_BEET:
  214. break;
  215. default:
  216. goto out;
  217. }
  218. err = 0;
  219. out:
  220. return err;
  221. }
  222. static int attach_one_algo(struct xfrm_algo **algpp, u8 *props,
  223. struct xfrm_algo_desc *(*get_byname)(const char *, int),
  224. struct nlattr *rta)
  225. {
  226. struct xfrm_algo *p, *ualg;
  227. struct xfrm_algo_desc *algo;
  228. if (!rta)
  229. return 0;
  230. ualg = nla_data(rta);
  231. algo = get_byname(ualg->alg_name, 1);
  232. if (!algo)
  233. return -ENOSYS;
  234. *props = algo->desc.sadb_alg_id;
  235. p = kmemdup(ualg, xfrm_alg_len(ualg), GFP_KERNEL);
  236. if (!p)
  237. return -ENOMEM;
  238. strcpy(p->alg_name, algo->name);
  239. *algpp = p;
  240. return 0;
  241. }
  242. static int attach_crypt(struct xfrm_state *x, struct nlattr *rta)
  243. {
  244. struct xfrm_algo *p, *ualg;
  245. struct xfrm_algo_desc *algo;
  246. if (!rta)
  247. return 0;
  248. ualg = nla_data(rta);
  249. algo = xfrm_ealg_get_byname(ualg->alg_name, 1);
  250. if (!algo)
  251. return -ENOSYS;
  252. x->props.ealgo = algo->desc.sadb_alg_id;
  253. p = kmemdup(ualg, xfrm_alg_len(ualg), GFP_KERNEL);
  254. if (!p)
  255. return -ENOMEM;
  256. strcpy(p->alg_name, algo->name);
  257. x->ealg = p;
  258. x->geniv = algo->uinfo.encr.geniv;
  259. return 0;
  260. }
  261. static int attach_auth(struct xfrm_algo_auth **algpp, u8 *props,
  262. struct nlattr *rta)
  263. {
  264. struct xfrm_algo *ualg;
  265. struct xfrm_algo_auth *p;
  266. struct xfrm_algo_desc *algo;
  267. if (!rta)
  268. return 0;
  269. ualg = nla_data(rta);
  270. algo = xfrm_aalg_get_byname(ualg->alg_name, 1);
  271. if (!algo)
  272. return -ENOSYS;
  273. *props = algo->desc.sadb_alg_id;
  274. p = kmalloc(sizeof(*p) + (ualg->alg_key_len + 7) / 8, GFP_KERNEL);
  275. if (!p)
  276. return -ENOMEM;
  277. strcpy(p->alg_name, algo->name);
  278. p->alg_key_len = ualg->alg_key_len;
  279. p->alg_trunc_len = algo->uinfo.auth.icv_truncbits;
  280. memcpy(p->alg_key, ualg->alg_key, (ualg->alg_key_len + 7) / 8);
  281. *algpp = p;
  282. return 0;
  283. }
  284. static int attach_auth_trunc(struct xfrm_algo_auth **algpp, u8 *props,
  285. struct nlattr *rta)
  286. {
  287. struct xfrm_algo_auth *p, *ualg;
  288. struct xfrm_algo_desc *algo;
  289. if (!rta)
  290. return 0;
  291. ualg = nla_data(rta);
  292. algo = xfrm_aalg_get_byname(ualg->alg_name, 1);
  293. if (!algo)
  294. return -ENOSYS;
  295. if (ualg->alg_trunc_len > algo->uinfo.auth.icv_fullbits)
  296. return -EINVAL;
  297. *props = algo->desc.sadb_alg_id;
  298. p = kmemdup(ualg, xfrm_alg_auth_len(ualg), GFP_KERNEL);
  299. if (!p)
  300. return -ENOMEM;
  301. strcpy(p->alg_name, algo->name);
  302. if (!p->alg_trunc_len)
  303. p->alg_trunc_len = algo->uinfo.auth.icv_truncbits;
  304. *algpp = p;
  305. return 0;
  306. }
  307. static int attach_aead(struct xfrm_state *x, struct nlattr *rta)
  308. {
  309. struct xfrm_algo_aead *p, *ualg;
  310. struct xfrm_algo_desc *algo;
  311. if (!rta)
  312. return 0;
  313. ualg = nla_data(rta);
  314. algo = xfrm_aead_get_byname(ualg->alg_name, ualg->alg_icv_len, 1);
  315. if (!algo)
  316. return -ENOSYS;
  317. x->props.ealgo = algo->desc.sadb_alg_id;
  318. p = kmemdup(ualg, aead_len(ualg), GFP_KERNEL);
  319. if (!p)
  320. return -ENOMEM;
  321. strcpy(p->alg_name, algo->name);
  322. x->aead = p;
  323. x->geniv = algo->uinfo.aead.geniv;
  324. return 0;
  325. }
  326. static inline int xfrm_replay_verify_len(struct xfrm_replay_state_esn *replay_esn,
  327. struct nlattr *rp)
  328. {
  329. struct xfrm_replay_state_esn *up;
  330. int ulen;
  331. if (!replay_esn || !rp)
  332. return 0;
  333. up = nla_data(rp);
  334. ulen = xfrm_replay_state_esn_len(up);
  335. /* Check the overall length and the internal bitmap length to avoid
  336. * potential overflow. */
  337. if (nla_len(rp) < ulen ||
  338. xfrm_replay_state_esn_len(replay_esn) != ulen ||
  339. replay_esn->bmp_len != up->bmp_len)
  340. return -EINVAL;
  341. if (up->replay_window > up->bmp_len * sizeof(__u32) * 8)
  342. return -EINVAL;
  343. return 0;
  344. }
  345. static int xfrm_alloc_replay_state_esn(struct xfrm_replay_state_esn **replay_esn,
  346. struct xfrm_replay_state_esn **preplay_esn,
  347. struct nlattr *rta)
  348. {
  349. struct xfrm_replay_state_esn *p, *pp, *up;
  350. int klen, ulen;
  351. if (!rta)
  352. return 0;
  353. up = nla_data(rta);
  354. klen = xfrm_replay_state_esn_len(up);
  355. ulen = nla_len(rta) >= klen ? klen : sizeof(*up);
  356. p = kzalloc(klen, GFP_KERNEL);
  357. if (!p)
  358. return -ENOMEM;
  359. pp = kzalloc(klen, GFP_KERNEL);
  360. if (!pp) {
  361. kfree(p);
  362. return -ENOMEM;
  363. }
  364. memcpy(p, up, ulen);
  365. memcpy(pp, up, ulen);
  366. *replay_esn = p;
  367. *preplay_esn = pp;
  368. return 0;
  369. }
  370. static inline int xfrm_user_sec_ctx_size(struct xfrm_sec_ctx *xfrm_ctx)
  371. {
  372. int len = 0;
  373. if (xfrm_ctx) {
  374. len += sizeof(struct xfrm_user_sec_ctx);
  375. len += xfrm_ctx->ctx_len;
  376. }
  377. return len;
  378. }
  379. static void copy_from_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
  380. {
  381. memcpy(&x->id, &p->id, sizeof(x->id));
  382. memcpy(&x->sel, &p->sel, sizeof(x->sel));
  383. memcpy(&x->lft, &p->lft, sizeof(x->lft));
  384. x->props.mode = p->mode;
  385. x->props.replay_window = min_t(unsigned int, p->replay_window,
  386. sizeof(x->replay.bitmap) * 8);
  387. x->props.reqid = p->reqid;
  388. x->props.family = p->family;
  389. memcpy(&x->props.saddr, &p->saddr, sizeof(x->props.saddr));
  390. x->props.flags = p->flags;
  391. if (!x->sel.family && !(p->flags & XFRM_STATE_AF_UNSPEC))
  392. x->sel.family = p->family;
  393. }
  394. /*
  395. * someday when pfkey also has support, we could have the code
  396. * somehow made shareable and move it to xfrm_state.c - JHS
  397. *
  398. */
  399. static void xfrm_update_ae_params(struct xfrm_state *x, struct nlattr **attrs,
  400. int update_esn)
  401. {
  402. struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
  403. struct nlattr *re = update_esn ? attrs[XFRMA_REPLAY_ESN_VAL] : NULL;
  404. struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
  405. struct nlattr *et = attrs[XFRMA_ETIMER_THRESH];
  406. struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH];
  407. if (re) {
  408. struct xfrm_replay_state_esn *replay_esn;
  409. replay_esn = nla_data(re);
  410. memcpy(x->replay_esn, replay_esn,
  411. xfrm_replay_state_esn_len(replay_esn));
  412. memcpy(x->preplay_esn, replay_esn,
  413. xfrm_replay_state_esn_len(replay_esn));
  414. }
  415. if (rp) {
  416. struct xfrm_replay_state *replay;
  417. replay = nla_data(rp);
  418. memcpy(&x->replay, replay, sizeof(*replay));
  419. memcpy(&x->preplay, replay, sizeof(*replay));
  420. }
  421. if (lt) {
  422. struct xfrm_lifetime_cur *ltime;
  423. ltime = nla_data(lt);
  424. x->curlft.bytes = ltime->bytes;
  425. x->curlft.packets = ltime->packets;
  426. x->curlft.add_time = ltime->add_time;
  427. x->curlft.use_time = ltime->use_time;
  428. }
  429. if (et)
  430. x->replay_maxage = nla_get_u32(et);
  431. if (rt)
  432. x->replay_maxdiff = nla_get_u32(rt);
  433. }
  434. static struct xfrm_state *xfrm_state_construct(struct net *net,
  435. struct xfrm_usersa_info *p,
  436. struct nlattr **attrs,
  437. int *errp)
  438. {
  439. struct xfrm_state *x = xfrm_state_alloc(net);
  440. int err = -ENOMEM;
  441. if (!x)
  442. goto error_no_put;
  443. copy_from_user_state(x, p);
  444. if (attrs[XFRMA_SA_EXTRA_FLAGS])
  445. x->props.extra_flags = nla_get_u32(attrs[XFRMA_SA_EXTRA_FLAGS]);
  446. if ((err = attach_aead(x, attrs[XFRMA_ALG_AEAD])))
  447. goto error;
  448. if ((err = attach_auth_trunc(&x->aalg, &x->props.aalgo,
  449. attrs[XFRMA_ALG_AUTH_TRUNC])))
  450. goto error;
  451. if (!x->props.aalgo) {
  452. if ((err = attach_auth(&x->aalg, &x->props.aalgo,
  453. attrs[XFRMA_ALG_AUTH])))
  454. goto error;
  455. }
  456. if ((err = attach_crypt(x, attrs[XFRMA_ALG_CRYPT])))
  457. goto error;
  458. if ((err = attach_one_algo(&x->calg, &x->props.calgo,
  459. xfrm_calg_get_byname,
  460. attrs[XFRMA_ALG_COMP])))
  461. goto error;
  462. if (attrs[XFRMA_ENCAP]) {
  463. x->encap = kmemdup(nla_data(attrs[XFRMA_ENCAP]),
  464. sizeof(*x->encap), GFP_KERNEL);
  465. if (x->encap == NULL)
  466. goto error;
  467. }
  468. if (attrs[XFRMA_TFCPAD])
  469. x->tfcpad = nla_get_u32(attrs[XFRMA_TFCPAD]);
  470. if (attrs[XFRMA_COADDR]) {
  471. x->coaddr = kmemdup(nla_data(attrs[XFRMA_COADDR]),
  472. sizeof(*x->coaddr), GFP_KERNEL);
  473. if (x->coaddr == NULL)
  474. goto error;
  475. }
  476. xfrm_mark_get(attrs, &x->mark);
  477. if (attrs[XFRMA_OUTPUT_MARK])
  478. x->props.output_mark = nla_get_u32(attrs[XFRMA_OUTPUT_MARK]);
  479. err = __xfrm_init_state(x, false, attrs[XFRMA_OFFLOAD_DEV]);
  480. if (err)
  481. goto error;
  482. if (attrs[XFRMA_SEC_CTX]) {
  483. err = security_xfrm_state_alloc(x,
  484. nla_data(attrs[XFRMA_SEC_CTX]));
  485. if (err)
  486. goto error;
  487. }
  488. if (attrs[XFRMA_OFFLOAD_DEV]) {
  489. err = xfrm_dev_state_add(net, x,
  490. nla_data(attrs[XFRMA_OFFLOAD_DEV]));
  491. if (err)
  492. goto error;
  493. }
  494. if ((err = xfrm_alloc_replay_state_esn(&x->replay_esn, &x->preplay_esn,
  495. attrs[XFRMA_REPLAY_ESN_VAL])))
  496. goto error;
  497. x->km.seq = p->seq;
  498. x->replay_maxdiff = net->xfrm.sysctl_aevent_rseqth;
  499. /* sysctl_xfrm_aevent_etime is in 100ms units */
  500. x->replay_maxage = (net->xfrm.sysctl_aevent_etime*HZ)/XFRM_AE_ETH_M;
  501. if ((err = xfrm_init_replay(x)))
  502. goto error;
  503. /* override default values from above */
  504. xfrm_update_ae_params(x, attrs, 0);
  505. return x;
  506. error:
  507. x->km.state = XFRM_STATE_DEAD;
  508. xfrm_state_put(x);
  509. error_no_put:
  510. *errp = err;
  511. return NULL;
  512. }
  513. static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
  514. struct nlattr **attrs)
  515. {
  516. struct net *net = sock_net(skb->sk);
  517. struct xfrm_usersa_info *p = nlmsg_data(nlh);
  518. struct xfrm_state *x;
  519. int err;
  520. struct km_event c;
  521. err = verify_newsa_info(p, attrs);
  522. if (err)
  523. return err;
  524. x = xfrm_state_construct(net, p, attrs, &err);
  525. if (!x)
  526. return err;
  527. xfrm_state_hold(x);
  528. if (nlh->nlmsg_type == XFRM_MSG_NEWSA)
  529. err = xfrm_state_add(x);
  530. else
  531. err = xfrm_state_update(x);
  532. xfrm_audit_state_add(x, err ? 0 : 1, true);
  533. if (err < 0) {
  534. x->km.state = XFRM_STATE_DEAD;
  535. xfrm_dev_state_delete(x);
  536. __xfrm_state_put(x);
  537. goto out;
  538. }
  539. c.seq = nlh->nlmsg_seq;
  540. c.portid = nlh->nlmsg_pid;
  541. c.event = nlh->nlmsg_type;
  542. km_state_notify(x, &c);
  543. out:
  544. xfrm_state_put(x);
  545. return err;
  546. }
  547. static struct xfrm_state *xfrm_user_state_lookup(struct net *net,
  548. struct xfrm_usersa_id *p,
  549. struct nlattr **attrs,
  550. int *errp)
  551. {
  552. struct xfrm_state *x = NULL;
  553. struct xfrm_mark m;
  554. int err;
  555. u32 mark = xfrm_mark_get(attrs, &m);
  556. if (xfrm_id_proto_match(p->proto, IPSEC_PROTO_ANY)) {
  557. err = -ESRCH;
  558. x = xfrm_state_lookup(net, mark, &p->daddr, p->spi, p->proto, p->family);
  559. } else {
  560. xfrm_address_t *saddr = NULL;
  561. verify_one_addr(attrs, XFRMA_SRCADDR, &saddr);
  562. if (!saddr) {
  563. err = -EINVAL;
  564. goto out;
  565. }
  566. err = -ESRCH;
  567. x = xfrm_state_lookup_byaddr(net, mark,
  568. &p->daddr, saddr,
  569. p->proto, p->family);
  570. }
  571. out:
  572. if (!x && errp)
  573. *errp = err;
  574. return x;
  575. }
  576. static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
  577. struct nlattr **attrs)
  578. {
  579. struct net *net = sock_net(skb->sk);
  580. struct xfrm_state *x;
  581. int err = -ESRCH;
  582. struct km_event c;
  583. struct xfrm_usersa_id *p = nlmsg_data(nlh);
  584. x = xfrm_user_state_lookup(net, p, attrs, &err);
  585. if (x == NULL)
  586. return err;
  587. if ((err = security_xfrm_state_delete(x)) != 0)
  588. goto out;
  589. if (xfrm_state_kern(x)) {
  590. err = -EPERM;
  591. goto out;
  592. }
  593. err = xfrm_state_delete(x);
  594. if (err < 0)
  595. goto out;
  596. c.seq = nlh->nlmsg_seq;
  597. c.portid = nlh->nlmsg_pid;
  598. c.event = nlh->nlmsg_type;
  599. km_state_notify(x, &c);
  600. out:
  601. xfrm_audit_state_delete(x, err ? 0 : 1, true);
  602. xfrm_state_put(x);
  603. return err;
  604. }
  605. static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
  606. {
  607. memset(p, 0, sizeof(*p));
  608. memcpy(&p->id, &x->id, sizeof(p->id));
  609. memcpy(&p->sel, &x->sel, sizeof(p->sel));
  610. memcpy(&p->lft, &x->lft, sizeof(p->lft));
  611. memcpy(&p->curlft, &x->curlft, sizeof(p->curlft));
  612. put_unaligned(x->stats.replay_window, &p->stats.replay_window);
  613. put_unaligned(x->stats.replay, &p->stats.replay);
  614. put_unaligned(x->stats.integrity_failed, &p->stats.integrity_failed);
  615. memcpy(&p->saddr, &x->props.saddr, sizeof(p->saddr));
  616. p->mode = x->props.mode;
  617. p->replay_window = x->props.replay_window;
  618. p->reqid = x->props.reqid;
  619. p->family = x->props.family;
  620. p->flags = x->props.flags;
  621. p->seq = x->km.seq;
  622. }
  623. struct xfrm_dump_info {
  624. struct sk_buff *in_skb;
  625. struct sk_buff *out_skb;
  626. u32 nlmsg_seq;
  627. u16 nlmsg_flags;
  628. };
  629. static int copy_sec_ctx(struct xfrm_sec_ctx *s, struct sk_buff *skb)
  630. {
  631. struct xfrm_user_sec_ctx *uctx;
  632. struct nlattr *attr;
  633. int ctx_size = sizeof(*uctx) + s->ctx_len;
  634. attr = nla_reserve(skb, XFRMA_SEC_CTX, ctx_size);
  635. if (attr == NULL)
  636. return -EMSGSIZE;
  637. uctx = nla_data(attr);
  638. uctx->exttype = XFRMA_SEC_CTX;
  639. uctx->len = ctx_size;
  640. uctx->ctx_doi = s->ctx_doi;
  641. uctx->ctx_alg = s->ctx_alg;
  642. uctx->ctx_len = s->ctx_len;
  643. memcpy(uctx + 1, s->ctx_str, s->ctx_len);
  644. return 0;
  645. }
  646. static int copy_user_offload(struct xfrm_state_offload *xso, struct sk_buff *skb)
  647. {
  648. struct xfrm_user_offload *xuo;
  649. struct nlattr *attr;
  650. attr = nla_reserve(skb, XFRMA_OFFLOAD_DEV, sizeof(*xuo));
  651. if (attr == NULL)
  652. return -EMSGSIZE;
  653. xuo = nla_data(attr);
  654. memset(xuo, 0, sizeof(*xuo));
  655. xuo->ifindex = xso->dev->ifindex;
  656. xuo->flags = xso->flags;
  657. return 0;
  658. }
  659. static int copy_to_user_auth(struct xfrm_algo_auth *auth, struct sk_buff *skb)
  660. {
  661. struct xfrm_algo *algo;
  662. struct nlattr *nla;
  663. nla = nla_reserve(skb, XFRMA_ALG_AUTH,
  664. sizeof(*algo) + (auth->alg_key_len + 7) / 8);
  665. if (!nla)
  666. return -EMSGSIZE;
  667. algo = nla_data(nla);
  668. strncpy(algo->alg_name, auth->alg_name, sizeof(algo->alg_name));
  669. memcpy(algo->alg_key, auth->alg_key, (auth->alg_key_len + 7) / 8);
  670. algo->alg_key_len = auth->alg_key_len;
  671. return 0;
  672. }
  673. /* Don't change this without updating xfrm_sa_len! */
  674. static int copy_to_user_state_extra(struct xfrm_state *x,
  675. struct xfrm_usersa_info *p,
  676. struct sk_buff *skb)
  677. {
  678. int ret = 0;
  679. copy_to_user_state(x, p);
  680. if (x->props.extra_flags) {
  681. ret = nla_put_u32(skb, XFRMA_SA_EXTRA_FLAGS,
  682. x->props.extra_flags);
  683. if (ret)
  684. goto out;
  685. }
  686. if (x->coaddr) {
  687. ret = nla_put(skb, XFRMA_COADDR, sizeof(*x->coaddr), x->coaddr);
  688. if (ret)
  689. goto out;
  690. }
  691. if (x->lastused) {
  692. ret = nla_put_u64_64bit(skb, XFRMA_LASTUSED, x->lastused,
  693. XFRMA_PAD);
  694. if (ret)
  695. goto out;
  696. }
  697. if (x->aead) {
  698. ret = nla_put(skb, XFRMA_ALG_AEAD, aead_len(x->aead), x->aead);
  699. if (ret)
  700. goto out;
  701. }
  702. if (x->aalg) {
  703. ret = copy_to_user_auth(x->aalg, skb);
  704. if (!ret)
  705. ret = nla_put(skb, XFRMA_ALG_AUTH_TRUNC,
  706. xfrm_alg_auth_len(x->aalg), x->aalg);
  707. if (ret)
  708. goto out;
  709. }
  710. if (x->ealg) {
  711. ret = nla_put(skb, XFRMA_ALG_CRYPT, xfrm_alg_len(x->ealg), x->ealg);
  712. if (ret)
  713. goto out;
  714. }
  715. if (x->calg) {
  716. ret = nla_put(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
  717. if (ret)
  718. goto out;
  719. }
  720. if (x->encap) {
  721. ret = nla_put(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
  722. if (ret)
  723. goto out;
  724. }
  725. if (x->tfcpad) {
  726. ret = nla_put_u32(skb, XFRMA_TFCPAD, x->tfcpad);
  727. if (ret)
  728. goto out;
  729. }
  730. ret = xfrm_mark_put(skb, &x->mark);
  731. if (ret)
  732. goto out;
  733. if (x->replay_esn)
  734. ret = nla_put(skb, XFRMA_REPLAY_ESN_VAL,
  735. xfrm_replay_state_esn_len(x->replay_esn),
  736. x->replay_esn);
  737. else
  738. ret = nla_put(skb, XFRMA_REPLAY_VAL, sizeof(x->replay),
  739. &x->replay);
  740. if (ret)
  741. goto out;
  742. if(x->xso.dev)
  743. ret = copy_user_offload(&x->xso, skb);
  744. if (ret)
  745. goto out;
  746. if (x->props.output_mark) {
  747. ret = nla_put_u32(skb, XFRMA_OUTPUT_MARK, x->props.output_mark);
  748. if (ret)
  749. goto out;
  750. }
  751. if (x->security)
  752. ret = copy_sec_ctx(x->security, skb);
  753. out:
  754. return ret;
  755. }
  756. static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
  757. {
  758. struct xfrm_dump_info *sp = ptr;
  759. struct sk_buff *in_skb = sp->in_skb;
  760. struct sk_buff *skb = sp->out_skb;
  761. struct xfrm_usersa_info *p;
  762. struct nlmsghdr *nlh;
  763. int err;
  764. nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, sp->nlmsg_seq,
  765. XFRM_MSG_NEWSA, sizeof(*p), sp->nlmsg_flags);
  766. if (nlh == NULL)
  767. return -EMSGSIZE;
  768. p = nlmsg_data(nlh);
  769. err = copy_to_user_state_extra(x, p, skb);
  770. if (err) {
  771. nlmsg_cancel(skb, nlh);
  772. return err;
  773. }
  774. nlmsg_end(skb, nlh);
  775. return 0;
  776. }
  777. static int xfrm_dump_sa_done(struct netlink_callback *cb)
  778. {
  779. struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
  780. struct sock *sk = cb->skb->sk;
  781. struct net *net = sock_net(sk);
  782. if (cb->args[0])
  783. xfrm_state_walk_done(walk, net);
  784. return 0;
  785. }
  786. static const struct nla_policy xfrma_policy[XFRMA_MAX+1];
  787. static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
  788. {
  789. struct net *net = sock_net(skb->sk);
  790. struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
  791. struct xfrm_dump_info info;
  792. BUILD_BUG_ON(sizeof(struct xfrm_state_walk) >
  793. sizeof(cb->args) - sizeof(cb->args[0]));
  794. info.in_skb = cb->skb;
  795. info.out_skb = skb;
  796. info.nlmsg_seq = cb->nlh->nlmsg_seq;
  797. info.nlmsg_flags = NLM_F_MULTI;
  798. if (!cb->args[0]) {
  799. struct nlattr *attrs[XFRMA_MAX+1];
  800. struct xfrm_address_filter *filter = NULL;
  801. u8 proto = 0;
  802. int err;
  803. err = nlmsg_parse(cb->nlh, 0, attrs, XFRMA_MAX, xfrma_policy,
  804. NULL);
  805. if (err < 0)
  806. return err;
  807. if (attrs[XFRMA_ADDRESS_FILTER]) {
  808. filter = kmemdup(nla_data(attrs[XFRMA_ADDRESS_FILTER]),
  809. sizeof(*filter), GFP_KERNEL);
  810. if (filter == NULL)
  811. return -ENOMEM;
  812. }
  813. if (attrs[XFRMA_PROTO])
  814. proto = nla_get_u8(attrs[XFRMA_PROTO]);
  815. xfrm_state_walk_init(walk, proto, filter);
  816. cb->args[0] = 1;
  817. }
  818. (void) xfrm_state_walk(net, walk, dump_one_state, &info);
  819. return skb->len;
  820. }
  821. static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
  822. struct xfrm_state *x, u32 seq)
  823. {
  824. struct xfrm_dump_info info;
  825. struct sk_buff *skb;
  826. int err;
  827. skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
  828. if (!skb)
  829. return ERR_PTR(-ENOMEM);
  830. info.in_skb = in_skb;
  831. info.out_skb = skb;
  832. info.nlmsg_seq = seq;
  833. info.nlmsg_flags = 0;
  834. err = dump_one_state(x, 0, &info);
  835. if (err) {
  836. kfree_skb(skb);
  837. return ERR_PTR(err);
  838. }
  839. return skb;
  840. }
  841. /* A wrapper for nlmsg_multicast() checking that nlsk is still available.
  842. * Must be called with RCU read lock.
  843. */
  844. static inline int xfrm_nlmsg_multicast(struct net *net, struct sk_buff *skb,
  845. u32 pid, unsigned int group)
  846. {
  847. struct sock *nlsk = rcu_dereference(net->xfrm.nlsk);
  848. if (nlsk)
  849. return nlmsg_multicast(nlsk, skb, pid, group, GFP_ATOMIC);
  850. else
  851. return -1;
  852. }
  853. static inline size_t xfrm_spdinfo_msgsize(void)
  854. {
  855. return NLMSG_ALIGN(4)
  856. + nla_total_size(sizeof(struct xfrmu_spdinfo))
  857. + nla_total_size(sizeof(struct xfrmu_spdhinfo))
  858. + nla_total_size(sizeof(struct xfrmu_spdhthresh))
  859. + nla_total_size(sizeof(struct xfrmu_spdhthresh));
  860. }
  861. static int build_spdinfo(struct sk_buff *skb, struct net *net,
  862. u32 portid, u32 seq, u32 flags)
  863. {
  864. struct xfrmk_spdinfo si;
  865. struct xfrmu_spdinfo spc;
  866. struct xfrmu_spdhinfo sph;
  867. struct xfrmu_spdhthresh spt4, spt6;
  868. struct nlmsghdr *nlh;
  869. int err;
  870. u32 *f;
  871. unsigned lseq;
  872. nlh = nlmsg_put(skb, portid, seq, XFRM_MSG_NEWSPDINFO, sizeof(u32), 0);
  873. if (nlh == NULL) /* shouldn't really happen ... */
  874. return -EMSGSIZE;
  875. f = nlmsg_data(nlh);
  876. *f = flags;
  877. xfrm_spd_getinfo(net, &si);
  878. spc.incnt = si.incnt;
  879. spc.outcnt = si.outcnt;
  880. spc.fwdcnt = si.fwdcnt;
  881. spc.inscnt = si.inscnt;
  882. spc.outscnt = si.outscnt;
  883. spc.fwdscnt = si.fwdscnt;
  884. sph.spdhcnt = si.spdhcnt;
  885. sph.spdhmcnt = si.spdhmcnt;
  886. do {
  887. lseq = read_seqbegin(&net->xfrm.policy_hthresh.lock);
  888. spt4.lbits = net->xfrm.policy_hthresh.lbits4;
  889. spt4.rbits = net->xfrm.policy_hthresh.rbits4;
  890. spt6.lbits = net->xfrm.policy_hthresh.lbits6;
  891. spt6.rbits = net->xfrm.policy_hthresh.rbits6;
  892. } while (read_seqretry(&net->xfrm.policy_hthresh.lock, lseq));
  893. err = nla_put(skb, XFRMA_SPD_INFO, sizeof(spc), &spc);
  894. if (!err)
  895. err = nla_put(skb, XFRMA_SPD_HINFO, sizeof(sph), &sph);
  896. if (!err)
  897. err = nla_put(skb, XFRMA_SPD_IPV4_HTHRESH, sizeof(spt4), &spt4);
  898. if (!err)
  899. err = nla_put(skb, XFRMA_SPD_IPV6_HTHRESH, sizeof(spt6), &spt6);
  900. if (err) {
  901. nlmsg_cancel(skb, nlh);
  902. return err;
  903. }
  904. nlmsg_end(skb, nlh);
  905. return 0;
  906. }
  907. static int xfrm_set_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
  908. struct nlattr **attrs)
  909. {
  910. struct net *net = sock_net(skb->sk);
  911. struct xfrmu_spdhthresh *thresh4 = NULL;
  912. struct xfrmu_spdhthresh *thresh6 = NULL;
  913. /* selector prefixlen thresholds to hash policies */
  914. if (attrs[XFRMA_SPD_IPV4_HTHRESH]) {
  915. struct nlattr *rta = attrs[XFRMA_SPD_IPV4_HTHRESH];
  916. if (nla_len(rta) < sizeof(*thresh4))
  917. return -EINVAL;
  918. thresh4 = nla_data(rta);
  919. if (thresh4->lbits > 32 || thresh4->rbits > 32)
  920. return -EINVAL;
  921. }
  922. if (attrs[XFRMA_SPD_IPV6_HTHRESH]) {
  923. struct nlattr *rta = attrs[XFRMA_SPD_IPV6_HTHRESH];
  924. if (nla_len(rta) < sizeof(*thresh6))
  925. return -EINVAL;
  926. thresh6 = nla_data(rta);
  927. if (thresh6->lbits > 128 || thresh6->rbits > 128)
  928. return -EINVAL;
  929. }
  930. if (thresh4 || thresh6) {
  931. write_seqlock(&net->xfrm.policy_hthresh.lock);
  932. if (thresh4) {
  933. net->xfrm.policy_hthresh.lbits4 = thresh4->lbits;
  934. net->xfrm.policy_hthresh.rbits4 = thresh4->rbits;
  935. }
  936. if (thresh6) {
  937. net->xfrm.policy_hthresh.lbits6 = thresh6->lbits;
  938. net->xfrm.policy_hthresh.rbits6 = thresh6->rbits;
  939. }
  940. write_sequnlock(&net->xfrm.policy_hthresh.lock);
  941. xfrm_policy_hash_rebuild(net);
  942. }
  943. return 0;
  944. }
  945. static int xfrm_get_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
  946. struct nlattr **attrs)
  947. {
  948. struct net *net = sock_net(skb->sk);
  949. struct sk_buff *r_skb;
  950. u32 *flags = nlmsg_data(nlh);
  951. u32 sportid = NETLINK_CB(skb).portid;
  952. u32 seq = nlh->nlmsg_seq;
  953. r_skb = nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC);
  954. if (r_skb == NULL)
  955. return -ENOMEM;
  956. if (build_spdinfo(r_skb, net, sportid, seq, *flags) < 0)
  957. BUG();
  958. return nlmsg_unicast(net->xfrm.nlsk, r_skb, sportid);
  959. }
  960. static inline size_t xfrm_sadinfo_msgsize(void)
  961. {
  962. return NLMSG_ALIGN(4)
  963. + nla_total_size(sizeof(struct xfrmu_sadhinfo))
  964. + nla_total_size(4); /* XFRMA_SAD_CNT */
  965. }
  966. static int build_sadinfo(struct sk_buff *skb, struct net *net,
  967. u32 portid, u32 seq, u32 flags)
  968. {
  969. struct xfrmk_sadinfo si;
  970. struct xfrmu_sadhinfo sh;
  971. struct nlmsghdr *nlh;
  972. int err;
  973. u32 *f;
  974. nlh = nlmsg_put(skb, portid, seq, XFRM_MSG_NEWSADINFO, sizeof(u32), 0);
  975. if (nlh == NULL) /* shouldn't really happen ... */
  976. return -EMSGSIZE;
  977. f = nlmsg_data(nlh);
  978. *f = flags;
  979. xfrm_sad_getinfo(net, &si);
  980. sh.sadhmcnt = si.sadhmcnt;
  981. sh.sadhcnt = si.sadhcnt;
  982. err = nla_put_u32(skb, XFRMA_SAD_CNT, si.sadcnt);
  983. if (!err)
  984. err = nla_put(skb, XFRMA_SAD_HINFO, sizeof(sh), &sh);
  985. if (err) {
  986. nlmsg_cancel(skb, nlh);
  987. return err;
  988. }
  989. nlmsg_end(skb, nlh);
  990. return 0;
  991. }
  992. static int xfrm_get_sadinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
  993. struct nlattr **attrs)
  994. {
  995. struct net *net = sock_net(skb->sk);
  996. struct sk_buff *r_skb;
  997. u32 *flags = nlmsg_data(nlh);
  998. u32 sportid = NETLINK_CB(skb).portid;
  999. u32 seq = nlh->nlmsg_seq;
  1000. r_skb = nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC);
  1001. if (r_skb == NULL)
  1002. return -ENOMEM;
  1003. if (build_sadinfo(r_skb, net, sportid, seq, *flags) < 0)
  1004. BUG();
  1005. return nlmsg_unicast(net->xfrm.nlsk, r_skb, sportid);
  1006. }
  1007. static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
  1008. struct nlattr **attrs)
  1009. {
  1010. struct net *net = sock_net(skb->sk);
  1011. struct xfrm_usersa_id *p = nlmsg_data(nlh);
  1012. struct xfrm_state *x;
  1013. struct sk_buff *resp_skb;
  1014. int err = -ESRCH;
  1015. x = xfrm_user_state_lookup(net, p, attrs, &err);
  1016. if (x == NULL)
  1017. goto out_noput;
  1018. resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
  1019. if (IS_ERR(resp_skb)) {
  1020. err = PTR_ERR(resp_skb);
  1021. } else {
  1022. err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).portid);
  1023. }
  1024. xfrm_state_put(x);
  1025. out_noput:
  1026. return err;
  1027. }
  1028. static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh,
  1029. struct nlattr **attrs)
  1030. {
  1031. struct net *net = sock_net(skb->sk);
  1032. struct xfrm_state *x;
  1033. struct xfrm_userspi_info *p;
  1034. struct sk_buff *resp_skb;
  1035. xfrm_address_t *daddr;
  1036. int family;
  1037. int err;
  1038. u32 mark;
  1039. struct xfrm_mark m;
  1040. p = nlmsg_data(nlh);
  1041. err = verify_spi_info(p->info.id.proto, p->min, p->max);
  1042. if (err)
  1043. goto out_noput;
  1044. family = p->info.family;
  1045. daddr = &p->info.id.daddr;
  1046. x = NULL;
  1047. mark = xfrm_mark_get(attrs, &m);
  1048. if (p->info.seq) {
  1049. x = xfrm_find_acq_byseq(net, mark, p->info.seq);
  1050. if (x && !xfrm_addr_equal(&x->id.daddr, daddr, family)) {
  1051. xfrm_state_put(x);
  1052. x = NULL;
  1053. }
  1054. }
  1055. if (!x)
  1056. x = xfrm_find_acq(net, &m, p->info.mode, p->info.reqid,
  1057. p->info.id.proto, daddr,
  1058. &p->info.saddr, 1,
  1059. family);
  1060. err = -ENOENT;
  1061. if (x == NULL)
  1062. goto out_noput;
  1063. err = xfrm_alloc_spi(x, p->min, p->max);
  1064. if (err)
  1065. goto out;
  1066. resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
  1067. if (IS_ERR(resp_skb)) {
  1068. err = PTR_ERR(resp_skb);
  1069. goto out;
  1070. }
  1071. err = nlmsg_unicast(net->xfrm.nlsk, resp_skb, NETLINK_CB(skb).portid);
  1072. out:
  1073. xfrm_state_put(x);
  1074. out_noput:
  1075. return err;
  1076. }
  1077. static int verify_policy_dir(u8 dir)
  1078. {
  1079. switch (dir) {
  1080. case XFRM_POLICY_IN:
  1081. case XFRM_POLICY_OUT:
  1082. case XFRM_POLICY_FWD:
  1083. break;
  1084. default:
  1085. return -EINVAL;
  1086. }
  1087. return 0;
  1088. }
  1089. static int verify_policy_type(u8 type)
  1090. {
  1091. switch (type) {
  1092. case XFRM_POLICY_TYPE_MAIN:
  1093. #ifdef CONFIG_XFRM_SUB_POLICY
  1094. case XFRM_POLICY_TYPE_SUB:
  1095. #endif
  1096. break;
  1097. default:
  1098. return -EINVAL;
  1099. }
  1100. return 0;
  1101. }
  1102. static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
  1103. {
  1104. int ret;
  1105. switch (p->share) {
  1106. case XFRM_SHARE_ANY:
  1107. case XFRM_SHARE_SESSION:
  1108. case XFRM_SHARE_USER:
  1109. case XFRM_SHARE_UNIQUE:
  1110. break;
  1111. default:
  1112. return -EINVAL;
  1113. }
  1114. switch (p->action) {
  1115. case XFRM_POLICY_ALLOW:
  1116. case XFRM_POLICY_BLOCK:
  1117. break;
  1118. default:
  1119. return -EINVAL;
  1120. }
  1121. switch (p->sel.family) {
  1122. case AF_INET:
  1123. break;
  1124. case AF_INET6:
  1125. #if IS_ENABLED(CONFIG_IPV6)
  1126. break;
  1127. #else
  1128. return -EAFNOSUPPORT;
  1129. #endif
  1130. default:
  1131. return -EINVAL;
  1132. }
  1133. ret = verify_policy_dir(p->dir);
  1134. if (ret)
  1135. return ret;
  1136. if (p->index && ((p->index & XFRM_POLICY_MAX) != p->dir))
  1137. return -EINVAL;
  1138. return 0;
  1139. }
  1140. static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct nlattr **attrs)
  1141. {
  1142. struct nlattr *rt = attrs[XFRMA_SEC_CTX];
  1143. struct xfrm_user_sec_ctx *uctx;
  1144. if (!rt)
  1145. return 0;
  1146. uctx = nla_data(rt);
  1147. return security_xfrm_policy_alloc(&pol->security, uctx, GFP_KERNEL);
  1148. }
  1149. static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
  1150. int nr)
  1151. {
  1152. int i;
  1153. xp->xfrm_nr = nr;
  1154. for (i = 0; i < nr; i++, ut++) {
  1155. struct xfrm_tmpl *t = &xp->xfrm_vec[i];
  1156. memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
  1157. memcpy(&t->saddr, &ut->saddr,
  1158. sizeof(xfrm_address_t));
  1159. t->reqid = ut->reqid;
  1160. t->mode = ut->mode;
  1161. t->share = ut->share;
  1162. t->optional = ut->optional;
  1163. t->aalgos = ut->aalgos;
  1164. t->ealgos = ut->ealgos;
  1165. t->calgos = ut->calgos;
  1166. /* If all masks are ~0, then we allow all algorithms. */
  1167. t->allalgs = !~(t->aalgos & t->ealgos & t->calgos);
  1168. t->encap_family = ut->family;
  1169. }
  1170. }
  1171. static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family)
  1172. {
  1173. int i;
  1174. if (nr > XFRM_MAX_DEPTH)
  1175. return -EINVAL;
  1176. for (i = 0; i < nr; i++) {
  1177. /* We never validated the ut->family value, so many
  1178. * applications simply leave it at zero. The check was
  1179. * never made and ut->family was ignored because all
  1180. * templates could be assumed to have the same family as
  1181. * the policy itself. Now that we will have ipv4-in-ipv6
  1182. * and ipv6-in-ipv4 tunnels, this is no longer true.
  1183. */
  1184. if (!ut[i].family)
  1185. ut[i].family = family;
  1186. switch (ut[i].family) {
  1187. case AF_INET:
  1188. break;
  1189. #if IS_ENABLED(CONFIG_IPV6)
  1190. case AF_INET6:
  1191. break;
  1192. #endif
  1193. default:
  1194. return -EINVAL;
  1195. }
  1196. }
  1197. return 0;
  1198. }
  1199. static int copy_from_user_tmpl(struct xfrm_policy *pol, struct nlattr **attrs)
  1200. {
  1201. struct nlattr *rt = attrs[XFRMA_TMPL];
  1202. if (!rt) {
  1203. pol->xfrm_nr = 0;
  1204. } else {
  1205. struct xfrm_user_tmpl *utmpl = nla_data(rt);
  1206. int nr = nla_len(rt) / sizeof(*utmpl);
  1207. int err;
  1208. err = validate_tmpl(nr, utmpl, pol->family);
  1209. if (err)
  1210. return err;
  1211. copy_templates(pol, utmpl, nr);
  1212. }
  1213. return 0;
  1214. }
  1215. static int copy_from_user_policy_type(u8 *tp, struct nlattr **attrs)
  1216. {
  1217. struct nlattr *rt = attrs[XFRMA_POLICY_TYPE];
  1218. struct xfrm_userpolicy_type *upt;
  1219. u8 type = XFRM_POLICY_TYPE_MAIN;
  1220. int err;
  1221. if (rt) {
  1222. upt = nla_data(rt);
  1223. type = upt->type;
  1224. }
  1225. err = verify_policy_type(type);
  1226. if (err)
  1227. return err;
  1228. *tp = type;
  1229. return 0;
  1230. }
  1231. static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
  1232. {
  1233. xp->priority = p->priority;
  1234. xp->index = p->index;
  1235. memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
  1236. memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
  1237. xp->action = p->action;
  1238. xp->flags = p->flags;
  1239. xp->family = p->sel.family;
  1240. /* XXX xp->share = p->share; */
  1241. }
  1242. static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
  1243. {
  1244. memset(p, 0, sizeof(*p));
  1245. memcpy(&p->sel, &xp->selector, sizeof(p->sel));
  1246. memcpy(&p->lft, &xp->lft, sizeof(p->lft));
  1247. memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
  1248. p->priority = xp->priority;
  1249. p->index = xp->index;
  1250. p->sel.family = xp->family;
  1251. p->dir = dir;
  1252. p->action = xp->action;
  1253. p->flags = xp->flags;
  1254. p->share = XFRM_SHARE_ANY; /* XXX xp->share */
  1255. }
  1256. static struct xfrm_policy *xfrm_policy_construct(struct net *net, struct xfrm_userpolicy_info *p, struct nlattr **attrs, int *errp)
  1257. {
  1258. struct xfrm_policy *xp = xfrm_policy_alloc(net, GFP_KERNEL);
  1259. int err;
  1260. if (!xp) {
  1261. *errp = -ENOMEM;
  1262. return NULL;
  1263. }
  1264. copy_from_user_policy(xp, p);
  1265. err = copy_from_user_policy_type(&xp->type, attrs);
  1266. if (err)
  1267. goto error;
  1268. if (!(err = copy_from_user_tmpl(xp, attrs)))
  1269. err = copy_from_user_sec_ctx(xp, attrs);
  1270. if (err)
  1271. goto error;
  1272. xfrm_mark_get(attrs, &xp->mark);
  1273. return xp;
  1274. error:
  1275. *errp = err;
  1276. xp->walk.dead = 1;
  1277. xfrm_policy_destroy(xp);
  1278. return NULL;
  1279. }
  1280. static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
  1281. struct nlattr **attrs)
  1282. {
  1283. struct net *net = sock_net(skb->sk);
  1284. struct xfrm_userpolicy_info *p = nlmsg_data(nlh);
  1285. struct xfrm_policy *xp;
  1286. struct km_event c;
  1287. int err;
  1288. int excl;
  1289. err = verify_newpolicy_info(p);
  1290. if (err)
  1291. return err;
  1292. err = verify_sec_ctx_len(attrs);
  1293. if (err)
  1294. return err;
  1295. xp = xfrm_policy_construct(net, p, attrs, &err);
  1296. if (!xp)
  1297. return err;
  1298. /* shouldn't excl be based on nlh flags??
  1299. * Aha! this is anti-netlink really i.e more pfkey derived
  1300. * in netlink excl is a flag and you wouldnt need
  1301. * a type XFRM_MSG_UPDPOLICY - JHS */
  1302. excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
  1303. err = xfrm_policy_insert(p->dir, xp, excl);
  1304. xfrm_audit_policy_add(xp, err ? 0 : 1, true);
  1305. if (err) {
  1306. security_xfrm_policy_free(xp->security);
  1307. kfree(xp);
  1308. return err;
  1309. }
  1310. c.event = nlh->nlmsg_type;
  1311. c.seq = nlh->nlmsg_seq;
  1312. c.portid = nlh->nlmsg_pid;
  1313. km_policy_notify(xp, p->dir, &c);
  1314. xfrm_pol_put(xp);
  1315. return 0;
  1316. }
  1317. static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
  1318. {
  1319. struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
  1320. int i;
  1321. if (xp->xfrm_nr == 0)
  1322. return 0;
  1323. for (i = 0; i < xp->xfrm_nr; i++) {
  1324. struct xfrm_user_tmpl *up = &vec[i];
  1325. struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
  1326. memset(up, 0, sizeof(*up));
  1327. memcpy(&up->id, &kp->id, sizeof(up->id));
  1328. up->family = kp->encap_family;
  1329. memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
  1330. up->reqid = kp->reqid;
  1331. up->mode = kp->mode;
  1332. up->share = kp->share;
  1333. up->optional = kp->optional;
  1334. up->aalgos = kp->aalgos;
  1335. up->ealgos = kp->ealgos;
  1336. up->calgos = kp->calgos;
  1337. }
  1338. return nla_put(skb, XFRMA_TMPL,
  1339. sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr, vec);
  1340. }
  1341. static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb)
  1342. {
  1343. if (x->security) {
  1344. return copy_sec_ctx(x->security, skb);
  1345. }
  1346. return 0;
  1347. }
  1348. static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
  1349. {
  1350. if (xp->security)
  1351. return copy_sec_ctx(xp->security, skb);
  1352. return 0;
  1353. }
  1354. static inline size_t userpolicy_type_attrsize(void)
  1355. {
  1356. #ifdef CONFIG_XFRM_SUB_POLICY
  1357. return nla_total_size(sizeof(struct xfrm_userpolicy_type));
  1358. #else
  1359. return 0;
  1360. #endif
  1361. }
  1362. #ifdef CONFIG_XFRM_SUB_POLICY
  1363. static int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
  1364. {
  1365. struct xfrm_userpolicy_type upt = {
  1366. .type = type,
  1367. };
  1368. return nla_put(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
  1369. }
  1370. #else
  1371. static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
  1372. {
  1373. return 0;
  1374. }
  1375. #endif
  1376. static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
  1377. {
  1378. struct xfrm_dump_info *sp = ptr;
  1379. struct xfrm_userpolicy_info *p;
  1380. struct sk_buff *in_skb = sp->in_skb;
  1381. struct sk_buff *skb = sp->out_skb;
  1382. struct nlmsghdr *nlh;
  1383. int err;
  1384. nlh = nlmsg_put(skb, NETLINK_CB(in_skb).portid, sp->nlmsg_seq,
  1385. XFRM_MSG_NEWPOLICY, sizeof(*p), sp->nlmsg_flags);
  1386. if (nlh == NULL)
  1387. return -EMSGSIZE;
  1388. p = nlmsg_data(nlh);
  1389. copy_to_user_policy(xp, p, dir);
  1390. err = copy_to_user_tmpl(xp, skb);
  1391. if (!err)
  1392. err = copy_to_user_sec_ctx(xp, skb);
  1393. if (!err)
  1394. err = copy_to_user_policy_type(xp->type, skb);
  1395. if (!err)
  1396. err = xfrm_mark_put(skb, &xp->mark);
  1397. if (err) {
  1398. nlmsg_cancel(skb, nlh);
  1399. return err;
  1400. }
  1401. nlmsg_end(skb, nlh);
  1402. return 0;
  1403. }
  1404. static int xfrm_dump_policy_done(struct netlink_callback *cb)
  1405. {
  1406. struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *)cb->args;
  1407. struct net *net = sock_net(cb->skb->sk);
  1408. xfrm_policy_walk_done(walk, net);
  1409. return 0;
  1410. }
  1411. static int xfrm_dump_policy_start(struct netlink_callback *cb)
  1412. {
  1413. struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *)cb->args;
  1414. BUILD_BUG_ON(sizeof(*walk) > sizeof(cb->args));
  1415. xfrm_policy_walk_init(walk, XFRM_POLICY_TYPE_ANY);
  1416. return 0;
  1417. }
  1418. static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
  1419. {
  1420. struct net *net = sock_net(skb->sk);
  1421. struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *)cb->args;
  1422. struct xfrm_dump_info info;
  1423. info.in_skb = cb->skb;
  1424. info.out_skb = skb;
  1425. info.nlmsg_seq = cb->nlh->nlmsg_seq;
  1426. info.nlmsg_flags = NLM_F_MULTI;
  1427. (void) xfrm_policy_walk(net, walk, dump_one_policy, &info);
  1428. return skb->len;
  1429. }
  1430. static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
  1431. struct xfrm_policy *xp,
  1432. int dir, u32 seq)
  1433. {
  1434. struct xfrm_dump_info info;
  1435. struct sk_buff *skb;
  1436. int err;
  1437. skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  1438. if (!skb)
  1439. return ERR_PTR(-ENOMEM);
  1440. info.in_skb = in_skb;
  1441. info.out_skb = skb;
  1442. info.nlmsg_seq = seq;
  1443. info.nlmsg_flags = 0;
  1444. err = dump_one_policy(xp, dir, 0, &info);
  1445. if (err) {
  1446. kfree_skb(skb);
  1447. return ERR_PTR(err);
  1448. }
  1449. return skb;
  1450. }
  1451. static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
  1452. struct nlattr **attrs)
  1453. {
  1454. struct net *net = sock_net(skb->sk);
  1455. struct xfrm_policy *xp;
  1456. struct xfrm_userpolicy_id *p;
  1457. u8 type = XFRM_POLICY_TYPE_MAIN;
  1458. int err;
  1459. struct km_event c;
  1460. int delete;
  1461. struct xfrm_mark m;
  1462. u32 mark = xfrm_mark_get(attrs, &m);
  1463. p = nlmsg_data(nlh);
  1464. delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
  1465. err = copy_from_user_policy_type(&type, attrs);
  1466. if (err)
  1467. return err;
  1468. err = verify_policy_dir(p->dir);
  1469. if (err)
  1470. return err;
  1471. if (p->index)
  1472. xp = xfrm_policy_byid(net, mark, type, p->dir, p->index, delete, &err);
  1473. else {
  1474. struct nlattr *rt = attrs[XFRMA_SEC_CTX];
  1475. struct xfrm_sec_ctx *ctx;
  1476. err = verify_sec_ctx_len(attrs);
  1477. if (err)
  1478. return err;
  1479. ctx = NULL;
  1480. if (rt) {
  1481. struct xfrm_user_sec_ctx *uctx = nla_data(rt);
  1482. err = security_xfrm_policy_alloc(&ctx, uctx, GFP_KERNEL);
  1483. if (err)
  1484. return err;
  1485. }
  1486. xp = xfrm_policy_bysel_ctx(net, mark, type, p->dir, &p->sel,
  1487. ctx, delete, &err);
  1488. security_xfrm_policy_free(ctx);
  1489. }
  1490. if (xp == NULL)
  1491. return -ENOENT;
  1492. if (!delete) {
  1493. struct sk_buff *resp_skb;
  1494. resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
  1495. if (IS_ERR(resp_skb)) {
  1496. err = PTR_ERR(resp_skb);
  1497. } else {
  1498. err = nlmsg_unicast(net->xfrm.nlsk, resp_skb,
  1499. NETLINK_CB(skb).portid);
  1500. }
  1501. } else {
  1502. xfrm_audit_policy_delete(xp, err ? 0 : 1, true);
  1503. if (err != 0)
  1504. goto out;
  1505. c.data.byid = p->index;
  1506. c.event = nlh->nlmsg_type;
  1507. c.seq = nlh->nlmsg_seq;
  1508. c.portid = nlh->nlmsg_pid;
  1509. km_policy_notify(xp, p->dir, &c);
  1510. }
  1511. out:
  1512. xfrm_pol_put(xp);
  1513. return err;
  1514. }
  1515. static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
  1516. struct nlattr **attrs)
  1517. {
  1518. struct net *net = sock_net(skb->sk);
  1519. struct km_event c;
  1520. struct xfrm_usersa_flush *p = nlmsg_data(nlh);
  1521. int err;
  1522. err = xfrm_state_flush(net, p->proto, true);
  1523. if (err) {
  1524. if (err == -ESRCH) /* empty table */
  1525. return 0;
  1526. return err;
  1527. }
  1528. c.data.proto = p->proto;
  1529. c.event = nlh->nlmsg_type;
  1530. c.seq = nlh->nlmsg_seq;
  1531. c.portid = nlh->nlmsg_pid;
  1532. c.net = net;
  1533. km_state_notify(NULL, &c);
  1534. return 0;
  1535. }
  1536. static inline size_t xfrm_aevent_msgsize(struct xfrm_state *x)
  1537. {
  1538. size_t replay_size = x->replay_esn ?
  1539. xfrm_replay_state_esn_len(x->replay_esn) :
  1540. sizeof(struct xfrm_replay_state);
  1541. return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id))
  1542. + nla_total_size(replay_size)
  1543. + nla_total_size_64bit(sizeof(struct xfrm_lifetime_cur))
  1544. + nla_total_size(sizeof(struct xfrm_mark))
  1545. + nla_total_size(4) /* XFRM_AE_RTHR */
  1546. + nla_total_size(4); /* XFRM_AE_ETHR */
  1547. }
  1548. static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
  1549. {
  1550. struct xfrm_aevent_id *id;
  1551. struct nlmsghdr *nlh;
  1552. int err;
  1553. nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0);
  1554. if (nlh == NULL)
  1555. return -EMSGSIZE;
  1556. id = nlmsg_data(nlh);
  1557. memset(&id->sa_id, 0, sizeof(id->sa_id));
  1558. memcpy(&id->sa_id.daddr, &x->id.daddr, sizeof(x->id.daddr));
  1559. id->sa_id.spi = x->id.spi;
  1560. id->sa_id.family = x->props.family;
  1561. id->sa_id.proto = x->id.proto;
  1562. memcpy(&id->saddr, &x->props.saddr, sizeof(x->props.saddr));
  1563. id->reqid = x->props.reqid;
  1564. id->flags = c->data.aevent;
  1565. if (x->replay_esn) {
  1566. err = nla_put(skb, XFRMA_REPLAY_ESN_VAL,
  1567. xfrm_replay_state_esn_len(x->replay_esn),
  1568. x->replay_esn);
  1569. } else {
  1570. err = nla_put(skb, XFRMA_REPLAY_VAL, sizeof(x->replay),
  1571. &x->replay);
  1572. }
  1573. if (err)
  1574. goto out_cancel;
  1575. err = nla_put_64bit(skb, XFRMA_LTIME_VAL, sizeof(x->curlft), &x->curlft,
  1576. XFRMA_PAD);
  1577. if (err)
  1578. goto out_cancel;
  1579. if (id->flags & XFRM_AE_RTHR) {
  1580. err = nla_put_u32(skb, XFRMA_REPLAY_THRESH, x->replay_maxdiff);
  1581. if (err)
  1582. goto out_cancel;
  1583. }
  1584. if (id->flags & XFRM_AE_ETHR) {
  1585. err = nla_put_u32(skb, XFRMA_ETIMER_THRESH,
  1586. x->replay_maxage * 10 / HZ);
  1587. if (err)
  1588. goto out_cancel;
  1589. }
  1590. err = xfrm_mark_put(skb, &x->mark);
  1591. if (err)
  1592. goto out_cancel;
  1593. nlmsg_end(skb, nlh);
  1594. return 0;
  1595. out_cancel:
  1596. nlmsg_cancel(skb, nlh);
  1597. return err;
  1598. }
  1599. static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
  1600. struct nlattr **attrs)
  1601. {
  1602. struct net *net = sock_net(skb->sk);
  1603. struct xfrm_state *x;
  1604. struct sk_buff *r_skb;
  1605. int err;
  1606. struct km_event c;
  1607. u32 mark;
  1608. struct xfrm_mark m;
  1609. struct xfrm_aevent_id *p = nlmsg_data(nlh);
  1610. struct xfrm_usersa_id *id = &p->sa_id;
  1611. mark = xfrm_mark_get(attrs, &m);
  1612. x = xfrm_state_lookup(net, mark, &id->daddr, id->spi, id->proto, id->family);
  1613. if (x == NULL)
  1614. return -ESRCH;
  1615. r_skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
  1616. if (r_skb == NULL) {
  1617. xfrm_state_put(x);
  1618. return -ENOMEM;
  1619. }
  1620. /*
  1621. * XXX: is this lock really needed - none of the other
  1622. * gets lock (the concern is things getting updated
  1623. * while we are still reading) - jhs
  1624. */
  1625. spin_lock_bh(&x->lock);
  1626. c.data.aevent = p->flags;
  1627. c.seq = nlh->nlmsg_seq;
  1628. c.portid = nlh->nlmsg_pid;
  1629. if (build_aevent(r_skb, x, &c) < 0)
  1630. BUG();
  1631. err = nlmsg_unicast(net->xfrm.nlsk, r_skb, NETLINK_CB(skb).portid);
  1632. spin_unlock_bh(&x->lock);
  1633. xfrm_state_put(x);
  1634. return err;
  1635. }
  1636. static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
  1637. struct nlattr **attrs)
  1638. {
  1639. struct net *net = sock_net(skb->sk);
  1640. struct xfrm_state *x;
  1641. struct km_event c;
  1642. int err = -EINVAL;
  1643. u32 mark = 0;
  1644. struct xfrm_mark m;
  1645. struct xfrm_aevent_id *p = nlmsg_data(nlh);
  1646. struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
  1647. struct nlattr *re = attrs[XFRMA_REPLAY_ESN_VAL];
  1648. struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
  1649. struct nlattr *et = attrs[XFRMA_ETIMER_THRESH];
  1650. struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH];
  1651. if (!lt && !rp && !re && !et && !rt)
  1652. return err;
  1653. /* pedantic mode - thou shalt sayeth replaceth */
  1654. if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
  1655. return err;
  1656. mark = xfrm_mark_get(attrs, &m);
  1657. x = xfrm_state_lookup(net, mark, &p->sa_id.daddr, p->sa_id.spi, p->sa_id.proto, p->sa_id.family);
  1658. if (x == NULL)
  1659. return -ESRCH;
  1660. if (x->km.state != XFRM_STATE_VALID)
  1661. goto out;
  1662. err = xfrm_replay_verify_len(x->replay_esn, re);
  1663. if (err)
  1664. goto out;
  1665. spin_lock_bh(&x->lock);
  1666. xfrm_update_ae_params(x, attrs, 1);
  1667. spin_unlock_bh(&x->lock);
  1668. c.event = nlh->nlmsg_type;
  1669. c.seq = nlh->nlmsg_seq;
  1670. c.portid = nlh->nlmsg_pid;
  1671. c.data.aevent = XFRM_AE_CU;
  1672. km_state_notify(x, &c);
  1673. err = 0;
  1674. out:
  1675. xfrm_state_put(x);
  1676. return err;
  1677. }
  1678. static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
  1679. struct nlattr **attrs)
  1680. {
  1681. struct net *net = sock_net(skb->sk);
  1682. struct km_event c;
  1683. u8 type = XFRM_POLICY_TYPE_MAIN;
  1684. int err;
  1685. err = copy_from_user_policy_type(&type, attrs);
  1686. if (err)
  1687. return err;
  1688. err = xfrm_policy_flush(net, type, true);
  1689. if (err) {
  1690. if (err == -ESRCH) /* empty table */
  1691. return 0;
  1692. return err;
  1693. }
  1694. c.data.type = type;
  1695. c.event = nlh->nlmsg_type;
  1696. c.seq = nlh->nlmsg_seq;
  1697. c.portid = nlh->nlmsg_pid;
  1698. c.net = net;
  1699. km_policy_notify(NULL, 0, &c);
  1700. return 0;
  1701. }
  1702. static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
  1703. struct nlattr **attrs)
  1704. {
  1705. struct net *net = sock_net(skb->sk);
  1706. struct xfrm_policy *xp;
  1707. struct xfrm_user_polexpire *up = nlmsg_data(nlh);
  1708. struct xfrm_userpolicy_info *p = &up->pol;
  1709. u8 type = XFRM_POLICY_TYPE_MAIN;
  1710. int err = -ENOENT;
  1711. struct xfrm_mark m;
  1712. u32 mark = xfrm_mark_get(attrs, &m);
  1713. err = copy_from_user_policy_type(&type, attrs);
  1714. if (err)
  1715. return err;
  1716. err = verify_policy_dir(p->dir);
  1717. if (err)
  1718. return err;
  1719. if (p->index)
  1720. xp = xfrm_policy_byid(net, mark, type, p->dir, p->index, 0, &err);
  1721. else {
  1722. struct nlattr *rt = attrs[XFRMA_SEC_CTX];
  1723. struct xfrm_sec_ctx *ctx;
  1724. err = verify_sec_ctx_len(attrs);
  1725. if (err)
  1726. return err;
  1727. ctx = NULL;
  1728. if (rt) {
  1729. struct xfrm_user_sec_ctx *uctx = nla_data(rt);
  1730. err = security_xfrm_policy_alloc(&ctx, uctx, GFP_KERNEL);
  1731. if (err)
  1732. return err;
  1733. }
  1734. xp = xfrm_policy_bysel_ctx(net, mark, type, p->dir,
  1735. &p->sel, ctx, 0, &err);
  1736. security_xfrm_policy_free(ctx);
  1737. }
  1738. if (xp == NULL)
  1739. return -ENOENT;
  1740. if (unlikely(xp->walk.dead))
  1741. goto out;
  1742. err = 0;
  1743. if (up->hard) {
  1744. xfrm_policy_delete(xp, p->dir);
  1745. xfrm_audit_policy_delete(xp, 1, true);
  1746. }
  1747. km_policy_expired(xp, p->dir, up->hard, nlh->nlmsg_pid);
  1748. out:
  1749. xfrm_pol_put(xp);
  1750. return err;
  1751. }
  1752. static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
  1753. struct nlattr **attrs)
  1754. {
  1755. struct net *net = sock_net(skb->sk);
  1756. struct xfrm_state *x;
  1757. int err;
  1758. struct xfrm_user_expire *ue = nlmsg_data(nlh);
  1759. struct xfrm_usersa_info *p = &ue->state;
  1760. struct xfrm_mark m;
  1761. u32 mark = xfrm_mark_get(attrs, &m);
  1762. x = xfrm_state_lookup(net, mark, &p->id.daddr, p->id.spi, p->id.proto, p->family);
  1763. err = -ENOENT;
  1764. if (x == NULL)
  1765. return err;
  1766. spin_lock_bh(&x->lock);
  1767. err = -EINVAL;
  1768. if (x->km.state != XFRM_STATE_VALID)
  1769. goto out;
  1770. km_state_expired(x, ue->hard, nlh->nlmsg_pid);
  1771. if (ue->hard) {
  1772. __xfrm_state_delete(x);
  1773. xfrm_audit_state_delete(x, 1, true);
  1774. }
  1775. err = 0;
  1776. out:
  1777. spin_unlock_bh(&x->lock);
  1778. xfrm_state_put(x);
  1779. return err;
  1780. }
  1781. static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh,
  1782. struct nlattr **attrs)
  1783. {
  1784. struct net *net = sock_net(skb->sk);
  1785. struct xfrm_policy *xp;
  1786. struct xfrm_user_tmpl *ut;
  1787. int i;
  1788. struct nlattr *rt = attrs[XFRMA_TMPL];
  1789. struct xfrm_mark mark;
  1790. struct xfrm_user_acquire *ua = nlmsg_data(nlh);
  1791. struct xfrm_state *x = xfrm_state_alloc(net);
  1792. int err = -ENOMEM;
  1793. if (!x)
  1794. goto nomem;
  1795. xfrm_mark_get(attrs, &mark);
  1796. err = verify_newpolicy_info(&ua->policy);
  1797. if (err)
  1798. goto free_state;
  1799. /* build an XP */
  1800. xp = xfrm_policy_construct(net, &ua->policy, attrs, &err);
  1801. if (!xp)
  1802. goto free_state;
  1803. memcpy(&x->id, &ua->id, sizeof(ua->id));
  1804. memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
  1805. memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
  1806. xp->mark.m = x->mark.m = mark.m;
  1807. xp->mark.v = x->mark.v = mark.v;
  1808. ut = nla_data(rt);
  1809. /* extract the templates and for each call km_key */
  1810. for (i = 0; i < xp->xfrm_nr; i++, ut++) {
  1811. struct xfrm_tmpl *t = &xp->xfrm_vec[i];
  1812. memcpy(&x->id, &t->id, sizeof(x->id));
  1813. x->props.mode = t->mode;
  1814. x->props.reqid = t->reqid;
  1815. x->props.family = ut->family;
  1816. t->aalgos = ua->aalgos;
  1817. t->ealgos = ua->ealgos;
  1818. t->calgos = ua->calgos;
  1819. err = km_query(x, t, xp);
  1820. }
  1821. kfree(x);
  1822. kfree(xp);
  1823. return 0;
  1824. free_state:
  1825. kfree(x);
  1826. nomem:
  1827. return err;
  1828. }
  1829. #ifdef CONFIG_XFRM_MIGRATE
  1830. static int copy_from_user_migrate(struct xfrm_migrate *ma,
  1831. struct xfrm_kmaddress *k,
  1832. struct nlattr **attrs, int *num)
  1833. {
  1834. struct nlattr *rt = attrs[XFRMA_MIGRATE];
  1835. struct xfrm_user_migrate *um;
  1836. int i, num_migrate;
  1837. if (k != NULL) {
  1838. struct xfrm_user_kmaddress *uk;
  1839. uk = nla_data(attrs[XFRMA_KMADDRESS]);
  1840. memcpy(&k->local, &uk->local, sizeof(k->local));
  1841. memcpy(&k->remote, &uk->remote, sizeof(k->remote));
  1842. k->family = uk->family;
  1843. k->reserved = uk->reserved;
  1844. }
  1845. um = nla_data(rt);
  1846. num_migrate = nla_len(rt) / sizeof(*um);
  1847. if (num_migrate <= 0 || num_migrate > XFRM_MAX_DEPTH)
  1848. return -EINVAL;
  1849. for (i = 0; i < num_migrate; i++, um++, ma++) {
  1850. memcpy(&ma->old_daddr, &um->old_daddr, sizeof(ma->old_daddr));
  1851. memcpy(&ma->old_saddr, &um->old_saddr, sizeof(ma->old_saddr));
  1852. memcpy(&ma->new_daddr, &um->new_daddr, sizeof(ma->new_daddr));
  1853. memcpy(&ma->new_saddr, &um->new_saddr, sizeof(ma->new_saddr));
  1854. ma->proto = um->proto;
  1855. ma->mode = um->mode;
  1856. ma->reqid = um->reqid;
  1857. ma->old_family = um->old_family;
  1858. ma->new_family = um->new_family;
  1859. }
  1860. *num = i;
  1861. return 0;
  1862. }
  1863. static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
  1864. struct nlattr **attrs)
  1865. {
  1866. struct xfrm_userpolicy_id *pi = nlmsg_data(nlh);
  1867. struct xfrm_migrate m[XFRM_MAX_DEPTH];
  1868. struct xfrm_kmaddress km, *kmp;
  1869. u8 type;
  1870. int err;
  1871. int n = 0;
  1872. struct net *net = sock_net(skb->sk);
  1873. struct xfrm_encap_tmpl *encap = NULL;
  1874. if (attrs[XFRMA_MIGRATE] == NULL)
  1875. return -EINVAL;
  1876. kmp = attrs[XFRMA_KMADDRESS] ? &km : NULL;
  1877. err = copy_from_user_policy_type(&type, attrs);
  1878. if (err)
  1879. return err;
  1880. err = copy_from_user_migrate((struct xfrm_migrate *)m, kmp, attrs, &n);
  1881. if (err)
  1882. return err;
  1883. if (!n)
  1884. return 0;
  1885. if (attrs[XFRMA_ENCAP]) {
  1886. encap = kmemdup(nla_data(attrs[XFRMA_ENCAP]),
  1887. sizeof(*encap), GFP_KERNEL);
  1888. if (!encap)
  1889. return 0;
  1890. }
  1891. err = xfrm_migrate(&pi->sel, pi->dir, type, m, n, kmp, net, encap);
  1892. kfree(encap);
  1893. return err;
  1894. }
  1895. #else
  1896. static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
  1897. struct nlattr **attrs)
  1898. {
  1899. return -ENOPROTOOPT;
  1900. }
  1901. #endif
  1902. #ifdef CONFIG_XFRM_MIGRATE
  1903. static int copy_to_user_migrate(const struct xfrm_migrate *m, struct sk_buff *skb)
  1904. {
  1905. struct xfrm_user_migrate um;
  1906. memset(&um, 0, sizeof(um));
  1907. um.proto = m->proto;
  1908. um.mode = m->mode;
  1909. um.reqid = m->reqid;
  1910. um.old_family = m->old_family;
  1911. memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr));
  1912. memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr));
  1913. um.new_family = m->new_family;
  1914. memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr));
  1915. memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr));
  1916. return nla_put(skb, XFRMA_MIGRATE, sizeof(um), &um);
  1917. }
  1918. static int copy_to_user_kmaddress(const struct xfrm_kmaddress *k, struct sk_buff *skb)
  1919. {
  1920. struct xfrm_user_kmaddress uk;
  1921. memset(&uk, 0, sizeof(uk));
  1922. uk.family = k->family;
  1923. uk.reserved = k->reserved;
  1924. memcpy(&uk.local, &k->local, sizeof(uk.local));
  1925. memcpy(&uk.remote, &k->remote, sizeof(uk.remote));
  1926. return nla_put(skb, XFRMA_KMADDRESS, sizeof(uk), &uk);
  1927. }
  1928. static inline size_t xfrm_migrate_msgsize(int num_migrate, int with_kma,
  1929. int with_encp)
  1930. {
  1931. return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id))
  1932. + (with_kma ? nla_total_size(sizeof(struct xfrm_kmaddress)) : 0)
  1933. + (with_encp ? nla_total_size(sizeof(struct xfrm_encap_tmpl)) : 0)
  1934. + nla_total_size(sizeof(struct xfrm_user_migrate) * num_migrate)
  1935. + userpolicy_type_attrsize();
  1936. }
  1937. static int build_migrate(struct sk_buff *skb, const struct xfrm_migrate *m,
  1938. int num_migrate, const struct xfrm_kmaddress *k,
  1939. const struct xfrm_selector *sel,
  1940. const struct xfrm_encap_tmpl *encap, u8 dir, u8 type)
  1941. {
  1942. const struct xfrm_migrate *mp;
  1943. struct xfrm_userpolicy_id *pol_id;
  1944. struct nlmsghdr *nlh;
  1945. int i, err;
  1946. nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id), 0);
  1947. if (nlh == NULL)
  1948. return -EMSGSIZE;
  1949. pol_id = nlmsg_data(nlh);
  1950. /* copy data from selector, dir, and type to the pol_id */
  1951. memset(pol_id, 0, sizeof(*pol_id));
  1952. memcpy(&pol_id->sel, sel, sizeof(pol_id->sel));
  1953. pol_id->dir = dir;
  1954. if (k != NULL) {
  1955. err = copy_to_user_kmaddress(k, skb);
  1956. if (err)
  1957. goto out_cancel;
  1958. }
  1959. if (encap) {
  1960. err = nla_put(skb, XFRMA_ENCAP, sizeof(*encap), encap);
  1961. if (err)
  1962. goto out_cancel;
  1963. }
  1964. err = copy_to_user_policy_type(type, skb);
  1965. if (err)
  1966. goto out_cancel;
  1967. for (i = 0, mp = m ; i < num_migrate; i++, mp++) {
  1968. err = copy_to_user_migrate(mp, skb);
  1969. if (err)
  1970. goto out_cancel;
  1971. }
  1972. nlmsg_end(skb, nlh);
  1973. return 0;
  1974. out_cancel:
  1975. nlmsg_cancel(skb, nlh);
  1976. return err;
  1977. }
  1978. static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
  1979. const struct xfrm_migrate *m, int num_migrate,
  1980. const struct xfrm_kmaddress *k,
  1981. const struct xfrm_encap_tmpl *encap)
  1982. {
  1983. struct net *net = &init_net;
  1984. struct sk_buff *skb;
  1985. skb = nlmsg_new(xfrm_migrate_msgsize(num_migrate, !!k, !!encap),
  1986. GFP_ATOMIC);
  1987. if (skb == NULL)
  1988. return -ENOMEM;
  1989. /* build migrate */
  1990. if (build_migrate(skb, m, num_migrate, k, sel, encap, dir, type) < 0)
  1991. BUG();
  1992. return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_MIGRATE);
  1993. }
  1994. #else
  1995. static int xfrm_send_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
  1996. const struct xfrm_migrate *m, int num_migrate,
  1997. const struct xfrm_kmaddress *k,
  1998. const struct xfrm_encap_tmpl *encap)
  1999. {
  2000. return -ENOPROTOOPT;
  2001. }
  2002. #endif
  2003. #define XMSGSIZE(type) sizeof(struct type)
  2004. static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
  2005. [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
  2006. [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
  2007. [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
  2008. [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
  2009. [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
  2010. [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
  2011. [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
  2012. [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
  2013. [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
  2014. [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
  2015. [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
  2016. [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
  2017. [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
  2018. [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = 0,
  2019. [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
  2020. [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
  2021. [XFRM_MSG_REPORT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report),
  2022. [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
  2023. [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = sizeof(u32),
  2024. [XFRM_MSG_NEWSPDINFO - XFRM_MSG_BASE] = sizeof(u32),
  2025. [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = sizeof(u32),
  2026. };
  2027. #undef XMSGSIZE
  2028. static const struct nla_policy xfrma_policy[XFRMA_MAX+1] = {
  2029. [XFRMA_SA] = { .len = sizeof(struct xfrm_usersa_info)},
  2030. [XFRMA_POLICY] = { .len = sizeof(struct xfrm_userpolicy_info)},
  2031. [XFRMA_LASTUSED] = { .type = NLA_U64},
  2032. [XFRMA_ALG_AUTH_TRUNC] = { .len = sizeof(struct xfrm_algo_auth)},
  2033. [XFRMA_ALG_AEAD] = { .len = sizeof(struct xfrm_algo_aead) },
  2034. [XFRMA_ALG_AUTH] = { .len = sizeof(struct xfrm_algo) },
  2035. [XFRMA_ALG_CRYPT] = { .len = sizeof(struct xfrm_algo) },
  2036. [XFRMA_ALG_COMP] = { .len = sizeof(struct xfrm_algo) },
  2037. [XFRMA_ENCAP] = { .len = sizeof(struct xfrm_encap_tmpl) },
  2038. [XFRMA_TMPL] = { .len = sizeof(struct xfrm_user_tmpl) },
  2039. [XFRMA_SEC_CTX] = { .len = sizeof(struct xfrm_sec_ctx) },
  2040. [XFRMA_LTIME_VAL] = { .len = sizeof(struct xfrm_lifetime_cur) },
  2041. [XFRMA_REPLAY_VAL] = { .len = sizeof(struct xfrm_replay_state) },
  2042. [XFRMA_REPLAY_THRESH] = { .type = NLA_U32 },
  2043. [XFRMA_ETIMER_THRESH] = { .type = NLA_U32 },
  2044. [XFRMA_SRCADDR] = { .len = sizeof(xfrm_address_t) },
  2045. [XFRMA_COADDR] = { .len = sizeof(xfrm_address_t) },
  2046. [XFRMA_POLICY_TYPE] = { .len = sizeof(struct xfrm_userpolicy_type)},
  2047. [XFRMA_MIGRATE] = { .len = sizeof(struct xfrm_user_migrate) },
  2048. [XFRMA_KMADDRESS] = { .len = sizeof(struct xfrm_user_kmaddress) },
  2049. [XFRMA_MARK] = { .len = sizeof(struct xfrm_mark) },
  2050. [XFRMA_TFCPAD] = { .type = NLA_U32 },
  2051. [XFRMA_REPLAY_ESN_VAL] = { .len = sizeof(struct xfrm_replay_state_esn) },
  2052. [XFRMA_SA_EXTRA_FLAGS] = { .type = NLA_U32 },
  2053. [XFRMA_PROTO] = { .type = NLA_U8 },
  2054. [XFRMA_ADDRESS_FILTER] = { .len = sizeof(struct xfrm_address_filter) },
  2055. [XFRMA_OFFLOAD_DEV] = { .len = sizeof(struct xfrm_user_offload) },
  2056. [XFRMA_OUTPUT_MARK] = { .len = NLA_U32 },
  2057. };
  2058. static const struct nla_policy xfrma_spd_policy[XFRMA_SPD_MAX+1] = {
  2059. [XFRMA_SPD_IPV4_HTHRESH] = { .len = sizeof(struct xfrmu_spdhthresh) },
  2060. [XFRMA_SPD_IPV6_HTHRESH] = { .len = sizeof(struct xfrmu_spdhthresh) },
  2061. };
  2062. static const struct xfrm_link {
  2063. int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **);
  2064. int (*start)(struct netlink_callback *);
  2065. int (*dump)(struct sk_buff *, struct netlink_callback *);
  2066. int (*done)(struct netlink_callback *);
  2067. const struct nla_policy *nla_pol;
  2068. int nla_max;
  2069. } xfrm_dispatch[XFRM_NR_MSGTYPES] = {
  2070. [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
  2071. [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa },
  2072. [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
  2073. .dump = xfrm_dump_sa,
  2074. .done = xfrm_dump_sa_done },
  2075. [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
  2076. [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy },
  2077. [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
  2078. .start = xfrm_dump_policy_start,
  2079. .dump = xfrm_dump_policy,
  2080. .done = xfrm_dump_policy_done },
  2081. [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
  2082. [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire },
  2083. [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
  2084. [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
  2085. [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
  2086. [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
  2087. [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa },
  2088. [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy },
  2089. [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = { .doit = xfrm_new_ae },
  2090. [XFRM_MSG_GETAE - XFRM_MSG_BASE] = { .doit = xfrm_get_ae },
  2091. [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = { .doit = xfrm_do_migrate },
  2092. [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_sadinfo },
  2093. [XFRM_MSG_NEWSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_set_spdinfo,
  2094. .nla_pol = xfrma_spd_policy,
  2095. .nla_max = XFRMA_SPD_MAX },
  2096. [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_spdinfo },
  2097. };
  2098. static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh,
  2099. struct netlink_ext_ack *extack)
  2100. {
  2101. struct net *net = sock_net(skb->sk);
  2102. struct nlattr *attrs[XFRMA_MAX+1];
  2103. const struct xfrm_link *link;
  2104. int type, err;
  2105. #ifdef CONFIG_COMPAT
  2106. if (in_compat_syscall())
  2107. return -EOPNOTSUPP;
  2108. #endif
  2109. type = nlh->nlmsg_type;
  2110. if (type > XFRM_MSG_MAX)
  2111. return -EINVAL;
  2112. type -= XFRM_MSG_BASE;
  2113. link = &xfrm_dispatch[type];
  2114. /* All operations require privileges, even GET */
  2115. if (!netlink_net_capable(skb, CAP_NET_ADMIN))
  2116. return -EPERM;
  2117. if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
  2118. type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
  2119. (nlh->nlmsg_flags & NLM_F_DUMP)) {
  2120. if (link->dump == NULL)
  2121. return -EINVAL;
  2122. {
  2123. struct netlink_dump_control c = {
  2124. .start = link->start,
  2125. .dump = link->dump,
  2126. .done = link->done,
  2127. };
  2128. return netlink_dump_start(net->xfrm.nlsk, skb, nlh, &c);
  2129. }
  2130. }
  2131. err = nlmsg_parse(nlh, xfrm_msg_min[type], attrs,
  2132. link->nla_max ? : XFRMA_MAX,
  2133. link->nla_pol ? : xfrma_policy, extack);
  2134. if (err < 0)
  2135. return err;
  2136. if (link->doit == NULL)
  2137. return -EINVAL;
  2138. return link->doit(skb, nlh, attrs);
  2139. }
  2140. static void xfrm_netlink_rcv(struct sk_buff *skb)
  2141. {
  2142. struct net *net = sock_net(skb->sk);
  2143. mutex_lock(&net->xfrm.xfrm_cfg_mutex);
  2144. netlink_rcv_skb(skb, &xfrm_user_rcv_msg);
  2145. mutex_unlock(&net->xfrm.xfrm_cfg_mutex);
  2146. }
  2147. static inline size_t xfrm_expire_msgsize(void)
  2148. {
  2149. return NLMSG_ALIGN(sizeof(struct xfrm_user_expire))
  2150. + nla_total_size(sizeof(struct xfrm_mark));
  2151. }
  2152. static int build_expire(struct sk_buff *skb, struct xfrm_state *x, const struct km_event *c)
  2153. {
  2154. struct xfrm_user_expire *ue;
  2155. struct nlmsghdr *nlh;
  2156. int err;
  2157. nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_EXPIRE, sizeof(*ue), 0);
  2158. if (nlh == NULL)
  2159. return -EMSGSIZE;
  2160. ue = nlmsg_data(nlh);
  2161. copy_to_user_state(x, &ue->state);
  2162. ue->hard = (c->data.hard != 0) ? 1 : 0;
  2163. /* clear the padding bytes */
  2164. memset(&ue->hard + 1, 0, sizeof(*ue) - offsetofend(typeof(*ue), hard));
  2165. err = xfrm_mark_put(skb, &x->mark);
  2166. if (err)
  2167. return err;
  2168. nlmsg_end(skb, nlh);
  2169. return 0;
  2170. }
  2171. static int xfrm_exp_state_notify(struct xfrm_state *x, const struct km_event *c)
  2172. {
  2173. struct net *net = xs_net(x);
  2174. struct sk_buff *skb;
  2175. skb = nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC);
  2176. if (skb == NULL)
  2177. return -ENOMEM;
  2178. if (build_expire(skb, x, c) < 0) {
  2179. kfree_skb(skb);
  2180. return -EMSGSIZE;
  2181. }
  2182. return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_EXPIRE);
  2183. }
  2184. static int xfrm_aevent_state_notify(struct xfrm_state *x, const struct km_event *c)
  2185. {
  2186. struct net *net = xs_net(x);
  2187. struct sk_buff *skb;
  2188. skb = nlmsg_new(xfrm_aevent_msgsize(x), GFP_ATOMIC);
  2189. if (skb == NULL)
  2190. return -ENOMEM;
  2191. if (build_aevent(skb, x, c) < 0)
  2192. BUG();
  2193. return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_AEVENTS);
  2194. }
  2195. static int xfrm_notify_sa_flush(const struct km_event *c)
  2196. {
  2197. struct net *net = c->net;
  2198. struct xfrm_usersa_flush *p;
  2199. struct nlmsghdr *nlh;
  2200. struct sk_buff *skb;
  2201. int len = NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush));
  2202. skb = nlmsg_new(len, GFP_ATOMIC);
  2203. if (skb == NULL)
  2204. return -ENOMEM;
  2205. nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHSA, sizeof(*p), 0);
  2206. if (nlh == NULL) {
  2207. kfree_skb(skb);
  2208. return -EMSGSIZE;
  2209. }
  2210. p = nlmsg_data(nlh);
  2211. p->proto = c->data.proto;
  2212. nlmsg_end(skb, nlh);
  2213. return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_SA);
  2214. }
  2215. static inline size_t xfrm_sa_len(struct xfrm_state *x)
  2216. {
  2217. size_t l = 0;
  2218. if (x->aead)
  2219. l += nla_total_size(aead_len(x->aead));
  2220. if (x->aalg) {
  2221. l += nla_total_size(sizeof(struct xfrm_algo) +
  2222. (x->aalg->alg_key_len + 7) / 8);
  2223. l += nla_total_size(xfrm_alg_auth_len(x->aalg));
  2224. }
  2225. if (x->ealg)
  2226. l += nla_total_size(xfrm_alg_len(x->ealg));
  2227. if (x->calg)
  2228. l += nla_total_size(sizeof(*x->calg));
  2229. if (x->encap)
  2230. l += nla_total_size(sizeof(*x->encap));
  2231. if (x->tfcpad)
  2232. l += nla_total_size(sizeof(x->tfcpad));
  2233. if (x->replay_esn)
  2234. l += nla_total_size(xfrm_replay_state_esn_len(x->replay_esn));
  2235. else
  2236. l += nla_total_size(sizeof(struct xfrm_replay_state));
  2237. if (x->security)
  2238. l += nla_total_size(sizeof(struct xfrm_user_sec_ctx) +
  2239. x->security->ctx_len);
  2240. if (x->coaddr)
  2241. l += nla_total_size(sizeof(*x->coaddr));
  2242. if (x->props.extra_flags)
  2243. l += nla_total_size(sizeof(x->props.extra_flags));
  2244. if (x->xso.dev)
  2245. l += nla_total_size(sizeof(x->xso));
  2246. if (x->props.output_mark)
  2247. l += nla_total_size(sizeof(x->props.output_mark));
  2248. /* Must count x->lastused as it may become non-zero behind our back. */
  2249. l += nla_total_size_64bit(sizeof(u64));
  2250. return l;
  2251. }
  2252. static int xfrm_notify_sa(struct xfrm_state *x, const struct km_event *c)
  2253. {
  2254. struct net *net = xs_net(x);
  2255. struct xfrm_usersa_info *p;
  2256. struct xfrm_usersa_id *id;
  2257. struct nlmsghdr *nlh;
  2258. struct sk_buff *skb;
  2259. int len = xfrm_sa_len(x);
  2260. int headlen, err;
  2261. headlen = sizeof(*p);
  2262. if (c->event == XFRM_MSG_DELSA) {
  2263. len += nla_total_size(headlen);
  2264. headlen = sizeof(*id);
  2265. len += nla_total_size(sizeof(struct xfrm_mark));
  2266. }
  2267. len += NLMSG_ALIGN(headlen);
  2268. skb = nlmsg_new(len, GFP_ATOMIC);
  2269. if (skb == NULL)
  2270. return -ENOMEM;
  2271. nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0);
  2272. err = -EMSGSIZE;
  2273. if (nlh == NULL)
  2274. goto out_free_skb;
  2275. p = nlmsg_data(nlh);
  2276. if (c->event == XFRM_MSG_DELSA) {
  2277. struct nlattr *attr;
  2278. id = nlmsg_data(nlh);
  2279. memset(id, 0, sizeof(*id));
  2280. memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
  2281. id->spi = x->id.spi;
  2282. id->family = x->props.family;
  2283. id->proto = x->id.proto;
  2284. attr = nla_reserve(skb, XFRMA_SA, sizeof(*p));
  2285. err = -EMSGSIZE;
  2286. if (attr == NULL)
  2287. goto out_free_skb;
  2288. p = nla_data(attr);
  2289. }
  2290. err = copy_to_user_state_extra(x, p, skb);
  2291. if (err)
  2292. goto out_free_skb;
  2293. nlmsg_end(skb, nlh);
  2294. return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_SA);
  2295. out_free_skb:
  2296. kfree_skb(skb);
  2297. return err;
  2298. }
  2299. static int xfrm_send_state_notify(struct xfrm_state *x, const struct km_event *c)
  2300. {
  2301. switch (c->event) {
  2302. case XFRM_MSG_EXPIRE:
  2303. return xfrm_exp_state_notify(x, c);
  2304. case XFRM_MSG_NEWAE:
  2305. return xfrm_aevent_state_notify(x, c);
  2306. case XFRM_MSG_DELSA:
  2307. case XFRM_MSG_UPDSA:
  2308. case XFRM_MSG_NEWSA:
  2309. return xfrm_notify_sa(x, c);
  2310. case XFRM_MSG_FLUSHSA:
  2311. return xfrm_notify_sa_flush(c);
  2312. default:
  2313. printk(KERN_NOTICE "xfrm_user: Unknown SA event %d\n",
  2314. c->event);
  2315. break;
  2316. }
  2317. return 0;
  2318. }
  2319. static inline size_t xfrm_acquire_msgsize(struct xfrm_state *x,
  2320. struct xfrm_policy *xp)
  2321. {
  2322. return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire))
  2323. + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
  2324. + nla_total_size(sizeof(struct xfrm_mark))
  2325. + nla_total_size(xfrm_user_sec_ctx_size(x->security))
  2326. + userpolicy_type_attrsize();
  2327. }
  2328. static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
  2329. struct xfrm_tmpl *xt, struct xfrm_policy *xp)
  2330. {
  2331. __u32 seq = xfrm_get_acqseq();
  2332. struct xfrm_user_acquire *ua;
  2333. struct nlmsghdr *nlh;
  2334. int err;
  2335. nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_ACQUIRE, sizeof(*ua), 0);
  2336. if (nlh == NULL)
  2337. return -EMSGSIZE;
  2338. ua = nlmsg_data(nlh);
  2339. memcpy(&ua->id, &x->id, sizeof(ua->id));
  2340. memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
  2341. memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
  2342. copy_to_user_policy(xp, &ua->policy, XFRM_POLICY_OUT);
  2343. ua->aalgos = xt->aalgos;
  2344. ua->ealgos = xt->ealgos;
  2345. ua->calgos = xt->calgos;
  2346. ua->seq = x->km.seq = seq;
  2347. err = copy_to_user_tmpl(xp, skb);
  2348. if (!err)
  2349. err = copy_to_user_state_sec_ctx(x, skb);
  2350. if (!err)
  2351. err = copy_to_user_policy_type(xp->type, skb);
  2352. if (!err)
  2353. err = xfrm_mark_put(skb, &xp->mark);
  2354. if (err) {
  2355. nlmsg_cancel(skb, nlh);
  2356. return err;
  2357. }
  2358. nlmsg_end(skb, nlh);
  2359. return 0;
  2360. }
  2361. static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
  2362. struct xfrm_policy *xp)
  2363. {
  2364. struct net *net = xs_net(x);
  2365. struct sk_buff *skb;
  2366. skb = nlmsg_new(xfrm_acquire_msgsize(x, xp), GFP_ATOMIC);
  2367. if (skb == NULL)
  2368. return -ENOMEM;
  2369. if (build_acquire(skb, x, xt, xp) < 0)
  2370. BUG();
  2371. return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_ACQUIRE);
  2372. }
  2373. /* User gives us xfrm_user_policy_info followed by an array of 0
  2374. * or more templates.
  2375. */
  2376. static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
  2377. u8 *data, int len, int *dir)
  2378. {
  2379. struct net *net = sock_net(sk);
  2380. struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
  2381. struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
  2382. struct xfrm_policy *xp;
  2383. int nr;
  2384. switch (sk->sk_family) {
  2385. case AF_INET:
  2386. if (opt != IP_XFRM_POLICY) {
  2387. *dir = -EOPNOTSUPP;
  2388. return NULL;
  2389. }
  2390. break;
  2391. #if IS_ENABLED(CONFIG_IPV6)
  2392. case AF_INET6:
  2393. if (opt != IPV6_XFRM_POLICY) {
  2394. *dir = -EOPNOTSUPP;
  2395. return NULL;
  2396. }
  2397. break;
  2398. #endif
  2399. default:
  2400. *dir = -EINVAL;
  2401. return NULL;
  2402. }
  2403. *dir = -EINVAL;
  2404. if (len < sizeof(*p) ||
  2405. verify_newpolicy_info(p))
  2406. return NULL;
  2407. nr = ((len - sizeof(*p)) / sizeof(*ut));
  2408. if (validate_tmpl(nr, ut, p->sel.family))
  2409. return NULL;
  2410. if (p->dir > XFRM_POLICY_OUT)
  2411. return NULL;
  2412. xp = xfrm_policy_alloc(net, GFP_ATOMIC);
  2413. if (xp == NULL) {
  2414. *dir = -ENOBUFS;
  2415. return NULL;
  2416. }
  2417. copy_from_user_policy(xp, p);
  2418. xp->type = XFRM_POLICY_TYPE_MAIN;
  2419. copy_templates(xp, ut, nr);
  2420. *dir = p->dir;
  2421. return xp;
  2422. }
  2423. static inline size_t xfrm_polexpire_msgsize(struct xfrm_policy *xp)
  2424. {
  2425. return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire))
  2426. + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
  2427. + nla_total_size(xfrm_user_sec_ctx_size(xp->security))
  2428. + nla_total_size(sizeof(struct xfrm_mark))
  2429. + userpolicy_type_attrsize();
  2430. }
  2431. static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
  2432. int dir, const struct km_event *c)
  2433. {
  2434. struct xfrm_user_polexpire *upe;
  2435. int hard = c->data.hard;
  2436. struct nlmsghdr *nlh;
  2437. int err;
  2438. nlh = nlmsg_put(skb, c->portid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe), 0);
  2439. if (nlh == NULL)
  2440. return -EMSGSIZE;
  2441. upe = nlmsg_data(nlh);
  2442. copy_to_user_policy(xp, &upe->pol, dir);
  2443. err = copy_to_user_tmpl(xp, skb);
  2444. if (!err)
  2445. err = copy_to_user_sec_ctx(xp, skb);
  2446. if (!err)
  2447. err = copy_to_user_policy_type(xp->type, skb);
  2448. if (!err)
  2449. err = xfrm_mark_put(skb, &xp->mark);
  2450. if (err) {
  2451. nlmsg_cancel(skb, nlh);
  2452. return err;
  2453. }
  2454. upe->hard = !!hard;
  2455. nlmsg_end(skb, nlh);
  2456. return 0;
  2457. }
  2458. static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
  2459. {
  2460. struct net *net = xp_net(xp);
  2461. struct sk_buff *skb;
  2462. skb = nlmsg_new(xfrm_polexpire_msgsize(xp), GFP_ATOMIC);
  2463. if (skb == NULL)
  2464. return -ENOMEM;
  2465. if (build_polexpire(skb, xp, dir, c) < 0)
  2466. BUG();
  2467. return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_EXPIRE);
  2468. }
  2469. static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, const struct km_event *c)
  2470. {
  2471. int len = nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
  2472. struct net *net = xp_net(xp);
  2473. struct xfrm_userpolicy_info *p;
  2474. struct xfrm_userpolicy_id *id;
  2475. struct nlmsghdr *nlh;
  2476. struct sk_buff *skb;
  2477. int headlen, err;
  2478. headlen = sizeof(*p);
  2479. if (c->event == XFRM_MSG_DELPOLICY) {
  2480. len += nla_total_size(headlen);
  2481. headlen = sizeof(*id);
  2482. }
  2483. len += userpolicy_type_attrsize();
  2484. len += nla_total_size(sizeof(struct xfrm_mark));
  2485. len += NLMSG_ALIGN(headlen);
  2486. skb = nlmsg_new(len, GFP_ATOMIC);
  2487. if (skb == NULL)
  2488. return -ENOMEM;
  2489. nlh = nlmsg_put(skb, c->portid, c->seq, c->event, headlen, 0);
  2490. err = -EMSGSIZE;
  2491. if (nlh == NULL)
  2492. goto out_free_skb;
  2493. p = nlmsg_data(nlh);
  2494. if (c->event == XFRM_MSG_DELPOLICY) {
  2495. struct nlattr *attr;
  2496. id = nlmsg_data(nlh);
  2497. memset(id, 0, sizeof(*id));
  2498. id->dir = dir;
  2499. if (c->data.byid)
  2500. id->index = xp->index;
  2501. else
  2502. memcpy(&id->sel, &xp->selector, sizeof(id->sel));
  2503. attr = nla_reserve(skb, XFRMA_POLICY, sizeof(*p));
  2504. err = -EMSGSIZE;
  2505. if (attr == NULL)
  2506. goto out_free_skb;
  2507. p = nla_data(attr);
  2508. }
  2509. copy_to_user_policy(xp, p, dir);
  2510. err = copy_to_user_tmpl(xp, skb);
  2511. if (!err)
  2512. err = copy_to_user_policy_type(xp->type, skb);
  2513. if (!err)
  2514. err = xfrm_mark_put(skb, &xp->mark);
  2515. if (err)
  2516. goto out_free_skb;
  2517. nlmsg_end(skb, nlh);
  2518. return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_POLICY);
  2519. out_free_skb:
  2520. kfree_skb(skb);
  2521. return err;
  2522. }
  2523. static int xfrm_notify_policy_flush(const struct km_event *c)
  2524. {
  2525. struct net *net = c->net;
  2526. struct nlmsghdr *nlh;
  2527. struct sk_buff *skb;
  2528. int err;
  2529. skb = nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC);
  2530. if (skb == NULL)
  2531. return -ENOMEM;
  2532. nlh = nlmsg_put(skb, c->portid, c->seq, XFRM_MSG_FLUSHPOLICY, 0, 0);
  2533. err = -EMSGSIZE;
  2534. if (nlh == NULL)
  2535. goto out_free_skb;
  2536. err = copy_to_user_policy_type(c->data.type, skb);
  2537. if (err)
  2538. goto out_free_skb;
  2539. nlmsg_end(skb, nlh);
  2540. return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_POLICY);
  2541. out_free_skb:
  2542. kfree_skb(skb);
  2543. return err;
  2544. }
  2545. static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, const struct km_event *c)
  2546. {
  2547. switch (c->event) {
  2548. case XFRM_MSG_NEWPOLICY:
  2549. case XFRM_MSG_UPDPOLICY:
  2550. case XFRM_MSG_DELPOLICY:
  2551. return xfrm_notify_policy(xp, dir, c);
  2552. case XFRM_MSG_FLUSHPOLICY:
  2553. return xfrm_notify_policy_flush(c);
  2554. case XFRM_MSG_POLEXPIRE:
  2555. return xfrm_exp_policy_notify(xp, dir, c);
  2556. default:
  2557. printk(KERN_NOTICE "xfrm_user: Unknown Policy event %d\n",
  2558. c->event);
  2559. }
  2560. return 0;
  2561. }
  2562. static inline size_t xfrm_report_msgsize(void)
  2563. {
  2564. return NLMSG_ALIGN(sizeof(struct xfrm_user_report));
  2565. }
  2566. static int build_report(struct sk_buff *skb, u8 proto,
  2567. struct xfrm_selector *sel, xfrm_address_t *addr)
  2568. {
  2569. struct xfrm_user_report *ur;
  2570. struct nlmsghdr *nlh;
  2571. nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur), 0);
  2572. if (nlh == NULL)
  2573. return -EMSGSIZE;
  2574. ur = nlmsg_data(nlh);
  2575. ur->proto = proto;
  2576. memcpy(&ur->sel, sel, sizeof(ur->sel));
  2577. if (addr) {
  2578. int err = nla_put(skb, XFRMA_COADDR, sizeof(*addr), addr);
  2579. if (err) {
  2580. nlmsg_cancel(skb, nlh);
  2581. return err;
  2582. }
  2583. }
  2584. nlmsg_end(skb, nlh);
  2585. return 0;
  2586. }
  2587. static int xfrm_send_report(struct net *net, u8 proto,
  2588. struct xfrm_selector *sel, xfrm_address_t *addr)
  2589. {
  2590. struct sk_buff *skb;
  2591. skb = nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC);
  2592. if (skb == NULL)
  2593. return -ENOMEM;
  2594. if (build_report(skb, proto, sel, addr) < 0)
  2595. BUG();
  2596. return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_REPORT);
  2597. }
  2598. static inline size_t xfrm_mapping_msgsize(void)
  2599. {
  2600. return NLMSG_ALIGN(sizeof(struct xfrm_user_mapping));
  2601. }
  2602. static int build_mapping(struct sk_buff *skb, struct xfrm_state *x,
  2603. xfrm_address_t *new_saddr, __be16 new_sport)
  2604. {
  2605. struct xfrm_user_mapping *um;
  2606. struct nlmsghdr *nlh;
  2607. nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MAPPING, sizeof(*um), 0);
  2608. if (nlh == NULL)
  2609. return -EMSGSIZE;
  2610. um = nlmsg_data(nlh);
  2611. memcpy(&um->id.daddr, &x->id.daddr, sizeof(um->id.daddr));
  2612. um->id.spi = x->id.spi;
  2613. um->id.family = x->props.family;
  2614. um->id.proto = x->id.proto;
  2615. memcpy(&um->new_saddr, new_saddr, sizeof(um->new_saddr));
  2616. memcpy(&um->old_saddr, &x->props.saddr, sizeof(um->old_saddr));
  2617. um->new_sport = new_sport;
  2618. um->old_sport = x->encap->encap_sport;
  2619. um->reqid = x->props.reqid;
  2620. nlmsg_end(skb, nlh);
  2621. return 0;
  2622. }
  2623. static int xfrm_send_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr,
  2624. __be16 sport)
  2625. {
  2626. struct net *net = xs_net(x);
  2627. struct sk_buff *skb;
  2628. if (x->id.proto != IPPROTO_ESP)
  2629. return -EINVAL;
  2630. if (!x->encap)
  2631. return -EINVAL;
  2632. skb = nlmsg_new(xfrm_mapping_msgsize(), GFP_ATOMIC);
  2633. if (skb == NULL)
  2634. return -ENOMEM;
  2635. if (build_mapping(skb, x, ipaddr, sport) < 0)
  2636. BUG();
  2637. return xfrm_nlmsg_multicast(net, skb, 0, XFRMNLGRP_MAPPING);
  2638. }
  2639. static bool xfrm_is_alive(const struct km_event *c)
  2640. {
  2641. return (bool)xfrm_acquire_is_on(c->net);
  2642. }
  2643. static struct xfrm_mgr netlink_mgr = {
  2644. .notify = xfrm_send_state_notify,
  2645. .acquire = xfrm_send_acquire,
  2646. .compile_policy = xfrm_compile_policy,
  2647. .notify_policy = xfrm_send_policy_notify,
  2648. .report = xfrm_send_report,
  2649. .migrate = xfrm_send_migrate,
  2650. .new_mapping = xfrm_send_mapping,
  2651. .is_alive = xfrm_is_alive,
  2652. };
  2653. static int __net_init xfrm_user_net_init(struct net *net)
  2654. {
  2655. struct sock *nlsk;
  2656. struct netlink_kernel_cfg cfg = {
  2657. .groups = XFRMNLGRP_MAX,
  2658. .input = xfrm_netlink_rcv,
  2659. };
  2660. nlsk = netlink_kernel_create(net, NETLINK_XFRM, &cfg);
  2661. if (nlsk == NULL)
  2662. return -ENOMEM;
  2663. net->xfrm.nlsk_stash = nlsk; /* Don't set to NULL */
  2664. rcu_assign_pointer(net->xfrm.nlsk, nlsk);
  2665. return 0;
  2666. }
  2667. static void __net_exit xfrm_user_net_exit(struct list_head *net_exit_list)
  2668. {
  2669. struct net *net;
  2670. list_for_each_entry(net, net_exit_list, exit_list)
  2671. RCU_INIT_POINTER(net->xfrm.nlsk, NULL);
  2672. synchronize_net();
  2673. list_for_each_entry(net, net_exit_list, exit_list)
  2674. netlink_kernel_release(net->xfrm.nlsk_stash);
  2675. }
  2676. static struct pernet_operations xfrm_user_net_ops = {
  2677. .init = xfrm_user_net_init,
  2678. .exit_batch = xfrm_user_net_exit,
  2679. };
  2680. static int __init xfrm_user_init(void)
  2681. {
  2682. int rv;
  2683. printk(KERN_INFO "Initializing XFRM netlink socket\n");
  2684. rv = register_pernet_subsys(&xfrm_user_net_ops);
  2685. if (rv < 0)
  2686. return rv;
  2687. rv = xfrm_register_km(&netlink_mgr);
  2688. if (rv < 0)
  2689. unregister_pernet_subsys(&xfrm_user_net_ops);
  2690. return rv;
  2691. }
  2692. static void __exit xfrm_user_exit(void)
  2693. {
  2694. xfrm_unregister_km(&netlink_mgr);
  2695. unregister_pernet_subsys(&xfrm_user_net_ops);
  2696. }
  2697. module_init(xfrm_user_init);
  2698. module_exit(xfrm_user_exit);
  2699. MODULE_LICENSE("GPL");
  2700. MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);