esp4.c 27 KB

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