netfilter.h 11 KB

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