xfrm_user.c 57 KB

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