xfrm_user.c 75 KB

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