xfrm_user.c 56 KB

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