xfrm_user.c 75 KB

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