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