esp4.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. #define pr_fmt(fmt) "IPsec: " fmt
  3. #include <crypto/aead.h>
  4. #include <crypto/authenc.h>
  5. #include <linux/err.h>
  6. #include <linux/module.h>
  7. #include <net/ip.h>
  8. #include <net/xfrm.h>
  9. #include <net/esp.h>
  10. #include <linux/scatterlist.h>
  11. #include <linux/kernel.h>
  12. #include <linux/pfkeyv2.h>
  13. #include <linux/rtnetlink.h>
  14. #include <linux/slab.h>
  15. #include <linux/spinlock.h>
  16. #include <linux/in6.h>
  17. #include <net/icmp.h>
  18. #include <net/protocol.h>
  19. #include <net/udp.h>
  20. #include <net/tcp.h>
  21. #include <net/espintcp.h>
  22. #include <linux/highmem.h>
  23. struct esp_skb_cb {
  24. struct xfrm_skb_cb xfrm;
  25. void *tmp;
  26. };
  27. struct esp_output_extra {
  28. __be32 seqhi;
  29. u32 esphoff;
  30. };
  31. #define ESP_SKB_CB(__skb) ((struct esp_skb_cb *)&((__skb)->cb[0]))
  32. /*
  33. * Allocate an AEAD request structure with extra space for SG and IV.
  34. *
  35. * For alignment considerations the IV is placed at the front, followed
  36. * by the request and finally the SG list.
  37. *
  38. * TODO: Use spare space in skb for this where possible.
  39. */
  40. static void *esp_alloc_tmp(struct crypto_aead *aead, int nfrags, int extralen)
  41. {
  42. unsigned int len;
  43. len = extralen;
  44. len += crypto_aead_ivsize(aead);
  45. if (len) {
  46. len += crypto_aead_alignmask(aead) &
  47. ~(crypto_tfm_ctx_alignment() - 1);
  48. len = ALIGN(len, crypto_tfm_ctx_alignment());
  49. }
  50. len += sizeof(struct aead_request) + crypto_aead_reqsize(aead);
  51. len = ALIGN(len, __alignof__(struct scatterlist));
  52. len += sizeof(struct scatterlist) * nfrags;
  53. return kmalloc(len, GFP_ATOMIC);
  54. }
  55. static inline void *esp_tmp_extra(void *tmp)
  56. {
  57. return PTR_ALIGN(tmp, __alignof__(struct esp_output_extra));
  58. }
  59. static inline u8 *esp_tmp_iv(struct crypto_aead *aead, void *tmp, int extralen)
  60. {
  61. return crypto_aead_ivsize(aead) ?
  62. PTR_ALIGN((u8 *)tmp + extralen,
  63. crypto_aead_alignmask(aead) + 1) : tmp + extralen;
  64. }
  65. static inline struct aead_request *esp_tmp_req(struct crypto_aead *aead, u8 *iv)
  66. {
  67. struct aead_request *req;
  68. req = (void *)PTR_ALIGN(iv + crypto_aead_ivsize(aead),
  69. crypto_tfm_ctx_alignment());
  70. aead_request_set_tfm(req, aead);
  71. return req;
  72. }
  73. static inline struct scatterlist *esp_req_sg(struct crypto_aead *aead,
  74. struct aead_request *req)
  75. {
  76. return (void *)ALIGN((unsigned long)(req + 1) +
  77. crypto_aead_reqsize(aead),
  78. __alignof__(struct scatterlist));
  79. }
  80. static void esp_ssg_unref(struct xfrm_state *x, void *tmp)
  81. {
  82. struct esp_output_extra *extra = esp_tmp_extra(tmp);
  83. struct crypto_aead *aead = x->data;
  84. int extralen = 0;
  85. u8 *iv;
  86. struct aead_request *req;
  87. struct scatterlist *sg;
  88. if (x->props.flags & XFRM_STATE_ESN)
  89. extralen += sizeof(*extra);
  90. extra = esp_tmp_extra(tmp);
  91. iv = esp_tmp_iv(aead, tmp, extralen);
  92. req = esp_tmp_req(aead, iv);
  93. /* Unref skb_frag_pages in the src scatterlist if necessary.
  94. * Skip the first sg which comes from skb->data.
  95. */
  96. if (req->src != req->dst)
  97. for (sg = sg_next(req->src); sg; sg = sg_next(sg))
  98. put_page(sg_page(sg));
  99. }
  100. #ifdef CONFIG_INET_ESPINTCP
  101. struct esp_tcp_sk {
  102. struct sock *sk;
  103. struct rcu_head rcu;
  104. };
  105. static void esp_free_tcp_sk(struct rcu_head *head)
  106. {
  107. struct esp_tcp_sk *esk = container_of(head, struct esp_tcp_sk, rcu);
  108. sock_put(esk->sk);
  109. kfree(esk);
  110. }
  111. static struct sock *esp_find_tcp_sk(struct xfrm_state *x)
  112. {
  113. struct xfrm_encap_tmpl *encap = x->encap;
  114. struct esp_tcp_sk *esk;
  115. __be16 sport, dport;
  116. struct sock *nsk;
  117. struct sock *sk;
  118. sk = rcu_dereference(x->encap_sk);
  119. if (sk && sk->sk_state == TCP_ESTABLISHED)
  120. return sk;
  121. spin_lock_bh(&x->lock);
  122. sport = encap->encap_sport;
  123. dport = encap->encap_dport;
  124. nsk = rcu_dereference_protected(x->encap_sk,
  125. lockdep_is_held(&x->lock));
  126. if (sk && sk == nsk) {
  127. esk = kmalloc(sizeof(*esk), GFP_ATOMIC);
  128. if (!esk) {
  129. spin_unlock_bh(&x->lock);
  130. return ERR_PTR(-ENOMEM);
  131. }
  132. RCU_INIT_POINTER(x->encap_sk, NULL);
  133. esk->sk = sk;
  134. call_rcu(&esk->rcu, esp_free_tcp_sk);
  135. }
  136. spin_unlock_bh(&x->lock);
  137. sk = inet_lookup_established(xs_net(x), &tcp_hashinfo, x->id.daddr.a4,
  138. dport, x->props.saddr.a4, sport, 0);
  139. if (!sk)
  140. return ERR_PTR(-ENOENT);
  141. if (!tcp_is_ulp_esp(sk)) {
  142. sock_put(sk);
  143. return ERR_PTR(-EINVAL);
  144. }
  145. spin_lock_bh(&x->lock);
  146. nsk = rcu_dereference_protected(x->encap_sk,
  147. lockdep_is_held(&x->lock));
  148. if (encap->encap_sport != sport ||
  149. encap->encap_dport != dport) {
  150. sock_put(sk);
  151. sk = nsk ?: ERR_PTR(-EREMCHG);
  152. } else if (sk == nsk) {
  153. sock_put(sk);
  154. } else {
  155. rcu_assign_pointer(x->encap_sk, sk);
  156. }
  157. spin_unlock_bh(&x->lock);
  158. return sk;
  159. }
  160. static int esp_output_tcp_finish(struct xfrm_state *x, struct sk_buff *skb)
  161. {
  162. struct sock *sk;
  163. int err;
  164. rcu_read_lock();
  165. sk = esp_find_tcp_sk(x);
  166. err = PTR_ERR_OR_ZERO(sk);
  167. if (err)
  168. goto out;
  169. bh_lock_sock(sk);
  170. if (sock_owned_by_user(sk))
  171. err = espintcp_queue_out(sk, skb);
  172. else
  173. err = espintcp_push_skb(sk, skb);
  174. bh_unlock_sock(sk);
  175. out:
  176. rcu_read_unlock();
  177. return err;
  178. }
  179. static int esp_output_tcp_encap_cb(struct net *net, struct sock *sk,
  180. struct sk_buff *skb)
  181. {
  182. struct dst_entry *dst = skb_dst(skb);
  183. struct xfrm_state *x = dst->xfrm;
  184. return esp_output_tcp_finish(x, skb);
  185. }
  186. static int esp_output_tail_tcp(struct xfrm_state *x, struct sk_buff *skb)
  187. {
  188. int err;
  189. local_bh_disable();
  190. err = xfrm_trans_queue_net(xs_net(x), skb, esp_output_tcp_encap_cb);
  191. local_bh_enable();
  192. /* EINPROGRESS just happens to do the right thing. It
  193. * actually means that the skb has been consumed and
  194. * isn't coming back.
  195. */
  196. return err ?: -EINPROGRESS;
  197. }
  198. #else
  199. static int esp_output_tail_tcp(struct xfrm_state *x, struct sk_buff *skb)
  200. {
  201. kfree_skb(skb);
  202. return -EOPNOTSUPP;
  203. }
  204. #endif
  205. static void esp_output_done(struct crypto_async_request *base, int err)
  206. {
  207. struct sk_buff *skb = base->data;
  208. struct xfrm_offload *xo = xfrm_offload(skb);
  209. void *tmp;
  210. struct xfrm_state *x;
  211. if (xo && (xo->flags & XFRM_DEV_RESUME)) {
  212. struct sec_path *sp = skb_sec_path(skb);
  213. x = sp->xvec[sp->len - 1];
  214. } else {
  215. x = skb_dst(skb)->xfrm;
  216. }
  217. tmp = ESP_SKB_CB(skb)->tmp;
  218. esp_ssg_unref(x, tmp);
  219. kfree(tmp);
  220. if (xo && (xo->flags & XFRM_DEV_RESUME)) {
  221. if (err) {
  222. XFRM_INC_STATS(xs_net(x), LINUX_MIB_XFRMOUTSTATEPROTOERROR);
  223. kfree_skb(skb);
  224. return;
  225. }
  226. skb_push(skb, skb->data - skb_mac_header(skb));
  227. secpath_reset(skb);
  228. xfrm_dev_resume(skb);
  229. } else {
  230. if (!err &&
  231. x->encap && x->encap->encap_type == TCP_ENCAP_ESPINTCP)
  232. esp_output_tail_tcp(x, skb);
  233. else
  234. xfrm_output_resume(skb, err);
  235. }
  236. }
  237. /* Move ESP header back into place. */
  238. static void esp_restore_header(struct sk_buff *skb, unsigned int offset)
  239. {
  240. struct ip_esp_hdr *esph = (void *)(skb->data + offset);
  241. void *tmp = ESP_SKB_CB(skb)->tmp;
  242. __be32 *seqhi = esp_tmp_extra(tmp);
  243. esph->seq_no = esph->spi;
  244. esph->spi = *seqhi;
  245. }
  246. static void esp_output_restore_header(struct sk_buff *skb)
  247. {
  248. void *tmp = ESP_SKB_CB(skb)->tmp;
  249. struct esp_output_extra *extra = esp_tmp_extra(tmp);
  250. esp_restore_header(skb, skb_transport_offset(skb) + extra->esphoff -
  251. sizeof(__be32));
  252. }
  253. static struct ip_esp_hdr *esp_output_set_extra(struct sk_buff *skb,
  254. struct xfrm_state *x,
  255. struct ip_esp_hdr *esph,
  256. struct esp_output_extra *extra)
  257. {
  258. /* For ESN we move the header forward by 4 bytes to
  259. * accomodate the high bits. We will move it back after
  260. * encryption.
  261. */
  262. if ((x->props.flags & XFRM_STATE_ESN)) {
  263. __u32 seqhi;
  264. struct xfrm_offload *xo = xfrm_offload(skb);
  265. if (xo)
  266. seqhi = xo->seq.hi;
  267. else
  268. seqhi = XFRM_SKB_CB(skb)->seq.output.hi;
  269. extra->esphoff = (unsigned char *)esph -
  270. skb_transport_header(skb);
  271. esph = (struct ip_esp_hdr *)((unsigned char *)esph - 4);
  272. extra->seqhi = esph->spi;
  273. esph->seq_no = htonl(seqhi);
  274. }
  275. esph->spi = x->id.spi;
  276. return esph;
  277. }
  278. static void esp_output_done_esn(struct crypto_async_request *base, int err)
  279. {
  280. struct sk_buff *skb = base->data;
  281. esp_output_restore_header(skb);
  282. esp_output_done(base, err);
  283. }
  284. static struct ip_esp_hdr *esp_output_udp_encap(struct sk_buff *skb,
  285. int encap_type,
  286. struct esp_info *esp,
  287. __be16 sport,
  288. __be16 dport)
  289. {
  290. struct udphdr *uh;
  291. __be32 *udpdata32;
  292. unsigned int len;
  293. len = skb->len + esp->tailen - skb_transport_offset(skb);
  294. if (len + sizeof(struct iphdr) > IP_MAX_MTU)
  295. return ERR_PTR(-EMSGSIZE);
  296. uh = (struct udphdr *)esp->esph;
  297. uh->source = sport;
  298. uh->dest = dport;
  299. uh->len = htons(len);
  300. uh->check = 0;
  301. *skb_mac_header(skb) = IPPROTO_UDP;
  302. if (encap_type == UDP_ENCAP_ESPINUDP_NON_IKE) {
  303. udpdata32 = (__be32 *)(uh + 1);
  304. udpdata32[0] = udpdata32[1] = 0;
  305. return (struct ip_esp_hdr *)(udpdata32 + 2);
  306. }
  307. return (struct ip_esp_hdr *)(uh + 1);
  308. }
  309. #ifdef CONFIG_INET_ESPINTCP
  310. static struct ip_esp_hdr *esp_output_tcp_encap(struct xfrm_state *x,
  311. struct sk_buff *skb,
  312. struct esp_info *esp)
  313. {
  314. __be16 *lenp = (void *)esp->esph;
  315. struct ip_esp_hdr *esph;
  316. unsigned int len;
  317. struct sock *sk;
  318. len = skb->len + esp->tailen - skb_transport_offset(skb);
  319. if (len > IP_MAX_MTU)
  320. return ERR_PTR(-EMSGSIZE);
  321. rcu_read_lock();
  322. sk = esp_find_tcp_sk(x);
  323. rcu_read_unlock();
  324. if (IS_ERR(sk))
  325. return ERR_CAST(sk);
  326. *lenp = htons(len);
  327. esph = (struct ip_esp_hdr *)(lenp + 1);
  328. return esph;
  329. }
  330. #else
  331. static struct ip_esp_hdr *esp_output_tcp_encap(struct xfrm_state *x,
  332. struct sk_buff *skb,
  333. struct esp_info *esp)
  334. {
  335. return ERR_PTR(-EOPNOTSUPP);
  336. }
  337. #endif
  338. static int esp_output_encap(struct xfrm_state *x, struct sk_buff *skb,
  339. struct esp_info *esp)
  340. {
  341. struct xfrm_encap_tmpl *encap = x->encap;
  342. struct ip_esp_hdr *esph;
  343. __be16 sport, dport;
  344. int encap_type;
  345. spin_lock_bh(&x->lock);
  346. sport = encap->encap_sport;
  347. dport = encap->encap_dport;
  348. encap_type = encap->encap_type;
  349. spin_unlock_bh(&x->lock);
  350. switch (encap_type) {
  351. default:
  352. case UDP_ENCAP_ESPINUDP:
  353. case UDP_ENCAP_ESPINUDP_NON_IKE:
  354. esph = esp_output_udp_encap(skb, encap_type, esp, sport, dport);
  355. break;
  356. case TCP_ENCAP_ESPINTCP:
  357. esph = esp_output_tcp_encap(x, skb, esp);
  358. break;
  359. }
  360. if (IS_ERR(esph))
  361. return PTR_ERR(esph);
  362. esp->esph = esph;
  363. return 0;
  364. }
  365. int esp_output_head(struct xfrm_state *x, struct sk_buff *skb, struct esp_info *esp)
  366. {
  367. u8 *tail;
  368. int nfrags;
  369. int esph_offset;
  370. struct page *page;
  371. struct sk_buff *trailer;
  372. int tailen = esp->tailen;
  373. /* this is non-NULL only with TCP/UDP Encapsulation */
  374. if (x->encap) {
  375. int err = esp_output_encap(x, skb, esp);
  376. if (err < 0)
  377. return err;
  378. }
  379. if (ALIGN(tailen, L1_CACHE_BYTES) > PAGE_SIZE ||
  380. ALIGN(skb->data_len, L1_CACHE_BYTES) > PAGE_SIZE)
  381. goto cow;
  382. if (!skb_cloned(skb)) {
  383. if (tailen <= skb_tailroom(skb)) {
  384. nfrags = 1;
  385. trailer = skb;
  386. tail = skb_tail_pointer(trailer);
  387. goto skip_cow;
  388. } else if ((skb_shinfo(skb)->nr_frags < MAX_SKB_FRAGS)
  389. && !skb_has_frag_list(skb)) {
  390. int allocsize;
  391. struct sock *sk = skb->sk;
  392. struct page_frag *pfrag = &x->xfrag;
  393. esp->inplace = false;
  394. allocsize = ALIGN(tailen, L1_CACHE_BYTES);
  395. spin_lock_bh(&x->lock);
  396. if (unlikely(!skb_page_frag_refill(allocsize, pfrag, GFP_ATOMIC))) {
  397. spin_unlock_bh(&x->lock);
  398. goto cow;
  399. }
  400. page = pfrag->page;
  401. get_page(page);
  402. tail = page_address(page) + pfrag->offset;
  403. esp_output_fill_trailer(tail, esp->tfclen, esp->plen, esp->proto);
  404. nfrags = skb_shinfo(skb)->nr_frags;
  405. __skb_fill_page_desc(skb, nfrags, page, pfrag->offset,
  406. tailen);
  407. skb_shinfo(skb)->nr_frags = ++nfrags;
  408. pfrag->offset = pfrag->offset + allocsize;
  409. spin_unlock_bh(&x->lock);
  410. nfrags++;
  411. skb->len += tailen;
  412. skb->data_len += tailen;
  413. skb->truesize += tailen;
  414. if (sk && sk_fullsock(sk))
  415. refcount_add(tailen, &sk->sk_wmem_alloc);
  416. goto out;
  417. }
  418. }
  419. cow:
  420. esph_offset = (unsigned char *)esp->esph - skb_transport_header(skb);
  421. nfrags = skb_cow_data(skb, tailen, &trailer);
  422. if (nfrags < 0)
  423. goto out;
  424. tail = skb_tail_pointer(trailer);
  425. esp->esph = (struct ip_esp_hdr *)(skb_transport_header(skb) + esph_offset);
  426. skip_cow:
  427. esp_output_fill_trailer(tail, esp->tfclen, esp->plen, esp->proto);
  428. pskb_put(skb, trailer, tailen);
  429. out:
  430. return nfrags;
  431. }
  432. EXPORT_SYMBOL_GPL(esp_output_head);
  433. int esp_output_tail(struct xfrm_state *x, struct sk_buff *skb, struct esp_info *esp)
  434. {
  435. u8 *iv;
  436. int alen;
  437. void *tmp;
  438. int ivlen;
  439. int assoclen;
  440. int extralen;
  441. struct page *page;
  442. struct ip_esp_hdr *esph;
  443. struct crypto_aead *aead;
  444. struct aead_request *req;
  445. struct scatterlist *sg, *dsg;
  446. struct esp_output_extra *extra;
  447. int err = -ENOMEM;
  448. assoclen = sizeof(struct ip_esp_hdr);
  449. extralen = 0;
  450. if (x->props.flags & XFRM_STATE_ESN) {
  451. extralen += sizeof(*extra);
  452. assoclen += sizeof(__be32);
  453. }
  454. aead = x->data;
  455. alen = crypto_aead_authsize(aead);
  456. ivlen = crypto_aead_ivsize(aead);
  457. tmp = esp_alloc_tmp(aead, esp->nfrags + 2, extralen);
  458. if (!tmp)
  459. goto error;
  460. extra = esp_tmp_extra(tmp);
  461. iv = esp_tmp_iv(aead, tmp, extralen);
  462. req = esp_tmp_req(aead, iv);
  463. sg = esp_req_sg(aead, req);
  464. if (esp->inplace)
  465. dsg = sg;
  466. else
  467. dsg = &sg[esp->nfrags];
  468. esph = esp_output_set_extra(skb, x, esp->esph, extra);
  469. esp->esph = esph;
  470. sg_init_table(sg, esp->nfrags);
  471. err = skb_to_sgvec(skb, sg,
  472. (unsigned char *)esph - skb->data,
  473. assoclen + ivlen + esp->clen + alen);
  474. if (unlikely(err < 0))
  475. goto error_free;
  476. if (!esp->inplace) {
  477. int allocsize;
  478. struct page_frag *pfrag = &x->xfrag;
  479. allocsize = ALIGN(skb->data_len, L1_CACHE_BYTES);
  480. spin_lock_bh(&x->lock);
  481. if (unlikely(!skb_page_frag_refill(allocsize, pfrag, GFP_ATOMIC))) {
  482. spin_unlock_bh(&x->lock);
  483. goto error_free;
  484. }
  485. skb_shinfo(skb)->nr_frags = 1;
  486. page = pfrag->page;
  487. get_page(page);
  488. /* replace page frags in skb with new page */
  489. __skb_fill_page_desc(skb, 0, page, pfrag->offset, skb->data_len);
  490. pfrag->offset = pfrag->offset + allocsize;
  491. spin_unlock_bh(&x->lock);
  492. sg_init_table(dsg, skb_shinfo(skb)->nr_frags + 1);
  493. err = skb_to_sgvec(skb, dsg,
  494. (unsigned char *)esph - skb->data,
  495. assoclen + ivlen + esp->clen + alen);
  496. if (unlikely(err < 0))
  497. goto error_free;
  498. }
  499. if ((x->props.flags & XFRM_STATE_ESN))
  500. aead_request_set_callback(req, 0, esp_output_done_esn, skb);
  501. else
  502. aead_request_set_callback(req, 0, esp_output_done, skb);
  503. aead_request_set_crypt(req, sg, dsg, ivlen + esp->clen, iv);
  504. aead_request_set_ad(req, assoclen);
  505. memset(iv, 0, ivlen);
  506. memcpy(iv + ivlen - min(ivlen, 8), (u8 *)&esp->seqno + 8 - min(ivlen, 8),
  507. min(ivlen, 8));
  508. ESP_SKB_CB(skb)->tmp = tmp;
  509. err = crypto_aead_encrypt(req);
  510. switch (err) {
  511. case -EINPROGRESS:
  512. goto error;
  513. case -ENOSPC:
  514. err = NET_XMIT_DROP;
  515. break;
  516. case 0:
  517. if ((x->props.flags & XFRM_STATE_ESN))
  518. esp_output_restore_header(skb);
  519. }
  520. if (sg != dsg)
  521. esp_ssg_unref(x, tmp);
  522. if (!err && x->encap && x->encap->encap_type == TCP_ENCAP_ESPINTCP)
  523. err = esp_output_tail_tcp(x, skb);
  524. error_free:
  525. kfree(tmp);
  526. error:
  527. return err;
  528. }
  529. EXPORT_SYMBOL_GPL(esp_output_tail);
  530. static int esp_output(struct xfrm_state *x, struct sk_buff *skb)
  531. {
  532. int alen;
  533. int blksize;
  534. struct ip_esp_hdr *esph;
  535. struct crypto_aead *aead;
  536. struct esp_info esp;
  537. esp.inplace = true;
  538. esp.proto = *skb_mac_header(skb);
  539. *skb_mac_header(skb) = IPPROTO_ESP;
  540. /* skb is pure payload to encrypt */
  541. aead = x->data;
  542. alen = crypto_aead_authsize(aead);
  543. esp.tfclen = 0;
  544. if (x->tfcpad) {
  545. struct xfrm_dst *dst = (struct xfrm_dst *)skb_dst(skb);
  546. u32 padto;
  547. padto = min(x->tfcpad, xfrm_state_mtu(x, dst->child_mtu_cached));
  548. if (skb->len < padto)
  549. esp.tfclen = padto - skb->len;
  550. }
  551. blksize = ALIGN(crypto_aead_blocksize(aead), 4);
  552. esp.clen = ALIGN(skb->len + 2 + esp.tfclen, blksize);
  553. esp.plen = esp.clen - skb->len - esp.tfclen;
  554. esp.tailen = esp.tfclen + esp.plen + alen;
  555. esp.esph = ip_esp_hdr(skb);
  556. esp.nfrags = esp_output_head(x, skb, &esp);
  557. if (esp.nfrags < 0)
  558. return esp.nfrags;
  559. esph = esp.esph;
  560. esph->spi = x->id.spi;
  561. esph->seq_no = htonl(XFRM_SKB_CB(skb)->seq.output.low);
  562. esp.seqno = cpu_to_be64(XFRM_SKB_CB(skb)->seq.output.low +
  563. ((u64)XFRM_SKB_CB(skb)->seq.output.hi << 32));
  564. skb_push(skb, -skb_network_offset(skb));
  565. return esp_output_tail(x, skb, &esp);
  566. }
  567. static inline int esp_remove_trailer(struct sk_buff *skb)
  568. {
  569. struct xfrm_state *x = xfrm_input_state(skb);
  570. struct xfrm_offload *xo = xfrm_offload(skb);
  571. struct crypto_aead *aead = x->data;
  572. int alen, hlen, elen;
  573. int padlen, trimlen;
  574. __wsum csumdiff;
  575. u8 nexthdr[2];
  576. int ret;
  577. alen = crypto_aead_authsize(aead);
  578. hlen = sizeof(struct ip_esp_hdr) + crypto_aead_ivsize(aead);
  579. elen = skb->len - hlen;
  580. if (xo && (xo->flags & XFRM_ESP_NO_TRAILER)) {
  581. ret = xo->proto;
  582. goto out;
  583. }
  584. if (skb_copy_bits(skb, skb->len - alen - 2, nexthdr, 2))
  585. BUG();
  586. ret = -EINVAL;
  587. padlen = nexthdr[0];
  588. if (padlen + 2 + alen >= elen) {
  589. net_dbg_ratelimited("ipsec esp packet is garbage padlen=%d, elen=%d\n",
  590. padlen + 2, elen - alen);
  591. goto out;
  592. }
  593. trimlen = alen + padlen + 2;
  594. if (skb->ip_summed == CHECKSUM_COMPLETE) {
  595. csumdiff = skb_checksum(skb, skb->len - trimlen, trimlen, 0);
  596. skb->csum = csum_block_sub(skb->csum, csumdiff,
  597. skb->len - trimlen);
  598. }
  599. pskb_trim(skb, skb->len - trimlen);
  600. ret = nexthdr[1];
  601. out:
  602. return ret;
  603. }
  604. int esp_input_done2(struct sk_buff *skb, int err)
  605. {
  606. const struct iphdr *iph;
  607. struct xfrm_state *x = xfrm_input_state(skb);
  608. struct xfrm_offload *xo = xfrm_offload(skb);
  609. struct crypto_aead *aead = x->data;
  610. int hlen = sizeof(struct ip_esp_hdr) + crypto_aead_ivsize(aead);
  611. int ihl;
  612. if (!xo || (xo && !(xo->flags & CRYPTO_DONE)))
  613. kfree(ESP_SKB_CB(skb)->tmp);
  614. if (unlikely(err))
  615. goto out;
  616. err = esp_remove_trailer(skb);
  617. if (unlikely(err < 0))
  618. goto out;
  619. iph = ip_hdr(skb);
  620. ihl = iph->ihl * 4;
  621. if (x->encap) {
  622. struct xfrm_encap_tmpl *encap = x->encap;
  623. struct tcphdr *th = (void *)(skb_network_header(skb) + ihl);
  624. struct udphdr *uh = (void *)(skb_network_header(skb) + ihl);
  625. __be16 source;
  626. switch (x->encap->encap_type) {
  627. case TCP_ENCAP_ESPINTCP:
  628. source = th->source;
  629. break;
  630. case UDP_ENCAP_ESPINUDP:
  631. case UDP_ENCAP_ESPINUDP_NON_IKE:
  632. source = uh->source;
  633. break;
  634. default:
  635. WARN_ON_ONCE(1);
  636. err = -EINVAL;
  637. goto out;
  638. }
  639. /*
  640. * 1) if the NAT-T peer's IP or port changed then
  641. * advertize the change to the keying daemon.
  642. * This is an inbound SA, so just compare
  643. * SRC ports.
  644. */
  645. if (iph->saddr != x->props.saddr.a4 ||
  646. source != encap->encap_sport) {
  647. xfrm_address_t ipaddr;
  648. ipaddr.a4 = iph->saddr;
  649. km_new_mapping(x, &ipaddr, source);
  650. /* XXX: perhaps add an extra
  651. * policy check here, to see
  652. * if we should allow or
  653. * reject a packet from a
  654. * different source
  655. * address/port.
  656. */
  657. }
  658. /*
  659. * 2) ignore UDP/TCP checksums in case
  660. * of NAT-T in Transport Mode, or
  661. * perform other post-processing fixes
  662. * as per draft-ietf-ipsec-udp-encaps-06,
  663. * section 3.1.2
  664. */
  665. if (x->props.mode == XFRM_MODE_TRANSPORT)
  666. skb->ip_summed = CHECKSUM_UNNECESSARY;
  667. }
  668. skb_pull_rcsum(skb, hlen);
  669. if (x->props.mode == XFRM_MODE_TUNNEL)
  670. skb_reset_transport_header(skb);
  671. else
  672. skb_set_transport_header(skb, -ihl);
  673. /* RFC4303: Drop dummy packets without any error */
  674. if (err == IPPROTO_NONE)
  675. err = -EINVAL;
  676. out:
  677. return err;
  678. }
  679. EXPORT_SYMBOL_GPL(esp_input_done2);
  680. static void esp_input_done(struct crypto_async_request *base, int err)
  681. {
  682. struct sk_buff *skb = base->data;
  683. xfrm_input_resume(skb, esp_input_done2(skb, err));
  684. }
  685. static void esp_input_restore_header(struct sk_buff *skb)
  686. {
  687. esp_restore_header(skb, 0);
  688. __skb_pull(skb, 4);
  689. }
  690. static void esp_input_set_header(struct sk_buff *skb, __be32 *seqhi)
  691. {
  692. struct xfrm_state *x = xfrm_input_state(skb);
  693. struct ip_esp_hdr *esph;
  694. /* For ESN we move the header forward by 4 bytes to
  695. * accomodate the high bits. We will move it back after
  696. * decryption.
  697. */
  698. if ((x->props.flags & XFRM_STATE_ESN)) {
  699. esph = skb_push(skb, 4);
  700. *seqhi = esph->spi;
  701. esph->spi = esph->seq_no;
  702. esph->seq_no = XFRM_SKB_CB(skb)->seq.input.hi;
  703. }
  704. }
  705. static void esp_input_done_esn(struct crypto_async_request *base, int err)
  706. {
  707. struct sk_buff *skb = base->data;
  708. esp_input_restore_header(skb);
  709. esp_input_done(base, err);
  710. }
  711. /*
  712. * Note: detecting truncated vs. non-truncated authentication data is very
  713. * expensive, so we only support truncated data, which is the recommended
  714. * and common case.
  715. */
  716. static int esp_input(struct xfrm_state *x, struct sk_buff *skb)
  717. {
  718. struct crypto_aead *aead = x->data;
  719. struct aead_request *req;
  720. struct sk_buff *trailer;
  721. int ivlen = crypto_aead_ivsize(aead);
  722. int elen = skb->len - sizeof(struct ip_esp_hdr) - ivlen;
  723. int nfrags;
  724. int assoclen;
  725. int seqhilen;
  726. __be32 *seqhi;
  727. void *tmp;
  728. u8 *iv;
  729. struct scatterlist *sg;
  730. int err = -EINVAL;
  731. if (!pskb_may_pull(skb, sizeof(struct ip_esp_hdr) + ivlen))
  732. goto out;
  733. if (elen <= 0)
  734. goto out;
  735. assoclen = sizeof(struct ip_esp_hdr);
  736. seqhilen = 0;
  737. if (x->props.flags & XFRM_STATE_ESN) {
  738. seqhilen += sizeof(__be32);
  739. assoclen += seqhilen;
  740. }
  741. if (!skb_cloned(skb)) {
  742. if (!skb_is_nonlinear(skb)) {
  743. nfrags = 1;
  744. goto skip_cow;
  745. } else if (!skb_has_frag_list(skb)) {
  746. nfrags = skb_shinfo(skb)->nr_frags;
  747. nfrags++;
  748. goto skip_cow;
  749. }
  750. }
  751. err = skb_cow_data(skb, 0, &trailer);
  752. if (err < 0)
  753. goto out;
  754. nfrags = err;
  755. skip_cow:
  756. err = -ENOMEM;
  757. tmp = esp_alloc_tmp(aead, nfrags, seqhilen);
  758. if (!tmp)
  759. goto out;
  760. ESP_SKB_CB(skb)->tmp = tmp;
  761. seqhi = esp_tmp_extra(tmp);
  762. iv = esp_tmp_iv(aead, tmp, seqhilen);
  763. req = esp_tmp_req(aead, iv);
  764. sg = esp_req_sg(aead, req);
  765. esp_input_set_header(skb, seqhi);
  766. sg_init_table(sg, nfrags);
  767. err = skb_to_sgvec(skb, sg, 0, skb->len);
  768. if (unlikely(err < 0)) {
  769. kfree(tmp);
  770. goto out;
  771. }
  772. skb->ip_summed = CHECKSUM_NONE;
  773. if ((x->props.flags & XFRM_STATE_ESN))
  774. aead_request_set_callback(req, 0, esp_input_done_esn, skb);
  775. else
  776. aead_request_set_callback(req, 0, esp_input_done, skb);
  777. aead_request_set_crypt(req, sg, sg, elen + ivlen, iv);
  778. aead_request_set_ad(req, assoclen);
  779. err = crypto_aead_decrypt(req);
  780. if (err == -EINPROGRESS)
  781. goto out;
  782. if ((x->props.flags & XFRM_STATE_ESN))
  783. esp_input_restore_header(skb);
  784. err = esp_input_done2(skb, err);
  785. out:
  786. return err;
  787. }
  788. static int esp4_err(struct sk_buff *skb, u32 info)
  789. {
  790. struct net *net = dev_net(skb->dev);
  791. const struct iphdr *iph = (const struct iphdr *)skb->data;
  792. struct ip_esp_hdr *esph = (struct ip_esp_hdr *)(skb->data+(iph->ihl<<2));
  793. struct xfrm_state *x;
  794. switch (icmp_hdr(skb)->type) {
  795. case ICMP_DEST_UNREACH:
  796. if (icmp_hdr(skb)->code != ICMP_FRAG_NEEDED)
  797. return 0;
  798. case ICMP_REDIRECT:
  799. break;
  800. default:
  801. return 0;
  802. }
  803. x = xfrm_state_lookup(net, skb->mark, (const xfrm_address_t *)&iph->daddr,
  804. esph->spi, IPPROTO_ESP, AF_INET);
  805. if (!x)
  806. return 0;
  807. if (icmp_hdr(skb)->type == ICMP_DEST_UNREACH)
  808. ipv4_update_pmtu(skb, net, info, 0, IPPROTO_ESP);
  809. else
  810. ipv4_redirect(skb, net, 0, IPPROTO_ESP);
  811. xfrm_state_put(x);
  812. return 0;
  813. }
  814. static void esp_destroy(struct xfrm_state *x)
  815. {
  816. struct crypto_aead *aead = x->data;
  817. if (!aead)
  818. return;
  819. crypto_free_aead(aead);
  820. }
  821. static int esp_init_aead(struct xfrm_state *x)
  822. {
  823. char aead_name[CRYPTO_MAX_ALG_NAME];
  824. struct crypto_aead *aead;
  825. int err;
  826. err = -ENAMETOOLONG;
  827. if (snprintf(aead_name, CRYPTO_MAX_ALG_NAME, "%s(%s)",
  828. x->geniv, x->aead->alg_name) >= CRYPTO_MAX_ALG_NAME)
  829. goto error;
  830. aead = crypto_alloc_aead(aead_name, 0, 0);
  831. err = PTR_ERR(aead);
  832. if (IS_ERR(aead))
  833. goto error;
  834. x->data = aead;
  835. err = crypto_aead_setkey(aead, x->aead->alg_key,
  836. (x->aead->alg_key_len + 7) / 8);
  837. if (err)
  838. goto error;
  839. err = crypto_aead_setauthsize(aead, x->aead->alg_icv_len / 8);
  840. if (err)
  841. goto error;
  842. error:
  843. return err;
  844. }
  845. static int esp_init_authenc(struct xfrm_state *x)
  846. {
  847. struct crypto_aead *aead;
  848. struct crypto_authenc_key_param *param;
  849. struct rtattr *rta;
  850. char *key;
  851. char *p;
  852. char authenc_name[CRYPTO_MAX_ALG_NAME];
  853. unsigned int keylen;
  854. int err;
  855. err = -EINVAL;
  856. if (!x->ealg)
  857. goto error;
  858. err = -ENAMETOOLONG;
  859. if ((x->props.flags & XFRM_STATE_ESN)) {
  860. if (snprintf(authenc_name, CRYPTO_MAX_ALG_NAME,
  861. "%s%sauthencesn(%s,%s)%s",
  862. x->geniv ?: "", x->geniv ? "(" : "",
  863. x->aalg ? x->aalg->alg_name : "digest_null",
  864. x->ealg->alg_name,
  865. x->geniv ? ")" : "") >= CRYPTO_MAX_ALG_NAME)
  866. goto error;
  867. } else {
  868. if (snprintf(authenc_name, CRYPTO_MAX_ALG_NAME,
  869. "%s%sauthenc(%s,%s)%s",
  870. x->geniv ?: "", x->geniv ? "(" : "",
  871. x->aalg ? x->aalg->alg_name : "digest_null",
  872. x->ealg->alg_name,
  873. x->geniv ? ")" : "") >= CRYPTO_MAX_ALG_NAME)
  874. goto error;
  875. }
  876. aead = crypto_alloc_aead(authenc_name, 0, 0);
  877. err = PTR_ERR(aead);
  878. if (IS_ERR(aead))
  879. goto error;
  880. x->data = aead;
  881. keylen = (x->aalg ? (x->aalg->alg_key_len + 7) / 8 : 0) +
  882. (x->ealg->alg_key_len + 7) / 8 + RTA_SPACE(sizeof(*param));
  883. err = -ENOMEM;
  884. key = kmalloc(keylen, GFP_KERNEL);
  885. if (!key)
  886. goto error;
  887. p = key;
  888. rta = (void *)p;
  889. rta->rta_type = CRYPTO_AUTHENC_KEYA_PARAM;
  890. rta->rta_len = RTA_LENGTH(sizeof(*param));
  891. param = RTA_DATA(rta);
  892. p += RTA_SPACE(sizeof(*param));
  893. if (x->aalg) {
  894. struct xfrm_algo_desc *aalg_desc;
  895. memcpy(p, x->aalg->alg_key, (x->aalg->alg_key_len + 7) / 8);
  896. p += (x->aalg->alg_key_len + 7) / 8;
  897. aalg_desc = xfrm_aalg_get_byname(x->aalg->alg_name, 0);
  898. BUG_ON(!aalg_desc);
  899. err = -EINVAL;
  900. if (aalg_desc->uinfo.auth.icv_fullbits / 8 !=
  901. crypto_aead_authsize(aead)) {
  902. pr_info("ESP: %s digestsize %u != %hu\n",
  903. x->aalg->alg_name,
  904. crypto_aead_authsize(aead),
  905. aalg_desc->uinfo.auth.icv_fullbits / 8);
  906. goto free_key;
  907. }
  908. err = crypto_aead_setauthsize(
  909. aead, x->aalg->alg_trunc_len / 8);
  910. if (err)
  911. goto free_key;
  912. }
  913. param->enckeylen = cpu_to_be32((x->ealg->alg_key_len + 7) / 8);
  914. memcpy(p, x->ealg->alg_key, (x->ealg->alg_key_len + 7) / 8);
  915. err = crypto_aead_setkey(aead, key, keylen);
  916. free_key:
  917. kfree(key);
  918. error:
  919. return err;
  920. }
  921. static int esp_init_state(struct xfrm_state *x)
  922. {
  923. struct crypto_aead *aead;
  924. u32 align;
  925. int err;
  926. x->data = NULL;
  927. if (x->aead)
  928. err = esp_init_aead(x);
  929. else
  930. err = esp_init_authenc(x);
  931. if (err)
  932. goto error;
  933. aead = x->data;
  934. x->props.header_len = sizeof(struct ip_esp_hdr) +
  935. crypto_aead_ivsize(aead);
  936. if (x->props.mode == XFRM_MODE_TUNNEL)
  937. x->props.header_len += sizeof(struct iphdr);
  938. else if (x->props.mode == XFRM_MODE_BEET && x->sel.family != AF_INET6)
  939. x->props.header_len += IPV4_BEET_PHMAXLEN;
  940. if (x->encap) {
  941. struct xfrm_encap_tmpl *encap = x->encap;
  942. switch (encap->encap_type) {
  943. default:
  944. err = -EINVAL;
  945. goto error;
  946. case UDP_ENCAP_ESPINUDP:
  947. x->props.header_len += sizeof(struct udphdr);
  948. break;
  949. case UDP_ENCAP_ESPINUDP_NON_IKE:
  950. x->props.header_len += sizeof(struct udphdr) + 2 * sizeof(u32);
  951. break;
  952. #ifdef CONFIG_INET_ESPINTCP
  953. case TCP_ENCAP_ESPINTCP:
  954. /* only the length field, TCP encap is done by
  955. * the socket
  956. */
  957. x->props.header_len += 2;
  958. break;
  959. #endif
  960. }
  961. }
  962. align = ALIGN(crypto_aead_blocksize(aead), 4);
  963. x->props.trailer_len = align + 1 + crypto_aead_authsize(aead);
  964. error:
  965. return err;
  966. }
  967. static int esp4_rcv_cb(struct sk_buff *skb, int err)
  968. {
  969. return 0;
  970. }
  971. static const struct xfrm_type esp_type =
  972. {
  973. .description = "ESP4",
  974. .owner = THIS_MODULE,
  975. .proto = IPPROTO_ESP,
  976. .flags = XFRM_TYPE_REPLAY_PROT,
  977. .init_state = esp_init_state,
  978. .destructor = esp_destroy,
  979. .input = esp_input,
  980. .output = esp_output,
  981. };
  982. static struct xfrm4_protocol esp4_protocol = {
  983. .handler = xfrm4_rcv,
  984. .input_handler = xfrm_input,
  985. .cb_handler = esp4_rcv_cb,
  986. .err_handler = esp4_err,
  987. .priority = 0,
  988. };
  989. static int __init esp4_init(void)
  990. {
  991. if (xfrm_register_type(&esp_type, AF_INET) < 0) {
  992. pr_info("%s: can't add xfrm type\n", __func__);
  993. return -EAGAIN;
  994. }
  995. if (xfrm4_protocol_register(&esp4_protocol, IPPROTO_ESP) < 0) {
  996. pr_info("%s: can't add protocol\n", __func__);
  997. xfrm_unregister_type(&esp_type, AF_INET);
  998. return -EAGAIN;
  999. }
  1000. return 0;
  1001. }
  1002. static void __exit esp4_fini(void)
  1003. {
  1004. if (xfrm4_protocol_deregister(&esp4_protocol, IPPROTO_ESP) < 0)
  1005. pr_info("%s: can't remove protocol\n", __func__);
  1006. xfrm_unregister_type(&esp_type, AF_INET);
  1007. }
  1008. module_init(esp4_init);
  1009. module_exit(esp4_fini);
  1010. MODULE_LICENSE("GPL");
  1011. MODULE_ALIAS_XFRM_TYPE(AF_INET, XFRM_PROTO_ESP);