xfrm_user.c 55 KB

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