xfrm.h 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _NET_XFRM_H
  3. #define _NET_XFRM_H
  4. #include <linux/compiler.h>
  5. #include <linux/xfrm.h>
  6. #include <linux/spinlock.h>
  7. #include <linux/list.h>
  8. #include <linux/skbuff.h>
  9. #include <linux/socket.h>
  10. #include <linux/pfkeyv2.h>
  11. #include <linux/ipsec.h>
  12. #include <linux/in6.h>
  13. #include <linux/mutex.h>
  14. #include <linux/audit.h>
  15. #include <linux/slab.h>
  16. #include <linux/refcount.h>
  17. #include <net/sock.h>
  18. #include <net/dst.h>
  19. #include <net/ip.h>
  20. #include <net/route.h>
  21. #include <net/ipv6.h>
  22. #include <net/ip6_fib.h>
  23. #include <net/flow.h>
  24. #include <net/gro_cells.h>
  25. #include <linux/interrupt.h>
  26. #ifdef CONFIG_XFRM_STATISTICS
  27. #include <net/snmp.h>
  28. #endif
  29. #define XFRM_PROTO_ESP 50
  30. #define XFRM_PROTO_AH 51
  31. #define XFRM_PROTO_COMP 108
  32. #define XFRM_PROTO_IPIP 4
  33. #define XFRM_PROTO_IPV6 41
  34. #define XFRM_PROTO_ROUTING IPPROTO_ROUTING
  35. #define XFRM_PROTO_DSTOPTS IPPROTO_DSTOPTS
  36. #define XFRM_ALIGN4(len) (((len) + 3) & ~3)
  37. #define XFRM_ALIGN8(len) (((len) + 7) & ~7)
  38. #define MODULE_ALIAS_XFRM_MODE(family, encap) \
  39. MODULE_ALIAS("xfrm-mode-" __stringify(family) "-" __stringify(encap))
  40. #define MODULE_ALIAS_XFRM_TYPE(family, proto) \
  41. MODULE_ALIAS("xfrm-type-" __stringify(family) "-" __stringify(proto))
  42. #define MODULE_ALIAS_XFRM_OFFLOAD_TYPE(family, proto) \
  43. MODULE_ALIAS("xfrm-offload-" __stringify(family) "-" __stringify(proto))
  44. #ifdef CONFIG_XFRM_STATISTICS
  45. #define XFRM_INC_STATS(net, field) SNMP_INC_STATS((net)->mib.xfrm_statistics, field)
  46. #else
  47. #define XFRM_INC_STATS(net, field) ((void)(net))
  48. #endif
  49. /* Organization of SPD aka "XFRM rules"
  50. ------------------------------------
  51. Basic objects:
  52. - policy rule, struct xfrm_policy (=SPD entry)
  53. - bundle of transformations, struct dst_entry == struct xfrm_dst (=SA bundle)
  54. - instance of a transformer, struct xfrm_state (=SA)
  55. - template to clone xfrm_state, struct xfrm_tmpl
  56. SPD is plain linear list of xfrm_policy rules, ordered by priority.
  57. (To be compatible with existing pfkeyv2 implementations,
  58. many rules with priority of 0x7fffffff are allowed to exist and
  59. such rules are ordered in an unpredictable way, thanks to bsd folks.)
  60. Lookup is plain linear search until the first match with selector.
  61. If "action" is "block", then we prohibit the flow, otherwise:
  62. if "xfrms_nr" is zero, the flow passes untransformed. Otherwise,
  63. policy entry has list of up to XFRM_MAX_DEPTH transformations,
  64. described by templates xfrm_tmpl. Each template is resolved
  65. to a complete xfrm_state (see below) and we pack bundle of transformations
  66. to a dst_entry returned to requestor.
  67. dst -. xfrm .-> xfrm_state #1
  68. |---. child .-> dst -. xfrm .-> xfrm_state #2
  69. |---. child .-> dst -. xfrm .-> xfrm_state #3
  70. |---. child .-> NULL
  71. Bundles are cached at xrfm_policy struct (field ->bundles).
  72. Resolution of xrfm_tmpl
  73. -----------------------
  74. Template contains:
  75. 1. ->mode Mode: transport or tunnel
  76. 2. ->id.proto Protocol: AH/ESP/IPCOMP
  77. 3. ->id.daddr Remote tunnel endpoint, ignored for transport mode.
  78. Q: allow to resolve security gateway?
  79. 4. ->id.spi If not zero, static SPI.
  80. 5. ->saddr Local tunnel endpoint, ignored for transport mode.
  81. 6. ->algos List of allowed algos. Plain bitmask now.
  82. Q: ealgos, aalgos, calgos. What a mess...
  83. 7. ->share Sharing mode.
  84. Q: how to implement private sharing mode? To add struct sock* to
  85. flow id?
  86. Having this template we search through SAD searching for entries
  87. with appropriate mode/proto/algo, permitted by selector.
  88. If no appropriate entry found, it is requested from key manager.
  89. PROBLEMS:
  90. Q: How to find all the bundles referring to a physical path for
  91. PMTU discovery? Seems, dst should contain list of all parents...
  92. and enter to infinite locking hierarchy disaster.
  93. No! It is easier, we will not search for them, let them find us.
  94. We add genid to each dst plus pointer to genid of raw IP route,
  95. pmtu disc will update pmtu on raw IP route and increase its genid.
  96. dst_check() will see this for top level and trigger resyncing
  97. metrics. Plus, it will be made via sk->sk_dst_cache. Solved.
  98. */
  99. struct xfrm_state_walk {
  100. struct list_head all;
  101. u8 state;
  102. u8 dying;
  103. u8 proto;
  104. u32 seq;
  105. struct xfrm_address_filter *filter;
  106. };
  107. struct xfrm_state_offload {
  108. struct net_device *dev;
  109. unsigned long offload_handle;
  110. unsigned int num_exthdrs;
  111. u8 flags;
  112. };
  113. #define XFRM_TRACK_ADDRS_COUNT 16
  114. struct xfrm_state_trace {
  115. int cpu;
  116. int pid;
  117. int count;
  118. unsigned long long when_sec;
  119. unsigned long when_nsec;
  120. unsigned long addrs[XFRM_TRACK_ADDRS_COUNT];
  121. };
  122. /* Full description of state of transformer. */
  123. struct xfrm_state {
  124. possible_net_t xs_net;
  125. union {
  126. struct hlist_node gclist;
  127. struct hlist_node bydst;
  128. };
  129. struct hlist_node bysrc;
  130. struct hlist_node byspi;
  131. refcount_t refcnt;
  132. spinlock_t lock;
  133. struct xfrm_state_trace xfrm_alloc_trace;
  134. struct xfrm_state_trace xfrm_free_trace;
  135. struct xfrm_state_trace xfrm_transfer_trace;
  136. struct xfrm_state_trace xfrm_find_trace;
  137. struct xfrm_state_trace xfrm_insert_trace;
  138. struct xfrm_id id;
  139. struct xfrm_selector sel;
  140. struct xfrm_mark mark;
  141. u32 if_id;
  142. u32 tfcpad;
  143. u32 genid;
  144. /* Key manager bits */
  145. struct xfrm_state_walk km;
  146. /* Parameters of this state. */
  147. struct {
  148. u32 reqid;
  149. u8 mode;
  150. u8 replay_window;
  151. u8 aalgo, ealgo, calgo;
  152. u8 flags;
  153. u16 family;
  154. xfrm_address_t saddr;
  155. int header_len;
  156. int trailer_len;
  157. u32 extra_flags;
  158. struct xfrm_mark smark;
  159. } props;
  160. struct xfrm_lifetime_cfg lft;
  161. /* Data for transformer */
  162. struct xfrm_algo_auth *aalg;
  163. struct xfrm_algo *ealg;
  164. struct xfrm_algo *calg;
  165. struct xfrm_algo_aead *aead;
  166. const char *geniv;
  167. /* Data for encapsulator */
  168. struct xfrm_encap_tmpl *encap;
  169. /* Data for care-of address */
  170. xfrm_address_t *coaddr;
  171. /* IPComp needs an IPIP tunnel for handling uncompressed packets */
  172. struct xfrm_state *tunnel;
  173. /* If a tunnel, number of users + 1 */
  174. atomic_t tunnel_users;
  175. /* State for replay detection */
  176. struct xfrm_replay_state replay;
  177. struct xfrm_replay_state_esn *replay_esn;
  178. /* Replay detection state at the time we sent the last notification */
  179. struct xfrm_replay_state preplay;
  180. struct xfrm_replay_state_esn *preplay_esn;
  181. /* The functions for replay detection. */
  182. const struct xfrm_replay *repl;
  183. /* internal flag that only holds state for delayed aevent at the
  184. * moment
  185. */
  186. u32 xflags;
  187. /* Replay detection notification settings */
  188. u32 replay_maxage;
  189. u32 replay_maxdiff;
  190. /* Replay detection notification timer */
  191. struct timer_list rtimer;
  192. /* Statistics */
  193. struct xfrm_stats stats;
  194. struct xfrm_lifetime_cur curlft;
  195. struct tasklet_hrtimer mtimer;
  196. struct xfrm_state_offload xso;
  197. /* used to fix curlft->add_time when changing date */
  198. long saved_tmo;
  199. /* Last used time */
  200. unsigned long lastused;
  201. struct page_frag xfrag;
  202. /* Reference to data common to all the instances of this
  203. * transformer. */
  204. const struct xfrm_type *type;
  205. struct xfrm_mode *inner_mode;
  206. struct xfrm_mode *inner_mode_iaf;
  207. struct xfrm_mode *outer_mode;
  208. const struct xfrm_type_offload *type_offload;
  209. /* Security context */
  210. struct xfrm_sec_ctx *security;
  211. /* Private data of this transformer, format is opaque,
  212. * interpreted by xfrm_type methods. */
  213. void *data;
  214. };
  215. static inline struct net *xs_net(struct xfrm_state *x)
  216. {
  217. return read_pnet(&x->xs_net);
  218. }
  219. /* xflags - make enum if more show up */
  220. #define XFRM_TIME_DEFER 1
  221. #define XFRM_SOFT_EXPIRE 2
  222. enum {
  223. XFRM_STATE_VOID,
  224. XFRM_STATE_ACQ,
  225. XFRM_STATE_VALID,
  226. XFRM_STATE_ERROR,
  227. XFRM_STATE_EXPIRED,
  228. XFRM_STATE_DEAD
  229. };
  230. /* callback structure passed from either netlink or pfkey */
  231. struct km_event {
  232. union {
  233. u32 hard;
  234. u32 proto;
  235. u32 byid;
  236. u32 aevent;
  237. u32 type;
  238. } data;
  239. u32 seq;
  240. u32 portid;
  241. u32 event;
  242. struct net *net;
  243. };
  244. struct xfrm_replay {
  245. void (*advance)(struct xfrm_state *x, __be32 net_seq);
  246. int (*check)(struct xfrm_state *x,
  247. struct sk_buff *skb,
  248. __be32 net_seq);
  249. int (*recheck)(struct xfrm_state *x,
  250. struct sk_buff *skb,
  251. __be32 net_seq);
  252. void (*notify)(struct xfrm_state *x, int event);
  253. int (*overflow)(struct xfrm_state *x, struct sk_buff *skb);
  254. };
  255. struct xfrm_if_cb {
  256. struct xfrm_if *(*decode_session)(struct sk_buff *skb);
  257. };
  258. void xfrm_if_register_cb(const struct xfrm_if_cb *ifcb);
  259. void xfrm_if_unregister_cb(void);
  260. struct net_device;
  261. struct xfrm_type;
  262. struct xfrm_dst;
  263. struct xfrm_policy_afinfo {
  264. struct dst_ops *dst_ops;
  265. struct dst_entry *(*dst_lookup)(struct net *net,
  266. int tos, int oif,
  267. const xfrm_address_t *saddr,
  268. const xfrm_address_t *daddr,
  269. u32 mark);
  270. int (*get_saddr)(struct net *net, int oif,
  271. xfrm_address_t *saddr,
  272. xfrm_address_t *daddr,
  273. u32 mark);
  274. void (*decode_session)(struct sk_buff *skb,
  275. struct flowi *fl,
  276. int reverse);
  277. int (*get_tos)(const struct flowi *fl);
  278. int (*init_path)(struct xfrm_dst *path,
  279. struct dst_entry *dst,
  280. int nfheader_len);
  281. int (*fill_dst)(struct xfrm_dst *xdst,
  282. struct net_device *dev,
  283. const struct flowi *fl);
  284. struct dst_entry *(*blackhole_route)(struct net *net, struct dst_entry *orig);
  285. };
  286. int xfrm_policy_register_afinfo(const struct xfrm_policy_afinfo *afinfo, int family);
  287. void xfrm_policy_unregister_afinfo(const struct xfrm_policy_afinfo *afinfo);
  288. void km_policy_notify(struct xfrm_policy *xp, int dir,
  289. const struct km_event *c);
  290. void km_state_notify(struct xfrm_state *x, const struct km_event *c);
  291. struct xfrm_tmpl;
  292. int km_query(struct xfrm_state *x, struct xfrm_tmpl *t,
  293. struct xfrm_policy *pol);
  294. void km_state_expired(struct xfrm_state *x, int hard, u32 portid);
  295. int __xfrm_state_delete(struct xfrm_state *x);
  296. struct xfrm_state_afinfo {
  297. unsigned int family;
  298. unsigned int proto;
  299. __be16 eth_proto;
  300. struct module *owner;
  301. const struct xfrm_type *type_map[IPPROTO_MAX];
  302. const struct xfrm_type_offload *type_offload_map[IPPROTO_MAX];
  303. struct xfrm_mode *mode_map[XFRM_MODE_MAX];
  304. int (*init_flags)(struct xfrm_state *x);
  305. void (*init_tempsel)(struct xfrm_selector *sel,
  306. const struct flowi *fl);
  307. void (*init_temprop)(struct xfrm_state *x,
  308. const struct xfrm_tmpl *tmpl,
  309. const xfrm_address_t *daddr,
  310. const xfrm_address_t *saddr);
  311. int (*tmpl_sort)(struct xfrm_tmpl **dst, struct xfrm_tmpl **src, int n);
  312. int (*state_sort)(struct xfrm_state **dst, struct xfrm_state **src, int n);
  313. int (*output)(struct net *net, struct sock *sk, struct sk_buff *skb);
  314. int (*output_finish)(struct sock *sk, struct sk_buff *skb);
  315. int (*extract_input)(struct xfrm_state *x,
  316. struct sk_buff *skb);
  317. int (*extract_output)(struct xfrm_state *x,
  318. struct sk_buff *skb);
  319. int (*transport_finish)(struct sk_buff *skb,
  320. int async);
  321. void (*local_error)(struct sk_buff *skb, u32 mtu);
  322. };
  323. int xfrm_state_register_afinfo(struct xfrm_state_afinfo *afinfo);
  324. int xfrm_state_unregister_afinfo(struct xfrm_state_afinfo *afinfo);
  325. struct xfrm_state_afinfo *xfrm_state_get_afinfo(unsigned int family);
  326. struct xfrm_state_afinfo *xfrm_state_afinfo_get_rcu(unsigned int family);
  327. struct xfrm_input_afinfo {
  328. unsigned int family;
  329. int (*callback)(struct sk_buff *skb, u8 protocol,
  330. int err);
  331. };
  332. int xfrm_input_register_afinfo(const struct xfrm_input_afinfo *afinfo);
  333. int xfrm_input_unregister_afinfo(const struct xfrm_input_afinfo *afinfo);
  334. void xfrm_state_delete_tunnel(struct xfrm_state *x);
  335. struct xfrm_type {
  336. char *description;
  337. struct module *owner;
  338. u8 proto;
  339. u8 flags;
  340. #define XFRM_TYPE_NON_FRAGMENT 1
  341. #define XFRM_TYPE_REPLAY_PROT 2
  342. #define XFRM_TYPE_LOCAL_COADDR 4
  343. #define XFRM_TYPE_REMOTE_COADDR 8
  344. int (*init_state)(struct xfrm_state *x);
  345. void (*destructor)(struct xfrm_state *);
  346. int (*input)(struct xfrm_state *, struct sk_buff *skb);
  347. int (*output)(struct xfrm_state *, struct sk_buff *pskb);
  348. int (*reject)(struct xfrm_state *, struct sk_buff *,
  349. const struct flowi *);
  350. int (*hdr_offset)(struct xfrm_state *, struct sk_buff *, u8 **);
  351. /* Estimate maximal size of result of transformation of a dgram */
  352. u32 (*get_mtu)(struct xfrm_state *, int size);
  353. };
  354. int xfrm_register_type(const struct xfrm_type *type, unsigned short family);
  355. int xfrm_unregister_type(const struct xfrm_type *type, unsigned short family);
  356. struct xfrm_type_offload {
  357. char *description;
  358. struct module *owner;
  359. u8 proto;
  360. void (*encap)(struct xfrm_state *, struct sk_buff *pskb);
  361. int (*input_tail)(struct xfrm_state *x, struct sk_buff *skb);
  362. int (*xmit)(struct xfrm_state *, struct sk_buff *pskb, netdev_features_t features);
  363. };
  364. int xfrm_register_type_offload(const struct xfrm_type_offload *type, unsigned short family);
  365. int xfrm_unregister_type_offload(const struct xfrm_type_offload *type, unsigned short family);
  366. struct xfrm_mode {
  367. /*
  368. * Remove encapsulation header.
  369. *
  370. * The IP header will be moved over the top of the encapsulation
  371. * header.
  372. *
  373. * On entry, the transport header shall point to where the IP header
  374. * should be and the network header shall be set to where the IP
  375. * header currently is. skb->data shall point to the start of the
  376. * payload.
  377. */
  378. int (*input2)(struct xfrm_state *x, struct sk_buff *skb);
  379. /*
  380. * This is the actual input entry point.
  381. *
  382. * For transport mode and equivalent this would be identical to
  383. * input2 (which does not need to be set). While tunnel mode
  384. * and equivalent would set this to the tunnel encapsulation function
  385. * xfrm4_prepare_input that would in turn call input2.
  386. */
  387. int (*input)(struct xfrm_state *x, struct sk_buff *skb);
  388. /*
  389. * Add encapsulation header.
  390. *
  391. * On exit, the transport header will be set to the start of the
  392. * encapsulation header to be filled in by x->type->output and
  393. * the mac header will be set to the nextheader (protocol for
  394. * IPv4) field of the extension header directly preceding the
  395. * encapsulation header, or in its absence, that of the top IP
  396. * header. The value of the network header will always point
  397. * to the top IP header while skb->data will point to the payload.
  398. */
  399. int (*output2)(struct xfrm_state *x,struct sk_buff *skb);
  400. /*
  401. * This is the actual output entry point.
  402. *
  403. * For transport mode and equivalent this would be identical to
  404. * output2 (which does not need to be set). While tunnel mode
  405. * and equivalent would set this to a tunnel encapsulation function
  406. * (xfrm4_prepare_output or xfrm6_prepare_output) that would in turn
  407. * call output2.
  408. */
  409. int (*output)(struct xfrm_state *x, struct sk_buff *skb);
  410. /*
  411. * Adjust pointers into the packet and do GSO segmentation.
  412. */
  413. struct sk_buff *(*gso_segment)(struct xfrm_state *x, struct sk_buff *skb, netdev_features_t features);
  414. /*
  415. * Adjust pointers into the packet when IPsec is done at layer2.
  416. */
  417. void (*xmit)(struct xfrm_state *x, struct sk_buff *skb);
  418. struct xfrm_state_afinfo *afinfo;
  419. struct module *owner;
  420. unsigned int encap;
  421. int flags;
  422. };
  423. /* Flags for xfrm_mode. */
  424. enum {
  425. XFRM_MODE_FLAG_TUNNEL = 1,
  426. };
  427. int xfrm_register_mode(struct xfrm_mode *mode, int family);
  428. int xfrm_unregister_mode(struct xfrm_mode *mode, int family);
  429. static inline int xfrm_af2proto(unsigned int family)
  430. {
  431. switch(family) {
  432. case AF_INET:
  433. return IPPROTO_IPIP;
  434. case AF_INET6:
  435. return IPPROTO_IPV6;
  436. default:
  437. return 0;
  438. }
  439. }
  440. static inline struct xfrm_mode *xfrm_ip2inner_mode(struct xfrm_state *x, int ipproto)
  441. {
  442. if ((ipproto == IPPROTO_IPIP && x->props.family == AF_INET) ||
  443. (ipproto == IPPROTO_IPV6 && x->props.family == AF_INET6))
  444. return x->inner_mode;
  445. else
  446. return x->inner_mode_iaf;
  447. }
  448. struct xfrm_tmpl {
  449. /* id in template is interpreted as:
  450. * daddr - destination of tunnel, may be zero for transport mode.
  451. * spi - zero to acquire spi. Not zero if spi is static, then
  452. * daddr must be fixed too.
  453. * proto - AH/ESP/IPCOMP
  454. */
  455. struct xfrm_id id;
  456. /* Source address of tunnel. Ignored, if it is not a tunnel. */
  457. xfrm_address_t saddr;
  458. unsigned short encap_family;
  459. u32 reqid;
  460. /* Mode: transport, tunnel etc. */
  461. u8 mode;
  462. /* Sharing mode: unique, this session only, this user only etc. */
  463. u8 share;
  464. /* May skip this transfomration if no SA is found */
  465. u8 optional;
  466. /* Skip aalgos/ealgos/calgos checks. */
  467. u8 allalgs;
  468. /* Bit mask of algos allowed for acquisition */
  469. u32 aalgos;
  470. u32 ealgos;
  471. u32 calgos;
  472. };
  473. #define XFRM_MAX_DEPTH 6
  474. #define XFRM_MAX_OFFLOAD_DEPTH 1
  475. struct xfrm_policy_walk_entry {
  476. struct list_head all;
  477. u8 dead;
  478. };
  479. struct xfrm_policy_walk {
  480. struct xfrm_policy_walk_entry walk;
  481. u8 type;
  482. u32 seq;
  483. };
  484. struct xfrm_policy_queue {
  485. struct sk_buff_head hold_queue;
  486. struct timer_list hold_timer;
  487. unsigned long timeout;
  488. };
  489. struct xfrm_policy {
  490. possible_net_t xp_net;
  491. struct hlist_node bydst;
  492. struct hlist_node byidx;
  493. /* This lock only affects elements except for entry. */
  494. rwlock_t lock;
  495. refcount_t refcnt;
  496. struct timer_list timer;
  497. atomic_t genid;
  498. u32 priority;
  499. u32 index;
  500. u32 if_id;
  501. struct xfrm_mark mark;
  502. struct xfrm_selector selector;
  503. struct xfrm_lifetime_cfg lft;
  504. struct xfrm_lifetime_cur curlft;
  505. struct xfrm_policy_walk_entry walk;
  506. struct xfrm_policy_queue polq;
  507. u8 type;
  508. u8 action;
  509. u8 flags;
  510. u8 xfrm_nr;
  511. u16 family;
  512. struct xfrm_sec_ctx *security;
  513. struct xfrm_tmpl xfrm_vec[XFRM_MAX_DEPTH];
  514. struct rcu_head rcu;
  515. };
  516. static inline struct net *xp_net(const struct xfrm_policy *xp)
  517. {
  518. return read_pnet(&xp->xp_net);
  519. }
  520. struct xfrm_kmaddress {
  521. xfrm_address_t local;
  522. xfrm_address_t remote;
  523. u32 reserved;
  524. u16 family;
  525. };
  526. struct xfrm_migrate {
  527. xfrm_address_t old_daddr;
  528. xfrm_address_t old_saddr;
  529. xfrm_address_t new_daddr;
  530. xfrm_address_t new_saddr;
  531. u8 proto;
  532. u8 mode;
  533. u16 reserved;
  534. u32 reqid;
  535. u16 old_family;
  536. u16 new_family;
  537. };
  538. #define XFRM_KM_TIMEOUT 30
  539. /* what happened */
  540. #define XFRM_REPLAY_UPDATE XFRM_AE_CR
  541. #define XFRM_REPLAY_TIMEOUT XFRM_AE_CE
  542. /* default aevent timeout in units of 100ms */
  543. #define XFRM_AE_ETIME 10
  544. /* Async Event timer multiplier */
  545. #define XFRM_AE_ETH_M 10
  546. /* default seq threshold size */
  547. #define XFRM_AE_SEQT_SIZE 2
  548. struct xfrm_mgr {
  549. struct list_head list;
  550. int (*notify)(struct xfrm_state *x, const struct km_event *c);
  551. int (*acquire)(struct xfrm_state *x, struct xfrm_tmpl *, struct xfrm_policy *xp);
  552. struct xfrm_policy *(*compile_policy)(struct sock *sk, int opt, u8 *data, int len, int *dir);
  553. int (*new_mapping)(struct xfrm_state *x, xfrm_address_t *ipaddr, __be16 sport);
  554. int (*notify_policy)(struct xfrm_policy *x, int dir, const struct km_event *c);
  555. int (*report)(struct net *net, u8 proto, struct xfrm_selector *sel, xfrm_address_t *addr);
  556. int (*migrate)(const struct xfrm_selector *sel,
  557. u8 dir, u8 type,
  558. const struct xfrm_migrate *m,
  559. int num_bundles,
  560. const struct xfrm_kmaddress *k,
  561. const struct xfrm_encap_tmpl *encap);
  562. bool (*is_alive)(const struct km_event *c);
  563. };
  564. int xfrm_register_km(struct xfrm_mgr *km);
  565. int xfrm_unregister_km(struct xfrm_mgr *km);
  566. struct xfrm_tunnel_skb_cb {
  567. union {
  568. struct inet_skb_parm h4;
  569. struct inet6_skb_parm h6;
  570. } header;
  571. union {
  572. struct ip_tunnel *ip4;
  573. struct ip6_tnl *ip6;
  574. } tunnel;
  575. };
  576. #define XFRM_TUNNEL_SKB_CB(__skb) ((struct xfrm_tunnel_skb_cb *)&((__skb)->cb[0]))
  577. /*
  578. * This structure is used for the duration where packets are being
  579. * transformed by IPsec. As soon as the packet leaves IPsec the
  580. * area beyond the generic IP part may be overwritten.
  581. */
  582. struct xfrm_skb_cb {
  583. struct xfrm_tunnel_skb_cb header;
  584. /* Sequence number for replay protection. */
  585. union {
  586. struct {
  587. __u32 low;
  588. __u32 hi;
  589. } output;
  590. struct {
  591. __be32 low;
  592. __be32 hi;
  593. } input;
  594. } seq;
  595. };
  596. #define XFRM_SKB_CB(__skb) ((struct xfrm_skb_cb *)&((__skb)->cb[0]))
  597. /*
  598. * This structure is used by the afinfo prepare_input/prepare_output functions
  599. * to transmit header information to the mode input/output functions.
  600. */
  601. struct xfrm_mode_skb_cb {
  602. struct xfrm_tunnel_skb_cb header;
  603. /* Copied from header for IPv4, always set to zero and DF for IPv6. */
  604. __be16 id;
  605. __be16 frag_off;
  606. /* IP header length (excluding options or extension headers). */
  607. u8 ihl;
  608. /* TOS for IPv4, class for IPv6. */
  609. u8 tos;
  610. /* TTL for IPv4, hop limitfor IPv6. */
  611. u8 ttl;
  612. /* Protocol for IPv4, NH for IPv6. */
  613. u8 protocol;
  614. /* Option length for IPv4, zero for IPv6. */
  615. u8 optlen;
  616. /* Used by IPv6 only, zero for IPv4. */
  617. u8 flow_lbl[3];
  618. };
  619. #define XFRM_MODE_SKB_CB(__skb) ((struct xfrm_mode_skb_cb *)&((__skb)->cb[0]))
  620. /*
  621. * This structure is used by the input processing to locate the SPI and
  622. * related information.
  623. */
  624. struct xfrm_spi_skb_cb {
  625. struct xfrm_tunnel_skb_cb header;
  626. unsigned int daddroff;
  627. unsigned int family;
  628. __be32 seq;
  629. };
  630. #define XFRM_SPI_SKB_CB(__skb) ((struct xfrm_spi_skb_cb *)&((__skb)->cb[0]))
  631. #ifdef CONFIG_AUDITSYSCALL
  632. static inline struct audit_buffer *xfrm_audit_start(const char *op)
  633. {
  634. struct audit_buffer *audit_buf = NULL;
  635. if (audit_enabled == 0)
  636. return NULL;
  637. audit_buf = audit_log_start(current->audit_context, GFP_ATOMIC,
  638. AUDIT_MAC_IPSEC_EVENT);
  639. if (audit_buf == NULL)
  640. return NULL;
  641. audit_log_format(audit_buf, "op=%s", op);
  642. return audit_buf;
  643. }
  644. static inline void xfrm_audit_helper_usrinfo(bool task_valid,
  645. struct audit_buffer *audit_buf)
  646. {
  647. const unsigned int auid = from_kuid(&init_user_ns, task_valid ?
  648. audit_get_loginuid(current) :
  649. INVALID_UID);
  650. const unsigned int ses = task_valid ? audit_get_sessionid(current) :
  651. (unsigned int) -1;
  652. audit_log_format(audit_buf, " auid=%u ses=%u", auid, ses);
  653. audit_log_task_context(audit_buf);
  654. }
  655. void xfrm_audit_policy_add(struct xfrm_policy *xp, int result, bool task_valid);
  656. void xfrm_audit_policy_delete(struct xfrm_policy *xp, int result,
  657. bool task_valid);
  658. void xfrm_audit_state_add(struct xfrm_state *x, int result, bool task_valid);
  659. void xfrm_audit_state_delete(struct xfrm_state *x, int result, bool task_valid);
  660. void xfrm_audit_state_replay_overflow(struct xfrm_state *x,
  661. struct sk_buff *skb);
  662. void xfrm_audit_state_replay(struct xfrm_state *x, struct sk_buff *skb,
  663. __be32 net_seq);
  664. void xfrm_audit_state_notfound_simple(struct sk_buff *skb, u16 family);
  665. void xfrm_audit_state_notfound(struct sk_buff *skb, u16 family, __be32 net_spi,
  666. __be32 net_seq);
  667. void xfrm_audit_state_icvfail(struct xfrm_state *x, struct sk_buff *skb,
  668. u8 proto);
  669. #else
  670. static inline void xfrm_audit_policy_add(struct xfrm_policy *xp, int result,
  671. bool task_valid)
  672. {
  673. }
  674. static inline void xfrm_audit_policy_delete(struct xfrm_policy *xp, int result,
  675. bool task_valid)
  676. {
  677. }
  678. static inline void xfrm_audit_state_add(struct xfrm_state *x, int result,
  679. bool task_valid)
  680. {
  681. }
  682. static inline void xfrm_audit_state_delete(struct xfrm_state *x, int result,
  683. bool task_valid)
  684. {
  685. }
  686. static inline void xfrm_audit_state_replay_overflow(struct xfrm_state *x,
  687. struct sk_buff *skb)
  688. {
  689. }
  690. static inline void xfrm_audit_state_replay(struct xfrm_state *x,
  691. struct sk_buff *skb, __be32 net_seq)
  692. {
  693. }
  694. static inline void xfrm_audit_state_notfound_simple(struct sk_buff *skb,
  695. u16 family)
  696. {
  697. }
  698. static inline void xfrm_audit_state_notfound(struct sk_buff *skb, u16 family,
  699. __be32 net_spi, __be32 net_seq)
  700. {
  701. }
  702. static inline void xfrm_audit_state_icvfail(struct xfrm_state *x,
  703. struct sk_buff *skb, u8 proto)
  704. {
  705. }
  706. #endif /* CONFIG_AUDITSYSCALL */
  707. static inline void xfrm_pol_hold(struct xfrm_policy *policy)
  708. {
  709. if (likely(policy != NULL))
  710. refcount_inc(&policy->refcnt);
  711. }
  712. void xfrm_policy_destroy(struct xfrm_policy *policy);
  713. static inline void xfrm_pol_put(struct xfrm_policy *policy)
  714. {
  715. if (refcount_dec_and_test(&policy->refcnt))
  716. xfrm_policy_destroy(policy);
  717. }
  718. static inline void xfrm_pols_put(struct xfrm_policy **pols, int npols)
  719. {
  720. int i;
  721. for (i = npols - 1; i >= 0; --i)
  722. xfrm_pol_put(pols[i]);
  723. }
  724. void __xfrm_state_destroy(struct xfrm_state *);
  725. static inline void __xfrm_state_put(struct xfrm_state *x)
  726. {
  727. refcount_dec(&x->refcnt);
  728. }
  729. static inline void xfrm_state_put(struct xfrm_state *x)
  730. {
  731. if (refcount_dec_and_test(&x->refcnt))
  732. __xfrm_state_destroy(x);
  733. }
  734. static inline void xfrm_state_hold(struct xfrm_state *x)
  735. {
  736. refcount_inc(&x->refcnt);
  737. }
  738. static inline bool addr_match(const void *token1, const void *token2,
  739. unsigned int prefixlen)
  740. {
  741. const __be32 *a1 = token1;
  742. const __be32 *a2 = token2;
  743. unsigned int pdw;
  744. unsigned int pbi;
  745. pdw = prefixlen >> 5; /* num of whole u32 in prefix */
  746. pbi = prefixlen & 0x1f; /* num of bits in incomplete u32 in prefix */
  747. if (pdw)
  748. if (memcmp(a1, a2, pdw << 2))
  749. return false;
  750. if (pbi) {
  751. __be32 mask;
  752. mask = htonl((0xffffffff) << (32 - pbi));
  753. if ((a1[pdw] ^ a2[pdw]) & mask)
  754. return false;
  755. }
  756. return true;
  757. }
  758. static inline bool addr4_match(__be32 a1, __be32 a2, u8 prefixlen)
  759. {
  760. /* C99 6.5.7 (3): u32 << 32 is undefined behaviour */
  761. if (sizeof(long) == 4 && prefixlen == 0)
  762. return true;
  763. return !((a1 ^ a2) & htonl(~0UL << (32 - prefixlen)));
  764. }
  765. static __inline__
  766. __be16 xfrm_flowi_sport(const struct flowi *fl, const union flowi_uli *uli)
  767. {
  768. __be16 port;
  769. switch(fl->flowi_proto) {
  770. case IPPROTO_TCP:
  771. case IPPROTO_UDP:
  772. case IPPROTO_UDPLITE:
  773. case IPPROTO_SCTP:
  774. port = uli->ports.sport;
  775. break;
  776. case IPPROTO_ICMP:
  777. case IPPROTO_ICMPV6:
  778. port = htons(uli->icmpt.type);
  779. break;
  780. case IPPROTO_MH:
  781. port = htons(uli->mht.type);
  782. break;
  783. case IPPROTO_GRE:
  784. port = htons(ntohl(uli->gre_key) >> 16);
  785. break;
  786. default:
  787. port = 0; /*XXX*/
  788. }
  789. return port;
  790. }
  791. static __inline__
  792. __be16 xfrm_flowi_dport(const struct flowi *fl, const union flowi_uli *uli)
  793. {
  794. __be16 port;
  795. switch(fl->flowi_proto) {
  796. case IPPROTO_TCP:
  797. case IPPROTO_UDP:
  798. case IPPROTO_UDPLITE:
  799. case IPPROTO_SCTP:
  800. port = uli->ports.dport;
  801. break;
  802. case IPPROTO_ICMP:
  803. case IPPROTO_ICMPV6:
  804. port = htons(uli->icmpt.code);
  805. break;
  806. case IPPROTO_GRE:
  807. port = htons(ntohl(uli->gre_key) & 0xffff);
  808. break;
  809. default:
  810. port = 0; /*XXX*/
  811. }
  812. return port;
  813. }
  814. bool xfrm_selector_match(const struct xfrm_selector *sel,
  815. const struct flowi *fl, unsigned short family);
  816. #ifdef CONFIG_SECURITY_NETWORK_XFRM
  817. /* If neither has a context --> match
  818. * Otherwise, both must have a context and the sids, doi, alg must match
  819. */
  820. static inline bool xfrm_sec_ctx_match(struct xfrm_sec_ctx *s1, struct xfrm_sec_ctx *s2)
  821. {
  822. return ((!s1 && !s2) ||
  823. (s1 && s2 &&
  824. (s1->ctx_sid == s2->ctx_sid) &&
  825. (s1->ctx_doi == s2->ctx_doi) &&
  826. (s1->ctx_alg == s2->ctx_alg)));
  827. }
  828. #else
  829. static inline bool xfrm_sec_ctx_match(struct xfrm_sec_ctx *s1, struct xfrm_sec_ctx *s2)
  830. {
  831. return true;
  832. }
  833. #endif
  834. /* A struct encoding bundle of transformations to apply to some set of flow.
  835. *
  836. * dst->child points to the next element of bundle.
  837. * dst->xfrm points to an instanse of transformer.
  838. *
  839. * Due to unfortunate limitations of current routing cache, which we
  840. * have no time to fix, it mirrors struct rtable and bound to the same
  841. * routing key, including saddr,daddr. However, we can have many of
  842. * bundles differing by session id. All the bundles grow from a parent
  843. * policy rule.
  844. */
  845. struct xfrm_dst {
  846. union {
  847. struct dst_entry dst;
  848. struct rtable rt;
  849. struct rt6_info rt6;
  850. } u;
  851. struct dst_entry *route;
  852. struct xfrm_policy *pols[XFRM_POLICY_TYPE_MAX];
  853. int num_pols, num_xfrms;
  854. u32 xfrm_genid;
  855. u32 policy_genid;
  856. u32 route_mtu_cached;
  857. u32 child_mtu_cached;
  858. u32 route_cookie;
  859. u32 path_cookie;
  860. };
  861. #ifdef CONFIG_XFRM
  862. static inline void xfrm_dst_destroy(struct xfrm_dst *xdst)
  863. {
  864. xfrm_pols_put(xdst->pols, xdst->num_pols);
  865. dst_release(xdst->route);
  866. if (likely(xdst->u.dst.xfrm))
  867. xfrm_state_put(xdst->u.dst.xfrm);
  868. }
  869. #endif
  870. void xfrm_dst_ifdown(struct dst_entry *dst, struct net_device *dev);
  871. struct xfrm_if_parms {
  872. char name[IFNAMSIZ]; /* name of XFRM device */
  873. int link; /* ifindex of underlying L2 interface */
  874. u32 if_id; /* interface identifyer */
  875. };
  876. struct xfrm_if {
  877. struct xfrm_if __rcu *next; /* next interface in list */
  878. struct net_device *dev; /* virtual device associated with interface */
  879. struct net_device *phydev; /* physical device */
  880. struct net *net; /* netns for packet i/o */
  881. struct xfrm_if_parms p; /* interface parms */
  882. struct gro_cells gro_cells;
  883. };
  884. struct xfrm_offload {
  885. /* Output sequence number for replay protection on offloading. */
  886. struct {
  887. __u32 low;
  888. __u32 hi;
  889. } seq;
  890. __u32 flags;
  891. #define SA_DELETE_REQ 1
  892. #define CRYPTO_DONE 2
  893. #define CRYPTO_NEXT_DONE 4
  894. #define CRYPTO_FALLBACK 8
  895. #define XFRM_GSO_SEGMENT 16
  896. #define XFRM_GRO 32
  897. #define XFRM_ESP_NO_TRAILER 64
  898. __u32 status;
  899. #define CRYPTO_SUCCESS 1
  900. #define CRYPTO_GENERIC_ERROR 2
  901. #define CRYPTO_TRANSPORT_AH_AUTH_FAILED 4
  902. #define CRYPTO_TRANSPORT_ESP_AUTH_FAILED 8
  903. #define CRYPTO_TUNNEL_AH_AUTH_FAILED 16
  904. #define CRYPTO_TUNNEL_ESP_AUTH_FAILED 32
  905. #define CRYPTO_INVALID_PACKET_SYNTAX 64
  906. #define CRYPTO_INVALID_PROTOCOL 128
  907. __u8 proto;
  908. };
  909. struct sec_path {
  910. refcount_t refcnt;
  911. int len;
  912. int olen;
  913. struct xfrm_state *xvec[XFRM_MAX_DEPTH];
  914. struct xfrm_offload ovec[XFRM_MAX_OFFLOAD_DEPTH];
  915. };
  916. static inline int secpath_exists(struct sk_buff *skb)
  917. {
  918. #ifdef CONFIG_XFRM
  919. return skb->sp != NULL;
  920. #else
  921. return 0;
  922. #endif
  923. }
  924. static inline struct sec_path *
  925. secpath_get(struct sec_path *sp)
  926. {
  927. if (sp)
  928. refcount_inc(&sp->refcnt);
  929. return sp;
  930. }
  931. void __secpath_destroy(struct sec_path *sp);
  932. static inline void
  933. secpath_put(struct sec_path *sp)
  934. {
  935. if (sp && refcount_dec_and_test(&sp->refcnt))
  936. __secpath_destroy(sp);
  937. }
  938. struct sec_path *secpath_dup(struct sec_path *src);
  939. int secpath_set(struct sk_buff *skb);
  940. static inline void
  941. secpath_reset(struct sk_buff *skb)
  942. {
  943. #ifdef CONFIG_XFRM
  944. secpath_put(skb->sp);
  945. skb->sp = NULL;
  946. #endif
  947. }
  948. static inline int
  949. xfrm_addr_any(const xfrm_address_t *addr, unsigned short family)
  950. {
  951. switch (family) {
  952. case AF_INET:
  953. return addr->a4 == 0;
  954. case AF_INET6:
  955. return ipv6_addr_any(&addr->in6);
  956. }
  957. return 0;
  958. }
  959. static inline int
  960. __xfrm4_state_addr_cmp(const struct xfrm_tmpl *tmpl, const struct xfrm_state *x)
  961. {
  962. return (tmpl->saddr.a4 &&
  963. tmpl->saddr.a4 != x->props.saddr.a4);
  964. }
  965. static inline int
  966. __xfrm6_state_addr_cmp(const struct xfrm_tmpl *tmpl, const struct xfrm_state *x)
  967. {
  968. return (!ipv6_addr_any((struct in6_addr*)&tmpl->saddr) &&
  969. !ipv6_addr_equal((struct in6_addr *)&tmpl->saddr, (struct in6_addr*)&x->props.saddr));
  970. }
  971. static inline int
  972. xfrm_state_addr_cmp(const struct xfrm_tmpl *tmpl, const struct xfrm_state *x, unsigned short family)
  973. {
  974. switch (family) {
  975. case AF_INET:
  976. return __xfrm4_state_addr_cmp(tmpl, x);
  977. case AF_INET6:
  978. return __xfrm6_state_addr_cmp(tmpl, x);
  979. }
  980. return !0;
  981. }
  982. #ifdef CONFIG_XFRM
  983. int __xfrm_policy_check(struct sock *, int dir, struct sk_buff *skb,
  984. unsigned short family);
  985. static inline int __xfrm_policy_check2(struct sock *sk, int dir,
  986. struct sk_buff *skb,
  987. unsigned int family, int reverse)
  988. {
  989. struct net *net = dev_net(skb->dev);
  990. int ndir = dir | (reverse ? XFRM_POLICY_MASK + 1 : 0);
  991. if (sk && sk->sk_policy[XFRM_POLICY_IN])
  992. return __xfrm_policy_check(sk, ndir, skb, family);
  993. return (!net->xfrm.policy_count[dir] && !skb->sp) ||
  994. (skb_dst(skb)->flags & DST_NOPOLICY) ||
  995. __xfrm_policy_check(sk, ndir, skb, family);
  996. }
  997. static inline int xfrm_policy_check(struct sock *sk, int dir, struct sk_buff *skb, unsigned short family)
  998. {
  999. return __xfrm_policy_check2(sk, dir, skb, family, 0);
  1000. }
  1001. static inline int xfrm4_policy_check(struct sock *sk, int dir, struct sk_buff *skb)
  1002. {
  1003. return xfrm_policy_check(sk, dir, skb, AF_INET);
  1004. }
  1005. static inline int xfrm6_policy_check(struct sock *sk, int dir, struct sk_buff *skb)
  1006. {
  1007. return xfrm_policy_check(sk, dir, skb, AF_INET6);
  1008. }
  1009. static inline int xfrm4_policy_check_reverse(struct sock *sk, int dir,
  1010. struct sk_buff *skb)
  1011. {
  1012. return __xfrm_policy_check2(sk, dir, skb, AF_INET, 1);
  1013. }
  1014. static inline int xfrm6_policy_check_reverse(struct sock *sk, int dir,
  1015. struct sk_buff *skb)
  1016. {
  1017. return __xfrm_policy_check2(sk, dir, skb, AF_INET6, 1);
  1018. }
  1019. int __xfrm_decode_session(struct sk_buff *skb, struct flowi *fl,
  1020. unsigned int family, int reverse);
  1021. static inline int xfrm_decode_session(struct sk_buff *skb, struct flowi *fl,
  1022. unsigned int family)
  1023. {
  1024. return __xfrm_decode_session(skb, fl, family, 0);
  1025. }
  1026. static inline int xfrm_decode_session_reverse(struct sk_buff *skb,
  1027. struct flowi *fl,
  1028. unsigned int family)
  1029. {
  1030. return __xfrm_decode_session(skb, fl, family, 1);
  1031. }
  1032. int __xfrm_route_forward(struct sk_buff *skb, unsigned short family);
  1033. static inline int xfrm_route_forward(struct sk_buff *skb, unsigned short family)
  1034. {
  1035. struct net *net = dev_net(skb->dev);
  1036. return !net->xfrm.policy_count[XFRM_POLICY_OUT] ||
  1037. (skb_dst(skb)->flags & DST_NOXFRM) ||
  1038. __xfrm_route_forward(skb, family);
  1039. }
  1040. static inline int xfrm4_route_forward(struct sk_buff *skb)
  1041. {
  1042. return xfrm_route_forward(skb, AF_INET);
  1043. }
  1044. static inline int xfrm6_route_forward(struct sk_buff *skb)
  1045. {
  1046. return xfrm_route_forward(skb, AF_INET6);
  1047. }
  1048. int __xfrm_sk_clone_policy(struct sock *sk, const struct sock *osk);
  1049. static inline int xfrm_sk_clone_policy(struct sock *sk, const struct sock *osk)
  1050. {
  1051. sk->sk_policy[0] = NULL;
  1052. sk->sk_policy[1] = NULL;
  1053. if (unlikely(osk->sk_policy[0] || osk->sk_policy[1]))
  1054. return __xfrm_sk_clone_policy(sk, osk);
  1055. return 0;
  1056. }
  1057. int xfrm_policy_delete(struct xfrm_policy *pol, int dir);
  1058. static inline void xfrm_sk_free_policy(struct sock *sk)
  1059. {
  1060. struct xfrm_policy *pol;
  1061. pol = rcu_dereference_protected(sk->sk_policy[0], 1);
  1062. if (unlikely(pol != NULL)) {
  1063. xfrm_policy_delete(pol, XFRM_POLICY_MAX);
  1064. sk->sk_policy[0] = NULL;
  1065. }
  1066. pol = rcu_dereference_protected(sk->sk_policy[1], 1);
  1067. if (unlikely(pol != NULL)) {
  1068. xfrm_policy_delete(pol, XFRM_POLICY_MAX+1);
  1069. sk->sk_policy[1] = NULL;
  1070. }
  1071. }
  1072. #else
  1073. static inline void xfrm_sk_free_policy(struct sock *sk) {}
  1074. static inline int xfrm_sk_clone_policy(struct sock *sk, const struct sock *osk) { return 0; }
  1075. static inline int xfrm6_route_forward(struct sk_buff *skb) { return 1; }
  1076. static inline int xfrm4_route_forward(struct sk_buff *skb) { return 1; }
  1077. static inline int xfrm6_policy_check(struct sock *sk, int dir, struct sk_buff *skb)
  1078. {
  1079. return 1;
  1080. }
  1081. static inline int xfrm4_policy_check(struct sock *sk, int dir, struct sk_buff *skb)
  1082. {
  1083. return 1;
  1084. }
  1085. static inline int xfrm_policy_check(struct sock *sk, int dir, struct sk_buff *skb, unsigned short family)
  1086. {
  1087. return 1;
  1088. }
  1089. static inline int xfrm_decode_session_reverse(struct sk_buff *skb,
  1090. struct flowi *fl,
  1091. unsigned int family)
  1092. {
  1093. return -ENOSYS;
  1094. }
  1095. static inline int xfrm4_policy_check_reverse(struct sock *sk, int dir,
  1096. struct sk_buff *skb)
  1097. {
  1098. return 1;
  1099. }
  1100. static inline int xfrm6_policy_check_reverse(struct sock *sk, int dir,
  1101. struct sk_buff *skb)
  1102. {
  1103. return 1;
  1104. }
  1105. #endif
  1106. static __inline__
  1107. xfrm_address_t *xfrm_flowi_daddr(const struct flowi *fl, unsigned short family)
  1108. {
  1109. switch (family){
  1110. case AF_INET:
  1111. return (xfrm_address_t *)&fl->u.ip4.daddr;
  1112. case AF_INET6:
  1113. return (xfrm_address_t *)&fl->u.ip6.daddr;
  1114. }
  1115. return NULL;
  1116. }
  1117. static __inline__
  1118. xfrm_address_t *xfrm_flowi_saddr(const struct flowi *fl, unsigned short family)
  1119. {
  1120. switch (family){
  1121. case AF_INET:
  1122. return (xfrm_address_t *)&fl->u.ip4.saddr;
  1123. case AF_INET6:
  1124. return (xfrm_address_t *)&fl->u.ip6.saddr;
  1125. }
  1126. return NULL;
  1127. }
  1128. static __inline__
  1129. void xfrm_flowi_addr_get(const struct flowi *fl,
  1130. xfrm_address_t *saddr, xfrm_address_t *daddr,
  1131. unsigned short family)
  1132. {
  1133. switch(family) {
  1134. case AF_INET:
  1135. memcpy(&saddr->a4, &fl->u.ip4.saddr, sizeof(saddr->a4));
  1136. memcpy(&daddr->a4, &fl->u.ip4.daddr, sizeof(daddr->a4));
  1137. break;
  1138. case AF_INET6:
  1139. saddr->in6 = fl->u.ip6.saddr;
  1140. daddr->in6 = fl->u.ip6.daddr;
  1141. break;
  1142. }
  1143. }
  1144. static __inline__ int
  1145. __xfrm4_state_addr_check(const struct xfrm_state *x,
  1146. const xfrm_address_t *daddr, const xfrm_address_t *saddr)
  1147. {
  1148. if (daddr->a4 == x->id.daddr.a4 &&
  1149. (saddr->a4 == x->props.saddr.a4 || !saddr->a4 || !x->props.saddr.a4))
  1150. return 1;
  1151. return 0;
  1152. }
  1153. static __inline__ int
  1154. __xfrm6_state_addr_check(const struct xfrm_state *x,
  1155. const xfrm_address_t *daddr, const xfrm_address_t *saddr)
  1156. {
  1157. if (ipv6_addr_equal((struct in6_addr *)daddr, (struct in6_addr *)&x->id.daddr) &&
  1158. (ipv6_addr_equal((struct in6_addr *)saddr, (struct in6_addr *)&x->props.saddr) ||
  1159. ipv6_addr_any((struct in6_addr *)saddr) ||
  1160. ipv6_addr_any((struct in6_addr *)&x->props.saddr)))
  1161. return 1;
  1162. return 0;
  1163. }
  1164. static __inline__ int
  1165. xfrm_state_addr_check(const struct xfrm_state *x,
  1166. const xfrm_address_t *daddr, const xfrm_address_t *saddr,
  1167. unsigned short family)
  1168. {
  1169. switch (family) {
  1170. case AF_INET:
  1171. return __xfrm4_state_addr_check(x, daddr, saddr);
  1172. case AF_INET6:
  1173. return __xfrm6_state_addr_check(x, daddr, saddr);
  1174. }
  1175. return 0;
  1176. }
  1177. static __inline__ int
  1178. xfrm_state_addr_flow_check(const struct xfrm_state *x, const struct flowi *fl,
  1179. unsigned short family)
  1180. {
  1181. switch (family) {
  1182. case AF_INET:
  1183. return __xfrm4_state_addr_check(x,
  1184. (const xfrm_address_t *)&fl->u.ip4.daddr,
  1185. (const xfrm_address_t *)&fl->u.ip4.saddr);
  1186. case AF_INET6:
  1187. return __xfrm6_state_addr_check(x,
  1188. (const xfrm_address_t *)&fl->u.ip6.daddr,
  1189. (const xfrm_address_t *)&fl->u.ip6.saddr);
  1190. }
  1191. return 0;
  1192. }
  1193. static inline int xfrm_state_kern(const struct xfrm_state *x)
  1194. {
  1195. return atomic_read(&x->tunnel_users);
  1196. }
  1197. static inline bool xfrm_id_proto_valid(u8 proto)
  1198. {
  1199. switch (proto) {
  1200. case IPPROTO_AH:
  1201. case IPPROTO_ESP:
  1202. case IPPROTO_COMP:
  1203. #if IS_ENABLED(CONFIG_IPV6)
  1204. case IPPROTO_ROUTING:
  1205. case IPPROTO_DSTOPTS:
  1206. #endif
  1207. return true;
  1208. default:
  1209. return false;
  1210. }
  1211. }
  1212. /* IPSEC_PROTO_ANY only matches 3 IPsec protocols, 0 could match all. */
  1213. static inline int xfrm_id_proto_match(u8 proto, u8 userproto)
  1214. {
  1215. return (!userproto || proto == userproto ||
  1216. (userproto == IPSEC_PROTO_ANY && (proto == IPPROTO_AH ||
  1217. proto == IPPROTO_ESP ||
  1218. proto == IPPROTO_COMP)));
  1219. }
  1220. /*
  1221. * xfrm algorithm information
  1222. */
  1223. struct xfrm_algo_aead_info {
  1224. char *geniv;
  1225. u16 icv_truncbits;
  1226. };
  1227. struct xfrm_algo_auth_info {
  1228. u16 icv_truncbits;
  1229. u16 icv_fullbits;
  1230. };
  1231. struct xfrm_algo_encr_info {
  1232. char *geniv;
  1233. u16 blockbits;
  1234. u16 defkeybits;
  1235. };
  1236. struct xfrm_algo_comp_info {
  1237. u16 threshold;
  1238. };
  1239. struct xfrm_algo_desc {
  1240. char *name;
  1241. char *compat;
  1242. u8 available:1;
  1243. u8 pfkey_supported:1;
  1244. union {
  1245. struct xfrm_algo_aead_info aead;
  1246. struct xfrm_algo_auth_info auth;
  1247. struct xfrm_algo_encr_info encr;
  1248. struct xfrm_algo_comp_info comp;
  1249. } uinfo;
  1250. struct sadb_alg desc;
  1251. };
  1252. /* XFRM protocol handlers. */
  1253. struct xfrm4_protocol {
  1254. int (*handler)(struct sk_buff *skb);
  1255. int (*input_handler)(struct sk_buff *skb, int nexthdr, __be32 spi,
  1256. int encap_type);
  1257. int (*cb_handler)(struct sk_buff *skb, int err);
  1258. int (*err_handler)(struct sk_buff *skb, u32 info);
  1259. struct xfrm4_protocol __rcu *next;
  1260. int priority;
  1261. };
  1262. struct xfrm6_protocol {
  1263. int (*handler)(struct sk_buff *skb);
  1264. int (*cb_handler)(struct sk_buff *skb, int err);
  1265. int (*err_handler)(struct sk_buff *skb, struct inet6_skb_parm *opt,
  1266. u8 type, u8 code, int offset, __be32 info);
  1267. struct xfrm6_protocol __rcu *next;
  1268. int priority;
  1269. };
  1270. /* XFRM tunnel handlers. */
  1271. struct xfrm_tunnel {
  1272. int (*handler)(struct sk_buff *skb);
  1273. int (*err_handler)(struct sk_buff *skb, u32 info);
  1274. struct xfrm_tunnel __rcu *next;
  1275. int priority;
  1276. };
  1277. struct xfrm6_tunnel {
  1278. int (*handler)(struct sk_buff *skb);
  1279. int (*err_handler)(struct sk_buff *skb, struct inet6_skb_parm *opt,
  1280. u8 type, u8 code, int offset, __be32 info);
  1281. struct xfrm6_tunnel __rcu *next;
  1282. int priority;
  1283. };
  1284. void xfrm_init(void);
  1285. void xfrm4_init(void);
  1286. int xfrm_state_init(struct net *net);
  1287. void xfrm_state_fini(struct net *net);
  1288. void xfrm4_state_init(void);
  1289. void xfrm4_protocol_init(void);
  1290. #ifdef CONFIG_XFRM
  1291. int xfrm6_init(void);
  1292. void xfrm6_fini(void);
  1293. int xfrm6_state_init(void);
  1294. void xfrm6_state_fini(void);
  1295. int xfrm6_protocol_init(void);
  1296. void xfrm6_protocol_fini(void);
  1297. #else
  1298. static inline int xfrm6_init(void)
  1299. {
  1300. return 0;
  1301. }
  1302. static inline void xfrm6_fini(void)
  1303. {
  1304. ;
  1305. }
  1306. #endif
  1307. #ifdef CONFIG_XFRM_STATISTICS
  1308. int xfrm_proc_init(struct net *net);
  1309. void xfrm_proc_fini(struct net *net);
  1310. #endif
  1311. int xfrm_sysctl_init(struct net *net);
  1312. #ifdef CONFIG_SYSCTL
  1313. void xfrm_sysctl_fini(struct net *net);
  1314. #else
  1315. static inline void xfrm_sysctl_fini(struct net *net)
  1316. {
  1317. }
  1318. #endif
  1319. void xfrm_state_walk_init(struct xfrm_state_walk *walk, u8 proto,
  1320. struct xfrm_address_filter *filter);
  1321. int xfrm_state_walk(struct net *net, struct xfrm_state_walk *walk,
  1322. int (*func)(struct xfrm_state *, int, void*), void *);
  1323. void xfrm_state_walk_done(struct xfrm_state_walk *walk, struct net *net);
  1324. struct xfrm_state *xfrm_state_alloc(struct net *net);
  1325. struct xfrm_state *xfrm_state_find(const xfrm_address_t *daddr,
  1326. const xfrm_address_t *saddr,
  1327. const struct flowi *fl,
  1328. struct xfrm_tmpl *tmpl,
  1329. struct xfrm_policy *pol, int *err,
  1330. unsigned short family, u32 if_id);
  1331. struct xfrm_state *xfrm_stateonly_find(struct net *net, u32 mark, u32 if_id,
  1332. xfrm_address_t *daddr,
  1333. xfrm_address_t *saddr,
  1334. unsigned short family,
  1335. u8 mode, u8 proto, u32 reqid);
  1336. struct xfrm_state *xfrm_state_lookup_byspi(struct net *net, __be32 spi,
  1337. unsigned short family);
  1338. int xfrm_state_check_expire(struct xfrm_state *x);
  1339. void xfrm_state_insert(struct xfrm_state *x);
  1340. int xfrm_state_add(struct xfrm_state *x);
  1341. int xfrm_state_update(struct xfrm_state *x);
  1342. struct xfrm_state *xfrm_state_lookup(struct net *net, u32 mark,
  1343. const xfrm_address_t *daddr, __be32 spi,
  1344. u8 proto, unsigned short family);
  1345. struct xfrm_state *xfrm_state_lookup_byaddr(struct net *net, u32 mark,
  1346. const xfrm_address_t *daddr,
  1347. const xfrm_address_t *saddr,
  1348. u8 proto,
  1349. unsigned short family);
  1350. #ifdef CONFIG_XFRM_SUB_POLICY
  1351. int xfrm_tmpl_sort(struct xfrm_tmpl **dst, struct xfrm_tmpl **src, int n,
  1352. unsigned short family, struct net *net);
  1353. int xfrm_state_sort(struct xfrm_state **dst, struct xfrm_state **src, int n,
  1354. unsigned short family);
  1355. #else
  1356. static inline int xfrm_tmpl_sort(struct xfrm_tmpl **dst, struct xfrm_tmpl **src,
  1357. int n, unsigned short family, struct net *net)
  1358. {
  1359. return -ENOSYS;
  1360. }
  1361. static inline int xfrm_state_sort(struct xfrm_state **dst, struct xfrm_state **src,
  1362. int n, unsigned short family)
  1363. {
  1364. return -ENOSYS;
  1365. }
  1366. #endif
  1367. struct xfrmk_sadinfo {
  1368. u32 sadhcnt; /* current hash bkts */
  1369. u32 sadhmcnt; /* max allowed hash bkts */
  1370. u32 sadcnt; /* current running count */
  1371. };
  1372. struct xfrmk_spdinfo {
  1373. u32 incnt;
  1374. u32 outcnt;
  1375. u32 fwdcnt;
  1376. u32 inscnt;
  1377. u32 outscnt;
  1378. u32 fwdscnt;
  1379. u32 spdhcnt;
  1380. u32 spdhmcnt;
  1381. };
  1382. struct xfrm_state *xfrm_find_acq_byseq(struct net *net, u32 mark, u32 seq);
  1383. int xfrm_state_delete(struct xfrm_state *x);
  1384. int xfrm_state_flush(struct net *net, u8 proto, bool task_valid);
  1385. int xfrm_dev_state_flush(struct net *net, struct net_device *dev, bool task_valid);
  1386. void xfrm_sad_getinfo(struct net *net, struct xfrmk_sadinfo *si);
  1387. void xfrm_spd_getinfo(struct net *net, struct xfrmk_spdinfo *si);
  1388. u32 xfrm_replay_seqhi(struct xfrm_state *x, __be32 net_seq);
  1389. int xfrm_init_replay(struct xfrm_state *x);
  1390. int xfrm_state_mtu(struct xfrm_state *x, int mtu);
  1391. int __xfrm_init_state(struct xfrm_state *x, bool init_replay, bool offload);
  1392. int xfrm_init_state(struct xfrm_state *x);
  1393. int xfrm_prepare_input(struct xfrm_state *x, struct sk_buff *skb);
  1394. int xfrm_input(struct sk_buff *skb, int nexthdr, __be32 spi, int encap_type);
  1395. int xfrm_input_resume(struct sk_buff *skb, int nexthdr);
  1396. int xfrm_trans_queue(struct sk_buff *skb,
  1397. int (*finish)(struct net *, struct sock *,
  1398. struct sk_buff *));
  1399. int xfrm_output_resume(struct sk_buff *skb, int err);
  1400. int xfrm_output(struct sock *sk, struct sk_buff *skb);
  1401. int xfrm_inner_extract_output(struct xfrm_state *x, struct sk_buff *skb);
  1402. void xfrm_local_error(struct sk_buff *skb, int mtu);
  1403. int xfrm4_extract_header(struct sk_buff *skb);
  1404. int xfrm4_extract_input(struct xfrm_state *x, struct sk_buff *skb);
  1405. int xfrm4_rcv_encap(struct sk_buff *skb, int nexthdr, __be32 spi,
  1406. int encap_type);
  1407. int xfrm4_transport_finish(struct sk_buff *skb, int async);
  1408. int xfrm4_rcv(struct sk_buff *skb);
  1409. int xfrm_parse_spi(struct sk_buff *skb, u8 nexthdr, __be32 *spi, __be32 *seq);
  1410. static inline int xfrm4_rcv_spi(struct sk_buff *skb, int nexthdr, __be32 spi)
  1411. {
  1412. XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip4 = NULL;
  1413. XFRM_SPI_SKB_CB(skb)->family = AF_INET;
  1414. XFRM_SPI_SKB_CB(skb)->daddroff = offsetof(struct iphdr, daddr);
  1415. return xfrm_input(skb, nexthdr, spi, 0);
  1416. }
  1417. int xfrm4_extract_output(struct xfrm_state *x, struct sk_buff *skb);
  1418. int xfrm4_prepare_output(struct xfrm_state *x, struct sk_buff *skb);
  1419. int xfrm4_output(struct net *net, struct sock *sk, struct sk_buff *skb);
  1420. int xfrm4_output_finish(struct sock *sk, struct sk_buff *skb);
  1421. int xfrm4_rcv_cb(struct sk_buff *skb, u8 protocol, int err);
  1422. int xfrm4_protocol_register(struct xfrm4_protocol *handler, unsigned char protocol);
  1423. int xfrm4_protocol_deregister(struct xfrm4_protocol *handler, unsigned char protocol);
  1424. int xfrm4_tunnel_register(struct xfrm_tunnel *handler, unsigned short family);
  1425. int xfrm4_tunnel_deregister(struct xfrm_tunnel *handler, unsigned short family);
  1426. void xfrm4_local_error(struct sk_buff *skb, u32 mtu);
  1427. int xfrm6_extract_header(struct sk_buff *skb);
  1428. int xfrm6_extract_input(struct xfrm_state *x, struct sk_buff *skb);
  1429. int xfrm6_rcv_spi(struct sk_buff *skb, int nexthdr, __be32 spi,
  1430. struct ip6_tnl *t);
  1431. int xfrm6_transport_finish(struct sk_buff *skb, int async);
  1432. int xfrm6_rcv_tnl(struct sk_buff *skb, struct ip6_tnl *t);
  1433. int xfrm6_rcv(struct sk_buff *skb);
  1434. int xfrm6_input_addr(struct sk_buff *skb, xfrm_address_t *daddr,
  1435. xfrm_address_t *saddr, u8 proto);
  1436. void xfrm6_local_error(struct sk_buff *skb, u32 mtu);
  1437. int xfrm6_rcv_cb(struct sk_buff *skb, u8 protocol, int err);
  1438. int xfrm6_protocol_register(struct xfrm6_protocol *handler, unsigned char protocol);
  1439. int xfrm6_protocol_deregister(struct xfrm6_protocol *handler, unsigned char protocol);
  1440. int xfrm6_tunnel_register(struct xfrm6_tunnel *handler, unsigned short family);
  1441. int xfrm6_tunnel_deregister(struct xfrm6_tunnel *handler, unsigned short family);
  1442. __be32 xfrm6_tunnel_alloc_spi(struct net *net, xfrm_address_t *saddr);
  1443. __be32 xfrm6_tunnel_spi_lookup(struct net *net, const xfrm_address_t *saddr);
  1444. int xfrm6_extract_output(struct xfrm_state *x, struct sk_buff *skb);
  1445. int xfrm6_prepare_output(struct xfrm_state *x, struct sk_buff *skb);
  1446. int xfrm6_output(struct net *net, struct sock *sk, struct sk_buff *skb);
  1447. int xfrm6_output_finish(struct sock *sk, struct sk_buff *skb);
  1448. int xfrm6_find_1stfragopt(struct xfrm_state *x, struct sk_buff *skb,
  1449. u8 **prevhdr);
  1450. #ifdef CONFIG_XFRM
  1451. int xfrm4_udp_encap_rcv(struct sock *sk, struct sk_buff *skb);
  1452. int xfrm_user_policy(struct sock *sk, int optname,
  1453. u8 __user *optval, int optlen);
  1454. #else
  1455. static inline int xfrm_user_policy(struct sock *sk, int optname, u8 __user *optval, int optlen)
  1456. {
  1457. return -ENOPROTOOPT;
  1458. }
  1459. static inline int xfrm4_udp_encap_rcv(struct sock *sk, struct sk_buff *skb)
  1460. {
  1461. /* should not happen */
  1462. kfree_skb(skb);
  1463. return 0;
  1464. }
  1465. #endif
  1466. struct dst_entry *__xfrm_dst_lookup(struct net *net, int tos, int oif,
  1467. const xfrm_address_t *saddr,
  1468. const xfrm_address_t *daddr,
  1469. int family, u32 mark);
  1470. struct xfrm_policy *xfrm_policy_alloc(struct net *net, gfp_t gfp);
  1471. void xfrm_policy_walk_init(struct xfrm_policy_walk *walk, u8 type);
  1472. int xfrm_policy_walk(struct net *net, struct xfrm_policy_walk *walk,
  1473. int (*func)(struct xfrm_policy *, int, int, void*),
  1474. void *);
  1475. void xfrm_policy_walk_done(struct xfrm_policy_walk *walk, struct net *net);
  1476. int xfrm_policy_insert(int dir, struct xfrm_policy *policy, int excl);
  1477. struct xfrm_policy *xfrm_policy_bysel_ctx(struct net *net, u32 mark, u32 if_id,
  1478. u8 type, int dir,
  1479. struct xfrm_selector *sel,
  1480. struct xfrm_sec_ctx *ctx, int delete,
  1481. int *err);
  1482. struct xfrm_policy *xfrm_policy_byid(struct net *net, u32 mark, u32 if_id, u8,
  1483. int dir, u32 id, int delete, int *err);
  1484. int xfrm_policy_flush(struct net *net, u8 type, bool task_valid);
  1485. void xfrm_policy_hash_rebuild(struct net *net);
  1486. u32 xfrm_get_acqseq(void);
  1487. int verify_spi_info(u8 proto, u32 min, u32 max);
  1488. int xfrm_alloc_spi(struct xfrm_state *x, u32 minspi, u32 maxspi);
  1489. struct xfrm_state *xfrm_find_acq(struct net *net, const struct xfrm_mark *mark,
  1490. u8 mode, u32 reqid, u32 if_id, u8 proto,
  1491. const xfrm_address_t *daddr,
  1492. const xfrm_address_t *saddr, int create,
  1493. unsigned short family);
  1494. int xfrm_sk_policy_insert(struct sock *sk, int dir, struct xfrm_policy *pol);
  1495. #ifdef CONFIG_XFRM_MIGRATE
  1496. int km_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
  1497. const struct xfrm_migrate *m, int num_bundles,
  1498. const struct xfrm_kmaddress *k,
  1499. const struct xfrm_encap_tmpl *encap);
  1500. struct xfrm_state *xfrm_migrate_state_find(struct xfrm_migrate *m, struct net *net);
  1501. struct xfrm_state *xfrm_state_migrate(struct xfrm_state *x,
  1502. struct xfrm_migrate *m,
  1503. struct xfrm_encap_tmpl *encap);
  1504. int xfrm_migrate(const struct xfrm_selector *sel, u8 dir, u8 type,
  1505. struct xfrm_migrate *m, int num_bundles,
  1506. struct xfrm_kmaddress *k, struct net *net,
  1507. struct xfrm_encap_tmpl *encap);
  1508. #endif
  1509. int km_new_mapping(struct xfrm_state *x, xfrm_address_t *ipaddr, __be16 sport);
  1510. void km_policy_expired(struct xfrm_policy *pol, int dir, int hard, u32 portid);
  1511. int km_report(struct net *net, u8 proto, struct xfrm_selector *sel,
  1512. xfrm_address_t *addr);
  1513. void xfrm_input_init(void);
  1514. int xfrm_parse_spi(struct sk_buff *skb, u8 nexthdr, __be32 *spi, __be32 *seq);
  1515. void xfrm_probe_algs(void);
  1516. int xfrm_count_pfkey_auth_supported(void);
  1517. int xfrm_count_pfkey_enc_supported(void);
  1518. struct xfrm_algo_desc *xfrm_aalg_get_byidx(unsigned int idx);
  1519. struct xfrm_algo_desc *xfrm_ealg_get_byidx(unsigned int idx);
  1520. struct xfrm_algo_desc *xfrm_aalg_get_byid(int alg_id);
  1521. struct xfrm_algo_desc *xfrm_ealg_get_byid(int alg_id);
  1522. struct xfrm_algo_desc *xfrm_calg_get_byid(int alg_id);
  1523. struct xfrm_algo_desc *xfrm_aalg_get_byname(const char *name, int probe);
  1524. struct xfrm_algo_desc *xfrm_ealg_get_byname(const char *name, int probe);
  1525. struct xfrm_algo_desc *xfrm_calg_get_byname(const char *name, int probe);
  1526. struct xfrm_algo_desc *xfrm_aead_get_byname(const char *name, int icv_len,
  1527. int probe);
  1528. static inline bool xfrm6_addr_equal(const xfrm_address_t *a,
  1529. const xfrm_address_t *b)
  1530. {
  1531. return ipv6_addr_equal((const struct in6_addr *)a,
  1532. (const struct in6_addr *)b);
  1533. }
  1534. static inline bool xfrm_addr_equal(const xfrm_address_t *a,
  1535. const xfrm_address_t *b,
  1536. sa_family_t family)
  1537. {
  1538. switch (family) {
  1539. default:
  1540. case AF_INET:
  1541. return ((__force u32)a->a4 ^ (__force u32)b->a4) == 0;
  1542. case AF_INET6:
  1543. return xfrm6_addr_equal(a, b);
  1544. }
  1545. }
  1546. static inline int xfrm_policy_id2dir(u32 index)
  1547. {
  1548. return index & 7;
  1549. }
  1550. #ifdef CONFIG_XFRM
  1551. static inline int xfrm_aevent_is_on(struct net *net)
  1552. {
  1553. struct sock *nlsk;
  1554. int ret = 0;
  1555. rcu_read_lock();
  1556. nlsk = rcu_dereference(net->xfrm.nlsk);
  1557. if (nlsk)
  1558. ret = netlink_has_listeners(nlsk, XFRMNLGRP_AEVENTS);
  1559. rcu_read_unlock();
  1560. return ret;
  1561. }
  1562. static inline int xfrm_acquire_is_on(struct net *net)
  1563. {
  1564. struct sock *nlsk;
  1565. int ret = 0;
  1566. rcu_read_lock();
  1567. nlsk = rcu_dereference(net->xfrm.nlsk);
  1568. if (nlsk)
  1569. ret = netlink_has_listeners(nlsk, XFRMNLGRP_ACQUIRE);
  1570. rcu_read_unlock();
  1571. return ret;
  1572. }
  1573. #endif
  1574. static inline int aead_len(struct xfrm_algo_aead *alg)
  1575. {
  1576. return sizeof(*alg) + ((alg->alg_key_len + 7) / 8);
  1577. }
  1578. static inline int xfrm_alg_len(const struct xfrm_algo *alg)
  1579. {
  1580. return sizeof(*alg) + ((alg->alg_key_len + 7) / 8);
  1581. }
  1582. static inline int xfrm_alg_auth_len(const struct xfrm_algo_auth *alg)
  1583. {
  1584. return sizeof(*alg) + ((alg->alg_key_len + 7) / 8);
  1585. }
  1586. static inline int xfrm_replay_state_esn_len(struct xfrm_replay_state_esn *replay_esn)
  1587. {
  1588. return sizeof(*replay_esn) + replay_esn->bmp_len * sizeof(__u32);
  1589. }
  1590. #ifdef CONFIG_XFRM_MIGRATE
  1591. static inline int xfrm_replay_clone(struct xfrm_state *x,
  1592. struct xfrm_state *orig)
  1593. {
  1594. x->replay_esn = kmemdup(orig->replay_esn,
  1595. xfrm_replay_state_esn_len(orig->replay_esn),
  1596. GFP_KERNEL);
  1597. if (!x->replay_esn)
  1598. return -ENOMEM;
  1599. x->preplay_esn = kmemdup(orig->preplay_esn,
  1600. xfrm_replay_state_esn_len(orig->preplay_esn),
  1601. GFP_KERNEL);
  1602. if (!x->preplay_esn)
  1603. return -ENOMEM;
  1604. return 0;
  1605. }
  1606. static inline struct xfrm_algo_aead *xfrm_algo_aead_clone(struct xfrm_algo_aead *orig)
  1607. {
  1608. return kmemdup(orig, aead_len(orig), GFP_KERNEL);
  1609. }
  1610. static inline struct xfrm_algo *xfrm_algo_clone(struct xfrm_algo *orig)
  1611. {
  1612. return kmemdup(orig, xfrm_alg_len(orig), GFP_KERNEL);
  1613. }
  1614. static inline struct xfrm_algo_auth *xfrm_algo_auth_clone(struct xfrm_algo_auth *orig)
  1615. {
  1616. return kmemdup(orig, xfrm_alg_auth_len(orig), GFP_KERNEL);
  1617. }
  1618. static inline void xfrm_states_put(struct xfrm_state **states, int n)
  1619. {
  1620. int i;
  1621. for (i = 0; i < n; i++)
  1622. xfrm_state_put(*(states + i));
  1623. }
  1624. static inline void xfrm_states_delete(struct xfrm_state **states, int n)
  1625. {
  1626. int i;
  1627. for (i = 0; i < n; i++)
  1628. xfrm_state_delete(*(states + i));
  1629. }
  1630. #endif
  1631. #ifdef CONFIG_XFRM
  1632. static inline struct xfrm_state *xfrm_input_state(struct sk_buff *skb)
  1633. {
  1634. return skb->sp->xvec[skb->sp->len - 1];
  1635. }
  1636. static inline struct xfrm_offload *xfrm_offload(struct sk_buff *skb)
  1637. {
  1638. struct sec_path *sp = skb->sp;
  1639. if (!sp || !sp->olen || sp->len != sp->olen)
  1640. return NULL;
  1641. return &sp->ovec[sp->olen - 1];
  1642. }
  1643. #endif
  1644. void __net_init xfrm_dev_init(void);
  1645. #ifdef CONFIG_XFRM_OFFLOAD
  1646. int validate_xmit_xfrm(struct sk_buff *skb, netdev_features_t features);
  1647. int xfrm_dev_state_add(struct net *net, struct xfrm_state *x,
  1648. struct xfrm_user_offload *xuo);
  1649. bool xfrm_dev_offload_ok(struct sk_buff *skb, struct xfrm_state *x);
  1650. static inline bool xfrm_dst_offload_ok(struct dst_entry *dst)
  1651. {
  1652. struct xfrm_state *x = dst->xfrm;
  1653. if (!x || !x->type_offload)
  1654. return false;
  1655. if (x->xso.offload_handle && (x->xso.dev == dst->path->dev) &&
  1656. !dst->child->xfrm)
  1657. return true;
  1658. return false;
  1659. }
  1660. static inline void xfrm_dev_state_delete(struct xfrm_state *x)
  1661. {
  1662. struct xfrm_state_offload *xso = &x->xso;
  1663. if (xso->dev)
  1664. xso->dev->xfrmdev_ops->xdo_dev_state_delete(x);
  1665. }
  1666. static inline void xfrm_dev_state_free(struct xfrm_state *x)
  1667. {
  1668. struct xfrm_state_offload *xso = &x->xso;
  1669. struct net_device *dev = xso->dev;
  1670. if (dev && dev->xfrmdev_ops) {
  1671. dev->xfrmdev_ops->xdo_dev_state_free(x);
  1672. xso->dev = NULL;
  1673. dev_put(dev);
  1674. }
  1675. }
  1676. #else
  1677. static inline int validate_xmit_xfrm(struct sk_buff *skb, netdev_features_t features)
  1678. {
  1679. return 0;
  1680. }
  1681. static inline int xfrm_dev_state_add(struct net *net, struct xfrm_state *x, struct xfrm_user_offload *xuo)
  1682. {
  1683. return 0;
  1684. }
  1685. static inline void xfrm_dev_state_delete(struct xfrm_state *x)
  1686. {
  1687. }
  1688. static inline void xfrm_dev_state_free(struct xfrm_state *x)
  1689. {
  1690. }
  1691. static inline bool xfrm_dev_offload_ok(struct sk_buff *skb, struct xfrm_state *x)
  1692. {
  1693. return false;
  1694. }
  1695. static inline bool xfrm_dst_offload_ok(struct dst_entry *dst)
  1696. {
  1697. return false;
  1698. }
  1699. #endif
  1700. static inline int xfrm_mark_get(struct nlattr **attrs, struct xfrm_mark *m)
  1701. {
  1702. if (attrs[XFRMA_MARK])
  1703. memcpy(m, nla_data(attrs[XFRMA_MARK]), sizeof(struct xfrm_mark));
  1704. else
  1705. m->v = m->m = 0;
  1706. return m->v & m->m;
  1707. }
  1708. static inline int xfrm_mark_put(struct sk_buff *skb, const struct xfrm_mark *m)
  1709. {
  1710. int ret = 0;
  1711. if (m->m | m->v)
  1712. ret = nla_put(skb, XFRMA_MARK, sizeof(struct xfrm_mark), m);
  1713. return ret;
  1714. }
  1715. static inline __u32 xfrm_smark_get(__u32 mark, struct xfrm_state *x)
  1716. {
  1717. struct xfrm_mark *m = &x->props.smark;
  1718. return (m->v & m->m) | (mark & ~m->m);
  1719. }
  1720. static inline int xfrm_if_id_put(struct sk_buff *skb, __u32 if_id)
  1721. {
  1722. int ret = 0;
  1723. if (if_id)
  1724. ret = nla_put_u32(skb, XFRMA_IF_ID, if_id);
  1725. return ret;
  1726. }
  1727. static inline int xfrm_tunnel_check(struct sk_buff *skb, struct xfrm_state *x,
  1728. unsigned int family)
  1729. {
  1730. bool tunnel = false;
  1731. switch(family) {
  1732. case AF_INET:
  1733. if (XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip4)
  1734. tunnel = true;
  1735. break;
  1736. case AF_INET6:
  1737. if (XFRM_TUNNEL_SKB_CB(skb)->tunnel.ip6)
  1738. tunnel = true;
  1739. break;
  1740. }
  1741. if (tunnel && !(x->outer_mode->flags & XFRM_MODE_FLAG_TUNNEL))
  1742. return -EINVAL;
  1743. return 0;
  1744. }
  1745. extern const int xfrm_msg_min[XFRM_NR_MSGTYPES];
  1746. extern const struct nla_policy xfrma_policy[XFRMA_MAX+1];
  1747. struct xfrm_translator {
  1748. /* Allocate frag_list and put compat translation there */
  1749. int (*alloc_compat)(struct sk_buff *skb, const struct nlmsghdr *src);
  1750. /* Allocate nlmsg with 64-bit translaton of received 32-bit message */
  1751. struct nlmsghdr *(*rcv_msg_compat)(const struct nlmsghdr *nlh,
  1752. int maxtype, const struct nla_policy *policy,
  1753. struct netlink_ext_ack *extack);
  1754. /* Translate 32-bit user_policy from sockptr */
  1755. int (*xlate_user_policy_sockptr)(u8 **pdata32, int optlen);
  1756. struct module *owner;
  1757. };
  1758. #if IS_ENABLED(CONFIG_XFRM_USER_COMPAT)
  1759. extern int xfrm_register_translator(struct xfrm_translator *xtr);
  1760. extern int xfrm_unregister_translator(struct xfrm_translator *xtr);
  1761. extern struct xfrm_translator *xfrm_get_translator(void);
  1762. extern void xfrm_put_translator(struct xfrm_translator *xtr);
  1763. #else
  1764. static inline struct xfrm_translator *xfrm_get_translator(void)
  1765. {
  1766. return NULL;
  1767. }
  1768. static inline void xfrm_put_translator(struct xfrm_translator *xtr)
  1769. {
  1770. }
  1771. #endif
  1772. #endif /* _NET_XFRM_H */