xfrm_user.c 73 KB

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