xfrm_user.c 55 KB

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