xfrm_iptfs.c 78 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* xfrm_iptfs: IPTFS encapsulation support
  3. *
  4. * April 21 2022, Christian Hopps <chopps@labn.net>
  5. *
  6. * Copyright (c) 2022, LabN Consulting, L.L.C.
  7. *
  8. */
  9. #include <linux/kernel.h>
  10. #include <linux/icmpv6.h>
  11. #include <linux/skbuff_ref.h>
  12. #include <net/gro.h>
  13. #include <net/icmp.h>
  14. #include <net/ip6_route.h>
  15. #include <net/inet_ecn.h>
  16. #include <net/xfrm.h>
  17. #include <crypto/aead.h>
  18. #include "xfrm_inout.h"
  19. #include "trace_iptfs.h"
  20. /* IPTFS encap (header) values. */
  21. #define IPTFS_SUBTYPE_BASIC 0
  22. #define IPTFS_SUBTYPE_CC 1
  23. /* ----------------------------------------------- */
  24. /* IP-TFS default SA values (tunnel egress/dir-in) */
  25. /* ----------------------------------------------- */
  26. /**
  27. * define IPTFS_DEFAULT_DROP_TIME_USECS - default drop time
  28. *
  29. * The default IPTFS drop time in microseconds. The drop time is the amount of
  30. * time before a missing out-of-order IPTFS tunnel packet is considered lost.
  31. * See also the reorder window.
  32. *
  33. * Default 1s.
  34. */
  35. #define IPTFS_DEFAULT_DROP_TIME_USECS 1000000
  36. /**
  37. * define IPTFS_DEFAULT_REORDER_WINDOW - default reorder window size
  38. *
  39. * The default IPTFS reorder window size. The reorder window size dictates the
  40. * maximum number of IPTFS tunnel packets in a sequence that may arrive out of
  41. * order.
  42. *
  43. * Default 3. (tcp folks suggested)
  44. */
  45. #define IPTFS_DEFAULT_REORDER_WINDOW 3
  46. /* ------------------------------------------------ */
  47. /* IPTFS default SA values (tunnel ingress/dir-out) */
  48. /* ------------------------------------------------ */
  49. /**
  50. * define IPTFS_DEFAULT_INIT_DELAY_USECS - default initial output delay
  51. *
  52. * The initial output delay is the amount of time prior to servicing the output
  53. * queue after queueing the first packet on said queue. This applies anytime the
  54. * output queue was previously empty.
  55. *
  56. * Default 0.
  57. */
  58. #define IPTFS_DEFAULT_INIT_DELAY_USECS 0
  59. /**
  60. * define IPTFS_DEFAULT_MAX_QUEUE_SIZE - default max output queue size.
  61. *
  62. * The default IPTFS max output queue size in octets. The output queue is where
  63. * received packets destined for output over an IPTFS tunnel are stored prior to
  64. * being output in aggregated/fragmented form over the IPTFS tunnel.
  65. *
  66. * Default 1M.
  67. */
  68. #define IPTFS_DEFAULT_MAX_QUEUE_SIZE (1024 * 10240)
  69. /* Assumed: skb->head is cache aligned.
  70. *
  71. * L2 Header resv: Arrange for cacheline to start at skb->data - 16 to keep the
  72. * to-be-pushed L2 header in the same cacheline as resulting `skb->data` (i.e.,
  73. * the L3 header). If cacheline size is > 64 then skb->data + pushed L2 will all
  74. * be in a single cacheline if we simply reserve 64 bytes.
  75. *
  76. * L3 Header resv: For L3+L2 headers (i.e., skb->data points at the IPTFS payload)
  77. * we want `skb->data` to be cacheline aligned and all pushed L2L3 headers will
  78. * be in their own cacheline[s]. 128 works for cachelins up to 128 bytes, for
  79. * any larger cacheline sizes the pushed headers will simply share the cacheline
  80. * with the start of the IPTFS payload (skb->data).
  81. */
  82. #define XFRM_IPTFS_MIN_L3HEADROOM 128
  83. #define XFRM_IPTFS_MIN_L2HEADROOM (L1_CACHE_BYTES > 64 ? 64 : 64 + 16)
  84. /* Min to try to share outer iptfs skb data vs copying into new skb */
  85. #define IPTFS_PKT_SHARE_MIN 129
  86. #define NSECS_IN_USEC 1000
  87. #define IPTFS_HRTIMER_MODE HRTIMER_MODE_REL_SOFT
  88. /**
  89. * struct xfrm_iptfs_config - configuration for the IPTFS tunnel.
  90. * @pkt_size: size of the outer IP packet. 0 to use interface and MTU discovery,
  91. * otherwise the user specified value.
  92. * @max_queue_size: The maximum number of octets allowed to be queued to be sent
  93. * over the IPTFS SA. The queue size is measured as the size of all the
  94. * packets enqueued.
  95. * @reorder_win_size: the number slots in the reorder window, thus the number of
  96. * packets that may arrive out of order.
  97. * @dont_frag: true to inhibit fragmenting across IPTFS outer packets.
  98. */
  99. struct xfrm_iptfs_config {
  100. u32 pkt_size; /* outer_packet_size or 0 */
  101. u32 max_queue_size; /* octets */
  102. u16 reorder_win_size;
  103. u8 dont_frag : 1;
  104. };
  105. struct skb_wseq {
  106. struct sk_buff *skb;
  107. u64 drop_time;
  108. };
  109. /**
  110. * struct xfrm_iptfs_data - mode specific xfrm state.
  111. * @cfg: IPTFS tunnel config.
  112. * @x: owning SA (xfrm_state).
  113. * @queue: queued user packets to send.
  114. * @queue_size: number of octets on queue (sum of packet sizes).
  115. * @ecn_queue_size: octets above with ECN mark.
  116. * @init_delay_ns: nanoseconds to wait to send initial IPTFS packet.
  117. * @iptfs_timer: output timer.
  118. * @iptfs_settime: time the output timer was set.
  119. * @payload_mtu: max payload size.
  120. * @w_seq_set: true after first seq received.
  121. * @w_wantseq: waiting for this seq number as next to process (in order).
  122. * @w_saved: the saved buf array (reorder window).
  123. * @w_savedlen: the saved len (not size).
  124. * @drop_lock: lock to protect reorder queue.
  125. * @drop_timer: timer for considering next packet lost.
  126. * @drop_time_ns: timer intervan in nanoseconds.
  127. * @ra_newskb: new pkt being reassembled.
  128. * @ra_wantseq: expected next sequence for reassembly.
  129. * @ra_runt: last pkt bytes from very end of last skb.
  130. * @ra_runtlen: size of ra_runt.
  131. */
  132. struct xfrm_iptfs_data {
  133. struct xfrm_iptfs_config cfg;
  134. /* Ingress User Input */
  135. struct xfrm_state *x; /* owning state */
  136. struct sk_buff_head queue; /* output queue */
  137. u32 queue_size; /* octets */
  138. u32 ecn_queue_size; /* octets above which ECN mark */
  139. u64 init_delay_ns; /* nanoseconds */
  140. struct hrtimer iptfs_timer; /* output timer */
  141. time64_t iptfs_settime; /* time timer was set */
  142. u32 payload_mtu; /* max payload size */
  143. /* Tunnel input reordering */
  144. bool w_seq_set; /* true after first seq received */
  145. u64 w_wantseq; /* expected next sequence */
  146. struct skb_wseq *w_saved; /* the saved buf array */
  147. u32 w_savedlen; /* the saved len (not size) */
  148. spinlock_t drop_lock;
  149. struct hrtimer drop_timer;
  150. u64 drop_time_ns;
  151. /* Tunnel input reassembly */
  152. struct sk_buff *ra_newskb; /* new pkt being reassembled */
  153. u64 ra_wantseq; /* expected next sequence */
  154. u8 ra_runt[6]; /* last pkt bytes from last skb */
  155. u8 ra_runtlen; /* count of ra_runt */
  156. };
  157. static u32 __iptfs_get_inner_mtu(struct xfrm_state *x, int outer_mtu);
  158. static enum hrtimer_restart iptfs_delay_timer(struct hrtimer *me);
  159. static enum hrtimer_restart iptfs_drop_timer(struct hrtimer *me);
  160. /* ================= */
  161. /* Utility Functions */
  162. /* ================= */
  163. #ifdef TRACEPOINTS_ENABLED
  164. static u32 __trace_ip_proto(struct iphdr *iph)
  165. {
  166. if (iph->version == 4)
  167. return iph->protocol;
  168. return ((struct ipv6hdr *)iph)->nexthdr;
  169. }
  170. static u32 __trace_ip_proto_seq(struct iphdr *iph)
  171. {
  172. void *nexthdr;
  173. u32 protocol = 0;
  174. if (iph->version == 4) {
  175. nexthdr = (void *)(iph + 1);
  176. protocol = iph->protocol;
  177. } else if (iph->version == 6) {
  178. nexthdr = (void *)(((struct ipv6hdr *)(iph)) + 1);
  179. protocol = ((struct ipv6hdr *)(iph))->nexthdr;
  180. }
  181. switch (protocol) {
  182. case IPPROTO_ICMP:
  183. return ntohs(((struct icmphdr *)nexthdr)->un.echo.sequence);
  184. case IPPROTO_ICMPV6:
  185. return ntohs(((struct icmp6hdr *)nexthdr)->icmp6_sequence);
  186. case IPPROTO_TCP:
  187. return ntohl(((struct tcphdr *)nexthdr)->seq);
  188. case IPPROTO_UDP:
  189. return ntohs(((struct udphdr *)nexthdr)->source);
  190. default:
  191. return 0;
  192. }
  193. }
  194. #endif /*TRACEPOINTS_ENABLED*/
  195. static u64 __esp_seq(struct sk_buff *skb)
  196. {
  197. u64 seq = ntohl(XFRM_SKB_CB(skb)->seq.input.low);
  198. return seq | (u64)ntohl(XFRM_SKB_CB(skb)->seq.input.hi) << 32;
  199. }
  200. /* ======================= */
  201. /* IPTFS SK_BUFF Functions */
  202. /* ======================= */
  203. /**
  204. * iptfs_alloc_skb() - Allocate a new `skb`.
  205. * @tpl: the skb to copy required meta-data from.
  206. * @len: the linear length of the head data, zero is fine.
  207. * @l3resv: true if skb reserve needs to support pushing L3 headers
  208. *
  209. * A new `skb` is allocated and required meta-data is copied from `tpl`, the
  210. * head data is sized to `len` + reserved space set according to the @l3resv
  211. * boolean.
  212. *
  213. * When @l3resv is false, resv is XFRM_IPTFS_MIN_L2HEADROOM which arranges for
  214. * `skb->data - 16` which is a good guess for good cache alignment (placing the
  215. * to be pushed L2 header at the start of a cacheline.
  216. *
  217. * Otherwise, @l3resv is true and resv is set to the correct reserved space for
  218. * dst->dev plus the calculated L3 overhead for the xfrm dst or
  219. * XFRM_IPTFS_MIN_L3HEADROOM whichever is larger. This is then cache aligned so
  220. * that all the headers will commonly fall in a cacheline when possible.
  221. *
  222. * l3resv=true is used on tunnel ingress (tx), because we need to reserve for
  223. * the new IPTFS packet (i.e., L2+L3 headers). On tunnel egress (rx) the data
  224. * being copied into the skb includes the user L3 headers already so we only
  225. * need to reserve for L2.
  226. *
  227. * Return: the new skb or NULL.
  228. */
  229. static struct sk_buff *iptfs_alloc_skb(struct sk_buff *tpl, u32 len, bool l3resv)
  230. {
  231. struct sk_buff *skb;
  232. u32 resv;
  233. if (!l3resv) {
  234. resv = XFRM_IPTFS_MIN_L2HEADROOM;
  235. } else {
  236. struct dst_entry *dst = skb_dst(tpl);
  237. resv = LL_RESERVED_SPACE(dst->dev) + dst->header_len;
  238. resv = max(resv, XFRM_IPTFS_MIN_L3HEADROOM);
  239. resv = L1_CACHE_ALIGN(resv);
  240. }
  241. skb = alloc_skb(len + resv, GFP_ATOMIC | __GFP_NOWARN);
  242. if (!skb)
  243. return NULL;
  244. skb_reserve(skb, resv);
  245. if (!l3resv) {
  246. /* xfrm_input resume needs dev and xfrm ext from tunnel pkt */
  247. skb->dev = tpl->dev;
  248. __skb_ext_copy(skb, tpl);
  249. }
  250. /* dropped by xfrm_input, used by xfrm_output */
  251. skb_dst_copy(skb, tpl);
  252. return skb;
  253. }
  254. /**
  255. * iptfs_skb_head_to_frag() - initialize a skb_frag_t based on skb head data
  256. * @skb: skb with the head data
  257. * @frag: frag to initialize
  258. */
  259. static void iptfs_skb_head_to_frag(const struct sk_buff *skb, skb_frag_t *frag)
  260. {
  261. struct page *page = virt_to_head_page(skb->data);
  262. unsigned char *addr = (unsigned char *)page_address(page);
  263. skb_frag_fill_page_desc(frag, page, skb->data - addr, skb_headlen(skb));
  264. }
  265. /**
  266. * struct iptfs_skb_frag_walk - use to track a walk through fragments
  267. * @fragi: current fragment index
  268. * @past: length of data in fragments before @fragi
  269. * @total: length of data in all fragments
  270. * @nr_frags: number of fragments present in array
  271. * @initial_offset: the value passed in to skb_prepare_frag_walk()
  272. * @frags: the page fragments inc. room for head page
  273. * @pp_recycle: copy of skb->pp_recycle
  274. */
  275. struct iptfs_skb_frag_walk {
  276. u32 fragi;
  277. u32 past;
  278. u32 total;
  279. u32 nr_frags;
  280. u32 initial_offset;
  281. skb_frag_t frags[MAX_SKB_FRAGS + 1];
  282. bool pp_recycle;
  283. };
  284. /**
  285. * iptfs_skb_prepare_frag_walk() - initialize a frag walk over an skb.
  286. * @skb: the skb to walk.
  287. * @initial_offset: start the walk @initial_offset into the skb.
  288. * @walk: the walk to initialize
  289. *
  290. * Future calls to skb_add_frags() will expect the @offset value to be at
  291. * least @initial_offset large.
  292. */
  293. static void iptfs_skb_prepare_frag_walk(struct sk_buff *skb, u32 initial_offset,
  294. struct iptfs_skb_frag_walk *walk)
  295. {
  296. struct skb_shared_info *shinfo = skb_shinfo(skb);
  297. skb_frag_t *frag, *from;
  298. u32 i;
  299. walk->initial_offset = initial_offset;
  300. walk->fragi = 0;
  301. walk->past = 0;
  302. walk->total = 0;
  303. walk->nr_frags = 0;
  304. walk->pp_recycle = skb->pp_recycle;
  305. if (skb->head_frag) {
  306. if (initial_offset >= skb_headlen(skb)) {
  307. initial_offset -= skb_headlen(skb);
  308. } else {
  309. frag = &walk->frags[walk->nr_frags++];
  310. iptfs_skb_head_to_frag(skb, frag);
  311. frag->offset += initial_offset;
  312. frag->len -= initial_offset;
  313. walk->total += frag->len;
  314. initial_offset = 0;
  315. }
  316. } else {
  317. initial_offset -= skb_headlen(skb);
  318. }
  319. for (i = 0; i < shinfo->nr_frags; i++) {
  320. from = &shinfo->frags[i];
  321. if (initial_offset >= from->len) {
  322. initial_offset -= from->len;
  323. continue;
  324. }
  325. frag = &walk->frags[walk->nr_frags++];
  326. *frag = *from;
  327. if (initial_offset) {
  328. frag->offset += initial_offset;
  329. frag->len -= initial_offset;
  330. initial_offset = 0;
  331. }
  332. walk->total += frag->len;
  333. }
  334. }
  335. static u32 iptfs_skb_reset_frag_walk(struct iptfs_skb_frag_walk *walk,
  336. u32 offset)
  337. {
  338. /* Adjust offset to refer to internal walk values */
  339. offset -= walk->initial_offset;
  340. /* Get to the correct fragment for offset */
  341. while (offset < walk->past) {
  342. walk->past -= walk->frags[--walk->fragi].len;
  343. if (offset >= walk->past)
  344. break;
  345. }
  346. while (offset >= walk->past + walk->frags[walk->fragi].len)
  347. walk->past += walk->frags[walk->fragi++].len;
  348. /* offset now relative to this current frag */
  349. offset -= walk->past;
  350. return offset;
  351. }
  352. /**
  353. * iptfs_skb_can_add_frags() - check if ok to add frags from walk to skb
  354. * @skb: skb to check for adding frags to
  355. * @walk: the walk that will be used as source for frags.
  356. * @offset: offset from beginning of original skb to start from.
  357. * @len: amount of data to add frag references to in @skb.
  358. *
  359. * Return: true if ok to add frags.
  360. */
  361. static bool iptfs_skb_can_add_frags(const struct sk_buff *skb,
  362. struct iptfs_skb_frag_walk *walk,
  363. u32 offset, u32 len)
  364. {
  365. struct skb_shared_info *shinfo = skb_shinfo(skb);
  366. u32 fragi, nr_frags, fraglen;
  367. if (skb_has_frag_list(skb) || skb->pp_recycle != walk->pp_recycle)
  368. return false;
  369. /* Make offset relative to current frag after setting that */
  370. offset = iptfs_skb_reset_frag_walk(walk, offset);
  371. /* Verify we have array space for the fragments we need to add */
  372. fragi = walk->fragi;
  373. nr_frags = shinfo->nr_frags;
  374. while (len && fragi < walk->nr_frags) {
  375. skb_frag_t *frag = &walk->frags[fragi];
  376. fraglen = frag->len;
  377. if (offset) {
  378. fraglen -= offset;
  379. offset = 0;
  380. }
  381. if (++nr_frags > MAX_SKB_FRAGS)
  382. return false;
  383. if (len <= fraglen)
  384. return true;
  385. len -= fraglen;
  386. fragi++;
  387. }
  388. /* We may not copy all @len but what we have will fit. */
  389. return true;
  390. }
  391. /**
  392. * iptfs_skb_add_frags() - add a range of fragment references into an skb
  393. * @skb: skb to add references into
  394. * @walk: the walk to add referenced fragments from.
  395. * @offset: offset from beginning of original skb to start from.
  396. * @len: amount of data to add frag references to in @skb.
  397. *
  398. * iptfs_skb_can_add_frags() should be called before this function to verify
  399. * that the destination @skb is compatible with the walk and has space in the
  400. * array for the to be added frag references.
  401. *
  402. * Return: The number of bytes not added to @skb b/c we reached the end of the
  403. * walk before adding all of @len.
  404. */
  405. static int iptfs_skb_add_frags(struct sk_buff *skb,
  406. struct iptfs_skb_frag_walk *walk, u32 offset,
  407. u32 len)
  408. {
  409. struct skb_shared_info *shinfo = skb_shinfo(skb);
  410. u32 fraglen;
  411. if (!walk->nr_frags || offset >= walk->total + walk->initial_offset)
  412. return len;
  413. /* make offset relative to current frag after setting that */
  414. offset = iptfs_skb_reset_frag_walk(walk, offset);
  415. while (len && walk->fragi < walk->nr_frags) {
  416. skb_frag_t *frag = &walk->frags[walk->fragi];
  417. skb_frag_t *tofrag = &shinfo->frags[shinfo->nr_frags];
  418. *tofrag = *frag;
  419. if (offset) {
  420. tofrag->offset += offset;
  421. tofrag->len -= offset;
  422. offset = 0;
  423. }
  424. __skb_frag_ref(tofrag);
  425. shinfo->nr_frags++;
  426. /* see if we are done */
  427. fraglen = tofrag->len;
  428. if (len < fraglen) {
  429. tofrag->len = len;
  430. skb->len += len;
  431. skb->data_len += len;
  432. return 0;
  433. }
  434. /* advance to next source fragment */
  435. len -= fraglen; /* careful, use dst bv_len */
  436. skb->len += fraglen; /* careful, " " " */
  437. skb->data_len += fraglen; /* careful, " " " */
  438. walk->past += frag->len; /* careful, use src bv_len */
  439. walk->fragi++;
  440. }
  441. return len;
  442. }
  443. /* ================================== */
  444. /* IPTFS Trace Event Definitions */
  445. /* ================================== */
  446. #define CREATE_TRACE_POINTS
  447. #include "trace_iptfs.h"
  448. /* ================================== */
  449. /* IPTFS Receiving (egress) Functions */
  450. /* ================================== */
  451. /**
  452. * iptfs_pskb_add_frags() - Create and add frags into a new sk_buff.
  453. * @tpl: template to create new skb from.
  454. * @walk: The source for fragments to add.
  455. * @off: The offset into @walk to add frags from, also used with @st and
  456. * @copy_len.
  457. * @len: The length of data to add covering frags from @walk into @skb.
  458. * This must be <= @skblen.
  459. * @st: The sequence state to copy from into the new head skb.
  460. * @copy_len: Copy @copy_len bytes from @st at offset @off into the new skb
  461. * linear space.
  462. *
  463. * Create a new sk_buff `skb` using the template @tpl. Copy @copy_len bytes from
  464. * @st into the new skb linear space, and then add shared fragments from the
  465. * frag walk for the remaining @len of data (i.e., @len - @copy_len bytes).
  466. *
  467. * Return: The newly allocated sk_buff `skb` or NULL if an error occurs.
  468. */
  469. static struct sk_buff *
  470. iptfs_pskb_add_frags(struct sk_buff *tpl, struct iptfs_skb_frag_walk *walk,
  471. u32 off, u32 len, struct skb_seq_state *st, u32 copy_len)
  472. {
  473. struct sk_buff *skb;
  474. skb = iptfs_alloc_skb(tpl, copy_len, false);
  475. if (!skb)
  476. return NULL;
  477. /* this should not normally be happening */
  478. if (!iptfs_skb_can_add_frags(skb, walk, off + copy_len,
  479. len - copy_len)) {
  480. kfree_skb(skb);
  481. return NULL;
  482. }
  483. if (copy_len &&
  484. skb_copy_seq_read(st, off, skb_put(skb, copy_len), copy_len)) {
  485. XFRM_INC_STATS(dev_net(st->root_skb->dev),
  486. LINUX_MIB_XFRMINERROR);
  487. kfree_skb(skb);
  488. return NULL;
  489. }
  490. iptfs_skb_add_frags(skb, walk, off + copy_len, len - copy_len);
  491. return skb;
  492. }
  493. /**
  494. * iptfs_pskb_extract_seq() - Create and load data into a new sk_buff.
  495. * @skblen: the total data size for `skb`.
  496. * @st: The source for the rest of the data to copy into `skb`.
  497. * @off: The offset into @st to copy data from.
  498. * @len: The length of data to copy from @st into `skb`. This must be <=
  499. * @skblen.
  500. *
  501. * Create a new sk_buff `skb` with @skblen of packet data space. If non-zero,
  502. * copy @rlen bytes of @runt into `skb`. Then using seq functions copy @len
  503. * bytes from @st into `skb` starting from @off.
  504. *
  505. * It is an error for @len to be greater than the amount of data left in @st.
  506. *
  507. * Return: The newly allocated sk_buff `skb` or NULL if an error occurs.
  508. */
  509. static struct sk_buff *
  510. iptfs_pskb_extract_seq(u32 skblen, struct skb_seq_state *st, u32 off, int len)
  511. {
  512. struct sk_buff *skb = iptfs_alloc_skb(st->root_skb, skblen, false);
  513. if (!skb)
  514. return NULL;
  515. if (skb_copy_seq_read(st, off, skb_put(skb, len), len)) {
  516. XFRM_INC_STATS(dev_net(st->root_skb->dev), LINUX_MIB_XFRMINERROR);
  517. kfree_skb(skb);
  518. return NULL;
  519. }
  520. return skb;
  521. }
  522. /**
  523. * iptfs_input_save_runt() - save data in xtfs runt space.
  524. * @xtfs: xtfs state
  525. * @seq: the current sequence
  526. * @buf: packet data
  527. * @len: length of packet data
  528. *
  529. * Save the small (`len`) start of a fragmented packet in `buf` in the xtfs data
  530. * runt space.
  531. */
  532. static void iptfs_input_save_runt(struct xfrm_iptfs_data *xtfs, u64 seq,
  533. u8 *buf, int len)
  534. {
  535. memcpy(xtfs->ra_runt, buf, len);
  536. xtfs->ra_runtlen = len;
  537. xtfs->ra_wantseq = seq + 1;
  538. }
  539. /**
  540. * __iptfs_iphlen() - return the v4/v6 header length using packet data.
  541. * @data: pointer at octet with version nibble
  542. *
  543. * The version data has been checked to be valid (i.e., either 4 or 6).
  544. *
  545. * Return: the IP header size based on the IP version.
  546. */
  547. static u32 __iptfs_iphlen(u8 *data)
  548. {
  549. struct iphdr *iph = (struct iphdr *)data;
  550. if (iph->version == 0x4)
  551. return sizeof(*iph);
  552. return sizeof(struct ipv6hdr);
  553. }
  554. /**
  555. * __iptfs_iplen() - return the v4/v6 length using packet data.
  556. * @data: pointer to ip (v4/v6) packet header
  557. *
  558. * Grab the IPv4 or IPv6 length value in the start of the inner packet header
  559. * pointed to by `data`. Assumes data len is enough for the length field only.
  560. *
  561. * The version data has been checked to be valid (i.e., either 4 or 6).
  562. *
  563. * Return: the length value.
  564. */
  565. static u32 __iptfs_iplen(u8 *data)
  566. {
  567. struct iphdr *iph = (struct iphdr *)data;
  568. if (iph->version == 0x4)
  569. return ntohs(iph->tot_len);
  570. return ntohs(((struct ipv6hdr *)iph)->payload_len) +
  571. sizeof(struct ipv6hdr);
  572. }
  573. /**
  574. * iptfs_complete_inner_skb() - finish preparing the inner packet for gro recv.
  575. * @x: xfrm state
  576. * @skb: the inner packet
  577. *
  578. * Finish the standard xfrm processing on the inner packet prior to sending back
  579. * through gro_cells_receive. We do this separately b/c we are building a list
  580. * of packets in the hopes that one day a list will be taken by
  581. * xfrm_input.
  582. */
  583. static void iptfs_complete_inner_skb(struct xfrm_state *x, struct sk_buff *skb)
  584. {
  585. skb_reset_network_header(skb);
  586. /* The packet is going back through gro_cells_receive no need to
  587. * set this.
  588. */
  589. skb_reset_transport_header(skb);
  590. /* Packet already has checksum value set. */
  591. skb->ip_summed = CHECKSUM_NONE;
  592. /* Our skb will contain the header data copied when this outer packet
  593. * which contained the start of this inner packet. This is true
  594. * when we allocate a new skb as well as when we reuse the existing skb.
  595. */
  596. if (ip_hdr(skb)->version == 0x4) {
  597. struct iphdr *iph = ip_hdr(skb);
  598. if (x->props.flags & XFRM_STATE_DECAP_DSCP)
  599. ipv4_copy_dscp(XFRM_MODE_SKB_CB(skb)->tos, iph);
  600. if (!(x->props.flags & XFRM_STATE_NOECN))
  601. if (INET_ECN_is_ce(XFRM_MODE_SKB_CB(skb)->tos))
  602. IP_ECN_set_ce(iph);
  603. skb->protocol = htons(ETH_P_IP);
  604. } else {
  605. struct ipv6hdr *iph = ipv6_hdr(skb);
  606. if (x->props.flags & XFRM_STATE_DECAP_DSCP)
  607. ipv6_copy_dscp(XFRM_MODE_SKB_CB(skb)->tos, iph);
  608. if (!(x->props.flags & XFRM_STATE_NOECN))
  609. if (INET_ECN_is_ce(XFRM_MODE_SKB_CB(skb)->tos))
  610. IP6_ECN_set_ce(skb, iph);
  611. skb->protocol = htons(ETH_P_IPV6);
  612. }
  613. }
  614. static void __iptfs_reassem_done(struct xfrm_iptfs_data *xtfs, bool free)
  615. {
  616. assert_spin_locked(&xtfs->drop_lock);
  617. /* We don't care if it works locking takes care of things */
  618. hrtimer_try_to_cancel(&xtfs->drop_timer);
  619. if (free)
  620. kfree_skb(xtfs->ra_newskb);
  621. xtfs->ra_newskb = NULL;
  622. }
  623. /**
  624. * iptfs_reassem_abort() - In-progress packet is aborted free the state.
  625. * @xtfs: xtfs state
  626. */
  627. static void iptfs_reassem_abort(struct xfrm_iptfs_data *xtfs)
  628. {
  629. __iptfs_reassem_done(xtfs, true);
  630. }
  631. /**
  632. * iptfs_reassem_done() - In-progress packet is complete, clear the state.
  633. * @xtfs: xtfs state
  634. */
  635. static void iptfs_reassem_done(struct xfrm_iptfs_data *xtfs)
  636. {
  637. __iptfs_reassem_done(xtfs, false);
  638. }
  639. /**
  640. * iptfs_reassem_cont() - Continue the reassembly of an inner packets.
  641. * @xtfs: xtfs state
  642. * @seq: sequence of current packet
  643. * @st: seq read stat for current packet
  644. * @skb: current packet
  645. * @data: offset into sequential packet data
  646. * @blkoff: packet blkoff value
  647. * @list: list of skbs to enqueue completed packet on
  648. *
  649. * Process an IPTFS payload that has a non-zero `blkoff` or when we are
  650. * expecting the continuation b/c we have a runt or in-progress packet.
  651. *
  652. * Return: the new data offset to continue processing from.
  653. */
  654. static u32 iptfs_reassem_cont(struct xfrm_iptfs_data *xtfs, u64 seq,
  655. struct skb_seq_state *st, struct sk_buff *skb,
  656. u32 data, u32 blkoff, struct list_head *list)
  657. {
  658. struct iptfs_skb_frag_walk _fragwalk;
  659. struct iptfs_skb_frag_walk *fragwalk = NULL;
  660. struct sk_buff *newskb = xtfs->ra_newskb;
  661. u32 remaining = skb->len - data;
  662. u32 runtlen = xtfs->ra_runtlen;
  663. u32 copylen, fraglen, ipremain, iphlen, iphremain, rrem;
  664. /* Handle packet fragment we aren't expecting */
  665. if (!runtlen && !xtfs->ra_newskb)
  666. return data + min(blkoff, remaining);
  667. /* Important to remember that input to this function is an ordered
  668. * packet stream (unless the user disabled the reorder window). Thus if
  669. * we are waiting for, and expecting the next packet so we can continue
  670. * assembly, a newer sequence number indicates older ones are not coming
  671. * (or if they do should be ignored). Technically we can receive older
  672. * ones when the reorder window is disabled; however, the user should
  673. * have disabled fragmentation in this case, and regardless we don't
  674. * deal with it.
  675. *
  676. * blkoff could be zero if the stream is messed up (or it's an all pad
  677. * insertion) be careful to handle that case in each of the below
  678. */
  679. /* Too old case: This can happen when the reorder window is disabled so
  680. * ordering isn't actually guaranteed.
  681. */
  682. if (seq < xtfs->ra_wantseq)
  683. return data + remaining;
  684. /* Too new case: We missed what we wanted cleanup. */
  685. if (seq > xtfs->ra_wantseq) {
  686. XFRM_INC_STATS(xs_net(xtfs->x), LINUX_MIB_XFRMINIPTFSERROR);
  687. goto abandon;
  688. }
  689. if (blkoff == 0) {
  690. if ((*skb->data & 0xF0) != 0) {
  691. XFRM_INC_STATS(xs_net(xtfs->x),
  692. LINUX_MIB_XFRMINIPTFSERROR);
  693. goto abandon;
  694. }
  695. /* Handle all pad case, advance expected sequence number.
  696. * (RFC 9347 S2.2.3)
  697. */
  698. xtfs->ra_wantseq++;
  699. /* will end parsing */
  700. return data + remaining;
  701. }
  702. if (runtlen) {
  703. /* Regardless of what happens we're done with the runt */
  704. xtfs->ra_runtlen = 0;
  705. /* The start of this inner packet was at the very end of the last
  706. * iptfs payload which didn't include enough for the ip header
  707. * length field. We must have *at least* that now.
  708. */
  709. rrem = sizeof(xtfs->ra_runt) - runtlen;
  710. if (remaining < rrem || blkoff < rrem) {
  711. XFRM_INC_STATS(xs_net(xtfs->x),
  712. LINUX_MIB_XFRMINIPTFSERROR);
  713. goto abandon;
  714. }
  715. /* fill in the runt data */
  716. if (skb_copy_seq_read(st, data, &xtfs->ra_runt[runtlen],
  717. rrem)) {
  718. XFRM_INC_STATS(xs_net(xtfs->x),
  719. LINUX_MIB_XFRMINBUFFERERROR);
  720. goto abandon;
  721. }
  722. /* We have enough data to get the ip length value now,
  723. * allocate an in progress skb
  724. */
  725. ipremain = __iptfs_iplen(xtfs->ra_runt);
  726. if (ipremain < sizeof(xtfs->ra_runt)) {
  727. /* length has to be at least runtsize large */
  728. XFRM_INC_STATS(xs_net(xtfs->x),
  729. LINUX_MIB_XFRMINIPTFSERROR);
  730. goto abandon;
  731. }
  732. /* For the runt case we don't attempt sharing currently. NOTE:
  733. * Currently, this IPTFS implementation will not create runts.
  734. */
  735. newskb = iptfs_alloc_skb(skb, ipremain, false);
  736. if (!newskb) {
  737. XFRM_INC_STATS(xs_net(xtfs->x), LINUX_MIB_XFRMINERROR);
  738. goto abandon;
  739. }
  740. xtfs->ra_newskb = newskb;
  741. /* Copy the runt data into the buffer, but leave data
  742. * pointers the same as normal non-runt case. The extra `rrem`
  743. * recopied bytes are basically cacheline free. Allows using
  744. * same logic below to complete.
  745. */
  746. memcpy(skb_put(newskb, runtlen), xtfs->ra_runt,
  747. sizeof(xtfs->ra_runt));
  748. }
  749. /* Continue reassembling the packet */
  750. ipremain = __iptfs_iplen(newskb->data);
  751. iphlen = __iptfs_iphlen(newskb->data);
  752. ipremain -= newskb->len;
  753. if (blkoff < ipremain) {
  754. /* Corrupt data, we don't have enough to complete the packet */
  755. XFRM_INC_STATS(xs_net(xtfs->x), LINUX_MIB_XFRMINIPTFSERROR);
  756. goto abandon;
  757. }
  758. /* We want the IP header in linear space */
  759. if (newskb->len < iphlen) {
  760. iphremain = iphlen - newskb->len;
  761. if (blkoff < iphremain) {
  762. XFRM_INC_STATS(xs_net(xtfs->x),
  763. LINUX_MIB_XFRMINIPTFSERROR);
  764. goto abandon;
  765. }
  766. fraglen = min(blkoff, remaining);
  767. copylen = min(fraglen, iphremain);
  768. if (skb_copy_seq_read(st, data, skb_put(newskb, copylen),
  769. copylen)) {
  770. XFRM_INC_STATS(xs_net(xtfs->x),
  771. LINUX_MIB_XFRMINBUFFERERROR);
  772. goto abandon;
  773. }
  774. /* this is a silly condition that might occur anyway */
  775. if (copylen < iphremain) {
  776. xtfs->ra_wantseq++;
  777. return data + fraglen;
  778. }
  779. /* update data and things derived from it */
  780. data += copylen;
  781. blkoff -= copylen;
  782. remaining -= copylen;
  783. ipremain -= copylen;
  784. }
  785. fraglen = min(blkoff, remaining);
  786. copylen = min(fraglen, ipremain);
  787. /* If we may have the opportunity to share prepare a fragwalk. */
  788. if (!skb_has_frag_list(skb) && !skb_has_frag_list(newskb) &&
  789. (skb->head_frag || skb->len == skb->data_len) &&
  790. skb->pp_recycle == newskb->pp_recycle) {
  791. fragwalk = &_fragwalk;
  792. iptfs_skb_prepare_frag_walk(skb, data, fragwalk);
  793. }
  794. /* Try share then copy. */
  795. if (fragwalk &&
  796. iptfs_skb_can_add_frags(newskb, fragwalk, data, copylen)) {
  797. iptfs_skb_add_frags(newskb, fragwalk, data, copylen);
  798. } else {
  799. if (skb_linearize(newskb)) {
  800. XFRM_INC_STATS(xs_net(xtfs->x),
  801. LINUX_MIB_XFRMINBUFFERERROR);
  802. goto abandon;
  803. }
  804. /* copy fragment data into newskb */
  805. if (skb_copy_seq_read(st, data, skb_put(newskb, copylen),
  806. copylen)) {
  807. XFRM_INC_STATS(xs_net(xtfs->x),
  808. LINUX_MIB_XFRMINBUFFERERROR);
  809. goto abandon;
  810. }
  811. }
  812. if (copylen < ipremain) {
  813. xtfs->ra_wantseq++;
  814. } else {
  815. /* We are done with packet reassembly! */
  816. iptfs_reassem_done(xtfs);
  817. iptfs_complete_inner_skb(xtfs->x, newskb);
  818. list_add_tail(&newskb->list, list);
  819. }
  820. /* will continue on to new data block or end */
  821. return data + fraglen;
  822. abandon:
  823. if (xtfs->ra_newskb) {
  824. iptfs_reassem_abort(xtfs);
  825. } else {
  826. xtfs->ra_runtlen = 0;
  827. xtfs->ra_wantseq = 0;
  828. }
  829. /* skip past fragment, maybe to end */
  830. return data + min(blkoff, remaining);
  831. }
  832. static bool __input_process_payload(struct xfrm_state *x, u32 data,
  833. struct skb_seq_state *skbseq,
  834. struct list_head *sublist)
  835. {
  836. u8 hbytes[sizeof(struct ipv6hdr)];
  837. struct iptfs_skb_frag_walk _fragwalk;
  838. struct iptfs_skb_frag_walk *fragwalk = NULL;
  839. struct sk_buff *defer, *first_skb, *next, *skb;
  840. const unsigned char *old_mac;
  841. struct xfrm_iptfs_data *xtfs;
  842. struct iphdr *iph;
  843. struct net *net;
  844. u32 first_iplen, iphlen, iplen, remaining, tail;
  845. u32 capturelen;
  846. u64 seq;
  847. xtfs = x->mode_data;
  848. net = xs_net(x);
  849. skb = skbseq->root_skb;
  850. first_skb = NULL;
  851. defer = NULL;
  852. seq = __esp_seq(skb);
  853. /* Save the old mac header if set */
  854. old_mac = skb_mac_header_was_set(skb) ? skb_mac_header(skb) : NULL;
  855. /* New packets */
  856. tail = skb->len;
  857. while (data < tail) {
  858. __be16 protocol = 0;
  859. /* Gather information on the next data block.
  860. * `data` points to the start of the data block.
  861. */
  862. remaining = tail - data;
  863. /* try and copy enough bytes to read length from ipv4/ipv6 */
  864. iphlen = min_t(u32, remaining, 6);
  865. if (skb_copy_seq_read(skbseq, data, hbytes, iphlen)) {
  866. XFRM_INC_STATS(net, LINUX_MIB_XFRMINBUFFERERROR);
  867. goto done;
  868. }
  869. iph = (struct iphdr *)hbytes;
  870. if (iph->version == 0x4) {
  871. /* must have at least tot_len field present */
  872. if (remaining < 4) {
  873. /* save the bytes we have, advance data and exit */
  874. iptfs_input_save_runt(xtfs, seq, hbytes,
  875. remaining);
  876. data += remaining;
  877. break;
  878. }
  879. iplen = be16_to_cpu(iph->tot_len);
  880. iphlen = iph->ihl << 2;
  881. if (iplen < iphlen || iphlen < sizeof(*iph)) {
  882. XFRM_INC_STATS(net,
  883. LINUX_MIB_XFRMINHDRERROR);
  884. goto done;
  885. }
  886. protocol = cpu_to_be16(ETH_P_IP);
  887. XFRM_MODE_SKB_CB(skbseq->root_skb)->tos = iph->tos;
  888. } else if (iph->version == 0x6) {
  889. /* must have at least payload_len field present */
  890. if (remaining < 6) {
  891. /* save the bytes we have, advance data and exit */
  892. iptfs_input_save_runt(xtfs, seq, hbytes,
  893. remaining);
  894. data += remaining;
  895. break;
  896. }
  897. iplen = be16_to_cpu(((struct ipv6hdr *)hbytes)->payload_len);
  898. iplen += sizeof(struct ipv6hdr);
  899. iphlen = sizeof(struct ipv6hdr);
  900. protocol = cpu_to_be16(ETH_P_IPV6);
  901. XFRM_MODE_SKB_CB(skbseq->root_skb)->tos =
  902. ipv6_get_dsfield((struct ipv6hdr *)iph);
  903. } else if (iph->version == 0x0) {
  904. /* pad */
  905. data = tail;
  906. break;
  907. } else {
  908. XFRM_INC_STATS(net, LINUX_MIB_XFRMINBUFFERERROR);
  909. goto done;
  910. }
  911. if (unlikely(skbseq->stepped_offset)) {
  912. /* We need to reset our seq read, it can't backup at
  913. * this point.
  914. */
  915. struct sk_buff *save = skbseq->root_skb;
  916. skb_abort_seq_read(skbseq);
  917. skb_prepare_seq_read(save, data, tail, skbseq);
  918. }
  919. if (first_skb) {
  920. skb = NULL;
  921. } else {
  922. first_skb = skb;
  923. first_iplen = iplen;
  924. fragwalk = NULL;
  925. /* We are going to skip over `data` bytes to reach the
  926. * start of the IP header of `iphlen` len for `iplen`
  927. * inner packet.
  928. */
  929. if (skb_has_frag_list(skb)) {
  930. defer = skb;
  931. skb = NULL;
  932. } else if (data + iphlen <= skb_headlen(skb) &&
  933. /* make sure our header is 32-bit aligned? */
  934. /* ((uintptr_t)(skb->data + data) & 0x3) == 0 && */
  935. skb_tailroom(skb) + tail - data >= iplen) {
  936. /* Reuse the received skb.
  937. *
  938. * We have enough headlen to pull past any
  939. * initial fragment data, leaving at least the
  940. * IP header in the linear buffer space.
  941. *
  942. * For linear buffer space we only require that
  943. * linear buffer space is large enough to
  944. * eventually hold the entire reassembled
  945. * packet (by including tailroom in the check).
  946. *
  947. * For non-linear tailroom is 0 and so we only
  948. * re-use if the entire packet is present
  949. * already.
  950. *
  951. * NOTE: there are many more options for
  952. * sharing, KISS for now. Also, this can produce
  953. * skb's with the IP header unaligned to 32
  954. * bits. If that ends up being a problem then a
  955. * check should be added to the conditional
  956. * above that the header lies on a 32-bit
  957. * boundary as well.
  958. */
  959. skb_pull(skb, data);
  960. /* our range just changed */
  961. data = 0;
  962. tail = skb->len;
  963. remaining = skb->len;
  964. skb->protocol = protocol;
  965. skb_mac_header_rebuild(skb);
  966. if (skb->mac_len)
  967. eth_hdr(skb)->h_proto = skb->protocol;
  968. /* all pointers could be changed now reset walk */
  969. skb_abort_seq_read(skbseq);
  970. skb_prepare_seq_read(skb, data, tail, skbseq);
  971. } else if (skb->head_frag &&
  972. /* We have the IP header right now */
  973. remaining >= iphlen) {
  974. fragwalk = &_fragwalk;
  975. iptfs_skb_prepare_frag_walk(skb, data, fragwalk);
  976. defer = skb;
  977. skb = NULL;
  978. } else {
  979. /* We couldn't reuse the input skb so allocate a
  980. * new one.
  981. */
  982. defer = skb;
  983. skb = NULL;
  984. }
  985. /* Don't trim `first_skb` until the end as we are
  986. * walking that data now.
  987. */
  988. }
  989. capturelen = min(iplen, remaining);
  990. if (!skb) {
  991. if (!fragwalk ||
  992. /* Large enough to be worth sharing */
  993. iplen < IPTFS_PKT_SHARE_MIN ||
  994. /* Have IP header + some data to share. */
  995. capturelen <= iphlen ||
  996. /* Try creating skb and adding frags */
  997. !(skb = iptfs_pskb_add_frags(first_skb, fragwalk,
  998. data, capturelen,
  999. skbseq, iphlen))) {
  1000. skb = iptfs_pskb_extract_seq(iplen, skbseq, data, capturelen);
  1001. }
  1002. if (!skb) {
  1003. /* skip to next packet or done */
  1004. data += capturelen;
  1005. continue;
  1006. }
  1007. skb->protocol = protocol;
  1008. if (old_mac) {
  1009. /* rebuild the mac header */
  1010. skb_set_mac_header(skb, -first_skb->mac_len);
  1011. memcpy(skb_mac_header(skb), old_mac, first_skb->mac_len);
  1012. eth_hdr(skb)->h_proto = skb->protocol;
  1013. }
  1014. }
  1015. data += capturelen;
  1016. if (skb->len < iplen) {
  1017. /* Start reassembly */
  1018. spin_lock(&xtfs->drop_lock);
  1019. xtfs->ra_newskb = skb;
  1020. xtfs->ra_wantseq = seq + 1;
  1021. if (!hrtimer_is_queued(&xtfs->drop_timer)) {
  1022. /* softirq blocked lest the timer fire and interrupt us */
  1023. hrtimer_start(&xtfs->drop_timer,
  1024. xtfs->drop_time_ns,
  1025. IPTFS_HRTIMER_MODE);
  1026. }
  1027. spin_unlock(&xtfs->drop_lock);
  1028. break;
  1029. }
  1030. iptfs_complete_inner_skb(x, skb);
  1031. list_add_tail(&skb->list, sublist);
  1032. }
  1033. if (data != tail)
  1034. /* this should not happen from the above code */
  1035. XFRM_INC_STATS(net, LINUX_MIB_XFRMINIPTFSERROR);
  1036. if (first_skb && first_iplen && !defer && first_skb != xtfs->ra_newskb) {
  1037. /* first_skb is queued b/c !defer and not partial */
  1038. if (pskb_trim(first_skb, first_iplen)) {
  1039. /* error trimming */
  1040. list_del(&first_skb->list);
  1041. defer = first_skb;
  1042. }
  1043. first_skb->ip_summed = CHECKSUM_NONE;
  1044. }
  1045. /* Send the packets! */
  1046. list_for_each_entry_safe(skb, next, sublist, list) {
  1047. skb_list_del_init(skb);
  1048. if (xfrm_input(skb, 0, 0, -2))
  1049. kfree_skb(skb);
  1050. }
  1051. done:
  1052. skb = skbseq->root_skb;
  1053. skb_abort_seq_read(skbseq);
  1054. if (defer) {
  1055. consume_skb(defer);
  1056. } else if (!first_skb) {
  1057. /* skb is the original passed in skb, but we didn't get far
  1058. * enough to process it as the first_skb, if we had it would
  1059. * either be save in ra_newskb, trimmed and sent on as an skb or
  1060. * placed in defer to be freed.
  1061. */
  1062. kfree_skb(skb);
  1063. }
  1064. return true;
  1065. }
  1066. /**
  1067. * iptfs_input_ordered() - handle next in order IPTFS payload.
  1068. * @x: xfrm state
  1069. * @skb: current packet
  1070. *
  1071. * Process the IPTFS payload in `skb` and consume it afterwards.
  1072. */
  1073. static void iptfs_input_ordered(struct xfrm_state *x, struct sk_buff *skb)
  1074. {
  1075. struct ip_iptfs_cc_hdr iptcch;
  1076. struct skb_seq_state skbseq;
  1077. struct list_head sublist; /* rename this it's just a list */
  1078. struct xfrm_iptfs_data *xtfs;
  1079. struct ip_iptfs_hdr *ipth;
  1080. struct net *net;
  1081. u32 blkoff, data, remaining;
  1082. bool consumed = false;
  1083. u64 seq;
  1084. xtfs = x->mode_data;
  1085. net = xs_net(x);
  1086. seq = __esp_seq(skb);
  1087. /* Large enough to hold both types of header */
  1088. ipth = (struct ip_iptfs_hdr *)&iptcch;
  1089. skb_prepare_seq_read(skb, 0, skb->len, &skbseq);
  1090. /* Get the IPTFS header and validate it */
  1091. if (skb_copy_seq_read(&skbseq, 0, ipth, sizeof(*ipth))) {
  1092. XFRM_INC_STATS(net, LINUX_MIB_XFRMINBUFFERERROR);
  1093. goto done;
  1094. }
  1095. data = sizeof(*ipth);
  1096. trace_iptfs_egress_recv(skb, xtfs, be16_to_cpu(ipth->block_offset));
  1097. /* Set data past the basic header */
  1098. if (ipth->subtype == IPTFS_SUBTYPE_CC) {
  1099. /* Copy the rest of the CC header */
  1100. remaining = sizeof(iptcch) - sizeof(*ipth);
  1101. if (skb_copy_seq_read(&skbseq, data, ipth + 1, remaining)) {
  1102. XFRM_INC_STATS(net, LINUX_MIB_XFRMINBUFFERERROR);
  1103. goto done;
  1104. }
  1105. data += remaining;
  1106. } else if (ipth->subtype != IPTFS_SUBTYPE_BASIC) {
  1107. XFRM_INC_STATS(net, LINUX_MIB_XFRMINHDRERROR);
  1108. goto done;
  1109. }
  1110. if (ipth->flags != 0) {
  1111. XFRM_INC_STATS(net, LINUX_MIB_XFRMINHDRERROR);
  1112. goto done;
  1113. }
  1114. INIT_LIST_HEAD(&sublist);
  1115. /* Handle fragment at start of payload, and/or waiting reassembly. */
  1116. blkoff = ntohs(ipth->block_offset);
  1117. /* check before locking i.e., maybe */
  1118. if (blkoff || xtfs->ra_runtlen || xtfs->ra_newskb) {
  1119. spin_lock(&xtfs->drop_lock);
  1120. /* check again after lock */
  1121. if (blkoff || xtfs->ra_runtlen || xtfs->ra_newskb) {
  1122. data = iptfs_reassem_cont(xtfs, seq, &skbseq, skb, data,
  1123. blkoff, &sublist);
  1124. }
  1125. spin_unlock(&xtfs->drop_lock);
  1126. }
  1127. /* New packets */
  1128. consumed = __input_process_payload(x, data, &skbseq, &sublist);
  1129. done:
  1130. if (!consumed) {
  1131. skb = skbseq.root_skb;
  1132. skb_abort_seq_read(&skbseq);
  1133. kfree_skb(skb);
  1134. }
  1135. }
  1136. /* ------------------------------- */
  1137. /* Input (Egress) Re-ordering Code */
  1138. /* ------------------------------- */
  1139. static void __vec_shift(struct xfrm_iptfs_data *xtfs, u32 shift)
  1140. {
  1141. u32 savedlen = xtfs->w_savedlen;
  1142. if (shift > savedlen)
  1143. shift = savedlen;
  1144. if (shift != savedlen)
  1145. memcpy(xtfs->w_saved, xtfs->w_saved + shift,
  1146. (savedlen - shift) * sizeof(*xtfs->w_saved));
  1147. memset(xtfs->w_saved + savedlen - shift, 0,
  1148. shift * sizeof(*xtfs->w_saved));
  1149. xtfs->w_savedlen -= shift;
  1150. }
  1151. static void __reorder_past(struct xfrm_iptfs_data *xtfs, struct sk_buff *inskb,
  1152. struct list_head *freelist)
  1153. {
  1154. list_add_tail(&inskb->list, freelist);
  1155. }
  1156. static u32 __reorder_drop(struct xfrm_iptfs_data *xtfs, struct list_head *list)
  1157. {
  1158. struct skb_wseq *s, *se;
  1159. const u32 savedlen = xtfs->w_savedlen;
  1160. time64_t now = ktime_get_raw_fast_ns();
  1161. u32 count = 0;
  1162. u32 scount = 0;
  1163. if (xtfs->w_saved[0].drop_time > now)
  1164. goto set_timer;
  1165. ++xtfs->w_wantseq;
  1166. /* Keep flushing packets until we reach a drop time greater than now. */
  1167. s = xtfs->w_saved;
  1168. se = s + savedlen;
  1169. do {
  1170. /* Walking past empty slots until we reach a packet */
  1171. for (; s < se && !s->skb; s++) {
  1172. if (s->drop_time > now)
  1173. goto outerdone;
  1174. }
  1175. /* Sending packets until we hit another empty slot. */
  1176. for (; s < se && s->skb; scount++, s++)
  1177. list_add_tail(&s->skb->list, list);
  1178. } while (s < se);
  1179. outerdone:
  1180. count = s - xtfs->w_saved;
  1181. if (count) {
  1182. xtfs->w_wantseq += count;
  1183. /* Shift handled slots plus final empty slot into slot 0. */
  1184. __vec_shift(xtfs, count);
  1185. }
  1186. if (xtfs->w_savedlen) {
  1187. set_timer:
  1188. /* Drifting is OK */
  1189. hrtimer_start(&xtfs->drop_timer,
  1190. xtfs->w_saved[0].drop_time - now,
  1191. IPTFS_HRTIMER_MODE);
  1192. }
  1193. return scount;
  1194. }
  1195. static void __reorder_this(struct xfrm_iptfs_data *xtfs, struct sk_buff *inskb,
  1196. struct list_head *list)
  1197. {
  1198. struct skb_wseq *s, *se;
  1199. const u32 savedlen = xtfs->w_savedlen;
  1200. u32 count = 0;
  1201. /* Got what we wanted. */
  1202. list_add_tail(&inskb->list, list);
  1203. ++xtfs->w_wantseq;
  1204. if (!savedlen)
  1205. return;
  1206. /* Flush remaining consecutive packets. */
  1207. /* Keep sending until we hit another missed pkt. */
  1208. for (s = xtfs->w_saved, se = s + savedlen; s < se && s->skb; s++)
  1209. list_add_tail(&s->skb->list, list);
  1210. count = s - xtfs->w_saved;
  1211. if (count)
  1212. xtfs->w_wantseq += count;
  1213. /* Shift handled slots plus final empty slot into slot 0. */
  1214. __vec_shift(xtfs, count + 1);
  1215. }
  1216. /* Set the slot's drop time and all the empty slots below it until reaching a
  1217. * filled slot which will already be set.
  1218. */
  1219. static void iptfs_set_window_drop_times(struct xfrm_iptfs_data *xtfs, int index)
  1220. {
  1221. const u32 savedlen = xtfs->w_savedlen;
  1222. struct skb_wseq *s = xtfs->w_saved;
  1223. time64_t drop_time;
  1224. assert_spin_locked(&xtfs->drop_lock);
  1225. if (savedlen > index + 1) {
  1226. /* we are below another, our drop time and the timer are already set */
  1227. return;
  1228. }
  1229. /* we are the most future so get a new drop time. */
  1230. drop_time = ktime_get_raw_fast_ns();
  1231. drop_time += xtfs->drop_time_ns;
  1232. /* Walk back through the array setting drop times as we go */
  1233. s[index].drop_time = drop_time;
  1234. while (index-- > 0 && !s[index].skb)
  1235. s[index].drop_time = drop_time;
  1236. /* If we walked all the way back, schedule the drop timer if needed */
  1237. if (index == -1 && !hrtimer_is_queued(&xtfs->drop_timer))
  1238. hrtimer_start(&xtfs->drop_timer, xtfs->drop_time_ns,
  1239. IPTFS_HRTIMER_MODE);
  1240. }
  1241. static void __reorder_future_fits(struct xfrm_iptfs_data *xtfs,
  1242. struct sk_buff *inskb,
  1243. struct list_head *freelist)
  1244. {
  1245. const u64 inseq = __esp_seq(inskb);
  1246. const u64 wantseq = xtfs->w_wantseq;
  1247. const u64 distance = inseq - wantseq;
  1248. const u32 savedlen = xtfs->w_savedlen;
  1249. const u32 index = distance - 1;
  1250. /* Handle future sequence number received which fits in the window.
  1251. *
  1252. * We know we don't have the seq we want so we won't be able to flush
  1253. * anything.
  1254. */
  1255. /* slot count is 4, saved size is 3 savedlen is 2
  1256. *
  1257. * "window boundary" is based on the fixed window size
  1258. * distance is also slot number
  1259. * index is an array index (i.e., - 1 of slot)
  1260. * : : - implicit NULL after array len
  1261. *
  1262. * +--------- used length (savedlen == 2)
  1263. * | +----- array size (nslots - 1 == 3)
  1264. * | | + window boundary (nslots == 4)
  1265. * V V | V
  1266. * |
  1267. * 0 1 2 3 | slot number
  1268. * --- 0 1 2 | array index
  1269. * [-] [b] : :| array
  1270. *
  1271. * "2" "3" "4" *5*| seq numbers
  1272. *
  1273. * We receive seq number 5
  1274. * distance == 3 [inseq(5) - w_wantseq(2)]
  1275. * index == 2 [distance(6) - 1]
  1276. */
  1277. if (xtfs->w_saved[index].skb) {
  1278. /* a dup of a future */
  1279. list_add_tail(&inskb->list, freelist);
  1280. return;
  1281. }
  1282. xtfs->w_saved[index].skb = inskb;
  1283. xtfs->w_savedlen = max(savedlen, index + 1);
  1284. iptfs_set_window_drop_times(xtfs, index);
  1285. }
  1286. static void __reorder_future_shifts(struct xfrm_iptfs_data *xtfs,
  1287. struct sk_buff *inskb,
  1288. struct list_head *list)
  1289. {
  1290. const u32 nslots = xtfs->cfg.reorder_win_size + 1;
  1291. const u64 inseq = __esp_seq(inskb);
  1292. u32 savedlen = xtfs->w_savedlen;
  1293. u64 wantseq = xtfs->w_wantseq;
  1294. struct skb_wseq *wnext;
  1295. struct sk_buff *slot0;
  1296. u32 beyond, shifting, slot;
  1297. u64 distance;
  1298. /* Handle future sequence number received.
  1299. *
  1300. * IMPORTANT: we are at least advancing w_wantseq (i.e., wantseq) by 1
  1301. * b/c we are beyond the window boundary.
  1302. *
  1303. * We know we don't have the wantseq so that counts as a drop.
  1304. */
  1305. /* example: slot count is 4, array size is 3 savedlen is 2, slot 0 is
  1306. * the missing sequence number.
  1307. *
  1308. * the final slot at savedlen (index savedlen - 1) is always occupied.
  1309. *
  1310. * beyond is "beyond array size" not savedlen.
  1311. *
  1312. * +--------- array length (savedlen == 2)
  1313. * | +----- array size (nslots - 1 == 3)
  1314. * | | +- window boundary (nslots == 4)
  1315. * V V |
  1316. * |
  1317. * 0 1 2 3 | slot number
  1318. * --- 0 1 2 | array index
  1319. * [b] [c] : :| array
  1320. * |
  1321. * "2" "3" "4" "5"|*6* seq numbers
  1322. *
  1323. * We receive seq number 6
  1324. * distance == 4 [inseq(6) - w_wantseq(2)]
  1325. * newslot == distance
  1326. * index == 3 [distance(4) - 1]
  1327. * beyond == 1 [newslot(4) - lastslot((nslots(4) - 1))]
  1328. * shifting == 1 [min(savedlen(2), beyond(1)]
  1329. * slot0_skb == [b], and should match w_wantseq
  1330. *
  1331. * +--- window boundary (nslots == 4)
  1332. * 0 1 2 3 | 4 slot number
  1333. * --- 0 1 2 | 3 array index
  1334. * [b] : : : :| array
  1335. * "2" "3" "4" "5" *6* seq numbers
  1336. *
  1337. * We receive seq number 6
  1338. * distance == 4 [inseq(6) - w_wantseq(2)]
  1339. * newslot == distance
  1340. * index == 3 [distance(4) - 1]
  1341. * beyond == 1 [newslot(4) - lastslot((nslots(4) - 1))]
  1342. * shifting == 1 [min(savedlen(1), beyond(1)]
  1343. * slot0_skb == [b] and should match w_wantseq
  1344. *
  1345. * +-- window boundary (nslots == 4)
  1346. * 0 1 2 3 | 4 5 6 slot number
  1347. * --- 0 1 2 | 3 4 5 array index
  1348. * [-] [c] : :| array
  1349. * "2" "3" "4" "5" "6" "7" *8* seq numbers
  1350. *
  1351. * savedlen = 2, beyond = 3
  1352. * iter 1: slot0 == NULL, missed++, lastdrop = 2 (2+1-1), slot0 = [-]
  1353. * iter 2: slot0 == NULL, missed++, lastdrop = 3 (2+2-1), slot0 = [c]
  1354. * 2 < 3, extra = 1 (3-2), missed += extra, lastdrop = 4 (2+2+1-1)
  1355. *
  1356. * We receive seq number 8
  1357. * distance == 6 [inseq(8) - w_wantseq(2)]
  1358. * newslot == distance
  1359. * index == 5 [distance(6) - 1]
  1360. * beyond == 3 [newslot(6) - lastslot((nslots(4) - 1))]
  1361. * shifting == 2 [min(savedlen(2), beyond(3)]
  1362. *
  1363. * slot0_skb == NULL changed from [b] when "savedlen < beyond" is true.
  1364. */
  1365. /* Now send any packets that are being shifted out of saved, and account
  1366. * for missing packets that are exiting the window as we shift it.
  1367. */
  1368. distance = inseq - wantseq;
  1369. beyond = distance - (nslots - 1);
  1370. /* If savedlen > beyond we are shifting some, else all. */
  1371. shifting = min(savedlen, beyond);
  1372. /* slot0 is the buf that just shifted out and into slot0 */
  1373. slot0 = NULL;
  1374. wnext = xtfs->w_saved;
  1375. for (slot = 1; slot <= shifting; slot++, wnext++) {
  1376. /* handle what was in slot0 before we occupy it */
  1377. if (slot0)
  1378. list_add_tail(&slot0->list, list);
  1379. slot0 = wnext->skb;
  1380. wnext->skb = NULL;
  1381. }
  1382. /* slot0 is now either NULL (in which case it's what we now are waiting
  1383. * for, or a buf in which case we need to handle it like we received it;
  1384. * however, we may be advancing past that buffer as well..
  1385. */
  1386. /* Handle case where we need to shift more than we had saved, slot0 will
  1387. * be NULL iff savedlen is 0, otherwise slot0 will always be
  1388. * non-NULL b/c we shifted the final element, which is always set if
  1389. * there is any saved, into slot0.
  1390. */
  1391. if (savedlen < beyond) {
  1392. if (savedlen != 0)
  1393. list_add_tail(&slot0->list, list);
  1394. slot0 = NULL;
  1395. /* slot0 has had an empty slot pushed into it */
  1396. }
  1397. /* Remove the entries */
  1398. __vec_shift(xtfs, beyond);
  1399. /* Advance want seq */
  1400. xtfs->w_wantseq += beyond;
  1401. /* Process drops here when implementing congestion control */
  1402. /* We've shifted. plug the packet in at the end. */
  1403. xtfs->w_savedlen = nslots - 1;
  1404. xtfs->w_saved[xtfs->w_savedlen - 1].skb = inskb;
  1405. iptfs_set_window_drop_times(xtfs, xtfs->w_savedlen - 1);
  1406. /* if we don't have a slot0 then we must wait for it */
  1407. if (!slot0)
  1408. return;
  1409. /* If slot0, seq must match new want seq */
  1410. /* slot0 is valid, treat like we received expected. */
  1411. __reorder_this(xtfs, slot0, list);
  1412. }
  1413. /* Receive a new packet into the reorder window. Return a list of ordered
  1414. * packets from the window.
  1415. */
  1416. static void iptfs_input_reorder(struct xfrm_iptfs_data *xtfs,
  1417. struct sk_buff *inskb, struct list_head *list,
  1418. struct list_head *freelist)
  1419. {
  1420. const u32 nslots = xtfs->cfg.reorder_win_size + 1;
  1421. u64 inseq = __esp_seq(inskb);
  1422. u64 wantseq;
  1423. assert_spin_locked(&xtfs->drop_lock);
  1424. if (unlikely(!xtfs->w_seq_set)) {
  1425. xtfs->w_seq_set = true;
  1426. xtfs->w_wantseq = inseq;
  1427. }
  1428. wantseq = xtfs->w_wantseq;
  1429. if (likely(inseq == wantseq))
  1430. __reorder_this(xtfs, inskb, list);
  1431. else if (inseq < wantseq)
  1432. __reorder_past(xtfs, inskb, freelist);
  1433. else if ((inseq - wantseq) < nslots)
  1434. __reorder_future_fits(xtfs, inskb, freelist);
  1435. else
  1436. __reorder_future_shifts(xtfs, inskb, list);
  1437. }
  1438. /**
  1439. * iptfs_drop_timer() - Handle drop timer expiry.
  1440. * @me: the timer
  1441. *
  1442. * This is similar to our input function.
  1443. *
  1444. * The drop timer is set when we start an in progress reassembly, and also when
  1445. * we save a future packet in the window saved array.
  1446. *
  1447. * NOTE packets in the save window are always newer WRT drop times as
  1448. * they get further in the future. i.e. for:
  1449. *
  1450. * if slots (S0, S1, ... Sn) and `Dn` is the drop time for slot `Sn`,
  1451. * then D(n-1) <= D(n).
  1452. *
  1453. * So, regardless of why the timer is firing we can always discard any inprogress
  1454. * fragment; either it's the reassembly timer, or slot 0 is going to be
  1455. * dropped as S0 must have the most recent drop time, and slot 0 holds the
  1456. * continuation fragment of the in progress packet.
  1457. *
  1458. * Returns HRTIMER_NORESTART.
  1459. */
  1460. static enum hrtimer_restart iptfs_drop_timer(struct hrtimer *me)
  1461. {
  1462. struct sk_buff *skb, *next;
  1463. struct list_head list;
  1464. struct xfrm_iptfs_data *xtfs;
  1465. struct xfrm_state *x;
  1466. u32 count;
  1467. xtfs = container_of(me, typeof(*xtfs), drop_timer);
  1468. x = xtfs->x;
  1469. INIT_LIST_HEAD(&list);
  1470. spin_lock(&xtfs->drop_lock);
  1471. /* Drop any in progress packet */
  1472. skb = xtfs->ra_newskb;
  1473. xtfs->ra_newskb = NULL;
  1474. /* Now drop as many packets as we should from the reordering window
  1475. * saved array
  1476. */
  1477. count = xtfs->w_savedlen ? __reorder_drop(xtfs, &list) : 0;
  1478. spin_unlock(&xtfs->drop_lock);
  1479. if (skb)
  1480. kfree_skb_reason(skb, SKB_DROP_REASON_FRAG_REASM_TIMEOUT);
  1481. if (count) {
  1482. list_for_each_entry_safe(skb, next, &list, list) {
  1483. skb_list_del_init(skb);
  1484. iptfs_input_ordered(x, skb);
  1485. }
  1486. }
  1487. return HRTIMER_NORESTART;
  1488. }
  1489. /**
  1490. * iptfs_input() - handle receipt of iptfs payload
  1491. * @x: xfrm state
  1492. * @skb: the packet
  1493. *
  1494. * We have an IPTFS payload order it if needed, then process newly in order
  1495. * packets.
  1496. *
  1497. * Return: -EINPROGRESS to inform xfrm_input to stop processing the skb.
  1498. */
  1499. static int iptfs_input(struct xfrm_state *x, struct sk_buff *skb)
  1500. {
  1501. struct list_head freelist, list;
  1502. struct xfrm_iptfs_data *xtfs = x->mode_data;
  1503. struct sk_buff *next;
  1504. /* Fast path for no reorder window. */
  1505. if (xtfs->cfg.reorder_win_size == 0) {
  1506. iptfs_input_ordered(x, skb);
  1507. goto done;
  1508. }
  1509. /* Fetch list of in-order packets from the reordering window as well as
  1510. * a list of buffers we need to now free.
  1511. */
  1512. INIT_LIST_HEAD(&list);
  1513. INIT_LIST_HEAD(&freelist);
  1514. spin_lock(&xtfs->drop_lock);
  1515. iptfs_input_reorder(xtfs, skb, &list, &freelist);
  1516. spin_unlock(&xtfs->drop_lock);
  1517. list_for_each_entry_safe(skb, next, &list, list) {
  1518. skb_list_del_init(skb);
  1519. iptfs_input_ordered(x, skb);
  1520. }
  1521. list_for_each_entry_safe(skb, next, &freelist, list) {
  1522. skb_list_del_init(skb);
  1523. kfree_skb(skb);
  1524. }
  1525. done:
  1526. /* We always have dealt with the input SKB, either we are re-using it,
  1527. * or we have freed it. Return EINPROGRESS so that xfrm_input stops
  1528. * processing it.
  1529. */
  1530. return -EINPROGRESS;
  1531. }
  1532. /* ================================= */
  1533. /* IPTFS Sending (ingress) Functions */
  1534. /* ================================= */
  1535. /* ------------------------- */
  1536. /* Enqueue to send functions */
  1537. /* ------------------------- */
  1538. /**
  1539. * iptfs_enqueue() - enqueue packet if ok to send.
  1540. * @xtfs: xtfs state
  1541. * @skb: the packet
  1542. *
  1543. * Return: true if packet enqueued.
  1544. */
  1545. static bool iptfs_enqueue(struct xfrm_iptfs_data *xtfs, struct sk_buff *skb)
  1546. {
  1547. u64 newsz = xtfs->queue_size + skb->len;
  1548. struct iphdr *iph;
  1549. assert_spin_locked(&xtfs->x->lock);
  1550. if (newsz > xtfs->cfg.max_queue_size)
  1551. return false;
  1552. /* Set ECN CE if we are above our ECN queue threshold */
  1553. if (newsz > xtfs->ecn_queue_size) {
  1554. iph = ip_hdr(skb);
  1555. if (iph->version == 4)
  1556. IP_ECN_set_ce(iph);
  1557. else if (iph->version == 6)
  1558. IP6_ECN_set_ce(skb, ipv6_hdr(skb));
  1559. }
  1560. __skb_queue_tail(&xtfs->queue, skb);
  1561. xtfs->queue_size += skb->len;
  1562. return true;
  1563. }
  1564. static int iptfs_get_cur_pmtu(struct xfrm_state *x, struct xfrm_iptfs_data *xtfs,
  1565. struct sk_buff *skb)
  1566. {
  1567. struct xfrm_dst *xdst = (struct xfrm_dst *)skb_dst(skb);
  1568. u32 payload_mtu = xtfs->payload_mtu;
  1569. u32 pmtu = __iptfs_get_inner_mtu(x, xdst->child_mtu_cached);
  1570. if (payload_mtu && payload_mtu < pmtu)
  1571. pmtu = payload_mtu;
  1572. return pmtu;
  1573. }
  1574. static int iptfs_is_too_big(struct sock *sk, struct sk_buff *skb, u32 pmtu)
  1575. {
  1576. if (skb->len <= pmtu)
  1577. return 0;
  1578. /* We only send ICMP too big if the user has configured us as
  1579. * dont-fragment.
  1580. */
  1581. if (skb->dev)
  1582. XFRM_INC_STATS(dev_net(skb->dev), LINUX_MIB_XFRMOUTERROR);
  1583. if (sk)
  1584. xfrm_local_error(skb, pmtu);
  1585. else if (ip_hdr(skb)->version == 4)
  1586. icmp_send(skb, ICMP_DEST_UNREACH, ICMP_FRAG_NEEDED, htonl(pmtu));
  1587. else
  1588. icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, pmtu);
  1589. return 1;
  1590. }
  1591. /* IPv4/IPv6 packet ingress to IPTFS tunnel, arrange to send in IPTFS payload
  1592. * (i.e., aggregating or fragmenting as appropriate).
  1593. * This is set in dst->output for an SA.
  1594. */
  1595. static int iptfs_output_collect(struct net *net, struct sock *sk, struct sk_buff *skb)
  1596. {
  1597. struct dst_entry *dst = skb_dst(skb);
  1598. struct xfrm_state *x = dst->xfrm;
  1599. struct xfrm_iptfs_data *xtfs = x->mode_data;
  1600. struct sk_buff *segs, *nskb;
  1601. u32 pmtu = 0;
  1602. bool ok = true;
  1603. bool was_gso;
  1604. /* We have hooked into dst_entry->output which means we have skipped the
  1605. * protocol specific netfilter (see xfrm4_output, xfrm6_output).
  1606. * when our timer runs we will end up calling xfrm_output directly on
  1607. * the encapsulated traffic.
  1608. *
  1609. * For both cases this is the NF_INET_POST_ROUTING hook which allows
  1610. * changing the skb->dst entry which then may not be xfrm based anymore
  1611. * in which case a REROUTED flag is set. and dst_output is called.
  1612. *
  1613. * For IPv6 we are also skipping fragmentation handling for local
  1614. * sockets, which may or may not be good depending on our tunnel DF
  1615. * setting. Normally with fragmentation supported we want to skip this
  1616. * fragmentation.
  1617. */
  1618. if (xtfs->cfg.dont_frag)
  1619. pmtu = iptfs_get_cur_pmtu(x, xtfs, skb);
  1620. /* Break apart GSO skbs. If the queue is nearing full then we want the
  1621. * accounting and queuing to be based on the individual packets not on the
  1622. * aggregate GSO buffer.
  1623. */
  1624. was_gso = skb_is_gso(skb);
  1625. if (!was_gso) {
  1626. segs = skb;
  1627. } else {
  1628. segs = skb_gso_segment(skb, 0);
  1629. if (IS_ERR_OR_NULL(segs)) {
  1630. XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTERROR);
  1631. kfree_skb(skb);
  1632. if (IS_ERR(segs))
  1633. return PTR_ERR(segs);
  1634. return -EINVAL;
  1635. }
  1636. consume_skb(skb);
  1637. skb = NULL;
  1638. }
  1639. /* We can be running on multiple cores and from the network softirq or
  1640. * from user context depending on where the packet is coming from.
  1641. */
  1642. spin_lock_bh(&x->lock);
  1643. skb_list_walk_safe(segs, skb, nskb) {
  1644. skb_mark_not_on_list(skb);
  1645. /* Once we drop due to no queue space we continue to drop the
  1646. * rest of the packets from that GRO.
  1647. */
  1648. if (!ok) {
  1649. nospace:
  1650. trace_iptfs_no_queue_space(skb, xtfs, pmtu, was_gso);
  1651. XFRM_INC_STATS(net, LINUX_MIB_XFRMOUTNOQSPACE);
  1652. kfree_skb_reason(skb, SKB_DROP_REASON_FULL_RING);
  1653. continue;
  1654. }
  1655. /* If the user indicated no iptfs fragmenting check before
  1656. * enqueue.
  1657. */
  1658. if (xtfs->cfg.dont_frag && iptfs_is_too_big(sk, skb, pmtu)) {
  1659. trace_iptfs_too_big(skb, xtfs, pmtu, was_gso);
  1660. kfree_skb_reason(skb, SKB_DROP_REASON_PKT_TOO_BIG);
  1661. continue;
  1662. }
  1663. /* Enqueue to send in tunnel */
  1664. ok = iptfs_enqueue(xtfs, skb);
  1665. if (!ok)
  1666. goto nospace;
  1667. trace_iptfs_enqueue(skb, xtfs, pmtu, was_gso);
  1668. }
  1669. /* Start a delay timer if we don't have one yet */
  1670. if (!hrtimer_is_queued(&xtfs->iptfs_timer)) {
  1671. hrtimer_start(&xtfs->iptfs_timer, xtfs->init_delay_ns, IPTFS_HRTIMER_MODE);
  1672. xtfs->iptfs_settime = ktime_get_raw_fast_ns();
  1673. trace_iptfs_timer_start(xtfs, xtfs->init_delay_ns);
  1674. }
  1675. spin_unlock_bh(&x->lock);
  1676. return 0;
  1677. }
  1678. /* -------------------------- */
  1679. /* Dequeue and send functions */
  1680. /* -------------------------- */
  1681. static void iptfs_output_prepare_skb(struct sk_buff *skb, u32 blkoff)
  1682. {
  1683. struct ip_iptfs_hdr *h;
  1684. size_t hsz = sizeof(*h);
  1685. /* now reset values to be pointing at the rest of the packets */
  1686. h = skb_push(skb, hsz);
  1687. memset(h, 0, hsz);
  1688. if (blkoff)
  1689. h->block_offset = htons(blkoff);
  1690. /* network_header current points at the inner IP packet
  1691. * move it to the iptfs header
  1692. */
  1693. skb->transport_header = skb->network_header;
  1694. skb->network_header -= hsz;
  1695. IPCB(skb)->flags |= IPSKB_XFRM_TUNNEL_SIZE;
  1696. }
  1697. /**
  1698. * iptfs_copy_create_frag() - create an inner fragment skb.
  1699. * @st: The source packet data.
  1700. * @offset: offset in @st of the new fragment data.
  1701. * @copy_len: the amount of data to copy from @st.
  1702. *
  1703. * Create a new skb holding a single IPTFS inner packet fragment. @copy_len must
  1704. * not be greater than the max fragment size.
  1705. *
  1706. * Return: the new fragment skb or an ERR_PTR().
  1707. */
  1708. static struct sk_buff *iptfs_copy_create_frag(struct skb_seq_state *st, u32 offset, u32 copy_len)
  1709. {
  1710. struct sk_buff *src = st->root_skb;
  1711. struct sk_buff *skb;
  1712. int err;
  1713. skb = iptfs_alloc_skb(src, copy_len, true);
  1714. if (!skb)
  1715. return ERR_PTR(-ENOMEM);
  1716. /* Now copy `copy_len` data from src */
  1717. err = skb_copy_seq_read(st, offset, skb_put(skb, copy_len), copy_len);
  1718. if (err) {
  1719. kfree_skb(skb);
  1720. return ERR_PTR(err);
  1721. }
  1722. return skb;
  1723. }
  1724. /**
  1725. * iptfs_copy_create_frags() - create and send N-1 fragments of a larger skb.
  1726. * @skbp: the source packet skb (IN), skb holding the last fragment in
  1727. * the fragment stream (OUT).
  1728. * @xtfs: IPTFS SA state.
  1729. * @mtu: the max IPTFS fragment size.
  1730. *
  1731. * This function is responsible for fragmenting a larger inner packet into a
  1732. * sequence of IPTFS payload packets. The last fragment is returned rather than
  1733. * being sent so that the caller can append more inner packets (aggregation) if
  1734. * there is room.
  1735. *
  1736. * Return: 0 on success or a negative error code on failure
  1737. */
  1738. static int iptfs_copy_create_frags(struct sk_buff **skbp, struct xfrm_iptfs_data *xtfs, u32 mtu)
  1739. {
  1740. struct skb_seq_state skbseq;
  1741. struct list_head sublist;
  1742. struct sk_buff *skb = *skbp;
  1743. struct sk_buff *nskb = *skbp;
  1744. u32 copy_len, offset;
  1745. u32 to_copy = skb->len - mtu;
  1746. u32 blkoff = 0;
  1747. int err = 0;
  1748. INIT_LIST_HEAD(&sublist);
  1749. skb_prepare_seq_read(skb, 0, skb->len, &skbseq);
  1750. /* A trimmed `skb` will be sent as the first fragment, later. */
  1751. offset = mtu;
  1752. to_copy = skb->len - offset;
  1753. while (to_copy) {
  1754. /* Send all but last fragment to allow agg. append */
  1755. trace_iptfs_first_fragmenting(nskb, mtu, to_copy, NULL);
  1756. list_add_tail(&nskb->list, &sublist);
  1757. /* FUTURE: if the packet has an odd/non-aligning length we could
  1758. * send less data in the penultimate fragment so that the last
  1759. * fragment then ends on an aligned boundary.
  1760. */
  1761. copy_len = min(to_copy, mtu);
  1762. nskb = iptfs_copy_create_frag(&skbseq, offset, copy_len);
  1763. if (IS_ERR(nskb)) {
  1764. XFRM_INC_STATS(xs_net(xtfs->x), LINUX_MIB_XFRMOUTERROR);
  1765. skb_abort_seq_read(&skbseq);
  1766. err = PTR_ERR(nskb);
  1767. nskb = NULL;
  1768. break;
  1769. }
  1770. iptfs_output_prepare_skb(nskb, to_copy);
  1771. offset += copy_len;
  1772. to_copy -= copy_len;
  1773. blkoff = to_copy;
  1774. }
  1775. skb_abort_seq_read(&skbseq);
  1776. /* return last fragment that will be unsent (or NULL) */
  1777. *skbp = nskb;
  1778. if (nskb)
  1779. trace_iptfs_first_final_fragment(nskb, mtu, blkoff, NULL);
  1780. /* trim the original skb to MTU */
  1781. if (!err)
  1782. err = pskb_trim(skb, mtu);
  1783. if (err) {
  1784. /* Free all frags. Don't bother sending a partial packet we will
  1785. * never complete.
  1786. */
  1787. kfree_skb(nskb);
  1788. list_for_each_entry_safe(skb, nskb, &sublist, list) {
  1789. skb_list_del_init(skb);
  1790. kfree_skb(skb);
  1791. }
  1792. return err;
  1793. }
  1794. /* prepare the initial fragment with an iptfs header */
  1795. iptfs_output_prepare_skb(skb, 0);
  1796. /* Send all but last fragment, if we fail to send a fragment then free
  1797. * the rest -- no point in sending a packet that can't be reassembled.
  1798. */
  1799. list_for_each_entry_safe(skb, nskb, &sublist, list) {
  1800. skb_list_del_init(skb);
  1801. if (!err)
  1802. err = xfrm_output(NULL, skb);
  1803. else
  1804. kfree_skb(skb);
  1805. }
  1806. if (err)
  1807. kfree_skb(*skbp);
  1808. return err;
  1809. }
  1810. /**
  1811. * iptfs_first_skb() - handle the first dequeued inner packet for output
  1812. * @skbp: the source packet skb (IN), skb holding the last fragment in
  1813. * the fragment stream (OUT).
  1814. * @xtfs: IPTFS SA state.
  1815. * @mtu: the max IPTFS fragment size.
  1816. *
  1817. * This function is responsible for fragmenting a larger inner packet into a
  1818. * sequence of IPTFS payload packets.
  1819. *
  1820. * The last fragment is returned rather than being sent so that the caller can
  1821. * append more inner packets (aggregation) if there is room.
  1822. *
  1823. * Return: 0 on success or a negative error code on failure
  1824. */
  1825. static int iptfs_first_skb(struct sk_buff **skbp, struct xfrm_iptfs_data *xtfs, u32 mtu)
  1826. {
  1827. struct sk_buff *skb = *skbp;
  1828. int err;
  1829. /* Classic ESP skips the don't fragment ICMP error if DF is clear on
  1830. * the inner packet or ignore_df is set. Otherwise it will send an ICMP
  1831. * or local error if the inner packet won't fit it's MTU.
  1832. *
  1833. * With IPTFS we do not care about the inner packet DF bit. If the
  1834. * tunnel is configured to "don't fragment" we error back if things
  1835. * don't fit in our max packet size. Otherwise we iptfs-fragment as
  1836. * normal.
  1837. */
  1838. /* The opportunity for HW offload has ended */
  1839. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  1840. err = skb_checksum_help(skb);
  1841. if (err)
  1842. return err;
  1843. }
  1844. /* We've split gso up before queuing */
  1845. trace_iptfs_first_dequeue(skb, mtu, 0, ip_hdr(skb));
  1846. /* Consider the buffer Tx'd and no longer owned */
  1847. skb_orphan(skb);
  1848. /* Simple case -- it fits. `mtu` accounted for all the overhead
  1849. * including the basic IPTFS header.
  1850. */
  1851. if (skb->len <= mtu) {
  1852. iptfs_output_prepare_skb(skb, 0);
  1853. return 0;
  1854. }
  1855. return iptfs_copy_create_frags(skbp, xtfs, mtu);
  1856. }
  1857. static struct sk_buff **iptfs_rehome_fraglist(struct sk_buff **nextp, struct sk_buff *child)
  1858. {
  1859. u32 fllen = 0;
  1860. /* It might be possible to account for a frag list in addition to page
  1861. * fragment if it's a valid state to be in. The page fragments size
  1862. * should be kept as data_len so only the frag_list size is removed,
  1863. * this must be done above as well.
  1864. */
  1865. *nextp = skb_shinfo(child)->frag_list;
  1866. while (*nextp) {
  1867. fllen += (*nextp)->len;
  1868. nextp = &(*nextp)->next;
  1869. }
  1870. skb_frag_list_init(child);
  1871. child->len -= fllen;
  1872. child->data_len -= fllen;
  1873. return nextp;
  1874. }
  1875. static void iptfs_consume_frags(struct sk_buff *to, struct sk_buff *from)
  1876. {
  1877. struct skb_shared_info *fromi = skb_shinfo(from);
  1878. struct skb_shared_info *toi = skb_shinfo(to);
  1879. unsigned int new_truesize;
  1880. /* If we have data in a head page, grab it */
  1881. if (!skb_headlen(from)) {
  1882. new_truesize = SKB_TRUESIZE(skb_end_offset(from));
  1883. } else {
  1884. iptfs_skb_head_to_frag(from, &toi->frags[toi->nr_frags]);
  1885. skb_frag_ref(to, toi->nr_frags++);
  1886. new_truesize = SKB_DATA_ALIGN(sizeof(struct sk_buff));
  1887. }
  1888. /* Move any other page fragments rather than copy */
  1889. memcpy(&toi->frags[toi->nr_frags], fromi->frags,
  1890. sizeof(fromi->frags[0]) * fromi->nr_frags);
  1891. toi->nr_frags += fromi->nr_frags;
  1892. fromi->nr_frags = 0;
  1893. from->data_len = 0;
  1894. from->len = 0;
  1895. to->truesize += from->truesize - new_truesize;
  1896. from->truesize = new_truesize;
  1897. /* We are done with this SKB */
  1898. consume_skb(from);
  1899. }
  1900. static void iptfs_output_queued(struct xfrm_state *x, struct sk_buff_head *list)
  1901. {
  1902. struct xfrm_iptfs_data *xtfs = x->mode_data;
  1903. struct sk_buff *skb, *skb2, **nextp;
  1904. struct skb_shared_info *shi, *shi2;
  1905. /* If we are fragmenting due to a large inner packet we will output all
  1906. * the outer IPTFS packets required to contain the fragments of the
  1907. * single large inner packet. These outer packets need to be sent
  1908. * consecutively (ESP seq-wise). Since this output function is always
  1909. * running from a timer we do not need a lock to provide this guarantee.
  1910. * We will output our packets consecutively before the timer is allowed
  1911. * to run again on some other CPU.
  1912. */
  1913. while ((skb = __skb_dequeue(list))) {
  1914. u32 mtu = iptfs_get_cur_pmtu(x, xtfs, skb);
  1915. bool share_ok = true;
  1916. int remaining;
  1917. /* protocol comes to us cleared sometimes */
  1918. skb->protocol = x->outer_mode.family == AF_INET ? htons(ETH_P_IP) :
  1919. htons(ETH_P_IPV6);
  1920. if (skb->len > mtu && xtfs->cfg.dont_frag) {
  1921. /* We handle this case before enqueueing so we are only
  1922. * here b/c MTU changed after we enqueued before we
  1923. * dequeued, just drop these.
  1924. */
  1925. XFRM_INC_STATS(xs_net(x), LINUX_MIB_XFRMOUTERROR);
  1926. trace_iptfs_first_toobig(skb, mtu, 0, ip_hdr(skb));
  1927. kfree_skb_reason(skb, SKB_DROP_REASON_PKT_TOO_BIG);
  1928. continue;
  1929. }
  1930. /* Convert first inner packet into an outer IPTFS packet,
  1931. * dealing with any fragmentation into multiple outer packets
  1932. * if necessary.
  1933. */
  1934. if (iptfs_first_skb(&skb, xtfs, mtu))
  1935. continue;
  1936. /* If fragmentation was required the returned skb is the last
  1937. * IPTFS fragment in the chain, and it's IPTFS header blkoff has
  1938. * been set just past the end of the fragment data.
  1939. *
  1940. * In either case the space remaining to send more inner packet
  1941. * data is `mtu` - (skb->len - sizeof iptfs header). This is b/c
  1942. * the `mtu` value has the basic IPTFS header len accounted for,
  1943. * and we added that header to the skb so it is a part of
  1944. * skb->len, thus we subtract it from the skb length.
  1945. */
  1946. remaining = mtu - (skb->len - sizeof(struct ip_iptfs_hdr));
  1947. /* Re-home (un-nest) nested fragment lists. We need to do this
  1948. * b/c we will simply be appending any following aggregated
  1949. * inner packets using the frag list.
  1950. */
  1951. shi = skb_shinfo(skb);
  1952. nextp = &shi->frag_list;
  1953. while (*nextp) {
  1954. if (skb_has_frag_list(*nextp))
  1955. nextp = iptfs_rehome_fraglist(&(*nextp)->next, *nextp);
  1956. else
  1957. nextp = &(*nextp)->next;
  1958. }
  1959. if (shi->frag_list || skb_cloned(skb) || skb_shared(skb))
  1960. share_ok = false;
  1961. /* See if we have enough space to simply append.
  1962. *
  1963. * NOTE: Maybe do not append if we will be mis-aligned,
  1964. * SW-based endpoints will probably have to copy in this
  1965. * case.
  1966. */
  1967. while ((skb2 = skb_peek(list))) {
  1968. trace_iptfs_ingress_nth_peek(skb2, remaining);
  1969. if (skb2->len > remaining)
  1970. break;
  1971. __skb_unlink(skb2, list);
  1972. /* Consider the buffer Tx'd and no longer owned */
  1973. skb_orphan(skb);
  1974. /* If we don't have a cksum in the packet we need to add
  1975. * one before encapsulation.
  1976. */
  1977. if (skb2->ip_summed == CHECKSUM_PARTIAL) {
  1978. if (skb_checksum_help(skb2)) {
  1979. XFRM_INC_STATS(xs_net(x), LINUX_MIB_XFRMOUTERROR);
  1980. kfree_skb(skb2);
  1981. continue;
  1982. }
  1983. }
  1984. /* skb->pp_recycle is passed to __skb_flag_unref for all
  1985. * frag pages so we can only share pages with skb's who
  1986. * match ourselves.
  1987. */
  1988. shi2 = skb_shinfo(skb2);
  1989. if (share_ok &&
  1990. (shi2->frag_list ||
  1991. (!skb2->head_frag && skb_headlen(skb)) ||
  1992. skb->pp_recycle != skb2->pp_recycle ||
  1993. skb_zcopy(skb2) ||
  1994. (shi->nr_frags + shi2->nr_frags + 1 > MAX_SKB_FRAGS)))
  1995. share_ok = false;
  1996. /* Do accounting */
  1997. skb->data_len += skb2->len;
  1998. skb->len += skb2->len;
  1999. remaining -= skb2->len;
  2000. trace_iptfs_ingress_nth_add(skb2, share_ok);
  2001. if (share_ok) {
  2002. iptfs_consume_frags(skb, skb2);
  2003. } else {
  2004. /* Append to the frag_list */
  2005. *nextp = skb2;
  2006. nextp = &skb2->next;
  2007. if (skb_has_frag_list(skb2))
  2008. nextp = iptfs_rehome_fraglist(nextp,
  2009. skb2);
  2010. skb->truesize += skb2->truesize;
  2011. }
  2012. }
  2013. xfrm_output(NULL, skb);
  2014. }
  2015. }
  2016. static enum hrtimer_restart iptfs_delay_timer(struct hrtimer *me)
  2017. {
  2018. struct sk_buff_head list;
  2019. struct xfrm_iptfs_data *xtfs;
  2020. struct xfrm_state *x;
  2021. time64_t settime;
  2022. xtfs = container_of(me, typeof(*xtfs), iptfs_timer);
  2023. x = xtfs->x;
  2024. /* Process all the queued packets
  2025. *
  2026. * softirq execution order: timer > tasklet > hrtimer
  2027. *
  2028. * Network rx will have run before us giving one last chance to queue
  2029. * ingress packets for us to process and transmit.
  2030. */
  2031. spin_lock(&x->lock);
  2032. __skb_queue_head_init(&list);
  2033. skb_queue_splice_init(&xtfs->queue, &list);
  2034. xtfs->queue_size = 0;
  2035. settime = xtfs->iptfs_settime;
  2036. spin_unlock(&x->lock);
  2037. /* After the above unlock, packets can begin queuing again, and the
  2038. * timer can be set again, from another CPU either in softirq or user
  2039. * context (not from this one since we are running at softirq level
  2040. * already).
  2041. */
  2042. trace_iptfs_timer_expire(xtfs, (unsigned long long)(ktime_get_raw_fast_ns() - settime));
  2043. iptfs_output_queued(x, &list);
  2044. return HRTIMER_NORESTART;
  2045. }
  2046. /**
  2047. * iptfs_encap_add_ipv4() - add outer encaps
  2048. * @x: xfrm state
  2049. * @skb: the packet
  2050. *
  2051. * This was originally taken from xfrm4_tunnel_encap_add. The reason for the
  2052. * copy is that IP-TFS/AGGFRAG can have different functionality for how to set
  2053. * the TOS/DSCP bits. Sets the protocol to a different value and doesn't do
  2054. * anything with inner headers as they aren't pointing into a normal IP
  2055. * singleton inner packet.
  2056. *
  2057. * Return: 0 on success or a negative error code on failure
  2058. */
  2059. static int iptfs_encap_add_ipv4(struct xfrm_state *x, struct sk_buff *skb)
  2060. {
  2061. struct dst_entry *dst = skb_dst(skb);
  2062. struct iphdr *top_iph;
  2063. skb_reset_inner_network_header(skb);
  2064. skb_reset_inner_transport_header(skb);
  2065. skb_set_network_header(skb, -(x->props.header_len - x->props.enc_hdr_len));
  2066. skb->mac_header = skb->network_header + offsetof(struct iphdr, protocol);
  2067. skb->transport_header = skb->network_header + sizeof(*top_iph);
  2068. top_iph = ip_hdr(skb);
  2069. top_iph->ihl = 5;
  2070. top_iph->version = 4;
  2071. top_iph->protocol = IPPROTO_AGGFRAG;
  2072. /* As we have 0, fractional, 1 or N inner packets there's no obviously
  2073. * correct DSCP mapping to inherit. ECN should be cleared per RFC9347
  2074. * 3.1.
  2075. */
  2076. top_iph->tos = 0;
  2077. top_iph->frag_off = htons(IP_DF);
  2078. top_iph->ttl = ip4_dst_hoplimit(xfrm_dst_child(dst));
  2079. top_iph->saddr = x->props.saddr.a4;
  2080. top_iph->daddr = x->id.daddr.a4;
  2081. ip_select_ident(dev_net(dst->dev), skb, NULL);
  2082. return 0;
  2083. }
  2084. #if IS_ENABLED(CONFIG_IPV6)
  2085. /**
  2086. * iptfs_encap_add_ipv6() - add outer encaps
  2087. * @x: xfrm state
  2088. * @skb: the packet
  2089. *
  2090. * This was originally taken from xfrm6_tunnel_encap_add. The reason for the
  2091. * copy is that IP-TFS/AGGFRAG can have different functionality for how to set
  2092. * the flow label and TOS/DSCP bits. It also sets the protocol to a different
  2093. * value and doesn't do anything with inner headers as they aren't pointing into
  2094. * a normal IP singleton inner packet.
  2095. *
  2096. * Return: 0 on success or a negative error code on failure
  2097. */
  2098. static int iptfs_encap_add_ipv6(struct xfrm_state *x, struct sk_buff *skb)
  2099. {
  2100. struct dst_entry *dst = skb_dst(skb);
  2101. struct ipv6hdr *top_iph;
  2102. int dsfield;
  2103. skb_reset_inner_network_header(skb);
  2104. skb_reset_inner_transport_header(skb);
  2105. skb_set_network_header(skb, -x->props.header_len + x->props.enc_hdr_len);
  2106. skb->mac_header = skb->network_header + offsetof(struct ipv6hdr, nexthdr);
  2107. skb->transport_header = skb->network_header + sizeof(*top_iph);
  2108. top_iph = ipv6_hdr(skb);
  2109. top_iph->version = 6;
  2110. top_iph->priority = 0;
  2111. memset(top_iph->flow_lbl, 0, sizeof(top_iph->flow_lbl));
  2112. top_iph->nexthdr = IPPROTO_AGGFRAG;
  2113. /* As we have 0, fractional, 1 or N inner packets there's no obviously
  2114. * correct DSCP mapping to inherit. ECN should be cleared per RFC9347
  2115. * 3.1.
  2116. */
  2117. dsfield = 0;
  2118. ipv6_change_dsfield(top_iph, 0, dsfield);
  2119. top_iph->hop_limit = ip6_dst_hoplimit(xfrm_dst_child(dst));
  2120. top_iph->saddr = *(struct in6_addr *)&x->props.saddr;
  2121. top_iph->daddr = *(struct in6_addr *)&x->id.daddr;
  2122. return 0;
  2123. }
  2124. #endif
  2125. /**
  2126. * iptfs_prepare_output() - prepare the skb for output
  2127. * @x: xfrm state
  2128. * @skb: the packet
  2129. *
  2130. * Return: Error value, if 0 then skb values should be as follows:
  2131. * - transport_header should point at ESP header
  2132. * - network_header should point at Outer IP header
  2133. * - mac_header should point at protocol/nexthdr of the outer IP
  2134. */
  2135. static int iptfs_prepare_output(struct xfrm_state *x, struct sk_buff *skb)
  2136. {
  2137. if (x->outer_mode.family == AF_INET)
  2138. return iptfs_encap_add_ipv4(x, skb);
  2139. if (x->outer_mode.family == AF_INET6) {
  2140. #if IS_ENABLED(CONFIG_IPV6)
  2141. return iptfs_encap_add_ipv6(x, skb);
  2142. #else
  2143. return -EAFNOSUPPORT;
  2144. #endif
  2145. }
  2146. return -EOPNOTSUPP;
  2147. }
  2148. /* ========================== */
  2149. /* State Management Functions */
  2150. /* ========================== */
  2151. /**
  2152. * __iptfs_get_inner_mtu() - return inner MTU with no fragmentation.
  2153. * @x: xfrm state.
  2154. * @outer_mtu: the outer mtu
  2155. *
  2156. * Return: Correct MTU taking in to account the encap overhead.
  2157. */
  2158. static u32 __iptfs_get_inner_mtu(struct xfrm_state *x, int outer_mtu)
  2159. {
  2160. struct crypto_aead *aead;
  2161. u32 blksize;
  2162. aead = x->data;
  2163. blksize = ALIGN(crypto_aead_blocksize(aead), 4);
  2164. return ((outer_mtu - x->props.header_len - crypto_aead_authsize(aead)) &
  2165. ~(blksize - 1)) - 2;
  2166. }
  2167. /**
  2168. * iptfs_get_inner_mtu() - return the inner MTU for an IPTFS xfrm.
  2169. * @x: xfrm state.
  2170. * @outer_mtu: Outer MTU for the encapsulated packet.
  2171. *
  2172. * Return: Correct MTU taking in to account the encap overhead.
  2173. */
  2174. static u32 iptfs_get_inner_mtu(struct xfrm_state *x, int outer_mtu)
  2175. {
  2176. struct xfrm_iptfs_data *xtfs = x->mode_data;
  2177. /* If not dont-frag we have no MTU */
  2178. if (!xtfs->cfg.dont_frag)
  2179. return x->outer_mode.family == AF_INET ? IP_MAX_MTU : IP6_MAX_MTU;
  2180. return __iptfs_get_inner_mtu(x, outer_mtu);
  2181. }
  2182. /**
  2183. * iptfs_user_init() - initialize the SA with IPTFS options from netlink.
  2184. * @net: the net data
  2185. * @x: xfrm state
  2186. * @attrs: netlink attributes
  2187. * @extack: extack return data
  2188. *
  2189. * Return: 0 on success or a negative error code on failure
  2190. */
  2191. static int iptfs_user_init(struct net *net, struct xfrm_state *x,
  2192. struct nlattr **attrs,
  2193. struct netlink_ext_ack *extack)
  2194. {
  2195. struct xfrm_iptfs_data *xtfs = x->mode_data;
  2196. struct xfrm_iptfs_config *xc;
  2197. u64 q;
  2198. xc = &xtfs->cfg;
  2199. xc->max_queue_size = IPTFS_DEFAULT_MAX_QUEUE_SIZE;
  2200. xc->reorder_win_size = IPTFS_DEFAULT_REORDER_WINDOW;
  2201. xtfs->drop_time_ns = IPTFS_DEFAULT_DROP_TIME_USECS * NSECS_IN_USEC;
  2202. xtfs->init_delay_ns = IPTFS_DEFAULT_INIT_DELAY_USECS * NSECS_IN_USEC;
  2203. if (attrs[XFRMA_IPTFS_DONT_FRAG])
  2204. xc->dont_frag = true;
  2205. if (attrs[XFRMA_IPTFS_REORDER_WINDOW])
  2206. xc->reorder_win_size =
  2207. nla_get_u16(attrs[XFRMA_IPTFS_REORDER_WINDOW]);
  2208. /* saved array is for saving 1..N seq nums from wantseq */
  2209. if (xc->reorder_win_size) {
  2210. xtfs->w_saved = kzalloc_objs(*xtfs->w_saved,
  2211. xc->reorder_win_size);
  2212. if (!xtfs->w_saved) {
  2213. NL_SET_ERR_MSG(extack, "Cannot alloc reorder window");
  2214. return -ENOMEM;
  2215. }
  2216. }
  2217. if (attrs[XFRMA_IPTFS_PKT_SIZE]) {
  2218. xc->pkt_size = nla_get_u32(attrs[XFRMA_IPTFS_PKT_SIZE]);
  2219. if (!xc->pkt_size) {
  2220. xtfs->payload_mtu = 0;
  2221. } else if (xc->pkt_size > x->props.header_len) {
  2222. xtfs->payload_mtu = xc->pkt_size - x->props.header_len;
  2223. } else {
  2224. NL_SET_ERR_MSG(extack,
  2225. "Packet size must be 0 or greater than IPTFS/ESP header length");
  2226. return -EINVAL;
  2227. }
  2228. }
  2229. if (attrs[XFRMA_IPTFS_MAX_QSIZE])
  2230. xc->max_queue_size = nla_get_u32(attrs[XFRMA_IPTFS_MAX_QSIZE]);
  2231. if (attrs[XFRMA_IPTFS_DROP_TIME])
  2232. xtfs->drop_time_ns =
  2233. (u64)nla_get_u32(attrs[XFRMA_IPTFS_DROP_TIME]) *
  2234. NSECS_IN_USEC;
  2235. if (attrs[XFRMA_IPTFS_INIT_DELAY])
  2236. xtfs->init_delay_ns =
  2237. (u64)nla_get_u32(attrs[XFRMA_IPTFS_INIT_DELAY]) * NSECS_IN_USEC;
  2238. q = (u64)xc->max_queue_size * 95;
  2239. do_div(q, 100);
  2240. xtfs->ecn_queue_size = (u32)q;
  2241. return 0;
  2242. }
  2243. static unsigned int iptfs_sa_len(const struct xfrm_state *x)
  2244. {
  2245. struct xfrm_iptfs_data *xtfs = x->mode_data;
  2246. struct xfrm_iptfs_config *xc = &xtfs->cfg;
  2247. unsigned int l = 0;
  2248. if (x->dir == XFRM_SA_DIR_IN) {
  2249. l += nla_total_size(sizeof(u32)); /* drop time usec */
  2250. l += nla_total_size(sizeof(xc->reorder_win_size));
  2251. } else {
  2252. if (xc->dont_frag)
  2253. l += nla_total_size(0); /* dont-frag flag */
  2254. l += nla_total_size(sizeof(u32)); /* init delay usec */
  2255. l += nla_total_size(sizeof(xc->max_queue_size));
  2256. l += nla_total_size(sizeof(xc->pkt_size));
  2257. }
  2258. return l;
  2259. }
  2260. static int iptfs_copy_to_user(struct xfrm_state *x, struct sk_buff *skb)
  2261. {
  2262. struct xfrm_iptfs_data *xtfs = x->mode_data;
  2263. struct xfrm_iptfs_config *xc = &xtfs->cfg;
  2264. int ret = 0;
  2265. u64 q;
  2266. if (x->dir == XFRM_SA_DIR_IN) {
  2267. q = xtfs->drop_time_ns;
  2268. do_div(q, NSECS_IN_USEC);
  2269. ret = nla_put_u32(skb, XFRMA_IPTFS_DROP_TIME, q);
  2270. if (ret)
  2271. return ret;
  2272. ret = nla_put_u16(skb, XFRMA_IPTFS_REORDER_WINDOW,
  2273. xc->reorder_win_size);
  2274. } else {
  2275. if (xc->dont_frag) {
  2276. ret = nla_put_flag(skb, XFRMA_IPTFS_DONT_FRAG);
  2277. if (ret)
  2278. return ret;
  2279. }
  2280. q = xtfs->init_delay_ns;
  2281. do_div(q, NSECS_IN_USEC);
  2282. ret = nla_put_u32(skb, XFRMA_IPTFS_INIT_DELAY, q);
  2283. if (ret)
  2284. return ret;
  2285. ret = nla_put_u32(skb, XFRMA_IPTFS_MAX_QSIZE, xc->max_queue_size);
  2286. if (ret)
  2287. return ret;
  2288. ret = nla_put_u32(skb, XFRMA_IPTFS_PKT_SIZE, xc->pkt_size);
  2289. }
  2290. return ret;
  2291. }
  2292. static void __iptfs_init_state(struct xfrm_state *x,
  2293. struct xfrm_iptfs_data *xtfs)
  2294. {
  2295. __skb_queue_head_init(&xtfs->queue);
  2296. hrtimer_setup(&xtfs->iptfs_timer, iptfs_delay_timer, CLOCK_MONOTONIC, IPTFS_HRTIMER_MODE);
  2297. spin_lock_init(&xtfs->drop_lock);
  2298. hrtimer_setup(&xtfs->drop_timer, iptfs_drop_timer, CLOCK_MONOTONIC, IPTFS_HRTIMER_MODE);
  2299. /* Modify type (esp) adjustment values */
  2300. if (x->props.family == AF_INET)
  2301. x->props.header_len += sizeof(struct iphdr) + sizeof(struct ip_iptfs_hdr);
  2302. else if (x->props.family == AF_INET6)
  2303. x->props.header_len += sizeof(struct ipv6hdr) + sizeof(struct ip_iptfs_hdr);
  2304. x->props.enc_hdr_len = sizeof(struct ip_iptfs_hdr);
  2305. /* Always keep a module reference when x->mode_data is set */
  2306. __module_get(x->mode_cbs->owner);
  2307. x->mode_data = xtfs;
  2308. xtfs->x = x;
  2309. }
  2310. static int iptfs_clone_state(struct xfrm_state *x, struct xfrm_state *orig)
  2311. {
  2312. struct xfrm_iptfs_data *xtfs;
  2313. xtfs = kmemdup(orig->mode_data, sizeof(*xtfs), GFP_KERNEL);
  2314. if (!xtfs)
  2315. return -ENOMEM;
  2316. xtfs->ra_newskb = NULL;
  2317. if (xtfs->cfg.reorder_win_size) {
  2318. xtfs->w_saved = kzalloc_objs(*xtfs->w_saved,
  2319. xtfs->cfg.reorder_win_size);
  2320. if (!xtfs->w_saved) {
  2321. kfree_sensitive(xtfs);
  2322. return -ENOMEM;
  2323. }
  2324. }
  2325. x->mode_data = xtfs;
  2326. xtfs->x = x;
  2327. return 0;
  2328. }
  2329. static int iptfs_init_state(struct xfrm_state *x)
  2330. {
  2331. struct xfrm_iptfs_data *xtfs;
  2332. if (x->mode_data) {
  2333. /* We have arrived here from xfrm_state_clone() */
  2334. xtfs = x->mode_data;
  2335. } else {
  2336. xtfs = kzalloc_obj(*xtfs);
  2337. if (!xtfs)
  2338. return -ENOMEM;
  2339. }
  2340. __iptfs_init_state(x, xtfs);
  2341. return 0;
  2342. }
  2343. static void iptfs_destroy_state(struct xfrm_state *x)
  2344. {
  2345. struct xfrm_iptfs_data *xtfs = x->mode_data;
  2346. struct sk_buff_head list;
  2347. struct skb_wseq *s, *se;
  2348. struct sk_buff *skb;
  2349. if (!xtfs)
  2350. return;
  2351. spin_lock_bh(&xtfs->x->lock);
  2352. hrtimer_cancel(&xtfs->iptfs_timer);
  2353. __skb_queue_head_init(&list);
  2354. skb_queue_splice_init(&xtfs->queue, &list);
  2355. spin_unlock_bh(&xtfs->x->lock);
  2356. while ((skb = __skb_dequeue(&list)))
  2357. kfree_skb(skb);
  2358. spin_lock_bh(&xtfs->drop_lock);
  2359. hrtimer_cancel(&xtfs->drop_timer);
  2360. spin_unlock_bh(&xtfs->drop_lock);
  2361. if (xtfs->ra_newskb)
  2362. kfree_skb(xtfs->ra_newskb);
  2363. for (s = xtfs->w_saved, se = s + xtfs->w_savedlen; s < se; s++) {
  2364. if (s->skb)
  2365. kfree_skb(s->skb);
  2366. }
  2367. kfree_sensitive(xtfs->w_saved);
  2368. kfree_sensitive(xtfs);
  2369. module_put(x->mode_cbs->owner);
  2370. }
  2371. static const struct xfrm_mode_cbs iptfs_mode_cbs = {
  2372. .owner = THIS_MODULE,
  2373. .init_state = iptfs_init_state,
  2374. .clone_state = iptfs_clone_state,
  2375. .destroy_state = iptfs_destroy_state,
  2376. .user_init = iptfs_user_init,
  2377. .copy_to_user = iptfs_copy_to_user,
  2378. .sa_len = iptfs_sa_len,
  2379. .get_inner_mtu = iptfs_get_inner_mtu,
  2380. .input = iptfs_input,
  2381. .output = iptfs_output_collect,
  2382. .prepare_output = iptfs_prepare_output,
  2383. };
  2384. static int __init xfrm_iptfs_init(void)
  2385. {
  2386. int err;
  2387. pr_info("xfrm_iptfs: IPsec IP-TFS tunnel mode module\n");
  2388. err = xfrm_register_mode_cbs(XFRM_MODE_IPTFS, &iptfs_mode_cbs);
  2389. if (err < 0)
  2390. pr_info("%s: can't register IP-TFS\n", __func__);
  2391. return err;
  2392. }
  2393. static void __exit xfrm_iptfs_fini(void)
  2394. {
  2395. xfrm_unregister_mode_cbs(XFRM_MODE_IPTFS);
  2396. }
  2397. module_init(xfrm_iptfs_init);
  2398. module_exit(xfrm_iptfs_fini);
  2399. MODULE_LICENSE("GPL");
  2400. MODULE_DESCRIPTION("IP-TFS support for xfrm ipsec tunnels");