netfilter.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. #ifndef __LINUX_NETFILTER_H
  2. #define __LINUX_NETFILTER_H
  3. #ifdef __KERNEL__
  4. #include <linux/init.h>
  5. #include <linux/skbuff.h>
  6. #include <linux/net.h>
  7. #include <linux/if.h>
  8. #include <linux/in.h>
  9. #include <linux/in6.h>
  10. #include <linux/wait.h>
  11. #include <linux/list.h>
  12. #endif
  13. #include <linux/types.h>
  14. #include <linux/compiler.h>
  15. #include <linux/sysctl.h>
  16. /* Responses from hook functions. */
  17. #define NF_DROP 0
  18. #define NF_ACCEPT 1
  19. #define NF_STOLEN 2
  20. #define NF_QUEUE 3
  21. #define NF_REPEAT 4
  22. #define NF_STOP 5
  23. #define NF_MAX_VERDICT NF_STOP
  24. /* we overload the higher bits for encoding auxiliary data such as the queue
  25. * number or errno values. Not nice, but better than additional function
  26. * arguments. */
  27. #define NF_VERDICT_MASK 0x000000ff
  28. /* extra verdict flags have mask 0x0000ff00 */
  29. #define NF_VERDICT_FLAG_QUEUE_BYPASS 0x00008000
  30. /* queue number (NF_QUEUE) or errno (NF_DROP) */
  31. #define NF_VERDICT_QMASK 0xffff0000
  32. #define NF_VERDICT_QBITS 16
  33. #define NF_QUEUE_NR(x) ((((x) << 16) & NF_VERDICT_QMASK) | NF_QUEUE)
  34. #define NF_DROP_ERR(x) (((-x) << 16) | NF_DROP)
  35. /* only for userspace compatibility */
  36. #ifndef __KERNEL__
  37. /* Generic cache responses from hook functions.
  38. <= 0x2000 is used for protocol-flags. */
  39. #define NFC_UNKNOWN 0x4000
  40. #define NFC_ALTERED 0x8000
  41. /* NF_VERDICT_BITS should be 8 now, but userspace might break if this changes */
  42. #define NF_VERDICT_BITS 16
  43. #endif
  44. enum nf_inet_hooks {
  45. NF_INET_PRE_ROUTING,
  46. NF_INET_LOCAL_IN,
  47. NF_INET_FORWARD,
  48. NF_INET_LOCAL_OUT,
  49. NF_INET_POST_ROUTING,
  50. NF_INET_NUMHOOKS
  51. };
  52. enum {
  53. NFPROTO_UNSPEC = 0,
  54. NFPROTO_IPV4 = 2,
  55. NFPROTO_ARP = 3,
  56. NFPROTO_BRIDGE = 7,
  57. NFPROTO_IPV6 = 10,
  58. NFPROTO_DECNET = 12,
  59. NFPROTO_NUMPROTO,
  60. };
  61. union nf_inet_addr {
  62. __u32 all[4];
  63. __be32 ip;
  64. __be32 ip6[4];
  65. struct in_addr in;
  66. struct in6_addr in6;
  67. };
  68. #ifdef __KERNEL__
  69. #ifdef CONFIG_NETFILTER
  70. static inline int NF_DROP_GETERR(int verdict)
  71. {
  72. return -(verdict >> NF_VERDICT_QBITS);
  73. }
  74. static inline int nf_inet_addr_cmp(const union nf_inet_addr *a1,
  75. const union nf_inet_addr *a2)
  76. {
  77. return a1->all[0] == a2->all[0] &&
  78. a1->all[1] == a2->all[1] &&
  79. a1->all[2] == a2->all[2] &&
  80. a1->all[3] == a2->all[3];
  81. }
  82. extern void netfilter_init(void);
  83. /* Largest hook number + 1 */
  84. #define NF_MAX_HOOKS 8
  85. struct sk_buff;
  86. typedef unsigned int nf_hookfn(unsigned int hooknum,
  87. struct sk_buff *skb,
  88. const struct net_device *in,
  89. const struct net_device *out,
  90. int (*okfn)(struct sk_buff *));
  91. struct nf_hook_ops {
  92. struct list_head list;
  93. /* User fills in from here down. */
  94. nf_hookfn *hook;
  95. struct module *owner;
  96. u_int8_t pf;
  97. unsigned int hooknum;
  98. /* Hooks are ordered in ascending priority. */
  99. int priority;
  100. };
  101. struct nf_sockopt_ops {
  102. struct list_head list;
  103. u_int8_t pf;
  104. /* Non-inclusive ranges: use 0/0/NULL to never get called. */
  105. int set_optmin;
  106. int set_optmax;
  107. int (*set)(struct sock *sk, int optval, void __user *user, unsigned int len);
  108. #ifdef CONFIG_COMPAT
  109. int (*compat_set)(struct sock *sk, int optval,
  110. void __user *user, unsigned int len);
  111. #endif
  112. int get_optmin;
  113. int get_optmax;
  114. int (*get)(struct sock *sk, int optval, void __user *user, int *len);
  115. #ifdef CONFIG_COMPAT
  116. int (*compat_get)(struct sock *sk, int optval,
  117. void __user *user, int *len);
  118. #endif
  119. /* Use the module struct to lock set/get code in place */
  120. struct module *owner;
  121. };
  122. /* Function to register/unregister hook points. */
  123. int nf_register_hook(struct nf_hook_ops *reg);
  124. void nf_unregister_hook(struct nf_hook_ops *reg);
  125. int nf_register_hooks(struct nf_hook_ops *reg, unsigned int n);
  126. void nf_unregister_hooks(struct nf_hook_ops *reg, unsigned int n);
  127. /* Functions to register get/setsockopt ranges (non-inclusive). You
  128. need to check permissions yourself! */
  129. int nf_register_sockopt(struct nf_sockopt_ops *reg);
  130. void nf_unregister_sockopt(struct nf_sockopt_ops *reg);
  131. #ifdef CONFIG_SYSCTL
  132. /* Sysctl registration */
  133. extern struct ctl_path nf_net_netfilter_sysctl_path[];
  134. extern struct ctl_path nf_net_ipv4_netfilter_sysctl_path[];
  135. #endif /* CONFIG_SYSCTL */
  136. extern struct list_head nf_hooks[NFPROTO_NUMPROTO][NF_MAX_HOOKS];
  137. #if defined(CONFIG_JUMP_LABEL)
  138. #include <linux/static_key.h>
  139. extern struct static_key nf_hooks_needed[NFPROTO_NUMPROTO][NF_MAX_HOOKS];
  140. static inline bool nf_hooks_active(u_int8_t pf, unsigned int hook)
  141. {
  142. if (__builtin_constant_p(pf) &&
  143. __builtin_constant_p(hook))
  144. return static_key_false(&nf_hooks_needed[pf][hook]);
  145. return !list_empty(&nf_hooks[pf][hook]);
  146. }
  147. #else
  148. static inline bool nf_hooks_active(u_int8_t pf, unsigned int hook)
  149. {
  150. return !list_empty(&nf_hooks[pf][hook]);
  151. }
  152. #endif
  153. int nf_hook_slow(u_int8_t pf, unsigned int hook, struct sk_buff *skb,
  154. struct net_device *indev, struct net_device *outdev,
  155. int (*okfn)(struct sk_buff *), int thresh);
  156. /**
  157. * nf_hook_thresh - call a netfilter hook
  158. *
  159. * Returns 1 if the hook has allowed the packet to pass. The function
  160. * okfn must be invoked by the caller in this case. Any other return
  161. * value indicates the packet has been consumed by the hook.
  162. */
  163. static inline int nf_hook_thresh(u_int8_t pf, unsigned int hook,
  164. struct sk_buff *skb,
  165. struct net_device *indev,
  166. struct net_device *outdev,
  167. int (*okfn)(struct sk_buff *), int thresh)
  168. {
  169. if (nf_hooks_active(pf, hook))
  170. return nf_hook_slow(pf, hook, skb, indev, outdev, okfn, thresh);
  171. return 1;
  172. }
  173. static inline int nf_hook(u_int8_t pf, unsigned int hook, struct sk_buff *skb,
  174. struct net_device *indev, struct net_device *outdev,
  175. int (*okfn)(struct sk_buff *))
  176. {
  177. return nf_hook_thresh(pf, hook, skb, indev, outdev, okfn, INT_MIN);
  178. }
  179. /* Activate hook; either okfn or kfree_skb called, unless a hook
  180. returns NF_STOLEN (in which case, it's up to the hook to deal with
  181. the consequences).
  182. Returns -ERRNO if packet dropped. Zero means queued, stolen or
  183. accepted.
  184. */
  185. /* RR:
  186. > I don't want nf_hook to return anything because people might forget
  187. > about async and trust the return value to mean "packet was ok".
  188. AK:
  189. Just document it clearly, then you can expect some sense from kernel
  190. coders :)
  191. */
  192. static inline int
  193. NF_HOOK_THRESH(uint8_t pf, unsigned int hook, struct sk_buff *skb,
  194. struct net_device *in, struct net_device *out,
  195. int (*okfn)(struct sk_buff *), int thresh)
  196. {
  197. int ret = nf_hook_thresh(pf, hook, skb, in, out, okfn, thresh);
  198. if (ret == 1)
  199. ret = okfn(skb);
  200. return ret;
  201. }
  202. static inline int
  203. NF_HOOK_COND(uint8_t pf, unsigned int hook, struct sk_buff *skb,
  204. struct net_device *in, struct net_device *out,
  205. int (*okfn)(struct sk_buff *), bool cond)
  206. {
  207. int ret;
  208. if (!cond ||
  209. ((ret = nf_hook_thresh(pf, hook, skb, in, out, okfn, INT_MIN)) == 1))
  210. ret = okfn(skb);
  211. return ret;
  212. }
  213. static inline int
  214. NF_HOOK(uint8_t pf, unsigned int hook, struct sk_buff *skb,
  215. struct net_device *in, struct net_device *out,
  216. int (*okfn)(struct sk_buff *))
  217. {
  218. return NF_HOOK_THRESH(pf, hook, skb, in, out, okfn, INT_MIN);
  219. }
  220. /* Call setsockopt() */
  221. int nf_setsockopt(struct sock *sk, u_int8_t pf, int optval, char __user *opt,
  222. unsigned int len);
  223. int nf_getsockopt(struct sock *sk, u_int8_t pf, int optval, char __user *opt,
  224. int *len);
  225. #ifdef CONFIG_COMPAT
  226. int compat_nf_setsockopt(struct sock *sk, u_int8_t pf, int optval,
  227. char __user *opt, unsigned int len);
  228. int compat_nf_getsockopt(struct sock *sk, u_int8_t pf, int optval,
  229. char __user *opt, int *len);
  230. #endif
  231. /* Call this before modifying an existing packet: ensures it is
  232. modifiable and linear to the point you care about (writable_len).
  233. Returns true or false. */
  234. extern int skb_make_writable(struct sk_buff *skb, unsigned int writable_len);
  235. struct flowi;
  236. struct nf_queue_entry;
  237. struct nf_afinfo {
  238. unsigned short family;
  239. __sum16 (*checksum)(struct sk_buff *skb, unsigned int hook,
  240. unsigned int dataoff, u_int8_t protocol);
  241. __sum16 (*checksum_partial)(struct sk_buff *skb,
  242. unsigned int hook,
  243. unsigned int dataoff,
  244. unsigned int len,
  245. u_int8_t protocol);
  246. int (*route)(struct net *net, struct dst_entry **dst,
  247. struct flowi *fl, bool strict);
  248. void (*saveroute)(const struct sk_buff *skb,
  249. struct nf_queue_entry *entry);
  250. int (*reroute)(struct sk_buff *skb,
  251. const struct nf_queue_entry *entry);
  252. int route_key_size;
  253. };
  254. extern const struct nf_afinfo __rcu *nf_afinfo[NFPROTO_NUMPROTO];
  255. static inline const struct nf_afinfo *nf_get_afinfo(unsigned short family)
  256. {
  257. return rcu_dereference(nf_afinfo[family]);
  258. }
  259. static inline __sum16
  260. nf_checksum(struct sk_buff *skb, unsigned int hook, unsigned int dataoff,
  261. u_int8_t protocol, unsigned short family)
  262. {
  263. const struct nf_afinfo *afinfo;
  264. __sum16 csum = 0;
  265. rcu_read_lock();
  266. afinfo = nf_get_afinfo(family);
  267. if (afinfo)
  268. csum = afinfo->checksum(skb, hook, dataoff, protocol);
  269. rcu_read_unlock();
  270. return csum;
  271. }
  272. static inline __sum16
  273. nf_checksum_partial(struct sk_buff *skb, unsigned int hook,
  274. unsigned int dataoff, unsigned int len,
  275. u_int8_t protocol, unsigned short family)
  276. {
  277. const struct nf_afinfo *afinfo;
  278. __sum16 csum = 0;
  279. rcu_read_lock();
  280. afinfo = nf_get_afinfo(family);
  281. if (afinfo)
  282. csum = afinfo->checksum_partial(skb, hook, dataoff, len,
  283. protocol);
  284. rcu_read_unlock();
  285. return csum;
  286. }
  287. extern int nf_register_afinfo(const struct nf_afinfo *afinfo);
  288. extern void nf_unregister_afinfo(const struct nf_afinfo *afinfo);
  289. #include <net/flow.h>
  290. extern void (*ip_nat_decode_session)(struct sk_buff *, struct flowi *);
  291. static inline void
  292. nf_nat_decode_session(struct sk_buff *skb, struct flowi *fl, u_int8_t family)
  293. {
  294. #ifdef CONFIG_NF_NAT_NEEDED
  295. void (*decodefn)(struct sk_buff *, struct flowi *);
  296. if (family == AF_INET) {
  297. rcu_read_lock();
  298. decodefn = rcu_dereference(ip_nat_decode_session);
  299. if (decodefn)
  300. decodefn(skb, fl);
  301. rcu_read_unlock();
  302. }
  303. #endif
  304. }
  305. #ifdef CONFIG_PROC_FS
  306. #include <linux/proc_fs.h>
  307. extern struct proc_dir_entry *proc_net_netfilter;
  308. #endif
  309. #else /* !CONFIG_NETFILTER */
  310. #define NF_HOOK(pf, hook, skb, indev, outdev, okfn) (okfn)(skb)
  311. #define NF_HOOK_COND(pf, hook, skb, indev, outdev, okfn, cond) (okfn)(skb)
  312. static inline int nf_hook_thresh(u_int8_t pf, unsigned int hook,
  313. struct sk_buff *skb,
  314. struct net_device *indev,
  315. struct net_device *outdev,
  316. int (*okfn)(struct sk_buff *), int thresh)
  317. {
  318. return okfn(skb);
  319. }
  320. static inline int nf_hook(u_int8_t pf, unsigned int hook, struct sk_buff *skb,
  321. struct net_device *indev, struct net_device *outdev,
  322. int (*okfn)(struct sk_buff *))
  323. {
  324. return 1;
  325. }
  326. struct flowi;
  327. static inline void
  328. nf_nat_decode_session(struct sk_buff *skb, struct flowi *fl, u_int8_t family)
  329. {
  330. }
  331. #endif /*CONFIG_NETFILTER*/
  332. #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
  333. extern void (*ip_ct_attach)(struct sk_buff *, struct sk_buff *) __rcu;
  334. extern void nf_ct_attach(struct sk_buff *, struct sk_buff *);
  335. extern void (*nf_ct_destroy)(struct nf_conntrack *) __rcu;
  336. #else
  337. static inline void nf_ct_attach(struct sk_buff *new, struct sk_buff *skb) {}
  338. #endif
  339. #endif /*__KERNEL__*/
  340. #endif /*__LINUX_NETFILTER_H*/