xfrm_user.c 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526
  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 <asm/uaccess.h>
  29. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  30. #include <linux/in6.h>
  31. #endif
  32. static inline int aead_len(struct xfrm_algo_aead *alg)
  33. {
  34. return sizeof(*alg) + ((alg->alg_key_len + 7) / 8);
  35. }
  36. static int verify_one_alg(struct nlattr **attrs, enum xfrm_attr_type_t type)
  37. {
  38. struct nlattr *rt = attrs[type];
  39. struct xfrm_algo *algp;
  40. if (!rt)
  41. return 0;
  42. algp = nla_data(rt);
  43. if (nla_len(rt) < xfrm_alg_len(algp))
  44. return -EINVAL;
  45. switch (type) {
  46. case XFRMA_ALG_AUTH:
  47. if (!algp->alg_key_len &&
  48. strcmp(algp->alg_name, "digest_null") != 0)
  49. return -EINVAL;
  50. break;
  51. case XFRMA_ALG_CRYPT:
  52. if (!algp->alg_key_len &&
  53. strcmp(algp->alg_name, "cipher_null") != 0)
  54. return -EINVAL;
  55. break;
  56. case XFRMA_ALG_COMP:
  57. /* Zero length keys are legal. */
  58. break;
  59. default:
  60. return -EINVAL;
  61. }
  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 int verify_newsa_info(struct xfrm_usersa_info *p,
  96. struct nlattr **attrs)
  97. {
  98. int err;
  99. err = -EINVAL;
  100. switch (p->family) {
  101. case AF_INET:
  102. break;
  103. case AF_INET6:
  104. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  105. break;
  106. #else
  107. err = -EAFNOSUPPORT;
  108. goto out;
  109. #endif
  110. default:
  111. goto out;
  112. }
  113. err = -EINVAL;
  114. switch (p->id.proto) {
  115. case IPPROTO_AH:
  116. if (!attrs[XFRMA_ALG_AUTH] ||
  117. attrs[XFRMA_ALG_AEAD] ||
  118. attrs[XFRMA_ALG_CRYPT] ||
  119. attrs[XFRMA_ALG_COMP])
  120. goto out;
  121. break;
  122. case IPPROTO_ESP:
  123. if (attrs[XFRMA_ALG_COMP])
  124. goto out;
  125. if (!attrs[XFRMA_ALG_AUTH] &&
  126. !attrs[XFRMA_ALG_CRYPT] &&
  127. !attrs[XFRMA_ALG_AEAD])
  128. goto out;
  129. if ((attrs[XFRMA_ALG_AUTH] ||
  130. attrs[XFRMA_ALG_CRYPT]) &&
  131. attrs[XFRMA_ALG_AEAD])
  132. goto out;
  133. break;
  134. case IPPROTO_COMP:
  135. if (!attrs[XFRMA_ALG_COMP] ||
  136. attrs[XFRMA_ALG_AEAD] ||
  137. attrs[XFRMA_ALG_AUTH] ||
  138. attrs[XFRMA_ALG_CRYPT])
  139. goto out;
  140. break;
  141. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  142. case IPPROTO_DSTOPTS:
  143. case IPPROTO_ROUTING:
  144. if (attrs[XFRMA_ALG_COMP] ||
  145. attrs[XFRMA_ALG_AUTH] ||
  146. attrs[XFRMA_ALG_AEAD] ||
  147. attrs[XFRMA_ALG_CRYPT] ||
  148. attrs[XFRMA_ENCAP] ||
  149. attrs[XFRMA_SEC_CTX] ||
  150. !attrs[XFRMA_COADDR])
  151. goto out;
  152. break;
  153. #endif
  154. default:
  155. goto out;
  156. }
  157. if ((err = verify_aead(attrs)))
  158. goto out;
  159. if ((err = verify_one_alg(attrs, XFRMA_ALG_AUTH)))
  160. goto out;
  161. if ((err = verify_one_alg(attrs, XFRMA_ALG_CRYPT)))
  162. goto out;
  163. if ((err = verify_one_alg(attrs, XFRMA_ALG_COMP)))
  164. goto out;
  165. if ((err = verify_sec_ctx_len(attrs)))
  166. goto out;
  167. err = -EINVAL;
  168. switch (p->mode) {
  169. case XFRM_MODE_TRANSPORT:
  170. case XFRM_MODE_TUNNEL:
  171. case XFRM_MODE_ROUTEOPTIMIZATION:
  172. case XFRM_MODE_BEET:
  173. break;
  174. default:
  175. goto out;
  176. }
  177. err = 0;
  178. out:
  179. return err;
  180. }
  181. static int attach_one_algo(struct xfrm_algo **algpp, u8 *props,
  182. struct xfrm_algo_desc *(*get_byname)(char *, int),
  183. struct nlattr *rta)
  184. {
  185. struct xfrm_algo *p, *ualg;
  186. struct xfrm_algo_desc *algo;
  187. if (!rta)
  188. return 0;
  189. ualg = nla_data(rta);
  190. algo = get_byname(ualg->alg_name, 1);
  191. if (!algo)
  192. return -ENOSYS;
  193. *props = algo->desc.sadb_alg_id;
  194. p = kmemdup(ualg, xfrm_alg_len(ualg), GFP_KERNEL);
  195. if (!p)
  196. return -ENOMEM;
  197. strcpy(p->alg_name, algo->name);
  198. *algpp = p;
  199. return 0;
  200. }
  201. static int attach_aead(struct xfrm_algo_aead **algpp, u8 *props,
  202. struct nlattr *rta)
  203. {
  204. struct xfrm_algo_aead *p, *ualg;
  205. struct xfrm_algo_desc *algo;
  206. if (!rta)
  207. return 0;
  208. ualg = nla_data(rta);
  209. algo = xfrm_aead_get_byname(ualg->alg_name, ualg->alg_icv_len, 1);
  210. if (!algo)
  211. return -ENOSYS;
  212. *props = algo->desc.sadb_alg_id;
  213. p = kmemdup(ualg, aead_len(ualg), GFP_KERNEL);
  214. if (!p)
  215. return -ENOMEM;
  216. strcpy(p->alg_name, algo->name);
  217. *algpp = p;
  218. return 0;
  219. }
  220. static inline int xfrm_user_sec_ctx_size(struct xfrm_sec_ctx *xfrm_ctx)
  221. {
  222. int len = 0;
  223. if (xfrm_ctx) {
  224. len += sizeof(struct xfrm_user_sec_ctx);
  225. len += xfrm_ctx->ctx_len;
  226. }
  227. return len;
  228. }
  229. static void copy_from_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
  230. {
  231. memcpy(&x->id, &p->id, sizeof(x->id));
  232. memcpy(&x->sel, &p->sel, sizeof(x->sel));
  233. memcpy(&x->lft, &p->lft, sizeof(x->lft));
  234. x->props.mode = p->mode;
  235. x->props.replay_window = p->replay_window;
  236. x->props.reqid = p->reqid;
  237. x->props.family = p->family;
  238. memcpy(&x->props.saddr, &p->saddr, sizeof(x->props.saddr));
  239. x->props.flags = p->flags;
  240. if (!x->sel.family)
  241. x->sel.family = p->family;
  242. }
  243. /*
  244. * someday when pfkey also has support, we could have the code
  245. * somehow made shareable and move it to xfrm_state.c - JHS
  246. *
  247. */
  248. static void xfrm_update_ae_params(struct xfrm_state *x, struct nlattr **attrs)
  249. {
  250. struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
  251. struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
  252. struct nlattr *et = attrs[XFRMA_ETIMER_THRESH];
  253. struct nlattr *rt = attrs[XFRMA_REPLAY_THRESH];
  254. if (rp) {
  255. struct xfrm_replay_state *replay;
  256. replay = nla_data(rp);
  257. memcpy(&x->replay, replay, sizeof(*replay));
  258. memcpy(&x->preplay, replay, sizeof(*replay));
  259. }
  260. if (lt) {
  261. struct xfrm_lifetime_cur *ltime;
  262. ltime = nla_data(lt);
  263. x->curlft.bytes = ltime->bytes;
  264. x->curlft.packets = ltime->packets;
  265. x->curlft.add_time = ltime->add_time;
  266. x->curlft.use_time = ltime->use_time;
  267. }
  268. if (et)
  269. x->replay_maxage = nla_get_u32(et);
  270. if (rt)
  271. x->replay_maxdiff = nla_get_u32(rt);
  272. }
  273. static struct xfrm_state *xfrm_state_construct(struct xfrm_usersa_info *p,
  274. struct nlattr **attrs,
  275. int *errp)
  276. {
  277. struct xfrm_state *x = xfrm_state_alloc();
  278. int err = -ENOMEM;
  279. if (!x)
  280. goto error_no_put;
  281. copy_from_user_state(x, p);
  282. if ((err = attach_aead(&x->aead, &x->props.ealgo,
  283. attrs[XFRMA_ALG_AEAD])))
  284. goto error;
  285. if ((err = attach_one_algo(&x->aalg, &x->props.aalgo,
  286. xfrm_aalg_get_byname,
  287. attrs[XFRMA_ALG_AUTH])))
  288. goto error;
  289. if ((err = attach_one_algo(&x->ealg, &x->props.ealgo,
  290. xfrm_ealg_get_byname,
  291. attrs[XFRMA_ALG_CRYPT])))
  292. goto error;
  293. if ((err = attach_one_algo(&x->calg, &x->props.calgo,
  294. xfrm_calg_get_byname,
  295. attrs[XFRMA_ALG_COMP])))
  296. goto error;
  297. if (attrs[XFRMA_ENCAP]) {
  298. x->encap = kmemdup(nla_data(attrs[XFRMA_ENCAP]),
  299. sizeof(*x->encap), GFP_KERNEL);
  300. if (x->encap == NULL)
  301. goto error;
  302. }
  303. if (attrs[XFRMA_COADDR]) {
  304. x->coaddr = kmemdup(nla_data(attrs[XFRMA_COADDR]),
  305. sizeof(*x->coaddr), GFP_KERNEL);
  306. if (x->coaddr == NULL)
  307. goto error;
  308. }
  309. err = xfrm_init_state(x);
  310. if (err)
  311. goto error;
  312. if (attrs[XFRMA_SEC_CTX] &&
  313. security_xfrm_state_alloc(x, nla_data(attrs[XFRMA_SEC_CTX])))
  314. goto error;
  315. x->km.seq = p->seq;
  316. x->replay_maxdiff = sysctl_xfrm_aevent_rseqth;
  317. /* sysctl_xfrm_aevent_etime is in 100ms units */
  318. x->replay_maxage = (sysctl_xfrm_aevent_etime*HZ)/XFRM_AE_ETH_M;
  319. x->preplay.bitmap = 0;
  320. x->preplay.seq = x->replay.seq+x->replay_maxdiff;
  321. x->preplay.oseq = x->replay.oseq +x->replay_maxdiff;
  322. /* override default values from above */
  323. xfrm_update_ae_params(x, attrs);
  324. return x;
  325. error:
  326. x->km.state = XFRM_STATE_DEAD;
  327. xfrm_state_put(x);
  328. error_no_put:
  329. *errp = err;
  330. return NULL;
  331. }
  332. static int xfrm_add_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
  333. struct nlattr **attrs)
  334. {
  335. struct xfrm_usersa_info *p = nlmsg_data(nlh);
  336. struct xfrm_state *x;
  337. int err;
  338. struct km_event c;
  339. uid_t loginuid = NETLINK_CB(skb).loginuid;
  340. u32 sessionid = NETLINK_CB(skb).sessionid;
  341. u32 sid = NETLINK_CB(skb).sid;
  342. err = verify_newsa_info(p, attrs);
  343. if (err)
  344. return err;
  345. x = xfrm_state_construct(p, attrs, &err);
  346. if (!x)
  347. return err;
  348. xfrm_state_hold(x);
  349. if (nlh->nlmsg_type == XFRM_MSG_NEWSA)
  350. err = xfrm_state_add(x);
  351. else
  352. err = xfrm_state_update(x);
  353. xfrm_audit_state_add(x, err ? 0 : 1, loginuid, sessionid, sid);
  354. if (err < 0) {
  355. x->km.state = XFRM_STATE_DEAD;
  356. __xfrm_state_put(x);
  357. goto out;
  358. }
  359. c.seq = nlh->nlmsg_seq;
  360. c.pid = nlh->nlmsg_pid;
  361. c.event = nlh->nlmsg_type;
  362. km_state_notify(x, &c);
  363. out:
  364. xfrm_state_put(x);
  365. return err;
  366. }
  367. static struct xfrm_state *xfrm_user_state_lookup(struct xfrm_usersa_id *p,
  368. struct nlattr **attrs,
  369. int *errp)
  370. {
  371. struct xfrm_state *x = NULL;
  372. int err;
  373. if (xfrm_id_proto_match(p->proto, IPSEC_PROTO_ANY)) {
  374. err = -ESRCH;
  375. x = xfrm_state_lookup(&p->daddr, p->spi, p->proto, p->family);
  376. } else {
  377. xfrm_address_t *saddr = NULL;
  378. verify_one_addr(attrs, XFRMA_SRCADDR, &saddr);
  379. if (!saddr) {
  380. err = -EINVAL;
  381. goto out;
  382. }
  383. err = -ESRCH;
  384. x = xfrm_state_lookup_byaddr(&p->daddr, saddr, p->proto,
  385. p->family);
  386. }
  387. out:
  388. if (!x && errp)
  389. *errp = err;
  390. return x;
  391. }
  392. static int xfrm_del_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
  393. struct nlattr **attrs)
  394. {
  395. struct xfrm_state *x;
  396. int err = -ESRCH;
  397. struct km_event c;
  398. struct xfrm_usersa_id *p = nlmsg_data(nlh);
  399. uid_t loginuid = NETLINK_CB(skb).loginuid;
  400. u32 sessionid = NETLINK_CB(skb).sessionid;
  401. u32 sid = NETLINK_CB(skb).sid;
  402. x = xfrm_user_state_lookup(p, attrs, &err);
  403. if (x == NULL)
  404. return err;
  405. if ((err = security_xfrm_state_delete(x)) != 0)
  406. goto out;
  407. if (xfrm_state_kern(x)) {
  408. err = -EPERM;
  409. goto out;
  410. }
  411. err = xfrm_state_delete(x);
  412. if (err < 0)
  413. goto out;
  414. c.seq = nlh->nlmsg_seq;
  415. c.pid = nlh->nlmsg_pid;
  416. c.event = nlh->nlmsg_type;
  417. km_state_notify(x, &c);
  418. out:
  419. xfrm_audit_state_delete(x, err ? 0 : 1, loginuid, sessionid, sid);
  420. xfrm_state_put(x);
  421. return err;
  422. }
  423. static void copy_to_user_state(struct xfrm_state *x, struct xfrm_usersa_info *p)
  424. {
  425. memcpy(&p->id, &x->id, sizeof(p->id));
  426. memcpy(&p->sel, &x->sel, sizeof(p->sel));
  427. memcpy(&p->lft, &x->lft, sizeof(p->lft));
  428. memcpy(&p->curlft, &x->curlft, sizeof(p->curlft));
  429. memcpy(&p->stats, &x->stats, sizeof(p->stats));
  430. memcpy(&p->saddr, &x->props.saddr, sizeof(p->saddr));
  431. p->mode = x->props.mode;
  432. p->replay_window = x->props.replay_window;
  433. p->reqid = x->props.reqid;
  434. p->family = x->props.family;
  435. p->flags = x->props.flags;
  436. p->seq = x->km.seq;
  437. }
  438. struct xfrm_dump_info {
  439. struct sk_buff *in_skb;
  440. struct sk_buff *out_skb;
  441. u32 nlmsg_seq;
  442. u16 nlmsg_flags;
  443. };
  444. static int copy_sec_ctx(struct xfrm_sec_ctx *s, struct sk_buff *skb)
  445. {
  446. struct xfrm_user_sec_ctx *uctx;
  447. struct nlattr *attr;
  448. int ctx_size = sizeof(*uctx) + s->ctx_len;
  449. attr = nla_reserve(skb, XFRMA_SEC_CTX, ctx_size);
  450. if (attr == NULL)
  451. return -EMSGSIZE;
  452. uctx = nla_data(attr);
  453. uctx->exttype = XFRMA_SEC_CTX;
  454. uctx->len = ctx_size;
  455. uctx->ctx_doi = s->ctx_doi;
  456. uctx->ctx_alg = s->ctx_alg;
  457. uctx->ctx_len = s->ctx_len;
  458. memcpy(uctx + 1, s->ctx_str, s->ctx_len);
  459. return 0;
  460. }
  461. /* Don't change this without updating xfrm_sa_len! */
  462. static int copy_to_user_state_extra(struct xfrm_state *x,
  463. struct xfrm_usersa_info *p,
  464. struct sk_buff *skb)
  465. {
  466. copy_to_user_state(x, p);
  467. if (x->coaddr)
  468. NLA_PUT(skb, XFRMA_COADDR, sizeof(*x->coaddr), x->coaddr);
  469. if (x->lastused)
  470. NLA_PUT_U64(skb, XFRMA_LASTUSED, x->lastused);
  471. if (x->aead)
  472. NLA_PUT(skb, XFRMA_ALG_AEAD, aead_len(x->aead), x->aead);
  473. if (x->aalg)
  474. NLA_PUT(skb, XFRMA_ALG_AUTH, xfrm_alg_len(x->aalg), x->aalg);
  475. if (x->ealg)
  476. NLA_PUT(skb, XFRMA_ALG_CRYPT, xfrm_alg_len(x->ealg), x->ealg);
  477. if (x->calg)
  478. NLA_PUT(skb, XFRMA_ALG_COMP, sizeof(*(x->calg)), x->calg);
  479. if (x->encap)
  480. NLA_PUT(skb, XFRMA_ENCAP, sizeof(*x->encap), x->encap);
  481. if (x->security && copy_sec_ctx(x->security, skb) < 0)
  482. goto nla_put_failure;
  483. return 0;
  484. nla_put_failure:
  485. return -EMSGSIZE;
  486. }
  487. static int dump_one_state(struct xfrm_state *x, int count, void *ptr)
  488. {
  489. struct xfrm_dump_info *sp = ptr;
  490. struct sk_buff *in_skb = sp->in_skb;
  491. struct sk_buff *skb = sp->out_skb;
  492. struct xfrm_usersa_info *p;
  493. struct nlmsghdr *nlh;
  494. int err;
  495. nlh = nlmsg_put(skb, NETLINK_CB(in_skb).pid, sp->nlmsg_seq,
  496. XFRM_MSG_NEWSA, sizeof(*p), sp->nlmsg_flags);
  497. if (nlh == NULL)
  498. return -EMSGSIZE;
  499. p = nlmsg_data(nlh);
  500. err = copy_to_user_state_extra(x, p, skb);
  501. if (err)
  502. goto nla_put_failure;
  503. nlmsg_end(skb, nlh);
  504. return 0;
  505. nla_put_failure:
  506. nlmsg_cancel(skb, nlh);
  507. return err;
  508. }
  509. static int xfrm_dump_sa_done(struct netlink_callback *cb)
  510. {
  511. struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
  512. xfrm_state_walk_done(walk);
  513. return 0;
  514. }
  515. static int xfrm_dump_sa(struct sk_buff *skb, struct netlink_callback *cb)
  516. {
  517. struct xfrm_state_walk *walk = (struct xfrm_state_walk *) &cb->args[1];
  518. struct xfrm_dump_info info;
  519. BUILD_BUG_ON(sizeof(struct xfrm_state_walk) >
  520. sizeof(cb->args) - sizeof(cb->args[0]));
  521. info.in_skb = cb->skb;
  522. info.out_skb = skb;
  523. info.nlmsg_seq = cb->nlh->nlmsg_seq;
  524. info.nlmsg_flags = NLM_F_MULTI;
  525. if (!cb->args[0]) {
  526. cb->args[0] = 1;
  527. xfrm_state_walk_init(walk, 0);
  528. }
  529. (void) xfrm_state_walk(walk, dump_one_state, &info);
  530. return skb->len;
  531. }
  532. static struct sk_buff *xfrm_state_netlink(struct sk_buff *in_skb,
  533. struct xfrm_state *x, u32 seq)
  534. {
  535. struct xfrm_dump_info info;
  536. struct sk_buff *skb;
  537. skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
  538. if (!skb)
  539. return ERR_PTR(-ENOMEM);
  540. info.in_skb = in_skb;
  541. info.out_skb = skb;
  542. info.nlmsg_seq = seq;
  543. info.nlmsg_flags = 0;
  544. if (dump_one_state(x, 0, &info)) {
  545. kfree_skb(skb);
  546. return NULL;
  547. }
  548. return skb;
  549. }
  550. static inline size_t xfrm_spdinfo_msgsize(void)
  551. {
  552. return NLMSG_ALIGN(4)
  553. + nla_total_size(sizeof(struct xfrmu_spdinfo))
  554. + nla_total_size(sizeof(struct xfrmu_spdhinfo));
  555. }
  556. static int build_spdinfo(struct sk_buff *skb, u32 pid, u32 seq, u32 flags)
  557. {
  558. struct xfrmk_spdinfo si;
  559. struct xfrmu_spdinfo spc;
  560. struct xfrmu_spdhinfo sph;
  561. struct nlmsghdr *nlh;
  562. u32 *f;
  563. nlh = nlmsg_put(skb, pid, seq, XFRM_MSG_NEWSPDINFO, sizeof(u32), 0);
  564. if (nlh == NULL) /* shouldnt really happen ... */
  565. return -EMSGSIZE;
  566. f = nlmsg_data(nlh);
  567. *f = flags;
  568. xfrm_spd_getinfo(&si);
  569. spc.incnt = si.incnt;
  570. spc.outcnt = si.outcnt;
  571. spc.fwdcnt = si.fwdcnt;
  572. spc.inscnt = si.inscnt;
  573. spc.outscnt = si.outscnt;
  574. spc.fwdscnt = si.fwdscnt;
  575. sph.spdhcnt = si.spdhcnt;
  576. sph.spdhmcnt = si.spdhmcnt;
  577. NLA_PUT(skb, XFRMA_SPD_INFO, sizeof(spc), &spc);
  578. NLA_PUT(skb, XFRMA_SPD_HINFO, sizeof(sph), &sph);
  579. return nlmsg_end(skb, nlh);
  580. nla_put_failure:
  581. nlmsg_cancel(skb, nlh);
  582. return -EMSGSIZE;
  583. }
  584. static int xfrm_get_spdinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
  585. struct nlattr **attrs)
  586. {
  587. struct sk_buff *r_skb;
  588. u32 *flags = nlmsg_data(nlh);
  589. u32 spid = NETLINK_CB(skb).pid;
  590. u32 seq = nlh->nlmsg_seq;
  591. r_skb = nlmsg_new(xfrm_spdinfo_msgsize(), GFP_ATOMIC);
  592. if (r_skb == NULL)
  593. return -ENOMEM;
  594. if (build_spdinfo(r_skb, spid, seq, *flags) < 0)
  595. BUG();
  596. return nlmsg_unicast(xfrm_nl, r_skb, spid);
  597. }
  598. static inline size_t xfrm_sadinfo_msgsize(void)
  599. {
  600. return NLMSG_ALIGN(4)
  601. + nla_total_size(sizeof(struct xfrmu_sadhinfo))
  602. + nla_total_size(4); /* XFRMA_SAD_CNT */
  603. }
  604. static int build_sadinfo(struct sk_buff *skb, u32 pid, u32 seq, u32 flags)
  605. {
  606. struct xfrmk_sadinfo si;
  607. struct xfrmu_sadhinfo sh;
  608. struct nlmsghdr *nlh;
  609. u32 *f;
  610. nlh = nlmsg_put(skb, pid, seq, XFRM_MSG_NEWSADINFO, sizeof(u32), 0);
  611. if (nlh == NULL) /* shouldnt really happen ... */
  612. return -EMSGSIZE;
  613. f = nlmsg_data(nlh);
  614. *f = flags;
  615. xfrm_sad_getinfo(&si);
  616. sh.sadhmcnt = si.sadhmcnt;
  617. sh.sadhcnt = si.sadhcnt;
  618. NLA_PUT_U32(skb, XFRMA_SAD_CNT, si.sadcnt);
  619. NLA_PUT(skb, XFRMA_SAD_HINFO, sizeof(sh), &sh);
  620. return nlmsg_end(skb, nlh);
  621. nla_put_failure:
  622. nlmsg_cancel(skb, nlh);
  623. return -EMSGSIZE;
  624. }
  625. static int xfrm_get_sadinfo(struct sk_buff *skb, struct nlmsghdr *nlh,
  626. struct nlattr **attrs)
  627. {
  628. struct sk_buff *r_skb;
  629. u32 *flags = nlmsg_data(nlh);
  630. u32 spid = NETLINK_CB(skb).pid;
  631. u32 seq = nlh->nlmsg_seq;
  632. r_skb = nlmsg_new(xfrm_sadinfo_msgsize(), GFP_ATOMIC);
  633. if (r_skb == NULL)
  634. return -ENOMEM;
  635. if (build_sadinfo(r_skb, spid, seq, *flags) < 0)
  636. BUG();
  637. return nlmsg_unicast(xfrm_nl, r_skb, spid);
  638. }
  639. static int xfrm_get_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
  640. struct nlattr **attrs)
  641. {
  642. struct xfrm_usersa_id *p = nlmsg_data(nlh);
  643. struct xfrm_state *x;
  644. struct sk_buff *resp_skb;
  645. int err = -ESRCH;
  646. x = xfrm_user_state_lookup(p, attrs, &err);
  647. if (x == NULL)
  648. goto out_noput;
  649. resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
  650. if (IS_ERR(resp_skb)) {
  651. err = PTR_ERR(resp_skb);
  652. } else {
  653. err = nlmsg_unicast(xfrm_nl, resp_skb, NETLINK_CB(skb).pid);
  654. }
  655. xfrm_state_put(x);
  656. out_noput:
  657. return err;
  658. }
  659. static int verify_userspi_info(struct xfrm_userspi_info *p)
  660. {
  661. switch (p->info.id.proto) {
  662. case IPPROTO_AH:
  663. case IPPROTO_ESP:
  664. break;
  665. case IPPROTO_COMP:
  666. /* IPCOMP spi is 16-bits. */
  667. if (p->max >= 0x10000)
  668. return -EINVAL;
  669. break;
  670. default:
  671. return -EINVAL;
  672. }
  673. if (p->min > p->max)
  674. return -EINVAL;
  675. return 0;
  676. }
  677. static int xfrm_alloc_userspi(struct sk_buff *skb, struct nlmsghdr *nlh,
  678. struct nlattr **attrs)
  679. {
  680. struct xfrm_state *x;
  681. struct xfrm_userspi_info *p;
  682. struct sk_buff *resp_skb;
  683. xfrm_address_t *daddr;
  684. int family;
  685. int err;
  686. p = nlmsg_data(nlh);
  687. err = verify_userspi_info(p);
  688. if (err)
  689. goto out_noput;
  690. family = p->info.family;
  691. daddr = &p->info.id.daddr;
  692. x = NULL;
  693. if (p->info.seq) {
  694. x = xfrm_find_acq_byseq(p->info.seq);
  695. if (x && xfrm_addr_cmp(&x->id.daddr, daddr, family)) {
  696. xfrm_state_put(x);
  697. x = NULL;
  698. }
  699. }
  700. if (!x)
  701. x = xfrm_find_acq(p->info.mode, p->info.reqid,
  702. p->info.id.proto, daddr,
  703. &p->info.saddr, 1,
  704. family);
  705. err = -ENOENT;
  706. if (x == NULL)
  707. goto out_noput;
  708. err = xfrm_alloc_spi(x, p->min, p->max);
  709. if (err)
  710. goto out;
  711. resp_skb = xfrm_state_netlink(skb, x, nlh->nlmsg_seq);
  712. if (IS_ERR(resp_skb)) {
  713. err = PTR_ERR(resp_skb);
  714. goto out;
  715. }
  716. err = nlmsg_unicast(xfrm_nl, resp_skb, NETLINK_CB(skb).pid);
  717. out:
  718. xfrm_state_put(x);
  719. out_noput:
  720. return err;
  721. }
  722. static int verify_policy_dir(u8 dir)
  723. {
  724. switch (dir) {
  725. case XFRM_POLICY_IN:
  726. case XFRM_POLICY_OUT:
  727. case XFRM_POLICY_FWD:
  728. break;
  729. default:
  730. return -EINVAL;
  731. }
  732. return 0;
  733. }
  734. static int verify_policy_type(u8 type)
  735. {
  736. switch (type) {
  737. case XFRM_POLICY_TYPE_MAIN:
  738. #ifdef CONFIG_XFRM_SUB_POLICY
  739. case XFRM_POLICY_TYPE_SUB:
  740. #endif
  741. break;
  742. default:
  743. return -EINVAL;
  744. }
  745. return 0;
  746. }
  747. static int verify_newpolicy_info(struct xfrm_userpolicy_info *p)
  748. {
  749. switch (p->share) {
  750. case XFRM_SHARE_ANY:
  751. case XFRM_SHARE_SESSION:
  752. case XFRM_SHARE_USER:
  753. case XFRM_SHARE_UNIQUE:
  754. break;
  755. default:
  756. return -EINVAL;
  757. }
  758. switch (p->action) {
  759. case XFRM_POLICY_ALLOW:
  760. case XFRM_POLICY_BLOCK:
  761. break;
  762. default:
  763. return -EINVAL;
  764. }
  765. switch (p->sel.family) {
  766. case AF_INET:
  767. break;
  768. case AF_INET6:
  769. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  770. break;
  771. #else
  772. return -EAFNOSUPPORT;
  773. #endif
  774. default:
  775. return -EINVAL;
  776. }
  777. return verify_policy_dir(p->dir);
  778. }
  779. static int copy_from_user_sec_ctx(struct xfrm_policy *pol, struct nlattr **attrs)
  780. {
  781. struct nlattr *rt = attrs[XFRMA_SEC_CTX];
  782. struct xfrm_user_sec_ctx *uctx;
  783. if (!rt)
  784. return 0;
  785. uctx = nla_data(rt);
  786. return security_xfrm_policy_alloc(&pol->security, uctx);
  787. }
  788. static void copy_templates(struct xfrm_policy *xp, struct xfrm_user_tmpl *ut,
  789. int nr)
  790. {
  791. int i;
  792. xp->xfrm_nr = nr;
  793. for (i = 0; i < nr; i++, ut++) {
  794. struct xfrm_tmpl *t = &xp->xfrm_vec[i];
  795. memcpy(&t->id, &ut->id, sizeof(struct xfrm_id));
  796. memcpy(&t->saddr, &ut->saddr,
  797. sizeof(xfrm_address_t));
  798. t->reqid = ut->reqid;
  799. t->mode = ut->mode;
  800. t->share = ut->share;
  801. t->optional = ut->optional;
  802. t->aalgos = ut->aalgos;
  803. t->ealgos = ut->ealgos;
  804. t->calgos = ut->calgos;
  805. /* If all masks are ~0, then we allow all algorithms. */
  806. t->allalgs = !~(t->aalgos & t->ealgos & t->calgos);
  807. t->encap_family = ut->family;
  808. }
  809. }
  810. static int validate_tmpl(int nr, struct xfrm_user_tmpl *ut, u16 family)
  811. {
  812. int i;
  813. if (nr > XFRM_MAX_DEPTH)
  814. return -EINVAL;
  815. for (i = 0; i < nr; i++) {
  816. /* We never validated the ut->family value, so many
  817. * applications simply leave it at zero. The check was
  818. * never made and ut->family was ignored because all
  819. * templates could be assumed to have the same family as
  820. * the policy itself. Now that we will have ipv4-in-ipv6
  821. * and ipv6-in-ipv4 tunnels, this is no longer true.
  822. */
  823. if (!ut[i].family)
  824. ut[i].family = family;
  825. switch (ut[i].family) {
  826. case AF_INET:
  827. break;
  828. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  829. case AF_INET6:
  830. break;
  831. #endif
  832. default:
  833. return -EINVAL;
  834. }
  835. }
  836. return 0;
  837. }
  838. static int copy_from_user_tmpl(struct xfrm_policy *pol, struct nlattr **attrs)
  839. {
  840. struct nlattr *rt = attrs[XFRMA_TMPL];
  841. if (!rt) {
  842. pol->xfrm_nr = 0;
  843. } else {
  844. struct xfrm_user_tmpl *utmpl = nla_data(rt);
  845. int nr = nla_len(rt) / sizeof(*utmpl);
  846. int err;
  847. err = validate_tmpl(nr, utmpl, pol->family);
  848. if (err)
  849. return err;
  850. copy_templates(pol, utmpl, nr);
  851. }
  852. return 0;
  853. }
  854. static int copy_from_user_policy_type(u8 *tp, struct nlattr **attrs)
  855. {
  856. struct nlattr *rt = attrs[XFRMA_POLICY_TYPE];
  857. struct xfrm_userpolicy_type *upt;
  858. u8 type = XFRM_POLICY_TYPE_MAIN;
  859. int err;
  860. if (rt) {
  861. upt = nla_data(rt);
  862. type = upt->type;
  863. }
  864. err = verify_policy_type(type);
  865. if (err)
  866. return err;
  867. *tp = type;
  868. return 0;
  869. }
  870. static void copy_from_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p)
  871. {
  872. xp->priority = p->priority;
  873. xp->index = p->index;
  874. memcpy(&xp->selector, &p->sel, sizeof(xp->selector));
  875. memcpy(&xp->lft, &p->lft, sizeof(xp->lft));
  876. xp->action = p->action;
  877. xp->flags = p->flags;
  878. xp->family = p->sel.family;
  879. /* XXX xp->share = p->share; */
  880. }
  881. static void copy_to_user_policy(struct xfrm_policy *xp, struct xfrm_userpolicy_info *p, int dir)
  882. {
  883. memcpy(&p->sel, &xp->selector, sizeof(p->sel));
  884. memcpy(&p->lft, &xp->lft, sizeof(p->lft));
  885. memcpy(&p->curlft, &xp->curlft, sizeof(p->curlft));
  886. p->priority = xp->priority;
  887. p->index = xp->index;
  888. p->sel.family = xp->family;
  889. p->dir = dir;
  890. p->action = xp->action;
  891. p->flags = xp->flags;
  892. p->share = XFRM_SHARE_ANY; /* XXX xp->share */
  893. }
  894. static struct xfrm_policy *xfrm_policy_construct(struct xfrm_userpolicy_info *p, struct nlattr **attrs, int *errp)
  895. {
  896. struct xfrm_policy *xp = xfrm_policy_alloc(GFP_KERNEL);
  897. int err;
  898. if (!xp) {
  899. *errp = -ENOMEM;
  900. return NULL;
  901. }
  902. copy_from_user_policy(xp, p);
  903. err = copy_from_user_policy_type(&xp->type, attrs);
  904. if (err)
  905. goto error;
  906. if (!(err = copy_from_user_tmpl(xp, attrs)))
  907. err = copy_from_user_sec_ctx(xp, attrs);
  908. if (err)
  909. goto error;
  910. return xp;
  911. error:
  912. *errp = err;
  913. xp->dead = 1;
  914. xfrm_policy_destroy(xp);
  915. return NULL;
  916. }
  917. static int xfrm_add_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
  918. struct nlattr **attrs)
  919. {
  920. struct xfrm_userpolicy_info *p = nlmsg_data(nlh);
  921. struct xfrm_policy *xp;
  922. struct km_event c;
  923. int err;
  924. int excl;
  925. uid_t loginuid = NETLINK_CB(skb).loginuid;
  926. u32 sessionid = NETLINK_CB(skb).sessionid;
  927. u32 sid = NETLINK_CB(skb).sid;
  928. err = verify_newpolicy_info(p);
  929. if (err)
  930. return err;
  931. err = verify_sec_ctx_len(attrs);
  932. if (err)
  933. return err;
  934. xp = xfrm_policy_construct(p, attrs, &err);
  935. if (!xp)
  936. return err;
  937. /* shouldnt excl be based on nlh flags??
  938. * Aha! this is anti-netlink really i.e more pfkey derived
  939. * in netlink excl is a flag and you wouldnt need
  940. * a type XFRM_MSG_UPDPOLICY - JHS */
  941. excl = nlh->nlmsg_type == XFRM_MSG_NEWPOLICY;
  942. err = xfrm_policy_insert(p->dir, xp, excl);
  943. xfrm_audit_policy_add(xp, err ? 0 : 1, loginuid, sessionid, sid);
  944. if (err) {
  945. security_xfrm_policy_free(xp->security);
  946. kfree(xp);
  947. return err;
  948. }
  949. c.event = nlh->nlmsg_type;
  950. c.seq = nlh->nlmsg_seq;
  951. c.pid = nlh->nlmsg_pid;
  952. km_policy_notify(xp, p->dir, &c);
  953. xfrm_pol_put(xp);
  954. return 0;
  955. }
  956. static int copy_to_user_tmpl(struct xfrm_policy *xp, struct sk_buff *skb)
  957. {
  958. struct xfrm_user_tmpl vec[XFRM_MAX_DEPTH];
  959. int i;
  960. if (xp->xfrm_nr == 0)
  961. return 0;
  962. for (i = 0; i < xp->xfrm_nr; i++) {
  963. struct xfrm_user_tmpl *up = &vec[i];
  964. struct xfrm_tmpl *kp = &xp->xfrm_vec[i];
  965. memcpy(&up->id, &kp->id, sizeof(up->id));
  966. up->family = kp->encap_family;
  967. memcpy(&up->saddr, &kp->saddr, sizeof(up->saddr));
  968. up->reqid = kp->reqid;
  969. up->mode = kp->mode;
  970. up->share = kp->share;
  971. up->optional = kp->optional;
  972. up->aalgos = kp->aalgos;
  973. up->ealgos = kp->ealgos;
  974. up->calgos = kp->calgos;
  975. }
  976. return nla_put(skb, XFRMA_TMPL,
  977. sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr, vec);
  978. }
  979. static inline int copy_to_user_state_sec_ctx(struct xfrm_state *x, struct sk_buff *skb)
  980. {
  981. if (x->security) {
  982. return copy_sec_ctx(x->security, skb);
  983. }
  984. return 0;
  985. }
  986. static inline int copy_to_user_sec_ctx(struct xfrm_policy *xp, struct sk_buff *skb)
  987. {
  988. if (xp->security) {
  989. return copy_sec_ctx(xp->security, skb);
  990. }
  991. return 0;
  992. }
  993. static inline size_t userpolicy_type_attrsize(void)
  994. {
  995. #ifdef CONFIG_XFRM_SUB_POLICY
  996. return nla_total_size(sizeof(struct xfrm_userpolicy_type));
  997. #else
  998. return 0;
  999. #endif
  1000. }
  1001. #ifdef CONFIG_XFRM_SUB_POLICY
  1002. static int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
  1003. {
  1004. struct xfrm_userpolicy_type upt = {
  1005. .type = type,
  1006. };
  1007. return nla_put(skb, XFRMA_POLICY_TYPE, sizeof(upt), &upt);
  1008. }
  1009. #else
  1010. static inline int copy_to_user_policy_type(u8 type, struct sk_buff *skb)
  1011. {
  1012. return 0;
  1013. }
  1014. #endif
  1015. static int dump_one_policy(struct xfrm_policy *xp, int dir, int count, void *ptr)
  1016. {
  1017. struct xfrm_dump_info *sp = ptr;
  1018. struct xfrm_userpolicy_info *p;
  1019. struct sk_buff *in_skb = sp->in_skb;
  1020. struct sk_buff *skb = sp->out_skb;
  1021. struct nlmsghdr *nlh;
  1022. nlh = nlmsg_put(skb, NETLINK_CB(in_skb).pid, sp->nlmsg_seq,
  1023. XFRM_MSG_NEWPOLICY, sizeof(*p), sp->nlmsg_flags);
  1024. if (nlh == NULL)
  1025. return -EMSGSIZE;
  1026. p = nlmsg_data(nlh);
  1027. copy_to_user_policy(xp, p, dir);
  1028. if (copy_to_user_tmpl(xp, skb) < 0)
  1029. goto nlmsg_failure;
  1030. if (copy_to_user_sec_ctx(xp, skb))
  1031. goto nlmsg_failure;
  1032. if (copy_to_user_policy_type(xp->type, skb) < 0)
  1033. goto nlmsg_failure;
  1034. nlmsg_end(skb, nlh);
  1035. return 0;
  1036. nlmsg_failure:
  1037. nlmsg_cancel(skb, nlh);
  1038. return -EMSGSIZE;
  1039. }
  1040. static int xfrm_dump_policy_done(struct netlink_callback *cb)
  1041. {
  1042. struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1];
  1043. xfrm_policy_walk_done(walk);
  1044. return 0;
  1045. }
  1046. static int xfrm_dump_policy(struct sk_buff *skb, struct netlink_callback *cb)
  1047. {
  1048. struct xfrm_policy_walk *walk = (struct xfrm_policy_walk *) &cb->args[1];
  1049. struct xfrm_dump_info info;
  1050. BUILD_BUG_ON(sizeof(struct xfrm_policy_walk) >
  1051. sizeof(cb->args) - sizeof(cb->args[0]));
  1052. info.in_skb = cb->skb;
  1053. info.out_skb = skb;
  1054. info.nlmsg_seq = cb->nlh->nlmsg_seq;
  1055. info.nlmsg_flags = NLM_F_MULTI;
  1056. if (!cb->args[0]) {
  1057. cb->args[0] = 1;
  1058. xfrm_policy_walk_init(walk, XFRM_POLICY_TYPE_ANY);
  1059. }
  1060. (void) xfrm_policy_walk(walk, dump_one_policy, &info);
  1061. return skb->len;
  1062. }
  1063. static struct sk_buff *xfrm_policy_netlink(struct sk_buff *in_skb,
  1064. struct xfrm_policy *xp,
  1065. int dir, u32 seq)
  1066. {
  1067. struct xfrm_dump_info info;
  1068. struct sk_buff *skb;
  1069. skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  1070. if (!skb)
  1071. return ERR_PTR(-ENOMEM);
  1072. info.in_skb = in_skb;
  1073. info.out_skb = skb;
  1074. info.nlmsg_seq = seq;
  1075. info.nlmsg_flags = 0;
  1076. if (dump_one_policy(xp, dir, 0, &info) < 0) {
  1077. kfree_skb(skb);
  1078. return NULL;
  1079. }
  1080. return skb;
  1081. }
  1082. static int xfrm_get_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
  1083. struct nlattr **attrs)
  1084. {
  1085. struct xfrm_policy *xp;
  1086. struct xfrm_userpolicy_id *p;
  1087. u8 type = XFRM_POLICY_TYPE_MAIN;
  1088. int err;
  1089. struct km_event c;
  1090. int delete;
  1091. p = nlmsg_data(nlh);
  1092. delete = nlh->nlmsg_type == XFRM_MSG_DELPOLICY;
  1093. err = copy_from_user_policy_type(&type, attrs);
  1094. if (err)
  1095. return err;
  1096. err = verify_policy_dir(p->dir);
  1097. if (err)
  1098. return err;
  1099. if (p->index)
  1100. xp = xfrm_policy_byid(type, p->dir, p->index, delete, &err);
  1101. else {
  1102. struct nlattr *rt = attrs[XFRMA_SEC_CTX];
  1103. struct xfrm_sec_ctx *ctx;
  1104. err = verify_sec_ctx_len(attrs);
  1105. if (err)
  1106. return err;
  1107. ctx = NULL;
  1108. if (rt) {
  1109. struct xfrm_user_sec_ctx *uctx = nla_data(rt);
  1110. err = security_xfrm_policy_alloc(&ctx, uctx);
  1111. if (err)
  1112. return err;
  1113. }
  1114. xp = xfrm_policy_bysel_ctx(type, p->dir, &p->sel, ctx,
  1115. delete, &err);
  1116. security_xfrm_policy_free(ctx);
  1117. }
  1118. if (xp == NULL)
  1119. return -ENOENT;
  1120. if (!delete) {
  1121. struct sk_buff *resp_skb;
  1122. resp_skb = xfrm_policy_netlink(skb, xp, p->dir, nlh->nlmsg_seq);
  1123. if (IS_ERR(resp_skb)) {
  1124. err = PTR_ERR(resp_skb);
  1125. } else {
  1126. err = nlmsg_unicast(xfrm_nl, resp_skb,
  1127. NETLINK_CB(skb).pid);
  1128. }
  1129. } else {
  1130. uid_t loginuid = NETLINK_CB(skb).loginuid;
  1131. u32 sessionid = NETLINK_CB(skb).sessionid;
  1132. u32 sid = NETLINK_CB(skb).sid;
  1133. xfrm_audit_policy_delete(xp, err ? 0 : 1, loginuid, sessionid,
  1134. sid);
  1135. if (err != 0)
  1136. goto out;
  1137. c.data.byid = p->index;
  1138. c.event = nlh->nlmsg_type;
  1139. c.seq = nlh->nlmsg_seq;
  1140. c.pid = nlh->nlmsg_pid;
  1141. km_policy_notify(xp, p->dir, &c);
  1142. }
  1143. out:
  1144. xfrm_pol_put(xp);
  1145. return err;
  1146. }
  1147. static int xfrm_flush_sa(struct sk_buff *skb, struct nlmsghdr *nlh,
  1148. struct nlattr **attrs)
  1149. {
  1150. struct km_event c;
  1151. struct xfrm_usersa_flush *p = nlmsg_data(nlh);
  1152. struct xfrm_audit audit_info;
  1153. int err;
  1154. audit_info.loginuid = NETLINK_CB(skb).loginuid;
  1155. audit_info.sessionid = NETLINK_CB(skb).sessionid;
  1156. audit_info.secid = NETLINK_CB(skb).sid;
  1157. err = xfrm_state_flush(p->proto, &audit_info);
  1158. if (err)
  1159. return err;
  1160. c.data.proto = p->proto;
  1161. c.event = nlh->nlmsg_type;
  1162. c.seq = nlh->nlmsg_seq;
  1163. c.pid = nlh->nlmsg_pid;
  1164. km_state_notify(NULL, &c);
  1165. return 0;
  1166. }
  1167. static inline size_t xfrm_aevent_msgsize(void)
  1168. {
  1169. return NLMSG_ALIGN(sizeof(struct xfrm_aevent_id))
  1170. + nla_total_size(sizeof(struct xfrm_replay_state))
  1171. + nla_total_size(sizeof(struct xfrm_lifetime_cur))
  1172. + nla_total_size(4) /* XFRM_AE_RTHR */
  1173. + nla_total_size(4); /* XFRM_AE_ETHR */
  1174. }
  1175. static int build_aevent(struct sk_buff *skb, struct xfrm_state *x, struct km_event *c)
  1176. {
  1177. struct xfrm_aevent_id *id;
  1178. struct nlmsghdr *nlh;
  1179. nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_NEWAE, sizeof(*id), 0);
  1180. if (nlh == NULL)
  1181. return -EMSGSIZE;
  1182. id = nlmsg_data(nlh);
  1183. memcpy(&id->sa_id.daddr, &x->id.daddr,sizeof(x->id.daddr));
  1184. id->sa_id.spi = x->id.spi;
  1185. id->sa_id.family = x->props.family;
  1186. id->sa_id.proto = x->id.proto;
  1187. memcpy(&id->saddr, &x->props.saddr,sizeof(x->props.saddr));
  1188. id->reqid = x->props.reqid;
  1189. id->flags = c->data.aevent;
  1190. NLA_PUT(skb, XFRMA_REPLAY_VAL, sizeof(x->replay), &x->replay);
  1191. NLA_PUT(skb, XFRMA_LTIME_VAL, sizeof(x->curlft), &x->curlft);
  1192. if (id->flags & XFRM_AE_RTHR)
  1193. NLA_PUT_U32(skb, XFRMA_REPLAY_THRESH, x->replay_maxdiff);
  1194. if (id->flags & XFRM_AE_ETHR)
  1195. NLA_PUT_U32(skb, XFRMA_ETIMER_THRESH,
  1196. x->replay_maxage * 10 / HZ);
  1197. return nlmsg_end(skb, nlh);
  1198. nla_put_failure:
  1199. nlmsg_cancel(skb, nlh);
  1200. return -EMSGSIZE;
  1201. }
  1202. static int xfrm_get_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
  1203. struct nlattr **attrs)
  1204. {
  1205. struct xfrm_state *x;
  1206. struct sk_buff *r_skb;
  1207. int err;
  1208. struct km_event c;
  1209. struct xfrm_aevent_id *p = nlmsg_data(nlh);
  1210. struct xfrm_usersa_id *id = &p->sa_id;
  1211. r_skb = nlmsg_new(xfrm_aevent_msgsize(), GFP_ATOMIC);
  1212. if (r_skb == NULL)
  1213. return -ENOMEM;
  1214. x = xfrm_state_lookup(&id->daddr, id->spi, id->proto, id->family);
  1215. if (x == NULL) {
  1216. kfree_skb(r_skb);
  1217. return -ESRCH;
  1218. }
  1219. /*
  1220. * XXX: is this lock really needed - none of the other
  1221. * gets lock (the concern is things getting updated
  1222. * while we are still reading) - jhs
  1223. */
  1224. spin_lock_bh(&x->lock);
  1225. c.data.aevent = p->flags;
  1226. c.seq = nlh->nlmsg_seq;
  1227. c.pid = nlh->nlmsg_pid;
  1228. if (build_aevent(r_skb, x, &c) < 0)
  1229. BUG();
  1230. err = nlmsg_unicast(xfrm_nl, r_skb, NETLINK_CB(skb).pid);
  1231. spin_unlock_bh(&x->lock);
  1232. xfrm_state_put(x);
  1233. return err;
  1234. }
  1235. static int xfrm_new_ae(struct sk_buff *skb, struct nlmsghdr *nlh,
  1236. struct nlattr **attrs)
  1237. {
  1238. struct xfrm_state *x;
  1239. struct km_event c;
  1240. int err = - EINVAL;
  1241. struct xfrm_aevent_id *p = nlmsg_data(nlh);
  1242. struct nlattr *rp = attrs[XFRMA_REPLAY_VAL];
  1243. struct nlattr *lt = attrs[XFRMA_LTIME_VAL];
  1244. if (!lt && !rp)
  1245. return err;
  1246. /* pedantic mode - thou shalt sayeth replaceth */
  1247. if (!(nlh->nlmsg_flags&NLM_F_REPLACE))
  1248. return err;
  1249. x = xfrm_state_lookup(&p->sa_id.daddr, p->sa_id.spi, p->sa_id.proto, p->sa_id.family);
  1250. if (x == NULL)
  1251. return -ESRCH;
  1252. if (x->km.state != XFRM_STATE_VALID)
  1253. goto out;
  1254. spin_lock_bh(&x->lock);
  1255. xfrm_update_ae_params(x, attrs);
  1256. spin_unlock_bh(&x->lock);
  1257. c.event = nlh->nlmsg_type;
  1258. c.seq = nlh->nlmsg_seq;
  1259. c.pid = nlh->nlmsg_pid;
  1260. c.data.aevent = XFRM_AE_CU;
  1261. km_state_notify(x, &c);
  1262. err = 0;
  1263. out:
  1264. xfrm_state_put(x);
  1265. return err;
  1266. }
  1267. static int xfrm_flush_policy(struct sk_buff *skb, struct nlmsghdr *nlh,
  1268. struct nlattr **attrs)
  1269. {
  1270. struct km_event c;
  1271. u8 type = XFRM_POLICY_TYPE_MAIN;
  1272. int err;
  1273. struct xfrm_audit audit_info;
  1274. err = copy_from_user_policy_type(&type, attrs);
  1275. if (err)
  1276. return err;
  1277. audit_info.loginuid = NETLINK_CB(skb).loginuid;
  1278. audit_info.sessionid = NETLINK_CB(skb).sessionid;
  1279. audit_info.secid = NETLINK_CB(skb).sid;
  1280. err = xfrm_policy_flush(type, &audit_info);
  1281. if (err)
  1282. return err;
  1283. c.data.type = type;
  1284. c.event = nlh->nlmsg_type;
  1285. c.seq = nlh->nlmsg_seq;
  1286. c.pid = nlh->nlmsg_pid;
  1287. km_policy_notify(NULL, 0, &c);
  1288. return 0;
  1289. }
  1290. static int xfrm_add_pol_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
  1291. struct nlattr **attrs)
  1292. {
  1293. struct xfrm_policy *xp;
  1294. struct xfrm_user_polexpire *up = nlmsg_data(nlh);
  1295. struct xfrm_userpolicy_info *p = &up->pol;
  1296. u8 type = XFRM_POLICY_TYPE_MAIN;
  1297. int err = -ENOENT;
  1298. err = copy_from_user_policy_type(&type, attrs);
  1299. if (err)
  1300. return err;
  1301. if (p->index)
  1302. xp = xfrm_policy_byid(type, p->dir, p->index, 0, &err);
  1303. else {
  1304. struct nlattr *rt = attrs[XFRMA_SEC_CTX];
  1305. struct xfrm_sec_ctx *ctx;
  1306. err = verify_sec_ctx_len(attrs);
  1307. if (err)
  1308. return err;
  1309. ctx = NULL;
  1310. if (rt) {
  1311. struct xfrm_user_sec_ctx *uctx = nla_data(rt);
  1312. err = security_xfrm_policy_alloc(&ctx, uctx);
  1313. if (err)
  1314. return err;
  1315. }
  1316. xp = xfrm_policy_bysel_ctx(type, p->dir, &p->sel, ctx, 0, &err);
  1317. security_xfrm_policy_free(ctx);
  1318. }
  1319. if (xp == NULL)
  1320. return -ENOENT;
  1321. read_lock(&xp->lock);
  1322. if (xp->dead) {
  1323. read_unlock(&xp->lock);
  1324. goto out;
  1325. }
  1326. read_unlock(&xp->lock);
  1327. err = 0;
  1328. if (up->hard) {
  1329. uid_t loginuid = NETLINK_CB(skb).loginuid;
  1330. uid_t sessionid = NETLINK_CB(skb).sessionid;
  1331. u32 sid = NETLINK_CB(skb).sid;
  1332. xfrm_policy_delete(xp, p->dir);
  1333. xfrm_audit_policy_delete(xp, 1, loginuid, sessionid, sid);
  1334. } else {
  1335. // reset the timers here?
  1336. printk("Dont know what to do with soft policy expire\n");
  1337. }
  1338. km_policy_expired(xp, p->dir, up->hard, current->pid);
  1339. out:
  1340. xfrm_pol_put(xp);
  1341. return err;
  1342. }
  1343. static int xfrm_add_sa_expire(struct sk_buff *skb, struct nlmsghdr *nlh,
  1344. struct nlattr **attrs)
  1345. {
  1346. struct xfrm_state *x;
  1347. int err;
  1348. struct xfrm_user_expire *ue = nlmsg_data(nlh);
  1349. struct xfrm_usersa_info *p = &ue->state;
  1350. x = xfrm_state_lookup(&p->id.daddr, p->id.spi, p->id.proto, p->family);
  1351. err = -ENOENT;
  1352. if (x == NULL)
  1353. return err;
  1354. spin_lock_bh(&x->lock);
  1355. err = -EINVAL;
  1356. if (x->km.state != XFRM_STATE_VALID)
  1357. goto out;
  1358. km_state_expired(x, ue->hard, current->pid);
  1359. if (ue->hard) {
  1360. uid_t loginuid = NETLINK_CB(skb).loginuid;
  1361. uid_t sessionid = NETLINK_CB(skb).sessionid;
  1362. u32 sid = NETLINK_CB(skb).sid;
  1363. __xfrm_state_delete(x);
  1364. xfrm_audit_state_delete(x, 1, loginuid, sessionid, sid);
  1365. }
  1366. err = 0;
  1367. out:
  1368. spin_unlock_bh(&x->lock);
  1369. xfrm_state_put(x);
  1370. return err;
  1371. }
  1372. static int xfrm_add_acquire(struct sk_buff *skb, struct nlmsghdr *nlh,
  1373. struct nlattr **attrs)
  1374. {
  1375. struct xfrm_policy *xp;
  1376. struct xfrm_user_tmpl *ut;
  1377. int i;
  1378. struct nlattr *rt = attrs[XFRMA_TMPL];
  1379. struct xfrm_user_acquire *ua = nlmsg_data(nlh);
  1380. struct xfrm_state *x = xfrm_state_alloc();
  1381. int err = -ENOMEM;
  1382. if (!x)
  1383. return err;
  1384. err = verify_newpolicy_info(&ua->policy);
  1385. if (err) {
  1386. printk("BAD policy passed\n");
  1387. kfree(x);
  1388. return err;
  1389. }
  1390. /* build an XP */
  1391. xp = xfrm_policy_construct(&ua->policy, attrs, &err);
  1392. if (!xp) {
  1393. kfree(x);
  1394. return err;
  1395. }
  1396. memcpy(&x->id, &ua->id, sizeof(ua->id));
  1397. memcpy(&x->props.saddr, &ua->saddr, sizeof(ua->saddr));
  1398. memcpy(&x->sel, &ua->sel, sizeof(ua->sel));
  1399. ut = nla_data(rt);
  1400. /* extract the templates and for each call km_key */
  1401. for (i = 0; i < xp->xfrm_nr; i++, ut++) {
  1402. struct xfrm_tmpl *t = &xp->xfrm_vec[i];
  1403. memcpy(&x->id, &t->id, sizeof(x->id));
  1404. x->props.mode = t->mode;
  1405. x->props.reqid = t->reqid;
  1406. x->props.family = ut->family;
  1407. t->aalgos = ua->aalgos;
  1408. t->ealgos = ua->ealgos;
  1409. t->calgos = ua->calgos;
  1410. err = km_query(x, t, xp);
  1411. }
  1412. kfree(x);
  1413. kfree(xp);
  1414. return 0;
  1415. }
  1416. #ifdef CONFIG_XFRM_MIGRATE
  1417. static int copy_from_user_migrate(struct xfrm_migrate *ma,
  1418. struct nlattr **attrs, int *num)
  1419. {
  1420. struct nlattr *rt = attrs[XFRMA_MIGRATE];
  1421. struct xfrm_user_migrate *um;
  1422. int i, num_migrate;
  1423. um = nla_data(rt);
  1424. num_migrate = nla_len(rt) / sizeof(*um);
  1425. if (num_migrate <= 0 || num_migrate > XFRM_MAX_DEPTH)
  1426. return -EINVAL;
  1427. for (i = 0; i < num_migrate; i++, um++, ma++) {
  1428. memcpy(&ma->old_daddr, &um->old_daddr, sizeof(ma->old_daddr));
  1429. memcpy(&ma->old_saddr, &um->old_saddr, sizeof(ma->old_saddr));
  1430. memcpy(&ma->new_daddr, &um->new_daddr, sizeof(ma->new_daddr));
  1431. memcpy(&ma->new_saddr, &um->new_saddr, sizeof(ma->new_saddr));
  1432. ma->proto = um->proto;
  1433. ma->mode = um->mode;
  1434. ma->reqid = um->reqid;
  1435. ma->old_family = um->old_family;
  1436. ma->new_family = um->new_family;
  1437. }
  1438. *num = i;
  1439. return 0;
  1440. }
  1441. static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
  1442. struct nlattr **attrs)
  1443. {
  1444. struct xfrm_userpolicy_id *pi = nlmsg_data(nlh);
  1445. struct xfrm_migrate m[XFRM_MAX_DEPTH];
  1446. u8 type;
  1447. int err;
  1448. int n = 0;
  1449. if (attrs[XFRMA_MIGRATE] == NULL)
  1450. return -EINVAL;
  1451. err = copy_from_user_policy_type(&type, attrs);
  1452. if (err)
  1453. return err;
  1454. err = copy_from_user_migrate((struct xfrm_migrate *)m,
  1455. attrs, &n);
  1456. if (err)
  1457. return err;
  1458. if (!n)
  1459. return 0;
  1460. xfrm_migrate(&pi->sel, pi->dir, type, m, n);
  1461. return 0;
  1462. }
  1463. #else
  1464. static int xfrm_do_migrate(struct sk_buff *skb, struct nlmsghdr *nlh,
  1465. struct nlattr **attrs)
  1466. {
  1467. return -ENOPROTOOPT;
  1468. }
  1469. #endif
  1470. #ifdef CONFIG_XFRM_MIGRATE
  1471. static int copy_to_user_migrate(struct xfrm_migrate *m, struct sk_buff *skb)
  1472. {
  1473. struct xfrm_user_migrate um;
  1474. memset(&um, 0, sizeof(um));
  1475. um.proto = m->proto;
  1476. um.mode = m->mode;
  1477. um.reqid = m->reqid;
  1478. um.old_family = m->old_family;
  1479. memcpy(&um.old_daddr, &m->old_daddr, sizeof(um.old_daddr));
  1480. memcpy(&um.old_saddr, &m->old_saddr, sizeof(um.old_saddr));
  1481. um.new_family = m->new_family;
  1482. memcpy(&um.new_daddr, &m->new_daddr, sizeof(um.new_daddr));
  1483. memcpy(&um.new_saddr, &m->new_saddr, sizeof(um.new_saddr));
  1484. return nla_put(skb, XFRMA_MIGRATE, sizeof(um), &um);
  1485. }
  1486. static inline size_t xfrm_migrate_msgsize(int num_migrate)
  1487. {
  1488. return NLMSG_ALIGN(sizeof(struct xfrm_userpolicy_id))
  1489. + nla_total_size(sizeof(struct xfrm_user_migrate) * num_migrate)
  1490. + userpolicy_type_attrsize();
  1491. }
  1492. static int build_migrate(struct sk_buff *skb, struct xfrm_migrate *m,
  1493. int num_migrate, struct xfrm_selector *sel,
  1494. u8 dir, u8 type)
  1495. {
  1496. struct xfrm_migrate *mp;
  1497. struct xfrm_userpolicy_id *pol_id;
  1498. struct nlmsghdr *nlh;
  1499. int i;
  1500. nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_MIGRATE, sizeof(*pol_id), 0);
  1501. if (nlh == NULL)
  1502. return -EMSGSIZE;
  1503. pol_id = nlmsg_data(nlh);
  1504. /* copy data from selector, dir, and type to the pol_id */
  1505. memset(pol_id, 0, sizeof(*pol_id));
  1506. memcpy(&pol_id->sel, sel, sizeof(pol_id->sel));
  1507. pol_id->dir = dir;
  1508. if (copy_to_user_policy_type(type, skb) < 0)
  1509. goto nlmsg_failure;
  1510. for (i = 0, mp = m ; i < num_migrate; i++, mp++) {
  1511. if (copy_to_user_migrate(mp, skb) < 0)
  1512. goto nlmsg_failure;
  1513. }
  1514. return nlmsg_end(skb, nlh);
  1515. nlmsg_failure:
  1516. nlmsg_cancel(skb, nlh);
  1517. return -EMSGSIZE;
  1518. }
  1519. static int xfrm_send_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
  1520. struct xfrm_migrate *m, int num_migrate)
  1521. {
  1522. struct sk_buff *skb;
  1523. skb = nlmsg_new(xfrm_migrate_msgsize(num_migrate), GFP_ATOMIC);
  1524. if (skb == NULL)
  1525. return -ENOMEM;
  1526. /* build migrate */
  1527. if (build_migrate(skb, m, num_migrate, sel, dir, type) < 0)
  1528. BUG();
  1529. return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_MIGRATE, GFP_ATOMIC);
  1530. }
  1531. #else
  1532. static int xfrm_send_migrate(struct xfrm_selector *sel, u8 dir, u8 type,
  1533. struct xfrm_migrate *m, int num_migrate)
  1534. {
  1535. return -ENOPROTOOPT;
  1536. }
  1537. #endif
  1538. #define XMSGSIZE(type) sizeof(struct type)
  1539. static const int xfrm_msg_min[XFRM_NR_MSGTYPES] = {
  1540. [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
  1541. [XFRM_MSG_DELSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
  1542. [XFRM_MSG_GETSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_id),
  1543. [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
  1544. [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
  1545. [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
  1546. [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userspi_info),
  1547. [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_acquire),
  1548. [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_expire),
  1549. [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_info),
  1550. [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_info),
  1551. [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_polexpire),
  1552. [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = XMSGSIZE(xfrm_usersa_flush),
  1553. [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = 0,
  1554. [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
  1555. [XFRM_MSG_GETAE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_aevent_id),
  1556. [XFRM_MSG_REPORT - XFRM_MSG_BASE] = XMSGSIZE(xfrm_user_report),
  1557. [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = XMSGSIZE(xfrm_userpolicy_id),
  1558. [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = sizeof(u32),
  1559. [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = sizeof(u32),
  1560. };
  1561. #undef XMSGSIZE
  1562. static const struct nla_policy xfrma_policy[XFRMA_MAX+1] = {
  1563. [XFRMA_ALG_AEAD] = { .len = sizeof(struct xfrm_algo_aead) },
  1564. [XFRMA_ALG_AUTH] = { .len = sizeof(struct xfrm_algo) },
  1565. [XFRMA_ALG_CRYPT] = { .len = sizeof(struct xfrm_algo) },
  1566. [XFRMA_ALG_COMP] = { .len = sizeof(struct xfrm_algo) },
  1567. [XFRMA_ENCAP] = { .len = sizeof(struct xfrm_encap_tmpl) },
  1568. [XFRMA_TMPL] = { .len = sizeof(struct xfrm_user_tmpl) },
  1569. [XFRMA_SEC_CTX] = { .len = sizeof(struct xfrm_sec_ctx) },
  1570. [XFRMA_LTIME_VAL] = { .len = sizeof(struct xfrm_lifetime_cur) },
  1571. [XFRMA_REPLAY_VAL] = { .len = sizeof(struct xfrm_replay_state) },
  1572. [XFRMA_REPLAY_THRESH] = { .type = NLA_U32 },
  1573. [XFRMA_ETIMER_THRESH] = { .type = NLA_U32 },
  1574. [XFRMA_SRCADDR] = { .len = sizeof(xfrm_address_t) },
  1575. [XFRMA_COADDR] = { .len = sizeof(xfrm_address_t) },
  1576. [XFRMA_POLICY_TYPE] = { .len = sizeof(struct xfrm_userpolicy_type)},
  1577. [XFRMA_MIGRATE] = { .len = sizeof(struct xfrm_user_migrate) },
  1578. };
  1579. static struct xfrm_link {
  1580. int (*doit)(struct sk_buff *, struct nlmsghdr *, struct nlattr **);
  1581. int (*dump)(struct sk_buff *, struct netlink_callback *);
  1582. int (*done)(struct netlink_callback *);
  1583. } xfrm_dispatch[XFRM_NR_MSGTYPES] = {
  1584. [XFRM_MSG_NEWSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
  1585. [XFRM_MSG_DELSA - XFRM_MSG_BASE] = { .doit = xfrm_del_sa },
  1586. [XFRM_MSG_GETSA - XFRM_MSG_BASE] = { .doit = xfrm_get_sa,
  1587. .dump = xfrm_dump_sa,
  1588. .done = xfrm_dump_sa_done },
  1589. [XFRM_MSG_NEWPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
  1590. [XFRM_MSG_DELPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy },
  1591. [XFRM_MSG_GETPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_get_policy,
  1592. .dump = xfrm_dump_policy,
  1593. .done = xfrm_dump_policy_done },
  1594. [XFRM_MSG_ALLOCSPI - XFRM_MSG_BASE] = { .doit = xfrm_alloc_userspi },
  1595. [XFRM_MSG_ACQUIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_acquire },
  1596. [XFRM_MSG_EXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_sa_expire },
  1597. [XFRM_MSG_UPDPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_add_policy },
  1598. [XFRM_MSG_UPDSA - XFRM_MSG_BASE] = { .doit = xfrm_add_sa },
  1599. [XFRM_MSG_POLEXPIRE - XFRM_MSG_BASE] = { .doit = xfrm_add_pol_expire},
  1600. [XFRM_MSG_FLUSHSA - XFRM_MSG_BASE] = { .doit = xfrm_flush_sa },
  1601. [XFRM_MSG_FLUSHPOLICY - XFRM_MSG_BASE] = { .doit = xfrm_flush_policy },
  1602. [XFRM_MSG_NEWAE - XFRM_MSG_BASE] = { .doit = xfrm_new_ae },
  1603. [XFRM_MSG_GETAE - XFRM_MSG_BASE] = { .doit = xfrm_get_ae },
  1604. [XFRM_MSG_MIGRATE - XFRM_MSG_BASE] = { .doit = xfrm_do_migrate },
  1605. [XFRM_MSG_GETSADINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_sadinfo },
  1606. [XFRM_MSG_GETSPDINFO - XFRM_MSG_BASE] = { .doit = xfrm_get_spdinfo },
  1607. };
  1608. static int xfrm_user_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
  1609. {
  1610. struct nlattr *attrs[XFRMA_MAX+1];
  1611. struct xfrm_link *link;
  1612. int type, err;
  1613. type = nlh->nlmsg_type;
  1614. if (type > XFRM_MSG_MAX)
  1615. return -EINVAL;
  1616. type -= XFRM_MSG_BASE;
  1617. link = &xfrm_dispatch[type];
  1618. /* All operations require privileges, even GET */
  1619. if (security_netlink_recv(skb, CAP_NET_ADMIN))
  1620. return -EPERM;
  1621. if ((type == (XFRM_MSG_GETSA - XFRM_MSG_BASE) ||
  1622. type == (XFRM_MSG_GETPOLICY - XFRM_MSG_BASE)) &&
  1623. (nlh->nlmsg_flags & NLM_F_DUMP)) {
  1624. if (link->dump == NULL)
  1625. return -EINVAL;
  1626. return netlink_dump_start(xfrm_nl, skb, nlh, link->dump, link->done);
  1627. }
  1628. err = nlmsg_parse(nlh, xfrm_msg_min[type], attrs, XFRMA_MAX,
  1629. xfrma_policy);
  1630. if (err < 0)
  1631. return err;
  1632. if (link->doit == NULL)
  1633. return -EINVAL;
  1634. return link->doit(skb, nlh, attrs);
  1635. }
  1636. static void xfrm_netlink_rcv(struct sk_buff *skb)
  1637. {
  1638. mutex_lock(&xfrm_cfg_mutex);
  1639. netlink_rcv_skb(skb, &xfrm_user_rcv_msg);
  1640. mutex_unlock(&xfrm_cfg_mutex);
  1641. }
  1642. static inline size_t xfrm_expire_msgsize(void)
  1643. {
  1644. return NLMSG_ALIGN(sizeof(struct xfrm_user_expire));
  1645. }
  1646. static int build_expire(struct sk_buff *skb, struct xfrm_state *x, struct km_event *c)
  1647. {
  1648. struct xfrm_user_expire *ue;
  1649. struct nlmsghdr *nlh;
  1650. nlh = nlmsg_put(skb, c->pid, 0, XFRM_MSG_EXPIRE, sizeof(*ue), 0);
  1651. if (nlh == NULL)
  1652. return -EMSGSIZE;
  1653. ue = nlmsg_data(nlh);
  1654. copy_to_user_state(x, &ue->state);
  1655. ue->hard = (c->data.hard != 0) ? 1 : 0;
  1656. return nlmsg_end(skb, nlh);
  1657. }
  1658. static int xfrm_exp_state_notify(struct xfrm_state *x, struct km_event *c)
  1659. {
  1660. struct sk_buff *skb;
  1661. skb = nlmsg_new(xfrm_expire_msgsize(), GFP_ATOMIC);
  1662. if (skb == NULL)
  1663. return -ENOMEM;
  1664. if (build_expire(skb, x, c) < 0)
  1665. BUG();
  1666. return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
  1667. }
  1668. static int xfrm_aevent_state_notify(struct xfrm_state *x, struct km_event *c)
  1669. {
  1670. struct sk_buff *skb;
  1671. skb = nlmsg_new(xfrm_aevent_msgsize(), GFP_ATOMIC);
  1672. if (skb == NULL)
  1673. return -ENOMEM;
  1674. if (build_aevent(skb, x, c) < 0)
  1675. BUG();
  1676. return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_AEVENTS, GFP_ATOMIC);
  1677. }
  1678. static int xfrm_notify_sa_flush(struct km_event *c)
  1679. {
  1680. struct xfrm_usersa_flush *p;
  1681. struct nlmsghdr *nlh;
  1682. struct sk_buff *skb;
  1683. int len = NLMSG_ALIGN(sizeof(struct xfrm_usersa_flush));
  1684. skb = nlmsg_new(len, GFP_ATOMIC);
  1685. if (skb == NULL)
  1686. return -ENOMEM;
  1687. nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_FLUSHSA, sizeof(*p), 0);
  1688. if (nlh == NULL) {
  1689. kfree_skb(skb);
  1690. return -EMSGSIZE;
  1691. }
  1692. p = nlmsg_data(nlh);
  1693. p->proto = c->data.proto;
  1694. nlmsg_end(skb, nlh);
  1695. return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
  1696. }
  1697. static inline size_t xfrm_sa_len(struct xfrm_state *x)
  1698. {
  1699. size_t l = 0;
  1700. if (x->aead)
  1701. l += nla_total_size(aead_len(x->aead));
  1702. if (x->aalg)
  1703. l += nla_total_size(xfrm_alg_len(x->aalg));
  1704. if (x->ealg)
  1705. l += nla_total_size(xfrm_alg_len(x->ealg));
  1706. if (x->calg)
  1707. l += nla_total_size(sizeof(*x->calg));
  1708. if (x->encap)
  1709. l += nla_total_size(sizeof(*x->encap));
  1710. if (x->security)
  1711. l += nla_total_size(sizeof(struct xfrm_user_sec_ctx) +
  1712. x->security->ctx_len);
  1713. if (x->coaddr)
  1714. l += nla_total_size(sizeof(*x->coaddr));
  1715. /* Must count x->lastused as it may become non-zero behind our back. */
  1716. l += nla_total_size(sizeof(u64));
  1717. return l;
  1718. }
  1719. static int xfrm_notify_sa(struct xfrm_state *x, struct km_event *c)
  1720. {
  1721. struct xfrm_usersa_info *p;
  1722. struct xfrm_usersa_id *id;
  1723. struct nlmsghdr *nlh;
  1724. struct sk_buff *skb;
  1725. int len = xfrm_sa_len(x);
  1726. int headlen;
  1727. headlen = sizeof(*p);
  1728. if (c->event == XFRM_MSG_DELSA) {
  1729. len += nla_total_size(headlen);
  1730. headlen = sizeof(*id);
  1731. }
  1732. len += NLMSG_ALIGN(headlen);
  1733. skb = nlmsg_new(len, GFP_ATOMIC);
  1734. if (skb == NULL)
  1735. return -ENOMEM;
  1736. nlh = nlmsg_put(skb, c->pid, c->seq, c->event, headlen, 0);
  1737. if (nlh == NULL)
  1738. goto nla_put_failure;
  1739. p = nlmsg_data(nlh);
  1740. if (c->event == XFRM_MSG_DELSA) {
  1741. struct nlattr *attr;
  1742. id = nlmsg_data(nlh);
  1743. memcpy(&id->daddr, &x->id.daddr, sizeof(id->daddr));
  1744. id->spi = x->id.spi;
  1745. id->family = x->props.family;
  1746. id->proto = x->id.proto;
  1747. attr = nla_reserve(skb, XFRMA_SA, sizeof(*p));
  1748. if (attr == NULL)
  1749. goto nla_put_failure;
  1750. p = nla_data(attr);
  1751. }
  1752. if (copy_to_user_state_extra(x, p, skb))
  1753. goto nla_put_failure;
  1754. nlmsg_end(skb, nlh);
  1755. return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_SA, GFP_ATOMIC);
  1756. nla_put_failure:
  1757. /* Somebody screwed up with xfrm_sa_len! */
  1758. WARN_ON(1);
  1759. kfree_skb(skb);
  1760. return -1;
  1761. }
  1762. static int xfrm_send_state_notify(struct xfrm_state *x, struct km_event *c)
  1763. {
  1764. switch (c->event) {
  1765. case XFRM_MSG_EXPIRE:
  1766. return xfrm_exp_state_notify(x, c);
  1767. case XFRM_MSG_NEWAE:
  1768. return xfrm_aevent_state_notify(x, c);
  1769. case XFRM_MSG_DELSA:
  1770. case XFRM_MSG_UPDSA:
  1771. case XFRM_MSG_NEWSA:
  1772. return xfrm_notify_sa(x, c);
  1773. case XFRM_MSG_FLUSHSA:
  1774. return xfrm_notify_sa_flush(c);
  1775. default:
  1776. printk("xfrm_user: Unknown SA event %d\n", c->event);
  1777. break;
  1778. }
  1779. return 0;
  1780. }
  1781. static inline size_t xfrm_acquire_msgsize(struct xfrm_state *x,
  1782. struct xfrm_policy *xp)
  1783. {
  1784. return NLMSG_ALIGN(sizeof(struct xfrm_user_acquire))
  1785. + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
  1786. + nla_total_size(xfrm_user_sec_ctx_size(x->security))
  1787. + userpolicy_type_attrsize();
  1788. }
  1789. static int build_acquire(struct sk_buff *skb, struct xfrm_state *x,
  1790. struct xfrm_tmpl *xt, struct xfrm_policy *xp,
  1791. int dir)
  1792. {
  1793. struct xfrm_user_acquire *ua;
  1794. struct nlmsghdr *nlh;
  1795. __u32 seq = xfrm_get_acqseq();
  1796. nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_ACQUIRE, sizeof(*ua), 0);
  1797. if (nlh == NULL)
  1798. return -EMSGSIZE;
  1799. ua = nlmsg_data(nlh);
  1800. memcpy(&ua->id, &x->id, sizeof(ua->id));
  1801. memcpy(&ua->saddr, &x->props.saddr, sizeof(ua->saddr));
  1802. memcpy(&ua->sel, &x->sel, sizeof(ua->sel));
  1803. copy_to_user_policy(xp, &ua->policy, dir);
  1804. ua->aalgos = xt->aalgos;
  1805. ua->ealgos = xt->ealgos;
  1806. ua->calgos = xt->calgos;
  1807. ua->seq = x->km.seq = seq;
  1808. if (copy_to_user_tmpl(xp, skb) < 0)
  1809. goto nlmsg_failure;
  1810. if (copy_to_user_state_sec_ctx(x, skb))
  1811. goto nlmsg_failure;
  1812. if (copy_to_user_policy_type(xp->type, skb) < 0)
  1813. goto nlmsg_failure;
  1814. return nlmsg_end(skb, nlh);
  1815. nlmsg_failure:
  1816. nlmsg_cancel(skb, nlh);
  1817. return -EMSGSIZE;
  1818. }
  1819. static int xfrm_send_acquire(struct xfrm_state *x, struct xfrm_tmpl *xt,
  1820. struct xfrm_policy *xp, int dir)
  1821. {
  1822. struct sk_buff *skb;
  1823. skb = nlmsg_new(xfrm_acquire_msgsize(x, xp), GFP_ATOMIC);
  1824. if (skb == NULL)
  1825. return -ENOMEM;
  1826. if (build_acquire(skb, x, xt, xp, dir) < 0)
  1827. BUG();
  1828. return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_ACQUIRE, GFP_ATOMIC);
  1829. }
  1830. /* User gives us xfrm_user_policy_info followed by an array of 0
  1831. * or more templates.
  1832. */
  1833. static struct xfrm_policy *xfrm_compile_policy(struct sock *sk, int opt,
  1834. u8 *data, int len, int *dir)
  1835. {
  1836. struct xfrm_userpolicy_info *p = (struct xfrm_userpolicy_info *)data;
  1837. struct xfrm_user_tmpl *ut = (struct xfrm_user_tmpl *) (p + 1);
  1838. struct xfrm_policy *xp;
  1839. int nr;
  1840. switch (sk->sk_family) {
  1841. case AF_INET:
  1842. if (opt != IP_XFRM_POLICY) {
  1843. *dir = -EOPNOTSUPP;
  1844. return NULL;
  1845. }
  1846. break;
  1847. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  1848. case AF_INET6:
  1849. if (opt != IPV6_XFRM_POLICY) {
  1850. *dir = -EOPNOTSUPP;
  1851. return NULL;
  1852. }
  1853. break;
  1854. #endif
  1855. default:
  1856. *dir = -EINVAL;
  1857. return NULL;
  1858. }
  1859. *dir = -EINVAL;
  1860. if (len < sizeof(*p) ||
  1861. verify_newpolicy_info(p))
  1862. return NULL;
  1863. nr = ((len - sizeof(*p)) / sizeof(*ut));
  1864. if (validate_tmpl(nr, ut, p->sel.family))
  1865. return NULL;
  1866. if (p->dir > XFRM_POLICY_OUT)
  1867. return NULL;
  1868. xp = xfrm_policy_alloc(GFP_KERNEL);
  1869. if (xp == NULL) {
  1870. *dir = -ENOBUFS;
  1871. return NULL;
  1872. }
  1873. copy_from_user_policy(xp, p);
  1874. xp->type = XFRM_POLICY_TYPE_MAIN;
  1875. copy_templates(xp, ut, nr);
  1876. *dir = p->dir;
  1877. return xp;
  1878. }
  1879. static inline size_t xfrm_polexpire_msgsize(struct xfrm_policy *xp)
  1880. {
  1881. return NLMSG_ALIGN(sizeof(struct xfrm_user_polexpire))
  1882. + nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr)
  1883. + nla_total_size(xfrm_user_sec_ctx_size(xp->security))
  1884. + userpolicy_type_attrsize();
  1885. }
  1886. static int build_polexpire(struct sk_buff *skb, struct xfrm_policy *xp,
  1887. int dir, struct km_event *c)
  1888. {
  1889. struct xfrm_user_polexpire *upe;
  1890. struct nlmsghdr *nlh;
  1891. int hard = c->data.hard;
  1892. nlh = nlmsg_put(skb, c->pid, 0, XFRM_MSG_POLEXPIRE, sizeof(*upe), 0);
  1893. if (nlh == NULL)
  1894. return -EMSGSIZE;
  1895. upe = nlmsg_data(nlh);
  1896. copy_to_user_policy(xp, &upe->pol, dir);
  1897. if (copy_to_user_tmpl(xp, skb) < 0)
  1898. goto nlmsg_failure;
  1899. if (copy_to_user_sec_ctx(xp, skb))
  1900. goto nlmsg_failure;
  1901. if (copy_to_user_policy_type(xp->type, skb) < 0)
  1902. goto nlmsg_failure;
  1903. upe->hard = !!hard;
  1904. return nlmsg_end(skb, nlh);
  1905. nlmsg_failure:
  1906. nlmsg_cancel(skb, nlh);
  1907. return -EMSGSIZE;
  1908. }
  1909. static int xfrm_exp_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
  1910. {
  1911. struct sk_buff *skb;
  1912. skb = nlmsg_new(xfrm_polexpire_msgsize(xp), GFP_ATOMIC);
  1913. if (skb == NULL)
  1914. return -ENOMEM;
  1915. if (build_polexpire(skb, xp, dir, c) < 0)
  1916. BUG();
  1917. return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_EXPIRE, GFP_ATOMIC);
  1918. }
  1919. static int xfrm_notify_policy(struct xfrm_policy *xp, int dir, struct km_event *c)
  1920. {
  1921. struct xfrm_userpolicy_info *p;
  1922. struct xfrm_userpolicy_id *id;
  1923. struct nlmsghdr *nlh;
  1924. struct sk_buff *skb;
  1925. int len = nla_total_size(sizeof(struct xfrm_user_tmpl) * xp->xfrm_nr);
  1926. int headlen;
  1927. headlen = sizeof(*p);
  1928. if (c->event == XFRM_MSG_DELPOLICY) {
  1929. len += nla_total_size(headlen);
  1930. headlen = sizeof(*id);
  1931. }
  1932. len += userpolicy_type_attrsize();
  1933. len += NLMSG_ALIGN(headlen);
  1934. skb = nlmsg_new(len, GFP_ATOMIC);
  1935. if (skb == NULL)
  1936. return -ENOMEM;
  1937. nlh = nlmsg_put(skb, c->pid, c->seq, c->event, headlen, 0);
  1938. if (nlh == NULL)
  1939. goto nlmsg_failure;
  1940. p = nlmsg_data(nlh);
  1941. if (c->event == XFRM_MSG_DELPOLICY) {
  1942. struct nlattr *attr;
  1943. id = nlmsg_data(nlh);
  1944. memset(id, 0, sizeof(*id));
  1945. id->dir = dir;
  1946. if (c->data.byid)
  1947. id->index = xp->index;
  1948. else
  1949. memcpy(&id->sel, &xp->selector, sizeof(id->sel));
  1950. attr = nla_reserve(skb, XFRMA_POLICY, sizeof(*p));
  1951. if (attr == NULL)
  1952. goto nlmsg_failure;
  1953. p = nla_data(attr);
  1954. }
  1955. copy_to_user_policy(xp, p, dir);
  1956. if (copy_to_user_tmpl(xp, skb) < 0)
  1957. goto nlmsg_failure;
  1958. if (copy_to_user_policy_type(xp->type, skb) < 0)
  1959. goto nlmsg_failure;
  1960. nlmsg_end(skb, nlh);
  1961. return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
  1962. nlmsg_failure:
  1963. kfree_skb(skb);
  1964. return -1;
  1965. }
  1966. static int xfrm_notify_policy_flush(struct km_event *c)
  1967. {
  1968. struct nlmsghdr *nlh;
  1969. struct sk_buff *skb;
  1970. skb = nlmsg_new(userpolicy_type_attrsize(), GFP_ATOMIC);
  1971. if (skb == NULL)
  1972. return -ENOMEM;
  1973. nlh = nlmsg_put(skb, c->pid, c->seq, XFRM_MSG_FLUSHPOLICY, 0, 0);
  1974. if (nlh == NULL)
  1975. goto nlmsg_failure;
  1976. if (copy_to_user_policy_type(c->data.type, skb) < 0)
  1977. goto nlmsg_failure;
  1978. nlmsg_end(skb, nlh);
  1979. return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_POLICY, GFP_ATOMIC);
  1980. nlmsg_failure:
  1981. kfree_skb(skb);
  1982. return -1;
  1983. }
  1984. static int xfrm_send_policy_notify(struct xfrm_policy *xp, int dir, struct km_event *c)
  1985. {
  1986. switch (c->event) {
  1987. case XFRM_MSG_NEWPOLICY:
  1988. case XFRM_MSG_UPDPOLICY:
  1989. case XFRM_MSG_DELPOLICY:
  1990. return xfrm_notify_policy(xp, dir, c);
  1991. case XFRM_MSG_FLUSHPOLICY:
  1992. return xfrm_notify_policy_flush(c);
  1993. case XFRM_MSG_POLEXPIRE:
  1994. return xfrm_exp_policy_notify(xp, dir, c);
  1995. default:
  1996. printk("xfrm_user: Unknown Policy event %d\n", c->event);
  1997. }
  1998. return 0;
  1999. }
  2000. static inline size_t xfrm_report_msgsize(void)
  2001. {
  2002. return NLMSG_ALIGN(sizeof(struct xfrm_user_report));
  2003. }
  2004. static int build_report(struct sk_buff *skb, u8 proto,
  2005. struct xfrm_selector *sel, xfrm_address_t *addr)
  2006. {
  2007. struct xfrm_user_report *ur;
  2008. struct nlmsghdr *nlh;
  2009. nlh = nlmsg_put(skb, 0, 0, XFRM_MSG_REPORT, sizeof(*ur), 0);
  2010. if (nlh == NULL)
  2011. return -EMSGSIZE;
  2012. ur = nlmsg_data(nlh);
  2013. ur->proto = proto;
  2014. memcpy(&ur->sel, sel, sizeof(ur->sel));
  2015. if (addr)
  2016. NLA_PUT(skb, XFRMA_COADDR, sizeof(*addr), addr);
  2017. return nlmsg_end(skb, nlh);
  2018. nla_put_failure:
  2019. nlmsg_cancel(skb, nlh);
  2020. return -EMSGSIZE;
  2021. }
  2022. static int xfrm_send_report(u8 proto, struct xfrm_selector *sel,
  2023. xfrm_address_t *addr)
  2024. {
  2025. struct sk_buff *skb;
  2026. skb = nlmsg_new(xfrm_report_msgsize(), GFP_ATOMIC);
  2027. if (skb == NULL)
  2028. return -ENOMEM;
  2029. if (build_report(skb, proto, sel, addr) < 0)
  2030. BUG();
  2031. return nlmsg_multicast(xfrm_nl, skb, 0, XFRMNLGRP_REPORT, GFP_ATOMIC);
  2032. }
  2033. static struct xfrm_mgr netlink_mgr = {
  2034. .id = "netlink",
  2035. .notify = xfrm_send_state_notify,
  2036. .acquire = xfrm_send_acquire,
  2037. .compile_policy = xfrm_compile_policy,
  2038. .notify_policy = xfrm_send_policy_notify,
  2039. .report = xfrm_send_report,
  2040. .migrate = xfrm_send_migrate,
  2041. };
  2042. static int __init xfrm_user_init(void)
  2043. {
  2044. struct sock *nlsk;
  2045. printk(KERN_INFO "Initializing XFRM netlink socket\n");
  2046. nlsk = netlink_kernel_create(&init_net, NETLINK_XFRM, XFRMNLGRP_MAX,
  2047. xfrm_netlink_rcv, NULL, THIS_MODULE);
  2048. if (nlsk == NULL)
  2049. return -ENOMEM;
  2050. rcu_assign_pointer(xfrm_nl, nlsk);
  2051. xfrm_register_km(&netlink_mgr);
  2052. return 0;
  2053. }
  2054. static void __exit xfrm_user_exit(void)
  2055. {
  2056. struct sock *nlsk = xfrm_nl;
  2057. xfrm_unregister_km(&netlink_mgr);
  2058. rcu_assign_pointer(xfrm_nl, NULL);
  2059. synchronize_rcu();
  2060. netlink_kernel_release(nlsk);
  2061. }
  2062. module_init(xfrm_user_init);
  2063. module_exit(xfrm_user_exit);
  2064. MODULE_LICENSE("GPL");
  2065. MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_XFRM);