netfilter.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. #ifndef __LINUX_NETFILTER_H
  2. #define __LINUX_NETFILTER_H
  3. #ifdef __KERNEL__
  4. #include <linux/init.h>
  5. #include <linux/types.h>
  6. #include <linux/skbuff.h>
  7. #include <linux/net.h>
  8. #include <linux/if.h>
  9. #include <linux/wait.h>
  10. #include <linux/list.h>
  11. #endif
  12. #include <linux/compiler.h>
  13. /* Responses from hook functions. */
  14. #define NF_DROP 0
  15. #define NF_ACCEPT 1
  16. #define NF_STOLEN 2
  17. #define NF_QUEUE 3
  18. #define NF_REPEAT 4
  19. #define NF_STOP 5
  20. #define NF_MAX_VERDICT NF_STOP
  21. /* we overload the higher bits for encoding auxiliary data such as the queue
  22. * number. Not nice, but better than additional function arguments. */
  23. #define NF_VERDICT_MASK 0x0000ffff
  24. #define NF_VERDICT_BITS 16
  25. #define NF_VERDICT_QMASK 0xffff0000
  26. #define NF_VERDICT_QBITS 16
  27. #define NF_QUEUE_NR(x) (((x << NF_VERDICT_QBITS) & NF_VERDICT_QMASK) | NF_QUEUE)
  28. /* only for userspace compatibility */
  29. #ifndef __KERNEL__
  30. /* Generic cache responses from hook functions.
  31. <= 0x2000 is used for protocol-flags. */
  32. #define NFC_UNKNOWN 0x4000
  33. #define NFC_ALTERED 0x8000
  34. #endif
  35. #ifdef __KERNEL__
  36. #ifdef CONFIG_NETFILTER
  37. extern void netfilter_init(void);
  38. /* Largest hook number + 1 */
  39. #define NF_MAX_HOOKS 8
  40. struct sk_buff;
  41. struct net_device;
  42. typedef unsigned int nf_hookfn(unsigned int hooknum,
  43. struct sk_buff **skb,
  44. const struct net_device *in,
  45. const struct net_device *out,
  46. int (*okfn)(struct sk_buff *));
  47. struct nf_hook_ops
  48. {
  49. struct list_head list;
  50. /* User fills in from here down. */
  51. nf_hookfn *hook;
  52. struct module *owner;
  53. int pf;
  54. int hooknum;
  55. /* Hooks are ordered in ascending priority. */
  56. int priority;
  57. };
  58. struct nf_sockopt_ops
  59. {
  60. struct list_head list;
  61. int pf;
  62. /* Non-inclusive ranges: use 0/0/NULL to never get called. */
  63. int set_optmin;
  64. int set_optmax;
  65. int (*set)(struct sock *sk, int optval, void __user *user, unsigned int len);
  66. int (*compat_set)(struct sock *sk, int optval,
  67. void __user *user, unsigned int len);
  68. int get_optmin;
  69. int get_optmax;
  70. int (*get)(struct sock *sk, int optval, void __user *user, int *len);
  71. int (*compat_get)(struct sock *sk, int optval,
  72. void __user *user, int *len);
  73. /* Use the module struct to lock set/get code in place */
  74. struct module *owner;
  75. };
  76. /* Each queued (to userspace) skbuff has one of these. */
  77. struct nf_info
  78. {
  79. /* The ops struct which sent us to userspace. */
  80. struct nf_hook_ops *elem;
  81. /* If we're sent to userspace, this keeps housekeeping info */
  82. int pf;
  83. unsigned int hook;
  84. struct net_device *indev, *outdev;
  85. int (*okfn)(struct sk_buff *);
  86. };
  87. /* Function to register/unregister hook points. */
  88. int nf_register_hook(struct nf_hook_ops *reg);
  89. void nf_unregister_hook(struct nf_hook_ops *reg);
  90. int nf_register_hooks(struct nf_hook_ops *reg, unsigned int n);
  91. void nf_unregister_hooks(struct nf_hook_ops *reg, unsigned int n);
  92. /* Functions to register get/setsockopt ranges (non-inclusive). You
  93. need to check permissions yourself! */
  94. int nf_register_sockopt(struct nf_sockopt_ops *reg);
  95. void nf_unregister_sockopt(struct nf_sockopt_ops *reg);
  96. #ifdef CONFIG_SYSCTL
  97. /* Sysctl registration */
  98. struct ctl_table_header *nf_register_sysctl_table(struct ctl_table *path,
  99. struct ctl_table *table);
  100. void nf_unregister_sysctl_table(struct ctl_table_header *header,
  101. struct ctl_table *table);
  102. extern struct ctl_table nf_net_netfilter_sysctl_path[];
  103. extern struct ctl_table nf_net_ipv4_netfilter_sysctl_path[];
  104. #endif /* CONFIG_SYSCTL */
  105. extern struct list_head nf_hooks[NPROTO][NF_MAX_HOOKS];
  106. /* those NF_LOG_* defines and struct nf_loginfo are legacy definitios that will
  107. * disappear once iptables is replaced with pkttables. Please DO NOT use them
  108. * for any new code! */
  109. #define NF_LOG_TCPSEQ 0x01 /* Log TCP sequence numbers */
  110. #define NF_LOG_TCPOPT 0x02 /* Log TCP options */
  111. #define NF_LOG_IPOPT 0x04 /* Log IP options */
  112. #define NF_LOG_UID 0x08 /* Log UID owning local socket */
  113. #define NF_LOG_MASK 0x0f
  114. #define NF_LOG_TYPE_LOG 0x01
  115. #define NF_LOG_TYPE_ULOG 0x02
  116. struct nf_loginfo {
  117. u_int8_t type;
  118. union {
  119. struct {
  120. u_int32_t copy_len;
  121. u_int16_t group;
  122. u_int16_t qthreshold;
  123. } ulog;
  124. struct {
  125. u_int8_t level;
  126. u_int8_t logflags;
  127. } log;
  128. } u;
  129. };
  130. typedef void nf_logfn(unsigned int pf,
  131. unsigned int hooknum,
  132. const struct sk_buff *skb,
  133. const struct net_device *in,
  134. const struct net_device *out,
  135. const struct nf_loginfo *li,
  136. const char *prefix);
  137. struct nf_logger {
  138. struct module *me;
  139. nf_logfn *logfn;
  140. char *name;
  141. };
  142. /* Function to register/unregister log function. */
  143. int nf_log_register(int pf, struct nf_logger *logger);
  144. void nf_log_unregister(struct nf_logger *logger);
  145. void nf_log_unregister_pf(int pf);
  146. /* Calls the registered backend logging function */
  147. void nf_log_packet(int pf,
  148. unsigned int hooknum,
  149. const struct sk_buff *skb,
  150. const struct net_device *in,
  151. const struct net_device *out,
  152. struct nf_loginfo *li,
  153. const char *fmt, ...);
  154. int nf_hook_slow(int pf, unsigned int hook, struct sk_buff **pskb,
  155. struct net_device *indev, struct net_device *outdev,
  156. int (*okfn)(struct sk_buff *), int thresh);
  157. /**
  158. * nf_hook_thresh - call a netfilter hook
  159. *
  160. * Returns 1 if the hook has allowed the packet to pass. The function
  161. * okfn must be invoked by the caller in this case. Any other return
  162. * value indicates the packet has been consumed by the hook.
  163. */
  164. static inline int nf_hook_thresh(int pf, unsigned int hook,
  165. struct sk_buff **pskb,
  166. struct net_device *indev,
  167. struct net_device *outdev,
  168. int (*okfn)(struct sk_buff *), int thresh,
  169. int cond)
  170. {
  171. if (!cond)
  172. return 1;
  173. #ifndef CONFIG_NETFILTER_DEBUG
  174. if (list_empty(&nf_hooks[pf][hook]))
  175. return 1;
  176. #endif
  177. return nf_hook_slow(pf, hook, pskb, indev, outdev, okfn, thresh);
  178. }
  179. static inline int nf_hook(int pf, unsigned int hook, struct sk_buff **pskb,
  180. struct net_device *indev, struct net_device *outdev,
  181. int (*okfn)(struct sk_buff *))
  182. {
  183. return nf_hook_thresh(pf, hook, pskb, indev, outdev, okfn, INT_MIN, 1);
  184. }
  185. /* Activate hook; either okfn or kfree_skb called, unless a hook
  186. returns NF_STOLEN (in which case, it's up to the hook to deal with
  187. the consequences).
  188. Returns -ERRNO if packet dropped. Zero means queued, stolen or
  189. accepted.
  190. */
  191. /* RR:
  192. > I don't want nf_hook to return anything because people might forget
  193. > about async and trust the return value to mean "packet was ok".
  194. AK:
  195. Just document it clearly, then you can expect some sense from kernel
  196. coders :)
  197. */
  198. /* This is gross, but inline doesn't cut it for avoiding the function
  199. call in fast path: gcc doesn't inline (needs value tracking?). --RR */
  200. /* HX: It's slightly less gross now. */
  201. #define NF_HOOK_THRESH(pf, hook, skb, indev, outdev, okfn, thresh) \
  202. ({int __ret; \
  203. if ((__ret=nf_hook_thresh(pf, hook, &(skb), indev, outdev, okfn, thresh, 1)) == 1)\
  204. __ret = (okfn)(skb); \
  205. __ret;})
  206. #define NF_HOOK_COND(pf, hook, skb, indev, outdev, okfn, cond) \
  207. ({int __ret; \
  208. if ((__ret=nf_hook_thresh(pf, hook, &(skb), indev, outdev, okfn, INT_MIN, cond)) == 1)\
  209. __ret = (okfn)(skb); \
  210. __ret;})
  211. #define NF_HOOK(pf, hook, skb, indev, outdev, okfn) \
  212. NF_HOOK_THRESH(pf, hook, skb, indev, outdev, okfn, INT_MIN)
  213. /* Call setsockopt() */
  214. int nf_setsockopt(struct sock *sk, int pf, int optval, char __user *opt,
  215. int len);
  216. int nf_getsockopt(struct sock *sk, int pf, int optval, char __user *opt,
  217. int *len);
  218. int compat_nf_setsockopt(struct sock *sk, int pf, int optval,
  219. char __user *opt, int len);
  220. int compat_nf_getsockopt(struct sock *sk, int pf, int optval,
  221. char __user *opt, int *len);
  222. /* Packet queuing */
  223. struct nf_queue_handler {
  224. int (*outfn)(struct sk_buff *skb, struct nf_info *info,
  225. unsigned int queuenum, void *data);
  226. void *data;
  227. char *name;
  228. };
  229. extern int nf_register_queue_handler(int pf,
  230. struct nf_queue_handler *qh);
  231. extern int nf_unregister_queue_handler(int pf,
  232. struct nf_queue_handler *qh);
  233. extern void nf_unregister_queue_handlers(struct nf_queue_handler *qh);
  234. extern void nf_reinject(struct sk_buff *skb,
  235. struct nf_info *info,
  236. unsigned int verdict);
  237. /* FIXME: Before cache is ever used, this must be implemented for real. */
  238. extern void nf_invalidate_cache(int pf);
  239. /* Call this before modifying an existing packet: ensures it is
  240. modifiable and linear to the point you care about (writable_len).
  241. Returns true or false. */
  242. extern int skb_make_writable(struct sk_buff **pskb, unsigned int writable_len);
  243. static inline void nf_csum_replace4(__sum16 *sum, __be32 from, __be32 to)
  244. {
  245. __be32 diff[] = { ~from, to };
  246. *sum = csum_fold(csum_partial((char *)diff, sizeof(diff), ~csum_unfold(*sum)));
  247. }
  248. static inline void nf_csum_replace2(__sum16 *sum, __be16 from, __be16 to)
  249. {
  250. nf_csum_replace4(sum, (__force __be32)from, (__force __be32)to);
  251. }
  252. extern void nf_proto_csum_replace4(__sum16 *sum, struct sk_buff *skb,
  253. __be32 from, __be32 to, int pseudohdr);
  254. static inline void nf_proto_csum_replace2(__sum16 *sum, struct sk_buff *skb,
  255. __be16 from, __be16 to, int pseudohdr)
  256. {
  257. nf_proto_csum_replace4(sum, skb, (__force __be32)from,
  258. (__force __be32)to, pseudohdr);
  259. }
  260. struct nf_afinfo {
  261. unsigned short family;
  262. __sum16 (*checksum)(struct sk_buff *skb, unsigned int hook,
  263. unsigned int dataoff, u_int8_t protocol);
  264. void (*saveroute)(const struct sk_buff *skb,
  265. struct nf_info *info);
  266. int (*reroute)(struct sk_buff **skb,
  267. const struct nf_info *info);
  268. int route_key_size;
  269. };
  270. extern struct nf_afinfo *nf_afinfo[];
  271. static inline struct nf_afinfo *nf_get_afinfo(unsigned short family)
  272. {
  273. return rcu_dereference(nf_afinfo[family]);
  274. }
  275. static inline __sum16
  276. nf_checksum(struct sk_buff *skb, unsigned int hook, unsigned int dataoff,
  277. u_int8_t protocol, unsigned short family)
  278. {
  279. struct nf_afinfo *afinfo;
  280. __sum16 csum = 0;
  281. rcu_read_lock();
  282. afinfo = nf_get_afinfo(family);
  283. if (afinfo)
  284. csum = afinfo->checksum(skb, hook, dataoff, protocol);
  285. rcu_read_unlock();
  286. return csum;
  287. }
  288. extern int nf_register_afinfo(struct nf_afinfo *afinfo);
  289. extern void nf_unregister_afinfo(struct nf_afinfo *afinfo);
  290. #define nf_info_reroute(x) ((void *)x + sizeof(struct nf_info))
  291. #include <net/flow.h>
  292. extern void (*ip_nat_decode_session)(struct sk_buff *, struct flowi *);
  293. static inline void
  294. nf_nat_decode_session(struct sk_buff *skb, struct flowi *fl, int family)
  295. {
  296. #if defined(CONFIG_IP_NF_NAT_NEEDED) || defined(CONFIG_NF_NAT_NEEDED)
  297. void (*decodefn)(struct sk_buff *, struct flowi *);
  298. if (family == AF_INET && (decodefn = ip_nat_decode_session) != NULL)
  299. decodefn(skb, fl);
  300. #endif
  301. }
  302. #ifdef CONFIG_PROC_FS
  303. #include <linux/proc_fs.h>
  304. extern struct proc_dir_entry *proc_net_netfilter;
  305. #endif
  306. #else /* !CONFIG_NETFILTER */
  307. #define NF_HOOK(pf, hook, skb, indev, outdev, okfn) (okfn)(skb)
  308. #define NF_HOOK_COND(pf, hook, skb, indev, outdev, okfn, cond) (okfn)(skb)
  309. static inline int nf_hook_thresh(int pf, unsigned int hook,
  310. struct sk_buff **pskb,
  311. struct net_device *indev,
  312. struct net_device *outdev,
  313. int (*okfn)(struct sk_buff *), int thresh,
  314. int cond)
  315. {
  316. return okfn(*pskb);
  317. }
  318. static inline int nf_hook(int pf, unsigned int hook, struct sk_buff **pskb,
  319. struct net_device *indev, struct net_device *outdev,
  320. int (*okfn)(struct sk_buff *))
  321. {
  322. return 1;
  323. }
  324. struct flowi;
  325. static inline void
  326. nf_nat_decode_session(struct sk_buff *skb, struct flowi *fl, int family) {}
  327. #endif /*CONFIG_NETFILTER*/
  328. #if defined(CONFIG_NF_CONNTRACK) || defined(CONFIG_NF_CONNTRACK_MODULE)
  329. extern void (*ip_ct_attach)(struct sk_buff *, struct sk_buff *);
  330. extern void nf_ct_attach(struct sk_buff *, struct sk_buff *);
  331. extern void (*nf_ct_destroy)(struct nf_conntrack *);
  332. #else
  333. static inline void nf_ct_attach(struct sk_buff *new, struct sk_buff *skb) {}
  334. #endif
  335. #endif /*__KERNEL__*/
  336. #endif /*__LINUX_NETFILTER_H*/