xfrm_user.c 73 KB

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