netfilter.h 11 KB

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