pf_if.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813
  1. /* $OpenBSD: pf_if.c,v 1.79 2015/07/21 02:32:04 sashan Exp $ */
  2. /*
  3. * Copyright 2005 Henning Brauer <henning@openbsd.org>
  4. * Copyright 2005 Ryan McBride <mcbride@openbsd.org>
  5. * Copyright (c) 2001 Daniel Hartmeier
  6. * Copyright (c) 2003 Cedric Berger
  7. * All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. *
  13. * - Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. * - Redistributions in binary form must reproduce the above
  16. * copyright notice, this list of conditions and the following
  17. * disclaimer in the documentation and/or other materials provided
  18. * with the distribution.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  23. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  24. * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  25. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  26. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  27. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  28. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  30. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  31. * POSSIBILITY OF SUCH DAMAGE.
  32. */
  33. #include <sys/param.h>
  34. #include <sys/systm.h>
  35. #include <sys/mbuf.h>
  36. #include <sys/filio.h>
  37. #include <sys/socket.h>
  38. #include <sys/socketvar.h>
  39. #include <sys/kernel.h>
  40. #include <sys/device.h>
  41. #include <sys/time.h>
  42. #include <sys/pool.h>
  43. #include <sys/syslog.h>
  44. #include <net/if.h>
  45. #include <net/if_var.h>
  46. #include <netinet/in.h>
  47. #include <netinet/ip.h>
  48. #include <netinet/ip_var.h>
  49. #include <net/pfvar.h>
  50. #ifdef INET6
  51. #include <netinet/ip6.h>
  52. #endif /* INET6 */
  53. struct pfi_kif *pfi_all = NULL;
  54. struct pool pfi_addr_pl;
  55. struct pfi_ifhead pfi_ifs;
  56. long pfi_update = 1;
  57. struct pfr_addr *pfi_buffer;
  58. int pfi_buffer_cnt;
  59. int pfi_buffer_max;
  60. void pfi_kif_update(struct pfi_kif *);
  61. void pfi_dynaddr_update(struct pfi_dynaddr *dyn);
  62. void pfi_table_update(struct pfr_ktable *, struct pfi_kif *,
  63. u_int8_t, int);
  64. void pfi_kifaddr_update(void *);
  65. void pfi_instance_add(struct ifnet *, u_int8_t, int);
  66. void pfi_address_add(struct sockaddr *, sa_family_t, u_int8_t);
  67. int pfi_if_compare(struct pfi_kif *, struct pfi_kif *);
  68. int pfi_skip_if(const char *, struct pfi_kif *);
  69. int pfi_unmask(void *);
  70. RB_PROTOTYPE(pfi_ifhead, pfi_kif, pfik_tree, pfi_if_compare);
  71. RB_GENERATE(pfi_ifhead, pfi_kif, pfik_tree, pfi_if_compare);
  72. #define PFI_BUFFER_MAX 0x10000
  73. #define PFI_MTYPE M_IFADDR
  74. void
  75. pfi_initialize(void)
  76. {
  77. if (pfi_all != NULL) /* already initialized */
  78. return;
  79. pool_init(&pfi_addr_pl, sizeof(struct pfi_dynaddr), 0, 0, 0,
  80. "pfiaddrpl", &pool_allocator_nointr);
  81. pfi_buffer_max = 64;
  82. pfi_buffer = mallocarray(pfi_buffer_max, sizeof(*pfi_buffer),
  83. PFI_MTYPE, M_WAITOK);
  84. if ((pfi_all = pfi_kif_get(IFG_ALL)) == NULL)
  85. panic("pfi_kif_get for pfi_all failed");
  86. }
  87. struct pfi_kif *
  88. pfi_kif_get(const char *kif_name)
  89. {
  90. struct pfi_kif *kif;
  91. struct pfi_kif_cmp s;
  92. bzero(&s, sizeof(s));
  93. strlcpy(s.pfik_name, kif_name, sizeof(s.pfik_name));
  94. if ((kif = RB_FIND(pfi_ifhead, &pfi_ifs, (struct pfi_kif *)&s)) != NULL)
  95. return (kif);
  96. /* create new one */
  97. if ((kif = malloc(sizeof(*kif), PFI_MTYPE, M_NOWAIT|M_ZERO)) == NULL)
  98. return (NULL);
  99. strlcpy(kif->pfik_name, kif_name, sizeof(kif->pfik_name));
  100. kif->pfik_tzero = time_second;
  101. TAILQ_INIT(&kif->pfik_dynaddrs);
  102. if (!strcmp(kif->pfik_name, "any")) {
  103. /* both so it works in the ioctl and the regular case */
  104. kif->pfik_flags |= PFI_IFLAG_ANY;
  105. kif->pfik_flags_new |= PFI_IFLAG_ANY;
  106. }
  107. RB_INSERT(pfi_ifhead, &pfi_ifs, kif);
  108. return (kif);
  109. }
  110. void
  111. pfi_kif_ref(struct pfi_kif *kif, enum pfi_kif_refs what)
  112. {
  113. switch (what) {
  114. case PFI_KIF_REF_RULE:
  115. kif->pfik_rules++;
  116. break;
  117. case PFI_KIF_REF_STATE:
  118. kif->pfik_states++;
  119. break;
  120. case PFI_KIF_REF_ROUTE:
  121. kif->pfik_routes++;
  122. break;
  123. default:
  124. panic("pfi_kif_ref with unknown type");
  125. }
  126. }
  127. void
  128. pfi_kif_unref(struct pfi_kif *kif, enum pfi_kif_refs what)
  129. {
  130. if (kif == NULL)
  131. return;
  132. switch (what) {
  133. case PFI_KIF_REF_NONE:
  134. break;
  135. case PFI_KIF_REF_RULE:
  136. if (kif->pfik_rules <= 0) {
  137. DPFPRINTF(LOG_ERR,
  138. "pfi_kif_unref: rules refcount <= 0");
  139. return;
  140. }
  141. kif->pfik_rules--;
  142. break;
  143. case PFI_KIF_REF_STATE:
  144. if (kif->pfik_states <= 0) {
  145. DPFPRINTF(LOG_ERR,
  146. "pfi_kif_unref: state refcount <= 0");
  147. return;
  148. }
  149. kif->pfik_states--;
  150. break;
  151. case PFI_KIF_REF_ROUTE:
  152. if (kif->pfik_routes <= 0) {
  153. DPFPRINTF(LOG_ERR,
  154. "pfi_kif_unref: state refcount <= 0");
  155. return;
  156. }
  157. kif->pfik_routes--;
  158. break;
  159. default:
  160. panic("pfi_kif_unref with unknown type");
  161. }
  162. if (kif->pfik_ifp != NULL || kif->pfik_group != NULL || kif == pfi_all)
  163. return;
  164. if (kif->pfik_rules || kif->pfik_states || kif->pfik_routes)
  165. return;
  166. RB_REMOVE(pfi_ifhead, &pfi_ifs, kif);
  167. free(kif, PFI_MTYPE, 0);
  168. }
  169. int
  170. pfi_kif_match(struct pfi_kif *rule_kif, struct pfi_kif *packet_kif)
  171. {
  172. struct ifg_list *p;
  173. if (rule_kif == NULL || rule_kif == packet_kif)
  174. return (1);
  175. if (rule_kif->pfik_group != NULL)
  176. TAILQ_FOREACH(p, &packet_kif->pfik_ifp->if_groups, ifgl_next)
  177. if (p->ifgl_group == rule_kif->pfik_group)
  178. return (1);
  179. if (rule_kif->pfik_flags & PFI_IFLAG_ANY && packet_kif->pfik_ifp &&
  180. !(packet_kif->pfik_ifp->if_flags & IFF_LOOPBACK))
  181. return (1);
  182. return (0);
  183. }
  184. void
  185. pfi_attach_ifnet(struct ifnet *ifp)
  186. {
  187. struct pfi_kif *kif;
  188. int s;
  189. pfi_initialize();
  190. s = splsoftnet();
  191. pfi_update++;
  192. if ((kif = pfi_kif_get(ifp->if_xname)) == NULL)
  193. panic("pfi_kif_get failed");
  194. kif->pfik_ifp = ifp;
  195. ifp->if_pf_kif = (caddr_t)kif;
  196. if ((kif->pfik_ah_cookie = hook_establish(ifp->if_addrhooks, 1,
  197. pfi_kifaddr_update, kif)) == NULL)
  198. panic("pfi_attach_ifnet: cannot allocate '%s' address hook",
  199. ifp->if_xname);
  200. pfi_kif_update(kif);
  201. splx(s);
  202. }
  203. void
  204. pfi_detach_ifnet(struct ifnet *ifp)
  205. {
  206. int s;
  207. struct pfi_kif *kif;
  208. if ((kif = (struct pfi_kif *)ifp->if_pf_kif) == NULL)
  209. return;
  210. s = splsoftnet();
  211. pfi_update++;
  212. hook_disestablish(ifp->if_addrhooks, kif->pfik_ah_cookie);
  213. pfi_kif_update(kif);
  214. if (HFSC_ENABLED(&ifp->if_snd)) {
  215. pf_remove_queues(ifp);
  216. pf_free_queues(pf_queues_active, ifp);
  217. }
  218. kif->pfik_ifp = NULL;
  219. ifp->if_pf_kif = NULL;
  220. pfi_kif_unref(kif, PFI_KIF_REF_NONE);
  221. splx(s);
  222. }
  223. void
  224. pfi_attach_ifgroup(struct ifg_group *ifg)
  225. {
  226. struct pfi_kif *kif;
  227. int s;
  228. pfi_initialize();
  229. s = splsoftnet();
  230. pfi_update++;
  231. if ((kif = pfi_kif_get(ifg->ifg_group)) == NULL)
  232. panic("pfi_kif_get failed");
  233. kif->pfik_group = ifg;
  234. ifg->ifg_pf_kif = (caddr_t)kif;
  235. splx(s);
  236. }
  237. void
  238. pfi_detach_ifgroup(struct ifg_group *ifg)
  239. {
  240. int s;
  241. struct pfi_kif *kif;
  242. if ((kif = (struct pfi_kif *)ifg->ifg_pf_kif) == NULL)
  243. return;
  244. s = splsoftnet();
  245. pfi_update++;
  246. kif->pfik_group = NULL;
  247. ifg->ifg_pf_kif = NULL;
  248. pfi_kif_unref(kif, PFI_KIF_REF_NONE);
  249. splx(s);
  250. }
  251. void
  252. pfi_group_change(const char *group)
  253. {
  254. struct pfi_kif *kif;
  255. int s;
  256. s = splsoftnet();
  257. pfi_update++;
  258. if ((kif = pfi_kif_get(group)) == NULL)
  259. panic("pfi_kif_get failed");
  260. pfi_kif_update(kif);
  261. splx(s);
  262. }
  263. int
  264. pfi_match_addr(struct pfi_dynaddr *dyn, struct pf_addr *a, sa_family_t af)
  265. {
  266. switch (af) {
  267. case AF_INET:
  268. switch (dyn->pfid_acnt4) {
  269. case 0:
  270. return (0);
  271. case 1:
  272. return (PF_MATCHA(0, &dyn->pfid_addr4,
  273. &dyn->pfid_mask4, a, AF_INET));
  274. default:
  275. return (pfr_match_addr(dyn->pfid_kt, a, AF_INET));
  276. }
  277. break;
  278. #ifdef INET6
  279. case AF_INET6:
  280. switch (dyn->pfid_acnt6) {
  281. case 0:
  282. return (0);
  283. case 1:
  284. return (PF_MATCHA(0, &dyn->pfid_addr6,
  285. &dyn->pfid_mask6, a, AF_INET6));
  286. default:
  287. return (pfr_match_addr(dyn->pfid_kt, a, AF_INET6));
  288. }
  289. break;
  290. #endif /* INET6 */
  291. default:
  292. return (0);
  293. }
  294. }
  295. int
  296. pfi_dynaddr_setup(struct pf_addr_wrap *aw, sa_family_t af)
  297. {
  298. struct pfi_dynaddr *dyn;
  299. char tblname[PF_TABLE_NAME_SIZE];
  300. struct pf_ruleset *ruleset = NULL;
  301. int s, rv = 0;
  302. if (aw->type != PF_ADDR_DYNIFTL)
  303. return (0);
  304. if ((dyn = pool_get(&pfi_addr_pl, PR_WAITOK | PR_LIMITFAIL | PR_ZERO))
  305. == NULL)
  306. return (1);
  307. s = splsoftnet();
  308. if (!strcmp(aw->v.ifname, "self"))
  309. dyn->pfid_kif = pfi_kif_get(IFG_ALL);
  310. else
  311. dyn->pfid_kif = pfi_kif_get(aw->v.ifname);
  312. if (dyn->pfid_kif == NULL) {
  313. rv = 1;
  314. goto _bad;
  315. }
  316. pfi_kif_ref(dyn->pfid_kif, PFI_KIF_REF_RULE);
  317. dyn->pfid_net = pfi_unmask(&aw->v.a.mask);
  318. if (af == AF_INET && dyn->pfid_net == 32)
  319. dyn->pfid_net = 128;
  320. strlcpy(tblname, aw->v.ifname, sizeof(tblname));
  321. if (aw->iflags & PFI_AFLAG_NETWORK)
  322. strlcat(tblname, ":network", sizeof(tblname));
  323. if (aw->iflags & PFI_AFLAG_BROADCAST)
  324. strlcat(tblname, ":broadcast", sizeof(tblname));
  325. if (aw->iflags & PFI_AFLAG_PEER)
  326. strlcat(tblname, ":peer", sizeof(tblname));
  327. if (aw->iflags & PFI_AFLAG_NOALIAS)
  328. strlcat(tblname, ":0", sizeof(tblname));
  329. if (dyn->pfid_net != 128)
  330. snprintf(tblname + strlen(tblname),
  331. sizeof(tblname) - strlen(tblname), "/%d", dyn->pfid_net);
  332. if ((ruleset = pf_find_or_create_ruleset(PF_RESERVED_ANCHOR)) == NULL) {
  333. rv = 1;
  334. goto _bad;
  335. }
  336. if ((dyn->pfid_kt = pfr_attach_table(ruleset, tblname, 1)) == NULL) {
  337. rv = 1;
  338. goto _bad;
  339. }
  340. dyn->pfid_kt->pfrkt_flags |= PFR_TFLAG_ACTIVE;
  341. dyn->pfid_iflags = aw->iflags;
  342. dyn->pfid_af = af;
  343. TAILQ_INSERT_TAIL(&dyn->pfid_kif->pfik_dynaddrs, dyn, entry);
  344. aw->p.dyn = dyn;
  345. pfi_kif_update(dyn->pfid_kif);
  346. splx(s);
  347. return (0);
  348. _bad:
  349. if (dyn->pfid_kt != NULL)
  350. pfr_detach_table(dyn->pfid_kt);
  351. if (ruleset != NULL)
  352. pf_remove_if_empty_ruleset(ruleset);
  353. if (dyn->pfid_kif != NULL)
  354. pfi_kif_unref(dyn->pfid_kif, PFI_KIF_REF_RULE);
  355. pool_put(&pfi_addr_pl, dyn);
  356. splx(s);
  357. return (rv);
  358. }
  359. void
  360. pfi_kif_update(struct pfi_kif *kif)
  361. {
  362. struct ifg_list *ifgl;
  363. struct pfi_dynaddr *p;
  364. /* update all dynaddr */
  365. TAILQ_FOREACH(p, &kif->pfik_dynaddrs, entry)
  366. pfi_dynaddr_update(p);
  367. /* again for all groups kif is member of */
  368. if (kif->pfik_ifp != NULL)
  369. TAILQ_FOREACH(ifgl, &kif->pfik_ifp->if_groups, ifgl_next)
  370. pfi_kif_update((struct pfi_kif *)
  371. ifgl->ifgl_group->ifg_pf_kif);
  372. }
  373. void
  374. pfi_dynaddr_update(struct pfi_dynaddr *dyn)
  375. {
  376. struct pfi_kif *kif;
  377. struct pfr_ktable *kt;
  378. if (dyn == NULL || dyn->pfid_kif == NULL || dyn->pfid_kt == NULL)
  379. panic("pfi_dynaddr_update");
  380. kif = dyn->pfid_kif;
  381. kt = dyn->pfid_kt;
  382. if (kt->pfrkt_larg != pfi_update) {
  383. /* this table needs to be brought up-to-date */
  384. pfi_table_update(kt, kif, dyn->pfid_net, dyn->pfid_iflags);
  385. kt->pfrkt_larg = pfi_update;
  386. }
  387. pfr_dynaddr_update(kt, dyn);
  388. }
  389. void
  390. pfi_table_update(struct pfr_ktable *kt, struct pfi_kif *kif, u_int8_t net, int flags)
  391. {
  392. int e, size2 = 0;
  393. struct ifg_member *ifgm;
  394. pfi_buffer_cnt = 0;
  395. if (kif->pfik_ifp != NULL)
  396. pfi_instance_add(kif->pfik_ifp, net, flags);
  397. else if (kif->pfik_group != NULL)
  398. TAILQ_FOREACH(ifgm, &kif->pfik_group->ifg_members, ifgm_next)
  399. pfi_instance_add(ifgm->ifgm_ifp, net, flags);
  400. if ((e = pfr_set_addrs(&kt->pfrkt_t, pfi_buffer, pfi_buffer_cnt, &size2,
  401. NULL, NULL, NULL, 0, PFR_TFLAG_ALLMASK)))
  402. DPFPRINTF(LOG_ERR,
  403. "pfi_table_update: cannot set %d new addresses "
  404. "into table %s: %d", pfi_buffer_cnt, kt->pfrkt_name, e);
  405. }
  406. void
  407. pfi_instance_add(struct ifnet *ifp, u_int8_t net, int flags)
  408. {
  409. struct ifaddr *ifa;
  410. int got4 = 0, got6 = 0;
  411. int net2, af;
  412. if (ifp == NULL)
  413. return;
  414. TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
  415. if (ifa->ifa_addr == NULL)
  416. continue;
  417. af = ifa->ifa_addr->sa_family;
  418. if (af != AF_INET && af != AF_INET6)
  419. continue;
  420. if ((flags & PFI_AFLAG_BROADCAST) && af == AF_INET6)
  421. continue;
  422. if ((flags & PFI_AFLAG_BROADCAST) &&
  423. !(ifp->if_flags & IFF_BROADCAST))
  424. continue;
  425. if ((flags & PFI_AFLAG_PEER) &&
  426. !(ifp->if_flags & IFF_POINTOPOINT))
  427. continue;
  428. if ((flags & PFI_AFLAG_NETWORK) && af == AF_INET6 &&
  429. IN6_IS_ADDR_LINKLOCAL(
  430. &((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr))
  431. continue;
  432. if (flags & PFI_AFLAG_NOALIAS) {
  433. if (af == AF_INET && got4)
  434. continue;
  435. if (af == AF_INET6 && got6)
  436. continue;
  437. }
  438. if (af == AF_INET)
  439. got4 = 1;
  440. else if (af == AF_INET6)
  441. got6 = 1;
  442. net2 = net;
  443. if (net2 == 128 && (flags & PFI_AFLAG_NETWORK)) {
  444. if (af == AF_INET)
  445. net2 = pfi_unmask(&((struct sockaddr_in *)
  446. ifa->ifa_netmask)->sin_addr);
  447. else if (af == AF_INET6)
  448. net2 = pfi_unmask(&((struct sockaddr_in6 *)
  449. ifa->ifa_netmask)->sin6_addr);
  450. }
  451. if (af == AF_INET && net2 > 32)
  452. net2 = 32;
  453. if (flags & PFI_AFLAG_BROADCAST)
  454. pfi_address_add(ifa->ifa_broadaddr, af, net2);
  455. else if (flags & PFI_AFLAG_PEER)
  456. pfi_address_add(ifa->ifa_dstaddr, af, net2);
  457. else
  458. pfi_address_add(ifa->ifa_addr, af, net2);
  459. }
  460. }
  461. void
  462. pfi_address_add(struct sockaddr *sa, sa_family_t af, u_int8_t net)
  463. {
  464. struct pfr_addr *p;
  465. int i;
  466. if (pfi_buffer_cnt >= pfi_buffer_max) {
  467. int new_max = pfi_buffer_max * 2;
  468. if (new_max > PFI_BUFFER_MAX) {
  469. DPFPRINTF(LOG_ERR,
  470. "pfi_address_add: address buffer full (%d/%d)",
  471. pfi_buffer_cnt, PFI_BUFFER_MAX);
  472. return;
  473. }
  474. p = mallocarray(new_max, sizeof(*pfi_buffer), PFI_MTYPE,
  475. M_DONTWAIT);
  476. if (p == NULL) {
  477. DPFPRINTF(LOG_ERR,
  478. "pfi_address_add: no memory to grow buffer "
  479. "(%d/%d)", pfi_buffer_cnt, PFI_BUFFER_MAX);
  480. return;
  481. }
  482. memcpy(p, pfi_buffer, pfi_buffer_max * sizeof(*pfi_buffer));
  483. /* no need to zero buffer */
  484. free(pfi_buffer, PFI_MTYPE, 0);
  485. pfi_buffer = p;
  486. pfi_buffer_max = new_max;
  487. }
  488. if (af == AF_INET && net > 32)
  489. net = 128;
  490. p = pfi_buffer + pfi_buffer_cnt++;
  491. bzero(p, sizeof(*p));
  492. p->pfra_af = af;
  493. p->pfra_net = net;
  494. if (af == AF_INET)
  495. p->pfra_ip4addr = ((struct sockaddr_in *)sa)->sin_addr;
  496. else if (af == AF_INET6) {
  497. p->pfra_ip6addr = ((struct sockaddr_in6 *)sa)->sin6_addr;
  498. if (IN6_IS_SCOPE_EMBED(&p->pfra_ip6addr))
  499. p->pfra_ip6addr.s6_addr16[1] = 0;
  500. }
  501. /* mask network address bits */
  502. if (net < 128)
  503. ((caddr_t)p)[p->pfra_net/8] &= ~(0xFF >> (p->pfra_net%8));
  504. for (i = (p->pfra_net+7)/8; i < sizeof(p->pfra_u); i++)
  505. ((caddr_t)p)[i] = 0;
  506. }
  507. void
  508. pfi_dynaddr_remove(struct pf_addr_wrap *aw)
  509. {
  510. int s;
  511. if (aw->type != PF_ADDR_DYNIFTL || aw->p.dyn == NULL ||
  512. aw->p.dyn->pfid_kif == NULL || aw->p.dyn->pfid_kt == NULL)
  513. return;
  514. s = splsoftnet();
  515. TAILQ_REMOVE(&aw->p.dyn->pfid_kif->pfik_dynaddrs, aw->p.dyn, entry);
  516. pfi_kif_unref(aw->p.dyn->pfid_kif, PFI_KIF_REF_RULE);
  517. aw->p.dyn->pfid_kif = NULL;
  518. pfr_detach_table(aw->p.dyn->pfid_kt);
  519. aw->p.dyn->pfid_kt = NULL;
  520. pool_put(&pfi_addr_pl, aw->p.dyn);
  521. aw->p.dyn = NULL;
  522. splx(s);
  523. }
  524. void
  525. pfi_dynaddr_copyout(struct pf_addr_wrap *aw)
  526. {
  527. if (aw->type != PF_ADDR_DYNIFTL || aw->p.dyn == NULL ||
  528. aw->p.dyn->pfid_kif == NULL)
  529. return;
  530. aw->p.dyncnt = aw->p.dyn->pfid_acnt4 + aw->p.dyn->pfid_acnt6;
  531. }
  532. void
  533. pfi_kifaddr_update(void *v)
  534. {
  535. int s;
  536. struct pfi_kif *kif = (struct pfi_kif *)v;
  537. s = splsoftnet();
  538. pfi_update++;
  539. pfi_kif_update(kif);
  540. splx(s);
  541. }
  542. int
  543. pfi_if_compare(struct pfi_kif *p, struct pfi_kif *q)
  544. {
  545. return (strncmp(p->pfik_name, q->pfik_name, IFNAMSIZ));
  546. }
  547. void
  548. pfi_update_status(const char *name, struct pf_status *pfs)
  549. {
  550. struct pfi_kif *p;
  551. struct pfi_kif_cmp key;
  552. struct ifg_member p_member, *ifgm;
  553. TAILQ_HEAD(, ifg_member) ifg_members;
  554. int i, j, k, s;
  555. s = splsoftnet();
  556. if (*name == '\0' && pfs == NULL) {
  557. RB_FOREACH(p, pfi_ifhead, &pfi_ifs) {
  558. bzero(p->pfik_packets, sizeof(p->pfik_packets));
  559. bzero(p->pfik_bytes, sizeof(p->pfik_bytes));
  560. p->pfik_tzero = time_second;
  561. }
  562. splx(s);
  563. return;
  564. }
  565. strlcpy(key.pfik_name, name, sizeof(key.pfik_name));
  566. p = RB_FIND(pfi_ifhead, &pfi_ifs, (struct pfi_kif *)&key);
  567. if (p == NULL) {
  568. splx(s);
  569. return;
  570. }
  571. if (p->pfik_group != NULL) {
  572. memcpy(&ifg_members, &p->pfik_group->ifg_members,
  573. sizeof(ifg_members));
  574. } else {
  575. /* build a temporary list for p only */
  576. bzero(&p_member, sizeof(p_member));
  577. p_member.ifgm_ifp = p->pfik_ifp;
  578. TAILQ_INIT(&ifg_members);
  579. TAILQ_INSERT_TAIL(&ifg_members, &p_member, ifgm_next);
  580. }
  581. if (pfs) {
  582. bzero(pfs->pcounters, sizeof(pfs->pcounters));
  583. bzero(pfs->bcounters, sizeof(pfs->bcounters));
  584. }
  585. TAILQ_FOREACH(ifgm, &ifg_members, ifgm_next) {
  586. if (ifgm->ifgm_ifp == NULL)
  587. continue;
  588. p = (struct pfi_kif *)ifgm->ifgm_ifp->if_pf_kif;
  589. /* just clear statistics */
  590. if (pfs == NULL) {
  591. bzero(p->pfik_packets, sizeof(p->pfik_packets));
  592. bzero(p->pfik_bytes, sizeof(p->pfik_bytes));
  593. p->pfik_tzero = time_second;
  594. continue;
  595. }
  596. for (i = 0; i < 2; i++)
  597. for (j = 0; j < 2; j++)
  598. for (k = 0; k < 2; k++) {
  599. pfs->pcounters[i][j][k] +=
  600. p->pfik_packets[i][j][k];
  601. pfs->bcounters[i][j] +=
  602. p->pfik_bytes[i][j][k];
  603. }
  604. }
  605. splx(s);
  606. }
  607. int
  608. pfi_get_ifaces(const char *name, struct pfi_kif *buf, int *size)
  609. {
  610. struct pfi_kif *p, *nextp;
  611. int s, n = 0;
  612. s = splsoftnet();
  613. for (p = RB_MIN(pfi_ifhead, &pfi_ifs); p; p = nextp) {
  614. nextp = RB_NEXT(pfi_ifhead, &pfi_ifs, p);
  615. if (pfi_skip_if(name, p))
  616. continue;
  617. if (*size > n++) {
  618. if (!p->pfik_tzero)
  619. p->pfik_tzero = time_second;
  620. pfi_kif_ref(p, PFI_KIF_REF_RULE);
  621. if (copyout(p, buf++, sizeof(*buf))) {
  622. pfi_kif_unref(p, PFI_KIF_REF_RULE);
  623. splx(s);
  624. return (EFAULT);
  625. }
  626. nextp = RB_NEXT(pfi_ifhead, &pfi_ifs, p);
  627. pfi_kif_unref(p, PFI_KIF_REF_RULE);
  628. }
  629. }
  630. splx(s);
  631. *size = n;
  632. return (0);
  633. }
  634. int
  635. pfi_skip_if(const char *filter, struct pfi_kif *p)
  636. {
  637. struct ifg_list *i;
  638. int n;
  639. if (filter == NULL || !*filter)
  640. return (0);
  641. if (!strcmp(p->pfik_name, filter))
  642. return (0); /* exact match */
  643. n = strlen(filter);
  644. if (n < 1 || n >= IFNAMSIZ)
  645. return (1); /* sanity check */
  646. if (filter[n-1] >= '0' && filter[n-1] <= '9')
  647. return (1); /* group names may not end in a digit */
  648. if (p->pfik_ifp != NULL)
  649. TAILQ_FOREACH(i, &p->pfik_ifp->if_groups, ifgl_next)
  650. if (!strncmp(i->ifgl_group->ifg_group, filter, IFNAMSIZ))
  651. return (0); /* iface is in group "filter" */
  652. return (1);
  653. }
  654. int
  655. pfi_set_flags(const char *name, int flags)
  656. {
  657. struct pfi_kif *p;
  658. int s;
  659. s = splsoftnet();
  660. RB_FOREACH(p, pfi_ifhead, &pfi_ifs) {
  661. if (pfi_skip_if(name, p))
  662. continue;
  663. p->pfik_flags_new = p->pfik_flags | flags;
  664. }
  665. splx(s);
  666. return (0);
  667. }
  668. int
  669. pfi_clear_flags(const char *name, int flags)
  670. {
  671. struct pfi_kif *p;
  672. int s;
  673. s = splsoftnet();
  674. RB_FOREACH(p, pfi_ifhead, &pfi_ifs) {
  675. if (pfi_skip_if(name, p))
  676. continue;
  677. p->pfik_flags_new = p->pfik_flags & ~flags;
  678. }
  679. splx(s);
  680. return (0);
  681. }
  682. void
  683. pfi_xcommit(void)
  684. {
  685. struct pfi_kif *p;
  686. int s;
  687. s = splsoftnet();
  688. RB_FOREACH(p, pfi_ifhead, &pfi_ifs)
  689. p->pfik_flags = p->pfik_flags_new;
  690. splx(s);
  691. }
  692. /* from pf_print_state.c */
  693. int
  694. pfi_unmask(void *addr)
  695. {
  696. struct pf_addr *m = addr;
  697. int i = 31, j = 0, b = 0;
  698. u_int32_t tmp;
  699. while (j < 4 && m->addr32[j] == 0xffffffff) {
  700. b += 32;
  701. j++;
  702. }
  703. if (j < 4) {
  704. tmp = ntohl(m->addr32[j]);
  705. for (i = 31; tmp & (1 << i); --i)
  706. b++;
  707. }
  708. return (b);
  709. }