lib.rs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  1. mod expand;
  2. use expand::{
  3. Args, BtfTracePoint, CgroupSkb, CgroupSock, CgroupSockAddr, CgroupSockopt, CgroupSysctl,
  4. FEntry, FExit, Lsm, Map, PerfEvent, Probe, ProbeKind, RawTracePoint, SchedClassifier, SkLookup,
  5. SkMsg, SkSkb, SkSkbKind, SockAddrArgs, SockOps, SocketFilter, SockoptArgs, TracePoint, Xdp,
  6. };
  7. use proc_macro::TokenStream;
  8. use syn::{parse_macro_input, ItemFn, ItemStatic};
  9. #[proc_macro_attribute]
  10. pub fn map(attrs: TokenStream, item: TokenStream) -> TokenStream {
  11. let args = parse_macro_input!(attrs as Args);
  12. let item = parse_macro_input!(item as ItemStatic);
  13. Map::from_syn(args, item)
  14. .and_then(|u| u.expand())
  15. .unwrap_or_else(|err| err.to_compile_error())
  16. .into()
  17. }
  18. #[proc_macro_attribute]
  19. pub fn kprobe(attrs: TokenStream, item: TokenStream) -> TokenStream {
  20. probe(ProbeKind::KProbe, attrs, item)
  21. }
  22. #[proc_macro_attribute]
  23. pub fn kretprobe(attrs: TokenStream, item: TokenStream) -> TokenStream {
  24. probe(ProbeKind::KRetProbe, attrs, item)
  25. }
  26. #[proc_macro_attribute]
  27. pub fn uprobe(attrs: TokenStream, item: TokenStream) -> TokenStream {
  28. probe(ProbeKind::UProbe, attrs, item)
  29. }
  30. #[proc_macro_attribute]
  31. pub fn uretprobe(attrs: TokenStream, item: TokenStream) -> TokenStream {
  32. probe(ProbeKind::URetProbe, attrs, item)
  33. }
  34. #[proc_macro_attribute]
  35. pub fn sock_ops(attrs: TokenStream, item: TokenStream) -> TokenStream {
  36. let args = parse_macro_input!(attrs as Args);
  37. let item = parse_macro_input!(item as ItemFn);
  38. SockOps::from_syn(args, item)
  39. .and_then(|u| u.expand())
  40. .unwrap_or_else(|err| err.to_compile_error())
  41. .into()
  42. }
  43. #[proc_macro_attribute]
  44. pub fn sk_msg(attrs: TokenStream, item: TokenStream) -> TokenStream {
  45. let args = parse_macro_input!(attrs as Args);
  46. let item = parse_macro_input!(item as ItemFn);
  47. SkMsg::from_syn(args, item)
  48. .and_then(|u| u.expand())
  49. .unwrap_or_else(|err| err.to_compile_error())
  50. .into()
  51. }
  52. #[proc_macro_attribute]
  53. pub fn xdp(attrs: TokenStream, item: TokenStream) -> TokenStream {
  54. let args = parse_macro_input!(attrs as Args);
  55. let item = parse_macro_input!(item as ItemFn);
  56. Xdp::from_syn(args, item)
  57. .and_then(|u| u.expand())
  58. .unwrap_or_else(|err| err.to_compile_error())
  59. .into()
  60. }
  61. #[proc_macro_attribute]
  62. pub fn classifier(attrs: TokenStream, item: TokenStream) -> TokenStream {
  63. let args = parse_macro_input!(attrs as Args);
  64. let item = parse_macro_input!(item as ItemFn);
  65. SchedClassifier::from_syn(args, item)
  66. .and_then(|u| u.expand())
  67. .unwrap_or_else(|err| err.to_compile_error())
  68. .into()
  69. }
  70. #[proc_macro_attribute]
  71. pub fn cgroup_sysctl(attrs: TokenStream, item: TokenStream) -> TokenStream {
  72. let args = parse_macro_input!(attrs as Args);
  73. let item = parse_macro_input!(item as ItemFn);
  74. CgroupSysctl::from_syn(args, item)
  75. .and_then(|u| u.expand())
  76. .unwrap_or_else(|err| err.to_compile_error())
  77. .into()
  78. }
  79. #[proc_macro_attribute]
  80. pub fn cgroup_sockopt(attrs: TokenStream, item: TokenStream) -> TokenStream {
  81. let args = parse_macro_input!(attrs as SockoptArgs);
  82. let attach_type = args.attach_type.to_string();
  83. let item = parse_macro_input!(item as ItemFn);
  84. CgroupSockopt::from_syn(args.args, item, attach_type)
  85. .and_then(|u| u.expand())
  86. .unwrap_or_else(|err| err.to_compile_error())
  87. .into()
  88. }
  89. #[proc_macro_attribute]
  90. pub fn cgroup_skb(attrs: TokenStream, item: TokenStream) -> TokenStream {
  91. let args = parse_macro_input!(attrs as Args);
  92. let item = parse_macro_input!(item as ItemFn);
  93. CgroupSkb::from_syn(args, item)
  94. .and_then(|u| u.expand())
  95. .unwrap_or_else(|err| err.to_compile_error())
  96. .into()
  97. }
  98. #[proc_macro_attribute]
  99. pub fn cgroup_sock_addr(attrs: TokenStream, item: TokenStream) -> TokenStream {
  100. let args = parse_macro_input!(attrs as SockAddrArgs);
  101. let attach_type = args.attach_type.to_string();
  102. let item = parse_macro_input!(item as ItemFn);
  103. CgroupSockAddr::from_syn(args.args, item, attach_type)
  104. .and_then(|u| u.expand())
  105. .unwrap_or_else(|err| err.to_compile_error())
  106. .into()
  107. }
  108. #[proc_macro_attribute]
  109. pub fn cgroup_sock(attrs: TokenStream, item: TokenStream) -> TokenStream {
  110. let args = parse_macro_input!(attrs as Args);
  111. let item = parse_macro_input!(item as ItemFn);
  112. CgroupSock::from_syn(args, item)
  113. .and_then(|u| u.expand())
  114. .unwrap_or_else(|err| err.to_compile_error())
  115. .into()
  116. }
  117. fn probe(kind: ProbeKind, attrs: TokenStream, item: TokenStream) -> TokenStream {
  118. let args = parse_macro_input!(attrs as Args);
  119. let item = parse_macro_input!(item as ItemFn);
  120. Probe::from_syn(kind, args, item)
  121. .and_then(|u| u.expand())
  122. .unwrap_or_else(|err| err.to_compile_error())
  123. .into()
  124. }
  125. #[proc_macro_attribute]
  126. pub fn tracepoint(attrs: TokenStream, item: TokenStream) -> TokenStream {
  127. let args = parse_macro_input!(attrs as Args);
  128. let item = parse_macro_input!(item as ItemFn);
  129. TracePoint::from_syn(args, item)
  130. .and_then(|u| u.expand())
  131. .unwrap_or_else(|err| err.to_compile_error())
  132. .into()
  133. }
  134. #[proc_macro_attribute]
  135. pub fn perf_event(attrs: TokenStream, item: TokenStream) -> TokenStream {
  136. let args = parse_macro_input!(attrs as Args);
  137. let item = parse_macro_input!(item as ItemFn);
  138. PerfEvent::from_syn(args, item)
  139. .and_then(|u| u.expand())
  140. .unwrap_or_else(|err| err.to_compile_error())
  141. .into()
  142. }
  143. /// Marks a function as a raw tracepoint eBPF program that can be attached at a
  144. /// pre-defined kernel trace point.
  145. ///
  146. /// The kernel provides a set of pre-defined trace points that eBPF programs can
  147. /// be attached to. See `/sys/kernel/debug/tracing/events` for a list of which
  148. /// events can be traced.
  149. ///
  150. /// # Minimum kernel version
  151. ///
  152. /// The minimum kernel version required to use this feature is 4.7.
  153. ///
  154. /// # Examples
  155. ///
  156. /// ```no_run
  157. /// use aya_bpf::{macros::raw_tracepoint, programs::RawTracePointContext};
  158. ///
  159. /// #[raw_tracepoint(name = "sys_enter")]
  160. /// pub fn sys_enter(ctx: RawTracePointContext) -> i32 {
  161. /// match unsafe { try_sys_enter(ctx) } {
  162. /// Ok(ret) => ret,
  163. /// Err(ret) => ret,
  164. /// }
  165. /// }
  166. ///
  167. /// unsafe fn try_sys_enter(_ctx: RawTracePointContext) -> Result<i32, i32> {
  168. /// Ok(0)
  169. /// }
  170. /// ```
  171. #[proc_macro_attribute]
  172. pub fn raw_tracepoint(attrs: TokenStream, item: TokenStream) -> TokenStream {
  173. let args = parse_macro_input!(attrs as Args);
  174. let item = parse_macro_input!(item as ItemFn);
  175. RawTracePoint::from_syn(args, item)
  176. .and_then(|u| u.expand())
  177. .unwrap_or_else(|err| err.to_compile_error())
  178. .into()
  179. }
  180. /// Marks a function as an LSM program that can be attached to Linux LSM hooks.
  181. /// Used to implement security policy and audit logging.
  182. ///
  183. /// LSM probes can be attached to the kernel's security hooks to implement mandatory
  184. /// access control policy and security auditing.
  185. ///
  186. /// LSM probes require a kernel compiled with `CONFIG_BPF_LSM=y` and `CONFIG_DEBUG_INFO_BTF=y`.
  187. /// In order for the probes to fire, you also need the BPF LSM to be enabled through your
  188. /// kernel's boot paramters (like `lsm=lockdown,yama,bpf`).
  189. ///
  190. /// # Minimum kernel version
  191. ///
  192. /// The minimum kernel version required to use this feature is 5.7.
  193. ///
  194. /// # Examples
  195. ///
  196. /// ```no_run
  197. /// use aya_bpf::{macros::lsm, programs::LsmContext};
  198. ///
  199. /// #[lsm(name = "file_open")]
  200. /// pub fn file_open(ctx: LsmContext) -> i32 {
  201. /// match unsafe { try_file_open(ctx) } {
  202. /// Ok(ret) => ret,
  203. /// Err(ret) => ret,
  204. /// }
  205. /// }
  206. ///
  207. /// unsafe fn try_file_open(_ctx: LsmContext) -> Result<i32, i32> {
  208. /// Ok(0)
  209. /// }
  210. /// ```
  211. #[proc_macro_attribute]
  212. pub fn lsm(attrs: TokenStream, item: TokenStream) -> TokenStream {
  213. let args = parse_macro_input!(attrs as Args);
  214. let item = parse_macro_input!(item as ItemFn);
  215. Lsm::from_syn(args, item)
  216. .and_then(|u| u.expand())
  217. .unwrap_or_else(|err| err.to_compile_error())
  218. .into()
  219. }
  220. /// Marks a function as a [BTF-enabled raw tracepoint][1] eBPF program that can be attached at
  221. /// a pre-defined kernel trace point.
  222. ///
  223. /// The kernel provides a set of pre-defined trace points that eBPF programs can
  224. /// be attached to. See `/sys/kernel/debug/tracing/events` for a list of which
  225. /// events can be traced.
  226. ///
  227. /// # Minimum kernel version
  228. ///
  229. /// The minimum kernel version required to use this feature is 5.5.
  230. ///
  231. /// # Examples
  232. ///
  233. /// ```no_run
  234. /// use aya_bpf::{macros::btf_tracepoint, programs::BtfTracePointContext};
  235. ///
  236. /// #[btf_tracepoint(name = "sched_process_fork")]
  237. /// pub fn sched_process_fork(ctx: BtfTracePointContext) -> u32 {
  238. /// match unsafe { try_sched_process_fork(ctx) } {
  239. /// Ok(ret) => ret,
  240. /// Err(ret) => ret,
  241. /// }
  242. /// }
  243. ///
  244. /// unsafe fn try_sched_process_fork(_ctx: BtfTracePointContext) -> Result<u32, u32> {
  245. /// Ok(0)
  246. /// }
  247. /// ```
  248. ///
  249. /// [1]: https://github.com/torvalds/linux/commit/9e15db66136a14cde3f35691f1d839d950118826
  250. #[proc_macro_attribute]
  251. pub fn btf_tracepoint(attrs: TokenStream, item: TokenStream) -> TokenStream {
  252. let args = parse_macro_input!(attrs as Args);
  253. let item = parse_macro_input!(item as ItemFn);
  254. BtfTracePoint::from_syn(args, item)
  255. .and_then(|u| u.expand())
  256. .unwrap_or_else(|err| err.to_compile_error())
  257. .into()
  258. }
  259. /// Marks a function as a SK_SKB Stream Parser eBPF program that can be attached
  260. /// to a SockMap
  261. ///
  262. /// # Minimum kernel version
  263. ///
  264. /// The minimum kernel version required to use this feature is 4.14
  265. ///
  266. /// # Examples
  267. ///
  268. /// ```no_run
  269. /// use aya_bpf::{macros::stream_parser, programs::SkBuffContext};
  270. ///
  271. ///
  272. ///#[stream_parser]
  273. ///fn stream_parser(ctx: SkBuffContext) -> u32 {
  274. /// match { try_stream_parser(ctx) } {
  275. /// Ok(ret) => ret,
  276. /// Err(ret) => ret,
  277. /// }
  278. ///}
  279. ///
  280. ///fn try_stream_parser(ctx: SkBuffContext) -> Result<u32, u32> {
  281. /// Ok(ctx.len())
  282. ///}
  283. /// ```
  284. #[proc_macro_attribute]
  285. pub fn stream_parser(attrs: TokenStream, item: TokenStream) -> TokenStream {
  286. sk_skb(SkSkbKind::StreamParser, attrs, item)
  287. }
  288. /// Marks a function as a SK_SKB Stream Verdict eBPF program that can be attached
  289. /// to a SockMap
  290. ///
  291. /// # Minimum kernel version
  292. ///
  293. /// The minimum kernel version required to use this feature is 4.14
  294. ///
  295. /// # Examples
  296. ///
  297. /// ```no_run
  298. /// use aya_bpf::{macros::stream_verdict, programs::SkBuffContext, bindings::sk_action};
  299. ///
  300. ///
  301. ///#[stream_verdict]
  302. ///fn stream_verdict(ctx: SkBuffContext) -> u32 {
  303. /// match { try_stream_verdict(ctx) } {
  304. /// Ok(ret) => ret,
  305. /// Err(ret) => ret,
  306. /// }
  307. ///}
  308. ///
  309. ///fn try_stream_verdict(_ctx: SkBuffContext) -> Result<u32, u32> {
  310. /// Ok(sk_action::SK_PASS)
  311. ///}
  312. /// ```
  313. #[proc_macro_attribute]
  314. pub fn stream_verdict(attrs: TokenStream, item: TokenStream) -> TokenStream {
  315. sk_skb(SkSkbKind::StreamVerdict, attrs, item)
  316. }
  317. fn sk_skb(kind: SkSkbKind, attrs: TokenStream, item: TokenStream) -> TokenStream {
  318. let args = parse_macro_input!(attrs as Args);
  319. let item = parse_macro_input!(item as ItemFn);
  320. SkSkb::from_syn(kind, args, item)
  321. .and_then(|u| u.expand())
  322. .unwrap_or_else(|err| err.to_compile_error())
  323. .into()
  324. }
  325. /// Marks a function as a eBPF Socket Filter program that can be attached to
  326. /// a socket.
  327. ///
  328. /// # Minimum kernel version
  329. ///
  330. /// The minimum kernel version required to use this feature is 3.19
  331. ///
  332. /// # Examples
  333. ///
  334. /// ```no_run
  335. /// use aya_bpf::{macros::socket_filter, programs::SkBuffContext};
  336. ///
  337. /// #[socket_filter(name = "accept_all")]
  338. /// pub fn accept_all(_ctx: SkBuffContext) -> i64 {
  339. /// return 0
  340. /// }
  341. /// ```
  342. #[proc_macro_attribute]
  343. pub fn socket_filter(attrs: TokenStream, item: TokenStream) -> TokenStream {
  344. let args = parse_macro_input!(attrs as Args);
  345. let item = parse_macro_input!(item as ItemFn);
  346. SocketFilter::from_syn(args, item)
  347. .and_then(|u| u.expand())
  348. .unwrap_or_else(|err| err.to_compile_error())
  349. .into()
  350. }
  351. /// Marks a function as a fentry eBPF program that can be attached to almost
  352. /// any function inside the kernel. The difference between fentry and kprobe
  353. /// is that fexit has practically zero overhead to call before kernel function.
  354. /// fentry programs can be also attached to other eBPF programs.
  355. ///
  356. /// # Minimumm kernel version
  357. ///
  358. /// The minimum kernel version required to use this feature is 5.5.
  359. ///
  360. /// # Examples
  361. ///
  362. /// ```no_run
  363. /// # #![allow(non_camel_case_types)]
  364. /// use aya_bpf::{macros::fentry, programs::FEntryContext};
  365. /// # type filename = u32;
  366. /// # type path = u32;
  367. ///
  368. /// #[fentry(name = "filename_lookup")]
  369. /// fn filename_lookup(ctx: FEntryContext) -> i32 {
  370. /// match unsafe { try_filename_lookup(ctx) } {
  371. /// Ok(ret) => ret,
  372. /// Err(ret) => ret,
  373. /// }
  374. /// }
  375. ///
  376. /// unsafe fn try_filename_lookup(ctx: FEntryContext) -> Result<i32, i32> {
  377. /// let _f: *const filename = ctx.arg(1);
  378. /// let _p: *const path = ctx.arg(3);
  379. ///
  380. /// Ok(0)
  381. /// }
  382. /// ```
  383. #[proc_macro_attribute]
  384. pub fn fentry(attrs: TokenStream, item: TokenStream) -> TokenStream {
  385. let args = parse_macro_input!(attrs as Args);
  386. let item = parse_macro_input!(item as ItemFn);
  387. FEntry::from_syn(args, item)
  388. .and_then(|u| u.expand())
  389. .unwrap_or_else(|err| err.to_compile_error())
  390. .into()
  391. }
  392. /// Marks a function as a fexit eBPF program that can be attached to almost
  393. /// any function inside the kernel. The difference between fexit and kretprobe
  394. /// is that fexit has practically zero overhead to call after kernel function
  395. /// and it focuses on access to arguments rather than the return value. fexit
  396. /// programs can be also attached to other eBPF programs
  397. ///
  398. /// # Minimumm kernel version
  399. ///
  400. /// The minimum kernel version required to use this feature is 5.5.
  401. ///
  402. /// # Examples
  403. ///
  404. /// ```no_run
  405. /// # #![allow(non_camel_case_types)]
  406. /// use aya_bpf::{macros::fexit, programs::FExitContext};
  407. /// # type filename = u32;
  408. /// # type path = u32;
  409. ///
  410. /// #[fexit(name = "filename_lookup")]
  411. /// fn filename_lookup(ctx: FExitContext) -> i32 {
  412. /// match unsafe { try_filename_lookup(ctx) } {
  413. /// Ok(ret) => ret,
  414. /// Err(ret) => ret,
  415. /// }
  416. /// }
  417. ///
  418. /// unsafe fn try_filename_lookup(ctx: FExitContext) -> Result<i32, i32> {
  419. /// let _f: *const filename = ctx.arg(1);
  420. /// let _p: *const path = ctx.arg(3);
  421. ///
  422. /// Ok(0)
  423. /// }
  424. /// ```
  425. #[proc_macro_attribute]
  426. pub fn fexit(attrs: TokenStream, item: TokenStream) -> TokenStream {
  427. let args = parse_macro_input!(attrs as Args);
  428. let item = parse_macro_input!(item as ItemFn);
  429. FExit::from_syn(args, item)
  430. .and_then(|u| u.expand())
  431. .unwrap_or_else(|err| err.to_compile_error())
  432. .into()
  433. }
  434. /// Marks a function as an eBPF Socket Lookup program that can be attached to
  435. /// a network namespace.
  436. ///
  437. /// # Minimum kernel version
  438. ///
  439. /// The minimum kernel version required to use this feature is 5.9
  440. ///
  441. /// # Examples
  442. ///
  443. /// ```no_run
  444. /// use aya_bpf::{macros::sk_lookup, programs::SkLookupContext};
  445. ///
  446. /// #[sk_lookup(name = "redirect")]
  447. /// pub fn accept_all(_ctx: SkLookupContext) -> u32 {
  448. /// // use sk_assign to redirect
  449. /// return 0
  450. /// }
  451. /// ```
  452. #[proc_macro_attribute]
  453. pub fn sk_lookup(attrs: TokenStream, item: TokenStream) -> TokenStream {
  454. let args = parse_macro_input!(attrs as Args);
  455. let item = parse_macro_input!(item as ItemFn);
  456. SkLookup::from_syn(args, item)
  457. .and_then(|u| u.expand())
  458. .unwrap_or_else(|err| err.to_compile_error())
  459. .into()
  460. }